Go to the documentation of this file.
68 #define HIST_SIZE (1<<15)
87 #define OFFSET(x) offsetof(PaletteGenContext, x)
88 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
90 {
"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 },
91 {
"reserve_transparent",
"reserve a palette entry for transparency",
OFFSET(reserve_transparent),
AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1,
FLAGS },
92 {
"transparency_color",
"set a background color for transparency",
OFFSET(transparency_color),
AV_OPT_TYPE_COLOR, {.str=
"lime"}, 0, 0,
FLAGS },
117 #define DECLARE_CMP_FUNC(k0, k1, k2) \
118 static int cmp_##k0##k1##k2(const void *pa, const void *pb) \
120 const struct color_ref * const *a = pa; \
121 const struct color_ref * const *b = pb; \
122 const int c0 = FFDIFFSIGN((*a)->lab.k0, (*b)->lab.k0); \
123 const int c1 = FFDIFFSIGN((*a)->lab.k1, (*b)->lab.k1); \
124 const int c2 = FFDIFFSIGN((*a)->lab.k2, (*b)->lab.k2); \
125 return c0 ? c0 : c1 ? c1 : c2; \
136 static const char *
const sortstr[] = {
"Lab",
"Lba",
"bLa",
"aLb",
"baL",
"abL" };
154 if (y >= z)
return ID_XYZ;
155 if (x >= z)
return ID_XZY;
158 if (x >= z)
return ID_YXZ;
159 if (y >= z)
return ID_YZX;
178 int64_t sL = 0, sa = 0, sb = 0;
182 sL +=
ref->lab.L *
ref->count;
183 sa +=
ref->lab.a *
ref->count;
184 sb +=
ref->lab.b *
ref->count;
192 for (
int i = box->
start; i < box->start + box->
len;
i++) {
197 er2[0] += dL * dL *
ref->count;
198 er2[1] += da * da *
ref->count;
199 er2[2] += db * db *
ref->count;
214 int best_box_id = -1;
217 if (
s->nb_boxes ==
s->max_colors -
s->reserve_transparent)
220 for (
int box_id = 0; box_id <
s->nb_boxes; box_id++) {
221 const struct range_box *box = &
s->boxes[box_id];
222 if (
s->boxes[box_id].len >= 2 && box->
cut_score > max_score) {
223 best_box_id = box_id;
236 struct range_box *new_box = &
s->boxes[
s->nb_boxes++];
237 new_box->
start = n + 1;
256 uint32_t *pal = (uint32_t *)
out->data[0];
257 const int pal_linesize =
out->linesize[0] >> 2;
258 uint32_t last_color = 0;
260 for (
int y = 0; y <
out->height; y++) {
261 for (
int x = 0; x <
out->width; x++) {
262 if (box_id < s->nb_boxes) {
263 pal[x] =
s->boxes[box_id++].color;
264 if ((x || y) && pal[x] == last_color)
274 if (
s->reserve_transparent) {
276 pal[
out->width - pal_linesize - 1] =
AV_RB32(&
s->transparency_color) >> 8;
306 const double ratio = (
double)nb_out / nb_in;
307 snprintf(buf,
sizeof(buf),
"%f", ratio);
339 box = &
s->boxes[box_id];
340 box->
len =
s->nb_refs;
345 while (box && box->
len > 1) {
349 ff_dlog(
ctx,
"box #%02X [%6d..%-6d] (%6d) w:%-6"PRIu64
" sort by %s (already sorted:%c) ",
361 median = (box->
weight + 1) >> 1;
370 ff_dlog(
ctx,
"split @ i=%-6d with w=%-6"PRIu64
" (target=%6"PRIu64
")\n",
i,
weight, median);
374 box = box_id >= 0 ? &
s->boxes[box_id] :
NULL;
379 s->nb_boxes,
s->reserve_transparent ?
"(+1)" :
"",
s->nb_refs, ratio);
381 for (
int i = 0;
i <
s->nb_boxes;
i++)
384 qsort(
s->boxes,
s->nb_boxes,
sizeof(*
s->boxes),
cmp_color);
424 int x, y,
ret, nb_diff_colors = 0;
426 for (y = 0; y < f1->
height; y++) {
427 const uint32_t *p = (
const uint32_t *)(f1->
data[0] + y*f1->
linesize[0]);
428 const uint32_t *q = (
const uint32_t *)(f2->
data[0] + y*f2->
linesize[0]);
430 for (x = 0; x < f1->
width; x++) {
436 nb_diff_colors +=
ret;
439 return nb_diff_colors;
447 int x, y,
ret, nb_diff_colors = 0;
449 for (y = 0; y <
f->height; y++) {
450 const uint32_t *p = (
const uint32_t *)(
f->data[0] + y*
f->linesize[0]);
452 for (x = 0; x <
f->width; x++) {
456 nb_diff_colors +=
ret;
459 return nb_diff_colors;
495 memset(
s->boxes, 0,
sizeof(
s->boxes));
496 memset(
s->histogram, 0,
sizeof(
s->histogram));
517 s->palette_pushed = 1;
528 outlink->
w = outlink->
h = 16;
537 if (
s->max_colors -
s->reserve_transparent < 2) {
538 av_log(
ctx,
AV_LOG_ERROR,
"max_colors=2 is only allowed without reserving a transparent color slot\n");
574 .
name =
"palettegen",
582 .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.
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.
#define FILTER_QUERY_FUNC(func)
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)
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_INPUTS(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 *)
static int query_formats(AVFilterContext *ctx)
static int weight(int i, int blen, int offset)
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
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)
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]
static const AVFilterPad palettegen_inputs[]
static int ref[MAX_W *MAX_W]
@ STATS_MODE_SINGLE_FRAMES
static void split_box(PaletteGenContext *s, struct range_box *box, int n)
Split given box in two at position n.
#define FILTER_OUTPUTS(array)
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)
struct Lab ff_srgb_u8_to_oklab_int(uint32_t srgb)
sRGB (non-linear) to OkLab conversion