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"}, 0, 0,
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;
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 0xffU<<24 |
r<<16 |
g<<8 |
b;
214 struct range_box *new_box = &
s->boxes[
s->nb_boxes++];
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;
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;
287 snprintf(buf,
sizeof(buf),
"%f", ratio);
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;
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;
369 for (
i = box->
start; i < box->start + box->
len - 2;
i++) {
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 const AVFilterPad inputs[]
static const AVFilterPad outputs[]
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
simple assert() macros that are a bit more flexible than ISO C assert().
#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.
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
Main libavfilter public API header.
#define FFDIFFSIGN(x, y)
Comparator.
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
#define AVERROR_EOF
End of file.
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
#define AV_LOG_WARNING
Something somehow does not look correct.
#define AV_LOG_INFO
Standard information.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
static AVRational av_make_q(int num, int den)
Create an AVRational.
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.
common internal API header
#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]
AVPixelFormat
Pixel format.
#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...
Describe the class of an AVClass context structure.
A link between two filters.
int w
agreed upon image width
int h
agreed upon image height
AVFilterContext * src
source filter
AVRational sample_aspect_ratio
agreed upon sample aspect ratio
AVFilterContext * dst
dest filter
A filter pad used for either input or output.
const char * name
Pad name.
const char * name
Filter name.
This structure describes decoded (raw) audio or video data.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
struct range_box boxes[256]
uint8_t transparency_color[4]
struct hist_node histogram[HIST_SIZE]
struct color_ref * entries
#define av_malloc_array(a, b)
static int ref[MAX_W *MAX_W]
static av_always_inline int diff(const uint32_t a, const uint32_t b)
static int cmp_color(const void *a, const void *b)
Simple color comparison for sorting the final palette.
@ STATS_MODE_SINGLE_FRAMES
static const cmp_func cmp_funcs[]
static int query_formats(AVFilterContext *ctx)
static int request_frame(AVFilterLink *outlink)
Returns only one frame at the end containing the full palette.
static int update_histogram_diff(struct hist_node *hist, const AVFrame *f1, const AVFrame *f2)
Update histogram when pixels differ from previous frame.
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Update the histogram for each passing frame.
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)
AVFilter ff_vf_palettegen
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 palettegen_outputs[]
static AVFrame * get_palette_frame(AVFilterContext *ctx)
Main function implementing the Median Cut Algorithm defined by Paul Heckbert in Color Image Quantizat...
static av_cold void uninit(AVFilterContext *ctx)
static unsigned color_hash(uint32_t color)
Hashing function for the color.
static const AVFilterPad palettegen_inputs[]
AVFILTER_DEFINE_CLASS(palettegen)
static int color_inc(struct hist_node *hist, uint32_t color)
Locate the color in the hash table and increment its counter.
static int config_output(AVFilterLink *outlink)
The output is one simple 16x16 squared-pixels palette.
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...
static void split_box(PaletteGenContext *s, struct range_box *box, int n)
Split given box in two at position n.
static int update_histogram_frame(struct hist_node *hist, const AVFrame *f)
Simple histogram of the frame.
int(* cmp_func)(const void *, const void *)
static double set_colorquant_ratio_meta(AVFrame *out, int nb_out, int nb_in)
static const AVOption palettegen_options[]
static void write_palette(AVFilterContext *ctx, AVFrame *out)
Write the palette into the output frame.
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.