~siretart/libav/merge.raring.libav-0.8.6

« back to all changes in this revision

Viewing changes to libavcodec/h264_cabac.c

  • Committer: Package Import Robot
  • Author(s): Reinhard Tartler
  • Date: 2012-01-12 22:30:00 UTC
  • mfrom: (1.4.1)
  • mto: (1.3.11 sid) (26.1.1 quantal-security)
  • mto: This revision was merged to the branch mainline in revision 15.
  • Revision ID: package-import@ubuntu.com-20120112223000-s1reiy1e28hnix42
Tags: upstream-0.8~beta2
ImportĀ upstreamĀ versionĀ 0.8~beta2

Show diffs side-by-side

added added

removed removed

Lines of Context:
1284
1284
 
1285
1285
    unsigned long ctx = 0;
1286
1286
 
1287
 
    ctx += h->mb_field_decoding_flag & !!s->mb_x; //for FMO:(s->current_picture.mb_type[mba_xy]>>7)&(h->slice_table[mba_xy] == h->slice_num);
1288
 
    ctx += (s->current_picture.mb_type[mbb_xy]>>7)&(h->slice_table[mbb_xy] == h->slice_num);
 
1287
    ctx += h->mb_field_decoding_flag & !!s->mb_x; //for FMO:(s->current_picture.f.mb_type[mba_xy] >> 7) & (h->slice_table[mba_xy] == h->slice_num);
 
1288
    ctx += (s->current_picture.f.mb_type[mbb_xy] >> 7) & (h->slice_table[mbb_xy] == h->slice_num);
1289
1289
 
1290
1290
    return get_cabac_noinline( &h->cabac, &(h->cabac_state+70)[ctx] );
1291
1291
}
1296
1296
 
1297
1297
    if(intra_slice){
1298
1298
        int ctx=0;
1299
 
        if( h->left_type[0] & (MB_TYPE_INTRA16x16|MB_TYPE_INTRA_PCM))
 
1299
        if( h->left_type[LTOP] & (MB_TYPE_INTRA16x16|MB_TYPE_INTRA_PCM))
1300
1300
            ctx++;
1301
 
        if( h->top_type     & (MB_TYPE_INTRA16x16|MB_TYPE_INTRA_PCM))
 
1301
        if( h->top_type        & (MB_TYPE_INTRA16x16|MB_TYPE_INTRA_PCM))
1302
1302
            ctx++;
1303
1303
        if( get_cabac_noinline( &h->cabac, &state[ctx] ) == 0 )
1304
1304
            return 0;   /* I4x4 */
1330
1330
        mba_xy = mb_xy - 1;
1331
1331
        if( (mb_y&1)
1332
1332
            && h->slice_table[mba_xy] == h->slice_num
1333
 
            && MB_FIELD == !!IS_INTERLACED( s->current_picture.mb_type[mba_xy] ) )
 
1333
            && MB_FIELD == !!IS_INTERLACED( s->current_picture.f.mb_type[mba_xy] ) )
1334
1334
            mba_xy += s->mb_stride;
1335
1335
        if( MB_FIELD ){
1336
1336
            mbb_xy = mb_xy - s->mb_stride;
1337
1337
            if( !(mb_y&1)
1338
1338
                && h->slice_table[mbb_xy] == h->slice_num
1339
 
                && IS_INTERLACED( s->current_picture.mb_type[mbb_xy] ) )
 
1339
                && IS_INTERLACED( s->current_picture.f.mb_type[mbb_xy] ) )
1340
1340
                mbb_xy -= s->mb_stride;
1341
1341
        }else
1342
1342
            mbb_xy = mb_x + (mb_y-1)*s->mb_stride;
1346
1346
        mbb_xy = mb_xy - (s->mb_stride << FIELD_PICTURE);
1347
1347
    }
1348
1348
 
1349
 
    if( h->slice_table[mba_xy] == h->slice_num && !IS_SKIP( s->current_picture.mb_type[mba_xy] ))
 
1349
    if( h->slice_table[mba_xy] == h->slice_num && !IS_SKIP( s->current_picture.f.mb_type[mba_xy] ))
1350
1350
        ctx++;
1351
 
    if( h->slice_table[mbb_xy] == h->slice_num && !IS_SKIP( s->current_picture.mb_type[mbb_xy] ))
 
1351
    if( h->slice_table[mbb_xy] == h->slice_num && !IS_SKIP( s->current_picture.f.mb_type[mbb_xy] ))
1352
1352
        ctx++;
1353
1353
 
1354
1354
    if( h->slice_type_nos == AV_PICTURE_TYPE_B )
1376
1376
    int ctx = 0;
1377
1377
 
1378
1378
    /* No need to test for IS_INTRA4x4 and IS_INTRA16x16, as we set chroma_pred_mode_table to 0 */
1379
 
    if( h->left_type[0] && h->chroma_pred_mode_table[mba_xy] != 0 )
 
1379
    if( h->left_type[LTOP] && h->chroma_pred_mode_table[mba_xy] != 0 )
1380
1380
        ctx++;
1381
1381
 
1382
 
    if( h->top_type     && h->chroma_pred_mode_table[mbb_xy] != 0 )
 
1382
    if( h->top_type        && h->chroma_pred_mode_table[mbb_xy] != 0 )
1383
1383
        ctx++;
1384
1384
 
1385
1385
    if( get_cabac_noinline( &h->cabac, &h->cabac_state[64+ctx] ) == 0 )
1565
1565
    5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8
1566
1566
};
1567
1567
 
1568
 
static av_always_inline void decode_cabac_residual_internal( H264Context *h, DCTELEM *block, int cat, int n, const uint8_t *scantable, const uint32_t *qmul, int max_coeff, int is_dc ) {
 
1568
static av_always_inline void
 
1569
decode_cabac_residual_internal(H264Context *h, DCTELEM *block,
 
1570
                               int cat, int n, const uint8_t *scantable,
 
1571
                               const uint32_t *qmul, int max_coeff,
 
1572
                               int is_dc, int chroma422)
 
1573
{
1569
1574
    static const int significant_coeff_flag_offset[2][14] = {
1570
1575
      { 105+0, 105+15, 105+29, 105+44, 105+47, 402, 484+0, 484+15, 484+29, 660, 528+0, 528+15, 528+29, 718 },
1571
1576
      { 277+0, 277+15, 277+29, 277+44, 277+47, 436, 776+0, 776+15, 776+29, 675, 820+0, 820+15, 820+29, 733 }
1587
1592
        9, 9,10,10, 8,11,12,11, 9, 9,10,10, 8,13,13, 9,
1588
1593
        9,10,10, 8,13,13, 9, 9,10,10,14,14,14,14,14 }
1589
1594
    };
 
1595
    static const uint8_t sig_coeff_offset_dc[7] = { 0, 0, 1, 1, 2, 2, 2 };
1590
1596
    /* node ctx: 0..3: abslevel1 (with abslevelgt1 == 0).
1591
1597
     * 4..7: abslevelgt1 + 3 (and abslevel1 doesn't matter).
1592
1598
     * map node ctx => cabac ctx for level=1 */
1593
1599
    static const uint8_t coeff_abs_level1_ctx[8] = { 1, 2, 3, 4, 0, 0, 0, 0 };
1594
1600
    /* map node ctx => cabac ctx for level>1 */
1595
 
    static const uint8_t coeff_abs_levelgt1_ctx[8] = { 5, 5, 5, 5, 6, 7, 8, 9 };
 
1601
    static const uint8_t coeff_abs_levelgt1_ctx[2][8] = {
 
1602
        { 5, 5, 5, 5, 6, 7, 8, 9 },
 
1603
        { 5, 5, 5, 5, 6, 7, 8, 8 }, // 422/dc case
 
1604
    };
1596
1605
    static const uint8_t coeff_abs_level_transition[2][8] = {
1597
1606
    /* update node ctx after decoding a level=1 */
1598
1607
        { 1, 2, 3, 3, 4, 5, 6, 7 },
1647
1656
            index[coeff_count++] = last;\
1648
1657
        }
1649
1658
        const uint8_t *sig_off = significant_coeff_flag_offset_8x8[MB_FIELD];
1650
 
#if ARCH_X86 && HAVE_7REGS && HAVE_EBX_AVAILABLE && !defined(BROKEN_RELOCATIONS)
 
1659
#if ARCH_X86 && HAVE_7REGS && !defined(BROKEN_RELOCATIONS)
1651
1660
        coeff_count= decode_significance_8x8_x86(CC, significant_coeff_ctx_base, index,
1652
 
                                                 last_coeff_ctx_base-significant_coeff_ctx_base, sig_off);
 
1661
                                                 last_coeff_ctx_base, sig_off);
1653
1662
    } else {
1654
 
        coeff_count= decode_significance_x86(CC, max_coeff, significant_coeff_ctx_base, index,
1655
 
                                             last_coeff_ctx_base-significant_coeff_ctx_base);
 
1663
        if (is_dc && chroma422) { // dc 422
 
1664
            DECODE_SIGNIFICANCE(7, sig_coeff_offset_dc[last], sig_coeff_offset_dc[last]);
 
1665
        } else {
 
1666
            coeff_count= decode_significance_x86(CC, max_coeff, significant_coeff_ctx_base, index,
 
1667
                                                 last_coeff_ctx_base-significant_coeff_ctx_base);
 
1668
        }
1656
1669
#else
1657
1670
        DECODE_SIGNIFICANCE( 63, sig_off[last], last_coeff_flag_offset_8x8[last] );
1658
1671
    } else {
1659
 
        DECODE_SIGNIFICANCE( max_coeff - 1, last, last );
 
1672
        if (is_dc && chroma422) { // dc 422
 
1673
            DECODE_SIGNIFICANCE(7, sig_coeff_offset_dc[last], sig_coeff_offset_dc[last]);
 
1674
        } else {
 
1675
            DECODE_SIGNIFICANCE(max_coeff - 1, last, last);
 
1676
        }
1660
1677
#endif
1661
1678
    }
1662
1679
    assert(coeff_count > 0);
1691
1708
            } \
1692
1709
        } else { \
1693
1710
            int coeff_abs = 2; \
1694
 
            ctx = coeff_abs_levelgt1_ctx[node_ctx] + abs_level_m1_ctx_base; \
 
1711
            ctx = coeff_abs_levelgt1_ctx[is_dc && chroma422][node_ctx] + abs_level_m1_ctx_base; \
1695
1712
            node_ctx = coeff_abs_level_transition[1][node_ctx]; \
1696
1713
\
1697
1714
            while( coeff_abs < 15 && get_cabac( CC, ctx ) ) { \
1733
1750
}
1734
1751
 
1735
1752
static void decode_cabac_residual_dc_internal( H264Context *h, DCTELEM *block, int cat, int n, const uint8_t *scantable, int max_coeff ) {
1736
 
    decode_cabac_residual_internal(h, block, cat, n, scantable, NULL, max_coeff, 1);
 
1753
    decode_cabac_residual_internal(h, block, cat, n, scantable, NULL, max_coeff, 1, 0);
 
1754
}
 
1755
 
 
1756
static void decode_cabac_residual_dc_internal_422(H264Context *h, DCTELEM *block,
 
1757
                                                  int cat, int n, const uint8_t *scantable,
 
1758
                                                  int max_coeff)
 
1759
{
 
1760
    decode_cabac_residual_internal(h, block, cat, n, scantable, NULL, max_coeff, 1, 1);
1737
1761
}
1738
1762
 
1739
1763
static void decode_cabac_residual_nondc_internal( H264Context *h, DCTELEM *block, int cat, int n, const uint8_t *scantable, const uint32_t *qmul, int max_coeff ) {
1740
 
    decode_cabac_residual_internal(h, block, cat, n, scantable, qmul, max_coeff, 0);
 
1764
    decode_cabac_residual_internal(h, block, cat, n, scantable, qmul, max_coeff, 0, 0);
1741
1765
}
1742
1766
 
1743
1767
/* cat: 0-> DC 16x16  n = 0
1761
1785
    decode_cabac_residual_dc_internal( h, block, cat, n, scantable, max_coeff );
1762
1786
}
1763
1787
 
 
1788
static av_always_inline void
 
1789
decode_cabac_residual_dc_422(H264Context *h, DCTELEM *block,
 
1790
                             int cat, int n, const uint8_t *scantable,
 
1791
                             int max_coeff)
 
1792
{
 
1793
    /* read coded block flag */
 
1794
    if (get_cabac(&h->cabac, &h->cabac_state[get_cabac_cbf_ctx(h, cat, n, max_coeff, 1)]) == 0) {
 
1795
        h->non_zero_count_cache[scan8[n]] = 0;
 
1796
        return;
 
1797
    }
 
1798
    decode_cabac_residual_dc_internal_422(h, block, cat, n, scantable, max_coeff);
 
1799
}
 
1800
 
1764
1801
static av_always_inline void decode_cabac_residual_nondc( H264Context *h, DCTELEM *block, int cat, int n, const uint8_t *scantable, const uint32_t *qmul, int max_coeff ) {
1765
1802
    /* read coded block flag */
1766
1803
    if( (cat != 5 || CHROMA444) && get_cabac( &h->cabac, &h->cabac_state[get_cabac_cbf_ctx( h, cat, n, max_coeff, 0 ) ] ) == 0 ) {
1818
1855
                    }
1819
1856
                }
1820
1857
            } else {
1821
 
                uint8_t * const nnz= &h->non_zero_count_cache[ scan8[4*i8x8+16*p] ];
1822
 
                nnz[0] = nnz[1] = nnz[8] = nnz[9] = 0;
 
1858
                fill_rectangle(&h->non_zero_count_cache[scan8[4*i8x8+16*p]], 2, 2, 8, 0, 1);
1823
1859
            }
1824
1860
        }
1825
1861
    }
1826
1862
}
1827
1863
 
1828
1864
/**
1829
 
 * decodes a macroblock
1830
 
 * @return 0 if OK, AC_ERROR / DC_ERROR / MV_ERROR if an error is noticed
 
1865
 * Decode a macroblock.
 
1866
 * @return 0 if OK, ER_AC_ERROR / ER_DC_ERROR / ER_MV_ERROR if an error is noticed
1831
1867
 */
1832
1868
int ff_h264_decode_mb_cabac(H264Context *h) {
1833
1869
    MpegEncContext * const s = &h->s;
1850
1886
        /* read skip flags */
1851
1887
        if( skip ) {
1852
1888
            if( FRAME_MBAFF && (s->mb_y&1)==0 ){
1853
 
                s->current_picture.mb_type[mb_xy] = MB_TYPE_SKIP;
 
1889
                s->current_picture.f.mb_type[mb_xy] = MB_TYPE_SKIP;
1854
1890
                h->next_mb_skipped = decode_cabac_mb_skip( h, s->mb_x, s->mb_y+1 );
1855
1891
                if(!h->next_mb_skipped)
1856
1892
                    h->mb_mbaff = h->mb_field_decoding_flag = decode_cabac_field_decoding_flag(h);
1880
1916
        int ctx = 0;
1881
1917
        assert(h->slice_type_nos == AV_PICTURE_TYPE_B);
1882
1918
 
1883
 
        if( !IS_DIRECT( h->left_type[0]-1 ) )
 
1919
        if( !IS_DIRECT( h->left_type[LTOP]-1 ) )
1884
1920
            ctx++;
1885
1921
        if( !IS_DIRECT( h->top_type-1 ) )
1886
1922
            ctx++;
1966
2002
        h->cbp_table[mb_xy] = 0xf7ef;
1967
2003
        h->chroma_pred_mode_table[mb_xy] = 0;
1968
2004
        // In deblocking, the quantizer is 0
1969
 
        s->current_picture.qscale_table[mb_xy]= 0;
 
2005
        s->current_picture.f.qscale_table[mb_xy] = 0;
1970
2006
        // All coeffs are present
1971
2007
        memset(h->non_zero_count[mb_xy], 16, 48);
1972
 
        s->current_picture.mb_type[mb_xy]= mb_type;
 
2008
        s->current_picture.f.mb_type[mb_xy] = mb_type;
1973
2009
        h->last_qscale_diff = 0;
1974
2010
        return 0;
1975
2011
    }
1999
2035
                //av_log( s->avctx, AV_LOG_ERROR, "i4x4 pred=%d mode=%d\n", pred, h->intra4x4_pred_mode_cache[ scan8[i] ] );
2000
2036
                }
2001
2037
            }
2002
 
            ff_h264_write_back_intra_pred_mode(h);
 
2038
            write_back_intra_pred_mode(h);
2003
2039
            if( ff_h264_check_intra4x4_pred_mode(h) < 0 ) return -1;
2004
2040
        } else {
2005
2041
            h->intra16x16_pred_mode= ff_h264_check_intra_pred_mode( h, h->intra16x16_pred_mode );
2248
2284
     * the transform mode of the current macroblock there. */
2249
2285
    if (CHROMA444 && IS_8x8DCT(mb_type)){
2250
2286
        int i;
 
2287
        uint8_t *nnz_cache = h->non_zero_count_cache;
2251
2288
        for (i = 0; i < 2; i++){
2252
 
            if (h->left_type[i] && !IS_8x8DCT(h->left_type[i])){
2253
 
                h->non_zero_count_cache[3+8* 1 + 2*8*i]=
2254
 
                h->non_zero_count_cache[3+8* 2 + 2*8*i]=
2255
 
                h->non_zero_count_cache[3+8* 6 + 2*8*i]=
2256
 
                h->non_zero_count_cache[3+8* 7 + 2*8*i]=
2257
 
                h->non_zero_count_cache[3+8*11 + 2*8*i]=
2258
 
                h->non_zero_count_cache[3+8*12 + 2*8*i]= IS_INTRA(mb_type) ? 64 : 0;
 
2289
            if (h->left_type[LEFT(i)] && !IS_8x8DCT(h->left_type[LEFT(i)])){
 
2290
                nnz_cache[3+8* 1 + 2*8*i]=
 
2291
                nnz_cache[3+8* 2 + 2*8*i]=
 
2292
                nnz_cache[3+8* 6 + 2*8*i]=
 
2293
                nnz_cache[3+8* 7 + 2*8*i]=
 
2294
                nnz_cache[3+8*11 + 2*8*i]=
 
2295
                nnz_cache[3+8*12 + 2*8*i]= IS_INTRA(mb_type) ? 64 : 0;
2259
2296
            }
2260
2297
        }
2261
2298
        if (h->top_type && !IS_8x8DCT(h->top_type)){
2262
2299
            uint32_t top_empty = CABAC && !IS_INTRA(mb_type) ? 0 : 0x40404040;
2263
 
            AV_WN32A(&h->non_zero_count_cache[4+8* 0], top_empty);
2264
 
            AV_WN32A(&h->non_zero_count_cache[4+8* 5], top_empty);
2265
 
            AV_WN32A(&h->non_zero_count_cache[4+8*10], top_empty);
 
2300
            AV_WN32A(&nnz_cache[4+8* 0], top_empty);
 
2301
            AV_WN32A(&nnz_cache[4+8* 5], top_empty);
 
2302
            AV_WN32A(&nnz_cache[4+8*10], top_empty);
2266
2303
        }
2267
2304
    }
2268
 
    s->current_picture.mb_type[mb_xy]= mb_type;
 
2305
    s->current_picture.f.mb_type[mb_xy] = mb_type;
2269
2306
 
2270
2307
    if( cbp || IS_INTRA16x16( mb_type ) ) {
2271
2308
        const uint8_t *scan, *scan8x8;
2313
2350
        if(CHROMA444){
2314
2351
            decode_cabac_luma_residual(h, scan, scan8x8, pixel_shift, mb_type, cbp, 1);
2315
2352
            decode_cabac_luma_residual(h, scan, scan8x8, pixel_shift, mb_type, cbp, 2);
2316
 
        } else {
 
2353
        } else if (CHROMA422) {
 
2354
            if( cbp&0x30 ){
 
2355
                int c;
 
2356
                for( c = 0; c < 2; c++ ) {
 
2357
                    //av_log( s->avctx, AV_LOG_ERROR, "INTRA C%d-DC\n",c );
 
2358
                    decode_cabac_residual_dc_422(h, h->mb + ((256 + 16*16*c) << pixel_shift), 3,
 
2359
                                                 CHROMA_DC_BLOCK_INDEX + c,
 
2360
                                                 chroma422_dc_scan, 8);
 
2361
                }
 
2362
            }
 
2363
 
 
2364
            if( cbp&0x20 ) {
 
2365
                int c, i, i8x8;
 
2366
                for( c = 0; c < 2; c++ ) {
 
2367
                    DCTELEM *mb = h->mb + (16*(16 + 16*c) << pixel_shift);
 
2368
                    qmul = h->dequant4_coeff[c+1+(IS_INTRA( mb_type ) ? 0:3)][h->chroma_qp[c]];
 
2369
                    for (i8x8 = 0; i8x8 < 2; i8x8++) {
 
2370
                        for (i = 0; i < 4; i++) {
 
2371
                            const int index = 16 + 16 * c + 8*i8x8 + i;
 
2372
                            //av_log(s->avctx, AV_LOG_ERROR, "INTRA C%d-AC %d\n",c, index - 16);
 
2373
                            decode_cabac_residual_nondc(h, mb, 4, index, scan + 1, qmul, 15);
 
2374
                            mb += 16<<pixel_shift;
 
2375
                        }
 
2376
                    }
 
2377
                }
 
2378
            } else {
 
2379
                fill_rectangle(&h->non_zero_count_cache[scan8[16]], 4, 4, 8, 0, 1);
 
2380
                fill_rectangle(&h->non_zero_count_cache[scan8[32]], 4, 4, 8, 0, 1);
 
2381
            }
 
2382
        } else /* yuv420 */ {
2317
2383
            if( cbp&0x30 ){
2318
2384
                int c;
2319
2385
                for( c = 0; c < 2; c++ ) {
2344
2410
        h->last_qscale_diff = 0;
2345
2411
    }
2346
2412
 
2347
 
    s->current_picture.qscale_table[mb_xy]= s->qscale;
 
2413
    s->current_picture.f.qscale_table[mb_xy] = s->qscale;
2348
2414
    write_back_non_zero_count(h);
2349
2415
 
2350
2416
    if(MB_MBAFF){