FFmpeg  2.6.9
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules 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 int 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  if ((c->range<<(CABAC_BITS+1)) < c->low)
68  return AVERROR_INVALIDDATA;
69  return 0;
70 }
71 
73 {
74  static int initialized = 0;
75 
76  if (initialized)
77  return;
78 
80 
81  initialized = 1;
82 }
83 
84 #ifdef TEST
85 #define SIZE 10240
86 
87 #include "libavutil/lfg.h"
88 #include "avcodec.h"
89 
90 static inline void put_cabac_bit(CABACContext *c, int b){
91  put_bits(&c->pb, 1, b);
92  for(;c->outstanding_count; c->outstanding_count--){
93  put_bits(&c->pb, 1, 1-b);
94  }
95 }
96 
97 static inline void renorm_cabac_encoder(CABACContext *c){
98  while(c->range < 0x100){
99  //FIXME optimize
100  if(c->low<0x100){
101  put_cabac_bit(c, 0);
102  }else if(c->low<0x200){
103  c->outstanding_count++;
104  c->low -= 0x100;
105  }else{
106  put_cabac_bit(c, 1);
107  c->low -= 0x200;
108  }
109 
110  c->range+= c->range;
111  c->low += c->low;
112  }
113 }
114 
115 static void put_cabac(CABACContext *c, uint8_t * const state, int bit){
116  int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + *state];
117 
118  if(bit == ((*state)&1)){
119  c->range -= RangeLPS;
120  *state = ff_h264_mlps_state[128 + *state];
121  }else{
122  c->low += c->range - RangeLPS;
123  c->range = RangeLPS;
124  *state= ff_h264_mlps_state[127 - *state];
125  }
126 
127  renorm_cabac_encoder(c);
128 }
129 
130 /**
131  * @param bit 0 -> write zero bit, !=0 write one bit
132  */
133 static void put_cabac_bypass(CABACContext *c, int bit){
134  c->low += c->low;
135 
136  if(bit){
137  c->low += c->range;
138  }
139 //FIXME optimize
140  if(c->low<0x200){
141  put_cabac_bit(c, 0);
142  }else if(c->low<0x400){
143  c->outstanding_count++;
144  c->low -= 0x200;
145  }else{
146  put_cabac_bit(c, 1);
147  c->low -= 0x400;
148  }
149 }
150 
151 /**
152  *
153  * @return the number of bytes written
154  */
155 static int put_cabac_terminate(CABACContext *c, int bit){
156  c->range -= 2;
157 
158  if(!bit){
159  renorm_cabac_encoder(c);
160  }else{
161  c->low += c->range;
162  c->range= 2;
163 
164  renorm_cabac_encoder(c);
165 
166  av_assert0(c->low <= 0x1FF);
167  put_cabac_bit(c, c->low>>9);
168  put_bits(&c->pb, 2, ((c->low>>7)&3)|1);
169 
170  flush_put_bits(&c->pb); //FIXME FIXME FIXME XXX wrong
171  }
172 
173  return (put_bits_count(&c->pb)+7)>>3;
174 }
175 
176 int main(void){
177  CABACContext c;
178  uint8_t b[9*SIZE];
179  uint8_t r[9*SIZE];
180  int i;
181  uint8_t state[10]= {0};
182  AVLFG prng;
183 
184  av_lfg_init(&prng, 1);
185  ff_init_cabac_encoder(&c, b, SIZE);
187 
188  for(i=0; i<SIZE; i++){
189  if(2*i<SIZE) r[i] = av_lfg_get(&prng) % 7;
190  else r[i] = (i>>8)&1;
191  }
192 
193  for(i=0; i<SIZE; i++){
195  put_cabac_bypass(&c, r[i]&1);
196 STOP_TIMER("put_cabac_bypass")
197  }
198 
199  for(i=0; i<SIZE; i++){
201  put_cabac(&c, state, r[i]&1);
202 STOP_TIMER("put_cabac")
203  }
204 
205  put_cabac_terminate(&c, 1);
206 
207  ff_init_cabac_decoder(&c, b, SIZE);
208 
209  memset(state, 0, sizeof(state));
210 
211  for(i=0; i<SIZE; i++){
213  if( (r[i]&1) != get_cabac_bypass(&c) )
214  av_log(NULL, AV_LOG_ERROR, "CABAC bypass failure at %d\n", i);
215 STOP_TIMER("get_cabac_bypass")
216  }
217 
218  for(i=0; i<SIZE; i++){
220  if( (r[i]&1) != get_cabac_noinline(&c, state) )
221  av_log(NULL, AV_LOG_ERROR, "CABAC failure at %d\n", i);
222 STOP_TIMER("get_cabac")
223  }
224  if(!get_cabac_terminate(&c))
225  av_log(NULL, AV_LOG_ERROR, "where's the Terminator?\n");
226 
227  return 0;
228 }
229 
230 #endif /* TEST */
Definition: lfg.h:25
#define NULL
Definition: coverity.c:32
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:198
const uint8_t * bytestream_end
Definition: cabac.h:54
const char * b
Definition: vf_curves.c:109
#define CABAC_BITS
Definition: cabac.h:45
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:175
const char * r
Definition: vf_curves.c:107
void ff_init_cabac_states(void)
Definition: cabac.c:72
static int put_bits_count(PutBitContext *s)
Definition: put_bits.h:85
const uint8_t * bytestream_start
Definition: cabac.h:52
int ff_init_cabac_decoder(CABACContext *c, const uint8_t *buf, int buf_size)
Definition: cabac.c:54
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
#define START_TIMER
Definition: timer.h:86
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:595
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
#define STOP_TIMER(id)
Definition: timer.h:87
int main(int argc, char **argv)
Definition: main.c:22
Context Adaptive Binary Arithmetic Coder.