[FFmpeg-devel] [PATCH] replace ogg seek function by read_timestamp and generic seek

Reimar Döffinger Reimar.Doeffinger
Wed Oct 3 15:41:27 CEST 2007


Hello,
the ogg seek function is quite complex and doesn't work that well
either, attached patch would remove it and instead add a working
read_timestamp function so we will use our generic binary search seek.
Split in two patches of course ;-)
Slightly related to issue 159.

Greetings,
Reimar D?ffinger
-------------- next part --------------
>From a39730ee91f963bb924eba29de32a473878d8d92 Mon Sep 17 00:00:00 2001
From: =?utf-8?q?Reimar=20D=C3=B6ffinger?= <reimar at hokum.(none)>
Date: Wed, 3 Oct 2007 15:35:01 +0200
Subject: [PATCH] Add a read_timestamp function to ogg demuxer

---
 libavformat/ogg2.c |   25 ++++++++++++++++---------
 1 files changed, 16 insertions(+), 9 deletions(-)

diff --git a/libavformat/ogg2.c b/libavformat/ogg2.c
index 2b4b160..4d73bd4 100644
--- a/libavformat/ogg2.c
+++ b/libavformat/ogg2.c
@@ -659,21 +659,28 @@ ogg_read_seek (AVFormatContext * s, int stream_index, int64_t target_ts,
 
 }
 
-#if 0
 static int64_t
 ogg_read_timestamp (AVFormatContext * s, int stream_index, int64_t * pos_arg,
                     int64_t pos_limit)
 {
     ogg_t *ogg = s->priv_data;
     ByteIOContext *bc = &s->pb;
-    int64_t pos, pts;
-
-    if (*pos_arg < 0)
-        return AV_NOPTS_VALUE;
-
-    pos = *pos_arg;
+    int64_t pts = AV_NOPTS_VALUE;
+    int i;
+    url_fseek(bc, *pos_arg, SEEK_SET);
+    while (url_ftell(bc) < pos_limit && !ogg_read_page (s, &i)) {
+        if (ogg->streams[i].granule != -1 && ogg->streams[i].granule != 0 &&
+            ogg->streams[i].codec && i == stream_index) {
+            pts = ogg_gptopts(s, i, ogg->streams[i].granule);
+            // FIXME: this is the position of the packet after the one with above
+            // pts.
+            *pos_arg = url_ftell(bc);
+            break;
+        }
+    }
+    ogg_reset(ogg);
+    return pts;
 }
-#endif
 
 static int ogg_probe(AVProbeData *p)
 {
@@ -694,6 +701,6 @@ AVInputFormat ogg_demuxer = {
     ogg_read_packet,
     ogg_read_close,
     ogg_read_seek,
-// ogg_read_timestamp,
+    ogg_read_timestamp,
     .extensions = "ogg",
 };
-- 
1.5.3.3

-------------- next part --------------
>From 3d5f37f9bec961e9ad1d76221497dfe852672ff3 Mon Sep 17 00:00:00 2001
From: =?utf-8?q?Reimar=20D=C3=B6ffinger?= <reimar at hokum.(none)>
Date: Wed, 3 Oct 2007 15:37:30 +0200
Subject: [PATCH] Remove ogg seek function, instead use generic seek via read_timestamp function

---
 libavformat/ogg2.c |   85 +---------------------------------------------------
 1 files changed, 1 insertions(+), 84 deletions(-)

diff --git a/libavformat/ogg2.c b/libavformat/ogg2.c
index 4d73bd4..ebc7567 100644
--- a/libavformat/ogg2.c
+++ b/libavformat/ogg2.c
@@ -576,89 +576,6 @@ ogg_read_close (AVFormatContext * s)
 }
 
 
-static int
-ogg_read_seek (AVFormatContext * s, int stream_index, int64_t target_ts,
-               int flags)
-{
-    AVStream *st = s->streams[stream_index];
-    ogg_t *ogg = s->priv_data;
-    ByteIOContext *bc = &s->pb;
-    uint64_t min = 0, max = ogg->size;
-    uint64_t tmin = st->start_time, tmax = st->start_time + st->duration;
-    int64_t pts = AV_NOPTS_VALUE;
-
-    ogg_save (s);
-
-    if ((uint64_t)target_ts < tmin || target_ts < 0)
-        target_ts = tmin;
-    while (min <= max && tmin < tmax){
-        uint64_t p = min + (max - min) * (target_ts - tmin) / (tmax - tmin);
-        int i = -1;
-
-        url_fseek (bc, p, SEEK_SET);
-
-        while (!ogg_read_page (s, &i)){
-            if (i == stream_index && ogg->streams[i].granule != 0 &&
-                ogg->streams[i].granule != -1)
-                break;
-        }
-
-        if (i == -1)
-            break;
-
-        pts = ogg_gptopts (s, i, ogg->streams[i].granule);
-        p = url_ftell (bc);
-
-        if (FFABS (pts - target_ts) * st->time_base.num < st->time_base.den)
-            break;
-
-        if (pts > target_ts){
-            if (max == p && tmax == pts) {
-                // probably our tmin is wrong, causing us to always end up too late in the file
-                tmin = (target_ts + tmin + 1) / 2;
-                if (tmin == target_ts) {
-                    url_fseek(bc, min, SEEK_SET);
-                    break;
-                }
-            }
-            max = p;
-            tmax = pts;
-        }else{
-            if (min == p && tmin == pts) {
-                // probably our tmax is wrong, causing us to always end up too early in the file
-                tmax = (target_ts + tmax) / 2;
-                if (tmax == target_ts) {
-                    url_fseek(bc, max, SEEK_SET);
-                    break;
-                }
-            }
-            min = p;
-            tmin = pts;
-        }
-    }
-
-    if (FFABS (pts - target_ts) * st->time_base.num < st->time_base.den){
-        ogg_restore (s, 1);
-        ogg_reset (ogg);
-    }else{
-        ogg_restore (s, 0);
-        pts = AV_NOPTS_VALUE;
-    }
-
-    av_update_cur_dts(s, st, pts);
-    return 0;
-
-#if 0
-    //later...
-    int64_t pos;
-    if (av_seek_frame_binary (s, stream_index, target_ts, flags) < 0)
-        return -1;
-    pos = url_ftell (&s->pb);
-    ogg_read_timestamp (s, stream_index, &pos, pos - 1);
-#endif
-
-}
-
 static int64_t
 ogg_read_timestamp (AVFormatContext * s, int stream_index, int64_t * pos_arg,
                     int64_t pos_limit)
@@ -700,7 +617,7 @@ AVInputFormat ogg_demuxer = {
     ogg_read_header,
     ogg_read_packet,
     ogg_read_close,
-    ogg_read_seek,
+    NULL,
     ogg_read_timestamp,
     .extensions = "ogg",
 };
-- 
1.5.3.3




More information about the ffmpeg-devel mailing list