Coverage Report

Created: 2026-10-17 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/libde265/libde265/cabac.cc
Line
Count
Source
1
/*
2
 * H.265 video codec.
3
 * Copyright (c) 2013-2014 struktur AG, Dirk Farin <farin@struktur.de>
4
 *
5
 * This file is part of libde265.
6
 *
7
 * libde265 is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Lesser General Public License as
9
 * published by the Free Software Foundation, either version 3 of
10
 * the License, or (at your option) any later version.
11
 *
12
 * libde265 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
15
 * GNU Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public License
18
 * along with libde265.  If not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
#include "cabac.h"
22
#include "util.h"
23
24
#include <stdint.h>
25
#include <stdio.h>
26
#include <stdlib.h>
27
#include <assert.h>
28
29
0
#define INITIAL_CABAC_BUFFER_CAPACITY 4096
30
31
32
static const uint8_t LPS_table[64][4] =
33
  {
34
    { 128, 176, 208, 240},
35
    { 128, 167, 197, 227},
36
    { 128, 158, 187, 216},
37
    { 123, 150, 178, 205},
38
    { 116, 142, 169, 195},
39
    { 111, 135, 160, 185},
40
    { 105, 128, 152, 175},
41
    { 100, 122, 144, 166},
42
    {  95, 116, 137, 158},
43
    {  90, 110, 130, 150},
44
    {  85, 104, 123, 142},
45
    {  81,  99, 117, 135},
46
    {  77,  94, 111, 128},
47
    {  73,  89, 105, 122},
48
    {  69,  85, 100, 116},
49
    {  66,  80,  95, 110},
50
    {  62,  76,  90, 104},
51
    {  59,  72,  86,  99},
52
    {  56,  69,  81,  94},
53
    {  53,  65,  77,  89},
54
    {  51,  62,  73,  85},
55
    {  48,  59,  69,  80},
56
    {  46,  56,  66,  76},
57
    {  43,  53,  63,  72},
58
    {  41,  50,  59,  69},
59
    {  39,  48,  56,  65},
60
    {  37,  45,  54,  62},
61
    {  35,  43,  51,  59},
62
    {  33,  41,  48,  56},
63
    {  32,  39,  46,  53},
64
    {  30,  37,  43,  50},
65
    {  29,  35,  41,  48},
66
    {  27,  33,  39,  45},
67
    {  26,  31,  37,  43},
68
    {  24,  30,  35,  41},
69
    {  23,  28,  33,  39},
70
    {  22,  27,  32,  37},
71
    {  21,  26,  30,  35},
72
    {  20,  24,  29,  33},
73
    {  19,  23,  27,  31},
74
    {  18,  22,  26,  30},
75
    {  17,  21,  25,  28},
76
    {  16,  20,  23,  27},
77
    {  15,  19,  22,  25},
78
    {  14,  18,  21,  24},
79
    {  14,  17,  20,  23},
80
    {  13,  16,  19,  22},
81
    {  12,  15,  18,  21},
82
    {  12,  14,  17,  20},
83
    {  11,  14,  16,  19},
84
    {  11,  13,  15,  18},
85
    {  10,  12,  15,  17},
86
    {  10,  12,  14,  16},
87
    {   9,  11,  13,  15},
88
    {   9,  11,  12,  14},
89
    {   8,  10,  12,  14},
90
    {   8,   9,  11,  13},
91
    {   7,   9,  11,  12},
92
    {   7,   9,  10,  12},
93
    {   7,   8,  10,  11},
94
    {   6,   8,   9,  11},
95
    {   6,   7,   9,  10},
96
    {   6,   7,   8,   9},
97
    {   2,   2,   2,   2}
98
  };
99
100
static const uint8_t renorm_table[32] =
101
  {
102
    6,  5,  4,  4,
103
    3,  3,  3,  3,
104
    2,  2,  2,  2,
105
    2,  2,  2,  2,
106
    1,  1,  1,  1,
107
    1,  1,  1,  1,
108
    1,  1,  1,  1,
109
    1,  1,  1,  1
110
  };
111
112
static const uint8_t next_state_MPS[64] =
113
  {
114
    1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,
115
    17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,
116
    33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,
117
    49,50,51,52,53,54,55,56,57,58,59,60,61,62,62,63
118
  };
119
120
static const uint8_t next_state_LPS[64] =
121
  {
122
    0,0,1,2,2,4,4,5,6,7,8,9,9,11,11,12,
123
    13,13,15,15,16,16,18,18,19,19,21,21,22,22,23,24,
124
    24,25,26,26,27,27,28,29,29,30,30,30,31,32,32,33,
125
    33,33,34,34,35,35,35,36,36,36,37,37,37,38,38,63
126
  };
127
128
129
130
131
132
#ifdef DE265_LOG_TRACE
133
int logcnt=1;
134
#endif
135
136
void CABAC_decoder::init(uint8_t* bitstream, int length)
137
90.9k
{
138
90.9k
  assert(length >= 0);
139
140
90.9k
  bitstream_start = bitstream;
141
90.9k
  bitstream_curr  = bitstream;
142
90.9k
  bitstream_end   = bitstream+length;
143
90.9k
}
144
145
void CABAC_decoder::init_CABAC()
146
93.4k
{
147
93.4k
  int length = bitstream_end - bitstream_curr;
148
149
93.4k
  range = 510;
150
93.4k
  bits_needed = 8;
151
152
93.4k
  value = 0;
153
154
93.4k
  if (length>0) { value  = (*bitstream_curr++) << 8;  bits_needed-=8; }
155
93.4k
  if (length>1) { value |= (*bitstream_curr++);       bits_needed-=8; }
156
157
93.4k
  logtrace(LogCABAC,"[%3d] init_CABAC_decode_2 r:%x v:%x\n", logcnt, range, value);
158
93.4k
}
159
160
161
#if defined(__x86_64__) && defined(__GNUC__) && !defined(DE265_LOG_TRACE)
162
#define DE265_CABAC_ASM_X86_64 1
163
// Combined state-transition table (folds the MPS-flip at state 0), indexed by
164
// the packed context byte (state*2+MPSbit) for MPS and (packed ^ 0xFF) for LPS.
165
static const uint8_t cabac_transition[256] =
166
  {
167
      2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,
168
     18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
169
     34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
170
     50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65,
171
     66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81,
172
     82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97,
173
     98, 99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,
174
    114,115,116,117,118,119,120,121,122,123,124,125,124,125,126,127,
175
    127,126, 77, 76, 77, 76, 75, 74, 75, 74, 75, 74, 73, 72, 73, 72,
176
     73, 72, 71, 70, 71, 70, 71, 70, 69, 68, 69, 68, 67, 66, 67, 66,
177
     67, 66, 65, 64, 65, 64, 63, 62, 61, 60, 61, 60, 61, 60, 59, 58,
178
     59, 58, 57, 56, 55, 54, 55, 54, 53, 52, 53, 52, 51, 50, 49, 48,
179
     49, 48, 47, 46, 45, 44, 45, 44, 43, 42, 43, 42, 39, 38, 39, 38,
180
     37, 36, 37, 36, 33, 32, 33, 32, 31, 30, 31, 30, 27, 26, 27, 26,
181
     25, 24, 23, 22, 23, 22, 19, 18, 19, 18, 17, 16, 15, 14, 13, 12,
182
     11, 10,  9,  8,  9,  8,  5,  4,  5,  4,  3,  2,  1,  0,  0,  1,
183
  };
184
#endif
185
186
int  CABAC_decoder::decode_bit(context_model* model)
187
618M
{
188
618M
#ifdef DE265_CABAC_ASM_X86_64
189
  // x86-64 branchless arithmetic decoder. Bit-identical to the C fallback below.
190
618M
  uint32_t range_l = range, value_l = value;
191
618M
  int      bn_l    = bits_needed;
192
618M
  uint8_t* curr_l = bitstream_curr;
193
618M
  uint8_t* sp = reinterpret_cast<uint8_t*>(model);
194
618M
  int bit_l;
195
196
618M
  __asm__ (
197
618M
    "movzbl (%[sp]), %%eax            \n\t" // eax = b (state*2 + MPS)
198
618M
    "mov    %[range], %%ecx           \n\t"
199
618M
    "shr    $6, %%ecx                 \n\t"
200
618M
    "sub    $4, %%ecx                 \n\t" // ecx = (range>>6)-4
201
618M
    "mov    %%eax, %%edx              \n\t"
202
618M
    "shr    $1, %%edx                 \n\t" // edx = state
203
618M
    "lea    (%%rcx,%%rdx,4), %%rcx    \n\t" // rcx = state*4 + ridx
204
618M
    "lea    %[lps], %%rdx             \n\t" // rdx = &LPS_table[0][0]
205
618M
    "movzbl (%%rdx,%%rcx,1), %%edx    \n\t" // edx = RangeLPS
206
618M
    "sub    %%edx, %[range]           \n\t" // range = range - RangeLPS (MPS range)
207
618M
    "mov    %[range], %%r8d           \n\t"
208
618M
    "shl    $7, %%r8d                 \n\t" // r8d = scaled_range
209
618M
    "mov    %[value], %%r9d           \n\t"
210
618M
    "sub    %%r8d, %%r9d              \n\t"
211
618M
    "sar    $31, %%r9d                \n\t" // r9d = mps_mask (-1 if MPS)
212
618M
    "not    %%r9d                     \n\t" // r9d = lps_mask (-1 if LPS)
213
618M
    "mov    %%r8d, %%ecx              \n\t"
214
618M
    "and    %%r9d, %%ecx              \n\t"
215
618M
    "sub    %%ecx, %[value]           \n\t" // value -= scaled & lps_mask
216
618M
    "mov    %%edx, %%ecx              \n\t" // RangeLPS
217
618M
    "sub    %[range], %%ecx           \n\t"
218
618M
    "and    %%r9d, %%ecx              \n\t"
219
618M
    "add    %%ecx, %[range]           \n\t" // range = RangeLPS if LPS, else unchanged
220
618M
    "and    $0xFF, %%r9d              \n\t"
221
618M
    "xor    %%r9d, %%eax              \n\t" // eax = idx
222
618M
    "mov    %%eax, %[bit]             \n\t"
223
618M
    "and    $1, %[bit]                \n\t" // bit = idx & 1
224
618M
    "lea    %[trans], %%rcx           \n\t" // rcx = &cabac_transition[0]
225
618M
    "movzbl (%%rcx,%%rax,1), %%edx    \n\t"
226
618M
    "mov    %%dl, (%[sp])             \n\t" // *sp = cabac_transition[idx]
227
618M
    "bsr    %[range], %%ecx           \n\t" // ecx = MSB index
228
618M
    "mov    $8, %%edx                 \n\t"
229
618M
    "sub    %%ecx, %%edx              \n\t" // edx = renorm shift = 8 - bsr
230
618M
    "mov    %%edx, %%ecx              \n\t"
231
618M
    "shl    %%cl, %[value]            \n\t"
232
618M
    "shl    %%cl, %[range]            \n\t"
233
618M
    "add    %%edx, %[bn]              \n\t" // bits_needed += shift
234
618M
    "test   %[bn], %[bn]              \n\t"
235
618M
    "js     1f                        \n\t" // bits_needed < 0: no refill
236
618M
    "cmp    %[end], %[curr]           \n\t"
237
618M
    "jae    2f                        \n\t" // curr >= end: no read
238
618M
    "movzbl (%[curr]), %%eax          \n\t"
239
618M
    "mov    %[bn], %%ecx              \n\t"
240
618M
    "shl    %%cl, %%eax              \n\t"
241
618M
    "or     %%eax, %[value]           \n\t" // value |= (*curr) << bits_needed
242
618M
    "inc    %[curr]                   \n\t"
243
618M
    "2:                               \n\t"
244
618M
    "sub    $8, %[bn]                 \n\t"
245
618M
    "1:                               \n\t"
246
618M
    : [range]"+r"(range_l), [value]"+r"(value_l), [bn]"+r"(bn_l),
247
618M
      [curr]"+r"(curr_l), [bit]"=&r"(bit_l)
248
618M
    : [sp]"r"(sp), [end]"r"(bitstream_end),
249
618M
      [lps]"m"(LPS_table[0][0]), [trans]"m"(cabac_transition[0])
250
618M
    : "rax","rcx","rdx","r8","r9","cc","memory"
251
618M
  );
252
253
618M
  range = range_l; value = value_l; bits_needed = (int16_t)bn_l; bitstream_curr = curr_l;
254
618M
  return bit_l;
255
#else
256
  logtrace(LogCABAC,"[%3d] decodeBin r:%x v:%x state:%d\n",logcnt,range, value, model->state);
257
258
  int decoded_bit;
259
  int LPS = LPS_table[model->state][ ( range >> 6 ) - 4 ];
260
  range -= LPS;
261
262
  uint32_t scaled_range = range << 7;
263
264
  logtrace(LogCABAC,"[%3d] sr:%x v:%x\n",logcnt,scaled_range, value);
265
266
  if (value < scaled_range)
267
    {
268
      logtrace(LogCABAC,"[%3d] MPS\n",logcnt);
269
270
      // MPS path
271
272
      decoded_bit = model->MPSbit;
273
      model->state = next_state_MPS[model->state];
274
275
      if (scaled_range < ( 256 << 7 ) )
276
        {
277
          // scaled range, highest bit (15) not set
278
279
          range = scaled_range >> 6; // shift range by one bit
280
          value <<= 1;               // shift value by one bit
281
          bits_needed++;
282
283
          if (bits_needed == 0)
284
            {
285
              bits_needed = -8;
286
              if (bitstream_curr < bitstream_end)
287
                { value |= *bitstream_curr++; }
288
            }
289
        }
290
    }
291
  else
292
    {
293
      logtrace(LogCABAC,"[%3d] LPS\n",logcnt);
294
      //printf("%d %d\n", model->state, 0);
295
296
      // LPS path
297
298
      value = (value - scaled_range);
299
300
      uint8_t num_bits = renorm_table[ LPS >> 3 ];
301
      value <<= num_bits;
302
      range   = LPS << num_bits;  /* this is always >= 0x100 except for state 63,
303
                                              but state 63 is never used */
304
305
#ifndef NDEBUG
306
      int num_bitsTab = renorm_table[ LPS >> 3 ];
307
      assert(num_bits == num_bitsTab);
308
#endif
309
310
      decoded_bit      = 1 - model->MPSbit;
311
312
      if (model->state==0) { model->MPSbit = 1-model->MPSbit; }
313
      model->state = next_state_LPS[model->state];
314
315
      bits_needed += num_bits;
316
317
      if (bits_needed >= 0)
318
        {
319
          logtrace(LogCABAC,"bits_needed: %d\n", bits_needed);
320
          if (bitstream_curr < bitstream_end)
321
            { value |= (*bitstream_curr++) << bits_needed; }
322
323
          bits_needed -= 8;
324
        }
325
    }
326
327
  logtrace(LogCABAC,"[%3d] -> bit %d  r:%x v:%x\n", logcnt, decoded_bit, range, value);
328
#ifdef DE265_LOG_TRACE
329
  logcnt++;
330
#endif
331
332
  return decoded_bit;
333
#endif
334
618M
}
335
336
int  CABAC_decoder::decode_term_bit()
337
2.88M
{
338
2.88M
  logtrace(LogCABAC,"CABAC term: range=%x\n", range);
339
340
2.88M
  range -= 2;
341
2.88M
  uint32_t scaledRange = range << 7;
342
343
2.88M
  if (value >= scaledRange)
344
5.42k
    {
345
5.42k
      return 1;
346
5.42k
    }
347
2.87M
  else
348
2.87M
    {
349
      // there is a while loop in the standard, but it will always be executed only once
350
351
2.87M
      if (scaledRange < (256<<7))
352
50.5k
        {
353
50.5k
          range = scaledRange >> 6;
354
50.5k
          value *= 2;
355
356
50.5k
          bits_needed++;
357
50.5k
          if (bits_needed==0)
358
7.32k
            {
359
7.32k
              bits_needed = -8;
360
361
7.32k
              if (bitstream_curr < bitstream_end) {
362
3.31k
                value += (*bitstream_curr++);
363
3.31k
              }
364
7.32k
            }
365
50.5k
        }
366
367
2.87M
      return 0;
368
2.87M
    }
369
2.88M
}
370
371
372
// When we read past the end of the bitstream (which should only happen on faulty bitstreams),
373
// we will eventually only return zeros.
374
/* (9.3.4.3.6) Alignment process prior to aligned bypass decoding. Invoked before the
375
   bypass-coded coeff_sign_flag / coeff_abs_level_remaining of a sub-block carrying escape
376
   data when cabac_bypass_alignment_enabled_flag is set. Bypass decoding leaves
377
   ivlCurrRange untouched, so a single call covers the whole run of bypass bins after it.
378
*/
379
void CABAC_decoder::align_bypass()
380
212k
{
381
212k
  range = 256;
382
212k
}
383
384
385
int  CABAC_decoder::decode_bypass()
386
238M
{
387
238M
#ifdef DE265_CABAC_ASM_X86_64
388
238M
  uint32_t value_l = value;
389
238M
  int      bn_l    = bits_needed;
390
238M
  uint8_t* curr_l  = bitstream_curr;
391
238M
  int bit_l;
392
393
238M
  __asm__ (
394
238M
    "add    %[value], %[value]        \n\t" // value <<= 1
395
238M
    "addl   $1, %[bn]                 \n\t" // bits_needed++
396
238M
    "js     1f                        \n\t" // bits_needed < 0: no refill
397
238M
    "cmp    %[end], %[curr]           \n\t"
398
238M
    "jae    2f                        \n\t" // curr >= end: no read
399
238M
    "movzbl (%[curr]), %%eax          \n\t"
400
238M
    "or     %%eax, %[value]           \n\t" // value |= *curr (bits_needed==0)
401
238M
    "inc    %[curr]                   \n\t"
402
238M
    "2:                               \n\t"
403
238M
    "movl   $-8, %[bn]                \n\t" // bits_needed = -8
404
238M
    "1:                               \n\t"
405
238M
    "mov    %[range], %%ecx           \n\t"
406
238M
    "shl    $7, %%ecx                 \n\t" // ecx = scaled_range
407
238M
    "mov    %[value], %%edx           \n\t"
408
238M
    "sub    %%ecx, %%edx              \n\t" // edx = value - scaled_range
409
238M
    "mov    %%edx, %%eax              \n\t"
410
238M
    "sar    $31, %%eax               \n\t"  // eax = mask (-1 if value<scaled -> bit 0)
411
238M
    "mov    %%eax, %[bit]             \n\t"
412
238M
    "add    $1, %[bit]                \n\t" // bit = mask + 1
413
238M
    "not    %%eax                     \n\t" // eax = ~mask
414
238M
    "and    %%eax, %%ecx              \n\t" // ecx = scaled & ~mask
415
238M
    "sub    %%ecx, %[value]           \n\t" // value -= scaled when bit==1
416
238M
    : [value]"+r"(value_l), [bn]"+r"(bn_l), [curr]"+r"(curr_l), [bit]"=&r"(bit_l)
417
238M
    : [range]"r"(range), [end]"r"(bitstream_end)
418
238M
    : "rax","rcx","rdx","cc","memory"
419
238M
  );
420
421
238M
  value = value_l; bits_needed = (int16_t)bn_l; bitstream_curr = curr_l;
422
238M
  return bit_l;
423
#else
424
  logtrace(LogCABAC,"[%3d] bypass r:%x v:%x\n",logcnt,range, value);
425
426
  value <<= 1;
427
  bits_needed++;
428
429
  if (bits_needed >= 0)
430
    {
431
      if (bitstream_end > bitstream_curr) {
432
        bits_needed = -8;
433
        value |= *bitstream_curr++;
434
      }
435
      else {
436
        // we read past the end of the bitstream, fill with 0
437
        bits_needed = -8;
438
      }
439
    }
440
441
  int bit;
442
  uint32_t scaled_range = range << 7;
443
  if (value >= scaled_range)
444
    {
445
      value -= scaled_range;
446
      bit=1;
447
    }
448
  else
449
    {
450
      bit=0;
451
    }
452
453
  logtrace(LogCABAC,"[%3d] -> bit %d  r:%x v:%x\n", logcnt, bit, range, value);
454
#ifdef DE265_LOG_TRACE
455
  logcnt++;
456
#endif
457
458
  return bit;
459
#endif
460
238M
}
461
462
463
int  CABAC_decoder::decode_TU_bypass(int cMax)
464
8.94M
{
465
9.79M
  for (int i=0;i<cMax;i++)
466
9.63M
    {
467
9.63M
      int bit = decode_bypass();
468
9.63M
      if (bit==0)
469
8.78M
        return i;
470
9.63M
    }
471
472
156k
  return cMax;
473
8.94M
}
474
475
int  CABAC_decoder::decode_TU(int cMax, context_model* model)
476
0
{
477
0
  for (int i=0;i<cMax;i++)
478
0
    {
479
0
      int bit = decode_bit(model);
480
0
      if (bit==0)
481
0
        return i;
482
0
    }
483
484
0
  return cMax;
485
0
}
486
487
488
int  CABAC_decoder::decode_FL_bypass_parallel(int nBits)
489
10.5M
{
490
10.5M
  logtrace(LogCABAC,"[%3d] bypass group r:%x v:%x (nBits=%d)\n",logcnt,
491
10.5M
           range, value, nBits);
492
493
10.5M
  value <<= nBits;
494
10.5M
  bits_needed+=nBits;
495
496
10.5M
  if (bits_needed >= 0)
497
5.00M
    {
498
5.00M
      if (bitstream_end > bitstream_curr) {
499
754k
        int input = *bitstream_curr++;
500
754k
        input <<= bits_needed;
501
502
754k
        bits_needed -= 8;
503
754k
        value |= input;
504
754k
      }
505
5.00M
    }
506
507
10.5M
  uint32_t scaled_range = range << 7;
508
10.5M
  int v = value / scaled_range;
509
10.5M
  if (unlikely(v>=(1<<nBits))) { v=(1<<nBits)-1; } // may happen with broken bitstreams
510
10.5M
  value -= v * scaled_range;
511
512
10.5M
  logtrace(LogCABAC,"[%3d] -> value %d  r:%x v:%x\n", logcnt+nBits-1,
513
10.5M
           v, range, value);
514
515
#ifdef DE265_LOG_TRACE
516
  logcnt+=nBits;
517
#endif
518
519
10.5M
  return v;
520
10.5M
}
521
522
523
uint32_t  CABAC_decoder::decode_FL_bypass(int nBits)
524
61.7M
{
525
61.7M
  uint32_t v=0;
526
527
61.7M
  if (likely(nBits<=8)) {
528
61.7M
    if (nBits==0) {
529
51.2M
      return 0;
530
51.2M
    }
531
    // we could use decode_bypass() for a single bit, but this seems to be slower
532
#if 0
533
    else if (nBits==1) {
534
      v = decode_bypass();
535
    }
536
#endif
537
10.5M
    else {
538
10.5M
      v = decode_FL_bypass_parallel(nBits);
539
10.5M
    }
540
61.7M
  }
541
7.11k
  else {
542
7.11k
    v = decode_FL_bypass_parallel(8);
543
7.11k
    nBits-=8;
544
545
37.7k
    while (nBits--) {
546
30.6k
      v <<= 1;
547
30.6k
      v |= decode_bypass();
548
30.6k
    }
549
7.11k
  }
550
10.5M
  logtrace(LogCABAC,"      -> FL: %d\n", v);
551
552
10.5M
  return v;
553
61.7M
}
554
555
int  CABAC_decoder::decode_TR_bypass(int cRiceParam, int cTRMax)
556
0
{
557
0
  int prefix = decode_TU_bypass(cTRMax>>cRiceParam);
558
0
  if (prefix==4) { // TODO check: constant 4 only works for coefficient decoding
559
0
    return cTRMax;
560
0
  }
561
562
0
  int suffix = decode_FL_bypass(cRiceParam);
563
564
0
  return (prefix << cRiceParam) | suffix;
565
0
}
566
567
568
uint32_t  CABAC_decoder::decode_EGk_bypass(int k)
569
1.09M
{
570
1.09M
  uint32_t base=0;
571
1.09M
  int n=k;
572
573
1.09M
  for (;;)
574
1.50M
    {
575
1.50M
      int bit = decode_bypass();
576
1.50M
      if (bit==0)
577
1.09M
        break;
578
409k
      else {
579
409k
        if (n >= 31) {
580
444
          return 0; // TODO: error
581
444
        }
582
409k
        base += 1u<<n;
583
409k
        n++;
584
409k
      }
585
1.50M
    }
586
587
1.09M
  uint32_t suffix = decode_FL_bypass(n);
588
1.09M
  return base + suffix;
589
1.09M
}
590
591
592
// ---------------------------------------------------------------------------
593
594
void CABAC_encoder::add_trailing_bits()
595
0
{
596
0
  write_bit(1);
597
0
  int nZeros = number_free_bits_in_byte();
598
0
  write_bits(0, nZeros);
599
0
}
600
601
602
603
CABAC_encoder_bitstream::CABAC_encoder_bitstream()
604
0
{
605
0
  init_CABAC();
606
0
}
607
608
CABAC_encoder_bitstream::~CABAC_encoder_bitstream()
609
0
{
610
0
  free(data_mem);
611
0
}
612
613
void CABAC_encoder_bitstream::reset()
614
0
{
615
0
  data_size = 0;
616
0
  state = 0;
617
618
0
  vlc_buffer_len = 0;
619
620
0
  init_CABAC();
621
0
}
622
623
void CABAC_encoder_bitstream::write_bits(uint32_t bits,int n)
624
0
{
625
0
  vlc_buffer <<= n;
626
0
  vlc_buffer |= bits;
627
0
  vlc_buffer_len += n;
628
629
  // TODO: errors returned by append_byte() are ignored, resulting in a broken output.
630
631
0
  while (vlc_buffer_len>=8) {
632
0
    append_byte((vlc_buffer >> (vlc_buffer_len-8)) & 0xFF);
633
0
    vlc_buffer_len -= 8;
634
0
  }
635
0
}
636
637
void CABAC_encoder::write_uvlc(int value)
638
0
{
639
0
  assert(value>=0);
640
641
0
  int nLeadingZeros=0;
642
0
  int base=0;
643
0
  int range=1;
644
645
0
  while (value>=base+range) {
646
0
    base += range;
647
0
    range <<= 1;
648
0
    nLeadingZeros++;
649
0
  }
650
651
0
  write_bits((1<<nLeadingZeros) | (value-base),2*nLeadingZeros+1);
652
0
}
653
654
void CABAC_encoder::write_svlc(int value)
655
0
{
656
0
  if      (value==0) write_bits(1,1);
657
0
  else if (value>0)  write_uvlc(2*value-1);
658
0
  else               write_uvlc(-2*value);
659
0
}
660
661
void CABAC_encoder_bitstream::flush_VLC()
662
0
{
663
  // TODO: errors returned by append_byte() are ignored, resulting in a broken output.
664
665
0
  while (vlc_buffer_len>=8) {
666
0
    append_byte((vlc_buffer >> (vlc_buffer_len-8)) & 0xFF);
667
0
    vlc_buffer_len -= 8;
668
0
  }
669
670
0
  if (vlc_buffer_len>0) {
671
0
    append_byte(vlc_buffer << (8-vlc_buffer_len));
672
0
    vlc_buffer_len = 0;
673
0
  }
674
675
0
  vlc_buffer = 0;
676
0
}
677
678
void CABAC_encoder_bitstream::skip_bits(int nBits)
679
0
{
680
0
  while (nBits>=8) {
681
0
    write_bits(0,8);
682
0
    nBits-=8;
683
0
  }
684
685
0
  if (nBits>0) {
686
0
    write_bits(0,nBits);
687
0
  }
688
0
}
689
690
691
int  CABAC_encoder_bitstream::number_free_bits_in_byte() const
692
0
{
693
0
  if ((vlc_buffer_len % 8)==0) return 0;
694
0
  return 8- (vlc_buffer_len % 8);
695
0
}
696
697
698
bool CABAC_encoder_bitstream::check_size_and_resize(int nBytes)
699
0
{
700
0
  if (data_size+nBytes > data_capacity) { // 1 extra byte for stuffing
701
0
    if (data_capacity==0) {
702
0
      data_capacity = INITIAL_CABAC_BUFFER_CAPACITY;
703
0
    } else {
704
0
      data_capacity *= 2;
705
0
    }
706
707
0
    uint8_t* new_data_mem = (uint8_t*)realloc(data_mem,data_capacity);
708
0
    if (new_data_mem) {
709
0
      data_mem = new_data_mem;
710
0
    }
711
0
    else {
712
0
      return false;
713
0
    }
714
0
  }
715
716
0
  return true;
717
0
}
718
719
720
bool CABAC_encoder_bitstream::append_byte(int byte)
721
0
{
722
0
  if (!check_size_and_resize(2)) {
723
0
    return false;
724
0
  }
725
726
  // --- emulation prevention ---
727
728
  /* These byte sequences may never occur in the bitstream:
729
     0x000000 / 0x000001 / 0x000002
730
731
     Hence, we have to add a 0x03 before the third byte.
732
     We also have to add a 0x03 for this sequence: 0x000003, because
733
     the escape byte itself also has to be escaped.
734
  */
735
736
  // S0 --(0)--> S1 --(0)--> S2 --(0,1,2,3)--> add stuffing
737
738
0
  if (byte<=3) {
739
0
    /**/ if (state< 2 && byte==0) { state++; }
740
0
    else if (state==2 && byte<=3) {
741
0
      data_mem[ data_size++ ] = 3;
742
743
0
      if (byte==0) state=1;
744
0
      else         state=0;
745
0
    }
746
0
    else { state=0; }
747
0
  }
748
0
  else { state=0; }
749
750
751
  // write actual data byte
752
753
0
  data_mem[ data_size++ ] = byte;
754
755
0
  return true;
756
0
}
757
758
759
bool CABAC_encoder_bitstream::write_startcode()
760
0
{
761
0
  if (!check_size_and_resize(3)) {
762
0
    return false;
763
0
  }
764
765
0
  data_mem[ data_size+0 ] = 0;
766
0
  data_mem[ data_size+1 ] = 0;
767
0
  data_mem[ data_size+2 ] = 1;
768
0
  data_size+=3;
769
770
0
  return true;
771
0
}
772
773
void CABAC_encoder_bitstream::init_CABAC()
774
0
{
775
0
  range = 510;
776
0
  low = 0;
777
778
0
  bits_left = 23;
779
0
  buffered_byte = 0xFF;
780
0
  num_buffered_bytes = 0;
781
0
}
782
783
void CABAC_encoder_bitstream::flush_CABAC()
784
0
{
785
  // TODO: errors returned by append_byte() are ignored, resulting in a broken output.
786
787
0
  if (low >> (32 - bits_left))
788
0
    {
789
0
      append_byte(buffered_byte + 1);
790
0
      while (num_buffered_bytes > 1)
791
0
        {
792
0
          append_byte(0x00);
793
0
          num_buffered_bytes--;
794
0
        }
795
796
0
      low -= 1 << (32 - bits_left);
797
0
    }
798
0
  else
799
0
    {
800
0
      if (num_buffered_bytes > 0)
801
0
        {
802
0
          append_byte(buffered_byte);
803
0
        }
804
805
0
      while (num_buffered_bytes > 1)
806
0
        {
807
0
          append_byte(0xff);
808
0
          num_buffered_bytes--;
809
0
        }
810
0
    }
811
812
  // printf("low: %08x  nbits left:%d  filled:%d\n",low,bits_left,32-bits_left);
813
814
0
  write_bits(low >> 8, 24-bits_left);
815
0
}
816
817
818
void CABAC_encoder_bitstream::write_out()
819
0
{
820
  // TODO: errors returned by append_byte() are ignored, resulting in a broken output.
821
  
822
  //logtrace(LogCABAC,"low = %08x (bits_left=%d)\n",low,bits_left);
823
0
  int leadByte = low >> (24 - bits_left);
824
0
  bits_left += 8;
825
0
  low &= 0xffffffffu >> bits_left;
826
827
  //logtrace(LogCABAC,"write byte %02x\n",leadByte);
828
  //logtrace(LogCABAC,"-> low = %08x\n",low);
829
830
0
  if (leadByte == 0xff)
831
0
    {
832
0
      num_buffered_bytes++;
833
0
    }
834
0
  else
835
0
    {
836
0
      if (num_buffered_bytes > 0)
837
0
        {
838
0
          int carry = leadByte >> 8;
839
0
          int byte = buffered_byte + carry;
840
0
          buffered_byte = leadByte & 0xff;
841
0
          append_byte(byte);
842
843
0
          byte = ( 0xff + carry ) & 0xff;
844
0
          while ( num_buffered_bytes > 1 )
845
0
            {
846
0
              append_byte(byte);
847
0
              num_buffered_bytes--;
848
0
            }
849
0
        }
850
0
      else
851
0
        {
852
0
          num_buffered_bytes = 1;
853
0
          buffered_byte = leadByte;
854
0
        }
855
0
    }
856
0
}
857
858
void CABAC_encoder_bitstream::testAndWriteOut()
859
0
{
860
  // logtrace(LogCABAC,"bits_left = %d\n",bits_left);
861
862
0
  if (bits_left < 12)
863
0
    {
864
0
      write_out();
865
0
    }
866
0
}
867
868
869
#ifdef DE265_LOG_TRACE
870
int encBinCnt=1;
871
#endif
872
873
void CABAC_encoder_bitstream::write_CABAC_bit(int modelIdx, int bin)
874
0
{
875
0
  context_model* model = &(*mCtxModels)[modelIdx];
876
  //m_uiBinsCoded += m_binCountIncrement;
877
  //rcCtxModel.setBinsCoded( 1 );
878
879
0
  logtrace(LogCABAC,"[%d] range=%x low=%x state=%d, bin=%d\n",
880
0
           encBinCnt, range,low, model->state,bin);
881
882
  /*
883
  printf("[%d] range=%x low=%x state=%d, bin=%d\n",
884
         encBinCnt, range,low, model->state,bin);
885
886
  printf("%d %d X\n",model->state,bin != model->MPSbit);
887
  */
888
889
#ifdef DE265_LOG_TRACE
890
  encBinCnt++;
891
#endif
892
893
0
  uint32_t LPS = LPS_table[model->state][ ( range >> 6 ) - 4 ];
894
0
  range -= LPS;
895
896
0
  if (bin != model->MPSbit)
897
0
    {
898
      //logtrace(LogCABAC,"LPS\n");
899
900
0
      int num_bits = renorm_table[ LPS >> 3 ];
901
0
      low = (low + range) << num_bits;
902
0
      range   = LPS << num_bits;
903
904
0
      if (model->state==0) { model->MPSbit = 1-model->MPSbit; }
905
906
0
      model->state = next_state_LPS[model->state];
907
908
0
      bits_left -= num_bits;
909
0
    }
910
0
  else
911
0
    {
912
      //logtrace(LogCABAC,"MPS\n");
913
914
0
      model->state = next_state_MPS[model->state];
915
916
917
      // renorm
918
919
0
      if (range >= 256) { return; }
920
921
0
      low <<= 1;
922
0
      range <<= 1;
923
0
      bits_left--;
924
0
    }
925
926
0
  testAndWriteOut();
927
0
}
928
929
void CABAC_encoder_bitstream::write_CABAC_bypass(int bin)
930
0
{
931
0
  logtrace(LogCABAC,"[%d] bypass = %d, range=%x\n",encBinCnt,bin,range);
932
  /*
933
  printf("[%d] bypass = %d, range=%x\n",encBinCnt,bin,range);
934
  printf("%d %d X\n",64, -1);
935
  */
936
937
#ifdef DE265_LOG_TRACE
938
  encBinCnt++;
939
#endif
940
941
  // BinsCoded += m_binCountIncrement;
942
0
  low <<= 1;
943
944
0
  if (bin)
945
0
    {
946
0
      low += range;
947
0
    }
948
0
  bits_left--;
949
950
0
  testAndWriteOut();
951
0
}
952
953
void CABAC_encoder::write_CABAC_TU_bypass(int value, int cMax)
954
0
{
955
0
  for (int i=0;i<value;i++) {
956
0
    write_CABAC_bypass(1);
957
0
  }
958
959
0
  if (value<cMax) {
960
0
    write_CABAC_bypass(0);
961
0
  }
962
0
}
963
964
void CABAC_encoder::write_CABAC_FL_bypass(int value, int n)
965
0
{
966
0
  while (n>0) {
967
0
    n--;
968
0
    write_CABAC_bypass(value & (1<<n));
969
0
  }
970
0
}
971
972
void CABAC_encoder_bitstream::write_CABAC_term_bit(int bit)
973
0
{
974
0
  logtrace(LogCABAC,"CABAC term: range=%x\n", range);
975
976
0
  range -= 2;
977
978
0
  if (bit) {
979
0
    low += range;
980
981
0
    low <<= 7;
982
0
    range = 2 << 7;
983
0
    bits_left -= 7;
984
0
  }
985
0
  else if (range >= 256)
986
0
    {
987
0
      return;
988
0
    }
989
0
  else
990
0
    {
991
0
      low   <<= 1;
992
0
      range <<= 1;
993
0
      bits_left--;
994
0
    }
995
996
0
  testAndWriteOut();
997
0
}
998
999
1000
1001
1002
static const uint32_t entropy_table[128] = {
1003
  // -------------------- 200 --------------------
1004
  /* state= 0 */  0x07d13 /* 0.977164 */,  0x08255 /* 1.018237 */,
1005
  /* state= 1 */  0x07738 /* 0.931417 */,  0x086ef /* 1.054179 */,
1006
  /* state= 2 */  0x0702b /* 0.876323 */,  0x0935a /* 1.151195 */,
1007
  /* state= 3 */  0x069e6 /* 0.827333 */,  0x09c7f /* 1.222650 */,
1008
  /* state= 4 */  0x062e8 /* 0.772716 */,  0x0a2c7 /* 1.271708 */,
1009
  /* state= 5 */  0x05c18 /* 0.719488 */,  0x0ae25 /* 1.360532 */,
1010
  /* state= 6 */  0x05632 /* 0.673414 */,  0x0b724 /* 1.430793 */,
1011
  /* state= 7 */  0x05144 /* 0.634904 */,  0x0c05d /* 1.502850 */,
1012
  /* state= 8 */  0x04bdf /* 0.592754 */,  0x0ccf2 /* 1.601145 */,
1013
  /* state= 9 */  0x0478d /* 0.559012 */,  0x0d57b /* 1.667843 */,
1014
  /* state=10 */  0x042ad /* 0.520924 */,  0x0de81 /* 1.738336 */,
1015
  /* state=11 */  0x03f4d /* 0.494564 */,  0x0e4b8 /* 1.786871 */,
1016
  /* state=12 */  0x03a9d /* 0.457945 */,  0x0f471 /* 1.909721 */,
1017
  /* state=13 */  0x037d5 /* 0.436201 */,  0x0fc56 /* 1.971385 */,
1018
  /* state=14 */  0x034c2 /* 0.412177 */,  0x10236 /* 2.017284 */,
1019
  /* state=15 */  0x031a6 /* 0.387895 */,  0x10d5c /* 2.104394 */,
1020
  /* state=16 */  0x02e62 /* 0.362383 */,  0x11b34 /* 2.212552 */,
1021
  /* state=17 */  0x02c20 /* 0.344752 */,  0x120b4 /* 2.255512 */,
1022
  /* state=18 */  0x029b8 /* 0.325943 */,  0x1294d /* 2.322672 */,
1023
  /* state=19 */  0x02791 /* 0.309143 */,  0x135e1 /* 2.420959 */,
1024
  /* state=20 */  0x02562 /* 0.292057 */,  0x13e37 /* 2.486077 */,
1025
  /* state=21 */  0x0230d /* 0.273846 */,  0x144fd /* 2.539000 */,
1026
  /* state=22 */  0x02193 /* 0.262308 */,  0x150c9 /* 2.631150 */,
1027
  /* state=23 */  0x01f5d /* 0.245026 */,  0x15ca0 /* 2.723641 */,
1028
  /* state=24 */  0x01de7 /* 0.233617 */,  0x162f9 /* 2.773246 */,
1029
  /* state=25 */  0x01c2f /* 0.220208 */,  0x16d99 /* 2.856259 */,
1030
  /* state=26 */  0x01a8e /* 0.207459 */,  0x17a93 /* 2.957634 */,
1031
  /* state=27 */  0x0195a /* 0.198065 */,  0x18051 /* 3.002477 */,
1032
  /* state=28 */  0x01809 /* 0.187778 */,  0x18764 /* 3.057759 */,
1033
  /* state=29 */  0x0164a /* 0.174144 */,  0x19460 /* 3.159206 */,
1034
  /* state=30 */  0x01539 /* 0.165824 */,  0x19f20 /* 3.243181 */,
1035
  /* state=31 */  0x01452 /* 0.158756 */,  0x1a465 /* 3.284334 */,
1036
  /* state=32 */  0x0133b /* 0.150261 */,  0x1b422 /* 3.407303 */,
1037
  /* state=33 */  0x0120c /* 0.140995 */,  0x1bce5 /* 3.475767 */,
1038
  /* state=34 */  0x01110 /* 0.133315 */,  0x1c394 /* 3.527962 */,
1039
  /* state=35 */  0x0104d /* 0.127371 */,  0x1d059 /* 3.627736 */,
1040
  /* state=36 */  0x00f8b /* 0.121451 */,  0x1d74b /* 3.681983 */,
1041
  /* state=37 */  0x00ef4 /* 0.116829 */,  0x1dfd0 /* 3.748540 */,
1042
  /* state=38 */  0x00e10 /* 0.109864 */,  0x1e6d3 /* 3.803335 */,
1043
  /* state=39 */  0x00d3f /* 0.103507 */,  0x1f925 /* 3.946462 */,
1044
  /* state=40 */  0x00cc4 /* 0.099758 */,  0x1fda7 /* 3.981667 */,
1045
  /* state=41 */  0x00c42 /* 0.095792 */,  0x203f8 /* 4.031012 */,
1046
  /* state=42 */  0x00b78 /* 0.089610 */,  0x20f7d /* 4.121014 */,
1047
  /* state=43 */  0x00afc /* 0.085830 */,  0x21dd6 /* 4.233102 */,
1048
  /* state=44 */  0x00a5e /* 0.081009 */,  0x22419 /* 4.282016 */,
1049
  /* state=45 */  0x00a1b /* 0.078950 */,  0x22a5e /* 4.331015 */,
1050
  /* state=46 */  0x00989 /* 0.074514 */,  0x23756 /* 4.432323 */,
1051
  /* state=47 */  0x0091b /* 0.071166 */,  0x24225 /* 4.516775 */,
1052
  /* state=48 */  0x008cf /* 0.068837 */,  0x2471a /* 4.555487 */,
1053
  /* state=49 */  0x00859 /* 0.065234 */,  0x25313 /* 4.649048 */,
1054
  /* state=50 */  0x00814 /* 0.063140 */,  0x25d67 /* 4.729721 */,
1055
  /* state=51 */  0x007b6 /* 0.060272 */,  0x2651f /* 4.790028 */,
1056
  /* state=52 */  0x0076e /* 0.058057 */,  0x2687c /* 4.816294 */,
1057
  /* state=53 */  0x00707 /* 0.054924 */,  0x27da7 /* 4.981661 */,
1058
  /* state=54 */  0x006d5 /* 0.053378 */,  0x28172 /* 5.011294 */,
1059
  /* state=55 */  0x00659 /* 0.049617 */,  0x28948 /* 5.072512 */,
1060
  /* state=56 */  0x00617 /* 0.047598 */,  0x297c5 /* 5.185722 */,
1061
  /* state=57 */  0x005dd /* 0.045814 */,  0x2a2df /* 5.272434 */,
1062
  /* state=58 */  0x005c1 /* 0.044965 */,  0x2a581 /* 5.293019 */,
1063
  /* state=59 */  0x00574 /* 0.042619 */,  0x2ad59 /* 5.354304 */,
1064
  /* state=60 */  0x0053b /* 0.040882 */,  0x2bba5 /* 5.465973 */,
1065
  /* state=61 */  0x0050c /* 0.039448 */,  0x2c596 /* 5.543651 */,
1066
  /* state=62 */  0x004e9 /* 0.038377 */,  0x2cd88 /* 5.605741 */,
1067
  0x00400 ,  0x2d000 /* dummy, should never be used */
1068
};
1069
1070
1071
#if 0
1072
static const uint32_t entropy_table_orig[128] = {
1073
  0x07b23, 0x085f9, 0x074a0, 0x08cbc, 0x06ee4, 0x09354, 0x067f4, 0x09c1b,
1074
  0x060b0, 0x0a62a, 0x05a9c, 0x0af5b, 0x0548d, 0x0b955, 0x04f56, 0x0c2a9,
1075
  0x04a87, 0x0cbf7, 0x045d6, 0x0d5c3, 0x04144, 0x0e01b, 0x03d88, 0x0e937,
1076
  0x039e0, 0x0f2cd, 0x03663, 0x0fc9e, 0x03347, 0x10600, 0x03050, 0x10f95,
1077
  0x02d4d, 0x11a02, 0x02ad3, 0x12333, 0x0286e, 0x12cad, 0x02604, 0x136df,
1078
  0x02425, 0x13f48, 0x021f4, 0x149c4, 0x0203e, 0x1527b, 0x01e4d, 0x15d00,
1079
  0x01c99, 0x166de, 0x01b18, 0x17017, 0x019a5, 0x17988, 0x01841, 0x18327,
1080
  0x016df, 0x18d50, 0x015d9, 0x19547, 0x0147c, 0x1a083, 0x0138e, 0x1a8a3,
1081
  0x01251, 0x1b418, 0x01166, 0x1bd27, 0x01068, 0x1c77b, 0x00f7f, 0x1d18e,
1082
  0x00eda, 0x1d91a, 0x00e19, 0x1e254, 0x00d4f, 0x1ec9a, 0x00c90, 0x1f6e0,
1083
  0x00c01, 0x1fef8, 0x00b5f, 0x208b1, 0x00ab6, 0x21362, 0x00a15, 0x21e46,
1084
  0x00988, 0x2285d, 0x00934, 0x22ea8, 0x008a8, 0x239b2, 0x0081d, 0x24577,
1085
  0x007c9, 0x24ce6, 0x00763, 0x25663, 0x00710, 0x25e8f, 0x006a0, 0x26a26,
1086
  0x00672, 0x26f23, 0x005e8, 0x27ef8, 0x005ba, 0x284b5, 0x0055e, 0x29057,
1087
  0x0050c, 0x29bab, 0x004c1, 0x2a674, 0x004a7, 0x2aa5e, 0x0046f, 0x2b32f,
1088
  0x0041f, 0x2c0ad, 0x003e7, 0x2ca8d, 0x003ba, 0x2d323, 0x0010c, 0x3bfbb
1089
};
1090
1091
1092
const uint32_t entropy_table_theory[128] =
1093
  {
1094
    0x08000, 0x08000, 0x076da, 0x089a0, 0x06e92, 0x09340, 0x0670a, 0x09cdf, 0x06029, 0x0a67f, 0x059dd, 0x0b01f, 0x05413, 0x0b9bf, 0x04ebf, 0x0c35f,
1095
    0x049d3, 0x0ccff, 0x04546, 0x0d69e, 0x0410d, 0x0e03e, 0x03d22, 0x0e9de, 0x0397d, 0x0f37e, 0x03619, 0x0fd1e, 0x032ee, 0x106be, 0x02ffa, 0x1105d,
1096
    0x02d37, 0x119fd, 0x02aa2, 0x1239d, 0x02836, 0x12d3d, 0x025f2, 0x136dd, 0x023d1, 0x1407c, 0x021d2, 0x14a1c, 0x01ff2, 0x153bc, 0x01e2f, 0x15d5c,
1097
    0x01c87, 0x166fc, 0x01af7, 0x1709b, 0x0197f, 0x17a3b, 0x0181d, 0x183db, 0x016d0, 0x18d7b, 0x01595, 0x1971b, 0x0146c, 0x1a0bb, 0x01354, 0x1aa5a,
1098
    0x0124c, 0x1b3fa, 0x01153, 0x1bd9a, 0x01067, 0x1c73a, 0x00f89, 0x1d0da, 0x00eb7, 0x1da79, 0x00df0, 0x1e419, 0x00d34, 0x1edb9, 0x00c82, 0x1f759,
1099
    0x00bda, 0x200f9, 0x00b3c, 0x20a99, 0x00aa5, 0x21438, 0x00a17, 0x21dd8, 0x00990, 0x22778, 0x00911, 0x23118, 0x00898, 0x23ab8, 0x00826, 0x24458,
1100
    0x007ba, 0x24df7, 0x00753, 0x25797, 0x006f2, 0x26137, 0x00696, 0x26ad7, 0x0063f, 0x27477, 0x005ed, 0x27e17, 0x0059f, 0x287b6, 0x00554, 0x29156,
1101
    0x0050e, 0x29af6, 0x004cc, 0x2a497, 0x0048d, 0x2ae35, 0x00451, 0x2b7d6, 0x00418, 0x2c176, 0x003e2, 0x2cb15, 0x003af, 0x2d4b5, 0x0037f, 0x2de55
1102
};
1103
#endif
1104
1105
1106
void CABAC_encoder_estim::write_CABAC_bit(int modelIdx, int bit)
1107
0
{
1108
0
  context_model* model = &(*mCtxModels)[modelIdx];
1109
  //printf("[%d] state=%d, bin=%d\n", encBinCnt, model->state,bit);
1110
  //encBinCnt++;
1111
1112
0
  int idx = model->state<<1;
1113
1114
0
  if (bit==model->MPSbit) {
1115
0
    model->state = next_state_MPS[model->state];
1116
0
  }
1117
0
  else {
1118
0
    idx++;
1119
0
    if (model->state==0) { model->MPSbit = 1-model->MPSbit; }
1120
0
    model->state = next_state_LPS[model->state];
1121
0
  }
1122
1123
0
  mFracBits += entropy_table[idx];
1124
1125
  //printf("-> %08lx %f\n",entropy_table[idx], entropy_table[idx] / float(1<<15));
1126
0
}
1127
1128
1129
float CABAC_encoder::RDBits_for_CABAC_bin(int modelIdx, int bit)
1130
0
{
1131
0
  context_model* model = &(*mCtxModels)[modelIdx];
1132
0
  int idx = model->state<<1;
1133
1134
0
  if (bit!=model->MPSbit) {
1135
0
    idx++;
1136
0
  }
1137
1138
0
  return entropy_table[idx] / float(1<<15);
1139
0
}
1140
1141
1142
void CABAC_encoder::write_CABAC_EGk(int val, int k)
1143
0
{
1144
0
  while (val  >=  ( 1 << k ) ) {
1145
0
    write_CABAC_bypass(1);
1146
0
    val = val - ( 1 << k );
1147
0
    k++;
1148
0
  }
1149
1150
0
  write_CABAC_bypass(0);
1151
1152
0
  while (k)  {
1153
0
    k--;
1154
0
    write_CABAC_bypass((val >> k) & 1);
1155
0
  }
1156
0
}
1157
1158
1159
1160
void CABAC_encoder_estim_constant::write_CABAC_bit(int modelIdx, int bit)
1161
0
{
1162
0
  context_model* model = &(*mCtxModels)[modelIdx];
1163
0
  int idx = model->state<<1;
1164
1165
0
  if (bit!=model->MPSbit) {
1166
0
    idx++;
1167
0
  }
1168
1169
0
  mFracBits += entropy_table[idx];
1170
0
}
1171
1172
1173
1174
#if 0
1175
void printtab(int idx,int s)
1176
{
1177
  printf("%d %f %f %f\n", s,
1178
         double(entropy_table[idx])/0x8000,
1179
         double(entropy_table_orig[idx])/0x8000,
1180
         double(entropy_table_f265[idx])/0x8000);
1181
}
1182
1183
1184
void plot_tables()
1185
{
1186
  for (int i=-62;i<=0;i++) {
1187
    int idx = -i *2;
1188
    int s = i;
1189
    printtab(idx,s);
1190
  }
1191
1192
  for (int i=0;i<=62;i++) {
1193
    int idx = 2*i +1;
1194
    int s = i;
1195
    printtab(idx,s);
1196
  }
1197
}
1198
#endif