Line data Source code
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 "vpx_dsp/vpx_dsp_common.h"
15 :
16 0 : static void accumulate_rd_opt(ThreadData *td, ThreadData *td_t) {
17 : int i, j, k, l, m, n;
18 :
19 0 : for (i = 0; i < REFERENCE_MODES; i++)
20 0 : td->rd_counts.comp_pred_diff[i] += td_t->rd_counts.comp_pred_diff[i];
21 :
22 0 : for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; i++)
23 0 : td->rd_counts.filter_diff[i] += td_t->rd_counts.filter_diff[i];
24 :
25 0 : for (i = 0; i < TX_SIZES; i++)
26 0 : for (j = 0; j < PLANE_TYPES; j++)
27 0 : for (k = 0; k < REF_TYPES; k++)
28 0 : for (l = 0; l < COEF_BANDS; l++)
29 0 : for (m = 0; m < COEFF_CONTEXTS; m++)
30 0 : for (n = 0; n < ENTROPY_TOKENS; n++)
31 0 : td->rd_counts.coef_counts[i][j][k][l][m][n] +=
32 0 : td_t->rd_counts.coef_counts[i][j][k][l][m][n];
33 0 : }
34 :
35 0 : static int enc_worker_hook(EncWorkerData *const thread_data, void *unused) {
36 0 : VP9_COMP *const cpi = thread_data->cpi;
37 0 : const VP9_COMMON *const cm = &cpi->common;
38 0 : const int tile_cols = 1 << cm->log2_tile_cols;
39 0 : const int tile_rows = 1 << cm->log2_tile_rows;
40 : int t;
41 :
42 : (void)unused;
43 :
44 0 : for (t = thread_data->start; t < tile_rows * tile_cols;
45 0 : t += cpi->num_workers) {
46 0 : int tile_row = t / tile_cols;
47 0 : int tile_col = t % tile_cols;
48 :
49 0 : vp9_encode_tile(cpi, thread_data->td, tile_row, tile_col);
50 : }
51 :
52 0 : return 0;
53 : }
54 :
55 0 : static int get_max_tile_cols(VP9_COMP *cpi) {
56 0 : const int aligned_width = ALIGN_POWER_OF_TWO(cpi->oxcf.width, MI_SIZE_LOG2);
57 0 : int mi_cols = aligned_width >> MI_SIZE_LOG2;
58 : int min_log2_tile_cols, max_log2_tile_cols;
59 : int log2_tile_cols;
60 :
61 0 : vp9_get_tile_n_bits(mi_cols, &min_log2_tile_cols, &max_log2_tile_cols);
62 0 : log2_tile_cols =
63 0 : clamp(cpi->oxcf.tile_columns, min_log2_tile_cols, max_log2_tile_cols);
64 0 : return (1 << log2_tile_cols);
65 : }
66 :
67 0 : void vp9_encode_tiles_mt(VP9_COMP *cpi) {
68 0 : VP9_COMMON *const cm = &cpi->common;
69 0 : const int tile_cols = 1 << cm->log2_tile_cols;
70 0 : const VPxWorkerInterface *const winterface = vpx_get_worker_interface();
71 0 : const int num_workers = VPXMIN(cpi->oxcf.max_threads, tile_cols);
72 : int i;
73 :
74 0 : vp9_init_tile_data(cpi);
75 :
76 : // Only run once to create threads and allocate thread data.
77 0 : if (cpi->num_workers == 0) {
78 0 : int allocated_workers = num_workers;
79 :
80 : // While using SVC, we need to allocate threads according to the highest
81 : // resolution.
82 0 : if (cpi->use_svc) {
83 0 : int max_tile_cols = get_max_tile_cols(cpi);
84 0 : allocated_workers = VPXMIN(cpi->oxcf.max_threads, max_tile_cols);
85 : }
86 :
87 0 : CHECK_MEM_ERROR(cm, cpi->workers,
88 : vpx_malloc(allocated_workers * sizeof(*cpi->workers)));
89 :
90 0 : CHECK_MEM_ERROR(cm, cpi->tile_thr_data,
91 : vpx_calloc(allocated_workers, sizeof(*cpi->tile_thr_data)));
92 :
93 0 : for (i = 0; i < allocated_workers; i++) {
94 0 : VPxWorker *const worker = &cpi->workers[i];
95 0 : EncWorkerData *thread_data = &cpi->tile_thr_data[i];
96 :
97 0 : ++cpi->num_workers;
98 0 : winterface->init(worker);
99 :
100 0 : if (i < allocated_workers - 1) {
101 0 : thread_data->cpi = cpi;
102 :
103 : // Allocate thread data.
104 0 : CHECK_MEM_ERROR(cm, thread_data->td,
105 : vpx_memalign(32, sizeof(*thread_data->td)));
106 0 : vp9_zero(*thread_data->td);
107 :
108 : // Set up pc_tree.
109 0 : thread_data->td->leaf_tree = NULL;
110 0 : thread_data->td->pc_tree = NULL;
111 0 : vp9_setup_pc_tree(cm, thread_data->td);
112 :
113 : // Allocate frame counters in thread data.
114 0 : CHECK_MEM_ERROR(cm, thread_data->td->counts,
115 : vpx_calloc(1, sizeof(*thread_data->td->counts)));
116 :
117 : // Create threads
118 0 : if (!winterface->reset(worker))
119 0 : vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
120 : "Tile encoder thread creation failed");
121 : } else {
122 : // Main thread acts as a worker and uses the thread data in cpi.
123 0 : thread_data->cpi = cpi;
124 0 : thread_data->td = &cpi->td;
125 : }
126 :
127 0 : winterface->sync(worker);
128 : }
129 : }
130 :
131 0 : for (i = 0; i < num_workers; i++) {
132 0 : VPxWorker *const worker = &cpi->workers[i];
133 : EncWorkerData *thread_data;
134 :
135 0 : worker->hook = (VPxWorkerHook)enc_worker_hook;
136 0 : worker->data1 = &cpi->tile_thr_data[i];
137 0 : worker->data2 = NULL;
138 0 : thread_data = (EncWorkerData *)worker->data1;
139 :
140 : // Before encoding a frame, copy the thread data from cpi.
141 0 : if (thread_data->td != &cpi->td) {
142 0 : thread_data->td->mb = cpi->td.mb;
143 0 : thread_data->td->rd_counts = cpi->td.rd_counts;
144 : }
145 0 : if (thread_data->td->counts != &cpi->common.counts) {
146 0 : memcpy(thread_data->td->counts, &cpi->common.counts,
147 : sizeof(cpi->common.counts));
148 : }
149 :
150 : // Handle use_nonrd_pick_mode case.
151 0 : if (cpi->sf.use_nonrd_pick_mode) {
152 0 : MACROBLOCK *const x = &thread_data->td->mb;
153 0 : MACROBLOCKD *const xd = &x->e_mbd;
154 0 : struct macroblock_plane *const p = x->plane;
155 0 : struct macroblockd_plane *const pd = xd->plane;
156 0 : PICK_MODE_CONTEXT *ctx = &thread_data->td->pc_root->none;
157 : int j;
158 :
159 0 : for (j = 0; j < MAX_MB_PLANE; ++j) {
160 0 : p[j].coeff = ctx->coeff_pbuf[j][0];
161 0 : p[j].qcoeff = ctx->qcoeff_pbuf[j][0];
162 0 : pd[j].dqcoeff = ctx->dqcoeff_pbuf[j][0];
163 0 : p[j].eobs = ctx->eobs_pbuf[j][0];
164 : }
165 : }
166 : }
167 :
168 : // Encode a frame
169 0 : for (i = 0; i < num_workers; i++) {
170 0 : VPxWorker *const worker = &cpi->workers[i];
171 0 : EncWorkerData *const thread_data = (EncWorkerData *)worker->data1;
172 :
173 : // Set the starting tile for each thread.
174 0 : thread_data->start = i;
175 :
176 0 : if (i == cpi->num_workers - 1)
177 0 : winterface->execute(worker);
178 : else
179 0 : winterface->launch(worker);
180 : }
181 :
182 : // Encoding ends.
183 0 : for (i = 0; i < num_workers; i++) {
184 0 : VPxWorker *const worker = &cpi->workers[i];
185 0 : winterface->sync(worker);
186 : }
187 :
188 0 : for (i = 0; i < num_workers; i++) {
189 0 : VPxWorker *const worker = &cpi->workers[i];
190 0 : EncWorkerData *const thread_data = (EncWorkerData *)worker->data1;
191 :
192 : // Accumulate counters.
193 0 : if (i < cpi->num_workers - 1) {
194 0 : vp9_accumulate_frame_counts(&cm->counts, thread_data->td->counts, 0);
195 0 : accumulate_rd_opt(&cpi->td, thread_data->td);
196 : }
197 : }
198 0 : }
|