1 /*
2 * Copyright (c) 2010 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
12 #include "onyx_int.h"
13 #include "threading.h"
14 #include "common.h"
15 #include "extend.h"
16
17
18 extern int vp8cx_encode_inter_macroblock(VP8_COMP *cpi, MACROBLOCK *x, TOKENEXTRA **t, int recon_yoffset, int recon_uvoffset);
19 extern int vp8cx_encode_intra_macro_block(VP8_COMP *cpi, MACROBLOCK *x, TOKENEXTRA **t);
20 extern void vp8cx_mb_init_quantizer(VP8_COMP *cpi, MACROBLOCK *x);
21 extern void vp8_build_block_offsets(MACROBLOCK *x);
22 extern void vp8_setup_block_ptrs(MACROBLOCK *x);
23
24 static
thread_encoding_proc(void * p_data)25 THREAD_FUNCTION thread_encoding_proc(void *p_data)
26 {
27 #if CONFIG_MULTITHREAD
28 int ithread = ((ENCODETHREAD_DATA *)p_data)->ithread;
29 VP8_COMP *cpi = (VP8_COMP *)(((ENCODETHREAD_DATA *)p_data)->ptr1);
30 MB_ROW_COMP *mbri = (MB_ROW_COMP *)(((ENCODETHREAD_DATA *)p_data)->ptr2);
31 ENTROPY_CONTEXT_PLANES mb_row_left_context;
32
33 //printf("Started thread %d\n", ithread);
34
35 while (1)
36 {
37 if (cpi->b_multi_threaded == 0)
38 break;
39
40 //if(WaitForSingleObject(cpi->h_event_mbrencoding[ithread], INFINITE) == WAIT_OBJECT_0)
41 if (sem_wait(&cpi->h_event_mbrencoding[ithread]) == 0)
42 {
43 if (cpi->b_multi_threaded == FALSE) // we're shutting down
44 break;
45 else
46 {
47 VP8_COMMON *cm = &cpi->common;
48 int mb_row = mbri->mb_row;
49 MACROBLOCK *x = &mbri->mb;
50 MACROBLOCKD *xd = &x->e_mbd;
51 TOKENEXTRA **tp = &mbri->tp;
52 int *segment_counts = mbri->segment_counts;
53 int *totalrate = &mbri->totalrate;
54
55 {
56 int i;
57 int recon_yoffset, recon_uvoffset;
58 int mb_col;
59 int ref_fb_idx = cm->lst_fb_idx;
60 int dst_fb_idx = cm->new_fb_idx;
61 int recon_y_stride = cm->yv12_fb[ref_fb_idx].y_stride;
62 int recon_uv_stride = cm->yv12_fb[ref_fb_idx].uv_stride;
63 volatile int *last_row_current_mb_col;
64
65 if (ithread > 0)
66 last_row_current_mb_col = &cpi->mb_row_ei[ithread-1].current_mb_col;
67 else
68 last_row_current_mb_col = &cpi->current_mb_col_main;
69
70 // reset above block coeffs
71 xd->above_context = cm->above_context;
72 xd->left_context = &mb_row_left_context;
73
74 vp8_zero(mb_row_left_context);
75
76 xd->up_available = (mb_row != 0);
77 recon_yoffset = (mb_row * recon_y_stride * 16);
78 recon_uvoffset = (mb_row * recon_uv_stride * 8);
79
80
81 cpi->tplist[mb_row].start = *tp;
82
83 //printf("Thread mb_row = %d\n", mb_row);
84
85 // for each macroblock col in image
86 for (mb_col = 0; mb_col < cm->mb_cols; mb_col++)
87 {
88 int seg_map_index = (mb_row * cm->mb_cols);
89
90 while (mb_col > (*last_row_current_mb_col - 1) && *last_row_current_mb_col != cm->mb_cols - 1)
91 {
92 x86_pause_hint();
93 thread_sleep(0);
94 }
95
96 // Distance of Mb to the various image edges.
97 // These specified to 8th pel as they are always compared to values that are in 1/8th pel units
98 xd->mb_to_left_edge = -((mb_col * 16) << 3);
99 xd->mb_to_right_edge = ((cm->mb_cols - 1 - mb_col) * 16) << 3;
100 xd->mb_to_top_edge = -((mb_row * 16) << 3);
101 xd->mb_to_bottom_edge = ((cm->mb_rows - 1 - mb_row) * 16) << 3;
102
103 // Set up limit values for motion vectors used to prevent them extending outside the UMV borders
104 x->mv_col_min = -((mb_col * 16) + (VP8BORDERINPIXELS - 16));
105 x->mv_col_max = ((cm->mb_cols - 1 - mb_col) * 16) + (VP8BORDERINPIXELS - 16);
106 x->mv_row_min = -((mb_row * 16) + (VP8BORDERINPIXELS - 16));
107 x->mv_row_max = ((cm->mb_rows - 1 - mb_row) * 16) + (VP8BORDERINPIXELS - 16);
108
109 xd->dst.y_buffer = cm->yv12_fb[dst_fb_idx].y_buffer + recon_yoffset;
110 xd->dst.u_buffer = cm->yv12_fb[dst_fb_idx].u_buffer + recon_uvoffset;
111 xd->dst.v_buffer = cm->yv12_fb[dst_fb_idx].v_buffer + recon_uvoffset;
112 xd->left_available = (mb_col != 0);
113
114 // Is segmentation enabled
115 // MB level adjutment to quantizer
116 if (xd->segmentation_enabled)
117 {
118 // Code to set segment id in xd->mbmi.segment_id for current MB (with range checking)
119 if (cpi->segmentation_map[seg_map_index+mb_col] <= 3)
120 xd->mode_info_context->mbmi.segment_id = cpi->segmentation_map[seg_map_index+mb_col];
121 else
122 xd->mode_info_context->mbmi.segment_id = 0;
123
124 vp8cx_mb_init_quantizer(cpi, x);
125 }
126 else
127 xd->mode_info_context->mbmi.segment_id = 0; // Set to Segment 0 by default
128
129
130 if (cm->frame_type == KEY_FRAME)
131 {
132 *totalrate += vp8cx_encode_intra_macro_block(cpi, x, tp);
133 #ifdef MODE_STATS
134 y_modes[xd->mbmi.mode] ++;
135 #endif
136 }
137 else
138 {
139 *totalrate += vp8cx_encode_inter_macroblock(cpi, x, tp, recon_yoffset, recon_uvoffset);
140
141 #ifdef MODE_STATS
142 inter_y_modes[xd->mbmi.mode] ++;
143
144 if (xd->mbmi.mode == SPLITMV)
145 {
146 int b;
147
148 for (b = 0; b < xd->mbmi.partition_count; b++)
149 {
150 inter_b_modes[x->partition->bmi[b].mode] ++;
151 }
152 }
153
154 #endif
155
156 // Count of last ref frame 0,0 useage
157 if ((xd->mode_info_context->mbmi.mode == ZEROMV) && (xd->mode_info_context->mbmi.ref_frame == LAST_FRAME))
158 cpi->inter_zz_count ++;
159
160 }
161
162 cpi->tplist[mb_row].stop = *tp;
163
164 x->gf_active_ptr++; // Increment pointer into gf useage flags structure for next mb
165
166 for (i = 0; i < 16; i++)
167 vpx_memcpy(&xd->mode_info_context->bmi[i], &xd->block[i].bmi, sizeof(xd->block[i].bmi));
168
169 // adjust to the next column of macroblocks
170 x->src.y_buffer += 16;
171 x->src.u_buffer += 8;
172 x->src.v_buffer += 8;
173
174 recon_yoffset += 16;
175 recon_uvoffset += 8;
176
177 // Keep track of segment useage
178 segment_counts[xd->mode_info_context->mbmi.segment_id] ++;
179
180 // skip to next mb
181 xd->mode_info_context++;
182 x->partition_info++;
183
184 xd->above_context++;
185
186 cpi->mb_row_ei[ithread].current_mb_col = mb_col;
187
188 }
189
190 //extend the recon for intra prediction
191 vp8_extend_mb_row(
192 &cm->yv12_fb[dst_fb_idx],
193 xd->dst.y_buffer + 16,
194 xd->dst.u_buffer + 8,
195 xd->dst.v_buffer + 8);
196
197 // this is to account for the border
198 xd->mode_info_context++;
199 x->partition_info++;
200
201 x->src.y_buffer += 16 * x->src.y_stride * (cpi->encoding_thread_count + 1) - 16 * cm->mb_cols;
202 x->src.u_buffer += 8 * x->src.uv_stride * (cpi->encoding_thread_count + 1) - 8 * cm->mb_cols;
203 x->src.v_buffer += 8 * x->src.uv_stride * (cpi->encoding_thread_count + 1) - 8 * cm->mb_cols;
204
205 xd->mode_info_context += xd->mode_info_stride * cpi->encoding_thread_count;
206 x->partition_info += xd->mode_info_stride * cpi->encoding_thread_count;
207
208 if (ithread == (cpi->encoding_thread_count - 1) || mb_row == cm->mb_rows - 1)
209 {
210 //SetEvent(cpi->h_event_main);
211 sem_post(&cpi->h_event_main);
212 }
213
214 }
215
216 }
217 }
218 }
219
220 #else
221 (void) p_data;
222 #endif
223
224 //printf("exit thread %d\n", ithread);
225 return 0;
226 }
227
setup_mbby_copy(MACROBLOCK * mbdst,MACROBLOCK * mbsrc)228 static void setup_mbby_copy(MACROBLOCK *mbdst, MACROBLOCK *mbsrc)
229 {
230
231 MACROBLOCK *x = mbsrc;
232 MACROBLOCK *z = mbdst;
233 int i;
234
235 z->ss = x->ss;
236 z->ss_count = x->ss_count;
237 z->searches_per_step = x->searches_per_step;
238 z->errorperbit = x->errorperbit;
239
240 z->sadperbit16 = x->sadperbit16;
241 z->sadperbit4 = x->sadperbit4;
242 z->errthresh = x->errthresh;
243 z->rddiv = x->rddiv;
244 z->rdmult = x->rdmult;
245
246 /*
247 z->mv_col_min = x->mv_col_min;
248 z->mv_col_max = x->mv_col_max;
249 z->mv_row_min = x->mv_row_min;
250 z->mv_row_max = x->mv_row_max;
251 z->vector_range = x->vector_range ;
252 */
253
254 z->vp8_short_fdct4x4 = x->vp8_short_fdct4x4;
255 z->vp8_short_fdct8x4 = x->vp8_short_fdct8x4;
256 z->short_walsh4x4 = x->short_walsh4x4;
257 z->quantize_b = x->quantize_b;
258
259 /*
260 z->mvc = x->mvc;
261 z->src.y_buffer = x->src.y_buffer;
262 z->src.u_buffer = x->src.u_buffer;
263 z->src.v_buffer = x->src.v_buffer;
264 */
265
266
267 vpx_memcpy(z->mvcosts, x->mvcosts, sizeof(x->mvcosts));
268 z->mvcost[0] = &z->mvcosts[0][mv_max+1];
269 z->mvcost[1] = &z->mvcosts[1][mv_max+1];
270 z->mvsadcost[0] = &z->mvsadcosts[0][mv_max+1];
271 z->mvsadcost[1] = &z->mvsadcosts[1][mv_max+1];
272
273
274 vpx_memcpy(z->token_costs, x->token_costs, sizeof(x->token_costs));
275 vpx_memcpy(z->inter_bmode_costs, x->inter_bmode_costs, sizeof(x->inter_bmode_costs));
276 //memcpy(z->mvcosts, x->mvcosts, sizeof(x->mvcosts));
277 //memcpy(z->mvcost, x->mvcost, sizeof(x->mvcost));
278 vpx_memcpy(z->mbmode_cost, x->mbmode_cost, sizeof(x->mbmode_cost));
279 vpx_memcpy(z->intra_uv_mode_cost, x->intra_uv_mode_cost, sizeof(x->intra_uv_mode_cost));
280 vpx_memcpy(z->bmode_costs, x->bmode_costs, sizeof(x->bmode_costs));
281
282 for (i = 0; i < 25; i++)
283 {
284 z->block[i].quant = x->block[i].quant;
285 z->block[i].quant_shift = x->block[i].quant_shift;
286 z->block[i].zbin = x->block[i].zbin;
287 z->block[i].zrun_zbin_boost = x->block[i].zrun_zbin_boost;
288 z->block[i].round = x->block[i].round;
289 /*
290 z->block[i].src = x->block[i].src;
291 */
292 z->block[i].src_stride = x->block[i].src_stride;
293 z->block[i].force_empty = x->block[i].force_empty;
294
295 }
296
297 {
298 MACROBLOCKD *xd = &x->e_mbd;
299 MACROBLOCKD *zd = &z->e_mbd;
300
301 /*
302 zd->mode_info_context = xd->mode_info_context;
303 zd->mode_info = xd->mode_info;
304
305 zd->mode_info_stride = xd->mode_info_stride;
306 zd->frame_type = xd->frame_type;
307 zd->up_available = xd->up_available ;
308 zd->left_available = xd->left_available;
309 zd->left_context = xd->left_context;
310 zd->last_frame_dc = xd->last_frame_dc;
311 zd->last_frame_dccons = xd->last_frame_dccons;
312 zd->gold_frame_dc = xd->gold_frame_dc;
313 zd->gold_frame_dccons = xd->gold_frame_dccons;
314 zd->mb_to_left_edge = xd->mb_to_left_edge;
315 zd->mb_to_right_edge = xd->mb_to_right_edge;
316 zd->mb_to_top_edge = xd->mb_to_top_edge ;
317 zd->mb_to_bottom_edge = xd->mb_to_bottom_edge;
318 zd->gf_active_ptr = xd->gf_active_ptr;
319 zd->frames_since_golden = xd->frames_since_golden;
320 zd->frames_till_alt_ref_frame = xd->frames_till_alt_ref_frame;
321 */
322 zd->subpixel_predict = xd->subpixel_predict;
323 zd->subpixel_predict8x4 = xd->subpixel_predict8x4;
324 zd->subpixel_predict8x8 = xd->subpixel_predict8x8;
325 zd->subpixel_predict16x16 = xd->subpixel_predict16x16;
326 zd->segmentation_enabled = xd->segmentation_enabled;
327 zd->mb_segement_abs_delta = xd->mb_segement_abs_delta;
328 vpx_memcpy(zd->segment_feature_data, xd->segment_feature_data, sizeof(xd->segment_feature_data));
329
330 for (i = 0; i < 25; i++)
331 {
332 zd->block[i].dequant = xd->block[i].dequant;
333 }
334 }
335 }
336
337
vp8cx_init_mbrthread_data(VP8_COMP * cpi,MACROBLOCK * x,MB_ROW_COMP * mbr_ei,int mb_row,int count)338 void vp8cx_init_mbrthread_data(VP8_COMP *cpi,
339 MACROBLOCK *x,
340 MB_ROW_COMP *mbr_ei,
341 int mb_row,
342 int count
343 )
344 {
345
346 VP8_COMMON *const cm = & cpi->common;
347 MACROBLOCKD *const xd = & x->e_mbd;
348 int i;
349 (void) mb_row;
350
351 for (i = 0; i < count; i++)
352 {
353 MACROBLOCK *mb = & mbr_ei[i].mb;
354 MACROBLOCKD *mbd = &mb->e_mbd;
355
356 mbd->subpixel_predict = xd->subpixel_predict;
357 mbd->subpixel_predict8x4 = xd->subpixel_predict8x4;
358 mbd->subpixel_predict8x8 = xd->subpixel_predict8x8;
359 mbd->subpixel_predict16x16 = xd->subpixel_predict16x16;
360 #if CONFIG_RUNTIME_CPU_DETECT
361 mbd->rtcd = xd->rtcd;
362 #endif
363 mb->gf_active_ptr = x->gf_active_ptr;
364
365 mb->vector_range = 32;
366
367 vpx_memset(mbr_ei[i].segment_counts, 0, sizeof(mbr_ei[i].segment_counts));
368 mbr_ei[i].totalrate = 0;
369
370 mb->partition_info = x->pi + x->e_mbd.mode_info_stride * (i + 1);
371
372 mbd->mode_info_context = cm->mi + x->e_mbd.mode_info_stride * (i + 1);
373 mbd->mode_info_stride = cm->mode_info_stride;
374
375 mbd->frame_type = cm->frame_type;
376
377 mbd->frames_since_golden = cm->frames_since_golden;
378 mbd->frames_till_alt_ref_frame = cm->frames_till_alt_ref_frame;
379
380 mb->src = * cpi->Source;
381 mbd->pre = cm->yv12_fb[cm->lst_fb_idx];
382 mbd->dst = cm->yv12_fb[cm->new_fb_idx];
383
384 mb->src.y_buffer += 16 * x->src.y_stride * (i + 1);
385 mb->src.u_buffer += 8 * x->src.uv_stride * (i + 1);
386 mb->src.v_buffer += 8 * x->src.uv_stride * (i + 1);
387
388
389 vp8_build_block_offsets(mb);
390
391 vp8_setup_block_dptrs(mbd);
392
393 vp8_setup_block_ptrs(mb);
394
395 mb->rddiv = cpi->RDDIV;
396 mb->rdmult = cpi->RDMULT;
397
398 mbd->left_context = &cm->left_context;
399 mb->mvc = cm->fc.mvc;
400
401 setup_mbby_copy(&mbr_ei[i].mb, x);
402
403 }
404 }
405
406
vp8cx_create_encoder_threads(VP8_COMP * cpi)407 void vp8cx_create_encoder_threads(VP8_COMP *cpi)
408 {
409 cpi->b_multi_threaded = 0;
410
411 cpi->processor_core_count = 32; //vp8_get_proc_core_count();
412
413 CHECK_MEM_ERROR(cpi->tplist, vpx_malloc(sizeof(TOKENLIST) * cpi->common.mb_rows));
414
415 #if CONFIG_MULTITHREAD
416
417 if (cpi->processor_core_count > 1 && cpi->oxcf.multi_threaded > 1)
418 {
419 int ithread;
420
421 if (cpi->oxcf.multi_threaded > cpi->processor_core_count)
422 cpi->encoding_thread_count = cpi->processor_core_count - 1;
423 else
424 cpi->encoding_thread_count = cpi->oxcf.multi_threaded - 1;
425
426
427 CHECK_MEM_ERROR(cpi->h_encoding_thread, vpx_malloc(sizeof(pthread_t) * cpi->encoding_thread_count));
428 CHECK_MEM_ERROR(cpi->h_event_mbrencoding, vpx_malloc(sizeof(sem_t) * cpi->encoding_thread_count));
429 CHECK_MEM_ERROR(cpi->mb_row_ei, vpx_memalign(32, sizeof(MB_ROW_COMP) * cpi->encoding_thread_count));
430 vpx_memset(cpi->mb_row_ei, 0, sizeof(MB_ROW_COMP) * cpi->encoding_thread_count);
431 CHECK_MEM_ERROR(cpi->en_thread_data, vpx_malloc(sizeof(ENCODETHREAD_DATA) * cpi->encoding_thread_count));
432 //cpi->h_event_main = CreateEvent(NULL, FALSE, FALSE, NULL);
433 sem_init(&cpi->h_event_main, 0, 0);
434
435 cpi->b_multi_threaded = 1;
436
437 //printf("[VP8:] multi_threaded encoding is enabled with %d threads\n\n", (cpi->encoding_thread_count +1));
438
439 for (ithread = 0; ithread < cpi->encoding_thread_count; ithread++)
440 {
441 //cpi->h_event_mbrencoding[ithread] = CreateEvent(NULL, FALSE, FALSE, NULL);
442 sem_init(&cpi->h_event_mbrencoding[ithread], 0, 0);
443 cpi->en_thread_data[ithread].ithread = ithread;
444 cpi->en_thread_data[ithread].ptr1 = (void *)cpi;
445 cpi->en_thread_data[ithread].ptr2 = (void *)&cpi->mb_row_ei[ithread];
446
447 //printf(" call begin thread %d \n", ithread);
448
449 //cpi->h_encoding_thread[ithread] = (HANDLE)_beginthreadex(
450 // NULL, // security
451 // 0, // stksize
452 // thread_encoding_proc,
453 // (&cpi->en_thread_data[ithread]), // Thread data
454 // 0,
455 // NULL);
456
457 pthread_create(&cpi->h_encoding_thread[ithread], 0, thread_encoding_proc, (&cpi->en_thread_data[ithread]));
458
459 }
460
461 }
462
463 #endif
464 }
465
vp8cx_remove_encoder_threads(VP8_COMP * cpi)466 void vp8cx_remove_encoder_threads(VP8_COMP *cpi)
467 {
468 #if CONFIG_MULTITHREAD
469
470 if (cpi->b_multi_threaded)
471 {
472 //shutdown other threads
473 cpi->b_multi_threaded = 0;
474 {
475 int i;
476
477 for (i = 0; i < cpi->encoding_thread_count; i++)
478 {
479 //SetEvent(cpi->h_event_mbrencoding[i]);
480 sem_post(&cpi->h_event_mbrencoding[i]);
481 pthread_join(cpi->h_encoding_thread[i], 0);
482 }
483
484 for (i = 0; i < cpi->encoding_thread_count; i++)
485 sem_destroy(&cpi->h_event_mbrencoding[i]);
486 }
487 //free thread related resources
488 vpx_free(cpi->h_event_mbrencoding);
489 vpx_free(cpi->h_encoding_thread);
490 vpx_free(cpi->mb_row_ei);
491 vpx_free(cpi->en_thread_data);
492 }
493
494 #endif
495 vpx_free(cpi->tplist);
496 }
497