2 * Silicon Graphics Movie demuxer
3 * Copyright (c) 2012 Peter Ross
5 * This file is part of FFmpeg.
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.
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.
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
24 * Silicon Graphics Movie demuxer
27 #include "libavutil/eval.h"
28 #include "libavutil/intreadwrite.h"
29 #include "libavutil/rational.h"
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 */
42 #define AUDIO_FORMAT_SIGNED 401
44 static int mv_probe(AVProbeData *p)
46 if (AV_RB32(p->buf) == MKBETAG('M','O','V','I') && AV_RB16(p->buf + 4) < 3)
47 return AVPROBE_SCORE_MAX;
51 static char * var_read_string(AVIOContext *pb, int size)
53 char *str = av_malloc(size + 1);
57 n = avio_get_str(pb, size, str, size + 1);
59 avio_skip(pb, size - n);
63 static int var_read_int(AVIOContext *pb, int size)
66 char * s = var_read_string(pb, size);
67 if (!s || sscanf(s, "%d", &v) != 1)
73 static AVRational var_read_float(AVIOContext *pb, int size)
76 char * s = var_read_string(pb, size);
78 return (AVRational){0, 0};
79 v = av_d2q(av_strtod(s, NULL), INT_MAX);
84 static void var_read_metadata(AVFormatContext *avctx, const char *tag, int size)
86 char *value = var_read_string(avctx->pb, size);
88 av_dict_set(&avctx->metadata, tag, value, AV_DICT_DONT_STRDUP_VAL);
92 * Parse global variable
93 * @return < 0 if unknown
95 static int parse_global_var(AVFormatContext *avctx, AVStream *st, const char *name, int size)
97 MvContext *mv = avctx->priv_data;
98 AVIOContext *pb = avctx->pb;
99 if (!strcmp(name, "__NUM_I_TRACKS")) {
100 mv->nb_video_tracks = var_read_int(pb, size);
101 } else if (!strcmp(name, "__NUM_A_TRACKS")) {
102 mv->nb_audio_tracks = var_read_int(pb, size);
103 } else if (!strcmp(name, "COMMENT") || !strcmp(name, "TITLE")) {
104 var_read_metadata(avctx, name, size);
105 } else if (!strcmp(name, "LOOP_MODE") || !strcmp(name, "NUM_LOOPS") || !strcmp(name, "OPTIMIZED")) {
106 avio_skip(pb, size); // ignore
114 * Parse audio variable
115 * @return < 0 if unknown
117 static int parse_audio_var(AVFormatContext *avctx, AVStream *st, const char *name, int size)
119 AVIOContext *pb = avctx->pb;
120 if (!strcmp(name, "__DIR_COUNT")) {
121 st->nb_frames = var_read_int(pb, size);
122 } else if (!strcmp(name, "AUDIO_FORMAT")) {
123 st->codec->codec_id = var_read_int(pb, size);
124 } else if (!strcmp(name, "COMPRESSION")) {
125 st->codec->codec_tag = var_read_int(pb, size);
126 } else if (!strcmp(name, "DEFAULT_VOL")) {
127 var_read_metadata(avctx, name, size);
128 } else if (!strcmp(name, "NUM_CHANNELS")) {
129 st->codec->channels = var_read_int(pb, size);
130 st->codec->channel_layout = (st->codec->channels == 1) ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
131 } else if (!strcmp(name, "SAMPLE_RATE")) {
132 st->codec->sample_rate = var_read_int(pb, size);
133 avpriv_set_pts_info(st, 33, 1, st->codec->sample_rate);
134 } else if (!strcmp(name, "SAMPLE_WIDTH")) {
135 st->codec->bits_per_coded_sample = var_read_int(pb, size) * 8;
142 * Parse video variable
143 * @return < 0 if unknown
145 static int parse_video_var(AVFormatContext *avctx, AVStream *st, const char *name, int size)
147 AVIOContext *pb = avctx->pb;
148 if (!strcmp(name, "__DIR_COUNT")) {
149 st->nb_frames = st->duration = var_read_int(pb, size);
150 } else if (!strcmp(name, "COMPRESSION")) {
151 char * str = var_read_string(pb, size);
152 if (!strcmp(str, "1")) {
153 st->codec->codec_id = AV_CODEC_ID_MVC1;
154 } else if (!strcmp(str, "2")) {
155 st->codec->pix_fmt = AV_PIX_FMT_ABGR;
156 st->codec->codec_id = AV_CODEC_ID_RAWVIDEO;
157 } else if (!strcmp(str, "3")) {
158 st->codec->codec_id = AV_CODEC_ID_SGIRLE;
159 } else if (!strcmp(str, "10")) {
160 st->codec->codec_id = AV_CODEC_ID_MJPEG;
161 } else if (!strcmp(str, "MVC2")) {
162 st->codec->codec_id = AV_CODEC_ID_MVC2;
164 av_log_ask_for_sample(avctx, "unknown video compression %s\n", str);
167 } else if (!strcmp(name, "FPS")) {
168 AVRational fps = var_read_float(pb, size);
169 avpriv_set_pts_info(st, 64, fps.den, fps.num);
170 } else if (!strcmp(name, "HEIGHT")) {
171 st->codec->height = var_read_int(pb, size);
172 } else if (!strcmp(name, "PIXEL_ASPECT")) {
173 st->sample_aspect_ratio = var_read_float(pb, size);
174 av_reduce(&st->sample_aspect_ratio.num, &st->sample_aspect_ratio.den,
175 st->sample_aspect_ratio.num, st->sample_aspect_ratio.den, INT_MAX);
176 } else if (!strcmp(name, "WIDTH")) {
177 st->codec->width = var_read_int(pb, size);
178 } else if (!strcmp(name, "ORIENTATION")) {
179 if (var_read_int(pb, size) == 1101) {
180 st->codec->extradata = av_strdup("BottomUp");
181 st->codec->extradata_size = 9;
183 } else if (!strcmp(name, "Q_SPATIAL") || !strcmp(name, "Q_TEMPORAL")) {
184 var_read_metadata(avctx, name, size);
185 } else if (!strcmp(name, "INTERLACING") || !strcmp(name, "PACKING")) {
186 avio_skip(pb, size); // ignore
192 static void read_table(AVFormatContext *avctx, AVStream *st, int (*parse)(AVFormatContext *avctx, AVStream *st, const char *name, int size))
195 AVIOContext *pb = avctx->pb;
197 count = avio_rb32(pb);
199 for (i = 0; i < count; i++) {
202 avio_read(pb, name, 16);
203 name[sizeof(name) - 1] = 0;
204 size = avio_rb32(pb);
205 if (parse(avctx, st, name, size) < 0) {
206 av_log_ask_for_sample(avctx, "unknown variable %s\n", name);
212 static void read_index(AVIOContext *pb, AVStream *st)
214 uint64_t timestamp = 0;
216 for (i = 0; i < st->nb_frames; i++) {
217 uint32_t pos = avio_rb32(pb);
218 uint32_t size = avio_rb32(pb);
220 av_add_index_entry(st, pos, timestamp, size, 0, AVINDEX_KEYFRAME);
221 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
222 timestamp += size / (st->codec->channels * 2);
229 static int mv_read_header(AVFormatContext *avctx)
231 MvContext *mv = avctx->priv_data;
232 AVIOContext *pb = avctx->pb;
233 AVStream *ast = NULL, *vst = NULL; //initialization to suppress warning
238 version = avio_rb16(pb);
244 /* allocate audio track first to prevent unnecessary seeking
245 (audio packet always precede video packet for a given frame) */
246 ast = avformat_new_stream(avctx, NULL);
248 return AVERROR(ENOMEM);
250 vst = avformat_new_stream(avctx, NULL);
252 return AVERROR(ENOMEM);
253 vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
254 avpriv_set_pts_info(vst, 64, 1, 15);
255 vst->nb_frames = avio_rb32(pb);
259 vst->codec->codec_id = AV_CODEC_ID_MVC1;
262 vst->codec->pix_fmt = AV_PIX_FMT_ARGB;
263 vst->codec->codec_id = AV_CODEC_ID_RAWVIDEO;
266 av_log_ask_for_sample(avctx, "unknown video compression %i\n", v);
269 vst->codec->codec_tag = 0;
270 vst->codec->width = avio_rb32(pb);
271 vst->codec->height = avio_rb32(pb);
274 ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
275 ast->nb_frames = vst->nb_frames;
276 ast->codec->sample_rate = avio_rb32(pb);
277 avpriv_set_pts_info(ast, 33, 1, ast->codec->sample_rate);
278 ast->codec->channels = avio_rb32(pb);
279 ast->codec->channel_layout = (ast->codec->channels == 1) ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
281 if (v == AUDIO_FORMAT_SIGNED) {
282 ast->codec->codec_id = AV_CODEC_ID_PCM_S16BE;
284 av_log_ask_for_sample(avctx, "unknown audio compression (format %i)\n", v);
288 var_read_metadata(avctx, "title", 0x80);
289 var_read_metadata(avctx, "comment", 0x100);
293 for (i = 0; i < vst->nb_frames; i++) {
294 uint32_t pos = avio_rb32(pb);
295 uint32_t asize = avio_rb32(pb);
296 uint32_t vsize = avio_rb32(pb);
298 av_add_index_entry(ast, pos, timestamp, asize, 0, AVINDEX_KEYFRAME);
299 av_add_index_entry(vst, pos + asize, i, vsize, 0, AVINDEX_KEYFRAME);
300 timestamp += asize / (ast->codec->channels * 2);
302 } else if (!version && avio_rb16(pb) == 3) {
305 read_table(avctx, NULL, parse_global_var);
307 if (mv->nb_audio_tracks > 1) {
308 av_log_ask_for_sample(avctx, "multiple audio streams\n");
309 return AVERROR_PATCHWELCOME;
310 } else if (mv->nb_audio_tracks) {
311 ast = avformat_new_stream(avctx, NULL);
313 return AVERROR(ENOMEM);
314 ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
315 /* temporarily store compression value in codec_tag; format value in codec_id */
316 read_table(avctx, ast, parse_audio_var);
317 if (ast->codec->codec_tag == 100 && ast->codec->codec_id == AUDIO_FORMAT_SIGNED && ast->codec->bits_per_coded_sample == 16) {
318 ast->codec->codec_id = AV_CODEC_ID_PCM_S16BE;
320 av_log_ask_for_sample(avctx, "unknown audio compression %i (format %i, width %i)\n",
321 ast->codec->codec_tag, ast->codec->codec_id, ast->codec->bits_per_coded_sample);
322 ast->codec->codec_id = AV_CODEC_ID_NONE;
324 ast->codec->codec_tag = 0;
327 if (mv->nb_video_tracks > 1) {
328 av_log_ask_for_sample(avctx, "multiple video streams\n");
329 return AVERROR_PATCHWELCOME;
330 } else if (mv->nb_video_tracks) {
331 vst = avformat_new_stream(avctx, NULL);
333 return AVERROR(ENOMEM);
334 vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
335 read_table(avctx, vst, parse_video_var);
338 if (mv->nb_audio_tracks)
341 if (mv->nb_video_tracks)
344 av_log_ask_for_sample(avctx, "unknown version %i\n", version);
345 return AVERROR_PATCHWELCOME;
351 static int mv_read_packet(AVFormatContext *avctx, AVPacket *pkt)
353 MvContext *mv = avctx->priv_data;
354 AVIOContext *pb = avctx->pb;
355 AVStream *st = avctx->streams[mv->stream_index];
356 const AVIndexEntry *index;
357 int frame = mv->frame[mv->stream_index];
361 if (frame < st->nb_frames) {
362 index = &st->index_entries[frame];
364 if (index->pos > pos)
365 avio_skip(pb, index->pos - pos);
366 else if (index->pos < pos) {
369 ret = avio_seek(pb, index->pos, SEEK_SET);
373 ret = av_get_packet(pb, pkt, index->size);
377 pkt->stream_index = mv->stream_index;
378 pkt->pts = index->timestamp;
379 pkt->flags |= AV_PKT_FLAG_KEY;
381 mv->frame[mv->stream_index]++;
385 if (mv->eof_count >= avctx->nb_streams)
390 if (mv->stream_index >= avctx->nb_streams)
391 mv->stream_index = 0;
396 static int mv_read_seek(AVFormatContext *avctx, int stream_index, int64_t timestamp, int flags)
398 MvContext *mv = avctx->priv_data;
399 AVStream *st = avctx->streams[stream_index];
402 if ((flags & AVSEEK_FLAG_FRAME) || (flags & AVSEEK_FLAG_BYTE))
403 return AVERROR(ENOSYS);
405 if (!avctx->pb->seekable)
408 frame = av_index_search_timestamp(st, timestamp, flags);
412 for (i = 0; i < avctx->nb_streams; i++)
413 mv->frame[i] = frame;
417 AVInputFormat ff_mv_demuxer = {
419 .long_name = NULL_IF_CONFIG_SMALL("Silicon Graphics Movie"),
420 .priv_data_size = sizeof(MvContext),
421 .read_probe = mv_probe,
422 .read_header = mv_read_header,
423 .read_packet = mv_read_packet,
424 .read_seek = mv_read_seek,