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, ...) { }
46 #define LE_16(x) ((((uint8_t*)(x))[1] << 8) | ((uint8_t*)(x))[0])
47 #define LE_32(x) ((((uint8_t*)(x))[3] << 24) | \
48 (((uint8_t*)(x))[2] << 16) | \
49 (((uint8_t*)(x))[1] << 8) | \
52 #define IPMOVIE_SIGNATURE "Interplay MVE File\x1A\0"
53 #define IPMOVIE_SIGNATURE_SIZE 20
54 #define CHUNK_PREAMBLE_SIZE 4
55 #define OPCODE_PREAMBLE_SIZE 4
57 #define CHUNK_INIT_AUDIO 0x0000
58 #define CHUNK_AUDIO_ONLY 0x0001
59 #define CHUNK_INIT_VIDEO 0x0002
60 #define CHUNK_VIDEO 0x0003
61 #define CHUNK_SHUTDOWN 0x0004
62 #define CHUNK_END 0x0005
63 /* these last types are used internally */
64 #define CHUNK_DONE 0xFFFC
65 #define CHUNK_NOMEM 0xFFFD
66 #define CHUNK_EOF 0xFFFE
67 #define CHUNK_BAD 0xFFFF
69 #define OPCODE_END_OF_STREAM 0x00
70 #define OPCODE_END_OF_CHUNK 0x01
71 #define OPCODE_CREATE_TIMER 0x02
72 #define OPCODE_INIT_AUDIO_BUFFERS 0x03
73 #define OPCODE_START_STOP_AUDIO 0x04
74 #define OPCODE_INIT_VIDEO_BUFFERS 0x05
75 #define OPCODE_UNKNOWN_06 0x06
76 #define OPCODE_SEND_BUFFER 0x07
77 #define OPCODE_AUDIO_FRAME 0x08
78 #define OPCODE_SILENCE_FRAME 0x09
79 #define OPCODE_INIT_VIDEO_MODE 0x0A
80 #define OPCODE_CREATE_GRADIENT 0x0B
81 #define OPCODE_SET_PALETTE 0x0C
82 #define OPCODE_SET_PALETTE_COMPRESSED 0x0D
83 #define OPCODE_UNKNOWN_0E 0x0E
84 #define OPCODE_SET_DECODING_MAP 0x0F
85 #define OPCODE_UNKNOWN_10 0x10
86 #define OPCODE_VIDEO_DATA 0x11
87 #define OPCODE_UNKNOWN_12 0x12
88 #define OPCODE_UNKNOWN_13 0x13
89 #define OPCODE_UNKNOWN_14 0x14
90 #define OPCODE_UNKNOWN_15 0x15
92 #define PALETTE_COUNT 256
94 typedef struct IPMVEContext {
102 unsigned int video_width;
103 unsigned int video_height;
106 unsigned int audio_bits;
107 unsigned int audio_channels;
108 unsigned int audio_sample_rate;
109 unsigned int audio_type;
110 unsigned int audio_frame_count;
112 int video_stream_index;
113 int audio_stream_index;
115 offset_t audio_chunk_offset;
116 int audio_chunk_size;
117 offset_t video_chunk_offset;
118 int video_chunk_size;
119 offset_t decode_map_chunk_offset;
120 int decode_map_chunk_size;
122 offset_t next_chunk_offset;
124 AVPaletteControl palette_control;
128 static int load_ipmovie_packet(IPMVEContext *s, ByteIOContext *pb,
132 int64_t audio_pts = 0;
134 if (s->audio_chunk_offset) {
136 url_fseek(pb, s->audio_chunk_offset, SEEK_SET);
137 s->audio_chunk_offset = 0;
139 /* figure out the audio pts */
141 audio_pts *= s->audio_frame_count;
142 audio_pts /= s->audio_sample_rate;
144 if (av_new_packet(pkt, s->audio_chunk_size))
147 pkt->stream_index = s->audio_stream_index;
148 pkt->pts = audio_pts;
149 if (get_buffer(pb, pkt->data, s->audio_chunk_size) !=
150 s->audio_chunk_size) {
155 /* audio frame maintenance */
156 if (s->audio_type != CODEC_ID_INTERPLAY_DPCM)
157 s->audio_frame_count +=
158 (s->audio_chunk_size / s->audio_channels / (s->audio_bits / 8));
160 s->audio_frame_count +=
161 (s->audio_chunk_size - 6) / s->audio_channels;
163 debug_ipmovie("sending audio frame with pts %lld (%d audio frames)\n",
164 audio_pts, s->audio_frame_count);
166 chunk_type = CHUNK_VIDEO;
168 } else if (s->decode_map_chunk_offset) {
170 /* send both the decode map and the video data together */
172 if (av_new_packet(pkt, s->decode_map_chunk_size + s->video_chunk_size))
175 url_fseek(pb, s->decode_map_chunk_offset, SEEK_SET);
176 s->decode_map_chunk_offset = 0;
178 if (get_buffer(pb, pkt->data, s->decode_map_chunk_size) !=
179 s->decode_map_chunk_size) {
184 url_fseek(pb, s->video_chunk_offset, SEEK_SET);
185 s->video_chunk_offset = 0;
187 if (get_buffer(pb, pkt->data + s->decode_map_chunk_size,
188 s->video_chunk_size) != s->video_chunk_size) {
193 pkt->stream_index = s->video_stream_index;
194 pkt->pts = s->video_pts;
196 s->video_pts += s->frame_pts_inc;
198 chunk_type = CHUNK_VIDEO;
202 url_fseek(pb, s->next_chunk_offset, SEEK_SET);
203 chunk_type = CHUNK_DONE;
210 /* This function loads and processes a single chunk in an IP movie file.
211 * It returns the type of chunk that was processed. */
212 static int process_ipmovie_chunk(IPMVEContext *s, ByteIOContext *pb,
215 unsigned char chunk_preamble[CHUNK_PREAMBLE_SIZE];
218 unsigned char opcode_preamble[OPCODE_PREAMBLE_SIZE];
219 unsigned char opcode_type;
220 unsigned char opcode_version;
222 unsigned char scratch[1024];
224 int first_color, last_color;
227 /* see if there are any pending packets */
228 chunk_type = load_ipmovie_packet(s, pb, pkt);
229 if ((chunk_type == CHUNK_VIDEO) && (chunk_type != CHUNK_DONE))
232 /* read the next chunk, wherever the file happens to be pointing */
235 if (get_buffer(pb, chunk_preamble, CHUNK_PREAMBLE_SIZE) !=
238 chunk_size = LE_16(&chunk_preamble[0]);
239 chunk_type = LE_16(&chunk_preamble[2]);
241 debug_ipmovie("chunk type 0x%04X, 0x%04X bytes: ", chunk_type, chunk_size);
243 switch (chunk_type) {
245 case CHUNK_INIT_AUDIO:
246 debug_ipmovie("initialize audio\n");
249 case CHUNK_AUDIO_ONLY:
250 debug_ipmovie("audio only\n");
253 case CHUNK_INIT_VIDEO:
254 debug_ipmovie("initialize video\n");
258 debug_ipmovie("video (and audio)\n");
262 debug_ipmovie("shutdown\n");
266 debug_ipmovie("end\n");
270 debug_ipmovie("invalid chunk\n");
271 chunk_type = CHUNK_BAD;
276 while ((chunk_size > 0) && (chunk_type != CHUNK_BAD)) {
278 /* read the next chunk, wherever the file happens to be pointing */
280 chunk_type = CHUNK_EOF;
283 if (get_buffer(pb, opcode_preamble, CHUNK_PREAMBLE_SIZE) !=
284 CHUNK_PREAMBLE_SIZE) {
285 chunk_type = CHUNK_BAD;
289 opcode_size = LE_16(&opcode_preamble[0]);
290 opcode_type = opcode_preamble[2];
291 opcode_version = opcode_preamble[3];
293 chunk_size -= OPCODE_PREAMBLE_SIZE;
294 chunk_size -= opcode_size;
295 if (chunk_size < 0) {
296 debug_ipmovie("chunk_size countdown just went negative\n");
297 chunk_type = CHUNK_BAD;
301 debug_ipmovie(" opcode type %02X, version %d, 0x%04X bytes: ",
302 opcode_type, opcode_version, opcode_size);
303 switch (opcode_type) {
305 case OPCODE_END_OF_STREAM:
306 debug_ipmovie("end of stream\n");
307 url_fseek(pb, opcode_size, SEEK_CUR);
310 case OPCODE_END_OF_CHUNK:
311 debug_ipmovie("end of chunk\n");
312 url_fseek(pb, opcode_size, SEEK_CUR);
315 case OPCODE_CREATE_TIMER:
316 debug_ipmovie("create timer\n");
317 if ((opcode_version > 0) || (opcode_size > 6)) {
318 debug_ipmovie("bad create_timer opcode\n");
319 chunk_type = CHUNK_BAD;
322 if (get_buffer(pb, scratch, opcode_size) !=
324 chunk_type = CHUNK_BAD;
327 s->fps = 1000000 / (LE_32(&scratch[0]) * LE_16(&scratch[4]));
328 s->fps++; /* above calculation usually yields 14.9; we need 15 */
329 s->frame_pts_inc = 90000 / s->fps;
330 debug_ipmovie("%d frames/second (timer div = %d, subdiv = %d)\n",
331 s->fps, LE_32(&scratch[0]), LE_16(&scratch[4]));
334 case OPCODE_INIT_AUDIO_BUFFERS:
335 debug_ipmovie("initialize audio buffers\n");
336 if ((opcode_version > 1) || (opcode_size > 10)) {
337 debug_ipmovie("bad init_audio_buffers opcode\n");
338 chunk_type = CHUNK_BAD;
341 if (get_buffer(pb, scratch, opcode_size) !=
343 chunk_type = CHUNK_BAD;
346 s->audio_sample_rate = LE_16(&scratch[4]);
347 audio_flags = LE_16(&scratch[2]);
348 /* bit 0 of the flags: 0 = mono, 1 = stereo */
349 s->audio_channels = (audio_flags & 1) + 1;
350 /* bit 1 of the flags: 0 = 8 bit, 1 = 16 bit */
351 s->audio_bits = (((audio_flags >> 1) & 1) + 1) * 8;
352 /* bit 2 indicates compressed audio in version 1 opcode */
353 if ((opcode_version == 1) && (audio_flags & 0x4))
354 s->audio_type = CODEC_ID_INTERPLAY_DPCM;
355 else if (s->audio_bits == 16)
356 s->audio_type = CODEC_ID_PCM_S16LE;
358 s->audio_type = CODEC_ID_PCM_U8;
359 debug_ipmovie("audio: %d bits, %d Hz, %s, %s format\n",
361 s->audio_sample_rate,
362 (s->audio_channels == 2) ? "stereo" : "mono",
363 (s->audio_type == CODEC_ID_INTERPLAY_DPCM) ?
364 "Interplay audio" : "PCM");
367 case OPCODE_START_STOP_AUDIO:
368 debug_ipmovie("start/stop audio\n");
369 url_fseek(pb, opcode_size, SEEK_CUR);
372 case OPCODE_INIT_VIDEO_BUFFERS:
373 debug_ipmovie("initialize video buffers\n");
374 if ((opcode_version > 2) || (opcode_size > 8)) {
375 debug_ipmovie("bad init_video_buffers opcode\n");
376 chunk_type = CHUNK_BAD;
379 if (get_buffer(pb, scratch, opcode_size) !=
381 chunk_type = CHUNK_BAD;
384 s->video_width = LE_16(&scratch[0]) * 8;
385 s->video_height = LE_16(&scratch[2]) * 8;
386 debug_ipmovie("video resolution: %d x %d\n",
387 s->video_width, s->video_height);
390 case OPCODE_UNKNOWN_06:
391 case OPCODE_UNKNOWN_0E:
392 case OPCODE_UNKNOWN_10:
393 case OPCODE_UNKNOWN_12:
394 case OPCODE_UNKNOWN_13:
395 case OPCODE_UNKNOWN_14:
396 case OPCODE_UNKNOWN_15:
397 debug_ipmovie("unknown (but documented) opcode %02X\n", opcode_type);
398 url_fseek(pb, opcode_size, SEEK_CUR);
401 case OPCODE_SEND_BUFFER:
402 debug_ipmovie("send buffer\n");
403 url_fseek(pb, opcode_size, SEEK_CUR);
406 case OPCODE_AUDIO_FRAME:
407 debug_ipmovie("audio frame\n");
409 /* log position and move on for now */
410 s->audio_chunk_offset = url_ftell(pb);
411 s->audio_chunk_size = opcode_size;
412 url_fseek(pb, opcode_size, SEEK_CUR);
415 case OPCODE_SILENCE_FRAME:
416 debug_ipmovie("silence frame\n");
417 url_fseek(pb, opcode_size, SEEK_CUR);
420 case OPCODE_INIT_VIDEO_MODE:
421 debug_ipmovie("initialize video mode\n");
422 url_fseek(pb, opcode_size, SEEK_CUR);
425 case OPCODE_CREATE_GRADIENT:
426 debug_ipmovie("create gradient\n");
427 url_fseek(pb, opcode_size, SEEK_CUR);
430 case OPCODE_SET_PALETTE:
431 debug_ipmovie("set palette\n");
432 /* check for the logical maximum palette size
433 * (3 * 256 + 4 bytes) */
434 if (opcode_size > 0x304) {
435 debug_ipmovie("demux_ipmovie: set_palette opcode too large\n");
436 chunk_type = CHUNK_BAD;
439 if (get_buffer(pb, scratch, opcode_size) != opcode_size) {
440 chunk_type = CHUNK_BAD;
444 /* load the palette into internal data structure */
445 first_color = LE_16(&scratch[0]);
446 last_color = LE_16(&scratch[2]);
447 /* sanity check (since they are 16 bit values) */
448 if ((first_color > 0xFF) || (last_color > 0xFF)) {
449 debug_ipmovie("demux_ipmovie: set_palette indices out of range (%d -> %d)\n",
450 first_color, last_color);
451 chunk_type = CHUNK_BAD;
454 j = 4; /* offset of first palette data */
455 for (i = first_color; i <= last_color; i++) {
456 /* the palette is stored as a 6-bit VGA palette, thus each
457 * component is shifted up to a 8-bit range */
458 s->palette_control.palette[i * 3 + 0] = scratch[j++] * 4;
459 s->palette_control.palette[i * 3 + 1] = scratch[j++] * 4;
460 s->palette_control.palette[i * 3 + 2] = scratch[j++] * 4;
462 /* indicate a palette change */
463 s->palette_control.palette_changed = 1;
466 case OPCODE_SET_PALETTE_COMPRESSED:
467 debug_ipmovie("set palette compressed\n");
468 url_fseek(pb, opcode_size, SEEK_CUR);
471 case OPCODE_SET_DECODING_MAP:
472 debug_ipmovie("set decoding map\n");
474 /* log position and move on for now */
475 s->decode_map_chunk_offset = url_ftell(pb);
476 s->decode_map_chunk_size = opcode_size;
477 url_fseek(pb, opcode_size, SEEK_CUR);
480 case OPCODE_VIDEO_DATA:
481 debug_ipmovie("set video data\n");
483 /* log position and move on for now */
484 s->video_chunk_offset = url_ftell(pb);
485 s->video_chunk_size = opcode_size;
486 url_fseek(pb, opcode_size, SEEK_CUR);
490 debug_ipmovie("*** unknown opcode type\n");
491 chunk_type = CHUNK_BAD;
497 /* make a note of where the stream is sitting */
498 s->next_chunk_offset = url_ftell(pb);
500 /* dispatch the first of any pending packets */
501 if ((chunk_type == CHUNK_VIDEO) || (chunk_type == CHUNK_AUDIO_ONLY))
502 chunk_type = load_ipmovie_packet(s, pb, pkt);
507 static int ipmovie_probe(AVProbeData *p)
509 if (p->buf_size < IPMOVIE_SIGNATURE_SIZE)
511 if (strncmp(p->buf, IPMOVIE_SIGNATURE, IPMOVIE_SIGNATURE_SIZE) != 0)
514 return AVPROBE_SCORE_MAX;
517 static int ipmovie_read_header(AVFormatContext *s,
518 AVFormatParameters *ap)
520 IPMVEContext *ipmovie = (IPMVEContext *)s->priv_data;
521 ByteIOContext *pb = &s->pb;
525 /* initialize private context members */
526 ipmovie->video_pts = ipmovie->audio_frame_count = 0;
527 ipmovie->audio_chunk_offset = ipmovie->video_chunk_offset =
528 ipmovie->decode_map_chunk_offset = 0;
530 /* on the first read, this will position the stream at the first chunk */
531 ipmovie->next_chunk_offset = IPMOVIE_SIGNATURE_SIZE + 6;
533 /* process the first chunk which should be CHUNK_INIT_VIDEO */
534 if (process_ipmovie_chunk(ipmovie, pb, &pkt) != CHUNK_INIT_VIDEO)
535 return AVERROR_INVALIDDATA;
537 /* process the next chunk which should be CHUNK_INIT_AUDIO */
538 if (process_ipmovie_chunk(ipmovie, pb, &pkt) != CHUNK_INIT_AUDIO)
539 return AVERROR_INVALIDDATA;
541 /* set the pts reference (1 pts = 1/90000) */
545 /* initialize the stream decoders */
546 st = av_new_stream(s, 0);
548 return AVERROR_NOMEM;
549 ipmovie->video_stream_index = st->index;
550 st->codec.codec_type = CODEC_TYPE_VIDEO;
551 st->codec.codec_id = CODEC_ID_INTERPLAY_VIDEO;
552 st->codec.codec_tag = 0; /* no fourcc */
553 st->codec.width = ipmovie->video_width;
554 st->codec.height = ipmovie->video_height;
556 /* palette considerations */
557 st->codec.extradata_size = sizeof(AVPaletteControl);
558 st->codec.extradata = &ipmovie->palette_control;
560 st = av_new_stream(s, 0);
562 return AVERROR_NOMEM;
563 ipmovie->audio_stream_index = st->index;
564 st->codec.codec_type = CODEC_TYPE_AUDIO;
565 st->codec.codec_id = ipmovie->audio_type;
566 st->codec.codec_tag = 0; /* no tag */
567 st->codec.channels = ipmovie->audio_channels;
568 st->codec.sample_rate = ipmovie->audio_sample_rate;
569 st->codec.bits_per_sample = ipmovie->audio_bits;
570 st->codec.bit_rate = st->codec.channels * st->codec.sample_rate *
571 st->codec.bits_per_sample /
572 (st->codec.codec_id == CODEC_ID_INTERPLAY_DPCM) ? 2 : 1;
573 st->codec.block_align = st->codec.channels * st->codec.bits_per_sample;
578 static int ipmovie_read_packet(AVFormatContext *s,
581 IPMVEContext *ipmovie = (IPMVEContext *)s->priv_data;
582 ByteIOContext *pb = &s->pb;
585 ret = process_ipmovie_chunk(ipmovie, pb, pkt);
586 if (ret == CHUNK_BAD)
587 ret = AVERROR_INVALIDDATA;
588 else if (ret == CHUNK_EOF)
590 else if (ret == CHUNK_NOMEM)
598 static int ipmovie_read_close(AVFormatContext *s)
600 // IPMVEContext *ipmovie = (IPMVEContext *)s->priv_data;
605 static AVInputFormat ipmovie_iformat = {
607 "Interplay MVE format",
608 sizeof(IPMVEContext),
615 int ipmovie_init(void)
617 av_register_input_format(&ipmovie_iformat);