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)
92 color4[0][0] =
min[0];
93 color4[0][1] =
min[1];
94 color4[0][2] =
min[2];
96 color4[3][0] =
max[0];
97 color4[3][1] =
max[1];
98 color4[3][2] =
max[2];
101 step = (color4[3][0] - color4[0][0] + 1) / 3;
102 color4[1][0] = color4[0][0] +
step;
103 color4[2][0] = color4[3][0] -
step;
106 step = (color4[3][1] - color4[0][1] + 1) / 3;
107 color4[1][1] = color4[0][1] +
step;
108 color4[2][1] = color4[3][1] -
step;
111 step = (color4[3][2] - color4[0][2] + 1) / 3;
112 color4[1][2] = color4[0][2] +
step;
113 color4[2][2] = color4[3][2] -
step;
158 static int diff_colors(
const uint8_t *colorA,
const uint8_t *colorB)
162 tot =
SQR(colorA[0] - colorB[0]);
163 tot +=
SQR(colorA[1] - colorB[1]);
164 tot +=
SQR(colorA[2] - colorB[2]);
200 uint8_t min_r, max_r, min_g, max_g, min_b, max_b;
204 min_r = min_g = min_b = UINT8_MAX;
205 max_r = max_g = max_b = 0;
211 min_r =
FFMIN(
R(block_ptr[x]), min_r);
212 min_g =
FFMIN(
G(block_ptr[x]), min_g);
213 min_b =
FFMIN(
B(block_ptr[x]), min_b);
215 max_r =
FFMAX(
R(block_ptr[x]), max_r);
216 max_g =
FFMAX(
G(block_ptr[x]), max_g);
217 max_b =
FFMAX(
B(block_ptr[x]), max_b);
226 if (
r >
g &&
r >
b) {
230 }
else if (
g >
b &&
g >=
r) {
253 if (
diff >= thresh) {
269 double *slope,
double *y_intercept,
double *correlation_coef)
271 double sumx = 0, sumy = 0, sumx2 = 0, sumy2 = 0, sumxy = 0,
272 sumx_sq = 0, sumy_sq = 0,
tmp, tmp2;
283 x =
GET_CHAN(block_ptr[j], xchannel);
284 y =
GET_CHAN(block_ptr[j], ychannel);
294 sumx_sq = sumx * sumx;
295 tmp = (count * sumx2 - sumx_sq);
301 sumy_sq = sumy * sumy;
303 *slope = (sumx * sumy - sumxy) /
tmp;
304 *y_intercept = (sumy - (*slope) * sumx) / count;
306 tmp2 = count * sumy2 - sumy_sq;
308 *correlation_coef = 0.0;
310 *correlation_coef = (count * sumxy - sumx * sumy) /
321 int min,
int max,
int tmp_min,
int tmp_max,
330 int x_inc, lin_y, lin_x;
331 x =
GET_CHAN(block_ptr[j], xchannel);
332 y =
GET_CHAN(block_ptr[j], ychannel);
339 lin_y = (
int)(tmp_min + (tmp_max - tmp_min) * x_inc / 3.0 + 0.5);
341 err =
FFABS(lin_y - y);
348 err =
FFABS(lin_x - x);
364 int smallest_variance = INT_MAX;
365 uint8_t dithered_color[3];
371 for (
int palette_entry = 0; palette_entry < 4; palette_entry++) {
372 int variance =
diff_colors(dithered_color, colors[palette_entry]);
374 if (variance < smallest_variance) {
375 smallest_variance = variance;
393 uint8_t color4[4][3];
394 uint16_t rounded_max, rounded_min;
403 put_bits(pb, 16, rounded_max & ~0x8000);
405 put_bits(pb, 16, rounded_min | 0x8000);
409 for (
int y = 0; y < y_size; y++) {
410 for (
int x = 0; x < x_size; x++) {
415 for (
int x = x_size; x < 4; x++)
420 for (
int y = y_size; y < 4; y++) {
421 for (
int x = 0; x < 4; x++)
431 uint16_t *dest_pixels,
437 for (
int y = 0; y < y_size; y++) {
438 memcpy(dest_pixels, src_pixels, x_size);
460 uint8_t min_color[3], uint8_t max_color[3],
461 int *total_rgb,
int *total_pixels,
462 uint8_t avg_color[3],
int first_block)
466 int total_pixels_blk;
469 uint8_t min_color_blk[3], max_color_blk[3];
470 int total_rgb_blk[3];
471 uint8_t avg_color_blk[3];
474 min_color[0] = UINT8_MAX;
475 min_color[1] = UINT8_MAX;
476 min_color[2] = UINT8_MAX;
484 threshold =
s->start_one_color_thresh;
486 threshold =
s->continue_one_color_thresh;
493 min_color_blk[0] = min_color[0];
494 min_color_blk[1] = min_color[1];
495 min_color_blk[2] = min_color[2];
496 max_color_blk[0] = max_color[0];
497 max_color_blk[1] = max_color[1];
498 max_color_blk[2] = max_color[2];
499 total_rgb_blk[0] = total_rgb[0];
500 total_rgb_blk[1] = total_rgb[1];
501 total_rgb_blk[2] = total_rgb[2];
509 total_rgb_blk[0] +=
R(
block[x]);
510 total_rgb_blk[1] +=
G(
block[x]);
511 total_rgb_blk[2] +=
B(
block[x]);
513 min_color_blk[0] =
FFMIN(
R(
block[x]), min_color_blk[0]);
514 min_color_blk[1] =
FFMIN(
G(
block[x]), min_color_blk[1]);
515 min_color_blk[2] =
FFMIN(
B(
block[x]), min_color_blk[2]);
517 max_color_blk[0] =
FFMAX(
R(
block[x]), max_color_blk[0]);
518 max_color_blk[1] =
FFMAX(
G(
block[x]), max_color_blk[1]);
519 max_color_blk[2] =
FFMAX(
B(
block[x]), max_color_blk[2]);
527 avg_color_blk[0] = total_rgb_blk[0] / total_pixels_blk;
528 avg_color_blk[1] = total_rgb_blk[1] / total_pixels_blk;
529 avg_color_blk[2] = total_rgb_blk[2] / total_pixels_blk;
534 is_in_range = (max_color_blk[0] - avg_color_blk[0] <= threshold &&
535 max_color_blk[1] - avg_color_blk[1] <= threshold &&
536 max_color_blk[2] - avg_color_blk[2] <= threshold &&
537 avg_color_blk[0] - min_color_blk[0] <= threshold &&
538 avg_color_blk[1] - min_color_blk[1] <= threshold &&
539 avg_color_blk[2] - min_color_blk[2] <= threshold);
545 min_color[0] = min_color_blk[0];
546 min_color[1] = min_color_blk[1];
547 min_color[2] = min_color_blk[2];
548 max_color[0] = max_color_blk[0];
549 max_color[1] = max_color_blk[1];
550 max_color[2] = max_color_blk[2];
551 total_rgb[0] = total_rgb_blk[0];
552 total_rgb[1] = total_rgb_blk[1];
553 total_rgb[2] = total_rgb_blk[2];
554 *total_pixels = total_pixels_blk;
555 avg_color[0] = avg_color_blk[0];
556 avg_color[1] = avg_color_blk[1];
557 avg_color[2] = avg_color_blk[2];
566 int block_counter = 0;
569 int prev_block_offset;
570 int block_offset = 0;
571 int pblock_offset = 0;
575 int tmp_min, tmp_max;
577 uint8_t avg_color[3];
579 uint8_t min_color[3], max_color[3];
580 double slope, y_intercept, correlation_coef;
581 const uint16_t *src_pixels = (
const uint16_t *)pict->
data[0];
582 uint16_t *prev_pixels = (uint16_t *)
s->prev_frame->data[0];
585 total_blocks = ((
s->frame_width + 3) / 4) * ((
s->frame_height + 3) / 4);
594 while (block_counter < total_blocks) {
598 if (!
s->first_frame) {
600 prev_block_offset = 0;
602 while (n_blocks < 32 && block_counter + n_blocks < total_blocks) {
613 if (prev_block_offset && block_offset - prev_block_offset > 12) {
617 prev_block_offset = block_offset;
620 &src_pixels[block_offset], &bi,
s->skip_frame_thresh) != 0) {
625 put_bits(&
s->pb, 8, 0x80 | (n_blocks - 1));
626 block_counter += n_blocks;
649 put_bits(&
s->pb, 8, 0x80 | (n_blocks - 1));
650 block_counter += n_blocks;
663 min_color, max_color,
664 total_rgb, &pixel_count, avg_color, 1)) {
665 prev_block_offset = block_offset;
671 &prev_pixels[pblock_offset], &bi, block_counter + n_blocks);
674 while (n_blocks < 32 && block_counter + n_blocks < total_blocks) {
680 if (block_offset - prev_block_offset > 12) {
685 min_color, max_color,
686 total_rgb, &pixel_count, avg_color, 0)) {
690 prev_block_offset = block_offset;
694 &prev_pixels[pblock_offset], &bi, block_counter + n_blocks);
700 put_bits(&
s->pb, 8, 0xa0 | (n_blocks - 1));
704 block_counter += n_blocks;
719 for (
i = 0;
i < 3;
i++) {
726 slope = y_intercept = correlation_coef = 0;
729 &slope, &y_intercept, &correlation_coef)) {
730 min_color[
i] =
GET_CHAN(src_pixels[block_offset],
i);
731 max_color[
i] =
GET_CHAN(src_pixels[block_offset],
i);
733 tmp_min = (
int)(0.5 +
min * slope + y_intercept);
734 tmp_max = (
int)(0.5 +
max * slope + y_intercept);
742 min,
max, tmp_min, tmp_max, chan,
i), err);
744 min_color[
i] = tmp_min;
745 max_color[
i] = tmp_max;
749 if (err >
s->sixteen_color_thresh) {
750 const uint16_t *row_ptr;
756 row_ptr = &src_pixels[block_offset];
759 for (
int y = 0; y < y_size; y++) {
760 for (
int x = 0; x < 4; x++) {
761 rgb555 = row_ptr[x] & ~0x8000;
768 for (
int y = y_size; y < 4; y++) {
769 for (
int x = 0; x < 4; x++)
776 &
s->pb, &src_pixels[block_offset], &bi);
781 &prev_pixels[pblock_offset], &bi, block_counter);
790 s->frame_width = avctx->
width;
791 s->frame_height = avctx->
height;
801 const AVFrame *pict,
int *got_packet)
815 if (!
s->prev_frame->data[0]) {
817 s->prev_frame->format = pict->
format;
818 s->prev_frame->width = pict->
width;
819 s->prev_frame->height = pict->
height;
854 #define OFFSET(x) offsetof(RpzaContext, x)
855 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM