mvi.c 4.97 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * Motion Pixels MVI Demuxer
 * Copyright (c) 2008 Gregory Montoir (cyx@users.sourceforge.net)
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * 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.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * 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
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

22 23
#include <inttypes.h>

Justin Ruggles's avatar
Justin Ruggles committed
24
#include "libavutil/channel_layout.h"
25
#include "avformat.h"
26
#include "internal.h"
27 28 29 30 31 32 33

#define MVI_FRAC_BITS 10

#define MVI_AUDIO_STREAM_INDEX 0
#define MVI_VIDEO_STREAM_INDEX 1

typedef struct MviDemuxContext {
34
    unsigned int (*get_int)(AVIOContext *);
35 36 37 38 39 40 41
    uint32_t audio_data_size;
    uint64_t audio_size_counter;
    uint64_t audio_frame_size;
    int audio_size_left;
    int video_frame_size;
} MviDemuxContext;

42
static int read_header(AVFormatContext *s)
43 44
{
    MviDemuxContext *mvi = s->priv_data;
45
    AVIOContext *pb = s->pb;
46 47 48
    AVStream *ast, *vst;
    unsigned int version, frames_count, msecs_per_frame, player_version;

49
    ast = avformat_new_stream(s, NULL);
50 51 52
    if (!ast)
        return AVERROR(ENOMEM);

53
    vst = avformat_new_stream(s, NULL);
54 55 56
    if (!vst)
        return AVERROR(ENOMEM);

57
    if (ff_alloc_extradata(vst->codecpar, 2))
58
        return AVERROR(ENOMEM);
59

60
    version                  = avio_r8(pb);
61 62
    vst->codecpar->extradata[0] = avio_r8(pb);
    vst->codecpar->extradata[1] = avio_r8(pb);
63 64
    frames_count             = avio_rl32(pb);
    msecs_per_frame          = avio_rl32(pb);
65 66
    vst->codecpar->width        = avio_rl16(pb);
    vst->codecpar->height       = avio_rl16(pb);
67
    avio_r8(pb);
68
    ast->codecpar->sample_rate  = avio_rl16(pb);
69 70 71 72 73
    mvi->audio_data_size     = avio_rl32(pb);
    avio_r8(pb);
    player_version           = avio_rl32(pb);
    avio_rl16(pb);
    avio_r8(pb);
74 75 76 77 78 79 80 81 82

    if (frames_count == 0 || mvi->audio_data_size == 0)
        return AVERROR_INVALIDDATA;

    if (version != 7 || player_version > 213) {
        av_log(s, AV_LOG_ERROR, "unhandled version (%d,%d)\n", version, player_version);
        return AVERROR_INVALIDDATA;
    }

83 84 85 86 87 88 89
    avpriv_set_pts_info(ast, 64, 1, ast->codecpar->sample_rate);
    ast->codecpar->codec_type      = AVMEDIA_TYPE_AUDIO;
    ast->codecpar->codec_id        = AV_CODEC_ID_PCM_U8;
    ast->codecpar->channels        = 1;
    ast->codecpar->channel_layout  = AV_CH_LAYOUT_MONO;
    ast->codecpar->bits_per_coded_sample = 8;
    ast->codecpar->bit_rate        = ast->codecpar->sample_rate * 8;
90

91
    avpriv_set_pts_info(vst, 64, msecs_per_frame, 1000000);
Anton Khirnov's avatar
Anton Khirnov committed
92
    vst->avg_frame_rate    = av_inv_q(vst->time_base);
93 94
    vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
    vst->codecpar->codec_id   = AV_CODEC_ID_MOTIONPIXELS;
95

96
    mvi->get_int = (vst->codecpar->width * vst->codecpar->height < (1 << 16)) ? avio_rl16 : avio_rl24;
97 98

    mvi->audio_frame_size   = ((uint64_t)mvi->audio_data_size << MVI_FRAC_BITS) / frames_count;
99
    if (mvi->audio_frame_size <= 1 << MVI_FRAC_BITS - 1) {
100 101
        av_log(s, AV_LOG_ERROR,
               "Invalid audio_data_size (%"PRIu32") or frames_count (%u)\n",
102
               mvi->audio_data_size, frames_count);
103 104
        return AVERROR_INVALIDDATA;
    }
105

106
    mvi->audio_size_counter = (ast->codecpar->sample_rate * 830 / mvi->audio_frame_size - 1) * mvi->audio_frame_size;
107 108 109 110 111 112 113 114 115
    mvi->audio_size_left    = mvi->audio_data_size;

    return 0;
}

static int read_packet(AVFormatContext *s, AVPacket *pkt)
{
    int ret, count;
    MviDemuxContext *mvi = s->priv_data;
116
    AVIOContext *pb = s->pb;
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138

    if (mvi->video_frame_size == 0) {
        mvi->video_frame_size = (mvi->get_int)(pb);
        if (mvi->audio_size_left == 0)
            return AVERROR(EIO);
        count = (mvi->audio_size_counter + mvi->audio_frame_size + 512) >> MVI_FRAC_BITS;
        if (count > mvi->audio_size_left)
            count = mvi->audio_size_left;
        if ((ret = av_get_packet(pb, pkt, count)) < 0)
            return ret;
        pkt->stream_index = MVI_AUDIO_STREAM_INDEX;
        mvi->audio_size_left -= count;
        mvi->audio_size_counter += mvi->audio_frame_size - (count << MVI_FRAC_BITS);
    } else {
        if ((ret = av_get_packet(pb, pkt, mvi->video_frame_size)) < 0)
            return ret;
        pkt->stream_index = MVI_VIDEO_STREAM_INDEX;
        mvi->video_frame_size = 0;
    }
    return 0;
}

139
AVInputFormat ff_mvi_demuxer = {
140
    .name           = "mvi",
141
    .long_name      = NULL_IF_CONFIG_SMALL("Motion Pixels MVI"),
142 143 144
    .priv_data_size = sizeof(MviDemuxContext),
    .read_header    = read_header,
    .read_packet    = read_packet,
145
    .extensions     = "mvi",
146
};