sierravmd.c 9.83 KB
Newer Older
1 2 3 4
/*
 * Sierra VMD Format Demuxer
 * Copyright (c) 2004 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
 * 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/intreadwrite.h"
31
#include "avformat.h"
32
#include "internal.h"
33
#include "avio_internal.h"
34 35 36 37 38 39

#define VMD_HEADER_SIZE 0x0330
#define BYTES_PER_FRAME_RECORD 16

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

typedef struct VmdDemuxContext {
    int video_stream_index;
    int audio_stream_index;

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

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

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

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

    /* only return half certainty since this check is a bit sketchy */
    return AVPROBE_SCORE_MAX / 2;
}

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

    /* fetch the main header, including the 2 header length bytes */
99
    avio_seek(pb, 0, SEEK_SET);
100
    if (avio_read(pb, vmd->vmd_header, VMD_HEADER_SIZE) != VMD_HEADER_SIZE)
101
        return AVERROR(EIO);
102

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

    /* if sample rate is 0, assume no audio */
127
    vmd->sample_rate = AV_RL16(&vmd->vmd_header[804]);
128
    if (vmd->sample_rate) {
129
        st = avformat_new_stream(s, NULL);
130
        if (!st)
131
            return AVERROR(ENOMEM);
132
        vmd->audio_stream_index = st->index;
133
        st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
134
        st->codec->codec_id = AV_CODEC_ID_VMDAUDIO;
135
        st->codec->codec_tag = 0;  /* no fourcc */
136
        st->codec->channels = (vmd->vmd_header[811] & 0x80) ? 2 : 1;
137
        st->codec->sample_rate = vmd->sample_rate;
138
        st->codec->block_align = AV_RL16(&vmd->vmd_header[806]);
139
        if (st->codec->block_align & 0x8000) {
140
            st->codec->bits_per_coded_sample = 16;
141
            st->codec->block_align = -(st->codec->block_align - 0x10000);
142
        } else {
143
            st->codec->bits_per_coded_sample = 8;
144
        }
145
        st->codec->bit_rate = st->codec->sample_rate *
146
            st->codec->bits_per_coded_sample * st->codec->channels;
147

148 149 150 151
        /* calculate pts */
        num = st->codec->block_align;
        den = st->codec->sample_rate * st->codec->channels;
        av_reduce(&den, &num, den, num, (1UL<<31)-1);
152 153
        avpriv_set_pts_info(vst, 33, num, den);
        avpriv_set_pts_info(st, 33, num, den);
154 155
    }

156 157 158
    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]);
159
    avio_seek(pb, toc_offset, SEEK_SET);
160 161 162

    raw_frame_table = NULL;
    vmd->frame_table = NULL;
163
    sound_buffers = AV_RL16(&vmd->vmd_header[808]);
164
    raw_frame_table_size = vmd->frame_count * 6;
165
    if(vmd->frame_count * vmd->frames_per_block >= UINT_MAX / sizeof(vmd_frame) - sound_buffers){
166 167 168
        av_log(s, AV_LOG_ERROR, "vmd->frame_count * vmd->frames_per_block too large\n");
        return -1;
    }
169
    raw_frame_table = av_malloc(raw_frame_table_size);
170
    vmd->frame_table = av_malloc((vmd->frame_count * vmd->frames_per_block + sound_buffers) * sizeof(vmd_frame));
171 172 173
    if (!raw_frame_table || !vmd->frame_table) {
        av_free(raw_frame_table);
        av_free(vmd->frame_table);
174
        return AVERROR(ENOMEM);
175
    }
176
    if (avio_read(pb, raw_frame_table, raw_frame_table_size) !=
177 178 179
        raw_frame_table_size) {
        av_free(raw_frame_table);
        av_free(vmd->frame_table);
180
        return AVERROR(EIO);
181 182
    }

183 184 185
    total_frames = 0;
    for (i = 0; i < vmd->frame_count; i++) {

186
        current_offset = AV_RL32(&raw_frame_table[6 * i + 2]);
187 188 189 190 191 192

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

193
            avio_read(pb, chunk, BYTES_PER_FRAME_RECORD);
194
            type = chunk[0];
195
            size = AV_RL32(&chunk[2]);
196
            if(!size && type != 1)
197 198 199
                continue;
            switch(type) {
            case 1: /* Audio Chunk */
200
                if (!st) break;
201
                /* first audio chunk contains several audio buffers */
202 203 204 205 206 207
                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++;
208
                if(!current_audio_pts)
209
                    current_audio_pts += sound_buffers - 1;
210 211
                else
                    current_audio_pts++;
212 213 214 215
                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;
216
                vmd->frame_table[total_frames].frame_size = size;
217
                memcpy(vmd->frame_table[total_frames].frame_record, chunk, BYTES_PER_FRAME_RECORD);
218
                vmd->frame_table[total_frames].pts = i;
219 220 221 222
                total_frames++;
                break;
            }
            current_offset += size;
223 224 225 226 227 228
        }
    }

    av_free(raw_frame_table);

    vmd->current_frame = 0;
229
    vmd->frame_count = total_frames;
230 231 232 233 234 235 236

    return 0;
}

static int vmd_read_packet(AVFormatContext *s,
                           AVPacket *pkt)
{
237
    VmdDemuxContext *vmd = s->priv_data;
238
    AVIOContext *pb = s->pb;
239
    int ret = 0;
240
    vmd_frame *frame;
241 242

    if (vmd->current_frame >= vmd->frame_count)
243
        return AVERROR(EIO);
244 245 246

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

249 250
    if(ffio_limit(pb, frame->frame_size) != frame->frame_size)
        return AVERROR(EIO);
251
    if (av_new_packet(pkt, frame->frame_size + BYTES_PER_FRAME_RECORD))
252
        return AVERROR(ENOMEM);
253
    pkt->pos= avio_tell(pb);
254
    memcpy(pkt->data, frame->frame_record, BYTES_PER_FRAME_RECORD);
255
    if(vmd->is_indeo3 && frame->frame_record[0] == 0x02)
256
        ret = avio_read(pb, pkt->data, frame->frame_size);
257
    else
258
        ret = avio_read(pb, pkt->data + BYTES_PER_FRAME_RECORD,
259
            frame->frame_size);
260

261 262
    if (ret != frame->frame_size) {
        av_free_packet(pkt);
263
        ret = AVERROR(EIO);
264
    }
265
    pkt->stream_index = frame->stream_index;
266
    pkt->pts = frame->pts;
267
    av_log(s, AV_LOG_DEBUG, " dispatching %s frame with %d bytes and pts %"PRId64"\n",
268 269
            (frame->frame_record[0] == 0x02) ? "video" : "audio",
            frame->frame_size + BYTES_PER_FRAME_RECORD,
270
            pkt->pts);
271 272 273 274 275 276 277 278

    vmd->current_frame++;

    return ret;
}

static int vmd_read_close(AVFormatContext *s)
{
279
    VmdDemuxContext *vmd = s->priv_data;
280 281 282 283 284 285

    av_free(vmd->frame_table);

    return 0;
}

286
AVInputFormat ff_vmd_demuxer = {
287
    .name           = "vmd",
288
    .long_name      = NULL_IF_CONFIG_SMALL("Sierra VMD"),
289 290 291 292 293
    .priv_data_size = sizeof(VmdDemuxContext),
    .read_probe     = vmd_probe,
    .read_header    = vmd_read_header,
    .read_packet    = vmd_read_packet,
    .read_close     = vmd_read_close,
294
};