]> git.sesse.net Git - ffmpeg/blob - libavformat/yuv4mpeg.c
Replace all CODEC_ID_* with AV_CODEC_ID_*
[ffmpeg] / libavformat / yuv4mpeg.c
1 /*
2  * YUV4MPEG format
3  * Copyright (c) 2001, 2002, 2003 Fabrice Bellard
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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
24 #define Y4M_MAGIC "YUV4MPEG2"
25 #define Y4M_FRAME_MAGIC "FRAME"
26 #define Y4M_LINE_MAX 256
27
28 struct frame_attributes {
29     int interlaced_frame;
30     int top_field_first;
31 };
32
33 #if CONFIG_YUV4MPEGPIPE_MUXER
34 static int yuv4_generate_header(AVFormatContext *s, char* buf)
35 {
36     AVStream *st;
37     int width, height;
38     int raten, rated, aspectn, aspectd, n;
39     char inter;
40     const char *colorspace = "";
41
42     st     = s->streams[0];
43     width  = st->codec->width;
44     height = st->codec->height;
45
46     av_reduce(&raten, &rated, st->codec->time_base.den,
47               st->codec->time_base.num, (1UL << 31) - 1);
48
49     aspectn = st->sample_aspect_ratio.num;
50     aspectd = st->sample_aspect_ratio.den;
51
52     if (aspectn == 0 && aspectd == 1)
53         aspectd = 0;  // 0:0 means unknown
54
55     inter = 'p'; /* progressive is the default */
56     if (st->codec->coded_frame && st->codec->coded_frame->interlaced_frame)
57         inter = st->codec->coded_frame->top_field_first ? 't' : 'b';
58
59     switch (st->codec->pix_fmt) {
60     case PIX_FMT_GRAY8:
61         colorspace = " Cmono";
62         break;
63     case PIX_FMT_YUV411P:
64         colorspace = " C411 XYSCSS=411";
65         break;
66     case PIX_FMT_YUV420P:
67         switch (st->codec->chroma_sample_location) {
68         case AVCHROMA_LOC_TOPLEFT: colorspace = " C420paldv XYSCSS=420PALDV"; break;
69         case AVCHROMA_LOC_LEFT:    colorspace = " C420mpeg2 XYSCSS=420MPEG2"; break;
70         default:                   colorspace = " C420jpeg XYSCSS=420JPEG";   break;
71         }
72         break;
73     case PIX_FMT_YUV422P:
74         colorspace = " C422 XYSCSS=422";
75         break;
76     case PIX_FMT_YUV444P:
77         colorspace = " C444 XYSCSS=444";
78         break;
79     }
80
81     /* construct stream header, if this is the first frame */
82     n = snprintf(buf, Y4M_LINE_MAX, "%s W%d H%d F%d:%d I%c A%d:%d%s\n",
83                  Y4M_MAGIC, width, height, raten, rated, inter,
84                  aspectn, aspectd, colorspace);
85
86     return n;
87 }
88
89 static int yuv4_write_packet(AVFormatContext *s, AVPacket *pkt)
90 {
91     AVStream *st = s->streams[pkt->stream_index];
92     AVIOContext *pb = s->pb;
93     AVPicture *picture;
94     int* first_pkt = s->priv_data;
95     int width, height, h_chroma_shift, v_chroma_shift;
96     int i;
97     char buf2[Y4M_LINE_MAX + 1];
98     char buf1[20];
99     uint8_t *ptr, *ptr1, *ptr2;
100
101     picture = (AVPicture *)pkt->data;
102
103     /* for the first packet we have to output the header as well */
104     if (*first_pkt) {
105         *first_pkt = 0;
106         if (yuv4_generate_header(s, buf2) < 0) {
107             av_log(s, AV_LOG_ERROR,
108                    "Error. YUV4MPEG stream header write failed.\n");
109             return AVERROR(EIO);
110         } else {
111             avio_write(pb, buf2, strlen(buf2));
112         }
113     }
114
115     /* construct frame header */
116
117     snprintf(buf1, sizeof(buf1), "%s\n", Y4M_FRAME_MAGIC);
118     avio_write(pb, buf1, strlen(buf1));
119
120     width  = st->codec->width;
121     height = st->codec->height;
122
123     ptr = picture->data[0];
124     for (i = 0; i < height; i++) {
125         avio_write(pb, ptr, width);
126         ptr += picture->linesize[0];
127     }
128
129     if (st->codec->pix_fmt != PIX_FMT_GRAY8) {
130         // Adjust for smaller Cb and Cr planes
131         avcodec_get_chroma_sub_sample(st->codec->pix_fmt, &h_chroma_shift,
132                                       &v_chroma_shift);
133         width  >>= h_chroma_shift;
134         height >>= v_chroma_shift;
135
136         ptr1 = picture->data[1];
137         ptr2 = picture->data[2];
138         for (i = 0; i < height; i++) {     /* Cb */
139             avio_write(pb, ptr1, width);
140             ptr1 += picture->linesize[1];
141         }
142         for (i = 0; i < height; i++) {     /* Cr */
143             avio_write(pb, ptr2, width);
144             ptr2 += picture->linesize[2];
145         }
146     }
147     avio_flush(pb);
148     return 0;
149 }
150
151 static int yuv4_write_header(AVFormatContext *s)
152 {
153     int *first_pkt = s->priv_data;
154
155     if (s->nb_streams != 1)
156         return AVERROR(EIO);
157
158     if (s->streams[0]->codec->pix_fmt == PIX_FMT_YUV411P) {
159         av_log(s, AV_LOG_ERROR, "Warning: generating rarely used 4:1:1 YUV "
160                "stream, some mjpegtools might not work.\n");
161     } else if ((s->streams[0]->codec->pix_fmt != PIX_FMT_YUV420P) &&
162                (s->streams[0]->codec->pix_fmt != PIX_FMT_YUV422P) &&
163                (s->streams[0]->codec->pix_fmt != PIX_FMT_GRAY8)   &&
164                (s->streams[0]->codec->pix_fmt != PIX_FMT_YUV444P)) {
165         av_log(s, AV_LOG_ERROR, "ERROR: yuv4mpeg only handles yuv444p, "
166                "yuv422p, yuv420p, yuv411p and gray pixel formats. "
167                "Use -pix_fmt to select one.\n");
168         return AVERROR(EIO);
169     }
170
171     *first_pkt = 1;
172     return 0;
173 }
174
175 AVOutputFormat ff_yuv4mpegpipe_muxer = {
176     .name              = "yuv4mpegpipe",
177     .long_name         = NULL_IF_CONFIG_SMALL("YUV4MPEG pipe"),
178     .mime_type         = "",
179     .extensions        = "y4m",
180     .priv_data_size    = sizeof(int),
181     .audio_codec       = AV_CODEC_ID_NONE,
182     .video_codec       = AV_CODEC_ID_RAWVIDEO,
183     .write_header      = yuv4_write_header,
184     .write_packet      = yuv4_write_packet,
185     .flags             = AVFMT_RAWPICTURE,
186 };
187 #endif
188
189 /* Header size increased to allow room for optional flags */
190 #define MAX_YUV4_HEADER 80
191 #define MAX_FRAME_HEADER 80
192
193 static int yuv4_read_header(AVFormatContext *s)
194 {
195     char header[MAX_YUV4_HEADER + 10];  // Include headroom for
196                                         // the longest option
197     char *tokstart, *tokend, *header_end;
198     int i;
199     AVIOContext *pb = s->pb;
200     int width = -1, height  = -1, raten   = 0,
201         rated =  0, aspectn =  0, aspectd = 0;
202     enum PixelFormat pix_fmt = PIX_FMT_NONE, alt_pix_fmt = PIX_FMT_NONE;
203     enum AVChromaLocation chroma_sample_location = AVCHROMA_LOC_UNSPECIFIED;
204     AVStream *st;
205     struct frame_attributes *s1 = s->priv_data;
206
207     for (i = 0; i < MAX_YUV4_HEADER; i++) {
208         header[i] = avio_r8(pb);
209         if (header[i] == '\n') {
210             header[i + 1] = 0x20;  // Add a space after last option.
211                                    // Makes parsing "444" vs "444alpha" easier.
212             header[i + 2] = 0;
213             break;
214         }
215     }
216     if (i == MAX_YUV4_HEADER)
217         return -1;
218     if (strncmp(header, Y4M_MAGIC, strlen(Y4M_MAGIC)))
219         return -1;
220
221     s1->interlaced_frame = 0;
222     s1->top_field_first = 0;
223     header_end = &header[i + 1]; // Include space
224     for (tokstart = &header[strlen(Y4M_MAGIC) + 1];
225          tokstart < header_end; tokstart++) {
226         if (*tokstart == 0x20)
227             continue;
228         switch (*tokstart++) {
229         case 'W': // Width. Required.
230             width    = strtol(tokstart, &tokend, 10);
231             tokstart = tokend;
232             break;
233         case 'H': // Height. Required.
234             height   = strtol(tokstart, &tokend, 10);
235             tokstart = tokend;
236             break;
237         case 'C': // Color space
238             if (strncmp("420jpeg", tokstart, 7) == 0) {
239                 pix_fmt = PIX_FMT_YUV420P;
240                 chroma_sample_location = AVCHROMA_LOC_CENTER;
241             } else if (strncmp("420mpeg2", tokstart, 8) == 0) {
242                 pix_fmt = PIX_FMT_YUV420P;
243                 chroma_sample_location = AVCHROMA_LOC_LEFT;
244             } else if (strncmp("420paldv", tokstart, 8) == 0) {
245                 pix_fmt = PIX_FMT_YUV420P;
246                 chroma_sample_location = AVCHROMA_LOC_TOPLEFT;
247             } else if (strncmp("420", tokstart, 3) == 0) {
248                 pix_fmt = PIX_FMT_YUV420P;
249                 chroma_sample_location = AVCHROMA_LOC_CENTER;
250             } else if (strncmp("411", tokstart, 3) == 0)
251                 pix_fmt = PIX_FMT_YUV411P;
252             else if (strncmp("422", tokstart, 3) == 0)
253                 pix_fmt = PIX_FMT_YUV422P;
254             else if (strncmp("444alpha", tokstart, 8) == 0 ) {
255                 av_log(s, AV_LOG_ERROR, "Cannot handle 4:4:4:4 "
256                        "YUV4MPEG stream.\n");
257                 return -1;
258             } else if (strncmp("444", tokstart, 3) == 0)
259                 pix_fmt = PIX_FMT_YUV444P;
260             else if (strncmp("mono", tokstart, 4) == 0) {
261                 pix_fmt = PIX_FMT_GRAY8;
262             } else {
263                 av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains an unknown "
264                        "pixel format.\n");
265                 return -1;
266             }
267             while (tokstart < header_end && *tokstart != 0x20)
268                 tokstart++;
269             break;
270         case 'I': // Interlace type
271             switch (*tokstart++){
272             case '?':
273                 break;
274             case 'p':
275                 s1->interlaced_frame = 0;
276                 break;
277             case 't':
278                 s1->interlaced_frame = 1;
279                 s1->top_field_first = 1;
280                 break;
281             case 'b':
282                 s1->interlaced_frame = 1;
283                 s1->top_field_first = 0;
284                 break;
285             case 'm':
286                 av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains mixed "
287                        "interlaced and non-interlaced frames.\n");
288                 return -1;
289             default:
290                 av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
291                 return -1;
292             }
293             break;
294         case 'F': // Frame rate
295             sscanf(tokstart, "%d:%d", &raten, &rated); // 0:0 if unknown
296             while (tokstart < header_end && *tokstart != 0x20)
297                 tokstart++;
298             break;
299         case 'A': // Pixel aspect
300             sscanf(tokstart, "%d:%d", &aspectn, &aspectd); // 0:0 if unknown
301             while (tokstart < header_end && *tokstart != 0x20)
302                 tokstart++;
303             break;
304         case 'X': // Vendor extensions
305             if (strncmp("YSCSS=", tokstart, 6) == 0) {
306                 // Older nonstandard pixel format representation
307                 tokstart += 6;
308                 if (strncmp("420JPEG", tokstart, 7) == 0)
309                     alt_pix_fmt = PIX_FMT_YUV420P;
310                 else if (strncmp("420MPEG2", tokstart, 8) == 0)
311                     alt_pix_fmt = PIX_FMT_YUV420P;
312                 else if (strncmp("420PALDV", tokstart, 8) == 0)
313                     alt_pix_fmt = PIX_FMT_YUV420P;
314                 else if (strncmp("411", tokstart, 3) == 0)
315                     alt_pix_fmt = PIX_FMT_YUV411P;
316                 else if (strncmp("422", tokstart, 3) == 0)
317                     alt_pix_fmt = PIX_FMT_YUV422P;
318                 else if (strncmp("444", tokstart, 3) == 0)
319                     alt_pix_fmt = PIX_FMT_YUV444P;
320             }
321             while (tokstart < header_end && *tokstart != 0x20)
322                 tokstart++;
323             break;
324         }
325     }
326
327     if (width == -1 || height == -1) {
328         av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
329         return -1;
330     }
331
332     if (pix_fmt == PIX_FMT_NONE) {
333         if (alt_pix_fmt == PIX_FMT_NONE)
334             pix_fmt = PIX_FMT_YUV420P;
335         else
336             pix_fmt = alt_pix_fmt;
337     }
338
339     if (raten <= 0 || rated <= 0) {
340         // Frame rate unknown
341         raten = 25;
342         rated = 1;
343     }
344
345     if (aspectn == 0 && aspectd == 0) {
346         // Pixel aspect unknown
347         aspectd = 1;
348     }
349
350     st = avformat_new_stream(s, NULL);
351     if (!st)
352         return AVERROR(ENOMEM);
353     st->codec->width  = width;
354     st->codec->height = height;
355     av_reduce(&raten, &rated, raten, rated, (1UL << 31) - 1);
356     avpriv_set_pts_info(st, 64, rated, raten);
357     st->codec->pix_fmt                = pix_fmt;
358     st->codec->codec_type             = AVMEDIA_TYPE_VIDEO;
359     st->codec->codec_id               = AV_CODEC_ID_RAWVIDEO;
360     st->sample_aspect_ratio           = (AVRational){ aspectn, aspectd };
361     st->codec->chroma_sample_location = chroma_sample_location;
362
363     return 0;
364 }
365
366 static int yuv4_read_packet(AVFormatContext *s, AVPacket *pkt)
367 {
368     int i;
369     char header[MAX_FRAME_HEADER+1];
370     int packet_size, width, height;
371     AVStream *st = s->streams[0];
372     struct frame_attributes *s1 = s->priv_data;
373
374     for (i = 0; i < MAX_FRAME_HEADER; i++) {
375         header[i] = avio_r8(s->pb);
376         if (header[i] == '\n') {
377             header[i + 1] = 0;
378             break;
379         }
380     }
381     if (i == MAX_FRAME_HEADER)
382         return -1;
383     if (strncmp(header, Y4M_FRAME_MAGIC, strlen(Y4M_FRAME_MAGIC)))
384         return -1;
385
386     width  = st->codec->width;
387     height = st->codec->height;
388
389     packet_size = avpicture_get_size(st->codec->pix_fmt, width, height);
390     if (packet_size < 0)
391         return -1;
392
393     if (av_get_packet(s->pb, pkt, packet_size) != packet_size)
394         return AVERROR(EIO);
395
396     if (st->codec->coded_frame) {
397         st->codec->coded_frame->interlaced_frame = s1->interlaced_frame;
398         st->codec->coded_frame->top_field_first  = s1->top_field_first;
399     }
400
401     pkt->stream_index = 0;
402     return 0;
403 }
404
405 static int yuv4_probe(AVProbeData *pd)
406 {
407     /* check file header */
408     if (strncmp(pd->buf, Y4M_MAGIC, sizeof(Y4M_MAGIC) - 1) == 0)
409         return AVPROBE_SCORE_MAX;
410     else
411         return 0;
412 }
413
414 #if CONFIG_YUV4MPEGPIPE_DEMUXER
415 AVInputFormat ff_yuv4mpegpipe_demuxer = {
416     .name           = "yuv4mpegpipe",
417     .long_name      = NULL_IF_CONFIG_SMALL("YUV4MPEG pipe"),
418     .priv_data_size = sizeof(struct frame_attributes),
419     .read_probe     = yuv4_probe,
420     .read_header    = yuv4_read_header,
421     .read_packet    = yuv4_read_packet,
422     .extensions     = "y4m",
423 };
424 #endif