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"
14 #include "vpx_dsp/vpx_dsp_common.h"
16 static void accumulate_rd_opt(ThreadData *td, ThreadData *td_t) {
19 for (i = 0; i < REFERENCE_MODES; i++)
20 td->rd_counts.comp_pred_diff[i] += td_t->rd_counts.comp_pred_diff[i];
22 for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; i++)
23 td->rd_counts.filter_diff[i] += td_t->rd_counts.filter_diff[i];
25 for (i = 0; i < TX_SIZES; i++)
26 for (j = 0; j < PLANE_TYPES; j++)
27 for (k = 0; k < REF_TYPES; k++)
28 for (l = 0; l < COEF_BANDS; l++)
29 for (m = 0; m < COEFF_CONTEXTS; m++)
30 for (n = 0; n < ENTROPY_TOKENS; n++)
31 td->rd_counts.coef_counts[i][j][k][l][m][n] +=
32 td_t->rd_counts.coef_counts[i][j][k][l][m][n];
35 // Counts of all motion searches and exhuastive mesh searches.
36 td->rd_counts.m_search_count += td_t->rd_counts.m_search_count;
37 td->rd_counts.ex_search_count += td_t->rd_counts.ex_search_count;
40 static int enc_worker_hook(EncWorkerData *const thread_data, void *unused) {
41 VP10_COMP *const cpi = thread_data->cpi;
42 const VP10_COMMON *const cm = &cpi->common;
43 const int tile_cols = 1 << cm->log2_tile_cols;
44 const int tile_rows = 1 << cm->log2_tile_rows;
49 for (t = thread_data->start; t < tile_rows * tile_cols;
50 t += cpi->num_workers) {
51 int tile_row = t / tile_cols;
52 int tile_col = t % tile_cols;
54 vp10_encode_tile(cpi, thread_data->td, tile_row, tile_col);
60 void vp10_encode_tiles_mt(VP10_COMP *cpi) {
61 VP10_COMMON *const cm = &cpi->common;
62 const int tile_cols = 1 << cm->log2_tile_cols;
63 const VPxWorkerInterface *const winterface = vpx_get_worker_interface();
64 const int num_workers = VPXMIN(cpi->oxcf.max_threads, tile_cols);
67 vp10_init_tile_data(cpi);
69 // Only run once to create threads and allocate thread data.
70 if (cpi->num_workers == 0) {
71 int allocated_workers = num_workers;
73 CHECK_MEM_ERROR(cm, cpi->workers,
74 vpx_malloc(allocated_workers * sizeof(*cpi->workers)));
76 CHECK_MEM_ERROR(cm, cpi->tile_thr_data,
77 vpx_calloc(allocated_workers,
78 sizeof(*cpi->tile_thr_data)));
80 for (i = 0; i < allocated_workers; i++) {
81 VPxWorker *const worker = &cpi->workers[i];
82 EncWorkerData *thread_data = &cpi->tile_thr_data[i];
85 winterface->init(worker);
87 if (i < allocated_workers - 1) {
88 thread_data->cpi = cpi;
90 // Allocate thread data.
91 CHECK_MEM_ERROR(cm, thread_data->td,
92 vpx_memalign(32, sizeof(*thread_data->td)));
93 vp10_zero(*thread_data->td);
96 thread_data->td->leaf_tree = NULL;
97 thread_data->td->pc_tree = NULL;
98 vp10_setup_pc_tree(cm, thread_data->td);
100 // Allocate frame counters in thread data.
101 CHECK_MEM_ERROR(cm, thread_data->td->counts,
102 vpx_calloc(1, sizeof(*thread_data->td->counts)));
105 if (!winterface->reset(worker))
106 vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
107 "Tile encoder thread creation failed");
109 // Main thread acts as a worker and uses the thread data in cpi.
110 thread_data->cpi = cpi;
111 thread_data->td = &cpi->td;
114 winterface->sync(worker);
118 for (i = 0; i < num_workers; i++) {
119 VPxWorker *const worker = &cpi->workers[i];
120 EncWorkerData *thread_data;
122 worker->hook = (VPxWorkerHook)enc_worker_hook;
123 worker->data1 = &cpi->tile_thr_data[i];
124 worker->data2 = NULL;
125 thread_data = (EncWorkerData*)worker->data1;
127 // Before encoding a frame, copy the thread data from cpi.
128 if (thread_data->td != &cpi->td) {
129 thread_data->td->mb = cpi->td.mb;
130 thread_data->td->rd_counts = cpi->td.rd_counts;
132 if (thread_data->td->counts != &cpi->common.counts) {
133 memcpy(thread_data->td->counts, &cpi->common.counts,
134 sizeof(cpi->common.counts));
139 for (i = 0; i < num_workers; i++) {
140 VPxWorker *const worker = &cpi->workers[i];
141 EncWorkerData *const thread_data = (EncWorkerData*)worker->data1;
143 // Set the starting tile for each thread.
144 thread_data->start = i;
146 if (i == cpi->num_workers - 1)
147 winterface->execute(worker);
149 winterface->launch(worker);
153 for (i = 0; i < num_workers; i++) {
154 VPxWorker *const worker = &cpi->workers[i];
155 winterface->sync(worker);
158 for (i = 0; i < num_workers; i++) {
159 VPxWorker *const worker = &cpi->workers[i];
160 EncWorkerData *const thread_data = (EncWorkerData*)worker->data1;
162 // Accumulate counters.
163 if (i < cpi->num_workers - 1) {
164 vp10_accumulate_frame_counts(cm, thread_data->td->counts, 0);
165 accumulate_rd_opt(&cpi->td, thread_data->td);