~ubuntu-dev/mplayer/ubuntu-feisty

« back to all changes in this revision

Viewing changes to libavcodec/h264.c

  • Committer: William Grant
  • Date: 2007-02-03 03:16:07 UTC
  • mto: This revision was merged to the branch mainline in revision 16.
  • Revision ID: william.grant@ubuntu.org.au-20070203031607-08gc2ompbz6spt9i
Update to 1.0rc1.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 * H.26L/H.264/AVC/JVT/14496-10/... encoder/decoder
3
3
 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
4
4
 *
5
 
 * This library is free software; you can redistribute it and/or
 
5
 * This file is part of FFmpeg.
 
6
 *
 
7
 * FFmpeg is free software; you can redistribute it and/or
6
8
 * modify it under the terms of the GNU Lesser General Public
7
9
 * License as published by the Free Software Foundation; either
8
 
 * version 2 of the License, or (at your option) any later version.
 
10
 * version 2.1 of the License, or (at your option) any later version.
9
11
 *
10
 
 * This library is distributed in the hope that it will be useful,
 
12
 * FFmpeg is distributed in the hope that it will be useful,
11
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
15
 * Lesser General Public License for more details.
14
16
 *
15
17
 * You should have received a copy of the GNU Lesser General Public
16
 
 * License along with this library; if not, write to the Free Software
 
18
 * License along with FFmpeg; if not, write to the Free Software
17
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
20
 *
19
21
 */
371
373
 
372
374
    /* 0x100 -> non null luma_dc, 0x80/0x40 -> non null chroma_dc (cb/cr), 0x?0 -> chroma_cbp(0,1,2), 0x0? luma_cbp */
373
375
    uint16_t     *cbp_table;
 
376
    int cbp;
374
377
    int top_cbp;
375
378
    int left_cbp;
376
379
    /* chroma_pred_mode for i4x4 or i16x16, else 0 */
409
412
static void svq3_luma_dc_dequant_idct_c(DCTELEM *block, int qp);
410
413
static void svq3_add_idct_c(uint8_t *dst, DCTELEM *block, int stride, int qp, int dc);
411
414
static void filter_mb( H264Context *h, int mb_x, int mb_y, uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr, unsigned int linesize, unsigned int uvlinesize);
 
415
static void filter_mb_fast( H264Context *h, int mb_x, int mb_y, uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr, unsigned int linesize, unsigned int uvlinesize);
412
416
 
413
417
static always_inline uint32_t pack16to32(int a, int b){
414
418
#ifdef WORDS_BIGENDIAN
514
518
    int left_block[8];
515
519
    int i;
516
520
 
517
 
    //FIXME deblocking can skip fill_caches much of the time with multiple slices too.
518
 
    // the actual condition is whether we're on the edge of a slice,
519
 
    // and even then the intra and nnz parts are unnecessary.
520
 
    if(for_deblock && h->slice_num == 1 && !FRAME_MBAFF)
 
521
    //FIXME deblocking could skip the intra and nnz parts.
 
522
    if(for_deblock && (h->slice_num == 1 || h->slice_table[mb_xy] == h->slice_table[mb_xy-s->mb_stride]) && !FRAME_MBAFF)
521
523
        return;
522
524
 
523
525
    //wow what a mess, why didn't they simplify the interlacing&intra stuff, i can't imagine that these complex rules are worth it
604
606
    h->left_mb_xy[0] = left_xy[0];
605
607
    h->left_mb_xy[1] = left_xy[1];
606
608
    if(for_deblock){
607
 
        topleft_type = h->slice_table[topleft_xy ] < 255 ? s->current_picture.mb_type[topleft_xy] : 0;
 
609
        topleft_type = 0;
 
610
        topright_type = 0;
608
611
        top_type     = h->slice_table[top_xy     ] < 255 ? s->current_picture.mb_type[top_xy]     : 0;
609
 
        topright_type= h->slice_table[topright_xy] < 255 ? s->current_picture.mb_type[topright_xy]: 0;
610
612
        left_type[0] = h->slice_table[left_xy[0] ] < 255 ? s->current_picture.mb_type[left_xy[0]] : 0;
611
613
        left_type[1] = h->slice_table[left_xy[1] ] < 255 ? s->current_picture.mb_type[left_xy[1]] : 0;
612
614
 
619
621
                if(USES_LIST(mb_type,list)){
620
622
                    uint32_t *src = (uint32_t*)s->current_picture.motion_val[list][h->mb2b_xy[mb_xy]];
621
623
                    uint32_t *dst = (uint32_t*)h->mv_cache[list][scan8[0]];
622
 
                    uint8_t *ref = &s->current_picture.ref_index[list][h->mb2b8_xy[mb_xy]];
 
624
                    int8_t *ref = &s->current_picture.ref_index[list][h->mb2b8_xy[mb_xy]];
623
625
                    for(i=0; i<4; i++, dst+=8, src+=h->b_stride){
624
626
                        dst[0] = src[0];
625
627
                        dst[1] = src[1];
781
783
    }
782
784
 
783
785
#if 1
784
 
    //FIXME direct mb can skip much of this
785
786
    if(IS_INTER(mb_type) || IS_DIRECT(mb_type)){
786
787
        int list;
787
788
        for(list=0; list<1+(h->slice_type==B_TYPE); list++){
844
845
                assert((!left_type[0]) == (!left_type[1]));
845
846
            }
846
847
 
847
 
            if(for_deblock || (IS_DIRECT(mb_type) && !h->direct_spatial_mv_pred))
 
848
            if((for_deblock || (IS_DIRECT(mb_type) && !h->direct_spatial_mv_pred)) && !FRAME_MBAFF)
848
849
                continue;
849
850
 
850
851
            if(USES_LIST(topleft_type, list)){
867
868
                h->ref_cache[list][scan8[0] + 4 - 1*8]= topright_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;
868
869
            }
869
870
 
 
871
            if((IS_SKIP(mb_type) || IS_DIRECT(mb_type)) && !FRAME_MBAFF)
 
872
                continue;
870
873
 
871
874
            h->ref_cache[list][scan8[5 ]+1] =
872
875
            h->ref_cache[list][scan8[7 ]+1] =
1132
1135
     * make mbaff happy, so we can't move all this logic to fill_caches */
1133
1136
    if(FRAME_MBAFF){
1134
1137
        MpegEncContext *s = &h->s;
1135
 
        const int *mb_types = s->current_picture_ptr->mb_type;
 
1138
        const uint32_t *mb_types = s->current_picture_ptr->mb_type;
1136
1139
        const int16_t *mv;
1137
1140
        *(uint32_t*)h->mv_cache[list][scan8[0]-2] = 0;
1138
1141
        *C = h->mv_cache[list][scan8[0]-2];
1340
1343
            h->dist_scale_factor[i] = 256;
1341
1344
        }else{
1342
1345
            int tb = clip(poc - poc0, -128, 127);
1343
 
            int tx = (16384 + (ABS(td) >> 1)) / td;
 
1346
            int tx = (16384 + (FFABS(td) >> 1)) / td;
1344
1347
            h->dist_scale_factor[i] = clip((tb*tx + 32) >> 6, -1024, 1023);
1345
1348
        }
1346
1349
    }
1471
1474
            fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, (uint8_t)ref[0], 1);
1472
1475
            fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, (uint8_t)ref[1], 1);
1473
1476
            if(!IS_INTRA(mb_type_col)
1474
 
               && (   (l1ref0[0] == 0 && ABS(l1mv0[0][0]) <= 1 && ABS(l1mv0[0][1]) <= 1)
1475
 
                   || (l1ref0[0]  < 0 && l1ref1[0] == 0 && ABS(l1mv1[0][0]) <= 1 && ABS(l1mv1[0][1]) <= 1
 
1477
               && (   (l1ref0[0] == 0 && FFABS(l1mv0[0][0]) <= 1 && FFABS(l1mv0[0][1]) <= 1)
 
1478
                   || (l1ref0[0]  < 0 && l1ref1[0] == 0 && FFABS(l1mv1[0][0]) <= 1 && FFABS(l1mv1[0][1]) <= 1
1476
1479
                       && (h->x264_build>33 || !h->x264_build)))){
1477
1480
                if(ref[0] > 0)
1478
1481
                    fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mv[0][0],mv[0][1]), 4);
1507
1510
                    const int16_t (*l1mv)[2]= l1ref0[x8 + y8*h->b8_stride] == 0 ? l1mv0 : l1mv1;
1508
1511
                    if(IS_SUB_8X8(sub_mb_type)){
1509
1512
                        const int16_t *mv_col = l1mv[x8*3 + y8*3*h->b_stride];
1510
 
                        if(ABS(mv_col[0]) <= 1 && ABS(mv_col[1]) <= 1){
 
1513
                        if(FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1){
1511
1514
                            if(ref[0] == 0)
1512
1515
                                fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
1513
1516
                            if(ref[1] == 0)
1516
1519
                    }else
1517
1520
                    for(i4=0; i4<4; i4++){
1518
1521
                        const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*h->b_stride];
1519
 
                        if(ABS(mv_col[0]) <= 1 && ABS(mv_col[1]) <= 1){
 
1522
                        if(FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1){
1520
1523
                            if(ref[0] == 0)
1521
1524
                                *(uint32_t*)h->mv_cache[0][scan8[i8*4+i4]] = 0;
1522
1525
                            if(ref[1] == 0)
1713
1716
            *(uint64_t*)s->current_picture.motion_val[list][b_xy + 2 + y*h->b_stride]= *(uint64_t*)h->mv_cache[list][scan8[0]+2 + 8*y];
1714
1717
        }
1715
1718
        if( h->pps.cabac ) {
 
1719
            if(IS_SKIP(mb_type))
 
1720
                fill_rectangle(h->mvd_table[list][b_xy], 4, 4, h->b_stride, 0, 4);
 
1721
            else
1716
1722
            for(y=0; y<4; y++){
1717
1723
                *(uint64_t*)h->mvd_table[list][b_xy + 0 + y*h->b_stride]= *(uint64_t*)h->mvd_cache[list][scan8[0]+0 + 8*y];
1718
1724
                *(uint64_t*)h->mvd_table[list][b_xy + 2 + y*h->b_stride]= *(uint64_t*)h->mvd_cache[list][scan8[0]+2 + 8*y];
1720
1726
        }
1721
1727
 
1722
1728
        {
1723
 
            uint8_t *ref_index = &s->current_picture.ref_index[list][b8_xy];
 
1729
            int8_t *ref_index = &s->current_picture.ref_index[list][b8_xy];
1724
1730
            ref_index[0+0*h->b8_stride]= h->ref_cache[list][scan8[0]];
1725
1731
            ref_index[1+0*h->b8_stride]= h->ref_cache[list][scan8[4]];
1726
1732
            ref_index[0+1*h->b8_stride]= h->ref_cache[list][scan8[8]];
3633
3639
    dest_cb = s->current_picture.data[1] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
3634
3640
    dest_cr = s->current_picture.data[2] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
3635
3641
 
 
3642
    s->dsp.prefetch(dest_y + (s->mb_x&3)*4*s->linesize + 64, s->linesize, 4);
 
3643
    s->dsp.prefetch(dest_cb + (s->mb_x&7)*s->uvlinesize + 64, dest_cr - dest_cb, 2);
 
3644
 
3636
3645
    if (MB_FIELD) {
3637
3646
        linesize   = h->mb_linesize   = s->linesize * 2;
3638
3647
        uvlinesize = h->mb_uvlinesize = s->uvlinesize * 2;
3781
3790
                xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 0);
3782
3791
        }else if(s->codec_id == CODEC_ID_H264){
3783
3792
            hl_motion(h, dest_y, dest_cb, dest_cr,
3784
 
                      s->dsp.put_h264_qpel_pixels_tab, s->dsp.put_h264_chroma_pixels_tab,
3785
 
                      s->dsp.avg_h264_qpel_pixels_tab, s->dsp.avg_h264_chroma_pixels_tab,
 
3793
                      s->me.qpel_put, s->dsp.put_h264_chroma_pixels_tab,
 
3794
                      s->me.qpel_avg, s->dsp.avg_h264_chroma_pixels_tab,
3786
3795
                      s->dsp.weight_h264_pixels_tab, s->dsp.biweight_h264_pixels_tab);
3787
3796
        }
3788
3797
 
3880
3889
            tprintf("call filter_mb\n");
3881
3890
            backup_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
3882
3891
            fill_caches(h, mb_type, 1); //FIXME don't fill stuff which isn't used by filter_mb
3883
 
            filter_mb(h, mb_x, mb_y, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
 
3892
            filter_mb_fast(h, mb_x, mb_y, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
3884
3893
        }
3885
3894
    }
3886
3895
}
4099
4108
    return 0;
4100
4109
}
4101
4110
 
4102
 
static int fill_mbaff_ref_list(H264Context *h){
 
4111
static void fill_mbaff_ref_list(H264Context *h){
4103
4112
    int list, i, j;
4104
4113
    for(list=0; list<2; list++){
4105
4114
        for(i=0; i<h->ref_count[list]; i++){
4204
4213
            int td = clip(poc1 - poc0, -128, 127);
4205
4214
            if(td){
4206
4215
                int tb = clip(cur_poc - poc0, -128, 127);
4207
 
                int tx = (16384 + (ABS(td) >> 1)) / td;
 
4216
                int tx = (16384 + (FFABS(td) >> 1)) / td;
4208
4217
                int dist_scale_factor = clip((tb*tx + 32) >> 6, -1024, 1023) >> 2;
4209
4218
                if(dist_scale_factor < -64 || dist_scale_factor > 128)
4210
4219
                    h->implicit_weight[ref0][ref1] = 32;
4884
4893
               );
4885
4894
    }
4886
4895
 
 
4896
    if((s->avctx->flags2 & CODEC_FLAG2_FAST) && !s->current_picture.reference){
 
4897
        s->me.qpel_put= s->dsp.put_2tap_qpel_pixels_tab;
 
4898
        s->me.qpel_avg= s->dsp.avg_2tap_qpel_pixels_tab;
 
4899
    }else{
 
4900
        s->me.qpel_put= s->dsp.put_h264_qpel_pixels_tab;
 
4901
        s->me.qpel_avg= s->dsp.avg_h264_qpel_pixels_tab;
 
4902
    }
 
4903
 
4887
4904
    return 0;
4888
4905
}
4889
4906
 
5101
5118
 
5102
5119
        fill_caches(h, mb_type, 0); //FIXME check what is needed and what not ...
5103
5120
        pred_direct_motion(h, &mb_type);
5104
 
        if(h->pps.cabac){
5105
 
            fill_rectangle(h->mvd_cache[0][scan8[0]], 4, 4, 8, 0, 4);
5106
 
            fill_rectangle(h->mvd_cache[1][scan8[0]], 4, 4, 8, 0, 4);
5107
 
        }
 
5121
        mb_type|= MB_TYPE_SKIP;
5108
5122
    }
5109
5123
    else
5110
5124
    {
5115
5129
        pred_pskip_motion(h, &mx, &my);
5116
5130
        fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, 0, 1);
5117
5131
        fill_rectangle(  h->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mx,my), 4);
5118
 
        if(h->pps.cabac)
5119
 
            fill_rectangle(h->mvd_cache[0][scan8[0]], 4, 4, 8, 0, 4);
5120
5132
    }
5121
5133
 
5122
5134
    write_back_motion(h, mb_type);
5123
 
    s->current_picture.mb_type[mb_xy]= mb_type|MB_TYPE_SKIP;
 
5135
    s->current_picture.mb_type[mb_xy]= mb_type;
5124
5136
    s->current_picture.qscale_table[mb_xy]= s->qscale;
5125
5137
    h->slice_table[ mb_xy ]= h->slice_num;
5126
5138
    h->prev_mb_skipped= 1;
5185
5197
       assert(h->slice_type == I_TYPE);
5186
5198
decode_intra_mb:
5187
5199
        if(mb_type > 25){
5188
 
            av_log(h->s.avctx, AV_LOG_ERROR, "mb_type %d in %c slice to large at %d %d\n", mb_type, av_get_pict_type_char(h->slice_type), s->mb_x, s->mb_y);
 
5200
            av_log(h->s.avctx, AV_LOG_ERROR, "mb_type %d in %c slice too large at %d %d\n", mb_type, av_get_pict_type_char(h->slice_type), s->mb_x, s->mb_y);
5189
5201
            return -1;
5190
5202
        }
5191
5203
        partition_count=0;
5258
5270
 
5259
5271
//                fill_intra4x4_pred_table(h);
5260
5272
                for(i=0; i<16; i+=di){
5261
 
                    const int mode_coded= !get_bits1(&s->gb);
5262
 
                    const int predicted_mode=  pred_intra_mode(h, i);
5263
 
                    int mode;
 
5273
                    int mode= pred_intra_mode(h, i);
5264
5274
 
5265
 
                    if(mode_coded){
 
5275
                    if(!get_bits1(&s->gb)){
5266
5276
                        const int rem_mode= get_bits(&s->gb, 3);
5267
 
                        if(rem_mode<predicted_mode)
5268
 
                            mode= rem_mode;
5269
 
                        else
5270
 
                            mode= rem_mode + 1;
5271
 
                    }else{
5272
 
                        mode= predicted_mode;
 
5277
                        mode = rem_mode + (rem_mode >= mode);
5273
5278
                    }
5274
5279
 
5275
5280
                    if(di==4)
5486
5491
        else
5487
5492
            cbp= golomb_to_inter_cbp[cbp];
5488
5493
    }
 
5494
    h->cbp = cbp;
5489
5495
 
5490
5496
    if(dct8x8_allowed && (cbp&15) && !IS_INTRA(mb_type)){
5491
5497
        if(get_bits1(&s->gb))
5627
5633
        ctx += 1;
5628
5634
    }
5629
5635
 
5630
 
    return get_cabac( &h->cabac, &h->cabac_state[70 + ctx] );
 
5636
    return get_cabac_noinline( &h->cabac, &h->cabac_state[70 + ctx] );
5631
5637
}
5632
5638
 
5633
5639
static int decode_cabac_intra_mb_type(H264Context *h, int ctx_base, int intra_slice) {
5643
5649
            ctx++;
5644
5650
        if( h->slice_table[mbb_xy] == h->slice_num && !IS_INTRA4x4( s->current_picture.mb_type[mbb_xy] ) )
5645
5651
            ctx++;
5646
 
        if( get_cabac( &h->cabac, &state[ctx] ) == 0 )
 
5652
        if( get_cabac_noinline( &h->cabac, &state[ctx] ) == 0 )
5647
5653
            return 0;   /* I4x4 */
5648
5654
        state += 2;
5649
5655
    }else{
5650
 
        if( get_cabac( &h->cabac, &state[0] ) == 0 )
 
5656
        if( get_cabac_noinline( &h->cabac, &state[0] ) == 0 )
5651
5657
            return 0;   /* I4x4 */
5652
5658
    }
5653
5659
 
5655
5661
        return 25;  /* PCM */
5656
5662
 
5657
5663
    mb_type = 1; /* I16x16 */
5658
 
    mb_type += 12 * get_cabac( &h->cabac, &state[1] ); /* cbp_luma != 0 */
5659
 
    if( get_cabac( &h->cabac, &state[2] ) ) /* cbp_chroma */
5660
 
        mb_type += 4 + 4 * get_cabac( &h->cabac, &state[2+intra_slice] );
5661
 
    mb_type += 2 * get_cabac( &h->cabac, &state[3+intra_slice] );
5662
 
    mb_type += 1 * get_cabac( &h->cabac, &state[3+2*intra_slice] );
 
5664
    mb_type += 12 * get_cabac_noinline( &h->cabac, &state[1] ); /* cbp_luma != 0 */
 
5665
    if( get_cabac_noinline( &h->cabac, &state[2] ) ) /* cbp_chroma */
 
5666
        mb_type += 4 + 4 * get_cabac_noinline( &h->cabac, &state[2+intra_slice] );
 
5667
    mb_type += 2 * get_cabac_noinline( &h->cabac, &state[3+intra_slice] );
 
5668
    mb_type += 1 * get_cabac_noinline( &h->cabac, &state[3+2*intra_slice] );
5663
5669
    return mb_type;
5664
5670
}
5665
5671
 
5669
5675
    if( h->slice_type == I_TYPE ) {
5670
5676
        return decode_cabac_intra_mb_type(h, 3, 1);
5671
5677
    } else if( h->slice_type == P_TYPE ) {
5672
 
        if( get_cabac( &h->cabac, &h->cabac_state[14] ) == 0 ) {
 
5678
        if( get_cabac_noinline( &h->cabac, &h->cabac_state[14] ) == 0 ) {
5673
5679
            /* P-type */
5674
 
            if( get_cabac( &h->cabac, &h->cabac_state[15] ) == 0 ) {
 
5680
            if( get_cabac_noinline( &h->cabac, &h->cabac_state[15] ) == 0 ) {
5675
5681
                /* P_L0_D16x16, P_8x8 */
5676
 
                return 3 * get_cabac( &h->cabac, &h->cabac_state[16] );
 
5682
                return 3 * get_cabac_noinline( &h->cabac, &h->cabac_state[16] );
5677
5683
            } else {
5678
5684
                /* P_L0_D8x16, P_L0_D16x8 */
5679
 
                return 2 - get_cabac( &h->cabac, &h->cabac_state[17] );
 
5685
                return 2 - get_cabac_noinline( &h->cabac, &h->cabac_state[17] );
5680
5686
            }
5681
5687
        } else {
5682
5688
            return decode_cabac_intra_mb_type(h, 17, 0) + 5;
5692
5698
        if( h->slice_table[mbb_xy] == h->slice_num && !IS_DIRECT( s->current_picture.mb_type[mbb_xy] ) )
5693
5699
            ctx++;
5694
5700
 
5695
 
        if( !get_cabac( &h->cabac, &h->cabac_state[27+ctx] ) )
 
5701
        if( !get_cabac_noinline( &h->cabac, &h->cabac_state[27+ctx] ) )
5696
5702
            return 0; /* B_Direct_16x16 */
5697
5703
 
5698
 
        if( !get_cabac( &h->cabac, &h->cabac_state[27+3] ) ) {
5699
 
            return 1 + get_cabac( &h->cabac, &h->cabac_state[27+5] ); /* B_L[01]_16x16 */
 
5704
        if( !get_cabac_noinline( &h->cabac, &h->cabac_state[27+3] ) ) {
 
5705
            return 1 + get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] ); /* B_L[01]_16x16 */
5700
5706
        }
5701
5707
 
5702
 
        bits = get_cabac( &h->cabac, &h->cabac_state[27+4] ) << 3;
5703
 
        bits|= get_cabac( &h->cabac, &h->cabac_state[27+5] ) << 2;
5704
 
        bits|= get_cabac( &h->cabac, &h->cabac_state[27+5] ) << 1;
5705
 
        bits|= get_cabac( &h->cabac, &h->cabac_state[27+5] );
 
5708
        bits = get_cabac_noinline( &h->cabac, &h->cabac_state[27+4] ) << 3;
 
5709
        bits|= get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] ) << 2;
 
5710
        bits|= get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] ) << 1;
 
5711
        bits|= get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] );
5706
5712
        if( bits < 8 )
5707
5713
            return bits + 3; /* B_Bi_16x16 through B_L1_L0_16x8 */
5708
5714
        else if( bits == 13 ) {
5712
5718
        else if( bits == 15 )
5713
5719
            return 22; /* B_8x8 */
5714
5720
 
5715
 
        bits= ( bits<<1 ) | get_cabac( &h->cabac, &h->cabac_state[27+5] );
 
5721
        bits= ( bits<<1 ) | get_cabac_noinline( &h->cabac, &h->cabac_state[27+5] );
5716
5722
        return bits - 4; /* B_L0_Bi_* through B_Bi_Bi_* */
5717
5723
    } else {
5718
5724
        /* TODO SI/SP frames? */
5753
5759
 
5754
5760
    if( h->slice_type == B_TYPE )
5755
5761
        ctx += 13;
5756
 
    return get_cabac( &h->cabac, &h->cabac_state[11+ctx] );
 
5762
    return get_cabac_noinline( &h->cabac, &h->cabac_state[11+ctx] );
5757
5763
}
5758
5764
 
5759
5765
static int decode_cabac_mb_intra4x4_pred_mode( H264Context *h, int pred_mode ) {
5785
5791
    if( h->slice_table[mbb_xy] == h->slice_num && h->chroma_pred_mode_table[mbb_xy] != 0 )
5786
5792
        ctx++;
5787
5793
 
5788
 
    if( get_cabac( &h->cabac, &h->cabac_state[64+ctx] ) == 0 )
 
5794
    if( get_cabac_noinline( &h->cabac, &h->cabac_state[64+ctx] ) == 0 )
5789
5795
        return 0;
5790
5796
 
5791
 
    if( get_cabac( &h->cabac, &h->cabac_state[64+3] ) == 0 )
 
5797
    if( get_cabac_noinline( &h->cabac, &h->cabac_state[64+3] ) == 0 )
5792
5798
        return 1;
5793
 
    if( get_cabac( &h->cabac, &h->cabac_state[64+3] ) == 0 )
 
5799
    if( get_cabac_noinline( &h->cabac, &h->cabac_state[64+3] ) == 0 )
5794
5800
        return 2;
5795
5801
    else
5796
5802
        return 3;
5867
5873
    ctx = 0;
5868
5874
    if( cbp_a > 0 ) ctx++;
5869
5875
    if( cbp_b > 0 ) ctx += 2;
5870
 
    if( get_cabac( &h->cabac, &h->cabac_state[77 + ctx] ) == 0 )
 
5876
    if( get_cabac_noinline( &h->cabac, &h->cabac_state[77 + ctx] ) == 0 )
5871
5877
        return 0;
5872
5878
 
5873
5879
    ctx = 4;
5874
5880
    if( cbp_a == 2 ) ctx++;
5875
5881
    if( cbp_b == 2 ) ctx += 2;
5876
 
    return 1 + get_cabac( &h->cabac, &h->cabac_state[77 + ctx] );
 
5882
    return 1 + get_cabac_noinline( &h->cabac, &h->cabac_state[77 + ctx] );
5877
5883
}
5878
5884
static int decode_cabac_mb_dqp( H264Context *h) {
5879
5885
    MpegEncContext * const s = &h->s;
5889
5895
    if( h->last_qscale_diff != 0 )
5890
5896
        ctx++;
5891
5897
 
5892
 
    while( get_cabac( &h->cabac, &h->cabac_state[60 + ctx] ) ) {
 
5898
    while( get_cabac_noinline( &h->cabac, &h->cabac_state[60 + ctx] ) ) {
5893
5899
        if( ctx < 2 )
5894
5900
            ctx = 2;
5895
5901
        else
5931
5937
}
5932
5938
 
5933
5939
static inline int decode_cabac_mb_transform_size( H264Context *h ) {
5934
 
    return get_cabac( &h->cabac, &h->cabac_state[399 + h->neighbor_transform_size] );
 
5940
    return get_cabac_noinline( &h->cabac, &h->cabac_state[399 + h->neighbor_transform_size] );
5935
5941
}
5936
5942
 
5937
5943
static int decode_cabac_mb_ref( H264Context *h, int list, int n ) {
5997
6003
                mvd += 1 << k;
5998
6004
        }
5999
6005
    }
6000
 
    if( get_cabac_bypass( &h->cabac ) )  return -mvd;
6001
 
    else                                 return  mvd;
 
6006
    return get_cabac_bypass_sign( &h->cabac, -mvd );
6002
6007
}
6003
6008
 
6004
6009
static int inline get_cabac_cbf_ctx( H264Context *h, int cat, int idx ) {
6029
6034
    return ctx + 4 * cat;
6030
6035
}
6031
6036
 
 
6037
static const __attribute((used)) uint8_t last_coeff_flag_offset_8x8[63] = {
 
6038
    0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
 
6039
    2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
 
6040
    3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
 
6041
    5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8
 
6042
};
 
6043
 
6032
6044
static int decode_cabac_residual( H264Context *h, DCTELEM *block, int cat, int n, const uint8_t *scantable, const uint32_t *qmul, int max_coeff) {
6033
6045
    const int mb_xy  = h->s.mb_x + h->s.mb_y*h->s.mb_stride;
6034
6046
    static const int significant_coeff_flag_offset[2][6] = {
6042
6054
    static const int coeff_abs_level_m1_offset[6] = {
6043
6055
        227+0, 227+10, 227+20, 227+30, 227+39, 426
6044
6056
    };
6045
 
    static const int significant_coeff_flag_offset_8x8[2][63] = {
 
6057
    static const uint8_t significant_coeff_flag_offset_8x8[2][63] = {
6046
6058
      { 0, 1, 2, 3, 4, 5, 5, 4, 4, 3, 3, 4, 4, 4, 5, 5,
6047
6059
        4, 4, 4, 4, 3, 3, 6, 7, 7, 7, 8, 9,10, 9, 8, 7,
6048
6060
        7, 6,11,12,13,11, 6, 7, 8, 9,14,10, 9, 8, 6,11,
6052
6064
        9, 9,10,10, 8,11,12,11, 9, 9,10,10, 8,13,13, 9,
6053
6065
        9,10,10, 8,13,13, 9, 9,10,10,14,14,14,14,14 }
6054
6066
    };
6055
 
    static const int last_coeff_flag_offset_8x8[63] = {
6056
 
        0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
6057
 
        2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
6058
 
        3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
6059
 
        5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8
6060
 
    };
6061
6067
 
6062
6068
    int index[64];
6063
6069
 
6064
 
    int i, last;
 
6070
    int last;
6065
6071
    int coeff_count = 0;
6066
6072
 
6067
6073
    int abslevel1 = 1;
6071
6077
    uint8_t *last_coeff_ctx_base;
6072
6078
    uint8_t *abs_level_m1_ctx_base;
6073
6079
 
 
6080
#ifndef ARCH_X86
 
6081
#define CABAC_ON_STACK
 
6082
#endif
 
6083
#ifdef CABAC_ON_STACK
 
6084
#define CC &cc
 
6085
    CABACContext cc;
 
6086
    cc.range     = h->cabac.range;
 
6087
    cc.low       = h->cabac.low;
 
6088
    cc.bytestream= h->cabac.bytestream;
 
6089
#else
 
6090
#define CC &h->cabac
 
6091
#endif
 
6092
 
 
6093
 
6074
6094
    /* cat: 0-> DC 16x16  n = 0
6075
6095
     *      1-> AC 16x16  n = luma4x4idx
6076
6096
     *      2-> Luma4x4   n = luma4x4idx
6081
6101
 
6082
6102
    /* read coded block flag */
6083
6103
    if( cat != 5 ) {
6084
 
        if( get_cabac( &h->cabac, &h->cabac_state[85 + get_cabac_cbf_ctx( h, cat, n ) ] ) == 0 ) {
 
6104
        if( get_cabac( CC, &h->cabac_state[85 + get_cabac_cbf_ctx( h, cat, n ) ] ) == 0 ) {
6085
6105
            if( cat == 1 || cat == 2 )
6086
6106
                h->non_zero_count_cache[scan8[n]] = 0;
6087
6107
            else if( cat == 4 )
6088
6108
                h->non_zero_count_cache[scan8[16+n]] = 0;
6089
 
 
 
6109
#ifdef CABAC_ON_STACK
 
6110
            h->cabac.range     = cc.range     ;
 
6111
            h->cabac.low       = cc.low       ;
 
6112
            h->cabac.bytestream= cc.bytestream;
 
6113
#endif
6090
6114
            return 0;
6091
6115
        }
6092
6116
    }
6102
6126
#define DECODE_SIGNIFICANCE( coefs, sig_off, last_off ) \
6103
6127
        for(last= 0; last < coefs; last++) { \
6104
6128
            uint8_t *sig_ctx = significant_coeff_ctx_base + sig_off; \
6105
 
            if( get_cabac( &h->cabac, sig_ctx )) { \
 
6129
            if( get_cabac( CC, sig_ctx )) { \
6106
6130
                uint8_t *last_ctx = last_coeff_ctx_base + last_off; \
6107
6131
                index[coeff_count++] = last; \
6108
 
                if( get_cabac( &h->cabac, last_ctx ) ) { \
 
6132
                if( get_cabac( CC, last_ctx ) ) { \
6109
6133
                    last= max_coeff; \
6110
6134
                    break; \
6111
6135
                } \
6112
6136
            } \
 
6137
        }\
 
6138
        if( last == max_coeff -1 ) {\
 
6139
            index[coeff_count++] = last;\
6113
6140
        }
6114
 
        const int *sig_off = significant_coeff_flag_offset_8x8[MB_FIELD];
 
6141
        const uint8_t *sig_off = significant_coeff_flag_offset_8x8[MB_FIELD];
 
6142
#ifdef ARCH_X86
 
6143
        coeff_count= decode_significance_8x8_x86(CC, significant_coeff_ctx_base, index, sig_off);
 
6144
    } else {
 
6145
        coeff_count= decode_significance_x86(CC, max_coeff, significant_coeff_ctx_base, index);
 
6146
#else
6115
6147
        DECODE_SIGNIFICANCE( 63, sig_off[last], last_coeff_flag_offset_8x8[last] );
6116
6148
    } else {
6117
6149
        DECODE_SIGNIFICANCE( max_coeff - 1, last, last );
6118
 
    }
6119
 
    if( last == max_coeff -1 ) {
6120
 
        index[coeff_count++] = last;
 
6150
#endif
6121
6151
    }
6122
6152
    assert(coeff_count > 0);
6123
6153
 
6134
6164
        fill_rectangle(&h->non_zero_count_cache[scan8[n]], 2, 2, 8, coeff_count, 1);
6135
6165
    }
6136
6166
 
6137
 
    for( i = coeff_count - 1; i >= 0; i-- ) {
 
6167
    for( coeff_count--; coeff_count >= 0; coeff_count-- ) {
6138
6168
        uint8_t *ctx = (abslevelgt1 != 0 ? 0 : FFMIN( 4, abslevel1 )) + abs_level_m1_ctx_base;
6139
 
        int j= scantable[index[i]];
 
6169
        int j= scantable[index[coeff_count]];
6140
6170
 
6141
 
        if( get_cabac( &h->cabac, ctx ) == 0 ) {
 
6171
        if( get_cabac( CC, ctx ) == 0 ) {
6142
6172
            if( !qmul ) {
6143
 
                if( get_cabac_bypass( &h->cabac ) ) block[j] = -1;
6144
 
                else                                block[j] =  1;
 
6173
                block[j] = get_cabac_bypass_sign( CC, -1);
6145
6174
            }else{
6146
 
                if( get_cabac_bypass( &h->cabac ) ) block[j] = (-qmul[j] + 32) >> 6;
6147
 
                else                                block[j] = ( qmul[j] + 32) >> 6;
 
6175
                block[j] = (get_cabac_bypass_sign( CC, -qmul[j]) + 32) >> 6;;
6148
6176
            }
6149
6177
 
6150
6178
            abslevel1++;
6151
6179
        } else {
6152
6180
            int coeff_abs = 2;
6153
6181
            ctx = 5 + FFMIN( 4, abslevelgt1 ) + abs_level_m1_ctx_base;
6154
 
            while( coeff_abs < 15 && get_cabac( &h->cabac, ctx ) ) {
 
6182
            while( coeff_abs < 15 && get_cabac( CC, ctx ) ) {
6155
6183
                coeff_abs++;
6156
6184
            }
6157
6185
 
6158
6186
            if( coeff_abs >= 15 ) {
6159
6187
                int j = 0;
6160
 
                while( get_cabac_bypass( &h->cabac ) ) {
6161
 
                    coeff_abs += 1 << j;
 
6188
                while( get_cabac_bypass( CC ) ) {
6162
6189
                    j++;
6163
6190
                }
6164
6191
 
 
6192
                coeff_abs=1;
6165
6193
                while( j-- ) {
6166
 
                    if( get_cabac_bypass( &h->cabac ) )
6167
 
                        coeff_abs += 1 << j ;
 
6194
                    coeff_abs += coeff_abs + get_cabac_bypass( CC );
6168
6195
                }
 
6196
                coeff_abs+= 14;
6169
6197
            }
6170
6198
 
6171
6199
            if( !qmul ) {
6172
 
                if( get_cabac_bypass( &h->cabac ) ) block[j] = -coeff_abs;
 
6200
                if( get_cabac_bypass( CC ) ) block[j] = -coeff_abs;
6173
6201
                else                                block[j] =  coeff_abs;
6174
6202
            }else{
6175
 
                if( get_cabac_bypass( &h->cabac ) ) block[j] = (-coeff_abs * qmul[j] + 32) >> 6;
 
6203
                if( get_cabac_bypass( CC ) ) block[j] = (-coeff_abs * qmul[j] + 32) >> 6;
6176
6204
                else                                block[j] = ( coeff_abs * qmul[j] + 32) >> 6;
6177
6205
            }
6178
6206
 
6179
6207
            abslevelgt1++;
6180
6208
        }
6181
6209
    }
 
6210
#ifdef CABAC_ON_STACK
 
6211
            h->cabac.range     = cc.range     ;
 
6212
            h->cabac.low       = cc.low       ;
 
6213
            h->cabac.bytestream= cc.bytestream;
 
6214
#endif
6182
6215
    return 0;
6183
6216
}
6184
6217
 
6387
6420
                sub_partition_count[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count;
6388
6421
                h->sub_mb_type[i]=      b_sub_mb_type_info[ h->sub_mb_type[i] ].type;
6389
6422
            }
6390
 
            if(   IS_DIRECT(h->sub_mb_type[0]) || IS_DIRECT(h->sub_mb_type[1])
6391
 
               || IS_DIRECT(h->sub_mb_type[2]) || IS_DIRECT(h->sub_mb_type[3])) {
 
6423
            if( IS_DIRECT(h->sub_mb_type[0] | h->sub_mb_type[1] |
 
6424
                          h->sub_mb_type[2] | h->sub_mb_type[3]) ) {
6392
6425
                pred_direct_motion(h, &mb_type);
6393
6426
                if( h->ref_count[0] > 1 || h->ref_count[1] > 1 ) {
6394
6427
                    for( i = 0; i < 4; i++ )
6588
6621
        cbp |= decode_cabac_mb_cbp_chroma( h ) << 4;
6589
6622
    }
6590
6623
 
6591
 
    h->cbp_table[mb_xy] = cbp;
 
6624
    h->cbp_table[mb_xy] = h->cbp = cbp;
6592
6625
 
6593
6626
    if( dct8x8_allowed && (cbp&15) && !IS_INTRA( mb_type ) ) {
6594
6627
        if( decode_cabac_mb_transform_size( h ) )
6648
6681
                    for( i4x4 = 0; i4x4 < 4; i4x4++ ) {
6649
6682
                        const int index = 4*i8x8 + i4x4;
6650
6683
                        //av_log( s->avctx, AV_LOG_ERROR, "Luma4x4: %d\n", index );
 
6684
//START_TIMER
6651
6685
                        if( decode_cabac_residual(h, h->mb + 16*index, 2, index, scan, h->dequant4_coeff[IS_INTRA( mb_type ) ? 0:3][s->qscale], 16) < 0 )
6652
6686
                            return -1;
 
6687
//STOP_TIMER("decode_residual")
6653
6688
                    }
6654
6689
                } else {
6655
6690
                    uint8_t * const nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ];
6702
6737
}
6703
6738
 
6704
6739
 
6705
 
static void filter_mb_edgev( H264Context *h, uint8_t *pix, int stride, int bS[4], int qp ) {
 
6740
static void filter_mb_edgev( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int qp ) {
6706
6741
    int i, d;
6707
6742
    const int index_a = clip( qp + h->slice_alpha_c0_offset, 0, 51 );
6708
6743
    const int alpha = alpha_table[index_a];
6725
6760
                const int q1 = pix[1];
6726
6761
                const int q2 = pix[2];
6727
6762
 
6728
 
                if( ABS( p0 - q0 ) < alpha &&
6729
 
                    ABS( p1 - p0 ) < beta &&
6730
 
                    ABS( q1 - q0 ) < beta ) {
 
6763
                if( FFABS( p0 - q0 ) < alpha &&
 
6764
                    FFABS( p1 - p0 ) < beta &&
 
6765
                    FFABS( q1 - q0 ) < beta ) {
6731
6766
 
6732
 
                    if(ABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
6733
 
                        if( ABS( p2 - p0 ) < beta)
 
6767
                    if(FFABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
 
6768
                        if( FFABS( p2 - p0 ) < beta)
6734
6769
                        {
6735
6770
                            const int p3 = pix[-4];
6736
6771
                            /* p0', p1', p2' */
6741
6776
                            /* p0' */
6742
6777
                            pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
6743
6778
                        }
6744
 
                        if( ABS( q2 - q0 ) < beta)
 
6779
                        if( FFABS( q2 - q0 ) < beta)
6745
6780
                        {
6746
6781
                            const int q3 = pix[3];
6747
6782
                            /* q0', q1', q2' */
6763
6798
            }
6764
6799
    }
6765
6800
}
6766
 
static void filter_mb_edgecv( H264Context *h, uint8_t *pix, int stride, int bS[4], int qp ) {
 
6801
static void filter_mb_edgecv( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int qp ) {
6767
6802
    int i;
6768
6803
    const int index_a = clip( qp + h->slice_alpha_c0_offset, 0, 51 );
6769
6804
    const int alpha = alpha_table[index_a];
6779
6814
    }
6780
6815
}
6781
6816
 
6782
 
static void filter_mb_mbaff_edgev( H264Context *h, uint8_t *pix, int stride, int bS[8], int qp[2] ) {
 
6817
static void filter_mb_mbaff_edgev( H264Context *h, uint8_t *pix, int stride, int16_t bS[8], int qp[2] ) {
6783
6818
    int i;
6784
6819
    for( i = 0; i < 16; i++, pix += stride) {
6785
6820
        int index_a;
6811
6846
            const int q1 = pix[1];
6812
6847
            const int q2 = pix[2];
6813
6848
 
6814
 
            if( ABS( p0 - q0 ) < alpha &&
6815
 
                ABS( p1 - p0 ) < beta &&
6816
 
                ABS( q1 - q0 ) < beta ) {
 
6849
            if( FFABS( p0 - q0 ) < alpha &&
 
6850
                FFABS( p1 - p0 ) < beta &&
 
6851
                FFABS( q1 - q0 ) < beta ) {
6817
6852
                int tc = tc0;
6818
6853
                int i_delta;
6819
6854
 
6820
 
                if( ABS( p2 - p0 ) < beta ) {
 
6855
                if( FFABS( p2 - p0 ) < beta ) {
6821
6856
                    pix[-2] = p1 + clip( ( p2 + ( ( p0 + q0 + 1 ) >> 1 ) - ( p1 << 1 ) ) >> 1, -tc0, tc0 );
6822
6857
                    tc++;
6823
6858
                }
6824
 
                if( ABS( q2 - q0 ) < beta ) {
 
6859
                if( FFABS( q2 - q0 ) < beta ) {
6825
6860
                    pix[1] = q1 + clip( ( q2 + ( ( p0 + q0 + 1 ) >> 1 ) - ( q1 << 1 ) ) >> 1, -tc0, tc0 );
6826
6861
                    tc++;
6827
6862
                }
6840
6875
            const int q1 = pix[1];
6841
6876
            const int q2 = pix[2];
6842
6877
 
6843
 
            if( ABS( p0 - q0 ) < alpha &&
6844
 
                ABS( p1 - p0 ) < beta &&
6845
 
                ABS( q1 - q0 ) < beta ) {
 
6878
            if( FFABS( p0 - q0 ) < alpha &&
 
6879
                FFABS( p1 - p0 ) < beta &&
 
6880
                FFABS( q1 - q0 ) < beta ) {
6846
6881
 
6847
 
                if(ABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
6848
 
                    if( ABS( p2 - p0 ) < beta)
 
6882
                if(FFABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
 
6883
                    if( FFABS( p2 - p0 ) < beta)
6849
6884
                    {
6850
6885
                        const int p3 = pix[-4];
6851
6886
                        /* p0', p1', p2' */
6856
6891
                        /* p0' */
6857
6892
                        pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
6858
6893
                    }
6859
 
                    if( ABS( q2 - q0 ) < beta)
 
6894
                    if( FFABS( q2 - q0 ) < beta)
6860
6895
                    {
6861
6896
                        const int q3 = pix[3];
6862
6897
                        /* q0', q1', q2' */
6877
6912
        }
6878
6913
    }
6879
6914
}
6880
 
static void filter_mb_mbaff_edgecv( H264Context *h, uint8_t *pix, int stride, int bS[8], int qp[2] ) {
 
6915
static void filter_mb_mbaff_edgecv( H264Context *h, uint8_t *pix, int stride, int16_t bS[8], int qp[2] ) {
6881
6916
    int i;
6882
6917
    for( i = 0; i < 8; i++, pix += stride) {
6883
6918
        int index_a;
6903
6938
            const int q0 = pix[0];
6904
6939
            const int q1 = pix[1];
6905
6940
 
6906
 
            if( ABS( p0 - q0 ) < alpha &&
6907
 
                ABS( p1 - p0 ) < beta &&
6908
 
                ABS( q1 - q0 ) < beta ) {
 
6941
            if( FFABS( p0 - q0 ) < alpha &&
 
6942
                FFABS( p1 - p0 ) < beta &&
 
6943
                FFABS( q1 - q0 ) < beta ) {
6909
6944
                const int i_delta = clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
6910
6945
 
6911
6946
                pix[-1] = clip_uint8( p0 + i_delta );    /* p0' */
6918
6953
            const int q0 = pix[0];
6919
6954
            const int q1 = pix[1];
6920
6955
 
6921
 
            if( ABS( p0 - q0 ) < alpha &&
6922
 
                ABS( p1 - p0 ) < beta &&
6923
 
                ABS( q1 - q0 ) < beta ) {
 
6956
            if( FFABS( p0 - q0 ) < alpha &&
 
6957
                FFABS( p1 - p0 ) < beta &&
 
6958
                FFABS( q1 - q0 ) < beta ) {
6924
6959
 
6925
6960
                pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;   /* p0' */
6926
6961
                pix[0]  = ( 2*q1 + q0 + p1 + 2 ) >> 2;   /* q0' */
6930
6965
    }
6931
6966
}
6932
6967
 
6933
 
static void filter_mb_edgeh( H264Context *h, uint8_t *pix, int stride, int bS[4], int qp ) {
 
6968
static void filter_mb_edgeh( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int qp ) {
6934
6969
    int i, d;
6935
6970
    const int index_a = clip( qp + h->slice_alpha_c0_offset, 0, 51 );
6936
6971
    const int alpha = alpha_table[index_a];
6952
6987
                const int q1 = pix[1*pix_next];
6953
6988
                const int q2 = pix[2*pix_next];
6954
6989
 
6955
 
                if( ABS( p0 - q0 ) < alpha &&
6956
 
                    ABS( p1 - p0 ) < beta &&
6957
 
                    ABS( q1 - q0 ) < beta ) {
 
6990
                if( FFABS( p0 - q0 ) < alpha &&
 
6991
                    FFABS( p1 - p0 ) < beta &&
 
6992
                    FFABS( q1 - q0 ) < beta ) {
6958
6993
 
6959
6994
                    const int p3 = pix[-4*pix_next];
6960
6995
                    const int q3 = pix[ 3*pix_next];
6961
6996
 
6962
 
                    if(ABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
6963
 
                        if( ABS( p2 - p0 ) < beta) {
 
6997
                    if(FFABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
 
6998
                        if( FFABS( p2 - p0 ) < beta) {
6964
6999
                            /* p0', p1', p2' */
6965
7000
                            pix[-1*pix_next] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
6966
7001
                            pix[-2*pix_next] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
6969
7004
                            /* p0' */
6970
7005
                            pix[-1*pix_next] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
6971
7006
                        }
6972
 
                        if( ABS( q2 - q0 ) < beta) {
 
7007
                        if( FFABS( q2 - q0 ) < beta) {
6973
7008
                            /* q0', q1', q2' */
6974
7009
                            pix[0*pix_next] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
6975
7010
                            pix[1*pix_next] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
6990
7025
    }
6991
7026
}
6992
7027
 
6993
 
static void filter_mb_edgech( H264Context *h, uint8_t *pix, int stride, int bS[4], int qp ) {
 
7028
static void filter_mb_edgech( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int qp ) {
6994
7029
    int i;
6995
7030
    const int index_a = clip( qp + h->slice_alpha_c0_offset, 0, 51 );
6996
7031
    const int alpha = alpha_table[index_a];
7006
7041
    }
7007
7042
}
7008
7043
 
 
7044
static void filter_mb_fast( H264Context *h, int mb_x, int mb_y, uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr, unsigned int linesize, unsigned int uvlinesize) {
 
7045
    MpegEncContext * const s = &h->s;
 
7046
    int mb_xy, mb_type;
 
7047
    int qp, qp0, qp1, qpc, qpc0, qpc1, qp_thresh;
 
7048
 
 
7049
    if(mb_x==0 || mb_y==0 || !s->dsp.h264_loop_filter_strength) {
 
7050
        filter_mb(h, mb_x, mb_y, img_y, img_cb, img_cr, linesize, uvlinesize);
 
7051
        return;
 
7052
    }
 
7053
    assert(!FRAME_MBAFF);
 
7054
 
 
7055
    mb_xy = mb_x + mb_y*s->mb_stride;
 
7056
    mb_type = s->current_picture.mb_type[mb_xy];
 
7057
    qp = s->current_picture.qscale_table[mb_xy];
 
7058
    qp0 = s->current_picture.qscale_table[mb_xy-1];
 
7059
    qp1 = s->current_picture.qscale_table[h->top_mb_xy];
 
7060
    qpc = get_chroma_qp( h->pps.chroma_qp_index_offset, qp );
 
7061
    qpc0 = get_chroma_qp( h->pps.chroma_qp_index_offset, qp0 );
 
7062
    qpc1 = get_chroma_qp( h->pps.chroma_qp_index_offset, qp1 );
 
7063
    qp0 = (qp + qp0 + 1) >> 1;
 
7064
    qp1 = (qp + qp1 + 1) >> 1;
 
7065
    qpc0 = (qpc + qpc0 + 1) >> 1;
 
7066
    qpc1 = (qpc + qpc1 + 1) >> 1;
 
7067
    qp_thresh = 15 - h->slice_alpha_c0_offset;
 
7068
    if(qp <= qp_thresh && qp0 <= qp_thresh && qp1 <= qp_thresh &&
 
7069
       qpc <= qp_thresh && qpc0 <= qp_thresh && qpc1 <= qp_thresh)
 
7070
        return;
 
7071
 
 
7072
    if( IS_INTRA(mb_type) ) {
 
7073
        int16_t bS4[4] = {4,4,4,4};
 
7074
        int16_t bS3[4] = {3,3,3,3};
 
7075
        if( IS_8x8DCT(mb_type) ) {
 
7076
            filter_mb_edgev( h, &img_y[4*0], linesize, bS4, qp0 );
 
7077
            filter_mb_edgev( h, &img_y[4*2], linesize, bS3, qp );
 
7078
            filter_mb_edgeh( h, &img_y[4*0*linesize], linesize, bS4, qp1 );
 
7079
            filter_mb_edgeh( h, &img_y[4*2*linesize], linesize, bS3, qp );
 
7080
        } else {
 
7081
            filter_mb_edgev( h, &img_y[4*0], linesize, bS4, qp0 );
 
7082
            filter_mb_edgev( h, &img_y[4*1], linesize, bS3, qp );
 
7083
            filter_mb_edgev( h, &img_y[4*2], linesize, bS3, qp );
 
7084
            filter_mb_edgev( h, &img_y[4*3], linesize, bS3, qp );
 
7085
            filter_mb_edgeh( h, &img_y[4*0*linesize], linesize, bS4, qp1 );
 
7086
            filter_mb_edgeh( h, &img_y[4*1*linesize], linesize, bS3, qp );
 
7087
            filter_mb_edgeh( h, &img_y[4*2*linesize], linesize, bS3, qp );
 
7088
            filter_mb_edgeh( h, &img_y[4*3*linesize], linesize, bS3, qp );
 
7089
        }
 
7090
        filter_mb_edgecv( h, &img_cb[2*0], uvlinesize, bS4, qpc0 );
 
7091
        filter_mb_edgecv( h, &img_cb[2*2], uvlinesize, bS3, qpc );
 
7092
        filter_mb_edgecv( h, &img_cr[2*0], uvlinesize, bS4, qpc0 );
 
7093
        filter_mb_edgecv( h, &img_cr[2*2], uvlinesize, bS3, qpc );
 
7094
        filter_mb_edgech( h, &img_cb[2*0*uvlinesize], uvlinesize, bS4, qpc1 );
 
7095
        filter_mb_edgech( h, &img_cb[2*2*uvlinesize], uvlinesize, bS3, qpc );
 
7096
        filter_mb_edgech( h, &img_cr[2*0*uvlinesize], uvlinesize, bS4, qpc1 );
 
7097
        filter_mb_edgech( h, &img_cr[2*2*uvlinesize], uvlinesize, bS3, qpc );
 
7098
        return;
 
7099
    } else {
 
7100
        DECLARE_ALIGNED_8(int16_t, bS[2][4][4]);
 
7101
        uint64_t (*bSv)[4] = (uint64_t(*)[4])bS;
 
7102
        int edges;
 
7103
        if( IS_8x8DCT(mb_type) && (h->cbp&7) == 7 ) {
 
7104
            edges = 4;
 
7105
            bSv[0][0] = bSv[0][2] = bSv[1][0] = bSv[1][2] = 0x0002000200020002ULL;
 
7106
        } else {
 
7107
            int mask_edge1 = (mb_type & (MB_TYPE_16x16 | MB_TYPE_8x16)) ? 3 :
 
7108
                             (mb_type & MB_TYPE_16x8) ? 1 : 0;
 
7109
            int mask_edge0 = (mb_type & (MB_TYPE_16x16 | MB_TYPE_8x16))
 
7110
                             && (s->current_picture.mb_type[mb_xy-1] & (MB_TYPE_16x16 | MB_TYPE_8x16))
 
7111
                             ? 3 : 0;
 
7112
            int step = IS_8x8DCT(mb_type) ? 2 : 1;
 
7113
            edges = (mb_type & MB_TYPE_16x16) && !(h->cbp & 15) ? 1 : 4;
 
7114
            s->dsp.h264_loop_filter_strength( bS, h->non_zero_count_cache, h->ref_cache, h->mv_cache,
 
7115
                                              (h->slice_type == B_TYPE), edges, step, mask_edge0, mask_edge1 );
 
7116
        }
 
7117
        if( IS_INTRA(s->current_picture.mb_type[mb_xy-1]) )
 
7118
            bSv[0][0] = 0x0004000400040004ULL;
 
7119
        if( IS_INTRA(s->current_picture.mb_type[h->top_mb_xy]) )
 
7120
            bSv[1][0] = 0x0004000400040004ULL;
 
7121
 
 
7122
#define FILTER(hv,dir,edge)\
 
7123
        if(bSv[dir][edge]) {\
 
7124
            filter_mb_edge##hv( h, &img_y[4*edge*(dir?linesize:1)], linesize, bS[dir][edge], edge ? qp : qp##dir );\
 
7125
            if(!(edge&1)) {\
 
7126
                filter_mb_edgec##hv( h, &img_cb[2*edge*(dir?uvlinesize:1)], uvlinesize, bS[dir][edge], edge ? qpc : qpc##dir );\
 
7127
                filter_mb_edgec##hv( h, &img_cr[2*edge*(dir?uvlinesize:1)], uvlinesize, bS[dir][edge], edge ? qpc : qpc##dir );\
 
7128
            }\
 
7129
        }
 
7130
        if( edges == 1 ) {
 
7131
            FILTER(v,0,0);
 
7132
            FILTER(h,1,0);
 
7133
        } else if( IS_8x8DCT(mb_type) ) {
 
7134
            FILTER(v,0,0);
 
7135
            FILTER(v,0,2);
 
7136
            FILTER(h,1,0);
 
7137
            FILTER(h,1,2);
 
7138
        } else {
 
7139
            FILTER(v,0,0);
 
7140
            FILTER(v,0,1);
 
7141
            FILTER(v,0,2);
 
7142
            FILTER(v,0,3);
 
7143
            FILTER(h,1,0);
 
7144
            FILTER(h,1,1);
 
7145
            FILTER(h,1,2);
 
7146
            FILTER(h,1,3);
 
7147
        }
 
7148
#undef FILTER
 
7149
    }
 
7150
}
 
7151
 
7009
7152
static void filter_mb( H264Context *h, int mb_x, int mb_y, uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr, unsigned int linesize, unsigned int uvlinesize) {
7010
7153
    MpegEncContext * const s = &h->s;
7011
7154
    const int mb_xy= mb_x + mb_y*s->mb_stride;
7043
7186
         */
7044
7187
        const int pair_xy = mb_x + (mb_y&~1)*s->mb_stride;
7045
7188
        const int left_mb_xy[2] = { pair_xy-1, pair_xy-1+s->mb_stride };
7046
 
        int bS[8];
 
7189
        int16_t bS[8];
7047
7190
        int qp[2];
7048
7191
        int chroma_qp[2];
7049
7192
        int mb_qp, mbn0_qp, mbn1_qp;
7122
7265
            int mbn_xy = mb_xy - 2 * s->mb_stride;
7123
7266
            int qp, chroma_qp;
7124
7267
            int i, j;
7125
 
            int bS[4];
 
7268
            int16_t bS[4];
7126
7269
 
7127
7270
            for(j=0; j<2; j++, mbn_xy += s->mb_stride){
7128
7271
                if( IS_INTRA(mb_type) ||
7158
7301
            /* mbn_xy: neighbor macroblock */
7159
7302
            const int mbn_xy = edge > 0 ? mb_xy : mbm_xy;
7160
7303
            const int mbn_type = s->current_picture.mb_type[mbn_xy];
7161
 
            int bS[4];
 
7304
            int16_t bS[4];
7162
7305
            int qp;
7163
7306
 
7164
7307
            if( (edge&1) && IS_8x8DCT(mb_type) )
7197
7340
                    int v = 0;
7198
7341
                    for( l = 0; !v && l < 1 + (h->slice_type == B_TYPE); l++ ) {
7199
7342
                        v |= ref2frm[h->ref_cache[l][b_idx]+2] != ref2frm[h->ref_cache[l][bn_idx]+2] ||
7200
 
                             ABS( h->mv_cache[l][b_idx][0] - h->mv_cache[l][bn_idx][0] ) >= 4 ||
7201
 
                             ABS( h->mv_cache[l][b_idx][1] - h->mv_cache[l][bn_idx][1] ) >= mvy_limit;
 
7343
                             FFABS( h->mv_cache[l][b_idx][0] - h->mv_cache[l][bn_idx][0] ) >= 4 ||
 
7344
                             FFABS( h->mv_cache[l][b_idx][1] - h->mv_cache[l][bn_idx][1] ) >= mvy_limit;
7202
7345
                    }
7203
7346
                    bS[0] = bS[1] = bS[2] = bS[3] = v;
7204
7347
                    mv_done = 1;
7221
7364
                        bS[i] = 0;
7222
7365
                        for( l = 0; l < 1 + (h->slice_type == B_TYPE); l++ ) {
7223
7366
                            if( ref2frm[h->ref_cache[l][b_idx]+2] != ref2frm[h->ref_cache[l][bn_idx]+2] ||
7224
 
                                ABS( h->mv_cache[l][b_idx][0] - h->mv_cache[l][bn_idx][0] ) >= 4 ||
7225
 
                                ABS( h->mv_cache[l][b_idx][1] - h->mv_cache[l][bn_idx][1] ) >= mvy_limit ) {
 
7367
                                FFABS( h->mv_cache[l][b_idx][0] - h->mv_cache[l][bn_idx][0] ) >= 4 ||
 
7368
                                FFABS( h->mv_cache[l][b_idx][1] - h->mv_cache[l][bn_idx][1] ) >= mvy_limit ) {
7226
7369
                                bS[i] = 1;
7227
7370
                                break;
7228
7371
                            }
7275
7418
        align_get_bits( &s->gb );
7276
7419
 
7277
7420
        /* init cabac */
7278
 
        ff_init_cabac_states( &h->cabac, ff_h264_lps_range, ff_h264_mps_state, ff_h264_lps_state, 64 );
 
7421
        ff_init_cabac_states( &h->cabac);
7279
7422
        ff_init_cabac_decoder( &h->cabac,
7280
7423
                               s->gb.buffer + get_bits_count(&s->gb)/8,
7281
7424
                               ( s->gb.size_in_bits - get_bits_count(&s->gb) + 7)/8);
7294
7437
        }
7295
7438
 
7296
7439
        for(;;){
 
7440
//START_TIMER
7297
7441
            int ret = decode_mb_cabac(h);
7298
7442
            int eos;
 
7443
//STOP_TIMER("decode_mb_cabac")
7299
7444
 
7300
7445
            if(ret>=0) hl_decode_mb(h);
7301
7446
 
7309
7454
            }
7310
7455
            eos = get_cabac_terminate( &h->cabac );
7311
7456
 
7312
 
            if( ret < 0 || h->cabac.bytestream > h->cabac.bytestream_end + 1) {
 
7457
            if( ret < 0 || h->cabac.bytestream > h->cabac.bytestream_end + 2) {
7313
7458
                av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding MB %d %d, bytestream (%d)\n", s->mb_x, s->mb_y, h->cabac.bytestream_end - h->cabac.bytestream);
7314
7459
                ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_ERROR|DC_ERROR|MV_ERROR)&part_mask);
7315
7460
                return -1;
7702
7847
 
7703
7848
#ifndef ALLOW_INTERLACE
7704
7849
    if(sps->mb_aff)
7705
 
        av_log(h->s.avctx, AV_LOG_ERROR, "MBAFF support not included; enable it compilation time\n");
 
7850
        av_log(h->s.avctx, AV_LOG_ERROR, "MBAFF support not included; enable it at compile-time.\n");
7706
7851
#endif
7707
7852
    if(!sps->direct_8x8_inference_flag && sps->mb_aff)
7708
7853
        av_log(h->s.avctx, AV_LOG_ERROR, "MBAFF + !direct_8x8_inference is not implemented\n");
7879
8024
    return END_NOT_FOUND;
7880
8025
}
7881
8026
 
 
8027
#ifdef CONFIG_H264_PARSER
7882
8028
static int h264_parse(AVCodecParserContext *s,
7883
8029
                      AVCodecContext *avctx,
7884
8030
                      uint8_t **poutbuf, int *poutbuf_size,
7924
8070
    }
7925
8071
    return 0;
7926
8072
}
7927
 
 
 
8073
#endif /* CONFIG_H264_PARSER */
7928
8074
 
7929
8075
static int decode_nal_units(H264Context *h, uint8_t *buf, int buf_size){
7930
8076
    MpegEncContext * const s = &h->s;
7973
8119
      }
7974
8120
 
7975
8121
        ptr= decode_nal(h, buf + buf_index, &dst_length, &consumed, h->is_avc ? nalsize : buf_size - buf_index);
7976
 
        if(ptr[dst_length - 1] == 0) dst_length--;
 
8122
        while(ptr[dst_length - 1] == 0 && dst_length > 1)
 
8123
            dst_length--;
7977
8124
        bit_length= 8*dst_length - decode_rbsp_trailing(ptr + dst_length - 1);
7978
8125
 
7979
8126
        if(s->avctx->debug&FF_DEBUG_STARTCODE){
8387
8534
        printf("\n");*/
8388
8535
 
8389
8536
        for(j=0; j<16; j++){
8390
 
            int diff= ABS(src[j] - ref[j]);
 
8537
            int diff= FFABS(src[j] - ref[j]);
8391
8538
 
8392
8539
            error+= diff*diff;
8393
8540
            max_error= FFMAX(max_error, diff);
8494
8641
    .flush= flush_dpb,
8495
8642
};
8496
8643
 
 
8644
#ifdef CONFIG_H264_PARSER
8497
8645
AVCodecParser h264_parser = {
8498
8646
    { CODEC_ID_H264 },
8499
8647
    sizeof(H264Context),
8502
8650
    ff_parse_close,
8503
8651
    h264_split,
8504
8652
};
 
8653
#endif
8505
8654
 
8506
8655
#include "svq3.c"