64 #define SQR(x) ((x) * (x))
67 #define GET_CHAN(color, chan) (((color) >> ((chan) * 5) & 0x1F) * 8)
68 #define R(color) GET_CHAN(color, RED)
69 #define G(color) GET_CHAN(color, GREEN)
70 #define B(color) GET_CHAN(color, BLUE)
90 color4[0][0] =
min[0];
91 color4[0][1] =
min[1];
92 color4[0][2] =
min[2];
94 color4[3][0] =
max[0];
95 color4[3][1] =
max[1];
96 color4[3][2] =
max[2];
99 step = (color4[3][0] - color4[0][0] + 1) / 3;
100 color4[1][0] = color4[0][0] +
step;
101 color4[2][0] = color4[3][0] -
step;
104 step = (color4[3][1] - color4[0][1] + 1) / 3;
105 color4[1][1] = color4[0][1] +
step;
106 color4[2][1] = color4[3][1] -
step;
109 step = (color4[3][2] - color4[0][2] + 1) / 3;
110 color4[1][2] = color4[0][2] +
step;
111 color4[2][2] = color4[3][2] -
step;
160 tot =
SQR(colorA[0] - colorB[0]);
161 tot +=
SQR(colorA[1] - colorB[1]);
162 tot +=
SQR(colorA[2] - colorB[2]);
198 uint8_t min_r, max_r, min_g, max_g, min_b, max_b;
202 min_r = min_g = min_b = UINT8_MAX;
203 max_r = max_g = max_b = 0;
209 min_r =
FFMIN(
R(block_ptr[x]), min_r);
210 min_g =
FFMIN(
G(block_ptr[x]), min_g);
211 min_b =
FFMIN(
B(block_ptr[x]), min_b);
213 max_r =
FFMAX(
R(block_ptr[x]), max_r);
214 max_g =
FFMAX(
G(block_ptr[x]), max_g);
215 max_b =
FFMAX(
B(block_ptr[x]), max_b);
224 if (
r >
g &&
r >
b) {
228 }
else if (
g >
b &&
g >=
r) {
250 if (
diff >= thresh) {
266 double *slope,
double *y_intercept,
double *correlation_coef)
268 double sumx = 0, sumy = 0, sumx2 = 0, sumy2 = 0, sumxy = 0,
269 sumx_sq = 0, sumy_sq = 0,
tmp, tmp2;
280 x =
GET_CHAN(block_ptr[j], xchannel);
281 y =
GET_CHAN(block_ptr[j], ychannel);
291 sumx_sq = sumx * sumx;
292 tmp = (count * sumx2 - sumx_sq);
298 sumy_sq = sumy * sumy;
300 *slope = (sumx * sumy - sumxy) /
tmp;
301 *y_intercept = (sumy - (*slope) * sumx) / count;
303 tmp2 = count * sumy2 - sumy_sq;
305 *correlation_coef = 0.0;
307 *correlation_coef = (count * sumxy - sumx * sumy) /
318 int min,
int max,
int tmp_min,
int tmp_max,
327 int x_inc, lin_y, lin_x;
328 x =
GET_CHAN(block_ptr[j], xchannel);
329 y =
GET_CHAN(block_ptr[j], ychannel);
336 lin_y = (
int)(tmp_min + (tmp_max - tmp_min) * x_inc / 3.0 + 0.5);
338 err =
FFABS(lin_y - y);
345 err =
FFABS(lin_x - x);
361 int smallest_variance = INT_MAX;
368 for (
int palette_entry = 0; palette_entry < 4; palette_entry++) {
369 int variance =
diff_colors(dithered_color, colors[palette_entry]);
371 if (variance < smallest_variance) {
372 smallest_variance = variance;
390 uint16_t rounded_max, rounded_min;
398 put_bits(pb, 16, rounded_max & ~0x8000);
400 put_bits(pb, 16, rounded_min | 0x8000);
404 for (y = 0; y < 4; y++) {
405 for (x = 0; x < 4; x++) {
418 uint16_t *dest_pixels,
421 for (
int y = 0; y < 4; y++) {
422 memcpy(dest_pixels, src_pixels, 8);
445 int *total_rgb,
int *total_pixels,
446 uint8_t avg_color[3],
int first_block)
450 int total_pixels_blk;
453 uint8_t min_color_blk[3], max_color_blk[3];
454 int total_rgb_blk[3];
458 min_color[0] = UINT8_MAX;
459 min_color[1] = UINT8_MAX;
460 min_color[2] = UINT8_MAX;
468 threshold =
s->start_one_color_thresh;
470 threshold =
s->continue_one_color_thresh;
477 min_color_blk[0] = min_color[0];
478 min_color_blk[1] = min_color[1];
479 min_color_blk[2] = min_color[2];
480 max_color_blk[0] = max_color[0];
481 max_color_blk[1] = max_color[1];
482 max_color_blk[2] = max_color[2];
483 total_rgb_blk[0] = total_rgb[0];
484 total_rgb_blk[1] = total_rgb[1];
485 total_rgb_blk[2] = total_rgb[2];
493 total_rgb_blk[0] +=
R(
block[x]);
494 total_rgb_blk[1] +=
G(
block[x]);
495 total_rgb_blk[2] +=
B(
block[x]);
497 min_color_blk[0] =
FFMIN(
R(
block[x]), min_color_blk[0]);
498 min_color_blk[1] =
FFMIN(
G(
block[x]), min_color_blk[1]);
499 min_color_blk[2] =
FFMIN(
B(
block[x]), min_color_blk[2]);
501 max_color_blk[0] =
FFMAX(
R(
block[x]), max_color_blk[0]);
502 max_color_blk[1] =
FFMAX(
G(
block[x]), max_color_blk[1]);
503 max_color_blk[2] =
FFMAX(
B(
block[x]), max_color_blk[2]);
511 avg_color_blk[0] = total_rgb_blk[0] / total_pixels_blk;
512 avg_color_blk[1] = total_rgb_blk[1] / total_pixels_blk;
513 avg_color_blk[2] = total_rgb_blk[2] / total_pixels_blk;
518 is_in_range = (max_color_blk[0] - avg_color_blk[0] <= threshold &&
519 max_color_blk[1] - avg_color_blk[1] <= threshold &&
520 max_color_blk[2] - avg_color_blk[2] <= threshold &&
521 avg_color_blk[0] - min_color_blk[0] <= threshold &&
522 avg_color_blk[1] - min_color_blk[1] <= threshold &&
523 avg_color_blk[2] - min_color_blk[2] <= threshold);
529 min_color[0] = min_color_blk[0];
530 min_color[1] = min_color_blk[1];
531 min_color[2] = min_color_blk[2];
532 max_color[0] = max_color_blk[0];
533 max_color[1] = max_color_blk[1];
534 max_color[2] = max_color_blk[2];
535 total_rgb[0] = total_rgb_blk[0];
536 total_rgb[1] = total_rgb_blk[1];
537 total_rgb[2] = total_rgb_blk[2];
538 *total_pixels = total_pixels_blk;
539 avg_color[0] = avg_color_blk[0];
540 avg_color[1] = avg_color_blk[1];
541 avg_color[2] = avg_color_blk[2];
550 int block_counter = 0;
553 int prev_block_offset;
554 int block_offset = 0;
558 int tmp_min, tmp_max;
562 uint8_t min_color[3], max_color[3];
563 double slope, y_intercept, correlation_coef;
564 uint16_t *src_pixels = (uint16_t *)pict->
data[0];
565 uint16_t *prev_pixels = (uint16_t *)
s->prev_frame->data[0];
568 total_blocks = ((
s->frame_width + 3) / 4) * ((
s->frame_height + 3) / 4);
576 while (block_counter < total_blocks) {
580 if (!
s->first_frame) {
582 prev_block_offset = 0;
584 while (n_blocks < 32 && block_counter + n_blocks < total_blocks) {
595 if (prev_block_offset && block_offset - prev_block_offset > 12) {
599 prev_block_offset = block_offset;
602 &src_pixels[block_offset], &bi,
s->skip_frame_thresh) != 0) {
607 put_bits(&
s->pb, 8, 0x80 | (n_blocks - 1));
608 block_counter += n_blocks;
631 put_bits(&
s->pb, 8, 0x80 | (n_blocks - 1));
632 block_counter += n_blocks;
644 min_color, max_color,
645 total_rgb, &pixel_count, avg_color, 1)) {
646 prev_block_offset = block_offset;
652 &prev_pixels[block_offset], &bi, block_counter + n_blocks);
655 while (n_blocks < 32 && block_counter + n_blocks < total_blocks) {
660 if (block_offset - prev_block_offset > 12) {
665 min_color, max_color,
666 total_rgb, &pixel_count, avg_color, 0)) {
670 prev_block_offset = block_offset;
674 &prev_pixels[block_offset], &bi, block_counter + n_blocks);
680 put_bits(&
s->pb, 8, 0xa0 | (n_blocks - 1));
684 block_counter += n_blocks;
699 for (
i = 0;
i < 3;
i++) {
706 slope = y_intercept = correlation_coef = 0;
709 &slope, &y_intercept, &correlation_coef)) {
710 min_color[
i] =
GET_CHAN(src_pixels[block_offset],
i);
711 max_color[
i] =
GET_CHAN(src_pixels[block_offset],
i);
713 tmp_min = (
int)(0.5 +
min * slope + y_intercept);
714 tmp_max = (
int)(0.5 +
max * slope + y_intercept);
722 min,
max, tmp_min, tmp_max, chan,
i), err);
724 min_color[
i] = tmp_min;
725 max_color[
i] = tmp_max;
729 if (err >
s->sixteen_color_thresh) {
735 row_ptr = &src_pixels[block_offset];
737 for (
int y = 0; y < 4; y++) {
738 for (
int x = 0; x < 4; x++){
739 rgb555 = row_ptr[x] & ~0x8000;
749 &
s->pb, &src_pixels[block_offset], &bi);
754 &prev_pixels[block_offset], &bi, block_counter);
763 s->frame_width = avctx->
width;
764 s->frame_height = avctx->
height;
789 if (!
s->prev_frame->data[0]) {
791 s->prev_frame->format = pict->
format;
792 s->prev_frame->width = pict->
width;
793 s->prev_frame->height = pict->
height;
828 #define OFFSET(x) offsetof(RpzaContext, x)
829 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM