Browse code

Silicon Graphics Movie (.mv) demuxer

Signed-off-by: Peter Ross <pross@xvid.org>

Peter Ross authored on 2012/12/17 18:37:54
Showing 5 changed files
... ...
@@ -44,6 +44,7 @@ version <next>:
44 44
 - aselect filter
45 45
 - SGI RLE 8-bit decoder
46 46
 - Silicon Graphics Motion Video Compressor 1 & 2 decoder
47
+- Silicon Graphics Movie demuxer
47 48
 
48 49
 
49 50
 version 1.0:
... ...
@@ -359,6 +359,7 @@ library:
359 359
 @item SDP                       @tab   @tab X
360 360
 @item Sega FILM/CPK             @tab   @tab X
361 361
     @tab Used in many Sega Saturn console games.
362
+@item Silicon Graphics Movie    @tab   @tab X
362 363
 @item Sierra SOL                @tab   @tab X
363 364
     @tab .sol files used in Sierra Online games.
364 365
 @item Sierra VMD                @tab   @tab X
... ...
@@ -222,6 +222,7 @@ OBJS-$(CONFIG_MPJPEG_MUXER)              += mpjpeg.o
222 222
 OBJS-$(CONFIG_MSNWC_TCP_DEMUXER)         += msnwc_tcp.o
223 223
 OBJS-$(CONFIG_MTV_DEMUXER)               += mtv.o
224 224
 OBJS-$(CONFIG_MVI_DEMUXER)               += mvi.o
225
+OBJS-$(CONFIG_MV_DEMUXER)                += mvdec.o
225 226
 OBJS-$(CONFIG_MXF_DEMUXER)               += mxfdec.o mxf.o
226 227
 OBJS-$(CONFIG_MXF_MUXER)                 += mxfenc.o mxf.o audiointerleave.o
227 228
 OBJS-$(CONFIG_MXG_DEMUXER)               += mxg.o
... ...
@@ -172,6 +172,7 @@ void av_register_all(void)
172 172
     REGISTER_MUXER    (MPJPEG, mpjpeg);
173 173
     REGISTER_DEMUXER  (MSNWC_TCP, msnwc_tcp);
174 174
     REGISTER_DEMUXER  (MTV, mtv);
175
+    REGISTER_DEMUXER  (MV, mv);
175 176
     REGISTER_DEMUXER  (MVI, mvi);
176 177
     REGISTER_MUXDEMUX (MXF, mxf);
177 178
     REGISTER_MUXER    (MXF_D10, mxf_d10);
178 179
new file mode 100644
... ...
@@ -0,0 +1,424 @@
0
+/*
1
+ * Silicon Graphics Movie demuxer
2
+ * Copyright (c) 2012 Peter Ross
3
+ *
4
+ * This file is part of FFmpeg.
5
+ *
6
+ * FFmpeg is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * FFmpeg is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with FFmpeg; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+/**
22
+ * @file
23
+ * Silicon Graphics Movie demuxer
24
+ */
25
+
26
+#include "libavutil/eval.h"
27
+#include "libavutil/intreadwrite.h"
28
+#include "libavutil/rational.h"
29
+#include "avformat.h"
30
+#include "internal.h"
31
+
32
+typedef struct {
33
+    int nb_video_tracks;
34
+    int nb_audio_tracks;
35
+
36
+    int eof_count;        /**< number of streams that have finished */
37
+    int stream_index;     /**< current stream index */
38
+    int frame[2];         /**< frame nb for current stream */
39
+} MvContext;
40
+
41
+#define AUDIO_FORMAT_SIGNED 401
42
+
43
+static int mv_probe(AVProbeData *p)
44
+{
45
+    if (AV_RB32(p->buf) == MKBETAG('M','O','V','I') && AV_RB16(p->buf + 4) < 3)
46
+        return AVPROBE_SCORE_MAX;
47
+    return 0;
48
+}
49
+
50
+static char * var_read_string(AVIOContext *pb, int size)
51
+{
52
+    char *str = av_malloc(size + 1);
53
+    int n;
54
+    if (!str)
55
+        return NULL;
56
+    n = avio_get_str(pb, size, str, size + 1);
57
+    if (n < size)
58
+         avio_skip(pb, size - n);
59
+    return str;
60
+}
61
+
62
+static int var_read_int(AVIOContext *pb, int size)
63
+{
64
+    int v;
65
+    char * s = var_read_string(pb, size);
66
+    if (!s || sscanf(s, "%d", &v) != 1)
67
+        v = 0;
68
+    av_free(s);
69
+    return v;
70
+}
71
+
72
+static AVRational var_read_float(AVIOContext *pb, int size)
73
+{
74
+    AVRational v;
75
+    char * s = var_read_string(pb, size);
76
+    if (!s)
77
+        return (AVRational){0, 0};
78
+    v = av_d2q(av_strtod(s, NULL), INT_MAX);
79
+    av_free(s);
80
+    return v;
81
+}
82
+
83
+static void var_read_metadata(AVFormatContext *avctx, const char *tag, int size)
84
+{
85
+    char *value = var_read_string(avctx->pb, size);
86
+    if (value)
87
+        av_dict_set(&avctx->metadata, tag, value, AV_DICT_DONT_STRDUP_VAL);
88
+}
89
+
90
+/**
91
+ * Parse global variable
92
+ * @return < 0 if unknown
93
+ */
94
+static int parse_global_var(AVFormatContext *avctx, AVStream *st, const char *name, int size)
95
+{
96
+    MvContext *mv = avctx->priv_data;
97
+    AVIOContext *pb = avctx->pb;
98
+    if (!strcmp(name, "__NUM_I_TRACKS")) {
99
+        mv->nb_video_tracks = var_read_int(pb, size);
100
+    } else if (!strcmp(name, "__NUM_A_TRACKS")) {
101
+        mv->nb_audio_tracks = var_read_int(pb, size);
102
+    } else if (!strcmp(name, "COMMENT") || !strcmp(name, "TITLE")) {
103
+        var_read_metadata(avctx, name, size);
104
+    } else if (!strcmp(name, "LOOP_MODE") || !strcmp(name, "NUM_LOOPS") || !strcmp(name, "OPTIMIZED")) {
105
+        avio_skip(pb, size); // ignore
106
+    } else
107
+        return -1;
108
+
109
+    return 0;
110
+}
111
+
112
+/**
113
+ * Parse audio variable
114
+ * @return < 0 if unknown
115
+ */
116
+static int parse_audio_var(AVFormatContext *avctx, AVStream *st, const char *name, int size)
117
+{
118
+    AVIOContext *pb = avctx->pb;
119
+    if (!strcmp(name, "__DIR_COUNT")) {
120
+        st->nb_frames = var_read_int(pb, size);
121
+    } else if (!strcmp(name, "AUDIO_FORMAT")) {
122
+        st->codec->codec_id = var_read_int(pb, size);
123
+    } else if (!strcmp(name, "COMPRESSION")) {
124
+        st->codec->codec_tag = var_read_int(pb, size);
125
+    } else if (!strcmp(name, "DEFAULT_VOL")) {
126
+        var_read_metadata(avctx, name, size);
127
+    } else if (!strcmp(name, "NUM_CHANNELS")) {
128
+        st->codec->channels = var_read_int(pb, size);
129
+        st->codec->channel_layout = (st->codec->channels == 1) ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
130
+    } else if (!strcmp(name, "SAMPLE_RATE")) {
131
+        st->codec->sample_rate = var_read_int(pb, size);
132
+        avpriv_set_pts_info(st, 33, 1, st->codec->sample_rate);
133
+    } else if (!strcmp(name, "SAMPLE_WIDTH")) {
134
+        st->codec->bits_per_coded_sample = var_read_int(pb, size) * 8;
135
+    } else
136
+        return -1;
137
+    return 0;
138
+}
139
+
140
+/**
141
+ * Parse video variable
142
+ * @return < 0 if unknown
143
+ */
144
+static int parse_video_var(AVFormatContext *avctx, AVStream *st, const char *name, int size)
145
+{
146
+    AVIOContext *pb = avctx->pb;
147
+    if (!strcmp(name, "__DIR_COUNT")) {
148
+        st->nb_frames = st->duration = var_read_int(pb, size);
149
+    } else if (!strcmp(name, "COMPRESSION")) {
150
+        char * str = var_read_string(pb, size);
151
+        if (!strcmp(str, "1")) {
152
+            st->codec->codec_id = AV_CODEC_ID_MVC1;
153
+        } else if (!strcmp(str, "2")) {
154
+            st->codec->pix_fmt  = AV_PIX_FMT_ABGR;
155
+            st->codec->codec_id = AV_CODEC_ID_RAWVIDEO;
156
+        } else if (!strcmp(str, "3")) {
157
+            st->codec->codec_id = AV_CODEC_ID_SGIRLE;
158
+        } else if (!strcmp(str, "10")) {
159
+            st->codec->codec_id = AV_CODEC_ID_MJPEG;
160
+        } else if (!strcmp(str, "MVC2")) {
161
+            st->codec->codec_id = AV_CODEC_ID_MVC2;
162
+        } else {
163
+            av_log_ask_for_sample(avctx, "unknown video compression %s\n", str);
164
+        }
165
+        av_free(str);
166
+    } else if (!strcmp(name, "FPS")) {
167
+        st->time_base = av_inv_q(var_read_float(pb, size));
168
+    } else if (!strcmp(name, "HEIGHT")) {
169
+        st->codec->height = var_read_int(pb, size);
170
+    } else if (!strcmp(name, "PIXEL_ASPECT")) {
171
+        st->sample_aspect_ratio = var_read_float(pb, size);
172
+        av_reduce(&st->sample_aspect_ratio.num, &st->sample_aspect_ratio.den,
173
+                  st->sample_aspect_ratio.num, st->sample_aspect_ratio.den, INT_MAX);
174
+    } else if (!strcmp(name, "WIDTH")) {
175
+        st->codec->width = var_read_int(pb, size);
176
+    } else if (!strcmp(name, "ORIENTATION")) {
177
+        if (var_read_int(pb, size) == 1101) {
178
+            st->codec->extradata       = av_strdup("BottomUp");
179
+            st->codec->extradata_size  = 9;
180
+        }
181
+    } else if (!strcmp(name, "Q_SPATIAL") || !strcmp(name, "Q_TEMPORAL")) {
182
+        var_read_metadata(avctx, name, size);
183
+    } else if (!strcmp(name, "INTERLACING") || !strcmp(name, "PACKING")) {
184
+        avio_skip(pb, size); // ignore
185
+    } else
186
+        return -1;
187
+    return 0;
188
+}
189
+
190
+static void read_table(AVFormatContext *avctx, AVStream *st, int (*parse)(AVFormatContext *avctx, AVStream *st, const char *name, int size))
191
+{
192
+    int count, i;
193
+    AVIOContext *pb = avctx->pb;
194
+    avio_skip(pb, 4);
195
+    count = avio_rb32(pb);
196
+    avio_skip(pb, 4);
197
+    for (i = 0; i < count; i++) {
198
+        char name[17];
199
+        int size;
200
+        avio_read(pb, name, 16);
201
+        name[sizeof(name) - 1] = 0;
202
+        size = avio_rb32(pb);
203
+        if (parse(avctx, st, name, size) < 0) {
204
+            av_log_ask_for_sample(avctx, "unknown variable %s\n", name);
205
+            avio_skip(pb, size);
206
+        }
207
+    }
208
+}
209
+
210
+static void read_index(AVIOContext *pb, AVStream *st)
211
+{
212
+    uint64_t timestamp = 0;
213
+    int i;
214
+    for (i = 0; i < st->nb_frames; i++) {
215
+        uint32_t pos  = avio_rb32(pb);
216
+        uint32_t size = avio_rb32(pb);
217
+        avio_skip(pb, 8);
218
+        av_add_index_entry(st, pos, timestamp, size, 0, AVINDEX_KEYFRAME);
219
+        if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
220
+            timestamp += size / (st->codec->channels * 2);
221
+        } else {
222
+            timestamp++;
223
+        }
224
+    }
225
+}
226
+
227
+static int mv_read_header(AVFormatContext *avctx)
228
+{
229
+    MvContext *mv = avctx->priv_data;
230
+    AVIOContext *pb = avctx->pb;
231
+    AVStream *ast, *vst;
232
+    int version, i;
233
+
234
+    avio_skip(pb, 4);
235
+
236
+    version = avio_rb16(pb);
237
+    if (version == 2) {
238
+        uint64_t timestamp;
239
+        int v;
240
+        avio_skip(pb, 22);
241
+
242
+        /* allocate audio track first to prevent unnecessary seeking
243
+           (audio packet always precede video packet for a given frame) */
244
+        ast = avformat_new_stream(avctx, NULL);
245
+        if (!ast)
246
+            return AVERROR(ENOMEM);
247
+
248
+        vst = avformat_new_stream(avctx, NULL);
249
+        if (!vst)
250
+            return AVERROR(ENOMEM);
251
+        vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
252
+        vst->time_base = (AVRational){1, 15};
253
+        vst->nb_frames = avio_rb32(pb);
254
+        v = avio_rb32(pb);
255
+        switch (v) {
256
+        case 1:
257
+            vst->codec->codec_id = AV_CODEC_ID_MVC1;
258
+            break;
259
+        case 2:
260
+            vst->codec->pix_fmt  = AV_PIX_FMT_ARGB;
261
+            vst->codec->codec_id = AV_CODEC_ID_RAWVIDEO;
262
+            break;
263
+        default:
264
+            av_log_ask_for_sample(avctx, "unknown video compression %i\n", v);
265
+            break;
266
+        }
267
+        vst->codec->codec_tag = 0;
268
+        vst->codec->width     = avio_rb32(pb);
269
+        vst->codec->height    = avio_rb32(pb);
270
+        avio_skip(pb, 12);
271
+
272
+        ast->codec->codec_type     = AVMEDIA_TYPE_AUDIO;
273
+        ast->nb_frames             = vst->nb_frames;
274
+        ast->codec->sample_rate    = avio_rb32(pb);
275
+        avpriv_set_pts_info(ast, 33, 1, ast->codec->sample_rate);
276
+        ast->codec->channels       = avio_rb32(pb);
277
+        ast->codec->channel_layout = (ast->codec->channels == 1) ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
278
+        v = avio_rb32(pb);
279
+        if (v == AUDIO_FORMAT_SIGNED) {
280
+            ast->codec->codec_id = AV_CODEC_ID_PCM_S16BE;
281
+        } else {
282
+            av_log_ask_for_sample(avctx, "unknown audio compression (format %i)\n", v);
283
+        }
284
+
285
+        avio_skip(pb, 12);
286
+        var_read_metadata(avctx, "title", 0x80);
287
+        var_read_metadata(avctx, "comment", 0x100);
288
+        avio_skip(pb, 0x80);
289
+
290
+        timestamp = 0;
291
+        for (i = 0; i < vst->nb_frames; i++) {
292
+            uint32_t pos   = avio_rb32(pb);
293
+            uint32_t asize = avio_rb32(pb);
294
+            uint32_t vsize = avio_rb32(pb);
295
+            avio_skip(pb, 8);
296
+            av_add_index_entry(ast, pos,         timestamp, asize, 0, AVINDEX_KEYFRAME);
297
+            av_add_index_entry(vst, pos + asize, i,         vsize, 0, AVINDEX_KEYFRAME);
298
+            timestamp += asize / (ast->codec->channels * 2);
299
+        }
300
+    } else if (!version && avio_rb16(pb) == 3) {
301
+        avio_skip(pb, 4);
302
+
303
+        read_table(avctx, NULL, parse_global_var);
304
+
305
+        if (mv->nb_audio_tracks > 1) {
306
+            av_log_ask_for_sample(avctx, "multiple audio streams\n");
307
+            return AVERROR_PATCHWELCOME;
308
+        } else if (mv->nb_audio_tracks) {
309
+            ast = avformat_new_stream(avctx, NULL);
310
+            if (!ast)
311
+                return AVERROR(ENOMEM);
312
+            ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
313
+            /* temporarily store compression value in codec_tag; format value in codec_id */
314
+            read_table(avctx, ast, parse_audio_var);
315
+            if (ast->codec->codec_tag == 100 && ast->codec->codec_id == AUDIO_FORMAT_SIGNED && ast->codec->bits_per_coded_sample == 16) {
316
+                ast->codec->codec_id = AV_CODEC_ID_PCM_S16BE;
317
+            } else {
318
+                av_log_ask_for_sample(avctx, "unknown audio compression %i (format %i, width %i)\n",
319
+                    ast->codec->codec_tag, ast->codec->codec_id, ast->codec->bits_per_coded_sample);
320
+                ast->codec->codec_id = AV_CODEC_ID_NONE;
321
+            }
322
+            ast->codec->codec_tag = 0;
323
+        }
324
+
325
+        if (mv->nb_video_tracks > 1) {
326
+            av_log_ask_for_sample(avctx, "multiple video streams\n");
327
+            return AVERROR_PATCHWELCOME;
328
+        } else if (mv->nb_video_tracks) {
329
+            vst = avformat_new_stream(avctx, NULL);
330
+            if (!vst)
331
+                return AVERROR(ENOMEM);
332
+            vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
333
+            read_table(avctx, vst, parse_video_var);
334
+        }
335
+
336
+        if (mv->nb_audio_tracks)
337
+            read_index(pb, ast);
338
+
339
+        if (mv->nb_video_tracks)
340
+            read_index(pb, vst);
341
+    } else {
342
+        av_log_ask_for_sample(avctx, "unknown version %i\n", version);
343
+        return AVERROR_PATCHWELCOME;
344
+    }
345
+
346
+    return 0;
347
+}
348
+
349
+static int mv_read_packet(AVFormatContext *avctx, AVPacket *pkt)
350
+{
351
+    MvContext *mv = avctx->priv_data;
352
+    AVIOContext *pb = avctx->pb;
353
+    AVStream *st = avctx->streams[mv->stream_index];
354
+    const AVIndexEntry *index;
355
+    int frame = mv->frame[mv->stream_index];
356
+    int ret;
357
+    uint64_t pos;
358
+
359
+    if (frame  < st->nb_frames) {
360
+        index = &st->index_entries[frame];
361
+        pos = avio_tell(pb);
362
+        if (index->pos > pos)
363
+            avio_skip(pb, index->pos - pos);
364
+        else if (index->pos < pos) {
365
+            if (!pb->seekable)
366
+                return AVERROR(EIO);
367
+            ret = avio_seek(pb, index->pos, SEEK_SET);
368
+            if (ret < 0)
369
+                return ret;
370
+        }
371
+        ret = av_get_packet(pb, pkt, index->size);
372
+        if (ret < 0)
373
+            return ret;
374
+
375
+        pkt->stream_index = mv->stream_index;
376
+        pkt->pts = index->timestamp;
377
+        pkt->flags |= AV_PKT_FLAG_KEY;
378
+
379
+        mv->frame[mv->stream_index]++;
380
+        mv->eof_count = 0;
381
+    } else {
382
+        mv->eof_count++;
383
+        if (mv->eof_count >= avctx->nb_streams)
384
+            return AVERROR_EOF;
385
+    }
386
+
387
+    mv->stream_index++;
388
+    if (mv->stream_index >= avctx->nb_streams)
389
+        mv->stream_index = 0;
390
+
391
+    return 0;
392
+}
393
+
394
+static int mv_read_seek(AVFormatContext *avctx, int stream_index, int64_t timestamp, int flags)
395
+{
396
+    MvContext *mv = avctx->priv_data;
397
+    AVStream *st = avctx->streams[stream_index];
398
+    int frame, i;
399
+
400
+    if ((flags & AVSEEK_FLAG_FRAME) || (flags & AVSEEK_FLAG_BYTE))
401
+        return AVERROR(ENOSYS);
402
+
403
+    if (!avctx->pb->seekable)
404
+        return AVERROR(EIO);
405
+
406
+    frame = av_index_search_timestamp(st, timestamp, flags);
407
+    if (frame < 0)
408
+        return -1;
409
+
410
+    for (i = 0; i < avctx->nb_streams; i++)
411
+        mv->frame[i] = frame;
412
+    return 0;
413
+}
414
+
415
+AVInputFormat ff_mv_demuxer = {
416
+    .name           = "mv",
417
+    .long_name      = NULL_IF_CONFIG_SMALL("Silicon Graphics Movie"),
418
+    .priv_data_size = sizeof(MvContext),
419
+    .read_probe     = mv_probe,
420
+    .read_header    = mv_read_header,
421
+    .read_packet    = mv_read_packet,
422
+    .read_seek      = mv_read_seek,
423
+};