2 * Interplay MVE File Demuxer
3 * Copyright (c) 2003 The ffmpeg Project
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * Interplay MVE file demuxer
23 * by Mike Melanson (melanson@pcisys.net)
24 * For more information regarding the Interplay MVE file format, visit:
25 * http://www.pcisys.net/~melanson/codecs/
26 * The aforementioned site also contains a command line utility for parsing
27 * IP MVE files so that you can get a good idea of the typical structure of
28 * such files. This demuxer is not the best example to use if you are trying
29 * to write your own as it uses a rather roundabout approach for splitting
30 * up and sending out the chunks.
35 /* debugging support: #define DEBUG_IPMOVIE as non-zero to see extremely
36 * verbose information about the demux process */
37 #define DEBUG_IPMOVIE 0
40 #define debug_ipmovie printf
42 static inline void debug_ipmovie(const char *format, ...) { }
45 #define IPMOVIE_SIGNATURE "Interplay MVE File\x1A\0"
46 #define IPMOVIE_SIGNATURE_SIZE 20
47 #define CHUNK_PREAMBLE_SIZE 4
48 #define OPCODE_PREAMBLE_SIZE 4
50 #define CHUNK_INIT_AUDIO 0x0000
51 #define CHUNK_AUDIO_ONLY 0x0001
52 #define CHUNK_INIT_VIDEO 0x0002
53 #define CHUNK_VIDEO 0x0003
54 #define CHUNK_SHUTDOWN 0x0004
55 #define CHUNK_END 0x0005
56 /* these last types are used internally */
57 #define CHUNK_DONE 0xFFFC
58 #define CHUNK_NOMEM 0xFFFD
59 #define CHUNK_EOF 0xFFFE
60 #define CHUNK_BAD 0xFFFF
62 #define OPCODE_END_OF_STREAM 0x00
63 #define OPCODE_END_OF_CHUNK 0x01
64 #define OPCODE_CREATE_TIMER 0x02
65 #define OPCODE_INIT_AUDIO_BUFFERS 0x03
66 #define OPCODE_START_STOP_AUDIO 0x04
67 #define OPCODE_INIT_VIDEO_BUFFERS 0x05
68 #define OPCODE_UNKNOWN_06 0x06
69 #define OPCODE_SEND_BUFFER 0x07
70 #define OPCODE_AUDIO_FRAME 0x08
71 #define OPCODE_SILENCE_FRAME 0x09
72 #define OPCODE_INIT_VIDEO_MODE 0x0A
73 #define OPCODE_CREATE_GRADIENT 0x0B
74 #define OPCODE_SET_PALETTE 0x0C
75 #define OPCODE_SET_PALETTE_COMPRESSED 0x0D
76 #define OPCODE_UNKNOWN_0E 0x0E
77 #define OPCODE_SET_DECODING_MAP 0x0F
78 #define OPCODE_UNKNOWN_10 0x10
79 #define OPCODE_VIDEO_DATA 0x11
80 #define OPCODE_UNKNOWN_12 0x12
81 #define OPCODE_UNKNOWN_13 0x13
82 #define OPCODE_UNKNOWN_14 0x14
83 #define OPCODE_UNKNOWN_15 0x15
85 #define PALETTE_COUNT 256
87 typedef struct IPMVEContext {
95 unsigned int video_width;
96 unsigned int video_height;
99 unsigned int audio_bits;
100 unsigned int audio_channels;
101 unsigned int audio_sample_rate;
102 unsigned int audio_type;
103 unsigned int audio_frame_count;
105 int video_stream_index;
106 int audio_stream_index;
108 offset_t audio_chunk_offset;
109 int audio_chunk_size;
110 offset_t video_chunk_offset;
111 int video_chunk_size;
112 offset_t decode_map_chunk_offset;
113 int decode_map_chunk_size;
115 offset_t next_chunk_offset;
117 AVPaletteControl palette_control;
121 static int load_ipmovie_packet(IPMVEContext *s, ByteIOContext *pb,
125 int64_t audio_pts = 0;
127 if (s->audio_chunk_offset) {
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;
135 url_fseek(pb, s->audio_chunk_offset, SEEK_SET);
136 s->audio_chunk_offset = 0;
138 /* figure out the audio pts */
140 audio_pts *= s->audio_frame_count;
141 audio_pts /= s->audio_sample_rate;
143 if (av_new_packet(pkt, s->audio_chunk_size))
146 pkt->stream_index = s->audio_stream_index;
147 pkt->pts = audio_pts;
148 if (get_buffer(pb, pkt->data, s->audio_chunk_size) !=
149 s->audio_chunk_size) {
154 /* audio frame maintenance */
155 if (s->audio_type != CODEC_ID_INTERPLAY_DPCM)
156 s->audio_frame_count +=
157 (s->audio_chunk_size / s->audio_channels / (s->audio_bits / 8));
159 s->audio_frame_count +=
160 (s->audio_chunk_size - 6) / s->audio_channels;
162 debug_ipmovie("sending audio frame with pts %lld (%d audio frames)\n",
163 audio_pts, s->audio_frame_count);
165 chunk_type = CHUNK_VIDEO;
167 } else if (s->decode_map_chunk_offset) {
169 /* send both the decode map and the video data together */
171 if (av_new_packet(pkt, s->decode_map_chunk_size + s->video_chunk_size))
174 url_fseek(pb, s->decode_map_chunk_offset, SEEK_SET);
175 s->decode_map_chunk_offset = 0;
177 if (get_buffer(pb, pkt->data, s->decode_map_chunk_size) !=
178 s->decode_map_chunk_size) {
183 url_fseek(pb, s->video_chunk_offset, SEEK_SET);
184 s->video_chunk_offset = 0;
186 if (get_buffer(pb, pkt->data + s->decode_map_chunk_size,
187 s->video_chunk_size) != s->video_chunk_size) {
192 pkt->stream_index = s->video_stream_index;
193 pkt->pts = s->video_pts;
195 debug_ipmovie("sending video frame with pts %lld\n",
198 s->video_pts += s->frame_pts_inc;
200 chunk_type = CHUNK_VIDEO;
204 url_fseek(pb, s->next_chunk_offset, SEEK_SET);
205 chunk_type = CHUNK_DONE;
212 /* This function loads and processes a single chunk in an IP movie file.
213 * It returns the type of chunk that was processed. */
214 static int process_ipmovie_chunk(IPMVEContext *s, ByteIOContext *pb,
217 unsigned char chunk_preamble[CHUNK_PREAMBLE_SIZE];
220 unsigned char opcode_preamble[OPCODE_PREAMBLE_SIZE];
221 unsigned char opcode_type;
222 unsigned char opcode_version;
224 unsigned char scratch[1024];
226 int first_color, last_color;
228 unsigned char r, g, b;
230 /* see if there are any pending packets */
231 chunk_type = load_ipmovie_packet(s, pb, pkt);
232 if ((chunk_type == CHUNK_VIDEO) && (chunk_type != CHUNK_DONE))
235 /* read the next chunk, wherever the file happens to be pointing */
238 if (get_buffer(pb, chunk_preamble, CHUNK_PREAMBLE_SIZE) !=
241 chunk_size = LE_16(&chunk_preamble[0]);
242 chunk_type = LE_16(&chunk_preamble[2]);
244 debug_ipmovie("chunk type 0x%04X, 0x%04X bytes: ", chunk_type, chunk_size);
246 switch (chunk_type) {
248 case CHUNK_INIT_AUDIO:
249 debug_ipmovie("initialize audio\n");
252 case CHUNK_AUDIO_ONLY:
253 debug_ipmovie("audio only\n");
256 case CHUNK_INIT_VIDEO:
257 debug_ipmovie("initialize video\n");
261 debug_ipmovie("video (and audio)\n");
265 debug_ipmovie("shutdown\n");
269 debug_ipmovie("end\n");
273 debug_ipmovie("invalid chunk\n");
274 chunk_type = CHUNK_BAD;
279 while ((chunk_size > 0) && (chunk_type != CHUNK_BAD)) {
281 /* read the next chunk, wherever the file happens to be pointing */
283 chunk_type = CHUNK_EOF;
286 if (get_buffer(pb, opcode_preamble, CHUNK_PREAMBLE_SIZE) !=
287 CHUNK_PREAMBLE_SIZE) {
288 chunk_type = CHUNK_BAD;
292 opcode_size = LE_16(&opcode_preamble[0]);
293 opcode_type = opcode_preamble[2];
294 opcode_version = opcode_preamble[3];
296 chunk_size -= OPCODE_PREAMBLE_SIZE;
297 chunk_size -= opcode_size;
298 if (chunk_size < 0) {
299 debug_ipmovie("chunk_size countdown just went negative\n");
300 chunk_type = CHUNK_BAD;
304 debug_ipmovie(" opcode type %02X, version %d, 0x%04X bytes: ",
305 opcode_type, opcode_version, opcode_size);
306 switch (opcode_type) {
308 case OPCODE_END_OF_STREAM:
309 debug_ipmovie("end of stream\n");
310 url_fseek(pb, opcode_size, SEEK_CUR);
313 case OPCODE_END_OF_CHUNK:
314 debug_ipmovie("end of chunk\n");
315 url_fseek(pb, opcode_size, SEEK_CUR);
318 case OPCODE_CREATE_TIMER:
319 debug_ipmovie("create timer\n");
320 if ((opcode_version > 0) || (opcode_size > 6)) {
321 debug_ipmovie("bad create_timer opcode\n");
322 chunk_type = CHUNK_BAD;
325 if (get_buffer(pb, scratch, opcode_size) !=
327 chunk_type = CHUNK_BAD;
330 s->fps = 1000000.0 / (LE_32(&scratch[0]) * LE_16(&scratch[4]));
331 s->frame_pts_inc = 90000 / s->fps;
332 debug_ipmovie(" %.2f frames/second (timer div = %d, subdiv = %d)\n",
333 s->fps, LE_32(&scratch[0]), LE_16(&scratch[4]));
336 case OPCODE_INIT_AUDIO_BUFFERS:
337 debug_ipmovie("initialize audio buffers\n");
338 if ((opcode_version > 1) || (opcode_size > 10)) {
339 debug_ipmovie("bad init_audio_buffers opcode\n");
340 chunk_type = CHUNK_BAD;
343 if (get_buffer(pb, scratch, opcode_size) !=
345 chunk_type = CHUNK_BAD;
348 s->audio_sample_rate = LE_16(&scratch[4]);
349 audio_flags = LE_16(&scratch[2]);
350 /* bit 0 of the flags: 0 = mono, 1 = stereo */
351 s->audio_channels = (audio_flags & 1) + 1;
352 /* bit 1 of the flags: 0 = 8 bit, 1 = 16 bit */
353 s->audio_bits = (((audio_flags >> 1) & 1) + 1) * 8;
354 /* bit 2 indicates compressed audio in version 1 opcode */
355 if ((opcode_version == 1) && (audio_flags & 0x4))
356 s->audio_type = CODEC_ID_INTERPLAY_DPCM;
357 else if (s->audio_bits == 16)
358 s->audio_type = CODEC_ID_PCM_S16LE;
360 s->audio_type = CODEC_ID_PCM_U8;
361 debug_ipmovie("audio: %d bits, %d Hz, %s, %s format\n",
363 s->audio_sample_rate,
364 (s->audio_channels == 2) ? "stereo" : "mono",
365 (s->audio_type == CODEC_ID_INTERPLAY_DPCM) ?
366 "Interplay audio" : "PCM");
369 case OPCODE_START_STOP_AUDIO:
370 debug_ipmovie("start/stop audio\n");
371 url_fseek(pb, opcode_size, SEEK_CUR);
374 case OPCODE_INIT_VIDEO_BUFFERS:
375 debug_ipmovie("initialize video buffers\n");
376 if ((opcode_version > 2) || (opcode_size > 8)) {
377 debug_ipmovie("bad init_video_buffers opcode\n");
378 chunk_type = CHUNK_BAD;
381 if (get_buffer(pb, scratch, opcode_size) !=
383 chunk_type = CHUNK_BAD;
386 s->video_width = LE_16(&scratch[0]) * 8;
387 s->video_height = LE_16(&scratch[2]) * 8;
388 debug_ipmovie("video resolution: %d x %d\n",
389 s->video_width, s->video_height);
392 case OPCODE_UNKNOWN_06:
393 case OPCODE_UNKNOWN_0E:
394 case OPCODE_UNKNOWN_10:
395 case OPCODE_UNKNOWN_12:
396 case OPCODE_UNKNOWN_13:
397 case OPCODE_UNKNOWN_14:
398 case OPCODE_UNKNOWN_15:
399 debug_ipmovie("unknown (but documented) opcode %02X\n", opcode_type);
400 url_fseek(pb, opcode_size, SEEK_CUR);
403 case OPCODE_SEND_BUFFER:
404 debug_ipmovie("send buffer\n");
405 url_fseek(pb, opcode_size, SEEK_CUR);
408 case OPCODE_AUDIO_FRAME:
409 debug_ipmovie("audio frame\n");
411 /* log position and move on for now */
412 s->audio_chunk_offset = url_ftell(pb);
413 s->audio_chunk_size = opcode_size;
414 url_fseek(pb, opcode_size, SEEK_CUR);
417 case OPCODE_SILENCE_FRAME:
418 debug_ipmovie("silence frame\n");
419 url_fseek(pb, opcode_size, SEEK_CUR);
422 case OPCODE_INIT_VIDEO_MODE:
423 debug_ipmovie("initialize video mode\n");
424 url_fseek(pb, opcode_size, SEEK_CUR);
427 case OPCODE_CREATE_GRADIENT:
428 debug_ipmovie("create gradient\n");
429 url_fseek(pb, opcode_size, SEEK_CUR);
432 case OPCODE_SET_PALETTE:
433 debug_ipmovie("set palette\n");
434 /* check for the logical maximum palette size
435 * (3 * 256 + 4 bytes) */
436 if (opcode_size > 0x304) {
437 debug_ipmovie("demux_ipmovie: set_palette opcode too large\n");
438 chunk_type = CHUNK_BAD;
441 if (get_buffer(pb, scratch, opcode_size) != opcode_size) {
442 chunk_type = CHUNK_BAD;
446 /* load the palette into internal data structure */
447 first_color = LE_16(&scratch[0]);
448 last_color = first_color + LE_16(&scratch[2]) - 1;
449 /* sanity check (since they are 16 bit values) */
450 if ((first_color > 0xFF) || (last_color > 0xFF)) {
451 debug_ipmovie("demux_ipmovie: set_palette indices out of range (%d -> %d)\n",
452 first_color, last_color);
453 chunk_type = CHUNK_BAD;
456 j = 4; /* offset of first palette data */
457 for (i = first_color; i <= last_color; i++) {
458 /* the palette is stored as a 6-bit VGA palette, thus each
459 * component is shifted up to a 8-bit range */
460 r = scratch[j++] * 4;
461 g = scratch[j++] * 4;
462 b = scratch[j++] * 4;
463 s->palette_control.palette[i] = (r << 16) | (g << 8) | (b);
465 /* indicate a palette change */
466 s->palette_control.palette_changed = 1;
469 case OPCODE_SET_PALETTE_COMPRESSED:
470 debug_ipmovie("set palette compressed\n");
471 url_fseek(pb, opcode_size, SEEK_CUR);
474 case OPCODE_SET_DECODING_MAP:
475 debug_ipmovie("set decoding map\n");
477 /* log position and move on for now */
478 s->decode_map_chunk_offset = url_ftell(pb);
479 s->decode_map_chunk_size = opcode_size;
480 url_fseek(pb, opcode_size, SEEK_CUR);
483 case OPCODE_VIDEO_DATA:
484 debug_ipmovie("set video data\n");
486 /* log position and move on for now */
487 s->video_chunk_offset = url_ftell(pb);
488 s->video_chunk_size = opcode_size;
489 url_fseek(pb, opcode_size, SEEK_CUR);
493 debug_ipmovie("*** unknown opcode type\n");
494 chunk_type = CHUNK_BAD;
500 /* make a note of where the stream is sitting */
501 s->next_chunk_offset = url_ftell(pb);
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);
510 static int ipmovie_probe(AVProbeData *p)
512 if (p->buf_size < IPMOVIE_SIGNATURE_SIZE)
514 if (strncmp(p->buf, IPMOVIE_SIGNATURE, IPMOVIE_SIGNATURE_SIZE) != 0)
517 return AVPROBE_SCORE_MAX;
520 static int ipmovie_read_header(AVFormatContext *s,
521 AVFormatParameters *ap)
523 IPMVEContext *ipmovie = (IPMVEContext *)s->priv_data;
524 ByteIOContext *pb = &s->pb;
527 unsigned char chunk_preamble[CHUNK_PREAMBLE_SIZE];
530 /* initialize private context members */
531 ipmovie->video_pts = ipmovie->audio_frame_count = 0;
532 ipmovie->audio_chunk_offset = ipmovie->video_chunk_offset =
533 ipmovie->decode_map_chunk_offset = 0;
535 /* on the first read, this will position the stream at the first chunk */
536 ipmovie->next_chunk_offset = IPMOVIE_SIGNATURE_SIZE + 6;
538 /* process the first chunk which should be CHUNK_INIT_VIDEO */
539 if (process_ipmovie_chunk(ipmovie, pb, &pkt) != CHUNK_INIT_VIDEO)
540 return AVERROR_INVALIDDATA;
542 /* peek ahead to the next chunk-- if it is an init audio chunk, process
543 * it; if it is the first video chunk, this is a silent file */
544 if (get_buffer(pb, chunk_preamble, CHUNK_PREAMBLE_SIZE) !=
547 chunk_type = LE_16(&chunk_preamble[2]);
548 url_fseek(pb, -CHUNK_PREAMBLE_SIZE, SEEK_CUR);
550 if (chunk_type == CHUNK_VIDEO)
551 ipmovie->audio_type = 0; /* no audio */
552 else if (process_ipmovie_chunk(ipmovie, pb, &pkt) != CHUNK_INIT_AUDIO)
553 return AVERROR_INVALIDDATA;
555 /* initialize the stream decoders */
556 st = av_new_stream(s, 0);
558 return AVERROR_NOMEM;
559 av_set_pts_info(st, 33, 1, 90000);
560 ipmovie->video_stream_index = st->index;
561 st->codec.codec_type = CODEC_TYPE_VIDEO;
562 st->codec.codec_id = CODEC_ID_INTERPLAY_VIDEO;
563 st->codec.codec_tag = 0; /* no fourcc */
564 st->codec.width = ipmovie->video_width;
565 st->codec.height = ipmovie->video_height;
567 /* palette considerations */
568 st->codec.palctrl = &ipmovie->palette_control;
570 if (ipmovie->audio_type) {
571 st = av_new_stream(s, 0);
573 return AVERROR_NOMEM;
574 av_set_pts_info(st, 33, 1, 90000);
575 ipmovie->audio_stream_index = st->index;
576 st->codec.codec_type = CODEC_TYPE_AUDIO;
577 st->codec.codec_id = ipmovie->audio_type;
578 st->codec.codec_tag = 0; /* no tag */
579 st->codec.channels = ipmovie->audio_channels;
580 st->codec.sample_rate = ipmovie->audio_sample_rate;
581 st->codec.bits_per_sample = ipmovie->audio_bits;
582 st->codec.bit_rate = st->codec.channels * st->codec.sample_rate *
583 st->codec.bits_per_sample;
584 if (st->codec.codec_id == CODEC_ID_INTERPLAY_DPCM)
585 st->codec.bit_rate /= 2;
586 st->codec.block_align = st->codec.channels * st->codec.bits_per_sample;
592 static int ipmovie_read_packet(AVFormatContext *s,
595 IPMVEContext *ipmovie = (IPMVEContext *)s->priv_data;
596 ByteIOContext *pb = &s->pb;
599 ret = process_ipmovie_chunk(ipmovie, pb, pkt);
600 if (ret == CHUNK_BAD)
601 ret = AVERROR_INVALIDDATA;
602 else if (ret == CHUNK_EOF)
604 else if (ret == CHUNK_NOMEM)
612 static int ipmovie_read_close(AVFormatContext *s)
614 // IPMVEContext *ipmovie = (IPMVEContext *)s->priv_data;
619 static AVInputFormat ipmovie_iformat = {
621 "Interplay MVE format",
622 sizeof(IPMVEContext),
629 int ipmovie_init(void)
631 av_register_input_format(&ipmovie_iformat);