Mercurial > libavformat.hg
annotate sierravmd.c @ 1478:5e1b279a26d2 libavformat
nut simple seeking first try
author | michael |
---|---|
date | Sat, 11 Nov 2006 19:37:21 +0000 |
parents | ad3b03b7b142 |
children | deaec052eec4 |
rev | line source |
---|---|
338 | 1 /* |
2 * Sierra VMD Format Demuxer | |
3 * Copyright (c) 2004 The ffmpeg Project | |
4 * | |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1169
diff
changeset
|
5 * This file is part of FFmpeg. |
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1169
diff
changeset
|
6 * |
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1169
diff
changeset
|
7 * FFmpeg is free software; you can redistribute it and/or |
338 | 8 * modify it under the terms of the GNU Lesser General Public |
9 * License as published by the Free Software Foundation; either | |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1169
diff
changeset
|
10 * version 2.1 of the License, or (at your option) any later version. |
338 | 11 * |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1169
diff
changeset
|
12 * FFmpeg is distributed in the hope that it will be useful, |
338 | 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 | |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1169
diff
changeset
|
18 * License along with FFmpeg; if not, write to the Free Software |
896
edbe5c3717f9
Update licensing information: The FSF changed postal address.
diego
parents:
887
diff
changeset
|
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
338 | 20 */ |
21 | |
22 /** | |
23 * @file sierravmd.c | |
24 * Sierra VMD file demuxer | |
25 * 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
|
26 * 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
|
27 * http://www.pcisys.net/~melanson/codecs/ |
338 | 28 */ |
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 audio_type; | |
49 unsigned int audio_samplerate; | |
50 unsigned int audio_bits; | |
51 unsigned int audio_channels; | |
52 | |
53 unsigned int frame_count; | |
1004
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
54 unsigned int frames_per_block; |
338 | 55 vmd_frame_t *frame_table; |
56 unsigned int current_frame; | |
57 | |
387
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
58 int sample_rate; |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
59 int64_t audio_sample_counter; |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
60 int audio_frame_divisor; |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
61 int audio_block_align; |
1004
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
62 int skiphdr; |
387
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
63 |
338 | 64 unsigned char vmd_header[VMD_HEADER_SIZE]; |
65 } VmdDemuxContext; | |
66 | |
67 static int vmd_probe(AVProbeData *p) | |
68 { | |
69 if (p->buf_size < 2) | |
70 return 0; | |
71 | |
72 /* check if the first 2 bytes of the file contain the appropriate size | |
73 * of a VMD header chunk */ | |
74 if (LE_16(&p->buf[0]) != VMD_HEADER_SIZE - 2) | |
75 return 0; | |
76 | |
77 /* only return half certainty since this check is a bit sketchy */ | |
78 return AVPROBE_SCORE_MAX / 2; | |
79 } | |
80 | |
387
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
81 /* 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
|
82 * 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
|
83 * encodings. */ |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
84 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
|
85 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
|
86 { |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
87 unsigned char *p = audio_chunk + 16; |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
88 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
|
89 int total_samples = 0; |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
90 unsigned int sound_flags; |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
91 |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
92 if (audio_chunk_size < 16) |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
93 return 0; |
1004
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
94 if (audio_chunk_size == block_align + 16) |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
95 return block_align; |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
96 if (audio_chunk_size == block_align + 17) |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
97 return block_align; |
387
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
98 |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
99 sound_flags = LE_32(p); |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
100 p += 4; |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
101 while (p < p_end) { |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
102 total_samples += block_align; |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
103 if ((sound_flags & 0x01) == 0) |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
104 p += block_align; |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
105 sound_flags >>= 1; |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
106 } |
1004
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
107 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
|
108 |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
109 return total_samples; |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
110 } |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
111 |
338 | 112 static int vmd_read_header(AVFormatContext *s, |
113 AVFormatParameters *ap) | |
114 { | |
115 VmdDemuxContext *vmd = (VmdDemuxContext *)s->priv_data; | |
116 ByteIOContext *pb = &s->pb; | |
117 AVStream *st; | |
118 unsigned int toc_offset; | |
119 unsigned char *raw_frame_table; | |
120 int raw_frame_table_size; | |
121 offset_t current_offset; | |
1004
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
122 int i, j; |
338 | 123 unsigned int total_frames; |
1004
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
124 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
|
125 int64_t current_video_pts = 0; |
1004
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
126 unsigned char chunk[BYTES_PER_FRAME_RECORD]; |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
127 int lastframe = 0; |
338 | 128 |
129 /* fetch the main header, including the 2 header length bytes */ | |
130 url_fseek(pb, 0, SEEK_SET); | |
131 if (get_buffer(pb, vmd->vmd_header, VMD_HEADER_SIZE) != VMD_HEADER_SIZE) | |
482 | 132 return AVERROR_IO; |
338 | 133 |
387
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
134 vmd->audio_sample_counter = 0; |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
135 vmd->audio_frame_divisor = 1; |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
136 vmd->audio_block_align = 1; |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
137 |
338 | 138 /* start up the decoders */ |
139 st = av_new_stream(s, 0); | |
140 if (!st) | |
141 return AVERROR_NOMEM; | |
462
b69898ffc92a
move time_base (pts_num/pts_den) from AVFormatContext -> AVStream
michael
parents:
387
diff
changeset
|
142 av_set_pts_info(st, 33, 1, 90000); |
338 | 143 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
|
144 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
|
145 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
|
146 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
|
147 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
|
148 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
|
149 st->codec->time_base.num = 1; |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
150 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
|
151 st->codec->extradata_size = VMD_HEADER_SIZE; |
884
2ece9c9dd94c
malloc padding to avoid reading past the malloc()ed area.
henry
parents:
881
diff
changeset
|
152 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
|
153 memcpy(st->codec->extradata, vmd->vmd_header, VMD_HEADER_SIZE); |
338 | 154 |
155 /* 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
|
156 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
|
157 if (vmd->sample_rate) { |
338 | 158 st = av_new_stream(s, 0); |
159 if (!st) | |
160 return AVERROR_NOMEM; | |
462
b69898ffc92a
move time_base (pts_num/pts_den) from AVFormatContext -> AVStream
michael
parents:
387
diff
changeset
|
161 av_set_pts_info(st, 33, 1, 90000); |
338 | 162 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
|
163 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
|
164 st->codec->codec_id = CODEC_ID_VMDAUDIO; |
1004
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
165 st->codec->codec_tag = 0; /* no fourcc */ |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
166 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
|
167 st->codec->sample_rate = vmd->sample_rate; |
885 | 168 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
|
169 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
|
170 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
|
171 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
|
172 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
|
173 vmd->audio_block_align = -(vmd->audio_block_align - 0x10000); |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
174 } else { |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
175 st->codec->bits_per_sample = 8; |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
176 } |
885 | 177 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
|
178 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
|
179 |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
180 /* for calculating pts */ |
1004
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
181 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
|
182 |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
183 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
|
184 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
|
185 video_pts_inc /= st->codec->sample_rate; |
1004
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
186 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
|
187 } else { |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
188 /* 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
|
189 video_pts_inc = 90000 / 10; |
338 | 190 } |
191 | |
192 toc_offset = LE_32(&vmd->vmd_header[812]); | |
193 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
|
194 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
|
195 url_fseek(pb, toc_offset, SEEK_SET); |
338 | 196 |
197 raw_frame_table = NULL; | |
198 vmd->frame_table = NULL; | |
1004
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
199 raw_frame_table_size = vmd->frame_count * 6; |
338 | 200 raw_frame_table = av_malloc(raw_frame_table_size); |
1079 | 201 if(vmd->frame_count * vmd->frames_per_block >= UINT_MAX / sizeof(vmd_frame_t)){ |
202 av_log(s, AV_LOG_ERROR, "vmd->frame_count * vmd->frames_per_block too large\n"); | |
203 return -1; | |
204 } | |
1004
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
205 vmd->frame_table = av_malloc(vmd->frame_count * vmd->frames_per_block * sizeof(vmd_frame_t)); |
338 | 206 if (!raw_frame_table || !vmd->frame_table) { |
207 av_free(raw_frame_table); | |
208 av_free(vmd->frame_table); | |
209 return AVERROR_NOMEM; | |
210 } | |
885 | 211 if (get_buffer(pb, raw_frame_table, raw_frame_table_size) != |
338 | 212 raw_frame_table_size) { |
213 av_free(raw_frame_table); | |
214 av_free(vmd->frame_table); | |
482 | 215 return AVERROR_IO; |
338 | 216 } |
217 | |
1004
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
218 total_frames = 0; |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
219 for (i = 0; i < vmd->frame_count; i++) { |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
220 |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
221 current_offset = LE_32(&raw_frame_table[6 * i + 2]); |
338 | 222 |
1004
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
223 /* handle each entry in index block */ |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
224 for (j = 0; j < vmd->frames_per_block; j++) { |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
225 int type; |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
226 uint32_t size; |
338 | 227 |
1004
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
228 get_buffer(pb, chunk, BYTES_PER_FRAME_RECORD); |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
229 type = chunk[0]; |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
230 size = LE_32(&chunk[2]); |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
231 if(!size) |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
232 continue; |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
233 switch(type) { |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
234 case 1: /* Audio Chunk */ |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
235 vmd->frame_table[total_frames].frame_offset = current_offset; |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
236 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
|
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 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
|
239 total_frames++; |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
240 break; |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
241 case 2: /* Video Chunk */ |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
242 vmd->frame_table[total_frames].frame_offset = current_offset; |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
243 vmd->frame_table[total_frames].frame_size = size; |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
244 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
|
245 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
|
246 vmd->frame_table[total_frames].pts = current_video_pts; |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
247 if (lastframe) { |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
248 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
|
249 } |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
250 lastframe = total_frames; |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
251 total_frames++; |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
252 break; |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
253 } |
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
254 current_offset += size; |
387
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
255 } |
1004
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
256 current_video_pts += video_pts_inc; |
338 | 257 } |
258 | |
259 av_free(raw_frame_table); | |
260 | |
261 vmd->current_frame = 0; | |
1004
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
262 vmd->frame_count = total_frames; |
338 | 263 |
264 return 0; | |
265 } | |
266 | |
267 static int vmd_read_packet(AVFormatContext *s, | |
268 AVPacket *pkt) | |
269 { | |
270 VmdDemuxContext *vmd = (VmdDemuxContext *)s->priv_data; | |
271 ByteIOContext *pb = &s->pb; | |
272 int ret = 0; | |
273 vmd_frame_t *frame; | |
274 | |
275 if (vmd->current_frame >= vmd->frame_count) | |
482 | 276 return AVERROR_IO; |
338 | 277 |
278 frame = &vmd->frame_table[vmd->current_frame]; | |
279 /* position the stream (will probably be there already) */ | |
280 url_fseek(pb, frame->frame_offset, SEEK_SET); | |
281 | |
282 if (av_new_packet(pkt, frame->frame_size + BYTES_PER_FRAME_RECORD)) | |
283 return AVERROR_NOMEM; | |
775 | 284 pkt->pos= url_ftell(pb); |
338 | 285 memcpy(pkt->data, frame->frame_record, BYTES_PER_FRAME_RECORD); |
885 | 286 ret = get_buffer(pb, pkt->data + BYTES_PER_FRAME_RECORD, |
338 | 287 frame->frame_size); |
288 | |
387
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
289 if (ret != frame->frame_size) { |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
290 av_free_packet(pkt); |
482 | 291 ret = AVERROR_IO; |
387
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
292 } |
338 | 293 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
|
294 if (frame->frame_record[0] == 0x02) |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
295 pkt->pts = frame->pts; |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
296 else { |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
297 pkt->pts = vmd->audio_sample_counter; |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
298 pkt->pts *= 90000; |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
299 pkt->pts /= vmd->sample_rate; |
1004
409b399440a3
More correct demuxing and timestamp setting fot Sierra VMD
kostya
parents:
896
diff
changeset
|
300 pkt->pts /= vmd->audio_channels; |
387
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
301 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
|
302 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
|
303 |
f2760852ed18
minor VMD system update; still not perfect, but should not crash either
melanson
parents:
338
diff
changeset
|
304 } |
1441
ad3b03b7b142
reindentation, patch by From: Steve Lhomme, slhomme divxcorp com
diego
parents:
1358
diff
changeset
|
305 av_log(NULL, AV_LOG_INFO, " dispatching %s frame with %d bytes and pts %"PRId64" (%0.1f sec)\n", |
ad3b03b7b142
reindentation, patch by From: Steve Lhomme, slhomme divxcorp com
diego
parents:
1358
diff
changeset
|
306 (frame->frame_record[0] == 0x02) ? "video" : "audio", |
ad3b03b7b142
reindentation, patch by From: Steve Lhomme, slhomme divxcorp com
diego
parents:
1358
diff
changeset
|
307 frame->frame_size + BYTES_PER_FRAME_RECORD, |
ad3b03b7b142
reindentation, patch by From: Steve Lhomme, slhomme divxcorp com
diego
parents:
1358
diff
changeset
|
308 pkt->pts, (float)(pkt->pts / 90000.0)); |
338 | 309 |
310 vmd->current_frame++; | |
311 | |
312 return ret; | |
313 } | |
314 | |
315 static int vmd_read_close(AVFormatContext *s) | |
316 { | |
317 VmdDemuxContext *vmd = (VmdDemuxContext *)s->priv_data; | |
318 | |
319 av_free(vmd->frame_table); | |
320 | |
321 return 0; | |
322 } | |
323 | |
1169 | 324 AVInputFormat vmd_demuxer = { |
338 | 325 "vmd", |
326 "Sierra VMD format", | |
327 sizeof(VmdDemuxContext), | |
328 vmd_probe, | |
329 vmd_read_header, | |
330 vmd_read_packet, | |
331 vmd_read_close, | |
332 }; |