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 st->duration = asf->hdr.play_time /
355 (10000000 / 1000) - start_time;
359 test_for_ext_stream_audio = 0;
360 if (!ff_guidcmp(&g, &ff_asf_audio_stream)) {
361 type = AVMEDIA_TYPE_AUDIO;
362 } else if (!ff_guidcmp(&g, &ff_asf_video_stream)) {
363 type = AVMEDIA_TYPE_VIDEO;
364 } else if (!ff_guidcmp(&g, &ff_asf_jfif_media)) {
365 type = AVMEDIA_TYPE_VIDEO;
366 st->codec->codec_id = AV_CODEC_ID_MJPEG;
367 } else if (!ff_guidcmp(&g, &ff_asf_command_stream)) {
368 type = AVMEDIA_TYPE_DATA;
369 } else if (!ff_guidcmp(&g, &ff_asf_ext_stream_embed_stream_header)) {
370 test_for_ext_stream_audio = 1;
371 type = AVMEDIA_TYPE_UNKNOWN;
376 avio_skip(pb, 8); /* total_size */
377 type_specific_size = avio_rl32(pb);
379 st->id = avio_rl16(pb) & 0x7f; /* stream id */
380 // mapping of asf ID to AV stream ID;
381 asf->asfid2avid[st->id] = s->nb_streams - 1;
385 if (test_for_ext_stream_audio) {
387 if (!ff_guidcmp(&g, &ff_asf_ext_stream_audio_stream)) {
388 type = AVMEDIA_TYPE_AUDIO;
399 st->codec->codec_type = type;
400 if (type == AVMEDIA_TYPE_AUDIO) {
401 int ret = ff_get_wav_header(pb, st->codec, type_specific_size);
404 if (is_dvr_ms_audio) {
405 // codec_id and codec_tag are unreliable in dvr_ms
406 // files. Set them later by probing stream.
407 st->request_probe= 1;
408 st->codec->codec_tag = 0;
410 if (st->codec->codec_id == AV_CODEC_ID_AAC) {
411 st->need_parsing = AVSTREAM_PARSE_NONE;
413 st->need_parsing = AVSTREAM_PARSE_FULL;
415 /* We have to init the frame size at some point .... */
416 pos2 = avio_tell(pb);
417 if (size >= (pos2 + 8 - pos1 + 24)) {
418 asf_st->ds_span = avio_r8(pb);
419 asf_st->ds_packet_size = avio_rl16(pb);
420 asf_st->ds_chunk_size = avio_rl16(pb);
421 avio_rl16(pb); //ds_data_size
422 avio_r8(pb); //ds_silence_data
424 if (asf_st->ds_span > 1) {
425 if (!asf_st->ds_chunk_size
426 || (asf_st->ds_packet_size/asf_st->ds_chunk_size <= 1)
427 || asf_st->ds_packet_size % asf_st->ds_chunk_size)
428 asf_st->ds_span = 0; // disable descrambling
430 } else if (type == AVMEDIA_TYPE_VIDEO &&
431 size - (avio_tell(pb) - pos1 + 24) >= 51) {
435 avio_rl16(pb); /* size */
436 sizeX= avio_rl32(pb); /* size */
437 st->codec->width = avio_rl32(pb);
438 st->codec->height = avio_rl32(pb);
439 /* not available for asf */
440 avio_rl16(pb); /* panes */
441 st->codec->bits_per_coded_sample = avio_rl16(pb); /* depth */
442 tag1 = avio_rl32(pb);
445 st->codec->extradata_size = sizeX - 40;
446 st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
447 avio_read(pb, st->codec->extradata, st->codec->extradata_size);
450 /* Extract palette from extradata if bpp <= 8 */
451 /* This code assumes that extradata contains only palette */
452 /* This is true for all paletted codecs implemented in libavcodec */
453 if (st->codec->extradata_size && (st->codec->bits_per_coded_sample <= 8)) {
456 for (i = 0; i < FFMIN(st->codec->extradata_size, AVPALETTE_SIZE)/4; i++)
457 asf_st->palette[i] = av_bswap32(((uint32_t*)st->codec->extradata)[i]);
459 memcpy(asf_st->palette, st->codec->extradata,
460 FFMIN(st->codec->extradata_size, AVPALETTE_SIZE));
462 asf_st->palette_changed = 1;
465 st->codec->codec_tag = tag1;
466 st->codec->codec_id = ff_codec_get_id(ff_codec_bmp_tags, tag1);
467 if(tag1 == MKTAG('D', 'V', 'R', ' ')){
468 st->need_parsing = AVSTREAM_PARSE_FULL;
469 // 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
471 st->codec->height = 0;
472 av_freep(&st->codec->extradata);
473 st->codec->extradata_size=0;
475 if(st->codec->codec_id == AV_CODEC_ID_H264)
476 st->need_parsing = AVSTREAM_PARSE_FULL_ONCE;
478 pos2 = avio_tell(pb);
479 avio_skip(pb, size - (pos2 - pos1 + 24));
484 static int asf_read_ext_stream_properties(AVFormatContext *s, int64_t size)
486 ASFContext *asf = s->priv_data;
487 AVIOContext *pb = s->pb;
489 int ext_len, payload_ext_ct, stream_ct, i;
490 uint32_t leak_rate, stream_num;
491 unsigned int stream_languageid_index;
493 avio_rl64(pb); // starttime
494 avio_rl64(pb); // endtime
495 leak_rate = avio_rl32(pb); // leak-datarate
496 avio_rl32(pb); // bucket-datasize
497 avio_rl32(pb); // init-bucket-fullness
498 avio_rl32(pb); // alt-leak-datarate
499 avio_rl32(pb); // alt-bucket-datasize
500 avio_rl32(pb); // alt-init-bucket-fullness
501 avio_rl32(pb); // max-object-size
502 avio_rl32(pb); // flags (reliable,seekable,no_cleanpoints?,resend-live-cleanpoints, rest of bits reserved)
503 stream_num = avio_rl16(pb); // stream-num
505 stream_languageid_index = avio_rl16(pb); // stream-language-id-index
506 if (stream_num < 128)
507 asf->streams[stream_num].stream_language_index = stream_languageid_index;
509 avio_rl64(pb); // avg frametime in 100ns units
510 stream_ct = avio_rl16(pb); //stream-name-count
511 payload_ext_ct = avio_rl16(pb); //payload-extension-system-count
513 if (stream_num < 128)
514 asf->stream_bitrates[stream_num] = leak_rate;
516 for (i=0; i<stream_ct; i++){
518 ext_len = avio_rl16(pb);
519 avio_skip(pb, ext_len);
522 for (i=0; i<payload_ext_ct; i++){
525 ext_len=avio_rl32(pb);
526 avio_skip(pb, ext_len);
532 static int asf_read_content_desc(AVFormatContext *s, int64_t size)
534 AVIOContext *pb = s->pb;
535 int len1, len2, len3, len4, len5;
537 len1 = avio_rl16(pb);
538 len2 = avio_rl16(pb);
539 len3 = avio_rl16(pb);
540 len4 = avio_rl16(pb);
541 len5 = avio_rl16(pb);
542 get_tag(s, "title" , 0, len1);
543 get_tag(s, "author" , 0, len2);
544 get_tag(s, "copyright", 0, len3);
545 get_tag(s, "comment" , 0, len4);
551 static int asf_read_ext_content_desc(AVFormatContext *s, int64_t size)
553 AVIOContext *pb = s->pb;
554 ASFContext *asf = s->priv_data;
555 int desc_count, i, ret;
557 desc_count = avio_rl16(pb);
558 for(i=0;i<desc_count;i++) {
559 int name_len,value_type,value_len;
562 name_len = avio_rl16(pb);
563 if (name_len%2) // must be even, broken lavf versions wrote len-1
565 if ((ret = avio_get_str16le(pb, name_len, name, sizeof(name))) < name_len)
566 avio_skip(pb, name_len - ret);
567 value_type = avio_rl16(pb);
568 value_len = avio_rl16(pb);
569 if (!value_type && value_len%2)
572 * My sample has that stream set to 0 maybe that mean the container.
573 * Asf stream count start at 1. I am using 0 to the container value since it's unused
575 if (!strcmp(name, "AspectRatioX")){
576 asf->dar[0].num= get_value(s->pb, value_type);
577 } else if(!strcmp(name, "AspectRatioY")){
578 asf->dar[0].den= get_value(s->pb, value_type);
580 get_tag(s, name, value_type, value_len);
586 static int asf_read_language_list(AVFormatContext *s, int64_t size)
588 AVIOContext *pb = s->pb;
589 ASFContext *asf = s->priv_data;
591 int stream_count = avio_rl16(pb);
592 for(j = 0; j < stream_count; j++) {
594 unsigned int lang_len = avio_r8(pb);
595 if ((ret = avio_get_str16le(pb, lang_len, lang, sizeof(lang))) < lang_len)
596 avio_skip(pb, lang_len - ret);
598 av_strlcpy(asf->stream_languages[j], lang, sizeof(*asf->stream_languages));
604 static int asf_read_metadata(AVFormatContext *s, int64_t size)
606 AVIOContext *pb = s->pb;
607 ASFContext *asf = s->priv_data;
608 int n, stream_num, name_len, value_len, value_num;
614 int av_unused value_type;
616 avio_rl16(pb); //lang_list_index
617 stream_num= avio_rl16(pb);
618 name_len= avio_rl16(pb);
619 value_type = avio_rl16(pb); /* value_type */
620 value_len= avio_rl32(pb);
622 if ((ret = avio_get_str16le(pb, name_len, name, sizeof(name))) < name_len)
623 avio_skip(pb, name_len - ret);
624 av_dlog(s, "%d %d %d %d %d <%s>\n",
625 i, stream_num, name_len, value_type, value_len, name);
626 value_num= avio_rl16(pb);//we should use get_value() here but it does not work 2 is le16 here but le32 elsewhere
627 avio_skip(pb, value_len - 2);
630 if (!strcmp(name, "AspectRatioX")) asf->dar[stream_num].num= value_num;
631 else if(!strcmp(name, "AspectRatioY")) asf->dar[stream_num].den= value_num;
638 static int asf_read_marker(AVFormatContext *s, int64_t size)
640 AVIOContext *pb = s->pb;
641 int i, count, name_len, ret;
644 avio_rl64(pb); // reserved 16 bytes
645 avio_rl64(pb); // ...
646 count = avio_rl32(pb); // markers count
647 avio_rl16(pb); // reserved 2 bytes
648 name_len = avio_rl16(pb); // name length
649 for(i=0;i<name_len;i++){
650 avio_r8(pb); // skip the name
653 for(i=0;i<count;i++){
657 avio_rl64(pb); // offset, 8 bytes
658 pres_time = avio_rl64(pb); // presentation time
659 avio_rl16(pb); // entry length
660 avio_rl32(pb); // send time
661 avio_rl32(pb); // flags
662 name_len = avio_rl32(pb); // name length
663 if ((ret = avio_get_str16le(pb, name_len * 2, name, sizeof(name))) < name_len)
664 avio_skip(pb, name_len - ret);
665 avpriv_new_chapter(s, i, (AVRational){1, 10000000}, pres_time, AV_NOPTS_VALUE, name );
671 static int asf_read_header(AVFormatContext *s)
673 ASFContext *asf = s->priv_data;
675 AVIOContext *pb = s->pb;
680 if (ff_guidcmp(&g, &ff_asf_header))
681 return AVERROR_INVALIDDATA;
686 memset(&asf->asfid2avid, -1, sizeof(asf->asfid2avid));
688 uint64_t gpos= avio_tell(pb);
690 gsize = avio_rl64(pb);
692 if (!ff_guidcmp(&g, &ff_asf_data_header)) {
693 asf->data_object_offset = avio_tell(pb);
694 // if not streaming, gsize is not unlimited (how?), and there is enough space in the file..
695 if (!(asf->hdr.flags & 0x01) && gsize >= 100) {
696 asf->data_object_size = gsize - 24;
698 asf->data_object_size = (uint64_t)-1;
703 return AVERROR_INVALIDDATA;
704 if (!ff_guidcmp(&g, &ff_asf_file_header)) {
705 int ret = asf_read_file_properties(s, gsize);
708 } else if (!ff_guidcmp(&g, &ff_asf_stream_header)) {
709 asf_read_stream_properties(s, gsize);
710 } else if (!ff_guidcmp(&g, &ff_asf_comment_header)) {
711 asf_read_content_desc(s, gsize);
712 } else if (!ff_guidcmp(&g, &ff_asf_language_guid)) {
713 asf_read_language_list(s, gsize);
714 } else if (!ff_guidcmp(&g, &ff_asf_extended_content_header)) {
715 asf_read_ext_content_desc(s, gsize);
716 } else if (!ff_guidcmp(&g, &ff_asf_metadata_header)) {
717 asf_read_metadata(s, gsize);
718 } else if (!ff_guidcmp(&g, &ff_asf_ext_stream_header)) {
719 asf_read_ext_stream_properties(s, gsize);
721 // there could be a optional stream properties object to follow
722 // if so the next iteration will pick it up
724 } else if (!ff_guidcmp(&g, &ff_asf_head1_guid)) {
728 } else if (!ff_guidcmp(&g, &ff_asf_marker_header)) {
729 asf_read_marker(s, gsize);
730 } else if (url_feof(pb)) {
734 if (!ff_guidcmp(&g, &ff_asf_content_encryption)) {
737 av_log(s, AV_LOG_WARNING, "DRM protected stream detected, decoding will likely fail!\n");
739 av_log(s, AV_LOG_DEBUG, "Secret data:\n");
740 av_get_packet(pb, &pkt, len); av_hex_dump_log(s, AV_LOG_DEBUG, pkt.data, pkt.size); av_free_packet(&pkt);
742 get_tag(s, "ASF_Protection_Type", -1, len);
744 get_tag(s, "ASF_Key_ID", -1, len);
746 get_tag(s, "ASF_License_URL", -1, len);
747 } else if (!ff_guidcmp(&g, &ff_asf_ext_content_encryption)) {
748 av_log(s, AV_LOG_WARNING, "Ext DRM protected stream detected, decoding will likely fail!\n");
749 av_dict_set(&s->metadata, "encryption", "ASF Extended Content Encryption", 0);
750 } else if (!ff_guidcmp(&g, &ff_asf_digital_signature)) {
751 av_log(s, AV_LOG_INFO, "Digital signature detected!\n");
755 if(avio_tell(pb) != gpos + gsize)
756 av_log(s, AV_LOG_DEBUG, "gpos mismatch our pos=%"PRIu64", end=%"PRIu64"\n", avio_tell(pb)-gpos, gsize);
757 avio_seek(pb, gpos + gsize, SEEK_SET);
765 asf->data_offset = avio_tell(pb);
766 asf->packet_size_left = 0;
769 for(i=0; i<128; i++){
770 int stream_num= asf->asfid2avid[i];
772 AVStream *st = s->streams[stream_num];
773 if (!st->codec->bit_rate)
774 st->codec->bit_rate = asf->stream_bitrates[i];
775 if (asf->dar[i].num > 0 && asf->dar[i].den > 0){
776 av_reduce(&st->sample_aspect_ratio.den,
777 &st->sample_aspect_ratio.num,
778 asf->dar[i].num, asf->dar[i].den, INT_MAX);
779 } 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.
780 av_reduce(&st->sample_aspect_ratio.den,
781 &st->sample_aspect_ratio.num,
782 asf->dar[0].num, asf->dar[0].den, INT_MAX);
784 av_dlog(s, "i=%d, st->codec->codec_type:%d, asf->dar %d:%d sar=%d:%d\n",
785 i, st->codec->codec_type, asf->dar[i].num, asf->dar[i].den,
786 st->sample_aspect_ratio.num, st->sample_aspect_ratio.den);
788 // copy and convert language codes to the frontend
789 if (asf->streams[i].stream_language_index < 128) {
790 const char *rfc1766 = asf->stream_languages[asf->streams[i].stream_language_index];
791 if (rfc1766 && strlen(rfc1766) > 1) {
792 const char primary_tag[3] = { rfc1766[0], rfc1766[1], '\0' }; // ignore country code if any
793 const char *iso6392 = av_convert_lang_to(primary_tag, AV_LANG_ISO639_2_BIBL);
795 av_dict_set(&st->metadata, "language", iso6392, 0);
801 ff_metadata_conv(&s->metadata, NULL, ff_asf_metadata_conv);
806 #define DO_2BITS(bits, var, defval) \
809 case 3: var = avio_rl32(pb); rsize += 4; break; \
810 case 2: var = avio_rl16(pb); rsize += 2; break; \
811 case 1: var = avio_r8(pb); rsize++; break; \
812 default: var = defval; break; \
816 * Load a single ASF packet into the demuxer.
817 * @param s demux context
818 * @param pb context to read data from
819 * @return 0 on success, <0 on error
821 static int ff_asf_get_packet(AVFormatContext *s, AVIOContext *pb)
823 ASFContext *asf = s->priv_data;
824 uint32_t packet_length, padsize;
828 // if we do not know packet size, allow skipping up to 32 kB
830 if (asf->no_resync_search)
832 else if (s->packet_size > 0)
833 off= (avio_tell(pb) - s->data_offset) % s->packet_size + 3;
839 if(c == 0x82 && !d && !e)
845 * This code allows handling of -EAGAIN at packet boundaries (i.e.
846 * if the packet sync code above triggers -EAGAIN). This does not
847 * imply complete -EAGAIN handling support at random positions in
850 if (pb->error == AVERROR(EAGAIN))
851 return AVERROR(EAGAIN);
853 av_log(s, AV_LOG_ERROR, "ff asf bad header %x at:%"PRId64"\n", c, avio_tell(pb));
855 if ((c & 0x8f) == 0x82) {
858 av_log(s, AV_LOG_ERROR, "ff asf bad non zero\n");
859 return AVERROR_INVALIDDATA;
864 }else if(!url_feof(pb)){
865 avio_seek(pb, -1, SEEK_CUR); //FIXME
868 asf->packet_flags = c;
869 asf->packet_property = d;
871 DO_2BITS(asf->packet_flags >> 5, packet_length, s->packet_size);
872 DO_2BITS(asf->packet_flags >> 1, padsize, 0); // sequence ignored
873 DO_2BITS(asf->packet_flags >> 3, padsize, 0); // padding length
875 //the following checks prevent overflows and infinite loops
876 if(!packet_length || packet_length >= (1U<<29)){
877 av_log(s, AV_LOG_ERROR, "invalid packet_length %d at:%"PRId64"\n", packet_length, avio_tell(pb));
878 return AVERROR_INVALIDDATA;
880 if(padsize >= packet_length){
881 av_log(s, AV_LOG_ERROR, "invalid padsize %d at:%"PRId64"\n", padsize, avio_tell(pb));
882 return AVERROR_INVALIDDATA;
885 asf->packet_timestamp = avio_rl32(pb);
886 avio_rl16(pb); /* duration */
887 // rsize has at least 11 bytes which have to be present
889 if (asf->packet_flags & 0x01) {
890 asf->packet_segsizetype = avio_r8(pb); rsize++;
891 asf->packet_segments = asf->packet_segsizetype & 0x3f;
893 asf->packet_segments = 1;
894 asf->packet_segsizetype = 0x80;
896 if (rsize > packet_length - padsize) {
897 asf->packet_size_left = 0;
898 av_log(s, AV_LOG_ERROR,
899 "invalid packet header length %d for pktlen %d-%d at %"PRId64"\n",
900 rsize, packet_length, padsize, avio_tell(pb));
901 return AVERROR_INVALIDDATA;
903 asf->packet_size_left = packet_length - padsize - rsize;
904 if (packet_length < asf->hdr.min_pktsize)
905 padsize += asf->hdr.min_pktsize - packet_length;
906 asf->packet_padsize = padsize;
907 av_dlog(s, "packet: size=%d padsize=%d left=%d\n", s->packet_size, asf->packet_padsize, asf->packet_size_left);
913 * @return <0 if error
915 static int asf_read_frame_header(AVFormatContext *s, AVIOContext *pb){
916 ASFContext *asf = s->priv_data;
918 int num = avio_r8(pb);
919 int64_t ts0, ts1 av_unused;
921 asf->packet_segments--;
922 asf->packet_key_frame = num >> 7;
923 asf->stream_index = asf->asfid2avid[num & 0x7f];
924 // sequence should be ignored!
925 DO_2BITS(asf->packet_property >> 4, asf->packet_seq, 0);
926 DO_2BITS(asf->packet_property >> 2, asf->packet_frag_offset, 0);
927 DO_2BITS(asf->packet_property, asf->packet_replic_size, 0);
928 av_dlog(asf, "key:%d stream:%d seq:%d offset:%d replic_size:%d\n",
929 asf->packet_key_frame, asf->stream_index, asf->packet_seq,
930 asf->packet_frag_offset, asf->packet_replic_size);
931 if (rsize+asf->packet_replic_size > asf->packet_size_left) {
932 av_log(s, AV_LOG_ERROR, "packet_replic_size %d is invalid\n", asf->packet_replic_size);
933 return AVERROR_INVALIDDATA;
935 if (asf->packet_replic_size >= 8) {
936 asf->packet_obj_size = avio_rl32(pb);
937 if(asf->packet_obj_size >= (1<<24) || asf->packet_obj_size <= 0){
938 av_log(s, AV_LOG_ERROR, "packet_obj_size invalid\n");
939 return AVERROR_INVALIDDATA;
941 asf->packet_frag_timestamp = avio_rl32(pb); // timestamp
942 if(asf->packet_replic_size >= 8+38+4){
948 avio_skip(pb, asf->packet_replic_size - 8 - 38 - 4);
949 if(ts0!= -1) asf->packet_frag_timestamp= ts0/10000;
950 else asf->packet_frag_timestamp= AV_NOPTS_VALUE;
952 avio_skip(pb, asf->packet_replic_size - 8);
953 rsize += asf->packet_replic_size; // FIXME - check validity
954 } else if (asf->packet_replic_size==1){
955 // multipacket - frag_offset is beginning timestamp
956 asf->packet_time_start = asf->packet_frag_offset;
957 asf->packet_frag_offset = 0;
958 asf->packet_frag_timestamp = asf->packet_timestamp;
960 asf->packet_time_delta = avio_r8(pb);
962 }else if(asf->packet_replic_size!=0){
963 av_log(s, AV_LOG_ERROR, "unexpected packet_replic_size of %d\n", asf->packet_replic_size);
964 return AVERROR_INVALIDDATA;
966 if (asf->packet_flags & 0x01) {
967 DO_2BITS(asf->packet_segsizetype >> 6, asf->packet_frag_size, 0); // 0 is illegal
968 if (rsize > asf->packet_size_left) {
969 av_log(s, AV_LOG_ERROR, "packet_replic_size is invalid\n");
970 return AVERROR_INVALIDDATA;
971 } else if(asf->packet_frag_size > asf->packet_size_left - rsize){
972 if (asf->packet_frag_size > asf->packet_size_left - rsize + asf->packet_padsize) {
973 av_log(s, AV_LOG_ERROR, "packet_frag_size is invalid (%d-%d)\n", asf->packet_size_left, rsize);
974 return AVERROR_INVALIDDATA;
976 int diff = asf->packet_frag_size - (asf->packet_size_left - rsize);
977 asf->packet_size_left += diff;
978 asf->packet_padsize -= diff;
982 asf->packet_frag_size = asf->packet_size_left - rsize;
984 if (asf->packet_replic_size == 1) {
985 asf->packet_multi_size = asf->packet_frag_size;
986 if (asf->packet_multi_size > asf->packet_size_left)
987 return AVERROR_INVALIDDATA;
989 asf->packet_size_left -= rsize;
995 * Parse data from individual ASF packets (which were previously loaded
996 * with asf_get_packet()).
997 * @param s demux context
998 * @param pb context to read data from
999 * @param pkt pointer to store packet data into
1000 * @return 0 if data was stored in pkt, <0 on error or 1 if more ASF
1001 * packets need to be loaded (through asf_get_packet())
1003 static int ff_asf_parse_packet(AVFormatContext *s, AVIOContext *pb, AVPacket *pkt)
1005 ASFContext *asf = s->priv_data;
1006 ASFStream *asf_st = 0;
1011 if (asf->packet_size_left < FRAME_HEADER_SIZE
1012 || asf->packet_segments < 1) {
1013 //asf->packet_size_left <= asf->packet_padsize) {
1014 int ret = asf->packet_size_left + asf->packet_padsize;
1019 asf->packet_pos= avio_tell(pb);
1020 if (asf->data_object_size != (uint64_t)-1 &&
1021 (asf->packet_pos - asf->data_object_offset >= asf->data_object_size))
1022 return AVERROR_EOF; /* Do not exceed the size of the data object */
1025 if (asf->packet_time_start == 0) {
1026 if(asf_read_frame_header(s, pb) < 0){
1027 asf->packet_segments= 0;
1030 if (asf->stream_index < 0
1031 || s->streams[asf->stream_index]->discard >= AVDISCARD_ALL
1032 || (!asf->packet_key_frame && s->streams[asf->stream_index]->discard >= AVDISCARD_NONKEY)
1034 asf->packet_time_start = 0;
1035 /* unhandled packet (should not happen) */
1036 avio_skip(pb, asf->packet_frag_size);
1037 asf->packet_size_left -= asf->packet_frag_size;
1038 if(asf->stream_index < 0)
1039 av_log(s, AV_LOG_ERROR, "ff asf skip %d (unknown stream)\n", asf->packet_frag_size);
1042 asf->asf_st = s->streams[asf->stream_index]->priv_data;
1044 asf_st = asf->asf_st;
1046 if (asf->packet_replic_size == 1) {
1047 // frag_offset is here used as the beginning timestamp
1048 asf->packet_frag_timestamp = asf->packet_time_start;
1049 asf->packet_time_start += asf->packet_time_delta;
1050 asf->packet_obj_size = asf->packet_frag_size = avio_r8(pb);
1051 asf->packet_size_left--;
1052 asf->packet_multi_size--;
1053 if (asf->packet_multi_size < asf->packet_obj_size)
1055 asf->packet_time_start = 0;
1056 avio_skip(pb, asf->packet_multi_size);
1057 asf->packet_size_left -= asf->packet_multi_size;
1060 asf->packet_multi_size -= asf->packet_obj_size;
1062 if( /*asf->packet_frag_size == asf->packet_obj_size*/
1063 asf_st->frag_offset + asf->packet_frag_size <= asf_st->pkt.size
1064 && asf_st->frag_offset + asf->packet_frag_size > asf->packet_obj_size){
1065 av_log(s, AV_LOG_INFO, "ignoring invalid packet_obj_size (%d %d %d %d)\n",
1066 asf_st->frag_offset, asf->packet_frag_size,
1067 asf->packet_obj_size, asf_st->pkt.size);
1068 asf->packet_obj_size= asf_st->pkt.size;
1071 if ( asf_st->pkt.size != asf->packet_obj_size
1072 || asf_st->frag_offset + asf->packet_frag_size > asf_st->pkt.size) { //FIXME is this condition sufficient?
1073 if(asf_st->pkt.data){
1074 av_log(s, AV_LOG_INFO, "freeing incomplete packet size %d, new %d\n", asf_st->pkt.size, asf->packet_obj_size);
1075 asf_st->frag_offset = 0;
1076 av_free_packet(&asf_st->pkt);
1079 av_new_packet(&asf_st->pkt, asf->packet_obj_size);
1080 asf_st->seq = asf->packet_seq;
1081 asf_st->pkt.dts = asf->packet_frag_timestamp - asf->hdr.preroll;
1082 asf_st->pkt.stream_index = asf->stream_index;
1084 asf_st->packet_pos= asf->packet_pos;
1085 if (asf_st->pkt.data && asf_st->palette_changed) {
1087 pal = av_packet_new_side_data(&asf_st->pkt, AV_PKT_DATA_PALETTE,
1090 av_log(s, AV_LOG_ERROR, "Cannot append palette to packet\n");
1092 memcpy(pal, asf_st->palette, AVPALETTE_SIZE);
1093 asf_st->palette_changed = 0;
1096 av_dlog(asf, "new packet: stream:%d key:%d packet_key:%d audio:%d size:%d\n",
1097 asf->stream_index, asf->packet_key_frame,
1098 asf_st->pkt.flags & AV_PKT_FLAG_KEY,
1099 s->streams[asf->stream_index]->codec->codec_type == AVMEDIA_TYPE_AUDIO,
1100 asf->packet_obj_size);
1101 if (s->streams[asf->stream_index]->codec->codec_type == AVMEDIA_TYPE_AUDIO)
1102 asf->packet_key_frame = 1;
1103 if (asf->packet_key_frame)
1104 asf_st->pkt.flags |= AV_PKT_FLAG_KEY;
1108 av_dlog(asf, "READ PACKET s:%d os:%d o:%d,%d l:%d DATA:%p\n",
1109 s->packet_size, asf_st->pkt.size, asf->packet_frag_offset,
1110 asf_st->frag_offset, asf->packet_frag_size, asf_st->pkt.data);
1111 asf->packet_size_left -= asf->packet_frag_size;
1112 if (asf->packet_size_left < 0)
1115 if( asf->packet_frag_offset >= asf_st->pkt.size
1116 || asf->packet_frag_size > asf_st->pkt.size - asf->packet_frag_offset){
1117 av_log(s, AV_LOG_ERROR, "packet fragment position invalid %u,%u not in %u\n",
1118 asf->packet_frag_offset, asf->packet_frag_size, asf_st->pkt.size);
1122 ret = avio_read(pb, asf_st->pkt.data + asf->packet_frag_offset,
1123 asf->packet_frag_size);
1124 if (ret != asf->packet_frag_size) {
1125 if (ret < 0 || asf->packet_frag_offset + ret == 0)
1126 return ret < 0 ? ret : AVERROR_EOF;
1127 if (asf_st->ds_span > 1) {
1128 // scrambling, we can either drop it completely or fill the remainder
1129 // TODO: should we fill the whole packet instead of just the current
1131 memset(asf_st->pkt.data + asf->packet_frag_offset + ret, 0,
1132 asf->packet_frag_size - ret);
1133 ret = asf->packet_frag_size;
1135 // no scrambling, so we can return partial packets
1136 av_shrink_packet(&asf_st->pkt, asf->packet_frag_offset + ret);
1138 if (s->key && s->keylen == 20)
1139 ff_asfcrypt_dec(s->key, asf_st->pkt.data + asf->packet_frag_offset,
1141 asf_st->frag_offset += ret;
1142 /* test if whole packet is read */
1143 if (asf_st->frag_offset == asf_st->pkt.size) {
1144 //workaround for macroshit radio DVR-MS files
1145 if( s->streams[asf->stream_index]->codec->codec_id == AV_CODEC_ID_MPEG2VIDEO
1146 && asf_st->pkt.size > 100){
1148 for(i=0; i<asf_st->pkt.size && !asf_st->pkt.data[i]; i++);
1149 if(i == asf_st->pkt.size){
1150 av_log(s, AV_LOG_DEBUG, "discarding ms fart\n");
1151 asf_st->frag_offset = 0;
1152 av_free_packet(&asf_st->pkt);
1158 if (asf_st->ds_span > 1) {
1159 if(asf_st->pkt.size != asf_st->ds_packet_size * asf_st->ds_span){
1160 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);
1162 /* packet descrambling */
1163 uint8_t *newdata = av_malloc(asf_st->pkt.size + FF_INPUT_BUFFER_PADDING_SIZE);
1166 memset(newdata + asf_st->pkt.size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
1167 while (offset < asf_st->pkt.size) {
1168 int off = offset / asf_st->ds_chunk_size;
1169 int row = off / asf_st->ds_span;
1170 int col = off % asf_st->ds_span;
1171 int idx = row + col * asf_st->ds_packet_size / asf_st->ds_chunk_size;
1172 assert(offset + asf_st->ds_chunk_size <= asf_st->pkt.size);
1173 assert(idx+1 <= asf_st->pkt.size / asf_st->ds_chunk_size);
1174 memcpy(newdata + offset,
1175 asf_st->pkt.data + idx * asf_st->ds_chunk_size,
1176 asf_st->ds_chunk_size);
1177 offset += asf_st->ds_chunk_size;
1179 av_free(asf_st->pkt.data);
1180 asf_st->pkt.data = newdata;
1184 asf_st->frag_offset = 0;
1186 asf_st->pkt.size = 0;
1187 asf_st->pkt.data = 0;
1188 asf_st->pkt.side_data_elems = 0;
1189 asf_st->pkt.side_data = NULL;
1190 break; // packet completed
1196 static int asf_read_packet(AVFormatContext *s, AVPacket *pkt)
1198 ASFContext *asf = s->priv_data;
1203 /* parse cached packets, if any */
1204 if ((ret = ff_asf_parse_packet(s, s->pb, pkt)) <= 0)
1206 if ((ret = ff_asf_get_packet(s, s->pb)) < 0)
1207 assert(asf->packet_size_left < FRAME_HEADER_SIZE || asf->packet_segments < 1);
1208 asf->packet_time_start = 0;
1212 // Added to support seeking after packets have been read
1213 // If information is not reset, read_packet fails due to
1214 // leftover information from previous reads
1215 static void asf_reset_header(AVFormatContext *s)
1217 ASFContext *asf = s->priv_data;
1221 asf->packet_size_left = 0;
1222 asf->packet_segments = 0;
1223 asf->packet_flags = 0;
1224 asf->packet_property = 0;
1225 asf->packet_timestamp = 0;
1226 asf->packet_segsizetype = 0;
1227 asf->packet_segments = 0;
1228 asf->packet_seq = 0;
1229 asf->packet_replic_size = 0;
1230 asf->packet_key_frame = 0;
1231 asf->packet_padsize = 0;
1232 asf->packet_frag_offset = 0;
1233 asf->packet_frag_size = 0;
1234 asf->packet_frag_timestamp = 0;
1235 asf->packet_multi_size = 0;
1236 asf->packet_obj_size = 0;
1237 asf->packet_time_delta = 0;
1238 asf->packet_time_start = 0;
1240 for(i=0; i<s->nb_streams; i++){
1241 asf_st= s->streams[i]->priv_data;
1242 av_free_packet(&asf_st->pkt);
1243 asf_st->frag_offset=0;
1249 static int asf_read_close(AVFormatContext *s)
1251 asf_reset_header(s);
1256 static int64_t asf_read_pts(AVFormatContext *s, int stream_index, int64_t *ppos, int64_t pos_limit)
1258 AVPacket pkt1, *pkt = &pkt1;
1263 int64_t start_pos[ASF_MAX_STREAMS];
1265 for(i=0; i<s->nb_streams; i++){
1269 if (s->packet_size > 0)
1270 pos= (pos+s->packet_size-1-s->data_offset)/s->packet_size*s->packet_size+ s->data_offset;
1272 if (avio_seek(s->pb, pos, SEEK_SET) < 0)
1273 return AV_NOPTS_VALUE;
1275 asf_reset_header(s);
1277 if (av_read_frame(s, pkt) < 0){
1278 av_log(s, AV_LOG_INFO, "asf_read_pts failed\n");
1279 return AV_NOPTS_VALUE;
1284 av_free_packet(pkt);
1285 if(pkt->flags&AV_PKT_FLAG_KEY){
1286 i= pkt->stream_index;
1288 asf_st= s->streams[i]->priv_data;
1290 // assert((asf_st->packet_pos - s->data_offset) % s->packet_size == 0);
1291 pos= asf_st->packet_pos;
1293 av_add_index_entry(s->streams[i], pos, pts, pkt->size, pos - start_pos[i] + 1, AVINDEX_KEYFRAME);
1294 start_pos[i]= asf_st->packet_pos + 1;
1296 if(pkt->stream_index == stream_index)
1305 static void asf_build_simple_index(AVFormatContext *s, int stream_index)
1308 ASFContext *asf = s->priv_data;
1309 int64_t current_pos= avio_tell(s->pb);
1311 if(avio_seek(s->pb, asf->data_object_offset + asf->data_object_size, SEEK_SET) < 0) {
1312 asf->index_read= -1;
1316 ff_get_guid(s->pb, &g);
1318 /* the data object can be followed by other top-level objects,
1319 skip them until the simple index object is reached */
1320 while (ff_guidcmp(&g, &ff_asf_simple_index_header)) {
1321 int64_t gsize= avio_rl64(s->pb);
1322 if (gsize < 24 || url_feof(s->pb)) {
1323 avio_seek(s->pb, current_pos, SEEK_SET);
1324 asf->index_read= -1;
1327 avio_skip(s->pb, gsize-24);
1328 ff_get_guid(s->pb, &g);
1332 int64_t itime, last_pos=-1;
1335 int64_t av_unused gsize= avio_rl64(s->pb);
1336 ff_get_guid(s->pb, &g);
1337 itime=avio_rl64(s->pb);
1338 pct=avio_rl32(s->pb);
1339 ict=avio_rl32(s->pb);
1340 av_log(s, AV_LOG_DEBUG, "itime:0x%"PRIx64", pct:%d, ict:%d\n",itime,pct,ict);
1342 for (i=0;i<ict;i++){
1343 int pktnum=avio_rl32(s->pb);
1344 int pktct =avio_rl16(s->pb);
1345 int64_t pos = s->data_offset + s->packet_size*(int64_t)pktnum;
1346 int64_t index_pts= FFMAX(av_rescale(itime, i, 10000) - asf->hdr.preroll, 0);
1348 if(pos != last_pos){
1349 av_log(s, AV_LOG_DEBUG, "pktnum:%d, pktct:%d pts: %"PRId64"\n", pktnum, pktct, index_pts);
1350 av_add_index_entry(s->streams[stream_index], pos, index_pts, s->packet_size, 0, AVINDEX_KEYFRAME);
1354 asf->index_read= ict > 1;
1356 avio_seek(s->pb, current_pos, SEEK_SET);
1359 static int asf_read_seek(AVFormatContext *s, int stream_index, int64_t pts, int flags)
1361 ASFContext *asf = s->priv_data;
1362 AVStream *st = s->streams[stream_index];
1364 if (s->packet_size <= 0)
1367 /* Try using the protocol's read_seek if available */
1369 int ret = avio_seek_time(s->pb, stream_index, pts, flags);
1371 asf_reset_header(s);
1372 if (ret != AVERROR(ENOSYS))
1376 if (!asf->index_read)
1377 asf_build_simple_index(s, stream_index);
1379 if((asf->index_read > 0 && st->index_entries)){
1380 int index= av_index_search_timestamp(st, pts, flags);
1382 /* find the position */
1383 uint64_t pos = st->index_entries[index].pos;
1386 av_log(s, AV_LOG_DEBUG, "SEEKTO: %"PRId64"\n", pos);
1387 if(avio_seek(s->pb, pos, SEEK_SET) < 0)
1389 asf_reset_header(s);
1393 /* no index or seeking by index failed */
1394 if (ff_seek_frame_binary(s, stream_index, pts, flags) < 0)
1396 asf_reset_header(s);
1400 AVInputFormat ff_asf_demuxer = {
1402 .long_name = NULL_IF_CONFIG_SMALL("ASF (Advanced / Active Streaming Format)"),
1403 .priv_data_size = sizeof(ASFContext),
1404 .read_probe = asf_probe,
1405 .read_header = asf_read_header,
1406 .read_packet = asf_read_packet,
1407 .read_close = asf_read_close,
1408 .read_seek = asf_read_seek,
1409 .read_timestamp = asf_read_pts,
1410 .flags = AVFMT_NOBINSEARCH | AVFMT_NOGENSEARCH,
1411 .priv_class = &asf_class,