Go to the documentation of this file.
62 #define HIST_SIZE (1<<(4*NBITS))
82 #define OFFSET(x) offsetof(PaletteGenContext, x)
83 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
85 {
"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 },
86 {
"reserve_transparent",
"reserve a palette entry for transparency",
OFFSET(reserve_transparent),
AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1,
FLAGS },
87 {
"transparency_color",
"set a background color for transparency",
OFFSET(transparency_color),
AV_OPT_TYPE_COLOR, {.str=
"lime"}, 0, 0,
FLAGS },
113 #define DECLARE_CMP_FUNC(name, pos) \
114 static int cmp_##name(const void *pa, const void *pb) \
116 const struct color_ref * const *a = pa; \
117 const struct color_ref * const *b = pb; \
118 return (int)((*a)->color >> (8 * (3 - (pos))) & 0xff) \
119 - (int)((*b)->color >> (8 * (3 - (pos))) & 0xff); \
141 const uint8_t
c1[] = {
a >> 16 & 0xff,
a >> 8 & 0xff,
a & 0xff};
142 const uint8_t
c2[] = {
b >> 16 & 0xff,
b >> 8 & 0xff,
b & 0xff};
143 const int dr =
c1[0] -
c2[0];
144 const int dg =
c1[1] -
c2[1];
145 const int db =
c1[2] -
c2[2];
146 return dr*dr + dg*dg + db*db;
151 const uint8_t
c1[] = {
a >> 24 & 0xff,
a >> 16 & 0xff,
a >> 8 & 0xff,
a & 0xff};
152 const uint8_t
c2[] = {
b >> 24 & 0xff,
b >> 16 & 0xff,
b >> 8 & 0xff,
b & 0xff};
153 const int da =
c1[0] -
c2[0];
154 const int dr =
c1[1] -
c2[1];
155 const int dg =
c1[2] -
c2[2];
156 const int db =
c1[3] -
c2[3];
157 return da*da + dr*dr + dg*dg + db*db;
165 int box_id,
i, best_box_id = -1;
166 int64_t max_variance = -1;
168 if (
s->nb_boxes ==
s->max_colors -
s->reserve_transparent)
171 for (box_id = 0; box_id <
s->nb_boxes; box_id++) {
174 if (
s->boxes[box_id].len >= 2) {
179 for (
i = 0;
i < box->
len;
i++) {
189 best_box_id = box_id;
204 const struct range_box *box,
int use_alpha)
207 const int n = box->
len;
208 uint64_t
a = 0,
r = 0,
g = 0,
b = 0, div = 0;
210 for (
i = 0;
i < n;
i++) {
213 a += (
ref->color >> 24 & 0xff) *
ref->count;
214 r += (
ref->color >> 16 & 0xff) *
ref->count;
215 g += (
ref->color >> 8 & 0xff) *
ref->count;
216 b += (
ref->color & 0xff) *
ref->count;
227 return a<<24 |
r<<16 |
g<<8 |
b;
229 return 0xff
U<<24 |
r<<16 |
g<<8 |
b;
238 struct range_box *new_box = &
s->boxes[
s->nb_boxes++];
239 new_box->
start = n + 1;
259 int x, y, box_id = 0;
260 uint32_t *pal = (uint32_t *)
out->data[0];
261 const int pal_linesize =
out->linesize[0] >> 2;
262 uint32_t last_color = 0;
264 for (y = 0; y <
out->height; y++) {
265 for (x = 0; x <
out->width; x++) {
266 if (box_id < s->nb_boxes) {
267 pal[x] =
s->boxes[box_id++].color;
268 if ((x || y) && pal[x] == last_color)
278 if (
s->reserve_transparent && !
s->use_alpha) {
280 pal[
out->width - pal_linesize - 1] =
AV_RB32(&
s->transparency_color) >> 8;
310 const double ratio = (double)nb_out / nb_in;
311 snprintf(buf,
sizeof(buf),
"%f", ratio);
343 box = &
s->boxes[box_id];
344 box->
len =
s->nb_refs;
350 while (box && box->
len > 1) {
351 int i, ar, rr, gr, br, longest;
352 uint64_t median, box_weight = 0;
356 uint8_t
min[4] = {0xff, 0xff, 0xff, 0xff};
357 uint8_t
max[4] = {0x00, 0x00, 0x00, 0x00};
360 const uint32_t
rgb =
ref->color;
361 const uint8_t
a =
rgb >> 24 & 0xff,
r =
rgb >> 16 & 0xff,
g =
rgb >> 8 & 0xff,
b =
rgb & 0xff;
366 box_weight +=
ref->count;
376 if (ar >= rr && ar >= br && ar >= gr) longest = 0;
377 if (br >= rr && br >= gr && br >= ar) longest = 3;
378 if (rr >= gr && rr >= br && rr >= ar) longest = 1;
379 if (gr >= rr && gr >= br && gr >= ar) longest = 2;
381 if (br >= rr && br >= gr) longest = 3;
382 if (rr >= gr && rr >= br) longest = 1;
383 if (gr >= rr && gr >= br) longest = 2;
386 ff_dlog(
ctx,
"box #%02X [%6d..%-6d] (%6d) w:%-6"PRIu64
" ranges:[%2x %2x %2x %2x] sort by %c (already sorted:%c) ",
388 ar, rr, gr, br,
"argb"[longest], box->
sorted_by == longest ?
'y' :
'n');
398 median = (box_weight + 1) >> 1;
402 for (
i = box->
start; i < box->start + box->
len - 2;
i++) {
403 box_weight +=
s->refs[
i]->count;
404 if (box_weight > median)
407 ff_dlog(
ctx,
"split @ i=%-6d with w=%-6"PRIu64
" (target=%6"PRIu64
")\n",
i, box_weight, median);
411 box = box_id >= 0 ? &
s->boxes[box_id] :
NULL;
416 s->nb_boxes,
s->reserve_transparent ?
"(+1)" :
"",
s->nb_refs, ratio);
418 qsort(
s->boxes,
s->nb_boxes,
sizeof(*
s->boxes),
cmp_color);
437 const uint8_t
a =
color >> 24 & ((1 <<
NBITS) - 1);
477 int x, y,
ret, nb_diff_colors = 0;
479 for (y = 0; y < f1->
height; y++) {
480 const uint32_t *p = (
const uint32_t *)(f1->
data[0] + y*f1->
linesize[0]);
481 const uint32_t *q = (
const uint32_t *)(f2->
data[0] + y*f2->
linesize[0]);
483 for (x = 0; x < f1->
width; x++) {
489 nb_diff_colors +=
ret;
492 return nb_diff_colors;
500 int x, y,
ret, nb_diff_colors = 0;
502 for (y = 0; y <
f->height; y++) {
503 const uint32_t *p = (
const uint32_t *)(
f->data[0] + y*
f->linesize[0]);
505 for (x = 0; x <
f->width; x++) {
509 nb_diff_colors +=
ret;
512 return nb_diff_colors;
544 memset(
s->boxes, 0,
sizeof(
s->boxes));
545 memset(
s->histogram, 0,
sizeof(
s->histogram));
566 s->palette_pushed = 1;
577 outlink->
w = outlink->
h = 16;
586 if (
s->use_alpha &&
s->reserve_transparent)
587 s->reserve_transparent = 0;
621 .
name =
"palettegen",
629 .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.
#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.
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
#define AVERROR_EOF
End of file.
#define DECLARE_CMP_FUNC(name, pos)
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.
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.
static int color_inc(struct hist_node *hist, uint32_t color, int use_alpha)
Locate the color in the hash table and increment its counter.
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.
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.
static av_cold void uninit(AVFilterContext *ctx)
#define FILTER_INPUTS(array)
Describe the class of an AVClass context structure.
static uint32_t get_avg_color(struct color_ref *const *refs, const struct range_box *box, int use_alpha)
Get the 32-bit average color for the range of RGB colors enclosed in the specified box.
int(* cmp_func)(const void *, const void *)
static int query_formats(AVFilterContext *ctx)
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.
static av_always_inline int diff_alpha(const uint32_t a, const uint32_t b)
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 variance.
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)
#define AV_QSORT(p, num, type, cmp)
Quicksort This sort is fast, and fully inplace but not stable and it is possible to construct input t...
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, int use_alpha)
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]
static unsigned color_hash(uint32_t color, int use_alpha)
Hashing function for the color.
static void split_box(PaletteGenContext *s, struct range_box *box, int n)
Split given box in two at position n.
static av_always_inline int diff(const uint32_t a, const uint32_t b)
@ STATS_MODE_SINGLE_FRAMES
#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.
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
static int update_histogram_diff(struct hist_node *hist, const AVFrame *f1, const AVFrame *f2, int use_alpha)
Update histogram when pixels differ from previous frame.
uint8_t transparency_color[4]
static double set_colorquant_ratio_meta(AVFrame *out, int nb_out, int nb_in)