vf_paletteuse.c 39 KB

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