From 0b9f467dff2e6d7bfc24f4abda8ae2a52bac2b09 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 |   25 ++++++++++++++++++++++++-
 1 files changed, 24 insertions(+), 1 deletions(-)

diff --git a/libavformat/flacdec.c b/libavformat/flacdec.c
index 4c1f943..da74803 100644
--- a/libavformat/flacdec.c
+++ b/libavformat/flacdec.c
@@ -28,11 +28,14 @@
 #include "vorbiscomment.h"
 #include "replaygain.h"
 
+#define SEEKPOINT_SIZE 18
+
 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;
+
     AVStream *st = avformat_new_stream(s, NULL);
     if (!st)
         return AVERROR(ENOMEM);
@@ -58,6 +61,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 +136,20 @@ 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;
+            for(i=0; i<seek_point_count; i++) {
+                int64_t timestamp = bytestream_get_be64(&seekpoint);
+                int64_t pos = bytestream_get_be64(&seekpoint);
+                /* skip number of samples */
+                bytestream_get_be16(&seekpoint);
+                av_add_index_entry(st, pos, timestamp, 0, 0, AVINDEX_KEYFRAME);
+            }
+            av_freep(&buffer);
+        }
+        else {
+
             /* STREAMINFO must be the first block */
             if (!found_streaminfo) {
                 RETURN_ERROR(AVERROR_INVALIDDATA);
@@ -169,6 +186,12 @@ static int flac_read_header(AVFormatContext *s)
     if (ret < 0)
         return ret;
 
+    /* the real seek index offset should be the size of metadata blocks with the offset in the frame blocks */
+    int metadata_head_size = avio_tell(s->pb);
+    int i;
+    for(i=0; i<st->nb_index_entries; i++) {
+        st->index_entries[i].pos += metadata_head_size;
+    }
     return 0;
 
 fail:
-- 
1.7.7

