Go to the documentation of this file.
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 },
118 typedef int (*
cmp_func)(
const void *,
const void *);
120 #define DECLARE_CMP_FUNC(k0, k1, k2) \
121 static 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; \
139 static 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;
195 for (
int i = box->
start; i < box->start + box->
len;
i++) {
200 er2[0] += dL * dL *
ref->count;
201 er2[1] += da * da *
ref->count;
202 er2[2] += db * db *
ref->count;
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++) {
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;
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) ",
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 .
name =
"palettegen",
585 .priv_class = &palettegen_class,
static int request_frame(AVFilterLink *outlink)
Returns only one frame at the end containing the full palette.
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
enum AVColorTransferCharacteristic color_trc
#define AV_LOG_WARNING
Something somehow does not look correct.
AVPixelFormat
Pixel format.
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
static int config_output(AVFilterLink *outlink)
The output is one simple 16x16 squared-pixels palette.
static int sort3id(int64_t x, int64_t y, int64_t z)
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
#define AVERROR_EOF
End of file.
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
#define FILTER_INPUTS(array)
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).
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 AVFrame * get_palette_frame(AVFilterContext *ctx)
Main function implementing the Median Cut Algorithm defined by Paul Heckbert in Color Image Quantizat...
static int cmp_color(const void *a, const void *b)
Simple color comparison for sorting the final palette.
static int update_histogram_diff(struct hist_node *hist, const AVFrame *f1, const AVFrame *f2)
Update histogram when pixels differ from previous frame.
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
struct color_ref * entries
const char * name
Filter name.
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...
A link between two filters.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
AVFILTER_DEFINE_CLASS(palettegen)
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Update the histogram for each passing frame.
uint32_t ff_oklab_int_to_srgb_u8(struct Lab c)
OkLab to sRGB (non-linear) conversion.
@ AVCOL_TRC_IEC61966_2_1
IEC 61966-2-1 (sRGB or sYCC)
const h264_weight_func weight
A filter pad used for either input or output.
#define FFDIFFSIGN(x, y)
Comparator.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
static int init(AVFilterContext *ctx)
AVRational sample_aspect_ratio
agreed upon sample aspect ratio
#define av_assert0(cond)
assert() equivalent, that is always enabled.
uint32_t ff_lowbias32(uint32_t x)
static const char *const sortstr[]
static av_cold void uninit(AVFilterContext *ctx)
#define FILTER_OUTPUTS(array)
static void compute_box_stats(PaletteGenContext *s, struct range_box *box)
Describe the class of an AVClass context structure.
int(* cmp_func)(const void *, const void *)
@ AV_OPT_TYPE_COLOR
Underlying C type is uint8_t[4].
@ STATS_MODE_SINGLE_FRAMES
static void write_palette(AVFilterContext *ctx, AVFrame *out)
Write the palette into the output frame.
struct range_box boxes[256]
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
#define DECLARE_CMP_FUNC(k0, k1, k2)
static const uint32_t color[16+AV_CLASS_CATEGORY_NB]
static AVRational av_make_q(int num, int den)
Create an AVRational.
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
AVFilterContext * src
source filter
static int get_next_box_id_to_split(PaletteGenContext *s)
Find the next box to split: pick the one with the highest cut score.
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
static const AVOption palettegen_options[]
#define AV_LOG_INFO
Standard information.
#define i(width, name, range_min, range_max)
int w
agreed upon image width
#define av_malloc_array(a, b)
#define FILTER_QUERY_FUNC2(func)
static const AVFilterPad palettegen_outputs[]
const char * name
Pad name.
static int update_histogram_frame(struct hist_node *hist, const AVFrame *f)
Simple histogram of the frame.
int h
agreed upon image height
static const cmp_func cmp_funcs[]
struct hist_node histogram[HIST_SIZE]
@ AV_OPT_TYPE_INT
Underlying C type is int.
static const AVFilterPad palettegen_inputs[]
static int ref[MAX_W *MAX_W]
static void split_box(PaletteGenContext *s, struct range_box *box, int n)
Split given box in two at position n.
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
static int color_inc(struct hist_node *hist, uint32_t color)
Locate the color in the hash table and increment its counter.
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
const AVFilter ff_vf_palettegen
uint8_t transparency_color[4]
static double set_colorquant_ratio_meta(AVFrame *out, int nb_out, int nb_in)
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
struct Lab ff_srgb_u8_to_oklab_int(uint32_t srgb)
sRGB (non-linear) to OkLab conversion