38 #define COLORS_PER_TABLE 256
56 #define ADVANCE_BLOCK(pixel_ptr, row_ptr, nb_blocks) \
58 for (int block = 0; block < nb_blocks && pixel_ptr && row_ptr; block++) { \
61 if (pixel_ptr - row_ptr >= width) \
63 row_ptr += stride * 4; \
64 pixel_ptr = row_ptr; \
73 const uint8_t *aa =
a, *bb =
b;
79 uint8_t *distinct_values,
84 distinct_values[0] = block_values[0];
85 for (
int i = 1;
i <
size;
i++) {
86 if (block_values[
i] != block_values[
i-1]) {
87 distinct_values[n] = block_values[
i];
95 #define CACHE_PAIR(x) \
96 (s->color_pairs[i][0] == distinct_values[x] || \
97 s->color_pairs[i][1] == distinct_values[x])
99 #define CACHE_QUAD(x) \
100 (s->color_quads[i][0] == distinct_values[x] || \
101 s->color_quads[i][1] == distinct_values[x] || \
102 s->color_quads[i][2] == distinct_values[x] || \
103 s->color_quads[i][3] == distinct_values[x])
105 #define CACHE_OCTET(x) \
106 (s->color_octets[i][0] == distinct_values[x] || \
107 s->color_octets[i][1] == distinct_values[x] || \
108 s->color_octets[i][2] == distinct_values[x] || \
109 s->color_octets[i][3] == distinct_values[x] || \
110 s->color_octets[i][4] == distinct_values[x] || \
111 s->color_octets[i][5] == distinct_values[x] || \
112 s->color_octets[i][6] == distinct_values[x] || \
113 s->color_octets[i][7] == distinct_values[x])
118 const uint8_t *src_pixels = (
const uint8_t *)
frame->data[0];
120 const uint8_t *prev_pixels = (
const uint8_t *)
s->prev_frame->data[0];
121 const ptrdiff_t prev_stride =
s->prev_frame->linesize[0];
122 uint8_t *distinct_values =
s->distinct_values;
123 const uint8_t *pixel_ptr, *row_ptr;
126 int block_counter = 0;
127 int color_pair_index = 0;
128 int color_quad_index = 0;
129 int color_octet_index = 0;
130 int color_table_index;
136 total_blocks = ((
width + 3) / 4) * ((
height + 3) / 4);
138 pixel_ptr = row_ptr = src_pixels;
140 while (block_counter < total_blocks) {
141 const uint8_t *xpixel_ptr = pixel_ptr;
142 const uint8_t *xrow_ptr = row_ptr;
143 int intra_skip_blocks = 0;
144 int inter_skip_blocks = 0;
145 int coded_distinct = 0;
146 int coded_blocks = 0;
153 while (prev_pixels &&
s->key_frame == 0 && block_counter + inter_skip_blocks < total_blocks) {
158 for (
int y = 0; y < y_size; y++) {
159 const uint8_t *prev_pixel_ptr = prev_pixels + (y + cur_y) * prev_stride + cur_x;
161 compare |= !!memcmp(prev_pixel_ptr, pixel_ptr + y *
stride, x_size);
170 if (inter_skip_blocks >= 256)
176 pixel_ptr = xpixel_ptr;
181 while (block_counter > 0 && block_counter + intra_skip_blocks < total_blocks) {
184 const ptrdiff_t
offset = xpixel_ptr - src_pixels;
187 const int ny = sx < 4 ?
FFMAX(sy - 4, 0) : sy;
189 const uint8_t *old_pixel_ptr = src_pixels + nx + ny *
stride;
192 for (
int y = 0; y < y_size; y++) {
202 if (intra_skip_blocks >= 256)
208 pixel_ptr = xpixel_ptr;
213 while (block_counter + coded_blocks < total_blocks && coded_blocks < 256) {
216 const int nb_elements = x_size * y_size;
217 uint8_t block_values[16] = { 0 };
218 for (
int y = 0; y < y_size; y++)
219 memcpy(block_values + y * x_size, pixel_ptr + y *
stride, x_size);
221 qsort(block_values, nb_elements,
sizeof(block_values[0]),
smc_cmp_values);
223 if (coded_blocks == 0) {
224 memcpy(distinct_values,
s->next_distinct_values,
sizeof(
s->distinct_values));
225 s->nb_distinct =
s->next_nb_distinct;
227 if (
s->next_nb_distinct !=
s->nb_distinct ||
228 memcmp(distinct_values,
s->next_distinct_values,
s->nb_distinct)) {
232 s->mono_value = block_values[0];
234 coded_distinct =
s->nb_distinct;
236 if (coded_distinct > 1 && coded_blocks >= 16)
242 pixel_ptr = xpixel_ptr;
247 blocks = coded_distinct <= 8 ? coded_blocks : 0;
248 distinct = coded_distinct;
250 if (intra_skip_blocks >= blocks && intra_skip_blocks >= inter_skip_blocks) {
252 blocks = intra_skip_blocks;
255 if (intra_skip_blocks > 16 && intra_skip_blocks >= inter_skip_blocks &&
256 intra_skip_blocks >= blocks) {
258 blocks = intra_skip_blocks;
261 if (inter_skip_blocks >= blocks && inter_skip_blocks > intra_skip_blocks) {
263 blocks = inter_skip_blocks;
266 if (inter_skip_blocks > 16 && inter_skip_blocks > intra_skip_blocks &&
267 inter_skip_blocks >= blocks) {
269 blocks = inter_skip_blocks;
273 blocks = coded_blocks;
274 distinct = coded_distinct;
280 bytestream2_put_byte(pb, 0x60 | (blocks - 1));
282 bytestream2_put_byte(pb, 0x70);
283 bytestream2_put_byte(pb, blocks - 1);
285 bytestream2_put_byte(pb,
s->mono_value);
298 if (cache_index >= 0) {
299 bytestream2_put_byte(pb, 0x90 | (blocks - 1));
300 bytestream2_put_byte(pb, cache_index);
301 color_table_index = cache_index;
303 bytestream2_put_byte(pb, 0x80 | (blocks - 1));
305 color_table_index = color_pair_index;
307 s->color_pairs[color_table_index][
i] = distinct_values[
i];
308 bytestream2_put_byte(pb, distinct_values[
i]);
313 color_pair_index = 0;
316 for (
int i = 0;
i < blocks;
i++) {
319 uint8_t
value =
s->color_pairs[color_table_index][1];
323 for (
int y = 0; y < y_size; y++) {
324 for (
int x = 0; x < x_size; x++) {
331 bytestream2_put_be16(pb,
flags);
349 if (cache_index >= 0) {
350 bytestream2_put_byte(pb, 0xB0 | (blocks - 1));
351 bytestream2_put_byte(pb, cache_index);
352 color_table_index = cache_index;
354 bytestream2_put_byte(pb, 0xA0 | (blocks - 1));
356 color_table_index = color_quad_index;
358 s->color_quads[color_table_index][
i] = distinct_values[
i];
359 bytestream2_put_byte(pb, distinct_values[
i]);
364 color_quad_index = 0;
367 for (
int i = 0;
i < blocks;
i++) {
374 for (
int k = 0; k < 4; k++)
375 quad[k] =
s->color_quads[color_table_index][k];
377 for (
int y = 0; y < y_size; y++) {
378 for (
int x = 0; x < x_size; x++) {
393 shift -= 2 * (4 - x_size);
396 bytestream2_put_be32(pb,
flags);
420 if (cache_index >= 0) {
421 bytestream2_put_byte(pb, 0xD0 | (blocks - 1));
422 bytestream2_put_byte(pb, cache_index);
423 color_table_index = cache_index;
425 bytestream2_put_byte(pb, 0xC0 | (blocks - 1));
427 color_table_index = color_octet_index;
429 s->color_octets[color_table_index][
i] = distinct_values[
i];
430 bytestream2_put_byte(pb, distinct_values[
i]);
435 color_octet_index = 0;
438 for (
int i = 0;
i < blocks;
i++) {
445 for (
int k = 0; k < 8; k++)
446 octet[k] =
s->color_octets[color_table_index][k];
448 for (
int y = 0; y < y_size; y++) {
449 for (
int x = 0; x < x_size; x++) {
464 shift -= 3 * (4 - x_size);
467 bytestream2_put_be16(pb, ((
flags >> 32) & 0xFFF0) | ((
flags >> 8) & 0xF));
468 bytestream2_put_be16(pb, ((
flags >> 20) & 0xFFF0) | ((
flags >> 4) & 0xF));
469 bytestream2_put_be16(pb, ((
flags >> 8) & 0xFFF0) | ((
flags >> 0) & 0xF));
475 bytestream2_put_byte(pb, 0xE0 | (blocks - 1));
476 for (
int i = 0;
i < blocks;
i++) {
479 for (
int y = 0; y < y_size; y++) {
480 for (
int x = 0; x < x_size; x++)
481 bytestream2_put_byte(pb, pixel_ptr[x + y *
stride]);
482 for (
int x = x_size; x < 4; x++)
483 bytestream2_put_byte(pb, 0);
486 for (
int y = y_size; y < 4; y++) {
487 for (
int x = 0; x < 4; x++)
488 bytestream2_put_byte(pb, 0);
495 bytestream2_put_byte(pb, 0x20 | (blocks - 1));
499 bytestream2_put_byte(pb, 0x30);
500 bytestream2_put_byte(pb, blocks - 1);
504 bytestream2_put_byte(pb, 0x00 | (blocks - 1));
508 bytestream2_put_byte(pb, 0x10);
509 bytestream2_put_byte(pb, blocks - 1);
514 block_counter += blocks;
544 if (avctx->
gop_size == 0 || !
s->prev_frame->data[0] ||
553 bytestream2_put_be32(&pb, 0x00);