FFmpeg
cabac.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
3  *
4  * This file is part of FFmpeg.
5  *
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.
10  *
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.
15  *
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
19  */
20 
21 #include "libavcodec/cabac.c"
22 
23 #define SIZE 10240
24 
25 #include "libavutil/lfg.h"
26 #include "libavcodec/avcodec.h"
27 
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);
32  }
33 }
34 
35 static inline void renorm_cabac_encoder(CABACContext *c){
36  while(c->range < 0x100){
37  //FIXME optimize
38  if(c->low<0x100){
39  put_cabac_bit(c, 0);
40  }else if(c->low<0x200){
41  c->outstanding_count++;
42  c->low -= 0x100;
43  }else{
44  put_cabac_bit(c, 1);
45  c->low -= 0x200;
46  }
47 
48  c->range+= c->range;
49  c->low += c->low;
50  }
51 }
52 
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];
55 
56  if(bit == ((*state)&1)){
57  c->range -= RangeLPS;
58  *state = ff_h264_mlps_state[128 + *state];
59  }else{
60  c->low += c->range - RangeLPS;
61  c->range = RangeLPS;
62  *state= ff_h264_mlps_state[127 - *state];
63  }
64 
66 }
67 
68 /**
69  * @param bit 0 -> write zero bit, !=0 write one bit
70  */
71 static void put_cabac_bypass(CABACContext *c, int bit){
72  c->low += c->low;
73 
74  if(bit){
75  c->low += c->range;
76  }
77 //FIXME optimize
78  if(c->low<0x200){
79  put_cabac_bit(c, 0);
80  }else if(c->low<0x400){
81  c->outstanding_count++;
82  c->low -= 0x200;
83  }else{
84  put_cabac_bit(c, 1);
85  c->low -= 0x400;
86  }
87 }
88 
89 /**
90  *
91  * @return the number of bytes written
92  */
94  c->range -= 2;
95 
96  if(!bit){
98  }else{
99  c->low += c->range;
100  c->range= 2;
101 
103 
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);
107 
108  flush_put_bits(&c->pb); //FIXME FIXME FIXME XXX wrong
109  }
110 
111  return (put_bits_count(&c->pb)+7)>>3;
112 }
113 
114 int main(void){
115  CABACContext c;
116  uint8_t b[9*SIZE];
117  uint8_t r[9*SIZE];
118  int i, ret = 0;
119  uint8_t state[10]= {0};
120  AVLFG prng;
121 
122  av_lfg_init(&prng, 1);
124 
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;
128  }
129 
130  for(i=0; i<SIZE; i++){
131  put_cabac_bypass(&c, r[i]&1);
132  }
133 
134  for(i=0; i<SIZE; i++){
135  put_cabac(&c, state, r[i]&1);
136  }
137 
138  i= put_cabac_terminate(&c, 1);
139  b[i++] = av_lfg_get(&prng);
140  b[i ] = av_lfg_get(&prng);
141 
143 
144  memset(state, 0, sizeof(state));
145 
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);
149  ret = 1;
150  }
151  }
152 
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);
156  ret = 1;
157  }
158  }
159  if(!get_cabac_terminate(&c)) {
160  av_log(NULL, AV_LOG_ERROR, "where's the Terminator?\n");
161  ret = 1;
162  }
163 
164  return ret;
165 }
put_cabac
static void put_cabac(CABACContext *c, uint8_t *const state, int bit)
Definition: cabac.c:53
r
const char * r
Definition: vf_curves.c:114
av_lfg_init
av_cold void av_lfg_init(AVLFG *c, unsigned int seed)
Definition: lfg.c:32
put_cabac_terminate
static int put_cabac_terminate(CABACContext *c, int bit)
Definition: cabac.c:93
put_bits
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:208
b
#define b
Definition: input.c:41
renorm_cabac_encoder
static void renorm_cabac_encoder(CABACContext *c)
Definition: cabac.c:35
ff_init_cabac_encoder
void ff_init_cabac_encoder(CABACContext *c, uint8_t *buf, int buf_size)
Definition: cabac.c:164
bit
#define bit(string, value)
Definition: cbs_mpeg2.c:58
SIZE
#define SIZE
Definition: cabac.c:23
put_cabac_bit
static void put_cabac_bit(CABACContext *c, int b)
Definition: cabac.c:28
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
state
static struct @313 state
av_lfg_get
static unsigned int av_lfg_get(AVLFG *c)
Get the next random unsigned 32-bit number using an ALFG.
Definition: lfg.h:53
lfg.h
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
main
int main(void)
Definition: cabac.c:114
NULL
#define NULL
Definition: coverity.c:32
ff_h264_mlps_state
static const uint8_t *const ff_h264_mlps_state
Definition: cabac_functions.h:54
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
AVLFG
Context structure for the Lagged Fibonacci PRNG.
Definition: lfg.h:33
put_cabac_bypass
static void put_cabac_bypass(CABACContext *c, int bit)
Definition: cabac.c:71
get_cabac_noinline
static int av_noinline av_unused get_cabac_noinline(CABACContext *c, uint8_t *const state)
Definition: cabac_functions.h:134
get_cabac_terminate
static int av_unused get_cabac_terminate(CABACContext *c)
Definition: cabac_functions.h:181
ff_h264_lps_range
static const uint8_t *const ff_h264_lps_range
Definition: cabac_functions.h:53
ff_init_cabac_decoder
int ff_init_cabac_decoder(CABACContext *c, const uint8_t *buf, int buf_size)
Definition: cabac.c:177
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
put_bits_count
static int put_bits_count(PutBitContext *s)
Definition: put_bits.h:85
get_cabac_bypass
static int av_unused get_cabac_bypass(CABACContext *c)
Definition: cabac_functions.h:143
uint8_t
uint8_t
Definition: audio_convert.c:194
avcodec.h
ret
ret
Definition: filter_design.txt:187
flush_put_bits
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:101
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
cabac.c
CABACContext
Definition: cabac.h:43