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.
12 #include "vp8/common/onyxc_int.h"
15 #include "vpx_mem/vpx_mem.h"
16 #include "vpx_scale/yv12extend.h"
17 #include "vpx_scale/vpxscale.h"
18 #include "vp8/common/alloccommon.h"
19 #include "vp8/common/loopfilter.h"
21 #include "vpx_ports/arm.h"
24 extern int vp8_calc_ss_err(YV12_BUFFER_CONFIG
*source
, YV12_BUFFER_CONFIG
*dest
, const vp8_variance_rtcd_vtable_t
*rtcd
);
26 extern void vp8_yv12_copy_frame_yonly_no_extend_frame_borders_neon(YV12_BUFFER_CONFIG
*src_ybc
, YV12_BUFFER_CONFIG
*dst_ybc
);
29 #if CONFIG_RUNTIME_CPU_DETECT
30 #define IF_RTCD(x) (x)
32 #define IF_RTCD(x) NULL
36 (*vp8_yv12_copy_partial_frame_ptr
)(YV12_BUFFER_CONFIG
*src_ybc
,
37 YV12_BUFFER_CONFIG
*dst_ybc
,
40 vp8_yv12_copy_partial_frame(YV12_BUFFER_CONFIG
*src_ybc
, YV12_BUFFER_CONFIG
*dst_ybc
, int Fraction
)
42 unsigned char *src_y
, *dst_y
;
49 border
= src_ybc
->border
;
50 yheight
= src_ybc
->y_height
;
51 ystride
= src_ybc
->y_stride
;
53 linestocopy
= (yheight
>> (Fraction
+ 4));
60 yoffset
= ystride
* ((yheight
>> 5) * 16 - 8);
61 src_y
= src_ybc
->y_buffer
+ yoffset
;
62 dst_y
= dst_ybc
->y_buffer
+ yoffset
;
64 vpx_memcpy(dst_y
, src_y
, ystride
*(linestocopy
+ 16));
67 static int vp8_calc_partial_ssl_err(YV12_BUFFER_CONFIG
*source
, YV12_BUFFER_CONFIG
*dest
, int Fraction
, const vp8_variance_rtcd_vtable_t
*rtcd
)
71 int srcoffset
, dstoffset
;
72 unsigned char *src
= source
->y_buffer
;
73 unsigned char *dst
= dest
->y_buffer
;
75 int linestocopy
= (source
->y_height
>> (Fraction
+ 4));
84 srcoffset
= source
->y_stride
* (dest
->y_height
>> 5) * 16;
85 dstoffset
= dest
->y_stride
* (dest
->y_height
>> 5) * 16;
90 // Loop through the Y plane raw and reconstruction data summing (square differences)
91 for (i
= 0; i
< linestocopy
; i
+= 16)
93 for (j
= 0; j
< source
->y_width
; j
+= 16)
96 Total
+= VARIANCE_INVOKE(rtcd
, mse16x16
)(src
+ j
, source
->y_stride
, dst
+ j
, dest
->y_stride
, &sse
);
99 src
+= 16 * source
->y_stride
;
100 dst
+= 16 * dest
->y_stride
;
106 // Enforce a minimum filter level based upon baseline Q
107 static int get_min_filter_level(VP8_COMP
*cpi
, int base_qindex
)
109 int min_filter_level
;
111 if (cpi
->source_alt_ref_active
&& cpi
->common
.refresh_golden_frame
&& !cpi
->common
.refresh_alt_ref_frame
)
112 min_filter_level
= 0;
115 if (base_qindex
<= 6)
116 min_filter_level
= 0;
117 else if (base_qindex
<= 16)
118 min_filter_level
= 1;
120 min_filter_level
= (base_qindex
/ 8);
123 return min_filter_level
;
126 // Enforce a maximum filter level based upon baseline Q
127 static int get_max_filter_level(VP8_COMP
*cpi
, int base_qindex
)
129 // PGW August 2006: Highest filter values almost always a bad idea
131 // jbb chg: 20100118 - not so any more with this overquant stuff allow high values
132 // with lots of intra coming in.
133 int max_filter_level
= MAX_LOOP_FILTER
;//* 3 / 4;
136 if (cpi
->twopass
.section_intra_rating
> 8)
137 max_filter_level
= MAX_LOOP_FILTER
* 3 / 4;
139 return max_filter_level
;
142 void vp8cx_pick_filter_level_fast(YV12_BUFFER_CONFIG
*sd
, VP8_COMP
*cpi
)
144 VP8_COMMON
*cm
= &cpi
->common
;
148 int min_filter_level
= get_min_filter_level(cpi
, cm
->base_qindex
);
149 int max_filter_level
= get_max_filter_level(cpi
, cm
->base_qindex
);
151 int best_filt_val
= cm
->filter_level
;
153 // Make a copy of the unfiltered / processed recon buffer
154 vp8_yv12_copy_partial_frame_ptr(cm
->frame_to_show
, &cpi
->last_frame_uf
, 3);
156 if (cm
->frame_type
== KEY_FRAME
)
157 cm
->sharpness_level
= 0;
159 cm
->sharpness_level
= cpi
->oxcf
.Sharpness
;
161 if (cm
->sharpness_level
!= cm
->last_sharpness_level
)
163 vp8_loop_filter_update_sharpness(&cm
->lf_info
, cm
->sharpness_level
);
164 cm
->last_sharpness_level
= cm
->sharpness_level
;
167 // Start the search at the previous frame filter level unless it is now out of range.
168 if (cm
->filter_level
< min_filter_level
)
169 cm
->filter_level
= min_filter_level
;
170 else if (cm
->filter_level
> max_filter_level
)
171 cm
->filter_level
= max_filter_level
;
173 filt_val
= cm
->filter_level
;
174 best_filt_val
= filt_val
;
176 // Get the err using the previous frame's filter value.
177 vp8_loop_filter_partial_frame(cm
, &cpi
->mb
.e_mbd
, filt_val
);
179 best_err
= vp8_calc_partial_ssl_err(sd
, cm
->frame_to_show
, 3, IF_RTCD(&cpi
->rtcd
.variance
));
181 // Re-instate the unfiltered frame
182 vp8_yv12_copy_partial_frame_ptr(&cpi
->last_frame_uf
, cm
->frame_to_show
, 3);
184 filt_val
-= (1 + ((filt_val
> 10) ? 1 : 0));
186 // Search lower filter levels
187 while (filt_val
>= min_filter_level
)
189 // Apply the loop filter
190 vp8_loop_filter_partial_frame(cm
, &cpi
->mb
.e_mbd
, filt_val
);
192 // Get the err for filtered frame
193 filt_err
= vp8_calc_partial_ssl_err(sd
, cm
->frame_to_show
, 3, IF_RTCD(&cpi
->rtcd
.variance
));
195 // Re-instate the unfiltered frame
196 vp8_yv12_copy_partial_frame_ptr(&cpi
->last_frame_uf
, cm
->frame_to_show
, 3);
199 // Update the best case record or exit loop.
200 if (filt_err
< best_err
)
203 best_filt_val
= filt_val
;
208 // Adjust filter level
209 filt_val
-= (1 + ((filt_val
> 10) ? 1 : 0));
212 // Search up (note that we have already done filt_val = cm->filter_level)
213 filt_val
= cm
->filter_level
+ (1 + ((filt_val
> 10) ? 1 : 0));
215 if (best_filt_val
== cm
->filter_level
)
217 // Resist raising filter level for very small gains
218 best_err
-= (best_err
>> 10);
220 while (filt_val
< max_filter_level
)
222 // Apply the loop filter
223 vp8_loop_filter_partial_frame(cm
, &cpi
->mb
.e_mbd
, filt_val
);
225 // Get the err for filtered frame
226 filt_err
= vp8_calc_partial_ssl_err(sd
, cm
->frame_to_show
, 3, IF_RTCD(&cpi
->rtcd
.variance
));
228 // Re-instate the unfiltered frame
229 vp8_yv12_copy_partial_frame_ptr(&cpi
->last_frame_uf
, cm
->frame_to_show
, 3);
231 // Update the best case record or exit loop.
232 if (filt_err
< best_err
)
234 // Do not raise filter level if improvement is < 1 part in 4096
235 best_err
= filt_err
- (filt_err
>> 10);
237 best_filt_val
= filt_val
;
242 // Adjust filter level
243 filt_val
+= (1 + ((filt_val
> 10) ? 1 : 0));
247 cm
->filter_level
= best_filt_val
;
249 if (cm
->filter_level
< min_filter_level
)
250 cm
->filter_level
= min_filter_level
;
252 if (cm
->filter_level
> max_filter_level
)
253 cm
->filter_level
= max_filter_level
;
256 // Stub function for now Alt LF not used
257 void vp8cx_set_alt_lf_level(VP8_COMP
*cpi
, int filt_val
)
259 MACROBLOCKD
*mbd
= &cpi
->mb
.e_mbd
;
262 mbd
->segment_feature_data
[MB_LVL_ALT_LF
][0] = cpi
->segment_feature_data
[MB_LVL_ALT_LF
][0];
263 mbd
->segment_feature_data
[MB_LVL_ALT_LF
][1] = cpi
->segment_feature_data
[MB_LVL_ALT_LF
][1];
264 mbd
->segment_feature_data
[MB_LVL_ALT_LF
][2] = cpi
->segment_feature_data
[MB_LVL_ALT_LF
][2];
265 mbd
->segment_feature_data
[MB_LVL_ALT_LF
][3] = cpi
->segment_feature_data
[MB_LVL_ALT_LF
][3];
268 void vp8cx_pick_filter_level(YV12_BUFFER_CONFIG
*sd
, VP8_COMP
*cpi
)
270 VP8_COMMON
*cm
= &cpi
->common
;
274 int min_filter_level
= get_min_filter_level(cpi
, cm
->base_qindex
);
275 int max_filter_level
= get_max_filter_level(cpi
, cm
->base_qindex
);
279 int filt_mid
= cm
->filter_level
; // Start search at previous frame filter level
282 int filt_direction
= 0;
284 int Bias
= 0; // Bias against raising loop filter and in favour of lowering it
286 // Make a copy of the unfiltered / processed recon buffer
288 #if CONFIG_RUNTIME_CPU_DETECT
289 if (cm
->rtcd
.flags
& HAS_NEON
)
292 vp8_yv12_copy_frame_yonly_no_extend_frame_borders_neon(cm
->frame_to_show
, &cpi
->last_frame_uf
);
294 #if CONFIG_RUNTIME_CPU_DETECT
298 #if !HAVE_ARMV7 || CONFIG_RUNTIME_CPU_DETECT
300 vp8_yv12_copy_frame_ptr(cm
->frame_to_show
, &cpi
->last_frame_uf
);
304 if (cm
->frame_type
== KEY_FRAME
)
305 cm
->sharpness_level
= 0;
307 cm
->sharpness_level
= cpi
->oxcf
.Sharpness
;
309 // Start the search at the previous frame filter level unless it is now out of range.
310 filt_mid
= cm
->filter_level
;
312 if (filt_mid
< min_filter_level
)
313 filt_mid
= min_filter_level
;
314 else if (filt_mid
> max_filter_level
)
315 filt_mid
= max_filter_level
;
317 // Define the initial step size
318 filter_step
= (filt_mid
< 16) ? 4 : filt_mid
/ 4;
320 // Get baseline error score
321 vp8cx_set_alt_lf_level(cpi
, filt_mid
);
322 vp8_loop_filter_frame_yonly(cm
, &cpi
->mb
.e_mbd
, filt_mid
);
324 best_err
= vp8_calc_ss_err(sd
, cm
->frame_to_show
, IF_RTCD(&cpi
->rtcd
.variance
));
325 filt_best
= filt_mid
;
327 // Re-instate the unfiltered frame
329 #if CONFIG_RUNTIME_CPU_DETECT
330 if (cm
->rtcd
.flags
& HAS_NEON
)
333 vp8_yv12_copy_frame_yonly_no_extend_frame_borders_neon(&cpi
->last_frame_uf
, cm
->frame_to_show
);
335 #if CONFIG_RUNTIME_CPU_DETECT
339 #if !HAVE_ARMV7 || CONFIG_RUNTIME_CPU_DETECT
341 vp8_yv12_copy_frame_yonly_ptr(&cpi
->last_frame_uf
, cm
->frame_to_show
);
345 while (filter_step
> 0)
347 Bias
= (best_err
>> (15 - (filt_mid
/ 8))) * filter_step
; //PGW change 12/12/06 for small images
349 // jbb chg: 20100118 - in sections with lots of new material coming in don't bias as much to a low filter value
350 if (cpi
->twopass
.section_intra_rating
< 20)
351 Bias
= Bias
* cpi
->twopass
.section_intra_rating
/ 20;
353 filt_high
= ((filt_mid
+ filter_step
) > max_filter_level
) ? max_filter_level
: (filt_mid
+ filter_step
);
354 filt_low
= ((filt_mid
- filter_step
) < min_filter_level
) ? min_filter_level
: (filt_mid
- filter_step
);
356 if ((filt_direction
<= 0) && (filt_low
!= filt_mid
))
358 // Get Low filter error score
359 vp8cx_set_alt_lf_level(cpi
, filt_low
);
360 vp8_loop_filter_frame_yonly(cm
, &cpi
->mb
.e_mbd
, filt_low
);
362 filt_err
= vp8_calc_ss_err(sd
, cm
->frame_to_show
, IF_RTCD(&cpi
->rtcd
.variance
));
364 // Re-instate the unfiltered frame
366 #if CONFIG_RUNTIME_CPU_DETECT
367 if (cm
->rtcd
.flags
& HAS_NEON
)
370 vp8_yv12_copy_frame_yonly_no_extend_frame_borders_neon(&cpi
->last_frame_uf
, cm
->frame_to_show
);
372 #if CONFIG_RUNTIME_CPU_DETECT
376 #if !HAVE_ARMV7 || CONFIG_RUNTIME_CPU_DETECT
378 vp8_yv12_copy_frame_yonly_ptr(&cpi
->last_frame_uf
, cm
->frame_to_show
);
382 // If value is close to the best so far then bias towards a lower loop filter value.
383 if ((filt_err
- Bias
) < best_err
)
385 // Was it actually better than the previous best?
386 if (filt_err
< best_err
)
389 filt_best
= filt_low
;
393 // Now look at filt_high
394 if ((filt_direction
>= 0) && (filt_high
!= filt_mid
))
396 vp8cx_set_alt_lf_level(cpi
, filt_high
);
397 vp8_loop_filter_frame_yonly(cm
, &cpi
->mb
.e_mbd
, filt_high
);
399 filt_err
= vp8_calc_ss_err(sd
, cm
->frame_to_show
, IF_RTCD(&cpi
->rtcd
.variance
));
401 // Re-instate the unfiltered frame
403 #if CONFIG_RUNTIME_CPU_DETECT
404 if (cm
->rtcd
.flags
& HAS_NEON
)
407 vp8_yv12_copy_frame_yonly_no_extend_frame_borders_neon(&cpi
->last_frame_uf
, cm
->frame_to_show
);
409 #if CONFIG_RUNTIME_CPU_DETECT
413 #if !HAVE_ARMV7 || CONFIG_RUNTIME_CPU_DETECT
415 vp8_yv12_copy_frame_yonly_ptr(&cpi
->last_frame_uf
, cm
->frame_to_show
);
419 // Was it better than the previous best?
420 if (filt_err
< (best_err
- Bias
))
423 filt_best
= filt_high
;
427 // Half the step distance if the best filter value was the same as last time
428 if (filt_best
== filt_mid
)
430 filter_step
= filter_step
/ 2;
435 filt_direction
= (filt_best
< filt_mid
) ? -1 : 1;
436 filt_mid
= filt_best
;
440 cm
->filter_level
= filt_best
;