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 "vp9/encoder/vp9_encodeframe.h"
12 #include "vp9/encoder/vp9_encoder.h"
13 #include "vp9/encoder/vp9_ethread.h"
14 #include "vp9/encoder/vp9_firstpass.h"
15 #include "vp9/encoder/vp9_multi_thread.h"
16 #include "vp9/encoder/vp9_temporal_filter.h"
17 #include "vpx_dsp/vpx_dsp_common.h"
18
accumulate_rd_opt(ThreadData * td,ThreadData * td_t)19 static void accumulate_rd_opt(ThreadData *td, ThreadData *td_t) {
20 int i, j, k, l, m, n;
21
22 for (i = 0; i < REFERENCE_MODES; i++)
23 td->rd_counts.comp_pred_diff[i] += td_t->rd_counts.comp_pred_diff[i];
24
25 for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; i++)
26 td->rd_counts.filter_diff[i] += td_t->rd_counts.filter_diff[i];
27
28 for (i = 0; i < TX_SIZES; i++)
29 for (j = 0; j < PLANE_TYPES; j++)
30 for (k = 0; k < REF_TYPES; k++)
31 for (l = 0; l < COEF_BANDS; l++)
32 for (m = 0; m < COEFF_CONTEXTS; m++)
33 for (n = 0; n < ENTROPY_TOKENS; n++)
34 td->rd_counts.coef_counts[i][j][k][l][m][n] +=
35 td_t->rd_counts.coef_counts[i][j][k][l][m][n];
36 }
37
enc_worker_hook(void * arg1,void * unused)38 static int enc_worker_hook(void *arg1, void *unused) {
39 EncWorkerData *const thread_data = (EncWorkerData *)arg1;
40 VP9_COMP *const cpi = thread_data->cpi;
41 const VP9_COMMON *const cm = &cpi->common;
42 const int tile_cols = 1 << cm->log2_tile_cols;
43 const int tile_rows = 1 << cm->log2_tile_rows;
44 int t;
45
46 (void)unused;
47
48 for (t = thread_data->start; t < tile_rows * tile_cols;
49 t += cpi->num_workers) {
50 int tile_row = t / tile_cols;
51 int tile_col = t % tile_cols;
52
53 vp9_encode_tile(cpi, thread_data->td, tile_row, tile_col);
54 }
55
56 return 0;
57 }
58
get_max_tile_cols(VP9_COMP * cpi)59 static int get_max_tile_cols(VP9_COMP *cpi) {
60 const int aligned_width = ALIGN_POWER_OF_TWO(cpi->oxcf.width, MI_SIZE_LOG2);
61 int mi_cols = aligned_width >> MI_SIZE_LOG2;
62 int min_log2_tile_cols, max_log2_tile_cols;
63 int log2_tile_cols;
64
65 vp9_get_tile_n_bits(mi_cols, &min_log2_tile_cols, &max_log2_tile_cols);
66 log2_tile_cols =
67 clamp(cpi->oxcf.tile_columns, min_log2_tile_cols, max_log2_tile_cols);
68 if (cpi->oxcf.target_level == LEVEL_AUTO) {
69 const int level_tile_cols =
70 log_tile_cols_from_picsize_level(cpi->common.width, cpi->common.height);
71 if (log2_tile_cols > level_tile_cols) {
72 log2_tile_cols = VPXMAX(level_tile_cols, min_log2_tile_cols);
73 }
74 }
75 return (1 << log2_tile_cols);
76 }
77
create_enc_workers(VP9_COMP * cpi,int num_workers)78 static void create_enc_workers(VP9_COMP *cpi, int num_workers) {
79 VP9_COMMON *const cm = &cpi->common;
80 const VPxWorkerInterface *const winterface = vpx_get_worker_interface();
81 int i;
82
83 // Only run once to create threads and allocate thread data.
84 if (cpi->num_workers == 0) {
85 int allocated_workers = num_workers;
86
87 // While using SVC, we need to allocate threads according to the highest
88 // resolution. When row based multithreading is enabled, it is OK to
89 // allocate more threads than the number of max tile columns.
90 if (cpi->use_svc && !cpi->row_mt) {
91 int max_tile_cols = get_max_tile_cols(cpi);
92 allocated_workers = VPXMIN(cpi->oxcf.max_threads, max_tile_cols);
93 }
94
95 CHECK_MEM_ERROR(cm, cpi->workers,
96 vpx_malloc(allocated_workers * sizeof(*cpi->workers)));
97
98 CHECK_MEM_ERROR(cm, cpi->tile_thr_data,
99 vpx_calloc(allocated_workers, sizeof(*cpi->tile_thr_data)));
100
101 for (i = 0; i < allocated_workers; i++) {
102 VPxWorker *const worker = &cpi->workers[i];
103 EncWorkerData *thread_data = &cpi->tile_thr_data[i];
104
105 ++cpi->num_workers;
106 winterface->init(worker);
107
108 if (i < allocated_workers - 1) {
109 thread_data->cpi = cpi;
110
111 // Allocate thread data.
112 CHECK_MEM_ERROR(cm, thread_data->td,
113 vpx_memalign(32, sizeof(*thread_data->td)));
114 vp9_zero(*thread_data->td);
115
116 // Set up pc_tree.
117 thread_data->td->leaf_tree = NULL;
118 thread_data->td->pc_tree = NULL;
119 vp9_setup_pc_tree(cm, thread_data->td);
120
121 // Allocate frame counters in thread data.
122 CHECK_MEM_ERROR(cm, thread_data->td->counts,
123 vpx_calloc(1, sizeof(*thread_data->td->counts)));
124
125 // Create threads
126 if (!winterface->reset(worker))
127 vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
128 "Tile encoder thread creation failed");
129 } else {
130 // Main thread acts as a worker and uses the thread data in cpi.
131 thread_data->cpi = cpi;
132 thread_data->td = &cpi->td;
133 }
134 winterface->sync(worker);
135 }
136 }
137 }
138
launch_enc_workers(VP9_COMP * cpi,VPxWorkerHook hook,void * data2,int num_workers)139 static void launch_enc_workers(VP9_COMP *cpi, VPxWorkerHook hook, void *data2,
140 int num_workers) {
141 const VPxWorkerInterface *const winterface = vpx_get_worker_interface();
142 int i;
143
144 for (i = 0; i < num_workers; i++) {
145 VPxWorker *const worker = &cpi->workers[i];
146 worker->hook = hook;
147 worker->data1 = &cpi->tile_thr_data[i];
148 worker->data2 = data2;
149 }
150
151 // Encode a frame
152 for (i = 0; i < num_workers; i++) {
153 VPxWorker *const worker = &cpi->workers[i];
154 EncWorkerData *const thread_data = (EncWorkerData *)worker->data1;
155
156 // Set the starting tile for each thread.
157 thread_data->start = i;
158
159 if (i == cpi->num_workers - 1)
160 winterface->execute(worker);
161 else
162 winterface->launch(worker);
163 }
164
165 // Encoding ends.
166 for (i = 0; i < num_workers; i++) {
167 VPxWorker *const worker = &cpi->workers[i];
168 winterface->sync(worker);
169 }
170 }
171
vp9_encode_tiles_mt(VP9_COMP * cpi)172 void vp9_encode_tiles_mt(VP9_COMP *cpi) {
173 VP9_COMMON *const cm = &cpi->common;
174 const int tile_cols = 1 << cm->log2_tile_cols;
175 const int num_workers = VPXMIN(cpi->oxcf.max_threads, tile_cols);
176 int i;
177
178 vp9_init_tile_data(cpi);
179
180 create_enc_workers(cpi, num_workers);
181
182 for (i = 0; i < num_workers; i++) {
183 EncWorkerData *thread_data;
184 thread_data = &cpi->tile_thr_data[i];
185
186 // Before encoding a frame, copy the thread data from cpi.
187 if (thread_data->td != &cpi->td) {
188 thread_data->td->mb = cpi->td.mb;
189 thread_data->td->rd_counts = cpi->td.rd_counts;
190 }
191 if (thread_data->td->counts != &cpi->common.counts) {
192 memcpy(thread_data->td->counts, &cpi->common.counts,
193 sizeof(cpi->common.counts));
194 }
195
196 // Handle use_nonrd_pick_mode case.
197 if (cpi->sf.use_nonrd_pick_mode) {
198 MACROBLOCK *const x = &thread_data->td->mb;
199 MACROBLOCKD *const xd = &x->e_mbd;
200 struct macroblock_plane *const p = x->plane;
201 struct macroblockd_plane *const pd = xd->plane;
202 PICK_MODE_CONTEXT *ctx = &thread_data->td->pc_root->none;
203 int j;
204
205 for (j = 0; j < MAX_MB_PLANE; ++j) {
206 p[j].coeff = ctx->coeff_pbuf[j][0];
207 p[j].qcoeff = ctx->qcoeff_pbuf[j][0];
208 pd[j].dqcoeff = ctx->dqcoeff_pbuf[j][0];
209 p[j].eobs = ctx->eobs_pbuf[j][0];
210 }
211 }
212 }
213
214 launch_enc_workers(cpi, enc_worker_hook, NULL, num_workers);
215
216 for (i = 0; i < num_workers; i++) {
217 VPxWorker *const worker = &cpi->workers[i];
218 EncWorkerData *const thread_data = (EncWorkerData *)worker->data1;
219
220 // Accumulate counters.
221 if (i < cpi->num_workers - 1) {
222 vp9_accumulate_frame_counts(&cm->counts, thread_data->td->counts, 0);
223 accumulate_rd_opt(&cpi->td, thread_data->td);
224 }
225 }
226 }
227
228 #if !CONFIG_REALTIME_ONLY
accumulate_fp_tile_stat(TileDataEnc * tile_data,TileDataEnc * tile_data_t)229 static void accumulate_fp_tile_stat(TileDataEnc *tile_data,
230 TileDataEnc *tile_data_t) {
231 tile_data->fp_data.intra_factor += tile_data_t->fp_data.intra_factor;
232 tile_data->fp_data.brightness_factor +=
233 tile_data_t->fp_data.brightness_factor;
234 tile_data->fp_data.coded_error += tile_data_t->fp_data.coded_error;
235 tile_data->fp_data.sr_coded_error += tile_data_t->fp_data.sr_coded_error;
236 tile_data->fp_data.frame_noise_energy +=
237 tile_data_t->fp_data.frame_noise_energy;
238 tile_data->fp_data.intra_error += tile_data_t->fp_data.intra_error;
239 tile_data->fp_data.intercount += tile_data_t->fp_data.intercount;
240 tile_data->fp_data.second_ref_count += tile_data_t->fp_data.second_ref_count;
241 tile_data->fp_data.neutral_count += tile_data_t->fp_data.neutral_count;
242 tile_data->fp_data.intra_count_low += tile_data_t->fp_data.intra_count_low;
243 tile_data->fp_data.intra_count_high += tile_data_t->fp_data.intra_count_high;
244 tile_data->fp_data.intra_skip_count += tile_data_t->fp_data.intra_skip_count;
245 tile_data->fp_data.mvcount += tile_data_t->fp_data.mvcount;
246 tile_data->fp_data.sum_mvr += tile_data_t->fp_data.sum_mvr;
247 tile_data->fp_data.sum_mvr_abs += tile_data_t->fp_data.sum_mvr_abs;
248 tile_data->fp_data.sum_mvc += tile_data_t->fp_data.sum_mvc;
249 tile_data->fp_data.sum_mvc_abs += tile_data_t->fp_data.sum_mvc_abs;
250 tile_data->fp_data.sum_mvrs += tile_data_t->fp_data.sum_mvrs;
251 tile_data->fp_data.sum_mvcs += tile_data_t->fp_data.sum_mvcs;
252 tile_data->fp_data.sum_in_vectors += tile_data_t->fp_data.sum_in_vectors;
253 tile_data->fp_data.intra_smooth_count +=
254 tile_data_t->fp_data.intra_smooth_count;
255 tile_data->fp_data.image_data_start_row =
256 VPXMIN(tile_data->fp_data.image_data_start_row,
257 tile_data_t->fp_data.image_data_start_row) == INVALID_ROW
258 ? VPXMAX(tile_data->fp_data.image_data_start_row,
259 tile_data_t->fp_data.image_data_start_row)
260 : VPXMIN(tile_data->fp_data.image_data_start_row,
261 tile_data_t->fp_data.image_data_start_row);
262 }
263 #endif // !CONFIG_REALTIME_ONLY
264
265 // Allocate memory for row synchronization
vp9_row_mt_sync_mem_alloc(VP9RowMTSync * row_mt_sync,VP9_COMMON * cm,int rows)266 void vp9_row_mt_sync_mem_alloc(VP9RowMTSync *row_mt_sync, VP9_COMMON *cm,
267 int rows) {
268 row_mt_sync->rows = rows;
269 #if CONFIG_MULTITHREAD
270 {
271 int i;
272
273 CHECK_MEM_ERROR(cm, row_mt_sync->mutex_,
274 vpx_malloc(sizeof(*row_mt_sync->mutex_) * rows));
275 if (row_mt_sync->mutex_) {
276 for (i = 0; i < rows; ++i) {
277 pthread_mutex_init(&row_mt_sync->mutex_[i], NULL);
278 }
279 }
280
281 CHECK_MEM_ERROR(cm, row_mt_sync->cond_,
282 vpx_malloc(sizeof(*row_mt_sync->cond_) * rows));
283 if (row_mt_sync->cond_) {
284 for (i = 0; i < rows; ++i) {
285 pthread_cond_init(&row_mt_sync->cond_[i], NULL);
286 }
287 }
288 }
289 #endif // CONFIG_MULTITHREAD
290
291 CHECK_MEM_ERROR(cm, row_mt_sync->cur_col,
292 vpx_malloc(sizeof(*row_mt_sync->cur_col) * rows));
293
294 // Set up nsync.
295 row_mt_sync->sync_range = 1;
296 }
297
298 // Deallocate row based multi-threading synchronization related mutex and data
vp9_row_mt_sync_mem_dealloc(VP9RowMTSync * row_mt_sync)299 void vp9_row_mt_sync_mem_dealloc(VP9RowMTSync *row_mt_sync) {
300 if (row_mt_sync != NULL) {
301 #if CONFIG_MULTITHREAD
302 int i;
303
304 if (row_mt_sync->mutex_ != NULL) {
305 for (i = 0; i < row_mt_sync->rows; ++i) {
306 pthread_mutex_destroy(&row_mt_sync->mutex_[i]);
307 }
308 vpx_free(row_mt_sync->mutex_);
309 }
310 if (row_mt_sync->cond_ != NULL) {
311 for (i = 0; i < row_mt_sync->rows; ++i) {
312 pthread_cond_destroy(&row_mt_sync->cond_[i]);
313 }
314 vpx_free(row_mt_sync->cond_);
315 }
316 #endif // CONFIG_MULTITHREAD
317 vpx_free(row_mt_sync->cur_col);
318 // clear the structure as the source of this call may be dynamic change
319 // in tiles in which case this call will be followed by an _alloc()
320 // which may fail.
321 vp9_zero(*row_mt_sync);
322 }
323 }
324
vp9_row_mt_sync_read(VP9RowMTSync * const row_mt_sync,int r,int c)325 void vp9_row_mt_sync_read(VP9RowMTSync *const row_mt_sync, int r, int c) {
326 #if CONFIG_MULTITHREAD
327 const int nsync = row_mt_sync->sync_range;
328
329 if (r && !(c & (nsync - 1))) {
330 pthread_mutex_t *const mutex = &row_mt_sync->mutex_[r - 1];
331 pthread_mutex_lock(mutex);
332
333 while (c > row_mt_sync->cur_col[r - 1] - nsync + 1) {
334 pthread_cond_wait(&row_mt_sync->cond_[r - 1], mutex);
335 }
336 pthread_mutex_unlock(mutex);
337 }
338 #else
339 (void)row_mt_sync;
340 (void)r;
341 (void)c;
342 #endif // CONFIG_MULTITHREAD
343 }
344
vp9_row_mt_sync_read_dummy(VP9RowMTSync * const row_mt_sync,int r,int c)345 void vp9_row_mt_sync_read_dummy(VP9RowMTSync *const row_mt_sync, int r, int c) {
346 (void)row_mt_sync;
347 (void)r;
348 (void)c;
349 return;
350 }
351
vp9_row_mt_sync_write(VP9RowMTSync * const row_mt_sync,int r,int c,const int cols)352 void vp9_row_mt_sync_write(VP9RowMTSync *const row_mt_sync, int r, int c,
353 const int cols) {
354 #if CONFIG_MULTITHREAD
355 const int nsync = row_mt_sync->sync_range;
356 int cur;
357 // Only signal when there are enough encoded blocks for next row to run.
358 int sig = 1;
359
360 if (c < cols - 1) {
361 cur = c;
362 if (c % nsync != nsync - 1) sig = 0;
363 } else {
364 cur = cols + nsync;
365 }
366
367 if (sig) {
368 pthread_mutex_lock(&row_mt_sync->mutex_[r]);
369
370 row_mt_sync->cur_col[r] = cur;
371
372 pthread_cond_signal(&row_mt_sync->cond_[r]);
373 pthread_mutex_unlock(&row_mt_sync->mutex_[r]);
374 }
375 #else
376 (void)row_mt_sync;
377 (void)r;
378 (void)c;
379 (void)cols;
380 #endif // CONFIG_MULTITHREAD
381 }
382
vp9_row_mt_sync_write_dummy(VP9RowMTSync * const row_mt_sync,int r,int c,const int cols)383 void vp9_row_mt_sync_write_dummy(VP9RowMTSync *const row_mt_sync, int r, int c,
384 const int cols) {
385 (void)row_mt_sync;
386 (void)r;
387 (void)c;
388 (void)cols;
389 return;
390 }
391
392 #if !CONFIG_REALTIME_ONLY
first_pass_worker_hook(EncWorkerData * const thread_data,MultiThreadHandle * multi_thread_ctxt)393 static int first_pass_worker_hook(EncWorkerData *const thread_data,
394 MultiThreadHandle *multi_thread_ctxt) {
395 VP9_COMP *const cpi = thread_data->cpi;
396 const VP9_COMMON *const cm = &cpi->common;
397 const int tile_cols = 1 << cm->log2_tile_cols;
398 int tile_row, tile_col;
399 TileDataEnc *this_tile;
400 int end_of_frame;
401 int thread_id = thread_data->thread_id;
402 int cur_tile_id = multi_thread_ctxt->thread_id_to_tile_id[thread_id];
403 JobNode *proc_job = NULL;
404 FIRSTPASS_DATA fp_acc_data;
405 MV zero_mv = { 0, 0 };
406 MV best_ref_mv;
407 int mb_row;
408
409 end_of_frame = 0;
410 while (0 == end_of_frame) {
411 // Get the next job in the queue
412 proc_job =
413 (JobNode *)vp9_enc_grp_get_next_job(multi_thread_ctxt, cur_tile_id);
414 if (NULL == proc_job) {
415 // Query for the status of other tiles
416 end_of_frame = vp9_get_tiles_proc_status(
417 multi_thread_ctxt, thread_data->tile_completion_status, &cur_tile_id,
418 tile_cols);
419 } else {
420 tile_col = proc_job->tile_col_id;
421 tile_row = proc_job->tile_row_id;
422
423 this_tile = &cpi->tile_data[tile_row * tile_cols + tile_col];
424 mb_row = proc_job->vert_unit_row_num;
425
426 best_ref_mv = zero_mv;
427 vp9_zero(fp_acc_data);
428 fp_acc_data.image_data_start_row = INVALID_ROW;
429 vp9_first_pass_encode_tile_mb_row(cpi, thread_data->td, &fp_acc_data,
430 this_tile, &best_ref_mv, mb_row);
431 }
432 }
433 return 0;
434 }
435
vp9_encode_fp_row_mt(VP9_COMP * cpi)436 void vp9_encode_fp_row_mt(VP9_COMP *cpi) {
437 VP9_COMMON *const cm = &cpi->common;
438 const int tile_cols = 1 << cm->log2_tile_cols;
439 const int tile_rows = 1 << cm->log2_tile_rows;
440 MultiThreadHandle *multi_thread_ctxt = &cpi->multi_thread_ctxt;
441 TileDataEnc *first_tile_col;
442 int num_workers = VPXMAX(cpi->oxcf.max_threads, 1);
443 int i;
444
445 if (multi_thread_ctxt->allocated_tile_cols < tile_cols ||
446 multi_thread_ctxt->allocated_tile_rows < tile_rows ||
447 multi_thread_ctxt->allocated_vert_unit_rows < cm->mb_rows) {
448 vp9_row_mt_mem_dealloc(cpi);
449 vp9_init_tile_data(cpi);
450 vp9_row_mt_mem_alloc(cpi);
451 } else {
452 vp9_init_tile_data(cpi);
453 }
454
455 create_enc_workers(cpi, num_workers);
456
457 vp9_assign_tile_to_thread(multi_thread_ctxt, tile_cols, cpi->num_workers);
458
459 vp9_prepare_job_queue(cpi, FIRST_PASS_JOB);
460
461 vp9_multi_thread_tile_init(cpi);
462
463 for (i = 0; i < num_workers; i++) {
464 EncWorkerData *thread_data;
465 thread_data = &cpi->tile_thr_data[i];
466
467 // Before encoding a frame, copy the thread data from cpi.
468 if (thread_data->td != &cpi->td) {
469 thread_data->td->mb = cpi->td.mb;
470 }
471 }
472
473 launch_enc_workers(cpi, (VPxWorkerHook)first_pass_worker_hook,
474 multi_thread_ctxt, num_workers);
475
476 first_tile_col = &cpi->tile_data[0];
477 for (i = 1; i < tile_cols; i++) {
478 TileDataEnc *this_tile = &cpi->tile_data[i];
479 accumulate_fp_tile_stat(first_tile_col, this_tile);
480 }
481 }
482
temporal_filter_worker_hook(EncWorkerData * const thread_data,MultiThreadHandle * multi_thread_ctxt)483 static int temporal_filter_worker_hook(EncWorkerData *const thread_data,
484 MultiThreadHandle *multi_thread_ctxt) {
485 VP9_COMP *const cpi = thread_data->cpi;
486 const VP9_COMMON *const cm = &cpi->common;
487 const int tile_cols = 1 << cm->log2_tile_cols;
488 int tile_row, tile_col;
489 int mb_col_start, mb_col_end;
490 TileDataEnc *this_tile;
491 int end_of_frame;
492 int thread_id = thread_data->thread_id;
493 int cur_tile_id = multi_thread_ctxt->thread_id_to_tile_id[thread_id];
494 JobNode *proc_job = NULL;
495 int mb_row;
496
497 end_of_frame = 0;
498 while (0 == end_of_frame) {
499 // Get the next job in the queue
500 proc_job =
501 (JobNode *)vp9_enc_grp_get_next_job(multi_thread_ctxt, cur_tile_id);
502 if (NULL == proc_job) {
503 // Query for the status of other tiles
504 end_of_frame = vp9_get_tiles_proc_status(
505 multi_thread_ctxt, thread_data->tile_completion_status, &cur_tile_id,
506 tile_cols);
507 } else {
508 tile_col = proc_job->tile_col_id;
509 tile_row = proc_job->tile_row_id;
510 this_tile = &cpi->tile_data[tile_row * tile_cols + tile_col];
511 mb_col_start = (this_tile->tile_info.mi_col_start) >> 1;
512 mb_col_end = (this_tile->tile_info.mi_col_end + 1) >> 1;
513 mb_row = proc_job->vert_unit_row_num;
514
515 vp9_temporal_filter_iterate_row_c(cpi, thread_data->td, mb_row,
516 mb_col_start, mb_col_end);
517 }
518 }
519 return 0;
520 }
521
vp9_temporal_filter_row_mt(VP9_COMP * cpi)522 void vp9_temporal_filter_row_mt(VP9_COMP *cpi) {
523 VP9_COMMON *const cm = &cpi->common;
524 const int tile_cols = 1 << cm->log2_tile_cols;
525 const int tile_rows = 1 << cm->log2_tile_rows;
526 MultiThreadHandle *multi_thread_ctxt = &cpi->multi_thread_ctxt;
527 int num_workers = cpi->num_workers ? cpi->num_workers : 1;
528 int i;
529
530 if (multi_thread_ctxt->allocated_tile_cols < tile_cols ||
531 multi_thread_ctxt->allocated_tile_rows < tile_rows ||
532 multi_thread_ctxt->allocated_vert_unit_rows < cm->mb_rows) {
533 vp9_row_mt_mem_dealloc(cpi);
534 vp9_init_tile_data(cpi);
535 vp9_row_mt_mem_alloc(cpi);
536 } else {
537 vp9_init_tile_data(cpi);
538 }
539
540 create_enc_workers(cpi, num_workers);
541
542 vp9_assign_tile_to_thread(multi_thread_ctxt, tile_cols, cpi->num_workers);
543
544 vp9_prepare_job_queue(cpi, ARNR_JOB);
545
546 for (i = 0; i < num_workers; i++) {
547 EncWorkerData *thread_data;
548 thread_data = &cpi->tile_thr_data[i];
549
550 // Before encoding a frame, copy the thread data from cpi.
551 if (thread_data->td != &cpi->td) {
552 thread_data->td->mb = cpi->td.mb;
553 }
554 }
555
556 launch_enc_workers(cpi, (VPxWorkerHook)temporal_filter_worker_hook,
557 multi_thread_ctxt, num_workers);
558 }
559 #endif // !CONFIG_REALTIME_ONLY
560
enc_row_mt_worker_hook(EncWorkerData * const thread_data,MultiThreadHandle * multi_thread_ctxt)561 static int enc_row_mt_worker_hook(EncWorkerData *const thread_data,
562 MultiThreadHandle *multi_thread_ctxt) {
563 VP9_COMP *const cpi = thread_data->cpi;
564 const VP9_COMMON *const cm = &cpi->common;
565 const int tile_cols = 1 << cm->log2_tile_cols;
566 int tile_row, tile_col;
567 int end_of_frame;
568 int thread_id = thread_data->thread_id;
569 int cur_tile_id = multi_thread_ctxt->thread_id_to_tile_id[thread_id];
570 JobNode *proc_job = NULL;
571 int mi_row;
572
573 end_of_frame = 0;
574 while (0 == end_of_frame) {
575 // Get the next job in the queue
576 proc_job =
577 (JobNode *)vp9_enc_grp_get_next_job(multi_thread_ctxt, cur_tile_id);
578 if (NULL == proc_job) {
579 // Query for the status of other tiles
580 end_of_frame = vp9_get_tiles_proc_status(
581 multi_thread_ctxt, thread_data->tile_completion_status, &cur_tile_id,
582 tile_cols);
583 } else {
584 tile_col = proc_job->tile_col_id;
585 tile_row = proc_job->tile_row_id;
586 mi_row = proc_job->vert_unit_row_num * MI_BLOCK_SIZE;
587
588 vp9_encode_sb_row(cpi, thread_data->td, tile_row, tile_col, mi_row);
589 }
590 }
591 return 0;
592 }
593
vp9_encode_tiles_row_mt(VP9_COMP * cpi)594 void vp9_encode_tiles_row_mt(VP9_COMP *cpi) {
595 VP9_COMMON *const cm = &cpi->common;
596 const int tile_cols = 1 << cm->log2_tile_cols;
597 const int tile_rows = 1 << cm->log2_tile_rows;
598 MultiThreadHandle *multi_thread_ctxt = &cpi->multi_thread_ctxt;
599 int num_workers = VPXMAX(cpi->oxcf.max_threads, 1);
600 int i;
601
602 if (multi_thread_ctxt->allocated_tile_cols < tile_cols ||
603 multi_thread_ctxt->allocated_tile_rows < tile_rows ||
604 multi_thread_ctxt->allocated_vert_unit_rows < cm->mb_rows) {
605 vp9_row_mt_mem_dealloc(cpi);
606 vp9_init_tile_data(cpi);
607 vp9_row_mt_mem_alloc(cpi);
608 } else {
609 vp9_init_tile_data(cpi);
610 }
611
612 create_enc_workers(cpi, num_workers);
613
614 vp9_assign_tile_to_thread(multi_thread_ctxt, tile_cols, cpi->num_workers);
615
616 vp9_prepare_job_queue(cpi, ENCODE_JOB);
617
618 vp9_multi_thread_tile_init(cpi);
619
620 for (i = 0; i < num_workers; i++) {
621 EncWorkerData *thread_data;
622 thread_data = &cpi->tile_thr_data[i];
623 // Before encoding a frame, copy the thread data from cpi.
624 if (thread_data->td != &cpi->td) {
625 thread_data->td->mb = cpi->td.mb;
626 thread_data->td->rd_counts = cpi->td.rd_counts;
627 }
628 if (thread_data->td->counts != &cpi->common.counts) {
629 memcpy(thread_data->td->counts, &cpi->common.counts,
630 sizeof(cpi->common.counts));
631 }
632
633 // Handle use_nonrd_pick_mode case.
634 if (cpi->sf.use_nonrd_pick_mode) {
635 MACROBLOCK *const x = &thread_data->td->mb;
636 MACROBLOCKD *const xd = &x->e_mbd;
637 struct macroblock_plane *const p = x->plane;
638 struct macroblockd_plane *const pd = xd->plane;
639 PICK_MODE_CONTEXT *ctx = &thread_data->td->pc_root->none;
640 int j;
641
642 for (j = 0; j < MAX_MB_PLANE; ++j) {
643 p[j].coeff = ctx->coeff_pbuf[j][0];
644 p[j].qcoeff = ctx->qcoeff_pbuf[j][0];
645 pd[j].dqcoeff = ctx->dqcoeff_pbuf[j][0];
646 p[j].eobs = ctx->eobs_pbuf[j][0];
647 }
648 }
649 }
650
651 launch_enc_workers(cpi, (VPxWorkerHook)enc_row_mt_worker_hook,
652 multi_thread_ctxt, num_workers);
653
654 for (i = 0; i < num_workers; i++) {
655 VPxWorker *const worker = &cpi->workers[i];
656 EncWorkerData *const thread_data = (EncWorkerData *)worker->data1;
657
658 // Accumulate counters.
659 if (i < cpi->num_workers - 1) {
660 vp9_accumulate_frame_counts(&cm->counts, thread_data->td->counts, 0);
661 accumulate_rd_opt(&cpi->td, thread_data->td);
662 }
663 }
664 }
665