You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1149 lines
42KB

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