ipmovie.c 21 KB

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