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