[FFmpeg-cvslog] asfenc: properly write index information

Ramiro Polla git at videolan.org
Wed Sep 12 16:50:38 CEST 2012


ffmpeg | branch: release/0.10 | Ramiro Polla <ramiro.polla at gmail.com> | Wed Apr  4 02:52:27 2012 -0300| [a1f678f7ca48d2dd9614e30f1d9c58bfaac07bda] | committer: Michael Niedermayer

asfenc: properly write index information

The index must take into account the pre-roll time and must seek backwards,
not forwards.

Signed-off-by: Michael Niedermayer <michaelni at gmx.at>
(cherry picked from commit bd603494f905a7db92fc04eab9c0f6793b0ed7d1)

Conflicts:

	tests/ref/lavf/asf
	tests/ref/seek/lavf_asf

Fixes Ticket1563

Signed-off-by: Michael Niedermayer <michaelni at gmx.at>

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=a1f678f7ca48d2dd9614e30f1d9c58bfaac07bda
---

 libavformat/asfenc.c    |   64 +++++++++++++++++++++++++++++++----------------
 tests/ref/lavf/asf      |    4 +--
 tests/ref/seek/lavf_asf |   14 +++++------
 3 files changed, 51 insertions(+), 31 deletions(-)

diff --git a/libavformat/asfenc.c b/libavformat/asfenc.c
index fd4ca53..322dc61 100644
--- a/libavformat/asfenc.c
+++ b/libavformat/asfenc.c
@@ -208,11 +208,13 @@ typedef struct {
     /* only for reading */
     uint64_t data_offset;                ///< beginning of the first data packet
 
-    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[] = {
@@ -557,10 +559,8 @@ static int asf_write_header(AVFormatContext *s)
     s->packet_size  = PACKET_SIZE;
     asf->nb_packets = 0;
 
-    asf->last_indexed_pts = 0;
     asf->index_ptr = av_malloc( sizeof(ASFIndex) * ASF_INDEX_BLOCK );
     asf->nb_index_memory_alloc = ASF_INDEX_BLOCK;
-    asf->nb_index_count = 0;
     asf->maximum_packet = 0;
 
     /* the data-chunk-size has to be 50, which is data_size - asf->data_offset
@@ -782,6 +782,34 @@ static void put_frame(
     stream->seq++;
 }
 
+static void update_index(AVFormatContext *s, int start_sec,
+                         uint32_t packet_number, uint16_t packet_count)
+{
+    ASFContext *asf = s->priv_data;
+
+    if (start_sec > asf->next_start_sec) {
+        int i;
+
+        if (!asf->next_start_sec) {
+            asf->next_packet_number = packet_number;
+            asf->next_packet_count  = packet_count;
+        }
+
+        if (start_sec > asf->nb_index_memory_alloc) {
+            asf->nb_index_memory_alloc = (start_sec + ASF_INDEX_BLOCK) & ~(ASF_INDEX_BLOCK - 1);
+            asf->index_ptr = av_realloc( asf->index_ptr, sizeof(ASFIndex) * asf->nb_index_memory_alloc );
+        }
+        for (i = asf->next_start_sec; i < start_sec; i++) {
+            asf->index_ptr[i].packet_number = asf->next_packet_number;
+            asf->index_ptr[i].packet_count  = asf->next_packet_count;
+        }
+    }
+    asf->maximum_packet     = FFMAX(asf->maximum_packet, packet_count);
+    asf->next_packet_number = packet_number;
+    asf->next_packet_count  = packet_count;
+    asf->next_start_sec     = start_sec;
+}
+
 static int asf_write_packet(AVFormatContext *s, AVPacket *pkt)
 {
     ASFContext *asf = s->priv_data;
@@ -789,7 +817,7 @@ static int asf_write_packet(AVFormatContext *s, AVPacket *pkt)
     AVCodecContext *codec;
     uint32_t packet_number;
     int64_t pts;
-    int start_sec,i;
+    int start_sec;
     int flags= pkt->flags;
 
     codec = s->streams[pkt->stream_index]->codec;
@@ -806,25 +834,16 @@ static int asf_write_packet(AVFormatContext *s, AVPacket *pkt)
     packet_number = asf->nb_packets;
     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)) {
         uint16_t packet_count = asf->nb_packets - packet_number;
-        start_sec = (int)(pts / INT64_C(10000000));
-        if (start_sec != (int)(asf->last_indexed_pts / INT64_C(10000000))) {
-            if (start_sec > asf->nb_index_memory_alloc) {
-                asf->nb_index_memory_alloc = (start_sec + ASF_INDEX_BLOCK) & ~(ASF_INDEX_BLOCK - 1);
-                asf->index_ptr = av_realloc( asf->index_ptr, sizeof(ASFIndex) * asf->nb_index_memory_alloc );
-            }
-            for(i=asf->nb_index_count;i<start_sec;i++) {
-                // store
-                asf->index_ptr[i].packet_number = packet_number;
-                asf->index_ptr[i].packet_count  = packet_count;
-                asf->maximum_packet = FFMAX(asf->maximum_packet, packet_count);
-            }
-            asf->nb_index_count = start_sec;
-            asf->last_indexed_pts = pts;
-        }
+        update_index(s, start_sec, packet_number, packet_count);
     }
+    asf->end_sec = start_sec;
+
     return 0;
 }
 
@@ -859,8 +878,9 @@ static int asf_write_trailer(AVFormatContext *s)
 
     /* write index */
     data_size = avio_tell(s->pb);
-    if ((!asf->is_streamed) && (asf->nb_index_count != 0)) {
-        asf_write_index(s, asf->index_ptr, asf->maximum_packet, asf->nb_index_count);
+    if (!asf->is_streamed && asf->next_start_sec) {
+        update_index(s, asf->end_sec + 1, 0, 0);
+        asf_write_index(s, asf->index_ptr, asf->maximum_packet, asf->next_start_sec);
     }
     avio_flush(s->pb);
 
diff --git a/tests/ref/lavf/asf b/tests/ref/lavf/asf
index c6e6b6b..ee819fa 100644
--- a/tests/ref/lavf/asf
+++ b/tests/ref/lavf/asf
@@ -1,3 +1,3 @@
-3d410176ebf9ffdf99d2738922cef260 *./tests/data/lavf/lavf.asf
-333489 ./tests/data/lavf/lavf.asf
+cee474c51df8a3e67d01b733cafbb7e8 *./tests/data/lavf/lavf.asf
+333581 ./tests/data/lavf/lavf.asf
 ./tests/data/lavf/lavf.asf CRC=0x9f5ab3e6
diff --git a/tests/ref/seek/lavf_asf b/tests/ref/seek/lavf_asf
index 5aee39e..868262c 100644
--- a/tests/ref/seek/lavf_asf
+++ b/tests/ref/seek/lavf_asf
@@ -2,9 +2,9 @@ ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:    689 size: 28487
 ret: 0         st:-1 flags:0  ts:-1.000000
 ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:    689 size: 28487
 ret: 0         st:-1 flags:1  ts: 1.894167
-ret: 0         st: 1 flags:1 dts: 0.940000 pts: 0.940000 pos: 301489 size:   209
+ret: 0         st: 1 flags:1 dts: 0.444000 pts: 0.444000 pos: 147889 size:   209
 ret: 0         st: 0 flags:0  ts: 0.788000
-ret: 0         st: 1 flags:1 dts: 0.940000 pts: 0.940000 pos: 301489 size:   209
+ret: 0         st: 1 flags:1 dts: 0.444000 pts: 0.444000 pos: 147889 size:   209
 ret: 0         st: 0 flags:1  ts:-0.317000
 ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:    689 size: 28487
 ret: 0         st: 1 flags:0  ts: 2.577000
@@ -22,11 +22,11 @@ ret: 0         st: 1 flags:1 dts: 0.940000 pts: 0.940000 pos: 301489 size:   209
 ret: 0         st: 1 flags:0  ts:-0.058000
 ret: 0         st: 1 flags:1 dts: 0.000000 pts: 0.000000 pos:  29489 size:   208
 ret: 0         st: 1 flags:1  ts: 2.836000
-ret: 0         st: 1 flags:1 dts: 0.967000 pts: 0.967000 pos: 330289 size:   209
+ret: 0         st: 1 flags:1 dts: 0.862000 pts: 0.862000 pos: 279089 size:   209
 ret: 0         st:-1 flags:0  ts: 1.730004
 ret: 0         st: 1 flags:1 dts: 0.940000 pts: 0.940000 pos: 301489 size:   209
 ret: 0         st:-1 flags:1  ts: 0.624171
-ret: 0         st: 1 flags:1 dts: 0.444000 pts: 0.444000 pos: 147889 size:   209
+ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:    689 size: 28487
 ret: 0         st: 0 flags:0  ts:-0.482000
 ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:    689 size: 28487
 ret: 0         st: 0 flags:1  ts: 2.413000
@@ -34,19 +34,19 @@ ret: 0         st: 1 flags:1 dts: 0.940000 pts: 0.940000 pos: 301489 size:   209
 ret: 0         st: 1 flags:0  ts: 1.307000
 ret: 0         st: 1 flags:1 dts: 0.967000 pts: 0.967000 pos: 330289 size:   209
 ret: 0         st: 1 flags:1  ts: 0.201000
-ret: 0         st: 1 flags:1 dts: 0.183000 pts: 0.183000 pos:  71089 size:   209
+ret: 0         st: 1 flags:1 dts: 0.967000 pts: 0.967000 pos: 330289 size:   209
 ret: 0         st:-1 flags:0  ts:-0.904994
 ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:    689 size: 28487
 ret: 0         st:-1 flags:1  ts: 1.989173
 ret: 0         st: 1 flags:1 dts: 0.940000 pts: 0.940000 pos: 301489 size:   209
 ret: 0         st: 0 flags:0  ts: 0.883000
-ret: 0         st: 1 flags:1 dts: 0.940000 pts: 0.940000 pos: 301489 size:   209
+ret: 0         st: 1 flags:1 dts: 0.444000 pts: 0.444000 pos: 147889 size:   209
 ret: 0         st: 0 flags:1  ts:-0.222000
 ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:    689 size: 28487
 ret: 0         st: 1 flags:0  ts: 2.672000
 ret: 0         st: 1 flags:1 dts: 0.967000 pts: 0.967000 pos: 330289 size:   209
 ret: 0         st: 1 flags:1  ts: 1.566000
-ret: 0         st: 1 flags:1 dts: 0.967000 pts: 0.967000 pos: 330289 size:   209
+ret: 0         st: 1 flags:1 dts: 0.862000 pts: 0.862000 pos: 279089 size:   209
 ret: 0         st:-1 flags:0  ts: 0.460008
 ret: 0         st: 1 flags:1 dts: 0.444000 pts: 0.444000 pos: 147889 size:   209
 ret: 0         st:-1 flags:1  ts:-0.645825



More information about the ffmpeg-cvslog mailing list