FFmpeg
Loading...
Searching...
No Matches
vf_palettegen.c
Go to the documentation of this file.
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 * Generate one palette for a whole video stream.
25 */
26
27#include "libavutil/avassert.h"
28#include "libavutil/internal.h"
29#include "libavutil/mem.h"
30#include "libavutil/opt.h"
32#include "avfilter.h"
33#include "filters.h"
34#include "formats.h"
35#include "palette.h"
36#include "video.h"
37
38/* Reference a color and how much it's used */
39struct color_ref {
40 uint32_t color;
41 struct Lab lab;
43};
44
45/* Store a range of colors */
46struct range_box {
47 uint32_t color; // average color
48 struct Lab avg; // average color in perceptual OkLab space
49 int major_axis; // best axis candidate for cutting the box
50 int64_t weight; // sum of all the weights of the colors
51 int64_t cut_score; // how likely the box is to be cut down (higher implying more likely)
52 int start; // index in PaletteGenContext->refs
53 int len; // number of referenced colors
54 int sorted_by; // whether range of colors is sorted by red (0), green (1) or blue (2)
55};
56
57struct hist_node {
60};
61
62enum {
67};
68
69#define HIST_SIZE (1<<15)
70
71typedef struct PaletteGenContext {
72 const AVClass *class;
73
77
78 AVFrame *prev_frame; // previous frame used for the diff stats_mode
79 struct hist_node histogram[HIST_SIZE]; // histogram/hashtable of the colors
80 struct color_ref **refs; // references of all the colors used in the stream
81 int nb_refs; // number of color references (or number of different colors)
82 struct range_box boxes[256]; // define the segmentation of the colorspace (the final palette)
83 int nb_boxes; // number of boxes (increase will segmenting them)
84 int palette_pushed; // if the palette frame is pushed into the outlink or not
85 uint8_t transparency_color[4]; // background color for transparency
87
88#define OFFSET(x) offsetof(PaletteGenContext, x)
89#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
90static const AVOption palettegen_options[] = {
91 { "max_colors", "set the maximum number of colors to use in the palette", OFFSET(max_colors), AV_OPT_TYPE_INT, {.i64=256}, 2, 256, FLAGS },
92 { "reserve_transparent", "reserve a palette entry for transparency", OFFSET(reserve_transparent), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
93 { "transparency_color", "set a background color for transparency", OFFSET(transparency_color), AV_OPT_TYPE_COLOR, {.str="lime"}, 0, 0, FLAGS },
94 { "stats_mode", "set statistics mode", OFFSET(stats_mode), AV_OPT_TYPE_INT, {.i64=STATS_MODE_ALL_FRAMES}, 0, NB_STATS_MODE-1, FLAGS, .unit = "mode" },
95 { "full", "compute full frame histograms", 0, AV_OPT_TYPE_CONST, {.i64=STATS_MODE_ALL_FRAMES}, INT_MIN, INT_MAX, FLAGS, .unit = "mode" },
96 { "diff", "compute histograms only for the part that differs from previous frame", 0, AV_OPT_TYPE_CONST, {.i64=STATS_MODE_DIFF_FRAMES}, INT_MIN, INT_MAX, FLAGS, .unit = "mode" },
97 { "single", "compute new histogram for each frame", 0, AV_OPT_TYPE_CONST, {.i64=STATS_MODE_SINGLE_FRAMES}, INT_MIN, INT_MAX, FLAGS, .unit = "mode" },
98 { NULL }
99};
100
102
104 AVFilterFormatsConfig **cfg_in,
105 AVFilterFormatsConfig **cfg_out)
106{
107 static const enum AVPixelFormat in_fmts[] = {AV_PIX_FMT_RGB32, AV_PIX_FMT_NONE};
108 static const enum AVPixelFormat out_fmts[] = {AV_PIX_FMT_RGB32, AV_PIX_FMT_NONE};
109 int ret;
110
111 if ((ret = ff_formats_ref(ff_make_pixel_format_list(in_fmts) , &cfg_in[0]->formats)) < 0)
112 return ret;
113 if ((ret = ff_formats_ref(ff_make_pixel_format_list(out_fmts), &cfg_out[0]->formats)) < 0)
114 return ret;
115 return 0;
116}
117
118typedef int (*cmp_func)(const void *, const void *);
119
120#define DECLARE_CMP_FUNC(k0, k1, k2) \
121static int cmp_##k0##k1##k2(const void *pa, const void *pb) \
122{ \
123 const struct color_ref * const *a = pa; \
124 const struct color_ref * const *b = pb; \
125 const int c0 = FFDIFFSIGN((*a)->lab.k0, (*b)->lab.k0); \
126 const int c1 = FFDIFFSIGN((*a)->lab.k1, (*b)->lab.k1); \
127 const int c2 = FFDIFFSIGN((*a)->lab.k2, (*b)->lab.k2); \
128 return c0 ? c0 : c1 ? c1 : c2; \
129}
130
137
139static const char * const sortstr[] = { "Lab", "Lba", "bLa", "aLb", "baL", "abL" };
140
141static const cmp_func cmp_funcs[] = {
142 [ID_XYZ] = cmp_Lab,
143 [ID_XZY] = cmp_Lba,
144 [ID_ZXY] = cmp_bLa,
145 [ID_YXZ] = cmp_aLb,
146 [ID_ZYX] = cmp_baL,
147 [ID_YZX] = cmp_abL,
148};
149
150/*
151 * Return an identifier for the order of x, y, z (from higher to lower),
152 * preferring x over y and y over z in case of equality.
153 */
154static int sort3id(int64_t x, int64_t y, int64_t z)
155{
156 if (x >= y) {
157 if (y >= z) return ID_XYZ;
158 if (x >= z) return ID_XZY;
159 return ID_ZXY;
160 }
161 if (x >= z) return ID_YXZ;
162 if (y >= z) return ID_YZX;
163 return ID_ZYX;
164}
165
166/**
167 * Simple color comparison for sorting the final palette
168 */
169static int cmp_color(const void *a, const void *b)
170{
171 const struct range_box *box1 = a;
172 const struct range_box *box2 = b;
173 return FFDIFFSIGN(box1->color, box2->color);
174}
175
177{
178 int64_t er2[3] = {0};
179
180 /* Compute average color */
181 int64_t sL = 0, sa = 0, sb = 0;
182 box->weight = 0;
183 for (int i = box->start; i < box->start + box->len; i++) {
184 const struct color_ref *ref = s->refs[i];
185 sL += ref->lab.L * ref->count;
186 sa += ref->lab.a * ref->count;
187 sb += ref->lab.b * ref->count;
188 box->weight += ref->count;
189 }
190 box->avg.L = sL / box->weight;
191 box->avg.a = sa / box->weight;
192 box->avg.b = sb / box->weight;
193
194 /* Compute squared error of each color channel */
195 for (int i = box->start; i < box->start + box->len; i++) {
196 const struct color_ref *ref = s->refs[i];
197 const int64_t dL = ref->lab.L - box->avg.L;
198 const int64_t da = ref->lab.a - box->avg.a;
199 const int64_t db = ref->lab.b - box->avg.b;
200 er2[0] += dL * dL * ref->count;
201 er2[1] += da * da * ref->count;
202 er2[2] += db * db * ref->count;
203 }
204
205 /* Define the best axis candidate for cutting the box */
206 box->major_axis = sort3id(er2[0], er2[1], er2[2]);
207
208 /* The box that has the axis with the biggest error amongst all boxes will but cut down */
209 box->cut_score = FFMAX3(er2[0], er2[1], er2[2]);
210}
211
212/**
213 * Find the next box to split: pick the one with the highest cut score
214 */
216{
217 int best_box_id = -1;
218 int64_t max_score = -1;
219
220 if (s->nb_boxes == s->max_colors - s->reserve_transparent)
221 return -1;
222
223 for (int box_id = 0; box_id < s->nb_boxes; box_id++) {
224 const struct range_box *box = &s->boxes[box_id];
225 if (s->boxes[box_id].len >= 2 && box->cut_score > max_score) {
226 best_box_id = box_id;
227 max_score = box->cut_score;
228 }
229 }
230 return best_box_id;
231}
232
233/**
234 * Split given box in two at position n. The original box becomes the left part
235 * of the split, and the new index box is the right part.
236 */
237static void split_box(PaletteGenContext *s, struct range_box *box, int n)
238{
239 struct range_box *new_box = &s->boxes[s->nb_boxes++];
240 new_box->start = n + 1;
241 new_box->len = box->start + box->len - new_box->start;
242 new_box->sorted_by = box->sorted_by;
243 box->len -= new_box->len;
244
245 av_assert0(box->len >= 1);
246 av_assert0(new_box->len >= 1);
247
249 compute_box_stats(s, new_box);
250}
251
252/**
253 * Write the palette into the output frame.
254 */
256{
257 const PaletteGenContext *s = ctx->priv;
258 int box_id = 0;
259 uint32_t *pal = (uint32_t *)out->data[0];
260 const int pal_linesize = out->linesize[0] >> 2;
261 uint32_t last_color = 0;
262
263 for (int y = 0; y < out->height; y++) {
264 for (int x = 0; x < out->width; x++) {
265 if (box_id < s->nb_boxes) {
266 pal[x] = s->boxes[box_id++].color;
267 if ((x || y) && pal[x] == last_color)
268 av_log(ctx, AV_LOG_WARNING, "Duped color: %08"PRIX32"\n", pal[x]);
269 last_color = pal[x];
270 } else {
271 pal[x] = last_color; // pad with last color
272 }
273 }
274 pal += pal_linesize;
275 }
276
277 if (s->reserve_transparent) {
278 av_assert0(s->nb_boxes < 256);
279 pal[out->width - pal_linesize - 1] = AV_RB32(&s->transparency_color) >> 8;
280 }
281}
282
283/**
284 * Crawl the histogram to get all the defined colors, and create a linear list
285 * of them (each color reference entry is a pointer to the value in the
286 * histogram/hash table).
287 */
288static struct color_ref **load_color_refs(const struct hist_node *hist, int nb_refs)
289{
290 int k = 0;
291 struct color_ref **refs = av_malloc_array(nb_refs, sizeof(*refs));
292
293 if (!refs)
294 return NULL;
295
296 for (int j = 0; j < HIST_SIZE; j++) {
297 const struct hist_node *node = &hist[j];
298
299 for (int i = 0; i < node->nb_entries; i++)
300 refs[k++] = &node->entries[i];
301 }
302
303 return refs;
304}
305
306static double set_colorquant_ratio_meta(AVFrame *out, int nb_out, int nb_in)
307{
308 char buf[32];
309 const double ratio = (double)nb_out / nb_in;
310 snprintf(buf, sizeof(buf), "%f", ratio);
311 av_dict_set(&out->metadata, "lavfi.color_quant_ratio", buf, 0);
312 return ratio;
313}
314
315/**
316 * Main function implementing the Median Cut Algorithm defined by Paul Heckbert
317 * in Color Image Quantization for Frame Buffer Display (1982)
318 */
320{
321 AVFrame *out;
322 PaletteGenContext *s = ctx->priv;
323 AVFilterLink *outlink = ctx->outputs[0];
324 double ratio;
325 int box_id = 0;
326 struct range_box *box;
327
328 /* reference only the used colors from histogram */
329 s->refs = load_color_refs(s->histogram, s->nb_refs);
330 if (!s->refs) {
331 av_log(ctx, AV_LOG_ERROR, "Unable to allocate references for %d different colors\n", s->nb_refs);
332 return NULL;
333 }
334
335 /* create the palette frame */
336 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
337 if (!out)
338 return NULL;
339 out->pts = 0;
340
341 /* set first box for 0..nb_refs */
342 box = &s->boxes[box_id];
343 box->len = s->nb_refs;
344 box->sorted_by = -1;
346 s->nb_boxes = 1;
347
348 while (box && box->len > 1) {
349 int i;
350 int64_t median, weight;
351
352 ff_dlog(ctx, "box #%02X [%6d..%-6d] (%6d) w:%-6"PRIu64" sort by %s (already sorted:%c) ",
353 box_id, box->start, box->start + box->len - 1, box->len, box->weight,
354 sortstr[box->major_axis], box->sorted_by == box->major_axis ? 'y':'n');
355
356 /* sort the range by its major axis if it's not already sorted */
357 if (box->sorted_by != box->major_axis) {
358 cmp_func cmpf = cmp_funcs[box->major_axis];
359 qsort(&s->refs[box->start], box->len, sizeof(struct color_ref *), cmpf);
360 box->sorted_by = box->major_axis;
361 }
362
363 /* locate the median where to split */
364 median = (box->weight + 1) >> 1;
365 weight = 0;
366 /* if you have 2 boxes, the maximum is actually #0: you must have at
367 * least 1 color on each side of the split, hence the -2 */
368 for (i = box->start; i < box->start + box->len - 2; i++) {
369 weight += s->refs[i]->count;
370 if (weight > median)
371 break;
372 }
373 ff_dlog(ctx, "split @ i=%-6d with w=%-6"PRIu64" (target=%6"PRIu64")\n", i, weight, median);
374 split_box(s, box, i);
375
376 box_id = get_next_box_id_to_split(s);
377 box = box_id >= 0 ? &s->boxes[box_id] : NULL;
378 }
379
380 ratio = set_colorquant_ratio_meta(out, s->nb_boxes, s->nb_refs);
381 av_log(ctx, AV_LOG_INFO, "%d%s colors generated out of %d colors; ratio=%f\n",
382 s->nb_boxes, s->reserve_transparent ? "(+1)" : "", s->nb_refs, ratio);
383
384 for (int i = 0; i < s->nb_boxes; i++)
385 s->boxes[i].color = 0xffU<<24 | ff_oklab_int_to_srgb_u8(s->boxes[i].avg);
386
387 qsort(s->boxes, s->nb_boxes, sizeof(*s->boxes), cmp_color);
388
390
391 return out;
392}
393
394/**
395 * Locate the color in the hash table and increment its counter.
396 */
397static int color_inc(struct hist_node *hist, uint32_t color)
398{
399 const uint32_t hash = ff_lowbias32(color) & (HIST_SIZE - 1);
400 struct hist_node *node = &hist[hash];
401 struct color_ref *e;
402
403 for (int i = 0; i < node->nb_entries; i++) {
404 e = &node->entries[i];
405 if (e->color == color) {
406 e->count++;
407 return 0;
408 }
409 }
410
411 e = av_dynarray2_add((void**)&node->entries, &node->nb_entries,
412 sizeof(*node->entries), NULL);
413 if (!e)
414 return AVERROR(ENOMEM);
415 e->color = color;
417 e->count = 1;
418 return 1;
419}
420
421/**
422 * Update histogram when pixels differ from previous frame.
423 */
424static int update_histogram_diff(struct hist_node *hist,
425 const AVFrame *f1, const AVFrame *f2)
426{
427 int x, y, ret, nb_diff_colors = 0;
428
429 for (y = 0; y < f1->height; y++) {
430 const uint32_t *p = (const uint32_t *)(f1->data[0] + y*f1->linesize[0]);
431 const uint32_t *q = (const uint32_t *)(f2->data[0] + y*f2->linesize[0]);
432
433 for (x = 0; x < f1->width; x++) {
434 if (p[x] == q[x])
435 continue;
436 ret = color_inc(hist, p[x]);
437 if (ret < 0)
438 return ret;
439 nb_diff_colors += ret;
440 }
441 }
442 return nb_diff_colors;
443}
444
445/**
446 * Simple histogram of the frame.
447 */
448static int update_histogram_frame(struct hist_node *hist, const AVFrame *f)
449{
450 int x, y, ret, nb_diff_colors = 0;
451
452 for (y = 0; y < f->height; y++) {
453 const uint32_t *p = (const uint32_t *)(f->data[0] + y*f->linesize[0]);
454
455 for (x = 0; x < f->width; x++) {
456 ret = color_inc(hist, p[x]);
457 if (ret < 0)
458 return ret;
459 nb_diff_colors += ret;
460 }
461 }
462 return nb_diff_colors;
463}
464
465/**
466 * Update the histogram for each passing frame. No frame will be pushed here.
467 */
468static int filter_frame(AVFilterLink *inlink, AVFrame *in)
469{
470 AVFilterContext *ctx = inlink->dst;
471 PaletteGenContext *s = ctx->priv;
472 int ret;
473
475 av_log(ctx, AV_LOG_WARNING, "The input frame is not in sRGB, colors may be off\n");
476
477 ret = s->prev_frame ? update_histogram_diff(s->histogram, s->prev_frame, in)
478 : update_histogram_frame(s->histogram, in);
479 if (ret > 0)
480 s->nb_refs += ret;
481
482 if (s->stats_mode == STATS_MODE_DIFF_FRAMES) {
483 av_frame_free(&s->prev_frame);
484 s->prev_frame = in;
485 } else if (s->stats_mode == STATS_MODE_SINGLE_FRAMES && s->nb_refs > 0) {
486 AVFrame *out;
487 int i;
488
490 out->pts = in->pts;
491 av_frame_free(&in);
492 ret = ff_filter_frame(ctx->outputs[0], out);
493 for (i = 0; i < HIST_SIZE; i++)
494 av_freep(&s->histogram[i].entries);
495 av_freep(&s->refs);
496 s->nb_refs = 0;
497 s->nb_boxes = 0;
498 memset(s->boxes, 0, sizeof(s->boxes));
499 memset(s->histogram, 0, sizeof(s->histogram));
500 } else {
501 av_frame_free(&in);
502 }
503
504 return ret;
505}
506
507/**
508 * Returns only one frame at the end containing the full palette.
509 */
510static int request_frame(AVFilterLink *outlink)
511{
512 AVFilterContext *ctx = outlink->src;
513 AVFilterLink *inlink = ctx->inputs[0];
514 PaletteGenContext *s = ctx->priv;
515 int r;
516
517 r = ff_request_frame(inlink);
518 if (r == AVERROR_EOF && !s->palette_pushed && s->nb_refs && s->stats_mode != STATS_MODE_SINGLE_FRAMES) {
520 s->palette_pushed = 1;
521 return r;
522 }
523 return r;
524}
525
526/**
527 * The output is one simple 16x16 squared-pixels palette.
528 */
529static int config_output(AVFilterLink *outlink)
530{
531 outlink->w = outlink->h = 16;
532 outlink->sample_aspect_ratio = av_make_q(1, 1);
533 return 0;
534}
535
537{
538 PaletteGenContext* s = ctx->priv;
539
540 if (s->max_colors - s->reserve_transparent < 2) {
541 av_log(ctx, AV_LOG_ERROR, "max_colors=2 is only allowed without reserving a transparent color slot\n");
542 return AVERROR(EINVAL);
543 }
544
545 return 0;
546}
547
549{
550 int i;
551 PaletteGenContext *s = ctx->priv;
552
553 for (i = 0; i < HIST_SIZE; i++)
554 av_freep(&s->histogram[i].entries);
555 av_freep(&s->refs);
556 av_frame_free(&s->prev_frame);
557}
558
560 {
561 .name = "default",
562 .type = AVMEDIA_TYPE_VIDEO,
563 .filter_frame = filter_frame,
564 },
565};
566
568 {
569 .name = "default",
570 .type = AVMEDIA_TYPE_VIDEO,
571 .config_props = config_output,
572 .request_frame = request_frame,
573 },
574};
575
577 .p.name = "palettegen",
578 .p.description = NULL_IF_CONFIG_SMALL("Find the optimal palette for a given stream."),
579 .p.priv_class = &palettegen_class,
580 .priv_size = sizeof(PaletteGenContext),
581 .init = init,
582 .uninit = uninit,
586};
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
Definition aeval.c:246
static int request_frame(AVFilterLink *outlink)
Definition af_aecho.c:272
const FFFilter ff_vf_palettegen
static FILE * out
static uint8_t hash[HASH_SIZE]
static AVFormatContext * ctx
#define L(x)
Definition vpx_arith.h:36
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
Definition avfilter.c:483
Main libavfilter public API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define f(width, name)
Definition cbs_vp8.c:236
#define s(width, name)
Definition cbs_vp9.c:198
#define FLAGS
Definition cmdutils.c:598
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static int filter_frame(DBEDecodeContext *s, AVFrame *frame)
Definition dolby_e.c:1067
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
#define HIST_SIZE
Definition f_ebur128.c:51
static av_unused double box(double x, const double *params)
Definition filters.c:320
int ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
Add ref as a new reference to formats.
Definition formats.c:756
av_warn_unused_result AVFilterFormats * ff_make_pixel_format_list(const enum AVPixelFormat *fmts)
Create a list of supported pixel formats.
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition opt.h:298
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition opt.h:326
@ AV_OPT_TYPE_COLOR
Underlying C type is uint8_t[4].
Definition opt.h:322
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition dict.c:86
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_INFO
Standard information.
Definition log.h:221
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
static AVRational av_make_q(int num, int den)
Create an AVRational.
Definition rational.h:71
void * av_dynarray2_add(void **tab_ptr, int *nb_ptr, size_t elem_size, const uint8_t *elem_data)
Add an element of size elem_size to a dynamic array.
Definition mem.c:341
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
int a
#define r
Definition input.c:42
#define b
Definition input.c:43
#define AV_RB32(p)
static av_cold void uninit(AVBitStreamFilterContext *ctx)
static int config_output(AVBitStreamFilterLink *outlink)
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:478
#define FILTER_QUERY_FUNC2(func)
Definition filters.h:241
#define av_cold
Definition attributes.h:117
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
#define FFMAX3(a, b, c)
Definition macros.h:48
#define FFDIFFSIGN(x, y)
Comparator.
Definition macros.h:45
Memory handling functions.
AVOptions.
uint32_t ff_lowbias32(uint32_t x)
Definition palette.c:206
struct Lab ff_srgb_u8_to_oklab_int(uint32_t srgb)
sRGB (non-linear) to OkLab conversion
Definition palette.c:165
uint32_t ff_oklab_int_to_srgb_u8(struct Lab c)
OkLab to sRGB (non-linear) conversion.
Definition palette.c:189
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ AV_PIX_FMT_NONE
Definition pixfmt.h:72
@ AVCOL_TRC_IEC61966_2_1
IEC 61966-2-1 (sRGB or sYCC)
Definition pixfmt.h:686
@ AVCOL_TRC_UNSPECIFIED
Definition pixfmt.h:675
#define AV_PIX_FMT_RGB32
Definition pixfmt.h:517
const h264_weight_func weight
formats
Definition signature.h:47
#define snprintf
Definition snprintf.h:34
Describe the class of an AVClass context structure.
Definition log.h:76
An instance of a filter.
Definition avfilter.h:273
Lists of formats / etc.
Definition avfilter.h:120
A filter pad used for either input or output.
Definition filters.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition frame.h:574
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition frame.h:493
int width
Definition frame.h:544
int height
Definition frame.h:544
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition frame.h:517
enum AVColorTransferCharacteristic color_trc
Definition frame.h:727
AVOption.
Definition opt.h:428
Definition palette.h:30
struct color_ref ** refs
struct range_box boxes[256]
uint8_t transparency_color[4]
struct hist_node histogram[HIST_SIZE]
uint32_t color
int64_t count
struct Lab lab
struct color_ref * entries
uint32_t color
int64_t weight
int64_t cut_score
struct Lab avg
#define av_malloc_array(a, b)
#define ff_dlog(a,...)
#define av_freep(p)
#define av_log(a,...)
static int ref[MAX_W *MAX_W]
static int cmp_color(const void *a, const void *b)
Simple color comparison for sorting the final palette.
@ ID_ZXY
@ ID_XYZ
@ ID_XZY
@ ID_YZX
@ ID_YXZ
@ ID_ZYX
static AVFrame * get_palette_frame(AVFilterContext *ctx)
Main function implementing the Median Cut Algorithm defined by Paul Heckbert in Color Image Quantizat...
static void compute_box_stats(PaletteGenContext *s, struct range_box *box)
static const char *const sortstr[]
static const cmp_func cmp_funcs[]
static struct color_ref ** load_color_refs(const struct hist_node *hist, int nb_refs)
Crawl the histogram to get all the defined colors, and create a linear list of them (each color refer...
static int request_frame(AVFilterLink *outlink)
Returns only one frame at the end containing the full palette.
#define DECLARE_CMP_FUNC(k0, k1, k2)
#define HIST_SIZE
static int sort3id(int64_t x, int64_t y, int64_t z)
static int update_histogram_diff(struct hist_node *hist, const AVFrame *f1, const AVFrame *f2)
Update histogram when pixels differ from previous frame.
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Update the histogram for each passing frame.
@ NB_STATS_MODE
@ STATS_MODE_ALL_FRAMES
@ STATS_MODE_SINGLE_FRAMES
@ STATS_MODE_DIFF_FRAMES
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
static int get_next_box_id_to_split(PaletteGenContext *s)
Find the next box to split: pick the one with the highest cut score.
static const AVFilterPad palettegen_outputs[]
static av_cold void uninit(AVFilterContext *ctx)
static const AVFilterPad palettegen_inputs[]
#define OFFSET(x)
static int color_inc(struct hist_node *hist, uint32_t color)
Locate the color in the hash table and increment its counter.
static int config_output(AVFilterLink *outlink)
The output is one simple 16x16 squared-pixels palette.
static void split_box(PaletteGenContext *s, struct range_box *box, int n)
Split given box in two at position n.
static int update_histogram_frame(struct hist_node *hist, const AVFrame *f)
Simple histogram of the frame.
int(* cmp_func)(const void *, const void *)
static double set_colorquant_ratio_meta(AVFrame *out, int nb_out, int nb_in)
static const AVOption palettegen_options[]
static void write_palette(AVFilterContext *ctx, AVFrame *out)
Write the palette into the output frame.
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition video.c:89