ipmovie.c 20 KB

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