• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavformat/yuv4mpeg.c

Go to the documentation of this file.
00001 /*
00002  * YUV4MPEG format
00003  * Copyright (c) 2001, 2002, 2003 Fabrice Bellard
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * FFmpeg is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 #include "avformat.h"
00022 
00023 #define Y4M_MAGIC "YUV4MPEG2"
00024 #define Y4M_FRAME_MAGIC "FRAME"
00025 #define Y4M_LINE_MAX 256
00026 
00027 struct frame_attributes {
00028     int interlaced_frame;
00029     int top_field_first;
00030 };
00031 
00032 #if CONFIG_YUV4MPEGPIPE_MUXER
00033 static int yuv4_generate_header(AVFormatContext *s, char* buf)
00034 {
00035     AVStream *st;
00036     int width, height;
00037     int raten, rated, aspectn, aspectd, n;
00038     char inter;
00039     const char *colorspace = "";
00040 
00041     st = s->streams[0];
00042     width = st->codec->width;
00043     height = st->codec->height;
00044 
00045     av_reduce(&raten, &rated, st->codec->time_base.den, st->codec->time_base.num, (1UL<<31)-1);
00046 
00047     aspectn = st->sample_aspect_ratio.num;
00048     aspectd = st->sample_aspect_ratio.den;
00049 
00050     if ( aspectn == 0 && aspectd == 1 ) aspectd = 0;  // 0:0 means unknown
00051 
00052     inter = 'p'; /* progressive is the default */
00053     if (st->codec->coded_frame && st->codec->coded_frame->interlaced_frame) {
00054         inter = st->codec->coded_frame->top_field_first ? 't' : 'b';
00055     }
00056 
00057     switch(st->codec->pix_fmt) {
00058     case PIX_FMT_GRAY8:
00059         colorspace = " Cmono";
00060         break;
00061     case PIX_FMT_YUV411P:
00062         colorspace = " C411 XYSCSS=411";
00063         break;
00064     case PIX_FMT_YUV420P:
00065         colorspace = (st->codec->codec_id == CODEC_ID_DVVIDEO)?" C420paldv XYSCSS=420PALDV":" C420mpeg2 XYSCSS=420MPEG2";
00066         break;
00067     case PIX_FMT_YUV422P:
00068         colorspace = " C422 XYSCSS=422";
00069         break;
00070     case PIX_FMT_YUV444P:
00071         colorspace = " C444 XYSCSS=444";
00072         break;
00073     }
00074 
00075     /* construct stream header, if this is the first frame */
00076     n = snprintf(buf, Y4M_LINE_MAX, "%s W%d H%d F%d:%d I%c A%d:%d%s\n",
00077                  Y4M_MAGIC,
00078                  width,
00079                  height,
00080                  raten, rated,
00081                  inter,
00082                  aspectn, aspectd,
00083                  colorspace);
00084 
00085     return n;
00086 }
00087 
00088 static int yuv4_write_packet(AVFormatContext *s, AVPacket *pkt)
00089 {
00090     AVStream *st = s->streams[pkt->stream_index];
00091     ByteIOContext *pb = s->pb;
00092     AVPicture *picture;
00093     int* first_pkt = s->priv_data;
00094     int width, height, h_chroma_shift, v_chroma_shift;
00095     int i, m;
00096     char buf2[Y4M_LINE_MAX+1];
00097     char buf1[20];
00098     uint8_t *ptr, *ptr1, *ptr2;
00099 
00100     picture = (AVPicture *)pkt->data;
00101 
00102     /* for the first packet we have to output the header as well */
00103     if (*first_pkt) {
00104         *first_pkt = 0;
00105         if (yuv4_generate_header(s, buf2) < 0) {
00106             av_log(s, AV_LOG_ERROR, "Error. YUV4MPEG stream header write failed.\n");
00107             return AVERROR(EIO);
00108         } else {
00109             put_buffer(pb, buf2, strlen(buf2));
00110         }
00111     }
00112 
00113     /* construct frame header */
00114 
00115     m = snprintf(buf1, sizeof(buf1), "%s\n", Y4M_FRAME_MAGIC);
00116     put_buffer(pb, buf1, strlen(buf1));
00117 
00118     width = st->codec->width;
00119     height = st->codec->height;
00120 
00121     ptr = picture->data[0];
00122     for(i=0;i<height;i++) {
00123         put_buffer(pb, ptr, width);
00124         ptr += picture->linesize[0];
00125     }
00126 
00127     if (st->codec->pix_fmt != PIX_FMT_GRAY8){
00128     // Adjust for smaller Cb and Cr planes
00129     avcodec_get_chroma_sub_sample(st->codec->pix_fmt, &h_chroma_shift, &v_chroma_shift);
00130     width >>= h_chroma_shift;
00131     height >>= v_chroma_shift;
00132 
00133     ptr1 = picture->data[1];
00134     ptr2 = picture->data[2];
00135     for(i=0;i<height;i++) {     /* Cb */
00136         put_buffer(pb, ptr1, width);
00137         ptr1 += picture->linesize[1];
00138     }
00139     for(i=0;i<height;i++) {     /* Cr */
00140         put_buffer(pb, ptr2, width);
00141             ptr2 += picture->linesize[2];
00142     }
00143     }
00144     put_flush_packet(pb);
00145     return 0;
00146 }
00147 
00148 static int yuv4_write_header(AVFormatContext *s)
00149 {
00150     int* first_pkt = s->priv_data;
00151 
00152     if (s->nb_streams != 1)
00153         return AVERROR(EIO);
00154 
00155     if (s->streams[0]->codec->codec_id != CODEC_ID_RAWVIDEO) {
00156         av_log(s, AV_LOG_ERROR, "ERROR: Only rawvideo supported.\n");
00157         return AVERROR_INVALIDDATA;
00158     }
00159 
00160     if (s->streams[0]->codec->pix_fmt == PIX_FMT_YUV411P) {
00161         av_log(s, AV_LOG_ERROR, "Warning: generating rarely used 4:1:1 YUV stream, some mjpegtools might not work.\n");
00162     }
00163     else if ((s->streams[0]->codec->pix_fmt != PIX_FMT_YUV420P) &&
00164              (s->streams[0]->codec->pix_fmt != PIX_FMT_YUV422P) &&
00165              (s->streams[0]->codec->pix_fmt != PIX_FMT_GRAY8) &&
00166              (s->streams[0]->codec->pix_fmt != PIX_FMT_YUV444P)) {
00167         av_log(s, AV_LOG_ERROR, "ERROR: yuv4mpeg only handles yuv444p, yuv422p, yuv420p, yuv411p and gray pixel formats. Use -pix_fmt to select one.\n");
00168         return AVERROR(EIO);
00169     }
00170 
00171     *first_pkt = 1;
00172     return 0;
00173 }
00174 
00175 AVOutputFormat yuv4mpegpipe_muxer = {
00176     "yuv4mpegpipe",
00177     NULL_IF_CONFIG_SMALL("YUV4MPEG pipe format"),
00178     "",
00179     "y4m",
00180     sizeof(int),
00181     CODEC_ID_NONE,
00182     CODEC_ID_RAWVIDEO,
00183     yuv4_write_header,
00184     yuv4_write_packet,
00185     .flags = AVFMT_RAWPICTURE,
00186 };
00187 #endif
00188 
00189 /* Header size increased to allow room for optional flags */
00190 #define MAX_YUV4_HEADER 80
00191 #define MAX_FRAME_HEADER 80
00192 
00193 static int yuv4_read_header(AVFormatContext *s, AVFormatParameters *ap)
00194 {
00195     char header[MAX_YUV4_HEADER+10];  // Include headroom for the longest option
00196     char *tokstart,*tokend,*header_end;
00197     int i;
00198     ByteIOContext *pb = s->pb;
00199     int width=-1, height=-1, raten=0, rated=0, aspectn=0, aspectd=0;
00200     enum PixelFormat pix_fmt=PIX_FMT_NONE,alt_pix_fmt=PIX_FMT_NONE;
00201     AVStream *st;
00202     struct frame_attributes *s1 = s->priv_data;
00203 
00204     for (i=0; i<MAX_YUV4_HEADER; i++) {
00205         header[i] = get_byte(pb);
00206         if (header[i] == '\n') {
00207             header[i+1] = 0x20;  // Add a space after last option. Makes parsing "444" vs "444alpha" easier.
00208             header[i+2] = 0;
00209             break;
00210         }
00211     }
00212     if (i == MAX_YUV4_HEADER) return -1;
00213     if (strncmp(header, Y4M_MAGIC, strlen(Y4M_MAGIC))) return -1;
00214 
00215     s1->interlaced_frame = 0;
00216     s1->top_field_first = 0;
00217     header_end = &header[i+1]; // Include space
00218     for(tokstart = &header[strlen(Y4M_MAGIC) + 1]; tokstart < header_end; tokstart++) {
00219         if (*tokstart==0x20) continue;
00220         switch (*tokstart++) {
00221         case 'W': // Width. Required.
00222             width = strtol(tokstart, &tokend, 10);
00223             tokstart=tokend;
00224             break;
00225         case 'H': // Height. Required.
00226             height = strtol(tokstart, &tokend, 10);
00227             tokstart=tokend;
00228             break;
00229         case 'C': // Color space
00230             if (strncmp("420jpeg",tokstart,7)==0)
00231                 pix_fmt = PIX_FMT_YUV420P;
00232             else if (strncmp("420mpeg2",tokstart,8)==0)
00233                 pix_fmt = PIX_FMT_YUV420P;
00234             else if (strncmp("420paldv", tokstart, 8)==0)
00235                 pix_fmt = PIX_FMT_YUV420P;
00236             else if (strncmp("411", tokstart, 3)==0)
00237                 pix_fmt = PIX_FMT_YUV411P;
00238             else if (strncmp("422", tokstart, 3)==0)
00239                 pix_fmt = PIX_FMT_YUV422P;
00240             else if (strncmp("444alpha", tokstart, 8)==0) {
00241                 av_log(s, AV_LOG_ERROR, "Cannot handle 4:4:4:4 YUV4MPEG stream.\n");
00242                 return -1;
00243             } else if (strncmp("444", tokstart, 3)==0)
00244                 pix_fmt = PIX_FMT_YUV444P;
00245             else if (strncmp("mono",tokstart, 4)==0) {
00246                 pix_fmt = PIX_FMT_GRAY8;
00247             } else {
00248                 av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains an unknown pixel format.\n");
00249                 return -1;
00250             }
00251             while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
00252             break;
00253         case 'I': // Interlace type
00254             switch (*tokstart++){
00255             case '?':
00256                 break;
00257             case 'p':
00258                 s1->interlaced_frame=0;
00259                 break;
00260             case 't':
00261                 s1->interlaced_frame=1;
00262                 s1->top_field_first=1;
00263                 break;
00264             case 'b':
00265                 s1->interlaced_frame=1;
00266                 s1->top_field_first=0;
00267                 break;
00268             case 'm':
00269                 av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains mixed interlaced and non-interlaced frames.\n");
00270                 return -1;
00271             default:
00272                 av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
00273                 return -1;
00274             }
00275             break;
00276         case 'F': // Frame rate
00277             sscanf(tokstart,"%d:%d",&raten,&rated); // 0:0 if unknown
00278             while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
00279             break;
00280         case 'A': // Pixel aspect
00281             sscanf(tokstart,"%d:%d",&aspectn,&aspectd); // 0:0 if unknown
00282             while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
00283             break;
00284         case 'X': // Vendor extensions
00285             if (strncmp("YSCSS=",tokstart,6)==0) {
00286                 // Older nonstandard pixel format representation
00287                 tokstart+=6;
00288                 if (strncmp("420JPEG",tokstart,7)==0)
00289                     alt_pix_fmt=PIX_FMT_YUV420P;
00290                 else if (strncmp("420MPEG2",tokstart,8)==0)
00291                     alt_pix_fmt=PIX_FMT_YUV420P;
00292                 else if (strncmp("420PALDV",tokstart,8)==0)
00293                     alt_pix_fmt=PIX_FMT_YUV420P;
00294                 else if (strncmp("411",tokstart,3)==0)
00295                     alt_pix_fmt=PIX_FMT_YUV411P;
00296                 else if (strncmp("422",tokstart,3)==0)
00297                     alt_pix_fmt=PIX_FMT_YUV422P;
00298                 else if (strncmp("444",tokstart,3)==0)
00299                     alt_pix_fmt=PIX_FMT_YUV444P;
00300             }
00301             while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
00302             break;
00303         }
00304     }
00305 
00306     if ((width == -1) || (height == -1)) {
00307         av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
00308         return -1;
00309     }
00310 
00311     if (pix_fmt == PIX_FMT_NONE) {
00312         if (alt_pix_fmt == PIX_FMT_NONE)
00313             pix_fmt = PIX_FMT_YUV420P;
00314         else
00315             pix_fmt = alt_pix_fmt;
00316     }
00317 
00318     if (raten == 0 && rated == 0) {
00319         // Frame rate unknown
00320         raten = 25;
00321         rated = 1;
00322     }
00323 
00324     if (aspectn == 0 && aspectd == 0) {
00325         // Pixel aspect unknown
00326         aspectd = 1;
00327     }
00328 
00329     st = av_new_stream(s, 0);
00330     if(!st)
00331         return AVERROR(ENOMEM);
00332     st->codec->width = width;
00333     st->codec->height = height;
00334     av_reduce(&raten, &rated, raten, rated, (1UL<<31)-1);
00335     av_set_pts_info(st, 64, rated, raten);
00336     st->codec->pix_fmt = pix_fmt;
00337     st->codec->codec_type = CODEC_TYPE_VIDEO;
00338     st->codec->codec_id = CODEC_ID_RAWVIDEO;
00339     st->sample_aspect_ratio= (AVRational){aspectn, aspectd};
00340 
00341     return 0;
00342 }
00343 
00344 static int yuv4_read_packet(AVFormatContext *s, AVPacket *pkt)
00345 {
00346     int i;
00347     char header[MAX_FRAME_HEADER+1];
00348     int packet_size, width, height, ret;
00349     AVStream *st = s->streams[0];
00350     struct frame_attributes *s1 = s->priv_data;
00351 
00352     for (i=0; i<MAX_FRAME_HEADER; i++) {
00353         header[i] = get_byte(s->pb);
00354         if (header[i] == '\n') {
00355             header[i+1] = 0;
00356             break;
00357         }
00358     }
00359     if (s->pb->error)
00360         return s->pb->error;
00361     else if (s->pb->eof_reached)
00362         return AVERROR_EOF;
00363     else if (i == MAX_FRAME_HEADER)
00364         return AVERROR_INVALIDDATA;
00365 
00366     if (strncmp(header, Y4M_FRAME_MAGIC, strlen(Y4M_FRAME_MAGIC)))
00367         return AVERROR_INVALIDDATA;
00368 
00369     width = st->codec->width;
00370     height = st->codec->height;
00371 
00372     packet_size = avpicture_get_size(st->codec->pix_fmt, width, height);
00373     if (packet_size < 0)
00374         return packet_size;
00375 
00376     ret = av_get_packet(s->pb, pkt, packet_size);
00377     if (ret < 0)
00378         return ret;
00379     else if (ret != packet_size)
00380         return s->pb->eof_reached ? AVERROR_EOF : AVERROR(EIO);
00381 
00382     if (s->streams[0]->codec->coded_frame) {
00383         s->streams[0]->codec->coded_frame->interlaced_frame = s1->interlaced_frame;
00384         s->streams[0]->codec->coded_frame->top_field_first = s1->top_field_first;
00385     }
00386 
00387     pkt->stream_index = 0;
00388     return 0;
00389 }
00390 
00391 static int yuv4_probe(AVProbeData *pd)
00392 {
00393     /* check file header */
00394     if (strncmp(pd->buf, Y4M_MAGIC, sizeof(Y4M_MAGIC)-1)==0)
00395         return AVPROBE_SCORE_MAX;
00396     else
00397         return 0;
00398 }
00399 
00400 #if CONFIG_YUV4MPEGPIPE_DEMUXER
00401 AVInputFormat yuv4mpegpipe_demuxer = {
00402     "yuv4mpegpipe",
00403     NULL_IF_CONFIG_SMALL("YUV4MPEG pipe format"),
00404     sizeof(struct frame_attributes),
00405     yuv4_probe,
00406     yuv4_read_header,
00407     yuv4_read_packet,
00408     .extensions = "y4m"
00409 };
00410 #endif

Generated on Sat Feb 16 2013 09:23:15 for ffmpeg by  doxygen 1.7.1