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/avassert.h"
26 #include "libavutil/avstring.h"
27 #include "libavutil/bswap.h"
28 #include "libavutil/common.h"
29 #include "libavutil/dict.h"
30 #include "libavutil/mathematics.h"
31 #include "libavutil/opt.h"
33 #include "avio_internal.h"
34 #include "avlanguage.h"
43 int asfid2avid[128]; ///< conversion table from asf ID 2 AVStream ID
44 ASFStream streams[128]; ///< it's max number and it's not that big
45 uint32_t stream_bitrates[128]; ///< max number of streams, bitrate for each (for streaming)
47 char stream_languages[128][6]; ///< max number of streams, language for each (RFC1766, e.g. en-US)
48 /* non streamed additonnal info */
51 /* only for reading */
52 uint64_t data_offset; ///< beginning of the first data packet
53 uint64_t data_object_offset; ///< data object offset (excl. GUID & size)
54 uint64_t data_object_size; ///< size of the data object
62 int packet_segsizetype;
65 int packet_replic_size;
68 unsigned int packet_frag_offset;
69 unsigned int packet_frag_size;
70 int64_t packet_frag_timestamp;
71 int packet_multi_size;
73 int packet_time_delta;
74 int packet_time_start;
79 ASFStream *asf_st; ///< currently decoded stream
84 static const AVOption options[] = {
85 { "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 },
89 static const AVClass asf_class = {
90 .class_name = "asf demuxer",
91 .item_name = av_default_item_name,
93 .version = LIBAVUTIL_VERSION_INT,
99 #define ASF_MAX_STREAMS 127
100 #define FRAME_HEADER_SIZE 16
101 // Fix Me! FRAME_HEADER_SIZE may be different. (17 is known to be too large)
104 static const ff_asf_guid stream_bitrate_guid = { /* (http://get.to/sdp) */
105 0xce, 0x75, 0xf8, 0x7b, 0x8d, 0x46, 0xd1, 0x11, 0x8d, 0x82, 0x00, 0x60, 0x97, 0xc9, 0xa2, 0xb2
108 #define PRINT_IF_GUID(g, cmp) \
109 if (!ff_guidcmp(g, &cmp)) \
110 av_dlog(NULL, "(GUID: %s) ", # cmp)
112 static void print_guid(ff_asf_guid *g)
115 PRINT_IF_GUID(g, ff_asf_header);
116 else PRINT_IF_GUID(g, ff_asf_file_header);
117 else PRINT_IF_GUID(g, ff_asf_stream_header);
118 else PRINT_IF_GUID(g, ff_asf_audio_stream);
119 else PRINT_IF_GUID(g, ff_asf_audio_conceal_none);
120 else PRINT_IF_GUID(g, ff_asf_video_stream);
121 else PRINT_IF_GUID(g, ff_asf_video_conceal_none);
122 else PRINT_IF_GUID(g, ff_asf_command_stream);
123 else PRINT_IF_GUID(g, ff_asf_comment_header);
124 else PRINT_IF_GUID(g, ff_asf_codec_comment_header);
125 else PRINT_IF_GUID(g, ff_asf_codec_comment1_header);
126 else PRINT_IF_GUID(g, ff_asf_data_header);
127 else PRINT_IF_GUID(g, ff_asf_simple_index_header);
128 else PRINT_IF_GUID(g, ff_asf_head1_guid);
129 else PRINT_IF_GUID(g, ff_asf_head2_guid);
130 else PRINT_IF_GUID(g, ff_asf_my_guid);
131 else PRINT_IF_GUID(g, ff_asf_ext_stream_header);
132 else PRINT_IF_GUID(g, ff_asf_extended_content_header);
133 else PRINT_IF_GUID(g, ff_asf_ext_stream_embed_stream_header);
134 else PRINT_IF_GUID(g, ff_asf_ext_stream_audio_stream);
135 else PRINT_IF_GUID(g, ff_asf_metadata_header);
136 else PRINT_IF_GUID(g, ff_asf_metadata_library_header);
137 else PRINT_IF_GUID(g, ff_asf_marker_header);
138 else PRINT_IF_GUID(g, stream_bitrate_guid);
139 else PRINT_IF_GUID(g, ff_asf_language_guid);
141 av_dlog(NULL, "(GUID: unknown) ");
142 for (i = 0; i < 16; i++)
143 av_dlog(NULL, " 0x%02x,", (*g)[i]);
144 av_dlog(NULL, "}\n");
148 #define print_guid(g)
151 static int asf_probe(AVProbeData *pd)
153 /* check file header */
154 if (!ff_guidcmp(pd->buf, &ff_asf_header))
155 return AVPROBE_SCORE_MAX;
160 /* size of type 2 (BOOL) is 32bit for "Extended Content Description Object"
161 * but 16 bit for "Metadata Object" and "Metadata Library Object" */
162 static int get_value(AVIOContext *pb, int type, int type2_size)
166 return (type2_size == 32) ? avio_rl32(pb) : avio_rl16(pb);
168 return avio_rl32(pb);
170 return avio_rl64(pb);
172 return avio_rl16(pb);
178 /* MSDN claims that this should be "compatible with the ID3 frame, APIC",
179 * but in reality this is only loosely similar */
180 static int asf_read_picture(AVFormatContext *s, int len)
182 AVPacket pkt = { 0 };
183 const CodecMime *mime = ff_id3v2_mime_tags;
184 enum AVCodecID id = AV_CODEC_ID_NONE;
186 uint8_t *desc = NULL;
188 int ret, type, picsize, desc_len;
190 /* type + picsize + mime + desc */
191 if (len < 1 + 4 + 2 + 2) {
192 av_log(s, AV_LOG_ERROR, "Invalid attached picture size: %d.\n", len);
193 return AVERROR_INVALIDDATA;
197 type = avio_r8(s->pb);
199 if (type >= FF_ARRAY_ELEMS(ff_id3v2_picture_types) || type < 0) {
200 av_log(s, AV_LOG_WARNING, "Unknown attached picture type: %d.\n", type);
204 /* picture data size */
205 picsize = avio_rl32(s->pb);
208 /* picture MIME type */
209 len -= avio_get_str16le(s->pb, len, mimetype, sizeof(mimetype));
210 while (mime->id != AV_CODEC_ID_NONE) {
211 if (!strncmp(mime->str, mimetype, sizeof(mimetype))) {
217 if (id == AV_CODEC_ID_NONE) {
218 av_log(s, AV_LOG_ERROR, "Unknown attached picture mimetype: %s.\n",
223 if (picsize >= len) {
224 av_log(s, AV_LOG_ERROR, "Invalid attached picture data size: %d >= %d.\n",
226 return AVERROR_INVALIDDATA;
229 /* picture description */
230 desc_len = (len - picsize) * 2 + 1;
231 desc = av_malloc(desc_len);
233 return AVERROR(ENOMEM);
234 len -= avio_get_str16le(s->pb, len - picsize, desc, desc_len);
236 ret = av_get_packet(s->pb, &pkt, picsize);
240 st = avformat_new_stream(s, NULL);
242 ret = AVERROR(ENOMEM);
245 st->disposition |= AV_DISPOSITION_ATTACHED_PIC;
246 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
247 st->codec->codec_id = id;
248 st->attached_pic = pkt;
249 st->attached_pic.stream_index = st->index;
250 st->attached_pic.flags |= AV_PKT_FLAG_KEY;
253 av_dict_set(&st->metadata, "title", desc, AV_DICT_DONT_STRDUP_VAL);
257 av_dict_set(&st->metadata, "comment", ff_id3v2_picture_types[type], 0);
263 av_free_packet(&pkt);
267 static void get_id3_tag(AVFormatContext *s, int len)
269 ID3v2ExtraMeta *id3v2_extra_meta = NULL;
271 ff_id3v2_read(s, ID3v2_DEFAULT_MAGIC, &id3v2_extra_meta);
272 if (id3v2_extra_meta)
273 ff_id3v2_parse_apic(s, &id3v2_extra_meta);
274 ff_id3v2_free_extra_meta(&id3v2_extra_meta);
277 static void get_tag(AVFormatContext *s, const char *key, int type, int len, int type2_size)
280 int64_t off = avio_tell(s->pb);
283 if ((unsigned)len >= (UINT_MAX - LEN) / 2)
286 value = av_malloc(2 * len + LEN);
290 if (type == 0) { // UTF16-LE
291 avio_get_str16le(s->pb, len, value, 2 * len + 1);
292 } else if (type == -1) { // ASCII
293 avio_read(s->pb, value, len);
295 } else if (type == 1) { // byte array
296 if (!strcmp(key, "WM/Picture")) { // handle cover art
297 asf_read_picture(s, len);
298 } else if (!strcmp(key, "ID3")) { // handle ID3 tag
301 av_log(s, AV_LOG_VERBOSE, "Unsupported byte array in tag %s.\n", key);
304 } else if (type > 1 && type <= 5) { // boolean or DWORD or QWORD or WORD
305 uint64_t num = get_value(s->pb, type, type2_size);
306 snprintf(value, LEN, "%"PRIu64, num);
307 } else if (type == 6) { // (don't) handle GUID
308 av_log(s, AV_LOG_DEBUG, "Unsupported GUID value in tag %s.\n", key);
311 av_log(s, AV_LOG_DEBUG,
312 "Unsupported value type %d in tag %s.\n", type, key);
316 av_dict_set(&s->metadata, key, value, 0);
320 avio_seek(s->pb, off + len, SEEK_SET);
323 static int asf_read_file_properties(AVFormatContext *s, int64_t size)
325 ASFContext *asf = s->priv_data;
326 AVIOContext *pb = s->pb;
328 ff_get_guid(pb, &asf->hdr.guid);
329 asf->hdr.file_size = avio_rl64(pb);
330 asf->hdr.create_time = avio_rl64(pb);
331 avio_rl64(pb); /* number of packets */
332 asf->hdr.play_time = avio_rl64(pb);
333 asf->hdr.send_time = avio_rl64(pb);
334 asf->hdr.preroll = avio_rl32(pb);
335 asf->hdr.ignore = avio_rl32(pb);
336 asf->hdr.flags = avio_rl32(pb);
337 asf->hdr.min_pktsize = avio_rl32(pb);
338 asf->hdr.max_pktsize = avio_rl32(pb);
339 if (asf->hdr.min_pktsize >= (1U << 29))
340 return AVERROR_INVALIDDATA;
341 asf->hdr.max_bitrate = avio_rl32(pb);
342 s->packet_size = asf->hdr.max_pktsize;
347 static int asf_read_stream_properties(AVFormatContext *s, int64_t size)
349 ASFContext *asf = s->priv_data;
350 AVIOContext *pb = s->pb;
354 enum AVMediaType type;
355 int type_specific_size, sizeX;
357 int64_t pos1, pos2, start_time;
358 int test_for_ext_stream_audio, is_dvr_ms_audio = 0;
360 if (s->nb_streams == ASF_MAX_STREAMS) {
361 av_log(s, AV_LOG_ERROR, "too many streams\n");
362 return AVERROR(EINVAL);
365 pos1 = avio_tell(pb);
367 st = avformat_new_stream(s, NULL);
369 return AVERROR(ENOMEM);
370 avpriv_set_pts_info(st, 32, 1, 1000); /* 32 bit pts in ms */
371 asf_st = av_mallocz(sizeof(ASFStream));
373 return AVERROR(ENOMEM);
374 st->priv_data = asf_st;
375 start_time = asf->hdr.preroll;
377 asf_st->stream_language_index = 128; // invalid stream index means no language info
379 if (!(asf->hdr.flags & 0x01)) { // if we aren't streaming...
380 int64_t fsize = avio_size(pb);
381 if (fsize <= 0 || (int64_t)asf->hdr.file_size <= 0 || FFABS(fsize - (int64_t)asf->hdr.file_size) < 10000)
382 st->duration = asf->hdr.play_time /
383 (10000000 / 1000) - start_time;
387 test_for_ext_stream_audio = 0;
388 if (!ff_guidcmp(&g, &ff_asf_audio_stream)) {
389 type = AVMEDIA_TYPE_AUDIO;
390 } else if (!ff_guidcmp(&g, &ff_asf_video_stream)) {
391 type = AVMEDIA_TYPE_VIDEO;
392 } else if (!ff_guidcmp(&g, &ff_asf_jfif_media)) {
393 type = AVMEDIA_TYPE_VIDEO;
394 st->codec->codec_id = AV_CODEC_ID_MJPEG;
395 } else if (!ff_guidcmp(&g, &ff_asf_command_stream)) {
396 type = AVMEDIA_TYPE_DATA;
397 } else if (!ff_guidcmp(&g, &ff_asf_ext_stream_embed_stream_header)) {
398 test_for_ext_stream_audio = 1;
399 type = AVMEDIA_TYPE_UNKNOWN;
404 avio_skip(pb, 8); /* total_size */
405 type_specific_size = avio_rl32(pb);
407 st->id = avio_rl16(pb) & 0x7f; /* stream id */
408 // mapping of asf ID to AV stream ID;
409 asf->asfid2avid[st->id] = s->nb_streams - 1;
413 if (test_for_ext_stream_audio) {
415 if (!ff_guidcmp(&g, &ff_asf_ext_stream_audio_stream)) {
416 type = AVMEDIA_TYPE_AUDIO;
427 st->codec->codec_type = type;
428 if (type == AVMEDIA_TYPE_AUDIO) {
429 int ret = ff_get_wav_header(pb, st->codec, type_specific_size);
432 if (is_dvr_ms_audio) {
433 // codec_id and codec_tag are unreliable in dvr_ms
434 // files. Set them later by probing stream.
435 st->request_probe = 1;
436 st->codec->codec_tag = 0;
438 if (st->codec->codec_id == AV_CODEC_ID_AAC)
439 st->need_parsing = AVSTREAM_PARSE_NONE;
441 st->need_parsing = AVSTREAM_PARSE_FULL;
442 /* We have to init the frame size at some point .... */
443 pos2 = avio_tell(pb);
444 if (size >= (pos2 + 8 - pos1 + 24)) {
445 asf_st->ds_span = avio_r8(pb);
446 asf_st->ds_packet_size = avio_rl16(pb);
447 asf_st->ds_chunk_size = avio_rl16(pb);
448 avio_rl16(pb); // ds_data_size
449 avio_r8(pb); // ds_silence_data
451 if (asf_st->ds_span > 1) {
452 if (!asf_st->ds_chunk_size ||
453 (asf_st->ds_packet_size / asf_st->ds_chunk_size <= 1) ||
454 asf_st->ds_packet_size % asf_st->ds_chunk_size)
455 asf_st->ds_span = 0; // disable descrambling
457 } else if (type == AVMEDIA_TYPE_VIDEO &&
458 size - (avio_tell(pb) - pos1 + 24) >= 51) {
462 avio_rl16(pb); /* size */
463 sizeX = avio_rl32(pb); /* size */
464 st->codec->width = avio_rl32(pb);
465 st->codec->height = avio_rl32(pb);
466 /* not available for asf */
467 avio_rl16(pb); /* panes */
468 st->codec->bits_per_coded_sample = avio_rl16(pb); /* depth */
469 tag1 = avio_rl32(pb);
472 st->codec->extradata_size = ffio_limit(pb, sizeX - 40);
473 st->codec->extradata = av_mallocz(st->codec->extradata_size +
474 FF_INPUT_BUFFER_PADDING_SIZE);
475 avio_read(pb, st->codec->extradata, st->codec->extradata_size);
478 /* Extract palette from extradata if bpp <= 8 */
479 /* This code assumes that extradata contains only palette */
480 /* This is true for all paletted codecs implemented in libavcodec */
481 if (st->codec->extradata_size && (st->codec->bits_per_coded_sample <= 8)) {
484 for (i = 0; i < FFMIN(st->codec->extradata_size, AVPALETTE_SIZE) / 4; i++)
485 asf_st->palette[i] = av_bswap32(((uint32_t *)st->codec->extradata)[i]);
487 memcpy(asf_st->palette, st->codec->extradata,
488 FFMIN(st->codec->extradata_size, AVPALETTE_SIZE));
490 asf_st->palette_changed = 1;
493 st->codec->codec_tag = tag1;
494 st->codec->codec_id = ff_codec_get_id(ff_codec_bmp_tags, tag1);
495 if (tag1 == MKTAG('D', 'V', 'R', ' ')) {
496 st->need_parsing = AVSTREAM_PARSE_FULL;
497 /* issue658 contains wrong w/h and MS even puts a fake seq header
498 * with wrong w/h in extradata while a correct one is in the stream.
499 * maximum lameness */
501 st->codec->height = 0;
502 av_freep(&st->codec->extradata);
503 st->codec->extradata_size = 0;
505 if (st->codec->codec_id == AV_CODEC_ID_H264)
506 st->need_parsing = AVSTREAM_PARSE_FULL_ONCE;
508 pos2 = avio_tell(pb);
509 avio_skip(pb, size - (pos2 - pos1 + 24));
514 static int asf_read_ext_stream_properties(AVFormatContext *s, int64_t size)
516 ASFContext *asf = s->priv_data;
517 AVIOContext *pb = s->pb;
519 int ext_len, payload_ext_ct, stream_ct, i;
520 uint32_t leak_rate, stream_num;
521 unsigned int stream_languageid_index;
523 avio_rl64(pb); // starttime
524 avio_rl64(pb); // endtime
525 leak_rate = avio_rl32(pb); // leak-datarate
526 avio_rl32(pb); // bucket-datasize
527 avio_rl32(pb); // init-bucket-fullness
528 avio_rl32(pb); // alt-leak-datarate
529 avio_rl32(pb); // alt-bucket-datasize
530 avio_rl32(pb); // alt-init-bucket-fullness
531 avio_rl32(pb); // max-object-size
532 avio_rl32(pb); // flags (reliable,seekable,no_cleanpoints?,resend-live-cleanpoints, rest of bits reserved)
533 stream_num = avio_rl16(pb); // stream-num
535 stream_languageid_index = avio_rl16(pb); // stream-language-id-index
536 if (stream_num < 128)
537 asf->streams[stream_num].stream_language_index = stream_languageid_index;
539 avio_rl64(pb); // avg frametime in 100ns units
540 stream_ct = avio_rl16(pb); // stream-name-count
541 payload_ext_ct = avio_rl16(pb); // payload-extension-system-count
543 if (stream_num < 128) {
544 asf->stream_bitrates[stream_num] = leak_rate;
545 asf->streams[stream_num].payload_ext_ct = 0;
548 for (i = 0; i < stream_ct; i++) {
550 ext_len = avio_rl16(pb);
551 avio_skip(pb, ext_len);
554 for (i = 0; i < payload_ext_ct; i++) {
557 size = avio_rl16(pb);
558 ext_len = avio_rl32(pb);
559 avio_skip(pb, ext_len);
560 if (stream_num < 128 && i < FF_ARRAY_ELEMS(asf->streams[stream_num].payload)) {
561 ASFPayload *p = &asf->streams[stream_num].payload[i];
564 av_log(s, AV_LOG_DEBUG, "Payload extension %x %d\n", g[0], p->size );
565 asf->streams[stream_num].payload_ext_ct ++;
572 static int asf_read_content_desc(AVFormatContext *s, int64_t size)
574 AVIOContext *pb = s->pb;
575 int len1, len2, len3, len4, len5;
577 len1 = avio_rl16(pb);
578 len2 = avio_rl16(pb);
579 len3 = avio_rl16(pb);
580 len4 = avio_rl16(pb);
581 len5 = avio_rl16(pb);
582 get_tag(s, "title", 0, len1, 32);
583 get_tag(s, "author", 0, len2, 32);
584 get_tag(s, "copyright", 0, len3, 32);
585 get_tag(s, "comment", 0, len4, 32);
591 static int asf_read_ext_content_desc(AVFormatContext *s, int64_t size)
593 AVIOContext *pb = s->pb;
594 ASFContext *asf = s->priv_data;
595 int desc_count, i, ret;
597 desc_count = avio_rl16(pb);
598 for (i = 0; i < desc_count; i++) {
599 int name_len, value_type, value_len;
602 name_len = avio_rl16(pb);
603 if (name_len % 2) // must be even, broken lavf versions wrote len-1
605 if ((ret = avio_get_str16le(pb, name_len, name, sizeof(name))) < name_len)
606 avio_skip(pb, name_len - ret);
607 value_type = avio_rl16(pb);
608 value_len = avio_rl16(pb);
609 if (!value_type && value_len % 2)
611 /* My sample has that stream set to 0 maybe that mean the container.
612 * ASF stream count starts at 1. I am using 0 to the container value
613 * since it's unused. */
614 if (!strcmp(name, "AspectRatioX"))
615 asf->dar[0].num = get_value(s->pb, value_type, 32);
616 else if (!strcmp(name, "AspectRatioY"))
617 asf->dar[0].den = get_value(s->pb, value_type, 32);
619 get_tag(s, name, value_type, value_len, 32);
625 static int asf_read_language_list(AVFormatContext *s, int64_t size)
627 AVIOContext *pb = s->pb;
628 ASFContext *asf = s->priv_data;
630 int stream_count = avio_rl16(pb);
631 for (j = 0; j < stream_count; j++) {
633 unsigned int lang_len = avio_r8(pb);
634 if ((ret = avio_get_str16le(pb, lang_len, lang,
635 sizeof(lang))) < lang_len)
636 avio_skip(pb, lang_len - ret);
638 av_strlcpy(asf->stream_languages[j], lang,
639 sizeof(*asf->stream_languages));
645 static int asf_read_metadata(AVFormatContext *s, int64_t size)
647 AVIOContext *pb = s->pb;
648 ASFContext *asf = s->priv_data;
649 int n, stream_num, name_len, value_len;
653 for (i = 0; i < n; i++) {
657 avio_rl16(pb); // lang_list_index
658 stream_num = avio_rl16(pb);
659 name_len = avio_rl16(pb);
660 value_type = avio_rl16(pb); /* value_type */
661 value_len = avio_rl32(pb);
663 if ((ret = avio_get_str16le(pb, name_len, name, sizeof(name))) < name_len)
664 avio_skip(pb, name_len - ret);
665 av_dlog(s, "%d stream %d name_len %2d type %d len %4d <%s>\n",
666 i, stream_num, name_len, value_type, value_len, name);
668 if (!strcmp(name, "AspectRatioX")){
669 int aspect_x = get_value(s->pb, value_type, 16);
671 asf->dar[stream_num].num = aspect_x;
672 } else if(!strcmp(name, "AspectRatioY")){
673 int aspect_y = get_value(s->pb, value_type, 16);
675 asf->dar[stream_num].den = aspect_y;
677 get_tag(s, name, value_type, value_len, 16);
684 static int asf_read_marker(AVFormatContext *s, int64_t size)
686 AVIOContext *pb = s->pb;
687 int i, count, name_len, ret;
690 avio_rl64(pb); // reserved 16 bytes
691 avio_rl64(pb); // ...
692 count = avio_rl32(pb); // markers count
693 avio_rl16(pb); // reserved 2 bytes
694 name_len = avio_rl16(pb); // name length
695 for (i = 0; i < name_len; i++)
696 avio_r8(pb); // skip the name
698 for (i = 0; i < count; i++) {
702 avio_rl64(pb); // offset, 8 bytes
703 pres_time = avio_rl64(pb); // presentation time
704 avio_rl16(pb); // entry length
705 avio_rl32(pb); // send time
706 avio_rl32(pb); // flags
707 name_len = avio_rl32(pb); // name length
708 if ((ret = avio_get_str16le(pb, name_len * 2, name,
709 sizeof(name))) < name_len)
710 avio_skip(pb, name_len - ret);
711 avpriv_new_chapter(s, i, (AVRational) { 1, 10000000 }, pres_time,
712 AV_NOPTS_VALUE, name);
718 static int asf_read_header(AVFormatContext *s)
720 ASFContext *asf = s->priv_data;
722 AVIOContext *pb = s->pb;
727 if (ff_guidcmp(&g, &ff_asf_header))
728 return AVERROR_INVALIDDATA;
733 memset(&asf->asfid2avid, -1, sizeof(asf->asfid2avid));
735 uint64_t gpos = avio_tell(pb);
737 gsize = avio_rl64(pb);
739 if (!ff_guidcmp(&g, &ff_asf_data_header)) {
740 asf->data_object_offset = avio_tell(pb);
741 /* If not streaming, gsize is not unlimited (how?),
742 * and there is enough space in the file.. */
743 if (!(asf->hdr.flags & 0x01) && gsize >= 100)
744 asf->data_object_size = gsize - 24;
746 asf->data_object_size = (uint64_t)-1;
750 return AVERROR_INVALIDDATA;
751 if (!ff_guidcmp(&g, &ff_asf_file_header)) {
752 int ret = asf_read_file_properties(s, gsize);
755 } else if (!ff_guidcmp(&g, &ff_asf_stream_header)) {
756 asf_read_stream_properties(s, gsize);
757 } else if (!ff_guidcmp(&g, &ff_asf_comment_header)) {
758 asf_read_content_desc(s, gsize);
759 } else if (!ff_guidcmp(&g, &ff_asf_language_guid)) {
760 asf_read_language_list(s, gsize);
761 } else if (!ff_guidcmp(&g, &ff_asf_extended_content_header)) {
762 asf_read_ext_content_desc(s, gsize);
763 } else if (!ff_guidcmp(&g, &ff_asf_metadata_header)) {
764 asf_read_metadata(s, gsize);
765 } else if (!ff_guidcmp(&g, &ff_asf_metadata_library_header)) {
766 asf_read_metadata(s, gsize);
767 } else if (!ff_guidcmp(&g, &ff_asf_ext_stream_header)) {
768 asf_read_ext_stream_properties(s, gsize);
770 // there could be a optional stream properties object to follow
771 // if so the next iteration will pick it up
773 } else if (!ff_guidcmp(&g, &ff_asf_head1_guid)) {
777 } else if (!ff_guidcmp(&g, &ff_asf_marker_header)) {
778 asf_read_marker(s, gsize);
779 } else if (url_feof(pb)) {
783 if (!ff_guidcmp(&g, &ff_asf_content_encryption)) {
786 av_log(s, AV_LOG_WARNING,
787 "DRM protected stream detected, decoding will likely fail!\n");
789 av_log(s, AV_LOG_DEBUG, "Secret data:\n");
790 av_get_packet(pb, &pkt, len); av_hex_dump_log(s, AV_LOG_DEBUG, pkt.data, pkt.size); av_free_packet(&pkt);
792 get_tag(s, "ASF_Protection_Type", -1, len, 32);
794 get_tag(s, "ASF_Key_ID", -1, len, 32);
796 get_tag(s, "ASF_License_URL", -1, len, 32);
797 } else if (!ff_guidcmp(&g, &ff_asf_ext_content_encryption)) {
798 av_log(s, AV_LOG_WARNING,
799 "Ext DRM protected stream detected, decoding will likely fail!\n");
800 av_dict_set(&s->metadata, "encryption", "ASF Extended Content Encryption", 0);
801 } else if (!ff_guidcmp(&g, &ff_asf_digital_signature)) {
802 av_log(s, AV_LOG_INFO, "Digital signature detected!\n");
806 if (avio_tell(pb) != gpos + gsize)
807 av_log(s, AV_LOG_DEBUG,
808 "gpos mismatch our pos=%"PRIu64", end=%"PRId64"\n",
809 avio_tell(pb) - gpos, gsize);
810 avio_seek(pb, gpos + gsize, SEEK_SET);
818 asf->data_offset = avio_tell(pb);
819 asf->packet_size_left = 0;
821 for (i = 0; i < 128; i++) {
822 int stream_num = asf->asfid2avid[i];
823 if (stream_num >= 0) {
824 AVStream *st = s->streams[stream_num];
825 if (!st->codec->bit_rate)
826 st->codec->bit_rate = asf->stream_bitrates[i];
827 if (asf->dar[i].num > 0 && asf->dar[i].den > 0) {
828 av_reduce(&st->sample_aspect_ratio.num,
829 &st->sample_aspect_ratio.den,
830 asf->dar[i].num, asf->dar[i].den, INT_MAX);
831 } else if ((asf->dar[0].num > 0) && (asf->dar[0].den > 0) &&
832 // Use ASF container value if the stream doesn't set AR.
833 (st->codec->codec_type == AVMEDIA_TYPE_VIDEO))
834 av_reduce(&st->sample_aspect_ratio.num,
835 &st->sample_aspect_ratio.den,
836 asf->dar[0].num, asf->dar[0].den, INT_MAX);
838 av_dlog(s, "i=%d, st->codec->codec_type:%d, asf->dar %d:%d sar=%d:%d\n",
839 i, st->codec->codec_type, asf->dar[i].num, asf->dar[i].den,
840 st->sample_aspect_ratio.num, st->sample_aspect_ratio.den);
842 // copy and convert language codes to the frontend
843 if (asf->streams[i].stream_language_index < 128) {
844 const char *rfc1766 = asf->stream_languages[asf->streams[i].stream_language_index];
845 if (rfc1766 && strlen(rfc1766) > 1) {
846 const char primary_tag[3] = { rfc1766[0], rfc1766[1], '\0' }; // ignore country code if any
847 const char *iso6392 = av_convert_lang_to(primary_tag,
848 AV_LANG_ISO639_2_BIBL);
850 av_dict_set(&st->metadata, "language", iso6392, 0);
856 ff_metadata_conv(&s->metadata, NULL, ff_asf_metadata_conv);
861 #define DO_2BITS(bits, var, defval) \
862 switch (bits & 3) { \
864 var = avio_rl32(pb); \
868 var = avio_rl16(pb); \
881 * Load a single ASF packet into the demuxer.
882 * @param s demux context
883 * @param pb context to read data from
884 * @return 0 on success, <0 on error
886 static int asf_get_packet(AVFormatContext *s, AVIOContext *pb)
888 ASFContext *asf = s->priv_data;
889 uint32_t packet_length, padsize;
893 // if we do not know packet size, allow skipping up to 32 kB
895 if (asf->no_resync_search)
897 else if (s->packet_size > 0)
898 off = (avio_tell(pb) - s->data_offset) % s->packet_size + 3;
905 if (c == 0x82 && !d && !e)
910 /* This code allows handling of -EAGAIN at packet boundaries (i.e.
911 * if the packet sync code above triggers -EAGAIN). This does not
912 * imply complete -EAGAIN handling support at random positions in
914 if (pb->error == AVERROR(EAGAIN))
915 return AVERROR(EAGAIN);
917 av_log(s, AV_LOG_ERROR,
918 "ff asf bad header %x at:%"PRId64"\n", c, avio_tell(pb));
920 if ((c & 0x8f) == 0x82) {
923 av_log(s, AV_LOG_ERROR, "ff asf bad non zero\n");
924 return AVERROR_INVALIDDATA;
929 } else if(!url_feof(pb)) {
930 avio_seek(pb, -1, SEEK_CUR); // FIXME
933 asf->packet_flags = c;
934 asf->packet_property = d;
936 DO_2BITS(asf->packet_flags >> 5, packet_length, s->packet_size);
937 DO_2BITS(asf->packet_flags >> 1, padsize, 0); // sequence ignored
938 DO_2BITS(asf->packet_flags >> 3, padsize, 0); // padding length
940 // the following checks prevent overflows and infinite loops
941 if (!packet_length || packet_length >= (1U << 29)) {
942 av_log(s, AV_LOG_ERROR,
943 "invalid packet_length %d at:%"PRId64"\n",
944 packet_length, avio_tell(pb));
945 return AVERROR_INVALIDDATA;
947 if (padsize >= packet_length) {
948 av_log(s, AV_LOG_ERROR,
949 "invalid padsize %d at:%"PRId64"\n", padsize, avio_tell(pb));
950 return AVERROR_INVALIDDATA;
953 asf->packet_timestamp = avio_rl32(pb);
954 avio_rl16(pb); /* duration */
955 // rsize has at least 11 bytes which have to be present
957 if (asf->packet_flags & 0x01) {
958 asf->packet_segsizetype = avio_r8(pb);
960 asf->packet_segments = asf->packet_segsizetype & 0x3f;
962 asf->packet_segments = 1;
963 asf->packet_segsizetype = 0x80;
965 if (rsize > packet_length - padsize) {
966 asf->packet_size_left = 0;
967 av_log(s, AV_LOG_ERROR,
968 "invalid packet header length %d for pktlen %d-%d at %"PRId64"\n",
969 rsize, packet_length, padsize, avio_tell(pb));
970 return AVERROR_INVALIDDATA;
972 asf->packet_size_left = packet_length - padsize - rsize;
973 if (packet_length < asf->hdr.min_pktsize)
974 padsize += asf->hdr.min_pktsize - packet_length;
975 asf->packet_padsize = padsize;
976 av_dlog(s, "packet: size=%d padsize=%d left=%d\n",
977 s->packet_size, asf->packet_padsize, asf->packet_size_left);
983 * @return <0 if error
985 static int asf_read_frame_header(AVFormatContext *s, AVIOContext *pb)
987 ASFContext *asf = s->priv_data;
990 int num = avio_r8(pb);
992 int64_t ts0, ts1 av_unused;
994 asf->packet_segments--;
995 asf->packet_key_frame = num >> 7;
996 asf->stream_index = asf->asfid2avid[num & 0x7f];
997 asfst = &asf->streams[num & 0x7f];
998 // sequence should be ignored!
999 DO_2BITS(asf->packet_property >> 4, asf->packet_seq, 0);
1000 DO_2BITS(asf->packet_property >> 2, asf->packet_frag_offset, 0);
1001 DO_2BITS(asf->packet_property, asf->packet_replic_size, 0);
1002 av_dlog(asf, "key:%d stream:%d seq:%d offset:%d replic_size:%d\n",
1003 asf->packet_key_frame, asf->stream_index, asf->packet_seq,
1004 asf->packet_frag_offset, asf->packet_replic_size);
1005 if (rsize+(int64_t)asf->packet_replic_size > asf->packet_size_left) {
1006 av_log(s, AV_LOG_ERROR, "packet_replic_size %d is invalid\n", asf->packet_replic_size);
1007 return AVERROR_INVALIDDATA;
1009 if (asf->packet_replic_size >= 8) {
1010 int64_t end = avio_tell(pb) + asf->packet_replic_size;
1012 asf->packet_obj_size = avio_rl32(pb);
1013 if (asf->packet_obj_size >= (1 << 24) || asf->packet_obj_size <= 0) {
1014 av_log(s, AV_LOG_ERROR, "packet_obj_size invalid\n");
1015 asf->packet_obj_size = 0;
1016 return AVERROR_INVALIDDATA;
1018 asf->packet_frag_timestamp = avio_rl32(pb); // timestamp
1020 for (i = 0; i < asfst->payload_ext_ct; i++) {
1021 ASFPayload *p = &asfst->payload[i];
1025 size = avio_rl16(pb);
1026 payend = avio_tell(pb) + size;
1028 av_log(s, AV_LOG_ERROR, "too long payload\n");
1033 // duration = avio_rl16(pb);
1036 aspect.num = avio_r8(pb);
1037 aspect.den = avio_r8(pb);
1038 if (aspect.num > 0 && aspect.den > 0 && asf->stream_index >= 0) {
1039 s->streams[asf->stream_index]->sample_aspect_ratio = aspect;
1044 ts0 = avio_rl64(pb);
1045 ts1 = avio_rl64(pb);
1046 if (ts0!= -1) asf->packet_frag_timestamp = ts0/10000;
1047 else asf->packet_frag_timestamp = AV_NOPTS_VALUE;
1057 avio_seek(pb, payend, SEEK_SET);
1060 avio_seek(pb, end, SEEK_SET);
1061 rsize += asf->packet_replic_size; // FIXME - check validity
1062 } else if (asf->packet_replic_size == 1) {
1063 // multipacket - frag_offset is beginning timestamp
1064 asf->packet_time_start = asf->packet_frag_offset;
1065 asf->packet_frag_offset = 0;
1066 asf->packet_frag_timestamp = asf->packet_timestamp;
1068 asf->packet_time_delta = avio_r8(pb);
1070 } else if (asf->packet_replic_size != 0) {
1071 av_log(s, AV_LOG_ERROR, "unexpected packet_replic_size of %d\n",
1072 asf->packet_replic_size);
1073 return AVERROR_INVALIDDATA;
1075 if (asf->packet_flags & 0x01) {
1076 DO_2BITS(asf->packet_segsizetype >> 6, asf->packet_frag_size, 0); // 0 is illegal
1077 if (rsize > asf->packet_size_left) {
1078 av_log(s, AV_LOG_ERROR, "packet_replic_size is invalid\n");
1079 return AVERROR_INVALIDDATA;
1080 } else if (asf->packet_frag_size > asf->packet_size_left - rsize) {
1081 if (asf->packet_frag_size > asf->packet_size_left - rsize + asf->packet_padsize) {
1082 av_log(s, AV_LOG_ERROR, "packet_frag_size is invalid (%d-%d)\n",
1083 asf->packet_size_left, rsize);
1084 return AVERROR_INVALIDDATA;
1086 int diff = asf->packet_frag_size - (asf->packet_size_left - rsize);
1087 asf->packet_size_left += diff;
1088 asf->packet_padsize -= diff;
1092 asf->packet_frag_size = asf->packet_size_left - rsize;
1094 if (asf->packet_replic_size == 1) {
1095 asf->packet_multi_size = asf->packet_frag_size;
1096 if (asf->packet_multi_size > asf->packet_size_left)
1097 return AVERROR_INVALIDDATA;
1099 asf->packet_size_left -= rsize;
1105 * Parse data from individual ASF packets (which were previously loaded
1106 * with asf_get_packet()).
1107 * @param s demux context
1108 * @param pb context to read data from
1109 * @param pkt pointer to store packet data into
1110 * @return 0 if data was stored in pkt, <0 on error or 1 if more ASF
1111 * packets need to be loaded (through asf_get_packet())
1113 static int asf_parse_packet(AVFormatContext *s, AVIOContext *pb, AVPacket *pkt)
1115 ASFContext *asf = s->priv_data;
1116 ASFStream *asf_st = 0;
1122 if (asf->packet_size_left < FRAME_HEADER_SIZE ||
1123 asf->packet_segments < 1) {
1124 int ret = asf->packet_size_left + asf->packet_padsize;
1130 asf->packet_pos = avio_tell(pb);
1131 if (asf->data_object_size != (uint64_t)-1 &&
1132 (asf->packet_pos - asf->data_object_offset >= asf->data_object_size))
1133 return AVERROR_EOF; /* Do not exceed the size of the data object */
1136 if (asf->packet_time_start == 0) {
1137 if (asf_read_frame_header(s, pb) < 0) {
1138 asf->packet_segments = 0;
1141 if (asf->stream_index < 0 ||
1142 s->streams[asf->stream_index]->discard >= AVDISCARD_ALL ||
1143 (!asf->packet_key_frame &&
1144 s->streams[asf->stream_index]->discard >= AVDISCARD_NONKEY)) {
1145 asf->packet_time_start = 0;
1146 /* unhandled packet (should not happen) */
1147 avio_skip(pb, asf->packet_frag_size);
1148 asf->packet_size_left -= asf->packet_frag_size;
1149 if (asf->stream_index < 0)
1150 av_log(s, AV_LOG_ERROR, "ff asf skip %d (unknown stream)\n",
1151 asf->packet_frag_size);
1154 asf->asf_st = s->streams[asf->stream_index]->priv_data;
1156 asf_st = asf->asf_st;
1159 if (asf->packet_replic_size == 1) {
1160 // frag_offset is here used as the beginning timestamp
1161 asf->packet_frag_timestamp = asf->packet_time_start;
1162 asf->packet_time_start += asf->packet_time_delta;
1163 asf->packet_obj_size = asf->packet_frag_size = avio_r8(pb);
1164 asf->packet_size_left--;
1165 asf->packet_multi_size--;
1166 if (asf->packet_multi_size < asf->packet_obj_size) {
1167 asf->packet_time_start = 0;
1168 avio_skip(pb, asf->packet_multi_size);
1169 asf->packet_size_left -= asf->packet_multi_size;
1172 asf->packet_multi_size -= asf->packet_obj_size;
1174 if (asf_st->frag_offset + asf->packet_frag_size <= asf_st->pkt.size &&
1175 asf_st->frag_offset + asf->packet_frag_size > asf->packet_obj_size) {
1176 av_log(s, AV_LOG_INFO, "ignoring invalid packet_obj_size (%d %d %d %d)\n",
1177 asf_st->frag_offset, asf->packet_frag_size,
1178 asf->packet_obj_size, asf_st->pkt.size);
1179 asf->packet_obj_size = asf_st->pkt.size;
1182 if (asf_st->pkt.size != asf->packet_obj_size ||
1183 // FIXME is this condition sufficient?
1184 asf_st->frag_offset + asf->packet_frag_size > asf_st->pkt.size) {
1185 if (asf_st->pkt.data) {
1186 av_log(s, AV_LOG_INFO,
1187 "freeing incomplete packet size %d, new %d\n",
1188 asf_st->pkt.size, asf->packet_obj_size);
1189 asf_st->frag_offset = 0;
1190 av_free_packet(&asf_st->pkt);
1193 av_new_packet(&asf_st->pkt, asf->packet_obj_size);
1194 asf_st->seq = asf->packet_seq;
1195 asf_st->pkt.dts = asf->packet_frag_timestamp - asf->hdr.preroll;
1196 asf_st->pkt.stream_index = asf->stream_index;
1197 asf_st->pkt.pos = asf_st->packet_pos = asf->packet_pos;
1199 if (asf_st->pkt.data && asf_st->palette_changed) {
1201 pal = av_packet_new_side_data(&asf_st->pkt, AV_PKT_DATA_PALETTE,
1204 av_log(s, AV_LOG_ERROR, "Cannot append palette to packet\n");
1206 memcpy(pal, asf_st->palette, AVPALETTE_SIZE);
1207 asf_st->palette_changed = 0;
1210 av_dlog(asf, "new packet: stream:%d key:%d packet_key:%d audio:%d size:%d\n",
1211 asf->stream_index, asf->packet_key_frame,
1212 asf_st->pkt.flags & AV_PKT_FLAG_KEY,
1213 s->streams[asf->stream_index]->codec->codec_type == AVMEDIA_TYPE_AUDIO,
1214 asf->packet_obj_size);
1215 if (s->streams[asf->stream_index]->codec->codec_type == AVMEDIA_TYPE_AUDIO)
1216 asf->packet_key_frame = 1;
1217 if (asf->packet_key_frame)
1218 asf_st->pkt.flags |= AV_PKT_FLAG_KEY;
1222 av_dlog(asf, "READ PACKET s:%d os:%d o:%d,%d l:%d DATA:%p\n",
1223 s->packet_size, asf_st->pkt.size, asf->packet_frag_offset,
1224 asf_st->frag_offset, asf->packet_frag_size, asf_st->pkt.data);
1225 asf->packet_size_left -= asf->packet_frag_size;
1226 if (asf->packet_size_left < 0)
1229 if (asf->packet_frag_offset >= asf_st->pkt.size ||
1230 asf->packet_frag_size > asf_st->pkt.size - asf->packet_frag_offset) {
1231 av_log(s, AV_LOG_ERROR,
1232 "packet fragment position invalid %u,%u not in %u\n",
1233 asf->packet_frag_offset, asf->packet_frag_size,
1238 ret = avio_read(pb, asf_st->pkt.data + asf->packet_frag_offset,
1239 asf->packet_frag_size);
1240 if (ret != asf->packet_frag_size) {
1241 if (ret < 0 || asf->packet_frag_offset + ret == 0)
1242 return ret < 0 ? ret : AVERROR_EOF;
1244 if (asf_st->ds_span > 1) {
1245 // scrambling, we can either drop it completely or fill the remainder
1246 // TODO: should we fill the whole packet instead of just the current
1248 memset(asf_st->pkt.data + asf->packet_frag_offset + ret, 0,
1249 asf->packet_frag_size - ret);
1250 ret = asf->packet_frag_size;
1252 // no scrambling, so we can return partial packets
1253 av_shrink_packet(&asf_st->pkt, asf->packet_frag_offset + ret);
1256 if (s->key && s->keylen == 20)
1257 ff_asfcrypt_dec(s->key, asf_st->pkt.data + asf->packet_frag_offset,
1259 asf_st->frag_offset += ret;
1260 /* test if whole packet is read */
1261 if (asf_st->frag_offset == asf_st->pkt.size) {
1262 // workaround for macroshit radio DVR-MS files
1263 if (s->streams[asf->stream_index]->codec->codec_id == AV_CODEC_ID_MPEG2VIDEO &&
1264 asf_st->pkt.size > 100) {
1266 for (i = 0; i < asf_st->pkt.size && !asf_st->pkt.data[i]; i++)
1268 if (i == asf_st->pkt.size) {
1269 av_log(s, AV_LOG_DEBUG, "discarding ms fart\n");
1270 asf_st->frag_offset = 0;
1271 av_free_packet(&asf_st->pkt);
1277 if (asf_st->ds_span > 1) {
1278 if (asf_st->pkt.size != asf_st->ds_packet_size * asf_st->ds_span) {
1279 av_log(s, AV_LOG_ERROR,
1280 "pkt.size != ds_packet_size * ds_span (%d %d %d)\n",
1281 asf_st->pkt.size, asf_st->ds_packet_size,
1284 /* packet descrambling */
1285 AVBufferRef *buf = av_buffer_alloc(asf_st->pkt.size +
1286 FF_INPUT_BUFFER_PADDING_SIZE);
1288 uint8_t *newdata = buf->data;
1290 memset(newdata + asf_st->pkt.size, 0,
1291 FF_INPUT_BUFFER_PADDING_SIZE);
1292 while (offset < asf_st->pkt.size) {
1293 int off = offset / asf_st->ds_chunk_size;
1294 int row = off / asf_st->ds_span;
1295 int col = off % asf_st->ds_span;
1296 int idx = row + col * asf_st->ds_packet_size / asf_st->ds_chunk_size;
1297 assert(offset + asf_st->ds_chunk_size <= asf_st->pkt.size);
1298 assert(idx + 1 <= asf_st->pkt.size / asf_st->ds_chunk_size);
1299 memcpy(newdata + offset,
1300 asf_st->pkt.data + idx * asf_st->ds_chunk_size,
1301 asf_st->ds_chunk_size);
1302 offset += asf_st->ds_chunk_size;
1304 av_buffer_unref(&asf_st->pkt.buf);
1305 asf_st->pkt.buf = buf;
1306 asf_st->pkt.data = buf->data;
1310 asf_st->frag_offset = 0;
1312 #if FF_API_DESTRUCT_PACKET
1313 asf_st->pkt.destruct = NULL;
1315 asf_st->pkt.buf = 0;
1316 asf_st->pkt.size = 0;
1317 asf_st->pkt.data = 0;
1318 asf_st->pkt.side_data_elems = 0;
1319 asf_st->pkt.side_data = NULL;
1320 break; // packet completed
1326 static int asf_read_packet(AVFormatContext *s, AVPacket *pkt)
1328 ASFContext *asf = s->priv_data;
1333 /* parse cached packets, if any */
1334 if ((ret = asf_parse_packet(s, s->pb, pkt)) <= 0)
1336 if ((ret = asf_get_packet(s, s->pb)) < 0)
1337 assert(asf->packet_size_left < FRAME_HEADER_SIZE ||
1338 asf->packet_segments < 1);
1339 asf->packet_time_start = 0;
1343 // Added to support seeking after packets have been read
1344 // If information is not reset, read_packet fails due to
1345 // leftover information from previous reads
1346 static void asf_reset_header(AVFormatContext *s)
1348 ASFContext *asf = s->priv_data;
1352 asf->packet_size_left = 0;
1353 asf->packet_segments = 0;
1354 asf->packet_flags = 0;
1355 asf->packet_property = 0;
1356 asf->packet_timestamp = 0;
1357 asf->packet_segsizetype = 0;
1358 asf->packet_segments = 0;
1359 asf->packet_seq = 0;
1360 asf->packet_replic_size = 0;
1361 asf->packet_key_frame = 0;
1362 asf->packet_padsize = 0;
1363 asf->packet_frag_offset = 0;
1364 asf->packet_frag_size = 0;
1365 asf->packet_frag_timestamp = 0;
1366 asf->packet_multi_size = 0;
1367 asf->packet_obj_size = 0;
1368 asf->packet_time_delta = 0;
1369 asf->packet_time_start = 0;
1371 for (i = 0; i < s->nb_streams; i++) {
1372 asf_st = s->streams[i]->priv_data;
1375 av_free_packet(&asf_st->pkt);
1376 asf_st->frag_offset = 0;
1382 static int asf_read_close(AVFormatContext *s)
1384 asf_reset_header(s);
1389 static int64_t asf_read_pts(AVFormatContext *s, int stream_index,
1390 int64_t *ppos, int64_t pos_limit)
1392 AVPacket pkt1, *pkt = &pkt1;
1395 int64_t pos = *ppos;
1397 int64_t start_pos[ASF_MAX_STREAMS];
1399 for (i = 0; i < s->nb_streams; i++)
1402 if (s->packet_size > 0)
1403 pos = (pos + s->packet_size - 1 - s->data_offset) /
1404 s->packet_size * s->packet_size +
1407 if (avio_seek(s->pb, pos, SEEK_SET) < 0)
1408 return AV_NOPTS_VALUE;
1410 asf_reset_header(s);
1412 if (av_read_frame(s, pkt) < 0) {
1413 av_log(s, AV_LOG_INFO, "asf_read_pts failed\n");
1414 return AV_NOPTS_VALUE;
1419 av_free_packet(pkt);
1420 if (pkt->flags & AV_PKT_FLAG_KEY) {
1421 i = pkt->stream_index;
1423 asf_st = s->streams[i]->priv_data;
1426 // assert((asf_st->packet_pos - s->data_offset) % s->packet_size == 0);
1427 pos = asf_st->packet_pos;
1429 av_add_index_entry(s->streams[i], pos, pts, pkt->size,
1430 pos - start_pos[i] + 1, AVINDEX_KEYFRAME);
1431 start_pos[i] = asf_st->packet_pos + 1;
1433 if (pkt->stream_index == stream_index)
1442 static void asf_build_simple_index(AVFormatContext *s, int stream_index)
1445 ASFContext *asf = s->priv_data;
1446 int64_t current_pos = avio_tell(s->pb);
1448 if(avio_seek(s->pb, asf->data_object_offset + asf->data_object_size, SEEK_SET) < 0) {
1449 asf->index_read= -1;
1453 ff_get_guid(s->pb, &g);
1455 /* the data object can be followed by other top-level objects,
1456 * skip them until the simple index object is reached */
1457 while (ff_guidcmp(&g, &ff_asf_simple_index_header)) {
1458 int64_t gsize = avio_rl64(s->pb);
1459 if (gsize < 24 || url_feof(s->pb)) {
1460 avio_seek(s->pb, current_pos, SEEK_SET);
1461 asf->index_read= -1;
1464 avio_skip(s->pb, gsize - 24);
1465 ff_get_guid(s->pb, &g);
1469 int64_t itime, last_pos = -1;
1472 int64_t av_unused gsize = avio_rl64(s->pb);
1473 ff_get_guid(s->pb, &g);
1474 itime = avio_rl64(s->pb);
1475 pct = avio_rl32(s->pb);
1476 ict = avio_rl32(s->pb);
1477 av_log(s, AV_LOG_DEBUG,
1478 "itime:0x%"PRIx64", pct:%d, ict:%d\n", itime, pct, ict);
1480 for (i = 0; i < ict; i++) {
1481 int pktnum = avio_rl32(s->pb);
1482 int pktct = avio_rl16(s->pb);
1483 int64_t pos = s->data_offset + s->packet_size * (int64_t)pktnum;
1484 int64_t index_pts = FFMAX(av_rescale(itime, i, 10000) - asf->hdr.preroll, 0);
1486 if (pos != last_pos) {
1487 av_log(s, AV_LOG_DEBUG, "pktnum:%d, pktct:%d pts: %"PRId64"\n",
1488 pktnum, pktct, index_pts);
1489 av_add_index_entry(s->streams[stream_index], pos, index_pts,
1490 s->packet_size, 0, AVINDEX_KEYFRAME);
1494 asf->index_read = ict > 1;
1496 avio_seek(s->pb, current_pos, SEEK_SET);
1499 static int asf_read_seek(AVFormatContext *s, int stream_index,
1500 int64_t pts, int flags)
1502 ASFContext *asf = s->priv_data;
1503 AVStream *st = s->streams[stream_index];
1505 if (s->packet_size <= 0)
1508 /* Try using the protocol's read_seek if available */
1510 int ret = avio_seek_time(s->pb, stream_index, pts, flags);
1512 asf_reset_header(s);
1513 if (ret != AVERROR(ENOSYS))
1517 if (!asf->index_read)
1518 asf_build_simple_index(s, stream_index);
1520 if ((asf->index_read > 0 && st->index_entries)) {
1521 int index = av_index_search_timestamp(st, pts, flags);
1523 /* find the position */
1524 uint64_t pos = st->index_entries[index].pos;
1527 av_log(s, AV_LOG_DEBUG, "SEEKTO: %"PRId64"\n", pos);
1528 if(avio_seek(s->pb, pos, SEEK_SET) < 0)
1530 asf_reset_header(s);
1534 /* no index or seeking by index failed */
1535 if (ff_seek_frame_binary(s, stream_index, pts, flags) < 0)
1537 asf_reset_header(s);
1541 AVInputFormat ff_asf_demuxer = {
1543 .long_name = NULL_IF_CONFIG_SMALL("ASF (Advanced / Active Streaming Format)"),
1544 .priv_data_size = sizeof(ASFContext),
1545 .read_probe = asf_probe,
1546 .read_header = asf_read_header,
1547 .read_packet = asf_read_packet,
1548 .read_close = asf_read_close,
1549 .read_seek = asf_read_seek,
1550 .read_timestamp = asf_read_pts,
1551 .flags = AVFMT_NOBINSEARCH | AVFMT_NOGENSEARCH,
1552 .priv_class = &asf_class,