From 3f34b0021f1f7b92409df88d3556010f5e55ba94 Mon Sep 17 00:00:00 2001
From: Pascal Massimino <pascal.massimino@gmail.com>
Date: Mon, 22 Sep 2014 14:48:57 -0700
Subject: [PATCH] avcodec/webp: fix default palette color 0xff000000 ->
 0x00000000

also add optimization: use local palette with extra padding
for big enough pictures.
---
 libavcodec/webp.c | 36 +++++++++++++++++++++++++-----------
 1 file changed, 25 insertions(+), 11 deletions(-)

diff --git a/libavcodec/webp.c b/libavcodec/webp.c
index 66c2d57..1cbc648 100644
--- a/libavcodec/webp.c
+++ b/libavcodec/webp.c
@@ -1060,20 +1060,34 @@ static int apply_color_indexing_transform(WebPContext *s)
         }
         av_free(line);
     }
-
-    for (y = 0; y < img->frame->height; y++) {
-        for (x = 0; x < img->frame->width; x++) {
-            p = GET_PIXEL(img->frame, x, y);
-            i = p[2];
-            if (i >= pal->frame->width) {
-                AV_WB32(p, 0xFF000000);
-            } else {
-                const uint8_t *pi = GET_PIXEL(pal->frame, i, 0);
-                AV_COPY32(p, pi);
+    // switch to local palette if it's worth initializing it
+    if (img->frame->height * img->frame->width > 300) {
+          uint8_t palette[256 * 4];
+          const int size = pal->frame->width * 4;
+          memcpy(palette, GET_PIXEL(pal->frame, 0, 0), size);   // copy palette
+          // set extra entries to transparent black
+          memset(palette + size, 0, 256 * 4 - size);
+          for (y = 0; y < img->frame->height; y++) {
+              for (x = 0; x < img->frame->width; x++) {
+                  p = GET_PIXEL(img->frame, x, y);
+                  i = p[2];
+                  AV_COPY32(p, &palette[i * 4]);
+              }
+          }
+    } else {
+        for (y = 0; y < img->frame->height; y++) {
+            for (x = 0; x < img->frame->width; x++) {
+                p = GET_PIXEL(img->frame, x, y);
+                i = p[2];
+                if (i >= pal->frame->width) {
+                    AV_WB32(p, 0x00000000);
+                } else {
+                    const uint8_t *pi = GET_PIXEL(pal->frame, i, 0);
+                    AV_COPY32(p, pi);
+                }
             }
         }
     }
-
     return 0;
 }
 
-- 
1.9.3

