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;
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) {
251 if (
diff >= thresh) {
267 double *slope,
double *y_intercept,
double *correlation_coef)
269 double sumx = 0, sumy = 0, sumx2 = 0, sumy2 = 0, sumxy = 0,
270 sumx_sq = 0, sumy_sq = 0,
tmp, tmp2;
281 x =
GET_CHAN(block_ptr[j], xchannel);
282 y =
GET_CHAN(block_ptr[j], ychannel);
292 sumx_sq = sumx * sumx;
293 tmp = (count * sumx2 - sumx_sq);
299 sumy_sq = sumy * sumy;
301 *slope = (sumx * sumy - sumxy) /
tmp;
302 *y_intercept = (sumy - (*slope) * sumx) / count;
304 tmp2 = count * sumy2 - sumy_sq;
306 *correlation_coef = 0.0;
308 *correlation_coef = (count * sumxy - sumx * sumy) /
319 int min,
int max,
int tmp_min,
int tmp_max,
328 int x_inc, lin_y, lin_x;
329 x =
GET_CHAN(block_ptr[j], xchannel);
330 y =
GET_CHAN(block_ptr[j], ychannel);
337 lin_y = (
int)(tmp_min + (tmp_max - tmp_min) * x_inc / 3.0 + 0.5);
339 err =
FFABS(lin_y - y);
346 err =
FFABS(lin_x - x);
362 int smallest_variance = INT_MAX;
363 uint8_t dithered_color[3];
369 for (
int palette_entry = 0; palette_entry < 4; palette_entry++) {
370 int variance =
diff_colors(dithered_color, colors[palette_entry]);
372 if (variance < smallest_variance) {
373 smallest_variance = variance;
390 uint8_t color4[4][3];
391 uint16_t rounded_max, rounded_min;
399 put_bits(pb, 16, rounded_max & ~0x8000);
401 put_bits(pb, 16, rounded_min | 0x8000);
405 for (y = 0; y < 4; y++) {
406 for (x = 0; x < 4; x++) {
419 uint16_t *dest_pixels,
422 for (
int y = 0; y < 4; y++) {
423 memcpy(dest_pixels, src_pixels, 8);
445 uint8_t min_color[3], uint8_t max_color[3],
446 int *total_rgb,
int *total_pixels,
447 uint8_t avg_color[3],
int first_block)
451 int total_pixels_blk;
454 uint8_t min_color_blk[3], max_color_blk[3];
455 int total_rgb_blk[3];
456 uint8_t avg_color_blk[3];
459 min_color[0] = UINT8_MAX;
460 min_color[1] = UINT8_MAX;
461 min_color[2] = UINT8_MAX;
469 threshold =
s->start_one_color_thresh;
471 threshold =
s->continue_one_color_thresh;
478 min_color_blk[0] = min_color[0];
479 min_color_blk[1] = min_color[1];
480 min_color_blk[2] = min_color[2];
481 max_color_blk[0] = max_color[0];
482 max_color_blk[1] = max_color[1];
483 max_color_blk[2] = max_color[2];
484 total_rgb_blk[0] = total_rgb[0];
485 total_rgb_blk[1] = total_rgb[1];
486 total_rgb_blk[2] = total_rgb[2];
494 total_rgb_blk[0] +=
R(
block[x]);
495 total_rgb_blk[1] +=
G(
block[x]);
496 total_rgb_blk[2] +=
B(
block[x]);
498 min_color_blk[0] =
FFMIN(
R(
block[x]), min_color_blk[0]);
499 min_color_blk[1] =
FFMIN(
G(
block[x]), min_color_blk[1]);
500 min_color_blk[2] =
FFMIN(
B(
block[x]), min_color_blk[2]);
502 max_color_blk[0] =
FFMAX(
R(
block[x]), max_color_blk[0]);
503 max_color_blk[1] =
FFMAX(
G(
block[x]), max_color_blk[1]);
504 max_color_blk[2] =
FFMAX(
B(
block[x]), max_color_blk[2]);
512 avg_color_blk[0] = total_rgb_blk[0] / total_pixels_blk;
513 avg_color_blk[1] = total_rgb_blk[1] / total_pixels_blk;
514 avg_color_blk[2] = total_rgb_blk[2] / total_pixels_blk;
519 is_in_range = (max_color_blk[0] - avg_color_blk[0] <= threshold &&
520 max_color_blk[1] - avg_color_blk[1] <= threshold &&
521 max_color_blk[2] - avg_color_blk[2] <= threshold &&
522 avg_color_blk[0] - min_color_blk[0] <= threshold &&
523 avg_color_blk[1] - min_color_blk[1] <= threshold &&
524 avg_color_blk[2] - min_color_blk[2] <= threshold);
530 min_color[0] = min_color_blk[0];
531 min_color[1] = min_color_blk[1];
532 min_color[2] = min_color_blk[2];
533 max_color[0] = max_color_blk[0];
534 max_color[1] = max_color_blk[1];
535 max_color[2] = max_color_blk[2];
536 total_rgb[0] = total_rgb_blk[0];
537 total_rgb[1] = total_rgb_blk[1];
538 total_rgb[2] = total_rgb_blk[2];
539 *total_pixels = total_pixels_blk;
540 avg_color[0] = avg_color_blk[0];
541 avg_color[1] = avg_color_blk[1];
542 avg_color[2] = avg_color_blk[2];
551 int block_counter = 0;
554 int prev_block_offset;
555 int block_offset = 0;
559 int tmp_min, tmp_max;
561 uint8_t avg_color[3];
563 uint8_t min_color[3], max_color[3];
564 double slope, y_intercept, correlation_coef;
565 uint16_t *src_pixels = (uint16_t *)pict->
data[0];
566 uint16_t *prev_pixels = (uint16_t *)
s->prev_frame->data[0];
569 total_blocks = ((
s->frame_width + 3) / 4) * ((
s->frame_height + 3) / 4);
577 while (block_counter < total_blocks) {
581 if (!
s->first_frame) {
583 prev_block_offset = 0;
585 while (n_blocks < 32 && block_counter + n_blocks < total_blocks) {
596 if (prev_block_offset && block_offset - prev_block_offset > 12) {
600 prev_block_offset = block_offset;
603 &src_pixels[block_offset], &bi,
s->skip_frame_thresh) != 0) {
608 put_bits(&
s->pb, 8, 0x80 | (n_blocks - 1));
609 block_counter += n_blocks;
632 put_bits(&
s->pb, 8, 0x80 | (n_blocks - 1));
633 block_counter += n_blocks;
645 min_color, max_color,
646 total_rgb, &pixel_count, avg_color, 1)) {
647 prev_block_offset = block_offset;
653 &prev_pixels[block_offset], &bi, block_counter + n_blocks);
656 while (n_blocks < 32 && block_counter + n_blocks < total_blocks) {
661 if (block_offset - prev_block_offset > 12) {
666 min_color, max_color,
667 total_rgb, &pixel_count, avg_color, 0)) {
671 prev_block_offset = block_offset;
675 &prev_pixels[block_offset], &bi, block_counter + n_blocks);
681 put_bits(&
s->pb, 8, 0xa0 | (n_blocks - 1));
685 block_counter += n_blocks;
700 for (
i = 0;
i < 3;
i++) {
707 slope = y_intercept = correlation_coef = 0;
710 &slope, &y_intercept, &correlation_coef)) {
711 min_color[
i] =
GET_CHAN(src_pixels[block_offset],
i);
712 max_color[
i] =
GET_CHAN(src_pixels[block_offset],
i);
714 tmp_min = (
int)(0.5 +
min * slope + y_intercept);
715 tmp_max = (
int)(0.5 +
max * slope + y_intercept);
723 min,
max, tmp_min, tmp_max, chan,
i), err);
725 min_color[
i] = tmp_min;
726 max_color[
i] = tmp_max;
730 if (err >
s->sixteen_color_thresh) {
736 row_ptr = &src_pixels[block_offset];
738 for (
int y = 0; y < 4; y++) {
739 for (
int x = 0; x < 4; x++){
740 rgb555 = row_ptr[x] & ~0x8000;
750 &
s->pb, &src_pixels[block_offset], &bi);
755 &prev_pixels[block_offset], &bi, block_counter);
764 s->frame_width = avctx->
width;
765 s->frame_height = avctx->
height;
790 if (!
s->prev_frame->data[0]) {
792 s->prev_frame->format = pict->
format;
793 s->prev_frame->width = pict->
width;
794 s->prev_frame->height = pict->
height;
829 #define OFFSET(x) offsetof(RpzaContext, x)
830 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM