2 * H.26L/H.264/AVC/JVT/14496-10/... encoder/decoder
3 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * Context Adaptive Binary Arithmetic Coder.
29 #include "libavutil/common.h"
30 #include "libavutil/timer.h"
33 #include "cabac_functions.h"
35 #include "cabac_tablegen.h"
39 * @param buf_size size of buf in bits
41 void ff_init_cabac_encoder(CABACContext
*c
, uint8_t *buf
, int buf_size
){
42 init_put_bits(&c
->pb
, buf
, buf_size
);
46 c
->outstanding_count
= 0;
47 c
->pb
.bit_left
++; //avoids firstBitFlag
52 * @param buf_size size of buf in bits
54 void ff_init_cabac_decoder(CABACContext
*c
, const uint8_t *buf
, int buf_size
){
57 c
->bytestream_end
= buf
+ buf_size
;
60 c
->low
= (*c
->bytestream
++)<<18;
61 c
->low
+= (*c
->bytestream
++)<<10;
63 c
->low
= (*c
->bytestream
++)<<10;
65 c
->low
+= ((*c
->bytestream
++)<<2) + 2;
69 void ff_init_cabac_states(void)
71 static int initialized
= 0;
84 #include "libavutil/lfg.h"
87 static inline void put_cabac_bit(CABACContext
*c
, int b
){
88 put_bits(&c
->pb
, 1, b
);
89 for(;c
->outstanding_count
; c
->outstanding_count
--){
90 put_bits(&c
->pb
, 1, 1-b
);
94 static inline void renorm_cabac_encoder(CABACContext
*c
){
95 while(c
->range
< 0x100){
99 }else if(c
->low
<0x200){
100 c
->outstanding_count
++;
112 static void put_cabac(CABACContext
*c
, uint8_t * const state
, int bit
){
113 int RangeLPS
= ff_h264_lps_range
[2*(c
->range
&0xC0) + *state
];
115 if(bit
== ((*state
)&1)){
116 c
->range
-= RangeLPS
;
117 *state
= ff_h264_mlps_state
[128 + *state
];
119 c
->low
+= c
->range
- RangeLPS
;
121 *state
= ff_h264_mlps_state
[127 - *state
];
124 renorm_cabac_encoder(c
);
128 * @param bit 0 -> write zero bit, !=0 write one bit
130 static void put_cabac_bypass(CABACContext
*c
, int bit
){
139 }else if(c
->low
<0x400){
140 c
->outstanding_count
++;
150 * @return the number of bytes written
152 static int put_cabac_terminate(CABACContext
*c
, int bit
){
156 renorm_cabac_encoder(c
);
161 renorm_cabac_encoder(c
);
163 av_assert0(c
->low
<= 0x1FF);
164 put_cabac_bit(c
, c
->low
>>9);
165 put_bits(&c
->pb
, 2, ((c
->low
>>7)&3)|1);
167 flush_put_bits(&c
->pb
); //FIXME FIXME FIXME XXX wrong
170 return (put_bits_count(&c
->pb
)+7)>>3;
178 uint8_t state
[10]= {0};
181 av_lfg_init(&prng
, 1);
182 ff_init_cabac_encoder(&c
, b
, SIZE
);
183 ff_init_cabac_states();
185 for(i
=0; i
<SIZE
; i
++){
186 if(2*i
<SIZE
) r
[i
] = av_lfg_get(&prng
) % 7;
187 else r
[i
] = (i
>>8)&1;
190 for(i
=0; i
<SIZE
; i
++){
192 put_cabac_bypass(&c
, r
[i
]&1);
193 STOP_TIMER("put_cabac_bypass")
196 for(i
=0; i
<SIZE
; i
++){
198 put_cabac(&c
, state
, r
[i
]&1);
199 STOP_TIMER("put_cabac")
202 put_cabac_terminate(&c
, 1);
204 ff_init_cabac_decoder(&c
, b
, SIZE
);
206 memset(state
, 0, sizeof(state
));
208 for(i
=0; i
<SIZE
; i
++){
210 if( (r
[i
]&1) != get_cabac_bypass(&c
) )
211 av_log(NULL
, AV_LOG_ERROR
, "CABAC bypass failure at %d\n", i
);
212 STOP_TIMER("get_cabac_bypass")
215 for(i
=0; i
<SIZE
; i
++){
217 if( (r
[i
]&1) != get_cabac_noinline(&c
, state
) )
218 av_log(NULL
, AV_LOG_ERROR
, "CABAC failure at %d\n", i
);
219 STOP_TIMER("get_cabac")
221 if(!get_cabac_terminate(&c
))
222 av_log(NULL
, AV_LOG_ERROR
, "where's the Terminator?\n");