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);
91 static int set_channels(AVFormatContext *avctx, AVStream *st, int channels) {
93 av_log(avctx, AV_LOG_ERROR, "Channel count %d invalid\n", channels);
94 return AVERROR_INVALIDDATA;
96 st->codec->channels = channels;
97 st->codec->channel_layout = (st->codec->channels == 1) ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
102 * Parse global variable
103 * @return < 0 if unknown
105 static int parse_global_var(AVFormatContext *avctx, AVStream *st, const char *name, int size)
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
124 * Parse audio variable
125 * @return < 0 if unknown
127 static int parse_audio_var(AVFormatContext *avctx, AVStream *st, const char *name, int size)
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;
151 * Parse video variable
152 * @return < 0 if unknown
154 static int parse_video_var(AVFormatContext *avctx, AVStream *st, const char *name, int size)
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;
173 av_log_ask_for_sample(avctx, "unknown video compression %s\n", 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;
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
201 static void read_table(AVFormatContext *avctx, AVStream *st, int (*parse)(AVFormatContext *avctx, AVStream *st, const char *name, int size))
204 AVIOContext *pb = avctx->pb;
206 count = avio_rb32(pb);
208 for (i = 0; i < count; i++) {
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);
221 static void read_index(AVIOContext *pb, AVStream *st)
223 uint64_t timestamp = 0;
225 for (i = 0; i < st->nb_frames; i++) {
226 uint32_t pos = avio_rb32(pb);
227 uint32_t size = avio_rb32(pb);
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);
238 static int mv_read_header(AVFormatContext *avctx)
240 MvContext *mv = avctx->priv_data;
241 AVIOContext *pb = avctx->pb;
242 AVStream *ast = NULL, *vst = NULL; //initialization to suppress warning
247 version = avio_rb16(pb);
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);
257 return AVERROR(ENOMEM);
259 vst = avformat_new_stream(avctx, NULL);
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);
268 vst->codec->codec_id = AV_CODEC_ID_MVC1;
271 vst->codec->pix_fmt = AV_PIX_FMT_ARGB;
272 vst->codec->codec_id = AV_CODEC_ID_RAWVIDEO;
275 av_log_ask_for_sample(avctx, "unknown video compression %i\n", v);
278 vst->codec->codec_tag = 0;
279 vst->codec->width = avio_rb32(pb);
280 vst->codec->height = avio_rb32(pb);
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;
291 if (v == AUDIO_FORMAT_SIGNED) {
292 ast->codec->codec_id = AV_CODEC_ID_PCM_S16BE;
294 av_log_ask_for_sample(avctx, "unknown audio compression (format %i)\n", v);
298 var_read_metadata(avctx, "title", 0x80);
299 var_read_metadata(avctx, "comment", 0x100);
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);
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);
312 } else if (!version && avio_rb16(pb) == 3) {
315 read_table(avctx, NULL, parse_global_var);
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);
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;
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;
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;
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);
347 return AVERROR(ENOMEM);
348 vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
349 read_table(avctx, vst, parse_video_var);
352 if (mv->nb_audio_tracks)
355 if (mv->nb_video_tracks)
358 av_log_ask_for_sample(avctx, "unknown version %i\n", version);
359 return AVERROR_PATCHWELCOME;
365 static int mv_read_packet(AVFormatContext *avctx, AVPacket *pkt)
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];
375 if (frame < st->nb_frames) {
376 index = &st->index_entries[frame];
378 if (index->pos > pos)
379 avio_skip(pb, index->pos - pos);
380 else if (index->pos < pos) {
383 ret = avio_seek(pb, index->pos, SEEK_SET);
387 ret = av_get_packet(pb, pkt, index->size);
391 pkt->stream_index = mv->stream_index;
392 pkt->pts = index->timestamp;
393 pkt->flags |= AV_PKT_FLAG_KEY;
395 mv->frame[mv->stream_index]++;
399 if (mv->eof_count >= avctx->nb_streams)
404 if (mv->stream_index >= avctx->nb_streams)
405 mv->stream_index = 0;
410 static int mv_read_seek(AVFormatContext *avctx, int stream_index, int64_t timestamp, int flags)
412 MvContext *mv = avctx->priv_data;
413 AVStream *st = avctx->streams[stream_index];
416 if ((flags & AVSEEK_FLAG_FRAME) || (flags & AVSEEK_FLAG_BYTE))
417 return AVERROR(ENOSYS);
419 if (!avctx->pb->seekable)
422 frame = av_index_search_timestamp(st, timestamp, flags);
426 for (i = 0; i < avctx->nb_streams; i++)
427 mv->frame[i] = frame;
431 AVInputFormat ff_mv_demuxer = {
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,