OSDN Git Service

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