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