OpenCores
URL https://opencores.org/ocsvn/bluespec-h264/bluespec-h264/trunk

Subversion Repositories bluespec-h264

[/] [bluespec-h264/] [trunk/] [test/] [decoder/] [ldecod/] [src/] [biaridecod.c] - Blame information for rev 100

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 14 jamey.hick
 
2
/*!
3
 *************************************************************************************
4
 * \file biaridecod.c
5
 *
6
 * \brief
7
 *    binary arithmetic decoder routines
8
 * \date
9
 *    21. Oct 2000
10
 * \author
11
 *    Main contributors (see contributors.h for copyright, address and affiliation details)
12
 *    - Detlev Marpe                    <marpe@hhi.de>
13
 *    - Gabi Blaettermann               <blaetter@hhi.de>
14
 *************************************************************************************
15
 */
16
 
17
#include <stdlib.h>
18
 
19
#include "global.h"
20
#include "memalloc.h"
21
 
22
extern int symbolCount;
23
 
24
int binCount = 0;
25
 
26
#define Dbuffer         (dep->Dbuffer)
27
#define Dbits_to_go     (dep->Dbits_to_go)
28
#define Dcodestrm       (dep->Dcodestrm)
29
#define Dcodestrm_len   (dep->Dcodestrm_len)
30
 
31
#define B_BITS    10  // Number of bits to represent the whole coding interval
32
#define HALF      (1 << (B_BITS-1))
33
#define QUARTER   (1 << (B_BITS-2))
34
 
35
/* Range table for  LPS */
36
const byte rLPS_table_64x4[64][4]=
37
{
38
  { 128, 176, 208, 240},
39
  { 128, 167, 197, 227},
40
  { 128, 158, 187, 216},
41
  { 123, 150, 178, 205},
42
  { 116, 142, 169, 195},
43
  { 111, 135, 160, 185},
44
  { 105, 128, 152, 175},
45
  { 100, 122, 144, 166},
46
  {  95, 116, 137, 158},
47
  {  90, 110, 130, 150},
48
  {  85, 104, 123, 142},
49
  {  81,  99, 117, 135},
50
  {  77,  94, 111, 128},
51
  {  73,  89, 105, 122},
52
  {  69,  85, 100, 116},
53
  {  66,  80,  95, 110},
54
  {  62,  76,  90, 104},
55
  {  59,  72,  86,  99},
56
  {  56,  69,  81,  94},
57
  {  53,  65,  77,  89},
58
  {  51,  62,  73,  85},
59
  {  48,  59,  69,  80},
60
  {  46,  56,  66,  76},
61
  {  43,  53,  63,  72},
62
  {  41,  50,  59,  69},
63
  {  39,  48,  56,  65},
64
  {  37,  45,  54,  62},
65
  {  35,  43,  51,  59},
66
  {  33,  41,  48,  56},
67
  {  32,  39,  46,  53},
68
  {  30,  37,  43,  50},
69
  {  29,  35,  41,  48},
70
  {  27,  33,  39,  45},
71
  {  26,  31,  37,  43},
72
  {  24,  30,  35,  41},
73
  {  23,  28,  33,  39},
74
  {  22,  27,  32,  37},
75
  {  21,  26,  30,  35},
76
  {  20,  24,  29,  33},
77
  {  19,  23,  27,  31},
78
  {  18,  22,  26,  30},
79
  {  17,  21,  25,  28},
80
  {  16,  20,  23,  27},
81
  {  15,  19,  22,  25},
82
  {  14,  18,  21,  24},
83
  {  14,  17,  20,  23},
84
  {  13,  16,  19,  22},
85
  {  12,  15,  18,  21},
86
  {  12,  14,  17,  20},
87
  {  11,  14,  16,  19},
88
  {  11,  13,  15,  18},
89
  {  10,  12,  15,  17},
90
  {  10,  12,  14,  16},
91
  {   9,  11,  13,  15},
92
  {   9,  11,  12,  14},
93
  {   8,  10,  12,  14},
94
  {   8,   9,  11,  13},
95
  {   7,   9,  11,  12},
96
  {   7,   9,  10,  12},
97
  {   7,   8,  10,  11},
98
  {   6,   8,   9,  11},
99
  {   6,   7,   9,  10},
100
  {   6,   7,   8,   9},
101
  {   2,   2,   2,   2}
102
};
103
 
104
 
105
 
106
const unsigned short AC_next_state_MPS_64[64] =
107
{
108
  1,2,3,4,5,6,7,8,9,10,
109
  11,12,13,14,15,16,17,18,19,20,
110
  21,22,23,24,25,26,27,28,29,30,
111
  31,32,33,34,35,36,37,38,39,40,
112
  41,42,43,44,45,46,47,48,49,50,
113
  51,52,53,54,55,56,57,58,59,60,
114
  61,62,62,63
115
};
116
 
117
 
118
const unsigned short AC_next_state_LPS_64[64] =
119
{
120
  0, 0, 1, 2, 2, 4, 4, 5, 6, 7,
121
  8, 9, 9,11,11,12,13,13,15,15,
122
  16,16,18,18,19,19,21,21,22,22,
123
  23,24,24,25,26,26,27,27,28,29,
124
  29,30,30,30,31,32,32,33,33,33,
125
  34,34,35,35,35,36,36,36,37,37,
126
  37,38,38,63
127
};
128
 
129
/************************************************************************
130
 * M a c r o s
131
 ************************************************************************
132
 */
133
 
134
#define get_byte(){                                         \
135
                    Dbuffer = Dcodestrm[(*Dcodestrm_len)++];\
136
                    Dbits_to_go = 7;                        \
137
                  }
138
 
139
 
140
/************************************************************************
141
 ************************************************************************
142
                      init / exit decoder
143
 ************************************************************************
144
 ************************************************************************/
145
 
146
 
147
/*!
148
 ************************************************************************
149
 * \brief
150
 *    Allocates memory for the DecodingEnvironment struct
151
 * \return DecodingContextPtr
152
 *    allocates memory
153
 ************************************************************************
154
 */
155
DecodingEnvironmentPtr arideco_create_decoding_environment()
156
{
157
  DecodingEnvironmentPtr dep;
158
 
159
  if ((dep = calloc(1,sizeof(DecodingEnvironment))) == NULL)
160
    no_mem_exit("arideco_create_decoding_environment: dep");
161
  return dep;
162
}
163
 
164
 
165
/*!
166
 ***********************************************************************
167
 * \brief
168
 *    Frees memory of the DecodingEnvironment struct
169
 ***********************************************************************
170
 */
171
void arideco_delete_decoding_environment(DecodingEnvironmentPtr dep)
172
{
173
  if (dep == NULL)
174
  {
175
    snprintf(errortext, ET_SIZE, "Error freeing dep (NULL pointer)");
176
    error (errortext, 200);
177
  }
178
  else
179
    free(dep);
180
}
181
 
182
 
183
/*!
184
 ************************************************************************
185
 * \brief
186
 *    Initializes the DecodingEnvironment for the arithmetic coder
187
 ************************************************************************
188
 */
189
void arideco_start_decoding(DecodingEnvironmentPtr dep, unsigned char *cpixcode,
190
                            int firstbyte, int *cpixcode_len, int slice_type )
191
{
192
 
193
  int value = 0;
194
 
195
  Dcodestrm = cpixcode;
196
  Dcodestrm_len = cpixcode_len;
197
  *Dcodestrm_len = firstbyte;
198
 
199
  {
200
    int i;
201
    Dbits_to_go = 0;
202
    for (i = 0; i < B_BITS -1 ; i++) // insertion of redundant bit
203
    {
204
      if (--Dbits_to_go < 0)
205
        get_byte();
206
      value = (value<<1)  | ((Dbuffer >> Dbits_to_go) & 0x01);
207
    }
208
  }
209
  dep->Drange = HALF-2;
210
  dep->Dvalue = value;
211
}
212
 
213
 
214
/*!
215
 ************************************************************************
216
 * \brief
217
 *    arideco_bits_read
218
 ************************************************************************
219
 */
220
int arideco_bits_read(DecodingEnvironmentPtr dep)
221
{
222
  return 8 * ((*Dcodestrm_len)-1) + (8 - Dbits_to_go) - 16;
223
}
224
 
225
 
226
/*!
227
 ************************************************************************
228
 * \brief
229
 *    arideco_done_decoding():
230
 ************************************************************************
231
 */
232
void arideco_done_decoding(DecodingEnvironmentPtr dep)
233
{
234
  (*Dcodestrm_len)++;
235
}
236
 
237
 
238
 
239
/*!
240
 ************************************************************************
241
 * \brief
242
 *    biari_decode_symbol():
243
 * \return
244
 *    the decoded symbol
245
 ************************************************************************
246
 */
247
unsigned int biari_decode_symbol(DecodingEnvironmentPtr dep, BiContextTypePtr bi_ct )
248
{
249
  register unsigned int bit = bi_ct->MPS;
250
  register unsigned int value = dep->Dvalue;
251
  register unsigned int range = dep->Drange;
252
  register unsigned int rLPS = (unsigned int) rLPS_table_64x4[bi_ct->state][(range>>6) & 0x03];
253
 
254
#if (2==TRACE)
255
  fprintf(p_trace, "%d  0x%04x  %d  %d\n", binCount++, dep->Drange, bi_ct->state, bi_ct->MPS );
256
#endif
257
 
258
  range -= rLPS;
259
 
260
  if (value < range) /* MPS */
261
    bi_ct->state = AC_next_state_MPS_64[bi_ct->state]; // next state
262
  else              /* LPS */
263
  {
264
    value -= range;
265
    range = rLPS;
266
    bit = !bit;
267
    if (!bi_ct->state)       // switch meaning of MPS if necessary
268
      bi_ct->MPS ^= 0x01;
269
    bi_ct->state = AC_next_state_LPS_64[bi_ct->state]; // next state
270
  }
271
 
272
  while (range < QUARTER)
273
  {
274
    /* Double range */
275
    range <<= 1;
276
    if (--Dbits_to_go < 0)
277
      get_byte();
278
    /* Shift in next bit and add to value */
279
    value = (value << 1) | ((Dbuffer >> Dbits_to_go) & 0x01);
280
 
281
  }
282
 
283
  dep->Drange = range;
284
  dep->Dvalue = value;
285
 
286
  return(bit);
287
}
288
 
289
 
290
/*!
291
 ************************************************************************
292
 * \brief
293
 *    biari_decode_symbol_eq_prob():
294
 * \return
295
 *    the decoded symbol
296
 ************************************************************************
297
 */
298
unsigned int biari_decode_symbol_eq_prob(DecodingEnvironmentPtr dep)
299
{
300
  register unsigned int bit = 0;
301
  register unsigned int value  = (dep->Dvalue<<1);
302
 
303
#if TRACE
304
//  fprintf(p_trace, "%d  0x%04x\n", binCount++, dep->Drange );
305
#endif
306
 
307
  if (--Dbits_to_go < 0)
308
    get_byte();
309
  /* Shift in next bit and add to value */
310
  value |= (Dbuffer >> Dbits_to_go) &  0x01;
311
  if (value >= dep->Drange)
312
  {
313
    bit = 1;
314
    value -= dep->Drange;
315
  }
316
 
317
  dep->Dvalue = value;
318
 
319
  return(bit);
320
}
321
 
322
/*!
323
 ************************************************************************
324
 * \brief
325
 *    biari_decode_symbol_final():
326
 * \return
327
 *    the decoded symbol
328
 ************************************************************************
329
 */
330
unsigned int biari_decode_final(DecodingEnvironmentPtr dep)
331
{
332
  register unsigned int value  = dep->Dvalue;
333
  register unsigned int range  = dep->Drange - 2;
334
 
335
#if (2==TRACE)
336
  fprintf(p_trace, "%d  0x%04x\n", binCount++, dep->Drange );
337
#endif
338
 
339
  if (value >= range)
340
  {
341
    return 1;
342
  }
343
  else
344
  {
345
    while (range < QUARTER)
346
    {
347
    /* Double range */
348
      range <<= 1;
349
      if (--Dbits_to_go < 0)
350
        get_byte();
351
      /* Shift in next bit and add to value */
352
      value = (value << 1) | ((Dbuffer >> Dbits_to_go) & 0x01);
353
    }
354
    dep->Dvalue = value;
355
    dep->Drange = range;
356
    return 0;
357
  }
358
}
359
 
360
 
361
 
362
/*!
363
 ************************************************************************
364
 * \brief
365
 *    Initializes a given context with some pre-defined probability state
366
 ************************************************************************
367
 */
368
void biari_init_context (struct img_par* img, BiContextTypePtr ctx, const int* ini)
369
{
370
  int pstate;
371
 
372
  pstate = ((ini[0]* imax(0,img->qp) )>>4) + ini[1];
373
  pstate = iClip3(1, 126, pstate);
374
 
375
  if ( pstate >= 64 )
376
  {
377
    ctx->state  = (unsigned short) (pstate - 64);
378
    ctx->MPS    = 1;
379
  }
380
  else
381
  {
382
    ctx->state  = (unsigned short) (63 - pstate);
383
    ctx->MPS    = 0;
384
  }
385
}
386
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.