sierravmd.c 11.2 KB
Newer Older
1 2
/*
 * Sierra VMD Format Demuxer
3
 * Copyright (c) 2004 The FFmpeg project
4
 *
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
 * Sierra VMD file demuxer
 * by Vladimir "VAG" Gneushev (vagsoft at mail.ru)
26 27
 * for more information on the Sierra VMD file format, visit:
 *   http://www.pcisys.net/~melanson/codecs/
28 29
 */

30
#include "libavutil/channel_layout.h"
31
#include "libavutil/intreadwrite.h"
32
#include "avformat.h"
33
#include "internal.h"
34
#include "avio_internal.h"
35 36 37 38

#define VMD_HEADER_SIZE 0x0330
#define BYTES_PER_FRAME_RECORD 16

39
typedef struct vmd_frame {
40
  int stream_index;
41
  int64_t frame_offset;
42 43 44 45
  unsigned int frame_size;
  int64_t pts;
  int keyframe;
  unsigned char frame_record[BYTES_PER_FRAME_RECORD];
46
} vmd_frame;
47 48 49 50 51 52

typedef struct VmdDemuxContext {
    int video_stream_index;
    int audio_stream_index;

    unsigned int frame_count;
53
    unsigned int frames_per_block;
54
    vmd_frame *frame_table;
55
    unsigned int current_frame;
56
    int is_indeo3;
57

58 59
    int sample_rate;
    int64_t audio_sample_counter;
60
    int skiphdr;
61

62 63 64 65 66
    unsigned char vmd_header[VMD_HEADER_SIZE];
} VmdDemuxContext;

static int vmd_probe(AVProbeData *p)
{
67 68
    int w, h, sample_rate;
    if (p->buf_size < 806)
69
        return 0;
70 71
    /* check if the first 2 bytes of the file contain the appropriate size
     * of a VMD header chunk */
72
    if (AV_RL16(&p->buf[0]) != VMD_HEADER_SIZE - 2)
73
        return 0;
74 75
    w = AV_RL16(&p->buf[12]);
    h = AV_RL16(&p->buf[14]);
76 77 78
    sample_rate = AV_RL16(&p->buf[804]);
    if ((!w || w > 2048 || !h || h > 2048) &&
        sample_rate != 22050)
79
        return 0;
80 81

    /* only return half certainty since this check is a bit sketchy */
82
    return AVPROBE_SCORE_EXTENSION;
83 84
}

85
static int vmd_read_header(AVFormatContext *s)
86
{
87
    VmdDemuxContext *vmd = s->priv_data;
88
    AVIOContext *pb = s->pb;
89
    AVStream *st = NULL, *vst = NULL;
90 91 92
    unsigned int toc_offset;
    unsigned char *raw_frame_table;
    int raw_frame_table_size;
93
    int64_t current_offset;
94
    int i, j, ret;
95
    int width, height;
96
    unsigned int total_frames;
97
    int64_t current_audio_pts = 0;
98
    unsigned char chunk[BYTES_PER_FRAME_RECORD];
99
    int num, den;
100
    int sound_buffers;
101 102

    /* fetch the main header, including the 2 header length bytes */
103
    avio_seek(pb, 0, SEEK_SET);
104
    if (avio_read(pb, vmd->vmd_header, VMD_HEADER_SIZE) != VMD_HEADER_SIZE)
105
        return AVERROR(EIO);
106

107 108 109
    width = AV_RL16(&vmd->vmd_header[12]);
    height = AV_RL16(&vmd->vmd_header[14]);
    if (width && height) {
110 111 112 113 114 115 116 117 118 119 120
        if(vmd->vmd_header[24] == 'i' && vmd->vmd_header[25] == 'v' && vmd->vmd_header[26] == '3') {
            vmd->is_indeo3 = 1;
        } else {
            vmd->is_indeo3 = 0;
        }
        /* start up the decoders */
        vst = avformat_new_stream(s, NULL);
        if (!vst)
            return AVERROR(ENOMEM);
        avpriv_set_pts_info(vst, 33, 1, 10);
        vmd->video_stream_index = vst->index;
121 122 123 124 125 126 127 128
        vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
        vst->codecpar->codec_id = vmd->is_indeo3 ? AV_CODEC_ID_INDEO3 : AV_CODEC_ID_VMDVIDEO;
        vst->codecpar->codec_tag = 0;  /* no fourcc */
        vst->codecpar->width = width;
        vst->codecpar->height = height;
        if(vmd->is_indeo3 && vst->codecpar->width > 320){
            vst->codecpar->width >>= 1;
            vst->codecpar->height >>= 1;
129
        }
130
        if (ff_alloc_extradata(vst->codecpar, VMD_HEADER_SIZE))
131
            return AVERROR(ENOMEM);
132
        memcpy(vst->codecpar->extradata, vmd->vmd_header, VMD_HEADER_SIZE);
133
    }
134 135

    /* if sample rate is 0, assume no audio */
136
    vmd->sample_rate = AV_RL16(&vmd->vmd_header[804]);
137
    if (vmd->sample_rate) {
138
        st = avformat_new_stream(s, NULL);
139
        if (!st)
140
            return AVERROR(ENOMEM);
141
        vmd->audio_stream_index = st->index;
142 143 144 145 146 147 148 149
        st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
        st->codecpar->codec_id   = AV_CODEC_ID_VMDAUDIO;
        st->codecpar->codec_tag  = 0;  /* no fourcc */
        st->codecpar->sample_rate = vmd->sample_rate;
        st->codecpar->block_align = AV_RL16(&vmd->vmd_header[806]);
        if (st->codecpar->block_align & 0x8000) {
            st->codecpar->bits_per_coded_sample = 16;
            st->codecpar->block_align = -(st->codecpar->block_align - 0x10000);
150
        } else {
151
            st->codecpar->bits_per_coded_sample = 8;
152
        }
153 154 155 156 157 158 159 160 161 162 163 164 165
        if (vmd->vmd_header[811] & 0x80) {
            st->codecpar->channels       = 2;
            st->codecpar->channel_layout = AV_CH_LAYOUT_STEREO;
        } else if (vmd->vmd_header[811] & 0x2) {
            /* Shivers 2 stereo audio */
            /* Frame length is for 1 channel */
            st->codecpar->channels       = 2;
            st->codecpar->channel_layout = AV_CH_LAYOUT_STEREO;
            st->codecpar->block_align = st->codecpar->block_align << 1;
        } else {
            st->codecpar->channels       = 1;
            st->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
        }
166 167
        st->codecpar->bit_rate = st->codecpar->sample_rate *
            st->codecpar->bits_per_coded_sample * st->codecpar->channels;
168

169
        /* calculate pts */
170 171
        num = st->codecpar->block_align;
        den = st->codecpar->sample_rate * st->codecpar->channels;
172
        av_reduce(&num, &den, num, den, (1UL<<31)-1);
173
        if (vst)
174
            avpriv_set_pts_info(vst, 33, num, den);
175
        avpriv_set_pts_info(st, 33, num, den);
176 177
    }

178 179 180
    toc_offset = AV_RL32(&vmd->vmd_header[812]);
    vmd->frame_count = AV_RL16(&vmd->vmd_header[6]);
    vmd->frames_per_block = AV_RL16(&vmd->vmd_header[18]);
181
    avio_seek(pb, toc_offset, SEEK_SET);
182 183 184

    raw_frame_table = NULL;
    vmd->frame_table = NULL;
185
    sound_buffers = AV_RL16(&vmd->vmd_header[808]);
186
    raw_frame_table_size = vmd->frame_count * 6;
187
    if(vmd->frame_count * vmd->frames_per_block >= UINT_MAX / sizeof(vmd_frame) - sound_buffers){
188 189 190
        av_log(s, AV_LOG_ERROR, "vmd->frame_count * vmd->frames_per_block too large\n");
        return -1;
    }
191
    raw_frame_table = av_malloc(raw_frame_table_size);
192
    vmd->frame_table = av_malloc_array(vmd->frame_count * vmd->frames_per_block + sound_buffers, sizeof(vmd_frame));
193
    if (!raw_frame_table || !vmd->frame_table) {
194 195
        ret = AVERROR(ENOMEM);
        goto error;
196
    }
197
    if (avio_read(pb, raw_frame_table, raw_frame_table_size) !=
198
        raw_frame_table_size) {
199 200
        ret = AVERROR(EIO);
        goto error;
201 202
    }

203 204 205
    total_frames = 0;
    for (i = 0; i < vmd->frame_count; i++) {

206
        current_offset = AV_RL32(&raw_frame_table[6 * i + 2]);
207 208 209 210 211 212

        /* handle each entry in index block */
        for (j = 0; j < vmd->frames_per_block; j++) {
            int type;
            uint32_t size;

213 214 215 216 217 218
            if ((ret = avio_read(pb, chunk, BYTES_PER_FRAME_RECORD)) != BYTES_PER_FRAME_RECORD) {
                av_log(s, AV_LOG_ERROR, "Failed to read frame record\n");
                if (ret >= 0)
                    ret = AVERROR_INVALIDDATA;
                goto error;
            }
219
            type = chunk[0];
220
            size = AV_RL32(&chunk[2]);
221 222 223 224 225
            if (size > INT_MAX / 2) {
                av_log(s, AV_LOG_ERROR, "Invalid frame size\n");
                ret = AVERROR_INVALIDDATA;
                goto error;
            }
226
            if(!size && type != 1)
227 228 229
                continue;
            switch(type) {
            case 1: /* Audio Chunk */
230
                if (!st) break;
231
                /* first audio chunk contains several audio buffers */
232 233 234 235 236 237
                vmd->frame_table[total_frames].frame_offset = current_offset;
                vmd->frame_table[total_frames].stream_index = vmd->audio_stream_index;
                vmd->frame_table[total_frames].frame_size = size;
                memcpy(vmd->frame_table[total_frames].frame_record, chunk, BYTES_PER_FRAME_RECORD);
                vmd->frame_table[total_frames].pts = current_audio_pts;
                total_frames++;
238
                if(!current_audio_pts)
239
                    current_audio_pts += sound_buffers - 1;
240 241
                else
                    current_audio_pts++;
242 243 244 245
                break;
            case 2: /* Video Chunk */
                vmd->frame_table[total_frames].frame_offset = current_offset;
                vmd->frame_table[total_frames].stream_index = vmd->video_stream_index;
246
                vmd->frame_table[total_frames].frame_size = size;
247
                memcpy(vmd->frame_table[total_frames].frame_record, chunk, BYTES_PER_FRAME_RECORD);
248
                vmd->frame_table[total_frames].pts = i;
249 250 251 252
                total_frames++;
                break;
            }
            current_offset += size;
253 254 255 256 257 258
        }
    }

    av_free(raw_frame_table);

    vmd->current_frame = 0;
259
    vmd->frame_count = total_frames;
260 261

    return 0;
262 263

error:
264 265
    av_freep(&raw_frame_table);
    av_freep(&vmd->frame_table);
266
    return ret;
267 268 269 270 271
}

static int vmd_read_packet(AVFormatContext *s,
                           AVPacket *pkt)
{
272
    VmdDemuxContext *vmd = s->priv_data;
273
    AVIOContext *pb = s->pb;
274
    int ret = 0;
275
    vmd_frame *frame;
276 277

    if (vmd->current_frame >= vmd->frame_count)
Piotr Bandurski's avatar
Piotr Bandurski committed
278
        return AVERROR_EOF;
279 280 281

    frame = &vmd->frame_table[vmd->current_frame];
    /* position the stream (will probably be there already) */
282
    avio_seek(pb, frame->frame_offset, SEEK_SET);
283

284 285
    if(ffio_limit(pb, frame->frame_size) != frame->frame_size)
        return AVERROR(EIO);
286
    if (av_new_packet(pkt, frame->frame_size + BYTES_PER_FRAME_RECORD))
287
        return AVERROR(ENOMEM);
288
    pkt->pos= avio_tell(pb);
289
    memcpy(pkt->data, frame->frame_record, BYTES_PER_FRAME_RECORD);
290
    if(vmd->is_indeo3 && frame->frame_record[0] == 0x02)
291
        ret = avio_read(pb, pkt->data, frame->frame_size);
292
    else
293
        ret = avio_read(pb, pkt->data + BYTES_PER_FRAME_RECORD,
294
            frame->frame_size);
295

296
    if (ret != frame->frame_size) {
297
        av_packet_unref(pkt);
298
        ret = AVERROR(EIO);
299
    }
300
    pkt->stream_index = frame->stream_index;
301
    pkt->pts = frame->pts;
302
    av_log(s, AV_LOG_DEBUG, " dispatching %s frame with %d bytes and pts %"PRId64"\n",
303 304
            (frame->frame_record[0] == 0x02) ? "video" : "audio",
            frame->frame_size + BYTES_PER_FRAME_RECORD,
305
            pkt->pts);
306 307 308 309 310 311 312 313

    vmd->current_frame++;

    return ret;
}

static int vmd_read_close(AVFormatContext *s)
{
314
    VmdDemuxContext *vmd = s->priv_data;
315

316
    av_freep(&vmd->frame_table);
317 318 319 320

    return 0;
}

321
AVInputFormat ff_vmd_demuxer = {
322
    .name           = "vmd",
323
    .long_name      = NULL_IF_CONFIG_SMALL("Sierra VMD"),
324 325 326 327 328
    .priv_data_size = sizeof(VmdDemuxContext),
    .read_probe     = vmd_probe,
    .read_header    = vmd_read_header,
    .read_packet    = vmd_read_packet,
    .read_close     = vmd_read_close,
329
};