Initial WebM release
[profile/ivi/libvpx.git] / vp8 / encoder / ethreading.c
1 /*
2  *  Copyright (c) 2010 The VP8 project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license and patent
5  *  grant that can be found in the LICENSE file in the root of the source
6  *  tree. All contributing project authors may be found in the AUTHORS
7  *  file in the root of the source tree.
8  */
9
10
11 #include "onyx_int.h"
12 #include "threading.h"
13 #include "common.h"
14 #include "extend.h"
15
16
17 extern int vp8cx_encode_inter_macroblock(VP8_COMP *cpi, MACROBLOCK *x, TOKENEXTRA **t, int recon_yoffset, int recon_uvoffset);
18 extern int vp8cx_encode_intra_macro_block(VP8_COMP *cpi, MACROBLOCK *x, TOKENEXTRA **t);
19 extern void vp8cx_mb_init_quantizer(VP8_COMP *cpi, MACROBLOCK *x);
20 extern void vp8_build_block_offsets(MACROBLOCK *x);
21 extern void vp8_setup_block_ptrs(MACROBLOCK *x);
22
23 static
24 THREAD_FUNCTION thread_encoding_proc(void *p_data)
25 {
26 #if CONFIG_MULTITHREAD
27     int ithread = ((ENCODETHREAD_DATA *)p_data)->ithread;
28     VP8_COMP *cpi   = (VP8_COMP *)(((ENCODETHREAD_DATA *)p_data)->ptr1);
29     MB_ROW_COMP *mbri = (MB_ROW_COMP *)(((ENCODETHREAD_DATA *)p_data)->ptr2);
30     ENTROPY_CONTEXT mb_row_left_context[4][4];
31
32     //printf("Started thread %d\n", ithread);
33
34     while (1)
35     {
36         if (cpi->b_multi_threaded == 0)
37             break;
38
39         //if(WaitForSingleObject(cpi->h_event_mbrencoding[ithread], INFINITE) == WAIT_OBJECT_0)
40         if (sem_wait(&cpi->h_event_mbrencoding[ithread]) == 0)
41         {
42             if (cpi->b_multi_threaded == FALSE) // we're shutting down
43                 break;
44             else
45             {
46                 VP8_COMMON *cm      = &cpi->common;
47                 int mb_row           = mbri->mb_row;
48                 MACROBLOCK  *x      = &mbri->mb;
49                 MACROBLOCKD *xd     = &x->e_mbd;
50                 TOKENEXTRA **tp     = &mbri->tp;
51                 int *segment_counts  = mbri->segment_counts;
52                 int *totalrate      = &mbri->totalrate;
53
54                 {
55                     int i;
56                     int recon_yoffset, recon_uvoffset;
57                     int mb_col;
58                     int recon_y_stride = cm->last_frame.y_stride;
59                     int recon_uv_stride = cm->last_frame.uv_stride;
60                     volatile int *last_row_current_mb_col;
61
62                     if (ithread > 0)
63                         last_row_current_mb_col = &cpi->mb_row_ei[ithread-1].current_mb_col;
64                     else
65                         last_row_current_mb_col = &cpi->current_mb_col_main;
66
67                     // reset above block coeffs
68                     xd->above_context[Y1CONTEXT] = cm->above_context[Y1CONTEXT];
69                     xd->above_context[UCONTEXT ] = cm->above_context[UCONTEXT ];
70                     xd->above_context[VCONTEXT ] = cm->above_context[VCONTEXT ];
71                     xd->above_context[Y2CONTEXT] = cm->above_context[Y2CONTEXT];
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->new_frame.y_buffer + recon_yoffset;
110                         xd->dst.u_buffer = cm->new_frame.u_buffer + recon_uvoffset;
111                         xd->dst.v_buffer = cm->new_frame.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->mbmi.segment_id = cpi->segmentation_map[seg_map_index+mb_col];
121                             else
122                                 xd->mbmi.segment_id = 0;
123
124                             vp8cx_mb_init_quantizer(cpi, x);
125                         }
126                         else
127                             xd->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[xd->mbmi.partition_bmi[b].mode] ++;
151                                 }
152                             }
153
154 #endif
155
156                             // Count of last ref frame 0,0 useage
157                             if ((xd->mbmi.mode == ZEROMV) && (xd->mbmi.ref_frame == LAST_FRAME))
158                                 cpi->inter_zz_count ++;
159
160                         }
161
162                         cpi->tplist[mb_row].stop = *tp;
163
164                         xd->gf_active_ptr++;      // Increment pointer into gf useage flags structure for next mb
165
166                         // store macroblock mode info into context array
167                         vpx_memcpy(&xd->mode_info_context->mbmi, &xd->mbmi, sizeof(xd->mbmi));
168
169                         for (i = 0; i < 16; i++)
170                             vpx_memcpy(&xd->mode_info_context->bmi[i], &xd->block[i].bmi, sizeof(xd->block[i].bmi));
171
172                         // adjust to the next column of macroblocks
173                         x->src.y_buffer += 16;
174                         x->src.u_buffer += 8;
175                         x->src.v_buffer += 8;
176
177                         recon_yoffset += 16;
178                         recon_uvoffset += 8;
179
180                         // Keep track of segment useage
181                         segment_counts[xd->mbmi.segment_id] ++;
182
183                         // skip to next mb
184                         xd->mode_info_context++;
185
186                         xd->above_context[Y1CONTEXT] += 4;
187                         xd->above_context[UCONTEXT ] += 2;
188                         xd->above_context[VCONTEXT ] += 2;
189                         xd->above_context[Y2CONTEXT] ++;
190
191                         cpi->mb_row_ei[ithread].current_mb_col = mb_col;
192
193                     }
194
195                     //extend the recon for intra prediction
196                     vp8_extend_mb_row(
197                         &cm->new_frame,
198                         xd->dst.y_buffer + 16,
199                         xd->dst.u_buffer + 8,
200                         xd->dst.v_buffer + 8);
201
202                     // this is to account for the border
203                     xd->mode_info_context++;
204
205                     x->src.y_buffer += 16 * x->src.y_stride * (cpi->encoding_thread_count + 1) - 16 * cm->mb_cols;
206                     x->src.u_buffer +=  8 * x->src.uv_stride * (cpi->encoding_thread_count + 1) - 8 * cm->mb_cols;
207                     x->src.v_buffer +=  8 * x->src.uv_stride * (cpi->encoding_thread_count + 1) - 8 * cm->mb_cols;
208
209                     xd->mode_info_context += xd->mode_info_stride * cpi->encoding_thread_count;
210
211                     if (ithread == (cpi->encoding_thread_count - 1) || mb_row == cm->mb_rows - 1)
212                     {
213                         //SetEvent(cpi->h_event_main);
214                         sem_post(&cpi->h_event_main);
215                     }
216
217                 }
218
219             }
220         }
221     }
222
223 #else
224     (void) p_data;
225 #endif
226
227     //printf("exit thread %d\n", ithread);
228     return 0;
229 }
230
231 static void setup_mbby_copy(MACROBLOCK *mbdst, MACROBLOCK *mbsrc)
232 {
233
234     MACROBLOCK *x = mbsrc;
235     MACROBLOCK *z = mbdst;
236     int i;
237
238     z->ss               = x->ss;
239     z->ss_count          = x->ss_count;
240     z->searches_per_step  = x->searches_per_step;
241     z->errorperbit      = x->errorperbit;
242
243     z->sadperbit16      = x->sadperbit16;
244     z->sadperbit4       = x->sadperbit4;
245     z->errthresh        = x->errthresh;
246     z->rddiv            = x->rddiv;
247     z->rdmult           = x->rdmult;
248
249     /*
250     z->mv_col_min    = x->mv_col_min;
251     z->mv_col_max    = x->mv_col_max;
252     z->mv_row_min    = x->mv_row_min;
253     z->mv_row_max    = x->mv_row_max;
254     z->vector_range = x->vector_range ;
255     */
256
257     z->vp8_short_fdct4x4     = x->vp8_short_fdct4x4;
258     z->vp8_short_fdct8x4     = x->vp8_short_fdct8x4;
259     z->short_fdct4x4rd   = x->short_fdct4x4rd;
260     z->short_fdct8x4rd   = x->short_fdct8x4rd;
261     z->short_fdct8x4rd   = x->short_fdct8x4rd;
262     z->vp8_short_fdct4x4_ptr = x->vp8_short_fdct4x4_ptr;
263     z->short_walsh4x4    = x->short_walsh4x4;
264     z->quantize_b        = x->quantize_b;
265     z->quantize_brd      = x->quantize_brd;
266
267     /*
268     z->mvc              = x->mvc;
269     z->src.y_buffer      = x->src.y_buffer;
270     z->src.u_buffer      = x->src.u_buffer;
271     z->src.v_buffer      = x->src.v_buffer;
272     */
273
274
275     vpx_memcpy(z->mvcosts,          x->mvcosts,         sizeof(x->mvcosts));
276     z->mvcost[0] = &z->mvcosts[0][mv_max+1];
277     z->mvcost[1] = &z->mvcosts[1][mv_max+1];
278     z->mvsadcost[0] = &z->mvsadcosts[0][mv_max+1];
279     z->mvsadcost[1] = &z->mvsadcosts[1][mv_max+1];
280
281
282     vpx_memcpy(z->token_costs,       x->token_costs,      sizeof(x->token_costs));
283     vpx_memcpy(z->inter_bmode_costs,  x->inter_bmode_costs, sizeof(x->inter_bmode_costs));
284     //memcpy(z->mvcosts,            x->mvcosts,         sizeof(x->mvcosts));
285     //memcpy(z->mvcost,         x->mvcost,          sizeof(x->mvcost));
286     vpx_memcpy(z->mbmode_cost,       x->mbmode_cost,      sizeof(x->mbmode_cost));
287     vpx_memcpy(z->intra_uv_mode_cost,  x->intra_uv_mode_cost, sizeof(x->intra_uv_mode_cost));
288     vpx_memcpy(z->bmode_costs,       x->bmode_costs,      sizeof(x->bmode_costs));
289
290     for (i = 0; i < 25; i++)
291     {
292         z->block[i].quant           = x->block[i].quant;
293         z->block[i].zbin            = x->block[i].zbin;
294         z->block[i].zrun_zbin_boost   = x->block[i].zrun_zbin_boost;
295         z->block[i].round           = x->block[i].round;
296         /*
297         z->block[i].src             = x->block[i].src;
298         */
299         z->block[i].src_stride       = x->block[i].src_stride;
300         z->block[i].force_empty      = x->block[i].force_empty;
301
302     }
303
304     {
305         MACROBLOCKD *xd = &x->e_mbd;
306         MACROBLOCKD *zd = &z->e_mbd;
307
308         /*
309         zd->mode_info_context = xd->mode_info_context;
310         zd->mode_info        = xd->mode_info;
311
312         zd->mode_info_stride  = xd->mode_info_stride;
313         zd->frame_type       = xd->frame_type;
314         zd->up_available     = xd->up_available   ;
315         zd->left_available   = xd->left_available;
316         zd->left_context     = xd->left_context;
317         zd->last_frame_dc     = xd->last_frame_dc;
318         zd->last_frame_dccons = xd->last_frame_dccons;
319         zd->gold_frame_dc     = xd->gold_frame_dc;
320         zd->gold_frame_dccons = xd->gold_frame_dccons;
321         zd->mb_to_left_edge    = xd->mb_to_left_edge;
322         zd->mb_to_right_edge   = xd->mb_to_right_edge;
323         zd->mb_to_top_edge     = xd->mb_to_top_edge   ;
324         zd->mb_to_bottom_edge  = xd->mb_to_bottom_edge;
325         zd->gf_active_ptr     = xd->gf_active_ptr;
326         zd->frames_since_golden       = xd->frames_since_golden;
327         zd->frames_till_alt_ref_frame   = xd->frames_till_alt_ref_frame;
328         */
329         zd->subpixel_predict         = xd->subpixel_predict;
330         zd->subpixel_predict8x4      = xd->subpixel_predict8x4;
331         zd->subpixel_predict8x8      = xd->subpixel_predict8x8;
332         zd->subpixel_predict16x16    = xd->subpixel_predict16x16;
333         zd->segmentation_enabled     = xd->segmentation_enabled;
334         zd->mb_segement_abs_delta      = xd->mb_segement_abs_delta;
335         vpx_memcpy(zd->segment_feature_data, xd->segment_feature_data, sizeof(xd->segment_feature_data));
336
337         /*
338         memcpy(zd->above_context,        xd->above_context, sizeof(xd->above_context));
339         memcpy(zd->mb_segment_tree_probs,  xd->mb_segment_tree_probs, sizeof(xd->mb_segment_tree_probs));
340         memcpy(zd->segment_feature_data,  xd->segment_feature_data, sizeof(xd->segment_feature_data));
341         */
342         for (i = 0; i < 25; i++)
343         {
344             zd->block[i].dequant = xd->block[i].dequant;
345         }
346     }
347 }
348
349
350 void vp8cx_init_mbrthread_data(VP8_COMP *cpi,
351                                MACROBLOCK *x,
352                                MB_ROW_COMP *mbr_ei,
353                                int mb_row,
354                                int count
355                               )
356 {
357
358     VP8_COMMON *const cm = & cpi->common;
359     MACROBLOCKD *const xd = & x->e_mbd;
360     int i;
361     (void) mb_row;
362
363     for (i = 0; i < count; i++)
364     {
365         MACROBLOCK *mb = & mbr_ei[i].mb;
366         MACROBLOCKD *mbd = &mb->e_mbd;
367
368         mbd->subpixel_predict        = xd->subpixel_predict;
369         mbd->subpixel_predict8x4     = xd->subpixel_predict8x4;
370         mbd->subpixel_predict8x8     = xd->subpixel_predict8x8;
371         mbd->subpixel_predict16x16   = xd->subpixel_predict16x16;
372 #if CONFIG_RUNTIME_CPU_DETECT
373         mbd->rtcd                   = xd->rtcd;
374 #endif
375         mbd->gf_active_ptr            = xd->gf_active_ptr;
376
377         mb->vector_range             = 32;
378
379         vpx_memset(mbr_ei[i].segment_counts, 0, sizeof(mbr_ei[i].segment_counts));
380         mbr_ei[i].totalrate = 0;
381
382         mbd->mode_info        = cm->mi - 1;
383         mbd->mode_info_context = cm->mi   + x->e_mbd.mode_info_stride * (i + 1);
384         mbd->mode_info_stride  = cm->mode_info_stride;
385
386         mbd->frame_type = cm->frame_type;
387
388         mbd->frames_since_golden = cm->frames_since_golden;
389         mbd->frames_till_alt_ref_frame = cm->frames_till_alt_ref_frame;
390
391         mb->src = * cpi->Source;
392         mbd->pre = cm->last_frame;
393         mbd->dst = cm->new_frame;
394
395         mb->src.y_buffer += 16 * x->src.y_stride * (i + 1);
396         mb->src.u_buffer +=  8 * x->src.uv_stride * (i + 1);
397         mb->src.v_buffer +=  8 * x->src.uv_stride * (i + 1);
398
399
400         vp8_build_block_offsets(mb);
401
402         vp8_setup_block_dptrs(mbd);
403
404         vp8_setup_block_ptrs(mb);
405
406         mb->rddiv = cpi->RDDIV;
407         mb->rdmult = cpi->RDMULT;
408
409         mbd->mbmi.mode = DC_PRED;
410         mbd->mbmi.uv_mode = DC_PRED;
411
412         mbd->left_context = cm->left_context;
413         mb->mvc = cm->fc.mvc;
414
415         setup_mbby_copy(&mbr_ei[i].mb, x);
416
417     }
418 }
419
420
421 void vp8cx_create_encoder_threads(VP8_COMP *cpi)
422 {
423     cpi->b_multi_threaded = 0;
424
425     cpi->processor_core_count = 32; //vp8_get_proc_core_count();
426
427     CHECK_MEM_ERROR(cpi->tplist, vpx_malloc(sizeof(TOKENLIST) * cpi->common.mb_rows));
428
429 #if CONFIG_MULTITHREAD
430
431     if (cpi->processor_core_count > 1 && cpi->oxcf.multi_threaded > 1)
432     {
433         int ithread;
434
435         if (cpi->oxcf.multi_threaded > cpi->processor_core_count)
436             cpi->encoding_thread_count = cpi->processor_core_count - 1;
437         else
438             cpi->encoding_thread_count = cpi->oxcf.multi_threaded - 1;
439
440
441         CHECK_MEM_ERROR(cpi->h_encoding_thread, vpx_malloc(sizeof(pthread_t) * cpi->encoding_thread_count));
442         CHECK_MEM_ERROR(cpi->h_event_mbrencoding, vpx_malloc(sizeof(sem_t) * cpi->encoding_thread_count));
443         CHECK_MEM_ERROR(cpi->mb_row_ei, vpx_memalign(32, sizeof(MB_ROW_COMP) * cpi->encoding_thread_count));
444         vpx_memset(cpi->mb_row_ei, 0, sizeof(MB_ROW_COMP) * cpi->encoding_thread_count);
445         CHECK_MEM_ERROR(cpi->en_thread_data, vpx_malloc(sizeof(ENCODETHREAD_DATA) * cpi->encoding_thread_count));
446         //cpi->h_event_main = CreateEvent(NULL, FALSE, FALSE, NULL);
447         sem_init(&cpi->h_event_main, 0, 0);
448
449         cpi->b_multi_threaded = 1;
450
451         //printf("[VP8:] multi_threaded encoding is enabled with %d threads\n\n", (cpi->encoding_thread_count +1));
452
453         for (ithread = 0; ithread < cpi->encoding_thread_count; ithread++)
454         {
455             //cpi->h_event_mbrencoding[ithread] = CreateEvent(NULL, FALSE, FALSE, NULL);
456             sem_init(&cpi->h_event_mbrencoding[ithread], 0, 0);
457             cpi->en_thread_data[ithread].ithread = ithread;
458             cpi->en_thread_data[ithread].ptr1 = (void *)cpi;
459             cpi->en_thread_data[ithread].ptr2 = (void *)&cpi->mb_row_ei[ithread];
460
461             //printf(" call begin thread %d \n", ithread);
462
463             //cpi->h_encoding_thread[ithread] =   (HANDLE)_beginthreadex(
464             //  NULL,           // security
465             //  0,              // stksize
466             //  thread_encoding_proc,
467             //  (&cpi->en_thread_data[ithread]),          // Thread data
468             //  0,
469             //  NULL);
470
471             pthread_create(&cpi->h_encoding_thread[ithread], 0, thread_encoding_proc, (&cpi->en_thread_data[ithread]));
472
473         }
474
475     }
476
477 #endif
478 }
479
480 void vp8cx_remove_encoder_threads(VP8_COMP *cpi)
481 {
482 #if CONFIG_MULTITHREAD
483
484     if (cpi->b_multi_threaded)
485     {
486         //shutdown other threads
487         cpi->b_multi_threaded = 0;
488         {
489             int i;
490
491             for (i = 0; i < cpi->encoding_thread_count; i++)
492             {
493                 //SetEvent(cpi->h_event_mbrencoding[i]);
494                 sem_post(&cpi->h_event_mbrencoding[i]);
495                 pthread_join(cpi->h_encoding_thread[i], 0);
496             }
497
498             for (i = 0; i < cpi->encoding_thread_count; i++)
499                 sem_destroy(&cpi->h_event_mbrencoding[i]);
500         }
501         //free thread related resources
502         vpx_free(cpi->h_event_mbrencoding);
503         vpx_free(cpi->h_encoding_thread);
504         vpx_free(cpi->mb_row_ei);
505         vpx_free(cpi->en_thread_data);
506     }
507
508 #endif
509     vpx_free(cpi->tplist);
510 }