FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
mvdec.c
Go to the documentation of this file.
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 {
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 
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 /**
92  * Parse global variable
93  * @return < 0 if unknown
94  */
95 static int parse_global_var(AVFormatContext *avctx, AVStream *st, const char *name, int size)
96 {
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
107  } else
108  return -1;
109 
110  return 0;
111 }
112 
113 /**
114  * Parse audio variable
115  * @return < 0 if unknown
116  */
117 static int parse_audio_var(AVFormatContext *avctx, AVStream *st, const char *name, int size)
118 {
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);
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;
136  } else
137  return -1;
138  return 0;
139 }
140 
141 /**
142  * Parse video variable
143  * @return < 0 if unknown
144  */
145 static int parse_video_var(AVFormatContext *avctx, AVStream *st, const char *name, int size)
146 {
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")) {
154  } else if (!strcmp(str, "2")) {
157  } else if (!strcmp(str, "3")) {
159  } else if (!strcmp(str, "10")) {
161  } else if (!strcmp(str, "MVC2")) {
163  } else {
164  av_log_ask_for_sample(avctx, "unknown video compression %s\n", str);
165  }
166  av_free(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);
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;
181  }
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
186  } else
187  return -1;
188  return 0;
189 }
190 
191 static void read_table(AVFormatContext *avctx, AVStream *st, int (*parse)(AVFormatContext *avctx, AVStream *st, const char *name, int size))
192 {
193  int count, i;
194  AVIOContext *pb = avctx->pb;
195  avio_skip(pb, 4);
196  count = avio_rb32(pb);
197  avio_skip(pb, 4);
198  for (i = 0; i < count; i++) {
199  char name[17];
200  int size;
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);
206  avio_skip(pb, size);
207  }
208  }
209 }
210 
211 static void read_index(AVIOContext *pb, AVStream *st)
212 {
213  uint64_t timestamp = 0;
214  int i;
215  for (i = 0; i < st->nb_frames; i++) {
216  uint32_t pos = avio_rb32(pb);
217  uint32_t size = avio_rb32(pb);
218  avio_skip(pb, 8);
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);
222  } else {
223  timestamp++;
224  }
225  }
226 }
227 
228 static int mv_read_header(AVFormatContext *avctx)
229 {
230  MvContext *mv = avctx->priv_data;
231  AVIOContext *pb = avctx->pb;
232  AVStream *ast = NULL, *vst = NULL; //initialization to suppress warning
233  int version, i;
234 
235  avio_skip(pb, 4);
236 
237  version = avio_rb16(pb);
238  if (version == 2) {
239  uint64_t timestamp;
240  int v;
241  avio_skip(pb, 22);
242 
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);
246  if (!ast)
247  return AVERROR(ENOMEM);
248 
249  vst = avformat_new_stream(avctx, NULL);
250  if (!vst)
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);
255  v = avio_rb32(pb);
256  switch (v) {
257  case 1:
258  vst->codec->codec_id = AV_CODEC_ID_MVC1;
259  break;
260  case 2:
261  vst->codec->pix_fmt = AV_PIX_FMT_ARGB;
262  vst->codec->codec_id = AV_CODEC_ID_RAWVIDEO;
263  break;
264  default:
265  av_log_ask_for_sample(avctx, "unknown video compression %i\n", v);
266  break;
267  }
268  vst->codec->codec_tag = 0;
269  vst->codec->width = avio_rb32(pb);
270  vst->codec->height = avio_rb32(pb);
271  avio_skip(pb, 12);
272 
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);
279  v = avio_rb32(pb);
280  if (v == AUDIO_FORMAT_SIGNED) {
282  } else {
283  av_log_ask_for_sample(avctx, "unknown audio compression (format %i)\n", v);
284  }
285 
286  avio_skip(pb, 12);
287  var_read_metadata(avctx, "title", 0x80);
288  var_read_metadata(avctx, "comment", 0x100);
289  avio_skip(pb, 0x80);
290 
291  timestamp = 0;
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);
296  avio_skip(pb, 8);
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);
300  }
301  } else if (!version && avio_rb16(pb) == 3) {
302  avio_skip(pb, 4);
303 
305 
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);
311  if (!ast)
312  return AVERROR(ENOMEM);
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) {
318  } else {
319  av_log_ask_for_sample(avctx, "unknown audio compression %i (format %i, width %i)\n",
322  }
323  ast->codec->codec_tag = 0;
324  }
325 
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);
331  if (!vst)
332  return AVERROR(ENOMEM);
333  vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
334  read_table(avctx, vst, parse_video_var);
335  }
336 
337  if (mv->nb_audio_tracks)
338  read_index(pb, ast);
339 
340  if (mv->nb_video_tracks)
341  read_index(pb, vst);
342  } else {
343  av_log_ask_for_sample(avctx, "unknown version %i\n", version);
344  return AVERROR_PATCHWELCOME;
345  }
346 
347  return 0;
348 }
349 
351 {
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];
357  int ret;
358  uint64_t pos;
359 
360  if (frame < st->nb_frames) {
361  index = &st->index_entries[frame];
362  pos = avio_tell(pb);
363  if (index->pos > pos)
364  avio_skip(pb, index->pos - pos);
365  else if (index->pos < pos) {
366  if (!pb->seekable)
367  return AVERROR(EIO);
368  ret = avio_seek(pb, index->pos, SEEK_SET);
369  if (ret < 0)
370  return ret;
371  }
372  ret = av_get_packet(pb, pkt, index->size);
373  if (ret < 0)
374  return ret;
375 
376  pkt->stream_index = mv->stream_index;
377  pkt->pts = index->timestamp;
378  pkt->flags |= AV_PKT_FLAG_KEY;
379 
380  mv->frame[mv->stream_index]++;
381  mv->eof_count = 0;
382  } else {
383  mv->eof_count++;
384  if (mv->eof_count >= avctx->nb_streams)
385  return AVERROR_EOF;
386  }
387 
388  mv->stream_index++;
389  if (mv->stream_index >= avctx->nb_streams)
390  mv->stream_index = 0;
391 
392  return 0;
393 }
394 
395 static int mv_read_seek(AVFormatContext *avctx, int stream_index, int64_t timestamp, int flags)
396 {
397  MvContext *mv = avctx->priv_data;
398  AVStream *st = avctx->streams[stream_index];
399  int frame, i;
400 
401  if ((flags & AVSEEK_FLAG_FRAME) || (flags & AVSEEK_FLAG_BYTE))
402  return AVERROR(ENOSYS);
403 
404  if (!avctx->pb->seekable)
405  return AVERROR(EIO);
406 
407  frame = av_index_search_timestamp(st, timestamp, flags);
408  if (frame < 0)
409  return -1;
410 
411  for (i = 0; i < avctx->nb_streams; i++)
412  mv->frame[i] = frame;
413  return 0;
414 }
415 
417  .name = "mv",
418  .long_name = NULL_IF_CONFIG_SMALL("Silicon Graphics Movie"),
419  .priv_data_size = sizeof(MvContext),
420  .read_probe = mv_probe,
424 };