~diresu/blender/blender-command-port

« back to all changes in this revision

Viewing changes to extern/ffmpeg/libavformat/ipmovie.c

  • Committer: theeth
  • Date: 2008-10-14 16:52:04 UTC
  • Revision ID: vcs-imports@canonical.com-20081014165204-r32w2gm6s0osvdhn
copy back trunk

Show diffs side-by-side

added added

removed removed

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