113 #define QUALITY_THRESHOLD 100
114 #define THRESHOLD_MULTIPLIER 0.6
118 int threshold,
int lambda,
int intra)
120 int count,
y, x, i, j,
split, best_mean, best_score, best_count;
122 int block_sum[7] = { 0, 0, 0, 0, 0, 0 };
123 int w = 2 << (level + 2 >> 1);
124 int h = 2 << (level + 1 >> 1);
126 int16_t
block[7][256];
127 const int8_t *codebook_sum, *codebook;
128 const uint16_t(*mean_vlc)[2];
129 const uint8_t(*multistage_vlc)[2];
138 for (y = 0; y < h; y++) {
139 for (x = 0; x < w; x++) {
141 block[0][x + w *
y] =
v;
151 for (y = 0; y < h; y++) {
152 for (x = 0; x < w; x++) {
154 block[0][x + w *
y] =
v;
162 best_score -= (int)((
unsigned)block_sum[0] * block_sum[0] >> (level + 3));
163 best_mean = block_sum[0] + (size >> 1) >> (level + 3);
166 for (count = 1; count < 7; count++) {
167 int best_vector_score = INT_MAX;
168 int best_vector_sum = -999, best_vector_mean = -999;
169 const int stage = count - 1;
170 const int8_t *vector;
172 for (i = 0; i < 16; i++) {
173 int sum = codebook_sum[stage * 16 + i];
174 int sqr,
diff, score;
176 vector = codebook + stage * size * 16 + i *
size;
178 diff = block_sum[stage] - sum;
179 score = sqr - (diff * (int64_t)diff >> (level + 3));
180 if (score < best_vector_score) {
181 int mean = diff + (size >> 1) >> (level + 3);
183 mean = av_clip(mean, intra ? 0 : -256, 255);
184 best_vector_score = score;
185 best_vector[stage] = i;
186 best_vector_sum = sum;
187 best_vector_mean = mean;
191 vector = codebook + stage * size * 16 + best_vector[stage] *
size;
192 for (j = 0; j <
size; j++)
193 block[stage + 1][j] = block[stage][j] - vector[j];
194 block_sum[stage + 1] = block_sum[stage] - best_vector_sum;
195 best_vector_score += lambda *
197 multistage_vlc[1 +
count][1]
198 + mean_vlc[best_vector_mean][1]);
200 if (best_vector_score < best_score) {
201 best_score = best_vector_score;
203 best_mean = best_vector_mean;
209 if (best_score > threshold && level) {
211 int offset = level & 1 ? stride * h / 2 : w / 2;
214 for (i = level - 1; i >= 0; i--)
216 score +=
encode_block(s, src, ref, decoded, stride, level - 1,
217 threshold >> 1, lambda, intra);
218 score +=
encode_block(s, src + offset, ref + offset, decoded + offset,
219 stride, level - 1, threshold >> 1, lambda, intra);
222 if (score < best_score) {
226 for (i = level - 1; i >= 0; i--)
234 av_assert1(best_mean >= 0 && best_mean < 256 || !intra);
235 av_assert1(best_mean >= -256 && best_mean < 256);
236 av_assert1(best_count >= 0 && best_count < 7);
241 multistage_vlc[1 + best_count][1],
242 multistage_vlc[1 + best_count][0]);
244 mean_vlc[best_mean][0]);
246 for (i = 0; i < best_count; i++) {
247 av_assert2(best_vector[i] >= 0 && best_vector[i] < 16);
251 for (y = 0; y < h; y++)
252 for (x = 0; x < w; x++)
253 decoded[x + y * stride] = src[x + y * stride] -
254 block[best_count][x + w * y] +
262 unsigned char *src_plane,
263 unsigned char *ref_plane,
264 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--)
536 const AVFrame *pict,
int *got_packet)
572 for (i = 0; i < 3; i++)
613 for (i = 0; i < 3; i++) {