ipmovie.c 20.3 KB
Newer Older
1 2 3 4
/*
 * Interplay MVE File Demuxer
 * Copyright (c) 2003 The ffmpeg Project
 *
5 6 7
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
8 9
 * modify it under the terms of the GNU Lesser General Public
 * 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 14 15 16 17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * 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 25 26 27 28 29 30 31 32 33 34
 * Interplay MVE file demuxer
 * by Mike Melanson (melanson@pcisys.net)
 * For more information regarding the Interplay MVE file format, visit:
 *   http://www.pcisys.net/~melanson/codecs/
 * The aforementioned site also contains a command line utility for parsing
 * IP MVE files so that you can get a good idea of the typical structure of
 * such files. This demuxer is not the best example to use if you are trying
 * to write your own as it uses a rather roundabout approach for splitting
 * up and sending out the chunks.
 */

35
#include "libavutil/intreadwrite.h"
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
#include "avformat.h"

#define CHUNK_PREAMBLE_SIZE 4
#define OPCODE_PREAMBLE_SIZE 4

#define CHUNK_INIT_AUDIO   0x0000
#define CHUNK_AUDIO_ONLY   0x0001
#define CHUNK_INIT_VIDEO   0x0002
#define CHUNK_VIDEO        0x0003
#define CHUNK_SHUTDOWN     0x0004
#define CHUNK_END          0x0005
/* these last types are used internally */
#define CHUNK_DONE         0xFFFC
#define CHUNK_NOMEM        0xFFFD
#define CHUNK_EOF          0xFFFE
#define CHUNK_BAD          0xFFFF

#define OPCODE_END_OF_STREAM           0x00
#define OPCODE_END_OF_CHUNK            0x01
#define OPCODE_CREATE_TIMER            0x02
#define OPCODE_INIT_AUDIO_BUFFERS      0x03
#define OPCODE_START_STOP_AUDIO        0x04
#define OPCODE_INIT_VIDEO_BUFFERS      0x05
#define OPCODE_UNKNOWN_06              0x06
#define OPCODE_SEND_BUFFER             0x07
#define OPCODE_AUDIO_FRAME             0x08
#define OPCODE_SILENCE_FRAME           0x09
#define OPCODE_INIT_VIDEO_MODE         0x0A
#define OPCODE_CREATE_GRADIENT         0x0B
#define OPCODE_SET_PALETTE             0x0C
#define OPCODE_SET_PALETTE_COMPRESSED  0x0D
#define OPCODE_UNKNOWN_0E              0x0E
#define OPCODE_SET_DECODING_MAP        0x0F
#define OPCODE_UNKNOWN_10              0x10
#define OPCODE_VIDEO_DATA              0x11
#define OPCODE_UNKNOWN_12              0x12
#define OPCODE_UNKNOWN_13              0x13
#define OPCODE_UNKNOWN_14              0x14
#define OPCODE_UNKNOWN_15              0x15

#define PALETTE_COUNT 256

typedef struct IPMVEContext {

    unsigned char *buf;
    int buf_size;

83
    uint64_t frame_pts_inc;
84

85
    unsigned int video_bpp;
86 87 88
    unsigned int video_width;
    unsigned int video_height;
    int64_t video_pts;
89 90
    uint32_t     palette[256];
    int          has_palette;
91 92 93 94

    unsigned int audio_bits;
    unsigned int audio_channels;
    unsigned int audio_sample_rate;
95
    enum CodecID audio_type;
96 97 98 99 100
    unsigned int audio_frame_count;

    int video_stream_index;
    int audio_stream_index;

101
    int64_t audio_chunk_offset;
102
    int audio_chunk_size;
103
    int64_t video_chunk_offset;
104
    int video_chunk_size;
105
    int64_t decode_map_chunk_offset;
106 107
    int decode_map_chunk_size;

108
    int64_t next_chunk_offset;
109 110 111

} IPMVEContext;

112
static int load_ipmovie_packet(IPMVEContext *s, AVIOContext *pb,
113 114 115 116 117 118
    AVPacket *pkt) {

    int chunk_type;

    if (s->audio_chunk_offset) {

119 120 121 122 123 124
        /* adjust for PCM audio by skipping chunk header */
        if (s->audio_type != CODEC_ID_INTERPLAY_DPCM) {
            s->audio_chunk_offset += 6;
            s->audio_chunk_size -= 6;
        }

125
        avio_seek(pb, s->audio_chunk_offset, SEEK_SET);
126 127
        s->audio_chunk_offset = 0;

Michael Niedermayer's avatar
Michael Niedermayer committed
128 129
        if (s->audio_chunk_size != av_get_packet(pb, pkt, s->audio_chunk_size))
            return CHUNK_EOF;
130 131

        pkt->stream_index = s->audio_stream_index;
132
        pkt->pts = s->audio_frame_count;
133 134 135 136 137 138 139 140 141

        /* audio frame maintenance */
        if (s->audio_type != CODEC_ID_INTERPLAY_DPCM)
            s->audio_frame_count +=
            (s->audio_chunk_size / s->audio_channels / (s->audio_bits / 8));
        else
            s->audio_frame_count +=
                (s->audio_chunk_size - 6) / s->audio_channels;

142 143
        av_dlog(NULL, "sending audio frame with pts %"PRId64" (%d audio frames)\n",
                pkt->pts, s->audio_frame_count);
144 145 146 147 148

        chunk_type = CHUNK_VIDEO;

    } else if (s->decode_map_chunk_offset) {

149
        /* send both the decode map and the video data together */
150

151
        if (av_new_packet(pkt, s->decode_map_chunk_size + s->video_chunk_size))
152 153
            return CHUNK_NOMEM;

154 155 156 157 158 159 160 161 162 163 164
        if (s->has_palette) {
            uint8_t *pal;

            pal = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE,
                                          AVPALETTE_SIZE);
            if (pal) {
                memcpy(pal, s->palette, AVPALETTE_SIZE);
                s->has_palette = 0;
            }
        }

Michael Niedermayer's avatar
Michael Niedermayer committed
165
        pkt->pos= s->decode_map_chunk_offset;
166
        avio_seek(pb, s->decode_map_chunk_offset, SEEK_SET);
167 168
        s->decode_map_chunk_offset = 0;

169
        if (avio_read(pb, pkt->data, s->decode_map_chunk_size) !=
170 171 172 173 174
            s->decode_map_chunk_size) {
            av_free_packet(pkt);
            return CHUNK_EOF;
        }

175
        avio_seek(pb, s->video_chunk_offset, SEEK_SET);
176 177
        s->video_chunk_offset = 0;

178
        if (avio_read(pb, pkt->data + s->decode_map_chunk_size,
179
            s->video_chunk_size) != s->video_chunk_size) {
180 181 182 183
            av_free_packet(pkt);
            return CHUNK_EOF;
        }

184 185 186
        pkt->stream_index = s->video_stream_index;
        pkt->pts = s->video_pts;

187
        av_dlog(NULL, "sending video frame with pts %"PRId64"\n", pkt->pts);
188

189 190 191 192 193 194
        s->video_pts += s->frame_pts_inc;

        chunk_type = CHUNK_VIDEO;

    } else {

195
        avio_seek(pb, s->next_chunk_offset, SEEK_SET);
196 197 198 199 200 201 202 203 204
        chunk_type = CHUNK_DONE;

    }

    return chunk_type;
}

/* This function loads and processes a single chunk in an IP movie file.
 * It returns the type of chunk that was processed. */
205
static int process_ipmovie_chunk(IPMVEContext *s, AVIOContext *pb,
206 207 208 209 210 211 212 213 214 215
    AVPacket *pkt)
{
    unsigned char chunk_preamble[CHUNK_PREAMBLE_SIZE];
    int chunk_type;
    int chunk_size;
    unsigned char opcode_preamble[OPCODE_PREAMBLE_SIZE];
    unsigned char opcode_type;
    unsigned char opcode_version;
    int opcode_size;
    unsigned char scratch[1024];
216
    int i, j;
217 218
    int first_color, last_color;
    int audio_flags;
219
    unsigned char r, g, b;
220 221 222

    /* see if there are any pending packets */
    chunk_type = load_ipmovie_packet(s, pb, pkt);
223
    if (chunk_type != CHUNK_DONE)
224 225 226 227 228
        return chunk_type;

    /* read the next chunk, wherever the file happens to be pointing */
    if (url_feof(pb))
        return CHUNK_EOF;
229
    if (avio_read(pb, chunk_preamble, CHUNK_PREAMBLE_SIZE) !=
230 231
        CHUNK_PREAMBLE_SIZE)
        return CHUNK_BAD;
232 233
    chunk_size = AV_RL16(&chunk_preamble[0]);
    chunk_type = AV_RL16(&chunk_preamble[2]);
234

235
    av_dlog(NULL, "chunk type 0x%04X, 0x%04X bytes: ", chunk_type, chunk_size);
236 237 238 239

    switch (chunk_type) {

    case CHUNK_INIT_AUDIO:
240
        av_dlog(NULL, "initialize audio\n");
241 242 243
        break;

    case CHUNK_AUDIO_ONLY:
244
        av_dlog(NULL, "audio only\n");
245 246 247
        break;

    case CHUNK_INIT_VIDEO:
248
        av_dlog(NULL, "initialize video\n");
249 250 251
        break;

    case CHUNK_VIDEO:
252
        av_dlog(NULL, "video (and audio)\n");
253 254 255
        break;

    case CHUNK_SHUTDOWN:
256
        av_dlog(NULL, "shutdown\n");
257 258 259
        break;

    case CHUNK_END:
260
        av_dlog(NULL, "end\n");
261 262 263
        break;

    default:
264
        av_dlog(NULL, "invalid chunk\n");
265 266 267 268 269 270 271 272
        chunk_type = CHUNK_BAD;
        break;

    }

    while ((chunk_size > 0) && (chunk_type != CHUNK_BAD)) {

        /* read the next chunk, wherever the file happens to be pointing */
Reimar Döffinger's avatar
Reimar Döffinger committed
273
        if (url_feof(pb)) {
274 275 276
            chunk_type = CHUNK_EOF;
            break;
        }
277
        if (avio_read(pb, opcode_preamble, CHUNK_PREAMBLE_SIZE) !=
278 279 280 281 282
            CHUNK_PREAMBLE_SIZE) {
            chunk_type = CHUNK_BAD;
            break;
        }

283
        opcode_size = AV_RL16(&opcode_preamble[0]);
284 285 286 287 288 289
        opcode_type = opcode_preamble[2];
        opcode_version = opcode_preamble[3];

        chunk_size -= OPCODE_PREAMBLE_SIZE;
        chunk_size -= opcode_size;
        if (chunk_size < 0) {
290
            av_dlog(NULL, "chunk_size countdown just went negative\n");
291 292 293 294
            chunk_type = CHUNK_BAD;
            break;
        }

295 296
        av_dlog(NULL, "  opcode type %02X, version %d, 0x%04X bytes: ",
                opcode_type, opcode_version, opcode_size);
297 298 299
        switch (opcode_type) {

        case OPCODE_END_OF_STREAM:
300
            av_dlog(NULL, "end of stream\n");
301
            avio_skip(pb, opcode_size);
302 303 304
            break;

        case OPCODE_END_OF_CHUNK:
305
            av_dlog(NULL, "end of chunk\n");
306
            avio_skip(pb, opcode_size);
307 308 309
            break;

        case OPCODE_CREATE_TIMER:
310
            av_dlog(NULL, "create timer\n");
311
            if ((opcode_version > 0) || (opcode_size > 6)) {
312
                av_dlog(NULL, "bad create_timer opcode\n");
313 314 315
                chunk_type = CHUNK_BAD;
                break;
            }
316
            if (avio_read(pb, scratch, opcode_size) !=
317 318 319 320
                opcode_size) {
                chunk_type = CHUNK_BAD;
                break;
            }
321
            s->frame_pts_inc = ((uint64_t)AV_RL32(&scratch[0])) * AV_RL16(&scratch[4]);
322 323 324
            av_dlog(NULL, "  %.2f frames/second (timer div = %d, subdiv = %d)\n",
                    1000000.0 / s->frame_pts_inc, AV_RL32(&scratch[0]),
                    AV_RL16(&scratch[4]));
325 326 327
            break;

        case OPCODE_INIT_AUDIO_BUFFERS:
328
            av_dlog(NULL, "initialize audio buffers\n");
329
            if ((opcode_version > 1) || (opcode_size > 10)) {
330
                av_dlog(NULL, "bad init_audio_buffers opcode\n");
331 332 333
                chunk_type = CHUNK_BAD;
                break;
            }
334
            if (avio_read(pb, scratch, opcode_size) !=
335 336 337 338
                opcode_size) {
                chunk_type = CHUNK_BAD;
                break;
            }
339 340
            s->audio_sample_rate = AV_RL16(&scratch[4]);
            audio_flags = AV_RL16(&scratch[2]);
341 342 343 344 345 346 347 348 349 350 351
            /* bit 0 of the flags: 0 = mono, 1 = stereo */
            s->audio_channels = (audio_flags & 1) + 1;
            /* bit 1 of the flags: 0 = 8 bit, 1 = 16 bit */
            s->audio_bits = (((audio_flags >> 1) & 1) + 1) * 8;
            /* bit 2 indicates compressed audio in version 1 opcode */
            if ((opcode_version == 1) && (audio_flags & 0x4))
                s->audio_type = CODEC_ID_INTERPLAY_DPCM;
            else if (s->audio_bits == 16)
                s->audio_type = CODEC_ID_PCM_S16LE;
            else
                s->audio_type = CODEC_ID_PCM_U8;
352 353 354 355 356
            av_dlog(NULL, "audio: %d bits, %d Hz, %s, %s format\n",
                    s->audio_bits, s->audio_sample_rate,
                    (s->audio_channels == 2) ? "stereo" : "mono",
                    (s->audio_type == CODEC_ID_INTERPLAY_DPCM) ?
                    "Interplay audio" : "PCM");
357 358 359
            break;

        case OPCODE_START_STOP_AUDIO:
360
            av_dlog(NULL, "start/stop audio\n");
361
            avio_skip(pb, opcode_size);
362 363 364
            break;

        case OPCODE_INIT_VIDEO_BUFFERS:
365
            av_dlog(NULL, "initialize video buffers\n");
366
            if ((opcode_version > 2) || (opcode_size > 8)) {
367
                av_dlog(NULL, "bad init_video_buffers opcode\n");
368 369 370
                chunk_type = CHUNK_BAD;
                break;
            }
371
            if (avio_read(pb, scratch, opcode_size) !=
372 373 374 375
                opcode_size) {
                chunk_type = CHUNK_BAD;
                break;
            }
376 377
            s->video_width = AV_RL16(&scratch[0]) * 8;
            s->video_height = AV_RL16(&scratch[2]) * 8;
378 379 380 381 382
            if (opcode_version < 2 || !AV_RL16(&scratch[6])) {
                s->video_bpp = 8;
            } else {
                s->video_bpp = 16;
            }
383 384
            av_dlog(NULL, "video resolution: %d x %d\n",
                    s->video_width, s->video_height);
385 386 387 388 389 390 391 392 393
            break;

        case OPCODE_UNKNOWN_06:
        case OPCODE_UNKNOWN_0E:
        case OPCODE_UNKNOWN_10:
        case OPCODE_UNKNOWN_12:
        case OPCODE_UNKNOWN_13:
        case OPCODE_UNKNOWN_14:
        case OPCODE_UNKNOWN_15:
394
            av_dlog(NULL, "unknown (but documented) opcode %02X\n", opcode_type);
395
            avio_skip(pb, opcode_size);
396 397 398
            break;

        case OPCODE_SEND_BUFFER:
399
            av_dlog(NULL, "send buffer\n");
400
            avio_skip(pb, opcode_size);
401 402 403
            break;

        case OPCODE_AUDIO_FRAME:
404
            av_dlog(NULL, "audio frame\n");
405 406

            /* log position and move on for now */
407
            s->audio_chunk_offset = avio_tell(pb);
408
            s->audio_chunk_size = opcode_size;
409
            avio_skip(pb, opcode_size);
410 411 412
            break;

        case OPCODE_SILENCE_FRAME:
413
            av_dlog(NULL, "silence frame\n");
414
            avio_skip(pb, opcode_size);
415 416 417
            break;

        case OPCODE_INIT_VIDEO_MODE:
418
            av_dlog(NULL, "initialize video mode\n");
419
            avio_skip(pb, opcode_size);
420 421 422
            break;

        case OPCODE_CREATE_GRADIENT:
423
            av_dlog(NULL, "create gradient\n");
424
            avio_skip(pb, opcode_size);
425 426 427
            break;

        case OPCODE_SET_PALETTE:
428
            av_dlog(NULL, "set palette\n");
429 430 431
            /* check for the logical maximum palette size
             * (3 * 256 + 4 bytes) */
            if (opcode_size > 0x304) {
432
                av_dlog(NULL, "demux_ipmovie: set_palette opcode too large\n");
433 434 435
                chunk_type = CHUNK_BAD;
                break;
            }
436
            if (avio_read(pb, scratch, opcode_size) != opcode_size) {
437 438 439 440 441
                chunk_type = CHUNK_BAD;
                break;
            }

            /* load the palette into internal data structure */
442 443
            first_color = AV_RL16(&scratch[0]);
            last_color = first_color + AV_RL16(&scratch[2]) - 1;
444 445
            /* sanity check (since they are 16 bit values) */
            if ((first_color > 0xFF) || (last_color > 0xFF)) {
446
                av_dlog(NULL, "demux_ipmovie: set_palette indexes out of range (%d -> %d)\n",
447 448 449 450 451 452
                    first_color, last_color);
                chunk_type = CHUNK_BAD;
                break;
            }
            j = 4;  /* offset of first palette data */
            for (i = first_color; i <= last_color; i++) {
453 454
                /* the palette is stored as a 6-bit VGA palette, thus each
                 * component is shifted up to a 8-bit range */
455 456 457
                r = scratch[j++] * 4;
                g = scratch[j++] * 4;
                b = scratch[j++] * 4;
458
                s->palette[i] = (r << 16) | (g << 8) | (b);
459
            }
460
            s->has_palette = 1;
461 462 463
            break;

        case OPCODE_SET_PALETTE_COMPRESSED:
464
            av_dlog(NULL, "set palette compressed\n");
465
            avio_skip(pb, opcode_size);
466 467 468
            break;

        case OPCODE_SET_DECODING_MAP:
469
            av_dlog(NULL, "set decoding map\n");
470 471

            /* log position and move on for now */
472
            s->decode_map_chunk_offset = avio_tell(pb);
473
            s->decode_map_chunk_size = opcode_size;
474
            avio_skip(pb, opcode_size);
475 476 477
            break;

        case OPCODE_VIDEO_DATA:
478
            av_dlog(NULL, "set video data\n");
479 480

            /* log position and move on for now */
481
            s->video_chunk_offset = avio_tell(pb);
482
            s->video_chunk_size = opcode_size;
483
            avio_skip(pb, opcode_size);
484 485 486
            break;

        default:
487
            av_dlog(NULL, "*** unknown opcode type\n");
488 489 490 491 492 493 494
            chunk_type = CHUNK_BAD;
            break;

        }
    }

    /* make a note of where the stream is sitting */
495
    s->next_chunk_offset = avio_tell(pb);
496 497 498 499 500 501 502 503

    /* dispatch the first of any pending packets */
    if ((chunk_type == CHUNK_VIDEO) || (chunk_type == CHUNK_AUDIO_ONLY))
        chunk_type = load_ipmovie_packet(s, pb, pkt);

    return chunk_type;
}

504 505
static const char signature[] = "Interplay MVE File\x1A\0\x1A";

506 507
static int ipmovie_probe(AVProbeData *p)
{
508 509 510 511 512 513 514 515
    uint8_t *b = p->buf;
    uint8_t *b_end = p->buf + p->buf_size - sizeof(signature);
    do {
        if (memcmp(b++, signature, sizeof(signature)) == 0)
            return AVPROBE_SCORE_MAX;
    } while (b < b_end);

    return 0;
516 517 518 519 520
}

static int ipmovie_read_header(AVFormatContext *s,
                               AVFormatParameters *ap)
{
521
    IPMVEContext *ipmovie = s->priv_data;
522
    AVIOContext *pb = s->pb;
523 524
    AVPacket pkt;
    AVStream *st;
525 526
    unsigned char chunk_preamble[CHUNK_PREAMBLE_SIZE];
    int chunk_type;
527 528
    uint8_t signature_buffer[sizeof(signature)];

529
    avio_read(pb, signature_buffer, sizeof(signature_buffer));
530 531
    while (memcmp(signature_buffer, signature, sizeof(signature))) {
        memmove(signature_buffer, signature_buffer + 1, sizeof(signature_buffer) - 1);
532
        signature_buffer[sizeof(signature_buffer) - 1] = avio_r8(pb);
533 534 535
        if (url_feof(pb))
            return AVERROR_EOF;
    }
536 537 538 539 540 541
    /* initialize private context members */
    ipmovie->video_pts = ipmovie->audio_frame_count = 0;
    ipmovie->audio_chunk_offset = ipmovie->video_chunk_offset =
    ipmovie->decode_map_chunk_offset = 0;

    /* on the first read, this will position the stream at the first chunk */
542
    ipmovie->next_chunk_offset = avio_tell(pb) + 4;
543 544 545 546 547

    /* process the first chunk which should be CHUNK_INIT_VIDEO */
    if (process_ipmovie_chunk(ipmovie, pb, &pkt) != CHUNK_INIT_VIDEO)
        return AVERROR_INVALIDDATA;

548 549
    /* peek ahead to the next chunk-- if it is an init audio chunk, process
     * it; if it is the first video chunk, this is a silent file */
550
    if (avio_read(pb, chunk_preamble, CHUNK_PREAMBLE_SIZE) !=
551
        CHUNK_PREAMBLE_SIZE)
552
        return AVERROR(EIO);
553
    chunk_type = AV_RL16(&chunk_preamble[2]);
554
    avio_seek(pb, -CHUNK_PREAMBLE_SIZE, SEEK_CUR);
555 556

    if (chunk_type == CHUNK_VIDEO)
557
        ipmovie->audio_type = CODEC_ID_NONE;  /* no audio */
558
    else if (process_ipmovie_chunk(ipmovie, pb, &pkt) != CHUNK_INIT_AUDIO)
559 560 561 562 563
        return AVERROR_INVALIDDATA;

    /* initialize the stream decoders */
    st = av_new_stream(s, 0);
    if (!st)
564
        return AVERROR(ENOMEM);
565
    av_set_pts_info(st, 63, 1, 1000000);
566
    ipmovie->video_stream_index = st->index;
567
    st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
568 569 570 571
    st->codec->codec_id = CODEC_ID_INTERPLAY_VIDEO;
    st->codec->codec_tag = 0;  /* no fourcc */
    st->codec->width = ipmovie->video_width;
    st->codec->height = ipmovie->video_height;
572
    st->codec->bits_per_coded_sample = ipmovie->video_bpp;
573

574 575 576
    if (ipmovie->audio_type) {
        st = av_new_stream(s, 0);
        if (!st)
577
            return AVERROR(ENOMEM);
578
        av_set_pts_info(st, 32, 1, ipmovie->audio_sample_rate);
579
        ipmovie->audio_stream_index = st->index;
580
        st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
581 582 583 584
        st->codec->codec_id = ipmovie->audio_type;
        st->codec->codec_tag = 0;  /* no tag */
        st->codec->channels = ipmovie->audio_channels;
        st->codec->sample_rate = ipmovie->audio_sample_rate;
585
        st->codec->bits_per_coded_sample = ipmovie->audio_bits;
586
        st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
587
            st->codec->bits_per_coded_sample;
588 589
        if (st->codec->codec_id == CODEC_ID_INTERPLAY_DPCM)
            st->codec->bit_rate /= 2;
590
        st->codec->block_align = st->codec->channels * st->codec->bits_per_coded_sample;
591
    }
592 593 594 595 596 597 598

    return 0;
}

static int ipmovie_read_packet(AVFormatContext *s,
                               AVPacket *pkt)
{
599
    IPMVEContext *ipmovie = s->priv_data;
600
    AVIOContext *pb = s->pb;
601 602 603 604 605 606
    int ret;

    ret = process_ipmovie_chunk(ipmovie, pb, pkt);
    if (ret == CHUNK_BAD)
        ret = AVERROR_INVALIDDATA;
    else if (ret == CHUNK_EOF)
607
        ret = AVERROR(EIO);
608
    else if (ret == CHUNK_NOMEM)
609
        ret = AVERROR(ENOMEM);
610
    else if (ret == CHUNK_VIDEO)
611
        ret = 0;
612 613
    else
        ret = -1;
614 615 616 617

    return ret;
}

618
AVInputFormat ff_ipmovie_demuxer = {
619
    "ipmovie",
620
    NULL_IF_CONFIG_SMALL("Interplay MVE format"),
621 622 623 624 625
    sizeof(IPMVEContext),
    ipmovie_probe,
    ipmovie_read_header,
    ipmovie_read_packet,
};