2 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "libavcodec/cabac.c"
25 #include "libavutil/lfg.h"
26 #include "libavcodec/avcodec.h"
28 static inline void put_cabac_bit(CABACContext *c, int b){
29 put_bits(&c->pb, 1, b);
30 for(;c->outstanding_count; c->outstanding_count--){
31 put_bits(&c->pb, 1, 1-b);
35 static inline void renorm_cabac_encoder(CABACContext *c){
36 while(c->range < 0x100){
40 }else if(c->low<0x200){
41 c->outstanding_count++;
53 static void put_cabac(CABACContext *c, uint8_t * const state, int bit){
54 int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + *state];
56 if(bit == ((*state)&1)){
58 *state = ff_h264_mlps_state[128 + *state];
60 c->low += c->range - RangeLPS;
62 *state= ff_h264_mlps_state[127 - *state];
65 renorm_cabac_encoder(c);
69 * @param bit 0 -> write zero bit, !=0 write one bit
71 static void put_cabac_bypass(CABACContext *c, int bit){
80 }else if(c->low<0x400){
81 c->outstanding_count++;
91 * @return the number of bytes written
93 static int put_cabac_terminate(CABACContext *c, int bit){
97 renorm_cabac_encoder(c);
102 renorm_cabac_encoder(c);
104 av_assert0(c->low <= 0x1FF);
105 put_cabac_bit(c, c->low>>9);
106 put_bits(&c->pb, 2, ((c->low>>7)&3)|1);
108 flush_put_bits(&c->pb); //FIXME FIXME FIXME XXX wrong
111 return (put_bits_count(&c->pb)+7)>>3;
119 uint8_t state[10]= {0};
122 av_lfg_init(&prng, 1);
123 ff_init_cabac_encoder(&c, b, SIZE);
125 for(i=0; i<SIZE; i++){
126 if(2*i<SIZE) r[i] = av_lfg_get(&prng) % 7;
127 else r[i] = (i>>8)&1;
130 for(i=0; i<SIZE; i++){
131 put_cabac_bypass(&c, r[i]&1);
134 for(i=0; i<SIZE; i++){
135 put_cabac(&c, state, r[i]&1);
138 i= put_cabac_terminate(&c, 1);
139 b[i++] = av_lfg_get(&prng);
140 b[i ] = av_lfg_get(&prng);
142 ff_init_cabac_decoder(&c, b, SIZE);
144 memset(state, 0, sizeof(state));
146 for(i=0; i<SIZE; i++){
147 if( (r[i]&1) != get_cabac_bypass(&c) ) {
148 av_log(NULL, AV_LOG_ERROR, "CABAC bypass failure at %d\n", i);
153 for(i=0; i<SIZE; i++){
154 if( (r[i]&1) != get_cabac_noinline(&c, state) ) {
155 av_log(NULL, AV_LOG_ERROR, "CABAC failure at %d\n", i);
159 if(!get_cabac_terminate(&c)) {
160 av_log(NULL, AV_LOG_ERROR, "where's the Terminator?\n");