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