File ffmpeg-4.3.1-ogg.patch of Package kodi

From cec779723072f32a37ccfabe5488ffe2228f4ea0 Mon Sep 17 00:00:00 2001
From: Marton Balint <cus@passwd.hu>
Date: Sun, 20 Sep 2020 09:32:44 +0200
Subject: [PATCH 1/8] avformat/aviobuf: write data into the IO buffer till the
 very end of the buffer

There was an off-by-one error when checking if the IO buffer still has enough
space till the end. One more byte can be safely written.

Signed-off-by: Marton Balint <cus@passwd.hu>
---
 libavformat/aviobuf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git libavformat/aviobuf.c libavformat/aviobuf.c
index a48ceebaef..aab3f1ef00 100644
--- libavformat/aviobuf.c
+++ libavformat/aviobuf.c
@@ -528,7 +528,7 @@ static void fill_buffer(AVIOContext *s)
 {
     int max_buffer_size = s->max_packet_size ?
                           s->max_packet_size : IO_BUFFER_SIZE;
-    uint8_t *dst        = s->buf_end - s->buffer + max_buffer_size < s->buffer_size ?
+    uint8_t *dst        = s->buf_end - s->buffer + max_buffer_size <= s->buffer_size ?
                           s->buf_end : s->buffer;
     int len             = s->buffer_size - (dst - s->buffer);
 
-- 
2.35.3


From 09e3cc9889ae7d4aad6a8a9c624d1e23499c0f0b Mon Sep 17 00:00:00 2001
From: Marton Balint <cus@passwd.hu>
Date: Sat, 26 Sep 2020 19:20:50 +0200
Subject: [PATCH 2/8] avformat/aviobuf: check if requested seekback buffer is
 already read

Existing code did not check if the requested seekback buffer is
already read entirely. In this case, nothing has to be done to guarantee
seekback.

Signed-off-by: Marton Balint <cus@passwd.hu>
---
 libavformat/aviobuf.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git libavformat/aviobuf.c libavformat/aviobuf.c
index aab3f1ef00..de047c44f6 100644
--- libavformat/aviobuf.c
+++ libavformat/aviobuf.c
@@ -987,6 +987,9 @@ int ffio_ensure_seekback(AVIOContext *s, int64_t buf_size)
     int filled = s->buf_end - s->buffer;
     ptrdiff_t checksum_ptr_offset = s->checksum_ptr ? s->checksum_ptr - s->buffer : -1;
 
+    if (buf_size <= s->buf_end - s->buf_ptr)
+        return 0;
+
     buf_size += s->buf_ptr - s->buffer + max_buffer_size;
 
     if (buf_size < filled || s->seekable || !s->read_packet)
-- 
2.35.3


From 5ee650fd6c5276e3977ad9f856146053e16b23e5 Mon Sep 17 00:00:00 2001
From: Marton Balint <cus@passwd.hu>
Date: Sun, 20 Sep 2020 00:01:48 +0200
Subject: [PATCH 3/8] avformat/aviobuf: fix checks in ffio_ensure_seekback

The new buf_size was detemined too conservatively, maybe because of the
off-by-one issue which was fixed recently in fill_buffer. We can safely
substract 1 more from the new buffer size, because max_buffer_size space must
only be guaranteed when we are reading the last byte of the requested window.

Comparing the new buf_size against filled did not make a lot of sense, what
makes sense is that we want to reallocate the buffer if the new buf_size is
bigger than the old, therefore the change in the check.

Signed-off-by: Marton Balint <cus@passwd.hu>
---
 libavformat/aviobuf.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git libavformat/aviobuf.c libavformat/aviobuf.c
index de047c44f6..c8be9122a7 100644
--- libavformat/aviobuf.c
+++ libavformat/aviobuf.c
@@ -990,9 +990,9 @@ int ffio_ensure_seekback(AVIOContext *s, int64_t buf_size)
     if (buf_size <= s->buf_end - s->buf_ptr)
         return 0;
 
-    buf_size += s->buf_ptr - s->buffer + max_buffer_size;
+    buf_size += s->buf_ptr - s->buffer + max_buffer_size - 1;
 
-    if (buf_size < filled || s->seekable || !s->read_packet)
+    if (buf_size <= s->buffer_size || s->seekable || !s->read_packet)
         return 0;
     av_assert0(!s->write_flag);
 
-- 
2.35.3


From 11f324f1db3976f726ff850196200176b7f31c0f Mon Sep 17 00:00:00 2001
From: Marton Balint <cus@passwd.hu>
Date: Mon, 28 Sep 2020 23:48:34 +0200
Subject: [PATCH 4/8] avformat/aviobuf: discard part of the IO buffer in
 ffio_ensure_seekback if needed

Previously ffio_ensure_seekback never flushed the buffer, so successive
ffio_ensure_seekback calls were all respected. This could eventually cause
unlimited memory and CPU usage if a demuxer called ffio_ensure_seekback on all
it's read data.

Most demuxers however only rely on being able to seek back till the position of
the last ffio_ensure_seekback call, therefore we change the semantics of
ffio_ensure_seekback so that a new call can invalidate seek guarantees of the
old. In order to support some level of "nested" ffio_ensure_seekback calls, we
document that the function only invalidates the old window (and potentially
discards the already read data from the IO buffer), if the newly requested
window does not fit into the old one.

This way we limit the memory usage for ffio_ensure_seekback calls requesting
consecutive data windows.

Signed-off-by: Marton Balint <cus@passwd.hu>
---
 libavformat/avio_internal.h |  4 +++-
 libavformat/aviobuf.c       | 27 +++++++++++++++++----------
 2 files changed, 20 insertions(+), 11 deletions(-)

diff --git libavformat/avio_internal.h libavformat/avio_internal.h
index c575df8035..fe87f2a288 100644
--- libavformat/avio_internal.h
+++ libavformat/avio_internal.h
@@ -100,7 +100,9 @@ int ffio_realloc_buf(AVIOContext *s, int buf_size);
  *
  * Will ensure that when reading sequentially up to buf_size, seeking
  * within the current pos and pos+buf_size is possible.
- * Once the stream position moves outside this window this guarantee is lost.
+ * Once the stream position moves outside this window or another
+ * ffio_ensure_seekback call requests a buffer outside this window this
+ * guarantee is lost.
  */
 int ffio_ensure_seekback(AVIOContext *s, int64_t buf_size);
 
diff --git libavformat/aviobuf.c libavformat/aviobuf.c
index c8be9122a7..d3e07ed38f 100644
--- libavformat/aviobuf.c
+++ libavformat/aviobuf.c
@@ -979,35 +979,42 @@ URLContext* ffio_geturlcontext(AVIOContext *s)
         return NULL;
 }
 
+static void update_checksum(AVIOContext *s)
+{
+    if (s->update_checksum && s->buf_ptr > s->checksum_ptr) {
+        s->checksum = s->update_checksum(s->checksum, s->checksum_ptr,
+                                         s->buf_ptr - s->checksum_ptr);
+    }
+}
+
 int ffio_ensure_seekback(AVIOContext *s, int64_t buf_size)
 {
     uint8_t *buffer;
     int max_buffer_size = s->max_packet_size ?
                           s->max_packet_size : IO_BUFFER_SIZE;
-    int filled = s->buf_end - s->buffer;
-    ptrdiff_t checksum_ptr_offset = s->checksum_ptr ? s->checksum_ptr - s->buffer : -1;
+    ptrdiff_t filled = s->buf_end - s->buf_ptr;
 
     if (buf_size <= s->buf_end - s->buf_ptr)
         return 0;
 
-    buf_size += s->buf_ptr - s->buffer + max_buffer_size - 1;
+    buf_size += max_buffer_size - 1;
 
-    if (buf_size <= s->buffer_size || s->seekable || !s->read_packet)
+    if (buf_size + s->buf_ptr - s->buffer <= s->buffer_size || s->seekable || !s->read_packet)
         return 0;
     av_assert0(!s->write_flag);
 
+    buf_size = FFMAX(buf_size, s->buffer_size);
     buffer = av_malloc(buf_size);
     if (!buffer)
         return AVERROR(ENOMEM);
-
-    memcpy(buffer, s->buffer, filled);
+    update_checksum(s);
+    memcpy(buffer, s->buf_ptr, filled);
     av_free(s->buffer);
-    s->buf_ptr = buffer + (s->buf_ptr - s->buffer);
-    s->buf_end = buffer + (s->buf_end - s->buffer);
     s->buffer = buffer;
     s->buffer_size = buf_size;
-    if (checksum_ptr_offset >= 0)
-        s->checksum_ptr = s->buffer + checksum_ptr_offset;
+    s->buf_ptr = s->buffer;
+    s->buf_end = s->buffer + filled;
+    s->checksum_ptr = s->buffer;
     return 0;
 }
 
-- 
2.35.3


From 238ca22eed9987705b48a80ce44176f5d956e460 Mon Sep 17 00:00:00 2001
From: Marton Balint <cus@passwd.hu>
Date: Tue, 29 Sep 2020 00:15:27 +0200
Subject: [PATCH 5/8] avformat/aviobuf: do not allocate a new buffer in
 ffio_ensure_seekback if not needed

Let's move unread data to the start of the old buffer instead.

Signed-off-by: Marton Balint <cus@passwd.hu>
---
 libavformat/aviobuf.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git libavformat/aviobuf.c libavformat/aviobuf.c
index d3e07ed38f..34ae25b4b1 100644
--- libavformat/aviobuf.c
+++ libavformat/aviobuf.c
@@ -1003,7 +1003,10 @@ int ffio_ensure_seekback(AVIOContext *s, int64_t buf_size)
         return 0;
     av_assert0(!s->write_flag);
 
-    buf_size = FFMAX(buf_size, s->buffer_size);
+    if (buf_size <= s->buffer_size) {
+        update_checksum(s);
+        memmove(s->buffer, s->buf_ptr, filled);
+    } else {
     buffer = av_malloc(buf_size);
     if (!buffer)
         return AVERROR(ENOMEM);
@@ -1012,6 +1015,7 @@ int ffio_ensure_seekback(AVIOContext *s, int64_t buf_size)
     av_free(s->buffer);
     s->buffer = buffer;
     s->buffer_size = buf_size;
+    }
     s->buf_ptr = s->buffer;
     s->buf_end = s->buffer + filled;
     s->checksum_ptr = s->buffer;
-- 
2.35.3


From 07c6d9b4088afb7d88708ff3e0761c1b9386f4fc Mon Sep 17 00:00:00 2001
From: Marton Balint <cus@passwd.hu>
Date: Tue, 29 Sep 2020 00:16:47 +0200
Subject: [PATCH 6/8] avformat/aviobuf: fix indentation

Signed-off-by: Marton Balint <cus@passwd.hu>
---
 libavformat/aviobuf.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git libavformat/aviobuf.c libavformat/aviobuf.c
index 34ae25b4b1..8f6f8019d1 100644
--- libavformat/aviobuf.c
+++ libavformat/aviobuf.c
@@ -1007,14 +1007,14 @@ int ffio_ensure_seekback(AVIOContext *s, int64_t buf_size)
         update_checksum(s);
         memmove(s->buffer, s->buf_ptr, filled);
     } else {
-    buffer = av_malloc(buf_size);
-    if (!buffer)
-        return AVERROR(ENOMEM);
-    update_checksum(s);
-    memcpy(buffer, s->buf_ptr, filled);
-    av_free(s->buffer);
-    s->buffer = buffer;
-    s->buffer_size = buf_size;
+        buffer = av_malloc(buf_size);
+        if (!buffer)
+            return AVERROR(ENOMEM);
+        update_checksum(s);
+        memcpy(buffer, s->buf_ptr, filled);
+        av_free(s->buffer);
+        s->buffer = buffer;
+        s->buffer_size = buf_size;
     }
     s->buf_ptr = s->buffer;
     s->buf_end = s->buffer + filled;
-- 
2.35.3


From b1bf8ee8951cc099ab43d96baa952323560c768b Mon Sep 17 00:00:00 2001
From: Marton Balint <cus@passwd.hu>
Date: Sun, 20 Sep 2020 00:02:19 +0200
Subject: [PATCH 7/8] avformat/aviobuf: increase default read buffer size to
 2*max_buffer_size for streamed data

This should increase the effectiveness of ffio_ensure_seekback by reducing the
number of buffer reallocations and memmoves/memcpys because even a small
seekback window requires max_buffer_size+window_size buffer space.

Signed-off-by: Marton Balint <cus@passwd.hu>
---
 libavformat/aviobuf.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git libavformat/aviobuf.c libavformat/aviobuf.c
index 8f6f8019d1..039f3182e1 100644
--- libavformat/aviobuf.c
+++ libavformat/aviobuf.c
@@ -926,6 +926,11 @@ int ffio_fdopen(AVIOContext **s, URLContext *h)
     } else {
         buffer_size = IO_BUFFER_SIZE;
     }
+    if (!(h->flags & AVIO_FLAG_WRITE) && h->is_streamed) {
+        if (buffer_size > INT_MAX/2)
+            return AVERROR(EINVAL);
+        buffer_size *= 2;
+    }
     buffer = av_malloc(buffer_size);
     if (!buffer)
         return AVERROR(ENOMEM);
-- 
2.35.3


From 727c8d9dea957ffc66af741ac318710398fa8c6f Mon Sep 17 00:00:00 2001
From: Marton Balint <cus@passwd.hu>
Date: Fri, 9 Oct 2020 00:18:07 +0200
Subject: [PATCH 8/8] Revert "aviobuf: Discard old buffered, previously read
 data in ffio_read_partial"

This is unneeded after 2ca48e466675a8a3630061cd2c15325eab8eda97 and it breaks
ffio_ensure_seekback().

This reverts commit 53c25ee0736497b46bb76064cc2c84c976b2d295.

Signed-off-by: Marton Balint <cus@passwd.hu>
---
 libavformat/aviobuf.c | 7 -------
 1 file changed, 7 deletions(-)

diff --git libavformat/aviobuf.c libavformat/aviobuf.c
index 039f3182e1..050c063998 100644
--- libavformat/aviobuf.c
+++ libavformat/aviobuf.c
@@ -707,13 +707,6 @@ int avio_read_partial(AVIOContext *s, unsigned char *buf, int size)
 
     len = s->buf_end - s->buf_ptr;
     if (len == 0) {
-        /* Reset the buf_end pointer to the start of the buffer, to make sure
-         * the fill_buffer call tries to read as much data as fits into the
-         * full buffer, instead of just what space is left after buf_end.
-         * This avoids returning partial packets at the end of the buffer,
-         * for packet based inputs.
-         */
-        s->buf_end = s->buf_ptr = s->buffer;
         fill_buffer(s);
         len = s->buf_end - s->buf_ptr;
     }
-- 
2.35.3