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
23 * @file libavformat/ipmovie.c
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 /* debugging support: #define DEBUG_IPMOVIE as non-zero to see extremely
39 * verbose information about the demux process */
40 #define DEBUG_IPMOVIE 0
43 #define debug_ipmovie printf
45 static inline void debug_ipmovie(const char *format
, ...) { }
48 #define IPMOVIE_SIGNATURE "Interplay MVE File\x1A\0"
49 #define IPMOVIE_SIGNATURE_SIZE 20
50 #define CHUNK_PREAMBLE_SIZE 4
51 #define OPCODE_PREAMBLE_SIZE 4
53 #define CHUNK_INIT_AUDIO 0x0000
54 #define CHUNK_AUDIO_ONLY 0x0001
55 #define CHUNK_INIT_VIDEO 0x0002
56 #define CHUNK_VIDEO 0x0003
57 #define CHUNK_SHUTDOWN 0x0004
58 #define CHUNK_END 0x0005
59 /* these last types are used internally */
60 #define CHUNK_DONE 0xFFFC
61 #define CHUNK_NOMEM 0xFFFD
62 #define CHUNK_EOF 0xFFFE
63 #define CHUNK_BAD 0xFFFF
65 #define OPCODE_END_OF_STREAM 0x00
66 #define OPCODE_END_OF_CHUNK 0x01
67 #define OPCODE_CREATE_TIMER 0x02
68 #define OPCODE_INIT_AUDIO_BUFFERS 0x03
69 #define OPCODE_START_STOP_AUDIO 0x04
70 #define OPCODE_INIT_VIDEO_BUFFERS 0x05
71 #define OPCODE_UNKNOWN_06 0x06
72 #define OPCODE_SEND_BUFFER 0x07
73 #define OPCODE_AUDIO_FRAME 0x08
74 #define OPCODE_SILENCE_FRAME 0x09
75 #define OPCODE_INIT_VIDEO_MODE 0x0A
76 #define OPCODE_CREATE_GRADIENT 0x0B
77 #define OPCODE_SET_PALETTE 0x0C
78 #define OPCODE_SET_PALETTE_COMPRESSED 0x0D
79 #define OPCODE_UNKNOWN_0E 0x0E
80 #define OPCODE_SET_DECODING_MAP 0x0F
81 #define OPCODE_UNKNOWN_10 0x10
82 #define OPCODE_VIDEO_DATA 0x11
83 #define OPCODE_UNKNOWN_12 0x12
84 #define OPCODE_UNKNOWN_13 0x13
85 #define OPCODE_UNKNOWN_14 0x14
86 #define OPCODE_UNKNOWN_15 0x15
88 #define PALETTE_COUNT 256
90 typedef struct IPMVEContext
{
98 unsigned int video_width
;
99 unsigned int video_height
;
102 unsigned int audio_bits
;
103 unsigned int audio_channels
;
104 unsigned int audio_sample_rate
;
105 enum CodecID audio_type
;
106 unsigned int audio_frame_count
;
108 int video_stream_index
;
109 int audio_stream_index
;
111 int64_t audio_chunk_offset
;
112 int audio_chunk_size
;
113 int64_t video_chunk_offset
;
114 int video_chunk_size
;
115 int64_t decode_map_chunk_offset
;
116 int decode_map_chunk_size
;
118 int64_t next_chunk_offset
;
120 AVPaletteControl palette_control
;
124 static int load_ipmovie_packet(IPMVEContext
*s
, ByteIOContext
*pb
,
128 int64_t audio_pts
= 0;
130 if (s
->audio_chunk_offset
) {
132 /* adjust for PCM audio by skipping chunk header */
133 if (s
->audio_type
!= CODEC_ID_INTERPLAY_DPCM
) {
134 s
->audio_chunk_offset
+= 6;
135 s
->audio_chunk_size
-= 6;
138 url_fseek(pb
, s
->audio_chunk_offset
, SEEK_SET
);
139 s
->audio_chunk_offset
= 0;
141 /* figure out the audio pts */
143 audio_pts
*= s
->audio_frame_count
;
144 audio_pts
/= s
->audio_sample_rate
;
146 if (s
->audio_chunk_size
!= av_get_packet(pb
, pkt
, s
->audio_chunk_size
))
149 pkt
->stream_index
= s
->audio_stream_index
;
150 pkt
->pts
= audio_pts
;
152 /* audio frame maintenance */
153 if (s
->audio_type
!= CODEC_ID_INTERPLAY_DPCM
)
154 s
->audio_frame_count
+=
155 (s
->audio_chunk_size
/ s
->audio_channels
/ (s
->audio_bits
/ 8));
157 s
->audio_frame_count
+=
158 (s
->audio_chunk_size
- 6) / s
->audio_channels
;
160 debug_ipmovie("sending audio frame with pts %"PRId64
" (%d audio frames)\n",
161 audio_pts
, s
->audio_frame_count
);
163 chunk_type
= CHUNK_VIDEO
;
165 } else if (s
->decode_map_chunk_offset
) {
167 /* send both the decode map and the video data together */
169 if (av_new_packet(pkt
, s
->decode_map_chunk_size
+ s
->video_chunk_size
))
172 pkt
->pos
= s
->decode_map_chunk_offset
;
173 url_fseek(pb
, s
->decode_map_chunk_offset
, SEEK_SET
);
174 s
->decode_map_chunk_offset
= 0;
176 if (get_buffer(pb
, pkt
->data
, s
->decode_map_chunk_size
) !=
177 s
->decode_map_chunk_size
) {
182 url_fseek(pb
, s
->video_chunk_offset
, SEEK_SET
);
183 s
->video_chunk_offset
= 0;
185 if (get_buffer(pb
, pkt
->data
+ s
->decode_map_chunk_size
,
186 s
->video_chunk_size
) != s
->video_chunk_size
) {
191 pkt
->stream_index
= s
->video_stream_index
;
192 pkt
->pts
= s
->video_pts
;
194 debug_ipmovie("sending video frame with pts %"PRId64
"\n",
197 s
->video_pts
+= s
->frame_pts_inc
;
199 chunk_type
= CHUNK_VIDEO
;
203 url_fseek(pb
, s
->next_chunk_offset
, SEEK_SET
);
204 chunk_type
= CHUNK_DONE
;
211 /* This function loads and processes a single chunk in an IP movie file.
212 * It returns the type of chunk that was processed. */
213 static int process_ipmovie_chunk(IPMVEContext
*s
, ByteIOContext
*pb
,
216 unsigned char chunk_preamble
[CHUNK_PREAMBLE_SIZE
];
219 unsigned char opcode_preamble
[OPCODE_PREAMBLE_SIZE
];
220 unsigned char opcode_type
;
221 unsigned char opcode_version
;
223 unsigned char scratch
[1024];
225 int first_color
, last_color
;
227 unsigned char r
, g
, b
;
229 /* see if there are any pending packets */
230 chunk_type
= load_ipmovie_packet(s
, pb
, pkt
);
231 if (chunk_type
!= CHUNK_DONE
)
234 /* read the next chunk, wherever the file happens to be pointing */
237 if (get_buffer(pb
, chunk_preamble
, CHUNK_PREAMBLE_SIZE
) !=
240 chunk_size
= AV_RL16(&chunk_preamble
[0]);
241 chunk_type
= AV_RL16(&chunk_preamble
[2]);
243 debug_ipmovie("chunk type 0x%04X, 0x%04X bytes: ", chunk_type
, chunk_size
);
245 switch (chunk_type
) {
247 case CHUNK_INIT_AUDIO
:
248 debug_ipmovie("initialize audio\n");
251 case CHUNK_AUDIO_ONLY
:
252 debug_ipmovie("audio only\n");
255 case CHUNK_INIT_VIDEO
:
256 debug_ipmovie("initialize video\n");
260 debug_ipmovie("video (and audio)\n");
264 debug_ipmovie("shutdown\n");
268 debug_ipmovie("end\n");
272 debug_ipmovie("invalid chunk\n");
273 chunk_type
= CHUNK_BAD
;
278 while ((chunk_size
> 0) && (chunk_type
!= CHUNK_BAD
)) {
280 /* read the next chunk, wherever the file happens to be pointing */
282 chunk_type
= CHUNK_EOF
;
285 if (get_buffer(pb
, opcode_preamble
, CHUNK_PREAMBLE_SIZE
) !=
286 CHUNK_PREAMBLE_SIZE
) {
287 chunk_type
= CHUNK_BAD
;
291 opcode_size
= AV_RL16(&opcode_preamble
[0]);
292 opcode_type
= opcode_preamble
[2];
293 opcode_version
= opcode_preamble
[3];
295 chunk_size
-= OPCODE_PREAMBLE_SIZE
;
296 chunk_size
-= opcode_size
;
297 if (chunk_size
< 0) {
298 debug_ipmovie("chunk_size countdown just went negative\n");
299 chunk_type
= CHUNK_BAD
;
303 debug_ipmovie(" opcode type %02X, version %d, 0x%04X bytes: ",
304 opcode_type
, opcode_version
, opcode_size
);
305 switch (opcode_type
) {
307 case OPCODE_END_OF_STREAM
:
308 debug_ipmovie("end of stream\n");
309 url_fseek(pb
, opcode_size
, SEEK_CUR
);
312 case OPCODE_END_OF_CHUNK
:
313 debug_ipmovie("end of chunk\n");
314 url_fseek(pb
, opcode_size
, SEEK_CUR
);
317 case OPCODE_CREATE_TIMER
:
318 debug_ipmovie("create timer\n");
319 if ((opcode_version
> 0) || (opcode_size
> 6)) {
320 debug_ipmovie("bad create_timer opcode\n");
321 chunk_type
= CHUNK_BAD
;
324 if (get_buffer(pb
, scratch
, opcode_size
) !=
326 chunk_type
= CHUNK_BAD
;
329 s
->fps
= 1000000.0 / (AV_RL32(&scratch
[0]) * AV_RL16(&scratch
[4]));
330 s
->frame_pts_inc
= 90000 / s
->fps
;
331 debug_ipmovie(" %.2f frames/second (timer div = %d, subdiv = %d)\n",
332 s
->fps
, AV_RL32(&scratch
[0]), AV_RL16(&scratch
[4]));
335 case OPCODE_INIT_AUDIO_BUFFERS
:
336 debug_ipmovie("initialize audio buffers\n");
337 if ((opcode_version
> 1) || (opcode_size
> 10)) {
338 debug_ipmovie("bad init_audio_buffers opcode\n");
339 chunk_type
= CHUNK_BAD
;
342 if (get_buffer(pb
, scratch
, opcode_size
) !=
344 chunk_type
= CHUNK_BAD
;
347 s
->audio_sample_rate
= AV_RL16(&scratch
[4]);
348 audio_flags
= AV_RL16(&scratch
[2]);
349 /* bit 0 of the flags: 0 = mono, 1 = stereo */
350 s
->audio_channels
= (audio_flags
& 1) + 1;
351 /* bit 1 of the flags: 0 = 8 bit, 1 = 16 bit */
352 s
->audio_bits
= (((audio_flags
>> 1) & 1) + 1) * 8;
353 /* bit 2 indicates compressed audio in version 1 opcode */
354 if ((opcode_version
== 1) && (audio_flags
& 0x4))
355 s
->audio_type
= CODEC_ID_INTERPLAY_DPCM
;
356 else if (s
->audio_bits
== 16)
357 s
->audio_type
= CODEC_ID_PCM_S16LE
;
359 s
->audio_type
= CODEC_ID_PCM_U8
;
360 debug_ipmovie("audio: %d bits, %d Hz, %s, %s format\n",
362 s
->audio_sample_rate
,
363 (s
->audio_channels
== 2) ? "stereo" : "mono",
364 (s
->audio_type
== CODEC_ID_INTERPLAY_DPCM
) ?
365 "Interplay audio" : "PCM");
368 case OPCODE_START_STOP_AUDIO
:
369 debug_ipmovie("start/stop audio\n");
370 url_fseek(pb
, opcode_size
, SEEK_CUR
);
373 case OPCODE_INIT_VIDEO_BUFFERS
:
374 debug_ipmovie("initialize video buffers\n");
375 if ((opcode_version
> 2) || (opcode_size
> 8)) {
376 debug_ipmovie("bad init_video_buffers opcode\n");
377 chunk_type
= CHUNK_BAD
;
380 if (get_buffer(pb
, scratch
, opcode_size
) !=
382 chunk_type
= CHUNK_BAD
;
385 s
->video_width
= AV_RL16(&scratch
[0]) * 8;
386 s
->video_height
= AV_RL16(&scratch
[2]) * 8;
387 debug_ipmovie("video resolution: %d x %d\n",
388 s
->video_width
, s
->video_height
);
391 case OPCODE_UNKNOWN_06
:
392 case OPCODE_UNKNOWN_0E
:
393 case OPCODE_UNKNOWN_10
:
394 case OPCODE_UNKNOWN_12
:
395 case OPCODE_UNKNOWN_13
:
396 case OPCODE_UNKNOWN_14
:
397 case OPCODE_UNKNOWN_15
:
398 debug_ipmovie("unknown (but documented) opcode %02X\n", opcode_type
);
399 url_fseek(pb
, opcode_size
, SEEK_CUR
);
402 case OPCODE_SEND_BUFFER
:
403 debug_ipmovie("send buffer\n");
404 url_fseek(pb
, opcode_size
, SEEK_CUR
);
407 case OPCODE_AUDIO_FRAME
:
408 debug_ipmovie("audio frame\n");
410 /* log position and move on for now */
411 s
->audio_chunk_offset
= url_ftell(pb
);
412 s
->audio_chunk_size
= opcode_size
;
413 url_fseek(pb
, opcode_size
, SEEK_CUR
);
416 case OPCODE_SILENCE_FRAME
:
417 debug_ipmovie("silence frame\n");
418 url_fseek(pb
, opcode_size
, SEEK_CUR
);
421 case OPCODE_INIT_VIDEO_MODE
:
422 debug_ipmovie("initialize video mode\n");
423 url_fseek(pb
, opcode_size
, SEEK_CUR
);
426 case OPCODE_CREATE_GRADIENT
:
427 debug_ipmovie("create gradient\n");
428 url_fseek(pb
, opcode_size
, SEEK_CUR
);
431 case OPCODE_SET_PALETTE
:
432 debug_ipmovie("set palette\n");
433 /* check for the logical maximum palette size
434 * (3 * 256 + 4 bytes) */
435 if (opcode_size
> 0x304) {
436 debug_ipmovie("demux_ipmovie: set_palette opcode too large\n");
437 chunk_type
= CHUNK_BAD
;
440 if (get_buffer(pb
, scratch
, opcode_size
) != opcode_size
) {
441 chunk_type
= CHUNK_BAD
;
445 /* load the palette into internal data structure */
446 first_color
= AV_RL16(&scratch
[0]);
447 last_color
= first_color
+ AV_RL16(&scratch
[2]) - 1;
448 /* sanity check (since they are 16 bit values) */
449 if ((first_color
> 0xFF) || (last_color
> 0xFF)) {
450 debug_ipmovie("demux_ipmovie: set_palette indexes out of range (%d -> %d)\n",
451 first_color
, last_color
);
452 chunk_type
= CHUNK_BAD
;
455 j
= 4; /* offset of first palette data */
456 for (i
= first_color
; i
<= last_color
; i
++) {
457 /* the palette is stored as a 6-bit VGA palette, thus each
458 * component is shifted up to a 8-bit range */
459 r
= scratch
[j
++] * 4;
460 g
= scratch
[j
++] * 4;
461 b
= scratch
[j
++] * 4;
462 s
->palette_control
.palette
[i
] = (r
<< 16) | (g
<< 8) | (b
);
464 /* indicate a palette change */
465 s
->palette_control
.palette_changed
= 1;
468 case OPCODE_SET_PALETTE_COMPRESSED
:
469 debug_ipmovie("set palette compressed\n");
470 url_fseek(pb
, opcode_size
, SEEK_CUR
);
473 case OPCODE_SET_DECODING_MAP
:
474 debug_ipmovie("set decoding map\n");
476 /* log position and move on for now */
477 s
->decode_map_chunk_offset
= url_ftell(pb
);
478 s
->decode_map_chunk_size
= opcode_size
;
479 url_fseek(pb
, opcode_size
, SEEK_CUR
);
482 case OPCODE_VIDEO_DATA
:
483 debug_ipmovie("set video data\n");
485 /* log position and move on for now */
486 s
->video_chunk_offset
= url_ftell(pb
);
487 s
->video_chunk_size
= opcode_size
;
488 url_fseek(pb
, opcode_size
, SEEK_CUR
);
492 debug_ipmovie("*** unknown opcode type\n");
493 chunk_type
= CHUNK_BAD
;
499 /* make a note of where the stream is sitting */
500 s
->next_chunk_offset
= url_ftell(pb
);
502 /* dispatch the first of any pending packets */
503 if ((chunk_type
== CHUNK_VIDEO
) || (chunk_type
== CHUNK_AUDIO_ONLY
))
504 chunk_type
= load_ipmovie_packet(s
, pb
, pkt
);
509 static int ipmovie_probe(AVProbeData
*p
)
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
= s
->priv_data
;
521 ByteIOContext
*pb
= s
->pb
;
524 unsigned char chunk_preamble
[CHUNK_PREAMBLE_SIZE
];
527 /* initialize private context members */
528 ipmovie
->video_pts
= ipmovie
->audio_frame_count
= 0;
529 ipmovie
->audio_chunk_offset
= ipmovie
->video_chunk_offset
=
530 ipmovie
->decode_map_chunk_offset
= 0;
532 /* on the first read, this will position the stream at the first chunk */
533 ipmovie
->next_chunk_offset
= IPMOVIE_SIGNATURE_SIZE
+ 6;
535 /* process the first chunk which should be CHUNK_INIT_VIDEO */
536 if (process_ipmovie_chunk(ipmovie
, pb
, &pkt
) != CHUNK_INIT_VIDEO
)
537 return AVERROR_INVALIDDATA
;
539 /* peek ahead to the next chunk-- if it is an init audio chunk, process
540 * it; if it is the first video chunk, this is a silent file */
541 if (get_buffer(pb
, chunk_preamble
, CHUNK_PREAMBLE_SIZE
) !=
544 chunk_type
= AV_RL16(&chunk_preamble
[2]);
545 url_fseek(pb
, -CHUNK_PREAMBLE_SIZE
, SEEK_CUR
);
547 if (chunk_type
== CHUNK_VIDEO
)
548 ipmovie
->audio_type
= CODEC_ID_NONE
; /* no audio */
549 else if (process_ipmovie_chunk(ipmovie
, pb
, &pkt
) != CHUNK_INIT_AUDIO
)
550 return AVERROR_INVALIDDATA
;
552 /* initialize the stream decoders */
553 st
= av_new_stream(s
, 0);
555 return AVERROR(ENOMEM
);
556 av_set_pts_info(st
, 33, 1, 90000);
557 ipmovie
->video_stream_index
= st
->index
;
558 st
->codec
->codec_type
= CODEC_TYPE_VIDEO
;
559 st
->codec
->codec_id
= CODEC_ID_INTERPLAY_VIDEO
;
560 st
->codec
->codec_tag
= 0; /* no fourcc */
561 st
->codec
->width
= ipmovie
->video_width
;
562 st
->codec
->height
= ipmovie
->video_height
;
564 /* palette considerations */
565 st
->codec
->palctrl
= &ipmovie
->palette_control
;
567 if (ipmovie
->audio_type
) {
568 st
= av_new_stream(s
, 0);
570 return AVERROR(ENOMEM
);
571 av_set_pts_info(st
, 33, 1, 90000);
572 ipmovie
->audio_stream_index
= st
->index
;
573 st
->codec
->codec_type
= CODEC_TYPE_AUDIO
;
574 st
->codec
->codec_id
= ipmovie
->audio_type
;
575 st
->codec
->codec_tag
= 0; /* no tag */
576 st
->codec
->channels
= ipmovie
->audio_channels
;
577 st
->codec
->sample_rate
= ipmovie
->audio_sample_rate
;
578 st
->codec
->bits_per_coded_sample
= ipmovie
->audio_bits
;
579 st
->codec
->bit_rate
= st
->codec
->channels
* st
->codec
->sample_rate
*
580 st
->codec
->bits_per_coded_sample
;
581 if (st
->codec
->codec_id
== CODEC_ID_INTERPLAY_DPCM
)
582 st
->codec
->bit_rate
/= 2;
583 st
->codec
->block_align
= st
->codec
->channels
* st
->codec
->bits_per_coded_sample
;
589 static int ipmovie_read_packet(AVFormatContext
*s
,
592 IPMVEContext
*ipmovie
= s
->priv_data
;
593 ByteIOContext
*pb
= s
->pb
;
596 ret
= process_ipmovie_chunk(ipmovie
, pb
, pkt
);
597 if (ret
== CHUNK_BAD
)
598 ret
= AVERROR_INVALIDDATA
;
599 else if (ret
== CHUNK_EOF
)
601 else if (ret
== CHUNK_NOMEM
)
602 ret
= AVERROR(ENOMEM
);
603 else if (ret
== CHUNK_VIDEO
)
611 AVInputFormat ipmovie_demuxer
= {
613 NULL_IF_CONFIG_SMALL("Interplay MVE format"),
614 sizeof(IPMVEContext
),