• 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 #include "internal.h"
00023 
00024 #define Y4M_MAGIC "YUV4MPEG2"
00025 #define Y4M_FRAME_MAGIC "FRAME"
00026 #define Y4M_LINE_MAX 256
00027 
00028 struct frame_attributes {
00029     int interlaced_frame;
00030     int top_field_first;
00031 };
00032 
00033 #if CONFIG_YUV4MPEGPIPE_MUXER
00034 static int yuv4_generate_header(AVFormatContext *s, char* buf)
00035 {
00036     AVStream *st;
00037     int width, height;
00038     int raten, rated, aspectn, aspectd, n;
00039     char inter;
00040     const char *colorspace = "";
00041 
00042     st     = s->streams[0];
00043     width  = st->codec->width;
00044     height = st->codec->height;
00045 
00046     av_reduce(&raten, &rated, st->codec->time_base.den,
00047               st->codec->time_base.num, (1UL << 31) - 1);
00048 
00049     aspectn = st->sample_aspect_ratio.num;
00050     aspectd = st->sample_aspect_ratio.den;
00051 
00052     if (aspectn == 0 && aspectd == 1)
00053         aspectd = 0;  // 0:0 means unknown
00054 
00055     inter = 'p'; /* progressive is the default */
00056     if (st->codec->coded_frame && st->codec->coded_frame->interlaced_frame)
00057         inter = st->codec->coded_frame->top_field_first ? 't' : 'b';
00058 
00059     switch (st->codec->pix_fmt) {
00060     case PIX_FMT_GRAY8:
00061         colorspace = " Cmono";
00062         break;
00063     case PIX_FMT_YUV411P:
00064         colorspace = " C411 XYSCSS=411";
00065         break;
00066     case PIX_FMT_YUV420P:
00067         switch (st->codec->chroma_sample_location) {
00068         case AVCHROMA_LOC_TOPLEFT: colorspace = " C420paldv XYSCSS=420PALDV"; break;
00069         case AVCHROMA_LOC_LEFT:    colorspace = " C420mpeg2 XYSCSS=420MPEG2"; break;
00070         default:                   colorspace = " C420jpeg XYSCSS=420JPEG";   break;
00071         }
00072         break;
00073     case PIX_FMT_YUV422P:
00074         colorspace = " C422 XYSCSS=422";
00075         break;
00076     case PIX_FMT_YUV444P:
00077         colorspace = " C444 XYSCSS=444";
00078         break;
00079     }
00080 
00081     /* construct stream header, if this is the first frame */
00082     n = snprintf(buf, Y4M_LINE_MAX, "%s W%d H%d F%d:%d I%c A%d:%d%s\n",
00083                  Y4M_MAGIC, width, height, raten, rated, inter,
00084                  aspectn, aspectd, colorspace);
00085 
00086     return n;
00087 }
00088 
00089 static int yuv4_write_packet(AVFormatContext *s, AVPacket *pkt)
00090 {
00091     AVStream *st = s->streams[pkt->stream_index];
00092     AVIOContext *pb = s->pb;
00093     AVPicture *picture;
00094     int* first_pkt = s->priv_data;
00095     int width, height, h_chroma_shift, v_chroma_shift;
00096     int i;
00097     char buf2[Y4M_LINE_MAX + 1];
00098     char buf1[20];
00099     uint8_t *ptr, *ptr1, *ptr2;
00100 
00101     picture = (AVPicture *)pkt->data;
00102 
00103     /* for the first packet we have to output the header as well */
00104     if (*first_pkt) {
00105         *first_pkt = 0;
00106         if (yuv4_generate_header(s, buf2) < 0) {
00107             av_log(s, AV_LOG_ERROR,
00108                    "Error. YUV4MPEG stream header write failed.\n");
00109             return AVERROR(EIO);
00110         } else {
00111             avio_write(pb, buf2, strlen(buf2));
00112         }
00113     }
00114 
00115     /* construct frame header */
00116 
00117     snprintf(buf1, sizeof(buf1), "%s\n", Y4M_FRAME_MAGIC);
00118     avio_write(pb, buf1, strlen(buf1));
00119 
00120     width  = st->codec->width;
00121     height = st->codec->height;
00122 
00123     ptr = picture->data[0];
00124     for (i = 0; i < height; i++) {
00125         avio_write(pb, ptr, width);
00126         ptr += picture->linesize[0];
00127     }
00128 
00129     if (st->codec->pix_fmt != PIX_FMT_GRAY8) {
00130         // Adjust for smaller Cb and Cr planes
00131         avcodec_get_chroma_sub_sample(st->codec->pix_fmt, &h_chroma_shift,
00132                                       &v_chroma_shift);
00133         width  >>= h_chroma_shift;
00134         height >>= v_chroma_shift;
00135 
00136         ptr1 = picture->data[1];
00137         ptr2 = picture->data[2];
00138         for (i = 0; i < height; i++) {     /* Cb */
00139             avio_write(pb, ptr1, width);
00140             ptr1 += picture->linesize[1];
00141         }
00142         for (i = 0; i < height; i++) {     /* Cr */
00143             avio_write(pb, ptr2, width);
00144             ptr2 += picture->linesize[2];
00145         }
00146     }
00147     avio_flush(pb);
00148     return 0;
00149 }
00150 
00151 static int yuv4_write_header(AVFormatContext *s)
00152 {
00153     int *first_pkt = s->priv_data;
00154 
00155     if (s->nb_streams != 1)
00156         return AVERROR(EIO);
00157 
00158     if (s->streams[0]->codec->codec_id != CODEC_ID_RAWVIDEO) {
00159         av_log(s, AV_LOG_ERROR,
00160                "A non-rawvideo stream was selected, but yuv4mpeg only handles rawvideo streams\n");
00161         return AVERROR(EINVAL);
00162     }
00163 
00164     if (s->streams[0]->codec->pix_fmt == PIX_FMT_YUV411P) {
00165         av_log(s, AV_LOG_ERROR, "Warning: generating rarely used 4:1:1 YUV "
00166                "stream, some mjpegtools might not work.\n");
00167     } else if ((s->streams[0]->codec->pix_fmt != PIX_FMT_YUV420P) &&
00168                (s->streams[0]->codec->pix_fmt != PIX_FMT_YUV422P) &&
00169                (s->streams[0]->codec->pix_fmt != PIX_FMT_GRAY8)   &&
00170                (s->streams[0]->codec->pix_fmt != PIX_FMT_YUV444P)) {
00171         av_log(s, AV_LOG_ERROR, "ERROR: yuv4mpeg only handles yuv444p, "
00172                "yuv422p, yuv420p, yuv411p and gray pixel formats. "
00173                "Use -pix_fmt to select one.\n");
00174         return AVERROR(EIO);
00175     }
00176 
00177     *first_pkt = 1;
00178     return 0;
00179 }
00180 
00181 AVOutputFormat ff_yuv4mpegpipe_muxer = {
00182     .name              = "yuv4mpegpipe",
00183     .long_name         = NULL_IF_CONFIG_SMALL("YUV4MPEG pipe format"),
00184     .extensions        = "y4m",
00185     .priv_data_size    = sizeof(int),
00186     .audio_codec       = CODEC_ID_NONE,
00187     .video_codec       = CODEC_ID_RAWVIDEO,
00188     .write_header      = yuv4_write_header,
00189     .write_packet      = yuv4_write_packet,
00190     .flags             = AVFMT_RAWPICTURE,
00191 };
00192 #endif
00193 
00194 /* Header size increased to allow room for optional flags */
00195 #define MAX_YUV4_HEADER 80
00196 #define MAX_FRAME_HEADER 80
00197 
00198 static int yuv4_read_header(AVFormatContext *s, AVFormatParameters *ap)
00199 {
00200     char header[MAX_YUV4_HEADER + 10];  // Include headroom for
00201                                         // the longest option
00202     char *tokstart, *tokend, *header_end;
00203     int i;
00204     AVIOContext *pb = s->pb;
00205     int width = -1, height  = -1, raten   = 0,
00206         rated =  0, aspectn =  0, aspectd = 0;
00207     enum PixelFormat pix_fmt = PIX_FMT_NONE, alt_pix_fmt = PIX_FMT_NONE;
00208     enum AVChromaLocation chroma_sample_location = AVCHROMA_LOC_UNSPECIFIED;
00209     AVStream *st;
00210     struct frame_attributes *s1 = s->priv_data;
00211 
00212     for (i = 0; i < MAX_YUV4_HEADER; i++) {
00213         header[i] = avio_r8(pb);
00214         if (header[i] == '\n') {
00215             header[i + 1] = 0x20;  // Add a space after last option.
00216                                    // Makes parsing "444" vs "444alpha" easier.
00217             header[i + 2] = 0;
00218             break;
00219         }
00220     }
00221     if (i == MAX_YUV4_HEADER)
00222         return -1;
00223     if (strncmp(header, Y4M_MAGIC, strlen(Y4M_MAGIC)))
00224         return -1;
00225 
00226     s1->interlaced_frame = 0;
00227     s1->top_field_first = 0;
00228     header_end = &header[i + 1]; // Include space
00229     for (tokstart = &header[strlen(Y4M_MAGIC) + 1];
00230          tokstart < header_end; tokstart++) {
00231         if (*tokstart == 0x20)
00232             continue;
00233         switch (*tokstart++) {
00234         case 'W': // Width. Required.
00235             width    = strtol(tokstart, &tokend, 10);
00236             tokstart = tokend;
00237             break;
00238         case 'H': // Height. Required.
00239             height   = strtol(tokstart, &tokend, 10);
00240             tokstart = tokend;
00241             break;
00242         case 'C': // Color space
00243             if (strncmp("420jpeg", tokstart, 7) == 0) {
00244                 pix_fmt = PIX_FMT_YUV420P;
00245                 chroma_sample_location = AVCHROMA_LOC_CENTER;
00246             } else if (strncmp("420mpeg2", tokstart, 8) == 0) {
00247                 pix_fmt = PIX_FMT_YUV420P;
00248                 chroma_sample_location = AVCHROMA_LOC_LEFT;
00249             } else if (strncmp("420paldv", tokstart, 8) == 0) {
00250                 pix_fmt = PIX_FMT_YUV420P;
00251                 chroma_sample_location = AVCHROMA_LOC_TOPLEFT;
00252             } else if (strncmp("411", tokstart, 3) == 0)
00253                 pix_fmt = PIX_FMT_YUV411P;
00254             else if (strncmp("422", tokstart, 3) == 0)
00255                 pix_fmt = PIX_FMT_YUV422P;
00256             else if (strncmp("444alpha", tokstart, 8) == 0 ) {
00257                 av_log(s, AV_LOG_ERROR, "Cannot handle 4:4:4:4 "
00258                        "YUV4MPEG stream.\n");
00259                 return -1;
00260             } else if (strncmp("444", tokstart, 3) == 0)
00261                 pix_fmt = PIX_FMT_YUV444P;
00262             else if (strncmp("mono", tokstart, 4) == 0) {
00263                 pix_fmt = PIX_FMT_GRAY8;
00264             } else {
00265                 av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains an unknown "
00266                        "pixel format.\n");
00267                 return -1;
00268             }
00269             while (tokstart < header_end && *tokstart != 0x20)
00270                 tokstart++;
00271             break;
00272         case 'I': // Interlace type
00273             switch (*tokstart++){
00274             case '?':
00275                 break;
00276             case 'p':
00277                 s1->interlaced_frame = 0;
00278                 break;
00279             case 't':
00280                 s1->interlaced_frame = 1;
00281                 s1->top_field_first = 1;
00282                 break;
00283             case 'b':
00284                 s1->interlaced_frame = 1;
00285                 s1->top_field_first = 0;
00286                 break;
00287             case 'm':
00288                 av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains mixed "
00289                        "interlaced and non-interlaced frames.\n");
00290                 return -1;
00291             default:
00292                 av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
00293                 return -1;
00294             }
00295             break;
00296         case 'F': // Frame rate
00297             sscanf(tokstart, "%d:%d", &raten, &rated); // 0:0 if unknown
00298             while (tokstart < header_end && *tokstart != 0x20)
00299                 tokstart++;
00300             break;
00301         case 'A': // Pixel aspect
00302             sscanf(tokstart, "%d:%d", &aspectn, &aspectd); // 0:0 if unknown
00303             while (tokstart < header_end && *tokstart != 0x20)
00304                 tokstart++;
00305             break;
00306         case 'X': // Vendor extensions
00307             if (strncmp("YSCSS=", tokstart, 6) == 0) {
00308                 // Older nonstandard pixel format representation
00309                 tokstart += 6;
00310                 if (strncmp("420JPEG", tokstart, 7) == 0)
00311                     alt_pix_fmt = PIX_FMT_YUV420P;
00312                 else if (strncmp("420MPEG2", tokstart, 8) == 0)
00313                     alt_pix_fmt = PIX_FMT_YUV420P;
00314                 else if (strncmp("420PALDV", tokstart, 8) == 0)
00315                     alt_pix_fmt = PIX_FMT_YUV420P;
00316                 else if (strncmp("411", tokstart, 3) == 0)
00317                     alt_pix_fmt = PIX_FMT_YUV411P;
00318                 else if (strncmp("422", tokstart, 3) == 0)
00319                     alt_pix_fmt = PIX_FMT_YUV422P;
00320                 else if (strncmp("444", tokstart, 3) == 0)
00321                     alt_pix_fmt = PIX_FMT_YUV444P;
00322             }
00323             while (tokstart < header_end && *tokstart != 0x20)
00324                 tokstart++;
00325             break;
00326         }
00327     }
00328 
00329     if (width == -1 || height == -1) {
00330         av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
00331         return -1;
00332     }
00333 
00334     if (pix_fmt == PIX_FMT_NONE) {
00335         if (alt_pix_fmt == PIX_FMT_NONE)
00336             pix_fmt = PIX_FMT_YUV420P;
00337         else
00338             pix_fmt = alt_pix_fmt;
00339     }
00340 
00341     if (raten <= 0 || rated <= 0) {
00342         // Frame rate unknown
00343         raten = 25;
00344         rated = 1;
00345     }
00346 
00347     if (aspectn == 0 && aspectd == 0) {
00348         // Pixel aspect unknown
00349         aspectd = 1;
00350     }
00351 
00352     st = avformat_new_stream(s, NULL);
00353     if (!st)
00354         return AVERROR(ENOMEM);
00355     st->codec->width  = width;
00356     st->codec->height = height;
00357     av_reduce(&raten, &rated, raten, rated, (1UL << 31) - 1);
00358     avpriv_set_pts_info(st, 64, rated, raten);
00359     st->codec->pix_fmt                = pix_fmt;
00360     st->codec->codec_type             = AVMEDIA_TYPE_VIDEO;
00361     st->codec->codec_id               = CODEC_ID_RAWVIDEO;
00362     st->sample_aspect_ratio           = (AVRational){ aspectn, aspectd };
00363     st->codec->chroma_sample_location = chroma_sample_location;
00364 
00365     return 0;
00366 }
00367 
00368 static int yuv4_read_packet(AVFormatContext *s, AVPacket *pkt)
00369 {
00370     int i;
00371     char header[MAX_FRAME_HEADER+1];
00372     int packet_size, width, height, ret;
00373     AVStream *st = s->streams[0];
00374     struct frame_attributes *s1 = s->priv_data;
00375 
00376     for (i = 0; i < MAX_FRAME_HEADER; i++) {
00377         header[i] = avio_r8(s->pb);
00378         if (header[i] == '\n') {
00379             header[i + 1] = 0;
00380             break;
00381         }
00382     }
00383     if (s->pb->error)
00384         return s->pb->error;
00385     else if (s->pb->eof_reached)
00386         return AVERROR_EOF;
00387     else if (i == MAX_FRAME_HEADER)
00388         return AVERROR_INVALIDDATA;
00389 
00390     if (strncmp(header, Y4M_FRAME_MAGIC, strlen(Y4M_FRAME_MAGIC)))
00391         return AVERROR_INVALIDDATA;
00392 
00393     width  = st->codec->width;
00394     height = st->codec->height;
00395 
00396     packet_size = avpicture_get_size(st->codec->pix_fmt, width, height);
00397     if (packet_size < 0)
00398         return packet_size;
00399 
00400     ret = av_get_packet(s->pb, pkt, packet_size);
00401     if (ret < 0)
00402         return ret;
00403     else if (ret != packet_size)
00404         return s->pb->eof_reached ? AVERROR_EOF : AVERROR(EIO);
00405 
00406     if (st->codec->coded_frame) {
00407         st->codec->coded_frame->interlaced_frame = s1->interlaced_frame;
00408         st->codec->coded_frame->top_field_first  = s1->top_field_first;
00409     }
00410 
00411     pkt->stream_index = 0;
00412     return 0;
00413 }
00414 
00415 static int yuv4_probe(AVProbeData *pd)
00416 {
00417     /* check file header */
00418     if (strncmp(pd->buf, Y4M_MAGIC, sizeof(Y4M_MAGIC) - 1) == 0)
00419         return AVPROBE_SCORE_MAX;
00420     else
00421         return 0;
00422 }
00423 
00424 #if CONFIG_YUV4MPEGPIPE_DEMUXER
00425 AVInputFormat ff_yuv4mpegpipe_demuxer = {
00426     .name           = "yuv4mpegpipe",
00427     .long_name      = NULL_IF_CONFIG_SMALL("YUV4MPEG pipe format"),
00428     .priv_data_size = sizeof(struct frame_attributes),
00429     .read_probe     = yuv4_probe,
00430     .read_header    = yuv4_read_header,
00431     .read_packet    = yuv4_read_packet,
00432     .extensions     = "y4m"
00433 };
00434 #endif
Generated on Fri Feb 1 2013 14:34:55 for FFmpeg by doxygen 1.7.1