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