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.

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