66 #define CACHE_SIZE (1<<(3*NBITS)) 96 int ordered_dither[8*8];
109 #define OFFSET(x) offsetof(PaletteUseContext, x) 110 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM 122 {
"alpha_threshold",
"set the alpha threshold for transparency",
OFFSET(trans_thresh),
AV_OPT_TYPE_INT, {.i64=128}, 0, 255 },
130 {
"mean_err",
"compute and print mean error",
OFFSET(calc_mean_err),
AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
148 if (!in || !inpal || !out) {
163 return av_clip_uint8( px >> 24 ) << 24
164 | av_clip_uint8((px >> 16 & 0xff) + ((er * scale) / (1<<shift))) << 16
165 | av_clip_uint8((px >> 8 & 0xff) + ((eg * scale) / (1<<shift))) << 8
166 | av_clip_uint8((px & 0xff) + ((eb * scale) / (1<<shift)));
172 const int dr = c1[1] - c2[1];
173 const int dg = c1[2] - c2[2];
174 const int db = c1[3] - c2[3];
176 if (c1[0] < trans_thresh && c2[0] < trans_thresh) {
178 }
else if (c1[0] >= trans_thresh && c2[0] >= trans_thresh) {
179 return dr*dr + dg*dg + db*db;
181 return 255*255 + 255*255 + 255*255;
187 int i, pal_id = -1, min_dist = INT_MAX;
190 const uint32_t
c = palette[i];
192 if (c >> 24 >= trans_thresh) {
194 palette[i]>>24 & 0xff,
195 palette[i]>>16 & 0xff,
196 palette[i]>> 8 & 0xff,
199 const int d =
diff(palargb, argb, trans_thresh);
218 const int trans_thresh,
222 const int s = kd->
split;
223 int dx, nearer_kd_id, further_kd_id;
225 const int current_to_target =
diff(target, current, trans_thresh);
227 if (current_to_target < nearest->dist_sqd) {
229 nearest->
dist_sqd = current_to_target;
233 dx = target[
s] - current[
s];
238 if (nearer_kd_id != -1)
241 if (further_kd_id != -1 && dx*dx < nearest->dist_sqd)
260 int pos = 0, best_node_id = -1, best_dist = INT_MAX, cur_color_id = 0;
266 const struct color_node *kd = &root[cur_color_id];
268 const int current_to_target =
diff(target, current, trans_thresh);
272 if (current_to_target < best_dist) {
273 best_node_id = cur_color_id;
274 if (!current_to_target)
276 best_dist = current_to_target;
282 const int dx = target[
split] - current[
split];
283 int nearer_kd_id, further_kd_id;
289 if (nearer_kd_id != -1) {
290 if (further_kd_id != -1) {
301 cur_color_id = nearer_kd_id;
303 }
else if (dx*dx < best_dist) {
308 cur_color_id = further_kd_id;
319 }
while (node->
dx2 >= best_dist);
330 #define COLORMAP_NEAREST(search, palette, root, target, trans_thresh) \ 331 search == COLOR_SEARCH_NNS_ITERATIVE ? colormap_nearest_iterative(root, target, trans_thresh) : \ 332 search == COLOR_SEARCH_NNS_RECURSIVE ? colormap_nearest_recursive(root, target, trans_thresh) : \ 333 colormap_nearest_bruteforce(palette, target, trans_thresh) 361 if (e->
color == color)
376 uint32_t
c,
int *er,
int *eg,
int *eb,
384 const int dstx =
color_get(s, c, a, r, g, b, search_method);
388 *er = r - (dstc >> 16 & 0xff);
389 *eg = g - (dstc >> 8 & 0xff);
390 *eb = b - (dstc & 0xff);
395 int x_start,
int y_start,
int w,
int h,
400 const int src_linesize = in ->
linesize[0] >> 2;
401 const int dst_linesize = out->
linesize[0];
402 uint32_t *
src = ((uint32_t *)in ->
data[0]) + y_start*src_linesize;
403 uint8_t *dst = out->
data[0] + y_start*dst_linesize;
408 for (y = y_start; y <
h; y++) {
409 for (x = x_start; x <
w; x++) {
414 const uint8_t a8 = src[x] >> 24 & 0xff;
415 const uint8_t r8 = src[x] >> 16 & 0xff;
416 const uint8_t g8 = src[x] >> 8 & 0xff;
417 const uint8_t b8 = src[x] & 0xff;
418 const uint8_t r = av_clip_uint8(r8 + d);
419 const uint8_t g = av_clip_uint8(g8 + d);
420 const uint8_t b = av_clip_uint8(b8 + d);
421 const int color =
color_get(s, src[x], a8, r, g, b, search_method);
428 const int right = x < w - 1, down = y < h - 1;
435 if (right) src[ x + 1] =
dither_color(src[ x + 1], er, eg, eb, 3, 3);
436 if ( down) src[src_linesize + x ] =
dither_color(src[src_linesize + x ], er, eg, eb, 3, 3);
437 if (right && down) src[src_linesize + x + 1] =
dither_color(src[src_linesize + x + 1], er, eg, eb, 2, 3);
440 const int right = x < w - 1, down = y < h - 1, left = x > x_start;
447 if (right) src[ x + 1] =
dither_color(src[ x + 1], er, eg, eb, 7, 4);
448 if (left && down) src[src_linesize + x - 1] =
dither_color(src[src_linesize + x - 1], er, eg, eb, 3, 4);
449 if ( down) src[src_linesize + x ] =
dither_color(src[src_linesize + x ], er, eg, eb, 5, 4);
450 if (right && down) src[src_linesize + x + 1] =
dither_color(src[src_linesize + x + 1], er, eg, eb, 1, 4);
453 const int right = x < w - 1, down = y < h - 1, left = x > x_start;
454 const int right2 = x < w - 2, left2 = x > x_start + 1;
461 if (right) src[ x + 1] =
dither_color(src[ x + 1], er, eg, eb, 4, 4);
462 if (right2) src[ x + 2] =
dither_color(src[ x + 2], er, eg, eb, 3, 4);
465 if (left2) src[ src_linesize + x - 2] =
dither_color(src[ src_linesize + x - 2], er, eg, eb, 1, 4);
466 if (left) src[ src_linesize + x - 1] =
dither_color(src[ src_linesize + x - 1], er, eg, eb, 2, 4);
467 if (1) src[ src_linesize + x ] =
dither_color(src[ src_linesize + x ], er, eg, eb, 3, 4);
468 if (right) src[ src_linesize + x + 1] =
dither_color(src[ src_linesize + x + 1], er, eg, eb, 2, 4);
469 if (right2) src[ src_linesize + x + 2] =
dither_color(src[ src_linesize + x + 2], er, eg, eb, 1, 4);
473 const int right = x < w - 1, down = y < h - 1, left = x > x_start;
480 if (right) src[ x + 1] =
dither_color(src[ x + 1], er, eg, eb, 2, 2);
481 if (left && down) src[src_linesize + x - 1] =
dither_color(src[src_linesize + x - 1], er, eg, eb, 1, 2);
482 if ( down) src[src_linesize + x ] =
dither_color(src[src_linesize + x ], er, eg, eb, 1, 2);
485 const uint8_t a = src[x] >> 24 & 0xff;
486 const uint8_t r = src[x] >> 16 & 0xff;
487 const uint8_t g = src[x] >> 8 & 0xff;
489 const int color =
color_get(s, src[x], a, r, g, b, search_method);
505 int parent_id,
int node_id,
508 const struct color_node *node = &map[node_id];
509 const uint32_t fontcolor = node->
val[1] > 0x50 &&
510 node->
val[2] > 0x50 &&
511 node->
val[3] > 0x50 ? 0 : 0xffffff;
512 const int rgb_comp = node->
split - 1;
514 "label=\"%c%02X%c%02X%c%02X%c\" " 515 "fillcolor=\"#%02x%02x%02x\" " 516 "fontcolor=\"#%06"PRIX32
"\"]\n",
518 "[ "[rgb_comp], node->
val[1],
519 "][ "[rgb_comp], node->
val[2],
520 " ]["[rgb_comp], node->
val[3],
522 node->
val[1], node->
val[2], node->
val[3],
525 av_bprintf(buf,
"%*cnode%d -> node%d\n", depth*INDENT,
' ',
547 av_bprintf(&buf,
" node [style=filled fontsize=10 shape=box]\n");
551 fwrite(buf.str, 1, buf.len, f);
560 int r,
g,
b, ret = 0;
562 for (r = 0; r < 256; r++) {
563 for (g = 0; g < 256; g++) {
564 for (b = 0; b < 256; b++) {
566 const int r1 =
COLORMAP_NEAREST(search_method, palette, node, argb, trans_thresh);
569 const uint32_t
c1 = palette[r1];
570 const uint32_t
c2 = palette[r2];
571 const uint8_t palargb1[] = { 0xff, c1>>16 & 0xff, c1>> 8 & 0xff, c1 & 0xff };
572 const uint8_t palargb2[] = { 0xff, c2>>16 & 0xff, c2>> 8 & 0xff, c2 & 0xff };
573 const int d1 =
diff(palargb1, argb, trans_thresh);
574 const int d2 =
diff(palargb2, argb, trans_thresh);
577 "/!\\ %02X%02X%02X: %d ! %d (%06"PRIX32
" ! %06"PRIX32
") / dist: %d ! %d\n",
578 r, g, b, r1, r2, c1 & 0xffffff, c2 & 0xffffff, d1, d2);
600 #define DECLARE_CMP_FUNC(name, pos) \ 601 static int cmp_##name(const void *pa, const void *pb) \ 603 const struct color *a = pa; \ 604 const struct color *b = pb; \ 605 return (a->value >> (8 * (3 - (pos))) & 0xff) \ 606 - (b->value >> (8 * (3 - (pos))) & 0xff); \ 617 const int trans_thresh,
622 unsigned nb_color = 0;
624 struct color tmp_pal[256];
627 ranges.
min[0] = ranges.
min[1] = ranges.
min[2] = 0xff;
628 ranges.
max[0] = ranges.
max[1] = ranges.
max[2] = 0x00;
631 const uint32_t
c = palette[i];
637 if (a < trans_thresh) {
641 if (color_used[i] || (a != 0xff) ||
642 r < box->
min[0] || g < box->
min[1] || b < box->
min[2] ||
643 r > box->
max[0] || g > box->
max[1] || b > box->
max[2])
646 if (r < ranges.
min[0]) ranges.
min[0] =
r;
647 if (g < ranges.
min[1]) ranges.
min[1] =
g;
648 if (b < ranges.
min[2]) ranges.
min[2] =
b;
650 if (r > ranges.
max[0]) ranges.
max[0] =
r;
651 if (g > ranges.
max[1]) ranges.
max[1] =
g;
652 if (b > ranges.
max[2]) ranges.
max[2] =
b;
654 tmp_pal[nb_color].
value =
c;
655 tmp_pal[nb_color].
pal_id = i;
664 wr = ranges.
max[0] - ranges.
min[0];
665 wg = ranges.
max[1] - ranges.
min[1];
666 wb = ranges.
max[2] - ranges.
min[2];
667 if (wr >= wg && wr >= wb) longest = 1;
668 if (wg >= wr && wg >= wb) longest = 2;
669 if (wb >= wr && wb >= wg) longest = 3;
671 *component = longest;
676 return tmp_pal[nb_color >> 1].
pal_id;
682 const uint32_t *palette,
683 const int trans_thresh,
687 int component, cur_id;
688 int node_left_id = -1, node_right_id = -1;
691 const int pal_id =
get_next_color(color_used, palette, trans_thresh, &component, box);
697 cur_id = (*nb_used)++;
700 node->
split = component;
702 node->
val[0] = c>>24 & 0xff;
703 node->
val[1] = c>>16 & 0xff;
704 node->
val[2] = c>> 8 & 0xff;
705 node->
val[3] = c & 0xff;
707 color_used[pal_id] = 1;
711 box1.
max[component-1] = node->
val[component];
712 box2.
min[component-1] = node->
val[component] + 1;
714 node_left_id =
colormap_insert(map, color_used, nb_used, palette, trans_thresh, &box1);
716 if (box2.
min[component-1] <= box2.
max[component-1])
717 node_right_id =
colormap_insert(map, color_used, nb_used, palette, trans_thresh, &box2);
727 const int c1 = *(
const uint32_t *)a & 0xffffff;
728 const int c2 = *(
const uint32_t *)b & 0xffffff;
736 uint32_t last_color = 0;
744 if ((s->
palette[i]>>24 & 0xff) == 0) {
753 if (i != 0 && c == last_color) {
764 box.
min[0] = box.
min[1] = box.
min[2] = 0x00;
765 box.
max[0] = box.
max[1] = box.
max[2] = 0xff;
779 const AVFrame *in2,
int frame_count)
782 const uint32_t *palette = s->
palette;
783 uint32_t *
src1 = (uint32_t *)in1->
data[0];
785 const int src1_linesize = in1->
linesize[0] >> 2;
786 const int src2_linesize = in2->
linesize[0];
788 unsigned mean_err = 0;
791 for (x = 0; x < in1->
width; x++) {
792 const uint32_t
c1 = src1[x];
793 const uint32_t
c2 = palette[src2[x]];
794 const uint8_t argb1[] = {0xff, c1 >> 16 & 0xff, c1 >> 8 & 0xff, c1 & 0xff};
795 const uint8_t argb2[] = {0xff, c2 >> 16 & 0xff, c2 >> 8 & 0xff, c2 & 0xff};
798 src1 += src1_linesize;
799 src2 += src2_linesize;
811 int *xp,
int *yp,
int *wp,
int *hp)
813 int x_start = 0, y_start = 0;
819 int x_end = cur_src->
width - 1,
820 y_end = cur_src->
height - 1;
821 const uint32_t *prv_srcp = (
const uint32_t *)prv_src->
data[0];
822 const uint32_t *cur_srcp = (
const uint32_t *)cur_src->
data[0];
826 const int prv_src_linesize = prv_src->
linesize[0] >> 2;
827 const int cur_src_linesize = cur_src->
linesize[0] >> 2;
828 const int prv_dst_linesize = prv_dst->
linesize[0];
829 const int cur_dst_linesize = cur_dst->
linesize[0];
832 while (y_start < y_end && !memcmp(prv_srcp + y_start*prv_src_linesize,
833 cur_srcp + y_start*cur_src_linesize,
834 cur_src->
width * 4)) {
835 memcpy(cur_dstp + y_start*cur_dst_linesize,
836 prv_dstp + y_start*prv_dst_linesize,
840 while (y_end > y_start && !memcmp(prv_srcp + y_end*prv_src_linesize,
841 cur_srcp + y_end*cur_src_linesize,
842 cur_src->
width * 4)) {
843 memcpy(cur_dstp + y_end*cur_dst_linesize,
844 prv_dstp + y_end*prv_dst_linesize,
849 height = y_end + 1 - y_start;
852 while (x_start < x_end) {
854 for (y = y_start; y <= y_end; y++) {
855 if (prv_srcp[y*prv_src_linesize + x_start] != cur_srcp[y*cur_src_linesize + x_start]) {
864 while (x_end > x_start) {
866 for (y = y_start; y <= y_end; y++) {
867 if (prv_srcp[y*prv_src_linesize + x_end] != cur_srcp[y*cur_src_linesize + x_end]) {
876 width = x_end + 1 - x_start;
879 for (y = y_start; y <= y_end; y++)
880 memcpy(cur_dstp + y*cur_dst_linesize,
881 prv_dstp + y*prv_dst_linesize, x_start);
883 if (x_end != cur_src->
width - 1) {
884 const int copy_len = cur_src->
width - 1 - x_end;
885 for (y = y_start; y <= y_end; y++)
886 memcpy(cur_dstp + y*cur_dst_linesize + x_end + 1,
887 prv_dstp + y*prv_dst_linesize + x_end + 1,
926 ff_dlog(ctx,
"%dx%d rect: (%d;%d) -> (%d,%d) [area:%dx%d]\n",
929 ret = s->
set_frame(s, out, in, x, y, w, h);
971 "Palette input must contain exactly %d pixels. " 972 "Specified input has %dx%d=%d pixels\n",
974 inlink->
w * inlink->
h);
983 const uint32_t *p = (
const uint32_t *)palette_frame->
data[0];
984 const int p_linesize = palette_frame->
linesize[0] >> 2;
990 memset(s->
map, 0,
sizeof(s->
map));
997 for (y = 0; y < palette_frame->
height; y++) {
998 for (x = 0; x < palette_frame->
width; x++) {
1026 if (!master || !second) {
1043 #define DEFINE_SET_FRAME(color_search, name, value) \ 1044 static int set_frame_##name(PaletteUseContext *s, AVFrame *out, AVFrame *in, \ 1045 int x_start, int y_start, int w, int h) \ 1047 return set_frame(s, out, in, x_start, y_start, w, h, value, color_search); \ 1050 #define DEFINE_SET_FRAME_COLOR_SEARCH(color_search, color_search_macro) \ 1051 DEFINE_SET_FRAME(color_search_macro, color_search##_##none, DITHERING_NONE) \ 1052 DEFINE_SET_FRAME(color_search_macro, color_search##_##bayer, DITHERING_BAYER) \ 1053 DEFINE_SET_FRAME(color_search_macro, color_search##_##heckbert, DITHERING_HECKBERT) \ 1054 DEFINE_SET_FRAME(color_search_macro, color_search##_##floyd_steinberg, DITHERING_FLOYD_STEINBERG) \ 1055 DEFINE_SET_FRAME(color_search_macro, color_search##_##sierra2, DITHERING_SIERRA2) \ 1056 DEFINE_SET_FRAME(color_search_macro, color_search##_##sierra2_4a, DITHERING_SIERRA2_4A) \ 1062 #define DITHERING_ENTRIES(color_search) { \ 1063 set_frame_##color_search##_none, \ 1064 set_frame_##color_search##_bayer, \ 1065 set_frame_##color_search##_heckbert, \ 1066 set_frame_##color_search##_floyd_steinberg, \ 1067 set_frame_##color_search##_sierra2, \ 1068 set_frame_##color_search##_sierra2_4a, \ 1079 const int q = p ^ (p >> 3);
1080 return (p & 4) >> 2 | (q & 4) >> 1 \
1081 | (p & 2) << 1 | (q & 2) << 2 \
1082 | (p & 1) << 4 | (q & 1) << 5;
1142 .
name =
"paletteuse",
1149 .
inputs = paletteuse_inputs,
1150 .
outputs = paletteuse_outputs,
1151 .priv_class = &paletteuse_class,
static av_always_inline int get_dst_color_err(PaletteUseContext *s, uint32_t c, int *er, int *eg, int *eb, const enum color_search_method search_method)
static void colormap_nearest_node(const struct color_node *map, const int node_pos, const uint8_t *target, const int trans_thresh, struct nearest_color *nearest)
AVFILTER_DEFINE_CLASS(paletteuse)
void av_bprintf(AVBPrint *buf, const char *fmt,...)
static int shift(int a, int b)
static void load_palette(PaletteUseContext *s, const AVFrame *palette_frame)
This structure describes decoded (raw) audio or video data.
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)
Main libavfilter public API header.
int h
agreed upon image height
static int query_formats(AVFilterContext *ctx)
FILE * av_fopen_utf8(const char *path, const char *mode)
Open a file using a UTF-8 filename.
static av_always_inline uint8_t colormap_nearest_iterative(const struct color_node *root, const uint8_t *target, const int trans_thresh)
static const AVFilterPad paletteuse_outputs[]
static av_always_inline uint8_t colormap_nearest_recursive(const struct color_node *node, const uint8_t *rgb, const int trans_thresh)
int ff_framesync_configure(FFFrameSync *fs)
Configure a frame sync structure.
static av_always_inline int dither_color(uint32_t px, int er, int eg, int eb, int scale, int shift)
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
static av_cold int init(AVFilterContext *ctx)
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
enum FFFrameSyncExtMode before
Extrapolation mode for timestamps before the first frame.
const char * name
Pad name.
AVFilterContext * parent
Parent filter context.
AVFilterLink ** inputs
array of pointers to input links
AVFilter ff_vf_paletteuse
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
8 bits with AV_PIX_FMT_RGB32 palette
static const uint32_t color[16+AV_CLASS_CATEGORY_NB]
int ff_framesync_init_dualinput(FFFrameSync *fs, AVFilterContext *parent)
Initialize a frame sync structure for dualinput.
static av_cold int end(AVCodecContext *avctx)
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.
static const cmp_func cmp_funcs[]
static int config_input_palette(AVFilterLink *inlink)
FFFrameSyncIn * in
Pointer to array of inputs.
int(* set_frame_func)(struct PaletteUseContext *s, AVFrame *out, AVFrame *in, int x_start, int y_start, int width, int height)
enum FFFrameSyncExtMode after
Extrapolation mode for timestamps after the last frame.
uint32_t palette[AVPALETTE_COUNT]
static const AVOption paletteuse_options[]
A filter pad used for either input or output.
#define DEFINE_SET_FRAME_COLOR_SEARCH(color_search, color_search_macro)
static int disp_tree(const struct color_node *node, const char *fname)
A link between two filters.
static int load_apply_palette(FFFrameSync *fs)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
#define AV_BPRINT_SIZE_UNLIMITED
void ff_framesync_uninit(FFFrameSync *fs)
Free all memory currently allocated.
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
#define COLORMAP_NEAREST(search, palette, root, target, trans_thresh)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
static const uint8_t dither[8][8]
void * priv
private data for use by the filter
static const set_frame_func set_frame_lut[NB_COLOR_SEARCHES][NB_DITHERING]
AVRational time_base
Define the time base used by the PTS of the frames/samples which will pass through this link...
int ff_framesync_activate(FFFrameSync *fs)
Examine the frames in the filter's input and try to produce output.
AVFilterFormats * in_formats
Lists of formats and channel layouts supported by the input and output filters respectively.
int(* on_event)(struct FFFrameSync *fs)
Callback called when a frame event is ready.
static int dither_value(int p)
int w
agreed upon image width
static int debug_accuracy(const struct color_node *node, const uint32_t *palette, const int trans_thresh, const enum color_search_method search_method)
common internal API header
static int get_next_color(const uint8_t *color_used, const uint32_t *palette, const int trans_thresh, int *component, const struct color_rect *box)
static void disp_node(AVBPrint *buf, const struct color_node *map, int parent_id, int node_id, int depth)
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)
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
static int apply_palette(AVFilterLink *inlink, AVFrame *in, AVFrame **outf)
AVFilterContext * src
source filter
static const AVFilterPad inputs[]
static void error(const char *err)
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
static int activate(AVFilterContext *ctx)
static const AVFilterPad outputs[]
#define FF_ARRAY_ELEMS(a)
static int config_output(AVFilterLink *outlink)
#define AV_LOG_INFO
Standard information.
Extend the frame to infinity.
static int cmp_pal_entry(const void *a, const void *b)
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 av_always_inline int color_get(PaletteUseContext *s, uint32_t color, uint8_t a, uint8_t r, uint8_t g, uint8_t b, const enum color_search_method search_method)
Check if the requested color is in the cache already.
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
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)\", 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.
#define DITHERING_ENTRIES(color_search)
const char * name
Filter name.
static void load_colormap(PaletteUseContext *s)
const VDPAUPixFmtMap * map
#define DECLARE_CMP_FUNC(name, pos)
AVFilterLink ** outputs
array of pointers to output links
int av_frame_make_writable(AVFrame *frame)
Ensure that the frame data is writable, avoiding data copy if possible.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
struct cache_node cache[CACHE_SIZE]
int(* cmp_func)(const void *, const void *)
struct cached_color * entries
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, const enum color_search_method search_method)
static av_cold void uninit(AVFilterContext *ctx)
AVFilterContext * dst
dest filter
static const AVFilterPad paletteuse_inputs[]
static av_always_inline uint8_t colormap_nearest_bruteforce(const uint32_t *palette, const uint8_t *argb, const int trans_thresh)
static av_always_inline int diff(const uint8_t *c1, const uint8_t *c2, const int trans_thresh)
struct color_node map[AVPALETTE_COUNT]
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...
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
static void debug_mean_error(PaletteUseContext *s, const AVFrame *in1, const AVFrame *in2, int frame_count)
AVFilterFormats * out_formats