111 #define QUALITY_THRESHOLD 100
112 #define THRESHOLD_MULTIPLIER 0.6
116 int threshold,
int lambda,
int intra)
118 int count, y, x, i, j,
split, best_mean, best_score, best_count;
120 int block_sum[7] = { 0, 0, 0, 0, 0, 0 };
121 int w = 2 << (level + 2 >> 1);
122 int h = 2 << (level + 1 >> 1);
124 int16_t
block[7][256];
125 const int8_t *codebook_sum, *codebook;
126 const uint16_t(*mean_vlc)[2];
127 const uint8_t(*multistage_vlc)[2];
136 for (y = 0; y < h; y++) {
137 for (x = 0; x < w; x++) {
138 int v = src[x + y *
stride];
139 block[0][x + w * y] = v;
149 for (y = 0; y < h; y++) {
150 for (x = 0; x < w; x++) {
152 block[0][x + w * y] = v;
160 best_score -= (int)((
unsigned)block_sum[0] * block_sum[0] >> (level + 3));
161 best_mean = block_sum[0] + (size >> 1) >> (level + 3);
164 for (count = 1; count < 7; count++) {
165 int best_vector_score = INT_MAX;
166 int best_vector_sum = -999, best_vector_mean = -999;
167 const int stage = count - 1;
168 const int8_t *vector;
170 for (i = 0; i < 16; i++) {
171 int sum = codebook_sum[stage * 16 + i];
172 int sqr,
diff, score;
174 vector = codebook + stage * size * 16 + i *
size;
176 diff = block_sum[stage] - sum;
177 score = sqr - (diff * (int64_t)diff >> (level + 3));
178 if (score < best_vector_score) {
179 int mean = diff + (size >> 1) >> (level + 3);
181 mean = av_clip(mean, intra ? 0 : -256, 255);
182 best_vector_score = score;
183 best_vector[stage] = i;
184 best_vector_sum = sum;
185 best_vector_mean = mean;
189 vector = codebook + stage * size * 16 + best_vector[stage] *
size;
190 for (j = 0; j <
size; j++)
191 block[stage + 1][j] = block[stage][j] - vector[j];
192 block_sum[stage + 1] = block_sum[stage] - best_vector_sum;
193 best_vector_score += lambda *
195 multistage_vlc[1 + count][1]
196 + mean_vlc[best_vector_mean][1]);
198 if (best_vector_score < best_score) {
199 best_score = best_vector_score;
201 best_mean = best_vector_mean;
207 if (best_score > threshold && level) {
209 int offset = level & 1 ? stride * h / 2 : w / 2;
212 for (i = level - 1; i >= 0; i--)
214 score +=
encode_block(s, src, ref, decoded, stride, level - 1,
215 threshold >> 1, lambda, intra);
216 score +=
encode_block(s, src + offset, ref + offset, decoded + offset,
217 stride, level - 1, threshold >> 1, lambda, intra);
220 if (score < best_score) {
224 for (i = level - 1; i >= 0; i--)
232 av_assert1(best_mean >= 0 && best_mean < 256 || !intra);
233 av_assert1(best_mean >= -256 && best_mean < 256);
234 av_assert1(best_count >= 0 && best_count < 7);
239 multistage_vlc[1 + best_count][1],
240 multistage_vlc[1 + best_count][0]);
242 mean_vlc[best_mean][0]);
244 for (i = 0; i < best_count; i++) {
245 av_assert2(best_vector[i] >= 0 && best_vector[i] < 16);
249 for (y = 0; y < h; y++)
250 for (x = 0; x < w; x++)
251 decoded[x + y * stride] = src[x + y * stride] -
252 block[best_count][x + w * y] +
260 unsigned char *src_plane,
261 unsigned char *ref_plane,
262 unsigned char *decoded_plane,
267 int block_width, block_height;
276 for (level = 4; level >= 0; level--)
279 block_width = (width + 15) / 16;
280 block_height = (height + 15) / 16;
314 block_height * 2 + 2) *
315 2 *
sizeof(int16_t));
317 (block_height + 2) + 1) *
318 2 *
sizeof(int16_t));
337 for (y = 0; y < block_height; y++) {
341 for (i = 0; i < 16 && i + 16 * y <
height; i++) {
342 memcpy(&src[i * stride], &src_plane[(i + 16 * y) * src_stride],
344 for (x = width; x < 16 * block_width; x++)
345 src[i * stride + x] = src[i * stride + x - 1];
347 for (; i < 16 && i + 16 * y < 16 * block_height; i++)
348 memcpy(&src[i * stride], &src[(i - 1) * stride],
351 for (x = 0; x < block_width; x++) {
367 for (y = 0; y < block_height; y++) {
368 for (i = 0; i < 16 && i + 16 * y <
height; i++) {
369 memcpy(&src[i * stride], &src_plane[(i + 16 * y) * src_stride],
371 for (x = width; x < 16 * block_width; x++)
372 src[i * stride + x] = src[i * stride + x - 1];
374 for (; i < 16 && i + 16 * y < 16 * block_height; i++)
375 memcpy(&src[i * stride], &src[(i - 1) * stride], 16 * block_width);
378 for (x = 0; x < block_width; x++) {
379 uint8_t reorder_buffer[3][6][7 * 32];
381 int offset = y * 16 * stride + x * 16;
384 int score[4] = { 0, 0, 0, 0 }, best;
400 for (i = 0; i < 6; i++)
406 score[0] = vlc[1] * lambda;
410 for (i = 0; i < 6; i++) {
421 int mx, my, pred_x, pred_y, dxy;
427 for (i = 0; i < 6; i++)
445 dxy = (mx & 1) + 2 * (my & 1);
453 decoded, stride, 5, 64, lambda, 0);
454 best = score[1] <= score[0];
457 score[2] = s->
dsp.
sse[0](
NULL, src + 16 * x, ref,
459 score[2] += vlc[1] * lambda;
460 if (score[2] < score[best] && mx == 0 && my == 0) {
463 for (i = 0; i < 6; i++)
470 for (i = 0; i < 6; i++) {
488 for (i = 5; i >= 0; i--)
533 const AVFrame *pict,
int *got_packet)
569 for (i = 0; i < 3; i++)
610 for (i = 0; i < 3; i++) {