ipmovie.c 20 KB

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