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 = 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;
518 for (i=0; i<stream_ct; i++){
520 ext_len = avio_rl16(pb);
521 avio_skip(pb, ext_len);
524 for (i=0; i<payload_ext_ct; i++){
527 ext_len=avio_rl32(pb);
528 avio_skip(pb, ext_len);
534 static int asf_read_content_desc(AVFormatContext *s, int64_t size)
536 AVIOContext *pb = s->pb;
537 int len1, len2, len3, len4, len5;
539 len1 = avio_rl16(pb);
540 len2 = avio_rl16(pb);
541 len3 = avio_rl16(pb);
542 len4 = avio_rl16(pb);
543 len5 = avio_rl16(pb);
544 get_tag(s, "title" , 0, len1);
545 get_tag(s, "author" , 0, len2);
546 get_tag(s, "copyright", 0, len3);
547 get_tag(s, "comment" , 0, len4);
553 static int asf_read_ext_content_desc(AVFormatContext *s, int64_t size)
555 AVIOContext *pb = s->pb;
556 ASFContext *asf = s->priv_data;
557 int desc_count, i, ret;
559 desc_count = avio_rl16(pb);
560 for(i=0;i<desc_count;i++) {
561 int name_len,value_type,value_len;
564 name_len = avio_rl16(pb);
565 if (name_len%2) // must be even, broken lavf versions wrote len-1
567 if ((ret = avio_get_str16le(pb, name_len, name, sizeof(name))) < name_len)
568 avio_skip(pb, name_len - ret);
569 value_type = avio_rl16(pb);
570 value_len = avio_rl16(pb);
571 if (!value_type && value_len%2)
574 * My sample has that stream set to 0 maybe that mean the container.
575 * Asf stream count start at 1. I am using 0 to the container value since it's unused
577 if (!strcmp(name, "AspectRatioX")){
578 asf->dar[0].num= get_value(s->pb, value_type);
579 } else if(!strcmp(name, "AspectRatioY")){
580 asf->dar[0].den= get_value(s->pb, value_type);
582 get_tag(s, name, value_type, value_len);
588 static int asf_read_language_list(AVFormatContext *s, int64_t size)
590 AVIOContext *pb = s->pb;
591 ASFContext *asf = s->priv_data;
593 int stream_count = avio_rl16(pb);
594 for(j = 0; j < stream_count; j++) {
596 unsigned int lang_len = avio_r8(pb);
597 if ((ret = avio_get_str16le(pb, lang_len, lang, sizeof(lang))) < lang_len)
598 avio_skip(pb, lang_len - ret);
600 av_strlcpy(asf->stream_languages[j], lang, sizeof(*asf->stream_languages));
606 static int asf_read_metadata(AVFormatContext *s, int64_t size)
608 AVIOContext *pb = s->pb;
609 ASFContext *asf = s->priv_data;
610 int n, stream_num, name_len, value_len, value_num;
616 int av_unused value_type;
618 avio_rl16(pb); //lang_list_index
619 stream_num= avio_rl16(pb);
620 name_len= avio_rl16(pb);
621 value_type = avio_rl16(pb); /* value_type */
622 value_len= avio_rl32(pb);
624 if ((ret = avio_get_str16le(pb, name_len, name, sizeof(name))) < name_len)
625 avio_skip(pb, name_len - ret);
626 av_dlog(s, "%d %d %d %d %d <%s>\n",
627 i, stream_num, name_len, value_type, value_len, name);
628 value_num= avio_rl16(pb);//we should use get_value() here but it does not work 2 is le16 here but le32 elsewhere
629 avio_skip(pb, value_len - 2);
632 if (!strcmp(name, "AspectRatioX")) asf->dar[stream_num].num= value_num;
633 else if(!strcmp(name, "AspectRatioY")) asf->dar[stream_num].den= value_num;
640 static int asf_read_marker(AVFormatContext *s, int64_t size)
642 AVIOContext *pb = s->pb;
643 int i, count, name_len, ret;
646 avio_rl64(pb); // reserved 16 bytes
647 avio_rl64(pb); // ...
648 count = avio_rl32(pb); // markers count
649 avio_rl16(pb); // reserved 2 bytes
650 name_len = avio_rl16(pb); // name length
651 for(i=0;i<name_len;i++){
652 avio_r8(pb); // skip the name
655 for(i=0;i<count;i++){
659 avio_rl64(pb); // offset, 8 bytes
660 pres_time = avio_rl64(pb); // presentation time
661 avio_rl16(pb); // entry length
662 avio_rl32(pb); // send time
663 avio_rl32(pb); // flags
664 name_len = avio_rl32(pb); // name length
665 if ((ret = avio_get_str16le(pb, name_len * 2, name, sizeof(name))) < name_len)
666 avio_skip(pb, name_len - ret);
667 avpriv_new_chapter(s, i, (AVRational){1, 10000000}, pres_time, AV_NOPTS_VALUE, name );
673 static int asf_read_header(AVFormatContext *s)
675 ASFContext *asf = s->priv_data;
677 AVIOContext *pb = s->pb;
682 if (ff_guidcmp(&g, &ff_asf_header))
683 return AVERROR_INVALIDDATA;
688 memset(&asf->asfid2avid, -1, sizeof(asf->asfid2avid));
690 uint64_t gpos= avio_tell(pb);
692 gsize = avio_rl64(pb);
694 if (!ff_guidcmp(&g, &ff_asf_data_header)) {
695 asf->data_object_offset = avio_tell(pb);
696 // if not streaming, gsize is not unlimited (how?), and there is enough space in the file..
697 if (!(asf->hdr.flags & 0x01) && gsize >= 100) {
698 asf->data_object_size = gsize - 24;
700 asf->data_object_size = (uint64_t)-1;
705 return AVERROR_INVALIDDATA;
706 if (!ff_guidcmp(&g, &ff_asf_file_header)) {
707 int ret = asf_read_file_properties(s, gsize);
710 } else if (!ff_guidcmp(&g, &ff_asf_stream_header)) {
711 asf_read_stream_properties(s, gsize);
712 } else if (!ff_guidcmp(&g, &ff_asf_comment_header)) {
713 asf_read_content_desc(s, gsize);
714 } else if (!ff_guidcmp(&g, &ff_asf_language_guid)) {
715 asf_read_language_list(s, gsize);
716 } else if (!ff_guidcmp(&g, &ff_asf_extended_content_header)) {
717 asf_read_ext_content_desc(s, gsize);
718 } else if (!ff_guidcmp(&g, &ff_asf_metadata_header)) {
719 asf_read_metadata(s, gsize);
720 } else if (!ff_guidcmp(&g, &ff_asf_ext_stream_header)) {
721 asf_read_ext_stream_properties(s, gsize);
723 // there could be a optional stream properties object to follow
724 // if so the next iteration will pick it up
726 } else if (!ff_guidcmp(&g, &ff_asf_head1_guid)) {
730 } else if (!ff_guidcmp(&g, &ff_asf_marker_header)) {
731 asf_read_marker(s, gsize);
732 } else if (url_feof(pb)) {
736 if (!ff_guidcmp(&g, &ff_asf_content_encryption)) {
739 av_log(s, AV_LOG_WARNING, "DRM protected stream detected, decoding will likely fail!\n");
741 av_log(s, AV_LOG_DEBUG, "Secret data:\n");
742 av_get_packet(pb, &pkt, len); av_hex_dump_log(s, AV_LOG_DEBUG, pkt.data, pkt.size); av_free_packet(&pkt);
744 get_tag(s, "ASF_Protection_Type", -1, len);
746 get_tag(s, "ASF_Key_ID", -1, len);
748 get_tag(s, "ASF_License_URL", -1, len);
749 } else if (!ff_guidcmp(&g, &ff_asf_ext_content_encryption)) {
750 av_log(s, AV_LOG_WARNING, "Ext DRM protected stream detected, decoding will likely fail!\n");
751 av_dict_set(&s->metadata, "encryption", "ASF Extended Content Encryption", 0);
752 } else if (!ff_guidcmp(&g, &ff_asf_digital_signature)) {
753 av_log(s, AV_LOG_INFO, "Digital signature detected!\n");
757 if(avio_tell(pb) != gpos + gsize)
758 av_log(s, AV_LOG_DEBUG, "gpos mismatch our pos=%"PRIu64", end=%"PRIu64"\n", avio_tell(pb)-gpos, gsize);
759 avio_seek(pb, gpos + gsize, SEEK_SET);
767 asf->data_offset = avio_tell(pb);
768 asf->packet_size_left = 0;
771 for(i=0; i<128; i++){
772 int stream_num= asf->asfid2avid[i];
774 AVStream *st = s->streams[stream_num];
775 if (!st->codec->bit_rate)
776 st->codec->bit_rate = asf->stream_bitrates[i];
777 if (asf->dar[i].num > 0 && asf->dar[i].den > 0){
778 av_reduce(&st->sample_aspect_ratio.den,
779 &st->sample_aspect_ratio.num,
780 asf->dar[i].num, asf->dar[i].den, INT_MAX);
781 } 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.
782 av_reduce(&st->sample_aspect_ratio.den,
783 &st->sample_aspect_ratio.num,
784 asf->dar[0].num, asf->dar[0].den, INT_MAX);
786 av_dlog(s, "i=%d, st->codec->codec_type:%d, asf->dar %d:%d sar=%d:%d\n",
787 i, st->codec->codec_type, asf->dar[i].num, asf->dar[i].den,
788 st->sample_aspect_ratio.num, st->sample_aspect_ratio.den);
790 // copy and convert language codes to the frontend
791 if (asf->streams[i].stream_language_index < 128) {
792 const char *rfc1766 = asf->stream_languages[asf->streams[i].stream_language_index];
793 if (rfc1766 && strlen(rfc1766) > 1) {
794 const char primary_tag[3] = { rfc1766[0], rfc1766[1], '\0' }; // ignore country code if any
795 const char *iso6392 = av_convert_lang_to(primary_tag, AV_LANG_ISO639_2_BIBL);
797 av_dict_set(&st->metadata, "language", iso6392, 0);
803 ff_metadata_conv(&s->metadata, NULL, ff_asf_metadata_conv);
808 #define DO_2BITS(bits, var, defval) \
811 case 3: var = avio_rl32(pb); rsize += 4; break; \
812 case 2: var = avio_rl16(pb); rsize += 2; break; \
813 case 1: var = avio_r8(pb); rsize++; break; \
814 default: var = defval; break; \
818 * Load a single ASF packet into the demuxer.
819 * @param s demux context
820 * @param pb context to read data from
821 * @return 0 on success, <0 on error
823 static int ff_asf_get_packet(AVFormatContext *s, AVIOContext *pb)
825 ASFContext *asf = s->priv_data;
826 uint32_t packet_length, padsize;
830 // if we do not know packet size, allow skipping up to 32 kB
832 if (asf->no_resync_search)
834 else if (s->packet_size > 0)
835 off= (avio_tell(pb) - s->data_offset) % s->packet_size + 3;
841 if(c == 0x82 && !d && !e)
847 * This code allows handling of -EAGAIN at packet boundaries (i.e.
848 * if the packet sync code above triggers -EAGAIN). This does not
849 * imply complete -EAGAIN handling support at random positions in
852 if (pb->error == AVERROR(EAGAIN))
853 return AVERROR(EAGAIN);
855 av_log(s, AV_LOG_ERROR, "ff asf bad header %x at:%"PRId64"\n", c, avio_tell(pb));
857 if ((c & 0x8f) == 0x82) {
860 av_log(s, AV_LOG_ERROR, "ff asf bad non zero\n");
861 return AVERROR_INVALIDDATA;
866 }else if(!url_feof(pb)){
867 avio_seek(pb, -1, SEEK_CUR); //FIXME
870 asf->packet_flags = c;
871 asf->packet_property = d;
873 DO_2BITS(asf->packet_flags >> 5, packet_length, s->packet_size);
874 DO_2BITS(asf->packet_flags >> 1, padsize, 0); // sequence ignored
875 DO_2BITS(asf->packet_flags >> 3, padsize, 0); // padding length
877 //the following checks prevent overflows and infinite loops
878 if(!packet_length || packet_length >= (1U<<29)){
879 av_log(s, AV_LOG_ERROR, "invalid packet_length %d at:%"PRId64"\n", packet_length, avio_tell(pb));
880 return AVERROR_INVALIDDATA;
882 if(padsize >= packet_length){
883 av_log(s, AV_LOG_ERROR, "invalid padsize %d at:%"PRId64"\n", padsize, avio_tell(pb));
884 return AVERROR_INVALIDDATA;
887 asf->packet_timestamp = avio_rl32(pb);
888 avio_rl16(pb); /* duration */
889 // rsize has at least 11 bytes which have to be present
891 if (asf->packet_flags & 0x01) {
892 asf->packet_segsizetype = avio_r8(pb); rsize++;
893 asf->packet_segments = asf->packet_segsizetype & 0x3f;
895 asf->packet_segments = 1;
896 asf->packet_segsizetype = 0x80;
898 if (rsize > packet_length - padsize) {
899 asf->packet_size_left = 0;
900 av_log(s, AV_LOG_ERROR,
901 "invalid packet header length %d for pktlen %d-%d at %"PRId64"\n",
902 rsize, packet_length, padsize, avio_tell(pb));
903 return AVERROR_INVALIDDATA;
905 asf->packet_size_left = packet_length - padsize - rsize;
906 if (packet_length < asf->hdr.min_pktsize)
907 padsize += asf->hdr.min_pktsize - packet_length;
908 asf->packet_padsize = padsize;
909 av_dlog(s, "packet: size=%d padsize=%d left=%d\n", s->packet_size, asf->packet_padsize, asf->packet_size_left);
915 * @return <0 if error
917 static int asf_read_frame_header(AVFormatContext *s, AVIOContext *pb){
918 ASFContext *asf = s->priv_data;
920 int num = avio_r8(pb);
921 int64_t ts0, ts1 av_unused;
923 asf->packet_segments--;
924 asf->packet_key_frame = num >> 7;
925 asf->stream_index = asf->asfid2avid[num & 0x7f];
926 // sequence should be ignored!
927 DO_2BITS(asf->packet_property >> 4, asf->packet_seq, 0);
928 DO_2BITS(asf->packet_property >> 2, asf->packet_frag_offset, 0);
929 DO_2BITS(asf->packet_property, asf->packet_replic_size, 0);
930 av_dlog(asf, "key:%d stream:%d seq:%d offset:%d replic_size:%d\n",
931 asf->packet_key_frame, asf->stream_index, asf->packet_seq,
932 asf->packet_frag_offset, asf->packet_replic_size);
933 if (rsize+asf->packet_replic_size > asf->packet_size_left) {
934 av_log(s, AV_LOG_ERROR, "packet_replic_size %d is invalid\n", asf->packet_replic_size);
935 return AVERROR_INVALIDDATA;
937 if (asf->packet_replic_size >= 8) {
938 asf->packet_obj_size = avio_rl32(pb);
939 if(asf->packet_obj_size >= (1<<24) || asf->packet_obj_size <= 0){
940 av_log(s, AV_LOG_ERROR, "packet_obj_size invalid\n");
941 return AVERROR_INVALIDDATA;
943 asf->packet_frag_timestamp = avio_rl32(pb); // timestamp
944 if(asf->packet_replic_size >= 8+38+4){
950 avio_skip(pb, asf->packet_replic_size - 8 - 38 - 4);
951 if(ts0!= -1) asf->packet_frag_timestamp= ts0/10000;
952 else asf->packet_frag_timestamp= AV_NOPTS_VALUE;
954 avio_skip(pb, asf->packet_replic_size - 8);
955 rsize += asf->packet_replic_size; // FIXME - check validity
956 } else if (asf->packet_replic_size==1){
957 // multipacket - frag_offset is beginning timestamp
958 asf->packet_time_start = asf->packet_frag_offset;
959 asf->packet_frag_offset = 0;
960 asf->packet_frag_timestamp = asf->packet_timestamp;
962 asf->packet_time_delta = avio_r8(pb);
964 }else if(asf->packet_replic_size!=0){
965 av_log(s, AV_LOG_ERROR, "unexpected packet_replic_size of %d\n", asf->packet_replic_size);
966 return AVERROR_INVALIDDATA;
968 if (asf->packet_flags & 0x01) {
969 DO_2BITS(asf->packet_segsizetype >> 6, asf->packet_frag_size, 0); // 0 is illegal
970 if (rsize > asf->packet_size_left) {
971 av_log(s, AV_LOG_ERROR, "packet_replic_size is invalid\n");
972 return AVERROR_INVALIDDATA;
973 } else if(asf->packet_frag_size > asf->packet_size_left - rsize){
974 if (asf->packet_frag_size > asf->packet_size_left - rsize + asf->packet_padsize) {
975 av_log(s, AV_LOG_ERROR, "packet_frag_size is invalid (%d-%d)\n", asf->packet_size_left, rsize);
976 return AVERROR_INVALIDDATA;
978 int diff = asf->packet_frag_size - (asf->packet_size_left - rsize);
979 asf->packet_size_left += diff;
980 asf->packet_padsize -= diff;
984 asf->packet_frag_size = asf->packet_size_left - rsize;
986 if (asf->packet_replic_size == 1) {
987 asf->packet_multi_size = asf->packet_frag_size;
988 if (asf->packet_multi_size > asf->packet_size_left)
989 return AVERROR_INVALIDDATA;
991 asf->packet_size_left -= rsize;
997 * Parse data from individual ASF packets (which were previously loaded
998 * with asf_get_packet()).
999 * @param s demux context
1000 * @param pb context to read data from
1001 * @param pkt pointer to store packet data into
1002 * @return 0 if data was stored in pkt, <0 on error or 1 if more ASF
1003 * packets need to be loaded (through asf_get_packet())
1005 static int ff_asf_parse_packet(AVFormatContext *s, AVIOContext *pb, AVPacket *pkt)
1007 ASFContext *asf = s->priv_data;
1008 ASFStream *asf_st = 0;
1014 if (asf->packet_size_left < FRAME_HEADER_SIZE ||
1015 asf->packet_segments < 1) {
1016 int ret = asf->packet_size_left + asf->packet_padsize;
1022 asf->packet_pos= avio_tell(pb);
1023 if (asf->data_object_size != (uint64_t)-1 &&
1024 (asf->packet_pos - asf->data_object_offset >= asf->data_object_size))
1025 return AVERROR_EOF; /* Do not exceed the size of the data object */
1028 if (asf->packet_time_start == 0) {
1029 if (asf_read_frame_header(s, pb) < 0) {
1030 asf->packet_segments = 0;
1033 if (asf->stream_index < 0 ||
1034 s->streams[asf->stream_index]->discard >= AVDISCARD_ALL ||
1035 (!asf->packet_key_frame &&
1036 s->streams[asf->stream_index]->discard >= AVDISCARD_NONKEY)) {
1037 asf->packet_time_start = 0;
1038 /* unhandled packet (should not happen) */
1039 avio_skip(pb, asf->packet_frag_size);
1040 asf->packet_size_left -= asf->packet_frag_size;
1041 if (asf->stream_index < 0)
1042 av_log(s, AV_LOG_ERROR, "ff asf skip %d (unknown stream)\n", asf->packet_frag_size);
1045 asf->asf_st = s->streams[asf->stream_index]->priv_data;
1047 asf_st = asf->asf_st;
1049 if (asf->packet_replic_size == 1) {
1050 // frag_offset is here used as the beginning timestamp
1051 asf->packet_frag_timestamp = asf->packet_time_start;
1052 asf->packet_time_start += asf->packet_time_delta;
1053 asf->packet_obj_size = asf->packet_frag_size = avio_r8(pb);
1054 asf->packet_size_left--;
1055 asf->packet_multi_size--;
1056 if (asf->packet_multi_size < asf->packet_obj_size) {
1057 asf->packet_time_start = 0;
1058 avio_skip(pb, asf->packet_multi_size);
1059 asf->packet_size_left -= asf->packet_multi_size;
1062 asf->packet_multi_size -= asf->packet_obj_size;
1064 if (asf_st->frag_offset + asf->packet_frag_size <= asf_st->pkt.size &&
1065 asf_st->frag_offset + asf->packet_frag_size > asf->packet_obj_size) {
1066 av_log(s, AV_LOG_INFO, "ignoring invalid packet_obj_size (%d %d %d %d)\n",
1067 asf_st->frag_offset, asf->packet_frag_size,
1068 asf->packet_obj_size, asf_st->pkt.size);
1069 asf->packet_obj_size = asf_st->pkt.size;
1072 if (asf_st->pkt.size != asf->packet_obj_size ||
1073 //FIXME is this condition sufficient?
1074 asf_st->frag_offset + asf->packet_frag_size > asf_st->pkt.size) {
1075 if (asf_st->pkt.data) {
1076 av_log(s, AV_LOG_INFO, "freeing incomplete packet size %d, "
1077 "new %d\n", asf_st->pkt.size, asf->packet_obj_size);
1078 asf_st->frag_offset = 0;
1079 av_free_packet(&asf_st->pkt);
1082 av_new_packet(&asf_st->pkt, asf->packet_obj_size);
1083 asf_st->seq = asf->packet_seq;
1084 asf_st->pkt.dts = asf->packet_frag_timestamp - asf->hdr.preroll;
1085 asf_st->pkt.stream_index = asf->stream_index;
1086 asf_st->pkt.pos = asf_st->packet_pos = asf->packet_pos;
1088 if (asf_st->pkt.data && asf_st->palette_changed) {
1090 pal = av_packet_new_side_data(&asf_st->pkt, AV_PKT_DATA_PALETTE,
1093 av_log(s, AV_LOG_ERROR, "Cannot append palette to packet\n");
1095 memcpy(pal, asf_st->palette, AVPALETTE_SIZE);
1096 asf_st->palette_changed = 0;
1099 av_dlog(asf, "new packet: stream:%d key:%d packet_key:%d audio:%d size:%d\n",
1100 asf->stream_index, asf->packet_key_frame,
1101 asf_st->pkt.flags & AV_PKT_FLAG_KEY,
1102 s->streams[asf->stream_index]->codec->codec_type == AVMEDIA_TYPE_AUDIO,
1103 asf->packet_obj_size);
1104 if (s->streams[asf->stream_index]->codec->codec_type == AVMEDIA_TYPE_AUDIO)
1105 asf->packet_key_frame = 1;
1106 if (asf->packet_key_frame)
1107 asf_st->pkt.flags |= AV_PKT_FLAG_KEY;
1111 av_dlog(asf, "READ PACKET s:%d os:%d o:%d,%d l:%d DATA:%p\n",
1112 s->packet_size, asf_st->pkt.size, asf->packet_frag_offset,
1113 asf_st->frag_offset, asf->packet_frag_size, asf_st->pkt.data);
1114 asf->packet_size_left -= asf->packet_frag_size;
1115 if (asf->packet_size_left < 0)
1118 if (asf->packet_frag_offset >= asf_st->pkt.size ||
1119 asf->packet_frag_size > asf_st->pkt.size - asf->packet_frag_offset) {
1120 av_log(s, AV_LOG_ERROR, "packet fragment position invalid %u,%u not in %u\n",
1121 asf->packet_frag_offset, asf->packet_frag_size, asf_st->pkt.size);
1125 ret = avio_read(pb, asf_st->pkt.data + asf->packet_frag_offset,
1126 asf->packet_frag_size);
1127 if (ret != asf->packet_frag_size) {
1128 if (ret < 0 || asf->packet_frag_offset + ret == 0)
1129 return ret < 0 ? ret : AVERROR_EOF;
1131 if (asf_st->ds_span > 1) {
1132 // scrambling, we can either drop it completely or fill the remainder
1133 // TODO: should we fill the whole packet instead of just the current
1135 memset(asf_st->pkt.data + asf->packet_frag_offset + ret, 0,
1136 asf->packet_frag_size - ret);
1137 ret = asf->packet_frag_size;
1139 // no scrambling, so we can return partial packets
1140 av_shrink_packet(&asf_st->pkt, asf->packet_frag_offset + ret);
1143 if (s->key && s->keylen == 20)
1144 ff_asfcrypt_dec(s->key, asf_st->pkt.data + asf->packet_frag_offset,
1146 asf_st->frag_offset += ret;
1147 /* test if whole packet is read */
1148 if (asf_st->frag_offset == asf_st->pkt.size) {
1149 //workaround for macroshit radio DVR-MS files
1150 if (s->streams[asf->stream_index]->codec->codec_id == AV_CODEC_ID_MPEG2VIDEO &&
1151 asf_st->pkt.size > 100) {
1153 for (i = 0; i < asf_st->pkt.size && !asf_st->pkt.data[i]; i++);
1154 if (i == asf_st->pkt.size) {
1155 av_log(s, AV_LOG_DEBUG, "discarding ms fart\n");
1156 asf_st->frag_offset = 0;
1157 av_free_packet(&asf_st->pkt);
1163 if (asf_st->ds_span > 1) {
1164 if(asf_st->pkt.size != asf_st->ds_packet_size * asf_st->ds_span) {
1165 av_log(s, AV_LOG_ERROR, "pkt.size != ds_packet_size * "
1166 "ds_span (%d %d %d)\n", asf_st->pkt.size,
1167 asf_st->ds_packet_size, asf_st->ds_span);
1169 /* packet descrambling */
1170 uint8_t *newdata = av_malloc(asf_st->pkt.size + FF_INPUT_BUFFER_PADDING_SIZE);
1173 memset(newdata + asf_st->pkt.size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
1174 while (offset < asf_st->pkt.size) {
1175 int off = offset / asf_st->ds_chunk_size;
1176 int row = off / asf_st->ds_span;
1177 int col = off % asf_st->ds_span;
1178 int idx = row + col * asf_st->ds_packet_size / asf_st->ds_chunk_size;
1179 assert(offset + asf_st->ds_chunk_size <= asf_st->pkt.size);
1180 assert(idx+1 <= asf_st->pkt.size / asf_st->ds_chunk_size);
1181 memcpy(newdata + offset,
1182 asf_st->pkt.data + idx * asf_st->ds_chunk_size,
1183 asf_st->ds_chunk_size);
1184 offset += asf_st->ds_chunk_size;
1186 av_free(asf_st->pkt.data);
1187 asf_st->pkt.data = newdata;
1191 asf_st->frag_offset = 0;
1193 asf_st->pkt.size = 0;
1194 asf_st->pkt.data = 0;
1195 asf_st->pkt.side_data_elems = 0;
1196 asf_st->pkt.side_data = NULL;
1197 break; // packet completed
1203 static int asf_read_packet(AVFormatContext *s, AVPacket *pkt)
1205 ASFContext *asf = s->priv_data;
1210 /* parse cached packets, if any */
1211 if ((ret = ff_asf_parse_packet(s, s->pb, pkt)) <= 0)
1213 if ((ret = ff_asf_get_packet(s, s->pb)) < 0)
1214 assert(asf->packet_size_left < FRAME_HEADER_SIZE || asf->packet_segments < 1);
1215 asf->packet_time_start = 0;
1219 // Added to support seeking after packets have been read
1220 // If information is not reset, read_packet fails due to
1221 // leftover information from previous reads
1222 static void asf_reset_header(AVFormatContext *s)
1224 ASFContext *asf = s->priv_data;
1228 asf->packet_size_left = 0;
1229 asf->packet_segments = 0;
1230 asf->packet_flags = 0;
1231 asf->packet_property = 0;
1232 asf->packet_timestamp = 0;
1233 asf->packet_segsizetype = 0;
1234 asf->packet_segments = 0;
1235 asf->packet_seq = 0;
1236 asf->packet_replic_size = 0;
1237 asf->packet_key_frame = 0;
1238 asf->packet_padsize = 0;
1239 asf->packet_frag_offset = 0;
1240 asf->packet_frag_size = 0;
1241 asf->packet_frag_timestamp = 0;
1242 asf->packet_multi_size = 0;
1243 asf->packet_obj_size = 0;
1244 asf->packet_time_delta = 0;
1245 asf->packet_time_start = 0;
1247 for(i=0; i<s->nb_streams; i++){
1248 asf_st= s->streams[i]->priv_data;
1249 av_free_packet(&asf_st->pkt);
1250 asf_st->frag_offset=0;
1256 static int asf_read_close(AVFormatContext *s)
1258 asf_reset_header(s);
1263 static int64_t asf_read_pts(AVFormatContext *s, int stream_index, int64_t *ppos, int64_t pos_limit)
1265 AVPacket pkt1, *pkt = &pkt1;
1270 int64_t start_pos[ASF_MAX_STREAMS];
1272 for(i=0; i<s->nb_streams; i++){
1276 if (s->packet_size > 0)
1277 pos= (pos+s->packet_size-1-s->data_offset)/s->packet_size*s->packet_size+ s->data_offset;
1279 if (avio_seek(s->pb, pos, SEEK_SET) < 0)
1280 return AV_NOPTS_VALUE;
1282 asf_reset_header(s);
1284 if (av_read_frame(s, pkt) < 0){
1285 av_log(s, AV_LOG_INFO, "asf_read_pts failed\n");
1286 return AV_NOPTS_VALUE;
1291 av_free_packet(pkt);
1292 if(pkt->flags&AV_PKT_FLAG_KEY){
1293 i= pkt->stream_index;
1295 asf_st= s->streams[i]->priv_data;
1297 // assert((asf_st->packet_pos - s->data_offset) % s->packet_size == 0);
1298 pos= asf_st->packet_pos;
1300 av_add_index_entry(s->streams[i], pos, pts, pkt->size, pos - start_pos[i] + 1, AVINDEX_KEYFRAME);
1301 start_pos[i]= asf_st->packet_pos + 1;
1303 if(pkt->stream_index == stream_index)
1312 static void asf_build_simple_index(AVFormatContext *s, int stream_index)
1315 ASFContext *asf = s->priv_data;
1316 int64_t current_pos= avio_tell(s->pb);
1318 if(avio_seek(s->pb, asf->data_object_offset + asf->data_object_size, SEEK_SET) < 0) {
1319 asf->index_read= -1;
1323 ff_get_guid(s->pb, &g);
1325 /* the data object can be followed by other top-level objects,
1326 skip them until the simple index object is reached */
1327 while (ff_guidcmp(&g, &ff_asf_simple_index_header)) {
1328 int64_t gsize= avio_rl64(s->pb);
1329 if (gsize < 24 || url_feof(s->pb)) {
1330 avio_seek(s->pb, current_pos, SEEK_SET);
1331 asf->index_read= -1;
1334 avio_skip(s->pb, gsize-24);
1335 ff_get_guid(s->pb, &g);
1339 int64_t itime, last_pos=-1;
1342 int64_t av_unused gsize= avio_rl64(s->pb);
1343 ff_get_guid(s->pb, &g);
1344 itime=avio_rl64(s->pb);
1345 pct=avio_rl32(s->pb);
1346 ict=avio_rl32(s->pb);
1347 av_log(s, AV_LOG_DEBUG, "itime:0x%"PRIx64", pct:%d, ict:%d\n",itime,pct,ict);
1349 for (i=0;i<ict;i++){
1350 int pktnum=avio_rl32(s->pb);
1351 int pktct =avio_rl16(s->pb);
1352 int64_t pos = s->data_offset + s->packet_size*(int64_t)pktnum;
1353 int64_t index_pts= FFMAX(av_rescale(itime, i, 10000) - asf->hdr.preroll, 0);
1355 if(pos != last_pos){
1356 av_log(s, AV_LOG_DEBUG, "pktnum:%d, pktct:%d pts: %"PRId64"\n", pktnum, pktct, index_pts);
1357 av_add_index_entry(s->streams[stream_index], pos, index_pts, s->packet_size, 0, AVINDEX_KEYFRAME);
1361 asf->index_read= ict > 1;
1363 avio_seek(s->pb, current_pos, SEEK_SET);
1366 static int asf_read_seek(AVFormatContext *s, int stream_index, int64_t pts, int flags)
1368 ASFContext *asf = s->priv_data;
1369 AVStream *st = s->streams[stream_index];
1371 if (s->packet_size <= 0)
1374 /* Try using the protocol's read_seek if available */
1376 int ret = avio_seek_time(s->pb, stream_index, pts, flags);
1378 asf_reset_header(s);
1379 if (ret != AVERROR(ENOSYS))
1383 if (!asf->index_read)
1384 asf_build_simple_index(s, stream_index);
1386 if((asf->index_read > 0 && st->index_entries)){
1387 int index= av_index_search_timestamp(st, pts, flags);
1389 /* find the position */
1390 uint64_t pos = st->index_entries[index].pos;
1393 av_log(s, AV_LOG_DEBUG, "SEEKTO: %"PRId64"\n", pos);
1394 if(avio_seek(s->pb, pos, SEEK_SET) < 0)
1396 asf_reset_header(s);
1400 /* no index or seeking by index failed */
1401 if (ff_seek_frame_binary(s, stream_index, pts, flags) < 0)
1403 asf_reset_header(s);
1407 AVInputFormat ff_asf_demuxer = {
1409 .long_name = NULL_IF_CONFIG_SMALL("ASF (Advanced / Active Streaming Format)"),
1410 .priv_data_size = sizeof(ASFContext),
1411 .read_probe = asf_probe,
1412 .read_header = asf_read_header,
1413 .read_packet = asf_read_packet,
1414 .read_close = asf_read_close,
1415 .read_seek = asf_read_seek,
1416 .read_timestamp = asf_read_pts,
1417 .flags = AVFMT_NOBINSEARCH | AVFMT_NOGENSEARCH,
1418 .priv_class = &asf_class,