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