FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_paletteuse.c
Date: 2022-11-26 13:19:19
Exec Total Coverage
Lines: 385 582 66.2%
Branches: 235 436 53.9%

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