Statistics
| Branch: | Revision:

ffmpeg / libavformat / sierravmd.c @ 1afdcdf1

History | View | Annotate | Download (10.4 KB)

1 a7eb3c8d Mike Melanson
/*
2
 * Sierra VMD Format Demuxer
3
 * Copyright (c) 2004 The ffmpeg Project
4
 *
5 b78e7197 Diego Biurrun
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8 a7eb3c8d Mike Melanson
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10 b78e7197 Diego Biurrun
 * version 2.1 of the License, or (at your option) any later version.
11 a7eb3c8d Mike Melanson
 *
12 b78e7197 Diego Biurrun
 * FFmpeg is distributed in the hope that it will be useful,
13 a7eb3c8d Mike Melanson
 * 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 b78e7197 Diego Biurrun
 * License along with FFmpeg; if not, write to the Free Software
19 5509bffa Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 a7eb3c8d Mike Melanson
 */
21
22
/**
23
 * @file sierravmd.c
24
 * Sierra VMD file demuxer
25
 * by Vladimir "VAG" Gneushev (vagsoft at mail.ru)
26 23fe14bb Mike Melanson
 * for more information on the Sierra VMD file format, visit:
27
 *   http://www.pcisys.net/~melanson/codecs/
28 a7eb3c8d Mike Melanson
 */
29
30
#include "avformat.h"
31
32
#define VMD_HEADER_SIZE 0x0330
33
#define BYTES_PER_FRAME_RECORD 16
34
35
typedef struct {
36
  int stream_index;
37
  offset_t frame_offset;
38
  unsigned int frame_size;
39
  int64_t pts;
40
  int keyframe;
41
  unsigned char frame_record[BYTES_PER_FRAME_RECORD];
42
} vmd_frame_t;
43
44
typedef struct VmdDemuxContext {
45
    int video_stream_index;
46
    int audio_stream_index;
47
48
    unsigned int frame_count;
49 d82da3a8 Kostya Shishkov
    unsigned int frames_per_block;
50 a7eb3c8d Mike Melanson
    vmd_frame_t *frame_table;
51
    unsigned int current_frame;
52
53 23fe14bb Mike Melanson
    int sample_rate;
54
    int64_t audio_sample_counter;
55 d82da3a8 Kostya Shishkov
    int skiphdr;
56 23fe14bb Mike Melanson
57 a7eb3c8d Mike Melanson
    unsigned char vmd_header[VMD_HEADER_SIZE];
58
} VmdDemuxContext;
59
60
static int vmd_probe(AVProbeData *p)
61
{
62
    if (p->buf_size < 2)
63
        return 0;
64
65
    /* check if the first 2 bytes of the file contain the appropriate size
66
     * of a VMD header chunk */
67 fead30d4 Alex Beregszaszi
    if (AV_RL16(&p->buf[0]) != VMD_HEADER_SIZE - 2)
68 a7eb3c8d Mike Melanson
        return 0;
69
70
    /* only return half certainty since this check is a bit sketchy */
71
    return AVPROBE_SCORE_MAX / 2;
72
}
73
74
static int vmd_read_header(AVFormatContext *s,
75
                           AVFormatParameters *ap)
76
{
77
    VmdDemuxContext *vmd = (VmdDemuxContext *)s->priv_data;
78
    ByteIOContext *pb = &s->pb;
79 ffa0a3d6 Kostya Shishkov
    AVStream *st, *vst;
80 a7eb3c8d Mike Melanson
    unsigned int toc_offset;
81
    unsigned char *raw_frame_table;
82
    int raw_frame_table_size;
83
    offset_t current_offset;
84 d82da3a8 Kostya Shishkov
    int i, j;
85 a7eb3c8d Mike Melanson
    unsigned int total_frames;
86 ffa0a3d6 Kostya Shishkov
    int64_t pts_inc = 1;
87
    int64_t current_video_pts = 0, current_audio_pts = 0;
88 d82da3a8 Kostya Shishkov
    unsigned char chunk[BYTES_PER_FRAME_RECORD];
89 ffa0a3d6 Kostya Shishkov
    int num, den;
90 c8f9f9b9 Kostya Shishkov
    int sound_buffers;
91 a7eb3c8d Mike Melanson
92
    /* fetch the main header, including the 2 header length bytes */
93
    url_fseek(pb, 0, SEEK_SET);
94
    if (get_buffer(pb, vmd->vmd_header, VMD_HEADER_SIZE) != VMD_HEADER_SIZE)
95 0bd586c5 Mike Melanson
        return AVERROR_IO;
96 a7eb3c8d Mike Melanson
97
    /* start up the decoders */
98 ffa0a3d6 Kostya Shishkov
    vst = av_new_stream(s, 0);
99
    if (!vst)
100 a7eb3c8d Mike Melanson
        return AVERROR_NOMEM;
101 ffa0a3d6 Kostya Shishkov
    av_set_pts_info(vst, 33, 1, 10);
102
    vmd->video_stream_index = vst->index;
103
    vst->codec->codec_type = CODEC_TYPE_VIDEO;
104
    vst->codec->codec_id = CODEC_ID_VMDVIDEO;
105
    vst->codec->codec_tag = 0;  /* no fourcc */
106 fead30d4 Alex Beregszaszi
    vst->codec->width = AV_RL16(&vmd->vmd_header[12]);
107
    vst->codec->height = AV_RL16(&vmd->vmd_header[14]);
108 ffa0a3d6 Kostya Shishkov
    vst->codec->extradata_size = VMD_HEADER_SIZE;
109
    vst->codec->extradata = av_mallocz(VMD_HEADER_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
110
    memcpy(vst->codec->extradata, vmd->vmd_header, VMD_HEADER_SIZE);
111 a7eb3c8d Mike Melanson
112
    /* if sample rate is 0, assume no audio */
113 fead30d4 Alex Beregszaszi
    vmd->sample_rate = AV_RL16(&vmd->vmd_header[804]);
114 23fe14bb Mike Melanson
    if (vmd->sample_rate) {
115 a7eb3c8d Mike Melanson
        st = av_new_stream(s, 0);
116
        if (!st)
117
            return AVERROR_NOMEM;
118
        vmd->audio_stream_index = st->index;
119 01f4895c Michael Niedermayer
        st->codec->codec_type = CODEC_TYPE_AUDIO;
120
        st->codec->codec_id = CODEC_ID_VMDAUDIO;
121 d82da3a8 Kostya Shishkov
        st->codec->codec_tag = 0;  /* no fourcc */
122 ffa0a3d6 Kostya Shishkov
        st->codec->channels = (vmd->vmd_header[811] & 0x80) ? 2 : 1;
123 01f4895c Michael Niedermayer
        st->codec->sample_rate = vmd->sample_rate;
124 fead30d4 Alex Beregszaszi
        st->codec->block_align = AV_RL16(&vmd->vmd_header[806]);
125 01f4895c Michael Niedermayer
        if (st->codec->block_align & 0x8000) {
126
            st->codec->bits_per_sample = 16;
127
            st->codec->block_align = -(st->codec->block_align - 0x10000);
128 d82da3a8 Kostya Shishkov
        } else {
129
            st->codec->bits_per_sample = 8;
130
        }
131 115329f1 Diego Biurrun
        st->codec->bit_rate = st->codec->sample_rate *
132 01f4895c Michael Niedermayer
            st->codec->bits_per_sample * st->codec->channels;
133 23fe14bb Mike Melanson
134 ffa0a3d6 Kostya Shishkov
        /* calculate pts */
135
        num = st->codec->block_align;
136
        den = st->codec->sample_rate * st->codec->channels;
137
        av_reduce(&den, &num, den, num, (1UL<<31)-1);
138
        av_set_pts_info(vst, 33, num, den);
139
        av_set_pts_info(st, 33, num, den);
140
        pts_inc = num;
141 a7eb3c8d Mike Melanson
    }
142
143 fead30d4 Alex Beregszaszi
    toc_offset = AV_RL32(&vmd->vmd_header[812]);
144
    vmd->frame_count = AV_RL16(&vmd->vmd_header[6]);
145
    vmd->frames_per_block = AV_RL16(&vmd->vmd_header[18]);
146 d82da3a8 Kostya Shishkov
    url_fseek(pb, toc_offset, SEEK_SET);
147 a7eb3c8d Mike Melanson
148
    raw_frame_table = NULL;
149
    vmd->frame_table = NULL;
150 fead30d4 Alex Beregszaszi
    sound_buffers = AV_RL16(&vmd->vmd_header[808]);
151 d82da3a8 Kostya Shishkov
    raw_frame_table_size = vmd->frame_count * 6;
152 a7eb3c8d Mike Melanson
    raw_frame_table = av_malloc(raw_frame_table_size);
153 a443a253 Michael Niedermayer
    if(vmd->frame_count * vmd->frames_per_block  >= UINT_MAX / sizeof(vmd_frame_t)){
154
        av_log(s, AV_LOG_ERROR, "vmd->frame_count * vmd->frames_per_block too large\n");
155
        return -1;
156
    }
157 c8f9f9b9 Kostya Shishkov
    vmd->frame_table = av_malloc((vmd->frame_count * vmd->frames_per_block + sound_buffers) * sizeof(vmd_frame_t));
158 a7eb3c8d Mike Melanson
    if (!raw_frame_table || !vmd->frame_table) {
159
        av_free(raw_frame_table);
160
        av_free(vmd->frame_table);
161
        return AVERROR_NOMEM;
162
    }
163 115329f1 Diego Biurrun
    if (get_buffer(pb, raw_frame_table, raw_frame_table_size) !=
164 a7eb3c8d Mike Melanson
        raw_frame_table_size) {
165
        av_free(raw_frame_table);
166
        av_free(vmd->frame_table);
167 0bd586c5 Mike Melanson
        return AVERROR_IO;
168 a7eb3c8d Mike Melanson
    }
169
170 d82da3a8 Kostya Shishkov
    total_frames = 0;
171
    for (i = 0; i < vmd->frame_count; i++) {
172
173 fead30d4 Alex Beregszaszi
        current_offset = AV_RL32(&raw_frame_table[6 * i + 2]);
174 d82da3a8 Kostya Shishkov
175
        /* handle each entry in index block */
176
        for (j = 0; j < vmd->frames_per_block; j++) {
177
            int type;
178
            uint32_t size;
179
180
            get_buffer(pb, chunk, BYTES_PER_FRAME_RECORD);
181
            type = chunk[0];
182 fead30d4 Alex Beregszaszi
            size = AV_RL32(&chunk[2]);
183 d82da3a8 Kostya Shishkov
            if(!size)
184
                continue;
185
            switch(type) {
186
            case 1: /* Audio Chunk */
187 c8f9f9b9 Kostya Shishkov
                /* first audio chunk contains several audio buffers */
188
                if(current_audio_pts){
189 322af4be Kostya Shishkov
                    vmd->frame_table[total_frames].frame_offset = current_offset;
190
                    vmd->frame_table[total_frames].stream_index = vmd->audio_stream_index;
191
                    vmd->frame_table[total_frames].frame_size = size;
192
                    memcpy(vmd->frame_table[total_frames].frame_record, chunk, BYTES_PER_FRAME_RECORD);
193
                    vmd->frame_table[total_frames].pts = current_audio_pts;
194
                    total_frames++;
195
                    current_audio_pts += pts_inc;
196 c8f9f9b9 Kostya Shishkov
                }else{
197
                    uint32_t flags;
198
                    int k;
199
                    int noff;
200
                    int64_t pos;
201
202
                    pos = url_ftell(pb);
203
                    url_fseek(pb, current_offset, SEEK_SET);
204
                    flags = get_le32(pb);
205
                    noff = 4;
206
                    url_fseek(pb, pos, SEEK_SET);
207
                    av_log(s, AV_LOG_DEBUG, "Sound mapping = %08X (%i bufs)\n", flags, sound_buffers);
208
                    for(k = 0; k < sound_buffers - 1; k++){
209
                        if(flags & 1) { /* silent block */
210
                            vmd->frame_table[total_frames].frame_size = 0;
211
                        }else{
212
                            vmd->frame_table[total_frames].frame_size = st->codec->block_align + (st->codec->block_align & 1);
213
                        }
214
                        noff += vmd->frame_table[total_frames].frame_size;
215
                        vmd->frame_table[total_frames].frame_offset = current_offset + noff;
216
                        vmd->frame_table[total_frames].stream_index = vmd->audio_stream_index;
217
                        memcpy(vmd->frame_table[total_frames].frame_record, chunk, BYTES_PER_FRAME_RECORD);
218
                        vmd->frame_table[total_frames].pts = current_audio_pts;
219
                        total_frames++;
220
                        current_audio_pts += pts_inc;
221
                        flags >>= 1;
222
                    }
223
                }
224 d82da3a8 Kostya Shishkov
                break;
225
            case 2: /* Video Chunk */
226
                vmd->frame_table[total_frames].frame_offset = current_offset;
227
                vmd->frame_table[total_frames].stream_index = vmd->video_stream_index;
228 ffa0a3d6 Kostya Shishkov
                vmd->frame_table[total_frames].frame_size = size;
229 d82da3a8 Kostya Shishkov
                memcpy(vmd->frame_table[total_frames].frame_record, chunk, BYTES_PER_FRAME_RECORD);
230
                vmd->frame_table[total_frames].pts = current_video_pts;
231
                total_frames++;
232
                break;
233
            }
234
            current_offset += size;
235 a7eb3c8d Mike Melanson
        }
236 ffa0a3d6 Kostya Shishkov
        current_video_pts += pts_inc;
237 a7eb3c8d Mike Melanson
    }
238
239
    av_free(raw_frame_table);
240
241
    vmd->current_frame = 0;
242 d82da3a8 Kostya Shishkov
    vmd->frame_count = total_frames;
243 a7eb3c8d Mike Melanson
244
    return 0;
245
}
246
247
static int vmd_read_packet(AVFormatContext *s,
248
                           AVPacket *pkt)
249
{
250
    VmdDemuxContext *vmd = (VmdDemuxContext *)s->priv_data;
251
    ByteIOContext *pb = &s->pb;
252
    int ret = 0;
253
    vmd_frame_t *frame;
254
255
    if (vmd->current_frame >= vmd->frame_count)
256 0bd586c5 Mike Melanson
        return AVERROR_IO;
257 a7eb3c8d Mike Melanson
258
    frame = &vmd->frame_table[vmd->current_frame];
259
    /* position the stream (will probably be there already) */
260
    url_fseek(pb, frame->frame_offset, SEEK_SET);
261
262
    if (av_new_packet(pkt, frame->frame_size + BYTES_PER_FRAME_RECORD))
263
        return AVERROR_NOMEM;
264 2692067a Michael Niedermayer
    pkt->pos= url_ftell(pb);
265 a7eb3c8d Mike Melanson
    memcpy(pkt->data, frame->frame_record, BYTES_PER_FRAME_RECORD);
266 115329f1 Diego Biurrun
    ret = get_buffer(pb, pkt->data + BYTES_PER_FRAME_RECORD,
267 a7eb3c8d Mike Melanson
        frame->frame_size);
268
269 23fe14bb Mike Melanson
    if (ret != frame->frame_size) {
270
        av_free_packet(pkt);
271 0bd586c5 Mike Melanson
        ret = AVERROR_IO;
272 23fe14bb Mike Melanson
    }
273 a7eb3c8d Mike Melanson
    pkt->stream_index = frame->stream_index;
274 ffa0a3d6 Kostya Shishkov
    pkt->pts = frame->pts;
275
    av_log(NULL, AV_LOG_INFO, " dispatching %s frame with %d bytes and pts %"PRId64"\n",
276 e344c1ea Steve L'Homme
            (frame->frame_record[0] == 0x02) ? "video" : "audio",
277
            frame->frame_size + BYTES_PER_FRAME_RECORD,
278 ffa0a3d6 Kostya Shishkov
            pkt->pts);
279 a7eb3c8d Mike Melanson
280
    vmd->current_frame++;
281
282
    return ret;
283
}
284
285
static int vmd_read_close(AVFormatContext *s)
286
{
287
    VmdDemuxContext *vmd = (VmdDemuxContext *)s->priv_data;
288
289
    av_free(vmd->frame_table);
290
291
    return 0;
292
}
293
294 ff70e601 Måns Rullgård
AVInputFormat vmd_demuxer = {
295 a7eb3c8d Mike Melanson
    "vmd",
296
    "Sierra VMD format",
297
    sizeof(VmdDemuxContext),
298
    vmd_probe,
299
    vmd_read_header,
300
    vmd_read_packet,
301
    vmd_read_close,
302
};