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.

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