1 /*
2 * Copyright (c) 2015 Stupeflix
3 * Copyright (c) 2022 Clément Bœsch <u pkh me>
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /**
23 * @file
24 * Use a palette to downsample an input video stream.
25 */
26
39
51 };
52
57 };
58
62 };
63
69 };
70
71 #define CACHE_SIZE (1<<15)
72
76 };
77
81 };
82
84
87
94 int transparency_index;
/* index in the palette of transparency. -1 if there is no transparency in the palette. */
105
106 /* debug options */
111
112 #define OFFSET(x) offsetof(PaletteUseContext, x)
113 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
128 {
"alpha_threshold",
"set the alpha threshold for transparency",
OFFSET(trans_thresh),
AV_OPT_TYPE_INT, {.i64=128}, 0, 255,
FLAGS },
129
130 /* following are the debug options, not part of the official API */
133 };
134
136
138
142 {
154 return 0;
155 }
156
159 {
160 return (
px & 0xff000000)
164 }
165
167 {
168 const uint8_t alpha_a =
a->srgb >> 24;
169 const uint8_t alpha_b =
b->srgb >> 24;
170
171 if (alpha_a < trans_thresh && alpha_b < trans_thresh) {
172 return 0;
173 } else if (alpha_a >= trans_thresh && alpha_b >= trans_thresh) {
174 const int64_t dL =
a->lab[0] -
b->lab[0];
175 const int64_t da =
a->lab[1] -
b->lab[1];
176 const int64_t db =
a->lab[2] -
b->lab[2];
179 } else {
180 return INT32_MAX - 1;
181 }
182 }
183
185 {
189 }
190
194 };
195
197 const int node_pos,
199 const int trans_thresh,
201 {
203 int nearer_kd_id, further_kd_id;
205 const int64_t current_to_target =
diff(target, current, trans_thresh);
206
207 if (current_to_target < nearest->dist_sqd) {
209 nearest->
dist_sqd = current_to_target;
210 }
211
214
217
218 if (nearer_kd_id != -1)
220
221 if (further_kd_id != -1 && dx*dx < nearest->dist_sqd)
223 }
224 }
225
227 {
231 }
232
236 };
237
238 /**
239 * Check if the requested color is in the cache already. If not, find it in the
240 * color tree and cache it.
241 */
243 {
248
249 // first, check for transparency
250 if (
color>>24 <
s->trans_thresh &&
s->transparency_index >= 0) {
251 return s->transparency_index;
252 }
253
258 }
259
262 if (!e)
267
269 }
270
272 uint32_t
c,
int *er,
int *eg,
int *eb)
273 {
274 uint32_t dstc;
276 if (dstx < 0)
277 return dstx;
278 dstc =
s->palette[dstx];
279 if (dstx ==
s->transparency_index) {
280 *er = *eg = *eb = 0;
281 } else {
282 const uint8_t
r =
c >> 16 & 0xff;
283 const uint8_t
g =
c >> 8 & 0xff;
284 const uint8_t
b =
c & 0xff;
285 *er = (int)
r - (
int)(dstc >> 16 & 0xff);
286 *eg = (int)
g - (
int)(dstc >> 8 & 0xff);
287 *eb = (int)
b - (
int)(dstc & 0xff);
288 }
289 return dstx;
290 }
291
293 int x_start,
int y_start,
int w,
int h,
295 {
296 const int src_linesize = in ->
linesize[0] >> 2;
297 const int dst_linesize =
out->linesize[0];
298 uint32_t *
src = ((uint32_t *)in ->
data[0]) + y_start*src_linesize;
299 uint8_t *
dst =
out->data[0] + y_start*dst_linesize;
300
303
304 for (
int y = y_start; y <
h; y++) {
305 for (
int x = x_start; x <
w; x++) {
306 int er, eg, eb;
307
309 const int d =
s->ordered_dither[(y & 7)<<3 | (x & 7)];
310 const uint8_t a8 =
src[x] >> 24;
311 const uint8_t r8 =
src[x] >> 16 & 0xff;
312 const uint8_t g8 =
src[x] >> 8 & 0xff;
313 const uint8_t b8 =
src[x] & 0xff;
317 const uint32_t color_new = (unsigned)(a8) << 24 |
r << 16 |
g << 8 |
b;
319
323
325 const int right = x <
w - 1, down = y <
h - 1;
327
331
333 if ( down)
src[src_linesize + x ] =
dither_color(
src[src_linesize + x ], er, eg, eb, 3, 3);
334 if (right && down)
src[src_linesize + x + 1] =
dither_color(
src[src_linesize + x + 1], er, eg, eb, 2, 3);
335
337 const int right = x <
w - 1, down = y <
h - 1,
left = x > x_start;
339
343
346 if ( down)
src[src_linesize + x ] =
dither_color(
src[src_linesize + x ], er, eg, eb, 5, 4);
347 if (right && down)
src[src_linesize + x + 1] =
dither_color(
src[src_linesize + x + 1], er, eg, eb, 1, 4);
348
350 const int right = x <
w - 1, down = y <
h - 1,
left = x > x_start;
351 const int right2 = x <
w - 2, left2 = x > x_start + 1;
353
357
360
361 if (down) {
362 if (left2)
src[ src_linesize + x - 2] =
dither_color(
src[ src_linesize + x - 2], er, eg, eb, 1, 4);
364 if (1)
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
370 const int right = x <
w - 1, down = y <
h - 1,
left = x > x_start;
372
376
379 if ( down)
src[src_linesize + x ] =
dither_color(
src[src_linesize + x ], er, eg, eb, 1, 2);
380
382 const int right = x <
w - 1, down = y <
h - 1,
left = x > x_start;
383 const int right2 = x <
w - 2, down2 = y <
h - 2, left2 = x > x_start + 1;
385
389
392
393 if (down) {
394 if (left2)
src[src_linesize + x - 2] =
dither_color(
src[src_linesize + x - 2], er, eg, eb, 2, 5);
396 if (1)
src[src_linesize + x ] =
dither_color(
src[src_linesize + x ], er, eg, eb, 5, 5);
397 if (right)
src[src_linesize + x + 1] =
dither_color(
src[src_linesize + x + 1], er, eg, eb, 4, 5);
398 if (right2)
src[src_linesize + x + 2] =
dither_color(
src[src_linesize + x + 2], er, eg, eb, 2, 5);
399
400 if (down2) {
402 if (1)
src[src_linesize*2 + x ] =
dither_color(
src[src_linesize*2 + x ], er, eg, eb, 3, 5);
403 if (right)
src[src_linesize*2 + x + 1] =
dither_color(
src[src_linesize*2 + x + 1], er, eg, eb, 2, 5);
404 }
405 }
406
408 const int right = x <
w - 1, down = y <
h - 1,
left = x > x_start;
409 const int right2 = x <
w - 2, left2 = x > x_start + 1;
411
415
418
419 if (down) {
420 if (left2)
src[src_linesize + x - 2] =
dither_color(
src[src_linesize + x - 2], er, eg, eb, 2, 5);
422 if (1)
src[src_linesize + x ] =
dither_color(
src[src_linesize + x ], er, eg, eb, 8, 5);
423 if (right)
src[src_linesize + x + 1] =
dither_color(
src[src_linesize + x + 1], er, eg, eb, 4, 5);
424 if (right2)
src[src_linesize + x + 2] =
dither_color(
src[src_linesize + x + 2], er, eg, eb, 2, 5);
425 }
426
428 const int right = x <
w - 1, down = y <
h - 1,
left = x > x_start;
429 const int right2 = x <
w - 2, down2 = y <
h - 2;
431
435
438
439 if (down) {
441 if (1)
src[src_linesize + x ] =
dither_color(
src[src_linesize + x ], er, eg, eb, 1, 3);
442 if (right)
src[src_linesize + x + 1] =
dither_color(
src[src_linesize + x + 1], er, eg, eb, 1, 3);
443 if (down2)
src[src_linesize*2 + x ] =
dither_color(
src[src_linesize*2 + x ], er, eg, eb, 1, 3);
444 }
445
446 } else {
448
452 }
453 }
456 }
457 return 0;
458 }
459
463 int parent_id, int node_id,
464 int depth)
465 {
467 const uint32_t fontcolor = node->
c.
lab[0] > 0x7fff ? 0 : 0xffffff;
468 const int lab_comp = node->
split;
470 "label=\"%c%d%c%d%c%d%c\" "
471 "fillcolor=\"#%06"PRIX32"\" "
472 "fontcolor=\"#%06"PRIX32"\"]\n",
474 "[ "[lab_comp], node->
c.
lab[0],
475 "][ "[lab_comp], node->
c.
lab[1],
476 " ]["[lab_comp], node->
c.
lab[2],
477 " ]"[lab_comp],
478 node->
c.
srgb & 0xffffff,
479 fontcolor);
480 if (parent_id != -1)
485 }
486
487 // debug_kdtree=kdtree.dot -> dot -Tpng kdtree.dot > kdtree.png
489 {
490 AVBPrint buf;
492
498 }
499
501
503 av_bprintf(&buf,
" node [style=filled fontsize=10 shape=box]\n");
506
507 fwrite(buf.str, 1, buf.len,
f);
510 return 0;
511 }
512
516 };
517
521 };
522
523 typedef int (*
cmp_func)(
const void *,
const void *);
524
525 #define DECLARE_CMP_FUNC(name) \
526 static int cmp_##name(const void *pa, const void *pb) \
527 { \
528 const struct color *a = pa; \
529 const struct color *b = pb; \
530 return FFDIFFSIGN(a->value.name, b->value.name); \
531 }
532
536
538
541 {
542 int wL, wa, wb;
543 int longest = 0;
544 unsigned nb_color = 0;
546 struct color tmp_pal[256];
548
549 ranges.
min[0] = ranges.
min[1] = ranges.
min[2] = 0xffff;
550 ranges.
max[0] = ranges.
max[1] = ranges.
max[2] = -0xffff;
551
553 const uint32_t
c = palette[
i];
554 const uint8_t
a =
c >> 24;
556
557 if (color_used[
i] || (
a != 0xff) ||
558 lab.
L < box->
min[0] || lab.
a < box->
min[1] || lab.
b < box->
min[2] ||
559 lab.
L > box->
max[0] || lab.
a > box->
max[1] || lab.
b > box->
max[2])
560 continue;
561
562 if (lab.
L < ranges.
min[0]) ranges.
min[0] = lab.
L;
563 if (lab.
a < ranges.
min[1]) ranges.
min[1] = lab.
a;
564 if (lab.
b < ranges.
min[2]) ranges.
min[2] = lab.
b;
565
566 if (lab.
L > ranges.
max[0]) ranges.
max[0] = lab.
L;
567 if (lab.
a > ranges.
max[1]) ranges.
max[1] = lab.
a;
568 if (lab.
b > ranges.
max[2]) ranges.
max[2] = lab.
b;
569
570 tmp_pal[nb_color].
value = lab;
572
573 nb_color++;
574 }
575
576 if (!nb_color)
577 return -1;
578
579 /* define longest axis that will be the split component */
580 wL = ranges.
max[0] - ranges.
min[0];
581 wa = ranges.
max[1] - ranges.
min[1];
582 wb = ranges.
max[2] - ranges.
min[2];
583 if (wb >= wL && wb >= wa) longest = 2;
584 if (wa >= wL && wa >= wb) longest = 1;
585 if (wL >= wa && wL >= wb) longest = 0;
587 *component = longest;
588
589 /* sort along this axis to get median */
591
592 return tmp_pal[nb_color >> 1].
pal_id;
593 }
594
596 uint8_t *color_used,
597 int *nb_used,
598 const uint32_t *palette,
599 const int trans_thresh,
601 {
602 int component, cur_id;
603 int comp_value;
604 int node_left_id = -1, node_right_id = -1;
607 const int pal_id =
get_next_color(color_used, palette, &component, box);
608
609 if (pal_id < 0)
610 return -1;
611
612 /* create new node with that color */
613 cur_id = (*nb_used)++;
615 node->
split = component;
618
619 color_used[pal_id] = 1;
620
621 /* get the two boxes this node creates */
622 box1 = box2 = *box;
623 comp_value = node->
c.
lab[component];
624 box1.
max[component] = comp_value;
625 box2.
min[component] =
FFMIN(comp_value + 1, 0xffff);
626
627 node_left_id =
colormap_insert(
map, color_used, nb_used, palette, trans_thresh, &box1);
628
629 if (box2.
min[component] <= box2.
max[component])
630 node_right_id =
colormap_insert(
map, color_used, nb_used, palette, trans_thresh, &box2);
631
634
635 return cur_id;
636 }
637
639 {
640 const int c1 = *(
const uint32_t *)
a & 0xffffff;
641 const int c2 = *(
const uint32_t *)
b & 0xffffff;
643 }
644
646 {
647 int nb_used = 0;
649 uint32_t last_color = 0;
651
652 if (
s->transparency_index >= 0) {
653 FFSWAP(uint32_t,
s->palette[
s->transparency_index],
s->palette[255]);
654 }
655
656 /* disable transparent colors and dups */
658
660 const uint32_t
c =
s->palette[
i];
661 if (
i != 0 &&
c == last_color) {
663 continue;
664 }
666 if (
c >> 24 <
s->trans_thresh) {
667 color_used[
i] = 1;
// ignore transparent color(s)
668 continue;
669 }
670 }
671
672 box.
min[0] = box.
min[1] = box.
min[2] = -0xffff;
673 box.
max[0] = box.
max[1] = box.
max[2] = 0xffff;
674
676
679 }
680
684 int *xp, int *yp, int *wp, int *hp)
685 {
686 int x_start = 0, y_start = 0;
689
691 int y;
692 int x_end = cur_src->
width - 1,
693 y_end = cur_src->
height - 1;
694 const uint32_t *prv_srcp = (
const uint32_t *)prv_src->
data[0];
695 const uint32_t *cur_srcp = (
const uint32_t *)cur_src->
data[0];
696 const uint8_t *prv_dstp = prv_dst->
data[0];
697 uint8_t *cur_dstp = cur_dst->
data[0];
698
699 const int prv_src_linesize = prv_src->
linesize[0] >> 2;
700 const int cur_src_linesize = cur_src->
linesize[0] >> 2;
701 const int prv_dst_linesize = prv_dst->
linesize[0];
702 const int cur_dst_linesize = cur_dst->
linesize[0];
703
704 /* skip common lines */
705 while (y_start < y_end && !memcmp(prv_srcp + y_start*prv_src_linesize,
706 cur_srcp + y_start*cur_src_linesize,
707 cur_src->
width * 4)) {
708 memcpy(cur_dstp + y_start*cur_dst_linesize,
709 prv_dstp + y_start*prv_dst_linesize,
711 y_start++;
712 }
713 while (y_end > y_start && !memcmp(prv_srcp + y_end*prv_src_linesize,
714 cur_srcp + y_end*cur_src_linesize,
715 cur_src->
width * 4)) {
716 memcpy(cur_dstp + y_end*cur_dst_linesize,
717 prv_dstp + y_end*prv_dst_linesize,
719 y_end--;
720 }
721
722 height = y_end + 1 - y_start;
723
724 /* skip common columns */
725 while (x_start < x_end) {
726 int same_column = 1;
727 for (y = y_start; y <= y_end; y++) {
728 if (prv_srcp[y*prv_src_linesize + x_start] != cur_srcp[y*cur_src_linesize + x_start]) {
729 same_column = 0;
730 break;
731 }
732 }
733 if (!same_column)
734 break;
735 x_start++;
736 }
737 while (x_end > x_start) {
738 int same_column = 1;
739 for (y = y_start; y <= y_end; y++) {
740 if (prv_srcp[y*prv_src_linesize + x_end] != cur_srcp[y*cur_src_linesize + x_end]) {
741 same_column = 0;
742 break;
743 }
744 }
745 if (!same_column)
746 break;
747 x_end--;
748 }
749 width = x_end + 1 - x_start;
750
751 if (x_start) {
752 for (y = y_start; y <= y_end; y++)
753 memcpy(cur_dstp + y*cur_dst_linesize,
754 prv_dstp + y*prv_dst_linesize, x_start);
755 }
756 if (x_end != cur_src->
width - 1) {
757 const int copy_len = cur_src->
width - 1 - x_end;
758 for (y = y_start; y <= y_end; y++)
759 memcpy(cur_dstp + y*cur_dst_linesize + x_end + 1,
760 prv_dstp + y*prv_dst_linesize + x_end + 1,
761 copy_len);
762 }
763 }
764 *xp = x_start;
765 *yp = y_start;
768 }
769
771 {
776
781 }
783
785 s->last_out,
out, &x, &y, &
w, &
h);
793 }
794
795 ff_dlog(
ctx,
"%dx%d rect: (%d;%d) -> (%d,%d) [area:%dx%d]\n",
797
803 }
806 return 0;
807 }
808
810 {
814
818 s->fs.opt_repeatlast = 1;
// only 1 frame in the palette
821
822 outlink->
w =
ctx->inputs[0]->w;
823 outlink->
h =
ctx->inputs[0]->h;
824
828 return 0;
829 }
830
832 {
834
837 "Palette input must contain exactly %d pixels. "
838 "Specified input has %dx%d=%d pixels\n",
842 }
843 return 0;
844 }
845
847 {
849 const uint32_t *
p = (
const uint32_t *)palette_frame->
data[0];
850 const ptrdiff_t p_linesize = palette_frame->
linesize[0] >> 2;
851
852 s->transparency_index = -1;
853
855 memset(
s->palette, 0,
sizeof(
s->palette));
856 memset(
s->map, 0,
sizeof(
s->map));
859 memset(
s->cache, 0,
sizeof(
s->cache));
860 }
861
863 for (y = 0; y < palette_frame->
height; y++) {
864 for (x = 0; x < palette_frame->
width; x++) {
865 s->palette[
i] =
p[x];
866 if (
p[x]>>24 <
s->trans_thresh) {
867 s->transparency_index =
i;
// we are assuming at most one transparent color in palette
868 }
870 }
872 }
873
875
877 s->palette_loaded = 1;
878 }
879
881 {
887
888 // writable for error diffusal dithering
895 }
896 if (!
s->palette_loaded) {
898 }
904 }
905
906 #define DEFINE_SET_FRAME(name, value) \
907 static int set_frame_##name(PaletteUseContext *s, AVFrame *out, AVFrame *in, \
908 int x_start, int y_start, int w, int h) \
909 { \
910 return set_frame(s, out, in, x_start, y_start, w, h, value); \
911 }
912
922
933 };
934
936 {
937 const int q =
p ^ (
p >> 3);
938 return (
p & 4) >> 2 | (q & 4) >> 1 \
939 | (
p & 2) << 1 | (q & 2) << 2 \
940 | (
p & 1) << 4 | (q & 1) << 5;
941 }
942
944 {
946
949 if (!
s->last_in || !
s->last_out)
951
953
955 const int delta = 1 << (5 -
s->bayer_scale);
// to avoid too much luma
956
959 }
960
961 return 0;
962 }
963
965 {
968 }
969
971 {
973
979 }
980
982 {
985 },{
986 .name = "palette",
989 },
990 };
991
993 {
997 },
998 };
999
1001 .
p.
name =
"paletteuse",
1003 .p.priv_class = &paletteuse_class,
1011 };