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 "onyxc_int.h"
15 #include "vpx_mem/vpx_mem.h"
16 #include "vpx_scale/yv12extend.h"
17 #include "vpx_scale/vpxscale.h"
18 #include "alloccommon.h"
20 extern void vp8_loop_filter_frame(VP8_COMMON *cm, MACROBLOCKD *mbd, int filt_val);
21 extern void vp8_loop_filter_frame_yonly(VP8_COMMON *cm, MACROBLOCKD *mbd, int filt_val, int sharpness_lvl);
22 extern int vp8_calc_ss_err(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *dest, const vp8_variance_rtcd_vtable_t *rtcd);
24 extern void vp8_yv12_copy_frame_yonly_no_extend_frame_borders_neon(YV12_BUFFER_CONFIG *src_ybc, YV12_BUFFER_CONFIG *dst_ybc);
27 #if CONFIG_RUNTIME_CPU_DETECT
28 #define IF_RTCD(x) (x)
30 #define IF_RTCD(x) NULL
34 (*vp8_yv12_copy_partial_frame_ptr)(YV12_BUFFER_CONFIG *src_ybc,
35 YV12_BUFFER_CONFIG *dst_ybc,
38 vp8_yv12_copy_partial_frame(YV12_BUFFER_CONFIG *src_ybc, YV12_BUFFER_CONFIG *dst_ybc, int Fraction)
40 unsigned char *src_y, *dst_y;
47 border = src_ybc->border;
48 yheight = src_ybc->y_height;
49 ystride = src_ybc->y_stride;
51 linestocopy = (yheight >> (Fraction + 4));
58 yoffset = ystride * ((yheight >> 5) * 16 - 8);
59 src_y = src_ybc->y_buffer + yoffset;
60 dst_y = dst_ybc->y_buffer + yoffset;
62 vpx_memcpy(dst_y, src_y, ystride *(linestocopy + 16));
65 static int vp8_calc_partial_ssl_err(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *dest, int Fraction, const vp8_variance_rtcd_vtable_t *rtcd)
69 int srcoffset, dstoffset;
70 unsigned char *src = source->y_buffer;
71 unsigned char *dst = dest->y_buffer;
73 int linestocopy = (source->y_height >> (Fraction + 4));
82 srcoffset = source->y_stride * (dest->y_height >> 5) * 16;
83 dstoffset = dest->y_stride * (dest->y_height >> 5) * 16;
88 // Loop through the Y plane raw and reconstruction data summing (square differences)
89 for (i = 0; i < linestocopy; i += 16)
91 for (j = 0; j < source->y_width; j += 16)
94 Total += VARIANCE_INVOKE(rtcd, mse16x16)(src + j, source->y_stride, dst + j, dest->y_stride, &sse);
97 src += 16 * source->y_stride;
98 dst += 16 * dest->y_stride;
104 extern void vp8_loop_filter_partial_frame
108 int default_filt_lvl,
113 // Enforce a minimum filter level based upon baseline Q
114 static int get_min_filter_level(VP8_COMP *cpi, int base_qindex)
116 int min_filter_level;
118 if (cpi->source_alt_ref_active && cpi->common.refresh_golden_frame && !cpi->common.refresh_alt_ref_frame)
119 min_filter_level = 0;
122 if (base_qindex <= 6)
123 min_filter_level = 0;
124 else if (base_qindex <= 16)
125 min_filter_level = 1;
127 min_filter_level = (base_qindex / 8);
130 return min_filter_level;
133 // Enforce a maximum filter level based upon baseline Q
134 static int get_max_filter_level(VP8_COMP *cpi, int base_qindex)
136 // PGW August 2006: Highest filter values almost always a bad idea
138 // jbb chg: 20100118 - not so any more with this overquant stuff allow high values
139 // with lots of intra coming in.
140 int max_filter_level = MAX_LOOP_FILTER ;//* 3 / 4;
142 if (cpi->section_intra_rating > 8)
143 max_filter_level = MAX_LOOP_FILTER * 3 / 4;
148 return max_filter_level;
151 void vp8cx_pick_filter_level_fast(YV12_BUFFER_CONFIG *sd, VP8_COMP *cpi)
153 VP8_COMMON *cm = &cpi->common;
157 int min_filter_level = 0;
158 int max_filter_level = MAX_LOOP_FILTER * 3 / 4; // PGW August 2006: Highest filter values almost always a bad idea
160 int best_filt_val = cm->filter_level;
162 // Make a copy of the unfiltered / processed recon buffer
163 //vp8_yv12_copy_frame_ptr( cm->frame_to_show, &cpi->last_frame_uf );
164 vp8_yv12_copy_partial_frame_ptr(cm->frame_to_show, &cpi->last_frame_uf, 3);
166 if (cm->frame_type == KEY_FRAME)
167 cm->sharpness_level = 0;
169 cm->sharpness_level = cpi->oxcf.Sharpness;
171 // Enforce a minimum filter level based upon Q
172 min_filter_level = get_min_filter_level(cpi, cm->base_qindex);
173 max_filter_level = get_max_filter_level(cpi, cm->base_qindex);
175 // Start the search at the previous frame filter level unless it is now out of range.
176 if (cm->filter_level < min_filter_level)
177 cm->filter_level = min_filter_level;
178 else if (cm->filter_level > max_filter_level)
179 cm->filter_level = max_filter_level;
181 filt_val = cm->filter_level;
182 best_filt_val = filt_val;
184 // Set up alternate filter values
186 // Get the err using the previous frame's filter value.
187 vp8_loop_filter_partial_frame(cm, &cpi->mb.e_mbd, filt_val, 0 , 3);
188 cm->last_frame_type = cm->frame_type;
189 cm->last_filter_type = cm->filter_type;
190 cm->last_sharpness_level = cm->sharpness_level;
192 best_err = vp8_calc_partial_ssl_err(sd, cm->frame_to_show, 3, IF_RTCD(&cpi->rtcd.variance));
194 // Re-instate the unfiltered frame
195 vp8_yv12_copy_partial_frame_ptr(&cpi->last_frame_uf, cm->frame_to_show, 3);
197 filt_val -= (1 + ((filt_val > 10) ? 1 : 0));
199 // Search lower filter levels
200 while (filt_val >= min_filter_level)
202 // Apply the loop filter
203 vp8_loop_filter_partial_frame(cm, &cpi->mb.e_mbd, filt_val, 0, 3);
204 cm->last_frame_type = cm->frame_type;
205 cm->last_filter_type = cm->filter_type;
206 cm->last_sharpness_level = cm->sharpness_level;
208 // Get the err for filtered frame
209 filt_err = vp8_calc_partial_ssl_err(sd, cm->frame_to_show, 3, IF_RTCD(&cpi->rtcd.variance));
212 // Re-instate the unfiltered frame
213 vp8_yv12_copy_partial_frame_ptr(&cpi->last_frame_uf, cm->frame_to_show, 3);
216 // Update the best case record or exit loop.
217 if (filt_err < best_err)
220 best_filt_val = filt_val;
225 // Adjust filter level
226 filt_val -= (1 + ((filt_val > 10) ? 1 : 0));
229 // Search up (note that we have already done filt_val = cm->filter_level)
230 filt_val = cm->filter_level + (1 + ((filt_val > 10) ? 1 : 0));
232 if (best_filt_val == cm->filter_level)
234 // Resist raising filter level for very small gains
235 best_err -= (best_err >> 10);
237 while (filt_val < max_filter_level)
239 // Apply the loop filter
240 vp8_loop_filter_partial_frame(cm, &cpi->mb.e_mbd, filt_val, 0, 3);
241 cm->last_frame_type = cm->frame_type;
242 cm->last_filter_type = cm->filter_type;
243 cm->last_sharpness_level = cm->sharpness_level;
245 // Get the err for filtered frame
246 filt_err = vp8_calc_partial_ssl_err(sd, cm->frame_to_show, 3, IF_RTCD(&cpi->rtcd.variance));
248 // Re-instate the unfiltered frame
249 vp8_yv12_copy_partial_frame_ptr(&cpi->last_frame_uf, cm->frame_to_show, 3);
251 // Update the best case record or exit loop.
252 if (filt_err < best_err)
254 // Do not raise filter level if improvement is < 1 part in 4096
255 best_err = filt_err - (filt_err >> 10);
257 best_filt_val = filt_val;
262 // Adjust filter level
263 filt_val += (1 + ((filt_val > 10) ? 1 : 0));
267 cm->filter_level = best_filt_val;
269 if (cm->filter_level < min_filter_level)
270 cm->filter_level = min_filter_level;
272 if (cm->filter_level > max_filter_level)
273 cm->filter_level = max_filter_level;
276 // Stub function for now Alt LF not used
277 void vp8cx_set_alt_lf_level(VP8_COMP *cpi, int filt_val)
279 MACROBLOCKD *mbd = &cpi->mb.e_mbd;
282 mbd->segment_feature_data[MB_LVL_ALT_LF][0] = cpi->segment_feature_data[MB_LVL_ALT_LF][0];
283 mbd->segment_feature_data[MB_LVL_ALT_LF][1] = cpi->segment_feature_data[MB_LVL_ALT_LF][1];
284 mbd->segment_feature_data[MB_LVL_ALT_LF][2] = cpi->segment_feature_data[MB_LVL_ALT_LF][2];
285 mbd->segment_feature_data[MB_LVL_ALT_LF][3] = cpi->segment_feature_data[MB_LVL_ALT_LF][3];
288 void vp8cx_pick_filter_level(YV12_BUFFER_CONFIG *sd, VP8_COMP *cpi)
290 VP8_COMMON *cm = &cpi->common;
294 int min_filter_level;
295 int max_filter_level;
296 int prediction_difference = (int)(100 * abs((int)(cpi->last_auto_filter_prediction_error - cpi->prediction_error)) / (1 + cpi->prediction_error));
300 int filt_mid = cm->filter_level; // Start search at previous frame filter level
303 int filt_direction = 0;
305 int Bias = 0; // Bias against raising loop filter and in favour of lowering it
307 // Make a copy of the unfiltered / processed recon buffer
309 vp8_yv12_copy_frame_yonly_no_extend_frame_borders_neon(cm->frame_to_show, &cpi->last_frame_uf);
311 vp8_yv12_copy_frame_ptr(cm->frame_to_show, &cpi->last_frame_uf);
314 if (cm->frame_type == KEY_FRAME)
315 cm->sharpness_level = 0;
317 cm->sharpness_level = cpi->oxcf.Sharpness;
319 // Enforce a minimum filter level based upon Q
320 min_filter_level = get_min_filter_level(cpi, cm->base_qindex);
321 max_filter_level = get_max_filter_level(cpi, cm->base_qindex);
323 // Start the search at the previous frame filter level unless it is now out of range.
324 filt_mid = cm->filter_level;
326 if (filt_mid < min_filter_level)
327 filt_mid = min_filter_level;
328 else if (filt_mid > max_filter_level)
329 filt_mid = max_filter_level;
331 // Define the initial step size
332 filter_step = (filt_mid < 16) ? 4 : filt_mid / 4;
334 // Get baseline error score
335 vp8cx_set_alt_lf_level(cpi, filt_mid);
336 vp8_loop_filter_frame_yonly(cm, &cpi->mb.e_mbd, filt_mid, 0);
337 cm->last_frame_type = cm->frame_type;
338 cm->last_filter_type = cm->filter_type;
339 cm->last_sharpness_level = cm->sharpness_level;
341 best_err = vp8_calc_ss_err(sd, cm->frame_to_show, IF_RTCD(&cpi->rtcd.variance));
342 filt_best = filt_mid;
344 // Re-instate the unfiltered frame
346 vp8_yv12_copy_frame_yonly_no_extend_frame_borders_neon(&cpi->last_frame_uf, cm->frame_to_show);
348 vp8_yv12_copy_frame_yonly_ptr(&cpi->last_frame_uf, cm->frame_to_show);
351 while (filter_step > 0)
353 Bias = (best_err >> (15 - (filt_mid / 8))) * filter_step; //PGW change 12/12/06 for small images
355 // jbb chg: 20100118 - in sections with lots of new material coming in don't bias as much to a low filter value
356 if (cpi->section_intra_rating < 20)
357 Bias = Bias * cpi->section_intra_rating / 20;
359 filt_high = ((filt_mid + filter_step) > max_filter_level) ? max_filter_level : (filt_mid + filter_step);
360 filt_low = ((filt_mid - filter_step) < min_filter_level) ? min_filter_level : (filt_mid - filter_step);
362 if ((filt_direction <= 0) && (filt_low != filt_mid))
364 // Get Low filter error score
365 vp8cx_set_alt_lf_level(cpi, filt_low);
366 vp8_loop_filter_frame_yonly(cm, &cpi->mb.e_mbd, filt_low, 0);
367 cm->last_frame_type = cm->frame_type;
368 cm->last_filter_type = cm->filter_type;
369 cm->last_sharpness_level = cm->sharpness_level;
371 filt_err = vp8_calc_ss_err(sd, cm->frame_to_show, IF_RTCD(&cpi->rtcd.variance));
373 // Re-instate the unfiltered frame
375 vp8_yv12_copy_frame_yonly_no_extend_frame_borders_neon(&cpi->last_frame_uf, cm->frame_to_show);
377 vp8_yv12_copy_frame_yonly_ptr(&cpi->last_frame_uf, cm->frame_to_show);
380 // If value is close to the best so far then bias towards a lower loop filter value.
381 if ((filt_err - Bias) < best_err)
383 // Was it actually better than the previous best?
384 if (filt_err < best_err)
387 filt_best = filt_low;
391 // Now look at filt_high
392 if ((filt_direction >= 0) && (filt_high != filt_mid))
394 vp8cx_set_alt_lf_level(cpi, filt_high);
395 vp8_loop_filter_frame_yonly(cm, &cpi->mb.e_mbd, filt_high, 0);
396 cm->last_frame_type = cm->frame_type;
397 cm->last_filter_type = cm->filter_type;
398 cm->last_sharpness_level = cm->sharpness_level;
400 filt_err = vp8_calc_ss_err(sd, cm->frame_to_show, IF_RTCD(&cpi->rtcd.variance));
402 // Re-instate the unfiltered frame
404 vp8_yv12_copy_frame_yonly_no_extend_frame_borders_neon(&cpi->last_frame_uf, cm->frame_to_show);
406 vp8_yv12_copy_frame_yonly_ptr(&cpi->last_frame_uf, cm->frame_to_show);
409 // Was it better than the previous best?
410 if (filt_err < (best_err - Bias))
413 filt_best = filt_high;
417 // Half the step distance if the best filter value was the same as last time
418 if (filt_best == filt_mid)
420 filter_step = filter_step / 2;
425 filt_direction = (filt_best < filt_mid) ? -1 : 1;
426 filt_mid = filt_best;
430 cm->filter_level = filt_best;
431 cpi->last_auto_filt_val = filt_best;
432 cpi->last_auto_filt_q = cm->base_qindex;
434 cpi->last_auto_filter_prediction_error = cpi->prediction_error;
435 cpi->frames_since_auto_filter = 0;