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