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.

942 lines
35KB

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