cook: Remove unused debug functions.
[libav.git] / libavformat / ipmovie.c
CommitLineData
3ef8be2b
MM
1/*
2 * Interplay MVE File Demuxer
3 * Copyright (c) 2003 The ffmpeg Project
4 *
2912e87a 5 * This file is part of Libav.
b78e7197 6 *
2912e87a 7 * Libav 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 *
2912e87a 12 * Libav 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
2912e87a 18 * License along with Libav; 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/**
ba87f080 23 * @file
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 95
8badb23e 96 unsigned int video_bpp;
3ef8be2b
MM
97 unsigned int video_width;
98 unsigned int video_height;
99 int64_t video_pts;
2d8591c2
KS
100 uint32_t palette[256];
101 int has_palette;
3ef8be2b
MM
102
103 unsigned int audio_bits;
104 unsigned int audio_channels;
105 unsigned int audio_sample_rate;
1c53290b 106 enum CodecID audio_type;
3ef8be2b
MM
107 unsigned int audio_frame_count;
108
109 int video_stream_index;
110 int audio_stream_index;
111
bc5c918e 112 int64_t audio_chunk_offset;
3ef8be2b 113 int audio_chunk_size;
bc5c918e 114 int64_t video_chunk_offset;
3ef8be2b 115 int video_chunk_size;
bc5c918e 116 int64_t decode_map_chunk_offset;
3ef8be2b
MM
117 int decode_map_chunk_size;
118
bc5c918e 119 int64_t next_chunk_offset;
3ef8be2b
MM
120
121} IPMVEContext;
122
ae628ec1 123static int load_ipmovie_packet(IPMVEContext *s, AVIOContext *pb,
3ef8be2b
MM
124 AVPacket *pkt) {
125
126 int chunk_type;
3ef8be2b
MM
127
128 if (s->audio_chunk_offset) {
129
b59ebf62
MM
130 /* adjust for PCM audio by skipping chunk header */
131 if (s->audio_type != CODEC_ID_INTERPLAY_DPCM) {
132 s->audio_chunk_offset += 6;
133 s->audio_chunk_size -= 6;
134 }
135
6b4aa5da 136 avio_seek(pb, s->audio_chunk_offset, SEEK_SET);
3ef8be2b
MM
137 s->audio_chunk_offset = 0;
138
2692067a
MN
139 if (s->audio_chunk_size != av_get_packet(pb, pkt, s->audio_chunk_size))
140 return CHUNK_EOF;
3ef8be2b
MM
141
142 pkt->stream_index = s->audio_stream_index;
03c03afd 143 pkt->pts = s->audio_frame_count;
3ef8be2b
MM
144
145 /* audio frame maintenance */
146 if (s->audio_type != CODEC_ID_INTERPLAY_DPCM)
147 s->audio_frame_count +=
148 (s->audio_chunk_size / s->audio_channels / (s->audio_bits / 8));
149 else
150 s->audio_frame_count +=
151 (s->audio_chunk_size - 6) / s->audio_channels;
152
949b1a13 153 debug_ipmovie("sending audio frame with pts %"PRId64" (%d audio frames)\n",
03c03afd 154 pkt->pts, s->audio_frame_count);
3ef8be2b
MM
155
156 chunk_type = CHUNK_VIDEO;
157
158 } else if (s->decode_map_chunk_offset) {
159
b76c707a 160 /* send both the decode map and the video data together */
3ef8be2b 161
b76c707a 162 if (av_new_packet(pkt, s->decode_map_chunk_size + s->video_chunk_size))
3ef8be2b
MM
163 return CHUNK_NOMEM;
164
2d8591c2
KS
165 if (s->has_palette) {
166 uint8_t *pal;
167
168 pal = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE,
169 AVPALETTE_SIZE);
170 if (pal) {
171 memcpy(pal, s->palette, AVPALETTE_SIZE);
172 s->has_palette = 0;
173 }
174 }
175
2692067a 176 pkt->pos= s->decode_map_chunk_offset;
6b4aa5da 177 avio_seek(pb, s->decode_map_chunk_offset, SEEK_SET);
b76c707a
MM
178 s->decode_map_chunk_offset = 0;
179
b7effd4e 180 if (avio_read(pb, pkt->data, s->decode_map_chunk_size) !=
3ef8be2b
MM
181 s->decode_map_chunk_size) {
182 av_free_packet(pkt);
183 return CHUNK_EOF;
184 }
185
6b4aa5da 186 avio_seek(pb, s->video_chunk_offset, SEEK_SET);
3ef8be2b
MM
187 s->video_chunk_offset = 0;
188
b7effd4e 189 if (avio_read(pb, pkt->data + s->decode_map_chunk_size,
b76c707a 190 s->video_chunk_size) != s->video_chunk_size) {
3ef8be2b
MM
191 av_free_packet(pkt);
192 return CHUNK_EOF;
193 }
194
b76c707a
MM
195 pkt->stream_index = s->video_stream_index;
196 pkt->pts = s->video_pts;
197
949b1a13 198 debug_ipmovie("sending video frame with pts %"PRId64"\n",
dda61423
MM
199 pkt->pts);
200
3ef8be2b
MM
201 s->video_pts += s->frame_pts_inc;
202
203 chunk_type = CHUNK_VIDEO;
204
205 } else {
206
6b4aa5da 207 avio_seek(pb, s->next_chunk_offset, SEEK_SET);
3ef8be2b
MM
208 chunk_type = CHUNK_DONE;
209
210 }
211
212 return chunk_type;
213}
214
215/* This function loads and processes a single chunk in an IP movie file.
216 * It returns the type of chunk that was processed. */
ae628ec1 217static int process_ipmovie_chunk(IPMVEContext *s, AVIOContext *pb,
3ef8be2b
MM
218 AVPacket *pkt)
219{
220 unsigned char chunk_preamble[CHUNK_PREAMBLE_SIZE];
221 int chunk_type;
222 int chunk_size;
223 unsigned char opcode_preamble[OPCODE_PREAMBLE_SIZE];
224 unsigned char opcode_type;
225 unsigned char opcode_version;
226 int opcode_size;
227 unsigned char scratch[1024];
b76c707a 228 int i, j;
3ef8be2b
MM
229 int first_color, last_color;
230 int audio_flags;
2a2bbcb0 231 unsigned char r, g, b;
3ef8be2b
MM
232
233 /* see if there are any pending packets */
234 chunk_type = load_ipmovie_packet(s, pb, pkt);
49c8132b 235 if (chunk_type != CHUNK_DONE)
3ef8be2b
MM
236 return chunk_type;
237
238 /* read the next chunk, wherever the file happens to be pointing */
66e5b1df 239 if (pb->eof_reached)
3ef8be2b 240 return CHUNK_EOF;
b7effd4e 241 if (avio_read(pb, chunk_preamble, CHUNK_PREAMBLE_SIZE) !=
3ef8be2b
MM
242 CHUNK_PREAMBLE_SIZE)
243 return CHUNK_BAD;
fead30d4
AB
244 chunk_size = AV_RL16(&chunk_preamble[0]);
245 chunk_type = AV_RL16(&chunk_preamble[2]);
3ef8be2b
MM
246
247 debug_ipmovie("chunk type 0x%04X, 0x%04X bytes: ", chunk_type, chunk_size);
248
249 switch (chunk_type) {
250
251 case CHUNK_INIT_AUDIO:
252 debug_ipmovie("initialize audio\n");
253 break;
254
255 case CHUNK_AUDIO_ONLY:
256 debug_ipmovie("audio only\n");
257 break;
258
259 case CHUNK_INIT_VIDEO:
260 debug_ipmovie("initialize video\n");
261 break;
262
263 case CHUNK_VIDEO:
264 debug_ipmovie("video (and audio)\n");
265 break;
266
267 case CHUNK_SHUTDOWN:
268 debug_ipmovie("shutdown\n");
269 break;
270
271 case CHUNK_END:
272 debug_ipmovie("end\n");
273 break;
274
275 default:
276 debug_ipmovie("invalid chunk\n");
277 chunk_type = CHUNK_BAD;
278 break;
279
280 }
281
282 while ((chunk_size > 0) && (chunk_type != CHUNK_BAD)) {
283
284 /* read the next chunk, wherever the file happens to be pointing */
66e5b1df 285 if (pb->eof_reached) {
3ef8be2b
MM
286 chunk_type = CHUNK_EOF;
287 break;
288 }
b7effd4e 289 if (avio_read(pb, opcode_preamble, CHUNK_PREAMBLE_SIZE) !=
3ef8be2b
MM
290 CHUNK_PREAMBLE_SIZE) {
291 chunk_type = CHUNK_BAD;
292 break;
293 }
294
fead30d4 295 opcode_size = AV_RL16(&opcode_preamble[0]);
3ef8be2b
MM
296 opcode_type = opcode_preamble[2];
297 opcode_version = opcode_preamble[3];
298
299 chunk_size -= OPCODE_PREAMBLE_SIZE;
300 chunk_size -= opcode_size;
301 if (chunk_size < 0) {
302 debug_ipmovie("chunk_size countdown just went negative\n");
303 chunk_type = CHUNK_BAD;
304 break;
305 }
306
307 debug_ipmovie(" opcode type %02X, version %d, 0x%04X bytes: ",
308 opcode_type, opcode_version, opcode_size);
309 switch (opcode_type) {
310
311 case OPCODE_END_OF_STREAM:
312 debug_ipmovie("end of stream\n");
45a8a02a 313 avio_skip(pb, opcode_size);
3ef8be2b
MM
314 break;
315
316 case OPCODE_END_OF_CHUNK:
317 debug_ipmovie("end of chunk\n");
45a8a02a 318 avio_skip(pb, opcode_size);
3ef8be2b
MM
319 break;
320
321 case OPCODE_CREATE_TIMER:
322 debug_ipmovie("create timer\n");
323 if ((opcode_version > 0) || (opcode_size > 6)) {
324 debug_ipmovie("bad create_timer opcode\n");
325 chunk_type = CHUNK_BAD;
326 break;
327 }
b7effd4e 328 if (avio_read(pb, scratch, opcode_size) !=
3ef8be2b
MM
329 opcode_size) {
330 chunk_type = CHUNK_BAD;
331 break;
332 }
03c03afd 333 s->frame_pts_inc = ((uint64_t)AV_RL32(&scratch[0])) * AV_RL16(&scratch[4]);
dda61423 334 debug_ipmovie(" %.2f frames/second (timer div = %d, subdiv = %d)\n",
03c03afd 335 1000000.0/s->frame_pts_inc, AV_RL32(&scratch[0]), AV_RL16(&scratch[4]));
3ef8be2b
MM
336 break;
337
338 case OPCODE_INIT_AUDIO_BUFFERS:
339 debug_ipmovie("initialize audio buffers\n");
340 if ((opcode_version > 1) || (opcode_size > 10)) {
341 debug_ipmovie("bad init_audio_buffers opcode\n");
342 chunk_type = CHUNK_BAD;
343 break;
344 }
b7effd4e 345 if (avio_read(pb, scratch, opcode_size) !=
3ef8be2b
MM
346 opcode_size) {
347 chunk_type = CHUNK_BAD;
348 break;
349 }
fead30d4
AB
350 s->audio_sample_rate = AV_RL16(&scratch[4]);
351 audio_flags = AV_RL16(&scratch[2]);
3ef8be2b
MM
352 /* bit 0 of the flags: 0 = mono, 1 = stereo */
353 s->audio_channels = (audio_flags & 1) + 1;
354 /* bit 1 of the flags: 0 = 8 bit, 1 = 16 bit */
355 s->audio_bits = (((audio_flags >> 1) & 1) + 1) * 8;
356 /* bit 2 indicates compressed audio in version 1 opcode */
357 if ((opcode_version == 1) && (audio_flags & 0x4))
358 s->audio_type = CODEC_ID_INTERPLAY_DPCM;
359 else if (s->audio_bits == 16)
360 s->audio_type = CODEC_ID_PCM_S16LE;
361 else
362 s->audio_type = CODEC_ID_PCM_U8;
363 debug_ipmovie("audio: %d bits, %d Hz, %s, %s format\n",
364 s->audio_bits,
365 s->audio_sample_rate,
366 (s->audio_channels == 2) ? "stereo" : "mono",
115329f1 367 (s->audio_type == CODEC_ID_INTERPLAY_DPCM) ?
3ef8be2b
MM
368 "Interplay audio" : "PCM");
369 break;
370
371 case OPCODE_START_STOP_AUDIO:
372 debug_ipmovie("start/stop audio\n");
45a8a02a 373 avio_skip(pb, opcode_size);
3ef8be2b
MM
374 break;
375
376 case OPCODE_INIT_VIDEO_BUFFERS:
377 debug_ipmovie("initialize video buffers\n");
378 if ((opcode_version > 2) || (opcode_size > 8)) {
379 debug_ipmovie("bad init_video_buffers opcode\n");
380 chunk_type = CHUNK_BAD;
381 break;
382 }
b7effd4e 383 if (avio_read(pb, scratch, opcode_size) !=
3ef8be2b
MM
384 opcode_size) {
385 chunk_type = CHUNK_BAD;
386 break;
387 }
fead30d4
AB
388 s->video_width = AV_RL16(&scratch[0]) * 8;
389 s->video_height = AV_RL16(&scratch[2]) * 8;
8badb23e
KS
390 if (opcode_version < 2 || !AV_RL16(&scratch[6])) {
391 s->video_bpp = 8;
392 } else {
393 s->video_bpp = 16;
394 }
3ef8be2b
MM
395 debug_ipmovie("video resolution: %d x %d\n",
396 s->video_width, s->video_height);
397 break;
398
399 case OPCODE_UNKNOWN_06:
400 case OPCODE_UNKNOWN_0E:
401 case OPCODE_UNKNOWN_10:
402 case OPCODE_UNKNOWN_12:
403 case OPCODE_UNKNOWN_13:
404 case OPCODE_UNKNOWN_14:
405 case OPCODE_UNKNOWN_15:
406 debug_ipmovie("unknown (but documented) opcode %02X\n", opcode_type);
45a8a02a 407 avio_skip(pb, opcode_size);
3ef8be2b
MM
408 break;
409
410 case OPCODE_SEND_BUFFER:
411 debug_ipmovie("send buffer\n");
45a8a02a 412 avio_skip(pb, opcode_size);
3ef8be2b
MM
413 break;
414
415 case OPCODE_AUDIO_FRAME:
416 debug_ipmovie("audio frame\n");
417
418 /* log position and move on for now */
a2704c97 419 s->audio_chunk_offset = avio_tell(pb);
3ef8be2b 420 s->audio_chunk_size = opcode_size;
45a8a02a 421 avio_skip(pb, opcode_size);
3ef8be2b
MM
422 break;
423
424 case OPCODE_SILENCE_FRAME:
425 debug_ipmovie("silence frame\n");
45a8a02a 426 avio_skip(pb, opcode_size);
3ef8be2b
MM
427 break;
428
429 case OPCODE_INIT_VIDEO_MODE:
430 debug_ipmovie("initialize video mode\n");
45a8a02a 431 avio_skip(pb, opcode_size);
3ef8be2b
MM
432 break;
433
434 case OPCODE_CREATE_GRADIENT:
435 debug_ipmovie("create gradient\n");
45a8a02a 436 avio_skip(pb, opcode_size);
3ef8be2b
MM
437 break;
438
439 case OPCODE_SET_PALETTE:
440 debug_ipmovie("set palette\n");
441 /* check for the logical maximum palette size
442 * (3 * 256 + 4 bytes) */
443 if (opcode_size > 0x304) {
444 debug_ipmovie("demux_ipmovie: set_palette opcode too large\n");
445 chunk_type = CHUNK_BAD;
446 break;
447 }
b7effd4e 448 if (avio_read(pb, scratch, opcode_size) != opcode_size) {
3ef8be2b
MM
449 chunk_type = CHUNK_BAD;
450 break;
451 }
452
453 /* load the palette into internal data structure */
fead30d4
AB
454 first_color = AV_RL16(&scratch[0]);
455 last_color = first_color + AV_RL16(&scratch[2]) - 1;
3ef8be2b
MM
456 /* sanity check (since they are 16 bit values) */
457 if ((first_color > 0xFF) || (last_color > 0xFF)) {
f4433de9 458 debug_ipmovie("demux_ipmovie: set_palette indexes out of range (%d -> %d)\n",
3ef8be2b
MM
459 first_color, last_color);
460 chunk_type = CHUNK_BAD;
461 break;
462 }
463 j = 4; /* offset of first palette data */
3ef8be2b 464 for (i = first_color; i <= last_color; i++) {
b76c707a
MM
465 /* the palette is stored as a 6-bit VGA palette, thus each
466 * component is shifted up to a 8-bit range */
2a2bbcb0
MM
467 r = scratch[j++] * 4;
468 g = scratch[j++] * 4;
469 b = scratch[j++] * 4;
2d8591c2 470 s->palette[i] = (r << 16) | (g << 8) | (b);
3ef8be2b 471 }
2d8591c2 472 s->has_palette = 1;
3ef8be2b
MM
473 break;
474
475 case OPCODE_SET_PALETTE_COMPRESSED:
476 debug_ipmovie("set palette compressed\n");
45a8a02a 477 avio_skip(pb, opcode_size);
3ef8be2b
MM
478 break;
479
480 case OPCODE_SET_DECODING_MAP:
481 debug_ipmovie("set decoding map\n");
482
483 /* log position and move on for now */
a2704c97 484 s->decode_map_chunk_offset = avio_tell(pb);
3ef8be2b 485 s->decode_map_chunk_size = opcode_size;
45a8a02a 486 avio_skip(pb, opcode_size);
3ef8be2b
MM
487 break;
488
489 case OPCODE_VIDEO_DATA:
490 debug_ipmovie("set video data\n");
491
492 /* log position and move on for now */
a2704c97 493 s->video_chunk_offset = avio_tell(pb);
3ef8be2b 494 s->video_chunk_size = opcode_size;
45a8a02a 495 avio_skip(pb, opcode_size);
3ef8be2b
MM
496 break;
497
498 default:
499 debug_ipmovie("*** unknown opcode type\n");
500 chunk_type = CHUNK_BAD;
501 break;
502
503 }
504 }
505
506 /* make a note of where the stream is sitting */
a2704c97 507 s->next_chunk_offset = avio_tell(pb);
3ef8be2b
MM
508
509 /* dispatch the first of any pending packets */
510 if ((chunk_type == CHUNK_VIDEO) || (chunk_type == CHUNK_AUDIO_ONLY))
511 chunk_type = load_ipmovie_packet(s, pb, pkt);
512
513 return chunk_type;
514}
515
78bfe6c3
RD
516static const char signature[] = "Interplay MVE File\x1A\0\x1A";
517
3ef8be2b
MM
518static int ipmovie_probe(AVProbeData *p)
519{
61e8efd3
RD
520 uint8_t *b = p->buf;
521 uint8_t *b_end = p->buf + p->buf_size - sizeof(signature);
522 do {
523 if (memcmp(b++, signature, sizeof(signature)) == 0)
524 return AVPROBE_SCORE_MAX;
525 } while (b < b_end);
526
527 return 0;
3ef8be2b
MM
528}
529
530static int ipmovie_read_header(AVFormatContext *s,
531 AVFormatParameters *ap)
532{
e4141433 533 IPMVEContext *ipmovie = s->priv_data;
ae628ec1 534 AVIOContext *pb = s->pb;
3ef8be2b
MM
535 AVPacket pkt;
536 AVStream *st;
dda61423
MM
537 unsigned char chunk_preamble[CHUNK_PREAMBLE_SIZE];
538 int chunk_type;
61e8efd3
RD
539 uint8_t signature_buffer[sizeof(signature)];
540
b7effd4e 541 avio_read(pb, signature_buffer, sizeof(signature_buffer));
61e8efd3
RD
542 while (memcmp(signature_buffer, signature, sizeof(signature))) {
543 memmove(signature_buffer, signature_buffer + 1, sizeof(signature_buffer) - 1);
b7effd4e 544 signature_buffer[sizeof(signature_buffer) - 1] = avio_r8(pb);
66e5b1df 545 if (pb->eof_reached)
61e8efd3
RD
546 return AVERROR_EOF;
547 }
3ef8be2b
MM
548 /* initialize private context members */
549 ipmovie->video_pts = ipmovie->audio_frame_count = 0;
550 ipmovie->audio_chunk_offset = ipmovie->video_chunk_offset =
551 ipmovie->decode_map_chunk_offset = 0;
552
553 /* on the first read, this will position the stream at the first chunk */
a2704c97 554 ipmovie->next_chunk_offset = avio_tell(pb) + 4;
3ef8be2b
MM
555
556 /* process the first chunk which should be CHUNK_INIT_VIDEO */
557 if (process_ipmovie_chunk(ipmovie, pb, &pkt) != CHUNK_INIT_VIDEO)
558 return AVERROR_INVALIDDATA;
559
dda61423
MM
560 /* peek ahead to the next chunk-- if it is an init audio chunk, process
561 * it; if it is the first video chunk, this is a silent file */
b7effd4e 562 if (avio_read(pb, chunk_preamble, CHUNK_PREAMBLE_SIZE) !=
dda61423 563 CHUNK_PREAMBLE_SIZE)
6f3e0b21 564 return AVERROR(EIO);
fead30d4 565 chunk_type = AV_RL16(&chunk_preamble[2]);
6b4aa5da 566 avio_seek(pb, -CHUNK_PREAMBLE_SIZE, SEEK_CUR);
dda61423
MM
567
568 if (chunk_type == CHUNK_VIDEO)
1c53290b 569 ipmovie->audio_type = CODEC_ID_NONE; /* no audio */
dda61423 570 else if (process_ipmovie_chunk(ipmovie, pb, &pkt) != CHUNK_INIT_AUDIO)
3ef8be2b
MM
571 return AVERROR_INVALIDDATA;
572
3ef8be2b
MM
573 /* initialize the stream decoders */
574 st = av_new_stream(s, 0);
575 if (!st)
769e10f0 576 return AVERROR(ENOMEM);
03c03afd 577 av_set_pts_info(st, 63, 1, 1000000);
3ef8be2b 578 ipmovie->video_stream_index = st->index;
72415b2a 579 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
01f4895c
MN
580 st->codec->codec_id = CODEC_ID_INTERPLAY_VIDEO;
581 st->codec->codec_tag = 0; /* no fourcc */
582 st->codec->width = ipmovie->video_width;
583 st->codec->height = ipmovie->video_height;
8badb23e 584 st->codec->bits_per_coded_sample = ipmovie->video_bpp;
3ef8be2b 585
dda61423
MM
586 if (ipmovie->audio_type) {
587 st = av_new_stream(s, 0);
588 if (!st)
769e10f0 589 return AVERROR(ENOMEM);
03c03afd 590 av_set_pts_info(st, 32, 1, ipmovie->audio_sample_rate);
dda61423 591 ipmovie->audio_stream_index = st->index;
72415b2a 592 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
01f4895c
MN
593 st->codec->codec_id = ipmovie->audio_type;
594 st->codec->codec_tag = 0; /* no tag */
595 st->codec->channels = ipmovie->audio_channels;
596 st->codec->sample_rate = ipmovie->audio_sample_rate;
dd1c8f3e 597 st->codec->bits_per_coded_sample = ipmovie->audio_bits;
01f4895c 598 st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
dd1c8f3e 599 st->codec->bits_per_coded_sample;
01f4895c
MN
600 if (st->codec->codec_id == CODEC_ID_INTERPLAY_DPCM)
601 st->codec->bit_rate /= 2;
dd1c8f3e 602 st->codec->block_align = st->codec->channels * st->codec->bits_per_coded_sample;
dda61423 603 }
3ef8be2b
MM
604
605 return 0;
606}
607
608static int ipmovie_read_packet(AVFormatContext *s,
609 AVPacket *pkt)
610{
e4141433 611 IPMVEContext *ipmovie = s->priv_data;
ae628ec1 612 AVIOContext *pb = s->pb;
3ef8be2b
MM
613 int ret;
614
615 ret = process_ipmovie_chunk(ipmovie, pb, pkt);
616 if (ret == CHUNK_BAD)
617 ret = AVERROR_INVALIDDATA;
618 else if (ret == CHUNK_EOF)
6f3e0b21 619 ret = AVERROR(EIO);
3ef8be2b 620 else if (ret == CHUNK_NOMEM)
769e10f0 621 ret = AVERROR(ENOMEM);
49c8132b 622 else if (ret == CHUNK_VIDEO)
3ef8be2b 623 ret = 0;
49c8132b
MN
624 else
625 ret = -1;
3ef8be2b
MM
626
627 return ret;
628}
629
c6610a21 630AVInputFormat ff_ipmovie_demuxer = {
3ef8be2b 631 "ipmovie",
bde15e74 632 NULL_IF_CONFIG_SMALL("Interplay MVE format"),
3ef8be2b
MM
633 sizeof(IPMVEContext),
634 ipmovie_probe,
635 ipmovie_read_header,
636 ipmovie_read_packet,
3ef8be2b 637};