diff mbox series

[FFmpeg-devel] avformat/flic: add support for seeking to start

Message ID 20200910115921.27843-1-onemda@gmail.com
State Accepted
Commit 2752323046a48003d7046cc662a8835bbf65de5c
Headers show
Series [FFmpeg-devel] avformat/flic: add support for seeking to start
Related show

Checks

Context Check Description
andriy/default pending
andriy/make success Make finished
andriy/make_fate success Make fate finished

Commit Message

Paul B Mahol Sept. 10, 2020, 11:59 a.m. UTC
Signed-off-by: Paul B Mahol <onemda@gmail.com>
---
 libavformat/flic.c | 38 +++++++++++++++++++++++++++++++++++---
 1 file changed, 35 insertions(+), 3 deletions(-)

Comments

Paul B Mahol Sept. 13, 2020, 9:08 a.m. UTC | #1
On Thu, Sep 10, 2020 at 01:59:21PM +0200, Paul B Mahol wrote:
> Signed-off-by: Paul B Mahol <onemda@gmail.com>
> ---
>  libavformat/flic.c | 38 +++++++++++++++++++++++++++++++++++---
>  1 file changed, 35 insertions(+), 3 deletions(-)
> 

will apply
diff mbox series

Patch

diff --git a/libavformat/flic.c b/libavformat/flic.c
index e65c157777..4552bff69c 100644
--- a/libavformat/flic.c
+++ b/libavformat/flic.c
@@ -202,6 +202,7 @@  static int flic_read_packet(AVFormatContext *s,
     int magic;
     int ret = 0;
     unsigned char preamble[FLIC_PREAMBLE_SIZE];
+    int64_t pos = avio_tell(pb);
 
     while (!packet_read && !avio_feof(pb)) {
 
@@ -219,15 +220,19 @@  static int flic_read_packet(AVFormatContext *s,
                 return ret;
 
             pkt->stream_index = flic->video_stream_index;
-            pkt->pts = flic->frame_number++;
-            pkt->pos = avio_tell(pb);
+            pkt->pos = pos;
             memcpy(pkt->data, preamble, FLIC_PREAMBLE_SIZE);
             ret = avio_read(pb, pkt->data + FLIC_PREAMBLE_SIZE,
                 size - FLIC_PREAMBLE_SIZE);
             if (ret != size - FLIC_PREAMBLE_SIZE) {
                 ret = AVERROR(EIO);
             }
+            pkt->flags = flic->frame_number == 0 ? AV_PKT_FLAG_KEY : 0;
+            pkt->pts = flic->frame_number;
+            if (flic->frame_number == 0)
+                av_add_index_entry(s->streams[flic->video_stream_index], pkt->pos, pkt->pts, pkt->size, 0, AVINDEX_KEYFRAME);
             packet_read = 1;
+            flic->frame_number++;
         } else if (magic == FLIC_TFTD_CHUNK_AUDIO) {
             if ((ret = av_new_packet(pkt, size)) < 0)
                 return ret;
@@ -236,7 +241,8 @@  static int flic_read_packet(AVFormatContext *s,
             avio_skip(pb, 10);
 
             pkt->stream_index = flic->audio_stream_index;
-            pkt->pos = avio_tell(pb);
+            pkt->pos = pos;
+            pkt->flags = AV_PKT_FLAG_KEY;
             ret = avio_read(pb, pkt->data, size);
 
             if (ret != size) {
@@ -254,6 +260,31 @@  static int flic_read_packet(AVFormatContext *s,
     return avio_feof(pb) ? AVERROR_EOF : ret;
 }
 
+static int flic_read_seek(AVFormatContext *s, int stream_index,
+                          int64_t pts, int flags)
+{
+    FlicDemuxContext *flic = s->priv_data;
+    AVStream *st = s->streams[stream_index];
+    int64_t pos, ts;
+    int index;
+
+    if (!st->index_entries || stream_index != flic->video_stream_index)
+        return -1;
+
+    index = av_index_search_timestamp(st, pts, flags);
+
+    if (index < 0)
+        index = av_index_search_timestamp(st, pts, flags ^ AVSEEK_FLAG_BACKWARD);
+    if (index < 0)
+        return -1;
+
+    pos = st->index_entries[index].pos;
+    ts  = st->index_entries[index].timestamp;
+    flic->frame_number = ts;
+    avio_seek(s->pb, pos, SEEK_SET);
+    return 0;
+}
+
 AVInputFormat ff_flic_demuxer = {
     .name           = "flic",
     .long_name      = NULL_IF_CONFIG_SMALL("FLI/FLC/FLX animation"),
@@ -261,4 +292,5 @@  AVInputFormat ff_flic_demuxer = {
     .read_probe     = flic_probe,
     .read_header    = flic_read_header,
     .read_packet    = flic_read_packet,
+    .read_seek      = flic_read_seek,
 };