112 #define QUALITY_THRESHOLD 100
113 #define THRESHOLD_MULTIPLIER 0.6
117 int threshold,
int lambda,
int intra)
119 int count,
y, x, i, j,
split, best_mean, best_score, best_count;
121 int block_sum[7] = { 0, 0, 0, 0, 0, 0 };
122 int w = 2 << (level + 2 >> 1);
123 int h = 2 << (level + 1 >> 1);
125 int16_t
block[7][256];
126 const int8_t *codebook_sum, *codebook;
127 const uint16_t(*mean_vlc)[2];
128 const uint8_t(*multistage_vlc)[2];
137 for (y = 0; y < h; y++) {
138 for (x = 0; x < w; x++) {
139 int v = src[x + y * stride];
140 block[0][x + w *
y] =
v;
150 for (y = 0; y < h; y++) {
151 for (x = 0; x < w; x++) {
152 int v = src[x + y * stride] - ref[x + y * stride];
153 block[0][x + w *
y] =
v;
161 best_score -= (int)((
unsigned)block_sum[0] * block_sum[0] >> (level + 3));
162 best_mean = block_sum[0] + (size >> 1) >> (level + 3);
165 for (count = 1; count < 7; count++) {
166 int best_vector_score = INT_MAX;
167 int best_vector_sum = -999, best_vector_mean = -999;
168 const int stage = count - 1;
169 const int8_t *vector;
171 for (i = 0; i < 16; i++) {
172 int sum = codebook_sum[stage * 16 + i];
173 int sqr, diff, score;
175 vector = codebook + stage * size * 16 + i *
size;
177 diff = block_sum[stage] - sum;
178 score = sqr - (diff * (int64_t)diff >> (level + 3));
179 if (score < best_vector_score) {
180 int mean = diff + (size >> 1) >> (level + 3);
182 mean = av_clip(mean, intra ? 0 : -256, 255);
183 best_vector_score = score;
184 best_vector[stage] = i;
185 best_vector_sum = sum;
186 best_vector_mean = mean;
190 vector = codebook + stage * size * 16 + best_vector[stage] *
size;
191 for (j = 0; j <
size; j++)
192 block[stage + 1][j] = block[stage][j] - vector[j];
193 block_sum[stage + 1] = block_sum[stage] - best_vector_sum;
194 best_vector_score += lambda *
196 multistage_vlc[1 +
count][1]
197 + mean_vlc[best_vector_mean][1]);
199 if (best_vector_score < best_score) {
200 best_score = best_vector_score;
202 best_mean = best_vector_mean;
208 if (best_score > threshold && level) {
210 int offset = level & 1 ? stride * h / 2 : w / 2;
213 for (i = level - 1; i >= 0; i--)
215 score +=
encode_block(s, src, ref, decoded, stride, level - 1,
216 threshold >> 1, lambda, intra);
217 score +=
encode_block(s, src + offset, ref + offset, decoded + offset,
218 stride, level - 1, threshold >> 1, lambda, intra);
221 if (score < best_score) {
225 for (i = level - 1; i >= 0; i--)
233 av_assert1(best_mean >= 0 && best_mean < 256 || !intra);
234 av_assert1(best_mean >= -256 && best_mean < 256);
235 av_assert1(best_count >= 0 && best_count < 7);
240 multistage_vlc[1 + best_count][1],
241 multistage_vlc[1 + best_count][0]);
243 mean_vlc[best_mean][0]);
245 for (i = 0; i < best_count; i++) {
246 av_assert2(best_vector[i] >= 0 && best_vector[i] < 16);
250 for (y = 0; y < h; y++)
251 for (x = 0; x < w; x++)
252 decoded[x + y * stride] = src[x + y * stride] -
253 block[best_count][x + w * y] +
261 unsigned char *src_plane,
262 unsigned char *ref_plane,
263 unsigned char *decoded_plane,
269 int block_width, block_height;
278 for (level = 4; level >= 0; level--)
281 block_width = (width + 15) / 16;
282 block_height = (height + 15) / 16;
316 block_height * 2 + 2) *
317 2 *
sizeof(int16_t));
319 (block_height + 2) + 1) *
320 2 *
sizeof(int16_t));
339 for (y = 0; y < block_height; y++) {
343 for (i = 0; i < 16 && i + 16 * y <
height; i++) {
344 memcpy(&src[i * stride], &src_plane[(i + 16 * y) * src_stride],
346 for (x = width; x < 16 * block_width; x++)
347 src[i * stride + x] = src[i * stride + x - 1];
349 for (; i < 16 && i + 16 * y < 16 * block_height; i++)
350 memcpy(&src[i * stride], &src[(i - 1) * stride],
353 for (x = 0; x < block_width; x++) {
369 for (y = 0; y < block_height; y++) {
370 for (i = 0; i < 16 && i + 16 * y <
height; i++) {
371 memcpy(&src[i * stride], &src_plane[(i + 16 * y) * src_stride],
373 for (x = width; x < 16 * block_width; x++)
374 src[i * stride + x] = src[i * stride + x - 1];
376 for (; i < 16 && i + 16 * y < 16 * block_height; i++)
377 memcpy(&src[i * stride], &src[(i - 1) * stride], 16 * block_width);
380 for (x = 0; x < block_width; x++) {
381 uint8_t reorder_buffer[3][6][7 * 32];
383 int offset = y * 16 * stride + x * 16;
386 int score[4] = { 0, 0, 0, 0 }, best;
402 for (i = 0; i < 6; i++)
408 score[0] = vlc[1] * lambda;
410 score[0] +=
encode_block(s, src + 16 * x, NULL, temp, stride,
412 for (i = 0; i < 6; i++) {
423 int mx, my, pred_x, pred_y, dxy;
429 for (i = 0; i < 6; i++)
447 dxy = (mx & 1) + 2 * (my & 1);
455 decoded, stride, 5, 64, lambda, 0);
456 best = score[1] <= score[0];
459 score[2] = s->
dsp.
sse[0](NULL, src + 16 * x, ref,
461 score[2] += vlc[1] * lambda;
462 if (score[2] < score[best] && mx == 0 && my == 0) {
465 for (i = 0; i < 6; i++)
472 for (i = 0; i < 6; i++) {
490 for (i = 5; i >= 0; i--)
517 for (i = 0; i < 3; i++) {
571 const AVFrame *pict,
int *got_packet)
604 for (i = 0; i < 3; i++)