]> granicus.if.org Git - libvpx/blob - vp10/common/thread_common.c
bbc6d115de290b55669f84a49e576dfba24c21ca
[libvpx] / vp10 / common / thread_common.c
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 "./vpx_config.h"
12 #include "vpx_dsp/vpx_dsp_common.h"
13 #include "vpx_mem/vpx_mem.h"
14 #include "vp10/common/entropymode.h"
15 #include "vp10/common/thread_common.h"
16 #include "vp10/common/reconinter.h"
17 #include "vp10/common/loopfilter.h"
18
19 #if CONFIG_MULTITHREAD
20 static INLINE void mutex_lock(pthread_mutex_t *const mutex) {
21   const int kMaxTryLocks = 4000;
22   int locked = 0;
23   int i;
24
25   for (i = 0; i < kMaxTryLocks; ++i) {
26     if (!pthread_mutex_trylock(mutex)) {
27       locked = 1;
28       break;
29     }
30   }
31
32   if (!locked)
33     pthread_mutex_lock(mutex);
34 }
35 #endif  // CONFIG_MULTITHREAD
36
37 static INLINE void sync_read(VP9LfSync *const lf_sync, int r, int c) {
38 #if CONFIG_MULTITHREAD
39   const int nsync = lf_sync->sync_range;
40
41   if (r && !(c & (nsync - 1))) {
42     pthread_mutex_t *const mutex = &lf_sync->mutex_[r - 1];
43     mutex_lock(mutex);
44
45     while (c > lf_sync->cur_sb_col[r - 1] - nsync) {
46       pthread_cond_wait(&lf_sync->cond_[r - 1], mutex);
47     }
48     pthread_mutex_unlock(mutex);
49   }
50 #else
51   (void)lf_sync;
52   (void)r;
53   (void)c;
54 #endif  // CONFIG_MULTITHREAD
55 }
56
57 static INLINE void sync_write(VP9LfSync *const lf_sync, int r, int c,
58                               const int sb_cols) {
59 #if CONFIG_MULTITHREAD
60   const int nsync = lf_sync->sync_range;
61   int cur;
62   // Only signal when there are enough filtered SB for next row to run.
63   int sig = 1;
64
65   if (c < sb_cols - 1) {
66     cur = c;
67     if (c % nsync)
68       sig = 0;
69   } else {
70     cur = sb_cols + nsync;
71   }
72
73   if (sig) {
74     mutex_lock(&lf_sync->mutex_[r]);
75
76     lf_sync->cur_sb_col[r] = cur;
77
78     pthread_cond_signal(&lf_sync->cond_[r]);
79     pthread_mutex_unlock(&lf_sync->mutex_[r]);
80   }
81 #else
82   (void)lf_sync;
83   (void)r;
84   (void)c;
85   (void)sb_cols;
86 #endif  // CONFIG_MULTITHREAD
87 }
88
89 // Implement row loopfiltering for each thread.
90 static INLINE
91 void thread_loop_filter_rows(const YV12_BUFFER_CONFIG *const frame_buffer,
92                              VP10_COMMON *const cm,
93                              struct macroblockd_plane planes[MAX_MB_PLANE],
94                              int start, int stop, int y_only,
95                              VP9LfSync *const lf_sync) {
96   const int num_planes = y_only ? 1 : MAX_MB_PLANE;
97   const int sb_cols = mi_cols_aligned_to_sb(cm->mi_cols) >> MI_BLOCK_SIZE_LOG2;
98   int mi_row, mi_col;
99   enum lf_path path;
100   if (y_only)
101     path = LF_PATH_444;
102   else if (planes[1].subsampling_y == 1 && planes[1].subsampling_x == 1)
103     path = LF_PATH_420;
104   else if (planes[1].subsampling_y == 0 && planes[1].subsampling_x == 0)
105     path = LF_PATH_444;
106   else
107     path = LF_PATH_SLOW;
108
109   for (mi_row = start; mi_row < stop;
110        mi_row += lf_sync->num_workers * MI_BLOCK_SIZE) {
111     MODE_INFO **const mi = cm->mi_grid_visible + mi_row * cm->mi_stride;
112
113     for (mi_col = 0; mi_col < cm->mi_cols; mi_col += MI_BLOCK_SIZE) {
114       const int r = mi_row >> MI_BLOCK_SIZE_LOG2;
115       const int c = mi_col >> MI_BLOCK_SIZE_LOG2;
116       LOOP_FILTER_MASK lfm;
117       int plane;
118
119       sync_read(lf_sync, r, c);
120
121       vp10_setup_dst_planes(planes, frame_buffer, mi_row, mi_col);
122
123       // TODO(JBB): Make setup_mask work for non 420.
124       vp10_setup_mask(cm, mi_row, mi_col, mi + mi_col, cm->mi_stride,
125                      &lfm);
126
127       vp10_filter_block_plane_ss00(cm, &planes[0], mi_row, &lfm);
128       for (plane = 1; plane < num_planes; ++plane) {
129         switch (path) {
130           case LF_PATH_420:
131             vp10_filter_block_plane_ss11(cm, &planes[plane], mi_row, &lfm);
132             break;
133           case LF_PATH_444:
134             vp10_filter_block_plane_ss00(cm, &planes[plane], mi_row, &lfm);
135             break;
136           case LF_PATH_SLOW:
137             vp10_filter_block_plane_non420(cm, &planes[plane], mi + mi_col,
138                                           mi_row, mi_col);
139             break;
140         }
141       }
142
143       sync_write(lf_sync, r, c, sb_cols);
144     }
145   }
146 }
147
148 // Row-based multi-threaded loopfilter hook
149 static int loop_filter_row_worker(VP9LfSync *const lf_sync,
150                                   LFWorkerData *const lf_data) {
151   thread_loop_filter_rows(lf_data->frame_buffer, lf_data->cm, lf_data->planes,
152                           lf_data->start, lf_data->stop, lf_data->y_only,
153                           lf_sync);
154   return 1;
155 }
156
157 static void loop_filter_rows_mt(YV12_BUFFER_CONFIG *frame,
158                                 VP10_COMMON *cm,
159                                 struct macroblockd_plane planes[MAX_MB_PLANE],
160                                 int start, int stop, int y_only,
161                                 VPxWorker *workers, int nworkers,
162                                 VP9LfSync *lf_sync) {
163   const VPxWorkerInterface *const winterface = vpx_get_worker_interface();
164   // Number of superblock rows and cols
165   const int sb_rows = mi_cols_aligned_to_sb(cm->mi_rows) >> MI_BLOCK_SIZE_LOG2;
166   // Decoder may allocate more threads than number of tiles based on user's
167   // input.
168   const int tile_cols = 1 << cm->log2_tile_cols;
169   const int num_workers = VPXMIN(nworkers, tile_cols);
170   int i;
171
172   if (!lf_sync->sync_range || sb_rows != lf_sync->rows ||
173       num_workers > lf_sync->num_workers) {
174     vp10_loop_filter_dealloc(lf_sync);
175     vp10_loop_filter_alloc(lf_sync, cm, sb_rows, cm->width, num_workers);
176   }
177
178   // Initialize cur_sb_col to -1 for all SB rows.
179   memset(lf_sync->cur_sb_col, -1, sizeof(*lf_sync->cur_sb_col) * sb_rows);
180
181   // Set up loopfilter thread data.
182   // The decoder is capping num_workers because it has been observed that using
183   // more threads on the loopfilter than there are cores will hurt performance
184   // on Android. This is because the system will only schedule the tile decode
185   // workers on cores equal to the number of tile columns. Then if the decoder
186   // tries to use more threads for the loopfilter, it will hurt performance
187   // because of contention. If the multithreading code changes in the future
188   // then the number of workers used by the loopfilter should be revisited.
189   for (i = 0; i < num_workers; ++i) {
190     VPxWorker *const worker = &workers[i];
191     LFWorkerData *const lf_data = &lf_sync->lfdata[i];
192
193     worker->hook = (VPxWorkerHook)loop_filter_row_worker;
194     worker->data1 = lf_sync;
195     worker->data2 = lf_data;
196
197     // Loopfilter data
198     vp10_loop_filter_data_reset(lf_data, frame, cm, planes);
199     lf_data->start = start + i * MI_BLOCK_SIZE;
200     lf_data->stop = stop;
201     lf_data->y_only = y_only;
202
203     // Start loopfiltering
204     if (i == num_workers - 1) {
205       winterface->execute(worker);
206     } else {
207       winterface->launch(worker);
208     }
209   }
210
211   // Wait till all rows are finished
212   for (i = 0; i < num_workers; ++i) {
213     winterface->sync(&workers[i]);
214   }
215 }
216
217 void vp10_loop_filter_frame_mt(YV12_BUFFER_CONFIG *frame,
218                               VP10_COMMON *cm,
219                               struct macroblockd_plane planes[MAX_MB_PLANE],
220                               int frame_filter_level,
221                               int y_only, int partial_frame,
222                               VPxWorker *workers, int num_workers,
223                               VP9LfSync *lf_sync) {
224   int start_mi_row, end_mi_row, mi_rows_to_filter;
225
226   if (!frame_filter_level) return;
227
228   start_mi_row = 0;
229   mi_rows_to_filter = cm->mi_rows;
230   if (partial_frame && cm->mi_rows > 8) {
231     start_mi_row = cm->mi_rows >> 1;
232     start_mi_row &= 0xfffffff8;
233     mi_rows_to_filter = VPXMAX(cm->mi_rows / 8, 8);
234   }
235   end_mi_row = start_mi_row + mi_rows_to_filter;
236   vp10_loop_filter_frame_init(cm, frame_filter_level);
237
238   loop_filter_rows_mt(frame, cm, planes, start_mi_row, end_mi_row,
239                       y_only, workers, num_workers, lf_sync);
240 }
241
242 // Set up nsync by width.
243 static INLINE int get_sync_range(int width) {
244   // nsync numbers are picked by testing. For example, for 4k
245   // video, using 4 gives best performance.
246   if (width < 640)
247     return 1;
248   else if (width <= 1280)
249     return 2;
250   else if (width <= 4096)
251     return 4;
252   else
253     return 8;
254 }
255
256 // Allocate memory for lf row synchronization
257 void vp10_loop_filter_alloc(VP9LfSync *lf_sync, VP10_COMMON *cm, int rows,
258                            int width, int num_workers) {
259   lf_sync->rows = rows;
260 #if CONFIG_MULTITHREAD
261   {
262     int i;
263
264     CHECK_MEM_ERROR(cm, lf_sync->mutex_,
265                     vpx_malloc(sizeof(*lf_sync->mutex_) * rows));
266     if (lf_sync->mutex_) {
267       for (i = 0; i < rows; ++i) {
268         pthread_mutex_init(&lf_sync->mutex_[i], NULL);
269       }
270     }
271
272     CHECK_MEM_ERROR(cm, lf_sync->cond_,
273                     vpx_malloc(sizeof(*lf_sync->cond_) * rows));
274     if (lf_sync->cond_) {
275       for (i = 0; i < rows; ++i) {
276         pthread_cond_init(&lf_sync->cond_[i], NULL);
277       }
278     }
279   }
280 #endif  // CONFIG_MULTITHREAD
281
282   CHECK_MEM_ERROR(cm, lf_sync->lfdata,
283                   vpx_malloc(num_workers * sizeof(*lf_sync->lfdata)));
284   lf_sync->num_workers = num_workers;
285
286   CHECK_MEM_ERROR(cm, lf_sync->cur_sb_col,
287                   vpx_malloc(sizeof(*lf_sync->cur_sb_col) * rows));
288
289   // Set up nsync.
290   lf_sync->sync_range = get_sync_range(width);
291 }
292
293 // Deallocate lf synchronization related mutex and data
294 void vp10_loop_filter_dealloc(VP9LfSync *lf_sync) {
295   if (lf_sync != NULL) {
296 #if CONFIG_MULTITHREAD
297     int i;
298
299     if (lf_sync->mutex_ != NULL) {
300       for (i = 0; i < lf_sync->rows; ++i) {
301         pthread_mutex_destroy(&lf_sync->mutex_[i]);
302       }
303       vpx_free(lf_sync->mutex_);
304     }
305     if (lf_sync->cond_ != NULL) {
306       for (i = 0; i < lf_sync->rows; ++i) {
307         pthread_cond_destroy(&lf_sync->cond_[i]);
308       }
309       vpx_free(lf_sync->cond_);
310     }
311 #endif  // CONFIG_MULTITHREAD
312     vpx_free(lf_sync->lfdata);
313     vpx_free(lf_sync->cur_sb_col);
314     // clear the structure as the source of this call may be a resize in which
315     // case this call will be followed by an _alloc() which may fail.
316     vp10_zero(*lf_sync);
317   }
318 }
319
320 // Accumulate frame counts.
321 void vp10_accumulate_frame_counts(VP10_COMMON *cm, FRAME_COUNTS *counts,
322                                  int is_dec) {
323   int i, j, k, l, m;
324
325   for (i = 0; i < BLOCK_SIZE_GROUPS; i++)
326     for (j = 0; j < INTRA_MODES; j++)
327       cm->counts.y_mode[i][j] += counts->y_mode[i][j];
328
329   for (i = 0; i < INTRA_MODES; i++)
330     for (j = 0; j < INTRA_MODES; j++)
331       cm->counts.uv_mode[i][j] += counts->uv_mode[i][j];
332
333   for (i = 0; i < PARTITION_CONTEXTS; i++)
334     for (j = 0; j < PARTITION_TYPES; j++)
335       cm->counts.partition[i][j] += counts->partition[i][j];
336
337   if (is_dec) {
338     int n;
339     for (i = 0; i < TX_SIZES; i++)
340       for (j = 0; j < PLANE_TYPES; j++)
341         for (k = 0; k < REF_TYPES; k++)
342           for (l = 0; l < COEF_BANDS; l++)
343             for (m = 0; m < COEFF_CONTEXTS; m++) {
344               cm->counts.eob_branch[i][j][k][l][m] +=
345                   counts->eob_branch[i][j][k][l][m];
346               for (n = 0; n < UNCONSTRAINED_NODES + 1; n++)
347                 cm->counts.coef[i][j][k][l][m][n] +=
348                     counts->coef[i][j][k][l][m][n];
349             }
350   } else {
351     for (i = 0; i < TX_SIZES; i++)
352       for (j = 0; j < PLANE_TYPES; j++)
353         for (k = 0; k < REF_TYPES; k++)
354           for (l = 0; l < COEF_BANDS; l++)
355             for (m = 0; m < COEFF_CONTEXTS; m++)
356               cm->counts.eob_branch[i][j][k][l][m] +=
357                   counts->eob_branch[i][j][k][l][m];
358                 // In the encoder, cm->counts.coef is only updated at frame
359                 // level, so not need to accumulate it here.
360                 // for (n = 0; n < UNCONSTRAINED_NODES + 1; n++)
361                 //   cm->counts.coef[i][j][k][l][m][n] +=
362                 //       counts->coef[i][j][k][l][m][n];
363   }
364
365   for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; i++)
366     for (j = 0; j < SWITCHABLE_FILTERS; j++)
367       cm->counts.switchable_interp[i][j] += counts->switchable_interp[i][j];
368
369   for (i = 0; i < INTER_MODE_CONTEXTS; i++)
370     for (j = 0; j < INTER_MODES; j++)
371       cm->counts.inter_mode[i][j] += counts->inter_mode[i][j];
372
373   for (i = 0; i < INTRA_INTER_CONTEXTS; i++)
374     for (j = 0; j < 2; j++)
375       cm->counts.intra_inter[i][j] += counts->intra_inter[i][j];
376
377   for (i = 0; i < COMP_INTER_CONTEXTS; i++)
378     for (j = 0; j < 2; j++)
379       cm->counts.comp_inter[i][j] += counts->comp_inter[i][j];
380
381   for (i = 0; i < REF_CONTEXTS; i++)
382     for (j = 0; j < 2; j++)
383       for (k = 0; k < 2; k++)
384       cm->counts.single_ref[i][j][k] += counts->single_ref[i][j][k];
385
386   for (i = 0; i < REF_CONTEXTS; i++)
387     for (j = 0; j < 2; j++)
388       cm->counts.comp_ref[i][j] += counts->comp_ref[i][j];
389
390   for (i = 0; i < TX_SIZE_CONTEXTS; i++) {
391     for (j = 0; j < TX_SIZES; j++)
392       cm->counts.tx.p32x32[i][j] += counts->tx.p32x32[i][j];
393
394     for (j = 0; j < TX_SIZES - 1; j++)
395       cm->counts.tx.p16x16[i][j] += counts->tx.p16x16[i][j];
396
397     for (j = 0; j < TX_SIZES - 2; j++)
398       cm->counts.tx.p8x8[i][j] += counts->tx.p8x8[i][j];
399   }
400
401   for (i = 0; i < TX_SIZES; i++)
402     cm->counts.tx.tx_totals[i] += counts->tx.tx_totals[i];
403
404   for (i = 0; i < SKIP_CONTEXTS; i++)
405     for (j = 0; j < 2; j++)
406       cm->counts.skip[i][j] += counts->skip[i][j];
407
408   for (i = 0; i < MV_JOINTS; i++)
409     cm->counts.mv.joints[i] += counts->mv.joints[i];
410
411   for (k = 0; k < 2; k++) {
412     nmv_component_counts *comps = &cm->counts.mv.comps[k];
413     nmv_component_counts *comps_t = &counts->mv.comps[k];
414
415     for (i = 0; i < 2; i++) {
416       comps->sign[i] += comps_t->sign[i];
417       comps->class0_hp[i] += comps_t->class0_hp[i];
418       comps->hp[i] += comps_t->hp[i];
419     }
420
421     for (i = 0; i < MV_CLASSES; i++)
422       comps->classes[i] += comps_t->classes[i];
423
424     for (i = 0; i < CLASS0_SIZE; i++) {
425       comps->class0[i] += comps_t->class0[i];
426       for (j = 0; j < MV_FP_SIZE; j++)
427         comps->class0_fp[i][j] += comps_t->class0_fp[i][j];
428     }
429
430     for (i = 0; i < MV_OFFSET_BITS; i++)
431       for (j = 0; j < 2; j++)
432         comps->bits[i][j] += comps_t->bits[i][j];
433
434     for (i = 0; i < MV_FP_SIZE; i++)
435       comps->fp[i] += comps_t->fp[i];
436   }
437 }