Coverage Report

Created: 2026-09-14 08:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libvpx/vp8/encoder/picklpf.c
Line
Count
Source
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
#include "./vpx_dsp_rtcd.h"
12
#include "./vpx_scale_rtcd.h"
13
#include "vp8/common/onyxc_int.h"
14
#include "onyx_int.h"
15
#include "vp8/encoder/picklpf.h"
16
#include "vp8/encoder/quantize.h"
17
#include "vpx_mem/vpx_mem.h"
18
#include "vpx_scale/vpx_scale.h"
19
#include "vp8/common/alloccommon.h"
20
#include "vp8/common/loopfilter.h"
21
#if VPX_ARCH_ARM
22
#include "vpx_ports/arm.h"
23
#endif
24
25
extern int vp8_calc_ss_err(YV12_BUFFER_CONFIG *source,
26
                           YV12_BUFFER_CONFIG *dest);
27
28
static void yv12_copy_partial_frame(YV12_BUFFER_CONFIG *src_ybc,
29
53
                                    YV12_BUFFER_CONFIG *dst_ybc) {
30
53
  unsigned char *src_y, *dst_y;
31
53
  int yheight;
32
53
  int ystride;
33
53
  int yoffset;
34
53
  int linestocopy;
35
36
53
  yheight = src_ybc->y_height;
37
53
  ystride = src_ybc->y_stride;
38
39
  /* number of MB rows to use in partial filtering */
40
53
  linestocopy = (yheight >> 4) / PARTIAL_FRAME_FRACTION;
41
53
  linestocopy = linestocopy ? linestocopy << 4 : 16; /* 16 lines per MB */
42
43
  /* Copy extra 4 so that full filter context is available if filtering done
44
   * on the copied partial frame and not original. Partial filter does mb
45
   * filtering for top row also, which can modify3 pixels above.
46
   */
47
53
  linestocopy += 4;
48
  /* partial image starts at ~middle of frame (macroblock border)*/
49
53
  yoffset = ystride * (((yheight >> 5) * 16) - 4);
50
53
  src_y = src_ybc->y_buffer + yoffset;
51
53
  dst_y = dst_ybc->y_buffer + yoffset;
52
53
  // The border will be used in vp8_loop_filter_partial_frame so it needs to be
54
  // extended to avoid a valgrind warning.
55
53
  const unsigned char *const top_row = src_ybc->y_buffer;
56
53
  for (int i = yoffset; i < 0; i += ystride, --linestocopy) {
57
0
    memcpy(dst_y, top_row, ystride);
58
0
    dst_y += ystride;
59
0
    src_y += ystride;
60
0
  }
61
53
  memcpy(dst_y, src_y, ystride * linestocopy);
62
53
}
63
64
static int calc_partial_ssl_err(YV12_BUFFER_CONFIG *source,
65
53
                                YV12_BUFFER_CONFIG *dest) {
66
53
  int i, j;
67
53
  int Total = 0;
68
53
  int srcoffset, dstoffset;
69
53
  unsigned char *src = source->y_buffer;
70
53
  unsigned char *dst = dest->y_buffer;
71
72
53
  int linestocopy;
73
74
  /* number of MB rows to use in partial filtering */
75
53
  linestocopy = (source->y_height >> 4) / PARTIAL_FRAME_FRACTION;
76
53
  linestocopy = linestocopy ? linestocopy << 4 : 16; /* 16 lines per MB */
77
78
  /* partial image starts at ~middle of frame (macroblock border)*/
79
53
  srcoffset = source->y_stride * ((dest->y_height >> 5) * 16);
80
53
  dstoffset = dest->y_stride * ((dest->y_height >> 5) * 16);
81
82
53
  src += srcoffset;
83
53
  dst += dstoffset;
84
85
  /* Loop through the Y plane raw and reconstruction data summing
86
   * (square differences)
87
   */
88
419
  for (i = 0; i < linestocopy; i += 16) {
89
6.49k
    for (j = 0; j < source->y_width; j += 16) {
90
6.13k
      unsigned int sse;
91
6.13k
      Total += vpx_mse16x16(src + j, source->y_stride, dst + j, dest->y_stride,
92
6.13k
                            &sse);
93
6.13k
    }
94
95
366
    src += 16 * source->y_stride;
96
366
    dst += 16 * dest->y_stride;
97
366
  }
98
99
53
  return Total;
100
53
}
101
102
/* Enforce a minimum filter level based upon baseline Q */
103
117k
static int get_min_filter_level(VP8_COMP *cpi, int base_qindex) {
104
117k
  int min_filter_level;
105
106
117k
  if (cpi->source_alt_ref_active && cpi->common.refresh_golden_frame &&
107
0
      !cpi->common.refresh_alt_ref_frame) {
108
0
    min_filter_level = 0;
109
117k
  } else {
110
117k
    if (base_qindex <= 6) {
111
53.1k
      min_filter_level = 0;
112
64.3k
    } else if (base_qindex <= 16) {
113
3.01k
      min_filter_level = 1;
114
61.3k
    } else {
115
61.3k
      min_filter_level = (base_qindex / 8);
116
61.3k
    }
117
117k
  }
118
119
117k
  return min_filter_level;
120
117k
}
121
122
/* Enforce a maximum filter level based upon baseline Q */
123
117k
static int get_max_filter_level(VP8_COMP *cpi, int base_qindex) {
124
  /* PGW August 2006: Highest filter values almost always a bad idea */
125
126
  /* jbb chg: 20100118 - not so any more with this overquant stuff allow
127
   * high values with lots of intra coming in.
128
   */
129
117k
  int max_filter_level = MAX_LOOP_FILTER;
130
117k
  (void)base_qindex;
131
132
117k
  if (cpi->twopass.section_intra_rating > 8) {
133
0
    max_filter_level = MAX_LOOP_FILTER * 3 / 4;
134
0
  }
135
136
117k
  return max_filter_level;
137
117k
}
138
139
11
void vp8cx_pick_filter_level_fast(YV12_BUFFER_CONFIG *sd, VP8_COMP *cpi) {
140
11
  VP8_COMMON *cm = &cpi->common;
141
142
11
  int best_err = 0;
143
11
  int filt_err = 0;
144
11
  int min_filter_level = get_min_filter_level(cpi, cm->base_qindex);
145
11
  int max_filter_level = get_max_filter_level(cpi, cm->base_qindex);
146
11
  int filt_val;
147
11
  int best_filt_val;
148
11
  YV12_BUFFER_CONFIG *saved_frame = cm->frame_to_show;
149
150
  /* Replace unfiltered frame buffer with a new one */
151
11
  cm->frame_to_show = &cpi->pick_lf_lvl_frame;
152
153
11
  if (cm->frame_type == KEY_FRAME) {
154
0
    cm->sharpness_level = 0;
155
11
  } else {
156
11
    cm->sharpness_level = cpi->oxcf.Sharpness;
157
11
  }
158
159
11
  if (cm->sharpness_level != cm->last_sharpness_level) {
160
0
    vp8_loop_filter_update_sharpness(&cm->lf_info, cm->sharpness_level);
161
0
    cm->last_sharpness_level = cm->sharpness_level;
162
0
  }
163
164
  /* Start the search at the previous frame filter level unless it is
165
   * now out of range.
166
   */
167
11
  if (cm->filter_level < min_filter_level) {
168
6
    cm->filter_level = min_filter_level;
169
6
  } else if (cm->filter_level > max_filter_level) {
170
0
    cm->filter_level = max_filter_level;
171
0
  }
172
173
11
  filt_val = cm->filter_level;
174
11
  best_filt_val = filt_val;
175
176
  /* Get the err using the previous frame's filter value. */
177
178
  /* Copy the unfiltered / processed recon buffer to the new buffer */
179
11
  yv12_copy_partial_frame(saved_frame, cm->frame_to_show);
180
11
  vp8_loop_filter_partial_frame(cm, &cpi->mb.e_mbd, filt_val);
181
182
11
  best_err = calc_partial_ssl_err(sd, cm->frame_to_show);
183
184
11
  filt_val -= 1 + (filt_val > 10);
185
186
  /* Search lower filter levels */
187
11
  while (filt_val >= min_filter_level) {
188
    /* Apply the loop filter */
189
5
    yv12_copy_partial_frame(saved_frame, cm->frame_to_show);
190
5
    vp8_loop_filter_partial_frame(cm, &cpi->mb.e_mbd, filt_val);
191
192
    /* Get the err for filtered frame */
193
5
    filt_err = calc_partial_ssl_err(sd, cm->frame_to_show);
194
195
    /* Update the best case record or exit loop. */
196
5
    if (filt_err < best_err) {
197
0
      best_err = filt_err;
198
0
      best_filt_val = filt_val;
199
5
    } else {
200
5
      break;
201
5
    }
202
203
    /* Adjust filter level */
204
0
    filt_val -= 1 + (filt_val > 10);
205
0
  }
206
207
  /* Search up (note that we have already done filt_val = cm->filter_level) */
208
11
  filt_val = cm->filter_level + 1 + (filt_val > 10);
209
210
11
  if (best_filt_val == cm->filter_level) {
211
    /* Resist raising filter level for very small gains */
212
11
    best_err -= (best_err >> 10);
213
214
37
    while (filt_val < max_filter_level) {
215
      /* Apply the loop filter */
216
37
      yv12_copy_partial_frame(saved_frame, cm->frame_to_show);
217
218
37
      vp8_loop_filter_partial_frame(cm, &cpi->mb.e_mbd, filt_val);
219
220
      /* Get the err for filtered frame */
221
37
      filt_err = calc_partial_ssl_err(sd, cm->frame_to_show);
222
223
      /* Update the best case record or exit loop. */
224
37
      if (filt_err < best_err) {
225
        /* Do not raise filter level if improvement is < 1 part
226
         * in 4096
227
         */
228
26
        best_err = filt_err - (filt_err >> 10);
229
230
26
        best_filt_val = filt_val;
231
26
      } else {
232
11
        break;
233
11
      }
234
235
      /* Adjust filter level */
236
26
      filt_val += 1 + (filt_val > 10);
237
26
    }
238
11
  }
239
240
11
  cm->filter_level = best_filt_val;
241
242
11
  if (cm->filter_level < min_filter_level) cm->filter_level = min_filter_level;
243
244
11
  if (cm->filter_level > max_filter_level) cm->filter_level = max_filter_level;
245
246
  /* restore unfiltered frame pointer */
247
11
  cm->frame_to_show = saved_frame;
248
11
}
249
250
/* Stub function for now Alt LF not used */
251
885k
void vp8cx_set_alt_lf_level(VP8_COMP *cpi, int filt_val) {
252
885k
  MACROBLOCKD *mbd = &cpi->mb.e_mbd;
253
885k
  (void)filt_val;
254
255
885k
  mbd->segment_feature_data[MB_LVL_ALT_LF][0] =
256
885k
      cpi->segment_feature_data[MB_LVL_ALT_LF][0];
257
885k
  mbd->segment_feature_data[MB_LVL_ALT_LF][1] =
258
885k
      cpi->segment_feature_data[MB_LVL_ALT_LF][1];
259
885k
  mbd->segment_feature_data[MB_LVL_ALT_LF][2] =
260
885k
      cpi->segment_feature_data[MB_LVL_ALT_LF][2];
261
885k
  mbd->segment_feature_data[MB_LVL_ALT_LF][3] =
262
885k
      cpi->segment_feature_data[MB_LVL_ALT_LF][3];
263
885k
}
264
265
117k
void vp8cx_pick_filter_level(YV12_BUFFER_CONFIG *sd, VP8_COMP *cpi) {
266
117k
  VP8_COMMON *cm = &cpi->common;
267
268
117k
  int best_err = 0;
269
117k
  int filt_err = 0;
270
117k
  int min_filter_level = get_min_filter_level(cpi, cm->base_qindex);
271
117k
  int max_filter_level = get_max_filter_level(cpi, cm->base_qindex);
272
273
117k
  int filter_step;
274
117k
  int filt_high = 0;
275
117k
  int filt_mid;
276
117k
  int filt_low = 0;
277
117k
  int filt_best;
278
117k
  int filt_direction = 0;
279
280
  /* Bias against raising loop filter and in favor of lowering it */
281
117k
  int Bias = 0;
282
283
117k
  int ss_err[MAX_LOOP_FILTER + 1];
284
285
117k
  YV12_BUFFER_CONFIG *saved_frame = cm->frame_to_show;
286
287
117k
  memset(ss_err, 0, sizeof(ss_err));
288
289
  /* Replace unfiltered frame buffer with a new one */
290
117k
  cm->frame_to_show = &cpi->pick_lf_lvl_frame;
291
292
117k
  if (cm->frame_type == KEY_FRAME) {
293
20.8k
    cm->sharpness_level = 0;
294
96.6k
  } else {
295
96.6k
    cm->sharpness_level = cpi->oxcf.Sharpness;
296
96.6k
  }
297
298
  /* Start the search at the previous frame filter level unless it is
299
   * now out of range.
300
   */
301
117k
  filt_mid = cm->filter_level;
302
303
117k
  if (filt_mid < min_filter_level) {
304
5.40k
    filt_mid = min_filter_level;
305
112k
  } else if (filt_mid > max_filter_level) {
306
0
    filt_mid = max_filter_level;
307
0
  }
308
309
  /* Define the initial step size */
310
117k
  filter_step = (filt_mid < 16) ? 4 : filt_mid / 4;
311
312
  /* Get baseline error score */
313
314
  /* Copy the unfiltered / processed recon buffer to the new buffer */
315
117k
  vpx_yv12_copy_y(saved_frame, cm->frame_to_show);
316
317
117k
  vp8cx_set_alt_lf_level(cpi, filt_mid);
318
117k
  vp8_loop_filter_frame_yonly(cm, &cpi->mb.e_mbd, filt_mid);
319
320
117k
  best_err = vp8_calc_ss_err(sd, cm->frame_to_show);
321
322
117k
  ss_err[filt_mid] = best_err;
323
324
117k
  filt_best = filt_mid;
325
326
571k
  while (filter_step > 0) {
327
454k
    Bias = (best_err >> (15 - (filt_mid / 8))) * filter_step;
328
329
454k
    if (cpi->twopass.section_intra_rating < 20) {
330
454k
      Bias = Bias * cpi->twopass.section_intra_rating / 20;
331
454k
    }
332
333
454k
    filt_high = ((filt_mid + filter_step) > max_filter_level)
334
454k
                    ? max_filter_level
335
454k
                    : (filt_mid + filter_step);
336
454k
    filt_low = ((filt_mid - filter_step) < min_filter_level)
337
454k
                   ? min_filter_level
338
454k
                   : (filt_mid - filter_step);
339
340
454k
    if ((filt_direction <= 0) && (filt_low != filt_mid)) {
341
323k
      if (ss_err[filt_low] == 0) {
342
        /* Get Low filter error score */
343
285k
        vpx_yv12_copy_y(saved_frame, cm->frame_to_show);
344
285k
        vp8cx_set_alt_lf_level(cpi, filt_low);
345
285k
        vp8_loop_filter_frame_yonly(cm, &cpi->mb.e_mbd, filt_low);
346
347
285k
        filt_err = vp8_calc_ss_err(sd, cm->frame_to_show);
348
285k
        ss_err[filt_low] = filt_err;
349
285k
      } else {
350
38.2k
        filt_err = ss_err[filt_low];
351
38.2k
      }
352
353
      /* If value is close to the best so far then bias towards a
354
       * lower loop filter value.
355
       */
356
323k
      if ((filt_err - Bias) < best_err) {
357
        /* Was it actually better than the previous best? */
358
26.1k
        if (filt_err < best_err) best_err = filt_err;
359
360
26.1k
        filt_best = filt_low;
361
26.1k
      }
362
323k
    }
363
364
    /* Now look at filt_high */
365
454k
    if ((filt_direction >= 0) && (filt_high != filt_mid)) {
366
400k
      if (ss_err[filt_high] == 0) {
367
377k
        vpx_yv12_copy_y(saved_frame, cm->frame_to_show);
368
377k
        vp8cx_set_alt_lf_level(cpi, filt_high);
369
377k
        vp8_loop_filter_frame_yonly(cm, &cpi->mb.e_mbd, filt_high);
370
371
377k
        filt_err = vp8_calc_ss_err(sd, cm->frame_to_show);
372
377k
        ss_err[filt_high] = filt_err;
373
377k
      } else {
374
22.3k
        filt_err = ss_err[filt_high];
375
22.3k
      }
376
377
      /* Was it better than the previous best? */
378
400k
      if (filt_err < (best_err - Bias)) {
379
46.5k
        best_err = filt_err;
380
46.5k
        filt_best = filt_high;
381
46.5k
      }
382
400k
    }
383
384
    /* Half the step distance if the best filter value was the same
385
     * as last time
386
     */
387
454k
    if (filt_best == filt_mid) {
388
383k
      filter_step = filter_step / 2;
389
383k
      filt_direction = 0;
390
383k
    } else {
391
71.0k
      filt_direction = (filt_best < filt_mid) ? -1 : 1;
392
71.0k
      filt_mid = filt_best;
393
71.0k
    }
394
454k
  }
395
396
117k
  cm->filter_level = filt_best;
397
398
  /* restore unfiltered frame pointer */
399
117k
  cm->frame_to_show = saved_frame;
400
117k
}