71#define CACHE_SIZE (1<<15)
112#define OFFSET(x) offsetof(PaletteUseContext, x)
113#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
128 {
"alpha_threshold",
"set the alpha threshold for transparency",
OFFSET(trans_thresh),
AV_OPT_TYPE_INT, {.i64=128}, 0, 255,
FLAGS },
160 return (px & 0xff000000)
168 const uint8_t alpha_a =
a->srgb >> 24;
169 const uint8_t alpha_b =
b->srgb >> 24;
171 if (alpha_a < trans_thresh && alpha_b < trans_thresh) {
173 }
else if (alpha_a >= trans_thresh && alpha_b >= trans_thresh) {
174 const int64_t dL =
a->lab[0] -
b->lab[0];
175 const int64_t da =
a->lab[1] -
b->lab[1];
176 const int64_t db =
a->lab[2] -
b->lab[2];
177 const int64_t ret = dL*dL + da*da + db*db;
178 return FFMIN(ret, INT32_MAX - 1);
180 return INT32_MAX - 1;
199 const int trans_thresh,
203 int nearer_kd_id, further_kd_id;
207 if (current_to_target < nearest->dist_sqd) {
209 nearest->
dist_sqd = current_to_target;
218 if (nearer_kd_id != -1)
221 if (further_kd_id != -1 && dx*dx < nearest->dist_sqd)
228 struct nearest_color res = {.dist_sqd = INT_MAX, .node_pos = -1};
250 if (
color>>24 <
s->trans_thresh &&
s->transparency_index >= 0) {
251 return s->transparency_index;
272 uint32_t
c,
int *er,
int *eg,
int *eb)
278 dstc =
s->palette[dstx];
279 if (dstx ==
s->transparency_index) {
282 const uint8_t
r =
c >> 16 & 0xff;
283 const uint8_t
g =
c >> 8 & 0xff;
284 const uint8_t
b =
c & 0xff;
285 *er = (int)
r - (
int)(dstc >> 16 & 0xff);
286 *eg = (int)
g - (
int)(dstc >> 8 & 0xff);
287 *eb = (int)
b - (
int)(dstc & 0xff);
293 int x_start,
int y_start,
int w,
int h,
296 const int src_linesize = in ->
linesize[0] >> 2;
297 const int dst_linesize =
out->linesize[0];
298 uint32_t *
src = ((uint32_t *)in ->
data[0]) + y_start*src_linesize;
299 uint8_t *
dst =
out->data[0] + y_start*dst_linesize;
304 for (
int y = y_start; y <
h; y++) {
305 for (
int x = x_start; x <
w; x++) {
309 const int d =
s->ordered_dither[(y & 7)<<3 | (x & 7)];
310 const uint8_t a8 =
src[x] >> 24;
311 const uint8_t r8 =
src[x] >> 16 & 0xff;
312 const uint8_t g8 =
src[x] >> 8 & 0xff;
313 const uint8_t b8 =
src[x] & 0xff;
317 const uint32_t color_new = (unsigned)(a8) << 24 |
r << 16 |
g << 8 |
b;
325 const int right = x <
w - 1, down = y <
h - 1;
333 if ( down)
src[src_linesize + x ] =
dither_color(
src[src_linesize + x ], er, eg, eb, 3, 3);
334 if (right && down)
src[src_linesize + x + 1] =
dither_color(
src[src_linesize + x + 1], er, eg, eb, 2, 3);
337 const int right = x <
w - 1, down = y <
h - 1,
left = x > x_start;
346 if ( down)
src[src_linesize + x ] =
dither_color(
src[src_linesize + x ], er, eg, eb, 5, 4);
347 if (right && down)
src[src_linesize + x + 1] =
dither_color(
src[src_linesize + x + 1], er, eg, eb, 1, 4);
350 const int right = x <
w - 1, down = y <
h - 1,
left = x > x_start;
351 const int right2 = x <
w - 2, left2 = x > x_start + 1;
362 if (left2)
src[ src_linesize + x - 2] =
dither_color(
src[ src_linesize + x - 2], er, eg, eb, 1, 4);
364 if (1)
src[ src_linesize + x ] =
dither_color(
src[ src_linesize + x ], er, eg, eb, 3, 4);
365 if (right)
src[ src_linesize + x + 1] =
dither_color(
src[ src_linesize + x + 1], er, eg, eb, 2, 4);
366 if (right2)
src[ src_linesize + x + 2] =
dither_color(
src[ src_linesize + x + 2], er, eg, eb, 1, 4);
370 const int right = x <
w - 1, down = y <
h - 1,
left = x > x_start;
379 if ( down)
src[src_linesize + x ] =
dither_color(
src[src_linesize + x ], er, eg, eb, 1, 2);
382 const int right = x <
w - 1, down = y <
h - 1,
left = x > x_start;
383 const int right2 = x <
w - 2, down2 = y <
h - 2, left2 = x > x_start + 1;
394 if (left2)
src[src_linesize + x - 2] =
dither_color(
src[src_linesize + x - 2], er, eg, eb, 2, 5);
396 if (1)
src[src_linesize + x ] =
dither_color(
src[src_linesize + x ], er, eg, eb, 5, 5);
397 if (right)
src[src_linesize + x + 1] =
dither_color(
src[src_linesize + x + 1], er, eg, eb, 4, 5);
398 if (right2)
src[src_linesize + x + 2] =
dither_color(
src[src_linesize + x + 2], er, eg, eb, 2, 5);
402 if (1)
src[src_linesize*2 + x ] =
dither_color(
src[src_linesize*2 + x ], er, eg, eb, 3, 5);
403 if (right)
src[src_linesize*2 + x + 1] =
dither_color(
src[src_linesize*2 + x + 1], er, eg, eb, 2, 5);
408 const int right = x <
w - 1, down = y <
h - 1,
left = x > x_start;
409 const int right2 = x <
w - 2, left2 = x > x_start + 1;
420 if (left2)
src[src_linesize + x - 2] =
dither_color(
src[src_linesize + x - 2], er, eg, eb, 2, 5);
422 if (1)
src[src_linesize + x ] =
dither_color(
src[src_linesize + x ], er, eg, eb, 8, 5);
423 if (right)
src[src_linesize + x + 1] =
dither_color(
src[src_linesize + x + 1], er, eg, eb, 4, 5);
424 if (right2)
src[src_linesize + x + 2] =
dither_color(
src[src_linesize + x + 2], er, eg, eb, 2, 5);
428 const int right = x <
w - 1, down = y <
h - 1,
left = x > x_start;
429 const int right2 = x <
w - 2, down2 = y <
h - 2;
441 if (1)
src[src_linesize + x ] =
dither_color(
src[src_linesize + x ], er, eg, eb, 1, 3);
442 if (right)
src[src_linesize + x + 1] =
dither_color(
src[src_linesize + x + 1], er, eg, eb, 1, 3);
443 if (down2)
src[src_linesize*2 + x ] =
dither_color(
src[src_linesize*2 + x ], er, eg, eb, 1, 3);
463 int parent_id,
int node_id,
467 const uint32_t fontcolor = node->
c.
lab[0] > 0x7fff ? 0 : 0xffffff;
468 const int lab_comp = node->
split;
470 "label=\"%c%d%c%d%c%d%c\" "
471 "fillcolor=\"#%06"PRIX32
"\" "
472 "fontcolor=\"#%06"PRIX32
"\"]\n",
474 "[ "[lab_comp], node->
c.
lab[0],
475 "][ "[lab_comp], node->
c.
lab[1],
476 " ]["[lab_comp], node->
c.
lab[2],
478 node->
c.
srgb & 0xffffff,
503 av_bprintf(&buf,
" node [style=filled fontsize=10 shape=box]\n");
507 fwrite(buf.str, 1, buf.len,
f);
523typedef int (*
cmp_func)(
const void *,
const void *);
525#define DECLARE_CMP_FUNC(name) \
526static int cmp_##name(const void *pa, const void *pb) \
528 const struct color *a = pa; \
529 const struct color *b = pb; \
530 return FFDIFFSIGN(a->value.name, b->value.name); \
544 unsigned nb_color = 0;
546 struct color tmp_pal[256];
549 ranges.
min[0] = ranges.
min[1] = ranges.
min[2] = 0xffff;
550 ranges.
max[0] = ranges.
max[1] = ranges.
max[2] = -0xffff;
553 const uint32_t
c = palette[
i];
554 const uint8_t
a =
c >> 24;
557 if (color_used[
i] || (
a != 0xff) ||
558 lab.
L <
box->min[0] || lab.
a <
box->min[1] || lab.
b <
box->min[2] ||
559 lab.
L >
box->max[0] || lab.
a >
box->max[1] || lab.
b >
box->max[2])
562 if (lab.
L < ranges.
min[0]) ranges.
min[0] = lab.
L;
563 if (lab.
a < ranges.
min[1]) ranges.
min[1] = lab.
a;
564 if (lab.
b < ranges.
min[2]) ranges.
min[2] = lab.
b;
566 if (lab.
L > ranges.
max[0]) ranges.
max[0] = lab.
L;
567 if (lab.
a > ranges.
max[1]) ranges.
max[1] = lab.
a;
568 if (lab.
b > ranges.
max[2]) ranges.
max[2] = lab.
b;
570 tmp_pal[nb_color].
value = lab;
580 wL = ranges.
max[0] - ranges.
min[0];
581 wa = ranges.
max[1] - ranges.
min[1];
582 wb = ranges.
max[2] - ranges.
min[2];
583 if (wb >= wL && wb >= wa) longest = 2;
584 if (wa >= wL && wa >= wb) longest = 1;
585 if (wL >= wa && wL >= wb) longest = 0;
587 *component = longest;
592 return tmp_pal[nb_color >> 1].
pal_id;
598 const uint32_t *palette,
599 const int trans_thresh,
602 int component, cur_id;
604 int node_left_id = -1, node_right_id = -1;
613 cur_id = (*nb_used)++;
615 node->
split = component;
619 color_used[pal_id] = 1;
623 comp_value = node->
c.
lab[component];
624 box1.
max[component] = comp_value;
625 box2.
min[component] =
FFMIN(comp_value + 1, 0xffff);
627 node_left_id =
colormap_insert(
map, color_used, nb_used, palette, trans_thresh, &box1);
629 if (box2.
min[component] <= box2.
max[component])
630 node_right_id =
colormap_insert(
map, color_used, nb_used, palette, trans_thresh, &box2);
640 const int c1 = *(
const uint32_t *)
a & 0xffffff;
641 const int c2 = *(
const uint32_t *)
b & 0xffffff;
649 uint32_t last_color = 0;
652 if (
s->transparency_index >= 0) {
653 FFSWAP(uint32_t,
s->palette[
s->transparency_index],
s->palette[255]);
660 const uint32_t
c =
s->palette[
i];
661 if (
i != 0 &&
c == last_color) {
666 if (
c >> 24 <
s->trans_thresh) {
672 box.min[0] =
box.min[1] =
box.min[2] = -0xffff;
673 box.max[0] =
box.max[1] =
box.max[2] = 0xffff;
684 int *xp,
int *yp,
int *wp,
int *hp)
686 int x_start = 0, y_start = 0;
692 int x_end = cur_src->
width - 1,
693 y_end = cur_src->
height - 1;
694 const uint32_t *prv_srcp = (
const uint32_t *)prv_src->
data[0];
695 const uint32_t *cur_srcp = (
const uint32_t *)cur_src->
data[0];
696 const uint8_t *prv_dstp = prv_dst->
data[0];
697 uint8_t *cur_dstp = cur_dst->
data[0];
699 const int prv_src_linesize = prv_src->
linesize[0] >> 2;
700 const int cur_src_linesize = cur_src->
linesize[0] >> 2;
701 const int prv_dst_linesize = prv_dst->
linesize[0];
702 const int cur_dst_linesize = cur_dst->
linesize[0];
705 while (y_start < y_end && !memcmp(prv_srcp + y_start*prv_src_linesize,
706 cur_srcp + y_start*cur_src_linesize,
707 cur_src->
width * 4)) {
708 memcpy(cur_dstp + y_start*cur_dst_linesize,
709 prv_dstp + y_start*prv_dst_linesize,
713 while (y_end > y_start && !memcmp(prv_srcp + y_end*prv_src_linesize,
714 cur_srcp + y_end*cur_src_linesize,
715 cur_src->
width * 4)) {
716 memcpy(cur_dstp + y_end*cur_dst_linesize,
717 prv_dstp + y_end*prv_dst_linesize,
722 height = y_end + 1 - y_start;
725 while (x_start < x_end) {
727 for (y = y_start; y <= y_end; y++) {
728 if (prv_srcp[y*prv_src_linesize + x_start] != cur_srcp[y*cur_src_linesize + x_start]) {
737 while (x_end > x_start) {
739 for (y = y_start; y <= y_end; y++) {
740 if (prv_srcp[y*prv_src_linesize + x_end] != cur_srcp[y*cur_src_linesize + x_end]) {
749 width = x_end + 1 - x_start;
752 for (y = y_start; y <= y_end; y++)
753 memcpy(cur_dstp + y*cur_dst_linesize,
754 prv_dstp + y*prv_dst_linesize, x_start);
756 if (x_end != cur_src->
width - 1) {
757 const int copy_len = cur_src->
width - 1 - x_end;
758 for (y = y_start; y <= y_end; y++)
759 memcpy(cur_dstp + y*cur_dst_linesize + x_end + 1,
760 prv_dstp + y*prv_dst_linesize + x_end + 1,
785 s->last_out,
out, &x, &y, &
w, &
h);
795 ff_dlog(
ctx,
"%dx%d rect: (%d;%d) -> (%d,%d) [area:%dx%d]\n",
798 ret =
s->set_frame(
s,
out, in, x, y,
w,
h);
818 s->fs.opt_repeatlast = 1;
822 outlink->
w =
ctx->inputs[0]->w;
823 outlink->
h =
ctx->inputs[0]->h;
837 "Palette input must contain exactly %d pixels. "
838 "Specified input has %dx%d=%d pixels\n",
840 inlink->
w * inlink->
h);
849 const uint32_t *p = (
const uint32_t *)palette_frame->
data[0];
850 const ptrdiff_t p_linesize = palette_frame->
linesize[0] >> 2;
852 s->transparency_index = -1;
855 memset(
s->palette, 0,
sizeof(
s->palette));
856 memset(
s->map, 0,
sizeof(
s->map));
859 memset(
s->cache, 0,
sizeof(
s->cache));
863 for (y = 0; y < palette_frame->
height; y++) {
864 for (x = 0; x < palette_frame->
width; x++) {
865 s->palette[
i] = p[x];
866 if (p[x]>>24 <
s->trans_thresh) {
867 s->transparency_index =
i;
877 s->palette_loaded = 1;
896 if (!
s->palette_loaded) {
906#define DEFINE_SET_FRAME(name, value) \
907static int set_frame_##name(PaletteUseContext *s, AVFrame *out, AVFrame *in, \
908 int x_start, int y_start, int w, int h) \
910 return set_frame(s, out, in, x_start, y_start, w, h, value); \
937 const int q = p ^ (p >> 3);
938 return (p & 4) >> 2 | (q & 4) >> 1 \
939 | (p & 2) << 1 | (q & 2) << 2 \
940 | (p & 1) << 4 | (q & 1) << 5;
949 if (!
s->last_in || !
s->last_out)
955 const int delta = 1 << (5 -
s->bayer_scale);
1001 .p.name =
"paletteuse",
1003 .p.priv_class = &paletteuse_class,
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
const FFFilter ff_vf_paletteuse
static uint8_t hash[HASH_SIZE]
static AVFormatContext * ctx
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
int ff_inlink_make_frame_writable(AVFilterLink *link, AVFrame **rframe)
Make sure a frame is writable.
Main libavfilter public API header.
static int BS_FUNC left(const BSCTX *bc)
Return the number of the bits left in a buffer.
void av_bprintf(AVBPrint *buf, const char *fmt,...)
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
#define AV_BPRINT_SIZE_UNLIMITED
#define i(width, name, range_min, range_max)
#define fs(width, name, subs,...)
int(* init)(AVBSFContext *ctx)
static struct @111144215057303131116103221376075045141373005341 current
static av_unused double box(double x, const double *params)
int ff_framesync_configure(FFFrameSync *fs)
Configure a frame sync structure.
int ff_framesync_activate(FFFrameSync *fs)
Examine the frames in the filter's input and try to produce output.
int ff_framesync_init_dualinput(FFFrameSync *fs, AVFilterContext *parent)
Initialize a frame sync structure for dualinput.
void ff_framesync_uninit(FFFrameSync *fs)
Free all memory currently allocated.
int ff_framesync_dualinput_get_writable(FFFrameSync *fs, AVFrame **f0, AVFrame **f1)
Same as ff_framesync_dualinput_get(), but make sure that f0 is writable.
@ EXT_INFINITY
Extend the frame to infinity.
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
@ AV_OPT_TYPE_INT
Underlying C type is int.
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
@ AV_OPT_TYPE_STRING
Underlying C type is a uint8_t* that is either NULL or points to a C string allocated with the av_mal...
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
int av_frame_replace(AVFrame *dst, const AVFrame *src)
Ensure the destination frame refers to the same data described by the source frame,...
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
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.
const VDPAUPixFmtMap * map
static void scale(int *out, const int *in, const int w, const int h, const int shift)
static av_cold void uninit(AVBitStreamFilterContext *ctx)
static int activate(AVBitStreamFilterContext *ctx)
static int config_output(AVBitStreamFilterLink *outlink)
static int shift(int a, int b)
#define FILTER_INPUTS(array)
#define FILTER_OUTPUTS(array)
#define AVFILTER_DEFINE_CLASS(fname)
#define FILTER_QUERY_FUNC2(func)
FILE * avpriv_fopen_utf8(const char *path, const char *mode)
Open a file using a UTF-8 filename.
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
#define FFSWAP(type, a, b)
Memory handling functions.
uint32_t ff_lowbias32(uint32_t x)
struct Lab ff_srgb_u8_to_oklab_int(uint32_t srgb)
sRGB (non-linear) to OkLab conversion
AVPixelFormat
Pixel format.
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
#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...
#define FF_ARRAY_ELEMS(a)
Describe the class of an AVClass context structure.
AVFilterLink ** outputs
array of pointers to output links
A link between two filters.
int w
agreed upon image width
int h
agreed upon image height
AVFilterContext * src
source filter
AVRational time_base
Define the time base used by the PTS of the frames/samples which will pass through this link.
AVFilterContext * dst
dest filter
A filter pad used for either input or output.
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, a positive or negative value, which is typically indicating the size in bytes of each pict...
struct color_node map[AVPALETTE_COUNT]
uint32_t palette[AVPALETTE_COUNT]
struct cache_node cache[CACHE_SIZE]
struct cached_color * entries
static const uint16_t dither[8][8]
@ DITHERING_FLOYD_STEINBERG
static const cmp_func cmp_funcs[]
int(* cmp_func)(const void *, const void *)
static struct color_info get_color_from_srgb(uint32_t srgb)
static av_always_inline uint32_t dither_color(uint32_t px, int er, int eg, int eb, int scale, int shift)
static int get_next_color(const uint8_t *color_used, const uint32_t *palette, int *component, const struct color_rect *box)
static const AVFilterPad paletteuse_outputs[]
#define DEFINE_SET_FRAME(name, value)
static void set_processing_window(enum diff_mode diff_mode, const AVFrame *prv_src, const AVFrame *cur_src, const AVFrame *prv_dst, AVFrame *cur_dst, int *xp, int *yp, int *wp, int *hp)
static av_always_inline int get_dst_color_err(PaletteUseContext *s, uint32_t c, int *er, int *eg, int *eb)
static int dither_value(int p)
static int disp_tree(const struct color_node *node, const char *fname)
static int cmp_pal_entry(const void *a, const void *b)
static int load_apply_palette(FFFrameSync *fs)
static void load_colormap(PaletteUseContext *s)
static av_always_inline int color_get(PaletteUseContext *s, uint32_t color)
Check if the requested color is in the cache already.
static av_always_inline uint8_t colormap_nearest(const struct color_node *node, const struct color_info *target, const int trans_thresh)
static int config_input_palette(AVFilterLink *inlink)
static const AVOption paletteuse_options[]
static void colormap_nearest_node(const struct color_node *map, const int node_pos, const struct color_info *target, const int trans_thresh, struct nearest_color *nearest)
static const AVFilterPad paletteuse_inputs[]
static int apply_palette(AVFilterLink *inlink, AVFrame *in, AVFrame **outf)
int(* set_frame_func)(struct PaletteUseContext *s, AVFrame *out, AVFrame *in, int x_start, int y_start, int width, int height)
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
static int activate(AVFilterContext *ctx)
static av_cold void uninit(AVFilterContext *ctx)
static void load_palette(PaletteUseContext *s, const AVFrame *palette_frame)
static av_always_inline int set_frame(PaletteUseContext *s, AVFrame *out, AVFrame *in, int x_start, int y_start, int w, int h, enum dithering_mode dither)
#define DECLARE_CMP_FUNC(name)
static int config_output(AVFilterLink *outlink)
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)
static const set_frame_func set_frame_lut[NB_DITHERING]
static void disp_node(AVBPrint *buf, const struct color_node *map, int parent_id, int node_id, int depth)
static int colormap_insert(struct color_node *map, uint8_t *color_used, int *nb_used, const uint32_t *palette, const int trans_thresh, const struct color_rect *box)
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.