]> git.sesse.net Git - ffmpeg/blob - libavformat/mvdec.c
avcodec/dvbsubdec: prefer to use variable instead of type for sizeof
[ffmpeg] / libavformat / mvdec.c
1 /*
2  * Silicon Graphics Movie demuxer
3  * Copyright (c) 2012 Peter Ross
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 /**
23  * @file
24  * Silicon Graphics Movie demuxer
25  */
26
27 #include "libavutil/channel_layout.h"
28 #include "libavutil/eval.h"
29 #include "libavutil/intreadwrite.h"
30 #include "libavutil/rational.h"
31
32 #include "avformat.h"
33 #include "internal.h"
34
35 typedef struct MvContext {
36     int nb_video_tracks;
37     int nb_audio_tracks;
38
39     int eof_count;        ///< number of streams that have finished
40     int stream_index;     ///< current stream index
41     int frame[2];         ///< frame nb for current stream
42
43     int acompression;     ///< compression level for audio stream
44     int aformat;          ///< audio format
45 } MvContext;
46
47 #define AUDIO_FORMAT_SIGNED 401
48
49 static int mv_probe(const AVProbeData *p)
50 {
51     if (AV_RB32(p->buf) == MKBETAG('M', 'O', 'V', 'I') &&
52         AV_RB16(p->buf + 4) < 3)
53         return AVPROBE_SCORE_MAX;
54     return 0;
55 }
56
57 static char *var_read_string(AVIOContext *pb, int size)
58 {
59     int n;
60     char *str;
61
62     if (size < 0 || size == INT_MAX)
63         return NULL;
64
65     str = av_malloc(size + 1);
66     if (!str)
67         return NULL;
68     n = avio_get_str(pb, size, str, size + 1);
69     if (n < size)
70         avio_skip(pb, size - n);
71     return str;
72 }
73
74 static int var_read_int(AVIOContext *pb, int size)
75 {
76     int v;
77     char *s = var_read_string(pb, size);
78     if (!s)
79         return 0;
80     v = strtol(s, NULL, 10);
81     av_free(s);
82     return v;
83 }
84
85 static AVRational var_read_float(AVIOContext *pb, int size)
86 {
87     AVRational v;
88     char *s = var_read_string(pb, size);
89     if (!s)
90         return (AVRational) { 0, 0 };
91     v = av_d2q(av_strtod(s, NULL), INT_MAX);
92     av_free(s);
93     return v;
94 }
95
96 static void var_read_metadata(AVFormatContext *avctx, const char *tag, int size)
97 {
98     char *value = var_read_string(avctx->pb, size);
99     if (value)
100         av_dict_set(&avctx->metadata, tag, value, AV_DICT_DONT_STRDUP_VAL);
101 }
102
103 static int set_channels(AVFormatContext *avctx, AVStream *st, int channels)
104 {
105     if (channels <= 0) {
106         av_log(avctx, AV_LOG_ERROR, "Channel count %d invalid.\n", channels);
107         return AVERROR_INVALIDDATA;
108     }
109     st->codecpar->channels       = channels;
110     st->codecpar->channel_layout = (st->codecpar->channels == 1) ? AV_CH_LAYOUT_MONO
111                                                                  : AV_CH_LAYOUT_STEREO;
112     return 0;
113 }
114
115 /**
116  * Parse global variable
117  * @return < 0 if unknown
118  */
119 static int parse_global_var(AVFormatContext *avctx, AVStream *st,
120                             const char *name, int size)
121 {
122     MvContext *mv = avctx->priv_data;
123     AVIOContext *pb = avctx->pb;
124     if (!strcmp(name, "__NUM_I_TRACKS")) {
125         mv->nb_video_tracks = var_read_int(pb, size);
126     } else if (!strcmp(name, "__NUM_A_TRACKS")) {
127         mv->nb_audio_tracks = var_read_int(pb, size);
128     } else if (!strcmp(name, "COMMENT") || !strcmp(name, "TITLE")) {
129         var_read_metadata(avctx, name, size);
130     } else if (!strcmp(name, "LOOP_MODE") || !strcmp(name, "NUM_LOOPS") ||
131                !strcmp(name, "OPTIMIZED")) {
132         avio_skip(pb, size); // ignore
133     } else
134         return AVERROR_INVALIDDATA;
135
136     return 0;
137 }
138
139 /**
140  * Parse audio variable
141  * @return < 0 if unknown
142  */
143 static int parse_audio_var(AVFormatContext *avctx, AVStream *st,
144                            const char *name, int size)
145 {
146     MvContext *mv = avctx->priv_data;
147     AVIOContext *pb = avctx->pb;
148     if (!strcmp(name, "__DIR_COUNT")) {
149         st->nb_frames = var_read_int(pb, size);
150     } else if (!strcmp(name, "AUDIO_FORMAT")) {
151         mv->aformat = var_read_int(pb, size);
152     } else if (!strcmp(name, "COMPRESSION")) {
153         mv->acompression = var_read_int(pb, size);
154     } else if (!strcmp(name, "DEFAULT_VOL")) {
155         var_read_metadata(avctx, name, size);
156     } else if (!strcmp(name, "NUM_CHANNELS")) {
157         return set_channels(avctx, st, var_read_int(pb, size));
158     } else if (!strcmp(name, "SAMPLE_RATE")) {
159         st->codecpar->sample_rate = var_read_int(pb, size);
160         avpriv_set_pts_info(st, 33, 1, st->codecpar->sample_rate);
161     } else if (!strcmp(name, "SAMPLE_WIDTH")) {
162         st->codecpar->bits_per_coded_sample = var_read_int(pb, size) * 8;
163     } else
164         return AVERROR_INVALIDDATA;
165
166     return 0;
167 }
168
169 /**
170  * Parse video variable
171  * @return < 0 if unknown
172  */
173 static int parse_video_var(AVFormatContext *avctx, AVStream *st,
174                            const char *name, int size)
175 {
176     AVIOContext *pb = avctx->pb;
177     if (!strcmp(name, "__DIR_COUNT")) {
178         st->nb_frames = st->duration = var_read_int(pb, size);
179     } else if (!strcmp(name, "COMPRESSION")) {
180         char *str = var_read_string(pb, size);
181         if (!str)
182             return AVERROR_INVALIDDATA;
183         if (!strcmp(str, "1")) {
184             st->codecpar->codec_id = AV_CODEC_ID_MVC1;
185         } else if (!strcmp(str, "2")) {
186             st->codecpar->format = AV_PIX_FMT_ABGR;
187             st->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO;
188         } else if (!strcmp(str, "3")) {
189             st->codecpar->codec_id = AV_CODEC_ID_SGIRLE;
190         } else if (!strcmp(str, "10")) {
191             st->codecpar->codec_id = AV_CODEC_ID_MJPEG;
192         } else if (!strcmp(str, "MVC2")) {
193             st->codecpar->codec_id = AV_CODEC_ID_MVC2;
194         } else {
195             avpriv_request_sample(avctx, "Video compression %s", str);
196         }
197         av_free(str);
198     } else if (!strcmp(name, "FPS")) {
199         AVRational fps = var_read_float(pb, size);
200         avpriv_set_pts_info(st, 64, fps.den, fps.num);
201         st->avg_frame_rate = fps;
202     } else if (!strcmp(name, "HEIGHT")) {
203         st->codecpar->height = var_read_int(pb, size);
204     } else if (!strcmp(name, "PIXEL_ASPECT")) {
205         st->sample_aspect_ratio = var_read_float(pb, size);
206         av_reduce(&st->sample_aspect_ratio.num, &st->sample_aspect_ratio.den,
207                   st->sample_aspect_ratio.num, st->sample_aspect_ratio.den,
208                   INT_MAX);
209     } else if (!strcmp(name, "WIDTH")) {
210         st->codecpar->width = var_read_int(pb, size);
211     } else if (!strcmp(name, "ORIENTATION")) {
212         if (var_read_int(pb, size) == 1101) {
213             st->codecpar->extradata      = av_strdup("BottomUp");
214             if (!st->codecpar->extradata)
215                 return AVERROR(ENOMEM);
216             st->codecpar->extradata_size = 9;
217         }
218     } else if (!strcmp(name, "Q_SPATIAL") || !strcmp(name, "Q_TEMPORAL")) {
219         var_read_metadata(avctx, name, size);
220     } else if (!strcmp(name, "INTERLACING") || !strcmp(name, "PACKING")) {
221         avio_skip(pb, size); // ignore
222     } else
223         return AVERROR_INVALIDDATA;
224
225     return 0;
226 }
227
228 static int read_table(AVFormatContext *avctx, AVStream *st,
229                        int (*parse)(AVFormatContext *avctx, AVStream *st,
230                                     const char *name, int size))
231 {
232     unsigned count;
233     int i;
234
235     AVIOContext *pb = avctx->pb;
236     avio_skip(pb, 4);
237     count = avio_rb32(pb);
238     avio_skip(pb, 4);
239     for (i = 0; i < count; i++) {
240         char name[17];
241         int size;
242
243         if (avio_feof(pb))
244             return AVERROR_EOF;
245
246         avio_read(pb, name, 16);
247         name[sizeof(name) - 1] = 0;
248         size = avio_rb32(pb);
249         if (size < 0) {
250             av_log(avctx, AV_LOG_ERROR, "entry size %d is invalid\n", size);
251             return AVERROR_INVALIDDATA;
252         }
253         if (parse(avctx, st, name, size) < 0) {
254             avpriv_request_sample(avctx, "Variable %s", name);
255             avio_skip(pb, size);
256         }
257     }
258     return 0;
259 }
260
261 static void read_index(AVIOContext *pb, AVStream *st)
262 {
263     uint64_t timestamp = 0;
264     int i;
265     for (i = 0; i < st->nb_frames; i++) {
266         uint32_t pos  = avio_rb32(pb);
267         uint32_t size = avio_rb32(pb);
268         avio_skip(pb, 8);
269         av_add_index_entry(st, pos, timestamp, size, 0, AVINDEX_KEYFRAME);
270         if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
271             timestamp += size / (st->codecpar->channels * 2LL);
272         } else {
273             timestamp++;
274         }
275     }
276 }
277
278 static int mv_read_header(AVFormatContext *avctx)
279 {
280     MvContext *mv = avctx->priv_data;
281     AVIOContext *pb = avctx->pb;
282     AVStream *ast = NULL, *vst = NULL; //initialization to suppress warning
283     int version, i;
284     int ret;
285
286     avio_skip(pb, 4);
287
288     version = avio_rb16(pb);
289     if (version == 2) {
290         uint64_t timestamp;
291         int v;
292         avio_skip(pb, 22);
293
294         /* allocate audio track first to prevent unnecessary seeking
295          * (audio packet always precede video packet for a given frame) */
296         ast = avformat_new_stream(avctx, NULL);
297         if (!ast)
298             return AVERROR(ENOMEM);
299
300         vst = avformat_new_stream(avctx, NULL);
301         if (!vst)
302             return AVERROR(ENOMEM);
303         avpriv_set_pts_info(vst, 64, 1, 15);
304         vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
305         vst->avg_frame_rate    = av_inv_q(vst->time_base);
306         vst->nb_frames         = avio_rb32(pb);
307         v = avio_rb32(pb);
308         switch (v) {
309         case 1:
310             vst->codecpar->codec_id = AV_CODEC_ID_MVC1;
311             break;
312         case 2:
313             vst->codecpar->format = AV_PIX_FMT_ARGB;
314             vst->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO;
315             break;
316         default:
317             avpriv_request_sample(avctx, "Video compression %i", v);
318             break;
319         }
320         vst->codecpar->codec_tag = 0;
321         vst->codecpar->width     = avio_rb32(pb);
322         vst->codecpar->height    = avio_rb32(pb);
323         avio_skip(pb, 12);
324
325         ast->codecpar->codec_type  = AVMEDIA_TYPE_AUDIO;
326         ast->nb_frames          = vst->nb_frames;
327         ast->codecpar->sample_rate = avio_rb32(pb);
328         if (ast->codecpar->sample_rate <= 0) {
329             av_log(avctx, AV_LOG_ERROR, "Invalid sample rate %d\n", ast->codecpar->sample_rate);
330             return AVERROR_INVALIDDATA;
331         }
332         avpriv_set_pts_info(ast, 33, 1, ast->codecpar->sample_rate);
333         if (set_channels(avctx, ast, avio_rb32(pb)) < 0)
334             return AVERROR_INVALIDDATA;
335
336         v = avio_rb32(pb);
337         if (v == AUDIO_FORMAT_SIGNED) {
338             ast->codecpar->codec_id = AV_CODEC_ID_PCM_S16BE;
339         } else {
340             avpriv_request_sample(avctx, "Audio compression (format %i)", v);
341         }
342
343         avio_skip(pb, 12);
344         var_read_metadata(avctx, "title", 0x80);
345         var_read_metadata(avctx, "comment", 0x100);
346         avio_skip(pb, 0x80);
347
348         timestamp = 0;
349         for (i = 0; i < vst->nb_frames; i++) {
350             uint32_t pos   = avio_rb32(pb);
351             uint32_t asize = avio_rb32(pb);
352             uint32_t vsize = avio_rb32(pb);
353             if (avio_feof(pb))
354                 return AVERROR_INVALIDDATA;
355             avio_skip(pb, 8);
356             av_add_index_entry(ast, pos, timestamp, asize, 0, AVINDEX_KEYFRAME);
357             av_add_index_entry(vst, pos + asize, i, vsize, 0, AVINDEX_KEYFRAME);
358             timestamp += asize / (ast->codecpar->channels * 2LL);
359         }
360     } else if (!version && avio_rb16(pb) == 3) {
361         avio_skip(pb, 4);
362
363         if ((ret = read_table(avctx, NULL, parse_global_var)) < 0)
364             return ret;
365
366         if (mv->nb_audio_tracks < 0  || mv->nb_video_tracks < 0 ||
367            (mv->nb_audio_tracks == 0 && mv->nb_video_tracks == 0)) {
368             av_log(avctx, AV_LOG_ERROR, "Stream count is invalid.\n");
369             return AVERROR_INVALIDDATA;
370         }
371
372         if (mv->nb_audio_tracks > 1) {
373             avpriv_request_sample(avctx, "Multiple audio streams support");
374             return AVERROR_PATCHWELCOME;
375         } else if (mv->nb_audio_tracks) {
376             ast = avformat_new_stream(avctx, NULL);
377             if (!ast)
378                 return AVERROR(ENOMEM);
379             ast->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
380             if ((read_table(avctx, ast, parse_audio_var)) < 0)
381                 return ret;
382             if (mv->acompression == 100 &&
383                 mv->aformat == AUDIO_FORMAT_SIGNED &&
384                 ast->codecpar->bits_per_coded_sample == 16) {
385                 ast->codecpar->codec_id = AV_CODEC_ID_PCM_S16BE;
386             } else {
387                 avpriv_request_sample(avctx,
388                                       "Audio compression %i (format %i, sr %i)",
389                                       mv->acompression, mv->aformat,
390                                       ast->codecpar->bits_per_coded_sample);
391                 ast->codecpar->codec_id = AV_CODEC_ID_NONE;
392             }
393             if (ast->codecpar->channels <= 0) {
394                 av_log(avctx, AV_LOG_ERROR, "No valid channel count found.\n");
395                 return AVERROR_INVALIDDATA;
396             }
397         }
398
399         if (mv->nb_video_tracks > 1) {
400             avpriv_request_sample(avctx, "Multiple video streams support");
401             return AVERROR_PATCHWELCOME;
402         } else if (mv->nb_video_tracks) {
403             vst = avformat_new_stream(avctx, NULL);
404             if (!vst)
405                 return AVERROR(ENOMEM);
406             vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
407             if ((ret = read_table(avctx, vst, parse_video_var))<0)
408                 return ret;
409         }
410
411         if (mv->nb_audio_tracks)
412             read_index(pb, ast);
413
414         if (mv->nb_video_tracks)
415             read_index(pb, vst);
416     } else {
417         avpriv_request_sample(avctx, "Version %i", version);
418         return AVERROR_PATCHWELCOME;
419     }
420
421     return 0;
422 }
423
424 static int mv_read_packet(AVFormatContext *avctx, AVPacket *pkt)
425 {
426     MvContext *mv = avctx->priv_data;
427     AVIOContext *pb = avctx->pb;
428     AVStream *st = avctx->streams[mv->stream_index];
429     const AVIndexEntry *index;
430     int frame = mv->frame[mv->stream_index];
431     int64_t ret;
432     uint64_t pos;
433
434     if (frame < st->nb_index_entries) {
435         index = &st->index_entries[frame];
436         pos   = avio_tell(pb);
437         if (index->pos > pos)
438             avio_skip(pb, index->pos - pos);
439         else if (index->pos < pos) {
440             if (!(pb->seekable & AVIO_SEEKABLE_NORMAL))
441                 return AVERROR(EIO);
442             ret = avio_seek(pb, index->pos, SEEK_SET);
443             if (ret < 0)
444                 return ret;
445         }
446         ret = av_get_packet(pb, pkt, index->size);
447         if (ret < 0)
448             return ret;
449
450         pkt->stream_index = mv->stream_index;
451         pkt->pts          = index->timestamp;
452         pkt->flags       |= AV_PKT_FLAG_KEY;
453
454         mv->frame[mv->stream_index]++;
455         mv->eof_count = 0;
456     } else {
457         mv->eof_count++;
458         if (mv->eof_count >= avctx->nb_streams)
459             return AVERROR_EOF;
460
461         // avoid returning 0 without a packet
462         return AVERROR(EAGAIN);
463     }
464
465     mv->stream_index++;
466     if (mv->stream_index >= avctx->nb_streams)
467         mv->stream_index = 0;
468
469     return 0;
470 }
471
472 static int mv_read_seek(AVFormatContext *avctx, int stream_index,
473                         int64_t timestamp, int flags)
474 {
475     MvContext *mv = avctx->priv_data;
476     AVStream *st = avctx->streams[stream_index];
477     int frame, i;
478
479     if ((flags & AVSEEK_FLAG_FRAME) || (flags & AVSEEK_FLAG_BYTE))
480         return AVERROR(ENOSYS);
481
482     if (!(avctx->pb->seekable & AVIO_SEEKABLE_NORMAL))
483         return AVERROR(EIO);
484
485     frame = av_index_search_timestamp(st, timestamp, flags);
486     if (frame < 0)
487         return AVERROR_INVALIDDATA;
488
489     for (i = 0; i < avctx->nb_streams; i++)
490         mv->frame[i] = frame;
491     return 0;
492 }
493
494 AVInputFormat ff_mv_demuxer = {
495     .name           = "mv",
496     .long_name      = NULL_IF_CONFIG_SMALL("Silicon Graphics Movie"),
497     .priv_data_size = sizeof(MvContext),
498     .read_probe     = mv_probe,
499     .read_header    = mv_read_header,
500     .read_packet    = mv_read_packet,
501     .read_seek      = mv_read_seek,
502 };