Mercurial > libavformat.hg
annotate sierravmd.c @ 1052:66bd5e175690 libavformat
KMVC (used in Worms games) decoder. Works fine with samples from MPHQ
author | kostya |
---|---|
date | Fri, 07 Apr 2006 16:59:57 +0000 |
parents | 409b399440a3 |
children | 40e81416015d |
rev | line source |
---|---|
338 | 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 | |
896
edbe5c3717f9
Update licensing information: The FSF changed postal address.
diego
parents:
887
diff
changeset
|
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
338 | 18 */ |
19 | |
20 /** | |
21 * @file sierravmd.c | |
22 * Sierra VMD file demuxer | |
23 * by Vladimir "VAG" Gneushev (vagsoft at mail.ru) | |
387
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
24 * for more information on the Sierra VMD file format, visit: |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
25 * http://www.pcisys.net/~melanson/codecs/ |
338 | 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; | |
1004
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
52 unsigned int frames_per_block; |
338 | 53 vmd_frame_t *frame_table; |
54 unsigned int current_frame; | |
55 | |
387
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
56 int sample_rate; |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
57 int64_t audio_sample_counter; |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
58 int audio_frame_divisor; |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
59 int audio_block_align; |
1004
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
60 int skiphdr; |
387
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
61 |
338 | 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 | |
387
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
79 /* This is a support function to determine the duration, in sample |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
80 * frames, of a particular audio chunk, taking into account silent |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
81 * encodings. */ |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
82 static int vmd_calculate_audio_duration(unsigned char *audio_chunk, |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
83 int audio_chunk_size, int block_align) |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
84 { |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
85 unsigned char *p = audio_chunk + 16; |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
86 unsigned char *p_end = audio_chunk + audio_chunk_size; |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
87 int total_samples = 0; |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
88 unsigned int sound_flags; |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
89 |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
90 if (audio_chunk_size < 16) |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
91 return 0; |
1004
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
92 if (audio_chunk_size == block_align + 16) |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
93 return block_align; |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
94 if (audio_chunk_size == block_align + 17) |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
95 return block_align; |
387
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
96 |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
97 sound_flags = LE_32(p); |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
98 p += 4; |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
99 while (p < p_end) { |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
100 total_samples += block_align; |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
101 if ((sound_flags & 0x01) == 0) |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
102 p += block_align; |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
103 sound_flags >>= 1; |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
104 } |
1004
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
105 av_log(NULL,0,"Got %i samples for size %i map %08X\n", total_samples, audio_chunk_size, LE_32(audio_chunk)); |
387
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
106 |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
107 return total_samples; |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
108 } |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
109 |
338 | 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; | |
1004
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
120 int i, j; |
338 | 121 unsigned int total_frames; |
1004
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
122 int64_t video_pts_inc = 0; |
387
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
123 int64_t current_video_pts = 0; |
1004
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
124 unsigned char chunk[BYTES_PER_FRAME_RECORD]; |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
125 int lastframe = 0; |
338 | 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) | |
482 | 130 return AVERROR_IO; |
338 | 131 |
387
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
132 vmd->audio_sample_counter = 0; |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
133 vmd->audio_frame_divisor = 1; |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
134 vmd->audio_block_align = 1; |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
135 |
338 | 136 /* start up the decoders */ |
137 st = av_new_stream(s, 0); | |
138 if (!st) | |
139 return AVERROR_NOMEM; | |
462
b69898ffc92a
move time_base (pts_num/pts_den) from AVFormatContext -> AVStream
michael
parents:
387
diff
changeset
|
140 av_set_pts_info(st, 33, 1, 90000); |
338 | 141 vmd->video_stream_index = st->index; |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
815
diff
changeset
|
142 st->codec->codec_type = CODEC_TYPE_VIDEO; |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
815
diff
changeset
|
143 st->codec->codec_id = CODEC_ID_VMDVIDEO; |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
815
diff
changeset
|
144 st->codec->codec_tag = 0; /* no fourcc */ |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
815
diff
changeset
|
145 st->codec->width = LE_16(&vmd->vmd_header[12]); |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
815
diff
changeset
|
146 st->codec->height = LE_16(&vmd->vmd_header[14]); |
1004
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
147 st->codec->time_base.num = 1; |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
148 st->codec->time_base.den = 10; |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
815
diff
changeset
|
149 st->codec->extradata_size = VMD_HEADER_SIZE; |
884
2ece9c9dd94c
malloc padding to avoid reading past the malloc()ed area.
henry
parents:
881
diff
changeset
|
150 st->codec->extradata = av_mallocz(VMD_HEADER_SIZE + FF_INPUT_BUFFER_PADDING_SIZE); |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
815
diff
changeset
|
151 memcpy(st->codec->extradata, vmd->vmd_header, VMD_HEADER_SIZE); |
338 | 152 |
153 /* if sample rate is 0, assume no audio */ | |
387
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
154 vmd->sample_rate = LE_16(&vmd->vmd_header[804]); |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
155 if (vmd->sample_rate) { |
338 | 156 st = av_new_stream(s, 0); |
157 if (!st) | |
158 return AVERROR_NOMEM; | |
462
b69898ffc92a
move time_base (pts_num/pts_den) from AVFormatContext -> AVStream
michael
parents:
387
diff
changeset
|
159 av_set_pts_info(st, 33, 1, 90000); |
338 | 160 vmd->audio_stream_index = st->index; |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
815
diff
changeset
|
161 st->codec->codec_type = CODEC_TYPE_AUDIO; |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
815
diff
changeset
|
162 st->codec->codec_id = CODEC_ID_VMDAUDIO; |
1004
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
163 st->codec->codec_tag = 0; /* no fourcc */ |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
164 st->codec->channels = vmd->audio_channels = (vmd->vmd_header[811] & 0x80) ? 2 : 1; |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
815
diff
changeset
|
165 st->codec->sample_rate = vmd->sample_rate; |
885 | 166 st->codec->block_align = vmd->audio_block_align = |
387
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
167 LE_16(&vmd->vmd_header[806]); |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
815
diff
changeset
|
168 if (st->codec->block_align & 0x8000) { |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
815
diff
changeset
|
169 st->codec->bits_per_sample = 16; |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
815
diff
changeset
|
170 st->codec->block_align = -(st->codec->block_align - 0x10000); |
1004
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
171 vmd->audio_block_align = -(vmd->audio_block_align - 0x10000); |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
172 } else { |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
173 st->codec->bits_per_sample = 8; |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
174 } |
885 | 175 st->codec->bit_rate = st->codec->sample_rate * |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
815
diff
changeset
|
176 st->codec->bits_per_sample * st->codec->channels; |
387
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
177 |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
178 /* for calculating pts */ |
1004
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
179 vmd->audio_frame_divisor = st->codec->channels; |
387
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
180 |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
181 video_pts_inc = 90000; |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
815
diff
changeset
|
182 video_pts_inc *= st->codec->block_align; |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
815
diff
changeset
|
183 video_pts_inc /= st->codec->sample_rate; |
1004
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
184 video_pts_inc /= st->codec->channels; |
387
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
185 } else { |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
186 /* if no audio, assume 10 frames/second */ |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
187 video_pts_inc = 90000 / 10; |
338 | 188 } |
189 | |
190 toc_offset = LE_32(&vmd->vmd_header[812]); | |
191 vmd->frame_count = LE_16(&vmd->vmd_header[6]); | |
1004
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
192 vmd->frames_per_block = LE_16(&vmd->vmd_header[18]); |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
193 url_fseek(pb, toc_offset, SEEK_SET); |
338 | 194 |
195 raw_frame_table = NULL; | |
196 vmd->frame_table = NULL; | |
1004
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
197 raw_frame_table_size = vmd->frame_count * 6; |
338 | 198 raw_frame_table = av_malloc(raw_frame_table_size); |
1004
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
199 vmd->frame_table = av_malloc(vmd->frame_count * vmd->frames_per_block * sizeof(vmd_frame_t)); |
338 | 200 if (!raw_frame_table || !vmd->frame_table) { |
201 av_free(raw_frame_table); | |
202 av_free(vmd->frame_table); | |
203 return AVERROR_NOMEM; | |
204 } | |
885 | 205 if (get_buffer(pb, raw_frame_table, raw_frame_table_size) != |
338 | 206 raw_frame_table_size) { |
207 av_free(raw_frame_table); | |
208 av_free(vmd->frame_table); | |
482 | 209 return AVERROR_IO; |
338 | 210 } |
211 | |
1004
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
212 total_frames = 0; |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
213 for (i = 0; i < vmd->frame_count; i++) { |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
214 |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
215 current_offset = LE_32(&raw_frame_table[6 * i + 2]); |
338 | 216 |
1004
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
217 /* handle each entry in index block */ |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
218 for (j = 0; j < vmd->frames_per_block; j++) { |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
219 int type; |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
220 uint32_t size; |
338 | 221 |
1004
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
222 get_buffer(pb, chunk, BYTES_PER_FRAME_RECORD); |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
223 type = chunk[0]; |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
224 size = LE_32(&chunk[2]); |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
225 if(!size) |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
226 continue; |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
227 switch(type) { |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
228 case 1: /* Audio Chunk */ |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
229 vmd->frame_table[total_frames].frame_offset = current_offset; |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
230 vmd->frame_table[total_frames].stream_index = vmd->audio_stream_index; |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
231 vmd->frame_table[total_frames].frame_size = size; |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
232 memcpy(vmd->frame_table[total_frames].frame_record, chunk, BYTES_PER_FRAME_RECORD); |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
233 total_frames++; |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
234 break; |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
235 case 2: /* Video Chunk */ |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
236 vmd->frame_table[total_frames].frame_offset = current_offset; |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
237 vmd->frame_table[total_frames].frame_size = size; |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
238 vmd->frame_table[total_frames].stream_index = vmd->video_stream_index; |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
239 memcpy(vmd->frame_table[total_frames].frame_record, chunk, BYTES_PER_FRAME_RECORD); |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
240 vmd->frame_table[total_frames].pts = current_video_pts; |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
241 if (lastframe) { |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
242 vmd->frame_table[lastframe].pts = current_video_pts - video_pts_inc; |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
243 } |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
244 lastframe = total_frames; |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
245 total_frames++; |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
246 break; |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
247 } |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
248 current_offset += size; |
387
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
249 } |
1004
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
250 current_video_pts += video_pts_inc; |
338 | 251 } |
252 | |
253 av_free(raw_frame_table); | |
254 | |
255 vmd->current_frame = 0; | |
1004
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
256 vmd->frame_count = total_frames; |
338 | 257 |
258 return 0; | |
259 } | |
260 | |
261 static int vmd_read_packet(AVFormatContext *s, | |
262 AVPacket *pkt) | |
263 { | |
264 VmdDemuxContext *vmd = (VmdDemuxContext *)s->priv_data; | |
265 ByteIOContext *pb = &s->pb; | |
266 int ret = 0; | |
267 vmd_frame_t *frame; | |
268 | |
269 if (vmd->current_frame >= vmd->frame_count) | |
482 | 270 return AVERROR_IO; |
338 | 271 |
272 frame = &vmd->frame_table[vmd->current_frame]; | |
273 /* position the stream (will probably be there already) */ | |
274 url_fseek(pb, frame->frame_offset, SEEK_SET); | |
275 | |
276 if (av_new_packet(pkt, frame->frame_size + BYTES_PER_FRAME_RECORD)) | |
277 return AVERROR_NOMEM; | |
775 | 278 pkt->pos= url_ftell(pb); |
338 | 279 memcpy(pkt->data, frame->frame_record, BYTES_PER_FRAME_RECORD); |
885 | 280 ret = get_buffer(pb, pkt->data + BYTES_PER_FRAME_RECORD, |
338 | 281 frame->frame_size); |
282 | |
387
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
283 if (ret != frame->frame_size) { |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
284 av_free_packet(pkt); |
482 | 285 ret = AVERROR_IO; |
387
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
286 } |
338 | 287 pkt->stream_index = frame->stream_index; |
387
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
288 if (frame->frame_record[0] == 0x02) |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
289 pkt->pts = frame->pts; |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
290 else { |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
291 pkt->pts = vmd->audio_sample_counter; |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
292 pkt->pts *= 90000; |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
293 pkt->pts /= vmd->sample_rate; |
1004
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
294 pkt->pts /= vmd->audio_channels; |
387
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
295 vmd->audio_sample_counter += vmd_calculate_audio_duration( |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
296 pkt->data, pkt->size, vmd->audio_block_align); |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
297 |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
298 } |
881 | 299 av_log(NULL, AV_LOG_INFO, " dispatching %s frame with %d bytes and pts %"PRId64" (%0.1f sec)\n", |
387
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
300 (frame->frame_record[0] == 0x02) ? "video" : "audio", |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
301 frame->frame_size + BYTES_PER_FRAME_RECORD, |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
302 pkt->pts, (float)(pkt->pts / 90000.0)); |
338 | 303 |
304 vmd->current_frame++; | |
305 | |
306 return ret; | |
307 } | |
308 | |
309 static int vmd_read_close(AVFormatContext *s) | |
310 { | |
311 VmdDemuxContext *vmd = (VmdDemuxContext *)s->priv_data; | |
312 | |
313 av_free(vmd->frame_table); | |
314 | |
315 return 0; | |
316 } | |
317 | |
318 static AVInputFormat vmd_iformat = { | |
319 "vmd", | |
320 "Sierra VMD format", | |
321 sizeof(VmdDemuxContext), | |
322 vmd_probe, | |
323 vmd_read_header, | |
324 vmd_read_packet, | |
325 vmd_read_close, | |
326 }; | |
327 | |
328 int vmd_init(void) | |
329 { | |
330 av_register_input_format(&vmd_iformat); | |
331 return 0; | |
332 } |