2 * Interplay MVE File Demuxer
3 * Copyright (c) 2003 The ffmpeg Project
5 * This file is part of FFmpeg.
7 * FFmpeg 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 * FFmpeg 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 FFmpeg; 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"
39 #define CHUNK_PREAMBLE_SIZE 4
40 #define OPCODE_PREAMBLE_SIZE 4
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
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
77 #define PALETTE_COUNT 256
79 typedef struct IPMVEContext {
84 uint64_t frame_pts_inc;
86 unsigned int video_bpp;
87 unsigned int video_width;
88 unsigned int video_height;
90 uint32_t palette[256];
94 unsigned int audio_bits;
95 unsigned int audio_channels;
96 unsigned int audio_sample_rate;
97 enum AVCodecID audio_type;
98 unsigned int audio_frame_count;
100 int video_stream_index;
101 int audio_stream_index;
103 int64_t audio_chunk_offset;
104 int audio_chunk_size;
105 int64_t video_chunk_offset;
106 int video_chunk_size;
107 int64_t decode_map_chunk_offset;
108 int decode_map_chunk_size;
110 int64_t next_chunk_offset;
114 static int load_ipmovie_packet(IPMVEContext *s, AVIOContext *pb,
119 if (s->audio_chunk_offset && s->audio_channels && s->audio_bits) {
120 if (s->audio_type == AV_CODEC_ID_NONE) {
121 av_log(NULL, AV_LOG_ERROR, "Can not read audio packet before"
122 "audio codec is known\n");
126 /* adjust for PCM audio by skipping chunk header */
127 if (s->audio_type != AV_CODEC_ID_INTERPLAY_DPCM) {
128 s->audio_chunk_offset += 6;
129 s->audio_chunk_size -= 6;
132 avio_seek(pb, s->audio_chunk_offset, SEEK_SET);
133 s->audio_chunk_offset = 0;
135 if (s->audio_chunk_size != av_get_packet(pb, pkt, s->audio_chunk_size))
138 pkt->stream_index = s->audio_stream_index;
139 pkt->pts = s->audio_frame_count;
141 /* audio frame maintenance */
142 if (s->audio_type != AV_CODEC_ID_INTERPLAY_DPCM)
143 s->audio_frame_count +=
144 (s->audio_chunk_size / s->audio_channels / (s->audio_bits / 8));
146 s->audio_frame_count +=
147 (s->audio_chunk_size - 6 - s->audio_channels) / s->audio_channels;
149 av_dlog(NULL, "sending audio frame with pts %"PRId64" (%d audio frames)\n",
150 pkt->pts, s->audio_frame_count);
152 chunk_type = CHUNK_VIDEO;
154 } else if (s->decode_map_chunk_offset) {
156 /* send both the decode map and the video data together */
158 if (av_new_packet(pkt, s->decode_map_chunk_size + s->video_chunk_size))
161 if (s->has_palette) {
164 pal = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE,
167 memcpy(pal, s->palette, AVPALETTE_SIZE);
173 ff_add_param_change(pkt, 0, 0, 0, s->video_width, s->video_height);
176 pkt->pos= s->decode_map_chunk_offset;
177 avio_seek(pb, s->decode_map_chunk_offset, SEEK_SET);
178 s->decode_map_chunk_offset = 0;
180 if (avio_read(pb, pkt->data, s->decode_map_chunk_size) !=
181 s->decode_map_chunk_size) {
186 avio_seek(pb, s->video_chunk_offset, SEEK_SET);
187 s->video_chunk_offset = 0;
189 if (avio_read(pb, pkt->data + s->decode_map_chunk_size,
190 s->video_chunk_size) != s->video_chunk_size) {
195 pkt->stream_index = s->video_stream_index;
196 pkt->pts = s->video_pts;
198 av_dlog(NULL, "sending video frame with pts %"PRId64"\n", pkt->pts);
200 s->video_pts += s->frame_pts_inc;
202 chunk_type = CHUNK_VIDEO;
206 avio_seek(pb, s->next_chunk_offset, SEEK_SET);
207 chunk_type = CHUNK_DONE;
214 /* This function loads and processes a single chunk in an IP movie file.
215 * It returns the type of chunk that was processed. */
216 static int process_ipmovie_chunk(IPMVEContext *s, AVIOContext *pb,
219 unsigned char chunk_preamble[CHUNK_PREAMBLE_SIZE];
222 unsigned char opcode_preamble[OPCODE_PREAMBLE_SIZE];
223 unsigned char opcode_type;
224 unsigned char opcode_version;
226 unsigned char scratch[1024];
228 int first_color, last_color;
230 unsigned char r, g, b;
231 unsigned int width, height;
233 /* see if there are any pending packets */
234 chunk_type = load_ipmovie_packet(s, pb, pkt);
235 if (chunk_type != CHUNK_DONE)
238 /* read the next chunk, wherever the file happens to be pointing */
241 if (avio_read(pb, chunk_preamble, CHUNK_PREAMBLE_SIZE) !=
244 chunk_size = AV_RL16(&chunk_preamble[0]);
245 chunk_type = AV_RL16(&chunk_preamble[2]);
247 av_dlog(NULL, "chunk type 0x%04X, 0x%04X bytes: ", chunk_type, chunk_size);
249 switch (chunk_type) {
251 case CHUNK_INIT_AUDIO:
252 av_dlog(NULL, "initialize audio\n");
255 case CHUNK_AUDIO_ONLY:
256 av_dlog(NULL, "audio only\n");
259 case CHUNK_INIT_VIDEO:
260 av_dlog(NULL, "initialize video\n");
264 av_dlog(NULL, "video (and audio)\n");
268 av_dlog(NULL, "shutdown\n");
272 av_dlog(NULL, "end\n");
276 av_dlog(NULL, "invalid chunk\n");
277 chunk_type = CHUNK_BAD;
282 while ((chunk_size > 0) && (chunk_type != CHUNK_BAD)) {
284 /* read the next chunk, wherever the file happens to be pointing */
286 chunk_type = CHUNK_EOF;
289 if (avio_read(pb, opcode_preamble, CHUNK_PREAMBLE_SIZE) !=
290 CHUNK_PREAMBLE_SIZE) {
291 chunk_type = CHUNK_BAD;
295 opcode_size = AV_RL16(&opcode_preamble[0]);
296 opcode_type = opcode_preamble[2];
297 opcode_version = opcode_preamble[3];
299 chunk_size -= OPCODE_PREAMBLE_SIZE;
300 chunk_size -= opcode_size;
301 if (chunk_size < 0) {
302 av_dlog(NULL, "chunk_size countdown just went negative\n");
303 chunk_type = CHUNK_BAD;
307 av_dlog(NULL, " opcode type %02X, version %d, 0x%04X bytes: ",
308 opcode_type, opcode_version, opcode_size);
309 switch (opcode_type) {
311 case OPCODE_END_OF_STREAM:
312 av_dlog(NULL, "end of stream\n");
313 avio_skip(pb, opcode_size);
316 case OPCODE_END_OF_CHUNK:
317 av_dlog(NULL, "end of chunk\n");
318 avio_skip(pb, opcode_size);
321 case OPCODE_CREATE_TIMER:
322 av_dlog(NULL, "create timer\n");
323 if ((opcode_version > 0) || (opcode_size > 6)) {
324 av_dlog(NULL, "bad create_timer opcode\n");
325 chunk_type = CHUNK_BAD;
328 if (avio_read(pb, scratch, opcode_size) !=
330 chunk_type = CHUNK_BAD;
333 s->frame_pts_inc = ((uint64_t)AV_RL32(&scratch[0])) * AV_RL16(&scratch[4]);
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]));
339 case OPCODE_INIT_AUDIO_BUFFERS:
340 av_dlog(NULL, "initialize audio buffers\n");
341 if ((opcode_version > 1) || (opcode_size > 10)) {
342 av_dlog(NULL, "bad init_audio_buffers opcode\n");
343 chunk_type = CHUNK_BAD;
346 if (avio_read(pb, scratch, opcode_size) !=
348 chunk_type = CHUNK_BAD;
351 s->audio_sample_rate = AV_RL16(&scratch[4]);
352 audio_flags = AV_RL16(&scratch[2]);
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 = AV_CODEC_ID_INTERPLAY_DPCM;
360 else if (s->audio_bits == 16)
361 s->audio_type = AV_CODEC_ID_PCM_S16LE;
363 s->audio_type = AV_CODEC_ID_PCM_U8;
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 == AV_CODEC_ID_INTERPLAY_DPCM) ?
368 "Interplay audio" : "PCM");
371 case OPCODE_START_STOP_AUDIO:
372 av_dlog(NULL, "start/stop audio\n");
373 avio_skip(pb, opcode_size);
376 case OPCODE_INIT_VIDEO_BUFFERS:
377 av_dlog(NULL, "initialize video buffers\n");
378 if ((opcode_version > 2) || (opcode_size > 8)) {
379 av_dlog(NULL, "bad init_video_buffers opcode\n");
380 chunk_type = CHUNK_BAD;
383 if (avio_read(pb, scratch, opcode_size) !=
385 chunk_type = CHUNK_BAD;
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;
394 if (height != s->video_height) {
395 s->video_height = height;
398 if (opcode_version < 2 || !AV_RL16(&scratch[6])) {
403 av_dlog(NULL, "video resolution: %d x %d\n",
404 s->video_width, s->video_height);
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:
414 av_dlog(NULL, "unknown (but documented) opcode %02X\n", opcode_type);
415 avio_skip(pb, opcode_size);
418 case OPCODE_SEND_BUFFER:
419 av_dlog(NULL, "send buffer\n");
420 avio_skip(pb, opcode_size);
423 case OPCODE_AUDIO_FRAME:
424 av_dlog(NULL, "audio frame\n");
426 /* log position and move on for now */
427 s->audio_chunk_offset = avio_tell(pb);
428 s->audio_chunk_size = opcode_size;
429 avio_skip(pb, opcode_size);
432 case OPCODE_SILENCE_FRAME:
433 av_dlog(NULL, "silence frame\n");
434 avio_skip(pb, opcode_size);
437 case OPCODE_INIT_VIDEO_MODE:
438 av_dlog(NULL, "initialize video mode\n");
439 avio_skip(pb, opcode_size);
442 case OPCODE_CREATE_GRADIENT:
443 av_dlog(NULL, "create gradient\n");
444 avio_skip(pb, opcode_size);
447 case OPCODE_SET_PALETTE:
448 av_dlog(NULL, "set palette\n");
449 /* check for the logical maximum palette size
450 * (3 * 256 + 4 bytes) */
451 if (opcode_size > 0x304) {
452 av_dlog(NULL, "demux_ipmovie: set_palette opcode too large\n");
453 chunk_type = CHUNK_BAD;
456 if (avio_read(pb, scratch, opcode_size) != opcode_size) {
457 chunk_type = CHUNK_BAD;
461 /* load the palette into internal data structure */
462 first_color = AV_RL16(&scratch[0]);
463 last_color = first_color + AV_RL16(&scratch[2]) - 1;
464 /* sanity check (since they are 16 bit values) */
465 if ((first_color > 0xFF) || (last_color > 0xFF)) {
466 av_dlog(NULL, "demux_ipmovie: set_palette indexes out of range (%d -> %d)\n",
467 first_color, last_color);
468 chunk_type = CHUNK_BAD;
471 j = 4; /* offset of first palette data */
472 for (i = first_color; i <= last_color; i++) {
473 /* the palette is stored as a 6-bit VGA palette, thus each
474 * component is shifted up to a 8-bit range */
475 r = scratch[j++] * 4;
476 g = scratch[j++] * 4;
477 b = scratch[j++] * 4;
478 s->palette[i] = (0xFFU << 24) | (r << 16) | (g << 8) | (b);
479 s->palette[i] |= s->palette[i] >> 6 & 0x30303;
484 case OPCODE_SET_PALETTE_COMPRESSED:
485 av_dlog(NULL, "set palette compressed\n");
486 avio_skip(pb, opcode_size);
489 case OPCODE_SET_DECODING_MAP:
490 av_dlog(NULL, "set decoding map\n");
492 /* log position and move on for now */
493 s->decode_map_chunk_offset = avio_tell(pb);
494 s->decode_map_chunk_size = opcode_size;
495 avio_skip(pb, opcode_size);
498 case OPCODE_VIDEO_DATA:
499 av_dlog(NULL, "set video data\n");
501 /* log position and move on for now */
502 s->video_chunk_offset = avio_tell(pb);
503 s->video_chunk_size = opcode_size;
504 avio_skip(pb, opcode_size);
508 av_dlog(NULL, "*** unknown opcode type\n");
509 chunk_type = CHUNK_BAD;
515 /* make a note of where the stream is sitting */
516 s->next_chunk_offset = avio_tell(pb);
518 /* dispatch the first of any pending packets */
519 if ((chunk_type == CHUNK_VIDEO) || (chunk_type == CHUNK_AUDIO_ONLY))
520 chunk_type = load_ipmovie_packet(s, pb, pkt);
525 static const char signature[] = "Interplay MVE File\x1A\0\x1A";
527 static int ipmovie_probe(AVProbeData *p)
530 uint8_t *b_end = p->buf + p->buf_size - sizeof(signature);
532 if (b[0] == signature[0] && memcmp(b, signature, sizeof(signature)) == 0)
533 return AVPROBE_SCORE_MAX;
540 static int ipmovie_read_header(AVFormatContext *s)
542 IPMVEContext *ipmovie = s->priv_data;
543 AVIOContext *pb = s->pb;
546 unsigned char chunk_preamble[CHUNK_PREAMBLE_SIZE];
548 uint8_t signature_buffer[sizeof(signature)];
550 avio_read(pb, signature_buffer, sizeof(signature_buffer));
551 while (memcmp(signature_buffer, signature, sizeof(signature))) {
552 memmove(signature_buffer, signature_buffer + 1, sizeof(signature_buffer) - 1);
553 signature_buffer[sizeof(signature_buffer) - 1] = avio_r8(pb);
557 /* initialize private context members */
558 ipmovie->video_pts = ipmovie->audio_frame_count = 0;
559 ipmovie->audio_chunk_offset = ipmovie->video_chunk_offset =
560 ipmovie->decode_map_chunk_offset = 0;
562 /* on the first read, this will position the stream at the first chunk */
563 ipmovie->next_chunk_offset = avio_tell(pb) + 4;
565 for (i = 0; i < 256; i++)
566 ipmovie->palette[i] = 0xFFU << 24;
568 /* process the first chunk which should be CHUNK_INIT_VIDEO */
569 if (process_ipmovie_chunk(ipmovie, pb, &pkt) != CHUNK_INIT_VIDEO)
570 return AVERROR_INVALIDDATA;
572 /* peek ahead to the next chunk-- if it is an init audio chunk, process
573 * it; if it is the first video chunk, this is a silent file */
574 if (avio_read(pb, chunk_preamble, CHUNK_PREAMBLE_SIZE) !=
577 chunk_type = AV_RL16(&chunk_preamble[2]);
578 avio_seek(pb, -CHUNK_PREAMBLE_SIZE, SEEK_CUR);
580 if (chunk_type == CHUNK_VIDEO)
581 ipmovie->audio_type = AV_CODEC_ID_NONE; /* no audio */
582 else if (process_ipmovie_chunk(ipmovie, pb, &pkt) != CHUNK_INIT_AUDIO)
583 return AVERROR_INVALIDDATA;
585 /* initialize the stream decoders */
586 st = avformat_new_stream(s, NULL);
588 return AVERROR(ENOMEM);
589 avpriv_set_pts_info(st, 63, 1, 1000000);
590 ipmovie->video_stream_index = st->index;
591 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
592 st->codec->codec_id = AV_CODEC_ID_INTERPLAY_VIDEO;
593 st->codec->codec_tag = 0; /* no fourcc */
594 st->codec->width = ipmovie->video_width;
595 st->codec->height = ipmovie->video_height;
596 st->codec->bits_per_coded_sample = ipmovie->video_bpp;
598 if (ipmovie->audio_type) {
599 st = avformat_new_stream(s, NULL);
601 return AVERROR(ENOMEM);
602 avpriv_set_pts_info(st, 32, 1, ipmovie->audio_sample_rate);
603 ipmovie->audio_stream_index = st->index;
604 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
605 st->codec->codec_id = ipmovie->audio_type;
606 st->codec->codec_tag = 0; /* no tag */
607 st->codec->channels = ipmovie->audio_channels;
608 st->codec->sample_rate = ipmovie->audio_sample_rate;
609 st->codec->bits_per_coded_sample = ipmovie->audio_bits;
610 st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
611 st->codec->bits_per_coded_sample;
612 if (st->codec->codec_id == AV_CODEC_ID_INTERPLAY_DPCM)
613 st->codec->bit_rate /= 2;
614 st->codec->block_align = st->codec->channels * st->codec->bits_per_coded_sample;
620 static int ipmovie_read_packet(AVFormatContext *s,
623 IPMVEContext *ipmovie = s->priv_data;
624 AVIOContext *pb = s->pb;
628 ret = process_ipmovie_chunk(ipmovie, pb, pkt);
629 if (ret == CHUNK_BAD)
630 ret = AVERROR_INVALIDDATA;
631 else if (ret == CHUNK_EOF)
633 else if (ret == CHUNK_NOMEM)
634 ret = AVERROR(ENOMEM);
635 else if (ret == CHUNK_VIDEO)
637 else if (ret == CHUNK_INIT_VIDEO || ret == CHUNK_INIT_AUDIO)
646 AVInputFormat ff_ipmovie_demuxer = {
648 .long_name = NULL_IF_CONFIG_SMALL("Interplay MVE"),
649 .priv_data_size = sizeof(IPMVEContext),
650 .read_probe = ipmovie_probe,
651 .read_header = ipmovie_read_header,
652 .read_packet = ipmovie_read_packet,