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.

425 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. st->time_base = av_inv_q(var_read_float(pb, size));
  155. } else if (!strcmp(name, "HEIGHT")) {
  156. st->codec->height = var_read_int(pb, size);
  157. } else if (!strcmp(name, "PIXEL_ASPECT")) {
  158. st->sample_aspect_ratio = var_read_float(pb, size);
  159. av_reduce(&st->sample_aspect_ratio.num, &st->sample_aspect_ratio.den,
  160. st->sample_aspect_ratio.num, st->sample_aspect_ratio.den, INT_MAX);
  161. } else if (!strcmp(name, "WIDTH")) {
  162. st->codec->width = var_read_int(pb, size);
  163. } else if (!strcmp(name, "ORIENTATION")) {
  164. if (var_read_int(pb, size) == 1101) {
  165. st->codec->extradata = av_strdup("BottomUp");
  166. st->codec->extradata_size = 9;
  167. }
  168. } else if (!strcmp(name, "Q_SPATIAL") || !strcmp(name, "Q_TEMPORAL")) {
  169. var_read_metadata(avctx, name, size);
  170. } else if (!strcmp(name, "INTERLACING") || !strcmp(name, "PACKING")) {
  171. avio_skip(pb, size); // ignore
  172. } else
  173. return -1;
  174. return 0;
  175. }
  176. static void read_table(AVFormatContext *avctx, AVStream *st, int (*parse)(AVFormatContext *avctx, AVStream *st, const char *name, int size))
  177. {
  178. int count, i;
  179. AVIOContext *pb = avctx->pb;
  180. avio_skip(pb, 4);
  181. count = avio_rb32(pb);
  182. avio_skip(pb, 4);
  183. for (i = 0; i < count; i++) {
  184. char name[17];
  185. int size;
  186. avio_read(pb, name, 16);
  187. name[sizeof(name) - 1] = 0;
  188. size = avio_rb32(pb);
  189. if (parse(avctx, st, name, size) < 0) {
  190. av_log_ask_for_sample(avctx, "unknown variable %s\n", name);
  191. avio_skip(pb, size);
  192. }
  193. }
  194. }
  195. static void read_index(AVIOContext *pb, AVStream *st)
  196. {
  197. uint64_t timestamp = 0;
  198. int i;
  199. for (i = 0; i < st->nb_frames; i++) {
  200. uint32_t pos = avio_rb32(pb);
  201. uint32_t size = avio_rb32(pb);
  202. avio_skip(pb, 8);
  203. av_add_index_entry(st, pos, timestamp, size, 0, AVINDEX_KEYFRAME);
  204. if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
  205. timestamp += size / (st->codec->channels * 2);
  206. } else {
  207. timestamp++;
  208. }
  209. }
  210. }
  211. static int mv_read_header(AVFormatContext *avctx)
  212. {
  213. MvContext *mv = avctx->priv_data;
  214. AVIOContext *pb = avctx->pb;
  215. AVStream *ast = NULL, *vst = NULL; //initialization to suppress warning
  216. int version, i;
  217. avio_skip(pb, 4);
  218. version = avio_rb16(pb);
  219. if (version == 2) {
  220. uint64_t timestamp;
  221. int v;
  222. avio_skip(pb, 22);
  223. /* allocate audio track first to prevent unnecessary seeking
  224. (audio packet always precede video packet for a given frame) */
  225. ast = avformat_new_stream(avctx, NULL);
  226. if (!ast)
  227. return AVERROR(ENOMEM);
  228. vst = avformat_new_stream(avctx, NULL);
  229. if (!vst)
  230. return AVERROR(ENOMEM);
  231. vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  232. vst->time_base = (AVRational){1, 15};
  233. vst->nb_frames = avio_rb32(pb);
  234. v = avio_rb32(pb);
  235. switch (v) {
  236. case 1:
  237. vst->codec->codec_id = AV_CODEC_ID_MVC1;
  238. break;
  239. case 2:
  240. vst->codec->pix_fmt = AV_PIX_FMT_ARGB;
  241. vst->codec->codec_id = AV_CODEC_ID_RAWVIDEO;
  242. break;
  243. default:
  244. av_log_ask_for_sample(avctx, "unknown video compression %i\n", v);
  245. break;
  246. }
  247. vst->codec->codec_tag = 0;
  248. vst->codec->width = avio_rb32(pb);
  249. vst->codec->height = avio_rb32(pb);
  250. avio_skip(pb, 12);
  251. ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  252. ast->nb_frames = vst->nb_frames;
  253. ast->codec->sample_rate = avio_rb32(pb);
  254. avpriv_set_pts_info(ast, 33, 1, ast->codec->sample_rate);
  255. ast->codec->channels = avio_rb32(pb);
  256. ast->codec->channel_layout = (ast->codec->channels == 1) ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
  257. v = avio_rb32(pb);
  258. if (v == AUDIO_FORMAT_SIGNED) {
  259. ast->codec->codec_id = AV_CODEC_ID_PCM_S16BE;
  260. } else {
  261. av_log_ask_for_sample(avctx, "unknown audio compression (format %i)\n", v);
  262. }
  263. avio_skip(pb, 12);
  264. var_read_metadata(avctx, "title", 0x80);
  265. var_read_metadata(avctx, "comment", 0x100);
  266. avio_skip(pb, 0x80);
  267. timestamp = 0;
  268. for (i = 0; i < vst->nb_frames; i++) {
  269. uint32_t pos = avio_rb32(pb);
  270. uint32_t asize = avio_rb32(pb);
  271. uint32_t vsize = avio_rb32(pb);
  272. avio_skip(pb, 8);
  273. av_add_index_entry(ast, pos, timestamp, asize, 0, AVINDEX_KEYFRAME);
  274. av_add_index_entry(vst, pos + asize, i, vsize, 0, AVINDEX_KEYFRAME);
  275. timestamp += asize / (ast->codec->channels * 2);
  276. }
  277. } else if (!version && avio_rb16(pb) == 3) {
  278. avio_skip(pb, 4);
  279. read_table(avctx, NULL, parse_global_var);
  280. if (mv->nb_audio_tracks > 1) {
  281. av_log_ask_for_sample(avctx, "multiple audio streams\n");
  282. return AVERROR_PATCHWELCOME;
  283. } else if (mv->nb_audio_tracks) {
  284. ast = avformat_new_stream(avctx, NULL);
  285. if (!ast)
  286. return AVERROR(ENOMEM);
  287. ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  288. /* temporarily store compression value in codec_tag; format value in codec_id */
  289. read_table(avctx, ast, parse_audio_var);
  290. if (ast->codec->codec_tag == 100 && ast->codec->codec_id == AUDIO_FORMAT_SIGNED && ast->codec->bits_per_coded_sample == 16) {
  291. ast->codec->codec_id = AV_CODEC_ID_PCM_S16BE;
  292. } else {
  293. av_log_ask_for_sample(avctx, "unknown audio compression %i (format %i, width %i)\n",
  294. ast->codec->codec_tag, ast->codec->codec_id, ast->codec->bits_per_coded_sample);
  295. ast->codec->codec_id = AV_CODEC_ID_NONE;
  296. }
  297. ast->codec->codec_tag = 0;
  298. }
  299. if (mv->nb_video_tracks > 1) {
  300. av_log_ask_for_sample(avctx, "multiple video streams\n");
  301. return AVERROR_PATCHWELCOME;
  302. } else if (mv->nb_video_tracks) {
  303. vst = avformat_new_stream(avctx, NULL);
  304. if (!vst)
  305. return AVERROR(ENOMEM);
  306. vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  307. read_table(avctx, vst, parse_video_var);
  308. }
  309. if (mv->nb_audio_tracks)
  310. read_index(pb, ast);
  311. if (mv->nb_video_tracks)
  312. read_index(pb, vst);
  313. } else {
  314. av_log_ask_for_sample(avctx, "unknown version %i\n", version);
  315. return AVERROR_PATCHWELCOME;
  316. }
  317. return 0;
  318. }
  319. static int mv_read_packet(AVFormatContext *avctx, AVPacket *pkt)
  320. {
  321. MvContext *mv = avctx->priv_data;
  322. AVIOContext *pb = avctx->pb;
  323. AVStream *st = avctx->streams[mv->stream_index];
  324. const AVIndexEntry *index;
  325. int frame = mv->frame[mv->stream_index];
  326. int ret;
  327. uint64_t pos;
  328. if (frame < st->nb_frames) {
  329. index = &st->index_entries[frame];
  330. pos = avio_tell(pb);
  331. if (index->pos > pos)
  332. avio_skip(pb, index->pos - pos);
  333. else if (index->pos < pos) {
  334. if (!pb->seekable)
  335. return AVERROR(EIO);
  336. ret = avio_seek(pb, index->pos, SEEK_SET);
  337. if (ret < 0)
  338. return ret;
  339. }
  340. ret = av_get_packet(pb, pkt, index->size);
  341. if (ret < 0)
  342. return ret;
  343. pkt->stream_index = mv->stream_index;
  344. pkt->pts = index->timestamp;
  345. pkt->flags |= AV_PKT_FLAG_KEY;
  346. mv->frame[mv->stream_index]++;
  347. mv->eof_count = 0;
  348. } else {
  349. mv->eof_count++;
  350. if (mv->eof_count >= avctx->nb_streams)
  351. return AVERROR_EOF;
  352. }
  353. mv->stream_index++;
  354. if (mv->stream_index >= avctx->nb_streams)
  355. mv->stream_index = 0;
  356. return 0;
  357. }
  358. static int mv_read_seek(AVFormatContext *avctx, int stream_index, int64_t timestamp, int flags)
  359. {
  360. MvContext *mv = avctx->priv_data;
  361. AVStream *st = avctx->streams[stream_index];
  362. int frame, i;
  363. if ((flags & AVSEEK_FLAG_FRAME) || (flags & AVSEEK_FLAG_BYTE))
  364. return AVERROR(ENOSYS);
  365. if (!avctx->pb->seekable)
  366. return AVERROR(EIO);
  367. frame = av_index_search_timestamp(st, timestamp, flags);
  368. if (frame < 0)
  369. return -1;
  370. for (i = 0; i < avctx->nb_streams; i++)
  371. mv->frame[i] = frame;
  372. return 0;
  373. }
  374. AVInputFormat ff_mv_demuxer = {
  375. .name = "mv",
  376. .long_name = NULL_IF_CONFIG_SMALL("Silicon Graphics Movie"),
  377. .priv_data_size = sizeof(MvContext),
  378. .read_probe = mv_probe,
  379. .read_header = mv_read_header,
  380. .read_packet = mv_read_packet,
  381. .read_seek = mv_read_seek,
  382. };