Merge commit '48a4ffa722c0874b251de9d201babed52cef0bcb'
authorMichael Niedermayer <michaelni@gmx.at>
Wed, 6 Feb 2013 13:10:26 +0000 (14:10 +0100)
committerMichael Niedermayer <michaelni@gmx.at>
Wed, 6 Feb 2013 13:25:28 +0000 (14:25 +0100)
* commit '48a4ffa722c0874b251de9d201babed52cef0bcb':
  asf: K&R formatting cosmetics
  vc1dec: use codec_id instead of codec_tag for VC1IMAGE
  sh4: drop unused functions

Conflicts:
libavformat/asf.c
libavformat/asfdec.c
libavformat/asfenc.c

Merged-by: Michael Niedermayer <michaelni@gmx.at>
1  2 
libavcodec/sh4/dsputil_align.c
libavcodec/vc1dec.c
libavformat/asf.c
libavformat/asfcrypt.c
libavformat/asfdec.c
libavformat/asfenc.c

Simple merge
Simple merge
@@@ -138,22 -137,23 +137,22 @@@ const ff_asf_guid ff_asf_digital_signat
  
  /* List of official tags at http://msdn.microsoft.com/en-us/library/dd743066(VS.85).aspx */
  const AVMetadataConv ff_asf_metadata_conv[] = {
-     { "WM/AlbumArtist"     , "album_artist"},
-     { "WM/AlbumTitle"      , "album"       },
-     { "Author"             , "artist"      },
-     { "Description"        , "comment"     },
-     { "WM/Composer"        , "composer"    },
-     { "WM/EncodedBy"       , "encoded_by"  },
-     { "WM/EncodingSettings", "encoder"     },
-     { "WM/Genre"           , "genre"       },
-     { "WM/Language"        , "language"    },
-     { "WM/OriginalFilename", "filename"    },
-     { "WM/PartOfSet"       , "disc"        },
-     { "WM/Publisher"       , "publisher"   },
-     { "WM/Tool"            , "encoder"     },
-     { "WM/TrackNumber"     , "track"       },
+     { "WM/AlbumArtist",          "album_artist"     },
+     { "WM/AlbumTitle",           "album"            },
+     { "Author",                  "artist"           },
+     { "Description",             "comment"          },
+     { "WM/Composer",             "composer"         },
+     { "WM/EncodedBy",            "encoded_by"       },
+     { "WM/EncodingSettings",     "encoder"          },
+     { "WM/Genre",                "genre"            },
+     { "WM/Language",             "language"         },
+     { "WM/OriginalFilename",     "filename"         },
+     { "WM/PartOfSet",            "disc"             },
+     { "WM/Publisher",            "publisher"        },
+     { "WM/Tool",                 "encoder"          },
+     { "WM/TrackNumber",          "track"            },
 -    { "WM/Track",                "track"            },
      { "WM/MediaStationCallSign", "service_provider" },
-     { "WM/MediaStationName", "service_name" },
+     { "WM/MediaStationName",     "service_name"     },
  //  { "Year"               , "date"        }, TODO: conversion year<->date
      { 0 }
  };
Simple merge
@@@ -273,10 -287,7 +277,10 @@@ static void get_tag(AVFormatContext *s
          goto finish;
  
      if (type == 0) {         // UTF16-LE
-         avio_get_str16le(s->pb, len, value, 2*len + 1);
+         avio_get_str16le(s->pb, len, value, 2 * len + 1);
 +    } else if (type == -1) { // ASCII
 +        avio_read(s->pb, value, len);
 +        value[len]=0;
      } else if (type > 1 && type <= 5) {  // boolean or DWORD or QWORD or WORD
          uint64_t num = get_value(s->pb, type);
          snprintf(value, len, "%"PRIu64, num);
@@@ -345,16 -358,15 +351,16 @@@ static int asf_read_stream_properties(A
      asf_st = av_mallocz(sizeof(ASFStream));
      if (!asf_st)
          return AVERROR(ENOMEM);
-     st->priv_data = asf_st;
-     start_time = asf->hdr.preroll;
+     st->priv_data  = asf_st;
 -    st->start_time = 0;
+     start_time     = asf->hdr.preroll;
  
      asf_st->stream_language_index = 128; // invalid stream index means no language info
  
-     if(!(asf->hdr.flags & 0x01)) { // if we aren't streaming...
+     if (!(asf->hdr.flags & 0x01)) { // if we aren't streaming...
 -        st->duration = asf->hdr.play_time /
 +        int64_t fsize = avio_size(pb);
 +        if (fsize <= 0 || (int64_t)asf->hdr.file_size <= 0 || FFABS(fsize - (int64_t)asf->hdr.file_size) < 10000)
 +            st->duration = asf->hdr.play_time /
-                 (10000000 / 1000) - start_time;
+                        (10000000 / 1000) - start_time;
      }
      ff_get_guid(pb, &g);
  
          if (is_dvr_ms_audio) {
              // codec_id and codec_tag are unreliable in dvr_ms
              // files. Set them later by probing stream.
-             st->request_probe= 1;
 -            st->codec->codec_id  = AV_CODEC_ID_PROBE;
++            st->request_probe    = 1;
              st->codec->codec_tag = 0;
          }
-         if (st->codec->codec_id == AV_CODEC_ID_AAC) {
+         if (st->codec->codec_id == AV_CODEC_ID_AAC)
              st->need_parsing = AVSTREAM_PARSE_NONE;
-         } else {
+         else
              st->need_parsing = AVSTREAM_PARSE_FULL;
-         }
          /* We have to init the frame size at some point .... */
          pos2 = avio_tell(pb);
          if (size >= (pos2 + 8 - pos1 + 24)) {
          /* not available for asf */
          avio_rl16(pb); /* panes */
          st->codec->bits_per_coded_sample = avio_rl16(pb); /* depth */
-         tag1 = avio_rl32(pb);
+         tag1                             = avio_rl32(pb);
          avio_skip(pb, 20);
          if (sizeX > 40) {
 -            st->codec->extradata_size = sizeX - 40;
 +            st->codec->extradata_size = ffio_limit(pb, sizeX - 40);
-             st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
+             st->codec->extradata      = av_mallocz(st->codec->extradata_size +
+                                                    FF_INPUT_BUFFER_PADDING_SIZE);
              avio_read(pb, st->codec->extradata, st->codec->extradata_size);
          }
  
@@@ -509,33 -523,23 +517,33 @@@ static int asf_read_ext_stream_properti
          asf->streams[stream_num].stream_language_index = stream_languageid_index;
  
      avio_rl64(pb); // avg frametime in 100ns units
-     stream_ct = avio_rl16(pb); //stream-name-count
-     payload_ext_ct = avio_rl16(pb); //payload-extension-system-count
+     stream_ct      = avio_rl16(pb); // stream-name-count
+     payload_ext_ct = avio_rl16(pb); // payload-extension-system-count
  
 -    if (stream_num < 128)
 +    if (stream_num < 128) {
          asf->stream_bitrates[stream_num] = leak_rate;
 +        asf->streams[stream_num].payload_ext_ct = 0;
 +    }
  
-     for (i=0; i<stream_ct; i++){
+     for (i = 0; i < stream_ct; i++) {
          avio_rl16(pb);
          ext_len = avio_rl16(pb);
          avio_skip(pb, ext_len);
      }
  
-     for (i=0; i<payload_ext_ct; i++){
+     for (i = 0; i < payload_ext_ct; i++) {
 +        int size;
          ff_get_guid(pb, &g);
 -        avio_skip(pb, 2);
 +        size = avio_rl16(pb);
-         ext_len=avio_rl32(pb);
+         ext_len = avio_rl32(pb);
          avio_skip(pb, ext_len);
 +        if (stream_num < 128 && i < FF_ARRAY_ELEMS(asf->streams[stream_num].payload)) {
 +            ASFPayload *p = &asf->streams[stream_num].payload[i];
 +            p->type = g[0];
 +            p->size = size;
 +            av_log(s, AV_LOG_DEBUG, "Payload extension %x %d\n", g[0], p->size );
 +            asf->streams[stream_num].payload_ext_ct ++;
 +        }
      }
  
      return 0;
@@@ -744,23 -754,14 +758,25 @@@ static int asf_read_header(AVFormatCont
          } else {
              if (!s->keylen) {
                  if (!ff_guidcmp(&g, &ff_asf_content_encryption)) {
-                     av_log(s, AV_LOG_WARNING, "DRM protected stream detected, decoding will likely fail!\n");
 +                    unsigned int len;
 +                    AVPacket pkt;
+                     av_log(s, AV_LOG_WARNING,
+                            "DRM protected stream detected, decoding will likely fail!\n");
 +                    len= avio_rl32(pb);
 +                    av_log(s, AV_LOG_DEBUG, "Secret data:\n");
 +                    av_get_packet(pb, &pkt, len); av_hex_dump_log(s, AV_LOG_DEBUG, pkt.data, pkt.size); av_free_packet(&pkt);
 +                    len= avio_rl32(pb);
 +                    get_tag(s, "ASF_Protection_Type", -1, len);
 +                    len= avio_rl32(pb);
 +                    get_tag(s, "ASF_Key_ID", -1, len);
 +                    len= avio_rl32(pb);
 +                    get_tag(s, "ASF_License_URL", -1, len);
                  } else if (!ff_guidcmp(&g, &ff_asf_ext_content_encryption)) {
-                     av_log(s, AV_LOG_WARNING, "Ext DRM protected stream detected, decoding will likely fail!\n");
+                     av_log(s, AV_LOG_WARNING,
+                            "Ext DRM protected stream detected, decoding will likely fail!\n");
 +                    av_dict_set(&s->metadata, "encryption", "ASF Extended Content Encryption", 0);
                  } else if (!ff_guidcmp(&g, &ff_asf_digital_signature)) {
 -                    av_log(s, AV_LOG_WARNING,
 -                           "Digital signature detected, decoding will likely fail!\n");
 +                    av_log(s, AV_LOG_INFO, "Digital signature detected!\n");
                  }
              }
          }
      avio_rl64(pb);
      avio_r8(pb);
      avio_r8(pb);
 -    if (pb->eof_reached)
 -        return -1;
 +    if (url_feof(pb))
 +        return AVERROR_EOF;
-     asf->data_offset = avio_tell(pb);
+     asf->data_offset      = avio_tell(pb);
      asf->packet_size_left = 0;
  
-     for(i=0; i<128; i++){
-         int stream_num= asf->asfid2avid[i];
-         if(stream_num>=0){
+     for (i = 0; i < 128; i++) {
+         int stream_num = asf->asfid2avid[i];
+         if (stream_num >= 0) {
              AVStream *st = s->streams[stream_num];
              if (!st->codec->bit_rate)
                  st->codec->bit_rate = asf->stream_bitrates[i];
@@@ -853,28 -869,27 +884,27 @@@ static int ff_asf_get_packet(AVFormatCo
      }
  
      if (c != 0x82) {
-         /**
-          * This code allows handling of -EAGAIN at packet boundaries (i.e.
+         /* This code allows handling of -EAGAIN at packet boundaries (i.e.
           * if the packet sync code above triggers -EAGAIN). This does not
           * imply complete -EAGAIN handling support at random positions in
-          * the stream.
-          */
+          * the stream. */
          if (pb->error == AVERROR(EAGAIN))
              return AVERROR(EAGAIN);
 -        if (!pb->eof_reached)
 +        if (!url_feof(pb))
-             av_log(s, AV_LOG_ERROR, "ff asf bad header %x  at:%"PRId64"\n", c, avio_tell(pb));
+             av_log(s, AV_LOG_ERROR,
+                    "ff asf bad header %x  at:%"PRId64"\n", c, avio_tell(pb));
      }
      if ((c & 0x8f) == 0x82) {
          if (d || e) {
 -            if (!pb->eof_reached)
 +            if (!url_feof(pb))
                  av_log(s, AV_LOG_ERROR, "ff asf bad non zero\n");
 -            return -1;
 +            return AVERROR_INVALIDDATA;
          }
-         c= avio_r8(pb);
-         d= avio_r8(pb);
-         rsize+=3;
-     }else if(!url_feof(pb)){
-         avio_seek(pb, -1, SEEK_CUR); //FIXME
+         c      = avio_r8(pb);
+         d      = avio_r8(pb);
+         rsize += 3;
 -    } else if (!pb->eof_reached) {
++    } else if(!url_feof(pb)) {
+         avio_seek(pb, -1, SEEK_CUR); // FIXME
      }
  
      asf->packet_flags    = c;
      DO_2BITS(asf->packet_flags >> 1, padsize, 0); // sequence ignored
      DO_2BITS(asf->packet_flags >> 3, padsize, 0); // padding length
  
-     //the following checks prevent overflows and infinite loops
-     if(!packet_length || packet_length >= (1U<<29)){
-         av_log(s, AV_LOG_ERROR, "invalid packet_length %d at:%"PRId64"\n", packet_length, avio_tell(pb));
+     // the following checks prevent overflows and infinite loops
+     if (!packet_length || packet_length >= (1U << 29)) {
+         av_log(s, AV_LOG_ERROR,
+                "invalid packet_length %d at:%"PRId64"\n",
+                packet_length, avio_tell(pb));
 -        return -1;
 +        return AVERROR_INVALIDDATA;
      }
-     if(padsize >= packet_length){
-         av_log(s, AV_LOG_ERROR, "invalid padsize %d at:%"PRId64"\n", padsize, avio_tell(pb));
+     if (padsize >= packet_length) {
+         av_log(s, AV_LOG_ERROR,
+                "invalid padsize %d at:%"PRId64"\n", padsize, avio_tell(pb));
 -        return -1;
 +        return AVERROR_INVALIDDATA;
      }
  
      asf->packet_timestamp = avio_rl32(pb);
   *
   * @return <0 if error
   */
- static int asf_read_frame_header(AVFormatContext *s, AVIOContext *pb){
+ static int asf_read_frame_header(AVFormatContext *s, AVIOContext *pb)
+ {
      ASFContext *asf = s->priv_data;
-     int rsize = 1;
-     int num = avio_r8(pb);
 +    ASFStream *asfst;
 -    int64_t ts0;
+     int rsize       = 1;
+     int num         = avio_r8(pb);
 +    int i;
 +    int64_t ts0, ts1 av_unused;
  
      asf->packet_segments--;
      asf->packet_key_frame = num >> 7;
-     asf->stream_index = asf->asfid2avid[num & 0x7f];
-     asfst = &asf->streams[num & 0x7f];
+     asf->stream_index     = asf->asfid2avid[num & 0x7f];
++    asfst                 = &asf->streams[num & 0x7f];
      // sequence should be ignored!
      DO_2BITS(asf->packet_property >> 4, asf->packet_seq, 0);
      DO_2BITS(asf->packet_property >> 2, asf->packet_frag_offset, 0);
      av_dlog(asf, "key:%d stream:%d seq:%d offset:%d replic_size:%d\n",
              asf->packet_key_frame, asf->stream_index, asf->packet_seq,
              asf->packet_frag_offset, asf->packet_replic_size);
 +    if (rsize+(int64_t)asf->packet_replic_size > asf->packet_size_left) {
 +        av_log(s, AV_LOG_ERROR, "packet_replic_size %d is invalid\n", asf->packet_replic_size);
 +        return AVERROR_INVALIDDATA;
 +    }
      if (asf->packet_replic_size >= 8) {
 +        int64_t end = avio_tell(pb) + asf->packet_replic_size;
 +        AVRational aspect;
          asf->packet_obj_size = avio_rl32(pb);
-         if(asf->packet_obj_size >= (1<<24) || asf->packet_obj_size <= 0){
+         if (asf->packet_obj_size >= (1 << 24) || asf->packet_obj_size <= 0) {
              av_log(s, AV_LOG_ERROR, "packet_obj_size invalid\n");
 -            return -1;
 +            return AVERROR_INVALIDDATA;
          }
          asf->packet_frag_timestamp = avio_rl32(pb); // timestamp
 -        if (asf->packet_replic_size >= 8 + 38 + 4) {
 -            avio_skip(pb, 10);
 -            ts0 = avio_rl64(pb);
 -            avio_skip(pb, 8);
 -            avio_skip(pb, 12);
 -            avio_rl32(pb);
 -            avio_skip(pb, asf->packet_replic_size - 8 - 38 - 4);
 -            if (ts0 != -1)
 -                asf->packet_frag_timestamp = ts0 / 10000;
 -            else
 -                asf->packet_frag_timestamp = AV_NOPTS_VALUE;
 -        } else
 -            avio_skip(pb, asf->packet_replic_size - 8);
 +
-         for (i=0; i<asfst->payload_ext_ct; i++) {
++        for (i = 0; i < asfst->payload_ext_ct; i++) {
 +            ASFPayload *p = &asfst->payload[i];
 +            int size = p->size;
 +            int64_t payend;
-             if(size == 0xFFFF)
++            if (size == 0xFFFF)
 +                size = avio_rl16(pb);
 +            payend = avio_tell(pb) + size;
 +            if (payend > end) {
 +                av_log(s, AV_LOG_ERROR, "too long payload\n");
 +                break;
 +            }
-             switch(p->type) {
++            switch (p->type) {
 +            case 0x50:
 +//              duration = avio_rl16(pb);
 +                break;
 +            case 0x54:
 +                aspect.num = avio_r8(pb);
 +                aspect.den = avio_r8(pb);
 +                if (aspect.num > 0 && aspect.den > 0 && asf->stream_index >= 0) {
 +                    s->streams[asf->stream_index]->sample_aspect_ratio = aspect;
 +                }
 +                break;
 +            case 0x2A:
 +                avio_skip(pb, 8);
-                 ts0= avio_rl64(pb);
-                 ts1= avio_rl64(pb);
-                 if(ts0!= -1) asf->packet_frag_timestamp= ts0/10000;
-                 else         asf->packet_frag_timestamp= AV_NOPTS_VALUE;
++                ts0 = avio_rl64(pb);
++                ts1 = avio_rl64(pb);
++                if (ts0!= -1) asf->packet_frag_timestamp = ts0/10000;
++                else          asf->packet_frag_timestamp = AV_NOPTS_VALUE;
 +                break;
 +            case 0x5B:
 +            case 0xB7:
 +            case 0xCC:
 +            case 0xC0:
 +            case 0xA0:
 +                //unknown
 +                break;
 +            }
 +            avio_seek(pb, payend, SEEK_SET);
 +        }
 +
 +        avio_seek(pb, end, SEEK_SET);
          rsize += asf->packet_replic_size; // FIXME - check validity
-     } else if (asf->packet_replic_size==1){
+     } else if (asf->packet_replic_size == 1) {
          // multipacket - frag_offset is beginning timestamp
-         asf->packet_time_start = asf->packet_frag_offset;
-         asf->packet_frag_offset = 0;
+         asf->packet_time_start     = asf->packet_frag_offset;
+         asf->packet_frag_offset    = 0;
          asf->packet_frag_timestamp = asf->packet_timestamp;
  
          asf->packet_time_delta = avio_r8(pb);
          rsize++;
-     }else if(asf->packet_replic_size!=0){
-         av_log(s, AV_LOG_ERROR, "unexpected packet_replic_size of %d\n", asf->packet_replic_size);
+     } else if (asf->packet_replic_size != 0) {
+         av_log(s, AV_LOG_ERROR, "unexpected packet_replic_size of %d\n",
+                asf->packet_replic_size);
 -        return -1;
 +        return AVERROR_INVALIDDATA;
      }
      if (asf->packet_flags & 0x01) {
          DO_2BITS(asf->packet_segsizetype >> 6, asf->packet_frag_size, 0); // 0 is illegal
          if (rsize > asf->packet_size_left) {
              av_log(s, AV_LOG_ERROR, "packet_replic_size is invalid\n");
 -            return -1;
 +            return AVERROR_INVALIDDATA;
-         } else if(asf->packet_frag_size > asf->packet_size_left - rsize){
+         } else if (asf->packet_frag_size > asf->packet_size_left - rsize) {
              if (asf->packet_frag_size > asf->packet_size_left - rsize + asf->packet_padsize) {
-                 av_log(s, AV_LOG_ERROR, "packet_frag_size is invalid (%d-%d)\n", asf->packet_size_left, rsize);
+                 av_log(s, AV_LOG_ERROR, "packet_frag_size is invalid (%d-%d)\n",
+                        asf->packet_size_left, rsize);
 -                return -1;
 +                return AVERROR_INVALIDDATA;
              } else {
                  int diff = asf->packet_frag_size - (asf->packet_size_left - rsize);
                  asf->packet_size_left += diff;
@@@ -1315,19 -1315,19 +1363,20 @@@ static int64_t asf_read_pts(AVFormatCon
      int i;
      int64_t start_pos[ASF_MAX_STREAMS];
  
-     for(i=0; i<s->nb_streams; i++){
-         start_pos[i]= pos;
-     }
+     for (i = 0; i < s->nb_streams; i++)
+         start_pos[i] = pos;
  
      if (s->packet_size > 0)
-         pos= (pos+s->packet_size-1-s->data_offset)/s->packet_size*s->packet_size+ s->data_offset;
-     *ppos= pos;
+         pos = (pos + s->packet_size - 1 - s->data_offset) /
+               s->packet_size * s->packet_size +
+               s->data_offset;
+     *ppos = pos;
 -    avio_seek(s->pb, pos, SEEK_SET);
 +    if (avio_seek(s->pb, pos, SEEK_SET) < 0)
 +        return AV_NOPTS_VALUE;
  
      asf_reset_header(s);
-     for(;;){
-         if (av_read_frame(s, pkt) < 0){
+     for (;;) {
 -        if (asf_read_packet(s, pkt) < 0) {
++        if (av_read_frame(s, pkt) < 0) {
              av_log(s, AV_LOG_INFO, "asf_read_pts failed\n");
              return AV_NOPTS_VALUE;
          }
  static void asf_build_simple_index(AVFormatContext *s, int stream_index)
  {
      ff_asf_guid g;
-     ASFContext *asf = s->priv_data;
-     int64_t current_pos= avio_tell(s->pb);
+     ASFContext *asf     = s->priv_data;
+     int64_t current_pos = avio_tell(s->pb);
 -    int i;
  
 -    avio_seek(s->pb, asf->data_object_offset + asf->data_object_size, SEEK_SET);
 +    if(avio_seek(s->pb, asf->data_object_offset + asf->data_object_size, SEEK_SET) < 0) {
 +        asf->index_read= -1;
 +        return;
 +    }
 +
      ff_get_guid(s->pb, &g);
  
      /* the data object can be followed by other top-level objects,
-        skip them until the simple index object is reached */
+      * skip them until the simple index object is reached */
 -    while (ff_guidcmp(&g, &index_guid)) {
 +    while (ff_guidcmp(&g, &ff_asf_simple_index_header)) {
-         int64_t gsize= avio_rl64(s->pb);
+         int64_t gsize = avio_rl64(s->pb);
 -        if (gsize < 24 || s->pb->eof_reached) {
 +        if (gsize < 24 || url_feof(s->pb)) {
              avio_seek(s->pb, current_pos, SEEK_SET);
 +            asf->index_read= -1;
              return;
          }
-         avio_skip(s->pb, gsize-24);
+         avio_skip(s->pb, gsize - 24);
          ff_get_guid(s->pb, &g);
      }
  
      {
-         int64_t itime, last_pos=-1;
+         int64_t itime, last_pos = -1;
          int pct, ict;
-         int64_t av_unused gsize= avio_rl64(s->pb);
 +        int i;
+         int64_t av_unused gsize = avio_rl64(s->pb);
          ff_get_guid(s->pb, &g);
-         itime=avio_rl64(s->pb);
-         pct=avio_rl32(s->pb);
-         ict=avio_rl32(s->pb);
-         av_log(s, AV_LOG_DEBUG, "itime:0x%"PRIx64", pct:%d, ict:%d\n",itime,pct,ict);
-         for (i=0;i<ict;i++){
-             int pktnum=avio_rl32(s->pb);
-             int pktct =avio_rl16(s->pb);
-             int64_t pos      = s->data_offset + s->packet_size*(int64_t)pktnum;
-             int64_t index_pts= FFMAX(av_rescale(itime, i, 10000) - asf->hdr.preroll, 0);
-             if(pos != last_pos){
-             av_log(s, AV_LOG_DEBUG, "pktnum:%d, pktct:%d  pts: %"PRId64"\n", pktnum, pktct, index_pts);
-             av_add_index_entry(s->streams[stream_index], pos, index_pts, s->packet_size, 0, AVINDEX_KEYFRAME);
-             last_pos=pos;
+         itime = avio_rl64(s->pb);
+         pct   = avio_rl32(s->pb);
+         ict   = avio_rl32(s->pb);
+         av_log(s, AV_LOG_DEBUG,
+                "itime:0x%"PRIx64", pct:%d, ict:%d\n", itime, pct, ict);
+         for (i = 0; i < ict; i++) {
+             int pktnum        = avio_rl32(s->pb);
+             int pktct         = avio_rl16(s->pb);
+             int64_t pos       = s->data_offset + s->packet_size * (int64_t)pktnum;
+             int64_t index_pts = FFMAX(av_rescale(itime, i, 10000) - asf->hdr.preroll, 0);
+             if (pos != last_pos) {
+                 av_log(s, AV_LOG_DEBUG, "pktnum:%d, pktct:%d  pts: %"PRId64"\n",
+                        pktnum, pktct, index_pts);
+                 av_add_index_entry(s->streams[stream_index], pos, index_pts,
+                                    s->packet_size, 0, AVINDEX_KEYFRAME);
+                 last_pos = pos;
              }
          }
-         asf->index_read= ict > 1;
 -        asf->index_read = ict > 0;
++        asf->index_read = ict > 1;
      }
      avio_seek(s->pb, current_pos, SEEK_SET);
  }
  
- static int asf_read_seek(AVFormatContext *s, int stream_index, int64_t pts, int flags)
+ static int asf_read_seek(AVFormatContext *s, int stream_index,
+                          int64_t pts, int flags)
  {
      ASFContext *asf = s->priv_data;
-     AVStream *st = s->streams[stream_index];
+     AVStream *st    = s->streams[stream_index];
 -    int64_t pos;
 -    int index;
  
      if (s->packet_size <= 0)
          return -1;
      if (!asf->index_read)
          asf_build_simple_index(s, stream_index);
  
-     if((asf->index_read > 0 && st->index_entries)){
-         int index= av_index_search_timestamp(st, pts, flags);
-         if(index >= 0) {
 -    if ((asf->index_read && st->index_entries)) {
 -        index = av_index_search_timestamp(st, pts, flags);
++    if ((asf->index_read > 0 && st->index_entries)) {
++        int index = av_index_search_timestamp(st, pts, flags);
+         if (index >= 0) {
              /* find the position */
 -            pos = st->index_entries[index].pos;
 +            uint64_t pos = st->index_entries[index].pos;
  
              /* do the seek */
              av_log(s, AV_LOG_DEBUG, "SEEKTO: %"PRId64"\n", pos);
   * Lesser General Public License for more details.
   *
   * You should have received a copy of the GNU Lesser General Public
 - * License along with Libav; if not, write to the Free Software
 + * License along with FFmpeg; if not, write to the Free Software
   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
   */
+ #include "libavutil/dict.h"
  #include "avformat.h"
+ #include "avio_internal.h"
  #include "internal.h"
  #include "riff.h"
  #include "asf.h"
  
  
  #define ASF_INDEXED_INTERVAL    10000000
 -#define ASF_INDEX_BLOCK         600
 +#define ASF_INDEX_BLOCK         (1<<9)
  
  #define ASF_PACKET_ERROR_CORRECTION_DATA_SIZE 0x2
- #define ASF_PACKET_ERROR_CORRECTION_FLAGS (\
-                 ASF_PACKET_FLAG_ERROR_CORRECTION_PRESENT | \
-                 ASF_PACKET_ERROR_CORRECTION_DATA_SIZE\
-                 )
+ #define ASF_PACKET_ERROR_CORRECTION_FLAGS          \
+     (ASF_PACKET_FLAG_ERROR_CORRECTION_PRESENT |    \
+      ASF_PACKET_ERROR_CORRECTION_DATA_SIZE)
  
  #if (ASF_PACKET_ERROR_CORRECTION_FLAGS != 0)
  #   define ASF_PACKET_ERROR_CORRECTION_FLAGS_FIELD_SIZE 1
@@@ -208,20 -200,18 +200,20 @@@ typedef struct 
      /* only for reading */
      uint64_t data_offset;                ///< beginning of the first data packet
  
-     ASFIndex* index_ptr;
 -    int64_t last_indexed_pts;
+     ASFIndex *index_ptr;
 -    uint32_t nb_index_count;
      uint32_t nb_index_memory_alloc;
      uint16_t maximum_packet;
 +    uint32_t next_packet_number;
 +    uint16_t next_packet_count;
 +    int      next_start_sec;
 +    int      end_sec;
  } ASFContext;
  
  static const AVCodecTag codec_asf_bmp_tags[] = {
-     { AV_CODEC_ID_MPEG4, MKTAG('M', '4', 'S', '2') },
-     { AV_CODEC_ID_MPEG4, MKTAG('M', 'P', '4', 'S') },
 -    { AV_CODEC_ID_MPEG4,     MKTAG('M', 'P', '4', 'S') },
+     { AV_CODEC_ID_MPEG4,     MKTAG('M', '4', 'S', '2') },
++    { AV_CODEC_ID_MPEG4,     MKTAG('M', 'P', '4', 'S') },
      { AV_CODEC_ID_MSMPEG4V3, MKTAG('M', 'P', '4', '3') },
-     { AV_CODEC_ID_NONE, 0 },
+     { AV_CODEC_ID_NONE,      0 },
  };
  
  #define PREROLL_TIME 3100
@@@ -341,8 -333,8 +335,8 @@@ static int asf_write_header1(AVFormatCo
  
      /* file header */
      header_offset = avio_tell(pb);
-     hpos = put_header(pb, &ff_asf_file_header);
+     hpos          = put_header(pb, &ff_asf_file_header);
 -    put_guid(pb, &ff_asf_my_guid);
 +    ff_put_guid(pb, &ff_asf_my_guid);
      avio_wl64(pb, file_size);
      file_time = 0;
      avio_wl64(pb, unix_to_file_time(file_time));
          int64_t es_pos;
          //        ASFStream *stream = &asf->streams[n];
  
-         enc = s->streams[n]->codec;
+         enc                 = s->streams[n]->codec;
          asf->streams[n].num = n + 1;
 -        asf->streams[n].seq = 0;
 +        asf->streams[n].seq = 1;
  
-         switch(enc->codec_type) {
+         switch (enc->codec_type) {
          case AVMEDIA_TYPE_AUDIO:
              wav_extra_size = 0;
-             extra_size = 18 + wav_extra_size;
-             extra_size2 = 8;
+             extra_size     = 18 + wav_extra_size;
+             extra_size2    = 8;
              break;
          default:
          case AVMEDIA_TYPE_VIDEO:
      /* media comments */
  
      hpos = put_header(pb, &ff_asf_codec_comment_header);
 -    put_guid(pb, &ff_asf_codec_comment1_header);
 +    ff_put_guid(pb, &ff_asf_codec_comment1_header);
      avio_wl32(pb, s->nb_streams);
-     for(n=0;n<s->nb_streams;n++) {
+     for (n = 0; n < s->nb_streams; n++) {
          AVCodec *p;
          const char *desc;
          int len;
@@@ -559,9 -549,11 +551,9 @@@ static int asf_write_header(AVFormatCon
      s->packet_size  = PACKET_SIZE;
      asf->nb_packets = 0;
  
-     asf->index_ptr = av_malloc( sizeof(ASFIndex) * ASF_INDEX_BLOCK );
 -    asf->last_indexed_pts      = 0;
+     asf->index_ptr             = av_malloc(sizeof(ASFIndex) * ASF_INDEX_BLOCK);
      asf->nb_index_memory_alloc = ASF_INDEX_BLOCK;
-     asf->maximum_packet = 0;
 -    asf->nb_index_count        = 0;
+     asf->maximum_packet        = 0;
  
      /* the data-chunk-size has to be 50, which is data_size - asf->data_offset
       *  at the moment this function is done. It is needed to use asf as
  
      avio_flush(s->pb);
  
-     asf->packet_nb_payloads = 0;
+     asf->packet_nb_payloads     = 0;
      asf->packet_timestamp_start = -1;
-     asf->packet_timestamp_end = -1;
+     asf->packet_timestamp_end   = -1;
      ffio_init_context(&asf->pb, asf->packet_buf, s->packet_size, 1,
-                   NULL, NULL, NULL, NULL);
+                       NULL, NULL, NULL, NULL);
  
 +    if (s->avoid_negative_ts < 0)
 +        s->avoid_negative_ts = 1;
 +
      return 0;
  }
  
@@@ -654,17 -638,15 +641,14 @@@ static void flush_packet(AVFormatContex
  
      assert(asf->packet_timestamp_end >= asf->packet_timestamp_start);
  
-     if (asf->is_streamed) {
+     if (asf->is_streamed)
          put_chunk(s, 0x4424, s->packet_size, 0);
-     }
  
-     packet_hdr_size = put_payload_parsing_info(
-                             s,
-                             asf->packet_timestamp_start,
-                             asf->packet_timestamp_end - asf->packet_timestamp_start,
-                             asf->packet_nb_payloads,
-                             asf->packet_size_left
-                         );
+     packet_hdr_size = put_payload_parsing_info(s,
+                                                asf->packet_timestamp_start,
 -                                               asf->packet_timestamp_end -
 -                                               asf->packet_timestamp_start,
++                                               asf->packet_timestamp_end - asf->packet_timestamp_start,
+                                                asf->packet_nb_payloads,
+                                                asf->packet_size_left);
  
      packet_filled_size = PACKET_SIZE - asf->packet_size_left;
      assert(packet_hdr_size <= asf->packet_size_left);
  
      avio_flush(s->pb);
      asf->nb_packets++;
-     asf->packet_nb_payloads = 0;
+     asf->packet_nb_payloads     = 0;
      asf->packet_timestamp_start = -1;
-     asf->packet_timestamp_end = -1;
+     asf->packet_timestamp_end   = -1;
      ffio_init_context(&asf->pb, asf->packet_buf, s->packet_size, 1,
-                   NULL, NULL, NULL, NULL);
+                       NULL, NULL, NULL, NULL);
  }
  
- static void put_payload_header(
-                                 AVFormatContext *s,
-                                 ASFStream       *stream,
-                                 int64_t         presentation_time,
-                                 int             m_obj_size,
-                                 int             m_obj_offset,
-                                 int             payload_len,
-                                 int             flags
-             )
+ static void put_payload_header(AVFormatContext *s, ASFStream *stream,
 -                               int presentation_time, int m_obj_size,
++                               int64_t presentation_time, int m_obj_size,
+                                int m_obj_offset, int payload_len, int flags)
  {
      ASFContext *asf = s->priv_data;
      AVIOContext *pb = &asf->pb;
      // Presentation Time for the media object that the payload belongs to.
      avio_w8(pb, ASF_PAYLOAD_REPLICATED_DATA_LENGTH);
  
-     avio_wl32(pb, m_obj_size);       //Replicated Data - Media Object Size
-     avio_wl32(pb, (uint32_t) presentation_time);//Replicated Data - Presentation Time
+     avio_wl32(pb, m_obj_size);        // Replicated Data - Media Object Size
 -    avio_wl32(pb, presentation_time); // Replicated Data - Presentation Time
++    avio_wl32(pb, (uint32_t) presentation_time); // Replicated Data - Presentation Time
  
-     if (asf->multi_payloads_present){
-         avio_wl16(pb, payload_len);   //payload length
+     if (asf->multi_payloads_present) {
+         avio_wl16(pb, payload_len);   // payload length
      }
  }
  
- static void put_frame(
-                     AVFormatContext *s,
-                     ASFStream       *stream,
-                     AVStream        *avst,
-                     int64_t         timestamp,
-                     const uint8_t   *buf,
-                     int             m_obj_size,
-                     int             flags
-                 )
+ static void put_frame(AVFormatContext *s, ASFStream *stream, AVStream *avst,
 -                      int timestamp, const uint8_t *buf,
++                      int64_t timestamp, const uint8_t *buf,
+                       int m_obj_size, int flags)
  {
      ASFContext *asf = s->priv_data;
      int m_obj_offset, payload_len, frag_len1;
@@@ -817,36 -761,48 +791,37 @@@ static int asf_write_packet(AVFormatCon
  {
      ASFContext *asf = s->priv_data;
      ASFStream *stream;
 -    int64_t duration;
      AVCodecContext *codec;
 -    int64_t packet_st, pts;
 -    int start_sec, i;
 +    uint32_t packet_number;
 +    int64_t pts;
 +    int start_sec;
-     int flags= pkt->flags;
+     int flags = pkt->flags;
  
-     codec = s->streams[pkt->stream_index]->codec;
+     codec  = s->streams[pkt->stream_index]->codec;
      stream = &asf->streams[pkt->stream_index];
  
-     if(codec->codec_type == AVMEDIA_TYPE_AUDIO)
+     if (codec->codec_type == AVMEDIA_TYPE_AUDIO)
          flags &= ~AV_PKT_FLAG_KEY;
  
      pts = (pkt->pts != AV_NOPTS_VALUE) ? pkt->pts : pkt->dts;
      assert(pts != AV_NOPTS_VALUE);
 -    duration      = pts * 10000;
 -    asf->duration = FFMAX(asf->duration, duration + pkt->duration * 10000);
 +    pts *= 10000;
-     asf->duration= FFMAX(asf->duration, pts + pkt->duration * 10000);
++    asf->duration = FFMAX(asf->duration, pts + pkt->duration * 10000);
  
 -    packet_st = asf->nb_packets;
 +    packet_number = asf->nb_packets;
-     put_frame(s, stream, s->streams[pkt->stream_index], pkt->dts, pkt->data, pkt->size, flags);
+     put_frame(s, stream, s->streams[pkt->stream_index],
+               pkt->dts, pkt->data, pkt->size, flags);
  
 +    start_sec = (int)((PREROLL_TIME * 10000 + pts + ASF_INDEXED_INTERVAL - 1)
 +              / ASF_INDEXED_INTERVAL);
 +
      /* check index */
      if ((!asf->is_streamed) && (flags & AV_PKT_FLAG_KEY)) {
 -        start_sec = (int)(duration / INT64_C(10000000));
 -        if (start_sec != (int)(asf->last_indexed_pts / INT64_C(10000000))) {
 -            for (i = asf->nb_index_count; i < start_sec; i++) {
 -                if (i >= asf->nb_index_memory_alloc) {
 -                    asf->nb_index_memory_alloc += ASF_INDEX_BLOCK;
 -                    asf->index_ptr              = (ASFIndex *)av_realloc(asf->index_ptr,
 -                                                                         sizeof(ASFIndex) *
 -                                                                         asf->nb_index_memory_alloc);
 -                }
 -                // store
 -                asf->index_ptr[i].packet_number = (uint32_t)packet_st;
 -                asf->index_ptr[i].packet_count  = (uint16_t)(asf->nb_packets - packet_st);
 -                asf->maximum_packet             = FFMAX(asf->maximum_packet,
 -                                                        (uint16_t)(asf->nb_packets - packet_st));
 -            }
 -            asf->nb_index_count   = start_sec;
 -            asf->last_indexed_pts = duration;
 -        }
 +        uint16_t packet_count = asf->nb_packets - packet_number;
 +        update_index(s, start_sec, packet_number, packet_count);
      }
 +    asf->end_sec = start_sec;
 +
      return 0;
  }
  
@@@ -856,9 -812,9 +831,9 @@@ static int asf_write_index(AVFormatCont
      AVIOContext *pb = s->pb;
      int i;
  
 -    put_guid(pb, &ff_asf_simple_index_header);
 +    ff_put_guid(pb, &ff_asf_simple_index_header);
-     avio_wl64(pb, 24 + 16 + 8 + 4 + 4 + (4 + 2)*count);
+     avio_wl64(pb, 24 + 16 + 8 + 4 + 4 + (4 + 2) * count);
 -    put_guid(pb, &ff_asf_my_guid);
 +    ff_put_guid(pb, &ff_asf_my_guid);
      avio_wl64(pb, ASF_INDEXED_INTERVAL);
      avio_wl32(pb, max);
      avio_wl32(pb, count);