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