ipmovie.c 20 KB

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