aboutsummaryrefslogtreecommitdiff
path: root/vp9/encoder/vp9_multi_thread.c
blob: 0843cd97e47249c34c62016955a9a23255cee0ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*
 *  Copyright (c) 2017 The WebM project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include <assert.h>

#include "vp9/encoder/vp9_encoder.h"
#include "vp9/encoder/vp9_ethread.h"
#include "vp9/encoder/vp9_multi_thread.h"
#include "vp9/encoder/vp9_temporal_filter.h"

void *vp9_enc_grp_get_next_job(MultiThreadHandle *multi_thread_ctxt,
                               int tile_id) {
  RowMTInfo *row_mt_info;
  JobQueueHandle *job_queue_hdl = NULL;
  void *next = NULL;
  JobNode *job_info = NULL;
#if CONFIG_MULTITHREAD
  pthread_mutex_t *mutex_handle = NULL;
#endif

  row_mt_info = (RowMTInfo *)(&multi_thread_ctxt->row_mt_info[tile_id]);
  job_queue_hdl = (JobQueueHandle *)&row_mt_info->job_queue_hdl;
#if CONFIG_MULTITHREAD
  mutex_handle = &row_mt_info->job_mutex;
#endif

// lock the mutex for queue access
#if CONFIG_MULTITHREAD
  pthread_mutex_lock(mutex_handle);
#endif
  next = job_queue_hdl->next;
  if (next != NULL) {
    JobQueue *job_queue = (JobQueue *)next;
    job_info = &job_queue->job_info;
    // Update the next job in the queue
    job_queue_hdl->next = job_queue->next;
    job_queue_hdl->num_jobs_acquired++;
  }

#if CONFIG_MULTITHREAD
  pthread_mutex_unlock(mutex_handle);
#endif

  return job_info;
}

void vp9_row_mt_alloc_rd_thresh(VP9_COMP *const cpi,
                                TileDataEnc *const this_tile) {
  VP9_COMMON *const cm = &cpi->common;
  const int sb_rows =
      (mi_cols_aligned_to_sb(cm->mi_rows) >> MI_BLOCK_SIZE_LOG2) + 1;
  int i;

  CHECK_MEM_ERROR(
      &cm->error, this_tile->row_base_thresh_freq_fact,
      (int *)vpx_calloc(sb_rows * BLOCK_SIZES * MAX_MODES,
                        sizeof(*(this_tile->row_base_thresh_freq_fact))));
  for (i = 0; i < sb_rows * BLOCK_SIZES * MAX_MODES; i++)
    this_tile->row_base_thresh_freq_fact[i] = RD_THRESH_INIT_FACT;
}

void vp9_row_mt_mem_alloc(VP9_COMP *cpi) {
  struct VP9Common *cm = &cpi->common;
  MultiThreadHandle *multi_thread_ctxt = &cpi->multi_thread_ctxt;
  int tile_row, tile_col;
  const int tile_cols = 1 << cm->log2_tile_cols;
  const int tile_rows = 1 << cm->log2_tile_rows;
  const int sb_rows = mi_cols_aligned_to_sb(cm->mi_rows) >> MI_BLOCK_SIZE_LOG2;
  int jobs_per_tile_col, total_jobs;

  // Allocate memory that is large enough for all row_mt stages. First pass
  // uses 16x16 block size.
  jobs_per_tile_col = VPXMAX(cm->mb_rows, sb_rows);
  // Calculate the total number of jobs
  total_jobs = jobs_per_tile_col * tile_cols;

  multi_thread_ctxt->allocated_tile_cols = tile_cols;
  multi_thread_ctxt->allocated_tile_rows = tile_rows;
  multi_thread_ctxt->allocated_vert_unit_rows = jobs_per_tile_col;

  CHECK_MEM_ERROR(&cm->error, multi_thread_ctxt->job_queue,
                  (JobQueue *)vpx_memalign(32, total_jobs * sizeof(JobQueue)));

#if CONFIG_MULTITHREAD
  // Create mutex for each tile
  for (tile_col = 0; tile_col < tile_cols; tile_col++) {
    RowMTInfo *row_mt_info = &multi_thread_ctxt->row_mt_info[tile_col];
    pthread_mutex_init(&row_mt_info->job_mutex, NULL);
  }
#endif

  // Allocate memory for row based multi-threading
  for (tile_col = 0; tile_col < tile_cols; tile_col++) {
    TileDataEnc *this_tile = &cpi->tile_data[tile_col];
    vp9_row_mt_sync_mem_alloc(&this_tile->row_mt_sync, cm, jobs_per_tile_col);
    if (cpi->sf.adaptive_rd_thresh_row_mt) {
      if (this_tile->row_base_thresh_freq_fact != NULL) {
        vpx_free(this_tile->row_base_thresh_freq_fact);
        this_tile->row_base_thresh_freq_fact = NULL;
      }
      vp9_row_mt_alloc_rd_thresh(cpi, this_tile);
    }
  }

  // Assign the sync pointer of tile row zero for every tile row > 0
  for (tile_row = 1; tile_row < tile_rows; tile_row++) {
    for (tile_col = 0; tile_col < tile_cols; tile_col++) {
      TileDataEnc *this_tile = &cpi->tile_data[tile_row * tile_cols + tile_col];
      TileDataEnc *this_col_tile = &cpi->tile_data[tile_col];
      this_tile->row_mt_sync = this_col_tile->row_mt_sync;
    }
  }

  // Calculate the number of vertical units in the given tile row
  for (tile_row = 0; tile_row < tile_rows; tile_row++) {
    TileDataEnc *this_tile = &cpi->tile_data[tile_row * tile_cols];
    TileInfo *tile_info = &this_tile->tile_info;
    multi_thread_ctxt->num_tile_vert_sbs[tile_row] =
        get_num_vert_units(*tile_info, MI_BLOCK_SIZE_LOG2);
  }
}

void vp9_row_mt_mem_dealloc(VP9_COMP *cpi) {
  MultiThreadHandle *multi_thread_ctxt = &cpi->multi_thread_ctxt;
  int tile_col;
#if CONFIG_MULTITHREAD
  int tile_row;
#endif

  // Deallocate memory for job queue
  if (multi_thread_ctxt->job_queue) vpx_free(multi_thread_ctxt->job_queue);

#if CONFIG_MULTITHREAD
  // Destroy mutex for each tile
  for (tile_col = 0; tile_col < multi_thread_ctxt->allocated_tile_cols;
       tile_col++) {
    RowMTInfo *row_mt_info = &multi_thread_ctxt->row_mt_info[tile_col];
    if (row_mt_info) pthread_mutex_destroy(&row_mt_info->job_mutex);
  }
#endif

  // Free row based multi-threading sync memory
  for (tile_col = 0; tile_col < multi_thread_ctxt->allocated_tile_cols;
       tile_col++) {
    TileDataEnc *this_tile = &cpi->tile_data[tile_col];
    vp9_row_mt_sync_mem_dealloc(&this_tile->row_mt_sync);
  }

#if CONFIG_MULTITHREAD
  for (tile_row = 0; tile_row < multi_thread_ctxt->allocated_tile_rows;
       tile_row++) {
    for (tile_col = 0; tile_col < multi_thread_ctxt->allocated_tile_cols;
         tile_col++) {
      TileDataEnc *this_tile =
          &cpi->tile_data[tile_row * multi_thread_ctxt->allocated_tile_cols +
                          tile_col];
      if (this_tile->row_base_thresh_freq_fact != NULL) {
        vpx_free(this_tile->row_base_thresh_freq_fact);
        this_tile->row_base_thresh_freq_fact = NULL;
      }
    }
  }
#endif
}

void vp9_multi_thread_tile_init(VP9_COMP *cpi) {
  VP9_COMMON *const cm = &cpi->common;
  const int tile_cols = 1 << cm->log2_tile_cols;
  const int sb_rows = mi_cols_aligned_to_sb(cm->mi_rows) >> MI_BLOCK_SIZE_LOG2;
  int i;

  for (i = 0; i < tile_cols; i++) {
    TileDataEnc *this_tile = &cpi->tile_data[i];
    int jobs_per_tile_col = cpi->oxcf.pass == 1 ? cm->mb_rows : sb_rows;

    // Initialize cur_col to -1 for all rows.
    memset(this_tile->row_mt_sync.cur_col, -1,
           sizeof(*this_tile->row_mt_sync.cur_col) * jobs_per_tile_col);
    vp9_zero(this_tile->fp_data);
    this_tile->fp_data.image_data_start_row = INVALID_ROW;
  }
}

void vp9_assign_tile_to_thread(MultiThreadHandle *multi_thread_ctxt,
                               int tile_cols, int num_workers) {
  int tile_id = 0;
  int i;

  // Allocating the threads for the tiles
  for (i = 0; i < num_workers; i++) {
    multi_thread_ctxt->thread_id_to_tile_id[i] = tile_id++;
    if (tile_id == tile_cols) tile_id = 0;
  }
}

int vp9_get_job_queue_status(MultiThreadHandle *multi_thread_ctxt,
                             int cur_tile_id) {
  RowMTInfo *row_mt_info;
  JobQueueHandle *job_queue_hndl;
#if CONFIG_MULTITHREAD
  pthread_mutex_t *mutex;
#endif
  int num_jobs_remaining;

  row_mt_info = &multi_thread_ctxt->row_mt_info[cur_tile_id];
  job_queue_hndl = &row_mt_info->job_queue_hdl;
#if CONFIG_MULTITHREAD
  mutex = &row_mt_info->job_mutex;
#endif

#if CONFIG_MULTITHREAD
  pthread_mutex_lock(mutex);
#endif
  num_jobs_remaining =
      multi_thread_ctxt->jobs_per_tile_col - job_queue_hndl->num_jobs_acquired;
#if CONFIG_MULTITHREAD
  pthread_mutex_unlock(mutex);
#endif

  return (num_jobs_remaining);
}

void vp9_prepare_job_queue(VP9_COMP *cpi, JOB_TYPE job_type) {
  VP9_COMMON *const cm = &cpi->common;
  MultiThreadHandle *multi_thread_ctxt = &cpi->multi_thread_ctxt;
  JobQueue *job_queue = multi_thread_ctxt->job_queue;
  const int tile_cols = 1 << cm->log2_tile_cols;
  int job_row_num, jobs_per_tile, jobs_per_tile_col = 0, total_jobs;
  const int sb_rows = mi_cols_aligned_to_sb(cm->mi_rows) >> MI_BLOCK_SIZE_LOG2;
  int tile_col, i;

  switch (job_type) {
    case ENCODE_JOB: jobs_per_tile_col = sb_rows; break;
    case FIRST_PASS_JOB: jobs_per_tile_col = cm->mb_rows; break;
    case ARNR_JOB:
      jobs_per_tile_col = ((cm->mi_rows + TF_ROUND) >> TF_SHIFT);
      break;
    default: assert(0);
  }

  total_jobs = jobs_per_tile_col * tile_cols;

  multi_thread_ctxt->jobs_per_tile_col = jobs_per_tile_col;
  // memset the entire job queue buffer to zero
  memset(job_queue, 0, total_jobs * sizeof(JobQueue));

  // Job queue preparation
  for (tile_col = 0; tile_col < tile_cols; tile_col++) {
    RowMTInfo *tile_ctxt = &multi_thread_ctxt->row_mt_info[tile_col];
    JobQueue *job_queue_curr, *job_queue_temp;
    int tile_row = 0;

    tile_ctxt->job_queue_hdl.next = (void *)job_queue;
    tile_ctxt->job_queue_hdl.num_jobs_acquired = 0;

    job_queue_curr = job_queue;
    job_queue_temp = job_queue;

    // loop over all the vertical rows
    for (job_row_num = 0, jobs_per_tile = 0; job_row_num < jobs_per_tile_col;
         job_row_num++, jobs_per_tile++) {
      job_queue_curr->job_info.vert_unit_row_num = job_row_num;
      job_queue_curr->job_info.tile_col_id = tile_col;
      job_queue_curr->job_info.tile_row_id = tile_row;
      job_queue_curr->next = (void *)(job_queue_temp + 1);
      job_queue_curr = ++job_queue_temp;

      if (ENCODE_JOB == job_type) {
        if (jobs_per_tile >=
            multi_thread_ctxt->num_tile_vert_sbs[tile_row] - 1) {
          tile_row++;
          jobs_per_tile = -1;
        }
      }
    }

    // Set the last pointer to NULL
    job_queue_curr += -1;
    job_queue_curr->next = (void *)NULL;

    // Move to the next tile
    job_queue += jobs_per_tile_col;
  }

  for (i = 0; i < cpi->num_workers; i++) {
    EncWorkerData *thread_data;
    thread_data = &cpi->tile_thr_data[i];
    thread_data->thread_id = i;

    for (tile_col = 0; tile_col < tile_cols; tile_col++)
      thread_data->tile_completion_status[tile_col] = 0;
  }
}

int vp9_get_tiles_proc_status(MultiThreadHandle *multi_thread_ctxt,
                              int *tile_completion_status, int *cur_tile_id,
                              int tile_cols) {
  int tile_col;
  int tile_id = -1;  // Stores the tile ID with minimum proc done
  int max_num_jobs_remaining = 0;
  int num_jobs_remaining;

  // Mark the completion to avoid check in the loop
  tile_completion_status[*cur_tile_id] = 1;
  // Check for the status of all the tiles
  for (tile_col = 0; tile_col < tile_cols; tile_col++) {
    if (tile_completion_status[tile_col] == 0) {
      num_jobs_remaining =
          vp9_get_job_queue_status(multi_thread_ctxt, tile_col);
      // Mark the completion to avoid checks during future switches across tiles
      if (num_jobs_remaining == 0) tile_completion_status[tile_col] = 1;
      if (num_jobs_remaining > max_num_jobs_remaining) {
        max_num_jobs_remaining = num_jobs_remaining;
        tile_id = tile_col;
      }
    }
  }

  if (-1 == tile_id) {
    return 1;
  } else {
    // Update the cur ID to the next tile ID that will be processed,
    // which will be the least processed tile
    *cur_tile_id = tile_id;
    return 0;
  }
}