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/mem.h" | ||
31 | #include "libavutil/opt.h" | ||
32 | #include "libavutil/qsort.h" | ||
33 | #include "avfilter.h" | ||
34 | #include "filters.h" | ||
35 | #include "formats.h" | ||
36 | #include "framesync.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(const AVFilterContext *ctx, | |
140 | AVFilterFormatsConfig **cfg_in, | ||
141 | AVFilterFormatsConfig **cfg_out) | ||
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 | &cfg_in[0]->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 | &cfg_in[1]->formats)) < 0 || |
151 | 4 | (ret = ff_formats_ref(ff_make_format_list(out_fmts), | |
152 | 4 | &cfg_out[0]->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 & 0xff000000) | |
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 | 10265674 | static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh) | |
167 | { | ||
168 | 10265674 | const uint8_t alpha_a = a->srgb >> 24; | |
169 | 10265674 | const uint8_t alpha_b = b->srgb >> 24; | |
170 | |||
171 |
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) { |
172 | ✗ | return 0; | |
173 |
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) { |
174 | 10265674 | const int64_t dL = a->lab[0] - b->lab[0]; | |
175 | 10265674 | const int64_t da = a->lab[1] - b->lab[1]; | |
176 | 10265674 | const int64_t db = a->lab[2] - b->lab[2]; | |
177 | 10265674 | const int64_t ret = dL*dL + da*da + db*db; | |
178 | 10265674 | return FFMIN(ret, INT32_MAX - 1); | |
179 | } else { | ||
180 | ✗ | return INT32_MAX - 1; | |
181 | } | ||
182 | } | ||
183 | |||
184 | 487922 | static struct color_info get_color_from_srgb(uint32_t srgb) | |
185 | { | ||
186 | 487922 | const struct Lab lab = ff_srgb_u8_to_oklab_int(srgb); | |
187 | 487922 | struct color_info ret = {.srgb=srgb, .lab={lab.L, lab.a, lab.b}}; | |
188 | 487922 | return ret; | |
189 | } | ||
190 | |||
191 | struct nearest_color { | ||
192 | int node_pos; | ||
193 | int64_t dist_sqd; | ||
194 | }; | ||
195 | |||
196 | 10265674 | static void colormap_nearest_node(const struct color_node *map, | |
197 | const int node_pos, | ||
198 | const struct color_info *target, | ||
199 | const int trans_thresh, | ||
200 | struct nearest_color *nearest) | ||
201 | { | ||
202 | 10265674 | const struct color_node *kd = map + node_pos; | |
203 | int nearer_kd_id, further_kd_id; | ||
204 | 10265674 | const struct color_info *current = &kd->c; | |
205 | 10265674 | const int64_t current_to_target = diff(target, current, trans_thresh); | |
206 | |||
207 |
2/2✓ Branch 0 taken 2506085 times.
✓ Branch 1 taken 7759589 times.
|
10265674 | if (current_to_target < nearest->dist_sqd) { |
208 | 2506085 | nearest->node_pos = node_pos; | |
209 | 2506085 | nearest->dist_sqd = current_to_target; | |
210 | } | ||
211 | |||
212 |
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) { |
213 | 6984637 | const int64_t dx = target->lab[kd->split] - current->lab[kd->split]; | |
214 | |||
215 |
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; |
216 | 3306231 | else nearer_kd_id = kd->right_id, further_kd_id = kd->left_id; | |
217 | |||
218 |
2/2✓ Branch 0 taken 6966650 times.
✓ Branch 1 taken 17987 times.
|
6984637 | if (nearer_kd_id != -1) |
219 | 6966650 | colormap_nearest_node(map, nearer_kd_id, target, trans_thresh, nearest); | |
220 | |||
221 |
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) |
222 | 2812122 | colormap_nearest_node(map, further_kd_id, target, trans_thresh, nearest); | |
223 | } | ||
224 | 10265674 | } | |
225 | |||
226 | 486902 | static av_always_inline uint8_t colormap_nearest(const struct color_node *node, const struct color_info *target, const int trans_thresh) | |
227 | { | ||
228 | 486902 | struct nearest_color res = {.dist_sqd = INT_MAX, .node_pos = -1}; | |
229 | 486902 | colormap_nearest_node(node, 0, target, trans_thresh, &res); | |
230 | 486902 | return node[res.node_pos].palette_id; | |
231 | } | ||
232 | |||
233 | struct stack_node { | ||
234 | int color_id; | ||
235 | int dx2; | ||
236 | }; | ||
237 | |||
238 | /** | ||
239 | * Check if the requested color is in the cache already. If not, find it in the | ||
240 | * color tree and cache it. | ||
241 | */ | ||
242 | 16275189 | static av_always_inline int color_get(PaletteUseContext *s, uint32_t color) | |
243 | { | ||
244 | struct color_info clrinfo; | ||
245 | 16275189 | const uint32_t hash = ff_lowbias32(color) & (CACHE_SIZE - 1); | |
246 | 16275189 | struct cache_node *node = &s->cache[hash]; | |
247 | struct cached_color *e; | ||
248 | |||
249 | // first, check for transparency | ||
250 |
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) { |
251 | ✗ | return s->transparency_index; | |
252 | } | ||
253 | |||
254 |
2/2✓ Branch 0 taken 28679153 times.
✓ Branch 1 taken 486902 times.
|
29166055 | for (int i = 0; i < node->nb_entries; i++) { |
255 | 28679153 | e = &node->entries[i]; | |
256 |
2/2✓ Branch 0 taken 15788287 times.
✓ Branch 1 taken 12890866 times.
|
28679153 | if (e->color == color) |
257 | 15788287 | return e->pal_entry; | |
258 | } | ||
259 | |||
260 | 486902 | e = av_dynarray2_add((void**)&node->entries, &node->nb_entries, | |
261 | sizeof(*node->entries), NULL); | ||
262 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 486902 times.
|
486902 | if (!e) |
263 | ✗ | return AVERROR(ENOMEM); | |
264 | 486902 | e->color = color; | |
265 | 486902 | clrinfo = get_color_from_srgb(color); | |
266 | 486902 | e->pal_entry = colormap_nearest(s->map, &clrinfo, s->trans_thresh); | |
267 | |||
268 | 486902 | return e->pal_entry; | |
269 | } | ||
270 | |||
271 | 4006389 | static av_always_inline int get_dst_color_err(PaletteUseContext *s, | |
272 | uint32_t c, int *er, int *eg, int *eb) | ||
273 | { | ||
274 | uint32_t dstc; | ||
275 | 4006389 | const int dstx = color_get(s, c); | |
276 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4006389 times.
|
4006389 | if (dstx < 0) |
277 | ✗ | return dstx; | |
278 | 4006389 | dstc = s->palette[dstx]; | |
279 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4006389 times.
|
4006389 | if (dstx == s->transparency_index) { |
280 | ✗ | *er = *eg = *eb = 0; | |
281 | } else { | ||
282 | 4006389 | const uint8_t r = c >> 16 & 0xff; | |
283 | 4006389 | const uint8_t g = c >> 8 & 0xff; | |
284 | 4006389 | const uint8_t b = c & 0xff; | |
285 | 4006389 | *er = (int)r - (int)(dstc >> 16 & 0xff); | |
286 | 4006389 | *eg = (int)g - (int)(dstc >> 8 & 0xff); | |
287 | 4006389 | *eb = (int)b - (int)(dstc & 0xff); | |
288 | } | ||
289 | 4006389 | return dstx; | |
290 | } | ||
291 | |||
292 | 284 | static av_always_inline int set_frame(PaletteUseContext *s, AVFrame *out, AVFrame *in, | |
293 | int x_start, int y_start, int w, int h, | ||
294 | enum dithering_mode dither) | ||
295 | { | ||
296 | 284 | const int src_linesize = in ->linesize[0] >> 2; | |
297 | 284 | const int dst_linesize = out->linesize[0]; | |
298 | 284 | uint32_t *src = ((uint32_t *)in ->data[0]) + y_start*src_linesize; | |
299 | 284 | uint8_t *dst = out->data[0] + y_start*dst_linesize; | |
300 | |||
301 | 284 | w += x_start; | |
302 | 284 | h += y_start; | |
303 | |||
304 |
2/2✓ Branch 0 taken 50911 times.
✓ Branch 1 taken 284 times.
|
51195 | for (int y = y_start; y < h; y++) { |
305 |
2/2✓ Branch 0 taken 16275189 times.
✓ Branch 1 taken 50911 times.
|
16326100 | for (int x = x_start; x < w; x++) { |
306 | int er, eg, eb; | ||
307 | |||
308 |
2/2✓ Branch 0 taken 8179200 times.
✓ Branch 1 taken 8095989 times.
|
16275189 | if (dither == DITHERING_BAYER) { |
309 | 8179200 | const int d = s->ordered_dither[(y & 7)<<3 | (x & 7)]; | |
310 | 8179200 | const uint8_t a8 = src[x] >> 24; | |
311 | 8179200 | const uint8_t r8 = src[x] >> 16 & 0xff; | |
312 | 8179200 | const uint8_t g8 = src[x] >> 8 & 0xff; | |
313 | 8179200 | const uint8_t b8 = src[x] & 0xff; | |
314 | 8179200 | const uint8_t r = av_clip_uint8(r8 + d); | |
315 | 8179200 | const uint8_t g = av_clip_uint8(g8 + d); | |
316 | 8179200 | const uint8_t b = av_clip_uint8(b8 + d); | |
317 | 8179200 | const uint32_t color_new = (unsigned)(a8) << 24 | r << 16 | g << 8 | b; | |
318 | 8179200 | const int color = color_get(s, color_new); | |
319 | |||
320 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8179200 times.
|
8179200 | if (color < 0) |
321 | ✗ | return color; | |
322 | 8179200 | dst[x] = color; | |
323 | |||
324 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8095989 times.
|
8095989 | } else if (dither == DITHERING_HECKBERT) { |
325 | ✗ | const int right = x < w - 1, down = y < h - 1; | |
326 | ✗ | const int color = get_dst_color_err(s, src[x], &er, &eg, &eb); | |
327 | |||
328 | ✗ | if (color < 0) | |
329 | ✗ | return color; | |
330 | ✗ | dst[x] = color; | |
331 | |||
332 | ✗ | if (right) src[ x + 1] = dither_color(src[ x + 1], er, eg, eb, 3, 3); | |
333 | ✗ | if ( down) src[src_linesize + x ] = dither_color(src[src_linesize + x ], er, eg, eb, 3, 3); | |
334 | ✗ | if (right && down) src[src_linesize + x + 1] = dither_color(src[src_linesize + x + 1], er, eg, eb, 2, 3); | |
335 | |||
336 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8095989 times.
|
8095989 | } else if (dither == DITHERING_FLOYD_STEINBERG) { |
337 | ✗ | const int right = x < w - 1, down = y < h - 1, left = x > x_start; | |
338 | ✗ | const int color = get_dst_color_err(s, src[x], &er, &eg, &eb); | |
339 | |||
340 | ✗ | if (color < 0) | |
341 | ✗ | return color; | |
342 | ✗ | dst[x] = color; | |
343 | |||
344 | ✗ | if (right) src[ x + 1] = dither_color(src[ x + 1], er, eg, eb, 7, 4); | |
345 | ✗ | if (left && down) src[src_linesize + x - 1] = dither_color(src[src_linesize + x - 1], er, eg, eb, 3, 4); | |
346 | ✗ | if ( down) src[src_linesize + x ] = dither_color(src[src_linesize + x ], er, eg, eb, 5, 4); | |
347 | ✗ | if (right && down) src[src_linesize + x + 1] = dither_color(src[src_linesize + x + 1], er, eg, eb, 1, 4); | |
348 | |||
349 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8095989 times.
|
8095989 | } else if (dither == DITHERING_SIERRA2) { |
350 | ✗ | const int right = x < w - 1, down = y < h - 1, left = x > x_start; | |
351 | ✗ | const int right2 = x < w - 2, left2 = x > x_start + 1; | |
352 | ✗ | const int color = get_dst_color_err(s, src[x], &er, &eg, &eb); | |
353 | |||
354 | ✗ | if (color < 0) | |
355 | ✗ | return color; | |
356 | ✗ | dst[x] = color; | |
357 | |||
358 | ✗ | if (right) src[ x + 1] = dither_color(src[ x + 1], er, eg, eb, 4, 4); | |
359 | ✗ | if (right2) src[ x + 2] = dither_color(src[ x + 2], er, eg, eb, 3, 4); | |
360 | |||
361 | ✗ | if (down) { | |
362 | ✗ | if (left2) src[ src_linesize + x - 2] = dither_color(src[ src_linesize + x - 2], er, eg, eb, 1, 4); | |
363 | ✗ | if (left) src[ src_linesize + x - 1] = dither_color(src[ src_linesize + x - 1], er, eg, eb, 2, 4); | |
364 | ✗ | if (1) src[ src_linesize + x ] = dither_color(src[ src_linesize + x ], er, eg, eb, 3, 4); | |
365 | ✗ | if (right) src[ src_linesize + x + 1] = dither_color(src[ src_linesize + x + 1], er, eg, eb, 2, 4); | |
366 | ✗ | if (right2) src[ src_linesize + x + 2] = dither_color(src[ src_linesize + x + 2], er, eg, eb, 1, 4); | |
367 | } | ||
368 | |||
369 |
2/2✓ Branch 0 taken 4006389 times.
✓ Branch 1 taken 4089600 times.
|
8095989 | } else if (dither == DITHERING_SIERRA2_4A) { |
370 | 4006389 | const int right = x < w - 1, down = y < h - 1, left = x > x_start; | |
371 | 4006389 | const int color = get_dst_color_err(s, src[x], &er, &eg, &eb); | |
372 | |||
373 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4006389 times.
|
4006389 | if (color < 0) |
374 | ✗ | return color; | |
375 | 4006389 | dst[x] = color; | |
376 | |||
377 |
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); |
378 |
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); |
379 |
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); |
380 | |||
381 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4089600 times.
|
4089600 | } else if (dither == DITHERING_SIERRA3) { |
382 | ✗ | const int right = x < w - 1, down = y < h - 1, left = x > x_start; | |
383 | ✗ | const int right2 = x < w - 2, down2 = y < h - 2, left2 = x > x_start + 1; | |
384 | ✗ | const int color = get_dst_color_err(s, src[x], &er, &eg, &eb); | |
385 | |||
386 | ✗ | if (color < 0) | |
387 | ✗ | return color; | |
388 | ✗ | dst[x] = color; | |
389 | |||
390 | ✗ | if (right) src[ x + 1] = dither_color(src[ x + 1], er, eg, eb, 5, 5); | |
391 | ✗ | if (right2) src[ x + 2] = dither_color(src[ x + 2], er, eg, eb, 3, 5); | |
392 | |||
393 | ✗ | if (down) { | |
394 | ✗ | if (left2) src[src_linesize + x - 2] = dither_color(src[src_linesize + x - 2], er, eg, eb, 2, 5); | |
395 | ✗ | if (left) src[src_linesize + x - 1] = dither_color(src[src_linesize + x - 1], er, eg, eb, 4, 5); | |
396 | ✗ | if (1) src[src_linesize + x ] = dither_color(src[src_linesize + x ], er, eg, eb, 5, 5); | |
397 | ✗ | if (right) src[src_linesize + x + 1] = dither_color(src[src_linesize + x + 1], er, eg, eb, 4, 5); | |
398 | ✗ | if (right2) src[src_linesize + x + 2] = dither_color(src[src_linesize + x + 2], er, eg, eb, 2, 5); | |
399 | |||
400 | ✗ | if (down2) { | |
401 | ✗ | if (left) src[src_linesize*2 + x - 1] = dither_color(src[src_linesize*2 + x - 1], er, eg, eb, 2, 5); | |
402 | ✗ | if (1) src[src_linesize*2 + x ] = dither_color(src[src_linesize*2 + x ], er, eg, eb, 3, 5); | |
403 | ✗ | if (right) src[src_linesize*2 + x + 1] = dither_color(src[src_linesize*2 + x + 1], er, eg, eb, 2, 5); | |
404 | } | ||
405 | } | ||
406 | |||
407 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4089600 times.
|
4089600 | } else if (dither == DITHERING_BURKES) { |
408 | ✗ | const int right = x < w - 1, down = y < h - 1, left = x > x_start; | |
409 | ✗ | const int right2 = x < w - 2, left2 = x > x_start + 1; | |
410 | ✗ | const int color = get_dst_color_err(s, src[x], &er, &eg, &eb); | |
411 | |||
412 | ✗ | if (color < 0) | |
413 | ✗ | return color; | |
414 | ✗ | dst[x] = color; | |
415 | |||
416 | ✗ | if (right) src[ x + 1] = dither_color(src[ x + 1], er, eg, eb, 8, 5); | |
417 | ✗ | if (right2) src[ x + 2] = dither_color(src[ x + 2], er, eg, eb, 4, 5); | |
418 | |||
419 | ✗ | if (down) { | |
420 | ✗ | if (left2) src[src_linesize + x - 2] = dither_color(src[src_linesize + x - 2], er, eg, eb, 2, 5); | |
421 | ✗ | if (left) src[src_linesize + x - 1] = dither_color(src[src_linesize + x - 1], er, eg, eb, 4, 5); | |
422 | ✗ | if (1) src[src_linesize + x ] = dither_color(src[src_linesize + x ], er, eg, eb, 8, 5); | |
423 | ✗ | if (right) src[src_linesize + x + 1] = dither_color(src[src_linesize + x + 1], er, eg, eb, 4, 5); | |
424 | ✗ | if (right2) src[src_linesize + x + 2] = dither_color(src[src_linesize + x + 2], er, eg, eb, 2, 5); | |
425 | } | ||
426 | |||
427 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4089600 times.
|
4089600 | } else if (dither == DITHERING_ATKINSON) { |
428 | ✗ | const int right = x < w - 1, down = y < h - 1, left = x > x_start; | |
429 | ✗ | const int right2 = x < w - 2, down2 = y < h - 2; | |
430 | ✗ | const int color = get_dst_color_err(s, src[x], &er, &eg, &eb); | |
431 | |||
432 | ✗ | if (color < 0) | |
433 | ✗ | return color; | |
434 | ✗ | dst[x] = color; | |
435 | |||
436 | ✗ | if (right) src[ x + 1] = dither_color(src[ x + 1], er, eg, eb, 1, 3); | |
437 | ✗ | if (right2) src[ x + 2] = dither_color(src[ x + 2], er, eg, eb, 1, 3); | |
438 | |||
439 | ✗ | if (down) { | |
440 | ✗ | if (left) src[src_linesize + x - 1] = dither_color(src[src_linesize + x - 1], er, eg, eb, 1, 3); | |
441 | ✗ | if (1) src[src_linesize + x ] = dither_color(src[src_linesize + x ], er, eg, eb, 1, 3); | |
442 | ✗ | if (right) src[src_linesize + x + 1] = dither_color(src[src_linesize + x + 1], er, eg, eb, 1, 3); | |
443 | ✗ | if (down2) src[src_linesize*2 + x ] = dither_color(src[src_linesize*2 + x ], er, eg, eb, 1, 3); | |
444 | } | ||
445 | |||
446 | } else { | ||
447 | 4089600 | const int color = color_get(s, src[x]); | |
448 | |||
449 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4089600 times.
|
4089600 | if (color < 0) |
450 | ✗ | return color; | |
451 | 4089600 | dst[x] = color; | |
452 | } | ||
453 | } | ||
454 | 50911 | src += src_linesize; | |
455 | 50911 | dst += dst_linesize; | |
456 | } | ||
457 | 284 | return 0; | |
458 | } | ||
459 | |||
460 | #define INDENT 4 | ||
461 | ✗ | static void disp_node(AVBPrint *buf, | |
462 | const struct color_node *map, | ||
463 | int parent_id, int node_id, | ||
464 | int depth) | ||
465 | { | ||
466 | ✗ | const struct color_node *node = &map[node_id]; | |
467 | ✗ | const uint32_t fontcolor = node->c.lab[0] > 0x7fff ? 0 : 0xffffff; | |
468 | ✗ | const int lab_comp = node->split; | |
469 | ✗ | av_bprintf(buf, "%*cnode%d [" | |
470 | "label=\"%c%d%c%d%c%d%c\" " | ||
471 | "fillcolor=\"#%06"PRIX32"\" " | ||
472 | "fontcolor=\"#%06"PRIX32"\"]\n", | ||
473 | ✗ | depth*INDENT, ' ', node->palette_id, | |
474 | ✗ | "[ "[lab_comp], node->c.lab[0], | |
475 | ✗ | "][ "[lab_comp], node->c.lab[1], | |
476 | ✗ | " ]["[lab_comp], node->c.lab[2], | |
477 | ✗ | " ]"[lab_comp], | |
478 | ✗ | node->c.srgb & 0xffffff, | |
479 | fontcolor); | ||
480 | ✗ | if (parent_id != -1) | |
481 | ✗ | av_bprintf(buf, "%*cnode%d -> node%d\n", depth*INDENT, ' ', | |
482 | ✗ | map[parent_id].palette_id, node->palette_id); | |
483 | ✗ | if (node->left_id != -1) disp_node(buf, map, node_id, node->left_id, depth + 1); | |
484 | ✗ | if (node->right_id != -1) disp_node(buf, map, node_id, node->right_id, depth + 1); | |
485 | ✗ | } | |
486 | |||
487 | // debug_kdtree=kdtree.dot -> dot -Tpng kdtree.dot > kdtree.png | ||
488 | ✗ | static int disp_tree(const struct color_node *node, const char *fname) | |
489 | { | ||
490 | AVBPrint buf; | ||
491 | ✗ | FILE *f = avpriv_fopen_utf8(fname, "w"); | |
492 | |||
493 | ✗ | if (!f) { | |
494 | ✗ | int ret = AVERROR(errno); | |
495 | ✗ | av_log(NULL, AV_LOG_ERROR, "Cannot open file '%s' for writing: %s\n", | |
496 | ✗ | fname, av_err2str(ret)); | |
497 | ✗ | return ret; | |
498 | } | ||
499 | |||
500 | ✗ | av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED); | |
501 | |||
502 | ✗ | av_bprintf(&buf, "digraph {\n"); | |
503 | ✗ | av_bprintf(&buf, " node [style=filled fontsize=10 shape=box]\n"); | |
504 | ✗ | disp_node(&buf, node, -1, 0, 0); | |
505 | ✗ | av_bprintf(&buf, "}\n"); | |
506 | |||
507 | ✗ | fwrite(buf.str, 1, buf.len, f); | |
508 | ✗ | fclose(f); | |
509 | ✗ | av_bprint_finalize(&buf, NULL); | |
510 | ✗ | return 0; | |
511 | } | ||
512 | |||
513 | struct color { | ||
514 | struct Lab value; | ||
515 | uint8_t pal_id; | ||
516 | }; | ||
517 | |||
518 | struct color_rect { | ||
519 | int32_t min[3]; | ||
520 | int32_t max[3]; | ||
521 | }; | ||
522 | |||
523 | typedef int (*cmp_func)(const void *, const void *); | ||
524 | |||
525 | #define DECLARE_CMP_FUNC(name) \ | ||
526 | static int cmp_##name(const void *pa, const void *pb) \ | ||
527 | { \ | ||
528 | const struct color *a = pa; \ | ||
529 | const struct color *b = pb; \ | ||
530 | return FFDIFFSIGN(a->value.name, b->value.name); \ | ||
531 | } | ||
532 | |||
533 | 22876 | DECLARE_CMP_FUNC(L) | |
534 | 3460 | DECLARE_CMP_FUNC(a) | |
535 | 2692 | DECLARE_CMP_FUNC(b) | |
536 | |||
537 | static const cmp_func cmp_funcs[] = {cmp_L, cmp_a, cmp_b}; | ||
538 | |||
539 | 2040 | static int get_next_color(const uint8_t *color_used, const uint32_t *palette, | |
540 | int *component, const struct color_rect *box) | ||
541 | { | ||
542 | int wL, wa, wb; | ||
543 | 2040 | int longest = 0; | |
544 | 2040 | unsigned nb_color = 0; | |
545 | struct color_rect ranges; | ||
546 | struct color tmp_pal[256]; | ||
547 | cmp_func cmpf; | ||
548 | |||
549 | 2040 | ranges.min[0] = ranges.min[1] = ranges.min[2] = 0xffff; | |
550 | 2040 | ranges.max[0] = ranges.max[1] = ranges.max[2] = -0xffff; | |
551 | |||
552 |
2/2✓ Branch 0 taken 522240 times.
✓ Branch 1 taken 2040 times.
|
524280 | for (int i = 0; i < AVPALETTE_COUNT; i++) { |
553 | 522240 | const uint32_t c = palette[i]; | |
554 | 522240 | const uint8_t a = c >> 24; | |
555 | 522240 | const struct Lab lab = ff_srgb_u8_to_oklab_int(c); | |
556 | |||
557 |
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) || |
558 |
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] || |
559 |
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]) |
560 | 515064 | continue; | |
561 | |||
562 |
2/2✓ Branch 0 taken 1336 times.
✓ Branch 1 taken 5840 times.
|
7176 | if (lab.L < ranges.min[0]) ranges.min[0] = lab.L; |
563 |
2/2✓ Branch 0 taken 1360 times.
✓ Branch 1 taken 5816 times.
|
7176 | if (lab.a < ranges.min[1]) ranges.min[1] = lab.a; |
564 |
2/2✓ Branch 0 taken 2192 times.
✓ Branch 1 taken 4984 times.
|
7176 | if (lab.b < ranges.min[2]) ranges.min[2] = lab.b; |
565 | |||
566 |
2/2✓ Branch 0 taken 3312 times.
✓ Branch 1 taken 3864 times.
|
7176 | if (lab.L > ranges.max[0]) ranges.max[0] = lab.L; |
567 |
2/2✓ Branch 0 taken 2416 times.
✓ Branch 1 taken 4760 times.
|
7176 | if (lab.a > ranges.max[1]) ranges.max[1] = lab.a; |
568 |
2/2✓ Branch 0 taken 1636 times.
✓ Branch 1 taken 5540 times.
|
7176 | if (lab.b > ranges.max[2]) ranges.max[2] = lab.b; |
569 | |||
570 | 7176 | tmp_pal[nb_color].value = lab; | |
571 | 7176 | tmp_pal[nb_color].pal_id = i; | |
572 | |||
573 | 7176 | nb_color++; | |
574 | } | ||
575 | |||
576 |
2/2✓ Branch 0 taken 1020 times.
✓ Branch 1 taken 1020 times.
|
2040 | if (!nb_color) |
577 | 1020 | return -1; | |
578 | |||
579 | /* define longest axis that will be the split component */ | ||
580 | 1020 | wL = ranges.max[0] - ranges.min[0]; | |
581 | 1020 | wa = ranges.max[1] - ranges.min[1]; | |
582 | 1020 | wb = ranges.max[2] - ranges.min[2]; | |
583 |
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; |
584 |
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; |
585 |
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; |
586 | 1020 | cmpf = cmp_funcs[longest]; | |
587 | 1020 | *component = longest; | |
588 | |||
589 | /* sort along this axis to get median */ | ||
590 |
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); |
591 | |||
592 | 1020 | return tmp_pal[nb_color >> 1].pal_id; | |
593 | } | ||
594 | |||
595 | 2040 | static int colormap_insert(struct color_node *map, | |
596 | uint8_t *color_used, | ||
597 | int *nb_used, | ||
598 | const uint32_t *palette, | ||
599 | const int trans_thresh, | ||
600 | const struct color_rect *box) | ||
601 | { | ||
602 | int component, cur_id; | ||
603 | int comp_value; | ||
604 | 2040 | int node_left_id = -1, node_right_id = -1; | |
605 | struct color_node *node; | ||
606 | struct color_rect box1, box2; | ||
607 | 2040 | const int pal_id = get_next_color(color_used, palette, &component, box); | |
608 | |||
609 |
2/2✓ Branch 0 taken 1020 times.
✓ Branch 1 taken 1020 times.
|
2040 | if (pal_id < 0) |
610 | 1020 | return -1; | |
611 | |||
612 | /* create new node with that color */ | ||
613 | 1020 | cur_id = (*nb_used)++; | |
614 | 1020 | node = &map[cur_id]; | |
615 | 1020 | node->split = component; | |
616 | 1020 | node->palette_id = pal_id; | |
617 | 1020 | node->c = get_color_from_srgb(palette[pal_id]); | |
618 | |||
619 | 1020 | color_used[pal_id] = 1; | |
620 | |||
621 | /* get the two boxes this node creates */ | ||
622 | 1020 | box1 = box2 = *box; | |
623 | 1020 | comp_value = node->c.lab[component]; | |
624 | 1020 | box1.max[component] = comp_value; | |
625 | 1020 | box2.min[component] = FFMIN(comp_value + 1, 0xffff); | |
626 | |||
627 | 1020 | node_left_id = colormap_insert(map, color_used, nb_used, palette, trans_thresh, &box1); | |
628 | |||
629 |
2/2✓ Branch 0 taken 1016 times.
✓ Branch 1 taken 4 times.
|
1020 | if (box2.min[component] <= box2.max[component]) |
630 | 1016 | node_right_id = colormap_insert(map, color_used, nb_used, palette, trans_thresh, &box2); | |
631 | |||
632 | 1020 | node->left_id = node_left_id; | |
633 | 1020 | node->right_id = node_right_id; | |
634 | |||
635 | 1020 | return cur_id; | |
636 | } | ||
637 | |||
638 | 4064 | static int cmp_pal_entry(const void *a, const void *b) | |
639 | { | ||
640 | 4064 | const int c1 = *(const uint32_t *)a & 0xffffff; | |
641 | 4064 | const int c2 = *(const uint32_t *)b & 0xffffff; | |
642 | 4064 | return c1 - c2; | |
643 | } | ||
644 | |||
645 | 4 | static void load_colormap(PaletteUseContext *s) | |
646 | { | ||
647 | 4 | int nb_used = 0; | |
648 | 4 | uint8_t color_used[AVPALETTE_COUNT] = {0}; | |
649 | 4 | uint32_t last_color = 0; | |
650 | struct color_rect box; | ||
651 | |||
652 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | if (s->transparency_index >= 0) { |
653 | 4 | FFSWAP(uint32_t, s->palette[s->transparency_index], s->palette[255]); | |
654 | } | ||
655 | |||
656 | /* disable transparent colors and dups */ | ||
657 |
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); |
658 | |||
659 |
2/2✓ Branch 0 taken 1024 times.
✓ Branch 1 taken 4 times.
|
1028 | for (int i = 0; i < AVPALETTE_COUNT; i++) { |
660 | 1024 | const uint32_t c = s->palette[i]; | |
661 |
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) { |
662 | ✗ | color_used[i] = 1; | |
663 | ✗ | continue; | |
664 | } | ||
665 | 1024 | last_color = c; | |
666 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1020 times.
|
1024 | if (c >> 24 < s->trans_thresh) { |
667 | 4 | color_used[i] = 1; // ignore transparent color(s) | |
668 | 4 | continue; | |
669 | } | ||
670 | } | ||
671 | |||
672 | 4 | box.min[0] = box.min[1] = box.min[2] = -0xffff; | |
673 | 4 | box.max[0] = box.max[1] = box.max[2] = 0xffff; | |
674 | |||
675 | 4 | colormap_insert(s->map, color_used, &nb_used, s->palette, s->trans_thresh, &box); | |
676 | |||
677 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (s->dot_filename) |
678 | ✗ | disp_tree(s->map, s->dot_filename); | |
679 | 4 | } | |
680 | |||
681 | 284 | static void set_processing_window(enum diff_mode diff_mode, | |
682 | const AVFrame *prv_src, const AVFrame *cur_src, | ||
683 | const AVFrame *prv_dst, AVFrame *cur_dst, | ||
684 | int *xp, int *yp, int *wp, int *hp) | ||
685 | { | ||
686 | 284 | int x_start = 0, y_start = 0; | |
687 | 284 | int width = cur_src->width; | |
688 | 284 | int height = cur_src->height; | |
689 | |||
690 |
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) { |
691 | int y; | ||
692 | 70 | int x_end = cur_src->width - 1, | |
693 | 70 | y_end = cur_src->height - 1; | |
694 | 70 | const uint32_t *prv_srcp = (const uint32_t *)prv_src->data[0]; | |
695 | 70 | const uint32_t *cur_srcp = (const uint32_t *)cur_src->data[0]; | |
696 | 70 | const uint8_t *prv_dstp = prv_dst->data[0]; | |
697 | 70 | uint8_t *cur_dstp = cur_dst->data[0]; | |
698 | |||
699 | 70 | const int prv_src_linesize = prv_src->linesize[0] >> 2; | |
700 | 70 | const int cur_src_linesize = cur_src->linesize[0] >> 2; | |
701 | 70 | const int prv_dst_linesize = prv_dst->linesize[0]; | |
702 | 70 | const int cur_dst_linesize = cur_dst->linesize[0]; | |
703 | |||
704 | /* skip common lines */ | ||
705 |
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, |
706 | 259 | cur_srcp + y_start*cur_src_linesize, | |
707 |
2/2✓ Branch 0 taken 189 times.
✓ Branch 1 taken 70 times.
|
259 | cur_src->width * 4)) { |
708 | 189 | memcpy(cur_dstp + y_start*cur_dst_linesize, | |
709 | 189 | prv_dstp + y_start*prv_dst_linesize, | |
710 | 189 | cur_dst->width); | |
711 | 189 | y_start++; | |
712 | } | ||
713 |
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, |
714 | 90 | cur_srcp + y_end*cur_src_linesize, | |
715 |
2/2✓ Branch 0 taken 20 times.
✓ Branch 1 taken 70 times.
|
90 | cur_src->width * 4)) { |
716 | 20 | memcpy(cur_dstp + y_end*cur_dst_linesize, | |
717 | 20 | prv_dstp + y_end*prv_dst_linesize, | |
718 | 20 | cur_dst->width); | |
719 | 20 | y_end--; | |
720 | } | ||
721 | |||
722 | 70 | height = y_end + 1 - y_start; | |
723 | |||
724 | /* skip common columns */ | ||
725 |
1/2✓ Branch 0 taken 138 times.
✗ Branch 1 not taken.
|
138 | while (x_start < x_end) { |
726 | 138 | int same_column = 1; | |
727 |
2/2✓ Branch 0 taken 17740 times.
✓ Branch 1 taken 68 times.
|
17808 | for (y = y_start; y <= y_end; y++) { |
728 |
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]) { |
729 | 70 | same_column = 0; | |
730 | 70 | break; | |
731 | } | ||
732 | } | ||
733 |
2/2✓ Branch 0 taken 70 times.
✓ Branch 1 taken 68 times.
|
138 | if (!same_column) |
734 | 70 | break; | |
735 | 68 | x_start++; | |
736 | } | ||
737 |
1/2✓ Branch 0 taken 97 times.
✗ Branch 1 not taken.
|
97 | while (x_end > x_start) { |
738 | 97 | int same_column = 1; | |
739 |
2/2✓ Branch 0 taken 9281 times.
✓ Branch 1 taken 27 times.
|
9308 | for (y = y_start; y <= y_end; y++) { |
740 |
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]) { |
741 | 70 | same_column = 0; | |
742 | 70 | break; | |
743 | } | ||
744 | } | ||
745 |
2/2✓ Branch 0 taken 70 times.
✓ Branch 1 taken 27 times.
|
97 | if (!same_column) |
746 | 70 | break; | |
747 | 27 | x_end--; | |
748 | } | ||
749 | 70 | width = x_end + 1 - x_start; | |
750 | |||
751 |
2/2✓ Branch 0 taken 23 times.
✓ Branch 1 taken 47 times.
|
70 | if (x_start) { |
752 |
2/2✓ Branch 0 taken 3999 times.
✓ Branch 1 taken 23 times.
|
4022 | for (y = y_start; y <= y_end; y++) |
753 | 3999 | memcpy(cur_dstp + y*cur_dst_linesize, | |
754 | 3999 | prv_dstp + y*prv_dst_linesize, x_start); | |
755 | } | ||
756 |
2/2✓ Branch 0 taken 14 times.
✓ Branch 1 taken 56 times.
|
70 | if (x_end != cur_src->width - 1) { |
757 | 14 | const int copy_len = cur_src->width - 1 - x_end; | |
758 |
2/2✓ Branch 0 taken 2460 times.
✓ Branch 1 taken 14 times.
|
2474 | for (y = y_start; y <= y_end; y++) |
759 | 2460 | memcpy(cur_dstp + y*cur_dst_linesize + x_end + 1, | |
760 | 2460 | prv_dstp + y*prv_dst_linesize + x_end + 1, | |
761 | copy_len); | ||
762 | } | ||
763 | } | ||
764 | 284 | *xp = x_start; | |
765 | 284 | *yp = y_start; | |
766 | 284 | *wp = width; | |
767 | 284 | *hp = height; | |
768 | 284 | } | |
769 | |||
770 | 284 | static int apply_palette(AVFilterLink *inlink, AVFrame *in, AVFrame **outf) | |
771 | { | ||
772 | int x, y, w, h, ret; | ||
773 | 284 | AVFilterContext *ctx = inlink->dst; | |
774 | 284 | PaletteUseContext *s = ctx->priv; | |
775 | 284 | AVFilterLink *outlink = inlink->dst->outputs[0]; | |
776 | |||
777 | 284 | AVFrame *out = ff_get_video_buffer(outlink, outlink->w, outlink->h); | |
778 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 284 times.
|
284 | if (!out) { |
779 | ✗ | *outf = NULL; | |
780 | ✗ | return AVERROR(ENOMEM); | |
781 | } | ||
782 | 284 | av_frame_copy_props(out, in); | |
783 | |||
784 | 284 | set_processing_window(s->diff_mode, s->last_in, in, | |
785 | 284 | s->last_out, out, &x, &y, &w, &h); | |
786 | 284 | av_frame_unref(s->last_out); | |
787 |
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 || |
788 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 284 times.
|
568 | (ret = av_frame_ref(s->last_out, out)) < 0 || |
789 | 284 | (ret = ff_inlink_make_frame_writable(inlink, &s->last_in)) < 0) { | |
790 | ✗ | av_frame_free(&out); | |
791 | ✗ | *outf = NULL; | |
792 | ✗ | return ret; | |
793 | } | ||
794 | |||
795 | ff_dlog(ctx, "%dx%d rect: (%d;%d) -> (%d,%d) [area:%dx%d]\n", | ||
796 | w, h, x, y, x+w, y+h, in->width, in->height); | ||
797 | |||
798 | 284 | ret = s->set_frame(s, out, in, x, y, w, h); | |
799 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 284 times.
|
284 | if (ret < 0) { |
800 | ✗ | av_frame_free(&out); | |
801 | ✗ | *outf = NULL; | |
802 | ✗ | return ret; | |
803 | } | ||
804 | 284 | memcpy(out->data[1], s->palette, AVPALETTE_SIZE); | |
805 | 284 | *outf = out; | |
806 | 284 | return 0; | |
807 | } | ||
808 | |||
809 | 4 | static int config_output(AVFilterLink *outlink) | |
810 | { | ||
811 | int ret; | ||
812 | 4 | AVFilterContext *ctx = outlink->src; | |
813 | 4 | PaletteUseContext *s = ctx->priv; | |
814 | |||
815 | 4 | ret = ff_framesync_init_dualinput(&s->fs, ctx); | |
816 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (ret < 0) |
817 | ✗ | return ret; | |
818 | 4 | s->fs.opt_repeatlast = 1; // only 1 frame in the palette | |
819 | 4 | s->fs.in[1].before = s->fs.in[1].after = EXT_INFINITY; | |
820 | 4 | s->fs.on_event = load_apply_palette; | |
821 | |||
822 | 4 | outlink->w = ctx->inputs[0]->w; | |
823 | 4 | outlink->h = ctx->inputs[0]->h; | |
824 | |||
825 | 4 | outlink->time_base = ctx->inputs[0]->time_base; | |
826 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
|
4 | if ((ret = ff_framesync_configure(&s->fs)) < 0) |
827 | ✗ | return ret; | |
828 | 4 | return 0; | |
829 | } | ||
830 | |||
831 | 4 | static int config_input_palette(AVFilterLink *inlink) | |
832 | { | ||
833 | 4 | AVFilterContext *ctx = inlink->dst; | |
834 | |||
835 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (inlink->w * inlink->h != AVPALETTE_COUNT) { |
836 | ✗ | av_log(ctx, AV_LOG_ERROR, | |
837 | "Palette input must contain exactly %d pixels. " | ||
838 | "Specified input has %dx%d=%d pixels\n", | ||
839 | AVPALETTE_COUNT, inlink->w, inlink->h, | ||
840 | ✗ | inlink->w * inlink->h); | |
841 | ✗ | return AVERROR(EINVAL); | |
842 | } | ||
843 | 4 | return 0; | |
844 | } | ||
845 | |||
846 | 4 | static void load_palette(PaletteUseContext *s, const AVFrame *palette_frame) | |
847 | { | ||
848 | int i, x, y; | ||
849 | 4 | const uint32_t *p = (const uint32_t *)palette_frame->data[0]; | |
850 | 4 | const ptrdiff_t p_linesize = palette_frame->linesize[0] >> 2; | |
851 | |||
852 | 4 | s->transparency_index = -1; | |
853 | |||
854 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (s->new) { |
855 | ✗ | memset(s->palette, 0, sizeof(s->palette)); | |
856 | ✗ | memset(s->map, 0, sizeof(s->map)); | |
857 | ✗ | for (i = 0; i < CACHE_SIZE; i++) | |
858 | ✗ | av_freep(&s->cache[i].entries); | |
859 | ✗ | memset(s->cache, 0, sizeof(s->cache)); | |
860 | } | ||
861 | |||
862 | 4 | i = 0; | |
863 |
2/2✓ Branch 0 taken 64 times.
✓ Branch 1 taken 4 times.
|
68 | for (y = 0; y < palette_frame->height; y++) { |
864 |
2/2✓ Branch 0 taken 1024 times.
✓ Branch 1 taken 64 times.
|
1088 | for (x = 0; x < palette_frame->width; x++) { |
865 | 1024 | s->palette[i] = p[x]; | |
866 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1020 times.
|
1024 | if (p[x]>>24 < s->trans_thresh) { |
867 | 4 | s->transparency_index = i; // we are assuming at most one transparent color in palette | |
868 | } | ||
869 | 1024 | i++; | |
870 | } | ||
871 | 64 | p += p_linesize; | |
872 | } | ||
873 | |||
874 | 4 | load_colormap(s); | |
875 | |||
876 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | if (!s->new) |
877 | 4 | s->palette_loaded = 1; | |
878 | 4 | } | |
879 | |||
880 | 284 | static int load_apply_palette(FFFrameSync *fs) | |
881 | { | ||
882 | 284 | AVFilterContext *ctx = fs->parent; | |
883 | 284 | AVFilterLink *inlink = ctx->inputs[0]; | |
884 | 284 | PaletteUseContext *s = ctx->priv; | |
885 | 284 | AVFrame *master, *second, *out = NULL; | |
886 | int ret; | ||
887 | |||
888 | // writable for error diffusal dithering | ||
889 | 284 | ret = ff_framesync_dualinput_get_writable(fs, &master, &second); | |
890 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 284 times.
|
284 | if (ret < 0) |
891 | ✗ | return ret; | |
892 |
2/4✓ Branch 0 taken 284 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 284 times.
|
284 | if (!master || !second) { |
893 | ✗ | av_frame_free(&master); | |
894 | ✗ | return AVERROR_BUG; | |
895 | } | ||
896 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 280 times.
|
284 | if (!s->palette_loaded) { |
897 | 4 | load_palette(s, second); | |
898 | } | ||
899 | 284 | ret = apply_palette(inlink, master, &out); | |
900 | 284 | av_frame_free(&master); | |
901 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 284 times.
|
284 | if (ret < 0) |
902 | ✗ | return ret; | |
903 | 284 | return ff_filter_frame(ctx->outputs[0], out); | |
904 | } | ||
905 | |||
906 | #define DEFINE_SET_FRAME(name, value) \ | ||
907 | static int set_frame_##name(PaletteUseContext *s, AVFrame *out, AVFrame *in, \ | ||
908 | int x_start, int y_start, int w, int h) \ | ||
909 | { \ | ||
910 | return set_frame(s, out, in, x_start, y_start, w, h, value); \ | ||
911 | } | ||
912 | |||
913 | 71 | DEFINE_SET_FRAME(none, DITHERING_NONE) | |
914 | 142 | DEFINE_SET_FRAME(bayer, DITHERING_BAYER) | |
915 | ✗ | DEFINE_SET_FRAME(heckbert, DITHERING_HECKBERT) | |
916 | ✗ | DEFINE_SET_FRAME(floyd_steinberg, DITHERING_FLOYD_STEINBERG) | |
917 | ✗ | DEFINE_SET_FRAME(sierra2, DITHERING_SIERRA2) | |
918 | 71 | DEFINE_SET_FRAME(sierra2_4a, DITHERING_SIERRA2_4A) | |
919 | ✗ | DEFINE_SET_FRAME(sierra3, DITHERING_SIERRA3) | |
920 | ✗ | DEFINE_SET_FRAME(burkes, DITHERING_BURKES) | |
921 | ✗ | DEFINE_SET_FRAME(atkinson, DITHERING_ATKINSON) | |
922 | |||
923 | static const set_frame_func set_frame_lut[NB_DITHERING] = { | ||
924 | [DITHERING_NONE] = set_frame_none, | ||
925 | [DITHERING_BAYER] = set_frame_bayer, | ||
926 | [DITHERING_HECKBERT] = set_frame_heckbert, | ||
927 | [DITHERING_FLOYD_STEINBERG] = set_frame_floyd_steinberg, | ||
928 | [DITHERING_SIERRA2] = set_frame_sierra2, | ||
929 | [DITHERING_SIERRA2_4A] = set_frame_sierra2_4a, | ||
930 | [DITHERING_SIERRA3] = set_frame_sierra3, | ||
931 | [DITHERING_BURKES] = set_frame_burkes, | ||
932 | [DITHERING_ATKINSON] = set_frame_atkinson, | ||
933 | }; | ||
934 | |||
935 | 256 | static int dither_value(int p) | |
936 | { | ||
937 | 256 | const int q = p ^ (p >> 3); | |
938 | 256 | return (p & 4) >> 2 | (q & 4) >> 1 \ | |
939 | 256 | | (p & 2) << 1 | (q & 2) << 2 \ | |
940 | 256 | | (p & 1) << 4 | (q & 1) << 5; | |
941 | } | ||
942 | |||
943 | 8 | static av_cold int init(AVFilterContext *ctx) | |
944 | { | ||
945 | 8 | PaletteUseContext *s = ctx->priv; | |
946 | |||
947 | 8 | s->last_in = av_frame_alloc(); | |
948 | 8 | s->last_out = av_frame_alloc(); | |
949 |
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) |
950 | ✗ | return AVERROR(ENOMEM); | |
951 | |||
952 | 8 | s->set_frame = set_frame_lut[s->dither]; | |
953 | |||
954 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
|
8 | if (s->dither == DITHERING_BAYER) { |
955 | 4 | const int delta = 1 << (5 - s->bayer_scale); // to avoid too much luma | |
956 | |||
957 |
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++) |
958 | 256 | s->ordered_dither[i] = (dither_value(i) >> s->bayer_scale) - delta; | |
959 | } | ||
960 | |||
961 | 8 | return 0; | |
962 | } | ||
963 | |||
964 | 580 | static int activate(AVFilterContext *ctx) | |
965 | { | ||
966 | 580 | PaletteUseContext *s = ctx->priv; | |
967 | 580 | return ff_framesync_activate(&s->fs); | |
968 | } | ||
969 | |||
970 | 8 | static av_cold void uninit(AVFilterContext *ctx) | |
971 | { | ||
972 | 8 | PaletteUseContext *s = ctx->priv; | |
973 | |||
974 | 8 | ff_framesync_uninit(&s->fs); | |
975 |
2/2✓ Branch 0 taken 262144 times.
✓ Branch 1 taken 8 times.
|
262152 | for (int i = 0; i < CACHE_SIZE; i++) |
976 | 262144 | av_freep(&s->cache[i].entries); | |
977 | 8 | av_frame_free(&s->last_in); | |
978 | 8 | av_frame_free(&s->last_out); | |
979 | 8 | } | |
980 | |||
981 | static const AVFilterPad paletteuse_inputs[] = { | ||
982 | { | ||
983 | .name = "default", | ||
984 | .type = AVMEDIA_TYPE_VIDEO, | ||
985 | },{ | ||
986 | .name = "palette", | ||
987 | .type = AVMEDIA_TYPE_VIDEO, | ||
988 | .config_props = config_input_palette, | ||
989 | }, | ||
990 | }; | ||
991 | |||
992 | static const AVFilterPad paletteuse_outputs[] = { | ||
993 | { | ||
994 | .name = "default", | ||
995 | .type = AVMEDIA_TYPE_VIDEO, | ||
996 | .config_props = config_output, | ||
997 | }, | ||
998 | }; | ||
999 | |||
1000 | const AVFilter ff_vf_paletteuse = { | ||
1001 | .name = "paletteuse", | ||
1002 | .description = NULL_IF_CONFIG_SMALL("Use a palette to downsample an input video stream."), | ||
1003 | .priv_size = sizeof(PaletteUseContext), | ||
1004 | .init = init, | ||
1005 | .uninit = uninit, | ||
1006 | .activate = activate, | ||
1007 | FILTER_INPUTS(paletteuse_inputs), | ||
1008 | FILTER_OUTPUTS(paletteuse_outputs), | ||
1009 | FILTER_QUERY_FUNC2(query_formats), | ||
1010 | .priv_class = &paletteuse_class, | ||
1011 | }; | ||
1012 |