cabac.h 5.01 KB
Newer Older
1
/*****************************************************************************
2
 * cabac.h: arithmetic coder
3
 *****************************************************************************
Henrik Gramner's avatar
Henrik Gramner committed
4
 * Copyright (C) 2003-2019 x264 project
5
 *
6 7
 * Authors: Loren Merritt <lorenm@u.washington.edu>
 *          Laurent Aimar <fenrir@via.ecp.fr>
8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * 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
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
22 23 24
 *
 * This program is also available under a commercial proprietary license.
 * For more information, contact us at licensing@x264.com.
25 26
 *****************************************************************************/

27 28
#ifndef X264_CABAC_H
#define X264_CABAC_H
29 30 31 32 33 34 35 36

typedef struct
{
    /* state */
    int i_low;
    int i_range;

    /* bit stream */
37
    int i_queue; //stored with an offset of -8 for faster asm
38 39 40 41 42
    int i_bytes_outstanding;

    uint8_t *p_start;
    uint8_t *p;
    uint8_t *p_end;
43

44
    /* aligned for memcpy_aligned starting here */
45
    ALIGNED_64( int f8_bits_encoded ); // only if using x264_cabac_size_decision()
Loren Merritt's avatar
Loren Merritt committed
46

47
    /* context */
Fiona Glaser's avatar
Fiona Glaser committed
48 49 50 51
    uint8_t state[1024];

    /* for 16-byte alignment */
    uint8_t padding[12];
52 53
} x264_cabac_t;

54
/* init the contexts given i_slice_type, the quantif and the model */
55
#define x264_cabac_context_init x264_template(cabac_context_init)
Fiona Glaser's avatar
Fiona Glaser committed
56
void x264_cabac_context_init( x264_t *h, x264_cabac_t *cb, int i_slice_type, int i_qp, int i_model );
57

58
#define x264_cabac_encode_init_core x264_template(cabac_encode_init_core)
59
void x264_cabac_encode_init_core( x264_cabac_t *cb );
60
#define x264_cabac_encode_init x264_template(cabac_encode_init)
Anton Mitrofanov's avatar
Anton Mitrofanov committed
61
void x264_cabac_encode_init( x264_cabac_t *cb, uint8_t *p_data, uint8_t *p_end );
62
#define x264_cabac_encode_decision_c x264_template(cabac_encode_decision_c)
Loren Merritt's avatar
Loren Merritt committed
63
void x264_cabac_encode_decision_c( x264_cabac_t *cb, int i_ctx, int b );
64
#define x264_cabac_encode_decision_asm x264_template(cabac_encode_decision_asm)
Loren Merritt's avatar
Loren Merritt committed
65
void x264_cabac_encode_decision_asm( x264_cabac_t *cb, int i_ctx, int b );
66
#define x264_cabac_encode_bypass_c x264_template(cabac_encode_bypass_c)
67
void x264_cabac_encode_bypass_c( x264_cabac_t *cb, int b );
68
#define x264_cabac_encode_bypass_asm x264_template(cabac_encode_bypass_asm)
69
void x264_cabac_encode_bypass_asm( x264_cabac_t *cb, int b );
70
#define x264_cabac_encode_terminal_c x264_template(cabac_encode_terminal_c)
71
void x264_cabac_encode_terminal_c( x264_cabac_t *cb );
72
#define x264_cabac_encode_terminal_asm x264_template(cabac_encode_terminal_asm)
73
void x264_cabac_encode_terminal_asm( x264_cabac_t *cb );
74
#define x264_cabac_encode_ue_bypass x264_template(cabac_encode_ue_bypass)
75
void x264_cabac_encode_ue_bypass( x264_cabac_t *cb, int exp_bits, int val );
76
#define x264_cabac_encode_flush x264_template(cabac_encode_flush)
77
void x264_cabac_encode_flush( x264_t *h, x264_cabac_t *cb );
78

Steven Walters's avatar
Steven Walters committed
79
#if HAVE_MMX
Loren Merritt's avatar
Loren Merritt committed
80
#define x264_cabac_encode_decision x264_cabac_encode_decision_asm
81 82
#define x264_cabac_encode_bypass x264_cabac_encode_bypass_asm
#define x264_cabac_encode_terminal x264_cabac_encode_terminal_asm
83 84 85 86
#elif defined(ARCH_AARCH64)
#define x264_cabac_encode_decision x264_cabac_encode_decision_asm
#define x264_cabac_encode_bypass x264_cabac_encode_bypass_asm
#define x264_cabac_encode_terminal x264_cabac_encode_terminal_asm
Loren Merritt's avatar
Loren Merritt committed
87 88
#else
#define x264_cabac_encode_decision x264_cabac_encode_decision_c
89 90
#define x264_cabac_encode_bypass x264_cabac_encode_bypass_c
#define x264_cabac_encode_terminal x264_cabac_encode_terminal_c
Loren Merritt's avatar
Loren Merritt committed
91
#endif
92
#define x264_cabac_encode_decision_noup x264_cabac_encode_decision
Loren Merritt's avatar
Loren Merritt committed
93

94
static ALWAYS_INLINE int x264_cabac_pos( x264_cabac_t *cb )
95
{
96
    return (cb->p - cb->p_start + cb->i_bytes_outstanding) * 8 + cb->i_queue;
97
}
98

99 100
/* internal only. these don't write the bitstream, just calculate bit cost: */

101
static ALWAYS_INLINE void x264_cabac_size_decision( x264_cabac_t *cb, long i_ctx, long b )
102 103
{
    int i_state = cb->state[i_ctx];
Loren Merritt's avatar
Loren Merritt committed
104
    cb->state[i_ctx] = x264_cabac_transition[i_state][b];
105
    cb->f8_bits_encoded += x264_cabac_entropy[i_state^b];
106 107
}

108
static ALWAYS_INLINE int x264_cabac_size_decision2( uint8_t *state, long b )
109 110
{
    int i_state = *state;
Loren Merritt's avatar
Loren Merritt committed
111
    *state = x264_cabac_transition[i_state][b];
112
    return x264_cabac_entropy[i_state^b];
113 114
}

115
static ALWAYS_INLINE void x264_cabac_size_decision_noup( x264_cabac_t *cb, long i_ctx, long b )
116 117
{
    int i_state = cb->state[i_ctx];
118
    cb->f8_bits_encoded += x264_cabac_entropy[i_state^b];
119 120
}

121
static ALWAYS_INLINE int x264_cabac_size_decision_noup2( uint8_t *state, long b )
122
{
123
    return x264_cabac_entropy[*state^b];
124 125
}

126
#endif