]> granicus.if.org Git - libvpx/blob - vp10/encoder/ethread.c
Remove vp9_ prefix from vp10 files
[libvpx] / vp10 / encoder / ethread.c
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 "vp10/encoder/encodeframe.h"
12 #include "vp10/encoder/encoder.h"
13 #include "vp10/encoder/ethread.h"
14
15 static void accumulate_rd_opt(ThreadData *td, ThreadData *td_t) {
16   int i, j, k, l, m, n;
17
18   for (i = 0; i < REFERENCE_MODES; i++)
19     td->rd_counts.comp_pred_diff[i] += td_t->rd_counts.comp_pred_diff[i];
20
21   for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; i++)
22     td->rd_counts.filter_diff[i] += td_t->rd_counts.filter_diff[i];
23
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];
32 }
33
34 static int enc_worker_hook(EncWorkerData *const thread_data, void *unused) {
35   VP9_COMP *const cpi = thread_data->cpi;
36   const VP9_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;
39   int t;
40
41   (void) unused;
42
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;
47
48     vp10_encode_tile(cpi, thread_data->td, tile_row, tile_col);
49   }
50
51   return 0;
52 }
53
54 static int get_max_tile_cols(VP9_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;
58   int log2_tile_cols;
59
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);
64 }
65
66 void vp10_encode_tiles_mt(VP9_COMP *cpi) {
67   VP9_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);
71   int i;
72
73   vp10_init_tile_data(cpi);
74
75   // Only run once to create threads and allocate thread data.
76   if (cpi->num_workers == 0) {
77     int allocated_workers = num_workers;
78
79     // While using SVC, we need to allocate threads according to the highest
80     // resolution.
81     if (cpi->use_svc) {
82       int max_tile_cols = get_max_tile_cols(cpi);
83       allocated_workers = MIN(cpi->oxcf.max_threads, max_tile_cols);
84     }
85
86     CHECK_MEM_ERROR(cm, cpi->workers,
87                     vpx_malloc(allocated_workers * sizeof(*cpi->workers)));
88
89     CHECK_MEM_ERROR(cm, cpi->tile_thr_data,
90                     vpx_calloc(allocated_workers,
91                     sizeof(*cpi->tile_thr_data)));
92
93     for (i = 0; i < allocated_workers; i++) {
94       VPxWorker *const worker = &cpi->workers[i];
95       EncWorkerData *thread_data = &cpi->tile_thr_data[i];
96
97       ++cpi->num_workers;
98       winterface->init(worker);
99
100       if (i < allocated_workers - 1) {
101         thread_data->cpi = cpi;
102
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);
107
108         // Set up pc_tree.
109         thread_data->td->leaf_tree = NULL;
110         thread_data->td->pc_tree = NULL;
111         vp10_setup_pc_tree(cm, thread_data->td);
112
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)));
116
117         // Create threads
118         if (!winterface->reset(worker))
119           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         thread_data->cpi = cpi;
124         thread_data->td = &cpi->td;
125       }
126
127       winterface->sync(worker);
128     }
129   }
130
131   for (i = 0; i < num_workers; i++) {
132     VPxWorker *const worker = &cpi->workers[i];
133     EncWorkerData *thread_data;
134
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;
139
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;
144     }
145     if (thread_data->td->counts != &cpi->common.counts) {
146       memcpy(thread_data->td->counts, &cpi->common.counts,
147              sizeof(cpi->common.counts));
148     }
149
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;
157       int j;
158
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];
164       }
165     }
166   }
167
168   // Encode a frame
169   for (i = 0; i < num_workers; i++) {
170     VPxWorker *const worker = &cpi->workers[i];
171     EncWorkerData *const thread_data = (EncWorkerData*)worker->data1;
172
173     // Set the starting tile for each thread.
174     thread_data->start = i;
175
176     if (i == cpi->num_workers - 1)
177       winterface->execute(worker);
178     else
179       winterface->launch(worker);
180   }
181
182   // Encoding ends.
183   for (i = 0; i < num_workers; i++) {
184     VPxWorker *const worker = &cpi->workers[i];
185     winterface->sync(worker);
186   }
187
188   for (i = 0; i < num_workers; i++) {
189     VPxWorker *const worker = &cpi->workers[i];
190     EncWorkerData *const thread_data = (EncWorkerData*)worker->data1;
191
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);
196     }
197   }
198 }