x264-dsp
cabac.h
1 /*****************************************************************************
2  * cabac.h: arithmetic coder
3  *****************************************************************************/
4 
5 #ifndef X264_CABAC_H
6 #define X264_CABAC_H
7 
8 typedef struct
9 {
10  /* state */
11  int i_low;
12  int i_range;
13 
14  /* bit stream */
15  int i_queue; // stored with an offset of -8 for faster asm
16  int i_bytes_outstanding;
17 
18  uint8_t *p_start; /* start position of bitstream. refer to h->out.bs.p_start */
19  uint8_t *p; /* current position of bitstream. refer to h->out.bs.p */
20  uint8_t *p_end; /* end position of bitstream. refer to h->out.bs.p_end */
21 
22  /* aligned for memcpy_aligned starting here */
23  ALIGNED_16(int f8_bits_encoded); // only if using x264_cabac_size_decision()
24 
25  /************************************************************************
26  * States of all contexts are keeped in following state array. *
27  * The initial values are based on current slice type and qp. *
28  * States include following two parts, value range: [2~127] *
29  * The high 7 bit represents probability index of LPS. value: [1~63] *
30  * The lowest bit represents MPS(Most Probability Symbol). value: [0~1] *
31  * Odd means MPS is 1, even means MPS is 0. *
32  ************************************************************************/
33  uint8_t state[276];
34 
35  /* for 16-byte alignment */
36  uint8_t padding[12];
37 } x264_cabac_t;
38 
39 extern const uint8_t x264_cabac_transition[128][2];
40 extern const uint16_t x264_cabac_entropy[128];
41 
42 /* init the contexts given i_slice_type, the quantif and the model */
43 void x264_cabac_context_init(x264_t *h, x264_cabac_t *cb, int i_slice_type, int i_qp, int i_model);
44 void x264_cabac_encode_init(x264_cabac_t *cb, uint8_t *p_data, uint8_t *p_end);
45 void x264_cabac_encode_decision_c(x264_cabac_t *cb, int i_ctx, int b);
46 void x264_cabac_encode_bypass_c(x264_cabac_t *cb, int b);
47 void x264_cabac_encode_terminal_c(x264_cabac_t *cb);
48 void x264_cabac_encode_ue_bypass(x264_cabac_t *cb, int exp_bits, int val);
49 void x264_cabac_encode_flush(x264_t *h, x264_cabac_t *cb);
50 
51 #define x264_cabac_encode_decision x264_cabac_encode_decision_c
52 #define x264_cabac_encode_bypass x264_cabac_encode_bypass_c
53 #define x264_cabac_encode_terminal x264_cabac_encode_terminal_c
54 #define x264_cabac_encode_decision_noup x264_cabac_encode_decision
55 
56 static ALWAYS_INLINE int x264_cabac_pos(x264_cabac_t *cb) {
57  return ((cb->p - cb->p_start + cb->i_bytes_outstanding) << 3) + cb->i_queue;
58 }
59 
60 /* internal only. these don't write the bitstream, just calculate bit cost: */
61 
62 static ALWAYS_INLINE void x264_cabac_size_decision(x264_cabac_t *cb, long i_ctx, long b) {
63  int i_state = cb->state[i_ctx];
64  cb->state[i_ctx] = x264_cabac_transition[i_state][b];
65  cb->f8_bits_encoded += x264_cabac_entropy[i_state ^ b];
66 }
67 
68 static ALWAYS_INLINE int x264_cabac_size_decision2(uint8_t *state, long b) {
69  int i_state = *state;
70  *state = x264_cabac_transition[i_state][b];
71  return x264_cabac_entropy[i_state ^ b];
72 }
73 
74 static ALWAYS_INLINE void x264_cabac_size_decision_noup(x264_cabac_t *cb, long i_ctx, long b) {
75  int i_state = cb->state[i_ctx];
76  cb->f8_bits_encoded += x264_cabac_entropy[i_state ^ b];
77 }
78 
79 static ALWAYS_INLINE int x264_cabac_size_decision_noup2(uint8_t *state, long b) {
80  return x264_cabac_entropy[*state ^ b];
81 }
82 
83 #endif
Definition: me.h:11
Definition: cabac.h:9
Definition: common.h:499