59 #define HIST_SIZE (1<<(3*NBITS))
77 #define OFFSET(x) offsetof(PaletteGenContext, x)
78 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
80 {
"max_colors",
"set the maximum number of colors to use in the palette",
OFFSET(max_colors),
AV_OPT_TYPE_INT, {.i64=256}, 4, 256,
FLAGS },
81 {
"reserve_transparent",
"reserve a palette entry for transparency",
OFFSET(reserve_transparent),
AV_OPT_TYPE_INT, {.i64=1}, 0, 1,
FLAGS },
106 typedef int (*
cmp_func)(
const void *,
const void *);
108 #define DECLARE_CMP_FUNC(name, pos) \
109 static int cmp_##name(const void *pa, const void *pb) \
111 const struct color_ref * const *a = pa; \
112 const struct color_ref * const *b = pb; \
113 return ((*a)->color >> (8 * (2 - (pos))) & 0xff) \
114 - ((*b)->color >> (8 * (2 - (pos))) & 0xff); \
135 const uint8_t c1[] = {a >> 16 & 0xff, a >> 8 & 0xff, a & 0xff};
136 const uint8_t c2[] = {b >> 16 & 0xff, b >> 8 & 0xff, b & 0xff};
137 const int dr = c1[0] - c2[0];
138 const int dg = c1[1] - c2[1];
139 const int db = c1[2] - c2[2];
140 return dr*dr + dg*dg + db*db;
148 int box_id, i, best_box_id = -1;
149 int64_t max_variance = -1;
154 for (box_id = 0; box_id < s->
nb_boxes; box_id++) {
162 for (i = 0; i < box->
len; i++) {
169 best_box_id = box_id;
187 const int n = box->
len;
188 uint64_t
r = 0,
g = 0,
b = 0, div = 0;
190 for (i = 0; i <
n; i++) {
202 return 0xff
U<<24 | r<<16 |
g<<8 |
b;
212 new_box->
start = n + 1;
232 int x,
y, box_id = 0;
233 uint32_t *pal = (uint32_t *)out->
data[0];
234 const int pal_linesize = out->
linesize[0] >> 2;
235 uint32_t last_color = 0;
238 for (x = 0; x < out->
width; x++) {
239 if (box_id < s->nb_boxes) {
241 if ((x || y) && pal[x] == last_color)
253 pal[out->
width - pal_linesize - 1] = 0x0000ff00;
283 const double ratio = (double)nb_out / nb_in;
284 snprintf(buf,
sizeof(buf),
"%f", ratio);
316 box = &s->
boxes[box_id];
323 while (box && box->
len > 1) {
324 int i, rr, gr, br, longest;
325 uint64_t median, box_weight = 0;
330 uint8_t max[3] = {0x00, 0x00, 0x00};
333 const uint32_t rgb = ref->
color;
334 const uint8_t r = rgb >> 16 & 0xff,
g = rgb >> 8 & 0xff,
b = rgb & 0xff;
335 min[0] =
FFMIN(r, min[0]), max[0] =
FFMAX(r, max[0]);
336 min[1] =
FFMIN(g, min[1]), max[1] =
FFMAX(g, max[1]);
337 min[2] =
FFMIN(b, min[2]), max[2] =
FFMAX(b, max[2]);
338 box_weight += ref->
count;
342 rr = max[0] - min[0];
343 gr = max[1] - min[1];
344 br = max[2] - min[2];
346 if (br >= rr && br >= gr) longest = 2;
347 if (rr >= gr && rr >= br) longest = 0;
348 if (gr >= rr && gr >= br) longest = 1;
350 av_dlog(ctx,
"box #%02X [%6d..%-6d] (%6d) w:%-6"PRIu64
" ranges:[%2x %2x %2x] sort by %c (already sorted:%c) ",
352 rr, gr, br,
"rgb"[longest], box->
sorted_by == longest ?
'y':
'n');
362 median = (box_weight + 1) >> 1;
368 if (box_weight > median)
371 av_dlog(ctx,
"split @ i=%-6d with w=%-6"PRIu64
" (target=%6"PRIu64
")\n", i, box_weight, median);
375 box = box_id >= 0 ? &s->
boxes[box_id] :
NULL;
414 if (e->
color == color) {
435 int x,
y,
ret, nb_diff_colors = 0;
437 for (y = 0; y < f1->
height; y++) {
438 const uint32_t *p = (
const uint32_t *)(f1->
data[0] + y*f1->
linesize[0]);
439 const uint32_t *q = (
const uint32_t *)(f2->
data[0] + y*f2->
linesize[0]);
441 for (x = 0; x < f1->
width; x++) {
447 nb_diff_colors +=
ret;
450 return nb_diff_colors;
458 int x,
y,
ret, nb_diff_colors = 0;
460 for (y = 0; y < f->
height; y++) {
461 const uint32_t *p = (
const uint32_t *)(f->
data[0] + y*f->
linesize[0]);
463 for (x = 0; x < f->
width; x++) {
467 nb_diff_colors +=
ret;
470 return nb_diff_colors;
520 outlink->
w = outlink->
h = 16;
557 .
name =
"palettegen",
562 .
inputs = palettegen_inputs,
564 .priv_class = &palettegen_class,