diff mbox

[FFmpeg-devel,3/3] avcodec/dvbsubdec: Avoid re-computing clut

Message ID 20171104001921.24006-3-michael@niedermayer.cc
State Accepted
Commit eec67f7b24da5407cc2e8933ffe72358336811ab
Headers show

Commit Message

Michael Niedermayer Nov. 4, 2017, 12:19 a.m. UTC
Fixes: Timeout
Fixes: 3218/clusterfuzz-testcase-5390672154591232

Found-by: continuous fuzzing process https://github.com/google/oss-fuzz/tree/master/projects/ffmpeg
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
---
 libavcodec/dvbsubdec.c | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)

Comments

Michael Niedermayer Nov. 15, 2017, 1:52 a.m. UTC | #1
On Sat, Nov 04, 2017 at 01:19:21AM +0100, Michael Niedermayer wrote:
> Fixes: Timeout
> Fixes: 3218/clusterfuzz-testcase-5390672154591232
> 
> Found-by: continuous fuzzing process https://github.com/google/oss-fuzz/tree/master/projects/ffmpeg
> Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
> ---
>  libavcodec/dvbsubdec.c | 18 ++++++++++++++----
>  1 file changed, 14 insertions(+), 4 deletions(-)

will apply

[...]
diff mbox

Patch

diff --git a/libavcodec/dvbsubdec.c b/libavcodec/dvbsubdec.c
index 85a59f3ced..c60aa9a34d 100644
--- a/libavcodec/dvbsubdec.c
+++ b/libavcodec/dvbsubdec.c
@@ -96,6 +96,9 @@  typedef struct DVBSubRegion {
     int clut;
     int bgcolor;
 
+    uint8_t computed_clut[4*256];
+    int has_computed_clut;
+
     uint8_t *pbuf;
     int buf_size;
     int dirty;
@@ -647,7 +650,7 @@  static int dvbsub_read_8bit_string(AVCodecContext *avctx,
     return pixels_read;
 }
 
-static void compute_default_clut(AVSubtitleRect *rect, int w, int h)
+static void compute_default_clut(uint8_t *clut, AVSubtitleRect *rect, int w, int h)
 {
     uint8_t list[256] = {0};
     uint8_t list_inv[256];
@@ -703,7 +706,7 @@  static void compute_default_clut(AVSubtitleRect *rect, int w, int h)
     count = FFMAX(i - 1, 1);
     for (i--; i>=0; i--) {
         int v = i*255/count;
-        AV_WN32(rect->data[1] + 4*list_inv[i], RGBA(v/2,v,v/2,v));
+        AV_WN32(clut + 4*list_inv[i], RGBA(v/2,v,v/2,v));
     }
 }
 
@@ -809,8 +812,14 @@  static int save_subtitle_set(AVCodecContext *avctx, AVSubtitle *sub, int *got_ou
 
             memcpy(rect->data[0], region->pbuf, region->buf_size);
 
-            if ((clut == &default_clut && ctx->compute_clut == -1) || ctx->compute_clut == 1)
-                compute_default_clut(rect, rect->w, rect->h);
+            if ((clut == &default_clut && ctx->compute_clut == -1) || ctx->compute_clut == 1) {
+                if (!region->has_computed_clut) {
+                    compute_default_clut(region->computed_clut, rect, rect->w, rect->h);
+                    region->has_computed_clut = 1;
+                }
+
+                memcpy(rect->data[1], region->computed_clut, sizeof(region->computed_clut));
+            }
 
 #if FF_API_AVPICTURE
 FF_DISABLE_DEPRECATION_WARNINGS
@@ -959,6 +968,7 @@  static void dvbsub_parse_pixel_data_block(AVCodecContext *avctx, DVBSubObjectDis
         }
     }
 
+    region->has_computed_clut = 0;
 }
 
 static int dvbsub_parse_object_segment(AVCodecContext *avctx,