]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_paletteuse.c
Merge commit '3db51bf671defd47f2ec5ab67b11fb7730fb5e5a'
[ffmpeg] / libavfilter / vf_paletteuse.c
1 /*
2  * Copyright (c) 2015 Stupeflix
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 /**
22  * @file
23  * Use a palette to downsample an input video stream.
24  */
25
26 #include "libavutil/bprint.h"
27 #include "libavutil/internal.h"
28 #include "libavutil/opt.h"
29 #include "libavutil/qsort.h"
30 #include "dualinput.h"
31 #include "avfilter.h"
32
33 enum dithering_mode {
34     DITHERING_NONE,
35     DITHERING_BAYER,
36     DITHERING_HECKBERT,
37     DITHERING_FLOYD_STEINBERG,
38     DITHERING_SIERRA2,
39     DITHERING_SIERRA2_4A,
40     NB_DITHERING
41 };
42
43 enum color_search_method {
44     COLOR_SEARCH_NNS_ITERATIVE,
45     COLOR_SEARCH_NNS_RECURSIVE,
46     COLOR_SEARCH_BRUTEFORCE,
47     NB_COLOR_SEARCHES
48 };
49
50 enum diff_mode {
51     DIFF_MODE_NONE,
52     DIFF_MODE_RECTANGLE,
53     NB_DIFF_MODE
54 };
55
56 struct color_node {
57     uint8_t val[3];
58     uint8_t palette_id;
59     int split;
60     int left_id, right_id;
61 };
62
63 #define NBITS 5
64 #define CACHE_SIZE (1<<(3*NBITS))
65
66 struct cached_color {
67     uint32_t color;
68     uint8_t pal_entry;
69 };
70
71 struct cache_node {
72     struct cached_color *entries;
73     int nb_entries;
74 };
75
76 struct PaletteUseContext;
77
78 typedef int (*set_frame_func)(struct PaletteUseContext *s, AVFrame *out, AVFrame *in,
79                               int x_start, int y_start, int width, int height);
80
81 typedef struct PaletteUseContext {
82     const AVClass *class;
83     FFDualInputContext dinput;
84     struct cache_node cache[CACHE_SIZE];    /* lookup cache */
85     struct color_node map[AVPALETTE_COUNT]; /* 3D-Tree (KD-Tree with K=3) for reverse colormap */
86     uint32_t palette[AVPALETTE_COUNT];
87     int palette_loaded;
88     int dither;
89     int new;
90     set_frame_func set_frame;
91     int bayer_scale;
92     int ordered_dither[8*8];
93     int diff_mode;
94     AVFrame *last_in;
95     AVFrame *last_out;
96
97     /* debug options */
98     char *dot_filename;
99     int color_search_method;
100     int calc_mean_err;
101     uint64_t total_mean_err;
102     int debug_accuracy;
103 } PaletteUseContext;
104
105 #define OFFSET(x) offsetof(PaletteUseContext, x)
106 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
107 static const AVOption paletteuse_options[] = {
108     { "dither", "select dithering mode", OFFSET(dither), AV_OPT_TYPE_INT, {.i64=DITHERING_SIERRA2_4A}, 0, NB_DITHERING-1, FLAGS, "dithering_mode" },
109         { "bayer",           "ordered 8x8 bayer dithering (deterministic)",                            0, AV_OPT_TYPE_CONST, {.i64=DITHERING_BAYER},           INT_MIN, INT_MAX, FLAGS, "dithering_mode" },
110         { "heckbert",        "dithering as defined by Paul Heckbert in 1982 (simple error diffusion)", 0, AV_OPT_TYPE_CONST, {.i64=DITHERING_HECKBERT},        INT_MIN, INT_MAX, FLAGS, "dithering_mode" },
111         { "floyd_steinberg", "Floyd and Steingberg dithering (error diffusion)",                       0, AV_OPT_TYPE_CONST, {.i64=DITHERING_FLOYD_STEINBERG}, INT_MIN, INT_MAX, FLAGS, "dithering_mode" },
112         { "sierra2",         "Frankie Sierra dithering v2 (error diffusion)",                          0, AV_OPT_TYPE_CONST, {.i64=DITHERING_SIERRA2},         INT_MIN, INT_MAX, FLAGS, "dithering_mode" },
113         { "sierra2_4a",      "Frankie Sierra dithering v2 \"Lite\" (error diffusion)",                 0, AV_OPT_TYPE_CONST, {.i64=DITHERING_SIERRA2_4A},      INT_MIN, INT_MAX, FLAGS, "dithering_mode" },
114     { "bayer_scale", "set scale for bayer dithering", OFFSET(bayer_scale), AV_OPT_TYPE_INT, {.i64=2}, 0, 5, FLAGS },
115     { "diff_mode",   "set frame difference mode",     OFFSET(diff_mode),   AV_OPT_TYPE_INT, {.i64=DIFF_MODE_NONE}, 0, NB_DIFF_MODE-1, FLAGS, "diff_mode" },
116         { "rectangle", "process smallest different rectangle", 0, AV_OPT_TYPE_CONST, {.i64=DIFF_MODE_RECTANGLE}, INT_MIN, INT_MAX, FLAGS, "diff_mode" },
117
118     /* following are the debug options, not part of the official API */
119     { "debug_kdtree", "save Graphviz graph of the kdtree in specified file", OFFSET(dot_filename), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
120     { "color_search", "set reverse colormap color search method", OFFSET(color_search_method), AV_OPT_TYPE_INT, {.i64=COLOR_SEARCH_NNS_ITERATIVE}, 0, NB_COLOR_SEARCHES-1, FLAGS, "search" },
121         { "nns_iterative", "iterative search",             0, AV_OPT_TYPE_CONST, {.i64=COLOR_SEARCH_NNS_ITERATIVE}, INT_MIN, INT_MAX, FLAGS, "search" },
122         { "nns_recursive", "recursive search",             0, AV_OPT_TYPE_CONST, {.i64=COLOR_SEARCH_NNS_RECURSIVE}, INT_MIN, INT_MAX, FLAGS, "search" },
123         { "bruteforce",    "brute-force into the palette", 0, AV_OPT_TYPE_CONST, {.i64=COLOR_SEARCH_BRUTEFORCE},    INT_MIN, INT_MAX, FLAGS, "search" },
124     { "mean_err", "compute and print mean error", OFFSET(calc_mean_err), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
125     { "debug_accuracy", "test color search accuracy", OFFSET(debug_accuracy), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
126     { "new", "take new palette for each output frame", OFFSET(new), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
127     { NULL }
128 };
129
130 AVFILTER_DEFINE_CLASS(paletteuse);
131
132 static int query_formats(AVFilterContext *ctx)
133 {
134     static const enum AVPixelFormat in_fmts[]    = {AV_PIX_FMT_RGB32, AV_PIX_FMT_NONE};
135     static const enum AVPixelFormat inpal_fmts[] = {AV_PIX_FMT_RGB32, AV_PIX_FMT_NONE};
136     static const enum AVPixelFormat out_fmts[]   = {AV_PIX_FMT_PAL8,  AV_PIX_FMT_NONE};
137     int ret;
138     AVFilterFormats *in    = ff_make_format_list(in_fmts);
139     AVFilterFormats *inpal = ff_make_format_list(inpal_fmts);
140     AVFilterFormats *out   = ff_make_format_list(out_fmts);
141     if (!in || !inpal || !out) {
142         av_freep(&in);
143         av_freep(&inpal);
144         av_freep(&out);
145         return AVERROR(ENOMEM);
146     }
147     if ((ret = ff_formats_ref(in   , &ctx->inputs[0]->out_formats)) < 0 ||
148         (ret = ff_formats_ref(inpal, &ctx->inputs[1]->out_formats)) < 0 ||
149         (ret = ff_formats_ref(out  , &ctx->outputs[0]->in_formats)) < 0)
150         return ret;
151     return 0;
152 }
153
154 static av_always_inline int dither_color(uint32_t px, int er, int eg, int eb, int scale, int shift)
155 {
156     return av_clip_uint8((px >> 16 & 0xff) + ((er * scale) / (1<<shift))) << 16
157          | av_clip_uint8((px >>  8 & 0xff) + ((eg * scale) / (1<<shift))) <<  8
158          | av_clip_uint8((px       & 0xff) + ((eb * scale) / (1<<shift)));
159 }
160
161 static av_always_inline int diff(const uint8_t *c1, const uint8_t *c2)
162 {
163     // XXX: try L*a*b with CIE76 (dL*dL + da*da + db*db)
164     const int dr = c1[0] - c2[0];
165     const int dg = c1[1] - c2[1];
166     const int db = c1[2] - c2[2];
167     return dr*dr + dg*dg + db*db;
168 }
169
170 static av_always_inline uint8_t colormap_nearest_bruteforce(const uint32_t *palette, const uint8_t *rgb)
171 {
172     int i, pal_id = -1, min_dist = INT_MAX;
173
174     for (i = 0; i < AVPALETTE_COUNT; i++) {
175         const uint32_t c = palette[i];
176
177         if ((c & 0xff000000) == 0xff000000) { // ignore transparent entry
178             const uint8_t palrgb[] = {
179                 palette[i]>>16 & 0xff,
180                 palette[i]>> 8 & 0xff,
181                 palette[i]     & 0xff,
182             };
183             const int d = diff(palrgb, rgb);
184             if (d < min_dist) {
185                 pal_id = i;
186                 min_dist = d;
187             }
188         }
189     }
190     return pal_id;
191 }
192
193 /* Recursive form, simpler but a bit slower. Kept for reference. */
194 struct nearest_color {
195     int node_pos;
196     int dist_sqd;
197 };
198
199 static void colormap_nearest_node(const struct color_node *map,
200                                   const int node_pos,
201                                   const uint8_t *target,
202                                   struct nearest_color *nearest)
203 {
204     const struct color_node *kd = map + node_pos;
205     const int s = kd->split;
206     int dx, nearer_kd_id, further_kd_id;
207     const uint8_t *current = kd->val;
208     const int current_to_target = diff(target, current);
209
210     if (current_to_target < nearest->dist_sqd) {
211         nearest->node_pos = node_pos;
212         nearest->dist_sqd = current_to_target;
213     }
214
215     if (kd->left_id != -1 || kd->right_id != -1) {
216         dx = target[s] - current[s];
217
218         if (dx <= 0) nearer_kd_id = kd->left_id,  further_kd_id = kd->right_id;
219         else         nearer_kd_id = kd->right_id, further_kd_id = kd->left_id;
220
221         if (nearer_kd_id != -1)
222             colormap_nearest_node(map, nearer_kd_id, target, nearest);
223
224         if (further_kd_id != -1 && dx*dx < nearest->dist_sqd)
225             colormap_nearest_node(map, further_kd_id, target, nearest);
226     }
227 }
228
229 static av_always_inline uint8_t colormap_nearest_recursive(const struct color_node *node, const uint8_t *rgb)
230 {
231     struct nearest_color res = {.dist_sqd = INT_MAX, .node_pos = -1};
232     colormap_nearest_node(node, 0, rgb, &res);
233     return node[res.node_pos].palette_id;
234 }
235
236 struct stack_node {
237     int color_id;
238     int dx2;
239 };
240
241 static av_always_inline uint8_t colormap_nearest_iterative(const struct color_node *root, const uint8_t *target)
242 {
243     int pos = 0, best_node_id = -1, best_dist = INT_MAX, cur_color_id = 0;
244     struct stack_node nodes[16];
245     struct stack_node *node = &nodes[0];
246
247     for (;;) {
248
249         const struct color_node *kd = &root[cur_color_id];
250         const uint8_t *current = kd->val;
251         const int current_to_target = diff(target, current);
252
253         /* Compare current color node to the target and update our best node if
254          * it's actually better. */
255         if (current_to_target < best_dist) {
256             best_node_id = cur_color_id;
257             if (!current_to_target)
258                 goto end; // exact match, we can return immediately
259             best_dist = current_to_target;
260         }
261
262         /* Check if it's not a leaf */
263         if (kd->left_id != -1 || kd->right_id != -1) {
264             const int split = kd->split;
265             const int dx = target[split] - current[split];
266             int nearer_kd_id, further_kd_id;
267
268             /* Define which side is the most interesting. */
269             if (dx <= 0) nearer_kd_id = kd->left_id,  further_kd_id = kd->right_id;
270             else         nearer_kd_id = kd->right_id, further_kd_id = kd->left_id;
271
272             if (nearer_kd_id != -1) {
273                 if (further_kd_id != -1) {
274                     /* Here, both paths are defined, so we push a state for
275                      * when we are going back. */
276                     node->color_id = further_kd_id;
277                     node->dx2 = dx*dx;
278                     pos++;
279                     node++;
280                 }
281                 /* We can now update current color with the most probable path
282                  * (no need to create a state since there is nothing to save
283                  * anymore). */
284                 cur_color_id = nearer_kd_id;
285                 continue;
286             } else if (dx*dx < best_dist) {
287                 /* The nearest path isn't available, so there is only one path
288                  * possible and it's the least probable. We enter it only if the
289                  * distance from the current point to the hyper rectangle is
290                  * less than our best distance. */
291                 cur_color_id = further_kd_id;
292                 continue;
293             }
294         }
295
296         /* Unstack as much as we can, typically as long as the least probable
297          * branch aren't actually probable. */
298         do {
299             if (--pos < 0)
300                 goto end;
301             node--;
302         } while (node->dx2 >= best_dist);
303
304         /* We got a node where the least probable branch might actually contain
305          * a relevant color. */
306         cur_color_id = node->color_id;
307     }
308
309 end:
310     return root[best_node_id].palette_id;
311 }
312
313 #define COLORMAP_NEAREST(search, palette, root, target)                                    \
314     search == COLOR_SEARCH_NNS_ITERATIVE ? colormap_nearest_iterative(root, target) :      \
315     search == COLOR_SEARCH_NNS_RECURSIVE ? colormap_nearest_recursive(root, target) :      \
316                                            colormap_nearest_bruteforce(palette, target)
317
318 /**
319  * Check if the requested color is in the cache already. If not, find it in the
320  * color tree and cache it.
321  * Note: r, g, and b are the component of c but are passed as well to avoid
322  * recomputing them (they are generally computed by the caller for other uses).
323  */
324 static av_always_inline int color_get(struct cache_node *cache, uint32_t color,
325                                       uint8_t r, uint8_t g, uint8_t b,
326                                       const struct color_node *map,
327                                       const uint32_t *palette,
328                                       const enum color_search_method search_method)
329 {
330     int i;
331     const uint8_t rgb[] = {r, g, b};
332     const uint8_t rhash = r & ((1<<NBITS)-1);
333     const uint8_t ghash = g & ((1<<NBITS)-1);
334     const uint8_t bhash = b & ((1<<NBITS)-1);
335     const unsigned hash = rhash<<(NBITS*2) | ghash<<NBITS | bhash;
336     struct cache_node *node = &cache[hash];
337     struct cached_color *e;
338
339     for (i = 0; i < node->nb_entries; i++) {
340         e = &node->entries[i];
341         if (e->color == color)
342             return e->pal_entry;
343     }
344
345     e = av_dynarray2_add((void**)&node->entries, &node->nb_entries,
346                          sizeof(*node->entries), NULL);
347     if (!e)
348         return AVERROR(ENOMEM);
349     e->color = color;
350     e->pal_entry = COLORMAP_NEAREST(search_method, palette, map, rgb);
351     return e->pal_entry;
352 }
353
354 static av_always_inline int get_dst_color_err(struct cache_node *cache,
355                                               uint32_t c, const struct color_node *map,
356                                               const uint32_t *palette,
357                                               int *er, int *eg, int *eb,
358                                               const enum color_search_method search_method)
359 {
360     const uint8_t r = c >> 16 & 0xff;
361     const uint8_t g = c >>  8 & 0xff;
362     const uint8_t b = c       & 0xff;
363     const int dstx = color_get(cache, c, r, g, b, map, palette, search_method);
364     const uint32_t dstc = palette[dstx];
365     *er = r - (dstc >> 16 & 0xff);
366     *eg = g - (dstc >>  8 & 0xff);
367     *eb = b - (dstc       & 0xff);
368     return dstx;
369 }
370
371 static av_always_inline int set_frame(PaletteUseContext *s, AVFrame *out, AVFrame *in,
372                                       int x_start, int y_start, int w, int h,
373                                       enum dithering_mode dither,
374                                       const enum color_search_method search_method)
375 {
376     int x, y;
377     const struct color_node *map = s->map;
378     struct cache_node *cache = s->cache;
379     const uint32_t *palette = s->palette;
380     const int src_linesize = in ->linesize[0] >> 2;
381     const int dst_linesize = out->linesize[0];
382     uint32_t *src = ((uint32_t *)in ->data[0]) + y_start*src_linesize;
383     uint8_t  *dst =              out->data[0]  + y_start*dst_linesize;
384
385     w += x_start;
386     h += y_start;
387
388     for (y = y_start; y < h; y++) {
389         for (x = x_start; x < w; x++) {
390             int er, eg, eb;
391
392             if (dither == DITHERING_BAYER) {
393                 const int d = s->ordered_dither[(y & 7)<<3 | (x & 7)];
394                 const uint8_t r8 = src[x] >> 16 & 0xff;
395                 const uint8_t g8 = src[x] >>  8 & 0xff;
396                 const uint8_t b8 = src[x]       & 0xff;
397                 const uint8_t r = av_clip_uint8(r8 + d);
398                 const uint8_t g = av_clip_uint8(g8 + d);
399                 const uint8_t b = av_clip_uint8(b8 + d);
400                 const uint32_t c = r<<16 | g<<8 | b;
401                 const int color = color_get(cache, c, r, g, b, map, palette, search_method);
402
403                 if (color < 0)
404                     return color;
405                 dst[x] = color;
406
407             } else if (dither == DITHERING_HECKBERT) {
408                 const int right = x < w - 1, down = y < h - 1;
409                 const int color = get_dst_color_err(cache, src[x], map, palette, &er, &eg, &eb, search_method);
410
411                 if (color < 0)
412                     return color;
413                 dst[x] = color;
414
415                 if (right)         src[               x + 1] = dither_color(src[               x + 1], er, eg, eb, 3, 3);
416                 if (         down) src[src_linesize + x    ] = dither_color(src[src_linesize + x    ], er, eg, eb, 3, 3);
417                 if (right && down) src[src_linesize + x + 1] = dither_color(src[src_linesize + x + 1], er, eg, eb, 2, 3);
418
419             } else if (dither == DITHERING_FLOYD_STEINBERG) {
420                 const int right = x < w - 1, down = y < h - 1, left = x > x_start;
421                 const int color = get_dst_color_err(cache, src[x], map, palette, &er, &eg, &eb, search_method);
422
423                 if (color < 0)
424                     return color;
425                 dst[x] = color;
426
427                 if (right)         src[               x + 1] = dither_color(src[               x + 1], er, eg, eb, 7, 4);
428                 if (left  && down) src[src_linesize + x - 1] = dither_color(src[src_linesize + x - 1], er, eg, eb, 3, 4);
429                 if (         down) src[src_linesize + x    ] = dither_color(src[src_linesize + x    ], er, eg, eb, 5, 4);
430                 if (right && down) src[src_linesize + x + 1] = dither_color(src[src_linesize + x + 1], er, eg, eb, 1, 4);
431
432             } else if (dither == DITHERING_SIERRA2) {
433                 const int right  = x < w - 1, down  = y < h - 1, left  = x > x_start;
434                 const int right2 = x < w - 2,                    left2 = x > x_start + 1;
435                 const int color = get_dst_color_err(cache, src[x], map, palette, &er, &eg, &eb, search_method);
436
437                 if (color < 0)
438                     return color;
439                 dst[x] = color;
440
441                 if (right)          src[                 x + 1] = dither_color(src[                 x + 1], er, eg, eb, 4, 4);
442                 if (right2)         src[                 x + 2] = dither_color(src[                 x + 2], er, eg, eb, 3, 4);
443
444                 if (down) {
445                     if (left2)      src[  src_linesize + x - 2] = dither_color(src[  src_linesize + x - 2], er, eg, eb, 1, 4);
446                     if (left)       src[  src_linesize + x - 1] = dither_color(src[  src_linesize + x - 1], er, eg, eb, 2, 4);
447                                     src[  src_linesize + x    ] = dither_color(src[  src_linesize + x    ], er, eg, eb, 3, 4);
448                     if (right)      src[  src_linesize + x + 1] = dither_color(src[  src_linesize + x + 1], er, eg, eb, 2, 4);
449                     if (right2)     src[  src_linesize + x + 2] = dither_color(src[  src_linesize + x + 2], er, eg, eb, 1, 4);
450                 }
451
452             } else if (dither == DITHERING_SIERRA2_4A) {
453                 const int right = x < w - 1, down = y < h - 1, left = x > x_start;
454                 const int color = get_dst_color_err(cache, src[x], map, palette, &er, &eg, &eb, search_method);
455
456                 if (color < 0)
457                     return color;
458                 dst[x] = color;
459
460                 if (right)         src[               x + 1] = dither_color(src[               x + 1], er, eg, eb, 2, 2);
461                 if (left  && down) src[src_linesize + x - 1] = dither_color(src[src_linesize + x - 1], er, eg, eb, 1, 2);
462                 if (         down) src[src_linesize + x    ] = dither_color(src[src_linesize + x    ], er, eg, eb, 1, 2);
463
464             } else {
465                 const uint8_t r = src[x] >> 16 & 0xff;
466                 const uint8_t g = src[x] >>  8 & 0xff;
467                 const uint8_t b = src[x]       & 0xff;
468                 const int color = color_get(cache, src[x] & 0xffffff, r, g, b, map, palette, search_method);
469
470                 if (color < 0)
471                     return color;
472                 dst[x] = color;
473             }
474         }
475         src += src_linesize;
476         dst += dst_linesize;
477     }
478     return 0;
479 }
480
481 #define INDENT 4
482 static void disp_node(AVBPrint *buf,
483                       const struct color_node *map,
484                       int parent_id, int node_id,
485                       int depth)
486 {
487     const struct color_node *node = &map[node_id];
488     const uint32_t fontcolor = node->val[0] > 0x50 &&
489                                node->val[1] > 0x50 &&
490                                node->val[2] > 0x50 ? 0 : 0xffffff;
491     av_bprintf(buf, "%*cnode%d ["
492                "label=\"%c%02X%c%02X%c%02X%c\" "
493                "fillcolor=\"#%02x%02x%02x\" "
494                "fontcolor=\"#%06X\"]\n",
495                depth*INDENT, ' ', node->palette_id,
496                "[  "[node->split], node->val[0],
497                "][ "[node->split], node->val[1],
498                " ]["[node->split], node->val[2],
499                "  ]"[node->split],
500                node->val[0], node->val[1], node->val[2],
501                fontcolor);
502     if (parent_id != -1)
503         av_bprintf(buf, "%*cnode%d -> node%d\n", depth*INDENT, ' ',
504                    map[parent_id].palette_id, node->palette_id);
505     if (node->left_id  != -1) disp_node(buf, map, node_id, node->left_id,  depth + 1);
506     if (node->right_id != -1) disp_node(buf, map, node_id, node->right_id, depth + 1);
507 }
508
509 // debug_kdtree=kdtree.dot -> dot -Tpng kdtree.dot > kdtree.png
510 static int disp_tree(const struct color_node *node, const char *fname)
511 {
512     AVBPrint buf;
513     FILE *f = av_fopen_utf8(fname, "w");
514
515     if (!f) {
516         int ret = AVERROR(errno);
517         av_log(NULL, AV_LOG_ERROR, "Cannot open file '%s' for writing: %s\n",
518                fname, av_err2str(ret));
519         return ret;
520     }
521
522     av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
523
524     av_bprintf(&buf, "digraph {\n");
525     av_bprintf(&buf, "    node [style=filled fontsize=10 shape=box]\n");
526     disp_node(&buf, node, -1, 0, 0);
527     av_bprintf(&buf, "}\n");
528
529     fwrite(buf.str, 1, buf.len, f);
530     fclose(f);
531     av_bprint_finalize(&buf, NULL);
532     return 0;
533 }
534
535 static int debug_accuracy(const struct color_node *node, const uint32_t *palette,
536                           const enum color_search_method search_method)
537 {
538     int r, g, b, ret = 0;
539
540     for (r = 0; r < 256; r++) {
541         for (g = 0; g < 256; g++) {
542             for (b = 0; b < 256; b++) {
543                 const uint8_t rgb[] = {r, g, b};
544                 const int r1 = COLORMAP_NEAREST(search_method, palette, node, rgb);
545                 const int r2 = colormap_nearest_bruteforce(palette, rgb);
546                 if (r1 != r2) {
547                     const uint32_t c1 = palette[r1];
548                     const uint32_t c2 = palette[r2];
549                     const uint8_t palrgb1[] = { c1>>16 & 0xff, c1>> 8 & 0xff, c1 & 0xff };
550                     const uint8_t palrgb2[] = { c2>>16 & 0xff, c2>> 8 & 0xff, c2 & 0xff };
551                     const int d1 = diff(palrgb1, rgb);
552                     const int d2 = diff(palrgb2, rgb);
553                     if (d1 != d2) {
554                         av_log(NULL, AV_LOG_ERROR,
555                                "/!\\ %02X%02X%02X: %d ! %d (%06X ! %06X) / dist: %d ! %d\n",
556                                r, g, b, r1, r2, c1 & 0xffffff, c2 & 0xffffff, d1, d2);
557                         ret = 1;
558                     }
559                 }
560             }
561         }
562     }
563     return ret;
564 }
565
566 struct color {
567     uint32_t value;
568     uint8_t pal_id;
569 };
570
571 struct color_rect {
572     uint8_t min[3];
573     uint8_t max[3];
574 };
575
576 typedef int (*cmp_func)(const void *, const void *);
577
578 #define DECLARE_CMP_FUNC(name, pos)                     \
579 static int cmp_##name(const void *pa, const void *pb)   \
580 {                                                       \
581     const struct color *a = pa;                         \
582     const struct color *b = pb;                         \
583     return   (a->value >> (8 * (2 - (pos))) & 0xff)     \
584            - (b->value >> (8 * (2 - (pos))) & 0xff);    \
585 }
586
587 DECLARE_CMP_FUNC(r, 0)
588 DECLARE_CMP_FUNC(g, 1)
589 DECLARE_CMP_FUNC(b, 2)
590
591 static const cmp_func cmp_funcs[] = {cmp_r, cmp_g, cmp_b};
592
593 static int get_next_color(const uint8_t *color_used, const uint32_t *palette,
594                           int *component, const struct color_rect *box)
595 {
596     int wr, wg, wb;
597     int i, longest = 0;
598     unsigned nb_color = 0;
599     struct color_rect ranges;
600     struct color tmp_pal[256];
601     cmp_func cmpf;
602
603     ranges.min[0] = ranges.min[1] = ranges.min[2] = 0xff;
604     ranges.max[0] = ranges.max[1] = ranges.max[2] = 0x00;
605
606     for (i = 0; i < AVPALETTE_COUNT; i++) {
607         const uint32_t c = palette[i];
608         const uint8_t r = c >> 16 & 0xff;
609         const uint8_t g = c >>  8 & 0xff;
610         const uint8_t b = c       & 0xff;
611
612         if (color_used[i] ||
613             r < box->min[0] || g < box->min[1] || b < box->min[2] ||
614             r > box->max[0] || g > box->max[1] || b > box->max[2])
615             continue;
616
617         if (r < ranges.min[0]) ranges.min[0] = r;
618         if (g < ranges.min[1]) ranges.min[1] = g;
619         if (b < ranges.min[2]) ranges.min[2] = b;
620
621         if (r > ranges.max[0]) ranges.max[0] = r;
622         if (g > ranges.max[1]) ranges.max[1] = g;
623         if (b > ranges.max[2]) ranges.max[2] = b;
624
625         tmp_pal[nb_color].value  = c;
626         tmp_pal[nb_color].pal_id = i;
627
628         nb_color++;
629     }
630
631     if (!nb_color)
632         return -1;
633
634     /* define longest axis that will be the split component */
635     wr = ranges.max[0] - ranges.min[0];
636     wg = ranges.max[1] - ranges.min[1];
637     wb = ranges.max[2] - ranges.min[2];
638     if (wr >= wg && wr >= wb) longest = 0;
639     if (wg >= wr && wg >= wb) longest = 1;
640     if (wb >= wr && wb >= wg) longest = 2;
641     cmpf = cmp_funcs[longest];
642     *component = longest;
643
644     /* sort along this axis to get median */
645     AV_QSORT(tmp_pal, nb_color, struct color, cmpf);
646
647     return tmp_pal[nb_color >> 1].pal_id;
648 }
649
650 static int colormap_insert(struct color_node *map,
651                            uint8_t *color_used,
652                            int *nb_used,
653                            const uint32_t *palette,
654                            const struct color_rect *box)
655 {
656     uint32_t c;
657     int component, cur_id;
658     int node_left_id = -1, node_right_id = -1;
659     struct color_node *node;
660     struct color_rect box1, box2;
661     const int pal_id = get_next_color(color_used, palette, &component, box);
662
663     if (pal_id < 0)
664         return -1;
665
666     /* create new node with that color */
667     cur_id = (*nb_used)++;
668     c = palette[pal_id];
669     node = &map[cur_id];
670     node->split = component;
671     node->palette_id = pal_id;
672     node->val[0] = c>>16 & 0xff;
673     node->val[1] = c>> 8 & 0xff;
674     node->val[2] = c     & 0xff;
675
676     color_used[pal_id] = 1;
677
678     /* get the two boxes this node creates */
679     box1 = box2 = *box;
680     box1.max[component] = node->val[component];
681     box2.min[component] = node->val[component] + 1;
682
683     node_left_id = colormap_insert(map, color_used, nb_used, palette, &box1);
684
685     if (box2.min[component] <= box2.max[component])
686         node_right_id = colormap_insert(map, color_used, nb_used, palette, &box2);
687
688     node->left_id  = node_left_id;
689     node->right_id = node_right_id;
690
691     return cur_id;
692 }
693
694 static int cmp_pal_entry(const void *a, const void *b)
695 {
696     const int c1 = *(const uint32_t *)a & 0xffffff;
697     const int c2 = *(const uint32_t *)b & 0xffffff;
698     return c1 - c2;
699 }
700
701 static void load_colormap(PaletteUseContext *s)
702 {
703     int i, nb_used = 0;
704     uint8_t color_used[AVPALETTE_COUNT] = {0};
705     uint32_t last_color = 0;
706     struct color_rect box;
707
708     /* disable transparent colors and dups */
709     qsort(s->palette, AVPALETTE_COUNT, sizeof(*s->palette), cmp_pal_entry);
710     for (i = 0; i < AVPALETTE_COUNT; i++) {
711         const uint32_t c = s->palette[i];
712         if (i != 0 && c == last_color) {
713             color_used[i] = 1;
714             continue;
715         }
716         last_color = c;
717         if ((c & 0xff000000) != 0xff000000) {
718             color_used[i] = 1; // ignore transparent color(s)
719             continue;
720         }
721     }
722
723     box.min[0] = box.min[1] = box.min[2] = 0x00;
724     box.max[0] = box.max[1] = box.max[2] = 0xff;
725
726     colormap_insert(s->map, color_used, &nb_used, s->palette, &box);
727
728     if (s->dot_filename)
729         disp_tree(s->map, s->dot_filename);
730
731     if (s->debug_accuracy) {
732         if (!debug_accuracy(s->map, s->palette, s->color_search_method))
733             av_log(NULL, AV_LOG_INFO, "Accuracy check passed\n");
734     }
735 }
736
737 static void debug_mean_error(PaletteUseContext *s, const AVFrame *in1,
738                              const AVFrame *in2, int frame_count)
739 {
740     int x, y;
741     const uint32_t *palette = s->palette;
742     uint32_t *src1 = (uint32_t *)in1->data[0];
743     uint8_t  *src2 =             in2->data[0];
744     const int src1_linesize = in1->linesize[0] >> 2;
745     const int src2_linesize = in2->linesize[0];
746     const float div = in1->width * in1->height * 3;
747     unsigned mean_err = 0;
748
749     for (y = 0; y < in1->height; y++) {
750         for (x = 0; x < in1->width; x++) {
751             const uint32_t c1 = src1[x];
752             const uint32_t c2 = palette[src2[x]];
753             const uint8_t rgb1[] = {c1 >> 16 & 0xff, c1 >> 8 & 0xff, c1 & 0xff};
754             const uint8_t rgb2[] = {c2 >> 16 & 0xff, c2 >> 8 & 0xff, c2 & 0xff};
755             mean_err += diff(rgb1, rgb2);
756         }
757         src1 += src1_linesize;
758         src2 += src2_linesize;
759     }
760
761     s->total_mean_err += mean_err;
762
763     av_log(NULL, AV_LOG_INFO, "MEP:%.3f TotalMEP:%.3f\n",
764            mean_err / div, s->total_mean_err / (div * frame_count));
765 }
766
767 static void set_processing_window(enum diff_mode diff_mode,
768                                   const AVFrame *prv_src, const AVFrame *cur_src,
769                                   const AVFrame *prv_dst,       AVFrame *cur_dst,
770                                   int *xp, int *yp, int *wp, int *hp)
771 {
772     int x_start = 0, y_start = 0;
773     int width  = cur_src->width;
774     int height = cur_src->height;
775
776     if (prv_src && diff_mode == DIFF_MODE_RECTANGLE) {
777         int y;
778         int x_end = cur_src->width  - 1,
779             y_end = cur_src->height - 1;
780         const uint32_t *prv_srcp = (const uint32_t *)prv_src->data[0];
781         const uint32_t *cur_srcp = (const uint32_t *)cur_src->data[0];
782         const uint8_t  *prv_dstp = prv_dst->data[0];
783         uint8_t        *cur_dstp = cur_dst->data[0];
784
785         const int prv_src_linesize = prv_src->linesize[0] >> 2;
786         const int cur_src_linesize = cur_src->linesize[0] >> 2;
787         const int prv_dst_linesize = prv_dst->linesize[0];
788         const int cur_dst_linesize = cur_dst->linesize[0];
789
790         /* skip common lines */
791         while (y_start < y_end && !memcmp(prv_srcp + y_start*prv_src_linesize,
792                                           cur_srcp + y_start*cur_src_linesize,
793                                           cur_src->width * 4)) {
794             memcpy(cur_dstp + y_start*cur_dst_linesize,
795                    prv_dstp + y_start*prv_dst_linesize,
796                    cur_dst->width);
797             y_start++;
798         }
799         while (y_end > y_start && !memcmp(prv_srcp + y_end*prv_src_linesize,
800                                           cur_srcp + y_end*cur_src_linesize,
801                                           cur_src->width * 4)) {
802             memcpy(cur_dstp + y_end*cur_dst_linesize,
803                    prv_dstp + y_end*prv_dst_linesize,
804                    cur_dst->width);
805             y_end--;
806         }
807
808         height = y_end + 1 - y_start;
809
810         /* skip common columns */
811         while (x_start < x_end) {
812             int same_column = 1;
813             for (y = y_start; y <= y_end; y++) {
814                 if (prv_srcp[y*prv_src_linesize + x_start] != cur_srcp[y*cur_src_linesize + x_start]) {
815                     same_column = 0;
816                     break;
817                 }
818             }
819             if (!same_column)
820                 break;
821             x_start++;
822         }
823         while (x_end > x_start) {
824             int same_column = 1;
825             for (y = y_start; y <= y_end; y++) {
826                 if (prv_srcp[y*prv_src_linesize + x_end] != cur_srcp[y*cur_src_linesize + x_end]) {
827                     same_column = 0;
828                     break;
829                 }
830             }
831             if (!same_column)
832                 break;
833             x_end--;
834         }
835         width = x_end + 1 - x_start;
836
837         if (x_start) {
838             for (y = y_start; y <= y_end; y++)
839                 memcpy(cur_dstp + y*cur_dst_linesize,
840                        prv_dstp + y*prv_dst_linesize, x_start);
841         }
842         if (x_end != cur_src->width - 1) {
843             const int copy_len = cur_src->width - 1 - x_end;
844             for (y = y_start; y <= y_end; y++)
845                 memcpy(cur_dstp + y*cur_dst_linesize + x_end + 1,
846                        prv_dstp + y*prv_dst_linesize + x_end + 1,
847                        copy_len);
848         }
849     }
850     *xp = x_start;
851     *yp = y_start;
852     *wp = width;
853     *hp = height;
854 }
855
856 static AVFrame *apply_palette(AVFilterLink *inlink, AVFrame *in)
857 {
858     int x, y, w, h;
859     AVFilterContext *ctx = inlink->dst;
860     PaletteUseContext *s = ctx->priv;
861     AVFilterLink *outlink = inlink->dst->outputs[0];
862
863     AVFrame *out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
864     if (!out) {
865         av_frame_free(&in);
866         return NULL;
867     }
868     av_frame_copy_props(out, in);
869
870     set_processing_window(s->diff_mode, s->last_in, in,
871                           s->last_out, out, &x, &y, &w, &h);
872     av_frame_free(&s->last_in);
873     av_frame_free(&s->last_out);
874     s->last_in  = av_frame_clone(in);
875     s->last_out = av_frame_clone(out);
876     if (!s->last_in || !s->last_out ||
877         av_frame_make_writable(s->last_in) < 0) {
878         av_frame_free(&in);
879         av_frame_free(&out);
880         return NULL;
881     }
882
883     ff_dlog(ctx, "%dx%d rect: (%d;%d) -> (%d,%d) [area:%dx%d]\n",
884             w, h, x, y, x+w, y+h, in->width, in->height);
885
886     if (s->set_frame(s, out, in, x, y, w, h) < 0) {
887         av_frame_free(&out);
888         return NULL;
889     }
890     memcpy(out->data[1], s->palette, AVPALETTE_SIZE);
891     if (s->calc_mean_err)
892         debug_mean_error(s, in, out, inlink->frame_count_out);
893     av_frame_free(&in);
894     return out;
895 }
896
897 static int config_output(AVFilterLink *outlink)
898 {
899     int ret;
900     AVFilterContext *ctx = outlink->src;
901     PaletteUseContext *s = ctx->priv;
902
903     outlink->w = ctx->inputs[0]->w;
904     outlink->h = ctx->inputs[0]->h;
905
906     outlink->time_base = ctx->inputs[0]->time_base;
907     if ((ret = ff_dualinput_init(ctx, &s->dinput)) < 0)
908         return ret;
909     return 0;
910 }
911
912 static int config_input_palette(AVFilterLink *inlink)
913 {
914     AVFilterContext *ctx = inlink->dst;
915
916     if (inlink->w * inlink->h != AVPALETTE_COUNT) {
917         av_log(ctx, AV_LOG_ERROR,
918                "Palette input must contain exactly %d pixels. "
919                "Specified input has %dx%d=%d pixels\n",
920                AVPALETTE_COUNT, inlink->w, inlink->h,
921                inlink->w * inlink->h);
922         return AVERROR(EINVAL);
923     }
924     return 0;
925 }
926
927 static void load_palette(PaletteUseContext *s, const AVFrame *palette_frame)
928 {
929     int i, x, y;
930     const uint32_t *p = (const uint32_t *)palette_frame->data[0];
931     const int p_linesize = palette_frame->linesize[0] >> 2;
932
933     if (s->new) {
934         memset(s->palette, 0, sizeof(s->palette));
935         memset(s->map, 0, sizeof(s->map));
936         for (i = 0; i < CACHE_SIZE; i++)
937             av_freep(&s->cache[i].entries);
938         memset(s->cache, 0, sizeof(s->cache));
939     }
940
941     i = 0;
942     for (y = 0; y < palette_frame->height; y++) {
943         for (x = 0; x < palette_frame->width; x++)
944             s->palette[i++] = p[x];
945         p += p_linesize;
946     }
947
948     load_colormap(s);
949
950     if (!s->new)
951         s->palette_loaded = 1;
952 }
953
954 static AVFrame *load_apply_palette(AVFilterContext *ctx, AVFrame *main,
955                                    const AVFrame *second)
956 {
957     AVFilterLink *inlink = ctx->inputs[0];
958     PaletteUseContext *s = ctx->priv;
959     if (!s->palette_loaded) {
960         load_palette(s, second);
961     }
962     return apply_palette(inlink, main);
963 }
964
965 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
966 {
967     PaletteUseContext *s = inlink->dst->priv;
968     return ff_dualinput_filter_frame(&s->dinput, inlink, in);
969 }
970
971 #define DEFINE_SET_FRAME(color_search, name, value)                             \
972 static int set_frame_##name(PaletteUseContext *s, AVFrame *out, AVFrame *in,    \
973                             int x_start, int y_start, int w, int h)             \
974 {                                                                               \
975     return set_frame(s, out, in, x_start, y_start, w, h, value, color_search);  \
976 }
977
978 #define DEFINE_SET_FRAME_COLOR_SEARCH(color_search, color_search_macro)                                 \
979     DEFINE_SET_FRAME(color_search_macro, color_search##_##none,            DITHERING_NONE)              \
980     DEFINE_SET_FRAME(color_search_macro, color_search##_##bayer,           DITHERING_BAYER)             \
981     DEFINE_SET_FRAME(color_search_macro, color_search##_##heckbert,        DITHERING_HECKBERT)          \
982     DEFINE_SET_FRAME(color_search_macro, color_search##_##floyd_steinberg, DITHERING_FLOYD_STEINBERG)   \
983     DEFINE_SET_FRAME(color_search_macro, color_search##_##sierra2,         DITHERING_SIERRA2)           \
984     DEFINE_SET_FRAME(color_search_macro, color_search##_##sierra2_4a,      DITHERING_SIERRA2_4A)        \
985
986 DEFINE_SET_FRAME_COLOR_SEARCH(nns_iterative, COLOR_SEARCH_NNS_ITERATIVE)
987 DEFINE_SET_FRAME_COLOR_SEARCH(nns_recursive, COLOR_SEARCH_NNS_RECURSIVE)
988 DEFINE_SET_FRAME_COLOR_SEARCH(bruteforce,    COLOR_SEARCH_BRUTEFORCE)
989
990 #define DITHERING_ENTRIES(color_search) {       \
991     set_frame_##color_search##_none,            \
992     set_frame_##color_search##_bayer,           \
993     set_frame_##color_search##_heckbert,        \
994     set_frame_##color_search##_floyd_steinberg, \
995     set_frame_##color_search##_sierra2,         \
996     set_frame_##color_search##_sierra2_4a,      \
997 }
998
999 static const set_frame_func set_frame_lut[NB_COLOR_SEARCHES][NB_DITHERING] = {
1000     DITHERING_ENTRIES(nns_iterative),
1001     DITHERING_ENTRIES(nns_recursive),
1002     DITHERING_ENTRIES(bruteforce),
1003 };
1004
1005 static int dither_value(int p)
1006 {
1007     const int q = p ^ (p >> 3);
1008     return   (p & 4) >> 2 | (q & 4) >> 1 \
1009            | (p & 2) << 1 | (q & 2) << 2 \
1010            | (p & 1) << 4 | (q & 1) << 5;
1011 }
1012
1013 static av_cold int init(AVFilterContext *ctx)
1014 {
1015     PaletteUseContext *s = ctx->priv;
1016     s->dinput.repeatlast = 1; // only 1 frame in the palette
1017     s->dinput.skip_initial_unpaired = 1;
1018     s->dinput.process    = load_apply_palette;
1019
1020     s->set_frame = set_frame_lut[s->color_search_method][s->dither];
1021
1022     if (s->dither == DITHERING_BAYER) {
1023         int i;
1024         const int delta = 1 << (5 - s->bayer_scale); // to avoid too much luma
1025
1026         for (i = 0; i < FF_ARRAY_ELEMS(s->ordered_dither); i++)
1027             s->ordered_dither[i] = (dither_value(i) >> s->bayer_scale) - delta;
1028     }
1029
1030     return 0;
1031 }
1032
1033 static int request_frame(AVFilterLink *outlink)
1034 {
1035     PaletteUseContext *s = outlink->src->priv;
1036     return ff_dualinput_request_frame(&s->dinput, outlink);
1037 }
1038
1039 static av_cold void uninit(AVFilterContext *ctx)
1040 {
1041     int i;
1042     PaletteUseContext *s = ctx->priv;
1043
1044     ff_dualinput_uninit(&s->dinput);
1045     for (i = 0; i < CACHE_SIZE; i++)
1046         av_freep(&s->cache[i].entries);
1047     av_frame_free(&s->last_in);
1048     av_frame_free(&s->last_out);
1049 }
1050
1051 static const AVFilterPad paletteuse_inputs[] = {
1052     {
1053         .name           = "default",
1054         .type           = AVMEDIA_TYPE_VIDEO,
1055         .filter_frame   = filter_frame,
1056         .needs_writable = 1, // for error diffusal dithering
1057     },{
1058         .name           = "palette",
1059         .type           = AVMEDIA_TYPE_VIDEO,
1060         .config_props   = config_input_palette,
1061         .filter_frame   = filter_frame,
1062     },
1063     { NULL }
1064 };
1065
1066 static const AVFilterPad paletteuse_outputs[] = {
1067     {
1068         .name          = "default",
1069         .type          = AVMEDIA_TYPE_VIDEO,
1070         .config_props  = config_output,
1071         .request_frame = request_frame,
1072     },
1073     { NULL }
1074 };
1075
1076 AVFilter ff_vf_paletteuse = {
1077     .name          = "paletteuse",
1078     .description   = NULL_IF_CONFIG_SMALL("Use a palette to downsample an input video stream."),
1079     .priv_size     = sizeof(PaletteUseContext),
1080     .query_formats = query_formats,
1081     .init          = init,
1082     .uninit        = uninit,
1083     .inputs        = paletteuse_inputs,
1084     .outputs       = paletteuse_outputs,
1085     .priv_class    = &paletteuse_class,
1086 };