2 * Copyright (c) 2014 The WebM project authors. All Rights Reserved.
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.
11 #include "./vpx_config.h"
12 #include "vpx_dsp/vpx_dsp_common.h"
13 #include "vpx_mem/vpx_mem.h"
14 #include "vp9/common/vp9_entropymode.h"
15 #include "vp9/common/vp9_thread_common.h"
16 #include "vp9/common/vp9_reconinter.h"
17 #include "vp9/common/vp9_loopfilter.h"
19 #if CONFIG_MULTITHREAD
20 static INLINE void mutex_lock(pthread_mutex_t *const mutex) {
21 const int kMaxTryLocks = 4000;
25 for (i = 0; i < kMaxTryLocks; ++i) {
26 if (!pthread_mutex_trylock(mutex)) {
32 if (!locked) pthread_mutex_lock(mutex);
34 #endif // CONFIG_MULTITHREAD
36 static INLINE void sync_read(VP9LfSync *const lf_sync, int r, int c) {
37 #if CONFIG_MULTITHREAD
38 const int nsync = lf_sync->sync_range;
40 if (r && !(c & (nsync - 1))) {
41 pthread_mutex_t *const mutex = &lf_sync->mutex[r - 1];
44 while (c > lf_sync->cur_sb_col[r - 1] - nsync) {
45 pthread_cond_wait(&lf_sync->cond[r - 1], mutex);
47 pthread_mutex_unlock(mutex);
53 #endif // CONFIG_MULTITHREAD
56 static INLINE void sync_write(VP9LfSync *const lf_sync, int r, int c,
58 #if CONFIG_MULTITHREAD
59 const int nsync = lf_sync->sync_range;
61 // Only signal when there are enough filtered SB for next row to run.
64 if (c < sb_cols - 1) {
66 if (c % nsync) sig = 0;
68 cur = sb_cols + nsync;
72 mutex_lock(&lf_sync->mutex[r]);
74 lf_sync->cur_sb_col[r] = cur;
76 pthread_cond_signal(&lf_sync->cond[r]);
77 pthread_mutex_unlock(&lf_sync->mutex[r]);
84 #endif // CONFIG_MULTITHREAD
87 // Implement row loopfiltering for each thread.
88 static INLINE void thread_loop_filter_rows(
89 const YV12_BUFFER_CONFIG *const frame_buffer, VP9_COMMON *const cm,
90 struct macroblockd_plane planes[MAX_MB_PLANE], int start, int stop,
91 int y_only, VP9LfSync *const lf_sync) {
92 const int num_planes = y_only ? 1 : MAX_MB_PLANE;
93 const int sb_cols = mi_cols_aligned_to_sb(cm->mi_cols) >> MI_BLOCK_SIZE_LOG2;
94 const int num_active_workers = VPXMIN(lf_sync->num_workers, lf_sync->rows);
99 else if (planes[1].subsampling_y == 1 && planes[1].subsampling_x == 1)
101 else if (planes[1].subsampling_y == 0 && planes[1].subsampling_x == 0)
106 for (mi_row = start; mi_row < stop;
107 mi_row += num_active_workers * MI_BLOCK_SIZE) {
108 MODE_INFO **const mi = cm->mi_grid_visible + mi_row * cm->mi_stride;
109 LOOP_FILTER_MASK *lfm = get_lfm(&cm->lf, mi_row, 0);
111 for (mi_col = 0; mi_col < cm->mi_cols; mi_col += MI_BLOCK_SIZE, ++lfm) {
112 const int r = mi_row >> MI_BLOCK_SIZE_LOG2;
113 const int c = mi_col >> MI_BLOCK_SIZE_LOG2;
116 sync_read(lf_sync, r, c);
118 vp9_setup_dst_planes(planes, frame_buffer, mi_row, mi_col);
120 vp9_adjust_mask(cm, mi_row, mi_col, lfm);
122 vp9_filter_block_plane_ss00(cm, &planes[0], mi_row, lfm);
123 for (plane = 1; plane < num_planes; ++plane) {
126 vp9_filter_block_plane_ss11(cm, &planes[plane], mi_row, lfm);
129 vp9_filter_block_plane_ss00(cm, &planes[plane], mi_row, lfm);
132 vp9_filter_block_plane_non420(cm, &planes[plane], mi + mi_col,
138 sync_write(lf_sync, r, c, sb_cols);
143 // Row-based multi-threaded loopfilter hook
144 static int loop_filter_row_worker(void *arg1, void *arg2) {
145 VP9LfSync *const lf_sync = (VP9LfSync *)arg1;
146 LFWorkerData *const lf_data = (LFWorkerData *)arg2;
147 thread_loop_filter_rows(lf_data->frame_buffer, lf_data->cm, lf_data->planes,
148 lf_data->start, lf_data->stop, lf_data->y_only,
153 static void loop_filter_rows_mt(YV12_BUFFER_CONFIG *frame, VP9_COMMON *cm,
154 struct macroblockd_plane planes[MAX_MB_PLANE],
155 int start, int stop, int y_only,
156 VPxWorker *workers, int nworkers,
157 VP9LfSync *lf_sync) {
158 const VPxWorkerInterface *const winterface = vpx_get_worker_interface();
159 // Number of superblock rows and cols
160 const int sb_rows = mi_cols_aligned_to_sb(cm->mi_rows) >> MI_BLOCK_SIZE_LOG2;
161 const int num_workers = VPXMIN(nworkers, sb_rows);
164 if (!lf_sync->sync_range || sb_rows != lf_sync->rows ||
165 num_workers > lf_sync->num_workers) {
166 vp9_loop_filter_dealloc(lf_sync);
167 vp9_loop_filter_alloc(lf_sync, cm, sb_rows, cm->width, num_workers);
170 // Initialize cur_sb_col to -1 for all SB rows.
171 memset(lf_sync->cur_sb_col, -1, sizeof(*lf_sync->cur_sb_col) * sb_rows);
173 // Set up loopfilter thread data.
174 // The decoder is capping num_workers because it has been observed that using
175 // more threads on the loopfilter than there are cores will hurt performance
176 // on Android. This is because the system will only schedule the tile decode
177 // workers on cores equal to the number of tile columns. Then if the decoder
178 // tries to use more threads for the loopfilter, it will hurt performance
179 // because of contention. If the multithreading code changes in the future
180 // then the number of workers used by the loopfilter should be revisited.
181 for (i = 0; i < num_workers; ++i) {
182 VPxWorker *const worker = &workers[i];
183 LFWorkerData *const lf_data = &lf_sync->lfdata[i];
185 worker->hook = loop_filter_row_worker;
186 worker->data1 = lf_sync;
187 worker->data2 = lf_data;
190 vp9_loop_filter_data_reset(lf_data, frame, cm, planes);
191 lf_data->start = start + i * MI_BLOCK_SIZE;
192 lf_data->stop = stop;
193 lf_data->y_only = y_only;
195 // Start loopfiltering
196 if (i == num_workers - 1) {
197 winterface->execute(worker);
199 winterface->launch(worker);
203 // Wait till all rows are finished
204 for (i = 0; i < num_workers; ++i) {
205 winterface->sync(&workers[i]);
209 void vp9_loop_filter_frame_mt(YV12_BUFFER_CONFIG *frame, VP9_COMMON *cm,
210 struct macroblockd_plane planes[MAX_MB_PLANE],
211 int frame_filter_level, int y_only,
212 int partial_frame, VPxWorker *workers,
213 int num_workers, VP9LfSync *lf_sync) {
214 int start_mi_row, end_mi_row, mi_rows_to_filter;
216 if (!frame_filter_level) return;
219 mi_rows_to_filter = cm->mi_rows;
220 if (partial_frame && cm->mi_rows > 8) {
221 start_mi_row = cm->mi_rows >> 1;
222 start_mi_row &= 0xfffffff8;
223 mi_rows_to_filter = VPXMAX(cm->mi_rows / 8, 8);
225 end_mi_row = start_mi_row + mi_rows_to_filter;
226 vp9_loop_filter_frame_init(cm, frame_filter_level);
228 loop_filter_rows_mt(frame, cm, planes, start_mi_row, end_mi_row, y_only,
229 workers, num_workers, lf_sync);
232 // Set up nsync by width.
233 static INLINE int get_sync_range(int width) {
234 // nsync numbers are picked by testing. For example, for 4k
235 // video, using 4 gives best performance.
238 else if (width <= 1280)
240 else if (width <= 4096)
246 // Allocate memory for lf row synchronization
247 void vp9_loop_filter_alloc(VP9LfSync *lf_sync, VP9_COMMON *cm, int rows,
248 int width, int num_workers) {
249 lf_sync->rows = rows;
250 #if CONFIG_MULTITHREAD
254 CHECK_MEM_ERROR(cm, lf_sync->mutex,
255 vpx_malloc(sizeof(*lf_sync->mutex) * rows));
256 if (lf_sync->mutex) {
257 for (i = 0; i < rows; ++i) {
258 pthread_mutex_init(&lf_sync->mutex[i], NULL);
262 CHECK_MEM_ERROR(cm, lf_sync->cond,
263 vpx_malloc(sizeof(*lf_sync->cond) * rows));
265 for (i = 0; i < rows; ++i) {
266 pthread_cond_init(&lf_sync->cond[i], NULL);
270 #endif // CONFIG_MULTITHREAD
272 CHECK_MEM_ERROR(cm, lf_sync->lfdata,
273 vpx_malloc(num_workers * sizeof(*lf_sync->lfdata)));
274 lf_sync->num_workers = num_workers;
276 CHECK_MEM_ERROR(cm, lf_sync->cur_sb_col,
277 vpx_malloc(sizeof(*lf_sync->cur_sb_col) * rows));
280 lf_sync->sync_range = get_sync_range(width);
283 // Deallocate lf synchronization related mutex and data
284 void vp9_loop_filter_dealloc(VP9LfSync *lf_sync) {
285 if (lf_sync != NULL) {
286 #if CONFIG_MULTITHREAD
289 if (lf_sync->mutex != NULL) {
290 for (i = 0; i < lf_sync->rows; ++i) {
291 pthread_mutex_destroy(&lf_sync->mutex[i]);
293 vpx_free(lf_sync->mutex);
295 if (lf_sync->cond != NULL) {
296 for (i = 0; i < lf_sync->rows; ++i) {
297 pthread_cond_destroy(&lf_sync->cond[i]);
299 vpx_free(lf_sync->cond);
301 #endif // CONFIG_MULTITHREAD
302 vpx_free(lf_sync->lfdata);
303 vpx_free(lf_sync->cur_sb_col);
304 // clear the structure as the source of this call may be a resize in which
305 // case this call will be followed by an _alloc() which may fail.
310 // Accumulate frame counts.
311 void vp9_accumulate_frame_counts(FRAME_COUNTS *accum,
312 const FRAME_COUNTS *counts, int is_dec) {
315 for (i = 0; i < BLOCK_SIZE_GROUPS; i++)
316 for (j = 0; j < INTRA_MODES; j++)
317 accum->y_mode[i][j] += counts->y_mode[i][j];
319 for (i = 0; i < INTRA_MODES; i++)
320 for (j = 0; j < INTRA_MODES; j++)
321 accum->uv_mode[i][j] += counts->uv_mode[i][j];
323 for (i = 0; i < PARTITION_CONTEXTS; i++)
324 for (j = 0; j < PARTITION_TYPES; j++)
325 accum->partition[i][j] += counts->partition[i][j];
329 for (i = 0; i < TX_SIZES; i++)
330 for (j = 0; j < PLANE_TYPES; j++)
331 for (k = 0; k < REF_TYPES; k++)
332 for (l = 0; l < COEF_BANDS; l++)
333 for (m = 0; m < COEFF_CONTEXTS; m++) {
334 accum->eob_branch[i][j][k][l][m] +=
335 counts->eob_branch[i][j][k][l][m];
336 for (n = 0; n < UNCONSTRAINED_NODES + 1; n++)
337 accum->coef[i][j][k][l][m][n] += counts->coef[i][j][k][l][m][n];
340 for (i = 0; i < TX_SIZES; i++)
341 for (j = 0; j < PLANE_TYPES; j++)
342 for (k = 0; k < REF_TYPES; k++)
343 for (l = 0; l < COEF_BANDS; l++)
344 for (m = 0; m < COEFF_CONTEXTS; m++)
345 accum->eob_branch[i][j][k][l][m] +=
346 counts->eob_branch[i][j][k][l][m];
347 // In the encoder, coef is only updated at frame
348 // level, so not need to accumulate it here.
349 // for (n = 0; n < UNCONSTRAINED_NODES + 1; n++)
350 // accum->coef[i][j][k][l][m][n] +=
351 // counts->coef[i][j][k][l][m][n];
354 for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; i++)
355 for (j = 0; j < SWITCHABLE_FILTERS; j++)
356 accum->switchable_interp[i][j] += counts->switchable_interp[i][j];
358 for (i = 0; i < INTER_MODE_CONTEXTS; i++)
359 for (j = 0; j < INTER_MODES; j++)
360 accum->inter_mode[i][j] += counts->inter_mode[i][j];
362 for (i = 0; i < INTRA_INTER_CONTEXTS; i++)
363 for (j = 0; j < 2; j++)
364 accum->intra_inter[i][j] += counts->intra_inter[i][j];
366 for (i = 0; i < COMP_INTER_CONTEXTS; i++)
367 for (j = 0; j < 2; j++) accum->comp_inter[i][j] += counts->comp_inter[i][j];
369 for (i = 0; i < REF_CONTEXTS; i++)
370 for (j = 0; j < 2; j++)
371 for (k = 0; k < 2; k++)
372 accum->single_ref[i][j][k] += counts->single_ref[i][j][k];
374 for (i = 0; i < REF_CONTEXTS; i++)
375 for (j = 0; j < 2; j++) accum->comp_ref[i][j] += counts->comp_ref[i][j];
377 for (i = 0; i < TX_SIZE_CONTEXTS; i++) {
378 for (j = 0; j < TX_SIZES; j++)
379 accum->tx.p32x32[i][j] += counts->tx.p32x32[i][j];
381 for (j = 0; j < TX_SIZES - 1; j++)
382 accum->tx.p16x16[i][j] += counts->tx.p16x16[i][j];
384 for (j = 0; j < TX_SIZES - 2; j++)
385 accum->tx.p8x8[i][j] += counts->tx.p8x8[i][j];
388 for (i = 0; i < TX_SIZES; i++)
389 accum->tx.tx_totals[i] += counts->tx.tx_totals[i];
391 for (i = 0; i < SKIP_CONTEXTS; i++)
392 for (j = 0; j < 2; j++) accum->skip[i][j] += counts->skip[i][j];
394 for (i = 0; i < MV_JOINTS; i++) accum->mv.joints[i] += counts->mv.joints[i];
396 for (k = 0; k < 2; k++) {
397 nmv_component_counts *const comps = &accum->mv.comps[k];
398 const nmv_component_counts *const comps_t = &counts->mv.comps[k];
400 for (i = 0; i < 2; i++) {
401 comps->sign[i] += comps_t->sign[i];
402 comps->class0_hp[i] += comps_t->class0_hp[i];
403 comps->hp[i] += comps_t->hp[i];
406 for (i = 0; i < MV_CLASSES; i++) comps->classes[i] += comps_t->classes[i];
408 for (i = 0; i < CLASS0_SIZE; i++) {
409 comps->class0[i] += comps_t->class0[i];
410 for (j = 0; j < MV_FP_SIZE; j++)
411 comps->class0_fp[i][j] += comps_t->class0_fp[i][j];
414 for (i = 0; i < MV_OFFSET_BITS; i++)
415 for (j = 0; j < 2; j++) comps->bits[i][j] += comps_t->bits[i][j];
417 for (i = 0; i < MV_FP_SIZE; i++) comps->fp[i] += comps_t->fp[i];