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/internal.h"
31 #include "libavutil/mathematics.h"
32 #include "libavutil/opt.h"
34 #include "avio_internal.h"
35 #include "avlanguage.h"
42 typedef struct ASFPayload {
47 typedef struct ASFStream {
59 int ds_span; /* descrambling */
65 uint16_t stream_language_index;
68 uint32_t palette[256];
71 ASFPayload payload[8];
74 typedef struct ASFContext {
76 int asfid2avid[128]; ///< conversion table from asf ID 2 AVStream ID
77 ASFStream streams[128]; ///< it's max number and it's not that big
78 uint32_t stream_bitrates[128]; ///< max number of streams, bitrate for each (for streaming)
80 char stream_languages[128][6]; ///< max number of streams, language for each (RFC1766, e.g. en-US)
81 /* non streamed additonnal info */
84 /* only for reading */
85 uint64_t data_offset; ///< beginning of the first data packet
86 uint64_t data_object_offset; ///< data object offset (excl. GUID & size)
87 uint64_t data_object_size; ///< size of the data object
95 int packet_segsizetype;
98 int packet_replic_size;
101 unsigned int packet_frag_offset;
102 unsigned int packet_frag_size;
103 int64_t packet_frag_timestamp;
105 int packet_multi_size;
106 int packet_time_delta;
107 int packet_time_start;
112 ASFStream *asf_st; ///< currently decoded stream
114 int no_resync_search;
120 static const AVOption options[] = {
121 { "no_resync_search", "Don't try to resynchronize by looking for a certain optional start code", offsetof(ASFContext, no_resync_search), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, AV_OPT_FLAG_DECODING_PARAM },
122 { "export_xmp", "Export full XMP metadata", offsetof(ASFContext, export_xmp), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, AV_OPT_FLAG_DECODING_PARAM },
126 static const AVClass asf_class = {
127 .class_name = "asf demuxer",
128 .item_name = av_default_item_name,
130 .version = LIBAVUTIL_VERSION_INT,
136 #define ASF_MAX_STREAMS 127
137 #define FRAME_HEADER_SIZE 6
138 // Fix Me! FRAME_HEADER_SIZE may be different.
139 // (7 is known to be too large for GipsyGuitar.wmv)
142 static const ff_asf_guid stream_bitrate_guid = { /* (http://get.to/sdp) */
143 0xce, 0x75, 0xf8, 0x7b, 0x8d, 0x46, 0xd1, 0x11, 0x8d, 0x82, 0x00, 0x60, 0x97, 0xc9, 0xa2, 0xb2
146 #define PRINT_IF_GUID(g, cmp) \
147 if (!ff_guidcmp(g, &cmp)) \
148 av_log(NULL, AV_LOG_TRACE, "(GUID: %s) ", # cmp)
150 static void print_guid(ff_asf_guid *g)
153 PRINT_IF_GUID(g, ff_asf_header);
154 else PRINT_IF_GUID(g, ff_asf_file_header);
155 else PRINT_IF_GUID(g, ff_asf_stream_header);
156 else PRINT_IF_GUID(g, ff_asf_audio_stream);
157 else PRINT_IF_GUID(g, ff_asf_audio_conceal_none);
158 else PRINT_IF_GUID(g, ff_asf_video_stream);
159 else PRINT_IF_GUID(g, ff_asf_video_conceal_none);
160 else PRINT_IF_GUID(g, ff_asf_command_stream);
161 else PRINT_IF_GUID(g, ff_asf_comment_header);
162 else PRINT_IF_GUID(g, ff_asf_codec_comment_header);
163 else PRINT_IF_GUID(g, ff_asf_codec_comment1_header);
164 else PRINT_IF_GUID(g, ff_asf_data_header);
165 else PRINT_IF_GUID(g, ff_asf_simple_index_header);
166 else PRINT_IF_GUID(g, ff_asf_head1_guid);
167 else PRINT_IF_GUID(g, ff_asf_head2_guid);
168 else PRINT_IF_GUID(g, ff_asf_my_guid);
169 else PRINT_IF_GUID(g, ff_asf_ext_stream_header);
170 else PRINT_IF_GUID(g, ff_asf_extended_content_header);
171 else PRINT_IF_GUID(g, ff_asf_ext_stream_embed_stream_header);
172 else PRINT_IF_GUID(g, ff_asf_ext_stream_audio_stream);
173 else PRINT_IF_GUID(g, ff_asf_metadata_header);
174 else PRINT_IF_GUID(g, ff_asf_metadata_library_header);
175 else PRINT_IF_GUID(g, ff_asf_marker_header);
176 else PRINT_IF_GUID(g, stream_bitrate_guid);
177 else PRINT_IF_GUID(g, ff_asf_language_guid);
179 av_log(NULL, AV_LOG_TRACE, "(GUID: unknown) ");
180 for (i = 0; i < 16; i++)
181 av_log(NULL, AV_LOG_TRACE, " 0x%02x,", (*g)[i]);
182 av_log(NULL, AV_LOG_TRACE, "}\n");
186 #define print_guid(g) while(0)
189 static int asf_probe(const AVProbeData *pd)
191 /* check file header */
192 if (!ff_guidcmp(pd->buf, &ff_asf_header))
193 return AVPROBE_SCORE_MAX;
198 /* size of type 2 (BOOL) is 32bit for "Extended Content Description Object"
199 * but 16 bit for "Metadata Object" and "Metadata Library Object" */
200 static int get_value(AVIOContext *pb, int type, int type2_size)
204 return (type2_size == 32) ? avio_rl32(pb) : avio_rl16(pb);
206 return avio_rl32(pb);
208 return avio_rl64(pb);
210 return avio_rl16(pb);
216 /* MSDN claims that this should be "compatible with the ID3 frame, APIC",
217 * but in reality this is only loosely similar */
218 static int asf_read_picture(AVFormatContext *s, int len)
220 AVPacket pkt = { 0 };
221 const CodecMime *mime = ff_id3v2_mime_tags;
222 enum AVCodecID id = AV_CODEC_ID_NONE;
224 uint8_t *desc = NULL;
226 int ret, type, picsize, desc_len;
228 /* type + picsize + mime + desc */
229 if (len < 1 + 4 + 2 + 2) {
230 av_log(s, AV_LOG_ERROR, "Invalid attached picture size: %d.\n", len);
231 return AVERROR_INVALIDDATA;
235 type = avio_r8(s->pb);
237 if (type >= FF_ARRAY_ELEMS(ff_id3v2_picture_types) || type < 0) {
238 av_log(s, AV_LOG_WARNING, "Unknown attached picture type: %d.\n", type);
242 /* picture data size */
243 picsize = avio_rl32(s->pb);
246 /* picture MIME type */
247 len -= avio_get_str16le(s->pb, len, mimetype, sizeof(mimetype));
248 while (mime->id != AV_CODEC_ID_NONE) {
249 if (!strncmp(mime->str, mimetype, sizeof(mimetype))) {
255 if (id == AV_CODEC_ID_NONE) {
256 av_log(s, AV_LOG_ERROR, "Unknown attached picture mimetype: %s.\n",
261 if (picsize >= len) {
262 av_log(s, AV_LOG_ERROR, "Invalid attached picture data size: %d >= %d.\n",
264 return AVERROR_INVALIDDATA;
267 /* picture description */
268 desc_len = (len - picsize) * 2 + 1;
269 desc = av_malloc(desc_len);
271 return AVERROR(ENOMEM);
272 len -= avio_get_str16le(s->pb, len - picsize, desc, desc_len);
274 ret = av_get_packet(s->pb, &pkt, picsize);
278 st = avformat_new_stream(s, NULL);
280 ret = AVERROR(ENOMEM);
283 st->disposition |= AV_DISPOSITION_ATTACHED_PIC;
284 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
285 st->codecpar->codec_id = id;
286 st->attached_pic = pkt;
287 st->attached_pic.stream_index = st->index;
288 st->attached_pic.flags |= AV_PKT_FLAG_KEY;
291 av_dict_set(&st->metadata, "title", desc, AV_DICT_DONT_STRDUP_VAL);
295 av_dict_set(&st->metadata, "comment", ff_id3v2_picture_types[type], 0);
301 av_packet_unref(&pkt);
305 static void get_id3_tag(AVFormatContext *s, int len)
307 ID3v2ExtraMeta *id3v2_extra_meta = NULL;
309 ff_id3v2_read(s, ID3v2_DEFAULT_MAGIC, &id3v2_extra_meta, len);
310 if (id3v2_extra_meta) {
311 ff_id3v2_parse_apic(s, id3v2_extra_meta);
312 ff_id3v2_parse_chapters(s, id3v2_extra_meta);
314 ff_id3v2_free_extra_meta(&id3v2_extra_meta);
317 static void get_tag(AVFormatContext *s, const char *key, int type, int len, int type2_size)
319 ASFContext *asf = s->priv_data;
321 int64_t off = avio_tell(s->pb);
324 av_assert0((unsigned)len < (INT_MAX - LEN) / 2);
326 if (!asf->export_xmp && !strncmp(key, "xmp", 3))
329 value = av_malloc(2 * len + LEN);
335 avio_get_str16le(s->pb, len, value, 2 * len + 1);
338 avio_read(s->pb, value, len);
342 if (!strcmp(key, "WM/Picture")) { // handle cover art
343 asf_read_picture(s, len);
344 } else if (!strcmp(key, "ID3")) { // handle ID3 tag
347 av_log(s, AV_LOG_VERBOSE, "Unsupported byte array in tag %s.\n", key);
354 uint64_t num = get_value(s->pb, type, type2_size);
355 snprintf(value, LEN, "%"PRIu64, num);
359 av_log(s, AV_LOG_DEBUG, "Unsupported GUID value in tag %s.\n", key);
362 av_log(s, AV_LOG_DEBUG,
363 "Unsupported value type %d in tag %s.\n", type, key);
367 av_dict_set(&s->metadata, key, value, 0);
371 avio_seek(s->pb, off + len, SEEK_SET);
374 static int asf_read_file_properties(AVFormatContext *s, int64_t size)
376 ASFContext *asf = s->priv_data;
377 AVIOContext *pb = s->pb;
379 ff_get_guid(pb, &asf->hdr.guid);
380 asf->hdr.file_size = avio_rl64(pb);
381 asf->hdr.create_time = avio_rl64(pb);
382 avio_rl64(pb); /* number of packets */
383 asf->hdr.play_time = avio_rl64(pb);
384 asf->hdr.send_time = avio_rl64(pb);
385 asf->hdr.preroll = avio_rl32(pb);
386 asf->hdr.ignore = avio_rl32(pb);
387 asf->hdr.flags = avio_rl32(pb);
388 asf->hdr.min_pktsize = avio_rl32(pb);
389 asf->hdr.max_pktsize = avio_rl32(pb);
390 if (asf->hdr.min_pktsize >= (1U << 29))
391 return AVERROR_INVALIDDATA;
392 asf->hdr.max_bitrate = avio_rl32(pb);
393 s->packet_size = asf->hdr.max_pktsize;
398 static int asf_read_stream_properties(AVFormatContext *s, int64_t size)
400 ASFContext *asf = s->priv_data;
401 AVIOContext *pb = s->pb;
405 enum AVMediaType type;
406 int type_specific_size, sizeX;
408 int64_t pos1, pos2, start_time;
409 int test_for_ext_stream_audio, is_dvr_ms_audio = 0;
411 if (s->nb_streams == ASF_MAX_STREAMS) {
412 av_log(s, AV_LOG_ERROR, "too many streams\n");
413 return AVERROR(EINVAL);
416 pos1 = avio_tell(pb);
418 st = avformat_new_stream(s, NULL);
420 return AVERROR(ENOMEM);
421 avpriv_set_pts_info(st, 32, 1, 1000); /* 32 bit pts in ms */
422 start_time = asf->hdr.preroll;
424 if (!(asf->hdr.flags & 0x01)) { // if we aren't streaming...
425 int64_t fsize = avio_size(pb);
426 if (fsize <= 0 || (int64_t)asf->hdr.file_size <= 0 ||
427 FFABS(fsize - (int64_t)asf->hdr.file_size) < FFMIN(fsize, asf->hdr.file_size)/20)
428 st->duration = asf->hdr.play_time /
429 (10000000 / 1000) - start_time;
433 test_for_ext_stream_audio = 0;
434 if (!ff_guidcmp(&g, &ff_asf_audio_stream)) {
435 type = AVMEDIA_TYPE_AUDIO;
436 } else if (!ff_guidcmp(&g, &ff_asf_video_stream)) {
437 type = AVMEDIA_TYPE_VIDEO;
438 } else if (!ff_guidcmp(&g, &ff_asf_jfif_media)) {
439 type = AVMEDIA_TYPE_VIDEO;
440 st->codecpar->codec_id = AV_CODEC_ID_MJPEG;
441 } else if (!ff_guidcmp(&g, &ff_asf_command_stream)) {
442 type = AVMEDIA_TYPE_DATA;
443 } else if (!ff_guidcmp(&g, &ff_asf_ext_stream_embed_stream_header)) {
444 test_for_ext_stream_audio = 1;
445 type = AVMEDIA_TYPE_UNKNOWN;
450 avio_skip(pb, 8); /* total_size */
451 type_specific_size = avio_rl32(pb);
453 st->id = avio_rl16(pb) & 0x7f; /* stream id */
454 // mapping of asf ID to AV stream ID;
455 asf->asfid2avid[st->id] = s->nb_streams - 1;
456 asf_st = &asf->streams[st->id];
460 if (test_for_ext_stream_audio) {
462 if (!ff_guidcmp(&g, &ff_asf_ext_stream_audio_stream)) {
463 type = AVMEDIA_TYPE_AUDIO;
474 st->codecpar->codec_type = type;
475 if (type == AVMEDIA_TYPE_AUDIO) {
476 int ret = ff_get_wav_header(s, pb, st->codecpar, type_specific_size, 0);
479 if (is_dvr_ms_audio) {
480 // codec_id and codec_tag are unreliable in dvr_ms
481 // files. Set them later by probing stream.
482 st->internal->request_probe = 1;
483 st->codecpar->codec_tag = 0;
485 if (st->codecpar->codec_id == AV_CODEC_ID_AAC)
486 st->need_parsing = AVSTREAM_PARSE_NONE;
488 st->need_parsing = AVSTREAM_PARSE_FULL;
489 /* We have to init the frame size at some point .... */
490 pos2 = avio_tell(pb);
491 if (size >= (pos2 + 8 - pos1 + 24)) {
492 asf_st->ds_span = avio_r8(pb);
493 asf_st->ds_packet_size = avio_rl16(pb);
494 asf_st->ds_chunk_size = avio_rl16(pb);
495 avio_rl16(pb); // ds_data_size
496 avio_r8(pb); // ds_silence_data
498 if (asf_st->ds_span > 1) {
499 if (!asf_st->ds_chunk_size ||
500 (asf_st->ds_packet_size / asf_st->ds_chunk_size <= 1) ||
501 asf_st->ds_packet_size % asf_st->ds_chunk_size)
502 asf_st->ds_span = 0; // disable descrambling
504 } else if (type == AVMEDIA_TYPE_VIDEO &&
505 size - (avio_tell(pb) - pos1 + 24) >= 51) {
509 avio_rl16(pb); /* size */
510 sizeX = avio_rl32(pb); /* size */
511 st->codecpar->width = avio_rl32(pb);
512 st->codecpar->height = avio_rl32(pb);
513 /* not available for asf */
514 avio_rl16(pb); /* panes */
515 st->codecpar->bits_per_coded_sample = avio_rl16(pb); /* depth */
516 tag1 = avio_rl32(pb);
519 st->codecpar->extradata_size = ffio_limit(pb, sizeX - 40);
520 st->codecpar->extradata = av_mallocz(st->codecpar->extradata_size +
521 AV_INPUT_BUFFER_PADDING_SIZE);
522 if (!st->codecpar->extradata)
523 return AVERROR(ENOMEM);
524 avio_read(pb, st->codecpar->extradata, st->codecpar->extradata_size);
527 /* Extract palette from extradata if bpp <= 8 */
528 /* This code assumes that extradata contains only palette */
529 /* This is true for all paletted codecs implemented in libavcodec */
530 if (st->codecpar->extradata_size && (st->codecpar->bits_per_coded_sample <= 8)) {
533 for (i = 0; i < FFMIN(st->codecpar->extradata_size, AVPALETTE_SIZE) / 4; i++)
534 asf_st->palette[i] = av_bswap32(((uint32_t *)st->codecpar->extradata)[i]);
536 memcpy(asf_st->palette, st->codecpar->extradata,
537 FFMIN(st->codecpar->extradata_size, AVPALETTE_SIZE));
539 asf_st->palette_changed = 1;
542 st->codecpar->codec_tag = tag1;
543 st->codecpar->codec_id = ff_codec_get_id(ff_codec_bmp_tags, tag1);
544 if (tag1 == MKTAG('D', 'V', 'R', ' ')) {
545 st->need_parsing = AVSTREAM_PARSE_FULL;
546 /* issue658 contains wrong w/h and MS even puts a fake seq header
547 * with wrong w/h in extradata while a correct one is in the stream.
548 * maximum lameness */
549 st->codecpar->width =
550 st->codecpar->height = 0;
551 av_freep(&st->codecpar->extradata);
552 st->codecpar->extradata_size = 0;
554 if (st->codecpar->codec_id == AV_CODEC_ID_H264)
555 st->need_parsing = AVSTREAM_PARSE_FULL_ONCE;
556 if (st->codecpar->codec_id == AV_CODEC_ID_MPEG4)
557 st->need_parsing = AVSTREAM_PARSE_FULL_ONCE;
559 pos2 = avio_tell(pb);
560 avio_skip(pb, size - (pos2 - pos1 + 24));
565 static int asf_read_ext_stream_properties(AVFormatContext *s, int64_t size)
567 ASFContext *asf = s->priv_data;
568 AVIOContext *pb = s->pb;
570 int ext_len, payload_ext_ct, stream_ct, i;
571 uint32_t leak_rate, stream_num;
572 unsigned int stream_languageid_index;
574 avio_rl64(pb); // starttime
575 avio_rl64(pb); // endtime
576 leak_rate = avio_rl32(pb); // leak-datarate
577 avio_rl32(pb); // bucket-datasize
578 avio_rl32(pb); // init-bucket-fullness
579 avio_rl32(pb); // alt-leak-datarate
580 avio_rl32(pb); // alt-bucket-datasize
581 avio_rl32(pb); // alt-init-bucket-fullness
582 avio_rl32(pb); // max-object-size
583 avio_rl32(pb); // flags (reliable,seekable,no_cleanpoints?,resend-live-cleanpoints, rest of bits reserved)
584 stream_num = avio_rl16(pb); // stream-num
586 stream_languageid_index = avio_rl16(pb); // stream-language-id-index
587 if (stream_num < 128)
588 asf->streams[stream_num].stream_language_index = stream_languageid_index;
590 avio_rl64(pb); // avg frametime in 100ns units
591 stream_ct = avio_rl16(pb); // stream-name-count
592 payload_ext_ct = avio_rl16(pb); // payload-extension-system-count
594 if (stream_num < 128) {
595 asf->stream_bitrates[stream_num] = leak_rate;
596 asf->streams[stream_num].payload_ext_ct = 0;
599 for (i = 0; i < stream_ct; i++) {
601 ext_len = avio_rl16(pb);
602 avio_skip(pb, ext_len);
605 for (i = 0; i < payload_ext_ct; i++) {
608 size = avio_rl16(pb);
609 ext_len = avio_rl32(pb);
611 return AVERROR_INVALIDDATA;
612 avio_skip(pb, ext_len);
613 if (stream_num < 128 && i < FF_ARRAY_ELEMS(asf->streams[stream_num].payload)) {
614 ASFPayload *p = &asf->streams[stream_num].payload[i];
617 av_log(s, AV_LOG_DEBUG, "Payload extension %x %d\n", g[0], p->size );
618 asf->streams[stream_num].payload_ext_ct ++;
625 static int asf_read_content_desc(AVFormatContext *s, int64_t size)
627 AVIOContext *pb = s->pb;
628 int len1, len2, len3, len4, len5;
630 len1 = avio_rl16(pb);
631 len2 = avio_rl16(pb);
632 len3 = avio_rl16(pb);
633 len4 = avio_rl16(pb);
634 len5 = avio_rl16(pb);
635 get_tag(s, "title", 0, len1, 32);
636 get_tag(s, "author", 0, len2, 32);
637 get_tag(s, "copyright", 0, len3, 32);
638 get_tag(s, "comment", 0, len4, 32);
644 static int asf_read_ext_content_desc(AVFormatContext *s, int64_t size)
646 AVIOContext *pb = s->pb;
647 ASFContext *asf = s->priv_data;
648 int desc_count, i, ret;
650 desc_count = avio_rl16(pb);
651 for (i = 0; i < desc_count; i++) {
652 int name_len, value_type, value_len;
655 name_len = avio_rl16(pb);
656 if (name_len % 2) // must be even, broken lavf versions wrote len-1
658 if ((ret = avio_get_str16le(pb, name_len, name, sizeof(name))) < name_len)
659 avio_skip(pb, name_len - ret);
660 value_type = avio_rl16(pb);
661 value_len = avio_rl16(pb);
662 if (!value_type && value_len % 2)
664 /* My sample has that stream set to 0 maybe that mean the container.
665 * ASF stream count starts at 1. I am using 0 to the container value
666 * since it's unused. */
667 if (!strcmp(name, "AspectRatioX"))
668 asf->dar[0].num = get_value(s->pb, value_type, 32);
669 else if (!strcmp(name, "AspectRatioY"))
670 asf->dar[0].den = get_value(s->pb, value_type, 32);
672 get_tag(s, name, value_type, value_len, 32);
678 static int asf_read_language_list(AVFormatContext *s, int64_t size)
680 AVIOContext *pb = s->pb;
681 ASFContext *asf = s->priv_data;
683 int stream_count = avio_rl16(pb);
684 for (j = 0; j < stream_count; j++) {
686 unsigned int lang_len = avio_r8(pb);
687 if ((ret = avio_get_str16le(pb, lang_len, lang,
688 sizeof(lang))) < lang_len)
689 avio_skip(pb, lang_len - ret);
691 av_strlcpy(asf->stream_languages[j], lang,
692 sizeof(*asf->stream_languages));
698 static int asf_read_metadata(AVFormatContext *s, int64_t size)
700 AVIOContext *pb = s->pb;
701 ASFContext *asf = s->priv_data;
702 int n, stream_num, name_len_utf16, name_len_utf8, value_len;
706 for (i = 0; i < n; i++) {
710 avio_rl16(pb); // lang_list_index
711 stream_num = avio_rl16(pb);
712 name_len_utf16 = avio_rl16(pb);
713 value_type = avio_rl16(pb); /* value_type */
714 value_len = avio_rl32(pb);
716 if (value_len < 0 || value_len > UINT16_MAX)
717 return AVERROR_INVALIDDATA;
719 name_len_utf8 = 2*name_len_utf16 + 1;
720 name = av_malloc(name_len_utf8);
722 return AVERROR(ENOMEM);
724 if ((ret = avio_get_str16le(pb, name_len_utf16, name, name_len_utf8)) < name_len_utf16)
725 avio_skip(pb, name_len_utf16 - ret);
726 av_log(s, AV_LOG_TRACE, "%d stream %d name_len %2d type %d len %4d <%s>\n",
727 i, stream_num, name_len_utf16, value_type, value_len, name);
729 if (!strcmp(name, "AspectRatioX")){
730 int aspect_x = get_value(s->pb, value_type, 16);
732 asf->dar[stream_num].num = aspect_x;
733 } else if(!strcmp(name, "AspectRatioY")){
734 int aspect_y = get_value(s->pb, value_type, 16);
736 asf->dar[stream_num].den = aspect_y;
738 get_tag(s, name, value_type, value_len, 16);
746 static int asf_read_marker(AVFormatContext *s, int64_t size)
748 AVIOContext *pb = s->pb;
749 ASFContext *asf = s->priv_data;
750 int i, count, name_len, ret;
753 avio_rl64(pb); // reserved 16 bytes
754 avio_rl64(pb); // ...
755 count = avio_rl32(pb); // markers count
756 avio_rl16(pb); // reserved 2 bytes
757 name_len = avio_rl16(pb); // name length
758 avio_skip(pb, name_len);
760 for (i = 0; i < count; i++) {
765 return AVERROR_INVALIDDATA;
767 avio_rl64(pb); // offset, 8 bytes
768 pres_time = avio_rl64(pb); // presentation time
769 pres_time -= asf->hdr.preroll * 10000;
770 avio_rl16(pb); // entry length
771 avio_rl32(pb); // send time
772 avio_rl32(pb); // flags
773 name_len = avio_rl32(pb); // name length
774 if ((unsigned)name_len > INT_MAX / 2)
775 return AVERROR_INVALIDDATA;
776 if ((ret = avio_get_str16le(pb, name_len * 2, name,
777 sizeof(name))) < name_len)
778 avio_skip(pb, name_len - ret);
779 avpriv_new_chapter(s, i, (AVRational) { 1, 10000000 }, pres_time,
780 AV_NOPTS_VALUE, name);
786 static int asf_read_header(AVFormatContext *s)
788 ASFContext *asf = s->priv_data;
790 AVIOContext *pb = s->pb;
795 if (ff_guidcmp(&g, &ff_asf_header))
796 return AVERROR_INVALIDDATA;
801 memset(&asf->asfid2avid, -1, sizeof(asf->asfid2avid));
803 for (i = 0; i<128; i++)
804 asf->streams[i].stream_language_index = 128; // invalid stream index means no language info
807 uint64_t gpos = avio_tell(pb);
810 gsize = avio_rl64(pb);
812 if (!ff_guidcmp(&g, &ff_asf_data_header)) {
813 asf->data_object_offset = avio_tell(pb);
814 /* If not streaming, gsize is not unlimited (how?),
815 * and there is enough space in the file.. */
816 if (!(asf->hdr.flags & 0x01) && gsize >= 100)
817 asf->data_object_size = gsize - 24;
819 asf->data_object_size = (uint64_t)-1;
823 return AVERROR_INVALIDDATA;
824 if (!ff_guidcmp(&g, &ff_asf_file_header)) {
825 ret = asf_read_file_properties(s, gsize);
826 } else if (!ff_guidcmp(&g, &ff_asf_stream_header)) {
827 ret = asf_read_stream_properties(s, gsize);
828 } else if (!ff_guidcmp(&g, &ff_asf_comment_header)) {
829 asf_read_content_desc(s, gsize);
830 } else if (!ff_guidcmp(&g, &ff_asf_language_guid)) {
831 asf_read_language_list(s, gsize);
832 } else if (!ff_guidcmp(&g, &ff_asf_extended_content_header)) {
833 asf_read_ext_content_desc(s, gsize);
834 } else if (!ff_guidcmp(&g, &ff_asf_metadata_header)) {
835 asf_read_metadata(s, gsize);
836 } else if (!ff_guidcmp(&g, &ff_asf_metadata_library_header)) {
837 asf_read_metadata(s, gsize);
838 } else if (!ff_guidcmp(&g, &ff_asf_ext_stream_header)) {
839 asf_read_ext_stream_properties(s, gsize);
841 // there could be an optional stream properties object to follow
842 // if so the next iteration will pick it up
844 } else if (!ff_guidcmp(&g, &ff_asf_head1_guid)) {
848 } else if (!ff_guidcmp(&g, &ff_asf_marker_header)) {
849 asf_read_marker(s, gsize);
850 } else if (avio_feof(pb)) {
854 if (!ff_guidcmp(&g, &ff_asf_content_encryption)) {
857 av_log(s, AV_LOG_WARNING,
858 "DRM protected stream detected, decoding will likely fail!\n");
860 av_log(s, AV_LOG_DEBUG, "Secret data:\n");
862 if ((ret = av_get_packet(pb, &pkt, len)) < 0)
864 av_hex_dump_log(s, AV_LOG_DEBUG, pkt.data, pkt.size);
865 av_packet_unref(&pkt);
868 if (len > UINT16_MAX)
869 return AVERROR_INVALIDDATA;
870 get_tag(s, "ASF_Protection_Type", -1, len, 32);
873 if (len > UINT16_MAX)
874 return AVERROR_INVALIDDATA;
875 get_tag(s, "ASF_Key_ID", -1, len, 32);
878 if (len > UINT16_MAX)
879 return AVERROR_INVALIDDATA;
880 get_tag(s, "ASF_License_URL", -1, len, 32);
881 } else if (!ff_guidcmp(&g, &ff_asf_ext_content_encryption)) {
882 av_log(s, AV_LOG_WARNING,
883 "Ext DRM protected stream detected, decoding will likely fail!\n");
884 av_dict_set(&s->metadata, "encryption", "ASF Extended Content Encryption", 0);
885 } else if (!ff_guidcmp(&g, &ff_asf_digital_signature)) {
886 av_log(s, AV_LOG_INFO, "Digital signature detected!\n");
893 if (avio_tell(pb) != gpos + gsize)
894 av_log(s, AV_LOG_DEBUG,
895 "gpos mismatch our pos=%"PRIu64", end=%"PRId64"\n",
896 avio_tell(pb) - gpos, gsize);
897 avio_seek(pb, gpos + gsize, SEEK_SET);
905 asf->data_offset = avio_tell(pb);
906 asf->packet_size_left = 0;
908 for (i = 0; i < 128; i++) {
909 int stream_num = asf->asfid2avid[i];
910 if (stream_num >= 0) {
911 AVStream *st = s->streams[stream_num];
912 if (!st->codecpar->bit_rate)
913 st->codecpar->bit_rate = asf->stream_bitrates[i];
914 if (asf->dar[i].num > 0 && asf->dar[i].den > 0) {
915 av_reduce(&st->sample_aspect_ratio.num,
916 &st->sample_aspect_ratio.den,
917 asf->dar[i].num, asf->dar[i].den, INT_MAX);
918 } else if ((asf->dar[0].num > 0) && (asf->dar[0].den > 0) &&
919 // Use ASF container value if the stream doesn't set AR.
920 (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO))
921 av_reduce(&st->sample_aspect_ratio.num,
922 &st->sample_aspect_ratio.den,
923 asf->dar[0].num, asf->dar[0].den, INT_MAX);
925 av_log(s, AV_LOG_TRACE, "i=%d, st->codecpar->codec_type:%d, asf->dar %d:%d sar=%d:%d\n",
926 i, st->codecpar->codec_type, asf->dar[i].num, asf->dar[i].den,
927 st->sample_aspect_ratio.num, st->sample_aspect_ratio.den);
929 // copy and convert language codes to the frontend
930 if (asf->streams[i].stream_language_index < 128) {
931 const char *rfc1766 = asf->stream_languages[asf->streams[i].stream_language_index];
932 if (rfc1766 && strlen(rfc1766) > 1) {
933 const char primary_tag[3] = { rfc1766[0], rfc1766[1], '\0' }; // ignore country code if any
934 const char *iso6392 = ff_convert_lang_to(primary_tag,
935 AV_LANG_ISO639_2_BIBL);
937 av_dict_set(&st->metadata, "language", iso6392, 0);
943 ff_metadata_conv(&s->metadata, NULL, ff_asf_metadata_conv);
948 #define DO_2BITS(bits, var, defval) \
949 switch (bits & 3) { \
951 var = avio_rl32(pb); \
955 var = avio_rl16(pb); \
968 * Load a single ASF packet into the demuxer.
969 * @param s demux context
970 * @param pb context to read data from
971 * @return 0 on success, <0 on error
973 static int asf_get_packet(AVFormatContext *s, AVIOContext *pb)
975 ASFContext *asf = s->priv_data;
976 uint32_t packet_length, padsize;
980 if (asf->uses_std_ecc > 0) {
981 // if we do not know packet size, allow skipping up to 32 kB
983 if (asf->no_resync_search)
985 // else if (s->packet_size > 0 && !asf->uses_std_ecc)
986 // off = (avio_tell(pb) - s->internal->data_offset) % s->packet_size + 3;
993 if (c == 0x82 && !d && !e)
998 /* This code allows handling of -EAGAIN at packet boundaries (i.e.
999 * if the packet sync code above triggers -EAGAIN). This does not
1000 * imply complete -EAGAIN handling support at random positions in
1002 if (pb->error == AVERROR(EAGAIN))
1003 return AVERROR(EAGAIN);
1005 av_log(s, AV_LOG_ERROR,
1006 "ff asf bad header %x at:%"PRId64"\n", c, avio_tell(pb));
1008 if ((c & 0x8f) == 0x82) {
1011 av_log(s, AV_LOG_ERROR, "ff asf bad non zero\n");
1012 return AVERROR_INVALIDDATA;
1017 } else if(!avio_feof(pb)) {
1018 avio_seek(pb, -1, SEEK_CUR); // FIXME
1027 avio_seek(pb, (c & 0xF) - 2, SEEK_CUR);
1032 avpriv_request_sample(s, "Invalid ECC byte");
1034 if (!asf->uses_std_ecc)
1035 asf->uses_std_ecc = (c == 0x82 && !d && !e) ? 1 : -1;
1039 asf->uses_std_ecc = -1;
1043 asf->packet_flags = c;
1044 asf->packet_property = d;
1046 DO_2BITS(asf->packet_flags >> 5, packet_length, s->packet_size);
1047 DO_2BITS(asf->packet_flags >> 1, padsize, 0); // sequence ignored
1048 DO_2BITS(asf->packet_flags >> 3, padsize, 0); // padding length
1050 // the following checks prevent overflows and infinite loops
1051 if (!packet_length || packet_length >= (1U << 29)) {
1052 av_log(s, AV_LOG_ERROR,
1053 "invalid packet_length %"PRIu32" at:%"PRId64"\n",
1054 packet_length, avio_tell(pb));
1055 return AVERROR_INVALIDDATA;
1057 if (padsize >= packet_length) {
1058 av_log(s, AV_LOG_ERROR,
1059 "invalid padsize %"PRIu32" at:%"PRId64"\n", padsize, avio_tell(pb));
1060 return AVERROR_INVALIDDATA;
1063 asf->packet_timestamp = avio_rl32(pb);
1064 avio_rl16(pb); /* duration */
1065 // rsize has at least 11 bytes which have to be present
1067 if (asf->packet_flags & 0x01) {
1068 asf->packet_segsizetype = avio_r8(pb);
1070 asf->packet_segments = asf->packet_segsizetype & 0x3f;
1072 asf->packet_segments = 1;
1073 asf->packet_segsizetype = 0x80;
1075 if (rsize > packet_length - padsize) {
1076 asf->packet_size_left = 0;
1077 av_log(s, AV_LOG_ERROR,
1078 "invalid packet header length %d for pktlen %"PRIu32"-%"PRIu32" at %"PRId64"\n",
1079 rsize, packet_length, padsize, avio_tell(pb));
1080 return AVERROR_INVALIDDATA;
1082 asf->packet_size_left = packet_length - padsize - rsize;
1083 if (packet_length < asf->hdr.min_pktsize)
1084 padsize += asf->hdr.min_pktsize - packet_length;
1085 asf->packet_padsize = padsize;
1086 av_log(s, AV_LOG_TRACE, "packet: size=%d padsize=%d left=%d\n",
1087 s->packet_size, asf->packet_padsize, asf->packet_size_left);
1093 * @return <0 if error
1095 static int asf_read_frame_header(AVFormatContext *s, AVIOContext *pb)
1097 ASFContext *asf = s->priv_data;
1100 int num = avio_r8(pb);
1102 int64_t ts0, ts1 av_unused;
1104 asf->packet_segments--;
1105 asf->packet_key_frame = num >> 7;
1106 asf->stream_index = asf->asfid2avid[num & 0x7f];
1107 asfst = &asf->streams[num & 0x7f];
1108 // sequence should be ignored!
1109 DO_2BITS(asf->packet_property >> 4, asf->packet_seq, 0);
1110 DO_2BITS(asf->packet_property >> 2, asf->packet_frag_offset, 0);
1111 DO_2BITS(asf->packet_property, asf->packet_replic_size, 0);
1112 av_log(asf, AV_LOG_TRACE, "key:%d stream:%d seq:%d offset:%d replic_size:%d num:%X packet_property %X\n",
1113 asf->packet_key_frame, asf->stream_index, asf->packet_seq,
1114 asf->packet_frag_offset, asf->packet_replic_size, num, asf->packet_property);
1115 if (rsize+(int64_t)asf->packet_replic_size > asf->packet_size_left) {
1116 av_log(s, AV_LOG_ERROR, "packet_replic_size %d is invalid\n", asf->packet_replic_size);
1117 return AVERROR_INVALIDDATA;
1119 if (asf->packet_replic_size >= 8) {
1120 int64_t end = avio_tell(pb) + asf->packet_replic_size;
1122 asfst->packet_obj_size = avio_rl32(pb);
1123 if (asfst->packet_obj_size >= (1 << 24) || asfst->packet_obj_size < 0) {
1124 av_log(s, AV_LOG_ERROR, "packet_obj_size %d invalid\n", asfst->packet_obj_size);
1125 asfst->packet_obj_size = 0;
1126 return AVERROR_INVALIDDATA;
1128 asf->packet_frag_timestamp = avio_rl32(pb); // timestamp
1130 for (i = 0; i < asfst->payload_ext_ct; i++) {
1131 ASFPayload *p = &asfst->payload[i];
1135 size = avio_rl16(pb);
1136 payend = avio_tell(pb) + size;
1138 av_log(s, AV_LOG_ERROR, "too long payload\n");
1143 // duration = avio_rl16(pb);
1146 aspect.num = avio_r8(pb);
1147 aspect.den = avio_r8(pb);
1148 if (aspect.num > 0 && aspect.den > 0 && asf->stream_index >= 0) {
1149 s->streams[asf->stream_index]->sample_aspect_ratio = aspect;
1154 ts0 = avio_rl64(pb);
1155 ts1 = avio_rl64(pb);
1156 if (ts0!= -1) asf->packet_frag_timestamp = ts0/10000;
1157 else asf->packet_frag_timestamp = AV_NOPTS_VALUE;
1168 avio_seek(pb, payend, SEEK_SET);
1171 avio_seek(pb, end, SEEK_SET);
1172 rsize += asf->packet_replic_size; // FIXME - check validity
1173 } else if (asf->packet_replic_size == 1) {
1174 // multipacket - frag_offset is beginning timestamp
1175 asf->packet_time_start = asf->packet_frag_offset;
1176 asf->packet_frag_offset = 0;
1177 asf->packet_frag_timestamp = asf->packet_timestamp;
1179 asf->packet_time_delta = avio_r8(pb);
1181 } else if (asf->packet_replic_size != 0) {
1182 av_log(s, AV_LOG_ERROR, "unexpected packet_replic_size of %d\n",
1183 asf->packet_replic_size);
1184 return AVERROR_INVALIDDATA;
1186 if (asf->packet_flags & 0x01) {
1187 DO_2BITS(asf->packet_segsizetype >> 6, asf->packet_frag_size, 0); // 0 is illegal
1188 if (rsize > asf->packet_size_left) {
1189 av_log(s, AV_LOG_ERROR, "packet_replic_size is invalid\n");
1190 return AVERROR_INVALIDDATA;
1191 } else if (asf->packet_frag_size > asf->packet_size_left - rsize) {
1192 if (asf->packet_frag_size > asf->packet_size_left - rsize + asf->packet_padsize) {
1193 av_log(s, AV_LOG_ERROR, "packet_frag_size is invalid (%d>%d-%d+%d)\n",
1194 asf->packet_frag_size, asf->packet_size_left, rsize, asf->packet_padsize);
1195 return AVERROR_INVALIDDATA;
1197 int diff = asf->packet_frag_size - (asf->packet_size_left - rsize);
1198 asf->packet_size_left += diff;
1199 asf->packet_padsize -= diff;
1203 asf->packet_frag_size = asf->packet_size_left - rsize;
1205 if (asf->packet_replic_size == 1) {
1206 asf->packet_multi_size = asf->packet_frag_size;
1207 if (asf->packet_multi_size > asf->packet_size_left)
1208 return AVERROR_INVALIDDATA;
1210 asf->packet_size_left -= rsize;
1216 * Parse data from individual ASF packets (which were previously loaded
1217 * with asf_get_packet()).
1218 * @param s demux context
1219 * @param pb context to read data from
1220 * @param pkt pointer to store packet data into
1221 * @return 0 if data was stored in pkt, <0 on error or 1 if more ASF
1222 * packets need to be loaded (through asf_get_packet())
1224 static int asf_parse_packet(AVFormatContext *s, AVIOContext *pb, AVPacket *pkt)
1226 ASFContext *asf = s->priv_data;
1227 ASFStream *asf_st = 0;
1232 if (asf->packet_size_left < FRAME_HEADER_SIZE ||
1233 asf->packet_segments < 1 && asf->packet_time_start == 0) {
1234 int ret = asf->packet_size_left + asf->packet_padsize;
1236 if (asf->packet_size_left && asf->packet_size_left < FRAME_HEADER_SIZE)
1237 av_log(s, AV_LOG_WARNING, "Skip due to FRAME_HEADER_SIZE\n");
1243 asf->packet_pos = avio_tell(pb);
1244 if (asf->data_object_size != (uint64_t)-1 &&
1245 (asf->packet_pos - asf->data_object_offset >= asf->data_object_size))
1246 return AVERROR_EOF; /* Do not exceed the size of the data object */
1249 if (asf->packet_time_start == 0) {
1250 if (asf_read_frame_header(s, pb) < 0) {
1251 asf->packet_time_start = asf->packet_segments = 0;
1254 if (asf->stream_index < 0 ||
1255 s->streams[asf->stream_index]->discard >= AVDISCARD_ALL ||
1256 (!asf->packet_key_frame &&
1257 (s->streams[asf->stream_index]->discard >= AVDISCARD_NONKEY || asf->streams[s->streams[asf->stream_index]->id].skip_to_key))) {
1258 asf->packet_time_start = 0;
1259 /* unhandled packet (should not happen) */
1260 avio_skip(pb, asf->packet_frag_size);
1261 asf->packet_size_left -= asf->packet_frag_size;
1262 if (asf->stream_index < 0)
1263 av_log(s, AV_LOG_ERROR, "ff asf skip %d (unknown stream)\n",
1264 asf->packet_frag_size);
1267 asf->asf_st = &asf->streams[s->streams[asf->stream_index]->id];
1268 if (!asf->packet_frag_offset)
1269 asf->asf_st->skip_to_key = 0;
1271 asf_st = asf->asf_st;
1274 if (!asf_st->frag_offset && asf->packet_frag_offset) {
1275 av_log(s, AV_LOG_TRACE, "skipping asf data pkt with fragment offset for "
1276 "stream:%d, expected:%d but got %d from pkt)\n",
1277 asf->stream_index, asf_st->frag_offset,
1278 asf->packet_frag_offset);
1279 avio_skip(pb, asf->packet_frag_size);
1280 asf->packet_size_left -= asf->packet_frag_size;
1284 if (asf->packet_replic_size == 1) {
1285 // frag_offset is here used as the beginning timestamp
1286 asf->packet_frag_timestamp = asf->packet_time_start;
1287 asf->packet_time_start += asf->packet_time_delta;
1288 asf_st->packet_obj_size = asf->packet_frag_size = avio_r8(pb);
1289 asf->packet_size_left--;
1290 asf->packet_multi_size--;
1291 if (asf->packet_multi_size < asf_st->packet_obj_size) {
1292 asf->packet_time_start = 0;
1293 avio_skip(pb, asf->packet_multi_size);
1294 asf->packet_size_left -= asf->packet_multi_size;
1297 asf->packet_multi_size -= asf_st->packet_obj_size;
1300 if (asf_st->pkt.size != asf_st->packet_obj_size ||
1301 // FIXME is this condition sufficient?
1302 asf_st->frag_offset + asf->packet_frag_size > asf_st->pkt.size) {
1305 if (asf_st->pkt.data) {
1306 av_log(s, AV_LOG_INFO,
1307 "freeing incomplete packet size %d, new %d\n",
1308 asf_st->pkt.size, asf_st->packet_obj_size);
1309 asf_st->frag_offset = 0;
1310 av_packet_unref(&asf_st->pkt);
1313 if ((ret = av_new_packet(&asf_st->pkt, asf_st->packet_obj_size)) < 0)
1315 asf_st->seq = asf->packet_seq;
1316 if (asf->ts_is_pts) {
1317 asf_st->pkt.pts = asf->packet_frag_timestamp - asf->hdr.preroll;
1319 asf_st->pkt.dts = asf->packet_frag_timestamp - asf->hdr.preroll;
1320 asf_st->pkt.stream_index = asf->stream_index;
1321 asf_st->pkt.pos = asf_st->packet_pos = asf->packet_pos;
1322 asf_st->pkt_clean = 0;
1324 if (asf_st->pkt.data && asf_st->palette_changed) {
1326 pal = av_packet_new_side_data(&asf_st->pkt, AV_PKT_DATA_PALETTE,
1329 av_log(s, AV_LOG_ERROR, "Cannot append palette to packet\n");
1331 memcpy(pal, asf_st->palette, AVPALETTE_SIZE);
1332 asf_st->palette_changed = 0;
1335 av_log(asf, AV_LOG_TRACE, "new packet: stream:%d key:%d packet_key:%d audio:%d size:%d\n",
1336 asf->stream_index, asf->packet_key_frame,
1337 asf_st->pkt.flags & AV_PKT_FLAG_KEY,
1338 s->streams[asf->stream_index]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO,
1339 asf_st->packet_obj_size);
1340 if (s->streams[asf->stream_index]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)
1341 asf->packet_key_frame = 1;
1342 if (asf->packet_key_frame)
1343 asf_st->pkt.flags |= AV_PKT_FLAG_KEY;
1347 av_log(asf, AV_LOG_TRACE, "READ PACKET s:%d os:%d o:%d,%d l:%d DATA:%p\n",
1348 s->packet_size, asf_st->pkt.size, asf->packet_frag_offset,
1349 asf_st->frag_offset, asf->packet_frag_size, asf_st->pkt.data);
1350 asf->packet_size_left -= asf->packet_frag_size;
1351 if (asf->packet_size_left < 0)
1354 if (asf->packet_frag_offset >= asf_st->pkt.size ||
1355 asf->packet_frag_size > asf_st->pkt.size - asf->packet_frag_offset) {
1356 av_log(s, AV_LOG_ERROR,
1357 "packet fragment position invalid %u,%u not in %u\n",
1358 asf->packet_frag_offset, asf->packet_frag_size,
1363 if (asf->packet_frag_offset != asf_st->frag_offset && !asf_st->pkt_clean) {
1364 memset(asf_st->pkt.data + asf_st->frag_offset, 0, asf_st->pkt.size - asf_st->frag_offset);
1365 asf_st->pkt_clean = 1;
1368 ret = avio_read(pb, asf_st->pkt.data + asf->packet_frag_offset,
1369 asf->packet_frag_size);
1370 if (ret != asf->packet_frag_size) {
1371 if (ret < 0 || asf->packet_frag_offset + ret == 0)
1372 return ret < 0 ? ret : AVERROR_EOF;
1374 if (asf_st->ds_span > 1) {
1375 // scrambling, we can either drop it completely or fill the remainder
1376 // TODO: should we fill the whole packet instead of just the current
1378 memset(asf_st->pkt.data + asf->packet_frag_offset + ret, 0,
1379 asf->packet_frag_size - ret);
1380 ret = asf->packet_frag_size;
1382 // no scrambling, so we can return partial packets
1383 av_shrink_packet(&asf_st->pkt, asf->packet_frag_offset + ret);
1386 if (s->key && s->keylen == 20)
1387 ff_asfcrypt_dec(s->key, asf_st->pkt.data + asf->packet_frag_offset,
1389 asf_st->frag_offset += ret;
1390 /* test if whole packet is read */
1391 if (asf_st->frag_offset == asf_st->pkt.size) {
1392 // workaround for macroshit radio DVR-MS files
1393 if (s->streams[asf->stream_index]->codecpar->codec_id == AV_CODEC_ID_MPEG2VIDEO &&
1394 asf_st->pkt.size > 100) {
1396 for (i = 0; i < asf_st->pkt.size && !asf_st->pkt.data[i]; i++)
1398 if (i == asf_st->pkt.size) {
1399 av_log(s, AV_LOG_DEBUG, "discarding ms fart\n");
1400 asf_st->frag_offset = 0;
1401 av_packet_unref(&asf_st->pkt);
1407 if (asf_st->ds_span > 1) {
1408 if (asf_st->pkt.size != asf_st->ds_packet_size * asf_st->ds_span) {
1409 av_log(s, AV_LOG_ERROR,
1410 "pkt.size != ds_packet_size * ds_span (%d %d %d)\n",
1411 asf_st->pkt.size, asf_st->ds_packet_size,
1414 /* packet descrambling */
1415 AVBufferRef *buf = av_buffer_alloc(asf_st->pkt.size +
1416 AV_INPUT_BUFFER_PADDING_SIZE);
1418 uint8_t *newdata = buf->data;
1420 memset(newdata + asf_st->pkt.size, 0,
1421 AV_INPUT_BUFFER_PADDING_SIZE);
1422 while (offset < asf_st->pkt.size) {
1423 int off = offset / asf_st->ds_chunk_size;
1424 int row = off / asf_st->ds_span;
1425 int col = off % asf_st->ds_span;
1426 int idx = row + col * asf_st->ds_packet_size / asf_st->ds_chunk_size;
1427 assert(offset + asf_st->ds_chunk_size <= asf_st->pkt.size);
1428 assert(idx + 1 <= asf_st->pkt.size / asf_st->ds_chunk_size);
1429 memcpy(newdata + offset,
1430 asf_st->pkt.data + idx * asf_st->ds_chunk_size,
1431 asf_st->ds_chunk_size);
1432 offset += asf_st->ds_chunk_size;
1434 av_buffer_unref(&asf_st->pkt.buf);
1435 asf_st->pkt.buf = buf;
1436 asf_st->pkt.data = buf->data;
1440 asf_st->frag_offset = 0;
1442 asf_st->pkt.buf = 0;
1443 asf_st->pkt.size = 0;
1444 asf_st->pkt.data = 0;
1445 asf_st->pkt.side_data_elems = 0;
1446 asf_st->pkt.side_data = NULL;
1447 break; // packet completed
1453 static int asf_read_packet(AVFormatContext *s, AVPacket *pkt)
1455 ASFContext *asf = s->priv_data;
1460 /* parse cached packets, if any */
1461 if ((ret = asf_parse_packet(s, s->pb, pkt)) <= 0)
1463 if ((ret = asf_get_packet(s, s->pb)) < 0)
1464 assert(asf->packet_size_left < FRAME_HEADER_SIZE ||
1465 asf->packet_segments < 1);
1466 asf->packet_time_start = 0;
1470 // Added to support seeking after packets have been read
1471 // If information is not reset, read_packet fails due to
1472 // leftover information from previous reads
1473 static void asf_reset_header(AVFormatContext *s)
1475 ASFContext *asf = s->priv_data;
1479 asf->packet_size_left = 0;
1480 asf->packet_flags = 0;
1481 asf->packet_property = 0;
1482 asf->packet_timestamp = 0;
1483 asf->packet_segsizetype = 0;
1484 asf->packet_segments = 0;
1485 asf->packet_seq = 0;
1486 asf->packet_replic_size = 0;
1487 asf->packet_key_frame = 0;
1488 asf->packet_padsize = 0;
1489 asf->packet_frag_offset = 0;
1490 asf->packet_frag_size = 0;
1491 asf->packet_frag_timestamp = 0;
1492 asf->packet_multi_size = 0;
1493 asf->packet_time_delta = 0;
1494 asf->packet_time_start = 0;
1496 for (i = 0; i < 128; i++) {
1497 asf_st = &asf->streams[i];
1498 av_packet_unref(&asf_st->pkt);
1499 asf_st->packet_obj_size = 0;
1500 asf_st->frag_offset = 0;
1506 static void skip_to_key(AVFormatContext *s)
1508 ASFContext *asf = s->priv_data;
1511 for (i = 0; i < 128; i++) {
1512 int j = asf->asfid2avid[i];
1513 ASFStream *asf_st = &asf->streams[i];
1514 if (j < 0 || s->streams[j]->codecpar->codec_type != AVMEDIA_TYPE_VIDEO)
1517 asf_st->skip_to_key = 1;
1521 static int asf_read_close(AVFormatContext *s)
1523 asf_reset_header(s);
1528 static int64_t asf_read_pts(AVFormatContext *s, int stream_index,
1529 int64_t *ppos, int64_t pos_limit)
1531 ASFContext *asf = s->priv_data;
1532 AVPacket pkt1, *pkt = &pkt1;
1535 int64_t pos = *ppos;
1537 int64_t start_pos[ASF_MAX_STREAMS];
1539 for (i = 0; i < s->nb_streams; i++)
1542 if (s->packet_size > 0)
1543 pos = (pos + s->packet_size - 1 - s->internal->data_offset) /
1544 s->packet_size * s->packet_size +
1545 s->internal->data_offset;
1547 if (avio_seek(s->pb, pos, SEEK_SET) < 0)
1548 return AV_NOPTS_VALUE;
1550 ff_read_frame_flush(s);
1551 asf_reset_header(s);
1553 if (av_read_frame(s, pkt) < 0) {
1554 av_log(s, AV_LOG_INFO, "asf_read_pts failed\n");
1555 return AV_NOPTS_VALUE;
1560 if (pkt->flags & AV_PKT_FLAG_KEY) {
1561 i = pkt->stream_index;
1563 asf_st = &asf->streams[s->streams[i]->id];
1565 // assert((asf_st->packet_pos - s->data_offset) % s->packet_size == 0);
1566 pos = asf_st->packet_pos;
1567 av_assert1(pkt->pos == asf_st->packet_pos);
1569 av_add_index_entry(s->streams[i], pos, pts, pkt->size,
1570 pos - start_pos[i] + 1, AVINDEX_KEYFRAME);
1571 start_pos[i] = asf_st->packet_pos + 1;
1573 if (pkt->stream_index == stream_index) {
1574 av_packet_unref(pkt);
1578 av_packet_unref(pkt);
1585 static int asf_build_simple_index(AVFormatContext *s, int stream_index)
1588 ASFContext *asf = s->priv_data;
1589 int64_t current_pos = avio_tell(s->pb);
1592 if((ret = avio_seek(s->pb, asf->data_object_offset + asf->data_object_size, SEEK_SET)) < 0) {
1596 if ((ret = ff_get_guid(s->pb, &g)) < 0)
1599 /* the data object can be followed by other top-level objects,
1600 * skip them until the simple index object is reached */
1601 while (ff_guidcmp(&g, &ff_asf_simple_index_header)) {
1602 int64_t gsize = avio_rl64(s->pb);
1603 if (gsize < 24 || avio_feof(s->pb)) {
1606 avio_skip(s->pb, gsize - 24);
1607 if ((ret = ff_get_guid(s->pb, &g)) < 0)
1612 int64_t itime, last_pos = -1;
1615 int64_t av_unused gsize = avio_rl64(s->pb);
1616 if ((ret = ff_get_guid(s->pb, &g)) < 0)
1618 itime = avio_rl64(s->pb);
1619 pct = avio_rl32(s->pb);
1620 ict = avio_rl32(s->pb);
1621 av_log(s, AV_LOG_DEBUG,
1622 "itime:0x%"PRIx64", pct:%d, ict:%d\n", itime, pct, ict);
1624 for (i = 0; i < ict; i++) {
1625 int pktnum = avio_rl32(s->pb);
1626 int pktct = avio_rl16(s->pb);
1627 int64_t pos = s->internal->data_offset + s->packet_size * (int64_t)pktnum;
1628 int64_t index_pts = FFMAX(av_rescale(itime, i, 10000) - asf->hdr.preroll, 0);
1630 if (avio_feof(s->pb)) {
1631 ret = AVERROR_INVALIDDATA;
1635 if (pos != last_pos) {
1636 av_log(s, AV_LOG_DEBUG, "pktnum:%d, pktct:%d pts: %"PRId64"\n",
1637 pktnum, pktct, index_pts);
1638 av_add_index_entry(s->streams[stream_index], pos, index_pts,
1639 s->packet_size, 0, AVINDEX_KEYFRAME);
1643 asf->index_read = ict > 1;
1646 // if (avio_feof(s->pb)) {
1649 avio_seek(s->pb, current_pos, SEEK_SET);
1653 static int asf_read_seek(AVFormatContext *s, int stream_index,
1654 int64_t pts, int flags)
1656 ASFContext *asf = s->priv_data;
1657 AVStream *st = s->streams[stream_index];
1660 if (s->packet_size <= 0)
1663 /* Try using the protocol's read_seek if available */
1665 int64_t ret = avio_seek_time(s->pb, stream_index, pts, flags);
1667 asf_reset_header(s);
1668 if (ret != AVERROR(ENOSYS))
1672 /* explicitly handle the case of seeking to 0 */
1674 asf_reset_header(s);
1675 avio_seek(s->pb, s->internal->data_offset, SEEK_SET);
1679 if (!asf->index_read) {
1680 ret = asf_build_simple_index(s, stream_index);
1682 asf->index_read = -1;
1685 if (asf->index_read > 0 && st->internal->index_entries) {
1686 int index = av_index_search_timestamp(st, pts, flags);
1688 /* find the position */
1689 uint64_t pos = st->internal->index_entries[index].pos;
1692 av_log(s, AV_LOG_DEBUG, "SEEKTO: %"PRId64"\n", pos);
1693 if(avio_seek(s->pb, pos, SEEK_SET) < 0)
1695 asf_reset_header(s);
1700 /* no index or seeking by index failed */
1701 if (ff_seek_frame_binary(s, stream_index, pts, flags) < 0)
1703 asf_reset_header(s);
1708 AVInputFormat ff_asf_demuxer = {
1710 .long_name = NULL_IF_CONFIG_SMALL("ASF (Advanced / Active Streaming Format)"),
1711 .priv_data_size = sizeof(ASFContext),
1712 .read_probe = asf_probe,
1713 .read_header = asf_read_header,
1714 .read_packet = asf_read_packet,
1715 .read_close = asf_read_close,
1716 .read_seek = asf_read_seek,
1717 .read_timestamp = asf_read_pts,
1718 .flags = AVFMT_NOBINSEARCH | AVFMT_NOGENSEARCH,
1719 .priv_class = &asf_class,