1 /*
2  * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3  *
4  * This source code is subject to the terms of the BSD 2 Clause License and
5  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6  * was not distributed with this source code in the LICENSE file, you can
7  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8  * Media Patent License 1.0 was not distributed with this source code in the
9  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10  */
11 
12 #include "av1/encoder/encodeframe.h"
13 #include "av1/encoder/encoder.h"
14 #include "av1/encoder/ethread.h"
15 #include "aom_dsp/aom_dsp_common.h"
16 
accumulate_rd_opt(ThreadData * td,ThreadData * td_t)17 static void accumulate_rd_opt(ThreadData *td, ThreadData *td_t) {
18   for (int 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 (int i = 0; i < REF_FRAMES; i++)
22     td->rd_counts.global_motion_used[i] +=
23         td_t->rd_counts.global_motion_used[i];
24 
25   td->rd_counts.compound_ref_used_flag |=
26       td_t->rd_counts.compound_ref_used_flag;
27   td->rd_counts.skip_mode_used_flag |= td_t->rd_counts.skip_mode_used_flag;
28 }
29 
enc_worker_hook(void * arg1,void * unused)30 static int enc_worker_hook(void *arg1, void *unused) {
31   EncWorkerData *const thread_data = (EncWorkerData *)arg1;
32   AV1_COMP *const cpi = thread_data->cpi;
33   const AV1_COMMON *const cm = &cpi->common;
34   const int tile_cols = cm->tile_cols;
35   const int tile_rows = cm->tile_rows;
36   int t;
37 
38   (void)unused;
39 
40   for (t = thread_data->start; t < tile_rows * tile_cols;
41        t += cpi->num_workers) {
42     int tile_row = t / tile_cols;
43     int tile_col = t % tile_cols;
44 
45     av1_encode_tile(cpi, thread_data->td, tile_row, tile_col);
46   }
47 
48   return 1;
49 }
50 
create_enc_workers(AV1_COMP * cpi,int num_workers)51 static void create_enc_workers(AV1_COMP *cpi, int num_workers) {
52   AV1_COMMON *const cm = &cpi->common;
53   const AVxWorkerInterface *const winterface = aom_get_worker_interface();
54 
55   CHECK_MEM_ERROR(cm, cpi->workers,
56                   aom_malloc(num_workers * sizeof(*cpi->workers)));
57 
58   CHECK_MEM_ERROR(cm, cpi->tile_thr_data,
59                   aom_calloc(num_workers, sizeof(*cpi->tile_thr_data)));
60 
61   for (int i = 0; i < num_workers; i++) {
62     AVxWorker *const worker = &cpi->workers[i];
63     EncWorkerData *const thread_data = &cpi->tile_thr_data[i];
64 
65     ++cpi->num_workers;
66     winterface->init(worker);
67 
68     thread_data->cpi = cpi;
69 
70     if (i < num_workers - 1) {
71       // Allocate thread data.
72       CHECK_MEM_ERROR(cm, thread_data->td,
73                       aom_memalign(32, sizeof(*thread_data->td)));
74       av1_zero(*thread_data->td);
75 
76       // Set up pc_tree.
77       thread_data->td->pc_tree = NULL;
78       av1_setup_pc_tree(cm, thread_data->td);
79 
80       CHECK_MEM_ERROR(cm, thread_data->td->above_pred_buf,
81                       (uint8_t *)aom_memalign(
82                           16, MAX_MB_PLANE * MAX_SB_SQUARE *
83                                   sizeof(*thread_data->td->above_pred_buf)));
84       CHECK_MEM_ERROR(cm, thread_data->td->left_pred_buf,
85                       (uint8_t *)aom_memalign(
86                           16, MAX_MB_PLANE * MAX_SB_SQUARE *
87                                   sizeof(*thread_data->td->left_pred_buf)));
88 
89       CHECK_MEM_ERROR(
90           cm, thread_data->td->wsrc_buf,
91           (int32_t *)aom_memalign(
92               16, MAX_SB_SQUARE * sizeof(*thread_data->td->wsrc_buf)));
93 
94       for (int x = 0; x < 2; x++)
95         for (int y = 0; y < 2; y++)
96           CHECK_MEM_ERROR(
97               cm, thread_data->td->hash_value_buffer[x][y],
98               (uint32_t *)aom_malloc(
99                   AOM_BUFFER_SIZE_FOR_BLOCK_HASH *
100                   sizeof(*thread_data->td->hash_value_buffer[0][0])));
101 
102       CHECK_MEM_ERROR(
103           cm, thread_data->td->mask_buf,
104           (int32_t *)aom_memalign(
105               16, MAX_SB_SQUARE * sizeof(*thread_data->td->mask_buf)));
106       // Allocate frame counters in thread data.
107       CHECK_MEM_ERROR(cm, thread_data->td->counts,
108                       aom_calloc(1, sizeof(*thread_data->td->counts)));
109 
110       // Allocate buffers used by palette coding mode.
111       CHECK_MEM_ERROR(
112           cm, thread_data->td->palette_buffer,
113           aom_memalign(16, sizeof(*thread_data->td->palette_buffer)));
114 
115       CHECK_MEM_ERROR(
116           cm, thread_data->td->tmp_conv_dst,
117           aom_memalign(32, MAX_SB_SIZE * MAX_SB_SIZE *
118                                sizeof(*thread_data->td->tmp_conv_dst)));
119       for (int j = 0; j < 2; ++j) {
120         CHECK_MEM_ERROR(
121             cm, thread_data->td->tmp_obmc_bufs[j],
122             aom_memalign(16, 2 * MAX_MB_PLANE * MAX_SB_SQUARE *
123                                  sizeof(*thread_data->td->tmp_obmc_bufs[j])));
124       }
125 
126       // Create threads
127       if (!winterface->reset(worker))
128         aom_internal_error(&cm->error, AOM_CODEC_ERROR,
129                            "Tile encoder thread creation failed");
130     } else {
131       // Main thread acts as a worker and uses the thread data in cpi.
132       thread_data->td = &cpi->td;
133     }
134     winterface->sync(worker);
135   }
136 }
137 
launch_enc_workers(AV1_COMP * cpi,int num_workers)138 static void launch_enc_workers(AV1_COMP *cpi, int num_workers) {
139   const AVxWorkerInterface *const winterface = aom_get_worker_interface();
140   // Encode a frame
141   for (int i = 0; i < num_workers; i++) {
142     AVxWorker *const worker = &cpi->workers[i];
143     EncWorkerData *const thread_data = (EncWorkerData *)worker->data1;
144 
145     // Set the starting tile for each thread.
146     thread_data->start = i;
147 
148     if (i == cpi->num_workers - 1)
149       winterface->execute(worker);
150     else
151       winterface->launch(worker);
152   }
153 }
154 
sync_enc_workers(AV1_COMP * cpi,int num_workers)155 static void sync_enc_workers(AV1_COMP *cpi, int num_workers) {
156   const AVxWorkerInterface *const winterface = aom_get_worker_interface();
157 
158   // Encoding ends.
159   for (int i = 0; i < num_workers; i++) {
160     AVxWorker *const worker = &cpi->workers[i];
161     winterface->sync(worker);
162   }
163 }
164 
accumulate_counters_enc_workers(AV1_COMP * cpi,int num_workers)165 static void accumulate_counters_enc_workers(AV1_COMP *cpi, int num_workers) {
166   for (int i = 0; i < num_workers; i++) {
167     AVxWorker *const worker = &cpi->workers[i];
168     EncWorkerData *const thread_data = (EncWorkerData *)worker->data1;
169     cpi->intrabc_used |= thread_data->td->intrabc_used_this_tile;
170     // Accumulate counters.
171     if (i < cpi->num_workers - 1) {
172       av1_accumulate_frame_counts(&cpi->counts, thread_data->td->counts);
173       accumulate_rd_opt(&cpi->td, thread_data->td);
174       cpi->td.mb.txb_split_count += thread_data->td->mb.txb_split_count;
175     }
176   }
177 }
178 
prepare_enc_workers(AV1_COMP * cpi,AVxWorkerHook hook,int num_workers)179 static void prepare_enc_workers(AV1_COMP *cpi, AVxWorkerHook hook,
180                                 int num_workers) {
181   for (int i = 0; i < num_workers; i++) {
182     AVxWorker *const worker = &cpi->workers[i];
183     EncWorkerData *const thread_data = &cpi->tile_thr_data[i];
184 
185     worker->hook = hook;
186     worker->data1 = thread_data;
187     worker->data2 = NULL;
188 
189     // Before encoding a frame, copy the thread data from cpi.
190     if (thread_data->td != &cpi->td) {
191       thread_data->td->mb = cpi->td.mb;
192       thread_data->td->rd_counts = cpi->td.rd_counts;
193       thread_data->td->mb.above_pred_buf = thread_data->td->above_pred_buf;
194       thread_data->td->mb.left_pred_buf = thread_data->td->left_pred_buf;
195       thread_data->td->mb.wsrc_buf = thread_data->td->wsrc_buf;
196       for (int x = 0; x < 2; x++) {
197         for (int y = 0; y < 2; y++) {
198           memcpy(thread_data->td->hash_value_buffer[x][y],
199                  cpi->td.mb.hash_value_buffer[x][y],
200                  AOM_BUFFER_SIZE_FOR_BLOCK_HASH *
201                      sizeof(*thread_data->td->hash_value_buffer[0][0]));
202           thread_data->td->mb.hash_value_buffer[x][y] =
203               thread_data->td->hash_value_buffer[x][y];
204         }
205       }
206       thread_data->td->mb.mask_buf = thread_data->td->mask_buf;
207     }
208     if (thread_data->td->counts != &cpi->counts) {
209       memcpy(thread_data->td->counts, &cpi->counts, sizeof(cpi->counts));
210     }
211 
212     if (i < num_workers - 1) {
213       thread_data->td->mb.palette_buffer = thread_data->td->palette_buffer;
214       thread_data->td->mb.tmp_conv_dst = thread_data->td->tmp_conv_dst;
215       for (int j = 0; j < 2; ++j) {
216         thread_data->td->mb.tmp_obmc_bufs[j] =
217             thread_data->td->tmp_obmc_bufs[j];
218       }
219 
220       thread_data->td->mb.e_mbd.tmp_conv_dst = thread_data->td->mb.tmp_conv_dst;
221       for (int j = 0; j < 2; ++j) {
222         thread_data->td->mb.e_mbd.tmp_obmc_bufs[j] =
223             thread_data->td->mb.tmp_obmc_bufs[j];
224       }
225     }
226   }
227 }
228 
av1_encode_tiles_mt(AV1_COMP * cpi)229 void av1_encode_tiles_mt(AV1_COMP *cpi) {
230   AV1_COMMON *const cm = &cpi->common;
231   const int tile_cols = cm->tile_cols;
232   const int tile_rows = cm->tile_rows;
233   int num_workers = AOMMIN(cpi->oxcf.max_threads, tile_cols * tile_rows);
234 
235   if (cpi->tile_data == NULL || cpi->allocated_tiles < tile_cols * tile_rows)
236     av1_alloc_tile_data(cpi);
237 
238   av1_init_tile_data(cpi);
239   // Only run once to create threads and allocate thread data.
240   if (cpi->num_workers == 0) {
241     create_enc_workers(cpi, num_workers);
242   } else {
243     num_workers = AOMMIN(num_workers, cpi->num_workers);
244   }
245   prepare_enc_workers(cpi, enc_worker_hook, num_workers);
246   launch_enc_workers(cpi, num_workers);
247   sync_enc_workers(cpi, num_workers);
248   accumulate_counters_enc_workers(cpi, num_workers);
249 }
250 
251 // Accumulate frame counts. FRAME_COUNTS consist solely of 'unsigned int'
252 // members, so we treat it as an array, and sum over the whole length.
av1_accumulate_frame_counts(FRAME_COUNTS * acc_counts,const FRAME_COUNTS * counts)253 void av1_accumulate_frame_counts(FRAME_COUNTS *acc_counts,
254                                  const FRAME_COUNTS *counts) {
255   unsigned int *const acc = (unsigned int *)acc_counts;
256   const unsigned int *const cnt = (const unsigned int *)counts;
257 
258   const unsigned int n_counts = sizeof(FRAME_COUNTS) / sizeof(unsigned int);
259 
260   for (unsigned int i = 0; i < n_counts; i++) acc[i] += cnt[i];
261 }
262