| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /** | ||
| 2 | * @file | ||
| 3 | * Vorbis I decoder | ||
| 4 | * @author Denes Balatoni ( dbalatoni programozo hu ) | ||
| 5 | * | ||
| 6 | * This file is part of FFmpeg. | ||
| 7 | * | ||
| 8 | * FFmpeg is free software; you can redistribute it and/or | ||
| 9 | * modify it under the terms of the GNU Lesser General Public | ||
| 10 | * License as published by the Free Software Foundation; either | ||
| 11 | * version 2.1 of the License, or (at your option) any later version. | ||
| 12 | * | ||
| 13 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 16 | * Lesser General Public License for more details. | ||
| 17 | * | ||
| 18 | * You should have received a copy of the GNU Lesser General Public | ||
| 19 | * License along with FFmpeg; if not, write to the Free Software | ||
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 21 | */ | ||
| 22 | |||
| 23 | /** | ||
| 24 | * @file | ||
| 25 | * Vorbis I decoder | ||
| 26 | * @author Denes Balatoni ( dbalatoni programozo hu ) | ||
| 27 | */ | ||
| 28 | |||
| 29 | #include <inttypes.h> | ||
| 30 | #include <math.h> | ||
| 31 | |||
| 32 | #include "libavutil/avassert.h" | ||
| 33 | #include "libavutil/float_dsp.h" | ||
| 34 | #include "libavutil/mem.h" | ||
| 35 | #include "libavutil/tx.h" | ||
| 36 | |||
| 37 | #define BITSTREAM_READER_LE | ||
| 38 | #include "avcodec.h" | ||
| 39 | #include "codec_internal.h" | ||
| 40 | #include "decode.h" | ||
| 41 | #include "get_bits.h" | ||
| 42 | #include "internal.h" | ||
| 43 | #include "vorbis.h" | ||
| 44 | #include "vorbisdsp.h" | ||
| 45 | #include "vorbis_data.h" | ||
| 46 | #include "xiph.h" | ||
| 47 | |||
| 48 | #define V_NB_BITS 8 | ||
| 49 | #define V_NB_BITS2 11 | ||
| 50 | #define V_MAX_VLCS (1 << 16) | ||
| 51 | #define V_MAX_PARTITIONS (1 << 20) | ||
| 52 | |||
| 53 | typedef struct vorbis_codebook { | ||
| 54 | uint8_t dimensions; | ||
| 55 | uint8_t lookup_type; | ||
| 56 | uint8_t maxdepth; | ||
| 57 | VLC vlc; | ||
| 58 | float *codevectors; | ||
| 59 | unsigned int nb_bits; | ||
| 60 | } vorbis_codebook; | ||
| 61 | |||
| 62 | typedef union vorbis_floor_u vorbis_floor_data; | ||
| 63 | typedef struct vorbis_floor0_s vorbis_floor0; | ||
| 64 | typedef struct vorbis_floor1_s vorbis_floor1; | ||
| 65 | struct vorbis_context_s; | ||
| 66 | typedef | ||
| 67 | int (* vorbis_floor_decode_func) | ||
| 68 | (struct vorbis_context_s *, vorbis_floor_data *, float *); | ||
| 69 | typedef struct vorbis_floor { | ||
| 70 | uint8_t floor_type; | ||
| 71 | vorbis_floor_decode_func decode; | ||
| 72 | union vorbis_floor_u { | ||
| 73 | struct vorbis_floor0_s { | ||
| 74 | uint8_t order; | ||
| 75 | uint16_t rate; | ||
| 76 | uint16_t bark_map_size; | ||
| 77 | int32_t *map[2]; | ||
| 78 | uint32_t map_size[2]; | ||
| 79 | uint8_t amplitude_bits; | ||
| 80 | uint8_t amplitude_offset; | ||
| 81 | uint8_t num_books; | ||
| 82 | uint8_t *book_list; | ||
| 83 | float *lsp; | ||
| 84 | } t0; | ||
| 85 | struct vorbis_floor1_s { | ||
| 86 | uint8_t partitions; | ||
| 87 | uint8_t partition_class[32]; | ||
| 88 | uint8_t class_dimensions[16]; | ||
| 89 | uint8_t class_subclasses[16]; | ||
| 90 | uint8_t class_masterbook[16]; | ||
| 91 | int16_t subclass_books[16][8]; | ||
| 92 | uint8_t multiplier; | ||
| 93 | uint16_t x_list_dim; | ||
| 94 | vorbis_floor1_entry *list; | ||
| 95 | } t1; | ||
| 96 | } data; | ||
| 97 | } vorbis_floor; | ||
| 98 | |||
| 99 | typedef struct vorbis_residue { | ||
| 100 | uint16_t type; | ||
| 101 | uint32_t begin; | ||
| 102 | uint32_t end; | ||
| 103 | unsigned partition_size; | ||
| 104 | uint8_t classifications; | ||
| 105 | uint8_t classbook; | ||
| 106 | int16_t books[64][8]; | ||
| 107 | uint8_t maxpass; | ||
| 108 | uint16_t ptns_to_read; | ||
| 109 | uint8_t *classifs; | ||
| 110 | } vorbis_residue; | ||
| 111 | |||
| 112 | typedef struct vorbis_mapping { | ||
| 113 | uint8_t submaps; | ||
| 114 | uint16_t coupling_steps; | ||
| 115 | uint8_t *magnitude; | ||
| 116 | uint8_t *angle; | ||
| 117 | uint8_t *mux; | ||
| 118 | uint8_t submap_floor[16]; | ||
| 119 | uint8_t submap_residue[16]; | ||
| 120 | } vorbis_mapping; | ||
| 121 | |||
| 122 | typedef struct vorbis_mode { | ||
| 123 | uint8_t blockflag; | ||
| 124 | uint16_t windowtype; | ||
| 125 | uint16_t transformtype; | ||
| 126 | uint8_t mapping; | ||
| 127 | } vorbis_mode; | ||
| 128 | |||
| 129 | typedef struct vorbis_context_s { | ||
| 130 | AVCodecContext *avctx; | ||
| 131 | GetBitContext gb; | ||
| 132 | VorbisDSPContext dsp; | ||
| 133 | AVFloatDSPContext *fdsp; | ||
| 134 | |||
| 135 | AVTXContext *mdct[2]; | ||
| 136 | av_tx_fn mdct_fn[2]; | ||
| 137 | |||
| 138 | uint8_t first_frame; | ||
| 139 | uint32_t version; | ||
| 140 | uint8_t audio_channels; | ||
| 141 | uint32_t audio_samplerate; | ||
| 142 | uint32_t bitrate_maximum; | ||
| 143 | uint32_t bitrate_nominal; | ||
| 144 | uint32_t bitrate_minimum; | ||
| 145 | uint32_t blocksize[2]; | ||
| 146 | const float *win[2]; | ||
| 147 | uint16_t codebook_count; | ||
| 148 | vorbis_codebook *codebooks; | ||
| 149 | uint8_t floor_count; | ||
| 150 | vorbis_floor *floors; | ||
| 151 | uint8_t residue_count; | ||
| 152 | vorbis_residue *residues; | ||
| 153 | uint8_t mapping_count; | ||
| 154 | vorbis_mapping *mappings; | ||
| 155 | uint8_t mode_count; | ||
| 156 | vorbis_mode *modes; | ||
| 157 | uint8_t mode_number; // mode number for the current packet | ||
| 158 | int8_t previous_window; | ||
| 159 | float *channel_residues; | ||
| 160 | float *saved; | ||
| 161 | } vorbis_context; | ||
| 162 | |||
| 163 | /* Helper functions */ | ||
| 164 | |||
| 165 | #define BARK(x) \ | ||
| 166 | (13.1f * atan(0.00074f * (x)) + 2.24f * atan(1.85e-8f * (x) * (x)) + 1e-4f * (x)) | ||
| 167 | |||
| 168 | static const char idx_err_str[] = "Index value %d out of range (0 - %d) for %s at %s:%i\n"; | ||
| 169 | #define VALIDATE_INDEX(idx, limit) \ | ||
| 170 | if (idx >= limit) {\ | ||
| 171 | av_log(vc->avctx, AV_LOG_ERROR,\ | ||
| 172 | idx_err_str,\ | ||
| 173 | (int)(idx), (int)(limit - 1), #idx, __FILE__, __LINE__);\ | ||
| 174 | return AVERROR_INVALIDDATA;\ | ||
| 175 | } | ||
| 176 | #define GET_VALIDATED_INDEX(idx, bits, limit) \ | ||
| 177 | {\ | ||
| 178 | idx = get_bits(gb, bits);\ | ||
| 179 | VALIDATE_INDEX(idx, limit)\ | ||
| 180 | } | ||
| 181 | |||
| 182 | 1900 | static float vorbisfloat2float(unsigned val) | |
| 183 | { | ||
| 184 | 1900 | float mant = val & 0x1fffff; | |
| 185 | 1900 | int exp = (val & 0x7fe00000) >> 21; | |
| 186 |
2/2✓ Branch 0 taken 882 times.
✓ Branch 1 taken 1018 times.
|
1900 | if (val & 0x80000000) |
| 187 | 882 | mant = -mant; | |
| 188 | 1900 | return ldexpf(mant, exp - 20 - 768); | |
| 189 | } | ||
| 190 | |||
| 191 | |||
| 192 | // Free all allocated memory ----------------------------------------- | ||
| 193 | |||
| 194 | 71 | static void vorbis_free(vorbis_context *vc) | |
| 195 | { | ||
| 196 | int i; | ||
| 197 | |||
| 198 | 71 | av_freep(&vc->channel_residues); | |
| 199 | 71 | av_freep(&vc->saved); | |
| 200 | 71 | av_freep(&vc->fdsp); | |
| 201 | |||
| 202 |
1/2✓ Branch 0 taken 71 times.
✗ Branch 1 not taken.
|
71 | if (vc->residues) |
| 203 |
2/2✓ Branch 0 taken 142 times.
✓ Branch 1 taken 71 times.
|
213 | for (i = 0; i < vc->residue_count; i++) |
| 204 | 142 | av_freep(&vc->residues[i].classifs); | |
| 205 | 71 | av_freep(&vc->residues); | |
| 206 | 71 | av_freep(&vc->modes); | |
| 207 | |||
| 208 | 71 | av_tx_uninit(&vc->mdct[0]); | |
| 209 | 71 | av_tx_uninit(&vc->mdct[1]); | |
| 210 | |||
| 211 |
1/2✓ Branch 0 taken 71 times.
✗ Branch 1 not taken.
|
71 | if (vc->codebooks) |
| 212 |
2/2✓ Branch 0 taken 2221 times.
✓ Branch 1 taken 71 times.
|
2292 | for (i = 0; i < vc->codebook_count; ++i) { |
| 213 | 2221 | av_freep(&vc->codebooks[i].codevectors); | |
| 214 | 2221 | ff_vlc_free(&vc->codebooks[i].vlc); | |
| 215 | } | ||
| 216 | 71 | av_freep(&vc->codebooks); | |
| 217 | |||
| 218 |
1/2✓ Branch 0 taken 71 times.
✗ Branch 1 not taken.
|
71 | if (vc->floors) |
| 219 |
2/2✓ Branch 0 taken 142 times.
✓ Branch 1 taken 71 times.
|
213 | for (i = 0; i < vc->floor_count; ++i) { |
| 220 |
2/2✓ Branch 0 taken 44 times.
✓ Branch 1 taken 98 times.
|
142 | if (vc->floors[i].floor_type == 0) { |
| 221 | 44 | av_freep(&vc->floors[i].data.t0.map[0]); | |
| 222 | 44 | av_freep(&vc->floors[i].data.t0.map[1]); | |
| 223 | 44 | av_freep(&vc->floors[i].data.t0.book_list); | |
| 224 | 44 | av_freep(&vc->floors[i].data.t0.lsp); | |
| 225 | } else { | ||
| 226 | 98 | av_freep(&vc->floors[i].data.t1.list); | |
| 227 | } | ||
| 228 | } | ||
| 229 | 71 | av_freep(&vc->floors); | |
| 230 | |||
| 231 |
1/2✓ Branch 0 taken 71 times.
✗ Branch 1 not taken.
|
71 | if (vc->mappings) |
| 232 |
2/2✓ Branch 0 taken 140 times.
✓ Branch 1 taken 71 times.
|
211 | for (i = 0; i < vc->mapping_count; ++i) { |
| 233 | 140 | av_freep(&vc->mappings[i].magnitude); | |
| 234 | 140 | av_freep(&vc->mappings[i].angle); | |
| 235 | 140 | av_freep(&vc->mappings[i].mux); | |
| 236 | } | ||
| 237 | 71 | av_freep(&vc->mappings); | |
| 238 | 71 | } | |
| 239 | |||
| 240 | // Parse setup header ------------------------------------------------- | ||
| 241 | |||
| 242 | // Process codebooks part | ||
| 243 | |||
| 244 | 71 | static int vorbis_parse_setup_hdr_codebooks(vorbis_context *vc) | |
| 245 | { | ||
| 246 | unsigned cb; | ||
| 247 | 71 | uint8_t *tmp_vlc_bits = NULL; | |
| 248 | 71 | uint32_t *tmp_vlc_codes = NULL; | |
| 249 | 71 | GetBitContext *gb = &vc->gb; | |
| 250 | 71 | uint16_t *codebook_multiplicands = NULL; | |
| 251 | 71 | int ret = 0; | |
| 252 | |||
| 253 | 71 | vc->codebook_count = get_bits(gb, 8) + 1; | |
| 254 | |||
| 255 | ff_dlog(NULL, " Codebooks: %d \n", vc->codebook_count); | ||
| 256 | |||
| 257 | 71 | vc->codebooks = av_mallocz(vc->codebook_count * sizeof(*vc->codebooks)); | |
| 258 | 71 | tmp_vlc_bits = av_mallocz(V_MAX_VLCS * sizeof(*tmp_vlc_bits)); | |
| 259 | 71 | tmp_vlc_codes = av_mallocz(V_MAX_VLCS * sizeof(*tmp_vlc_codes)); | |
| 260 | 71 | codebook_multiplicands = av_malloc(V_MAX_VLCS * sizeof(*codebook_multiplicands)); | |
| 261 |
2/4✓ Branch 0 taken 71 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 71 times.
✗ Branch 3 not taken.
|
71 | if (!vc->codebooks || |
| 262 |
2/4✓ Branch 0 taken 71 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 71 times.
|
71 | !tmp_vlc_bits || !tmp_vlc_codes || !codebook_multiplicands) { |
| 263 | ✗ | ret = AVERROR(ENOMEM); | |
| 264 | ✗ | goto error; | |
| 265 | } | ||
| 266 | |||
| 267 |
2/2✓ Branch 0 taken 2221 times.
✓ Branch 1 taken 71 times.
|
2292 | for (cb = 0; cb < vc->codebook_count; ++cb) { |
| 268 | 2221 | vorbis_codebook *codebook_setup = &vc->codebooks[cb]; | |
| 269 | 2221 | unsigned ordered, t, entries, used_entries = 0; | |
| 270 | |||
| 271 | ff_dlog(NULL, " %u. Codebook\n", cb); | ||
| 272 | |||
| 273 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2221 times.
|
2221 | if (get_bits(gb, 24) != 0x564342) { |
| 274 | ✗ | av_log(vc->avctx, AV_LOG_ERROR, | |
| 275 | " %u. Codebook setup data corrupt.\n", cb); | ||
| 276 | ✗ | ret = AVERROR_INVALIDDATA; | |
| 277 | ✗ | goto error; | |
| 278 | } | ||
| 279 | |||
| 280 | 2221 | codebook_setup->dimensions=get_bits(gb, 16); | |
| 281 |
2/4✓ Branch 0 taken 2221 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2221 times.
|
2221 | if (codebook_setup->dimensions > 16 || codebook_setup->dimensions == 0) { |
| 282 | ✗ | av_log(vc->avctx, AV_LOG_ERROR, | |
| 283 | " %u. Codebook's dimension is invalid (%d).\n", | ||
| 284 | ✗ | cb, codebook_setup->dimensions); | |
| 285 | ✗ | ret = AVERROR_INVALIDDATA; | |
| 286 | ✗ | goto error; | |
| 287 | } | ||
| 288 | 2221 | entries = get_bits(gb, 24); | |
| 289 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2221 times.
|
2221 | if (entries > V_MAX_VLCS) { |
| 290 | ✗ | av_log(vc->avctx, AV_LOG_ERROR, | |
| 291 | " %u. Codebook has too many entries (%u).\n", | ||
| 292 | cb, entries); | ||
| 293 | ✗ | ret = AVERROR_INVALIDDATA; | |
| 294 | ✗ | goto error; | |
| 295 | } | ||
| 296 | |||
| 297 | 2221 | ordered = get_bits1(gb); | |
| 298 | |||
| 299 | ff_dlog(NULL, " codebook_dimensions %d, codebook_entries %u\n", | ||
| 300 | codebook_setup->dimensions, entries); | ||
| 301 | |||
| 302 |
2/2✓ Branch 0 taken 2197 times.
✓ Branch 1 taken 24 times.
|
2221 | if (!ordered) { |
| 303 | unsigned ce, flag; | ||
| 304 | 2197 | unsigned sparse = get_bits1(gb); | |
| 305 | |||
| 306 | ff_dlog(NULL, " not ordered \n"); | ||
| 307 | |||
| 308 |
2/2✓ Branch 0 taken 906 times.
✓ Branch 1 taken 1291 times.
|
2197 | if (sparse) { |
| 309 | ff_dlog(NULL, " sparse \n"); | ||
| 310 | |||
| 311 | 906 | used_entries = 0; | |
| 312 |
2/2✓ Branch 0 taken 295726 times.
✓ Branch 1 taken 906 times.
|
296632 | for (ce = 0; ce < entries; ++ce) { |
| 313 | 295726 | flag = get_bits1(gb); | |
| 314 |
2/2✓ Branch 0 taken 109136 times.
✓ Branch 1 taken 186590 times.
|
295726 | if (flag) { |
| 315 | 109136 | tmp_vlc_bits[ce] = get_bits(gb, 5) + 1; | |
| 316 | 109136 | ++used_entries; | |
| 317 | } else | ||
| 318 | 186590 | tmp_vlc_bits[ce] = 0; | |
| 319 | } | ||
| 320 | } else { | ||
| 321 | ff_dlog(NULL, " not sparse \n"); | ||
| 322 | |||
| 323 | 1291 | used_entries = entries; | |
| 324 |
2/2✓ Branch 0 taken 311874 times.
✓ Branch 1 taken 1291 times.
|
313165 | for (ce = 0; ce < entries; ++ce) |
| 325 | 311874 | tmp_vlc_bits[ce] = get_bits(gb, 5) + 1; | |
| 326 | } | ||
| 327 | } else { | ||
| 328 | 24 | unsigned current_entry = 0; | |
| 329 | 24 | unsigned current_length = get_bits(gb, 5) + 1; | |
| 330 | |||
| 331 | ff_dlog(NULL, " ordered, current length: %u\n", current_length); //FIXME | ||
| 332 | |||
| 333 | 24 | used_entries = entries; | |
| 334 |
3/4✓ Branch 0 taken 84 times.
✓ Branch 1 taken 24 times.
✓ Branch 2 taken 84 times.
✗ Branch 3 not taken.
|
108 | for (; current_entry < used_entries && current_length <= 32; ++current_length) { |
| 335 | unsigned i, number; | ||
| 336 | |||
| 337 | ff_dlog(NULL, " number bits: %u ", ilog(entries - current_entry)); | ||
| 338 | |||
| 339 | 84 | number = get_bits(gb, ilog(entries - current_entry)); | |
| 340 | |||
| 341 | ff_dlog(NULL, " number: %u\n", number); | ||
| 342 | |||
| 343 |
2/2✓ Branch 0 taken 902 times.
✓ Branch 1 taken 84 times.
|
986 | for (i = current_entry; i < number+current_entry; ++i) |
| 344 |
1/2✓ Branch 0 taken 902 times.
✗ Branch 1 not taken.
|
902 | if (i < used_entries) |
| 345 | 902 | tmp_vlc_bits[i] = current_length; | |
| 346 | |||
| 347 | 84 | current_entry+=number; | |
| 348 | } | ||
| 349 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
|
24 | if (current_entry>used_entries) { |
| 350 | ✗ | av_log(vc->avctx, AV_LOG_ERROR, " More codelengths than codes in codebook. \n"); | |
| 351 | ✗ | ret = AVERROR_INVALIDDATA; | |
| 352 | ✗ | goto error; | |
| 353 | } | ||
| 354 | } | ||
| 355 | |||
| 356 | 2221 | codebook_setup->lookup_type = get_bits(gb, 4); | |
| 357 | |||
| 358 | ff_dlog(NULL, " lookup type: %d : %s \n", codebook_setup->lookup_type, | ||
| 359 | codebook_setup->lookup_type ? "vq" : "no lookup"); | ||
| 360 | |||
| 361 | // If the codebook is used for (inverse) VQ, calculate codevectors. | ||
| 362 | |||
| 363 |
2/2✓ Branch 0 taken 950 times.
✓ Branch 1 taken 1271 times.
|
2221 | if (codebook_setup->lookup_type == 1) { |
| 364 | unsigned i, j, k; | ||
| 365 | 950 | unsigned codebook_lookup_values = ff_vorbis_nth_root(entries, codebook_setup->dimensions); | |
| 366 | |||
| 367 | 950 | float codebook_minimum_value = vorbisfloat2float(get_bits_long(gb, 32)); | |
| 368 | 950 | float codebook_delta_value = vorbisfloat2float(get_bits_long(gb, 32)); | |
| 369 | 950 | unsigned codebook_value_bits = get_bits(gb, 4) + 1; | |
| 370 | 950 | unsigned codebook_sequence_p = get_bits1(gb); | |
| 371 | |||
| 372 |
2/4✓ Branch 0 taken 950 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 950 times.
|
950 | if (!isfinite(codebook_minimum_value) || !isfinite(codebook_delta_value)) { |
| 373 | ✗ | ret = AVERROR_INVALIDDATA; | |
| 374 | ✗ | goto error; | |
| 375 | } | ||
| 376 | ff_dlog(NULL, " We expect %d numbers for building the codevectors. \n", | ||
| 377 | codebook_lookup_values); | ||
| 378 | ff_dlog(NULL, " delta %f minimum %f \n", | ||
| 379 | codebook_delta_value, codebook_minimum_value); | ||
| 380 | |||
| 381 |
2/2✓ Branch 0 taken 17652 times.
✓ Branch 1 taken 950 times.
|
18602 | for (i = 0; i < codebook_lookup_values; ++i) { |
| 382 | 17652 | codebook_multiplicands[i] = get_bits(gb, codebook_value_bits); | |
| 383 | |||
| 384 | ff_dlog(NULL, " multiplicands*delta+minimum : %e \n", | ||
| 385 | (float)codebook_multiplicands[i] * codebook_delta_value + codebook_minimum_value); | ||
| 386 | ff_dlog(NULL, " multiplicand %u\n", codebook_multiplicands[i]); | ||
| 387 | } | ||
| 388 | |||
| 389 | // Weed out unused vlcs and build codevector vector | ||
| 390 |
1/2✓ Branch 0 taken 950 times.
✗ Branch 1 not taken.
|
950 | if (used_entries) { |
| 391 | 950 | codebook_setup->codevectors = | |
| 392 | 950 | av_calloc(used_entries, codebook_setup->dimensions * | |
| 393 | sizeof(*codebook_setup->codevectors)); | ||
| 394 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 950 times.
|
950 | if (!codebook_setup->codevectors) { |
| 395 | ✗ | ret = AVERROR(ENOMEM); | |
| 396 | ✗ | goto error; | |
| 397 | } | ||
| 398 | } else | ||
| 399 | ✗ | codebook_setup->codevectors = NULL; | |
| 400 | |||
| 401 |
2/2✓ Branch 0 taken 503908 times.
✓ Branch 1 taken 950 times.
|
504858 | for (j = 0, i = 0; i < entries; ++i) { |
| 402 | 503908 | unsigned dim = codebook_setup->dimensions; | |
| 403 | |||
| 404 |
2/2✓ Branch 0 taken 331000 times.
✓ Branch 1 taken 172908 times.
|
503908 | if (tmp_vlc_bits[i]) { |
| 405 | 331000 | float last = 0.0; | |
| 406 | 331000 | unsigned lookup_offset = i; | |
| 407 | |||
| 408 | ff_dlog(vc->avctx, "Lookup offset %u ,", i); | ||
| 409 | |||
| 410 |
2/2✓ Branch 0 taken 999503 times.
✓ Branch 1 taken 331000 times.
|
1330503 | for (k = 0; k < dim; ++k) { |
| 411 | 999503 | unsigned multiplicand_offset = lookup_offset % codebook_lookup_values; | |
| 412 | 999503 | codebook_setup->codevectors[j * dim + k] = codebook_multiplicands[multiplicand_offset] * codebook_delta_value + codebook_minimum_value + last; | |
| 413 |
2/2✓ Branch 0 taken 257530 times.
✓ Branch 1 taken 741973 times.
|
999503 | if (codebook_sequence_p) |
| 414 | 257530 | last = codebook_setup->codevectors[j * dim + k]; | |
| 415 | 999503 | lookup_offset/=codebook_lookup_values; | |
| 416 | } | ||
| 417 | 331000 | tmp_vlc_bits[j] = tmp_vlc_bits[i]; | |
| 418 | |||
| 419 | ff_dlog(vc->avctx, "real lookup offset %u, vector: ", j); | ||
| 420 |
2/2✓ Branch 0 taken 999503 times.
✓ Branch 1 taken 331000 times.
|
1330503 | for (k = 0; k < dim; ++k) |
| 421 | ff_dlog(vc->avctx, " %f ", | ||
| 422 | codebook_setup->codevectors[j * dim + k]); | ||
| 423 | ff_dlog(vc->avctx, "\n"); | ||
| 424 | |||
| 425 | 331000 | ++j; | |
| 426 | } | ||
| 427 | } | ||
| 428 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 950 times.
|
950 | if (j != used_entries) { |
| 429 | ✗ | av_log(vc->avctx, AV_LOG_ERROR, "Bug in codevector vector building code. \n"); | |
| 430 | ✗ | ret = AVERROR_INVALIDDATA; | |
| 431 | ✗ | goto error; | |
| 432 | } | ||
| 433 | 950 | entries = used_entries; | |
| 434 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1271 times.
|
1271 | } else if (codebook_setup->lookup_type >= 2) { |
| 435 | ✗ | av_log(vc->avctx, AV_LOG_ERROR, "Codebook lookup type not supported. \n"); | |
| 436 | ✗ | ret = AVERROR_INVALIDDATA; | |
| 437 | ✗ | goto error; | |
| 438 | } | ||
| 439 | |||
| 440 | // Initialize VLC table | ||
| 441 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2221 times.
|
2221 | if (ff_vorbis_len2vlc(tmp_vlc_bits, tmp_vlc_codes, entries)) { |
| 442 | ✗ | av_log(vc->avctx, AV_LOG_ERROR, " Invalid code lengths while generating vlcs. \n"); | |
| 443 | ✗ | ret = AVERROR_INVALIDDATA; | |
| 444 | ✗ | goto error; | |
| 445 | } | ||
| 446 | 2221 | codebook_setup->maxdepth = 0; | |
| 447 |
2/2✓ Branch 0 taken 435594 times.
✓ Branch 1 taken 2221 times.
|
437815 | for (t = 0; t < entries; ++t) |
| 448 |
2/2✓ Branch 0 taken 120222 times.
✓ Branch 1 taken 315372 times.
|
435594 | if (tmp_vlc_bits[t] >= codebook_setup->maxdepth) |
| 449 | 120222 | codebook_setup->maxdepth = tmp_vlc_bits[t]; | |
| 450 | |||
| 451 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2221 times.
|
2221 | if (codebook_setup->maxdepth > 3 * V_NB_BITS) |
| 452 | ✗ | codebook_setup->nb_bits = V_NB_BITS2; | |
| 453 | else | ||
| 454 | 2221 | codebook_setup->nb_bits = V_NB_BITS; | |
| 455 | |||
| 456 | 2221 | codebook_setup->maxdepth = (codebook_setup->maxdepth+codebook_setup->nb_bits - 1) / codebook_setup->nb_bits; | |
| 457 | |||
| 458 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2221 times.
|
2221 | if ((ret = vlc_init(&codebook_setup->vlc, codebook_setup->nb_bits, |
| 459 | entries, tmp_vlc_bits, sizeof(*tmp_vlc_bits), | ||
| 460 | sizeof(*tmp_vlc_bits), tmp_vlc_codes, | ||
| 461 | sizeof(*tmp_vlc_codes), sizeof(*tmp_vlc_codes), | ||
| 462 | VLC_INIT_LE))) { | ||
| 463 | ✗ | av_log(vc->avctx, AV_LOG_ERROR, " Error generating vlc tables. \n"); | |
| 464 | ✗ | goto error; | |
| 465 | } | ||
| 466 | } | ||
| 467 | |||
| 468 | 71 | av_free(tmp_vlc_bits); | |
| 469 | 71 | av_free(tmp_vlc_codes); | |
| 470 | 71 | av_free(codebook_multiplicands); | |
| 471 | 71 | return 0; | |
| 472 | |||
| 473 | // Error: | ||
| 474 | ✗ | error: | |
| 475 | ✗ | av_free(tmp_vlc_bits); | |
| 476 | ✗ | av_free(tmp_vlc_codes); | |
| 477 | ✗ | av_free(codebook_multiplicands); | |
| 478 | ✗ | return ret; | |
| 479 | } | ||
| 480 | |||
| 481 | // Process time domain transforms part (unused in Vorbis I) | ||
| 482 | |||
| 483 | 71 | static int vorbis_parse_setup_hdr_tdtransforms(vorbis_context *vc) | |
| 484 | { | ||
| 485 | 71 | GetBitContext *gb = &vc->gb; | |
| 486 | 71 | unsigned i, vorbis_time_count = get_bits(gb, 6) + 1; | |
| 487 | |||
| 488 |
2/2✓ Branch 0 taken 71 times.
✓ Branch 1 taken 71 times.
|
142 | for (i = 0; i < vorbis_time_count; ++i) { |
| 489 | 71 | unsigned vorbis_tdtransform = get_bits(gb, 16); | |
| 490 | |||
| 491 | ff_dlog(NULL, " Vorbis time domain transform %u: %u\n", | ||
| 492 | vorbis_time_count, vorbis_tdtransform); | ||
| 493 | |||
| 494 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 71 times.
|
71 | if (vorbis_tdtransform) { |
| 495 | ✗ | av_log(vc->avctx, AV_LOG_ERROR, "Vorbis time domain transform data nonzero. \n"); | |
| 496 | ✗ | return AVERROR_INVALIDDATA; | |
| 497 | } | ||
| 498 | } | ||
| 499 | 71 | return 0; | |
| 500 | } | ||
| 501 | |||
| 502 | // Process floors part | ||
| 503 | |||
| 504 | static int vorbis_floor0_decode(vorbis_context *vc, | ||
| 505 | vorbis_floor_data *vfu, float *vec); | ||
| 506 | static int create_map(vorbis_context *vc, unsigned floor_number); | ||
| 507 | static int vorbis_floor1_decode(vorbis_context *vc, | ||
| 508 | vorbis_floor_data *vfu, float *vec); | ||
| 509 | 71 | static int vorbis_parse_setup_hdr_floors(vorbis_context *vc) | |
| 510 | { | ||
| 511 | 71 | GetBitContext *gb = &vc->gb; | |
| 512 | int i, j, k, ret; | ||
| 513 | |||
| 514 | 71 | vc->floor_count = get_bits(gb, 6) + 1; | |
| 515 | |||
| 516 | 71 | vc->floors = av_mallocz(vc->floor_count * sizeof(*vc->floors)); | |
| 517 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 71 times.
|
71 | if (!vc->floors) |
| 518 | ✗ | return AVERROR(ENOMEM); | |
| 519 | |||
| 520 |
2/2✓ Branch 0 taken 142 times.
✓ Branch 1 taken 71 times.
|
213 | for (i = 0; i < vc->floor_count; ++i) { |
| 521 | 142 | vorbis_floor *floor_setup = &vc->floors[i]; | |
| 522 | |||
| 523 | 142 | floor_setup->floor_type = get_bits(gb, 16); | |
| 524 | |||
| 525 | ff_dlog(NULL, " %d. floor type %d \n", i, floor_setup->floor_type); | ||
| 526 | |||
| 527 |
2/2✓ Branch 0 taken 98 times.
✓ Branch 1 taken 44 times.
|
142 | if (floor_setup->floor_type == 1) { |
| 528 | 98 | int maximum_class = -1; | |
| 529 | 98 | unsigned rangebits, rangemax, floor1_values = 2; | |
| 530 | |||
| 531 | 98 | floor_setup->decode = vorbis_floor1_decode; | |
| 532 | |||
| 533 | 98 | floor_setup->data.t1.partitions = get_bits(gb, 5); | |
| 534 | |||
| 535 | ff_dlog(NULL, " %d.floor: %d partitions \n", | ||
| 536 | i, floor_setup->data.t1.partitions); | ||
| 537 | |||
| 538 |
2/2✓ Branch 0 taken 552 times.
✓ Branch 1 taken 98 times.
|
650 | for (j = 0; j < floor_setup->data.t1.partitions; ++j) { |
| 539 | 552 | floor_setup->data.t1.partition_class[j] = get_bits(gb, 4); | |
| 540 |
2/2✓ Branch 0 taken 353 times.
✓ Branch 1 taken 199 times.
|
552 | if (floor_setup->data.t1.partition_class[j] > maximum_class) |
| 541 | 353 | maximum_class = floor_setup->data.t1.partition_class[j]; | |
| 542 | |||
| 543 | ff_dlog(NULL, " %d. floor %d partition class %d \n", | ||
| 544 | i, j, floor_setup->data.t1.partition_class[j]); | ||
| 545 | |||
| 546 | } | ||
| 547 | |||
| 548 | ff_dlog(NULL, " maximum class %d \n", maximum_class); | ||
| 549 | |||
| 550 |
2/2✓ Branch 0 taken 353 times.
✓ Branch 1 taken 98 times.
|
451 | for (j = 0; j <= maximum_class; ++j) { |
| 551 | 353 | floor_setup->data.t1.class_dimensions[j] = get_bits(gb, 3) + 1; | |
| 552 | 353 | floor_setup->data.t1.class_subclasses[j] = get_bits(gb, 2); | |
| 553 | |||
| 554 | ff_dlog(NULL, " %d floor %d class dim: %d subclasses %d \n", i, j, | ||
| 555 | floor_setup->data.t1.class_dimensions[j], | ||
| 556 | floor_setup->data.t1.class_subclasses[j]); | ||
| 557 | |||
| 558 |
2/2✓ Branch 0 taken 292 times.
✓ Branch 1 taken 61 times.
|
353 | if (floor_setup->data.t1.class_subclasses[j]) { |
| 559 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 292 times.
|
292 | GET_VALIDATED_INDEX(floor_setup->data.t1.class_masterbook[j], 8, vc->codebook_count) |
| 560 | |||
| 561 | ff_dlog(NULL, " masterbook: %d \n", floor_setup->data.t1.class_masterbook[j]); | ||
| 562 | } | ||
| 563 | |||
| 564 |
2/2✓ Branch 0 taken 1009 times.
✓ Branch 1 taken 353 times.
|
1362 | for (k = 0; k < (1 << floor_setup->data.t1.class_subclasses[j]); ++k) { |
| 565 | 1009 | int16_t bits = get_bits(gb, 8) - 1; | |
| 566 |
2/2✓ Branch 0 taken 837 times.
✓ Branch 1 taken 172 times.
|
1009 | if (bits != -1) |
| 567 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 837 times.
|
837 | VALIDATE_INDEX(bits, vc->codebook_count) |
| 568 | 1009 | floor_setup->data.t1.subclass_books[j][k] = bits; | |
| 569 | |||
| 570 | ff_dlog(NULL, " book %d. : %d \n", k, floor_setup->data.t1.subclass_books[j][k]); | ||
| 571 | } | ||
| 572 | } | ||
| 573 | |||
| 574 | 98 | floor_setup->data.t1.multiplier = get_bits(gb, 2) + 1; | |
| 575 | 98 | floor_setup->data.t1.x_list_dim = 2; | |
| 576 | |||
| 577 |
2/2✓ Branch 0 taken 552 times.
✓ Branch 1 taken 98 times.
|
650 | for (j = 0; j < floor_setup->data.t1.partitions; ++j) |
| 578 | 552 | floor_setup->data.t1.x_list_dim+=floor_setup->data.t1.class_dimensions[floor_setup->data.t1.partition_class[j]]; | |
| 579 | |||
| 580 | 98 | floor_setup->data.t1.list = av_calloc(floor_setup->data.t1.x_list_dim, | |
| 581 | sizeof(*floor_setup->data.t1.list)); | ||
| 582 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 98 times.
|
98 | if (!floor_setup->data.t1.list) |
| 583 | ✗ | return AVERROR(ENOMEM); | |
| 584 | |||
| 585 | 98 | rangebits = get_bits(gb, 4); | |
| 586 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 98 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
98 | if (!rangebits && floor_setup->data.t1.partitions) { |
| 587 | ✗ | av_log(vc->avctx, AV_LOG_ERROR, | |
| 588 | "A rangebits value of 0 is not compliant with the Vorbis I specification.\n"); | ||
| 589 | ✗ | return AVERROR_INVALIDDATA; | |
| 590 | } | ||
| 591 | 98 | rangemax = (1 << rangebits); | |
| 592 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 98 times.
|
98 | if (rangemax > vc->blocksize[1] / 2) { |
| 593 | ✗ | av_log(vc->avctx, AV_LOG_ERROR, | |
| 594 | "Floor value is too large for blocksize: %u (%"PRIu32")\n", | ||
| 595 | ✗ | rangemax, vc->blocksize[1] / 2); | |
| 596 | ✗ | return AVERROR_INVALIDDATA; | |
| 597 | } | ||
| 598 | 98 | floor_setup->data.t1.list[0].x = 0; | |
| 599 | 98 | floor_setup->data.t1.list[1].x = rangemax; | |
| 600 | |||
| 601 |
2/2✓ Branch 0 taken 552 times.
✓ Branch 1 taken 98 times.
|
650 | for (j = 0; j < floor_setup->data.t1.partitions; ++j) { |
| 602 |
2/2✓ Branch 0 taken 1788 times.
✓ Branch 1 taken 552 times.
|
2340 | for (k = 0; k < floor_setup->data.t1.class_dimensions[floor_setup->data.t1.partition_class[j]]; ++k, ++floor1_values) { |
| 603 | 1788 | floor_setup->data.t1.list[floor1_values].x = get_bits(gb, rangebits); | |
| 604 | |||
| 605 | ff_dlog(NULL, " %u. floor1 Y coord. %d\n", floor1_values, | ||
| 606 | floor_setup->data.t1.list[floor1_values].x); | ||
| 607 | } | ||
| 608 | } | ||
| 609 | |||
| 610 | // Precalculate order of x coordinates - needed for decode | ||
| 611 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 98 times.
|
98 | if (ff_vorbis_ready_floor1_list(vc->avctx, |
| 612 | floor_setup->data.t1.list, | ||
| 613 | 98 | floor_setup->data.t1.x_list_dim)) { | |
| 614 | ✗ | return AVERROR_INVALIDDATA; | |
| 615 | } | ||
| 616 |
1/2✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
|
44 | } else if (floor_setup->floor_type == 0) { |
| 617 | 44 | unsigned max_codebook_dim = 0; | |
| 618 | |||
| 619 | 44 | floor_setup->decode = vorbis_floor0_decode; | |
| 620 | |||
| 621 | 44 | floor_setup->data.t0.order = get_bits(gb, 8); | |
| 622 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 44 times.
|
44 | if (!floor_setup->data.t0.order) { |
| 623 | ✗ | av_log(vc->avctx, AV_LOG_ERROR, "Floor 0 order is 0.\n"); | |
| 624 | ✗ | return AVERROR_INVALIDDATA; | |
| 625 | } | ||
| 626 | 44 | floor_setup->data.t0.rate = get_bits(gb, 16); | |
| 627 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 44 times.
|
44 | if (!floor_setup->data.t0.rate) { |
| 628 | ✗ | av_log(vc->avctx, AV_LOG_ERROR, "Floor 0 rate is 0.\n"); | |
| 629 | ✗ | return AVERROR_INVALIDDATA; | |
| 630 | } | ||
| 631 | 44 | floor_setup->data.t0.bark_map_size = get_bits(gb, 16); | |
| 632 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 44 times.
|
44 | if (!floor_setup->data.t0.bark_map_size) { |
| 633 | ✗ | av_log(vc->avctx, AV_LOG_ERROR, | |
| 634 | "Floor 0 bark map size is 0.\n"); | ||
| 635 | ✗ | return AVERROR_INVALIDDATA; | |
| 636 | } | ||
| 637 | 44 | floor_setup->data.t0.amplitude_bits = get_bits(gb, 6); | |
| 638 | 44 | floor_setup->data.t0.amplitude_offset = get_bits(gb, 8); | |
| 639 | 44 | floor_setup->data.t0.num_books = get_bits(gb, 4) + 1; | |
| 640 | |||
| 641 | /* allocate mem for booklist */ | ||
| 642 | 44 | floor_setup->data.t0.book_list = | |
| 643 | 44 | av_malloc(floor_setup->data.t0.num_books); | |
| 644 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 44 times.
|
44 | if (!floor_setup->data.t0.book_list) |
| 645 | ✗ | return AVERROR(ENOMEM); | |
| 646 | /* read book indexes */ | ||
| 647 | { | ||
| 648 | int idx; | ||
| 649 | unsigned book_idx; | ||
| 650 |
2/2✓ Branch 0 taken 68 times.
✓ Branch 1 taken 44 times.
|
112 | for (idx = 0; idx < floor_setup->data.t0.num_books; ++idx) { |
| 651 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 68 times.
|
68 | GET_VALIDATED_INDEX(book_idx, 8, vc->codebook_count) |
| 652 | 68 | floor_setup->data.t0.book_list[idx] = book_idx; | |
| 653 |
1/2✓ Branch 0 taken 68 times.
✗ Branch 1 not taken.
|
68 | if (vc->codebooks[book_idx].dimensions > max_codebook_dim) |
| 654 | 68 | max_codebook_dim = vc->codebooks[book_idx].dimensions; | |
| 655 | } | ||
| 656 | } | ||
| 657 | |||
| 658 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 44 times.
|
44 | if ((ret = create_map(vc, i)) < 0) |
| 659 | ✗ | return ret; | |
| 660 | |||
| 661 | /* codebook dim is for padding if codebook dim doesn't * | ||
| 662 | * divide order+1 then we need to read more data */ | ||
| 663 | 44 | floor_setup->data.t0.lsp = | |
| 664 | 44 | av_malloc_array((floor_setup->data.t0.order + 1 + max_codebook_dim), | |
| 665 | sizeof(*floor_setup->data.t0.lsp)); | ||
| 666 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 44 times.
|
44 | if (!floor_setup->data.t0.lsp) |
| 667 | ✗ | return AVERROR(ENOMEM); | |
| 668 | |||
| 669 | /* debug output parsed headers */ | ||
| 670 | ff_dlog(NULL, "floor0 order: %u\n", floor_setup->data.t0.order); | ||
| 671 | ff_dlog(NULL, "floor0 rate: %u\n", floor_setup->data.t0.rate); | ||
| 672 | ff_dlog(NULL, "floor0 bark map size: %u\n", | ||
| 673 | floor_setup->data.t0.bark_map_size); | ||
| 674 | ff_dlog(NULL, "floor0 amplitude bits: %u\n", | ||
| 675 | floor_setup->data.t0.amplitude_bits); | ||
| 676 | ff_dlog(NULL, "floor0 amplitude offset: %u\n", | ||
| 677 | floor_setup->data.t0.amplitude_offset); | ||
| 678 | ff_dlog(NULL, "floor0 number of books: %u\n", | ||
| 679 | floor_setup->data.t0.num_books); | ||
| 680 | ff_dlog(NULL, "floor0 book list pointer: %p\n", | ||
| 681 | floor_setup->data.t0.book_list); | ||
| 682 | { | ||
| 683 | int idx; | ||
| 684 |
2/2✓ Branch 0 taken 68 times.
✓ Branch 1 taken 44 times.
|
112 | for (idx = 0; idx < floor_setup->data.t0.num_books; ++idx) { |
| 685 | ff_dlog(NULL, " Book %d: %u\n", idx + 1, | ||
| 686 | floor_setup->data.t0.book_list[idx]); | ||
| 687 | } | ||
| 688 | } | ||
| 689 | } else { | ||
| 690 | ✗ | av_log(vc->avctx, AV_LOG_ERROR, "Invalid floor type!\n"); | |
| 691 | ✗ | return AVERROR_INVALIDDATA; | |
| 692 | } | ||
| 693 | } | ||
| 694 | 71 | return 0; | |
| 695 | } | ||
| 696 | |||
| 697 | // Process residues part | ||
| 698 | |||
| 699 | 71 | static int vorbis_parse_setup_hdr_residues(vorbis_context *vc) | |
| 700 | { | ||
| 701 | 71 | GetBitContext *gb = &vc->gb; | |
| 702 | unsigned i, j, k; | ||
| 703 | |||
| 704 | 71 | vc->residue_count = get_bits(gb, 6)+1; | |
| 705 | 71 | vc->residues = av_mallocz(vc->residue_count * sizeof(*vc->residues)); | |
| 706 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 71 times.
|
71 | if (!vc->residues) |
| 707 | ✗ | return AVERROR(ENOMEM); | |
| 708 | |||
| 709 | ff_dlog(NULL, " There are %d residues. \n", vc->residue_count); | ||
| 710 | |||
| 711 |
2/2✓ Branch 0 taken 142 times.
✓ Branch 1 taken 71 times.
|
213 | for (i = 0; i < vc->residue_count; ++i) { |
| 712 | 142 | vorbis_residue *res_setup = &vc->residues[i]; | |
| 713 | uint8_t cascade[64]; | ||
| 714 | unsigned high_bits, low_bits; | ||
| 715 | |||
| 716 | 142 | res_setup->type = get_bits(gb, 16); | |
| 717 | |||
| 718 | ff_dlog(NULL, " %u. residue type %d\n", i, res_setup->type); | ||
| 719 | |||
| 720 | 142 | res_setup->begin = get_bits(gb, 24); | |
| 721 | 142 | res_setup->end = get_bits(gb, 24); | |
| 722 | 142 | res_setup->partition_size = get_bits(gb, 24) + 1; | |
| 723 | /* Validations to prevent a buffer overflow later. */ | ||
| 724 |
1/2✓ Branch 0 taken 142 times.
✗ Branch 1 not taken.
|
142 | if (res_setup->begin>res_setup->end || |
| 725 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 142 times.
|
142 | (res_setup->end-res_setup->begin) / res_setup->partition_size > FFMIN(V_MAX_PARTITIONS, 65535)) { |
| 726 | ✗ | av_log(vc->avctx, AV_LOG_ERROR, | |
| 727 | "partition out of bounds: type, begin, end, size, blocksize: %"PRIu16", %"PRIu32", %"PRIu32", %u, %"PRIu32"\n", | ||
| 728 | ✗ | res_setup->type, res_setup->begin, res_setup->end, | |
| 729 | ✗ | res_setup->partition_size, vc->blocksize[1] / 2); | |
| 730 | ✗ | return AVERROR_INVALIDDATA; | |
| 731 | } | ||
| 732 | |||
| 733 | 142 | res_setup->classifications = get_bits(gb, 6) + 1; | |
| 734 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 142 times.
|
142 | GET_VALIDATED_INDEX(res_setup->classbook, 8, vc->codebook_count) |
| 735 | |||
| 736 | 142 | res_setup->ptns_to_read = | |
| 737 | 142 | (res_setup->end - res_setup->begin) / res_setup->partition_size; | |
| 738 | 284 | res_setup->classifs = av_malloc_array(res_setup->ptns_to_read, | |
| 739 | 142 | vc->audio_channels * | |
| 740 | sizeof(*res_setup->classifs)); | ||
| 741 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 142 times.
|
142 | if (!res_setup->classifs) |
| 742 | ✗ | return AVERROR(ENOMEM); | |
| 743 | |||
| 744 | ff_dlog(NULL, " begin %"PRIu32" end %"PRIu32" part.size %u classif.s %"PRIu8" classbook %"PRIu8"\n", | ||
| 745 | res_setup->begin, res_setup->end, res_setup->partition_size, | ||
| 746 | res_setup->classifications, res_setup->classbook); | ||
| 747 | |||
| 748 |
2/2✓ Branch 0 taken 1224 times.
✓ Branch 1 taken 142 times.
|
1366 | for (j = 0; j < res_setup->classifications; ++j) { |
| 749 | 1224 | high_bits = 0; | |
| 750 | 1224 | low_bits = get_bits(gb, 3); | |
| 751 |
2/2✓ Branch 1 taken 4 times.
✓ Branch 2 taken 1220 times.
|
1224 | if (get_bits1(gb)) |
| 752 | 4 | high_bits = get_bits(gb, 5); | |
| 753 | 1224 | cascade[j] = (high_bits << 3) + low_bits; | |
| 754 | |||
| 755 | ff_dlog(NULL, " %u class cascade depth: %d\n", j, ilog(cascade[j])); | ||
| 756 | } | ||
| 757 | |||
| 758 | 142 | res_setup->maxpass = 0; | |
| 759 |
2/2✓ Branch 0 taken 1224 times.
✓ Branch 1 taken 142 times.
|
1366 | for (j = 0; j < res_setup->classifications; ++j) { |
| 760 |
2/2✓ Branch 0 taken 9792 times.
✓ Branch 1 taken 1224 times.
|
11016 | for (k = 0; k < 8; ++k) { |
| 761 |
2/2✓ Branch 0 taken 1448 times.
✓ Branch 1 taken 8344 times.
|
9792 | if (cascade[j]&(1 << k)) { |
| 762 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1448 times.
|
1448 | GET_VALIDATED_INDEX(res_setup->books[j][k], 8, vc->codebook_count) |
| 763 | |||
| 764 | ff_dlog(NULL, " %u class cascade depth %u book: %d\n", | ||
| 765 | j, k, res_setup->books[j][k]); | ||
| 766 | |||
| 767 |
2/2✓ Branch 0 taken 116 times.
✓ Branch 1 taken 1332 times.
|
1448 | if (k>res_setup->maxpass) |
| 768 | 116 | res_setup->maxpass = k; | |
| 769 | } else { | ||
| 770 | 8344 | res_setup->books[j][k] = -1; | |
| 771 | } | ||
| 772 | } | ||
| 773 | } | ||
| 774 | } | ||
| 775 | 71 | return 0; | |
| 776 | } | ||
| 777 | |||
| 778 | // Process mappings part | ||
| 779 | |||
| 780 | 71 | static int vorbis_parse_setup_hdr_mappings(vorbis_context *vc) | |
| 781 | { | ||
| 782 | 71 | GetBitContext *gb = &vc->gb; | |
| 783 | unsigned i, j; | ||
| 784 | |||
| 785 | 71 | vc->mapping_count = get_bits(gb, 6)+1; | |
| 786 | 71 | vc->mappings = av_mallocz(vc->mapping_count * sizeof(*vc->mappings)); | |
| 787 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 71 times.
|
71 | if (!vc->mappings) |
| 788 | ✗ | return AVERROR(ENOMEM); | |
| 789 | |||
| 790 | ff_dlog(NULL, " There are %d mappings. \n", vc->mapping_count); | ||
| 791 | |||
| 792 |
2/2✓ Branch 0 taken 140 times.
✓ Branch 1 taken 71 times.
|
211 | for (i = 0; i < vc->mapping_count; ++i) { |
| 793 | 140 | vorbis_mapping *mapping_setup = &vc->mappings[i]; | |
| 794 | |||
| 795 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 140 times.
|
140 | if (get_bits(gb, 16)) { |
| 796 | ✗ | av_log(vc->avctx, AV_LOG_ERROR, "Other mappings than type 0 are not compliant with the Vorbis I specification. \n"); | |
| 797 | ✗ | return AVERROR_INVALIDDATA; | |
| 798 | } | ||
| 799 |
2/2✓ Branch 1 taken 4 times.
✓ Branch 2 taken 136 times.
|
140 | if (get_bits1(gb)) { |
| 800 | 4 | mapping_setup->submaps = get_bits(gb, 4) + 1; | |
| 801 | } else { | ||
| 802 | 136 | mapping_setup->submaps = 1; | |
| 803 | } | ||
| 804 | |||
| 805 |
2/2✓ Branch 1 taken 50 times.
✓ Branch 2 taken 90 times.
|
140 | if (get_bits1(gb)) { |
| 806 | 50 | mapping_setup->coupling_steps = get_bits(gb, 8) + 1; | |
| 807 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
|
50 | if (vc->audio_channels < 2) { |
| 808 | ✗ | av_log(vc->avctx, AV_LOG_ERROR, | |
| 809 | "Square polar channel mapping with less than two channels is not compliant with the Vorbis I specification.\n"); | ||
| 810 | ✗ | return AVERROR_INVALIDDATA; | |
| 811 | } | ||
| 812 | 50 | mapping_setup->magnitude = av_mallocz(mapping_setup->coupling_steps * | |
| 813 | sizeof(*mapping_setup->magnitude)); | ||
| 814 | 50 | mapping_setup->angle = av_mallocz(mapping_setup->coupling_steps * | |
| 815 | sizeof(*mapping_setup->angle)); | ||
| 816 |
2/4✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 50 times.
|
50 | if (!mapping_setup->angle || !mapping_setup->magnitude) |
| 817 | ✗ | return AVERROR(ENOMEM); | |
| 818 | |||
| 819 |
2/2✓ Branch 0 taken 62 times.
✓ Branch 1 taken 50 times.
|
112 | for (j = 0; j < mapping_setup->coupling_steps; ++j) { |
| 820 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 62 times.
|
62 | GET_VALIDATED_INDEX(mapping_setup->magnitude[j], ilog(vc->audio_channels - 1), vc->audio_channels) |
| 821 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 62 times.
|
62 | GET_VALIDATED_INDEX(mapping_setup->angle[j], ilog(vc->audio_channels - 1), vc->audio_channels) |
| 822 | } | ||
| 823 | } else { | ||
| 824 | 90 | mapping_setup->coupling_steps = 0; | |
| 825 | } | ||
| 826 | |||
| 827 | ff_dlog(NULL, " %u mapping coupling steps: %d\n", | ||
| 828 | i, mapping_setup->coupling_steps); | ||
| 829 | |||
| 830 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 140 times.
|
140 | if (get_bits(gb, 2)) { |
| 831 | ✗ | av_log(vc->avctx, AV_LOG_ERROR, "%u. mapping setup data invalid.\n", i); | |
| 832 | ✗ | return AVERROR_INVALIDDATA; // following spec. | |
| 833 | } | ||
| 834 | |||
| 835 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 136 times.
|
140 | if (mapping_setup->submaps>1) { |
| 836 | 4 | mapping_setup->mux = av_calloc(vc->audio_channels, | |
| 837 | sizeof(*mapping_setup->mux)); | ||
| 838 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (!mapping_setup->mux) |
| 839 | ✗ | return AVERROR(ENOMEM); | |
| 840 | |||
| 841 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 4 times.
|
28 | for (j = 0; j < vc->audio_channels; ++j) |
| 842 | 24 | mapping_setup->mux[j] = get_bits(gb, 4); | |
| 843 | } | ||
| 844 | |||
| 845 |
2/2✓ Branch 0 taken 144 times.
✓ Branch 1 taken 140 times.
|
284 | for (j = 0; j < mapping_setup->submaps; ++j) { |
| 846 | 144 | skip_bits(gb, 8); // FIXME check? | |
| 847 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 144 times.
|
144 | GET_VALIDATED_INDEX(mapping_setup->submap_floor[j], 8, vc->floor_count) |
| 848 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 144 times.
|
144 | GET_VALIDATED_INDEX(mapping_setup->submap_residue[j], 8, vc->residue_count) |
| 849 | |||
| 850 | ff_dlog(NULL, " %u mapping %u submap : floor %d, residue %d\n", i, j, | ||
| 851 | mapping_setup->submap_floor[j], | ||
| 852 | mapping_setup->submap_residue[j]); | ||
| 853 | } | ||
| 854 | } | ||
| 855 | 71 | return 0; | |
| 856 | } | ||
| 857 | |||
| 858 | // Process modes part | ||
| 859 | |||
| 860 | 44 | static int create_map(vorbis_context *vc, unsigned floor_number) | |
| 861 | { | ||
| 862 | 44 | vorbis_floor *floors = vc->floors; | |
| 863 | vorbis_floor0 *vf; | ||
| 864 | int idx; | ||
| 865 | int blockflag, n; | ||
| 866 | int32_t *map; | ||
| 867 | |||
| 868 |
2/2✓ Branch 0 taken 88 times.
✓ Branch 1 taken 44 times.
|
132 | for (blockflag = 0; blockflag < 2; ++blockflag) { |
| 869 | 88 | n = vc->blocksize[blockflag] / 2; | |
| 870 | 176 | floors[floor_number].data.t0.map[blockflag] = | |
| 871 | 88 | av_malloc_array(n + 1, sizeof(int32_t)); // n + sentinel | |
| 872 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 88 times.
|
88 | if (!floors[floor_number].data.t0.map[blockflag]) |
| 873 | ✗ | return AVERROR(ENOMEM); | |
| 874 | |||
| 875 | 88 | map = floors[floor_number].data.t0.map[blockflag]; | |
| 876 | 88 | vf = &floors[floor_number].data.t0; | |
| 877 | |||
| 878 |
2/2✓ Branch 0 taken 51200 times.
✓ Branch 1 taken 88 times.
|
51288 | for (idx = 0; idx < n; ++idx) { |
| 879 | 51200 | map[idx] = floor(BARK((vf->rate * idx) / (2.0f * n)) * | |
| 880 | 51200 | (vf->bark_map_size / BARK(vf->rate / 2.0f))); | |
| 881 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 51200 times.
|
51200 | if (vf->bark_map_size-1 < map[idx]) |
| 882 | ✗ | map[idx] = vf->bark_map_size - 1; | |
| 883 | } | ||
| 884 | 88 | map[n] = -1; | |
| 885 | 88 | vf->map_size[blockflag] = n; | |
| 886 | } | ||
| 887 | |||
| 888 |
2/2✓ Branch 0 taken 45100 times.
✓ Branch 1 taken 44 times.
|
45144 | for (idx = 0; idx <= n; ++idx) { |
| 889 | ff_dlog(NULL, "floor0 map: map at pos %d is %"PRId32"\n", idx, map[idx]); | ||
| 890 | } | ||
| 891 | |||
| 892 | 44 | return 0; | |
| 893 | } | ||
| 894 | |||
| 895 | 71 | static int vorbis_parse_setup_hdr_modes(vorbis_context *vc) | |
| 896 | { | ||
| 897 | 71 | GetBitContext *gb = &vc->gb; | |
| 898 | unsigned i; | ||
| 899 | |||
| 900 | 71 | vc->mode_count = get_bits(gb, 6) + 1; | |
| 901 | 71 | vc->modes = av_mallocz(vc->mode_count * sizeof(*vc->modes)); | |
| 902 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 71 times.
|
71 | if (!vc->modes) |
| 903 | ✗ | return AVERROR(ENOMEM); | |
| 904 | |||
| 905 | ff_dlog(NULL, " There are %d modes.\n", vc->mode_count); | ||
| 906 | |||
| 907 |
2/2✓ Branch 0 taken 142 times.
✓ Branch 1 taken 71 times.
|
213 | for (i = 0; i < vc->mode_count; ++i) { |
| 908 | 142 | vorbis_mode *mode_setup = &vc->modes[i]; | |
| 909 | |||
| 910 | 142 | mode_setup->blockflag = get_bits1(gb); | |
| 911 | 142 | mode_setup->windowtype = get_bits(gb, 16); | |
| 912 | 142 | mode_setup->transformtype = get_bits(gb, 16); | |
| 913 |
2/4✓ Branch 0 taken 142 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 142 times.
|
142 | if (mode_setup->transformtype != 0 || mode_setup->windowtype != 0) { |
| 914 | ✗ | av_log(vc->avctx, AV_LOG_WARNING, | |
| 915 | "Invalid mode: windowtype %u, transformtype %u (both must be 0)\n", | ||
| 916 | ✗ | mode_setup->windowtype, mode_setup->transformtype); | |
| 917 | } | ||
| 918 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 142 times.
|
142 | GET_VALIDATED_INDEX(mode_setup->mapping, 8, vc->mapping_count); |
| 919 | |||
| 920 | ff_dlog(NULL, " %u mode: blockflag %d, windowtype %d, transformtype %d, mapping %d\n", | ||
| 921 | i, mode_setup->blockflag, mode_setup->windowtype, | ||
| 922 | mode_setup->transformtype, mode_setup->mapping); | ||
| 923 | } | ||
| 924 | 71 | return 0; | |
| 925 | } | ||
| 926 | |||
| 927 | // Process the whole setup header using the functions above | ||
| 928 | |||
| 929 | 71 | static int vorbis_parse_setup_hdr(vorbis_context *vc) | |
| 930 | { | ||
| 931 | 71 | GetBitContext *gb = &vc->gb; | |
| 932 | int ret; | ||
| 933 | |||
| 934 |
3/6✓ Branch 1 taken 71 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 71 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 71 times.
✗ Branch 7 not taken.
|
142 | if ((get_bits(gb, 8) != 'v') || (get_bits(gb, 8) != 'o') || |
| 935 |
2/4✓ Branch 2 taken 71 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 71 times.
✗ Branch 5 not taken.
|
213 | (get_bits(gb, 8) != 'r') || (get_bits(gb, 8) != 'b') || |
| 936 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 71 times.
|
142 | (get_bits(gb, 8) != 'i') || (get_bits(gb, 8) != 's')) { |
| 937 | ✗ | av_log(vc->avctx, AV_LOG_ERROR, " Vorbis setup header packet corrupt (no vorbis signature). \n"); | |
| 938 | ✗ | return AVERROR_INVALIDDATA; | |
| 939 | } | ||
| 940 | |||
| 941 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 71 times.
|
71 | if ((ret = vorbis_parse_setup_hdr_codebooks(vc))) { |
| 942 | ✗ | av_log(vc->avctx, AV_LOG_ERROR, " Vorbis setup header packet corrupt (codebooks). \n"); | |
| 943 | ✗ | return ret; | |
| 944 | } | ||
| 945 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 71 times.
|
71 | if ((ret = vorbis_parse_setup_hdr_tdtransforms(vc))) { |
| 946 | ✗ | av_log(vc->avctx, AV_LOG_ERROR, " Vorbis setup header packet corrupt (time domain transforms). \n"); | |
| 947 | ✗ | return ret; | |
| 948 | } | ||
| 949 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 71 times.
|
71 | if ((ret = vorbis_parse_setup_hdr_floors(vc))) { |
| 950 | ✗ | av_log(vc->avctx, AV_LOG_ERROR, " Vorbis setup header packet corrupt (floors). \n"); | |
| 951 | ✗ | return ret; | |
| 952 | } | ||
| 953 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 71 times.
|
71 | if ((ret = vorbis_parse_setup_hdr_residues(vc))) { |
| 954 | ✗ | av_log(vc->avctx, AV_LOG_ERROR, " Vorbis setup header packet corrupt (residues). \n"); | |
| 955 | ✗ | return ret; | |
| 956 | } | ||
| 957 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 71 times.
|
71 | if ((ret = vorbis_parse_setup_hdr_mappings(vc))) { |
| 958 | ✗ | av_log(vc->avctx, AV_LOG_ERROR, " Vorbis setup header packet corrupt (mappings). \n"); | |
| 959 | ✗ | return ret; | |
| 960 | } | ||
| 961 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 71 times.
|
71 | if ((ret = vorbis_parse_setup_hdr_modes(vc))) { |
| 962 | ✗ | av_log(vc->avctx, AV_LOG_ERROR, " Vorbis setup header packet corrupt (modes). \n"); | |
| 963 | ✗ | return ret; | |
| 964 | } | ||
| 965 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 71 times.
|
71 | if (!get_bits1(gb)) { |
| 966 | ✗ | av_log(vc->avctx, AV_LOG_ERROR, " Vorbis setup header packet corrupt (framing flag). \n"); | |
| 967 | ✗ | return AVERROR_INVALIDDATA; // framing flag bit unset error | |
| 968 | } | ||
| 969 | |||
| 970 | 71 | return 0; | |
| 971 | } | ||
| 972 | |||
| 973 | // Process the identification header | ||
| 974 | |||
| 975 | 71 | static int vorbis_parse_id_hdr(vorbis_context *vc) | |
| 976 | { | ||
| 977 | 71 | GetBitContext *gb = &vc->gb; | |
| 978 | unsigned bl0, bl1; | ||
| 979 | 71 | float scale = -1.0; | |
| 980 | int ret; | ||
| 981 | |||
| 982 |
3/6✓ Branch 1 taken 71 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 71 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 71 times.
✗ Branch 7 not taken.
|
142 | if ((get_bits(gb, 8) != 'v') || (get_bits(gb, 8) != 'o') || |
| 983 |
2/4✓ Branch 2 taken 71 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 71 times.
✗ Branch 5 not taken.
|
213 | (get_bits(gb, 8) != 'r') || (get_bits(gb, 8) != 'b') || |
| 984 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 71 times.
|
142 | (get_bits(gb, 8) != 'i') || (get_bits(gb, 8) != 's')) { |
| 985 | ✗ | av_log(vc->avctx, AV_LOG_ERROR, " Vorbis id header packet corrupt (no vorbis signature). \n"); | |
| 986 | ✗ | return AVERROR_INVALIDDATA; | |
| 987 | } | ||
| 988 | |||
| 989 | 71 | vc->version = get_bits_long(gb, 32); //FIXME check 0 | |
| 990 | 71 | vc->audio_channels = get_bits(gb, 8); | |
| 991 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 71 times.
|
71 | if (vc->audio_channels <= 0) { |
| 992 | ✗ | av_log(vc->avctx, AV_LOG_ERROR, "Invalid number of channels\n"); | |
| 993 | ✗ | return AVERROR_INVALIDDATA; | |
| 994 | } | ||
| 995 | 71 | vc->audio_samplerate = get_bits_long(gb, 32); | |
| 996 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 71 times.
|
71 | if (vc->audio_samplerate <= 0) { |
| 997 | ✗ | av_log(vc->avctx, AV_LOG_ERROR, "Invalid samplerate\n"); | |
| 998 | ✗ | return AVERROR_INVALIDDATA; | |
| 999 | } | ||
| 1000 | 71 | vc->bitrate_maximum = get_bits_long(gb, 32); | |
| 1001 | 71 | vc->bitrate_nominal = get_bits_long(gb, 32); | |
| 1002 | 71 | vc->bitrate_minimum = get_bits_long(gb, 32); | |
| 1003 | 71 | bl0 = get_bits(gb, 4); | |
| 1004 | 71 | bl1 = get_bits(gb, 4); | |
| 1005 |
5/10✓ Branch 0 taken 71 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 71 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 71 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 71 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 71 times.
|
71 | if (bl0 > 13 || bl0 < 6 || bl1 > 13 || bl1 < 6 || bl1 < bl0) { |
| 1006 | ✗ | av_log(vc->avctx, AV_LOG_ERROR, " Vorbis id header packet corrupt (illegal blocksize). \n"); | |
| 1007 | ✗ | return AVERROR_INVALIDDATA; | |
| 1008 | } | ||
| 1009 | 71 | vc->blocksize[0] = (1 << bl0); | |
| 1010 | 71 | vc->blocksize[1] = (1 << bl1); | |
| 1011 | 71 | vc->win[0] = ff_vorbis_vwin[bl0 - 6]; | |
| 1012 | 71 | vc->win[1] = ff_vorbis_vwin[bl1 - 6]; | |
| 1013 | |||
| 1014 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 71 times.
|
71 | if ((get_bits1(gb)) == 0) { |
| 1015 | ✗ | av_log(vc->avctx, AV_LOG_ERROR, " Vorbis id header packet corrupt (framing flag not set). \n"); | |
| 1016 | ✗ | return AVERROR_INVALIDDATA; | |
| 1017 | } | ||
| 1018 | |||
| 1019 | 71 | vc->channel_residues = av_malloc_array(vc->blocksize[1] / 2, vc->audio_channels * sizeof(*vc->channel_residues)); | |
| 1020 | 71 | vc->saved = av_calloc(vc->blocksize[1] / 4, vc->audio_channels * sizeof(*vc->saved)); | |
| 1021 |
2/4✓ Branch 0 taken 71 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 71 times.
|
71 | if (!vc->channel_residues || !vc->saved) |
| 1022 | ✗ | return AVERROR(ENOMEM); | |
| 1023 | |||
| 1024 | 71 | vc->previous_window = -1; | |
| 1025 | |||
| 1026 | 71 | ret = av_tx_init(&vc->mdct[0], &vc->mdct_fn[0], AV_TX_FLOAT_MDCT, 1, | |
| 1027 | 71 | vc->blocksize[0] >> 1, &scale, 0); | |
| 1028 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 71 times.
|
71 | if (ret < 0) |
| 1029 | ✗ | return ret; | |
| 1030 | |||
| 1031 | 71 | ret = av_tx_init(&vc->mdct[1], &vc->mdct_fn[1], AV_TX_FLOAT_MDCT, 1, | |
| 1032 | 71 | vc->blocksize[1] >> 1, &scale, 0); | |
| 1033 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 71 times.
|
71 | if (ret < 0) |
| 1034 | ✗ | return ret; | |
| 1035 | |||
| 1036 | 71 | vc->fdsp = avpriv_float_dsp_alloc(vc->avctx->flags & AV_CODEC_FLAG_BITEXACT); | |
| 1037 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 71 times.
|
71 | if (!vc->fdsp) |
| 1038 | ✗ | return AVERROR(ENOMEM); | |
| 1039 | |||
| 1040 | ff_dlog(NULL, " vorbis version %"PRIu32" \n audio_channels %"PRIu8" \n audio_samplerate %"PRIu32" \n bitrate_max %"PRIu32" \n bitrate_nom %"PRIu32" \n bitrate_min %"PRIu32" \n blk_0 %"PRIu32" blk_1 %"PRIu32" \n ", | ||
| 1041 | vc->version, vc->audio_channels, vc->audio_samplerate, vc->bitrate_maximum, vc->bitrate_nominal, vc->bitrate_minimum, vc->blocksize[0], vc->blocksize[1]); | ||
| 1042 | |||
| 1043 | /* | ||
| 1044 | BLK = vc->blocksize[0]; | ||
| 1045 | for (i = 0; i < BLK / 2; ++i) { | ||
| 1046 | vc->win[0][i] = sin(0.5*3.14159265358*(sin(((float)i + 0.5) / (float)BLK*3.14159265358))*(sin(((float)i + 0.5) / (float)BLK*3.14159265358))); | ||
| 1047 | } | ||
| 1048 | */ | ||
| 1049 | |||
| 1050 | 71 | return 0; | |
| 1051 | } | ||
| 1052 | |||
| 1053 | // Process the extradata using the functions above (identification header, setup header) | ||
| 1054 | |||
| 1055 | 71 | static av_cold int vorbis_decode_init(AVCodecContext *avctx) | |
| 1056 | { | ||
| 1057 | 71 | vorbis_context *vc = avctx->priv_data; | |
| 1058 | 71 | uint8_t *headers = avctx->extradata; | |
| 1059 | 71 | int headers_len = avctx->extradata_size; | |
| 1060 | const uint8_t *header_start[3]; | ||
| 1061 | int header_len[3]; | ||
| 1062 | 71 | GetBitContext *gb = &vc->gb; | |
| 1063 | int hdr_type, ret; | ||
| 1064 | |||
| 1065 | 71 | vc->avctx = avctx; | |
| 1066 | 71 | ff_vorbisdsp_init(&vc->dsp); | |
| 1067 | |||
| 1068 | 71 | avctx->sample_fmt = AV_SAMPLE_FMT_FLTP; | |
| 1069 | |||
| 1070 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 71 times.
|
71 | if (!headers_len) { |
| 1071 | ✗ | av_log(avctx, AV_LOG_ERROR, "Extradata missing.\n"); | |
| 1072 | ✗ | return AVERROR_INVALIDDATA; | |
| 1073 | } | ||
| 1074 | |||
| 1075 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 71 times.
|
71 | if ((ret = avpriv_split_xiph_headers(headers, headers_len, 30, header_start, header_len)) < 0) { |
| 1076 | ✗ | av_log(avctx, AV_LOG_ERROR, "Extradata corrupt.\n"); | |
| 1077 | ✗ | return ret; | |
| 1078 | } | ||
| 1079 | |||
| 1080 | 71 | init_get_bits(gb, header_start[0], header_len[0]*8); | |
| 1081 | 71 | hdr_type = get_bits(gb, 8); | |
| 1082 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 71 times.
|
71 | if (hdr_type != 1) { |
| 1083 | ✗ | av_log(avctx, AV_LOG_ERROR, "First header is not the id header.\n"); | |
| 1084 | ✗ | return AVERROR_INVALIDDATA; | |
| 1085 | } | ||
| 1086 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 71 times.
|
71 | if ((ret = vorbis_parse_id_hdr(vc))) { |
| 1087 | ✗ | av_log(avctx, AV_LOG_ERROR, "Id header corrupt.\n"); | |
| 1088 | ✗ | vorbis_free(vc); | |
| 1089 | ✗ | return ret; | |
| 1090 | } | ||
| 1091 | |||
| 1092 | 71 | init_get_bits(gb, header_start[2], header_len[2]*8); | |
| 1093 | 71 | hdr_type = get_bits(gb, 8); | |
| 1094 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 71 times.
|
71 | if (hdr_type != 5) { |
| 1095 | ✗ | av_log(avctx, AV_LOG_ERROR, "Third header is not the setup header.\n"); | |
| 1096 | ✗ | vorbis_free(vc); | |
| 1097 | ✗ | return AVERROR_INVALIDDATA; | |
| 1098 | } | ||
| 1099 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 71 times.
|
71 | if ((ret = vorbis_parse_setup_hdr(vc))) { |
| 1100 | ✗ | av_log(avctx, AV_LOG_ERROR, "Setup header corrupt.\n"); | |
| 1101 | ✗ | vorbis_free(vc); | |
| 1102 | ✗ | return ret; | |
| 1103 | } | ||
| 1104 | |||
| 1105 | 71 | av_channel_layout_uninit(&avctx->ch_layout); | |
| 1106 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 71 times.
|
71 | if (vc->audio_channels > 8) { |
| 1107 | ✗ | avctx->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC; | |
| 1108 | ✗ | avctx->ch_layout.nb_channels = vc->audio_channels; | |
| 1109 | } else { | ||
| 1110 | 71 | av_channel_layout_copy(&avctx->ch_layout, &ff_vorbis_ch_layouts[vc->audio_channels - 1]); | |
| 1111 | } | ||
| 1112 | |||
| 1113 | 71 | avctx->sample_rate = vc->audio_samplerate; | |
| 1114 | |||
| 1115 | 71 | return 0; | |
| 1116 | } | ||
| 1117 | |||
| 1118 | // Decode audiopackets ------------------------------------------------- | ||
| 1119 | |||
| 1120 | // Read and decode floor | ||
| 1121 | |||
| 1122 | 6375 | static int vorbis_floor0_decode(vorbis_context *vc, | |
| 1123 | vorbis_floor_data *vfu, float *vec) | ||
| 1124 | { | ||
| 1125 | 6375 | vorbis_floor0 *vf = &vfu->t0; | |
| 1126 | 6375 | float *lsp = vf->lsp; | |
| 1127 | unsigned book_idx; | ||
| 1128 | uint64_t amplitude; | ||
| 1129 | 6375 | unsigned blockflag = vc->modes[vc->mode_number].blockflag; | |
| 1130 | |||
| 1131 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6375 times.
|
6375 | if (!vf->amplitude_bits) |
| 1132 | ✗ | return 1; | |
| 1133 | |||
| 1134 | 6375 | amplitude = get_bits64(&vc->gb, vf->amplitude_bits); | |
| 1135 |
2/2✓ Branch 0 taken 6251 times.
✓ Branch 1 taken 124 times.
|
6375 | if (amplitude > 0) { |
| 1136 | 6251 | float last = 0; | |
| 1137 | 6251 | unsigned idx, lsp_len = 0; | |
| 1138 | vorbis_codebook codebook; | ||
| 1139 | |||
| 1140 | 6251 | book_idx = get_bits(&vc->gb, ilog(vf->num_books)); | |
| 1141 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6251 times.
|
6251 | if (book_idx >= vf->num_books) { |
| 1142 | ✗ | av_log(vc->avctx, AV_LOG_ERROR, "floor0 dec: booknumber too high!\n"); | |
| 1143 | ✗ | book_idx = 0; | |
| 1144 | } | ||
| 1145 | ff_dlog(NULL, "floor0 dec: booknumber: %u\n", book_idx); | ||
| 1146 | 6251 | codebook = vc->codebooks[vf->book_list[book_idx]]; | |
| 1147 | /* Invalid codebook! */ | ||
| 1148 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6251 times.
|
6251 | if (!codebook.codevectors) |
| 1149 | ✗ | return AVERROR_INVALIDDATA; | |
| 1150 | |||
| 1151 |
2/2✓ Branch 0 taken 50348 times.
✓ Branch 1 taken 6251 times.
|
56599 | while (lsp_len<vf->order) { |
| 1152 | int vec_off; | ||
| 1153 | |||
| 1154 | ff_dlog(NULL, "floor0 dec: book dimension: %d\n", codebook.dimensions); | ||
| 1155 | ff_dlog(NULL, "floor0 dec: maximum depth: %d\n", codebook.maxdepth); | ||
| 1156 | /* read temp vector */ | ||
| 1157 | 50348 | vec_off = get_vlc2(&vc->gb, codebook.vlc.table, | |
| 1158 | 50348 | codebook.nb_bits, 3); | |
| 1159 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 50348 times.
|
50348 | if (vec_off < 0) |
| 1160 | ✗ | return AVERROR_INVALIDDATA; | |
| 1161 | 50348 | vec_off *= codebook.dimensions; | |
| 1162 | ff_dlog(NULL, "floor0 dec: vector offset: %d\n", vec_off); | ||
| 1163 | /* copy each vector component and add last to it */ | ||
| 1164 |
2/2✓ Branch 0 taken 145572 times.
✓ Branch 1 taken 50348 times.
|
195920 | for (idx = 0; idx < codebook.dimensions; ++idx) |
| 1165 | 145572 | lsp[lsp_len+idx] = codebook.codevectors[vec_off+idx] + last; | |
| 1166 | 50348 | last = lsp[lsp_len+idx-1]; /* set last to last vector component */ | |
| 1167 | |||
| 1168 | 50348 | lsp_len += codebook.dimensions; | |
| 1169 | } | ||
| 1170 | /* DEBUG: output lsp coeffs */ | ||
| 1171 | { | ||
| 1172 | int idx; | ||
| 1173 |
2/2✓ Branch 0 taken 145572 times.
✓ Branch 1 taken 6251 times.
|
151823 | for (idx = 0; idx < lsp_len; ++idx) |
| 1174 | ff_dlog(NULL, "floor0 dec: coeff at %d is %f\n", idx, lsp[idx]); | ||
| 1175 | } | ||
| 1176 | |||
| 1177 | /* synthesize floor output vector */ | ||
| 1178 | { | ||
| 1179 | int i; | ||
| 1180 | 6251 | int order = vf->order; | |
| 1181 | 6251 | float wstep = M_PI / vf->bark_map_size; | |
| 1182 | |||
| 1183 |
2/2✓ Branch 0 taken 145572 times.
✓ Branch 1 taken 6251 times.
|
151823 | for (i = 0; i < order; i++) |
| 1184 | 145572 | lsp[i] = 2.0f * cos(lsp[i]); | |
| 1185 | |||
| 1186 | ff_dlog(NULL, "floor0 synth: map_size = %"PRIu32"; m = %d; wstep = %f\n", | ||
| 1187 | vf->map_size[blockflag], order, wstep); | ||
| 1188 | |||
| 1189 | 6251 | i = 0; | |
| 1190 |
2/2✓ Branch 0 taken 939090 times.
✓ Branch 1 taken 6251 times.
|
945341 | while (i < vf->map_size[blockflag]) { |
| 1191 | 939090 | int j, iter_cond = vf->map[blockflag][i]; | |
| 1192 | 939090 | float p = 0.5f; | |
| 1193 | 939090 | float q = 0.5f; | |
| 1194 | 939090 | float two_cos_w = 2.0f * cos(wstep * iter_cond); // needed all times | |
| 1195 | |||
| 1196 | /* similar part for the q and p products */ | ||
| 1197 |
2/2✓ Branch 0 taken 13168596 times.
✓ Branch 1 taken 939090 times.
|
14107686 | for (j = 0; j + 1 < order; j += 2) { |
| 1198 | 13168596 | q *= lsp[j] - two_cos_w; | |
| 1199 | 13168596 | p *= lsp[j + 1] - two_cos_w; | |
| 1200 | } | ||
| 1201 |
2/2✓ Branch 0 taken 925416 times.
✓ Branch 1 taken 13674 times.
|
939090 | if (j == order) { // even order |
| 1202 | 925416 | p *= p * (2.0f - two_cos_w); | |
| 1203 | 925416 | q *= q * (2.0f + two_cos_w); | |
| 1204 | } else { // odd order | ||
| 1205 | 13674 | q *= two_cos_w-lsp[j]; // one more time for q | |
| 1206 | |||
| 1207 | /* final step and square */ | ||
| 1208 | 13674 | p *= p * (4.f - two_cos_w * two_cos_w); | |
| 1209 | 13674 | q *= q; | |
| 1210 | } | ||
| 1211 | |||
| 1212 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 939090 times.
|
939090 | if (p + q == 0.0) |
| 1213 | ✗ | return AVERROR_INVALIDDATA; | |
| 1214 | |||
| 1215 | /* calculate linear floor value */ | ||
| 1216 | 939090 | q = exp((((amplitude*vf->amplitude_offset) / | |
| 1217 | 939090 | (((1ULL << vf->amplitude_bits) - 1) * sqrt(p + q))) | |
| 1218 | 939090 | - vf->amplitude_offset) * .11512925f); | |
| 1219 | |||
| 1220 | /* fill vector */ | ||
| 1221 | do { | ||
| 1222 | 4384000 | vec[i] = q; ++i; | |
| 1223 |
2/2✓ Branch 0 taken 3444910 times.
✓ Branch 1 taken 939090 times.
|
4384000 | } while (vf->map[blockflag][i] == iter_cond); |
| 1224 | } | ||
| 1225 | } | ||
| 1226 | } else { | ||
| 1227 | /* this channel is unused */ | ||
| 1228 | 124 | return 1; | |
| 1229 | } | ||
| 1230 | |||
| 1231 | ff_dlog(NULL, " Floor0 decoded\n"); | ||
| 1232 | |||
| 1233 | 6251 | return 0; | |
| 1234 | } | ||
| 1235 | |||
| 1236 | 11478 | static int vorbis_floor1_decode(vorbis_context *vc, | |
| 1237 | vorbis_floor_data *vfu, float *vec) | ||
| 1238 | { | ||
| 1239 | 11478 | vorbis_floor1 *vf = &vfu->t1; | |
| 1240 | 11478 | GetBitContext *gb = &vc->gb; | |
| 1241 | 11478 | uint16_t range_v[4] = { 256, 128, 86, 64 }; | |
| 1242 | 11478 | unsigned range = range_v[vf->multiplier - 1]; | |
| 1243 | uint16_t floor1_Y[258]; | ||
| 1244 | uint16_t floor1_Y_final[258]; | ||
| 1245 | int floor1_flag[258]; | ||
| 1246 | unsigned partition_class, cdim, cbits, csub, cval, offset, i, j; | ||
| 1247 | int book, adx, ady, dy, off, predicted, err; | ||
| 1248 | |||
| 1249 | |||
| 1250 |
2/2✓ Branch 1 taken 2001 times.
✓ Branch 2 taken 9477 times.
|
11478 | if (!get_bits1(gb)) // silence |
| 1251 | 2001 | return 1; | |
| 1252 | |||
| 1253 | // Read values (or differences) for the floor's points | ||
| 1254 | |||
| 1255 | 9477 | floor1_Y[0] = get_bits(gb, ilog(range - 1)); | |
| 1256 | 9477 | floor1_Y[1] = get_bits(gb, ilog(range - 1)); | |
| 1257 | |||
| 1258 | ff_dlog(NULL, "floor 0 Y %d floor 1 Y %d \n", floor1_Y[0], floor1_Y[1]); | ||
| 1259 | |||
| 1260 | 9477 | offset = 2; | |
| 1261 |
2/2✓ Branch 0 taken 68865 times.
✓ Branch 1 taken 9477 times.
|
78342 | for (i = 0; i < vf->partitions; ++i) { |
| 1262 | 68865 | partition_class = vf->partition_class[i]; | |
| 1263 | 68865 | cdim = vf->class_dimensions[partition_class]; | |
| 1264 | 68865 | cbits = vf->class_subclasses[partition_class]; | |
| 1265 | 68865 | csub = (1 << cbits) - 1; | |
| 1266 | 68865 | cval = 0; | |
| 1267 | |||
| 1268 | ff_dlog(NULL, "Cbits %u\n", cbits); | ||
| 1269 | |||
| 1270 |
2/2✓ Branch 0 taken 62935 times.
✓ Branch 1 taken 5930 times.
|
68865 | if (cbits) // this reads all subclasses for this partition's class |
| 1271 | 62935 | cval = get_vlc2(gb, vc->codebooks[vf->class_masterbook[partition_class]].vlc.table, | |
| 1272 | 62935 | vc->codebooks[vf->class_masterbook[partition_class]].nb_bits, 3); | |
| 1273 | |||
| 1274 |
2/2✓ Branch 0 taken 216695 times.
✓ Branch 1 taken 68865 times.
|
285560 | for (j = 0; j < cdim; ++j) { |
| 1275 | 216695 | book = vf->subclass_books[partition_class][cval & csub]; | |
| 1276 | |||
| 1277 | ff_dlog(NULL, "book %d Cbits %u cval %u bits:%d\n", | ||
| 1278 | book, cbits, cval, get_bits_count(gb)); | ||
| 1279 | |||
| 1280 | 216695 | cval = cval >> cbits; | |
| 1281 |
2/2✓ Branch 0 taken 114081 times.
✓ Branch 1 taken 102614 times.
|
216695 | if (book > -1) { |
| 1282 | 114081 | int v = get_vlc2(gb, vc->codebooks[book].vlc.table, | |
| 1283 | 114081 | vc->codebooks[book].nb_bits, 3); | |
| 1284 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 114081 times.
|
114081 | if (v < 0) |
| 1285 | ✗ | return AVERROR_INVALIDDATA; | |
| 1286 | 114081 | floor1_Y[offset+j] = v; | |
| 1287 | } else { | ||
| 1288 | 102614 | floor1_Y[offset+j] = 0; | |
| 1289 | } | ||
| 1290 | |||
| 1291 | ff_dlog(NULL, " floor(%d) = %d \n", | ||
| 1292 | vf->list[offset+j].x, floor1_Y[offset+j]); | ||
| 1293 | } | ||
| 1294 | 68865 | offset+=cdim; | |
| 1295 | } | ||
| 1296 | |||
| 1297 | // Amplitude calculation from the differences | ||
| 1298 | |||
| 1299 | 9477 | floor1_flag[0] = 1; | |
| 1300 | 9477 | floor1_flag[1] = 1; | |
| 1301 | 9477 | floor1_Y_final[0] = floor1_Y[0]; | |
| 1302 | 9477 | floor1_Y_final[1] = floor1_Y[1]; | |
| 1303 | |||
| 1304 |
2/2✓ Branch 0 taken 216695 times.
✓ Branch 1 taken 9477 times.
|
226172 | for (i = 2; i < vf->x_list_dim; ++i) { |
| 1305 | unsigned val, highroom, lowroom, room, high_neigh_offs, low_neigh_offs; | ||
| 1306 | |||
| 1307 | 216695 | low_neigh_offs = vf->list[i].low; | |
| 1308 | 216695 | high_neigh_offs = vf->list[i].high; | |
| 1309 | 216695 | dy = floor1_Y_final[high_neigh_offs] - floor1_Y_final[low_neigh_offs]; // render_point begin | |
| 1310 | 216695 | adx = vf->list[high_neigh_offs].x - vf->list[low_neigh_offs].x; | |
| 1311 | 216695 | ady = FFABS(dy); | |
| 1312 | 216695 | err = ady * (vf->list[i].x - vf->list[low_neigh_offs].x); | |
| 1313 | 216695 | off = err / adx; | |
| 1314 |
2/2✓ Branch 0 taken 119671 times.
✓ Branch 1 taken 97024 times.
|
216695 | if (dy < 0) { |
| 1315 | 119671 | predicted = floor1_Y_final[low_neigh_offs] - off; | |
| 1316 | } else { | ||
| 1317 | 97024 | predicted = floor1_Y_final[low_neigh_offs] + off; | |
| 1318 | } // render_point end | ||
| 1319 | |||
| 1320 | 216695 | val = floor1_Y[i]; | |
| 1321 | 216695 | highroom = range-predicted; | |
| 1322 | 216695 | lowroom = predicted; | |
| 1323 |
2/2✓ Branch 0 taken 88049 times.
✓ Branch 1 taken 128646 times.
|
216695 | if (highroom < lowroom) { |
| 1324 | 88049 | room = highroom * 2; | |
| 1325 | } else { | ||
| 1326 | 128646 | room = lowroom * 2; // SPEC misspelling | |
| 1327 | } | ||
| 1328 |
2/2✓ Branch 0 taken 98124 times.
✓ Branch 1 taken 118571 times.
|
216695 | if (val) { |
| 1329 | 98124 | floor1_flag[low_neigh_offs] = 1; | |
| 1330 | 98124 | floor1_flag[high_neigh_offs] = 1; | |
| 1331 | 98124 | floor1_flag[i] = 1; | |
| 1332 |
2/2✓ Branch 0 taken 123 times.
✓ Branch 1 taken 98001 times.
|
98124 | if (val >= room) { |
| 1333 |
2/2✓ Branch 0 taken 80 times.
✓ Branch 1 taken 43 times.
|
123 | if (highroom > lowroom) { |
| 1334 | 80 | floor1_Y_final[i] = av_clip_uint16(val - lowroom + predicted); | |
| 1335 | } else { | ||
| 1336 | 43 | floor1_Y_final[i] = av_clip_uint16(predicted - val + highroom - 1); | |
| 1337 | } | ||
| 1338 | } else { | ||
| 1339 |
2/2✓ Branch 0 taken 63615 times.
✓ Branch 1 taken 34386 times.
|
98001 | if (val & 1) { |
| 1340 | 63615 | floor1_Y_final[i] = av_clip_uint16(predicted - (val + 1) / 2); | |
| 1341 | } else { | ||
| 1342 | 34386 | floor1_Y_final[i] = av_clip_uint16(predicted + val / 2); | |
| 1343 | } | ||
| 1344 | } | ||
| 1345 | } else { | ||
| 1346 | 118571 | floor1_flag[i] = 0; | |
| 1347 | 118571 | floor1_Y_final[i] = av_clip_uint16(predicted); | |
| 1348 | } | ||
| 1349 | |||
| 1350 | ff_dlog(NULL, " Decoded floor(%d) = %u / val %u\n", | ||
| 1351 | vf->list[i].x, floor1_Y_final[i], val); | ||
| 1352 | } | ||
| 1353 | |||
| 1354 | // Curve synth - connect the calculated dots and convert from dB scale FIXME optimize ? | ||
| 1355 | |||
| 1356 | 9477 | ff_vorbis_floor1_render_list(vf->list, vf->x_list_dim, floor1_Y_final, floor1_flag, vf->multiplier, vec, vf->list[1].x); | |
| 1357 | |||
| 1358 | ff_dlog(NULL, " Floor decoded\n"); | ||
| 1359 | |||
| 1360 | 9477 | return 0; | |
| 1361 | } | ||
| 1362 | |||
| 1363 | 134699 | static av_always_inline int setup_classifs(vorbis_context *vc, | |
| 1364 | vorbis_residue *vr, | ||
| 1365 | uint8_t *do_not_decode, | ||
| 1366 | unsigned ch_used, | ||
| 1367 | int partition_count, | ||
| 1368 | int ptns_to_read | ||
| 1369 | ) | ||
| 1370 | { | ||
| 1371 | 134699 | vorbis_codebook *codebook = vc->codebooks + vr->classbook; | |
| 1372 | int p, j, i; | ||
| 1373 | 134699 | unsigned c_p_c = codebook->dimensions; | |
| 1374 | 134699 | unsigned inverse_class = ff_inverse[vr->classifications]; | |
| 1375 | int temp, temp2; | ||
| 1376 |
2/2✓ Branch 0 taken 163952 times.
✓ Branch 1 taken 134699 times.
|
298651 | for (p = 0, j = 0; j < ch_used; ++j) { |
| 1377 |
2/2✓ Branch 0 taken 162631 times.
✓ Branch 1 taken 1321 times.
|
163952 | if (!do_not_decode[j]) { |
| 1378 | 162631 | temp = get_vlc2(&vc->gb, codebook->vlc.table, | |
| 1379 | 162631 | codebook->nb_bits, 3); | |
| 1380 | |||
| 1381 | ff_dlog(NULL, "Classword: %u\n", temp); | ||
| 1382 | |||
| 1383 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 162631 times.
|
162631 | av_assert0(temp < 65536); |
| 1384 | |||
| 1385 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 162631 times.
|
162631 | if (temp < 0) { |
| 1386 | ✗ | av_log(vc->avctx, AV_LOG_ERROR, | |
| 1387 | "Invalid vlc code decoding %d channel.", j); | ||
| 1388 | ✗ | return AVERROR_INVALIDDATA; | |
| 1389 | } | ||
| 1390 | |||
| 1391 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 162631 times.
|
162631 | if (vr->classifications == 1) { |
| 1392 | ✗ | for (i = partition_count + c_p_c - 1; i >= partition_count; i--) { | |
| 1393 | ✗ | if (i < ptns_to_read) | |
| 1394 | ✗ | vr->classifs[p + i] = 0; | |
| 1395 | } | ||
| 1396 | } else { | ||
| 1397 |
2/2✓ Branch 0 taken 344379 times.
✓ Branch 1 taken 162631 times.
|
507010 | for (i = partition_count + c_p_c - 1; i >= partition_count; i--) { |
| 1398 | 344379 | temp2 = (((uint64_t)temp) * inverse_class) >> 32; | |
| 1399 | |||
| 1400 |
2/2✓ Branch 0 taken 344148 times.
✓ Branch 1 taken 231 times.
|
344379 | if (i < ptns_to_read) |
| 1401 | 344148 | vr->classifs[p + i] = temp - temp2 * vr->classifications; | |
| 1402 | 344379 | temp = temp2; | |
| 1403 | } | ||
| 1404 | } | ||
| 1405 | } | ||
| 1406 | 163952 | p += ptns_to_read; | |
| 1407 | } | ||
| 1408 | 134699 | return 0; | |
| 1409 | } | ||
| 1410 | // Read and decode residue | ||
| 1411 | |||
| 1412 | 9510 | static av_always_inline int vorbis_residue_decode_internal(vorbis_context *vc, | |
| 1413 | vorbis_residue *vr, | ||
| 1414 | unsigned ch, | ||
| 1415 | uint8_t *do_not_decode, | ||
| 1416 | float *vec, | ||
| 1417 | unsigned vlen, | ||
| 1418 | unsigned ch_left, | ||
| 1419 | int vr_type) | ||
| 1420 | { | ||
| 1421 | 9510 | GetBitContext *gb = &vc->gb; | |
| 1422 | 9510 | unsigned c_p_c = vc->codebooks[vr->classbook].dimensions; | |
| 1423 | 9510 | uint8_t *classifs = vr->classifs; | |
| 1424 | unsigned pass, ch_used, i, j, k, l; | ||
| 1425 | 9510 | unsigned max_output = (ch - 1) * vlen; | |
| 1426 | 9510 | int ptns_to_read = vr->ptns_to_read; | |
| 1427 | 9510 | int libvorbis_bug = 0; | |
| 1428 | |||
| 1429 |
2/2✓ Branch 0 taken 4275 times.
✓ Branch 1 taken 5235 times.
|
9510 | if (vr_type == 2) { |
| 1430 |
2/2✓ Branch 0 taken 5388 times.
✓ Branch 1 taken 4275 times.
|
9663 | for (j = 1; j < ch; ++j) |
| 1431 | 5388 | do_not_decode[0] &= do_not_decode[j]; // FIXME - clobbering input | |
| 1432 |
2/2✓ Branch 0 taken 120 times.
✓ Branch 1 taken 4155 times.
|
4275 | if (do_not_decode[0]) |
| 1433 | 120 | return 0; | |
| 1434 | 4155 | ch_used = 1; | |
| 1435 | 4155 | max_output += vr->end / ch; | |
| 1436 | } else { | ||
| 1437 | 5235 | ch_used = ch; | |
| 1438 | 5235 | max_output += vr->end; | |
| 1439 | } | ||
| 1440 | |||
| 1441 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9390 times.
|
9390 | if (max_output > ch_left * vlen) { |
| 1442 | ✗ | if (max_output <= ch_left * vlen + vr->partition_size*ch_used/ch) { | |
| 1443 | ✗ | ptns_to_read--; | |
| 1444 | ✗ | libvorbis_bug = 1; | |
| 1445 | } else { | ||
| 1446 | ✗ | av_log(vc->avctx, AV_LOG_ERROR, "Insufficient output buffer\n"); | |
| 1447 | ✗ | return AVERROR_INVALIDDATA; | |
| 1448 | } | ||
| 1449 | } | ||
| 1450 | |||
| 1451 | ff_dlog(NULL, " residue type 0/1/2 decode begin, ch: %d cpc %d \n", ch, c_p_c); | ||
| 1452 | |||
| 1453 |
2/2✓ Branch 0 taken 21649 times.
✓ Branch 1 taken 9390 times.
|
31039 | for (pass = 0; pass <= vr->maxpass; ++pass) { // FIXME OPTIMIZE? |
| 1454 | int voffset, partition_count, j_times_ptns_to_read; | ||
| 1455 | |||
| 1456 | 21649 | voffset = vr->begin; | |
| 1457 |
2/2✓ Branch 0 taken 350086 times.
✓ Branch 1 taken 21649 times.
|
371735 | for (partition_count = 0; partition_count < ptns_to_read;) { // SPEC error |
| 1458 |
2/2✓ Branch 0 taken 134699 times.
✓ Branch 1 taken 215387 times.
|
350086 | if (!pass) { |
| 1459 | 134699 | int ret = setup_classifs(vc, vr, do_not_decode, ch_used, partition_count, ptns_to_read); | |
| 1460 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 134699 times.
|
134699 | if (ret < 0) |
| 1461 | ✗ | return ret; | |
| 1462 | } | ||
| 1463 |
4/4✓ Branch 0 taken 736397 times.
✓ Branch 1 taken 349393 times.
✓ Branch 2 taken 735704 times.
✓ Branch 3 taken 693 times.
|
1085790 | for (i = 0; (i < c_p_c) && (partition_count < ptns_to_read); ++i) { |
| 1464 |
2/2✓ Branch 0 taken 798242 times.
✓ Branch 1 taken 735704 times.
|
1533946 | for (j_times_ptns_to_read = 0, j = 0; j < ch_used; ++j) { |
| 1465 | unsigned voffs; | ||
| 1466 | |||
| 1467 |
2/2✓ Branch 0 taken 794226 times.
✓ Branch 1 taken 4016 times.
|
798242 | if (!do_not_decode[j]) { |
| 1468 | 794226 | unsigned vqclass = classifs[j_times_ptns_to_read + partition_count]; | |
| 1469 | 794226 | int vqbook = vr->books[vqclass][pass]; | |
| 1470 | |||
| 1471 |
3/4✓ Branch 0 taken 321825 times.
✓ Branch 1 taken 472401 times.
✓ Branch 2 taken 321825 times.
✗ Branch 3 not taken.
|
794226 | if (vqbook >= 0 && vc->codebooks[vqbook].codevectors) { |
| 1472 | int coffs; | ||
| 1473 | 321825 | unsigned dim = vc->codebooks[vqbook].dimensions; | |
| 1474 | 321825 | unsigned step = FASTDIV(vr->partition_size << 1, dim << 1); | |
| 1475 | 321825 | vorbis_codebook codebook = vc->codebooks[vqbook]; | |
| 1476 | |||
| 1477 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 321825 times.
|
321825 | if (get_bits_left(gb) < 0) { |
| 1478 | ✗ | av_log(vc->avctx, AV_LOG_ERROR, "Overread %d bits\n", -get_bits_left(gb)); | |
| 1479 | ✗ | return 0; | |
| 1480 | } | ||
| 1481 | |||
| 1482 |
2/2✓ Branch 0 taken 115082 times.
✓ Branch 1 taken 206743 times.
|
321825 | if (vr_type == 0) { |
| 1483 | |||
| 1484 | 115082 | voffs = voffset+j*vlen; | |
| 1485 |
2/2✓ Branch 0 taken 1242824 times.
✓ Branch 1 taken 115082 times.
|
1357906 | for (k = 0; k < step; ++k) { |
| 1486 | 1242824 | coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3); | |
| 1487 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1242824 times.
|
1242824 | if (coffs < 0) |
| 1488 | ✗ | return coffs; | |
| 1489 | 1242824 | coffs *= dim; | |
| 1490 |
2/2✓ Branch 0 taken 3535904 times.
✓ Branch 1 taken 1242824 times.
|
4778728 | for (l = 0; l < dim; ++l) |
| 1491 | 3535904 | vec[voffs + k + l * step] += codebook.codevectors[coffs + l]; | |
| 1492 | } | ||
| 1493 |
2/2✓ Branch 0 taken 27784 times.
✓ Branch 1 taken 178959 times.
|
206743 | } else if (vr_type == 1) { |
| 1494 | 27784 | voffs = voffset + j * vlen; | |
| 1495 |
2/2✓ Branch 0 taken 287154 times.
✓ Branch 1 taken 27784 times.
|
314938 | for (k = 0; k < step; ++k) { |
| 1496 | 287154 | coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3); | |
| 1497 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 287154 times.
|
287154 | if (coffs < 0) |
| 1498 | ✗ | return coffs; | |
| 1499 | 287154 | coffs *= dim; | |
| 1500 |
2/2✓ Branch 0 taken 791460 times.
✓ Branch 1 taken 287154 times.
|
1078614 | for (l = 0; l < dim; ++l, ++voffs) { |
| 1501 | 791460 | vec[voffs]+=codebook.codevectors[coffs+l]; | |
| 1502 | |||
| 1503 | ff_dlog(NULL, " pass %d offs: %d curr: %f change: %f cv offs.: %d \n", | ||
| 1504 | pass, voffs, vec[voffs], codebook.codevectors[coffs+l], coffs); | ||
| 1505 | } | ||
| 1506 | } | ||
| 1507 |
5/8✓ Branch 0 taken 178959 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 151234 times.
✓ Branch 3 taken 27725 times.
✓ Branch 4 taken 151234 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 151234 times.
✗ Branch 7 not taken.
|
178959 | } else if (vr_type == 2 && ch == 2 && (voffset & 1) == 0 && (dim & 1) == 0) { // most frequent case optimized |
| 1508 | 151234 | voffs = voffset >> 1; | |
| 1509 | |||
| 1510 |
2/2✓ Branch 0 taken 64528 times.
✓ Branch 1 taken 86706 times.
|
151234 | if (dim == 2) { |
| 1511 |
2/2✓ Branch 0 taken 1004556 times.
✓ Branch 1 taken 64528 times.
|
1069084 | for (k = 0; k < step; ++k) { |
| 1512 | 1004556 | coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3); | |
| 1513 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1004556 times.
|
1004556 | if (coffs < 0) |
| 1514 | ✗ | return coffs; | |
| 1515 | 1004556 | coffs *= 2; | |
| 1516 | 1004556 | vec[voffs + k ] += codebook.codevectors[coffs ]; | |
| 1517 | 1004556 | vec[voffs + k + vlen] += codebook.codevectors[coffs + 1]; | |
| 1518 | } | ||
| 1519 |
2/2✓ Branch 0 taken 63074 times.
✓ Branch 1 taken 23632 times.
|
86706 | } else if (dim == 4) { |
| 1520 |
2/2✓ Branch 0 taken 447102 times.
✓ Branch 1 taken 63074 times.
|
510176 | for (k = 0; k < step; ++k, voffs += 2) { |
| 1521 | 447102 | coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3); | |
| 1522 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 447102 times.
|
447102 | if (coffs < 0) |
| 1523 | ✗ | return coffs; | |
| 1524 | 447102 | coffs *= 4; | |
| 1525 | 447102 | vec[voffs ] += codebook.codevectors[coffs ]; | |
| 1526 | 447102 | vec[voffs + 1 ] += codebook.codevectors[coffs + 2]; | |
| 1527 | 447102 | vec[voffs + vlen ] += codebook.codevectors[coffs + 1]; | |
| 1528 | 447102 | vec[voffs + vlen + 1] += codebook.codevectors[coffs + 3]; | |
| 1529 | } | ||
| 1530 | } else | ||
| 1531 |
2/2✓ Branch 0 taken 94166 times.
✓ Branch 1 taken 23632 times.
|
117798 | for (k = 0; k < step; ++k) { |
| 1532 | 94166 | coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3); | |
| 1533 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 94166 times.
|
94166 | if (coffs < 0) |
| 1534 | ✗ | return coffs; | |
| 1535 | 94166 | coffs *= dim; | |
| 1536 |
2/2✓ Branch 0 taken 376664 times.
✓ Branch 1 taken 94166 times.
|
470830 | for (l = 0; l < dim; l += 2, voffs++) { |
| 1537 | 376664 | vec[voffs ] += codebook.codevectors[coffs + l ]; | |
| 1538 | 376664 | vec[voffs + vlen] += codebook.codevectors[coffs + l + 1]; | |
| 1539 | |||
| 1540 | ff_dlog(NULL, " pass %d offs: %d curr: %f change: %f cv offs.: %d+%d \n", | ||
| 1541 | pass, voffset / ch + (voffs % ch) * vlen, | ||
| 1542 | vec[voffset / ch + (voffs % ch) * vlen], | ||
| 1543 | codebook.codevectors[coffs + l], coffs, l); | ||
| 1544 | } | ||
| 1545 | } | ||
| 1546 | |||
| 1547 |
1/2✓ Branch 0 taken 27725 times.
✗ Branch 1 not taken.
|
27725 | } else if (vr_type == 2) { |
| 1548 |
1/2✓ Branch 0 taken 27725 times.
✗ Branch 1 not taken.
|
27725 | unsigned voffs_div = ch == 1 ? voffset : FASTDIV(voffset, ch); |
| 1549 | 27725 | unsigned voffs_mod = voffset - voffs_div * ch; | |
| 1550 | |||
| 1551 |
2/2✓ Branch 0 taken 164109 times.
✓ Branch 1 taken 27725 times.
|
191834 | for (k = 0; k < step; ++k) { |
| 1552 | 164109 | coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3); | |
| 1553 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 164109 times.
|
164109 | if (coffs < 0) |
| 1554 | ✗ | return coffs; | |
| 1555 | 164109 | coffs *= dim; | |
| 1556 |
2/2✓ Branch 0 taken 748485 times.
✓ Branch 1 taken 164109 times.
|
912594 | for (l = 0; l < dim; ++l) { |
| 1557 | 748485 | vec[voffs_div + voffs_mod * vlen] += | |
| 1558 | 748485 | codebook.codevectors[coffs + l]; | |
| 1559 | |||
| 1560 | ff_dlog(NULL, " pass %d offs: %d curr: %f change: %f cv offs.: %d+%d \n", | ||
| 1561 | pass, voffs_div + voffs_mod * vlen, | ||
| 1562 | vec[voffs_div + voffs_mod * vlen], | ||
| 1563 | codebook.codevectors[coffs + l], coffs, l); | ||
| 1564 | |||
| 1565 |
2/2✓ Branch 0 taken 149697 times.
✓ Branch 1 taken 598788 times.
|
748485 | if (++voffs_mod == ch) { |
| 1566 | 149697 | voffs_div++; | |
| 1567 | 149697 | voffs_mod = 0; | |
| 1568 | } | ||
| 1569 | } | ||
| 1570 | } | ||
| 1571 | } | ||
| 1572 | } | ||
| 1573 | } | ||
| 1574 | 798242 | j_times_ptns_to_read += ptns_to_read; | |
| 1575 | } | ||
| 1576 | 735704 | ++partition_count; | |
| 1577 | 735704 | voffset += vr->partition_size; | |
| 1578 | } | ||
| 1579 | } | ||
| 1580 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 21649 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
21649 | if (libvorbis_bug && !pass) { |
| 1581 | ✗ | for (j = 0; j < ch_used; ++j) { | |
| 1582 | ✗ | if (!do_not_decode[j]) { | |
| 1583 | ✗ | get_vlc2(&vc->gb, vc->codebooks[vr->classbook].vlc.table, | |
| 1584 | ✗ | vc->codebooks[vr->classbook].nb_bits, 3); | |
| 1585 | } | ||
| 1586 | } | ||
| 1587 | } | ||
| 1588 | } | ||
| 1589 | 9390 | return 0; | |
| 1590 | } | ||
| 1591 | |||
| 1592 | 9510 | static inline int vorbis_residue_decode(vorbis_context *vc, vorbis_residue *vr, | |
| 1593 | unsigned ch, | ||
| 1594 | uint8_t *do_not_decode, | ||
| 1595 | float *vec, unsigned vlen, | ||
| 1596 | unsigned ch_left) | ||
| 1597 | { | ||
| 1598 |
2/2✓ Branch 0 taken 4275 times.
✓ Branch 1 taken 5235 times.
|
9510 | if (vr->type == 2) |
| 1599 | 4275 | return vorbis_residue_decode_internal(vc, vr, ch, do_not_decode, vec, vlen, ch_left, 2); | |
| 1600 |
2/2✓ Branch 0 taken 1815 times.
✓ Branch 1 taken 3420 times.
|
5235 | else if (vr->type == 1) |
| 1601 | 1815 | return vorbis_residue_decode_internal(vc, vr, ch, do_not_decode, vec, vlen, ch_left, 1); | |
| 1602 |
1/2✓ Branch 0 taken 3420 times.
✗ Branch 1 not taken.
|
3420 | else if (vr->type == 0) |
| 1603 | 3420 | return vorbis_residue_decode_internal(vc, vr, ch, do_not_decode, vec, vlen, ch_left, 0); | |
| 1604 | else { | ||
| 1605 | ✗ | av_log(vc->avctx, AV_LOG_ERROR, " Invalid residue type while residue decode?! \n"); | |
| 1606 | ✗ | return AVERROR_INVALIDDATA; | |
| 1607 | } | ||
| 1608 | } | ||
| 1609 | |||
| 1610 | // Decode the audio packet using the functions above | ||
| 1611 | |||
| 1612 | 9139 | static int vorbis_parse_audio_packet(vorbis_context *vc, float **floor_ptr) | |
| 1613 | { | ||
| 1614 | 9139 | GetBitContext *gb = &vc->gb; | |
| 1615 | AVTXContext *mdct; | ||
| 1616 | av_tx_fn mdct_fn; | ||
| 1617 | 9139 | int previous_window = vc->previous_window; | |
| 1618 | unsigned mode_number, blockflag, blocksize; | ||
| 1619 | int i, j; | ||
| 1620 | uint8_t no_residue[255]; | ||
| 1621 | uint8_t do_not_decode[255]; | ||
| 1622 | vorbis_mapping *mapping; | ||
| 1623 | 9139 | float *ch_res_ptr = vc->channel_residues; | |
| 1624 | uint8_t res_chan[255]; | ||
| 1625 | 9139 | unsigned res_num = 0; | |
| 1626 | 9139 | int retlen = 0; | |
| 1627 | 9139 | unsigned ch_left = vc->audio_channels; | |
| 1628 | unsigned vlen; | ||
| 1629 | |||
| 1630 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 9139 times.
|
9139 | if (get_bits1(gb)) { |
| 1631 | ✗ | av_log(vc->avctx, AV_LOG_ERROR, "Not a Vorbis I audio packet.\n"); | |
| 1632 | ✗ | return AVERROR_INVALIDDATA; // packet type not audio | |
| 1633 | } | ||
| 1634 | |||
| 1635 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9139 times.
|
9139 | if (vc->mode_count == 1) { |
| 1636 | ✗ | mode_number = 0; | |
| 1637 | } else { | ||
| 1638 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 9139 times.
|
9139 | GET_VALIDATED_INDEX(mode_number, ilog(vc->mode_count-1), vc->mode_count) |
| 1639 | } | ||
| 1640 | 9139 | vc->mode_number = mode_number; | |
| 1641 | 9139 | mapping = &vc->mappings[vc->modes[mode_number].mapping]; | |
| 1642 | |||
| 1643 | ff_dlog(NULL, " Mode number: %u , mapping: %d , blocktype %d\n", mode_number, | ||
| 1644 | vc->modes[mode_number].mapping, vc->modes[mode_number].blockflag); | ||
| 1645 | |||
| 1646 | 9139 | blockflag = vc->modes[mode_number].blockflag; | |
| 1647 | 9139 | blocksize = vc->blocksize[blockflag]; | |
| 1648 | 9139 | vlen = blocksize / 2; | |
| 1649 |
2/2✓ Branch 0 taken 5983 times.
✓ Branch 1 taken 3156 times.
|
9139 | if (blockflag) { |
| 1650 | 5983 | int code = get_bits(gb, 2); | |
| 1651 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 5980 times.
|
5983 | if (previous_window < 0) |
| 1652 | 3 | previous_window = code>>1; | |
| 1653 |
2/2✓ Branch 0 taken 58 times.
✓ Branch 1 taken 3098 times.
|
3156 | } else if (previous_window < 0) |
| 1654 | 58 | previous_window = 0; | |
| 1655 | |||
| 1656 | 9139 | memset(ch_res_ptr, 0, sizeof(float) * vc->audio_channels * vlen); //FIXME can this be removed ? | |
| 1657 |
2/2✓ Branch 0 taken 17853 times.
✓ Branch 1 taken 9139 times.
|
26992 | for (i = 0; i < vc->audio_channels; ++i) |
| 1658 | 17853 | memset(floor_ptr[i], 0, vlen * sizeof(floor_ptr[0][0])); //FIXME can this be removed ? | |
| 1659 | |||
| 1660 | // Decode floor | ||
| 1661 | |||
| 1662 |
2/2✓ Branch 0 taken 17853 times.
✓ Branch 1 taken 9139 times.
|
26992 | for (i = 0; i < vc->audio_channels; ++i) { |
| 1663 | vorbis_floor *floor; | ||
| 1664 | int ret; | ||
| 1665 |
2/2✓ Branch 0 taken 2226 times.
✓ Branch 1 taken 15627 times.
|
17853 | if (mapping->submaps > 1) { |
| 1666 | 2226 | floor = &vc->floors[mapping->submap_floor[mapping->mux[i]]]; | |
| 1667 | } else { | ||
| 1668 | 15627 | floor = &vc->floors[mapping->submap_floor[0]]; | |
| 1669 | } | ||
| 1670 | |||
| 1671 | 17853 | ret = floor->decode(vc, &floor->data, floor_ptr[i]); | |
| 1672 | |||
| 1673 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 17853 times.
|
17853 | if (ret < 0) { |
| 1674 | ✗ | av_log(vc->avctx, AV_LOG_ERROR, "Invalid codebook in vorbis_floor_decode.\n"); | |
| 1675 | ✗ | return AVERROR_INVALIDDATA; | |
| 1676 | } | ||
| 1677 | 17853 | no_residue[i] = ret; | |
| 1678 | } | ||
| 1679 | |||
| 1680 | // Nonzero vector propagate | ||
| 1681 | |||
| 1682 |
2/2✓ Branch 0 taken 5388 times.
✓ Branch 1 taken 9139 times.
|
14527 | for (i = mapping->coupling_steps - 1; i >= 0; --i) { |
| 1683 |
2/2✓ Branch 0 taken 4732 times.
✓ Branch 1 taken 656 times.
|
5388 | if (!(no_residue[mapping->magnitude[i]] & no_residue[mapping->angle[i]])) { |
| 1684 | 4732 | no_residue[mapping->magnitude[i]] = 0; | |
| 1685 | 4732 | no_residue[mapping->angle[i]] = 0; | |
| 1686 | } | ||
| 1687 | } | ||
| 1688 | |||
| 1689 | // Decode residue | ||
| 1690 | |||
| 1691 |
2/2✓ Branch 0 taken 9510 times.
✓ Branch 1 taken 9139 times.
|
18649 | for (i = 0; i < mapping->submaps; ++i) { |
| 1692 | vorbis_residue *residue; | ||
| 1693 | 9510 | unsigned ch = 0; | |
| 1694 | int ret; | ||
| 1695 | |||
| 1696 |
2/2✓ Branch 0 taken 20079 times.
✓ Branch 1 taken 9510 times.
|
29589 | for (j = 0; j < vc->audio_channels; ++j) { |
| 1697 |
4/4✓ Branch 0 taken 4452 times.
✓ Branch 1 taken 15627 times.
✓ Branch 2 taken 2226 times.
✓ Branch 3 taken 2226 times.
|
20079 | if ((mapping->submaps == 1) || (i == mapping->mux[j])) { |
| 1698 | 17853 | res_chan[j] = res_num; | |
| 1699 |
2/2✓ Branch 0 taken 1226 times.
✓ Branch 1 taken 16627 times.
|
17853 | if (no_residue[j]) { |
| 1700 | 1226 | do_not_decode[ch] = 1; | |
| 1701 | } else { | ||
| 1702 | 16627 | do_not_decode[ch] = 0; | |
| 1703 | } | ||
| 1704 | 17853 | ++ch; | |
| 1705 | 17853 | ++res_num; | |
| 1706 | } | ||
| 1707 | } | ||
| 1708 | 9510 | residue = &vc->residues[mapping->submap_residue[i]]; | |
| 1709 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9510 times.
|
9510 | if (ch_left < ch) { |
| 1710 | ✗ | av_log(vc->avctx, AV_LOG_ERROR, "Too many channels in vorbis_floor_decode.\n"); | |
| 1711 | ✗ | return AVERROR_INVALIDDATA; | |
| 1712 | } | ||
| 1713 |
1/2✓ Branch 0 taken 9510 times.
✗ Branch 1 not taken.
|
9510 | if (ch) { |
| 1714 | 9510 | ret = vorbis_residue_decode(vc, residue, ch, do_not_decode, ch_res_ptr, vlen, ch_left); | |
| 1715 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9510 times.
|
9510 | if (ret < 0) |
| 1716 | ✗ | return ret; | |
| 1717 | } | ||
| 1718 | |||
| 1719 | 9510 | ch_res_ptr += ch * vlen; | |
| 1720 | 9510 | ch_left -= ch; | |
| 1721 | } | ||
| 1722 | |||
| 1723 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9139 times.
|
9139 | if (ch_left > 0) |
| 1724 | ✗ | return AVERROR_INVALIDDATA; | |
| 1725 | |||
| 1726 | // Inverse coupling | ||
| 1727 | |||
| 1728 |
2/2✓ Branch 0 taken 5388 times.
✓ Branch 1 taken 9139 times.
|
14527 | for (i = mapping->coupling_steps - 1; i >= 0; --i) { //warning: i has to be signed |
| 1729 | float *mag, *ang; | ||
| 1730 | |||
| 1731 | 5388 | mag = vc->channel_residues+res_chan[mapping->magnitude[i]] * blocksize / 2; | |
| 1732 | 5388 | ang = vc->channel_residues+res_chan[mapping->angle[i]] * blocksize / 2; | |
| 1733 | 5388 | vc->dsp.vorbis_inverse_coupling(mag, ang, blocksize / 2); | |
| 1734 | } | ||
| 1735 | |||
| 1736 | // Dotproduct, MDCT | ||
| 1737 | |||
| 1738 | 9139 | mdct = vc->mdct[blockflag]; | |
| 1739 | 9139 | mdct_fn = vc->mdct_fn[blockflag]; | |
| 1740 | |||
| 1741 |
2/2✓ Branch 0 taken 17853 times.
✓ Branch 1 taken 9139 times.
|
26992 | for (j = vc->audio_channels-1;j >= 0; j--) { |
| 1742 | 17853 | ch_res_ptr = vc->channel_residues + res_chan[j] * blocksize / 2; | |
| 1743 | 17853 | vc->fdsp->vector_fmul(floor_ptr[j], floor_ptr[j], ch_res_ptr, blocksize / 2); | |
| 1744 | 17853 | mdct_fn(mdct, ch_res_ptr, floor_ptr[j], sizeof(float)); | |
| 1745 | } | ||
| 1746 | |||
| 1747 | // Overlap/add, save data for next overlapping | ||
| 1748 | |||
| 1749 | 9139 | retlen = (blocksize + vc->blocksize[previous_window]) / 4; | |
| 1750 |
2/2✓ Branch 0 taken 17853 times.
✓ Branch 1 taken 9139 times.
|
26992 | for (j = 0; j < vc->audio_channels; j++) { |
| 1751 | 17853 | unsigned bs0 = vc->blocksize[0]; | |
| 1752 | 17853 | unsigned bs1 = vc->blocksize[1]; | |
| 1753 | 17853 | float *residue = vc->channel_residues + res_chan[j] * blocksize / 2; | |
| 1754 | 17853 | float *saved = vc->saved + j * bs1 / 4; | |
| 1755 | 17853 | float *ret = floor_ptr[j]; | |
| 1756 | 17853 | float *buf = residue; | |
| 1757 | 17853 | const float *win = vc->win[blockflag & previous_window]; | |
| 1758 | |||
| 1759 |
2/2✓ Branch 0 taken 16416 times.
✓ Branch 1 taken 1437 times.
|
17853 | if (blockflag == previous_window) { |
| 1760 | 16416 | vc->fdsp->vector_fmul_window(ret, saved, buf, win, blocksize / 4); | |
| 1761 |
2/2✓ Branch 0 taken 739 times.
✓ Branch 1 taken 698 times.
|
1437 | } else if (blockflag > previous_window) { |
| 1762 | 739 | vc->fdsp->vector_fmul_window(ret, saved, buf, win, bs0 / 4); | |
| 1763 | 739 | memcpy(ret+bs0/2, buf+bs0/4, ((bs1-bs0)/4) * sizeof(float)); | |
| 1764 | } else { | ||
| 1765 | 698 | memcpy(ret, saved, ((bs1 - bs0) / 4) * sizeof(float)); | |
| 1766 | 698 | vc->fdsp->vector_fmul_window(ret + (bs1 - bs0) / 4, saved + (bs1 - bs0) / 4, buf, win, bs0 / 4); | |
| 1767 | } | ||
| 1768 | 17853 | memcpy(saved, buf + blocksize / 4, blocksize / 4 * sizeof(float)); | |
| 1769 | } | ||
| 1770 | |||
| 1771 | 9139 | vc->previous_window = blockflag; | |
| 1772 | 9139 | return retlen; | |
| 1773 | } | ||
| 1774 | |||
| 1775 | // Return the decoded audio packet through the standard api | ||
| 1776 | |||
| 1777 | 9139 | static int vorbis_decode_frame(AVCodecContext *avctx, AVFrame *frame, | |
| 1778 | int *got_frame_ptr, AVPacket *avpkt) | ||
| 1779 | { | ||
| 1780 | 9139 | const uint8_t *buf = avpkt->data; | |
| 1781 | 9139 | int buf_size = avpkt->size; | |
| 1782 | 9139 | vorbis_context *vc = avctx->priv_data; | |
| 1783 | 9139 | GetBitContext *gb = &vc->gb; | |
| 1784 | float *channel_ptrs[255]; | ||
| 1785 | int i, len, ret; | ||
| 1786 | |||
| 1787 | ff_dlog(NULL, "packet length %d \n", buf_size); | ||
| 1788 | |||
| 1789 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 9139 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
9139 | if (*buf == 1 && buf_size > 7) { |
| 1790 | ✗ | if ((ret = init_get_bits8(gb, buf + 1, buf_size - 1)) < 0) | |
| 1791 | ✗ | return ret; | |
| 1792 | |||
| 1793 | ✗ | vorbis_free(vc); | |
| 1794 | ✗ | if ((ret = vorbis_parse_id_hdr(vc))) { | |
| 1795 | ✗ | av_log(avctx, AV_LOG_ERROR, "Id header corrupt.\n"); | |
| 1796 | ✗ | vorbis_free(vc); | |
| 1797 | ✗ | return ret; | |
| 1798 | } | ||
| 1799 | |||
| 1800 | ✗ | av_channel_layout_uninit(&avctx->ch_layout); | |
| 1801 | ✗ | if (vc->audio_channels > 8) { | |
| 1802 | ✗ | avctx->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC; | |
| 1803 | ✗ | avctx->ch_layout.nb_channels = vc->audio_channels; | |
| 1804 | } else { | ||
| 1805 | ✗ | av_channel_layout_copy(&avctx->ch_layout, &ff_vorbis_ch_layouts[vc->audio_channels - 1]); | |
| 1806 | } | ||
| 1807 | |||
| 1808 | ✗ | avctx->sample_rate = vc->audio_samplerate; | |
| 1809 | ✗ | return buf_size; | |
| 1810 | } | ||
| 1811 | |||
| 1812 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 9139 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
9139 | if (*buf == 3 && buf_size > 7) { |
| 1813 | ✗ | av_log(avctx, AV_LOG_DEBUG, "Ignoring comment header\n"); | |
| 1814 | ✗ | return buf_size; | |
| 1815 | } | ||
| 1816 | |||
| 1817 |
1/8✗ Branch 0 not taken.
✓ Branch 1 taken 9139 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
9139 | if (*buf == 5 && buf_size > 7 && vc->channel_residues && !vc->modes) { |
| 1818 | ✗ | if ((ret = init_get_bits8(gb, buf + 1, buf_size - 1)) < 0) | |
| 1819 | ✗ | return ret; | |
| 1820 | |||
| 1821 | ✗ | if ((ret = vorbis_parse_setup_hdr(vc))) { | |
| 1822 | ✗ | av_log(avctx, AV_LOG_ERROR, "Setup header corrupt.\n"); | |
| 1823 | ✗ | vorbis_free(vc); | |
| 1824 | ✗ | return ret; | |
| 1825 | } | ||
| 1826 | ✗ | return buf_size; | |
| 1827 | } | ||
| 1828 | |||
| 1829 |
2/4✓ Branch 0 taken 9139 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 9139 times.
|
9139 | if (!vc->channel_residues || !vc->modes) { |
| 1830 | ✗ | av_log(avctx, AV_LOG_ERROR, "Data packet before valid headers\n"); | |
| 1831 | ✗ | return AVERROR_INVALIDDATA; | |
| 1832 | } | ||
| 1833 | |||
| 1834 | /* get output buffer */ | ||
| 1835 | 9139 | frame->nb_samples = vc->blocksize[1] / 2; | |
| 1836 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 9139 times.
|
9139 | if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) |
| 1837 | ✗ | return ret; | |
| 1838 | |||
| 1839 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9139 times.
|
9139 | if (vc->audio_channels > 8) { |
| 1840 | ✗ | for (i = 0; i < vc->audio_channels; i++) | |
| 1841 | ✗ | channel_ptrs[i] = (float *)frame->extended_data[i]; | |
| 1842 | } else { | ||
| 1843 |
2/2✓ Branch 0 taken 17853 times.
✓ Branch 1 taken 9139 times.
|
26992 | for (i = 0; i < vc->audio_channels; i++) { |
| 1844 | 17853 | int ch = ff_vorbis_channel_layout_offsets[vc->audio_channels - 1][i]; | |
| 1845 | 17853 | channel_ptrs[ch] = (float *)frame->extended_data[i]; | |
| 1846 | } | ||
| 1847 | } | ||
| 1848 | |||
| 1849 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 9139 times.
|
9139 | if ((ret = init_get_bits8(gb, buf, buf_size)) < 0) |
| 1850 | ✗ | return ret; | |
| 1851 | |||
| 1852 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 9139 times.
|
9139 | if ((len = vorbis_parse_audio_packet(vc, channel_ptrs)) <= 0) |
| 1853 | ✗ | return len; | |
| 1854 | |||
| 1855 |
2/2✓ Branch 0 taken 61 times.
✓ Branch 1 taken 9078 times.
|
9139 | if (!vc->first_frame) { |
| 1856 | 61 | vc->first_frame = 1; | |
| 1857 | 61 | avctx->internal->skip_samples = len; | |
| 1858 | } | ||
| 1859 | |||
| 1860 | ff_dlog(NULL, "parsed %d bytes %d bits, returned %d samples (*ch*bits) \n", | ||
| 1861 | get_bits_count(gb) / 8, get_bits_count(gb) % 8, len); | ||
| 1862 | |||
| 1863 | 9139 | frame->nb_samples = len; | |
| 1864 | 9139 | *got_frame_ptr = 1; | |
| 1865 | |||
| 1866 | 9139 | return buf_size; | |
| 1867 | } | ||
| 1868 | |||
| 1869 | // Close decoder | ||
| 1870 | |||
| 1871 | 71 | static av_cold int vorbis_decode_close(AVCodecContext *avctx) | |
| 1872 | { | ||
| 1873 | 71 | vorbis_context *vc = avctx->priv_data; | |
| 1874 | |||
| 1875 | 71 | vorbis_free(vc); | |
| 1876 | |||
| 1877 | 71 | return 0; | |
| 1878 | } | ||
| 1879 | |||
| 1880 | ✗ | static av_cold void vorbis_decode_flush(AVCodecContext *avctx) | |
| 1881 | { | ||
| 1882 | ✗ | vorbis_context *vc = avctx->priv_data; | |
| 1883 | |||
| 1884 | ✗ | if (vc->saved) { | |
| 1885 | ✗ | memset(vc->saved, 0, (vc->blocksize[1] / 4) * vc->audio_channels * | |
| 1886 | sizeof(*vc->saved)); | ||
| 1887 | } | ||
| 1888 | ✗ | vc->previous_window = -1; | |
| 1889 | ✗ | vc->first_frame = 0; | |
| 1890 | ✗ | } | |
| 1891 | |||
| 1892 | const FFCodec ff_vorbis_decoder = { | ||
| 1893 | .p.name = "vorbis", | ||
| 1894 | CODEC_LONG_NAME("Vorbis"), | ||
| 1895 | .p.type = AVMEDIA_TYPE_AUDIO, | ||
| 1896 | .p.id = AV_CODEC_ID_VORBIS, | ||
| 1897 | .priv_data_size = sizeof(vorbis_context), | ||
| 1898 | .init = vorbis_decode_init, | ||
| 1899 | .close = vorbis_decode_close, | ||
| 1900 | FF_CODEC_DECODE_CB(vorbis_decode_frame), | ||
| 1901 | .flush = vorbis_decode_flush, | ||
| 1902 | .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_CHANNEL_CONF, | ||
| 1903 | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, | ||
| 1904 | }; | ||
| 1905 |