76 #define QUALITY_THRESHOLD 100
77 #define THRESHOLD_MULTIPLIER 0.6
84 for (i = 0; i <
size; i++)
85 score += (pix1[i] - pix2[i]) * (pix1[i] - pix2[i]);
91 int threshold,
int lambda,
int intra)
93 int count,
y, x, i, j,
split, best_mean, best_score, best_count;
95 int block_sum[7] = { 0, 0, 0, 0, 0, 0 };
96 int w = 2 << (level + 2 >> 1);
97 int h = 2 << (level + 1 >> 1);
100 const int8_t *codebook_sum, *codebook;
101 const uint16_t(*mean_vlc)[2];
102 const uint8_t(*multistage_vlc)[2];
111 for (y = 0; y < h; y++) {
112 for (x = 0; x < w; x++) {
113 int v = src[x + y * stride];
124 for (y = 0; y < h; y++) {
125 for (x = 0; x < w; x++) {
126 int v = src[x + y * stride] - ref[x + y * stride];
135 best_score -= (int)((
unsigned)block_sum[0] * block_sum[0] >> (level + 3));
136 best_mean = block_sum[0] + (size >> 1) >> (level + 3);
139 for (count = 1; count < 7; count++) {
140 int best_vector_score = INT_MAX;
141 int best_vector_sum = -999, best_vector_mean = -999;
142 const int stage = count - 1;
143 const int8_t *vector;
145 for (i = 0; i < 16; i++) {
146 int sum = codebook_sum[stage * 16 + i];
147 int sqr, diff, score;
149 vector = codebook + stage * size * 16 + i *
size;
151 diff = block_sum[stage] - sum;
152 score = sqr - (diff * (int64_t)diff >> (level + 3));
153 if (score < best_vector_score) {
154 int mean = diff + (size >> 1) >> (level + 3);
156 mean = av_clip(mean, intra ? 0 : -256, 255);
157 best_vector_score = score;
158 best_vector[stage] = i;
159 best_vector_sum = sum;
160 best_vector_mean = mean;
164 vector = codebook + stage * size * 16 + best_vector[stage] *
size;
165 for (j = 0; j <
size; j++)
166 block[stage + 1][j] =
block[stage][j] - vector[j];
167 block_sum[stage + 1] = block_sum[stage] - best_vector_sum;
168 best_vector_score += lambda *
170 multistage_vlc[1 +
count][1]
171 + mean_vlc[best_vector_mean][1]);
173 if (best_vector_score < best_score) {
174 best_score = best_vector_score;
176 best_mean = best_vector_mean;
182 if (best_score > threshold && level) {
184 int offset = level & 1 ? stride * h / 2 : w / 2;
187 for (i = level - 1; i >= 0; i--)
189 score +=
encode_block(s, src, ref, decoded, stride, level - 1,
190 threshold >> 1, lambda, intra);
191 score +=
encode_block(s, src + offset, ref + offset, decoded + offset,
192 stride, level - 1, threshold >> 1, lambda, intra);
195 if (score < best_score) {
199 for (i = level - 1; i >= 0; i--)
207 av_assert1(best_mean >= 0 && best_mean < 256 || !intra);
208 av_assert1(best_mean >= -256 && best_mean < 256);
209 av_assert1(best_count >= 0 && best_count < 7);
214 multistage_vlc[1 + best_count][1],
215 multistage_vlc[1 + best_count][0]);
217 mean_vlc[best_mean][0]);
219 for (i = 0; i < best_count; i++) {
220 av_assert2(best_vector[i] >= 0 && best_vector[i] < 16);
224 for (y = 0; y < h; y++)
225 for (x = 0; x < w; x++)
226 decoded[x + y * stride] = src[x + y * stride] -
227 block[best_count][x + w * y] +
244 unsigned char *src_plane,
245 unsigned char *ref_plane,
246 unsigned char *decoded_plane,
252 int block_width, block_height;
261 for (level = 4; level >= 0; level--)
264 block_width = (width + 15) / 16;
265 block_height = (height + 15) / 16;
299 block_height * 2 + 2) *
300 2 *
sizeof(int16_t));
302 (block_height + 2) + 1) *
303 2 *
sizeof(int16_t));
322 for (y = 0; y < block_height; y++) {
326 for (i = 0; i < 16 && i + 16 * y <
height; i++) {
327 memcpy(&src[i * stride], &src_plane[(i + 16 * y) * src_stride],
329 for (x = width; x < 16 * block_width; x++)
330 src[i * stride + x] = src[i * stride + x - 1];
332 for (; i < 16 && i + 16 * y < 16 * block_height; i++)
333 memcpy(&src[i * stride], &src[(i - 1) * stride],
336 for (x = 0; x < block_width; x++) {
351 for (y = 0; y < block_height; y++) {
352 for (i = 0; i < 16 && i + 16 * y <
height; i++) {
353 memcpy(&src[i * stride], &src_plane[(i + 16 * y) * src_stride],
355 for (x = width; x < 16 * block_width; x++)
356 src[i * stride + x] = src[i * stride + x - 1];
358 for (; i < 16 && i + 16 * y < 16 * block_height; i++)
359 memcpy(&src[i * stride], &src[(i - 1) * stride], 16 * block_width);
362 for (x = 0; x < block_width; x++) {
363 uint8_t reorder_buffer[2][6][7 * 32];
365 int offset = y * 16 * stride + x * 16;
368 int score[4] = { 0, 0, 0, 0 }, best;
383 for (i = 0; i < 6; i++)
389 score[0] = vlc[1] * lambda;
391 score[0] +=
encode_block(s, src + 16 * x, NULL, temp, stride,
393 for (i = 0; i < 6; i++) {
404 int mx, my, pred_x, pred_y, dxy;
410 for (i = 0; i < 6; i++)
428 dxy = (mx & 1) + 2 * (my & 1);
435 score[1] +=
encode_block(s, src + 16 * x, temp + 16*stride,
436 decoded, stride, 5, 64, lambda, 0);
437 best = score[1] <= score[0];
440 score[2] = s->
mecc.
sse[0](NULL, src + 16 * x, ref,
442 score[2] += vlc[1] * lambda;
443 if (score[2] < score[best] && mx == 0 && my == 0) {
451 for (i = 0; i < 6; i++) {
470 for (i = 5; i >= 0; i--)
500 for (i = 0; i < 3; i++) {
569 const AVFrame *pict,
int *got_packet)
602 for (i = 0; i < 3; i++)