2 * ASF compatible demuxer
3 * Copyright (c) 2000, 2001 Fabrice Bellard
5 * This file is part of Libav.
7 * Libav is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * Libav is distributed in the hope that it will be useful,
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.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "libavutil/common.h"
25 #include "libavutil/avstring.h"
26 #include "libavcodec/mpegaudio.h"
28 #include "avio_internal.h"
32 #include "avlanguage.h"
34 void ff_mms_set_stream_selection(URLContext *h, AVFormatContext *format);
37 int asfid2avid[128]; ///< conversion table from asf ID 2 AVStream ID
38 ASFStream streams[128]; ///< it's max number and it's not that big
39 uint32_t stream_bitrates[128]; ///< max number of streams, bitrate for each (for streaming)
41 char stream_languages[128][6]; ///< max number of streams, language for each (RFC1766, e.g. en-US)
42 /* non streamed additonnal info */
45 /* only for reading */
46 uint64_t data_offset; ///< beginning of the first data packet
47 uint64_t data_object_offset; ///< data object offset (excl. GUID & size)
48 uint64_t data_object_size; ///< size of the data object
56 int packet_segsizetype;
59 int packet_replic_size;
62 unsigned int packet_frag_offset;
63 unsigned int packet_frag_size;
64 int64_t packet_frag_timestamp;
65 int packet_multi_size;
67 int packet_time_delta;
68 int packet_time_start;
73 ASFStream* asf_st; ///< currently decoded stream
79 #define ASF_MAX_STREAMS 127
80 #define FRAME_HEADER_SIZE 17
81 // Fix Me! FRAME_HEADER_SIZE may be different.
83 static const ff_asf_guid index_guid = {
84 0x90, 0x08, 0x00, 0x33, 0xb1, 0xe5, 0xcf, 0x11, 0x89, 0xf4, 0x00, 0xa0, 0xc9, 0x03, 0x49, 0xcb
87 static const ff_asf_guid stream_bitrate_guid = { /* (http://get.to/sdp) */
88 0xce, 0x75, 0xf8, 0x7b, 0x8d, 0x46, 0xd1, 0x11, 0x8d, 0x82, 0x00, 0x60, 0x97, 0xc9, 0xa2, 0xb2
90 /**********************************/
94 #define PRINT_IF_GUID(g,cmp) \
95 if (!ff_guidcmp(g, &cmp)) \
96 av_dlog(NULL, "(GUID: %s) ", #cmp)
98 static void print_guid(const ff_asf_guid *g)
101 PRINT_IF_GUID(g, ff_asf_header);
102 else PRINT_IF_GUID(g, ff_asf_file_header);
103 else PRINT_IF_GUID(g, ff_asf_stream_header);
104 else PRINT_IF_GUID(g, ff_asf_audio_stream);
105 else PRINT_IF_GUID(g, ff_asf_audio_conceal_none);
106 else PRINT_IF_GUID(g, ff_asf_video_stream);
107 else PRINT_IF_GUID(g, ff_asf_video_conceal_none);
108 else PRINT_IF_GUID(g, ff_asf_command_stream);
109 else PRINT_IF_GUID(g, ff_asf_comment_header);
110 else PRINT_IF_GUID(g, ff_asf_codec_comment_header);
111 else PRINT_IF_GUID(g, ff_asf_codec_comment1_header);
112 else PRINT_IF_GUID(g, ff_asf_data_header);
113 else PRINT_IF_GUID(g, index_guid);
114 else PRINT_IF_GUID(g, ff_asf_head1_guid);
115 else PRINT_IF_GUID(g, ff_asf_head2_guid);
116 else PRINT_IF_GUID(g, ff_asf_my_guid);
117 else PRINT_IF_GUID(g, ff_asf_ext_stream_header);
118 else PRINT_IF_GUID(g, ff_asf_extended_content_header);
119 else PRINT_IF_GUID(g, ff_asf_ext_stream_embed_stream_header);
120 else PRINT_IF_GUID(g, ff_asf_ext_stream_audio_stream);
121 else PRINT_IF_GUID(g, ff_asf_metadata_header);
122 else PRINT_IF_GUID(g, ff_asf_marker_header);
123 else PRINT_IF_GUID(g, stream_bitrate_guid);
124 else PRINT_IF_GUID(g, ff_asf_language_guid);
126 av_dlog(NULL, "(GUID: unknown) ");
128 av_dlog(NULL, " 0x%02x,", (*g)[i]);
129 av_dlog(NULL, "}\n");
133 #define print_guid(g)
136 void ff_get_guid(AVIOContext *s, ff_asf_guid *g)
138 assert(sizeof(*g) == 16);
139 avio_read(s, *g, sizeof(*g));
142 static int asf_probe(AVProbeData *pd)
144 /* check file header */
145 if (!ff_guidcmp(pd->buf, &ff_asf_header))
146 return AVPROBE_SCORE_MAX;
151 static int get_value(AVIOContext *pb, int type){
153 case 2: return avio_rl32(pb);
154 case 3: return avio_rl32(pb);
155 case 4: return avio_rl64(pb);
156 case 5: return avio_rl16(pb);
157 default:return INT_MIN;
161 static void get_tag(AVFormatContext *s, const char *key, int type, int len)
164 int64_t off = avio_tell(s->pb);
166 if ((unsigned)len >= (UINT_MAX - 1)/2)
169 value = av_malloc(2*len+1);
173 if (type == 0) { // UTF16-LE
174 avio_get_str16le(s->pb, len, value, 2*len + 1);
175 } else if (type > 1 && type <= 5) { // boolean or DWORD or QWORD or WORD
176 uint64_t num = get_value(s->pb, type);
177 snprintf(value, len, "%"PRIu64, num);
179 av_log(s, AV_LOG_DEBUG, "Unsupported value type %d in tag %s.\n", type, key);
183 av_metadata_set2(&s->metadata, key, value, 0);
186 avio_seek(s->pb, off + len, SEEK_SET);
189 static int asf_read_file_properties(AVFormatContext *s, int64_t size)
191 ASFContext *asf = s->priv_data;
192 AVIOContext *pb = s->pb;
194 ff_get_guid(pb, &asf->hdr.guid);
195 asf->hdr.file_size = avio_rl64(pb);
196 asf->hdr.create_time = avio_rl64(pb);
197 avio_rl64(pb); /* number of packets */
198 asf->hdr.play_time = avio_rl64(pb);
199 asf->hdr.send_time = avio_rl64(pb);
200 asf->hdr.preroll = avio_rl32(pb);
201 asf->hdr.ignore = avio_rl32(pb);
202 asf->hdr.flags = avio_rl32(pb);
203 asf->hdr.min_pktsize = avio_rl32(pb);
204 asf->hdr.max_pktsize = avio_rl32(pb);
205 asf->hdr.max_bitrate = avio_rl32(pb);
206 s->packet_size = asf->hdr.max_pktsize;
211 static int asf_read_stream_properties(AVFormatContext *s, int64_t size)
213 ASFContext *asf = s->priv_data;
214 AVIOContext *pb = s->pb;
218 enum AVMediaType type;
219 int type_specific_size, sizeX;
222 int64_t pos1, pos2, start_time;
223 int test_for_ext_stream_audio, is_dvr_ms_audio=0;
225 if (s->nb_streams == ASF_MAX_STREAMS) {
226 av_log(s, AV_LOG_ERROR, "too many streams\n");
227 return AVERROR(EINVAL);
230 pos1 = avio_tell(pb);
232 st = av_new_stream(s, 0);
234 return AVERROR(ENOMEM);
235 av_set_pts_info(st, 32, 1, 1000); /* 32 bit pts in ms */
236 asf_st = av_mallocz(sizeof(ASFStream));
238 return AVERROR(ENOMEM);
239 st->priv_data = asf_st;
241 start_time = asf->hdr.preroll;
243 asf_st->stream_language_index = 128; // invalid stream index means no language info
245 if(!(asf->hdr.flags & 0x01)) { // if we aren't streaming...
246 st->duration = asf->hdr.play_time /
247 (10000000 / 1000) - start_time;
251 test_for_ext_stream_audio = 0;
252 if (!ff_guidcmp(&g, &ff_asf_audio_stream)) {
253 type = AVMEDIA_TYPE_AUDIO;
254 } else if (!ff_guidcmp(&g, &ff_asf_video_stream)) {
255 type = AVMEDIA_TYPE_VIDEO;
256 } else if (!ff_guidcmp(&g, &ff_asf_jfif_media)) {
257 type = AVMEDIA_TYPE_VIDEO;
258 st->codec->codec_id = CODEC_ID_MJPEG;
259 } else if (!ff_guidcmp(&g, &ff_asf_command_stream)) {
260 type = AVMEDIA_TYPE_DATA;
261 } else if (!ff_guidcmp(&g, &ff_asf_ext_stream_embed_stream_header)) {
262 test_for_ext_stream_audio = 1;
263 type = AVMEDIA_TYPE_UNKNOWN;
268 total_size = avio_rl64(pb);
269 type_specific_size = avio_rl32(pb);
271 st->id = avio_rl16(pb) & 0x7f; /* stream id */
272 // mapping of asf ID to AV stream ID;
273 asf->asfid2avid[st->id] = s->nb_streams - 1;
277 if (test_for_ext_stream_audio) {
279 if (!ff_guidcmp(&g, &ff_asf_ext_stream_audio_stream)) {
280 type = AVMEDIA_TYPE_AUDIO;
291 st->codec->codec_type = type;
292 if (type == AVMEDIA_TYPE_AUDIO) {
293 ff_get_wav_header(pb, st->codec, type_specific_size);
294 if (is_dvr_ms_audio) {
295 // codec_id and codec_tag are unreliable in dvr_ms
296 // files. Set them later by probing stream.
297 st->codec->codec_id = CODEC_ID_PROBE;
298 st->codec->codec_tag = 0;
300 if (st->codec->codec_id == CODEC_ID_AAC) {
301 st->need_parsing = AVSTREAM_PARSE_NONE;
303 st->need_parsing = AVSTREAM_PARSE_FULL;
305 /* We have to init the frame size at some point .... */
306 pos2 = avio_tell(pb);
307 if (size >= (pos2 + 8 - pos1 + 24)) {
308 asf_st->ds_span = avio_r8(pb);
309 asf_st->ds_packet_size = avio_rl16(pb);
310 asf_st->ds_chunk_size = avio_rl16(pb);
311 avio_rl16(pb); //ds_data_size
312 avio_r8(pb); //ds_silence_data
314 //printf("Descrambling: ps:%d cs:%d ds:%d s:%d sd:%d\n",
315 // asf_st->ds_packet_size, asf_st->ds_chunk_size,
316 // asf_st->ds_data_size, asf_st->ds_span, asf_st->ds_silence_data);
317 if (asf_st->ds_span > 1) {
318 if (!asf_st->ds_chunk_size
319 || (asf_st->ds_packet_size/asf_st->ds_chunk_size <= 1)
320 || asf_st->ds_packet_size % asf_st->ds_chunk_size)
321 asf_st->ds_span = 0; // disable descrambling
323 switch (st->codec->codec_id) {
325 st->codec->frame_size = MPA_FRAME_SIZE;
327 case CODEC_ID_PCM_S16LE:
328 case CODEC_ID_PCM_S16BE:
329 case CODEC_ID_PCM_U16LE:
330 case CODEC_ID_PCM_U16BE:
331 case CODEC_ID_PCM_S8:
332 case CODEC_ID_PCM_U8:
333 case CODEC_ID_PCM_ALAW:
334 case CODEC_ID_PCM_MULAW:
335 st->codec->frame_size = 1;
338 /* This is probably wrong, but it prevents a crash later */
339 st->codec->frame_size = 1;
342 } else if (type == AVMEDIA_TYPE_VIDEO &&
343 size - (avio_tell(pb) - pos1 + 24) >= 51) {
347 avio_rl16(pb); /* size */
348 sizeX= avio_rl32(pb); /* size */
349 st->codec->width = avio_rl32(pb);
350 st->codec->height = avio_rl32(pb);
351 /* not available for asf */
352 avio_rl16(pb); /* panes */
353 st->codec->bits_per_coded_sample = avio_rl16(pb); /* depth */
354 tag1 = avio_rl32(pb);
356 // av_log(s, AV_LOG_DEBUG, "size:%d tsize:%d sizeX:%d\n", size, total_size, sizeX);
358 st->codec->extradata_size = sizeX - 40;
359 st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
360 avio_read(pb, st->codec->extradata, st->codec->extradata_size);
363 /* Extract palette from extradata if bpp <= 8 */
364 /* This code assumes that extradata contains only palette */
365 /* This is true for all paletted codecs implemented in ffmpeg */
366 if (st->codec->extradata_size && (st->codec->bits_per_coded_sample <= 8)) {
368 st->codec->palctrl = av_mallocz(sizeof(AVPaletteControl));
370 for (i = 0; i < FFMIN(st->codec->extradata_size, AVPALETTE_SIZE)/4; i++)
371 st->codec->palctrl->palette[i] = av_bswap32(((uint32_t*)st->codec->extradata)[i]);
373 memcpy(st->codec->palctrl->palette, st->codec->extradata,
374 FFMIN(st->codec->extradata_size, AVPALETTE_SIZE));
376 st->codec->palctrl->palette_changed = 1;
379 st->codec->codec_tag = tag1;
380 st->codec->codec_id = ff_codec_get_id(ff_codec_bmp_tags, tag1);
381 if(tag1 == MKTAG('D', 'V', 'R', ' ')){
382 st->need_parsing = AVSTREAM_PARSE_FULL;
383 // issue658 containse wrong w/h and MS even puts a fake seq header with wrong w/h in extradata while a correct one is in te stream. maximum lameness
385 st->codec->height = 0;
386 av_freep(&st->codec->extradata);
387 st->codec->extradata_size=0;
389 if(st->codec->codec_id == CODEC_ID_H264)
390 st->need_parsing = AVSTREAM_PARSE_FULL_ONCE;
392 pos2 = avio_tell(pb);
393 avio_skip(pb, size - (pos2 - pos1 + 24));
398 static int asf_read_ext_stream_properties(AVFormatContext *s, int64_t size)
400 ASFContext *asf = s->priv_data;
401 AVIOContext *pb = s->pb;
403 int ext_len, payload_ext_ct, stream_ct, i;
404 uint32_t ext_d, leak_rate, stream_num;
405 unsigned int stream_languageid_index;
407 avio_rl64(pb); // starttime
408 avio_rl64(pb); // endtime
409 leak_rate = avio_rl32(pb); // leak-datarate
410 avio_rl32(pb); // bucket-datasize
411 avio_rl32(pb); // init-bucket-fullness
412 avio_rl32(pb); // alt-leak-datarate
413 avio_rl32(pb); // alt-bucket-datasize
414 avio_rl32(pb); // alt-init-bucket-fullness
415 avio_rl32(pb); // max-object-size
416 avio_rl32(pb); // flags (reliable,seekable,no_cleanpoints?,resend-live-cleanpoints, rest of bits reserved)
417 stream_num = avio_rl16(pb); // stream-num
419 stream_languageid_index = avio_rl16(pb); // stream-language-id-index
420 if (stream_num < 128)
421 asf->streams[stream_num].stream_language_index = stream_languageid_index;
423 avio_rl64(pb); // avg frametime in 100ns units
424 stream_ct = avio_rl16(pb); //stream-name-count
425 payload_ext_ct = avio_rl16(pb); //payload-extension-system-count
427 if (stream_num < 128)
428 asf->stream_bitrates[stream_num] = leak_rate;
430 for (i=0; i<stream_ct; i++){
432 ext_len = avio_rl16(pb);
433 avio_skip(pb, ext_len);
436 for (i=0; i<payload_ext_ct; i++){
439 ext_len=avio_rl32(pb);
440 avio_skip(pb, ext_len);
446 static int asf_read_content_desc(AVFormatContext *s, int64_t size)
448 AVIOContext *pb = s->pb;
449 int len1, len2, len3, len4, len5;
451 len1 = avio_rl16(pb);
452 len2 = avio_rl16(pb);
453 len3 = avio_rl16(pb);
454 len4 = avio_rl16(pb);
455 len5 = avio_rl16(pb);
456 get_tag(s, "title" , 0, len1);
457 get_tag(s, "author" , 0, len2);
458 get_tag(s, "copyright", 0, len3);
459 get_tag(s, "comment" , 0, len4);
465 static int asf_read_ext_content_desc(AVFormatContext *s, int64_t size)
467 AVIOContext *pb = s->pb;
468 ASFContext *asf = s->priv_data;
469 int desc_count, i, ret;
471 desc_count = avio_rl16(pb);
472 for(i=0;i<desc_count;i++) {
473 int name_len,value_type,value_len;
476 name_len = avio_rl16(pb);
477 if (name_len%2) // must be even, broken lavf versions wrote len-1
479 if ((ret = avio_get_str16le(pb, name_len, name, sizeof(name))) < name_len)
480 avio_skip(pb, name_len - ret);
481 value_type = avio_rl16(pb);
482 value_len = avio_rl16(pb);
483 if (!value_type && value_len%2)
486 * My sample has that stream set to 0 maybe that mean the container.
487 * Asf stream count start at 1. I am using 0 to the container value since it's unused
489 if (!strcmp(name, "AspectRatioX")){
490 asf->dar[0].num= get_value(s->pb, value_type);
491 } else if(!strcmp(name, "AspectRatioY")){
492 asf->dar[0].den= get_value(s->pb, value_type);
494 get_tag(s, name, value_type, value_len);
500 static int asf_read_language_list(AVFormatContext *s, int64_t size)
502 AVIOContext *pb = s->pb;
503 ASFContext *asf = s->priv_data;
505 int stream_count = avio_rl16(pb);
506 for(j = 0; j < stream_count; j++) {
508 unsigned int lang_len = avio_r8(pb);
509 if ((ret = avio_get_str16le(pb, lang_len, lang, sizeof(lang))) < lang_len)
510 avio_skip(pb, lang_len - ret);
512 av_strlcpy(asf->stream_languages[j], lang, sizeof(*asf->stream_languages));
518 static int asf_read_metadata(AVFormatContext *s, int64_t size)
520 AVIOContext *pb = s->pb;
521 ASFContext *asf = s->priv_data;
522 int n, stream_num, name_len, value_len, value_type, value_num;
529 avio_rl16(pb); //lang_list_index
530 stream_num= avio_rl16(pb);
531 name_len= avio_rl16(pb);
532 value_type= avio_rl16(pb);
533 value_len= avio_rl32(pb);
535 if ((ret = avio_get_str16le(pb, name_len, name, sizeof(name))) < name_len)
536 avio_skip(pb, name_len - ret);
537 //av_log(s, AV_LOG_ERROR, "%d %d %d %d %d <%s>\n", i, stream_num, name_len, value_type, value_len, name);
538 value_num= avio_rl16(pb);//we should use get_value() here but it does not work 2 is le16 here but le32 elsewhere
539 avio_skip(pb, value_len - 2);
542 if (!strcmp(name, "AspectRatioX")) asf->dar[stream_num].num= value_num;
543 else if(!strcmp(name, "AspectRatioY")) asf->dar[stream_num].den= value_num;
550 static int asf_read_marker(AVFormatContext *s, int64_t size)
552 AVIOContext *pb = s->pb;
553 int i, count, name_len, ret;
556 avio_rl64(pb); // reserved 16 bytes
557 avio_rl64(pb); // ...
558 count = avio_rl32(pb); // markers count
559 avio_rl16(pb); // reserved 2 bytes
560 name_len = avio_rl16(pb); // name length
561 for(i=0;i<name_len;i++){
562 avio_r8(pb); // skip the name
565 for(i=0;i<count;i++){
569 avio_rl64(pb); // offset, 8 bytes
570 pres_time = avio_rl64(pb); // presentation time
571 avio_rl16(pb); // entry length
572 avio_rl32(pb); // send time
573 avio_rl32(pb); // flags
574 name_len = avio_rl32(pb); // name length
575 if ((ret = avio_get_str16le(pb, name_len * 2, name, sizeof(name))) < name_len)
576 avio_skip(pb, name_len - ret);
577 ff_new_chapter(s, i, (AVRational){1, 10000000}, pres_time, AV_NOPTS_VALUE, name );
583 static int asf_read_header(AVFormatContext *s, AVFormatParameters *ap)
585 ASFContext *asf = s->priv_data;
587 AVIOContext *pb = s->pb;
592 if (ff_guidcmp(&g, &ff_asf_header))
598 memset(&asf->asfid2avid, -1, sizeof(asf->asfid2avid));
600 uint64_t gpos= avio_tell(pb);
602 gsize = avio_rl64(pb);
603 av_dlog(s, "%08"PRIx64": ", gpos);
605 av_dlog(s, " size=0x%"PRIx64"\n", gsize);
606 if (!ff_guidcmp(&g, &ff_asf_data_header)) {
607 asf->data_object_offset = avio_tell(pb);
608 // if not streaming, gsize is not unlimited (how?), and there is enough space in the file..
609 if (!(asf->hdr.flags & 0x01) && gsize >= 100) {
610 asf->data_object_size = gsize - 24;
612 asf->data_object_size = (uint64_t)-1;
618 if (!ff_guidcmp(&g, &ff_asf_file_header)) {
619 asf_read_file_properties(s, gsize);
620 } else if (!ff_guidcmp(&g, &ff_asf_stream_header)) {
621 asf_read_stream_properties(s, gsize);
622 } else if (!ff_guidcmp(&g, &ff_asf_comment_header)) {
623 asf_read_content_desc(s, gsize);
624 } else if (!ff_guidcmp(&g, &ff_asf_language_guid)) {
625 asf_read_language_list(s, gsize);
626 } else if (!ff_guidcmp(&g, &ff_asf_extended_content_header)) {
627 asf_read_ext_content_desc(s, gsize);
628 } else if (!ff_guidcmp(&g, &ff_asf_metadata_header)) {
629 asf_read_metadata(s, gsize);
630 } else if (!ff_guidcmp(&g, &ff_asf_ext_stream_header)) {
631 asf_read_ext_stream_properties(s, gsize);
633 // there could be a optional stream properties object to follow
634 // if so the next iteration will pick it up
636 } else if (!ff_guidcmp(&g, &ff_asf_head1_guid)) {
642 } else if (!ff_guidcmp(&g, &ff_asf_marker_header)) {
643 asf_read_marker(s, gsize);
644 } else if (pb->eof_reached) {
648 if (!ff_guidcmp(&g, &ff_asf_content_encryption)) {
649 av_log(s, AV_LOG_WARNING, "DRM protected stream detected, decoding will likely fail!\n");
650 } else if (!ff_guidcmp(&g, &ff_asf_ext_content_encryption)) {
651 av_log(s, AV_LOG_WARNING, "Ext DRM protected stream detected, decoding will likely fail!\n");
652 } else if (!ff_guidcmp(&g, &ff_asf_digital_signature)) {
653 av_log(s, AV_LOG_WARNING, "Digital signature detected, decoding will likely fail!\n");
657 if(avio_tell(pb) != gpos + gsize)
658 av_log(s, AV_LOG_DEBUG, "gpos mismatch our pos=%"PRIu64", end=%"PRIu64"\n", avio_tell(pb)-gpos, gsize);
659 avio_seek(pb, gpos + gsize, SEEK_SET);
667 asf->data_offset = avio_tell(pb);
668 asf->packet_size_left = 0;
671 for(i=0; i<128; i++){
672 int stream_num= asf->asfid2avid[i];
674 AVStream *st = s->streams[stream_num];
675 if (!st->codec->bit_rate)
676 st->codec->bit_rate = asf->stream_bitrates[i];
677 if (asf->dar[i].num > 0 && asf->dar[i].den > 0){
678 av_reduce(&st->sample_aspect_ratio.num,
679 &st->sample_aspect_ratio.den,
680 asf->dar[i].num, asf->dar[i].den, INT_MAX);
681 } else if ((asf->dar[0].num > 0) && (asf->dar[0].den > 0) && (st->codec->codec_type==AVMEDIA_TYPE_VIDEO)) // Use ASF container value if the stream doesn't AR set.
682 av_reduce(&st->sample_aspect_ratio.num,
683 &st->sample_aspect_ratio.den,
684 asf->dar[0].num, asf->dar[0].den, INT_MAX);
686 //av_log(s, AV_LOG_INFO, "i=%d, st->codec->codec_type:%d, dar %d:%d sar=%d:%d\n", i, st->codec->codec_type, dar[i].num, dar[i].den, st->sample_aspect_ratio.num, st->sample_aspect_ratio.den);
688 // copy and convert language codes to the frontend
689 if (asf->streams[i].stream_language_index < 128) {
690 const char *rfc1766 = asf->stream_languages[asf->streams[i].stream_language_index];
691 if (rfc1766 && strlen(rfc1766) > 1) {
692 const char primary_tag[3] = { rfc1766[0], rfc1766[1], '\0' }; // ignore country code if any
693 const char *iso6392 = av_convert_lang_to(primary_tag, AV_LANG_ISO639_2_BIBL);
695 av_metadata_set2(&st->metadata, "language", iso6392, 0);
701 ff_metadata_conv(&s->metadata, NULL, ff_asf_metadata_conv);
706 #define DO_2BITS(bits, var, defval) \
709 case 3: var = avio_rl32(pb); rsize += 4; break; \
710 case 2: var = avio_rl16(pb); rsize += 2; break; \
711 case 1: var = avio_r8(pb); rsize++; break; \
712 default: var = defval; break; \
716 * Load a single ASF packet into the demuxer.
717 * @param s demux context
718 * @param pb context to read data from
719 * @return 0 on success, <0 on error
721 static int ff_asf_get_packet(AVFormatContext *s, AVIOContext *pb)
723 ASFContext *asf = s->priv_data;
724 uint32_t packet_length, padsize;
728 // if we do not know packet size, allow skipping up to 32 kB
730 if (s->packet_size > 0)
731 off= (avio_tell(pb) - s->data_offset) % s->packet_size + 3;
737 if(c == 0x82 && !d && !e)
743 * This code allows handling of -EAGAIN at packet boundaries (i.e.
744 * if the packet sync code above triggers -EAGAIN). This does not
745 * imply complete -EAGAIN handling support at random positions in
748 if (pb->error == AVERROR(EAGAIN))
749 return AVERROR(EAGAIN);
750 if (!pb->eof_reached)
751 av_log(s, AV_LOG_ERROR, "ff asf bad header %x at:%"PRId64"\n", c, avio_tell(pb));
753 if ((c & 0x8f) == 0x82) {
755 if (!pb->eof_reached)
756 av_log(s, AV_LOG_ERROR, "ff asf bad non zero\n");
763 avio_seek(pb, -1, SEEK_CUR); //FIXME
766 asf->packet_flags = c;
767 asf->packet_property = d;
769 DO_2BITS(asf->packet_flags >> 5, packet_length, s->packet_size);
770 DO_2BITS(asf->packet_flags >> 1, padsize, 0); // sequence ignored
771 DO_2BITS(asf->packet_flags >> 3, padsize, 0); // padding length
773 //the following checks prevent overflows and infinite loops
774 if(!packet_length || packet_length >= (1U<<29)){
775 av_log(s, AV_LOG_ERROR, "invalid packet_length %d at:%"PRId64"\n", packet_length, avio_tell(pb));
778 if(padsize >= packet_length){
779 av_log(s, AV_LOG_ERROR, "invalid padsize %d at:%"PRId64"\n", padsize, avio_tell(pb));
783 asf->packet_timestamp = avio_rl32(pb);
784 avio_rl16(pb); /* duration */
785 // rsize has at least 11 bytes which have to be present
787 if (asf->packet_flags & 0x01) {
788 asf->packet_segsizetype = avio_r8(pb); rsize++;
789 asf->packet_segments = asf->packet_segsizetype & 0x3f;
791 asf->packet_segments = 1;
792 asf->packet_segsizetype = 0x80;
794 asf->packet_size_left = packet_length - padsize - rsize;
795 if (packet_length < asf->hdr.min_pktsize)
796 padsize += asf->hdr.min_pktsize - packet_length;
797 asf->packet_padsize = padsize;
798 av_dlog(s, "packet: size=%d padsize=%d left=%d\n", s->packet_size, asf->packet_padsize, asf->packet_size_left);
804 * @return <0 if error
806 static int asf_read_frame_header(AVFormatContext *s, AVIOContext *pb){
807 ASFContext *asf = s->priv_data;
809 int num = avio_r8(pb);
812 asf->packet_segments--;
813 asf->packet_key_frame = num >> 7;
814 asf->stream_index = asf->asfid2avid[num & 0x7f];
815 // sequence should be ignored!
816 DO_2BITS(asf->packet_property >> 4, asf->packet_seq, 0);
817 DO_2BITS(asf->packet_property >> 2, asf->packet_frag_offset, 0);
818 DO_2BITS(asf->packet_property, asf->packet_replic_size, 0);
819 //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);
820 if (asf->packet_replic_size >= 8) {
821 asf->packet_obj_size = avio_rl32(pb);
822 if(asf->packet_obj_size >= (1<<24) || asf->packet_obj_size <= 0){
823 av_log(s, AV_LOG_ERROR, "packet_obj_size invalid\n");
826 asf->packet_frag_timestamp = avio_rl32(pb); // timestamp
827 if(asf->packet_replic_size >= 8+38+4){
828 // for(i=0; i<asf->packet_replic_size-8; i++)
829 // av_log(s, AV_LOG_DEBUG, "%02X ",avio_r8(pb));
830 // av_log(s, AV_LOG_DEBUG, "\n");
836 avio_skip(pb, asf->packet_replic_size - 8 - 38 - 4);
837 if(ts0!= -1) asf->packet_frag_timestamp= ts0/10000;
838 else asf->packet_frag_timestamp= AV_NOPTS_VALUE;
840 avio_skip(pb, asf->packet_replic_size - 8);
841 rsize += asf->packet_replic_size; // FIXME - check validity
842 } else if (asf->packet_replic_size==1){
843 // multipacket - frag_offset is beginning timestamp
844 asf->packet_time_start = asf->packet_frag_offset;
845 asf->packet_frag_offset = 0;
846 asf->packet_frag_timestamp = asf->packet_timestamp;
848 asf->packet_time_delta = avio_r8(pb);
850 }else if(asf->packet_replic_size!=0){
851 av_log(s, AV_LOG_ERROR, "unexpected packet_replic_size of %d\n", asf->packet_replic_size);
854 if (asf->packet_flags & 0x01) {
855 DO_2BITS(asf->packet_segsizetype >> 6, asf->packet_frag_size, 0); // 0 is illegal
856 if(asf->packet_frag_size > asf->packet_size_left - rsize){
857 av_log(s, AV_LOG_ERROR, "packet_frag_size is invalid\n");
860 //printf("Fragsize %d\n", asf->packet_frag_size);
862 asf->packet_frag_size = asf->packet_size_left - rsize;
863 //printf("Using rest %d %d %d\n", asf->packet_frag_size, asf->packet_size_left, rsize);
865 if (asf->packet_replic_size == 1) {
866 asf->packet_multi_size = asf->packet_frag_size;
867 if (asf->packet_multi_size > asf->packet_size_left)
870 asf->packet_size_left -= rsize;
871 //printf("___objsize____ %d %d rs:%d\n", asf->packet_obj_size, asf->packet_frag_offset, rsize);
877 * Parse data from individual ASF packets (which were previously loaded
878 * with asf_get_packet()).
879 * @param s demux context
880 * @param pb context to read data from
881 * @param pkt pointer to store packet data into
882 * @return 0 if data was stored in pkt, <0 on error or 1 if more ASF
883 * packets need to be loaded (through asf_get_packet())
885 static int ff_asf_parse_packet(AVFormatContext *s, AVIOContext *pb, AVPacket *pkt)
887 ASFContext *asf = s->priv_data;
888 ASFStream *asf_st = 0;
893 if (asf->packet_size_left < FRAME_HEADER_SIZE
894 || asf->packet_segments < 1) {
895 //asf->packet_size_left <= asf->packet_padsize) {
896 int ret = asf->packet_size_left + asf->packet_padsize;
897 //printf("PacketLeftSize:%d Pad:%d Pos:%"PRId64"\n", asf->packet_size_left, asf->packet_padsize, avio_tell(pb));
902 asf->packet_pos= avio_tell(pb);
903 if (asf->data_object_size != (uint64_t)-1 &&
904 (asf->packet_pos - asf->data_object_offset >= asf->data_object_size))
905 return AVERROR_EOF; /* Do not exceed the size of the data object */
908 if (asf->packet_time_start == 0) {
909 if(asf_read_frame_header(s, pb) < 0){
910 asf->packet_segments= 0;
913 if (asf->stream_index < 0
914 || s->streams[asf->stream_index]->discard >= AVDISCARD_ALL
915 || (!asf->packet_key_frame && s->streams[asf->stream_index]->discard >= AVDISCARD_NONKEY)
917 asf->packet_time_start = 0;
918 /* unhandled packet (should not happen) */
919 avio_skip(pb, asf->packet_frag_size);
920 asf->packet_size_left -= asf->packet_frag_size;
921 if(asf->stream_index < 0)
922 av_log(s, AV_LOG_ERROR, "ff asf skip %d (unknown stream)\n", asf->packet_frag_size);
925 asf->asf_st = s->streams[asf->stream_index]->priv_data;
927 asf_st = asf->asf_st;
929 if (asf->packet_replic_size == 1) {
930 // frag_offset is here used as the beginning timestamp
931 asf->packet_frag_timestamp = asf->packet_time_start;
932 asf->packet_time_start += asf->packet_time_delta;
933 asf->packet_obj_size = asf->packet_frag_size = avio_r8(pb);
934 asf->packet_size_left--;
935 asf->packet_multi_size--;
936 if (asf->packet_multi_size < asf->packet_obj_size)
938 asf->packet_time_start = 0;
939 avio_skip(pb, asf->packet_multi_size);
940 asf->packet_size_left -= asf->packet_multi_size;
943 asf->packet_multi_size -= asf->packet_obj_size;
944 //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);
946 if( /*asf->packet_frag_size == asf->packet_obj_size*/
947 asf_st->frag_offset + asf->packet_frag_size <= asf_st->pkt.size
948 && asf_st->frag_offset + asf->packet_frag_size > asf->packet_obj_size){
949 av_log(s, AV_LOG_INFO, "ignoring invalid packet_obj_size (%d %d %d %d)\n",
950 asf_st->frag_offset, asf->packet_frag_size,
951 asf->packet_obj_size, asf_st->pkt.size);
952 asf->packet_obj_size= asf_st->pkt.size;
955 if ( asf_st->pkt.size != asf->packet_obj_size
956 || asf_st->frag_offset + asf->packet_frag_size > asf_st->pkt.size) { //FIXME is this condition sufficient?
957 if(asf_st->pkt.data){
958 av_log(s, AV_LOG_INFO, "freeing incomplete packet size %d, new %d\n", asf_st->pkt.size, asf->packet_obj_size);
959 asf_st->frag_offset = 0;
960 av_free_packet(&asf_st->pkt);
963 av_new_packet(&asf_st->pkt, asf->packet_obj_size);
964 asf_st->seq = asf->packet_seq;
965 asf_st->pkt.dts = asf->packet_frag_timestamp - asf->hdr.preroll;
966 asf_st->pkt.stream_index = asf->stream_index;
968 asf_st->packet_pos= asf->packet_pos;
969 //printf("new packet: stream:%d key:%d packet_key:%d audio:%d size:%d\n",
970 //asf->stream_index, asf->packet_key_frame, asf_st->pkt.flags & AV_PKT_FLAG_KEY,
971 //s->streams[asf->stream_index]->codec->codec_type == AVMEDIA_TYPE_AUDIO, asf->packet_obj_size);
972 if (s->streams[asf->stream_index]->codec->codec_type == AVMEDIA_TYPE_AUDIO)
973 asf->packet_key_frame = 1;
974 if (asf->packet_key_frame)
975 asf_st->pkt.flags |= AV_PKT_FLAG_KEY;
979 //printf("READ PACKET s:%d os:%d o:%d,%d l:%d DATA:%p\n",
980 // s->packet_size, asf_st->pkt.size, asf->packet_frag_offset,
981 // asf_st->frag_offset, asf->packet_frag_size, asf_st->pkt.data);
982 asf->packet_size_left -= asf->packet_frag_size;
983 if (asf->packet_size_left < 0)
986 if( asf->packet_frag_offset >= asf_st->pkt.size
987 || asf->packet_frag_size > asf_st->pkt.size - asf->packet_frag_offset){
988 av_log(s, AV_LOG_ERROR, "packet fragment position invalid %u,%u not in %u\n",
989 asf->packet_frag_offset, asf->packet_frag_size, asf_st->pkt.size);
993 ret = avio_read(pb, asf_st->pkt.data + asf->packet_frag_offset,
994 asf->packet_frag_size);
995 if (ret != asf->packet_frag_size) {
996 if (ret < 0 || asf->packet_frag_offset + ret == 0)
997 return ret < 0 ? ret : AVERROR_EOF;
998 if (asf_st->ds_span > 1) {
999 // scrambling, we can either drop it completely or fill the remainder
1000 // TODO: should we fill the whole packet instead of just the current
1002 memset(asf_st->pkt.data + asf->packet_frag_offset + ret, 0,
1003 asf->packet_frag_size - ret);
1004 ret = asf->packet_frag_size;
1006 // no scrambling, so we can return partial packets
1007 av_shrink_packet(&asf_st->pkt, asf->packet_frag_offset + ret);
1009 if (s->key && s->keylen == 20)
1010 ff_asfcrypt_dec(s->key, asf_st->pkt.data + asf->packet_frag_offset,
1012 asf_st->frag_offset += ret;
1013 /* test if whole packet is read */
1014 if (asf_st->frag_offset == asf_st->pkt.size) {
1015 //workaround for macroshit radio DVR-MS files
1016 if( s->streams[asf->stream_index]->codec->codec_id == CODEC_ID_MPEG2VIDEO
1017 && asf_st->pkt.size > 100){
1019 for(i=0; i<asf_st->pkt.size && !asf_st->pkt.data[i]; i++);
1020 if(i == asf_st->pkt.size){
1021 av_log(s, AV_LOG_DEBUG, "discarding ms fart\n");
1022 asf_st->frag_offset = 0;
1023 av_free_packet(&asf_st->pkt);
1029 if (asf_st->ds_span > 1) {
1030 if(asf_st->pkt.size != asf_st->ds_packet_size * asf_st->ds_span){
1031 av_log(s, AV_LOG_ERROR, "pkt.size != ds_packet_size * ds_span (%d %d %d)\n", asf_st->pkt.size, asf_st->ds_packet_size, asf_st->ds_span);
1033 /* packet descrambling */
1034 uint8_t *newdata = av_malloc(asf_st->pkt.size + FF_INPUT_BUFFER_PADDING_SIZE);
1037 memset(newdata + asf_st->pkt.size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
1038 while (offset < asf_st->pkt.size) {
1039 int off = offset / asf_st->ds_chunk_size;
1040 int row = off / asf_st->ds_span;
1041 int col = off % asf_st->ds_span;
1042 int idx = row + col * asf_st->ds_packet_size / asf_st->ds_chunk_size;
1043 //printf("off:%d row:%d col:%d idx:%d\n", off, row, col, idx);
1045 assert(offset + asf_st->ds_chunk_size <= asf_st->pkt.size);
1046 assert(idx+1 <= asf_st->pkt.size / asf_st->ds_chunk_size);
1047 memcpy(newdata + offset,
1048 asf_st->pkt.data + idx * asf_st->ds_chunk_size,
1049 asf_st->ds_chunk_size);
1050 offset += asf_st->ds_chunk_size;
1052 av_free(asf_st->pkt.data);
1053 asf_st->pkt.data = newdata;
1057 asf_st->frag_offset = 0;
1059 //printf("packet %d %d\n", asf_st->pkt.size, asf->packet_frag_size);
1060 asf_st->pkt.size = 0;
1061 asf_st->pkt.data = 0;
1062 break; // packet completed
1068 static int asf_read_packet(AVFormatContext *s, AVPacket *pkt)
1070 ASFContext *asf = s->priv_data;
1075 /* parse cached packets, if any */
1076 if ((ret = ff_asf_parse_packet(s, s->pb, pkt)) <= 0)
1078 if ((ret = ff_asf_get_packet(s, s->pb)) < 0)
1079 assert(asf->packet_size_left < FRAME_HEADER_SIZE || asf->packet_segments < 1);
1080 asf->packet_time_start = 0;
1086 // Added to support seeking after packets have been read
1087 // If information is not reset, read_packet fails due to
1088 // leftover information from previous reads
1089 static void asf_reset_header(AVFormatContext *s)
1091 ASFContext *asf = s->priv_data;
1095 asf->packet_size_left = 0;
1096 asf->packet_segments = 0;
1097 asf->packet_flags = 0;
1098 asf->packet_property = 0;
1099 asf->packet_timestamp = 0;
1100 asf->packet_segsizetype = 0;
1101 asf->packet_segments = 0;
1102 asf->packet_seq = 0;
1103 asf->packet_replic_size = 0;
1104 asf->packet_key_frame = 0;
1105 asf->packet_padsize = 0;
1106 asf->packet_frag_offset = 0;
1107 asf->packet_frag_size = 0;
1108 asf->packet_frag_timestamp = 0;
1109 asf->packet_multi_size = 0;
1110 asf->packet_obj_size = 0;
1111 asf->packet_time_delta = 0;
1112 asf->packet_time_start = 0;
1114 for(i=0; i<s->nb_streams; i++){
1115 asf_st= s->streams[i]->priv_data;
1116 av_free_packet(&asf_st->pkt);
1117 asf_st->frag_offset=0;
1123 static int asf_read_close(AVFormatContext *s)
1127 asf_reset_header(s);
1128 for(i=0;i<s->nb_streams;i++) {
1129 AVStream *st = s->streams[i];
1130 av_free(st->codec->palctrl);
1135 static int64_t asf_read_pts(AVFormatContext *s, int stream_index, int64_t *ppos, int64_t pos_limit)
1137 AVPacket pkt1, *pkt = &pkt1;
1142 int64_t start_pos[ASF_MAX_STREAMS];
1144 for(i=0; i<s->nb_streams; i++){
1148 if (s->packet_size > 0)
1149 pos= (pos+s->packet_size-1-s->data_offset)/s->packet_size*s->packet_size+ s->data_offset;
1151 avio_seek(s->pb, pos, SEEK_SET);
1153 //printf("asf_read_pts\n");
1154 asf_reset_header(s);
1156 if (av_read_frame(s, pkt) < 0){
1157 av_log(s, AV_LOG_INFO, "asf_read_pts failed\n");
1158 return AV_NOPTS_VALUE;
1163 av_free_packet(pkt);
1164 if(pkt->flags&AV_PKT_FLAG_KEY){
1165 i= pkt->stream_index;
1167 asf_st= s->streams[i]->priv_data;
1169 // assert((asf_st->packet_pos - s->data_offset) % s->packet_size == 0);
1170 pos= asf_st->packet_pos;
1172 av_add_index_entry(s->streams[i], pos, pts, pkt->size, pos - start_pos[i] + 1, AVINDEX_KEYFRAME);
1173 start_pos[i]= asf_st->packet_pos + 1;
1175 if(pkt->stream_index == stream_index)
1181 //printf("found keyframe at %"PRId64" stream %d stamp:%"PRId64"\n", *ppos, stream_index, pts);
1186 static void asf_build_simple_index(AVFormatContext *s, int stream_index)
1189 ASFContext *asf = s->priv_data;
1190 int64_t current_pos= avio_tell(s->pb);
1193 avio_seek(s->pb, asf->data_object_offset + asf->data_object_size, SEEK_SET);
1194 ff_get_guid(s->pb, &g);
1196 /* the data object can be followed by other top-level objects,
1197 skip them until the simple index object is reached */
1198 while (ff_guidcmp(&g, &index_guid)) {
1199 int64_t gsize= avio_rl64(s->pb);
1200 if (gsize < 24 || s->pb->eof_reached) {
1201 avio_seek(s->pb, current_pos, SEEK_SET);
1204 avio_skip(s->pb, gsize-24);
1205 ff_get_guid(s->pb, &g);
1209 int64_t itime, last_pos=-1;
1211 int64_t av_unused gsize= avio_rl64(s->pb);
1212 ff_get_guid(s->pb, &g);
1213 itime=avio_rl64(s->pb);
1214 pct=avio_rl32(s->pb);
1215 ict=avio_rl32(s->pb);
1216 av_log(s, AV_LOG_DEBUG, "itime:0x%"PRIx64", pct:%d, ict:%d\n",itime,pct,ict);
1218 for (i=0;i<ict;i++){
1219 int pktnum=avio_rl32(s->pb);
1220 int pktct =avio_rl16(s->pb);
1221 int64_t pos = s->data_offset + s->packet_size*(int64_t)pktnum;
1222 int64_t index_pts= av_rescale(itime, i, 10000);
1224 if(pos != last_pos){
1225 av_log(s, AV_LOG_DEBUG, "pktnum:%d, pktct:%d\n", pktnum, pktct);
1226 av_add_index_entry(s->streams[stream_index], pos, index_pts, s->packet_size, 0, AVINDEX_KEYFRAME);
1232 avio_seek(s->pb, current_pos, SEEK_SET);
1235 static int asf_read_seek(AVFormatContext *s, int stream_index, int64_t pts, int flags)
1237 ASFContext *asf = s->priv_data;
1238 AVStream *st = s->streams[stream_index];
1242 if (s->packet_size <= 0)
1245 /* Try using the protocol's read_seek if available */
1247 int ret = ffio_read_seek(s->pb, stream_index, pts, flags);
1249 asf_reset_header(s);
1250 if (ret != AVERROR(ENOSYS))
1254 if (!asf->index_read)
1255 asf_build_simple_index(s, stream_index);
1257 if(!(asf->index_read && st->index_entries)){
1258 if(av_seek_frame_binary(s, stream_index, pts, flags)<0)
1261 index= av_index_search_timestamp(st, pts, flags);
1265 /* find the position */
1266 pos = st->index_entries[index].pos;
1268 // various attempts to find key frame have failed so far
1269 // asf_reset_header(s);
1270 // avio_seek(s->pb, pos, SEEK_SET);
1272 // for(i=0;i<16;i++){
1273 // pos = avio_tell(s->pb);
1274 // if (av_read_frame(s, &pkt) < 0){
1275 // av_log(s, AV_LOG_INFO, "seek failed\n");
1278 // asf_st = s->streams[stream_index]->priv_data;
1279 // pos += st->parser->frame_offset;
1281 // if (pkt.size > b) {
1286 // av_free_packet(&pkt);
1290 av_log(s, AV_LOG_DEBUG, "SEEKTO: %"PRId64"\n", pos);
1291 avio_seek(s->pb, pos, SEEK_SET);
1293 asf_reset_header(s);
1297 AVInputFormat ff_asf_demuxer = {
1299 NULL_IF_CONFIG_SMALL("ASF format"),