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 #include "libavcodec/put_bits.h"
28 
29 typedef struct CABACTestContext {
34 
35 static inline void put_cabac_bit(CABACTestContext *c, int b)
36 {
37  put_bits(&c->pb, 1, b);
38  for(;c->outstanding_count; c->outstanding_count--){
39  put_bits(&c->pb, 1, 1-b);
40  }
41 }
42 
44 {
45  while (c->dec.range < 0x100) {
46  //FIXME optimize
47  if (c->dec.low < 0x100) {
48  put_cabac_bit(c, 0);
49  } else if (c->dec.low < 0x200) {
50  c->outstanding_count++;
51  c->dec.low -= 0x100;
52  }else{
53  put_cabac_bit(c, 1);
54  c->dec.low -= 0x200;
55  }
56 
57  c->dec.range += c->dec.range;
58  c->dec.low += c->dec.low;
59  }
60 }
61 
62 static void put_cabac(CABACTestContext *c, uint8_t * const state, int bit)
63 {
64  int RangeLPS = ff_h264_lps_range[2 * (c->dec.range & 0xC0) + *state];
65 
66  if(bit == ((*state)&1)){
67  c->dec.range -= RangeLPS;
68  *state = ff_h264_mlps_state[128 + *state];
69  }else{
70  c->dec.low += c->dec.range - RangeLPS;
71  c->dec.range = RangeLPS;
72  *state= ff_h264_mlps_state[127 - *state];
73  }
74 
76 }
77 
78 /**
79  * @param bit 0 -> write zero bit, !=0 write one bit
80  */
82 {
83  c->dec.low += c->dec.low;
84 
85  if(bit){
86  c->dec.low += c->dec.range;
87  }
88 //FIXME optimize
89  if (c->dec.low < 0x200) {
90  put_cabac_bit(c, 0);
91  } else if (c->dec.low < 0x400) {
92  c->outstanding_count++;
93  c->dec.low -= 0x200;
94  }else{
95  put_cabac_bit(c, 1);
96  c->dec.low -= 0x400;
97  }
98 }
99 
100 /**
101  *
102  * @return the number of bytes written
103  */
105 {
106  c->dec.range -= 2;
107 
108  if(!bit){
110  }else{
111  c->dec.low += c->dec.range;
112  c->dec.range = 2;
113 
115 
116  av_assert0(c->dec.low <= 0x1FF);
117  put_cabac_bit(c, c->dec.low >> 9);
118  put_bits(&c->pb, 2, ((c->dec.low >> 7) & 3) | 1);
119 
120  flush_put_bits(&c->pb); //FIXME FIXME FIXME XXX wrong
121  }
122 
123  return put_bytes_count(&c->pb, 1);
124 }
125 
126 /**
127  * @param buf_size size of buf in bits
128  */
129 static void init_cabac_encoder(CABACTestContext *c, uint8_t *buf, int buf_size)
130 {
131  init_put_bits(&c->pb, buf, buf_size);
132 
133  c->dec.low = 0;
134  c->dec.range = 0x1FE;
135  c->outstanding_count = 0;
136  c->pb.bit_left++; //avoids firstBitFlag
137 }
138 
139 int main(void){
141  uint8_t b[9*SIZE];
142  uint8_t r[9*SIZE];
143  int i, ret = 0;
144  uint8_t state[10]= {0};
145  AVLFG prng;
146 
147  av_lfg_init(&prng, 1);
149 
150  for(i=0; i<SIZE; i++){
151  if(2*i<SIZE) r[i] = av_lfg_get(&prng) % 7;
152  else r[i] = (i>>8)&1;
153  }
154 
155  for(i=0; i<SIZE; i++){
156  put_cabac_bypass(&c, r[i]&1);
157  }
158 
159  for(i=0; i<SIZE; i++){
160  put_cabac(&c, state, r[i]&1);
161  }
162 
163  i= put_cabac_terminate(&c, 1);
164  b[i++] = av_lfg_get(&prng);
165  b[i ] = av_lfg_get(&prng);
166 
167  ff_init_cabac_decoder(&c.dec, b, SIZE);
168 
169  memset(state, 0, sizeof(state));
170 
171  for(i=0; i<SIZE; i++){
172  if ((r[i] & 1) != get_cabac_bypass(&c.dec)) {
173  av_log(NULL, AV_LOG_ERROR, "CABAC bypass failure at %d\n", i);
174  ret = 1;
175  }
176  }
177 
178  for(i=0; i<SIZE; i++){
179  if ((r[i] & 1) != get_cabac_noinline(&c.dec, state)) {
180  av_log(NULL, AV_LOG_ERROR, "CABAC failure at %d\n", i);
181  ret = 1;
182  }
183  }
184  if (!get_cabac_terminate(&c.dec)) {
185  av_log(NULL, AV_LOG_ERROR, "where's the Terminator?\n");
186  ret = 1;
187  }
188 
189  return ret;
190 }
r
const char * r
Definition: vf_curves.c:116
av_lfg_init
av_cold void av_lfg_init(AVLFG *c, unsigned int seed)
Definition: lfg.c:32
CABACTestContext::pb
PutBitContext pb
Definition: cabac.c:32
init_put_bits
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:61
state
static struct @321 state
put_bits
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:220
b
#define b
Definition: input.c:41
put_bytes_count
static int put_bytes_count(const PutBitContext *s, int round_up)
Definition: put_bits.h:99
bit
#define bit(string, value)
Definition: cbs_mpeg2.c:58
put_cabac
static void put_cabac(CABACTestContext *c, uint8_t *const state, int bit)
Definition: cabac.c:62
renorm_cabac_encoder
static void renorm_cabac_encoder(CABACTestContext *c)
Definition: cabac.c:43
SIZE
#define SIZE
Definition: cabac.c:23
CABACTestContext::dec
CABACContext dec
Definition: cabac.c:30
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:181
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
PutBitContext
Definition: put_bits.h:49
main
int main(void)
Definition: cabac.c:139
NULL
#define NULL
Definition: coverity.c:32
put_cabac_terminate
static int put_cabac_terminate(CABACTestContext *c, int bit)
Definition: cabac.c:104
ff_h264_mlps_state
static const uint8_t *const ff_h264_mlps_state
Definition: cabac_functions.h:55
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
get_cabac_noinline
static int av_noinline av_unused get_cabac_noinline(CABACContext *c, uint8_t *const state)
Definition: cabac_functions.h:135
put_cabac_bypass
static void put_cabac_bypass(CABACTestContext *c, int bit)
Definition: cabac.c:81
get_cabac_terminate
static int av_unused get_cabac_terminate(CABACContext *c)
Definition: cabac_functions.h:182
ff_h264_lps_range
static const uint8_t *const ff_h264_lps_range
Definition: cabac_functions.h:54
ff_init_cabac_decoder
int ff_init_cabac_decoder(CABACContext *c, const uint8_t *buf, int buf_size)
Definition: cabac.c:165
i
int i
Definition: input.c:407
init_cabac_encoder
static void init_cabac_encoder(CABACTestContext *c, uint8_t *buf, int buf_size)
Definition: cabac.c:129
avcodec.h
get_cabac_bypass
#define get_cabac_bypass
Definition: cabac.h:115
ret
ret
Definition: filter_design.txt:187
CABACTestContext
Definition: cabac.c:29
flush_put_bits
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:142
CABACTestContext::outstanding_count
int outstanding_count
Definition: cabac.c:31
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
put_bits.h
cabac.c
CABACContext
Definition: cabac.h:41
put_cabac_bit
static void put_cabac_bit(CABACTestContext *c, int b)
Definition: cabac.c:35