]> granicus.if.org Git - libvpx/blob - vp8/encoder/picklpf.c
safety check to avoid divide by 0s
[libvpx] / vp8 / encoder / picklpf.c
1 /*
2  *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3  *
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  */
10
11
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
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);
23 #if HAVE_ARMV7
24 extern void vp8_yv12_copy_frame_yonly_no_extend_frame_borders_neon(YV12_BUFFER_CONFIG *src_ybc, YV12_BUFFER_CONFIG *dst_ybc);
25 #endif
26
27 #if CONFIG_RUNTIME_CPU_DETECT
28 #define IF_RTCD(x) (x)
29 #else
30 #define IF_RTCD(x) NULL
31 #endif
32
33 extern void
34 (*vp8_yv12_copy_partial_frame_ptr)(YV12_BUFFER_CONFIG *src_ybc,
35                                    YV12_BUFFER_CONFIG *dst_ybc,
36                                    int Fraction);
37 void
38 vp8_yv12_copy_partial_frame(YV12_BUFFER_CONFIG *src_ybc, YV12_BUFFER_CONFIG *dst_ybc, int Fraction)
39 {
40     unsigned char *src_y, *dst_y;
41     int yheight;
42     int ystride;
43     int border;
44     int yoffset;
45     int linestocopy;
46
47     border   = src_ybc->border;
48     yheight  = src_ybc->y_height;
49     ystride  = src_ybc->y_stride;
50
51     linestocopy = (yheight >> (Fraction + 4));
52
53     if (linestocopy < 1)
54         linestocopy = 1;
55
56     linestocopy <<= 4;
57
58     yoffset  = ystride * ((yheight >> 5) * 16 - 8);
59     src_y = src_ybc->y_buffer + yoffset;
60     dst_y = dst_ybc->y_buffer + yoffset;
61
62     vpx_memcpy(dst_y, src_y, ystride *(linestocopy + 16));
63 }
64
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)
66 {
67     int i, j;
68     int Total = 0;
69     int srcoffset, dstoffset;
70     unsigned char *src = source->y_buffer;
71     unsigned char *dst = dest->y_buffer;
72
73     int linestocopy = (source->y_height >> (Fraction + 4));
74     (void)rtcd;
75
76     if (linestocopy < 1)
77         linestocopy = 1;
78
79     linestocopy <<= 4;
80
81
82     srcoffset = source->y_stride   * (dest->y_height >> 5) * 16;
83     dstoffset = dest->y_stride     * (dest->y_height >> 5) * 16;
84
85     src += srcoffset;
86     dst += dstoffset;
87
88     // Loop through the Y plane raw and reconstruction data summing (square differences)
89     for (i = 0; i < linestocopy; i += 16)
90     {
91         for (j = 0; j < source->y_width; j += 16)
92         {
93             unsigned int sse;
94             Total += VARIANCE_INVOKE(rtcd, mse16x16)(src + j, source->y_stride, dst + j, dest->y_stride, &sse);
95         }
96
97         src += 16 * source->y_stride;
98         dst += 16 * dest->y_stride;
99     }
100
101     return Total;
102 }
103
104 extern void vp8_loop_filter_partial_frame
105 (
106     VP8_COMMON *cm,
107     MACROBLOCKD *mbd,
108     int default_filt_lvl,
109     int sharpness_lvl,
110     int Fraction
111 );
112
113 // Enforce a minimum filter level based upon baseline Q
114 static int get_min_filter_level(VP8_COMP *cpi, int base_qindex)
115 {
116     int min_filter_level;
117
118     if (cpi->source_alt_ref_active && cpi->common.refresh_golden_frame && !cpi->common.refresh_alt_ref_frame)
119         min_filter_level = 0;
120     else
121     {
122         if (base_qindex <= 6)
123             min_filter_level = 0;
124         else if (base_qindex <= 16)
125             min_filter_level = 1;
126         else
127             min_filter_level = (base_qindex / 8);
128     }
129
130     return min_filter_level;
131 }
132
133 // Enforce a maximum filter level based upon baseline Q
134 static int get_max_filter_level(VP8_COMP *cpi, int base_qindex)
135 {
136     // PGW August 2006: Highest filter values almost always a bad idea
137
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;
141
142     if (cpi->section_intra_rating > 8)
143         max_filter_level = MAX_LOOP_FILTER * 3 / 4;
144
145     (void) cpi;
146     (void) base_qindex;
147
148     return max_filter_level;
149 }
150
151 void vp8cx_pick_filter_level_fast(YV12_BUFFER_CONFIG *sd, VP8_COMP *cpi)
152 {
153     VP8_COMMON *cm = &cpi->common;
154
155     int best_err = 0;
156     int filt_err = 0;
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
159     int filt_val;
160     int best_filt_val = cm->filter_level;
161
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);
165
166     if (cm->frame_type == KEY_FRAME)
167         cm->sharpness_level = 0;
168     else
169         cm->sharpness_level = cpi->oxcf.Sharpness;
170
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);
174
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;
180
181     filt_val = cm->filter_level;
182     best_filt_val = filt_val;
183
184     // Set up alternate filter values
185
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;
191
192     best_err = vp8_calc_partial_ssl_err(sd, cm->frame_to_show, 3, IF_RTCD(&cpi->rtcd.variance));
193
194     //  Re-instate the unfiltered frame
195     vp8_yv12_copy_partial_frame_ptr(&cpi->last_frame_uf, cm->frame_to_show, 3);
196
197     filt_val -= (1 + ((filt_val > 10) ? 1 : 0));
198
199     // Search lower filter levels
200     while (filt_val >= min_filter_level)
201     {
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;
207
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));
210
211
212         //  Re-instate the unfiltered frame
213         vp8_yv12_copy_partial_frame_ptr(&cpi->last_frame_uf, cm->frame_to_show, 3);
214
215
216         // Update the best case record or exit loop.
217         if (filt_err < best_err)
218         {
219             best_err = filt_err;
220             best_filt_val = filt_val;
221         }
222         else
223             break;
224
225         // Adjust filter level
226         filt_val -= (1 + ((filt_val > 10) ? 1 : 0));
227     }
228
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));
231
232     if (best_filt_val == cm->filter_level)
233     {
234         // Resist raising filter level for very small gains
235         best_err -= (best_err >> 10);
236
237         while (filt_val < max_filter_level)
238         {
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;
244
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));
247
248             //  Re-instate the unfiltered frame
249             vp8_yv12_copy_partial_frame_ptr(&cpi->last_frame_uf, cm->frame_to_show, 3);
250
251             // Update the best case record or exit loop.
252             if (filt_err < best_err)
253             {
254                 // Do not raise filter level if improvement is < 1 part in 4096
255                 best_err = filt_err - (filt_err >> 10);
256
257                 best_filt_val = filt_val;
258             }
259             else
260                 break;
261
262             // Adjust filter level
263             filt_val += (1 + ((filt_val > 10) ? 1 : 0));
264         }
265     }
266
267     cm->filter_level = best_filt_val;
268
269     if (cm->filter_level < min_filter_level)
270         cm->filter_level = min_filter_level;
271
272     if (cm->filter_level > max_filter_level)
273         cm->filter_level = max_filter_level;
274 }
275
276 // Stub function for now Alt LF not used
277 void vp8cx_set_alt_lf_level(VP8_COMP *cpi, int filt_val)
278 {
279     MACROBLOCKD *mbd = &cpi->mb.e_mbd;
280     (void) filt_val;
281
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];
286 }
287
288 void vp8cx_pick_filter_level(YV12_BUFFER_CONFIG *sd, VP8_COMP *cpi)
289 {
290     VP8_COMMON *cm = &cpi->common;
291
292     int best_err = 0;
293     int filt_err = 0;
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));
297
298     int filter_step;
299     int filt_high = 0;
300     int filt_mid = cm->filter_level;      // Start search at previous frame filter level
301     int filt_low = 0;
302     int filt_best;
303     int filt_direction = 0;
304
305     int Bias = 0;                       // Bias against raising loop filter and in favour of lowering it
306
307     //  Make a copy of the unfiltered / processed recon buffer
308 #if HAVE_ARMV7
309     vp8_yv12_copy_frame_yonly_no_extend_frame_borders_neon(cm->frame_to_show, &cpi->last_frame_uf);
310 #else
311     vp8_yv12_copy_frame_ptr(cm->frame_to_show, &cpi->last_frame_uf);
312 #endif
313
314     if (cm->frame_type == KEY_FRAME)
315         cm->sharpness_level = 0;
316     else
317         cm->sharpness_level = cpi->oxcf.Sharpness;
318
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);
322
323     // Start the search at the previous frame filter level unless it is now out of range.
324     filt_mid = cm->filter_level;
325
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;
330
331     // Define the initial step size
332     filter_step = (filt_mid < 16) ? 4 : filt_mid / 4;
333
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;
340
341     best_err = vp8_calc_ss_err(sd, cm->frame_to_show, IF_RTCD(&cpi->rtcd.variance));
342     filt_best = filt_mid;
343
344     //  Re-instate the unfiltered frame
345 #if HAVE_ARMV7
346     vp8_yv12_copy_frame_yonly_no_extend_frame_borders_neon(&cpi->last_frame_uf, cm->frame_to_show);
347 #else
348     vp8_yv12_copy_frame_yonly_ptr(&cpi->last_frame_uf, cm->frame_to_show);
349 #endif
350
351     while (filter_step > 0)
352     {
353         Bias = (best_err >> (15 - (filt_mid / 8))) * filter_step; //PGW change 12/12/06 for small images
354
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;
358
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);
361
362         if ((filt_direction <= 0) && (filt_low != filt_mid))
363         {
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;
370
371             filt_err = vp8_calc_ss_err(sd, cm->frame_to_show, IF_RTCD(&cpi->rtcd.variance));
372
373             //  Re-instate the unfiltered frame
374 #if HAVE_ARMV7
375             vp8_yv12_copy_frame_yonly_no_extend_frame_borders_neon(&cpi->last_frame_uf, cm->frame_to_show);
376 #else
377             vp8_yv12_copy_frame_yonly_ptr(&cpi->last_frame_uf, cm->frame_to_show);
378 #endif
379
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)
382             {
383                 // Was it actually better than the previous best?
384                 if (filt_err < best_err)
385                     best_err = filt_err;
386
387                 filt_best = filt_low;
388             }
389         }
390
391         // Now look at filt_high
392         if ((filt_direction >= 0) && (filt_high != filt_mid))
393         {
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;
399
400             filt_err = vp8_calc_ss_err(sd, cm->frame_to_show, IF_RTCD(&cpi->rtcd.variance));
401
402             //  Re-instate the unfiltered frame
403 #if HAVE_ARMV7
404             vp8_yv12_copy_frame_yonly_no_extend_frame_borders_neon(&cpi->last_frame_uf, cm->frame_to_show);
405 #else
406             vp8_yv12_copy_frame_yonly_ptr(&cpi->last_frame_uf, cm->frame_to_show);
407 #endif
408
409             // Was it better than the previous best?
410             if (filt_err < (best_err - Bias))
411             {
412                 best_err = filt_err;
413                 filt_best = filt_high;
414             }
415         }
416
417         // Half the step distance if the best filter value was the same as last time
418         if (filt_best == filt_mid)
419         {
420             filter_step = filter_step / 2;
421             filt_direction = 0;
422         }
423         else
424         {
425             filt_direction = (filt_best < filt_mid) ? -1 : 1;
426             filt_mid = filt_best;
427         }
428     }
429
430     cm->filter_level = filt_best;
431     cpi->last_auto_filt_val = filt_best;
432     cpi->last_auto_filt_q  = cm->base_qindex;
433
434     cpi->last_auto_filter_prediction_error = cpi->prediction_error;
435     cpi->frames_since_auto_filter = 0;
436 }