]> git.sesse.net Git - ffmpeg/blob - libavformat/yuv4mpegdec.c
yuv4mpeg: split the demuxer and muxer into separate files
[ffmpeg] / libavformat / yuv4mpegdec.c
1 /*
2  * YUV4MPEG demuxer
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
22 #include "avformat.h"
23 #include "internal.h"
24 #include "yuv4mpeg.h"
25
26 /* Header size increased to allow room for optional flags */
27 #define MAX_YUV4_HEADER 80
28 #define MAX_FRAME_HEADER 80
29
30 struct frame_attributes {
31     int interlaced_frame;
32     int top_field_first;
33 };
34
35 static int yuv4_read_header(AVFormatContext *s)
36 {
37     char header[MAX_YUV4_HEADER + 10];  // Include headroom for
38                                         // the longest option
39     char *tokstart, *tokend, *header_end;
40     int i;
41     AVIOContext *pb = s->pb;
42     int width = -1, height  = -1, raten   = 0,
43         rated =  0, aspectn =  0, aspectd = 0;
44     enum AVPixelFormat pix_fmt = AV_PIX_FMT_NONE, alt_pix_fmt = AV_PIX_FMT_NONE;
45     enum AVChromaLocation chroma_sample_location = AVCHROMA_LOC_UNSPECIFIED;
46     AVStream *st;
47     struct frame_attributes *s1 = s->priv_data;
48
49     for (i = 0; i < MAX_YUV4_HEADER; i++) {
50         header[i] = avio_r8(pb);
51         if (header[i] == '\n') {
52             header[i + 1] = 0x20;  // Add a space after last option.
53                                    // Makes parsing "444" vs "444alpha" easier.
54             header[i + 2] = 0;
55             break;
56         }
57     }
58     if (i == MAX_YUV4_HEADER)
59         return -1;
60     if (strncmp(header, Y4M_MAGIC, strlen(Y4M_MAGIC)))
61         return -1;
62
63     s1->interlaced_frame = 0;
64     s1->top_field_first = 0;
65     header_end = &header[i + 1]; // Include space
66     for (tokstart = &header[strlen(Y4M_MAGIC) + 1];
67          tokstart < header_end; tokstart++) {
68         if (*tokstart == 0x20)
69             continue;
70         switch (*tokstart++) {
71         case 'W': // Width. Required.
72             width    = strtol(tokstart, &tokend, 10);
73             tokstart = tokend;
74             break;
75         case 'H': // Height. Required.
76             height   = strtol(tokstart, &tokend, 10);
77             tokstart = tokend;
78             break;
79         case 'C': // Color space
80             if (strncmp("420jpeg", tokstart, 7) == 0) {
81                 pix_fmt = AV_PIX_FMT_YUV420P;
82                 chroma_sample_location = AVCHROMA_LOC_CENTER;
83             } else if (strncmp("420mpeg2", tokstart, 8) == 0) {
84                 pix_fmt = AV_PIX_FMT_YUV420P;
85                 chroma_sample_location = AVCHROMA_LOC_LEFT;
86             } else if (strncmp("420paldv", tokstart, 8) == 0) {
87                 pix_fmt = AV_PIX_FMT_YUV420P;
88                 chroma_sample_location = AVCHROMA_LOC_TOPLEFT;
89             } else if (strncmp("420", tokstart, 3) == 0) {
90                 pix_fmt = AV_PIX_FMT_YUV420P;
91                 chroma_sample_location = AVCHROMA_LOC_CENTER;
92             } else if (strncmp("411", tokstart, 3) == 0)
93                 pix_fmt = AV_PIX_FMT_YUV411P;
94             else if (strncmp("422", tokstart, 3) == 0)
95                 pix_fmt = AV_PIX_FMT_YUV422P;
96             else if (strncmp("444alpha", tokstart, 8) == 0 ) {
97                 av_log(s, AV_LOG_ERROR, "Cannot handle 4:4:4:4 "
98                        "YUV4MPEG stream.\n");
99                 return -1;
100             } else if (strncmp("444", tokstart, 3) == 0)
101                 pix_fmt = AV_PIX_FMT_YUV444P;
102             else if (strncmp("mono", tokstart, 4) == 0) {
103                 pix_fmt = AV_PIX_FMT_GRAY8;
104             } else {
105                 av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains an unknown "
106                        "pixel format.\n");
107                 return -1;
108             }
109             while (tokstart < header_end && *tokstart != 0x20)
110                 tokstart++;
111             break;
112         case 'I': // Interlace type
113             switch (*tokstart++){
114             case '?':
115                 break;
116             case 'p':
117                 s1->interlaced_frame = 0;
118                 break;
119             case 't':
120                 s1->interlaced_frame = 1;
121                 s1->top_field_first = 1;
122                 break;
123             case 'b':
124                 s1->interlaced_frame = 1;
125                 s1->top_field_first = 0;
126                 break;
127             case 'm':
128                 av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains mixed "
129                        "interlaced and non-interlaced frames.\n");
130                 return -1;
131             default:
132                 av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
133                 return -1;
134             }
135             break;
136         case 'F': // Frame rate
137             sscanf(tokstart, "%d:%d", &raten, &rated); // 0:0 if unknown
138             while (tokstart < header_end && *tokstart != 0x20)
139                 tokstart++;
140             break;
141         case 'A': // Pixel aspect
142             sscanf(tokstart, "%d:%d", &aspectn, &aspectd); // 0:0 if unknown
143             while (tokstart < header_end && *tokstart != 0x20)
144                 tokstart++;
145             break;
146         case 'X': // Vendor extensions
147             if (strncmp("YSCSS=", tokstart, 6) == 0) {
148                 // Older nonstandard pixel format representation
149                 tokstart += 6;
150                 if (strncmp("420JPEG", tokstart, 7) == 0)
151                     alt_pix_fmt = AV_PIX_FMT_YUV420P;
152                 else if (strncmp("420MPEG2", tokstart, 8) == 0)
153                     alt_pix_fmt = AV_PIX_FMT_YUV420P;
154                 else if (strncmp("420PALDV", tokstart, 8) == 0)
155                     alt_pix_fmt = AV_PIX_FMT_YUV420P;
156                 else if (strncmp("411", tokstart, 3) == 0)
157                     alt_pix_fmt = AV_PIX_FMT_YUV411P;
158                 else if (strncmp("422", tokstart, 3) == 0)
159                     alt_pix_fmt = AV_PIX_FMT_YUV422P;
160                 else if (strncmp("444", tokstart, 3) == 0)
161                     alt_pix_fmt = AV_PIX_FMT_YUV444P;
162             }
163             while (tokstart < header_end && *tokstart != 0x20)
164                 tokstart++;
165             break;
166         }
167     }
168
169     if (width == -1 || height == -1) {
170         av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
171         return -1;
172     }
173
174     if (pix_fmt == AV_PIX_FMT_NONE) {
175         if (alt_pix_fmt == AV_PIX_FMT_NONE)
176             pix_fmt = AV_PIX_FMT_YUV420P;
177         else
178             pix_fmt = alt_pix_fmt;
179     }
180
181     if (raten <= 0 || rated <= 0) {
182         // Frame rate unknown
183         raten = 25;
184         rated = 1;
185     }
186
187     if (aspectn == 0 && aspectd == 0) {
188         // Pixel aspect unknown
189         aspectd = 1;
190     }
191
192     st = avformat_new_stream(s, NULL);
193     if (!st)
194         return AVERROR(ENOMEM);
195     st->codec->width  = width;
196     st->codec->height = height;
197     av_reduce(&raten, &rated, raten, rated, (1UL << 31) - 1);
198     avpriv_set_pts_info(st, 64, rated, raten);
199     st->avg_frame_rate                = av_inv_q(st->time_base);
200     st->codec->pix_fmt                = pix_fmt;
201     st->codec->codec_type             = AVMEDIA_TYPE_VIDEO;
202     st->codec->codec_id               = AV_CODEC_ID_RAWVIDEO;
203     st->sample_aspect_ratio           = (AVRational){ aspectn, aspectd };
204     st->codec->chroma_sample_location = chroma_sample_location;
205
206     return 0;
207 }
208
209 static int yuv4_read_packet(AVFormatContext *s, AVPacket *pkt)
210 {
211     int i;
212     char header[MAX_FRAME_HEADER+1];
213     int packet_size, width, height, ret;
214     AVStream *st = s->streams[0];
215     struct frame_attributes *s1 = s->priv_data;
216
217     for (i = 0; i < MAX_FRAME_HEADER; i++) {
218         header[i] = avio_r8(s->pb);
219         if (header[i] == '\n') {
220             header[i + 1] = 0;
221             break;
222         }
223     }
224     if (s->pb->error)
225         return s->pb->error;
226     else if (s->pb->eof_reached)
227         return AVERROR_EOF;
228     else if (i == MAX_FRAME_HEADER)
229         return AVERROR_INVALIDDATA;
230
231     if (strncmp(header, Y4M_FRAME_MAGIC, strlen(Y4M_FRAME_MAGIC)))
232         return AVERROR_INVALIDDATA;
233
234     width  = st->codec->width;
235     height = st->codec->height;
236
237     packet_size = avpicture_get_size(st->codec->pix_fmt, width, height);
238     if (packet_size < 0)
239         return packet_size;
240
241     ret = av_get_packet(s->pb, pkt, packet_size);
242     if (ret < 0)
243         return ret;
244     else if (ret != packet_size)
245         return s->pb->eof_reached ? AVERROR_EOF : AVERROR(EIO);
246
247     if (st->codec->coded_frame) {
248         st->codec->coded_frame->interlaced_frame = s1->interlaced_frame;
249         st->codec->coded_frame->top_field_first  = s1->top_field_first;
250     }
251
252     pkt->stream_index = 0;
253     return 0;
254 }
255
256 static int yuv4_probe(AVProbeData *pd)
257 {
258     /* check file header */
259     if (strncmp(pd->buf, Y4M_MAGIC, sizeof(Y4M_MAGIC) - 1) == 0)
260         return AVPROBE_SCORE_MAX;
261     else
262         return 0;
263 }
264
265 AVInputFormat ff_yuv4mpegpipe_demuxer = {
266     .name           = "yuv4mpegpipe",
267     .long_name      = NULL_IF_CONFIG_SMALL("YUV4MPEG pipe"),
268     .priv_data_size = sizeof(struct frame_attributes),
269     .read_probe     = yuv4_probe,
270     .read_header    = yuv4_read_header,
271     .read_packet    = yuv4_read_packet,
272     .extensions     = "y4m",
273 };