Merge "neon fast quantizer updated"
[profile/ivi/libvpx.git] / vp8 / decoder / threading.c
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 #if !defined(WIN32) && CONFIG_OS_SUPPORT == 1
13 # include <unistd.h>
14 #endif
15 #include "onyxd_int.h"
16 #include "vpx_mem/vpx_mem.h"
17 #include "vp8/common/threading.h"
18
19 #include "vp8/common/loopfilter.h"
20 #include "vp8/common/extend.h"
21 #include "vpx_ports/vpx_timer.h"
22 #include "detokenize.h"
23 #include "vp8/common/reconinter.h"
24 #include "reconintra_mt.h"
25
26 extern void mb_init_dequantizer(VP8D_COMP *pbi, MACROBLOCKD *xd);
27 extern void clamp_mvs(MACROBLOCKD *xd);
28 extern void vp8_build_uvmvs(MACROBLOCKD *x, int fullpixel);
29
30 #if CONFIG_RUNTIME_CPU_DETECT
31 #define RTCD_VTABLE(x) (&(pbi)->common.rtcd.x)
32 #else
33 #define RTCD_VTABLE(x) NULL
34 #endif
35
36 static void setup_decoding_thread_data(VP8D_COMP *pbi, MACROBLOCKD *xd, MB_ROW_DEC *mbrd, int count)
37 {
38     VP8_COMMON *const pc = & pbi->common;
39     int i, j;
40
41     for (i = 0; i < count; i++)
42     {
43         MACROBLOCKD *mbd = &mbrd[i].mbd;
44 #if CONFIG_RUNTIME_CPU_DETECT
45         mbd->rtcd = xd->rtcd;
46 #endif
47         mbd->subpixel_predict        = xd->subpixel_predict;
48         mbd->subpixel_predict8x4     = xd->subpixel_predict8x4;
49         mbd->subpixel_predict8x8     = xd->subpixel_predict8x8;
50         mbd->subpixel_predict16x16   = xd->subpixel_predict16x16;
51
52         mbd->mode_info_context = pc->mi   + pc->mode_info_stride * (i + 1);
53         mbd->mode_info_stride  = pc->mode_info_stride;
54
55         mbd->frame_type = pc->frame_type;
56         mbd->frames_since_golden      = pc->frames_since_golden;
57         mbd->frames_till_alt_ref_frame  = pc->frames_till_alt_ref_frame;
58
59         mbd->pre = pc->yv12_fb[pc->lst_fb_idx];
60         mbd->dst = pc->yv12_fb[pc->new_fb_idx];
61
62         vp8_setup_block_dptrs(mbd);
63         vp8_build_block_doffsets(mbd);
64         mbd->segmentation_enabled    = xd->segmentation_enabled;
65         mbd->mb_segement_abs_delta     = xd->mb_segement_abs_delta;
66         vpx_memcpy(mbd->segment_feature_data, xd->segment_feature_data, sizeof(xd->segment_feature_data));
67
68         /*signed char ref_lf_deltas[MAX_REF_LF_DELTAS];*/
69         vpx_memcpy(mbd->ref_lf_deltas, xd->ref_lf_deltas, sizeof(xd->ref_lf_deltas));
70         /*signed char mode_lf_deltas[MAX_MODE_LF_DELTAS];*/
71         vpx_memcpy(mbd->mode_lf_deltas, xd->mode_lf_deltas, sizeof(xd->mode_lf_deltas));
72         /*unsigned char mode_ref_lf_delta_enabled;
73         unsigned char mode_ref_lf_delta_update;*/
74         mbd->mode_ref_lf_delta_enabled    = xd->mode_ref_lf_delta_enabled;
75         mbd->mode_ref_lf_delta_update    = xd->mode_ref_lf_delta_update;
76
77         mbd->current_bc = &pbi->bc2;
78
79         for (j = 0; j < 25; j++)
80         {
81             mbd->block[j].dequant = xd->block[j].dequant;
82         }
83     }
84
85     for (i=0; i< pc->mb_rows; i++)
86         pbi->mt_current_mb_col[i]=-1;
87 }
88
89
90 static void decode_macroblock(VP8D_COMP *pbi, MACROBLOCKD *xd, int mb_row, int mb_col)
91 {
92     int eobtotal = 0;
93     int i, do_clamp = xd->mode_info_context->mbmi.need_to_clamp_mvs;
94     VP8_COMMON *pc = &pbi->common;
95
96     if (xd->mode_info_context->mbmi.mb_skip_coeff)
97     {
98         vp8_reset_mb_tokens_context(xd);
99     }
100     else
101     {
102         eobtotal = vp8_decode_mb_tokens(pbi, xd);
103     }
104
105     /* Perform temporary clamping of the MV to be used for prediction */
106     if (do_clamp)
107     {
108         clamp_mvs(xd);
109     }
110
111     eobtotal |= (xd->mode_info_context->mbmi.mode == B_PRED ||
112                   xd->mode_info_context->mbmi.mode == SPLITMV);
113     if (!eobtotal)
114     {
115         /* Special case:  Force the loopfilter to skip when eobtotal and
116          * mb_skip_coeff are zero.
117          * */
118         xd->mode_info_context->mbmi.mb_skip_coeff = 1;
119
120         /*mt_skip_recon_mb(pbi, xd, mb_row, mb_col);*/
121         if (xd->mode_info_context->mbmi.ref_frame == INTRA_FRAME)
122         {
123             vp8mt_build_intra_predictors_mbuv_s(pbi, xd, mb_row, mb_col);
124             vp8mt_build_intra_predictors_mby_s(pbi, xd, mb_row, mb_col);
125         }
126         else
127         {
128             vp8_build_inter16x16_predictors_mb(xd, xd->dst.y_buffer,
129                                                xd->dst.u_buffer, xd->dst.v_buffer,
130                                                xd->dst.y_stride, xd->dst.uv_stride);
131         }
132         return;
133     }
134
135     if (xd->segmentation_enabled)
136         mb_init_dequantizer(pbi, xd);
137
138     /* do prediction */
139     if (xd->frame_type == KEY_FRAME  ||  xd->mode_info_context->mbmi.ref_frame == INTRA_FRAME)
140     {
141         vp8mt_build_intra_predictors_mbuv(pbi, xd, mb_row, mb_col);
142
143         if (xd->mode_info_context->mbmi.mode != B_PRED)
144         {
145             vp8mt_build_intra_predictors_mby(pbi, xd, mb_row, mb_col);
146         } else {
147             vp8mt_intra_prediction_down_copy(pbi, xd, mb_row, mb_col);
148         }
149     }
150     else
151     {
152         vp8_build_inter_predictors_mb(xd);
153     }
154
155     /* dequantization and idct */
156     if (xd->mode_info_context->mbmi.mode != B_PRED && xd->mode_info_context->mbmi.mode != SPLITMV)
157     {
158         BLOCKD *b = &xd->block[24];
159         DEQUANT_INVOKE(&pbi->dequant, block)(b);
160
161         /* do 2nd order transform on the dc block */
162         if (xd->eobs[24] > 1)
163         {
164             IDCT_INVOKE(RTCD_VTABLE(idct), iwalsh16)(&b->dqcoeff[0], b->diff);
165             ((int *)b->qcoeff)[0] = 0;
166             ((int *)b->qcoeff)[1] = 0;
167             ((int *)b->qcoeff)[2] = 0;
168             ((int *)b->qcoeff)[3] = 0;
169             ((int *)b->qcoeff)[4] = 0;
170             ((int *)b->qcoeff)[5] = 0;
171             ((int *)b->qcoeff)[6] = 0;
172             ((int *)b->qcoeff)[7] = 0;
173         }
174         else
175         {
176             IDCT_INVOKE(RTCD_VTABLE(idct), iwalsh1)(&b->dqcoeff[0], b->diff);
177             ((int *)b->qcoeff)[0] = 0;
178         }
179
180         DEQUANT_INVOKE (&pbi->dequant, dc_idct_add_y_block)
181                         (xd->qcoeff, xd->block[0].dequant,
182                          xd->predictor, xd->dst.y_buffer,
183                          xd->dst.y_stride, xd->eobs, xd->block[24].diff);
184     }
185     else if ((xd->frame_type == KEY_FRAME  ||  xd->mode_info_context->mbmi.ref_frame == INTRA_FRAME) && xd->mode_info_context->mbmi.mode == B_PRED)
186     {
187         for (i = 0; i < 16; i++)
188         {
189             BLOCKD *b = &xd->block[i];
190             vp8mt_predict_intra4x4(pbi, xd, b->bmi.mode, b->predictor, mb_row, mb_col, i);
191
192             if (xd->eobs[i] > 1)
193             {
194                 DEQUANT_INVOKE(&pbi->dequant, idct_add)
195                     (b->qcoeff, b->dequant,  b->predictor,
196                     *(b->base_dst) + b->dst, 16, b->dst_stride);
197             }
198             else
199             {
200                 IDCT_INVOKE(RTCD_VTABLE(idct), idct1_scalar_add)
201                     (b->qcoeff[0] * b->dequant[0], b->predictor,
202                     *(b->base_dst) + b->dst, 16, b->dst_stride);
203                 ((int *)b->qcoeff)[0] = 0;
204             }
205         }
206     }
207     else
208     {
209         DEQUANT_INVOKE (&pbi->dequant, idct_add_y_block)
210                         (xd->qcoeff, xd->block[0].dequant,
211                          xd->predictor, xd->dst.y_buffer,
212                          xd->dst.y_stride, xd->eobs);
213     }
214
215     DEQUANT_INVOKE (&pbi->dequant, idct_add_uv_block)
216                     (xd->qcoeff+16*16, xd->block[16].dequant,
217                      xd->predictor+16*16, xd->dst.u_buffer, xd->dst.v_buffer,
218                      xd->dst.uv_stride, xd->eobs+16);
219 }
220
221
222 static THREAD_FUNCTION thread_decoding_proc(void *p_data)
223 {
224     int ithread = ((DECODETHREAD_DATA *)p_data)->ithread;
225     VP8D_COMP *pbi = (VP8D_COMP *)(((DECODETHREAD_DATA *)p_data)->ptr1);
226     MB_ROW_DEC *mbrd = (MB_ROW_DEC *)(((DECODETHREAD_DATA *)p_data)->ptr2);
227     ENTROPY_CONTEXT_PLANES mb_row_left_context;
228
229     while (1)
230     {
231         if (pbi->b_multithreaded_rd == 0)
232             break;
233
234         /*if(WaitForSingleObject(pbi->h_event_start_decoding[ithread], INFINITE) == WAIT_OBJECT_0)*/
235         if (sem_wait(&pbi->h_event_start_decoding[ithread]) == 0)
236         {
237             if (pbi->b_multithreaded_rd == 0)
238                 break;
239             else
240             {
241                 VP8_COMMON *pc = &pbi->common;
242                 MACROBLOCKD *xd = &mbrd->mbd;
243
244                 int mb_row;
245                 int num_part = 1 << pbi->common.multi_token_partition;
246                 volatile int *last_row_current_mb_col;
247                 int nsync = pbi->sync_range;
248
249                 for (mb_row = ithread+1; mb_row < pc->mb_rows; mb_row += (pbi->decoding_thread_count + 1))
250                 {
251                     int i;
252                     int recon_yoffset, recon_uvoffset;
253                     int mb_col;
254                     int ref_fb_idx = pc->lst_fb_idx;
255                     int dst_fb_idx = pc->new_fb_idx;
256                     int recon_y_stride = pc->yv12_fb[ref_fb_idx].y_stride;
257                     int recon_uv_stride = pc->yv12_fb[ref_fb_idx].uv_stride;
258
259                     int filter_level;
260                     loop_filter_info *lfi = pc->lf_info;
261                     int alt_flt_enabled = xd->segmentation_enabled;
262                     int Segment;
263
264                     pbi->mb_row_di[ithread].mb_row = mb_row;
265                     pbi->mb_row_di[ithread].mbd.current_bc =  &pbi->mbc[mb_row%num_part];
266
267                     last_row_current_mb_col = &pbi->mt_current_mb_col[mb_row -1];
268
269                     recon_yoffset = mb_row * recon_y_stride * 16;
270                     recon_uvoffset = mb_row * recon_uv_stride * 8;
271                     /* reset above block coeffs */
272
273                     xd->above_context = pc->above_context;
274                     xd->left_context = &mb_row_left_context;
275                     vpx_memset(&mb_row_left_context, 0, sizeof(mb_row_left_context));
276                     xd->up_available = (mb_row != 0);
277
278                     xd->mb_to_top_edge = -((mb_row * 16)) << 3;
279                     xd->mb_to_bottom_edge = ((pc->mb_rows - 1 - mb_row) * 16) << 3;
280
281                     for (mb_col = 0; mb_col < pc->mb_cols; mb_col++)
282                     {
283                         if ((mb_col & (nsync-1)) == 0)
284                         {
285                             while (mb_col > (*last_row_current_mb_col - nsync) && *last_row_current_mb_col != pc->mb_cols - 1)
286                             {
287                                 x86_pause_hint();
288                                 thread_sleep(0);
289                             }
290                         }
291
292                         if (xd->mode_info_context->mbmi.mode == SPLITMV || xd->mode_info_context->mbmi.mode == B_PRED)
293                         {
294                             for (i = 0; i < 16; i++)
295                             {
296                                 BLOCKD *d = &xd->block[i];
297                                 vpx_memcpy(&d->bmi, &xd->mode_info_context->bmi[i], sizeof(B_MODE_INFO));
298                             }
299                         }
300
301                         /* Distance of Mb to the various image edges.
302                          * These are specified to 8th pel as they are always compared to values that are in 1/8th pel units
303                          */
304                         xd->mb_to_left_edge = -((mb_col * 16) << 3);
305                         xd->mb_to_right_edge = ((pc->mb_cols - 1 - mb_col) * 16) << 3;
306
307                         xd->dst.y_buffer = pc->yv12_fb[dst_fb_idx].y_buffer + recon_yoffset;
308                         xd->dst.u_buffer = pc->yv12_fb[dst_fb_idx].u_buffer + recon_uvoffset;
309                         xd->dst.v_buffer = pc->yv12_fb[dst_fb_idx].v_buffer + recon_uvoffset;
310
311                         xd->left_available = (mb_col != 0);
312
313                         /* Select the appropriate reference frame for this MB */
314                         if (xd->mode_info_context->mbmi.ref_frame == LAST_FRAME)
315                             ref_fb_idx = pc->lst_fb_idx;
316                         else if (xd->mode_info_context->mbmi.ref_frame == GOLDEN_FRAME)
317                             ref_fb_idx = pc->gld_fb_idx;
318                         else
319                             ref_fb_idx = pc->alt_fb_idx;
320
321                         xd->pre.y_buffer = pc->yv12_fb[ref_fb_idx].y_buffer + recon_yoffset;
322                         xd->pre.u_buffer = pc->yv12_fb[ref_fb_idx].u_buffer + recon_uvoffset;
323                         xd->pre.v_buffer = pc->yv12_fb[ref_fb_idx].v_buffer + recon_uvoffset;
324
325                         vp8_build_uvmvs(xd, pc->full_pixel);
326                         decode_macroblock(pbi, xd, mb_row, mb_col);
327
328                         if (pbi->common.filter_level)
329                         {
330                             int skip_lf;
331                             if( mb_row != pc->mb_rows-1 )
332                             {
333                                 /* Save decoded MB last row data for next-row decoding */
334                                 vpx_memcpy((pbi->mt_yabove_row[mb_row + 1] + 32 + mb_col*16), (xd->dst.y_buffer + 15 * recon_y_stride), 16);
335                                 vpx_memcpy((pbi->mt_uabove_row[mb_row + 1] + 16 + mb_col*8), (xd->dst.u_buffer + 7 * recon_uv_stride), 8);
336                                 vpx_memcpy((pbi->mt_vabove_row[mb_row + 1] + 16 + mb_col*8), (xd->dst.v_buffer + 7 * recon_uv_stride), 8);
337                             }
338
339                             /* save left_col for next MB decoding */
340                             if(mb_col != pc->mb_cols-1)
341                             {
342                                 MODE_INFO *next = xd->mode_info_context +1;
343
344                                 if (xd->frame_type == KEY_FRAME  ||  next->mbmi.ref_frame == INTRA_FRAME)
345                                 {
346                                     for (i = 0; i < 16; i++)
347                                         pbi->mt_yleft_col[mb_row][i] = xd->dst.y_buffer [i* recon_y_stride + 15];
348                                     for (i = 0; i < 8; i++)
349                                     {
350                                         pbi->mt_uleft_col[mb_row][i] = xd->dst.u_buffer [i* recon_uv_stride + 7];
351                                         pbi->mt_vleft_col[mb_row][i] = xd->dst.v_buffer [i* recon_uv_stride + 7];
352                                     }
353                                 }
354                             }
355
356                             /* update loopfilter info */
357                             Segment = (alt_flt_enabled) ? xd->mode_info_context->mbmi.segment_id : 0;
358                             skip_lf = (xd->mode_info_context->mbmi.mode != B_PRED &&
359                                             xd->mode_info_context->mbmi.mode != SPLITMV &&
360                                             xd->mode_info_context->mbmi.mb_skip_coeff);
361
362                             filter_level = pbi->mt_baseline_filter_level[Segment];
363                             /* Distance of Mb to the various image edges.
364                              * These are specified to 8th pel as they are always compared to values that are in 1/8th pel units
365                              * Apply any context driven MB level adjustment
366                              */
367                             filter_level = vp8_adjust_mb_lf_value(xd, filter_level);
368
369                             /* loopfilter on this macroblock. */
370                             if (filter_level)
371                             {
372                                 if (mb_col > 0)
373                                     pc->lf_mbv(xd->dst.y_buffer, xd->dst.u_buffer, xd->dst.v_buffer, recon_y_stride, recon_uv_stride, &lfi[filter_level]);
374
375                                 if (!skip_lf)
376                                     pc->lf_bv(xd->dst.y_buffer, xd->dst.u_buffer, xd->dst.v_buffer, recon_y_stride, recon_uv_stride, &lfi[filter_level]);
377
378                                 /* don't apply across umv border */
379                                 if (mb_row > 0)
380                                     pc->lf_mbh(xd->dst.y_buffer, xd->dst.u_buffer, xd->dst.v_buffer, recon_y_stride, recon_uv_stride, &lfi[filter_level]);
381
382                                 if (!skip_lf)
383                                     pc->lf_bh(xd->dst.y_buffer, xd->dst.u_buffer, xd->dst.v_buffer, recon_y_stride, recon_uv_stride, &lfi[filter_level]);
384                             }
385                         }
386
387                         recon_yoffset += 16;
388                         recon_uvoffset += 8;
389
390                         ++xd->mode_info_context;  /* next mb */
391
392                         xd->above_context++;
393
394                         /*pbi->mb_row_di[ithread].current_mb_col = mb_col;*/
395                         pbi->mt_current_mb_col[mb_row] = mb_col;
396                     }
397
398                     /* adjust to the next row of mbs */
399                     if (pbi->common.filter_level)
400                     {
401                         if(mb_row != pc->mb_rows-1)
402                         {
403                             int lasty = pc->yv12_fb[ref_fb_idx].y_width + VP8BORDERINPIXELS;
404                             int lastuv = (pc->yv12_fb[ref_fb_idx].y_width>>1) + (VP8BORDERINPIXELS>>1);
405
406                             for (i = 0; i < 4; i++)
407                             {
408                                 pbi->mt_yabove_row[mb_row +1][lasty + i] = pbi->mt_yabove_row[mb_row +1][lasty -1];
409                                 pbi->mt_uabove_row[mb_row +1][lastuv + i] = pbi->mt_uabove_row[mb_row +1][lastuv -1];
410                                 pbi->mt_vabove_row[mb_row +1][lastuv + i] = pbi->mt_vabove_row[mb_row +1][lastuv -1];
411                             }
412                         }
413                     } else
414                         vp8_extend_mb_row(&pc->yv12_fb[dst_fb_idx], xd->dst.y_buffer + 16, xd->dst.u_buffer + 8, xd->dst.v_buffer + 8);
415
416                     ++xd->mode_info_context;      /* skip prediction column */
417
418                     /* since we have multithread */
419                     xd->mode_info_context += xd->mode_info_stride * pbi->decoding_thread_count;
420                 }
421             }
422         }
423         /*  add this to each frame */
424         if ((mbrd->mb_row == pbi->common.mb_rows-1) || ((mbrd->mb_row == pbi->common.mb_rows-2) && (pbi->common.mb_rows % (pbi->decoding_thread_count+1))==1))
425         {
426             /*SetEvent(pbi->h_event_end_decoding);*/
427             sem_post(&pbi->h_event_end_decoding);
428         }
429     }
430
431     return 0 ;
432 }
433
434
435 void vp8_decoder_create_threads(VP8D_COMP *pbi)
436 {
437     int core_count = 0;
438     int ithread;
439
440     pbi->b_multithreaded_rd = 0;
441     pbi->allocated_decoding_thread_count = 0;
442
443     /* limit decoding threads to the max number of token partitions */
444     core_count = (pbi->max_threads > 8) ? 8 : pbi->max_threads;
445
446     /* limit decoding threads to the available cores */
447     if (core_count > pbi->common.processor_core_count)
448         core_count = pbi->common.processor_core_count;
449
450     if (core_count > 1)
451     {
452         pbi->b_multithreaded_rd = 1;
453         pbi->decoding_thread_count = core_count - 1;
454
455         CHECK_MEM_ERROR(pbi->h_decoding_thread, vpx_malloc(sizeof(pthread_t) * pbi->decoding_thread_count));
456         CHECK_MEM_ERROR(pbi->h_event_start_decoding, vpx_malloc(sizeof(sem_t) * pbi->decoding_thread_count));
457         CHECK_MEM_ERROR(pbi->mb_row_di, vpx_memalign(32, sizeof(MB_ROW_DEC) * pbi->decoding_thread_count));
458         vpx_memset(pbi->mb_row_di, 0, sizeof(MB_ROW_DEC) * pbi->decoding_thread_count);
459         CHECK_MEM_ERROR(pbi->de_thread_data, vpx_malloc(sizeof(DECODETHREAD_DATA) * pbi->decoding_thread_count));
460
461         for (ithread = 0; ithread < pbi->decoding_thread_count; ithread++)
462         {
463             sem_init(&pbi->h_event_start_decoding[ithread], 0, 0);
464
465             pbi->de_thread_data[ithread].ithread  = ithread;
466             pbi->de_thread_data[ithread].ptr1     = (void *)pbi;
467             pbi->de_thread_data[ithread].ptr2     = (void *) &pbi->mb_row_di[ithread];
468
469             pthread_create(&pbi->h_decoding_thread[ithread], 0, thread_decoding_proc, (&pbi->de_thread_data[ithread]));
470         }
471
472         sem_init(&pbi->h_event_end_decoding, 0, 0);
473
474         pbi->allocated_decoding_thread_count = pbi->decoding_thread_count;
475     }
476 }
477
478
479 void vp8mt_de_alloc_temp_buffers(VP8D_COMP *pbi, int mb_rows)
480 {
481     VP8_COMMON *const pc = & pbi->common;
482     int i;
483
484     if (pbi->b_multithreaded_rd)
485     {
486             vpx_free(pbi->mt_current_mb_col);
487             pbi->mt_current_mb_col = NULL ;
488
489         /* Free above_row buffers. */
490         if (pbi->mt_yabove_row)
491         {
492             for (i=0; i< mb_rows; i++)
493             {
494                     vpx_free(pbi->mt_yabove_row[i]);
495                     pbi->mt_yabove_row[i] = NULL ;
496             }
497             vpx_free(pbi->mt_yabove_row);
498             pbi->mt_yabove_row = NULL ;
499         }
500
501         if (pbi->mt_uabove_row)
502         {
503             for (i=0; i< mb_rows; i++)
504             {
505                     vpx_free(pbi->mt_uabove_row[i]);
506                     pbi->mt_uabove_row[i] = NULL ;
507             }
508             vpx_free(pbi->mt_uabove_row);
509             pbi->mt_uabove_row = NULL ;
510         }
511
512         if (pbi->mt_vabove_row)
513         {
514             for (i=0; i< mb_rows; i++)
515             {
516                     vpx_free(pbi->mt_vabove_row[i]);
517                     pbi->mt_vabove_row[i] = NULL ;
518             }
519             vpx_free(pbi->mt_vabove_row);
520             pbi->mt_vabove_row = NULL ;
521         }
522
523         /* Free left_col buffers. */
524         if (pbi->mt_yleft_col)
525         {
526             for (i=0; i< mb_rows; i++)
527             {
528                     vpx_free(pbi->mt_yleft_col[i]);
529                     pbi->mt_yleft_col[i] = NULL ;
530             }
531             vpx_free(pbi->mt_yleft_col);
532             pbi->mt_yleft_col = NULL ;
533         }
534
535         if (pbi->mt_uleft_col)
536         {
537             for (i=0; i< mb_rows; i++)
538             {
539                     vpx_free(pbi->mt_uleft_col[i]);
540                     pbi->mt_uleft_col[i] = NULL ;
541             }
542             vpx_free(pbi->mt_uleft_col);
543             pbi->mt_uleft_col = NULL ;
544         }
545
546         if (pbi->mt_vleft_col)
547         {
548             for (i=0; i< mb_rows; i++)
549             {
550                     vpx_free(pbi->mt_vleft_col[i]);
551                     pbi->mt_vleft_col[i] = NULL ;
552             }
553             vpx_free(pbi->mt_vleft_col);
554             pbi->mt_vleft_col = NULL ;
555         }
556     }
557 }
558
559
560 void vp8mt_alloc_temp_buffers(VP8D_COMP *pbi, int width, int prev_mb_rows)
561 {
562     VP8_COMMON *const pc = & pbi->common;
563     int i;
564     int uv_width;
565
566     if (pbi->b_multithreaded_rd)
567     {
568         vp8mt_de_alloc_temp_buffers(pbi, prev_mb_rows);
569
570         /* our internal buffers are always multiples of 16 */
571         if ((width & 0xf) != 0)
572             width += 16 - (width & 0xf);
573
574         if (width < 640) pbi->sync_range = 1;
575         else if (width <= 1280) pbi->sync_range = 8;
576         else if (width <= 2560) pbi->sync_range =16;
577         else pbi->sync_range = 32;
578
579         uv_width = width >>1;
580
581         /* Allocate an int for each mb row. */
582         CHECK_MEM_ERROR(pbi->mt_current_mb_col, vpx_malloc(sizeof(int) * pc->mb_rows));
583
584         /* Allocate memory for above_row buffers. */
585         CHECK_MEM_ERROR(pbi->mt_yabove_row, vpx_malloc(sizeof(unsigned char *) * pc->mb_rows));
586         for (i=0; i< pc->mb_rows; i++)
587             CHECK_MEM_ERROR(pbi->mt_yabove_row[i], vpx_calloc(sizeof(unsigned char) * (width + (VP8BORDERINPIXELS<<1)), 1));
588
589         CHECK_MEM_ERROR(pbi->mt_uabove_row, vpx_malloc(sizeof(unsigned char *) * pc->mb_rows));
590         for (i=0; i< pc->mb_rows; i++)
591             CHECK_MEM_ERROR(pbi->mt_uabove_row[i], vpx_calloc(sizeof(unsigned char) * (uv_width + VP8BORDERINPIXELS), 1));
592
593         CHECK_MEM_ERROR(pbi->mt_vabove_row, vpx_malloc(sizeof(unsigned char *) * pc->mb_rows));
594         for (i=0; i< pc->mb_rows; i++)
595             CHECK_MEM_ERROR(pbi->mt_vabove_row[i], vpx_calloc(sizeof(unsigned char) * (uv_width + VP8BORDERINPIXELS), 1));
596
597         /* Allocate memory for left_col buffers. */
598         CHECK_MEM_ERROR(pbi->mt_yleft_col, vpx_malloc(sizeof(unsigned char *) * pc->mb_rows));
599         for (i=0; i< pc->mb_rows; i++)
600             CHECK_MEM_ERROR(pbi->mt_yleft_col[i], vpx_calloc(sizeof(unsigned char) * 16, 1));
601
602         CHECK_MEM_ERROR(pbi->mt_uleft_col, vpx_malloc(sizeof(unsigned char *) * pc->mb_rows));
603         for (i=0; i< pc->mb_rows; i++)
604             CHECK_MEM_ERROR(pbi->mt_uleft_col[i], vpx_calloc(sizeof(unsigned char) * 8, 1));
605
606         CHECK_MEM_ERROR(pbi->mt_vleft_col, vpx_malloc(sizeof(unsigned char *) * pc->mb_rows));
607         for (i=0; i< pc->mb_rows; i++)
608             CHECK_MEM_ERROR(pbi->mt_vleft_col[i], vpx_calloc(sizeof(unsigned char) * 8, 1));
609     }
610 }
611
612
613 void vp8_decoder_remove_threads(VP8D_COMP *pbi)
614 {
615     /* shutdown MB Decoding thread; */
616     if (pbi->b_multithreaded_rd)
617     {
618         int i;
619
620         pbi->b_multithreaded_rd = 0;
621
622         /* allow all threads to exit */
623         for (i = 0; i < pbi->allocated_decoding_thread_count; i++)
624         {
625             sem_post(&pbi->h_event_start_decoding[i]);
626             pthread_join(pbi->h_decoding_thread[i], NULL);
627         }
628
629         for (i = 0; i < pbi->allocated_decoding_thread_count; i++)
630         {
631             sem_destroy(&pbi->h_event_start_decoding[i]);
632         }
633
634         sem_destroy(&pbi->h_event_end_decoding);
635
636             vpx_free(pbi->h_decoding_thread);
637             pbi->h_decoding_thread = NULL;
638
639             vpx_free(pbi->h_event_start_decoding);
640             pbi->h_event_start_decoding = NULL;
641
642             vpx_free(pbi->mb_row_di);
643             pbi->mb_row_di = NULL ;
644
645             vpx_free(pbi->de_thread_data);
646             pbi->de_thread_data = NULL;
647     }
648 }
649
650
651 static void lpf_init( VP8D_COMP *pbi, int default_filt_lvl)
652 {
653     VP8_COMMON *cm  = &pbi->common;
654     MACROBLOCKD *mbd = &pbi->mb;
655     /*YV12_BUFFER_CONFIG *post = &cm->new_frame;*/  /*frame_to_show;*/
656     loop_filter_info *lfi = cm->lf_info;
657     FRAME_TYPE frame_type = cm->frame_type;
658
659     /*int mb_row;
660     int mb_col;
661     int baseline_filter_level[MAX_MB_SEGMENTS];*/
662     int alt_flt_enabled = mbd->segmentation_enabled;
663
664     int i;
665     /*unsigned char *y_ptr, *u_ptr, *v_ptr;*/
666
667     /* Note the baseline filter values for each segment */
668     if (alt_flt_enabled)
669     {
670         for (i = 0; i < MAX_MB_SEGMENTS; i++)
671         {
672             /* Abs value */
673             if (mbd->mb_segement_abs_delta == SEGMENT_ABSDATA)
674                 pbi->mt_baseline_filter_level[i] = mbd->segment_feature_data[MB_LVL_ALT_LF][i];
675             /* Delta Value */
676             else
677             {
678                 pbi->mt_baseline_filter_level[i] = default_filt_lvl + mbd->segment_feature_data[MB_LVL_ALT_LF][i];
679                 pbi->mt_baseline_filter_level[i] = (pbi->mt_baseline_filter_level[i] >= 0) ? ((pbi->mt_baseline_filter_level[i] <= MAX_LOOP_FILTER) ? pbi->mt_baseline_filter_level[i] : MAX_LOOP_FILTER) : 0;  /* Clamp to valid range */
680             }
681         }
682     }
683     else
684     {
685         for (i = 0; i < MAX_MB_SEGMENTS; i++)
686             pbi->mt_baseline_filter_level[i] = default_filt_lvl;
687     }
688
689     /* Initialize the loop filter for this frame. */
690     if ((cm->last_filter_type != cm->filter_type) || (cm->last_sharpness_level != cm->sharpness_level))
691         vp8_init_loop_filter(cm);
692     else if (frame_type != cm->last_frame_type)
693         vp8_frame_init_loop_filter(lfi, frame_type);
694 }
695
696
697 void vp8mt_decode_mb_rows( VP8D_COMP *pbi, MACROBLOCKD *xd)
698 {
699     int mb_row;
700     VP8_COMMON *pc = &pbi->common;
701
702     int ibc = 0;
703     int num_part = 1 << pbi->common.multi_token_partition;
704     int i;
705     volatile int *last_row_current_mb_col = NULL;
706     int nsync = pbi->sync_range;
707
708     int filter_level;
709     loop_filter_info *lfi = pc->lf_info;
710     int alt_flt_enabled = xd->segmentation_enabled;
711     int Segment;
712
713     if(pbi->common.filter_level)
714     {
715         /* Set above_row buffer to 127 for decoding first MB row */
716         vpx_memset(pbi->mt_yabove_row[0] + VP8BORDERINPIXELS-1, 127, pc->yv12_fb[pc->lst_fb_idx].y_width + 5);
717         vpx_memset(pbi->mt_uabove_row[0] + (VP8BORDERINPIXELS>>1)-1, 127, (pc->yv12_fb[pc->lst_fb_idx].y_width>>1) +5);
718         vpx_memset(pbi->mt_vabove_row[0] + (VP8BORDERINPIXELS>>1)-1, 127, (pc->yv12_fb[pc->lst_fb_idx].y_width>>1) +5);
719
720         for (i=1; i<pc->mb_rows; i++)
721         {
722             vpx_memset(pbi->mt_yabove_row[i] + VP8BORDERINPIXELS-1, (unsigned char)129, 1);
723             vpx_memset(pbi->mt_uabove_row[i] + (VP8BORDERINPIXELS>>1)-1, (unsigned char)129, 1);
724             vpx_memset(pbi->mt_vabove_row[i] + (VP8BORDERINPIXELS>>1)-1, (unsigned char)129, 1);
725         }
726
727         /* Set left_col to 129 initially */
728         for (i=0; i<pc->mb_rows; i++)
729         {
730             vpx_memset(pbi->mt_yleft_col[i], (unsigned char)129, 16);
731             vpx_memset(pbi->mt_uleft_col[i], (unsigned char)129, 8);
732             vpx_memset(pbi->mt_vleft_col[i], (unsigned char)129, 8);
733         }
734         lpf_init(pbi, pc->filter_level);
735     }
736
737     setup_decoding_thread_data(pbi, xd, pbi->mb_row_di, pbi->decoding_thread_count);
738
739     for (i = 0; i < pbi->decoding_thread_count; i++)
740         sem_post(&pbi->h_event_start_decoding[i]);
741
742     for (mb_row = 0; mb_row < pc->mb_rows; mb_row += (pbi->decoding_thread_count + 1))
743     {
744
745         xd->current_bc = &pbi->mbc[mb_row%num_part];
746
747         /* vp8_decode_mb_row(pbi, pc, mb_row, xd); */
748         {
749             int i;
750             int recon_yoffset, recon_uvoffset;
751             int mb_col;
752             int ref_fb_idx = pc->lst_fb_idx;
753             int dst_fb_idx = pc->new_fb_idx;
754             int recon_y_stride = pc->yv12_fb[ref_fb_idx].y_stride;
755             int recon_uv_stride = pc->yv12_fb[ref_fb_idx].uv_stride;
756
757            /* volatile int *last_row_current_mb_col = NULL; */
758             if (mb_row > 0)
759                 last_row_current_mb_col = &pbi->mt_current_mb_col[mb_row -1];
760
761             vpx_memset(&pc->left_context, 0, sizeof(pc->left_context));
762             recon_yoffset = mb_row * recon_y_stride * 16;
763             recon_uvoffset = mb_row * recon_uv_stride * 8;
764             /* reset above block coeffs */
765
766             xd->above_context = pc->above_context;
767             xd->up_available = (mb_row != 0);
768
769             xd->mb_to_top_edge = -((mb_row * 16)) << 3;
770             xd->mb_to_bottom_edge = ((pc->mb_rows - 1 - mb_row) * 16) << 3;
771
772             for (mb_col = 0; mb_col < pc->mb_cols; mb_col++)
773             {
774                 if ( mb_row > 0 && (mb_col & (nsync-1)) == 0){
775                     while (mb_col > (*last_row_current_mb_col - nsync) && *last_row_current_mb_col != pc->mb_cols - 1)
776                     {
777                         x86_pause_hint();
778                         thread_sleep(0);
779                     }
780                 }
781
782                 if (xd->mode_info_context->mbmi.mode == SPLITMV || xd->mode_info_context->mbmi.mode == B_PRED)
783                 {
784                     for (i = 0; i < 16; i++)
785                     {
786                         BLOCKD *d = &xd->block[i];
787                         vpx_memcpy(&d->bmi, &xd->mode_info_context->bmi[i], sizeof(B_MODE_INFO));
788                     }
789                 }
790
791                 /* Distance of Mb to the various image edges.
792                  * These are specified to 8th pel as they are always compared to values that are in 1/8th pel units
793                  */
794                 xd->mb_to_left_edge = -((mb_col * 16) << 3);
795                 xd->mb_to_right_edge = ((pc->mb_cols - 1 - mb_col) * 16) << 3;
796
797                 xd->dst.y_buffer = pc->yv12_fb[dst_fb_idx].y_buffer + recon_yoffset;
798                 xd->dst.u_buffer = pc->yv12_fb[dst_fb_idx].u_buffer + recon_uvoffset;
799                 xd->dst.v_buffer = pc->yv12_fb[dst_fb_idx].v_buffer + recon_uvoffset;
800
801                 xd->left_available = (mb_col != 0);
802
803                 /* Select the appropriate reference frame for this MB */
804                 if (xd->mode_info_context->mbmi.ref_frame == LAST_FRAME)
805                     ref_fb_idx = pc->lst_fb_idx;
806                 else if (xd->mode_info_context->mbmi.ref_frame == GOLDEN_FRAME)
807                     ref_fb_idx = pc->gld_fb_idx;
808                 else
809                     ref_fb_idx = pc->alt_fb_idx;
810
811                 xd->pre.y_buffer = pc->yv12_fb[ref_fb_idx].y_buffer + recon_yoffset;
812                 xd->pre.u_buffer = pc->yv12_fb[ref_fb_idx].u_buffer + recon_uvoffset;
813                 xd->pre.v_buffer = pc->yv12_fb[ref_fb_idx].v_buffer + recon_uvoffset;
814
815                 if (xd->mode_info_context->mbmi.ref_frame != INTRA_FRAME)
816                 {
817                     /* propagate errors from reference frames */
818                     xd->corrupted |= pc->yv12_fb[ref_fb_idx].corrupted;
819                 }
820
821                 vp8_build_uvmvs(xd, pc->full_pixel);
822                 decode_macroblock(pbi, xd, mb_row, mb_col);
823
824                 /* check if the boolean decoder has suffered an error */
825                 xd->corrupted |= vp8dx_bool_error(xd->current_bc);
826
827                 if (pbi->common.filter_level)
828                 {
829                     int skip_lf;
830                     /* Save decoded MB last row data for next-row decoding */
831                     if(mb_row != pc->mb_rows-1)
832                     {
833                         vpx_memcpy((pbi->mt_yabove_row[mb_row +1] + 32 + mb_col*16), (xd->dst.y_buffer + 15 * recon_y_stride), 16);
834                         vpx_memcpy((pbi->mt_uabove_row[mb_row +1] + 16 + mb_col*8), (xd->dst.u_buffer + 7 * recon_uv_stride), 8);
835                         vpx_memcpy((pbi->mt_vabove_row[mb_row +1] + 16 + mb_col*8), (xd->dst.v_buffer + 7 * recon_uv_stride), 8);
836                     }
837
838                     /* save left_col for next MB decoding */
839                     if(mb_col != pc->mb_cols-1)
840                     {
841                         MODE_INFO *next = xd->mode_info_context +1;
842
843                         if (xd->frame_type == KEY_FRAME  ||  next->mbmi.ref_frame == INTRA_FRAME)
844                         {
845                             for (i = 0; i < 16; i++)
846                                 pbi->mt_yleft_col[mb_row][i] = xd->dst.y_buffer [i* recon_y_stride + 15];
847                             for (i = 0; i < 8; i++)
848                             {
849                                 pbi->mt_uleft_col[mb_row][i] = xd->dst.u_buffer [i* recon_uv_stride + 7];
850                                 pbi->mt_vleft_col[mb_row][i] = xd->dst.v_buffer [i* recon_uv_stride + 7];
851                             }
852                         }
853                     }
854
855                     /* update loopfilter info */
856                     Segment = (alt_flt_enabled) ? xd->mode_info_context->mbmi.segment_id : 0;
857                     skip_lf = (xd->mode_info_context->mbmi.mode != B_PRED &&
858                                     xd->mode_info_context->mbmi.mode != SPLITMV &&
859                                     xd->mode_info_context->mbmi.mb_skip_coeff);
860                     filter_level = pbi->mt_baseline_filter_level[Segment];
861                     /* Distance of Mb to the various image edges.
862                      * These are specified to 8th pel as they are always compared to values that are in 1/8th pel units
863                      * Apply any context driven MB level adjustment
864                      */
865                     filter_level = vp8_adjust_mb_lf_value(xd, filter_level);
866
867                     /* loopfilter on this macroblock. */
868                     if (filter_level)
869                     {
870                         if (mb_col > 0)
871                             pc->lf_mbv(xd->dst.y_buffer, xd->dst.u_buffer, xd->dst.v_buffer, recon_y_stride, recon_uv_stride, &lfi[filter_level]);
872
873                         if (!skip_lf)
874                             pc->lf_bv(xd->dst.y_buffer, xd->dst.u_buffer, xd->dst.v_buffer, recon_y_stride, recon_uv_stride, &lfi[filter_level]);
875
876                         /* don't apply across umv border */
877                         if (mb_row > 0)
878                             pc->lf_mbh(xd->dst.y_buffer, xd->dst.u_buffer, xd->dst.v_buffer, recon_y_stride, recon_uv_stride, &lfi[filter_level]);
879
880                         if (!skip_lf)
881                             pc->lf_bh(xd->dst.y_buffer, xd->dst.u_buffer, xd->dst.v_buffer, recon_y_stride, recon_uv_stride, &lfi[filter_level]);
882                     }
883                 }
884
885                 recon_yoffset += 16;
886                 recon_uvoffset += 8;
887
888                 ++xd->mode_info_context;  /* next mb */
889
890                 xd->above_context++;
891
892                 pbi->mt_current_mb_col[mb_row] = mb_col;
893             }
894
895             /* adjust to the next row of mbs */
896             if (pbi->common.filter_level)
897             {
898                 if(mb_row != pc->mb_rows-1)
899                 {
900                     int lasty = pc->yv12_fb[ref_fb_idx].y_width + VP8BORDERINPIXELS;
901                     int lastuv = (pc->yv12_fb[ref_fb_idx].y_width>>1) + (VP8BORDERINPIXELS>>1);
902
903                     for (i = 0; i < 4; i++)
904                     {
905                         pbi->mt_yabove_row[mb_row +1][lasty + i] = pbi->mt_yabove_row[mb_row +1][lasty -1];
906                         pbi->mt_uabove_row[mb_row +1][lastuv + i] = pbi->mt_uabove_row[mb_row +1][lastuv -1];
907                         pbi->mt_vabove_row[mb_row +1][lastuv + i] = pbi->mt_vabove_row[mb_row +1][lastuv -1];
908                     }
909                 }
910             }else
911                 vp8_extend_mb_row(&pc->yv12_fb[dst_fb_idx], xd->dst.y_buffer + 16, xd->dst.u_buffer + 8, xd->dst.v_buffer + 8);
912
913             ++xd->mode_info_context;      /* skip prediction column */
914         }
915         xd->mode_info_context += xd->mode_info_stride * pbi->decoding_thread_count;
916     }
917
918     sem_wait(&pbi->h_event_end_decoding);   /* add back for each frame */
919 }