69#define HIST_SIZE (1<<15)
88#define OFFSET(x) offsetof(PaletteGenContext, x)
89#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
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 },
118typedef int (*
cmp_func)(
const void *,
const void *);
120#define DECLARE_CMP_FUNC(k0, k1, k2) \
121static int cmp_##k0##k1##k2(const void *pa, const void *pb) \
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; \
139static const char *
const sortstr[] = {
"Lab",
"Lba",
"bLa",
"aLb",
"baL",
"abL" };
157 if (y >= z)
return ID_XYZ;
158 if (x >= z)
return ID_XZY;
161 if (x >= z)
return ID_YXZ;
162 if (y >= z)
return ID_YZX;
181 int64_t sL = 0, sa = 0, sb = 0;
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;
190 box->avg.L = sL /
box->weight;
191 box->avg.a = sa /
box->weight;
192 box->avg.b = sb /
box->weight;
200 er2[0] += dL * dL *
ref->count;
201 er2[1] += da * da *
ref->count;
202 er2[2] += db * db *
ref->count;
206 box->major_axis =
sort3id(er2[0], er2[1], er2[2]);
209 box->cut_score =
FFMAX3(er2[0], er2[1], er2[2]);
217 int best_box_id = -1;
220 if (
s->nb_boxes ==
s->max_colors -
s->reserve_transparent)
223 for (
int box_id = 0; box_id <
s->nb_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;
239 struct range_box *new_box = &
s->boxes[
s->nb_boxes++];
240 new_box->
start = n + 1;
259 uint32_t *pal = (uint32_t *)
out->data[0];
260 const int pal_linesize =
out->linesize[0] >> 2;
261 uint32_t last_color = 0;
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)
277 if (
s->reserve_transparent) {
279 pal[
out->width - pal_linesize - 1] =
AV_RB32(&
s->transparency_color) >> 8;
309 const double ratio = (
double)nb_out / nb_in;
310 snprintf(buf,
sizeof(buf),
"%f", ratio);
342 box = &
s->boxes[box_id];
343 box->len =
s->nb_refs;
348 while (
box &&
box->len > 1) {
352 ff_dlog(
ctx,
"box #%02X [%6d..%-6d] (%6d) w:%-6"PRIu64
" sort by %s (already sorted:%c) ",
357 if (
box->sorted_by !=
box->major_axis) {
360 box->sorted_by =
box->major_axis;
364 median = (
box->weight + 1) >> 1;
373 ff_dlog(
ctx,
"split @ i=%-6d with w=%-6"PRIu64
" (target=%6"PRIu64
")\n",
i,
weight, median);
377 box = box_id >= 0 ? &
s->boxes[box_id] :
NULL;
382 s->nb_boxes,
s->reserve_transparent ?
"(+1)" :
"",
s->nb_refs, ratio);
384 for (
int i = 0;
i <
s->nb_boxes;
i++)
387 qsort(
s->boxes,
s->nb_boxes,
sizeof(*
s->boxes),
cmp_color);
427 int x, y, ret, nb_diff_colors = 0;
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]);
433 for (x = 0; x < f1->
width; x++) {
439 nb_diff_colors += ret;
442 return nb_diff_colors;
450 int x, y, ret, nb_diff_colors = 0;
452 for (y = 0; y <
f->height; y++) {
453 const uint32_t *p = (
const uint32_t *)(
f->data[0] + y*
f->linesize[0]);
455 for (x = 0; x <
f->width; x++) {
459 nb_diff_colors += ret;
462 return nb_diff_colors;
498 memset(
s->boxes, 0,
sizeof(
s->boxes));
499 memset(
s->histogram, 0,
sizeof(
s->histogram));
520 s->palette_pushed = 1;
531 outlink->
w = outlink->
h = 16;
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");
577 .p.name =
"palettegen",
579 .p.priv_class = &palettegen_class,
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
static int request_frame(AVFilterLink *outlink)
const FFFilter ff_vf_palettegen
static uint8_t hash[HASH_SIZE]
static AVFormatContext * ctx
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
Main libavfilter public API header.
#define i(width, name, range_min, range_max)
static int filter_frame(DBEDecodeContext *s, AVFrame *frame)
int(* init)(AVBSFContext *ctx)
static av_unused double box(double x, const double *params)
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
@ AV_OPT_TYPE_INT
Underlying C type is int.
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
@ AV_OPT_TYPE_COLOR
Underlying C type is uint8_t[4].
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
#define AVERROR_EOF
End of file.
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
#define AV_LOG_WARNING
Something somehow does not look correct.
#define AV_LOG_INFO
Standard information.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
static AVRational av_make_q(int num, int den)
Create an AVRational.
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.
static av_cold void uninit(AVBitStreamFilterContext *ctx)
static int config_output(AVBitStreamFilterLink *outlink)
#define FILTER_INPUTS(array)
#define FILTER_OUTPUTS(array)
#define AVFILTER_DEFINE_CLASS(fname)
#define FILTER_QUERY_FUNC2(func)
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
#define FFDIFFSIGN(x, y)
Comparator.
Memory handling functions.
uint32_t ff_lowbias32(uint32_t x)
struct Lab ff_srgb_u8_to_oklab_int(uint32_t srgb)
sRGB (non-linear) to OkLab conversion
uint32_t ff_oklab_int_to_srgb_u8(struct Lab c)
OkLab to sRGB (non-linear) conversion.
AVPixelFormat
Pixel format.
@ AVCOL_TRC_IEC61966_2_1
IEC 61966-2-1 (sRGB or sYCC)
const h264_weight_func weight
Describe the class of an AVClass context structure.
A link between two filters.
int w
agreed upon image width
int h
agreed upon image height
AVFilterContext * src
source filter
AVRational sample_aspect_ratio
agreed upon sample aspect ratio
AVFilterContext * dst
dest filter
A filter pad used for either input or output.
This structure describes decoded (raw) audio or video data.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
enum AVColorTransferCharacteristic color_trc
struct range_box boxes[256]
uint8_t transparency_color[4]
struct hist_node histogram[HIST_SIZE]
struct color_ref * entries
#define av_malloc_array(a, b)
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.
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)
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.
@ STATS_MODE_SINGLE_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[]
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.