cavlc.c 26.3 KB
Newer Older
Laurent Aimar's avatar
Laurent Aimar committed
1
/*****************************************************************************
Fiona Glaser's avatar
Fiona Glaser committed
2
 * cavlc.c: cavlc bitstream writing
Laurent Aimar's avatar
Laurent Aimar committed
3
 *****************************************************************************
Henrik Gramner's avatar
Henrik Gramner committed
4
 * Copyright (C) 2003-2018 x264 project
Laurent Aimar's avatar
Laurent Aimar committed
5 6
 *
 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
7
 *          Loren Merritt <lorenm@u.washington.edu>
8
 *          Fiona Glaser <fiona@x264.com>
Laurent Aimar's avatar
Laurent Aimar committed
9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
Fiona Glaser's avatar
Fiona Glaser committed
23 24 25
 *
 * This program is also available under a commercial proprietary license.
 * For more information, contact us at licensing@x264.com.
Laurent Aimar's avatar
Laurent Aimar committed
26 27
 *****************************************************************************/

28
#include "common/common.h"
Laurent Aimar's avatar
Laurent Aimar committed
29 30
#include "macroblock.h"

31 32 33 34
#ifndef RDO_SKIP_BS
#define RDO_SKIP_BS 0
#endif

Fiona Glaser's avatar
Fiona Glaser committed
35 36
/* [400,420][inter,intra] */
static const uint8_t cbp_to_golomb[2][2][48] =
Laurent Aimar's avatar
Laurent Aimar committed
37
{
Fiona Glaser's avatar
Fiona Glaser committed
38 39 40 41 42 43 44 45
    {{ 0,  1,  2,  5,  3,  6, 14, 10,  4, 15,  7, 11,  8, 12, 13,  9 },
     { 1, 10, 11,  6, 12,  7, 14,  2, 13, 15,  8,  3,  9,  4,  5,  0 }},
    {{ 0,  2,  3,  7,  4,  8, 17, 13,  5, 18,  9, 14, 10, 15, 16, 11,
       1, 32, 33, 36, 34, 37, 44, 40, 35, 45, 38, 41, 39, 42, 43, 19,
       6, 24, 25, 20, 26, 21, 46, 28, 27, 47, 22, 29, 23, 30, 31, 12 },
     { 3, 29, 30, 17, 31, 18, 37,  8, 32, 38, 19,  9, 20, 10, 11,  2,
      16, 33, 34, 21, 35, 22, 39,  4, 36, 40, 23,  5, 24,  6,  7,  1,
      41, 42, 43, 25, 44, 26, 46, 12, 45, 47, 27, 13, 28, 14, 15,  0 }}
Laurent Aimar's avatar
Laurent Aimar committed
46
};
Fiona Glaser's avatar
Fiona Glaser committed
47

Loren Merritt's avatar
Loren Merritt committed
48 49 50 51 52 53
static const uint8_t mb_type_b_to_golomb[3][9]=
{
    { 4,  8, 12, 10,  6, 14, 16, 18, 20 }, /* D_16x8 */
    { 5,  9, 13, 11,  7, 15, 17, 19, 21 }, /* D_8x16 */
    { 1, -1, -1, -1,  2, -1, -1, -1,  3 }  /* D_16x16 */
};
Fiona Glaser's avatar
Fiona Glaser committed
54

55
static const uint8_t subpartition_p_to_golomb[4]=
Loren Merritt's avatar
Loren Merritt committed
56 57 58
{
    3, 1, 2, 0
};
Fiona Glaser's avatar
Fiona Glaser committed
59

60
static const uint8_t subpartition_b_to_golomb[13]=
Loren Merritt's avatar
Loren Merritt committed
61 62 63
{
    10,  4,  5,  1, 11,  6,  7,  2, 12,  8,  9,  3,  0
};
Laurent Aimar's avatar
Laurent Aimar committed
64

65
#define bs_write_vlc(s,v) bs_write( s, (v).i_size, (v).i_bits )
Laurent Aimar's avatar
Laurent Aimar committed
66 67

/****************************************************************************
68
 * x264_cavlc_block_residual:
Laurent Aimar's avatar
Laurent Aimar committed
69
 ****************************************************************************/
70
static inline int cavlc_block_residual_escape( x264_t *h, int i_suffix_length, int level )
71
{
72
    bs_t *s = &h->out.bs;
73 74
    static const uint16_t next_suffix[7] = { 0, 3, 6, 12, 24, 48, 0xffff };
    int i_level_prefix = 15;
75
    int mask = level >> 31;
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
    int abs_level = (level^mask)-mask;
    int i_level_code = abs_level*2-mask-2;
    if( ( i_level_code >> i_suffix_length ) < 15 )
    {
        bs_write( s, (i_level_code >> i_suffix_length) + 1 + i_suffix_length,
                 (1<<i_suffix_length) + (i_level_code & ((1<<i_suffix_length)-1)) );
    }
    else
    {
        i_level_code -= 15 << i_suffix_length;
        if( i_suffix_length == 0 )
            i_level_code -= 15;

        /* If the prefix size exceeds 15, High Profile is required. */
        if( i_level_code >= 1<<12 )
        {
            if( h->sps->i_profile_idc >= PROFILE_HIGH )
            {
                while( i_level_code > 1<<(i_level_prefix-3) )
                {
                    i_level_code -= 1<<(i_level_prefix-3);
                    i_level_prefix++;
                }
            }
            else
            {
#if RDO_SKIP_BS
                /* Weight highly against overflows. */
Fiona Glaser's avatar
Fiona Glaser committed
104
                s->i_bits_encoded += 2000;
105
#else
106 107
                /* We've had an overflow; note it down and re-encode the MB later. */
                h->mb.b_overflow = 1;
108 109 110 111 112 113 114 115 116 117 118 119 120
#endif
            }
        }
        bs_write( s, i_level_prefix + 1, 1 );
        bs_write( s, i_level_prefix - 3, i_level_code & ((1<<(i_level_prefix-3))-1) );
    }
    if( i_suffix_length == 0 )
        i_suffix_length++;
    if( abs_level > next_suffix[i_suffix_length] )
        i_suffix_length++;
    return i_suffix_length;
}

121
static int cavlc_block_residual_internal( x264_t *h, int ctx_block_cat, dctcoef *l, int nC )
Laurent Aimar's avatar
Laurent Aimar committed
122
{
123
    bs_t *s = &h->out.bs;
124
    static const uint8_t ctz_index[8] = {3,0,1,0,2,0,1,0};
Henrik Gramner's avatar
Henrik Gramner committed
125
    static const uint8_t count_cat[14] = {16, 15, 16, 0, 15, 64, 16, 15, 16, 64, 16, 15, 16, 64};
126
    x264_run_level_t runlevel;
Henrik Gramner's avatar
Henrik Gramner committed
127
    int i_total, i_trailing, i_total_zero, i_suffix_length;
Laurent Aimar's avatar
Laurent Aimar committed
128 129
    unsigned int i_sign;

130
    /* level and run and total */
Anton Mitrofanov's avatar
Anton Mitrofanov committed
131
    i_total = h->quantf.coeff_level_run[ctx_block_cat]( l, &runlevel );
132
    x264_prefetch( &x264_run_before[runlevel.mask] );
133
    i_total_zero = runlevel.last + 1 - i_total;
Laurent Aimar's avatar
Laurent Aimar committed
134

135 136 137
    /* branchless i_trailing calculation */
    runlevel.level[i_total+0] = 2;
    runlevel.level[i_total+1] = 2;
138 139 140
    i_trailing = ((((runlevel.level[0]+1) | (1-runlevel.level[0])) >> 31) & 1) // abs(runlevel.level[0])>1
               | ((((runlevel.level[1]+1) | (1-runlevel.level[1])) >> 31) & 2)
               | ((((runlevel.level[2]+1) | (1-runlevel.level[2])) >> 31) & 4);
141
    i_trailing = ctz_index[i_trailing];
142 143 144
    i_sign = ((runlevel.level[2] >> 31) & 1)
           | ((runlevel.level[1] >> 31) & 2)
           | ((runlevel.level[0] >> 31) & 4);
145
    i_sign >>= 3-i_trailing;
Laurent Aimar's avatar
Laurent Aimar committed
146 147

    /* total/trailing */
Henrik Gramner's avatar
Henrik Gramner committed
148
    bs_write_vlc( s, x264_coeff_token[nC][i_total-1][i_trailing] );
Laurent Aimar's avatar
Laurent Aimar committed
149

150
    i_suffix_length = i_total > 10 && i_trailing < 3;
151
    bs_write( s, i_trailing, i_sign );
152 153

    if( i_trailing < i_total )
Laurent Aimar's avatar
Laurent Aimar committed
154
    {
155 156 157
        int val = runlevel.level[i_trailing];
        int val_original = runlevel.level[i_trailing]+LEVEL_TABLE_SIZE/2;
        val -= ((val>>31)|1) & -(i_trailing < 3); /* as runlevel.level[i] can't be 1 for the first one if i_trailing < 3 */
158 159 160 161 162 163 164
        val += LEVEL_TABLE_SIZE/2;

        if( (unsigned)val_original < LEVEL_TABLE_SIZE )
        {
            bs_write_vlc( s, x264_level_token[i_suffix_length][val] );
            i_suffix_length = x264_level_token[i_suffix_length][val_original].i_next;
        }
Laurent Aimar's avatar
Laurent Aimar committed
165
        else
166
            i_suffix_length = cavlc_block_residual_escape( h, i_suffix_length, val-LEVEL_TABLE_SIZE/2 );
167
        for( int i = i_trailing+1; i < i_total; i++ )
Laurent Aimar's avatar
Laurent Aimar committed
168
        {
169
            val = runlevel.level[i] + LEVEL_TABLE_SIZE/2;
170
            if( (unsigned)val < LEVEL_TABLE_SIZE )
171
            {
172 173
                bs_write_vlc( s, x264_level_token[i_suffix_length][val] );
                i_suffix_length = x264_level_token[i_suffix_length][val].i_next;
174
            }
175
            else
176
                i_suffix_length = cavlc_block_residual_escape( h, i_suffix_length, val-LEVEL_TABLE_SIZE/2 );
Laurent Aimar's avatar
Laurent Aimar committed
177 178 179
        }
    }

Henrik Gramner's avatar
Henrik Gramner committed
180
    if( ctx_block_cat == DCT_CHROMA_DC )
Laurent Aimar's avatar
Laurent Aimar committed
181
    {
182
        if( i_total < 8>>CHROMA_V_SHIFT )
Henrik Gramner's avatar
Henrik Gramner committed
183 184 185 186 187
        {
            vlc_t total_zeros = CHROMA_FORMAT == CHROMA_420 ? x264_total_zeros_2x2_dc[i_total-1][i_total_zero]
                                                            : x264_total_zeros_2x4_dc[i_total-1][i_total_zero];
            bs_write_vlc( s, total_zeros );
        }
Laurent Aimar's avatar
Laurent Aimar committed
188
    }
Henrik Gramner's avatar
Henrik Gramner committed
189 190
    else if( (uint8_t)i_total < count_cat[ctx_block_cat] )
        bs_write_vlc( s, x264_total_zeros[i_total-1][i_total_zero] );
Laurent Aimar's avatar
Laurent Aimar committed
191

192 193
    int zero_run_code = x264_run_before[runlevel.mask];
    bs_write( s, zero_run_code&0x1f, zero_run_code>>5 );
194 195

    return i_total;
Laurent Aimar's avatar
Laurent Aimar committed
196 197
}

Fiona Glaser's avatar
Fiona Glaser committed
198 199
static const uint8_t ct_index[17] = {0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,3};

200
#define x264_cavlc_block_residual(h,cat,idx,l)\
Fiona Glaser's avatar
Fiona Glaser committed
201
{\
202
    int nC = cat == DCT_CHROMA_DC ? 5 - CHROMA_V_SHIFT\
Henrik Gramner's avatar
Henrik Gramner committed
203
                                  : ct_index[x264_mb_predict_non_zero_code( h, cat == DCT_LUMA_DC ? (idx - LUMA_DC)*16 : idx )];\
204 205
    uint8_t *nnz = &h->mb.cache.non_zero_count[x264_scan8[idx]];\
    if( !*nnz )\
206
        bs_write_vlc( &h->out.bs, x264_coeff0_token[nC] );\
207
    else\
208
        *nnz = cavlc_block_residual_internal(h,cat,l,nC);\
Fiona Glaser's avatar
Fiona Glaser committed
209 210
}

211
static void cavlc_qp_delta( x264_t *h )
212
{
213
    bs_t *s = &h->out.bs;
214
    int i_dqp = h->mb.i_qp - h->mb.i_last_qp;
215

216 217 218
    /* Avoid writing a delta quant if we have an empty i16x16 block, e.g. in a completely
     * flat background area. Don't do this if it would raise the quantizer, since that could
     * cause unexpected deblocking artifacts. */
219
    if( h->mb.i_type == I_16x16 && !(h->mb.i_cbp_luma | h->mb.i_cbp_chroma)
Fiona Glaser's avatar
Fiona Glaser committed
220 221
        && !h->mb.cache.non_zero_count[x264_scan8[LUMA_DC]]
        && !h->mb.cache.non_zero_count[x264_scan8[CHROMA_DC+0]]
222 223
        && !h->mb.cache.non_zero_count[x264_scan8[CHROMA_DC+1]]
        && h->mb.i_qp > h->mb.i_last_qp )
224
    {
225
#if !RDO_SKIP_BS
226 227 228 229 230
        h->mb.i_qp = h->mb.i_last_qp;
#endif
        i_dqp = 0;
    }

231 232
    if( i_dqp )
    {
233 234 235 236
        if( i_dqp < -(QP_MAX_SPEC+1)/2 )
            i_dqp += QP_MAX_SPEC+1;
        else if( i_dqp > QP_MAX_SPEC/2 )
            i_dqp -= QP_MAX_SPEC+1;
237
    }
238
    bs_write_se( s, i_dqp );
239 240
}

241
static void cavlc_mvd( x264_t *h, int i_list, int idx, int width )
242
{
243
    bs_t *s = &h->out.bs;
244
    ALIGNED_4( int16_t mvp[2] );
245 246 247 248
    x264_mb_predict_mv( h, i_list, idx, width, mvp );
    bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[idx]][0] - mvp[0] );
    bs_write_se( s, h->mb.cache.mv[i_list][x264_scan8[idx]][1] - mvp[1] );
}
249

250
static inline void cavlc_8x8_mvd( x264_t *h, int i )
251 252 253 254
{
    switch( h->mb.i_sub_partition[i] )
    {
        case D_L0_8x8:
255
            cavlc_mvd( h, 0, 4*i, 2 );
256 257
            break;
        case D_L0_8x4:
258 259
            cavlc_mvd( h, 0, 4*i+0, 2 );
            cavlc_mvd( h, 0, 4*i+2, 2 );
260 261
            break;
        case D_L0_4x8:
262 263
            cavlc_mvd( h, 0, 4*i+0, 1 );
            cavlc_mvd( h, 0, 4*i+1, 1 );
264 265
            break;
        case D_L0_4x4:
266 267 268 269
            cavlc_mvd( h, 0, 4*i+0, 1 );
            cavlc_mvd( h, 0, 4*i+1, 1 );
            cavlc_mvd( h, 0, 4*i+2, 1 );
            cavlc_mvd( h, 0, 4*i+3, 1 );
270
            break;
271 272 273
    }
}

274
static ALWAYS_INLINE void cavlc_macroblock_luma_residual( x264_t *h, int plane_count )
275 276 277 278
{
    if( h->mb.b_transform_8x8 )
    {
        /* shuffle 8x8 dct coeffs into 4x4 lists */
279 280 281 282 283
        for( int p = 0; p < plane_count; p++ )
            for( int i8 = 0; i8 < 4; i8++ )
                if( h->mb.cache.non_zero_count[x264_scan8[p*16+i8*4]] )
                    h->zigzagf.interleave_8x8_cavlc( h->dct.luma4x4[p*16+i8*4], h->dct.luma8x8[p*4+i8],
                                                     &h->mb.cache.non_zero_count[x264_scan8[p*16+i8*4]] );
284 285
    }

286 287
    for( int p = 0; p < plane_count; p++ )
        FOREACH_BIT( i8, 0, h->mb.i_cbp_luma )
288
            for( int i4 = 0; i4 < 4; i4++ )
289 290 291
                x264_cavlc_block_residual( h, DCT_LUMA_4x4, i4+i8*4+p*16, h->dct.luma4x4[i4+i8*4+p*16] );
}

292
#if RDO_SKIP_BS
293
static ALWAYS_INLINE void cavlc_partition_luma_residual( x264_t *h, int i8, int p )
294 295 296 297 298 299 300 301
{
    if( h->mb.b_transform_8x8 && h->mb.cache.non_zero_count[x264_scan8[i8*4]] )
        h->zigzagf.interleave_8x8_cavlc( h->dct.luma4x4[i8*4+p*16], h->dct.luma8x8[i8+p*4],
                                         &h->mb.cache.non_zero_count[x264_scan8[i8*4+p*16]] );

    if( h->mb.i_cbp_luma & (1 << i8) )
        for( int i4 = 0; i4 < 4; i4++ )
            x264_cavlc_block_residual( h, DCT_LUMA_4x4, i4+i8*4+p*16, h->dct.luma4x4[i4+i8*4+p*16] );
302
}
303
#endif
304

305
static void cavlc_mb_header_i( x264_t *h, int i_mb_type, int i_mb_i_offset, int chroma )
Laurent Aimar's avatar
Laurent Aimar committed
306
{
307
    bs_t *s = &h->out.bs;
308
    if( i_mb_type == I_16x16 )
Laurent Aimar's avatar
Laurent Aimar committed
309
    {
310 311
        bs_write_ue( s, i_mb_i_offset + 1 + x264_mb_pred_mode16x16_fix[h->mb.i_intra16x16_pred_mode] +
                        h->mb.i_cbp_chroma * 4 + ( h->mb.i_cbp_luma == 0 ? 0 : 12 ) );
Laurent Aimar's avatar
Laurent Aimar committed
312
    }
313
    else //if( i_mb_type == I_4x4 || i_mb_type == I_8x8 )
Laurent Aimar's avatar
Laurent Aimar committed
314
    {
315
        int di = i_mb_type == I_8x8 ? 4 : 1;
Laurent Aimar's avatar
Laurent Aimar committed
316
        bs_write_ue( s, i_mb_i_offset + 0 );
317 318
        if( h->pps->b_transform_8x8_mode )
            bs_write1( s, h->mb.b_transform_8x8 );
Laurent Aimar's avatar
Laurent Aimar committed
319 320

        /* Prediction: Luma */
321
        for( int i = 0; i < 16; i += di )
Laurent Aimar's avatar
Laurent Aimar committed
322 323
        {
            int i_pred = x264_mb_predict_intra4x4_mode( h, i );
324
            int i_mode = x264_mb_pred_mode4x4_fix( h->mb.cache.intra4x4_pred_mode[x264_scan8[i]] );
Laurent Aimar's avatar
Laurent Aimar committed
325

326
            if( i_pred == i_mode )
Laurent Aimar's avatar
Laurent Aimar committed
327 328
                bs_write1( s, 1 );  /* b_prev_intra4x4_pred_mode */
            else
329
                bs_write( s, 4, i_mode - (i_mode > i_pred) );
Laurent Aimar's avatar
Laurent Aimar committed
330
        }
331

Laurent Aimar's avatar
Laurent Aimar committed
332
    }
333 334 335 336
    if( chroma )
        bs_write_ue( s, x264_mb_chroma_pred_mode_fix[h->mb.i_chroma_pred_mode] );
}

337
static ALWAYS_INLINE void cavlc_mb_header_p( x264_t *h, int i_mb_type, int chroma )
338 339 340
{
    bs_t *s = &h->out.bs;
    if( i_mb_type == P_L0 )
Laurent Aimar's avatar
Laurent Aimar committed
341 342 343
    {
        if( h->mb.i_partition == D_16x16 )
        {
Fiona Glaser's avatar
Fiona Glaser committed
344
            bs_write1( s, 1 );
Laurent Aimar's avatar
Laurent Aimar committed
345

346 347
            if( h->mb.pic.i_fref[0] > 1 )
                bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[0]] );
348
            cavlc_mvd( h, 0, 0, 4 );
Laurent Aimar's avatar
Laurent Aimar committed
349 350 351 352
        }
        else if( h->mb.i_partition == D_16x8 )
        {
            bs_write_ue( s, 1 );
353
            if( h->mb.pic.i_fref[0] > 1 )
Laurent Aimar's avatar
Laurent Aimar committed
354
            {
355 356
                bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[0]] );
                bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[8]] );
Laurent Aimar's avatar
Laurent Aimar committed
357
            }
358 359
            cavlc_mvd( h, 0, 0, 4 );
            cavlc_mvd( h, 0, 8, 4 );
Laurent Aimar's avatar
Laurent Aimar committed
360 361 362 363
        }
        else if( h->mb.i_partition == D_8x16 )
        {
            bs_write_ue( s, 2 );
364
            if( h->mb.pic.i_fref[0] > 1 )
Laurent Aimar's avatar
Laurent Aimar committed
365
            {
366 367
                bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[0]] );
                bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[4]] );
Laurent Aimar's avatar
Laurent Aimar committed
368
            }
369 370
            cavlc_mvd( h, 0, 0, 2 );
            cavlc_mvd( h, 0, 4, 2 );
Laurent Aimar's avatar
Laurent Aimar committed
371 372 373 374
        }
    }
    else if( i_mb_type == P_8x8 )
    {
Fiona Glaser's avatar
Fiona Glaser committed
375
        int b_sub_ref;
376 377
        if( (h->mb.cache.ref[0][x264_scan8[0]] | h->mb.cache.ref[0][x264_scan8[ 4]] |
             h->mb.cache.ref[0][x264_scan8[8]] | h->mb.cache.ref[0][x264_scan8[12]]) == 0 )
Laurent Aimar's avatar
Laurent Aimar committed
378 379
        {
            bs_write_ue( s, 4 );
Fiona Glaser's avatar
Fiona Glaser committed
380
            b_sub_ref = 0;
Laurent Aimar's avatar
Laurent Aimar committed
381 382 383 384
        }
        else
        {
            bs_write_ue( s, 3 );
Fiona Glaser's avatar
Fiona Glaser committed
385
            b_sub_ref = 1;
Laurent Aimar's avatar
Laurent Aimar committed
386
        }
387

Laurent Aimar's avatar
Laurent Aimar committed
388
        /* sub mb type */
389
        if( h->param.analyse.inter & X264_ANALYSE_PSUB8x8 )
390
            for( int i = 0; i < 4; i++ )
391
                bs_write_ue( s, subpartition_p_to_golomb[ h->mb.i_sub_partition[i] ] );
392 393 394
        else
            bs_write( s, 4, 0xf );

Laurent Aimar's avatar
Laurent Aimar committed
395
        /* ref0 */
Fiona Glaser's avatar
Fiona Glaser committed
396
        if( b_sub_ref )
Laurent Aimar's avatar
Laurent Aimar committed
397
        {
398 399 400 401
            bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[0]] );
            bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[4]] );
            bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[8]] );
            bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[12]] );
Laurent Aimar's avatar
Laurent Aimar committed
402
        }
403

404
        for( int i = 0; i < 4; i++ )
405
            cavlc_8x8_mvd( h, i );
406
    }
407
    else //if( IS_INTRA( i_mb_type ) )
408
        cavlc_mb_header_i( h, i_mb_type, 5, chroma );
409 410
}

411
static ALWAYS_INLINE void cavlc_mb_header_b( x264_t *h, int i_mb_type, int chroma )
412 413 414
{
    bs_t *s = &h->out.bs;
    if( i_mb_type == B_8x8 )
415 416 417 418
    {
        bs_write_ue( s, 22 );

        /* sub mb type */
419
        for( int i = 0; i < 4; i++ )
420
            bs_write_ue( s, subpartition_b_to_golomb[ h->mb.i_sub_partition[i] ] );
421

422
        /* ref */
Fiona Glaser's avatar
Fiona Glaser committed
423
        if( h->mb.pic.i_fref[0] > 1 )
424
            for( int i = 0; i < 4; i++ )
Fiona Glaser's avatar
Fiona Glaser committed
425 426 427
                if( x264_mb_partition_listX_table[0][ h->mb.i_sub_partition[i] ] )
                    bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[i*4]] );
        if( h->mb.pic.i_fref[1] > 1 )
428
            for( int i = 0; i < 4; i++ )
Fiona Glaser's avatar
Fiona Glaser committed
429 430
                if( x264_mb_partition_listX_table[1][ h->mb.i_sub_partition[i] ] )
                    bs_write_te( s, h->mb.pic.i_fref[1] - 1, h->mb.cache.ref[1][x264_scan8[i*4]] );
431

432
        /* mvd */
433
        for( int i = 0; i < 4; i++ )
434
            if( x264_mb_partition_listX_table[0][ h->mb.i_sub_partition[i] ] )
435
                cavlc_mvd( h, 0, 4*i, 2 );
436
        for( int i = 0; i < 4; i++ )
437
            if( x264_mb_partition_listX_table[1][ h->mb.i_sub_partition[i] ] )
438
                cavlc_mvd( h, 1, 4*i, 2 );
Laurent Aimar's avatar
Laurent Aimar committed
439
    }
440
    else if( i_mb_type >= B_L0_L0 && i_mb_type <= B_BI_BI )
Laurent Aimar's avatar
Laurent Aimar committed
441 442 443
    {
        /* All B mode */
        /* Motion Vector */
444
        const uint8_t (*b_list)[2] = x264_mb_type_list_table[i_mb_type];
445 446
        const int i_ref0_max = h->mb.pic.i_fref[0] - 1;
        const int i_ref1_max = h->mb.pic.i_fref[1] - 1;
Laurent Aimar's avatar
Laurent Aimar committed
447

Loren Merritt's avatar
Loren Merritt committed
448
        bs_write_ue( s, mb_type_b_to_golomb[ h->mb.i_partition - D_16x8 ][ i_mb_type - B_L0_L0 ] );
Fiona Glaser's avatar
Fiona Glaser committed
449
        if( h->mb.i_partition == D_16x16 )
Laurent Aimar's avatar
Laurent Aimar committed
450
        {
Fiona Glaser's avatar
Fiona Glaser committed
451 452
            if( i_ref0_max && b_list[0][0] ) bs_write_te( s, i_ref0_max, h->mb.cache.ref[0][x264_scan8[0]] );
            if( i_ref1_max && b_list[1][0] ) bs_write_te( s, i_ref1_max, h->mb.cache.ref[1][x264_scan8[0]] );
453 454
            if( b_list[0][0] ) cavlc_mvd( h, 0, 0, 4 );
            if( b_list[1][0] ) cavlc_mvd( h, 1, 0, 4 );
Fiona Glaser's avatar
Fiona Glaser committed
455 456 457 458 459 460 461 462 463
        }
        else
        {
            if( i_ref0_max && b_list[0][0] ) bs_write_te( s, i_ref0_max, h->mb.cache.ref[0][x264_scan8[ 0]] );
            if( i_ref0_max && b_list[0][1] ) bs_write_te( s, i_ref0_max, h->mb.cache.ref[0][x264_scan8[12]] );
            if( i_ref1_max && b_list[1][0] ) bs_write_te( s, i_ref1_max, h->mb.cache.ref[1][x264_scan8[ 0]] );
            if( i_ref1_max && b_list[1][1] ) bs_write_te( s, i_ref1_max, h->mb.cache.ref[1][x264_scan8[12]] );
            if( h->mb.i_partition == D_16x8 )
            {
464 465 466 467
                if( b_list[0][0] ) cavlc_mvd( h, 0, 0, 4 );
                if( b_list[0][1] ) cavlc_mvd( h, 0, 8, 4 );
                if( b_list[1][0] ) cavlc_mvd( h, 1, 0, 4 );
                if( b_list[1][1] ) cavlc_mvd( h, 1, 8, 4 );
Fiona Glaser's avatar
Fiona Glaser committed
468 469 470
            }
            else //if( h->mb.i_partition == D_8x16 )
            {
471 472 473 474
                if( b_list[0][0] ) cavlc_mvd( h, 0, 0, 2 );
                if( b_list[0][1] ) cavlc_mvd( h, 0, 4, 2 );
                if( b_list[1][0] ) cavlc_mvd( h, 1, 0, 2 );
                if( b_list[1][1] ) cavlc_mvd( h, 1, 4, 2 );
Fiona Glaser's avatar
Fiona Glaser committed
475
            }
Laurent Aimar's avatar
Laurent Aimar committed
476 477
        }
    }
478
    else if( i_mb_type == B_DIRECT )
Fiona Glaser's avatar
Fiona Glaser committed
479
        bs_write1( s, 1 );
480
    else //if( IS_INTRA( i_mb_type ) )
481
        cavlc_mb_header_i( h, i_mb_type, 23, chroma );
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
}

/*****************************************************************************
 * x264_macroblock_write:
 *****************************************************************************/
void x264_macroblock_write_cavlc( x264_t *h )
{
    bs_t *s = &h->out.bs;
    const int i_mb_type = h->mb.i_type;
    int plane_count = CHROMA444 ? 3 : 1;
    int chroma = !CHROMA444;

#if RDO_SKIP_BS
    s->i_bits_encoded = 0;
#else
    const int i_mb_pos_start = bs_pos( s );
    int       i_mb_pos_tex;
#endif

    if( SLICE_MBAFF
        && (!(h->mb.i_mb_y & 1) || IS_SKIP(h->mb.type[h->mb.i_mb_xy - h->mb.i_mb_stride])) )
    {
        bs_write1( s, MB_INTERLACED );
505 506 507
#if !RDO_SKIP_BS
        h->mb.field_decoding_flag = MB_INTERLACED;
#endif
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
    }

#if !RDO_SKIP_BS
    if( i_mb_type == I_PCM )
    {
        static const uint8_t i_offsets[3] = {5,23,0};
        uint8_t *p_start = s->p_start;
        bs_write_ue( s, i_offsets[h->sh.i_type] + 25 );
        i_mb_pos_tex = bs_pos( s );
        h->stat.frame.i_mv_bits += i_mb_pos_tex - i_mb_pos_start;

        bs_align_0( s );

        for( int p = 0; p < plane_count; p++ )
            for( int i = 0; i < 256; i++ )
                bs_write( s, BIT_DEPTH, h->mb.pic.p_fenc[p][i] );
        if( chroma )
            for( int ch = 1; ch < 3; ch++ )
526
                for( int i = 0; i < 16>>CHROMA_V_SHIFT; i++ )
527 528 529 530 531 532 533 534 535 536 537 538
                    for( int j = 0; j < 8; j++ )
                        bs_write( s, BIT_DEPTH, h->mb.pic.p_fenc[ch][i*FENC_STRIDE+j] );

        bs_init( s, s->p, s->p_end - s->p );
        s->p_start = p_start;

        h->stat.frame.i_tex_bits += bs_pos(s) - i_mb_pos_tex;
        return;
    }
#endif

    if( h->sh.i_type == SLICE_TYPE_P )
539
        cavlc_mb_header_p( h, i_mb_type, chroma );
540
    else if( h->sh.i_type == SLICE_TYPE_B )
541
        cavlc_mb_header_b( h, i_mb_type, chroma );
542
    else //if( h->sh.i_type == SLICE_TYPE_I )
543
        cavlc_mb_header_i( h, i_mb_type, 0, chroma );
Laurent Aimar's avatar
Laurent Aimar committed
544

545
#if !RDO_SKIP_BS
546
    i_mb_pos_tex = bs_pos( s );
547
    h->stat.frame.i_mv_bits += i_mb_pos_tex - i_mb_pos_start;
548
#endif
549

Fiona Glaser's avatar
Fiona Glaser committed
550 551 552
    /* Coded block pattern */
    if( i_mb_type != I_16x16 )
        bs_write_ue( s, cbp_to_golomb[chroma][IS_INTRA(i_mb_type)][(h->mb.i_cbp_chroma << 4)|h->mb.i_cbp_luma] );
Laurent Aimar's avatar
Laurent Aimar committed
553

554
    /* transform size 8x8 flag */
555
    if( x264_mb_transform_8x8_allowed( h ) && h->mb.i_cbp_luma )
556 557
        bs_write1( s, h->mb.b_transform_8x8 );

Laurent Aimar's avatar
Laurent Aimar committed
558 559
    if( i_mb_type == I_16x16 )
    {
560
        cavlc_qp_delta( h );
Laurent Aimar's avatar
Laurent Aimar committed
561 562

        /* DC Luma */
Fiona Glaser's avatar
Fiona Glaser committed
563 564
        for( int p = 0; p < plane_count; p++ )
        {
565
            x264_cavlc_block_residual( h, DCT_LUMA_DC, LUMA_DC+p, h->dct.luma16x16_dc[p] );
Laurent Aimar's avatar
Laurent Aimar committed
566

Fiona Glaser's avatar
Fiona Glaser committed
567 568 569
            /* AC Luma */
            if( h->mb.i_cbp_luma )
                for( int i = p*16; i < p*16+16; i++ )
570
                    x264_cavlc_block_residual( h, DCT_LUMA_AC, i, h->dct.luma4x4[i]+1 );
Fiona Glaser's avatar
Fiona Glaser committed
571
        }
Laurent Aimar's avatar
Laurent Aimar committed
572
    }
573
    else if( h->mb.i_cbp_luma | h->mb.i_cbp_chroma )
Laurent Aimar's avatar
Laurent Aimar committed
574
    {
575 576
        cavlc_qp_delta( h );
        cavlc_macroblock_luma_residual( h, plane_count );
Laurent Aimar's avatar
Laurent Aimar committed
577
    }
578
    if( h->mb.i_cbp_chroma )
Laurent Aimar's avatar
Laurent Aimar committed
579 580
    {
        /* Chroma DC residual present */
581 582
        x264_cavlc_block_residual( h, DCT_CHROMA_DC, CHROMA_DC+0, h->dct.chroma_dc[0] );
        x264_cavlc_block_residual( h, DCT_CHROMA_DC, CHROMA_DC+1, h->dct.chroma_dc[1] );
Henrik Gramner's avatar
Henrik Gramner committed
583 584
        if( h->mb.i_cbp_chroma == 2 ) /* Chroma AC residual present */
        {
585
            int step = 8 << CHROMA_V_SHIFT;
Henrik Gramner's avatar
Henrik Gramner committed
586 587
            for( int i = 16; i < 3*16; i += step )
                for( int j = i; j < i+4; j++ )
588
                    x264_cavlc_block_residual( h, DCT_CHROMA_AC, j, h->dct.luma4x4[j]+1 );
Henrik Gramner's avatar
Henrik Gramner committed
589
        }
Laurent Aimar's avatar
Laurent Aimar committed
590
    }
591

592
#if !RDO_SKIP_BS
593
    h->stat.frame.i_tex_bits += bs_pos(s) - i_mb_pos_tex;
594
#endif
Laurent Aimar's avatar
Laurent Aimar committed
595
}
596

597
#if RDO_SKIP_BS
598 599 600
/*****************************************************************************
 * RD only; doesn't generate a valid bitstream
 * doesn't write cbp or chroma dc (I don't know how much this matters)
601 602
 * doesn't write ref (never varies between calls, so no point in doing so)
 * only writes subpartition for p8x8, needed for sub-8x8 mode decision RDO
603 604
 * works on all partition sizes except 16x16
 *****************************************************************************/
605
static int partition_size_cavlc( x264_t *h, int i8, int i_pixel )
606
{
607
    bs_t *s = &h->out.bs;
608
    const int i_mb_type = h->mb.i_type;
609
    int b_8x16 = h->mb.i_partition == D_8x16;
Fiona Glaser's avatar
Fiona Glaser committed
610
    int plane_count = CHROMA444 ? 3 : 1;
611 612
    int j;

Anton Mitrofanov's avatar
Anton Mitrofanov committed
613 614
    h->out.bs.i_bits_encoded = 0;

615
    if( i_mb_type == P_8x8 )
616
    {
617
        cavlc_8x8_mvd( h, i8 );
618
        bs_write_ue( s, subpartition_p_to_golomb[ h->mb.i_sub_partition[i8] ] );
619
    }
620
    else if( i_mb_type == P_L0 )
621
        cavlc_mvd( h, 0, 4*i8, 4>>b_8x16 );
622
    else if( i_mb_type > B_DIRECT && i_mb_type < B_8x8 )
623
    {
624 625
        if( x264_mb_type_list_table[ i_mb_type ][0][!!i8] ) cavlc_mvd( h, 0, 4*i8, 4>>b_8x16 );
        if( x264_mb_type_list_table[ i_mb_type ][1][!!i8] ) cavlc_mvd( h, 1, 4*i8, 4>>b_8x16 );
626
    }
627
    else //if( i_mb_type == B_8x8 )
628
    {
629
        if( x264_mb_partition_listX_table[0][ h->mb.i_sub_partition[i8] ] )
630
            cavlc_mvd( h, 0, 4*i8, 2 );
631
        if( x264_mb_partition_listX_table[1][ h->mb.i_sub_partition[i8] ] )
632
            cavlc_mvd( h, 1, 4*i8, 2 );
633 634 635 636
    }

    for( j = (i_pixel < PIXEL_8x8); j >= 0; j-- )
    {
Fiona Glaser's avatar
Fiona Glaser committed
637
        for( int p = 0; p < plane_count; p++ )
638
            cavlc_partition_luma_residual( h, i8, p );
Fiona Glaser's avatar
Fiona Glaser committed
639 640
        if( h->mb.i_cbp_chroma )
        {
Henrik Gramner's avatar
Henrik Gramner committed
641 642 643
            if( CHROMA_FORMAT == CHROMA_422 )
            {
                int offset = (5*i8) & 0x09;
644 645 646 647
                x264_cavlc_block_residual( h, DCT_CHROMA_AC, 16+offset, h->dct.luma4x4[16+offset]+1 );
                x264_cavlc_block_residual( h, DCT_CHROMA_AC, 18+offset, h->dct.luma4x4[18+offset]+1 );
                x264_cavlc_block_residual( h, DCT_CHROMA_AC, 32+offset, h->dct.luma4x4[32+offset]+1 );
                x264_cavlc_block_residual( h, DCT_CHROMA_AC, 34+offset, h->dct.luma4x4[34+offset]+1 );
Henrik Gramner's avatar
Henrik Gramner committed
648 649 650
            }
            else
            {
651 652
                x264_cavlc_block_residual( h, DCT_CHROMA_AC, 16+i8, h->dct.luma4x4[16+i8]+1 );
                x264_cavlc_block_residual( h, DCT_CHROMA_AC, 32+i8, h->dct.luma4x4[32+i8]+1 );
Henrik Gramner's avatar
Henrik Gramner committed
653
            }
Fiona Glaser's avatar
Fiona Glaser committed
654
        }
655 656 657
        i8 += x264_pixel_size[i_pixel].h >> 3;
    }

658
    return h->out.bs.i_bits_encoded;
659 660
}

661
static int subpartition_size_cavlc( x264_t *h, int i4, int i_pixel )
Fiona Glaser's avatar
Fiona Glaser committed
662
{
Fiona Glaser's avatar
Fiona Glaser committed
663
    int plane_count = CHROMA444 ? 3 : 1;
Fiona Glaser's avatar
Fiona Glaser committed
664
    int b_8x4 = i_pixel == PIXEL_8x4;
665
    h->out.bs.i_bits_encoded = 0;
666
    cavlc_mvd( h, 0, i4, 1+b_8x4 );
Fiona Glaser's avatar
Fiona Glaser committed
667
    for( int p = 0; p < plane_count; p++ )
Fiona Glaser's avatar
Fiona Glaser committed
668
    {
669
        x264_cavlc_block_residual( h, DCT_LUMA_4x4, p*16+i4, h->dct.luma4x4[p*16+i4] );
Fiona Glaser's avatar
Fiona Glaser committed
670
        if( i_pixel != PIXEL_4x4 )
671
            x264_cavlc_block_residual( h, DCT_LUMA_4x4, p*16+i4+2-b_8x4, h->dct.luma4x4[p*16+i4+2-b_8x4] );
Fiona Glaser's avatar
Fiona Glaser committed
672 673
    }

674
    return h->out.bs.i_bits_encoded;
Fiona Glaser's avatar
Fiona Glaser committed
675 676
}

677
static int cavlc_intra4x4_pred_size( x264_t *h, int i4, int i_mode )
678 679 680 681 682 683 684
{
    if( x264_mb_predict_intra4x4_mode( h, i4 ) == x264_mb_pred_mode4x4_fix( i_mode ) )
        return 1;
    else
        return 4;
}

685
static int partition_i8x8_size_cavlc( x264_t *h, int i8, int i_mode )
686
{
Fiona Glaser's avatar
Fiona Glaser committed
687
    int plane_count = CHROMA444 ? 3 : 1;
688
    h->out.bs.i_bits_encoded = cavlc_intra4x4_pred_size( h, 4*i8, i_mode );
Fiona Glaser's avatar
Fiona Glaser committed
689 690
    bs_write_ue( &h->out.bs, cbp_to_golomb[!CHROMA444][1][(h->mb.i_cbp_chroma << 4)|h->mb.i_cbp_luma] );
    for( int p = 0; p < plane_count; p++ )
691
        cavlc_partition_luma_residual( h, i8, p );
692 693 694
    return h->out.bs.i_bits_encoded;
}

695
static int partition_i4x4_size_cavlc( x264_t *h, int i4, int i_mode )
696
{
Fiona Glaser's avatar
Fiona Glaser committed
697
    int plane_count = CHROMA444 ? 3 : 1;
698
    h->out.bs.i_bits_encoded = cavlc_intra4x4_pred_size( h, i4, i_mode );
Fiona Glaser's avatar
Fiona Glaser committed
699
    for( int p = 0; p < plane_count; p++ )
700
        x264_cavlc_block_residual( h, DCT_LUMA_4x4, p*16+i4, h->dct.luma4x4[p*16+i4] );
701 702
    return h->out.bs.i_bits_encoded;
}
703

704
static int chroma_size_cavlc( x264_t *h )
705
{
Henrik Gramner's avatar
Henrik Gramner committed
706
    h->out.bs.i_bits_encoded = bs_size_ue( x264_mb_chroma_pred_mode_fix[h->mb.i_chroma_pred_mode] );
707
    if( h->mb.i_cbp_chroma )
708
    {
709 710
        x264_cavlc_block_residual( h, DCT_CHROMA_DC, CHROMA_DC+0, h->dct.chroma_dc[0] );
        x264_cavlc_block_residual( h, DCT_CHROMA_DC, CHROMA_DC+1, h->dct.chroma_dc[1] );
711 712

        if( h->mb.i_cbp_chroma == 2 )
Henrik Gramner's avatar
Henrik Gramner committed
713
        {
714
            int step = 8 << CHROMA_V_SHIFT;
Henrik Gramner's avatar
Henrik Gramner committed
715 716
            for( int i = 16; i < 3*16; i += step )
                for( int j = i; j < i+4; j++ )
717
                    x264_cavlc_block_residual( h, DCT_CHROMA_AC, j, h->dct.luma4x4[j]+1 );
Henrik Gramner's avatar
Henrik Gramner committed
718
        }
719 720 721
    }
    return h->out.bs.i_bits_encoded;
}
722
#endif