]> git.sesse.net Git - ffmpeg/blob - libavformat/ipmovie.c
Interplay MVE: Refactor IP packet format
[ffmpeg] / libavformat / ipmovie.c
1 /*
2  * Interplay MVE File Demuxer
3  * Copyright (c) 2003 The FFmpeg project
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 /**
23  * @file
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.
33  */
34
35 #include "libavutil/channel_layout.h"
36 #include "libavutil/intreadwrite.h"
37 #include "avformat.h"
38 #include "internal.h"
39
40 #define CHUNK_PREAMBLE_SIZE 4
41 #define OPCODE_PREAMBLE_SIZE 4
42
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
54
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
77
78 #define PALETTE_COUNT 256
79
80 typedef struct IPMVEContext {
81     AVFormatContext *avf;
82     unsigned char *buf;
83     int buf_size;
84
85     uint64_t frame_pts_inc;
86
87     unsigned int video_bpp;
88     unsigned int video_width;
89     unsigned int video_height;
90     int64_t video_pts;
91     uint32_t     palette[256];
92     int          has_palette;
93     int          changed;
94     uint8_t      send_buffer;
95     uint8_t      frame_format;
96
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;
102
103     int video_stream_index;
104     int audio_stream_index;
105
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;
112
113     int64_t next_chunk_offset;
114
115 } IPMVEContext;
116
117 static int load_ipmovie_packet(IPMVEContext *s, AVIOContext *pb,
118     AVPacket *pkt) {
119
120     int chunk_type;
121
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");
126                 return CHUNK_BAD;
127         }
128
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;
133         }
134
135         avio_seek(pb, s->audio_chunk_offset, SEEK_SET);
136         s->audio_chunk_offset = 0;
137
138         if (s->audio_chunk_size != av_get_packet(pb, pkt, s->audio_chunk_size))
139             return CHUNK_EOF;
140
141         pkt->stream_index = s->audio_stream_index;
142         pkt->pts = s->audio_frame_count;
143
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));
148         else
149             s->audio_frame_count +=
150                 (s->audio_chunk_size - 6 - s->audio_channels) / s->audio_channels;
151
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);
154
155         chunk_type = CHUNK_VIDEO;
156
157     } else if (s->frame_format) {
158
159         /* send the frame format, decode map, the video data, and the send_buffer flag together */
160
161         if (av_new_packet(pkt, 6 + s->decode_map_chunk_size + s->video_chunk_size))
162             return CHUNK_NOMEM;
163
164         if (s->has_palette) {
165             uint8_t *pal;
166
167             pal = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE,
168                                           AVPALETTE_SIZE);
169             if (pal) {
170                 memcpy(pal, s->palette, AVPALETTE_SIZE);
171                 s->has_palette = 0;
172             }
173         }
174
175         if (s->changed) {
176             ff_add_param_change(pkt, 0, 0, 0, s->video_width, s->video_height);
177             s->changed = 0;
178         }
179
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);
184
185         s->frame_format = 0;
186         s->send_buffer = 0;
187
188         pkt->pos = s->video_chunk_offset;
189         avio_seek(pb, s->video_chunk_offset, SEEK_SET);
190         s->video_chunk_offset = 0;
191
192         if (avio_read(pb, pkt->data + 6, s->video_chunk_size) !=
193             s->video_chunk_size) {
194             av_packet_unref(pkt);
195             return CHUNK_EOF;
196         }
197
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;
201
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);
205             return CHUNK_EOF;
206         }
207
208         s->video_chunk_size = 0;
209         s->decode_map_chunk_size = 0;
210
211         pkt->stream_index = s->video_stream_index;
212         pkt->pts = s->video_pts;
213
214         av_log(s->avf, AV_LOG_TRACE, "sending video frame with pts %"PRId64"\n", pkt->pts);
215
216         s->video_pts += s->frame_pts_inc;
217
218         chunk_type = CHUNK_VIDEO;
219
220     } else {
221
222         avio_seek(pb, s->next_chunk_offset, SEEK_SET);
223         chunk_type = CHUNK_DONE;
224
225     }
226
227     return chunk_type;
228 }
229
230 static int init_audio(AVFormatContext *s)
231 {
232     IPMVEContext *ipmovie = s->priv_data;
233     AVStream *st = avformat_new_stream(s, NULL);
234     if (!st)
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 :
243                                                             AV_CH_LAYOUT_STEREO;
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;
251
252     return 0;
253 }
254
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,
258     AVPacket *pkt)
259 {
260     unsigned char chunk_preamble[CHUNK_PREAMBLE_SIZE];
261     int chunk_type;
262     int chunk_size;
263     unsigned char opcode_preamble[OPCODE_PREAMBLE_SIZE];
264     unsigned char opcode_type;
265     unsigned char opcode_version;
266     int opcode_size;
267     unsigned char scratch[1024];
268     int i, j;
269     int first_color, last_color;
270     int audio_flags;
271     unsigned char r, g, b;
272     unsigned int width, height;
273
274     /* see if there are any pending packets */
275     chunk_type = load_ipmovie_packet(s, pb, pkt);
276     if (chunk_type != CHUNK_DONE)
277         return chunk_type;
278
279     /* read the next chunk, wherever the file happens to be pointing */
280     if (avio_feof(pb))
281         return CHUNK_EOF;
282     if (avio_read(pb, chunk_preamble, CHUNK_PREAMBLE_SIZE) !=
283         CHUNK_PREAMBLE_SIZE)
284         return CHUNK_BAD;
285     chunk_size = AV_RL16(&chunk_preamble[0]);
286     chunk_type = AV_RL16(&chunk_preamble[2]);
287
288     av_log(s->avf, AV_LOG_TRACE, "chunk type 0x%04X, 0x%04X bytes: ", chunk_type, chunk_size);
289
290     switch (chunk_type) {
291
292     case CHUNK_INIT_AUDIO:
293         av_log(s->avf, AV_LOG_TRACE, "initialize audio\n");
294         break;
295
296     case CHUNK_AUDIO_ONLY:
297         av_log(s->avf, AV_LOG_TRACE, "audio only\n");
298         break;
299
300     case CHUNK_INIT_VIDEO:
301         av_log(s->avf, AV_LOG_TRACE, "initialize video\n");
302         break;
303
304     case CHUNK_VIDEO:
305         av_log(s->avf, AV_LOG_TRACE, "video (and audio)\n");
306         break;
307
308     case CHUNK_SHUTDOWN:
309         av_log(s->avf, AV_LOG_TRACE, "shutdown\n");
310         break;
311
312     case CHUNK_END:
313         av_log(s->avf, AV_LOG_TRACE, "end\n");
314         break;
315
316     default:
317         av_log(s->avf, AV_LOG_TRACE, "invalid chunk\n");
318         chunk_type = CHUNK_BAD;
319         break;
320
321     }
322
323     while ((chunk_size > 0) && (chunk_type != CHUNK_BAD)) {
324
325         /* read the next chunk, wherever the file happens to be pointing */
326         if (avio_feof(pb)) {
327             chunk_type = CHUNK_EOF;
328             break;
329         }
330         if (avio_read(pb, opcode_preamble, CHUNK_PREAMBLE_SIZE) !=
331             CHUNK_PREAMBLE_SIZE) {
332             chunk_type = CHUNK_BAD;
333             break;
334         }
335
336         opcode_size = AV_RL16(&opcode_preamble[0]);
337         opcode_type = opcode_preamble[2];
338         opcode_version = opcode_preamble[3];
339
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;
345             break;
346         }
347
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) {
351
352         case OPCODE_END_OF_STREAM:
353             av_log(s->avf, AV_LOG_TRACE, "end of stream\n");
354             avio_skip(pb, opcode_size);
355             break;
356
357         case OPCODE_END_OF_CHUNK:
358             av_log(s->avf, AV_LOG_TRACE, "end of chunk\n");
359             avio_skip(pb, opcode_size);
360             break;
361
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;
367                 break;
368             }
369             if (avio_read(pb, scratch, opcode_size) !=
370                 opcode_size) {
371                 chunk_type = CHUNK_BAD;
372                 break;
373             }
374             s->frame_pts_inc = ((uint64_t)AV_RL32(&scratch[0])) * AV_RL16(&scratch[4]);
375             break;
376
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;
382                 break;
383             }
384             if (avio_read(pb, scratch, opcode_size) !=
385                 opcode_size) {
386                 chunk_type = CHUNK_BAD;
387                 break;
388             }
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;
400             else
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");
407             break;
408
409         case OPCODE_START_STOP_AUDIO:
410             av_log(s->avf, AV_LOG_TRACE, "start/stop audio\n");
411             avio_skip(pb, opcode_size);
412             break;
413
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
418             ) {
419                 av_log(s->avf, AV_LOG_TRACE, "bad init_video_buffers opcode\n");
420                 chunk_type = CHUNK_BAD;
421                 break;
422             }
423             if (avio_read(pb, scratch, opcode_size) !=
424                 opcode_size) {
425                 chunk_type = CHUNK_BAD;
426                 break;
427             }
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;
432                 s->changed++;
433             }
434             if (height != s->video_height) {
435                 s->video_height = height;
436                 s->changed++;
437             }
438             if (opcode_version < 2 || !AV_RL16(&scratch[6])) {
439                 s->video_bpp = 8;
440             } else {
441                 s->video_bpp = 16;
442             }
443             av_log(s->avf, AV_LOG_TRACE, "video resolution: %d x %d\n",
444                     s->video_width, s->video_height);
445             break;
446
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);
456             break;
457
458         case OPCODE_SEND_BUFFER:
459             av_log(s->avf, AV_LOG_TRACE, "send buffer\n");
460             avio_skip(pb, opcode_size);
461             s->send_buffer = 1;
462             break;
463
464         case OPCODE_AUDIO_FRAME:
465             av_log(s->avf, AV_LOG_TRACE, "audio frame\n");
466
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);
471             break;
472
473         case OPCODE_SILENCE_FRAME:
474             av_log(s->avf, AV_LOG_TRACE, "silence frame\n");
475             avio_skip(pb, opcode_size);
476             break;
477
478         case OPCODE_INIT_VIDEO_MODE:
479             av_log(s->avf, AV_LOG_TRACE, "initialize video mode\n");
480             avio_skip(pb, opcode_size);
481             break;
482
483         case OPCODE_CREATE_GRADIENT:
484             av_log(s->avf, AV_LOG_TRACE, "create gradient\n");
485             avio_skip(pb, opcode_size);
486             break;
487
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;
495                 break;
496             }
497             if (avio_read(pb, scratch, opcode_size) != opcode_size) {
498                 chunk_type = CHUNK_BAD;
499                 break;
500             }
501
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;
511                 break;
512             }
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;
522             }
523             s->has_palette = 1;
524             break;
525
526         case OPCODE_SET_PALETTE_COMPRESSED:
527             av_log(s->avf, AV_LOG_TRACE, "set palette compressed\n");
528             avio_skip(pb, opcode_size);
529             break;
530
531         case OPCODE_SET_DECODING_MAP:
532             av_log(s->avf, AV_LOG_TRACE, "set decoding map\n");
533
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);
538             break;
539
540         case OPCODE_VIDEO_DATA_11:
541             av_log(s->avf, AV_LOG_TRACE, "set video data\n");
542             s->frame_format = 0x11;
543
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);
548             break;
549
550         default:
551             av_log(s->avf, AV_LOG_TRACE, "*** unknown opcode type\n");
552             chunk_type = CHUNK_BAD;
553             break;
554
555         }
556     }
557
558     if (s->avf->nb_streams == 1 && s->audio_type)
559         init_audio(s->avf);
560
561     /* make a note of where the stream is sitting */
562     s->next_chunk_offset = avio_tell(pb);
563
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);
567
568     return chunk_type;
569 }
570
571 static const char signature[] = "Interplay MVE File\x1A\0\x1A";
572
573 static int ipmovie_probe(AVProbeData *p)
574 {
575     const uint8_t *b = p->buf;
576     const uint8_t *b_end = p->buf + p->buf_size - sizeof(signature);
577     do {
578         if (b[0] == signature[0] && memcmp(b, signature, sizeof(signature)) == 0)
579             return AVPROBE_SCORE_MAX;
580         b++;
581     } while (b < b_end);
582
583     return 0;
584 }
585
586 static int ipmovie_read_header(AVFormatContext *s)
587 {
588     IPMVEContext *ipmovie = s->priv_data;
589     AVIOContext *pb = s->pb;
590     AVPacket pkt;
591     AVStream *st;
592     unsigned char chunk_preamble[CHUNK_PREAMBLE_SIZE];
593     int chunk_type, i;
594     uint8_t signature_buffer[sizeof(signature)];
595
596     ipmovie->avf = s;
597
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);
602         if (avio_feof(pb))
603             return AVERROR_EOF;
604     }
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;
611
612     /* on the first read, this will position the stream at the first chunk */
613     ipmovie->next_chunk_offset = avio_tell(pb) + 4;
614
615     for (i = 0; i < 256; i++)
616         ipmovie->palette[i] = 0xFFU << 24;
617
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;
621
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) !=
625         CHUNK_PREAMBLE_SIZE)
626         return AVERROR(EIO);
627     chunk_type = AV_RL16(&chunk_preamble[2]);
628     avio_seek(pb, -CHUNK_PREAMBLE_SIZE, SEEK_CUR);
629
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;
634
635     /* initialize the stream decoders */
636     st = avformat_new_stream(s, NULL);
637     if (!st)
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;
647
648     if (ipmovie->audio_type) {
649         return init_audio(s);
650     } else
651        s->ctx_flags |= AVFMTCTX_NOHEADER;
652
653     return 0;
654 }
655
656 static int ipmovie_read_packet(AVFormatContext *s,
657                                AVPacket *pkt)
658 {
659     IPMVEContext *ipmovie = s->priv_data;
660     AVIOContext *pb = s->pb;
661     int ret;
662
663     for (;;) {
664     ret = process_ipmovie_chunk(ipmovie, pb, pkt);
665     if (ret == CHUNK_BAD)
666         ret = AVERROR_INVALIDDATA;
667     else if (ret == CHUNK_EOF)
668         ret = AVERROR(EIO);
669     else if (ret == CHUNK_NOMEM)
670         ret = AVERROR(ENOMEM);
671     else if (ret == CHUNK_END || ret == CHUNK_SHUTDOWN)
672         ret = AVERROR_EOF;
673     else if (ret == CHUNK_VIDEO)
674         ret = 0;
675     else if (ret == CHUNK_INIT_VIDEO || ret == CHUNK_INIT_AUDIO)
676         continue;
677     else
678         continue;
679
680     return ret;
681     }
682 }
683
684 AVInputFormat ff_ipmovie_demuxer = {
685     .name           = "ipmovie",
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,
691 };