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 st->time_base = av_inv_q(var_read_float(pb, size));
169 } else if (!strcmp(name, "HEIGHT")) {
170 st->codec->height = var_read_int(pb, size);
171 } else if (!strcmp(name, "PIXEL_ASPECT")) {
172 st->sample_aspect_ratio = var_read_float(pb, size);
173 av_reduce(&st->sample_aspect_ratio.num, &st->sample_aspect_ratio.den,
174 st->sample_aspect_ratio.num, st->sample_aspect_ratio.den, INT_MAX);
175 } else if (!strcmp(name, "WIDTH")) {
176 st->codec->width = var_read_int(pb, size);
177 } else if (!strcmp(name, "ORIENTATION")) {
178 if (var_read_int(pb, size) == 1101) {
179 st->codec->extradata = av_strdup("BottomUp");
180 st->codec->extradata_size = 9;
182 } else if (!strcmp(name, "Q_SPATIAL") || !strcmp(name, "Q_TEMPORAL")) {
183 var_read_metadata(avctx, name, size);
184 } else if (!strcmp(name, "INTERLACING") || !strcmp(name, "PACKING")) {
185 avio_skip(pb, size); // ignore
191 static void read_table(AVFormatContext *avctx, AVStream *st, int (*parse)(AVFormatContext *avctx, AVStream *st, const char *name, int size))
194 AVIOContext *pb = avctx->pb;
196 count = avio_rb32(pb);
198 for (i = 0; i < count; i++) {
201 avio_read(pb, name, 16);
202 name[sizeof(name) - 1] = 0;
203 size = avio_rb32(pb);
204 if (parse(avctx, st, name, size) < 0) {
205 av_log_ask_for_sample(avctx, "unknown variable %s\n", name);
211 static void read_index(AVIOContext *pb, AVStream *st)
213 uint64_t timestamp = 0;
215 for (i = 0; i < st->nb_frames; i++) {
216 uint32_t pos = avio_rb32(pb);
217 uint32_t size = avio_rb32(pb);
219 av_add_index_entry(st, pos, timestamp, size, 0, AVINDEX_KEYFRAME);
220 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
221 timestamp += size / (st->codec->channels * 2);
228 static int mv_read_header(AVFormatContext *avctx)
230 MvContext *mv = avctx->priv_data;
231 AVIOContext *pb = avctx->pb;
232 AVStream *ast = NULL, *vst = NULL; //initialization to suppress warning
237 version = avio_rb16(pb);
243 /* allocate audio track first to prevent unnecessary seeking
244 (audio packet always precede video packet for a given frame) */
245 ast = avformat_new_stream(avctx, NULL);
247 return AVERROR(ENOMEM);
249 vst = avformat_new_stream(avctx, NULL);
251 return AVERROR(ENOMEM);
252 vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
253 vst->time_base = (AVRational){1, 15};
254 vst->nb_frames = avio_rb32(pb);
258 vst->codec->codec_id = AV_CODEC_ID_MVC1;
261 vst->codec->pix_fmt = AV_PIX_FMT_ARGB;
262 vst->codec->codec_id = AV_CODEC_ID_RAWVIDEO;
265 av_log_ask_for_sample(avctx, "unknown video compression %i\n", v);
268 vst->codec->codec_tag = 0;
269 vst->codec->width = avio_rb32(pb);
270 vst->codec->height = avio_rb32(pb);
273 ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
274 ast->nb_frames = vst->nb_frames;
275 ast->codec->sample_rate = avio_rb32(pb);
276 avpriv_set_pts_info(ast, 33, 1, ast->codec->sample_rate);
277 ast->codec->channels = avio_rb32(pb);
278 ast->codec->channel_layout = (ast->codec->channels == 1) ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
280 if (v == AUDIO_FORMAT_SIGNED) {
281 ast->codec->codec_id = AV_CODEC_ID_PCM_S16BE;
283 av_log_ask_for_sample(avctx, "unknown audio compression (format %i)\n", v);
287 var_read_metadata(avctx, "title", 0x80);
288 var_read_metadata(avctx, "comment", 0x100);
292 for (i = 0; i < vst->nb_frames; i++) {
293 uint32_t pos = avio_rb32(pb);
294 uint32_t asize = avio_rb32(pb);
295 uint32_t vsize = avio_rb32(pb);
297 av_add_index_entry(ast, pos, timestamp, asize, 0, AVINDEX_KEYFRAME);
298 av_add_index_entry(vst, pos + asize, i, vsize, 0, AVINDEX_KEYFRAME);
299 timestamp += asize / (ast->codec->channels * 2);
301 } else if (!version && avio_rb16(pb) == 3) {
304 read_table(avctx, NULL, parse_global_var);
306 if (mv->nb_audio_tracks > 1) {
307 av_log_ask_for_sample(avctx, "multiple audio streams\n");
308 return AVERROR_PATCHWELCOME;
309 } else if (mv->nb_audio_tracks) {
310 ast = avformat_new_stream(avctx, NULL);
312 return AVERROR(ENOMEM);
313 ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
314 /* temporarily store compression value in codec_tag; format value in codec_id */
315 read_table(avctx, ast, parse_audio_var);
316 if (ast->codec->codec_tag == 100 && ast->codec->codec_id == AUDIO_FORMAT_SIGNED && ast->codec->bits_per_coded_sample == 16) {
317 ast->codec->codec_id = AV_CODEC_ID_PCM_S16BE;
319 av_log_ask_for_sample(avctx, "unknown audio compression %i (format %i, width %i)\n",
320 ast->codec->codec_tag, ast->codec->codec_id, ast->codec->bits_per_coded_sample);
321 ast->codec->codec_id = AV_CODEC_ID_NONE;
323 ast->codec->codec_tag = 0;
326 if (mv->nb_video_tracks > 1) {
327 av_log_ask_for_sample(avctx, "multiple video streams\n");
328 return AVERROR_PATCHWELCOME;
329 } else if (mv->nb_video_tracks) {
330 vst = avformat_new_stream(avctx, NULL);
332 return AVERROR(ENOMEM);
333 vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
334 read_table(avctx, vst, parse_video_var);
337 if (mv->nb_audio_tracks)
340 if (mv->nb_video_tracks)
343 av_log_ask_for_sample(avctx, "unknown version %i\n", version);
344 return AVERROR_PATCHWELCOME;
350 static int mv_read_packet(AVFormatContext *avctx, AVPacket *pkt)
352 MvContext *mv = avctx->priv_data;
353 AVIOContext *pb = avctx->pb;
354 AVStream *st = avctx->streams[mv->stream_index];
355 const AVIndexEntry *index;
356 int frame = mv->frame[mv->stream_index];
360 if (frame < st->nb_frames) {
361 index = &st->index_entries[frame];
363 if (index->pos > pos)
364 avio_skip(pb, index->pos - pos);
365 else if (index->pos < pos) {
368 ret = avio_seek(pb, index->pos, SEEK_SET);
372 ret = av_get_packet(pb, pkt, index->size);
376 pkt->stream_index = mv->stream_index;
377 pkt->pts = index->timestamp;
378 pkt->flags |= AV_PKT_FLAG_KEY;
380 mv->frame[mv->stream_index]++;
384 if (mv->eof_count >= avctx->nb_streams)
389 if (mv->stream_index >= avctx->nb_streams)
390 mv->stream_index = 0;
395 static int mv_read_seek(AVFormatContext *avctx, int stream_index, int64_t timestamp, int flags)
397 MvContext *mv = avctx->priv_data;
398 AVStream *st = avctx->streams[stream_index];
401 if ((flags & AVSEEK_FLAG_FRAME) || (flags & AVSEEK_FLAG_BYTE))
402 return AVERROR(ENOSYS);
404 if (!avctx->pb->seekable)
407 frame = av_index_search_timestamp(st, timestamp, flags);
411 for (i = 0; i < avctx->nb_streams; i++)
412 mv->frame[i] = frame;
416 AVInputFormat ff_mv_demuxer = {
418 .long_name = NULL_IF_CONFIG_SMALL("Silicon Graphics Movie"),
419 .priv_data_size = sizeof(MvContext),
420 .read_probe = mv_probe,
421 .read_header = mv_read_header,
422 .read_packet = mv_read_packet,
423 .read_seek = mv_read_seek,