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 Libav.
00006  *
00007  * Libav 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  * Libav 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 Libav; 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->pix_fmt == PIX_FMT_YUV411P) {
00159         av_log(s, AV_LOG_ERROR, "Warning: generating rarely used 4:1:1 YUV "
00160                "stream, some mjpegtools might not work.\n");
00161     } else if ((s->streams[0]->codec->pix_fmt != PIX_FMT_YUV420P) &&
00162                (s->streams[0]->codec->pix_fmt != PIX_FMT_YUV422P) &&
00163                (s->streams[0]->codec->pix_fmt != PIX_FMT_GRAY8)   &&
00164                (s->streams[0]->codec->pix_fmt != PIX_FMT_YUV444P)) {
00165         av_log(s, AV_LOG_ERROR, "ERROR: yuv4mpeg only handles yuv444p, "
00166                "yuv422p, yuv420p, yuv411p and gray pixel formats. "
00167                "Use -pix_fmt to select one.\n");
00168         return AVERROR(EIO);
00169     }
00170 
00171     *first_pkt = 1;
00172     return 0;
00173 }
00174 
00175 AVOutputFormat ff_yuv4mpegpipe_muxer = {
00176     .name              = "yuv4mpegpipe",
00177     .long_name         = NULL_IF_CONFIG_SMALL("YUV4MPEG pipe format"),
00178     .mime_type         = "",
00179     .extensions        = "y4m",
00180     .priv_data_size    = sizeof(int),
00181     .audio_codec       = CODEC_ID_NONE,
00182     .video_codec       = CODEC_ID_RAWVIDEO,
00183     .write_header      = yuv4_write_header,
00184     .write_packet      = 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
00196                                         // the longest option
00197     char *tokstart, *tokend, *header_end;
00198     int i;
00199     AVIOContext *pb = s->pb;
00200     int width = -1, height  = -1, raten   = 0,
00201         rated =  0, aspectn =  0, aspectd = 0;
00202     enum PixelFormat pix_fmt = PIX_FMT_NONE, alt_pix_fmt = PIX_FMT_NONE;
00203     enum AVChromaLocation chroma_sample_location = AVCHROMA_LOC_UNSPECIFIED;
00204     AVStream *st;
00205     struct frame_attributes *s1 = s->priv_data;
00206 
00207     for (i = 0; i < MAX_YUV4_HEADER; i++) {
00208         header[i] = avio_r8(pb);
00209         if (header[i] == '\n') {
00210             header[i + 1] = 0x20;  // Add a space after last option.
00211                                    // Makes parsing "444" vs "444alpha" easier.
00212             header[i + 2] = 0;
00213             break;
00214         }
00215     }
00216     if (i == MAX_YUV4_HEADER)
00217         return -1;
00218     if (strncmp(header, Y4M_MAGIC, strlen(Y4M_MAGIC)))
00219         return -1;
00220 
00221     s1->interlaced_frame = 0;
00222     s1->top_field_first = 0;
00223     header_end = &header[i + 1]; // Include space
00224     for (tokstart = &header[strlen(Y4M_MAGIC) + 1];
00225          tokstart < header_end; tokstart++) {
00226         if (*tokstart == 0x20)
00227             continue;
00228         switch (*tokstart++) {
00229         case 'W': // Width. Required.
00230             width    = strtol(tokstart, &tokend, 10);
00231             tokstart = tokend;
00232             break;
00233         case 'H': // Height. Required.
00234             height   = strtol(tokstart, &tokend, 10);
00235             tokstart = tokend;
00236             break;
00237         case 'C': // Color space
00238             if (strncmp("420jpeg", tokstart, 7) == 0) {
00239                 pix_fmt = PIX_FMT_YUV420P;
00240                 chroma_sample_location = AVCHROMA_LOC_CENTER;
00241             } else if (strncmp("420mpeg2", tokstart, 8) == 0) {
00242                 pix_fmt = PIX_FMT_YUV420P;
00243                 chroma_sample_location = AVCHROMA_LOC_LEFT;
00244             } else if (strncmp("420paldv", tokstart, 8) == 0) {
00245                 pix_fmt = PIX_FMT_YUV420P;
00246                 chroma_sample_location = AVCHROMA_LOC_TOPLEFT;
00247             } else if (strncmp("411", tokstart, 3) == 0)
00248                 pix_fmt = PIX_FMT_YUV411P;
00249             else if (strncmp("422", tokstart, 3) == 0)
00250                 pix_fmt = PIX_FMT_YUV422P;
00251             else if (strncmp("444alpha", tokstart, 8) == 0 ) {
00252                 av_log(s, AV_LOG_ERROR, "Cannot handle 4:4:4:4 "
00253                        "YUV4MPEG stream.\n");
00254                 return -1;
00255             } else if (strncmp("444", tokstart, 3) == 0)
00256                 pix_fmt = PIX_FMT_YUV444P;
00257             else if (strncmp("mono", tokstart, 4) == 0) {
00258                 pix_fmt = PIX_FMT_GRAY8;
00259             } else {
00260                 av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains an unknown "
00261                        "pixel format.\n");
00262                 return -1;
00263             }
00264             while (tokstart < header_end && *tokstart != 0x20)
00265                 tokstart++;
00266             break;
00267         case 'I': // Interlace type
00268             switch (*tokstart++){
00269             case '?':
00270                 break;
00271             case 'p':
00272                 s1->interlaced_frame = 0;
00273                 break;
00274             case 't':
00275                 s1->interlaced_frame = 1;
00276                 s1->top_field_first = 1;
00277                 break;
00278             case 'b':
00279                 s1->interlaced_frame = 1;
00280                 s1->top_field_first = 0;
00281                 break;
00282             case 'm':
00283                 av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains mixed "
00284                        "interlaced and non-interlaced frames.\n");
00285                 return -1;
00286             default:
00287                 av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
00288                 return -1;
00289             }
00290             break;
00291         case 'F': // Frame rate
00292             sscanf(tokstart, "%d:%d", &raten, &rated); // 0:0 if unknown
00293             while (tokstart < header_end && *tokstart != 0x20)
00294                 tokstart++;
00295             break;
00296         case 'A': // Pixel aspect
00297             sscanf(tokstart, "%d:%d", &aspectn, &aspectd); // 0:0 if unknown
00298             while (tokstart < header_end && *tokstart != 0x20)
00299                 tokstart++;
00300             break;
00301         case 'X': // Vendor extensions
00302             if (strncmp("YSCSS=", tokstart, 6) == 0) {
00303                 // Older nonstandard pixel format representation
00304                 tokstart += 6;
00305                 if (strncmp("420JPEG", tokstart, 7) == 0)
00306                     alt_pix_fmt = PIX_FMT_YUV420P;
00307                 else if (strncmp("420MPEG2", tokstart, 8) == 0)
00308                     alt_pix_fmt = PIX_FMT_YUV420P;
00309                 else if (strncmp("420PALDV", tokstart, 8) == 0)
00310                     alt_pix_fmt = PIX_FMT_YUV420P;
00311                 else if (strncmp("411", tokstart, 3) == 0)
00312                     alt_pix_fmt = PIX_FMT_YUV411P;
00313                 else if (strncmp("422", tokstart, 3) == 0)
00314                     alt_pix_fmt = PIX_FMT_YUV422P;
00315                 else if (strncmp("444", tokstart, 3) == 0)
00316                     alt_pix_fmt = PIX_FMT_YUV444P;
00317             }
00318             while (tokstart < header_end && *tokstart != 0x20)
00319                 tokstart++;
00320             break;
00321         }
00322     }
00323 
00324     if (width == -1 || height == -1) {
00325         av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
00326         return -1;
00327     }
00328 
00329     if (pix_fmt == PIX_FMT_NONE) {
00330         if (alt_pix_fmt == PIX_FMT_NONE)
00331             pix_fmt = PIX_FMT_YUV420P;
00332         else
00333             pix_fmt = alt_pix_fmt;
00334     }
00335 
00336     if (raten <= 0 || rated <= 0) {
00337         // Frame rate unknown
00338         raten = 25;
00339         rated = 1;
00340     }
00341 
00342     if (aspectn == 0 && aspectd == 0) {
00343         // Pixel aspect unknown
00344         aspectd = 1;
00345     }
00346 
00347     st = avformat_new_stream(s, NULL);
00348     if (!st)
00349         return AVERROR(ENOMEM);
00350     st->codec->width  = width;
00351     st->codec->height = height;
00352     av_reduce(&raten, &rated, raten, rated, (1UL << 31) - 1);
00353     avpriv_set_pts_info(st, 64, rated, raten);
00354     st->codec->pix_fmt                = pix_fmt;
00355     st->codec->codec_type             = AVMEDIA_TYPE_VIDEO;
00356     st->codec->codec_id               = CODEC_ID_RAWVIDEO;
00357     st->sample_aspect_ratio           = (AVRational){ aspectn, aspectd };
00358     st->codec->chroma_sample_location = chroma_sample_location;
00359 
00360     return 0;
00361 }
00362 
00363 static int yuv4_read_packet(AVFormatContext *s, AVPacket *pkt)
00364 {
00365     int i;
00366     char header[MAX_FRAME_HEADER+1];
00367     int packet_size, width, height;
00368     AVStream *st = s->streams[0];
00369     struct frame_attributes *s1 = s->priv_data;
00370 
00371     for (i = 0; i < MAX_FRAME_HEADER; i++) {
00372         header[i] = avio_r8(s->pb);
00373         if (header[i] == '\n') {
00374             header[i + 1] = 0;
00375             break;
00376         }
00377     }
00378     if (i == MAX_FRAME_HEADER)
00379         return -1;
00380     if (strncmp(header, Y4M_FRAME_MAGIC, strlen(Y4M_FRAME_MAGIC)))
00381         return -1;
00382 
00383     width  = st->codec->width;
00384     height = st->codec->height;
00385 
00386     packet_size = avpicture_get_size(st->codec->pix_fmt, width, height);
00387     if (packet_size < 0)
00388         return -1;
00389 
00390     if (av_get_packet(s->pb, pkt, packet_size) != packet_size)
00391         return AVERROR(EIO);
00392 
00393     if (st->codec->coded_frame) {
00394         st->codec->coded_frame->interlaced_frame = s1->interlaced_frame;
00395         st->codec->coded_frame->top_field_first  = s1->top_field_first;
00396     }
00397 
00398     pkt->stream_index = 0;
00399     return 0;
00400 }
00401 
00402 static int yuv4_probe(AVProbeData *pd)
00403 {
00404     /* check file header */
00405     if (strncmp(pd->buf, Y4M_MAGIC, sizeof(Y4M_MAGIC) - 1) == 0)
00406         return AVPROBE_SCORE_MAX;
00407     else
00408         return 0;
00409 }
00410 
00411 #if CONFIG_YUV4MPEGPIPE_DEMUXER
00412 AVInputFormat ff_yuv4mpegpipe_demuxer = {
00413     .name           = "yuv4mpegpipe",
00414     .long_name      = NULL_IF_CONFIG_SMALL("YUV4MPEG pipe format"),
00415     .priv_data_size = sizeof(struct frame_attributes),
00416     .read_probe     = yuv4_probe,
00417     .read_header    = yuv4_read_header,
00418     .read_packet    = yuv4_read_packet,
00419     .extensions     = "y4m"
00420 };
00421 #endif