sierravmd.c 10.4 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 24 25
 */

/**
 * @file sierravmd.c
 * 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 31 32 33 34 35 36
 */

#include "avformat.h"

#define VMD_HEADER_SIZE 0x0330
#define BYTES_PER_FRAME_RECORD 16

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

typedef struct VmdDemuxContext {
    int video_stream_index;
    int audio_stream_index;

    unsigned int frame_count;
49
    unsigned int frames_per_block;
50
    vmd_frame *frame_table;
51 52
    unsigned int current_frame;

53 54
    int sample_rate;
    int64_t audio_sample_counter;
55
    int skiphdr;
56

57 58 59 60 61 62 63
    unsigned char vmd_header[VMD_HEADER_SIZE];
} VmdDemuxContext;

static int vmd_probe(AVProbeData *p)
{
    /* check if the first 2 bytes of the file contain the appropriate size
     * of a VMD header chunk */
64
    if (AV_RL16(&p->buf[0]) != VMD_HEADER_SIZE - 2)
65 66 67 68 69 70 71 72 73
        return 0;

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

static int vmd_read_header(AVFormatContext *s,
                           AVFormatParameters *ap)
{
74
    VmdDemuxContext *vmd = s->priv_data;
75
    ByteIOContext *pb = s->pb;
76
    AVStream *st = NULL, *vst;
77 78 79
    unsigned int toc_offset;
    unsigned char *raw_frame_table;
    int raw_frame_table_size;
80
    int64_t current_offset;
81
    int i, j;
82
    unsigned int total_frames;
83 84
    int64_t pts_inc = 1;
    int64_t current_video_pts = 0, current_audio_pts = 0;
85
    unsigned char chunk[BYTES_PER_FRAME_RECORD];
86
    int num, den;
87
    int sound_buffers;
88 89 90 91

    /* fetch the main header, including the 2 header length bytes */
    url_fseek(pb, 0, SEEK_SET);
    if (get_buffer(pb, vmd->vmd_header, VMD_HEADER_SIZE) != VMD_HEADER_SIZE)
92
        return AVERROR(EIO);
93 94

    /* start up the decoders */
95 96
    vst = av_new_stream(s, 0);
    if (!vst)
97
        return AVERROR(ENOMEM);
98 99 100 101 102
    av_set_pts_info(vst, 33, 1, 10);
    vmd->video_stream_index = vst->index;
    vst->codec->codec_type = CODEC_TYPE_VIDEO;
    vst->codec->codec_id = CODEC_ID_VMDVIDEO;
    vst->codec->codec_tag = 0;  /* no fourcc */
103 104
    vst->codec->width = AV_RL16(&vmd->vmd_header[12]);
    vst->codec->height = AV_RL16(&vmd->vmd_header[14]);
105 106 107
    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);
108 109

    /* if sample rate is 0, assume no audio */
110
    vmd->sample_rate = AV_RL16(&vmd->vmd_header[804]);
111
    if (vmd->sample_rate) {
112 113
        st = av_new_stream(s, 0);
        if (!st)
114
            return AVERROR(ENOMEM);
115
        vmd->audio_stream_index = st->index;
116 117
        st->codec->codec_type = CODEC_TYPE_AUDIO;
        st->codec->codec_id = CODEC_ID_VMDAUDIO;
118
        st->codec->codec_tag = 0;  /* no fourcc */
119
        st->codec->channels = (vmd->vmd_header[811] & 0x80) ? 2 : 1;
120
        st->codec->sample_rate = vmd->sample_rate;
121
        st->codec->block_align = AV_RL16(&vmd->vmd_header[806]);
122
        if (st->codec->block_align & 0x8000) {
123
            st->codec->bits_per_coded_sample = 16;
124
            st->codec->block_align = -(st->codec->block_align - 0x10000);
125
        } else {
126
            st->codec->bits_per_coded_sample = 8;
127
        }
128
        st->codec->bit_rate = st->codec->sample_rate *
129
            st->codec->bits_per_coded_sample * st->codec->channels;
130

131 132 133 134 135 136 137
        /* calculate pts */
        num = st->codec->block_align;
        den = st->codec->sample_rate * st->codec->channels;
        av_reduce(&den, &num, den, num, (1UL<<31)-1);
        av_set_pts_info(vst, 33, num, den);
        av_set_pts_info(st, 33, num, den);
        pts_inc = num;
138 139
    }

140 141 142
    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]);
143
    url_fseek(pb, toc_offset, SEEK_SET);
144 145 146

    raw_frame_table = NULL;
    vmd->frame_table = NULL;
147
    sound_buffers = AV_RL16(&vmd->vmd_header[808]);
148
    raw_frame_table_size = vmd->frame_count * 6;
149
    if(vmd->frame_count * vmd->frames_per_block  >= UINT_MAX / sizeof(vmd_frame)){
150 151 152
        av_log(s, AV_LOG_ERROR, "vmd->frame_count * vmd->frames_per_block too large\n");
        return -1;
    }
153
    raw_frame_table = av_malloc(raw_frame_table_size);
154
    vmd->frame_table = av_malloc((vmd->frame_count * vmd->frames_per_block + sound_buffers) * sizeof(vmd_frame));
155 156 157
    if (!raw_frame_table || !vmd->frame_table) {
        av_free(raw_frame_table);
        av_free(vmd->frame_table);
158
        return AVERROR(ENOMEM);
159
    }
160
    if (get_buffer(pb, raw_frame_table, raw_frame_table_size) !=
161 162 163
        raw_frame_table_size) {
        av_free(raw_frame_table);
        av_free(vmd->frame_table);
164
        return AVERROR(EIO);
165 166
    }

167 168 169
    total_frames = 0;
    for (i = 0; i < vmd->frame_count; i++) {

170
        current_offset = AV_RL32(&raw_frame_table[6 * i + 2]);
171 172 173 174 175 176 177 178

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

            get_buffer(pb, chunk, BYTES_PER_FRAME_RECORD);
            type = chunk[0];
179
            size = AV_RL32(&chunk[2]);
180 181 182 183
            if(!size)
                continue;
            switch(type) {
            case 1: /* Audio Chunk */
184
                if (!st) break;
185 186
                /* first audio chunk contains several audio buffers */
                if(current_audio_pts){
Kostya Shishkov's avatar
Kostya Shishkov committed
187 188 189 190 191 192 193
                    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++;
                    current_audio_pts += pts_inc;
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
                }else{
                    uint32_t flags;
                    int k;
                    int noff;
                    int64_t pos;

                    pos = url_ftell(pb);
                    url_fseek(pb, current_offset, SEEK_SET);
                    flags = get_le32(pb);
                    noff = 4;
                    url_fseek(pb, pos, SEEK_SET);
                    av_log(s, AV_LOG_DEBUG, "Sound mapping = %08X (%i bufs)\n", flags, sound_buffers);
                    for(k = 0; k < sound_buffers - 1; k++){
                        if(flags & 1) { /* silent block */
                            vmd->frame_table[total_frames].frame_size = 0;
                        }else{
                            vmd->frame_table[total_frames].frame_size = st->codec->block_align + (st->codec->block_align & 1);
                        }
                        noff += vmd->frame_table[total_frames].frame_size;
                        vmd->frame_table[total_frames].frame_offset = current_offset + noff;
                        vmd->frame_table[total_frames].stream_index = vmd->audio_stream_index;
                        memcpy(vmd->frame_table[total_frames].frame_record, chunk, BYTES_PER_FRAME_RECORD);
                        vmd->frame_table[total_frames].pts = current_audio_pts;
                        total_frames++;
                        current_audio_pts += pts_inc;
                        flags >>= 1;
                    }
                }
222 223 224 225
                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;
226
                vmd->frame_table[total_frames].frame_size = size;
227 228 229 230 231 232
                memcpy(vmd->frame_table[total_frames].frame_record, chunk, BYTES_PER_FRAME_RECORD);
                vmd->frame_table[total_frames].pts = current_video_pts;
                total_frames++;
                break;
            }
            current_offset += size;
233
        }
234
        current_video_pts += pts_inc;
235 236 237 238 239
    }

    av_free(raw_frame_table);

    vmd->current_frame = 0;
240
    vmd->frame_count = total_frames;
241 242 243 244 245 246 247

    return 0;
}

static int vmd_read_packet(AVFormatContext *s,
                           AVPacket *pkt)
{
248
    VmdDemuxContext *vmd = s->priv_data;
249
    ByteIOContext *pb = s->pb;
250
    int ret = 0;
251
    vmd_frame *frame;
252 253

    if (vmd->current_frame >= vmd->frame_count)
254
        return AVERROR(EIO);
255 256 257 258 259 260

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

    if (av_new_packet(pkt, frame->frame_size + BYTES_PER_FRAME_RECORD))
261
        return AVERROR(ENOMEM);
Michael Niedermayer's avatar
Michael Niedermayer committed
262
    pkt->pos= url_ftell(pb);
263
    memcpy(pkt->data, frame->frame_record, BYTES_PER_FRAME_RECORD);
264
    ret = get_buffer(pb, pkt->data + BYTES_PER_FRAME_RECORD,
265 266
        frame->frame_size);

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

    vmd->current_frame++;

    return ret;
}

static int vmd_read_close(AVFormatContext *s)
{
285
    VmdDemuxContext *vmd = s->priv_data;
286 287 288 289 290 291

    av_free(vmd->frame_table);

    return 0;
}

292
AVInputFormat vmd_demuxer = {
293
    "vmd",
294
    NULL_IF_CONFIG_SMALL("Sierra VMD format"),
295 296 297 298 299 300
    sizeof(VmdDemuxContext),
    vmd_probe,
    vmd_read_header,
    vmd_read_packet,
    vmd_read_close,
};