FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_paletteuse.c
Date: 2024-03-28 14:59:00
Exec Total Coverage
Lines: 355 506 70.2%
Functions: 29 37 78.4%
Branches: 215 368 58.4%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2015 Stupeflix
3 * Copyright (c) 2022 Clément Bœsch <u pkh me>
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /**
23 * @file
24 * Use a palette to downsample an input video stream.
25 */
26
27 #include "libavutil/bprint.h"
28 #include "libavutil/file_open.h"
29 #include "libavutil/internal.h"
30 #include "libavutil/opt.h"
31 #include "libavutil/qsort.h"
32 #include "avfilter.h"
33 #include "filters.h"
34 #include "formats.h"
35 #include "framesync.h"
36 #include "internal.h"
37 #include "palette.h"
38 #include "video.h"
39
40 enum dithering_mode {
41 DITHERING_NONE,
42 DITHERING_BAYER,
43 DITHERING_HECKBERT,
44 DITHERING_FLOYD_STEINBERG,
45 DITHERING_SIERRA2,
46 DITHERING_SIERRA2_4A,
47 DITHERING_SIERRA3,
48 DITHERING_BURKES,
49 DITHERING_ATKINSON,
50 NB_DITHERING
51 };
52
53 enum diff_mode {
54 DIFF_MODE_NONE,
55 DIFF_MODE_RECTANGLE,
56 NB_DIFF_MODE
57 };
58
59 struct color_info {
60 uint32_t srgb;
61 int32_t lab[3];
62 };
63
64 struct color_node {
65 struct color_info c;
66 uint8_t palette_id;
67 int split;
68 int left_id, right_id;
69 };
70
71 #define CACHE_SIZE (1<<15)
72
73 struct cached_color {
74 uint32_t color;
75 uint8_t pal_entry;
76 };
77
78 struct cache_node {
79 struct cached_color *entries;
80 int nb_entries;
81 };
82
83 struct PaletteUseContext;
84
85 typedef int (*set_frame_func)(struct PaletteUseContext *s, AVFrame *out, AVFrame *in,
86 int x_start, int y_start, int width, int height);
87
88 typedef struct PaletteUseContext {
89 const AVClass *class;
90 FFFrameSync fs;
91 struct cache_node cache[CACHE_SIZE]; /* lookup cache */
92 struct color_node map[AVPALETTE_COUNT]; /* 3D-Tree (KD-Tree with K=3) for reverse colormap */
93 uint32_t palette[AVPALETTE_COUNT];
94 int transparency_index; /* index in the palette of transparency. -1 if there is no transparency in the palette. */
95 int trans_thresh;
96 int palette_loaded;
97 int dither;
98 int new;
99 set_frame_func set_frame;
100 int bayer_scale;
101 int ordered_dither[8*8];
102 int diff_mode;
103 AVFrame *last_in;
104 AVFrame *last_out;
105
106 /* debug options */
107 char *dot_filename;
108 int calc_mean_err;
109 uint64_t total_mean_err;
110 } PaletteUseContext;
111
112 #define OFFSET(x) offsetof(PaletteUseContext, x)
113 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
114 static const AVOption paletteuse_options[] = {
115 { "dither", "select dithering mode", OFFSET(dither), AV_OPT_TYPE_INT, {.i64=DITHERING_SIERRA2_4A}, 0, NB_DITHERING-1, FLAGS, .unit = "dithering_mode" },
116 { "bayer", "ordered 8x8 bayer dithering (deterministic)", 0, AV_OPT_TYPE_CONST, {.i64=DITHERING_BAYER}, INT_MIN, INT_MAX, FLAGS, .unit = "dithering_mode" },
117 { "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, .unit = "dithering_mode" },
118 { "floyd_steinberg", "Floyd and Steingberg dithering (error diffusion)", 0, AV_OPT_TYPE_CONST, {.i64=DITHERING_FLOYD_STEINBERG}, INT_MIN, INT_MAX, FLAGS, .unit = "dithering_mode" },
119 { "sierra2", "Frankie Sierra dithering v2 (error diffusion)", 0, AV_OPT_TYPE_CONST, {.i64=DITHERING_SIERRA2}, INT_MIN, INT_MAX, FLAGS, .unit = "dithering_mode" },
120 { "sierra2_4a", "Frankie Sierra dithering v2 \"Lite\" (error diffusion)", 0, AV_OPT_TYPE_CONST, {.i64=DITHERING_SIERRA2_4A}, INT_MIN, INT_MAX, FLAGS, .unit = "dithering_mode" },
121 { "sierra3", "Frankie Sierra dithering v3 (error diffusion)", 0, AV_OPT_TYPE_CONST, {.i64=DITHERING_SIERRA3}, INT_MIN, INT_MAX, FLAGS, .unit = "dithering_mode" },
122 { "burkes", "Burkes dithering (error diffusion)", 0, AV_OPT_TYPE_CONST, {.i64=DITHERING_BURKES}, INT_MIN, INT_MAX, FLAGS, .unit = "dithering_mode" },
123 { "atkinson", "Atkinson dithering by Bill Atkinson at Apple Computer (error diffusion)",0, AV_OPT_TYPE_CONST, {.i64=DITHERING_ATKINSON}, INT_MIN, INT_MAX, FLAGS, .unit = "dithering_mode" },
124 { "bayer_scale", "set scale for bayer dithering", OFFSET(bayer_scale), AV_OPT_TYPE_INT, {.i64=2}, 0, 5, FLAGS },
125 { "diff_mode", "set frame difference mode", OFFSET(diff_mode), AV_OPT_TYPE_INT, {.i64=DIFF_MODE_NONE}, 0, NB_DIFF_MODE-1, FLAGS, .unit = "diff_mode" },
126 { "rectangle", "process smallest different rectangle", 0, AV_OPT_TYPE_CONST, {.i64=DIFF_MODE_RECTANGLE}, INT_MIN, INT_MAX, FLAGS, .unit = "diff_mode" },
127 { "new", "take new palette for each output frame", OFFSET(new), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
128 { "alpha_threshold", "set the alpha threshold for transparency", OFFSET(trans_thresh), AV_OPT_TYPE_INT, {.i64=128}, 0, 255, FLAGS },
129
130 /* following are the debug options, not part of the official API */
131 { "debug_kdtree", "save Graphviz graph of the kdtree in specified file", OFFSET(dot_filename), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
132 { NULL }
133 };
134
135 AVFILTER_DEFINE_CLASS(paletteuse);
136
137 static int load_apply_palette(FFFrameSync *fs);
138
139 4 static int query_formats(AVFilterContext *ctx)
140 {
141 static const enum AVPixelFormat in_fmts[] = {AV_PIX_FMT_RGB32, AV_PIX_FMT_NONE};
142 static const enum AVPixelFormat inpal_fmts[] = {AV_PIX_FMT_RGB32, AV_PIX_FMT_NONE};
143 static const enum AVPixelFormat out_fmts[] = {AV_PIX_FMT_PAL8, AV_PIX_FMT_NONE};
144 int ret;
145
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 if ((ret = ff_formats_ref(ff_make_format_list(in_fmts),
146
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
8 &ctx->inputs[0]->outcfg.formats)) < 0 ||
147 4 (ret = ff_formats_ref(ff_make_format_list(inpal_fmts),
148
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
8 &ctx->inputs[1]->outcfg.formats)) < 0 ||
149 4 (ret = ff_formats_ref(ff_make_format_list(out_fmts),
150 4 &ctx->outputs[0]->incfg.formats)) < 0)
151 return ret;
152 4 return 0;
153 }
154
155 11948846 static av_always_inline uint32_t dither_color(uint32_t px, int er, int eg,
156 int eb, int scale, int shift)
157 {
158 11948846 return (px & 0xff000000)
159 11948846 | av_clip_uint8((px >> 16 & 0xff) + ((er * scale) / (1<<shift))) << 16
160 11948846 | av_clip_uint8((px >> 8 & 0xff) + ((eg * scale) / (1<<shift))) << 8
161 11948846 | av_clip_uint8((px & 0xff) + ((eb * scale) / (1<<shift)));
162 }
163
164 10265674 static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)
165 {
166 10265674 const uint8_t alpha_a = a->srgb >> 24;
167 10265674 const uint8_t alpha_b = b->srgb >> 24;
168
169
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 10265674 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
10265674 if (alpha_a < trans_thresh && alpha_b < trans_thresh) {
170 return 0;
171
2/4
✓ Branch 0 taken 10265674 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10265674 times.
✗ Branch 3 not taken.
10265674 } else if (alpha_a >= trans_thresh && alpha_b >= trans_thresh) {
172 10265674 const int64_t dL = a->lab[0] - b->lab[0];
173 10265674 const int64_t da = a->lab[1] - b->lab[1];
174 10265674 const int64_t db = a->lab[2] - b->lab[2];
175 10265674 const int64_t ret = dL*dL + da*da + db*db;
176 10265674 return FFMIN(ret, INT32_MAX - 1);
177 } else {
178 return INT32_MAX - 1;
179 }
180 }
181
182 487922 static struct color_info get_color_from_srgb(uint32_t srgb)
183 {
184 487922 const struct Lab lab = ff_srgb_u8_to_oklab_int(srgb);
185 487922 struct color_info ret = {.srgb=srgb, .lab={lab.L, lab.a, lab.b}};
186 487922 return ret;
187 }
188
189 struct nearest_color {
190 int node_pos;
191 int64_t dist_sqd;
192 };
193
194 10265674 static void colormap_nearest_node(const struct color_node *map,
195 const int node_pos,
196 const struct color_info *target,
197 const int trans_thresh,
198 struct nearest_color *nearest)
199 {
200 10265674 const struct color_node *kd = map + node_pos;
201 int nearer_kd_id, further_kd_id;
202 10265674 const struct color_info *current = &kd->c;
203 10265674 const int64_t current_to_target = diff(target, current, trans_thresh);
204
205
2/2
✓ Branch 0 taken 2506085 times.
✓ Branch 1 taken 7759589 times.
10265674 if (current_to_target < nearest->dist_sqd) {
206 2506085 nearest->node_pos = node_pos;
207 2506085 nearest->dist_sqd = current_to_target;
208 }
209
210
3/4
✓ Branch 0 taken 3281037 times.
✓ Branch 1 taken 6984637 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3281037 times.
10265674 if (kd->left_id != -1 || kd->right_id != -1) {
211 6984637 const int64_t dx = target->lab[kd->split] - current->lab[kd->split];
212
213
2/2
✓ Branch 0 taken 3678406 times.
✓ Branch 1 taken 3306231 times.
6984637 if (dx <= 0) nearer_kd_id = kd->left_id, further_kd_id = kd->right_id;
214 3306231 else nearer_kd_id = kd->right_id, further_kd_id = kd->left_id;
215
216
2/2
✓ Branch 0 taken 6966650 times.
✓ Branch 1 taken 17987 times.
6984637 if (nearer_kd_id != -1)
217 6966650 colormap_nearest_node(map, nearer_kd_id, target, trans_thresh, nearest);
218
219
4/4
✓ Branch 0 taken 6954929 times.
✓ Branch 1 taken 29708 times.
✓ Branch 2 taken 2812122 times.
✓ Branch 3 taken 4142807 times.
6984637 if (further_kd_id != -1 && dx*dx < nearest->dist_sqd)
220 2812122 colormap_nearest_node(map, further_kd_id, target, trans_thresh, nearest);
221 }
222 10265674 }
223
224 486902 static av_always_inline uint8_t colormap_nearest(const struct color_node *node, const struct color_info *target, const int trans_thresh)
225 {
226 486902 struct nearest_color res = {.dist_sqd = INT_MAX, .node_pos = -1};
227 486902 colormap_nearest_node(node, 0, target, trans_thresh, &res);
228 486902 return node[res.node_pos].palette_id;
229 }
230
231 struct stack_node {
232 int color_id;
233 int dx2;
234 };
235
236 /**
237 * Check if the requested color is in the cache already. If not, find it in the
238 * color tree and cache it.
239 */
240 16275189 static av_always_inline int color_get(PaletteUseContext *s, uint32_t color)
241 {
242 struct color_info clrinfo;
243 16275189 const uint32_t hash = ff_lowbias32(color) & (CACHE_SIZE - 1);
244 16275189 struct cache_node *node = &s->cache[hash];
245 struct cached_color *e;
246
247 // first, check for transparency
248
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 16275189 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
16275189 if (color>>24 < s->trans_thresh && s->transparency_index >= 0) {
249 return s->transparency_index;
250 }
251
252
2/2
✓ Branch 0 taken 28679153 times.
✓ Branch 1 taken 486902 times.
29166055 for (int i = 0; i < node->nb_entries; i++) {
253 28679153 e = &node->entries[i];
254
2/2
✓ Branch 0 taken 15788287 times.
✓ Branch 1 taken 12890866 times.
28679153 if (e->color == color)
255 15788287 return e->pal_entry;
256 }
257
258 486902 e = av_dynarray2_add((void**)&node->entries, &node->nb_entries,
259 sizeof(*node->entries), NULL);
260
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 486902 times.
486902 if (!e)
261 return AVERROR(ENOMEM);
262 486902 e->color = color;
263 486902 clrinfo = get_color_from_srgb(color);
264 486902 e->pal_entry = colormap_nearest(s->map, &clrinfo, s->trans_thresh);
265
266 486902 return e->pal_entry;
267 }
268
269 4006389 static av_always_inline int get_dst_color_err(PaletteUseContext *s,
270 uint32_t c, int *er, int *eg, int *eb)
271 {
272 uint32_t dstc;
273 4006389 const int dstx = color_get(s, c);
274
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4006389 times.
4006389 if (dstx < 0)
275 return dstx;
276 4006389 dstc = s->palette[dstx];
277
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4006389 times.
4006389 if (dstx == s->transparency_index) {
278 *er = *eg = *eb = 0;
279 } else {
280 4006389 const uint8_t r = c >> 16 & 0xff;
281 4006389 const uint8_t g = c >> 8 & 0xff;
282 4006389 const uint8_t b = c & 0xff;
283 4006389 *er = (int)r - (int)(dstc >> 16 & 0xff);
284 4006389 *eg = (int)g - (int)(dstc >> 8 & 0xff);
285 4006389 *eb = (int)b - (int)(dstc & 0xff);
286 }
287 4006389 return dstx;
288 }
289
290 284 static av_always_inline int set_frame(PaletteUseContext *s, AVFrame *out, AVFrame *in,
291 int x_start, int y_start, int w, int h,
292 enum dithering_mode dither)
293 {
294 284 const int src_linesize = in ->linesize[0] >> 2;
295 284 const int dst_linesize = out->linesize[0];
296 284 uint32_t *src = ((uint32_t *)in ->data[0]) + y_start*src_linesize;
297 284 uint8_t *dst = out->data[0] + y_start*dst_linesize;
298
299 284 w += x_start;
300 284 h += y_start;
301
302
2/2
✓ Branch 0 taken 50911 times.
✓ Branch 1 taken 284 times.
51195 for (int y = y_start; y < h; y++) {
303
2/2
✓ Branch 0 taken 16275189 times.
✓ Branch 1 taken 50911 times.
16326100 for (int x = x_start; x < w; x++) {
304 int er, eg, eb;
305
306
2/2
✓ Branch 0 taken 8179200 times.
✓ Branch 1 taken 8095989 times.
16275189 if (dither == DITHERING_BAYER) {
307 8179200 const int d = s->ordered_dither[(y & 7)<<3 | (x & 7)];
308 8179200 const uint8_t a8 = src[x] >> 24;
309 8179200 const uint8_t r8 = src[x] >> 16 & 0xff;
310 8179200 const uint8_t g8 = src[x] >> 8 & 0xff;
311 8179200 const uint8_t b8 = src[x] & 0xff;
312 8179200 const uint8_t r = av_clip_uint8(r8 + d);
313 8179200 const uint8_t g = av_clip_uint8(g8 + d);
314 8179200 const uint8_t b = av_clip_uint8(b8 + d);
315 8179200 const uint32_t color_new = (unsigned)(a8) << 24 | r << 16 | g << 8 | b;
316 8179200 const int color = color_get(s, color_new);
317
318
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8179200 times.
8179200 if (color < 0)
319 return color;
320 8179200 dst[x] = color;
321
322
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8095989 times.
8095989 } else if (dither == DITHERING_HECKBERT) {
323 const int right = x < w - 1, down = y < h - 1;
324 const int color = get_dst_color_err(s, src[x], &er, &eg, &eb);
325
326 if (color < 0)
327 return color;
328 dst[x] = color;
329
330 if (right) src[ x + 1] = dither_color(src[ x + 1], er, eg, eb, 3, 3);
331 if ( down) src[src_linesize + x ] = dither_color(src[src_linesize + x ], er, eg, eb, 3, 3);
332 if (right && down) src[src_linesize + x + 1] = dither_color(src[src_linesize + x + 1], er, eg, eb, 2, 3);
333
334
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8095989 times.
8095989 } else if (dither == DITHERING_FLOYD_STEINBERG) {
335 const int right = x < w - 1, down = y < h - 1, left = x > x_start;
336 const int color = get_dst_color_err(s, src[x], &er, &eg, &eb);
337
338 if (color < 0)
339 return color;
340 dst[x] = color;
341
342 if (right) src[ x + 1] = dither_color(src[ x + 1], er, eg, eb, 7, 4);
343 if (left && down) src[src_linesize + x - 1] = dither_color(src[src_linesize + x - 1], er, eg, eb, 3, 4);
344 if ( down) src[src_linesize + x ] = dither_color(src[src_linesize + x ], er, eg, eb, 5, 4);
345 if (right && down) src[src_linesize + x + 1] = dither_color(src[src_linesize + x + 1], er, eg, eb, 1, 4);
346
347
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8095989 times.
8095989 } else if (dither == DITHERING_SIERRA2) {
348 const int right = x < w - 1, down = y < h - 1, left = x > x_start;
349 const int right2 = x < w - 2, left2 = x > x_start + 1;
350 const int color = get_dst_color_err(s, src[x], &er, &eg, &eb);
351
352 if (color < 0)
353 return color;
354 dst[x] = color;
355
356 if (right) src[ x + 1] = dither_color(src[ x + 1], er, eg, eb, 4, 4);
357 if (right2) src[ x + 2] = dither_color(src[ x + 2], er, eg, eb, 3, 4);
358
359 if (down) {
360 if (left2) src[ src_linesize + x - 2] = dither_color(src[ src_linesize + x - 2], er, eg, eb, 1, 4);
361 if (left) src[ src_linesize + x - 1] = dither_color(src[ src_linesize + x - 1], er, eg, eb, 2, 4);
362 if (1) src[ src_linesize + x ] = dither_color(src[ src_linesize + x ], er, eg, eb, 3, 4);
363 if (right) src[ src_linesize + x + 1] = dither_color(src[ src_linesize + x + 1], er, eg, eb, 2, 4);
364 if (right2) src[ src_linesize + x + 2] = dither_color(src[ src_linesize + x + 2], er, eg, eb, 1, 4);
365 }
366
367
2/2
✓ Branch 0 taken 4006389 times.
✓ Branch 1 taken 4089600 times.
8095989 } else if (dither == DITHERING_SIERRA2_4A) {
368 4006389 const int right = x < w - 1, down = y < h - 1, left = x > x_start;
369 4006389 const int color = get_dst_color_err(s, src[x], &er, &eg, &eb);
370
371
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4006389 times.
4006389 if (color < 0)
372 return color;
373 4006389 dst[x] = color;
374
375
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);
376
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);
377
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);
378
379
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4089600 times.
4089600 } else if (dither == DITHERING_SIERRA3) {
380 const int right = x < w - 1, down = y < h - 1, left = x > x_start;
381 const int right2 = x < w - 2, down2 = y < h - 2, left2 = x > x_start + 1;
382 const int color = get_dst_color_err(s, src[x], &er, &eg, &eb);
383
384 if (color < 0)
385 return color;
386 dst[x] = color;
387
388 if (right) src[ x + 1] = dither_color(src[ x + 1], er, eg, eb, 5, 5);
389 if (right2) src[ x + 2] = dither_color(src[ x + 2], er, eg, eb, 3, 5);
390
391 if (down) {
392 if (left2) src[src_linesize + x - 2] = dither_color(src[src_linesize + x - 2], er, eg, eb, 2, 5);
393 if (left) src[src_linesize + x - 1] = dither_color(src[src_linesize + x - 1], er, eg, eb, 4, 5);
394 if (1) src[src_linesize + x ] = dither_color(src[src_linesize + x ], er, eg, eb, 5, 5);
395 if (right) src[src_linesize + x + 1] = dither_color(src[src_linesize + x + 1], er, eg, eb, 4, 5);
396 if (right2) src[src_linesize + x + 2] = dither_color(src[src_linesize + x + 2], er, eg, eb, 2, 5);
397
398 if (down2) {
399 if (left) src[src_linesize*2 + x - 1] = dither_color(src[src_linesize*2 + x - 1], er, eg, eb, 2, 5);
400 if (1) src[src_linesize*2 + x ] = dither_color(src[src_linesize*2 + x ], er, eg, eb, 3, 5);
401 if (right) src[src_linesize*2 + x + 1] = dither_color(src[src_linesize*2 + x + 1], er, eg, eb, 2, 5);
402 }
403 }
404
405
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4089600 times.
4089600 } else if (dither == DITHERING_BURKES) {
406 const int right = x < w - 1, down = y < h - 1, left = x > x_start;
407 const int right2 = x < w - 2, left2 = x > x_start + 1;
408 const int color = get_dst_color_err(s, src[x], &er, &eg, &eb);
409
410 if (color < 0)
411 return color;
412 dst[x] = color;
413
414 if (right) src[ x + 1] = dither_color(src[ x + 1], er, eg, eb, 8, 5);
415 if (right2) src[ x + 2] = dither_color(src[ x + 2], er, eg, eb, 4, 5);
416
417 if (down) {
418 if (left2) src[src_linesize + x - 2] = dither_color(src[src_linesize + x - 2], er, eg, eb, 2, 5);
419 if (left) src[src_linesize + x - 1] = dither_color(src[src_linesize + x - 1], er, eg, eb, 4, 5);
420 if (1) src[src_linesize + x ] = dither_color(src[src_linesize + x ], er, eg, eb, 8, 5);
421 if (right) src[src_linesize + x + 1] = dither_color(src[src_linesize + x + 1], er, eg, eb, 4, 5);
422 if (right2) src[src_linesize + x + 2] = dither_color(src[src_linesize + x + 2], er, eg, eb, 2, 5);
423 }
424
425
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4089600 times.
4089600 } else if (dither == DITHERING_ATKINSON) {
426 const int right = x < w - 1, down = y < h - 1, left = x > x_start;
427 const int right2 = x < w - 2, down2 = y < h - 2;
428 const int color = get_dst_color_err(s, src[x], &er, &eg, &eb);
429
430 if (color < 0)
431 return color;
432 dst[x] = color;
433
434 if (right) src[ x + 1] = dither_color(src[ x + 1], er, eg, eb, 1, 3);
435 if (right2) src[ x + 2] = dither_color(src[ x + 2], er, eg, eb, 1, 3);
436
437 if (down) {
438 if (left) src[src_linesize + x - 1] = dither_color(src[src_linesize + x - 1], er, eg, eb, 1, 3);
439 if (1) src[src_linesize + x ] = dither_color(src[src_linesize + x ], er, eg, eb, 1, 3);
440 if (right) src[src_linesize + x + 1] = dither_color(src[src_linesize + x + 1], er, eg, eb, 1, 3);
441 if (down2) src[src_linesize*2 + x ] = dither_color(src[src_linesize*2 + x ], er, eg, eb, 1, 3);
442 }
443
444 } else {
445 4089600 const int color = color_get(s, src[x]);
446
447
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4089600 times.
4089600 if (color < 0)
448 return color;
449 4089600 dst[x] = color;
450 }
451 }
452 50911 src += src_linesize;
453 50911 dst += dst_linesize;
454 }
455 284 return 0;
456 }
457
458 #define INDENT 4
459 static void disp_node(AVBPrint *buf,
460 const struct color_node *map,
461 int parent_id, int node_id,
462 int depth)
463 {
464 const struct color_node *node = &map[node_id];
465 const uint32_t fontcolor = node->c.lab[0] > 0x7fff ? 0 : 0xffffff;
466 const int lab_comp = node->split;
467 av_bprintf(buf, "%*cnode%d ["
468 "label=\"%c%d%c%d%c%d%c\" "
469 "fillcolor=\"#%06"PRIX32"\" "
470 "fontcolor=\"#%06"PRIX32"\"]\n",
471 depth*INDENT, ' ', node->palette_id,
472 "[ "[lab_comp], node->c.lab[0],
473 "][ "[lab_comp], node->c.lab[1],
474 " ]["[lab_comp], node->c.lab[2],
475 " ]"[lab_comp],
476 node->c.srgb & 0xffffff,
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 = avpriv_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 struct color {
512 struct Lab value;
513 uint8_t pal_id;
514 };
515
516 struct color_rect {
517 int32_t min[3];
518 int32_t max[3];
519 };
520
521 typedef int (*cmp_func)(const void *, const void *);
522
523 #define DECLARE_CMP_FUNC(name) \
524 static int cmp_##name(const void *pa, const void *pb) \
525 { \
526 const struct color *a = pa; \
527 const struct color *b = pb; \
528 return FFDIFFSIGN(a->value.name, b->value.name); \
529 }
530
531 22876 DECLARE_CMP_FUNC(L)
532 3460 DECLARE_CMP_FUNC(a)
533 2692 DECLARE_CMP_FUNC(b)
534
535 static const cmp_func cmp_funcs[] = {cmp_L, cmp_a, cmp_b};
536
537 2040 static int get_next_color(const uint8_t *color_used, const uint32_t *palette,
538 int *component, const struct color_rect *box)
539 {
540 int wL, wa, wb;
541 2040 int longest = 0;
542 2040 unsigned nb_color = 0;
543 struct color_rect ranges;
544 struct color tmp_pal[256];
545 cmp_func cmpf;
546
547 2040 ranges.min[0] = ranges.min[1] = ranges.min[2] = 0xffff;
548 2040 ranges.max[0] = ranges.max[1] = ranges.max[2] = -0xffff;
549
550
2/2
✓ Branch 0 taken 522240 times.
✓ Branch 1 taken 2040 times.
524280 for (int i = 0; i < AVPALETTE_COUNT; i++) {
551 522240 const uint32_t c = palette[i];
552 522240 const uint8_t a = c >> 24;
553 522240 const struct Lab lab = ff_srgb_u8_to_oklab_int(c);
554
555
3/4
✓ Branch 0 taken 256928 times.
✓ Branch 1 taken 265312 times.
✓ Branch 2 taken 256928 times.
✗ Branch 3 not taken.
522240 if (color_used[i] || (a != 0xff) ||
556
6/6
✓ Branch 0 taken 249244 times.
✓ Branch 1 taken 7684 times.
✓ Branch 2 taken 173476 times.
✓ Branch 3 taken 75768 times.
✓ Branch 4 taken 135232 times.
✓ Branch 5 taken 38244 times.
256928 lab.L < box->min[0] || lab.a < box->min[1] || lab.b < box->min[2] ||
557
6/6
✓ Branch 0 taken 11860 times.
✓ Branch 1 taken 123372 times.
✓ Branch 2 taken 9116 times.
✓ Branch 3 taken 2744 times.
✓ Branch 4 taken 1940 times.
✓ Branch 5 taken 7176 times.
135232 lab.L > box->max[0] || lab.a > box->max[1] || lab.b > box->max[2])
558 515064 continue;
559
560
2/2
✓ Branch 0 taken 1336 times.
✓ Branch 1 taken 5840 times.
7176 if (lab.L < ranges.min[0]) ranges.min[0] = lab.L;
561
2/2
✓ Branch 0 taken 1360 times.
✓ Branch 1 taken 5816 times.
7176 if (lab.a < ranges.min[1]) ranges.min[1] = lab.a;
562
2/2
✓ Branch 0 taken 2192 times.
✓ Branch 1 taken 4984 times.
7176 if (lab.b < ranges.min[2]) ranges.min[2] = lab.b;
563
564
2/2
✓ Branch 0 taken 3312 times.
✓ Branch 1 taken 3864 times.
7176 if (lab.L > ranges.max[0]) ranges.max[0] = lab.L;
565
2/2
✓ Branch 0 taken 2416 times.
✓ Branch 1 taken 4760 times.
7176 if (lab.a > ranges.max[1]) ranges.max[1] = lab.a;
566
2/2
✓ Branch 0 taken 1636 times.
✓ Branch 1 taken 5540 times.
7176 if (lab.b > ranges.max[2]) ranges.max[2] = lab.b;
567
568 7176 tmp_pal[nb_color].value = lab;
569 7176 tmp_pal[nb_color].pal_id = i;
570
571 7176 nb_color++;
572 }
573
574
2/2
✓ Branch 0 taken 1020 times.
✓ Branch 1 taken 1020 times.
2040 if (!nb_color)
575 1020 return -1;
576
577 /* define longest axis that will be the split component */
578 1020 wL = ranges.max[0] - ranges.min[0];
579 1020 wa = ranges.max[1] - ranges.min[1];
580 1020 wb = ranges.max[2] - ranges.min[2];
581
4/4
✓ Branch 0 taken 696 times.
✓ Branch 1 taken 324 times.
✓ Branch 2 taken 604 times.
✓ Branch 3 taken 92 times.
1020 if (wb >= wL && wb >= wa) longest = 2;
582
4/4
✓ Branch 0 taken 704 times.
✓ Branch 1 taken 316 times.
✓ Branch 2 taken 656 times.
✓ Branch 3 taken 48 times.
1020 if (wa >= wL && wa >= wb) longest = 1;
583
4/4
✓ Branch 0 taken 824 times.
✓ Branch 1 taken 196 times.
✓ Branch 2 taken 776 times.
✓ Branch 3 taken 48 times.
1020 if (wL >= wa && wL >= wb) longest = 0;
584 1020 cmpf = cmp_funcs[longest];
585 1020 *component = longest;
586
587 /* sort along this axis to get median */
588
44/44
✓ Branch 0 taken 2628 times.
✓ Branch 1 taken 780 times.
✓ Branch 3 taken 864 times.
✓ Branch 4 taken 1764 times.
✓ Branch 6 taken 308 times.
✓ Branch 7 taken 556 times.
✓ Branch 9 taken 444 times.
✓ Branch 10 taken 1320 times.
✓ Branch 12 taken 1008 times.
✓ Branch 13 taken 1620 times.
✓ Branch 14 taken 760 times.
✓ Branch 15 taken 1868 times.
✓ Branch 16 taken 10372 times.
✓ Branch 17 taken 684 times.
✓ Branch 19 taken 7536 times.
✓ Branch 20 taken 2836 times.
✓ Branch 21 taken 9036 times.
✓ Branch 22 taken 1564 times.
✓ Branch 24 taken 7080 times.
✓ Branch 25 taken 1956 times.
✓ Branch 26 taken 1564 times.
✓ Branch 27 taken 1956 times.
✓ Branch 28 taken 3520 times.
✓ Branch 29 taken 1868 times.
✓ Branch 30 taken 640 times.
✓ Branch 31 taken 1228 times.
✓ Branch 32 taken 508 times.
✓ Branch 33 taken 132 times.
✓ Branch 34 taken 196 times.
✓ Branch 35 taken 312 times.
✓ Branch 36 taken 956 times.
✓ Branch 37 taken 56 times.
✓ Branch 39 taken 684 times.
✓ Branch 40 taken 272 times.
✓ Branch 41 taken 56 times.
✓ Branch 42 taken 272 times.
✓ Branch 43 taken 828 times.
✓ Branch 44 taken 984 times.
✓ Branch 46 taken 420 times.
✓ Branch 47 taken 360 times.
✓ Branch 48 taken 3408 times.
✓ Branch 49 taken 1236 times.
✓ Branch 50 taken 2832 times.
✓ Branch 51 taken 1020 times.
26352 AV_QSORT(tmp_pal, nb_color, struct color, cmpf);
589
590 1020 return tmp_pal[nb_color >> 1].pal_id;
591 }
592
593 2040 static int colormap_insert(struct color_node *map,
594 uint8_t *color_used,
595 int *nb_used,
596 const uint32_t *palette,
597 const int trans_thresh,
598 const struct color_rect *box)
599 {
600 int component, cur_id;
601 int comp_value;
602 2040 int node_left_id = -1, node_right_id = -1;
603 struct color_node *node;
604 struct color_rect box1, box2;
605 2040 const int pal_id = get_next_color(color_used, palette, &component, box);
606
607
2/2
✓ Branch 0 taken 1020 times.
✓ Branch 1 taken 1020 times.
2040 if (pal_id < 0)
608 1020 return -1;
609
610 /* create new node with that color */
611 1020 cur_id = (*nb_used)++;
612 1020 node = &map[cur_id];
613 1020 node->split = component;
614 1020 node->palette_id = pal_id;
615 1020 node->c = get_color_from_srgb(palette[pal_id]);
616
617 1020 color_used[pal_id] = 1;
618
619 /* get the two boxes this node creates */
620 1020 box1 = box2 = *box;
621 1020 comp_value = node->c.lab[component];
622 1020 box1.max[component] = comp_value;
623 1020 box2.min[component] = FFMIN(comp_value + 1, 0xffff);
624
625 1020 node_left_id = colormap_insert(map, color_used, nb_used, palette, trans_thresh, &box1);
626
627
2/2
✓ Branch 0 taken 1016 times.
✓ Branch 1 taken 4 times.
1020 if (box2.min[component] <= box2.max[component])
628 1016 node_right_id = colormap_insert(map, color_used, nb_used, palette, trans_thresh, &box2);
629
630 1020 node->left_id = node_left_id;
631 1020 node->right_id = node_right_id;
632
633 1020 return cur_id;
634 }
635
636 4064 static int cmp_pal_entry(const void *a, const void *b)
637 {
638 4064 const int c1 = *(const uint32_t *)a & 0xffffff;
639 4064 const int c2 = *(const uint32_t *)b & 0xffffff;
640 4064 return c1 - c2;
641 }
642
643 4 static void load_colormap(PaletteUseContext *s)
644 {
645 4 int nb_used = 0;
646 4 uint8_t color_used[AVPALETTE_COUNT] = {0};
647 4 uint32_t last_color = 0;
648 struct color_rect box;
649
650
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (s->transparency_index >= 0) {
651 4 FFSWAP(uint32_t, s->palette[s->transparency_index], s->palette[255]);
652 }
653
654 /* disable transparent colors and dups */
655
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 qsort(s->palette, AVPALETTE_COUNT-(s->transparency_index >= 0), sizeof(*s->palette), cmp_pal_entry);
656
657
2/2
✓ Branch 0 taken 1024 times.
✓ Branch 1 taken 4 times.
1028 for (int i = 0; i < AVPALETTE_COUNT; i++) {
658 1024 const uint32_t c = s->palette[i];
659
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) {
660 color_used[i] = 1;
661 continue;
662 }
663 1024 last_color = c;
664
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1020 times.
1024 if (c >> 24 < s->trans_thresh) {
665 4 color_used[i] = 1; // ignore transparent color(s)
666 4 continue;
667 }
668 }
669
670 4 box.min[0] = box.min[1] = box.min[2] = -0xffff;
671 4 box.max[0] = box.max[1] = box.max[2] = 0xffff;
672
673 4 colormap_insert(s->map, color_used, &nb_used, s->palette, s->trans_thresh, &box);
674
675
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (s->dot_filename)
676 disp_tree(s->map, s->dot_filename);
677 4 }
678
679 284 static void set_processing_window(enum diff_mode diff_mode,
680 const AVFrame *prv_src, const AVFrame *cur_src,
681 const AVFrame *prv_dst, AVFrame *cur_dst,
682 int *xp, int *yp, int *wp, int *hp)
683 {
684 284 int x_start = 0, y_start = 0;
685 284 int width = cur_src->width;
686 284 int height = cur_src->height;
687
688
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) {
689 int y;
690 70 int x_end = cur_src->width - 1,
691 70 y_end = cur_src->height - 1;
692 70 const uint32_t *prv_srcp = (const uint32_t *)prv_src->data[0];
693 70 const uint32_t *cur_srcp = (const uint32_t *)cur_src->data[0];
694 70 const uint8_t *prv_dstp = prv_dst->data[0];
695 70 uint8_t *cur_dstp = cur_dst->data[0];
696
697 70 const int prv_src_linesize = prv_src->linesize[0] >> 2;
698 70 const int cur_src_linesize = cur_src->linesize[0] >> 2;
699 70 const int prv_dst_linesize = prv_dst->linesize[0];
700 70 const int cur_dst_linesize = cur_dst->linesize[0];
701
702 /* skip common lines */
703
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,
704 259 cur_srcp + y_start*cur_src_linesize,
705
2/2
✓ Branch 0 taken 189 times.
✓ Branch 1 taken 70 times.
259 cur_src->width * 4)) {
706 189 memcpy(cur_dstp + y_start*cur_dst_linesize,
707 189 prv_dstp + y_start*prv_dst_linesize,
708 189 cur_dst->width);
709 189 y_start++;
710 }
711
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,
712 90 cur_srcp + y_end*cur_src_linesize,
713
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 70 times.
90 cur_src->width * 4)) {
714 20 memcpy(cur_dstp + y_end*cur_dst_linesize,
715 20 prv_dstp + y_end*prv_dst_linesize,
716 20 cur_dst->width);
717 20 y_end--;
718 }
719
720 70 height = y_end + 1 - y_start;
721
722 /* skip common columns */
723
1/2
✓ Branch 0 taken 138 times.
✗ Branch 1 not taken.
138 while (x_start < x_end) {
724 138 int same_column = 1;
725
2/2
✓ Branch 0 taken 17740 times.
✓ Branch 1 taken 68 times.
17808 for (y = y_start; y <= y_end; y++) {
726
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]) {
727 70 same_column = 0;
728 70 break;
729 }
730 }
731
2/2
✓ Branch 0 taken 70 times.
✓ Branch 1 taken 68 times.
138 if (!same_column)
732 70 break;
733 68 x_start++;
734 }
735
1/2
✓ Branch 0 taken 97 times.
✗ Branch 1 not taken.
97 while (x_end > x_start) {
736 97 int same_column = 1;
737
2/2
✓ Branch 0 taken 9281 times.
✓ Branch 1 taken 27 times.
9308 for (y = y_start; y <= y_end; y++) {
738
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]) {
739 70 same_column = 0;
740 70 break;
741 }
742 }
743
2/2
✓ Branch 0 taken 70 times.
✓ Branch 1 taken 27 times.
97 if (!same_column)
744 70 break;
745 27 x_end--;
746 }
747 70 width = x_end + 1 - x_start;
748
749
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 47 times.
70 if (x_start) {
750
2/2
✓ Branch 0 taken 3999 times.
✓ Branch 1 taken 23 times.
4022 for (y = y_start; y <= y_end; y++)
751 3999 memcpy(cur_dstp + y*cur_dst_linesize,
752 3999 prv_dstp + y*prv_dst_linesize, x_start);
753 }
754
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 56 times.
70 if (x_end != cur_src->width - 1) {
755 14 const int copy_len = cur_src->width - 1 - x_end;
756
2/2
✓ Branch 0 taken 2460 times.
✓ Branch 1 taken 14 times.
2474 for (y = y_start; y <= y_end; y++)
757 2460 memcpy(cur_dstp + y*cur_dst_linesize + x_end + 1,
758 2460 prv_dstp + y*prv_dst_linesize + x_end + 1,
759 copy_len);
760 }
761 }
762 284 *xp = x_start;
763 284 *yp = y_start;
764 284 *wp = width;
765 284 *hp = height;
766 284 }
767
768 284 static int apply_palette(AVFilterLink *inlink, AVFrame *in, AVFrame **outf)
769 {
770 int x, y, w, h, ret;
771 284 AVFilterContext *ctx = inlink->dst;
772 284 PaletteUseContext *s = ctx->priv;
773 284 AVFilterLink *outlink = inlink->dst->outputs[0];
774
775 284 AVFrame *out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
776
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 284 times.
284 if (!out) {
777 *outf = NULL;
778 return AVERROR(ENOMEM);
779 }
780 284 av_frame_copy_props(out, in);
781
782 284 set_processing_window(s->diff_mode, s->last_in, in,
783 284 s->last_out, out, &x, &y, &w, &h);
784 284 av_frame_unref(s->last_out);
785
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_replace(s->last_in, in)) < 0 ||
786
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 284 times.
568 (ret = av_frame_ref(s->last_out, out)) < 0 ||
787 284 (ret = ff_inlink_make_frame_writable(inlink, &s->last_in)) < 0) {
788 av_frame_free(&out);
789 *outf = NULL;
790 return ret;
791 }
792
793 ff_dlog(ctx, "%dx%d rect: (%d;%d) -> (%d,%d) [area:%dx%d]\n",
794 w, h, x, y, x+w, y+h, in->width, in->height);
795
796 284 ret = s->set_frame(s, out, in, x, y, w, h);
797
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 284 times.
284 if (ret < 0) {
798 av_frame_free(&out);
799 *outf = NULL;
800 return ret;
801 }
802 284 memcpy(out->data[1], s->palette, AVPALETTE_SIZE);
803 284 *outf = out;
804 284 return 0;
805 }
806
807 4 static int config_output(AVFilterLink *outlink)
808 {
809 int ret;
810 4 AVFilterContext *ctx = outlink->src;
811 4 PaletteUseContext *s = ctx->priv;
812
813 4 ret = ff_framesync_init_dualinput(&s->fs, ctx);
814
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (ret < 0)
815 return ret;
816 4 s->fs.opt_repeatlast = 1; // only 1 frame in the palette
817 4 s->fs.in[1].before = s->fs.in[1].after = EXT_INFINITY;
818 4 s->fs.on_event = load_apply_palette;
819
820 4 outlink->w = ctx->inputs[0]->w;
821 4 outlink->h = ctx->inputs[0]->h;
822
823 4 outlink->time_base = ctx->inputs[0]->time_base;
824
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 if ((ret = ff_framesync_configure(&s->fs)) < 0)
825 return ret;
826 4 return 0;
827 }
828
829 4 static int config_input_palette(AVFilterLink *inlink)
830 {
831 4 AVFilterContext *ctx = inlink->dst;
832
833
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (inlink->w * inlink->h != AVPALETTE_COUNT) {
834 av_log(ctx, AV_LOG_ERROR,
835 "Palette input must contain exactly %d pixels. "
836 "Specified input has %dx%d=%d pixels\n",
837 AVPALETTE_COUNT, inlink->w, inlink->h,
838 inlink->w * inlink->h);
839 return AVERROR(EINVAL);
840 }
841 4 return 0;
842 }
843
844 4 static void load_palette(PaletteUseContext *s, const AVFrame *palette_frame)
845 {
846 int i, x, y;
847 4 const uint32_t *p = (const uint32_t *)palette_frame->data[0];
848 4 const ptrdiff_t p_linesize = palette_frame->linesize[0] >> 2;
849
850 4 s->transparency_index = -1;
851
852
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (s->new) {
853 memset(s->palette, 0, sizeof(s->palette));
854 memset(s->map, 0, sizeof(s->map));
855 for (i = 0; i < CACHE_SIZE; i++)
856 av_freep(&s->cache[i].entries);
857 memset(s->cache, 0, sizeof(s->cache));
858 }
859
860 4 i = 0;
861
2/2
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 4 times.
68 for (y = 0; y < palette_frame->height; y++) {
862
2/2
✓ Branch 0 taken 1024 times.
✓ Branch 1 taken 64 times.
1088 for (x = 0; x < palette_frame->width; x++) {
863 1024 s->palette[i] = p[x];
864
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1020 times.
1024 if (p[x]>>24 < s->trans_thresh) {
865 4 s->transparency_index = i; // we are assuming at most one transparent color in palette
866 }
867 1024 i++;
868 }
869 64 p += p_linesize;
870 }
871
872 4 load_colormap(s);
873
874
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (!s->new)
875 4 s->palette_loaded = 1;
876 4 }
877
878 284 static int load_apply_palette(FFFrameSync *fs)
879 {
880 284 AVFilterContext *ctx = fs->parent;
881 284 AVFilterLink *inlink = ctx->inputs[0];
882 284 PaletteUseContext *s = ctx->priv;
883 284 AVFrame *master, *second, *out = NULL;
884 int ret;
885
886 // writable for error diffusal dithering
887 284 ret = ff_framesync_dualinput_get_writable(fs, &master, &second);
888
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 284 times.
284 if (ret < 0)
889 return ret;
890
2/4
✓ Branch 0 taken 284 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 284 times.
284 if (!master || !second) {
891 av_frame_free(&master);
892 return AVERROR_BUG;
893 }
894
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 280 times.
284 if (!s->palette_loaded) {
895 4 load_palette(s, second);
896 }
897 284 ret = apply_palette(inlink, master, &out);
898 284 av_frame_free(&master);
899
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 284 times.
284 if (ret < 0)
900 return ret;
901 284 return ff_filter_frame(ctx->outputs[0], out);
902 }
903
904 #define DEFINE_SET_FRAME(name, value) \
905 static int set_frame_##name(PaletteUseContext *s, AVFrame *out, AVFrame *in, \
906 int x_start, int y_start, int w, int h) \
907 { \
908 return set_frame(s, out, in, x_start, y_start, w, h, value); \
909 }
910
911 71 DEFINE_SET_FRAME(none, DITHERING_NONE)
912 142 DEFINE_SET_FRAME(bayer, DITHERING_BAYER)
913 DEFINE_SET_FRAME(heckbert, DITHERING_HECKBERT)
914 DEFINE_SET_FRAME(floyd_steinberg, DITHERING_FLOYD_STEINBERG)
915 DEFINE_SET_FRAME(sierra2, DITHERING_SIERRA2)
916 71 DEFINE_SET_FRAME(sierra2_4a, DITHERING_SIERRA2_4A)
917 DEFINE_SET_FRAME(sierra3, DITHERING_SIERRA3)
918 DEFINE_SET_FRAME(burkes, DITHERING_BURKES)
919 DEFINE_SET_FRAME(atkinson, DITHERING_ATKINSON)
920
921 static const set_frame_func set_frame_lut[NB_DITHERING] = {
922 [DITHERING_NONE] = set_frame_none,
923 [DITHERING_BAYER] = set_frame_bayer,
924 [DITHERING_HECKBERT] = set_frame_heckbert,
925 [DITHERING_FLOYD_STEINBERG] = set_frame_floyd_steinberg,
926 [DITHERING_SIERRA2] = set_frame_sierra2,
927 [DITHERING_SIERRA2_4A] = set_frame_sierra2_4a,
928 [DITHERING_SIERRA3] = set_frame_sierra3,
929 [DITHERING_BURKES] = set_frame_burkes,
930 [DITHERING_ATKINSON] = set_frame_atkinson,
931 };
932
933 256 static int dither_value(int p)
934 {
935 256 const int q = p ^ (p >> 3);
936 256 return (p & 4) >> 2 | (q & 4) >> 1 \
937 256 | (p & 2) << 1 | (q & 2) << 2 \
938 256 | (p & 1) << 4 | (q & 1) << 5;
939 }
940
941 8 static av_cold int init(AVFilterContext *ctx)
942 {
943 8 PaletteUseContext *s = ctx->priv;
944
945 8 s->last_in = av_frame_alloc();
946 8 s->last_out = av_frame_alloc();
947
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)
948 return AVERROR(ENOMEM);
949
950 8 s->set_frame = set_frame_lut[s->dither];
951
952
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
8 if (s->dither == DITHERING_BAYER) {
953 4 const int delta = 1 << (5 - s->bayer_scale); // to avoid too much luma
954
955
2/2
✓ Branch 0 taken 256 times.
✓ Branch 1 taken 4 times.
260 for (int i = 0; i < FF_ARRAY_ELEMS(s->ordered_dither); i++)
956 256 s->ordered_dither[i] = (dither_value(i) >> s->bayer_scale) - delta;
957 }
958
959 8 return 0;
960 }
961
962 584 static int activate(AVFilterContext *ctx)
963 {
964 584 PaletteUseContext *s = ctx->priv;
965 584 return ff_framesync_activate(&s->fs);
966 }
967
968 8 static av_cold void uninit(AVFilterContext *ctx)
969 {
970 8 PaletteUseContext *s = ctx->priv;
971
972 8 ff_framesync_uninit(&s->fs);
973
2/2
✓ Branch 0 taken 262144 times.
✓ Branch 1 taken 8 times.
262152 for (int i = 0; i < CACHE_SIZE; i++)
974 262144 av_freep(&s->cache[i].entries);
975 8 av_frame_free(&s->last_in);
976 8 av_frame_free(&s->last_out);
977 8 }
978
979 static const AVFilterPad paletteuse_inputs[] = {
980 {
981 .name = "default",
982 .type = AVMEDIA_TYPE_VIDEO,
983 },{
984 .name = "palette",
985 .type = AVMEDIA_TYPE_VIDEO,
986 .config_props = config_input_palette,
987 },
988 };
989
990 static const AVFilterPad paletteuse_outputs[] = {
991 {
992 .name = "default",
993 .type = AVMEDIA_TYPE_VIDEO,
994 .config_props = config_output,
995 },
996 };
997
998 const AVFilter ff_vf_paletteuse = {
999 .name = "paletteuse",
1000 .description = NULL_IF_CONFIG_SMALL("Use a palette to downsample an input video stream."),
1001 .priv_size = sizeof(PaletteUseContext),
1002 .init = init,
1003 .uninit = uninit,
1004 .activate = activate,
1005 FILTER_INPUTS(paletteuse_inputs),
1006 FILTER_OUTPUTS(paletteuse_outputs),
1007 FILTER_QUERY_FUNC(query_formats),
1008 .priv_class = &paletteuse_class,
1009 };
1010