2 * Copyright (c) 2014 The WebM project authors. All Rights Reserved.
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.
11 #include "vp10/encoder/encodeframe.h"
12 #include "vp10/encoder/encoder.h"
13 #include "vp10/encoder/ethread.h"
15 static void accumulate_rd_opt(ThreadData *td, ThreadData *td_t) {
18 for (i = 0; i < REFERENCE_MODES; i++)
19 td->rd_counts.comp_pred_diff[i] += td_t->rd_counts.comp_pred_diff[i];
21 for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; i++)
22 td->rd_counts.filter_diff[i] += td_t->rd_counts.filter_diff[i];
24 for (i = 0; i < TX_SIZES; i++)
25 for (j = 0; j < PLANE_TYPES; j++)
26 for (k = 0; k < REF_TYPES; k++)
27 for (l = 0; l < COEF_BANDS; l++)
28 for (m = 0; m < COEFF_CONTEXTS; m++)
29 for (n = 0; n < ENTROPY_TOKENS; n++)
30 td->rd_counts.coef_counts[i][j][k][l][m][n] +=
31 td_t->rd_counts.coef_counts[i][j][k][l][m][n];
34 static int enc_worker_hook(EncWorkerData *const thread_data, void *unused) {
35 VP10_COMP *const cpi = thread_data->cpi;
36 const VP10_COMMON *const cm = &cpi->common;
37 const int tile_cols = 1 << cm->log2_tile_cols;
38 const int tile_rows = 1 << cm->log2_tile_rows;
43 for (t = thread_data->start; t < tile_rows * tile_cols;
44 t += cpi->num_workers) {
45 int tile_row = t / tile_cols;
46 int tile_col = t % tile_cols;
48 vp10_encode_tile(cpi, thread_data->td, tile_row, tile_col);
54 static int get_max_tile_cols(VP10_COMP *cpi) {
55 const int aligned_width = ALIGN_POWER_OF_TWO(cpi->oxcf.width, MI_SIZE_LOG2);
56 int mi_cols = aligned_width >> MI_SIZE_LOG2;
57 int min_log2_tile_cols, max_log2_tile_cols;
60 vp10_get_tile_n_bits(mi_cols, &min_log2_tile_cols, &max_log2_tile_cols);
61 log2_tile_cols = clamp(cpi->oxcf.tile_columns,
62 min_log2_tile_cols, max_log2_tile_cols);
63 return (1 << log2_tile_cols);
66 void vp10_encode_tiles_mt(VP10_COMP *cpi) {
67 VP10_COMMON *const cm = &cpi->common;
68 const int tile_cols = 1 << cm->log2_tile_cols;
69 const VPxWorkerInterface *const winterface = vpx_get_worker_interface();
70 const int num_workers = MIN(cpi->oxcf.max_threads, tile_cols);
73 vp10_init_tile_data(cpi);
75 // Only run once to create threads and allocate thread data.
76 if (cpi->num_workers == 0) {
77 int allocated_workers = num_workers;
79 // While using SVC, we need to allocate threads according to the highest
82 int max_tile_cols = get_max_tile_cols(cpi);
83 allocated_workers = MIN(cpi->oxcf.max_threads, max_tile_cols);
86 CHECK_MEM_ERROR(cm, cpi->workers,
87 vpx_malloc(allocated_workers * sizeof(*cpi->workers)));
89 CHECK_MEM_ERROR(cm, cpi->tile_thr_data,
90 vpx_calloc(allocated_workers,
91 sizeof(*cpi->tile_thr_data)));
93 for (i = 0; i < allocated_workers; i++) {
94 VPxWorker *const worker = &cpi->workers[i];
95 EncWorkerData *thread_data = &cpi->tile_thr_data[i];
98 winterface->init(worker);
100 if (i < allocated_workers - 1) {
101 thread_data->cpi = cpi;
103 // Allocate thread data.
104 CHECK_MEM_ERROR(cm, thread_data->td,
105 vpx_memalign(32, sizeof(*thread_data->td)));
106 vp10_zero(*thread_data->td);
109 thread_data->td->leaf_tree = NULL;
110 thread_data->td->pc_tree = NULL;
111 vp10_setup_pc_tree(cm, thread_data->td);
113 // Allocate frame counters in thread data.
114 CHECK_MEM_ERROR(cm, thread_data->td->counts,
115 vpx_calloc(1, sizeof(*thread_data->td->counts)));
118 if (!winterface->reset(worker))
119 vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
120 "Tile encoder thread creation failed");
122 // Main thread acts as a worker and uses the thread data in cpi.
123 thread_data->cpi = cpi;
124 thread_data->td = &cpi->td;
127 winterface->sync(worker);
131 for (i = 0; i < num_workers; i++) {
132 VPxWorker *const worker = &cpi->workers[i];
133 EncWorkerData *thread_data;
135 worker->hook = (VPxWorkerHook)enc_worker_hook;
136 worker->data1 = &cpi->tile_thr_data[i];
137 worker->data2 = NULL;
138 thread_data = (EncWorkerData*)worker->data1;
140 // Before encoding a frame, copy the thread data from cpi.
141 if (thread_data->td != &cpi->td) {
142 thread_data->td->mb = cpi->td.mb;
143 thread_data->td->rd_counts = cpi->td.rd_counts;
145 if (thread_data->td->counts != &cpi->common.counts) {
146 memcpy(thread_data->td->counts, &cpi->common.counts,
147 sizeof(cpi->common.counts));
150 // Handle use_nonrd_pick_mode case.
151 if (cpi->sf.use_nonrd_pick_mode) {
152 MACROBLOCK *const x = &thread_data->td->mb;
153 MACROBLOCKD *const xd = &x->e_mbd;
154 struct macroblock_plane *const p = x->plane;
155 struct macroblockd_plane *const pd = xd->plane;
156 PICK_MODE_CONTEXT *ctx = &thread_data->td->pc_root->none;
159 for (j = 0; j < MAX_MB_PLANE; ++j) {
160 p[j].coeff = ctx->coeff_pbuf[j][0];
161 p[j].qcoeff = ctx->qcoeff_pbuf[j][0];
162 pd[j].dqcoeff = ctx->dqcoeff_pbuf[j][0];
163 p[j].eobs = ctx->eobs_pbuf[j][0];
169 for (i = 0; i < num_workers; i++) {
170 VPxWorker *const worker = &cpi->workers[i];
171 EncWorkerData *const thread_data = (EncWorkerData*)worker->data1;
173 // Set the starting tile for each thread.
174 thread_data->start = i;
176 if (i == cpi->num_workers - 1)
177 winterface->execute(worker);
179 winterface->launch(worker);
183 for (i = 0; i < num_workers; i++) {
184 VPxWorker *const worker = &cpi->workers[i];
185 winterface->sync(worker);
188 for (i = 0; i < num_workers; i++) {
189 VPxWorker *const worker = &cpi->workers[i];
190 EncWorkerData *const thread_data = (EncWorkerData*)worker->data1;
192 // Accumulate counters.
193 if (i < cpi->num_workers - 1) {
194 vp10_accumulate_frame_counts(cm, thread_data->td->counts, 0);
195 accumulate_rd_opt(&cpi->td, thread_data->td);