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.

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