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