66 #define SQR(x) ((x) * (x))
69 #define GET_CHAN(color, chan) (((color) >> ((chan) * 5) & 0x1F))
70 #define R(color) GET_CHAN(color, RED)
71 #define G(color) GET_CHAN(color, GREEN)
72 #define B(color) GET_CHAN(color, BLUE)
93 color4[0][0] =
min[0];
94 color4[0][1] =
min[1];
95 color4[0][2] =
min[2];
97 color4[3][0] =
max[0];
98 color4[3][1] =
max[1];
99 color4[3][2] =
max[2];
102 step = (color4[3][0] - color4[0][0] + 1) / 3;
103 color4[1][0] = color4[0][0] +
step;
104 color4[2][0] = color4[3][0] -
step;
107 step = (color4[3][1] - color4[0][1] + 1) / 3;
108 color4[1][1] = color4[0][1] +
step;
109 color4[2][1] = color4[3][1] -
step;
112 step = (color4[3][2] - color4[0][2] + 1) / 3;
113 color4[1][2] = color4[0][2] +
step;
114 color4[2][2] = color4[3][2] -
step;
159 static int diff_colors(
const uint8_t *colorA,
const uint8_t *colorB)
163 tot =
SQR(colorA[0] - colorB[0]);
164 tot +=
SQR(colorA[1] - colorB[1]);
165 tot +=
SQR(colorA[2] - colorB[2]);
201 uint8_t min_r, max_r, min_g, max_g, min_b, max_b;
205 min_r = min_g = min_b = UINT8_MAX;
206 max_r = max_g = max_b = 0;
212 min_r =
FFMIN(
R(block_ptr[x]), min_r);
213 min_g =
FFMIN(
G(block_ptr[x]), min_g);
214 min_b =
FFMIN(
B(block_ptr[x]), min_b);
216 max_r =
FFMAX(
R(block_ptr[x]), max_r);
217 max_g =
FFMAX(
G(block_ptr[x]), max_g);
218 max_b =
FFMAX(
B(block_ptr[x]), max_b);
227 if (
r >
g &&
r >
b) {
231 }
else if (
g >
b &&
g >=
r) {
254 if (
diff >= thresh) {
270 int *slope,
int *y_intercept,
int *correlation_coef)
272 int sumx = 0, sumy = 0, sumx2 = 0, sumy2 = 0, sumxy = 0,
273 sumx_sq = 0, sumy_sq = 0,
tmp, tmp2;
284 x =
GET_CHAN(block_ptr[j], xchannel);
285 y =
GET_CHAN(block_ptr[j], ychannel);
295 sumx_sq = sumx * sumx;
296 tmp = (count * sumx2 - sumx_sq);
302 sumy_sq = sumy * sumy;
304 *slope = (sumx * sumy - sumxy) /
tmp;
305 *y_intercept = (sumy - (*slope) * sumx) / count;
307 tmp2 = count * sumy2 - sumy_sq;
309 *correlation_coef = 0;
311 *correlation_coef = (count * sumxy - sumx * sumy) /
322 int min,
int max,
int tmp_min,
int tmp_max,
331 int x_inc, lin_y, lin_x;
332 x =
GET_CHAN(block_ptr[j], xchannel);
333 y =
GET_CHAN(block_ptr[j], ychannel);
340 lin_y = tmp_min + (tmp_max - tmp_min) * x_inc / 3 + 1;
342 err =
FFABS(lin_y - y);
347 lin_x =
min + (
max -
min) * x_inc / 3 + 1;
349 err =
FFABS(lin_x - x);
365 int smallest_variance = INT_MAX;
366 uint8_t dithered_color[3];
372 for (
int palette_entry = 0; palette_entry < 4; palette_entry++) {
373 int variance =
diff_colors(dithered_color, colors[palette_entry]);
375 if (variance < smallest_variance) {
376 smallest_variance = variance;
394 uint8_t color4[4][3];
395 uint16_t rounded_max, rounded_min;
404 put_bits(pb, 16, rounded_max & ~0x8000);
406 put_bits(pb, 16, rounded_min | 0x8000);
410 for (
int y = 0; y < y_size; y++) {
411 for (
int x = 0; x < x_size; x++) {
416 for (
int x = x_size; x < 4; x++)
421 for (
int y = y_size; y < 4; y++) {
422 for (
int x = 0; x < 4; x++)
432 uint16_t *dest_pixels,
438 for (
int y = 0; y < y_size; y++) {
439 memcpy(dest_pixels, src_pixels, x_size);
461 uint8_t min_color[3], uint8_t max_color[3],
462 int *total_rgb,
int *total_pixels,
463 uint8_t avg_color[3],
int first_block)
467 int total_pixels_blk;
470 uint8_t min_color_blk[3], max_color_blk[3];
471 int total_rgb_blk[3];
472 uint8_t avg_color_blk[3];
475 min_color[0] = UINT8_MAX;
476 min_color[1] = UINT8_MAX;
477 min_color[2] = UINT8_MAX;
485 threshold =
s->start_one_color_thresh;
487 threshold =
s->continue_one_color_thresh;
494 min_color_blk[0] = min_color[0];
495 min_color_blk[1] = min_color[1];
496 min_color_blk[2] = min_color[2];
497 max_color_blk[0] = max_color[0];
498 max_color_blk[1] = max_color[1];
499 max_color_blk[2] = max_color[2];
500 total_rgb_blk[0] = total_rgb[0];
501 total_rgb_blk[1] = total_rgb[1];
502 total_rgb_blk[2] = total_rgb[2];
510 total_rgb_blk[0] +=
R(
block[x]);
511 total_rgb_blk[1] +=
G(
block[x]);
512 total_rgb_blk[2] +=
B(
block[x]);
514 min_color_blk[0] =
FFMIN(
R(
block[x]), min_color_blk[0]);
515 min_color_blk[1] =
FFMIN(
G(
block[x]), min_color_blk[1]);
516 min_color_blk[2] =
FFMIN(
B(
block[x]), min_color_blk[2]);
518 max_color_blk[0] =
FFMAX(
R(
block[x]), max_color_blk[0]);
519 max_color_blk[1] =
FFMAX(
G(
block[x]), max_color_blk[1]);
520 max_color_blk[2] =
FFMAX(
B(
block[x]), max_color_blk[2]);
528 avg_color_blk[0] = total_rgb_blk[0] / total_pixels_blk;
529 avg_color_blk[1] = total_rgb_blk[1] / total_pixels_blk;
530 avg_color_blk[2] = total_rgb_blk[2] / total_pixels_blk;
535 is_in_range = (max_color_blk[0] - avg_color_blk[0] <= threshold &&
536 max_color_blk[1] - avg_color_blk[1] <= threshold &&
537 max_color_blk[2] - avg_color_blk[2] <= threshold &&
538 avg_color_blk[0] - min_color_blk[0] <= threshold &&
539 avg_color_blk[1] - min_color_blk[1] <= threshold &&
540 avg_color_blk[2] - min_color_blk[2] <= threshold);
546 min_color[0] = min_color_blk[0];
547 min_color[1] = min_color_blk[1];
548 min_color[2] = min_color_blk[2];
549 max_color[0] = max_color_blk[0];
550 max_color[1] = max_color_blk[1];
551 max_color[2] = max_color_blk[2];
552 total_rgb[0] = total_rgb_blk[0];
553 total_rgb[1] = total_rgb_blk[1];
554 total_rgb[2] = total_rgb_blk[2];
555 *total_pixels = total_pixels_blk;
556 avg_color[0] = avg_color_blk[0];
557 avg_color[1] = avg_color_blk[1];
558 avg_color[2] = avg_color_blk[2];
567 int block_counter = 0;
570 int prev_block_offset;
571 int block_offset = 0;
572 int pblock_offset = 0;
576 int tmp_min, tmp_max;
578 uint8_t avg_color[3];
580 uint8_t min_color[3], max_color[3];
581 int slope, y_intercept, correlation_coef;
582 const uint16_t *src_pixels = (
const uint16_t *)pict->
data[0];
583 uint16_t *prev_pixels = (uint16_t *)
s->prev_frame->data[0];
586 total_blocks = ((
s->frame_width + 3) / 4) * ((
s->frame_height + 3) / 4);
595 while (block_counter < total_blocks) {
599 if (!
s->first_frame) {
601 prev_block_offset = 0;
603 while (n_blocks < 32 && block_counter + n_blocks < total_blocks) {
614 if (prev_block_offset && block_offset - prev_block_offset > 12) {
618 prev_block_offset = block_offset;
621 &src_pixels[block_offset], &bi,
s->skip_frame_thresh) != 0) {
626 put_bits(&
s->pb, 8, 0x80 | (n_blocks - 1));
627 block_counter += n_blocks;
650 put_bits(&
s->pb, 8, 0x80 | (n_blocks - 1));
651 block_counter += n_blocks;
664 min_color, max_color,
665 total_rgb, &pixel_count, avg_color, 1)) {
666 prev_block_offset = block_offset;
672 &prev_pixels[pblock_offset], &bi, block_counter + n_blocks);
675 while (n_blocks < 32 && block_counter + n_blocks < total_blocks) {
681 if (block_offset - prev_block_offset > 12) {
686 min_color, max_color,
687 total_rgb, &pixel_count, avg_color, 0)) {
691 prev_block_offset = block_offset;
695 &prev_pixels[pblock_offset], &bi, block_counter + n_blocks);
701 put_bits(&
s->pb, 8, 0xa0 | (n_blocks - 1));
705 block_counter += n_blocks;
720 for (
i = 0;
i < 3;
i++) {
727 slope = y_intercept = correlation_coef = 0;
730 &slope, &y_intercept, &correlation_coef)) {
731 min_color[
i] =
GET_CHAN(src_pixels[block_offset],
i);
732 max_color[
i] =
GET_CHAN(src_pixels[block_offset],
i);
734 tmp_min = 1 +
min * slope + y_intercept;
735 tmp_max = 1 +
max * slope + y_intercept;
743 min,
max, tmp_min, tmp_max, chan,
i), err);
745 min_color[
i] = tmp_min;
746 max_color[
i] = tmp_max;
750 if (err >
s->sixteen_color_thresh) {
751 const uint16_t *row_ptr;
752 int y_size, x_size, rgb555;
757 row_ptr = &src_pixels[block_offset];
761 for (
int y = 0; y < y_size; y++) {
762 for (
int x = 0; x < x_size; x++) {
763 rgb555 = row_ptr[x] & ~0x8000;
767 for (
int x = x_size; x < 4; x++)
773 for (
int y = y_size; y < 4; y++) {
774 for (
int x = 0; x < 4; x++)
781 &
s->pb, &src_pixels[block_offset], &bi);
786 &prev_pixels[pblock_offset], &bi, block_counter);
795 s->frame_width = avctx->
width;
796 s->frame_height = avctx->
height;
806 const AVFrame *pict,
int *got_packet)
820 if (!
s->prev_frame->data[0]) {
822 s->prev_frame->format = pict->
format;
823 s->prev_frame->width = pict->
width;
824 s->prev_frame->height = pict->
height;
859 #define OFFSET(x) offsetof(RpzaContext, x)
860 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM