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