• 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 #include "av1/encoder/av1_multi_thread.h"
13 #include "av1/encoder/encodeframe.h"
14 #include "av1/encoder/encoder.h"
15 #include "av1/encoder/ethread.h"
16 #include "av1/encoder/rdopt.h"
17 #include "aom_dsp/aom_dsp_common.h"
18 
accumulate_rd_opt(ThreadData * td,ThreadData * td_t)19 static AOM_INLINE void accumulate_rd_opt(ThreadData *td, ThreadData *td_t) {
20   for (int i = 0; i < REFERENCE_MODES; i++)
21     td->rd_counts.comp_pred_diff[i] += td_t->rd_counts.comp_pred_diff[i];
22 
23   for (int i = 0; i < REF_FRAMES; i++)
24     td->rd_counts.global_motion_used[i] +=
25         td_t->rd_counts.global_motion_used[i];
26 
27   td->rd_counts.compound_ref_used_flag |=
28       td_t->rd_counts.compound_ref_used_flag;
29   td->rd_counts.skip_mode_used_flag |= td_t->rd_counts.skip_mode_used_flag;
30 
31   for (int i = 0; i < TX_SIZES_ALL; i++) {
32     for (int j = 0; j < TX_TYPES; j++)
33       td->rd_counts.tx_type_used[i][j] += td_t->rd_counts.tx_type_used[i][j];
34   }
35 
36   for (int i = 0; i < BLOCK_SIZES_ALL; i++) {
37     for (int j = 0; j < 2; j++) {
38       td->rd_counts.obmc_used[i][j] += td_t->rd_counts.obmc_used[i][j];
39     }
40   }
41 
42   for (int i = 0; i < 2; i++) {
43     td->rd_counts.warped_used[i] += td_t->rd_counts.warped_used[i];
44   }
45 }
46 
update_delta_lf_for_row_mt(AV1_COMP * cpi)47 static AOM_INLINE void update_delta_lf_for_row_mt(AV1_COMP *cpi) {
48   AV1_COMMON *cm = &cpi->common;
49   MACROBLOCKD *xd = &cpi->td.mb.e_mbd;
50   const int mib_size = cm->seq_params.mib_size;
51   const int frame_lf_count =
52       av1_num_planes(cm) > 1 ? FRAME_LF_COUNT : FRAME_LF_COUNT - 2;
53   for (int row = 0; row < cm->tiles.rows; row++) {
54     for (int col = 0; col < cm->tiles.cols; col++) {
55       TileDataEnc *tile_data = &cpi->tile_data[row * cm->tiles.cols + col];
56       const TileInfo *const tile_info = &tile_data->tile_info;
57       for (int mi_row = tile_info->mi_row_start; mi_row < tile_info->mi_row_end;
58            mi_row += mib_size) {
59         if (mi_row == tile_info->mi_row_start)
60           av1_reset_loop_filter_delta(xd, av1_num_planes(cm));
61         for (int mi_col = tile_info->mi_col_start;
62              mi_col < tile_info->mi_col_end; mi_col += mib_size) {
63           const int idx_str = cm->mi_params.mi_stride * mi_row + mi_col;
64           MB_MODE_INFO **mi = cm->mi_params.mi_grid_base + idx_str;
65           MB_MODE_INFO *mbmi = mi[0];
66           if (mbmi->skip == 1 && (mbmi->sb_type == cm->seq_params.sb_size)) {
67             for (int lf_id = 0; lf_id < frame_lf_count; ++lf_id)
68               mbmi->delta_lf[lf_id] = xd->delta_lf[lf_id];
69             mbmi->delta_lf_from_base = xd->delta_lf_from_base;
70           } else {
71             if (cm->delta_q_info.delta_lf_multi) {
72               for (int lf_id = 0; lf_id < frame_lf_count; ++lf_id)
73                 xd->delta_lf[lf_id] = mbmi->delta_lf[lf_id];
74             } else {
75               xd->delta_lf_from_base = mbmi->delta_lf_from_base;
76             }
77           }
78         }
79       }
80     }
81   }
82 }
83 
av1_row_mt_sync_read_dummy(struct AV1RowMTSyncData * const row_mt_sync,int r,int c)84 void av1_row_mt_sync_read_dummy(struct AV1RowMTSyncData *const row_mt_sync,
85                                 int r, int c) {
86   (void)row_mt_sync;
87   (void)r;
88   (void)c;
89   return;
90 }
91 
av1_row_mt_sync_write_dummy(struct AV1RowMTSyncData * const row_mt_sync,int r,int c,const int cols)92 void av1_row_mt_sync_write_dummy(struct AV1RowMTSyncData *const row_mt_sync,
93                                  int r, int c, const int cols) {
94   (void)row_mt_sync;
95   (void)r;
96   (void)c;
97   (void)cols;
98   return;
99 }
100 
av1_row_mt_sync_read(AV1RowMTSync * const row_mt_sync,int r,int c)101 void av1_row_mt_sync_read(AV1RowMTSync *const row_mt_sync, int r, int c) {
102 #if CONFIG_MULTITHREAD
103   const int nsync = row_mt_sync->sync_range;
104 
105   if (r) {
106     pthread_mutex_t *const mutex = &row_mt_sync->mutex_[r - 1];
107     pthread_mutex_lock(mutex);
108 
109     while (c > row_mt_sync->cur_col[r - 1] - nsync) {
110       pthread_cond_wait(&row_mt_sync->cond_[r - 1], mutex);
111     }
112     pthread_mutex_unlock(mutex);
113   }
114 #else
115   (void)row_mt_sync;
116   (void)r;
117   (void)c;
118 #endif  // CONFIG_MULTITHREAD
119 }
120 
av1_row_mt_sync_write(AV1RowMTSync * const row_mt_sync,int r,int c,const int cols)121 void av1_row_mt_sync_write(AV1RowMTSync *const row_mt_sync, int r, int c,
122                            const int cols) {
123 #if CONFIG_MULTITHREAD
124   const int nsync = row_mt_sync->sync_range;
125   int cur;
126   // Only signal when there are enough encoded blocks for next row to run.
127   int sig = 1;
128 
129   if (c < cols - 1) {
130     cur = c;
131     if (c % nsync) sig = 0;
132   } else {
133     cur = cols + nsync;
134   }
135 
136   if (sig) {
137     pthread_mutex_lock(&row_mt_sync->mutex_[r]);
138 
139     row_mt_sync->cur_col[r] = cur;
140 
141     pthread_cond_signal(&row_mt_sync->cond_[r]);
142     pthread_mutex_unlock(&row_mt_sync->mutex_[r]);
143   }
144 #else
145   (void)row_mt_sync;
146   (void)r;
147   (void)c;
148   (void)cols;
149 #endif  // CONFIG_MULTITHREAD
150 }
151 
152 // Allocate memory for row synchronization
av1_row_mt_sync_mem_alloc(AV1RowMTSync * row_mt_sync,AV1_COMMON * cm,int rows)153 void av1_row_mt_sync_mem_alloc(AV1RowMTSync *row_mt_sync, AV1_COMMON *cm,
154                                int rows) {
155   row_mt_sync->rows = rows;
156 #if CONFIG_MULTITHREAD
157   {
158     int i;
159 
160     CHECK_MEM_ERROR(cm, row_mt_sync->mutex_,
161                     aom_malloc(sizeof(*row_mt_sync->mutex_) * rows));
162     if (row_mt_sync->mutex_) {
163       for (i = 0; i < rows; ++i) {
164         pthread_mutex_init(&row_mt_sync->mutex_[i], NULL);
165       }
166     }
167 
168     CHECK_MEM_ERROR(cm, row_mt_sync->cond_,
169                     aom_malloc(sizeof(*row_mt_sync->cond_) * rows));
170     if (row_mt_sync->cond_) {
171       for (i = 0; i < rows; ++i) {
172         pthread_cond_init(&row_mt_sync->cond_[i], NULL);
173       }
174     }
175   }
176 #endif  // CONFIG_MULTITHREAD
177 
178   CHECK_MEM_ERROR(cm, row_mt_sync->cur_col,
179                   aom_malloc(sizeof(*row_mt_sync->cur_col) * rows));
180 
181   // Set up nsync.
182   row_mt_sync->sync_range = 1;
183 }
184 
185 // Deallocate row based multi-threading synchronization related mutex and data
av1_row_mt_sync_mem_dealloc(AV1RowMTSync * row_mt_sync)186 void av1_row_mt_sync_mem_dealloc(AV1RowMTSync *row_mt_sync) {
187   if (row_mt_sync != NULL) {
188 #if CONFIG_MULTITHREAD
189     int i;
190 
191     if (row_mt_sync->mutex_ != NULL) {
192       for (i = 0; i < row_mt_sync->rows; ++i) {
193         pthread_mutex_destroy(&row_mt_sync->mutex_[i]);
194       }
195       aom_free(row_mt_sync->mutex_);
196     }
197     if (row_mt_sync->cond_ != NULL) {
198       for (i = 0; i < row_mt_sync->rows; ++i) {
199         pthread_cond_destroy(&row_mt_sync->cond_[i]);
200       }
201       aom_free(row_mt_sync->cond_);
202     }
203 #endif  // CONFIG_MULTITHREAD
204     aom_free(row_mt_sync->cur_col);
205     // clear the structure as the source of this call may be dynamic change
206     // in tiles in which case this call will be followed by an _alloc()
207     // which may fail.
208     av1_zero(*row_mt_sync);
209   }
210 }
211 
assign_tile_to_thread(MultiThreadHandle * multi_thread_ctxt,int num_tiles,int num_workers)212 static AOM_INLINE void assign_tile_to_thread(
213     MultiThreadHandle *multi_thread_ctxt, int num_tiles, int num_workers) {
214   int tile_id = 0;
215   int i;
216 
217   for (i = 0; i < num_workers; i++) {
218     multi_thread_ctxt->thread_id_to_tile_id[i] = tile_id++;
219     if (tile_id == num_tiles) tile_id = 0;
220   }
221 }
222 
get_next_job(AV1_COMP * const cpi,int * current_mi_row,int cur_tile_id)223 static int get_next_job(AV1_COMP *const cpi, int *current_mi_row,
224                         int cur_tile_id) {
225   AV1_COMMON *const cm = &cpi->common;
226   TileDataEnc *const this_tile = &cpi->tile_data[cur_tile_id];
227   AV1RowMTInfo *row_mt_info = &this_tile->row_mt_info;
228 
229   if (row_mt_info->current_mi_row < this_tile->tile_info.mi_row_end) {
230     *current_mi_row = row_mt_info->current_mi_row;
231     row_mt_info->num_threads_working++;
232     row_mt_info->current_mi_row += cm->seq_params.mib_size;
233     return 1;
234   }
235   return 0;
236 }
237 
switch_tile_and_get_next_job(AV1_COMP * const cpi,int * cur_tile_id,int * current_mi_row,int * end_of_frame)238 static AOM_INLINE void switch_tile_and_get_next_job(AV1_COMP *const cpi,
239                                                     int *cur_tile_id,
240                                                     int *current_mi_row,
241                                                     int *end_of_frame) {
242   AV1_COMMON *const cm = &cpi->common;
243   const int tile_cols = cm->tiles.cols;
244   const int tile_rows = cm->tiles.rows;
245 
246   int tile_id = -1;  // Stores the tile ID with minimum proc done
247   int max_mis_to_encode = 0;
248   int min_num_threads_working = INT_MAX;
249 
250   for (int tile_row = 0; tile_row < tile_rows; tile_row++) {
251     for (int tile_col = 0; tile_col < tile_cols; tile_col++) {
252       int tile_index = tile_row * tile_cols + tile_col;
253       TileDataEnc *this_tile = &cpi->tile_data[tile_index];
254       AV1RowMTInfo *row_mt_info = &this_tile->row_mt_info;
255       int num_sb_rows_in_tile =
256           av1_get_sb_rows_in_tile(cm, this_tile->tile_info);
257       int num_sb_cols_in_tile =
258           av1_get_sb_cols_in_tile(cm, this_tile->tile_info);
259       int theoretical_limit_on_threads =
260           AOMMIN((num_sb_cols_in_tile + 1) >> 1, num_sb_rows_in_tile);
261       int num_threads_working = row_mt_info->num_threads_working;
262       if (num_threads_working < theoretical_limit_on_threads) {
263         int num_mis_to_encode =
264             this_tile->tile_info.mi_row_end - row_mt_info->current_mi_row;
265 
266         // Tile to be processed by this thread is selected on the basis of
267         // availability of jobs:
268         // 1) If jobs are available, tile to be processed is chosen on the
269         // basis of minimum number of threads working for that tile. If two or
270         // more tiles have same number of threads working for them, then the
271         // tile with maximum number of jobs available will be chosen.
272         // 2) If no jobs are available, then end_of_frame is reached.
273         if (num_mis_to_encode > 0) {
274           if (num_threads_working < min_num_threads_working) {
275             min_num_threads_working = num_threads_working;
276             max_mis_to_encode = 0;
277           }
278           if (num_threads_working == min_num_threads_working &&
279               num_mis_to_encode > max_mis_to_encode) {
280             tile_id = tile_index;
281             max_mis_to_encode = num_mis_to_encode;
282           }
283         }
284       }
285     }
286   }
287   if (tile_id == -1) {
288     *end_of_frame = 1;
289   } else {
290     // Update the cur ID to the next tile ID that will be processed,
291     // which will be the least processed tile
292     *cur_tile_id = tile_id;
293     get_next_job(cpi, current_mi_row, *cur_tile_id);
294   }
295 }
296 
enc_row_mt_worker_hook(void * arg1,void * unused)297 static int enc_row_mt_worker_hook(void *arg1, void *unused) {
298   EncWorkerData *const thread_data = (EncWorkerData *)arg1;
299   AV1_COMP *const cpi = thread_data->cpi;
300   AV1_COMMON *const cm = &cpi->common;
301 
302   MultiThreadHandle *multi_thread_ctxt = &cpi->multi_thread_ctxt;
303   int thread_id = thread_data->thread_id;
304   int cur_tile_id = multi_thread_ctxt->thread_id_to_tile_id[thread_id];
305   (void)unused;
306 
307   assert(cur_tile_id != -1);
308 
309   int end_of_frame = 0;
310   while (1) {
311     int current_mi_row = -1;
312 #if CONFIG_MULTITHREAD
313     pthread_mutex_lock(cpi->row_mt_mutex_);
314 #endif
315     if (!get_next_job(cpi, &current_mi_row, cur_tile_id)) {
316       // No jobs are available for the current tile. Query for the status of
317       // other tiles and get the next job if available
318       switch_tile_and_get_next_job(cpi, &cur_tile_id, &current_mi_row,
319                                    &end_of_frame);
320     }
321 #if CONFIG_MULTITHREAD
322     pthread_mutex_unlock(cpi->row_mt_mutex_);
323 #endif
324     if (end_of_frame == 1) break;
325 
326     TileDataEnc *const this_tile = &cpi->tile_data[cur_tile_id];
327     int tile_row = this_tile->tile_info.tile_row;
328     int tile_col = this_tile->tile_info.tile_col;
329 
330     assert(current_mi_row != -1 &&
331            current_mi_row <= this_tile->tile_info.mi_row_end);
332 
333     ThreadData *td = thread_data->td;
334 
335     td->mb.e_mbd.tile_ctx = td->tctx;
336     td->mb.tile_pb_ctx = &this_tile->tctx;
337     if (this_tile->allow_update_cdf) {
338       td->mb.row_ctx = this_tile->row_ctx;
339       if (current_mi_row == this_tile->tile_info.mi_row_start)
340         memcpy(td->mb.e_mbd.tile_ctx, &this_tile->tctx, sizeof(FRAME_CONTEXT));
341     } else {
342       memcpy(td->mb.e_mbd.tile_ctx, &this_tile->tctx, sizeof(FRAME_CONTEXT));
343     }
344 
345     av1_init_above_context(&cm->above_contexts, av1_num_planes(cm), tile_row,
346                            &td->mb.e_mbd);
347 
348     cfl_init(&td->mb.e_mbd.cfl, &cm->seq_params);
349     av1_crc32c_calculator_init(&td->mb.mb_rd_record.crc_calculator);
350 
351     av1_encode_sb_row(cpi, td, tile_row, tile_col, current_mi_row);
352 #if CONFIG_MULTITHREAD
353     pthread_mutex_lock(cpi->row_mt_mutex_);
354 #endif
355     this_tile->row_mt_info.num_threads_working--;
356 #if CONFIG_MULTITHREAD
357     pthread_mutex_unlock(cpi->row_mt_mutex_);
358 #endif
359   }
360 
361   return 1;
362 }
363 
enc_worker_hook(void * arg1,void * unused)364 static int enc_worker_hook(void *arg1, void *unused) {
365   EncWorkerData *const thread_data = (EncWorkerData *)arg1;
366   AV1_COMP *const cpi = thread_data->cpi;
367   const AV1_COMMON *const cm = &cpi->common;
368   const int tile_cols = cm->tiles.cols;
369   const int tile_rows = cm->tiles.rows;
370   int t;
371 
372   (void)unused;
373 
374   for (t = thread_data->start; t < tile_rows * tile_cols;
375        t += cpi->num_workers) {
376     int tile_row = t / tile_cols;
377     int tile_col = t % tile_cols;
378 
379     TileDataEnc *const this_tile =
380         &cpi->tile_data[tile_row * cm->tiles.cols + tile_col];
381     thread_data->td->mb.e_mbd.tile_ctx = &this_tile->tctx;
382     thread_data->td->mb.tile_pb_ctx = &this_tile->tctx;
383     av1_encode_tile(cpi, thread_data->td, tile_row, tile_col);
384   }
385 
386   return 1;
387 }
388 
create_enc_workers(AV1_COMP * cpi,int num_workers)389 static AOM_INLINE void create_enc_workers(AV1_COMP *cpi, int num_workers) {
390   AV1_COMMON *const cm = &cpi->common;
391   const AVxWorkerInterface *const winterface = aom_get_worker_interface();
392   int sb_mi_size = av1_get_sb_mi_size(cm);
393 
394   CHECK_MEM_ERROR(cm, cpi->workers,
395                   aom_malloc(num_workers * sizeof(*cpi->workers)));
396 
397   CHECK_MEM_ERROR(cm, cpi->tile_thr_data,
398                   aom_calloc(num_workers, sizeof(*cpi->tile_thr_data)));
399 
400 #if CONFIG_MULTITHREAD
401   if (cpi->oxcf.row_mt == 1) {
402     if (cpi->row_mt_mutex_ == NULL) {
403       CHECK_MEM_ERROR(cm, cpi->row_mt_mutex_,
404                       aom_malloc(sizeof(*(cpi->row_mt_mutex_))));
405       if (cpi->row_mt_mutex_) pthread_mutex_init(cpi->row_mt_mutex_, NULL);
406     }
407   }
408 #endif
409 
410   for (int i = num_workers - 1; i >= 0; i--) {
411     AVxWorker *const worker = &cpi->workers[i];
412     EncWorkerData *const thread_data = &cpi->tile_thr_data[i];
413 
414     ++cpi->num_workers;
415     winterface->init(worker);
416     worker->thread_name = "aom enc worker";
417 
418     thread_data->cpi = cpi;
419     thread_data->thread_id = i;
420 
421     if (i > 0) {
422       // Allocate thread data.
423       CHECK_MEM_ERROR(cm, thread_data->td,
424                       aom_memalign(32, sizeof(*thread_data->td)));
425       av1_zero(*thread_data->td);
426 
427       // Set up pc_tree.
428       thread_data->td->pc_tree = NULL;
429       av1_setup_pc_tree(cpi, thread_data->td);
430 
431       CHECK_MEM_ERROR(cm, thread_data->td->above_pred_buf,
432                       (uint8_t *)aom_memalign(
433                           16, MAX_MB_PLANE * MAX_SB_SQUARE *
434                                   sizeof(*thread_data->td->above_pred_buf)));
435       CHECK_MEM_ERROR(cm, thread_data->td->left_pred_buf,
436                       (uint8_t *)aom_memalign(
437                           16, MAX_MB_PLANE * MAX_SB_SQUARE *
438                                   sizeof(*thread_data->td->left_pred_buf)));
439 
440       CHECK_MEM_ERROR(
441           cm, thread_data->td->wsrc_buf,
442           (int32_t *)aom_memalign(
443               16, MAX_SB_SQUARE * sizeof(*thread_data->td->wsrc_buf)));
444 
445       CHECK_MEM_ERROR(cm, thread_data->td->inter_modes_info,
446                       (InterModesInfo *)aom_malloc(
447                           sizeof(*thread_data->td->inter_modes_info)));
448 
449       for (int x = 0; x < 2; x++)
450         for (int y = 0; y < 2; y++)
451           CHECK_MEM_ERROR(
452               cm, thread_data->td->hash_value_buffer[x][y],
453               (uint32_t *)aom_malloc(
454                   AOM_BUFFER_SIZE_FOR_BLOCK_HASH *
455                   sizeof(*thread_data->td->hash_value_buffer[0][0])));
456 
457       CHECK_MEM_ERROR(
458           cm, thread_data->td->mask_buf,
459           (int32_t *)aom_memalign(
460               16, MAX_SB_SQUARE * sizeof(*thread_data->td->mask_buf)));
461       // Allocate frame counters in thread data.
462       CHECK_MEM_ERROR(cm, thread_data->td->counts,
463                       aom_calloc(1, sizeof(*thread_data->td->counts)));
464 
465       // Allocate buffers used by palette coding mode.
466       CHECK_MEM_ERROR(
467           cm, thread_data->td->palette_buffer,
468           aom_memalign(16, sizeof(*thread_data->td->palette_buffer)));
469 
470       av1_alloc_compound_type_rd_buffers(cm, &thread_data->td->comp_rd_buffer);
471 
472       CHECK_MEM_ERROR(
473           cm, thread_data->td->tmp_conv_dst,
474           aom_memalign(32, MAX_SB_SIZE * MAX_SB_SIZE *
475                                sizeof(*thread_data->td->tmp_conv_dst)));
476       for (int j = 0; j < 2; ++j) {
477         CHECK_MEM_ERROR(
478             cm, thread_data->td->tmp_obmc_bufs[j],
479             aom_memalign(32, 2 * MAX_MB_PLANE * MAX_SB_SQUARE *
480                                  sizeof(*thread_data->td->tmp_obmc_bufs[j])));
481       }
482 
483       CHECK_MEM_ERROR(
484           cm, thread_data->td->mbmi_ext,
485           aom_calloc(sb_mi_size, sizeof(*thread_data->td->mbmi_ext)));
486 
487       if (cpi->sf.part_sf.partition_search_type == VAR_BASED_PARTITION) {
488         const int num_64x64_blocks =
489             (cm->seq_params.sb_size == BLOCK_64X64) ? 1 : 4;
490         CHECK_MEM_ERROR(
491             cm, thread_data->td->vt64x64,
492             aom_malloc(sizeof(*thread_data->td->vt64x64) * num_64x64_blocks));
493       }
494 
495       // Create threads
496       if (!winterface->reset(worker))
497         aom_internal_error(&cm->error, AOM_CODEC_ERROR,
498                            "Tile encoder thread creation failed");
499     } else {
500       // Main thread acts as a worker and uses the thread data in cpi.
501       thread_data->td = &cpi->td;
502     }
503     if (cpi->oxcf.row_mt == 1)
504       CHECK_MEM_ERROR(
505           cm, thread_data->td->tctx,
506           (FRAME_CONTEXT *)aom_memalign(16, sizeof(*thread_data->td->tctx)));
507     winterface->sync(worker);
508   }
509 }
510 
launch_enc_workers(AV1_COMP * cpi,int num_workers)511 static AOM_INLINE void launch_enc_workers(AV1_COMP *cpi, int num_workers) {
512   const AVxWorkerInterface *const winterface = aom_get_worker_interface();
513   // Encode a frame
514   for (int i = num_workers - 1; i >= 0; i--) {
515     AVxWorker *const worker = &cpi->workers[i];
516     EncWorkerData *const thread_data = (EncWorkerData *)worker->data1;
517 
518     // Set the starting tile for each thread.
519     thread_data->start = i;
520 
521     if (i == 0)
522       winterface->execute(worker);
523     else
524       winterface->launch(worker);
525   }
526 }
527 
sync_enc_workers(AV1_COMP * cpi,int num_workers)528 static AOM_INLINE void sync_enc_workers(AV1_COMP *cpi, int num_workers) {
529   const AVxWorkerInterface *const winterface = aom_get_worker_interface();
530   int had_error = 0;
531 
532   // Encoding ends.
533   for (int i = num_workers - 1; i >= 0; i--) {
534     AVxWorker *const worker = &cpi->workers[i];
535     had_error |= !winterface->sync(worker);
536   }
537 
538   if (had_error)
539     aom_internal_error(&cpi->common.error, AOM_CODEC_ERROR,
540                        "Failed to encode tile data");
541 }
542 
accumulate_counters_enc_workers(AV1_COMP * cpi,int num_workers)543 static AOM_INLINE void accumulate_counters_enc_workers(AV1_COMP *cpi,
544                                                        int num_workers) {
545   for (int i = num_workers - 1; i >= 0; i--) {
546     AVxWorker *const worker = &cpi->workers[i];
547     EncWorkerData *const thread_data = (EncWorkerData *)worker->data1;
548     cpi->intrabc_used |= thread_data->td->intrabc_used;
549     cpi->deltaq_used |= thread_data->td->deltaq_used;
550 
551     // Accumulate counters.
552     if (i > 0) {
553       av1_accumulate_frame_counts(&cpi->counts, thread_data->td->counts);
554       accumulate_rd_opt(&cpi->td, thread_data->td);
555       cpi->td.mb.txb_split_count += thread_data->td->mb.txb_split_count;
556 #if CONFIG_SPEED_STATS
557       cpi->td.mb.tx_search_count += thread_data->td->mb.tx_search_count;
558 #endif  // CONFIG_SPEED_STATS
559     }
560   }
561 }
562 
prepare_enc_workers(AV1_COMP * cpi,AVxWorkerHook hook,int num_workers)563 static AOM_INLINE void prepare_enc_workers(AV1_COMP *cpi, AVxWorkerHook hook,
564                                            int num_workers) {
565   for (int i = num_workers - 1; i >= 0; i--) {
566     AVxWorker *const worker = &cpi->workers[i];
567     EncWorkerData *const thread_data = &cpi->tile_thr_data[i];
568 
569     worker->hook = hook;
570     worker->data1 = thread_data;
571     worker->data2 = NULL;
572 
573     thread_data->td->intrabc_used = 0;
574     thread_data->td->deltaq_used = 0;
575 
576     // Before encoding a frame, copy the thread data from cpi.
577     if (thread_data->td != &cpi->td) {
578       thread_data->td->mb = cpi->td.mb;
579       thread_data->td->rd_counts = cpi->td.rd_counts;
580       thread_data->td->mb.above_pred_buf = thread_data->td->above_pred_buf;
581       thread_data->td->mb.left_pred_buf = thread_data->td->left_pred_buf;
582       thread_data->td->mb.wsrc_buf = thread_data->td->wsrc_buf;
583 
584       thread_data->td->mb.inter_modes_info = thread_data->td->inter_modes_info;
585       for (int x = 0; x < 2; x++) {
586         for (int y = 0; y < 2; y++) {
587           memcpy(thread_data->td->hash_value_buffer[x][y],
588                  cpi->td.mb.intrabc_hash_info.hash_value_buffer[x][y],
589                  AOM_BUFFER_SIZE_FOR_BLOCK_HASH *
590                      sizeof(*thread_data->td->hash_value_buffer[0][0]));
591           thread_data->td->mb.intrabc_hash_info.hash_value_buffer[x][y] =
592               thread_data->td->hash_value_buffer[x][y];
593         }
594       }
595       thread_data->td->mb.mask_buf = thread_data->td->mask_buf;
596       thread_data->td->mb.mbmi_ext = thread_data->td->mbmi_ext;
597     }
598     if (thread_data->td->counts != &cpi->counts) {
599       memcpy(thread_data->td->counts, &cpi->counts, sizeof(cpi->counts));
600     }
601 
602     if (i > 0) {
603       thread_data->td->mb.palette_buffer = thread_data->td->palette_buffer;
604       thread_data->td->mb.comp_rd_buffer = thread_data->td->comp_rd_buffer;
605       thread_data->td->mb.tmp_conv_dst = thread_data->td->tmp_conv_dst;
606       for (int j = 0; j < 2; ++j) {
607         thread_data->td->mb.tmp_obmc_bufs[j] =
608             thread_data->td->tmp_obmc_bufs[j];
609       }
610 
611       thread_data->td->mb.e_mbd.tmp_conv_dst = thread_data->td->mb.tmp_conv_dst;
612       for (int j = 0; j < 2; ++j) {
613         thread_data->td->mb.e_mbd.tmp_obmc_bufs[j] =
614             thread_data->td->mb.tmp_obmc_bufs[j];
615       }
616     }
617   }
618 }
619 
av1_encode_tiles_mt(AV1_COMP * cpi)620 void av1_encode_tiles_mt(AV1_COMP *cpi) {
621   AV1_COMMON *const cm = &cpi->common;
622   const int tile_cols = cm->tiles.cols;
623   const int tile_rows = cm->tiles.rows;
624   int num_workers = AOMMIN(cpi->oxcf.max_threads, tile_cols * tile_rows);
625 
626   if (cpi->tile_data == NULL || cpi->allocated_tiles < tile_cols * tile_rows)
627     av1_alloc_tile_data(cpi);
628 
629   av1_init_tile_data(cpi);
630   // Only run once to create threads and allocate thread data.
631   if (cpi->num_workers == 0) {
632     create_enc_workers(cpi, num_workers);
633   } else {
634     num_workers = AOMMIN(num_workers, cpi->num_workers);
635   }
636   prepare_enc_workers(cpi, enc_worker_hook, num_workers);
637   launch_enc_workers(cpi, num_workers);
638   sync_enc_workers(cpi, num_workers);
639   accumulate_counters_enc_workers(cpi, num_workers);
640 }
641 
642 // Accumulate frame counts. FRAME_COUNTS consist solely of 'unsigned int'
643 // members, so we treat it as an array, and sum over the whole length.
av1_accumulate_frame_counts(FRAME_COUNTS * acc_counts,const FRAME_COUNTS * counts)644 void av1_accumulate_frame_counts(FRAME_COUNTS *acc_counts,
645                                  const FRAME_COUNTS *counts) {
646   unsigned int *const acc = (unsigned int *)acc_counts;
647   const unsigned int *const cnt = (const unsigned int *)counts;
648 
649   const unsigned int n_counts = sizeof(FRAME_COUNTS) / sizeof(unsigned int);
650 
651   for (unsigned int i = 0; i < n_counts; i++) acc[i] += cnt[i];
652 }
653 
av1_encode_tiles_row_mt(AV1_COMP * cpi)654 void av1_encode_tiles_row_mt(AV1_COMP *cpi) {
655   AV1_COMMON *const cm = &cpi->common;
656   const int tile_cols = cm->tiles.cols;
657   const int tile_rows = cm->tiles.rows;
658   MultiThreadHandle *multi_thread_ctxt = &cpi->multi_thread_ctxt;
659   int num_workers = 0;
660   int total_num_threads_row_mt = 0;
661   int max_sb_rows = 0;
662 
663   if (cpi->tile_data == NULL || cpi->allocated_tiles < tile_cols * tile_rows) {
664     av1_row_mt_mem_dealloc(cpi);
665     av1_alloc_tile_data(cpi);
666   }
667 
668   av1_init_tile_data(cpi);
669 
670   for (int row = 0; row < tile_rows; row++) {
671     for (int col = 0; col < tile_cols; col++) {
672       TileDataEnc *tile_data = &cpi->tile_data[row * cm->tiles.cols + col];
673       int num_sb_rows_in_tile =
674           av1_get_sb_rows_in_tile(cm, tile_data->tile_info);
675       int num_sb_cols_in_tile =
676           av1_get_sb_cols_in_tile(cm, tile_data->tile_info);
677       total_num_threads_row_mt +=
678           AOMMIN((num_sb_cols_in_tile + 1) >> 1, num_sb_rows_in_tile);
679       max_sb_rows = AOMMAX(max_sb_rows, num_sb_rows_in_tile);
680     }
681   }
682   // TODO(ravi.chaudhary@ittiam.com): Currently the percentage of
683   // post-processing stages in encoder is quiet low, so limiting the number of
684   // threads to the theoretical limit in row-mt does not have much impact on
685   // post-processing multi-threading stage. Need to revisit this when
686   // post-processing time starts shooting up.
687   num_workers = AOMMIN(cpi->oxcf.max_threads, total_num_threads_row_mt);
688 
689   if (multi_thread_ctxt->allocated_tile_cols != tile_cols ||
690       multi_thread_ctxt->allocated_tile_rows != tile_rows ||
691       multi_thread_ctxt->allocated_sb_rows != max_sb_rows) {
692     av1_row_mt_mem_dealloc(cpi);
693     av1_row_mt_mem_alloc(cpi, max_sb_rows);
694   }
695 
696   memset(multi_thread_ctxt->thread_id_to_tile_id, -1,
697          sizeof(*multi_thread_ctxt->thread_id_to_tile_id) * MAX_NUM_THREADS);
698 
699   for (int tile_row = 0; tile_row < tile_rows; tile_row++) {
700     for (int tile_col = 0; tile_col < tile_cols; tile_col++) {
701       int tile_id = tile_row * tile_cols + tile_col;
702       TileDataEnc *this_tile = &cpi->tile_data[tile_id];
703 
704       // Initialize cur_col to -1 for all rows.
705       memset(this_tile->row_mt_sync.cur_col, -1,
706              sizeof(*this_tile->row_mt_sync.cur_col) * max_sb_rows);
707       this_tile->row_mt_info.current_mi_row = this_tile->tile_info.mi_row_start;
708       this_tile->row_mt_info.num_threads_working = 0;
709 
710       av1_inter_mode_data_init(this_tile);
711       av1_zero_above_context(cm, &cpi->td.mb.e_mbd,
712                              this_tile->tile_info.mi_col_start,
713                              this_tile->tile_info.mi_col_end, tile_row);
714     }
715   }
716 
717   // Only run once to create threads and allocate thread data.
718   if (cpi->num_workers == 0) {
719     create_enc_workers(cpi, num_workers);
720   } else {
721     num_workers = AOMMIN(num_workers, cpi->num_workers);
722   }
723   assign_tile_to_thread(multi_thread_ctxt, tile_cols * tile_rows, num_workers);
724   prepare_enc_workers(cpi, enc_row_mt_worker_hook, num_workers);
725   launch_enc_workers(cpi, num_workers);
726   sync_enc_workers(cpi, num_workers);
727   if (cm->delta_q_info.delta_lf_present_flag) update_delta_lf_for_row_mt(cpi);
728   accumulate_counters_enc_workers(cpi, num_workers);
729 }
730