2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
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.
12 #include "vp8/common/threading.h"
13 #include "vp8/common/common.h"
14 #include "vp8/common/extend.h"
16 #include "bitstream.h"
18 #if CONFIG_MULTITHREAD
20 extern int vp8cx_encode_inter_macroblock(VP8_COMP *cpi, MACROBLOCK *x,
22 int recon_yoffset, int recon_uvoffset,
23 int mb_row, int mb_col);
24 extern int vp8cx_encode_intra_macroblock(VP8_COMP *cpi, MACROBLOCK *x,
26 extern void vp8cx_mb_init_quantizer(VP8_COMP *cpi, MACROBLOCK *x, int ok_to_skip);
27 extern void vp8_build_block_offsets(MACROBLOCK *x);
28 extern void vp8_setup_block_ptrs(MACROBLOCK *x);
30 extern void vp8_loopfilter_frame(VP8_COMP *cpi, VP8_COMMON *cm);
32 static THREAD_FUNCTION thread_loopfilter(void *p_data)
34 VP8_COMP *cpi = (VP8_COMP *)(((LPFTHREAD_DATA *)p_data)->ptr1);
35 VP8_COMMON *cm = &cpi->common;
39 if (cpi->b_multi_threaded == 0)
42 if (sem_wait(&cpi->h_event_start_lpf) == 0)
44 if (cpi->b_multi_threaded == 0) // we're shutting down
47 vp8_loopfilter_frame(cpi, cm);
49 sem_post(&cpi->h_event_end_lpf);
57 THREAD_FUNCTION thread_encoding_proc(void *p_data)
59 int ithread = ((ENCODETHREAD_DATA *)p_data)->ithread;
60 VP8_COMP *cpi = (VP8_COMP *)(((ENCODETHREAD_DATA *)p_data)->ptr1);
61 MB_ROW_COMP *mbri = (MB_ROW_COMP *)(((ENCODETHREAD_DATA *)p_data)->ptr2);
62 ENTROPY_CONTEXT_PLANES mb_row_left_context;
64 const int nsync = cpi->mt_sync_range;
65 //printf("Started thread %d\n", ithread);
69 if (cpi->b_multi_threaded == 0)
72 //if(WaitForSingleObject(cpi->h_event_mbrencoding[ithread], INFINITE) == WAIT_OBJECT_0)
73 if (sem_wait(&cpi->h_event_start_encoding[ithread]) == 0)
75 VP8_COMMON *cm = &cpi->common;
77 MACROBLOCK *x = &mbri->mb;
78 MACROBLOCKD *xd = &x->e_mbd;
80 #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
81 TOKENEXTRA *tp_start = cpi->tok + (1 + ithread) * (16 * 24);
82 const int num_part = (1 << cm->multi_token_partition);
85 int *segment_counts = mbri->segment_counts;
86 int *totalrate = &mbri->totalrate;
88 if (cpi->b_multi_threaded == 0) // we're shutting down
91 for (mb_row = ithread + 1; mb_row < cm->mb_rows; mb_row += (cpi->encoding_thread_count + 1))
94 int recon_yoffset, recon_uvoffset;
96 int ref_fb_idx = cm->lst_fb_idx;
97 int dst_fb_idx = cm->new_fb_idx;
98 int recon_y_stride = cm->yv12_fb[ref_fb_idx].y_stride;
99 int recon_uv_stride = cm->yv12_fb[ref_fb_idx].uv_stride;
100 int map_index = (mb_row * cm->mb_cols);
101 volatile const int *last_row_current_mb_col;
102 volatile int *current_mb_col = &cpi->mt_current_mb_col[mb_row];
104 #if (CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING)
105 vp8_writer *w = &cpi->bc[1 + (mb_row % num_part)];
107 tp = cpi->tok + (mb_row * (cm->mb_cols * 16 * 24));
108 cpi->tplist[mb_row].start = tp;
111 last_row_current_mb_col = &cpi->mt_current_mb_col[mb_row - 1];
113 // reset above block coeffs
114 xd->above_context = cm->above_context;
115 xd->left_context = &mb_row_left_context;
117 vp8_zero(mb_row_left_context);
119 xd->up_available = (mb_row != 0);
120 recon_yoffset = (mb_row * recon_y_stride * 16);
121 recon_uvoffset = (mb_row * recon_uv_stride * 8);
123 // Set the mb activity pointer to the start of the row.
124 x->mb_activity_ptr = &cpi->mb_activity_map[map_index];
126 // for each macroblock col in image
127 for (mb_col = 0; mb_col < cm->mb_cols; mb_col++)
129 *current_mb_col = mb_col - 1;
131 if ((mb_col & (nsync - 1)) == 0)
133 while (mb_col > (*last_row_current_mb_col - nsync))
140 #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
144 // Distance of Mb to the various image edges.
145 // These specified to 8th pel as they are always compared to values that are in 1/8th pel units
146 xd->mb_to_left_edge = -((mb_col * 16) << 3);
147 xd->mb_to_right_edge = ((cm->mb_cols - 1 - mb_col) * 16) << 3;
148 xd->mb_to_top_edge = -((mb_row * 16) << 3);
149 xd->mb_to_bottom_edge = ((cm->mb_rows - 1 - mb_row) * 16) << 3;
151 // Set up limit values for motion vectors used to prevent them extending outside the UMV borders
152 x->mv_col_min = -((mb_col * 16) + (VP8BORDERINPIXELS - 16));
153 x->mv_col_max = ((cm->mb_cols - 1 - mb_col) * 16) + (VP8BORDERINPIXELS - 16);
154 x->mv_row_min = -((mb_row * 16) + (VP8BORDERINPIXELS - 16));
155 x->mv_row_max = ((cm->mb_rows - 1 - mb_row) * 16) + (VP8BORDERINPIXELS - 16);
157 xd->dst.y_buffer = cm->yv12_fb[dst_fb_idx].y_buffer + recon_yoffset;
158 xd->dst.u_buffer = cm->yv12_fb[dst_fb_idx].u_buffer + recon_uvoffset;
159 xd->dst.v_buffer = cm->yv12_fb[dst_fb_idx].v_buffer + recon_uvoffset;
160 xd->left_available = (mb_col != 0);
162 x->rddiv = cpi->RDDIV;
163 x->rdmult = cpi->RDMULT;
165 //Copy current mb to a buffer
166 vp8_copy_mem16x16(x->src.y_buffer, x->src.y_stride, x->thismb, 16);
168 if (cpi->oxcf.tuning == VP8_TUNE_SSIM)
169 vp8_activity_masking(cpi, x);
171 // Is segmentation enabled
172 // MB level adjustment to quantizer
173 if (xd->segmentation_enabled)
175 // Code to set segment id in xd->mbmi.segment_id for current MB (with range checking)
176 if (cpi->segmentation_map[map_index + mb_col] <= 3)
177 xd->mode_info_context->mbmi.segment_id = cpi->segmentation_map[map_index + mb_col];
179 xd->mode_info_context->mbmi.segment_id = 0;
181 vp8cx_mb_init_quantizer(cpi, x, 1);
184 xd->mode_info_context->mbmi.segment_id = 0; // Set to Segment 0 by default
186 x->active_ptr = cpi->active_map + map_index + mb_col;
188 if (cm->frame_type == KEY_FRAME)
190 *totalrate += vp8cx_encode_intra_macroblock(cpi, x, &tp);
192 y_modes[xd->mbmi.mode] ++;
197 *totalrate += vp8cx_encode_inter_macroblock(cpi, x, &tp, recon_yoffset, recon_uvoffset, mb_row, mb_col);
200 inter_y_modes[xd->mbmi.mode] ++;
202 if (xd->mbmi.mode == SPLITMV)
206 for (b = 0; b < xd->mbmi.partition_count; b++)
208 inter_b_modes[x->partition->bmi[b].mode] ++;
214 // Count of last ref frame 0,0 usage
215 if ((xd->mode_info_context->mbmi.mode == ZEROMV) && (xd->mode_info_context->mbmi.ref_frame == LAST_FRAME))
216 cpi->inter_zz_count++;
218 // Special case code for cyclic refresh
219 // If cyclic update enabled then copy xd->mbmi.segment_id; (which may have been updated based on mode
220 // during vp8cx_encode_inter_macroblock()) back into the global segmentation map
221 if (cpi->cyclic_refresh_mode_enabled && xd->segmentation_enabled)
223 const MB_MODE_INFO * mbmi = &xd->mode_info_context->mbmi;
224 cpi->segmentation_map[map_index + mb_col] = mbmi->segment_id;
226 // If the block has been refreshed mark it as clean (the magnitude of the -ve influences how long it will be before we consider another refresh):
227 // Else if it was coded (last frame 0,0) and has not already been refreshed then mark it as a candidate for cleanup next time (marked 0)
228 // else mark it as dirty (1).
229 if (mbmi->segment_id)
230 cpi->cyclic_refresh_map[map_index + mb_col] = -1;
231 else if ((mbmi->mode == ZEROMV) && (mbmi->ref_frame == LAST_FRAME))
233 if (cpi->cyclic_refresh_map[map_index + mb_col] == 1)
234 cpi->cyclic_refresh_map[map_index + mb_col] = 0;
237 cpi->cyclic_refresh_map[map_index + mb_col] = 1;
242 #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
243 /* pack tokens for this MB */
245 int tok_count = tp - tp_start;
246 pack_tokens(w, tp_start, tok_count);
249 cpi->tplist[mb_row].stop = tp;
251 // Increment pointer into gf usage flags structure.
254 // Increment the activity mask pointers.
255 x->mb_activity_ptr++;
257 // adjust to the next column of macroblocks
258 x->src.y_buffer += 16;
259 x->src.u_buffer += 8;
260 x->src.v_buffer += 8;
265 // Keep track of segment usage
266 segment_counts[xd->mode_info_context->mbmi.segment_id]++;
269 xd->mode_info_context++;
274 vp8_extend_mb_row( &cm->yv12_fb[dst_fb_idx],
275 xd->dst.y_buffer + 16,
276 xd->dst.u_buffer + 8,
277 xd->dst.v_buffer + 8);
279 *current_mb_col = mb_col + nsync;
281 // this is to account for the border
282 xd->mode_info_context++;
285 x->src.y_buffer += 16 * x->src.y_stride * (cpi->encoding_thread_count + 1) - 16 * cm->mb_cols;
286 x->src.u_buffer += 8 * x->src.uv_stride * (cpi->encoding_thread_count + 1) - 8 * cm->mb_cols;
287 x->src.v_buffer += 8 * x->src.uv_stride * (cpi->encoding_thread_count + 1) - 8 * cm->mb_cols;
289 xd->mode_info_context += xd->mode_info_stride * cpi->encoding_thread_count;
290 x->partition_info += xd->mode_info_stride * cpi->encoding_thread_count;
291 x->gf_active_ptr += cm->mb_cols * cpi->encoding_thread_count;
293 if (mb_row == cm->mb_rows - 1)
295 sem_post(&cpi->h_event_end_encoding); /* signal frame encoding end */
301 //printf("exit thread %d\n", ithread);
305 static void setup_mbby_copy(MACROBLOCK *mbdst, MACROBLOCK *mbsrc)
308 MACROBLOCK *x = mbsrc;
309 MACROBLOCK *z = mbdst;
313 z->ss_count = x->ss_count;
314 z->searches_per_step = x->searches_per_step;
315 z->errorperbit = x->errorperbit;
317 z->sadperbit16 = x->sadperbit16;
318 z->sadperbit4 = x->sadperbit4;
321 z->mv_col_min = x->mv_col_min;
322 z->mv_col_max = x->mv_col_max;
323 z->mv_row_min = x->mv_row_min;
324 z->mv_row_max = x->mv_row_max;
327 z->short_fdct4x4 = x->short_fdct4x4;
328 z->short_fdct8x4 = x->short_fdct8x4;
329 z->short_walsh4x4 = x->short_walsh4x4;
330 z->quantize_b = x->quantize_b;
331 z->quantize_b_pair = x->quantize_b_pair;
332 z->optimize = x->optimize;
336 z->src.y_buffer = x->src.y_buffer;
337 z->src.u_buffer = x->src.u_buffer;
338 z->src.v_buffer = x->src.v_buffer;
342 vpx_memcpy(z->mvcosts, x->mvcosts, sizeof(x->mvcosts));
343 z->mvcost[0] = &z->mvcosts[0][mv_max+1];
344 z->mvcost[1] = &z->mvcosts[1][mv_max+1];
345 z->mvsadcost[0] = &z->mvsadcosts[0][mvfp_max+1];
346 z->mvsadcost[1] = &z->mvsadcosts[1][mvfp_max+1];
349 vpx_memcpy(z->token_costs, x->token_costs, sizeof(x->token_costs));
350 vpx_memcpy(z->inter_bmode_costs, x->inter_bmode_costs, sizeof(x->inter_bmode_costs));
351 //memcpy(z->mvcosts, x->mvcosts, sizeof(x->mvcosts));
352 //memcpy(z->mvcost, x->mvcost, sizeof(x->mvcost));
353 vpx_memcpy(z->mbmode_cost, x->mbmode_cost, sizeof(x->mbmode_cost));
354 vpx_memcpy(z->intra_uv_mode_cost, x->intra_uv_mode_cost, sizeof(x->intra_uv_mode_cost));
355 vpx_memcpy(z->bmode_costs, x->bmode_costs, sizeof(x->bmode_costs));
357 for (i = 0; i < 25; i++)
359 z->block[i].quant = x->block[i].quant;
360 z->block[i].quant_fast = x->block[i].quant_fast;
361 z->block[i].quant_shift = x->block[i].quant_shift;
362 z->block[i].zbin = x->block[i].zbin;
363 z->block[i].zrun_zbin_boost = x->block[i].zrun_zbin_boost;
364 z->block[i].round = x->block[i].round;
365 z->q_index = x->q_index;
366 z->act_zbin_adj = x->act_zbin_adj;
367 z->last_act_zbin_adj = x->last_act_zbin_adj;
369 z->block[i].src = x->block[i].src;
371 z->block[i].src_stride = x->block[i].src_stride;
375 MACROBLOCKD *xd = &x->e_mbd;
376 MACROBLOCKD *zd = &z->e_mbd;
379 zd->mode_info_context = xd->mode_info_context;
380 zd->mode_info = xd->mode_info;
382 zd->mode_info_stride = xd->mode_info_stride;
383 zd->frame_type = xd->frame_type;
384 zd->up_available = xd->up_available ;
385 zd->left_available = xd->left_available;
386 zd->left_context = xd->left_context;
387 zd->last_frame_dc = xd->last_frame_dc;
388 zd->last_frame_dccons = xd->last_frame_dccons;
389 zd->gold_frame_dc = xd->gold_frame_dc;
390 zd->gold_frame_dccons = xd->gold_frame_dccons;
391 zd->mb_to_left_edge = xd->mb_to_left_edge;
392 zd->mb_to_right_edge = xd->mb_to_right_edge;
393 zd->mb_to_top_edge = xd->mb_to_top_edge ;
394 zd->mb_to_bottom_edge = xd->mb_to_bottom_edge;
395 zd->gf_active_ptr = xd->gf_active_ptr;
396 zd->frames_since_golden = xd->frames_since_golden;
397 zd->frames_till_alt_ref_frame = xd->frames_till_alt_ref_frame;
399 zd->subpixel_predict = xd->subpixel_predict;
400 zd->subpixel_predict8x4 = xd->subpixel_predict8x4;
401 zd->subpixel_predict8x8 = xd->subpixel_predict8x8;
402 zd->subpixel_predict16x16 = xd->subpixel_predict16x16;
403 zd->segmentation_enabled = xd->segmentation_enabled;
404 zd->mb_segement_abs_delta = xd->mb_segement_abs_delta;
405 vpx_memcpy(zd->segment_feature_data, xd->segment_feature_data, sizeof(xd->segment_feature_data));
407 vpx_memcpy(zd->dequant_y1_dc, xd->dequant_y1_dc, sizeof(xd->dequant_y1_dc));
408 vpx_memcpy(zd->dequant_y1, xd->dequant_y1, sizeof(xd->dequant_y1));
409 vpx_memcpy(zd->dequant_y2, xd->dequant_y2, sizeof(xd->dequant_y2));
410 vpx_memcpy(zd->dequant_uv, xd->dequant_uv, sizeof(xd->dequant_uv));
413 /*TODO: Remove dequant from BLOCKD. This is a temporary solution until
414 * the quantizer code uses a passed in pointer to the dequant constants.
415 * This will also require modifications to the x86 and neon assembly.
417 for (i = 0; i < 16; i++)
418 zd->block[i].dequant = zd->dequant_y1;
419 for (i = 16; i < 24; i++)
420 zd->block[i].dequant = zd->dequant_uv;
421 zd->block[24].dequant = zd->dequant_y2;
426 void vp8cx_init_mbrthread_data(VP8_COMP *cpi,
434 VP8_COMMON *const cm = & cpi->common;
435 MACROBLOCKD *const xd = & x->e_mbd;
439 for (i = 0; i < count; i++)
441 MACROBLOCK *mb = & mbr_ei[i].mb;
442 MACROBLOCKD *mbd = &mb->e_mbd;
444 mbd->subpixel_predict = xd->subpixel_predict;
445 mbd->subpixel_predict8x4 = xd->subpixel_predict8x4;
446 mbd->subpixel_predict8x8 = xd->subpixel_predict8x8;
447 mbd->subpixel_predict16x16 = xd->subpixel_predict16x16;
448 mb->gf_active_ptr = x->gf_active_ptr;
450 vpx_memset(mbr_ei[i].segment_counts, 0, sizeof(mbr_ei[i].segment_counts));
451 mbr_ei[i].totalrate = 0;
453 mb->partition_info = x->pi + x->e_mbd.mode_info_stride * (i + 1);
455 mbd->mode_info_context = cm->mi + x->e_mbd.mode_info_stride * (i + 1);
456 mbd->mode_info_stride = cm->mode_info_stride;
458 mbd->frame_type = cm->frame_type;
460 mb->src = * cpi->Source;
461 mbd->pre = cm->yv12_fb[cm->lst_fb_idx];
462 mbd->dst = cm->yv12_fb[cm->new_fb_idx];
464 mb->src.y_buffer += 16 * x->src.y_stride * (i + 1);
465 mb->src.u_buffer += 8 * x->src.uv_stride * (i + 1);
466 mb->src.v_buffer += 8 * x->src.uv_stride * (i + 1);
468 vp8_build_block_offsets(mb);
470 vp8_setup_block_dptrs(mbd);
472 vp8_setup_block_ptrs(mb);
474 mbd->left_context = &cm->left_context;
475 mb->mvc = cm->fc.mvc;
477 setup_mbby_copy(&mbr_ei[i].mb, x);
479 mbd->fullpixel_mask = 0xffffffff;
481 mbd->fullpixel_mask = 0xfffffff8;
485 void vp8cx_create_encoder_threads(VP8_COMP *cpi)
487 const VP8_COMMON * cm = &cpi->common;
489 cpi->b_multi_threaded = 0;
490 cpi->encoding_thread_count = 0;
491 cpi->b_lpf_running = 0;
493 if (cm->processor_core_count > 1 && cpi->oxcf.multi_threaded > 1)
496 int th_count = cpi->oxcf.multi_threaded - 1;
498 /* don't allocate more threads than cores available */
499 if (cpi->oxcf.multi_threaded > cm->processor_core_count)
500 th_count = cm->processor_core_count - 1;
502 /* we have th_count + 1 (main) threads processing one row each */
503 /* no point to have more threads than the sync range allows */
504 if(th_count > ((cm->mb_cols / cpi->mt_sync_range) - 1))
506 th_count = (cm->mb_cols / cpi->mt_sync_range) - 1;
512 CHECK_MEM_ERROR(cpi->h_encoding_thread, vpx_malloc(sizeof(pthread_t) * th_count));
513 CHECK_MEM_ERROR(cpi->h_event_start_encoding, vpx_malloc(sizeof(sem_t) * th_count));
514 CHECK_MEM_ERROR(cpi->mb_row_ei, vpx_memalign(32, sizeof(MB_ROW_COMP) * th_count));
515 vpx_memset(cpi->mb_row_ei, 0, sizeof(MB_ROW_COMP) * th_count);
516 CHECK_MEM_ERROR(cpi->en_thread_data,
517 vpx_malloc(sizeof(ENCODETHREAD_DATA) * th_count));
518 CHECK_MEM_ERROR(cpi->mt_current_mb_col,
519 vpx_malloc(sizeof(*cpi->mt_current_mb_col) * cm->mb_rows));
521 sem_init(&cpi->h_event_end_encoding, 0, 0);
523 cpi->b_multi_threaded = 1;
524 cpi->encoding_thread_count = th_count;
527 printf("[VP8:] multi_threaded encoding is enabled with %d threads\n\n",
528 (cpi->encoding_thread_count +1));
531 for (ithread = 0; ithread < th_count; ithread++)
533 ENCODETHREAD_DATA * ethd = &cpi->en_thread_data[ithread];
535 sem_init(&cpi->h_event_start_encoding[ithread], 0, 0);
536 ethd->ithread = ithread;
537 ethd->ptr1 = (void *)cpi;
538 ethd->ptr2 = (void *)&cpi->mb_row_ei[ithread];
540 pthread_create(&cpi->h_encoding_thread[ithread], 0, thread_encoding_proc, ethd);
544 LPFTHREAD_DATA * lpfthd = &cpi->lpf_thread_data;
546 sem_init(&cpi->h_event_start_lpf, 0, 0);
547 sem_init(&cpi->h_event_end_lpf, 0, 0);
549 lpfthd->ptr1 = (void *)cpi;
550 pthread_create(&cpi->h_filter_thread, 0, thread_loopfilter, lpfthd);
556 void vp8cx_remove_encoder_threads(VP8_COMP *cpi)
558 if (cpi->b_multi_threaded)
560 //shutdown other threads
561 cpi->b_multi_threaded = 0;
565 for (i = 0; i < cpi->encoding_thread_count; i++)
567 //SetEvent(cpi->h_event_mbrencoding[i]);
568 sem_post(&cpi->h_event_start_encoding[i]);
569 pthread_join(cpi->h_encoding_thread[i], 0);
571 sem_destroy(&cpi->h_event_start_encoding[i]);
574 sem_post(&cpi->h_event_start_lpf);
575 pthread_join(cpi->h_filter_thread, 0);
578 sem_destroy(&cpi->h_event_end_encoding);
579 sem_destroy(&cpi->h_event_end_lpf);
580 sem_destroy(&cpi->h_event_start_lpf);
582 //free thread related resources
583 vpx_free(cpi->h_event_start_encoding);
584 vpx_free(cpi->h_encoding_thread);
585 vpx_free(cpi->mb_row_ei);
586 vpx_free(cpi->en_thread_data);
587 vpx_free(cpi->mt_current_mb_col);