LCOV - code coverage report
Current view: top level - libavformat - yuv4mpegdec.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 109 241 45.2 %
Date: 2017-12-11 04:34:20 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /*
       2             :  * YUV4MPEG demuxer
       3             :  * Copyright (c) 2001, 2002, 2003 Fabrice Bellard
       4             :  *
       5             :  * This file is part of FFmpeg.
       6             :  *
       7             :  * FFmpeg is free software; you can redistribute it and/or
       8             :  * modify it under the terms of the GNU Lesser General Public
       9             :  * License as published by the Free Software Foundation; either
      10             :  * version 2.1 of the License, or (at your option) any later version.
      11             :  *
      12             :  * FFmpeg is distributed in the hope that it will be useful,
      13             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15             :  * Lesser General Public License for more details.
      16             :  *
      17             :  * You should have received a copy of the GNU Lesser General Public
      18             :  * License along with FFmpeg; if not, write to the Free Software
      19             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      20             :  */
      21             : 
      22             : #include "libavutil/imgutils.h"
      23             : 
      24             : #include "avformat.h"
      25             : #include "internal.h"
      26             : #include "yuv4mpeg.h"
      27             : 
      28             : /* Header size increased to allow room for optional flags */
      29             : #define MAX_YUV4_HEADER 80
      30             : #define MAX_FRAME_HEADER 80
      31             : 
      32           1 : static int yuv4_read_header(AVFormatContext *s)
      33             : {
      34             :     char header[MAX_YUV4_HEADER + 10];  // Include headroom for
      35             :                                         // the longest option
      36             :     char *tokstart, *tokend, *header_end;
      37             :     int i;
      38           1 :     AVIOContext *pb = s->pb;
      39           1 :     int width = -1, height  = -1, raten   = 0,
      40           1 :         rated =  0, aspectn =  0, aspectd = 0;
      41           1 :     enum AVPixelFormat pix_fmt = AV_PIX_FMT_NONE, alt_pix_fmt = AV_PIX_FMT_NONE;
      42           1 :     enum AVChromaLocation chroma_sample_location = AVCHROMA_LOC_UNSPECIFIED;
      43           1 :     enum AVFieldOrder field_order = AV_FIELD_UNKNOWN;
      44             :     AVStream *st;
      45             : 
      46          58 :     for (i = 0; i < MAX_YUV4_HEADER; i++) {
      47          58 :         header[i] = avio_r8(pb);
      48          58 :         if (header[i] == '\n') {
      49           1 :             header[i + 1] = 0x20;  // Add a space after last option.
      50             :                                    // Makes parsing "444" vs "444alpha" easier.
      51           1 :             header[i + 2] = 0;
      52           1 :             break;
      53             :         }
      54             :     }
      55           1 :     if (i == MAX_YUV4_HEADER)
      56           0 :         return -1;
      57           1 :     if (strncmp(header, Y4M_MAGIC, strlen(Y4M_MAGIC)))
      58           0 :         return -1;
      59             : 
      60           1 :     header_end = &header[i + 1]; // Include space
      61           9 :     for (tokstart = &header[strlen(Y4M_MAGIC) + 1];
      62           7 :          tokstart < header_end; tokstart++) {
      63           7 :         if (*tokstart == 0x20)
      64           0 :             continue;
      65           7 :         switch (*tokstart++) {
      66           1 :         case 'W': // Width. Required.
      67           1 :             width    = strtol(tokstart, &tokend, 10);
      68           1 :             tokstart = tokend;
      69           1 :             break;
      70           1 :         case 'H': // Height. Required.
      71           1 :             height   = strtol(tokstart, &tokend, 10);
      72           1 :             tokstart = tokend;
      73           1 :             break;
      74           1 :         case 'C': // Color space
      75           1 :             if (strncmp("420jpeg", tokstart, 7) == 0) {
      76           1 :                 pix_fmt = AV_PIX_FMT_YUV420P;
      77           1 :                 chroma_sample_location = AVCHROMA_LOC_CENTER;
      78           0 :             } else if (strncmp("420mpeg2", tokstart, 8) == 0) {
      79           0 :                 pix_fmt = AV_PIX_FMT_YUV420P;
      80           0 :                 chroma_sample_location = AVCHROMA_LOC_LEFT;
      81           0 :             } else if (strncmp("420paldv", tokstart, 8) == 0) {
      82           0 :                 pix_fmt = AV_PIX_FMT_YUV420P;
      83           0 :                 chroma_sample_location = AVCHROMA_LOC_TOPLEFT;
      84           0 :             } else if (strncmp("420p16", tokstart, 6) == 0) {
      85           0 :                 pix_fmt = AV_PIX_FMT_YUV420P16;
      86           0 :             } else if (strncmp("422p16", tokstart, 6) == 0) {
      87           0 :                 pix_fmt = AV_PIX_FMT_YUV422P16;
      88           0 :             } else if (strncmp("444p16", tokstart, 6) == 0) {
      89           0 :                 pix_fmt = AV_PIX_FMT_YUV444P16;
      90           0 :             } else if (strncmp("420p14", tokstart, 6) == 0) {
      91           0 :                 pix_fmt = AV_PIX_FMT_YUV420P14;
      92           0 :             } else if (strncmp("422p14", tokstart, 6) == 0) {
      93           0 :                 pix_fmt = AV_PIX_FMT_YUV422P14;
      94           0 :             } else if (strncmp("444p14", tokstart, 6) == 0) {
      95           0 :                 pix_fmt = AV_PIX_FMT_YUV444P14;
      96           0 :             } else if (strncmp("420p12", tokstart, 6) == 0) {
      97           0 :                 pix_fmt = AV_PIX_FMT_YUV420P12;
      98           0 :             } else if (strncmp("422p12", tokstart, 6) == 0) {
      99           0 :                 pix_fmt = AV_PIX_FMT_YUV422P12;
     100           0 :             } else if (strncmp("444p12", tokstart, 6) == 0) {
     101           0 :                 pix_fmt = AV_PIX_FMT_YUV444P12;
     102           0 :             } else if (strncmp("420p10", tokstart, 6) == 0) {
     103           0 :                 pix_fmt = AV_PIX_FMT_YUV420P10;
     104           0 :             } else if (strncmp("422p10", tokstart, 6) == 0) {
     105           0 :                 pix_fmt = AV_PIX_FMT_YUV422P10;
     106           0 :             } else if (strncmp("444p10", tokstart, 6) == 0) {
     107           0 :                 pix_fmt = AV_PIX_FMT_YUV444P10;
     108           0 :             } else if (strncmp("420p9", tokstart, 5) == 0) {
     109           0 :                 pix_fmt = AV_PIX_FMT_YUV420P9;
     110           0 :             } else if (strncmp("422p9", tokstart, 5) == 0) {
     111           0 :                 pix_fmt = AV_PIX_FMT_YUV422P9;
     112           0 :             } else if (strncmp("444p9", tokstart, 5) == 0) {
     113           0 :                 pix_fmt = AV_PIX_FMT_YUV444P9;
     114           0 :             } else if (strncmp("420", tokstart, 3) == 0) {
     115           0 :                 pix_fmt = AV_PIX_FMT_YUV420P;
     116           0 :                 chroma_sample_location = AVCHROMA_LOC_CENTER;
     117           0 :             } else if (strncmp("411", tokstart, 3) == 0) {
     118           0 :                 pix_fmt = AV_PIX_FMT_YUV411P;
     119           0 :             } else if (strncmp("422", tokstart, 3) == 0) {
     120           0 :                 pix_fmt = AV_PIX_FMT_YUV422P;
     121           0 :             } else if (strncmp("444alpha", tokstart, 8) == 0 ) {
     122           0 :                 av_log(s, AV_LOG_ERROR, "Cannot handle 4:4:4:4 "
     123             :                        "YUV4MPEG stream.\n");
     124           0 :                 return -1;
     125           0 :             } else if (strncmp("444", tokstart, 3) == 0) {
     126           0 :                 pix_fmt = AV_PIX_FMT_YUV444P;
     127           0 :             } else if (strncmp("mono16", tokstart, 6) == 0) {
     128           0 :                 pix_fmt = AV_PIX_FMT_GRAY16;
     129           0 :             } else if (strncmp("mono12", tokstart, 6) == 0) {
     130           0 :                 pix_fmt = AV_PIX_FMT_GRAY12;
     131           0 :             } else if (strncmp("mono10", tokstart, 6) == 0) {
     132           0 :                 pix_fmt = AV_PIX_FMT_GRAY10;
     133           0 :             } else if (strncmp("mono9", tokstart, 5) == 0) {
     134           0 :                 pix_fmt = AV_PIX_FMT_GRAY9;
     135           0 :             } else if (strncmp("mono", tokstart, 4) == 0) {
     136           0 :                 pix_fmt = AV_PIX_FMT_GRAY8;
     137             :             } else {
     138           0 :                 av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains an unknown "
     139             :                        "pixel format.\n");
     140           0 :                 return -1;
     141             :             }
     142           9 :             while (tokstart < header_end && *tokstart != 0x20)
     143           7 :                 tokstart++;
     144           1 :             break;
     145           1 :         case 'I': // Interlace type
     146           1 :             switch (*tokstart++){
     147           0 :             case '?':
     148           0 :                 field_order = AV_FIELD_UNKNOWN;
     149           0 :                 break;
     150           1 :             case 'p':
     151           1 :                 field_order = AV_FIELD_PROGRESSIVE;
     152           1 :                 break;
     153           0 :             case 't':
     154           0 :                 field_order = AV_FIELD_TT;
     155           0 :                 break;
     156           0 :             case 'b':
     157           0 :                 field_order = AV_FIELD_BB;
     158           0 :                 break;
     159           0 :             case 'm':
     160           0 :                 av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains mixed "
     161             :                        "interlaced and non-interlaced frames.\n");
     162           0 :             default:
     163           0 :                 av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
     164           0 :                 return AVERROR(EINVAL);
     165             :             }
     166           1 :             break;
     167           1 :         case 'F': // Frame rate
     168           1 :             sscanf(tokstart, "%d:%d", &raten, &rated); // 0:0 if unknown
     169           6 :             while (tokstart < header_end && *tokstart != 0x20)
     170           4 :                 tokstart++;
     171           1 :             break;
     172           1 :         case 'A': // Pixel aspect
     173           1 :             sscanf(tokstart, "%d:%d", &aspectn, &aspectd); // 0:0 if unknown
     174           5 :             while (tokstart < header_end && *tokstart != 0x20)
     175           3 :                 tokstart++;
     176           1 :             break;
     177           1 :         case 'X': // Vendor extensions
     178           1 :             if (strncmp("YSCSS=", tokstart, 6) == 0) {
     179             :                 // Older nonstandard pixel format representation
     180           1 :                 tokstart += 6;
     181           1 :                 if (strncmp("420JPEG", tokstart, 7) == 0)
     182           1 :                     alt_pix_fmt = AV_PIX_FMT_YUV420P;
     183           0 :                 else if (strncmp("420MPEG2", tokstart, 8) == 0)
     184           0 :                     alt_pix_fmt = AV_PIX_FMT_YUV420P;
     185           0 :                 else if (strncmp("420PALDV", tokstart, 8) == 0)
     186           0 :                     alt_pix_fmt = AV_PIX_FMT_YUV420P;
     187           0 :                 else if (strncmp("420P9", tokstart, 5) == 0)
     188           0 :                     alt_pix_fmt = AV_PIX_FMT_YUV420P9;
     189           0 :                 else if (strncmp("422P9", tokstart, 5) == 0)
     190           0 :                     alt_pix_fmt = AV_PIX_FMT_YUV422P9;
     191           0 :                 else if (strncmp("444P9", tokstart, 5) == 0)
     192           0 :                     alt_pix_fmt = AV_PIX_FMT_YUV444P9;
     193           0 :                 else if (strncmp("420P10", tokstart, 6) == 0)
     194           0 :                     alt_pix_fmt = AV_PIX_FMT_YUV420P10;
     195           0 :                 else if (strncmp("422P10", tokstart, 6) == 0)
     196           0 :                     alt_pix_fmt = AV_PIX_FMT_YUV422P10;
     197           0 :                 else if (strncmp("444P10", tokstart, 6) == 0)
     198           0 :                     alt_pix_fmt = AV_PIX_FMT_YUV444P10;
     199           0 :                 else if (strncmp("420P12", tokstart, 6) == 0)
     200           0 :                     alt_pix_fmt = AV_PIX_FMT_YUV420P12;
     201           0 :                 else if (strncmp("422P12", tokstart, 6) == 0)
     202           0 :                     alt_pix_fmt = AV_PIX_FMT_YUV422P12;
     203           0 :                 else if (strncmp("444P12", tokstart, 6) == 0)
     204           0 :                     alt_pix_fmt = AV_PIX_FMT_YUV444P12;
     205           0 :                 else if (strncmp("420P14", tokstart, 6) == 0)
     206           0 :                     alt_pix_fmt = AV_PIX_FMT_YUV420P14;
     207           0 :                 else if (strncmp("422P14", tokstart, 6) == 0)
     208           0 :                     alt_pix_fmt = AV_PIX_FMT_YUV422P14;
     209           0 :                 else if (strncmp("444P14", tokstart, 6) == 0)
     210           0 :                     alt_pix_fmt = AV_PIX_FMT_YUV444P14;
     211           0 :                 else if (strncmp("420P16", tokstart, 6) == 0)
     212           0 :                     alt_pix_fmt = AV_PIX_FMT_YUV420P16;
     213           0 :                 else if (strncmp("422P16", tokstart, 6) == 0)
     214           0 :                     alt_pix_fmt = AV_PIX_FMT_YUV422P16;
     215           0 :                 else if (strncmp("444P16", tokstart, 6) == 0)
     216           0 :                     alt_pix_fmt = AV_PIX_FMT_YUV444P16;
     217           0 :                 else if (strncmp("411", tokstart, 3) == 0)
     218           0 :                     alt_pix_fmt = AV_PIX_FMT_YUV411P;
     219           0 :                 else if (strncmp("422", tokstart, 3) == 0)
     220           0 :                     alt_pix_fmt = AV_PIX_FMT_YUV422P;
     221           0 :                 else if (strncmp("444", tokstart, 3) == 0)
     222           0 :                     alt_pix_fmt = AV_PIX_FMT_YUV444P;
     223             :             }
     224          10 :             while (tokstart < header_end && *tokstart != 0x20)
     225           8 :                 tokstart++;
     226           1 :             break;
     227             :         }
     228             :     }
     229             : 
     230           1 :     if (width == -1 || height == -1) {
     231           0 :         av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
     232           0 :         return -1;
     233             :     }
     234             : 
     235           1 :     if (pix_fmt == AV_PIX_FMT_NONE) {
     236           0 :         if (alt_pix_fmt == AV_PIX_FMT_NONE)
     237           0 :             pix_fmt = AV_PIX_FMT_YUV420P;
     238             :         else
     239           0 :             pix_fmt = alt_pix_fmt;
     240             :     }
     241             : 
     242           1 :     if (raten <= 0 || rated <= 0) {
     243             :         // Frame rate unknown
     244           0 :         raten = 25;
     245           0 :         rated = 1;
     246             :     }
     247             : 
     248           1 :     if (aspectn == 0 && aspectd == 0) {
     249             :         // Pixel aspect unknown
     250           1 :         aspectd = 1;
     251             :     }
     252             : 
     253           1 :     st = avformat_new_stream(s, NULL);
     254           1 :     if (!st)
     255           0 :         return AVERROR(ENOMEM);
     256           1 :     st->codecpar->width  = width;
     257           1 :     st->codecpar->height = height;
     258           1 :     av_reduce(&raten, &rated, raten, rated, (1UL << 31) - 1);
     259           1 :     avpriv_set_pts_info(st, 64, rated, raten);
     260           1 :     st->avg_frame_rate                = av_inv_q(st->time_base);
     261           1 :     st->codecpar->format              = pix_fmt;
     262           1 :     st->codecpar->codec_type          = AVMEDIA_TYPE_VIDEO;
     263           1 :     st->codecpar->codec_id            = AV_CODEC_ID_RAWVIDEO;
     264           1 :     st->sample_aspect_ratio           = (AVRational){ aspectn, aspectd };
     265           1 :     st->codecpar->chroma_location     = chroma_sample_location;
     266           1 :     st->codecpar->field_order         = field_order;
     267           1 :     s->packet_size = av_image_get_buffer_size(st->codecpar->format, width, height, 1) + Y4M_FRAME_MAGIC_LEN;
     268           1 :     if ((int) s->packet_size < 0)
     269           0 :         return s->packet_size;
     270           1 :     s->internal->data_offset = avio_tell(pb);
     271             : 
     272           1 :     st->duration = (avio_size(pb) - avio_tell(pb)) / s->packet_size;
     273             : 
     274           1 :     return 0;
     275             : }
     276             : 
     277          27 : static int yuv4_read_packet(AVFormatContext *s, AVPacket *pkt)
     278             : {
     279             :     int i;
     280             :     char header[MAX_FRAME_HEADER+1];
     281             :     int ret;
     282          27 :     int64_t off = avio_tell(s->pb);
     283             : 
     284        1062 :     for (i = 0; i < MAX_FRAME_HEADER; i++) {
     285        1050 :         header[i] = avio_r8(s->pb);
     286        1050 :         if (header[i] == '\n') {
     287          15 :             header[i + 1] = 0;
     288          15 :             break;
     289             :         }
     290             :     }
     291          27 :     if (s->pb->error)
     292           0 :         return s->pb->error;
     293          27 :     else if (s->pb->eof_reached)
     294          12 :         return AVERROR_EOF;
     295          15 :     else if (i == MAX_FRAME_HEADER)
     296           0 :         return AVERROR_INVALIDDATA;
     297             : 
     298          15 :     if (strncmp(header, Y4M_FRAME_MAGIC, strlen(Y4M_FRAME_MAGIC)))
     299           0 :         return AVERROR_INVALIDDATA;
     300             : 
     301          15 :     ret = av_get_packet(s->pb, pkt, s->packet_size - Y4M_FRAME_MAGIC_LEN);
     302          15 :     if (ret < 0)
     303           0 :         return ret;
     304          15 :     else if (ret != s->packet_size - Y4M_FRAME_MAGIC_LEN) {
     305           0 :         av_packet_unref(pkt);
     306           0 :         return s->pb->eof_reached ? AVERROR_EOF : AVERROR(EIO);
     307             :     }
     308          15 :     pkt->stream_index = 0;
     309          15 :     pkt->pts = (off - s->internal->data_offset) / s->packet_size;
     310          15 :     pkt->duration = 1;
     311          15 :     return 0;
     312             : }
     313             : 
     314          26 : static int yuv4_read_seek(AVFormatContext *s, int stream_index,
     315             :                           int64_t pts, int flags)
     316             : {
     317          26 :     if (avio_seek(s->pb, pts * s->packet_size + s->internal->data_offset, SEEK_SET) < 0)
     318           8 :         return -1;
     319          18 :     return 0;
     320             : }
     321             : 
     322        6130 : static int yuv4_probe(AVProbeData *pd)
     323             : {
     324             :     /* check file header */
     325        6130 :     if (strncmp(pd->buf, Y4M_MAGIC, sizeof(Y4M_MAGIC) - 1) == 0)
     326           1 :         return AVPROBE_SCORE_MAX;
     327             :     else
     328        6129 :         return 0;
     329             : }
     330             : 
     331             : AVInputFormat ff_yuv4mpegpipe_demuxer = {
     332             :     .name           = "yuv4mpegpipe",
     333             :     .long_name      = NULL_IF_CONFIG_SMALL("YUV4MPEG pipe"),
     334             :     .read_probe     = yuv4_probe,
     335             :     .read_header    = yuv4_read_header,
     336             :     .read_packet    = yuv4_read_packet,
     337             :     .read_seek      = yuv4_read_seek,
     338             :     .extensions     = "y4m",
     339             : };

Generated by: LCOV version 1.13