/src/libde265/libde265/cabac.cc
Line | Count | Source (jump to first uncovered line) |
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 init_CABAC_decoder(CABAC_decoder* decoder, uint8_t* bitstream, int length) |
137 | 24.4k | { |
138 | 24.4k | assert(length >= 0); |
139 | | |
140 | 0 | decoder->bitstream_start = bitstream; |
141 | 24.4k | decoder->bitstream_curr = bitstream; |
142 | 24.4k | decoder->bitstream_end = bitstream+length; |
143 | 24.4k | } |
144 | | |
145 | | void init_CABAC_decoder_2(CABAC_decoder* decoder) |
146 | 24.9k | { |
147 | 24.9k | int length = decoder->bitstream_end - decoder->bitstream_curr; |
148 | | |
149 | 24.9k | decoder->range = 510; |
150 | 24.9k | decoder->bits_needed = 8; |
151 | | |
152 | 24.9k | decoder->value = 0; |
153 | | |
154 | 24.9k | if (length>0) { decoder->value = (*decoder->bitstream_curr++) << 8; decoder->bits_needed-=8; } |
155 | 24.9k | if (length>1) { decoder->value |= (*decoder->bitstream_curr++); decoder->bits_needed-=8; } |
156 | | |
157 | 24.9k | logtrace(LogCABAC,"[%3d] init_CABAC_decode_2 r:%x v:%x\n", logcnt, decoder->range, decoder->value); |
158 | 24.9k | } |
159 | | |
160 | | |
161 | | int decode_CABAC_bit(CABAC_decoder* decoder, context_model* model) |
162 | 897M | { |
163 | 897M | logtrace(LogCABAC,"[%3d] decodeBin r:%x v:%x state:%d\n",logcnt,decoder->range, decoder->value, model->state); |
164 | | |
165 | 897M | int decoded_bit; |
166 | 897M | int LPS = LPS_table[model->state][ ( decoder->range >> 6 ) - 4 ]; |
167 | 897M | decoder->range -= LPS; |
168 | | |
169 | 897M | uint32_t scaled_range = decoder->range << 7; |
170 | | |
171 | 897M | logtrace(LogCABAC,"[%3d] sr:%x v:%x\n",logcnt,scaled_range, decoder->value); |
172 | | |
173 | 897M | if (decoder->value < scaled_range) |
174 | 895M | { |
175 | 895M | logtrace(LogCABAC,"[%3d] MPS\n",logcnt); |
176 | | |
177 | | // MPS path |
178 | | |
179 | 895M | decoded_bit = model->MPSbit; |
180 | 895M | model->state = next_state_MPS[model->state]; |
181 | | |
182 | 895M | if (scaled_range < ( 256 << 7 ) ) |
183 | 38.3M | { |
184 | | // scaled range, highest bit (15) not set |
185 | | |
186 | 38.3M | decoder->range = scaled_range >> 6; // shift range by one bit |
187 | 38.3M | decoder->value <<= 1; // shift value by one bit |
188 | 38.3M | decoder->bits_needed++; |
189 | | |
190 | 38.3M | if (decoder->bits_needed == 0) |
191 | 303k | { |
192 | 303k | decoder->bits_needed = -8; |
193 | 303k | if (decoder->bitstream_curr < decoder->bitstream_end) |
194 | 240k | { decoder->value |= *decoder->bitstream_curr++; } |
195 | 303k | } |
196 | 38.3M | } |
197 | 895M | } |
198 | 1.48M | else |
199 | 1.48M | { |
200 | 1.48M | logtrace(LogCABAC,"[%3d] LPS\n",logcnt); |
201 | | //printf("%d %d\n", model->state, 0); |
202 | | |
203 | | // LPS path |
204 | | |
205 | 1.48M | decoder->value = (decoder->value - scaled_range); |
206 | | |
207 | 1.48M | int num_bits = renorm_table[ LPS >> 3 ]; |
208 | 1.48M | decoder->value <<= num_bits; |
209 | 1.48M | decoder->range = LPS << num_bits; /* this is always >= 0x100 except for state 63, |
210 | | but state 63 is never used */ |
211 | | |
212 | 1.48M | int num_bitsTab = renorm_table[ LPS >> 3 ]; |
213 | | |
214 | 1.48M | assert(num_bits == num_bitsTab); |
215 | | |
216 | 0 | decoded_bit = 1 - model->MPSbit; |
217 | | |
218 | 1.48M | if (model->state==0) { model->MPSbit = 1-model->MPSbit; } |
219 | 1.48M | model->state = next_state_LPS[model->state]; |
220 | | |
221 | 1.48M | decoder->bits_needed += num_bits; |
222 | | |
223 | 1.48M | if (decoder->bits_needed >= 0) |
224 | 975k | { |
225 | 975k | logtrace(LogCABAC,"bits_needed: %d\n", decoder->bits_needed); |
226 | 975k | if (decoder->bitstream_curr < decoder->bitstream_end) |
227 | 345k | { decoder->value |= (*decoder->bitstream_curr++) << decoder->bits_needed; } |
228 | | |
229 | 975k | decoder->bits_needed -= 8; |
230 | 975k | } |
231 | 1.48M | } |
232 | | |
233 | 897M | logtrace(LogCABAC,"[%3d] -> bit %d r:%x v:%x\n", logcnt, decoded_bit, decoder->range, decoder->value); |
234 | | #ifdef DE265_LOG_TRACE |
235 | | logcnt++; |
236 | | #endif |
237 | | |
238 | 897M | return decoded_bit; |
239 | 897M | } |
240 | | |
241 | | int decode_CABAC_term_bit(CABAC_decoder* decoder) |
242 | 1.06M | { |
243 | 1.06M | logtrace(LogCABAC,"CABAC term: range=%x\n", decoder->range); |
244 | | |
245 | 1.06M | decoder->range -= 2; |
246 | 1.06M | uint32_t scaledRange = decoder->range << 7; |
247 | | |
248 | 1.06M | if (decoder->value >= scaledRange) |
249 | 896 | { |
250 | 896 | return 1; |
251 | 896 | } |
252 | 1.06M | else |
253 | 1.06M | { |
254 | | // there is a while loop in the standard, but it will always be executed only once |
255 | | |
256 | 1.06M | if (scaledRange < (256<<7)) |
257 | 14.4k | { |
258 | 14.4k | decoder->range = scaledRange >> 6; |
259 | 14.4k | decoder->value *= 2; |
260 | | |
261 | 14.4k | decoder->bits_needed++; |
262 | 14.4k | if (decoder->bits_needed==0) |
263 | 771 | { |
264 | 771 | decoder->bits_needed = -8; |
265 | | |
266 | 771 | if (decoder->bitstream_curr < decoder->bitstream_end) { |
267 | 703 | decoder->value += (*decoder->bitstream_curr++); |
268 | 703 | } |
269 | 771 | } |
270 | 14.4k | } |
271 | | |
272 | 1.06M | return 0; |
273 | 1.06M | } |
274 | 1.06M | } |
275 | | |
276 | | |
277 | | |
278 | | int decode_CABAC_bypass(CABAC_decoder* decoder) |
279 | 544M | { |
280 | 544M | logtrace(LogCABAC,"[%3d] bypass r:%x v:%x\n",logcnt,decoder->range, decoder->value); |
281 | | |
282 | 544M | decoder->value <<= 1; |
283 | 544M | decoder->bits_needed++; |
284 | | |
285 | 544M | if (decoder->bits_needed >= 0) |
286 | 428M | { |
287 | 428M | if (decoder->bitstream_end > decoder->bitstream_curr) { |
288 | 371k | decoder->bits_needed = -8; |
289 | 371k | decoder->value |= *decoder->bitstream_curr++; |
290 | 371k | } |
291 | 428M | } |
292 | | |
293 | 544M | int bit; |
294 | 544M | uint32_t scaled_range = decoder->range << 7; |
295 | 544M | if (decoder->value >= scaled_range) |
296 | 3.17M | { |
297 | 3.17M | decoder->value -= scaled_range; |
298 | 3.17M | bit=1; |
299 | 3.17M | } |
300 | 541M | else |
301 | 541M | { |
302 | 541M | bit=0; |
303 | 541M | } |
304 | | |
305 | 544M | logtrace(LogCABAC,"[%3d] -> bit %d r:%x v:%x\n", logcnt, bit, decoder->range, decoder->value); |
306 | | #ifdef DE265_LOG_TRACE |
307 | | logcnt++; |
308 | | #endif |
309 | | |
310 | 544M | return bit; |
311 | 544M | } |
312 | | |
313 | | |
314 | | int decode_CABAC_TU_bypass(CABAC_decoder* decoder, int cMax) |
315 | 25.6M | { |
316 | 26.0M | for (int i=0;i<cMax;i++) |
317 | 25.9M | { |
318 | 25.9M | int bit = decode_CABAC_bypass(decoder); |
319 | 25.9M | if (bit==0) |
320 | 25.6M | return i; |
321 | 25.9M | } |
322 | | |
323 | 25.8k | return cMax; |
324 | 25.6M | } |
325 | | |
326 | | int decode_CABAC_TU(CABAC_decoder* decoder, int cMax, context_model* model) |
327 | 0 | { |
328 | 0 | for (int i=0;i<cMax;i++) |
329 | 0 | { |
330 | 0 | int bit = decode_CABAC_bit(decoder,model); |
331 | 0 | if (bit==0) |
332 | 0 | return i; |
333 | 0 | } |
334 | | |
335 | 0 | return cMax; |
336 | 0 | } |
337 | | |
338 | | |
339 | | int decode_CABAC_FL_bypass_parallel(CABAC_decoder* decoder, int nBits) |
340 | 22.8M | { |
341 | 22.8M | logtrace(LogCABAC,"[%3d] bypass group r:%x v:%x (nBits=%d)\n",logcnt, |
342 | 22.8M | decoder->range, decoder->value, nBits); |
343 | | |
344 | 22.8M | decoder->value <<= nBits; |
345 | 22.8M | decoder->bits_needed+=nBits; |
346 | | |
347 | 22.8M | if (decoder->bits_needed >= 0) |
348 | 15.0M | { |
349 | 15.0M | if (decoder->bitstream_end > decoder->bitstream_curr) { |
350 | 98.8k | int input = *decoder->bitstream_curr++; |
351 | 98.8k | input <<= decoder->bits_needed; |
352 | | |
353 | 98.8k | decoder->bits_needed -= 8; |
354 | 98.8k | decoder->value |= input; |
355 | 98.8k | } |
356 | 15.0M | } |
357 | | |
358 | 22.8M | uint32_t scaled_range = decoder->range << 7; |
359 | 22.8M | int value = decoder->value / scaled_range; |
360 | 22.8M | if (unlikely(value>=(1<<nBits))) { value=(1<<nBits)-1; } // may happen with broken bitstreams |
361 | 22.8M | decoder->value -= value * scaled_range; |
362 | | |
363 | 22.8M | logtrace(LogCABAC,"[%3d] -> value %d r:%x v:%x\n", logcnt+nBits-1, |
364 | 22.8M | value, decoder->range, decoder->value); |
365 | | |
366 | | #ifdef DE265_LOG_TRACE |
367 | | logcnt+=nBits; |
368 | | #endif |
369 | | |
370 | 22.8M | return value; |
371 | 22.8M | } |
372 | | |
373 | | |
374 | | int decode_CABAC_FL_bypass(CABAC_decoder* decoder, int nBits) |
375 | 189M | { |
376 | 189M | int value=0; |
377 | | |
378 | 189M | if (likely(nBits<=8)) { |
379 | 189M | if (nBits==0) { |
380 | 169M | return 0; |
381 | 169M | } |
382 | | // we could use decode_CABAC_bypass() for a single bit, but this seems to be slower |
383 | | #if 0 |
384 | | else if (nBits==1) { |
385 | | value = decode_CABAC_bypass(decoder); |
386 | | } |
387 | | #endif |
388 | 19.6M | else { |
389 | 19.6M | value = decode_CABAC_FL_bypass_parallel(decoder,nBits); |
390 | 19.6M | } |
391 | 189M | } |
392 | 508k | else { |
393 | 508k | value = decode_CABAC_FL_bypass_parallel(decoder,8); |
394 | 508k | nBits-=8; |
395 | | |
396 | 14.8M | while (nBits--) { |
397 | 14.3M | value <<= 1; |
398 | 14.3M | value |= decode_CABAC_bypass(decoder); |
399 | 14.3M | } |
400 | 508k | } |
401 | 20.1M | logtrace(LogCABAC," -> FL: %d\n", value); |
402 | | |
403 | 20.1M | return value; |
404 | 189M | } |
405 | | |
406 | | int decode_CABAC_TR_bypass(CABAC_decoder* decoder, int cRiceParam, int cTRMax) |
407 | 0 | { |
408 | 0 | int prefix = decode_CABAC_TU_bypass(decoder, cTRMax>>cRiceParam); |
409 | 0 | if (prefix==4) { // TODO check: constant 4 only works for coefficient decoding |
410 | 0 | return cTRMax; |
411 | 0 | } |
412 | | |
413 | 0 | int suffix = decode_CABAC_FL_bypass(decoder, cRiceParam); |
414 | |
|
415 | 0 | return (prefix << cRiceParam) | suffix; |
416 | 0 | } |
417 | | |
418 | | |
419 | 27.3k | #define MAX_PREFIX 32 |
420 | | |
421 | | int decode_CABAC_EGk_bypass(CABAC_decoder* decoder, int k) |
422 | 4.49M | { |
423 | 4.49M | int base=0; |
424 | 4.49M | int n=k; |
425 | | |
426 | 4.49M | for (;;) |
427 | 4.52M | { |
428 | 4.52M | int bit = decode_CABAC_bypass(decoder); |
429 | 4.52M | if (bit==0) |
430 | 4.49M | break; |
431 | 27.3k | else { |
432 | 27.3k | base += 1<<n; |
433 | 27.3k | n++; |
434 | 27.3k | } |
435 | | |
436 | 27.3k | if (n == k+MAX_PREFIX) { |
437 | 16 | return 0; // TODO: error |
438 | 16 | } |
439 | 27.3k | } |
440 | | |
441 | 4.49M | int suffix = decode_CABAC_FL_bypass(decoder, n); |
442 | 4.49M | return base + suffix; |
443 | 4.49M | } |
444 | | |
445 | | |
446 | | // --------------------------------------------------------------------------- |
447 | | |
448 | | void CABAC_encoder::add_trailing_bits() |
449 | 0 | { |
450 | 0 | write_bit(1); |
451 | 0 | int nZeros = number_free_bits_in_byte(); |
452 | 0 | write_bits(0, nZeros); |
453 | 0 | } |
454 | | |
455 | | |
456 | | |
457 | | CABAC_encoder_bitstream::CABAC_encoder_bitstream() |
458 | 0 | { |
459 | 0 | data_mem = NULL; |
460 | 0 | data_capacity = 0; |
461 | 0 | data_size = 0; |
462 | 0 | state = 0; |
463 | |
|
464 | 0 | vlc_buffer_len = 0; |
465 | |
|
466 | 0 | init_CABAC(); |
467 | 0 | } |
468 | | |
469 | | CABAC_encoder_bitstream::~CABAC_encoder_bitstream() |
470 | 0 | { |
471 | 0 | free(data_mem); |
472 | 0 | } |
473 | | |
474 | | void CABAC_encoder_bitstream::reset() |
475 | 0 | { |
476 | 0 | data_size = 0; |
477 | 0 | state = 0; |
478 | |
|
479 | 0 | vlc_buffer_len = 0; |
480 | |
|
481 | 0 | init_CABAC(); |
482 | 0 | } |
483 | | |
484 | | void CABAC_encoder_bitstream::write_bits(uint32_t bits,int n) |
485 | 0 | { |
486 | 0 | vlc_buffer <<= n; |
487 | 0 | vlc_buffer |= bits; |
488 | 0 | vlc_buffer_len += n; |
489 | |
|
490 | 0 | while (vlc_buffer_len>=8) { |
491 | 0 | append_byte((vlc_buffer >> (vlc_buffer_len-8)) & 0xFF); |
492 | 0 | vlc_buffer_len -= 8; |
493 | 0 | } |
494 | 0 | } |
495 | | |
496 | | void CABAC_encoder::write_uvlc(int value) |
497 | 0 | { |
498 | 0 | assert(value>=0); |
499 | | |
500 | 0 | int nLeadingZeros=0; |
501 | 0 | int base=0; |
502 | 0 | int range=1; |
503 | |
|
504 | 0 | while (value>=base+range) { |
505 | 0 | base += range; |
506 | 0 | range <<= 1; |
507 | 0 | nLeadingZeros++; |
508 | 0 | } |
509 | |
|
510 | 0 | write_bits((1<<nLeadingZeros) | (value-base),2*nLeadingZeros+1); |
511 | 0 | } |
512 | | |
513 | | void CABAC_encoder::write_svlc(int value) |
514 | 0 | { |
515 | 0 | if (value==0) write_bits(1,1); |
516 | 0 | else if (value>0) write_uvlc(2*value-1); |
517 | 0 | else write_uvlc(-2*value); |
518 | 0 | } |
519 | | |
520 | | void CABAC_encoder_bitstream::flush_VLC() |
521 | 0 | { |
522 | 0 | while (vlc_buffer_len>=8) { |
523 | 0 | append_byte((vlc_buffer >> (vlc_buffer_len-8)) & 0xFF); |
524 | 0 | vlc_buffer_len -= 8; |
525 | 0 | } |
526 | |
|
527 | 0 | if (vlc_buffer_len>0) { |
528 | 0 | append_byte(vlc_buffer << (8-vlc_buffer_len)); |
529 | 0 | vlc_buffer_len = 0; |
530 | 0 | } |
531 | |
|
532 | 0 | vlc_buffer = 0; |
533 | 0 | } |
534 | | |
535 | | void CABAC_encoder_bitstream::skip_bits(int nBits) |
536 | 0 | { |
537 | 0 | while (nBits>=8) { |
538 | 0 | write_bits(0,8); |
539 | 0 | nBits-=8; |
540 | 0 | } |
541 | |
|
542 | 0 | if (nBits>0) { |
543 | 0 | write_bits(0,nBits); |
544 | 0 | } |
545 | 0 | } |
546 | | |
547 | | |
548 | | int CABAC_encoder_bitstream::number_free_bits_in_byte() const |
549 | 0 | { |
550 | 0 | if ((vlc_buffer_len % 8)==0) return 0; |
551 | 0 | return 8- (vlc_buffer_len % 8); |
552 | 0 | } |
553 | | |
554 | | |
555 | | void CABAC_encoder_bitstream::check_size_and_resize(int nBytes) |
556 | 0 | { |
557 | 0 | if (data_size+nBytes > data_capacity) { // 1 extra byte for stuffing |
558 | 0 | if (data_capacity==0) { |
559 | 0 | data_capacity = INITIAL_CABAC_BUFFER_CAPACITY; |
560 | 0 | } else { |
561 | 0 | data_capacity *= 2; |
562 | 0 | } |
563 | |
|
564 | 0 | data_mem = (uint8_t*)realloc(data_mem,data_capacity); |
565 | 0 | } |
566 | 0 | } |
567 | | |
568 | | |
569 | | void CABAC_encoder_bitstream::append_byte(int byte) |
570 | 0 | { |
571 | 0 | check_size_and_resize(2); |
572 | | |
573 | | // --- emulation prevention --- |
574 | | |
575 | | /* These byte sequences may never occur in the bitstream: |
576 | | 0x000000 / 0x000001 / 0x000002 |
577 | | |
578 | | Hence, we have to add a 0x03 before the third byte. |
579 | | We also have to add a 0x03 for this sequence: 0x000003, because |
580 | | the escape byte itself also has to be escaped. |
581 | | */ |
582 | | |
583 | | // S0 --(0)--> S1 --(0)--> S2 --(0,1,2,3)--> add stuffing |
584 | |
|
585 | 0 | if (byte<=3) { |
586 | 0 | /**/ if (state< 2 && byte==0) { state++; } |
587 | 0 | else if (state==2 && byte<=3) { |
588 | 0 | data_mem[ data_size++ ] = 3; |
589 | |
|
590 | 0 | if (byte==0) state=1; |
591 | 0 | else state=0; |
592 | 0 | } |
593 | 0 | else { state=0; } |
594 | 0 | } |
595 | 0 | else { state=0; } |
596 | | |
597 | | |
598 | | // write actual data byte |
599 | |
|
600 | 0 | data_mem[ data_size++ ] = byte; |
601 | 0 | } |
602 | | |
603 | | |
604 | | void CABAC_encoder_bitstream::write_startcode() |
605 | 0 | { |
606 | 0 | check_size_and_resize(3); |
607 | |
|
608 | 0 | data_mem[ data_size+0 ] = 0; |
609 | 0 | data_mem[ data_size+1 ] = 0; |
610 | 0 | data_mem[ data_size+2 ] = 1; |
611 | 0 | data_size+=3; |
612 | 0 | } |
613 | | |
614 | | void CABAC_encoder_bitstream::init_CABAC() |
615 | 0 | { |
616 | 0 | range = 510; |
617 | 0 | low = 0; |
618 | |
|
619 | 0 | bits_left = 23; |
620 | 0 | buffered_byte = 0xFF; |
621 | 0 | num_buffered_bytes = 0; |
622 | 0 | } |
623 | | |
624 | | void CABAC_encoder_bitstream::flush_CABAC() |
625 | 0 | { |
626 | 0 | if (low >> (32 - bits_left)) |
627 | 0 | { |
628 | 0 | append_byte(buffered_byte + 1); |
629 | 0 | while (num_buffered_bytes > 1) |
630 | 0 | { |
631 | 0 | append_byte(0x00); |
632 | 0 | num_buffered_bytes--; |
633 | 0 | } |
634 | |
|
635 | 0 | low -= 1 << (32 - bits_left); |
636 | 0 | } |
637 | 0 | else |
638 | 0 | { |
639 | 0 | if (num_buffered_bytes > 0) |
640 | 0 | { |
641 | 0 | append_byte(buffered_byte); |
642 | 0 | } |
643 | |
|
644 | 0 | while (num_buffered_bytes > 1) |
645 | 0 | { |
646 | 0 | append_byte(0xff); |
647 | 0 | num_buffered_bytes--; |
648 | 0 | } |
649 | 0 | } |
650 | | |
651 | | // printf("low: %08x nbits left:%d filled:%d\n",low,bits_left,32-bits_left); |
652 | |
|
653 | 0 | write_bits(low >> 8, 24-bits_left); |
654 | 0 | } |
655 | | |
656 | | |
657 | | void CABAC_encoder_bitstream::write_out() |
658 | 0 | { |
659 | | //logtrace(LogCABAC,"low = %08x (bits_left=%d)\n",low,bits_left); |
660 | 0 | int leadByte = low >> (24 - bits_left); |
661 | 0 | bits_left += 8; |
662 | 0 | low &= 0xffffffffu >> bits_left; |
663 | | |
664 | | //logtrace(LogCABAC,"write byte %02x\n",leadByte); |
665 | | //logtrace(LogCABAC,"-> low = %08x\n",low); |
666 | |
|
667 | 0 | if (leadByte == 0xff) |
668 | 0 | { |
669 | 0 | num_buffered_bytes++; |
670 | 0 | } |
671 | 0 | else |
672 | 0 | { |
673 | 0 | if (num_buffered_bytes > 0) |
674 | 0 | { |
675 | 0 | int carry = leadByte >> 8; |
676 | 0 | int byte = buffered_byte + carry; |
677 | 0 | buffered_byte = leadByte & 0xff; |
678 | 0 | append_byte(byte); |
679 | |
|
680 | 0 | byte = ( 0xff + carry ) & 0xff; |
681 | 0 | while ( num_buffered_bytes > 1 ) |
682 | 0 | { |
683 | 0 | append_byte(byte); |
684 | 0 | num_buffered_bytes--; |
685 | 0 | } |
686 | 0 | } |
687 | 0 | else |
688 | 0 | { |
689 | 0 | num_buffered_bytes = 1; |
690 | 0 | buffered_byte = leadByte; |
691 | 0 | } |
692 | 0 | } |
693 | 0 | } |
694 | | |
695 | | void CABAC_encoder_bitstream::testAndWriteOut() |
696 | 0 | { |
697 | | // logtrace(LogCABAC,"bits_left = %d\n",bits_left); |
698 | |
|
699 | 0 | if (bits_left < 12) |
700 | 0 | { |
701 | 0 | write_out(); |
702 | 0 | } |
703 | 0 | } |
704 | | |
705 | | |
706 | | #ifdef DE265_LOG_TRACE |
707 | | int encBinCnt=1; |
708 | | #endif |
709 | | |
710 | | void CABAC_encoder_bitstream::write_CABAC_bit(int modelIdx, int bin) |
711 | 0 | { |
712 | 0 | context_model* model = &(*mCtxModels)[modelIdx]; |
713 | | //m_uiBinsCoded += m_binCountIncrement; |
714 | | //rcCtxModel.setBinsCoded( 1 ); |
715 | |
|
716 | 0 | logtrace(LogCABAC,"[%d] range=%x low=%x state=%d, bin=%d\n", |
717 | 0 | encBinCnt, range,low, model->state,bin); |
718 | | |
719 | | /* |
720 | | printf("[%d] range=%x low=%x state=%d, bin=%d\n", |
721 | | encBinCnt, range,low, model->state,bin); |
722 | | |
723 | | printf("%d %d X\n",model->state,bin != model->MPSbit); |
724 | | */ |
725 | |
|
726 | | #ifdef DE265_LOG_TRACE |
727 | | encBinCnt++; |
728 | | #endif |
729 | |
|
730 | 0 | uint32_t LPS = LPS_table[model->state][ ( range >> 6 ) - 4 ]; |
731 | 0 | range -= LPS; |
732 | |
|
733 | 0 | if (bin != model->MPSbit) |
734 | 0 | { |
735 | | //logtrace(LogCABAC,"LPS\n"); |
736 | |
|
737 | 0 | int num_bits = renorm_table[ LPS >> 3 ]; |
738 | 0 | low = (low + range) << num_bits; |
739 | 0 | range = LPS << num_bits; |
740 | |
|
741 | 0 | if (model->state==0) { model->MPSbit = 1-model->MPSbit; } |
742 | |
|
743 | 0 | model->state = next_state_LPS[model->state]; |
744 | |
|
745 | 0 | bits_left -= num_bits; |
746 | 0 | } |
747 | 0 | else |
748 | 0 | { |
749 | | //logtrace(LogCABAC,"MPS\n"); |
750 | |
|
751 | 0 | model->state = next_state_MPS[model->state]; |
752 | | |
753 | | |
754 | | // renorm |
755 | |
|
756 | 0 | if (range >= 256) { return; } |
757 | | |
758 | 0 | low <<= 1; |
759 | 0 | range <<= 1; |
760 | 0 | bits_left--; |
761 | 0 | } |
762 | | |
763 | 0 | testAndWriteOut(); |
764 | 0 | } |
765 | | |
766 | | void CABAC_encoder_bitstream::write_CABAC_bypass(int bin) |
767 | 0 | { |
768 | 0 | logtrace(LogCABAC,"[%d] bypass = %d, range=%x\n",encBinCnt,bin,range); |
769 | | /* |
770 | | printf("[%d] bypass = %d, range=%x\n",encBinCnt,bin,range); |
771 | | printf("%d %d X\n",64, -1); |
772 | | */ |
773 | |
|
774 | | #ifdef DE265_LOG_TRACE |
775 | | encBinCnt++; |
776 | | #endif |
777 | | |
778 | | // BinsCoded += m_binCountIncrement; |
779 | 0 | low <<= 1; |
780 | |
|
781 | 0 | if (bin) |
782 | 0 | { |
783 | 0 | low += range; |
784 | 0 | } |
785 | 0 | bits_left--; |
786 | |
|
787 | 0 | testAndWriteOut(); |
788 | 0 | } |
789 | | |
790 | | void CABAC_encoder::write_CABAC_TU_bypass(int value, int cMax) |
791 | 0 | { |
792 | 0 | for (int i=0;i<value;i++) { |
793 | 0 | write_CABAC_bypass(1); |
794 | 0 | } |
795 | |
|
796 | 0 | if (value<cMax) { |
797 | 0 | write_CABAC_bypass(0); |
798 | 0 | } |
799 | 0 | } |
800 | | |
801 | | void CABAC_encoder::write_CABAC_FL_bypass(int value, int n) |
802 | 0 | { |
803 | 0 | while (n>0) { |
804 | 0 | n--; |
805 | 0 | write_CABAC_bypass(value & (1<<n)); |
806 | 0 | } |
807 | 0 | } |
808 | | |
809 | | void CABAC_encoder_bitstream::write_CABAC_term_bit(int bit) |
810 | 0 | { |
811 | 0 | logtrace(LogCABAC,"CABAC term: range=%x\n", range); |
812 | |
|
813 | 0 | range -= 2; |
814 | |
|
815 | 0 | if (bit) { |
816 | 0 | low += range; |
817 | |
|
818 | 0 | low <<= 7; |
819 | 0 | range = 2 << 7; |
820 | 0 | bits_left -= 7; |
821 | 0 | } |
822 | 0 | else if (range >= 256) |
823 | 0 | { |
824 | 0 | return; |
825 | 0 | } |
826 | 0 | else |
827 | 0 | { |
828 | 0 | low <<= 1; |
829 | 0 | range <<= 1; |
830 | 0 | bits_left--; |
831 | 0 | } |
832 | | |
833 | 0 | testAndWriteOut(); |
834 | 0 | } |
835 | | |
836 | | |
837 | | |
838 | | |
839 | | static const uint32_t entropy_table[128] = { |
840 | | // -------------------- 200 -------------------- |
841 | | /* state= 0 */ 0x07d13 /* 0.977164 */, 0x08255 /* 1.018237 */, |
842 | | /* state= 1 */ 0x07738 /* 0.931417 */, 0x086ef /* 1.054179 */, |
843 | | /* state= 2 */ 0x0702b /* 0.876323 */, 0x0935a /* 1.151195 */, |
844 | | /* state= 3 */ 0x069e6 /* 0.827333 */, 0x09c7f /* 1.222650 */, |
845 | | /* state= 4 */ 0x062e8 /* 0.772716 */, 0x0a2c7 /* 1.271708 */, |
846 | | /* state= 5 */ 0x05c18 /* 0.719488 */, 0x0ae25 /* 1.360532 */, |
847 | | /* state= 6 */ 0x05632 /* 0.673414 */, 0x0b724 /* 1.430793 */, |
848 | | /* state= 7 */ 0x05144 /* 0.634904 */, 0x0c05d /* 1.502850 */, |
849 | | /* state= 8 */ 0x04bdf /* 0.592754 */, 0x0ccf2 /* 1.601145 */, |
850 | | /* state= 9 */ 0x0478d /* 0.559012 */, 0x0d57b /* 1.667843 */, |
851 | | /* state=10 */ 0x042ad /* 0.520924 */, 0x0de81 /* 1.738336 */, |
852 | | /* state=11 */ 0x03f4d /* 0.494564 */, 0x0e4b8 /* 1.786871 */, |
853 | | /* state=12 */ 0x03a9d /* 0.457945 */, 0x0f471 /* 1.909721 */, |
854 | | /* state=13 */ 0x037d5 /* 0.436201 */, 0x0fc56 /* 1.971385 */, |
855 | | /* state=14 */ 0x034c2 /* 0.412177 */, 0x10236 /* 2.017284 */, |
856 | | /* state=15 */ 0x031a6 /* 0.387895 */, 0x10d5c /* 2.104394 */, |
857 | | /* state=16 */ 0x02e62 /* 0.362383 */, 0x11b34 /* 2.212552 */, |
858 | | /* state=17 */ 0x02c20 /* 0.344752 */, 0x120b4 /* 2.255512 */, |
859 | | /* state=18 */ 0x029b8 /* 0.325943 */, 0x1294d /* 2.322672 */, |
860 | | /* state=19 */ 0x02791 /* 0.309143 */, 0x135e1 /* 2.420959 */, |
861 | | /* state=20 */ 0x02562 /* 0.292057 */, 0x13e37 /* 2.486077 */, |
862 | | /* state=21 */ 0x0230d /* 0.273846 */, 0x144fd /* 2.539000 */, |
863 | | /* state=22 */ 0x02193 /* 0.262308 */, 0x150c9 /* 2.631150 */, |
864 | | /* state=23 */ 0x01f5d /* 0.245026 */, 0x15ca0 /* 2.723641 */, |
865 | | /* state=24 */ 0x01de7 /* 0.233617 */, 0x162f9 /* 2.773246 */, |
866 | | /* state=25 */ 0x01c2f /* 0.220208 */, 0x16d99 /* 2.856259 */, |
867 | | /* state=26 */ 0x01a8e /* 0.207459 */, 0x17a93 /* 2.957634 */, |
868 | | /* state=27 */ 0x0195a /* 0.198065 */, 0x18051 /* 3.002477 */, |
869 | | /* state=28 */ 0x01809 /* 0.187778 */, 0x18764 /* 3.057759 */, |
870 | | /* state=29 */ 0x0164a /* 0.174144 */, 0x19460 /* 3.159206 */, |
871 | | /* state=30 */ 0x01539 /* 0.165824 */, 0x19f20 /* 3.243181 */, |
872 | | /* state=31 */ 0x01452 /* 0.158756 */, 0x1a465 /* 3.284334 */, |
873 | | /* state=32 */ 0x0133b /* 0.150261 */, 0x1b422 /* 3.407303 */, |
874 | | /* state=33 */ 0x0120c /* 0.140995 */, 0x1bce5 /* 3.475767 */, |
875 | | /* state=34 */ 0x01110 /* 0.133315 */, 0x1c394 /* 3.527962 */, |
876 | | /* state=35 */ 0x0104d /* 0.127371 */, 0x1d059 /* 3.627736 */, |
877 | | /* state=36 */ 0x00f8b /* 0.121451 */, 0x1d74b /* 3.681983 */, |
878 | | /* state=37 */ 0x00ef4 /* 0.116829 */, 0x1dfd0 /* 3.748540 */, |
879 | | /* state=38 */ 0x00e10 /* 0.109864 */, 0x1e6d3 /* 3.803335 */, |
880 | | /* state=39 */ 0x00d3f /* 0.103507 */, 0x1f925 /* 3.946462 */, |
881 | | /* state=40 */ 0x00cc4 /* 0.099758 */, 0x1fda7 /* 3.981667 */, |
882 | | /* state=41 */ 0x00c42 /* 0.095792 */, 0x203f8 /* 4.031012 */, |
883 | | /* state=42 */ 0x00b78 /* 0.089610 */, 0x20f7d /* 4.121014 */, |
884 | | /* state=43 */ 0x00afc /* 0.085830 */, 0x21dd6 /* 4.233102 */, |
885 | | /* state=44 */ 0x00a5e /* 0.081009 */, 0x22419 /* 4.282016 */, |
886 | | /* state=45 */ 0x00a1b /* 0.078950 */, 0x22a5e /* 4.331015 */, |
887 | | /* state=46 */ 0x00989 /* 0.074514 */, 0x23756 /* 4.432323 */, |
888 | | /* state=47 */ 0x0091b /* 0.071166 */, 0x24225 /* 4.516775 */, |
889 | | /* state=48 */ 0x008cf /* 0.068837 */, 0x2471a /* 4.555487 */, |
890 | | /* state=49 */ 0x00859 /* 0.065234 */, 0x25313 /* 4.649048 */, |
891 | | /* state=50 */ 0x00814 /* 0.063140 */, 0x25d67 /* 4.729721 */, |
892 | | /* state=51 */ 0x007b6 /* 0.060272 */, 0x2651f /* 4.790028 */, |
893 | | /* state=52 */ 0x0076e /* 0.058057 */, 0x2687c /* 4.816294 */, |
894 | | /* state=53 */ 0x00707 /* 0.054924 */, 0x27da7 /* 4.981661 */, |
895 | | /* state=54 */ 0x006d5 /* 0.053378 */, 0x28172 /* 5.011294 */, |
896 | | /* state=55 */ 0x00659 /* 0.049617 */, 0x28948 /* 5.072512 */, |
897 | | /* state=56 */ 0x00617 /* 0.047598 */, 0x297c5 /* 5.185722 */, |
898 | | /* state=57 */ 0x005dd /* 0.045814 */, 0x2a2df /* 5.272434 */, |
899 | | /* state=58 */ 0x005c1 /* 0.044965 */, 0x2a581 /* 5.293019 */, |
900 | | /* state=59 */ 0x00574 /* 0.042619 */, 0x2ad59 /* 5.354304 */, |
901 | | /* state=60 */ 0x0053b /* 0.040882 */, 0x2bba5 /* 5.465973 */, |
902 | | /* state=61 */ 0x0050c /* 0.039448 */, 0x2c596 /* 5.543651 */, |
903 | | /* state=62 */ 0x004e9 /* 0.038377 */, 0x2cd88 /* 5.605741 */, |
904 | | 0x00400 , 0x2d000 /* dummy, should never be used */ |
905 | | }; |
906 | | |
907 | | |
908 | | static const uint32_t entropy_table_orig[128] = { |
909 | | 0x07b23, 0x085f9, 0x074a0, 0x08cbc, 0x06ee4, 0x09354, 0x067f4, 0x09c1b, |
910 | | 0x060b0, 0x0a62a, 0x05a9c, 0x0af5b, 0x0548d, 0x0b955, 0x04f56, 0x0c2a9, |
911 | | 0x04a87, 0x0cbf7, 0x045d6, 0x0d5c3, 0x04144, 0x0e01b, 0x03d88, 0x0e937, |
912 | | 0x039e0, 0x0f2cd, 0x03663, 0x0fc9e, 0x03347, 0x10600, 0x03050, 0x10f95, |
913 | | 0x02d4d, 0x11a02, 0x02ad3, 0x12333, 0x0286e, 0x12cad, 0x02604, 0x136df, |
914 | | 0x02425, 0x13f48, 0x021f4, 0x149c4, 0x0203e, 0x1527b, 0x01e4d, 0x15d00, |
915 | | 0x01c99, 0x166de, 0x01b18, 0x17017, 0x019a5, 0x17988, 0x01841, 0x18327, |
916 | | 0x016df, 0x18d50, 0x015d9, 0x19547, 0x0147c, 0x1a083, 0x0138e, 0x1a8a3, |
917 | | 0x01251, 0x1b418, 0x01166, 0x1bd27, 0x01068, 0x1c77b, 0x00f7f, 0x1d18e, |
918 | | 0x00eda, 0x1d91a, 0x00e19, 0x1e254, 0x00d4f, 0x1ec9a, 0x00c90, 0x1f6e0, |
919 | | 0x00c01, 0x1fef8, 0x00b5f, 0x208b1, 0x00ab6, 0x21362, 0x00a15, 0x21e46, |
920 | | 0x00988, 0x2285d, 0x00934, 0x22ea8, 0x008a8, 0x239b2, 0x0081d, 0x24577, |
921 | | 0x007c9, 0x24ce6, 0x00763, 0x25663, 0x00710, 0x25e8f, 0x006a0, 0x26a26, |
922 | | 0x00672, 0x26f23, 0x005e8, 0x27ef8, 0x005ba, 0x284b5, 0x0055e, 0x29057, |
923 | | 0x0050c, 0x29bab, 0x004c1, 0x2a674, 0x004a7, 0x2aa5e, 0x0046f, 0x2b32f, |
924 | | 0x0041f, 0x2c0ad, 0x003e7, 0x2ca8d, 0x003ba, 0x2d323, 0x0010c, 0x3bfbb |
925 | | }; |
926 | | |
927 | | |
928 | | const uint32_t entropy_table_theory[128] = |
929 | | { |
930 | | 0x08000, 0x08000, 0x076da, 0x089a0, 0x06e92, 0x09340, 0x0670a, 0x09cdf, 0x06029, 0x0a67f, 0x059dd, 0x0b01f, 0x05413, 0x0b9bf, 0x04ebf, 0x0c35f, |
931 | | 0x049d3, 0x0ccff, 0x04546, 0x0d69e, 0x0410d, 0x0e03e, 0x03d22, 0x0e9de, 0x0397d, 0x0f37e, 0x03619, 0x0fd1e, 0x032ee, 0x106be, 0x02ffa, 0x1105d, |
932 | | 0x02d37, 0x119fd, 0x02aa2, 0x1239d, 0x02836, 0x12d3d, 0x025f2, 0x136dd, 0x023d1, 0x1407c, 0x021d2, 0x14a1c, 0x01ff2, 0x153bc, 0x01e2f, 0x15d5c, |
933 | | 0x01c87, 0x166fc, 0x01af7, 0x1709b, 0x0197f, 0x17a3b, 0x0181d, 0x183db, 0x016d0, 0x18d7b, 0x01595, 0x1971b, 0x0146c, 0x1a0bb, 0x01354, 0x1aa5a, |
934 | | 0x0124c, 0x1b3fa, 0x01153, 0x1bd9a, 0x01067, 0x1c73a, 0x00f89, 0x1d0da, 0x00eb7, 0x1da79, 0x00df0, 0x1e419, 0x00d34, 0x1edb9, 0x00c82, 0x1f759, |
935 | | 0x00bda, 0x200f9, 0x00b3c, 0x20a99, 0x00aa5, 0x21438, 0x00a17, 0x21dd8, 0x00990, 0x22778, 0x00911, 0x23118, 0x00898, 0x23ab8, 0x00826, 0x24458, |
936 | | 0x007ba, 0x24df7, 0x00753, 0x25797, 0x006f2, 0x26137, 0x00696, 0x26ad7, 0x0063f, 0x27477, 0x005ed, 0x27e17, 0x0059f, 0x287b6, 0x00554, 0x29156, |
937 | | 0x0050e, 0x29af6, 0x004cc, 0x2a497, 0x0048d, 0x2ae35, 0x00451, 0x2b7d6, 0x00418, 0x2c176, 0x003e2, 0x2cb15, 0x003af, 0x2d4b5, 0x0037f, 0x2de55 |
938 | | }; |
939 | | |
940 | | |
941 | | void CABAC_encoder_estim::write_CABAC_bit(int modelIdx, int bit) |
942 | 0 | { |
943 | 0 | context_model* model = &(*mCtxModels)[modelIdx]; |
944 | | //printf("[%d] state=%d, bin=%d\n", encBinCnt, model->state,bit); |
945 | | //encBinCnt++; |
946 | |
|
947 | 0 | int idx = model->state<<1; |
948 | |
|
949 | 0 | if (bit==model->MPSbit) { |
950 | 0 | model->state = next_state_MPS[model->state]; |
951 | 0 | } |
952 | 0 | else { |
953 | 0 | idx++; |
954 | 0 | if (model->state==0) { model->MPSbit = 1-model->MPSbit; } |
955 | 0 | model->state = next_state_LPS[model->state]; |
956 | 0 | } |
957 | |
|
958 | 0 | mFracBits += entropy_table[idx]; |
959 | | |
960 | | //printf("-> %08lx %f\n",entropy_table[idx], entropy_table[idx] / float(1<<15)); |
961 | 0 | } |
962 | | |
963 | | |
964 | | float CABAC_encoder::RDBits_for_CABAC_bin(int modelIdx, int bit) |
965 | 0 | { |
966 | 0 | context_model* model = &(*mCtxModels)[modelIdx]; |
967 | 0 | int idx = model->state<<1; |
968 | |
|
969 | 0 | if (bit!=model->MPSbit) { |
970 | 0 | idx++; |
971 | 0 | } |
972 | |
|
973 | 0 | return entropy_table[idx] / float(1<<15); |
974 | 0 | } |
975 | | |
976 | | |
977 | | void CABAC_encoder::write_CABAC_EGk(int val, int k) |
978 | 0 | { |
979 | 0 | while (val >= ( 1 << k ) ) { |
980 | 0 | write_CABAC_bypass(1); |
981 | 0 | val = val - ( 1 << k ); |
982 | 0 | k++; |
983 | 0 | } |
984 | |
|
985 | 0 | write_CABAC_bypass(0); |
986 | |
|
987 | 0 | while (k) { |
988 | 0 | k--; |
989 | 0 | write_CABAC_bypass((val >> k) & 1); |
990 | 0 | } |
991 | 0 | } |
992 | | |
993 | | |
994 | | |
995 | | void CABAC_encoder_estim_constant::write_CABAC_bit(int modelIdx, int bit) |
996 | 0 | { |
997 | 0 | context_model* model = &(*mCtxModels)[modelIdx]; |
998 | 0 | int idx = model->state<<1; |
999 | |
|
1000 | 0 | if (bit!=model->MPSbit) { |
1001 | 0 | idx++; |
1002 | 0 | } |
1003 | |
|
1004 | 0 | mFracBits += entropy_table[idx]; |
1005 | 0 | } |
1006 | | |
1007 | | |
1008 | | |
1009 | | #if 0 |
1010 | | void printtab(int idx,int s) |
1011 | | { |
1012 | | printf("%d %f %f %f\n", s, |
1013 | | double(entropy_table[idx])/0x8000, |
1014 | | double(entropy_table_orig[idx])/0x8000, |
1015 | | double(entropy_table_f265[idx])/0x8000); |
1016 | | } |
1017 | | |
1018 | | |
1019 | | void plot_tables() |
1020 | | { |
1021 | | for (int i=-62;i<=0;i++) { |
1022 | | int idx = -i *2; |
1023 | | int s = i; |
1024 | | printtab(idx,s); |
1025 | | } |
1026 | | |
1027 | | for (int i=0;i<=62;i++) { |
1028 | | int idx = 2*i +1; |
1029 | | int s = i; |
1030 | | printtab(idx,s); |
1031 | | } |
1032 | | } |
1033 | | #endif |