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.

495 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 * 2);
  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 * 2);
  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 > 1) {
  328. avpriv_request_sample(avctx, "Multiple audio streams support");
  329. return AVERROR_PATCHWELCOME;
  330. } else if (mv->nb_audio_tracks) {
  331. ast = avformat_new_stream(avctx, NULL);
  332. if (!ast)
  333. return AVERROR(ENOMEM);
  334. ast->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  335. if ((read_table(avctx, ast, parse_audio_var)) < 0)
  336. return ret;
  337. if (mv->acompression == 100 &&
  338. mv->aformat == AUDIO_FORMAT_SIGNED &&
  339. ast->codecpar->bits_per_coded_sample == 16) {
  340. ast->codecpar->codec_id = AV_CODEC_ID_PCM_S16BE;
  341. } else {
  342. avpriv_request_sample(avctx,
  343. "Audio compression %i (format %i, sr %i)",
  344. mv->acompression, mv->aformat,
  345. ast->codecpar->bits_per_coded_sample);
  346. ast->codecpar->codec_id = AV_CODEC_ID_NONE;
  347. }
  348. if (ast->codecpar->channels <= 0) {
  349. av_log(avctx, AV_LOG_ERROR, "No valid channel count found.\n");
  350. return AVERROR_INVALIDDATA;
  351. }
  352. }
  353. if (mv->nb_video_tracks > 1) {
  354. avpriv_request_sample(avctx, "Multiple video streams support");
  355. return AVERROR_PATCHWELCOME;
  356. } else if (mv->nb_video_tracks) {
  357. vst = avformat_new_stream(avctx, NULL);
  358. if (!vst)
  359. return AVERROR(ENOMEM);
  360. vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  361. if ((ret = read_table(avctx, vst, parse_video_var))<0)
  362. return ret;
  363. }
  364. if (mv->nb_audio_tracks)
  365. read_index(pb, ast);
  366. if (mv->nb_video_tracks)
  367. read_index(pb, vst);
  368. } else {
  369. avpriv_request_sample(avctx, "Version %i", version);
  370. return AVERROR_PATCHWELCOME;
  371. }
  372. return 0;
  373. }
  374. static int mv_read_packet(AVFormatContext *avctx, AVPacket *pkt)
  375. {
  376. MvContext *mv = avctx->priv_data;
  377. AVIOContext *pb = avctx->pb;
  378. AVStream *st = avctx->streams[mv->stream_index];
  379. const AVIndexEntry *index;
  380. int frame = mv->frame[mv->stream_index];
  381. int64_t ret;
  382. uint64_t pos;
  383. if (frame < st->nb_index_entries) {
  384. index = &st->index_entries[frame];
  385. pos = avio_tell(pb);
  386. if (index->pos > pos)
  387. avio_skip(pb, index->pos - pos);
  388. else if (index->pos < pos) {
  389. if (!(pb->seekable & AVIO_SEEKABLE_NORMAL))
  390. return AVERROR(EIO);
  391. ret = avio_seek(pb, index->pos, SEEK_SET);
  392. if (ret < 0)
  393. return ret;
  394. }
  395. ret = av_get_packet(pb, pkt, index->size);
  396. if (ret < 0)
  397. return ret;
  398. pkt->stream_index = mv->stream_index;
  399. pkt->pts = index->timestamp;
  400. pkt->flags |= AV_PKT_FLAG_KEY;
  401. mv->frame[mv->stream_index]++;
  402. mv->eof_count = 0;
  403. } else {
  404. mv->eof_count++;
  405. if (mv->eof_count >= avctx->nb_streams)
  406. return AVERROR_EOF;
  407. // avoid returning 0 without a packet
  408. return AVERROR(EAGAIN);
  409. }
  410. mv->stream_index++;
  411. if (mv->stream_index >= avctx->nb_streams)
  412. mv->stream_index = 0;
  413. return 0;
  414. }
  415. static int mv_read_seek(AVFormatContext *avctx, int stream_index,
  416. int64_t timestamp, int flags)
  417. {
  418. MvContext *mv = avctx->priv_data;
  419. AVStream *st = avctx->streams[stream_index];
  420. int frame, i;
  421. if ((flags & AVSEEK_FLAG_FRAME) || (flags & AVSEEK_FLAG_BYTE))
  422. return AVERROR(ENOSYS);
  423. if (!(avctx->pb->seekable & AVIO_SEEKABLE_NORMAL))
  424. return AVERROR(EIO);
  425. frame = av_index_search_timestamp(st, timestamp, flags);
  426. if (frame < 0)
  427. return AVERROR_INVALIDDATA;
  428. for (i = 0; i < avctx->nb_streams; i++)
  429. mv->frame[i] = frame;
  430. return 0;
  431. }
  432. AVInputFormat ff_mv_demuxer = {
  433. .name = "mv",
  434. .long_name = NULL_IF_CONFIG_SMALL("Silicon Graphics Movie"),
  435. .priv_data_size = sizeof(MvContext),
  436. .read_probe = mv_probe,
  437. .read_header = mv_read_header,
  438. .read_packet = mv_read_packet,
  439. .read_seek = mv_read_seek,
  440. };