FFmpeg
Loading...
Searching...
No Matches
apv_entropy.c
Go to the documentation of this file.
1/*
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19#include "apv.h"
20#include "apv_decode.h"
21
22#include "put_bits.h"
23
24
26static unsigned int apv_read_vlc(GetBitContext *restrict gbc, int k_param,
27 const APVVLCLUT *restrict lut)
28{
29 unsigned int next_bits;
30 const APVSingleVLCLUTEntry *ent;
31
32 next_bits = show_bits(gbc, APV_VLC_LUT_BITS);
33 ent = &lut->single_lut[k_param][next_bits];
34
35 if (ent->more) {
36 unsigned int leading_zeroes;
37
38 skip_bits(gbc, ent->consume);
39
40 next_bits = show_bits(gbc, 16);
41 leading_zeroes = 15 - av_log2(next_bits);
42
43 if (leading_zeroes == 0) {
44 // This can't happen mid-stream because the lookup would
45 // have resolved a leading one into a shorter code, but it
46 // can happen if we are hitting the end of the buffer.
47 // Return an invalid code to propagate as an error.
48 return APV_MAX_TRANS_COEFF + 1;
49 }
50
51 skip_bits(gbc, leading_zeroes + 1);
52
53 return (2 << k_param) +
54 ((1 << leading_zeroes) - 1) * (1 << k_param) +
55 get_bits(gbc, leading_zeroes + k_param);
56 } else {
57 skip_bits(gbc, ent->consume);
58 return ent->result;
59 }
60}
61
63{
64 const int code_len = APV_VLC_LUT_BITS;
65 const int lut_size = APV_VLC_LUT_SIZE;
66
67 // Build the single-symbol VLC table.
68 for (int k = 0; k <= 5; k++) {
69 for (unsigned int code = 0; code < lut_size; code++) {
70 APVSingleVLCLUTEntry *ent = &decode_lut->single_lut[k][code];
71 unsigned int first_bit = code & (1 << code_len - 1);
72 unsigned int remaining_bits = code ^ first_bit;
73
74 if (first_bit) {
75 ent->consume = 1 + k;
76 ent->result = remaining_bits >> (code_len - k - 1);
77 ent->more = 0;
78 } else {
79 unsigned int second_bit = code & (1 << code_len - 2);
80 remaining_bits ^= second_bit;
81
82 if (second_bit) {
83 unsigned int bits_left = code_len - 2;
84 unsigned int first_set = bits_left - av_log2(remaining_bits);
85 unsigned int last_bits = first_set - 1 + k;
86
87 if (first_set + last_bits <= bits_left) {
88 // Whole code fits here.
89 ent->consume = 2 + first_set + last_bits;
90 ent->result = ((2 << k) +
91 (((1 << first_set - 1) - 1) << k) +
92 ((code >> bits_left - first_set - last_bits) & (1 << last_bits) - 1));
93 ent->more = 0;
94 } else {
95 // Need to read more, collapse to default.
96 ent->consume = 2;
97 ent->more = 1;
98 }
99 } else {
100 ent->consume = 2 + k;
101 ent->result = (1 << k) + (remaining_bits >> (code_len - k - 2));
102 ent->more = 0;
103 }
104 }
105 }
106 }
107
108 // Build the multi-symbol VLC table.
109 for (int start_run = 0; start_run <= 2; start_run++) {
110 for (int start_level = 0; start_level <= 4; start_level++) {
111 for (unsigned int code = 0; code < lut_size; code++) {
113 int k_run, k_level;
114 GetBitContext gbc;
115 PutBitContext pbc;
116 uint8_t buffer[16];
117 uint8_t run_first_buffer[16];
118 uint8_t level_first_buffer[16];
119
120 memset(buffer, 0, sizeof(buffer));
121 init_put_bits(&pbc, buffer, sizeof(buffer));
123 flush_put_bits(&pbc);
124
125 memcpy(run_first_buffer, buffer, sizeof(buffer));
126 memcpy(level_first_buffer, buffer, sizeof(buffer));
127
128 k_run = start_run;
129 k_level = start_level;
130
131 ent = &decode_lut->run_first_lut[k_run][k_level][code];
132 memset(ent, 0, sizeof(*ent));
133 init_get_bits8(&gbc, run_first_buffer, sizeof(run_first_buffer));
134
135 ent->count = 0;
136 for (int i = 0; i <= 1; i++) {
137 int value, sign, pos;
138
139 value = apv_read_vlc(&gbc, k_run, decode_lut);
140 pos = get_bits_count(&gbc);
141 if (pos > APV_VLC_LUT_BITS)
142 break;
143 ent->run[i] = value;
144 ent->offset[ent->count] = pos;
145 ++ent->count;
146 k_run = FFMIN(value >> 2, 2);
147
148 value = apv_read_vlc(&gbc, k_level, decode_lut);
149 sign = get_bits1(&gbc);
150 pos = get_bits_count(&gbc);
151 if (pos > APV_VLC_LUT_BITS)
152 break;
153 ++value;
154 ent->level[i] = sign ? -value : value;
155 ent->offset[ent->count] = pos;
156 ++ent->count;
157 k_level = FFMIN(value >> 2, 4);
158 if (i == 0)
159 ent->k_level_0 = k_level;
160 }
161 if (ent->count > 0 && ent->count < 4)
162 ent->offset[3] = ent->offset[ent->count - 1];
163 ent->k_run = k_run;
164 ent->k_level_1 = k_level;
165
166 k_run = start_run;
167 k_level = start_level;
168
169 ent = &decode_lut->level_first_lut[k_run][k_level][code];
170 memset(ent, 0, sizeof(*ent));
171 init_get_bits8(&gbc, level_first_buffer, sizeof(level_first_buffer));
172
173 ent->count = 0;
174 for (int i = 0; i <= 1; i++) {
175 int value, sign, pos;
176
177 value = apv_read_vlc(&gbc, k_level, decode_lut);
178 sign = get_bits1(&gbc);
179 pos = get_bits_count(&gbc);
180 if (pos > APV_VLC_LUT_BITS)
181 break;
182 ++value;
183 ent->level[i] = sign ? -value : value;
184 ent->offset[ent->count] = pos;
185 ++ent->count;
186 k_level = FFMIN(value >> 2, 4);
187 if (i == 0)
188 ent->k_level_0 = k_level;
189
190 value = apv_read_vlc(&gbc, k_run, decode_lut);
191 pos = get_bits_count(&gbc);
192 if (pos > APV_VLC_LUT_BITS)
193 break;
194 ent->run[i] = value;
195 ent->offset[ent->count] = pos;
196 ++ent->count;
197 k_run = FFMIN(value >> 2, 2);
198 }
199 if (ent->count > 0 && ent->count < 4)
200 ent->offset[3] = ent->offset[ent->count - 1];
201 ent->k_run = k_run;
202 ent->k_level_1 = k_level;
203 }
204 }
205 }
206}
207
208int ff_apv_entropy_decode_block(int16_t *restrict coeff,
209 GetBitContext *restrict gbc,
210 APVEntropyState *restrict state)
211{
212 const APVVLCLUT *lut = state->decode_lut;
213 int scan_pos;
214 int k_dc = state->prev_k_dc;
215 int k_run, k_level;
216 uint32_t next_bits, lut_bits;
217 const APVMultiVLCLUTEntry *ent;
218
219 // DC coefficient is likely to be large and cannot be usefully
220 // combined with other read steps, so extract it separately.
221 {
222 int dc_coeff, abs_diff, sign;
223
224 abs_diff = apv_read_vlc(gbc, k_dc, lut);
225
226 if (abs_diff) {
227 sign = get_bits1(gbc);
228 if (sign)
229 dc_coeff = state->prev_dc - abs_diff;
230 else
231 dc_coeff = state->prev_dc + abs_diff;
232 } else {
233 dc_coeff = state->prev_dc;
234 }
235
236
237 if (dc_coeff < APV_MIN_TRANS_COEFF ||
238 dc_coeff > APV_MAX_TRANS_COEFF) {
239 av_log(state->log_ctx, AV_LOG_ERROR,
240 "Out-of-range DC coefficient value: %d.\n",
241 dc_coeff);
242 return AVERROR_INVALIDDATA;
243 }
244
245 coeff[0] = dc_coeff;
246
247 state->prev_dc = dc_coeff;
248 state->prev_k_dc = FFMIN(abs_diff >> 1, 5);
249 }
250
251 // Repeatedly read 18 bits, look up the first half of them in either
252 // the run-first or the level-first table. If the next code is too
253 // long the 18 bits will allow resolving a run code (up to 63)
254 // without reading any more bits, and will allow the exact length
255 // of a level code to be determined. (Note that reusing the
256 // single-symbol LUT is never useful here as the multisymbol lookup
257 // has already determined that the code is too long.)
258
259 // Run a single iteration of the run-first LUT to start, then a
260 // single iteration of the level-first LUT if that only read a
261 // single code. This avoids dealing with the first-AC logic inside
262 // the normal code lookup sequence.
263
264 k_level = state->prev_k_level;
265 {
266 next_bits = show_bits(gbc, 18);
267 lut_bits = next_bits >> (18 - APV_VLC_LUT_BITS);
268
269 ent = &lut->run_first_lut[0][k_level][lut_bits];
270
271 if (ent->count == 0) {
272 // One long code.
273 uint32_t bits, low_bits;
274 unsigned int leading_zeroes, low_bit_count, low_bit_shift;
275 int run;
276
277 // Remove the prefix bits.
278 bits = next_bits & 0xffff;
279 // Determine code length.
280 leading_zeroes = 15 - av_log2(bits);
281 if (leading_zeroes >= 6) {
282 // 6 zeroes implies run > 64, which is always invalid.
283 av_log(state->log_ctx, AV_LOG_ERROR,
284 "Out-of-range run value: %d leading zeroes.\n",
285 leading_zeroes);
286 return AVERROR_INVALIDDATA;
287 }
288 // Extract the low bits.
289 low_bit_count = leading_zeroes;
290 low_bit_shift = 16 - (1 + 2 * leading_zeroes);
291 low_bits = av_zero_extend(bits >> low_bit_shift, low_bit_count);
292 // Construct run code.
293 run = 2 + ((1 << leading_zeroes) - 1) + low_bits;
294 // Skip over the bits just used.
295 skip_bits(gbc, 2 + leading_zeroes + 1 + low_bit_count);
296
297 scan_pos = run + 1;
298 if (scan_pos >= 64)
299 goto end_of_block;
300 k_run = FFMIN(run >> 2, 2);
301 goto first_level;
302 } else {
303 // One or more short codes starting with a run; if there is
304 // a level code then the length needs to be saved for the
305 // next block.
306
307 scan_pos = ent->run[0] + 1;
308 if (scan_pos >= 64) {
309 skip_bits(gbc, ent->offset[0]);
310 goto end_of_block;
311 }
312 if (ent->count > 1) {
313 coeff[ff_zigzag_direct[scan_pos]] = ent->level[0];
314 ++scan_pos;
315 state->prev_k_level = ent->k_level_0;
316 if (scan_pos >= 64) {
317 skip_bits(gbc, ent->offset[1]);
318 goto end_of_block;
319 }
320 }
321 if (ent->count > 2) {
322 scan_pos += ent->run[1];
323 if (scan_pos >= 64) {
324 skip_bits(gbc, ent->offset[2]);
325 goto end_of_block;
326 }
327 }
328 if (ent->count > 3) {
329 coeff[ff_zigzag_direct[scan_pos]] = ent->level[1];
330 ++scan_pos;
331 if (scan_pos >= 64) {
332 skip_bits(gbc, ent->offset[3]);
333 goto end_of_block;
334 }
335 }
336 skip_bits(gbc, ent->offset[3]);
337 k_run = ent->k_run;
338 k_level = ent->k_level_1;
339 if (ent->count == 1)
340 goto first_level;
341 else if (ent->count & 1)
342 goto next_is_level;
343 else
344 goto next_is_run;
345 }
346 }
347
348 first_level: {
349 next_bits = show_bits(gbc, 18);
350 lut_bits = next_bits >> (18 - APV_VLC_LUT_BITS);
351
352 ent = &lut->level_first_lut[k_run][k_level][lut_bits];
353
354 if (ent->count == 0) {
355 // One long code.
356 uint32_t bits;
357 unsigned int leading_zeroes;
358 int level, abs_level, sign;
359
360 // Remove the prefix bits.
361 bits = next_bits & 0xffff;
362 // Determine code length.
363 leading_zeroes = 15 - av_log2(bits);
364 // Skip the prefix and length bits.
365 skip_bits(gbc, 2 + leading_zeroes + 1);
366 // Read the rest of the code and construct the level.
367 // Include the + 1 offset for nonzero value here.
368 abs_level = (2 << k_level) +
369 ((1 << leading_zeroes) - 1) * (1 << k_level) +
370 get_bits(gbc, leading_zeroes + k_level) + 1;
371
372 sign = get_bits(gbc, 1);
373 if (sign)
374 level = -abs_level;
375 else
376 level = abs_level;
377
378 // Check range (not checked in any other case, only a long
379 // code can be out of range).
382 av_log(state->log_ctx, AV_LOG_ERROR,
383 "Out-of-range AC coefficient value at %d: %d.\n",
384 scan_pos, level);
385 return AVERROR_INVALIDDATA;
386 }
387 coeff[ff_zigzag_direct[scan_pos]] = level;
388 ++scan_pos;
389 k_level = FFMIN(abs_level >> 2, 4);
390 state->prev_k_level = k_level;
391 if (scan_pos >= 64)
392 goto end_of_block;
393 goto next_is_run;
394
395 } else {
396 // One or more short codes.
397
398 coeff[ff_zigzag_direct[scan_pos]] = ent->level[0];
399 ++scan_pos;
400 state->prev_k_level = ent->k_level_0;
401 if (scan_pos >= 64) {
402 skip_bits(gbc, ent->offset[0]);
403 goto end_of_block;
404 }
405 if (ent->count > 1) {
406 scan_pos += ent->run[0];
407 if (scan_pos >= 64) {
408 skip_bits(gbc, ent->offset[1]);
409 goto end_of_block;
410 }
411 }
412 if (ent->count > 2) {
413 coeff[ff_zigzag_direct[scan_pos]] = ent->level[1];
414 ++scan_pos;
415 if (scan_pos >= 64) {
416 skip_bits(gbc, ent->offset[2]);
417 goto end_of_block;
418 }
419 }
420 if (ent->count > 3) {
421 scan_pos += ent->run[1];
422 if (scan_pos >= 64) {
423 skip_bits(gbc, ent->offset[3]);
424 goto end_of_block;
425 }
426 }
427 skip_bits(gbc, ent->offset[3]);
428 k_run = ent->k_run;
429 k_level = ent->k_level_1;
430 if (ent->count & 1)
431 goto next_is_run;
432 else
433 goto next_is_level;
434 }
435 }
436
437 next_is_run: {
438 next_bits = show_bits(gbc, 18);
439 lut_bits = next_bits >> (18 - APV_VLC_LUT_BITS);
440
441 ent = &lut->run_first_lut[k_run][k_level][lut_bits];
442
443 if (ent->count == 0) {
444 // One long code.
445 uint32_t bits, low_bits;
446 unsigned int leading_zeroes, low_bit_count, low_bit_shift;
447 int run;
448
449 // Remove the prefix bits.
450 bits = next_bits & 0xffff;
451 // Determine code length.
452 leading_zeroes = 15 - av_log2(bits);
453 if (leading_zeroes >= 6) {
454 // 6 zeroes implies run > 64, which is always invalid.
455 av_log(state->log_ctx, AV_LOG_ERROR,
456 "Out-of-range run value: %d leading zeroes.\n",
457 leading_zeroes);
458 return AVERROR_INVALIDDATA;
459 }
460 // Extract the low bits.
461 low_bit_count = leading_zeroes + k_run;
462 low_bit_shift = 16 - (1 + 2 * leading_zeroes + k_run);
463 low_bits = av_zero_extend(bits >> low_bit_shift, low_bit_count);
464 // Construct run code.
465 run = (2 << k_run) +
466 ((1 << leading_zeroes) - 1) * (1 << k_run) +
467 low_bits;
468 // Skip over the bits just used.
469 skip_bits(gbc, 2 + leading_zeroes + 1 + low_bit_count);
470
471 scan_pos += run;
472 if (scan_pos >= 64)
473 goto end_of_block;
474 k_run = FFMIN(run >> 2, 2);
475 goto next_is_level;
476
477 } else {
478 // One or more short codes.
479
480 scan_pos += ent->run[0];
481 if (scan_pos >= 64) {
482 skip_bits(gbc, ent->offset[0]);
483 goto end_of_block;
484 }
485 if (ent->count > 1) {
486 coeff[ff_zigzag_direct[scan_pos]] = ent->level[0];
487 ++scan_pos;
488 if (scan_pos >= 64) {
489 skip_bits(gbc, ent->offset[1]);
490 goto end_of_block;
491 }
492 }
493 if (ent->count > 2) {
494 scan_pos += ent->run[1];
495 if (scan_pos >= 64) {
496 skip_bits(gbc, ent->offset[2]);
497 goto end_of_block;
498 }
499 }
500 if (ent->count > 3) {
501 coeff[ff_zigzag_direct[scan_pos]] = ent->level[1];
502 ++scan_pos;
503 if (scan_pos >= 64) {
504 skip_bits(gbc, ent->offset[3]);
505 goto end_of_block;
506 }
507 }
508 skip_bits(gbc, ent->offset[3]);
509 k_run = ent->k_run;
510 k_level = ent->k_level_1;
511 if (ent->count & 1)
512 goto next_is_level;
513 else
514 goto next_is_run;
515 }
516 }
517
518 next_is_level: {
519 next_bits = show_bits(gbc, 18);
520 lut_bits = next_bits >> (18 - APV_VLC_LUT_BITS);
521
522 ent = &lut->level_first_lut[k_run][k_level][lut_bits];
523
524 if (ent->count == 0) {
525 // One long code.
526 uint32_t bits;
527 unsigned int leading_zeroes;
528 int level, abs_level, sign;
529
530 // Remove the prefix bits.
531 bits = next_bits & 0xffff;
532 // Determine code length.
533 leading_zeroes = 15 - av_log2(bits);
534 // Skip the prefix and length bits.
535 skip_bits(gbc, 2 + leading_zeroes + 1);
536 // Read the rest of the code and construct the level.
537 // Include the + 1 offset for nonzero value here.
538 abs_level = (2 << k_level) +
539 ((1 << leading_zeroes) - 1) * (1 << k_level) +
540 get_bits(gbc, leading_zeroes + k_level) + 1;
541
542 sign = get_bits(gbc, 1);
543 if (sign)
544 level = -abs_level;
545 else
546 level = abs_level;
547
548 // Check range (not checked in any other case, only a long
549 // code can be out of range).
552 av_log(state->log_ctx, AV_LOG_ERROR,
553 "Out-of-range AC coefficient value at %d: %d.\n",
554 scan_pos, level);
555 return AVERROR_INVALIDDATA;
556 }
557 coeff[ff_zigzag_direct[scan_pos]] = level;
558 ++scan_pos;
559 k_level = FFMIN(abs_level >> 2, 4);
560 if (scan_pos >= 64)
561 goto end_of_block;
562 goto next_is_run;
563
564 } else {
565 // One or more short codes.
566
567 coeff[ff_zigzag_direct[scan_pos]] = ent->level[0];
568 ++scan_pos;
569 if (scan_pos >= 64) {
570 skip_bits(gbc, ent->offset[0]);
571 goto end_of_block;
572 }
573 if (ent->count > 1) {
574 scan_pos += ent->run[0];
575 if (scan_pos >= 64) {
576 skip_bits(gbc, ent->offset[1]);
577 goto end_of_block;
578 }
579 }
580 if (ent->count > 2) {
581 coeff[ff_zigzag_direct[scan_pos]] = ent->level[1];
582 ++scan_pos;
583 if (scan_pos >= 64) {
584 skip_bits(gbc, ent->offset[2]);
585 goto end_of_block;
586 }
587 }
588 if (ent->count > 3) {
589 scan_pos += ent->run[1];
590 if (scan_pos >= 64) {
591 skip_bits(gbc, ent->offset[3]);
592 goto end_of_block;
593 }
594 }
595 skip_bits(gbc, ent->offset[3]);
596 k_run = ent->k_run;
597 k_level = ent->k_level_1;
598 if (ent->count & 1)
599 goto next_is_run;
600 else
601 goto next_is_level;
602 }
603 }
604
605 end_of_block: {
606 if (scan_pos > 64) {
607 av_log(state->log_ctx, AV_LOG_ERROR,
608 "Block decode reached invalid scan position %d.\n",
609 scan_pos);
610 return AVERROR_INVALIDDATA;
611 }
612 return 0;
613 }
614}
#define APV_VLC_LUT_SIZE
Definition apv_decode.h:38
#define APV_VLC_LUT_BITS
Definition apv_decode.h:37
static av_always_inline unsigned int apv_read_vlc(GetBitContext *restrict gbc, int k_param, const APVVLCLUT *restrict lut)
Definition apv_entropy.c:26
int ff_apv_entropy_decode_block(int16_t *restrict coeff, GetBitContext *restrict gbc, APVEntropyState *restrict state)
Entropy decode a single 8x8 block to coefficients.
void ff_apv_entropy_build_decode_lut(APVVLCLUT *decode_lut)
Build the decoder VLC look-up tables.
Definition apv_entropy.c:62
#define bits_left
Definition bitstream.h:116
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define av_zero_extend
Definition common.h:151
double value
Definition eval.c:102
static struct @346255127015250356166251341105367306144006377143 state
static const uint8_t bits[8]
Definition fastaudio.c:100
static unsigned int get_bits1(GetBitContext *s)
Definition get_bits.h:391
static void skip_bits(GetBitContext *s, int n)
Definition get_bits.h:383
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition get_bits.h:544
static int get_bits_count(const GetBitContext *s)
Definition get_bits.h:254
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition get_bits.h:337
static unsigned int show_bits(GetBitContext *s, int n)
Show 1-25 bits.
Definition get_bits.h:373
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
#define av_log2
Definition intmath.h:84
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition j2kenc.c:154
@ APV_MIN_TRANS_COEFF
Definition apv.h:56
@ APV_MAX_TRANS_COEFF
Definition apv.h:57
#define av_always_inline
Definition attributes.h:72
#define FFMIN(a, b)
Definition macros.h:49
const uint8_t ff_zigzag_direct[64]
Definition mathtables.c:137
bitstream writer API
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition put_bits.h:62
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition put_bits.h:153
const uint8_t * code
Definition spdifenc.c:433
unsigned int pos
Definition spdifenc.c:431
uint8_t offset[4]
Definition apv_decode.h:60
APVMultiVLCLUTEntry run_first_lut[3][5][APV_VLC_LUT_SIZE]
Definition apv_decode.h:71
APVMultiVLCLUTEntry level_first_lut[3][5][APV_VLC_LUT_SIZE]
Definition apv_decode.h:72
APVSingleVLCLUTEntry single_lut[6][APV_VLC_LUT_SIZE]
Definition apv_decode.h:67
uint8_t run
Definition svq3.c:207
uint8_t level
Definition svq3.c:208
#define av_log(a,...)
static char buffer[20]
Definition seek.c:32
static const double coeff[2][5]
static int remaining_bits(WmallDecodeCtx *s, GetBitContext *gb)
Calculate remaining input buffer length.