FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
cabac.c
Go to the documentation of this file.
1 /*
2  * H.26L/H.264/AVC/JVT/14496-10/... encoder/decoder
3  * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21 
22 /**
23  * @file
24  * Context Adaptive Binary Arithmetic Coder.
25  */
26 
27 #include <string.h>
28 
29 #include "libavutil/common.h"
30 #include "libavutil/timer.h"
31 #include "get_bits.h"
32 #include "cabac.h"
33 #include "cabac_functions.h"
34 
35 #include "cabac_tablegen.h"
36 
37 /**
38  *
39  * @param buf_size size of buf in bits
40  */
42  init_put_bits(&c->pb, buf, buf_size);
43 
44  c->low= 0;
45  c->range= 0x1FE;
46  c->outstanding_count= 0;
47  c->pb.bit_left++; //avoids firstBitFlag
48 }
49 
50 /**
51  *
52  * @param buf_size size of buf in bits
53  */
54 void ff_init_cabac_decoder(CABACContext *c, const uint8_t *buf, int buf_size){
56  c->bytestream= buf;
57  c->bytestream_end= buf + buf_size;
58 
59 #if CABAC_BITS == 16
60  c->low = (*c->bytestream++)<<18;
61  c->low+= (*c->bytestream++)<<10;
62 #else
63  c->low = (*c->bytestream++)<<10;
64 #endif
65  c->low+= ((*c->bytestream++)<<2) + 2;
66  c->range= 0x1FE;
67 }
68 
70 {
71  static int initialized = 0;
72 
73  if (initialized)
74  return;
75 
77 
78  initialized = 1;
79 }
80 
81 #ifdef TEST
82 #define SIZE 10240
83 
84 #include "libavutil/lfg.h"
85 #include "avcodec.h"
86 
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);
91  }
92 }
93 
94 static inline void renorm_cabac_encoder(CABACContext *c){
95  while(c->range < 0x100){
96  //FIXME optimize
97  if(c->low<0x100){
98  put_cabac_bit(c, 0);
99  }else if(c->low<0x200){
100  c->outstanding_count++;
101  c->low -= 0x100;
102  }else{
103  put_cabac_bit(c, 1);
104  c->low -= 0x200;
105  }
106 
107  c->range+= c->range;
108  c->low += c->low;
109  }
110 }
111 
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];
114 
115  if(bit == ((*state)&1)){
116  c->range -= RangeLPS;
117  *state = ff_h264_mlps_state[128 + *state];
118  }else{
119  c->low += c->range - RangeLPS;
120  c->range = RangeLPS;
121  *state= ff_h264_mlps_state[127 - *state];
122  }
123 
124  renorm_cabac_encoder(c);
125 }
126 
127 /**
128  * @param bit 0 -> write zero bit, !=0 write one bit
129  */
130 static void put_cabac_bypass(CABACContext *c, int bit){
131  c->low += c->low;
132 
133  if(bit){
134  c->low += c->range;
135  }
136 //FIXME optimize
137  if(c->low<0x200){
138  put_cabac_bit(c, 0);
139  }else if(c->low<0x400){
140  c->outstanding_count++;
141  c->low -= 0x200;
142  }else{
143  put_cabac_bit(c, 1);
144  c->low -= 0x400;
145  }
146 }
147 
148 /**
149  *
150  * @return the number of bytes written
151  */
152 static int put_cabac_terminate(CABACContext *c, int bit){
153  c->range -= 2;
154 
155  if(!bit){
156  renorm_cabac_encoder(c);
157  }else{
158  c->low += c->range;
159  c->range= 2;
160 
161  renorm_cabac_encoder(c);
162 
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);
166 
167  flush_put_bits(&c->pb); //FIXME FIXME FIXME XXX wrong
168  }
169 
170  return (put_bits_count(&c->pb)+7)>>3;
171 }
172 
173 int main(void){
174  CABACContext c;
175  uint8_t b[9*SIZE];
176  uint8_t r[9*SIZE];
177  int i, ret = 0;
178  uint8_t state[10]= {0};
179  AVLFG prng;
180 
181  av_lfg_init(&prng, 1);
182  ff_init_cabac_encoder(&c, b, SIZE);
184 
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;
188  }
189 
190  for(i=0; i<SIZE; i++){
191  put_cabac_bypass(&c, r[i]&1);
192  }
193 
194  for(i=0; i<SIZE; i++){
195  put_cabac(&c, state, r[i]&1);
196  }
197 
198  put_cabac_terminate(&c, 1);
199 
200  ff_init_cabac_decoder(&c, b, SIZE);
201 
202  memset(state, 0, sizeof(state));
203 
204  for(i=0; i<SIZE; i++){
205  if( (r[i]&1) != get_cabac_bypass(&c) ) {
206  av_log(NULL, AV_LOG_ERROR, "CABAC bypass failure at %d\n", i);
207  ret = 1;
208  }
209  }
210 
211  for(i=0; i<SIZE; i++){
212  if( (r[i]&1) != get_cabac_noinline(&c, state) ) {
213  av_log(NULL, AV_LOG_ERROR, "CABAC failure at %d\n", i);
214  ret = 1;
215  }
216  }
217  if(!get_cabac_terminate(&c)) {
218  av_log(NULL, AV_LOG_ERROR, "where's the Terminator?\n");
219  ret = 1;
220  }
221 
222  return ret;
223 }
224 
225 #endif /* TEST */
Definition: lfg.h:25
#define NULL
Definition: coverity.c:32
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:160
const uint8_t * bytestream_end
Definition: cabac.h:54
const char * b
Definition: vf_curves.c:109
static CABAC_TABLE_CONST uint8_t *const ff_h264_mlps_state
static int av_noinline av_unused get_cabac_noinline(CABACContext *c, uint8_t *const state)
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
const uint8_t * bytestream
Definition: cabac.h:53
uint8_t
#define SIZE
Definition: golomb-test.c:31
bitstream reader API header.
high precision timer, useful to profile code
#define av_log(a,...)
static CABAC_TABLE_CONST uint8_t *const ff_h264_lps_range
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
const char * r
Definition: vf_curves.c:107
void ff_init_cabac_states(void)
Definition: cabac.c:69
static int put_bits_count(PutBitContext *s)
Definition: put_bits.h:85
const uint8_t * bytestream_start
Definition: cabac.h:52
ret
Definition: avfilter.c:974
Context Adaptive Binary Arithmetic Coder inline functions.
void ff_init_cabac_encoder(CABACContext *c, uint8_t *buf, int buf_size)
Definition: cabac.c:41
static int av_unused get_cabac_terminate(CABACContext *c)
int bit_left
Definition: put_bits.h:37
int outstanding_count
Definition: cabac.h:51
static av_cold void cabac_tableinit(void)
static unsigned int av_lfg_get(AVLFG *c)
Get the next random unsigned 32-bit number using an ALFG.
Definition: lfg.h:38
void * buf
Definition: avisynth_c.h:553
int range
Definition: cabac.h:50
av_cold void av_lfg_init(AVLFG *c, unsigned int seed)
Definition: lfg.c:30
PutBitContext pb
Definition: cabac.h:55
static uint32_t state
Definition: trasher.c:27
static int av_unused get_cabac_bypass(CABACContext *c)
int low
Definition: cabac.h:49
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:101
common internal and external API header
static double c[64]
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:48
void ff_init_cabac_decoder(CABACContext *c, const uint8_t *buf, int buf_size)
Definition: cabac.c:54
int main(int argc, char **argv)
Definition: main.c:22
Context Adaptive Binary Arithmetic Coder.