]> git.sesse.net Git - ffmpeg/blob - libavformat/yuv4mpeg.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavformat / yuv4mpeg.c
1 /*
2  * YUV4MPEG format
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 #include "avformat.h"
22 #include "internal.h"
23 #include "libavutil/pixdesc.h"
24
25 #define Y4M_MAGIC "YUV4MPEG2"
26 #define Y4M_FRAME_MAGIC "FRAME"
27 #define Y4M_LINE_MAX 256
28
29 struct frame_attributes {
30     int interlaced_frame;
31     int top_field_first;
32 };
33
34 #if CONFIG_YUV4MPEGPIPE_MUXER
35 static int yuv4_generate_header(AVFormatContext *s, char* buf)
36 {
37     AVStream *st;
38     int width, height;
39     int raten, rated, aspectn, aspectd, n;
40     char inter;
41     const char *colorspace = "";
42
43     st     = s->streams[0];
44     width  = st->codec->width;
45     height = st->codec->height;
46
47     av_reduce(&raten, &rated, st->codec->time_base.den,
48               st->codec->time_base.num, (1UL << 31) - 1);
49
50     aspectn = st->sample_aspect_ratio.num;
51     aspectd = st->sample_aspect_ratio.den;
52
53     if (aspectn == 0 && aspectd == 1)
54         aspectd = 0;  // 0:0 means unknown
55
56     inter = 'p'; /* progressive is the default */
57     if (st->codec->coded_frame && st->codec->coded_frame->interlaced_frame)
58         inter = st->codec->coded_frame->top_field_first ? 't' : 'b';
59
60     switch (st->codec->pix_fmt) {
61     case PIX_FMT_GRAY8:
62         colorspace = " Cmono";
63         break;
64     case PIX_FMT_GRAY16:
65         colorspace = " Cmono16";
66         break;
67     case PIX_FMT_YUV411P:
68         colorspace = " C411 XYSCSS=411";
69         break;
70     case PIX_FMT_YUV420P:
71         switch (st->codec->chroma_sample_location) {
72         case AVCHROMA_LOC_TOPLEFT: colorspace = " C420paldv XYSCSS=420PALDV"; break;
73         case AVCHROMA_LOC_LEFT:    colorspace = " C420mpeg2 XYSCSS=420MPEG2"; break;
74         default:                   colorspace = " C420jpeg XYSCSS=420JPEG";   break;
75         }
76         break;
77     case PIX_FMT_YUV422P:
78         colorspace = " C422 XYSCSS=422";
79         break;
80     case PIX_FMT_YUV444P:
81         colorspace = " C444 XYSCSS=444";
82         break;
83     case PIX_FMT_YUV420P9:
84         colorspace = " C420p9 XYSCSS=420P9";
85         break;
86     case PIX_FMT_YUV422P9:
87         colorspace = " C422p9 XYSCSS=422P9";
88         break;
89     case PIX_FMT_YUV444P9:
90         colorspace = " C444p9 XYSCSS=444P9";
91         break;
92     case PIX_FMT_YUV420P10:
93         colorspace = " C420p10 XYSCSS=420P10";
94         break;
95     case PIX_FMT_YUV422P10:
96         colorspace = " C422p10 XYSCSS=422P10";
97         break;
98     case PIX_FMT_YUV444P10:
99         colorspace = " C444p10 XYSCSS=444P10";
100         break;
101     case PIX_FMT_YUV420P16:
102         colorspace = " C420p16 XYSCSS=420P16";
103         break;
104     case PIX_FMT_YUV422P16:
105         colorspace = " C422p16 XYSCSS=422P16";
106         break;
107     case PIX_FMT_YUV444P16:
108         colorspace = " C444p16 XYSCSS=444P16";
109         break;
110     }
111
112     /* construct stream header, if this is the first frame */
113     n = snprintf(buf, Y4M_LINE_MAX, "%s W%d H%d F%d:%d I%c A%d:%d%s\n",
114                  Y4M_MAGIC, width, height, raten, rated, inter,
115                  aspectn, aspectd, colorspace);
116
117     return n;
118 }
119
120 static int yuv4_write_packet(AVFormatContext *s, AVPacket *pkt)
121 {
122     AVStream *st = s->streams[pkt->stream_index];
123     AVIOContext *pb = s->pb;
124     AVPicture *picture, picture_tmp;
125     int* first_pkt = s->priv_data;
126     int width, height, h_chroma_shift, v_chroma_shift;
127     int i;
128     char buf2[Y4M_LINE_MAX + 1];
129     char buf1[20];
130     uint8_t *ptr, *ptr1, *ptr2;
131
132     memcpy(&picture_tmp, pkt->data, sizeof(AVPicture));
133     picture = &picture_tmp;
134
135     /* for the first packet we have to output the header as well */
136     if (*first_pkt) {
137         *first_pkt = 0;
138         if (yuv4_generate_header(s, buf2) < 0) {
139             av_log(s, AV_LOG_ERROR,
140                    "Error. YUV4MPEG stream header write failed.\n");
141             return AVERROR(EIO);
142         } else {
143             avio_write(pb, buf2, strlen(buf2));
144         }
145     }
146
147     /* construct frame header */
148
149     snprintf(buf1, sizeof(buf1), "%s\n", Y4M_FRAME_MAGIC);
150     avio_write(pb, buf1, strlen(buf1));
151
152     width  = st->codec->width;
153     height = st->codec->height;
154
155     ptr = picture->data[0];
156
157     switch (st->codec->pix_fmt) {
158     case PIX_FMT_GRAY8:
159     case PIX_FMT_YUV411P:
160     case PIX_FMT_YUV420P:
161     case PIX_FMT_YUV422P:
162     case PIX_FMT_YUV444P:
163         break;
164     case PIX_FMT_GRAY16:
165     case PIX_FMT_YUV420P9:
166     case PIX_FMT_YUV422P9:
167     case PIX_FMT_YUV444P9:
168     case PIX_FMT_YUV420P10:
169     case PIX_FMT_YUV422P10:
170     case PIX_FMT_YUV444P10:
171     case PIX_FMT_YUV420P16:
172     case PIX_FMT_YUV422P16:
173     case PIX_FMT_YUV444P16:
174         width *= 2;
175         break;
176     default:
177         av_log(s, AV_LOG_ERROR, "The pixel format '%s' is not supported.\n",
178                av_get_pix_fmt_name(st->codec->pix_fmt));
179         return AVERROR(EINVAL);
180     }
181
182     for (i = 0; i < height; i++) {
183         avio_write(pb, ptr, width);
184         ptr += picture->linesize[0];
185     }
186
187     if (st->codec->pix_fmt != PIX_FMT_GRAY8 &&
188         st->codec->pix_fmt != PIX_FMT_GRAY16) {
189         // Adjust for smaller Cb and Cr planes
190         avcodec_get_chroma_sub_sample(st->codec->pix_fmt, &h_chroma_shift,
191                                       &v_chroma_shift);
192         width  >>= h_chroma_shift;
193         height >>= v_chroma_shift;
194
195         ptr1 = picture->data[1];
196         ptr2 = picture->data[2];
197         for (i = 0; i < height; i++) {     /* Cb */
198             avio_write(pb, ptr1, width);
199             ptr1 += picture->linesize[1];
200         }
201         for (i = 0; i < height; i++) {     /* Cr */
202             avio_write(pb, ptr2, width);
203             ptr2 += picture->linesize[2];
204         }
205     }
206
207     avio_flush(pb);
208     return 0;
209 }
210
211 static int yuv4_write_header(AVFormatContext *s)
212 {
213     int *first_pkt = s->priv_data;
214
215     if (s->nb_streams != 1)
216         return AVERROR(EIO);
217
218     if (s->streams[0]->codec->codec_id != AV_CODEC_ID_RAWVIDEO) {
219         av_log(s, AV_LOG_ERROR,
220                "A non-rawvideo stream was selected, but yuv4mpeg only handles rawvideo streams\n");
221         return AVERROR(EINVAL);
222     }
223
224     switch (s->streams[0]->codec->pix_fmt) {
225     case PIX_FMT_YUV411P:
226         av_log(s, AV_LOG_WARNING, "Warning: generating rarely used 4:1:1 YUV "
227                "stream, some mjpegtools might not work.\n");
228         break;
229     case PIX_FMT_GRAY8:
230     case PIX_FMT_GRAY16:
231     case PIX_FMT_YUV420P:
232     case PIX_FMT_YUV422P:
233     case PIX_FMT_YUV444P:
234         break;
235     case PIX_FMT_YUV420P9:
236     case PIX_FMT_YUV422P9:
237     case PIX_FMT_YUV444P9:
238     case PIX_FMT_YUV420P10:
239     case PIX_FMT_YUV422P10:
240     case PIX_FMT_YUV444P10:
241     case PIX_FMT_YUV420P16:
242     case PIX_FMT_YUV422P16:
243     case PIX_FMT_YUV444P16:
244         if (s->streams[0]->codec->strict_std_compliance >= FF_COMPLIANCE_NORMAL) {
245             av_log(s, AV_LOG_ERROR, "'%s' is not a official yuv4mpegpipe pixel format. "
246                    "Use '-strict -1' to encode to this pixel format.\n",
247                    av_get_pix_fmt_name(s->streams[0]->codec->pix_fmt));
248             return AVERROR(EINVAL);
249         }
250         av_log(s, AV_LOG_WARNING, "Warning: generating non standart YUV stream. "
251                "Mjpegtools will not work.\n");
252         break;
253     default:
254         av_log(s, AV_LOG_ERROR, "ERROR: yuv4mpeg can only handle "
255                "yuv444p, yuv422p, yuv420p, yuv411p and gray8 pixel formats. "
256                "And using 'strict -1' also yuv444p9, yuv422p9, yuv420p9, "
257                "yuv444p10, yuv422p10, yuv420p10, "
258                "yuv444p16, yuv422p16, yuv420p16 "
259                "and gray16 pixel formats. "
260                "Use -pix_fmt to select one.\n");
261         return AVERROR(EIO);
262     }
263
264     *first_pkt = 1;
265     return 0;
266 }
267
268 AVOutputFormat ff_yuv4mpegpipe_muxer = {
269     .name              = "yuv4mpegpipe",
270     .long_name         = NULL_IF_CONFIG_SMALL("YUV4MPEG pipe"),
271     .extensions        = "y4m",
272     .priv_data_size    = sizeof(int),
273     .audio_codec       = AV_CODEC_ID_NONE,
274     .video_codec       = AV_CODEC_ID_RAWVIDEO,
275     .write_header      = yuv4_write_header,
276     .write_packet      = yuv4_write_packet,
277     .flags             = AVFMT_RAWPICTURE,
278 };
279 #endif
280
281 /* Header size increased to allow room for optional flags */
282 #define MAX_YUV4_HEADER 80
283 #define MAX_FRAME_HEADER 80
284
285 static int yuv4_read_header(AVFormatContext *s)
286 {
287     char header[MAX_YUV4_HEADER + 10];  // Include headroom for
288                                         // the longest option
289     char *tokstart, *tokend, *header_end;
290     int i;
291     AVIOContext *pb = s->pb;
292     int width = -1, height  = -1, raten   = 0,
293         rated =  0, aspectn =  0, aspectd = 0;
294     enum PixelFormat pix_fmt = PIX_FMT_NONE, alt_pix_fmt = PIX_FMT_NONE;
295     enum AVChromaLocation chroma_sample_location = AVCHROMA_LOC_UNSPECIFIED;
296     AVStream *st;
297     struct frame_attributes *s1 = s->priv_data;
298
299     for (i = 0; i < MAX_YUV4_HEADER; i++) {
300         header[i] = avio_r8(pb);
301         if (header[i] == '\n') {
302             header[i + 1] = 0x20;  // Add a space after last option.
303                                    // Makes parsing "444" vs "444alpha" easier.
304             header[i + 2] = 0;
305             break;
306         }
307     }
308     if (i == MAX_YUV4_HEADER)
309         return -1;
310     if (strncmp(header, Y4M_MAGIC, strlen(Y4M_MAGIC)))
311         return -1;
312
313     s1->interlaced_frame = 0;
314     s1->top_field_first = 0;
315     header_end = &header[i + 1]; // Include space
316     for (tokstart = &header[strlen(Y4M_MAGIC) + 1];
317          tokstart < header_end; tokstart++) {
318         if (*tokstart == 0x20)
319             continue;
320         switch (*tokstart++) {
321         case 'W': // Width. Required.
322             width    = strtol(tokstart, &tokend, 10);
323             tokstart = tokend;
324             break;
325         case 'H': // Height. Required.
326             height   = strtol(tokstart, &tokend, 10);
327             tokstart = tokend;
328             break;
329         case 'C': // Color space
330             if (strncmp("420jpeg", tokstart, 7) == 0) {
331                 pix_fmt = PIX_FMT_YUV420P;
332                 chroma_sample_location = AVCHROMA_LOC_CENTER;
333             } else if (strncmp("420mpeg2", tokstart, 8) == 0) {
334                 pix_fmt = PIX_FMT_YUV420P;
335                 chroma_sample_location = AVCHROMA_LOC_LEFT;
336             } else if (strncmp("420paldv", tokstart, 8) == 0) {
337                 pix_fmt = PIX_FMT_YUV420P;
338                 chroma_sample_location = AVCHROMA_LOC_TOPLEFT;
339             } else if (strncmp("420p16", tokstart, 6) == 0) {
340                 pix_fmt = PIX_FMT_YUV420P16;
341             } else if (strncmp("422p16", tokstart, 6) == 0) {
342                 pix_fmt = PIX_FMT_YUV422P16;
343             } else if (strncmp("444p16", tokstart, 6) == 0) {
344                 pix_fmt = PIX_FMT_YUV444P16;
345             } else if (strncmp("420p10", tokstart, 6) == 0) {
346                 pix_fmt = PIX_FMT_YUV420P10;
347             } else if (strncmp("422p10", tokstart, 6) == 0) {
348                 pix_fmt = PIX_FMT_YUV422P10;
349             } else if (strncmp("444p10", tokstart, 6) == 0) {
350                 pix_fmt = PIX_FMT_YUV444P10;
351             } else if (strncmp("420p9", tokstart, 5) == 0) {
352                 pix_fmt = PIX_FMT_YUV420P9;
353             } else if (strncmp("422p9", tokstart, 5) == 0) {
354                 pix_fmt = PIX_FMT_YUV422P9;
355             } else if (strncmp("444p9", tokstart, 5) == 0) {
356                 pix_fmt = PIX_FMT_YUV444P9;
357             } else if (strncmp("420", tokstart, 3) == 0) {
358                 pix_fmt = PIX_FMT_YUV420P;
359                 chroma_sample_location = AVCHROMA_LOC_CENTER;
360             } else if (strncmp("411", tokstart, 3) == 0) {
361                 pix_fmt = PIX_FMT_YUV411P;
362             } else if (strncmp("422", tokstart, 3) == 0) {
363                 pix_fmt = PIX_FMT_YUV422P;
364             } else if (strncmp("444alpha", tokstart, 8) == 0 ) {
365                 av_log(s, AV_LOG_ERROR, "Cannot handle 4:4:4:4 "
366                        "YUV4MPEG stream.\n");
367                 return -1;
368             } else if (strncmp("444", tokstart, 3) == 0) {
369                 pix_fmt = PIX_FMT_YUV444P;
370             } else if (strncmp("mono16", tokstart, 6) == 0) {
371                 pix_fmt = PIX_FMT_GRAY16;
372             } else if (strncmp("mono", tokstart, 4) == 0) {
373                 pix_fmt = PIX_FMT_GRAY8;
374             } else {
375                 av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains an unknown "
376                        "pixel format.\n");
377                 return -1;
378             }
379             while (tokstart < header_end && *tokstart != 0x20)
380                 tokstart++;
381             break;
382         case 'I': // Interlace type
383             switch (*tokstart++){
384             case '?':
385                 break;
386             case 'p':
387                 s1->interlaced_frame = 0;
388                 break;
389             case 't':
390                 s1->interlaced_frame = 1;
391                 s1->top_field_first = 1;
392                 break;
393             case 'b':
394                 s1->interlaced_frame = 1;
395                 s1->top_field_first = 0;
396                 break;
397             case 'm':
398                 av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains mixed "
399                        "interlaced and non-interlaced frames.\n");
400                 return -1;
401             default:
402                 av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
403                 return -1;
404             }
405             break;
406         case 'F': // Frame rate
407             sscanf(tokstart, "%d:%d", &raten, &rated); // 0:0 if unknown
408             while (tokstart < header_end && *tokstart != 0x20)
409                 tokstart++;
410             break;
411         case 'A': // Pixel aspect
412             sscanf(tokstart, "%d:%d", &aspectn, &aspectd); // 0:0 if unknown
413             while (tokstart < header_end && *tokstart != 0x20)
414                 tokstart++;
415             break;
416         case 'X': // Vendor extensions
417             if (strncmp("YSCSS=", tokstart, 6) == 0) {
418                 // Older nonstandard pixel format representation
419                 tokstart += 6;
420                 if (strncmp("420JPEG", tokstart, 7) == 0)
421                     alt_pix_fmt = PIX_FMT_YUV420P;
422                 else if (strncmp("420MPEG2", tokstart, 8) == 0)
423                     alt_pix_fmt = PIX_FMT_YUV420P;
424                 else if (strncmp("420PALDV", tokstart, 8) == 0)
425                     alt_pix_fmt = PIX_FMT_YUV420P;
426                 else if (strncmp("420P9", tokstart, 5) == 0)
427                     alt_pix_fmt = PIX_FMT_YUV420P9;
428                 else if (strncmp("422P9", tokstart, 5) == 0)
429                     alt_pix_fmt = PIX_FMT_YUV422P9;
430                 else if (strncmp("444P9", tokstart, 5) == 0)
431                     alt_pix_fmt = PIX_FMT_YUV444P9;
432                 else if (strncmp("420P10", tokstart, 6) == 0)
433                     alt_pix_fmt = PIX_FMT_YUV420P10;
434                 else if (strncmp("422P10", tokstart, 6) == 0)
435                     alt_pix_fmt = PIX_FMT_YUV422P10;
436                 else if (strncmp("444P10", tokstart, 6) == 0)
437                     alt_pix_fmt = PIX_FMT_YUV444P10;
438                 else if (strncmp("420P16", tokstart, 6) == 0)
439                     alt_pix_fmt = PIX_FMT_YUV420P16;
440                 else if (strncmp("422P16", tokstart, 6) == 0)
441                     alt_pix_fmt = PIX_FMT_YUV422P16;
442                 else if (strncmp("444P16", tokstart, 6) == 0)
443                     alt_pix_fmt = PIX_FMT_YUV444P16;
444                 else if (strncmp("411", tokstart, 3) == 0)
445                     alt_pix_fmt = PIX_FMT_YUV411P;
446                 else if (strncmp("422", tokstart, 3) == 0)
447                     alt_pix_fmt = PIX_FMT_YUV422P;
448                 else if (strncmp("444", tokstart, 3) == 0)
449                     alt_pix_fmt = PIX_FMT_YUV444P;
450             }
451             while (tokstart < header_end && *tokstart != 0x20)
452                 tokstart++;
453             break;
454         }
455     }
456
457     if (width == -1 || height == -1) {
458         av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
459         return -1;
460     }
461
462     if (pix_fmt == PIX_FMT_NONE) {
463         if (alt_pix_fmt == PIX_FMT_NONE)
464             pix_fmt = PIX_FMT_YUV420P;
465         else
466             pix_fmt = alt_pix_fmt;
467     }
468
469     if (raten <= 0 || rated <= 0) {
470         // Frame rate unknown
471         raten = 25;
472         rated = 1;
473     }
474
475     if (aspectn == 0 && aspectd == 0) {
476         // Pixel aspect unknown
477         aspectd = 1;
478     }
479
480     st = avformat_new_stream(s, NULL);
481     if (!st)
482         return AVERROR(ENOMEM);
483     st->codec->width  = width;
484     st->codec->height = height;
485     av_reduce(&raten, &rated, raten, rated, (1UL << 31) - 1);
486     avpriv_set_pts_info(st, 64, rated, raten);
487     st->codec->pix_fmt                = pix_fmt;
488     st->codec->codec_type             = AVMEDIA_TYPE_VIDEO;
489     st->codec->codec_id               = AV_CODEC_ID_RAWVIDEO;
490     st->sample_aspect_ratio           = (AVRational){ aspectn, aspectd };
491     st->codec->chroma_sample_location = chroma_sample_location;
492
493     return 0;
494 }
495
496 static int yuv4_read_packet(AVFormatContext *s, AVPacket *pkt)
497 {
498     int i;
499     char header[MAX_FRAME_HEADER+1];
500     int packet_size, width, height;
501     AVStream *st = s->streams[0];
502     struct frame_attributes *s1 = s->priv_data;
503
504     for (i = 0; i < MAX_FRAME_HEADER; i++) {
505         header[i] = avio_r8(s->pb);
506         if (header[i] == '\n') {
507             header[i + 1] = 0;
508             break;
509         }
510     }
511     if (i == MAX_FRAME_HEADER)
512         return -1;
513     if (strncmp(header, Y4M_FRAME_MAGIC, strlen(Y4M_FRAME_MAGIC)))
514         return -1;
515
516     width  = st->codec->width;
517     height = st->codec->height;
518
519     packet_size = avpicture_get_size(st->codec->pix_fmt, width, height);
520     if (packet_size < 0)
521         return -1;
522
523     if (av_get_packet(s->pb, pkt, packet_size) != packet_size)
524         return AVERROR(EIO);
525
526     if (st->codec->coded_frame) {
527         st->codec->coded_frame->interlaced_frame = s1->interlaced_frame;
528         st->codec->coded_frame->top_field_first  = s1->top_field_first;
529     }
530
531     pkt->stream_index = 0;
532     return 0;
533 }
534
535 static int yuv4_probe(AVProbeData *pd)
536 {
537     /* check file header */
538     if (strncmp(pd->buf, Y4M_MAGIC, sizeof(Y4M_MAGIC) - 1) == 0)
539         return AVPROBE_SCORE_MAX;
540     else
541         return 0;
542 }
543
544 #if CONFIG_YUV4MPEGPIPE_DEMUXER
545 AVInputFormat ff_yuv4mpegpipe_demuxer = {
546     .name           = "yuv4mpegpipe",
547     .long_name      = NULL_IF_CONFIG_SMALL("YUV4MPEG pipe"),
548     .priv_data_size = sizeof(struct frame_attributes),
549     .read_probe     = yuv4_probe,
550     .read_header    = yuv4_read_header,
551     .read_packet    = yuv4_read_packet,
552     .extensions     = "y4m",
553 };
554 #endif