2 * ASF compatible demuxer
3 * Copyright (c) 2000, 2001 Fabrice Bellard
5 * This file is part of FFmpeg.
7 * FFmpeg 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 * FFmpeg 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 FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "libavutil/attributes.h"
25 #include "libavutil/bswap.h"
26 #include "libavutil/common.h"
27 #include "libavutil/avstring.h"
28 #include "libavutil/dict.h"
29 #include "libavutil/mathematics.h"
30 #include "libavutil/opt.h"
33 #include "avio_internal.h"
38 #include "avlanguage.h"
42 int asfid2avid[128]; ///< conversion table from asf ID 2 AVStream ID
43 ASFStream streams[128]; ///< it's max number and it's not that big
44 uint32_t stream_bitrates[128]; ///< max number of streams, bitrate for each (for streaming)
46 char stream_languages[128][6]; ///< max number of streams, language for each (RFC1766, e.g. en-US)
47 /* non streamed additonnal info */
50 /* only for reading */
51 uint64_t data_offset; ///< beginning of the first data packet
52 uint64_t data_object_offset; ///< data object offset (excl. GUID & size)
53 uint64_t data_object_size; ///< size of the data object
61 int packet_segsizetype;
64 int packet_replic_size;
67 unsigned int packet_frag_offset;
68 unsigned int packet_frag_size;
69 int64_t packet_frag_timestamp;
70 int packet_multi_size;
72 int packet_time_delta;
73 int packet_time_start;
78 ASFStream* asf_st; ///< currently decoded stream
83 static const AVOption options[] = {
84 {"no_resync_search", "Don't try to resynchronize by looking for a certain optional start code", offsetof(ASFContext, no_resync_search), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, AV_OPT_FLAG_DECODING_PARAM },
88 static const AVClass asf_class = {
89 .class_name = "asf demuxer",
90 .item_name = av_default_item_name,
92 .version = LIBAVUTIL_VERSION_INT,
98 #define ASF_MAX_STREAMS 127
99 #define FRAME_HEADER_SIZE 16
100 // Fix Me! FRAME_HEADER_SIZE may be different. (17 is known to be too large)
103 static const ff_asf_guid stream_bitrate_guid = { /* (http://get.to/sdp) */
104 0xce, 0x75, 0xf8, 0x7b, 0x8d, 0x46, 0xd1, 0x11, 0x8d, 0x82, 0x00, 0x60, 0x97, 0xc9, 0xa2, 0xb2
107 #define PRINT_IF_GUID(g,cmp) \
108 if (!ff_guidcmp(g, &cmp)) \
109 av_dlog(NULL, "(GUID: %s) ", #cmp)
111 static void print_guid(const ff_asf_guid *g)
114 PRINT_IF_GUID(g, ff_asf_header);
115 else PRINT_IF_GUID(g, ff_asf_file_header);
116 else PRINT_IF_GUID(g, ff_asf_stream_header);
117 else PRINT_IF_GUID(g, ff_asf_audio_stream);
118 else PRINT_IF_GUID(g, ff_asf_audio_conceal_none);
119 else PRINT_IF_GUID(g, ff_asf_video_stream);
120 else PRINT_IF_GUID(g, ff_asf_video_conceal_none);
121 else PRINT_IF_GUID(g, ff_asf_command_stream);
122 else PRINT_IF_GUID(g, ff_asf_comment_header);
123 else PRINT_IF_GUID(g, ff_asf_codec_comment_header);
124 else PRINT_IF_GUID(g, ff_asf_codec_comment1_header);
125 else PRINT_IF_GUID(g, ff_asf_data_header);
126 else PRINT_IF_GUID(g, ff_asf_simple_index_header);
127 else PRINT_IF_GUID(g, ff_asf_head1_guid);
128 else PRINT_IF_GUID(g, ff_asf_head2_guid);
129 else PRINT_IF_GUID(g, ff_asf_my_guid);
130 else PRINT_IF_GUID(g, ff_asf_ext_stream_header);
131 else PRINT_IF_GUID(g, ff_asf_extended_content_header);
132 else PRINT_IF_GUID(g, ff_asf_ext_stream_embed_stream_header);
133 else PRINT_IF_GUID(g, ff_asf_ext_stream_audio_stream);
134 else PRINT_IF_GUID(g, ff_asf_metadata_header);
135 else PRINT_IF_GUID(g, ff_asf_marker_header);
136 else PRINT_IF_GUID(g, stream_bitrate_guid);
137 else PRINT_IF_GUID(g, ff_asf_language_guid);
139 av_dlog(NULL, "(GUID: unknown) ");
141 av_dlog(NULL, " 0x%02x,", (*g)[i]);
142 av_dlog(NULL, "}\n");
146 #define print_guid(g)
149 static int asf_probe(AVProbeData *pd)
151 /* check file header */
152 if (!ff_guidcmp(pd->buf, &ff_asf_header))
153 return AVPROBE_SCORE_MAX;
158 static int get_value(AVIOContext *pb, int type){
160 case 2: return avio_rl32(pb);
161 case 3: return avio_rl32(pb);
162 case 4: return avio_rl64(pb);
163 case 5: return avio_rl16(pb);
164 default:return INT_MIN;
168 /* MSDN claims that this should be "compatible with the ID3 frame, APIC",
169 * but in reality this is only loosely similar */
170 static int asf_read_picture(AVFormatContext *s, int len)
172 AVPacket pkt = { 0 };
173 const CodecMime *mime = ff_id3v2_mime_tags;
174 enum AVCodecID id = AV_CODEC_ID_NONE;
176 uint8_t *desc = NULL;
177 ASFStream *ast = NULL;
179 int ret, type, picsize, desc_len;
181 /* type + picsize + mime + desc */
182 if (len < 1 + 4 + 2 + 2) {
183 av_log(s, AV_LOG_ERROR, "Invalid attached picture size: %d.\n", len);
184 return AVERROR_INVALIDDATA;
188 type = avio_r8(s->pb);
190 if (type >= FF_ARRAY_ELEMS(ff_id3v2_picture_types) || type < 0) {
191 av_log(s, AV_LOG_WARNING, "Unknown attached picture type: %d.\n", type);
195 /* picture data size */
196 picsize = avio_rl32(s->pb);
199 /* picture MIME type */
200 len -= avio_get_str16le(s->pb, len, mimetype, sizeof(mimetype));
201 while (mime->id != AV_CODEC_ID_NONE) {
202 if (!strncmp(mime->str, mimetype, sizeof(mimetype))) {
208 if (id == AV_CODEC_ID_NONE) {
209 av_log(s, AV_LOG_ERROR, "Unknown attached picture mimetype: %s.\n",
214 if (picsize >= len) {
215 av_log(s, AV_LOG_ERROR, "Invalid attached picture data size: %d >= %d.\n",
217 return AVERROR_INVALIDDATA;
220 /* picture description */
221 desc_len = (len - picsize) * 2 + 1;
222 desc = av_malloc(desc_len);
224 return AVERROR(ENOMEM);
225 len -= avio_get_str16le(s->pb, len - picsize, desc, desc_len);
227 ret = av_get_packet(s->pb, &pkt, picsize);
231 st = avformat_new_stream(s, NULL);
232 ast = av_mallocz(sizeof(*ast));
234 ret = AVERROR(ENOMEM);
239 st->disposition |= AV_DISPOSITION_ATTACHED_PIC;
240 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
241 st->codec->codec_id = id;
243 st->attached_pic = pkt;
244 st->attached_pic.stream_index = st->index;
245 st->attached_pic.flags |= AV_PKT_FLAG_KEY;
248 av_dict_set(&st->metadata, "title", desc, AV_DICT_DONT_STRDUP_VAL);
252 av_dict_set(&st->metadata, "comment", ff_id3v2_picture_types[type], 0);
259 av_free_packet(&pkt);
263 static void get_tag(AVFormatContext *s, const char *key, int type, int len)
266 int64_t off = avio_tell(s->pb);
268 if ((unsigned)len >= (UINT_MAX - 1)/2)
271 value = av_malloc(2*len+1);
275 if (type == 0) { // UTF16-LE
276 avio_get_str16le(s->pb, len, value, 2*len + 1);
277 } else if (type == -1) { // ASCII
278 avio_read(s->pb, value, len);
280 } else if (type > 1 && type <= 5) { // boolean or DWORD or QWORD or WORD
281 uint64_t num = get_value(s->pb, type);
282 snprintf(value, len, "%"PRIu64, num);
283 } else if (type == 1 && !strcmp(key, "WM/Picture")) { // handle cover art
284 asf_read_picture(s, len);
287 av_log(s, AV_LOG_DEBUG, "Unsupported value type %d in tag %s.\n", type, key);
291 av_dict_set(&s->metadata, key, value, 0);
294 avio_seek(s->pb, off + len, SEEK_SET);
297 static int asf_read_file_properties(AVFormatContext *s, int64_t size)
299 ASFContext *asf = s->priv_data;
300 AVIOContext *pb = s->pb;
302 ff_get_guid(pb, &asf->hdr.guid);
303 asf->hdr.file_size = avio_rl64(pb);
304 asf->hdr.create_time = avio_rl64(pb);
305 avio_rl64(pb); /* number of packets */
306 asf->hdr.play_time = avio_rl64(pb);
307 asf->hdr.send_time = avio_rl64(pb);
308 asf->hdr.preroll = avio_rl32(pb);
309 asf->hdr.ignore = avio_rl32(pb);
310 asf->hdr.flags = avio_rl32(pb);
311 asf->hdr.min_pktsize = avio_rl32(pb);
312 asf->hdr.max_pktsize = avio_rl32(pb);
313 if (asf->hdr.min_pktsize >= (1U<<29))
314 return AVERROR_INVALIDDATA;
315 asf->hdr.max_bitrate = avio_rl32(pb);
316 s->packet_size = asf->hdr.max_pktsize;
321 static int asf_read_stream_properties(AVFormatContext *s, int64_t size)
323 ASFContext *asf = s->priv_data;
324 AVIOContext *pb = s->pb;
328 enum AVMediaType type;
329 int type_specific_size, sizeX;
331 int64_t pos1, pos2, start_time;
332 int test_for_ext_stream_audio, is_dvr_ms_audio=0;
334 if (s->nb_streams == ASF_MAX_STREAMS) {
335 av_log(s, AV_LOG_ERROR, "too many streams\n");
336 return AVERROR(EINVAL);
339 pos1 = avio_tell(pb);
341 st = avformat_new_stream(s, NULL);
343 return AVERROR(ENOMEM);
344 avpriv_set_pts_info(st, 32, 1, 1000); /* 32 bit pts in ms */
345 asf_st = av_mallocz(sizeof(ASFStream));
347 return AVERROR(ENOMEM);
348 st->priv_data = asf_st;
349 start_time = asf->hdr.preroll;
351 asf_st->stream_language_index = 128; // invalid stream index means no language info
353 if(!(asf->hdr.flags & 0x01)) { // if we aren't streaming...
354 int64_t fsize = avio_size(pb);
355 if (fsize <= 0 || (int64_t)asf->hdr.file_size <= 0 || FFABS(fsize - (int64_t)asf->hdr.file_size) < 10000)
356 st->duration = asf->hdr.play_time /
357 (10000000 / 1000) - start_time;
361 test_for_ext_stream_audio = 0;
362 if (!ff_guidcmp(&g, &ff_asf_audio_stream)) {
363 type = AVMEDIA_TYPE_AUDIO;
364 } else if (!ff_guidcmp(&g, &ff_asf_video_stream)) {
365 type = AVMEDIA_TYPE_VIDEO;
366 } else if (!ff_guidcmp(&g, &ff_asf_jfif_media)) {
367 type = AVMEDIA_TYPE_VIDEO;
368 st->codec->codec_id = AV_CODEC_ID_MJPEG;
369 } else if (!ff_guidcmp(&g, &ff_asf_command_stream)) {
370 type = AVMEDIA_TYPE_DATA;
371 } else if (!ff_guidcmp(&g, &ff_asf_ext_stream_embed_stream_header)) {
372 test_for_ext_stream_audio = 1;
373 type = AVMEDIA_TYPE_UNKNOWN;
378 avio_skip(pb, 8); /* total_size */
379 type_specific_size = avio_rl32(pb);
381 st->id = avio_rl16(pb) & 0x7f; /* stream id */
382 // mapping of asf ID to AV stream ID;
383 asf->asfid2avid[st->id] = s->nb_streams - 1;
387 if (test_for_ext_stream_audio) {
389 if (!ff_guidcmp(&g, &ff_asf_ext_stream_audio_stream)) {
390 type = AVMEDIA_TYPE_AUDIO;
401 st->codec->codec_type = type;
402 if (type == AVMEDIA_TYPE_AUDIO) {
403 int ret = ff_get_wav_header(pb, st->codec, type_specific_size);
406 if (is_dvr_ms_audio) {
407 // codec_id and codec_tag are unreliable in dvr_ms
408 // files. Set them later by probing stream.
409 st->request_probe= 1;
410 st->codec->codec_tag = 0;
412 if (st->codec->codec_id == AV_CODEC_ID_AAC) {
413 st->need_parsing = AVSTREAM_PARSE_NONE;
415 st->need_parsing = AVSTREAM_PARSE_FULL;
417 /* We have to init the frame size at some point .... */
418 pos2 = avio_tell(pb);
419 if (size >= (pos2 + 8 - pos1 + 24)) {
420 asf_st->ds_span = avio_r8(pb);
421 asf_st->ds_packet_size = avio_rl16(pb);
422 asf_st->ds_chunk_size = avio_rl16(pb);
423 avio_rl16(pb); //ds_data_size
424 avio_r8(pb); //ds_silence_data
426 if (asf_st->ds_span > 1) {
427 if (!asf_st->ds_chunk_size
428 || (asf_st->ds_packet_size/asf_st->ds_chunk_size <= 1)
429 || asf_st->ds_packet_size % asf_st->ds_chunk_size)
430 asf_st->ds_span = 0; // disable descrambling
432 } else if (type == AVMEDIA_TYPE_VIDEO &&
433 size - (avio_tell(pb) - pos1 + 24) >= 51) {
437 avio_rl16(pb); /* size */
438 sizeX= avio_rl32(pb); /* size */
439 st->codec->width = avio_rl32(pb);
440 st->codec->height = avio_rl32(pb);
441 /* not available for asf */
442 avio_rl16(pb); /* panes */
443 st->codec->bits_per_coded_sample = avio_rl16(pb); /* depth */
444 tag1 = avio_rl32(pb);
447 st->codec->extradata_size = ffio_limit(pb, sizeX - 40);
448 st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
449 avio_read(pb, st->codec->extradata, st->codec->extradata_size);
452 /* Extract palette from extradata if bpp <= 8 */
453 /* This code assumes that extradata contains only palette */
454 /* This is true for all paletted codecs implemented in libavcodec */
455 if (st->codec->extradata_size && (st->codec->bits_per_coded_sample <= 8)) {
458 for (i = 0; i < FFMIN(st->codec->extradata_size, AVPALETTE_SIZE)/4; i++)
459 asf_st->palette[i] = av_bswap32(((uint32_t*)st->codec->extradata)[i]);
461 memcpy(asf_st->palette, st->codec->extradata,
462 FFMIN(st->codec->extradata_size, AVPALETTE_SIZE));
464 asf_st->palette_changed = 1;
467 st->codec->codec_tag = tag1;
468 st->codec->codec_id = ff_codec_get_id(ff_codec_bmp_tags, tag1);
469 if(tag1 == MKTAG('D', 'V', 'R', ' ')){
470 st->need_parsing = AVSTREAM_PARSE_FULL;
471 // 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
473 st->codec->height = 0;
474 av_freep(&st->codec->extradata);
475 st->codec->extradata_size=0;
477 if(st->codec->codec_id == AV_CODEC_ID_H264)
478 st->need_parsing = AVSTREAM_PARSE_FULL_ONCE;
480 pos2 = avio_tell(pb);
481 avio_skip(pb, size - (pos2 - pos1 + 24));
486 static int asf_read_ext_stream_properties(AVFormatContext *s, int64_t size)
488 ASFContext *asf = s->priv_data;
489 AVIOContext *pb = s->pb;
491 int ext_len, payload_ext_ct, stream_ct, i;
492 uint32_t leak_rate, stream_num;
493 unsigned int stream_languageid_index;
495 avio_rl64(pb); // starttime
496 avio_rl64(pb); // endtime
497 leak_rate = avio_rl32(pb); // leak-datarate
498 avio_rl32(pb); // bucket-datasize
499 avio_rl32(pb); // init-bucket-fullness
500 avio_rl32(pb); // alt-leak-datarate
501 avio_rl32(pb); // alt-bucket-datasize
502 avio_rl32(pb); // alt-init-bucket-fullness
503 avio_rl32(pb); // max-object-size
504 avio_rl32(pb); // flags (reliable,seekable,no_cleanpoints?,resend-live-cleanpoints, rest of bits reserved)
505 stream_num = avio_rl16(pb); // stream-num
507 stream_languageid_index = avio_rl16(pb); // stream-language-id-index
508 if (stream_num < 128)
509 asf->streams[stream_num].stream_language_index = stream_languageid_index;
511 avio_rl64(pb); // avg frametime in 100ns units
512 stream_ct = avio_rl16(pb); //stream-name-count
513 payload_ext_ct = avio_rl16(pb); //payload-extension-system-count
515 if (stream_num < 128) {
516 asf->stream_bitrates[stream_num] = leak_rate;
517 asf->streams[stream_num].payload_ext_ct = 0;
520 for (i=0; i<stream_ct; i++){
522 ext_len = avio_rl16(pb);
523 avio_skip(pb, ext_len);
526 for (i=0; i<payload_ext_ct; i++){
529 size = avio_rl16(pb);
530 ext_len=avio_rl32(pb);
531 avio_skip(pb, ext_len);
532 if (stream_num < 128 && i < FF_ARRAY_ELEMS(asf->streams[stream_num].payload)) {
533 ASFPayload *p = &asf->streams[stream_num].payload[i];
536 av_log(s, AV_LOG_DEBUG, "Payload extension %x %d\n", g[0], p->size );
537 asf->streams[stream_num].payload_ext_ct ++;
544 static int asf_read_content_desc(AVFormatContext *s, int64_t size)
546 AVIOContext *pb = s->pb;
547 int len1, len2, len3, len4, len5;
549 len1 = avio_rl16(pb);
550 len2 = avio_rl16(pb);
551 len3 = avio_rl16(pb);
552 len4 = avio_rl16(pb);
553 len5 = avio_rl16(pb);
554 get_tag(s, "title" , 0, len1);
555 get_tag(s, "author" , 0, len2);
556 get_tag(s, "copyright", 0, len3);
557 get_tag(s, "comment" , 0, len4);
563 static int asf_read_ext_content_desc(AVFormatContext *s, int64_t size)
565 AVIOContext *pb = s->pb;
566 ASFContext *asf = s->priv_data;
567 int desc_count, i, ret;
569 desc_count = avio_rl16(pb);
570 for(i=0;i<desc_count;i++) {
571 int name_len,value_type,value_len;
574 name_len = avio_rl16(pb);
575 if (name_len%2) // must be even, broken lavf versions wrote len-1
577 if ((ret = avio_get_str16le(pb, name_len, name, sizeof(name))) < name_len)
578 avio_skip(pb, name_len - ret);
579 value_type = avio_rl16(pb);
580 value_len = avio_rl16(pb);
581 if (!value_type && value_len%2)
584 * My sample has that stream set to 0 maybe that mean the container.
585 * Asf stream count start at 1. I am using 0 to the container value since it's unused
587 if (!strcmp(name, "AspectRatioX")){
588 asf->dar[0].num= get_value(s->pb, value_type);
589 } else if(!strcmp(name, "AspectRatioY")){
590 asf->dar[0].den= get_value(s->pb, value_type);
592 get_tag(s, name, value_type, value_len);
598 static int asf_read_language_list(AVFormatContext *s, int64_t size)
600 AVIOContext *pb = s->pb;
601 ASFContext *asf = s->priv_data;
603 int stream_count = avio_rl16(pb);
604 for(j = 0; j < stream_count; j++) {
606 unsigned int lang_len = avio_r8(pb);
607 if ((ret = avio_get_str16le(pb, lang_len, lang, sizeof(lang))) < lang_len)
608 avio_skip(pb, lang_len - ret);
610 av_strlcpy(asf->stream_languages[j], lang, sizeof(*asf->stream_languages));
616 static int asf_read_metadata(AVFormatContext *s, int64_t size)
618 AVIOContext *pb = s->pb;
619 ASFContext *asf = s->priv_data;
620 int n, stream_num, name_len, value_len, value_num;
626 int av_unused value_type;
628 avio_rl16(pb); //lang_list_index
629 stream_num= avio_rl16(pb);
630 name_len= avio_rl16(pb);
631 value_type = avio_rl16(pb); /* value_type */
632 value_len= avio_rl32(pb);
634 if ((ret = avio_get_str16le(pb, name_len, name, sizeof(name))) < name_len)
635 avio_skip(pb, name_len - ret);
636 av_dlog(s, "%d %d %d %d %d <%s>\n",
637 i, stream_num, name_len, value_type, value_len, name);
638 value_num= avio_rl16(pb);//we should use get_value() here but it does not work 2 is le16 here but le32 elsewhere
639 avio_skip(pb, value_len - 2);
642 if (!strcmp(name, "AspectRatioX")) asf->dar[stream_num].num= value_num;
643 else if(!strcmp(name, "AspectRatioY")) asf->dar[stream_num].den= value_num;
650 static int asf_read_marker(AVFormatContext *s, int64_t size)
652 AVIOContext *pb = s->pb;
653 int i, count, name_len, ret;
656 avio_rl64(pb); // reserved 16 bytes
657 avio_rl64(pb); // ...
658 count = avio_rl32(pb); // markers count
659 avio_rl16(pb); // reserved 2 bytes
660 name_len = avio_rl16(pb); // name length
661 for(i=0;i<name_len;i++){
662 avio_r8(pb); // skip the name
665 for(i=0;i<count;i++){
669 avio_rl64(pb); // offset, 8 bytes
670 pres_time = avio_rl64(pb); // presentation time
671 avio_rl16(pb); // entry length
672 avio_rl32(pb); // send time
673 avio_rl32(pb); // flags
674 name_len = avio_rl32(pb); // name length
675 if ((ret = avio_get_str16le(pb, name_len * 2, name, sizeof(name))) < name_len)
676 avio_skip(pb, name_len - ret);
677 avpriv_new_chapter(s, i, (AVRational){1, 10000000}, pres_time, AV_NOPTS_VALUE, name );
683 static int asf_read_header(AVFormatContext *s)
685 ASFContext *asf = s->priv_data;
687 AVIOContext *pb = s->pb;
692 if (ff_guidcmp(&g, &ff_asf_header))
693 return AVERROR_INVALIDDATA;
698 memset(&asf->asfid2avid, -1, sizeof(asf->asfid2avid));
700 uint64_t gpos= avio_tell(pb);
702 gsize = avio_rl64(pb);
704 if (!ff_guidcmp(&g, &ff_asf_data_header)) {
705 asf->data_object_offset = avio_tell(pb);
706 // if not streaming, gsize is not unlimited (how?), and there is enough space in the file..
707 if (!(asf->hdr.flags & 0x01) && gsize >= 100) {
708 asf->data_object_size = gsize - 24;
710 asf->data_object_size = (uint64_t)-1;
715 return AVERROR_INVALIDDATA;
716 if (!ff_guidcmp(&g, &ff_asf_file_header)) {
717 int ret = asf_read_file_properties(s, gsize);
720 } else if (!ff_guidcmp(&g, &ff_asf_stream_header)) {
721 asf_read_stream_properties(s, gsize);
722 } else if (!ff_guidcmp(&g, &ff_asf_comment_header)) {
723 asf_read_content_desc(s, gsize);
724 } else if (!ff_guidcmp(&g, &ff_asf_language_guid)) {
725 asf_read_language_list(s, gsize);
726 } else if (!ff_guidcmp(&g, &ff_asf_extended_content_header)) {
727 asf_read_ext_content_desc(s, gsize);
728 } else if (!ff_guidcmp(&g, &ff_asf_metadata_header)) {
729 asf_read_metadata(s, gsize);
730 } else if (!ff_guidcmp(&g, &ff_asf_ext_stream_header)) {
731 asf_read_ext_stream_properties(s, gsize);
733 // there could be a optional stream properties object to follow
734 // if so the next iteration will pick it up
736 } else if (!ff_guidcmp(&g, &ff_asf_head1_guid)) {
740 } else if (!ff_guidcmp(&g, &ff_asf_marker_header)) {
741 asf_read_marker(s, gsize);
742 } else if (url_feof(pb)) {
746 if (!ff_guidcmp(&g, &ff_asf_content_encryption)) {
749 av_log(s, AV_LOG_WARNING, "DRM protected stream detected, decoding will likely fail!\n");
751 av_log(s, AV_LOG_DEBUG, "Secret data:\n");
752 av_get_packet(pb, &pkt, len); av_hex_dump_log(s, AV_LOG_DEBUG, pkt.data, pkt.size); av_free_packet(&pkt);
754 get_tag(s, "ASF_Protection_Type", -1, len);
756 get_tag(s, "ASF_Key_ID", -1, len);
758 get_tag(s, "ASF_License_URL", -1, len);
759 } else if (!ff_guidcmp(&g, &ff_asf_ext_content_encryption)) {
760 av_log(s, AV_LOG_WARNING, "Ext DRM protected stream detected, decoding will likely fail!\n");
761 av_dict_set(&s->metadata, "encryption", "ASF Extended Content Encryption", 0);
762 } else if (!ff_guidcmp(&g, &ff_asf_digital_signature)) {
763 av_log(s, AV_LOG_INFO, "Digital signature detected!\n");
767 if(avio_tell(pb) != gpos + gsize)
768 av_log(s, AV_LOG_DEBUG, "gpos mismatch our pos=%"PRIu64", end=%"PRId64"\n", avio_tell(pb)-gpos, gsize);
769 avio_seek(pb, gpos + gsize, SEEK_SET);
777 asf->data_offset = avio_tell(pb);
778 asf->packet_size_left = 0;
781 for(i=0; i<128; i++){
782 int stream_num= asf->asfid2avid[i];
784 AVStream *st = s->streams[stream_num];
785 if (!st->codec->bit_rate)
786 st->codec->bit_rate = asf->stream_bitrates[i];
787 if (asf->dar[i].num > 0 && asf->dar[i].den > 0){
788 av_reduce(&st->sample_aspect_ratio.num,
789 &st->sample_aspect_ratio.den,
790 asf->dar[i].num, asf->dar[i].den, INT_MAX);
791 } 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.
792 av_reduce(&st->sample_aspect_ratio.num,
793 &st->sample_aspect_ratio.den,
794 asf->dar[0].num, asf->dar[0].den, INT_MAX);
796 av_dlog(s, "i=%d, st->codec->codec_type:%d, asf->dar %d:%d sar=%d:%d\n",
797 i, st->codec->codec_type, asf->dar[i].num, asf->dar[i].den,
798 st->sample_aspect_ratio.num, st->sample_aspect_ratio.den);
800 // copy and convert language codes to the frontend
801 if (asf->streams[i].stream_language_index < 128) {
802 const char *rfc1766 = asf->stream_languages[asf->streams[i].stream_language_index];
803 if (rfc1766 && strlen(rfc1766) > 1) {
804 const char primary_tag[3] = { rfc1766[0], rfc1766[1], '\0' }; // ignore country code if any
805 const char *iso6392 = av_convert_lang_to(primary_tag, AV_LANG_ISO639_2_BIBL);
807 av_dict_set(&st->metadata, "language", iso6392, 0);
813 ff_metadata_conv(&s->metadata, NULL, ff_asf_metadata_conv);
818 #define DO_2BITS(bits, var, defval) \
821 case 3: var = avio_rl32(pb); rsize += 4; break; \
822 case 2: var = avio_rl16(pb); rsize += 2; break; \
823 case 1: var = avio_r8(pb); rsize++; break; \
824 default: var = defval; break; \
828 * Load a single ASF packet into the demuxer.
829 * @param s demux context
830 * @param pb context to read data from
831 * @return 0 on success, <0 on error
833 static int ff_asf_get_packet(AVFormatContext *s, AVIOContext *pb)
835 ASFContext *asf = s->priv_data;
836 uint32_t packet_length, padsize;
840 // if we do not know packet size, allow skipping up to 32 kB
842 if (asf->no_resync_search)
844 else if (s->packet_size > 0)
845 off= (avio_tell(pb) - s->data_offset) % s->packet_size + 3;
851 if(c == 0x82 && !d && !e)
857 * This code allows handling of -EAGAIN at packet boundaries (i.e.
858 * if the packet sync code above triggers -EAGAIN). This does not
859 * imply complete -EAGAIN handling support at random positions in
862 if (pb->error == AVERROR(EAGAIN))
863 return AVERROR(EAGAIN);
865 av_log(s, AV_LOG_ERROR, "ff asf bad header %x at:%"PRId64"\n", c, avio_tell(pb));
867 if ((c & 0x8f) == 0x82) {
870 av_log(s, AV_LOG_ERROR, "ff asf bad non zero\n");
871 return AVERROR_INVALIDDATA;
876 }else if(!url_feof(pb)){
877 avio_seek(pb, -1, SEEK_CUR); //FIXME
880 asf->packet_flags = c;
881 asf->packet_property = d;
883 DO_2BITS(asf->packet_flags >> 5, packet_length, s->packet_size);
884 DO_2BITS(asf->packet_flags >> 1, padsize, 0); // sequence ignored
885 DO_2BITS(asf->packet_flags >> 3, padsize, 0); // padding length
887 //the following checks prevent overflows and infinite loops
888 if(!packet_length || packet_length >= (1U<<29)){
889 av_log(s, AV_LOG_ERROR, "invalid packet_length %d at:%"PRId64"\n", packet_length, avio_tell(pb));
890 return AVERROR_INVALIDDATA;
892 if(padsize >= packet_length){
893 av_log(s, AV_LOG_ERROR, "invalid padsize %d at:%"PRId64"\n", padsize, avio_tell(pb));
894 return AVERROR_INVALIDDATA;
897 asf->packet_timestamp = avio_rl32(pb);
898 avio_rl16(pb); /* duration */
899 // rsize has at least 11 bytes which have to be present
901 if (asf->packet_flags & 0x01) {
902 asf->packet_segsizetype = avio_r8(pb); rsize++;
903 asf->packet_segments = asf->packet_segsizetype & 0x3f;
905 asf->packet_segments = 1;
906 asf->packet_segsizetype = 0x80;
908 if (rsize > packet_length - padsize) {
909 asf->packet_size_left = 0;
910 av_log(s, AV_LOG_ERROR,
911 "invalid packet header length %d for pktlen %d-%d at %"PRId64"\n",
912 rsize, packet_length, padsize, avio_tell(pb));
913 return AVERROR_INVALIDDATA;
915 asf->packet_size_left = packet_length - padsize - rsize;
916 if (packet_length < asf->hdr.min_pktsize)
917 padsize += asf->hdr.min_pktsize - packet_length;
918 asf->packet_padsize = padsize;
919 av_dlog(s, "packet: size=%d padsize=%d left=%d\n", s->packet_size, asf->packet_padsize, asf->packet_size_left);
925 * @return <0 if error
927 static int asf_read_frame_header(AVFormatContext *s, AVIOContext *pb){
928 ASFContext *asf = s->priv_data;
931 int num = avio_r8(pb);
933 int64_t ts0, ts1 av_unused;
935 asf->packet_segments--;
936 asf->packet_key_frame = num >> 7;
937 asf->stream_index = asf->asfid2avid[num & 0x7f];
938 asfst = &asf->streams[num & 0x7f];
939 // sequence should be ignored!
940 DO_2BITS(asf->packet_property >> 4, asf->packet_seq, 0);
941 DO_2BITS(asf->packet_property >> 2, asf->packet_frag_offset, 0);
942 DO_2BITS(asf->packet_property, asf->packet_replic_size, 0);
943 av_dlog(asf, "key:%d stream:%d seq:%d offset:%d replic_size:%d\n",
944 asf->packet_key_frame, asf->stream_index, asf->packet_seq,
945 asf->packet_frag_offset, asf->packet_replic_size);
946 if (rsize+(int64_t)asf->packet_replic_size > asf->packet_size_left) {
947 av_log(s, AV_LOG_ERROR, "packet_replic_size %d is invalid\n", asf->packet_replic_size);
948 return AVERROR_INVALIDDATA;
950 if (asf->packet_replic_size >= 8) {
951 int64_t end = avio_tell(pb) + asf->packet_replic_size;
953 asf->packet_obj_size = avio_rl32(pb);
954 if(asf->packet_obj_size >= (1<<24) || asf->packet_obj_size <= 0){
955 av_log(s, AV_LOG_ERROR, "packet_obj_size invalid\n");
956 return AVERROR_INVALIDDATA;
958 asf->packet_frag_timestamp = avio_rl32(pb); // timestamp
960 for (i=0; i<asfst->payload_ext_ct; i++) {
961 ASFPayload *p = &asfst->payload[i];
965 size = avio_rl16(pb);
966 payend = avio_tell(pb) + size;
968 av_log(s, AV_LOG_ERROR, "too long payload\n");
973 // duration = avio_rl16(pb);
976 aspect.num = avio_r8(pb);
977 aspect.den = avio_r8(pb);
978 if (aspect.num > 0 && aspect.den > 0 && asf->stream_index >= 0) {
979 s->streams[asf->stream_index]->sample_aspect_ratio = aspect;
986 if(ts0!= -1) asf->packet_frag_timestamp= ts0/10000;
987 else asf->packet_frag_timestamp= AV_NOPTS_VALUE;
997 avio_seek(pb, payend, SEEK_SET);
1000 avio_seek(pb, end, SEEK_SET);
1001 rsize += asf->packet_replic_size; // FIXME - check validity
1002 } else if (asf->packet_replic_size==1){
1003 // multipacket - frag_offset is beginning timestamp
1004 asf->packet_time_start = asf->packet_frag_offset;
1005 asf->packet_frag_offset = 0;
1006 asf->packet_frag_timestamp = asf->packet_timestamp;
1008 asf->packet_time_delta = avio_r8(pb);
1010 }else if(asf->packet_replic_size!=0){
1011 av_log(s, AV_LOG_ERROR, "unexpected packet_replic_size of %d\n", asf->packet_replic_size);
1012 return AVERROR_INVALIDDATA;
1014 if (asf->packet_flags & 0x01) {
1015 DO_2BITS(asf->packet_segsizetype >> 6, asf->packet_frag_size, 0); // 0 is illegal
1016 if (rsize > asf->packet_size_left) {
1017 av_log(s, AV_LOG_ERROR, "packet_replic_size is invalid\n");
1018 return AVERROR_INVALIDDATA;
1019 } else if(asf->packet_frag_size > asf->packet_size_left - rsize){
1020 if (asf->packet_frag_size > asf->packet_size_left - rsize + asf->packet_padsize) {
1021 av_log(s, AV_LOG_ERROR, "packet_frag_size is invalid (%d-%d)\n", asf->packet_size_left, rsize);
1022 return AVERROR_INVALIDDATA;
1024 int diff = asf->packet_frag_size - (asf->packet_size_left - rsize);
1025 asf->packet_size_left += diff;
1026 asf->packet_padsize -= diff;
1030 asf->packet_frag_size = asf->packet_size_left - rsize;
1032 if (asf->packet_replic_size == 1) {
1033 asf->packet_multi_size = asf->packet_frag_size;
1034 if (asf->packet_multi_size > asf->packet_size_left)
1035 return AVERROR_INVALIDDATA;
1037 asf->packet_size_left -= rsize;
1043 * Parse data from individual ASF packets (which were previously loaded
1044 * with asf_get_packet()).
1045 * @param s demux context
1046 * @param pb context to read data from
1047 * @param pkt pointer to store packet data into
1048 * @return 0 if data was stored in pkt, <0 on error or 1 if more ASF
1049 * packets need to be loaded (through asf_get_packet())
1051 static int ff_asf_parse_packet(AVFormatContext *s, AVIOContext *pb, AVPacket *pkt)
1053 ASFContext *asf = s->priv_data;
1054 ASFStream *asf_st = 0;
1060 if (asf->packet_size_left < FRAME_HEADER_SIZE ||
1061 asf->packet_segments < 1) {
1062 int ret = asf->packet_size_left + asf->packet_padsize;
1068 asf->packet_pos= avio_tell(pb);
1069 if (asf->data_object_size != (uint64_t)-1 &&
1070 (asf->packet_pos - asf->data_object_offset >= asf->data_object_size))
1071 return AVERROR_EOF; /* Do not exceed the size of the data object */
1074 if (asf->packet_time_start == 0) {
1075 if (asf_read_frame_header(s, pb) < 0) {
1076 asf->packet_segments = 0;
1079 if (asf->stream_index < 0 ||
1080 s->streams[asf->stream_index]->discard >= AVDISCARD_ALL ||
1081 (!asf->packet_key_frame &&
1082 s->streams[asf->stream_index]->discard >= AVDISCARD_NONKEY)) {
1083 asf->packet_time_start = 0;
1084 /* unhandled packet (should not happen) */
1085 avio_skip(pb, asf->packet_frag_size);
1086 asf->packet_size_left -= asf->packet_frag_size;
1087 if (asf->stream_index < 0)
1088 av_log(s, AV_LOG_ERROR, "ff asf skip %d (unknown stream)\n", asf->packet_frag_size);
1091 asf->asf_st = s->streams[asf->stream_index]->priv_data;
1093 asf_st = asf->asf_st;
1095 if (asf->packet_replic_size == 1) {
1096 // frag_offset is here used as the beginning timestamp
1097 asf->packet_frag_timestamp = asf->packet_time_start;
1098 asf->packet_time_start += asf->packet_time_delta;
1099 asf->packet_obj_size = asf->packet_frag_size = avio_r8(pb);
1100 asf->packet_size_left--;
1101 asf->packet_multi_size--;
1102 if (asf->packet_multi_size < asf->packet_obj_size) {
1103 asf->packet_time_start = 0;
1104 avio_skip(pb, asf->packet_multi_size);
1105 asf->packet_size_left -= asf->packet_multi_size;
1108 asf->packet_multi_size -= asf->packet_obj_size;
1110 if (asf_st->frag_offset + asf->packet_frag_size <= asf_st->pkt.size &&
1111 asf_st->frag_offset + asf->packet_frag_size > asf->packet_obj_size) {
1112 av_log(s, AV_LOG_INFO, "ignoring invalid packet_obj_size (%d %d %d %d)\n",
1113 asf_st->frag_offset, asf->packet_frag_size,
1114 asf->packet_obj_size, asf_st->pkt.size);
1115 asf->packet_obj_size = asf_st->pkt.size;
1118 if (asf_st->pkt.size != asf->packet_obj_size ||
1119 //FIXME is this condition sufficient?
1120 asf_st->frag_offset + asf->packet_frag_size > asf_st->pkt.size) {
1121 if (asf_st->pkt.data) {
1122 av_log(s, AV_LOG_INFO, "freeing incomplete packet size %d, "
1123 "new %d\n", asf_st->pkt.size, asf->packet_obj_size);
1124 asf_st->frag_offset = 0;
1125 av_free_packet(&asf_st->pkt);
1128 av_new_packet(&asf_st->pkt, asf->packet_obj_size);
1129 asf_st->seq = asf->packet_seq;
1130 asf_st->pkt.dts = asf->packet_frag_timestamp - asf->hdr.preroll;
1131 asf_st->pkt.stream_index = asf->stream_index;
1132 asf_st->pkt.pos = asf_st->packet_pos = asf->packet_pos;
1134 if (asf_st->pkt.data && asf_st->palette_changed) {
1136 pal = av_packet_new_side_data(&asf_st->pkt, AV_PKT_DATA_PALETTE,
1139 av_log(s, AV_LOG_ERROR, "Cannot append palette to packet\n");
1141 memcpy(pal, asf_st->palette, AVPALETTE_SIZE);
1142 asf_st->palette_changed = 0;
1145 av_dlog(asf, "new packet: stream:%d key:%d packet_key:%d audio:%d size:%d\n",
1146 asf->stream_index, asf->packet_key_frame,
1147 asf_st->pkt.flags & AV_PKT_FLAG_KEY,
1148 s->streams[asf->stream_index]->codec->codec_type == AVMEDIA_TYPE_AUDIO,
1149 asf->packet_obj_size);
1150 if (s->streams[asf->stream_index]->codec->codec_type == AVMEDIA_TYPE_AUDIO)
1151 asf->packet_key_frame = 1;
1152 if (asf->packet_key_frame)
1153 asf_st->pkt.flags |= AV_PKT_FLAG_KEY;
1157 av_dlog(asf, "READ PACKET s:%d os:%d o:%d,%d l:%d DATA:%p\n",
1158 s->packet_size, asf_st->pkt.size, asf->packet_frag_offset,
1159 asf_st->frag_offset, asf->packet_frag_size, asf_st->pkt.data);
1160 asf->packet_size_left -= asf->packet_frag_size;
1161 if (asf->packet_size_left < 0)
1164 if (asf->packet_frag_offset >= asf_st->pkt.size ||
1165 asf->packet_frag_size > asf_st->pkt.size - asf->packet_frag_offset) {
1166 av_log(s, AV_LOG_ERROR, "packet fragment position invalid %u,%u not in %u\n",
1167 asf->packet_frag_offset, asf->packet_frag_size, asf_st->pkt.size);
1171 ret = avio_read(pb, asf_st->pkt.data + asf->packet_frag_offset,
1172 asf->packet_frag_size);
1173 if (ret != asf->packet_frag_size) {
1174 if (ret < 0 || asf->packet_frag_offset + ret == 0)
1175 return ret < 0 ? ret : AVERROR_EOF;
1177 if (asf_st->ds_span > 1) {
1178 // scrambling, we can either drop it completely or fill the remainder
1179 // TODO: should we fill the whole packet instead of just the current
1181 memset(asf_st->pkt.data + asf->packet_frag_offset + ret, 0,
1182 asf->packet_frag_size - ret);
1183 ret = asf->packet_frag_size;
1185 // no scrambling, so we can return partial packets
1186 av_shrink_packet(&asf_st->pkt, asf->packet_frag_offset + ret);
1189 if (s->key && s->keylen == 20)
1190 ff_asfcrypt_dec(s->key, asf_st->pkt.data + asf->packet_frag_offset,
1192 asf_st->frag_offset += ret;
1193 /* test if whole packet is read */
1194 if (asf_st->frag_offset == asf_st->pkt.size) {
1195 //workaround for macroshit radio DVR-MS files
1196 if (s->streams[asf->stream_index]->codec->codec_id == AV_CODEC_ID_MPEG2VIDEO &&
1197 asf_st->pkt.size > 100) {
1199 for (i = 0; i < asf_st->pkt.size && !asf_st->pkt.data[i]; i++);
1200 if (i == asf_st->pkt.size) {
1201 av_log(s, AV_LOG_DEBUG, "discarding ms fart\n");
1202 asf_st->frag_offset = 0;
1203 av_free_packet(&asf_st->pkt);
1209 if (asf_st->ds_span > 1) {
1210 if(asf_st->pkt.size != asf_st->ds_packet_size * asf_st->ds_span) {
1211 av_log(s, AV_LOG_ERROR, "pkt.size != ds_packet_size * "
1212 "ds_span (%d %d %d)\n", asf_st->pkt.size,
1213 asf_st->ds_packet_size, asf_st->ds_span);
1215 /* packet descrambling */
1216 uint8_t *newdata = av_malloc(asf_st->pkt.size + FF_INPUT_BUFFER_PADDING_SIZE);
1219 memset(newdata + asf_st->pkt.size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
1220 while (offset < asf_st->pkt.size) {
1221 int off = offset / asf_st->ds_chunk_size;
1222 int row = off / asf_st->ds_span;
1223 int col = off % asf_st->ds_span;
1224 int idx = row + col * asf_st->ds_packet_size / asf_st->ds_chunk_size;
1225 assert(offset + asf_st->ds_chunk_size <= asf_st->pkt.size);
1226 assert(idx+1 <= asf_st->pkt.size / asf_st->ds_chunk_size);
1227 memcpy(newdata + offset,
1228 asf_st->pkt.data + idx * asf_st->ds_chunk_size,
1229 asf_st->ds_chunk_size);
1230 offset += asf_st->ds_chunk_size;
1232 av_free(asf_st->pkt.data);
1233 asf_st->pkt.data = newdata;
1237 asf_st->frag_offset = 0;
1239 asf_st->pkt.size = 0;
1240 asf_st->pkt.data = 0;
1241 asf_st->pkt.side_data_elems = 0;
1242 asf_st->pkt.side_data = NULL;
1243 break; // packet completed
1249 static int asf_read_packet(AVFormatContext *s, AVPacket *pkt)
1251 ASFContext *asf = s->priv_data;
1256 /* parse cached packets, if any */
1257 if ((ret = ff_asf_parse_packet(s, s->pb, pkt)) <= 0)
1259 if ((ret = ff_asf_get_packet(s, s->pb)) < 0)
1260 assert(asf->packet_size_left < FRAME_HEADER_SIZE || asf->packet_segments < 1);
1261 asf->packet_time_start = 0;
1265 // Added to support seeking after packets have been read
1266 // If information is not reset, read_packet fails due to
1267 // leftover information from previous reads
1268 static void asf_reset_header(AVFormatContext *s)
1270 ASFContext *asf = s->priv_data;
1274 asf->packet_size_left = 0;
1275 asf->packet_segments = 0;
1276 asf->packet_flags = 0;
1277 asf->packet_property = 0;
1278 asf->packet_timestamp = 0;
1279 asf->packet_segsizetype = 0;
1280 asf->packet_segments = 0;
1281 asf->packet_seq = 0;
1282 asf->packet_replic_size = 0;
1283 asf->packet_key_frame = 0;
1284 asf->packet_padsize = 0;
1285 asf->packet_frag_offset = 0;
1286 asf->packet_frag_size = 0;
1287 asf->packet_frag_timestamp = 0;
1288 asf->packet_multi_size = 0;
1289 asf->packet_obj_size = 0;
1290 asf->packet_time_delta = 0;
1291 asf->packet_time_start = 0;
1293 for(i=0; i<s->nb_streams; i++){
1294 asf_st= s->streams[i]->priv_data;
1295 av_free_packet(&asf_st->pkt);
1296 asf_st->frag_offset=0;
1302 static int asf_read_close(AVFormatContext *s)
1304 asf_reset_header(s);
1309 static int64_t asf_read_pts(AVFormatContext *s, int stream_index, int64_t *ppos, int64_t pos_limit)
1311 AVPacket pkt1, *pkt = &pkt1;
1316 int64_t start_pos[ASF_MAX_STREAMS];
1318 for(i=0; i<s->nb_streams; i++){
1322 if (s->packet_size > 0)
1323 pos= (pos+s->packet_size-1-s->data_offset)/s->packet_size*s->packet_size+ s->data_offset;
1325 if (avio_seek(s->pb, pos, SEEK_SET) < 0)
1326 return AV_NOPTS_VALUE;
1328 asf_reset_header(s);
1330 if (av_read_frame(s, pkt) < 0){
1331 av_log(s, AV_LOG_INFO, "asf_read_pts failed\n");
1332 return AV_NOPTS_VALUE;
1337 av_free_packet(pkt);
1338 if(pkt->flags&AV_PKT_FLAG_KEY){
1339 i= pkt->stream_index;
1341 asf_st= s->streams[i]->priv_data;
1343 // assert((asf_st->packet_pos - s->data_offset) % s->packet_size == 0);
1344 pos= asf_st->packet_pos;
1346 av_add_index_entry(s->streams[i], pos, pts, pkt->size, pos - start_pos[i] + 1, AVINDEX_KEYFRAME);
1347 start_pos[i]= asf_st->packet_pos + 1;
1349 if(pkt->stream_index == stream_index)
1358 static void asf_build_simple_index(AVFormatContext *s, int stream_index)
1361 ASFContext *asf = s->priv_data;
1362 int64_t current_pos= avio_tell(s->pb);
1364 if(avio_seek(s->pb, asf->data_object_offset + asf->data_object_size, SEEK_SET) < 0) {
1365 asf->index_read= -1;
1369 ff_get_guid(s->pb, &g);
1371 /* the data object can be followed by other top-level objects,
1372 skip them until the simple index object is reached */
1373 while (ff_guidcmp(&g, &ff_asf_simple_index_header)) {
1374 int64_t gsize= avio_rl64(s->pb);
1375 if (gsize < 24 || url_feof(s->pb)) {
1376 avio_seek(s->pb, current_pos, SEEK_SET);
1377 asf->index_read= -1;
1380 avio_skip(s->pb, gsize-24);
1381 ff_get_guid(s->pb, &g);
1385 int64_t itime, last_pos=-1;
1388 int64_t av_unused gsize= avio_rl64(s->pb);
1389 ff_get_guid(s->pb, &g);
1390 itime=avio_rl64(s->pb);
1391 pct=avio_rl32(s->pb);
1392 ict=avio_rl32(s->pb);
1393 av_log(s, AV_LOG_DEBUG, "itime:0x%"PRIx64", pct:%d, ict:%d\n",itime,pct,ict);
1395 for (i=0;i<ict;i++){
1396 int pktnum=avio_rl32(s->pb);
1397 int pktct =avio_rl16(s->pb);
1398 int64_t pos = s->data_offset + s->packet_size*(int64_t)pktnum;
1399 int64_t index_pts= FFMAX(av_rescale(itime, i, 10000) - asf->hdr.preroll, 0);
1401 if(pos != last_pos){
1402 av_log(s, AV_LOG_DEBUG, "pktnum:%d, pktct:%d pts: %"PRId64"\n", pktnum, pktct, index_pts);
1403 av_add_index_entry(s->streams[stream_index], pos, index_pts, s->packet_size, 0, AVINDEX_KEYFRAME);
1407 asf->index_read= ict > 1;
1409 avio_seek(s->pb, current_pos, SEEK_SET);
1412 static int asf_read_seek(AVFormatContext *s, int stream_index, int64_t pts, int flags)
1414 ASFContext *asf = s->priv_data;
1415 AVStream *st = s->streams[stream_index];
1417 if (s->packet_size <= 0)
1420 /* Try using the protocol's read_seek if available */
1422 int ret = avio_seek_time(s->pb, stream_index, pts, flags);
1424 asf_reset_header(s);
1425 if (ret != AVERROR(ENOSYS))
1429 if (!asf->index_read)
1430 asf_build_simple_index(s, stream_index);
1432 if((asf->index_read > 0 && st->index_entries)){
1433 int index= av_index_search_timestamp(st, pts, flags);
1435 /* find the position */
1436 uint64_t pos = st->index_entries[index].pos;
1439 av_log(s, AV_LOG_DEBUG, "SEEKTO: %"PRId64"\n", pos);
1440 if(avio_seek(s->pb, pos, SEEK_SET) < 0)
1442 asf_reset_header(s);
1446 /* no index or seeking by index failed */
1447 if (ff_seek_frame_binary(s, stream_index, pts, flags) < 0)
1449 asf_reset_header(s);
1453 AVInputFormat ff_asf_demuxer = {
1455 .long_name = NULL_IF_CONFIG_SMALL("ASF (Advanced / Active Streaming Format)"),
1456 .priv_data_size = sizeof(ASFContext),
1457 .read_probe = asf_probe,
1458 .read_header = asf_read_header,
1459 .read_packet = asf_read_packet,
1460 .read_close = asf_read_close,
1461 .read_seek = asf_read_seek,
1462 .read_timestamp = asf_read_pts,
1463 .flags = AVFMT_NOBINSEARCH | AVFMT_NOGENSEARCH,
1464 .priv_class = &asf_class,