[FFmpeg-devel] [PATCH] flac demuxer: improve seeking

Rainer Hochecker fernetmenta at online.de
Sun Apr 13 06:16:08 CEST 2014


---
 libavcodec/flac_parser.c |  9 +++++++++
 libavformat/flacdec.c    | 27 +++++++++++++++++++++++++++
 2 files changed, 36 insertions(+)

diff --git a/libavcodec/flac_parser.c b/libavcodec/flac_parser.c
index ba1f060..259ff44 100644
--- a/libavcodec/flac_parser.c
+++ b/libavcodec/flac_parser.c
@@ -489,6 +489,12 @@ static int get_best_header(FLACParseContext* fpc, const uint8_t **poutbuf,
                                         &fpc->wrap_buf,
                                         &fpc->wrap_buf_allocated_size);
 
+
+    if (header->fi.is_var_size)
+        fpc->pc->pts = header->fi.frame_or_sample_num;
+    else if(fpc->last_fi_valid && fpc->last_fi.blocksize == header->fi.blocksize)
+        fpc->pc->pts = header->fi.frame_or_sample_num * header->fi.blocksize;
+
     fpc->best_header_valid = 0;
     fpc->last_fi_valid = 1;
     fpc->last_fi = header->fi;
@@ -516,6 +522,9 @@ static int flac_parse(AVCodecParserContext *s, AVCodecContext *avctx,
             s->duration = fi.blocksize;
             if (!avctx->sample_rate)
                 avctx->sample_rate = fi.samplerate;
+            fpc->pc->pts = fi.frame_or_sample_num;
+            if (!fi.is_var_size)
+                fpc->pc->pts *= fi.blocksize;
         }
         *poutbuf      = buf;
         *poutbuf_size = buf_size;
diff --git a/libavformat/flacdec.c b/libavformat/flacdec.c
index bbbbf66..55bbd30 100644
--- a/libavformat/flacdec.c
+++ b/libavformat/flacdec.c
@@ -162,12 +162,39 @@ static int flac_probe(AVProbeData *p)
     return AVPROBE_SCORE_EXTENSION;
 }
 
+static av_unused int64_t flac_read_timestamp(AVFormatContext *s, int stream_index,
+                                             int64_t *ppos, int64_t pos_limit)
+{
+    AVPacket pkt;
+
+    ff_read_frame_flush(s);
+    if (avio_seek(s->pb, *ppos, SEEK_SET) < 0)
+        return AV_NOPTS_VALUE;
+
+    av_init_packet(&pkt);
+
+    while(pkt.pts == AV_NOPTS_VALUE && pkt.pts <= pos_limit){
+        if (av_read_frame(s, &pkt) < 0)
+            return AV_NOPTS_VALUE;
+
+        av_free_packet(&pkt);
+        if(pkt.pts != AV_NOPTS_VALUE && pkt.pos >= 0){
+            if(pkt.stream_index == stream_index && pkt.pos <= pos_limit){
+                *ppos= pkt.pos;
+                return pkt.pts;
+            }
+        }
+    }
+    return AV_NOPTS_VALUE;
+}
+
 AVInputFormat ff_flac_demuxer = {
     .name           = "flac",
     .long_name      = NULL_IF_CONFIG_SMALL("raw FLAC"),
     .read_probe     = flac_probe,
     .read_header    = flac_read_header,
     .read_packet    = ff_raw_read_partial_packet,
+    .read_timestamp = flac_read_timestamp,
     .flags          = AVFMT_GENERIC_INDEX,
     .extensions     = "flac",
     .raw_codec_id   = AV_CODEC_ID_FLAC,
-- 
1.9.1



More information about the ffmpeg-devel mailing list