]> granicus.if.org Git - libvpx/blob - vp10/encoder/ethread.c
Changes to exhaustive motion search.
[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 #include "vpx_dsp/vpx_dsp_common.h"
15
16 static void accumulate_rd_opt(ThreadData *td, ThreadData *td_t) {
17   int i, j, k, l, m, n;
18
19   for (i = 0; i < REFERENCE_MODES; i++)
20     td->rd_counts.comp_pred_diff[i] += td_t->rd_counts.comp_pred_diff[i];
21
22   for (i = 0; i < SWITCHABLE_FILTER_CONTEXTS; i++)
23     td->rd_counts.filter_diff[i] += td_t->rd_counts.filter_diff[i];
24
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];
33
34
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;
38 }
39
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;
45   int t;
46
47   (void) unused;
48
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;
53
54     vp10_encode_tile(cpi, thread_data->td, tile_row, tile_col);
55   }
56
57   return 0;
58 }
59
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);
65   int i;
66
67   vp10_init_tile_data(cpi);
68
69   // Only run once to create threads and allocate thread data.
70   if (cpi->num_workers == 0) {
71     int allocated_workers = num_workers;
72
73     CHECK_MEM_ERROR(cm, cpi->workers,
74                     vpx_malloc(allocated_workers * sizeof(*cpi->workers)));
75
76     CHECK_MEM_ERROR(cm, cpi->tile_thr_data,
77                     vpx_calloc(allocated_workers,
78                     sizeof(*cpi->tile_thr_data)));
79
80     for (i = 0; i < allocated_workers; i++) {
81       VPxWorker *const worker = &cpi->workers[i];
82       EncWorkerData *thread_data = &cpi->tile_thr_data[i];
83
84       ++cpi->num_workers;
85       winterface->init(worker);
86
87       if (i < allocated_workers - 1) {
88         thread_data->cpi = cpi;
89
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);
94
95         // Set up pc_tree.
96         thread_data->td->leaf_tree = NULL;
97         thread_data->td->pc_tree = NULL;
98         vp10_setup_pc_tree(cm, thread_data->td);
99
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)));
103
104         // Create threads
105         if (!winterface->reset(worker))
106           vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
107                              "Tile encoder thread creation failed");
108       } else {
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;
112       }
113
114       winterface->sync(worker);
115     }
116   }
117
118   for (i = 0; i < num_workers; i++) {
119     VPxWorker *const worker = &cpi->workers[i];
120     EncWorkerData *thread_data;
121
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;
126
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;
131     }
132     if (thread_data->td->counts != &cpi->common.counts) {
133       memcpy(thread_data->td->counts, &cpi->common.counts,
134              sizeof(cpi->common.counts));
135     }
136   }
137
138   // Encode a frame
139   for (i = 0; i < num_workers; i++) {
140     VPxWorker *const worker = &cpi->workers[i];
141     EncWorkerData *const thread_data = (EncWorkerData*)worker->data1;
142
143     // Set the starting tile for each thread.
144     thread_data->start = i;
145
146     if (i == cpi->num_workers - 1)
147       winterface->execute(worker);
148     else
149       winterface->launch(worker);
150   }
151
152   // Encoding ends.
153   for (i = 0; i < num_workers; i++) {
154     VPxWorker *const worker = &cpi->workers[i];
155     winterface->sync(worker);
156   }
157
158   for (i = 0; i < num_workers; i++) {
159     VPxWorker *const worker = &cpi->workers[i];
160     EncWorkerData *const thread_data = (EncWorkerData*)worker->data1;
161
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);
166     }
167   }
168 }