]> git.sesse.net Git - ffmpeg/blob - libavformat/ipmovie.c
avformat/ipmovie: Remove redundant av_packet_unref()
[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_VIDEO_DATA_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_SET_SKIP_MAP            0x0E
70 #define OPCODE_SET_DECODING_MAP        0x0F
71 #define OPCODE_VIDEO_DATA_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 skip_map_chunk_offset;
111     int skip_map_chunk_size;
112     int64_t decode_map_chunk_offset;
113     int decode_map_chunk_size;
114
115     int64_t next_chunk_offset;
116
117 } IPMVEContext;
118
119 static int load_ipmovie_packet(IPMVEContext *s, AVIOContext *pb,
120     AVPacket *pkt) {
121
122     int chunk_type;
123
124     if (s->audio_chunk_offset && s->audio_channels && s->audio_bits) {
125         if (s->audio_type == AV_CODEC_ID_NONE) {
126             av_log(s->avf, AV_LOG_ERROR, "Can not read audio packet before"
127                    "audio codec is known\n");
128                 return CHUNK_BAD;
129         }
130
131         /* adjust for PCM audio by skipping chunk header */
132         if (s->audio_type != AV_CODEC_ID_INTERPLAY_DPCM) {
133             s->audio_chunk_offset += 6;
134             s->audio_chunk_size -= 6;
135         }
136
137         avio_seek(pb, s->audio_chunk_offset, SEEK_SET);
138         s->audio_chunk_offset = 0;
139
140         if (s->audio_chunk_size != av_get_packet(pb, pkt, s->audio_chunk_size))
141             return CHUNK_EOF;
142
143         pkt->stream_index = s->audio_stream_index;
144         pkt->pts = s->audio_frame_count;
145
146         /* audio frame maintenance */
147         if (s->audio_type != AV_CODEC_ID_INTERPLAY_DPCM)
148             s->audio_frame_count +=
149             (s->audio_chunk_size / s->audio_channels / (s->audio_bits / 8));
150         else
151             s->audio_frame_count +=
152                 (s->audio_chunk_size - 6 - s->audio_channels) / s->audio_channels;
153
154         av_log(s->avf, AV_LOG_TRACE, "sending audio frame with pts %"PRId64" (%d audio frames)\n",
155                 pkt->pts, s->audio_frame_count);
156
157         chunk_type = CHUNK_VIDEO;
158
159     } else if (s->frame_format) {
160
161         /* send the frame format, decode map, the video data, skip map, and the send_buffer flag together */
162
163         if (av_new_packet(pkt, 8 + s->decode_map_chunk_size + s->video_chunk_size + s->skip_map_chunk_size))
164             return CHUNK_NOMEM;
165
166         if (s->has_palette) {
167             uint8_t *pal;
168
169             pal = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE,
170                                           AVPALETTE_SIZE);
171             if (pal) {
172                 memcpy(pal, s->palette, AVPALETTE_SIZE);
173                 s->has_palette = 0;
174             }
175         }
176
177         if (s->changed) {
178             ff_add_param_change(pkt, 0, 0, 0, s->video_width, s->video_height);
179             s->changed = 0;
180         }
181
182         AV_WL8(pkt->data, s->frame_format);
183         AV_WL8(pkt->data + 1, s->send_buffer);
184         AV_WL16(pkt->data + 2, s->video_chunk_size);
185         AV_WL16(pkt->data + 4, s->decode_map_chunk_size);
186         AV_WL16(pkt->data + 6, s->skip_map_chunk_size);
187
188         s->frame_format = 0;
189         s->send_buffer = 0;
190
191         pkt->pos = s->video_chunk_offset;
192         avio_seek(pb, s->video_chunk_offset, SEEK_SET);
193         s->video_chunk_offset = 0;
194
195         if (avio_read(pb, pkt->data + 8, s->video_chunk_size) !=
196             s->video_chunk_size) {
197             return CHUNK_EOF;
198         }
199
200         if (s->decode_map_chunk_size) {
201             pkt->pos = s->decode_map_chunk_offset;
202             avio_seek(pb, s->decode_map_chunk_offset, SEEK_SET);
203             s->decode_map_chunk_offset = 0;
204
205             if (avio_read(pb, pkt->data + 8 + s->video_chunk_size,
206                 s->decode_map_chunk_size) != s->decode_map_chunk_size) {
207                 return CHUNK_EOF;
208             }
209         }
210
211         if (s->skip_map_chunk_size) {
212             pkt->pos = s->skip_map_chunk_offset;
213             avio_seek(pb, s->skip_map_chunk_offset, SEEK_SET);
214             s->skip_map_chunk_offset = 0;
215
216             if (avio_read(pb, pkt->data + 8 + s->video_chunk_size + s->decode_map_chunk_size,
217                 s->skip_map_chunk_size) != s->skip_map_chunk_size) {
218                 return CHUNK_EOF;
219             }
220         }
221
222         s->video_chunk_size = 0;
223         s->decode_map_chunk_size = 0;
224         s->skip_map_chunk_size = 0;
225
226         pkt->stream_index = s->video_stream_index;
227         pkt->pts = s->video_pts;
228
229         av_log(s->avf, AV_LOG_TRACE, "sending video frame with pts %"PRId64"\n", pkt->pts);
230
231         s->video_pts += s->frame_pts_inc;
232
233         chunk_type = CHUNK_VIDEO;
234
235     } else {
236
237         avio_seek(pb, s->next_chunk_offset, SEEK_SET);
238         chunk_type = CHUNK_DONE;
239
240     }
241
242     return chunk_type;
243 }
244
245 static int init_audio(AVFormatContext *s)
246 {
247     IPMVEContext *ipmovie = s->priv_data;
248     AVStream *st = avformat_new_stream(s, NULL);
249     if (!st)
250         return AVERROR(ENOMEM);
251     avpriv_set_pts_info(st, 32, 1, ipmovie->audio_sample_rate);
252     ipmovie->audio_stream_index = st->index;
253     st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
254     st->codecpar->codec_id = ipmovie->audio_type;
255     st->codecpar->codec_tag = 0;  /* no tag */
256     st->codecpar->channels = ipmovie->audio_channels;
257     st->codecpar->channel_layout = st->codecpar->channels == 1 ? AV_CH_LAYOUT_MONO :
258                                                             AV_CH_LAYOUT_STEREO;
259     st->codecpar->sample_rate = ipmovie->audio_sample_rate;
260     st->codecpar->bits_per_coded_sample = ipmovie->audio_bits;
261     st->codecpar->bit_rate = st->codecpar->channels * st->codecpar->sample_rate *
262         st->codecpar->bits_per_coded_sample;
263     if (st->codecpar->codec_id == AV_CODEC_ID_INTERPLAY_DPCM)
264         st->codecpar->bit_rate /= 2;
265     st->codecpar->block_align = st->codecpar->channels * st->codecpar->bits_per_coded_sample;
266
267     return 0;
268 }
269
270 /* This function loads and processes a single chunk in an IP movie file.
271  * It returns the type of chunk that was processed. */
272 static int process_ipmovie_chunk(IPMVEContext *s, AVIOContext *pb,
273     AVPacket *pkt)
274 {
275     unsigned char chunk_preamble[CHUNK_PREAMBLE_SIZE];
276     int chunk_type;
277     int chunk_size;
278     unsigned char opcode_preamble[OPCODE_PREAMBLE_SIZE];
279     unsigned char opcode_type;
280     unsigned char opcode_version;
281     int opcode_size;
282     unsigned char scratch[1024];
283     int i, j;
284     int first_color, last_color;
285     int audio_flags;
286     unsigned char r, g, b;
287     unsigned int width, height;
288
289     /* see if there are any pending packets */
290     chunk_type = load_ipmovie_packet(s, pb, pkt);
291     if (chunk_type != CHUNK_DONE)
292         return chunk_type;
293
294     /* read the next chunk, wherever the file happens to be pointing */
295     if (avio_feof(pb))
296         return CHUNK_EOF;
297     if (avio_read(pb, chunk_preamble, CHUNK_PREAMBLE_SIZE) !=
298         CHUNK_PREAMBLE_SIZE)
299         return CHUNK_BAD;
300     chunk_size = AV_RL16(&chunk_preamble[0]);
301     chunk_type = AV_RL16(&chunk_preamble[2]);
302
303     av_log(s->avf, AV_LOG_TRACE, "chunk type 0x%04X, 0x%04X bytes: ", chunk_type, chunk_size);
304
305     switch (chunk_type) {
306
307     case CHUNK_INIT_AUDIO:
308         av_log(s->avf, AV_LOG_TRACE, "initialize audio\n");
309         break;
310
311     case CHUNK_AUDIO_ONLY:
312         av_log(s->avf, AV_LOG_TRACE, "audio only\n");
313         break;
314
315     case CHUNK_INIT_VIDEO:
316         av_log(s->avf, AV_LOG_TRACE, "initialize video\n");
317         break;
318
319     case CHUNK_VIDEO:
320         av_log(s->avf, AV_LOG_TRACE, "video (and audio)\n");
321         break;
322
323     case CHUNK_SHUTDOWN:
324         av_log(s->avf, AV_LOG_TRACE, "shutdown\n");
325         break;
326
327     case CHUNK_END:
328         av_log(s->avf, AV_LOG_TRACE, "end\n");
329         break;
330
331     default:
332         av_log(s->avf, AV_LOG_TRACE, "invalid chunk\n");
333         chunk_type = CHUNK_BAD;
334         break;
335
336     }
337
338     while ((chunk_size > 0) && (chunk_type != CHUNK_BAD)) {
339
340         /* read the next chunk, wherever the file happens to be pointing */
341         if (avio_feof(pb)) {
342             chunk_type = CHUNK_EOF;
343             break;
344         }
345         if (avio_read(pb, opcode_preamble, CHUNK_PREAMBLE_SIZE) !=
346             CHUNK_PREAMBLE_SIZE) {
347             chunk_type = CHUNK_BAD;
348             break;
349         }
350
351         opcode_size = AV_RL16(&opcode_preamble[0]);
352         opcode_type = opcode_preamble[2];
353         opcode_version = opcode_preamble[3];
354
355         chunk_size -= OPCODE_PREAMBLE_SIZE;
356         chunk_size -= opcode_size;
357         if (chunk_size < 0) {
358             av_log(s->avf, AV_LOG_TRACE, "chunk_size countdown just went negative\n");
359             chunk_type = CHUNK_BAD;
360             break;
361         }
362
363         av_log(s->avf, AV_LOG_TRACE, "  opcode type %02X, version %d, 0x%04X bytes: ",
364                 opcode_type, opcode_version, opcode_size);
365         switch (opcode_type) {
366
367         case OPCODE_END_OF_STREAM:
368             av_log(s->avf, AV_LOG_TRACE, "end of stream\n");
369             avio_skip(pb, opcode_size);
370             break;
371
372         case OPCODE_END_OF_CHUNK:
373             av_log(s->avf, AV_LOG_TRACE, "end of chunk\n");
374             avio_skip(pb, opcode_size);
375             break;
376
377         case OPCODE_CREATE_TIMER:
378             av_log(s->avf, AV_LOG_TRACE, "create timer\n");
379             if ((opcode_version > 0) || (opcode_size != 6)) {
380                 av_log(s->avf, AV_LOG_TRACE, "bad create_timer 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->frame_pts_inc = ((uint64_t)AV_RL32(&scratch[0])) * AV_RL16(&scratch[4]);
390             break;
391
392         case OPCODE_INIT_AUDIO_BUFFERS:
393             av_log(s->avf, AV_LOG_TRACE, "initialize audio buffers\n");
394             if (opcode_version > 1 || opcode_size > 10 || opcode_size < 6) {
395                 av_log(s->avf, AV_LOG_TRACE, "bad init_audio_buffers opcode\n");
396                 chunk_type = CHUNK_BAD;
397                 break;
398             }
399             if (avio_read(pb, scratch, opcode_size) !=
400                 opcode_size) {
401                 chunk_type = CHUNK_BAD;
402                 break;
403             }
404             s->audio_sample_rate = AV_RL16(&scratch[4]);
405             audio_flags = AV_RL16(&scratch[2]);
406             /* bit 0 of the flags: 0 = mono, 1 = stereo */
407             s->audio_channels = (audio_flags & 1) + 1;
408             /* bit 1 of the flags: 0 = 8 bit, 1 = 16 bit */
409             s->audio_bits = (((audio_flags >> 1) & 1) + 1) * 8;
410             /* bit 2 indicates compressed audio in version 1 opcode */
411             if ((opcode_version == 1) && (audio_flags & 0x4))
412                 s->audio_type = AV_CODEC_ID_INTERPLAY_DPCM;
413             else if (s->audio_bits == 16)
414                 s->audio_type = AV_CODEC_ID_PCM_S16LE;
415             else
416                 s->audio_type = AV_CODEC_ID_PCM_U8;
417             av_log(s->avf, AV_LOG_TRACE, "audio: %d bits, %d Hz, %s, %s format\n",
418                     s->audio_bits, s->audio_sample_rate,
419                     (s->audio_channels == 2) ? "stereo" : "mono",
420                     (s->audio_type == AV_CODEC_ID_INTERPLAY_DPCM) ?
421                     "Interplay audio" : "PCM");
422             break;
423
424         case OPCODE_START_STOP_AUDIO:
425             av_log(s->avf, AV_LOG_TRACE, "start/stop audio\n");
426             avio_skip(pb, opcode_size);
427             break;
428
429         case OPCODE_INIT_VIDEO_BUFFERS:
430             av_log(s->avf, AV_LOG_TRACE, "initialize video buffers\n");
431             if ((opcode_version > 2) || (opcode_size > 8) || opcode_size < 4
432                 || opcode_version == 2 && opcode_size < 8
433             ) {
434                 av_log(s->avf, AV_LOG_TRACE, "bad init_video_buffers opcode\n");
435                 chunk_type = CHUNK_BAD;
436                 break;
437             }
438             if (avio_read(pb, scratch, opcode_size) !=
439                 opcode_size) {
440                 chunk_type = CHUNK_BAD;
441                 break;
442             }
443             width  = AV_RL16(&scratch[0]) * 8;
444             height = AV_RL16(&scratch[2]) * 8;
445             if (width != s->video_width) {
446                 s->video_width = width;
447                 s->changed++;
448             }
449             if (height != s->video_height) {
450                 s->video_height = height;
451                 s->changed++;
452             }
453             if (opcode_version < 2 || !AV_RL16(&scratch[6])) {
454                 s->video_bpp = 8;
455             } else {
456                 s->video_bpp = 16;
457             }
458             av_log(s->avf, AV_LOG_TRACE, "video resolution: %d x %d\n",
459                     s->video_width, s->video_height);
460             break;
461
462         case OPCODE_UNKNOWN_12:
463         case OPCODE_UNKNOWN_13:
464         case OPCODE_UNKNOWN_14:
465         case OPCODE_UNKNOWN_15:
466             av_log(s->avf, AV_LOG_TRACE, "unknown (but documented) opcode %02X\n", opcode_type);
467             avio_skip(pb, opcode_size);
468             break;
469
470         case OPCODE_SEND_BUFFER:
471             av_log(s->avf, AV_LOG_TRACE, "send buffer\n");
472             avio_skip(pb, opcode_size);
473             s->send_buffer = 1;
474             break;
475
476         case OPCODE_AUDIO_FRAME:
477             av_log(s->avf, AV_LOG_TRACE, "audio frame\n");
478
479             /* log position and move on for now */
480             s->audio_chunk_offset = avio_tell(pb);
481             s->audio_chunk_size = opcode_size;
482             avio_skip(pb, opcode_size);
483             break;
484
485         case OPCODE_SILENCE_FRAME:
486             av_log(s->avf, AV_LOG_TRACE, "silence frame\n");
487             avio_skip(pb, opcode_size);
488             break;
489
490         case OPCODE_INIT_VIDEO_MODE:
491             av_log(s->avf, AV_LOG_TRACE, "initialize video mode\n");
492             avio_skip(pb, opcode_size);
493             break;
494
495         case OPCODE_CREATE_GRADIENT:
496             av_log(s->avf, AV_LOG_TRACE, "create gradient\n");
497             avio_skip(pb, opcode_size);
498             break;
499
500         case OPCODE_SET_PALETTE:
501             av_log(s->avf, AV_LOG_TRACE, "set palette\n");
502             /* check for the logical maximum palette size
503              * (3 * 256 + 4 bytes) */
504             if (opcode_size > 0x304 || opcode_size < 4) {
505                 av_log(s->avf, AV_LOG_TRACE, "demux_ipmovie: set_palette opcode with invalid size\n");
506                 chunk_type = CHUNK_BAD;
507                 break;
508             }
509             if (avio_read(pb, scratch, opcode_size) != opcode_size) {
510                 chunk_type = CHUNK_BAD;
511                 break;
512             }
513
514             /* load the palette into internal data structure */
515             first_color = AV_RL16(&scratch[0]);
516             last_color = first_color + AV_RL16(&scratch[2]) - 1;
517             /* sanity check (since they are 16 bit values) */
518             if (   (first_color > 0xFF) || (last_color > 0xFF)
519                 || (last_color - first_color + 1)*3 + 4 > opcode_size) {
520                 av_log(s->avf, AV_LOG_TRACE, "demux_ipmovie: set_palette indexes out of range (%d -> %d)\n",
521                     first_color, last_color);
522                 chunk_type = CHUNK_BAD;
523                 break;
524             }
525             j = 4;  /* offset of first palette data */
526             for (i = first_color; i <= last_color; i++) {
527                 /* the palette is stored as a 6-bit VGA palette, thus each
528                  * component is shifted up to a 8-bit range */
529                 r = scratch[j++] * 4;
530                 g = scratch[j++] * 4;
531                 b = scratch[j++] * 4;
532                 s->palette[i] = (0xFFU << 24) | (r << 16) | (g << 8) | (b);
533                 s->palette[i] |= s->palette[i] >> 6 & 0x30303;
534             }
535             s->has_palette = 1;
536             break;
537
538         case OPCODE_SET_PALETTE_COMPRESSED:
539             av_log(s->avf, AV_LOG_TRACE, "set palette compressed\n");
540             avio_skip(pb, opcode_size);
541             break;
542
543         case OPCODE_SET_SKIP_MAP:
544             av_log(s->avf, AV_LOG_TRACE, "set skip map\n");
545
546             /* log position and move on for now */
547             s->skip_map_chunk_offset = avio_tell(pb);
548             s->skip_map_chunk_size = opcode_size;
549             avio_skip(pb, opcode_size);
550             break;
551
552         case OPCODE_SET_DECODING_MAP:
553             av_log(s->avf, AV_LOG_TRACE, "set decoding map\n");
554
555             /* log position and move on for now */
556             s->decode_map_chunk_offset = avio_tell(pb);
557             s->decode_map_chunk_size = opcode_size;
558             avio_skip(pb, opcode_size);
559             break;
560
561         case OPCODE_VIDEO_DATA_06:
562             av_log(s->avf, AV_LOG_TRACE, "set video data format 0x06\n");
563             s->frame_format = 0x06;
564
565             /* log position and move on for now */
566             s->video_chunk_offset = avio_tell(pb);
567             s->video_chunk_size = opcode_size;
568             avio_skip(pb, opcode_size);
569             break;
570
571         case OPCODE_VIDEO_DATA_10:
572             av_log(s->avf, AV_LOG_TRACE, "set video data format 0x10\n");
573             s->frame_format = 0x10;
574
575             /* log position and move on for now */
576             s->video_chunk_offset = avio_tell(pb);
577             s->video_chunk_size = opcode_size;
578             avio_skip(pb, opcode_size);
579             break;
580
581         case OPCODE_VIDEO_DATA_11:
582             av_log(s->avf, AV_LOG_TRACE, "set video data format 0x11\n");
583             s->frame_format = 0x11;
584
585             /* log position and move on for now */
586             s->video_chunk_offset = avio_tell(pb);
587             s->video_chunk_size = opcode_size;
588             avio_skip(pb, opcode_size);
589             break;
590
591         default:
592             av_log(s->avf, AV_LOG_TRACE, "*** unknown opcode type\n");
593             chunk_type = CHUNK_BAD;
594             break;
595
596         }
597     }
598
599     if (s->avf->nb_streams == 1 && s->audio_type)
600         init_audio(s->avf);
601
602     /* make a note of where the stream is sitting */
603     s->next_chunk_offset = avio_tell(pb);
604
605     /* dispatch the first of any pending packets */
606     if ((chunk_type == CHUNK_VIDEO) || (chunk_type == CHUNK_AUDIO_ONLY))
607         chunk_type = load_ipmovie_packet(s, pb, pkt);
608
609     return chunk_type;
610 }
611
612 static const char signature[] = "Interplay MVE File\x1A\0\x1A";
613
614 static int ipmovie_probe(const AVProbeData *p)
615 {
616     const uint8_t *b = p->buf;
617     const uint8_t *b_end = p->buf + p->buf_size - sizeof(signature);
618     do {
619         if (b[0] == signature[0] && memcmp(b, signature, sizeof(signature)) == 0)
620             return AVPROBE_SCORE_MAX;
621         b++;
622     } while (b < b_end);
623
624     return 0;
625 }
626
627 static int ipmovie_read_header(AVFormatContext *s)
628 {
629     IPMVEContext *ipmovie = s->priv_data;
630     AVIOContext *pb = s->pb;
631     AVPacket pkt;
632     AVStream *st;
633     unsigned char chunk_preamble[CHUNK_PREAMBLE_SIZE];
634     int chunk_type, i;
635     uint8_t signature_buffer[sizeof(signature)];
636
637     ipmovie->avf = s;
638
639     avio_read(pb, signature_buffer, sizeof(signature_buffer));
640     while (memcmp(signature_buffer, signature, sizeof(signature))) {
641         memmove(signature_buffer, signature_buffer + 1, sizeof(signature_buffer) - 1);
642         signature_buffer[sizeof(signature_buffer) - 1] = avio_r8(pb);
643         if (avio_feof(pb))
644             return AVERROR_EOF;
645     }
646     /* initialize private context members */
647     ipmovie->video_pts = ipmovie->audio_frame_count = 0;
648     ipmovie->audio_chunk_offset = ipmovie->video_chunk_offset =
649     ipmovie->decode_map_chunk_offset = ipmovie->skip_map_chunk_offset = 0;
650     ipmovie->decode_map_chunk_size = ipmovie->video_chunk_size =
651     ipmovie->skip_map_chunk_size = 0;
652     ipmovie->send_buffer = ipmovie->frame_format = 0;
653
654     /* on the first read, this will position the stream at the first chunk */
655     ipmovie->next_chunk_offset = avio_tell(pb) + 4;
656
657     for (i = 0; i < 256; i++)
658         ipmovie->palette[i] = 0xFFU << 24;
659
660     /* process the first chunk which should be CHUNK_INIT_VIDEO */
661     if (process_ipmovie_chunk(ipmovie, pb, &pkt) != CHUNK_INIT_VIDEO) {
662         av_packet_unref(&pkt);
663         return AVERROR_INVALIDDATA;
664     }
665
666     /* peek ahead to the next chunk-- if it is an init audio chunk, process
667      * it; if it is the first video chunk, this is a silent file */
668     if (avio_read(pb, chunk_preamble, CHUNK_PREAMBLE_SIZE) !=
669         CHUNK_PREAMBLE_SIZE)
670         return AVERROR(EIO);
671     chunk_type = AV_RL16(&chunk_preamble[2]);
672     avio_seek(pb, -CHUNK_PREAMBLE_SIZE, SEEK_CUR);
673
674     if (chunk_type == CHUNK_VIDEO)
675         ipmovie->audio_type = AV_CODEC_ID_NONE;  /* no audio */
676     else if (process_ipmovie_chunk(ipmovie, pb, &pkt) != CHUNK_INIT_AUDIO) {
677         av_packet_unref(&pkt);
678         return AVERROR_INVALIDDATA;
679     }
680
681     /* initialize the stream decoders */
682     st = avformat_new_stream(s, NULL);
683     if (!st)
684         return AVERROR(ENOMEM);
685     avpriv_set_pts_info(st, 63, 1, 1000000);
686     ipmovie->video_stream_index = st->index;
687     st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
688     st->codecpar->codec_id = AV_CODEC_ID_INTERPLAY_VIDEO;
689     st->codecpar->codec_tag = 0;  /* no fourcc */
690     st->codecpar->width = ipmovie->video_width;
691     st->codecpar->height = ipmovie->video_height;
692     st->codecpar->bits_per_coded_sample = ipmovie->video_bpp;
693
694     if (ipmovie->audio_type) {
695         return init_audio(s);
696     } else
697        s->ctx_flags |= AVFMTCTX_NOHEADER;
698
699     return 0;
700 }
701
702 static int ipmovie_read_packet(AVFormatContext *s,
703                                AVPacket *pkt)
704 {
705     IPMVEContext *ipmovie = s->priv_data;
706     AVIOContext *pb = s->pb;
707     int ret;
708
709     for (;;) {
710     ret = process_ipmovie_chunk(ipmovie, pb, pkt);
711     if (ret == CHUNK_BAD)
712         ret = AVERROR_INVALIDDATA;
713     else if (ret == CHUNK_EOF)
714         ret = AVERROR(EIO);
715     else if (ret == CHUNK_NOMEM)
716         ret = AVERROR(ENOMEM);
717     else if (ret == CHUNK_END || ret == CHUNK_SHUTDOWN)
718         ret = AVERROR_EOF;
719     else if (ret == CHUNK_VIDEO)
720         ret = 0;
721     else if (ret == CHUNK_INIT_VIDEO || ret == CHUNK_INIT_AUDIO)
722         continue;
723     else
724         continue;
725
726     return ret;
727     }
728 }
729
730 AVInputFormat ff_ipmovie_demuxer = {
731     .name           = "ipmovie",
732     .long_name      = NULL_IF_CONFIG_SMALL("Interplay MVE"),
733     .priv_data_size = sizeof(IPMVEContext),
734     .read_probe     = ipmovie_probe,
735     .read_header    = ipmovie_read_header,
736     .read_packet    = ipmovie_read_packet,
737 };