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.

501 lines
16KB

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