]> git.sesse.net Git - x264/blob - common/cabac.h
Much faster weightp
[x264] / common / cabac.h
1 /*****************************************************************************
2  * cabac.h: h264 encoder library
3  *****************************************************************************
4  * Copyright (C) 2003-2008 x264 project
5  *
6  * Authors: Loren Merritt <lorenm@u.washington.edu>
7  *          Laurent Aimar <fenrir@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * 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 #ifndef X264_CABAC_H
25 #define X264_CABAC_H
26
27 typedef struct
28 {
29     /* state */
30     int i_low;
31     int i_range;
32
33     /* bit stream */
34     int i_queue;
35     int i_bytes_outstanding;
36
37     uint8_t *p_start;
38     uint8_t *p;
39     uint8_t *p_end;
40
41     /* aligned for memcpy_aligned starting here */
42     ALIGNED_16( int f8_bits_encoded ); // only if using x264_cabac_size_decision()
43
44     /* context */
45     uint8_t state[460];
46 } x264_cabac_t;
47
48 extern const uint8_t x264_cabac_transition[128][2];
49 extern const uint16_t x264_cabac_entropy[128][2];
50
51 /* init the contexts given i_slice_type, the quantif and the model */
52 void x264_cabac_context_init( x264_cabac_t *cb, int i_slice_type, int i_qp, int i_model );
53
54 /* encoder only: */
55 void x264_cabac_encode_init ( x264_cabac_t *cb, uint8_t *p_data, uint8_t *p_end );
56 void x264_cabac_encode_decision_c( x264_cabac_t *cb, int i_ctx, int b );
57 void x264_cabac_encode_decision_asm( x264_cabac_t *cb, int i_ctx, int b );
58 void x264_cabac_encode_bypass( x264_cabac_t *cb, int b );
59 void x264_cabac_encode_ue_bypass( x264_cabac_t *cb, int exp_bits, int val );
60 void x264_cabac_encode_terminal( x264_cabac_t *cb );
61 void x264_cabac_encode_flush( x264_t *h, x264_cabac_t *cb );
62
63 #ifdef HAVE_MMX
64 #define x264_cabac_encode_decision x264_cabac_encode_decision_asm
65 #else
66 #define x264_cabac_encode_decision x264_cabac_encode_decision_c
67 #endif
68 #define x264_cabac_encode_decision_noup x264_cabac_encode_decision
69
70 static inline int x264_cabac_pos( x264_cabac_t *cb )
71 {
72     return (cb->p - cb->p_start + cb->i_bytes_outstanding) * 8 + cb->i_queue;
73 }
74
75 /* internal only. these don't write the bitstream, just calculate bit cost: */
76
77 static inline void x264_cabac_size_decision( x264_cabac_t *cb, long i_ctx, long b )
78 {
79     int i_state = cb->state[i_ctx];
80     cb->state[i_ctx] = x264_cabac_transition[i_state][b];
81     cb->f8_bits_encoded += x264_cabac_entropy[i_state][b];
82 }
83
84 static inline int x264_cabac_size_decision2( uint8_t *state, long b )
85 {
86     int i_state = *state;
87     *state = x264_cabac_transition[i_state][b];
88     return x264_cabac_entropy[i_state][b];
89 }
90
91 static inline void x264_cabac_size_decision_noup( x264_cabac_t *cb, long i_ctx, long b )
92 {
93     int i_state = cb->state[i_ctx];
94     cb->f8_bits_encoded += x264_cabac_entropy[i_state][b];
95 }
96
97 static inline int x264_cabac_size_decision_noup2( uint8_t *state, long b )
98 {
99     return x264_cabac_entropy[*state][b];
100 }
101
102 #endif