From abde188ef14fbc490e467d22acd4d08136d2f15b Mon Sep 17 00:00:00 2001
From: "Ching Yi, Chan" <chingyichan.tw@gmail.com>
Date: Thu, 24 Sep 2015 13:04:40 +0800
Subject: [PATCH] avformat/flacdec: support fast-seek

---
 libavformat/flacdec.c |   66 ++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 65 insertions(+), 1 deletions(-)

diff --git a/libavformat/flacdec.c b/libavformat/flacdec.c
index 4c1f943..23011ba 100644
--- a/libavformat/flacdec.c
+++ b/libavformat/flacdec.c
@@ -28,11 +28,23 @@
 #include "vorbiscomment.h"
 #include "replaygain.h"
 
+#define SEEKPOINT_SIZE 18
+#define MAX_SEEKPOINT_ENTRIES 1024
+
+typedef struct FlacDecContext {
+    AVClass *class;
+    int64_t metadata_head_size;
+    int num_index_entries;
+    AVIndexEntry index_entries[MAX_SEEKPOINT_ENTRIES];
+} FlacDecContext;
+
+
 static int flac_read_header(AVFormatContext *s)
 {
     int ret, metadata_last=0, metadata_type, metadata_size, found_streaminfo=0;
     uint8_t header[4];
     uint8_t *buffer=NULL;
+    FlacDecContext *flac_ctx = s->priv_data;
     AVStream *st = avformat_new_stream(s, NULL);
     if (!st)
         return AVERROR(ENOMEM);
@@ -58,6 +70,7 @@ static int flac_read_header(AVFormatContext *s)
         case FLAC_METADATA_TYPE_CUESHEET:
         case FLAC_METADATA_TYPE_PICTURE:
         case FLAC_METADATA_TYPE_VORBIS_COMMENT:
+        case FLAC_METADATA_TYPE_SEEKTABLE:
             buffer = av_mallocz(metadata_size + AV_INPUT_BUFFER_PADDING_SIZE);
             if (!buffer) {
                 return AVERROR(ENOMEM);
@@ -132,7 +145,25 @@ static int flac_read_header(AVFormatContext *s)
                 av_log(s, AV_LOG_ERROR, "Error parsing attached picture.\n");
                 return ret;
             }
-        } else {
+        } else if (metadata_type == FLAC_METADATA_TYPE_SEEKTABLE) {
+            const uint8_t *seekpoint = buffer;
+            int i, seek_point_count = metadata_size/SEEKPOINT_SIZE;
+            if(seek_point_count>MAX_SEEKPOINT_ENTRIES) {
+                av_log(s, AV_LOG_WARNING, "Too many seekpoints (over than %d)\n", MAX_SEEKPOINT_ENTRIES);
+            }
+            for(i=0; i<seek_point_count; i++) {
+                flac_ctx->index_entries[i].timestamp = bytestream_get_be64(&seekpoint);
+                flac_ctx->index_entries[i].pos = bytestream_get_be64(&seekpoint);
+                flac_ctx->num_index_entries = i + 1;
+                int samples = bytestream_get_be16(&seekpoint);
+                if(flac_ctx->num_index_entries>=MAX_SEEKPOINT_ENTRIES) {
+                    break ;
+                }
+            }
+            av_freep(&buffer);
+        }
+        else {
+
             /* STREAMINFO must be the first block */
             if (!found_streaminfo) {
                 RETURN_ERROR(AVERROR_INVALIDDATA);
@@ -169,6 +200,7 @@ static int flac_read_header(AVFormatContext *s)
     if (ret < 0)
         return ret;
 
+    flac_ctx->metadata_head_size = avio_tell(s->pb);
     return 0;
 
 fail:
@@ -249,14 +281,46 @@ static av_unused int64_t flac_read_timestamp(AVFormatContext *s, int stream_inde
     return pts;
 }
 
+static int flac_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags) {
+
+    FlacDecContext *flac_ctx = s->priv_data;
+
+    int64_t seek_pos = -1;
+    for(int i=0; i<flac_ctx->num_index_entries; i++) {
+        if(flac_ctx->index_entries[i].timestamp >= timestamp) {
+            seek_pos = flac_ctx->index_entries[i].pos;
+            break ;
+        }
+    }
+
+    if(seek_pos<0) {
+        return -1;
+    }
+
+    int ret = avio_seek(s->pb, flac_ctx->metadata_head_size + seek_pos, SEEK_SET);
+    if (ret >= 0) {
+        return ret;
+    }
+    return -1;
+}
+
+static const AVClass flac_class = {
+    .class_name = "flacdec",
+    .item_name  = av_default_item_name,
+    .version    = LIBAVUTIL_VERSION_INT,
+};
+
 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_seek      = flac_seek,
     .read_timestamp = flac_read_timestamp,
     .flags          = AVFMT_GENERIC_INDEX,
     .extensions     = "flac",
     .raw_codec_id   = AV_CODEC_ID_FLAC,
+    .priv_data_size = sizeof(FlacDecContext),
+    .priv_class     = &flac_class,
 };
-- 
1.7.7

