threading.c 30.1 KB
Newer Older
John Koleszar's avatar
John Koleszar committed
1
2
3
/*
 *  Copyright (c) 2010 The VP8 project authors. All Rights Reserved.
 *
4
 *  Use of this source code is governed by a BSD-style license
5
6
 *  that can be found in the LICENSE file in the root of the source
 *  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.
John Koleszar's avatar
John Koleszar committed
9
10
11
12
13
14
 */


#ifndef WIN32
# include <unistd.h>
#endif
15
16
17
#ifdef __APPLE__
#include <mach/mach_init.h>
#endif
John Koleszar's avatar
John Koleszar committed
18
19
20
21
22
23
24
25
#include "onyxd_int.h"
#include "vpx_mem/vpx_mem.h"
#include "threading.h"

#include "loopfilter.h"
#include "extend.h"
#include "vpx_ports/vpx_timer.h"

26
27
#define MAX_ROWS 256

John Koleszar's avatar
John Koleszar committed
28
29
30
31
32
33
34
35
extern void vp8_decode_mb_row(VP8D_COMP *pbi,
                              VP8_COMMON *pc,
                              int mb_row,
                              MACROBLOCKD *xd);

extern void vp8_build_uvmvs(MACROBLOCKD *x, int fullpixel);
extern void vp8_decode_macroblock(VP8D_COMP *pbi, MACROBLOCKD *xd);

36
37
void vp8_thread_loop_filter(VP8D_COMP *pbi, MB_ROW_DEC *mbrd, int ithread);

John Koleszar's avatar
John Koleszar committed
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
void vp8_setup_decoding_thread_data(VP8D_COMP *pbi, MACROBLOCKD *xd, MB_ROW_DEC *mbrd, int count)
{
#if CONFIG_MULTITHREAD
    VP8_COMMON *const pc = & pbi->common;
    int i, j;

    for (i = 0; i < count; i++)
    {
        MACROBLOCKD *mbd = &mbrd[i].mbd;
#if CONFIG_RUNTIME_CPU_DETECT
        mbd->rtcd = xd->rtcd;
#endif
        mbd->subpixel_predict        = xd->subpixel_predict;
        mbd->subpixel_predict8x4     = xd->subpixel_predict8x4;
        mbd->subpixel_predict8x8     = xd->subpixel_predict8x8;
        mbd->subpixel_predict16x16   = xd->subpixel_predict16x16;

        mbd->mode_info        = pc->mi - 1;
        mbd->mode_info_context = pc->mi   + pc->mode_info_stride * (i + 1);
        mbd->mode_info_stride  = pc->mode_info_stride;

        mbd->frame_type = pc->frame_type;
        mbd->frames_since_golden      = pc->frames_since_golden;
        mbd->frames_till_alt_ref_frame  = pc->frames_till_alt_ref_frame;

63
64
        mbd->pre = pc->yv12_fb[pc->lst_fb_idx];
        mbd->dst = pc->yv12_fb[pc->new_fb_idx];
John Koleszar's avatar
John Koleszar committed
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82

        vp8_setup_block_dptrs(mbd);
        vp8_build_block_doffsets(mbd);
        mbd->segmentation_enabled    = xd->segmentation_enabled;
        mbd->mb_segement_abs_delta     = xd->mb_segement_abs_delta;
        vpx_memcpy(mbd->segment_feature_data, xd->segment_feature_data, sizeof(xd->segment_feature_data));

        mbd->mbmi.mode = DC_PRED;
        mbd->mbmi.uv_mode = DC_PRED;

        mbd->current_bc = &pbi->bc2;

        for (j = 0; j < 25; j++)
        {
            mbd->block[j].dequant = xd->block[j].dequant;
        }
    }

83
84
    for (i=0; i< pc->mb_rows; i++)
        pbi->current_mb_col[i]=-1;
John Koleszar's avatar
John Koleszar committed
85
86
87
88
89
90
91
92
#else
    (void) pbi;
    (void) xd;
    (void) mbrd;
    (void) count;
#endif
}

93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
void vp8_setup_loop_filter_thread_data(VP8D_COMP *pbi, MACROBLOCKD *xd, MB_ROW_DEC *mbrd, int count)
{
#if CONFIG_MULTITHREAD
    VP8_COMMON *const pc = & pbi->common;
    int i, j;

    for (i = 0; i < count; i++)
    {
        MACROBLOCKD *mbd = &mbrd[i].mbd;
//#if CONFIG_RUNTIME_CPU_DETECT
//        mbd->rtcd = xd->rtcd;
//#endif

        //mbd->subpixel_predict        = xd->subpixel_predict;
        //mbd->subpixel_predict8x4     = xd->subpixel_predict8x4;
        //mbd->subpixel_predict8x8     = xd->subpixel_predict8x8;
        //mbd->subpixel_predict16x16   = xd->subpixel_predict16x16;

        mbd->mode_info        = pc->mi - 1;
        mbd->mode_info_context = pc->mi   + pc->mode_info_stride * (i + 1);
        mbd->mode_info_stride  = pc->mode_info_stride;

        //mbd->frame_type = pc->frame_type;
        //mbd->frames_since_golden      = pc->frames_since_golden;
        //mbd->frames_till_alt_ref_frame  = pc->frames_till_alt_ref_frame;

        //mbd->pre = pc->yv12_fb[pc->lst_fb_idx];
        //mbd->dst = pc->yv12_fb[pc->new_fb_idx];

        //vp8_setup_block_dptrs(mbd);
        //vp8_build_block_doffsets(mbd);
        mbd->segmentation_enabled    = xd->segmentation_enabled;  //
        mbd->mb_segement_abs_delta     = xd->mb_segement_abs_delta;  //
        vpx_memcpy(mbd->segment_feature_data, xd->segment_feature_data, sizeof(xd->segment_feature_data));   //

        //signed char ref_lf_deltas[MAX_REF_LF_DELTAS];
        vpx_memcpy(mbd->ref_lf_deltas, xd->ref_lf_deltas, sizeof(xd->ref_lf_deltas));
        //signed char mode_lf_deltas[MAX_MODE_LF_DELTAS];
        vpx_memcpy(mbd->mode_lf_deltas, xd->mode_lf_deltas, sizeof(xd->mode_lf_deltas));
        //unsigned char mode_ref_lf_delta_enabled;
        //unsigned char mode_ref_lf_delta_update;
        mbd->mode_ref_lf_delta_enabled    = xd->mode_ref_lf_delta_enabled;
        mbd->mode_ref_lf_delta_update    = xd->mode_ref_lf_delta_update;

        //mbd->mbmi.mode = DC_PRED;
        //mbd->mbmi.uv_mode = DC_PRED;
        //mbd->current_bc = &pbi->bc2;

        //for (j = 0; j < 25; j++)
        //{
        //    mbd->block[j].dequant = xd->block[j].dequant;
        //}
    }

    for (i=0; i< pc->mb_rows; i++)
        pbi->current_mb_col[i]=-1;
#else
    (void) pbi;
    (void) xd;
    (void) mbrd;
    (void) count;
#endif
}
John Koleszar's avatar
John Koleszar committed
156
157
158
159
160
161
162
163
164
165
166
167
168
169

THREAD_FUNCTION vp8_thread_decoding_proc(void *p_data)
{
#if CONFIG_MULTITHREAD
    int ithread = ((DECODETHREAD_DATA *)p_data)->ithread;
    VP8D_COMP *pbi = (VP8D_COMP *)(((DECODETHREAD_DATA *)p_data)->ptr1);
    MB_ROW_DEC *mbrd = (MB_ROW_DEC *)(((DECODETHREAD_DATA *)p_data)->ptr2);
    ENTROPY_CONTEXT mb_row_left_context[4][4];

    while (1)
    {
        if (pbi->b_multithreaded_rd == 0)
            break;

170
171
        //if(WaitForSingleObject(pbi->h_event_start_decoding[ithread], INFINITE) == WAIT_OBJECT_0)
        if (sem_wait(&pbi->h_event_start_decoding[ithread]) == 0)
John Koleszar's avatar
John Koleszar committed
172
173
174
175
176
177
178
179
        {
            if (pbi->b_multithreaded_rd == 0)
                break;
            else
            {
                VP8_COMMON *pc = &pbi->common;
                MACROBLOCKD *xd = &mbrd->mbd;

180
181
                int mb_row;
                int num_part = 1 << pbi->common.multi_token_partition;
John Koleszar's avatar
John Koleszar committed
182
183
                volatile int *last_row_current_mb_col;

184
                for (mb_row = ithread+1; mb_row < pc->mb_rows; mb_row += (pbi->decoding_thread_count + 1))
John Koleszar's avatar
John Koleszar committed
185
                {
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
                    int i;
                    int recon_yoffset, recon_uvoffset;
                    int mb_col;
                    int ref_fb_idx = pc->lst_fb_idx;
                    int dst_fb_idx = pc->new_fb_idx;
                    int recon_y_stride = pc->yv12_fb[ref_fb_idx].y_stride;
                    int recon_uv_stride = pc->yv12_fb[ref_fb_idx].uv_stride;

                    pbi->mb_row_di[ithread].mb_row = mb_row;
                    pbi->mb_row_di[ithread].mbd.current_bc =  &pbi->mbc[mb_row%num_part];

                    last_row_current_mb_col = &pbi->current_mb_col[mb_row -1];

                    recon_yoffset = mb_row * recon_y_stride * 16;
                    recon_uvoffset = mb_row * recon_uv_stride * 8;
                    // reset above block coeffs

                    xd->above_context[Y1CONTEXT] = pc->above_context[Y1CONTEXT];
                    xd->above_context[UCONTEXT ] = pc->above_context[UCONTEXT];
                    xd->above_context[VCONTEXT ] = pc->above_context[VCONTEXT];
                    xd->above_context[Y2CONTEXT] = pc->above_context[Y2CONTEXT];
                    xd->left_context = mb_row_left_context;
                    vpx_memset(mb_row_left_context, 0, sizeof(mb_row_left_context));
                    xd->up_available = (mb_row != 0);

                    xd->mb_to_top_edge = -((mb_row * 16)) << 3;
                    xd->mb_to_bottom_edge = ((pc->mb_rows - 1 - mb_row) * 16) << 3;

                    for (mb_col = 0; mb_col < pc->mb_cols; mb_col++)
John Koleszar's avatar
John Koleszar committed
215
                    {
216
217
218
219
220
221
222
223
                        if ((mb_col & 7) == 0)
                        {
                            while (mb_col > (*last_row_current_mb_col - 8) && *last_row_current_mb_col != pc->mb_cols - 1)
                            {
                                x86_pause_hint();
                                thread_sleep(0);
                            }
                        }
John Koleszar's avatar
John Koleszar committed
224

225
226
227
228
                        // Take a copy of the mode and Mv information for this macroblock into the xd->mbmi
                        // the partition_bmi array is unused in the decoder, so don't copy it.
                        vpx_memcpy(&xd->mbmi, &xd->mode_info_context->mbmi,
                                   sizeof(MB_MODE_INFO) - sizeof(xd->mbmi.partition_bmi));
John Koleszar's avatar
John Koleszar committed
229

230
                        if (xd->mbmi.mode == SPLITMV || xd->mbmi.mode == B_PRED)
John Koleszar's avatar
John Koleszar committed
231
                        {
232
233
234
235
236
                            for (i = 0; i < 16; i++)
                            {
                                BLOCKD *d = &xd->block[i];
                                vpx_memcpy(&d->bmi, &xd->mode_info_context->bmi[i], sizeof(B_MODE_INFO));
                            }
John Koleszar's avatar
John Koleszar committed
237
238
                        }

239
240
241
242
                        // Distance of Mb to the various image edges.
                        // These specified to 8th pel as they are always compared to values that are in 1/8th pel units
                        xd->mb_to_left_edge = -((mb_col * 16) << 3);
                        xd->mb_to_right_edge = ((pc->mb_cols - 1 - mb_col) * 16) << 3;
John Koleszar's avatar
John Koleszar committed
243

244
245
246
                        xd->dst.y_buffer = pc->yv12_fb[dst_fb_idx].y_buffer + recon_yoffset;
                        xd->dst.u_buffer = pc->yv12_fb[dst_fb_idx].u_buffer + recon_uvoffset;
                        xd->dst.v_buffer = pc->yv12_fb[dst_fb_idx].v_buffer + recon_uvoffset;
John Koleszar's avatar
John Koleszar committed
247

248
                        xd->left_available = (mb_col != 0);
249

250
251
252
253
254
255
256
                        // Select the appropriate reference frame for this MB
                        if (xd->mbmi.ref_frame == LAST_FRAME)
                            ref_fb_idx = pc->lst_fb_idx;
                        else if (xd->mbmi.ref_frame == GOLDEN_FRAME)
                            ref_fb_idx = pc->gld_fb_idx;
                        else
                            ref_fb_idx = pc->alt_fb_idx;
John Koleszar's avatar
John Koleszar committed
257

258
259
260
                        xd->pre.y_buffer = pc->yv12_fb[ref_fb_idx].y_buffer + recon_yoffset;
                        xd->pre.u_buffer = pc->yv12_fb[ref_fb_idx].u_buffer + recon_uvoffset;
                        xd->pre.v_buffer = pc->yv12_fb[ref_fb_idx].v_buffer + recon_uvoffset;
John Koleszar's avatar
John Koleszar committed
261

262
                        vp8_build_uvmvs(xd, pc->full_pixel);
John Koleszar's avatar
John Koleszar committed
263

264
                        vp8_decode_macroblock(pbi, xd);
John Koleszar's avatar
John Koleszar committed
265

266
267
                        recon_yoffset += 16;
                        recon_uvoffset += 8;
John Koleszar's avatar
John Koleszar committed
268

269
                        ++xd->mode_info_context;  /* next mb */
John Koleszar's avatar
John Koleszar committed
270

271
272
273
274
                        xd->above_context[Y1CONTEXT] += 4;
                        xd->above_context[UCONTEXT ] += 2;
                        xd->above_context[VCONTEXT ] += 2;
                        xd->above_context[Y2CONTEXT] ++;
John Koleszar's avatar
John Koleszar committed
275

276
277
278
                        //pbi->mb_row_di[ithread].current_mb_col = mb_col;
                        pbi->current_mb_col[mb_row] = mb_col;
                    }
John Koleszar's avatar
John Koleszar committed
279

280
281
                    // adjust to the next row of mbs
                    vp8_extend_mb_row(
282
                    &pc->yv12_fb[dst_fb_idx],
John Koleszar's avatar
John Koleszar committed
283
                    xd->dst.y_buffer + 16, xd->dst.u_buffer + 8, xd->dst.v_buffer + 8
284
                    );
John Koleszar's avatar
John Koleszar committed
285

286
                    ++xd->mode_info_context;      /* skip prediction column */
John Koleszar's avatar
John Koleszar committed
287

288
289
                    // since we have multithread
                    xd->mode_info_context += xd->mode_info_stride * pbi->decoding_thread_count;
John Koleszar's avatar
John Koleszar committed
290
291
292
293
294
295

                    pbi->last_mb_row_decoded = mb_row;

                }
            }
        }
296
297
298
299
300
301
302
303
304
        //  add this to each frame
        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))
        {
            //SetEvent(pbi->h_event_end_decoding);
            sem_post(&pbi->h_event_end_decoding);
        }

        if ((pbi->b_multithreaded_lf) &&(pbi->common.filter_level))
            vp8_thread_loop_filter(pbi, mbrd, ithread);
John Koleszar's avatar
John Koleszar committed
305

306
    }
John Koleszar's avatar
John Koleszar committed
307
308
309
310
311
312
313
#else
    (void) p_data;
#endif

    return 0 ;
}

314
315

void vp8_thread_loop_filter(VP8D_COMP *pbi, MB_ROW_DEC *mbrd, int ithread)
John Koleszar's avatar
John Koleszar committed
316
317
318
{
#if CONFIG_MULTITHREAD

319
        if (sem_wait(&pbi->h_event_start_lpf[ithread]) == 0)
John Koleszar's avatar
John Koleszar committed
320
        {
321
322
323
           // if (pbi->b_multithreaded_lf == 0) // we're shutting down      ????
           //     break;
           // else
John Koleszar's avatar
John Koleszar committed
324
325
            {
                VP8_COMMON *cm  = &pbi->common;
326
                MACROBLOCKD *mbd = &mbrd->mbd;
John Koleszar's avatar
John Koleszar committed
327
328
                int default_filt_lvl = pbi->common.filter_level;

329
                YV12_BUFFER_CONFIG *post = cm->frame_to_show;
John Koleszar's avatar
John Koleszar committed
330
                loop_filter_info *lfi = cm->lf_info;
331
                //int frame_type = cm->frame_type;
John Koleszar's avatar
John Koleszar committed
332
333
334
335
336
337
338
339
340
341

                int mb_row;
                int mb_col;

                int filter_level;
                int alt_flt_enabled = mbd->segmentation_enabled;

                int i;
                unsigned char *y_ptr, *u_ptr, *v_ptr;

342
                volatile int *last_row_current_mb_col;
John Koleszar's avatar
John Koleszar committed
343
344

                // Set up the buffer pointers
345
346
347
                y_ptr = post->y_buffer + post->y_stride  * 16 * (ithread +1);
                u_ptr = post->u_buffer + post->uv_stride *  8 * (ithread +1);
                v_ptr = post->v_buffer + post->uv_stride *  8 * (ithread +1);
John Koleszar's avatar
John Koleszar committed
348
349

                // vp8_filter each macro block
350
                for (mb_row = ithread+1; mb_row < cm->mb_rows; mb_row+= (pbi->decoding_thread_count + 1))
John Koleszar's avatar
John Koleszar committed
351
                {
352
                    last_row_current_mb_col = &pbi->current_mb_col[mb_row -1];
John Koleszar's avatar
John Koleszar committed
353
354
355
356
357

                    for (mb_col = 0; mb_col < cm->mb_cols; mb_col++)
                    {
                        int Segment = (alt_flt_enabled) ? mbd->mode_info_context->mbmi.segment_id : 0;

358
359
360
361
362
363
364
365
366
367
                        if ((mb_col & 7) == 0)
                        {
                            while (mb_col > (*last_row_current_mb_col-8) && *last_row_current_mb_col != cm->mb_cols - 1)
                            {
                                x86_pause_hint();
                                thread_sleep(0);
                            }
                        }

                        filter_level = pbi->mt_baseline_filter_level[Segment];
John Koleszar's avatar
John Koleszar committed
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392

                        // Apply any context driven MB level adjustment
                        vp8_adjust_mb_lf_value(mbd, &filter_level);

                        if (filter_level)
                        {
                            if (mb_col > 0)
                                cm->lf_mbv(y_ptr, u_ptr, v_ptr, post->y_stride, post->uv_stride, &lfi[filter_level], cm->simpler_lpf);

                            if (mbd->mode_info_context->mbmi.dc_diff > 0)
                                cm->lf_bv(y_ptr, u_ptr, v_ptr, post->y_stride, post->uv_stride, &lfi[filter_level], cm->simpler_lpf);

                            // don't apply across umv border
                            if (mb_row > 0)
                                cm->lf_mbh(y_ptr, u_ptr, v_ptr, post->y_stride, post->uv_stride, &lfi[filter_level], cm->simpler_lpf);

                            if (mbd->mode_info_context->mbmi.dc_diff > 0)
                                cm->lf_bh(y_ptr, u_ptr, v_ptr, post->y_stride, post->uv_stride, &lfi[filter_level], cm->simpler_lpf);
                        }

                        y_ptr += 16;
                        u_ptr += 8;
                        v_ptr += 8;

                        mbd->mode_info_context++;     // step to next MB
393
                        pbi->current_mb_col[mb_row] = mb_col;
John Koleszar's avatar
John Koleszar committed
394
395
396
397
                    }

                    mbd->mode_info_context++;         // Skip border mb

398
399
400
                    y_ptr += post->y_stride  * 16 * (pbi->decoding_thread_count + 1) - post->y_width;
                    u_ptr += post->uv_stride *  8 * (pbi->decoding_thread_count + 1) - post->uv_width;
                    v_ptr += post->uv_stride *  8 * (pbi->decoding_thread_count + 1) - post->uv_width;
John Koleszar's avatar
John Koleszar committed
401

402
403
404
                    mbd->mode_info_context += pbi->decoding_thread_count * mbd->mode_info_stride;         // Skip border mb
                }
            }
John Koleszar's avatar
John Koleszar committed
405
406
        }

407
408
409
410
411
        //  add this to each frame
        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))
        {
          sem_post(&pbi->h_event_end_lpf);
        }
John Koleszar's avatar
John Koleszar committed
412
#else
413
    (void) pbi;
John Koleszar's avatar
John Koleszar committed
414
415
416
417
418
419
420
421
422
423
424
425
#endif
}

void vp8_decoder_create_threads(VP8D_COMP *pbi)
{
#if CONFIG_MULTITHREAD
    int core_count = 0;
    int ithread;

    pbi->b_multithreaded_rd = 0;
    pbi->b_multithreaded_lf = 0;
    pbi->allocated_decoding_thread_count = 0;
426
    core_count = (pbi->max_threads > 16) ? 16 : pbi->max_threads;
John Koleszar's avatar
John Koleszar committed
427
428
429
430

    if (core_count > 1)
    {
        pbi->b_multithreaded_rd = 1;
431
432
        pbi->b_multithreaded_lf = 1;  // this can be merged with pbi->b_multithreaded_rd ?
        pbi->decoding_thread_count = core_count -1;
John Koleszar's avatar
John Koleszar committed
433
434

        CHECK_MEM_ERROR(pbi->h_decoding_thread, vpx_malloc(sizeof(pthread_t) * pbi->decoding_thread_count));
435
        CHECK_MEM_ERROR(pbi->h_event_start_decoding, vpx_malloc(sizeof(sem_t) * pbi->decoding_thread_count));
John Koleszar's avatar
John Koleszar committed
436
437
438
439
        CHECK_MEM_ERROR(pbi->mb_row_di, vpx_memalign(32, sizeof(MB_ROW_DEC) * pbi->decoding_thread_count));
        vpx_memset(pbi->mb_row_di, 0, sizeof(MB_ROW_DEC) * pbi->decoding_thread_count);
        CHECK_MEM_ERROR(pbi->de_thread_data, vpx_malloc(sizeof(DECODETHREAD_DATA) * pbi->decoding_thread_count));

440
441
442
        CHECK_MEM_ERROR(pbi->current_mb_col, vpx_malloc(sizeof(int) * MAX_ROWS));  // pc->mb_rows));
        CHECK_MEM_ERROR(pbi->h_event_start_lpf, vpx_malloc(sizeof(sem_t) * pbi->decoding_thread_count));

John Koleszar's avatar
John Koleszar committed
443
444
        for (ithread = 0; ithread < pbi->decoding_thread_count; ithread++)
        {
445
446
            sem_init(&pbi->h_event_start_decoding[ithread], 0, 0);
            sem_init(&pbi->h_event_start_lpf[ithread], 0, 0);
John Koleszar's avatar
John Koleszar committed
447
448
449
450
451
452
453
454

            pbi->de_thread_data[ithread].ithread  = ithread;
            pbi->de_thread_data[ithread].ptr1     = (void *)pbi;
            pbi->de_thread_data[ithread].ptr2     = (void *) &pbi->mb_row_di[ithread];

            pthread_create(&pbi->h_decoding_thread[ithread], 0, vp8_thread_decoding_proc, (&pbi->de_thread_data[ithread]));
        }

455
456
457
        sem_init(&pbi->h_event_end_decoding, 0, 0);
        sem_init(&pbi->h_event_end_lpf, 0, 0);

John Koleszar's avatar
John Koleszar committed
458
459
460
461
462
463
464
465
466
467
468
469
470
471
        pbi->allocated_decoding_thread_count = pbi->decoding_thread_count;
    }

#else
    (void) pbi;
#endif
}

void vp8_decoder_remove_threads(VP8D_COMP *pbi)
{
#if CONFIG_MULTITHREAD

    if (pbi->b_multithreaded_lf)
    {
472
        int i;
John Koleszar's avatar
John Koleszar committed
473
        pbi->b_multithreaded_lf = 0;
474
475
476
477
478

        for (i = 0; i < pbi->allocated_decoding_thread_count; i++)
            sem_destroy(&pbi->h_event_start_lpf[i]);

        sem_destroy(&pbi->h_event_end_lpf);
John Koleszar's avatar
John Koleszar committed
479
480
481
482
483
    }

    //shutdown MB Decoding thread;
    if (pbi->b_multithreaded_rd)
    {
484
485
        int i;

John Koleszar's avatar
John Koleszar committed
486
        pbi->b_multithreaded_rd = 0;
487

John Koleszar's avatar
John Koleszar committed
488
        // allow all threads to exit
489
        for (i = 0; i < pbi->allocated_decoding_thread_count; i++)
John Koleszar's avatar
John Koleszar committed
490
        {
491
492
            sem_post(&pbi->h_event_start_decoding[i]);
            pthread_join(pbi->h_decoding_thread[i], NULL);
John Koleszar's avatar
John Koleszar committed
493
494
        }

495
496
497
        for (i = 0; i < pbi->allocated_decoding_thread_count; i++)
        {
            sem_destroy(&pbi->h_event_start_decoding[i]);
John Koleszar's avatar
John Koleszar committed
498
499
        }

500
        sem_destroy(&pbi->h_event_end_decoding);
John Koleszar's avatar
John Koleszar committed
501
502
503
504
505
506
507

        if (pbi->h_decoding_thread)
        {
            vpx_free(pbi->h_decoding_thread);
            pbi->h_decoding_thread = NULL;
        }

508
        if (pbi->h_event_start_decoding)
John Koleszar's avatar
John Koleszar committed
509
        {
510
511
512
513
514
515
516
517
            vpx_free(pbi->h_event_start_decoding);
            pbi->h_event_start_decoding = NULL;
        }

        if (pbi->h_event_start_lpf)
        {
            vpx_free(pbi->h_event_start_lpf);
            pbi->h_event_start_lpf = NULL;
John Koleszar's avatar
John Koleszar committed
518
519
520
521
522
523
524
525
526
527
528
529
530
531
        }

        if (pbi->mb_row_di)
        {
            vpx_free(pbi->mb_row_di);
            pbi->mb_row_di = NULL ;
        }

        if (pbi->de_thread_data)
        {
            vpx_free(pbi->de_thread_data);
            pbi->de_thread_data = NULL;
        }

532
533
534
535
536
537
        if (pbi->current_mb_col)
        {
            vpx_free(pbi->current_mb_col);
            pbi->current_mb_col = NULL ;
        }
    }
John Koleszar's avatar
John Koleszar committed
538
539
540
541
542
543
544
545
546
#else
    (void) pbi;
#endif
}


void vp8_start_lfthread(VP8D_COMP *pbi)
{
#if CONFIG_MULTITHREAD
547
  /*
John Koleszar's avatar
John Koleszar committed
548
549
550
    memcpy(&pbi->lpfmb, &pbi->mb, sizeof(pbi->mb));
    pbi->last_mb_row_decoded = 0;
    sem_post(&pbi->h_event_start_lpf);
551
552
    */
    (void) pbi;
John Koleszar's avatar
John Koleszar committed
553
554
555
556
557
558
559
560
#else
    (void) pbi;
#endif
}

void vp8_stop_lfthread(VP8D_COMP *pbi)
{
#if CONFIG_MULTITHREAD
561
  /*
John Koleszar's avatar
John Koleszar committed
562
563
564
565
    struct vpx_usec_timer timer;

    vpx_usec_timer_start(&timer);

566
    sem_wait(&pbi->h_event_end_lpf);
John Koleszar's avatar
John Koleszar committed
567
568
569

    vpx_usec_timer_mark(&timer);
    pbi->time_loop_filtering += vpx_usec_timer_elapsed(&timer);
570
571
    */
    (void) pbi;
John Koleszar's avatar
John Koleszar committed
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
#else
    (void) pbi;
#endif
}


void vp8_mtdecode_mb_rows(VP8D_COMP *pbi,
                          MACROBLOCKD *xd)
{
#if CONFIG_MULTITHREAD
    int mb_row;
    VP8_COMMON *pc = &pbi->common;

    int ibc = 0;
    int num_part = 1 << pbi->common.multi_token_partition;
587
588
    int i;
    volatile int *last_row_current_mb_col = NULL;
John Koleszar's avatar
John Koleszar committed
589
590
591

    vp8_setup_decoding_thread_data(pbi, xd, pbi->mb_row_di, pbi->decoding_thread_count);

592
593
594
    for (i = 0; i < pbi->decoding_thread_count; i++)
        sem_post(&pbi->h_event_start_decoding[i]);

John Koleszar's avatar
John Koleszar committed
595
596
597
598
    for (mb_row = 0; mb_row < pc->mb_rows; mb_row += (pbi->decoding_thread_count + 1))
    {
        int i;

599
        xd->current_bc = &pbi->mbc[mb_row%num_part];
John Koleszar's avatar
John Koleszar committed
600

601
        //vp8_decode_mb_row(pbi, pc, mb_row, xd);
John Koleszar's avatar
John Koleszar committed
602
        {
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
            int i;
            int recon_yoffset, recon_uvoffset;
            int mb_col;
            int ref_fb_idx = pc->lst_fb_idx;
            int dst_fb_idx = pc->new_fb_idx;
            int recon_y_stride = pc->yv12_fb[ref_fb_idx].y_stride;
            int recon_uv_stride = pc->yv12_fb[ref_fb_idx].uv_stride;

           // volatile int *last_row_current_mb_col = NULL;
            if (mb_row > 0)
                last_row_current_mb_col = &pbi->current_mb_col[mb_row -1];

            vpx_memset(pc->left_context, 0, sizeof(pc->left_context));
            recon_yoffset = mb_row * recon_y_stride * 16;
            recon_uvoffset = mb_row * recon_uv_stride * 8;
            // reset above block coeffs

            xd->above_context[Y1CONTEXT] = pc->above_context[Y1CONTEXT];
            xd->above_context[UCONTEXT ] = pc->above_context[UCONTEXT];
            xd->above_context[VCONTEXT ] = pc->above_context[VCONTEXT];
            xd->above_context[Y2CONTEXT] = pc->above_context[Y2CONTEXT];
            xd->up_available = (mb_row != 0);

            xd->mb_to_top_edge = -((mb_row * 16)) << 3;
            xd->mb_to_bottom_edge = ((pc->mb_rows - 1 - mb_row) * 16) << 3;

            for (mb_col = 0; mb_col < pc->mb_cols; mb_col++)
            {
John Koleszar's avatar
John Koleszar committed
631

632
633
634
635
636
637
638
                if ( mb_row > 0 && (mb_col & 7) == 0){
               //if ( mb_row > 0 ){
                  while (mb_col > (*last_row_current_mb_col - 8) && *last_row_current_mb_col != pc->mb_cols - 1)
                  {
                      x86_pause_hint();
                      thread_sleep(0);
                  }
John Koleszar's avatar
John Koleszar committed
639

640
                }
John Koleszar's avatar
John Koleszar committed
641

642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
                // Take a copy of the mode and Mv information for this macroblock into the xd->mbmi
                // the partition_bmi array is unused in the decoder, so don't copy it.
                vpx_memcpy(&xd->mbmi, &xd->mode_info_context->mbmi,
                           sizeof(MB_MODE_INFO) - sizeof(xd->mbmi.partition_bmi));

                if (xd->mbmi.mode == SPLITMV || xd->mbmi.mode == B_PRED)
                {
                    for (i = 0; i < 16; i++)
                    {
                        BLOCKD *d = &xd->block[i];
                        vpx_memcpy(&d->bmi, &xd->mode_info_context->bmi[i], sizeof(B_MODE_INFO));
                    }
                }

                // Distance of Mb to the various image edges.
                // These specified to 8th pel as they are always compared to values that are in 1/8th pel units
                xd->mb_to_left_edge = -((mb_col * 16) << 3);
                xd->mb_to_right_edge = ((pc->mb_cols - 1 - mb_col) * 16) << 3;

                xd->dst.y_buffer = pc->yv12_fb[dst_fb_idx].y_buffer + recon_yoffset;
                xd->dst.u_buffer = pc->yv12_fb[dst_fb_idx].u_buffer + recon_uvoffset;
                xd->dst.v_buffer = pc->yv12_fb[dst_fb_idx].v_buffer + recon_uvoffset;

                xd->left_available = (mb_col != 0);

                // Select the appropriate reference frame for this MB
                if (xd->mbmi.ref_frame == LAST_FRAME)
                    ref_fb_idx = pc->lst_fb_idx;
                else if (xd->mbmi.ref_frame == GOLDEN_FRAME)
                    ref_fb_idx = pc->gld_fb_idx;
                else
                    ref_fb_idx = pc->alt_fb_idx;

                xd->pre.y_buffer = pc->yv12_fb[ref_fb_idx].y_buffer + recon_yoffset;
                xd->pre.u_buffer = pc->yv12_fb[ref_fb_idx].u_buffer + recon_uvoffset;
                xd->pre.v_buffer = pc->yv12_fb[ref_fb_idx].v_buffer + recon_uvoffset;

                vp8_build_uvmvs(xd, pc->full_pixel);

                vp8_decode_macroblock(pbi, xd);

                recon_yoffset += 16;
                recon_uvoffset += 8;

                ++xd->mode_info_context;  /* next mb */

                xd->above_context[Y1CONTEXT] += 4;
                xd->above_context[UCONTEXT ] += 2;
                xd->above_context[VCONTEXT ] += 2;
                xd->above_context[Y2CONTEXT] ++;

                //pbi->current_mb_col_main = mb_col;
                pbi->current_mb_col[mb_row] = mb_col;
            }

            // adjust to the next row of mbs
            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
            );
John Koleszar's avatar
John Koleszar committed
702

703
            ++xd->mode_info_context;      /* skip prediction column */
John Koleszar's avatar
John Koleszar committed
704

705
706
            pbi->last_mb_row_decoded = mb_row;
        }
John Koleszar's avatar
John Koleszar committed
707
        xd->mode_info_context += xd->mode_info_stride * pbi->decoding_thread_count;
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
    }

    sem_wait(&pbi->h_event_end_decoding);   // add back for each frame
#else
    (void) pbi;
    (void) xd;
#endif
}


void vp8_mt_loop_filter_frame( VP8D_COMP *pbi)
{
#if CONFIG_MULTITHREAD
    VP8_COMMON *cm  = &pbi->common;
    MACROBLOCKD *mbd = &pbi->mb;
    int default_filt_lvl = pbi->common.filter_level;

    YV12_BUFFER_CONFIG *post = cm->frame_to_show;
    loop_filter_info *lfi = cm->lf_info;
    int frame_type = cm->frame_type;

    int mb_row;
    int mb_col;

    int filter_level;
    int alt_flt_enabled = mbd->segmentation_enabled;

    int i;
    unsigned char *y_ptr, *u_ptr, *v_ptr;

    volatile int *last_row_current_mb_col=NULL;

    vp8_setup_loop_filter_thread_data(pbi, mbd, pbi->mb_row_di, pbi->decoding_thread_count);

    mbd->mode_info_context = cm->mi;          // Point at base of Mb MODE_INFO list

    // Note the baseline filter values for each segment
    if (alt_flt_enabled)
    {
        for (i = 0; i < MAX_MB_SEGMENTS; i++)
        {
            // Abs value
            if (mbd->mb_segement_abs_delta == SEGMENT_ABSDATA)
                pbi->mt_baseline_filter_level[i] = mbd->segment_feature_data[MB_LVL_ALT_LF][i];
            // Delta Value
            else
            {
                pbi->mt_baseline_filter_level[i] = default_filt_lvl + mbd->segment_feature_data[MB_LVL_ALT_LF][i];
                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
            }
        }
    }
    else
    {
        for (i = 0; i < MAX_MB_SEGMENTS; i++)
            pbi->mt_baseline_filter_level[i] = default_filt_lvl;
    }

    // Initialize the loop filter for this frame.
    if ((cm->last_filter_type != cm->filter_type) || (cm->last_sharpness_level != cm->sharpness_level))
        vp8_init_loop_filter(cm);
    else if (frame_type != cm->last_frame_type)
        vp8_frame_init_loop_filter(lfi, frame_type);

    for (i = 0; i < pbi->decoding_thread_count; i++)
        sem_post(&pbi->h_event_start_lpf[i]);
        // sem_post(&pbi->h_event_start_lpf);

    // Set up the buffer pointers
    y_ptr = post->y_buffer;
    u_ptr = post->u_buffer;
    v_ptr = post->v_buffer;
John Koleszar's avatar
John Koleszar committed
780

781
782
783
784
785
786
787
    // vp8_filter each macro block
    for (mb_row = 0; mb_row < cm->mb_rows; mb_row+= (pbi->decoding_thread_count + 1))
    {
        if (mb_row > 0)
            last_row_current_mb_col = &pbi->current_mb_col[mb_row -1];

        for (mb_col = 0; mb_col < cm->mb_cols; mb_col++)
John Koleszar's avatar
John Koleszar committed
788
        {
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
            int Segment = (alt_flt_enabled) ? mbd->mode_info_context->mbmi.segment_id : 0;


            if ( mb_row > 0 && (mb_col & 7) == 0){
            // if ( mb_row > 0 ){
                while (mb_col > (*last_row_current_mb_col-8) && *last_row_current_mb_col != cm->mb_cols - 1)
                {
                    x86_pause_hint();
                    thread_sleep(0);
                }
            }

            filter_level = pbi->mt_baseline_filter_level[Segment];

            // Distance of Mb to the various image edges.
            // These specified to 8th pel as they are always compared to values that are in 1/8th pel units
            // Apply any context driven MB level adjustment
            vp8_adjust_mb_lf_value(mbd, &filter_level);

            if (filter_level)
            {
                if (mb_col > 0)
                    cm->lf_mbv(y_ptr, u_ptr, v_ptr, post->y_stride, post->uv_stride, &lfi[filter_level], cm->simpler_lpf);

                if (mbd->mode_info_context->mbmi.dc_diff > 0)
                    cm->lf_bv(y_ptr, u_ptr, v_ptr, post->y_stride, post->uv_stride, &lfi[filter_level], cm->simpler_lpf);

                // don't apply across umv border
                if (mb_row > 0)
                    cm->lf_mbh(y_ptr, u_ptr, v_ptr, post->y_stride, post->uv_stride, &lfi[filter_level], cm->simpler_lpf);

                if (mbd->mode_info_context->mbmi.dc_diff > 0)
                    cm->lf_bh(y_ptr, u_ptr, v_ptr, post->y_stride, post->uv_stride, &lfi[filter_level], cm->simpler_lpf);
            }

            y_ptr += 16;
            u_ptr += 8;
            v_ptr += 8;

            mbd->mode_info_context++;     // step to next MB
            pbi->current_mb_col[mb_row] = mb_col;
John Koleszar's avatar
John Koleszar committed
830
        }
831
832
833
834
835
836
837
        mbd->mode_info_context++;         // Skip border mb

        //update for multi-thread
        y_ptr += post->y_stride  * 16 * (pbi->decoding_thread_count + 1) - post->y_width;
        u_ptr += post->uv_stride *  8 * (pbi->decoding_thread_count + 1) - post->uv_width;
        v_ptr += post->uv_stride *  8 * (pbi->decoding_thread_count + 1) - post->uv_width;
        mbd->mode_info_context += pbi->decoding_thread_count * mbd->mode_info_stride;
John Koleszar's avatar
John Koleszar committed
838
839
    }

840
    sem_wait(&pbi->h_event_end_lpf);
John Koleszar's avatar
John Koleszar committed
841
842
843
844
#else
    (void) pbi;
#endif
}