vf_paletteuse.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074
  1. /*
  2. * Copyright (c) 2015 Stupeflix
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * Use a palette to downsample an input video stream.
  23. */
  24. #include "libavutil/bprint.h"
  25. #include "libavutil/internal.h"
  26. #include "libavutil/opt.h"
  27. #include "libavutil/qsort.h"
  28. #include "dualinput.h"
  29. #include "avfilter.h"
  30. enum dithering_mode {
  31. DITHERING_NONE,
  32. DITHERING_BAYER,
  33. DITHERING_HECKBERT,
  34. DITHERING_FLOYD_STEINBERG,
  35. DITHERING_SIERRA2,
  36. DITHERING_SIERRA2_4A,
  37. NB_DITHERING
  38. };
  39. enum color_search_method {
  40. COLOR_SEARCH_NNS_ITERATIVE,
  41. COLOR_SEARCH_NNS_RECURSIVE,
  42. COLOR_SEARCH_BRUTEFORCE,
  43. NB_COLOR_SEARCHES
  44. };
  45. enum diff_mode {
  46. DIFF_MODE_NONE,
  47. DIFF_MODE_RECTANGLE,
  48. NB_DIFF_MODE
  49. };
  50. struct color_node {
  51. uint8_t val[3];
  52. uint8_t palette_id;
  53. int split;
  54. int left_id, right_id;
  55. };
  56. #define NBITS 5
  57. #define CACHE_SIZE (1<<(3*NBITS))
  58. struct cached_color {
  59. uint32_t color;
  60. uint8_t pal_entry;
  61. };
  62. struct cache_node {
  63. struct cached_color *entries;
  64. int nb_entries;
  65. };
  66. struct PaletteUseContext;
  67. typedef int (*set_frame_func)(struct PaletteUseContext *s, AVFrame *out, AVFrame *in,
  68. int x_start, int y_start, int width, int height);
  69. typedef struct PaletteUseContext {
  70. const AVClass *class;
  71. FFDualInputContext dinput;
  72. struct cache_node cache[CACHE_SIZE]; /* lookup cache */
  73. struct color_node map[AVPALETTE_COUNT]; /* 3D-Tree (KD-Tree with K=3) for reverse colormap */
  74. uint32_t palette[AVPALETTE_COUNT];
  75. int palette_loaded;
  76. int dither;
  77. set_frame_func set_frame;
  78. int bayer_scale;
  79. int ordered_dither[8*8];
  80. int diff_mode;
  81. AVFrame *last_in;
  82. AVFrame *last_out;
  83. /* debug options */
  84. char *dot_filename;
  85. int color_search_method;
  86. int calc_mean_err;
  87. uint64_t total_mean_err;
  88. int debug_accuracy;
  89. } PaletteUseContext;
  90. #define OFFSET(x) offsetof(PaletteUseContext, x)
  91. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  92. static const AVOption paletteuse_options[] = {
  93. { "dither", "select dithering mode", OFFSET(dither), AV_OPT_TYPE_INT, {.i64=DITHERING_SIERRA2_4A}, 0, NB_DITHERING-1, FLAGS, "dithering_mode" },
  94. { "bayer", "ordered 8x8 bayer dithering (deterministic)", 0, AV_OPT_TYPE_CONST, {.i64=DITHERING_BAYER}, INT_MIN, INT_MAX, FLAGS, "dithering_mode" },
  95. { "heckbert", "dithering as defined by Paul Heckbert in 1982 (simple error diffusion)", 0, AV_OPT_TYPE_CONST, {.i64=DITHERING_HECKBERT}, INT_MIN, INT_MAX, FLAGS, "dithering_mode" },
  96. { "floyd_steinberg", "Floyd and Steingberg dithering (error diffusion)", 0, AV_OPT_TYPE_CONST, {.i64=DITHERING_FLOYD_STEINBERG}, INT_MIN, INT_MAX, FLAGS, "dithering_mode" },
  97. { "sierra2", "Frankie Sierra dithering v2 (error diffusion)", 0, AV_OPT_TYPE_CONST, {.i64=DITHERING_SIERRA2}, INT_MIN, INT_MAX, FLAGS, "dithering_mode" },
  98. { "sierra2_4a", "Frankie Sierra dithering v2 \"Lite\" (error diffusion)", 0, AV_OPT_TYPE_CONST, {.i64=DITHERING_SIERRA2_4A}, INT_MIN, INT_MAX, FLAGS, "dithering_mode" },
  99. { "bayer_scale", "set scale for bayer dithering", OFFSET(bayer_scale), AV_OPT_TYPE_INT, {.i64=2}, 0, 5, FLAGS },
  100. { "diff_mode", "set frame difference mode", OFFSET(diff_mode), AV_OPT_TYPE_INT, {.i64=DIFF_MODE_NONE}, 0, NB_DIFF_MODE-1, FLAGS, "diff_mode" },
  101. { "rectangle", "process smallest different rectangle", 0, AV_OPT_TYPE_CONST, {.i64=DIFF_MODE_RECTANGLE}, INT_MIN, INT_MAX, FLAGS, "diff_mode" },
  102. /* following are the debug options, not part of the official API */
  103. { "debug_kdtree", "save Graphviz graph of the kdtree in specified file", OFFSET(dot_filename), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
  104. { "color_search", "set reverse colormap color search method", OFFSET(color_search_method), AV_OPT_TYPE_INT, {.i64=COLOR_SEARCH_NNS_ITERATIVE}, 0, NB_COLOR_SEARCHES-1, FLAGS, "search" },
  105. { "nns_iterative", "iterative search", 0, AV_OPT_TYPE_CONST, {.i64=COLOR_SEARCH_NNS_ITERATIVE}, INT_MIN, INT_MAX, FLAGS, "search" },
  106. { "nns_recursive", "recursive search", 0, AV_OPT_TYPE_CONST, {.i64=COLOR_SEARCH_NNS_RECURSIVE}, INT_MIN, INT_MAX, FLAGS, "search" },
  107. { "bruteforce", "brute-force into the palette", 0, AV_OPT_TYPE_CONST, {.i64=COLOR_SEARCH_BRUTEFORCE}, INT_MIN, INT_MAX, FLAGS, "search" },
  108. { "mean_err", "compute and print mean error", OFFSET(calc_mean_err), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
  109. { "debug_accuracy", "test color search accuracy", OFFSET(debug_accuracy), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
  110. { NULL }
  111. };
  112. AVFILTER_DEFINE_CLASS(paletteuse);
  113. static int query_formats(AVFilterContext *ctx)
  114. {
  115. static const enum AVPixelFormat in_fmts[] = {AV_PIX_FMT_RGB32, AV_PIX_FMT_NONE};
  116. static const enum AVPixelFormat inpal_fmts[] = {AV_PIX_FMT_RGB32, AV_PIX_FMT_NONE};
  117. static const enum AVPixelFormat out_fmts[] = {AV_PIX_FMT_PAL8, AV_PIX_FMT_NONE};
  118. int ret;
  119. AVFilterFormats *in = ff_make_format_list(in_fmts);
  120. AVFilterFormats *inpal = ff_make_format_list(inpal_fmts);
  121. AVFilterFormats *out = ff_make_format_list(out_fmts);
  122. if (!in || !inpal || !out) {
  123. av_freep(&in);
  124. av_freep(&inpal);
  125. av_freep(&out);
  126. return AVERROR(ENOMEM);
  127. }
  128. if ((ret = ff_formats_ref(in , &ctx->inputs[0]->out_formats)) < 0 ||
  129. (ret = ff_formats_ref(inpal, &ctx->inputs[1]->out_formats)) < 0 ||
  130. (ret = ff_formats_ref(out , &ctx->outputs[0]->in_formats)) < 0)
  131. return ret;
  132. return 0;
  133. }
  134. static av_always_inline int dither_color(uint32_t px, int er, int eg, int eb, int scale, int shift)
  135. {
  136. return av_clip_uint8((px >> 16 & 0xff) + ((er * scale) / (1<<shift))) << 16
  137. | av_clip_uint8((px >> 8 & 0xff) + ((eg * scale) / (1<<shift))) << 8
  138. | av_clip_uint8((px & 0xff) + ((eb * scale) / (1<<shift)));
  139. }
  140. static av_always_inline int diff(const uint8_t *c1, const uint8_t *c2)
  141. {
  142. // XXX: try L*a*b with CIE76 (dL*dL + da*da + db*db)
  143. const int dr = c1[0] - c2[0];
  144. const int dg = c1[1] - c2[1];
  145. const int db = c1[2] - c2[2];
  146. return dr*dr + dg*dg + db*db;
  147. }
  148. static av_always_inline uint8_t colormap_nearest_bruteforce(const uint32_t *palette, const uint8_t *rgb)
  149. {
  150. int i, pal_id = -1, min_dist = INT_MAX;
  151. for (i = 0; i < AVPALETTE_COUNT; i++) {
  152. const uint32_t c = palette[i];
  153. if ((c & 0xff000000) == 0xff000000) { // ignore transparent entry
  154. const uint8_t palrgb[] = {
  155. palette[i]>>16 & 0xff,
  156. palette[i]>> 8 & 0xff,
  157. palette[i] & 0xff,
  158. };
  159. const int d = diff(palrgb, rgb);
  160. if (d < min_dist) {
  161. pal_id = i;
  162. min_dist = d;
  163. }
  164. }
  165. }
  166. return pal_id;
  167. }
  168. /* Recursive form, simpler but a bit slower. Kept for reference. */
  169. struct nearest_color {
  170. int node_pos;
  171. int dist_sqd;
  172. };
  173. static void colormap_nearest_node(const struct color_node *map,
  174. const int node_pos,
  175. const uint8_t *target,
  176. struct nearest_color *nearest)
  177. {
  178. const struct color_node *kd = map + node_pos;
  179. const int s = kd->split;
  180. int dx, nearer_kd_id, further_kd_id;
  181. const uint8_t *current = kd->val;
  182. const int current_to_target = diff(target, current);
  183. if (current_to_target < nearest->dist_sqd) {
  184. nearest->node_pos = node_pos;
  185. nearest->dist_sqd = current_to_target;
  186. }
  187. if (kd->left_id != -1 || kd->right_id != -1) {
  188. dx = target[s] - current[s];
  189. if (dx <= 0) nearer_kd_id = kd->left_id, further_kd_id = kd->right_id;
  190. else nearer_kd_id = kd->right_id, further_kd_id = kd->left_id;
  191. if (nearer_kd_id != -1)
  192. colormap_nearest_node(map, nearer_kd_id, target, nearest);
  193. if (further_kd_id != -1 && dx*dx < nearest->dist_sqd)
  194. colormap_nearest_node(map, further_kd_id, target, nearest);
  195. }
  196. }
  197. static av_always_inline uint8_t colormap_nearest_recursive(const struct color_node *node, const uint8_t *rgb)
  198. {
  199. struct nearest_color res = {.dist_sqd = INT_MAX, .node_pos = -1};
  200. colormap_nearest_node(node, 0, rgb, &res);
  201. return node[res.node_pos].palette_id;
  202. }
  203. struct stack_node {
  204. int color_id;
  205. int dx2;
  206. };
  207. static av_always_inline uint8_t colormap_nearest_iterative(const struct color_node *root, const uint8_t *target)
  208. {
  209. int pos = 0, best_node_id = -1, best_dist = INT_MAX, cur_color_id = 0;
  210. struct stack_node nodes[16];
  211. struct stack_node *node = &nodes[0];
  212. for (;;) {
  213. const struct color_node *kd = &root[cur_color_id];
  214. const uint8_t *current = kd->val;
  215. const int current_to_target = diff(target, current);
  216. /* Compare current color node to the target and update our best node if
  217. * it's actually better. */
  218. if (current_to_target < best_dist) {
  219. best_node_id = cur_color_id;
  220. if (!current_to_target)
  221. goto end; // exact match, we can return immediately
  222. best_dist = current_to_target;
  223. }
  224. /* Check if it's not a leaf */
  225. if (kd->left_id != -1 || kd->right_id != -1) {
  226. const int split = kd->split;
  227. const int dx = target[split] - current[split];
  228. int nearer_kd_id, further_kd_id;
  229. /* Define which side is the most interesting. */
  230. if (dx <= 0) nearer_kd_id = kd->left_id, further_kd_id = kd->right_id;
  231. else nearer_kd_id = kd->right_id, further_kd_id = kd->left_id;
  232. if (nearer_kd_id != -1) {
  233. if (further_kd_id != -1) {
  234. /* Here, both paths are defined, so we push a state for
  235. * when we are going back. */
  236. node->color_id = further_kd_id;
  237. node->dx2 = dx*dx;
  238. pos++;
  239. node++;
  240. }
  241. /* We can now update current color with the most probable path
  242. * (no need to create a state since there is nothing to save
  243. * anymore). */
  244. cur_color_id = nearer_kd_id;
  245. continue;
  246. } else if (dx*dx < best_dist) {
  247. /* The nearest path isn't available, so there is only one path
  248. * possible and it's the least probable. We enter it only if the
  249. * distance from the current point to the hyper rectangle is
  250. * less than our best distance. */
  251. cur_color_id = further_kd_id;
  252. continue;
  253. }
  254. }
  255. /* Unstack as much as we can, typically as long as the least probable
  256. * branch aren't actually probable. */
  257. do {
  258. if (--pos < 0)
  259. goto end;
  260. node--;
  261. } while (node->dx2 >= best_dist);
  262. /* We got a node where the least probable branch might actually contain
  263. * a relevant color. */
  264. cur_color_id = node->color_id;
  265. }
  266. end:
  267. return root[best_node_id].palette_id;
  268. }
  269. #define COLORMAP_NEAREST(search, palette, root, target) \
  270. search == COLOR_SEARCH_NNS_ITERATIVE ? colormap_nearest_iterative(root, target) : \
  271. search == COLOR_SEARCH_NNS_RECURSIVE ? colormap_nearest_recursive(root, target) : \
  272. colormap_nearest_bruteforce(palette, target)
  273. /**
  274. * Check if the requested color is in the cache already. If not, find it in the
  275. * color tree and cache it.
  276. * Note: r, g, and b are the component of c but are passed as well to avoid
  277. * recomputing them (they are generally computed by the caller for other uses).
  278. */
  279. static av_always_inline int color_get(struct cache_node *cache, uint32_t color,
  280. uint8_t r, uint8_t g, uint8_t b,
  281. const struct color_node *map,
  282. const uint32_t *palette,
  283. const enum color_search_method search_method)
  284. {
  285. int i;
  286. const uint8_t rgb[] = {r, g, b};
  287. const uint8_t rhash = r & ((1<<NBITS)-1);
  288. const uint8_t ghash = g & ((1<<NBITS)-1);
  289. const uint8_t bhash = b & ((1<<NBITS)-1);
  290. const unsigned hash = rhash<<(NBITS*2) | ghash<<NBITS | bhash;
  291. struct cache_node *node = &cache[hash];
  292. struct cached_color *e;
  293. for (i = 0; i < node->nb_entries; i++) {
  294. e = &node->entries[i];
  295. if (e->color == color)
  296. return e->pal_entry;
  297. }
  298. e = av_dynarray2_add((void**)&node->entries, &node->nb_entries,
  299. sizeof(*node->entries), NULL);
  300. if (!e)
  301. return AVERROR(ENOMEM);
  302. e->color = color;
  303. e->pal_entry = COLORMAP_NEAREST(search_method, palette, map, rgb);
  304. return e->pal_entry;
  305. }
  306. static av_always_inline int get_dst_color_err(struct cache_node *cache,
  307. uint32_t c, const struct color_node *map,
  308. const uint32_t *palette,
  309. int *er, int *eg, int *eb,
  310. const enum color_search_method search_method)
  311. {
  312. const uint8_t r = c >> 16 & 0xff;
  313. const uint8_t g = c >> 8 & 0xff;
  314. const uint8_t b = c & 0xff;
  315. const int dstx = color_get(cache, c, r, g, b, map, palette, search_method);
  316. const uint32_t dstc = palette[dstx];
  317. *er = r - (dstc >> 16 & 0xff);
  318. *eg = g - (dstc >> 8 & 0xff);
  319. *eb = b - (dstc & 0xff);
  320. return dstx;
  321. }
  322. static av_always_inline int set_frame(PaletteUseContext *s, AVFrame *out, AVFrame *in,
  323. int x_start, int y_start, int w, int h,
  324. enum dithering_mode dither,
  325. const enum color_search_method search_method)
  326. {
  327. int x, y;
  328. const struct color_node *map = s->map;
  329. struct cache_node *cache = s->cache;
  330. const uint32_t *palette = s->palette;
  331. const int src_linesize = in ->linesize[0] >> 2;
  332. const int dst_linesize = out->linesize[0];
  333. uint32_t *src = ((uint32_t *)in ->data[0]) + y_start*src_linesize;
  334. uint8_t *dst = out->data[0] + y_start*dst_linesize;
  335. w += x_start;
  336. h += y_start;
  337. for (y = y_start; y < h; y++) {
  338. for (x = x_start; x < w; x++) {
  339. int er, eg, eb;
  340. if (dither == DITHERING_BAYER) {
  341. const int d = s->ordered_dither[(y & 7)<<3 | (x & 7)];
  342. const uint8_t r8 = src[x] >> 16 & 0xff;
  343. const uint8_t g8 = src[x] >> 8 & 0xff;
  344. const uint8_t b8 = src[x] & 0xff;
  345. const uint8_t r = av_clip_uint8(r8 + d);
  346. const uint8_t g = av_clip_uint8(g8 + d);
  347. const uint8_t b = av_clip_uint8(b8 + d);
  348. const uint32_t c = r<<16 | g<<8 | b;
  349. const int color = color_get(cache, c, r, g, b, map, palette, search_method);
  350. if (color < 0)
  351. return color;
  352. dst[x] = color;
  353. } else if (dither == DITHERING_HECKBERT) {
  354. const int right = x < w - 1, down = y < h - 1;
  355. const int color = get_dst_color_err(cache, src[x], map, palette, &er, &eg, &eb, search_method);
  356. if (color < 0)
  357. return color;
  358. dst[x] = color;
  359. if (right) src[ x + 1] = dither_color(src[ x + 1], er, eg, eb, 3, 3);
  360. if ( down) src[src_linesize + x ] = dither_color(src[src_linesize + x ], er, eg, eb, 3, 3);
  361. if (right && down) src[src_linesize + x + 1] = dither_color(src[src_linesize + x + 1], er, eg, eb, 2, 3);
  362. } else if (dither == DITHERING_FLOYD_STEINBERG) {
  363. const int right = x < w - 1, down = y < h - 1, left = x > x_start;
  364. const int color = get_dst_color_err(cache, src[x], map, palette, &er, &eg, &eb, search_method);
  365. if (color < 0)
  366. return color;
  367. dst[x] = color;
  368. if (right) src[ x + 1] = dither_color(src[ x + 1], er, eg, eb, 7, 4);
  369. if (left && down) src[src_linesize + x - 1] = dither_color(src[src_linesize + x - 1], er, eg, eb, 3, 4);
  370. if ( down) src[src_linesize + x ] = dither_color(src[src_linesize + x ], er, eg, eb, 5, 4);
  371. if (right && down) src[src_linesize + x + 1] = dither_color(src[src_linesize + x + 1], er, eg, eb, 1, 4);
  372. } else if (dither == DITHERING_SIERRA2) {
  373. const int right = x < w - 1, down = y < h - 1, left = x > x_start;
  374. const int right2 = x < w - 2, left2 = x > x_start + 1;
  375. const int color = get_dst_color_err(cache, src[x], map, palette, &er, &eg, &eb, search_method);
  376. if (color < 0)
  377. return color;
  378. dst[x] = color;
  379. if (right) src[ x + 1] = dither_color(src[ x + 1], er, eg, eb, 4, 4);
  380. if (right2) src[ x + 2] = dither_color(src[ x + 2], er, eg, eb, 3, 4);
  381. if (down) {
  382. if (left2) src[ src_linesize + x - 2] = dither_color(src[ src_linesize + x - 2], er, eg, eb, 1, 4);
  383. if (left) src[ src_linesize + x - 1] = dither_color(src[ src_linesize + x - 1], er, eg, eb, 2, 4);
  384. src[ src_linesize + x ] = dither_color(src[ src_linesize + x ], er, eg, eb, 3, 4);
  385. if (right) src[ src_linesize + x + 1] = dither_color(src[ src_linesize + x + 1], er, eg, eb, 2, 4);
  386. if (right2) src[ src_linesize + x + 2] = dither_color(src[ src_linesize + x + 2], er, eg, eb, 1, 4);
  387. }
  388. } else if (dither == DITHERING_SIERRA2_4A) {
  389. const int right = x < w - 1, down = y < h - 1, left = x > x_start;
  390. const int color = get_dst_color_err(cache, src[x], map, palette, &er, &eg, &eb, search_method);
  391. if (color < 0)
  392. return color;
  393. dst[x] = color;
  394. if (right) src[ x + 1] = dither_color(src[ x + 1], er, eg, eb, 2, 2);
  395. if (left && down) src[src_linesize + x - 1] = dither_color(src[src_linesize + x - 1], er, eg, eb, 1, 2);
  396. if ( down) src[src_linesize + x ] = dither_color(src[src_linesize + x ], er, eg, eb, 1, 2);
  397. } else {
  398. const uint8_t r = src[x] >> 16 & 0xff;
  399. const uint8_t g = src[x] >> 8 & 0xff;
  400. const uint8_t b = src[x] & 0xff;
  401. const int color = color_get(cache, src[x] & 0xffffff, r, g, b, map, palette, search_method);
  402. if (color < 0)
  403. return color;
  404. dst[x] = color;
  405. }
  406. }
  407. src += src_linesize;
  408. dst += dst_linesize;
  409. }
  410. return 0;
  411. }
  412. #define INDENT 4
  413. static void disp_node(AVBPrint *buf,
  414. const struct color_node *map,
  415. int parent_id, int node_id,
  416. int depth)
  417. {
  418. const struct color_node *node = &map[node_id];
  419. const uint32_t fontcolor = node->val[0] > 0x50 &&
  420. node->val[1] > 0x50 &&
  421. node->val[2] > 0x50 ? 0 : 0xffffff;
  422. av_bprintf(buf, "%*cnode%d ["
  423. "label=\"%c%02X%c%02X%c%02X%c\" "
  424. "fillcolor=\"#%02x%02x%02x\" "
  425. "fontcolor=\"#%06X\"]\n",
  426. depth*INDENT, ' ', node->palette_id,
  427. "[ "[node->split], node->val[0],
  428. "][ "[node->split], node->val[1],
  429. " ]["[node->split], node->val[2],
  430. " ]"[node->split],
  431. node->val[0], node->val[1], node->val[2],
  432. fontcolor);
  433. if (parent_id != -1)
  434. av_bprintf(buf, "%*cnode%d -> node%d\n", depth*INDENT, ' ',
  435. map[parent_id].palette_id, node->palette_id);
  436. if (node->left_id != -1) disp_node(buf, map, node_id, node->left_id, depth + 1);
  437. if (node->right_id != -1) disp_node(buf, map, node_id, node->right_id, depth + 1);
  438. }
  439. // debug_kdtree=kdtree.dot -> dot -Tpng kdtree.dot > kdtree.png
  440. static int disp_tree(const struct color_node *node, const char *fname)
  441. {
  442. AVBPrint buf;
  443. FILE *f = av_fopen_utf8(fname, "w");
  444. if (!f) {
  445. int ret = AVERROR(errno);
  446. av_log(NULL, AV_LOG_ERROR, "Cannot open file '%s' for writing: %s\n",
  447. fname, av_err2str(ret));
  448. return ret;
  449. }
  450. av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
  451. av_bprintf(&buf, "digraph {\n");
  452. av_bprintf(&buf, " node [style=filled fontsize=10 shape=box]\n");
  453. disp_node(&buf, node, -1, 0, 0);
  454. av_bprintf(&buf, "}\n");
  455. fwrite(buf.str, 1, buf.len, f);
  456. fclose(f);
  457. av_bprint_finalize(&buf, NULL);
  458. return 0;
  459. }
  460. static int debug_accuracy(const struct color_node *node, const uint32_t *palette,
  461. const enum color_search_method search_method)
  462. {
  463. int r, g, b, ret = 0;
  464. for (r = 0; r < 256; r++) {
  465. for (g = 0; g < 256; g++) {
  466. for (b = 0; b < 256; b++) {
  467. const uint8_t rgb[] = {r, g, b};
  468. const int r1 = COLORMAP_NEAREST(search_method, palette, node, rgb);
  469. const int r2 = colormap_nearest_bruteforce(palette, rgb);
  470. if (r1 != r2) {
  471. const uint32_t c1 = palette[r1];
  472. const uint32_t c2 = palette[r2];
  473. const uint8_t palrgb1[] = { c1>>16 & 0xff, c1>> 8 & 0xff, c1 & 0xff };
  474. const uint8_t palrgb2[] = { c2>>16 & 0xff, c2>> 8 & 0xff, c2 & 0xff };
  475. const int d1 = diff(palrgb1, rgb);
  476. const int d2 = diff(palrgb2, rgb);
  477. if (d1 != d2) {
  478. av_log(NULL, AV_LOG_ERROR,
  479. "/!\\ %02X%02X%02X: %d ! %d (%06X ! %06X) / dist: %d ! %d\n",
  480. r, g, b, r1, r2, c1 & 0xffffff, c2 & 0xffffff, d1, d2);
  481. ret = 1;
  482. }
  483. }
  484. }
  485. }
  486. }
  487. return ret;
  488. }
  489. struct color {
  490. uint32_t value;
  491. uint8_t pal_id;
  492. };
  493. struct color_rect {
  494. uint8_t min[3];
  495. uint8_t max[3];
  496. };
  497. typedef int (*cmp_func)(const void *, const void *);
  498. #define DECLARE_CMP_FUNC(name, pos) \
  499. static int cmp_##name(const void *pa, const void *pb) \
  500. { \
  501. const struct color *a = pa; \
  502. const struct color *b = pb; \
  503. return (a->value >> (8 * (2 - (pos))) & 0xff) \
  504. - (b->value >> (8 * (2 - (pos))) & 0xff); \
  505. }
  506. DECLARE_CMP_FUNC(r, 0)
  507. DECLARE_CMP_FUNC(g, 1)
  508. DECLARE_CMP_FUNC(b, 2)
  509. static const cmp_func cmp_funcs[] = {cmp_r, cmp_g, cmp_b};
  510. static int get_next_color(const uint8_t *color_used, const uint32_t *palette,
  511. int *component, const struct color_rect *box)
  512. {
  513. int wr, wg, wb;
  514. int i, longest = 0;
  515. unsigned nb_color = 0;
  516. struct color_rect ranges;
  517. struct color tmp_pal[256];
  518. cmp_func cmpf;
  519. ranges.min[0] = ranges.min[1] = ranges.min[2] = 0xff;
  520. ranges.max[0] = ranges.max[1] = ranges.max[2] = 0x00;
  521. for (i = 0; i < AVPALETTE_COUNT; i++) {
  522. const uint32_t c = palette[i];
  523. const uint8_t r = c >> 16 & 0xff;
  524. const uint8_t g = c >> 8 & 0xff;
  525. const uint8_t b = c & 0xff;
  526. if (color_used[i] ||
  527. r < box->min[0] || g < box->min[1] || b < box->min[2] ||
  528. r > box->max[0] || g > box->max[1] || b > box->max[2])
  529. continue;
  530. if (r < ranges.min[0]) ranges.min[0] = r;
  531. if (g < ranges.min[1]) ranges.min[1] = g;
  532. if (b < ranges.min[2]) ranges.min[2] = b;
  533. if (r > ranges.max[0]) ranges.max[0] = r;
  534. if (g > ranges.max[1]) ranges.max[1] = g;
  535. if (b > ranges.max[2]) ranges.max[2] = b;
  536. tmp_pal[nb_color].value = c;
  537. tmp_pal[nb_color].pal_id = i;
  538. nb_color++;
  539. }
  540. if (!nb_color)
  541. return -1;
  542. /* define longest axis that will be the split component */
  543. wr = ranges.max[0] - ranges.min[0];
  544. wg = ranges.max[1] - ranges.min[1];
  545. wb = ranges.max[2] - ranges.min[2];
  546. if (wr >= wg && wr >= wb) longest = 0;
  547. if (wg >= wr && wg >= wb) longest = 1;
  548. if (wb >= wr && wb >= wg) longest = 2;
  549. cmpf = cmp_funcs[longest];
  550. *component = longest;
  551. /* sort along this axis to get median */
  552. AV_QSORT(tmp_pal, nb_color, struct color, cmpf);
  553. return tmp_pal[nb_color >> 1].pal_id;
  554. }
  555. static int colormap_insert(struct color_node *map,
  556. uint8_t *color_used,
  557. int *nb_used,
  558. const uint32_t *palette,
  559. const struct color_rect *box)
  560. {
  561. uint32_t c;
  562. int component, cur_id;
  563. int node_left_id = -1, node_right_id = -1;
  564. struct color_node *node;
  565. struct color_rect box1, box2;
  566. const int pal_id = get_next_color(color_used, palette, &component, box);
  567. if (pal_id < 0)
  568. return -1;
  569. /* create new node with that color */
  570. cur_id = (*nb_used)++;
  571. c = palette[pal_id];
  572. node = &map[cur_id];
  573. node->split = component;
  574. node->palette_id = pal_id;
  575. node->val[0] = c>>16 & 0xff;
  576. node->val[1] = c>> 8 & 0xff;
  577. node->val[2] = c & 0xff;
  578. color_used[pal_id] = 1;
  579. /* get the two boxes this node creates */
  580. box1 = box2 = *box;
  581. box1.max[component] = node->val[component];
  582. box2.min[component] = node->val[component] + 1;
  583. node_left_id = colormap_insert(map, color_used, nb_used, palette, &box1);
  584. if (box2.min[component] <= box2.max[component])
  585. node_right_id = colormap_insert(map, color_used, nb_used, palette, &box2);
  586. node->left_id = node_left_id;
  587. node->right_id = node_right_id;
  588. return cur_id;
  589. }
  590. static int cmp_pal_entry(const void *a, const void *b)
  591. {
  592. const int c1 = *(const uint32_t *)a & 0xffffff;
  593. const int c2 = *(const uint32_t *)b & 0xffffff;
  594. return c1 - c2;
  595. }
  596. static void load_colormap(PaletteUseContext *s)
  597. {
  598. int i, nb_used = 0;
  599. uint8_t color_used[AVPALETTE_COUNT] = {0};
  600. uint32_t last_color = 0;
  601. struct color_rect box;
  602. /* disable transparent colors and dups */
  603. qsort(s->palette, AVPALETTE_COUNT, sizeof(*s->palette), cmp_pal_entry);
  604. for (i = 0; i < AVPALETTE_COUNT; i++) {
  605. const uint32_t c = s->palette[i];
  606. if (i != 0 && c == last_color) {
  607. color_used[i] = 1;
  608. continue;
  609. }
  610. last_color = c;
  611. if ((c & 0xff000000) != 0xff000000) {
  612. color_used[i] = 1; // ignore transparent color(s)
  613. continue;
  614. }
  615. }
  616. box.min[0] = box.min[1] = box.min[2] = 0x00;
  617. box.max[0] = box.max[1] = box.max[2] = 0xff;
  618. colormap_insert(s->map, color_used, &nb_used, s->palette, &box);
  619. if (s->dot_filename)
  620. disp_tree(s->map, s->dot_filename);
  621. if (s->debug_accuracy) {
  622. if (!debug_accuracy(s->map, s->palette, s->color_search_method))
  623. av_log(NULL, AV_LOG_INFO, "Accuracy check passed\n");
  624. }
  625. }
  626. static void debug_mean_error(PaletteUseContext *s, const AVFrame *in1,
  627. const AVFrame *in2, int frame_count)
  628. {
  629. int x, y;
  630. const uint32_t *palette = s->palette;
  631. uint32_t *src1 = (uint32_t *)in1->data[0];
  632. uint8_t *src2 = in2->data[0];
  633. const int src1_linesize = in1->linesize[0] >> 2;
  634. const int src2_linesize = in2->linesize[0];
  635. const float div = in1->width * in1->height * 3;
  636. unsigned mean_err = 0;
  637. for (y = 0; y < in1->height; y++) {
  638. for (x = 0; x < in1->width; x++) {
  639. const uint32_t c1 = src1[x];
  640. const uint32_t c2 = palette[src2[x]];
  641. const uint8_t rgb1[] = {c1 >> 16 & 0xff, c1 >> 8 & 0xff, c1 & 0xff};
  642. const uint8_t rgb2[] = {c2 >> 16 & 0xff, c2 >> 8 & 0xff, c2 & 0xff};
  643. mean_err += diff(rgb1, rgb2);
  644. }
  645. src1 += src1_linesize;
  646. src2 += src2_linesize;
  647. }
  648. s->total_mean_err += mean_err;
  649. av_log(NULL, AV_LOG_INFO, "MEP:%.3f TotalMEP:%.3f\n",
  650. mean_err / div, s->total_mean_err / (div * frame_count));
  651. }
  652. static void set_processing_window(enum diff_mode diff_mode,
  653. const AVFrame *prv_src, const AVFrame *cur_src,
  654. const AVFrame *prv_dst, AVFrame *cur_dst,
  655. int *xp, int *yp, int *wp, int *hp)
  656. {
  657. int x_start = 0, y_start = 0;
  658. int width = cur_src->width;
  659. int height = cur_src->height;
  660. if (prv_src && diff_mode == DIFF_MODE_RECTANGLE) {
  661. int y;
  662. int x_end = cur_src->width - 1,
  663. y_end = cur_src->height - 1;
  664. const uint32_t *prv_srcp = (const uint32_t *)prv_src->data[0];
  665. const uint32_t *cur_srcp = (const uint32_t *)cur_src->data[0];
  666. const uint8_t *prv_dstp = prv_dst->data[0];
  667. uint8_t *cur_dstp = cur_dst->data[0];
  668. const int prv_src_linesize = prv_src->linesize[0] >> 2;
  669. const int cur_src_linesize = cur_src->linesize[0] >> 2;
  670. const int prv_dst_linesize = prv_dst->linesize[0];
  671. const int cur_dst_linesize = cur_dst->linesize[0];
  672. /* skip common lines */
  673. while (y_start < y_end && !memcmp(prv_srcp + y_start*prv_src_linesize,
  674. cur_srcp + y_start*cur_src_linesize,
  675. cur_src->width * 4)) {
  676. memcpy(cur_dstp + y_start*cur_dst_linesize,
  677. prv_dstp + y_start*prv_dst_linesize,
  678. cur_dst->width);
  679. y_start++;
  680. }
  681. while (y_end > y_start && !memcmp(prv_srcp + y_end*prv_src_linesize,
  682. cur_srcp + y_end*cur_src_linesize,
  683. cur_src->width * 4)) {
  684. memcpy(cur_dstp + y_end*cur_dst_linesize,
  685. prv_dstp + y_end*prv_dst_linesize,
  686. cur_dst->width);
  687. y_end--;
  688. }
  689. height = y_end + 1 - y_start;
  690. /* skip common columns */
  691. while (x_start < x_end) {
  692. int same_column = 1;
  693. for (y = y_start; y <= y_end; y++) {
  694. if (prv_srcp[y*prv_src_linesize + x_start] != cur_srcp[y*cur_src_linesize + x_start]) {
  695. same_column = 0;
  696. break;
  697. }
  698. }
  699. if (!same_column)
  700. break;
  701. x_start++;
  702. }
  703. while (x_end > x_start) {
  704. int same_column = 1;
  705. for (y = y_start; y <= y_end; y++) {
  706. if (prv_srcp[y*prv_src_linesize + x_end] != cur_srcp[y*cur_src_linesize + x_end]) {
  707. same_column = 0;
  708. break;
  709. }
  710. }
  711. if (!same_column)
  712. break;
  713. x_end--;
  714. }
  715. width = x_end + 1 - x_start;
  716. if (x_start) {
  717. for (y = y_start; y <= y_end; y++)
  718. memcpy(cur_dstp + y*cur_dst_linesize,
  719. prv_dstp + y*prv_dst_linesize, x_start);
  720. }
  721. if (x_end != cur_src->width - 1) {
  722. const int copy_len = cur_src->width - 1 - x_end;
  723. for (y = y_start; y <= y_end; y++)
  724. memcpy(cur_dstp + y*cur_dst_linesize + x_end + 1,
  725. prv_dstp + y*prv_dst_linesize + x_end + 1,
  726. copy_len);
  727. }
  728. }
  729. *xp = x_start;
  730. *yp = y_start;
  731. *wp = width;
  732. *hp = height;
  733. }
  734. static AVFrame *apply_palette(AVFilterLink *inlink, AVFrame *in)
  735. {
  736. int x, y, w, h;
  737. AVFilterContext *ctx = inlink->dst;
  738. PaletteUseContext *s = ctx->priv;
  739. AVFilterLink *outlink = inlink->dst->outputs[0];
  740. AVFrame *out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  741. if (!out) {
  742. av_frame_free(&in);
  743. return NULL;
  744. }
  745. av_frame_copy_props(out, in);
  746. set_processing_window(s->diff_mode, s->last_in, in,
  747. s->last_out, out, &x, &y, &w, &h);
  748. av_frame_free(&s->last_in);
  749. av_frame_free(&s->last_out);
  750. s->last_in = av_frame_clone(in);
  751. s->last_out = av_frame_clone(out);
  752. if (!s->last_in || !s->last_out ||
  753. av_frame_make_writable(s->last_in) < 0) {
  754. av_frame_free(&in);
  755. av_frame_free(&out);
  756. return NULL;
  757. }
  758. ff_dlog(ctx, "%dx%d rect: (%d;%d) -> (%d,%d) [area:%dx%d]\n",
  759. w, h, x, y, x+w, y+h, in->width, in->height);
  760. if (s->set_frame(s, out, in, x, y, w, h) < 0) {
  761. av_frame_free(&out);
  762. return NULL;
  763. }
  764. memcpy(out->data[1], s->palette, AVPALETTE_SIZE);
  765. if (s->calc_mean_err)
  766. debug_mean_error(s, in, out, inlink->frame_count);
  767. av_frame_free(&in);
  768. return out;
  769. }
  770. static int config_output(AVFilterLink *outlink)
  771. {
  772. int ret;
  773. AVFilterContext *ctx = outlink->src;
  774. PaletteUseContext *s = ctx->priv;
  775. outlink->w = ctx->inputs[0]->w;
  776. outlink->h = ctx->inputs[0]->h;
  777. outlink->time_base = ctx->inputs[0]->time_base;
  778. if ((ret = ff_dualinput_init(ctx, &s->dinput)) < 0)
  779. return ret;
  780. return 0;
  781. }
  782. static int config_input_palette(AVFilterLink *inlink)
  783. {
  784. AVFilterContext *ctx = inlink->dst;
  785. if (inlink->w * inlink->h != AVPALETTE_COUNT) {
  786. av_log(ctx, AV_LOG_ERROR,
  787. "Palette input must contain exactly %d pixels. "
  788. "Specified input has %dx%d=%d pixels\n",
  789. AVPALETTE_COUNT, inlink->w, inlink->h,
  790. inlink->w * inlink->h);
  791. return AVERROR(EINVAL);
  792. }
  793. return 0;
  794. }
  795. static void load_palette(PaletteUseContext *s, const AVFrame *palette_frame)
  796. {
  797. int i, x, y;
  798. const uint32_t *p = (const uint32_t *)palette_frame->data[0];
  799. const int p_linesize = palette_frame->linesize[0] >> 2;
  800. i = 0;
  801. for (y = 0; y < palette_frame->height; y++) {
  802. for (x = 0; x < palette_frame->width; x++)
  803. s->palette[i++] = p[x];
  804. p += p_linesize;
  805. }
  806. load_colormap(s);
  807. s->palette_loaded = 1;
  808. }
  809. static AVFrame *load_apply_palette(AVFilterContext *ctx, AVFrame *main,
  810. const AVFrame *second)
  811. {
  812. AVFilterLink *inlink = ctx->inputs[0];
  813. PaletteUseContext *s = ctx->priv;
  814. if (!s->palette_loaded) {
  815. load_palette(s, second);
  816. }
  817. return apply_palette(inlink, main);
  818. }
  819. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  820. {
  821. PaletteUseContext *s = inlink->dst->priv;
  822. return ff_dualinput_filter_frame(&s->dinput, inlink, in);
  823. }
  824. #define DEFINE_SET_FRAME(color_search, name, value) \
  825. static int set_frame_##name(PaletteUseContext *s, AVFrame *out, AVFrame *in, \
  826. int x_start, int y_start, int w, int h) \
  827. { \
  828. return set_frame(s, out, in, x_start, y_start, w, h, value, color_search); \
  829. }
  830. #define DEFINE_SET_FRAME_COLOR_SEARCH(color_search, color_search_macro) \
  831. DEFINE_SET_FRAME(color_search_macro, color_search##_##none, DITHERING_NONE) \
  832. DEFINE_SET_FRAME(color_search_macro, color_search##_##bayer, DITHERING_BAYER) \
  833. DEFINE_SET_FRAME(color_search_macro, color_search##_##heckbert, DITHERING_HECKBERT) \
  834. DEFINE_SET_FRAME(color_search_macro, color_search##_##floyd_steinberg, DITHERING_FLOYD_STEINBERG) \
  835. DEFINE_SET_FRAME(color_search_macro, color_search##_##sierra2, DITHERING_SIERRA2) \
  836. DEFINE_SET_FRAME(color_search_macro, color_search##_##sierra2_4a, DITHERING_SIERRA2_4A) \
  837. DEFINE_SET_FRAME_COLOR_SEARCH(nns_iterative, COLOR_SEARCH_NNS_ITERATIVE)
  838. DEFINE_SET_FRAME_COLOR_SEARCH(nns_recursive, COLOR_SEARCH_NNS_RECURSIVE)
  839. DEFINE_SET_FRAME_COLOR_SEARCH(bruteforce, COLOR_SEARCH_BRUTEFORCE)
  840. #define DITHERING_ENTRIES(color_search) { \
  841. set_frame_##color_search##_none, \
  842. set_frame_##color_search##_bayer, \
  843. set_frame_##color_search##_heckbert, \
  844. set_frame_##color_search##_floyd_steinberg, \
  845. set_frame_##color_search##_sierra2, \
  846. set_frame_##color_search##_sierra2_4a, \
  847. }
  848. static const set_frame_func set_frame_lut[NB_COLOR_SEARCHES][NB_DITHERING] = {
  849. DITHERING_ENTRIES(nns_iterative),
  850. DITHERING_ENTRIES(nns_recursive),
  851. DITHERING_ENTRIES(bruteforce),
  852. };
  853. static int dither_value(int p)
  854. {
  855. const int q = p ^ (p >> 3);
  856. return (p & 4) >> 2 | (q & 4) >> 1 \
  857. | (p & 2) << 1 | (q & 2) << 2 \
  858. | (p & 1) << 4 | (q & 1) << 5;
  859. }
  860. static av_cold int init(AVFilterContext *ctx)
  861. {
  862. PaletteUseContext *s = ctx->priv;
  863. s->dinput.repeatlast = 1; // only 1 frame in the palette
  864. s->dinput.process = load_apply_palette;
  865. s->set_frame = set_frame_lut[s->color_search_method][s->dither];
  866. if (s->dither == DITHERING_BAYER) {
  867. int i;
  868. const int delta = 1 << (5 - s->bayer_scale); // to avoid too much luma
  869. for (i = 0; i < FF_ARRAY_ELEMS(s->ordered_dither); i++)
  870. s->ordered_dither[i] = (dither_value(i) >> s->bayer_scale) - delta;
  871. }
  872. return 0;
  873. }
  874. static int request_frame(AVFilterLink *outlink)
  875. {
  876. PaletteUseContext *s = outlink->src->priv;
  877. return ff_dualinput_request_frame(&s->dinput, outlink);
  878. }
  879. static av_cold void uninit(AVFilterContext *ctx)
  880. {
  881. int i;
  882. PaletteUseContext *s = ctx->priv;
  883. ff_dualinput_uninit(&s->dinput);
  884. for (i = 0; i < CACHE_SIZE; i++)
  885. av_freep(&s->cache[i].entries);
  886. av_frame_free(&s->last_in);
  887. av_frame_free(&s->last_out);
  888. }
  889. static const AVFilterPad paletteuse_inputs[] = {
  890. {
  891. .name = "default",
  892. .type = AVMEDIA_TYPE_VIDEO,
  893. .filter_frame = filter_frame,
  894. .needs_writable = 1, // for error diffusal dithering
  895. },{
  896. .name = "palette",
  897. .type = AVMEDIA_TYPE_VIDEO,
  898. .config_props = config_input_palette,
  899. .filter_frame = filter_frame,
  900. },
  901. { NULL }
  902. };
  903. static const AVFilterPad paletteuse_outputs[] = {
  904. {
  905. .name = "default",
  906. .type = AVMEDIA_TYPE_VIDEO,
  907. .config_props = config_output,
  908. .request_frame = request_frame,
  909. },
  910. { NULL }
  911. };
  912. AVFilter ff_vf_paletteuse = {
  913. .name = "paletteuse",
  914. .description = NULL_IF_CONFIG_SMALL("Use a palette to downsample an input video stream."),
  915. .priv_size = sizeof(PaletteUseContext),
  916. .query_formats = query_formats,
  917. .init = init,
  918. .uninit = uninit,
  919. .inputs = paletteuse_inputs,
  920. .outputs = paletteuse_outputs,
  921. .priv_class = &paletteuse_class,
  922. };