Removed unnecessary MB_MODE_INFO copies
[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 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[Y1CONTEXT] = cm->above_context[Y1CONTEXT];
72                     xd->above_context[UCONTEXT ] = cm->above_context[UCONTEXT ];
73                     xd->above_context[VCONTEXT ] = cm->above_context[VCONTEXT ];
74                     xd->above_context[Y2CONTEXT] = cm->above_context[Y2CONTEXT];
75                     xd->left_context = mb_row_left_context;
76
77                     vp8_zero(mb_row_left_context);
78
79                     xd->up_available = (mb_row != 0);
80                     recon_yoffset = (mb_row * recon_y_stride * 16);
81                     recon_uvoffset = (mb_row * recon_uv_stride * 8);
82
83
84                     cpi->tplist[mb_row].start = *tp;
85
86                     //printf("Thread mb_row = %d\n", mb_row);
87
88                     // for each macroblock col in image
89                     for (mb_col = 0; mb_col < cm->mb_cols; mb_col++)
90                     {
91                         int seg_map_index = (mb_row * cm->mb_cols);
92
93                         while (mb_col > (*last_row_current_mb_col - 1) && *last_row_current_mb_col != cm->mb_cols - 1)
94                         {
95                             x86_pause_hint();
96                             thread_sleep(0);
97                         }
98
99                         // Distance of Mb to the various image edges.
100                         // These specified to 8th pel as they are always compared to values that are in 1/8th pel units
101                         xd->mb_to_left_edge = -((mb_col * 16) << 3);
102                         xd->mb_to_right_edge = ((cm->mb_cols - 1 - mb_col) * 16) << 3;
103                         xd->mb_to_top_edge = -((mb_row * 16) << 3);
104                         xd->mb_to_bottom_edge = ((cm->mb_rows - 1 - mb_row) * 16) << 3;
105
106                         // Set up limit values for motion vectors used to prevent them extending outside the UMV borders
107                         x->mv_col_min = -((mb_col * 16) + (VP8BORDERINPIXELS - 16));
108                         x->mv_col_max = ((cm->mb_cols - 1 - mb_col) * 16) + (VP8BORDERINPIXELS - 16);
109                         x->mv_row_min = -((mb_row * 16) + (VP8BORDERINPIXELS - 16));
110                         x->mv_row_max = ((cm->mb_rows - 1 - mb_row) * 16) + (VP8BORDERINPIXELS - 16);
111
112                         xd->dst.y_buffer = cm->yv12_fb[dst_fb_idx].y_buffer + recon_yoffset;
113                         xd->dst.u_buffer = cm->yv12_fb[dst_fb_idx].u_buffer + recon_uvoffset;
114                         xd->dst.v_buffer = cm->yv12_fb[dst_fb_idx].v_buffer + recon_uvoffset;
115                         xd->left_available = (mb_col != 0);
116
117                         // Is segmentation enabled
118                         // MB level adjutment to quantizer
119                         if (xd->segmentation_enabled)
120                         {
121                             // Code to set segment id in xd->mbmi.segment_id for current MB (with range checking)
122                             if (cpi->segmentation_map[seg_map_index+mb_col] <= 3)
123                                 xd->mode_info_context->mbmi.segment_id = cpi->segmentation_map[seg_map_index+mb_col];
124                             else
125                                 xd->mode_info_context->mbmi.segment_id = 0;
126
127                             vp8cx_mb_init_quantizer(cpi, x);
128                         }
129                         else
130                             xd->mode_info_context->mbmi.segment_id = 0;         // Set to Segment 0 by default
131
132
133                         if (cm->frame_type == KEY_FRAME)
134                         {
135                             *totalrate += vp8cx_encode_intra_macro_block(cpi, x, tp);
136 #ifdef MODE_STATS
137                             y_modes[xd->mbmi.mode] ++;
138 #endif
139                         }
140                         else
141                         {
142                             *totalrate += vp8cx_encode_inter_macroblock(cpi, x, tp, recon_yoffset, recon_uvoffset);
143
144 #ifdef MODE_STATS
145                             inter_y_modes[xd->mbmi.mode] ++;
146
147                             if (xd->mbmi.mode == SPLITMV)
148                             {
149                                 int b;
150
151                                 for (b = 0; b < xd->mbmi.partition_count; b++)
152                                 {
153                                     inter_b_modes[xd->mbmi.partition_bmi[b].mode] ++;
154                                 }
155                             }
156
157 #endif
158
159                             // Count of last ref frame 0,0 useage
160                             if ((xd->mode_info_context->mbmi.mode == ZEROMV) && (xd->mode_info_context->mbmi.ref_frame == LAST_FRAME))
161                                 cpi->inter_zz_count ++;
162
163                         }
164
165                         cpi->tplist[mb_row].stop = *tp;
166
167                         x->gf_active_ptr++;      // Increment pointer into gf useage flags structure for next mb
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->mode_info_context->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->yv12_fb[dst_fb_idx],
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_walsh4x4    = x->short_walsh4x4;
260     z->quantize_b        = x->quantize_b;
261
262     /*
263     z->mvc              = x->mvc;
264     z->src.y_buffer      = x->src.y_buffer;
265     z->src.u_buffer      = x->src.u_buffer;
266     z->src.v_buffer      = x->src.v_buffer;
267     */
268
269
270     vpx_memcpy(z->mvcosts,          x->mvcosts,         sizeof(x->mvcosts));
271     z->mvcost[0] = &z->mvcosts[0][mv_max+1];
272     z->mvcost[1] = &z->mvcosts[1][mv_max+1];
273     z->mvsadcost[0] = &z->mvsadcosts[0][mv_max+1];
274     z->mvsadcost[1] = &z->mvsadcosts[1][mv_max+1];
275
276
277     vpx_memcpy(z->token_costs,       x->token_costs,      sizeof(x->token_costs));
278     vpx_memcpy(z->inter_bmode_costs,  x->inter_bmode_costs, sizeof(x->inter_bmode_costs));
279     //memcpy(z->mvcosts,            x->mvcosts,         sizeof(x->mvcosts));
280     //memcpy(z->mvcost,         x->mvcost,          sizeof(x->mvcost));
281     vpx_memcpy(z->mbmode_cost,       x->mbmode_cost,      sizeof(x->mbmode_cost));
282     vpx_memcpy(z->intra_uv_mode_cost,  x->intra_uv_mode_cost, sizeof(x->intra_uv_mode_cost));
283     vpx_memcpy(z->bmode_costs,       x->bmode_costs,      sizeof(x->bmode_costs));
284
285     for (i = 0; i < 25; i++)
286     {
287         z->block[i].quant           = x->block[i].quant;
288         z->block[i].quant_shift     = x->block[i].quant_shift;
289         z->block[i].zbin            = x->block[i].zbin;
290         z->block[i].zrun_zbin_boost   = x->block[i].zrun_zbin_boost;
291         z->block[i].round           = x->block[i].round;
292         /*
293         z->block[i].src             = x->block[i].src;
294         */
295         z->block[i].src_stride       = x->block[i].src_stride;
296         z->block[i].force_empty      = x->block[i].force_empty;
297
298     }
299
300     {
301         MACROBLOCKD *xd = &x->e_mbd;
302         MACROBLOCKD *zd = &z->e_mbd;
303
304         /*
305         zd->mode_info_context = xd->mode_info_context;
306         zd->mode_info        = xd->mode_info;
307
308         zd->mode_info_stride  = xd->mode_info_stride;
309         zd->frame_type       = xd->frame_type;
310         zd->up_available     = xd->up_available   ;
311         zd->left_available   = xd->left_available;
312         zd->left_context     = xd->left_context;
313         zd->last_frame_dc     = xd->last_frame_dc;
314         zd->last_frame_dccons = xd->last_frame_dccons;
315         zd->gold_frame_dc     = xd->gold_frame_dc;
316         zd->gold_frame_dccons = xd->gold_frame_dccons;
317         zd->mb_to_left_edge    = xd->mb_to_left_edge;
318         zd->mb_to_right_edge   = xd->mb_to_right_edge;
319         zd->mb_to_top_edge     = xd->mb_to_top_edge   ;
320         zd->mb_to_bottom_edge  = xd->mb_to_bottom_edge;
321         zd->gf_active_ptr     = xd->gf_active_ptr;
322         zd->frames_since_golden       = xd->frames_since_golden;
323         zd->frames_till_alt_ref_frame   = xd->frames_till_alt_ref_frame;
324         */
325         zd->subpixel_predict         = xd->subpixel_predict;
326         zd->subpixel_predict8x4      = xd->subpixel_predict8x4;
327         zd->subpixel_predict8x8      = xd->subpixel_predict8x8;
328         zd->subpixel_predict16x16    = xd->subpixel_predict16x16;
329         zd->segmentation_enabled     = xd->segmentation_enabled;
330         zd->mb_segement_abs_delta      = xd->mb_segement_abs_delta;
331         vpx_memcpy(zd->segment_feature_data, xd->segment_feature_data, sizeof(xd->segment_feature_data));
332
333         /*
334         memcpy(zd->above_context,        xd->above_context, sizeof(xd->above_context));
335         memcpy(zd->mb_segment_tree_probs,  xd->mb_segment_tree_probs, sizeof(xd->mb_segment_tree_probs));
336         memcpy(zd->segment_feature_data,  xd->segment_feature_data, sizeof(xd->segment_feature_data));
337         */
338         for (i = 0; i < 25; i++)
339         {
340             zd->block[i].dequant = xd->block[i].dequant;
341         }
342     }
343 }
344
345
346 void vp8cx_init_mbrthread_data(VP8_COMP *cpi,
347                                MACROBLOCK *x,
348                                MB_ROW_COMP *mbr_ei,
349                                int mb_row,
350                                int count
351                               )
352 {
353
354     VP8_COMMON *const cm = & cpi->common;
355     MACROBLOCKD *const xd = & x->e_mbd;
356     int i;
357     (void) mb_row;
358
359     for (i = 0; i < count; i++)
360     {
361         MACROBLOCK *mb = & mbr_ei[i].mb;
362         MACROBLOCKD *mbd = &mb->e_mbd;
363
364         mbd->subpixel_predict        = xd->subpixel_predict;
365         mbd->subpixel_predict8x4     = xd->subpixel_predict8x4;
366         mbd->subpixel_predict8x8     = xd->subpixel_predict8x8;
367         mbd->subpixel_predict16x16   = xd->subpixel_predict16x16;
368 #if CONFIG_RUNTIME_CPU_DETECT
369         mbd->rtcd                   = xd->rtcd;
370 #endif
371         mb->gf_active_ptr            = x->gf_active_ptr;
372
373         mb->vector_range             = 32;
374
375         vpx_memset(mbr_ei[i].segment_counts, 0, sizeof(mbr_ei[i].segment_counts));
376         mbr_ei[i].totalrate = 0;
377
378         mbd->mode_info        = cm->mi - 1;
379         mbd->mode_info_context = cm->mi   + x->e_mbd.mode_info_stride * (i + 1);
380         mbd->mode_info_stride  = cm->mode_info_stride;
381
382         mbd->frame_type = cm->frame_type;
383
384         mbd->frames_since_golden = cm->frames_since_golden;
385         mbd->frames_till_alt_ref_frame = cm->frames_till_alt_ref_frame;
386
387         mb->src = * cpi->Source;
388         mbd->pre = cm->yv12_fb[cm->lst_fb_idx];
389         mbd->dst = cm->yv12_fb[cm->new_fb_idx];
390
391         mb->src.y_buffer += 16 * x->src.y_stride * (i + 1);
392         mb->src.u_buffer +=  8 * x->src.uv_stride * (i + 1);
393         mb->src.v_buffer +=  8 * x->src.uv_stride * (i + 1);
394
395
396         vp8_build_block_offsets(mb);
397
398         vp8_setup_block_dptrs(mbd);
399
400         vp8_setup_block_ptrs(mb);
401
402         mb->rddiv = cpi->RDDIV;
403         mb->rdmult = cpi->RDMULT;
404
405         mbd->left_context = cm->left_context;
406         mb->mvc = cm->fc.mvc;
407
408         setup_mbby_copy(&mbr_ei[i].mb, x);
409
410     }
411 }
412
413
414 void vp8cx_create_encoder_threads(VP8_COMP *cpi)
415 {
416     cpi->b_multi_threaded = 0;
417
418     cpi->processor_core_count = 32; //vp8_get_proc_core_count();
419
420     CHECK_MEM_ERROR(cpi->tplist, vpx_malloc(sizeof(TOKENLIST) * cpi->common.mb_rows));
421
422 #if CONFIG_MULTITHREAD
423
424     if (cpi->processor_core_count > 1 && cpi->oxcf.multi_threaded > 1)
425     {
426         int ithread;
427
428         if (cpi->oxcf.multi_threaded > cpi->processor_core_count)
429             cpi->encoding_thread_count = cpi->processor_core_count - 1;
430         else
431             cpi->encoding_thread_count = cpi->oxcf.multi_threaded - 1;
432
433
434         CHECK_MEM_ERROR(cpi->h_encoding_thread, vpx_malloc(sizeof(pthread_t) * cpi->encoding_thread_count));
435         CHECK_MEM_ERROR(cpi->h_event_mbrencoding, vpx_malloc(sizeof(sem_t) * cpi->encoding_thread_count));
436         CHECK_MEM_ERROR(cpi->mb_row_ei, vpx_memalign(32, sizeof(MB_ROW_COMP) * cpi->encoding_thread_count));
437         vpx_memset(cpi->mb_row_ei, 0, sizeof(MB_ROW_COMP) * cpi->encoding_thread_count);
438         CHECK_MEM_ERROR(cpi->en_thread_data, vpx_malloc(sizeof(ENCODETHREAD_DATA) * cpi->encoding_thread_count));
439         //cpi->h_event_main = CreateEvent(NULL, FALSE, FALSE, NULL);
440         sem_init(&cpi->h_event_main, 0, 0);
441
442         cpi->b_multi_threaded = 1;
443
444         //printf("[VP8:] multi_threaded encoding is enabled with %d threads\n\n", (cpi->encoding_thread_count +1));
445
446         for (ithread = 0; ithread < cpi->encoding_thread_count; ithread++)
447         {
448             //cpi->h_event_mbrencoding[ithread] = CreateEvent(NULL, FALSE, FALSE, NULL);
449             sem_init(&cpi->h_event_mbrencoding[ithread], 0, 0);
450             cpi->en_thread_data[ithread].ithread = ithread;
451             cpi->en_thread_data[ithread].ptr1 = (void *)cpi;
452             cpi->en_thread_data[ithread].ptr2 = (void *)&cpi->mb_row_ei[ithread];
453
454             //printf(" call begin thread %d \n", ithread);
455
456             //cpi->h_encoding_thread[ithread] =   (HANDLE)_beginthreadex(
457             //  NULL,           // security
458             //  0,              // stksize
459             //  thread_encoding_proc,
460             //  (&cpi->en_thread_data[ithread]),          // Thread data
461             //  0,
462             //  NULL);
463
464             pthread_create(&cpi->h_encoding_thread[ithread], 0, thread_encoding_proc, (&cpi->en_thread_data[ithread]));
465
466         }
467
468     }
469
470 #endif
471 }
472
473 void vp8cx_remove_encoder_threads(VP8_COMP *cpi)
474 {
475 #if CONFIG_MULTITHREAD
476
477     if (cpi->b_multi_threaded)
478     {
479         //shutdown other threads
480         cpi->b_multi_threaded = 0;
481         {
482             int i;
483
484             for (i = 0; i < cpi->encoding_thread_count; i++)
485             {
486                 //SetEvent(cpi->h_event_mbrencoding[i]);
487                 sem_post(&cpi->h_event_mbrencoding[i]);
488                 pthread_join(cpi->h_encoding_thread[i], 0);
489             }
490
491             for (i = 0; i < cpi->encoding_thread_count; i++)
492                 sem_destroy(&cpi->h_event_mbrencoding[i]);
493         }
494         //free thread related resources
495         vpx_free(cpi->h_event_mbrencoding);
496         vpx_free(cpi->h_encoding_thread);
497         vpx_free(cpi->mb_row_ei);
498         vpx_free(cpi->en_thread_data);
499     }
500
501 #endif
502     vpx_free(cpi->tplist);
503 }