1 | /*
|
---|
2 | * Sierra VMD Format Demuxer
|
---|
3 | * Copyright (c) 2004 The ffmpeg Project
|
---|
4 | *
|
---|
5 | * This library is free software; you can redistribute it and/or
|
---|
6 | * modify it under the terms of the GNU Lesser General Public
|
---|
7 | * License as published by the Free Software Foundation; either
|
---|
8 | * version 2 of the License, or (at your option) any later version.
|
---|
9 | *
|
---|
10 | * This library is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
13 | * Lesser General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU Lesser General Public
|
---|
16 | * License along with this library; if not, write to the Free Software
|
---|
17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
---|
18 | */
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * @file sierravmd.c
|
---|
22 | * Sierra VMD file demuxer
|
---|
23 | * by Vladimir "VAG" Gneushev (vagsoft at mail.ru)
|
---|
24 | * for more information on the Sierra VMD file format, visit:
|
---|
25 | * http://www.pcisys.net/~melanson/codecs/
|
---|
26 | */
|
---|
27 |
|
---|
28 | #include "avformat.h"
|
---|
29 |
|
---|
30 | #define VMD_HEADER_SIZE 0x0330
|
---|
31 | #define BYTES_PER_FRAME_RECORD 16
|
---|
32 |
|
---|
33 | typedef struct {
|
---|
34 | int stream_index;
|
---|
35 | offset_t frame_offset;
|
---|
36 | unsigned int frame_size;
|
---|
37 | int64_t pts;
|
---|
38 | int keyframe;
|
---|
39 | unsigned char frame_record[BYTES_PER_FRAME_RECORD];
|
---|
40 | } vmd_frame_t;
|
---|
41 |
|
---|
42 | typedef struct VmdDemuxContext {
|
---|
43 | int video_stream_index;
|
---|
44 | int audio_stream_index;
|
---|
45 |
|
---|
46 | unsigned int audio_type;
|
---|
47 | unsigned int audio_samplerate;
|
---|
48 | unsigned int audio_bits;
|
---|
49 | unsigned int audio_channels;
|
---|
50 |
|
---|
51 | unsigned int frame_count;
|
---|
52 | unsigned int frames_per_block;
|
---|
53 | vmd_frame_t *frame_table;
|
---|
54 | unsigned int current_frame;
|
---|
55 |
|
---|
56 | int sample_rate;
|
---|
57 | int64_t audio_sample_counter;
|
---|
58 | int audio_frame_divisor;
|
---|
59 | int audio_block_align;
|
---|
60 | int skiphdr;
|
---|
61 |
|
---|
62 | unsigned char vmd_header[VMD_HEADER_SIZE];
|
---|
63 | } VmdDemuxContext;
|
---|
64 |
|
---|
65 | static int vmd_probe(AVProbeData *p)
|
---|
66 | {
|
---|
67 | if (p->buf_size < 2)
|
---|
68 | return 0;
|
---|
69 |
|
---|
70 | /* check if the first 2 bytes of the file contain the appropriate size
|
---|
71 | * of a VMD header chunk */
|
---|
72 | if (LE_16(&p->buf[0]) != VMD_HEADER_SIZE - 2)
|
---|
73 | return 0;
|
---|
74 |
|
---|
75 | /* only return half certainty since this check is a bit sketchy */
|
---|
76 | return AVPROBE_SCORE_MAX / 2;
|
---|
77 | }
|
---|
78 |
|
---|
79 | /* This is a support function to determine the duration, in sample
|
---|
80 | * frames, of a particular audio chunk, taking into account silent
|
---|
81 | * encodings. */
|
---|
82 | static int vmd_calculate_audio_duration(unsigned char *audio_chunk,
|
---|
83 | int audio_chunk_size, int block_align)
|
---|
84 | {
|
---|
85 | unsigned char *p = audio_chunk + 16;
|
---|
86 | unsigned char *p_end = audio_chunk + audio_chunk_size;
|
---|
87 | int total_samples = 0;
|
---|
88 | unsigned int sound_flags;
|
---|
89 |
|
---|
90 | if (audio_chunk_size < 16)
|
---|
91 | return 0;
|
---|
92 | if (audio_chunk_size == block_align + 16)
|
---|
93 | return block_align;
|
---|
94 | if (audio_chunk_size == block_align + 17)
|
---|
95 | return block_align;
|
---|
96 |
|
---|
97 | sound_flags = LE_32(p);
|
---|
98 | p += 4;
|
---|
99 | while (p < p_end) {
|
---|
100 | total_samples += block_align;
|
---|
101 | if ((sound_flags & 0x01) == 0)
|
---|
102 | p += block_align;
|
---|
103 | sound_flags >>= 1;
|
---|
104 | }
|
---|
105 | av_log(NULL,0,"Got %i samples for size %i map %08X\n", total_samples, audio_chunk_size, LE_32(audio_chunk));
|
---|
106 |
|
---|
107 | return total_samples;
|
---|
108 | }
|
---|
109 |
|
---|
110 | static int vmd_read_header(AVFormatContext *s,
|
---|
111 | AVFormatParameters *ap)
|
---|
112 | {
|
---|
113 | VmdDemuxContext *vmd = (VmdDemuxContext *)s->priv_data;
|
---|
114 | ByteIOContext *pb = &s->pb;
|
---|
115 | AVStream *st;
|
---|
116 | unsigned int toc_offset;
|
---|
117 | unsigned char *raw_frame_table;
|
---|
118 | int raw_frame_table_size;
|
---|
119 | offset_t current_offset;
|
---|
120 | int i, j;
|
---|
121 | unsigned int total_frames;
|
---|
122 | int64_t video_pts_inc = 0;
|
---|
123 | int64_t current_video_pts = 0;
|
---|
124 | unsigned char chunk[BYTES_PER_FRAME_RECORD];
|
---|
125 | int lastframe = 0;
|
---|
126 |
|
---|
127 | /* fetch the main header, including the 2 header length bytes */
|
---|
128 | url_fseek(pb, 0, SEEK_SET);
|
---|
129 | if (get_buffer(pb, vmd->vmd_header, VMD_HEADER_SIZE) != VMD_HEADER_SIZE)
|
---|
130 | return AVERROR_IO;
|
---|
131 |
|
---|
132 | vmd->audio_sample_counter = 0;
|
---|
133 | vmd->audio_frame_divisor = 1;
|
---|
134 | vmd->audio_block_align = 1;
|
---|
135 |
|
---|
136 | /* start up the decoders */
|
---|
137 | st = av_new_stream(s, 0);
|
---|
138 | if (!st)
|
---|
139 | return AVERROR_NOMEM;
|
---|
140 | av_set_pts_info(st, 33, 1, 90000);
|
---|
141 | vmd->video_stream_index = st->index;
|
---|
142 | st->codec->codec_type = CODEC_TYPE_VIDEO;
|
---|
143 | st->codec->codec_id = CODEC_ID_VMDVIDEO;
|
---|
144 | st->codec->codec_tag = 0; /* no fourcc */
|
---|
145 | st->codec->width = LE_16(&vmd->vmd_header[12]);
|
---|
146 | st->codec->height = LE_16(&vmd->vmd_header[14]);
|
---|
147 | st->codec->time_base.num = 1;
|
---|
148 | st->codec->time_base.den = 10;
|
---|
149 | st->codec->extradata_size = VMD_HEADER_SIZE;
|
---|
150 | st->codec->extradata = av_mallocz(VMD_HEADER_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
|
---|
151 | memcpy(st->codec->extradata, vmd->vmd_header, VMD_HEADER_SIZE);
|
---|
152 |
|
---|
153 | /* if sample rate is 0, assume no audio */
|
---|
154 | vmd->sample_rate = LE_16(&vmd->vmd_header[804]);
|
---|
155 | if (vmd->sample_rate) {
|
---|
156 | st = av_new_stream(s, 0);
|
---|
157 | if (!st)
|
---|
158 | return AVERROR_NOMEM;
|
---|
159 | av_set_pts_info(st, 33, 1, 90000);
|
---|
160 | vmd->audio_stream_index = st->index;
|
---|
161 | st->codec->codec_type = CODEC_TYPE_AUDIO;
|
---|
162 | st->codec->codec_id = CODEC_ID_VMDAUDIO;
|
---|
163 | st->codec->codec_tag = 0; /* no fourcc */
|
---|
164 | st->codec->channels = vmd->audio_channels = (vmd->vmd_header[811] & 0x80) ? 2 : 1;
|
---|
165 | st->codec->sample_rate = vmd->sample_rate;
|
---|
166 | st->codec->block_align = vmd->audio_block_align =
|
---|
167 | LE_16(&vmd->vmd_header[806]);
|
---|
168 | if (st->codec->block_align & 0x8000) {
|
---|
169 | st->codec->bits_per_sample = 16;
|
---|
170 | st->codec->block_align = -(st->codec->block_align - 0x10000);
|
---|
171 | vmd->audio_block_align = -(vmd->audio_block_align - 0x10000);
|
---|
172 | } else {
|
---|
173 | st->codec->bits_per_sample = 8;
|
---|
174 | }
|
---|
175 | st->codec->bit_rate = st->codec->sample_rate *
|
---|
176 | st->codec->bits_per_sample * st->codec->channels;
|
---|
177 |
|
---|
178 | /* for calculating pts */
|
---|
179 | vmd->audio_frame_divisor = st->codec->channels;
|
---|
180 |
|
---|
181 | video_pts_inc = 90000;
|
---|
182 | video_pts_inc *= st->codec->block_align;
|
---|
183 | video_pts_inc /= st->codec->sample_rate;
|
---|
184 | video_pts_inc /= st->codec->channels;
|
---|
185 | } else {
|
---|
186 | /* if no audio, assume 10 frames/second */
|
---|
187 | video_pts_inc = 90000 / 10;
|
---|
188 | }
|
---|
189 |
|
---|
190 | toc_offset = LE_32(&vmd->vmd_header[812]);
|
---|
191 | vmd->frame_count = LE_16(&vmd->vmd_header[6]);
|
---|
192 | vmd->frames_per_block = LE_16(&vmd->vmd_header[18]);
|
---|
193 | url_fseek(pb, toc_offset, SEEK_SET);
|
---|
194 |
|
---|
195 | raw_frame_table = NULL;
|
---|
196 | vmd->frame_table = NULL;
|
---|
197 | raw_frame_table_size = vmd->frame_count * 6;
|
---|
198 | raw_frame_table = av_malloc(raw_frame_table_size);
|
---|
199 | if(vmd->frame_count * vmd->frames_per_block >= UINT_MAX / sizeof(vmd_frame_t)){
|
---|
200 | av_log(s, AV_LOG_ERROR, "vmd->frame_count * vmd->frames_per_block too large\n");
|
---|
201 | return -1;
|
---|
202 | }
|
---|
203 | vmd->frame_table = av_malloc(vmd->frame_count * vmd->frames_per_block * sizeof(vmd_frame_t));
|
---|
204 | if (!raw_frame_table || !vmd->frame_table) {
|
---|
205 | av_free(raw_frame_table);
|
---|
206 | av_free(vmd->frame_table);
|
---|
207 | return AVERROR_NOMEM;
|
---|
208 | }
|
---|
209 | if (get_buffer(pb, raw_frame_table, raw_frame_table_size) !=
|
---|
210 | raw_frame_table_size) {
|
---|
211 | av_free(raw_frame_table);
|
---|
212 | av_free(vmd->frame_table);
|
---|
213 | return AVERROR_IO;
|
---|
214 | }
|
---|
215 |
|
---|
216 | total_frames = 0;
|
---|
217 | for (i = 0; i < vmd->frame_count; i++) {
|
---|
218 |
|
---|
219 | current_offset = LE_32(&raw_frame_table[6 * i + 2]);
|
---|
220 |
|
---|
221 | /* handle each entry in index block */
|
---|
222 | for (j = 0; j < vmd->frames_per_block; j++) {
|
---|
223 | int type;
|
---|
224 | uint32_t size;
|
---|
225 |
|
---|
226 | get_buffer(pb, chunk, BYTES_PER_FRAME_RECORD);
|
---|
227 | type = chunk[0];
|
---|
228 | size = LE_32(&chunk[2]);
|
---|
229 | if(!size)
|
---|
230 | continue;
|
---|
231 | switch(type) {
|
---|
232 | case 1: /* Audio Chunk */
|
---|
233 | vmd->frame_table[total_frames].frame_offset = current_offset;
|
---|
234 | vmd->frame_table[total_frames].stream_index = vmd->audio_stream_index;
|
---|
235 | vmd->frame_table[total_frames].frame_size = size;
|
---|
236 | memcpy(vmd->frame_table[total_frames].frame_record, chunk, BYTES_PER_FRAME_RECORD);
|
---|
237 | total_frames++;
|
---|
238 | break;
|
---|
239 | case 2: /* Video Chunk */
|
---|
240 | vmd->frame_table[total_frames].frame_offset = current_offset;
|
---|
241 | vmd->frame_table[total_frames].frame_size = size;
|
---|
242 | vmd->frame_table[total_frames].stream_index = vmd->video_stream_index;
|
---|
243 | memcpy(vmd->frame_table[total_frames].frame_record, chunk, BYTES_PER_FRAME_RECORD);
|
---|
244 | vmd->frame_table[total_frames].pts = current_video_pts;
|
---|
245 | if (lastframe) {
|
---|
246 | vmd->frame_table[lastframe].pts = current_video_pts - video_pts_inc;
|
---|
247 | }
|
---|
248 | lastframe = total_frames;
|
---|
249 | total_frames++;
|
---|
250 | break;
|
---|
251 | }
|
---|
252 | current_offset += size;
|
---|
253 | }
|
---|
254 | current_video_pts += video_pts_inc;
|
---|
255 | }
|
---|
256 |
|
---|
257 | av_free(raw_frame_table);
|
---|
258 |
|
---|
259 | vmd->current_frame = 0;
|
---|
260 | vmd->frame_count = total_frames;
|
---|
261 |
|
---|
262 | return 0;
|
---|
263 | }
|
---|
264 |
|
---|
265 | static int vmd_read_packet(AVFormatContext *s,
|
---|
266 | AVPacket *pkt)
|
---|
267 | {
|
---|
268 | VmdDemuxContext *vmd = (VmdDemuxContext *)s->priv_data;
|
---|
269 | ByteIOContext *pb = &s->pb;
|
---|
270 | int ret = 0;
|
---|
271 | vmd_frame_t *frame;
|
---|
272 |
|
---|
273 | if (vmd->current_frame >= vmd->frame_count)
|
---|
274 | return AVERROR_IO;
|
---|
275 |
|
---|
276 | frame = &vmd->frame_table[vmd->current_frame];
|
---|
277 | /* position the stream (will probably be there already) */
|
---|
278 | url_fseek(pb, frame->frame_offset, SEEK_SET);
|
---|
279 |
|
---|
280 | if (av_new_packet(pkt, frame->frame_size + BYTES_PER_FRAME_RECORD))
|
---|
281 | return AVERROR_NOMEM;
|
---|
282 | pkt->pos= url_ftell(pb);
|
---|
283 | memcpy(pkt->data, frame->frame_record, BYTES_PER_FRAME_RECORD);
|
---|
284 | ret = get_buffer(pb, pkt->data + BYTES_PER_FRAME_RECORD,
|
---|
285 | frame->frame_size);
|
---|
286 |
|
---|
287 | if (ret != frame->frame_size) {
|
---|
288 | av_free_packet(pkt);
|
---|
289 | ret = AVERROR_IO;
|
---|
290 | }
|
---|
291 | pkt->stream_index = frame->stream_index;
|
---|
292 | if (frame->frame_record[0] == 0x02)
|
---|
293 | pkt->pts = frame->pts;
|
---|
294 | else {
|
---|
295 | pkt->pts = vmd->audio_sample_counter;
|
---|
296 | pkt->pts *= 90000;
|
---|
297 | pkt->pts /= vmd->sample_rate;
|
---|
298 | pkt->pts /= vmd->audio_channels;
|
---|
299 | vmd->audio_sample_counter += vmd_calculate_audio_duration(
|
---|
300 | pkt->data, pkt->size, vmd->audio_block_align);
|
---|
301 |
|
---|
302 | }
|
---|
303 | av_log(NULL, AV_LOG_INFO, " dispatching %s frame with %d bytes and pts %"PRId64" (%0.1f sec)\n",
|
---|
304 | (frame->frame_record[0] == 0x02) ? "video" : "audio",
|
---|
305 | frame->frame_size + BYTES_PER_FRAME_RECORD,
|
---|
306 | pkt->pts, (float)(pkt->pts / 90000.0));
|
---|
307 |
|
---|
308 | vmd->current_frame++;
|
---|
309 |
|
---|
310 | return ret;
|
---|
311 | }
|
---|
312 |
|
---|
313 | static int vmd_read_close(AVFormatContext *s)
|
---|
314 | {
|
---|
315 | VmdDemuxContext *vmd = (VmdDemuxContext *)s->priv_data;
|
---|
316 |
|
---|
317 | av_free(vmd->frame_table);
|
---|
318 |
|
---|
319 | return 0;
|
---|
320 | }
|
---|
321 |
|
---|
322 | static AVInputFormat vmd_demuxer = {
|
---|
323 | "vmd",
|
---|
324 | "Sierra VMD format",
|
---|
325 | sizeof(VmdDemuxContext),
|
---|
326 | vmd_probe,
|
---|
327 | vmd_read_header,
|
---|
328 | vmd_read_packet,
|
---|
329 | vmd_read_close,
|
---|
330 | };
|
---|
331 |
|
---|
332 | int vmd_init(void)
|
---|
333 | {
|
---|
334 | av_register_input_format(&vmd_demuxer);
|
---|
335 | return 0;
|
---|
336 | }
|
---|