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