History

apv_decode: Multisymbol entropy decode

Mark Thompson committed 1 year ago

Commit a3c8fba1 parent 9980a0b3 files · +541 −135Browse files

Unified · Split

3 files
  1. libavcodec/apv_decode.c +3 −2
  2. libavcodec/apv_decode.h +40 −19
  3. libavcodec/apv_entropy.c +498 −114
libavcodec/apv_decode.c+3−2
@@ -160,6 +160,7 @@ static int apv_decode_block(AVCodecContext *avctx, · 159 unmodified lines
160 int err;160 int err;
161 161
162 LOCAL_ALIGNED_32(int16_t, coeff, [64]);162 LOCAL_ALIGNED_32(int16_t, coeff, [64]);
163 memset(coeff, 0, 64 * sizeof(int16_t));
163 164
164 err = ff_apv_entropy_decode_block(coeff, gbc, entropy_state);165 err = ff_apv_entropy_decode_block(coeff, gbc, entropy_state);
165 if (err < 0)166 if (err < 0)
@@ -216,8 +217,8 @@ static int apv_decode_tile_component(AVCodecContext *avctx, void *data, · 50 unmodified lines
216 .log_ctx = avctx,217 .log_ctx = avctx,
217 .decode_lut = &decode_lut,218 .decode_lut = &decode_lut,
218 .prev_dc = 0,219 .prev_dc = 0,
219 .prev_dc_diff = 20,220 .prev_k_dc = 5,
220 .prev_1st_ac_level = 0,221 .prev_k_level = 0,
221 };222 };
222 223
223 int err;224 int err;
libavcodec/apv_decode.h+40−19
@@ -33,14 +33,39 @@ · 32 unmodified lines
33#define APV_VLC_LUT_BITS 933#define APV_VLC_LUT_BITS 9
34#define APV_VLC_LUT_SIZE (1 << APV_VLC_LUT_BITS)34#define APV_VLC_LUT_SIZE (1 << APV_VLC_LUT_BITS)
35 35
36typedef struct APVVLCLUTEntry {36typedef struct APVSingleVLCLUTEntry {
37 uint16_t result; // Return value if not reading more.37 uint16_t result; // Return value if not reading more.
38 uint8_t consume; // Number of bits to consume.38 uint8_t consume; // Number of bits to consume.
39 uint8_t more; // Whether to read additional bits.39 uint8_t more; // Whether to read additional bits.
40} APVVLCLUTEntry;40} APVSingleVLCLUTEntry;
41
42typedef struct APVMultiVLCLUTEntry {
43 // Number of symbols this bit stream resolves to.
44 uint8_t count;
45 // k_run after decoding all symbols.
46 uint8_t k_run : 2;
47 // k_level after decoding the first level symbol.
48 uint8_t k_level_0 : 3;
49 // k_level after decoding all symbols.
50 uint8_t k_level_1 : 3;
51 // Run output values.
52 uint8_t run[2];
53 // Level output values.
54 int16_t level[2];
55 // Bit index of the end of each code.
56 uint8_t offset[4];
57} APVMultiVLCLUTEntry;
41 58
42typedef struct APVVLCLUT {59typedef struct APVVLCLUT {
43 APVVLCLUTEntry lut[6][APV_VLC_LUT_SIZE];60 // Single-symbol LUT for VLCs.
61 // Applies to all coefficients, but used only for DC coefficients
62 // in the decoder.
63 APVSingleVLCLUTEntry single_lut[6][APV_VLC_LUT_SIZE];
64 // Multi-symbol LUT for run/level combinations, decoding up to four
65 // symbols per step. Comes in two versions, which to use depends on
66 // whether the next symbol is a run or a level.
67 APVMultiVLCLUTEntry run_first_lut[3][5][APV_VLC_LUT_SIZE];
68 APVMultiVLCLUTEntry level_first_lut[3][5][APV_VLC_LUT_SIZE];
44} APVVLCLUT;69} APVVLCLUT;
45 70
46typedef struct APVEntropyState {71typedef struct APVEntropyState {
@@ -48,33 +73,29 @@ typedef struct APVEntropyState { · 1 unmodified line
48 73
49 const APVVLCLUT *decode_lut;74 const APVVLCLUT *decode_lut;
50 75
76 // Previous DC level value.
51 int16_t prev_dc;77 int16_t prev_dc;
52 int16_t prev_dc_diff;78 // k parameter implied by the previous DC level value.
53 int16_t prev_1st_ac_level;79 uint8_t prev_k_dc;
80 // k parameter implied by the previous first AC level value.
81 uint8_t prev_k_level;
54} APVEntropyState;82} APVEntropyState;
55 83
56 84
57/**85/**
58 * Build the decoder VLC look-up table.86 * Build the decoder VLC look-up tables.
59 */87 */
60void ff_apv_entropy_build_decode_lut(APVVLCLUT *decode_lut);88void ff_apv_entropy_build_decode_lut(APVVLCLUT *decode_lut);
61 89
62/**90/**
63 * Entropy decode a single 8x8 block to coefficients.91 * Entropy decode a single 8x8 block to coefficients.
64 *92 *
65 * Outputs in block order (dezigzag already applied).93 * Outputs nonzero coefficients only to the block row-major order
94 * (dezigzag is applied within the function). The output block
95 * must have been filled with zeroes before calling this function.
66 */96 */
67int ff_apv_entropy_decode_block(int16_t *coeff,97int ff_apv_entropy_decode_block(int16_t *restrict coeff,
68 GetBitContext *gbc,98 GetBitContext *restrict gbc,
69 APVEntropyState *state);99 APVEntropyState *restrict state);
70
71/**
72 * Read a single APV VLC code.
73 *
74 * This entrypoint is exposed for testing.
75 */
76unsigned int ff_apv_read_vlc(GetBitContext *gbc, int k_param,
77 const APVVLCLUT *lut);
78
79 100
80#endif /* AVCODEC_APV_DECODE_H */101#endif /* AVCODEC_APV_DECODE_H */
libavcodec/apv_entropy.c+498−114
@@ -19,15 +19,55 @@ · 18 unmodified lines
19#include "apv.h"19#include "apv.h"
20#include "apv_decode.h"20#include "apv_decode.h"
21 21
22#include "put_bits.h"
23
24
25av_always_inline
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}
22 61
23void ff_apv_entropy_build_decode_lut(APVVLCLUT *decode_lut)62void ff_apv_entropy_build_decode_lut(APVVLCLUT *decode_lut)
24{63{
25 const int code_len = APV_VLC_LUT_BITS;64 const int code_len = APV_VLC_LUT_BITS;
26 const int lut_size = APV_VLC_LUT_SIZE;65 const int lut_size = APV_VLC_LUT_SIZE;
27 66
67 // Build the single-symbol VLC table.
28 for (int k = 0; k <= 5; k++) {68 for (int k = 0; k <= 5; k++) {
29 for (unsigned int code = 0; code < lut_size; code++) {69 for (unsigned int code = 0; code < lut_size; code++) {
30 APVVLCLUTEntry *ent = &decode_lut->lut[k][code];70 APVSingleVLCLUTEntry *ent = &decode_lut->single_lut[k][code];
31 unsigned int first_bit = code & (1 << code_len - 1);71 unsigned int first_bit = code & (1 << code_len - 1);
32 unsigned int remaining_bits = code ^ first_bit;72 unsigned int remaining_bits = code ^ first_bit;
33 73
@@ -64,153 +104,497 @@ void ff_apv_entropy_build_decode_lut(APVVLCLUT *decode_lut) · 30 unmodified lines
64 }104 }
65 }105 }
66 }106 }
67}
68
69av_always_inline
70static unsigned int apv_read_vlc(GetBitContext *gbc, int k_param,
71 const APVVLCLUT *lut)
72{
73 unsigned int next_bits;
74 const APVVLCLUTEntry *ent;
75
76 next_bits = show_bits(gbc, APV_VLC_LUT_BITS);
77 ent = &lut->lut[k_param][next_bits];
78
79 if (ent->more) {
80 unsigned int leading_zeroes;
81 107
82 skip_bits(gbc, ent->consume);108 // Build the multi-symbol VLC table.
83 109 for (int start_run = 0; start_run <= 2; start_run++) {
84 next_bits = show_bits(gbc, 16);110 for (int start_level = 0; start_level <= 4; start_level++) {
85 leading_zeroes = 15 - av_log2(next_bits);111 for (unsigned int code = 0; code < lut_size; code++) {
86 112 APVMultiVLCLUTEntry *ent;
87 if (leading_zeroes == 0) {113 int k_run, k_level;
88 // This can't happen mid-stream because the lookup would114 GetBitContext gbc;
89 // have resolved a leading one into a shorter code, but it115 PutBitContext pbc;
90 // can happen if we are hitting the end of the buffer.116 uint8_t buffer[16];
91 // Return an invalid code to propagate as an error.117 uint8_t run_first_buffer[16];
92 return APV_MAX_TRANS_COEFF + 1;118 uint8_t level_first_buffer[16];
119
120 memset(buffer, 0, sizeof(buffer));
121 init_put_bits(&pbc, buffer, sizeof(buffer));
122 put_bits(&pbc, APV_VLC_LUT_BITS, code);
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 }
93 }204 }
94
95 skip_bits(gbc, leading_zeroes + 1);
96
97 return (2 << k_param) +
98 ((1 << leading_zeroes) - 1) * (1 << k_param) +
99 get_bits(gbc, leading_zeroes + k_param);
100 } else {
101 skip_bits(gbc, ent->consume);
102 return ent->result;
103 }205 }
104}206}
105 207
106unsigned int ff_apv_read_vlc(GetBitContext *gbc, int k_param,208int ff_apv_entropy_decode_block(int16_t *restrict coeff,
107 const APVVLCLUT *lut)209 GetBitContext *restrict gbc,
108{210 APVEntropyState *restrict state)
109 return apv_read_vlc(gbc, k_param, lut);
110}
111
112int ff_apv_entropy_decode_block(int16_t *coeff,
113 GetBitContext *gbc,
114 APVEntropyState *state)
115{211{
116 const APVVLCLUT *lut = state->decode_lut;212 const APVVLCLUT *lut = state->decode_lut;
117 int k_param;213 int scan_pos;
118 214 int k_dc = state->prev_k_dc;
119 // DC coefficient.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.
120 {221 {
121 int abs_dc_coeff_diff;222 int dc_coeff, abs_diff, sign;
122 int sign_dc_coeff_diff;223
123 int dc_coeff;224 abs_diff = apv_read_vlc(gbc, k_dc, lut);
124 225
125 k_param = av_clip(state->prev_dc_diff >> 1, 0, 5);226 if (abs_diff) {
126 abs_dc_coeff_diff = apv_read_vlc(gbc, k_param, lut);227 sign = get_bits1(gbc);
127 228 if (sign)
128 if (abs_dc_coeff_diff > 0)229 dc_coeff = state->prev_dc - abs_diff;
129 sign_dc_coeff_diff = get_bits1(gbc);230 else
130 else231 dc_coeff = state->prev_dc + abs_diff;
131 sign_dc_coeff_diff = 0;232 } else {
233 dc_coeff = state->prev_dc;
234 }
132 235
133 if (sign_dc_coeff_diff)
134 dc_coeff = state->prev_dc - abs_dc_coeff_diff;
135 else
136 dc_coeff = state->prev_dc + abs_dc_coeff_diff;
137 236
138 if (dc_coeff < APV_MIN_TRANS_COEFF ||237 if (dc_coeff < APV_MIN_TRANS_COEFF ||
139 dc_coeff > APV_MAX_TRANS_COEFF) {238 dc_coeff > APV_MAX_TRANS_COEFF) {
140 av_log(state->log_ctx, AV_LOG_ERROR,239 av_log(state->log_ctx, AV_LOG_ERROR,
141 "Out-of-range DC coefficient value: %d "240 "Out-of-range DC coefficient value: %d.\n",
142 "(from prev_dc %d abs_dc_coeff_diff %d sign_dc_coeff_diff %d)\n",241 dc_coeff);
143 dc_coeff, state->prev_dc, abs_dc_coeff_diff, sign_dc_coeff_diff);
144 return AVERROR_INVALIDDATA;242 return AVERROR_INVALIDDATA;
145 }243 }
146 244
147 coeff[0] = dc_coeff;245 coeff[0] = dc_coeff;
148 246
149 state->prev_dc = dc_coeff;247 state->prev_dc = dc_coeff;
150 state->prev_dc_diff = abs_dc_coeff_diff;248 state->prev_k_dc = FFMIN(abs_diff >> 1, 5);
151 }249 }
152 250
153 // AC coefficients.251 // Repeatedly read 18 bits, look up the first half of them in either
154 {252 // the run-first or the level-first table. If the next code is too
155 int scan_pos = 1;253 // long the 18 bits will allow resolving a run code (up to 63)
156 int first_ac = 1;254 // without reading any more bits, and will allow the exact length
157 int prev_level = state->prev_1st_ac_level;255 // of a level code to be determined. (Note that reusing the
158 int prev_run = 0;256 // single-symbol LUT is never useful here as the multisymbol lookup
257 // has already determined that the code is too long.)
159 258
160 do {259 // Run a single iteration of the run-first LUT to start, then a
161 int coeff_zero_run;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.
162 263
163 k_param = av_clip(prev_run >> 2, 0, 2);264 k_level = state->prev_k_level;
164 coeff_zero_run = apv_read_vlc(gbc, k_param, lut);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 // Extract the low bits.
282 low_bit_count = leading_zeroes;
283 low_bit_shift = 16 - (1 + 2 * leading_zeroes);
284 low_bits = (bits >> low_bit_shift) & ((1 << low_bit_count) - 1);
285 // Construct run code.
286 run = 2 + ((1 << leading_zeroes) - 1) + low_bits;
287 // Skip over the bits just used.
288 skip_bits(gbc, 2 + leading_zeroes + 1 + low_bit_count);
289
290 scan_pos = run + 1;
291 if (scan_pos >= 64)
292 goto end_of_block;
293 k_run = FFMIN(run >> 2, 2);
294 goto first_level;
295 } else {
296 // One or more short codes starting with a run; if there is
297 // a level code then the length needs to be saved for the
298 // next block.
299
300 scan_pos = ent->run[0] + 1;
301 if (scan_pos >= 64) {
302 skip_bits(gbc, ent->offset[0]);
303 goto end_of_block;
304 }
305 if (ent->count > 1) {
306 coeff[ff_zigzag_direct[scan_pos]] = ent->level[0];
307 ++scan_pos;
308 state->prev_k_level = ent->k_level_0;
309 if (scan_pos >= 64) {
310 skip_bits(gbc, ent->offset[1]);
311 goto end_of_block;
312 }
313 }
314 if (ent->count > 2) {
315 scan_pos += ent->run[1];
316 if (scan_pos >= 64) {
317 skip_bits(gbc, ent->offset[2]);
318 goto end_of_block;
319 }
320 }
321 if (ent->count > 3) {
322 coeff[ff_zigzag_direct[scan_pos]] = ent->level[1];
323 ++scan_pos;
324 if (scan_pos >= 64) {
325 skip_bits(gbc, ent->offset[3]);
326 goto end_of_block;
327 }
328 }
329 skip_bits(gbc, ent->offset[3]);
330 k_run = ent->k_run;
331 k_level = ent->k_level_1;
332 if (ent->count == 1)
333 goto first_level;
334 else if (ent->count & 1)
335 goto next_is_level;
336 else
337 goto next_is_run;
338 }
339 }
165 340
166 if (coeff_zero_run > APV_BLK_COEFFS - scan_pos) {341 first_level: {
342 next_bits = show_bits(gbc, 18);
343 lut_bits = next_bits >> (18 - APV_VLC_LUT_BITS);
344
345 ent = &lut->level_first_lut[k_run][k_level][lut_bits];
346
347 if (ent->count == 0) {
348 // One long code.
349 uint32_t bits;
350 unsigned int leading_zeroes;
351 int level, abs_level, sign;
352
353 // Remove the prefix bits.
354 bits = next_bits & 0xffff;
355 // Determine code length.
356 leading_zeroes = 15 - av_log2(bits);
357 // Skip the prefix and length bits.
358 skip_bits(gbc, 2 + leading_zeroes + 1);
359 // Read the rest of the code and construct the level.
360 // Include the + 1 offset for nonzero value here.
361 abs_level = (2 << k_level) +
362 ((1 << leading_zeroes) - 1) * (1 << k_level) +
363 get_bits(gbc, leading_zeroes + k_level) + 1;
364
365 sign = get_bits(gbc, 1);
366 if (sign)
367 level = -abs_level;
368 else
369 level = abs_level;
370
371 // Check range (not checked in any other case, only a long
372 // code can be out of range).
373 if (level < APV_MIN_TRANS_COEFF ||
374 level > APV_MAX_TRANS_COEFF) {
167 av_log(state->log_ctx, AV_LOG_ERROR,375 av_log(state->log_ctx, AV_LOG_ERROR,
168 "Out-of-range zero-run value: %d (at scan pos %d)\n",376 "Out-of-range AC coefficient value at %d: %d.\n",
169 coeff_zero_run, scan_pos);377 scan_pos, level);
170 return AVERROR_INVALIDDATA;378 return AVERROR_INVALIDDATA;
171 }379 }
172 380 coeff[ff_zigzag_direct[scan_pos]] = level;
173 for (int i = 0; i < coeff_zero_run; i++) {381 ++scan_pos;
174 coeff[ff_zigzag_direct[scan_pos]] = 0;382 k_level = FFMIN(abs_level >> 2, 4);
383 state->prev_k_level = k_level;
384 if (scan_pos >= 64)
385 goto end_of_block;
386 goto next_is_run;
387
388 } else {
389 // One or more short codes.
390
391 coeff[ff_zigzag_direct[scan_pos]] = ent->level[0];
392 ++scan_pos;
393 state->prev_k_level = ent->k_level_0;
394 if (scan_pos >= 64) {
395 skip_bits(gbc, ent->offset[0]);
396 goto end_of_block;
397 }
398 if (ent->count > 1) {
399 scan_pos += ent->run[0];
400 if (scan_pos >= 64) {
401 skip_bits(gbc, ent->offset[1]);
402 goto end_of_block;
403 }
404 }
405 if (ent->count > 2) {
406 coeff[ff_zigzag_direct[scan_pos]] = ent->level[1];
175 ++scan_pos;407 ++scan_pos;
408 if (scan_pos >= 64) {
409 skip_bits(gbc, ent->offset[2]);
410 goto end_of_block;
411 }
176 }412 }
177 prev_run = coeff_zero_run;413 if (ent->count > 3) {
178 414 scan_pos += ent->run[1];
179 if (scan_pos < APV_BLK_COEFFS) {415 if (scan_pos >= 64) {
180 int abs_ac_coeff_minus1;416 skip_bits(gbc, ent->offset[3]);
181 int sign_ac_coeff;417 goto end_of_block;
182 int level;
183
184 k_param = av_clip(prev_level >> 2, 0, 4);
185 abs_ac_coeff_minus1 = apv_read_vlc(gbc, k_param, lut);
186 sign_ac_coeff = get_bits(gbc, 1);
187
188 if (sign_ac_coeff)
189 level = -abs_ac_coeff_minus1 - 1;
190 else
191 level = abs_ac_coeff_minus1 + 1;
192
193 if (level < APV_MIN_TRANS_COEFF ||
194 level > APV_MAX_TRANS_COEFF) {
195 av_log(state->log_ctx, AV_LOG_ERROR,
196 "Out-of-range AC coefficient value: %d "
197 "(from prev_level %d abs_ac_coeff_minus1 %d sign_ac_coeff %d)\n",
198 level, prev_level, abs_ac_coeff_minus1, sign_ac_coeff);
199 }418 }
419 }
420 skip_bits(gbc, ent->offset[3]);
421 k_run = ent->k_run;
422 k_level = ent->k_level_1;
423 if (ent->count & 1)
424 goto next_is_run;
425 else
426 goto next_is_level;
427 }
428 }
200 429
201 coeff[ff_zigzag_direct[scan_pos]] = level;430 next_is_run: {
202 431 next_bits = show_bits(gbc, 18);
203 prev_level = abs_ac_coeff_minus1 + 1;432 lut_bits = next_bits >> (18 - APV_VLC_LUT_BITS);
204 if (first_ac) {433
205 state->prev_1st_ac_level = prev_level;434 ent = &lut->run_first_lut[k_run][k_level][lut_bits];
206 first_ac = 0;435
436 if (ent->count == 0) {
437 // One long code.
438 uint32_t bits, low_bits;
439 unsigned int leading_zeroes, low_bit_count, low_bit_shift;
440 int run;
441
442 // Remove the prefix bits.
443 bits = next_bits & 0xffff;
444 // Determine code length.
445 leading_zeroes = 15 - av_log2(bits);
446 // Extract the low bits.
447 low_bit_count = leading_zeroes + k_run;
448 low_bit_shift = 16 - (1 + 2 * leading_zeroes + k_run);
449 low_bits = (bits >> low_bit_shift) & ((1 << low_bit_count) - 1);
450 // Construct run code.
451 run = (2 << k_run) +
452 ((1 << leading_zeroes) - 1) * (1 << k_run) +
453 low_bits;
454 // Skip over the bits just used.
455 skip_bits(gbc, 2 + leading_zeroes + 1 + low_bit_count);
456
457 scan_pos += run;
458 if (scan_pos >= 64)
459 goto end_of_block;
460 k_run = FFMIN(run >> 2, 2);
461 goto next_is_level;
462
463 } else {
464 // One or more short codes.
465
466 scan_pos += ent->run[0];
467 if (scan_pos >= 64) {
468 skip_bits(gbc, ent->offset[0]);
469 goto end_of_block;
470 }
471 if (ent->count > 1) {
472 coeff[ff_zigzag_direct[scan_pos]] = ent->level[0];
473 ++scan_pos;
474 if (scan_pos >= 64) {
475 skip_bits(gbc, ent->offset[1]);
476 goto end_of_block;
207 }477 }
208 478 }
479 if (ent->count > 2) {
480 scan_pos += ent->run[1];
481 if (scan_pos >= 64) {
482 skip_bits(gbc, ent->offset[2]);
483 goto end_of_block;
484 }
485 }
486 if (ent->count > 3) {
487 coeff[ff_zigzag_direct[scan_pos]] = ent->level[1];
209 ++scan_pos;488 ++scan_pos;
489 if (scan_pos >= 64) {
490 skip_bits(gbc, ent->offset[3]);
491 goto end_of_block;
492 }
210 }493 }
494 skip_bits(gbc, ent->offset[3]);
495 k_run = ent->k_run;
496 k_level = ent->k_level_1;
497 if (ent->count & 1)
498 goto next_is_level;
499 else
500 goto next_is_run;
501 }
502 }
211 503
212 } while (scan_pos < APV_BLK_COEFFS);504 next_is_level: {
505 next_bits = show_bits(gbc, 18);
506 lut_bits = next_bits >> (18 - APV_VLC_LUT_BITS);
507
508 ent = &lut->level_first_lut[k_run][k_level][lut_bits];
509
510 if (ent->count == 0) {
511 // One long code.
512 uint32_t bits;
513 unsigned int leading_zeroes;
514 int level, abs_level, sign;
515
516 // Remove the prefix bits.
517 bits = next_bits & 0xffff;
518 // Determine code length.
519 leading_zeroes = 15 - av_log2(bits);
520 // Skip the prefix and length bits.
521 skip_bits(gbc, 2 + leading_zeroes + 1);
522 // Read the rest of the code and construct the level.
523 // Include the + 1 offset for nonzero value here.
524 abs_level = (2 << k_level) +
525 ((1 << leading_zeroes) - 1) * (1 << k_level) +
526 get_bits(gbc, leading_zeroes + k_level) + 1;
527
528 sign = get_bits(gbc, 1);
529 if (sign)
530 level = -abs_level;
531 else
532 level = abs_level;
533
534 // Check range (not checked in any other case, only a long
535 // code can be out of range).
536 if (level < APV_MIN_TRANS_COEFF ||
537 level > APV_MAX_TRANS_COEFF) {
538 av_log(state->log_ctx, AV_LOG_ERROR,
539 "Out-of-range AC coefficient value at %d: %d.\n",
540 scan_pos, level);
541 return AVERROR_INVALIDDATA;
542 }
543 coeff[ff_zigzag_direct[scan_pos]] = level;
544 ++scan_pos;
545 k_level = FFMIN(abs_level >> 2, 4);
546 if (scan_pos >= 64)
547 goto end_of_block;
548 goto next_is_run;
549
550 } else {
551 // One or more short codes.
552
553 coeff[ff_zigzag_direct[scan_pos]] = ent->level[0];
554 ++scan_pos;
555 if (scan_pos >= 64) {
556 skip_bits(gbc, ent->offset[0]);
557 goto end_of_block;
558 }
559 if (ent->count > 1) {
560 scan_pos += ent->run[0];
561 if (scan_pos >= 64) {
562 skip_bits(gbc, ent->offset[1]);
563 goto end_of_block;
564 }
565 }
566 if (ent->count > 2) {
567 coeff[ff_zigzag_direct[scan_pos]] = ent->level[1];
568 ++scan_pos;
569 if (scan_pos >= 64) {
570 skip_bits(gbc, ent->offset[2]);
571 goto end_of_block;
572 }
573 }
574 if (ent->count > 3) {
575 scan_pos += ent->run[1];
576 if (scan_pos >= 64) {
577 skip_bits(gbc, ent->offset[3]);
578 goto end_of_block;
579 }
580 }
581 skip_bits(gbc, ent->offset[3]);
582 k_run = ent->k_run;
583 k_level = ent->k_level_1;
584 if (ent->count & 1)
585 goto next_is_run;
586 else
587 goto next_is_level;
588 }
213 }589 }
214 590
215 return 0;591 end_of_block: {
592 if (scan_pos > 64) {
593 av_log(state->log_ctx, AV_LOG_ERROR,
594 "Block decode reached invalid scan position %d.\n",
595 scan_pos);
596 return AVERROR_INVALIDDATA;
597 }
598 return 0;
599 }
216}600}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t