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.

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