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 (int type = APV_ENTROPY_RUN_FIRST;
113 for (unsigned int code = 0; code < lut_size; code++) {
115 int k_run, k_level, adv, nb_levels;
116 GetBitContext gbc;
117 PutBitContext pbc;
118 uint8_t buffer[16];
119
120 memset(buffer, 0, sizeof(buffer));
121 init_put_bits(&pbc, buffer, sizeof(buffer));
123 flush_put_bits(&pbc);
124
125 k_run = start_run;
126 k_level = start_level;
127
128 ent = &decode_lut->multi_lut[type][k_run][k_level][code];
129 memset(ent, 0, sizeof(*ent));
130 init_get_bits8(&gbc, buffer, sizeof(buffer));
131
132 // Symbols alternate between run and level, starting
133 // with whichever the type indicates (so even values
134 // of sym are runs). Stop at the first one which
135 // does not fit in the lookup bits.
136 adv = 0;
137 nb_levels = 0;
138 for (int sym = type; sym < type + 4; sym++) {
139 int value, sign, pos;
140
141 if (sym % 2 == 0) {
142 // Reading a run.
143 value = apv_read_vlc(&gbc, k_run, decode_lut);
144 pos = get_bits_count(&gbc);
145 if (pos > APV_VLC_LUT_BITS)
146 break;
147 adv += value;
148 k_run = FFMIN(value >> 2, 2);
149 } else {
150 // Reading a level.
151 value = apv_read_vlc(&gbc, k_level, decode_lut);
152 sign = get_bits1(&gbc);
153 pos = get_bits_count(&gbc);
154 if (pos > APV_VLC_LUT_BITS)
155 break;
156 ++value;
157 av_assert0(value <= INT8_MAX);
158 ent->level[nb_levels] = sign ? -value : value;
159 ent->level_pos[nb_levels] = adv;
160 ++nb_levels;
161 adv += 1;
162 k_level = FFMIN(value >> 2, 4);
163 if (nb_levels == 1)
164 ent->k_level_0 = k_level;
165 }
166 ent->offset[ent->count] = pos;
167 ++ent->count;
168 }
169 // Missing levels write zeroes later in the array where
170 // they will either be overwritten or not used.
171 for (int i = nb_levels; i < 2; i++)
172 ent->level_pos[i] = adv;
173 // Entries are applied at scan positions below 64, so
174 // this keeps every write inside the extended zigzag.
175 av_assert0(adv < 64);
176 ent->scan_advance = adv;
177 if (ent->count > 0 && ent->count < 4)
178 ent->offset[3] = ent->offset[ent->count - 1];
179 ent->k_run = k_run;
180 ent->k_level_1 = k_level;
181 ent->next = APV_TABLE_INDEX(type ^ (ent->count & 1),
182 k_run, k_level);
183 }
184 }
185 }
186 }
187
188 // Positions past the end of the block all map to the dummy
189 // coefficient slot at the end.
190 for (int i = 0; i < FF_ARRAY_ELEMS(decode_lut->zigzag); i++)
191 decode_lut->zigzag[i] = i < 64 ? ff_zigzag_direct[i] : 64;
192}
193
194// Decode a run code which is too long for the multi-symbol LUT.
195// All such codes fit in the 18 bits already read.
197static int apv_decode_long_run(GetBitContext *restrict gbc,
198 uint32_t next_bits, int k_run,
199 APVEntropyState *restrict state)
200{
201 uint32_t bits, low_bits;
202 unsigned int leading_zeroes, low_bit_count, low_bit_shift;
203
204 // Remove the prefix bits.
205 bits = next_bits & 0xffff;
206 // Determine code length.
207 leading_zeroes = 15 - av_log2(bits);
208 if (leading_zeroes >= 6) {
209 // 6 zeroes implies run > 64, which is always invalid.
210 av_log(state->log_ctx, AV_LOG_ERROR,
211 "Out-of-range run value: %d leading zeroes.\n",
212 leading_zeroes);
213 return AVERROR_INVALIDDATA;
214 }
215 // Extract the low bits.
216 low_bit_count = leading_zeroes + k_run;
217 low_bit_shift = 16 - (1 + 2 * leading_zeroes + k_run);
218 low_bits = av_zero_extend(bits >> low_bit_shift, low_bit_count);
219 // Skip over the bits just used.
220 skip_bits(gbc, 2 + leading_zeroes + 1 + low_bit_count);
221 // Construct run code.
222 return (2 << k_run) +
223 ((1 << leading_zeroes) - 1) * (1 << k_run) +
224 low_bits;
225}
226
227// Decode a level code which is too long for the multi-symbol LUT,
228// returning the signed level and setting the absolute value.
229// The 18 bits already read are sufficient for the length, but we may
230// need to read more bits to complete the value.
232static int apv_decode_long_level(GetBitContext *restrict gbc,
233 uint32_t next_bits, int k_level,
234 int *abs_level_out, int scan_pos,
235 APVEntropyState *restrict state)
236{
237 uint32_t bits;
238 unsigned int leading_zeroes;
239 int level, abs_level, sign;
240
241 // Remove the prefix bits.
242 bits = next_bits & 0xffff;
243 // Determine code length.
244 leading_zeroes = 15 - av_log2(bits);
245 // Skip the prefix and length bits.
246 skip_bits(gbc, 2 + leading_zeroes + 1);
247 // Read the rest of the code and construct the level.
248 // Include the + 1 offset for nonzero value here.
249 abs_level = (2 << k_level) +
250 ((1 << leading_zeroes) - 1) * (1 << k_level) +
251 get_bits(gbc, leading_zeroes + k_level) + 1;
252
253 sign = get_bits1(gbc);
254 if (sign)
255 level = -abs_level;
256 else
257 level = abs_level;
258
259 // Check range (not checked in any other case, only a long
260 // code can be out of range).
263 av_log(state->log_ctx, AV_LOG_ERROR,
264 "Out-of-range AC coefficient value at %d: %d.\n",
265 scan_pos, level);
266 return AVERROR_INVALIDDATA;
267 }
268 *abs_level_out = abs_level;
269 return level;
270}
271
272// Apply the symbols from a multi-symbol LUT entry. Both levels are
273// always written: absent ones are zero, and any beyond the end of the
274// block go to the dummy coefficient. Returns nonzero if the end of
275// the block was reached, in which case only the bits up to the symbol
276// which reached it are consumed.
278static int apv_apply_multi_entry(int16_t *restrict coeff,
279 GetBitContext *restrict gbc,
280 const APVMultiVLCLUTEntry *ent,
281 const uint8_t *zigzag,
282 int *scan_pos_ptr, int type)
283{
284 int scan_pos = *scan_pos_ptr;
285
286 coeff[zigzag[scan_pos + ent->level_pos[0]]] = ent->level[0];
287 coeff[zigzag[scan_pos + ent->level_pos[1]]] = ent->level[1];
288
289 if (scan_pos + ent->scan_advance >= 64) {
290 // We've gone over the end: find the symbol which reached the
291 // end and stop there.
292 int end[4], i;
293
295 end[0] = ent->level_pos[0];
296 end[1] = ent->level_pos[0] + 1;
297 end[2] = ent->level_pos[1];
298 end[3] = ent->level_pos[1] + 1;
299 } else {
300 end[0] = 1;
301 end[1] = ent->level_pos[1];
302 end[2] = ent->level_pos[1] + 1;
303 end[3] = ent->scan_advance;
304 }
305 for (i = 0; i < ent->count - 1; i++) {
306 if (scan_pos + end[i] >= 64)
307 break;
308 }
309 skip_bits(gbc, ent->offset[i]);
310 *scan_pos_ptr = scan_pos + end[i];
311 return 1;
312 } else {
313 // All codes used: advance the bitstream and scan position by
314 // the full values.
315 skip_bits(gbc, ent->offset[3]);
316 *scan_pos_ptr = scan_pos + ent->scan_advance;
317 return 0;
318 }
319}
320
322static int apv_entropy_decode_block_internal(int16_t *restrict coeff,
323 GetBitContext *restrict gbc,
324 APVEntropyState *restrict state)
325{
326 const APVVLCLUT *lut = state->decode_lut;
327 const APVMultiVLCLUTEntry *tables = lut->multi_lut[0][0][0];
328 const uint8_t *zigzag = lut->zigzag;
329 int scan_pos, type, table_index;
330 int k_dc = state->prev_k_dc;
331 int k_run, k_level;
332 uint32_t next_bits, lut_bits;
333 const APVMultiVLCLUTEntry *ent;
334
335 // DC coefficient is likely to be large and cannot be usefully
336 // combined with other read steps, so extract it separately.
337 {
338 int dc_coeff, abs_diff, sign;
339
340 abs_diff = apv_read_vlc(gbc, k_dc, lut);
341
342 if (abs_diff) {
343 sign = get_bits1(gbc);
344 if (sign)
345 dc_coeff = state->prev_dc - abs_diff;
346 else
347 dc_coeff = state->prev_dc + abs_diff;
348 } else {
349 dc_coeff = state->prev_dc;
350 }
351
352
353 if (dc_coeff < APV_MIN_TRANS_COEFF ||
354 dc_coeff > APV_MAX_TRANS_COEFF) {
355 av_log(state->log_ctx, AV_LOG_ERROR,
356 "Out-of-range DC coefficient value: %d.\n",
357 dc_coeff);
358 return AVERROR_INVALIDDATA;
359 }
360
361 coeff[0] = dc_coeff;
362
363 state->prev_dc = dc_coeff;
364 state->prev_k_dc = FFMIN(abs_diff >> 1, 5);
365 }
366
367 // Repeatedly read 18 bits, look up the first half of them in either
368 // the run-first or the level-first table. If the next code is too
369 // long the 18 bits will allow resolving a run code (up to 63)
370 // without reading any more bits, and will allow the exact length
371 // of a level code to be determined. (Note that reusing the
372 // single-symbol LUT is never useful here as the multisymbol lookup
373 // has already determined that the code is too long.)
374
375 // The first level in the block sets the k_level for the next block,
376 // so run the first lookup (and the second, if the first did not
377 // contain a level) separately.
378
379 scan_pos = 1;
380 k_run = 0;
381 k_level = state->prev_k_level;
382 {
383 next_bits = show_bits(gbc, 18);
384 lut_bits = next_bits >> (18 - APV_VLC_LUT_BITS);
385
386 ent = &lut->multi_lut[APV_ENTROPY_RUN_FIRST][0][k_level][lut_bits];
387
388 if (ent->count == 0) {
389 int run = apv_decode_long_run(gbc, next_bits, 0, state);
390 if (run < 0)
391 return run;
392 scan_pos += run;
393 if (scan_pos >= 64)
394 goto end_of_block;
395 k_run = FFMIN(run >> 2, 2);
396 } else {
397 // The level is only real if the run before it did not
398 // reach the end of the block.
399 if (ent->count > 1 && scan_pos + ent->level_pos[0] < 64)
400 state->prev_k_level = ent->k_level_0;
401 if (apv_apply_multi_entry(coeff, gbc, ent, zigzag,
402 &scan_pos, APV_ENTROPY_RUN_FIRST))
403 goto end_of_block;
404 k_run = ent->k_run;
405 k_level = ent->k_level_1;
406 if (ent->count > 1) {
407 type = APV_ENTROPY_RUN_FIRST ^ (ent->count & 1);
408 goto normal_loop;
409 }
410 }
411 }
412
413 // Only read a run, still need to read the first level.
414 {
415 next_bits = show_bits(gbc, 18);
416 lut_bits = next_bits >> (18 - APV_VLC_LUT_BITS);
417
418 ent = &lut->multi_lut[APV_ENTROPY_LEVEL_FIRST][k_run][k_level][lut_bits];
419
420 if (ent->count == 0) {
421 int level, abs_level;
422 level = apv_decode_long_level(gbc, next_bits, k_level,
423 &abs_level, scan_pos, state);
425 return level;
426 coeff[zigzag[scan_pos]] = level;
427 ++scan_pos;
428 k_level = FFMIN(abs_level >> 2, 4);
429 state->prev_k_level = k_level;
430 if (scan_pos >= 64)
431 goto end_of_block;
433 } else {
434 state->prev_k_level = ent->k_level_0;
435 if (apv_apply_multi_entry(coeff, gbc, ent, zigzag,
436 &scan_pos, APV_ENTROPY_LEVEL_FIRST))
437 goto end_of_block;
438 k_run = ent->k_run;
439 k_level = ent->k_level_1;
440 type = APV_ENTROPY_LEVEL_FIRST ^ (ent->count & 1);
441 }
442 }
443
444normal_loop:
445 // From here the only state carried between lookups is the index of
446 // the table to use, which each entry supplies for the next one.
447 table_index = APV_TABLE_INDEX(type, k_run, k_level);
448 for (;;) {
449 next_bits = show_bits(gbc, 18);
450 lut_bits = next_bits >> (18 - APV_VLC_LUT_BITS);
451
452 ent = &tables[table_index * APV_VLC_LUT_SIZE + lut_bits];
453
454 if (ent->count == 0) {
455 // Missed in the lookup, so need to re-derive the type,
456 // k_run and k_level from the table index.
457 k_run = table_index / 5 % 3;
458 k_level = table_index % 5;
459 if (table_index < APV_TABLE_INDEX(APV_ENTROPY_LEVEL_FIRST, 0, 0)) {
460 int run = apv_decode_long_run(gbc, next_bits, k_run, state);
461 if (run < 0)
462 return run;
463 scan_pos += run;
464 if (scan_pos >= 64)
465 goto end_of_block;
466 k_run = FFMIN(run >> 2, 2);
467 table_index = APV_TABLE_INDEX(APV_ENTROPY_LEVEL_FIRST, k_run, k_level);
468 } else {
469 int level, abs_level;
470 level = apv_decode_long_level(gbc, next_bits, k_level,
471 &abs_level, scan_pos, state);
473 return level;
474 coeff[zigzag[scan_pos]] = level;
475 ++scan_pos;
476 k_level = FFMIN(abs_level >> 2, 4);
477 if (scan_pos >= 64)
478 goto end_of_block;
479 table_index = APV_TABLE_INDEX(APV_ENTROPY_RUN_FIRST, k_run, k_level);
480 }
481 } else {
482 type = table_index >= APV_TABLE_INDEX(APV_ENTROPY_LEVEL_FIRST, 0, 0);
483 if (apv_apply_multi_entry(coeff, gbc, ent, zigzag,
484 &scan_pos, type))
485 goto end_of_block;
486 table_index = ent->next;
487 }
488 }
489
490 end_of_block: {
491 if (scan_pos > 64) {
492 av_log(state->log_ctx, AV_LOG_ERROR,
493 "Block decode reached invalid scan position %d.\n",
494 scan_pos);
495 return AVERROR_INVALIDDATA;
496 }
497 return 0;
498 }
499}
500
501int ff_apv_entropy_decode_block(int16_t *restrict coeff,
502 GetBitContext *restrict gbc,
503 APVEntropyState *restrict state)
504{
505 // Use a local copy of the bitstream reader in this translation unit
506 // to improve optimisation opportunities. Only the position in the
507 // bitstream needs to be written back.
508 GetBitContext gb = *gbc;
509 int err = apv_entropy_decode_block_internal(coeff, &gb, state);
510 gbc->index = gb.index;
511 return err;
512}
#define APV_VLC_LUT_SIZE
Definition apv_decode.h:39
#define APV_VLC_LUT_BITS
Definition apv_decode.h:38
#define APV_TABLE_INDEX(type, k_run, k_level)
Definition apv_decode.h:89
@ APV_ENTROPY_LEVEL_FIRST
Definition apv_decode.h:85
@ APV_ENTROPY_RUN_FIRST
Definition apv_decode.h:83
static av_always_inline int apv_entropy_decode_block_internal(int16_t *restrict coeff, GetBitContext *restrict gbc, APVEntropyState *restrict state)
static av_always_inline unsigned int apv_read_vlc(GetBitContext *restrict gbc, int k_param, const APVVLCLUT *restrict lut)
Definition apv_entropy.c:26
static av_always_inline int apv_apply_multi_entry(int16_t *restrict coeff, GetBitContext *restrict gbc, const APVMultiVLCLUTEntry *ent, const uint8_t *zigzag, int *scan_pos_ptr, int type)
static av_always_inline int apv_decode_long_run(GetBitContext *restrict gbc, uint32_t next_bits, int k_run, APVEntropyState *restrict state)
int ff_apv_entropy_decode_block(int16_t *restrict coeff, GetBitContext *restrict gbc, APVEntropyState *restrict state)
Entropy decode a single 8x8 block to coefficients.
static av_always_inline int apv_decode_long_level(GetBitContext *restrict gbc, uint32_t next_bits, int k_level, int *abs_level_out, int scan_pos, APVEntropyState *restrict state)
void ff_apv_entropy_build_decode_lut(APVVLCLUT *decode_lut)
Build the decoder VLC look-up tables.
Definition apv_entropy.c:62
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
#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 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
enum MPFType type
Definition jpegmpfenc.c:55
@ 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
static const uint8_t zigzag[]
Definition mv30.c:88
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
#define FF_ARRAY_ELEMS(a)
const uint8_t * code
Definition spdifenc.c:433
unsigned int pos
Definition spdifenc.c:431
uint8_t level_pos[2]
Definition apv_decode.h:67
uint8_t offset[4]
Definition apv_decode.h:74
APVMultiVLCLUTEntry multi_lut[2][3][5][APV_VLC_LUT_SIZE]
Definition apv_decode.h:100
uint8_t zigzag[128]
Definition apv_decode.h:103
APVSingleVLCLUTEntry single_lut[6][APV_VLC_LUT_SIZE]
Definition apv_decode.h:96
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:33
static struct @232240247175332074307271266321204261331170211232 state
static const uint8_t *const tables[]
static int remaining_bits(WmallDecodeCtx *s, GetBitContext *gb)
Calculate remaining input buffer length.