Coverage Report

Created: 2026-01-16 07:48

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
2
                                    YV12_BUFFER_CONFIG *dst_ybc) {
30
2
  unsigned char *src_y, *dst_y;
31
2
  int yheight;
32
2
  int ystride;
33
2
  int yoffset;
34
2
  int linestocopy;
35
36
2
  yheight = src_ybc->y_height;
37
2
  ystride = src_ybc->y_stride;
38
39
  /* number of MB rows to use in partial filtering */
40
2
  linestocopy = (yheight >> 4) / PARTIAL_FRAME_FRACTION;
41
2
  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
2
  linestocopy += 4;
48
  /* partial image starts at ~middle of frame (macroblock border)*/
49
2
  yoffset = ystride * (((yheight >> 5) * 16) - 4);
50
2
  src_y = src_ybc->y_buffer + yoffset;
51
2
  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
2
  const unsigned char *const top_row = src_ybc->y_buffer;
56
2
  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
2
  memcpy(dst_y, src_y, ystride * linestocopy);
62
2
}
63
64
static int calc_partial_ssl_err(YV12_BUFFER_CONFIG *source,
65
2
                                YV12_BUFFER_CONFIG *dest) {
66
2
  int i, j;
67
2
  int Total = 0;
68
2
  int srcoffset, dstoffset;
69
2
  unsigned char *src = source->y_buffer;
70
2
  unsigned char *dst = dest->y_buffer;
71
72
2
  int linestocopy;
73
74
  /* number of MB rows to use in partial filtering */
75
2
  linestocopy = (source->y_height >> 4) / PARTIAL_FRAME_FRACTION;
76
2
  linestocopy = linestocopy ? linestocopy << 4 : 16; /* 16 lines per MB */
77
78
  /* partial image starts at ~middle of frame (macroblock border)*/
79
2
  srcoffset = source->y_stride * ((dest->y_height >> 5) * 16);
80
2
  dstoffset = dest->y_stride * ((dest->y_height >> 5) * 16);
81
82
2
  src += srcoffset;
83
2
  dst += dstoffset;
84
85
  /* Loop through the Y plane raw and reconstruction data summing
86
   * (square differences)
87
   */
88
18
  for (i = 0; i < linestocopy; i += 16) {
89
272
    for (j = 0; j < source->y_width; j += 16) {
90
256
      unsigned int sse;
91
256
      Total += vpx_mse16x16(src + j, source->y_stride, dst + j, dest->y_stride,
92
256
                            &sse);
93
256
    }
94
95
16
    src += 16 * source->y_stride;
96
16
    dst += 16 * dest->y_stride;
97
16
  }
98
99
2
  return Total;
100
2
}
101
102
/* Enforce a minimum filter level based upon baseline Q */
103
110k
static int get_min_filter_level(VP8_COMP *cpi, int base_qindex) {
104
110k
  int min_filter_level;
105
106
110k
  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
110k
  } else {
110
110k
    if (base_qindex <= 6) {
111
47.4k
      min_filter_level = 0;
112
62.7k
    } else if (base_qindex <= 16) {
113
3.54k
      min_filter_level = 1;
114
59.1k
    } else {
115
59.1k
      min_filter_level = (base_qindex / 8);
116
59.1k
    }
117
110k
  }
118
119
110k
  return min_filter_level;
120
110k
}
121
122
/* Enforce a maximum filter level based upon baseline Q */
123
110k
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
110k
  int max_filter_level = MAX_LOOP_FILTER;
130
110k
  (void)base_qindex;
131
132
110k
  if (cpi->twopass.section_intra_rating > 8) {
133
0
    max_filter_level = MAX_LOOP_FILTER * 3 / 4;
134
0
  }
135
136
110k
  return max_filter_level;
137
110k
}
138
139
1
void vp8cx_pick_filter_level_fast(YV12_BUFFER_CONFIG *sd, VP8_COMP *cpi) {
140
1
  VP8_COMMON *cm = &cpi->common;
141
142
1
  int best_err = 0;
143
1
  int filt_err = 0;
144
1
  int min_filter_level = get_min_filter_level(cpi, cm->base_qindex);
145
1
  int max_filter_level = get_max_filter_level(cpi, cm->base_qindex);
146
1
  int filt_val;
147
1
  int best_filt_val;
148
1
  YV12_BUFFER_CONFIG *saved_frame = cm->frame_to_show;
149
150
  /* Replace unfiltered frame buffer with a new one */
151
1
  cm->frame_to_show = &cpi->pick_lf_lvl_frame;
152
153
1
  if (cm->frame_type == KEY_FRAME) {
154
0
    cm->sharpness_level = 0;
155
1
  } else {
156
1
    cm->sharpness_level = cpi->oxcf.Sharpness;
157
1
  }
158
159
1
  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
1
  if (cm->filter_level < min_filter_level) {
168
1
    cm->filter_level = min_filter_level;
169
1
  } else if (cm->filter_level > max_filter_level) {
170
0
    cm->filter_level = max_filter_level;
171
0
  }
172
173
1
  filt_val = cm->filter_level;
174
1
  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
1
  yv12_copy_partial_frame(saved_frame, cm->frame_to_show);
180
1
  vp8_loop_filter_partial_frame(cm, &cpi->mb.e_mbd, filt_val);
181
182
1
  best_err = calc_partial_ssl_err(sd, cm->frame_to_show);
183
184
1
  filt_val -= 1 + (filt_val > 10);
185
186
  /* Search lower filter levels */
187
1
  while (filt_val >= min_filter_level) {
188
    /* Apply the loop filter */
189
0
    yv12_copy_partial_frame(saved_frame, cm->frame_to_show);
190
0
    vp8_loop_filter_partial_frame(cm, &cpi->mb.e_mbd, filt_val);
191
192
    /* Get the err for filtered frame */
193
0
    filt_err = calc_partial_ssl_err(sd, cm->frame_to_show);
194
195
    /* Update the best case record or exit loop. */
196
0
    if (filt_err < best_err) {
197
0
      best_err = filt_err;
198
0
      best_filt_val = filt_val;
199
0
    } else {
200
0
      break;
201
0
    }
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
1
  filt_val = cm->filter_level + 1 + (filt_val > 10);
209
210
1
  if (best_filt_val == cm->filter_level) {
211
    /* Resist raising filter level for very small gains */
212
1
    best_err -= (best_err >> 10);
213
214
1
    while (filt_val < max_filter_level) {
215
      /* Apply the loop filter */
216
1
      yv12_copy_partial_frame(saved_frame, cm->frame_to_show);
217
218
1
      vp8_loop_filter_partial_frame(cm, &cpi->mb.e_mbd, filt_val);
219
220
      /* Get the err for filtered frame */
221
1
      filt_err = calc_partial_ssl_err(sd, cm->frame_to_show);
222
223
      /* Update the best case record or exit loop. */
224
1
      if (filt_err < best_err) {
225
        /* Do not raise filter level if improvement is < 1 part
226
         * in 4096
227
         */
228
0
        best_err = filt_err - (filt_err >> 10);
229
230
0
        best_filt_val = filt_val;
231
1
      } else {
232
1
        break;
233
1
      }
234
235
      /* Adjust filter level */
236
0
      filt_val += 1 + (filt_val > 10);
237
0
    }
238
1
  }
239
240
1
  cm->filter_level = best_filt_val;
241
242
1
  if (cm->filter_level < min_filter_level) cm->filter_level = min_filter_level;
243
244
1
  if (cm->filter_level > max_filter_level) cm->filter_level = max_filter_level;
245
246
  /* restore unfiltered frame pointer */
247
1
  cm->frame_to_show = saved_frame;
248
1
}
249
250
/* Stub function for now Alt LF not used */
251
848k
void vp8cx_set_alt_lf_level(VP8_COMP *cpi, int filt_val) {
252
848k
  MACROBLOCKD *mbd = &cpi->mb.e_mbd;
253
848k
  (void)filt_val;
254
255
848k
  mbd->segment_feature_data[MB_LVL_ALT_LF][0] =
256
848k
      cpi->segment_feature_data[MB_LVL_ALT_LF][0];
257
848k
  mbd->segment_feature_data[MB_LVL_ALT_LF][1] =
258
848k
      cpi->segment_feature_data[MB_LVL_ALT_LF][1];
259
848k
  mbd->segment_feature_data[MB_LVL_ALT_LF][2] =
260
848k
      cpi->segment_feature_data[MB_LVL_ALT_LF][2];
261
848k
  mbd->segment_feature_data[MB_LVL_ALT_LF][3] =
262
848k
      cpi->segment_feature_data[MB_LVL_ALT_LF][3];
263
848k
}
264
265
110k
void vp8cx_pick_filter_level(YV12_BUFFER_CONFIG *sd, VP8_COMP *cpi) {
266
110k
  VP8_COMMON *cm = &cpi->common;
267
268
110k
  int best_err = 0;
269
110k
  int filt_err = 0;
270
110k
  int min_filter_level = get_min_filter_level(cpi, cm->base_qindex);
271
110k
  int max_filter_level = get_max_filter_level(cpi, cm->base_qindex);
272
273
110k
  int filter_step;
274
110k
  int filt_high = 0;
275
110k
  int filt_mid;
276
110k
  int filt_low = 0;
277
110k
  int filt_best;
278
110k
  int filt_direction = 0;
279
280
  /* Bias against raising loop filter and in favor of lowering it */
281
110k
  int Bias = 0;
282
283
110k
  int ss_err[MAX_LOOP_FILTER + 1];
284
285
110k
  YV12_BUFFER_CONFIG *saved_frame = cm->frame_to_show;
286
287
110k
  memset(ss_err, 0, sizeof(ss_err));
288
289
  /* Replace unfiltered frame buffer with a new one */
290
110k
  cm->frame_to_show = &cpi->pick_lf_lvl_frame;
291
292
110k
  if (cm->frame_type == KEY_FRAME) {
293
18.6k
    cm->sharpness_level = 0;
294
91.4k
  } else {
295
91.4k
    cm->sharpness_level = cpi->oxcf.Sharpness;
296
91.4k
  }
297
298
  /* Start the search at the previous frame filter level unless it is
299
   * now out of range.
300
   */
301
110k
  filt_mid = cm->filter_level;
302
303
110k
  if (filt_mid < min_filter_level) {
304
5.21k
    filt_mid = min_filter_level;
305
104k
  } else if (filt_mid > max_filter_level) {
306
0
    filt_mid = max_filter_level;
307
0
  }
308
309
  /* Define the initial step size */
310
110k
  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
110k
  vpx_yv12_copy_y(saved_frame, cm->frame_to_show);
316
317
110k
  vp8cx_set_alt_lf_level(cpi, filt_mid);
318
110k
  vp8_loop_filter_frame_yonly(cm, &cpi->mb.e_mbd, filt_mid);
319
320
110k
  best_err = vp8_calc_ss_err(sd, cm->frame_to_show);
321
322
110k
  ss_err[filt_mid] = best_err;
323
324
110k
  filt_best = filt_mid;
325
326
544k
  while (filter_step > 0) {
327
433k
    Bias = (best_err >> (15 - (filt_mid / 8))) * filter_step;
328
329
433k
    if (cpi->twopass.section_intra_rating < 20) {
330
433k
      Bias = Bias * cpi->twopass.section_intra_rating / 20;
331
433k
    }
332
333
433k
    filt_high = ((filt_mid + filter_step) > max_filter_level)
334
433k
                    ? max_filter_level
335
433k
                    : (filt_mid + filter_step);
336
433k
    filt_low = ((filt_mid - filter_step) < min_filter_level)
337
433k
                   ? min_filter_level
338
433k
                   : (filt_mid - filter_step);
339
340
433k
    if ((filt_direction <= 0) && (filt_low != filt_mid)) {
341
312k
      if (ss_err[filt_low] == 0) {
342
        /* Get Low filter error score */
343
282k
        vpx_yv12_copy_y(saved_frame, cm->frame_to_show);
344
282k
        vp8cx_set_alt_lf_level(cpi, filt_low);
345
282k
        vp8_loop_filter_frame_yonly(cm, &cpi->mb.e_mbd, filt_low);
346
347
282k
        filt_err = vp8_calc_ss_err(sd, cm->frame_to_show);
348
282k
        ss_err[filt_low] = filt_err;
349
282k
      } else {
350
29.8k
        filt_err = ss_err[filt_low];
351
29.8k
      }
352
353
      /* If value is close to the best so far then bias towards a
354
       * lower loop filter value.
355
       */
356
312k
      if ((filt_err - Bias) < best_err) {
357
        /* Was it actually better than the previous best? */
358
26.4k
        if (filt_err < best_err) best_err = filt_err;
359
360
26.4k
        filt_best = filt_low;
361
26.4k
      }
362
312k
    }
363
364
    /* Now look at filt_high */
365
433k
    if ((filt_direction >= 0) && (filt_high != filt_mid)) {
366
380k
      if (ss_err[filt_high] == 0) {
367
356k
        vpx_yv12_copy_y(saved_frame, cm->frame_to_show);
368
356k
        vp8cx_set_alt_lf_level(cpi, filt_high);
369
356k
        vp8_loop_filter_frame_yonly(cm, &cpi->mb.e_mbd, filt_high);
370
371
356k
        filt_err = vp8_calc_ss_err(sd, cm->frame_to_show);
372
356k
        ss_err[filt_high] = filt_err;
373
356k
      } else {
374
23.2k
        filt_err = ss_err[filt_high];
375
23.2k
      }
376
377
      /* Was it better than the previous best? */
378
380k
      if (filt_err < (best_err - Bias)) {
379
46.6k
        best_err = filt_err;
380
46.6k
        filt_best = filt_high;
381
46.6k
      }
382
380k
    }
383
384
    /* Half the step distance if the best filter value was the same
385
     * as last time
386
     */
387
433k
    if (filt_best == filt_mid) {
388
362k
      filter_step = filter_step / 2;
389
362k
      filt_direction = 0;
390
362k
    } else {
391
71.3k
      filt_direction = (filt_best < filt_mid) ? -1 : 1;
392
71.3k
      filt_mid = filt_best;
393
71.3k
    }
394
433k
  }
395
396
110k
  cm->filter_level = filt_best;
397
398
  /* restore unfiltered frame pointer */
399
110k
  cm->frame_to_show = saved_frame;
400
110k
}