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.

648 lines
21KB

  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/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) {
  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 (pb->eof_reached)
  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 (pb->eof_reached) {
  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)) {
  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)) {
  314. av_dlog(NULL, "bad init_video_buffers opcode\n");
  315. chunk_type = CHUNK_BAD;
  316. break;
  317. }
  318. if (avio_read(pb, scratch, opcode_size) !=
  319. opcode_size) {
  320. chunk_type = CHUNK_BAD;
  321. break;
  322. }
  323. width = AV_RL16(&scratch[0]) * 8;
  324. height = AV_RL16(&scratch[2]) * 8;
  325. if (width != s->video_width) {
  326. s->video_width = width;
  327. s->changed++;
  328. }
  329. if (height != s->video_height) {
  330. s->video_height = height;
  331. s->changed++;
  332. }
  333. if (opcode_version < 2 || !AV_RL16(&scratch[6])) {
  334. s->video_bpp = 8;
  335. } else {
  336. s->video_bpp = 16;
  337. }
  338. av_dlog(NULL, "video resolution: %d x %d\n",
  339. s->video_width, s->video_height);
  340. break;
  341. case OPCODE_UNKNOWN_06:
  342. case OPCODE_UNKNOWN_0E:
  343. case OPCODE_UNKNOWN_10:
  344. case OPCODE_UNKNOWN_12:
  345. case OPCODE_UNKNOWN_13:
  346. case OPCODE_UNKNOWN_14:
  347. case OPCODE_UNKNOWN_15:
  348. av_dlog(NULL, "unknown (but documented) opcode %02X\n", opcode_type);
  349. avio_skip(pb, opcode_size);
  350. break;
  351. case OPCODE_SEND_BUFFER:
  352. av_dlog(NULL, "send buffer\n");
  353. avio_skip(pb, opcode_size);
  354. break;
  355. case OPCODE_AUDIO_FRAME:
  356. av_dlog(NULL, "audio frame\n");
  357. /* log position and move on for now */
  358. s->audio_chunk_offset = avio_tell(pb);
  359. s->audio_chunk_size = opcode_size;
  360. avio_skip(pb, opcode_size);
  361. break;
  362. case OPCODE_SILENCE_FRAME:
  363. av_dlog(NULL, "silence frame\n");
  364. avio_skip(pb, opcode_size);
  365. break;
  366. case OPCODE_INIT_VIDEO_MODE:
  367. av_dlog(NULL, "initialize video mode\n");
  368. avio_skip(pb, opcode_size);
  369. break;
  370. case OPCODE_CREATE_GRADIENT:
  371. av_dlog(NULL, "create gradient\n");
  372. avio_skip(pb, opcode_size);
  373. break;
  374. case OPCODE_SET_PALETTE:
  375. av_dlog(NULL, "set palette\n");
  376. /* check for the logical maximum palette size
  377. * (3 * 256 + 4 bytes) */
  378. if (opcode_size > 0x304) {
  379. av_dlog(NULL, "demux_ipmovie: set_palette opcode too large\n");
  380. chunk_type = CHUNK_BAD;
  381. break;
  382. }
  383. if (avio_read(pb, scratch, opcode_size) != opcode_size) {
  384. chunk_type = CHUNK_BAD;
  385. break;
  386. }
  387. /* load the palette into internal data structure */
  388. first_color = AV_RL16(&scratch[0]);
  389. last_color = first_color + AV_RL16(&scratch[2]) - 1;
  390. /* sanity check (since they are 16 bit values) */
  391. if ((first_color > 0xFF) || (last_color > 0xFF)) {
  392. av_dlog(NULL, "demux_ipmovie: set_palette indexes out of range (%d -> %d)\n",
  393. first_color, last_color);
  394. chunk_type = CHUNK_BAD;
  395. break;
  396. }
  397. j = 4; /* offset of first palette data */
  398. for (i = first_color; i <= last_color; i++) {
  399. /* the palette is stored as a 6-bit VGA palette, thus each
  400. * component is shifted up to a 8-bit range */
  401. r = scratch[j++] * 4;
  402. g = scratch[j++] * 4;
  403. b = scratch[j++] * 4;
  404. s->palette[i] = (r << 16) | (g << 8) | (b);
  405. }
  406. s->has_palette = 1;
  407. break;
  408. case OPCODE_SET_PALETTE_COMPRESSED:
  409. av_dlog(NULL, "set palette compressed\n");
  410. avio_skip(pb, opcode_size);
  411. break;
  412. case OPCODE_SET_DECODING_MAP:
  413. av_dlog(NULL, "set decoding map\n");
  414. /* log position and move on for now */
  415. s->decode_map_chunk_offset = avio_tell(pb);
  416. s->decode_map_chunk_size = opcode_size;
  417. avio_skip(pb, opcode_size);
  418. break;
  419. case OPCODE_VIDEO_DATA:
  420. av_dlog(NULL, "set video data\n");
  421. /* log position and move on for now */
  422. s->video_chunk_offset = avio_tell(pb);
  423. s->video_chunk_size = opcode_size;
  424. avio_skip(pb, opcode_size);
  425. break;
  426. default:
  427. av_dlog(NULL, "*** unknown opcode type\n");
  428. chunk_type = CHUNK_BAD;
  429. break;
  430. }
  431. }
  432. /* make a note of where the stream is sitting */
  433. s->next_chunk_offset = avio_tell(pb);
  434. /* dispatch the first of any pending packets */
  435. if ((chunk_type == CHUNK_VIDEO) || (chunk_type == CHUNK_AUDIO_ONLY))
  436. chunk_type = load_ipmovie_packet(s, pb, pkt);
  437. return chunk_type;
  438. }
  439. static const char signature[] = "Interplay MVE File\x1A\0\x1A";
  440. static int ipmovie_probe(AVProbeData *p)
  441. {
  442. uint8_t *b = p->buf;
  443. uint8_t *b_end = p->buf + p->buf_size - sizeof(signature);
  444. do {
  445. if (memcmp(b++, signature, sizeof(signature)) == 0)
  446. return AVPROBE_SCORE_MAX;
  447. } while (b < b_end);
  448. return 0;
  449. }
  450. static int ipmovie_read_header(AVFormatContext *s)
  451. {
  452. IPMVEContext *ipmovie = s->priv_data;
  453. AVIOContext *pb = s->pb;
  454. AVPacket pkt;
  455. AVStream *st;
  456. unsigned char chunk_preamble[CHUNK_PREAMBLE_SIZE];
  457. int chunk_type;
  458. uint8_t signature_buffer[sizeof(signature)];
  459. avio_read(pb, signature_buffer, sizeof(signature_buffer));
  460. while (memcmp(signature_buffer, signature, sizeof(signature))) {
  461. memmove(signature_buffer, signature_buffer + 1, sizeof(signature_buffer) - 1);
  462. signature_buffer[sizeof(signature_buffer) - 1] = avio_r8(pb);
  463. if (pb->eof_reached)
  464. return AVERROR_EOF;
  465. }
  466. /* initialize private context members */
  467. ipmovie->video_pts = ipmovie->audio_frame_count = 0;
  468. ipmovie->audio_chunk_offset = ipmovie->video_chunk_offset =
  469. ipmovie->decode_map_chunk_offset = 0;
  470. /* on the first read, this will position the stream at the first chunk */
  471. ipmovie->next_chunk_offset = avio_tell(pb) + 4;
  472. /* process the first chunk which should be CHUNK_INIT_VIDEO */
  473. if (process_ipmovie_chunk(ipmovie, pb, &pkt) != CHUNK_INIT_VIDEO)
  474. return AVERROR_INVALIDDATA;
  475. /* peek ahead to the next chunk-- if it is an init audio chunk, process
  476. * it; if it is the first video chunk, this is a silent file */
  477. if (avio_read(pb, chunk_preamble, CHUNK_PREAMBLE_SIZE) !=
  478. CHUNK_PREAMBLE_SIZE)
  479. return AVERROR(EIO);
  480. chunk_type = AV_RL16(&chunk_preamble[2]);
  481. avio_seek(pb, -CHUNK_PREAMBLE_SIZE, SEEK_CUR);
  482. if (chunk_type == CHUNK_VIDEO)
  483. ipmovie->audio_type = AV_CODEC_ID_NONE; /* no audio */
  484. else if (process_ipmovie_chunk(ipmovie, pb, &pkt) != CHUNK_INIT_AUDIO)
  485. return AVERROR_INVALIDDATA;
  486. /* initialize the stream decoders */
  487. st = avformat_new_stream(s, NULL);
  488. if (!st)
  489. return AVERROR(ENOMEM);
  490. avpriv_set_pts_info(st, 63, 1, 1000000);
  491. ipmovie->video_stream_index = st->index;
  492. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  493. st->codec->codec_id = AV_CODEC_ID_INTERPLAY_VIDEO;
  494. st->codec->codec_tag = 0; /* no fourcc */
  495. st->codec->width = ipmovie->video_width;
  496. st->codec->height = ipmovie->video_height;
  497. st->codec->bits_per_coded_sample = ipmovie->video_bpp;
  498. if (ipmovie->audio_type) {
  499. st = avformat_new_stream(s, NULL);
  500. if (!st)
  501. return AVERROR(ENOMEM);
  502. avpriv_set_pts_info(st, 32, 1, ipmovie->audio_sample_rate);
  503. ipmovie->audio_stream_index = st->index;
  504. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  505. st->codec->codec_id = ipmovie->audio_type;
  506. st->codec->codec_tag = 0; /* no tag */
  507. st->codec->channels = ipmovie->audio_channels;
  508. st->codec->channel_layout = st->codec->channels == 1 ? AV_CH_LAYOUT_MONO :
  509. AV_CH_LAYOUT_STEREO;
  510. st->codec->sample_rate = ipmovie->audio_sample_rate;
  511. st->codec->bits_per_coded_sample = ipmovie->audio_bits;
  512. st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
  513. st->codec->bits_per_coded_sample;
  514. if (st->codec->codec_id == AV_CODEC_ID_INTERPLAY_DPCM)
  515. st->codec->bit_rate /= 2;
  516. st->codec->block_align = st->codec->channels * st->codec->bits_per_coded_sample;
  517. }
  518. return 0;
  519. }
  520. static int ipmovie_read_packet(AVFormatContext *s,
  521. AVPacket *pkt)
  522. {
  523. IPMVEContext *ipmovie = s->priv_data;
  524. AVIOContext *pb = s->pb;
  525. int ret;
  526. ret = process_ipmovie_chunk(ipmovie, pb, pkt);
  527. if (ret == CHUNK_BAD)
  528. ret = AVERROR_INVALIDDATA;
  529. else if (ret == CHUNK_EOF)
  530. ret = AVERROR(EIO);
  531. else if (ret == CHUNK_NOMEM)
  532. ret = AVERROR(ENOMEM);
  533. else if (ret == CHUNK_VIDEO)
  534. ret = 0;
  535. else
  536. ret = -1;
  537. return ret;
  538. }
  539. AVInputFormat ff_ipmovie_demuxer = {
  540. .name = "ipmovie",
  541. .long_name = NULL_IF_CONFIG_SMALL("Interplay MVE"),
  542. .priv_data_size = sizeof(IPMVEContext),
  543. .read_probe = ipmovie_probe,
  544. .read_header = ipmovie_read_header,
  545. .read_packet = ipmovie_read_packet,
  546. };