Statistics
| Branch: | Revision:

ffmpeg / libavformat / sierravmd.c @ 470bce2b

History | View | Annotate | Download (10.9 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 bc5c918e Diego Biurrun
  int64_t frame_offset;
38 a7eb3c8d Mike Melanson
  unsigned int frame_size;
39
  int64_t pts;
40
  int keyframe;
41
  unsigned char frame_record[BYTES_PER_FRAME_RECORD];
42 02fb2546 Mike Melanson
} vmd_frame;
43 a7eb3c8d Mike Melanson
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 02fb2546 Mike Melanson
    vmd_frame *frame_table;
51 a7eb3c8d Mike Melanson
    unsigned int current_frame;
52 10f35ebd Kostya Shishkov
    int is_indeo3;
53 a7eb3c8d Mike Melanson
54 23fe14bb Mike Melanson
    int sample_rate;
55
    int64_t audio_sample_counter;
56 d82da3a8 Kostya Shishkov
    int skiphdr;
57 23fe14bb Mike Melanson
58 a7eb3c8d Mike Melanson
    unsigned char vmd_header[VMD_HEADER_SIZE];
59
} VmdDemuxContext;
60
61
static int vmd_probe(AVProbeData *p)
62
{
63
    /* check if the first 2 bytes of the file contain the appropriate size
64
     * of a VMD header chunk */
65 fead30d4 Alex Beregszaszi
    if (AV_RL16(&p->buf[0]) != VMD_HEADER_SIZE - 2)
66 a7eb3c8d Mike Melanson
        return 0;
67
68
    /* only return half certainty since this check is a bit sketchy */
69
    return AVPROBE_SCORE_MAX / 2;
70
}
71
72
static int vmd_read_header(AVFormatContext *s,
73
                           AVFormatParameters *ap)
74
{
75 e4141433 Nicholas Tung
    VmdDemuxContext *vmd = s->priv_data;
76 899681cd Björn Axelsson
    ByteIOContext *pb = s->pb;
77 cd41a73f Reimar Döffinger
    AVStream *st = NULL, *vst;
78 a7eb3c8d Mike Melanson
    unsigned int toc_offset;
79
    unsigned char *raw_frame_table;
80
    int raw_frame_table_size;
81 bc5c918e Diego Biurrun
    int64_t current_offset;
82 d82da3a8 Kostya Shishkov
    int i, j;
83 a7eb3c8d Mike Melanson
    unsigned int total_frames;
84 ffa0a3d6 Kostya Shishkov
    int64_t pts_inc = 1;
85
    int64_t current_video_pts = 0, current_audio_pts = 0;
86 d82da3a8 Kostya Shishkov
    unsigned char chunk[BYTES_PER_FRAME_RECORD];
87 ffa0a3d6 Kostya Shishkov
    int num, den;
88 c8f9f9b9 Kostya Shishkov
    int sound_buffers;
89 a7eb3c8d Mike Melanson
90
    /* fetch the main header, including the 2 header length bytes */
91
    url_fseek(pb, 0, SEEK_SET);
92
    if (get_buffer(pb, vmd->vmd_header, VMD_HEADER_SIZE) != VMD_HEADER_SIZE)
93 6f3e0b21 Panagiotis Issaris
        return AVERROR(EIO);
94 a7eb3c8d Mike Melanson
95 10f35ebd Kostya Shishkov
    if(vmd->vmd_header[16] == 'i' && vmd->vmd_header[17] == 'v' && vmd->vmd_header[18] == '3')
96
        vmd->is_indeo3 = 1;
97
    else
98 5b3658f7 Kostya Shishkov
        vmd->is_indeo3 = 0;
99 a7eb3c8d Mike Melanson
    /* start up the decoders */
100 ffa0a3d6 Kostya Shishkov
    vst = av_new_stream(s, 0);
101
    if (!vst)
102 769e10f0 Panagiotis Issaris
        return AVERROR(ENOMEM);
103 ffa0a3d6 Kostya Shishkov
    av_set_pts_info(vst, 33, 1, 10);
104
    vmd->video_stream_index = vst->index;
105
    vst->codec->codec_type = CODEC_TYPE_VIDEO;
106 10f35ebd Kostya Shishkov
    vst->codec->codec_id = vmd->is_indeo3 ? CODEC_ID_INDEO3 : CODEC_ID_VMDVIDEO;
107 ffa0a3d6 Kostya Shishkov
    vst->codec->codec_tag = 0;  /* no fourcc */
108 fead30d4 Alex Beregszaszi
    vst->codec->width = AV_RL16(&vmd->vmd_header[12]);
109
    vst->codec->height = AV_RL16(&vmd->vmd_header[14]);
110 10f35ebd Kostya Shishkov
    if(vmd->is_indeo3 && vst->codec->width > 320){
111
        vst->codec->width >>= 1;
112
        vst->codec->height >>= 1;
113
    }
114 ffa0a3d6 Kostya Shishkov
    vst->codec->extradata_size = VMD_HEADER_SIZE;
115
    vst->codec->extradata = av_mallocz(VMD_HEADER_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
116
    memcpy(vst->codec->extradata, vmd->vmd_header, VMD_HEADER_SIZE);
117 a7eb3c8d Mike Melanson
118
    /* if sample rate is 0, assume no audio */
119 fead30d4 Alex Beregszaszi
    vmd->sample_rate = AV_RL16(&vmd->vmd_header[804]);
120 23fe14bb Mike Melanson
    if (vmd->sample_rate) {
121 a7eb3c8d Mike Melanson
        st = av_new_stream(s, 0);
122
        if (!st)
123 769e10f0 Panagiotis Issaris
            return AVERROR(ENOMEM);
124 a7eb3c8d Mike Melanson
        vmd->audio_stream_index = st->index;
125 01f4895c Michael Niedermayer
        st->codec->codec_type = CODEC_TYPE_AUDIO;
126
        st->codec->codec_id = CODEC_ID_VMDAUDIO;
127 d82da3a8 Kostya Shishkov
        st->codec->codec_tag = 0;  /* no fourcc */
128 ffa0a3d6 Kostya Shishkov
        st->codec->channels = (vmd->vmd_header[811] & 0x80) ? 2 : 1;
129 01f4895c Michael Niedermayer
        st->codec->sample_rate = vmd->sample_rate;
130 fead30d4 Alex Beregszaszi
        st->codec->block_align = AV_RL16(&vmd->vmd_header[806]);
131 01f4895c Michael Niedermayer
        if (st->codec->block_align & 0x8000) {
132 dd1c8f3e Luca Abeni
            st->codec->bits_per_coded_sample = 16;
133 01f4895c Michael Niedermayer
            st->codec->block_align = -(st->codec->block_align - 0x10000);
134 d82da3a8 Kostya Shishkov
        } else {
135 dd1c8f3e Luca Abeni
            st->codec->bits_per_coded_sample = 8;
136 d82da3a8 Kostya Shishkov
        }
137 115329f1 Diego Biurrun
        st->codec->bit_rate = st->codec->sample_rate *
138 dd1c8f3e Luca Abeni
            st->codec->bits_per_coded_sample * st->codec->channels;
139 23fe14bb Mike Melanson
140 ffa0a3d6 Kostya Shishkov
        /* calculate pts */
141
        num = st->codec->block_align;
142
        den = st->codec->sample_rate * st->codec->channels;
143
        av_reduce(&den, &num, den, num, (1UL<<31)-1);
144
        av_set_pts_info(vst, 33, num, den);
145
        av_set_pts_info(st, 33, num, den);
146
        pts_inc = num;
147 a7eb3c8d Mike Melanson
    }
148
149 fead30d4 Alex Beregszaszi
    toc_offset = AV_RL32(&vmd->vmd_header[812]);
150
    vmd->frame_count = AV_RL16(&vmd->vmd_header[6]);
151
    vmd->frames_per_block = AV_RL16(&vmd->vmd_header[18]);
152 d82da3a8 Kostya Shishkov
    url_fseek(pb, toc_offset, SEEK_SET);
153 a7eb3c8d Mike Melanson
154
    raw_frame_table = NULL;
155
    vmd->frame_table = NULL;
156 fead30d4 Alex Beregszaszi
    sound_buffers = AV_RL16(&vmd->vmd_header[808]);
157 d82da3a8 Kostya Shishkov
    raw_frame_table_size = vmd->frame_count * 6;
158 02fb2546 Mike Melanson
    if(vmd->frame_count * vmd->frames_per_block  >= UINT_MAX / sizeof(vmd_frame)){
159 a443a253 Michael Niedermayer
        av_log(s, AV_LOG_ERROR, "vmd->frame_count * vmd->frames_per_block too large\n");
160
        return -1;
161
    }
162 5a96cd64 Michael Niedermayer
    raw_frame_table = av_malloc(raw_frame_table_size);
163 02fb2546 Mike Melanson
    vmd->frame_table = av_malloc((vmd->frame_count * vmd->frames_per_block + sound_buffers) * sizeof(vmd_frame));
164 a7eb3c8d Mike Melanson
    if (!raw_frame_table || !vmd->frame_table) {
165
        av_free(raw_frame_table);
166
        av_free(vmd->frame_table);
167 769e10f0 Panagiotis Issaris
        return AVERROR(ENOMEM);
168 a7eb3c8d Mike Melanson
    }
169 115329f1 Diego Biurrun
    if (get_buffer(pb, raw_frame_table, raw_frame_table_size) !=
170 a7eb3c8d Mike Melanson
        raw_frame_table_size) {
171
        av_free(raw_frame_table);
172
        av_free(vmd->frame_table);
173 6f3e0b21 Panagiotis Issaris
        return AVERROR(EIO);
174 a7eb3c8d Mike Melanson
    }
175
176 d82da3a8 Kostya Shishkov
    total_frames = 0;
177
    for (i = 0; i < vmd->frame_count; i++) {
178
179 fead30d4 Alex Beregszaszi
        current_offset = AV_RL32(&raw_frame_table[6 * i + 2]);
180 d82da3a8 Kostya Shishkov
181
        /* handle each entry in index block */
182
        for (j = 0; j < vmd->frames_per_block; j++) {
183
            int type;
184
            uint32_t size;
185
186
            get_buffer(pb, chunk, BYTES_PER_FRAME_RECORD);
187
            type = chunk[0];
188 fead30d4 Alex Beregszaszi
            size = AV_RL32(&chunk[2]);
189 d82da3a8 Kostya Shishkov
            if(!size)
190
                continue;
191
            switch(type) {
192
            case 1: /* Audio Chunk */
193 cd41a73f Reimar Döffinger
                if (!st) break;
194 c8f9f9b9 Kostya Shishkov
                /* first audio chunk contains several audio buffers */
195
                if(current_audio_pts){
196 322af4be Kostya Shishkov
                    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 c8f9f9b9 Kostya Shishkov
                }else{
204
                    uint32_t flags;
205
                    int k;
206
                    int noff;
207
                    int64_t pos;
208
209
                    pos = url_ftell(pb);
210
                    url_fseek(pb, current_offset, SEEK_SET);
211
                    flags = get_le32(pb);
212
                    noff = 4;
213
                    url_fseek(pb, pos, SEEK_SET);
214
                    av_log(s, AV_LOG_DEBUG, "Sound mapping = %08X (%i bufs)\n", flags, sound_buffers);
215
                    for(k = 0; k < sound_buffers - 1; k++){
216
                        if(flags & 1) { /* silent block */
217
                            vmd->frame_table[total_frames].frame_size = 0;
218
                        }else{
219
                            vmd->frame_table[total_frames].frame_size = st->codec->block_align + (st->codec->block_align & 1);
220
                        }
221
                        noff += vmd->frame_table[total_frames].frame_size;
222
                        vmd->frame_table[total_frames].frame_offset = current_offset + noff;
223
                        vmd->frame_table[total_frames].stream_index = vmd->audio_stream_index;
224
                        memcpy(vmd->frame_table[total_frames].frame_record, chunk, BYTES_PER_FRAME_RECORD);
225
                        vmd->frame_table[total_frames].pts = current_audio_pts;
226
                        total_frames++;
227
                        current_audio_pts += pts_inc;
228
                        flags >>= 1;
229
                    }
230
                }
231 d82da3a8 Kostya Shishkov
                break;
232
            case 2: /* Video Chunk */
233
                vmd->frame_table[total_frames].frame_offset = current_offset;
234
                vmd->frame_table[total_frames].stream_index = vmd->video_stream_index;
235 ffa0a3d6 Kostya Shishkov
                vmd->frame_table[total_frames].frame_size = size;
236 d82da3a8 Kostya Shishkov
                memcpy(vmd->frame_table[total_frames].frame_record, chunk, BYTES_PER_FRAME_RECORD);
237
                vmd->frame_table[total_frames].pts = current_video_pts;
238
                total_frames++;
239
                break;
240
            }
241
            current_offset += size;
242 a7eb3c8d Mike Melanson
        }
243 ffa0a3d6 Kostya Shishkov
        current_video_pts += pts_inc;
244 a7eb3c8d Mike Melanson
    }
245
246
    av_free(raw_frame_table);
247
248
    vmd->current_frame = 0;
249 d82da3a8 Kostya Shishkov
    vmd->frame_count = total_frames;
250 a7eb3c8d Mike Melanson
251
    return 0;
252
}
253
254
static int vmd_read_packet(AVFormatContext *s,
255
                           AVPacket *pkt)
256
{
257 e4141433 Nicholas Tung
    VmdDemuxContext *vmd = s->priv_data;
258 899681cd Björn Axelsson
    ByteIOContext *pb = s->pb;
259 a7eb3c8d Mike Melanson
    int ret = 0;
260 02fb2546 Mike Melanson
    vmd_frame *frame;
261 a7eb3c8d Mike Melanson
262
    if (vmd->current_frame >= vmd->frame_count)
263 6f3e0b21 Panagiotis Issaris
        return AVERROR(EIO);
264 a7eb3c8d Mike Melanson
265
    frame = &vmd->frame_table[vmd->current_frame];
266
    /* position the stream (will probably be there already) */
267
    url_fseek(pb, frame->frame_offset, SEEK_SET);
268
269
    if (av_new_packet(pkt, frame->frame_size + BYTES_PER_FRAME_RECORD))
270 769e10f0 Panagiotis Issaris
        return AVERROR(ENOMEM);
271 2692067a Michael Niedermayer
    pkt->pos= url_ftell(pb);
272 a7eb3c8d Mike Melanson
    memcpy(pkt->data, frame->frame_record, BYTES_PER_FRAME_RECORD);
273 10f35ebd Kostya Shishkov
    if(vmd->is_indeo3)
274
        ret = get_buffer(pb, pkt->data, frame->frame_size);
275
    else
276
        ret = get_buffer(pb, pkt->data + BYTES_PER_FRAME_RECORD,
277
            frame->frame_size);
278 a7eb3c8d Mike Melanson
279 23fe14bb Mike Melanson
    if (ret != frame->frame_size) {
280
        av_free_packet(pkt);
281 6f3e0b21 Panagiotis Issaris
        ret = AVERROR(EIO);
282 23fe14bb Mike Melanson
    }
283 a7eb3c8d Mike Melanson
    pkt->stream_index = frame->stream_index;
284 ffa0a3d6 Kostya Shishkov
    pkt->pts = frame->pts;
285 2529024b Michael Niedermayer
    av_log(NULL, AV_LOG_DEBUG, " dispatching %s frame with %d bytes and pts %"PRId64"\n",
286 e344c1ea Steve L'Homme
            (frame->frame_record[0] == 0x02) ? "video" : "audio",
287
            frame->frame_size + BYTES_PER_FRAME_RECORD,
288 ffa0a3d6 Kostya Shishkov
            pkt->pts);
289 a7eb3c8d Mike Melanson
290
    vmd->current_frame++;
291
292
    return ret;
293
}
294
295
static int vmd_read_close(AVFormatContext *s)
296
{
297 e4141433 Nicholas Tung
    VmdDemuxContext *vmd = s->priv_data;
298 a7eb3c8d Mike Melanson
299
    av_free(vmd->frame_table);
300
301
    return 0;
302
}
303
304 ff70e601 Måns Rullgård
AVInputFormat vmd_demuxer = {
305 a7eb3c8d Mike Melanson
    "vmd",
306 bde15e74 Stefano Sabatini
    NULL_IF_CONFIG_SMALL("Sierra VMD format"),
307 a7eb3c8d Mike Melanson
    sizeof(VmdDemuxContext),
308
    vmd_probe,
309
    vmd_read_header,
310
    vmd_read_packet,
311
    vmd_read_close,
312
};