Statistics
| Branch: | Revision:

ffmpeg / libavformat / sierravmd.c @ 7d2b199b

History | View | Annotate | Download (9.38 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 sierravmd.c
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
    /* check if the first 2 bytes of the file contain the appropriate size
65
     * of a VMD header chunk */
66
    if (AV_RL16(&p->buf[0]) != VMD_HEADER_SIZE - 2)
67
        return 0;
68

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

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

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

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

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

    
141
        /* calculate pts */
142
        num = st->codec->block_align;
143
        den = st->codec->sample_rate * st->codec->channels;
144
        av_reduce(&den, &num, den, num, (1UL<<31)-1);
145
        av_set_pts_info(vst, 33, num, den);
146
        av_set_pts_info(st, 33, num, den);
147
        pts_inc = num;
148
    }
149

    
150
    toc_offset = AV_RL32(&vmd->vmd_header[812]);
151
    vmd->frame_count = AV_RL16(&vmd->vmd_header[6]);
152
    vmd->frames_per_block = AV_RL16(&vmd->vmd_header[18]);
153
    url_fseek(pb, toc_offset, SEEK_SET);
154

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

    
177
    total_frames = 0;
178
    for (i = 0; i < vmd->frame_count; i++) {
179

    
180
        current_offset = AV_RL32(&raw_frame_table[6 * i + 2]);
181

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

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

    
218
    av_free(raw_frame_table);
219

    
220
    vmd->current_frame = 0;
221
    vmd->frame_count = total_frames;
222

    
223
    return 0;
224
}
225

    
226
static int vmd_read_packet(AVFormatContext *s,
227
                           AVPacket *pkt)
228
{
229
    VmdDemuxContext *vmd = s->priv_data;
230
    ByteIOContext *pb = s->pb;
231
    int ret = 0;
232
    vmd_frame *frame;
233

    
234
    if (vmd->current_frame >= vmd->frame_count)
235
        return AVERROR(EIO);
236

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

    
241
    if (av_new_packet(pkt, frame->frame_size + BYTES_PER_FRAME_RECORD))
242
        return AVERROR(ENOMEM);
243
    pkt->pos= url_ftell(pb);
244
    memcpy(pkt->data, frame->frame_record, BYTES_PER_FRAME_RECORD);
245
    if(vmd->is_indeo3)
246
        ret = get_buffer(pb, pkt->data, frame->frame_size);
247
    else
248
        ret = get_buffer(pb, pkt->data + BYTES_PER_FRAME_RECORD,
249
            frame->frame_size);
250

    
251
    if (ret != frame->frame_size) {
252
        av_free_packet(pkt);
253
        ret = AVERROR(EIO);
254
    }
255
    pkt->stream_index = frame->stream_index;
256
    pkt->pts = frame->pts;
257
    av_log(NULL, AV_LOG_DEBUG, " dispatching %s frame with %d bytes and pts %"PRId64"\n",
258
            (frame->frame_record[0] == 0x02) ? "video" : "audio",
259
            frame->frame_size + BYTES_PER_FRAME_RECORD,
260
            pkt->pts);
261

    
262
    vmd->current_frame++;
263

    
264
    return ret;
265
}
266

    
267
static int vmd_read_close(AVFormatContext *s)
268
{
269
    VmdDemuxContext *vmd = s->priv_data;
270

    
271
    av_free(vmd->frame_table);
272

    
273
    return 0;
274
}
275

    
276
AVInputFormat vmd_demuxer = {
277
    "vmd",
278
    NULL_IF_CONFIG_SMALL("Sierra VMD format"),
279
    sizeof(VmdDemuxContext),
280
    vmd_probe,
281
    vmd_read_header,
282
    vmd_read_packet,
283
    vmd_read_close,
284
};