Simplify by combining increment with array access.
[FFMpeg-mirror/lagarith.git] / libavformat / ipmovie.c
blobc9b506a3ef374ab2e48a67d87ff0636d91a7431c
1 /*
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
22 /**
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"
36 #include "avformat.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
42 #if DEBUG_IPMOVIE
43 #undef printf
44 #define debug_ipmovie printf
45 #else
46 static inline void debug_ipmovie(const char *format, ...) { }
47 #endif
49 #define CHUNK_PREAMBLE_SIZE 4
50 #define OPCODE_PREAMBLE_SIZE 4
52 #define CHUNK_INIT_AUDIO 0x0000
53 #define CHUNK_AUDIO_ONLY 0x0001
54 #define CHUNK_INIT_VIDEO 0x0002
55 #define CHUNK_VIDEO 0x0003
56 #define CHUNK_SHUTDOWN 0x0004
57 #define CHUNK_END 0x0005
58 /* these last types are used internally */
59 #define CHUNK_DONE 0xFFFC
60 #define CHUNK_NOMEM 0xFFFD
61 #define CHUNK_EOF 0xFFFE
62 #define CHUNK_BAD 0xFFFF
64 #define OPCODE_END_OF_STREAM 0x00
65 #define OPCODE_END_OF_CHUNK 0x01
66 #define OPCODE_CREATE_TIMER 0x02
67 #define OPCODE_INIT_AUDIO_BUFFERS 0x03
68 #define OPCODE_START_STOP_AUDIO 0x04
69 #define OPCODE_INIT_VIDEO_BUFFERS 0x05
70 #define OPCODE_UNKNOWN_06 0x06
71 #define OPCODE_SEND_BUFFER 0x07
72 #define OPCODE_AUDIO_FRAME 0x08
73 #define OPCODE_SILENCE_FRAME 0x09
74 #define OPCODE_INIT_VIDEO_MODE 0x0A
75 #define OPCODE_CREATE_GRADIENT 0x0B
76 #define OPCODE_SET_PALETTE 0x0C
77 #define OPCODE_SET_PALETTE_COMPRESSED 0x0D
78 #define OPCODE_UNKNOWN_0E 0x0E
79 #define OPCODE_SET_DECODING_MAP 0x0F
80 #define OPCODE_UNKNOWN_10 0x10
81 #define OPCODE_VIDEO_DATA 0x11
82 #define OPCODE_UNKNOWN_12 0x12
83 #define OPCODE_UNKNOWN_13 0x13
84 #define OPCODE_UNKNOWN_14 0x14
85 #define OPCODE_UNKNOWN_15 0x15
87 #define PALETTE_COUNT 256
89 typedef struct IPMVEContext {
91 unsigned char *buf;
92 int buf_size;
94 uint64_t frame_pts_inc;
96 unsigned int video_width;
97 unsigned int video_height;
98 int64_t video_pts;
100 unsigned int audio_bits;
101 unsigned int audio_channels;
102 unsigned int audio_sample_rate;
103 enum CodecID audio_type;
104 unsigned int audio_frame_count;
106 int video_stream_index;
107 int audio_stream_index;
109 int64_t audio_chunk_offset;
110 int audio_chunk_size;
111 int64_t video_chunk_offset;
112 int video_chunk_size;
113 int64_t decode_map_chunk_offset;
114 int decode_map_chunk_size;
116 int64_t next_chunk_offset;
118 AVPaletteControl palette_control;
120 } IPMVEContext;
122 static int load_ipmovie_packet(IPMVEContext *s, ByteIOContext *pb,
123 AVPacket *pkt) {
125 int chunk_type;
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 if (s->audio_chunk_size != av_get_packet(pb, pkt, s->audio_chunk_size))
139 return CHUNK_EOF;
141 pkt->stream_index = s->audio_stream_index;
142 pkt->pts = s->audio_frame_count;
144 /* audio frame maintenance */
145 if (s->audio_type != CODEC_ID_INTERPLAY_DPCM)
146 s->audio_frame_count +=
147 (s->audio_chunk_size / s->audio_channels / (s->audio_bits / 8));
148 else
149 s->audio_frame_count +=
150 (s->audio_chunk_size - 6) / s->audio_channels;
152 debug_ipmovie("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->decode_map_chunk_offset) {
159 /* send both the decode map and the video data together */
161 if (av_new_packet(pkt, s->decode_map_chunk_size + s->video_chunk_size))
162 return CHUNK_NOMEM;
164 pkt->pos= s->decode_map_chunk_offset;
165 url_fseek(pb, s->decode_map_chunk_offset, SEEK_SET);
166 s->decode_map_chunk_offset = 0;
168 if (get_buffer(pb, pkt->data, s->decode_map_chunk_size) !=
169 s->decode_map_chunk_size) {
170 av_free_packet(pkt);
171 return CHUNK_EOF;
174 url_fseek(pb, s->video_chunk_offset, SEEK_SET);
175 s->video_chunk_offset = 0;
177 if (get_buffer(pb, pkt->data + s->decode_map_chunk_size,
178 s->video_chunk_size) != s->video_chunk_size) {
179 av_free_packet(pkt);
180 return CHUNK_EOF;
183 pkt->stream_index = s->video_stream_index;
184 pkt->pts = s->video_pts;
186 debug_ipmovie("sending video frame with pts %"PRId64"\n",
187 pkt->pts);
189 s->video_pts += s->frame_pts_inc;
191 chunk_type = CHUNK_VIDEO;
193 } else {
195 url_fseek(pb, s->next_chunk_offset, SEEK_SET);
196 chunk_type = CHUNK_DONE;
200 return chunk_type;
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, ByteIOContext *pb,
206 AVPacket *pkt)
208 unsigned char chunk_preamble[CHUNK_PREAMBLE_SIZE];
209 int chunk_type;
210 int chunk_size;
211 unsigned char opcode_preamble[OPCODE_PREAMBLE_SIZE];
212 unsigned char opcode_type;
213 unsigned char opcode_version;
214 int opcode_size;
215 unsigned char scratch[1024];
216 int i, j;
217 int first_color, last_color;
218 int audio_flags;
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)
224 return chunk_type;
226 /* read the next chunk, wherever the file happens to be pointing */
227 if (url_feof(pb))
228 return CHUNK_EOF;
229 if (get_buffer(pb, chunk_preamble, CHUNK_PREAMBLE_SIZE) !=
230 CHUNK_PREAMBLE_SIZE)
231 return CHUNK_BAD;
232 chunk_size = AV_RL16(&chunk_preamble[0]);
233 chunk_type = AV_RL16(&chunk_preamble[2]);
235 debug_ipmovie("chunk type 0x%04X, 0x%04X bytes: ", chunk_type, chunk_size);
237 switch (chunk_type) {
239 case CHUNK_INIT_AUDIO:
240 debug_ipmovie("initialize audio\n");
241 break;
243 case CHUNK_AUDIO_ONLY:
244 debug_ipmovie("audio only\n");
245 break;
247 case CHUNK_INIT_VIDEO:
248 debug_ipmovie("initialize video\n");
249 break;
251 case CHUNK_VIDEO:
252 debug_ipmovie("video (and audio)\n");
253 break;
255 case CHUNK_SHUTDOWN:
256 debug_ipmovie("shutdown\n");
257 break;
259 case CHUNK_END:
260 debug_ipmovie("end\n");
261 break;
263 default:
264 debug_ipmovie("invalid chunk\n");
265 chunk_type = CHUNK_BAD;
266 break;
270 while ((chunk_size > 0) && (chunk_type != CHUNK_BAD)) {
272 /* read the next chunk, wherever the file happens to be pointing */
273 if (url_feof(pb)) {
274 chunk_type = CHUNK_EOF;
275 break;
277 if (get_buffer(pb, opcode_preamble, CHUNK_PREAMBLE_SIZE) !=
278 CHUNK_PREAMBLE_SIZE) {
279 chunk_type = CHUNK_BAD;
280 break;
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 debug_ipmovie("chunk_size countdown just went negative\n");
291 chunk_type = CHUNK_BAD;
292 break;
295 debug_ipmovie(" 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 debug_ipmovie("end of stream\n");
301 url_fseek(pb, opcode_size, SEEK_CUR);
302 break;
304 case OPCODE_END_OF_CHUNK:
305 debug_ipmovie("end of chunk\n");
306 url_fseek(pb, opcode_size, SEEK_CUR);
307 break;
309 case OPCODE_CREATE_TIMER:
310 debug_ipmovie("create timer\n");
311 if ((opcode_version > 0) || (opcode_size > 6)) {
312 debug_ipmovie("bad create_timer opcode\n");
313 chunk_type = CHUNK_BAD;
314 break;
316 if (get_buffer(pb, scratch, opcode_size) !=
317 opcode_size) {
318 chunk_type = CHUNK_BAD;
319 break;
321 s->frame_pts_inc = ((uint64_t)AV_RL32(&scratch[0])) * AV_RL16(&scratch[4]);
322 debug_ipmovie(" %.2f frames/second (timer div = %d, subdiv = %d)\n",
323 1000000.0/s->frame_pts_inc, AV_RL32(&scratch[0]), AV_RL16(&scratch[4]));
324 break;
326 case OPCODE_INIT_AUDIO_BUFFERS:
327 debug_ipmovie("initialize audio buffers\n");
328 if ((opcode_version > 1) || (opcode_size > 10)) {
329 debug_ipmovie("bad init_audio_buffers opcode\n");
330 chunk_type = CHUNK_BAD;
331 break;
333 if (get_buffer(pb, scratch, opcode_size) !=
334 opcode_size) {
335 chunk_type = CHUNK_BAD;
336 break;
338 s->audio_sample_rate = AV_RL16(&scratch[4]);
339 audio_flags = AV_RL16(&scratch[2]);
340 /* bit 0 of the flags: 0 = mono, 1 = stereo */
341 s->audio_channels = (audio_flags & 1) + 1;
342 /* bit 1 of the flags: 0 = 8 bit, 1 = 16 bit */
343 s->audio_bits = (((audio_flags >> 1) & 1) + 1) * 8;
344 /* bit 2 indicates compressed audio in version 1 opcode */
345 if ((opcode_version == 1) && (audio_flags & 0x4))
346 s->audio_type = CODEC_ID_INTERPLAY_DPCM;
347 else if (s->audio_bits == 16)
348 s->audio_type = CODEC_ID_PCM_S16LE;
349 else
350 s->audio_type = CODEC_ID_PCM_U8;
351 debug_ipmovie("audio: %d bits, %d Hz, %s, %s format\n",
352 s->audio_bits,
353 s->audio_sample_rate,
354 (s->audio_channels == 2) ? "stereo" : "mono",
355 (s->audio_type == CODEC_ID_INTERPLAY_DPCM) ?
356 "Interplay audio" : "PCM");
357 break;
359 case OPCODE_START_STOP_AUDIO:
360 debug_ipmovie("start/stop audio\n");
361 url_fseek(pb, opcode_size, SEEK_CUR);
362 break;
364 case OPCODE_INIT_VIDEO_BUFFERS:
365 debug_ipmovie("initialize video buffers\n");
366 if ((opcode_version > 2) || (opcode_size > 8)) {
367 debug_ipmovie("bad init_video_buffers opcode\n");
368 chunk_type = CHUNK_BAD;
369 break;
371 if (get_buffer(pb, scratch, opcode_size) !=
372 opcode_size) {
373 chunk_type = CHUNK_BAD;
374 break;
376 s->video_width = AV_RL16(&scratch[0]) * 8;
377 s->video_height = AV_RL16(&scratch[2]) * 8;
378 debug_ipmovie("video resolution: %d x %d\n",
379 s->video_width, s->video_height);
380 break;
382 case OPCODE_UNKNOWN_06:
383 case OPCODE_UNKNOWN_0E:
384 case OPCODE_UNKNOWN_10:
385 case OPCODE_UNKNOWN_12:
386 case OPCODE_UNKNOWN_13:
387 case OPCODE_UNKNOWN_14:
388 case OPCODE_UNKNOWN_15:
389 debug_ipmovie("unknown (but documented) opcode %02X\n", opcode_type);
390 url_fseek(pb, opcode_size, SEEK_CUR);
391 break;
393 case OPCODE_SEND_BUFFER:
394 debug_ipmovie("send buffer\n");
395 url_fseek(pb, opcode_size, SEEK_CUR);
396 break;
398 case OPCODE_AUDIO_FRAME:
399 debug_ipmovie("audio frame\n");
401 /* log position and move on for now */
402 s->audio_chunk_offset = url_ftell(pb);
403 s->audio_chunk_size = opcode_size;
404 url_fseek(pb, opcode_size, SEEK_CUR);
405 break;
407 case OPCODE_SILENCE_FRAME:
408 debug_ipmovie("silence frame\n");
409 url_fseek(pb, opcode_size, SEEK_CUR);
410 break;
412 case OPCODE_INIT_VIDEO_MODE:
413 debug_ipmovie("initialize video mode\n");
414 url_fseek(pb, opcode_size, SEEK_CUR);
415 break;
417 case OPCODE_CREATE_GRADIENT:
418 debug_ipmovie("create gradient\n");
419 url_fseek(pb, opcode_size, SEEK_CUR);
420 break;
422 case OPCODE_SET_PALETTE:
423 debug_ipmovie("set palette\n");
424 /* check for the logical maximum palette size
425 * (3 * 256 + 4 bytes) */
426 if (opcode_size > 0x304) {
427 debug_ipmovie("demux_ipmovie: set_palette opcode too large\n");
428 chunk_type = CHUNK_BAD;
429 break;
431 if (get_buffer(pb, scratch, opcode_size) != opcode_size) {
432 chunk_type = CHUNK_BAD;
433 break;
436 /* load the palette into internal data structure */
437 first_color = AV_RL16(&scratch[0]);
438 last_color = first_color + AV_RL16(&scratch[2]) - 1;
439 /* sanity check (since they are 16 bit values) */
440 if ((first_color > 0xFF) || (last_color > 0xFF)) {
441 debug_ipmovie("demux_ipmovie: set_palette indexes out of range (%d -> %d)\n",
442 first_color, last_color);
443 chunk_type = CHUNK_BAD;
444 break;
446 j = 4; /* offset of first palette data */
447 for (i = first_color; i <= last_color; i++) {
448 /* the palette is stored as a 6-bit VGA palette, thus each
449 * component is shifted up to a 8-bit range */
450 r = scratch[j++] * 4;
451 g = scratch[j++] * 4;
452 b = scratch[j++] * 4;
453 s->palette_control.palette[i] = (r << 16) | (g << 8) | (b);
455 /* indicate a palette change */
456 s->palette_control.palette_changed = 1;
457 break;
459 case OPCODE_SET_PALETTE_COMPRESSED:
460 debug_ipmovie("set palette compressed\n");
461 url_fseek(pb, opcode_size, SEEK_CUR);
462 break;
464 case OPCODE_SET_DECODING_MAP:
465 debug_ipmovie("set decoding map\n");
467 /* log position and move on for now */
468 s->decode_map_chunk_offset = url_ftell(pb);
469 s->decode_map_chunk_size = opcode_size;
470 url_fseek(pb, opcode_size, SEEK_CUR);
471 break;
473 case OPCODE_VIDEO_DATA:
474 debug_ipmovie("set video data\n");
476 /* log position and move on for now */
477 s->video_chunk_offset = url_ftell(pb);
478 s->video_chunk_size = opcode_size;
479 url_fseek(pb, opcode_size, SEEK_CUR);
480 break;
482 default:
483 debug_ipmovie("*** unknown opcode type\n");
484 chunk_type = CHUNK_BAD;
485 break;
490 /* make a note of where the stream is sitting */
491 s->next_chunk_offset = url_ftell(pb);
493 /* dispatch the first of any pending packets */
494 if ((chunk_type == CHUNK_VIDEO) || (chunk_type == CHUNK_AUDIO_ONLY))
495 chunk_type = load_ipmovie_packet(s, pb, pkt);
497 return chunk_type;
500 static const char signature[] = "Interplay MVE File\x1A\0\x1A";
502 static int ipmovie_probe(AVProbeData *p)
504 uint8_t *b = p->buf;
505 uint8_t *b_end = p->buf + p->buf_size - sizeof(signature);
506 do {
507 if (memcmp(b++, signature, sizeof(signature)) == 0)
508 return AVPROBE_SCORE_MAX;
509 } while (b < b_end);
511 return 0;
514 static int ipmovie_read_header(AVFormatContext *s,
515 AVFormatParameters *ap)
517 IPMVEContext *ipmovie = s->priv_data;
518 ByteIOContext *pb = s->pb;
519 AVPacket pkt;
520 AVStream *st;
521 unsigned char chunk_preamble[CHUNK_PREAMBLE_SIZE];
522 int chunk_type;
523 uint8_t signature_buffer[sizeof(signature)];
525 get_buffer(pb, signature_buffer, sizeof(signature_buffer));
526 while (memcmp(signature_buffer, signature, sizeof(signature))) {
527 memmove(signature_buffer, signature_buffer + 1, sizeof(signature_buffer) - 1);
528 signature_buffer[sizeof(signature_buffer) - 1] = get_byte(pb);
529 if (url_feof(pb))
530 return AVERROR_EOF;
532 /* initialize private context members */
533 ipmovie->video_pts = ipmovie->audio_frame_count = 0;
534 ipmovie->audio_chunk_offset = ipmovie->video_chunk_offset =
535 ipmovie->decode_map_chunk_offset = 0;
537 /* on the first read, this will position the stream at the first chunk */
538 ipmovie->next_chunk_offset = url_ftell(pb) + 4;
540 /* process the first chunk which should be CHUNK_INIT_VIDEO */
541 if (process_ipmovie_chunk(ipmovie, pb, &pkt) != CHUNK_INIT_VIDEO)
542 return AVERROR_INVALIDDATA;
544 /* peek ahead to the next chunk-- if it is an init audio chunk, process
545 * it; if it is the first video chunk, this is a silent file */
546 if (get_buffer(pb, chunk_preamble, CHUNK_PREAMBLE_SIZE) !=
547 CHUNK_PREAMBLE_SIZE)
548 return AVERROR(EIO);
549 chunk_type = AV_RL16(&chunk_preamble[2]);
550 url_fseek(pb, -CHUNK_PREAMBLE_SIZE, SEEK_CUR);
552 if (chunk_type == CHUNK_VIDEO)
553 ipmovie->audio_type = CODEC_ID_NONE; /* no audio */
554 else if (process_ipmovie_chunk(ipmovie, pb, &pkt) != CHUNK_INIT_AUDIO)
555 return AVERROR_INVALIDDATA;
557 /* initialize the stream decoders */
558 st = av_new_stream(s, 0);
559 if (!st)
560 return AVERROR(ENOMEM);
561 av_set_pts_info(st, 63, 1, 1000000);
562 ipmovie->video_stream_index = st->index;
563 st->codec->codec_type = CODEC_TYPE_VIDEO;
564 st->codec->codec_id = CODEC_ID_INTERPLAY_VIDEO;
565 st->codec->codec_tag = 0; /* no fourcc */
566 st->codec->width = ipmovie->video_width;
567 st->codec->height = ipmovie->video_height;
569 /* palette considerations */
570 st->codec->palctrl = &ipmovie->palette_control;
572 if (ipmovie->audio_type) {
573 st = av_new_stream(s, 0);
574 if (!st)
575 return AVERROR(ENOMEM);
576 av_set_pts_info(st, 32, 1, ipmovie->audio_sample_rate);
577 ipmovie->audio_stream_index = st->index;
578 st->codec->codec_type = CODEC_TYPE_AUDIO;
579 st->codec->codec_id = ipmovie->audio_type;
580 st->codec->codec_tag = 0; /* no tag */
581 st->codec->channels = ipmovie->audio_channels;
582 st->codec->sample_rate = ipmovie->audio_sample_rate;
583 st->codec->bits_per_coded_sample = ipmovie->audio_bits;
584 st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
585 st->codec->bits_per_coded_sample;
586 if (st->codec->codec_id == CODEC_ID_INTERPLAY_DPCM)
587 st->codec->bit_rate /= 2;
588 st->codec->block_align = st->codec->channels * st->codec->bits_per_coded_sample;
591 return 0;
594 static int ipmovie_read_packet(AVFormatContext *s,
595 AVPacket *pkt)
597 IPMVEContext *ipmovie = s->priv_data;
598 ByteIOContext *pb = s->pb;
599 int ret;
601 ret = process_ipmovie_chunk(ipmovie, pb, pkt);
602 if (ret == CHUNK_BAD)
603 ret = AVERROR_INVALIDDATA;
604 else if (ret == CHUNK_EOF)
605 ret = AVERROR(EIO);
606 else if (ret == CHUNK_NOMEM)
607 ret = AVERROR(ENOMEM);
608 else if (ret == CHUNK_VIDEO)
609 ret = 0;
610 else
611 ret = -1;
613 return ret;
616 AVInputFormat ipmovie_demuxer = {
617 "ipmovie",
618 NULL_IF_CONFIG_SMALL("Interplay MVE format"),
619 sizeof(IPMVEContext),
620 ipmovie_probe,
621 ipmovie_read_header,
622 ipmovie_read_packet,