xmv.c 18.2 KB
Newer Older
Sven Hesse's avatar
Sven Hesse committed
1 2 3 4 5
/*
 * Microsoft XMV demuxer
 * Copyright (c) 2011 Sven Hesse <drmccoy@drmccoy.de>
 * Copyright (c) 2011 Matthew Hoops <clone2727@gmail.com>
 *
6
 * This file is part of FFmpeg.
Sven Hesse's avatar
Sven Hesse committed
7
 *
8
 * FFmpeg is free software; you can redistribute it and/or
Sven Hesse's avatar
Sven Hesse committed
9 10 11 12
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
13
 * FFmpeg is distributed in the hope that it will be useful,
Sven Hesse's avatar
Sven Hesse committed
14 15 16 17 18
 * 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
19
 * License along with FFmpeg; if not, write to the Free Software
Sven Hesse's avatar
Sven Hesse committed
20 21 22 23 24 25 26 27
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/**
 * @file
 * Microsoft XMV demuxer
 */

28
#include <inttypes.h>
Sven Hesse's avatar
Sven Hesse committed
29 30 31 32

#include "libavutil/intreadwrite.h"

#include "avformat.h"
33
#include "internal.h"
Sven Hesse's avatar
Sven Hesse committed
34
#include "riff.h"
Michael Niedermayer's avatar
Michael Niedermayer committed
35
#include "libavutil/avassert.h"
Sven Hesse's avatar
Sven Hesse committed
36

37
/** The min size of an XMV header. */
Sven Hesse's avatar
Sven Hesse committed
38 39
#define XMV_MIN_HEADER_SIZE 36

40
/** Audio flag: ADPCM'd 5.1 stream, front left / right channels */
Sven Hesse's avatar
Sven Hesse committed
41
#define XMV_AUDIO_ADPCM51_FRONTLEFTRIGHT 1
42
/** Audio flag: ADPCM'd 5.1 stream, front center / low frequency channels */
Sven Hesse's avatar
Sven Hesse committed
43
#define XMV_AUDIO_ADPCM51_FRONTCENTERLOW 2
44
/** Audio flag: ADPCM'd 5.1 stream, rear left / right channels */
Sven Hesse's avatar
Sven Hesse committed
45 46
#define XMV_AUDIO_ADPCM51_REARLEFTRIGHT  4

47
/** Audio flag: Any of the ADPCM'd 5.1 stream flags. */
Sven Hesse's avatar
Sven Hesse committed
48 49 50 51
#define XMV_AUDIO_ADPCM51 (XMV_AUDIO_ADPCM51_FRONTLEFTRIGHT | \
                           XMV_AUDIO_ADPCM51_FRONTCENTERLOW | \
                           XMV_AUDIO_ADPCM51_REARLEFTRIGHT)

52 53
#define XMV_BLOCK_ALIGN_SIZE 36

54
/** A video packet with an XMV file. */
Sven Hesse's avatar
Sven Hesse committed
55
typedef struct XMVVideoPacket {
56
    int created;
57
    int stream_index; ///< The decoder stream index for this video packet.
Sven Hesse's avatar
Sven Hesse committed
58

59 60
    uint32_t data_size;   ///< The size of the remaining video data.
    uint64_t data_offset; ///< The offset of the video data within the file.
Sven Hesse's avatar
Sven Hesse committed
61

62 63
    uint32_t current_frame; ///< The current frame within this video packet.
    uint32_t frame_count;   ///< The amount of frames within this video packet.
Sven Hesse's avatar
Sven Hesse committed
64

65 66
    int     has_extradata; ///< Does the video packet contain extra data?
    uint8_t extradata[4];  ///< The extra data
Sven Hesse's avatar
Sven Hesse committed
67

68 69
    int64_t last_pts; ///< PTS of the last video frame.
    int64_t pts;      ///< PTS of the most current video frame.
Sven Hesse's avatar
Sven Hesse committed
70 71
} XMVVideoPacket;

72
/** An audio packet with an XMV file. */
Sven Hesse's avatar
Sven Hesse committed
73
typedef struct XMVAudioPacket {
74
    int created;
75
    int stream_index; ///< The decoder stream index for this audio packet.
Sven Hesse's avatar
Sven Hesse committed
76

77 78 79
    /* Stream format properties. */
    uint16_t compression;     ///< The type of compression.
    uint16_t channels;        ///< Number of channels.
80
    int32_t sample_rate;      ///< Sampling rate.
81 82 83
    uint16_t bits_per_sample; ///< Bits per compressed sample.
    uint32_t bit_rate;        ///< Bits of compressed data per second.
    uint16_t flags;           ///< Flags
84
    unsigned block_align;     ///< Bytes per compressed block.
85
    uint16_t block_samples;   ///< Decompressed samples per compressed block.
86

87
    enum AVCodecID codec_id; ///< The codec ID of the compression scheme.
Sven Hesse's avatar
Sven Hesse committed
88

89 90
    uint32_t data_size;   ///< The size of the remaining audio data.
    uint64_t data_offset; ///< The offset of the audio data within the file.
Sven Hesse's avatar
Sven Hesse committed
91

92
    uint32_t frame_size; ///< Number of bytes to put into an audio frame.
Sven Hesse's avatar
Sven Hesse committed
93

94
    uint64_t block_count; ///< Running counter of decompressed audio block.
Sven Hesse's avatar
Sven Hesse committed
95 96
} XMVAudioPacket;

97
/** Context for demuxing an XMV file. */
Sven Hesse's avatar
Sven Hesse committed
98
typedef struct XMVDemuxContext {
99
    uint16_t audio_track_count; ///< Number of audio track in this file.
Sven Hesse's avatar
Sven Hesse committed
100

101 102
    uint32_t this_packet_size; ///< Size of the current packet.
    uint32_t next_packet_size; ///< Size of the next packet.
Sven Hesse's avatar
Sven Hesse committed
103

104 105
    uint64_t this_packet_offset; ///< Offset of the current packet.
    uint64_t next_packet_offset; ///< Offset of the next packet.
Sven Hesse's avatar
Sven Hesse committed
106

107 108
    uint16_t current_stream; ///< The index of the stream currently handling.
    uint16_t stream_count;   ///< The number of streams in this file.
Sven Hesse's avatar
Sven Hesse committed
109

110 111 112
    uint32_t video_duration;
    uint32_t video_width;
    uint32_t video_height;
Sven Hesse's avatar
Sven Hesse committed
113

114 115
    XMVVideoPacket  video; ///< The video packet contained in each packet.
    XMVAudioPacket *audio; ///< The audio packets contained in each packet.
Sven Hesse's avatar
Sven Hesse committed
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
} XMVDemuxContext;

static int xmv_probe(AVProbeData *p)
{
    uint32_t file_version;

    if (p->buf_size < XMV_MIN_HEADER_SIZE)
        return 0;

    file_version = AV_RL32(p->buf + 16);
    if ((file_version == 0) || (file_version > 4))
        return 0;

    if (!memcmp(p->buf + 12, "xobX", 4))
        return AVPROBE_SCORE_MAX;

    return 0;
}

135 136 137 138
static int xmv_read_close(AVFormatContext *s)
{
    XMVDemuxContext *xmv = s->priv_data;

139
    av_freep(&xmv->audio);
140 141 142 143

    return 0;
}

144
static int xmv_read_header(AVFormatContext *s)
Sven Hesse's avatar
Sven Hesse committed
145 146 147 148 149 150 151
{
    XMVDemuxContext *xmv = s->priv_data;
    AVIOContext     *pb  = s->pb;

    uint32_t file_version;
    uint32_t this_packet_size;
    uint16_t audio_track;
152
    int ret;
Sven Hesse's avatar
Sven Hesse committed
153

154 155
    s->ctx_flags |= AVFMTCTX_NOHEADER;

Sven Hesse's avatar
Sven Hesse committed
156 157 158 159 160 161 162 163 164
    avio_skip(pb, 4); /* Next packet size */

    this_packet_size = avio_rl32(pb);

    avio_skip(pb, 4); /* Max packet size */
    avio_skip(pb, 4); /* "xobX" */

    file_version = avio_rl32(pb);
    if ((file_version != 4) && (file_version != 2))
165
        avpriv_request_sample(s, "Uncommon version %"PRIu32"", file_version);
Sven Hesse's avatar
Sven Hesse committed
166

167
    /* Video tracks */
Sven Hesse's avatar
Sven Hesse committed
168

169 170 171
    xmv->video_width    = avio_rl32(pb);
    xmv->video_height   = avio_rl32(pb);
    xmv->video_duration = avio_rl32(pb);
Sven Hesse's avatar
Sven Hesse committed
172 173 174 175 176 177 178

    /* Audio tracks */

    xmv->audio_track_count = avio_rl16(pb);

    avio_skip(pb, 2); /* Unknown (padding?) */

179
    xmv->audio = av_mallocz_array(xmv->audio_track_count, sizeof(XMVAudioPacket));
180 181 182 183
    if (!xmv->audio) {
        ret = AVERROR(ENOMEM);
        goto fail;
    }
Sven Hesse's avatar
Sven Hesse committed
184 185

    for (audio_track = 0; audio_track < xmv->audio_track_count; audio_track++) {
186
        XMVAudioPacket *packet = &xmv->audio[audio_track];
Sven Hesse's avatar
Sven Hesse committed
187

188 189 190 191 192 193 194 195 196
        packet->compression     = avio_rl16(pb);
        packet->channels        = avio_rl16(pb);
        packet->sample_rate     = avio_rl32(pb);
        packet->bits_per_sample = avio_rl16(pb);
        packet->flags           = avio_rl16(pb);

        packet->bit_rate      = packet->bits_per_sample *
                                packet->sample_rate *
                                packet->channels;
197
        packet->block_align   = XMV_BLOCK_ALIGN_SIZE * packet->channels;
198 199 200 201
        packet->block_samples = 64;
        packet->codec_id      = ff_wav_codec_get_id(packet->compression,
                                                    packet->bits_per_sample);

Sven Hesse's avatar
Sven Hesse committed
202 203 204 205 206 207 208
        packet->stream_index = -1;

        packet->frame_size  = 0;
        packet->block_count = 0;

        /* TODO: ADPCM'd 5.1 sound is encoded in three separate streams.
         *       Those need to be interleaved to a proper 5.1 stream. */
209
        if (packet->flags & XMV_AUDIO_ADPCM51)
Sven Hesse's avatar
Sven Hesse committed
210
            av_log(s, AV_LOG_WARNING, "Unsupported 5.1 ADPCM audio stream "
211
                                      "(0x%04X)\n", packet->flags);
Sven Hesse's avatar
Sven Hesse committed
212

213
        if (!packet->channels || packet->sample_rate <= 0 ||
214
             packet->channels >= UINT16_MAX / XMV_BLOCK_ALIGN_SIZE) {
215
            av_log(s, AV_LOG_ERROR, "Invalid parameters for audio track %"PRIu16".\n",
216 217 218 219
                   audio_track);
            ret = AVERROR_INVALIDDATA;
            goto fail;
        }
Sven Hesse's avatar
Sven Hesse committed
220 221 222
    }


223
    /* Initialize the packet context */
Sven Hesse's avatar
Sven Hesse committed
224 225

    xmv->next_packet_offset = avio_tell(pb);
226 227
    xmv->next_packet_size   = this_packet_size - xmv->next_packet_offset;
    xmv->stream_count       = xmv->audio_track_count + 1;
Sven Hesse's avatar
Sven Hesse committed
228 229

    return 0;
230 231 232 233

fail:
    xmv_read_close(s);
    return ret;
Sven Hesse's avatar
Sven Hesse committed
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
}

static void xmv_read_extradata(uint8_t *extradata, AVIOContext *pb)
{
    /* Read the XMV extradata */

    uint32_t data = avio_rl32(pb);

    int mspel_bit        = !!(data & 0x01);
    int loop_filter      = !!(data & 0x02);
    int abt_flag         = !!(data & 0x04);
    int j_type_bit       = !!(data & 0x08);
    int top_left_mv_flag = !!(data & 0x10);
    int per_mb_rl_bit    = !!(data & 0x20);
    int slice_count      = (data >> 6) & 7;

    /* Write it back as standard WMV2 extradata */

    data = 0;

    data |= mspel_bit        << 15;
    data |= loop_filter      << 14;
    data |= abt_flag         << 13;
    data |= j_type_bit       << 12;
    data |= top_left_mv_flag << 11;
    data |= per_mb_rl_bit    << 10;
    data |= slice_count      <<  7;

    AV_WB32(extradata, data);
}

static int xmv_process_packet_header(AVFormatContext *s)
{
    XMVDemuxContext *xmv = s->priv_data;
    AVIOContext     *pb  = s->pb;
269
    int ret;
Sven Hesse's avatar
Sven Hesse committed
270 271 272

    uint8_t  data[8];
    uint16_t audio_track;
273
    uint64_t data_offset;
Sven Hesse's avatar
Sven Hesse committed
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289

    /* Next packet size */
    xmv->next_packet_size = avio_rl32(pb);

    /* Packet video header */

    if (avio_read(pb, data, 8) != 8)
        return AVERROR(EIO);

    xmv->video.data_size     = AV_RL32(data) & 0x007FFFFF;

    xmv->video.current_frame = 0;
    xmv->video.frame_count   = (AV_RL32(data) >> 23) & 0xFF;

    xmv->video.has_extradata = (data[3] & 0x80) != 0;

290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
    if (!xmv->video.created) {
        AVStream *vst = avformat_new_stream(s, NULL);
        if (!vst)
            return AVERROR(ENOMEM);

        avpriv_set_pts_info(vst, 32, 1, 1000);

        vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
        vst->codecpar->codec_id   = AV_CODEC_ID_WMV2;
        vst->codecpar->codec_tag  = MKBETAG('W', 'M', 'V', '2');
        vst->codecpar->width      = xmv->video_width;
        vst->codecpar->height     = xmv->video_height;

        vst->duration = xmv->video_duration;

        xmv->video.stream_index = vst->index;

        xmv->video.created = 1;
    }

Sven Hesse's avatar
Sven Hesse committed
310 311 312 313
    /* Adding the audio data sizes and the video data size keeps you 4 bytes
     * short for every audio track. But as playing around with XMV files with
     * ADPCM audio showed, taking the extra 4 bytes from the audio data gives
     * you either completely distorted audio or click (when skipping the
314
     * remaining 68 bytes of the ADPCM block). Subtracting 4 bytes for every
Sven Hesse's avatar
Sven Hesse committed
315 316 317 318 319 320 321 322 323
     * audio track from the video data works at least for the audio. Probably
     * some alignment thing?
     * The video data has (always?) lots of padding, so it should work out...
     */
    xmv->video.data_size -= xmv->audio_track_count * 4;

    xmv->current_stream = 0;
    if (!xmv->video.frame_count) {
        xmv->video.frame_count = 1;
324
        xmv->current_stream    = xmv->stream_count > 1;
Sven Hesse's avatar
Sven Hesse committed
325 326 327 328 329 330 331 332 333 334
    }

    /* Packet audio header */

    for (audio_track = 0; audio_track < xmv->audio_track_count; audio_track++) {
        XMVAudioPacket *packet = &xmv->audio[audio_track];

        if (avio_read(pb, data, 4) != 4)
            return AVERROR(EIO);

335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
        if (!packet->created) {
            AVStream *ast = avformat_new_stream(s, NULL);
            if (!ast)
                return AVERROR(ENOMEM);

            ast->codecpar->codec_type            = AVMEDIA_TYPE_AUDIO;
            ast->codecpar->codec_id              = packet->codec_id;
            ast->codecpar->codec_tag             = packet->compression;
            ast->codecpar->channels              = packet->channels;
            ast->codecpar->sample_rate           = packet->sample_rate;
            ast->codecpar->bits_per_coded_sample = packet->bits_per_sample;
            ast->codecpar->bit_rate              = packet->bit_rate;
            ast->codecpar->block_align           = 36 * packet->channels;

            avpriv_set_pts_info(ast, 32, packet->block_samples, packet->sample_rate);

            packet->stream_index = ast->index;

            ast->duration = xmv->video_duration;

            packet->created = 1;
        }

Sven Hesse's avatar
Sven Hesse committed
358 359 360 361 362 363 364 365 366
        packet->data_size = AV_RL32(data) & 0x007FFFFF;
        if ((packet->data_size == 0) && (audio_track != 0))
            /* This happens when I create an XMV with several identical audio
             * streams. From the size calculations, duplicating the previous
             * stream's size works out, but the track data itself is silent.
             * Maybe this should also redirect the offset to the previous track?
             */
            packet->data_size = xmv->audio[audio_track - 1].data_size;

367
        /* Carve up the audio data in frame_count slices */
Sven Hesse's avatar
Sven Hesse committed
368
        packet->frame_size  = packet->data_size  / xmv->video.frame_count;
369
        packet->frame_size -= packet->frame_size % packet->block_align;
Sven Hesse's avatar
Sven Hesse committed
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
    }

    /* Packet data offsets */

    data_offset = avio_tell(pb);

    xmv->video.data_offset = data_offset;
    data_offset += xmv->video.data_size;

    for (audio_track = 0; audio_track < xmv->audio_track_count; audio_track++) {
        xmv->audio[audio_track].data_offset = data_offset;
        data_offset += xmv->audio[audio_track].data_size;
    }

    /* Video frames header */

    /* Read new video extra data */
    if (xmv->video.data_size > 0) {
        if (xmv->video.has_extradata) {
            xmv_read_extradata(xmv->video.extradata, pb);

            xmv->video.data_size   -= 4;
            xmv->video.data_offset += 4;

            if (xmv->video.stream_index >= 0) {
                AVStream *vst = s->streams[xmv->video.stream_index];

Michael Niedermayer's avatar
Michael Niedermayer committed
397
                av_assert0(xmv->video.stream_index < s->nb_streams);
Sven Hesse's avatar
Sven Hesse committed
398

399
                if (vst->codecpar->extradata_size < 4) {
400
                    av_freep(&vst->codecpar->extradata);
Sven Hesse's avatar
Sven Hesse committed
401

402
                    if ((ret = ff_alloc_extradata(vst->codecpar, 4)) < 0)
403
                        return ret;
Sven Hesse's avatar
Sven Hesse committed
404 405
                }

406
                memcpy(vst->codecpar->extradata, xmv->video.extradata, 4);
Sven Hesse's avatar
Sven Hesse committed
407 408 409 410 411 412 413 414 415 416 417 418 419
            }
        }
    }

    return 0;
}

static int xmv_fetch_new_packet(AVFormatContext *s)
{
    XMVDemuxContext *xmv = s->priv_data;
    AVIOContext     *pb  = s->pb;
    int result;

Paul B Mahol's avatar
Paul B Mahol committed
420 421 422
    if (xmv->this_packet_offset == xmv->next_packet_offset)
        return AVERROR_EOF;

Sven Hesse's avatar
Sven Hesse committed
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
    /* Seek to it */
    xmv->this_packet_offset = xmv->next_packet_offset;
    if (avio_seek(pb, xmv->this_packet_offset, SEEK_SET) != xmv->this_packet_offset)
        return AVERROR(EIO);

    /* Update the size */
    xmv->this_packet_size = xmv->next_packet_size;
    if (xmv->this_packet_size < (12 + xmv->audio_track_count * 4))
        return AVERROR(EIO);

    /* Process the header */
    result = xmv_process_packet_header(s);
    if (result)
        return result;

    /* Update the offset */
    xmv->next_packet_offset = xmv->this_packet_offset + xmv->this_packet_size;

    return 0;
}

static int xmv_fetch_audio_packet(AVFormatContext *s,
                                  AVPacket *pkt, uint32_t stream)
{
    XMVDemuxContext *xmv   = s->priv_data;
    AVIOContext     *pb    = s->pb;
    XMVAudioPacket  *audio = &xmv->audio[stream];

    uint32_t data_size;
    uint32_t block_count;
    int result;

    /* Seek to it */
    if (avio_seek(pb, audio->data_offset, SEEK_SET) != audio->data_offset)
        return AVERROR(EIO);

    if ((xmv->video.current_frame + 1) < xmv->video.frame_count)
        /* Not the last frame, get at most frame_size bytes. */
        data_size = FFMIN(audio->frame_size, audio->data_size);
    else
        /* Last frame, get the rest. */
        data_size = audio->data_size;

    /* Read the packet */
    result = av_get_packet(pb, pkt, data_size);
    if (result <= 0)
        return result;

    pkt->stream_index = audio->stream_index;

    /* Calculate the PTS */

475
    block_count = data_size / audio->block_align;
Sven Hesse's avatar
Sven Hesse committed
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499

    pkt->duration = block_count;
    pkt->pts      = audio->block_count;
    pkt->dts      = AV_NOPTS_VALUE;

    audio->block_count += block_count;

    /* Advance offset */
    audio->data_size   -= data_size;
    audio->data_offset += data_size;

    return 0;
}

static int xmv_fetch_video_packet(AVFormatContext *s,
                                  AVPacket *pkt)
{
    XMVDemuxContext *xmv   = s->priv_data;
    AVIOContext     *pb    = s->pb;
    XMVVideoPacket  *video = &xmv->video;

    int result;
    uint32_t frame_header;
    uint32_t frame_size, frame_timestamp;
500
    uint8_t *data, *end;
Sven Hesse's avatar
Sven Hesse committed
501 502 503 504 505 506 507 508 509 510 511 512 513 514

    /* Seek to it */
    if (avio_seek(pb, video->data_offset, SEEK_SET) != video->data_offset)
        return AVERROR(EIO);

    /* Read the frame header */
    frame_header = avio_rl32(pb);

    frame_size      = (frame_header & 0x1FFFF) * 4 + 4;
    frame_timestamp = (frame_header >> 17);

    if ((frame_size + 4) > video->data_size)
        return AVERROR(EIO);

515 516 517
    /* Get the packet data */
    result = av_get_packet(pb, pkt, frame_size);
    if (result != frame_size)
Sven Hesse's avatar
Sven Hesse committed
518 519 520 521 522 523
        return result;

    /* Contrary to normal WMV2 video, the bit stream in XMV's
     * WMV2 is little-endian.
     * TODO: This manual swap is of course suboptimal.
     */
524 525
    for (data = pkt->data, end = pkt->data + frame_size; data < end; data += 4)
        AV_WB32(data, AV_RL32(data));
Sven Hesse's avatar
Sven Hesse committed
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571

    pkt->stream_index = video->stream_index;

    /* Calculate the PTS */

    video->last_pts = frame_timestamp + video->pts;

    pkt->duration = 0;
    pkt->pts      = video->last_pts;
    pkt->dts      = AV_NOPTS_VALUE;

    video->pts += frame_timestamp;

    /* Keyframe? */
    pkt->flags = (pkt->data[0] & 0x80) ? 0 : AV_PKT_FLAG_KEY;

    /* Advance offset */
    video->data_size   -= frame_size + 4;
    video->data_offset += frame_size + 4;

    return 0;
}

static int xmv_read_packet(AVFormatContext *s,
                           AVPacket *pkt)
{
    XMVDemuxContext *xmv = s->priv_data;
    int result;

    if (xmv->video.current_frame == xmv->video.frame_count) {
        /* No frames left in this packet, so we fetch a new one */

        result = xmv_fetch_new_packet(s);
        if (result)
            return result;
    }

    if (xmv->current_stream == 0) {
        /* Fetch a video frame */

        result = xmv_fetch_video_packet(s, pkt);
    } else {
        /* Fetch an audio frame */

        result = xmv_fetch_audio_packet(s, pkt, xmv->current_stream - 1);
    }
572 573 574
    if (result) {
        xmv->current_stream = 0;
        xmv->video.current_frame = xmv->video.frame_count;
575
        return result;
576
    }
577

Sven Hesse's avatar
Sven Hesse committed
578 579 580 581 582 583 584 585 586 587 588 589 590

    /* Increase our counters */
    if (++xmv->current_stream >= xmv->stream_count) {
        xmv->current_stream       = 0;
        xmv->video.current_frame += 1;
    }

    return 0;
}

AVInputFormat ff_xmv_demuxer = {
    .name           = "xmv",
    .long_name      = NULL_IF_CONFIG_SMALL("Microsoft XMV"),
591
    .extensions     = "xmv",
Sven Hesse's avatar
Sven Hesse committed
592 593 594 595 596 597
    .priv_data_size = sizeof(XMVDemuxContext),
    .read_probe     = xmv_probe,
    .read_header    = xmv_read_header,
    .read_packet    = xmv_read_packet,
    .read_close     = xmv_read_close,
};