static void filter_mb( H264Context *h, int mb_x, int mb_y, uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr) {

    MpegEncContext * const s = &h->s;

    const int mb_xy= mb_x + mb_y*s->mb_stride;

    int linesize, uvlinesize;

    int dir;



    /* FIXME Implement deblocking filter for field MB */

    if( h->sps.mb_aff ) {

        return;

    }

    linesize = s->linesize;

    uvlinesize = s->uvlinesize;



    /* dir : 0 -> vertical edge, 1 -> horizontal edge */

    for( dir = 0; dir < 2; dir++ )

    {

        int edge;

        const int mbm_xy = dir == 0 ? mb_xy -1 : mb_xy - s->mb_stride;

        int start = h->slice_table[mbm_xy] == 255 ? 1 : 0;



        if (h->deblocking_filter==2 && h->slice_table[mbm_xy] != h->slice_table[mb_xy])

            start = 1;



        /* Calculate bS */

        for( edge = start; edge < 4; edge++ ) {

            /* mbn_xy: neighbour macroblock (how that works for field ?) */

            int mbn_xy = edge > 0 ? mb_xy : mbm_xy;

            int bS[4];

            int qp;



            if( IS_INTRA( s->current_picture.mb_type[mb_xy] ) ||

                IS_INTRA( s->current_picture.mb_type[mbn_xy] ) ) {

                bS[0] = bS[1] = bS[2] = bS[3] = ( edge == 0 ? 4 : 3 );

            } else {

                int i;

                for( i = 0; i < 4; i++ ) {

                    int x = dir == 0 ? edge : i;

                    int y = dir == 0 ? i    : edge;

                    int b_idx= 8 + 4 + x + 8*y;

                    int bn_idx= b_idx - (dir ? 8:1);



                    if( h->non_zero_count_cache[b_idx] != 0 ||

                        h->non_zero_count_cache[bn_idx] != 0 ) {

                        bS[i] = 2;

                    }

                    else if( h->slice_type == P_TYPE ) {

                        if( h->ref_cache[0][b_idx] != h->ref_cache[0][bn_idx] ||

                            ABS( h->mv_cache[0][b_idx][0] - h->mv_cache[0][bn_idx][0] ) >= 4 ||

                            ABS( h->mv_cache[0][b_idx][1] - h->mv_cache[0][bn_idx][1] ) >= 4 )

                            bS[i] = 1;

                        else

                            bS[i] = 0;

                    } else {

                        /* FIXME Add support for B frame */

                        return;

                    }

                }



                if(bS[0]+bS[1]+bS[2]+bS[3] == 0)

                    continue;

            }



            /* Filter edge */

            // Do not use s->qscale as luma quantiser because it has not the same

            // value in IPCM macroblocks.

            qp = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[mbn_xy] + 1 ) >> 1;

            //tprintf("filter mb:%d/%d dir:%d edge:%d, QPy:%d, QPc:%d, QPcn:%d\n", mb_x, mb_y, dir, edge, qp, h->chroma_qp, s->current_picture.qscale_table[mbn_xy]);

            if( dir == 0 ) {

                filter_mb_edgev( h, &img_y[4*edge], linesize, bS, qp );

                if( (edge&1) == 0 ) {

                    int chroma_qp = ( h->chroma_qp +

                                      get_chroma_qp( h, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1;

                    filter_mb_edgecv( h, &img_cb[2*edge], uvlinesize, bS, chroma_qp );

                    filter_mb_edgecv( h, &img_cr[2*edge], uvlinesize, bS, chroma_qp );

                }

            } else {

                filter_mb_edgeh( h, &img_y[4*edge*linesize], linesize, bS, qp );

                if( (edge&1) == 0 ) {

                    int chroma_qp = ( h->chroma_qp +

                                      get_chroma_qp( h, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1;

                    filter_mb_edgech( h, &img_cb[2*edge*uvlinesize], uvlinesize, bS, chroma_qp );

                    filter_mb_edgech( h, &img_cr[2*edge*uvlinesize], uvlinesize, bS, chroma_qp );

                }

            }

        }

    }

}

static void filter_mb( H264Context *h, int mb_x, int mb_y, uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr) {

    MpegEncContext * const s = &h->s;

    const int mb_xy= mb_x + mb_y*s->mb_stride;

    int linesize, uvlinesize;

    int dir;



    

    if( h->sps.mb_aff ) {

        return;

    }

    linesize = s->linesize;

    uvlinesize = s->uvlinesize;



    

    for( dir = 0; dir < 2; dir++ )

    {

        int edge;

        const int mbm_xy = dir == 0 ? mb_xy -1 : mb_xy - s->mb_stride;

        int start = h->slice_table[mbm_xy] == 255 ? 1 : 0;



        if (h->deblocking_filter==2 && h->slice_table[mbm_xy] != h->slice_table[mb_xy])

            start = 1;



        

        for( edge = start; edge < 4; edge++ ) {

            

            int mbn_xy = edge > 0 ? mb_xy : mbm_xy;

            int bS[4];

            int qp;



            if( IS_INTRA( s->current_picture.mb_type[mb_xy] ) ||

                IS_INTRA( s->current_picture.mb_type[mbn_xy] ) ) {

                bS[0] = bS[1] = bS[2] = bS[3] = ( edge == 0 ? 4 : 3 );

            } else {

                int i;

                for( i = 0; i < 4; i++ ) {

                    int x = dir == 0 ? edge : i;

                    int y = dir == 0 ? i    : edge;

                    int b_idx= 8 + 4 + x + 8*y;

                    int bn_idx= b_idx - (dir ? 8:1);



                    if( h->non_zero_count_cache[b_idx] != 0 ||

                        h->non_zero_count_cache[bn_idx] != 0 ) {

                        bS[i] = 2;

                    }

                    else if( h->slice_type == P_TYPE ) {

                        if( h->ref_cache[0][b_idx] != h->ref_cache[0][bn_idx] ||

                            ABS( h->mv_cache[0][b_idx][0] - h->mv_cache[0][bn_idx][0] ) >= 4 ||

                            ABS( h->mv_cache[0][b_idx][1] - h->mv_cache[0][bn_idx][1] ) >= 4 )

                            bS[i] = 1;

                        else

                            bS[i] = 0;

                    } else {

                        

                        return;

                    }

                }



                if(bS[0]+bS[1]+bS[2]+bS[3] == 0)

                    continue;

            }



            

            

            

            qp = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[mbn_xy] + 1 ) >> 1;

            

            if( dir == 0 ) {

                filter_mb_edgev( h, &img_y[4*edge], linesize, bS, qp );

                if( (edge&1) == 0 ) {

                    int chroma_qp = ( h->chroma_qp +

                                      get_chroma_qp( h, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1;

                    filter_mb_edgecv( h, &img_cb[2*edge], uvlinesize, bS, chroma_qp );

                    filter_mb_edgecv( h, &img_cr[2*edge], uvlinesize, bS, chroma_qp );

                }

            } else {

                filter_mb_edgeh( h, &img_y[4*edge*linesize], linesize, bS, qp );

                if( (edge&1) == 0 ) {

                    int chroma_qp = ( h->chroma_qp +

                                      get_chroma_qp( h, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1;

                    filter_mb_edgech( h, &img_cb[2*edge*uvlinesize], uvlinesize, bS, chroma_qp );

                    filter_mb_edgech( h, &img_cr[2*edge*uvlinesize], uvlinesize, bS, chroma_qp );

                }

            }

        }

    }

}

static void FUNC_0( H264Context *VAR_0, int VAR_1, int VAR_2, uint8_t *VAR_3, uint8_t *VAR_4, uint8_t *VAR_5) {

    MpegEncContext * const s = &VAR_0->s;

    const int VAR_6= VAR_1 + VAR_2*s->mb_stride;

    int VAR_7, VAR_8;

    int VAR_9;



    

    if( VAR_0->sps.mb_aff ) {

        return;

    }

    VAR_7 = s->VAR_7;

    VAR_8 = s->VAR_8;



    

    for( VAR_9 = 0; VAR_9 < 2; VAR_9++ )

    {

        int VAR_10;

        const int VAR_11 = VAR_9 == 0 ? VAR_6 -1 : VAR_6 - s->mb_stride;

        int VAR_12 = VAR_0->slice_table[VAR_11] == 255 ? 1 : 0;



        if (VAR_0->deblocking_filter==2 && VAR_0->slice_table[VAR_11] != VAR_0->slice_table[VAR_6])

            VAR_12 = 1;



        

        for( VAR_10 = VAR_12; VAR_10 < 4; VAR_10++ ) {

            

            int VAR_13 = VAR_10 > 0 ? VAR_6 : VAR_11;

            int VAR_14[4];

            int VAR_15;



            if( IS_INTRA( s->current_picture.mb_type[VAR_6] ) ||

                IS_INTRA( s->current_picture.mb_type[VAR_13] ) ) {

                VAR_14[0] = VAR_14[1] = VAR_14[2] = VAR_14[3] = ( VAR_10 == 0 ? 4 : 3 );

            } else {

                int VAR_16;

                for( VAR_16 = 0; VAR_16 < 4; VAR_16++ ) {

                    int VAR_17 = VAR_9 == 0 ? VAR_10 : VAR_16;

                    int VAR_18 = VAR_9 == 0 ? VAR_16    : VAR_10;

                    int VAR_19= 8 + 4 + VAR_17 + 8*VAR_18;

                    int VAR_20= VAR_19 - (VAR_9 ? 8:1);



                    if( VAR_0->non_zero_count_cache[VAR_19] != 0 ||

                        VAR_0->non_zero_count_cache[VAR_20] != 0 ) {

                        VAR_14[VAR_16] = 2;

                    }

                    else if( VAR_0->slice_type == P_TYPE ) {

                        if( VAR_0->ref_cache[0][VAR_19] != VAR_0->ref_cache[0][VAR_20] ||

                            ABS( VAR_0->mv_cache[0][VAR_19][0] - VAR_0->mv_cache[0][VAR_20][0] ) >= 4 ||

                            ABS( VAR_0->mv_cache[0][VAR_19][1] - VAR_0->mv_cache[0][VAR_20][1] ) >= 4 )

                            VAR_14[VAR_16] = 1;

                        else

                            VAR_14[VAR_16] = 0;

                    } else {

                        

                        return;

                    }

                }



                if(VAR_14[0]+VAR_14[1]+VAR_14[2]+VAR_14[3] == 0)

                    continue;

            }



            

            

            

            VAR_15 = ( s->current_picture.qscale_table[VAR_6] + s->current_picture.qscale_table[VAR_13] + 1 ) >> 1;

            

            if( VAR_9 == 0 ) {

                filter_mb_edgev( VAR_0, &VAR_3[4*VAR_10], VAR_7, VAR_14, VAR_15 );

                if( (VAR_10&1) == 0 ) {

                    int VAR_22 = ( VAR_0->VAR_22 +

                                      get_chroma_qp( VAR_0, s->current_picture.qscale_table[VAR_13] ) + 1 ) >> 1;

                    filter_mb_edgecv( VAR_0, &VAR_4[2*VAR_10], VAR_8, VAR_14, VAR_22 );

                    filter_mb_edgecv( VAR_0, &VAR_5[2*VAR_10], VAR_8, VAR_14, VAR_22 );

                }

            } else {

                filter_mb_edgeh( VAR_0, &VAR_3[4*VAR_10*VAR_7], VAR_7, VAR_14, VAR_15 );

                if( (VAR_10&1) == 0 ) {

                    int VAR_22 = ( VAR_0->VAR_22 +

                                      get_chroma_qp( VAR_0, s->current_picture.qscale_table[VAR_13] ) + 1 ) >> 1;

                    filter_mb_edgech( VAR_0, &VAR_4[2*VAR_10*VAR_8], VAR_8, VAR_14, VAR_22 );

                    filter_mb_edgech( VAR_0, &VAR_5[2*VAR_10*VAR_8], VAR_8, VAR_14, VAR_22 );

                }

            }

        }

    }

}

static void FUNC_0( H264Context *VAR_0, int VAR_1, int VAR_2, uint8_t *VAR_3, uint8_t *VAR_4, uint8_t *VAR_5) { MpegEncContext * const s = &VAR_0->s; const int VAR_6= VAR_1 + VAR_2*s->mb_stride; int VAR_7, VAR_8; int VAR_9; if( VAR_0->sps.mb_aff ) { return; } VAR_7 = s->VAR_7; VAR_8 = s->VAR_8; for( VAR_9 = 0; VAR_9 < 2; VAR_9++ ) { int VAR_10; const int VAR_11 = VAR_9 == 0 ? VAR_6 -1 : VAR_6 - s->mb_stride; int VAR_12 = VAR_0->slice_table[VAR_11] == 255 ? 1 : 0; if (VAR_0->deblocking_filter==2 && VAR_0->slice_table[VAR_11] != VAR_0->slice_table[VAR_6])
VAR_12 = 1; for( VAR_10 = VAR_12; VAR_10 < 4; VAR_10++ ) { int VAR_13 = VAR_10 > 0 ? VAR_6 : VAR_11; int VAR_14[4]; int VAR_15; if( IS_INTRA( s->current_picture.mb_type[VAR_6] ) ||
IS_INTRA( s->current_picture.mb_type[VAR_13] ) ) { VAR_14[0] = VAR_14[1] = VAR_14[2] = VAR_14[3] = ( VAR_10 == 0 ? 4 : 3 ); } else { int VAR_16; for( VAR_16 = 0; VAR_16 < 4; VAR_16++ ) { int VAR_17 = VAR_9 == 0 ? VAR_10 : VAR_16; int VAR_18 = VAR_9 == 0 ? VAR_16    : VAR_10; int VAR_19= 8 + 4 + VAR_17 + 8*VAR_18; int VAR_20= VAR_19 - (VAR_9 ? 8:1); if( VAR_0->non_zero_count_cache[VAR_19] != 0 ||
VAR_0->non_zero_count_cache[VAR_20] != 0 ) { VAR_14[VAR_16] = 2; } else if( VAR_0->slice_type == P_TYPE ) { if( VAR_0->ref_cache[0][VAR_19] != VAR_0->ref_cache[0][VAR_20] ||
ABS( VAR_0->mv_cache[0][VAR_19][0] - VAR_0->mv_cache[0][VAR_20][0] ) >= 4 ||
ABS( VAR_0->mv_cache[0][VAR_19][1] - VAR_0->mv_cache[0][VAR_20][1] ) >= 4 )
VAR_14[VAR_16] = 1; else
VAR_14[VAR_16] = 0; } else { return; } } if(VAR_14[0]+VAR_14[1]+VAR_14[2]+VAR_14[3] == 0)
continue; } VAR_15 = ( s->current_picture.qscale_table[VAR_6] + s->current_picture.qscale_table[VAR_13] + 1 ) >> 1; if( VAR_9 == 0 ) { filter_mb_edgev( VAR_0, &VAR_3[4*VAR_10], VAR_7, VAR_14, VAR_15 ); if( (VAR_10&1) == 0 ) { int VAR_22 = ( VAR_0->VAR_22 +
get_chroma_qp( VAR_0, s->current_picture.qscale_table[VAR_13] ) + 1 ) >> 1; filter_mb_edgecv( VAR_0, &VAR_4[2*VAR_10], VAR_8, VAR_14, VAR_22 ); filter_mb_edgecv( VAR_0, &VAR_5[2*VAR_10], VAR_8, VAR_14, VAR_22 ); } } else { filter_mb_edgeh( VAR_0, &VAR_3[4*VAR_10*VAR_7], VAR_7, VAR_14, VAR_15 ); if( (VAR_10&1) == 0 ) { int VAR_22 = ( VAR_0->VAR_22 +
get_chroma_qp( VAR_0, s->current_picture.qscale_table[VAR_13] ) + 1 ) >> 1; filter_mb_edgech( VAR_0, &VAR_4[2*VAR_10*VAR_8], VAR_8, VAR_14, VAR_22 ); filter_mb_edgech( VAR_0, &VAR_5[2*VAR_10*VAR_8], VAR_8, VAR_14, VAR_22 ); } } } } }