Go to the documentation of this file.
62 #define HIST_SIZE (1<<(3*NBITS))
81 #define OFFSET(x) offsetof(PaletteGenContext, x)
82 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
84 {
"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 },
85 {
"reserve_transparent",
"reserve a palette entry for transparency",
OFFSET(reserve_transparent),
AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1,
FLAGS },
86 {
"transparency_color",
"set a background color for transparency",
OFFSET(transparency_color),
AV_OPT_TYPE_COLOR, {.str=
"lime"}, CHAR_MIN, CHAR_MAX,
FLAGS },
111 #define DECLARE_CMP_FUNC(name, pos) \
112 static int cmp_##name(const void *pa, const void *pb) \
114 const struct color_ref * const *a = pa; \
115 const struct color_ref * const *b = pb; \
116 return ((*a)->color >> (8 * (2 - (pos))) & 0xff) \
117 - ((*b)->color >> (8 * (2 - (pos))) & 0xff); \
138 const uint8_t c1[] = {
a >> 16 & 0xff,
a >> 8 & 0xff,
a & 0xff};
139 const uint8_t c2[] = {
b >> 16 & 0xff,
b >> 8 & 0xff,
b & 0xff};
140 const int dr =
c1[0] -
c2[0];
141 const int dg =
c1[1] -
c2[1];
142 const int db =
c1[2] -
c2[2];
143 return dr*dr + dg*dg + db*db;
151 int box_id,
i, best_box_id = -1;
152 int64_t max_variance = -1;
154 if (
s->nb_boxes ==
s->max_colors -
s->reserve_transparent)
157 for (box_id = 0; box_id <
s->nb_boxes; box_id++) {
160 if (
s->boxes[box_id].len >= 2) {
165 for (
i = 0;
i < box->
len;
i++) {
172 best_box_id = box_id;
190 const int n = box->
len;
191 uint64_t
r = 0,
g = 0,
b = 0, div = 0;
193 for (
i = 0;
i <
n;
i++) {
195 r += (
ref->color >> 16 & 0xff) *
ref->count;
196 g += (
ref->color >> 8 & 0xff) *
ref->count;
197 b += (
ref->color & 0xff) *
ref->count;
205 return 0xff
U<<24 |
r<<16 |
g<<8 |
b;
214 struct range_box *new_box = &
s->boxes[
s->nb_boxes++];
235 int x, y, box_id = 0;
236 uint32_t *pal = (uint32_t *)
out->data[0];
237 const int pal_linesize =
out->linesize[0] >> 2;
238 uint32_t last_color = 0;
240 for (y = 0; y <
out->height; y++) {
241 for (x = 0; x <
out->width; x++) {
242 if (box_id < s->nb_boxes) {
243 pal[x] =
s->boxes[box_id++].color;
244 if ((x || y) && pal[x] == last_color)
254 if (
s->reserve_transparent) {
256 pal[
out->width - pal_linesize - 1] =
AV_RB32(&
s->transparency_color) >> 8;
286 const double ratio = (double)nb_out / nb_in;
319 box = &
s->boxes[box_id];
320 box->
len =
s->nb_refs;
326 while (box && box->
len > 1) {
327 int i, rr, gr, br, longest;
328 uint64_t median, box_weight = 0;
336 const uint32_t rgb =
ref->color;
337 const uint8_t r = rgb >> 16 & 0xff,
g = rgb >> 8 & 0xff,
b = rgb & 0xff;
341 box_weight +=
ref->count;
349 if (br >= rr && br >= gr) longest = 2;
350 if (rr >= gr && rr >= br) longest = 0;
351 if (gr >= rr && gr >= br) longest = 1;
353 ff_dlog(
ctx,
"box #%02X [%6d..%-6d] (%6d) w:%-6"PRIu64
" ranges:[%2x %2x %2x] sort by %c (already sorted:%c) ",
355 rr, gr, br,
"rgb"[longest], box->
sorted_by == longest ?
'y':
'n');
365 median = (box_weight + 1) >> 1;
370 box_weight +=
s->refs[
i]->count;
371 if (box_weight > median)
374 ff_dlog(
ctx,
"split @ i=%-6d with w=%-6"PRIu64
" (target=%6"PRIu64
")\n",
i, box_weight, median);
378 box = box_id >= 0 ? &
s->boxes[box_id] :
NULL;
383 s->nb_boxes,
s->reserve_transparent ?
"(+1)" :
"",
s->nb_refs, ratio);
385 qsort(
s->boxes,
s->nb_boxes,
sizeof(*
s->boxes),
cmp_color);
438 int x, y,
ret, nb_diff_colors = 0;
440 for (y = 0; y < f1->
height; y++) {
441 const uint32_t *p = (
const uint32_t *)(f1->
data[0] + y*f1->
linesize[0]);
442 const uint32_t *q = (
const uint32_t *)(f2->
data[0] + y*f2->
linesize[0]);
444 for (x = 0; x < f1->
width; x++) {
450 nb_diff_colors +=
ret;
453 return nb_diff_colors;
461 int x, y,
ret, nb_diff_colors = 0;
463 for (y = 0; y <
f->height; y++) {
464 const uint32_t *p = (
const uint32_t *)(
f->data[0] + y*
f->linesize[0]);
466 for (x = 0; x <
f->width; x++) {
470 nb_diff_colors +=
ret;
473 return nb_diff_colors;
505 memset(
s->boxes, 0,
sizeof(
s->boxes));
506 memset(
s->histogram, 0,
sizeof(
s->histogram));
527 s->palette_pushed = 1;
538 outlink->
w = outlink->
h = 16;
574 .
name =
"palettegen",
581 .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.
@ STATS_MODE_SINGLE_FRAMES
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.
static uint32_t get_avg_color(struct color_ref *const *refs, const struct range_box *box)
Get the 32-bit average color for the range of RGB colors enclosed in the specified box.
#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.
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.
A filter pad used for either input or output.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
AVRational sample_aspect_ratio
agreed upon sample aspect ratio
#define av_assert0(cond)
assert() equivalent, that is always enabled.
static const AVFilterPad outputs[]
static av_cold void uninit(AVFilterContext *ctx)
Describe the class of an AVClass context structure.
int(* cmp_func)(const void *, const void *)
static int query_formats(AVFilterContext *ctx)
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several inputs
static void write_palette(AVFilterContext *ctx, AVFrame *out)
Write the palette into the output frame.
struct range_box boxes[256]
static unsigned color_hash(uint32_t color)
Hashing function for the color.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
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
#define FFDIFFSIGN(x, y)
Comparator.
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.
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) #define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac) { } void ff_audio_convert_free(AudioConvert **ac) { if(! *ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);} AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map) { AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method !=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2) { ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc) { av_free(ac);return NULL;} return ac;} in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar) { ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar ? ac->channels :1;} else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;} int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in) { int use_generic=1;int len=in->nb_samples;int p;if(ac->dc) { av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> in
#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)
AVFilter ff_vf_palettegen
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]
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)
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, size in bytes of each picture line.
uint8_t transparency_color[4]
static double set_colorquant_ratio_meta(AVFrame *out, int nb_out, int nb_in)