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.

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