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.

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