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 (s->audio_chunk_size != av_get_packet(pb, pkt, s->audio_chunk_size))
146 pkt->stream_index = s->audio_stream_index;
147 pkt->pts = audio_pts;
149 /* audio frame maintenance */
150 if (s->audio_type != CODEC_ID_INTERPLAY_DPCM)
151 s->audio_frame_count +=
152 (s->audio_chunk_size / s->audio_channels / (s->audio_bits / 8));
154 s->audio_frame_count +=
155 (s->audio_chunk_size - 6) / s->audio_channels;
157 debug_ipmovie("sending audio frame with pts %lld (%d audio frames)\n",
158 audio_pts, s->audio_frame_count);
160 chunk_type = CHUNK_VIDEO;
162 } else if (s->decode_map_chunk_offset) {
164 /* send both the decode map and the video data together */
166 if (av_new_packet(pkt, s->decode_map_chunk_size + s->video_chunk_size))
169 pkt->pos= s->decode_map_chunk_offset;
170 url_fseek(pb, s->decode_map_chunk_offset, SEEK_SET);
171 s->decode_map_chunk_offset = 0;
173 if (get_buffer(pb, pkt->data, s->decode_map_chunk_size) !=
174 s->decode_map_chunk_size) {
179 url_fseek(pb, s->video_chunk_offset, SEEK_SET);
180 s->video_chunk_offset = 0;
182 if (get_buffer(pb, pkt->data + s->decode_map_chunk_size,
183 s->video_chunk_size) != s->video_chunk_size) {
188 pkt->stream_index = s->video_stream_index;
189 pkt->pts = s->video_pts;
191 debug_ipmovie("sending video frame with pts %lld\n",
194 s->video_pts += s->frame_pts_inc;
196 chunk_type = CHUNK_VIDEO;
200 url_fseek(pb, s->next_chunk_offset, SEEK_SET);
201 chunk_type = CHUNK_DONE;
208 /* This function loads and processes a single chunk in an IP movie file.
209 * It returns the type of chunk that was processed. */
210 static int process_ipmovie_chunk(IPMVEContext *s, ByteIOContext *pb,
213 unsigned char chunk_preamble[CHUNK_PREAMBLE_SIZE];
216 unsigned char opcode_preamble[OPCODE_PREAMBLE_SIZE];
217 unsigned char opcode_type;
218 unsigned char opcode_version;
220 unsigned char scratch[1024];
222 int first_color, last_color;
224 unsigned char r, g, b;
226 /* see if there are any pending packets */
227 chunk_type = load_ipmovie_packet(s, pb, pkt);
228 if ((chunk_type == CHUNK_VIDEO) && (chunk_type != CHUNK_DONE))
231 /* read the next chunk, wherever the file happens to be pointing */
234 if (get_buffer(pb, chunk_preamble, CHUNK_PREAMBLE_SIZE) !=
237 chunk_size = LE_16(&chunk_preamble[0]);
238 chunk_type = LE_16(&chunk_preamble[2]);
240 debug_ipmovie("chunk type 0x%04X, 0x%04X bytes: ", chunk_type, chunk_size);
242 switch (chunk_type) {
244 case CHUNK_INIT_AUDIO:
245 debug_ipmovie("initialize audio\n");
248 case CHUNK_AUDIO_ONLY:
249 debug_ipmovie("audio only\n");
252 case CHUNK_INIT_VIDEO:
253 debug_ipmovie("initialize video\n");
257 debug_ipmovie("video (and audio)\n");
261 debug_ipmovie("shutdown\n");
265 debug_ipmovie("end\n");
269 debug_ipmovie("invalid chunk\n");
270 chunk_type = CHUNK_BAD;
275 while ((chunk_size > 0) && (chunk_type != CHUNK_BAD)) {
277 /* read the next chunk, wherever the file happens to be pointing */
279 chunk_type = CHUNK_EOF;
282 if (get_buffer(pb, opcode_preamble, CHUNK_PREAMBLE_SIZE) !=
283 CHUNK_PREAMBLE_SIZE) {
284 chunk_type = CHUNK_BAD;
288 opcode_size = LE_16(&opcode_preamble[0]);
289 opcode_type = opcode_preamble[2];
290 opcode_version = opcode_preamble[3];
292 chunk_size -= OPCODE_PREAMBLE_SIZE;
293 chunk_size -= opcode_size;
294 if (chunk_size < 0) {
295 debug_ipmovie("chunk_size countdown just went negative\n");
296 chunk_type = CHUNK_BAD;
300 debug_ipmovie(" opcode type %02X, version %d, 0x%04X bytes: ",
301 opcode_type, opcode_version, opcode_size);
302 switch (opcode_type) {
304 case OPCODE_END_OF_STREAM:
305 debug_ipmovie("end of stream\n");
306 url_fseek(pb, opcode_size, SEEK_CUR);
309 case OPCODE_END_OF_CHUNK:
310 debug_ipmovie("end of chunk\n");
311 url_fseek(pb, opcode_size, SEEK_CUR);
314 case OPCODE_CREATE_TIMER:
315 debug_ipmovie("create timer\n");
316 if ((opcode_version > 0) || (opcode_size > 6)) {
317 debug_ipmovie("bad create_timer opcode\n");
318 chunk_type = CHUNK_BAD;
321 if (get_buffer(pb, scratch, opcode_size) !=
323 chunk_type = CHUNK_BAD;
326 s->fps = 1000000.0 / (LE_32(&scratch[0]) * LE_16(&scratch[4]));
327 s->frame_pts_inc = 90000 / s->fps;
328 debug_ipmovie(" %.2f frames/second (timer div = %d, subdiv = %d)\n",
329 s->fps, LE_32(&scratch[0]), LE_16(&scratch[4]));
332 case OPCODE_INIT_AUDIO_BUFFERS:
333 debug_ipmovie("initialize audio buffers\n");
334 if ((opcode_version > 1) || (opcode_size > 10)) {
335 debug_ipmovie("bad init_audio_buffers opcode\n");
336 chunk_type = CHUNK_BAD;
339 if (get_buffer(pb, scratch, opcode_size) !=
341 chunk_type = CHUNK_BAD;
344 s->audio_sample_rate = LE_16(&scratch[4]);
345 audio_flags = LE_16(&scratch[2]);
346 /* bit 0 of the flags: 0 = mono, 1 = stereo */
347 s->audio_channels = (audio_flags & 1) + 1;
348 /* bit 1 of the flags: 0 = 8 bit, 1 = 16 bit */
349 s->audio_bits = (((audio_flags >> 1) & 1) + 1) * 8;
350 /* bit 2 indicates compressed audio in version 1 opcode */
351 if ((opcode_version == 1) && (audio_flags & 0x4))
352 s->audio_type = CODEC_ID_INTERPLAY_DPCM;
353 else if (s->audio_bits == 16)
354 s->audio_type = CODEC_ID_PCM_S16LE;
356 s->audio_type = CODEC_ID_PCM_U8;
357 debug_ipmovie("audio: %d bits, %d Hz, %s, %s format\n",
359 s->audio_sample_rate,
360 (s->audio_channels == 2) ? "stereo" : "mono",
361 (s->audio_type == CODEC_ID_INTERPLAY_DPCM) ?
362 "Interplay audio" : "PCM");
365 case OPCODE_START_STOP_AUDIO:
366 debug_ipmovie("start/stop audio\n");
367 url_fseek(pb, opcode_size, SEEK_CUR);
370 case OPCODE_INIT_VIDEO_BUFFERS:
371 debug_ipmovie("initialize video buffers\n");
372 if ((opcode_version > 2) || (opcode_size > 8)) {
373 debug_ipmovie("bad init_video_buffers opcode\n");
374 chunk_type = CHUNK_BAD;
377 if (get_buffer(pb, scratch, opcode_size) !=
379 chunk_type = CHUNK_BAD;
382 s->video_width = LE_16(&scratch[0]) * 8;
383 s->video_height = LE_16(&scratch[2]) * 8;
384 debug_ipmovie("video resolution: %d x %d\n",
385 s->video_width, s->video_height);
388 case OPCODE_UNKNOWN_06:
389 case OPCODE_UNKNOWN_0E:
390 case OPCODE_UNKNOWN_10:
391 case OPCODE_UNKNOWN_12:
392 case OPCODE_UNKNOWN_13:
393 case OPCODE_UNKNOWN_14:
394 case OPCODE_UNKNOWN_15:
395 debug_ipmovie("unknown (but documented) opcode %02X\n", opcode_type);
396 url_fseek(pb, opcode_size, SEEK_CUR);
399 case OPCODE_SEND_BUFFER:
400 debug_ipmovie("send buffer\n");
401 url_fseek(pb, opcode_size, SEEK_CUR);
404 case OPCODE_AUDIO_FRAME:
405 debug_ipmovie("audio frame\n");
407 /* log position and move on for now */
408 s->audio_chunk_offset = url_ftell(pb);
409 s->audio_chunk_size = opcode_size;
410 url_fseek(pb, opcode_size, SEEK_CUR);
413 case OPCODE_SILENCE_FRAME:
414 debug_ipmovie("silence frame\n");
415 url_fseek(pb, opcode_size, SEEK_CUR);
418 case OPCODE_INIT_VIDEO_MODE:
419 debug_ipmovie("initialize video mode\n");
420 url_fseek(pb, opcode_size, SEEK_CUR);
423 case OPCODE_CREATE_GRADIENT:
424 debug_ipmovie("create gradient\n");
425 url_fseek(pb, opcode_size, SEEK_CUR);
428 case OPCODE_SET_PALETTE:
429 debug_ipmovie("set palette\n");
430 /* check for the logical maximum palette size
431 * (3 * 256 + 4 bytes) */
432 if (opcode_size > 0x304) {
433 debug_ipmovie("demux_ipmovie: set_palette opcode too large\n");
434 chunk_type = CHUNK_BAD;
437 if (get_buffer(pb, scratch, opcode_size) != opcode_size) {
438 chunk_type = CHUNK_BAD;
442 /* load the palette into internal data structure */
443 first_color = LE_16(&scratch[0]);
444 last_color = first_color + LE_16(&scratch[2]) - 1;
445 /* sanity check (since they are 16 bit values) */
446 if ((first_color > 0xFF) || (last_color > 0xFF)) {
447 debug_ipmovie("demux_ipmovie: set_palette indices out of range (%d -> %d)\n",
448 first_color, last_color);
449 chunk_type = CHUNK_BAD;
452 j = 4; /* offset of first palette data */
453 for (i = first_color; i <= last_color; i++) {
454 /* the palette is stored as a 6-bit VGA palette, thus each
455 * component is shifted up to a 8-bit range */
456 r = scratch[j++] * 4;
457 g = scratch[j++] * 4;
458 b = scratch[j++] * 4;
459 s->palette_control.palette[i] = (r << 16) | (g << 8) | (b);
461 /* indicate a palette change */
462 s->palette_control.palette_changed = 1;
465 case OPCODE_SET_PALETTE_COMPRESSED:
466 debug_ipmovie("set palette compressed\n");
467 url_fseek(pb, opcode_size, SEEK_CUR);
470 case OPCODE_SET_DECODING_MAP:
471 debug_ipmovie("set decoding map\n");
473 /* log position and move on for now */
474 s->decode_map_chunk_offset = url_ftell(pb);
475 s->decode_map_chunk_size = opcode_size;
476 url_fseek(pb, opcode_size, SEEK_CUR);
479 case OPCODE_VIDEO_DATA:
480 debug_ipmovie("set video data\n");
482 /* log position and move on for now */
483 s->video_chunk_offset = url_ftell(pb);
484 s->video_chunk_size = opcode_size;
485 url_fseek(pb, opcode_size, SEEK_CUR);
489 debug_ipmovie("*** unknown opcode type\n");
490 chunk_type = CHUNK_BAD;
496 /* make a note of where the stream is sitting */
497 s->next_chunk_offset = url_ftell(pb);
499 /* dispatch the first of any pending packets */
500 if ((chunk_type == CHUNK_VIDEO) || (chunk_type == CHUNK_AUDIO_ONLY))
501 chunk_type = load_ipmovie_packet(s, pb, pkt);
506 static int ipmovie_probe(AVProbeData *p)
508 if (p->buf_size < IPMOVIE_SIGNATURE_SIZE)
510 if (strncmp(p->buf, IPMOVIE_SIGNATURE, IPMOVIE_SIGNATURE_SIZE) != 0)
513 return AVPROBE_SCORE_MAX;
516 static int ipmovie_read_header(AVFormatContext *s,
517 AVFormatParameters *ap)
519 IPMVEContext *ipmovie = (IPMVEContext *)s->priv_data;
520 ByteIOContext *pb = &s->pb;
523 unsigned char chunk_preamble[CHUNK_PREAMBLE_SIZE];
526 /* initialize private context members */
527 ipmovie->video_pts = ipmovie->audio_frame_count = 0;
528 ipmovie->audio_chunk_offset = ipmovie->video_chunk_offset =
529 ipmovie->decode_map_chunk_offset = 0;
531 /* on the first read, this will position the stream at the first chunk */
532 ipmovie->next_chunk_offset = IPMOVIE_SIGNATURE_SIZE + 6;
534 /* process the first chunk which should be CHUNK_INIT_VIDEO */
535 if (process_ipmovie_chunk(ipmovie, pb, &pkt) != CHUNK_INIT_VIDEO)
536 return AVERROR_INVALIDDATA;
538 /* peek ahead to the next chunk-- if it is an init audio chunk, process
539 * it; if it is the first video chunk, this is a silent file */
540 if (get_buffer(pb, chunk_preamble, CHUNK_PREAMBLE_SIZE) !=
543 chunk_type = LE_16(&chunk_preamble[2]);
544 url_fseek(pb, -CHUNK_PREAMBLE_SIZE, SEEK_CUR);
546 if (chunk_type == CHUNK_VIDEO)
547 ipmovie->audio_type = 0; /* no audio */
548 else if (process_ipmovie_chunk(ipmovie, pb, &pkt) != CHUNK_INIT_AUDIO)
549 return AVERROR_INVALIDDATA;
551 /* initialize the stream decoders */
552 st = av_new_stream(s, 0);
554 return AVERROR_NOMEM;
555 av_set_pts_info(st, 33, 1, 90000);
556 ipmovie->video_stream_index = st->index;
557 st->codec.codec_type = CODEC_TYPE_VIDEO;
558 st->codec.codec_id = CODEC_ID_INTERPLAY_VIDEO;
559 st->codec.codec_tag = 0; /* no fourcc */
560 st->codec.width = ipmovie->video_width;
561 st->codec.height = ipmovie->video_height;
563 /* palette considerations */
564 st->codec.palctrl = &ipmovie->palette_control;
566 if (ipmovie->audio_type) {
567 st = av_new_stream(s, 0);
569 return AVERROR_NOMEM;
570 av_set_pts_info(st, 33, 1, 90000);
571 ipmovie->audio_stream_index = st->index;
572 st->codec.codec_type = CODEC_TYPE_AUDIO;
573 st->codec.codec_id = ipmovie->audio_type;
574 st->codec.codec_tag = 0; /* no tag */
575 st->codec.channels = ipmovie->audio_channels;
576 st->codec.sample_rate = ipmovie->audio_sample_rate;
577 st->codec.bits_per_sample = ipmovie->audio_bits;
578 st->codec.bit_rate = st->codec.channels * st->codec.sample_rate *
579 st->codec.bits_per_sample;
580 if (st->codec.codec_id == CODEC_ID_INTERPLAY_DPCM)
581 st->codec.bit_rate /= 2;
582 st->codec.block_align = st->codec.channels * st->codec.bits_per_sample;
588 static int ipmovie_read_packet(AVFormatContext *s,
591 IPMVEContext *ipmovie = (IPMVEContext *)s->priv_data;
592 ByteIOContext *pb = &s->pb;
595 ret = process_ipmovie_chunk(ipmovie, pb, pkt);
596 if (ret == CHUNK_BAD)
597 ret = AVERROR_INVALIDDATA;
598 else if (ret == CHUNK_EOF)
600 else if (ret == CHUNK_NOMEM)
608 static int ipmovie_read_close(AVFormatContext *s)
610 // IPMVEContext *ipmovie = (IPMVEContext *)s->priv_data;
615 static AVInputFormat ipmovie_iformat = {
617 "Interplay MVE format",
618 sizeof(IPMVEContext),
625 int ipmovie_init(void)
627 av_register_input_format(&ipmovie_iformat);