2 * Interplay MVE File Demuxer
3 * Copyright (c) 2003 The ffmpeg Project
5 * This file is part of Libav.
7 * Libav is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * Libav is distributed in the hope that it will be useful,
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.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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.
35 #include "libavutil/intreadwrite.h"
38 #define CHUNK_PREAMBLE_SIZE 4
39 #define OPCODE_PREAMBLE_SIZE 4
41 #define CHUNK_INIT_AUDIO 0x0000
42 #define CHUNK_AUDIO_ONLY 0x0001
43 #define CHUNK_INIT_VIDEO 0x0002
44 #define CHUNK_VIDEO 0x0003
45 #define CHUNK_SHUTDOWN 0x0004
46 #define CHUNK_END 0x0005
47 /* these last types are used internally */
48 #define CHUNK_DONE 0xFFFC
49 #define CHUNK_NOMEM 0xFFFD
50 #define CHUNK_EOF 0xFFFE
51 #define CHUNK_BAD 0xFFFF
53 #define OPCODE_END_OF_STREAM 0x00
54 #define OPCODE_END_OF_CHUNK 0x01
55 #define OPCODE_CREATE_TIMER 0x02
56 #define OPCODE_INIT_AUDIO_BUFFERS 0x03
57 #define OPCODE_START_STOP_AUDIO 0x04
58 #define OPCODE_INIT_VIDEO_BUFFERS 0x05
59 #define OPCODE_UNKNOWN_06 0x06
60 #define OPCODE_SEND_BUFFER 0x07
61 #define OPCODE_AUDIO_FRAME 0x08
62 #define OPCODE_SILENCE_FRAME 0x09
63 #define OPCODE_INIT_VIDEO_MODE 0x0A
64 #define OPCODE_CREATE_GRADIENT 0x0B
65 #define OPCODE_SET_PALETTE 0x0C
66 #define OPCODE_SET_PALETTE_COMPRESSED 0x0D
67 #define OPCODE_UNKNOWN_0E 0x0E
68 #define OPCODE_SET_DECODING_MAP 0x0F
69 #define OPCODE_UNKNOWN_10 0x10
70 #define OPCODE_VIDEO_DATA 0x11
71 #define OPCODE_UNKNOWN_12 0x12
72 #define OPCODE_UNKNOWN_13 0x13
73 #define OPCODE_UNKNOWN_14 0x14
74 #define OPCODE_UNKNOWN_15 0x15
76 #define PALETTE_COUNT 256
78 typedef struct IPMVEContext {
83 uint64_t frame_pts_inc;
85 unsigned int video_bpp;
86 unsigned int video_width;
87 unsigned int video_height;
89 uint32_t palette[256];
92 unsigned int audio_bits;
93 unsigned int audio_channels;
94 unsigned int audio_sample_rate;
95 enum CodecID audio_type;
96 unsigned int audio_frame_count;
98 int video_stream_index;
99 int audio_stream_index;
101 int64_t audio_chunk_offset;
102 int audio_chunk_size;
103 int64_t video_chunk_offset;
104 int video_chunk_size;
105 int64_t decode_map_chunk_offset;
106 int decode_map_chunk_size;
108 int64_t next_chunk_offset;
112 static int load_ipmovie_packet(IPMVEContext *s, AVIOContext *pb,
117 if (s->audio_chunk_offset) {
119 /* adjust for PCM audio by skipping chunk header */
120 if (s->audio_type != CODEC_ID_INTERPLAY_DPCM) {
121 s->audio_chunk_offset += 6;
122 s->audio_chunk_size -= 6;
125 avio_seek(pb, s->audio_chunk_offset, SEEK_SET);
126 s->audio_chunk_offset = 0;
128 if (s->audio_chunk_size != av_get_packet(pb, pkt, s->audio_chunk_size))
131 pkt->stream_index = s->audio_stream_index;
132 pkt->pts = s->audio_frame_count;
134 /* audio frame maintenance */
135 if (s->audio_type != CODEC_ID_INTERPLAY_DPCM)
136 s->audio_frame_count +=
137 (s->audio_chunk_size / s->audio_channels / (s->audio_bits / 8));
139 s->audio_frame_count +=
140 (s->audio_chunk_size - 6) / s->audio_channels;
142 av_dlog(NULL, "sending audio frame with pts %"PRId64" (%d audio frames)\n",
143 pkt->pts, s->audio_frame_count);
145 chunk_type = CHUNK_VIDEO;
147 } else if (s->decode_map_chunk_offset) {
149 /* send both the decode map and the video data together */
151 if (av_new_packet(pkt, s->decode_map_chunk_size + s->video_chunk_size))
154 if (s->has_palette) {
157 pal = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE,
160 memcpy(pal, s->palette, AVPALETTE_SIZE);
165 pkt->pos= s->decode_map_chunk_offset;
166 avio_seek(pb, s->decode_map_chunk_offset, SEEK_SET);
167 s->decode_map_chunk_offset = 0;
169 if (avio_read(pb, pkt->data, s->decode_map_chunk_size) !=
170 s->decode_map_chunk_size) {
175 avio_seek(pb, s->video_chunk_offset, SEEK_SET);
176 s->video_chunk_offset = 0;
178 if (avio_read(pb, pkt->data + s->decode_map_chunk_size,
179 s->video_chunk_size) != s->video_chunk_size) {
184 pkt->stream_index = s->video_stream_index;
185 pkt->pts = s->video_pts;
187 av_dlog(NULL, "sending video frame with pts %"PRId64"\n", pkt->pts);
189 s->video_pts += s->frame_pts_inc;
191 chunk_type = CHUNK_VIDEO;
195 avio_seek(pb, s->next_chunk_offset, SEEK_SET);
196 chunk_type = CHUNK_DONE;
203 /* This function loads and processes a single chunk in an IP movie file.
204 * It returns the type of chunk that was processed. */
205 static int process_ipmovie_chunk(IPMVEContext *s, AVIOContext *pb,
208 unsigned char chunk_preamble[CHUNK_PREAMBLE_SIZE];
211 unsigned char opcode_preamble[OPCODE_PREAMBLE_SIZE];
212 unsigned char opcode_type;
213 unsigned char opcode_version;
215 unsigned char scratch[1024];
217 int first_color, last_color;
219 unsigned char r, g, b;
221 /* see if there are any pending packets */
222 chunk_type = load_ipmovie_packet(s, pb, pkt);
223 if (chunk_type != CHUNK_DONE)
226 /* read the next chunk, wherever the file happens to be pointing */
229 if (avio_read(pb, chunk_preamble, CHUNK_PREAMBLE_SIZE) !=
232 chunk_size = AV_RL16(&chunk_preamble[0]);
233 chunk_type = AV_RL16(&chunk_preamble[2]);
235 av_dlog(NULL, "chunk type 0x%04X, 0x%04X bytes: ", chunk_type, chunk_size);
237 switch (chunk_type) {
239 case CHUNK_INIT_AUDIO:
240 av_dlog(NULL, "initialize audio\n");
243 case CHUNK_AUDIO_ONLY:
244 av_dlog(NULL, "audio only\n");
247 case CHUNK_INIT_VIDEO:
248 av_dlog(NULL, "initialize video\n");
252 av_dlog(NULL, "video (and audio)\n");
256 av_dlog(NULL, "shutdown\n");
260 av_dlog(NULL, "end\n");
264 av_dlog(NULL, "invalid chunk\n");
265 chunk_type = CHUNK_BAD;
270 while ((chunk_size > 0) && (chunk_type != CHUNK_BAD)) {
272 /* read the next chunk, wherever the file happens to be pointing */
273 if (pb->eof_reached) {
274 chunk_type = CHUNK_EOF;
277 if (avio_read(pb, opcode_preamble, CHUNK_PREAMBLE_SIZE) !=
278 CHUNK_PREAMBLE_SIZE) {
279 chunk_type = CHUNK_BAD;
283 opcode_size = AV_RL16(&opcode_preamble[0]);
284 opcode_type = opcode_preamble[2];
285 opcode_version = opcode_preamble[3];
287 chunk_size -= OPCODE_PREAMBLE_SIZE;
288 chunk_size -= opcode_size;
289 if (chunk_size < 0) {
290 av_dlog(NULL, "chunk_size countdown just went negative\n");
291 chunk_type = CHUNK_BAD;
295 av_dlog(NULL, " opcode type %02X, version %d, 0x%04X bytes: ",
296 opcode_type, opcode_version, opcode_size);
297 switch (opcode_type) {
299 case OPCODE_END_OF_STREAM:
300 av_dlog(NULL, "end of stream\n");
301 avio_skip(pb, opcode_size);
304 case OPCODE_END_OF_CHUNK:
305 av_dlog(NULL, "end of chunk\n");
306 avio_skip(pb, opcode_size);
309 case OPCODE_CREATE_TIMER:
310 av_dlog(NULL, "create timer\n");
311 if ((opcode_version > 0) || (opcode_size > 6)) {
312 av_dlog(NULL, "bad create_timer opcode\n");
313 chunk_type = CHUNK_BAD;
316 if (avio_read(pb, scratch, opcode_size) !=
318 chunk_type = CHUNK_BAD;
321 s->frame_pts_inc = ((uint64_t)AV_RL32(&scratch[0])) * AV_RL16(&scratch[4]);
322 av_dlog(NULL, " %.2f frames/second (timer div = %d, subdiv = %d)\n",
323 1000000.0 / s->frame_pts_inc, AV_RL32(&scratch[0]),
324 AV_RL16(&scratch[4]));
327 case OPCODE_INIT_AUDIO_BUFFERS:
328 av_dlog(NULL, "initialize audio buffers\n");
329 if ((opcode_version > 1) || (opcode_size > 10)) {
330 av_dlog(NULL, "bad init_audio_buffers opcode\n");
331 chunk_type = CHUNK_BAD;
334 if (avio_read(pb, scratch, opcode_size) !=
336 chunk_type = CHUNK_BAD;
339 s->audio_sample_rate = AV_RL16(&scratch[4]);
340 audio_flags = AV_RL16(&scratch[2]);
341 /* bit 0 of the flags: 0 = mono, 1 = stereo */
342 s->audio_channels = (audio_flags & 1) + 1;
343 /* bit 1 of the flags: 0 = 8 bit, 1 = 16 bit */
344 s->audio_bits = (((audio_flags >> 1) & 1) + 1) * 8;
345 /* bit 2 indicates compressed audio in version 1 opcode */
346 if ((opcode_version == 1) && (audio_flags & 0x4))
347 s->audio_type = CODEC_ID_INTERPLAY_DPCM;
348 else if (s->audio_bits == 16)
349 s->audio_type = CODEC_ID_PCM_S16LE;
351 s->audio_type = CODEC_ID_PCM_U8;
352 av_dlog(NULL, "audio: %d bits, %d Hz, %s, %s format\n",
353 s->audio_bits, s->audio_sample_rate,
354 (s->audio_channels == 2) ? "stereo" : "mono",
355 (s->audio_type == CODEC_ID_INTERPLAY_DPCM) ?
356 "Interplay audio" : "PCM");
359 case OPCODE_START_STOP_AUDIO:
360 av_dlog(NULL, "start/stop audio\n");
361 avio_skip(pb, opcode_size);
364 case OPCODE_INIT_VIDEO_BUFFERS:
365 av_dlog(NULL, "initialize video buffers\n");
366 if ((opcode_version > 2) || (opcode_size > 8)) {
367 av_dlog(NULL, "bad init_video_buffers opcode\n");
368 chunk_type = CHUNK_BAD;
371 if (avio_read(pb, scratch, opcode_size) !=
373 chunk_type = CHUNK_BAD;
376 s->video_width = AV_RL16(&scratch[0]) * 8;
377 s->video_height = AV_RL16(&scratch[2]) * 8;
378 if (opcode_version < 2 || !AV_RL16(&scratch[6])) {
383 av_dlog(NULL, "video resolution: %d x %d\n",
384 s->video_width, s->video_height);
387 case OPCODE_UNKNOWN_06:
388 case OPCODE_UNKNOWN_0E:
389 case OPCODE_UNKNOWN_10:
390 case OPCODE_UNKNOWN_12:
391 case OPCODE_UNKNOWN_13:
392 case OPCODE_UNKNOWN_14:
393 case OPCODE_UNKNOWN_15:
394 av_dlog(NULL, "unknown (but documented) opcode %02X\n", opcode_type);
395 avio_skip(pb, opcode_size);
398 case OPCODE_SEND_BUFFER:
399 av_dlog(NULL, "send buffer\n");
400 avio_skip(pb, opcode_size);
403 case OPCODE_AUDIO_FRAME:
404 av_dlog(NULL, "audio frame\n");
406 /* log position and move on for now */
407 s->audio_chunk_offset = avio_tell(pb);
408 s->audio_chunk_size = opcode_size;
409 avio_skip(pb, opcode_size);
412 case OPCODE_SILENCE_FRAME:
413 av_dlog(NULL, "silence frame\n");
414 avio_skip(pb, opcode_size);
417 case OPCODE_INIT_VIDEO_MODE:
418 av_dlog(NULL, "initialize video mode\n");
419 avio_skip(pb, opcode_size);
422 case OPCODE_CREATE_GRADIENT:
423 av_dlog(NULL, "create gradient\n");
424 avio_skip(pb, opcode_size);
427 case OPCODE_SET_PALETTE:
428 av_dlog(NULL, "set palette\n");
429 /* check for the logical maximum palette size
430 * (3 * 256 + 4 bytes) */
431 if (opcode_size > 0x304) {
432 av_dlog(NULL, "demux_ipmovie: set_palette opcode too large\n");
433 chunk_type = CHUNK_BAD;
436 if (avio_read(pb, scratch, opcode_size) != opcode_size) {
437 chunk_type = CHUNK_BAD;
441 /* load the palette into internal data structure */
442 first_color = AV_RL16(&scratch[0]);
443 last_color = first_color + AV_RL16(&scratch[2]) - 1;
444 /* sanity check (since they are 16 bit values) */
445 if ((first_color > 0xFF) || (last_color > 0xFF)) {
446 av_dlog(NULL, "demux_ipmovie: set_palette indexes out of range (%d -> %d)\n",
447 first_color, last_color);
448 chunk_type = CHUNK_BAD;
451 j = 4; /* offset of first palette data */
452 for (i = first_color; i <= last_color; i++) {
453 /* the palette is stored as a 6-bit VGA palette, thus each
454 * component is shifted up to a 8-bit range */
455 r = scratch[j++] * 4;
456 g = scratch[j++] * 4;
457 b = scratch[j++] * 4;
458 s->palette[i] = (r << 16) | (g << 8) | (b);
463 case OPCODE_SET_PALETTE_COMPRESSED:
464 av_dlog(NULL, "set palette compressed\n");
465 avio_skip(pb, opcode_size);
468 case OPCODE_SET_DECODING_MAP:
469 av_dlog(NULL, "set decoding map\n");
471 /* log position and move on for now */
472 s->decode_map_chunk_offset = avio_tell(pb);
473 s->decode_map_chunk_size = opcode_size;
474 avio_skip(pb, opcode_size);
477 case OPCODE_VIDEO_DATA:
478 av_dlog(NULL, "set video data\n");
480 /* log position and move on for now */
481 s->video_chunk_offset = avio_tell(pb);
482 s->video_chunk_size = opcode_size;
483 avio_skip(pb, opcode_size);
487 av_dlog(NULL, "*** unknown opcode type\n");
488 chunk_type = CHUNK_BAD;
494 /* make a note of where the stream is sitting */
495 s->next_chunk_offset = avio_tell(pb);
497 /* dispatch the first of any pending packets */
498 if ((chunk_type == CHUNK_VIDEO) || (chunk_type == CHUNK_AUDIO_ONLY))
499 chunk_type = load_ipmovie_packet(s, pb, pkt);
504 static const char signature[] = "Interplay MVE File\x1A\0\x1A";
506 static int ipmovie_probe(AVProbeData *p)
509 uint8_t *b_end = p->buf + p->buf_size - sizeof(signature);
511 if (memcmp(b++, signature, sizeof(signature)) == 0)
512 return AVPROBE_SCORE_MAX;
518 static int ipmovie_read_header(AVFormatContext *s,
519 AVFormatParameters *ap)
521 IPMVEContext *ipmovie = s->priv_data;
522 AVIOContext *pb = s->pb;
525 unsigned char chunk_preamble[CHUNK_PREAMBLE_SIZE];
527 uint8_t signature_buffer[sizeof(signature)];
529 avio_read(pb, signature_buffer, sizeof(signature_buffer));
530 while (memcmp(signature_buffer, signature, sizeof(signature))) {
531 memmove(signature_buffer, signature_buffer + 1, sizeof(signature_buffer) - 1);
532 signature_buffer[sizeof(signature_buffer) - 1] = avio_r8(pb);
536 /* initialize private context members */
537 ipmovie->video_pts = ipmovie->audio_frame_count = 0;
538 ipmovie->audio_chunk_offset = ipmovie->video_chunk_offset =
539 ipmovie->decode_map_chunk_offset = 0;
541 /* on the first read, this will position the stream at the first chunk */
542 ipmovie->next_chunk_offset = avio_tell(pb) + 4;
544 /* process the first chunk which should be CHUNK_INIT_VIDEO */
545 if (process_ipmovie_chunk(ipmovie, pb, &pkt) != CHUNK_INIT_VIDEO)
546 return AVERROR_INVALIDDATA;
548 /* peek ahead to the next chunk-- if it is an init audio chunk, process
549 * it; if it is the first video chunk, this is a silent file */
550 if (avio_read(pb, chunk_preamble, CHUNK_PREAMBLE_SIZE) !=
553 chunk_type = AV_RL16(&chunk_preamble[2]);
554 avio_seek(pb, -CHUNK_PREAMBLE_SIZE, SEEK_CUR);
556 if (chunk_type == CHUNK_VIDEO)
557 ipmovie->audio_type = CODEC_ID_NONE; /* no audio */
558 else if (process_ipmovie_chunk(ipmovie, pb, &pkt) != CHUNK_INIT_AUDIO)
559 return AVERROR_INVALIDDATA;
561 /* initialize the stream decoders */
562 st = av_new_stream(s, 0);
564 return AVERROR(ENOMEM);
565 av_set_pts_info(st, 63, 1, 1000000);
566 ipmovie->video_stream_index = st->index;
567 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
568 st->codec->codec_id = CODEC_ID_INTERPLAY_VIDEO;
569 st->codec->codec_tag = 0; /* no fourcc */
570 st->codec->width = ipmovie->video_width;
571 st->codec->height = ipmovie->video_height;
572 st->codec->bits_per_coded_sample = ipmovie->video_bpp;
574 if (ipmovie->audio_type) {
575 st = av_new_stream(s, 0);
577 return AVERROR(ENOMEM);
578 av_set_pts_info(st, 32, 1, ipmovie->audio_sample_rate);
579 ipmovie->audio_stream_index = st->index;
580 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
581 st->codec->codec_id = ipmovie->audio_type;
582 st->codec->codec_tag = 0; /* no tag */
583 st->codec->channels = ipmovie->audio_channels;
584 st->codec->sample_rate = ipmovie->audio_sample_rate;
585 st->codec->bits_per_coded_sample = ipmovie->audio_bits;
586 st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
587 st->codec->bits_per_coded_sample;
588 if (st->codec->codec_id == CODEC_ID_INTERPLAY_DPCM)
589 st->codec->bit_rate /= 2;
590 st->codec->block_align = st->codec->channels * st->codec->bits_per_coded_sample;
596 static int ipmovie_read_packet(AVFormatContext *s,
599 IPMVEContext *ipmovie = s->priv_data;
600 AVIOContext *pb = s->pb;
603 ret = process_ipmovie_chunk(ipmovie, pb, pkt);
604 if (ret == CHUNK_BAD)
605 ret = AVERROR_INVALIDDATA;
606 else if (ret == CHUNK_EOF)
608 else if (ret == CHUNK_NOMEM)
609 ret = AVERROR(ENOMEM);
610 else if (ret == CHUNK_VIDEO)
618 AVInputFormat ff_ipmovie_demuxer = {
620 NULL_IF_CONFIG_SMALL("Interplay MVE format"),
621 sizeof(IPMVEContext),