110 #define QUALITY_THRESHOLD 100
111 #define THRESHOLD_MULTIPLIER 0.6
115 int threshold,
int lambda,
int intra)
117 int count,
y, x, i, j,
split, best_mean, best_score, best_count;
119 int block_sum[7] = { 0, 0, 0, 0, 0, 0 };
120 int w = 2 << (level + 2 >> 1);
121 int h = 2 << (level + 1 >> 1);
123 int16_t
block[7][256];
124 const int8_t *codebook_sum, *codebook;
125 const uint16_t(*mean_vlc)[2];
126 const uint8_t(*multistage_vlc)[2];
135 for (y = 0; y < h; y++) {
136 for (x = 0; x < w; x++) {
138 block[0][x + w *
y] =
v;
148 for (y = 0; y < h; y++) {
149 for (x = 0; x < w; x++) {
151 block[0][x + w *
y] =
v;
159 best_score -= (int)((
unsigned)block_sum[0] * block_sum[0] >> (level + 3));
160 best_mean = block_sum[0] + (size >> 1) >> (level + 3);
163 for (count = 1; count < 7; count++) {
164 int best_vector_score = INT_MAX;
165 int best_vector_sum = -999, best_vector_mean = -999;
166 const int stage = count - 1;
167 const int8_t *vector;
169 for (i = 0; i < 16; i++) {
170 int sum = codebook_sum[stage * 16 + i];
171 int sqr,
diff, score;
173 vector = codebook + stage * size * 16 + i *
size;
175 diff = block_sum[stage] - sum;
176 score = sqr - (diff * (int64_t)diff >> (level + 3));
177 if (score < best_vector_score) {
178 int mean = diff + (size >> 1) >> (level + 3);
180 mean = av_clip(mean, intra ? 0 : -256, 255);
181 best_vector_score = score;
182 best_vector[stage] = i;
183 best_vector_sum = sum;
184 best_vector_mean = mean;
188 vector = codebook + stage * size * 16 + best_vector[stage] *
size;
189 for (j = 0; j <
size; j++)
190 block[stage + 1][j] = block[stage][j] - vector[j];
191 block_sum[stage + 1] = block_sum[stage] - best_vector_sum;
192 best_vector_score += lambda *
194 multistage_vlc[1 + count][1]
195 + mean_vlc[best_vector_mean][1]);
197 if (best_vector_score < best_score) {
198 best_score = best_vector_score;
200 best_mean = best_vector_mean;
206 if (best_score > threshold && level) {
208 int offset = level & 1 ? stride * h / 2 : w / 2;
211 for (i = level - 1; i >= 0; i--)
213 score +=
encode_block(s, src, ref, decoded, stride, level - 1,
214 threshold >> 1, lambda, intra);
215 score +=
encode_block(s, src + offset, ref + offset, decoded + offset,
216 stride, level - 1, threshold >> 1, lambda, intra);
219 if (score < best_score) {
223 for (i = level - 1; i >= 0; i--)
231 av_assert1(best_mean >= 0 && best_mean < 256 || !intra);
232 av_assert1(best_mean >= -256 && best_mean < 256);
233 av_assert1(best_count >= 0 && best_count < 7);
238 multistage_vlc[1 + best_count][1],
239 multistage_vlc[1 + best_count][0]);
241 mean_vlc[best_mean][0]);
243 for (i = 0; i < best_count; i++) {
244 av_assert2(best_vector[i] >= 0 && best_vector[i] < 16);
248 for (y = 0; y < h; y++)
249 for (x = 0; x < w; x++)
250 decoded[x + y * stride] = src[x + y * stride] -
251 block[best_count][x + w * y] +
259 unsigned char *src_plane,
260 unsigned char *ref_plane,
261 unsigned char *decoded_plane,
266 int block_width, block_height;
275 for (level = 4; level >= 0; level--)
278 block_width = (width + 15) / 16;
279 block_height = (height + 15) / 16;
313 block_height * 2 + 2) *
314 2 *
sizeof(int16_t));
316 (block_height + 2) + 1) *
317 2 *
sizeof(int16_t));
336 for (y = 0; y < block_height; y++) {
340 for (i = 0; i < 16 && i + 16 * y <
height; i++) {
341 memcpy(&src[i * stride], &src_plane[(i + 16 * y) * src_stride],
343 for (x = width; x < 16 * block_width; x++)
344 src[i * stride + x] = src[i * stride + x - 1];
346 for (; i < 16 && i + 16 * y < 16 * block_height; i++)
347 memcpy(&src[i * stride], &src[(i - 1) * stride],
350 for (x = 0; x < block_width; x++) {
366 for (y = 0; y < block_height; y++) {
367 for (i = 0; i < 16 && i + 16 * y <
height; i++) {
368 memcpy(&src[i * stride], &src_plane[(i + 16 * y) * src_stride],
370 for (x = width; x < 16 * block_width; x++)
371 src[i * stride + x] = src[i * stride + x - 1];
373 for (; i < 16 && i + 16 * y < 16 * block_height; i++)
374 memcpy(&src[i * stride], &src[(i - 1) * stride], 16 * block_width);
377 for (x = 0; x < block_width; x++) {
378 uint8_t reorder_buffer[3][6][7 * 32];
380 int offset = y * 16 * stride + x * 16;
383 int score[4] = { 0, 0, 0, 0 }, best;
399 for (i = 0; i < 6; i++)
405 score[0] = vlc[1] * lambda;
409 for (i = 0; i < 6; i++) {
420 int mx, my, pred_x, pred_y, dxy;
426 for (i = 0; i < 6; i++)
444 dxy = (mx & 1) + 2 * (my & 1);
452 decoded, stride, 5, 64, lambda, 0);
453 best = score[1] <= score[0];
456 score[2] = s->
dsp.
sse[0](
NULL, src + 16 * x, ref,
458 score[2] += vlc[1] * lambda;
459 if (score[2] < score[best] && mx == 0 && my == 0) {
462 for (i = 0; i < 6; i++)
469 for (i = 0; i < 6; i++) {
487 for (i = 5; i >= 0; i--)
532 const AVFrame *pict,
int *got_packet)
568 for (i = 0; i < 3; i++)
609 for (i = 0; i < 3; i++) {