1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
|
/*!
*************************************************************************************
* \file biaridecod.c
*
* \brief
* Binary arithmetic decoder routines.
*
* This modified implementation of the M Coder is based on JVT-U084
* with the choice of M_BITS = 16.
*
* \date
* 21. Oct 2000
* \author
* Main contributors (see contributors.h for copyright, address and affiliation details)
* - Detlev Marpe <marpe@hhi.de>
* - Gabi Blaettermann
* - Gunnar Marten
*************************************************************************************
*/
#include "global.h"
#include "memalloc.h"
#include "biaridecod.h"
#define B_BITS 10 // Number of bits to represent the whole coding interval
#define HALF 0x01FE //(1 << (B_BITS-1)) - 2
#define QUARTER 0x0100 //(1 << (B_BITS-2))
/************************************************************************
************************************************************************
init / exit decoder
************************************************************************
************************************************************************/
/*!
************************************************************************
* \brief
* Allocates memory for the DecodingEnvironment struct
* \return DecodingContextPtr
* allocates memory
************************************************************************
*/
DecodingEnvironmentPtr arideco_create_decoding_environment()
{
DecodingEnvironmentPtr dep;
if ((dep = calloc(1,sizeof(DecodingEnvironment))) == NULL)
no_mem_exit("arideco_create_decoding_environment: dep");
return dep;
}
/*!
***********************************************************************
* \brief
* Frees memory of the DecodingEnvironment struct
***********************************************************************
*/
void arideco_delete_decoding_environment(DecodingEnvironmentPtr dep)
{
if (dep == NULL)
{
snprintf(errortext, ET_SIZE, "Error freeing dep (NULL pointer)");
error (errortext, 200);
}
else
free(dep);
}
/*!
************************************************************************
* \brief
* finalize arithetic decoding():
************************************************************************
*/
void arideco_done_decoding(DecodingEnvironmentPtr dep)
{
(*dep->Dcodestrm_len)++;
#if(TRACE==2)
fprintf(p_trace, "done_decoding: %d\n", *dep->Dcodestrm_len);
#endif
}
/*!
************************************************************************
* \brief
* read one byte from the bitstream
************************************************************************
*/
unsigned int getbyte(DecodingEnvironmentPtr dep)
{
#if(TRACE==2)
fprintf(p_trace, "get_byte: %d\n", (*dep->Dcodestrm_len));
#endif
return dep->Dcodestrm[(*dep->Dcodestrm_len)++];
}
/*!
************************************************************************
* \brief
* read two bytes from the bitstream
************************************************************************
*/
static unsigned int getword(DecodingEnvironmentPtr dep)
{
int d = *dep->Dcodestrm_len;
*dep->Dcodestrm_len += 2;
return ((dep->Dcodestrm[d]<<8) | dep->Dcodestrm[d+1]);
}
/*!
************************************************************************
* \brief
* Initializes the DecodingEnvironment for the arithmetic coder
************************************************************************
*/
void arideco_start_decoding(DecodingEnvironmentPtr dep, unsigned char *code_buffer,
int firstbyte, int *code_len)
{
dep->Dcodestrm = code_buffer;
dep->Dcodestrm_len = code_len;
*dep->Dcodestrm_len = firstbyte;
dep->Dvalue = getbyte(dep);
dep->Dvalue = (dep->Dvalue << 16) | getword(dep); // lookahead of 2 bytes: always make sure that bitstream buffer
// contains 2 more bytes than actual bitstream
dep->DbitsLeft = 15;
dep->Drange = HALF;
#if (2==TRACE)
fprintf(p_trace, "value: %d firstbyte: %d code_len: %d\n", dep->Dvalue >> dep->DbitsLeft, firstbyte, *code_len);
#endif
}
/*!
************************************************************************
* \brief
* biari_decode_symbol():
* \return
* the decoded symbol
************************************************************************
*/
/* random notes
max rLPS = 240 1111 1 111
max state = 63
max renorm = 6, min 1
max bitsleft = 16
max range = (1<<10) ????? (1024)
*/
#if !defined(_M_IX86) || defined(_DEBUG)
unsigned int biari_decode_symbol(DecodingEnvironmentPtr dep, BiContextTypePtr bi_ct )
{
unsigned int state = bi_ct->state;
unsigned int bit = bi_ct->MPS;
unsigned int value = dep->Dvalue;
unsigned int range = dep->Drange;
const unsigned int rLPS = rLPS_table_64x4[(range>>6)&3][state];
range -= rLPS;
if(value >= (range << dep->DbitsLeft))
{ // LPS
int renorm;
bi_ct->state = AC_next_state_LPS_64[state]; // next state
value -= (range << dep->DbitsLeft);
bit ^= 0x01;
//if (!state) // switch meaning of MPS if necessary
// bi_ct->MPS = bit;
bi_ct->MPS ^= !state;//0x01;
renorm = renorm_table_256[rLPS];
range = (rLPS << renorm);
dep->Drange = range;
dep->DbitsLeft -= renorm;
if( dep->DbitsLeft > 0 )
{
dep->Dvalue = value;
return(bit);
}
dep->Dvalue = (value << 16) | getword(dep); // lookahead of 2 bytes: always make sure that bitstream buffer
// contains 2 more bytes than actual bitstream
dep->DbitsLeft += 16;
return(bit);
}
else
{ //MPS
bi_ct->state = AC_next_state_MPS_64[state]; // next state
if( range < QUARTER )
{
dep->Drange = range << 1;
dep->DbitsLeft -= 1;
if( dep->DbitsLeft > 0 )
{
return(bit);
}
dep->Dvalue = (value << 16) | getword(dep); // lookahead of 2 bytes: always make sure that bitstream buffer
// contains 2 more bytes than actual bitstream
dep->DbitsLeft += 16;
return(bit);
}
else
{
dep->Drange = range;
return (bit);
}
}
}
#endif
/*!
************************************************************************
* \brief
* biari_decode_symbol_eq_prob():
* \return
* the decoded symbol
************************************************************************
*/
unsigned int biari_decode_symbol_eq_prob(DecodingEnvironmentPtr dep)
{
int tmp_value;
int value = dep->Dvalue;
if(--(dep->DbitsLeft) == 0)
{
value = (value << 16) | getword( dep ); // lookahead of 2 bytes: always make sure that bitstream buffer
// contains 2 more bytes than actual bitstream
dep->DbitsLeft = 16;
}
tmp_value = value - (dep->Drange << dep->DbitsLeft);
if (tmp_value < 0)
{
dep->Dvalue = value;
return 0;
}
else
{
dep->Dvalue = tmp_value;
return 1;
}
}
/*!
************************************************************************
* \brief
* biari_decode_symbol_final():
* \return
* the decoded symbol
************************************************************************
*/
unsigned int biari_decode_final(DecodingEnvironmentPtr dep)
{
unsigned int range = dep->Drange - 2;
int value = dep->Dvalue;
value -= (range << dep->DbitsLeft);
if (value < 0)
{
if( range >= QUARTER )
{
dep->Drange = range;
return 0;
}
else
{
dep->Drange = (range << 1);
if( --(dep->DbitsLeft) > 0 )
return 0;
else
{
dep->Dvalue = (dep->Dvalue << 16) | getword( dep ); // lookahead of 2 bytes: always make sure that bitstream buffer
// contains 2 more bytes than actual bitstream
dep->DbitsLeft = 16;
return 0;
}
}
}
else
{
return 1;
}
}
/*!
************************************************************************
* \brief
* Initializes a given context with some pre-defined probability state
************************************************************************
*/
void biari_init_context (int qp, BiContextTypePtr ctx, const char* ini)
{
int pstate = ((ini[0]* qp )>>4) + ini[1];
if ( pstate >= 64 )
{
pstate = imin(126, pstate);
ctx->state = (uint16) (pstate - 64);
ctx->MPS = 1;
}
else
{
pstate = imax(1, pstate);
ctx->state = (uint16) (63 - pstate);
ctx->MPS = 0;
}
}
|