• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef AOM_AV1_COMMON_THREAD_COMMON_H_
13 #define AOM_AV1_COMMON_THREAD_COMMON_H_
14 
15 #include "config/aom_config.h"
16 
17 #include "av1/common/av1_loopfilter.h"
18 #include "av1/common/cdef.h"
19 #include "aom_util/aom_pthread.h"
20 #include "aom_util/aom_thread.h"
21 
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 
26 struct AV1Common;
27 
28 typedef struct AV1LfMTInfo {
29   int mi_row;
30   int plane;
31   int dir;
32   int lpf_opt_level;
33 } AV1LfMTInfo;
34 
35 // Loopfilter row synchronization
36 typedef struct AV1LfSyncData {
37 #if CONFIG_MULTITHREAD
38   pthread_mutex_t *mutex_[MAX_MB_PLANE];
39   pthread_cond_t *cond_[MAX_MB_PLANE];
40 #endif
41   // Allocate memory to store the loop-filtered superblock index in each row.
42   int *cur_sb_col[MAX_MB_PLANE];
43   // The optimal sync_range for different resolution and platform should be
44   // determined by testing. Currently, it is chosen to be a power-of-2 number.
45   int sync_range;
46   int rows;
47 
48   // Row-based parallel loopfilter data
49   LFWorkerData *lfdata;
50   int num_workers;
51 
52 #if CONFIG_MULTITHREAD
53   pthread_mutex_t *job_mutex;
54 #endif
55   AV1LfMTInfo *job_queue;
56   int jobs_enqueued;
57   int jobs_dequeued;
58 
59   // Initialized to false, set to true by the worker thread that encounters an
60   // error in order to abort the processing of other worker threads.
61   bool lf_mt_exit;
62 } AV1LfSync;
63 
64 typedef struct AV1LrMTInfo {
65   int v_start;
66   int v_end;
67   int lr_unit_row;
68   int plane;
69   int sync_mode;
70   int v_copy_start;
71   int v_copy_end;
72 } AV1LrMTInfo;
73 
74 typedef struct LoopRestorationWorkerData {
75   int32_t *rst_tmpbuf;
76   void *rlbs;
77   void *lr_ctxt;
78   int do_extend_border;
79   struct aom_internal_error_info error_info;
80 } LRWorkerData;
81 
82 // Looprestoration row synchronization
83 typedef struct AV1LrSyncData {
84 #if CONFIG_MULTITHREAD
85   pthread_mutex_t *mutex_[MAX_MB_PLANE];
86   pthread_cond_t *cond_[MAX_MB_PLANE];
87 #endif
88   // Allocate memory to store the loop-restoration block index in each row.
89   int *cur_sb_col[MAX_MB_PLANE];
90   // The optimal sync_range for different resolution and platform should be
91   // determined by testing. Currently, it is chosen to be a power-of-2 number.
92   int sync_range;
93   int rows;
94   int num_planes;
95 
96   int num_workers;
97 
98 #if CONFIG_MULTITHREAD
99   pthread_mutex_t *job_mutex;
100 #endif
101   // Row-based parallel loopfilter data
102   LRWorkerData *lrworkerdata;
103 
104   AV1LrMTInfo *job_queue;
105   int jobs_enqueued;
106   int jobs_dequeued;
107   // Initialized to false, set to true by the worker thread that encounters
108   // an error in order to abort the processing of other worker threads.
109   bool lr_mt_exit;
110 } AV1LrSync;
111 
112 typedef struct AV1CdefWorker {
113   AV1_COMMON *cm;
114   MACROBLOCKD *xd;
115   uint16_t *colbuf[MAX_MB_PLANE];
116   uint16_t *srcbuf;
117   uint16_t *linebuf[MAX_MB_PLANE];
118   cdef_init_fb_row_t cdef_init_fb_row_fn;
119   int do_extend_border;
120   struct aom_internal_error_info error_info;
121 } AV1CdefWorkerData;
122 
123 typedef struct AV1CdefRowSync {
124 #if CONFIG_MULTITHREAD
125   pthread_mutex_t *row_mutex_;
126   pthread_cond_t *row_cond_;
127 #endif  // CONFIG_MULTITHREAD
128   int is_row_done;
129 } AV1CdefRowSync;
130 
131 // Data related to CDEF search multi-thread synchronization.
132 typedef struct AV1CdefSyncData {
133 #if CONFIG_MULTITHREAD
134   // Mutex lock used while dispatching jobs.
135   pthread_mutex_t *mutex_;
136 #endif  // CONFIG_MULTITHREAD
137   // Data related to CDEF row mt sync information
138   AV1CdefRowSync *cdef_row_mt;
139   // Flag to indicate all blocks are processed and end of frame is reached
140   int end_of_frame;
141   // Row index in units of 64x64 block
142   int fbr;
143   // Column index in units of 64x64 block
144   int fbc;
145   // Initialized to false, set to true by the worker thread that encounters
146   // an error in order to abort the processing of other worker threads.
147   bool cdef_mt_exit;
148 } AV1CdefSync;
149 
150 void av1_cdef_frame_mt(AV1_COMMON *const cm, MACROBLOCKD *const xd,
151                        AV1CdefWorkerData *const cdef_worker,
152                        AVxWorker *const workers, AV1CdefSync *const cdef_sync,
153                        int num_workers, cdef_init_fb_row_t cdef_init_fb_row_fn,
154                        int do_extend_border);
155 void av1_cdef_init_fb_row_mt(const AV1_COMMON *const cm,
156                              const MACROBLOCKD *const xd,
157                              CdefBlockInfo *const fb_info,
158                              uint16_t **const linebuf, uint16_t *const src,
159                              struct AV1CdefSyncData *const cdef_sync, int fbr);
160 void av1_cdef_copy_sb8_16(const AV1_COMMON *const cm, uint16_t *const dst,
161                           int dstride, const uint8_t *src, int src_voffset,
162                           int src_hoffset, int sstride, int vsize, int hsize);
163 void av1_cdef_copy_sb8_16_lowbd(uint16_t *const dst, int dstride,
164                                 const uint8_t *src, int src_voffset,
165                                 int src_hoffset, int sstride, int vsize,
166                                 int hsize);
167 void av1_cdef_copy_sb8_16_highbd(uint16_t *const dst, int dstride,
168                                  const uint8_t *src, int src_voffset,
169                                  int src_hoffset, int sstride, int vsize,
170                                  int hsize);
171 void av1_alloc_cdef_sync(AV1_COMMON *const cm, AV1CdefSync *cdef_sync,
172                          int num_workers);
173 void av1_free_cdef_sync(AV1CdefSync *cdef_sync);
174 
175 // Deallocate loopfilter synchronization related mutex and data.
176 void av1_loop_filter_dealloc(AV1LfSync *lf_sync);
177 void av1_loop_filter_alloc(AV1LfSync *lf_sync, AV1_COMMON *cm, int rows,
178                            int width, int num_workers);
179 
180 void av1_set_vert_loop_filter_done(AV1_COMMON *cm, AV1LfSync *lf_sync,
181                                    int num_mis_in_lpf_unit_height_log2);
182 
183 void av1_loop_filter_frame_mt(YV12_BUFFER_CONFIG *frame, struct AV1Common *cm,
184                               struct macroblockd *xd, int plane_start,
185                               int plane_end, int partial_frame,
186                               AVxWorker *workers, int num_workers,
187                               AV1LfSync *lf_sync, int lpf_opt_level);
188 
189 void av1_loop_restoration_filter_frame_mt(YV12_BUFFER_CONFIG *frame,
190                                           struct AV1Common *cm,
191                                           int optimized_lr, AVxWorker *workers,
192                                           int num_workers, AV1LrSync *lr_sync,
193                                           void *lr_ctxt, int do_extend_border);
194 void av1_loop_restoration_dealloc(AV1LrSync *lr_sync);
195 void av1_loop_restoration_alloc(AV1LrSync *lr_sync, AV1_COMMON *cm,
196                                 int num_workers, int num_rows_lr,
197                                 int num_planes, int width);
198 int av1_get_intrabc_extra_top_right_sb_delay(const AV1_COMMON *cm);
199 
200 void av1_thread_loop_filter_rows(
201     const YV12_BUFFER_CONFIG *const frame_buffer, AV1_COMMON *const cm,
202     struct macroblockd_plane *planes, MACROBLOCKD *xd, int mi_row, int plane,
203     int dir, int lpf_opt_level, AV1LfSync *const lf_sync,
204     struct aom_internal_error_info *error_info,
205     AV1_DEBLOCKING_PARAMETERS *params_buf, TX_SIZE *tx_buf, int mib_size_log2);
206 
skip_loop_filter_plane(const int planes_to_lf[MAX_MB_PLANE],int plane,int lpf_opt_level)207 static AOM_FORCE_INLINE bool skip_loop_filter_plane(
208     const int planes_to_lf[MAX_MB_PLANE], int plane, int lpf_opt_level) {
209   // If LPF_PICK_METHOD is LPF_PICK_FROM_Q, we have the option to filter both
210   // chroma planes together
211   if (lpf_opt_level == 2) {
212     if (plane == AOM_PLANE_Y) {
213       return !planes_to_lf[plane];
214     }
215     if (plane == AOM_PLANE_U) {
216       // U and V are handled together
217       return !planes_to_lf[1] && !planes_to_lf[2];
218     }
219     assert(plane == AOM_PLANE_V);
220     if (plane == AOM_PLANE_V) {
221       // V is handled when u is filtered
222       return true;
223     }
224   }
225 
226   // Normal operation mode
227   return !planes_to_lf[plane];
228 }
229 
enqueue_lf_jobs(AV1LfSync * lf_sync,int start,int stop,const int planes_to_lf[MAX_MB_PLANE],int lpf_opt_level,int num_mis_in_lpf_unit_height)230 static AOM_INLINE void enqueue_lf_jobs(AV1LfSync *lf_sync, int start, int stop,
231                                        const int planes_to_lf[MAX_MB_PLANE],
232                                        int lpf_opt_level,
233                                        int num_mis_in_lpf_unit_height) {
234   int mi_row, plane, dir;
235   AV1LfMTInfo *lf_job_queue = lf_sync->job_queue;
236   lf_sync->jobs_enqueued = 0;
237   lf_sync->jobs_dequeued = 0;
238 
239   // Launch all vertical jobs first, as they are blocking the horizontal ones.
240   // Launch top row jobs for all planes first, in case the output can be
241   // partially reconstructed row by row.
242   for (dir = 0; dir < 2; ++dir) {
243     for (mi_row = start; mi_row < stop; mi_row += num_mis_in_lpf_unit_height) {
244       for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
245         if (skip_loop_filter_plane(planes_to_lf, plane, lpf_opt_level)) {
246           continue;
247         }
248         if (!planes_to_lf[plane]) continue;
249         lf_job_queue->mi_row = mi_row;
250         lf_job_queue->plane = plane;
251         lf_job_queue->dir = dir;
252         lf_job_queue->lpf_opt_level = lpf_opt_level;
253         lf_job_queue++;
254         lf_sync->jobs_enqueued++;
255       }
256     }
257   }
258 }
259 
loop_filter_frame_mt_init(AV1_COMMON * cm,int start_mi_row,int end_mi_row,const int planes_to_lf[MAX_MB_PLANE],int num_workers,AV1LfSync * lf_sync,int lpf_opt_level,int num_mis_in_lpf_unit_height_log2)260 static AOM_INLINE void loop_filter_frame_mt_init(
261     AV1_COMMON *cm, int start_mi_row, int end_mi_row,
262     const int planes_to_lf[MAX_MB_PLANE], int num_workers, AV1LfSync *lf_sync,
263     int lpf_opt_level, int num_mis_in_lpf_unit_height_log2) {
264   // Number of superblock rows
265   const int sb_rows =
266       CEIL_POWER_OF_TWO(cm->mi_params.mi_rows, num_mis_in_lpf_unit_height_log2);
267 
268   if (!lf_sync->sync_range || sb_rows != lf_sync->rows ||
269       num_workers > lf_sync->num_workers) {
270     av1_loop_filter_dealloc(lf_sync);
271     av1_loop_filter_alloc(lf_sync, cm, sb_rows, cm->width, num_workers);
272   }
273   lf_sync->lf_mt_exit = false;
274 
275   // Initialize cur_sb_col to -1 for all SB rows.
276   for (int i = 0; i < MAX_MB_PLANE; i++) {
277     memset(lf_sync->cur_sb_col[i], -1,
278            sizeof(*(lf_sync->cur_sb_col[i])) * sb_rows);
279   }
280 
281   enqueue_lf_jobs(lf_sync, start_mi_row, end_mi_row, planes_to_lf,
282                   lpf_opt_level, (1 << num_mis_in_lpf_unit_height_log2));
283 }
284 
get_lf_job_info(AV1LfSync * lf_sync)285 static AOM_INLINE AV1LfMTInfo *get_lf_job_info(AV1LfSync *lf_sync) {
286   AV1LfMTInfo *cur_job_info = NULL;
287 
288 #if CONFIG_MULTITHREAD
289   pthread_mutex_lock(lf_sync->job_mutex);
290 
291   if (!lf_sync->lf_mt_exit && lf_sync->jobs_dequeued < lf_sync->jobs_enqueued) {
292     cur_job_info = lf_sync->job_queue + lf_sync->jobs_dequeued;
293     lf_sync->jobs_dequeued++;
294   }
295 
296   pthread_mutex_unlock(lf_sync->job_mutex);
297 #else
298   (void)lf_sync;
299 #endif
300 
301   return cur_job_info;
302 }
303 
loop_filter_data_reset(LFWorkerData * lf_data,YV12_BUFFER_CONFIG * frame_buffer,struct AV1Common * cm,MACROBLOCKD * xd)304 static AOM_INLINE void loop_filter_data_reset(LFWorkerData *lf_data,
305                                               YV12_BUFFER_CONFIG *frame_buffer,
306                                               struct AV1Common *cm,
307                                               MACROBLOCKD *xd) {
308   struct macroblockd_plane *pd = xd->plane;
309   lf_data->frame_buffer = frame_buffer;
310   lf_data->cm = cm;
311   lf_data->xd = xd;
312   for (int i = 0; i < MAX_MB_PLANE; i++) {
313     memcpy(&lf_data->planes[i].dst, &pd[i].dst, sizeof(lf_data->planes[i].dst));
314     lf_data->planes[i].subsampling_x = pd[i].subsampling_x;
315     lf_data->planes[i].subsampling_y = pd[i].subsampling_y;
316   }
317 }
318 
set_planes_to_loop_filter(const struct loopfilter * lf,int planes_to_lf[MAX_MB_PLANE],int plane_start,int plane_end)319 static AOM_INLINE void set_planes_to_loop_filter(const struct loopfilter *lf,
320                                                  int planes_to_lf[MAX_MB_PLANE],
321                                                  int plane_start,
322                                                  int plane_end) {
323   // For each luma and chroma plane, whether to filter it or not.
324   planes_to_lf[0] = (lf->filter_level[0] || lf->filter_level[1]) &&
325                     plane_start <= 0 && 0 < plane_end;
326   planes_to_lf[1] = lf->filter_level_u && plane_start <= 1 && 1 < plane_end;
327   planes_to_lf[2] = lf->filter_level_v && plane_start <= 2 && 2 < plane_end;
328 }
329 
check_planes_to_loop_filter(const struct loopfilter * lf,int planes_to_lf[MAX_MB_PLANE],int plane_start,int plane_end)330 static AOM_INLINE int check_planes_to_loop_filter(
331     const struct loopfilter *lf, int planes_to_lf[MAX_MB_PLANE],
332     int plane_start, int plane_end) {
333   set_planes_to_loop_filter(lf, planes_to_lf, plane_start, plane_end);
334   // If the luma plane is purposely not filtered, neither are the chroma
335   // planes.
336   if (!planes_to_lf[0] && plane_start <= 0 && 0 < plane_end) return 0;
337   // Early exit.
338   if (!planes_to_lf[0] && !planes_to_lf[1] && !planes_to_lf[2]) return 0;
339   return 1;
340 }
341 
342 #ifdef __cplusplus
343 }  // extern "C"
344 #endif
345 
346 #endif  // AOM_AV1_COMMON_THREAD_COMMON_H_
347