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.

1153 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. uint32_t dstc;
  334. const int dstx = color_get(s, c, a, r, g, b, search_method);
  335. if (dstx < 0)
  336. return dstx;
  337. dstc = s->palette[dstx];
  338. *er = r - (dstc >> 16 & 0xff);
  339. *eg = g - (dstc >> 8 & 0xff);
  340. *eb = b - (dstc & 0xff);
  341. return dstx;
  342. }
  343. static av_always_inline int set_frame(PaletteUseContext *s, AVFrame *out, AVFrame *in,
  344. int x_start, int y_start, int w, int h,
  345. enum dithering_mode dither,
  346. const enum color_search_method search_method)
  347. {
  348. int x, y;
  349. const int src_linesize = in ->linesize[0] >> 2;
  350. const int dst_linesize = out->linesize[0];
  351. uint32_t *src = ((uint32_t *)in ->data[0]) + y_start*src_linesize;
  352. uint8_t *dst = out->data[0] + y_start*dst_linesize;
  353. w += x_start;
  354. h += y_start;
  355. for (y = y_start; y < h; y++) {
  356. for (x = x_start; x < w; x++) {
  357. int er, eg, eb;
  358. if (dither == DITHERING_BAYER) {
  359. const int d = s->ordered_dither[(y & 7)<<3 | (x & 7)];
  360. const uint8_t a8 = src[x] >> 24 & 0xff;
  361. const uint8_t r8 = src[x] >> 16 & 0xff;
  362. const uint8_t g8 = src[x] >> 8 & 0xff;
  363. const uint8_t b8 = src[x] & 0xff;
  364. const uint8_t r = av_clip_uint8(r8 + d);
  365. const uint8_t g = av_clip_uint8(g8 + d);
  366. const uint8_t b = av_clip_uint8(b8 + d);
  367. const int color = color_get(s, src[x], a8, r, g, b, search_method);
  368. if (color < 0)
  369. return color;
  370. dst[x] = color;
  371. } else if (dither == DITHERING_HECKBERT) {
  372. const int right = x < w - 1, down = y < h - 1;
  373. const int color = get_dst_color_err(s, src[x], &er, &eg, &eb, search_method);
  374. if (color < 0)
  375. return color;
  376. dst[x] = color;
  377. if (right) src[ x + 1] = dither_color(src[ x + 1], er, eg, eb, 3, 3);
  378. if ( down) src[src_linesize + x ] = dither_color(src[src_linesize + x ], er, eg, eb, 3, 3);
  379. if (right && down) src[src_linesize + x + 1] = dither_color(src[src_linesize + x + 1], er, eg, eb, 2, 3);
  380. } else if (dither == DITHERING_FLOYD_STEINBERG) {
  381. const int right = x < w - 1, down = y < h - 1, left = x > x_start;
  382. const int color = get_dst_color_err(s, src[x], &er, &eg, &eb, search_method);
  383. if (color < 0)
  384. return color;
  385. dst[x] = color;
  386. if (right) src[ x + 1] = dither_color(src[ x + 1], er, eg, eb, 7, 4);
  387. if (left && down) src[src_linesize + x - 1] = dither_color(src[src_linesize + x - 1], er, eg, eb, 3, 4);
  388. if ( down) src[src_linesize + x ] = dither_color(src[src_linesize + x ], er, eg, eb, 5, 4);
  389. if (right && down) src[src_linesize + x + 1] = dither_color(src[src_linesize + x + 1], er, eg, eb, 1, 4);
  390. } else if (dither == DITHERING_SIERRA2) {
  391. const int right = x < w - 1, down = y < h - 1, left = x > x_start;
  392. const int right2 = x < w - 2, left2 = x > x_start + 1;
  393. const int color = get_dst_color_err(s, src[x], &er, &eg, &eb, search_method);
  394. if (color < 0)
  395. return color;
  396. dst[x] = color;
  397. if (right) src[ x + 1] = dither_color(src[ x + 1], er, eg, eb, 4, 4);
  398. if (right2) src[ x + 2] = dither_color(src[ x + 2], er, eg, eb, 3, 4);
  399. if (down) {
  400. if (left2) src[ src_linesize + x - 2] = dither_color(src[ src_linesize + x - 2], er, eg, eb, 1, 4);
  401. if (left) src[ src_linesize + x - 1] = dither_color(src[ src_linesize + x - 1], er, eg, eb, 2, 4);
  402. if (1) src[ src_linesize + x ] = dither_color(src[ src_linesize + x ], er, eg, eb, 3, 4);
  403. if (right) src[ src_linesize + x + 1] = dither_color(src[ src_linesize + x + 1], er, eg, eb, 2, 4);
  404. if (right2) src[ src_linesize + x + 2] = dither_color(src[ src_linesize + x + 2], er, eg, eb, 1, 4);
  405. }
  406. } else if (dither == DITHERING_SIERRA2_4A) {
  407. const int right = x < w - 1, down = y < h - 1, left = x > x_start;
  408. const int color = get_dst_color_err(s, src[x], &er, &eg, &eb, search_method);
  409. if (color < 0)
  410. return color;
  411. dst[x] = color;
  412. if (right) src[ x + 1] = dither_color(src[ x + 1], er, eg, eb, 2, 2);
  413. if (left && down) src[src_linesize + x - 1] = dither_color(src[src_linesize + x - 1], er, eg, eb, 1, 2);
  414. if ( down) src[src_linesize + x ] = dither_color(src[src_linesize + x ], er, eg, eb, 1, 2);
  415. } else {
  416. const uint8_t a = src[x] >> 24 & 0xff;
  417. const uint8_t r = src[x] >> 16 & 0xff;
  418. const uint8_t g = src[x] >> 8 & 0xff;
  419. const uint8_t b = src[x] & 0xff;
  420. const int color = color_get(s, src[x], a, r, g, b, search_method);
  421. if (color < 0)
  422. return color;
  423. dst[x] = color;
  424. }
  425. }
  426. src += src_linesize;
  427. dst += dst_linesize;
  428. }
  429. return 0;
  430. }
  431. #define INDENT 4
  432. static void disp_node(AVBPrint *buf,
  433. const struct color_node *map,
  434. int parent_id, int node_id,
  435. int depth)
  436. {
  437. const struct color_node *node = &map[node_id];
  438. const uint32_t fontcolor = node->val[1] > 0x50 &&
  439. node->val[2] > 0x50 &&
  440. node->val[3] > 0x50 ? 0 : 0xffffff;
  441. const int rgb_comp = node->split - 1;
  442. av_bprintf(buf, "%*cnode%d ["
  443. "label=\"%c%02X%c%02X%c%02X%c\" "
  444. "fillcolor=\"#%02x%02x%02x\" "
  445. "fontcolor=\"#%06"PRIX32"\"]\n",
  446. depth*INDENT, ' ', node->palette_id,
  447. "[ "[rgb_comp], node->val[1],
  448. "][ "[rgb_comp], node->val[2],
  449. " ]["[rgb_comp], node->val[3],
  450. " ]"[rgb_comp],
  451. node->val[1], node->val[2], node->val[3],
  452. fontcolor);
  453. if (parent_id != -1)
  454. av_bprintf(buf, "%*cnode%d -> node%d\n", depth*INDENT, ' ',
  455. map[parent_id].palette_id, node->palette_id);
  456. if (node->left_id != -1) disp_node(buf, map, node_id, node->left_id, depth + 1);
  457. if (node->right_id != -1) disp_node(buf, map, node_id, node->right_id, depth + 1);
  458. }
  459. // debug_kdtree=kdtree.dot -> dot -Tpng kdtree.dot > kdtree.png
  460. static int disp_tree(const struct color_node *node, const char *fname)
  461. {
  462. AVBPrint buf;
  463. FILE *f = av_fopen_utf8(fname, "w");
  464. if (!f) {
  465. int ret = AVERROR(errno);
  466. av_log(NULL, AV_LOG_ERROR, "Cannot open file '%s' for writing: %s\n",
  467. fname, av_err2str(ret));
  468. return ret;
  469. }
  470. av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
  471. av_bprintf(&buf, "digraph {\n");
  472. av_bprintf(&buf, " node [style=filled fontsize=10 shape=box]\n");
  473. disp_node(&buf, node, -1, 0, 0);
  474. av_bprintf(&buf, "}\n");
  475. fwrite(buf.str, 1, buf.len, f);
  476. fclose(f);
  477. av_bprint_finalize(&buf, NULL);
  478. return 0;
  479. }
  480. static int debug_accuracy(const struct color_node *node, const uint32_t *palette, const int trans_thresh,
  481. const enum color_search_method search_method)
  482. {
  483. int r, g, b, ret = 0;
  484. for (r = 0; r < 256; r++) {
  485. for (g = 0; g < 256; g++) {
  486. for (b = 0; b < 256; b++) {
  487. const uint8_t argb[] = {0xff, r, g, b};
  488. const int r1 = COLORMAP_NEAREST(search_method, palette, node, argb, trans_thresh);
  489. const int r2 = colormap_nearest_bruteforce(palette, argb, trans_thresh);
  490. if (r1 != r2) {
  491. const uint32_t c1 = palette[r1];
  492. const uint32_t c2 = palette[r2];
  493. const uint8_t palargb1[] = { 0xff, c1>>16 & 0xff, c1>> 8 & 0xff, c1 & 0xff };
  494. const uint8_t palargb2[] = { 0xff, c2>>16 & 0xff, c2>> 8 & 0xff, c2 & 0xff };
  495. const int d1 = diff(palargb1, argb, trans_thresh);
  496. const int d2 = diff(palargb2, argb, trans_thresh);
  497. if (d1 != d2) {
  498. av_log(NULL, AV_LOG_ERROR,
  499. "/!\\ %02X%02X%02X: %d ! %d (%06"PRIX32" ! %06"PRIX32") / dist: %d ! %d\n",
  500. r, g, b, r1, r2, c1 & 0xffffff, c2 & 0xffffff, d1, d2);
  501. ret = 1;
  502. }
  503. }
  504. }
  505. }
  506. }
  507. return ret;
  508. }
  509. struct color {
  510. uint32_t value;
  511. uint8_t pal_id;
  512. };
  513. struct color_rect {
  514. uint8_t min[3];
  515. uint8_t max[3];
  516. };
  517. typedef int (*cmp_func)(const void *, const void *);
  518. #define DECLARE_CMP_FUNC(name, pos) \
  519. static int cmp_##name(const void *pa, const void *pb) \
  520. { \
  521. const struct color *a = pa; \
  522. const struct color *b = pb; \
  523. return (a->value >> (8 * (3 - (pos))) & 0xff) \
  524. - (b->value >> (8 * (3 - (pos))) & 0xff); \
  525. }
  526. DECLARE_CMP_FUNC(a, 0)
  527. DECLARE_CMP_FUNC(r, 1)
  528. DECLARE_CMP_FUNC(g, 2)
  529. DECLARE_CMP_FUNC(b, 3)
  530. static const cmp_func cmp_funcs[] = {cmp_a, cmp_r, cmp_g, cmp_b};
  531. static int get_next_color(const uint8_t *color_used, const uint32_t *palette,
  532. const int trans_thresh,
  533. int *component, const struct color_rect *box)
  534. {
  535. int wr, wg, wb;
  536. int i, longest = 0;
  537. unsigned nb_color = 0;
  538. struct color_rect ranges;
  539. struct color tmp_pal[256];
  540. cmp_func cmpf;
  541. ranges.min[0] = ranges.min[1] = ranges.min[2] = 0xff;
  542. ranges.max[0] = ranges.max[1] = ranges.max[2] = 0x00;
  543. for (i = 0; i < AVPALETTE_COUNT; i++) {
  544. const uint32_t c = palette[i];
  545. const uint8_t a = c >> 24 & 0xff;
  546. const uint8_t r = c >> 16 & 0xff;
  547. const uint8_t g = c >> 8 & 0xff;
  548. const uint8_t b = c & 0xff;
  549. if (a < trans_thresh) {
  550. continue;
  551. }
  552. if (color_used[i] || (a != 0xff) ||
  553. r < box->min[0] || g < box->min[1] || b < box->min[2] ||
  554. r > box->max[0] || g > box->max[1] || b > box->max[2])
  555. continue;
  556. if (r < ranges.min[0]) ranges.min[0] = r;
  557. if (g < ranges.min[1]) ranges.min[1] = g;
  558. if (b < ranges.min[2]) ranges.min[2] = b;
  559. if (r > ranges.max[0]) ranges.max[0] = r;
  560. if (g > ranges.max[1]) ranges.max[1] = g;
  561. if (b > ranges.max[2]) ranges.max[2] = b;
  562. tmp_pal[nb_color].value = c;
  563. tmp_pal[nb_color].pal_id = i;
  564. nb_color++;
  565. }
  566. if (!nb_color)
  567. return -1;
  568. /* define longest axis that will be the split component */
  569. wr = ranges.max[0] - ranges.min[0];
  570. wg = ranges.max[1] - ranges.min[1];
  571. wb = ranges.max[2] - ranges.min[2];
  572. if (wr >= wg && wr >= wb) longest = 1;
  573. if (wg >= wr && wg >= wb) longest = 2;
  574. if (wb >= wr && wb >= wg) longest = 3;
  575. cmpf = cmp_funcs[longest];
  576. *component = longest;
  577. /* sort along this axis to get median */
  578. AV_QSORT(tmp_pal, nb_color, struct color, cmpf);
  579. return tmp_pal[nb_color >> 1].pal_id;
  580. }
  581. static int colormap_insert(struct color_node *map,
  582. uint8_t *color_used,
  583. int *nb_used,
  584. const uint32_t *palette,
  585. const int trans_thresh,
  586. const struct color_rect *box)
  587. {
  588. uint32_t c;
  589. int component, cur_id;
  590. int node_left_id = -1, node_right_id = -1;
  591. struct color_node *node;
  592. struct color_rect box1, box2;
  593. const int pal_id = get_next_color(color_used, palette, trans_thresh, &component, box);
  594. if (pal_id < 0)
  595. return -1;
  596. /* create new node with that color */
  597. cur_id = (*nb_used)++;
  598. c = palette[pal_id];
  599. node = &map[cur_id];
  600. node->split = component;
  601. node->palette_id = pal_id;
  602. node->val[0] = c>>24 & 0xff;
  603. node->val[1] = c>>16 & 0xff;
  604. node->val[2] = c>> 8 & 0xff;
  605. node->val[3] = c & 0xff;
  606. color_used[pal_id] = 1;
  607. /* get the two boxes this node creates */
  608. box1 = box2 = *box;
  609. box1.max[component-1] = node->val[component];
  610. box2.min[component-1] = node->val[component] + 1;
  611. node_left_id = colormap_insert(map, color_used, nb_used, palette, trans_thresh, &box1);
  612. if (box2.min[component-1] <= box2.max[component-1])
  613. node_right_id = colormap_insert(map, color_used, nb_used, palette, trans_thresh, &box2);
  614. node->left_id = node_left_id;
  615. node->right_id = node_right_id;
  616. return cur_id;
  617. }
  618. static int cmp_pal_entry(const void *a, const void *b)
  619. {
  620. const int c1 = *(const uint32_t *)a & 0xffffff;
  621. const int c2 = *(const uint32_t *)b & 0xffffff;
  622. return c1 - c2;
  623. }
  624. static void load_colormap(PaletteUseContext *s)
  625. {
  626. int i, nb_used = 0;
  627. uint8_t color_used[AVPALETTE_COUNT] = {0};
  628. uint32_t last_color = 0;
  629. struct color_rect box;
  630. /* disable transparent colors and dups */
  631. qsort(s->palette, AVPALETTE_COUNT, sizeof(*s->palette), cmp_pal_entry);
  632. // update transparency index:
  633. if (s->transparency_index >= 0) {
  634. for (i = 0; i < AVPALETTE_COUNT; i++) {
  635. if ((s->palette[i]>>24 & 0xff) == 0) {
  636. s->transparency_index = i; // we are assuming at most one transparent color in palette
  637. break;
  638. }
  639. }
  640. }
  641. for (i = 0; i < AVPALETTE_COUNT; i++) {
  642. const uint32_t c = s->palette[i];
  643. if (i != 0 && c == last_color) {
  644. color_used[i] = 1;
  645. continue;
  646. }
  647. last_color = c;
  648. if (c >> 24 < s->trans_thresh) {
  649. color_used[i] = 1; // ignore transparent color(s)
  650. continue;
  651. }
  652. }
  653. box.min[0] = box.min[1] = box.min[2] = 0x00;
  654. box.max[0] = box.max[1] = box.max[2] = 0xff;
  655. colormap_insert(s->map, color_used, &nb_used, s->palette, s->trans_thresh, &box);
  656. if (s->dot_filename)
  657. disp_tree(s->map, s->dot_filename);
  658. if (s->debug_accuracy) {
  659. if (!debug_accuracy(s->map, s->palette, s->trans_thresh, s->color_search_method))
  660. av_log(NULL, AV_LOG_INFO, "Accuracy check passed\n");
  661. }
  662. }
  663. static void debug_mean_error(PaletteUseContext *s, const AVFrame *in1,
  664. const AVFrame *in2, int frame_count)
  665. {
  666. int x, y;
  667. const uint32_t *palette = s->palette;
  668. uint32_t *src1 = (uint32_t *)in1->data[0];
  669. uint8_t *src2 = in2->data[0];
  670. const int src1_linesize = in1->linesize[0] >> 2;
  671. const int src2_linesize = in2->linesize[0];
  672. const float div = in1->width * in1->height * 3;
  673. unsigned mean_err = 0;
  674. for (y = 0; y < in1->height; y++) {
  675. for (x = 0; x < in1->width; x++) {
  676. const uint32_t c1 = src1[x];
  677. const uint32_t c2 = palette[src2[x]];
  678. const uint8_t argb1[] = {0xff, c1 >> 16 & 0xff, c1 >> 8 & 0xff, c1 & 0xff};
  679. const uint8_t argb2[] = {0xff, c2 >> 16 & 0xff, c2 >> 8 & 0xff, c2 & 0xff};
  680. mean_err += diff(argb1, argb2, s->trans_thresh);
  681. }
  682. src1 += src1_linesize;
  683. src2 += src2_linesize;
  684. }
  685. s->total_mean_err += mean_err;
  686. av_log(NULL, AV_LOG_INFO, "MEP:%.3f TotalMEP:%.3f\n",
  687. mean_err / div, s->total_mean_err / (div * frame_count));
  688. }
  689. static void set_processing_window(enum diff_mode diff_mode,
  690. const AVFrame *prv_src, const AVFrame *cur_src,
  691. const AVFrame *prv_dst, AVFrame *cur_dst,
  692. int *xp, int *yp, int *wp, int *hp)
  693. {
  694. int x_start = 0, y_start = 0;
  695. int width = cur_src->width;
  696. int height = cur_src->height;
  697. if (prv_src && diff_mode == DIFF_MODE_RECTANGLE) {
  698. int y;
  699. int x_end = cur_src->width - 1,
  700. y_end = cur_src->height - 1;
  701. const uint32_t *prv_srcp = (const uint32_t *)prv_src->data[0];
  702. const uint32_t *cur_srcp = (const uint32_t *)cur_src->data[0];
  703. const uint8_t *prv_dstp = prv_dst->data[0];
  704. uint8_t *cur_dstp = cur_dst->data[0];
  705. const int prv_src_linesize = prv_src->linesize[0] >> 2;
  706. const int cur_src_linesize = cur_src->linesize[0] >> 2;
  707. const int prv_dst_linesize = prv_dst->linesize[0];
  708. const int cur_dst_linesize = cur_dst->linesize[0];
  709. /* skip common lines */
  710. while (y_start < y_end && !memcmp(prv_srcp + y_start*prv_src_linesize,
  711. cur_srcp + y_start*cur_src_linesize,
  712. cur_src->width * 4)) {
  713. memcpy(cur_dstp + y_start*cur_dst_linesize,
  714. prv_dstp + y_start*prv_dst_linesize,
  715. cur_dst->width);
  716. y_start++;
  717. }
  718. while (y_end > y_start && !memcmp(prv_srcp + y_end*prv_src_linesize,
  719. cur_srcp + y_end*cur_src_linesize,
  720. cur_src->width * 4)) {
  721. memcpy(cur_dstp + y_end*cur_dst_linesize,
  722. prv_dstp + y_end*prv_dst_linesize,
  723. cur_dst->width);
  724. y_end--;
  725. }
  726. height = y_end + 1 - y_start;
  727. /* skip common columns */
  728. while (x_start < x_end) {
  729. int same_column = 1;
  730. for (y = y_start; y <= y_end; y++) {
  731. if (prv_srcp[y*prv_src_linesize + x_start] != cur_srcp[y*cur_src_linesize + x_start]) {
  732. same_column = 0;
  733. break;
  734. }
  735. }
  736. if (!same_column)
  737. break;
  738. x_start++;
  739. }
  740. while (x_end > x_start) {
  741. int same_column = 1;
  742. for (y = y_start; y <= y_end; y++) {
  743. if (prv_srcp[y*prv_src_linesize + x_end] != cur_srcp[y*cur_src_linesize + x_end]) {
  744. same_column = 0;
  745. break;
  746. }
  747. }
  748. if (!same_column)
  749. break;
  750. x_end--;
  751. }
  752. width = x_end + 1 - x_start;
  753. if (x_start) {
  754. for (y = y_start; y <= y_end; y++)
  755. memcpy(cur_dstp + y*cur_dst_linesize,
  756. prv_dstp + y*prv_dst_linesize, x_start);
  757. }
  758. if (x_end != cur_src->width - 1) {
  759. const int copy_len = cur_src->width - 1 - x_end;
  760. for (y = y_start; y <= y_end; y++)
  761. memcpy(cur_dstp + y*cur_dst_linesize + x_end + 1,
  762. prv_dstp + y*prv_dst_linesize + x_end + 1,
  763. copy_len);
  764. }
  765. }
  766. *xp = x_start;
  767. *yp = y_start;
  768. *wp = width;
  769. *hp = height;
  770. }
  771. static int apply_palette(AVFilterLink *inlink, AVFrame *in, AVFrame **outf)
  772. {
  773. int x, y, w, h, ret;
  774. AVFilterContext *ctx = inlink->dst;
  775. PaletteUseContext *s = ctx->priv;
  776. AVFilterLink *outlink = inlink->dst->outputs[0];
  777. AVFrame *out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  778. if (!out) {
  779. av_frame_free(&in);
  780. *outf = NULL;
  781. return AVERROR(ENOMEM);
  782. }
  783. av_frame_copy_props(out, in);
  784. set_processing_window(s->diff_mode, s->last_in, in,
  785. s->last_out, out, &x, &y, &w, &h);
  786. av_frame_free(&s->last_in);
  787. av_frame_free(&s->last_out);
  788. s->last_in = av_frame_clone(in);
  789. s->last_out = av_frame_clone(out);
  790. if (!s->last_in || !s->last_out ||
  791. av_frame_make_writable(s->last_in) < 0) {
  792. av_frame_free(&in);
  793. av_frame_free(&out);
  794. *outf = NULL;
  795. return AVERROR(ENOMEM);
  796. }
  797. ff_dlog(ctx, "%dx%d rect: (%d;%d) -> (%d,%d) [area:%dx%d]\n",
  798. w, h, x, y, x+w, y+h, in->width, in->height);
  799. ret = s->set_frame(s, out, in, x, y, w, h);
  800. if (ret < 0) {
  801. av_frame_free(&out);
  802. *outf = NULL;
  803. return ret;
  804. }
  805. memcpy(out->data[1], s->palette, AVPALETTE_SIZE);
  806. if (s->calc_mean_err)
  807. debug_mean_error(s, in, out, inlink->frame_count_out);
  808. av_frame_free(&in);
  809. *outf = out;
  810. return 0;
  811. }
  812. static int config_output(AVFilterLink *outlink)
  813. {
  814. int ret;
  815. AVFilterContext *ctx = outlink->src;
  816. PaletteUseContext *s = ctx->priv;
  817. ret = ff_framesync_init_dualinput(&s->fs, ctx);
  818. if (ret < 0)
  819. return ret;
  820. s->fs.opt_repeatlast = 1; // only 1 frame in the palette
  821. s->fs.in[1].before = s->fs.in[1].after = EXT_INFINITY;
  822. s->fs.on_event = load_apply_palette;
  823. outlink->w = ctx->inputs[0]->w;
  824. outlink->h = ctx->inputs[0]->h;
  825. outlink->time_base = ctx->inputs[0]->time_base;
  826. if ((ret = ff_framesync_configure(&s->fs)) < 0)
  827. return ret;
  828. return 0;
  829. }
  830. static int config_input_palette(AVFilterLink *inlink)
  831. {
  832. AVFilterContext *ctx = inlink->dst;
  833. if (inlink->w * inlink->h != AVPALETTE_COUNT) {
  834. av_log(ctx, AV_LOG_ERROR,
  835. "Palette input must contain exactly %d pixels. "
  836. "Specified input has %dx%d=%d pixels\n",
  837. AVPALETTE_COUNT, inlink->w, inlink->h,
  838. inlink->w * inlink->h);
  839. return AVERROR(EINVAL);
  840. }
  841. return 0;
  842. }
  843. static void load_palette(PaletteUseContext *s, const AVFrame *palette_frame)
  844. {
  845. int i, x, y;
  846. const uint32_t *p = (const uint32_t *)palette_frame->data[0];
  847. const int p_linesize = palette_frame->linesize[0] >> 2;
  848. s->transparency_index = -1;
  849. if (s->new) {
  850. memset(s->palette, 0, sizeof(s->palette));
  851. memset(s->map, 0, sizeof(s->map));
  852. for (i = 0; i < CACHE_SIZE; i++)
  853. av_freep(&s->cache[i].entries);
  854. memset(s->cache, 0, sizeof(s->cache));
  855. }
  856. i = 0;
  857. for (y = 0; y < palette_frame->height; y++) {
  858. for (x = 0; x < palette_frame->width; x++) {
  859. s->palette[i] = p[x];
  860. if (p[x]>>24 < s->trans_thresh) {
  861. s->transparency_index = i; // we are assuming at most one transparent color in palette
  862. }
  863. i++;
  864. }
  865. p += p_linesize;
  866. }
  867. load_colormap(s);
  868. if (!s->new)
  869. s->palette_loaded = 1;
  870. }
  871. static int load_apply_palette(FFFrameSync *fs)
  872. {
  873. AVFilterContext *ctx = fs->parent;
  874. AVFilterLink *inlink = ctx->inputs[0];
  875. PaletteUseContext *s = ctx->priv;
  876. AVFrame *master, *second, *out = NULL;
  877. int ret;
  878. // writable for error diffusal dithering
  879. ret = ff_framesync_dualinput_get_writable(fs, &master, &second);
  880. if (ret < 0)
  881. return ret;
  882. if (!master || !second) {
  883. ret = AVERROR_BUG;
  884. goto error;
  885. }
  886. if (!s->palette_loaded) {
  887. load_palette(s, second);
  888. }
  889. ret = apply_palette(inlink, master, &out);
  890. if (ret < 0)
  891. goto error;
  892. return ff_filter_frame(ctx->outputs[0], out);
  893. error:
  894. av_frame_free(&master);
  895. return ret;
  896. }
  897. #define DEFINE_SET_FRAME(color_search, name, value) \
  898. static int set_frame_##name(PaletteUseContext *s, AVFrame *out, AVFrame *in, \
  899. int x_start, int y_start, int w, int h) \
  900. { \
  901. return set_frame(s, out, in, x_start, y_start, w, h, value, color_search); \
  902. }
  903. #define DEFINE_SET_FRAME_COLOR_SEARCH(color_search, color_search_macro) \
  904. DEFINE_SET_FRAME(color_search_macro, color_search##_##none, DITHERING_NONE) \
  905. DEFINE_SET_FRAME(color_search_macro, color_search##_##bayer, DITHERING_BAYER) \
  906. DEFINE_SET_FRAME(color_search_macro, color_search##_##heckbert, DITHERING_HECKBERT) \
  907. DEFINE_SET_FRAME(color_search_macro, color_search##_##floyd_steinberg, DITHERING_FLOYD_STEINBERG) \
  908. DEFINE_SET_FRAME(color_search_macro, color_search##_##sierra2, DITHERING_SIERRA2) \
  909. DEFINE_SET_FRAME(color_search_macro, color_search##_##sierra2_4a, DITHERING_SIERRA2_4A) \
  910. DEFINE_SET_FRAME_COLOR_SEARCH(nns_iterative, COLOR_SEARCH_NNS_ITERATIVE)
  911. DEFINE_SET_FRAME_COLOR_SEARCH(nns_recursive, COLOR_SEARCH_NNS_RECURSIVE)
  912. DEFINE_SET_FRAME_COLOR_SEARCH(bruteforce, COLOR_SEARCH_BRUTEFORCE)
  913. #define DITHERING_ENTRIES(color_search) { \
  914. set_frame_##color_search##_none, \
  915. set_frame_##color_search##_bayer, \
  916. set_frame_##color_search##_heckbert, \
  917. set_frame_##color_search##_floyd_steinberg, \
  918. set_frame_##color_search##_sierra2, \
  919. set_frame_##color_search##_sierra2_4a, \
  920. }
  921. static const set_frame_func set_frame_lut[NB_COLOR_SEARCHES][NB_DITHERING] = {
  922. DITHERING_ENTRIES(nns_iterative),
  923. DITHERING_ENTRIES(nns_recursive),
  924. DITHERING_ENTRIES(bruteforce),
  925. };
  926. static int dither_value(int p)
  927. {
  928. const int q = p ^ (p >> 3);
  929. return (p & 4) >> 2 | (q & 4) >> 1 \
  930. | (p & 2) << 1 | (q & 2) << 2 \
  931. | (p & 1) << 4 | (q & 1) << 5;
  932. }
  933. static av_cold int init(AVFilterContext *ctx)
  934. {
  935. PaletteUseContext *s = ctx->priv;
  936. s->set_frame = set_frame_lut[s->color_search_method][s->dither];
  937. if (s->dither == DITHERING_BAYER) {
  938. int i;
  939. const int delta = 1 << (5 - s->bayer_scale); // to avoid too much luma
  940. for (i = 0; i < FF_ARRAY_ELEMS(s->ordered_dither); i++)
  941. s->ordered_dither[i] = (dither_value(i) >> s->bayer_scale) - delta;
  942. }
  943. return 0;
  944. }
  945. static int activate(AVFilterContext *ctx)
  946. {
  947. PaletteUseContext *s = ctx->priv;
  948. return ff_framesync_activate(&s->fs);
  949. }
  950. static av_cold void uninit(AVFilterContext *ctx)
  951. {
  952. int i;
  953. PaletteUseContext *s = ctx->priv;
  954. ff_framesync_uninit(&s->fs);
  955. for (i = 0; i < CACHE_SIZE; i++)
  956. av_freep(&s->cache[i].entries);
  957. av_frame_free(&s->last_in);
  958. av_frame_free(&s->last_out);
  959. }
  960. static const AVFilterPad paletteuse_inputs[] = {
  961. {
  962. .name = "default",
  963. .type = AVMEDIA_TYPE_VIDEO,
  964. },{
  965. .name = "palette",
  966. .type = AVMEDIA_TYPE_VIDEO,
  967. .config_props = config_input_palette,
  968. },
  969. { NULL }
  970. };
  971. static const AVFilterPad paletteuse_outputs[] = {
  972. {
  973. .name = "default",
  974. .type = AVMEDIA_TYPE_VIDEO,
  975. .config_props = config_output,
  976. },
  977. { NULL }
  978. };
  979. AVFilter ff_vf_paletteuse = {
  980. .name = "paletteuse",
  981. .description = NULL_IF_CONFIG_SMALL("Use a palette to downsample an input video stream."),
  982. .priv_size = sizeof(PaletteUseContext),
  983. .query_formats = query_formats,
  984. .init = init,
  985. .uninit = uninit,
  986. .activate = activate,
  987. .inputs = paletteuse_inputs,
  988. .outputs = paletteuse_outputs,
  989. .priv_class = &paletteuse_class,
  990. };