65 #define SQR(x) ((x) * (x))
68 #define GET_CHAN(color, chan) (((color) >> ((chan) * 5) & 0x1F) * 8)
69 #define R(color) GET_CHAN(color, RED)
70 #define G(color) GET_CHAN(color, GREEN)
71 #define B(color) GET_CHAN(color, BLUE)
91 color4[0][0] =
min[0];
92 color4[0][1] =
min[1];
93 color4[0][2] =
min[2];
95 color4[3][0] =
max[0];
96 color4[3][1] =
max[1];
97 color4[3][2] =
max[2];
100 step = (color4[3][0] - color4[0][0] + 1) / 3;
101 color4[1][0] = color4[0][0] +
step;
102 color4[2][0] = color4[3][0] -
step;
105 step = (color4[3][1] - color4[0][1] + 1) / 3;
106 color4[1][1] = color4[0][1] +
step;
107 color4[2][1] = color4[3][1] -
step;
110 step = (color4[3][2] - color4[0][2] + 1) / 3;
111 color4[1][2] = color4[0][2] +
step;
112 color4[2][2] = color4[3][2] -
step;
157 static int diff_colors(
const uint8_t *colorA,
const uint8_t *colorB)
161 tot =
SQR(colorA[0] - colorB[0]);
162 tot +=
SQR(colorA[1] - colorB[1]);
163 tot +=
SQR(colorA[2] - colorB[2]);
199 uint8_t min_r, max_r, min_g, max_g, min_b, max_b;
203 min_r = min_g = min_b = UINT8_MAX;
204 max_r = max_g = max_b = 0;
210 min_r =
FFMIN(
R(block_ptr[x]), min_r);
211 min_g =
FFMIN(
G(block_ptr[x]), min_g);
212 min_b =
FFMIN(
B(block_ptr[x]), min_b);
214 max_r =
FFMAX(
R(block_ptr[x]), max_r);
215 max_g =
FFMAX(
G(block_ptr[x]), max_g);
216 max_b =
FFMAX(
B(block_ptr[x]), max_b);
225 if (
r >
g &&
r >
b) {
229 }
else if (
g >
b &&
g >=
r) {
252 if (
diff >= thresh) {
268 double *slope,
double *y_intercept,
double *correlation_coef)
270 double sumx = 0, sumy = 0, sumx2 = 0, sumy2 = 0, sumxy = 0,
271 sumx_sq = 0, sumy_sq = 0,
tmp, tmp2;
282 x =
GET_CHAN(block_ptr[j], xchannel);
283 y =
GET_CHAN(block_ptr[j], ychannel);
293 sumx_sq = sumx * sumx;
294 tmp = (count * sumx2 - sumx_sq);
300 sumy_sq = sumy * sumy;
302 *slope = (sumx * sumy - sumxy) /
tmp;
303 *y_intercept = (sumy - (*slope) * sumx) / count;
305 tmp2 = count * sumy2 - sumy_sq;
307 *correlation_coef = 0.0;
309 *correlation_coef = (count * sumxy - sumx * sumy) /
320 int min,
int max,
int tmp_min,
int tmp_max,
329 int x_inc, lin_y, lin_x;
330 x =
GET_CHAN(block_ptr[j], xchannel);
331 y =
GET_CHAN(block_ptr[j], ychannel);
338 lin_y = (
int)(tmp_min + (tmp_max - tmp_min) * x_inc / 3.0 + 0.5);
340 err =
FFABS(lin_y - y);
347 err =
FFABS(lin_x - x);
363 int smallest_variance = INT_MAX;
364 uint8_t dithered_color[3];
370 for (
int palette_entry = 0; palette_entry < 4; palette_entry++) {
371 int variance =
diff_colors(dithered_color, colors[palette_entry]);
373 if (variance < smallest_variance) {
374 smallest_variance = variance;
391 uint8_t color4[4][3];
392 uint16_t rounded_max, rounded_min;
400 put_bits(pb, 16, rounded_max & ~0x8000);
402 put_bits(pb, 16, rounded_min | 0x8000);
406 for (y = 0; y < 4; y++) {
407 for (x = 0; x < 4; x++) {
420 uint16_t *dest_pixels,
423 for (
int y = 0; y < 4; y++) {
424 memcpy(dest_pixels, src_pixels, 8);
446 uint8_t min_color[3], uint8_t max_color[3],
447 int *total_rgb,
int *total_pixels,
448 uint8_t avg_color[3],
int first_block)
452 int total_pixels_blk;
455 uint8_t min_color_blk[3], max_color_blk[3];
456 int total_rgb_blk[3];
457 uint8_t avg_color_blk[3];
460 min_color[0] = UINT8_MAX;
461 min_color[1] = UINT8_MAX;
462 min_color[2] = UINT8_MAX;
470 threshold =
s->start_one_color_thresh;
472 threshold =
s->continue_one_color_thresh;
479 min_color_blk[0] = min_color[0];
480 min_color_blk[1] = min_color[1];
481 min_color_blk[2] = min_color[2];
482 max_color_blk[0] = max_color[0];
483 max_color_blk[1] = max_color[1];
484 max_color_blk[2] = max_color[2];
485 total_rgb_blk[0] = total_rgb[0];
486 total_rgb_blk[1] = total_rgb[1];
487 total_rgb_blk[2] = total_rgb[2];
495 total_rgb_blk[0] +=
R(
block[x]);
496 total_rgb_blk[1] +=
G(
block[x]);
497 total_rgb_blk[2] +=
B(
block[x]);
499 min_color_blk[0] =
FFMIN(
R(
block[x]), min_color_blk[0]);
500 min_color_blk[1] =
FFMIN(
G(
block[x]), min_color_blk[1]);
501 min_color_blk[2] =
FFMIN(
B(
block[x]), min_color_blk[2]);
503 max_color_blk[0] =
FFMAX(
R(
block[x]), max_color_blk[0]);
504 max_color_blk[1] =
FFMAX(
G(
block[x]), max_color_blk[1]);
505 max_color_blk[2] =
FFMAX(
B(
block[x]), max_color_blk[2]);
513 avg_color_blk[0] = total_rgb_blk[0] / total_pixels_blk;
514 avg_color_blk[1] = total_rgb_blk[1] / total_pixels_blk;
515 avg_color_blk[2] = total_rgb_blk[2] / total_pixels_blk;
520 is_in_range = (max_color_blk[0] - avg_color_blk[0] <= threshold &&
521 max_color_blk[1] - avg_color_blk[1] <= threshold &&
522 max_color_blk[2] - avg_color_blk[2] <= threshold &&
523 avg_color_blk[0] - min_color_blk[0] <= threshold &&
524 avg_color_blk[1] - min_color_blk[1] <= threshold &&
525 avg_color_blk[2] - min_color_blk[2] <= threshold);
531 min_color[0] = min_color_blk[0];
532 min_color[1] = min_color_blk[1];
533 min_color[2] = min_color_blk[2];
534 max_color[0] = max_color_blk[0];
535 max_color[1] = max_color_blk[1];
536 max_color[2] = max_color_blk[2];
537 total_rgb[0] = total_rgb_blk[0];
538 total_rgb[1] = total_rgb_blk[1];
539 total_rgb[2] = total_rgb_blk[2];
540 *total_pixels = total_pixels_blk;
541 avg_color[0] = avg_color_blk[0];
542 avg_color[1] = avg_color_blk[1];
543 avg_color[2] = avg_color_blk[2];
552 int block_counter = 0;
555 int prev_block_offset;
556 int block_offset = 0;
560 int tmp_min, tmp_max;
562 uint8_t avg_color[3];
564 uint8_t min_color[3], max_color[3];
565 double slope, y_intercept, correlation_coef;
566 const uint16_t *src_pixels = (
const uint16_t *)pict->
data[0];
567 uint16_t *prev_pixels = (uint16_t *)
s->prev_frame->data[0];
570 total_blocks = ((
s->frame_width + 3) / 4) * ((
s->frame_height + 3) / 4);
578 while (block_counter < total_blocks) {
582 if (!
s->first_frame) {
584 prev_block_offset = 0;
586 while (n_blocks < 32 && block_counter + n_blocks < total_blocks) {
597 if (prev_block_offset && block_offset - prev_block_offset > 12) {
601 prev_block_offset = block_offset;
604 &src_pixels[block_offset], &bi,
s->skip_frame_thresh) != 0) {
609 put_bits(&
s->pb, 8, 0x80 | (n_blocks - 1));
610 block_counter += n_blocks;
633 put_bits(&
s->pb, 8, 0x80 | (n_blocks - 1));
634 block_counter += n_blocks;
646 min_color, max_color,
647 total_rgb, &pixel_count, avg_color, 1)) {
648 prev_block_offset = block_offset;
654 &prev_pixels[block_offset], &bi, block_counter + n_blocks);
657 while (n_blocks < 32 && block_counter + n_blocks < total_blocks) {
662 if (block_offset - prev_block_offset > 12) {
667 min_color, max_color,
668 total_rgb, &pixel_count, avg_color, 0)) {
672 prev_block_offset = block_offset;
676 &prev_pixels[block_offset], &bi, block_counter + n_blocks);
682 put_bits(&
s->pb, 8, 0xa0 | (n_blocks - 1));
686 block_counter += n_blocks;
701 for (
i = 0;
i < 3;
i++) {
708 slope = y_intercept = correlation_coef = 0;
711 &slope, &y_intercept, &correlation_coef)) {
712 min_color[
i] =
GET_CHAN(src_pixels[block_offset],
i);
713 max_color[
i] =
GET_CHAN(src_pixels[block_offset],
i);
715 tmp_min = (
int)(0.5 +
min * slope + y_intercept);
716 tmp_max = (
int)(0.5 +
max * slope + y_intercept);
724 min,
max, tmp_min, tmp_max, chan,
i), err);
726 min_color[
i] = tmp_min;
727 max_color[
i] = tmp_max;
731 if (err >
s->sixteen_color_thresh) {
732 const uint16_t *row_ptr;
737 row_ptr = &src_pixels[block_offset];
739 for (
int y = 0; y < 4; y++) {
740 for (
int x = 0; x < 4; x++){
741 rgb555 = row_ptr[x] & ~0x8000;
751 &
s->pb, &src_pixels[block_offset], &bi);
756 &prev_pixels[block_offset], &bi, block_counter);
765 s->frame_width = avctx->
width;
766 s->frame_height = avctx->
height;
791 if (!
s->prev_frame->data[0]) {
793 s->prev_frame->format = pict->
format;
794 s->prev_frame->width = pict->
width;
795 s->prev_frame->height = pict->
height;
830 #define OFFSET(x) offsetof(RpzaContext, x)
831 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM