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;
157 for (box_id = 0; box_id < s->
nb_boxes; box_id++) {
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++) {
205 return 0xff
U<<24 | r<<16 |
g<<8 |
b;
215 new_box->
start = n + 1;
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;
241 for (x = 0; x < out->
width; x++) {
242 if (box_id < s->nb_boxes) {
244 if ((x || y) && pal[x] == last_color)
286 const double ratio = (double)nb_out / nb_in;
287 snprintf(buf,
sizeof(buf),
"%f", ratio);
319 box = &s->
boxes[box_id];
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;
338 min[0] =
FFMIN(r, min[0]), max[0] =
FFMAX(r, max[0]);
339 min[1] =
FFMIN(g, min[1]), max[1] =
FFMAX(g, max[1]);
340 min[2] =
FFMIN(b, min[2]), max[2] =
FFMAX(b, max[2]);
341 box_weight += ref->
count;
345 rr = max[0] - min[0];
346 gr = max[1] - min[1];
347 br = max[2] - min[2];
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;
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;
417 if (e->
color == 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;
538 outlink->
w = outlink->
h = 16;
574 .
name =
"palettegen",
579 .
inputs = palettegen_inputs,
581 .priv_class = &palettegen_class,
static int update_histogram_frame(struct hist_node *hist, const AVFrame *f)
Simple histogram of the frame.
#define DECLARE_CMP_FUNC(name, pos)
This structure describes decoded (raw) audio or video data.
static AVFrame * get_palette_frame(AVFilterContext *ctx)
Main function implementing the Median Cut Algorithm defined by Paul Heckbert in Color Image Quantizat...
static const cmp_func cmp_funcs[]
#define AV_LOG_WARNING
Something somehow does not look correct.
Main libavfilter public API header.
int h
agreed upon image height
struct color_ref * entries
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...
struct range_box boxes[256]
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
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
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...
AVFILTER_DEFINE_CLASS(palettegen)
const char * name
Pad name.
AVFilterLink ** inputs
array of pointers to input links
#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.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
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
struct hist_node histogram[HIST_SIZE]
#define AVERROR_EOF
End of file.
AVDictionary * metadata
metadata.
A filter pad used for either input or output.
static unsigned color_hash(uint32_t color)
Hashing function for the color.
A link between two filters.
#define i(width, name, range_min, range_max)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
static int query_formats(AVFilterContext *ctx)
static const AVOption palettegen_options[]
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
void * priv
private data for use by the filter
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Update the histogram for each passing frame.
simple assert() macros that are a bit more flexible than ISO C assert().
AVFilterFormats * in_formats
Lists of formats and channel layouts supported by the input and output filters respectively.
static const AVFilterPad palettegen_outputs[]
int(* cmp_func)(const void *, const void *)
int w
agreed upon image width
#define FFDIFFSIGN(x, y)
Comparator.
common internal API header
AVFilter ff_vf_palettegen
AVFilterContext * src
source filter
static const AVFilterPad palettegen_inputs[]
static int get_next_box_id_to_split(PaletteGenContext *s)
Find the next box to split: pick the one with the highest variance.
static const AVFilterPad outputs[]
static void write_palette(AVFilterContext *ctx, AVFrame *out)
Write the palette into the output frame.
#define AV_LOG_INFO
Standard information.
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.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
static AVRational av_make_q(int num, int den)
Create an AVRational.
static av_cold void uninit(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
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
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
Describe the class of an AVClass context structure.
const char * name
Filter name.
AVRational sample_aspect_ratio
agreed upon sample aspect ratio
AVFilterLink ** outputs
array of pointers to output links
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
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
static int config_output(AVFilterLink *outlink)
The output is one simple 16x16 squared-pixels palette.
static int request_frame(AVFilterLink *outlink)
Returns only one frame at the end containing the full palette.
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)
AVFilterContext * dst
dest filter
static int color_inc(struct hist_node *hist, uint32_t color)
Locate the color in the hash table and increment its counter.
static int update_histogram_diff(struct hist_node *hist, const AVFrame *f1, const AVFrame *f2)
Update histogram when pixels differ from previous frame.
#define av_malloc_array(a, b)
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.
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
AVPixelFormat
Pixel format.
static double set_colorquant_ratio_meta(AVFrame *out, int nb_out, int nb_in)
#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...
AVFilterFormats * out_formats
uint8_t transparency_color[4]