Extend ipmovie signature to match exactly one place in mve files that have a
[libav.git] / libavformat / ipmovie.c
CommitLineData
3ef8be2b
MM
1/*
2 * Interplay MVE File Demuxer
3 * Copyright (c) 2003 The ffmpeg Project
4 *
b78e7197
DB
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
3ef8be2b
MM
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
b78e7197 10 * version 2.1 of the License, or (at your option) any later version.
3ef8be2b 11 *
b78e7197 12 * FFmpeg is distributed in the hope that it will be useful,
3ef8be2b
MM
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
b78e7197 18 * License along with FFmpeg; if not, write to the Free Software
5509bffa 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
3ef8be2b
MM
20 */
21
22/**
bad5537e 23 * @file libavformat/ipmovie.c
3ef8be2b
MM
24 * Interplay MVE file demuxer
25 * by Mike Melanson (melanson@pcisys.net)
26 * For more information regarding the Interplay MVE file format, visit:
27 * http://www.pcisys.net/~melanson/codecs/
28 * The aforementioned site also contains a command line utility for parsing
29 * IP MVE files so that you can get a good idea of the typical structure of
30 * such files. This demuxer is not the best example to use if you are trying
31 * to write your own as it uses a rather roundabout approach for splitting
32 * up and sending out the chunks.
33 */
34
6a5d31ac 35#include "libavutil/intreadwrite.h"
3ef8be2b
MM
36#include "avformat.h"
37
38/* debugging support: #define DEBUG_IPMOVIE as non-zero to see extremely
39 * verbose information about the demux process */
9be653d0 40#define DEBUG_IPMOVIE 0
3ef8be2b
MM
41
42#if DEBUG_IPMOVIE
3bc5f6f9 43#undef printf
3ef8be2b
MM
44#define debug_ipmovie printf
45#else
46static inline void debug_ipmovie(const char *format, ...) { }
47#endif
48
3ef8be2b
MM
49#define CHUNK_PREAMBLE_SIZE 4
50#define OPCODE_PREAMBLE_SIZE 4
51
52#define CHUNK_INIT_AUDIO 0x0000
53#define CHUNK_AUDIO_ONLY 0x0001
54#define CHUNK_INIT_VIDEO 0x0002
55#define CHUNK_VIDEO 0x0003
56#define CHUNK_SHUTDOWN 0x0004
57#define CHUNK_END 0x0005
58/* these last types are used internally */
59#define CHUNK_DONE 0xFFFC
60#define CHUNK_NOMEM 0xFFFD
61#define CHUNK_EOF 0xFFFE
62#define CHUNK_BAD 0xFFFF
63
64#define OPCODE_END_OF_STREAM 0x00
65#define OPCODE_END_OF_CHUNK 0x01
66#define OPCODE_CREATE_TIMER 0x02
67#define OPCODE_INIT_AUDIO_BUFFERS 0x03
68#define OPCODE_START_STOP_AUDIO 0x04
69#define OPCODE_INIT_VIDEO_BUFFERS 0x05
70#define OPCODE_UNKNOWN_06 0x06
71#define OPCODE_SEND_BUFFER 0x07
72#define OPCODE_AUDIO_FRAME 0x08
73#define OPCODE_SILENCE_FRAME 0x09
74#define OPCODE_INIT_VIDEO_MODE 0x0A
75#define OPCODE_CREATE_GRADIENT 0x0B
76#define OPCODE_SET_PALETTE 0x0C
77#define OPCODE_SET_PALETTE_COMPRESSED 0x0D
78#define OPCODE_UNKNOWN_0E 0x0E
79#define OPCODE_SET_DECODING_MAP 0x0F
80#define OPCODE_UNKNOWN_10 0x10
81#define OPCODE_VIDEO_DATA 0x11
82#define OPCODE_UNKNOWN_12 0x12
83#define OPCODE_UNKNOWN_13 0x13
84#define OPCODE_UNKNOWN_14 0x14
85#define OPCODE_UNKNOWN_15 0x15
86
87#define PALETTE_COUNT 256
88
89typedef struct IPMVEContext {
90
91 unsigned char *buf;
92 int buf_size;
93
03c03afd 94 uint64_t frame_pts_inc;
3ef8be2b
MM
95
96 unsigned int video_width;
97 unsigned int video_height;
98 int64_t video_pts;
99
100 unsigned int audio_bits;
101 unsigned int audio_channels;
102 unsigned int audio_sample_rate;
1c53290b 103 enum CodecID audio_type;
3ef8be2b
MM
104 unsigned int audio_frame_count;
105
106 int video_stream_index;
107 int audio_stream_index;
108
bc5c918e 109 int64_t audio_chunk_offset;
3ef8be2b 110 int audio_chunk_size;
bc5c918e 111 int64_t video_chunk_offset;
3ef8be2b 112 int video_chunk_size;
bc5c918e 113 int64_t decode_map_chunk_offset;
3ef8be2b
MM
114 int decode_map_chunk_size;
115
bc5c918e 116 int64_t next_chunk_offset;
3ef8be2b 117
b76c707a
MM
118 AVPaletteControl palette_control;
119
3ef8be2b
MM
120} IPMVEContext;
121
115329f1 122static int load_ipmovie_packet(IPMVEContext *s, ByteIOContext *pb,
3ef8be2b
MM
123 AVPacket *pkt) {
124
125 int chunk_type;
3ef8be2b
MM
126
127 if (s->audio_chunk_offset) {
128
b59ebf62
MM
129 /* adjust for PCM audio by skipping chunk header */
130 if (s->audio_type != CODEC_ID_INTERPLAY_DPCM) {
131 s->audio_chunk_offset += 6;
132 s->audio_chunk_size -= 6;
133 }
134
3ef8be2b
MM
135 url_fseek(pb, s->audio_chunk_offset, SEEK_SET);
136 s->audio_chunk_offset = 0;
137
2692067a
MN
138 if (s->audio_chunk_size != av_get_packet(pb, pkt, s->audio_chunk_size))
139 return CHUNK_EOF;
3ef8be2b
MM
140
141 pkt->stream_index = s->audio_stream_index;
03c03afd 142 pkt->pts = s->audio_frame_count;
3ef8be2b
MM
143
144 /* audio frame maintenance */
145 if (s->audio_type != CODEC_ID_INTERPLAY_DPCM)
146 s->audio_frame_count +=
147 (s->audio_chunk_size / s->audio_channels / (s->audio_bits / 8));
148 else
149 s->audio_frame_count +=
150 (s->audio_chunk_size - 6) / s->audio_channels;
151
949b1a13 152 debug_ipmovie("sending audio frame with pts %"PRId64" (%d audio frames)\n",
03c03afd 153 pkt->pts, s->audio_frame_count);
3ef8be2b
MM
154
155 chunk_type = CHUNK_VIDEO;
156
157 } else if (s->decode_map_chunk_offset) {
158
b76c707a 159 /* send both the decode map and the video data together */
3ef8be2b 160
b76c707a 161 if (av_new_packet(pkt, s->decode_map_chunk_size + s->video_chunk_size))
3ef8be2b
MM
162 return CHUNK_NOMEM;
163
2692067a 164 pkt->pos= s->decode_map_chunk_offset;
b76c707a
MM
165 url_fseek(pb, s->decode_map_chunk_offset, SEEK_SET);
166 s->decode_map_chunk_offset = 0;
167
115329f1 168 if (get_buffer(pb, pkt->data, s->decode_map_chunk_size) !=
3ef8be2b
MM
169 s->decode_map_chunk_size) {
170 av_free_packet(pkt);
171 return CHUNK_EOF;
172 }
173
3ef8be2b
MM
174 url_fseek(pb, s->video_chunk_offset, SEEK_SET);
175 s->video_chunk_offset = 0;
176
b76c707a
MM
177 if (get_buffer(pb, pkt->data + s->decode_map_chunk_size,
178 s->video_chunk_size) != s->video_chunk_size) {
3ef8be2b
MM
179 av_free_packet(pkt);
180 return CHUNK_EOF;
181 }
182
b76c707a
MM
183 pkt->stream_index = s->video_stream_index;
184 pkt->pts = s->video_pts;
185
949b1a13 186 debug_ipmovie("sending video frame with pts %"PRId64"\n",
dda61423
MM
187 pkt->pts);
188
3ef8be2b
MM
189 s->video_pts += s->frame_pts_inc;
190
191 chunk_type = CHUNK_VIDEO;
192
193 } else {
194
195 url_fseek(pb, s->next_chunk_offset, SEEK_SET);
196 chunk_type = CHUNK_DONE;
197
198 }
199
200 return chunk_type;
201}
202
203/* This function loads and processes a single chunk in an IP movie file.
204 * It returns the type of chunk that was processed. */
115329f1 205static int process_ipmovie_chunk(IPMVEContext *s, ByteIOContext *pb,
3ef8be2b
MM
206 AVPacket *pkt)
207{
208 unsigned char chunk_preamble[CHUNK_PREAMBLE_SIZE];
209 int chunk_type;
210 int chunk_size;
211 unsigned char opcode_preamble[OPCODE_PREAMBLE_SIZE];
212 unsigned char opcode_type;
213 unsigned char opcode_version;
214 int opcode_size;
215 unsigned char scratch[1024];
b76c707a 216 int i, j;
3ef8be2b
MM
217 int first_color, last_color;
218 int audio_flags;
2a2bbcb0 219 unsigned char r, g, b;
3ef8be2b
MM
220
221 /* see if there are any pending packets */
222 chunk_type = load_ipmovie_packet(s, pb, pkt);
49c8132b 223 if (chunk_type != CHUNK_DONE)
3ef8be2b
MM
224 return chunk_type;
225
226 /* read the next chunk, wherever the file happens to be pointing */
227 if (url_feof(pb))
228 return CHUNK_EOF;
229 if (get_buffer(pb, chunk_preamble, CHUNK_PREAMBLE_SIZE) !=
230 CHUNK_PREAMBLE_SIZE)
231 return CHUNK_BAD;
fead30d4
AB
232 chunk_size = AV_RL16(&chunk_preamble[0]);
233 chunk_type = AV_RL16(&chunk_preamble[2]);
3ef8be2b
MM
234
235 debug_ipmovie("chunk type 0x%04X, 0x%04X bytes: ", chunk_type, chunk_size);
236
237 switch (chunk_type) {
238
239 case CHUNK_INIT_AUDIO:
240 debug_ipmovie("initialize audio\n");
241 break;
242
243 case CHUNK_AUDIO_ONLY:
244 debug_ipmovie("audio only\n");
245 break;
246
247 case CHUNK_INIT_VIDEO:
248 debug_ipmovie("initialize video\n");
249 break;
250
251 case CHUNK_VIDEO:
252 debug_ipmovie("video (and audio)\n");
253 break;
254
255 case CHUNK_SHUTDOWN:
256 debug_ipmovie("shutdown\n");
257 break;
258
259 case CHUNK_END:
260 debug_ipmovie("end\n");
261 break;
262
263 default:
264 debug_ipmovie("invalid chunk\n");
265 chunk_type = CHUNK_BAD;
266 break;
267
268 }
269
270 while ((chunk_size > 0) && (chunk_type != CHUNK_BAD)) {
271
272 /* read the next chunk, wherever the file happens to be pointing */
273 if (url_feof(pb)) {
274 chunk_type = CHUNK_EOF;
275 break;
276 }
277 if (get_buffer(pb, opcode_preamble, CHUNK_PREAMBLE_SIZE) !=
278 CHUNK_PREAMBLE_SIZE) {
279 chunk_type = CHUNK_BAD;
280 break;
281 }
282
fead30d4 283 opcode_size = AV_RL16(&opcode_preamble[0]);
3ef8be2b
MM
284 opcode_type = opcode_preamble[2];
285 opcode_version = opcode_preamble[3];
286
287 chunk_size -= OPCODE_PREAMBLE_SIZE;
288 chunk_size -= opcode_size;
289 if (chunk_size < 0) {
290 debug_ipmovie("chunk_size countdown just went negative\n");
291 chunk_type = CHUNK_BAD;
292 break;
293 }
294
295 debug_ipmovie(" opcode type %02X, version %d, 0x%04X bytes: ",
296 opcode_type, opcode_version, opcode_size);
297 switch (opcode_type) {
298
299 case OPCODE_END_OF_STREAM:
300 debug_ipmovie("end of stream\n");
301 url_fseek(pb, opcode_size, SEEK_CUR);
302 break;
303
304 case OPCODE_END_OF_CHUNK:
305 debug_ipmovie("end of chunk\n");
306 url_fseek(pb, opcode_size, SEEK_CUR);
307 break;
308
309 case OPCODE_CREATE_TIMER:
310 debug_ipmovie("create timer\n");
311 if ((opcode_version > 0) || (opcode_size > 6)) {
312 debug_ipmovie("bad create_timer opcode\n");
313 chunk_type = CHUNK_BAD;
314 break;
315 }
316 if (get_buffer(pb, scratch, opcode_size) !=
317 opcode_size) {
318 chunk_type = CHUNK_BAD;
319 break;
320 }
03c03afd 321 s->frame_pts_inc = ((uint64_t)AV_RL32(&scratch[0])) * AV_RL16(&scratch[4]);
dda61423 322 debug_ipmovie(" %.2f frames/second (timer div = %d, subdiv = %d)\n",
03c03afd 323 1000000.0/s->frame_pts_inc, AV_RL32(&scratch[0]), AV_RL16(&scratch[4]));
3ef8be2b
MM
324 break;
325
326 case OPCODE_INIT_AUDIO_BUFFERS:
327 debug_ipmovie("initialize audio buffers\n");
328 if ((opcode_version > 1) || (opcode_size > 10)) {
329 debug_ipmovie("bad init_audio_buffers opcode\n");
330 chunk_type = CHUNK_BAD;
331 break;
332 }
333 if (get_buffer(pb, scratch, opcode_size) !=
334 opcode_size) {
335 chunk_type = CHUNK_BAD;
336 break;
337 }
fead30d4
AB
338 s->audio_sample_rate = AV_RL16(&scratch[4]);
339 audio_flags = AV_RL16(&scratch[2]);
3ef8be2b
MM
340 /* bit 0 of the flags: 0 = mono, 1 = stereo */
341 s->audio_channels = (audio_flags & 1) + 1;
342 /* bit 1 of the flags: 0 = 8 bit, 1 = 16 bit */
343 s->audio_bits = (((audio_flags >> 1) & 1) + 1) * 8;
344 /* bit 2 indicates compressed audio in version 1 opcode */
345 if ((opcode_version == 1) && (audio_flags & 0x4))
346 s->audio_type = CODEC_ID_INTERPLAY_DPCM;
347 else if (s->audio_bits == 16)
348 s->audio_type = CODEC_ID_PCM_S16LE;
349 else
350 s->audio_type = CODEC_ID_PCM_U8;
351 debug_ipmovie("audio: %d bits, %d Hz, %s, %s format\n",
352 s->audio_bits,
353 s->audio_sample_rate,
354 (s->audio_channels == 2) ? "stereo" : "mono",
115329f1 355 (s->audio_type == CODEC_ID_INTERPLAY_DPCM) ?
3ef8be2b
MM
356 "Interplay audio" : "PCM");
357 break;
358
359 case OPCODE_START_STOP_AUDIO:
360 debug_ipmovie("start/stop audio\n");
361 url_fseek(pb, opcode_size, SEEK_CUR);
362 break;
363
364 case OPCODE_INIT_VIDEO_BUFFERS:
365 debug_ipmovie("initialize video buffers\n");
366 if ((opcode_version > 2) || (opcode_size > 8)) {
367 debug_ipmovie("bad init_video_buffers opcode\n");
368 chunk_type = CHUNK_BAD;
369 break;
370 }
371 if (get_buffer(pb, scratch, opcode_size) !=
372 opcode_size) {
373 chunk_type = CHUNK_BAD;
374 break;
375 }
fead30d4
AB
376 s->video_width = AV_RL16(&scratch[0]) * 8;
377 s->video_height = AV_RL16(&scratch[2]) * 8;
3ef8be2b
MM
378 debug_ipmovie("video resolution: %d x %d\n",
379 s->video_width, s->video_height);
380 break;
381
382 case OPCODE_UNKNOWN_06:
383 case OPCODE_UNKNOWN_0E:
384 case OPCODE_UNKNOWN_10:
385 case OPCODE_UNKNOWN_12:
386 case OPCODE_UNKNOWN_13:
387 case OPCODE_UNKNOWN_14:
388 case OPCODE_UNKNOWN_15:
389 debug_ipmovie("unknown (but documented) opcode %02X\n", opcode_type);
390 url_fseek(pb, opcode_size, SEEK_CUR);
391 break;
392
393 case OPCODE_SEND_BUFFER:
394 debug_ipmovie("send buffer\n");
395 url_fseek(pb, opcode_size, SEEK_CUR);
396 break;
397
398 case OPCODE_AUDIO_FRAME:
399 debug_ipmovie("audio frame\n");
400
401 /* log position and move on for now */
402 s->audio_chunk_offset = url_ftell(pb);
403 s->audio_chunk_size = opcode_size;
404 url_fseek(pb, opcode_size, SEEK_CUR);
405 break;
406
407 case OPCODE_SILENCE_FRAME:
408 debug_ipmovie("silence frame\n");
409 url_fseek(pb, opcode_size, SEEK_CUR);
410 break;
411
412 case OPCODE_INIT_VIDEO_MODE:
413 debug_ipmovie("initialize video mode\n");
414 url_fseek(pb, opcode_size, SEEK_CUR);
415 break;
416
417 case OPCODE_CREATE_GRADIENT:
418 debug_ipmovie("create gradient\n");
419 url_fseek(pb, opcode_size, SEEK_CUR);
420 break;
421
422 case OPCODE_SET_PALETTE:
423 debug_ipmovie("set palette\n");
424 /* check for the logical maximum palette size
425 * (3 * 256 + 4 bytes) */
426 if (opcode_size > 0x304) {
427 debug_ipmovie("demux_ipmovie: set_palette opcode too large\n");
428 chunk_type = CHUNK_BAD;
429 break;
430 }
431 if (get_buffer(pb, scratch, opcode_size) != opcode_size) {
432 chunk_type = CHUNK_BAD;
433 break;
434 }
435
436 /* load the palette into internal data structure */
fead30d4
AB
437 first_color = AV_RL16(&scratch[0]);
438 last_color = first_color + AV_RL16(&scratch[2]) - 1;
3ef8be2b
MM
439 /* sanity check (since they are 16 bit values) */
440 if ((first_color > 0xFF) || (last_color > 0xFF)) {
f4433de9 441 debug_ipmovie("demux_ipmovie: set_palette indexes out of range (%d -> %d)\n",
3ef8be2b
MM
442 first_color, last_color);
443 chunk_type = CHUNK_BAD;
444 break;
445 }
446 j = 4; /* offset of first palette data */
3ef8be2b 447 for (i = first_color; i <= last_color; i++) {
b76c707a
MM
448 /* the palette is stored as a 6-bit VGA palette, thus each
449 * component is shifted up to a 8-bit range */
2a2bbcb0
MM
450 r = scratch[j++] * 4;
451 g = scratch[j++] * 4;
452 b = scratch[j++] * 4;
453 s->palette_control.palette[i] = (r << 16) | (g << 8) | (b);
3ef8be2b 454 }
b76c707a
MM
455 /* indicate a palette change */
456 s->palette_control.palette_changed = 1;
3ef8be2b
MM
457 break;
458
459 case OPCODE_SET_PALETTE_COMPRESSED:
460 debug_ipmovie("set palette compressed\n");
461 url_fseek(pb, opcode_size, SEEK_CUR);
462 break;
463
464 case OPCODE_SET_DECODING_MAP:
465 debug_ipmovie("set decoding map\n");
466
467 /* log position and move on for now */
468 s->decode_map_chunk_offset = url_ftell(pb);
469 s->decode_map_chunk_size = opcode_size;
470 url_fseek(pb, opcode_size, SEEK_CUR);
471 break;
472
473 case OPCODE_VIDEO_DATA:
474 debug_ipmovie("set video data\n");
475
476 /* log position and move on for now */
477 s->video_chunk_offset = url_ftell(pb);
478 s->video_chunk_size = opcode_size;
479 url_fseek(pb, opcode_size, SEEK_CUR);
480 break;
481
482 default:
483 debug_ipmovie("*** unknown opcode type\n");
484 chunk_type = CHUNK_BAD;
485 break;
486
487 }
488 }
489
490 /* make a note of where the stream is sitting */
491 s->next_chunk_offset = url_ftell(pb);
492
493 /* dispatch the first of any pending packets */
494 if ((chunk_type == CHUNK_VIDEO) || (chunk_type == CHUNK_AUDIO_ONLY))
495 chunk_type = load_ipmovie_packet(s, pb, pkt);
496
497 return chunk_type;
498}
499
78bfe6c3
RD
500static const char signature[] = "Interplay MVE File\x1A\0\x1A";
501
3ef8be2b
MM
502static int ipmovie_probe(AVProbeData *p)
503{
78bfe6c3 504 if (memcmp(p->buf, signature, sizeof(signature)) != 0)
3ef8be2b
MM
505 return 0;
506
507 return AVPROBE_SCORE_MAX;
508}
509
510static int ipmovie_read_header(AVFormatContext *s,
511 AVFormatParameters *ap)
512{
e4141433 513 IPMVEContext *ipmovie = s->priv_data;
899681cd 514 ByteIOContext *pb = s->pb;
3ef8be2b
MM
515 AVPacket pkt;
516 AVStream *st;
dda61423
MM
517 unsigned char chunk_preamble[CHUNK_PREAMBLE_SIZE];
518 int chunk_type;
3ef8be2b
MM
519
520 /* initialize private context members */
521 ipmovie->video_pts = ipmovie->audio_frame_count = 0;
522 ipmovie->audio_chunk_offset = ipmovie->video_chunk_offset =
523 ipmovie->decode_map_chunk_offset = 0;
524
525 /* on the first read, this will position the stream at the first chunk */
78bfe6c3 526 ipmovie->next_chunk_offset = sizeof(signature) + 4;
3ef8be2b
MM
527
528 /* process the first chunk which should be CHUNK_INIT_VIDEO */
529 if (process_ipmovie_chunk(ipmovie, pb, &pkt) != CHUNK_INIT_VIDEO)
530 return AVERROR_INVALIDDATA;
531
dda61423
MM
532 /* peek ahead to the next chunk-- if it is an init audio chunk, process
533 * it; if it is the first video chunk, this is a silent file */
534 if (get_buffer(pb, chunk_preamble, CHUNK_PREAMBLE_SIZE) !=
535 CHUNK_PREAMBLE_SIZE)
6f3e0b21 536 return AVERROR(EIO);
fead30d4 537 chunk_type = AV_RL16(&chunk_preamble[2]);
dda61423
MM
538 url_fseek(pb, -CHUNK_PREAMBLE_SIZE, SEEK_CUR);
539
540 if (chunk_type == CHUNK_VIDEO)
1c53290b 541 ipmovie->audio_type = CODEC_ID_NONE; /* no audio */
dda61423 542 else if (process_ipmovie_chunk(ipmovie, pb, &pkt) != CHUNK_INIT_AUDIO)
3ef8be2b
MM
543 return AVERROR_INVALIDDATA;
544
3ef8be2b
MM
545 /* initialize the stream decoders */
546 st = av_new_stream(s, 0);
547 if (!st)
769e10f0 548 return AVERROR(ENOMEM);
03c03afd 549 av_set_pts_info(st, 63, 1, 1000000);
3ef8be2b 550 ipmovie->video_stream_index = st->index;
01f4895c
MN
551 st->codec->codec_type = CODEC_TYPE_VIDEO;
552 st->codec->codec_id = CODEC_ID_INTERPLAY_VIDEO;
553 st->codec->codec_tag = 0; /* no fourcc */
554 st->codec->width = ipmovie->video_width;
555 st->codec->height = ipmovie->video_height;
3ef8be2b 556
b76c707a 557 /* palette considerations */
01f4895c 558 st->codec->palctrl = &ipmovie->palette_control;
b76c707a 559
dda61423
MM
560 if (ipmovie->audio_type) {
561 st = av_new_stream(s, 0);
562 if (!st)
769e10f0 563 return AVERROR(ENOMEM);
03c03afd 564 av_set_pts_info(st, 32, 1, ipmovie->audio_sample_rate);
dda61423 565 ipmovie->audio_stream_index = st->index;
01f4895c
MN
566 st->codec->codec_type = CODEC_TYPE_AUDIO;
567 st->codec->codec_id = ipmovie->audio_type;
568 st->codec->codec_tag = 0; /* no tag */
569 st->codec->channels = ipmovie->audio_channels;
570 st->codec->sample_rate = ipmovie->audio_sample_rate;
dd1c8f3e 571 st->codec->bits_per_coded_sample = ipmovie->audio_bits;
01f4895c 572 st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
dd1c8f3e 573 st->codec->bits_per_coded_sample;
01f4895c
MN
574 if (st->codec->codec_id == CODEC_ID_INTERPLAY_DPCM)
575 st->codec->bit_rate /= 2;
dd1c8f3e 576 st->codec->block_align = st->codec->channels * st->codec->bits_per_coded_sample;
dda61423 577 }
3ef8be2b
MM
578
579 return 0;
580}
581
582static int ipmovie_read_packet(AVFormatContext *s,
583 AVPacket *pkt)
584{
e4141433 585 IPMVEContext *ipmovie = s->priv_data;
899681cd 586 ByteIOContext *pb = s->pb;
3ef8be2b
MM
587 int ret;
588
589 ret = process_ipmovie_chunk(ipmovie, pb, pkt);
590 if (ret == CHUNK_BAD)
591 ret = AVERROR_INVALIDDATA;
592 else if (ret == CHUNK_EOF)
6f3e0b21 593 ret = AVERROR(EIO);
3ef8be2b 594 else if (ret == CHUNK_NOMEM)
769e10f0 595 ret = AVERROR(ENOMEM);
49c8132b 596 else if (ret == CHUNK_VIDEO)
3ef8be2b 597 ret = 0;
49c8132b
MN
598 else
599 ret = -1;
3ef8be2b
MM
600
601 return ret;
602}
603
ff70e601 604AVInputFormat ipmovie_demuxer = {
3ef8be2b 605 "ipmovie",
bde15e74 606 NULL_IF_CONFIG_SMALL("Interplay MVE format"),
3ef8be2b
MM
607 sizeof(IPMVEContext),
608 ipmovie_probe,
609 ipmovie_read_header,
610 ipmovie_read_packet,
3ef8be2b 611};