You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

426 lines
14KB

  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. * @file
  23. * Silicon Graphics Movie demuxer
  24. */
  25. #include "libavutil/eval.h"
  26. #include "libavutil/intreadwrite.h"
  27. #include "libavutil/rational.h"
  28. #include "avformat.h"
  29. #include "internal.h"
  30. typedef struct {
  31. int nb_video_tracks;
  32. int nb_audio_tracks;
  33. int eof_count; /**< number of streams that have finished */
  34. int stream_index; /**< current stream index */
  35. int frame[2]; /**< frame nb for current stream */
  36. } MvContext;
  37. #define AUDIO_FORMAT_SIGNED 401
  38. static int mv_probe(AVProbeData *p)
  39. {
  40. if (AV_RB32(p->buf) == MKBETAG('M','O','V','I') && AV_RB16(p->buf + 4) < 3)
  41. return AVPROBE_SCORE_MAX;
  42. return 0;
  43. }
  44. static char * var_read_string(AVIOContext *pb, int size)
  45. {
  46. char *str = av_malloc(size + 1);
  47. int n;
  48. if (!str)
  49. return NULL;
  50. n = avio_get_str(pb, size, str, size + 1);
  51. if (n < size)
  52. avio_skip(pb, size - n);
  53. return str;
  54. }
  55. static int var_read_int(AVIOContext *pb, int size)
  56. {
  57. int v;
  58. char * s = var_read_string(pb, size);
  59. if (!s || sscanf(s, "%d", &v) != 1)
  60. v = 0;
  61. av_free(s);
  62. return v;
  63. }
  64. static AVRational var_read_float(AVIOContext *pb, int size)
  65. {
  66. AVRational v;
  67. char * s = var_read_string(pb, size);
  68. if (!s)
  69. return (AVRational){0, 0};
  70. v = av_d2q(av_strtod(s, NULL), INT_MAX);
  71. av_free(s);
  72. return v;
  73. }
  74. static void var_read_metadata(AVFormatContext *avctx, const char *tag, int size)
  75. {
  76. char *value = var_read_string(avctx->pb, size);
  77. if (value)
  78. av_dict_set(&avctx->metadata, tag, value, AV_DICT_DONT_STRDUP_VAL);
  79. }
  80. /**
  81. * Parse global variable
  82. * @return < 0 if unknown
  83. */
  84. static int parse_global_var(AVFormatContext *avctx, AVStream *st, const char *name, int size)
  85. {
  86. MvContext *mv = avctx->priv_data;
  87. AVIOContext *pb = avctx->pb;
  88. if (!strcmp(name, "__NUM_I_TRACKS")) {
  89. mv->nb_video_tracks = var_read_int(pb, size);
  90. } else if (!strcmp(name, "__NUM_A_TRACKS")) {
  91. mv->nb_audio_tracks = var_read_int(pb, size);
  92. } else if (!strcmp(name, "COMMENT") || !strcmp(name, "TITLE")) {
  93. var_read_metadata(avctx, name, size);
  94. } else if (!strcmp(name, "LOOP_MODE") || !strcmp(name, "NUM_LOOPS") || !strcmp(name, "OPTIMIZED")) {
  95. avio_skip(pb, size); // ignore
  96. } else
  97. return -1;
  98. return 0;
  99. }
  100. /**
  101. * Parse audio variable
  102. * @return < 0 if unknown
  103. */
  104. static int parse_audio_var(AVFormatContext *avctx, AVStream *st, const char *name, int size)
  105. {
  106. AVIOContext *pb = avctx->pb;
  107. if (!strcmp(name, "__DIR_COUNT")) {
  108. st->nb_frames = var_read_int(pb, size);
  109. } else if (!strcmp(name, "AUDIO_FORMAT")) {
  110. st->codec->codec_id = var_read_int(pb, size);
  111. } else if (!strcmp(name, "COMPRESSION")) {
  112. st->codec->codec_tag = var_read_int(pb, size);
  113. } else if (!strcmp(name, "DEFAULT_VOL")) {
  114. var_read_metadata(avctx, name, size);
  115. } else if (!strcmp(name, "NUM_CHANNELS")) {
  116. st->codec->channels = var_read_int(pb, size);
  117. st->codec->channel_layout = (st->codec->channels == 1) ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
  118. } else if (!strcmp(name, "SAMPLE_RATE")) {
  119. st->codec->sample_rate = var_read_int(pb, size);
  120. avpriv_set_pts_info(st, 33, 1, st->codec->sample_rate);
  121. } else if (!strcmp(name, "SAMPLE_WIDTH")) {
  122. st->codec->bits_per_coded_sample = var_read_int(pb, size) * 8;
  123. } else
  124. return -1;
  125. return 0;
  126. }
  127. /**
  128. * Parse video variable
  129. * @return < 0 if unknown
  130. */
  131. static int parse_video_var(AVFormatContext *avctx, AVStream *st, const char *name, int size)
  132. {
  133. AVIOContext *pb = avctx->pb;
  134. if (!strcmp(name, "__DIR_COUNT")) {
  135. st->nb_frames = st->duration = var_read_int(pb, size);
  136. } else if (!strcmp(name, "COMPRESSION")) {
  137. char * str = var_read_string(pb, size);
  138. if (!strcmp(str, "1")) {
  139. st->codec->codec_id = AV_CODEC_ID_MVC1;
  140. } else if (!strcmp(str, "2")) {
  141. st->codec->pix_fmt = AV_PIX_FMT_ABGR;
  142. st->codec->codec_id = AV_CODEC_ID_RAWVIDEO;
  143. } else if (!strcmp(str, "3")) {
  144. st->codec->codec_id = AV_CODEC_ID_SGIRLE;
  145. } else if (!strcmp(str, "10")) {
  146. st->codec->codec_id = AV_CODEC_ID_MJPEG;
  147. } else if (!strcmp(str, "MVC2")) {
  148. st->codec->codec_id = AV_CODEC_ID_MVC2;
  149. } else {
  150. av_log_ask_for_sample(avctx, "unknown video compression %s\n", str);
  151. }
  152. av_free(str);
  153. } else if (!strcmp(name, "FPS")) {
  154. AVRational tb = av_inv_q(var_read_float(pb, size));
  155. avpriv_set_pts_info(st, 64, tb.num, tb.den);
  156. } else if (!strcmp(name, "HEIGHT")) {
  157. st->codec->height = var_read_int(pb, size);
  158. } else if (!strcmp(name, "PIXEL_ASPECT")) {
  159. st->sample_aspect_ratio = var_read_float(pb, size);
  160. av_reduce(&st->sample_aspect_ratio.num, &st->sample_aspect_ratio.den,
  161. st->sample_aspect_ratio.num, st->sample_aspect_ratio.den, INT_MAX);
  162. } else if (!strcmp(name, "WIDTH")) {
  163. st->codec->width = var_read_int(pb, size);
  164. } else if (!strcmp(name, "ORIENTATION")) {
  165. if (var_read_int(pb, size) == 1101) {
  166. st->codec->extradata = av_strdup("BottomUp");
  167. st->codec->extradata_size = 9;
  168. }
  169. } else if (!strcmp(name, "Q_SPATIAL") || !strcmp(name, "Q_TEMPORAL")) {
  170. var_read_metadata(avctx, name, size);
  171. } else if (!strcmp(name, "INTERLACING") || !strcmp(name, "PACKING")) {
  172. avio_skip(pb, size); // ignore
  173. } else
  174. return -1;
  175. return 0;
  176. }
  177. static void read_table(AVFormatContext *avctx, AVStream *st, int (*parse)(AVFormatContext *avctx, AVStream *st, const char *name, int size))
  178. {
  179. int count, i;
  180. AVIOContext *pb = avctx->pb;
  181. avio_skip(pb, 4);
  182. count = avio_rb32(pb);
  183. avio_skip(pb, 4);
  184. for (i = 0; i < count; i++) {
  185. char name[17];
  186. int size;
  187. avio_read(pb, name, 16);
  188. name[sizeof(name) - 1] = 0;
  189. size = avio_rb32(pb);
  190. if (parse(avctx, st, name, size) < 0) {
  191. av_log_ask_for_sample(avctx, "unknown variable %s\n", name);
  192. avio_skip(pb, size);
  193. }
  194. }
  195. }
  196. static void read_index(AVIOContext *pb, AVStream *st)
  197. {
  198. uint64_t timestamp = 0;
  199. int i;
  200. for (i = 0; i < st->nb_frames; i++) {
  201. uint32_t pos = avio_rb32(pb);
  202. uint32_t size = avio_rb32(pb);
  203. avio_skip(pb, 8);
  204. av_add_index_entry(st, pos, timestamp, size, 0, AVINDEX_KEYFRAME);
  205. if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
  206. timestamp += size / (st->codec->channels * 2);
  207. } else {
  208. timestamp++;
  209. }
  210. }
  211. }
  212. static int mv_read_header(AVFormatContext *avctx)
  213. {
  214. MvContext *mv = avctx->priv_data;
  215. AVIOContext *pb = avctx->pb;
  216. AVStream *ast = NULL, *vst = NULL; //initialization to suppress warning
  217. int version, i;
  218. avio_skip(pb, 4);
  219. version = avio_rb16(pb);
  220. if (version == 2) {
  221. uint64_t timestamp;
  222. int v;
  223. avio_skip(pb, 22);
  224. /* allocate audio track first to prevent unnecessary seeking
  225. (audio packet always precede video packet for a given frame) */
  226. ast = avformat_new_stream(avctx, NULL);
  227. if (!ast)
  228. return AVERROR(ENOMEM);
  229. vst = avformat_new_stream(avctx, NULL);
  230. if (!vst)
  231. return AVERROR(ENOMEM);
  232. vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  233. avpriv_set_pts_info(vst, 64, 1, 15);
  234. vst->nb_frames = avio_rb32(pb);
  235. v = avio_rb32(pb);
  236. switch (v) {
  237. case 1:
  238. vst->codec->codec_id = AV_CODEC_ID_MVC1;
  239. break;
  240. case 2:
  241. vst->codec->pix_fmt = AV_PIX_FMT_ARGB;
  242. vst->codec->codec_id = AV_CODEC_ID_RAWVIDEO;
  243. break;
  244. default:
  245. av_log_ask_for_sample(avctx, "unknown video compression %i\n", v);
  246. break;
  247. }
  248. vst->codec->codec_tag = 0;
  249. vst->codec->width = avio_rb32(pb);
  250. vst->codec->height = avio_rb32(pb);
  251. avio_skip(pb, 12);
  252. ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  253. ast->nb_frames = vst->nb_frames;
  254. ast->codec->sample_rate = avio_rb32(pb);
  255. avpriv_set_pts_info(ast, 33, 1, ast->codec->sample_rate);
  256. ast->codec->channels = avio_rb32(pb);
  257. ast->codec->channel_layout = (ast->codec->channels == 1) ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
  258. v = avio_rb32(pb);
  259. if (v == AUDIO_FORMAT_SIGNED) {
  260. ast->codec->codec_id = AV_CODEC_ID_PCM_S16BE;
  261. } else {
  262. av_log_ask_for_sample(avctx, "unknown audio compression (format %i)\n", v);
  263. }
  264. avio_skip(pb, 12);
  265. var_read_metadata(avctx, "title", 0x80);
  266. var_read_metadata(avctx, "comment", 0x100);
  267. avio_skip(pb, 0x80);
  268. timestamp = 0;
  269. for (i = 0; i < vst->nb_frames; i++) {
  270. uint32_t pos = avio_rb32(pb);
  271. uint32_t asize = avio_rb32(pb);
  272. uint32_t vsize = avio_rb32(pb);
  273. avio_skip(pb, 8);
  274. av_add_index_entry(ast, pos, timestamp, asize, 0, AVINDEX_KEYFRAME);
  275. av_add_index_entry(vst, pos + asize, i, vsize, 0, AVINDEX_KEYFRAME);
  276. timestamp += asize / (ast->codec->channels * 2);
  277. }
  278. } else if (!version && avio_rb16(pb) == 3) {
  279. avio_skip(pb, 4);
  280. read_table(avctx, NULL, parse_global_var);
  281. if (mv->nb_audio_tracks > 1) {
  282. av_log_ask_for_sample(avctx, "multiple audio streams\n");
  283. return AVERROR_PATCHWELCOME;
  284. } else if (mv->nb_audio_tracks) {
  285. ast = avformat_new_stream(avctx, NULL);
  286. if (!ast)
  287. return AVERROR(ENOMEM);
  288. ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  289. /* temporarily store compression value in codec_tag; format value in codec_id */
  290. read_table(avctx, ast, parse_audio_var);
  291. if (ast->codec->codec_tag == 100 && ast->codec->codec_id == AUDIO_FORMAT_SIGNED && ast->codec->bits_per_coded_sample == 16) {
  292. ast->codec->codec_id = AV_CODEC_ID_PCM_S16BE;
  293. } else {
  294. av_log_ask_for_sample(avctx, "unknown audio compression %i (format %i, width %i)\n",
  295. ast->codec->codec_tag, ast->codec->codec_id, ast->codec->bits_per_coded_sample);
  296. ast->codec->codec_id = AV_CODEC_ID_NONE;
  297. }
  298. ast->codec->codec_tag = 0;
  299. }
  300. if (mv->nb_video_tracks > 1) {
  301. av_log_ask_for_sample(avctx, "multiple video streams\n");
  302. return AVERROR_PATCHWELCOME;
  303. } else if (mv->nb_video_tracks) {
  304. vst = avformat_new_stream(avctx, NULL);
  305. if (!vst)
  306. return AVERROR(ENOMEM);
  307. vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  308. read_table(avctx, vst, parse_video_var);
  309. }
  310. if (mv->nb_audio_tracks)
  311. read_index(pb, ast);
  312. if (mv->nb_video_tracks)
  313. read_index(pb, vst);
  314. } else {
  315. av_log_ask_for_sample(avctx, "unknown version %i\n", version);
  316. return AVERROR_PATCHWELCOME;
  317. }
  318. return 0;
  319. }
  320. static int mv_read_packet(AVFormatContext *avctx, AVPacket *pkt)
  321. {
  322. MvContext *mv = avctx->priv_data;
  323. AVIOContext *pb = avctx->pb;
  324. AVStream *st = avctx->streams[mv->stream_index];
  325. const AVIndexEntry *index;
  326. int frame = mv->frame[mv->stream_index];
  327. int ret;
  328. uint64_t pos;
  329. if (frame < st->nb_frames) {
  330. index = &st->index_entries[frame];
  331. pos = avio_tell(pb);
  332. if (index->pos > pos)
  333. avio_skip(pb, index->pos - pos);
  334. else if (index->pos < pos) {
  335. if (!pb->seekable)
  336. return AVERROR(EIO);
  337. ret = avio_seek(pb, index->pos, SEEK_SET);
  338. if (ret < 0)
  339. return ret;
  340. }
  341. ret = av_get_packet(pb, pkt, index->size);
  342. if (ret < 0)
  343. return ret;
  344. pkt->stream_index = mv->stream_index;
  345. pkt->pts = index->timestamp;
  346. pkt->flags |= AV_PKT_FLAG_KEY;
  347. mv->frame[mv->stream_index]++;
  348. mv->eof_count = 0;
  349. } else {
  350. mv->eof_count++;
  351. if (mv->eof_count >= avctx->nb_streams)
  352. return AVERROR_EOF;
  353. }
  354. mv->stream_index++;
  355. if (mv->stream_index >= avctx->nb_streams)
  356. mv->stream_index = 0;
  357. return 0;
  358. }
  359. static int mv_read_seek(AVFormatContext *avctx, int stream_index, int64_t timestamp, int flags)
  360. {
  361. MvContext *mv = avctx->priv_data;
  362. AVStream *st = avctx->streams[stream_index];
  363. int frame, i;
  364. if ((flags & AVSEEK_FLAG_FRAME) || (flags & AVSEEK_FLAG_BYTE))
  365. return AVERROR(ENOSYS);
  366. if (!avctx->pb->seekable)
  367. return AVERROR(EIO);
  368. frame = av_index_search_timestamp(st, timestamp, flags);
  369. if (frame < 0)
  370. return -1;
  371. for (i = 0; i < avctx->nb_streams; i++)
  372. mv->frame[i] = frame;
  373. return 0;
  374. }
  375. AVInputFormat ff_mv_demuxer = {
  376. .name = "mv",
  377. .long_name = NULL_IF_CONFIG_SMALL("Silicon Graphics Movie"),
  378. .priv_data_size = sizeof(MvContext),
  379. .read_probe = mv_probe,
  380. .read_header = mv_read_header,
  381. .read_packet = mv_read_packet,
  382. .read_seek = mv_read_seek,
  383. };