Removed unused vp8_recon_intra4x4mb function
[libvpx.git] / vp8 / encoder / picklpf.c
blob78aa866dd9daf0c23bc5d2aff6be20cda28a4e40
1 /*
2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
12 #include "onyxc_int.h"
13 #include "onyx_int.h"
14 #include "quantize.h"
15 #include "vpx_mem/vpx_mem.h"
16 #include "vpx_scale/yv12extend.h"
17 #include "vpx_scale/vpxscale.h"
18 #include "alloccommon.h"
19 #if ARCH_ARM
20 #include "vpx_ports/arm.h"
21 #endif
23 extern void vp8_loop_filter_frame(VP8_COMMON *cm, MACROBLOCKD *mbd, int filt_val);
24 extern void vp8_loop_filter_frame_yonly(VP8_COMMON *cm, MACROBLOCKD *mbd, int filt_val, int sharpness_lvl);
25 extern int vp8_calc_ss_err(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *dest, const vp8_variance_rtcd_vtable_t *rtcd);
26 #if HAVE_ARMV7
27 extern void vp8_yv12_copy_frame_yonly_no_extend_frame_borders_neon(YV12_BUFFER_CONFIG *src_ybc, YV12_BUFFER_CONFIG *dst_ybc);
28 #endif
30 #if CONFIG_RUNTIME_CPU_DETECT
31 #define IF_RTCD(x) (x)
32 #else
33 #define IF_RTCD(x) NULL
34 #endif
36 extern void
37 (*vp8_yv12_copy_partial_frame_ptr)(YV12_BUFFER_CONFIG *src_ybc,
38 YV12_BUFFER_CONFIG *dst_ybc,
39 int Fraction);
40 void
41 vp8_yv12_copy_partial_frame(YV12_BUFFER_CONFIG *src_ybc, YV12_BUFFER_CONFIG *dst_ybc, int Fraction)
43 unsigned char *src_y, *dst_y;
44 int yheight;
45 int ystride;
46 int border;
47 int yoffset;
48 int linestocopy;
50 border = src_ybc->border;
51 yheight = src_ybc->y_height;
52 ystride = src_ybc->y_stride;
54 linestocopy = (yheight >> (Fraction + 4));
56 if (linestocopy < 1)
57 linestocopy = 1;
59 linestocopy <<= 4;
61 yoffset = ystride * ((yheight >> 5) * 16 - 8);
62 src_y = src_ybc->y_buffer + yoffset;
63 dst_y = dst_ybc->y_buffer + yoffset;
65 vpx_memcpy(dst_y, src_y, ystride *(linestocopy + 16));
68 static int vp8_calc_partial_ssl_err(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *dest, int Fraction, const vp8_variance_rtcd_vtable_t *rtcd)
70 int i, j;
71 int Total = 0;
72 int srcoffset, dstoffset;
73 unsigned char *src = source->y_buffer;
74 unsigned char *dst = dest->y_buffer;
76 int linestocopy = (source->y_height >> (Fraction + 4));
77 (void)rtcd;
79 if (linestocopy < 1)
80 linestocopy = 1;
82 linestocopy <<= 4;
85 srcoffset = source->y_stride * (dest->y_height >> 5) * 16;
86 dstoffset = dest->y_stride * (dest->y_height >> 5) * 16;
88 src += srcoffset;
89 dst += dstoffset;
91 // Loop through the Y plane raw and reconstruction data summing (square differences)
92 for (i = 0; i < linestocopy; i += 16)
94 for (j = 0; j < source->y_width; j += 16)
96 unsigned int sse;
97 Total += VARIANCE_INVOKE(rtcd, mse16x16)(src + j, source->y_stride, dst + j, dest->y_stride, &sse);
100 src += 16 * source->y_stride;
101 dst += 16 * dest->y_stride;
104 return Total;
107 extern void vp8_loop_filter_partial_frame
109 VP8_COMMON *cm,
110 MACROBLOCKD *mbd,
111 int default_filt_lvl,
112 int sharpness_lvl,
113 int Fraction
116 // Enforce a minimum filter level based upon baseline Q
117 static int get_min_filter_level(VP8_COMP *cpi, int base_qindex)
119 int min_filter_level;
121 if (cpi->source_alt_ref_active && cpi->common.refresh_golden_frame && !cpi->common.refresh_alt_ref_frame)
122 min_filter_level = 0;
123 else
125 if (base_qindex <= 6)
126 min_filter_level = 0;
127 else if (base_qindex <= 16)
128 min_filter_level = 1;
129 else
130 min_filter_level = (base_qindex / 8);
133 return min_filter_level;
136 // Enforce a maximum filter level based upon baseline Q
137 static int get_max_filter_level(VP8_COMP *cpi, int base_qindex)
139 // PGW August 2006: Highest filter values almost always a bad idea
141 // jbb chg: 20100118 - not so any more with this overquant stuff allow high values
142 // with lots of intra coming in.
143 int max_filter_level = MAX_LOOP_FILTER ;//* 3 / 4;
145 if (cpi->section_intra_rating > 8)
146 max_filter_level = MAX_LOOP_FILTER * 3 / 4;
148 (void) cpi;
149 (void) base_qindex;
151 return max_filter_level;
154 void vp8cx_pick_filter_level_fast(YV12_BUFFER_CONFIG *sd, VP8_COMP *cpi)
156 VP8_COMMON *cm = &cpi->common;
158 int best_err = 0;
159 int filt_err = 0;
160 int min_filter_level = 0;
161 int max_filter_level = MAX_LOOP_FILTER * 3 / 4; // PGW August 2006: Highest filter values almost always a bad idea
162 int filt_val;
163 int best_filt_val = cm->filter_level;
165 // Make a copy of the unfiltered / processed recon buffer
166 //vp8_yv12_copy_frame_ptr( cm->frame_to_show, &cpi->last_frame_uf );
167 vp8_yv12_copy_partial_frame_ptr(cm->frame_to_show, &cpi->last_frame_uf, 3);
169 if (cm->frame_type == KEY_FRAME)
170 cm->sharpness_level = 0;
171 else
172 cm->sharpness_level = cpi->oxcf.Sharpness;
174 // Enforce a minimum filter level based upon Q
175 min_filter_level = get_min_filter_level(cpi, cm->base_qindex);
176 max_filter_level = get_max_filter_level(cpi, cm->base_qindex);
178 // Start the search at the previous frame filter level unless it is now out of range.
179 if (cm->filter_level < min_filter_level)
180 cm->filter_level = min_filter_level;
181 else if (cm->filter_level > max_filter_level)
182 cm->filter_level = max_filter_level;
184 filt_val = cm->filter_level;
185 best_filt_val = filt_val;
187 // Set up alternate filter values
189 // Get the err using the previous frame's filter value.
190 vp8_loop_filter_partial_frame(cm, &cpi->mb.e_mbd, filt_val, 0 , 3);
191 cm->last_frame_type = cm->frame_type;
192 cm->last_filter_type = cm->filter_type;
193 cm->last_sharpness_level = cm->sharpness_level;
195 best_err = vp8_calc_partial_ssl_err(sd, cm->frame_to_show, 3, IF_RTCD(&cpi->rtcd.variance));
197 // Re-instate the unfiltered frame
198 vp8_yv12_copy_partial_frame_ptr(&cpi->last_frame_uf, cm->frame_to_show, 3);
200 filt_val -= (1 + ((filt_val > 10) ? 1 : 0));
202 // Search lower filter levels
203 while (filt_val >= min_filter_level)
205 // Apply the loop filter
206 vp8_loop_filter_partial_frame(cm, &cpi->mb.e_mbd, filt_val, 0, 3);
207 cm->last_frame_type = cm->frame_type;
208 cm->last_filter_type = cm->filter_type;
209 cm->last_sharpness_level = cm->sharpness_level;
211 // Get the err for filtered frame
212 filt_err = vp8_calc_partial_ssl_err(sd, cm->frame_to_show, 3, IF_RTCD(&cpi->rtcd.variance));
215 // Re-instate the unfiltered frame
216 vp8_yv12_copy_partial_frame_ptr(&cpi->last_frame_uf, cm->frame_to_show, 3);
219 // Update the best case record or exit loop.
220 if (filt_err < best_err)
222 best_err = filt_err;
223 best_filt_val = filt_val;
225 else
226 break;
228 // Adjust filter level
229 filt_val -= (1 + ((filt_val > 10) ? 1 : 0));
232 // Search up (note that we have already done filt_val = cm->filter_level)
233 filt_val = cm->filter_level + (1 + ((filt_val > 10) ? 1 : 0));
235 if (best_filt_val == cm->filter_level)
237 // Resist raising filter level for very small gains
238 best_err -= (best_err >> 10);
240 while (filt_val < max_filter_level)
242 // Apply the loop filter
243 vp8_loop_filter_partial_frame(cm, &cpi->mb.e_mbd, filt_val, 0, 3);
244 cm->last_frame_type = cm->frame_type;
245 cm->last_filter_type = cm->filter_type;
246 cm->last_sharpness_level = cm->sharpness_level;
248 // Get the err for filtered frame
249 filt_err = vp8_calc_partial_ssl_err(sd, cm->frame_to_show, 3, IF_RTCD(&cpi->rtcd.variance));
251 // Re-instate the unfiltered frame
252 vp8_yv12_copy_partial_frame_ptr(&cpi->last_frame_uf, cm->frame_to_show, 3);
254 // Update the best case record or exit loop.
255 if (filt_err < best_err)
257 // Do not raise filter level if improvement is < 1 part in 4096
258 best_err = filt_err - (filt_err >> 10);
260 best_filt_val = filt_val;
262 else
263 break;
265 // Adjust filter level
266 filt_val += (1 + ((filt_val > 10) ? 1 : 0));
270 cm->filter_level = best_filt_val;
272 if (cm->filter_level < min_filter_level)
273 cm->filter_level = min_filter_level;
275 if (cm->filter_level > max_filter_level)
276 cm->filter_level = max_filter_level;
279 // Stub function for now Alt LF not used
280 void vp8cx_set_alt_lf_level(VP8_COMP *cpi, int filt_val)
282 MACROBLOCKD *mbd = &cpi->mb.e_mbd;
283 (void) filt_val;
285 mbd->segment_feature_data[MB_LVL_ALT_LF][0] = cpi->segment_feature_data[MB_LVL_ALT_LF][0];
286 mbd->segment_feature_data[MB_LVL_ALT_LF][1] = cpi->segment_feature_data[MB_LVL_ALT_LF][1];
287 mbd->segment_feature_data[MB_LVL_ALT_LF][2] = cpi->segment_feature_data[MB_LVL_ALT_LF][2];
288 mbd->segment_feature_data[MB_LVL_ALT_LF][3] = cpi->segment_feature_data[MB_LVL_ALT_LF][3];
291 void vp8cx_pick_filter_level(YV12_BUFFER_CONFIG *sd, VP8_COMP *cpi)
293 VP8_COMMON *cm = &cpi->common;
295 int best_err = 0;
296 int filt_err = 0;
297 int min_filter_level;
298 int max_filter_level;
300 int filter_step;
301 int filt_high = 0;
302 int filt_mid = cm->filter_level; // Start search at previous frame filter level
303 int filt_low = 0;
304 int filt_best;
305 int filt_direction = 0;
307 int Bias = 0; // Bias against raising loop filter and in favour of lowering it
309 // Make a copy of the unfiltered / processed recon buffer
310 #if HAVE_ARMV7
311 #if CONFIG_RUNTIME_CPU_DETECT
312 if (cm->rtcd.flags & HAS_NEON)
313 #endif
315 vp8_yv12_copy_frame_yonly_no_extend_frame_borders_neon(cm->frame_to_show, &cpi->last_frame_uf);
317 #if CONFIG_RUNTIME_CPU_DETECT
318 else
319 #endif
320 #endif
321 #if !HAVE_ARMV7 || CONFIG_RUNTIME_CPU_DETECT
323 vp8_yv12_copy_frame_ptr(cm->frame_to_show, &cpi->last_frame_uf);
325 #endif
327 if (cm->frame_type == KEY_FRAME)
328 cm->sharpness_level = 0;
329 else
330 cm->sharpness_level = cpi->oxcf.Sharpness;
332 // Enforce a minimum filter level based upon Q
333 min_filter_level = get_min_filter_level(cpi, cm->base_qindex);
334 max_filter_level = get_max_filter_level(cpi, cm->base_qindex);
336 // Start the search at the previous frame filter level unless it is now out of range.
337 filt_mid = cm->filter_level;
339 if (filt_mid < min_filter_level)
340 filt_mid = min_filter_level;
341 else if (filt_mid > max_filter_level)
342 filt_mid = max_filter_level;
344 // Define the initial step size
345 filter_step = (filt_mid < 16) ? 4 : filt_mid / 4;
347 // Get baseline error score
348 vp8cx_set_alt_lf_level(cpi, filt_mid);
349 vp8_loop_filter_frame_yonly(cm, &cpi->mb.e_mbd, filt_mid, 0);
350 cm->last_frame_type = cm->frame_type;
351 cm->last_filter_type = cm->filter_type;
352 cm->last_sharpness_level = cm->sharpness_level;
354 best_err = vp8_calc_ss_err(sd, cm->frame_to_show, IF_RTCD(&cpi->rtcd.variance));
355 filt_best = filt_mid;
357 // Re-instate the unfiltered frame
358 #if HAVE_ARMV7
359 #if CONFIG_RUNTIME_CPU_DETECT
360 if (cm->rtcd.flags & HAS_NEON)
361 #endif
363 vp8_yv12_copy_frame_yonly_no_extend_frame_borders_neon(&cpi->last_frame_uf, cm->frame_to_show);
365 #if CONFIG_RUNTIME_CPU_DETECT
366 else
367 #endif
368 #endif
369 #if !HAVE_ARMV7 || CONFIG_RUNTIME_CPU_DETECT
371 vp8_yv12_copy_frame_yonly_ptr(&cpi->last_frame_uf, cm->frame_to_show);
373 #endif
375 while (filter_step > 0)
377 Bias = (best_err >> (15 - (filt_mid / 8))) * filter_step; //PGW change 12/12/06 for small images
379 // jbb chg: 20100118 - in sections with lots of new material coming in don't bias as much to a low filter value
380 if (cpi->section_intra_rating < 20)
381 Bias = Bias * cpi->section_intra_rating / 20;
383 filt_high = ((filt_mid + filter_step) > max_filter_level) ? max_filter_level : (filt_mid + filter_step);
384 filt_low = ((filt_mid - filter_step) < min_filter_level) ? min_filter_level : (filt_mid - filter_step);
386 if ((filt_direction <= 0) && (filt_low != filt_mid))
388 // Get Low filter error score
389 vp8cx_set_alt_lf_level(cpi, filt_low);
390 vp8_loop_filter_frame_yonly(cm, &cpi->mb.e_mbd, filt_low, 0);
391 cm->last_frame_type = cm->frame_type;
392 cm->last_filter_type = cm->filter_type;
393 cm->last_sharpness_level = cm->sharpness_level;
395 filt_err = vp8_calc_ss_err(sd, cm->frame_to_show, IF_RTCD(&cpi->rtcd.variance));
397 // Re-instate the unfiltered frame
398 #if HAVE_ARMV7
399 #if CONFIG_RUNTIME_CPU_DETECT
400 if (cm->rtcd.flags & HAS_NEON)
401 #endif
403 vp8_yv12_copy_frame_yonly_no_extend_frame_borders_neon(&cpi->last_frame_uf, cm->frame_to_show);
405 #if CONFIG_RUNTIME_CPU_DETECT
406 else
407 #endif
408 #endif
409 #if !HAVE_ARMV7 || CONFIG_RUNTIME_CPU_DETECT
411 vp8_yv12_copy_frame_yonly_ptr(&cpi->last_frame_uf, cm->frame_to_show);
413 #endif
415 // If value is close to the best so far then bias towards a lower loop filter value.
416 if ((filt_err - Bias) < best_err)
418 // Was it actually better than the previous best?
419 if (filt_err < best_err)
420 best_err = filt_err;
422 filt_best = filt_low;
426 // Now look at filt_high
427 if ((filt_direction >= 0) && (filt_high != filt_mid))
429 vp8cx_set_alt_lf_level(cpi, filt_high);
430 vp8_loop_filter_frame_yonly(cm, &cpi->mb.e_mbd, filt_high, 0);
431 cm->last_frame_type = cm->frame_type;
432 cm->last_filter_type = cm->filter_type;
433 cm->last_sharpness_level = cm->sharpness_level;
435 filt_err = vp8_calc_ss_err(sd, cm->frame_to_show, IF_RTCD(&cpi->rtcd.variance));
437 // Re-instate the unfiltered frame
438 #if HAVE_ARMV7
439 #if CONFIG_RUNTIME_CPU_DETECT
440 if (cm->rtcd.flags & HAS_NEON)
441 #endif
443 vp8_yv12_copy_frame_yonly_no_extend_frame_borders_neon(&cpi->last_frame_uf, cm->frame_to_show);
445 #if CONFIG_RUNTIME_CPU_DETECT
446 else
447 #endif
448 #endif
449 #if !HAVE_ARMV7 || CONFIG_RUNTIME_CPU_DETECT
451 vp8_yv12_copy_frame_yonly_ptr(&cpi->last_frame_uf, cm->frame_to_show);
453 #endif
455 // Was it better than the previous best?
456 if (filt_err < (best_err - Bias))
458 best_err = filt_err;
459 filt_best = filt_high;
463 // Half the step distance if the best filter value was the same as last time
464 if (filt_best == filt_mid)
466 filter_step = filter_step / 2;
467 filt_direction = 0;
469 else
471 filt_direction = (filt_best < filt_mid) ? -1 : 1;
472 filt_mid = filt_best;
476 cm->filter_level = filt_best;
477 cpi->last_auto_filt_val = filt_best;
478 cpi->last_auto_filt_q = cm->base_qindex;
480 cpi->frames_since_auto_filter = 0;