Coverage Report

Created: 2026-10-14 06:45

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/aom/av1/common/cdef.c
Line
Count
Source
1
/*
2
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved.
3
 *
4
 * This source code is subject to the terms of the BSD 2 Clause License and
5
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6
 * was not distributed with this source code in the LICENSE file, you can
7
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8
 * Media Patent License 1.0 was not distributed with this source code in the
9
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10
 */
11
12
#include <assert.h>
13
#include <stddef.h>
14
#include <string.h>
15
16
#include "config/aom_scale_rtcd.h"
17
18
#include "aom/aom_integer.h"
19
#include "aom_util/aom_pthread.h"
20
#include "av1/common/av1_common_int.h"
21
#include "av1/common/cdef.h"
22
#include "av1/common/cdef_block.h"
23
#include "av1/common/common.h"
24
#include "av1/common/common_data.h"
25
#include "av1/common/enums.h"
26
#include "av1/common/reconinter.h"
27
#include "av1/common/thread_common.h"
28
29
static int is_8x8_block_skip(MB_MODE_INFO **grid, int mi_row, int mi_col,
30
1.67M
                             int mi_stride) {
31
1.67M
  MB_MODE_INFO **mbmi = grid + mi_row * mi_stride + mi_col;
32
3.46M
  for (int r = 0; r < mi_size_high[BLOCK_8X8]; ++r, mbmi += mi_stride) {
33
6.15M
    for (int c = 0; c < mi_size_wide[BLOCK_8X8]; ++c) {
34
4.36M
      if (!mbmi[c]->skip_txfm) return 0;
35
4.36M
    }
36
2.57M
  }
37
38
890k
  return 1;
39
1.67M
}
40
41
int av1_cdef_compute_sb_list(const CommonModeInfoParams *const mi_params,
42
                             int mi_row, int mi_col, cdef_list *dlist,
43
50.4k
                             BLOCK_SIZE bs) {
44
50.4k
  MB_MODE_INFO **grid = mi_params->mi_grid_base;
45
50.4k
  int maxc = mi_params->mi_cols - mi_col;
46
50.4k
  int maxr = mi_params->mi_rows - mi_row;
47
48
50.4k
  if (bs == BLOCK_128X128 || bs == BLOCK_128X64)
49
0
    maxc = AOMMIN(maxc, MI_SIZE_128X128);
50
50.4k
  else
51
50.4k
    maxc = AOMMIN(maxc, MI_SIZE_64X64);
52
50.5k
  if (bs == BLOCK_128X128 || bs == BLOCK_64X128)
53
0
    maxr = AOMMIN(maxr, MI_SIZE_128X128);
54
50.4k
  else
55
50.4k
    maxr = AOMMIN(maxr, MI_SIZE_64X64);
56
57
50.4k
  const int r_step = 2;  // mi_size_high[BLOCK_8X8]
58
50.4k
  const int c_step = 2;  // mi_size_wide[BLOCK_8X8]
59
50.4k
  const int r_shift = 1;
60
50.4k
  const int c_shift = 1;
61
50.4k
  int count = 0;
62
385k
  for (int r = 0; r < maxr; r += r_step) {
63
2.00M
    for (int c = 0; c < maxc; c += c_step) {
64
1.67M
      if (!is_8x8_block_skip(grid, mi_row + r, mi_col + c,
65
1.67M
                             mi_params->mi_stride)) {
66
782k
        dlist[count].by = r >> r_shift;
67
782k
        dlist[count].bx = c >> c_shift;
68
782k
        count++;
69
782k
      }
70
1.67M
    }
71
334k
  }
72
50.4k
  return count;
73
50.4k
}
74
75
void cdef_copy_rect8_8bit_to_16bit_c(uint16_t *dst, int dstride,
76
                                     const uint8_t *src, int sstride, int width,
77
76.9k
                                     int height) {
78
1.43M
  for (int i = 0; i < height; i++) {
79
69.3M
    for (int j = 0; j < width; j++) {
80
67.9M
      dst[i * dstride + j] = src[i * sstride + j];
81
67.9M
    }
82
1.36M
  }
83
76.9k
}
84
85
#if CONFIG_AV1_HIGHBITDEPTH
86
void cdef_copy_rect8_16bit_to_16bit_c(uint16_t *dst, int dstride,
87
                                      const uint16_t *src, int sstride,
88
99.5k
                                      int width, int height) {
89
1.69M
  for (int i = 0; i < height; i++) {
90
48.3M
    for (int j = 0; j < width; j++) {
91
46.7M
      dst[i * dstride + j] = src[i * sstride + j];
92
46.7M
    }
93
1.59M
  }
94
99.5k
}
95
#endif  // CONFIG_AV1_HIGHBITDEPTH
96
97
void av1_cdef_copy_sb8_16_lowbd(uint16_t *const dst, int dstride,
98
                                const uint8_t *src, int src_voffset,
99
                                int src_hoffset, int sstride, int vsize,
100
76.9k
                                int hsize) {
101
76.9k
  const uint8_t *base = &src[src_voffset * (ptrdiff_t)sstride + src_hoffset];
102
76.9k
  cdef_copy_rect8_8bit_to_16bit(dst, dstride, base, sstride, hsize, vsize);
103
76.9k
}
104
105
#if CONFIG_AV1_HIGHBITDEPTH
106
void av1_cdef_copy_sb8_16_highbd(uint16_t *const dst, int dstride,
107
                                 const uint8_t *src, int src_voffset,
108
                                 int src_hoffset, int sstride, int vsize,
109
99.5k
                                 int hsize) {
110
99.5k
  const uint16_t *base =
111
99.5k
      &CONVERT_TO_SHORTPTR(src)[src_voffset * (ptrdiff_t)sstride + src_hoffset];
112
99.5k
  cdef_copy_rect8_16bit_to_16bit(dst, dstride, base, sstride, hsize, vsize);
113
99.5k
}
114
#endif  // CONFIG_AV1_HIGHBITDEPTH
115
116
void av1_cdef_copy_sb8_16(const AV1_COMMON *const cm, uint16_t *const dst,
117
                          int dstride, const uint8_t *src, int src_voffset,
118
176k
                          int src_hoffset, int sstride, int vsize, int hsize) {
119
176k
#if CONFIG_AV1_HIGHBITDEPTH
120
176k
  if (cm->seq_params->use_highbitdepth) {
121
99.5k
    av1_cdef_copy_sb8_16_highbd(dst, dstride, src, src_voffset, src_hoffset,
122
99.5k
                                sstride, vsize, hsize);
123
99.5k
    return;
124
99.5k
  }
125
#else
126
  (void)cm;
127
#endif  // CONFIG_AV1_HIGHBITDEPTH
128
76.9k
  av1_cdef_copy_sb8_16_lowbd(dst, dstride, src, src_voffset, src_hoffset,
129
76.9k
                             sstride, vsize, hsize);
130
76.9k
}
131
132
static inline void copy_rect(uint16_t *dst, int dstride, const uint16_t *src,
133
249k
                             int sstride, int v, int h) {
134
6.18M
  for (int i = 0; i < v; i++) {
135
56.8M
    for (int j = 0; j < h; j++) {
136
50.9M
      dst[i * dstride + j] = src[i * sstride + j];
137
50.9M
    }
138
5.93M
  }
139
249k
}
140
141
// Prepares intermediate input buffer for CDEF.
142
// Inputs:
143
//   cm: Pointer to common structure.
144
//   fb_info: Pointer to the CDEF block-level parameter structure.
145
//   colbuf: Left column buffer for CDEF.
146
//   cdef_left: Left block is filtered or not.
147
//   fbc, fbr: col and row index of a block.
148
//   plane: plane index Y/CB/CR.
149
// Returns:
150
//   Nothing will be returned.
151
static void cdef_prepare_fb(const AV1_COMMON *const cm, CdefBlockInfo *fb_info,
152
                            uint16_t **const colbuf, const int cdef_left,
153
56.6k
                            int fbc, int fbr, int plane) {
154
56.6k
  const CommonModeInfoParams *const mi_params = &cm->mi_params;
155
56.6k
  uint16_t *src = fb_info->src;
156
56.6k
  const int luma_stride =
157
56.6k
      ALIGN_POWER_OF_TWO(mi_params->mi_cols << MI_SIZE_LOG2, 4);
158
56.6k
  const int nvfb = (mi_params->mi_rows + MI_SIZE_64X64 - 1) / MI_SIZE_64X64;
159
56.6k
  const int nhfb = (mi_params->mi_cols + MI_SIZE_64X64 - 1) / MI_SIZE_64X64;
160
56.6k
  int cstart = 0;
161
56.6k
  if (!cdef_left) cstart = -CDEF_HBORDER;
162
56.6k
  int rend, cend;
163
56.6k
  const int nhb =
164
56.6k
      AOMMIN(MI_SIZE_64X64, mi_params->mi_cols - MI_SIZE_64X64 * fbc);
165
56.6k
  const int nvb =
166
56.6k
      AOMMIN(MI_SIZE_64X64, mi_params->mi_rows - MI_SIZE_64X64 * fbr);
167
56.6k
  const int hsize = nhb << fb_info->mi_wide_l2;
168
56.6k
  const int vsize = nvb << fb_info->mi_high_l2;
169
56.6k
  const uint16_t *top_linebuf = fb_info->top_linebuf[plane];
170
56.6k
  const uint16_t *bot_linebuf = fb_info->bot_linebuf[plane];
171
56.6k
  const int bot_offset = (vsize + CDEF_VBORDER) * CDEF_BSTRIDE;
172
56.6k
  const int stride =
173
56.6k
      luma_stride >> (plane == AOM_PLANE_Y ? 0 : cm->seq_params->subsampling_x);
174
175
56.6k
  if (fbc == nhfb - 1)
176
37.3k
    cend = hsize;
177
19.2k
  else
178
19.2k
    cend = hsize + CDEF_HBORDER;
179
180
56.6k
  if (fbr == nvfb - 1)
181
16.2k
    rend = vsize;
182
40.4k
  else
183
40.4k
    rend = vsize + CDEF_VBORDER;
184
185
  /* Copy in the pixels we need from the current superblock for
186
  deringing.*/
187
56.6k
  av1_cdef_copy_sb8_16(
188
56.6k
      cm, &src[CDEF_VBORDER * CDEF_BSTRIDE + CDEF_HBORDER + cstart],
189
56.6k
      CDEF_BSTRIDE, fb_info->dst, fb_info->roffset, fb_info->coffset + cstart,
190
56.6k
      fb_info->dst_stride, vsize, cend - cstart);
191
192
  /* Copy in the pixels we need for the current superblock from bottom buffer.*/
193
56.6k
  if (fbr < nvfb - 1) {
194
40.4k
    copy_rect(&src[bot_offset + CDEF_HBORDER], CDEF_BSTRIDE,
195
40.4k
              &bot_linebuf[fb_info->coffset], stride, CDEF_VBORDER, hsize);
196
40.4k
  } else {
197
16.1k
    fill_rect(&src[bot_offset + CDEF_HBORDER], CDEF_BSTRIDE, CDEF_VBORDER,
198
16.1k
              hsize, CDEF_VERY_LARGE);
199
16.1k
  }
200
56.6k
  if (fbr < nvfb - 1 && fbc > 0) {
201
14.5k
    copy_rect(&src[bot_offset], CDEF_BSTRIDE,
202
14.5k
              &bot_linebuf[fb_info->coffset - CDEF_HBORDER], stride,
203
14.5k
              CDEF_VBORDER, CDEF_HBORDER);
204
42.0k
  } else {
205
42.0k
    fill_rect(&src[bot_offset], CDEF_BSTRIDE, CDEF_VBORDER, CDEF_HBORDER,
206
42.0k
              CDEF_VERY_LARGE);
207
42.0k
  }
208
56.6k
  if (fbr < nvfb - 1 && fbc < nhfb - 1) {
209
14.7k
    copy_rect(&src[bot_offset + hsize + CDEF_HBORDER], CDEF_BSTRIDE,
210
14.7k
              &bot_linebuf[fb_info->coffset + hsize], stride, CDEF_VBORDER,
211
14.7k
              CDEF_HBORDER);
212
41.8k
  } else {
213
41.8k
    fill_rect(&src[bot_offset + hsize + CDEF_HBORDER], CDEF_BSTRIDE,
214
41.8k
              CDEF_VBORDER, CDEF_HBORDER, CDEF_VERY_LARGE);
215
41.8k
  }
216
217
  /* Copy in the pixels we need from the current superblock from top buffer.*/
218
56.6k
  if (fbr > 0) {
219
40.6k
    copy_rect(&src[CDEF_HBORDER], CDEF_BSTRIDE, &top_linebuf[fb_info->coffset],
220
40.6k
              stride, CDEF_VBORDER, hsize);
221
40.6k
  } else {
222
15.9k
    fill_rect(&src[CDEF_HBORDER], CDEF_BSTRIDE, CDEF_VBORDER, hsize,
223
15.9k
              CDEF_VERY_LARGE);
224
15.9k
  }
225
56.6k
  if (fbr > 0 && fbc > 0) {
226
14.1k
    copy_rect(src, CDEF_BSTRIDE, &top_linebuf[fb_info->coffset - CDEF_HBORDER],
227
14.1k
              stride, CDEF_VBORDER, CDEF_HBORDER);
228
42.4k
  } else {
229
42.4k
    fill_rect(src, CDEF_BSTRIDE, CDEF_VBORDER, CDEF_HBORDER, CDEF_VERY_LARGE);
230
42.4k
  }
231
56.6k
  if (fbr > 0 && fbc < nhfb - 1) {
232
14.3k
    copy_rect(&src[hsize + CDEF_HBORDER], CDEF_BSTRIDE,
233
14.3k
              &top_linebuf[fb_info->coffset + hsize], stride, CDEF_VBORDER,
234
14.3k
              CDEF_HBORDER);
235
42.2k
  } else {
236
42.2k
    fill_rect(&src[hsize + CDEF_HBORDER], CDEF_BSTRIDE, CDEF_VBORDER,
237
42.2k
              CDEF_HBORDER, CDEF_VERY_LARGE);
238
42.2k
  }
239
56.6k
  if (cdef_left) {
240
    /* If we deringed the superblock on the left then we need to copy in
241
    saved pixels. */
242
54.1k
    copy_rect(src, CDEF_BSTRIDE, colbuf[plane], CDEF_HBORDER,
243
54.1k
              rend + CDEF_VBORDER, CDEF_HBORDER);
244
54.1k
  }
245
  /* Saving pixels in case we need to dering the superblock on the
246
  right. */
247
56.6k
  copy_rect(colbuf[plane], CDEF_HBORDER, src + hsize, CDEF_BSTRIDE,
248
56.6k
            rend + CDEF_VBORDER, CDEF_HBORDER);
249
250
56.6k
  if (fb_info->frame_boundary[LEFT]) {
251
37.7k
    fill_rect(src, CDEF_BSTRIDE, vsize + 2 * CDEF_VBORDER, CDEF_HBORDER,
252
37.7k
              CDEF_VERY_LARGE);
253
37.7k
  }
254
56.6k
  if (fb_info->frame_boundary[RIGHT]) {
255
37.4k
    fill_rect(&src[hsize + CDEF_HBORDER], CDEF_BSTRIDE,
256
37.4k
              vsize + 2 * CDEF_VBORDER, CDEF_HBORDER, CDEF_VERY_LARGE);
257
37.4k
  }
258
56.6k
}
259
260
static inline void cdef_filter_fb(CdefBlockInfo *const fb_info, int plane,
261
56.6k
                                  uint8_t use_highbitdepth) {
262
56.6k
  ptrdiff_t offset =
263
56.6k
      (ptrdiff_t)fb_info->dst_stride * fb_info->roffset + fb_info->coffset;
264
56.6k
  if (use_highbitdepth) {
265
29.5k
    av1_cdef_filter_fb(
266
29.5k
        NULL, CONVERT_TO_SHORTPTR(fb_info->dst + offset), fb_info->dst_stride,
267
29.5k
        &fb_info->src[CDEF_VBORDER * CDEF_BSTRIDE + CDEF_HBORDER],
268
29.5k
        fb_info->xdec, fb_info->ydec, fb_info->dir, NULL, fb_info->var, plane,
269
29.5k
        fb_info->dlist, fb_info->cdef_count, fb_info->level,
270
29.5k
        fb_info->sec_strength, fb_info->damping, fb_info->coeff_shift);
271
29.5k
  } else {
272
27.0k
    av1_cdef_filter_fb(
273
27.0k
        fb_info->dst + offset, NULL, fb_info->dst_stride,
274
27.0k
        &fb_info->src[CDEF_VBORDER * CDEF_BSTRIDE + CDEF_HBORDER],
275
27.0k
        fb_info->xdec, fb_info->ydec, fb_info->dir, NULL, fb_info->var, plane,
276
27.0k
        fb_info->dlist, fb_info->cdef_count, fb_info->level,
277
27.0k
        fb_info->sec_strength, fb_info->damping, fb_info->coeff_shift);
278
27.0k
  }
279
56.6k
}
280
281
// Initializes block-level parameters for CDEF.
282
static inline void cdef_init_fb_col(const MACROBLOCKD *const xd,
283
                                    CdefBlockInfo *const fb_info, int *level,
284
                                    int *sec_strength, int fbc, int fbr,
285
56.6k
                                    int plane) {
286
56.6k
  const PLANE_TYPE plane_type = get_plane_type(plane);
287
56.6k
  fb_info->level = level[plane_type];
288
56.6k
  fb_info->sec_strength = sec_strength[plane_type];
289
56.6k
  fb_info->dst = xd->plane[plane].dst.buf;
290
56.6k
  fb_info->dst_stride = xd->plane[plane].dst.stride;
291
292
56.6k
  fb_info->xdec = xd->plane[plane].subsampling_x;
293
56.6k
  fb_info->ydec = xd->plane[plane].subsampling_y;
294
56.6k
  fb_info->mi_wide_l2 = MI_SIZE_LOG2 - xd->plane[plane].subsampling_x;
295
56.6k
  fb_info->mi_high_l2 = MI_SIZE_LOG2 - xd->plane[plane].subsampling_y;
296
56.6k
  fb_info->roffset = MI_SIZE_64X64 * fbr << fb_info->mi_high_l2;
297
56.6k
  fb_info->coffset = MI_SIZE_64X64 * fbc << fb_info->mi_wide_l2;
298
56.6k
}
299
300
static void cdef_fb_col(const AV1_COMMON *const cm, const MACROBLOCKD *const xd,
301
                        CdefBlockInfo *const fb_info, uint16_t **const colbuf,
302
52.3k
                        int *cdef_left, int fbc, int fbr) {
303
52.3k
  const CommonModeInfoParams *const mi_params = &cm->mi_params;
304
52.3k
  const int mbmi_cdef_strength =
305
52.3k
      mi_params
306
52.3k
          ->mi_grid_base[MI_SIZE_64X64 * fbr * mi_params->mi_stride +
307
52.3k
                         MI_SIZE_64X64 * fbc]
308
52.3k
          ->cdef_strength;
309
52.3k
  const int num_planes = av1_num_planes(cm);
310
52.3k
  int is_zero_level[PLANE_TYPES] = { 1, 1 };
311
52.3k
  int level[PLANE_TYPES] = { 0 };
312
52.3k
  int sec_strength[PLANE_TYPES] = { 0 };
313
52.3k
  const CdefInfo *const cdef_info = &cm->cdef_info;
314
315
52.3k
  if (mi_params->mi_grid_base[MI_SIZE_64X64 * fbr * mi_params->mi_stride +
316
52.3k
                              MI_SIZE_64X64 * fbc] == NULL ||
317
52.3k
      mbmi_cdef_strength == -1) {
318
0
    av1_zero_array(cdef_left, num_planes);
319
0
    return;
320
0
  }
321
322
  // Compute level and secondary strength for planes
323
52.3k
  level[PLANE_TYPE_Y] =
324
52.3k
      cdef_info->cdef_strengths[mbmi_cdef_strength] / CDEF_SEC_STRENGTHS;
325
52.3k
  sec_strength[PLANE_TYPE_Y] =
326
52.3k
      cdef_info->cdef_strengths[mbmi_cdef_strength] % CDEF_SEC_STRENGTHS;
327
52.3k
  sec_strength[PLANE_TYPE_Y] += sec_strength[PLANE_TYPE_Y] == 3;
328
52.3k
  is_zero_level[PLANE_TYPE_Y] =
329
52.3k
      (level[PLANE_TYPE_Y] == 0) && (sec_strength[PLANE_TYPE_Y] == 0);
330
331
52.3k
  if (num_planes > 1) {
332
38.2k
    level[PLANE_TYPE_UV] =
333
38.2k
        cdef_info->cdef_uv_strengths[mbmi_cdef_strength] / CDEF_SEC_STRENGTHS;
334
38.2k
    sec_strength[PLANE_TYPE_UV] =
335
38.2k
        cdef_info->cdef_uv_strengths[mbmi_cdef_strength] % CDEF_SEC_STRENGTHS;
336
38.2k
    sec_strength[PLANE_TYPE_UV] += sec_strength[PLANE_TYPE_UV] == 3;
337
38.2k
    is_zero_level[PLANE_TYPE_UV] =
338
38.2k
        (level[PLANE_TYPE_UV] == 0) && (sec_strength[PLANE_TYPE_UV] == 0);
339
38.2k
  }
340
341
52.3k
  if (is_zero_level[PLANE_TYPE_Y] && is_zero_level[PLANE_TYPE_UV]) {
342
2.09k
    av1_zero_array(cdef_left, num_planes);
343
2.09k
    return;
344
2.09k
  }
345
346
50.2k
  fb_info->cdef_count = av1_cdef_compute_sb_list(mi_params, fbr * MI_SIZE_64X64,
347
50.2k
                                                 fbc * MI_SIZE_64X64,
348
50.2k
                                                 fb_info->dlist, BLOCK_64X64);
349
50.2k
  if (!fb_info->cdef_count) {
350
26.1k
    av1_zero_array(cdef_left, num_planes);
351
26.1k
    return;
352
26.1k
  }
353
354
83.4k
  for (int plane = 0; plane < num_planes; plane++) {
355
    // Do not skip cdef filtering for luma plane as filter direction is
356
    // computed based on luma.
357
59.3k
    if (plane && is_zero_level[get_plane_type(plane)]) {
358
2.71k
      cdef_left[plane] = 0;
359
2.71k
      continue;
360
2.71k
    }
361
56.6k
    cdef_init_fb_col(xd, fb_info, level, sec_strength, fbc, fbr, plane);
362
56.6k
    cdef_prepare_fb(cm, fb_info, colbuf, cdef_left[plane], fbc, fbr, plane);
363
56.6k
    cdef_filter_fb(fb_info, plane, cm->seq_params->use_highbitdepth);
364
56.6k
    cdef_left[plane] = 1;
365
56.6k
  }
366
24.1k
}
367
368
// Initializes row-level parameters for CDEF frame.
369
void av1_cdef_init_fb_row(const AV1_COMMON *const cm,
370
                          const MACROBLOCKD *const xd,
371
                          CdefBlockInfo *const fb_info,
372
                          uint16_t **const linebuf, uint16_t *const src,
373
4.04k
                          struct AV1CdefSyncData *const cdef_sync, int fbr) {
374
4.04k
  (void)cdef_sync;
375
4.04k
  const int num_planes = av1_num_planes(cm);
376
4.04k
  const int nvfb = (cm->mi_params.mi_rows + MI_SIZE_64X64 - 1) / MI_SIZE_64X64;
377
4.04k
  const int luma_stride =
378
4.04k
      ALIGN_POWER_OF_TWO(cm->mi_params.mi_cols << MI_SIZE_LOG2, 4);
379
4.04k
  const bool ping_pong = fbr & 1;
380
  // for the current filter block, it's top left corner mi structure (mi_tl)
381
  // is first accessed to check whether the top and left boundaries are
382
  // frame boundaries. Then bottom-left and top-right mi structures are
383
  // accessed to check whether the bottom and right boundaries
384
  // (respectively) are frame boundaries.
385
  //
386
  // Note that we can't just check the bottom-right mi structure - eg. if
387
  // we're at the right-hand edge of the frame but not the bottom, then
388
  // the bottom-right mi is NULL but the bottom-left is not.
389
4.04k
  fb_info->frame_boundary[TOP] = (MI_SIZE_64X64 * fbr == 0) ? 1 : 0;
390
4.04k
  if (fbr != nvfb - 1)
391
3.24k
    fb_info->frame_boundary[BOTTOM] =
392
3.24k
        (MI_SIZE_64X64 * (fbr + 1) == cm->mi_params.mi_rows) ? 1 : 0;
393
804
  else
394
804
    fb_info->frame_boundary[BOTTOM] = 1;
395
396
4.04k
  fb_info->src = src;
397
4.04k
  fb_info->damping = cm->cdef_info.cdef_damping;
398
4.04k
  fb_info->coeff_shift = AOMMAX(cm->seq_params->bit_depth - 8, 0);
399
4.04k
  av1_zero(fb_info->dir);
400
4.04k
  av1_zero(fb_info->var);
401
402
14.9k
  for (int plane = 0; plane < num_planes; plane++) {
403
10.9k
    const int mi_high_l2 = MI_SIZE_LOG2 - xd->plane[plane].subsampling_y;
404
10.9k
    const int offset = MI_SIZE_64X64 * (fbr + 1) << mi_high_l2;
405
10.9k
    const int stride = luma_stride >> xd->plane[plane].subsampling_x;
406
    // here ping-pong buffers are maintained for top linebuf
407
    // to avoid linebuf over-write by consecutive row.
408
10.9k
    uint16_t *const top_linebuf =
409
10.9k
        &linebuf[plane][ping_pong * CDEF_VBORDER * stride];
410
10.9k
    fb_info->bot_linebuf[plane] = &linebuf[plane][(CDEF_VBORDER << 1) * stride];
411
412
10.9k
    if (fbr != nvfb - 1)  // top line buffer copy
413
9.66k
      av1_cdef_copy_sb8_16(cm, top_linebuf, stride, xd->plane[plane].dst.buf,
414
9.66k
                           offset - CDEF_VBORDER, 0,
415
9.66k
                           xd->plane[plane].dst.stride, CDEF_VBORDER, stride);
416
10.9k
    fb_info->top_linebuf[plane] =
417
10.9k
        &linebuf[plane][(!ping_pong) * CDEF_VBORDER * stride];
418
419
10.9k
    if (fbr != nvfb - 1)  // bottom line buffer copy
420
9.66k
      av1_cdef_copy_sb8_16(cm, fb_info->bot_linebuf[plane], stride,
421
9.66k
                           xd->plane[plane].dst.buf, offset, 0,
422
9.66k
                           xd->plane[plane].dst.stride, CDEF_VBORDER, stride);
423
10.9k
  }
424
4.04k
}
425
426
void av1_cdef_fb_row(const AV1_COMMON *const cm, MACROBLOCKD *xd,
427
                     uint16_t **const linebuf, uint16_t **const colbuf,
428
                     uint16_t *const src, int fbr,
429
                     cdef_init_fb_row_t cdef_init_fb_row_fn,
430
                     struct AV1CdefSyncData *const cdef_sync,
431
29.1k
                     struct aom_internal_error_info *error_info) {
432
  // TODO(aomedia:3276): Pass error_info to the low-level functions as required
433
  // in future to handle error propagation.
434
29.1k
  (void)error_info;
435
29.1k
  CdefBlockInfo fb_info;
436
29.1k
  int cdef_left[MAX_MB_PLANE] = { 1, 1, 1 };
437
29.1k
  const int nhfb = (cm->mi_params.mi_cols + MI_SIZE_64X64 - 1) / MI_SIZE_64X64;
438
439
29.1k
  cdef_init_fb_row_fn(cm, xd, &fb_info, linebuf, src, cdef_sync, fbr);
440
29.1k
#if CONFIG_MULTITHREAD
441
29.1k
  if (cdef_sync && cm->cdef_info.allocated_num_workers > 1) {
442
25.1k
    pthread_mutex_lock(cdef_sync->mutex_);
443
25.1k
    const bool cdef_mt_exit = cdef_sync->cdef_mt_exit;
444
25.1k
    pthread_mutex_unlock(cdef_sync->mutex_);
445
    // Exit in case any worker has encountered an error.
446
25.1k
    if (cdef_mt_exit) return;
447
25.1k
  }
448
29.1k
#endif
449
81.4k
  for (int fbc = 0; fbc < nhfb; fbc++) {
450
52.2k
    fb_info.frame_boundary[LEFT] = (MI_SIZE_64X64 * fbc == 0) ? 1 : 0;
451
52.2k
    if (fbc != nhfb - 1)
452
23.6k
      fb_info.frame_boundary[RIGHT] =
453
23.6k
          (MI_SIZE_64X64 * (fbc + 1) == cm->mi_params.mi_cols) ? 1 : 0;
454
28.6k
    else
455
28.6k
      fb_info.frame_boundary[RIGHT] = 1;
456
52.2k
    cdef_fb_col(cm, xd, &fb_info, colbuf, &cdef_left[0], fbc, fbr);
457
52.2k
  }
458
29.1k
}
459
460
// Perform CDEF on input frame.
461
// Inputs:
462
//   frame: Pointer to input frame buffer.
463
//   cm: Pointer to common structure.
464
//   xd: Pointer to common current coding block structure.
465
// Returns:
466
//   Nothing will be returned.
467
void av1_cdef_frame(YV12_BUFFER_CONFIG *frame, AV1_COMMON *const cm,
468
804
                    MACROBLOCKD *xd, cdef_init_fb_row_t cdef_init_fb_row_fn) {
469
804
  const int num_planes = av1_num_planes(cm);
470
804
  const int nvfb = (cm->mi_params.mi_rows + MI_SIZE_64X64 - 1) / MI_SIZE_64X64;
471
472
804
  av1_setup_dst_planes(xd->plane, cm->seq_params->sb_size, frame, 0, 0, 0,
473
804
                       num_planes);
474
475
4.85k
  for (int fbr = 0; fbr < nvfb; fbr++)
476
4.04k
    av1_cdef_fb_row(cm, xd, cm->cdef_info.linebuf, cm->cdef_info.colbuf,
477
                    cm->cdef_info.srcbuf, fbr, cdef_init_fb_row_fn, NULL,
478
4.04k
                    xd->error_info);
479
804
}