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/channel_layout.h"
36 #include "libavutil/intreadwrite.h"
40 #define CHUNK_PREAMBLE_SIZE 4
41 #define OPCODE_PREAMBLE_SIZE 4
43 #define CHUNK_INIT_AUDIO 0x0000
44 #define CHUNK_AUDIO_ONLY 0x0001
45 #define CHUNK_INIT_VIDEO 0x0002
46 #define CHUNK_VIDEO 0x0003
47 #define CHUNK_SHUTDOWN 0x0004
48 #define CHUNK_END 0x0005
49 /* these last types are used internally */
50 #define CHUNK_DONE 0xFFFC
51 #define CHUNK_NOMEM 0xFFFD
52 #define CHUNK_EOF 0xFFFE
53 #define CHUNK_BAD 0xFFFF
55 #define OPCODE_END_OF_STREAM 0x00
56 #define OPCODE_END_OF_CHUNK 0x01
57 #define OPCODE_CREATE_TIMER 0x02
58 #define OPCODE_INIT_AUDIO_BUFFERS 0x03
59 #define OPCODE_START_STOP_AUDIO 0x04
60 #define OPCODE_INIT_VIDEO_BUFFERS 0x05
61 #define OPCODE_UNKNOWN_06 0x06
62 #define OPCODE_SEND_BUFFER 0x07
63 #define OPCODE_AUDIO_FRAME 0x08
64 #define OPCODE_SILENCE_FRAME 0x09
65 #define OPCODE_INIT_VIDEO_MODE 0x0A
66 #define OPCODE_CREATE_GRADIENT 0x0B
67 #define OPCODE_SET_PALETTE 0x0C
68 #define OPCODE_SET_PALETTE_COMPRESSED 0x0D
69 #define OPCODE_UNKNOWN_0E 0x0E
70 #define OPCODE_SET_DECODING_MAP 0x0F
71 #define OPCODE_UNKNOWN_10 0x10
72 #define OPCODE_VIDEO_DATA_11 0x11
73 #define OPCODE_UNKNOWN_12 0x12
74 #define OPCODE_UNKNOWN_13 0x13
75 #define OPCODE_UNKNOWN_14 0x14
76 #define OPCODE_UNKNOWN_15 0x15
78 #define PALETTE_COUNT 256
80 typedef struct IPMVEContext {
85 uint64_t frame_pts_inc;
87 unsigned int video_bpp;
88 unsigned int video_width;
89 unsigned int video_height;
91 uint32_t palette[256];
97 unsigned int audio_bits;
98 unsigned int audio_channels;
99 unsigned int audio_sample_rate;
100 enum AVCodecID audio_type;
101 unsigned int audio_frame_count;
103 int video_stream_index;
104 int audio_stream_index;
106 int64_t audio_chunk_offset;
107 int audio_chunk_size;
108 int64_t video_chunk_offset;
109 int video_chunk_size;
110 int64_t decode_map_chunk_offset;
111 int decode_map_chunk_size;
113 int64_t next_chunk_offset;
117 static int load_ipmovie_packet(IPMVEContext *s, AVIOContext *pb,
122 if (s->audio_chunk_offset && s->audio_channels && s->audio_bits) {
123 if (s->audio_type == AV_CODEC_ID_NONE) {
124 av_log(s->avf, AV_LOG_ERROR, "Can not read audio packet before"
125 "audio codec is known\n");
129 /* adjust for PCM audio by skipping chunk header */
130 if (s->audio_type != AV_CODEC_ID_INTERPLAY_DPCM) {
131 s->audio_chunk_offset += 6;
132 s->audio_chunk_size -= 6;
135 avio_seek(pb, s->audio_chunk_offset, SEEK_SET);
136 s->audio_chunk_offset = 0;
138 if (s->audio_chunk_size != av_get_packet(pb, pkt, s->audio_chunk_size))
141 pkt->stream_index = s->audio_stream_index;
142 pkt->pts = s->audio_frame_count;
144 /* audio frame maintenance */
145 if (s->audio_type != AV_CODEC_ID_INTERPLAY_DPCM)
146 s->audio_frame_count +=
147 (s->audio_chunk_size / s->audio_channels / (s->audio_bits / 8));
149 s->audio_frame_count +=
150 (s->audio_chunk_size - 6 - s->audio_channels) / s->audio_channels;
152 av_log(s->avf, AV_LOG_TRACE, "sending audio frame with pts %"PRId64" (%d audio frames)\n",
153 pkt->pts, s->audio_frame_count);
155 chunk_type = CHUNK_VIDEO;
157 } else if (s->frame_format) {
159 /* send the frame format, decode map, the video data, and the send_buffer flag together */
161 if (av_new_packet(pkt, 6 + s->decode_map_chunk_size + s->video_chunk_size))
164 if (s->has_palette) {
167 pal = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE,
170 memcpy(pal, s->palette, AVPALETTE_SIZE);
176 ff_add_param_change(pkt, 0, 0, 0, s->video_width, s->video_height);
180 AV_WL8(pkt->data, s->frame_format);
181 AV_WL8(pkt->data + 1, s->send_buffer);
182 AV_WL16(pkt->data + 2, s->video_chunk_size);
183 AV_WL16(pkt->data + 4, s->decode_map_chunk_size);
188 pkt->pos = s->video_chunk_offset;
189 avio_seek(pb, s->video_chunk_offset, SEEK_SET);
190 s->video_chunk_offset = 0;
192 if (avio_read(pb, pkt->data + 6, s->video_chunk_size) !=
193 s->video_chunk_size) {
194 av_packet_unref(pkt);
198 pkt->pos = s->decode_map_chunk_offset;
199 avio_seek(pb, s->decode_map_chunk_offset, SEEK_SET);
200 s->decode_map_chunk_offset = 0;
202 if (avio_read(pb, pkt->data + 6 + s->video_chunk_size,
203 s->decode_map_chunk_size) != s->decode_map_chunk_size) {
204 av_packet_unref(pkt);
208 s->video_chunk_size = 0;
209 s->decode_map_chunk_size = 0;
211 pkt->stream_index = s->video_stream_index;
212 pkt->pts = s->video_pts;
214 av_log(s->avf, AV_LOG_TRACE, "sending video frame with pts %"PRId64"\n", pkt->pts);
216 s->video_pts += s->frame_pts_inc;
218 chunk_type = CHUNK_VIDEO;
222 avio_seek(pb, s->next_chunk_offset, SEEK_SET);
223 chunk_type = CHUNK_DONE;
230 static int init_audio(AVFormatContext *s)
232 IPMVEContext *ipmovie = s->priv_data;
233 AVStream *st = avformat_new_stream(s, NULL);
235 return AVERROR(ENOMEM);
236 avpriv_set_pts_info(st, 32, 1, ipmovie->audio_sample_rate);
237 ipmovie->audio_stream_index = st->index;
238 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
239 st->codecpar->codec_id = ipmovie->audio_type;
240 st->codecpar->codec_tag = 0; /* no tag */
241 st->codecpar->channels = ipmovie->audio_channels;
242 st->codecpar->channel_layout = st->codecpar->channels == 1 ? AV_CH_LAYOUT_MONO :
244 st->codecpar->sample_rate = ipmovie->audio_sample_rate;
245 st->codecpar->bits_per_coded_sample = ipmovie->audio_bits;
246 st->codecpar->bit_rate = st->codecpar->channels * st->codecpar->sample_rate *
247 st->codecpar->bits_per_coded_sample;
248 if (st->codecpar->codec_id == AV_CODEC_ID_INTERPLAY_DPCM)
249 st->codecpar->bit_rate /= 2;
250 st->codecpar->block_align = st->codecpar->channels * st->codecpar->bits_per_coded_sample;
255 /* This function loads and processes a single chunk in an IP movie file.
256 * It returns the type of chunk that was processed. */
257 static int process_ipmovie_chunk(IPMVEContext *s, AVIOContext *pb,
260 unsigned char chunk_preamble[CHUNK_PREAMBLE_SIZE];
263 unsigned char opcode_preamble[OPCODE_PREAMBLE_SIZE];
264 unsigned char opcode_type;
265 unsigned char opcode_version;
267 unsigned char scratch[1024];
269 int first_color, last_color;
271 unsigned char r, g, b;
272 unsigned int width, height;
274 /* see if there are any pending packets */
275 chunk_type = load_ipmovie_packet(s, pb, pkt);
276 if (chunk_type != CHUNK_DONE)
279 /* read the next chunk, wherever the file happens to be pointing */
282 if (avio_read(pb, chunk_preamble, CHUNK_PREAMBLE_SIZE) !=
285 chunk_size = AV_RL16(&chunk_preamble[0]);
286 chunk_type = AV_RL16(&chunk_preamble[2]);
288 av_log(s->avf, AV_LOG_TRACE, "chunk type 0x%04X, 0x%04X bytes: ", chunk_type, chunk_size);
290 switch (chunk_type) {
292 case CHUNK_INIT_AUDIO:
293 av_log(s->avf, AV_LOG_TRACE, "initialize audio\n");
296 case CHUNK_AUDIO_ONLY:
297 av_log(s->avf, AV_LOG_TRACE, "audio only\n");
300 case CHUNK_INIT_VIDEO:
301 av_log(s->avf, AV_LOG_TRACE, "initialize video\n");
305 av_log(s->avf, AV_LOG_TRACE, "video (and audio)\n");
309 av_log(s->avf, AV_LOG_TRACE, "shutdown\n");
313 av_log(s->avf, AV_LOG_TRACE, "end\n");
317 av_log(s->avf, AV_LOG_TRACE, "invalid chunk\n");
318 chunk_type = CHUNK_BAD;
323 while ((chunk_size > 0) && (chunk_type != CHUNK_BAD)) {
325 /* read the next chunk, wherever the file happens to be pointing */
327 chunk_type = CHUNK_EOF;
330 if (avio_read(pb, opcode_preamble, CHUNK_PREAMBLE_SIZE) !=
331 CHUNK_PREAMBLE_SIZE) {
332 chunk_type = CHUNK_BAD;
336 opcode_size = AV_RL16(&opcode_preamble[0]);
337 opcode_type = opcode_preamble[2];
338 opcode_version = opcode_preamble[3];
340 chunk_size -= OPCODE_PREAMBLE_SIZE;
341 chunk_size -= opcode_size;
342 if (chunk_size < 0) {
343 av_log(s->avf, AV_LOG_TRACE, "chunk_size countdown just went negative\n");
344 chunk_type = CHUNK_BAD;
348 av_log(s->avf, AV_LOG_TRACE, " opcode type %02X, version %d, 0x%04X bytes: ",
349 opcode_type, opcode_version, opcode_size);
350 switch (opcode_type) {
352 case OPCODE_END_OF_STREAM:
353 av_log(s->avf, AV_LOG_TRACE, "end of stream\n");
354 avio_skip(pb, opcode_size);
357 case OPCODE_END_OF_CHUNK:
358 av_log(s->avf, AV_LOG_TRACE, "end of chunk\n");
359 avio_skip(pb, opcode_size);
362 case OPCODE_CREATE_TIMER:
363 av_log(s->avf, AV_LOG_TRACE, "create timer\n");
364 if ((opcode_version > 0) || (opcode_size != 6)) {
365 av_log(s->avf, AV_LOG_TRACE, "bad create_timer opcode\n");
366 chunk_type = CHUNK_BAD;
369 if (avio_read(pb, scratch, opcode_size) !=
371 chunk_type = CHUNK_BAD;
374 s->frame_pts_inc = ((uint64_t)AV_RL32(&scratch[0])) * AV_RL16(&scratch[4]);
377 case OPCODE_INIT_AUDIO_BUFFERS:
378 av_log(s->avf, AV_LOG_TRACE, "initialize audio buffers\n");
379 if (opcode_version > 1 || opcode_size > 10 || opcode_size < 6) {
380 av_log(s->avf, AV_LOG_TRACE, "bad init_audio_buffers opcode\n");
381 chunk_type = CHUNK_BAD;
384 if (avio_read(pb, scratch, opcode_size) !=
386 chunk_type = CHUNK_BAD;
389 s->audio_sample_rate = AV_RL16(&scratch[4]);
390 audio_flags = AV_RL16(&scratch[2]);
391 /* bit 0 of the flags: 0 = mono, 1 = stereo */
392 s->audio_channels = (audio_flags & 1) + 1;
393 /* bit 1 of the flags: 0 = 8 bit, 1 = 16 bit */
394 s->audio_bits = (((audio_flags >> 1) & 1) + 1) * 8;
395 /* bit 2 indicates compressed audio in version 1 opcode */
396 if ((opcode_version == 1) && (audio_flags & 0x4))
397 s->audio_type = AV_CODEC_ID_INTERPLAY_DPCM;
398 else if (s->audio_bits == 16)
399 s->audio_type = AV_CODEC_ID_PCM_S16LE;
401 s->audio_type = AV_CODEC_ID_PCM_U8;
402 av_log(s->avf, AV_LOG_TRACE, "audio: %d bits, %d Hz, %s, %s format\n",
403 s->audio_bits, s->audio_sample_rate,
404 (s->audio_channels == 2) ? "stereo" : "mono",
405 (s->audio_type == AV_CODEC_ID_INTERPLAY_DPCM) ?
406 "Interplay audio" : "PCM");
409 case OPCODE_START_STOP_AUDIO:
410 av_log(s->avf, AV_LOG_TRACE, "start/stop audio\n");
411 avio_skip(pb, opcode_size);
414 case OPCODE_INIT_VIDEO_BUFFERS:
415 av_log(s->avf, AV_LOG_TRACE, "initialize video buffers\n");
416 if ((opcode_version > 2) || (opcode_size > 8) || opcode_size < 4
417 || opcode_version == 2 && opcode_size < 8
419 av_log(s->avf, AV_LOG_TRACE, "bad init_video_buffers opcode\n");
420 chunk_type = CHUNK_BAD;
423 if (avio_read(pb, scratch, opcode_size) !=
425 chunk_type = CHUNK_BAD;
428 width = AV_RL16(&scratch[0]) * 8;
429 height = AV_RL16(&scratch[2]) * 8;
430 if (width != s->video_width) {
431 s->video_width = width;
434 if (height != s->video_height) {
435 s->video_height = height;
438 if (opcode_version < 2 || !AV_RL16(&scratch[6])) {
443 av_log(s->avf, AV_LOG_TRACE, "video resolution: %d x %d\n",
444 s->video_width, s->video_height);
447 case OPCODE_UNKNOWN_06:
448 case OPCODE_UNKNOWN_0E:
449 case OPCODE_UNKNOWN_10:
450 case OPCODE_UNKNOWN_12:
451 case OPCODE_UNKNOWN_13:
452 case OPCODE_UNKNOWN_14:
453 case OPCODE_UNKNOWN_15:
454 av_log(s->avf, AV_LOG_TRACE, "unknown (but documented) opcode %02X\n", opcode_type);
455 avio_skip(pb, opcode_size);
458 case OPCODE_SEND_BUFFER:
459 av_log(s->avf, AV_LOG_TRACE, "send buffer\n");
460 avio_skip(pb, opcode_size);
464 case OPCODE_AUDIO_FRAME:
465 av_log(s->avf, AV_LOG_TRACE, "audio frame\n");
467 /* log position and move on for now */
468 s->audio_chunk_offset = avio_tell(pb);
469 s->audio_chunk_size = opcode_size;
470 avio_skip(pb, opcode_size);
473 case OPCODE_SILENCE_FRAME:
474 av_log(s->avf, AV_LOG_TRACE, "silence frame\n");
475 avio_skip(pb, opcode_size);
478 case OPCODE_INIT_VIDEO_MODE:
479 av_log(s->avf, AV_LOG_TRACE, "initialize video mode\n");
480 avio_skip(pb, opcode_size);
483 case OPCODE_CREATE_GRADIENT:
484 av_log(s->avf, AV_LOG_TRACE, "create gradient\n");
485 avio_skip(pb, opcode_size);
488 case OPCODE_SET_PALETTE:
489 av_log(s->avf, AV_LOG_TRACE, "set palette\n");
490 /* check for the logical maximum palette size
491 * (3 * 256 + 4 bytes) */
492 if (opcode_size > 0x304 || opcode_size < 4) {
493 av_log(s->avf, AV_LOG_TRACE, "demux_ipmovie: set_palette opcode with invalid size\n");
494 chunk_type = CHUNK_BAD;
497 if (avio_read(pb, scratch, opcode_size) != opcode_size) {
498 chunk_type = CHUNK_BAD;
502 /* load the palette into internal data structure */
503 first_color = AV_RL16(&scratch[0]);
504 last_color = first_color + AV_RL16(&scratch[2]) - 1;
505 /* sanity check (since they are 16 bit values) */
506 if ( (first_color > 0xFF) || (last_color > 0xFF)
507 || (last_color - first_color + 1)*3 + 4 > opcode_size) {
508 av_log(s->avf, AV_LOG_TRACE, "demux_ipmovie: set_palette indexes out of range (%d -> %d)\n",
509 first_color, last_color);
510 chunk_type = CHUNK_BAD;
513 j = 4; /* offset of first palette data */
514 for (i = first_color; i <= last_color; i++) {
515 /* the palette is stored as a 6-bit VGA palette, thus each
516 * component is shifted up to a 8-bit range */
517 r = scratch[j++] * 4;
518 g = scratch[j++] * 4;
519 b = scratch[j++] * 4;
520 s->palette[i] = (0xFFU << 24) | (r << 16) | (g << 8) | (b);
521 s->palette[i] |= s->palette[i] >> 6 & 0x30303;
526 case OPCODE_SET_PALETTE_COMPRESSED:
527 av_log(s->avf, AV_LOG_TRACE, "set palette compressed\n");
528 avio_skip(pb, opcode_size);
531 case OPCODE_SET_DECODING_MAP:
532 av_log(s->avf, AV_LOG_TRACE, "set decoding map\n");
534 /* log position and move on for now */
535 s->decode_map_chunk_offset = avio_tell(pb);
536 s->decode_map_chunk_size = opcode_size;
537 avio_skip(pb, opcode_size);
540 case OPCODE_VIDEO_DATA_11:
541 av_log(s->avf, AV_LOG_TRACE, "set video data\n");
542 s->frame_format = 0x11;
544 /* log position and move on for now */
545 s->video_chunk_offset = avio_tell(pb);
546 s->video_chunk_size = opcode_size;
547 avio_skip(pb, opcode_size);
551 av_log(s->avf, AV_LOG_TRACE, "*** unknown opcode type\n");
552 chunk_type = CHUNK_BAD;
558 if (s->avf->nb_streams == 1 && s->audio_type)
561 /* make a note of where the stream is sitting */
562 s->next_chunk_offset = avio_tell(pb);
564 /* dispatch the first of any pending packets */
565 if ((chunk_type == CHUNK_VIDEO) || (chunk_type == CHUNK_AUDIO_ONLY))
566 chunk_type = load_ipmovie_packet(s, pb, pkt);
571 static const char signature[] = "Interplay MVE File\x1A\0\x1A";
573 static int ipmovie_probe(AVProbeData *p)
575 const uint8_t *b = p->buf;
576 const uint8_t *b_end = p->buf + p->buf_size - sizeof(signature);
578 if (b[0] == signature[0] && memcmp(b, signature, sizeof(signature)) == 0)
579 return AVPROBE_SCORE_MAX;
586 static int ipmovie_read_header(AVFormatContext *s)
588 IPMVEContext *ipmovie = s->priv_data;
589 AVIOContext *pb = s->pb;
592 unsigned char chunk_preamble[CHUNK_PREAMBLE_SIZE];
594 uint8_t signature_buffer[sizeof(signature)];
598 avio_read(pb, signature_buffer, sizeof(signature_buffer));
599 while (memcmp(signature_buffer, signature, sizeof(signature))) {
600 memmove(signature_buffer, signature_buffer + 1, sizeof(signature_buffer) - 1);
601 signature_buffer[sizeof(signature_buffer) - 1] = avio_r8(pb);
605 /* initialize private context members */
606 ipmovie->video_pts = ipmovie->audio_frame_count = 0;
607 ipmovie->audio_chunk_offset = ipmovie->video_chunk_offset =
608 ipmovie->decode_map_chunk_offset = 0;
609 ipmovie->decode_map_chunk_size = ipmovie->video_chunk_size = 0;
610 ipmovie->send_buffer = ipmovie->frame_format = 0;
612 /* on the first read, this will position the stream at the first chunk */
613 ipmovie->next_chunk_offset = avio_tell(pb) + 4;
615 for (i = 0; i < 256; i++)
616 ipmovie->palette[i] = 0xFFU << 24;
618 /* process the first chunk which should be CHUNK_INIT_VIDEO */
619 if (process_ipmovie_chunk(ipmovie, pb, &pkt) != CHUNK_INIT_VIDEO)
620 return AVERROR_INVALIDDATA;
622 /* peek ahead to the next chunk-- if it is an init audio chunk, process
623 * it; if it is the first video chunk, this is a silent file */
624 if (avio_read(pb, chunk_preamble, CHUNK_PREAMBLE_SIZE) !=
627 chunk_type = AV_RL16(&chunk_preamble[2]);
628 avio_seek(pb, -CHUNK_PREAMBLE_SIZE, SEEK_CUR);
630 if (chunk_type == CHUNK_VIDEO)
631 ipmovie->audio_type = AV_CODEC_ID_NONE; /* no audio */
632 else if (process_ipmovie_chunk(ipmovie, pb, &pkt) != CHUNK_INIT_AUDIO)
633 return AVERROR_INVALIDDATA;
635 /* initialize the stream decoders */
636 st = avformat_new_stream(s, NULL);
638 return AVERROR(ENOMEM);
639 avpriv_set_pts_info(st, 63, 1, 1000000);
640 ipmovie->video_stream_index = st->index;
641 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
642 st->codecpar->codec_id = AV_CODEC_ID_INTERPLAY_VIDEO;
643 st->codecpar->codec_tag = 0; /* no fourcc */
644 st->codecpar->width = ipmovie->video_width;
645 st->codecpar->height = ipmovie->video_height;
646 st->codecpar->bits_per_coded_sample = ipmovie->video_bpp;
648 if (ipmovie->audio_type) {
649 return init_audio(s);
651 s->ctx_flags |= AVFMTCTX_NOHEADER;
656 static int ipmovie_read_packet(AVFormatContext *s,
659 IPMVEContext *ipmovie = s->priv_data;
660 AVIOContext *pb = s->pb;
664 ret = process_ipmovie_chunk(ipmovie, pb, pkt);
665 if (ret == CHUNK_BAD)
666 ret = AVERROR_INVALIDDATA;
667 else if (ret == CHUNK_EOF)
669 else if (ret == CHUNK_NOMEM)
670 ret = AVERROR(ENOMEM);
671 else if (ret == CHUNK_END || ret == CHUNK_SHUTDOWN)
673 else if (ret == CHUNK_VIDEO)
675 else if (ret == CHUNK_INIT_VIDEO || ret == CHUNK_INIT_AUDIO)
684 AVInputFormat ff_ipmovie_demuxer = {
686 .long_name = NULL_IF_CONFIG_SMALL("Interplay MVE"),
687 .priv_data_size = sizeof(IPMVEContext),
688 .read_probe = ipmovie_probe,
689 .read_header = ipmovie_read_header,
690 .read_packet = ipmovie_read_packet,