Statistics
| Branch: | Revision:

ffmpeg / libavformat / sierravmd.c @ e63a3628

History | View | Annotate | Download (9.55 KB)

1
/*
2
 * Sierra VMD Format Demuxer
3
 * Copyright (c) 2004 The ffmpeg Project
4
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * 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
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * 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
 * Sierra VMD file demuxer
25
 * by Vladimir "VAG" Gneushev (vagsoft at mail.ru)
26
 * for more information on the Sierra VMD file format, visit:
27
 *   http://www.pcisys.net/~melanson/codecs/
28
 */
29

    
30
#include "libavutil/intreadwrite.h"
31
#include "avformat.h"
32

    
33
#define VMD_HEADER_SIZE 0x0330
34
#define BYTES_PER_FRAME_RECORD 16
35

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

    
45
typedef struct VmdDemuxContext {
46
    int video_stream_index;
47
    int audio_stream_index;
48

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

    
55
    int sample_rate;
56
    int64_t audio_sample_counter;
57
    int skiphdr;
58

    
59
    unsigned char vmd_header[VMD_HEADER_SIZE];
60
} VmdDemuxContext;
61

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

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

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

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

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

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

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

    
155
    toc_offset = AV_RL32(&vmd->vmd_header[812]);
156
    vmd->frame_count = AV_RL16(&vmd->vmd_header[6]);
157
    vmd->frames_per_block = AV_RL16(&vmd->vmd_header[18]);
158
    url_fseek(pb, toc_offset, SEEK_SET);
159

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

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

    
185
        current_offset = AV_RL32(&raw_frame_table[6 * i + 2]);
186

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

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

    
225
    av_free(raw_frame_table);
226

    
227
    vmd->current_frame = 0;
228
    vmd->frame_count = total_frames;
229

    
230
    return 0;
231
}
232

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

    
241
    if (vmd->current_frame >= vmd->frame_count)
242
        return AVERROR(EIO);
243

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

    
248
    if (av_new_packet(pkt, frame->frame_size + BYTES_PER_FRAME_RECORD))
249
        return AVERROR(ENOMEM);
250
    pkt->pos= url_ftell(pb);
251
    memcpy(pkt->data, frame->frame_record, BYTES_PER_FRAME_RECORD);
252
    if(vmd->is_indeo3)
253
        ret = avio_read(pb, pkt->data, frame->frame_size);
254
    else
255
        ret = avio_read(pb, pkt->data + BYTES_PER_FRAME_RECORD,
256
            frame->frame_size);
257

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

    
269
    vmd->current_frame++;
270

    
271
    return ret;
272
}
273

    
274
static int vmd_read_close(AVFormatContext *s)
275
{
276
    VmdDemuxContext *vmd = s->priv_data;
277

    
278
    av_free(vmd->frame_table);
279

    
280
    return 0;
281
}
282

    
283
AVInputFormat ff_vmd_demuxer = {
284
    "vmd",
285
    NULL_IF_CONFIG_SMALL("Sierra VMD format"),
286
    sizeof(VmdDemuxContext),
287
    vmd_probe,
288
    vmd_read_header,
289
    vmd_read_packet,
290
    vmd_read_close,
291
};