Mercurial > audlegacy-plugins
annotate src/wma/libffwma/asf.c @ 3123:a2093254960a
Sanity check to work around a WMA decoding error.
author | John Lindgren <john.lindgren@tds.net> |
---|---|
date | Wed, 06 May 2009 12:11:06 -0400 |
parents | 55703f60fd54 |
children |
rev | line source |
---|---|
878 | 1 /* |
2 * ASF compatible encoder and decoder. | |
3 * Copyright (c) 2000, 2001 Fabrice Bellard. | |
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 #include "avcodec.h" | |
20 #include "avformat.h" | |
21 #include "avi.h" | |
22 #include "utils.h" | |
23 | |
24 #include <assert.h> | |
25 #define MPA_FRAME_SIZE 1152 | |
26 #define PACKET_SIZE 3200 | |
27 #define PACKET_HEADER_SIZE 12 | |
28 #define FRAME_HEADER_SIZE 17 | |
29 | |
30 typedef struct { | |
31 int num; | |
32 int seq; | |
33 /* use for reading */ | |
34 AVPacket pkt; | |
35 int frag_offset; | |
36 int timestamp; | |
37 int64_t duration; | |
38 | |
39 int ds_span; /* descrambling */ | |
40 int ds_packet_size; | |
41 int ds_chunk_size; | |
42 int ds_data_size; | |
43 int ds_silence_data; | |
3123
a2093254960a
Sanity check to work around a WMA decoding error.
John Lindgren <john.lindgren@tds.net>
parents:
1169
diff
changeset
|
44 |
878 | 45 int packet_pos; |
46 | |
47 } ASFStream; | |
48 | |
49 typedef struct { | |
50 uint32_t v1; | |
51 uint16_t v2; | |
52 uint16_t v3; | |
53 uint8_t v4[8]; | |
54 } GUID; | |
55 | |
56 typedef struct { | |
57 GUID guid; // generated by client computer | |
58 uint64_t file_size; // in bytes | |
59 // invalid if broadcasting | |
60 uint64_t create_time; // time of creation, in 100-nanosecond units since 1.1.1601 | |
61 // invalid if broadcasting | |
62 uint64_t packets_count; // how many packets are there in the file | |
63 // invalid if broadcasting | |
64 uint64_t play_time; // play time, in 100-nanosecond units | |
65 // invalid if broadcasting | |
66 uint64_t send_time; // time to send file, in 100-nanosecond units | |
67 // invalid if broadcasting (could be ignored) | |
68 uint32_t preroll; // timestamp of the first packet, in milliseconds | |
69 // if nonzero - substract from time | |
70 uint32_t ignore; // preroll is 64bit - but let's just ignore it | |
71 uint32_t flags; // 0x01 - broadcast | |
72 // 0x02 - seekable | |
73 // rest is reserved should be 0 | |
74 uint32_t min_pktsize; // size of a data packet | |
75 // invalid if broadcasting | |
76 uint32_t max_pktsize; // shall be the same as for min_pktsize | |
77 // invalid if broadcasting | |
78 uint32_t max_bitrate; // bandwith of stream in bps | |
79 // should be the sum of bitrates of the | |
80 // individual media streams | |
81 } ASFMainHeader; | |
82 | |
83 | |
84 typedef struct { | |
85 int seqno; | |
86 int packet_size; | |
87 int is_streamed; | |
88 int asfid2avid[128]; /* conversion table from asf ID 2 AVStream ID */ | |
89 ASFStream streams[128]; /* it's max number and it's not that big */ | |
90 /* non streamed additonnal info */ | |
91 int64_t nb_packets; | |
92 int64_t duration; /* in 100ns units */ | |
93 /* packet filling */ | |
94 int packet_size_left; | |
95 int packet_timestamp_start; | |
96 int packet_timestamp_end; | |
97 int packet_nb_frames; | |
98 uint8_t packet_buf[PACKET_SIZE]; | |
99 ByteIOContext pb; | |
100 /* only for reading */ | |
101 uint64_t data_offset; /* begining of the first data packet */ | |
102 | |
103 ASFMainHeader hdr; | |
104 | |
105 int packet_flags; | |
106 int packet_property; | |
107 int packet_timestamp; | |
108 int packet_segsizetype; | |
109 int packet_segments; | |
110 int packet_seq; | |
111 int packet_replic_size; | |
112 int packet_key_frame; | |
113 int packet_padsize; | |
114 int packet_frag_offset; | |
115 int packet_frag_size; | |
116 int packet_frag_timestamp; | |
117 int packet_multi_size; | |
118 int packet_obj_size; | |
119 int packet_time_delta; | |
120 int packet_time_start; | |
121 int packet_pos; | |
122 | |
123 int stream_index; | |
124 ASFStream* asf_st; /* currently decoded stream */ | |
125 } ASFContext; | |
126 | |
127 static const GUID asf_header = { | |
128 0x75B22630, 0x668E, 0x11CF, { 0xA6, 0xD9, 0x00, 0xAA, 0x00, 0x62, 0xCE, 0x6C }, | |
129 }; | |
130 | |
131 static const GUID file_header = { | |
132 0x8CABDCA1, 0xA947, 0x11CF, { 0x8E, 0xE4, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65 }, | |
133 }; | |
134 | |
135 static const GUID stream_header = { | |
136 0xB7DC0791, 0xA9B7, 0x11CF, { 0x8E, 0xE6, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65 }, | |
137 }; | |
138 | |
139 static const GUID audio_stream = { | |
140 0xF8699E40, 0x5B4D, 0x11CF, { 0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B }, | |
141 }; | |
142 | |
143 static const GUID audio_conceal_none = { | |
144 // 0x49f1a440, 0x4ece, 0x11d0, { 0xa3, 0xac, 0x00, 0xa0, 0xc9, 0x03, 0x48, 0xf6 }, | |
145 // New value lifted from avifile | |
146 0x20fb5700, 0x5b55, 0x11cf, { 0xa8, 0xfd, 0x00, 0x80, 0x5f, 0x5c, 0x44, 0x2b }, | |
147 }; | |
148 | |
149 | |
150 static const GUID comment_header = { | |
151 0x75b22633, 0x668e, 0x11cf, { 0xa6, 0xd9, 0x00, 0xaa, 0x00, 0x62, 0xce, 0x6c }, | |
152 }; | |
153 | |
154 static const GUID codec_comment_header = { | |
155 0x86D15240, 0x311D, 0x11D0, { 0xA3, 0xA4, 0x00, 0xA0, 0xC9, 0x03, 0x48, 0xF6 }, | |
156 }; | |
157 static const GUID codec_comment1_header = { | |
158 0x86d15241, 0x311d, 0x11d0, { 0xa3, 0xa4, 0x00, 0xa0, 0xc9, 0x03, 0x48, 0xf6 }, | |
159 }; | |
160 | |
161 static const GUID data_header = { | |
162 0x75b22636, 0x668e, 0x11cf, { 0xa6, 0xd9, 0x00, 0xaa, 0x00, 0x62, 0xce, 0x6c }, | |
163 }; | |
164 | |
165 static const GUID index_guid = { | |
166 0x33000890, 0xe5b1, 0x11cf, { 0x89, 0xf4, 0x00, 0xa0, 0xc9, 0x03, 0x49, 0xcb }, | |
167 }; | |
168 | |
169 static const GUID head1_guid = { | |
170 0x5fbf03b5, 0xa92e, 0x11cf, { 0x8e, 0xe3, 0x00, 0xc0, 0x0c, 0x20, 0x53, 0x65 }, | |
171 }; | |
172 | |
173 static const GUID head2_guid = { | |
174 0xabd3d211, 0xa9ba, 0x11cf, { 0x8e, 0xe6, 0x00, 0xc0, 0x0c, 0x20, 0x53, 0x65 }, | |
175 }; | |
176 | |
177 static const GUID extended_content_header = { | |
178 0xD2D0A440, 0xE307, 0x11D2, { 0x97, 0xF0, 0x00, 0xA0, 0xC9, 0x5E, 0xA8, 0x50 }, | |
179 }; | |
180 | |
181 /* I am not a number !!! This GUID is the one found on the PC used to | |
182 generate the stream */ | |
183 static const GUID my_guid = { | |
184 0, 0, 0, { 0, 0, 0, 0, 0, 0, 0, 0 }, | |
185 }; | |
186 | |
187 const CodecTag codec_wav_tags[] = { | |
188 /* { CODEC_ID_MP2, 0x50 }, | |
189 { CODEC_ID_MP3, 0x55 }, | |
190 { CODEC_ID_AC3, 0x2000 }, | |
191 { CODEC_ID_PCM_S16LE, 0x01 }, | |
3123
a2093254960a
Sanity check to work around a WMA decoding error.
John Lindgren <john.lindgren@tds.net>
parents:
1169
diff
changeset
|
192 { CODEC_ID_PCM_U8, 0x01 }, |
878 | 193 { CODEC_ID_PCM_ALAW, 0x06 }, |
194 { CODEC_ID_PCM_MULAW, 0x07 }, | |
195 { CODEC_ID_ADPCM_MS, 0x02 }, | |
196 { CODEC_ID_ADPCM_IMA_WAV, 0x11 }, | |
3123
a2093254960a
Sanity check to work around a WMA decoding error.
John Lindgren <john.lindgren@tds.net>
parents:
1169
diff
changeset
|
197 { CODEC_ID_ADPCM_IMA_DK4, 0x61 }, |
878 | 198 { CODEC_ID_ADPCM_IMA_DK3, 0x62 },*/ |
199 { CODEC_ID_WMAV1, 0x160, 0 }, | |
200 { CODEC_ID_WMAV2, 0x161, 0 }, | |
201 { 0, 0, 0 }, | |
202 }; | |
203 | |
204 enum CodecID codec_get_id(const CodecTag *tags, unsigned int tag) | |
205 { | |
206 while (tags->id != 0) { | |
207 if( toupper((tag >> 0)&0xFF) == toupper((tags->tag >> 0)&0xFF) | |
208 && toupper((tag >> 8)&0xFF) == toupper((tags->tag >> 8)&0xFF) | |
209 && toupper((tag >>16)&0xFF) == toupper((tags->tag >>16)&0xFF) | |
210 && toupper((tag >>24)&0xFF) == toupper((tags->tag >>24)&0xFF)) | |
211 return tags->id; | |
212 tags++; | |
213 } | |
214 return CODEC_ID_NONE; | |
215 } | |
216 | |
217 int wav_codec_get_id(unsigned int tag, int bps) | |
218 { | |
219 int id; | |
220 id = codec_get_id(codec_wav_tags, tag); | |
221 if (id <= 0) | |
222 return id; | |
223 /* handle specific u8 codec */ | |
224 if (id == CODEC_ID_PCM_S16LE && bps == 8) | |
225 id = CODEC_ID_PCM_U8; | |
226 return id; | |
227 } | |
228 | |
229 void get_wav_header(ByteIOContext *pb, AVCodecContext *codec, int size) | |
230 { | |
231 int id; | |
232 | |
233 id = get_le16(pb); | |
234 codec->codec_type = CODEC_TYPE_AUDIO; | |
235 codec->codec_tag = id; | |
236 codec->channels = get_le16(pb); | |
237 codec->sample_rate = get_le32(pb); | |
238 codec->bit_rate = get_le32(pb) * 8; | |
239 codec->block_align = get_le16(pb); | |
240 if (size == 14) { /* We're dealing with plain vanilla WAVEFORMAT */ | |
241 codec->bits_per_sample = 8; | |
242 }else | |
243 codec->bits_per_sample = get_le16(pb); | |
244 codec->codec_id = wav_codec_get_id(id, codec->bits_per_sample); | |
245 | |
246 if (size > 16) { /* We're obviously dealing with WAVEFORMATEX */ | |
247 codec->extradata_size = get_le16(pb); | |
248 if (codec->extradata_size > 0) { | |
249 if (codec->extradata_size > size - 18) | |
250 codec->extradata_size = size - 18; | |
251 codec->extradata = av_mallocz(codec->extradata_size); | |
252 get_buffer(pb, codec->extradata, codec->extradata_size); | |
253 } else | |
254 codec->extradata_size = 0; | |
255 | |
256 /* It is possible for the chunk to contain garbage at the end */ | |
257 if (size - codec->extradata_size - 18 > 0) | |
258 url_fskip(pb, size - codec->extradata_size - 18); | |
259 } | |
260 } | |
261 | |
262 /**********************************/ | |
263 /* decoding */ | |
264 | |
265 #ifdef DEBUG | |
266 #define PRINT_IF_GUID(g,cmp) \ | |
267 if (!memcmp(g, &cmp, sizeof(GUID))) \ | |
268 printf("(GUID: %s) ", #cmp) | |
269 | |
270 static void print_guid(const GUID *g) | |
271 { | |
272 int i; | |
273 PRINT_IF_GUID(g, asf_header); | |
274 else PRINT_IF_GUID(g, file_header); | |
275 else PRINT_IF_GUID(g, stream_header); | |
276 else PRINT_IF_GUID(g, audio_stream); | |
277 else PRINT_IF_GUID(g, audio_conceal_none); | |
278 else PRINT_IF_GUID(g, comment_header); | |
279 else PRINT_IF_GUID(g, codec_comment_header); | |
280 else PRINT_IF_GUID(g, codec_comment1_header); | |
281 else PRINT_IF_GUID(g, data_header); | |
282 else PRINT_IF_GUID(g, index_guid); | |
283 else PRINT_IF_GUID(g, head1_guid); | |
284 else PRINT_IF_GUID(g, head2_guid); | |
285 else PRINT_IF_GUID(g, my_guid); | |
286 else | |
287 printf("(GUID: unknown) "); | |
288 printf("0x%08x, 0x%04x, 0x%04x, {", g->v1, g->v2, g->v3); | |
289 for(i=0;i<8;i++) | |
290 printf(" 0x%02x,", g->v4[i]); | |
291 printf("}\n"); | |
292 } | |
293 #undef PRINT_IF_GUID(g,cmp) | |
294 #endif | |
295 | |
296 static void get_guid(ByteIOContext *s, GUID *g) | |
297 { | |
298 int i; | |
299 | |
300 g->v1 = get_le32(s); | |
301 g->v2 = get_le16(s); | |
302 g->v3 = get_le16(s); | |
303 for(i=0;i<8;i++) | |
304 g->v4[i] = get_byte(s); | |
305 } | |
306 | |
307 static void get_str16_nolen(ByteIOContext *pb, int len, char *buf, int buf_size) | |
308 { | |
895
5d7e6f06c9b7
[svn] - now wma can handle wide characters in metadata.
yaz
parents:
878
diff
changeset
|
309 gchar *ucs, *uptr; |
5d7e6f06c9b7
[svn] - now wma can handle wide characters in metadata.
yaz
parents:
878
diff
changeset
|
310 gchar *tmp; |
5d7e6f06c9b7
[svn] - now wma can handle wide characters in metadata.
yaz
parents:
878
diff
changeset
|
311 int tmplen = len; |
5d7e6f06c9b7
[svn] - now wma can handle wide characters in metadata.
yaz
parents:
878
diff
changeset
|
312 |
1169
55703f60fd54
[svn] - add check for string length before call g_convert(). it may close #962.
yaz
parents:
895
diff
changeset
|
313 g_return_if_fail(len > 0); |
55703f60fd54
[svn] - add check for string length before call g_convert(). it may close #962.
yaz
parents:
895
diff
changeset
|
314 |
895
5d7e6f06c9b7
[svn] - now wma can handle wide characters in metadata.
yaz
parents:
878
diff
changeset
|
315 ucs = g_malloc0(len); |
5d7e6f06c9b7
[svn] - now wma can handle wide characters in metadata.
yaz
parents:
878
diff
changeset
|
316 uptr = ucs; |
878 | 317 |
895
5d7e6f06c9b7
[svn] - now wma can handle wide characters in metadata.
yaz
parents:
878
diff
changeset
|
318 while(tmplen > 0) { |
5d7e6f06c9b7
[svn] - now wma can handle wide characters in metadata.
yaz
parents:
878
diff
changeset
|
319 *uptr++ = (gchar)get_byte(pb); |
5d7e6f06c9b7
[svn] - now wma can handle wide characters in metadata.
yaz
parents:
878
diff
changeset
|
320 tmplen--; |
5d7e6f06c9b7
[svn] - now wma can handle wide characters in metadata.
yaz
parents:
878
diff
changeset
|
321 } |
5d7e6f06c9b7
[svn] - now wma can handle wide characters in metadata.
yaz
parents:
878
diff
changeset
|
322 |
5d7e6f06c9b7
[svn] - now wma can handle wide characters in metadata.
yaz
parents:
878
diff
changeset
|
323 tmp = g_convert(ucs, len, "UTF-8", "UCS-2LE", NULL, NULL, NULL); |
5d7e6f06c9b7
[svn] - now wma can handle wide characters in metadata.
yaz
parents:
878
diff
changeset
|
324 g_strlcpy(buf, tmp, buf_size); |
5d7e6f06c9b7
[svn] - now wma can handle wide characters in metadata.
yaz
parents:
878
diff
changeset
|
325 g_free(tmp); |
878 | 326 } |
327 | |
328 static int asf_probe(AVProbeData *pd) | |
329 { | |
330 GUID g; | |
331 const unsigned char *p; | |
332 int i; | |
333 | |
334 /* check file header */ | |
335 if (pd->buf_size <= 32) | |
336 return 0; | |
337 p = pd->buf; | |
338 g.v1 = p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24); | |
339 p += 4; | |
340 g.v2 = p[0] | (p[1] << 8); | |
341 p += 2; | |
342 g.v3 = p[0] | (p[1] << 8); | |
343 p += 2; | |
344 for(i=0;i<8;i++) | |
345 g.v4[i] = *p++; | |
346 | |
347 if (!memcmp(&g, &asf_header, sizeof(GUID))) | |
348 return AVPROBE_SCORE_MAX; | |
349 else | |
350 return 0; | |
351 } | |
352 | |
353 static int asf_read_header(AVFormatContext *s, AVFormatParameters *ap) | |
354 { | |
355 ASFContext *asf = s->priv_data; | |
356 GUID g; | |
357 ByteIOContext *pb = &s->pb; | |
358 AVStream *st; | |
359 ASFStream *asf_st; | |
360 //int size, i; | |
361 int i; | |
362 int64_t gsize; | |
3123
a2093254960a
Sanity check to work around a WMA decoding error.
John Lindgren <john.lindgren@tds.net>
parents:
1169
diff
changeset
|
363 uint64_t block_begin; |
878 | 364 |
365 av_set_pts_info(s, 32, 1, 1000); /* 32 bit pts in ms */ | |
366 | |
367 get_guid(pb, &g); | |
368 if (memcmp(&g, &asf_header, sizeof(GUID))) | |
369 goto fail; | |
370 get_le64(pb); | |
371 get_le32(pb); | |
372 get_byte(pb); | |
373 get_byte(pb); | |
3123
a2093254960a
Sanity check to work around a WMA decoding error.
John Lindgren <john.lindgren@tds.net>
parents:
1169
diff
changeset
|
374 |
a2093254960a
Sanity check to work around a WMA decoding error.
John Lindgren <john.lindgren@tds.net>
parents:
1169
diff
changeset
|
375 block_begin = 30; |
a2093254960a
Sanity check to work around a WMA decoding error.
John Lindgren <john.lindgren@tds.net>
parents:
1169
diff
changeset
|
376 |
878 | 377 memset(&asf->asfid2avid, -1, sizeof(asf->asfid2avid)); |
378 for(;;) { | |
3123
a2093254960a
Sanity check to work around a WMA decoding error.
John Lindgren <john.lindgren@tds.net>
parents:
1169
diff
changeset
|
379 if (url_ftell (pb) != block_begin) /* sanity check */ |
a2093254960a
Sanity check to work around a WMA decoding error.
John Lindgren <john.lindgren@tds.net>
parents:
1169
diff
changeset
|
380 { |
a2093254960a
Sanity check to work around a WMA decoding error.
John Lindgren <john.lindgren@tds.net>
parents:
1169
diff
changeset
|
381 printf ("ASF: error reading header; trying to recover...\n"); |
a2093254960a
Sanity check to work around a WMA decoding error.
John Lindgren <john.lindgren@tds.net>
parents:
1169
diff
changeset
|
382 url_fseek (pb, block_begin, SEEK_SET); |
a2093254960a
Sanity check to work around a WMA decoding error.
John Lindgren <john.lindgren@tds.net>
parents:
1169
diff
changeset
|
383 } |
a2093254960a
Sanity check to work around a WMA decoding error.
John Lindgren <john.lindgren@tds.net>
parents:
1169
diff
changeset
|
384 |
878 | 385 get_guid(pb, &g); |
386 gsize = get_le64(pb); | |
387 #ifdef DEBUG | |
388 printf("%08Lx: ", url_ftell(pb) - 24); | |
389 print_guid(&g); | |
390 printf(" size=0x%Lx\n", gsize); | |
391 #endif | |
392 if (gsize < 24) | |
393 goto fail; | |
3123
a2093254960a
Sanity check to work around a WMA decoding error.
John Lindgren <john.lindgren@tds.net>
parents:
1169
diff
changeset
|
394 |
a2093254960a
Sanity check to work around a WMA decoding error.
John Lindgren <john.lindgren@tds.net>
parents:
1169
diff
changeset
|
395 block_begin += gsize; |
a2093254960a
Sanity check to work around a WMA decoding error.
John Lindgren <john.lindgren@tds.net>
parents:
1169
diff
changeset
|
396 |
878 | 397 if (!memcmp(&g, &file_header, sizeof(GUID))) { |
398 get_guid(pb, &asf->hdr.guid); | |
399 asf->hdr.file_size = get_le64(pb); | |
400 asf->hdr.create_time = get_le64(pb); | |
401 asf->hdr.packets_count = get_le64(pb); | |
402 asf->hdr.play_time = get_le64(pb); | |
403 asf->hdr.send_time = get_le64(pb); | |
404 asf->hdr.preroll = get_le32(pb); | |
405 asf->hdr.ignore = get_le32(pb); | |
406 asf->hdr.flags = get_le32(pb); | |
407 asf->hdr.min_pktsize = get_le32(pb); | |
408 asf->hdr.max_pktsize = get_le32(pb); | |
409 asf->hdr.max_bitrate = get_le32(pb); | |
410 asf->packet_size = asf->hdr.max_pktsize; | |
411 asf->nb_packets = asf->hdr.packets_count; | |
412 } else if (!memcmp(&g, &stream_header, sizeof(GUID))) { | |
413 int type, total_size, type_specific_size; | |
414 //unsigned int tag1; | |
415 int64_t pos1, pos2; | |
416 | |
417 pos1 = url_ftell(pb); | |
418 | |
419 st = av_new_stream(s, 0); | |
420 if (!st) | |
421 goto fail; | |
422 asf_st = av_mallocz(sizeof(ASFStream)); | |
423 if (!asf_st) | |
424 goto fail; | |
425 st->priv_data = asf_st; | |
426 st->start_time = asf->hdr.preroll / (10000000 / AV_TIME_BASE); | |
3123
a2093254960a
Sanity check to work around a WMA decoding error.
John Lindgren <john.lindgren@tds.net>
parents:
1169
diff
changeset
|
427 st->duration = (asf->hdr.send_time - asf->hdr.preroll) / |
878 | 428 (10000000 / AV_TIME_BASE); |
429 get_guid(pb, &g); | |
430 if (!memcmp(&g, &audio_stream, sizeof(GUID))) { | |
431 type = CODEC_TYPE_AUDIO; | |
432 } else { | |
433 goto fail; | |
434 } | |
435 get_guid(pb, &g); | |
436 total_size = get_le64(pb); | |
437 type_specific_size = get_le32(pb); | |
438 get_le32(pb); | |
439 st->id = get_le16(pb) & 0x7f; /* stream id */ | |
440 // mapping of asf ID to AV stream ID; | |
441 asf->asfid2avid[st->id] = s->nb_streams - 1; | |
442 | |
443 get_le32(pb); | |
444 st->codec.codec_type = type; | |
445 /* 1 fps default (XXX: put 0 fps instead) */ | |
3123
a2093254960a
Sanity check to work around a WMA decoding error.
John Lindgren <john.lindgren@tds.net>
parents:
1169
diff
changeset
|
446 st->codec.frame_rate = 1; |
878 | 447 st->codec.frame_rate_base = 1; |
448 if (type == CODEC_TYPE_AUDIO) { | |
449 get_wav_header(pb, &st->codec, type_specific_size); | |
450 st->need_parsing = 1; | |
451 /* We have to init the frame size at some point .... */ | |
452 pos2 = url_ftell(pb); | |
453 if (gsize > (pos2 + 8 - pos1 + 24)) { | |
454 asf_st->ds_span = get_byte(pb); | |
455 asf_st->ds_packet_size = get_le16(pb); | |
456 asf_st->ds_chunk_size = get_le16(pb); | |
457 asf_st->ds_data_size = get_le16(pb); | |
458 asf_st->ds_silence_data = get_byte(pb); | |
459 } | |
460 //printf("Descrambling: ps:%d cs:%d ds:%d s:%d sd:%d\n", | |
461 // asf_st->ds_packet_size, asf_st->ds_chunk_size, | |
462 // asf_st->ds_data_size, asf_st->ds_span, asf_st->ds_silence_data); | |
463 if (asf_st->ds_span > 1) { | |
464 if (!asf_st->ds_chunk_size | |
465 || (asf_st->ds_packet_size/asf_st->ds_chunk_size <= 1)) | |
466 asf_st->ds_span = 0; // disable descrambling | |
467 } | |
468 switch (st->codec.codec_id) { | |
469 case CODEC_ID_PCM_S16LE: | |
470 case CODEC_ID_PCM_S16BE: | |
471 case CODEC_ID_PCM_U16LE: | |
472 case CODEC_ID_PCM_U16BE: | |
473 case CODEC_ID_PCM_S8: | |
474 case CODEC_ID_PCM_U8: | |
475 case CODEC_ID_PCM_ALAW: | |
476 case CODEC_ID_PCM_MULAW: | |
477 st->codec.frame_size = 1; | |
478 break; | |
479 default: | |
480 /* This is probably wrong, but it prevents a crash later */ | |
481 st->codec.frame_size = 1; | |
482 break; | |
483 } | |
484 } | |
485 pos2 = url_ftell(pb); | |
486 url_fskip(pb, gsize - (pos2 - pos1 + 24)); | |
487 } else if (!memcmp(&g, &data_header, sizeof(GUID))) { | |
488 break; | |
489 } else if (!memcmp(&g, &comment_header, sizeof(GUID))) { | |
490 int len1, len2, len3, len4, len5; | |
491 | |
492 len1 = get_le16(pb); | |
493 len2 = get_le16(pb); | |
494 len3 = get_le16(pb); | |
495 len4 = get_le16(pb); | |
496 len5 = get_le16(pb); | |
497 get_str16_nolen(pb, len1, s->title, sizeof(s->title)); | |
498 get_str16_nolen(pb, len2, s->author, sizeof(s->author)); | |
499 get_str16_nolen(pb, len3, s->copyright, sizeof(s->copyright)); | |
500 get_str16_nolen(pb, len4, s->comment, sizeof(s->comment)); | |
501 url_fskip(pb, len5); | |
502 } else if (!memcmp(&g, &extended_content_header, sizeof(GUID))) { | |
503 int desc_count, i; | |
504 | |
505 desc_count = get_le16(pb); | |
506 for(i=0;i<desc_count;i++) | |
507 { | |
508 int name_len,value_type,value_len,value_num = 0; | |
509 char *name, *value; | |
510 | |
511 name_len = get_le16(pb); | |
512 name = (char *)av_mallocz(name_len); | |
513 get_str16_nolen(pb, name_len, name, name_len); | |
514 value_type = get_le16(pb); | |
515 value_len = get_le16(pb); | |
516 if ((value_type == 0) || (value_type == 1)) // unicode or byte | |
517 { | |
518 value = (char *)av_mallocz(value_len); | |
519 get_str16_nolen(pb, value_len, value, value_len); | |
520 if (strcmp(name,"WM/AlbumTitle")==0) { strcpy(s->album, value); } | |
521 if (strcmp(name,"WM/Genre")==0) { strcpy(s->genre, value); } | |
522 if (strcmp(name,"WM/Year")==0) s->year = atoi(value); | |
523 free(value); | |
524 } | |
525 if ((value_type >= 2) || (value_type <= 5)) // boolean or DWORD or QWORD or WORD | |
526 { | |
527 if (value_type==2) value_num = get_le32(pb); | |
528 if (value_type==3) value_num = get_le32(pb); | |
529 if (value_type==4) value_num = get_le64(pb); | |
530 if (value_type==5) value_num = get_le16(pb); | |
531 if (strcmp(name,"WM/Track")==0) s->track = value_num + 1; | |
532 if (strcmp(name,"WM/TrackNumber")==0) s->track = value_num; | |
533 } | |
534 free(name); | |
535 } | |
536 } else if (url_feof(pb)) { | |
537 goto fail; | |
538 } else { | |
539 url_fseek(pb, gsize - 24, SEEK_CUR); | |
540 } | |
541 } | |
542 get_guid(pb, &g); | |
543 get_le64(pb); | |
544 get_byte(pb); | |
545 get_byte(pb); | |
546 if (url_feof(pb)) | |
547 goto fail; | |
548 asf->data_offset = url_ftell(pb); | |
549 asf->packet_size_left = 0; | |
550 | |
551 return 0; | |
552 | |
553 fail: | |
554 for(i=0;i<s->nb_streams;i++) { | |
555 AVStream *st = s->streams[i]; | |
556 if (st) { | |
557 free(st->priv_data); | |
558 free(st->codec.extradata); | |
559 } | |
560 free(st); | |
561 } | |
562 return -1; | |
563 } | |
564 | |
565 #define DO_2BITS(bits, var, defval) \ | |
566 switch (bits & 3) \ | |
567 { \ | |
568 case 3: var = get_le32(pb); rsize += 4; break; \ | |
569 case 2: var = get_le16(pb); rsize += 2; break; \ | |
570 case 1: var = get_byte(pb); rsize++; break; \ | |
571 default: var = defval; break; \ | |
572 } | |
573 | |
574 static int asf_get_packet(AVFormatContext *s) | |
575 { | |
576 ASFContext *asf = s->priv_data; | |
577 ByteIOContext *pb = &s->pb; | |
578 uint32_t packet_length, padsize; | |
579 int rsize = 9; | |
580 int c; | |
3123
a2093254960a
Sanity check to work around a WMA decoding error.
John Lindgren <john.lindgren@tds.net>
parents:
1169
diff
changeset
|
581 |
878 | 582 assert((url_ftell(&s->pb) - s->data_offset) % asf->packet_size == 0); |
3123
a2093254960a
Sanity check to work around a WMA decoding error.
John Lindgren <john.lindgren@tds.net>
parents:
1169
diff
changeset
|
583 |
878 | 584 c = get_byte(pb); |
585 if ((c & 0x0f) == 2) { // always true for now | |
586 if (get_le16(pb) != 0) { | |
587 if (!url_feof(pb)) | |
588 printf("ff asf bad non zero\n"); | |
589 return -EIO; | |
590 } | |
591 rsize+=2; | |
592 /* }else{ | |
593 if (!url_feof(pb)) | |
594 printf("ff asf bad header %x at:%lld\n", c, url_ftell(pb)); | |
595 return -EIO;*/ | |
596 } | |
597 | |
598 asf->packet_flags = get_byte(pb); | |
599 asf->packet_property = get_byte(pb); | |
600 | |
601 DO_2BITS(asf->packet_flags >> 5, packet_length, asf->packet_size); | |
602 DO_2BITS(asf->packet_flags >> 1, padsize, 0); // sequence ignored | |
603 DO_2BITS(asf->packet_flags >> 3, padsize, 0); // padding length | |
604 | |
605 asf->packet_timestamp = get_le32(pb); | |
606 get_le16(pb); /* duration */ | |
607 // rsize has at least 11 bytes which have to be present | |
608 | |
609 if (asf->packet_flags & 0x01) { | |
610 asf->packet_segsizetype = get_byte(pb); rsize++; | |
611 asf->packet_segments = asf->packet_segsizetype & 0x3f; | |
612 } else { | |
613 asf->packet_segments = 1; | |
614 asf->packet_segsizetype = 0x80; | |
615 } | |
616 asf->packet_size_left = packet_length - padsize - rsize; | |
617 if (packet_length < asf->hdr.min_pktsize) | |
618 padsize += asf->hdr.min_pktsize - packet_length; | |
619 asf->packet_padsize = padsize; | |
620 #ifdef DEBUG | |
621 printf("packet: size=%d padsize=%d left=%d\n", asf->packet_size, asf->packet_padsize, asf->packet_size_left); | |
622 #endif | |
623 return 0; | |
624 } | |
625 | |
626 static int asf_read_packet(AVFormatContext *s, AVPacket *pkt) | |
627 { | |
628 ASFContext *asf = s->priv_data; | |
629 ASFStream *asf_st = 0; | |
630 ByteIOContext *pb = &s->pb; | |
631 //static int pc = 0; | |
632 for (;;) { | |
633 int rsize = 0; | |
634 if (asf->packet_size_left < FRAME_HEADER_SIZE | |
635 || asf->packet_segments < 1) { | |
636 //asf->packet_size_left <= asf->packet_padsize) { | |
637 int ret = asf->packet_size_left + asf->packet_padsize; | |
638 //printf("PacketLeftSize:%d Pad:%d Pos:%Ld\n", asf->packet_size_left, asf->packet_padsize, url_ftell(pb)); | |
639 /* fail safe */ | |
640 url_fskip(pb, ret); | |
641 asf->packet_pos= url_ftell(&s->pb); | |
642 ret = asf_get_packet(s); | |
643 //printf("READ ASF PACKET %d r:%d c:%d\n", ret, asf->packet_size_left, pc++); | |
644 if (ret < 0 || url_feof(pb)) | |
645 return -EIO; | |
646 asf->packet_time_start = 0; | |
647 continue; | |
648 } | |
649 if (asf->packet_time_start == 0) { | |
650 /* read frame header */ | |
651 int num = get_byte(pb); | |
652 asf->packet_segments--; | |
653 rsize++; | |
654 asf->packet_key_frame = (num & 0x80) >> 7; | |
655 asf->stream_index = asf->asfid2avid[num & 0x7f]; | |
656 // sequence should be ignored! | |
657 DO_2BITS(asf->packet_property >> 4, asf->packet_seq, 0); | |
658 DO_2BITS(asf->packet_property >> 2, asf->packet_frag_offset, 0); | |
659 DO_2BITS(asf->packet_property, asf->packet_replic_size, 0); | |
660 //printf("key:%d stream:%d seq:%d offset:%d replic_size:%d\n", asf->packet_key_frame, asf->stream_index, asf->packet_seq, //asf->packet_frag_offset, asf->packet_replic_size); | |
661 if (asf->packet_replic_size > 1) { | |
662 assert(asf->packet_replic_size >= 8); | |
663 // it should be always at least 8 bytes - FIXME validate | |
664 asf->packet_obj_size = get_le32(pb); | |
665 asf->packet_frag_timestamp = get_le32(pb); // timestamp | |
666 if (asf->packet_replic_size > 8) | |
667 url_fskip(pb, asf->packet_replic_size - 8); | |
668 rsize += asf->packet_replic_size; // FIXME - check validity | |
669 } else if (asf->packet_replic_size==1){ | |
670 // multipacket - frag_offset is begining timestamp | |
671 asf->packet_time_start = asf->packet_frag_offset; | |
672 asf->packet_frag_offset = 0; | |
673 asf->packet_frag_timestamp = asf->packet_timestamp; | |
674 | |
675 asf->packet_time_delta = get_byte(pb); | |
676 rsize++; | |
677 }else{ | |
678 assert(asf->packet_replic_size==0); | |
679 } | |
680 if (asf->packet_flags & 0x01) { | |
681 DO_2BITS(asf->packet_segsizetype >> 6, asf->packet_frag_size, 0); // 0 is illegal | |
682 #undef DO_2BITS | |
683 //printf("Fragsize %d\n", asf->packet_frag_size); | |
684 } else { | |
685 asf->packet_frag_size = asf->packet_size_left - rsize; | |
686 //printf("Using rest %d %d %d\n", asf->packet_frag_size, asf->packet_size_left, rsize); | |
687 } | |
688 if (asf->packet_replic_size == 1) { | |
689 asf->packet_multi_size = asf->packet_frag_size; | |
690 if (asf->packet_multi_size > asf->packet_size_left) { | |
691 asf->packet_segments = 0; | |
692 continue; | |
693 } | |
694 } | |
695 asf->packet_size_left -= rsize; | |
696 //printf("___objsize____ %d %d rs:%d\n", asf->packet_obj_size, asf->packet_frag_offset, rsize); | |
697 | |
698 if (asf->stream_index < 0) { | |
699 asf->packet_time_start = 0; | |
700 /* unhandled packet (should not happen) */ | |
701 url_fskip(pb, asf->packet_frag_size); | |
702 asf->packet_size_left -= asf->packet_frag_size; | |
703 printf("ff asf skip %d %d\n", asf->packet_frag_size, num & 0x7f); | |
704 continue; | |
705 } | |
706 asf->asf_st = s->streams[asf->stream_index]->priv_data; | |
707 } | |
708 asf_st = asf->asf_st; | |
709 | |
710 if ((asf->packet_frag_offset != asf_st->frag_offset | |
711 || (asf->packet_frag_offset | |
712 && asf->packet_seq != asf_st->seq)) // seq should be ignored | |
713 ) { | |
714 /* cannot continue current packet: free it */ | |
715 // FIXME better check if packet was already allocated | |
716 printf("ff asf parser skips: %d - %d o:%d - %d %d %d fl:%d\n", | |
717 asf_st->pkt.size, | |
718 asf->packet_obj_size, | |
719 asf->packet_frag_offset, asf_st->frag_offset, | |
720 asf->packet_seq, asf_st->seq, asf->packet_frag_size); | |
721 if (asf_st->pkt.size) | |
722 av_free_packet(&asf_st->pkt); | |
723 asf_st->frag_offset = 0; | |
724 if (asf->packet_frag_offset != 0) { | |
725 url_fskip(pb, asf->packet_frag_size); | |
726 printf("ff asf parser skiping %db\n", asf->packet_frag_size); | |
727 asf->packet_size_left -= asf->packet_frag_size; | |
728 continue; | |
729 } | |
730 } | |
731 if (asf->packet_replic_size == 1) { | |
732 // frag_offset is here used as the begining timestamp | |
733 asf->packet_frag_timestamp = asf->packet_time_start; | |
734 asf->packet_time_start += asf->packet_time_delta; | |
735 asf->packet_obj_size = asf->packet_frag_size = get_byte(pb); | |
736 asf->packet_size_left--; | |
737 asf->packet_multi_size--; | |
738 if (asf->packet_multi_size < asf->packet_obj_size) | |
739 { | |
740 asf->packet_time_start = 0; | |
741 url_fskip(pb, asf->packet_multi_size); | |
742 asf->packet_size_left -= asf->packet_multi_size; | |
743 continue; | |
744 } | |
745 asf->packet_multi_size -= asf->packet_obj_size; | |
746 //printf("COMPRESS size %d %d %d ms:%d\n", asf->packet_obj_size, asf->packet_frag_timestamp, asf->packet_size_left, asf->packet_multi_size); | |
747 } | |
748 if (asf_st->frag_offset == 0) { | |
749 /* new packet */ | |
750 av_new_packet(&asf_st->pkt, asf->packet_obj_size); | |
751 asf_st->seq = asf->packet_seq; | |
752 asf_st->pkt.pts = asf->packet_frag_timestamp - asf->hdr.preroll; | |
753 asf_st->pkt.stream_index = asf->stream_index; | |
3123
a2093254960a
Sanity check to work around a WMA decoding error.
John Lindgren <john.lindgren@tds.net>
parents:
1169
diff
changeset
|
754 asf_st->packet_pos= asf->packet_pos; |
a2093254960a
Sanity check to work around a WMA decoding error.
John Lindgren <john.lindgren@tds.net>
parents:
1169
diff
changeset
|
755 //printf("new packet: stream:%d key:%d packet_key:%d audio:%d size:%d\n", |
878 | 756 //asf->stream_index, asf->packet_key_frame, asf_st->pkt.flags & PKT_FLAG_KEY, |
757 //s->streams[asf->stream_index]->codec.codec_type == CODEC_TYPE_AUDIO, asf->packet_obj_size); | |
3123
a2093254960a
Sanity check to work around a WMA decoding error.
John Lindgren <john.lindgren@tds.net>
parents:
1169
diff
changeset
|
758 if (s->streams[asf->stream_index]->codec.codec_type == CODEC_TYPE_AUDIO) |
878 | 759 asf->packet_key_frame = 1; |
760 if (asf->packet_key_frame) | |
761 asf_st->pkt.flags |= PKT_FLAG_KEY; | |
762 } | |
763 | |
764 /* read data */ | |
765 //printf("READ PACKET s:%d os:%d o:%d,%d l:%d DATA:%p\n", | |
766 // asf->packet_size, asf_st->pkt.size, asf->packet_frag_offset, | |
767 // asf_st->frag_offset, asf->packet_frag_size, asf_st->pkt.data); | |
768 asf->packet_size_left -= asf->packet_frag_size; | |
769 if (asf->packet_size_left < 0) | |
770 continue; | |
771 get_buffer(pb, asf_st->pkt.data + asf->packet_frag_offset, | |
772 asf->packet_frag_size); | |
773 asf_st->frag_offset += asf->packet_frag_size; | |
774 /* test if whole packet is read */ | |
775 if (asf_st->frag_offset == asf_st->pkt.size) { | |
776 /* return packet */ | |
777 if (asf_st->ds_span > 1) { | |
778 /* packet descrambling */ | |
779 unsigned char* newdata = av_malloc(asf_st->pkt.size); | |
780 if (newdata) { | |
781 int offset = 0; | |
782 while (offset < asf_st->pkt.size) { | |
783 int off = offset / asf_st->ds_chunk_size; | |
784 int row = off / asf_st->ds_span; | |
785 int col = off % asf_st->ds_span; | |
786 int idx = row + col * asf_st->ds_packet_size / asf_st->ds_chunk_size; | |
787 //printf("off:%d row:%d col:%d idx:%d\n", off, row, col, idx); | |
788 memcpy(newdata + offset, | |
789 asf_st->pkt.data + idx * asf_st->ds_chunk_size, | |
790 asf_st->ds_chunk_size); | |
791 offset += asf_st->ds_chunk_size; | |
792 } | |
793 free(asf_st->pkt.data); | |
794 asf_st->pkt.data = newdata; | |
795 } | |
796 } | |
797 asf_st->frag_offset = 0; | |
798 memcpy(pkt, &asf_st->pkt, sizeof(AVPacket)); | |
799 //printf("packet %d %d\n", asf_st->pkt.size, asf->packet_frag_size); | |
800 asf_st->pkt.size = 0; | |
801 asf_st->pkt.data = 0; | |
802 break; // packet completed | |
803 } | |
804 } | |
805 return 0; | |
806 } | |
807 | |
808 static int asf_read_close(AVFormatContext *s) | |
809 { | |
810 int i; | |
811 | |
812 for(i=0;i<s->nb_streams;i++) { | |
813 AVStream *st = s->streams[i]; | |
814 free(st->priv_data); | |
815 free(st->codec.extradata); | |
816 free(st->codec.palctrl); | |
817 } | |
818 return 0; | |
819 } | |
820 | |
821 // Added to support seeking after packets have been read | |
822 // If information is not reset, read_packet fails due to | |
823 // leftover information from previous reads | |
824 static void asf_reset_header(AVFormatContext *s) | |
825 { | |
826 ASFContext *asf = s->priv_data; | |
827 ASFStream *asf_st; | |
828 int i; | |
829 | |
830 asf->packet_nb_frames = 0; | |
831 asf->packet_timestamp_start = -1; | |
832 asf->packet_timestamp_end = -1; | |
833 asf->packet_size_left = 0; | |
834 asf->packet_segments = 0; | |
835 asf->packet_flags = 0; | |
836 asf->packet_property = 0; | |
837 asf->packet_timestamp = 0; | |
838 asf->packet_segsizetype = 0; | |
839 asf->packet_segments = 0; | |
840 asf->packet_seq = 0; | |
841 asf->packet_replic_size = 0; | |
842 asf->packet_key_frame = 0; | |
843 asf->packet_padsize = 0; | |
844 asf->packet_frag_offset = 0; | |
845 asf->packet_frag_size = 0; | |
846 asf->packet_frag_timestamp = 0; | |
847 asf->packet_multi_size = 0; | |
848 asf->packet_obj_size = 0; | |
849 asf->packet_time_delta = 0; | |
850 asf->packet_time_start = 0; | |
3123
a2093254960a
Sanity check to work around a WMA decoding error.
John Lindgren <john.lindgren@tds.net>
parents:
1169
diff
changeset
|
851 |
878 | 852 for(i=0; i<s->nb_streams; i++){ |
853 asf_st= s->streams[i]->priv_data; | |
854 av_free_packet(&asf_st->pkt); | |
855 asf_st->frag_offset=0; | |
856 asf_st->seq=0; | |
857 } | |
858 asf->asf_st= NULL; | |
859 } | |
860 | |
861 static int64_t asf_read_pts(AVFormatContext *s, int64_t *ppos, int stream_index) | |
862 { | |
863 ASFContext *asf = s->priv_data; | |
864 AVPacket pkt1, *pkt = &pkt1; | |
865 ASFStream *asf_st; | |
866 int64_t pts; | |
867 int64_t pos= *ppos; | |
868 int i; | |
869 int64_t start_pos[s->nb_streams]; | |
3123
a2093254960a
Sanity check to work around a WMA decoding error.
John Lindgren <john.lindgren@tds.net>
parents:
1169
diff
changeset
|
870 |
878 | 871 for(i=0; i<s->nb_streams; i++){ |
872 start_pos[i]= pos; | |
873 } | |
874 | |
875 //printf("asf_read_pts\n"); | |
876 url_fseek(&s->pb, pos*asf->packet_size + s->data_offset, SEEK_SET); | |
877 asf_reset_header(s); | |
878 for(;;){ | |
879 if (av_read_frame(s, pkt) < 0){ | |
880 printf("seek failed\n"); | |
881 return AV_NOPTS_VALUE; | |
882 } | |
883 pts= pkt->pts; | |
884 | |
885 av_free_packet(pkt); | |
886 if(pkt->flags&PKT_FLAG_KEY){ | |
887 i= pkt->stream_index; | |
888 | |
889 asf_st= s->streams[i]->priv_data; | |
890 | |
891 assert((asf_st->packet_pos - s->data_offset) % asf->packet_size == 0); | |
892 pos= (asf_st->packet_pos - s->data_offset) / asf->packet_size; | |
893 | |
894 av_add_index_entry(s->streams[i], pos, pts, pos - start_pos[i] + 1, AVINDEX_KEYFRAME); | |
895 start_pos[i]= pos + 1; | |
3123
a2093254960a
Sanity check to work around a WMA decoding error.
John Lindgren <john.lindgren@tds.net>
parents:
1169
diff
changeset
|
896 |
878 | 897 if(pkt->stream_index == stream_index) |
898 break; | |
899 } | |
900 } | |
901 | |
902 *ppos= pos; | |
903 //printf("found keyframe at %Ld stream %d stamp:%Ld\n", *ppos, stream_index, pts); | |
904 | |
905 return pts; | |
906 } | |
907 | |
908 static int asf_read_seek(AVFormatContext *s, int stream_index, int64_t pts) | |
909 { | |
910 ASFContext *asf = s->priv_data; | |
911 AVStream *st; | |
912 int64_t pos; | |
913 int64_t pos_min, pos_max, pts_min, pts_max, cur_pts, pos_limit; | |
914 int no_change; | |
3123
a2093254960a
Sanity check to work around a WMA decoding error.
John Lindgren <john.lindgren@tds.net>
parents:
1169
diff
changeset
|
915 |
878 | 916 if (stream_index == -1) |
917 stream_index= av_find_default_stream_index(s); | |
3123
a2093254960a
Sanity check to work around a WMA decoding error.
John Lindgren <john.lindgren@tds.net>
parents:
1169
diff
changeset
|
918 |
878 | 919 if (asf->packet_size <= 0) |
920 return -1; | |
921 | |
922 pts_max= | |
923 pts_min= AV_NOPTS_VALUE; | |
924 pos_max= pos_limit= -1; // gcc thinks its uninitalized | |
925 | |
926 st= s->streams[stream_index]; | |
927 if(st->index_entries){ | |
928 AVIndexEntry *e; | |
929 int index; | |
930 | |
931 index= av_index_search_timestamp(st, pts); | |
932 e= &st->index_entries[index]; | |
933 if(e->timestamp <= pts){ | |
934 pos_min= e->pos; | |
935 pts_min= e->timestamp; | |
936 }else{ | |
937 assert(index==0); | |
938 } | |
939 index++; | |
940 if(index < st->nb_index_entries){ | |
941 e= &st->index_entries[index]; | |
942 assert(e->timestamp >= pts); | |
943 pos_max= e->pos; | |
944 pts_max= e->timestamp; | |
945 pos_limit= pos_max - e->min_distance; | |
946 } | |
947 } | |
948 | |
949 if(pts_min == (int64_t)AV_NOPTS_VALUE){ | |
950 pos_min = 0; | |
951 pts_min = asf_read_pts(s, &pos_min, stream_index); | |
952 if (pts_min == (int64_t)AV_NOPTS_VALUE) return -1; | |
953 } | |
954 if(pts_max == (int64_t)AV_NOPTS_VALUE){ | |
955 pos_max = (url_filesize(url_fileno(&s->pb)) - 1 - s->data_offset) / asf->packet_size; //FIXME wrong | |
956 pts_max = s->duration; //FIXME wrong | |
957 pos_limit= pos_max; | |
3123
a2093254960a
Sanity check to work around a WMA decoding error.
John Lindgren <john.lindgren@tds.net>
parents:
1169
diff
changeset
|
958 } |
878 | 959 |
960 no_change=0; | |
961 while (pos_min < pos_limit) { | |
962 int64_t start_pos; | |
963 assert(pos_limit <= pos_max); | |
964 | |
965 if(no_change==0){ | |
966 int64_t approximate_keyframe_distance= pos_max - pos_limit; | |
967 // interpolate position (better than dichotomy) | |
968 pos = (int64_t)((double)(pos_max - pos_min) * | |
969 (double)(pts - pts_min) / | |
970 (double)(pts_max - pts_min)) + pos_min - approximate_keyframe_distance; | |
971 }else if(no_change==1){ | |
972 // bisection, if interpolation failed to change min or max pos last time | |
973 pos = (pos_min + pos_limit)>>1; | |
974 }else{ | |
975 // linear search if bisection failed, can only happen if there are very few or no keyframes between min/max | |
976 pos=pos_min; | |
977 } | |
978 if(pos <= pos_min) | |
979 pos= pos_min + 1; | |
980 else if(pos > pos_limit) | |
981 pos= pos_limit; | |
982 start_pos= pos; | |
983 | |
3123
a2093254960a
Sanity check to work around a WMA decoding error.
John Lindgren <john.lindgren@tds.net>
parents:
1169
diff
changeset
|
984 // read the next timestamp |
a2093254960a
Sanity check to work around a WMA decoding error.
John Lindgren <john.lindgren@tds.net>
parents:
1169
diff
changeset
|
985 cur_pts = asf_read_pts(s, &pos, stream_index); |
878 | 986 if(pos == pos_max) |
987 no_change++; | |
988 else | |
989 no_change=0; | |
990 | |
991 assert (cur_pts != AV_NOPTS_VALUE); | |
992 if (pts < cur_pts) { | |
993 pos_limit = start_pos - 1; | |
994 pos_max = pos; | |
995 pts_max = cur_pts; | |
996 } else { | |
997 pos_min = pos; | |
998 pts_min = cur_pts; | |
999 /* check if we are lucky */ | |
1000 if (pts == cur_pts) | |
1001 break; | |
1002 } | |
1003 } | |
1004 pos = pos_min; | |
1005 url_fseek(&s->pb, pos*asf->packet_size + s->data_offset, SEEK_SET); | |
1006 asf_reset_header(s); | |
1007 return 0; | |
1008 } | |
1009 | |
1010 static AVInputFormat asf_iformat = { | |
1011 "asf", | |
1012 "asf format", | |
1013 sizeof(ASFContext), | |
1014 asf_probe, | |
1015 asf_read_header, | |
1016 asf_read_packet, | |
1017 asf_read_close, | |
1018 asf_read_seek, | |
1019 0, NULL, 0, NULL, NULL, NULL | |
1020 }; | |
1021 | |
1022 int asf_init(void) | |
1023 { | |
1024 av_register_input_format(&asf_iformat); | |
3123
a2093254960a
Sanity check to work around a WMA decoding error.
John Lindgren <john.lindgren@tds.net>
parents:
1169
diff
changeset
|
1025 |
878 | 1026 return 0; |
1027 } |