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#include "onyx_int.h"
12#include "vp8/common/threading.h"
13#include "vp8/common/common.h"
14#include "vp8/common/extend.h"
15#include "bitstream.h"
16#include "encodeframe.h"
17
18#if CONFIG_MULTITHREAD
19
20extern void vp8cx_mb_init_quantizer(VP8_COMP *cpi, MACROBLOCK *x, int ok_to_skip);
21
22extern void vp8_loopfilter_frame(VP8_COMP *cpi, VP8_COMMON *cm);
23
24static THREAD_FUNCTION thread_loopfilter(void *p_data)
25{
26    VP8_COMP *cpi = (VP8_COMP *)(((LPFTHREAD_DATA *)p_data)->ptr1);
27    VP8_COMMON *cm = &cpi->common;
28
29    while (1)
30    {
31        if (cpi->b_multi_threaded == 0)
32            break;
33
34        if (sem_wait(&cpi->h_event_start_lpf) == 0)
35        {
36            if (cpi->b_multi_threaded == 0) /* we're shutting down */
37                break;
38
39            vp8_loopfilter_frame(cpi, cm);
40
41            sem_post(&cpi->h_event_end_lpf);
42        }
43    }
44
45    return 0;
46}
47
48static
49THREAD_FUNCTION thread_encoding_proc(void *p_data)
50{
51    int ithread = ((ENCODETHREAD_DATA *)p_data)->ithread;
52    VP8_COMP *cpi = (VP8_COMP *)(((ENCODETHREAD_DATA *)p_data)->ptr1);
53    MB_ROW_COMP *mbri = (MB_ROW_COMP *)(((ENCODETHREAD_DATA *)p_data)->ptr2);
54    ENTROPY_CONTEXT_PLANES mb_row_left_context;
55
56    while (1)
57    {
58        if (cpi->b_multi_threaded == 0)
59            break;
60
61        if (sem_wait(&cpi->h_event_start_encoding[ithread]) == 0)
62        {
63            const int nsync = cpi->mt_sync_range;
64            VP8_COMMON *cm = &cpi->common;
65            int mb_row;
66            MACROBLOCK *x = &mbri->mb;
67            MACROBLOCKD *xd = &x->e_mbd;
68            TOKENEXTRA *tp ;
69#if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
70            TOKENEXTRA *tp_start = cpi->tok + (1 + ithread) * (16 * 24);
71            const int num_part = (1 << cm->multi_token_partition);
72#endif
73
74            int *segment_counts = mbri->segment_counts;
75            int *totalrate = &mbri->totalrate;
76
77            if (cpi->b_multi_threaded == 0) /* we're shutting down */
78                break;
79
80            for (mb_row = ithread + 1; mb_row < cm->mb_rows; mb_row += (cpi->encoding_thread_count + 1))
81            {
82
83                int recon_yoffset, recon_uvoffset;
84                int mb_col;
85                int ref_fb_idx = cm->lst_fb_idx;
86                int dst_fb_idx = cm->new_fb_idx;
87                int recon_y_stride = cm->yv12_fb[ref_fb_idx].y_stride;
88                int recon_uv_stride = cm->yv12_fb[ref_fb_idx].uv_stride;
89                int map_index = (mb_row * cm->mb_cols);
90                volatile const int *last_row_current_mb_col;
91                volatile int *current_mb_col = &cpi->mt_current_mb_col[mb_row];
92
93#if  (CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING)
94                vp8_writer *w = &cpi->bc[1 + (mb_row % num_part)];
95#else
96                tp = cpi->tok + (mb_row * (cm->mb_cols * 16 * 24));
97                cpi->tplist[mb_row].start = tp;
98#endif
99
100                last_row_current_mb_col = &cpi->mt_current_mb_col[mb_row - 1];
101
102                /* reset above block coeffs */
103                xd->above_context = cm->above_context;
104                xd->left_context = &mb_row_left_context;
105
106                vp8_zero(mb_row_left_context);
107
108                xd->up_available = (mb_row != 0);
109                recon_yoffset = (mb_row * recon_y_stride * 16);
110                recon_uvoffset = (mb_row * recon_uv_stride * 8);
111
112                /* Set the mb activity pointer to the start of the row. */
113                x->mb_activity_ptr = &cpi->mb_activity_map[map_index];
114
115                /* for each macroblock col in image */
116                for (mb_col = 0; mb_col < cm->mb_cols; mb_col++)
117                {
118                    *current_mb_col = mb_col - 1;
119
120                    if ((mb_col & (nsync - 1)) == 0)
121                    {
122                        while (mb_col > (*last_row_current_mb_col - nsync))
123                        {
124                            x86_pause_hint();
125                            thread_sleep(0);
126                        }
127                    }
128
129#if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
130                    tp = tp_start;
131#endif
132
133                    /* Distance of Mb to the various image edges.
134                     * These specified to 8th pel as they are always compared
135                     * to values that are in 1/8th pel units
136                     */
137                    xd->mb_to_left_edge = -((mb_col * 16) << 3);
138                    xd->mb_to_right_edge = ((cm->mb_cols - 1 - mb_col) * 16) << 3;
139                    xd->mb_to_top_edge = -((mb_row * 16) << 3);
140                    xd->mb_to_bottom_edge = ((cm->mb_rows - 1 - mb_row) * 16) << 3;
141
142                    /* Set up limit values for motion vectors used to prevent
143                     * them extending outside the UMV borders
144                     */
145                    x->mv_col_min = -((mb_col * 16) + (VP8BORDERINPIXELS - 16));
146                    x->mv_col_max = ((cm->mb_cols - 1 - mb_col) * 16) + (VP8BORDERINPIXELS - 16);
147                    x->mv_row_min = -((mb_row * 16) + (VP8BORDERINPIXELS - 16));
148                    x->mv_row_max = ((cm->mb_rows - 1 - mb_row) * 16) + (VP8BORDERINPIXELS - 16);
149
150                    xd->dst.y_buffer = cm->yv12_fb[dst_fb_idx].y_buffer + recon_yoffset;
151                    xd->dst.u_buffer = cm->yv12_fb[dst_fb_idx].u_buffer + recon_uvoffset;
152                    xd->dst.v_buffer = cm->yv12_fb[dst_fb_idx].v_buffer + recon_uvoffset;
153                    xd->left_available = (mb_col != 0);
154
155                    x->rddiv = cpi->RDDIV;
156                    x->rdmult = cpi->RDMULT;
157
158                    /* Copy current mb to a buffer */
159                    vp8_copy_mem16x16(x->src.y_buffer, x->src.y_stride, x->thismb, 16);
160
161                    if (cpi->oxcf.tuning == VP8_TUNE_SSIM)
162                        vp8_activity_masking(cpi, x);
163
164                    /* Is segmentation enabled */
165                    /* MB level adjustment to quantizer */
166                    if (xd->segmentation_enabled)
167                    {
168                        /* Code to set segment id in xd->mbmi.segment_id for
169                         * current MB (with range checking)
170                         */
171                        if (cpi->segmentation_map[map_index + mb_col] <= 3)
172                            xd->mode_info_context->mbmi.segment_id = cpi->segmentation_map[map_index + mb_col];
173                        else
174                            xd->mode_info_context->mbmi.segment_id = 0;
175
176                        vp8cx_mb_init_quantizer(cpi, x, 1);
177                    }
178                    else
179                        /* Set to Segment 0 by default */
180                        xd->mode_info_context->mbmi.segment_id = 0;
181
182                    x->active_ptr = cpi->active_map + map_index + mb_col;
183
184                    if (cm->frame_type == KEY_FRAME)
185                    {
186                        *totalrate += vp8cx_encode_intra_macroblock(cpi, x, &tp);
187#ifdef MODE_STATS
188                        y_modes[xd->mbmi.mode] ++;
189#endif
190                    }
191                    else
192                    {
193                        *totalrate += vp8cx_encode_inter_macroblock(cpi, x, &tp, recon_yoffset, recon_uvoffset, mb_row, mb_col);
194
195#ifdef MODE_STATS
196                        inter_y_modes[xd->mbmi.mode] ++;
197
198                        if (xd->mbmi.mode == SPLITMV)
199                        {
200                            int b;
201
202                            for (b = 0; b < xd->mbmi.partition_count; b++)
203                            {
204                                inter_b_modes[x->partition->bmi[b].mode] ++;
205                            }
206                        }
207
208#endif
209
210                        /* Special case code for cyclic refresh
211                         * If cyclic update enabled then copy
212                         * xd->mbmi.segment_id; (which may have been updated
213                         * based on mode during
214                         * vp8cx_encode_inter_macroblock()) back into the
215                         * global segmentation map
216                         */
217                        if ((cpi->current_layer == 0) &&
218                            (cpi->cyclic_refresh_mode_enabled &&
219                             xd->segmentation_enabled))
220                        {
221                            const MB_MODE_INFO * mbmi = &xd->mode_info_context->mbmi;
222                            cpi->segmentation_map[map_index + mb_col] = mbmi->segment_id;
223
224                            /* If the block has been refreshed mark it as clean
225                             * (the magnitude of the -ve influences how long it
226                             * will be before we consider another refresh):
227                             * Else if it was coded (last frame 0,0) and has
228                             * not already been refreshed then mark it as a
229                             * candidate for cleanup next time (marked 0) else
230                             * mark it as dirty (1).
231                             */
232                            if (mbmi->segment_id)
233                                cpi->cyclic_refresh_map[map_index + mb_col] = -1;
234                            else if ((mbmi->mode == ZEROMV) && (mbmi->ref_frame == LAST_FRAME))
235                            {
236                                if (cpi->cyclic_refresh_map[map_index + mb_col] == 1)
237                                    cpi->cyclic_refresh_map[map_index + mb_col] = 0;
238                            }
239                            else
240                                cpi->cyclic_refresh_map[map_index + mb_col] = 1;
241
242                        }
243                    }
244
245#if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
246                    /* pack tokens for this MB */
247                    {
248                        int tok_count = tp - tp_start;
249                        pack_tokens(w, tp_start, tok_count);
250                    }
251#else
252                    cpi->tplist[mb_row].stop = tp;
253#endif
254                    /* Increment pointer into gf usage flags structure. */
255                    x->gf_active_ptr++;
256
257                    /* Increment the activity mask pointers. */
258                    x->mb_activity_ptr++;
259
260                    /* adjust to the next column of macroblocks */
261                    x->src.y_buffer += 16;
262                    x->src.u_buffer += 8;
263                    x->src.v_buffer += 8;
264
265                    recon_yoffset += 16;
266                    recon_uvoffset += 8;
267
268                    /* Keep track of segment usage */
269                    segment_counts[xd->mode_info_context->mbmi.segment_id]++;
270
271                    /* skip to next mb */
272                    xd->mode_info_context++;
273                    x->partition_info++;
274                    xd->above_context++;
275                }
276
277                vp8_extend_mb_row( &cm->yv12_fb[dst_fb_idx],
278                                    xd->dst.y_buffer + 16,
279                                    xd->dst.u_buffer + 8,
280                                    xd->dst.v_buffer + 8);
281
282                *current_mb_col = mb_col + nsync;
283
284                /* this is to account for the border */
285                xd->mode_info_context++;
286                x->partition_info++;
287
288                x->src.y_buffer += 16 * x->src.y_stride * (cpi->encoding_thread_count + 1) - 16 * cm->mb_cols;
289                x->src.u_buffer += 8 * x->src.uv_stride * (cpi->encoding_thread_count + 1) - 8 * cm->mb_cols;
290                x->src.v_buffer += 8 * x->src.uv_stride * (cpi->encoding_thread_count + 1) - 8 * cm->mb_cols;
291
292                xd->mode_info_context += xd->mode_info_stride * cpi->encoding_thread_count;
293                x->partition_info += xd->mode_info_stride * cpi->encoding_thread_count;
294                x->gf_active_ptr   += cm->mb_cols * cpi->encoding_thread_count;
295
296                if (mb_row == cm->mb_rows - 1)
297                {
298                    sem_post(&cpi->h_event_end_encoding); /* signal frame encoding end */
299                }
300            }
301        }
302    }
303
304    /* printf("exit thread %d\n", ithread); */
305    return 0;
306}
307
308static void setup_mbby_copy(MACROBLOCK *mbdst, MACROBLOCK *mbsrc)
309{
310
311    MACROBLOCK *x = mbsrc;
312    MACROBLOCK *z = mbdst;
313    int i;
314
315    z->ss               = x->ss;
316    z->ss_count          = x->ss_count;
317    z->searches_per_step  = x->searches_per_step;
318    z->errorperbit      = x->errorperbit;
319
320    z->sadperbit16      = x->sadperbit16;
321    z->sadperbit4       = x->sadperbit4;
322
323    /*
324    z->mv_col_min    = x->mv_col_min;
325    z->mv_col_max    = x->mv_col_max;
326    z->mv_row_min    = x->mv_row_min;
327    z->mv_row_max    = x->mv_row_max;
328    */
329
330    z->short_fdct4x4     = x->short_fdct4x4;
331    z->short_fdct8x4     = x->short_fdct8x4;
332    z->short_walsh4x4    = x->short_walsh4x4;
333    z->quantize_b        = x->quantize_b;
334    z->quantize_b_pair   = x->quantize_b_pair;
335    z->optimize          = x->optimize;
336
337    /*
338    z->mvc              = x->mvc;
339    z->src.y_buffer      = x->src.y_buffer;
340    z->src.u_buffer      = x->src.u_buffer;
341    z->src.v_buffer      = x->src.v_buffer;
342    */
343
344    z->mvcost[0] =  x->mvcost[0];
345    z->mvcost[1] =  x->mvcost[1];
346    z->mvsadcost[0] =  x->mvsadcost[0];
347    z->mvsadcost[1] =  x->mvsadcost[1];
348
349    z->token_costs = x->token_costs;
350    z->inter_bmode_costs = x->inter_bmode_costs;
351    z->mbmode_cost = x->mbmode_cost;
352    z->intra_uv_mode_cost = x->intra_uv_mode_cost;
353    z->bmode_costs = x->bmode_costs;
354
355    for (i = 0; i < 25; i++)
356    {
357        z->block[i].quant           = x->block[i].quant;
358        z->block[i].quant_fast      = x->block[i].quant_fast;
359        z->block[i].quant_shift     = x->block[i].quant_shift;
360        z->block[i].zbin            = x->block[i].zbin;
361        z->block[i].zrun_zbin_boost = x->block[i].zrun_zbin_boost;
362        z->block[i].round           = x->block[i].round;
363        z->block[i].src_stride      = x->block[i].src_stride;
364    }
365
366    z->q_index           = x->q_index;
367    z->act_zbin_adj      = x->act_zbin_adj;
368    z->last_act_zbin_adj = x->last_act_zbin_adj;
369
370    {
371        MACROBLOCKD *xd = &x->e_mbd;
372        MACROBLOCKD *zd = &z->e_mbd;
373
374        /*
375        zd->mode_info_context = xd->mode_info_context;
376        zd->mode_info        = xd->mode_info;
377
378        zd->mode_info_stride  = xd->mode_info_stride;
379        zd->frame_type       = xd->frame_type;
380        zd->up_available     = xd->up_available   ;
381        zd->left_available   = xd->left_available;
382        zd->left_context     = xd->left_context;
383        zd->last_frame_dc     = xd->last_frame_dc;
384        zd->last_frame_dccons = xd->last_frame_dccons;
385        zd->gold_frame_dc     = xd->gold_frame_dc;
386        zd->gold_frame_dccons = xd->gold_frame_dccons;
387        zd->mb_to_left_edge    = xd->mb_to_left_edge;
388        zd->mb_to_right_edge   = xd->mb_to_right_edge;
389        zd->mb_to_top_edge     = xd->mb_to_top_edge   ;
390        zd->mb_to_bottom_edge  = xd->mb_to_bottom_edge;
391        zd->gf_active_ptr     = xd->gf_active_ptr;
392        zd->frames_since_golden       = xd->frames_since_golden;
393        zd->frames_till_alt_ref_frame   = xd->frames_till_alt_ref_frame;
394        */
395        zd->subpixel_predict         = xd->subpixel_predict;
396        zd->subpixel_predict8x4      = xd->subpixel_predict8x4;
397        zd->subpixel_predict8x8      = xd->subpixel_predict8x8;
398        zd->subpixel_predict16x16    = xd->subpixel_predict16x16;
399        zd->segmentation_enabled     = xd->segmentation_enabled;
400        zd->mb_segement_abs_delta      = xd->mb_segement_abs_delta;
401        vpx_memcpy(zd->segment_feature_data, xd->segment_feature_data,
402                   sizeof(xd->segment_feature_data));
403
404        vpx_memcpy(zd->dequant_y1_dc, xd->dequant_y1_dc,
405                   sizeof(xd->dequant_y1_dc));
406        vpx_memcpy(zd->dequant_y1, xd->dequant_y1, sizeof(xd->dequant_y1));
407        vpx_memcpy(zd->dequant_y2, xd->dequant_y2, sizeof(xd->dequant_y2));
408        vpx_memcpy(zd->dequant_uv, xd->dequant_uv, sizeof(xd->dequant_uv));
409
410#if 1
411        /*TODO:  Remove dequant from BLOCKD.  This is a temporary solution until
412         * the quantizer code uses a passed in pointer to the dequant constants.
413         * This will also require modifications to the x86 and neon assembly.
414         * */
415        for (i = 0; i < 16; i++)
416            zd->block[i].dequant = zd->dequant_y1;
417        for (i = 16; i < 24; i++)
418            zd->block[i].dequant = zd->dequant_uv;
419        zd->block[24].dequant = zd->dequant_y2;
420#endif
421
422
423        vpx_memcpy(z->rd_threshes, x->rd_threshes, sizeof(x->rd_threshes));
424        vpx_memcpy(z->rd_thresh_mult, x->rd_thresh_mult,
425                   sizeof(x->rd_thresh_mult));
426
427        z->zbin_over_quant = x->zbin_over_quant;
428        z->zbin_mode_boost_enabled = x->zbin_mode_boost_enabled;
429        z->zbin_mode_boost = x->zbin_mode_boost;
430
431        vpx_memset(z->error_bins, 0, sizeof(z->error_bins));
432    }
433}
434
435void vp8cx_init_mbrthread_data(VP8_COMP *cpi,
436                               MACROBLOCK *x,
437                               MB_ROW_COMP *mbr_ei,
438                               int count
439                              )
440{
441
442    VP8_COMMON *const cm = & cpi->common;
443    MACROBLOCKD *const xd = & x->e_mbd;
444    int i;
445
446    for (i = 0; i < count; i++)
447    {
448        MACROBLOCK *mb = & mbr_ei[i].mb;
449        MACROBLOCKD *mbd = &mb->e_mbd;
450
451        mbd->subpixel_predict        = xd->subpixel_predict;
452        mbd->subpixel_predict8x4     = xd->subpixel_predict8x4;
453        mbd->subpixel_predict8x8     = xd->subpixel_predict8x8;
454        mbd->subpixel_predict16x16   = xd->subpixel_predict16x16;
455        mb->gf_active_ptr            = x->gf_active_ptr;
456
457        vpx_memset(mbr_ei[i].segment_counts, 0, sizeof(mbr_ei[i].segment_counts));
458        mbr_ei[i].totalrate = 0;
459
460        mb->partition_info = x->pi + x->e_mbd.mode_info_stride * (i + 1);
461
462        mbd->mode_info_context = cm->mi   + x->e_mbd.mode_info_stride * (i + 1);
463        mbd->mode_info_stride  = cm->mode_info_stride;
464
465        mbd->frame_type = cm->frame_type;
466
467        mb->src = * cpi->Source;
468        mbd->pre = cm->yv12_fb[cm->lst_fb_idx];
469        mbd->dst = cm->yv12_fb[cm->new_fb_idx];
470
471        mb->src.y_buffer += 16 * x->src.y_stride * (i + 1);
472        mb->src.u_buffer +=  8 * x->src.uv_stride * (i + 1);
473        mb->src.v_buffer +=  8 * x->src.uv_stride * (i + 1);
474
475        vp8_build_block_offsets(mb);
476
477        mbd->left_context = &cm->left_context;
478        mb->mvc = cm->fc.mvc;
479
480        setup_mbby_copy(&mbr_ei[i].mb, x);
481
482        mbd->fullpixel_mask = 0xffffffff;
483        if(cm->full_pixel)
484            mbd->fullpixel_mask = 0xfffffff8;
485
486        vp8_zero(mb->coef_counts);
487        vp8_zero(x->ymode_count);
488        mb->skip_true_count = 0;
489        vp8_zero(mb->MVcount);
490        mb->prediction_error = 0;
491        mb->intra_error = 0;
492        vp8_zero(mb->count_mb_ref_frame_usage);
493        mb->mbs_tested_so_far = 0;
494    }
495}
496
497int vp8cx_create_encoder_threads(VP8_COMP *cpi)
498{
499    const VP8_COMMON * cm = &cpi->common;
500
501    cpi->b_multi_threaded = 0;
502    cpi->encoding_thread_count = 0;
503    cpi->b_lpf_running = 0;
504
505    if (cm->processor_core_count > 1 && cpi->oxcf.multi_threaded > 1)
506    {
507        int ithread;
508        int th_count = cpi->oxcf.multi_threaded - 1;
509        int rc = 0;
510
511        /* don't allocate more threads than cores available */
512        if (cpi->oxcf.multi_threaded > cm->processor_core_count)
513            th_count = cm->processor_core_count - 1;
514
515        /* we have th_count + 1 (main) threads processing one row each */
516        /* no point to have more threads than the sync range allows */
517        if(th_count > ((cm->mb_cols / cpi->mt_sync_range) - 1))
518        {
519            th_count = (cm->mb_cols / cpi->mt_sync_range) - 1;
520        }
521
522        if(th_count == 0)
523            return 0;
524
525        CHECK_MEM_ERROR(cpi->h_encoding_thread,
526                        vpx_malloc(sizeof(pthread_t) * th_count));
527        CHECK_MEM_ERROR(cpi->h_event_start_encoding,
528                        vpx_malloc(sizeof(sem_t) * th_count));
529        CHECK_MEM_ERROR(cpi->mb_row_ei,
530                        vpx_memalign(32, sizeof(MB_ROW_COMP) * th_count));
531        vpx_memset(cpi->mb_row_ei, 0, sizeof(MB_ROW_COMP) * th_count);
532        CHECK_MEM_ERROR(cpi->en_thread_data,
533                        vpx_malloc(sizeof(ENCODETHREAD_DATA) * th_count));
534
535        sem_init(&cpi->h_event_end_encoding, 0, 0);
536
537        cpi->b_multi_threaded = 1;
538        cpi->encoding_thread_count = th_count;
539
540        /*
541        printf("[VP8:] multi_threaded encoding is enabled with %d threads\n\n",
542               (cpi->encoding_thread_count +1));
543        */
544
545        for (ithread = 0; ithread < th_count; ithread++)
546        {
547            ENCODETHREAD_DATA *ethd = &cpi->en_thread_data[ithread];
548
549            /* Setup block ptrs and offsets */
550            vp8_setup_block_ptrs(&cpi->mb_row_ei[ithread].mb);
551            vp8_setup_block_dptrs(&cpi->mb_row_ei[ithread].mb.e_mbd);
552
553            sem_init(&cpi->h_event_start_encoding[ithread], 0, 0);
554
555            ethd->ithread = ithread;
556            ethd->ptr1 = (void *)cpi;
557            ethd->ptr2 = (void *)&cpi->mb_row_ei[ithread];
558
559            rc = pthread_create(&cpi->h_encoding_thread[ithread], 0,
560                                thread_encoding_proc, ethd);
561            if(rc)
562                break;
563        }
564
565        if(rc)
566        {
567            /* shutdown other threads */
568            cpi->b_multi_threaded = 0;
569            for(--ithread; ithread >= 0; ithread--)
570            {
571                pthread_join(cpi->h_encoding_thread[ithread], 0);
572                sem_destroy(&cpi->h_event_start_encoding[ithread]);
573            }
574            sem_destroy(&cpi->h_event_end_encoding);
575
576            /* free thread related resources */
577            vpx_free(cpi->h_event_start_encoding);
578            vpx_free(cpi->h_encoding_thread);
579            vpx_free(cpi->mb_row_ei);
580            vpx_free(cpi->en_thread_data);
581
582            return -1;
583        }
584
585
586        {
587            LPFTHREAD_DATA * lpfthd = &cpi->lpf_thread_data;
588
589            sem_init(&cpi->h_event_start_lpf, 0, 0);
590            sem_init(&cpi->h_event_end_lpf, 0, 0);
591
592            lpfthd->ptr1 = (void *)cpi;
593            rc = pthread_create(&cpi->h_filter_thread, 0, thread_loopfilter,
594                                lpfthd);
595
596            if(rc)
597            {
598                /* shutdown other threads */
599                cpi->b_multi_threaded = 0;
600                for(--ithread; ithread >= 0; ithread--)
601                {
602                    sem_post(&cpi->h_event_start_encoding[ithread]);
603                    pthread_join(cpi->h_encoding_thread[ithread], 0);
604                    sem_destroy(&cpi->h_event_start_encoding[ithread]);
605                }
606                sem_destroy(&cpi->h_event_end_encoding);
607                sem_destroy(&cpi->h_event_end_lpf);
608                sem_destroy(&cpi->h_event_start_lpf);
609
610                /* free thread related resources */
611                vpx_free(cpi->h_event_start_encoding);
612                vpx_free(cpi->h_encoding_thread);
613                vpx_free(cpi->mb_row_ei);
614                vpx_free(cpi->en_thread_data);
615
616                return -2;
617            }
618        }
619    }
620    return 0;
621}
622
623void vp8cx_remove_encoder_threads(VP8_COMP *cpi)
624{
625    if (cpi->b_multi_threaded)
626    {
627        /* shutdown other threads */
628        cpi->b_multi_threaded = 0;
629        {
630            int i;
631
632            for (i = 0; i < cpi->encoding_thread_count; i++)
633            {
634                sem_post(&cpi->h_event_start_encoding[i]);
635                pthread_join(cpi->h_encoding_thread[i], 0);
636
637                sem_destroy(&cpi->h_event_start_encoding[i]);
638            }
639
640            sem_post(&cpi->h_event_start_lpf);
641            pthread_join(cpi->h_filter_thread, 0);
642        }
643
644        sem_destroy(&cpi->h_event_end_encoding);
645        sem_destroy(&cpi->h_event_end_lpf);
646        sem_destroy(&cpi->h_event_start_lpf);
647
648        /* free thread related resources */
649        vpx_free(cpi->h_event_start_encoding);
650        vpx_free(cpi->h_encoding_thread);
651        vpx_free(cpi->mb_row_ei);
652        vpx_free(cpi->en_thread_data);
653    }
654}
655#endif
656