FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
vf_paletteuse.c
Go to the documentation of this file.
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 /**
22  * @file
23  * Use a palette to downsample an input video stream.
24  */
25 
26 #include "libavutil/bprint.h"
27 #include "libavutil/internal.h"
28 #include "libavutil/opt.h"
29 #include "libavutil/qsort.h"
30 #include "dualinput.h"
31 #include "avfilter.h"
32 
41 };
42 
48 };
49 
50 enum diff_mode {
54 };
55 
56 struct color_node {
59  int split;
61 };
62 
63 #define NBITS 5
64 #define CACHE_SIZE (1<<(3*NBITS))
65 
66 struct cached_color {
67  uint32_t color;
69 };
70 
71 struct cache_node {
74 };
75 
76 struct PaletteUseContext;
77 
78 typedef int (*set_frame_func)(struct PaletteUseContext *s, AVFrame *out, AVFrame *in,
79  int x_start, int y_start, int width, int height);
80 
81 typedef struct PaletteUseContext {
82  const AVClass *class;
84  struct cache_node cache[CACHE_SIZE]; /* lookup cache */
85  struct color_node map[AVPALETTE_COUNT]; /* 3D-Tree (KD-Tree with K=3) for reverse colormap */
88  int dither;
91  int ordered_dither[8*8];
92  int diff_mode;
95 
96  /* debug options */
97  char *dot_filename;
100  uint64_t total_mean_err;
103 
104 #define OFFSET(x) offsetof(PaletteUseContext, x)
105 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
106 static const AVOption paletteuse_options[] = {
107  { "dither", "select dithering mode", OFFSET(dither), AV_OPT_TYPE_INT, {.i64=DITHERING_SIERRA2_4A}, 0, NB_DITHERING-1, FLAGS, "dithering_mode" },
108  { "bayer", "ordered 8x8 bayer dithering (deterministic)", 0, AV_OPT_TYPE_CONST, {.i64=DITHERING_BAYER}, INT_MIN, INT_MAX, FLAGS, "dithering_mode" },
109  { "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" },
110  { "floyd_steinberg", "Floyd and Steingberg dithering (error diffusion)", 0, AV_OPT_TYPE_CONST, {.i64=DITHERING_FLOYD_STEINBERG}, INT_MIN, INT_MAX, FLAGS, "dithering_mode" },
111  { "sierra2", "Frankie Sierra dithering v2 (error diffusion)", 0, AV_OPT_TYPE_CONST, {.i64=DITHERING_SIERRA2}, INT_MIN, INT_MAX, FLAGS, "dithering_mode" },
112  { "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" },
113  { "bayer_scale", "set scale for bayer dithering", OFFSET(bayer_scale), AV_OPT_TYPE_INT, {.i64=2}, 0, 5, FLAGS },
114  { "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" },
115  { "rectangle", "process smallest different rectangle", 0, AV_OPT_TYPE_CONST, {.i64=DIFF_MODE_RECTANGLE}, INT_MIN, INT_MAX, FLAGS, "diff_mode" },
116 
117  /* following are the debug options, not part of the official API */
118  { "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 },
119  { "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" },
120  { "nns_iterative", "iterative search", 0, AV_OPT_TYPE_CONST, {.i64=COLOR_SEARCH_NNS_ITERATIVE}, INT_MIN, INT_MAX, FLAGS, "search" },
121  { "nns_recursive", "recursive search", 0, AV_OPT_TYPE_CONST, {.i64=COLOR_SEARCH_NNS_RECURSIVE}, INT_MIN, INT_MAX, FLAGS, "search" },
122  { "bruteforce", "brute-force into the palette", 0, AV_OPT_TYPE_CONST, {.i64=COLOR_SEARCH_BRUTEFORCE}, INT_MIN, INT_MAX, FLAGS, "search" },
123  { "mean_err", "compute and print mean error", OFFSET(calc_mean_err), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
124  { "debug_accuracy", "test color search accuracy", OFFSET(debug_accuracy), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
125  { NULL }
126 };
127 
128 AVFILTER_DEFINE_CLASS(paletteuse);
129 
131 {
132  static const enum AVPixelFormat in_fmts[] = {AV_PIX_FMT_RGB32, AV_PIX_FMT_NONE};
133  static const enum AVPixelFormat inpal_fmts[] = {AV_PIX_FMT_RGB32, AV_PIX_FMT_NONE};
134  static const enum AVPixelFormat out_fmts[] = {AV_PIX_FMT_PAL8, AV_PIX_FMT_NONE};
135  int ret;
137  AVFilterFormats *inpal = ff_make_format_list(inpal_fmts);
139  if (!in || !inpal || !out) {
140  av_freep(&in);
141  av_freep(&inpal);
142  av_freep(&out);
143  return AVERROR(ENOMEM);
144  }
145  if ((ret = ff_formats_ref(in , &ctx->inputs[0]->out_formats)) < 0 ||
146  (ret = ff_formats_ref(inpal, &ctx->inputs[1]->out_formats)) < 0 ||
147  (ret = ff_formats_ref(out , &ctx->outputs[0]->in_formats)) < 0)
148  return ret;
149  return 0;
150 }
151 
152 static av_always_inline int dither_color(uint32_t px, int er, int eg, int eb, int scale, int shift)
153 {
154  return av_clip_uint8((px >> 16 & 0xff) + ((er * scale) / (1<<shift))) << 16
155  | av_clip_uint8((px >> 8 & 0xff) + ((eg * scale) / (1<<shift))) << 8
156  | av_clip_uint8((px & 0xff) + ((eb * scale) / (1<<shift)));
157 }
158 
159 static av_always_inline int diff(const uint8_t *c1, const uint8_t *c2)
160 {
161  // XXX: try L*a*b with CIE76 (dL*dL + da*da + db*db)
162  const int dr = c1[0] - c2[0];
163  const int dg = c1[1] - c2[1];
164  const int db = c1[2] - c2[2];
165  return dr*dr + dg*dg + db*db;
166 }
167 
169 {
170  int i, pal_id = -1, min_dist = INT_MAX;
171 
172  for (i = 0; i < AVPALETTE_COUNT; i++) {
173  const uint32_t c = palette[i];
174 
175  if ((c & 0xff000000) == 0xff000000) { // ignore transparent entry
176  const uint8_t palrgb[] = {
177  palette[i]>>16 & 0xff,
178  palette[i]>> 8 & 0xff,
179  palette[i] & 0xff,
180  };
181  const int d = diff(palrgb, rgb);
182  if (d < min_dist) {
183  pal_id = i;
184  min_dist = d;
185  }
186  }
187  }
188  return pal_id;
189 }
190 
191 /* Recursive form, simpler but a bit slower. Kept for reference. */
193  int node_pos;
194  int dist_sqd;
195 };
196 
197 static void colormap_nearest_node(const struct color_node *map,
198  const int node_pos,
199  const uint8_t *target,
200  struct nearest_color *nearest)
201 {
202  const struct color_node *kd = map + node_pos;
203  const int s = kd->split;
204  int dx, nearer_kd_id, further_kd_id;
205  const uint8_t *current = kd->val;
206  const int current_to_target = diff(target, current);
207 
208  if (current_to_target < nearest->dist_sqd) {
209  nearest->node_pos = node_pos;
210  nearest->dist_sqd = current_to_target;
211  }
212 
213  if (kd->left_id != -1 || kd->right_id != -1) {
214  dx = target[s] - current[s];
215 
216  if (dx <= 0) nearer_kd_id = kd->left_id, further_kd_id = kd->right_id;
217  else nearer_kd_id = kd->right_id, further_kd_id = kd->left_id;
218 
219  if (nearer_kd_id != -1)
220  colormap_nearest_node(map, nearer_kd_id, target, nearest);
221 
222  if (further_kd_id != -1 && dx*dx < nearest->dist_sqd)
223  colormap_nearest_node(map, further_kd_id, target, nearest);
224  }
225 }
226 
228 {
229  struct nearest_color res = {.dist_sqd = INT_MAX, .node_pos = -1};
230  colormap_nearest_node(node, 0, rgb, &res);
231  return node[res.node_pos].palette_id;
232 }
233 
234 struct stack_node {
235  int color_id;
236  int dx2;
237 };
238 
239 static av_always_inline uint8_t colormap_nearest_iterative(const struct color_node *root, const uint8_t *target)
240 {
241  int pos = 0, best_node_id = -1, best_dist = INT_MAX, cur_color_id = 0;
242  struct stack_node nodes[16];
243  struct stack_node *node = &nodes[0];
244 
245  for (;;) {
246 
247  const struct color_node *kd = &root[cur_color_id];
248  const uint8_t *current = kd->val;
249  const int current_to_target = diff(target, current);
250 
251  /* Compare current color node to the target and update our best node if
252  * it's actually better. */
253  if (current_to_target < best_dist) {
254  best_node_id = cur_color_id;
255  if (!current_to_target)
256  goto end; // exact match, we can return immediately
257  best_dist = current_to_target;
258  }
259 
260  /* Check if it's not a leaf */
261  if (kd->left_id != -1 || kd->right_id != -1) {
262  const int split = kd->split;
263  const int dx = target[split] - current[split];
264  int nearer_kd_id, further_kd_id;
265 
266  /* Define which side is the most interesting. */
267  if (dx <= 0) nearer_kd_id = kd->left_id, further_kd_id = kd->right_id;
268  else nearer_kd_id = kd->right_id, further_kd_id = kd->left_id;
269 
270  if (nearer_kd_id != -1) {
271  if (further_kd_id != -1) {
272  /* Here, both paths are defined, so we push a state for
273  * when we are going back. */
274  node->color_id = further_kd_id;
275  node->dx2 = dx*dx;
276  pos++;
277  node++;
278  }
279  /* We can now update current color with the most probable path
280  * (no need to create a state since there is nothing to save
281  * anymore). */
282  cur_color_id = nearer_kd_id;
283  continue;
284  } else if (dx*dx < best_dist) {
285  /* The nearest path isn't available, so there is only one path
286  * possible and it's the least probable. We enter it only if the
287  * distance from the current point to the hyper rectangle is
288  * less than our best distance. */
289  cur_color_id = further_kd_id;
290  continue;
291  }
292  }
293 
294  /* Unstack as much as we can, typically as long as the least probable
295  * branch aren't actually probable. */
296  do {
297  if (--pos < 0)
298  goto end;
299  node--;
300  } while (node->dx2 >= best_dist);
301 
302  /* We got a node where the least probable branch might actually contain
303  * a relevant color. */
304  cur_color_id = node->color_id;
305  }
306 
307 end:
308  return root[best_node_id].palette_id;
309 }
310 
311 #define COLORMAP_NEAREST(search, palette, root, target) \
312  search == COLOR_SEARCH_NNS_ITERATIVE ? colormap_nearest_iterative(root, target) : \
313  search == COLOR_SEARCH_NNS_RECURSIVE ? colormap_nearest_recursive(root, target) : \
314  colormap_nearest_bruteforce(palette, target)
315 
316 /**
317  * Check if the requested color is in the cache already. If not, find it in the
318  * color tree and cache it.
319  * Note: r, g, and b are the component of c but are passed as well to avoid
320  * recomputing them (they are generally computed by the caller for other uses).
321  */
322 static av_always_inline int color_get(struct cache_node *cache, uint32_t color,
324  const struct color_node *map,
325  const uint32_t *palette,
326  const enum color_search_method search_method)
327 {
328  int i;
329  const uint8_t rgb[] = {r, g, b};
330  const uint8_t rhash = r & ((1<<NBITS)-1);
331  const uint8_t ghash = g & ((1<<NBITS)-1);
332  const uint8_t bhash = b & ((1<<NBITS)-1);
333  const unsigned hash = rhash<<(NBITS*2) | ghash<<NBITS | bhash;
334  struct cache_node *node = &cache[hash];
335  struct cached_color *e;
336 
337  for (i = 0; i < node->nb_entries; i++) {
338  e = &node->entries[i];
339  if (e->color == color)
340  return e->pal_entry;
341  }
342 
343  e = av_dynarray2_add((void**)&node->entries, &node->nb_entries,
344  sizeof(*node->entries), NULL);
345  if (!e)
346  return AVERROR(ENOMEM);
347  e->color = color;
348  e->pal_entry = COLORMAP_NEAREST(search_method, palette, map, rgb);
349  return e->pal_entry;
350 }
351 
353  uint32_t c, const struct color_node *map,
354  const uint32_t *palette,
355  int *er, int *eg, int *eb,
356  const enum color_search_method search_method)
357 {
358  const uint8_t r = c >> 16 & 0xff;
359  const uint8_t g = c >> 8 & 0xff;
360  const uint8_t b = c & 0xff;
361  const int dstx = color_get(cache, c, r, g, b, map, palette, search_method);
362  const uint32_t dstc = palette[dstx];
363  *er = r - (dstc >> 16 & 0xff);
364  *eg = g - (dstc >> 8 & 0xff);
365  *eb = b - (dstc & 0xff);
366  return dstx;
367 }
368 
370  int x_start, int y_start, int w, int h,
371  enum dithering_mode dither,
372  const enum color_search_method search_method)
373 {
374  int x, y;
375  const struct color_node *map = s->map;
376  struct cache_node *cache = s->cache;
377  const uint32_t *palette = s->palette;
378  const int src_linesize = in ->linesize[0] >> 2;
379  const int dst_linesize = out->linesize[0];
380  uint32_t *src = ((uint32_t *)in ->data[0]) + y_start*src_linesize;
381  uint8_t *dst = out->data[0] + y_start*dst_linesize;
382 
383  w += x_start;
384  h += y_start;
385 
386  for (y = y_start; y < h; y++) {
387  for (x = x_start; x < w; x++) {
388  int er, eg, eb;
389 
390  if (dither == DITHERING_BAYER) {
391  const int d = s->ordered_dither[(y & 7)<<3 | (x & 7)];
392  const uint8_t r8 = src[x] >> 16 & 0xff;
393  const uint8_t g8 = src[x] >> 8 & 0xff;
394  const uint8_t b8 = src[x] & 0xff;
395  const uint8_t r = av_clip_uint8(r8 + d);
396  const uint8_t g = av_clip_uint8(g8 + d);
397  const uint8_t b = av_clip_uint8(b8 + d);
398  const uint32_t c = r<<16 | g<<8 | b;
399  const int color = color_get(cache, c, r, g, b, map, palette, search_method);
400 
401  if (color < 0)
402  return color;
403  dst[x] = color;
404 
405  } else if (dither == DITHERING_HECKBERT) {
406  const int right = x < w - 1, down = y < h - 1;
407  const int color = get_dst_color_err(cache, src[x], map, palette, &er, &eg, &eb, search_method);
408 
409  if (color < 0)
410  return color;
411  dst[x] = color;
412 
413  if (right) src[ x + 1] = dither_color(src[ x + 1], er, eg, eb, 3, 3);
414  if ( down) src[src_linesize + x ] = dither_color(src[src_linesize + x ], er, eg, eb, 3, 3);
415  if (right && down) src[src_linesize + x + 1] = dither_color(src[src_linesize + x + 1], er, eg, eb, 2, 3);
416 
417  } else if (dither == DITHERING_FLOYD_STEINBERG) {
418  const int right = x < w - 1, down = y < h - 1, left = x > x_start;
419  const int color = get_dst_color_err(cache, src[x], map, palette, &er, &eg, &eb, search_method);
420 
421  if (color < 0)
422  return color;
423  dst[x] = color;
424 
425  if (right) src[ x + 1] = dither_color(src[ x + 1], er, eg, eb, 7, 4);
426  if (left && down) src[src_linesize + x - 1] = dither_color(src[src_linesize + x - 1], er, eg, eb, 3, 4);
427  if ( down) src[src_linesize + x ] = dither_color(src[src_linesize + x ], er, eg, eb, 5, 4);
428  if (right && down) src[src_linesize + x + 1] = dither_color(src[src_linesize + x + 1], er, eg, eb, 1, 4);
429 
430  } else if (dither == DITHERING_SIERRA2) {
431  const int right = x < w - 1, down = y < h - 1, left = x > x_start;
432  const int right2 = x < w - 2, left2 = x > x_start + 1;
433  const int color = get_dst_color_err(cache, src[x], map, palette, &er, &eg, &eb, search_method);
434 
435  if (color < 0)
436  return color;
437  dst[x] = color;
438 
439  if (right) src[ x + 1] = dither_color(src[ x + 1], er, eg, eb, 4, 4);
440  if (right2) src[ x + 2] = dither_color(src[ x + 2], er, eg, eb, 3, 4);
441 
442  if (down) {
443  if (left2) src[ src_linesize + x - 2] = dither_color(src[ src_linesize + x - 2], er, eg, eb, 1, 4);
444  if (left) src[ src_linesize + x - 1] = dither_color(src[ src_linesize + x - 1], er, eg, eb, 2, 4);
445  src[ src_linesize + x ] = dither_color(src[ src_linesize + x ], er, eg, eb, 3, 4);
446  if (right) src[ src_linesize + x + 1] = dither_color(src[ src_linesize + x + 1], er, eg, eb, 2, 4);
447  if (right2) src[ src_linesize + x + 2] = dither_color(src[ src_linesize + x + 2], er, eg, eb, 1, 4);
448  }
449 
450  } else if (dither == DITHERING_SIERRA2_4A) {
451  const int right = x < w - 1, down = y < h - 1, left = x > x_start;
452  const int color = get_dst_color_err(cache, src[x], map, palette, &er, &eg, &eb, search_method);
453 
454  if (color < 0)
455  return color;
456  dst[x] = color;
457 
458  if (right) src[ x + 1] = dither_color(src[ x + 1], er, eg, eb, 2, 2);
459  if (left && down) src[src_linesize + x - 1] = dither_color(src[src_linesize + x - 1], er, eg, eb, 1, 2);
460  if ( down) src[src_linesize + x ] = dither_color(src[src_linesize + x ], er, eg, eb, 1, 2);
461 
462  } else {
463  const uint8_t r = src[x] >> 16 & 0xff;
464  const uint8_t g = src[x] >> 8 & 0xff;
465  const uint8_t b = src[x] & 0xff;
466  const int color = color_get(cache, src[x] & 0xffffff, r, g, b, map, palette, search_method);
467 
468  if (color < 0)
469  return color;
470  dst[x] = color;
471  }
472  }
473  src += src_linesize;
474  dst += dst_linesize;
475  }
476  return 0;
477 }
478 
479 #define INDENT 4
480 static void disp_node(AVBPrint *buf,
481  const struct color_node *map,
482  int parent_id, int node_id,
483  int depth)
484 {
485  const struct color_node *node = &map[node_id];
486  const uint32_t fontcolor = node->val[0] > 0x50 &&
487  node->val[1] > 0x50 &&
488  node->val[2] > 0x50 ? 0 : 0xffffff;
489  av_bprintf(buf, "%*cnode%d ["
490  "label=\"%c%02X%c%02X%c%02X%c\" "
491  "fillcolor=\"#%02x%02x%02x\" "
492  "fontcolor=\"#%06X\"]\n",
493  depth*INDENT, ' ', node->palette_id,
494  "[ "[node->split], node->val[0],
495  "][ "[node->split], node->val[1],
496  " ]["[node->split], node->val[2],
497  " ]"[node->split],
498  node->val[0], node->val[1], node->val[2],
499  fontcolor);
500  if (parent_id != -1)
501  av_bprintf(buf, "%*cnode%d -> node%d\n", depth*INDENT, ' ',
502  map[parent_id].palette_id, node->palette_id);
503  if (node->left_id != -1) disp_node(buf, map, node_id, node->left_id, depth + 1);
504  if (node->right_id != -1) disp_node(buf, map, node_id, node->right_id, depth + 1);
505 }
506 
507 // debug_kdtree=kdtree.dot -> dot -Tpng kdtree.dot > kdtree.png
508 static int disp_tree(const struct color_node *node, const char *fname)
509 {
510  AVBPrint buf;
511  FILE *f = av_fopen_utf8(fname, "w");
512 
513  if (!f) {
514  int ret = AVERROR(errno);
515  av_log(NULL, AV_LOG_ERROR, "Cannot open file '%s' for writing: %s\n",
516  fname, av_err2str(ret));
517  return ret;
518  }
519 
521 
522  av_bprintf(&buf, "digraph {\n");
523  av_bprintf(&buf, " node [style=filled fontsize=10 shape=box]\n");
524  disp_node(&buf, node, -1, 0, 0);
525  av_bprintf(&buf, "}\n");
526 
527  fwrite(buf.str, 1, buf.len, f);
528  fclose(f);
529  av_bprint_finalize(&buf, NULL);
530  return 0;
531 }
532 
533 static int debug_accuracy(const struct color_node *node, const uint32_t *palette,
534  const enum color_search_method search_method)
535 {
536  int r, g, b, ret = 0;
537 
538  for (r = 0; r < 256; r++) {
539  for (g = 0; g < 256; g++) {
540  for (b = 0; b < 256; b++) {
541  const uint8_t rgb[] = {r, g, b};
542  const int r1 = COLORMAP_NEAREST(search_method, palette, node, rgb);
543  const int r2 = colormap_nearest_bruteforce(palette, rgb);
544  if (r1 != r2) {
545  const uint32_t c1 = palette[r1];
546  const uint32_t c2 = palette[r2];
547  const uint8_t palrgb1[] = { c1>>16 & 0xff, c1>> 8 & 0xff, c1 & 0xff };
548  const uint8_t palrgb2[] = { c2>>16 & 0xff, c2>> 8 & 0xff, c2 & 0xff };
549  const int d1 = diff(palrgb1, rgb);
550  const int d2 = diff(palrgb2, rgb);
551  if (d1 != d2) {
553  "/!\\ %02X%02X%02X: %d ! %d (%06X ! %06X) / dist: %d ! %d\n",
554  r, g, b, r1, r2, c1 & 0xffffff, c2 & 0xffffff, d1, d2);
555  ret = 1;
556  }
557  }
558  }
559  }
560  }
561  return ret;
562 }
563 
564 struct color {
565  uint32_t value;
567 };
568 
569 struct color_rect {
572 };
573 
574 typedef int (*cmp_func)(const void *, const void *);
575 
576 #define DECLARE_CMP_FUNC(name, pos) \
577 static int cmp_##name(const void *pa, const void *pb) \
578 { \
579  const struct color *a = pa; \
580  const struct color *b = pb; \
581  return (a->value >> (8 * (2 - (pos))) & 0xff) \
582  - (b->value >> (8 * (2 - (pos))) & 0xff); \
583 }
584 
588 
589 static const cmp_func cmp_funcs[] = {cmp_r, cmp_g, cmp_b};
590 
591 static int get_next_color(const uint8_t *color_used, const uint32_t *palette,
592  int *component, const struct color_rect *box)
593 {
594  int wr, wg, wb;
595  int i, longest = 0;
596  unsigned nb_color = 0;
597  struct color_rect ranges;
598  struct color tmp_pal[256];
599  cmp_func cmpf;
600 
601  ranges.min[0] = ranges.min[1] = ranges.min[2] = 0xff;
602  ranges.max[0] = ranges.max[1] = ranges.max[2] = 0x00;
603 
604  for (i = 0; i < AVPALETTE_COUNT; i++) {
605  const uint32_t c = palette[i];
606  const uint8_t r = c >> 16 & 0xff;
607  const uint8_t g = c >> 8 & 0xff;
608  const uint8_t b = c & 0xff;
609 
610  if (color_used[i] ||
611  r < box->min[0] || g < box->min[1] || b < box->min[2] ||
612  r > box->max[0] || g > box->max[1] || b > box->max[2])
613  continue;
614 
615  if (r < ranges.min[0]) ranges.min[0] = r;
616  if (g < ranges.min[1]) ranges.min[1] = g;
617  if (b < ranges.min[2]) ranges.min[2] = b;
618 
619  if (r > ranges.max[0]) ranges.max[0] = r;
620  if (g > ranges.max[1]) ranges.max[1] = g;
621  if (b > ranges.max[2]) ranges.max[2] = b;
622 
623  tmp_pal[nb_color].value = c;
624  tmp_pal[nb_color].pal_id = i;
625 
626  nb_color++;
627  }
628 
629  if (!nb_color)
630  return -1;
631 
632  /* define longest axis that will be the split component */
633  wr = ranges.max[0] - ranges.min[0];
634  wg = ranges.max[1] - ranges.min[1];
635  wb = ranges.max[2] - ranges.min[2];
636  if (wr >= wg && wr >= wb) longest = 0;
637  if (wg >= wr && wg >= wb) longest = 1;
638  if (wb >= wr && wb >= wg) longest = 2;
639  cmpf = cmp_funcs[longest];
640  *component = longest;
641 
642  /* sort along this axis to get median */
643  AV_QSORT(tmp_pal, nb_color, struct color, cmpf);
644 
645  return tmp_pal[nb_color >> 1].pal_id;
646 }
647 
648 static int colormap_insert(struct color_node *map,
649  uint8_t *color_used,
650  int *nb_used,
651  const uint32_t *palette,
652  const struct color_rect *box)
653 {
654  uint32_t c;
655  int component, cur_id;
656  int node_left_id = -1, node_right_id = -1;
657  struct color_node *node;
658  struct color_rect box1, box2;
659  const int pal_id = get_next_color(color_used, palette, &component, box);
660 
661  if (pal_id < 0)
662  return -1;
663 
664  /* create new node with that color */
665  cur_id = (*nb_used)++;
666  c = palette[pal_id];
667  node = &map[cur_id];
668  node->split = component;
669  node->palette_id = pal_id;
670  node->val[0] = c>>16 & 0xff;
671  node->val[1] = c>> 8 & 0xff;
672  node->val[2] = c & 0xff;
673 
674  color_used[pal_id] = 1;
675 
676  /* get the two boxes this node creates */
677  box1 = box2 = *box;
678  box1.max[component] = node->val[component];
679  box2.min[component] = node->val[component] + 1;
680 
681  node_left_id = colormap_insert(map, color_used, nb_used, palette, &box1);
682 
683  if (box2.min[component] <= box2.max[component])
684  node_right_id = colormap_insert(map, color_used, nb_used, palette, &box2);
685 
686  node->left_id = node_left_id;
687  node->right_id = node_right_id;
688 
689  return cur_id;
690 }
691 
692 static int cmp_pal_entry(const void *a, const void *b)
693 {
694  const int c1 = *(const uint32_t *)a & 0xffffff;
695  const int c2 = *(const uint32_t *)b & 0xffffff;
696  return c1 - c2;
697 }
698 
700 {
701  int i, nb_used = 0;
702  uint8_t color_used[AVPALETTE_COUNT] = {0};
703  uint32_t last_color = 0;
704  struct color_rect box;
705 
706  /* disable transparent colors and dups */
707  qsort(s->palette, AVPALETTE_COUNT, sizeof(*s->palette), cmp_pal_entry);
708  for (i = 0; i < AVPALETTE_COUNT; i++) {
709  const uint32_t c = s->palette[i];
710  if (i != 0 && c == last_color) {
711  color_used[i] = 1;
712  continue;
713  }
714  last_color = c;
715  if ((c & 0xff000000) != 0xff000000) {
716  color_used[i] = 1; // ignore transparent color(s)
717  continue;
718  }
719  }
720 
721  box.min[0] = box.min[1] = box.min[2] = 0x00;
722  box.max[0] = box.max[1] = box.max[2] = 0xff;
723 
724  colormap_insert(s->map, color_used, &nb_used, s->palette, &box);
725 
726  if (s->dot_filename)
727  disp_tree(s->map, s->dot_filename);
728 
729  if (s->debug_accuracy) {
731  av_log(NULL, AV_LOG_INFO, "Accuracy check passed\n");
732  }
733 }
734 
735 static void debug_mean_error(PaletteUseContext *s, const AVFrame *in1,
736  const AVFrame *in2, int frame_count)
737 {
738  int x, y;
739  const uint32_t *palette = s->palette;
740  uint32_t *src1 = (uint32_t *)in1->data[0];
741  uint8_t *src2 = in2->data[0];
742  const int src1_linesize = in1->linesize[0] >> 2;
743  const int src2_linesize = in2->linesize[0];
744  const float div = in1->width * in1->height * 3;
745  unsigned mean_err = 0;
746 
747  for (y = 0; y < in1->height; y++) {
748  for (x = 0; x < in1->width; x++) {
749  const uint32_t c1 = src1[x];
750  const uint32_t c2 = palette[src2[x]];
751  const uint8_t rgb1[] = {c1 >> 16 & 0xff, c1 >> 8 & 0xff, c1 & 0xff};
752  const uint8_t rgb2[] = {c2 >> 16 & 0xff, c2 >> 8 & 0xff, c2 & 0xff};
753  mean_err += diff(rgb1, rgb2);
754  }
755  src1 += src1_linesize;
756  src2 += src2_linesize;
757  }
758 
759  s->total_mean_err += mean_err;
760 
761  av_log(NULL, AV_LOG_INFO, "MEP:%.3f TotalMEP:%.3f\n",
762  mean_err / div, s->total_mean_err / (div * frame_count));
763 }
764 
766  const AVFrame *prv_src, const AVFrame *cur_src,
767  const AVFrame *prv_dst, AVFrame *cur_dst,
768  int *xp, int *yp, int *wp, int *hp)
769 {
770  int x_start = 0, y_start = 0;
771  int width = cur_src->width;
772  int height = cur_src->height;
773 
774  if (prv_src && diff_mode == DIFF_MODE_RECTANGLE) {
775  int y;
776  int x_end = cur_src->width - 1,
777  y_end = cur_src->height - 1;
778  const uint32_t *prv_srcp = (const uint32_t *)prv_src->data[0];
779  const uint32_t *cur_srcp = (const uint32_t *)cur_src->data[0];
780  const uint8_t *prv_dstp = prv_dst->data[0];
781  uint8_t *cur_dstp = cur_dst->data[0];
782 
783  const int prv_src_linesize = prv_src->linesize[0] >> 2;
784  const int cur_src_linesize = cur_src->linesize[0] >> 2;
785  const int prv_dst_linesize = prv_dst->linesize[0];
786  const int cur_dst_linesize = cur_dst->linesize[0];
787 
788  /* skip common lines */
789  while (y_start < y_end && !memcmp(prv_srcp + y_start*prv_src_linesize,
790  cur_srcp + y_start*cur_src_linesize,
791  cur_src->width * 4)) {
792  memcpy(cur_dstp + y_start*cur_dst_linesize,
793  prv_dstp + y_start*prv_dst_linesize,
794  cur_dst->width);
795  y_start++;
796  }
797  while (y_end > y_start && !memcmp(prv_srcp + y_end*prv_src_linesize,
798  cur_srcp + y_end*cur_src_linesize,
799  cur_src->width * 4)) {
800  memcpy(cur_dstp + y_end*cur_dst_linesize,
801  prv_dstp + y_end*prv_dst_linesize,
802  cur_dst->width);
803  y_end--;
804  }
805 
806  height = y_end + 1 - y_start;
807 
808  /* skip common columns */
809  while (x_start < x_end) {
810  int same_column = 1;
811  for (y = y_start; y <= y_end; y++) {
812  if (prv_srcp[y*prv_src_linesize + x_start] != cur_srcp[y*cur_src_linesize + x_start]) {
813  same_column = 0;
814  break;
815  }
816  }
817  if (!same_column)
818  break;
819  x_start++;
820  }
821  while (x_end > x_start) {
822  int same_column = 1;
823  for (y = y_start; y <= y_end; y++) {
824  if (prv_srcp[y*prv_src_linesize + x_end] != cur_srcp[y*cur_src_linesize + x_end]) {
825  same_column = 0;
826  break;
827  }
828  }
829  if (!same_column)
830  break;
831  x_end--;
832  }
833  width = x_end + 1 - x_start;
834 
835  if (x_start) {
836  for (y = y_start; y <= y_end; y++)
837  memcpy(cur_dstp + y*cur_dst_linesize,
838  prv_dstp + y*prv_dst_linesize, x_start);
839  }
840  if (x_end != cur_src->width - 1) {
841  const int copy_len = cur_src->width - 1 - x_end;
842  for (y = y_start; y <= y_end; y++)
843  memcpy(cur_dstp + y*cur_dst_linesize + x_end + 1,
844  prv_dstp + y*prv_dst_linesize + x_end + 1,
845  copy_len);
846  }
847  }
848  *xp = x_start;
849  *yp = y_start;
850  *wp = width;
851  *hp = height;
852 }
853 
855 {
856  int x, y, w, h;
857  AVFilterContext *ctx = inlink->dst;
858  PaletteUseContext *s = ctx->priv;
859  AVFilterLink *outlink = inlink->dst->outputs[0];
860 
861  AVFrame *out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
862  if (!out) {
863  av_frame_free(&in);
864  return NULL;
865  }
866  av_frame_copy_props(out, in);
867 
869  s->last_out, out, &x, &y, &w, &h);
870  av_frame_free(&s->last_in);
871  av_frame_free(&s->last_out);
872  s->last_in = av_frame_clone(in);
873  s->last_out = av_frame_clone(out);
874  if (!s->last_in || !s->last_out ||
876  av_frame_free(&in);
877  av_frame_free(&out);
878  return NULL;
879  }
880 
881  ff_dlog(ctx, "%dx%d rect: (%d;%d) -> (%d,%d) [area:%dx%d]\n",
882  w, h, x, y, x+w, y+h, in->width, in->height);
883 
884  if (s->set_frame(s, out, in, x, y, w, h) < 0) {
885  av_frame_free(&out);
886  return NULL;
887  }
888  memcpy(out->data[1], s->palette, AVPALETTE_SIZE);
889  if (s->calc_mean_err)
890  debug_mean_error(s, in, out, inlink->frame_count);
891  av_frame_free(&in);
892  return out;
893 }
894 
895 static int config_output(AVFilterLink *outlink)
896 {
897  int ret;
898  AVFilterContext *ctx = outlink->src;
899  PaletteUseContext *s = ctx->priv;
900 
901  outlink->w = ctx->inputs[0]->w;
902  outlink->h = ctx->inputs[0]->h;
903 
904  outlink->time_base = ctx->inputs[0]->time_base;
905  if ((ret = ff_dualinput_init(ctx, &s->dinput)) < 0)
906  return ret;
907  return 0;
908 }
909 
911 {
912  AVFilterContext *ctx = inlink->dst;
913 
914  if (inlink->w * inlink->h != AVPALETTE_COUNT) {
915  av_log(ctx, AV_LOG_ERROR,
916  "Palette input must contain exactly %d pixels. "
917  "Specified input has %dx%d=%d pixels\n",
918  AVPALETTE_COUNT, inlink->w, inlink->h,
919  inlink->w * inlink->h);
920  return AVERROR(EINVAL);
921  }
922  return 0;
923 }
924 
925 static void load_palette(PaletteUseContext *s, const AVFrame *palette_frame)
926 {
927  int i, x, y;
928  const uint32_t *p = (const uint32_t *)palette_frame->data[0];
929  const int p_linesize = palette_frame->linesize[0] >> 2;
930 
931  i = 0;
932  for (y = 0; y < palette_frame->height; y++) {
933  for (x = 0; x < palette_frame->width; x++)
934  s->palette[i++] = p[x];
935  p += p_linesize;
936  }
937 
938  load_colormap(s);
939 
940  s->palette_loaded = 1;
941 }
942 
944  const AVFrame *second)
945 {
946  AVFilterLink *inlink = ctx->inputs[0];
947  PaletteUseContext *s = ctx->priv;
948  if (!s->palette_loaded) {
949  load_palette(s, second);
950  }
951  return apply_palette(inlink, main);
952 }
953 
954 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
955 {
956  PaletteUseContext *s = inlink->dst->priv;
957  return ff_dualinput_filter_frame(&s->dinput, inlink, in);
958 }
959 
960 #define DEFINE_SET_FRAME(color_search, name, value) \
961 static int set_frame_##name(PaletteUseContext *s, AVFrame *out, AVFrame *in, \
962  int x_start, int y_start, int w, int h) \
963 { \
964  return set_frame(s, out, in, x_start, y_start, w, h, value, color_search); \
965 }
966 
967 #define DEFINE_SET_FRAME_COLOR_SEARCH(color_search, color_search_macro) \
968  DEFINE_SET_FRAME(color_search_macro, color_search##_##none, DITHERING_NONE) \
969  DEFINE_SET_FRAME(color_search_macro, color_search##_##bayer, DITHERING_BAYER) \
970  DEFINE_SET_FRAME(color_search_macro, color_search##_##heckbert, DITHERING_HECKBERT) \
971  DEFINE_SET_FRAME(color_search_macro, color_search##_##floyd_steinberg, DITHERING_FLOYD_STEINBERG) \
972  DEFINE_SET_FRAME(color_search_macro, color_search##_##sierra2, DITHERING_SIERRA2) \
973  DEFINE_SET_FRAME(color_search_macro, color_search##_##sierra2_4a, DITHERING_SIERRA2_4A) \
974 
978 
979 #define DITHERING_ENTRIES(color_search) { \
980  set_frame_##color_search##_none, \
981  set_frame_##color_search##_bayer, \
982  set_frame_##color_search##_heckbert, \
983  set_frame_##color_search##_floyd_steinberg, \
984  set_frame_##color_search##_sierra2, \
985  set_frame_##color_search##_sierra2_4a, \
986 }
987 
989  DITHERING_ENTRIES(nns_iterative),
990  DITHERING_ENTRIES(nns_recursive),
991  DITHERING_ENTRIES(bruteforce),
992 };
993 
994 static int dither_value(int p)
995 {
996  const int q = p ^ (p >> 3);
997  return (p & 4) >> 2 | (q & 4) >> 1 \
998  | (p & 2) << 1 | (q & 2) << 2 \
999  | (p & 1) << 4 | (q & 1) << 5;
1000 }
1001 
1003 {
1004  PaletteUseContext *s = ctx->priv;
1005  s->dinput.repeatlast = 1; // only 1 frame in the palette
1008 
1009  s->set_frame = set_frame_lut[s->color_search_method][s->dither];
1010 
1011  if (s->dither == DITHERING_BAYER) {
1012  int i;
1013  const int delta = 1 << (5 - s->bayer_scale); // to avoid too much luma
1014 
1015  for (i = 0; i < FF_ARRAY_ELEMS(s->ordered_dither); i++)
1016  s->ordered_dither[i] = (dither_value(i) >> s->bayer_scale) - delta;
1017  }
1018 
1019  return 0;
1020 }
1021 
1022 static int request_frame(AVFilterLink *outlink)
1023 {
1024  PaletteUseContext *s = outlink->src->priv;
1025  return ff_dualinput_request_frame(&s->dinput, outlink);
1026 }
1027 
1029 {
1030  int i;
1031  PaletteUseContext *s = ctx->priv;
1032 
1034  for (i = 0; i < CACHE_SIZE; i++)
1035  av_freep(&s->cache[i].entries);
1036  av_frame_free(&s->last_in);
1037  av_frame_free(&s->last_out);
1038 }
1039 
1040 static const AVFilterPad paletteuse_inputs[] = {
1041  {
1042  .name = "default",
1043  .type = AVMEDIA_TYPE_VIDEO,
1044  .filter_frame = filter_frame,
1045  .needs_writable = 1, // for error diffusal dithering
1046  },{
1047  .name = "palette",
1048  .type = AVMEDIA_TYPE_VIDEO,
1049  .config_props = config_input_palette,
1050  .filter_frame = filter_frame,
1051  },
1052  { NULL }
1053 };
1054 
1056  {
1057  .name = "default",
1058  .type = AVMEDIA_TYPE_VIDEO,
1059  .config_props = config_output,
1060  .request_frame = request_frame,
1061  },
1062  { NULL }
1063 };
1064 
1066  .name = "paletteuse",
1067  .description = NULL_IF_CONFIG_SMALL("Use a palette to downsample an input video stream."),
1068  .priv_size = sizeof(PaletteUseContext),
1070  .init = init,
1071  .uninit = uninit,
1072  .inputs = paletteuse_inputs,
1073  .outputs = paletteuse_outputs,
1074  .priv_class = &paletteuse_class,
1075 };
diff_mode
Definition: vf_paletteuse.c:50
int(* cmp_func)(const void *, const void *)
uint64_t total_mean_err
AVFILTER_DEFINE_CLASS(paletteuse)
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:631
void av_bprintf(AVBPrint *buf, const char *fmt,...)
Definition: bprint.c:94
static int shift(int a, int b)
Definition: sonic.c:82
static void load_palette(PaletteUseContext *s, const AVFrame *palette_frame)
static AVFrame * load_apply_palette(AVFilterContext *ctx, AVFrame *main, const AVFrame *second)
This structure describes decoded (raw) audio or video data.
Definition: frame.h:184
AVOption.
Definition: opt.h:245
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
color_search_method
Definition: vf_paletteuse.c:43
Main libavfilter public API header.
static AVFrame * apply_palette(AVFilterLink *inlink, AVFrame *in)
const char * g
Definition: vf_curves.c:108
dithering_mode
Definition: vf_paletteuse.c:33
static int query_formats(AVFilterContext *ctx)
FILE * av_fopen_utf8(const char *path, const char *mode)
Open a file using a UTF-8 filename.
Definition: file_open.c:154
const char * b
Definition: vf_curves.c:109
static const AVFilterPad paletteuse_outputs[]
static int debug_accuracy(const struct color_node *node, const uint32_t *palette, const enum color_search_method search_method)
void * av_dynarray2_add(void **tab_ptr, int *nb_ptr, size_t elem_size, const uint8_t *elem_data)
Add an element of size elem_size to a dynamic array.
Definition: mem.c:338
static av_always_inline int dither_color(uint32_t px, int er, int eg, int eb, int scale, int shift)
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:76
static av_cold int init(AVFilterContext *ctx)
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
Definition: bprint.c:235
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:283
const char * name
Pad name.
Definition: internal.h:59
int ff_dualinput_filter_frame(FFDualInputContext *s, AVFilterLink *inlink, AVFrame *in)
Definition: dualinput.c:76
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:313
int(* set_frame_func)(struct PaletteUseContext *s, AVFrame *out, AVFrame *in, int x_start, int y_start, int width, int height)
Definition: vf_paletteuse.c:78
AVFilter ff_vf_paletteuse
uint8_t
#define av_cold
Definition: attributes.h:82
float delta
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:73
AVOptions.
uint8_t pal_id
#define INDENT
static const uint32_t color[16+AV_CLASS_CATEGORY_NB]
Definition: log.c:94
#define AVPALETTE_SIZE
Definition: pixfmt.h:32
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
uint32_t color
Definition: vf_paletteuse.c:67
static const cmp_func cmp_funcs[]
static int config_input_palette(AVFilterLink *inlink)
#define height
static const uint64_t c1
Definition: murmur3.c:49
#define ff_dlog(a,...)
uint32_t palette[AVPALETTE_COUNT]
Definition: vf_paletteuse.c:86
void ff_dualinput_uninit(FFDualInputContext *s)
Definition: dualinput.c:87
static const AVOption paletteuse_options[]
#define av_log(a,...)
uint8_t val[3]
Definition: vf_paletteuse.c:57
A filter pad used for either input or output.
Definition: internal.h:53
uint8_t hash[HASH_SIZE]
Definition: movenc.c:57
#define DEFINE_SET_FRAME_COLOR_SEARCH(color_search, color_search_macro)
static int disp_tree(const struct color_node *node, const char *fname)
set_frame_func set_frame
Definition: vf_paletteuse.c:89
int width
width and height of the video frame
Definition: frame.h:236
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
uint8_t max[3]
#define AV_BPRINT_SIZE_UNLIMITED
#define AVERROR(e)
Definition: error.h:43
static void colormap_nearest_node(const struct color_node *map, const int node_pos, const uint8_t *target, struct nearest_color *nearest)
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:153
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
Definition: bprint.c:69
const char * r
Definition: vf_curves.c:107
static const uint8_t dither[8][8]
Definition: vf_fspp.c:57
void * priv
private data for use by the filter
Definition: avfilter.h:320
static const set_frame_func set_frame_lut[NB_COLOR_SEARCHES][NB_DITHERING]
static int request_frame(AVFilterLink *outlink)
int depth
Definition: v4l.c:62
static int dither_value(int p)
static char * split(char *message, char delim)
Definition: af_channelmap.c:81
static av_always_inline uint8_t colormap_nearest_iterative(const struct color_node *root, const uint8_t *target)
common internal API header
int skip_initial_unpaired
Skip initial frames that do not have a 2nd input.
Definition: dualinput.h:38
static void disp_node(AVBPrint *buf, const struct color_node *map, int parent_id, int node_id, int depth)
#define width
int ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
Add *ref as a new reference to formats.
Definition: formats.c:440
static void set_processing_window(enum diff_mode diff_mode, const AVFrame *prv_src, const AVFrame *cur_src, const AVFrame *prv_dst, AVFrame *cur_dst, int *xp, int *yp, int *wp, int *hp)
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: error.h:119
AVFormatContext * ctx
Definition: movenc.c:48
#define NBITS
Definition: vf_paletteuse.c:63
uint8_t min[3]
static av_always_inline int diff(const uint8_t *c1, const uint8_t *c2)
AVFrame *(* process)(AVFilterContext *ctx, AVFrame *main, const AVFrame *second)
Definition: dualinput.h:35
static const AVFilterPad outputs[]
Definition: af_afftfilt.c:386
#define src
Definition: vp9dsp.c:530
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:471
#define FF_ARRAY_ELEMS(a)
static av_always_inline int get_dst_color_err(struct cache_node *cache, uint32_t c, const struct color_node *map, const uint32_t *palette, int *er, int *eg, int *eb, const enum color_search_method search_method)
static int config_output(AVFilterLink *outlink)
#define src1
Definition: h264pred.c:139
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
static const AVFilterPad inputs[]
Definition: af_afftfilt.c:376
static int cmp_pal_entry(const void *a, const void *b)
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:215
#define AV_PIX_FMT_RGB32
Definition: pixfmt.h:318
AVFrame * last_out
Definition: vf_paletteuse.c:94
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> in
int repeatlast
repeat last second frame
Definition: dualinput.h:37
void * buf
Definition: avisynth_c.h:553
Describe the class of an AVClass context structure.
Definition: log.h:67
#define DITHERING_ENTRIES(color_search)
Filter definition.
Definition: avfilter.h:142
static int get_next_color(const uint8_t *color_used, const uint32_t *palette, int *component, const struct color_rect *box)
const char * name
Filter name.
Definition: avfilter.h:146
static void load_colormap(PaletteUseContext *s)
static int colormap_insert(struct color_node *map, uint8_t *color_used, int *nb_used, const uint32_t *palette, const struct color_rect *box)
const VDPAUPixFmtMap * map
#define DECLARE_CMP_FUNC(name, pos)
#define OFFSET(x)
int ff_dualinput_init(AVFilterContext *ctx, FFDualInputContext *s)
Definition: dualinput.c:43
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:317
#define AVPALETTE_COUNT
Definition: pixfmt.h:33
#define COLORMAP_NEAREST(search, palette, root, target)
int av_frame_make_writable(AVFrame *frame)
Ensure that the frame data is writable, avoiding data copy if possible.
Definition: frame.c:537
#define FLAGS
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:198
FFDualInputContext dinput
Definition: vf_paletteuse.c:83
int palette
Definition: v4l.c:61
uint8_t pal_entry
Definition: vf_paletteuse.c:68
struct cache_node cache[CACHE_SIZE]
Definition: vf_paletteuse.c:84
static double c[64]
struct cached_color * entries
Definition: vf_paletteuse.c:72
static const uint64_t c2
Definition: murmur3.c:50
static av_always_inline uint8_t colormap_nearest_recursive(const struct color_node *node, const uint8_t *rgb)
static av_always_inline int set_frame(PaletteUseContext *s, AVFrame *out, AVFrame *in, int x_start, int y_start, int w, int h, enum dithering_mode dither, const enum color_search_method search_method)
static av_cold void uninit(AVFilterContext *ctx)
static av_always_inline uint8_t colormap_nearest_bruteforce(const uint32_t *palette, const uint8_t *rgb)
Double input streams helper for filters.
A list of supported formats for one end of a filter link.
Definition: formats.h:64
static av_always_inline int color_get(struct cache_node *cache, uint32_t color, uint8_t r, uint8_t g, uint8_t b, const struct color_node *map, const uint32_t *palette, const enum color_search_method search_method)
Check if the requested color is in the cache already.
An instance of a filter.
Definition: avfilter.h:305
int ff_dualinput_request_frame(FFDualInputContext *s, AVFilterLink *outlink)
Definition: dualinput.c:82
uint8_t palette_id
Definition: vf_paletteuse.c:58
int height
Definition: frame.h:236
FILE * out
Definition: movenc.c:54
#define av_freep(p)
#define av_always_inline
Definition: attributes.h:39
static const AVFilterPad paletteuse_inputs[]
int main(int argc, char **argv)
Definition: main.c:22
#define CACHE_SIZE
Definition: vf_paletteuse.c:64
struct color_node map[AVPALETTE_COUNT]
Definition: vf_paletteuse.c:85
float min
uint32_t value
AVPixelFormat
Pixel format.
Definition: pixfmt.h:60
#define AV_QSORT(p, num, type, cmp)
Quicksort This sort is fast, and fully inplace but not stable and it is possible to construct input t...
Definition: qsort.h:33
for(j=16;j >0;--j)
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:580
int ordered_dither[8 *8]
Definition: vf_paletteuse.c:91
static void debug_mean_error(PaletteUseContext *s, const AVFrame *in1, const AVFrame *in2, int frame_count)