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.

660 lines
22KB

  1. /*
  2. * Interplay MVE File Demuxer
  3. * Copyright (c) 2003 The ffmpeg Project
  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. * Interplay MVE file demuxer
  24. * by Mike Melanson (melanson@pcisys.net)
  25. * For more information regarding the Interplay MVE file format, visit:
  26. * http://www.pcisys.net/~melanson/codecs/
  27. * The aforementioned site also contains a command line utility for parsing
  28. * IP MVE files so that you can get a good idea of the typical structure of
  29. * such files. This demuxer is not the best example to use if you are trying
  30. * to write your own as it uses a rather roundabout approach for splitting
  31. * up and sending out the chunks.
  32. */
  33. #include "libavutil/channel_layout.h"
  34. #include "libavutil/intreadwrite.h"
  35. #include "avformat.h"
  36. #include "internal.h"
  37. #define CHUNK_PREAMBLE_SIZE 4
  38. #define OPCODE_PREAMBLE_SIZE 4
  39. #define CHUNK_INIT_AUDIO 0x0000
  40. #define CHUNK_AUDIO_ONLY 0x0001
  41. #define CHUNK_INIT_VIDEO 0x0002
  42. #define CHUNK_VIDEO 0x0003
  43. #define CHUNK_SHUTDOWN 0x0004
  44. #define CHUNK_END 0x0005
  45. /* these last types are used internally */
  46. #define CHUNK_DONE 0xFFFC
  47. #define CHUNK_NOMEM 0xFFFD
  48. #define CHUNK_EOF 0xFFFE
  49. #define CHUNK_BAD 0xFFFF
  50. #define OPCODE_END_OF_STREAM 0x00
  51. #define OPCODE_END_OF_CHUNK 0x01
  52. #define OPCODE_CREATE_TIMER 0x02
  53. #define OPCODE_INIT_AUDIO_BUFFERS 0x03
  54. #define OPCODE_START_STOP_AUDIO 0x04
  55. #define OPCODE_INIT_VIDEO_BUFFERS 0x05
  56. #define OPCODE_UNKNOWN_06 0x06
  57. #define OPCODE_SEND_BUFFER 0x07
  58. #define OPCODE_AUDIO_FRAME 0x08
  59. #define OPCODE_SILENCE_FRAME 0x09
  60. #define OPCODE_INIT_VIDEO_MODE 0x0A
  61. #define OPCODE_CREATE_GRADIENT 0x0B
  62. #define OPCODE_SET_PALETTE 0x0C
  63. #define OPCODE_SET_PALETTE_COMPRESSED 0x0D
  64. #define OPCODE_UNKNOWN_0E 0x0E
  65. #define OPCODE_SET_DECODING_MAP 0x0F
  66. #define OPCODE_UNKNOWN_10 0x10
  67. #define OPCODE_VIDEO_DATA 0x11
  68. #define OPCODE_UNKNOWN_12 0x12
  69. #define OPCODE_UNKNOWN_13 0x13
  70. #define OPCODE_UNKNOWN_14 0x14
  71. #define OPCODE_UNKNOWN_15 0x15
  72. #define PALETTE_COUNT 256
  73. typedef struct IPMVEContext {
  74. unsigned char *buf;
  75. int buf_size;
  76. uint64_t frame_pts_inc;
  77. unsigned int video_bpp;
  78. unsigned int video_width;
  79. unsigned int video_height;
  80. int64_t video_pts;
  81. uint32_t palette[256];
  82. int has_palette;
  83. int changed;
  84. unsigned int audio_bits;
  85. unsigned int audio_channels;
  86. unsigned int audio_sample_rate;
  87. enum AVCodecID audio_type;
  88. unsigned int audio_frame_count;
  89. int video_stream_index;
  90. int audio_stream_index;
  91. int64_t audio_chunk_offset;
  92. int audio_chunk_size;
  93. int64_t video_chunk_offset;
  94. int video_chunk_size;
  95. int64_t decode_map_chunk_offset;
  96. int decode_map_chunk_size;
  97. int64_t next_chunk_offset;
  98. } IPMVEContext;
  99. static int load_ipmovie_packet(IPMVEContext *s, AVIOContext *pb,
  100. AVPacket *pkt) {
  101. int chunk_type;
  102. if (s->audio_chunk_offset && s->audio_channels && s->audio_bits) {
  103. if (s->audio_type == AV_CODEC_ID_NONE) {
  104. av_log(NULL, AV_LOG_ERROR, "Can not read audio packet before"
  105. "audio codec is known\n");
  106. return CHUNK_BAD;
  107. }
  108. /* adjust for PCM audio by skipping chunk header */
  109. if (s->audio_type != AV_CODEC_ID_INTERPLAY_DPCM) {
  110. s->audio_chunk_offset += 6;
  111. s->audio_chunk_size -= 6;
  112. }
  113. avio_seek(pb, s->audio_chunk_offset, SEEK_SET);
  114. s->audio_chunk_offset = 0;
  115. if (s->audio_chunk_size != av_get_packet(pb, pkt, s->audio_chunk_size))
  116. return CHUNK_EOF;
  117. pkt->stream_index = s->audio_stream_index;
  118. pkt->pts = s->audio_frame_count;
  119. /* audio frame maintenance */
  120. if (s->audio_type != AV_CODEC_ID_INTERPLAY_DPCM)
  121. s->audio_frame_count +=
  122. (s->audio_chunk_size / s->audio_channels / (s->audio_bits / 8));
  123. else
  124. s->audio_frame_count +=
  125. (s->audio_chunk_size - 6 - s->audio_channels) / s->audio_channels;
  126. av_dlog(NULL, "sending audio frame with pts %"PRId64" (%d audio frames)\n",
  127. pkt->pts, s->audio_frame_count);
  128. chunk_type = CHUNK_VIDEO;
  129. } else if (s->decode_map_chunk_offset) {
  130. /* send both the decode map and the video data together */
  131. if (av_new_packet(pkt, s->decode_map_chunk_size + s->video_chunk_size))
  132. return CHUNK_NOMEM;
  133. if (s->has_palette) {
  134. uint8_t *pal;
  135. pal = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE,
  136. AVPALETTE_SIZE);
  137. if (pal) {
  138. memcpy(pal, s->palette, AVPALETTE_SIZE);
  139. s->has_palette = 0;
  140. }
  141. }
  142. if (s->changed) {
  143. ff_add_param_change(pkt, 0, 0, 0, s->video_width, s->video_height);
  144. s->changed = 0;
  145. }
  146. pkt->pos= s->decode_map_chunk_offset;
  147. avio_seek(pb, s->decode_map_chunk_offset, SEEK_SET);
  148. s->decode_map_chunk_offset = 0;
  149. if (avio_read(pb, pkt->data, s->decode_map_chunk_size) !=
  150. s->decode_map_chunk_size) {
  151. av_free_packet(pkt);
  152. return CHUNK_EOF;
  153. }
  154. avio_seek(pb, s->video_chunk_offset, SEEK_SET);
  155. s->video_chunk_offset = 0;
  156. if (avio_read(pb, pkt->data + s->decode_map_chunk_size,
  157. s->video_chunk_size) != s->video_chunk_size) {
  158. av_free_packet(pkt);
  159. return CHUNK_EOF;
  160. }
  161. pkt->stream_index = s->video_stream_index;
  162. pkt->pts = s->video_pts;
  163. av_dlog(NULL, "sending video frame with pts %"PRId64"\n", pkt->pts);
  164. s->video_pts += s->frame_pts_inc;
  165. chunk_type = CHUNK_VIDEO;
  166. } else {
  167. avio_seek(pb, s->next_chunk_offset, SEEK_SET);
  168. chunk_type = CHUNK_DONE;
  169. }
  170. return chunk_type;
  171. }
  172. /* This function loads and processes a single chunk in an IP movie file.
  173. * It returns the type of chunk that was processed. */
  174. static int process_ipmovie_chunk(IPMVEContext *s, AVIOContext *pb,
  175. AVPacket *pkt)
  176. {
  177. unsigned char chunk_preamble[CHUNK_PREAMBLE_SIZE];
  178. int chunk_type;
  179. int chunk_size;
  180. unsigned char opcode_preamble[OPCODE_PREAMBLE_SIZE];
  181. unsigned char opcode_type;
  182. unsigned char opcode_version;
  183. int opcode_size;
  184. unsigned char scratch[1024];
  185. int i, j;
  186. int first_color, last_color;
  187. int audio_flags;
  188. unsigned char r, g, b;
  189. unsigned int width, height;
  190. /* see if there are any pending packets */
  191. chunk_type = load_ipmovie_packet(s, pb, pkt);
  192. if (chunk_type != CHUNK_DONE)
  193. return chunk_type;
  194. /* read the next chunk, wherever the file happens to be pointing */
  195. if (url_feof(pb))
  196. return CHUNK_EOF;
  197. if (avio_read(pb, chunk_preamble, CHUNK_PREAMBLE_SIZE) !=
  198. CHUNK_PREAMBLE_SIZE)
  199. return CHUNK_BAD;
  200. chunk_size = AV_RL16(&chunk_preamble[0]);
  201. chunk_type = AV_RL16(&chunk_preamble[2]);
  202. av_dlog(NULL, "chunk type 0x%04X, 0x%04X bytes: ", chunk_type, chunk_size);
  203. switch (chunk_type) {
  204. case CHUNK_INIT_AUDIO:
  205. av_dlog(NULL, "initialize audio\n");
  206. break;
  207. case CHUNK_AUDIO_ONLY:
  208. av_dlog(NULL, "audio only\n");
  209. break;
  210. case CHUNK_INIT_VIDEO:
  211. av_dlog(NULL, "initialize video\n");
  212. break;
  213. case CHUNK_VIDEO:
  214. av_dlog(NULL, "video (and audio)\n");
  215. break;
  216. case CHUNK_SHUTDOWN:
  217. av_dlog(NULL, "shutdown\n");
  218. break;
  219. case CHUNK_END:
  220. av_dlog(NULL, "end\n");
  221. break;
  222. default:
  223. av_dlog(NULL, "invalid chunk\n");
  224. chunk_type = CHUNK_BAD;
  225. break;
  226. }
  227. while ((chunk_size > 0) && (chunk_type != CHUNK_BAD)) {
  228. /* read the next chunk, wherever the file happens to be pointing */
  229. if (url_feof(pb)) {
  230. chunk_type = CHUNK_EOF;
  231. break;
  232. }
  233. if (avio_read(pb, opcode_preamble, CHUNK_PREAMBLE_SIZE) !=
  234. CHUNK_PREAMBLE_SIZE) {
  235. chunk_type = CHUNK_BAD;
  236. break;
  237. }
  238. opcode_size = AV_RL16(&opcode_preamble[0]);
  239. opcode_type = opcode_preamble[2];
  240. opcode_version = opcode_preamble[3];
  241. chunk_size -= OPCODE_PREAMBLE_SIZE;
  242. chunk_size -= opcode_size;
  243. if (chunk_size < 0) {
  244. av_dlog(NULL, "chunk_size countdown just went negative\n");
  245. chunk_type = CHUNK_BAD;
  246. break;
  247. }
  248. av_dlog(NULL, " opcode type %02X, version %d, 0x%04X bytes: ",
  249. opcode_type, opcode_version, opcode_size);
  250. switch (opcode_type) {
  251. case OPCODE_END_OF_STREAM:
  252. av_dlog(NULL, "end of stream\n");
  253. avio_skip(pb, opcode_size);
  254. break;
  255. case OPCODE_END_OF_CHUNK:
  256. av_dlog(NULL, "end of chunk\n");
  257. avio_skip(pb, opcode_size);
  258. break;
  259. case OPCODE_CREATE_TIMER:
  260. av_dlog(NULL, "create timer\n");
  261. if ((opcode_version > 0) || (opcode_size != 6)) {
  262. av_dlog(NULL, "bad create_timer opcode\n");
  263. chunk_type = CHUNK_BAD;
  264. break;
  265. }
  266. if (avio_read(pb, scratch, opcode_size) !=
  267. opcode_size) {
  268. chunk_type = CHUNK_BAD;
  269. break;
  270. }
  271. s->frame_pts_inc = ((uint64_t)AV_RL32(&scratch[0])) * AV_RL16(&scratch[4]);
  272. av_dlog(NULL, " %.2f frames/second (timer div = %d, subdiv = %d)\n",
  273. 1000000.0 / s->frame_pts_inc, AV_RL32(&scratch[0]),
  274. AV_RL16(&scratch[4]));
  275. break;
  276. case OPCODE_INIT_AUDIO_BUFFERS:
  277. av_dlog(NULL, "initialize audio buffers\n");
  278. if (opcode_version > 1 || opcode_size > 10 || opcode_size < 6) {
  279. av_dlog(NULL, "bad init_audio_buffers opcode\n");
  280. chunk_type = CHUNK_BAD;
  281. break;
  282. }
  283. if (avio_read(pb, scratch, opcode_size) !=
  284. opcode_size) {
  285. chunk_type = CHUNK_BAD;
  286. break;
  287. }
  288. s->audio_sample_rate = AV_RL16(&scratch[4]);
  289. audio_flags = AV_RL16(&scratch[2]);
  290. /* bit 0 of the flags: 0 = mono, 1 = stereo */
  291. s->audio_channels = (audio_flags & 1) + 1;
  292. /* bit 1 of the flags: 0 = 8 bit, 1 = 16 bit */
  293. s->audio_bits = (((audio_flags >> 1) & 1) + 1) * 8;
  294. /* bit 2 indicates compressed audio in version 1 opcode */
  295. if ((opcode_version == 1) && (audio_flags & 0x4))
  296. s->audio_type = AV_CODEC_ID_INTERPLAY_DPCM;
  297. else if (s->audio_bits == 16)
  298. s->audio_type = AV_CODEC_ID_PCM_S16LE;
  299. else
  300. s->audio_type = AV_CODEC_ID_PCM_U8;
  301. av_dlog(NULL, "audio: %d bits, %d Hz, %s, %s format\n",
  302. s->audio_bits, s->audio_sample_rate,
  303. (s->audio_channels == 2) ? "stereo" : "mono",
  304. (s->audio_type == AV_CODEC_ID_INTERPLAY_DPCM) ?
  305. "Interplay audio" : "PCM");
  306. break;
  307. case OPCODE_START_STOP_AUDIO:
  308. av_dlog(NULL, "start/stop audio\n");
  309. avio_skip(pb, opcode_size);
  310. break;
  311. case OPCODE_INIT_VIDEO_BUFFERS:
  312. av_dlog(NULL, "initialize video buffers\n");
  313. if ((opcode_version > 2) || (opcode_size > 8) || opcode_size < 4
  314. || opcode_version == 2 && opcode_size < 8
  315. ) {
  316. av_dlog(NULL, "bad init_video_buffers opcode\n");
  317. chunk_type = CHUNK_BAD;
  318. break;
  319. }
  320. if (avio_read(pb, scratch, opcode_size) !=
  321. opcode_size) {
  322. chunk_type = CHUNK_BAD;
  323. break;
  324. }
  325. width = AV_RL16(&scratch[0]) * 8;
  326. height = AV_RL16(&scratch[2]) * 8;
  327. if (width != s->video_width) {
  328. s->video_width = width;
  329. s->changed++;
  330. }
  331. if (height != s->video_height) {
  332. s->video_height = height;
  333. s->changed++;
  334. }
  335. if (opcode_version < 2 || !AV_RL16(&scratch[6])) {
  336. s->video_bpp = 8;
  337. } else {
  338. s->video_bpp = 16;
  339. }
  340. av_dlog(NULL, "video resolution: %d x %d\n",
  341. s->video_width, s->video_height);
  342. break;
  343. case OPCODE_UNKNOWN_06:
  344. case OPCODE_UNKNOWN_0E:
  345. case OPCODE_UNKNOWN_10:
  346. case OPCODE_UNKNOWN_12:
  347. case OPCODE_UNKNOWN_13:
  348. case OPCODE_UNKNOWN_14:
  349. case OPCODE_UNKNOWN_15:
  350. av_dlog(NULL, "unknown (but documented) opcode %02X\n", opcode_type);
  351. avio_skip(pb, opcode_size);
  352. break;
  353. case OPCODE_SEND_BUFFER:
  354. av_dlog(NULL, "send buffer\n");
  355. avio_skip(pb, opcode_size);
  356. break;
  357. case OPCODE_AUDIO_FRAME:
  358. av_dlog(NULL, "audio frame\n");
  359. /* log position and move on for now */
  360. s->audio_chunk_offset = avio_tell(pb);
  361. s->audio_chunk_size = opcode_size;
  362. avio_skip(pb, opcode_size);
  363. break;
  364. case OPCODE_SILENCE_FRAME:
  365. av_dlog(NULL, "silence frame\n");
  366. avio_skip(pb, opcode_size);
  367. break;
  368. case OPCODE_INIT_VIDEO_MODE:
  369. av_dlog(NULL, "initialize video mode\n");
  370. avio_skip(pb, opcode_size);
  371. break;
  372. case OPCODE_CREATE_GRADIENT:
  373. av_dlog(NULL, "create gradient\n");
  374. avio_skip(pb, opcode_size);
  375. break;
  376. case OPCODE_SET_PALETTE:
  377. av_dlog(NULL, "set palette\n");
  378. /* check for the logical maximum palette size
  379. * (3 * 256 + 4 bytes) */
  380. if (opcode_size > 0x304 || opcode_size < 4) {
  381. av_dlog(NULL, "demux_ipmovie: set_palette opcode with invalid size\n");
  382. chunk_type = CHUNK_BAD;
  383. break;
  384. }
  385. if (avio_read(pb, scratch, opcode_size) != opcode_size) {
  386. chunk_type = CHUNK_BAD;
  387. break;
  388. }
  389. /* load the palette into internal data structure */
  390. first_color = AV_RL16(&scratch[0]);
  391. last_color = first_color + AV_RL16(&scratch[2]) - 1;
  392. /* sanity check (since they are 16 bit values) */
  393. if ( (first_color > 0xFF) || (last_color > 0xFF)
  394. || (last_color - first_color + 1)*3 + 4 > opcode_size) {
  395. av_dlog(NULL, "demux_ipmovie: set_palette indexes out of range (%d -> %d)\n",
  396. first_color, last_color);
  397. chunk_type = CHUNK_BAD;
  398. break;
  399. }
  400. j = 4; /* offset of first palette data */
  401. for (i = first_color; i <= last_color; i++) {
  402. /* the palette is stored as a 6-bit VGA palette, thus each
  403. * component is shifted up to a 8-bit range */
  404. r = scratch[j++] * 4;
  405. g = scratch[j++] * 4;
  406. b = scratch[j++] * 4;
  407. s->palette[i] = (0xFFU << 24) | (r << 16) | (g << 8) | (b);
  408. s->palette[i] |= s->palette[i] >> 6 & 0x30303;
  409. }
  410. s->has_palette = 1;
  411. break;
  412. case OPCODE_SET_PALETTE_COMPRESSED:
  413. av_dlog(NULL, "set palette compressed\n");
  414. avio_skip(pb, opcode_size);
  415. break;
  416. case OPCODE_SET_DECODING_MAP:
  417. av_dlog(NULL, "set decoding map\n");
  418. /* log position and move on for now */
  419. s->decode_map_chunk_offset = avio_tell(pb);
  420. s->decode_map_chunk_size = opcode_size;
  421. avio_skip(pb, opcode_size);
  422. break;
  423. case OPCODE_VIDEO_DATA:
  424. av_dlog(NULL, "set video data\n");
  425. /* log position and move on for now */
  426. s->video_chunk_offset = avio_tell(pb);
  427. s->video_chunk_size = opcode_size;
  428. avio_skip(pb, opcode_size);
  429. break;
  430. default:
  431. av_dlog(NULL, "*** unknown opcode type\n");
  432. chunk_type = CHUNK_BAD;
  433. break;
  434. }
  435. }
  436. /* make a note of where the stream is sitting */
  437. s->next_chunk_offset = avio_tell(pb);
  438. /* dispatch the first of any pending packets */
  439. if ((chunk_type == CHUNK_VIDEO) || (chunk_type == CHUNK_AUDIO_ONLY))
  440. chunk_type = load_ipmovie_packet(s, pb, pkt);
  441. return chunk_type;
  442. }
  443. static const char signature[] = "Interplay MVE File\x1A\0\x1A";
  444. static int ipmovie_probe(AVProbeData *p)
  445. {
  446. const uint8_t *b = p->buf;
  447. const uint8_t *b_end = p->buf + p->buf_size - sizeof(signature);
  448. do {
  449. if (b[0] == signature[0] && memcmp(b, signature, sizeof(signature)) == 0)
  450. return AVPROBE_SCORE_MAX;
  451. b++;
  452. } while (b < b_end);
  453. return 0;
  454. }
  455. static int ipmovie_read_header(AVFormatContext *s)
  456. {
  457. IPMVEContext *ipmovie = s->priv_data;
  458. AVIOContext *pb = s->pb;
  459. AVPacket pkt;
  460. AVStream *st;
  461. unsigned char chunk_preamble[CHUNK_PREAMBLE_SIZE];
  462. int chunk_type, i;
  463. uint8_t signature_buffer[sizeof(signature)];
  464. avio_read(pb, signature_buffer, sizeof(signature_buffer));
  465. while (memcmp(signature_buffer, signature, sizeof(signature))) {
  466. memmove(signature_buffer, signature_buffer + 1, sizeof(signature_buffer) - 1);
  467. signature_buffer[sizeof(signature_buffer) - 1] = avio_r8(pb);
  468. if (url_feof(pb))
  469. return AVERROR_EOF;
  470. }
  471. /* initialize private context members */
  472. ipmovie->video_pts = ipmovie->audio_frame_count = 0;
  473. ipmovie->audio_chunk_offset = ipmovie->video_chunk_offset =
  474. ipmovie->decode_map_chunk_offset = 0;
  475. /* on the first read, this will position the stream at the first chunk */
  476. ipmovie->next_chunk_offset = avio_tell(pb) + 4;
  477. for (i = 0; i < 256; i++)
  478. ipmovie->palette[i] = 0xFFU << 24;
  479. /* process the first chunk which should be CHUNK_INIT_VIDEO */
  480. if (process_ipmovie_chunk(ipmovie, pb, &pkt) != CHUNK_INIT_VIDEO)
  481. return AVERROR_INVALIDDATA;
  482. /* peek ahead to the next chunk-- if it is an init audio chunk, process
  483. * it; if it is the first video chunk, this is a silent file */
  484. if (avio_read(pb, chunk_preamble, CHUNK_PREAMBLE_SIZE) !=
  485. CHUNK_PREAMBLE_SIZE)
  486. return AVERROR(EIO);
  487. chunk_type = AV_RL16(&chunk_preamble[2]);
  488. avio_seek(pb, -CHUNK_PREAMBLE_SIZE, SEEK_CUR);
  489. if (chunk_type == CHUNK_VIDEO)
  490. ipmovie->audio_type = AV_CODEC_ID_NONE; /* no audio */
  491. else if (process_ipmovie_chunk(ipmovie, pb, &pkt) != CHUNK_INIT_AUDIO)
  492. return AVERROR_INVALIDDATA;
  493. /* initialize the stream decoders */
  494. st = avformat_new_stream(s, NULL);
  495. if (!st)
  496. return AVERROR(ENOMEM);
  497. avpriv_set_pts_info(st, 63, 1, 1000000);
  498. ipmovie->video_stream_index = st->index;
  499. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  500. st->codec->codec_id = AV_CODEC_ID_INTERPLAY_VIDEO;
  501. st->codec->codec_tag = 0; /* no fourcc */
  502. st->codec->width = ipmovie->video_width;
  503. st->codec->height = ipmovie->video_height;
  504. st->codec->bits_per_coded_sample = ipmovie->video_bpp;
  505. if (ipmovie->audio_type) {
  506. st = avformat_new_stream(s, NULL);
  507. if (!st)
  508. return AVERROR(ENOMEM);
  509. avpriv_set_pts_info(st, 32, 1, ipmovie->audio_sample_rate);
  510. ipmovie->audio_stream_index = st->index;
  511. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  512. st->codec->codec_id = ipmovie->audio_type;
  513. st->codec->codec_tag = 0; /* no tag */
  514. st->codec->channels = ipmovie->audio_channels;
  515. st->codec->channel_layout = st->codec->channels == 1 ? AV_CH_LAYOUT_MONO :
  516. AV_CH_LAYOUT_STEREO;
  517. st->codec->sample_rate = ipmovie->audio_sample_rate;
  518. st->codec->bits_per_coded_sample = ipmovie->audio_bits;
  519. st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
  520. st->codec->bits_per_coded_sample;
  521. if (st->codec->codec_id == AV_CODEC_ID_INTERPLAY_DPCM)
  522. st->codec->bit_rate /= 2;
  523. st->codec->block_align = st->codec->channels * st->codec->bits_per_coded_sample;
  524. }
  525. return 0;
  526. }
  527. static int ipmovie_read_packet(AVFormatContext *s,
  528. AVPacket *pkt)
  529. {
  530. IPMVEContext *ipmovie = s->priv_data;
  531. AVIOContext *pb = s->pb;
  532. int ret;
  533. for (;;) {
  534. ret = process_ipmovie_chunk(ipmovie, pb, pkt);
  535. if (ret == CHUNK_BAD)
  536. ret = AVERROR_INVALIDDATA;
  537. else if (ret == CHUNK_EOF)
  538. ret = AVERROR(EIO);
  539. else if (ret == CHUNK_NOMEM)
  540. ret = AVERROR(ENOMEM);
  541. else if (ret == CHUNK_VIDEO)
  542. ret = 0;
  543. else if (ret == CHUNK_INIT_VIDEO || ret == CHUNK_INIT_AUDIO)
  544. continue;
  545. else
  546. ret = -1;
  547. return ret;
  548. }
  549. }
  550. AVInputFormat ff_ipmovie_demuxer = {
  551. .name = "ipmovie",
  552. .long_name = NULL_IF_CONFIG_SMALL("Interplay MVE"),
  553. .priv_data_size = sizeof(IPMVEContext),
  554. .read_probe = ipmovie_probe,
  555. .read_header = ipmovie_read_header,
  556. .read_packet = ipmovie_read_packet,
  557. };