LCOV - code coverage report
Current view: top level - libavformat - asfdec_f.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 718 949 75.7 %
Date: 2017-12-11 04:34:20 Functions: 23 24 95.8 %

          Line data    Source code
       1             : /*
       2             :  * ASF compatible demuxer
       3             :  * Copyright (c) 2000, 2001 Fabrice Bellard
       4             :  *
       5             :  * This file is part of FFmpeg.
       6             :  *
       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.
      11             :  *
      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.
      16             :  *
      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
      20             :  */
      21             : 
      22             : #include <inttypes.h>
      23             : 
      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"
      33             : #include "avformat.h"
      34             : #include "avio_internal.h"
      35             : #include "avlanguage.h"
      36             : #include "id3v2.h"
      37             : #include "internal.h"
      38             : #include "riff.h"
      39             : #include "asf.h"
      40             : #include "asfcrypt.h"
      41             : 
      42             : typedef struct ASFPayload {
      43             :     uint8_t type;
      44             :     uint16_t size;
      45             : } ASFPayload;
      46             : 
      47             : typedef struct ASFStream {
      48             :     int num;
      49             :     unsigned char seq;
      50             :     /* use for reading */
      51             :     AVPacket pkt;
      52             :     int frag_offset;
      53             :     int packet_obj_size;
      54             :     int timestamp;
      55             :     int64_t duration;
      56             :     int skip_to_key;
      57             :     int pkt_clean;
      58             : 
      59             :     int ds_span;                /* descrambling  */
      60             :     int ds_packet_size;
      61             :     int ds_chunk_size;
      62             : 
      63             :     int64_t packet_pos;
      64             : 
      65             :     uint16_t stream_language_index;
      66             : 
      67             :     int      palette_changed;
      68             :     uint32_t palette[256];
      69             : 
      70             :     int payload_ext_ct;
      71             :     ASFPayload payload[8];
      72             : } ASFStream;
      73             : 
      74             : typedef struct ASFContext {
      75             :     const AVClass *class;
      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)
      79             :     AVRational dar[128];
      80             :     char stream_languages[128][6];       ///< max number of streams, language for each (RFC1766, e.g. en-US)
      81             :     /* non streamed additonnal info */
      82             :     /* packet filling */
      83             :     int packet_size_left;
      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
      88             :     int index_read;
      89             : 
      90             :     ASFMainHeader hdr;
      91             : 
      92             :     int packet_flags;
      93             :     int packet_property;
      94             :     int packet_timestamp;
      95             :     int packet_segsizetype;
      96             :     int packet_segments;
      97             :     int packet_seq;
      98             :     int packet_replic_size;
      99             :     int packet_key_frame;
     100             :     int packet_padsize;
     101             :     unsigned int packet_frag_offset;
     102             :     unsigned int packet_frag_size;
     103             :     int64_t packet_frag_timestamp;
     104             :     int ts_is_pts;
     105             :     int packet_multi_size;
     106             :     int packet_time_delta;
     107             :     int packet_time_start;
     108             :     int64_t packet_pos;
     109             : 
     110             :     int stream_index;
     111             : 
     112             :     ASFStream *asf_st;                   ///< currently decoded stream
     113             : 
     114             :     int no_resync_search;
     115             :     int export_xmp;
     116             : 
     117             :     int uses_std_ecc;
     118             : } ASFContext;
     119             : 
     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 },
     123             :     { NULL },
     124             : };
     125             : 
     126             : static const AVClass asf_class = {
     127             :     .class_name = "asf demuxer",
     128             :     .item_name  = av_default_item_name,
     129             :     .option     = options,
     130             :     .version    = LIBAVUTIL_VERSION_INT,
     131             : };
     132             : 
     133             : #undef NDEBUG
     134             : #include <assert.h>
     135             : 
     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)
     140             : 
     141             : #ifdef DEBUG
     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
     144             : };
     145             : 
     146             : #define PRINT_IF_GUID(g, cmp) \
     147             :     if (!ff_guidcmp(g, &cmp)) \
     148             :         av_log(NULL, AV_LOG_TRACE, "(GUID: %s) ", # cmp)
     149             : 
     150             : static void print_guid(ff_asf_guid *g)
     151             : {
     152             :     int i;
     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);
     178             :     else
     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");
     183             : }
     184             : #undef PRINT_IF_GUID
     185             : #else
     186             : #define print_guid(g) while(0)
     187             : #endif
     188             : 
     189        6130 : static int asf_probe(AVProbeData *pd)
     190             : {
     191             :     /* check file header */
     192        6130 :     if (!ff_guidcmp(pd->buf, &ff_asf_header))
     193          32 :         return AVPROBE_SCORE_MAX;
     194             :     else
     195        6098 :         return 0;
     196             : }
     197             : 
     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          98 : static int get_value(AVIOContext *pb, int type, int type2_size)
     201             : {
     202          98 :     switch (type) {
     203          50 :     case 2:
     204          50 :         return (type2_size == 32) ? avio_rl32(pb) : avio_rl16(pb);
     205          45 :     case 3:
     206          45 :         return avio_rl32(pb);
     207           3 :     case 4:
     208           3 :         return avio_rl64(pb);
     209           0 :     case 5:
     210           0 :         return avio_rl16(pb);
     211           0 :     default:
     212           0 :         return INT_MIN;
     213             :     }
     214             : }
     215             : 
     216             : /* MSDN claims that this should be "compatible with the ID3 frame, APIC",
     217             :  * but in reality this is only loosely similar */
     218           3 : static int asf_read_picture(AVFormatContext *s, int len)
     219             : {
     220           3 :     AVPacket pkt          = { 0 };
     221           3 :     const CodecMime *mime = ff_id3v2_mime_tags;
     222           3 :     enum  AVCodecID id    = AV_CODEC_ID_NONE;
     223             :     char mimetype[64];
     224           3 :     uint8_t  *desc = NULL;
     225           3 :     AVStream   *st = NULL;
     226             :     int ret, type, picsize, desc_len;
     227             : 
     228             :     /* type + picsize + mime + desc */
     229           3 :     if (len < 1 + 4 + 2 + 2) {
     230           0 :         av_log(s, AV_LOG_ERROR, "Invalid attached picture size: %d.\n", len);
     231           0 :         return AVERROR_INVALIDDATA;
     232             :     }
     233             : 
     234             :     /* picture type */
     235           3 :     type = avio_r8(s->pb);
     236           3 :     len--;
     237           3 :     if (type >= FF_ARRAY_ELEMS(ff_id3v2_picture_types) || type < 0) {
     238           0 :         av_log(s, AV_LOG_WARNING, "Unknown attached picture type: %d.\n", type);
     239           0 :         type = 0;
     240             :     }
     241             : 
     242             :     /* picture data size */
     243           3 :     picsize = avio_rl32(s->pb);
     244           3 :     len    -= 4;
     245             : 
     246             :     /* picture MIME type */
     247           3 :     len -= avio_get_str16le(s->pb, len, mimetype, sizeof(mimetype));
     248           9 :     while (mime->id != AV_CODEC_ID_NONE) {
     249           6 :         if (!strncmp(mime->str, mimetype, sizeof(mimetype))) {
     250           3 :             id = mime->id;
     251           3 :             break;
     252             :         }
     253           3 :         mime++;
     254             :     }
     255           3 :     if (id == AV_CODEC_ID_NONE) {
     256           0 :         av_log(s, AV_LOG_ERROR, "Unknown attached picture mimetype: %s.\n",
     257             :                mimetype);
     258           0 :         return 0;
     259             :     }
     260             : 
     261           3 :     if (picsize >= len) {
     262           0 :         av_log(s, AV_LOG_ERROR, "Invalid attached picture data size: %d >= %d.\n",
     263             :                picsize, len);
     264           0 :         return AVERROR_INVALIDDATA;
     265             :     }
     266             : 
     267             :     /* picture description */
     268           3 :     desc_len = (len - picsize) * 2 + 1;
     269           3 :     desc     = av_malloc(desc_len);
     270           3 :     if (!desc)
     271           0 :         return AVERROR(ENOMEM);
     272           3 :     len -= avio_get_str16le(s->pb, len - picsize, desc, desc_len);
     273             : 
     274           3 :     ret = av_get_packet(s->pb, &pkt, picsize);
     275           3 :     if (ret < 0)
     276           0 :         goto fail;
     277             : 
     278           3 :     st  = avformat_new_stream(s, NULL);
     279           3 :     if (!st) {
     280           0 :         ret = AVERROR(ENOMEM);
     281           0 :         goto fail;
     282             :     }
     283           3 :     st->disposition              |= AV_DISPOSITION_ATTACHED_PIC;
     284           3 :     st->codecpar->codec_type      = AVMEDIA_TYPE_VIDEO;
     285           3 :     st->codecpar->codec_id        = id;
     286           3 :     st->attached_pic              = pkt;
     287           3 :     st->attached_pic.stream_index = st->index;
     288           3 :     st->attached_pic.flags       |= AV_PKT_FLAG_KEY;
     289             : 
     290           3 :     if (*desc)
     291           2 :         av_dict_set(&st->metadata, "title", desc, AV_DICT_DONT_STRDUP_VAL);
     292             :     else
     293           1 :         av_freep(&desc);
     294             : 
     295           3 :     av_dict_set(&st->metadata, "comment", ff_id3v2_picture_types[type], 0);
     296             : 
     297           3 :     return 0;
     298             : 
     299           0 : fail:
     300           0 :     av_freep(&desc);
     301           0 :     av_packet_unref(&pkt);
     302           0 :     return ret;
     303             : }
     304             : 
     305           1 : static void get_id3_tag(AVFormatContext *s, int len)
     306             : {
     307           1 :     ID3v2ExtraMeta *id3v2_extra_meta = NULL;
     308             : 
     309           1 :     ff_id3v2_read(s, ID3v2_DEFAULT_MAGIC, &id3v2_extra_meta, len);
     310           1 :     if (id3v2_extra_meta) {
     311           1 :         ff_id3v2_parse_apic(s, &id3v2_extra_meta);
     312           1 :         ff_id3v2_parse_chapters(s, &id3v2_extra_meta);
     313             :     }
     314           1 :     ff_id3v2_free_extra_meta(&id3v2_extra_meta);
     315           1 : }
     316             : 
     317         317 : static void get_tag(AVFormatContext *s, const char *key, int type, int len, int type2_size)
     318             : {
     319         317 :     ASFContext *asf = s->priv_data;
     320         317 :     char *value = NULL;
     321         317 :     int64_t off = avio_tell(s->pb);
     322             : #define LEN 22
     323             : 
     324         317 :     if ((unsigned)len >= (UINT_MAX - LEN) / 2)
     325           0 :         return;
     326             : 
     327         317 :     if (!asf->export_xmp && !strncmp(key, "xmp", 3))
     328           0 :         goto finish;
     329             : 
     330         317 :     value = av_malloc(2 * len + LEN);
     331         317 :     if (!value)
     332           0 :         goto finish;
     333             : 
     334         317 :     switch (type) {
     335         192 :     case ASF_UNICODE:
     336         192 :         avio_get_str16le(s->pb, len, value, 2 * len + 1);
     337         192 :         break;
     338           3 :     case -1: // ASCI
     339           3 :         avio_read(s->pb, value, len);
     340           3 :         value[len]=0;
     341           3 :         break;
     342          16 :     case ASF_BYTE_ARRAY:
     343          16 :         if (!strcmp(key, "WM/Picture")) { // handle cover art
     344           3 :             asf_read_picture(s, len);
     345          13 :         } else if (!strcmp(key, "ID3")) { // handle ID3 tag
     346           1 :             get_id3_tag(s, len);
     347             :         } else {
     348          12 :             av_log(s, AV_LOG_VERBOSE, "Unsupported byte array in tag %s.\n", key);
     349             :         }
     350          16 :         goto finish;
     351          96 :     case ASF_BOOL:
     352             :     case ASF_DWORD:
     353             :     case ASF_QWORD:
     354             :     case ASF_WORD: {
     355          96 :         uint64_t num = get_value(s->pb, type, type2_size);
     356          96 :         snprintf(value, LEN, "%"PRIu64, num);
     357          96 :         break;
     358             :     }
     359          10 :     case ASF_GUID:
     360          10 :         av_log(s, AV_LOG_DEBUG, "Unsupported GUID value in tag %s.\n", key);
     361          10 :         goto finish;
     362           0 :     default:
     363           0 :         av_log(s, AV_LOG_DEBUG,
     364             :                "Unsupported value type %d in tag %s.\n", type, key);
     365           0 :         goto finish;
     366             :     }
     367         291 :     if (*value)
     368         250 :         av_dict_set(&s->metadata, key, value, 0);
     369             : 
     370         358 : finish:
     371         317 :     av_freep(&value);
     372         317 :     avio_seek(s->pb, off + len, SEEK_SET);
     373             : }
     374             : 
     375          32 : static int asf_read_file_properties(AVFormatContext *s, int64_t size)
     376             : {
     377          32 :     ASFContext *asf = s->priv_data;
     378          32 :     AVIOContext *pb = s->pb;
     379             : 
     380          32 :     ff_get_guid(pb, &asf->hdr.guid);
     381          32 :     asf->hdr.file_size   = avio_rl64(pb);
     382          32 :     asf->hdr.create_time = avio_rl64(pb);
     383          32 :     avio_rl64(pb);                               /* number of packets */
     384          32 :     asf->hdr.play_time   = avio_rl64(pb);
     385          32 :     asf->hdr.send_time   = avio_rl64(pb);
     386          32 :     asf->hdr.preroll     = avio_rl32(pb);
     387          32 :     asf->hdr.ignore      = avio_rl32(pb);
     388          32 :     asf->hdr.flags       = avio_rl32(pb);
     389          32 :     asf->hdr.min_pktsize = avio_rl32(pb);
     390          32 :     asf->hdr.max_pktsize = avio_rl32(pb);
     391          32 :     if (asf->hdr.min_pktsize >= (1U << 29))
     392           0 :         return AVERROR_INVALIDDATA;
     393          32 :     asf->hdr.max_bitrate = avio_rl32(pb);
     394          32 :     s->packet_size       = asf->hdr.max_pktsize;
     395             : 
     396          32 :     return 0;
     397             : }
     398             : 
     399          43 : static int asf_read_stream_properties(AVFormatContext *s, int64_t size)
     400             : {
     401          43 :     ASFContext *asf = s->priv_data;
     402          43 :     AVIOContext *pb = s->pb;
     403             :     AVStream *st;
     404             :     ASFStream *asf_st;
     405             :     ff_asf_guid g;
     406             :     enum AVMediaType type;
     407             :     int type_specific_size, sizeX;
     408             :     unsigned int tag1;
     409             :     int64_t pos1, pos2, start_time;
     410          43 :     int test_for_ext_stream_audio, is_dvr_ms_audio = 0;
     411             : 
     412          43 :     if (s->nb_streams == ASF_MAX_STREAMS) {
     413           0 :         av_log(s, AV_LOG_ERROR, "too many streams\n");
     414           0 :         return AVERROR(EINVAL);
     415             :     }
     416             : 
     417          43 :     pos1 = avio_tell(pb);
     418             : 
     419          43 :     st = avformat_new_stream(s, NULL);
     420          43 :     if (!st)
     421           0 :         return AVERROR(ENOMEM);
     422          43 :     avpriv_set_pts_info(st, 32, 1, 1000); /* 32 bit pts in ms */
     423          43 :     start_time     = asf->hdr.preroll;
     424             : 
     425          43 :     if (!(asf->hdr.flags & 0x01)) { // if we aren't streaming...
     426          43 :         int64_t fsize = avio_size(pb);
     427          86 :         if (fsize <= 0 || (int64_t)asf->hdr.file_size <= 0 ||
     428          43 :             20*FFABS(fsize - (int64_t)asf->hdr.file_size) < FFMIN(fsize, asf->hdr.file_size))
     429          52 :             st->duration = asf->hdr.play_time /
     430          26 :                        (10000000 / 1000) - start_time;
     431             :     }
     432          43 :     ff_get_guid(pb, &g);
     433             : 
     434          43 :     test_for_ext_stream_audio = 0;
     435          43 :     if (!ff_guidcmp(&g, &ff_asf_audio_stream)) {
     436          24 :         type = AVMEDIA_TYPE_AUDIO;
     437          19 :     } else if (!ff_guidcmp(&g, &ff_asf_video_stream)) {
     438          18 :         type = AVMEDIA_TYPE_VIDEO;
     439           1 :     } else if (!ff_guidcmp(&g, &ff_asf_jfif_media)) {
     440           0 :         type                = AVMEDIA_TYPE_VIDEO;
     441           0 :         st->codecpar->codec_id = AV_CODEC_ID_MJPEG;
     442           1 :     } else if (!ff_guidcmp(&g, &ff_asf_command_stream)) {
     443           1 :         type = AVMEDIA_TYPE_DATA;
     444           0 :     } else if (!ff_guidcmp(&g, &ff_asf_ext_stream_embed_stream_header)) {
     445           0 :         test_for_ext_stream_audio = 1;
     446           0 :         type                      = AVMEDIA_TYPE_UNKNOWN;
     447             :     } else {
     448           0 :         return -1;
     449             :     }
     450          43 :     ff_get_guid(pb, &g);
     451          43 :     avio_skip(pb, 8); /* total_size */
     452          43 :     type_specific_size = avio_rl32(pb);
     453          43 :     avio_rl32(pb);
     454          43 :     st->id = avio_rl16(pb) & 0x7f; /* stream id */
     455             :     // mapping of asf ID to AV stream ID;
     456          43 :     asf->asfid2avid[st->id] = s->nb_streams - 1;
     457          43 :     asf_st = &asf->streams[st->id];
     458             : 
     459          43 :     avio_rl32(pb);
     460             : 
     461          43 :     if (test_for_ext_stream_audio) {
     462           0 :         ff_get_guid(pb, &g);
     463           0 :         if (!ff_guidcmp(&g, &ff_asf_ext_stream_audio_stream)) {
     464           0 :             type            = AVMEDIA_TYPE_AUDIO;
     465           0 :             is_dvr_ms_audio = 1;
     466           0 :             ff_get_guid(pb, &g);
     467           0 :             avio_rl32(pb);
     468           0 :             avio_rl32(pb);
     469           0 :             avio_rl32(pb);
     470           0 :             ff_get_guid(pb, &g);
     471           0 :             avio_rl32(pb);
     472             :         }
     473             :     }
     474             : 
     475          43 :     st->codecpar->codec_type = type;
     476          43 :     if (type == AVMEDIA_TYPE_AUDIO) {
     477          24 :         int ret = ff_get_wav_header(s, pb, st->codecpar, type_specific_size, 0);
     478          24 :         if (ret < 0)
     479           0 :             return ret;
     480          24 :         if (is_dvr_ms_audio) {
     481             :             // codec_id and codec_tag are unreliable in dvr_ms
     482             :             // files. Set them later by probing stream.
     483           0 :             st->request_probe    = 1;
     484           0 :             st->codecpar->codec_tag = 0;
     485             :         }
     486          24 :         if (st->codecpar->codec_id == AV_CODEC_ID_AAC)
     487           0 :             st->need_parsing = AVSTREAM_PARSE_NONE;
     488             :         else
     489          24 :             st->need_parsing = AVSTREAM_PARSE_FULL;
     490             :         /* We have to init the frame size at some point .... */
     491          24 :         pos2 = avio_tell(pb);
     492          24 :         if (size >= (pos2 + 8 - pos1 + 24)) {
     493          24 :             asf_st->ds_span        = avio_r8(pb);
     494          24 :             asf_st->ds_packet_size = avio_rl16(pb);
     495          24 :             asf_st->ds_chunk_size  = avio_rl16(pb);
     496          24 :             avio_rl16(pb);  // ds_data_size
     497          24 :             avio_r8(pb);    // ds_silence_data
     498             :         }
     499          24 :         if (asf_st->ds_span > 1) {
     500           2 :             if (!asf_st->ds_chunk_size                                ||
     501           1 :                 (asf_st->ds_packet_size / asf_st->ds_chunk_size <= 1) ||
     502           0 :                 asf_st->ds_packet_size % asf_st->ds_chunk_size)
     503           1 :                 asf_st->ds_span = 0;  // disable descrambling
     504             :         }
     505          37 :     } else if (type == AVMEDIA_TYPE_VIDEO &&
     506          18 :                size - (avio_tell(pb) - pos1 + 24) >= 51) {
     507          18 :         avio_rl32(pb);
     508          18 :         avio_rl32(pb);
     509          18 :         avio_r8(pb);
     510          18 :         avio_rl16(pb);        /* size */
     511          18 :         sizeX             = avio_rl32(pb); /* size */
     512          18 :         st->codecpar->width  = avio_rl32(pb);
     513          18 :         st->codecpar->height = avio_rl32(pb);
     514             :         /* not available for asf */
     515          18 :         avio_rl16(pb); /* panes */
     516          18 :         st->codecpar->bits_per_coded_sample = avio_rl16(pb); /* depth */
     517          18 :         tag1                             = avio_rl32(pb);
     518          18 :         avio_skip(pb, 20);
     519          18 :         if (sizeX > 40) {
     520           9 :             st->codecpar->extradata_size = ffio_limit(pb, sizeX - 40);
     521           9 :             st->codecpar->extradata      = av_mallocz(st->codecpar->extradata_size +
     522             :                                                    AV_INPUT_BUFFER_PADDING_SIZE);
     523           9 :             if (!st->codecpar->extradata)
     524           0 :                 return AVERROR(ENOMEM);
     525           9 :             avio_read(pb, st->codecpar->extradata, st->codecpar->extradata_size);
     526             :         }
     527             : 
     528             :         /* Extract palette from extradata if bpp <= 8 */
     529             :         /* This code assumes that extradata contains only palette */
     530             :         /* This is true for all paletted codecs implemented in libavcodec */
     531          18 :         if (st->codecpar->extradata_size && (st->codecpar->bits_per_coded_sample <= 8)) {
     532             : #if HAVE_BIGENDIAN
     533             :             int i;
     534             :             for (i = 0; i < FFMIN(st->codecpar->extradata_size, AVPALETTE_SIZE) / 4; i++)
     535             :                 asf_st->palette[i] = av_bswap32(((uint32_t *)st->codecpar->extradata)[i]);
     536             : #else
     537           0 :             memcpy(asf_st->palette, st->codecpar->extradata,
     538           0 :                    FFMIN(st->codecpar->extradata_size, AVPALETTE_SIZE));
     539             : #endif
     540           0 :             asf_st->palette_changed = 1;
     541             :         }
     542             : 
     543          18 :         st->codecpar->codec_tag = tag1;
     544          18 :         st->codecpar->codec_id  = ff_codec_get_id(ff_codec_bmp_tags, tag1);
     545          18 :         if (tag1 == MKTAG('D', 'V', 'R', ' ')) {
     546           0 :             st->need_parsing = AVSTREAM_PARSE_FULL;
     547             :             /* issue658 contains wrong w/h and MS even puts a fake seq header
     548             :              * with wrong w/h in extradata while a correct one is in the stream.
     549             :              * maximum lameness */
     550           0 :             st->codecpar->width      =
     551           0 :                 st->codecpar->height = 0;
     552           0 :             av_freep(&st->codecpar->extradata);
     553           0 :             st->codecpar->extradata_size = 0;
     554             :         }
     555          18 :         if (st->codecpar->codec_id == AV_CODEC_ID_H264)
     556           0 :             st->need_parsing = AVSTREAM_PARSE_FULL_ONCE;
     557          18 :         if (st->codecpar->codec_id == AV_CODEC_ID_MPEG4)
     558           0 :             st->need_parsing = AVSTREAM_PARSE_FULL_ONCE;
     559             :     }
     560          43 :     pos2 = avio_tell(pb);
     561          43 :     avio_skip(pb, size - (pos2 - pos1 + 24));
     562             : 
     563          43 :     return 0;
     564             : }
     565             : 
     566          30 : static int asf_read_ext_stream_properties(AVFormatContext *s, int64_t size)
     567             : {
     568          30 :     ASFContext *asf = s->priv_data;
     569          30 :     AVIOContext *pb = s->pb;
     570             :     ff_asf_guid g;
     571             :     int ext_len, payload_ext_ct, stream_ct, i;
     572             :     uint32_t leak_rate, stream_num;
     573             :     unsigned int stream_languageid_index;
     574             : 
     575          30 :     avio_rl64(pb); // starttime
     576          30 :     avio_rl64(pb); // endtime
     577          30 :     leak_rate = avio_rl32(pb); // leak-datarate
     578          30 :     avio_rl32(pb); // bucket-datasize
     579          30 :     avio_rl32(pb); // init-bucket-fullness
     580          30 :     avio_rl32(pb); // alt-leak-datarate
     581          30 :     avio_rl32(pb); // alt-bucket-datasize
     582          30 :     avio_rl32(pb); // alt-init-bucket-fullness
     583          30 :     avio_rl32(pb); // max-object-size
     584          30 :     avio_rl32(pb); // flags (reliable,seekable,no_cleanpoints?,resend-live-cleanpoints, rest of bits reserved)
     585          30 :     stream_num = avio_rl16(pb); // stream-num
     586             : 
     587          30 :     stream_languageid_index = avio_rl16(pb); // stream-language-id-index
     588          30 :     if (stream_num < 128)
     589          30 :         asf->streams[stream_num].stream_language_index = stream_languageid_index;
     590             : 
     591          30 :     avio_rl64(pb); // avg frametime in 100ns units
     592          30 :     stream_ct      = avio_rl16(pb); // stream-name-count
     593          30 :     payload_ext_ct = avio_rl16(pb); // payload-extension-system-count
     594             : 
     595          30 :     if (stream_num < 128) {
     596          30 :         asf->stream_bitrates[stream_num] = leak_rate;
     597          30 :         asf->streams[stream_num].payload_ext_ct = 0;
     598             :     }
     599             : 
     600          30 :     for (i = 0; i < stream_ct; i++) {
     601           0 :         avio_rl16(pb);
     602           0 :         ext_len = avio_rl16(pb);
     603           0 :         avio_skip(pb, ext_len);
     604             :     }
     605             : 
     606          42 :     for (i = 0; i < payload_ext_ct; i++) {
     607             :         int size;
     608          12 :         ff_get_guid(pb, &g);
     609          12 :         size = avio_rl16(pb);
     610          12 :         ext_len = avio_rl32(pb);
     611          12 :         avio_skip(pb, ext_len);
     612          12 :         if (stream_num < 128 && i < FF_ARRAY_ELEMS(asf->streams[stream_num].payload)) {
     613          12 :             ASFPayload *p = &asf->streams[stream_num].payload[i];
     614          12 :             p->type = g[0];
     615          12 :             p->size = size;
     616          12 :             av_log(s, AV_LOG_DEBUG, "Payload extension %x %d\n", g[0], p->size );
     617          12 :             asf->streams[stream_num].payload_ext_ct ++;
     618             :         }
     619             :     }
     620             : 
     621          30 :     return 0;
     622             : }
     623             : 
     624          17 : static int asf_read_content_desc(AVFormatContext *s, int64_t size)
     625             : {
     626          17 :     AVIOContext *pb = s->pb;
     627             :     int len1, len2, len3, len4, len5;
     628             : 
     629          17 :     len1 = avio_rl16(pb);
     630          17 :     len2 = avio_rl16(pb);
     631          17 :     len3 = avio_rl16(pb);
     632          17 :     len4 = avio_rl16(pb);
     633          17 :     len5 = avio_rl16(pb);
     634          17 :     get_tag(s, "title", 0, len1, 32);
     635          17 :     get_tag(s, "author", 0, len2, 32);
     636          17 :     get_tag(s, "copyright", 0, len3, 32);
     637          17 :     get_tag(s, "comment", 0, len4, 32);
     638          17 :     avio_skip(pb, len5);
     639             : 
     640          17 :     return 0;
     641             : }
     642             : 
     643          32 : static int asf_read_ext_content_desc(AVFormatContext *s, int64_t size)
     644             : {
     645          32 :     AVIOContext *pb = s->pb;
     646          32 :     ASFContext *asf = s->priv_data;
     647             :     int desc_count, i, ret;
     648             : 
     649          32 :     desc_count = avio_rl16(pb);
     650         178 :     for (i = 0; i < desc_count; i++) {
     651             :         int name_len, value_type, value_len;
     652             :         char name[1024];
     653             : 
     654         146 :         name_len = avio_rl16(pb);
     655         146 :         if (name_len % 2)   // must be even, broken lavf versions wrote len-1
     656           0 :             name_len += 1;
     657         146 :         if ((ret = avio_get_str16le(pb, name_len, name, sizeof(name))) < name_len)
     658           0 :             avio_skip(pb, name_len - ret);
     659         146 :         value_type = avio_rl16(pb);
     660         146 :         value_len  = avio_rl16(pb);
     661         146 :         if (!value_type && value_len % 2)
     662           0 :             value_len += 1;
     663             :         /* My sample has that stream set to 0 maybe that mean the container.
     664             :          * ASF stream count starts at 1. I am using 0 to the container value
     665             :          * since it's unused. */
     666         146 :         if (!strcmp(name, "AspectRatioX"))
     667           0 :             asf->dar[0].num = get_value(s->pb, value_type, 32);
     668         146 :         else if (!strcmp(name, "AspectRatioY"))
     669           0 :             asf->dar[0].den = get_value(s->pb, value_type, 32);
     670             :         else
     671         146 :             get_tag(s, name, value_type, value_len, 32);
     672             :     }
     673             : 
     674          32 :     return 0;
     675             : }
     676             : 
     677          23 : static int asf_read_language_list(AVFormatContext *s, int64_t size)
     678             : {
     679          23 :     AVIOContext *pb = s->pb;
     680          23 :     ASFContext *asf = s->priv_data;
     681             :     int j, ret;
     682          23 :     int stream_count = avio_rl16(pb);
     683          48 :     for (j = 0; j < stream_count; j++) {
     684             :         char lang[6];
     685          25 :         unsigned int lang_len = avio_r8(pb);
     686          25 :         if ((ret = avio_get_str16le(pb, lang_len, lang,
     687             :                                     sizeof(lang))) < lang_len)
     688           4 :             avio_skip(pb, lang_len - ret);
     689          25 :         if (j < 128)
     690          25 :             av_strlcpy(asf->stream_languages[j], lang,
     691             :                        sizeof(*asf->stream_languages));
     692             :     }
     693             : 
     694          23 :     return 0;
     695             : }
     696             : 
     697          25 : static int asf_read_metadata(AVFormatContext *s, int64_t size)
     698             : {
     699          25 :     AVIOContext *pb = s->pb;
     700          25 :     ASFContext *asf = s->priv_data;
     701             :     int n, stream_num, name_len_utf16, name_len_utf8, value_len;
     702             :     int ret, i;
     703          25 :     n = avio_rl16(pb);
     704             : 
     705         127 :     for (i = 0; i < n; i++) {
     706             :         uint8_t *name;
     707             :         int value_type;
     708             : 
     709         102 :         avio_rl16(pb);  // lang_list_index
     710         102 :         stream_num = avio_rl16(pb);
     711         102 :         name_len_utf16 = avio_rl16(pb);
     712         102 :         value_type = avio_rl16(pb); /* value_type */
     713         102 :         value_len  = avio_rl32(pb);
     714             : 
     715         102 :         name_len_utf8 = 2*name_len_utf16 + 1;
     716         102 :         name          = av_malloc(name_len_utf8);
     717         102 :         if (!name)
     718           0 :             return AVERROR(ENOMEM);
     719             : 
     720         102 :         if ((ret = avio_get_str16le(pb, name_len_utf16, name, name_len_utf8)) < name_len_utf16)
     721           0 :             avio_skip(pb, name_len_utf16 - ret);
     722         102 :         av_log(s, AV_LOG_TRACE, "%d stream %d name_len %2d type %d len %4d <%s>\n",
     723             :                 i, stream_num, name_len_utf16, value_type, value_len, name);
     724             : 
     725         102 :         if (!strcmp(name, "AspectRatioX")){
     726           1 :             int aspect_x = get_value(s->pb, value_type, 16);
     727           1 :             if(stream_num < 128)
     728           1 :                 asf->dar[stream_num].num = aspect_x;
     729         101 :         } else if(!strcmp(name, "AspectRatioY")){
     730           1 :             int aspect_y = get_value(s->pb, value_type, 16);
     731           1 :             if(stream_num < 128)
     732           1 :                 asf->dar[stream_num].den = aspect_y;
     733             :         } else {
     734         100 :             get_tag(s, name, value_type, value_len, 16);
     735             :         }
     736         102 :         av_freep(&name);
     737             :     }
     738             : 
     739          25 :     return 0;
     740             : }
     741             : 
     742           0 : static int asf_read_marker(AVFormatContext *s, int64_t size)
     743             : {
     744           0 :     AVIOContext *pb = s->pb;
     745           0 :     ASFContext *asf = s->priv_data;
     746             :     int i, count, name_len, ret;
     747             :     char name[1024];
     748             : 
     749           0 :     avio_rl64(pb);            // reserved 16 bytes
     750           0 :     avio_rl64(pb);            // ...
     751           0 :     count = avio_rl32(pb);    // markers count
     752           0 :     avio_rl16(pb);            // reserved 2 bytes
     753           0 :     name_len = avio_rl16(pb); // name length
     754           0 :     avio_skip(pb, name_len);
     755             : 
     756           0 :     for (i = 0; i < count; i++) {
     757             :         int64_t pres_time;
     758             :         int name_len;
     759             : 
     760           0 :         if (avio_feof(pb))
     761           0 :             return AVERROR_INVALIDDATA;
     762             : 
     763           0 :         avio_rl64(pb);             // offset, 8 bytes
     764           0 :         pres_time = avio_rl64(pb); // presentation time
     765           0 :         pres_time -= asf->hdr.preroll * 10000;
     766           0 :         avio_rl16(pb);             // entry length
     767           0 :         avio_rl32(pb);             // send time
     768           0 :         avio_rl32(pb);             // flags
     769           0 :         name_len = avio_rl32(pb);  // name length
     770           0 :         if ((ret = avio_get_str16le(pb, name_len * 2, name,
     771             :                                     sizeof(name))) < name_len)
     772           0 :             avio_skip(pb, name_len - ret);
     773           0 :         avpriv_new_chapter(s, i, (AVRational) { 1, 10000000 }, pres_time,
     774             :                            AV_NOPTS_VALUE, name);
     775             :     }
     776             : 
     777           0 :     return 0;
     778             : }
     779             : 
     780          32 : static int asf_read_header(AVFormatContext *s)
     781             : {
     782          32 :     ASFContext *asf = s->priv_data;
     783             :     ff_asf_guid g;
     784          32 :     AVIOContext *pb = s->pb;
     785             :     int i;
     786             :     int64_t gsize;
     787             : 
     788          32 :     ff_get_guid(pb, &g);
     789          32 :     if (ff_guidcmp(&g, &ff_asf_header))
     790           0 :         return AVERROR_INVALIDDATA;
     791          32 :     avio_rl64(pb);
     792          32 :     avio_rl32(pb);
     793          32 :     avio_r8(pb);
     794          32 :     avio_r8(pb);
     795          32 :     memset(&asf->asfid2avid, -1, sizeof(asf->asfid2avid));
     796             : 
     797        4128 :     for (i = 0; i<128; i++)
     798        4096 :         asf->streams[i].stream_language_index = 128; // invalid stream index means no language info
     799             : 
     800         367 :     for (;;) {
     801         399 :         uint64_t gpos = avio_tell(pb);
     802         399 :         int ret = 0;
     803         399 :         ff_get_guid(pb, &g);
     804         399 :         gsize = avio_rl64(pb);
     805         399 :         print_guid(&g);
     806         399 :         if (!ff_guidcmp(&g, &ff_asf_data_header)) {
     807          32 :             asf->data_object_offset = avio_tell(pb);
     808             :             /* If not streaming, gsize is not unlimited (how?),
     809             :              * and there is enough space in the file.. */
     810          32 :             if (!(asf->hdr.flags & 0x01) && gsize >= 100)
     811          32 :                 asf->data_object_size = gsize - 24;
     812             :             else
     813           0 :                 asf->data_object_size = (uint64_t)-1;
     814          32 :             break;
     815             :         }
     816         367 :         if (gsize < 24)
     817           0 :             return AVERROR_INVALIDDATA;
     818         367 :         if (!ff_guidcmp(&g, &ff_asf_file_header)) {
     819          32 :             ret = asf_read_file_properties(s, gsize);
     820         335 :         } else if (!ff_guidcmp(&g, &ff_asf_stream_header)) {
     821          43 :             ret = asf_read_stream_properties(s, gsize);
     822         292 :         } else if (!ff_guidcmp(&g, &ff_asf_comment_header)) {
     823          17 :             asf_read_content_desc(s, gsize);
     824         275 :         } else if (!ff_guidcmp(&g, &ff_asf_language_guid)) {
     825          23 :             asf_read_language_list(s, gsize);
     826         252 :         } else if (!ff_guidcmp(&g, &ff_asf_extended_content_header)) {
     827          32 :             asf_read_ext_content_desc(s, gsize);
     828         220 :         } else if (!ff_guidcmp(&g, &ff_asf_metadata_header)) {
     829          22 :             asf_read_metadata(s, gsize);
     830         198 :         } else if (!ff_guidcmp(&g, &ff_asf_metadata_library_header)) {
     831           3 :             asf_read_metadata(s, gsize);
     832         195 :         } else if (!ff_guidcmp(&g, &ff_asf_ext_stream_header)) {
     833          30 :             asf_read_ext_stream_properties(s, gsize);
     834             : 
     835             :             // there could be an optional stream properties object to follow
     836             :             // if so the next iteration will pick it up
     837          30 :             continue;
     838         165 :         } else if (!ff_guidcmp(&g, &ff_asf_head1_guid)) {
     839          32 :             ff_get_guid(pb, &g);
     840          32 :             avio_skip(pb, 6);
     841          32 :             continue;
     842         133 :         } else if (!ff_guidcmp(&g, &ff_asf_marker_header)) {
     843           0 :             asf_read_marker(s, gsize);
     844         133 :         } else if (avio_feof(pb)) {
     845           0 :             return AVERROR_EOF;
     846             :         } else {
     847         133 :             if (!s->keylen) {
     848         117 :                 if (!ff_guidcmp(&g, &ff_asf_content_encryption)) {
     849             :                     unsigned int len;
     850             :                     AVPacket pkt;
     851           1 :                     av_log(s, AV_LOG_WARNING,
     852             :                            "DRM protected stream detected, decoding will likely fail!\n");
     853           1 :                     len= avio_rl32(pb);
     854           1 :                     av_log(s, AV_LOG_DEBUG, "Secret data:\n");
     855             : 
     856           1 :                     if ((ret = av_get_packet(pb, &pkt, len)) < 0)
     857           0 :                         return ret;
     858           1 :                     av_hex_dump_log(s, AV_LOG_DEBUG, pkt.data, pkt.size);
     859           1 :                     av_packet_unref(&pkt);
     860           1 :                     len= avio_rl32(pb);
     861           1 :                     get_tag(s, "ASF_Protection_Type", -1, len, 32);
     862           1 :                     len= avio_rl32(pb);
     863           1 :                     get_tag(s, "ASF_Key_ID", -1, len, 32);
     864           1 :                     len= avio_rl32(pb);
     865           1 :                     get_tag(s, "ASF_License_URL", -1, len, 32);
     866         116 :                 } else if (!ff_guidcmp(&g, &ff_asf_ext_content_encryption)) {
     867           0 :                     av_log(s, AV_LOG_WARNING,
     868             :                            "Ext DRM protected stream detected, decoding will likely fail!\n");
     869           0 :                     av_dict_set(&s->metadata, "encryption", "ASF Extended Content Encryption", 0);
     870         116 :                 } else if (!ff_guidcmp(&g, &ff_asf_digital_signature)) {
     871           0 :                     av_log(s, AV_LOG_INFO, "Digital signature detected!\n");
     872             :                 }
     873             :             }
     874             :         }
     875         305 :         if (ret < 0)
     876           0 :             return ret;
     877             : 
     878         305 :         if (avio_tell(pb) != gpos + gsize)
     879         132 :             av_log(s, AV_LOG_DEBUG,
     880             :                    "gpos mismatch our pos=%"PRIu64", end=%"PRId64"\n",
     881         132 :                    avio_tell(pb) - gpos, gsize);
     882         305 :         avio_seek(pb, gpos + gsize, SEEK_SET);
     883             :     }
     884          32 :     ff_get_guid(pb, &g);
     885          32 :     avio_rl64(pb);
     886          32 :     avio_r8(pb);
     887          32 :     avio_r8(pb);
     888          32 :     if (avio_feof(pb))
     889           0 :         return AVERROR_EOF;
     890          32 :     asf->data_offset      = avio_tell(pb);
     891          32 :     asf->packet_size_left = 0;
     892             : 
     893        4128 :     for (i = 0; i < 128; i++) {
     894        4096 :         int stream_num = asf->asfid2avid[i];
     895        4096 :         if (stream_num >= 0) {
     896          43 :             AVStream *st = s->streams[stream_num];
     897          43 :             if (!st->codecpar->bit_rate)
     898          19 :                 st->codecpar->bit_rate = asf->stream_bitrates[i];
     899          43 :             if (asf->dar[i].num > 0 && asf->dar[i].den > 0) {
     900           2 :                 av_reduce(&st->sample_aspect_ratio.num,
     901             :                           &st->sample_aspect_ratio.den,
     902           2 :                           asf->dar[i].num, asf->dar[i].den, INT_MAX);
     903          42 :             } else if ((asf->dar[0].num > 0) && (asf->dar[0].den > 0) &&
     904             :                        // Use ASF container value if the stream doesn't set AR.
     905           0 :                        (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO))
     906           0 :                 av_reduce(&st->sample_aspect_ratio.num,
     907             :                           &st->sample_aspect_ratio.den,
     908           0 :                           asf->dar[0].num, asf->dar[0].den, INT_MAX);
     909             : 
     910          86 :             av_log(s, AV_LOG_TRACE, "i=%d, st->codecpar->codec_type:%d, asf->dar %d:%d sar=%d:%d\n",
     911          43 :                     i, st->codecpar->codec_type, asf->dar[i].num, asf->dar[i].den,
     912             :                     st->sample_aspect_ratio.num, st->sample_aspect_ratio.den);
     913             : 
     914             :             // copy and convert language codes to the frontend
     915          43 :             if (asf->streams[i].stream_language_index < 128) {
     916          30 :                 const char *rfc1766 = asf->stream_languages[asf->streams[i].stream_language_index];
     917          30 :                 if (rfc1766 && strlen(rfc1766) > 1) {
     918          26 :                     const char primary_tag[3] = { rfc1766[0], rfc1766[1], '\0' }; // ignore country code if any
     919          26 :                     const char *iso6392       = ff_convert_lang_to(primary_tag,
     920             :                                                                    AV_LANG_ISO639_2_BIBL);
     921          26 :                     if (iso6392)
     922          26 :                         av_dict_set(&st->metadata, "language", iso6392, 0);
     923             :                 }
     924             :             }
     925             :         }
     926             :     }
     927             : 
     928          32 :     ff_metadata_conv(&s->metadata, NULL, ff_asf_metadata_conv);
     929             : 
     930          32 :     return 0;
     931             : }
     932             : 
     933             : #define DO_2BITS(bits, var, defval)             \
     934             :     switch (bits & 3) {                         \
     935             :     case 3:                                     \
     936             :         var = avio_rl32(pb);                    \
     937             :         rsize += 4;                             \
     938             :         break;                                  \
     939             :     case 2:                                     \
     940             :         var = avio_rl16(pb);                    \
     941             :         rsize += 2;                             \
     942             :         break;                                  \
     943             :     case 1:                                     \
     944             :         var = avio_r8(pb);                      \
     945             :         rsize++;                                \
     946             :         break;                                  \
     947             :     default:                                    \
     948             :         var = defval;                           \
     949             :         break;                                  \
     950             :     }
     951             : 
     952             : /**
     953             :  * Load a single ASF packet into the demuxer.
     954             :  * @param s demux context
     955             :  * @param pb context to read data from
     956             :  * @return 0 on success, <0 on error
     957             :  */
     958        1944 : static int asf_get_packet(AVFormatContext *s, AVIOContext *pb)
     959             : {
     960        1944 :     ASFContext *asf = s->priv_data;
     961             :     uint32_t packet_length, padsize;
     962        1944 :     int rsize = 8;
     963             :     int c, d, e, off;
     964             : 
     965        1944 :     if (asf->uses_std_ecc > 0) {
     966             :         // if we do not know packet size, allow skipping up to 32 kB
     967        1912 :         off = 32768;
     968        1912 :         if (asf->no_resync_search)
     969           0 :             off = 3;
     970             : //         else if (s->packet_size > 0 && !asf->uses_std_ecc)
     971             : //             off = (avio_tell(pb) - s->internal->data_offset) % s->packet_size + 3;
     972             : 
     973        1912 :         c = d = e = -1;
     974        7648 :         while (off-- > 0) {
     975        5736 :             c = d;
     976        5736 :             d = e;
     977        5736 :             e = avio_r8(pb);
     978        5736 :             if (c == 0x82 && !d && !e)
     979        1912 :                 break;
     980             :         }
     981             : 
     982        1912 :         if (c != 0x82) {
     983             :             /* This code allows handling of -EAGAIN at packet boundaries (i.e.
     984             :             * if the packet sync code above triggers -EAGAIN). This does not
     985             :             * imply complete -EAGAIN handling support at random positions in
     986             :             * the stream. */
     987           0 :             if (pb->error == AVERROR(EAGAIN))
     988           0 :                 return AVERROR(EAGAIN);
     989           0 :             if (!avio_feof(pb))
     990           0 :                 av_log(s, AV_LOG_ERROR,
     991             :                     "ff asf bad header %x  at:%"PRId64"\n", c, avio_tell(pb));
     992             :         }
     993        1912 :         if ((c & 0x8f) == 0x82) {
     994        1912 :             if (d || e) {
     995           0 :                 if (!avio_feof(pb))
     996           0 :                     av_log(s, AV_LOG_ERROR, "ff asf bad non zero\n");
     997           0 :                 return AVERROR_INVALIDDATA;
     998             :             }
     999        1912 :             c      = avio_r8(pb);
    1000        1912 :             d      = avio_r8(pb);
    1001        1912 :             rsize += 3;
    1002           0 :         } else if(!avio_feof(pb)) {
    1003           0 :             avio_seek(pb, -1, SEEK_CUR); // FIXME
    1004             :         }
    1005             :     } else {
    1006          32 :         c = avio_r8(pb);
    1007          32 :         if (c & 0x80) {
    1008          32 :             rsize ++;
    1009          32 :             if (!(c & 0x60)) {
    1010          32 :                 d = avio_r8(pb);
    1011          32 :                 e = avio_r8(pb);
    1012          32 :                 avio_seek(pb, (c & 0xF) - 2, SEEK_CUR);
    1013          32 :                 rsize += c & 0xF;
    1014             :             }
    1015             : 
    1016          32 :             if (c != 0x82)
    1017           0 :                 avpriv_request_sample(s, "Invalid ECC byte");
    1018             : 
    1019          32 :             if (!asf->uses_std_ecc)
    1020          32 :                 asf->uses_std_ecc =  (c == 0x82 && !d && !e) ? 1 : -1;
    1021             : 
    1022          32 :             c = avio_r8(pb);
    1023             :         } else
    1024           0 :             asf->uses_std_ecc =  -1;
    1025          32 :         d = avio_r8(pb);
    1026             :     }
    1027             : 
    1028        1944 :     asf->packet_flags    = c;
    1029        1944 :     asf->packet_property = d;
    1030             : 
    1031        1944 :     DO_2BITS(asf->packet_flags >> 5, packet_length, s->packet_size);
    1032        1944 :     DO_2BITS(asf->packet_flags >> 1, padsize, 0); // sequence ignored
    1033        1944 :     DO_2BITS(asf->packet_flags >> 3, padsize, 0); // padding length
    1034             : 
    1035             :     // the following checks prevent overflows and infinite loops
    1036        1944 :     if (!packet_length || packet_length >= (1U << 29)) {
    1037           0 :         av_log(s, AV_LOG_ERROR,
    1038             :                "invalid packet_length %"PRIu32" at:%"PRId64"\n",
    1039             :                packet_length, avio_tell(pb));
    1040           0 :         return AVERROR_INVALIDDATA;
    1041             :     }
    1042        1944 :     if (padsize >= packet_length) {
    1043           0 :         av_log(s, AV_LOG_ERROR,
    1044             :                "invalid padsize %"PRIu32" at:%"PRId64"\n", padsize, avio_tell(pb));
    1045           0 :         return AVERROR_INVALIDDATA;
    1046             :     }
    1047             : 
    1048        1944 :     asf->packet_timestamp = avio_rl32(pb);
    1049        1944 :     avio_rl16(pb); /* duration */
    1050             :     // rsize has at least 11 bytes which have to be present
    1051             : 
    1052        1944 :     if (asf->packet_flags & 0x01) {
    1053         933 :         asf->packet_segsizetype = avio_r8(pb);
    1054         933 :         rsize++;
    1055         933 :         asf->packet_segments = asf->packet_segsizetype & 0x3f;
    1056             :     } else {
    1057        1011 :         asf->packet_segments    = 1;
    1058        1011 :         asf->packet_segsizetype = 0x80;
    1059             :     }
    1060        1944 :     if (rsize > packet_length - padsize) {
    1061           0 :         asf->packet_size_left = 0;
    1062           0 :         av_log(s, AV_LOG_ERROR,
    1063             :                "invalid packet header length %d for pktlen %"PRIu32"-%"PRIu32" at %"PRId64"\n",
    1064             :                rsize, packet_length, padsize, avio_tell(pb));
    1065           0 :         return AVERROR_INVALIDDATA;
    1066             :     }
    1067        1944 :     asf->packet_size_left = packet_length - padsize - rsize;
    1068        1944 :     if (packet_length < asf->hdr.min_pktsize)
    1069           0 :         padsize += asf->hdr.min_pktsize - packet_length;
    1070        1944 :     asf->packet_padsize = padsize;
    1071        1944 :     av_log(s, AV_LOG_TRACE, "packet: size=%d padsize=%d  left=%d\n",
    1072             :             s->packet_size, asf->packet_padsize, asf->packet_size_left);
    1073        1944 :     return 0;
    1074             : }
    1075             : 
    1076             : /**
    1077             :  *
    1078             :  * @return <0 if error
    1079             :  */
    1080        3916 : static int asf_read_frame_header(AVFormatContext *s, AVIOContext *pb)
    1081             : {
    1082        3916 :     ASFContext *asf = s->priv_data;
    1083             :     ASFStream *asfst;
    1084        3916 :     int rsize       = 1;
    1085        3916 :     int num         = avio_r8(pb);
    1086             :     int i;
    1087             :     int64_t ts0, ts1 av_unused;
    1088             : 
    1089        3916 :     asf->packet_segments--;
    1090        3916 :     asf->packet_key_frame = num >> 7;
    1091        3916 :     asf->stream_index     = asf->asfid2avid[num & 0x7f];
    1092        3916 :     asfst                 = &asf->streams[num & 0x7f];
    1093             :     // sequence should be ignored!
    1094        3916 :     DO_2BITS(asf->packet_property >> 4, asf->packet_seq, 0);
    1095        3916 :     DO_2BITS(asf->packet_property >> 2, asf->packet_frag_offset, 0);
    1096        3916 :     DO_2BITS(asf->packet_property, asf->packet_replic_size, 0);
    1097        3916 :     av_log(asf, AV_LOG_TRACE, "key:%d stream:%d seq:%d offset:%d replic_size:%d num:%X packet_property %X\n",
    1098             :             asf->packet_key_frame, asf->stream_index, asf->packet_seq,
    1099             :             asf->packet_frag_offset, asf->packet_replic_size, num, asf->packet_property);
    1100        3916 :     if (rsize+(int64_t)asf->packet_replic_size > asf->packet_size_left) {
    1101           0 :         av_log(s, AV_LOG_ERROR, "packet_replic_size %d is invalid\n", asf->packet_replic_size);
    1102           0 :         return AVERROR_INVALIDDATA;
    1103             :     }
    1104        3916 :     if (asf->packet_replic_size >= 8) {
    1105        3786 :         int64_t end = avio_tell(pb) + asf->packet_replic_size;
    1106             :         AVRational aspect;
    1107        3786 :         asfst->packet_obj_size = avio_rl32(pb);
    1108        3786 :         if (asfst->packet_obj_size >= (1 << 24) || asfst->packet_obj_size < 0) {
    1109           0 :             av_log(s, AV_LOG_ERROR, "packet_obj_size %d invalid\n", asfst->packet_obj_size);
    1110           0 :             asfst->packet_obj_size = 0;
    1111           0 :             return AVERROR_INVALIDDATA;
    1112             :         }
    1113        3786 :         asf->packet_frag_timestamp = avio_rl32(pb); // timestamp
    1114             : 
    1115        5062 :         for (i = 0; i < asfst->payload_ext_ct; i++) {
    1116        1276 :             ASFPayload *p = &asfst->payload[i];
    1117        1276 :             int size = p->size;
    1118             :             int64_t payend;
    1119        1276 :             if (size == 0xFFFF)
    1120           0 :                 size = avio_rl16(pb);
    1121        1276 :             payend = avio_tell(pb) + size;
    1122        1276 :             if (payend > end) {
    1123           0 :                 av_log(s, AV_LOG_ERROR, "too long payload\n");
    1124           0 :                 break;
    1125             :             }
    1126        1276 :             switch (p->type) {
    1127        1108 :             case 0x50:
    1128             : //              duration = avio_rl16(pb);
    1129        1108 :                 break;
    1130         168 :             case 0x54:
    1131         168 :                 aspect.num = avio_r8(pb);
    1132         168 :                 aspect.den = avio_r8(pb);
    1133         168 :                 if (aspect.num > 0 && aspect.den > 0 && asf->stream_index >= 0) {
    1134         168 :                     s->streams[asf->stream_index]->sample_aspect_ratio = aspect;
    1135             :                 }
    1136         168 :                 break;
    1137           0 :             case 0x2A:
    1138           0 :                 avio_skip(pb, 8);
    1139           0 :                 ts0 = avio_rl64(pb);
    1140           0 :                 ts1 = avio_rl64(pb);
    1141           0 :                 if (ts0!= -1) asf->packet_frag_timestamp = ts0/10000;
    1142           0 :                 else          asf->packet_frag_timestamp = AV_NOPTS_VALUE;
    1143           0 :                 asf->ts_is_pts = 1;
    1144           0 :                 break;
    1145           0 :             case 0x5B:
    1146             :             case 0xB7:
    1147             :             case 0xCC:
    1148             :             case 0xC0:
    1149             :             case 0xA0:
    1150             :                 //unknown
    1151           0 :                 break;
    1152             :             }
    1153        1276 :             avio_seek(pb, payend, SEEK_SET);
    1154             :         }
    1155             : 
    1156        3786 :         avio_seek(pb, end, SEEK_SET);
    1157        3786 :         rsize += asf->packet_replic_size; // FIXME - check validity
    1158         130 :     } else if (asf->packet_replic_size == 1) {
    1159             :         // multipacket - frag_offset is beginning timestamp
    1160         130 :         asf->packet_time_start     = asf->packet_frag_offset;
    1161         130 :         asf->packet_frag_offset    = 0;
    1162         130 :         asf->packet_frag_timestamp = asf->packet_timestamp;
    1163             : 
    1164         130 :         asf->packet_time_delta = avio_r8(pb);
    1165         130 :         rsize++;
    1166           0 :     } else if (asf->packet_replic_size != 0) {
    1167           0 :         av_log(s, AV_LOG_ERROR, "unexpected packet_replic_size of %d\n",
    1168             :                asf->packet_replic_size);
    1169           0 :         return AVERROR_INVALIDDATA;
    1170             :     }
    1171        3916 :     if (asf->packet_flags & 0x01) {
    1172        2906 :         DO_2BITS(asf->packet_segsizetype >> 6, asf->packet_frag_size, 0); // 0 is illegal
    1173        2906 :         if (rsize > asf->packet_size_left) {
    1174           0 :             av_log(s, AV_LOG_ERROR, "packet_replic_size is invalid\n");
    1175           0 :             return AVERROR_INVALIDDATA;
    1176        2906 :         } else if (asf->packet_frag_size > asf->packet_size_left - rsize) {
    1177           0 :             if (asf->packet_frag_size > asf->packet_size_left - rsize + asf->packet_padsize) {
    1178           0 :                 av_log(s, AV_LOG_ERROR, "packet_frag_size is invalid (%d>%d-%d+%d)\n",
    1179             :                        asf->packet_frag_size, asf->packet_size_left, rsize, asf->packet_padsize);
    1180           0 :                 return AVERROR_INVALIDDATA;
    1181             :             } else {
    1182           0 :                 int diff = asf->packet_frag_size - (asf->packet_size_left - rsize);
    1183           0 :                 asf->packet_size_left += diff;
    1184           0 :                 asf->packet_padsize   -= diff;
    1185             :             }
    1186             :         }
    1187             :     } else {
    1188        1010 :         asf->packet_frag_size = asf->packet_size_left - rsize;
    1189             :     }
    1190        3916 :     if (asf->packet_replic_size == 1) {
    1191         130 :         asf->packet_multi_size = asf->packet_frag_size;
    1192         130 :         if (asf->packet_multi_size > asf->packet_size_left)
    1193           0 :             return AVERROR_INVALIDDATA;
    1194             :     }
    1195        3916 :     asf->packet_size_left -= rsize;
    1196             : 
    1197        3916 :     return 0;
    1198             : }
    1199             : 
    1200             : /**
    1201             :  * Parse data from individual ASF packets (which were previously loaded
    1202             :  * with asf_get_packet()).
    1203             :  * @param s demux context
    1204             :  * @param pb context to read data from
    1205             :  * @param pkt pointer to store packet data into
    1206             :  * @return 0 if data was stored in pkt, <0 on error or 1 if more ASF
    1207             :  *          packets need to be loaded (through asf_get_packet())
    1208             :  */
    1209        4385 : static int asf_parse_packet(AVFormatContext *s, AVIOContext *pb, AVPacket *pkt)
    1210             : {
    1211        4385 :     ASFContext *asf   = s->priv_data;
    1212        4385 :     ASFStream *asf_st = 0;
    1213        1577 :     for (;;) {
    1214             :         int ret;
    1215        5962 :         if (avio_feof(pb))
    1216          10 :             return AVERROR_EOF;
    1217        9909 :         if (asf->packet_size_left < FRAME_HEADER_SIZE ||
    1218        3958 :             asf->packet_segments < 1 && asf->packet_time_start == 0) {
    1219        1995 :             int ret = asf->packet_size_left + asf->packet_padsize;
    1220             : 
    1221        1995 :             if (asf->packet_size_left && asf->packet_size_left < FRAME_HEADER_SIZE)
    1222           0 :                 av_log(s, AV_LOG_WARNING, "Skip due to FRAME_HEADER_SIZE\n");
    1223             : 
    1224        1995 :             assert(ret >= 0);
    1225             :             /* fail safe */
    1226        1995 :             avio_skip(pb, ret);
    1227             : 
    1228        1995 :             asf->packet_pos = avio_tell(pb);
    1229        3990 :             if (asf->data_object_size != (uint64_t)-1 &&
    1230        1995 :                 (asf->packet_pos - asf->data_object_offset >= asf->data_object_size))
    1231          51 :                 return AVERROR_EOF;  /* Do not exceed the size of the data object */
    1232        1944 :             return 1;
    1233             :         }
    1234        3957 :         if (asf->packet_time_start == 0) {
    1235        3916 :             if (asf_read_frame_header(s, pb) < 0) {
    1236           0 :                 asf->packet_time_start = asf->packet_segments = 0;
    1237           0 :                 continue;
    1238             :             }
    1239        7832 :             if (asf->stream_index < 0 ||
    1240        7543 :                 s->streams[asf->stream_index]->discard >= AVDISCARD_ALL ||
    1241        6753 :                 (!asf->packet_key_frame &&
    1242        6252 :                  (s->streams[asf->stream_index]->discard >= AVDISCARD_NONKEY || asf->streams[s->streams[asf->stream_index]->id].skip_to_key))) {
    1243         304 :                 asf->packet_time_start = 0;
    1244             :                 /* unhandled packet (should not happen) */
    1245         304 :                 avio_skip(pb, asf->packet_frag_size);
    1246         304 :                 asf->packet_size_left -= asf->packet_frag_size;
    1247         304 :                 if (asf->stream_index < 0)
    1248           0 :                     av_log(s, AV_LOG_ERROR, "ff asf skip %d (unknown stream)\n",
    1249             :                            asf->packet_frag_size);
    1250         304 :                 continue;
    1251             :             }
    1252        3612 :             asf->asf_st = &asf->streams[s->streams[asf->stream_index]->id];
    1253        3612 :             if (!asf->packet_frag_offset)
    1254        2367 :                 asf->asf_st->skip_to_key = 0;
    1255             :         }
    1256        3653 :         asf_st = asf->asf_st;
    1257        3653 :         av_assert0(asf_st);
    1258             : 
    1259        3653 :         if (!asf_st->frag_offset && asf->packet_frag_offset) {
    1260          30 :             av_log(s, AV_LOG_TRACE, "skipping asf data pkt with fragment offset for "
    1261             :                     "stream:%d, expected:%d but got %d from pkt)\n",
    1262             :                     asf->stream_index, asf_st->frag_offset,
    1263             :                     asf->packet_frag_offset);
    1264          30 :             avio_skip(pb, asf->packet_frag_size);
    1265          30 :             asf->packet_size_left -= asf->packet_frag_size;
    1266          30 :             continue;
    1267             :         }
    1268             : 
    1269        3623 :         if (asf->packet_replic_size == 1) {
    1270             :             // frag_offset is here used as the beginning timestamp
    1271         115 :             asf->packet_frag_timestamp = asf->packet_time_start;
    1272         115 :             asf->packet_time_start    += asf->packet_time_delta;
    1273         115 :             asf_st->packet_obj_size    = asf->packet_frag_size = avio_r8(pb);
    1274         115 :             asf->packet_size_left--;
    1275         115 :             asf->packet_multi_size--;
    1276         115 :             if (asf->packet_multi_size < asf_st->packet_obj_size) {
    1277          28 :                 asf->packet_time_start = 0;
    1278          28 :                 avio_skip(pb, asf->packet_multi_size);
    1279          28 :                 asf->packet_size_left -= asf->packet_multi_size;
    1280          28 :                 continue;
    1281             :             }
    1282          87 :             asf->packet_multi_size -= asf_st->packet_obj_size;
    1283             :         }
    1284             : 
    1285        4810 :         if (asf_st->pkt.size != asf_st->packet_obj_size ||
    1286             :             // FIXME is this condition sufficient?
    1287        1215 :             asf_st->frag_offset + asf->packet_frag_size > asf_st->pkt.size) {
    1288             :             int ret;
    1289             : 
    1290        2380 :             if (asf_st->pkt.data) {
    1291           0 :                 av_log(s, AV_LOG_INFO,
    1292             :                        "freeing incomplete packet size %d, new %d\n",
    1293             :                        asf_st->pkt.size, asf_st->packet_obj_size);
    1294           0 :                 asf_st->frag_offset = 0;
    1295           0 :                 av_packet_unref(&asf_st->pkt);
    1296             :             }
    1297             :             /* new packet */
    1298        2380 :             if ((ret = av_new_packet(&asf_st->pkt, asf_st->packet_obj_size)) < 0)
    1299           0 :                 return ret;
    1300        2380 :             asf_st->seq              = asf->packet_seq;
    1301        2380 :             if (asf->ts_is_pts) {
    1302           0 :                 asf_st->pkt.pts          = asf->packet_frag_timestamp - asf->hdr.preroll;
    1303             :             } else
    1304        2380 :                 asf_st->pkt.dts          = asf->packet_frag_timestamp - asf->hdr.preroll;
    1305        2380 :             asf_st->pkt.stream_index = asf->stream_index;
    1306        2380 :             asf_st->pkt.pos          = asf_st->packet_pos = asf->packet_pos;
    1307        2380 :             asf_st->pkt_clean        = 0;
    1308             : 
    1309        2380 :             if (asf_st->pkt.data && asf_st->palette_changed) {
    1310             :                 uint8_t *pal;
    1311           0 :                 pal = av_packet_new_side_data(&asf_st->pkt, AV_PKT_DATA_PALETTE,
    1312             :                                               AVPALETTE_SIZE);
    1313           0 :                 if (!pal) {
    1314           0 :                     av_log(s, AV_LOG_ERROR, "Cannot append palette to packet\n");
    1315             :                 } else {
    1316           0 :                     memcpy(pal, asf_st->palette, AVPALETTE_SIZE);
    1317           0 :                     asf_st->palette_changed = 0;
    1318             :                 }
    1319             :             }
    1320        7140 :             av_log(asf, AV_LOG_TRACE, "new packet: stream:%d key:%d packet_key:%d audio:%d size:%d\n",
    1321             :                     asf->stream_index, asf->packet_key_frame,
    1322        2380 :                     asf_st->pkt.flags & AV_PKT_FLAG_KEY,
    1323        2380 :                     s->streams[asf->stream_index]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO,
    1324             :                     asf_st->packet_obj_size);
    1325        2380 :             if (s->streams[asf->stream_index]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)
    1326        1015 :                 asf->packet_key_frame = 1;
    1327        2380 :             if (asf->packet_key_frame)
    1328        1058 :                 asf_st->pkt.flags |= AV_PKT_FLAG_KEY;
    1329             :         }
    1330             : 
    1331             :         /* read data */
    1332        3595 :         av_log(asf, AV_LOG_TRACE, "READ PACKET s:%d  os:%d  o:%d,%d  l:%d   DATA:%p\n",
    1333             :                 s->packet_size, asf_st->pkt.size, asf->packet_frag_offset,
    1334             :                 asf_st->frag_offset, asf->packet_frag_size, asf_st->pkt.data);
    1335        3595 :         asf->packet_size_left -= asf->packet_frag_size;
    1336        3595 :         if (asf->packet_size_left < 0)
    1337           0 :             continue;
    1338             : 
    1339        7190 :         if (asf->packet_frag_offset >= asf_st->pkt.size ||
    1340        3595 :             asf->packet_frag_size > asf_st->pkt.size - asf->packet_frag_offset) {
    1341           0 :             av_log(s, AV_LOG_ERROR,
    1342             :                    "packet fragment position invalid %u,%u not in %u\n",
    1343             :                    asf->packet_frag_offset, asf->packet_frag_size,
    1344             :                    asf_st->pkt.size);
    1345           0 :             continue;
    1346             :         }
    1347             : 
    1348        3595 :         if (asf->packet_frag_offset != asf_st->frag_offset && !asf_st->pkt_clean) {
    1349           0 :             memset(asf_st->pkt.data + asf_st->frag_offset, 0, asf_st->pkt.size - asf_st->frag_offset);
    1350           0 :             asf_st->pkt_clean = 1;
    1351             :         }
    1352             : 
    1353        3595 :         ret = avio_read(pb, asf_st->pkt.data + asf->packet_frag_offset,
    1354        3595 :                         asf->packet_frag_size);
    1355        3595 :         if (ret != asf->packet_frag_size) {
    1356           3 :             if (ret < 0 || asf->packet_frag_offset + ret == 0)
    1357           0 :                 return ret < 0 ? ret : AVERROR_EOF;
    1358             : 
    1359           3 :             if (asf_st->ds_span > 1) {
    1360             :                 // scrambling, we can either drop it completely or fill the remainder
    1361             :                 // TODO: should we fill the whole packet instead of just the current
    1362             :                 // fragment?
    1363           0 :                 memset(asf_st->pkt.data + asf->packet_frag_offset + ret, 0,
    1364           0 :                        asf->packet_frag_size - ret);
    1365           0 :                 ret = asf->packet_frag_size;
    1366             :             } else {
    1367             :                 // no scrambling, so we can return partial packets
    1368           3 :                 av_shrink_packet(&asf_st->pkt, asf->packet_frag_offset + ret);
    1369             :             }
    1370             :         }
    1371        3595 :         if (s->key && s->keylen == 20)
    1372         548 :             ff_asfcrypt_dec(s->key, asf_st->pkt.data + asf->packet_frag_offset,
    1373             :                             ret);
    1374        3595 :         asf_st->frag_offset += ret;
    1375             :         /* test if whole packet is read */
    1376        3595 :         if (asf_st->frag_offset == asf_st->pkt.size) {
    1377             :             // workaround for macroshit radio DVR-MS files
    1378        2380 :             if (s->streams[asf->stream_index]->codecpar->codec_id == AV_CODEC_ID_MPEG2VIDEO &&
    1379           0 :                 asf_st->pkt.size > 100) {
    1380             :                 int i;
    1381           0 :                 for (i = 0; i < asf_st->pkt.size && !asf_st->pkt.data[i]; i++)
    1382             :                     ;
    1383           0 :                 if (i == asf_st->pkt.size) {
    1384           0 :                     av_log(s, AV_LOG_DEBUG, "discarding ms fart\n");
    1385           0 :                     asf_st->frag_offset = 0;
    1386           0 :                     av_packet_unref(&asf_st->pkt);
    1387           0 :                     continue;
    1388             :                 }
    1389             :             }
    1390             : 
    1391             :             /* return packet */
    1392        2380 :             if (asf_st->ds_span > 1) {
    1393           0 :                 if (asf_st->pkt.size != asf_st->ds_packet_size * asf_st->ds_span) {
    1394           0 :                     av_log(s, AV_LOG_ERROR,
    1395             :                            "pkt.size != ds_packet_size * ds_span (%d %d %d)\n",
    1396             :                            asf_st->pkt.size, asf_st->ds_packet_size,
    1397             :                            asf_st->ds_span);
    1398             :                 } else {
    1399             :                     /* packet descrambling */
    1400           0 :                     AVBufferRef *buf = av_buffer_alloc(asf_st->pkt.size +
    1401             :                                                        AV_INPUT_BUFFER_PADDING_SIZE);
    1402           0 :                     if (buf) {
    1403           0 :                         uint8_t *newdata = buf->data;
    1404           0 :                         int offset = 0;
    1405           0 :                         memset(newdata + asf_st->pkt.size, 0,
    1406             :                                AV_INPUT_BUFFER_PADDING_SIZE);
    1407           0 :                         while (offset < asf_st->pkt.size) {
    1408           0 :                             int off = offset / asf_st->ds_chunk_size;
    1409           0 :                             int row = off / asf_st->ds_span;
    1410           0 :                             int col = off % asf_st->ds_span;
    1411           0 :                             int idx = row + col * asf_st->ds_packet_size / asf_st->ds_chunk_size;
    1412           0 :                             assert(offset + asf_st->ds_chunk_size <= asf_st->pkt.size);
    1413           0 :                             assert(idx + 1 <= asf_st->pkt.size / asf_st->ds_chunk_size);
    1414           0 :                             memcpy(newdata + offset,
    1415           0 :                                    asf_st->pkt.data + idx * asf_st->ds_chunk_size,
    1416           0 :                                    asf_st->ds_chunk_size);
    1417           0 :                             offset += asf_st->ds_chunk_size;
    1418             :                         }
    1419           0 :                         av_buffer_unref(&asf_st->pkt.buf);
    1420           0 :                         asf_st->pkt.buf  = buf;
    1421           0 :                         asf_st->pkt.data = buf->data;
    1422             :                     }
    1423             :                 }
    1424             :             }
    1425        2380 :             asf_st->frag_offset         = 0;
    1426        2380 :             *pkt                        = asf_st->pkt;
    1427        2380 :             asf_st->pkt.buf             = 0;
    1428        2380 :             asf_st->pkt.size            = 0;
    1429        2380 :             asf_st->pkt.data            = 0;
    1430        2380 :             asf_st->pkt.side_data_elems = 0;
    1431        2380 :             asf_st->pkt.side_data       = NULL;
    1432        2380 :             break; // packet completed
    1433             :         }
    1434             :     }
    1435        2380 :     return 0;
    1436             : }
    1437             : 
    1438        2441 : static int asf_read_packet(AVFormatContext *s, AVPacket *pkt)
    1439             : {
    1440        2441 :     ASFContext *asf = s->priv_data;
    1441             : 
    1442        1944 :     for (;;) {
    1443             :         int ret;
    1444             : 
    1445             :         /* parse cached packets, if any */
    1446        4385 :         if ((ret = asf_parse_packet(s, s->pb, pkt)) <= 0)
    1447        4882 :             return ret;
    1448        1944 :         if ((ret = asf_get_packet(s, s->pb)) < 0)
    1449           0 :             assert(asf->packet_size_left < FRAME_HEADER_SIZE ||
    1450             :                    asf->packet_segments < 1);
    1451        1944 :         asf->packet_time_start = 0;
    1452             :     }
    1453             : }
    1454             : 
    1455             : // Added to support seeking after packets have been read
    1456             : // If information is not reset, read_packet fails due to
    1457             : // leftover information from previous reads
    1458          81 : static void asf_reset_header(AVFormatContext *s)
    1459             : {
    1460          81 :     ASFContext *asf = s->priv_data;
    1461             :     ASFStream *asf_st;
    1462             :     int i;
    1463             : 
    1464          81 :     asf->packet_size_left      = 0;
    1465          81 :     asf->packet_flags          = 0;
    1466          81 :     asf->packet_property       = 0;
    1467          81 :     asf->packet_timestamp      = 0;
    1468          81 :     asf->packet_segsizetype    = 0;
    1469          81 :     asf->packet_segments       = 0;
    1470          81 :     asf->packet_seq            = 0;
    1471          81 :     asf->packet_replic_size    = 0;
    1472          81 :     asf->packet_key_frame      = 0;
    1473          81 :     asf->packet_padsize        = 0;
    1474          81 :     asf->packet_frag_offset    = 0;
    1475          81 :     asf->packet_frag_size      = 0;
    1476          81 :     asf->packet_frag_timestamp = 0;
    1477          81 :     asf->packet_multi_size     = 0;
    1478          81 :     asf->packet_time_delta     = 0;
    1479          81 :     asf->packet_time_start     = 0;
    1480             : 
    1481       10449 :     for (i = 0; i < 128; i++) {
    1482       10368 :         asf_st = &asf->streams[i];
    1483       10368 :         av_packet_unref(&asf_st->pkt);
    1484       10368 :         asf_st->packet_obj_size = 0;
    1485       10368 :         asf_st->frag_offset = 0;
    1486       10368 :         asf_st->seq         = 0;
    1487             :     }
    1488          81 :     asf->asf_st = NULL;
    1489          81 : }
    1490             : 
    1491          26 : static void skip_to_key(AVFormatContext *s)
    1492             : {
    1493          26 :     ASFContext *asf = s->priv_data;
    1494             :     int i;
    1495             : 
    1496        3354 :     for (i = 0; i < 128; i++) {
    1497        3328 :         int j = asf->asfid2avid[i];
    1498        3328 :         ASFStream *asf_st = &asf->streams[i];
    1499        3328 :         if (j < 0 || s->streams[j]->codecpar->codec_type != AVMEDIA_TYPE_VIDEO)
    1500        3302 :             continue;
    1501             : 
    1502          26 :         asf_st->skip_to_key = 1;
    1503             :     }
    1504          26 : }
    1505             : 
    1506          32 : static int asf_read_close(AVFormatContext *s)
    1507             : {
    1508          32 :     asf_reset_header(s);
    1509             : 
    1510          32 :     return 0;
    1511             : }
    1512             : 
    1513          23 : static int64_t asf_read_pts(AVFormatContext *s, int stream_index,
    1514             :                             int64_t *ppos, int64_t pos_limit)
    1515             : {
    1516          23 :     ASFContext *asf     = s->priv_data;
    1517          23 :     AVPacket pkt1, *pkt = &pkt1;
    1518             :     ASFStream *asf_st;
    1519             :     int64_t pts;
    1520          23 :     int64_t pos = *ppos;
    1521             :     int i;
    1522             :     int64_t start_pos[ASF_MAX_STREAMS];
    1523             : 
    1524          69 :     for (i = 0; i < s->nb_streams; i++)
    1525          46 :         start_pos[i] = pos;
    1526             : 
    1527          23 :     if (s->packet_size > 0)
    1528          69 :         pos = (pos + s->packet_size - 1 - s->internal->data_offset) /
    1529          46 :               s->packet_size * s->packet_size +
    1530          23 :               s->internal->data_offset;
    1531          23 :     *ppos = pos;
    1532          23 :     if (avio_seek(s->pb, pos, SEEK_SET) < 0)
    1533           0 :         return AV_NOPTS_VALUE;
    1534             : 
    1535          23 :     ff_read_frame_flush(s);
    1536          23 :     asf_reset_header(s);
    1537             :     for (;;) {
    1538          49 :         if (av_read_frame(s, pkt) < 0) {
    1539          15 :             av_log(s, AV_LOG_INFO, "asf_read_pts failed\n");
    1540          15 :             return AV_NOPTS_VALUE;
    1541             :         }
    1542             : 
    1543          21 :         pts = pkt->dts;
    1544             : 
    1545          21 :         if (pkt->flags & AV_PKT_FLAG_KEY) {
    1546          21 :             i = pkt->stream_index;
    1547             : 
    1548          21 :             asf_st = &asf->streams[s->streams[i]->id];
    1549             : 
    1550             : //            assert((asf_st->packet_pos - s->data_offset) % s->packet_size == 0);
    1551          21 :             pos = asf_st->packet_pos;
    1552             :             av_assert1(pkt->pos == asf_st->packet_pos);
    1553             : 
    1554          21 :             av_add_index_entry(s->streams[i], pos, pts, pkt->size,
    1555          21 :                                pos - start_pos[i] + 1, AVINDEX_KEYFRAME);
    1556          21 :             start_pos[i] = asf_st->packet_pos + 1;
    1557             : 
    1558          21 :             if (pkt->stream_index == stream_index) {
    1559           8 :                 av_packet_unref(pkt);
    1560           8 :                 break;
    1561             :             }
    1562             :         }
    1563          13 :         av_packet_unref(pkt);
    1564             :     }
    1565             : 
    1566           8 :     *ppos = pos;
    1567           8 :     return pts;
    1568             : }
    1569             : 
    1570           1 : static int asf_build_simple_index(AVFormatContext *s, int stream_index)
    1571             : {
    1572             :     ff_asf_guid g;
    1573           1 :     ASFContext *asf     = s->priv_data;
    1574           1 :     int64_t current_pos = avio_tell(s->pb);
    1575             :     int64_t ret;
    1576             : 
    1577           1 :     if((ret = avio_seek(s->pb, asf->data_object_offset + asf->data_object_size, SEEK_SET)) < 0) {
    1578           0 :         return ret;
    1579             :     }
    1580             : 
    1581           1 :     if ((ret = ff_get_guid(s->pb, &g)) < 0)
    1582           0 :         goto end;
    1583             : 
    1584             :     /* the data object can be followed by other top-level objects,
    1585             :      * skip them until the simple index object is reached */
    1586           2 :     while (ff_guidcmp(&g, &ff_asf_simple_index_header)) {
    1587           0 :         int64_t gsize = avio_rl64(s->pb);
    1588           0 :         if (gsize < 24 || avio_feof(s->pb)) {
    1589             :             goto end;
    1590             :         }
    1591           0 :         avio_skip(s->pb, gsize - 24);
    1592           0 :         if ((ret = ff_get_guid(s->pb, &g)) < 0)
    1593           0 :             goto end;
    1594             :     }
    1595             : 
    1596             :     {
    1597           1 :         int64_t itime, last_pos = -1;
    1598             :         int pct, ict;
    1599             :         int i;
    1600           1 :         int64_t av_unused gsize = avio_rl64(s->pb);
    1601           1 :         if ((ret = ff_get_guid(s->pb, &g)) < 0)
    1602           0 :             goto end;
    1603           1 :         itime = avio_rl64(s->pb);
    1604           1 :         pct   = avio_rl32(s->pb);
    1605           1 :         ict   = avio_rl32(s->pb);
    1606           1 :         av_log(s, AV_LOG_DEBUG,
    1607             :                "itime:0x%"PRIx64", pct:%d, ict:%d\n", itime, pct, ict);
    1608             : 
    1609           7 :         for (i = 0; i < ict; i++) {
    1610           6 :             int pktnum        = avio_rl32(s->pb);
    1611           6 :             int pktct         = avio_rl16(s->pb);
    1612           6 :             int64_t pos       = s->internal->data_offset + s->packet_size * (int64_t)pktnum;
    1613           6 :             int64_t index_pts = FFMAX(av_rescale(itime, i, 10000) - asf->hdr.preroll, 0);
    1614             : 
    1615           6 :             if (avio_feof(s->pb)) {
    1616           0 :                 ret = AVERROR_INVALIDDATA;
    1617           0 :                 goto end;
    1618             :             }
    1619             : 
    1620           6 :             if (pos != last_pos) {
    1621           3 :                 av_log(s, AV_LOG_DEBUG, "pktnum:%d, pktct:%d  pts: %"PRId64"\n",
    1622             :                        pktnum, pktct, index_pts);
    1623           3 :                 av_add_index_entry(s->streams[stream_index], pos, index_pts,
    1624           3 :                                    s->packet_size, 0, AVINDEX_KEYFRAME);
    1625           3 :                 last_pos = pos;
    1626             :             }
    1627             :         }
    1628           1 :         asf->index_read = ict > 1;
    1629             :     }
    1630           1 : end:
    1631             : //     if (avio_feof(s->pb)) {
    1632             : //         ret = 0;
    1633             : //     }
    1634           1 :     avio_seek(s->pb, current_pos, SEEK_SET);
    1635           1 :     return ret;
    1636             : }
    1637             : 
    1638          26 : static int asf_read_seek(AVFormatContext *s, int stream_index,
    1639             :                          int64_t pts, int flags)
    1640             : {
    1641          26 :     ASFContext *asf = s->priv_data;
    1642          26 :     AVStream *st    = s->streams[stream_index];
    1643          26 :     int ret = 0;
    1644             : 
    1645          26 :     if (s->packet_size <= 0)
    1646           0 :         return -1;
    1647             : 
    1648             :     /* Try using the protocol's read_seek if available */
    1649          26 :     if (s->pb) {
    1650          26 :         int64_t ret = avio_seek_time(s->pb, stream_index, pts, flags);
    1651          26 :         if (ret >= 0)
    1652           0 :             asf_reset_header(s);
    1653          26 :         if (ret != AVERROR(ENOSYS))
    1654           0 :             return ret;
    1655             :     }
    1656             : 
    1657             :     /* explicitly handle the case of seeking to 0 */
    1658          26 :     if (!pts) {
    1659           0 :         asf_reset_header(s);
    1660           0 :         avio_seek(s->pb, s->internal->data_offset, SEEK_SET);
    1661           0 :         return 0;
    1662             :     }
    1663             : 
    1664          26 :     if (!asf->index_read) {
    1665           1 :         ret = asf_build_simple_index(s, stream_index);
    1666           1 :         if (ret < 0)
    1667           0 :             asf->index_read = -1;
    1668             :     }
    1669             : 
    1670          26 :     if (asf->index_read > 0 && st->index_entries) {
    1671          26 :         int index = av_index_search_timestamp(st, pts, flags);
    1672          26 :         if (index >= 0) {
    1673             :             /* find the position */
    1674          18 :             uint64_t pos = st->index_entries[index].pos;
    1675             : 
    1676             :             /* do the seek */
    1677          18 :             av_log(s, AV_LOG_DEBUG, "SEEKTO: %"PRId64"\n", pos);
    1678          18 :             if(avio_seek(s->pb, pos, SEEK_SET) < 0)
    1679           0 :                 return -1;
    1680          18 :             asf_reset_header(s);
    1681          18 :             skip_to_key(s);
    1682          18 :             return 0;
    1683             :         }
    1684             :     }
    1685             :     /* no index or seeking by index failed */
    1686           8 :     if (ff_seek_frame_binary(s, stream_index, pts, flags) < 0)
    1687           0 :         return -1;
    1688           8 :     asf_reset_header(s);
    1689           8 :     skip_to_key(s);
    1690           8 :     return 0;
    1691             : }
    1692             : 
    1693             : AVInputFormat ff_asf_demuxer = {
    1694             :     .name           = "asf",
    1695             :     .long_name      = NULL_IF_CONFIG_SMALL("ASF (Advanced / Active Streaming Format)"),
    1696             :     .priv_data_size = sizeof(ASFContext),
    1697             :     .read_probe     = asf_probe,
    1698             :     .read_header    = asf_read_header,
    1699             :     .read_packet    = asf_read_packet,
    1700             :     .read_close     = asf_read_close,
    1701             :     .read_seek      = asf_read_seek,
    1702             :     .read_timestamp = asf_read_pts,
    1703             :     .flags          = AVFMT_NOBINSEARCH | AVFMT_NOGENSEARCH,
    1704             :     .priv_class     = &asf_class,
    1705             : };

Generated by: LCOV version 1.13