ipmovie.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. /*
  2. * Interplay MVE File Demuxer
  3. * Copyright (c) 2003 The ffmpeg Project
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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. /* 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 CHUNK_PREAMBLE_SIZE 4
  45. #define OPCODE_PREAMBLE_SIZE 4
  46. #define CHUNK_INIT_AUDIO 0x0000
  47. #define CHUNK_AUDIO_ONLY 0x0001
  48. #define CHUNK_INIT_VIDEO 0x0002
  49. #define CHUNK_VIDEO 0x0003
  50. #define CHUNK_SHUTDOWN 0x0004
  51. #define CHUNK_END 0x0005
  52. /* these last types are used internally */
  53. #define CHUNK_DONE 0xFFFC
  54. #define CHUNK_NOMEM 0xFFFD
  55. #define CHUNK_EOF 0xFFFE
  56. #define CHUNK_BAD 0xFFFF
  57. #define OPCODE_END_OF_STREAM 0x00
  58. #define OPCODE_END_OF_CHUNK 0x01
  59. #define OPCODE_CREATE_TIMER 0x02
  60. #define OPCODE_INIT_AUDIO_BUFFERS 0x03
  61. #define OPCODE_START_STOP_AUDIO 0x04
  62. #define OPCODE_INIT_VIDEO_BUFFERS 0x05
  63. #define OPCODE_UNKNOWN_06 0x06
  64. #define OPCODE_SEND_BUFFER 0x07
  65. #define OPCODE_AUDIO_FRAME 0x08
  66. #define OPCODE_SILENCE_FRAME 0x09
  67. #define OPCODE_INIT_VIDEO_MODE 0x0A
  68. #define OPCODE_CREATE_GRADIENT 0x0B
  69. #define OPCODE_SET_PALETTE 0x0C
  70. #define OPCODE_SET_PALETTE_COMPRESSED 0x0D
  71. #define OPCODE_UNKNOWN_0E 0x0E
  72. #define OPCODE_SET_DECODING_MAP 0x0F
  73. #define OPCODE_UNKNOWN_10 0x10
  74. #define OPCODE_VIDEO_DATA 0x11
  75. #define OPCODE_UNKNOWN_12 0x12
  76. #define OPCODE_UNKNOWN_13 0x13
  77. #define OPCODE_UNKNOWN_14 0x14
  78. #define OPCODE_UNKNOWN_15 0x15
  79. #define PALETTE_COUNT 256
  80. typedef struct IPMVEContext {
  81. unsigned char *buf;
  82. int buf_size;
  83. uint64_t frame_pts_inc;
  84. unsigned int video_bpp;
  85. unsigned int video_width;
  86. unsigned int video_height;
  87. int64_t video_pts;
  88. unsigned int audio_bits;
  89. unsigned int audio_channels;
  90. unsigned int audio_sample_rate;
  91. enum CodecID audio_type;
  92. unsigned int audio_frame_count;
  93. int video_stream_index;
  94. int audio_stream_index;
  95. int64_t audio_chunk_offset;
  96. int audio_chunk_size;
  97. int64_t video_chunk_offset;
  98. int video_chunk_size;
  99. int64_t decode_map_chunk_offset;
  100. int decode_map_chunk_size;
  101. int64_t next_chunk_offset;
  102. AVPaletteControl palette_control;
  103. } IPMVEContext;
  104. static int load_ipmovie_packet(IPMVEContext *s, AVIOContext *pb,
  105. AVPacket *pkt) {
  106. int chunk_type;
  107. if (s->audio_chunk_offset) {
  108. /* adjust for PCM audio by skipping chunk header */
  109. if (s->audio_type != 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 != 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;
  126. debug_ipmovie("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. pkt->pos= s->decode_map_chunk_offset;
  134. avio_seek(pb, s->decode_map_chunk_offset, SEEK_SET);
  135. s->decode_map_chunk_offset = 0;
  136. if (avio_read(pb, pkt->data, s->decode_map_chunk_size) !=
  137. s->decode_map_chunk_size) {
  138. av_free_packet(pkt);
  139. return CHUNK_EOF;
  140. }
  141. avio_seek(pb, s->video_chunk_offset, SEEK_SET);
  142. s->video_chunk_offset = 0;
  143. if (avio_read(pb, pkt->data + s->decode_map_chunk_size,
  144. s->video_chunk_size) != s->video_chunk_size) {
  145. av_free_packet(pkt);
  146. return CHUNK_EOF;
  147. }
  148. pkt->stream_index = s->video_stream_index;
  149. pkt->pts = s->video_pts;
  150. debug_ipmovie("sending video frame with pts %"PRId64"\n",
  151. 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 (pb->eof_reached)
  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. debug_ipmovie("chunk type 0x%04X, 0x%04X bytes: ", chunk_type, chunk_size);
  190. switch (chunk_type) {
  191. case CHUNK_INIT_AUDIO:
  192. debug_ipmovie("initialize audio\n");
  193. break;
  194. case CHUNK_AUDIO_ONLY:
  195. debug_ipmovie("audio only\n");
  196. break;
  197. case CHUNK_INIT_VIDEO:
  198. debug_ipmovie("initialize video\n");
  199. break;
  200. case CHUNK_VIDEO:
  201. debug_ipmovie("video (and audio)\n");
  202. break;
  203. case CHUNK_SHUTDOWN:
  204. debug_ipmovie("shutdown\n");
  205. break;
  206. case CHUNK_END:
  207. debug_ipmovie("end\n");
  208. break;
  209. default:
  210. debug_ipmovie("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 (pb->eof_reached) {
  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. debug_ipmovie("chunk_size countdown just went negative\n");
  232. chunk_type = CHUNK_BAD;
  233. break;
  234. }
  235. debug_ipmovie(" 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. debug_ipmovie("end of stream\n");
  240. avio_skip(pb, opcode_size);
  241. break;
  242. case OPCODE_END_OF_CHUNK:
  243. debug_ipmovie("end of chunk\n");
  244. avio_skip(pb, opcode_size);
  245. break;
  246. case OPCODE_CREATE_TIMER:
  247. debug_ipmovie("create timer\n");
  248. if ((opcode_version > 0) || (opcode_size > 6)) {
  249. debug_ipmovie("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. debug_ipmovie(" %.2f frames/second (timer div = %d, subdiv = %d)\n",
  260. 1000000.0/s->frame_pts_inc, AV_RL32(&scratch[0]), AV_RL16(&scratch[4]));
  261. break;
  262. case OPCODE_INIT_AUDIO_BUFFERS:
  263. debug_ipmovie("initialize audio buffers\n");
  264. if ((opcode_version > 1) || (opcode_size > 10)) {
  265. debug_ipmovie("bad init_audio_buffers opcode\n");
  266. chunk_type = CHUNK_BAD;
  267. break;
  268. }
  269. if (avio_read(pb, scratch, opcode_size) !=
  270. opcode_size) {
  271. chunk_type = CHUNK_BAD;
  272. break;
  273. }
  274. s->audio_sample_rate = AV_RL16(&scratch[4]);
  275. audio_flags = AV_RL16(&scratch[2]);
  276. /* bit 0 of the flags: 0 = mono, 1 = stereo */
  277. s->audio_channels = (audio_flags & 1) + 1;
  278. /* bit 1 of the flags: 0 = 8 bit, 1 = 16 bit */
  279. s->audio_bits = (((audio_flags >> 1) & 1) + 1) * 8;
  280. /* bit 2 indicates compressed audio in version 1 opcode */
  281. if ((opcode_version == 1) && (audio_flags & 0x4))
  282. s->audio_type = CODEC_ID_INTERPLAY_DPCM;
  283. else if (s->audio_bits == 16)
  284. s->audio_type = CODEC_ID_PCM_S16LE;
  285. else
  286. s->audio_type = CODEC_ID_PCM_U8;
  287. debug_ipmovie("audio: %d bits, %d Hz, %s, %s format\n",
  288. s->audio_bits,
  289. 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. debug_ipmovie("start/stop audio\n");
  296. avio_skip(pb, opcode_size);
  297. break;
  298. case OPCODE_INIT_VIDEO_BUFFERS:
  299. debug_ipmovie("initialize video buffers\n");
  300. if ((opcode_version > 2) || (opcode_size > 8)) {
  301. debug_ipmovie("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. debug_ipmovie("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. debug_ipmovie("unknown (but documented) opcode %02X\n", opcode_type);
  328. avio_skip(pb, opcode_size);
  329. break;
  330. case OPCODE_SEND_BUFFER:
  331. debug_ipmovie("send buffer\n");
  332. avio_skip(pb, opcode_size);
  333. break;
  334. case OPCODE_AUDIO_FRAME:
  335. debug_ipmovie("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. debug_ipmovie("silence frame\n");
  343. avio_skip(pb, opcode_size);
  344. break;
  345. case OPCODE_INIT_VIDEO_MODE:
  346. debug_ipmovie("initialize video mode\n");
  347. avio_skip(pb, opcode_size);
  348. break;
  349. case OPCODE_CREATE_GRADIENT:
  350. debug_ipmovie("create gradient\n");
  351. avio_skip(pb, opcode_size);
  352. break;
  353. case OPCODE_SET_PALETTE:
  354. debug_ipmovie("set palette\n");
  355. /* check for the logical maximum palette size
  356. * (3 * 256 + 4 bytes) */
  357. if (opcode_size > 0x304) {
  358. debug_ipmovie("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. debug_ipmovie("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_control.palette[i] = (r << 16) | (g << 8) | (b);
  384. }
  385. /* indicate a palette change */
  386. s->palette_control.palette_changed = 1;
  387. break;
  388. case OPCODE_SET_PALETTE_COMPRESSED:
  389. debug_ipmovie("set palette compressed\n");
  390. avio_skip(pb, opcode_size);
  391. break;
  392. case OPCODE_SET_DECODING_MAP:
  393. debug_ipmovie("set decoding map\n");
  394. /* log position and move on for now */
  395. s->decode_map_chunk_offset = avio_tell(pb);
  396. s->decode_map_chunk_size = opcode_size;
  397. avio_skip(pb, opcode_size);
  398. break;
  399. case OPCODE_VIDEO_DATA:
  400. debug_ipmovie("set video data\n");
  401. /* log position and move on for now */
  402. s->video_chunk_offset = avio_tell(pb);
  403. s->video_chunk_size = opcode_size;
  404. avio_skip(pb, opcode_size);
  405. break;
  406. default:
  407. debug_ipmovie("*** unknown opcode type\n");
  408. chunk_type = CHUNK_BAD;
  409. break;
  410. }
  411. }
  412. /* make a note of where the stream is sitting */
  413. s->next_chunk_offset = avio_tell(pb);
  414. /* dispatch the first of any pending packets */
  415. if ((chunk_type == CHUNK_VIDEO) || (chunk_type == CHUNK_AUDIO_ONLY))
  416. chunk_type = load_ipmovie_packet(s, pb, pkt);
  417. return chunk_type;
  418. }
  419. static const char signature[] = "Interplay MVE File\x1A\0\x1A";
  420. static int ipmovie_probe(AVProbeData *p)
  421. {
  422. uint8_t *b = p->buf;
  423. uint8_t *b_end = p->buf + p->buf_size - sizeof(signature);
  424. do {
  425. if (memcmp(b++, signature, sizeof(signature)) == 0)
  426. return AVPROBE_SCORE_MAX;
  427. } while (b < b_end);
  428. return 0;
  429. }
  430. static int ipmovie_read_header(AVFormatContext *s,
  431. AVFormatParameters *ap)
  432. {
  433. IPMVEContext *ipmovie = s->priv_data;
  434. AVIOContext *pb = s->pb;
  435. AVPacket pkt;
  436. AVStream *st;
  437. unsigned char chunk_preamble[CHUNK_PREAMBLE_SIZE];
  438. int chunk_type;
  439. uint8_t signature_buffer[sizeof(signature)];
  440. avio_read(pb, signature_buffer, sizeof(signature_buffer));
  441. while (memcmp(signature_buffer, signature, sizeof(signature))) {
  442. memmove(signature_buffer, signature_buffer + 1, sizeof(signature_buffer) - 1);
  443. signature_buffer[sizeof(signature_buffer) - 1] = avio_r8(pb);
  444. if (pb->eof_reached)
  445. return AVERROR_EOF;
  446. }
  447. /* initialize private context members */
  448. ipmovie->video_pts = ipmovie->audio_frame_count = 0;
  449. ipmovie->audio_chunk_offset = ipmovie->video_chunk_offset =
  450. ipmovie->decode_map_chunk_offset = 0;
  451. /* on the first read, this will position the stream at the first chunk */
  452. ipmovie->next_chunk_offset = avio_tell(pb) + 4;
  453. /* process the first chunk which should be CHUNK_INIT_VIDEO */
  454. if (process_ipmovie_chunk(ipmovie, pb, &pkt) != CHUNK_INIT_VIDEO)
  455. return AVERROR_INVALIDDATA;
  456. /* peek ahead to the next chunk-- if it is an init audio chunk, process
  457. * it; if it is the first video chunk, this is a silent file */
  458. if (avio_read(pb, chunk_preamble, CHUNK_PREAMBLE_SIZE) !=
  459. CHUNK_PREAMBLE_SIZE)
  460. return AVERROR(EIO);
  461. chunk_type = AV_RL16(&chunk_preamble[2]);
  462. avio_seek(pb, -CHUNK_PREAMBLE_SIZE, SEEK_CUR);
  463. if (chunk_type == CHUNK_VIDEO)
  464. ipmovie->audio_type = CODEC_ID_NONE; /* no audio */
  465. else if (process_ipmovie_chunk(ipmovie, pb, &pkt) != CHUNK_INIT_AUDIO)
  466. return AVERROR_INVALIDDATA;
  467. /* initialize the stream decoders */
  468. st = av_new_stream(s, 0);
  469. if (!st)
  470. return AVERROR(ENOMEM);
  471. av_set_pts_info(st, 63, 1, 1000000);
  472. ipmovie->video_stream_index = st->index;
  473. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  474. st->codec->codec_id = CODEC_ID_INTERPLAY_VIDEO;
  475. st->codec->codec_tag = 0; /* no fourcc */
  476. st->codec->width = ipmovie->video_width;
  477. st->codec->height = ipmovie->video_height;
  478. st->codec->bits_per_coded_sample = ipmovie->video_bpp;
  479. /* palette considerations */
  480. st->codec->palctrl = &ipmovie->palette_control;
  481. if (ipmovie->audio_type) {
  482. st = av_new_stream(s, 0);
  483. if (!st)
  484. return AVERROR(ENOMEM);
  485. av_set_pts_info(st, 32, 1, ipmovie->audio_sample_rate);
  486. ipmovie->audio_stream_index = st->index;
  487. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  488. st->codec->codec_id = ipmovie->audio_type;
  489. st->codec->codec_tag = 0; /* no tag */
  490. st->codec->channels = ipmovie->audio_channels;
  491. st->codec->sample_rate = ipmovie->audio_sample_rate;
  492. st->codec->bits_per_coded_sample = ipmovie->audio_bits;
  493. st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
  494. st->codec->bits_per_coded_sample;
  495. if (st->codec->codec_id == CODEC_ID_INTERPLAY_DPCM)
  496. st->codec->bit_rate /= 2;
  497. st->codec->block_align = st->codec->channels * st->codec->bits_per_coded_sample;
  498. }
  499. return 0;
  500. }
  501. static int ipmovie_read_packet(AVFormatContext *s,
  502. AVPacket *pkt)
  503. {
  504. IPMVEContext *ipmovie = s->priv_data;
  505. AVIOContext *pb = s->pb;
  506. int ret;
  507. ret = process_ipmovie_chunk(ipmovie, pb, pkt);
  508. if (ret == CHUNK_BAD)
  509. ret = AVERROR_INVALIDDATA;
  510. else if (ret == CHUNK_EOF)
  511. ret = AVERROR(EIO);
  512. else if (ret == CHUNK_NOMEM)
  513. ret = AVERROR(ENOMEM);
  514. else if (ret == CHUNK_VIDEO)
  515. ret = 0;
  516. else
  517. ret = -1;
  518. return ret;
  519. }
  520. AVInputFormat ff_ipmovie_demuxer = {
  521. "ipmovie",
  522. NULL_IF_CONFIG_SMALL("Interplay MVE format"),
  523. sizeof(IPMVEContext),
  524. ipmovie_probe,
  525. ipmovie_read_header,
  526. ipmovie_read_packet,
  527. };