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.

483 lines
15KB

  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->codec->channels = channels;
  95. st->codec->channel_layout = (st->codec->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->codec->sample_rate = var_read_int(pb, size);
  142. avpriv_set_pts_info(st, 33, 1, st->codec->sample_rate);
  143. } else if (!strcmp(name, "SAMPLE_WIDTH")) {
  144. st->codec->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->codec->codec_id = AV_CODEC_ID_MVC1;
  165. } else if (!strcmp(str, "2")) {
  166. st->codec->pix_fmt = AV_PIX_FMT_ABGR;
  167. st->codec->codec_id = AV_CODEC_ID_RAWVIDEO;
  168. } else if (!strcmp(str, "3")) {
  169. st->codec->codec_id = AV_CODEC_ID_SGIRLE;
  170. } else if (!strcmp(str, "10")) {
  171. st->codec->codec_id = AV_CODEC_ID_MJPEG;
  172. } else if (!strcmp(str, "MVC2")) {
  173. st->codec->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->codec->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->codec->width = var_read_int(pb, size);
  191. } else if (!strcmp(name, "ORIENTATION")) {
  192. if (var_read_int(pb, size) == 1101) {
  193. st->codec->extradata = av_strdup("BottomUp");
  194. st->codec->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. int count, i;
  209. AVIOContext *pb = avctx->pb;
  210. avio_skip(pb, 4);
  211. count = avio_rb32(pb);
  212. avio_skip(pb, 4);
  213. for (i = 0; i < count; i++) {
  214. char name[17];
  215. int size;
  216. avio_read(pb, name, 16);
  217. name[sizeof(name) - 1] = 0;
  218. size = avio_rb32(pb);
  219. if (size < 0) {
  220. av_log(avctx, AV_LOG_ERROR, "entry size %d is invalid\n", size);
  221. return AVERROR_INVALIDDATA;
  222. }
  223. if (parse(avctx, st, name, size) < 0) {
  224. avpriv_request_sample(avctx, "Variable %s", name);
  225. avio_skip(pb, size);
  226. }
  227. }
  228. return 0;
  229. }
  230. static void read_index(AVIOContext *pb, AVStream *st)
  231. {
  232. uint64_t timestamp = 0;
  233. int i;
  234. for (i = 0; i < st->nb_frames; i++) {
  235. uint32_t pos = avio_rb32(pb);
  236. uint32_t size = avio_rb32(pb);
  237. avio_skip(pb, 8);
  238. av_add_index_entry(st, pos, timestamp, size, 0, AVINDEX_KEYFRAME);
  239. if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
  240. timestamp += size / (st->codec->channels * 2);
  241. } else {
  242. timestamp++;
  243. }
  244. }
  245. }
  246. static int mv_read_header(AVFormatContext *avctx)
  247. {
  248. MvContext *mv = avctx->priv_data;
  249. AVIOContext *pb = avctx->pb;
  250. AVStream *ast = NULL, *vst = NULL; //initialization to suppress warning
  251. int version, i;
  252. int ret;
  253. avio_skip(pb, 4);
  254. version = avio_rb16(pb);
  255. if (version == 2) {
  256. uint64_t timestamp;
  257. int v;
  258. avio_skip(pb, 22);
  259. /* allocate audio track first to prevent unnecessary seeking
  260. * (audio packet always precede video packet for a given frame) */
  261. ast = avformat_new_stream(avctx, NULL);
  262. if (!ast)
  263. return AVERROR(ENOMEM);
  264. vst = avformat_new_stream(avctx, NULL);
  265. if (!vst)
  266. return AVERROR(ENOMEM);
  267. avpriv_set_pts_info(vst, 64, 1, 15);
  268. vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  269. vst->avg_frame_rate = av_inv_q(vst->time_base);
  270. vst->nb_frames = avio_rb32(pb);
  271. v = avio_rb32(pb);
  272. switch (v) {
  273. case 1:
  274. vst->codec->codec_id = AV_CODEC_ID_MVC1;
  275. break;
  276. case 2:
  277. vst->codec->pix_fmt = AV_PIX_FMT_ARGB;
  278. vst->codec->codec_id = AV_CODEC_ID_RAWVIDEO;
  279. break;
  280. default:
  281. avpriv_request_sample(avctx, "Video compression %i", v);
  282. break;
  283. }
  284. vst->codec->codec_tag = 0;
  285. vst->codec->width = avio_rb32(pb);
  286. vst->codec->height = avio_rb32(pb);
  287. avio_skip(pb, 12);
  288. ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  289. ast->nb_frames = vst->nb_frames;
  290. ast->codec->sample_rate = avio_rb32(pb);
  291. avpriv_set_pts_info(ast, 33, 1, ast->codec->sample_rate);
  292. if (set_channels(avctx, ast, avio_rb32(pb)) < 0)
  293. return AVERROR_INVALIDDATA;
  294. v = avio_rb32(pb);
  295. if (v == AUDIO_FORMAT_SIGNED) {
  296. ast->codec->codec_id = AV_CODEC_ID_PCM_S16BE;
  297. } else {
  298. avpriv_request_sample(avctx, "Audio compression (format %i)", v);
  299. }
  300. avio_skip(pb, 12);
  301. var_read_metadata(avctx, "title", 0x80);
  302. var_read_metadata(avctx, "comment", 0x100);
  303. avio_skip(pb, 0x80);
  304. timestamp = 0;
  305. for (i = 0; i < vst->nb_frames; i++) {
  306. uint32_t pos = avio_rb32(pb);
  307. uint32_t asize = avio_rb32(pb);
  308. uint32_t vsize = avio_rb32(pb);
  309. avio_skip(pb, 8);
  310. av_add_index_entry(ast, pos, timestamp, asize, 0, AVINDEX_KEYFRAME);
  311. av_add_index_entry(vst, pos + asize, i, vsize, 0, AVINDEX_KEYFRAME);
  312. timestamp += asize / (ast->codec->channels * 2);
  313. }
  314. } else if (!version && avio_rb16(pb) == 3) {
  315. avio_skip(pb, 4);
  316. if ((ret = read_table(avctx, NULL, parse_global_var)) < 0)
  317. return ret;
  318. if (mv->nb_audio_tracks > 1) {
  319. avpriv_request_sample(avctx, "Multiple audio streams support");
  320. return AVERROR_PATCHWELCOME;
  321. } else if (mv->nb_audio_tracks) {
  322. ast = avformat_new_stream(avctx, NULL);
  323. if (!ast)
  324. return AVERROR(ENOMEM);
  325. ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  326. if ((read_table(avctx, ast, parse_audio_var)) < 0)
  327. return ret;
  328. if (mv->acompression == 100 &&
  329. mv->aformat == AUDIO_FORMAT_SIGNED &&
  330. ast->codec->bits_per_coded_sample == 16) {
  331. ast->codec->codec_id = AV_CODEC_ID_PCM_S16BE;
  332. } else {
  333. avpriv_request_sample(avctx,
  334. "Audio compression %i (format %i, sr %i)",
  335. mv->acompression, mv->aformat,
  336. ast->codec->bits_per_coded_sample);
  337. ast->codec->codec_id = AV_CODEC_ID_NONE;
  338. }
  339. if (ast->codec->channels <= 0) {
  340. av_log(avctx, AV_LOG_ERROR, "No valid channel count found.\n");
  341. return AVERROR_INVALIDDATA;
  342. }
  343. }
  344. if (mv->nb_video_tracks > 1) {
  345. avpriv_request_sample(avctx, "Multiple video streams support");
  346. return AVERROR_PATCHWELCOME;
  347. } else if (mv->nb_video_tracks) {
  348. vst = avformat_new_stream(avctx, NULL);
  349. if (!vst)
  350. return AVERROR(ENOMEM);
  351. vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  352. if ((ret = read_table(avctx, vst, parse_video_var))<0)
  353. return ret;
  354. }
  355. if (mv->nb_audio_tracks)
  356. read_index(pb, ast);
  357. if (mv->nb_video_tracks)
  358. read_index(pb, vst);
  359. } else {
  360. avpriv_request_sample(avctx, "Version %i", version);
  361. return AVERROR_PATCHWELCOME;
  362. }
  363. return 0;
  364. }
  365. static int mv_read_packet(AVFormatContext *avctx, AVPacket *pkt)
  366. {
  367. MvContext *mv = avctx->priv_data;
  368. AVIOContext *pb = avctx->pb;
  369. AVStream *st = avctx->streams[mv->stream_index];
  370. const AVIndexEntry *index;
  371. int frame = mv->frame[mv->stream_index];
  372. int64_t ret;
  373. uint64_t pos;
  374. if (frame < st->nb_index_entries) {
  375. index = &st->index_entries[frame];
  376. pos = avio_tell(pb);
  377. if (index->pos > pos)
  378. avio_skip(pb, index->pos - pos);
  379. else if (index->pos < pos) {
  380. if (!pb->seekable)
  381. return AVERROR(EIO);
  382. ret = avio_seek(pb, index->pos, SEEK_SET);
  383. if (ret < 0)
  384. return ret;
  385. }
  386. ret = av_get_packet(pb, pkt, index->size);
  387. if (ret < 0)
  388. return ret;
  389. pkt->stream_index = mv->stream_index;
  390. pkt->pts = index->timestamp;
  391. pkt->flags |= AV_PKT_FLAG_KEY;
  392. mv->frame[mv->stream_index]++;
  393. mv->eof_count = 0;
  394. } else {
  395. mv->eof_count++;
  396. if (mv->eof_count >= avctx->nb_streams)
  397. return AVERROR_EOF;
  398. // avoid returning 0 without a packet
  399. return AVERROR(EAGAIN);
  400. }
  401. mv->stream_index++;
  402. if (mv->stream_index >= avctx->nb_streams)
  403. mv->stream_index = 0;
  404. return 0;
  405. }
  406. static int mv_read_seek(AVFormatContext *avctx, int stream_index,
  407. int64_t timestamp, int flags)
  408. {
  409. MvContext *mv = avctx->priv_data;
  410. AVStream *st = avctx->streams[stream_index];
  411. int frame, i;
  412. if ((flags & AVSEEK_FLAG_FRAME) || (flags & AVSEEK_FLAG_BYTE))
  413. return AVERROR(ENOSYS);
  414. if (!avctx->pb->seekable)
  415. return AVERROR(EIO);
  416. frame = av_index_search_timestamp(st, timestamp, flags);
  417. if (frame < 0)
  418. return AVERROR_INVALIDDATA;
  419. for (i = 0; i < avctx->nb_streams; i++)
  420. mv->frame[i] = frame;
  421. return 0;
  422. }
  423. AVInputFormat ff_mv_demuxer = {
  424. .name = "mv",
  425. .long_name = NULL_IF_CONFIG_SMALL("Silicon Graphics Movie"),
  426. .priv_data_size = sizeof(MvContext),
  427. .read_probe = mv_probe,
  428. .read_header = mv_read_header,
  429. .read_packet = mv_read_packet,
  430. .read_seek = mv_read_seek,
  431. };