| Line | Branch | Exec | Source |
|---|---|---|---|
| 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 | |||
| 25 | av_always_inline | ||
| 26 | 6676543 | static 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 | 6676543 | next_bits = show_bits(gbc, APV_VLC_LUT_BITS); | |
| 33 | 6676543 | ent = &lut->single_lut[k_param][next_bits]; | |
| 34 | |||
| 35 |
2/2✓ Branch 0 taken 368880 times.
✓ Branch 1 taken 6307663 times.
|
6676543 | if (ent->more) { |
| 36 | unsigned int leading_zeroes; | ||
| 37 | |||
| 38 | 368880 | skip_bits(gbc, ent->consume); | |
| 39 | |||
| 40 | 368880 | next_bits = show_bits(gbc, 16); | |
| 41 | 368880 | leading_zeroes = 15 - av_log2(next_bits); | |
| 42 | |||
| 43 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 368880 times.
|
368880 | 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 | 368880 | skip_bits(gbc, leading_zeroes + 1); | |
| 52 | |||
| 53 | 368880 | return (2 << k_param) + | |
| 54 | 368880 | ((1 << leading_zeroes) - 1) * (1 << k_param) + | |
| 55 | 368880 | get_bits(gbc, leading_zeroes + k_param); | |
| 56 | } else { | ||
| 57 | 6307663 | skip_bits(gbc, ent->consume); | |
| 58 | 6307663 | return ent->result; | |
| 59 | } | ||
| 60 | } | ||
| 61 | |||
| 62 | 8 | void ff_apv_entropy_build_decode_lut(APVVLCLUT *decode_lut) | |
| 63 | { | ||
| 64 | 8 | const int code_len = APV_VLC_LUT_BITS; | |
| 65 | 8 | const int lut_size = APV_VLC_LUT_SIZE; | |
| 66 | |||
| 67 | // Build the single-symbol VLC table. | ||
| 68 |
2/2✓ Branch 0 taken 48 times.
✓ Branch 1 taken 8 times.
|
56 | for (int k = 0; k <= 5; k++) { |
| 69 |
2/2✓ Branch 0 taken 24576 times.
✓ Branch 1 taken 48 times.
|
24624 | for (unsigned int code = 0; code < lut_size; code++) { |
| 70 | 24576 | APVSingleVLCLUTEntry *ent = &decode_lut->single_lut[k][code]; | |
| 71 | 24576 | unsigned int first_bit = code & (1 << code_len - 1); | |
| 72 | 24576 | unsigned int remaining_bits = code ^ first_bit; | |
| 73 | |||
| 74 |
2/2✓ Branch 0 taken 12288 times.
✓ Branch 1 taken 12288 times.
|
24576 | if (first_bit) { |
| 75 | 12288 | ent->consume = 1 + k; | |
| 76 | 12288 | ent->result = remaining_bits >> (code_len - k - 1); | |
| 77 | 12288 | ent->more = 0; | |
| 78 | } else { | ||
| 79 | 12288 | unsigned int second_bit = code & (1 << code_len - 2); | |
| 80 | 12288 | remaining_bits ^= second_bit; | |
| 81 | |||
| 82 |
2/2✓ Branch 0 taken 6144 times.
✓ Branch 1 taken 6144 times.
|
12288 | if (second_bit) { |
| 83 | 6144 | unsigned int bits_left = code_len - 2; | |
| 84 | 6144 | unsigned int first_set = bits_left - av_log2(remaining_bits); | |
| 85 | 6144 | unsigned int last_bits = first_set - 1 + k; | |
| 86 | |||
| 87 |
2/2✓ Branch 0 taken 4800 times.
✓ Branch 1 taken 1344 times.
|
6144 | if (first_set + last_bits <= bits_left) { |
| 88 | // Whole code fits here. | ||
| 89 | 4800 | ent->consume = 2 + first_set + last_bits; | |
| 90 | 4800 | ent->result = ((2 << k) + | |
| 91 | 4800 | (((1 << first_set - 1) - 1) << k) + | |
| 92 | 4800 | ((code >> bits_left - first_set - last_bits) & (1 << last_bits) - 1)); | |
| 93 | 4800 | ent->more = 0; | |
| 94 | } else { | ||
| 95 | // Need to read more, collapse to default. | ||
| 96 | 1344 | ent->consume = 2; | |
| 97 | 1344 | ent->more = 1; | |
| 98 | } | ||
| 99 | } else { | ||
| 100 | 6144 | ent->consume = 2 + k; | |
| 101 | 6144 | ent->result = (1 << k) + (remaining_bits >> (code_len - k - 2)); | |
| 102 | 6144 | ent->more = 0; | |
| 103 | } | ||
| 104 | } | ||
| 105 | } | ||
| 106 | } | ||
| 107 | |||
| 108 | // Build the multi-symbol VLC table. | ||
| 109 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 8 times.
|
32 | for (int start_run = 0; start_run <= 2; start_run++) { |
| 110 |
2/2✓ Branch 0 taken 120 times.
✓ Branch 1 taken 24 times.
|
144 | for (int start_level = 0; start_level <= 4; start_level++) { |
| 111 |
2/2✓ Branch 0 taken 61440 times.
✓ Branch 1 taken 120 times.
|
61560 | for (unsigned int code = 0; code < lut_size; code++) { |
| 112 | APVMultiVLCLUTEntry *ent; | ||
| 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 | 61440 | memset(buffer, 0, sizeof(buffer)); | |
| 121 | 61440 | init_put_bits(&pbc, buffer, sizeof(buffer)); | |
| 122 | 61440 | put_bits(&pbc, APV_VLC_LUT_BITS, code); | |
| 123 | 61440 | flush_put_bits(&pbc); | |
| 124 | |||
| 125 | 61440 | memcpy(run_first_buffer, buffer, sizeof(buffer)); | |
| 126 | 61440 | memcpy(level_first_buffer, buffer, sizeof(buffer)); | |
| 127 | |||
| 128 | 61440 | k_run = start_run; | |
| 129 | 61440 | k_level = start_level; | |
| 130 | |||
| 131 | 61440 | ent = &decode_lut->run_first_lut[k_run][k_level][code]; | |
| 132 | 61440 | memset(ent, 0, sizeof(*ent)); | |
| 133 | 61440 | init_get_bits8(&gbc, run_first_buffer, sizeof(run_first_buffer)); | |
| 134 | |||
| 135 | 61440 | ent->count = 0; | |
| 136 |
2/2✓ Branch 0 taken 105152 times.
✓ Branch 1 taken 6944 times.
|
112096 | for (int i = 0; i <= 1; i++) { |
| 137 | int value, sign, pos; | ||
| 138 | |||
| 139 | 105152 | value = apv_read_vlc(&gbc, k_run, decode_lut); | |
| 140 | 105152 | pos = get_bits_count(&gbc); | |
| 141 |
2/2✓ Branch 0 taken 19504 times.
✓ Branch 1 taken 85648 times.
|
105152 | if (pos > APV_VLC_LUT_BITS) |
| 142 | 19504 | break; | |
| 143 | 85648 | ent->run[i] = value; | |
| 144 | 85648 | ent->offset[ent->count] = pos; | |
| 145 | 85648 | ++ent->count; | |
| 146 | 85648 | k_run = FFMIN(value >> 2, 2); | |
| 147 | |||
| 148 | 85648 | value = apv_read_vlc(&gbc, k_level, decode_lut); | |
| 149 | 85648 | sign = get_bits1(&gbc); | |
| 150 | 85648 | pos = get_bits_count(&gbc); | |
| 151 |
2/2✓ Branch 0 taken 34992 times.
✓ Branch 1 taken 50656 times.
|
85648 | if (pos > APV_VLC_LUT_BITS) |
| 152 | 34992 | break; | |
| 153 | 50656 | ++value; | |
| 154 |
2/2✓ Branch 0 taken 25328 times.
✓ Branch 1 taken 25328 times.
|
50656 | ent->level[i] = sign ? -value : value; |
| 155 | 50656 | ent->offset[ent->count] = pos; | |
| 156 | 50656 | ++ent->count; | |
| 157 | 50656 | k_level = FFMIN(value >> 2, 4); | |
| 158 |
2/2✓ Branch 0 taken 43712 times.
✓ Branch 1 taken 6944 times.
|
50656 | if (i == 0) |
| 159 | 43712 | ent->k_level_0 = k_level; | |
| 160 | } | ||
| 161 |
4/4✓ Branch 0 taken 59840 times.
✓ Branch 1 taken 1600 times.
✓ Branch 2 taken 52896 times.
✓ Branch 3 taken 6944 times.
|
61440 | if (ent->count > 0 && ent->count < 4) |
| 162 | 52896 | ent->offset[3] = ent->offset[ent->count - 1]; | |
| 163 | 61440 | ent->k_run = k_run; | |
| 164 | 61440 | ent->k_level_1 = k_level; | |
| 165 | |||
| 166 | 61440 | k_run = start_run; | |
| 167 | 61440 | k_level = start_level; | |
| 168 | |||
| 169 | 61440 | ent = &decode_lut->level_first_lut[k_run][k_level][code]; | |
| 170 | 61440 | memset(ent, 0, sizeof(*ent)); | |
| 171 | 61440 | init_get_bits8(&gbc, level_first_buffer, sizeof(level_first_buffer)); | |
| 172 | |||
| 173 | 61440 | ent->count = 0; | |
| 174 |
2/2✓ Branch 0 taken 105152 times.
✓ Branch 1 taken 6944 times.
|
112096 | for (int i = 0; i <= 1; i++) { |
| 175 | int value, sign, pos; | ||
| 176 | |||
| 177 | 105152 | value = apv_read_vlc(&gbc, k_level, decode_lut); | |
| 178 | 105152 | sign = get_bits1(&gbc); | |
| 179 | 105152 | pos = get_bits_count(&gbc); | |
| 180 |
2/2✓ Branch 0 taken 31264 times.
✓ Branch 1 taken 73888 times.
|
105152 | if (pos > APV_VLC_LUT_BITS) |
| 181 | 31264 | break; | |
| 182 | 73888 | ++value; | |
| 183 |
2/2✓ Branch 0 taken 36944 times.
✓ Branch 1 taken 36944 times.
|
73888 | ent->level[i] = sign ? -value : value; |
| 184 | 73888 | ent->offset[ent->count] = pos; | |
| 185 | 73888 | ++ent->count; | |
| 186 | 73888 | k_level = FFMIN(value >> 2, 4); | |
| 187 |
2/2✓ Branch 0 taken 57600 times.
✓ Branch 1 taken 16288 times.
|
73888 | if (i == 0) |
| 188 | 57600 | ent->k_level_0 = k_level; | |
| 189 | |||
| 190 | 73888 | value = apv_read_vlc(&gbc, k_run, decode_lut); | |
| 191 | 73888 | pos = get_bits_count(&gbc); | |
| 192 |
2/2✓ Branch 0 taken 23232 times.
✓ Branch 1 taken 50656 times.
|
73888 | if (pos > APV_VLC_LUT_BITS) |
| 193 | 23232 | break; | |
| 194 | 50656 | ent->run[i] = value; | |
| 195 | 50656 | ent->offset[ent->count] = pos; | |
| 196 | 50656 | ++ent->count; | |
| 197 | 50656 | k_run = FFMIN(value >> 2, 2); | |
| 198 | } | ||
| 199 |
4/4✓ Branch 0 taken 57600 times.
✓ Branch 1 taken 3840 times.
✓ Branch 2 taken 50656 times.
✓ Branch 3 taken 6944 times.
|
61440 | if (ent->count > 0 && ent->count < 4) |
| 200 | 50656 | ent->offset[3] = ent->offset[ent->count - 1]; | |
| 201 | 61440 | ent->k_run = k_run; | |
| 202 | 61440 | ent->k_level_1 = k_level; | |
| 203 | } | ||
| 204 | } | ||
| 205 | } | ||
| 206 | 8 | } | |
| 207 | |||
| 208 | 8740 | int ff_apv_entropy_decode_block(int16_t *restrict coeff, | |
| 209 | GetBitContext *restrict gbc, | ||
| 210 | APVEntropyState *restrict state) | ||
| 211 | { | ||
| 212 | 8740 | const APVVLCLUT *lut = state->decode_lut; | |
| 213 | int scan_pos; | ||
| 214 | 8740 | 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 | 8740 | abs_diff = apv_read_vlc(gbc, k_dc, lut); | |
| 225 | |||
| 226 |
2/2✓ Branch 0 taken 8230 times.
✓ Branch 1 taken 510 times.
|
8740 | if (abs_diff) { |
| 227 | 8230 | sign = get_bits1(gbc); | |
| 228 |
2/2✓ Branch 0 taken 4180 times.
✓ Branch 1 taken 4050 times.
|
8230 | if (sign) |
| 229 | 4180 | dc_coeff = state->prev_dc - abs_diff; | |
| 230 | else | ||
| 231 | 4050 | dc_coeff = state->prev_dc + abs_diff; | |
| 232 | } else { | ||
| 233 | 510 | dc_coeff = state->prev_dc; | |
| 234 | } | ||
| 235 | |||
| 236 | |||
| 237 |
2/4✓ Branch 0 taken 8740 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 8740 times.
|
8740 | 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 | 8740 | coeff[0] = dc_coeff; | |
| 246 | |||
| 247 | 8740 | state->prev_dc = dc_coeff; | |
| 248 | 8740 | 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 | 8740 | k_level = state->prev_k_level; | |
| 265 | { | ||
| 266 | 8740 | next_bits = show_bits(gbc, 18); | |
| 267 | 8740 | lut_bits = next_bits >> (18 - APV_VLC_LUT_BITS); | |
| 268 | |||
| 269 | 8740 | ent = &lut->run_first_lut[0][k_level][lut_bits]; | |
| 270 | |||
| 271 |
2/2✓ Branch 0 taken 361 times.
✓ Branch 1 taken 8379 times.
|
8740 | 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 | 361 | bits = next_bits & 0xffff; | |
| 279 | // Determine code length. | ||
| 280 | 361 | leading_zeroes = 15 - av_log2(bits); | |
| 281 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 361 times.
|
361 | 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 | 361 | low_bit_count = leading_zeroes; | |
| 290 | 361 | low_bit_shift = 16 - (1 + 2 * leading_zeroes); | |
| 291 | 361 | low_bits = av_zero_extend(bits >> low_bit_shift, low_bit_count); | |
| 292 | // Construct run code. | ||
| 293 | 361 | run = 2 + ((1 << leading_zeroes) - 1) + low_bits; | |
| 294 | // Skip over the bits just used. | ||
| 295 | 361 | skip_bits(gbc, 2 + leading_zeroes + 1 + low_bit_count); | |
| 296 | |||
| 297 | 361 | scan_pos = run + 1; | |
| 298 |
2/2✓ Branch 0 taken 354 times.
✓ Branch 1 taken 7 times.
|
361 | if (scan_pos >= 64) |
| 299 | 354 | goto end_of_block; | |
| 300 | 7 | k_run = FFMIN(run >> 2, 2); | |
| 301 | 7 | 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 | 8379 | scan_pos = ent->run[0] + 1; | |
| 308 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8379 times.
|
8379 | if (scan_pos >= 64) { |
| 309 | ✗ | skip_bits(gbc, ent->offset[0]); | |
| 310 | ✗ | goto end_of_block; | |
| 311 | } | ||
| 312 |
2/2✓ Branch 0 taken 6848 times.
✓ Branch 1 taken 1531 times.
|
8379 | if (ent->count > 1) { |
| 313 | 6848 | coeff[ff_zigzag_direct[scan_pos]] = ent->level[0]; | |
| 314 | 6848 | ++scan_pos; | |
| 315 | 6848 | state->prev_k_level = ent->k_level_0; | |
| 316 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6848 times.
|
6848 | if (scan_pos >= 64) { |
| 317 | ✗ | skip_bits(gbc, ent->offset[1]); | |
| 318 | ✗ | goto end_of_block; | |
| 319 | } | ||
| 320 | } | ||
| 321 |
2/2✓ Branch 0 taken 5394 times.
✓ Branch 1 taken 2985 times.
|
8379 | if (ent->count > 2) { |
| 322 | 5394 | scan_pos += ent->run[1]; | |
| 323 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5394 times.
|
5394 | if (scan_pos >= 64) { |
| 324 | ✗ | skip_bits(gbc, ent->offset[2]); | |
| 325 | ✗ | goto end_of_block; | |
| 326 | } | ||
| 327 | } | ||
| 328 |
2/2✓ Branch 0 taken 1152 times.
✓ Branch 1 taken 7227 times.
|
8379 | if (ent->count > 3) { |
| 329 | 1152 | coeff[ff_zigzag_direct[scan_pos]] = ent->level[1]; | |
| 330 | 1152 | ++scan_pos; | |
| 331 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1152 times.
|
1152 | if (scan_pos >= 64) { |
| 332 | ✗ | skip_bits(gbc, ent->offset[3]); | |
| 333 | ✗ | goto end_of_block; | |
| 334 | } | ||
| 335 | } | ||
| 336 | 8379 | skip_bits(gbc, ent->offset[3]); | |
| 337 | 8379 | k_run = ent->k_run; | |
| 338 | 8379 | k_level = ent->k_level_1; | |
| 339 |
2/2✓ Branch 0 taken 1531 times.
✓ Branch 1 taken 6848 times.
|
8379 | if (ent->count == 1) |
| 340 | 1531 | goto first_level; | |
| 341 |
2/2✓ Branch 0 taken 4242 times.
✓ Branch 1 taken 2606 times.
|
6848 | else if (ent->count & 1) |
| 342 | 4242 | goto next_is_level; | |
| 343 | else | ||
| 344 | 2606 | goto next_is_run; | |
| 345 | } | ||
| 346 | } | ||
| 347 | |||
| 348 | 1538 | first_level: { | |
| 349 | 1538 | next_bits = show_bits(gbc, 18); | |
| 350 | 1538 | lut_bits = next_bits >> (18 - APV_VLC_LUT_BITS); | |
| 351 | |||
| 352 | 1538 | ent = &lut->level_first_lut[k_run][k_level][lut_bits]; | |
| 353 | |||
| 354 |
2/2✓ Branch 0 taken 1098 times.
✓ Branch 1 taken 440 times.
|
1538 | 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 | 1098 | bits = next_bits & 0xffff; | |
| 362 | // Determine code length. | ||
| 363 | 1098 | leading_zeroes = 15 - av_log2(bits); | |
| 364 | // Skip the prefix and length bits. | ||
| 365 | 1098 | 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 | 2196 | abs_level = (2 << k_level) + | |
| 369 | 2196 | ((1 << leading_zeroes) - 1) * (1 << k_level) + | |
| 370 | 1098 | get_bits(gbc, leading_zeroes + k_level) + 1; | |
| 371 | |||
| 372 | 1098 | sign = get_bits(gbc, 1); | |
| 373 |
2/2✓ Branch 0 taken 517 times.
✓ Branch 1 taken 581 times.
|
1098 | if (sign) |
| 374 | 517 | level = -abs_level; | |
| 375 | else | ||
| 376 | 581 | level = abs_level; | |
| 377 | |||
| 378 | // Check range (not checked in any other case, only a long | ||
| 379 | // code can be out of range). | ||
| 380 |
2/4✓ Branch 0 taken 1098 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1098 times.
|
1098 | if (level < APV_MIN_TRANS_COEFF || |
| 381 | level > APV_MAX_TRANS_COEFF) { | ||
| 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 | 1098 | coeff[ff_zigzag_direct[scan_pos]] = level; | |
| 388 | 1098 | ++scan_pos; | |
| 389 | 1098 | k_level = FFMIN(abs_level >> 2, 4); | |
| 390 | 1098 | state->prev_k_level = k_level; | |
| 391 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1098 times.
|
1098 | if (scan_pos >= 64) |
| 392 | ✗ | goto end_of_block; | |
| 393 | 1098 | goto next_is_run; | |
| 394 | |||
| 395 | } else { | ||
| 396 | // One or more short codes. | ||
| 397 | |||
| 398 | 440 | coeff[ff_zigzag_direct[scan_pos]] = ent->level[0]; | |
| 399 | 440 | ++scan_pos; | |
| 400 | 440 | state->prev_k_level = ent->k_level_0; | |
| 401 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 440 times.
|
440 | if (scan_pos >= 64) { |
| 402 | ✗ | skip_bits(gbc, ent->offset[0]); | |
| 403 | ✗ | goto end_of_block; | |
| 404 | } | ||
| 405 |
2/2✓ Branch 0 taken 52 times.
✓ Branch 1 taken 388 times.
|
440 | if (ent->count > 1) { |
| 406 | 52 | scan_pos += ent->run[0]; | |
| 407 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 52 times.
|
52 | if (scan_pos >= 64) { |
| 408 | ✗ | skip_bits(gbc, ent->offset[1]); | |
| 409 | ✗ | goto end_of_block; | |
| 410 | } | ||
| 411 | } | ||
| 412 |
2/2✓ Branch 0 taken 11 times.
✓ Branch 1 taken 429 times.
|
440 | if (ent->count > 2) { |
| 413 | 11 | coeff[ff_zigzag_direct[scan_pos]] = ent->level[1]; | |
| 414 | 11 | ++scan_pos; | |
| 415 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
|
11 | if (scan_pos >= 64) { |
| 416 | ✗ | skip_bits(gbc, ent->offset[2]); | |
| 417 | ✗ | goto end_of_block; | |
| 418 | } | ||
| 419 | } | ||
| 420 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 439 times.
|
440 | if (ent->count > 3) { |
| 421 | 1 | scan_pos += ent->run[1]; | |
| 422 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (scan_pos >= 64) { |
| 423 | ✗ | skip_bits(gbc, ent->offset[3]); | |
| 424 | ✗ | goto end_of_block; | |
| 425 | } | ||
| 426 | } | ||
| 427 | 440 | skip_bits(gbc, ent->offset[3]); | |
| 428 | 440 | k_run = ent->k_run; | |
| 429 | 440 | k_level = ent->k_level_1; | |
| 430 |
2/2✓ Branch 0 taken 398 times.
✓ Branch 1 taken 42 times.
|
440 | if (ent->count & 1) |
| 431 | 398 | goto next_is_run; | |
| 432 | else | ||
| 433 | 42 | goto next_is_level; | |
| 434 | } | ||
| 435 | } | ||
| 436 | |||
| 437 | 34957 | next_is_run: { | |
| 438 | 65160 | next_bits = show_bits(gbc, 18); | |
| 439 | 65160 | lut_bits = next_bits >> (18 - APV_VLC_LUT_BITS); | |
| 440 | |||
| 441 | 65160 | ent = &lut->run_first_lut[k_run][k_level][lut_bits]; | |
| 442 | |||
| 443 |
2/2✓ Branch 0 taken 2326 times.
✓ Branch 1 taken 62834 times.
|
65160 | 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 | 2326 | bits = next_bits & 0xffff; | |
| 451 | // Determine code length. | ||
| 452 | 2326 | leading_zeroes = 15 - av_log2(bits); | |
| 453 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2326 times.
|
2326 | 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 | 2326 | low_bit_count = leading_zeroes + k_run; | |
| 462 | 2326 | low_bit_shift = 16 - (1 + 2 * leading_zeroes + k_run); | |
| 463 | 2326 | low_bits = av_zero_extend(bits >> low_bit_shift, low_bit_count); | |
| 464 | // Construct run code. | ||
| 465 | 2326 | run = (2 << k_run) + | |
| 466 | 2326 | ((1 << leading_zeroes) - 1) * (1 << k_run) + | |
| 467 | low_bits; | ||
| 468 | // Skip over the bits just used. | ||
| 469 | 2326 | skip_bits(gbc, 2 + leading_zeroes + 1 + low_bit_count); | |
| 470 | |||
| 471 | 2326 | scan_pos += run; | |
| 472 |
2/2✓ Branch 0 taken 2162 times.
✓ Branch 1 taken 164 times.
|
2326 | if (scan_pos >= 64) |
| 473 | 2162 | goto end_of_block; | |
| 474 | 164 | k_run = FFMIN(run >> 2, 2); | |
| 475 | 164 | goto next_is_level; | |
| 476 | |||
| 477 | } else { | ||
| 478 | // One or more short codes. | ||
| 479 | |||
| 480 | 62834 | scan_pos += ent->run[0]; | |
| 481 |
2/2✓ Branch 0 taken 2012 times.
✓ Branch 1 taken 60822 times.
|
62834 | if (scan_pos >= 64) { |
| 482 | 2012 | skip_bits(gbc, ent->offset[0]); | |
| 483 | 2012 | goto end_of_block; | |
| 484 | } | ||
| 485 |
2/2✓ Branch 0 taken 55877 times.
✓ Branch 1 taken 4945 times.
|
60822 | if (ent->count > 1) { |
| 486 | 55877 | coeff[ff_zigzag_direct[scan_pos]] = ent->level[0]; | |
| 487 | 55877 | ++scan_pos; | |
| 488 |
2/2✓ Branch 0 taken 560 times.
✓ Branch 1 taken 55317 times.
|
55877 | if (scan_pos >= 64) { |
| 489 | 560 | skip_bits(gbc, ent->offset[1]); | |
| 490 | 560 | goto end_of_block; | |
| 491 | } | ||
| 492 | } | ||
| 493 |
2/2✓ Branch 0 taken 46830 times.
✓ Branch 1 taken 13432 times.
|
60262 | if (ent->count > 2) { |
| 494 | 46830 | scan_pos += ent->run[1]; | |
| 495 |
2/2✓ Branch 0 taken 433 times.
✓ Branch 1 taken 46397 times.
|
46830 | if (scan_pos >= 64) { |
| 496 | 433 | skip_bits(gbc, ent->offset[2]); | |
| 497 | 433 | goto end_of_block; | |
| 498 | } | ||
| 499 | } | ||
| 500 |
2/2✓ Branch 0 taken 22089 times.
✓ Branch 1 taken 37740 times.
|
59829 | if (ent->count > 3) { |
| 501 | 22089 | coeff[ff_zigzag_direct[scan_pos]] = ent->level[1]; | |
| 502 | 22089 | ++scan_pos; | |
| 503 |
2/2✓ Branch 0 taken 373 times.
✓ Branch 1 taken 21716 times.
|
22089 | if (scan_pos >= 64) { |
| 504 | 373 | skip_bits(gbc, ent->offset[3]); | |
| 505 | 373 | goto end_of_block; | |
| 506 | } | ||
| 507 | } | ||
| 508 | 59456 | skip_bits(gbc, ent->offset[3]); | |
| 509 | 59456 | k_run = ent->k_run; | |
| 510 | 59456 | k_level = ent->k_level_1; | |
| 511 |
2/2✓ Branch 0 taken 29253 times.
✓ Branch 1 taken 30203 times.
|
59456 | if (ent->count & 1) |
| 512 | 29253 | goto next_is_level; | |
| 513 | else | ||
| 514 | 30203 | goto next_is_run; | |
| 515 | } | ||
| 516 | } | ||
| 517 | |||
| 518 | 33701 | next_is_level: { | |
| 519 | 137188 | next_bits = show_bits(gbc, 18); | |
| 520 | 137188 | lut_bits = next_bits >> (18 - APV_VLC_LUT_BITS); | |
| 521 | |||
| 522 | 137188 | ent = &lut->level_first_lut[k_run][k_level][lut_bits]; | |
| 523 | |||
| 524 |
2/2✓ Branch 0 taken 7416 times.
✓ Branch 1 taken 129772 times.
|
137188 | 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 | 7416 | bits = next_bits & 0xffff; | |
| 532 | // Determine code length. | ||
| 533 | 7416 | leading_zeroes = 15 - av_log2(bits); | |
| 534 | // Skip the prefix and length bits. | ||
| 535 | 7416 | 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 | 14832 | abs_level = (2 << k_level) + | |
| 539 | 14832 | ((1 << leading_zeroes) - 1) * (1 << k_level) + | |
| 540 | 7416 | get_bits(gbc, leading_zeroes + k_level) + 1; | |
| 541 | |||
| 542 | 7416 | sign = get_bits(gbc, 1); | |
| 543 |
2/2✓ Branch 0 taken 3719 times.
✓ Branch 1 taken 3697 times.
|
7416 | if (sign) |
| 544 | 3719 | level = -abs_level; | |
| 545 | else | ||
| 546 | 3697 | level = abs_level; | |
| 547 | |||
| 548 | // Check range (not checked in any other case, only a long | ||
| 549 | // code can be out of range). | ||
| 550 |
2/4✓ Branch 0 taken 7416 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 7416 times.
|
7416 | if (level < APV_MIN_TRANS_COEFF || |
| 551 | level > APV_MAX_TRANS_COEFF) { | ||
| 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 | 7416 | coeff[ff_zigzag_direct[scan_pos]] = level; | |
| 558 | 7416 | ++scan_pos; | |
| 559 | 7416 | k_level = FFMIN(abs_level >> 2, 4); | |
| 560 |
2/2✓ Branch 0 taken 29 times.
✓ Branch 1 taken 7387 times.
|
7416 | if (scan_pos >= 64) |
| 561 | 29 | goto end_of_block; | |
| 562 | 7387 | goto next_is_run; | |
| 563 | |||
| 564 | } else { | ||
| 565 | // One or more short codes. | ||
| 566 | |||
| 567 | 129772 | coeff[ff_zigzag_direct[scan_pos]] = ent->level[0]; | |
| 568 | 129772 | ++scan_pos; | |
| 569 |
2/2✓ Branch 0 taken 722 times.
✓ Branch 1 taken 129050 times.
|
129772 | if (scan_pos >= 64) { |
| 570 | 722 | skip_bits(gbc, ent->offset[0]); | |
| 571 | 722 | goto end_of_block; | |
| 572 | } | ||
| 573 |
2/2✓ Branch 0 taken 123115 times.
✓ Branch 1 taken 5935 times.
|
129050 | if (ent->count > 1) { |
| 574 | 123115 | scan_pos += ent->run[0]; | |
| 575 |
2/2✓ Branch 0 taken 1006 times.
✓ Branch 1 taken 122109 times.
|
123115 | if (scan_pos >= 64) { |
| 576 | 1006 | skip_bits(gbc, ent->offset[1]); | |
| 577 | 1006 | goto end_of_block; | |
| 578 | } | ||
| 579 | } | ||
| 580 |
2/2✓ Branch 0 taken 60350 times.
✓ Branch 1 taken 67694 times.
|
128044 | if (ent->count > 2) { |
| 581 | 60350 | coeff[ff_zigzag_direct[scan_pos]] = ent->level[1]; | |
| 582 | 60350 | ++scan_pos; | |
| 583 |
2/2✓ Branch 0 taken 714 times.
✓ Branch 1 taken 59636 times.
|
60350 | if (scan_pos >= 64) { |
| 584 | 714 | skip_bits(gbc, ent->offset[2]); | |
| 585 | 714 | goto end_of_block; | |
| 586 | } | ||
| 587 | } | ||
| 588 |
2/2✓ Branch 0 taken 42103 times.
✓ Branch 1 taken 85227 times.
|
127330 | if (ent->count > 3) { |
| 589 | 42103 | scan_pos += ent->run[1]; | |
| 590 |
2/2✓ Branch 0 taken 375 times.
✓ Branch 1 taken 41728 times.
|
42103 | if (scan_pos >= 64) { |
| 591 | 375 | skip_bits(gbc, ent->offset[3]); | |
| 592 | 375 | goto end_of_block; | |
| 593 | } | ||
| 594 | } | ||
| 595 | 126955 | skip_bits(gbc, ent->offset[3]); | |
| 596 | 126955 | k_run = ent->k_run; | |
| 597 | 126955 | k_level = ent->k_level_1; | |
| 598 |
2/2✓ Branch 0 taken 23468 times.
✓ Branch 1 taken 103487 times.
|
126955 | if (ent->count & 1) |
| 599 | 23468 | goto next_is_run; | |
| 600 | else | ||
| 601 | 103487 | goto next_is_level; | |
| 602 | } | ||
| 603 | } | ||
| 604 | |||
| 605 | 8740 | end_of_block: { | |
| 606 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8740 times.
|
8740 | 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 | 8740 | return 0; | |
| 613 | } | ||
| 614 | } | ||
| 615 |