diff mbox

[FFmpeg-devel,2/2] avfilter/vf_paletteuse: add option to use new palette for each output frame

Message ID 1472848930-1985-2-git-send-email-onemda@gmail.com
State Accepted
Headers show

Commit Message

Paul B Mahol Sept. 2, 2016, 8:42 p.m. UTC
Signed-off-by: Paul B Mahol <onemda@gmail.com>
---
 libavfilter/vf_paletteuse.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)

Comments

Clément Bœsch Sept. 6, 2016, 2:52 p.m. UTC | #1
On Fri, Sep 02, 2016 at 10:42:10PM +0200, Paul B Mahol wrote:
> Signed-off-by: Paul B Mahol <onemda@gmail.com>
> ---
>  libavfilter/vf_paletteuse.c | 16 +++++++++++++---
>  1 file changed, 13 insertions(+), 3 deletions(-)
> 
> diff --git a/libavfilter/vf_paletteuse.c b/libavfilter/vf_paletteuse.c
> index dece05a..ed51cfe 100644
> --- a/libavfilter/vf_paletteuse.c
> +++ b/libavfilter/vf_paletteuse.c
> @@ -86,6 +86,7 @@ typedef struct PaletteUseContext {
>      uint32_t palette[AVPALETTE_COUNT];
>      int palette_loaded;
>      int dither;
> +    int new;
>      set_frame_func set_frame;
>      int bayer_scale;
>      int ordered_dither[8*8];
> @@ -122,6 +123,7 @@ static const AVOption paletteuse_options[] = {
>          { "bruteforce",    "brute-force into the palette", 0, AV_OPT_TYPE_CONST, {.i64=COLOR_SEARCH_BRUTEFORCE},    INT_MIN, INT_MAX, FLAGS, "search" },
>      { "mean_err", "compute and print mean error", OFFSET(calc_mean_err), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
>      { "debug_accuracy", "test color search accuracy", OFFSET(debug_accuracy), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
> +    { "new", "take new palette for each output frame", OFFSET(new), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
>      { NULL }
>  };
>  
> @@ -928,8 +930,15 @@ static void load_palette(PaletteUseContext *s, const AVFrame *palette_frame)
>      const uint32_t *p = (const uint32_t *)palette_frame->data[0];
>      const int p_linesize = palette_frame->linesize[0] >> 2;
>  
> -    i = 0;
> -    for (y = 0; y < palette_frame->height; y++) {
> +    if (s->new) {
> +        memset(s->palette, 0, sizeof(s->palette));
> +        memset(s->map, 0, sizeof(s->map));
> +        for (i = 0; i < CACHE_SIZE; i++)
> +            av_freep(&s->cache[i].entries);
> +        memset(s->cache, 0, sizeof(s->cache));
> +    }
> +

> +    for (i = 0, y = 0; y < palette_frame->height; y++) {

Please keep the i=0 out of the loop; this line doesn't need to change.

Rest LGTM, thanks
diff mbox

Patch

diff --git a/libavfilter/vf_paletteuse.c b/libavfilter/vf_paletteuse.c
index dece05a..ed51cfe 100644
--- a/libavfilter/vf_paletteuse.c
+++ b/libavfilter/vf_paletteuse.c
@@ -86,6 +86,7 @@  typedef struct PaletteUseContext {
     uint32_t palette[AVPALETTE_COUNT];
     int palette_loaded;
     int dither;
+    int new;
     set_frame_func set_frame;
     int bayer_scale;
     int ordered_dither[8*8];
@@ -122,6 +123,7 @@  static const AVOption paletteuse_options[] = {
         { "bruteforce",    "brute-force into the palette", 0, AV_OPT_TYPE_CONST, {.i64=COLOR_SEARCH_BRUTEFORCE},    INT_MIN, INT_MAX, FLAGS, "search" },
     { "mean_err", "compute and print mean error", OFFSET(calc_mean_err), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
     { "debug_accuracy", "test color search accuracy", OFFSET(debug_accuracy), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
+    { "new", "take new palette for each output frame", OFFSET(new), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
     { NULL }
 };
 
@@ -928,8 +930,15 @@  static void load_palette(PaletteUseContext *s, const AVFrame *palette_frame)
     const uint32_t *p = (const uint32_t *)palette_frame->data[0];
     const int p_linesize = palette_frame->linesize[0] >> 2;
 
-    i = 0;
-    for (y = 0; y < palette_frame->height; y++) {
+    if (s->new) {
+        memset(s->palette, 0, sizeof(s->palette));
+        memset(s->map, 0, sizeof(s->map));
+        for (i = 0; i < CACHE_SIZE; i++)
+            av_freep(&s->cache[i].entries);
+        memset(s->cache, 0, sizeof(s->cache));
+    }
+
+    for (i = 0, y = 0; y < palette_frame->height; y++) {
         for (x = 0; x < palette_frame->width; x++)
             s->palette[i++] = p[x];
         p += p_linesize;
@@ -937,7 +946,8 @@  static void load_palette(PaletteUseContext *s, const AVFrame *palette_frame)
 
     load_colormap(s);
 
-    s->palette_loaded = 1;
+    if (!s->new)
+        s->palette_loaded = 1;
 }
 
 static AVFrame *load_apply_palette(AVFilterContext *ctx, AVFrame *main,