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.

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