Coverage Report

Created: 2025-08-28 07:12

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