2 * Microsoft Advanced Streaming Format demuxer
3 * Copyright (c) 2014 Alexandra Hájková
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
22 #include "libavutil/attributes.h"
23 #include "libavutil/avassert.h"
24 #include "libavutil/avstring.h"
25 #include "libavutil/bswap.h"
26 #include "libavutil/common.h"
27 #include "libavutil/dict.h"
28 #include "libavutil/internal.h"
29 #include "libavutil/mathematics.h"
30 #include "libavutil/opt.h"
31 #include "libavutil/time_internal.h"
34 #include "avio_internal.h"
35 #include "avlanguage.h"
47 #define ASF_UNICODE 0x0
48 #define ASF_FLAG_BROADCAST 0x1
49 #define ASF_BYTE_ARRAY 0x1
50 #define ASF_TYPE_AUDIO 0x2
51 #define ASF_TYPE_VIDEO 0x1
52 #define ASF_STREAM_NUM 0x7F
53 #define ASF_MAX_STREAMS 128
54 #define BMP_HEADER_SIZE 40
55 #define ASF_NUM_OF_PAYLOADS 0x3F
56 #define ASF_ERROR_CORRECTION_LENGTH_TYPE 0x60
57 #define ASF_PACKET_ERROR_CORRECTION_DATA_SIZE 0x2
59 typedef struct GUIDParseTable {
62 int (*read_object)(AVFormatContext *, const struct GUIDParseTable *);
66 typedef struct ASFPacket {
69 uint32_t frame_num; // ASF payloads with the same number are parts of the same frame
77 typedef struct ASFStream {
78 uint8_t stream_index; // from packet header
79 int index; // stream index in AVFormatContext, set in asf_read_stream_properties
81 int indexed; // added index entries from the Simple Index Object or not
82 int8_t span; // for deinterleaving
83 uint16_t virtual_pkt_len;
84 uint16_t virtual_chunk_len;
89 typedef struct ASFStreamData{
91 AVDictionary *asf_met; // for storing per-stream metadata
92 AVRational aspect_ratio;
95 typedef struct ASFContext {
97 int is_simple_index; // is simple index present or not 1/0
101 uint64_t nb_packets; // ASF packets
102 uint32_t packet_size;
106 uint32_t b_flags; // flags with broadcast flag
107 uint32_t prop_flags; // file properties object flags
109 uint64_t data_size; // data object size
110 uint64_t unknown_size; // size of the unknown object
112 int64_t offset; // offset of the current object
115 int64_t first_packet_offset; // packet offset
116 int64_t unknown_offset; // for top level header objects or subobjects without specified behavior
118 // ASF file must not contain more than 128 streams according to the specification
119 ASFStream *asf_st[ASF_MAX_STREAMS];
120 ASFStreamData asf_sd[ASF_MAX_STREAMS];
123 int stream_index; // from packet header, for the subpayload case
125 // packet parameteres
126 uint64_t sub_header_offset; // offset of subplayload header
128 uint8_t dts_delta; // for subpayloads
129 uint32_t packet_size_internal; // packet size stored inside ASFPacket, can be 0
130 int64_t packet_offset; // offset of the current packet inside Data Object
131 uint32_t pad_len; // padding after payload
132 uint32_t rep_data_len;
135 uint64_t sub_left; // subpayloads left or not
136 unsigned int nb_sub; // number of subpayloads read so far from the current ASF packet
137 uint16_t mult_sub_len; // total length of subpayloads array inside multiple payload
138 uint64_t nb_mult_left; // multiple payloads left
139 int return_subpayload;
148 static int detect_unknown_subobject(AVFormatContext *s, int64_t offset, int64_t size);
149 static const GUIDParseTable *find_guid(ff_asf_guid guid);
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/2;
160 static void swap_guid(ff_asf_guid guid)
162 FFSWAP(unsigned char, guid[0], guid[3]);
163 FFSWAP(unsigned char, guid[1], guid[2]);
164 FFSWAP(unsigned char, guid[4], guid[5]);
165 FFSWAP(unsigned char, guid[6], guid[7]);
168 static void align_position(AVIOContext *pb, int64_t offset, uint64_t size)
170 if (size < INT64_MAX - offset && avio_tell(pb) != offset + size)
171 avio_seek(pb, offset + size, SEEK_SET);
174 static int asf_read_unknown(AVFormatContext *s, const GUIDParseTable *g)
176 ASFContext *asf = s->priv_data;
177 AVIOContext *pb = s->pb;
178 uint64_t size = avio_rl64(pb);
181 if (size > INT64_MAX)
182 return AVERROR_INVALIDDATA;
185 asf->unknown_size = size;
187 if (!g->is_subobject) {
188 if (!(ret = strcmp(g->name, "Header Extension")))
189 avio_skip(pb, 22); // skip reserved fields and Data Size
190 if ((ret = detect_unknown_subobject(s, asf->unknown_offset,
191 asf->unknown_size)) < 0)
195 av_log(s, AV_LOG_ERROR, "Too small size %"PRIu64" (< 24).\n", size);
196 return AVERROR_INVALIDDATA;
198 avio_skip(pb, size - 24);
204 static int get_asf_string(AVIOContext *pb, int maxlen, char *buf, int buflen)
209 return AVERROR(EINVAL);
210 while (ret + 1 < maxlen) {
213 GET_UTF16(ch, (ret += 2) <= maxlen ? avio_rl16(pb) : 0, break;);
214 PUT_UTF8(ch, tmp, if (q - buf < buflen - 1) *q++ = tmp;)
221 static int asf_read_marker(AVFormatContext *s, const GUIDParseTable *g)
223 ASFContext *asf = s->priv_data;
224 AVIOContext *pb = s->pb;
225 uint64_t size = avio_rl64(pb);
226 int i, nb_markers, ret;
231 avio_skip(pb, 8); // skip reserved GUID
232 nb_markers = avio_rl32(pb);
233 avio_skip(pb, 2); // skip reserved field
235 for (i = 0; i < len; i++)
238 for (i = 0; i < nb_markers; i++) {
243 pts -= asf->preroll * 10000;
244 avio_skip(pb, 2); // entry length
245 avio_skip(pb, 4); // send time
246 avio_skip(pb, 4); // flags
249 if ((ret = avio_get_str16le(pb, len, name,
250 sizeof(name))) < len)
251 avio_skip(pb, len - ret);
252 avpriv_new_chapter(s, i, (AVRational) { 1, 10000000 }, pts,
253 AV_NOPTS_VALUE, name);
255 align_position(pb, asf->offset, size);
260 static int asf_read_metadata(AVFormatContext *s, const char *title, uint16_t len,
261 unsigned char *ch, uint16_t buflen)
263 AVIOContext *pb = s->pb;
265 avio_get_str16le(pb, len, ch, buflen);
267 if (av_dict_set(&s->metadata, title, ch, 0) < 0)
268 av_log(s, AV_LOG_WARNING, "av_dict_set failed.\n");
274 static int asf_read_value(AVFormatContext *s, const uint8_t *name,
275 uint16_t val_len, int type, AVDictionary **met)
279 uint16_t buflen = 2 * val_len + 1;
280 AVIOContext *pb = s->pb;
282 value = av_malloc(buflen);
284 return AVERROR(ENOMEM);
285 if (type == ASF_UNICODE) {
286 // get_asf_string reads UTF-16 and converts it to UTF-8 which needs longer buffer
287 if ((ret = get_asf_string(pb, val_len, value, buflen)) < 0)
289 if (av_dict_set(met, name, value, 0) < 0)
290 av_log(s, AV_LOG_WARNING, "av_dict_set failed.\n");
293 if (val_len > sizeof(buf)) {
294 ret = AVERROR_INVALIDDATA;
297 if ((ret = avio_read(pb, value, val_len)) < 0)
299 if (ret < 2 * val_len)
302 value[2 * val_len - 1] = '\0';
303 snprintf(buf, sizeof(buf), "%s", value);
304 if (av_dict_set(met, name, buf, 0) < 0)
305 av_log(s, AV_LOG_WARNING, "av_dict_set failed.\n");
315 static int asf_read_generic_value(AVIOContext *pb, int type, uint64_t *value)
320 *value = avio_rl16(pb);
323 *value = avio_rl32(pb);
326 *value = avio_rl64(pb);
329 *value = avio_rl16(pb);
332 return AVERROR_INVALIDDATA;
338 static int asf_set_metadata(AVFormatContext *s, const uint8_t *name,
339 int type, AVDictionary **met)
341 AVIOContext *pb = s->pb;
346 ret = asf_read_generic_value(pb, type, &value);
350 snprintf(buf, sizeof(buf), "%"PRIu64, value);
351 if (av_dict_set(met, name, buf, 0) < 0)
352 av_log(s, AV_LOG_WARNING, "av_dict_set failed.\n");
357 /* MSDN claims that this should be "compatible with the ID3 frame, APIC",
358 * but in reality this is only loosely similar */
359 static int asf_read_picture(AVFormatContext *s, int len)
361 ASFContext *asf = s->priv_data;
362 AVPacket pkt = { 0 };
363 const CodecMime *mime = ff_id3v2_mime_tags;
364 enum AVCodecID id = AV_CODEC_ID_NONE;
366 uint8_t *desc = NULL;
368 int ret, type, picsize, desc_len;
371 /* type + picsize + mime + desc */
372 if (len < 1 + 4 + 2 + 2) {
373 av_log(s, AV_LOG_ERROR, "Invalid attached picture size: %d.\n", len);
374 return AVERROR_INVALIDDATA;
378 type = avio_r8(s->pb);
380 if (type >= FF_ARRAY_ELEMS(ff_id3v2_picture_types) || type < 0) {
381 av_log(s, AV_LOG_WARNING, "Unknown attached picture type: %d.\n", type);
385 /* picture data size */
386 picsize = avio_rl32(s->pb);
389 /* picture MIME type */
390 len -= avio_get_str16le(s->pb, len, mimetype, sizeof(mimetype));
391 while (mime->id != AV_CODEC_ID_NONE) {
392 if (!strncmp(mime->str, mimetype, sizeof(mimetype))) {
398 if (id == AV_CODEC_ID_NONE) {
399 av_log(s, AV_LOG_ERROR, "Unknown attached picture mimetype: %s.\n",
404 if (picsize >= len) {
405 av_log(s, AV_LOG_ERROR, "Invalid attached picture data size: %d >= %d.\n",
407 return AVERROR_INVALIDDATA;
410 /* picture description */
411 desc_len = (len - picsize) * 2 + 1;
412 desc = av_malloc(desc_len);
414 return AVERROR(ENOMEM);
415 len -= avio_get_str16le(s->pb, len - picsize, desc, desc_len);
417 ret = av_get_packet(s->pb, &pkt, picsize);
421 st = avformat_new_stream(s, NULL);
423 ret = AVERROR(ENOMEM);
426 asf->asf_st[asf->nb_streams] = av_mallocz(sizeof(*asf_st));
427 asf_st = asf->asf_st[asf->nb_streams];
429 ret = AVERROR(ENOMEM);
433 st->disposition |= AV_DISPOSITION_ATTACHED_PIC;
434 st->codecpar->codec_type = asf_st->type = AVMEDIA_TYPE_VIDEO;
435 st->codecpar->codec_id = id;
436 st->attached_pic = pkt;
437 st->attached_pic.stream_index = asf_st->index = st->index;
438 st->attached_pic.flags |= AV_PKT_FLAG_KEY;
443 if (av_dict_set(&st->metadata, "title", desc, AV_DICT_DONT_STRDUP_VAL) < 0)
444 av_log(s, AV_LOG_WARNING, "av_dict_set failed.\n");
448 if (av_dict_set(&st->metadata, "comment", ff_id3v2_picture_types[type], 0) < 0)
449 av_log(s, AV_LOG_WARNING, "av_dict_set failed.\n");
455 av_packet_unref(&pkt);
459 static void get_id3_tag(AVFormatContext *s, int len)
461 ID3v2ExtraMeta *id3v2_extra_meta = NULL;
463 ff_id3v2_read(s, ID3v2_DEFAULT_MAGIC, &id3v2_extra_meta, len);
464 if (id3v2_extra_meta)
465 ff_id3v2_parse_apic(s, &id3v2_extra_meta);
466 ff_id3v2_free_extra_meta(&id3v2_extra_meta);
469 static int process_metadata(AVFormatContext *s, const uint8_t *name, uint16_t name_len,
470 uint16_t val_len, uint16_t type, AVDictionary **met)
478 asf_read_value(s, name, val_len, type, met);
481 if (!strcmp(name, "WM/Picture")) // handle cover art
482 asf_read_picture(s, val_len);
483 else if (!strcmp(name, "ID3")) // handle ID3 tag
484 get_id3_tag(s, val_len);
486 asf_read_value(s, name, val_len, type, met);
489 ff_get_guid(s->pb, &guid);
492 if ((ret = asf_set_metadata(s, name, type, met)) < 0)
501 static int asf_read_ext_content(AVFormatContext *s, const GUIDParseTable *g)
503 ASFContext *asf = s->priv_data;
504 AVIOContext *pb = s->pb;
505 uint64_t size = avio_rl64(pb);
506 uint16_t nb_desc = avio_rl16(pb);
509 for (i = 0; i < nb_desc; i++) {
510 uint16_t name_len, type, val_len;
511 uint8_t *name = NULL;
513 name_len = avio_rl16(pb);
515 return AVERROR_INVALIDDATA;
516 name = av_malloc(name_len);
518 return AVERROR(ENOMEM);
519 avio_get_str16le(pb, name_len, name,
521 type = avio_rl16(pb);
522 // BOOL values are 16 bits long in the Metadata Object
523 // but 32 bits long in the Extended Content Description Object
524 if (type == ASF_BOOL)
526 val_len = avio_rl16(pb);
528 ret = process_metadata(s, name, name_len, val_len, type, &s->metadata);
534 align_position(pb, asf->offset, size);
538 static AVStream *find_stream(AVFormatContext *s, uint16_t st_num)
541 ASFContext *asf = s->priv_data;
544 for (i = 0; i < asf->nb_streams; i++) {
545 if (asf->asf_st[i]->stream_index == st_num) {
546 st = s->streams[asf->asf_st[i]->index];
554 static int asf_store_aspect_ratio(AVFormatContext *s, uint8_t st_num, uint8_t *name, int type)
556 ASFContext *asf = s->priv_data;
557 AVIOContext *pb = s->pb;
561 ret = asf_read_generic_value(pb, type, &value);
565 if (st_num < ASF_MAX_STREAMS) {
566 if (!strcmp(name, "AspectRatioX"))
567 asf->asf_sd[st_num].aspect_ratio.num = value;
569 asf->asf_sd[st_num].aspect_ratio.den = value;
574 static int asf_read_metadata_obj(AVFormatContext *s, const GUIDParseTable *g)
576 ASFContext *asf = s->priv_data;
577 AVIOContext *pb = s->pb;
578 uint64_t size = avio_rl64(pb);
579 uint16_t nb_recs = avio_rl16(pb); // number of records in the Description Records list
582 for (i = 0; i < nb_recs; i++) {
583 uint16_t name_len, buflen, type, val_len, st_num;
584 uint8_t *name = NULL;
586 avio_skip(pb, 2); // skip reserved field
587 st_num = avio_rl16(pb);
588 name_len = avio_rl16(pb);
589 buflen = 2 * name_len + 1;
592 type = avio_rl16(pb);
593 val_len = avio_rl32(pb);
594 name = av_malloc(buflen);
596 return AVERROR(ENOMEM);
597 avio_get_str16le(pb, name_len, name,
599 if (!strcmp(name, "AspectRatioX") || !strcmp(name, "AspectRatioY")) {
600 ret = asf_store_aspect_ratio(s, st_num, name, type);
606 if (st_num < ASF_MAX_STREAMS) {
607 if ((ret = process_metadata(s, name, name_len, val_len, type,
608 &asf->asf_sd[st_num].asf_met)) < 0) {
617 align_position(pb, asf->offset, size);
621 static int asf_read_content_desc(AVFormatContext *s, const GUIDParseTable *g)
623 ASFContext *asf = s->priv_data;
624 AVIOContext *pb = s->pb;
626 static const char *const titles[] =
627 { "Title", "Author", "Copyright", "Description", "Rate" };
628 uint16_t len[5], buflen[5] = { 0 };
630 uint64_t size = avio_rl64(pb);
632 for (i = 0; i < 5; i++) {
633 len[i] = avio_rl16(pb);
634 // utf8 string should be <= 2 * utf16 string, extra byte for the terminator
635 buflen[i] = 2 * len[i] + 1;
638 for (i = 0; i < 5; i++) {
639 ch = av_malloc(buflen[i]);
641 return(AVERROR(ENOMEM));
642 asf_read_metadata(s, titles[i], len[i], ch, buflen[i]);
645 align_position(pb, asf->offset, size);
650 static int asf_read_properties(AVFormatContext *s, const GUIDParseTable *g)
652 ASFContext *asf = s->priv_data;
653 AVIOContext *pb = s->pb;
654 time_t creation_time;
656 avio_rl64(pb); // read object size
657 avio_skip(pb, 16); // skip File ID
658 avio_skip(pb, 8); // skip File size
659 creation_time = avio_rl64(pb);
660 if (!(asf->b_flags & ASF_FLAG_BROADCAST)) {
665 // creation date is in 100 ns units from 1 Jan 1601, conversion to s
666 creation_time /= 10000000;
667 // there are 11644473600 seconds between 1 Jan 1601 and 1 Jan 1970
668 creation_time -= 11644473600;
669 tm = gmtime_r(&creation_time, &tmbuf);
671 if (!strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", tm))
676 if (av_dict_set(&s->metadata, "creation_time", buf, 0) < 0)
677 av_log(s, AV_LOG_WARNING, "av_dict_set failed.\n");
680 asf->nb_packets = avio_rl64(pb);
681 asf->duration = avio_rl64(pb) / 10000; // stream duration
682 avio_skip(pb, 8); // skip send duration
683 asf->preroll = avio_rl64(pb);
684 asf->duration -= asf->preroll;
685 asf->b_flags = avio_rl32(pb);
686 avio_skip(pb, 4); // skip minimal packet size
687 asf->packet_size = avio_rl32(pb);
688 avio_skip(pb, 4); // skip max_bitrate
693 static int parse_video_info(AVIOContext *pb, AVStream *st)
698 st->codecpar->width = avio_rl32(pb);
699 st->codecpar->height = avio_rl32(pb);
700 avio_skip(pb, 1); // skip reserved flags
701 size = avio_rl16(pb); // size of the Format Data
702 tag = ff_get_bmp_header(pb, st, NULL);
703 st->codecpar->codec_tag = tag;
704 st->codecpar->codec_id = ff_codec_get_id(ff_codec_bmp_tags, tag);
706 if (size > BMP_HEADER_SIZE) {
708 st->codecpar->extradata_size = size - BMP_HEADER_SIZE;
709 if (!(st->codecpar->extradata = av_malloc(st->codecpar->extradata_size +
710 AV_INPUT_BUFFER_PADDING_SIZE))) {
711 st->codecpar->extradata_size = 0;
712 return AVERROR(ENOMEM);
714 memset(st->codecpar->extradata + st->codecpar->extradata_size , 0,
715 AV_INPUT_BUFFER_PADDING_SIZE);
716 if ((ret = avio_read(pb, st->codecpar->extradata,
717 st->codecpar->extradata_size)) < 0)
723 static int asf_read_stream_properties(AVFormatContext *s, const GUIDParseTable *g)
725 ASFContext *asf = s->priv_data;
726 AVIOContext *pb = s->pb;
728 uint32_t err_data_len, ts_data_len; // type specific data length
730 ff_asf_guid stream_type;
731 enum AVMediaType type;
733 uint8_t stream_index;
737 // ASF file must not contain more than 128 streams according to the specification
738 if (asf->nb_streams >= ASF_MAX_STREAMS)
739 return AVERROR_INVALIDDATA;
741 size = avio_rl64(pb);
742 ff_get_guid(pb, &stream_type);
743 if (!ff_guidcmp(&stream_type, &ff_asf_audio_stream))
744 type = AVMEDIA_TYPE_AUDIO;
745 else if (!ff_guidcmp(&stream_type, &ff_asf_video_stream))
746 type = AVMEDIA_TYPE_VIDEO;
747 else if (!ff_guidcmp(&stream_type, &ff_asf_jfif_media))
748 type = AVMEDIA_TYPE_VIDEO;
749 else if (!ff_guidcmp(&stream_type, &ff_asf_command_stream))
750 type = AVMEDIA_TYPE_DATA;
751 else if (!ff_guidcmp(&stream_type,
752 &ff_asf_ext_stream_embed_stream_header))
753 type = AVMEDIA_TYPE_UNKNOWN;
755 return AVERROR_INVALIDDATA;
757 ff_get_guid(pb, &stream_type); // error correction type
758 avio_skip(pb, 8); // skip the time offset
759 ts_data_len = avio_rl32(pb);
760 err_data_len = avio_rl32(pb);
761 flags = avio_rl16(pb); // bit 15 - Encrypted Content
763 stream_index = flags & ASF_STREAM_NUM;
764 for (i = 0; i < asf->nb_streams; i++)
765 if (stream_index == asf->asf_st[i]->stream_index) {
766 av_log(s, AV_LOG_WARNING,
767 "Duplicate stream found, this stream will be ignored.\n");
768 align_position(pb, asf->offset, size);
772 st = avformat_new_stream(s, NULL);
774 return AVERROR(ENOMEM);
775 avpriv_set_pts_info(st, 32, 1, 1000); // pts should be dword, in milliseconds
776 st->codecpar->codec_type = type;
777 asf->asf_st[asf->nb_streams] = av_mallocz(sizeof(*asf_st));
778 if (!asf->asf_st[asf->nb_streams])
779 return AVERROR(ENOMEM);
780 asf_st = asf->asf_st[asf->nb_streams];
782 asf_st->stream_index = stream_index;
783 asf_st->index = st->index;
785 st->id = flags & ASF_STREAM_NUM;
786 av_init_packet(&asf_st->pkt.avpkt);
787 asf_st->pkt.data_size = 0;
788 avio_skip(pb, 4); // skip reserved field
791 case AVMEDIA_TYPE_AUDIO:
792 asf_st->type = AVMEDIA_TYPE_AUDIO;
793 if ((ret = ff_get_wav_header(s, pb, st->codecpar, ts_data_len, 0)) < 0)
796 case AVMEDIA_TYPE_VIDEO:
797 asf_st->type = AVMEDIA_TYPE_VIDEO;
798 if ((ret = parse_video_info(pb, st)) < 0)
802 avio_skip(pb, ts_data_len);
807 if (type == AVMEDIA_TYPE_AUDIO) {
808 uint8_t span = avio_r8(pb);
811 asf_st->virtual_pkt_len = avio_rl16(pb);
812 asf_st->virtual_chunk_len = avio_rl16(pb);
813 if (!asf_st->virtual_chunk_len || !asf_st->virtual_pkt_len)
814 return AVERROR_INVALIDDATA;
815 avio_skip(pb, err_data_len - 5);
817 avio_skip(pb, err_data_len - 1);
819 avio_skip(pb, err_data_len);
822 align_position(pb, asf->offset, size);
827 static void set_language(AVFormatContext *s, const char *rfc1766, AVDictionary **met)
829 // language abbr should contain at least 2 chars
830 if (rfc1766 && strlen(rfc1766) > 1) {
831 const char primary_tag[3] = { rfc1766[0], rfc1766[1], '\0' }; // ignore country code if any
832 const char *iso6392 = av_convert_lang_to(primary_tag,
833 AV_LANG_ISO639_2_BIBL);
835 if (av_dict_set(met, "language", iso6392, 0) < 0)
836 av_log(s, AV_LOG_WARNING, "av_dict_set failed.\n");
840 static int asf_read_ext_stream_properties(AVFormatContext *s, const GUIDParseTable *g)
842 ASFContext *asf = s->priv_data;
843 AVIOContext *pb = s->pb;
846 uint16_t nb_st_name, nb_pay_exts, st_num, lang_idx;
849 uint64_t start_time, end_time, time_per_frame;
850 uint64_t size = avio_rl64(pb);
852 start_time = avio_rl64(pb);
853 end_time = avio_rl64(pb);
854 bitrate = avio_rl32(pb);
855 avio_skip(pb, 28); // skip some unused values
856 st_num = avio_rl16(pb);
857 st_num &= ASF_STREAM_NUM;
858 lang_idx = avio_rl16(pb); // Stream Language ID Index
859 for (i = 0; i < asf->nb_streams; i++) {
860 if (st_num == asf->asf_st[i]->stream_index) {
861 st = s->streams[asf->asf_st[i]->index];
862 asf->asf_st[i]->lang_idx = lang_idx;
866 time_per_frame = avio_rl64(pb); // average time per frame
868 st->start_time = start_time;
869 st->duration = end_time - start_time;
870 st->codecpar->bit_rate = bitrate;
871 st->avg_frame_rate.num = 10000000;
872 st->avg_frame_rate.den = time_per_frame;
874 nb_st_name = avio_rl16(pb);
875 nb_pay_exts = avio_rl16(pb);
876 for (i = 0; i < nb_st_name; i++) {
879 avio_rl16(pb); // Language ID Index
884 for (i = 0; i < nb_pay_exts; i++) {
886 avio_skip(pb, 16); // Extension System ID
887 avio_skip(pb, 2); // Extension Data Size
892 if ((ret = ff_get_guid(pb, &guid)) < 0) {
893 align_position(pb, asf->offset, size);
899 if (g && !(strcmp(g->name, "Stream Properties"))) {
900 if ((ret = g->read_object(s, g)) < 0)
904 align_position(pb, asf->offset, size);
908 static int asf_read_language_list(AVFormatContext *s, const GUIDParseTable *g)
910 ASFContext *asf = s->priv_data;
911 AVIOContext *pb = s->pb;
913 uint64_t size = avio_rl64(pb);
914 uint16_t nb_langs = avio_rl16(pb);
916 if (nb_langs < ASF_MAX_STREAMS) {
917 for (i = 0; i < nb_langs; i++) {
922 if ((ret = get_asf_string(pb, len, asf->asf_sd[i].langs,
923 sizeof(asf->asf_sd[i].langs))) < 0) {
929 align_position(pb, asf->offset, size);
933 // returns data object offset when reading this object for the first time
934 static int asf_read_data(AVFormatContext *s, const GUIDParseTable *g)
936 ASFContext *asf = s->priv_data;
937 AVIOContext *pb = s->pb;
938 uint64_t size = asf->data_size = avio_rl64(pb);
941 if (!asf->data_reached) {
942 asf->data_reached = 1;
943 asf->data_offset = asf->offset;
946 for (i = 0; i < asf->nb_streams; i++) {
947 if (!(asf->b_flags & ASF_FLAG_BROADCAST))
948 s->streams[i]->duration = asf->duration;
950 asf->nb_mult_left = 0;
952 asf->state = PARSE_PACKET_HEADER;
953 asf->return_subpayload = 0;
954 asf->packet_size_internal = 0;
955 avio_skip(pb, 16); // skip File ID
956 size = avio_rl64(pb); // Total Data Packets
957 if (size != asf->nb_packets)
958 av_log(s, AV_LOG_WARNING,
959 "Number of Packets from File Properties Object is not equal to Total"
960 "Datapackets value! num of packets %"PRIu64" total num %"PRIu64".\n",
961 size, asf->nb_packets);
962 avio_skip(pb, 2); // skip reserved field
963 asf->first_packet_offset = avio_tell(pb);
964 if (pb->seekable && !(asf->b_flags & ASF_FLAG_BROADCAST))
965 align_position(pb, asf->offset, asf->data_size);
970 static int asf_read_simple_index(AVFormatContext *s, const GUIDParseTable *g)
972 ASFContext *asf = s->priv_data;
973 AVIOContext *pb = s->pb;
975 uint64_t interval; // index entry time interval in 100 ns units, usually it's 1s
976 uint32_t pkt_num, nb_entries;
977 int32_t prev_pkt_num = -1;
979 uint64_t size = avio_rl64(pb);
981 // simple index objects should be ordered by stream number, this loop tries to find
982 // the first not indexed video stream
983 for (i = 0; i < asf->nb_streams; i++) {
984 if ((asf->asf_st[i]->type == AVMEDIA_TYPE_VIDEO) && !asf->asf_st[i]->indexed) {
985 asf->asf_st[i]->indexed = 1;
986 st = s->streams[asf->asf_st[i]->index];
991 avio_skip(pb, size - 24); // if there's no video stream, skip index object
994 avio_skip(pb, 16); // skip File ID
995 interval = avio_rl64(pb);
997 nb_entries = avio_rl32(pb);
998 for (i = 0; i < nb_entries; i++) {
999 pkt_num = avio_rl32(pb);
1000 ret = avio_skip(pb, 2);
1002 av_log(s, AV_LOG_ERROR, "Skipping failed in asf_read_simple_index.\n");
1005 if (prev_pkt_num != pkt_num) {
1006 av_add_index_entry(st, asf->first_packet_offset + asf->packet_size *
1007 pkt_num, av_rescale(interval, i, 10000),
1008 asf->packet_size, 0, AVINDEX_KEYFRAME);
1009 prev_pkt_num = pkt_num;
1012 asf->is_simple_index = 1;
1013 align_position(pb, asf->offset, size);
1018 static const GUIDParseTable gdef[] = {
1019 { "Data", { 0x75, 0xB2, 0x26, 0x36, 0x66, 0x8E, 0x11, 0xCF, 0xA6, 0xD9, 0x00, 0xAA, 0x00, 0x62, 0xCE, 0x6C }, asf_read_data, 1 },
1020 { "Simple Index", { 0x33, 0x00, 0x08, 0x90, 0xE5, 0xB1, 0x11, 0xCF, 0x89, 0xF4, 0x00, 0xA0, 0xC9, 0x03, 0x49, 0xCB }, asf_read_simple_index, 1 },
1021 { "Content Description", { 0x75, 0xB2, 0x26, 0x33, 0x66 ,0x8E, 0x11, 0xCF, 0xA6, 0xD9, 0x00, 0xAA, 0x00, 0x62, 0xCE, 0x6C }, asf_read_content_desc, 1 },
1022 { "Extended Content Description", { 0xD2, 0xD0, 0xA4, 0x40, 0xE3, 0x07, 0x11, 0xD2, 0x97, 0xF0, 0x00, 0xA0, 0xC9, 0x5e, 0xA8, 0x50 }, asf_read_ext_content, 1 },
1023 { "Stream Bitrate Properties", { 0x7B, 0xF8, 0x75, 0xCE, 0x46, 0x8D, 0x11, 0xD1, 0x8D, 0x82, 0x00, 0x60, 0x97, 0xC9, 0xA2, 0xB2 }, asf_read_unknown, 1 },
1024 { "File Properties", { 0x8C, 0xAB, 0xDC, 0xA1, 0xA9, 0x47, 0x11, 0xCF, 0x8E, 0xE4, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65 }, asf_read_properties, 1 },
1025 { "Header Extension", { 0x5F, 0xBF, 0x03, 0xB5, 0xA9, 0x2E, 0x11, 0xCF, 0x8E, 0xE3, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65 }, asf_read_unknown, 0 },
1026 { "Stream Properties", { 0xB7, 0xDC, 0x07, 0x91, 0xA9, 0xB7, 0x11, 0xCF, 0x8E, 0xE6, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65 }, asf_read_stream_properties, 1 },
1027 { "Codec List", { 0x86, 0xD1, 0x52, 0x40, 0x31, 0x1D, 0x11, 0xD0, 0xA3, 0xA4, 0x00, 0xA0, 0xC9, 0x03, 0x48, 0xF6 }, asf_read_unknown, 1 },
1028 { "Marker", { 0xF4, 0x87, 0xCD, 0x01, 0xA9, 0x51, 0x11, 0xCF, 0x8E, 0xE6, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65 }, asf_read_marker, 1 },
1029 { "Script Command", { 0x1E, 0xFB, 0x1A, 0x30, 0x0B, 0x62, 0x11, 0xD0, 0xA3, 0x9B, 0x00, 0xA0, 0xC9, 0x03, 0x48, 0xF6 }, asf_read_unknown, 1 },
1030 { "Language List", { 0x7C, 0x43, 0x46, 0xa9, 0xef, 0xe0, 0x4B, 0xFC, 0xB2, 0x29, 0x39, 0x3e, 0xde, 0x41, 0x5c, 0x85 }, asf_read_language_list, 1},
1031 { "Padding", { 0x18, 0x06, 0xD4, 0x74, 0xCA, 0xDF, 0x45, 0x09, 0xA4, 0xBA, 0x9A, 0xAB, 0xCB, 0x96, 0xAA, 0xE8 }, asf_read_unknown, 1 },
1032 { "DRMv1 Header", { 0x22, 0x11, 0xB3, 0xFB, 0xBD, 0x23, 0x11, 0xD2, 0xB4, 0xB7, 0x00, 0xA0, 0xC9, 0x55, 0xFC, 0x6E }, asf_read_unknown, 1 },
1033 { "DRMv2 Header", { 0x29, 0x8A, 0xE6, 0x14, 0x26, 0x22, 0x4C, 0x17, 0xB9, 0x35, 0xDA, 0xE0, 0x7E, 0xE9, 0x28, 0x9c }, asf_read_unknown, 1 },
1034 { "Index", { 0xD6, 0xE2, 0x29, 0xD3, 0x35, 0xDA, 0x11, 0xD1, 0x90, 0x34, 0x00, 0xA0, 0xC9, 0x03, 0x49, 0xBE }, asf_read_unknown, 1 },
1035 { "Media Object Index", { 0xFE, 0xB1, 0x03, 0xF8, 0x12, 0xAD, 0x4C, 0x64, 0x84, 0x0F, 0x2A, 0x1D, 0x2F, 0x7A, 0xD4, 0x8C }, asf_read_unknown, 1 },
1036 { "Timecode Index", { 0x3C, 0xB7, 0x3F, 0xD0, 0x0C, 0x4A, 0x48, 0x03, 0x95, 0x3D, 0xED, 0xF7, 0xB6, 0x22, 0x8F, 0x0C }, asf_read_unknown, 0 },
1037 { "Bitrate_Mutual_Exclusion", { 0xD6, 0xE2, 0x29, 0xDC, 0x35, 0xDA, 0x11, 0xD1, 0x90, 0x34, 0x00, 0xA0, 0xC9, 0x03, 0x49, 0xBE }, asf_read_unknown, 1 },
1038 { "Error Correction", { 0x75, 0xB2, 0x26, 0x35, 0x66, 0x8E, 0x11, 0xCF, 0xA6, 0xD9, 0x00, 0xAA, 0x00, 0x62, 0xCE, 0x6C }, asf_read_unknown, 1 },
1039 { "Content Branding", { 0x22, 0x11, 0xB3, 0xFA, 0xBD, 0x23, 0x11, 0xD2, 0xB4, 0xB7, 0x00, 0xA0, 0xC9, 0x55, 0xFC, 0x6E }, asf_read_unknown, 1 },
1040 { "Content Encryption", { 0x22, 0x11, 0xB3, 0xFB, 0xBD, 0x23, 0x11, 0xD2, 0xB4, 0xB7, 0x00, 0xA0, 0xC9, 0x55, 0xFC, 0x6E }, asf_read_unknown, 1 },
1041 { "Extended Content Encryption", { 0x29, 0x8A, 0xE6, 0x14, 0x26, 0x22, 0x4C, 0x17, 0xB9, 0x35, 0xDA, 0xE0, 0x7E, 0xE9, 0x28, 0x9C }, asf_read_unknown, 1 },
1042 { "Digital Signature", { 0x22, 0x11, 0xB3, 0xFC, 0xBD, 0x23, 0x11, 0xD2, 0xB4, 0xB7, 0x00, 0xA0, 0xC9, 0x55, 0xFC, 0x6E }, asf_read_unknown, 1 },
1043 { "Extended Stream Properties", { 0x14, 0xE6, 0xA5, 0xCB, 0xC6, 0x72, 0x43, 0x32, 0x83, 0x99, 0xA9, 0x69, 0x52, 0x06, 0x5B, 0x5A }, asf_read_ext_stream_properties, 1 },
1044 { "Advanced Mutual Exclusion", { 0xA0, 0x86, 0x49, 0xCF, 0x47, 0x75, 0x46, 0x70, 0x8A, 0x16, 0x6E, 0x35, 0x35, 0x75, 0x66, 0xCD }, asf_read_unknown, 1 },
1045 { "Group Mutual Exclusion", { 0xD1, 0x46, 0x5A, 0x40, 0x5A, 0x79, 0x43, 0x38, 0xB7, 0x1B, 0xE3, 0x6B, 0x8F, 0xD6, 0xC2, 0x49 }, asf_read_unknown, 1},
1046 { "Stream Prioritization", { 0xD4, 0xFE, 0xD1, 0x5B, 0x88, 0xD3, 0x45, 0x4F, 0x81, 0xF0, 0xED, 0x5C, 0x45, 0x99, 0x9E, 0x24 }, asf_read_unknown, 1 },
1047 { "Bandwidth Sharing Object", { 0xA6, 0x96, 0x09, 0xE6, 0x51, 0x7B, 0x11, 0xD2, 0xB6, 0xAF, 0x00, 0xC0, 0x4F, 0xD9, 0x08, 0xE9 }, asf_read_unknown, 1 },
1048 { "Metadata", { 0xC5, 0xF8, 0xCB, 0xEA, 0x5B, 0xAF, 0x48, 0x77, 0x84, 0x67, 0xAA, 0x8C, 0x44, 0xFA, 0x4C, 0xCA }, asf_read_metadata_obj, 1 },
1049 { "Metadata Library", { 0x44, 0x23, 0x1C, 0x94, 0x94, 0x98, 0x49, 0xD1, 0xA1, 0x41, 0x1D, 0x13, 0x4E, 0x45, 0x70, 0x54 }, asf_read_metadata_obj, 1 },
1050 { "Audio Spread", { 0xBF, 0xC3, 0xCD, 0x50, 0x61, 0x8F, 0x11, 0xCF, 0x8B, 0xB2, 0x00, 0xAA, 0x00, 0xB4, 0xE2, 0x20 }, asf_read_unknown, 1 },
1051 { "Index Parameters", { 0xD6, 0xE2, 0x29, 0xDF, 0x35, 0xDA, 0x11, 0xD1, 0x90, 0x34, 0x00, 0xA0, 0xC9, 0x03, 0x49, 0xBE }, asf_read_unknown, 1 },
1052 { "Content Encryption System Windows Media DRM Network Devices",
1053 { 0x7A, 0x07, 0x9B, 0xB6, 0xDA, 0XA4, 0x4e, 0x12, 0xA5, 0xCA, 0x91, 0xD3, 0x8D, 0xC1, 0x1A, 0x8D }, asf_read_unknown, 1 },
1054 { "Mutex Language", { 0xD6, 0xE2, 0x2A, 0x00, 0x25, 0xDA, 0x11, 0xD1, 0x90, 0x34, 0x00, 0xA0, 0xC9, 0x03, 0x49, 0xBE }, asf_read_unknown, 1 },
1055 { "Mutex Bitrate", { 0xD6, 0xE2, 0x2A, 0x01, 0x25, 0xDA, 0x11, 0xD1, 0x90, 0x34, 0x00, 0xA0, 0xC9, 0x03, 0x49, 0xBE }, asf_read_unknown, 1 },
1056 { "Mutex Unknown", { 0xD6, 0xE2, 0x2A, 0x02, 0x25, 0xDA, 0x11, 0xD1, 0x90, 0x34, 0x00, 0xA0, 0xC9, 0x03, 0x49, 0xBE }, asf_read_unknown, 1 },
1057 { "Bandwidth Sharing Exclusive", { 0xAF, 0x60, 0x60, 0xAA, 0x51, 0x97, 0x11, 0xD2, 0xB6, 0xAF, 0x00, 0xC0, 0x4F, 0xD9, 0x08, 0xE9 }, asf_read_unknown, 1 },
1058 { "Bandwidth Sharing Partial", { 0xAF, 0x60, 0x60, 0xAB, 0x51, 0x97, 0x11, 0xD2, 0xB6, 0xAF, 0x00, 0xC0, 0x4F, 0xD9, 0x08, 0xE9 }, asf_read_unknown, 1 },
1059 { "Payload Extension System Timecode", { 0x39, 0x95, 0x95, 0xEC, 0x86, 0x67, 0x4E, 0x2D, 0x8F, 0xDB, 0x98, 0x81, 0x4C, 0xE7, 0x6C, 0x1E }, asf_read_unknown, 1 },
1060 { "Payload Extension System File Name", { 0xE1, 0x65, 0xEC, 0x0E, 0x19, 0xED, 0x45, 0xD7, 0xB4, 0xA7, 0x25, 0xCB, 0xD1, 0xE2, 0x8E, 0x9B }, asf_read_unknown, 1 },
1061 { "Payload Extension System Content Type", { 0xD5, 0x90, 0xDC, 0x20, 0x07, 0xBC, 0x43, 0x6C, 0x9C, 0xF7, 0xF3, 0xBB, 0xFB, 0xF1, 0xA4, 0xDC }, asf_read_unknown, 1 },
1062 { "Payload Extension System Pixel Aspect Ratio", { 0x1, 0x1E, 0xE5, 0x54, 0xF9, 0xEA, 0x4B, 0xC8, 0x82, 0x1A, 0x37, 0x6B, 0x74, 0xE4, 0xC4, 0xB8 }, asf_read_unknown, 1 },
1063 { "Payload Extension System Sample Duration", { 0xC6, 0xBD, 0x94, 0x50, 0x86, 0x7F, 0x49, 0x07, 0x83, 0xA3, 0xC7, 0x79, 0x21, 0xB7, 0x33, 0xAD }, asf_read_unknown, 1 },
1064 { "Payload Extension System Encryption Sample ID", { 0x66, 0x98, 0xB8, 0x4E, 0x0A, 0xFA, 0x43, 0x30, 0xAE, 0xB2, 0x1C, 0x0A, 0x98, 0xD7, 0xA4, 0x4D }, asf_read_unknown, 1 },
1065 { "Payload Extension System Degradable JPEG", { 0x00, 0xE1, 0xAF, 0x06, 0x7B, 0xEC, 0x11, 0xD1, 0xA5, 0x82, 0x00, 0xC0, 0x4F, 0xC2, 0x9C, 0xFB }, asf_read_unknown, 1 },
1068 #define READ_LEN(flag, name, len) \
1070 if ((flag) == name ## IS_BYTE) \
1071 len = avio_r8(pb); \
1072 else if ((flag) == name ## IS_WORD) \
1073 len = avio_rl16(pb); \
1074 else if ((flag) == name ## IS_DWORD) \
1075 len = avio_rl32(pb); \
1080 static int asf_read_subpayload(AVFormatContext *s, AVPacket *pkt, int is_header)
1082 ASFContext *asf = s->priv_data;
1083 AVIOContext *pb = s->pb;
1088 asf->dts_delta = avio_r8(pb);
1089 if (asf->nb_mult_left) {
1090 asf->mult_sub_len = avio_rl16(pb); // total
1092 asf->sub_header_offset = avio_tell(pb);
1096 sub_len = avio_r8(pb);
1097 if ((ret = av_get_packet(pb, pkt, sub_len)) < 0) // each subpayload is entire frame
1099 for (i = 0; i < asf->nb_streams; i++) {
1100 if (asf->stream_index == asf->asf_st[i]->stream_index) {
1101 pkt->stream_index = asf->asf_st[i]->index;
1105 asf->return_subpayload = 1;
1107 asf->return_subpayload = 0;
1111 pkt->dts = asf->sub_dts + (asf->nb_sub - 1) * asf->dts_delta - asf->preroll;
1112 if (asf->nb_mult_left && (avio_tell(pb) >=
1113 (asf->sub_header_offset + asf->mult_sub_len))) {
1115 asf->nb_mult_left--;
1117 if (avio_tell(pb) >= asf->packet_offset + asf->packet_size - asf->pad_len) {
1119 if (!asf->nb_mult_left) {
1120 avio_skip(pb, asf->pad_len);
1121 if (avio_tell(pb) != asf->packet_offset + asf->packet_size) {
1122 if (!asf->packet_size)
1123 return AVERROR_INVALIDDATA;
1124 av_log(s, AV_LOG_WARNING,
1125 "Position %"PRId64" wrong, should be %"PRId64"\n",
1126 avio_tell(pb), asf->packet_offset + asf->packet_size);
1127 avio_seek(pb, asf->packet_offset + asf->packet_size, SEEK_SET);
1135 static void reset_packet(ASFPacket *asf_pkt)
1137 asf_pkt->size_left = 0;
1138 asf_pkt->data_size = 0;
1139 asf_pkt->duration = 0;
1142 asf_pkt->duration = 0;
1143 av_packet_unref(&asf_pkt->avpkt);
1144 av_init_packet(&asf_pkt->avpkt);
1147 static int asf_read_replicated_data(AVFormatContext *s, ASFPacket *asf_pkt)
1149 ASFContext *asf = s->priv_data;
1150 AVIOContext *pb = s->pb;
1153 if (!asf_pkt->data_size) {
1154 data_size = avio_rl32(pb); // read media object size
1156 return AVERROR_INVALIDDATA;
1157 if ((ret = av_new_packet(&asf_pkt->avpkt, data_size)) < 0)
1159 asf_pkt->data_size = asf_pkt->size_left = data_size;
1161 avio_skip(pb, 4); // reading of media object size is already done
1162 asf_pkt->dts = avio_rl32(pb); // read presentation time
1163 if (asf->rep_data_len && (asf->rep_data_len >= 8))
1164 avio_skip(pb, asf->rep_data_len - 8); // skip replicated data
1169 static int asf_read_multiple_payload(AVFormatContext *s, AVPacket *pkt,
1172 ASFContext *asf = s->priv_data;
1173 AVIOContext *pb = s->pb;
1179 // if replicated length is 1, subpayloads are present
1180 if (asf->rep_data_len == 1) {
1182 asf->state = READ_MULTI_SUB;
1183 pkt->flags = asf_pkt->flags;
1184 if ((ret = asf_read_subpayload(s, pkt, 1)) < 0)
1187 if (asf->rep_data_len)
1188 if ((ret = asf_read_replicated_data(s, asf_pkt)) < 0)
1190 pay_len = avio_rl16(pb); // payload length should be WORD
1191 if (pay_len > asf->packet_size) {
1192 av_log(s, AV_LOG_ERROR,
1193 "Error: invalid data packet size, pay_len %"PRIu16", "
1194 "asf->packet_size %"PRIu32", offset %"PRId64".\n",
1195 pay_len, asf->packet_size, avio_tell(pb));
1196 return AVERROR_INVALIDDATA;
1198 p = asf_pkt->avpkt.data + asf_pkt->data_size - asf_pkt->size_left;
1199 if (pay_len > asf_pkt->size_left) {
1200 av_log(s, AV_LOG_ERROR,
1201 "Error: invalid buffer size, pay_len %d, data size left %d.\n",
1202 pay_len, asf_pkt->size_left);
1203 skip = pay_len - asf_pkt->size_left;
1204 pay_len = asf_pkt->size_left;
1206 if (asf_pkt->size_left <= 0)
1207 return AVERROR_INVALIDDATA;
1208 if ((ret = avio_read(pb, p, pay_len)) < 0)
1210 if (s->key && s->keylen == 20)
1211 ff_asfcrypt_dec(s->key, p, ret);
1212 avio_skip(pb, skip);
1213 asf_pkt->size_left -= pay_len;
1214 asf->nb_mult_left--;
1220 static int asf_read_single_payload(AVFormatContext *s, ASFPacket *asf_pkt)
1222 ASFContext *asf = s->priv_data;
1223 AVIOContext *pb = s->pb;
1229 if (!asf_pkt->data_size) {
1230 data_size = avio_rl32(pb); // read media object size
1233 if ((ret = av_new_packet(&asf_pkt->avpkt, data_size)) < 0)
1235 asf_pkt->data_size = asf_pkt->size_left = data_size;
1237 avio_skip(pb, 4); // skip media object size
1238 asf_pkt->dts = avio_rl32(pb); // read presentation time
1239 if (asf->rep_data_len >= 8)
1240 avio_skip(pb, asf->rep_data_len - 8); // skip replicated data
1241 offset = avio_tell(pb);
1243 // size of the payload - size of the packet without header and padding
1244 if (asf->packet_size_internal)
1245 size = asf->packet_size_internal - offset + asf->packet_offset - asf->pad_len;
1247 size = asf->packet_size - offset + asf->packet_offset - asf->pad_len;
1248 if (size > asf->packet_size) {
1249 av_log(s, AV_LOG_ERROR,
1250 "Error: invalid data packet size, offset %"PRId64".\n",
1252 return AVERROR_INVALIDDATA;
1254 p = asf_pkt->avpkt.data + asf_pkt->data_size - asf_pkt->size_left;
1255 if (size > asf_pkt->size_left || asf_pkt->size_left <= 0)
1256 return AVERROR_INVALIDDATA;
1257 if (asf_pkt->size_left > size)
1258 asf_pkt->size_left -= size;
1260 asf_pkt->size_left = 0;
1261 if ((ret = avio_read(pb, p, size)) < 0)
1263 if (s->key && s->keylen == 20)
1264 ff_asfcrypt_dec(s->key, p, ret);
1265 if (asf->packet_size_internal)
1266 avio_skip(pb, asf->packet_size - asf->packet_size_internal);
1267 avio_skip(pb, asf->pad_len); // skip padding
1272 static int asf_read_payload(AVFormatContext *s, AVPacket *pkt)
1274 ASFContext *asf = s->priv_data;
1275 AVIOContext *pb = s->pb;
1277 ASFPacket *asf_pkt = NULL;
1279 if (!asf->sub_left) {
1280 uint32_t off_len, media_len;
1283 stream_num = avio_r8(pb);
1284 asf->stream_index = stream_num & ASF_STREAM_NUM;
1285 for (i = 0; i < asf->nb_streams; i++) {
1286 if (asf->stream_index == asf->asf_st[i]->stream_index) {
1287 asf_pkt = &asf->asf_st[i]->pkt;
1288 asf_pkt->stream_index = asf->asf_st[i]->index;
1293 if (asf->packet_offset + asf->packet_size <= asf->data_offset + asf->data_size) {
1294 if (!asf->packet_size) {
1295 av_log(s, AV_LOG_ERROR, "Invalid packet size 0.\n");
1296 return AVERROR_INVALIDDATA;
1298 avio_seek(pb, asf->packet_offset + asf->packet_size, SEEK_SET);
1299 av_log(s, AV_LOG_WARNING, "Skipping the stream with the invalid stream index %d.\n",
1301 return AVERROR(EAGAIN);
1303 return AVERROR_INVALIDDATA;
1306 if (stream_num >> 7)
1307 asf_pkt->flags |= AV_PKT_FLAG_KEY;
1308 READ_LEN(asf->prop_flags & ASF_PL_MASK_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_SIZE,
1309 ASF_PL_FLAG_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_, media_len);
1310 READ_LEN(asf->prop_flags & ASF_PL_MASK_OFFSET_INTO_MEDIA_OBJECT_LENGTH_FIELD_SIZE,
1311 ASF_PL_FLAG_OFFSET_INTO_MEDIA_OBJECT_LENGTH_FIELD_, off_len);
1312 READ_LEN(asf->prop_flags & ASF_PL_MASK_REPLICATED_DATA_LENGTH_FIELD_SIZE,
1313 ASF_PL_FLAG_REPLICATED_DATA_LENGTH_FIELD_, asf->rep_data_len);
1314 if (asf_pkt->size_left && (asf_pkt->frame_num != media_len)) {
1315 av_log(s, AV_LOG_WARNING, "Unfinished frame will be ignored\n");
1316 reset_packet(asf_pkt);
1318 asf_pkt->frame_num = media_len;
1319 asf->sub_dts = off_len;
1320 if (asf->nb_mult_left) {
1321 if ((ret = asf_read_multiple_payload(s, pkt, asf_pkt)) < 0)
1323 } else if (asf->rep_data_len == 1) {
1325 asf->state = READ_SINGLE;
1326 pkt->flags = asf_pkt->flags;
1327 if ((ret = asf_read_subpayload(s, pkt, 1)) < 0)
1330 if ((ret = asf_read_single_payload(s, asf_pkt)) < 0)
1334 for (i = 0; i <= asf->nb_streams; i++) {
1335 if (asf->stream_index == asf->asf_st[i]->stream_index) {
1336 asf_pkt = &asf->asf_st[i]->pkt;
1341 return AVERROR_INVALIDDATA;
1342 pkt->flags = asf_pkt->flags;
1343 pkt->dts = asf_pkt->dts;
1344 pkt->stream_index = asf->asf_st[i]->index;
1345 if ((ret = asf_read_subpayload(s, pkt, 0)) < 0) // read subpayload without its header
1352 static int asf_read_packet_header(AVFormatContext *s)
1354 ASFContext *asf = s->priv_data;
1355 AVIOContext *pb = s->pb;
1357 uint32_t av_unused seq;
1358 unsigned char error_flags, len_flags, pay_flags;
1360 asf->packet_offset = avio_tell(pb);
1361 error_flags = avio_r8(pb); // read Error Correction Flags
1362 if (error_flags & ASF_PACKET_FLAG_ERROR_CORRECTION_PRESENT) {
1363 if (!(error_flags & ASF_ERROR_CORRECTION_LENGTH_TYPE)) {
1364 size = error_flags & ASF_PACKET_ERROR_CORRECTION_DATA_SIZE;
1365 avio_skip(pb, size);
1367 len_flags = avio_r8(pb);
1369 len_flags = error_flags;
1370 asf->prop_flags = avio_r8(pb);
1371 READ_LEN(len_flags & ASF_PPI_MASK_PACKET_LENGTH_FIELD_SIZE,
1372 ASF_PPI_FLAG_PACKET_LENGTH_FIELD_, asf->packet_size_internal);
1373 READ_LEN(len_flags & ASF_PPI_MASK_SEQUENCE_FIELD_SIZE,
1374 ASF_PPI_FLAG_SEQUENCE_FIELD_, seq);
1375 READ_LEN(len_flags & ASF_PPI_MASK_PADDING_LENGTH_FIELD_SIZE,
1376 ASF_PPI_FLAG_PADDING_LENGTH_FIELD_, asf->pad_len );
1377 asf->send_time = avio_rl32(pb); // send time
1378 avio_skip(pb, 2); // skip duration
1379 if (len_flags & ASF_PPI_FLAG_MULTIPLE_PAYLOADS_PRESENT) { // Multiple Payloads present
1380 pay_flags = avio_r8(pb);
1381 asf->nb_mult_left = (pay_flags & ASF_NUM_OF_PAYLOADS);
1387 static int asf_deinterleave(AVFormatContext *s, ASFPacket *asf_pkt, int st_num)
1389 ASFContext *asf = s->priv_data;
1390 ASFStream *asf_st = asf->asf_st[st_num];
1391 unsigned char *p = asf_pkt->avpkt.data;
1392 uint16_t pkt_len = asf->asf_st[st_num]->virtual_pkt_len;
1393 uint16_t chunk_len = asf->asf_st[st_num]->virtual_chunk_len;
1394 int nchunks = pkt_len / chunk_len;
1396 int pos = 0, j, l, ret;
1399 if ((ret = av_new_packet(&pkt, asf_pkt->data_size)) < 0)
1402 while (asf_pkt->data_size >= asf_st->span * pkt_len + pos) {
1403 if (pos >= asf_pkt->data_size) {
1406 for (l = 0; l < pkt_len; l++) {
1407 if (pos >= asf_pkt->data_size) {
1410 for (j = 0; j < asf_st->span; j++) {
1411 if ((pos + chunk_len) >= asf_pkt->data_size)
1413 memcpy(pkt.data + pos,
1414 p + (j * nchunks + l) * chunk_len,
1419 p += asf_st->span * pkt_len;
1420 if (p > asf_pkt->avpkt.data + asf_pkt->data_size)
1423 av_packet_unref(&asf_pkt->avpkt);
1424 asf_pkt->avpkt = pkt;
1429 static int asf_read_packet(AVFormatContext *s, AVPacket *pkt)
1431 ASFContext *asf = s->priv_data;
1432 AVIOContext *pb = s->pb;
1435 if ((avio_tell(pb) >= asf->data_offset + asf->data_size) &&
1436 !(asf->b_flags & ASF_FLAG_BROADCAST))
1438 while (!pb->eof_reached) {
1439 if (asf->state == PARSE_PACKET_HEADER) {
1440 asf_read_packet_header(s);
1441 if (pb->eof_reached)
1443 if (!asf->nb_mult_left)
1444 asf->state = READ_SINGLE;
1446 asf->state = READ_MULTI;
1448 ret = asf_read_payload(s, pkt);
1449 if (ret == AVERROR(EAGAIN)) {
1450 asf->state = PARSE_PACKET_HEADER;
1456 switch (asf->state) {
1459 asf->state = PARSE_PACKET_HEADER;
1461 case READ_MULTI_SUB:
1462 if (!asf->sub_left && !asf->nb_mult_left) {
1463 asf->state = PARSE_PACKET_HEADER;
1464 if (!asf->return_subpayload &&
1465 (avio_tell(pb) <= asf->packet_offset +
1466 asf->packet_size - asf->pad_len))
1467 avio_skip(pb, asf->pad_len); // skip padding
1468 if (asf->packet_offset + asf->packet_size > avio_tell(pb))
1469 avio_seek(pb, asf->packet_offset + asf->packet_size, SEEK_SET);
1470 } else if (!asf->sub_left)
1471 asf->state = READ_MULTI;
1474 if (!asf->nb_mult_left) {
1475 asf->state = PARSE_PACKET_HEADER;
1476 if (!asf->return_subpayload &&
1477 (avio_tell(pb) <= asf->packet_offset +
1478 asf->packet_size - asf->pad_len))
1479 avio_skip(pb, asf->pad_len); // skip padding
1480 if (asf->packet_offset + asf->packet_size > avio_tell(pb))
1481 avio_seek(pb, asf->packet_offset + asf->packet_size, SEEK_SET);
1485 if (asf->return_subpayload) {
1486 asf->return_subpayload = 0;
1489 for (i = 0; i < s->nb_streams; i++) {
1490 ASFPacket *asf_pkt = &asf->asf_st[i]->pkt;
1491 if (asf_pkt && !asf_pkt->size_left && asf_pkt->data_size) {
1492 if (asf->asf_st[i]->span > 1 &&
1493 asf->asf_st[i]->type == AVMEDIA_TYPE_AUDIO)
1494 if ((ret = asf_deinterleave(s, asf_pkt, i)) < 0)
1496 av_packet_move_ref(pkt, &asf_pkt->avpkt);
1497 pkt->stream_index = asf->asf_st[i]->index;
1498 pkt->flags = asf_pkt->flags;
1499 pkt->dts = asf_pkt->dts - asf->preroll;
1500 asf_pkt->data_size = 0;
1501 asf_pkt->frame_num = 0;
1507 if (pb->eof_reached)
1513 static int asf_read_close(AVFormatContext *s)
1515 ASFContext *asf = s->priv_data;
1518 for (i = 0; i < ASF_MAX_STREAMS; i++) {
1519 av_dict_free(&asf->asf_sd[i].asf_met);
1520 if (i < asf->nb_streams) {
1521 av_packet_unref(&asf->asf_st[i]->pkt.avpkt);
1522 av_freep(&asf->asf_st[i]);
1526 asf->nb_streams = 0;
1530 static void reset_packet_state(AVFormatContext *s)
1532 ASFContext *asf = s->priv_data;
1535 asf->state = PARSE_PACKET_HEADER;
1537 asf->return_subpayload = 0;
1539 asf->sub_header_offset = 0;
1540 asf->packet_offset = asf->first_packet_offset;
1542 asf->rep_data_len = 0;
1544 asf->mult_sub_len = 0;
1545 asf->nb_mult_left = 0;
1547 asf->prop_flags = 0;
1549 for (i = 0; i < asf->nb_streams; i++) {
1550 ASFPacket *pkt = &asf->asf_st[i]->pkt;
1557 av_packet_unref(&pkt->avpkt);
1558 av_init_packet(&pkt->avpkt);
1563 * Find a timestamp for the requested position within the payload
1564 * where the pos (position) is the offset inside the Data Object.
1565 * When position is not on the packet boundary, asf_read_timestamp tries
1566 * to find the closest packet offset after this position. If this packet
1567 * is a key frame, this packet timestamp is read and an index entry is created
1568 * for the packet. If this packet belongs to the requested stream,
1569 * asf_read_timestamp upgrades pos to the packet beginning offset and
1570 * returns this packet's dts. So returned dts is the dts of the first key frame with
1571 * matching stream number after given position.
1573 static int64_t asf_read_timestamp(AVFormatContext *s, int stream_index,
1574 int64_t *pos, int64_t pos_limit)
1576 ASFContext *asf = s->priv_data;
1577 int64_t pkt_pos = *pos, pkt_offset, dts = AV_NOPTS_VALUE, data_end;
1581 data_end = asf->data_offset + asf->data_size;
1583 n = (pkt_pos - asf->first_packet_offset + asf->packet_size - 1) /
1585 n = av_clip(n, 0, ((data_end - asf->first_packet_offset) / asf->packet_size - 1));
1586 pkt_pos = asf->first_packet_offset + n * asf->packet_size;
1588 avio_seek(s->pb, pkt_pos, SEEK_SET);
1589 pkt_offset = pkt_pos;
1591 reset_packet_state(s);
1592 while (avio_tell(s->pb) < data_end) {
1594 int i, ret, st_found;
1596 av_init_packet(&pkt);
1597 pkt_offset = avio_tell(s->pb);
1598 if ((ret = asf_read_packet(s, &pkt)) < 0) {
1599 dts = AV_NOPTS_VALUE;
1602 // ASFPacket may contain fragments of packets belonging to different streams,
1603 // pkt_offset is the offset of the first fragment within it.
1604 if ((pkt_offset >= (pkt_pos + asf->packet_size)))
1605 pkt_pos += asf->packet_size;
1606 for (i = 0; i < asf->nb_streams; i++) {
1607 ASFStream *st = asf->asf_st[i];
1610 if (pkt.flags & AV_PKT_FLAG_KEY) {
1613 av_add_index_entry(s->streams[pkt.stream_index], pkt_pos,
1614 dts, pkt.size, 0, AVINDEX_KEYFRAME);
1615 if (stream_index == st->index) {
1624 av_packet_unref(&pkt);
1628 av_packet_unref(&pkt);
1632 static int asf_read_seek(AVFormatContext *s, int stream_index,
1633 int64_t timestamp, int flags)
1635 ASFContext *asf = s->priv_data;
1638 if (s->streams[stream_index]->nb_index_entries && asf->is_simple_index) {
1639 idx = av_index_search_timestamp(s->streams[stream_index], timestamp, flags);
1640 if (idx < 0 || idx >= s->streams[stream_index]->nb_index_entries)
1641 return AVERROR_INVALIDDATA;
1642 avio_seek(s->pb, s->streams[stream_index]->index_entries[idx].pos, SEEK_SET);
1644 if ((ret = ff_seek_frame_binary(s, stream_index, timestamp, flags)) < 0)
1648 reset_packet_state(s);
1653 static const GUIDParseTable *find_guid(ff_asf_guid guid)
1656 const GUIDParseTable *g;
1660 for (j = 0; j < FF_ARRAY_ELEMS(gdef); j++) {
1661 if (!(ret = memcmp(guid, g->guid, sizeof(g->guid))))
1669 static int detect_unknown_subobject(AVFormatContext *s, int64_t offset, int64_t size)
1671 ASFContext *asf = s->priv_data;
1672 AVIOContext *pb = s->pb;
1673 const GUIDParseTable *g = NULL;
1677 while (avio_tell(pb) <= offset + size) {
1678 if (avio_tell(pb) == asf->offset)
1680 asf->offset = avio_tell(pb);
1681 if ((ret = ff_get_guid(pb, &guid)) < 0)
1683 g = find_guid(guid);
1685 if ((ret = g->read_object(s, g)) < 0)
1690 g2.name = "Unknown";
1691 g2.is_subobject = 1;
1692 asf_read_unknown(s, &g2);
1699 static int asf_read_header(AVFormatContext *s)
1701 ASFContext *asf = s->priv_data;
1702 AVIOContext *pb = s->pb;
1703 const GUIDParseTable *g = NULL;
1709 asf->is_simple_index = 0;
1710 ff_get_guid(pb, &guid);
1711 if (ff_guidcmp(&guid, &ff_asf_header))
1712 return AVERROR_INVALIDDATA;
1713 avio_skip(pb, 8); // skip header object size
1714 avio_skip(pb, 6); // skip number of header objects and 2 reserved bytes
1715 asf->data_reached = 0;
1717 /* 1 is here instead of pb->eof_reached because (when not streaming), Data are skipped
1718 * for the first time,
1719 * Index object is processed and got eof and then seeking back to the Data is performed.
1722 // for the cases when object size is invalid
1723 if (avio_tell(pb) == asf->offset)
1725 asf->offset = avio_tell(pb);
1726 if ((ret = ff_get_guid(pb, &guid)) < 0) {
1727 if (ret == AVERROR_EOF && asf->data_reached)
1732 g = find_guid(guid);
1734 asf->unknown_offset = asf->offset;
1736 if ((ret = g->read_object(s, g)) < 0)
1739 size = avio_rl64(pb);
1740 align_position(pb, asf->offset, size);
1742 if (asf->data_reached && (!pb->seekable || (asf->b_flags & ASF_FLAG_BROADCAST)))
1746 if (!asf->data_reached) {
1747 av_log(s, AV_LOG_ERROR, "Data Object was not found.\n");
1748 ret = AVERROR_INVALIDDATA;
1752 avio_seek(pb, asf->first_packet_offset, SEEK_SET);
1754 for (i = 0; i < asf->nb_streams; i++) {
1755 const char *rfc1766 = asf->asf_sd[asf->asf_st[i]->lang_idx].langs;
1756 AVStream *st = s->streams[asf->asf_st[i]->index];
1757 set_language(s, rfc1766, &st->metadata);
1760 for (i = 0; i < ASF_MAX_STREAMS; i++) {
1761 AVStream *st = NULL;
1763 st = find_stream(s, i);
1765 av_dict_copy(&st->metadata, asf->asf_sd[i].asf_met, AV_DICT_IGNORE_SUFFIX);
1766 if (asf->asf_sd[i].aspect_ratio.num > 0 && asf->asf_sd[i].aspect_ratio.den > 0) {
1767 st->sample_aspect_ratio.num = asf->asf_sd[i].aspect_ratio.num;
1768 st->sample_aspect_ratio.den = asf->asf_sd[i].aspect_ratio.den;
1780 AVInputFormat ff_asf_o_demuxer = {
1782 .long_name = NULL_IF_CONFIG_SMALL("ASF (Advanced / Active Streaming Format)"),
1783 .priv_data_size = sizeof(ASFContext),
1784 .read_probe = asf_probe,
1785 .read_header = asf_read_header,
1786 .read_packet = asf_read_packet,
1787 .read_close = asf_read_close,
1788 .read_timestamp = asf_read_timestamp,
1789 .read_seek = asf_read_seek,
1790 .flags = AVFMT_NOBINSEARCH | AVFMT_NOGENSEARCH,