Coverage Report

Created: 2026-03-12 06:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libvpx/vp9/common/vp9_thread_common.c
Line
Count
Source
1
/*
2
 *  Copyright (c) 2014 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 <assert.h>
12
#include <limits.h>
13
#include "./vpx_config.h"
14
#include "vpx_dsp/vpx_dsp_common.h"
15
#include "vpx_mem/vpx_mem.h"
16
#include "vpx_util/vpx_pthread.h"
17
#include "vp9/common/vp9_entropymode.h"
18
#include "vp9/common/vp9_thread_common.h"
19
#include "vp9/common/vp9_reconinter.h"
20
#include "vp9/common/vp9_loopfilter.h"
21
22
811k
static INLINE void sync_read(VP9LfSync *const lf_sync, int r, int c) {
23
811k
#if CONFIG_MULTITHREAD
24
811k
  const int nsync = lf_sync->sync_range;
25
26
811k
  if (r && !(c & (nsync - 1))) {
27
346k
    pthread_mutex_t *const mutex = &lf_sync->mutex[r - 1];
28
346k
    pthread_mutex_lock(mutex);
29
30
382k
    while (c > lf_sync->cur_sb_col[r - 1] - nsync) {
31
36.0k
      pthread_cond_wait(&lf_sync->cond[r - 1], mutex);
32
36.0k
    }
33
346k
    pthread_mutex_unlock(mutex);
34
346k
  }
35
#else
36
  (void)lf_sync;
37
  (void)r;
38
  (void)c;
39
#endif  // CONFIG_MULTITHREAD
40
811k
}
41
42
static INLINE void sync_write(VP9LfSync *const lf_sync, int r, int c,
43
807k
                              const int sb_cols) {
44
807k
#if CONFIG_MULTITHREAD
45
807k
  const int nsync = lf_sync->sync_range;
46
807k
  int cur;
47
  // Only signal when there are enough filtered SB for next row to run.
48
807k
  int sig = 1;
49
50
807k
  if (c < sb_cols - 1) {
51
750k
    cur = c;
52
750k
    if (c % nsync) sig = 0;
53
750k
  } else {
54
56.9k
    cur = sb_cols + nsync;
55
56.9k
  }
56
57
807k
  if (sig) {
58
493k
    pthread_mutex_lock(&lf_sync->mutex[r]);
59
60
493k
    lf_sync->cur_sb_col[r] = cur;
61
62
493k
    pthread_cond_signal(&lf_sync->cond[r]);
63
493k
    pthread_mutex_unlock(&lf_sync->mutex[r]);
64
493k
  }
65
#else
66
  (void)lf_sync;
67
  (void)r;
68
  (void)c;
69
  (void)sb_cols;
70
#endif  // CONFIG_MULTITHREAD
71
807k
}
72
73
// Implement row loopfiltering for each thread.
74
static INLINE void thread_loop_filter_rows(
75
    const YV12_BUFFER_CONFIG *const frame_buffer, VP9_COMMON *const cm,
76
    struct macroblockd_plane planes[MAX_MB_PLANE], int start, int stop,
77
44.4k
    int y_only, VP9LfSync *const lf_sync) {
78
44.4k
  const int num_planes = y_only ? 1 : MAX_MB_PLANE;
79
44.4k
  const int sb_cols = mi_cols_aligned_to_sb(cm->mi_cols) >> MI_BLOCK_SIZE_LOG2;
80
44.4k
  const int num_active_workers = lf_sync->num_active_workers;
81
44.4k
  int mi_row, mi_col;
82
44.4k
  enum lf_path path;
83
44.4k
  if (y_only)
84
0
    path = LF_PATH_444;
85
44.4k
  else if (planes[1].subsampling_y == 1 && planes[1].subsampling_x == 1)
86
17.8k
    path = LF_PATH_420;
87
26.6k
  else if (planes[1].subsampling_y == 0 && planes[1].subsampling_x == 0)
88
7.76k
    path = LF_PATH_444;
89
18.8k
  else
90
18.8k
    path = LF_PATH_SLOW;
91
92
44.4k
  assert(num_active_workers > 0);
93
94
99.6k
  for (mi_row = start; mi_row < stop;
95
57.9k
       mi_row += num_active_workers * MI_BLOCK_SIZE) {
96
57.9k
    MODE_INFO **const mi = cm->mi_grid_visible + mi_row * cm->mi_stride;
97
57.9k
    LOOP_FILTER_MASK *lfm = get_lfm(&cm->lf, mi_row, 0);
98
99
866k
    for (mi_col = 0; mi_col < cm->mi_cols; mi_col += MI_BLOCK_SIZE, ++lfm) {
100
811k
      const int r = mi_row >> MI_BLOCK_SIZE_LOG2;
101
811k
      const int c = mi_col >> MI_BLOCK_SIZE_LOG2;
102
811k
      int plane;
103
104
811k
      sync_read(lf_sync, r, c);
105
106
811k
      vp9_setup_dst_planes(planes, frame_buffer, mi_row, mi_col);
107
108
811k
      vp9_adjust_mask(cm, mi_row, mi_col, lfm);
109
110
811k
      vp9_filter_block_plane_ss00(cm, &planes[0], mi_row, lfm);
111
2.41M
      for (plane = 1; plane < num_planes; ++plane) {
112
1.60M
        switch (path) {
113
977k
          case LF_PATH_420:
114
977k
            vp9_filter_block_plane_ss11(cm, &planes[plane], mi_row, lfm);
115
977k
            break;
116
129k
          case LF_PATH_444:
117
129k
            vp9_filter_block_plane_ss00(cm, &planes[plane], mi_row, lfm);
118
129k
            break;
119
499k
          case LF_PATH_SLOW:
120
499k
            vp9_filter_block_plane_non420(cm, &planes[plane], mi + mi_col,
121
499k
                                          mi_row, mi_col);
122
499k
            break;
123
1.60M
        }
124
1.60M
      }
125
126
808k
      sync_write(lf_sync, r, c, sb_cols);
127
808k
    }
128
57.9k
  }
129
44.4k
}
130
131
// Row-based multi-threaded loopfilter hook
132
14.5k
static int loop_filter_row_worker(void *arg1, void *arg2) {
133
14.5k
  VP9LfSync *const lf_sync = (VP9LfSync *)arg1;
134
14.5k
  LFWorkerData *const lf_data = (LFWorkerData *)arg2;
135
14.5k
  thread_loop_filter_rows(lf_data->frame_buffer, lf_data->cm, lf_data->planes,
136
14.5k
                          lf_data->start, lf_data->stop, lf_data->y_only,
137
14.5k
                          lf_sync);
138
14.5k
  return 1;
139
14.5k
}
140
141
static void loop_filter_rows_mt(YV12_BUFFER_CONFIG *frame, VP9_COMMON *cm,
142
                                struct macroblockd_plane planes[MAX_MB_PLANE],
143
                                int start, int stop, int y_only,
144
                                VPxWorker *workers, int nworkers,
145
8.01k
                                VP9LfSync *lf_sync) {
146
8.01k
  const VPxWorkerInterface *const winterface = vpx_get_worker_interface();
147
  // Number of superblock rows and cols
148
8.01k
  const int sb_rows = mi_cols_aligned_to_sb(cm->mi_rows) >> MI_BLOCK_SIZE_LOG2;
149
8.01k
  const int num_tile_cols = 1 << cm->log2_tile_cols;
150
  // Limit the number of workers to prevent changes in frame dimensions from
151
  // causing incorrect sync calculations when sb_rows < threads/tile_cols.
152
  // Further restrict them by the number of tile columns should the user
153
  // request more as this implementation doesn't scale well beyond that.
154
8.01k
  const int num_workers = VPXMIN(nworkers, VPXMIN(num_tile_cols, sb_rows));
155
8.01k
  int i;
156
157
8.01k
  if (!lf_sync->sync_range || sb_rows != lf_sync->rows ||
158
6.78k
      num_workers > lf_sync->num_workers) {
159
1.22k
    vp9_loop_filter_dealloc(lf_sync);
160
1.22k
    vp9_loop_filter_alloc(lf_sync, cm, sb_rows, cm->width, num_workers);
161
1.22k
  }
162
8.01k
  lf_sync->num_active_workers = num_workers;
163
164
  // Initialize cur_sb_col to -1 for all SB rows.
165
8.01k
  memset(lf_sync->cur_sb_col, -1, sizeof(*lf_sync->cur_sb_col) * sb_rows);
166
167
  // Set up loopfilter thread data.
168
  // The decoder is capping num_workers because it has been observed that using
169
  // more threads on the loopfilter than there are cores will hurt performance
170
  // on Android. This is because the system will only schedule the tile decode
171
  // workers on cores equal to the number of tile columns. Then if the decoder
172
  // tries to use more threads for the loopfilter, it will hurt performance
173
  // because of contention. If the multithreading code changes in the future
174
  // then the number of workers used by the loopfilter should be revisited.
175
22.5k
  for (i = 0; i < num_workers; ++i) {
176
14.5k
    VPxWorker *const worker = &workers[i];
177
14.5k
    LFWorkerData *const lf_data = &lf_sync->lfdata[i];
178
179
14.5k
    worker->hook = loop_filter_row_worker;
180
14.5k
    worker->data1 = lf_sync;
181
14.5k
    worker->data2 = lf_data;
182
183
    // Loopfilter data
184
14.5k
    vp9_loop_filter_data_reset(lf_data, frame, cm, planes);
185
14.5k
    lf_data->start = start + i * MI_BLOCK_SIZE;
186
14.5k
    lf_data->stop = stop;
187
14.5k
    lf_data->y_only = y_only;
188
189
    // Start loopfiltering
190
14.5k
    if (i == num_workers - 1) {
191
8.01k
      winterface->execute(worker);
192
8.01k
    } else {
193
6.49k
      winterface->launch(worker);
194
6.49k
    }
195
14.5k
  }
196
197
  // Wait till all rows are finished
198
22.5k
  for (i = 0; i < num_workers; ++i) {
199
14.5k
    winterface->sync(&workers[i]);
200
14.5k
  }
201
8.01k
}
202
203
void vp9_loop_filter_frame_mt(YV12_BUFFER_CONFIG *frame, VP9_COMMON *cm,
204
                              struct macroblockd_plane planes[MAX_MB_PLANE],
205
                              int frame_filter_level, int y_only,
206
                              int partial_frame, VPxWorker *workers,
207
8.63k
                              int num_workers, VP9LfSync *lf_sync) {
208
8.63k
  int start_mi_row, end_mi_row, mi_rows_to_filter;
209
210
8.63k
  if (!frame_filter_level) return;
211
212
8.01k
  start_mi_row = 0;
213
8.01k
  mi_rows_to_filter = cm->mi_rows;
214
8.01k
  if (partial_frame && cm->mi_rows > 8) {
215
0
    start_mi_row = cm->mi_rows >> 1;
216
0
    start_mi_row &= 0xfffffff8;
217
0
    mi_rows_to_filter = VPXMAX(cm->mi_rows / 8, 8);
218
0
  }
219
8.01k
  end_mi_row = start_mi_row + mi_rows_to_filter;
220
8.01k
  vp9_loop_filter_frame_init(cm, frame_filter_level);
221
222
8.01k
  loop_filter_rows_mt(frame, cm, planes, start_mi_row, end_mi_row, y_only,
223
8.01k
                      workers, num_workers, lf_sync);
224
8.01k
}
225
226
void vp9_lpf_mt_init(VP9LfSync *lf_sync, VP9_COMMON *cm, int frame_filter_level,
227
10.1k
                     int num_workers) {
228
10.1k
  const int sb_rows = mi_cols_aligned_to_sb(cm->mi_rows) >> MI_BLOCK_SIZE_LOG2;
229
230
10.1k
  if (!frame_filter_level) return;
231
232
10.1k
  if (!lf_sync->sync_range || sb_rows != lf_sync->rows ||
233
5.13k
      num_workers > lf_sync->num_workers) {
234
5.04k
    vp9_loop_filter_dealloc(lf_sync);
235
5.04k
    vp9_loop_filter_alloc(lf_sync, cm, sb_rows, cm->width, num_workers);
236
5.04k
  }
237
238
  // Initialize cur_sb_col to -1 for all SB rows.
239
10.1k
  memset(lf_sync->cur_sb_col, -1, sizeof(*lf_sync->cur_sb_col) * sb_rows);
240
241
10.1k
  lf_sync->corrupted = 0;
242
243
10.1k
  memset(lf_sync->num_tiles_done, 0,
244
10.1k
         sizeof(*lf_sync->num_tiles_done) * sb_rows);
245
10.1k
  cm->lf_row = 0;
246
10.1k
}
247
248
// Set up nsync by width.
249
6.26k
static INLINE int get_sync_range(int width) {
250
  // nsync numbers are picked by testing. For example, for 4k
251
  // video, using 4 gives best performance.
252
6.26k
  if (width < 640)
253
2.69k
    return 1;
254
3.56k
  else if (width <= 1280)
255
2.26k
    return 2;
256
1.30k
  else if (width <= 4096)
257
232
    return 4;
258
1.07k
  else
259
1.07k
    return 8;
260
6.26k
}
261
262
// Allocate memory for lf row synchronization
263
void vp9_loop_filter_alloc(VP9LfSync *lf_sync, VP9_COMMON *cm, int rows,
264
6.26k
                           int width, int num_workers) {
265
6.26k
  lf_sync->rows = rows;
266
6.26k
#if CONFIG_MULTITHREAD
267
6.26k
  {
268
6.26k
    int i;
269
270
6.26k
    CHECK_MEM_ERROR(&cm->error, lf_sync->mutex,
271
6.26k
                    vpx_malloc(sizeof(*lf_sync->mutex) * rows));
272
6.26k
    if (lf_sync->mutex) {
273
43.8k
      for (i = 0; i < rows; ++i) {
274
37.5k
        pthread_mutex_init(&lf_sync->mutex[i], NULL);
275
37.5k
      }
276
6.26k
    }
277
278
6.26k
    CHECK_MEM_ERROR(&cm->error, lf_sync->cond,
279
6.26k
                    vpx_malloc(sizeof(*lf_sync->cond) * rows));
280
6.26k
    if (lf_sync->cond) {
281
43.8k
      for (i = 0; i < rows; ++i) {
282
37.5k
        pthread_cond_init(&lf_sync->cond[i], NULL);
283
37.5k
      }
284
6.26k
    }
285
286
6.26k
    CHECK_MEM_ERROR(&cm->error, lf_sync->lf_mutex,
287
6.26k
                    vpx_malloc(sizeof(*lf_sync->lf_mutex)));
288
6.26k
    pthread_mutex_init(lf_sync->lf_mutex, NULL);
289
290
6.26k
    CHECK_MEM_ERROR(&cm->error, lf_sync->recon_done_mutex,
291
6.26k
                    vpx_malloc(sizeof(*lf_sync->recon_done_mutex) * rows));
292
6.26k
    if (lf_sync->recon_done_mutex) {
293
43.8k
      for (i = 0; i < rows; ++i) {
294
37.5k
        pthread_mutex_init(&lf_sync->recon_done_mutex[i], NULL);
295
37.5k
      }
296
6.26k
    }
297
298
6.26k
    CHECK_MEM_ERROR(&cm->error, lf_sync->recon_done_cond,
299
6.26k
                    vpx_malloc(sizeof(*lf_sync->recon_done_cond) * rows));
300
6.26k
    if (lf_sync->recon_done_cond) {
301
43.8k
      for (i = 0; i < rows; ++i) {
302
37.5k
        pthread_cond_init(&lf_sync->recon_done_cond[i], NULL);
303
37.5k
      }
304
6.26k
    }
305
6.26k
  }
306
6.26k
#endif  // CONFIG_MULTITHREAD
307
308
6.26k
  CHECK_MEM_ERROR(&cm->error, lf_sync->lfdata,
309
6.26k
                  vpx_malloc(num_workers * sizeof(*lf_sync->lfdata)));
310
6.26k
  lf_sync->num_workers = num_workers;
311
6.26k
  lf_sync->num_active_workers = lf_sync->num_workers;
312
313
6.26k
  CHECK_MEM_ERROR(&cm->error, lf_sync->cur_sb_col,
314
6.26k
                  vpx_malloc(sizeof(*lf_sync->cur_sb_col) * rows));
315
316
6.26k
  CHECK_MEM_ERROR(&cm->error, lf_sync->num_tiles_done,
317
6.26k
                  vpx_malloc(sizeof(*lf_sync->num_tiles_done) *
318
6.26k
                                 mi_cols_aligned_to_sb(cm->mi_rows) >>
319
6.26k
                             MI_BLOCK_SIZE_LOG2));
320
321
  // Set up nsync.
322
6.26k
  lf_sync->sync_range = get_sync_range(width);
323
6.26k
}
324
325
// Deallocate lf synchronization related mutex and data
326
20.9k
void vp9_loop_filter_dealloc(VP9LfSync *lf_sync) {
327
20.9k
  assert(lf_sync != NULL);
328
329
20.9k
#if CONFIG_MULTITHREAD
330
20.9k
  if (lf_sync->mutex != NULL) {
331
6.26k
    int i;
332
43.8k
    for (i = 0; i < lf_sync->rows; ++i) {
333
37.5k
      pthread_mutex_destroy(&lf_sync->mutex[i]);
334
37.5k
    }
335
6.26k
    vpx_free(lf_sync->mutex);
336
6.26k
  }
337
20.9k
  if (lf_sync->cond != NULL) {
338
6.26k
    int i;
339
43.8k
    for (i = 0; i < lf_sync->rows; ++i) {
340
37.5k
      pthread_cond_destroy(&lf_sync->cond[i]);
341
37.5k
    }
342
6.26k
    vpx_free(lf_sync->cond);
343
6.26k
  }
344
20.9k
  if (lf_sync->recon_done_mutex != NULL) {
345
6.26k
    int i;
346
43.8k
    for (i = 0; i < lf_sync->rows; ++i) {
347
37.5k
      pthread_mutex_destroy(&lf_sync->recon_done_mutex[i]);
348
37.5k
    }
349
6.26k
    vpx_free(lf_sync->recon_done_mutex);
350
6.26k
  }
351
352
20.9k
  if (lf_sync->lf_mutex != NULL) {
353
6.26k
    pthread_mutex_destroy(lf_sync->lf_mutex);
354
6.26k
    vpx_free(lf_sync->lf_mutex);
355
6.26k
  }
356
20.9k
  if (lf_sync->recon_done_cond != NULL) {
357
6.26k
    int i;
358
43.8k
    for (i = 0; i < lf_sync->rows; ++i) {
359
37.5k
      pthread_cond_destroy(&lf_sync->recon_done_cond[i]);
360
37.5k
    }
361
6.26k
    vpx_free(lf_sync->recon_done_cond);
362
6.26k
  }
363
20.9k
#endif  // CONFIG_MULTITHREAD
364
365
20.9k
  vpx_free(lf_sync->lfdata);
366
20.9k
  vpx_free(lf_sync->cur_sb_col);
367
20.9k
  vpx_free(lf_sync->num_tiles_done);
368
  // clear the structure as the source of this call may be a resize in which
369
  // case this call will be followed by an _alloc() which may fail.
370
20.9k
  vp9_zero(*lf_sync);
371
20.9k
}
372
373
48.6k
static int get_next_row(VP9_COMMON *cm, VP9LfSync *lf_sync) {
374
48.6k
  int return_val = -1;
375
48.6k
  const int max_rows = cm->mi_rows;
376
377
48.6k
#if CONFIG_MULTITHREAD
378
48.6k
  int cur_row;
379
48.6k
  const int tile_cols = 1 << cm->log2_tile_cols;
380
381
48.6k
  pthread_mutex_lock(lf_sync->lf_mutex);
382
48.6k
  if (cm->lf_row < max_rows) {
383
30.4k
    cur_row = cm->lf_row >> MI_BLOCK_SIZE_LOG2;
384
30.4k
    return_val = cm->lf_row;
385
30.4k
    cm->lf_row += MI_BLOCK_SIZE;
386
30.4k
    if (cm->lf_row < max_rows) {
387
      /* If this is not the last row, make sure the next row is also decoded.
388
       * This is because the intra predict has to happen before loop filter */
389
21.8k
      cur_row += 1;
390
21.8k
    }
391
30.4k
  }
392
48.6k
  pthread_mutex_unlock(lf_sync->lf_mutex);
393
394
48.6k
  if (return_val == -1) return return_val;
395
396
30.4k
  pthread_mutex_lock(&lf_sync->recon_done_mutex[cur_row]);
397
30.4k
  if (lf_sync->num_tiles_done[cur_row] < tile_cols) {
398
6.31k
    pthread_cond_wait(&lf_sync->recon_done_cond[cur_row],
399
6.31k
                      &lf_sync->recon_done_mutex[cur_row]);
400
6.31k
  }
401
30.4k
  pthread_mutex_unlock(&lf_sync->recon_done_mutex[cur_row]);
402
30.4k
  pthread_mutex_lock(lf_sync->lf_mutex);
403
30.4k
  if (lf_sync->corrupted) {
404
442
    int row = return_val >> MI_BLOCK_SIZE_LOG2;
405
442
    pthread_mutex_lock(&lf_sync->mutex[row]);
406
442
    lf_sync->cur_sb_col[row] = INT_MAX;
407
442
    pthread_cond_signal(&lf_sync->cond[row]);
408
442
    pthread_mutex_unlock(&lf_sync->mutex[row]);
409
442
    return_val = -1;
410
442
  }
411
30.4k
  pthread_mutex_unlock(lf_sync->lf_mutex);
412
#else
413
  (void)lf_sync;
414
  if (cm->lf_row < max_rows) {
415
    return_val = cm->lf_row;
416
    cm->lf_row += MI_BLOCK_SIZE;
417
  }
418
#endif  // CONFIG_MULTITHREAD
419
420
30.4k
  return return_val;
421
48.6k
}
422
423
18.6k
void vp9_loopfilter_rows(LFWorkerData *lf_data, VP9LfSync *lf_sync) {
424
18.6k
  int mi_row;
425
18.6k
  VP9_COMMON *cm = lf_data->cm;
426
427
48.6k
  while ((mi_row = get_next_row(cm, lf_sync)) != -1 && mi_row < cm->mi_rows) {
428
29.9k
    lf_data->start = mi_row;
429
29.9k
    lf_data->stop = mi_row + MI_BLOCK_SIZE;
430
431
29.9k
    thread_loop_filter_rows(lf_data->frame_buffer, lf_data->cm, lf_data->planes,
432
29.9k
                            lf_data->start, lf_data->stop, lf_data->y_only,
433
29.9k
                            lf_sync);
434
29.9k
  }
435
18.6k
}
436
437
void vp9_set_row(VP9LfSync *lf_sync, int num_tiles, int row, int is_last_row,
438
159k
                 int corrupted) {
439
159k
#if CONFIG_MULTITHREAD
440
159k
  pthread_mutex_lock(lf_sync->lf_mutex);
441
159k
  lf_sync->corrupted |= corrupted;
442
159k
  pthread_mutex_unlock(lf_sync->lf_mutex);
443
159k
  pthread_mutex_lock(&lf_sync->recon_done_mutex[row]);
444
159k
  lf_sync->num_tiles_done[row] += 1;
445
159k
  if (num_tiles == lf_sync->num_tiles_done[row]) {
446
45.6k
    if (is_last_row) {
447
      /* The last 2 rows wait on the last row to be done.
448
       * So, we have to broadcast the signal in this case.
449
       */
450
9.69k
      pthread_cond_broadcast(&lf_sync->recon_done_cond[row]);
451
35.9k
    } else {
452
35.9k
      pthread_cond_signal(&lf_sync->recon_done_cond[row]);
453
35.9k
    }
454
45.6k
  }
455
159k
  pthread_mutex_unlock(&lf_sync->recon_done_mutex[row]);
456
#else
457
  (void)lf_sync;
458
  (void)num_tiles;
459
  (void)row;
460
  (void)is_last_row;
461
  (void)corrupted;
462
#endif  // CONFIG_MULTITHREAD
463
159k
}
464
465
0
void vp9_loopfilter_job(LFWorkerData *lf_data, VP9LfSync *lf_sync) {
466
0
  thread_loop_filter_rows(lf_data->frame_buffer, lf_data->cm, lf_data->planes,
467
0
                          lf_data->start, lf_data->stop, lf_data->y_only,
468
0
                          lf_sync);
469
0
}
470
471
// Accumulate frame counts.
472
void vp9_accumulate_frame_counts(FRAME_COUNTS *accum,
473
46.7k
                                 const FRAME_COUNTS *counts, int is_dec) {
474
46.7k
  int i, j, k, l, m;
475
476
233k
  for (i = 0; i < BLOCK_SIZE_GROUPS; i++)
477
2.05M
    for (j = 0; j < INTRA_MODES; j++)
478
1.86M
      accum->y_mode[i][j] += counts->y_mode[i][j];
479
480
513k
  for (i = 0; i < INTRA_MODES; i++)
481
5.13M
    for (j = 0; j < INTRA_MODES; j++)
482
4.67M
      accum->uv_mode[i][j] += counts->uv_mode[i][j];
483
484
794k
  for (i = 0; i < PARTITION_CONTEXTS; i++)
485
3.73M
    for (j = 0; j < PARTITION_TYPES; j++)
486
2.99M
      accum->partition[i][j] += counts->partition[i][j];
487
488
46.7k
  if (is_dec) {
489
46.7k
    int n;
490
233k
    for (i = 0; i < TX_SIZES; i++)
491
560k
      for (j = 0; j < PLANE_TYPES; j++)
492
1.12M
        for (k = 0; k < REF_TYPES; k++)
493
5.23M
          for (l = 0; l < COEF_BANDS; l++)
494
31.3M
            for (m = 0; m < COEFF_CONTEXTS; m++) {
495
26.9M
              accum->eob_branch[i][j][k][l][m] +=
496
26.9M
                  counts->eob_branch[i][j][k][l][m];
497
134M
              for (n = 0; n < UNCONSTRAINED_NODES + 1; n++)
498
107M
                accum->coef[i][j][k][l][m][n] += counts->coef[i][j][k][l][m][n];
499
26.9M
            }
500
46.7k
  } else {
501
0
    for (i = 0; i < TX_SIZES; i++)
502
0
      for (j = 0; j < PLANE_TYPES; j++)
503
0
        for (k = 0; k < REF_TYPES; k++)
504
0
          for (l = 0; l < COEF_BANDS; l++)
505
0
            for (m = 0; m < COEFF_CONTEXTS; m++)
506
0
              accum->eob_branch[i][j][k][l][m] +=
507
0
                  counts->eob_branch[i][j][k][l][m];
508
    // In the encoder, coef is only updated at frame
509
    // level, so not need to accumulate it here.
510
    // for (n = 0; n < UNCONSTRAINED_NODES + 1; n++)
511
    //   accum->coef[i][j][k][l][m][n] +=
512
    //       counts->coef[i][j][k][l][m][n];
513
0
  }
514
515
233k
  for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; i++)
516
747k
    for (j = 0; j < SWITCHABLE_FILTERS; j++)
517
560k
      accum->switchable_interp[i][j] += counts->switchable_interp[i][j];
518
519
373k
  for (i = 0; i < INTER_MODE_CONTEXTS; i++)
520
1.63M
    for (j = 0; j < INTER_MODES; j++)
521
1.30M
      accum->inter_mode[i][j] += counts->inter_mode[i][j];
522
523
233k
  for (i = 0; i < INTRA_INTER_CONTEXTS; i++)
524
560k
    for (j = 0; j < 2; j++)
525
373k
      accum->intra_inter[i][j] += counts->intra_inter[i][j];
526
527
280k
  for (i = 0; i < COMP_INTER_CONTEXTS; i++)
528
700k
    for (j = 0; j < 2; j++) accum->comp_inter[i][j] += counts->comp_inter[i][j];
529
530
280k
  for (i = 0; i < REF_CONTEXTS; i++)
531
700k
    for (j = 0; j < 2; j++)
532
1.40M
      for (k = 0; k < 2; k++)
533
934k
        accum->single_ref[i][j][k] += counts->single_ref[i][j][k];
534
535
280k
  for (i = 0; i < REF_CONTEXTS; i++)
536
700k
    for (j = 0; j < 2; j++) accum->comp_ref[i][j] += counts->comp_ref[i][j];
537
538
140k
  for (i = 0; i < TX_SIZE_CONTEXTS; i++) {
539
467k
    for (j = 0; j < TX_SIZES; j++)
540
373k
      accum->tx.p32x32[i][j] += counts->tx.p32x32[i][j];
541
542
373k
    for (j = 0; j < TX_SIZES - 1; j++)
543
280k
      accum->tx.p16x16[i][j] += counts->tx.p16x16[i][j];
544
545
280k
    for (j = 0; j < TX_SIZES - 2; j++)
546
186k
      accum->tx.p8x8[i][j] += counts->tx.p8x8[i][j];
547
93.4k
  }
548
549
233k
  for (i = 0; i < TX_SIZES; i++)
550
186k
    accum->tx.tx_totals[i] += counts->tx.tx_totals[i];
551
552
186k
  for (i = 0; i < SKIP_CONTEXTS; i++)
553
420k
    for (j = 0; j < 2; j++) accum->skip[i][j] += counts->skip[i][j];
554
555
233k
  for (i = 0; i < MV_JOINTS; i++) accum->mv.joints[i] += counts->mv.joints[i];
556
557
140k
  for (k = 0; k < 2; k++) {
558
93.4k
    nmv_component_counts *const comps = &accum->mv.comps[k];
559
93.4k
    const nmv_component_counts *const comps_t = &counts->mv.comps[k];
560
561
280k
    for (i = 0; i < 2; i++) {
562
186k
      comps->sign[i] += comps_t->sign[i];
563
186k
      comps->class0_hp[i] += comps_t->class0_hp[i];
564
186k
      comps->hp[i] += comps_t->hp[i];
565
186k
    }
566
567
1.12M
    for (i = 0; i < MV_CLASSES; i++) comps->classes[i] += comps_t->classes[i];
568
569
280k
    for (i = 0; i < CLASS0_SIZE; i++) {
570
186k
      comps->class0[i] += comps_t->class0[i];
571
934k
      for (j = 0; j < MV_FP_SIZE; j++)
572
747k
        comps->class0_fp[i][j] += comps_t->class0_fp[i][j];
573
186k
    }
574
575
1.02M
    for (i = 0; i < MV_OFFSET_BITS; i++)
576
2.80M
      for (j = 0; j < 2; j++) comps->bits[i][j] += comps_t->bits[i][j];
577
578
467k
    for (i = 0; i < MV_FP_SIZE; i++) comps->fp[i] += comps_t->fp[i];
579
93.4k
  }
580
46.7k
}