[FFmpeg-devel] [PATCH] avformat/concatdec: don't call open_file when seek position within a file

raymond raymondzheng1412 at gmail.com
Tue Sep 20 07:31:16 EEST 2016


---
 libavformat/concatdec.c | 27 ++++++++++++++++++++-------
 1 file changed, 20 insertions(+), 7 deletions(-)

diff --git a/libavformat/concatdec.c b/libavformat/concatdec.c
index b3a430e..1cd9ec1 100644
--- a/libavformat/concatdec.c
+++ b/libavformat/concatdec.c
@@ -64,6 +64,8 @@ typedef struct {
     ConcatMatchMode stream_match_mode;
     unsigned auto_convert;
     int segment_time_metadata;
+    int cur_fileno;
+    AVFormatContext *cur_avf_saved;
 } ConcatContext;
 
 static int concat_probe(AVProbeData *probe)
@@ -333,6 +335,8 @@ static int open_file(AVFormatContext *avf, unsigned fileno)
         avformat_close_input(&cat->avf);
         return ret;
     }
+
+    cat->cur_fileno = fileno;
     cat->cur_file = file;
     if (file->start_time == AV_NOPTS_VALUE)
         file->start_time = !fileno ? 0 :
@@ -711,13 +715,18 @@ static int real_seek(AVFormatContext *avf, int stream,
             left  = mid;
     }
 
-    if ((ret = open_file(avf, left)) < 0)
-        return ret;
+    if (cat->cur_fileno != left) {
+        if ((ret = open_file(avf, left)) < 0)
+            return ret;
+    } else {
+        cat->avf = cat->cur_avf_saved;
+    }
 
     ret = try_seek(avf, stream, min_ts, ts, max_ts, flags);
     if (ret < 0 &&
         left < cat->nb_files - 1 &&
         cat->files[left + 1].start_time < max_ts) {
+        cat->avf = NULL;
         if ((ret = open_file(avf, left + 1)) < 0)
             return ret;
         ret = try_seek(avf, stream, min_ts, ts, max_ts, flags);
@@ -730,7 +739,7 @@ static int concat_seek(AVFormatContext *avf, int stream,
 {
     ConcatContext *cat = avf->priv_data;
     ConcatFile *cur_file_saved = cat->cur_file;
-    AVFormatContext *cur_avf_saved = cat->avf;
+    cat->cur_avf_saved = cat->avf;
     int ret;
 
     if (!cat->seekable)
@@ -739,12 +748,16 @@ static int concat_seek(AVFormatContext *avf, int stream,
         return AVERROR(ENOSYS);
     cat->avf = NULL;
     if ((ret = real_seek(avf, stream, min_ts, ts, max_ts, flags)) < 0) {
-        if (cat->avf)
-            avformat_close_input(&cat->avf);
-        cat->avf      = cur_avf_saved;
+        if (cat->cur_file != cur_file_saved) {
+            if (cat->avf)
+                avformat_close_input(&cat->avf);
+        }
+        cat->avf      = cat->cur_avf_saved;
         cat->cur_file = cur_file_saved;
     } else {
-        avformat_close_input(&cur_avf_saved);
+        if (cat->cur_file != cur_file_saved) {
+            avformat_close_input(&cat->cur_avf_saved);
+        }
         cat->eof = 0;
     }
     return ret;
-- 
2.7.4



More information about the ffmpeg-devel mailing list