| @@ -0,0 +1,1608 @@ |
| | 1 | /* |
| | 2 | * Copyright (c) 2024 Lynne <dev@lynne.ee> |
| | 3 | * |
| | 4 | * This file is part of FFmpeg. |
| | 5 | * |
| | 6 | * FFmpeg is free software; you can redistribute it and/or |
| | 7 | * modify it under the terms of the GNU Lesser General Public |
| | 8 | * License as published by the Free Software Foundation; either |
| | 9 | * version 2.1 of the License, or (at your option) any later version. |
| | 10 | * |
| | 11 | * FFmpeg is distributed in the hope that it will be useful, |
| | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| | 14 | * Lesser General Public License for more details. |
| | 15 | * |
| | 16 | * You should have received a copy of the GNU Lesser General Public |
| | 17 | * License along with FFmpeg; if not, write to the Free Software |
| | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| | 19 | */ |
| | 20 | |
| | 21 | #include "aacdec_usac.h" |
| | 22 | #include "aacdec_tab.h" |
| | 23 | #include "aacdec_lpd.h" |
| | 24 | #include "aacdec_ac.h" |
| | 25 | |
| | 26 | #include "libavcodec/aactab.h" |
| | 27 | #include "libavutil/mem.h" |
| | 28 | #include "libavcodec/mpeg4audio.h" |
| | 29 | #include "libavcodec/unary.h" |
| | 30 | |
| | 31 | /* Number of scalefactor bands per complex prediction band, equal to 2. */ |
| | 32 | #define SFB_PER_PRED_BAND 2 |
| | 33 | |
| | 34 | static inline uint32_t get_escaped_value(GetBitContext *gb, int nb1, int nb2, int nb3) |
| | 35 | { |
| | 36 | uint32_t val = get_bits(gb, nb1), val2; |
| | 37 | if (val < ((1 << nb1) - 1)) |
| | 38 | return val; |
| | 39 | |
| | 40 | val += val2 = get_bits(gb, nb2); |
| | 41 | if (val2 == ((1 << nb2) - 1)) |
| | 42 | val += get_bits(gb, nb3); |
| | 43 | |
| | 44 | return val; |
| | 45 | } |
| | 46 | |
| | 47 | /* ISO/IEC 23003-3, Table 74 — bsOutputChannelPos */ |
| | 48 | static const enum AVChannel usac_ch_pos_to_av[64] = { |
| | 49 | [0] = AV_CHAN_FRONT_LEFT, |
| | 50 | [1] = AV_CHAN_FRONT_RIGHT, |
| | 51 | [2] = AV_CHAN_FRONT_CENTER, |
| | 52 | [3] = AV_CHAN_LOW_FREQUENCY, |
| | 53 | [4] = AV_CHAN_SIDE_LEFT, // +110 degrees, Ls|LS|kAudioChannelLabel_LeftSurround |
| | 54 | [5] = AV_CHAN_SIDE_RIGHT, // -110 degrees, Rs|RS|kAudioChannelLabel_RightSurround |
| | 55 | [6] = AV_CHAN_FRONT_LEFT_OF_CENTER, |
| | 56 | [7] = AV_CHAN_FRONT_RIGHT_OF_CENTER, |
| | 57 | [8] = AV_CHAN_BACK_LEFT, // +135 degrees, Lsr|BL|kAudioChannelLabel_RearSurroundLeft |
| | 58 | [9] = AV_CHAN_BACK_RIGHT, // -135 degrees, Rsr|BR|kAudioChannelLabel_RearSurroundRight |
| | 59 | [10] = AV_CHAN_BACK_CENTER, |
| | 60 | [11] = AV_CHAN_SURROUND_DIRECT_LEFT, |
| | 61 | [12] = AV_CHAN_SURROUND_DIRECT_RIGHT, |
| | 62 | [13] = AV_CHAN_SIDE_SURROUND_LEFT, // +90 degrees, Lss|SL|kAudioChannelLabel_LeftSideSurround |
| | 63 | [14] = AV_CHAN_SIDE_SURROUND_RIGHT, // -90 degrees, Rss|SR|kAudioChannelLabel_RightSideSurround |
| | 64 | [15] = AV_CHAN_WIDE_LEFT, // +60 degrees, Lw|FLw|kAudioChannelLabel_LeftWide |
| | 65 | [16] = AV_CHAN_WIDE_RIGHT, // -60 degrees, Rw|FRw|kAudioChannelLabel_RightWide |
| | 66 | [17] = AV_CHAN_TOP_FRONT_LEFT, |
| | 67 | [18] = AV_CHAN_TOP_FRONT_RIGHT, |
| | 68 | [19] = AV_CHAN_TOP_FRONT_CENTER, |
| | 69 | [20] = AV_CHAN_TOP_BACK_LEFT, |
| | 70 | [21] = AV_CHAN_TOP_BACK_RIGHT, |
| | 71 | [22] = AV_CHAN_TOP_BACK_CENTER, |
| | 72 | [23] = AV_CHAN_TOP_SIDE_LEFT, |
| | 73 | [24] = AV_CHAN_TOP_SIDE_RIGHT, |
| | 74 | [25] = AV_CHAN_TOP_CENTER, |
| | 75 | [26] = AV_CHAN_LOW_FREQUENCY_2, |
| | 76 | [27] = AV_CHAN_BOTTOM_FRONT_LEFT, |
| | 77 | [28] = AV_CHAN_BOTTOM_FRONT_RIGHT, |
| | 78 | [29] = AV_CHAN_BOTTOM_FRONT_CENTER, |
| | 79 | [30] = AV_CHAN_TOP_SURROUND_LEFT, ///< +110 degrees, Lvs, TpLS |
| | 80 | [31] = AV_CHAN_TOP_SURROUND_RIGHT, ///< -110 degrees, Rvs, TpRS |
| | 81 | }; |
| | 82 | |
| | 83 | static int decode_loudness_info(AACDecContext *ac, AACUSACLoudnessInfo *info, |
| | 84 | GetBitContext *gb) |
| | 85 | { |
| | 86 | info->drc_set_id = get_bits(gb, 6); |
| | 87 | info->downmix_id = get_bits(gb, 7); |
| | 88 | |
| | 89 | if ((info->sample_peak.present = get_bits1(gb))) /* samplePeakLevelPresent */ |
| | 90 | info->sample_peak.lvl = get_bits(gb, 12); |
| | 91 | |
| | 92 | if ((info->true_peak.present = get_bits1(gb))) { /* truePeakLevelPresent */ |
| | 93 | info->true_peak.lvl = get_bits(gb, 12); |
| | 94 | info->true_peak.measurement = get_bits(gb, 4); |
| | 95 | info->true_peak.reliability = get_bits(gb, 2); |
| | 96 | } |
| | 97 | |
| | 98 | info->nb_measurements = get_bits(gb, 4); |
| | 99 | for (int i = 0; i < info->nb_measurements; i++) { |
| | 100 | info->measurements[i].method_def = get_bits(gb, 4); |
| | 101 | info->measurements[i].method_val = get_unary(gb, 0, 8); |
| | 102 | info->measurements[i].measurement = get_bits(gb, 4); |
| | 103 | info->measurements[i].reliability = get_bits(gb, 2); |
| | 104 | } |
| | 105 | |
| | 106 | return 0; |
| | 107 | } |
| | 108 | |
| | 109 | static int decode_loudness_set(AACDecContext *ac, AACUSACConfig *usac, |
| | 110 | GetBitContext *gb) |
| | 111 | { |
| | 112 | int ret; |
| | 113 | |
| | 114 | usac->loudness.nb_album = get_bits(gb, 6); /* loudnessInfoAlbumCount */ |
| | 115 | usac->loudness.nb_info = get_bits(gb, 6); /* loudnessInfoCount */ |
| | 116 | |
| | 117 | for (int i = 0; i < usac->loudness.nb_album; i++) { |
| | 118 | ret = decode_loudness_info(ac, &usac->loudness.album_info[i], gb); |
| | 119 | if (ret < 0) |
| | 120 | return ret; |
| | 121 | } |
| | 122 | |
| | 123 | for (int i = 0; i < usac->loudness.nb_info; i++) { |
| | 124 | ret = decode_loudness_info(ac, &usac->loudness.info[i], gb); |
| | 125 | if (ret < 0) |
| | 126 | return ret; |
| | 127 | } |
| | 128 | |
| | 129 | if (get_bits1(gb)) { /* loudnessInfoSetExtPresent */ |
| | 130 | enum AACUSACLoudnessExt type; |
| | 131 | while ((type = get_bits(gb, 4)) != UNIDRCLOUDEXT_TERM) { |
| | 132 | uint8_t size_bits = get_bits(gb, 4) + 4; |
| | 133 | uint8_t bit_size = get_bits(gb, size_bits) + 1; |
| | 134 | switch (type) { |
| | 135 | case UNIDRCLOUDEXT_EQ: |
| | 136 | avpriv_report_missing_feature(ac->avctx, "loudnessInfoV1"); |
| | 137 | return AVERROR_PATCHWELCOME; |
| | 138 | default: |
| | 139 | for (int i = 0; i < bit_size; i++) |
| | 140 | skip_bits1(gb); |
| | 141 | } |
| | 142 | } |
| | 143 | } |
| | 144 | |
| | 145 | return 0; |
| | 146 | } |
| | 147 | |
| | 148 | static void decode_usac_sbr_data(AACUsacElemConfig *e, GetBitContext *gb) |
| | 149 | { |
| | 150 | uint8_t header_extra1; |
| | 151 | uint8_t header_extra2; |
| | 152 | |
| | 153 | e->sbr.harmonic_sbr = get_bits1(gb); /* harmonicSBR */ |
| | 154 | e->sbr.bs_intertes = get_bits1(gb); /* bs_interTes */ |
| | 155 | e->sbr.bs_pvc = get_bits1(gb); /* bs_pvc */ |
| | 156 | |
| | 157 | e->sbr.dflt.start_freq = get_bits(gb, 4); /* dflt_start_freq */ |
| | 158 | e->sbr.dflt.stop_freq = get_bits(gb, 4); /* dflt_stop_freq */ |
| | 159 | |
| | 160 | header_extra1 = get_bits1(gb); /* dflt_header_extra1 */ |
| | 161 | header_extra2 = get_bits1(gb); /* dflt_header_extra2 */ |
| | 162 | |
| | 163 | e->sbr.dflt.freq_scale = 2; |
| | 164 | e->sbr.dflt.alter_scale = 1; |
| | 165 | e->sbr.dflt.noise_scale = 2; |
| | 166 | if (header_extra1) { |
| | 167 | e->sbr.dflt.freq_scale = get_bits(gb, 2); /* dflt_freq_scale */ |
| | 168 | e->sbr.dflt.alter_scale = get_bits1(gb); /* dflt_alter_scale */ |
| | 169 | e->sbr.dflt.noise_scale = get_bits(gb, 2); /* dflt_noise_scale */ |
| | 170 | } |
| | 171 | |
| | 172 | e->sbr.dflt.limiter_bands = 2; |
| | 173 | e->sbr.dflt.limiter_gains = 2; |
| | 174 | e->sbr.dflt.interpol_freq = 1; |
| | 175 | e->sbr.dflt.smoothing_mode = 1; |
| | 176 | if (header_extra2) { |
| | 177 | e->sbr.dflt.limiter_bands = get_bits(gb, 2); /* dflt_limiter_bands */ |
| | 178 | e->sbr.dflt.limiter_gains = get_bits(gb, 2); /* dflt_limiter_gains */ |
| | 179 | e->sbr.dflt.interpol_freq = get_bits1(gb); /* dflt_interpol_freq */ |
| | 180 | e->sbr.dflt.smoothing_mode = get_bits1(gb); /* dflt_smoothing_mode */ |
| | 181 | } |
| | 182 | } |
| | 183 | |
| | 184 | static void decode_usac_element_core(AACUsacElemConfig *e, |
| | 185 | GetBitContext *gb, |
| | 186 | int sbr_ratio) |
| | 187 | { |
| | 188 | e->tw_mdct = get_bits1(gb); /* tw_mdct */ |
| | 189 | e->noise_fill = get_bits1(gb); |
| | 190 | e->sbr.ratio = sbr_ratio; |
| | 191 | } |
| | 192 | |
| | 193 | static void decode_usac_element_pair(AACUsacElemConfig *e, GetBitContext *gb) |
| | 194 | { |
| | 195 | e->stereo_config_index = 0; |
| | 196 | if (e->sbr.ratio) { |
| | 197 | decode_usac_sbr_data(e, gb); |
| | 198 | e->stereo_config_index = get_bits(gb, 2); |
| | 199 | } |
| | 200 | if (e->stereo_config_index) { |
| | 201 | e->mps.freq_res = get_bits(gb, 3); /* bsFreqRes */ |
| | 202 | e->mps.fixed_gain = get_bits(gb, 3); /* bsFixedGainDMX */ |
| | 203 | e->mps.temp_shape_config = get_bits(gb, 2); /* bsTempShapeConfig */ |
| | 204 | e->mps.decorr_config = get_bits(gb, 2); /* bsDecorrConfig */ |
| | 205 | e->mps.high_rate_mode = get_bits1(gb); /* bsHighRateMode */ |
| | 206 | e->mps.phase_coding = get_bits1(gb); /* bsPhaseCoding */ |
| | 207 | |
| | 208 | if (get_bits1(gb)) /* bsOttBandsPhasePresent */ |
| | 209 | e->mps.otts_bands_phase = get_bits(gb, 5); /* bsOttBandsPhase */ |
| | 210 | |
| | 211 | e->mps.residual_coding = e->stereo_config_index >= 2; /* bsResidualCoding */ |
| | 212 | if (e->mps.residual_coding) { |
| | 213 | e->mps.residual_bands = get_bits(gb, 5); /* bsResidualBands */ |
| | 214 | e->mps.pseudo_lr = get_bits1(gb); /* bsPseudoLr */ |
| | 215 | } |
| | 216 | if (e->mps.temp_shape_config == 2) |
| | 217 | e->mps.env_quant_mode = get_bits1(gb); /* bsEnvQuantMode */ |
| | 218 | } |
| | 219 | } |
| | 220 | |
| | 221 | static int decode_usac_extension(AACDecContext *ac, AACUsacElemConfig *e, |
| | 222 | GetBitContext *gb) |
| | 223 | { |
| | 224 | int len = 0, ext_config_len; |
| | 225 | |
| | 226 | e->ext.type = get_escaped_value(gb, 4, 8, 16); /* usacExtElementType */ |
| | 227 | ext_config_len = get_escaped_value(gb, 4, 8, 16); /* usacExtElementConfigLength */ |
| | 228 | |
| | 229 | if (get_bits1(gb)) /* usacExtElementDefaultLengthPresent */ |
| | 230 | len = get_escaped_value(gb, 8, 16, 0) + 1; |
| | 231 | |
| | 232 | e->ext.default_len = len; |
| | 233 | e->ext.payload_frag = get_bits1(gb); /* usacExtElementPayloadFrag */ |
| | 234 | |
| | 235 | av_log(ac->avctx, AV_LOG_DEBUG, "Extension present: type %i, len %i\n", |
| | 236 | e->ext.type, ext_config_len); |
| | 237 | |
| | 238 | switch (e->ext.type) { |
| | 239 | #if 0 /* Skip unsupported values */ |
| | 240 | case ID_EXT_ELE_MPEGS: |
| | 241 | break; |
| | 242 | case ID_EXT_ELE_SAOC: |
| | 243 | break; |
| | 244 | case ID_EXT_ELE_UNI_DRC: |
| | 245 | break; |
| | 246 | #endif |
| | 247 | case ID_EXT_ELE_FILL: |
| | 248 | break; /* This is what the spec does */ |
| | 249 | case ID_EXT_ELE_AUDIOPREROLL: |
| | 250 | /* No configuration needed - fallthrough (len should be 0) */ |
| | 251 | default: |
| | 252 | skip_bits(gb, 8*ext_config_len); |
| | 253 | break; |
| | 254 | }; |
| | 255 | |
| | 256 | return 0; |
| | 257 | } |
| | 258 | |
| | 259 | int ff_aac_usac_reset_state(AACDecContext *ac, OutputConfiguration *oc) |
| | 260 | { |
| | 261 | AACUSACConfig *usac = &oc->usac; |
| | 262 | int elem_id[3 /* SCE, CPE, LFE */] = { 0, 0, 0 }; |
| | 263 | |
| | 264 | ChannelElement *che; |
| | 265 | enum RawDataBlockType type; |
| | 266 | int id, ch; |
| | 267 | |
| | 268 | /* Initialize state */ |
| | 269 | for (int i = 0; i < usac->nb_elems; i++) { |
| | 270 | AACUsacElemConfig *e = &usac->elems[i]; |
| | 271 | if (e->type != ID_USAC_SCE && e->type != ID_USAC_CPE) |
| | 272 | continue; |
| | 273 | |
| | 274 | if (e->type == ID_USAC_SCE) { |
| | 275 | ch = 1; |
| | 276 | type = TYPE_SCE; |
| | 277 | id = elem_id[0]++; |
| | 278 | } else { |
| | 279 | ch = 2; |
| | 280 | type = TYPE_CPE; |
| | 281 | id = elem_id[1]++; |
| | 282 | } |
| | 283 | |
| | 284 | che = ff_aac_get_che(ac, type, id); |
| | 285 | if (che) { |
| | 286 | AACUsacStereo *us = &che->us; |
| | 287 | memset(us, 0, sizeof(*us)); |
| | 288 | |
| | 289 | for (int j = 0; j < ch; j++) { |
| | 290 | SingleChannelElement *sce = &che->ch[ch]; |
| | 291 | AACUsacElemData *ue = &sce->ue; |
| | 292 | |
| | 293 | memset(ue, 0, sizeof(*ue)); |
| | 294 | |
| | 295 | if (!ch) |
| | 296 | ue->noise.seed = 0x3039; |
| | 297 | else |
| | 298 | che->ch[1].ue.noise.seed = 0x10932; |
| | 299 | } |
| | 300 | } |
| | 301 | } |
| | 302 | |
| | 303 | return 0; |
| | 304 | } |
| | 305 | |
| | 306 | /* UsacConfig */ |
| | 307 | int ff_aac_usac_config_decode(AACDecContext *ac, AVCodecContext *avctx, |
| | 308 | GetBitContext *gb, OutputConfiguration *oc, |
| | 309 | int channel_config) |
| | 310 | { |
| | 311 | int ret, idx; |
| | 312 | uint8_t freq_idx; |
| | 313 | uint8_t channel_config_idx; |
| | 314 | int nb_elements; |
| | 315 | int samplerate; |
| | 316 | int sbr_ratio; |
| | 317 | MPEG4AudioConfig *m4ac = &oc->m4ac; |
| | 318 | AACUSACConfig *usac = &oc->usac; |
| | 319 | int elem_id[3 /* SCE, CPE, LFE */]; |
| | 320 | |
| | 321 | uint8_t layout_map[MAX_ELEM_ID*4][3]; |
| | 322 | |
| | 323 | memset(usac, 0, sizeof(*usac)); |
| | 324 | |
| | 325 | freq_idx = get_bits(gb, 5); /* usacSamplingFrequencyIndex */ |
| | 326 | if (freq_idx == 0x1f) { |
| | 327 | samplerate = get_bits(gb, 24); /* usacSamplingFrequency */ |
| | 328 | |
| | 329 | /* Try to match up an index for the custom sample rate. |
| | 330 | * TODO: not sure if correct */ |
| | 331 | for (idx = 0; idx < /* FF_ARRAY_ELEMS(ff_aac_usac_samplerate) */ 32; idx++) { |
| | 332 | if (ff_aac_usac_samplerate[idx] >= samplerate) |
| | 333 | break; |
| | 334 | } |
| | 335 | idx = FFMIN(idx, /* FF_ARRAY_ELEMS(ff_aac_usac_samplerate) */ 32 - 1); |
| | 336 | usac->rate_idx = idx; |
| | 337 | } else { |
| | 338 | samplerate = ff_aac_usac_samplerate[freq_idx]; |
| | 339 | if (samplerate < 0) |
| | 340 | return AVERROR(EINVAL); |
| | 341 | usac->rate_idx = freq_idx; |
| | 342 | } |
| | 343 | |
| | 344 | m4ac->sample_rate = avctx->sample_rate = samplerate; |
| | 345 | |
| | 346 | usac->core_sbr_frame_len_idx = get_bits(gb, 3); /* coreSbrFrameLengthIndex */ |
| | 347 | m4ac->frame_length_short = usac->core_sbr_frame_len_idx == 0 || |
| | 348 | usac->core_sbr_frame_len_idx == 2; |
| | 349 | |
| | 350 | usac->core_frame_len = (usac->core_sbr_frame_len_idx == 0 || |
| | 351 | usac->core_sbr_frame_len_idx == 2) ? 768 : 1024; |
| | 352 | |
| | 353 | sbr_ratio = usac->core_sbr_frame_len_idx == 2 ? 2 : |
| | 354 | usac->core_sbr_frame_len_idx == 3 ? 3 : |
| | 355 | usac->core_sbr_frame_len_idx == 4 ? 1 : |
| | 356 | 0; |
| | 357 | |
| | 358 | channel_config_idx = get_bits(gb, 5); /* channelConfigurationIndex */ |
| | 359 | if (!channel_config_idx) { |
| | 360 | /* UsacChannelConfig() */ |
| | 361 | uint8_t nb_channels = get_escaped_value(gb, 5, 8, 16); /* numOutChannels */ |
| | 362 | if (nb_channels >= 64) |
| | 363 | return AVERROR(EINVAL); |
| | 364 | |
| | 365 | av_channel_layout_uninit(&ac->oc[1].ch_layout); |
| | 366 | |
| | 367 | ret = av_channel_layout_custom_init(&ac->oc[1].ch_layout, nb_channels); |
| | 368 | if (ret < 0) |
| | 369 | return ret; |
| | 370 | |
| | 371 | for (int i = 0; i < nb_channels; i++) { |
| | 372 | AVChannelCustom *cm = &ac->oc[1].ch_layout.u.map[i]; |
| | 373 | cm->id = usac_ch_pos_to_av[get_bits(gb, 5)]; /* bsOutputChannelPos */ |
| | 374 | if (cm->id == AV_CHAN_NONE) |
| | 375 | cm->id = AV_CHAN_UNKNOWN; |
| | 376 | } |
| | 377 | |
| | 378 | ret = av_channel_layout_retype(&ac->oc[1].ch_layout, |
| | 379 | AV_CHANNEL_ORDER_NATIVE, |
| | 380 | AV_CHANNEL_LAYOUT_RETYPE_FLAG_CANONICAL); |
| | 381 | if (ret < 0) |
| | 382 | return ret; |
| | 383 | |
| | 384 | ret = av_channel_layout_copy(&avctx->ch_layout, &ac->oc[1].ch_layout); |
| | 385 | if (ret < 0) |
| | 386 | return ret; |
| | 387 | } else { |
| | 388 | if ((ret = ff_aac_set_default_channel_config(ac, avctx, layout_map, |
| | 389 | &nb_elements, channel_config_idx))) |
| | 390 | return ret; |
| | 391 | } |
| | 392 | |
| | 393 | /* UsacDecoderConfig */ |
| | 394 | elem_id[0] = elem_id[1] = elem_id[2] = 0; |
| | 395 | usac->nb_elems = get_escaped_value(gb, 4, 8, 16) + 1; |
| | 396 | |
| | 397 | for (int i = 0; i < usac->nb_elems; i++) { |
| | 398 | AACUsacElemConfig *e = &usac->elems[i]; |
| | 399 | memset(e, 0, sizeof(*e)); |
| | 400 | |
| | 401 | e->type = get_bits(gb, 2); /* usacElementType */ |
| | 402 | av_log(ac->avctx, AV_LOG_DEBUG, "Element present: idx %i, type %i\n", |
| | 403 | i, e->type); |
| | 404 | |
| | 405 | switch (e->type) { |
| | 406 | case ID_USAC_SCE: /* SCE */ |
| | 407 | /* UsacCoreConfig */ |
| | 408 | decode_usac_element_core(e, gb, sbr_ratio); |
| | 409 | if (e->sbr.ratio > 0) |
| | 410 | decode_usac_sbr_data(e, gb); |
| | 411 | layout_map[i][0] = TYPE_SCE; |
| | 412 | layout_map[i][1] = i; |
| | 413 | layout_map[i][2] = AAC_CHANNEL_FRONT; |
| | 414 | elem_id[0]++; |
| | 415 | |
| | 416 | break; |
| | 417 | case ID_USAC_CPE: /* UsacChannelPairElementConf */ |
| | 418 | /* UsacCoreConfig */ |
| | 419 | decode_usac_element_core(e, gb, sbr_ratio); |
| | 420 | decode_usac_element_pair(e, gb); |
| | 421 | layout_map[i][0] = TYPE_CPE; |
| | 422 | layout_map[i][1] = i; |
| | 423 | layout_map[i][2] = AAC_CHANNEL_FRONT; |
| | 424 | elem_id[1]++; |
| | 425 | |
| | 426 | break; |
| | 427 | case ID_USAC_LFE: /* LFE */ |
| | 428 | /* LFE has no need for any configuration */ |
| | 429 | e->tw_mdct = 0; |
| | 430 | e->noise_fill = 0; |
| | 431 | elem_id[2]++; |
| | 432 | break; |
| | 433 | case ID_USAC_EXT: /* EXT */ |
| | 434 | ret = decode_usac_extension(ac, e, gb); |
| | 435 | if (ret < 0) |
| | 436 | return ret; |
| | 437 | break; |
| | 438 | }; |
| | 439 | } |
| | 440 | |
| | 441 | ret = ff_aac_output_configure(ac, layout_map, elem_id[0] + elem_id[1] + elem_id[2], OC_GLOBAL_HDR, 0); |
| | 442 | if (ret < 0) { |
| | 443 | av_log(avctx, AV_LOG_ERROR, "Unable to parse channel config!\n"); |
| | 444 | return ret; |
| | 445 | } |
| | 446 | |
| | 447 | if (get_bits1(gb)) { /* usacConfigExtensionPresent */ |
| | 448 | int invalid; |
| | 449 | int nb_extensions = get_escaped_value(gb, 2, 4, 8) + 1; /* numConfigExtensions */ |
| | 450 | for (int i = 0; i < nb_extensions; i++) { |
| | 451 | int type = get_escaped_value(gb, 4, 8, 16); |
| | 452 | int len = get_escaped_value(gb, 4, 8, 16); |
| | 453 | switch (type) { |
| | 454 | case ID_CONFIG_EXT_LOUDNESS_INFO: |
| | 455 | ret = decode_loudness_set(ac, usac, gb); |
| | 456 | if (ret < 0) |
| | 457 | return ret; |
| | 458 | break; |
| | 459 | case ID_CONFIG_EXT_STREAM_ID: |
| | 460 | usac->stream_identifier = get_bits(gb, 16); |
| | 461 | break; |
| | 462 | case ID_CONFIG_EXT_FILL: /* fallthrough */ |
| | 463 | invalid = 0; |
| | 464 | while (len--) { |
| | 465 | if (get_bits(gb, 8) != 0xA5) |
| | 466 | invalid++; |
| | 467 | } |
| | 468 | if (invalid) |
| | 469 | av_log(avctx, AV_LOG_WARNING, "Invalid fill bytes: %i\n", |
| | 470 | invalid); |
| | 471 | break; |
| | 472 | default: |
| | 473 | while (len--) |
| | 474 | skip_bits(gb, 8); |
| | 475 | break; |
| | 476 | } |
| | 477 | } |
| | 478 | } |
| | 479 | |
| | 480 | ret = ff_aac_usac_reset_state(ac, oc); |
| | 481 | if (ret < 0) |
| | 482 | return ret; |
| | 483 | |
| | 484 | return 0; |
| | 485 | } |
| | 486 | |
| | 487 | static int decode_usac_scale_factors(AACDecContext *ac, |
| | 488 | SingleChannelElement *sce, |
| | 489 | GetBitContext *gb, uint8_t global_gain) |
| | 490 | { |
| | 491 | IndividualChannelStream *ics = &sce->ics; |
| | 492 | |
| | 493 | /* Decode all scalefactors. */ |
| | 494 | int offset_sf = global_gain; |
| | 495 | for (int g = 0; g < ics->num_window_groups; g++) { |
| | 496 | for (int sfb = 0; sfb < ics->max_sfb; sfb++) { |
| | 497 | /* First coefficient is just the global gain */ |
| | 498 | if (!g && !sfb) { |
| | 499 | /* The cannonical representation of quantized scalefactors |
| | 500 | * in the spec is with 100 subtracted. */ |
| | 501 | sce->sfo[0] = offset_sf - 100; |
| | 502 | continue; |
| | 503 | } |
| | 504 | |
| | 505 | offset_sf += get_vlc2(gb, ff_vlc_scalefactors, 7, 3) - SCALE_DIFF_ZERO; |
| | 506 | if (offset_sf > 255U) { |
| | 507 | av_log(ac->avctx, AV_LOG_ERROR, |
| | 508 | "Scalefactor (%d) out of range.\n", offset_sf); |
| | 509 | return AVERROR_INVALIDDATA; |
| | 510 | } |
| | 511 | |
| | 512 | sce->sfo[g*ics->max_sfb + sfb] = offset_sf - 100; |
| | 513 | } |
| | 514 | } |
| | 515 | |
| | 516 | return 0; |
| | 517 | } |
| | 518 | |
| | 519 | /** |
| | 520 | * Decode and dequantize arithmetically coded, uniformly quantized value |
| | 521 | * |
| | 522 | * @param coef array of dequantized, scaled spectral data |
| | 523 | * @param sf array of scalefactors or intensity stereo positions |
| | 524 | * |
| | 525 | * @return Returns error status. 0 - OK, !0 - error |
| | 526 | */ |
| | 527 | static int decode_spectrum_and_dequant_ac(AACDecContext *s, float coef[1024], |
| | 528 | GetBitContext *gb, const float sf[120], |
| | 529 | AACArithState *state, int reset, |
| | 530 | uint16_t len, uint16_t N) |
| | 531 | { |
| | 532 | AACArith ac; |
| | 533 | int i, a, b; |
| | 534 | uint32_t c; |
| | 535 | |
| | 536 | int gb_count; |
| | 537 | GetBitContext gb2; |
| | 538 | |
| | 539 | ff_aac_ac_init(&ac, gb); |
| | 540 | c = ff_aac_ac_map_process(state, reset, N); |
| | 541 | |
| | 542 | /* Backup reader for rolling back by 14 bits at the end */ |
| | 543 | gb2 = (GetBitContext)*gb; |
| | 544 | gb_count = get_bits_count(&gb2); |
| | 545 | |
| | 546 | for (i = 0; i < len/2; i++) { |
| | 547 | /* MSB */ |
| | 548 | int lvl, esc_nb, m; |
| | 549 | c = ff_aac_ac_get_context(state, c, i, N); |
| | 550 | for (lvl=esc_nb=0;;) { |
| | 551 | uint32_t pki = ff_aac_ac_get_pk(c + (esc_nb << 17)); |
| | 552 | m = ff_aac_ac_decode(&ac, &gb2, ff_aac_ac_msb_cdfs[pki], |
| | 553 | FF_ARRAY_ELEMS(ff_aac_ac_msb_cdfs[pki])); |
| | 554 | if (m < FF_AAC_AC_ESCAPE) |
| | 555 | break; |
| | 556 | lvl++; |
| | 557 | |
| | 558 | /* Cargo-culted value. */ |
| | 559 | if (lvl > 23) |
| | 560 | return AVERROR(EINVAL); |
| | 561 | |
| | 562 | if ((esc_nb = lvl) > 7) |
| | 563 | esc_nb = 7; |
| | 564 | } |
| | 565 | |
| | 566 | b = m >> 2; |
| | 567 | a = m - (b << 2); |
| | 568 | |
| | 569 | /* ARITH_STOP detection */ |
| | 570 | if (!m) { |
| | 571 | if (esc_nb) |
| | 572 | break; |
| | 573 | a = b = 0; |
| | 574 | } |
| | 575 | |
| | 576 | /* LSB */ |
| | 577 | for (int l = lvl; l > 0; l--) { |
| | 578 | int lsbidx = !a ? 1 : (!b ? 0 : 2); |
| | 579 | uint8_t r = ff_aac_ac_decode(&ac, &gb2, ff_aac_ac_lsb_cdfs[lsbidx], |
| | 580 | FF_ARRAY_ELEMS(ff_aac_ac_lsb_cdfs[lsbidx])); |
| | 581 | a = (a << 1) | (r & 1); |
| | 582 | b = (b << 1) | ((r >> 1) & 1); |
| | 583 | } |
| | 584 | |
| | 585 | /* Dequantize coeffs here */ |
| | 586 | coef[2*i + 0] = a * cbrt(a); |
| | 587 | coef[2*i + 1] = b * cbrt(b); |
| | 588 | ff_aac_ac_update_context(state, i, a, b); |
| | 589 | } |
| | 590 | |
| | 591 | if (len > 1) { |
| | 592 | /* "Rewind" bitstream back by 14 bits */ |
| | 593 | int gb_count2 = get_bits_count(&gb2); |
| | 594 | skip_bits(gb, gb_count2 - gb_count - 14); |
| | 595 | } else { |
| | 596 | *gb = gb2; |
| | 597 | } |
| | 598 | |
| | 599 | ff_aac_ac_finish(state, i, N); |
| | 600 | |
| | 601 | for (; i < N/2; i++) { |
| | 602 | coef[2*i + 0] = 0; |
| | 603 | coef[2*i + 1] = 0; |
| | 604 | } |
| | 605 | |
| | 606 | /* Signs */ |
| | 607 | for (i = 0; i < len; i++) { |
| | 608 | if (coef[i]) { |
| | 609 | if (!get_bits1(gb)) /* s */ |
| | 610 | coef[i] *= -1; |
| | 611 | } |
| | 612 | } |
| | 613 | |
| | 614 | return 0; |
| | 615 | } |
| | 616 | |
| | 617 | static int decode_usac_stereo_cplx(AACDecContext *ac, AACUsacStereo *us, |
| | 618 | ChannelElement *cpe, GetBitContext *gb, |
| | 619 | int num_window_groups, int indep_flag) |
| | 620 | { |
| | 621 | int delta_code_time; |
| | 622 | IndividualChannelStream *ics = &cpe->ch[0].ics; |
| | 623 | |
| | 624 | if (!get_bits1(gb)) { /* cplx_pred_all */ |
| | 625 | for (int g = 0; g < num_window_groups; g++) { |
| | 626 | for (int sfb = 0; sfb < cpe->max_sfb_ste; sfb += SFB_PER_PRED_BAND) { |
| | 627 | const uint8_t val = get_bits1(gb); |
| | 628 | us->pred_used[g*cpe->max_sfb_ste + sfb] = val; |
| | 629 | if ((sfb + 1) < cpe->max_sfb_ste) |
| | 630 | us->pred_used[g*cpe->max_sfb_ste + sfb + 1] = val; |
| | 631 | } |
| | 632 | } |
| | 633 | } else { |
| | 634 | for (int g = 0; g < num_window_groups; g++) |
| | 635 | for (int sfb = 0; sfb < cpe->max_sfb_ste; sfb++) |
| | 636 | us->pred_used[g*cpe->max_sfb_ste + sfb] = 1; |
| | 637 | } |
| | 638 | |
| | 639 | us->pred_dir = get_bits1(gb); |
| | 640 | us->complex_coef = get_bits1(gb); |
| | 641 | |
| | 642 | us->use_prev_frame = 0; |
| | 643 | if (us->complex_coef && !indep_flag) |
| | 644 | us->use_prev_frame = get_bits1(gb); |
| | 645 | |
| | 646 | delta_code_time = 0; |
| | 647 | if (!indep_flag) |
| | 648 | delta_code_time = get_bits1(gb); |
| | 649 | |
| | 650 | /* TODO: shouldn't be needed */ |
| | 651 | for (int g = 0; g < num_window_groups; g++) { |
| | 652 | for (int sfb = 0; sfb < cpe->max_sfb_ste; sfb += SFB_PER_PRED_BAND) { |
| | 653 | float last_alpha_q_re = 0; |
| | 654 | float last_alpha_q_im = 0; |
| | 655 | if (delta_code_time) { |
| | 656 | if (g) { |
| | 657 | last_alpha_q_re = us->prev_alpha_q_re[(g - 1)*cpe->max_sfb_ste + sfb]; |
| | 658 | last_alpha_q_im = us->prev_alpha_q_im[(g - 1)*cpe->max_sfb_ste + sfb]; |
| | 659 | } else if ((ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) && |
| | 660 | ics->window_sequence[1] == EIGHT_SHORT_SEQUENCE || |
| | 661 | ics->window_sequence[1] == EIGHT_SHORT_SEQUENCE) { |
| | 662 | /* The spec doesn't explicitly mention this, but it doesn't make |
| | 663 | * any other sense otherwise! */ |
| | 664 | last_alpha_q_re = us->prev_alpha_q_re[7*cpe->max_sfb_ste + sfb]; |
| | 665 | last_alpha_q_im = us->prev_alpha_q_im[7*cpe->max_sfb_ste + sfb]; |
| | 666 | } else { |
| | 667 | last_alpha_q_re = us->prev_alpha_q_re[g*cpe->max_sfb_ste + sfb]; |
| | 668 | last_alpha_q_im = us->prev_alpha_q_im[g*cpe->max_sfb_ste + sfb]; |
| | 669 | } |
| | 670 | } else { |
| | 671 | if (sfb) { |
| | 672 | last_alpha_q_re = us->alpha_q_re[g*cpe->max_sfb_ste + sfb - 1]; |
| | 673 | last_alpha_q_im = us->alpha_q_im[g*cpe->max_sfb_ste + sfb - 1]; |
| | 674 | } |
| | 675 | } |
| | 676 | |
| | 677 | if (us->pred_used[g*cpe->max_sfb_ste + sfb]) { |
| | 678 | int val = -get_vlc2(gb, ff_vlc_scalefactors, 7, 3) + 60; |
| | 679 | last_alpha_q_re += val * 0.1f; |
| | 680 | if (us->complex_coef) { |
| | 681 | val = -get_vlc2(gb, ff_vlc_scalefactors, 7, 3) + 60; |
| | 682 | last_alpha_q_im += val * 0.1f; |
| | 683 | } |
| | 684 | us->alpha_q_re[g*cpe->max_sfb_ste + sfb] = last_alpha_q_re; |
| | 685 | us->alpha_q_im[g*cpe->max_sfb_ste + sfb] = last_alpha_q_im; |
| | 686 | } else { |
| | 687 | us->alpha_q_re[g*cpe->max_sfb_ste + sfb] = 0; |
| | 688 | us->alpha_q_im[g*cpe->max_sfb_ste + sfb] = 0; |
| | 689 | } |
| | 690 | |
| | 691 | if ((sfb + 1) < cpe->max_sfb_ste) { |
| | 692 | us->alpha_q_re[g*cpe->max_sfb_ste + sfb + 1] = |
| | 693 | us->alpha_q_re[g*cpe->max_sfb_ste + sfb]; |
| | 694 | us->alpha_q_im[g*cpe->max_sfb_ste + sfb + 1] = |
| | 695 | us->alpha_q_im[g*cpe->max_sfb_ste + sfb]; |
| | 696 | } |
| | 697 | } |
| | 698 | } |
| | 699 | |
| | 700 | return 0; |
| | 701 | } |
| | 702 | |
| | 703 | static int setup_sce(AACDecContext *ac, SingleChannelElement *sce, |
| | 704 | AACUSACConfig *usac) |
| | 705 | { |
| | 706 | AACUsacElemData *ue = &sce->ue; |
| | 707 | IndividualChannelStream *ics = &sce->ics; |
| | 708 | |
| | 709 | /* Setup window parameters */ |
| | 710 | if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) { |
| | 711 | if (usac->core_frame_len == 768) { |
| | 712 | ics->swb_offset = ff_swb_offset_96[usac->rate_idx]; |
| | 713 | ics->num_swb = ff_aac_num_swb_96[usac->rate_idx]; |
| | 714 | } else { |
| | 715 | ics->swb_offset = ff_swb_offset_128[usac->rate_idx]; |
| | 716 | ics->num_swb = ff_aac_num_swb_128[usac->rate_idx]; |
| | 717 | } |
| | 718 | ics->tns_max_bands = ff_tns_max_bands_128[usac->rate_idx]; |
| | 719 | |
| | 720 | /* Setup scalefactor grouping. 7 bit mask. */ |
| | 721 | ics->num_window_groups = 0; |
| | 722 | for (int j = 0; j < 7; j++) { |
| | 723 | ics->group_len[j] = 1; |
| | 724 | if (ue->scale_factor_grouping & (1 << (6 - j))) |
| | 725 | ics->group_len[ics->num_window_groups] += 1; |
| | 726 | else |
| | 727 | ics->num_window_groups++; |
| | 728 | } |
| | 729 | |
| | 730 | ics->group_len[7] = 1; |
| | 731 | ics->num_window_groups++; |
| | 732 | ics->num_windows = 8; |
| | 733 | } else { |
| | 734 | if (usac->core_frame_len == 768) { |
| | 735 | ics->swb_offset = ff_swb_offset_768[usac->rate_idx]; |
| | 736 | ics->num_swb = ff_aac_num_swb_768[usac->rate_idx]; |
| | 737 | } else { |
| | 738 | ics->swb_offset = ff_swb_offset_1024[usac->rate_idx]; |
| | 739 | ics->num_swb = ff_aac_num_swb_1024[usac->rate_idx]; |
| | 740 | } |
| | 741 | ics->tns_max_bands = ff_tns_max_bands_1024[usac->rate_idx]; |
| | 742 | |
| | 743 | ics->group_len[0] = 1; |
| | 744 | ics->num_window_groups = 1; |
| | 745 | ics->num_windows = 1; |
| | 746 | } |
| | 747 | |
| | 748 | if (ics->max_sfb > ics->num_swb) { |
| | 749 | av_log(ac->avctx, AV_LOG_ERROR, |
| | 750 | "Number of scalefactor bands in group (%d) " |
| | 751 | "exceeds limit (%d).\n", |
| | 752 | ics->max_sfb, ics->num_swb); |
| | 753 | return AVERROR(EINVAL); |
| | 754 | } |
| | 755 | |
| | 756 | /* Just some defaults for the band types */ |
| | 757 | for (int i = 0; i < FF_ARRAY_ELEMS(sce->band_type); i++) |
| | 758 | sce->band_type[i] = ESC_BT; |
| | 759 | |
| | 760 | return 0; |
| | 761 | } |
| | 762 | |
| | 763 | static int decode_usac_stereo_info(AACDecContext *ac, AACUSACConfig *usac, |
| | 764 | AACUsacElemConfig *ec, ChannelElement *cpe, |
| | 765 | GetBitContext *gb, int indep_flag) |
| | 766 | { |
| | 767 | int ret, tns_active; |
| | 768 | |
| | 769 | AACUsacStereo *us = &cpe->us; |
| | 770 | SingleChannelElement *sce1 = &cpe->ch[0]; |
| | 771 | SingleChannelElement *sce2 = &cpe->ch[1]; |
| | 772 | IndividualChannelStream *ics1 = &sce1->ics; |
| | 773 | IndividualChannelStream *ics2 = &sce2->ics; |
| | 774 | AACUsacElemData *ue1 = &sce1->ue; |
| | 775 | AACUsacElemData *ue2 = &sce2->ue; |
| | 776 | |
| | 777 | us->common_window = 0; |
| | 778 | us->common_tw = 0; |
| | 779 | |
| | 780 | if (!(!ue1->core_mode && !ue2->core_mode)) |
| | 781 | return 0; |
| | 782 | |
| | 783 | tns_active = get_bits1(gb); |
| | 784 | us->common_window = get_bits1(gb); |
| | 785 | |
| | 786 | if (us->common_window) { |
| | 787 | /* ics_info() */ |
| | 788 | ics1->window_sequence[1] = ics1->window_sequence[0]; |
| | 789 | ics2->window_sequence[1] = ics2->window_sequence[0]; |
| | 790 | ics1->window_sequence[0] = ics2->window_sequence[0] = get_bits(gb, 2); |
| | 791 | |
| | 792 | ics1->use_kb_window[1] = ics1->use_kb_window[0]; |
| | 793 | ics2->use_kb_window[1] = ics2->use_kb_window[0]; |
| | 794 | ics1->use_kb_window[0] = ics2->use_kb_window[0] = get_bits1(gb); |
| | 795 | |
| | 796 | if (ics1->window_sequence[0] == EIGHT_SHORT_SEQUENCE) { |
| | 797 | ics1->max_sfb = ics2->max_sfb = get_bits(gb, 4); |
| | 798 | ue1->scale_factor_grouping = ue2->scale_factor_grouping = get_bits(gb, 7); |
| | 799 | } else { |
| | 800 | ics1->max_sfb = ics2->max_sfb = get_bits(gb, 6); |
| | 801 | } |
| | 802 | |
| | 803 | if (!get_bits1(gb)) { /* common_max_sfb */ |
| | 804 | if (ics2->window_sequence[0] == EIGHT_SHORT_SEQUENCE) |
| | 805 | ics2->max_sfb = get_bits(gb, 4); |
| | 806 | else |
| | 807 | ics2->max_sfb = get_bits(gb, 6); |
| | 808 | } |
| | 809 | |
| | 810 | ret = setup_sce(ac, sce1, usac); |
| | 811 | if (ret < 0) |
| | 812 | return ret; |
| | 813 | |
| | 814 | ret = setup_sce(ac, sce2, usac); |
| | 815 | if (ret < 0) |
| | 816 | return ret; |
| | 817 | |
| | 818 | cpe->max_sfb_ste = FFMAX(ics1->max_sfb, ics2->max_sfb); |
| | 819 | |
| | 820 | us->ms_mask_mode = get_bits(gb, 2); /* ms_mask_present */ |
| | 821 | memset(cpe->ms_mask, 0, sizeof(cpe->ms_mask)); |
| | 822 | if (us->ms_mask_mode == 1) { |
| | 823 | for (int g = 0; g < ics1->num_window_groups; g++) |
| | 824 | for (int sfb = 0; sfb < cpe->max_sfb_ste; sfb++) |
| | 825 | cpe->ms_mask[g*cpe->max_sfb_ste + sfb] = get_bits1(gb); |
| | 826 | } else if (us->ms_mask_mode == 2) { |
| | 827 | memset(cpe->ms_mask, 0xFF, sizeof(cpe->ms_mask)); |
| | 828 | } else if ((us->ms_mask_mode == 3) && !ec->stereo_config_index) { |
| | 829 | ret = decode_usac_stereo_cplx(ac, us, cpe, gb, |
| | 830 | ics1->num_window_groups, indep_flag); |
| | 831 | if (ret < 0) |
| | 832 | return ret; |
| | 833 | } |
| | 834 | } |
| | 835 | |
| | 836 | if (ec->tw_mdct) { |
| | 837 | us->common_tw = get_bits1(gb); |
| | 838 | avpriv_report_missing_feature(ac->avctx, |
| | 839 | "AAC USAC timewarping"); |
| | 840 | return AVERROR_PATCHWELCOME; |
| | 841 | } |
| | 842 | |
| | 843 | sce1->tns.present = sce2->tns.present = 0; |
| | 844 | if (tns_active) { |
| | 845 | av_unused int tns_on_lr; |
| | 846 | int common_tns = 0; |
| | 847 | if (us->common_window) |
| | 848 | common_tns = get_bits1(gb); |
| | 849 | |
| | 850 | tns_on_lr = get_bits1(gb); |
| | 851 | if (common_tns) { |
| | 852 | ret = ff_aac_decode_tns(ac, &sce1->tns, gb, ics1); |
| | 853 | if (ret < 0) |
| | 854 | return ret; |
| | 855 | memcpy(&sce2->tns, &sce1->tns, sizeof(sce1->tns)); |
| | 856 | sce2->tns.present = 0; |
| | 857 | sce1->tns.present = 0; |
| | 858 | } else { |
| | 859 | if (get_bits1(gb)) { |
| | 860 | sce2->tns.present = 1; |
| | 861 | sce1->tns.present = 1; |
| | 862 | } else { |
| | 863 | sce2->tns.present = get_bits1(gb); |
| | 864 | sce1->tns.present = !sce2->tns.present; |
| | 865 | } |
| | 866 | } |
| | 867 | } |
| | 868 | |
| | 869 | return 0; |
| | 870 | } |
| | 871 | |
| | 872 | /* 7.2.4 Generation of random signs for spectral noise filling |
| | 873 | * This function is exactly defined, though we've helped the definition |
| | 874 | * along with being slightly faster. */ |
| | 875 | static inline float noise_random_sign(unsigned int *seed) |
| | 876 | { |
| | 877 | unsigned int new_seed = *seed = ((*seed) * 69069) + 5; |
| | 878 | if (((new_seed) & 0x10000) > 0) |
| | 879 | return -1.f; |
| | 880 | return +1.f; |
| | 881 | } |
| | 882 | |
| | 883 | static void apply_noise_fill(AACDecContext *ac, SingleChannelElement *sce, |
| | 884 | AACUsacElemData *ue) |
| | 885 | { |
| | 886 | float *coef; |
| | 887 | IndividualChannelStream *ics = &sce->ics; |
| | 888 | |
| | 889 | float noise_val = pow(2, (ue->noise.level - 14)/3); |
| | 890 | int noise_offset = ue->noise.offset - 16; |
| | 891 | int band_off; |
| | 892 | |
| | 893 | band_off = ff_usac_noise_fill_start_offset[ac->oc[1].m4ac.frame_length_short] |
| | 894 | [ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE]; |
| | 895 | |
| | 896 | coef = sce->coeffs; |
| | 897 | for (int g = 0; g < ics->num_window_groups; g++) { |
| | 898 | unsigned g_len = ics->group_len[g]; |
| | 899 | |
| | 900 | for (int sfb = 0; sfb < ics->max_sfb; sfb++) { |
| | 901 | float *cb = coef + ics->swb_offset[sfb]; |
| | 902 | int cb_len = ics->swb_offset[sfb + 1] - ics->swb_offset[sfb]; |
| | 903 | int band_quantized_to_zero = 1; |
| | 904 | |
| | 905 | if (ics->swb_offset[sfb] < band_off) |
| | 906 | continue; |
| | 907 | |
| | 908 | for (int group = 0; group < (unsigned)g_len; group++, cb += 128) { |
| | 909 | for (int z = 0; z < cb_len; z++) { |
| | 910 | if (cb[z] == 0) |
| | 911 | cb[z] = noise_random_sign(&sce->ue.noise.seed) * noise_val; |
| | 912 | else |
| | 913 | band_quantized_to_zero = 0; |
| | 914 | } |
| | 915 | } |
| | 916 | |
| | 917 | if (band_quantized_to_zero) |
| | 918 | sce->sf[g*ics->max_sfb + sfb] += noise_offset; |
| | 919 | } |
| | 920 | coef += g_len << 7; |
| | 921 | } |
| | 922 | } |
| | 923 | |
| | 924 | static void spectrum_scale(AACDecContext *ac, SingleChannelElement *sce, |
| | 925 | AACUsacElemData *ue) |
| | 926 | { |
| | 927 | IndividualChannelStream *ics = &sce->ics; |
| | 928 | float *coef; |
| | 929 | |
| | 930 | /* Synthesise noise */ |
| | 931 | if (ue->noise.level) |
| | 932 | apply_noise_fill(ac, sce, ue); |
| | 933 | |
| | 934 | /* Apply scalefactors */ |
| | 935 | coef = sce->coeffs; |
| | 936 | for (int g = 0; g < ics->num_window_groups; g++) { |
| | 937 | unsigned g_len = ics->group_len[g]; |
| | 938 | |
| | 939 | for (int sfb = 0; sfb < ics->max_sfb; sfb++) { |
| | 940 | float *cb = coef + ics->swb_offset[sfb]; |
| | 941 | int cb_len = ics->swb_offset[sfb + 1] - ics->swb_offset[sfb]; |
| | 942 | float sf = sce->sf[g*ics->max_sfb + sfb]; |
| | 943 | |
| | 944 | for (int group = 0; group < (unsigned)g_len; group++, cb += 128) |
| | 945 | ac->fdsp->vector_fmul_scalar(cb, cb, sf, cb_len); |
| | 946 | } |
| | 947 | coef += g_len << 7; |
| | 948 | } |
| | 949 | } |
| | 950 | |
| | 951 | static void complex_stereo_downmix_prev(AACDecContext *ac, ChannelElement *cpe, |
| | 952 | float *dmix_re) |
| | 953 | { |
| | 954 | IndividualChannelStream *ics = &cpe->ch[0].ics; |
| | 955 | int sign = !cpe->us.pred_dir ? +1 : -1; |
| | 956 | float *coef1 = cpe->ch[0].coeffs; |
| | 957 | float *coef2 = cpe->ch[1].coeffs; |
| | 958 | |
| | 959 | for (int g = 0; g < ics->num_window_groups; g++) { |
| | 960 | unsigned g_len = ics->group_len[g]; |
| | 961 | for (int sfb = 0; sfb < cpe->max_sfb_ste; sfb++) { |
| | 962 | int off = ics->swb_offset[sfb]; |
| | 963 | int cb_len = ics->swb_offset[sfb + 1] - off; |
| | 964 | |
| | 965 | float *c1 = coef1 + off; |
| | 966 | float *c2 = coef2 + off; |
| | 967 | float *dm = dmix_re + off; |
| | 968 | |
| | 969 | for (int group = 0; group < (unsigned)g_len; |
| | 970 | group++, c1 += 128, c2 += 128, dm += 128) { |
| | 971 | for (int z = 0; z < cb_len; z++) |
| | 972 | dm[z] = 0.5*(c1[z] + sign*c2[z]); |
| | 973 | } |
| | 974 | } |
| | 975 | |
| | 976 | coef1 += g_len << 7; |
| | 977 | coef2 += g_len << 7; |
| | 978 | dmix_re += g_len << 7; |
| | 979 | } |
| | 980 | } |
| | 981 | |
| | 982 | static void complex_stereo_downmix_cur(AACDecContext *ac, ChannelElement *cpe, |
| | 983 | float *dmix_re) |
| | 984 | { |
| | 985 | AACUsacStereo *us = &cpe->us; |
| | 986 | IndividualChannelStream *ics = &cpe->ch[0].ics; |
| | 987 | int sign = !cpe->us.pred_dir ? +1 : -1; |
| | 988 | float *coef1 = cpe->ch[0].coeffs; |
| | 989 | float *coef2 = cpe->ch[1].coeffs; |
| | 990 | |
| | 991 | for (int g = 0; g < ics->num_window_groups; g++) { |
| | 992 | unsigned g_len = ics->group_len[g]; |
| | 993 | for (int sfb = 0; sfb < cpe->max_sfb_ste; sfb++) { |
| | 994 | int off = ics->swb_offset[sfb]; |
| | 995 | int cb_len = ics->swb_offset[sfb + 1] - off; |
| | 996 | |
| | 997 | float *c1 = coef1 + off; |
| | 998 | float *c2 = coef2 + off; |
| | 999 | float *dm = dmix_re + off; |
| | 1000 | |
| | 1001 | if (us->pred_used[g*cpe->max_sfb_ste + sfb]) { |
| | 1002 | for (int group = 0; group < (unsigned)g_len; |
| | 1003 | group++, c1 += 128, c2 += 128, dm += 128) { |
| | 1004 | for (int z = 0; z < cb_len; z++) |
| | 1005 | dm[z] = 0.5*(c1[z] + sign*c2[z]); |
| | 1006 | } |
| | 1007 | } else { |
| | 1008 | for (int group = 0; group < (unsigned)g_len; |
| | 1009 | group++, c1 += 128, c2 += 128, dm += 128) { |
| | 1010 | for (int z = 0; z < cb_len; z++) |
| | 1011 | dm[z] = c1[z]; |
| | 1012 | } |
| | 1013 | } |
| | 1014 | } |
| | 1015 | |
| | 1016 | coef1 += g_len << 7; |
| | 1017 | coef2 += g_len << 7; |
| | 1018 | dmix_re += g_len << 7; |
| | 1019 | } |
| | 1020 | } |
| | 1021 | |
| | 1022 | static void complex_stereo_interpolate_imag(float *im, float *re, const float f[6], |
| | 1023 | int len, int factor_even, int factor_odd) |
| | 1024 | { |
| | 1025 | int i = 0; |
| | 1026 | float s; |
| | 1027 | |
| | 1028 | s = f[6]*re[2] + f[5]*re[1] + f[4]*re[0] + |
| | 1029 | f[3]*re[0] + |
| | 1030 | f[2]*re[1] + f[1]*re[2] + f[0]*re[3]; |
| | 1031 | im[i] += s*factor_even; |
| | 1032 | |
| | 1033 | i = 1; |
| | 1034 | s = f[6]*re[1] + f[5]*re[0] + f[4]*re[0] + |
| | 1035 | f[3]*re[1] + |
| | 1036 | f[2]*re[2] + f[1]*re[3] + f[0]*re[4]; |
| | 1037 | im[i] += s*factor_odd; |
| | 1038 | |
| | 1039 | i = 2; |
| | 1040 | s = f[6]*re[0] + f[5]*re[0] + f[4]*re[1] + |
| | 1041 | f[3]*re[2] + |
| | 1042 | f[2]*re[3] + f[1]*re[4] + f[0]*re[5]; |
| | 1043 | |
| | 1044 | im[i] += s*factor_even; |
| | 1045 | for (i = 3; i < len - 4; i += 2) { |
| | 1046 | s = f[6]*re[i-3] + f[5]*re[i-2] + f[4]*re[i-1] + |
| | 1047 | f[3]*re[i] + |
| | 1048 | f[2]*re[i+1] + f[1]*re[i+2] + f[0]*re[i+3]; |
| | 1049 | im[i+0] += s*factor_odd; |
| | 1050 | |
| | 1051 | s = f[6]*re[i-2] + f[5]*re[i-1] + f[4]*re[i] + |
| | 1052 | f[3]*re[i+1] + |
| | 1053 | f[2]*re[i+2] + f[1]*re[i+3] + f[0]*re[i+4]; |
| | 1054 | im[i+1] += s*factor_even; |
| | 1055 | } |
| | 1056 | |
| | 1057 | i = len - 3; |
| | 1058 | s = f[6]*re[i-3] + f[5]*re[i-2] + f[4]*re[i-1] + |
| | 1059 | f[3]*re[i] + |
| | 1060 | f[2]*re[i+1] + f[1]*re[i+2] + f[0]*re[i+2]; |
| | 1061 | im[i] += s*factor_odd; |
| | 1062 | |
| | 1063 | i = len - 2; |
| | 1064 | s = f[6]*re[i-3] + f[5]*re[i-2] + f[4]*re[i-1] + |
| | 1065 | f[3]*re[i] + |
| | 1066 | f[2]*re[i+1] + f[1]*re[i+1] + f[0]*re[i]; |
| | 1067 | im[i] += s*factor_even; |
| | 1068 | |
| | 1069 | i = len - 1; |
| | 1070 | s = f[6]*re[i-3] + f[5]*re[i-2] + f[4]*re[i-1] + |
| | 1071 | f[3]*re[i] + |
| | 1072 | f[2]*re[i] + f[1]*re[i-1] + f[0]*re[i-2]; |
| | 1073 | im[i] += s*factor_odd; |
| | 1074 | } |
| | 1075 | |
| | 1076 | static void apply_complex_stereo(AACDecContext *ac, ChannelElement *cpe) |
| | 1077 | { |
| | 1078 | AACUsacStereo *us = &cpe->us; |
| | 1079 | IndividualChannelStream *ics = &cpe->ch[0].ics; |
| | 1080 | float *coef1 = cpe->ch[0].coeffs; |
| | 1081 | float *coef2 = cpe->ch[1].coeffs; |
| | 1082 | float *dmix_im = us->dmix_im; |
| | 1083 | |
| | 1084 | for (int g = 0; g < ics->num_window_groups; g++) { |
| | 1085 | unsigned g_len = ics->group_len[g]; |
| | 1086 | for (int sfb = 0; sfb < cpe->max_sfb_ste; sfb++) { |
| | 1087 | int off = ics->swb_offset[sfb]; |
| | 1088 | int cb_len = ics->swb_offset[sfb + 1] - off; |
| | 1089 | |
| | 1090 | float *c1 = coef1 + off; |
| | 1091 | float *c2 = coef2 + off; |
| | 1092 | float *dm_im = dmix_im + off; |
| | 1093 | float alpha_re = us->alpha_q_re[g*cpe->max_sfb_ste + sfb]; |
| | 1094 | float alpha_im = us->alpha_q_im[g*cpe->max_sfb_ste + sfb]; |
| | 1095 | |
| | 1096 | if (!us->pred_used[g*cpe->max_sfb_ste + sfb]) |
| | 1097 | continue; |
| | 1098 | |
| | 1099 | if (!cpe->us.pred_dir) { |
| | 1100 | for (int group = 0; group < (unsigned)g_len; |
| | 1101 | group++, c1 += 128, c2 += 128, dm_im += 128) { |
| | 1102 | for (int z = 0; z < cb_len; z++) { |
| | 1103 | float side; |
| | 1104 | side = c2[z] - alpha_re*c1[z] - alpha_im*dm_im[z]; |
| | 1105 | c2[z] = c1[z] - side; |
| | 1106 | c1[z] = c1[z] + side; |
| | 1107 | } |
| | 1108 | } |
| | 1109 | } else { |
| | 1110 | for (int group = 0; group < (unsigned)g_len; |
| | 1111 | group++, c1 += 128, c2 += 128, dm_im += 128) { |
| | 1112 | for (int z = 0; z < cb_len; z++) { |
| | 1113 | float mid; |
| | 1114 | mid = c2[z] - alpha_re*c1[z] - alpha_im*dm_im[z]; |
| | 1115 | c2[z] = mid - c1[z]; |
| | 1116 | c1[z] = mid + c1[z]; |
| | 1117 | } |
| | 1118 | } |
| | 1119 | } |
| | 1120 | } |
| | 1121 | |
| | 1122 | coef1 += g_len << 7; |
| | 1123 | coef2 += g_len << 7; |
| | 1124 | dmix_im += g_len << 7; |
| | 1125 | } |
| | 1126 | } |
| | 1127 | |
| | 1128 | static const float *complex_stereo_get_filter(ChannelElement *cpe, int is_prev) |
| | 1129 | { |
| | 1130 | int win, shape; |
| | 1131 | if (!is_prev) { |
| | 1132 | switch (cpe->ch[0].ics.window_sequence[0]) { |
| | 1133 | default: |
| | 1134 | case ONLY_LONG_SEQUENCE: |
| | 1135 | case EIGHT_SHORT_SEQUENCE: |
| | 1136 | win = 0; |
| | 1137 | break; |
| | 1138 | case LONG_START_SEQUENCE: |
| | 1139 | win = 1; |
| | 1140 | break; |
| | 1141 | case LONG_STOP_SEQUENCE: |
| | 1142 | win = 2; |
| | 1143 | break; |
| | 1144 | } |
| | 1145 | |
| | 1146 | if (cpe->ch[0].ics.use_kb_window[0] == 0 && |
| | 1147 | cpe->ch[0].ics.use_kb_window[1] == 0) |
| | 1148 | shape = 0; |
| | 1149 | else if (cpe->ch[0].ics.use_kb_window[0] == 1 && |
| | 1150 | cpe->ch[0].ics.use_kb_window[1] == 1) |
| | 1151 | shape = 1; |
| | 1152 | else if (cpe->ch[0].ics.use_kb_window[0] == 0 && |
| | 1153 | cpe->ch[0].ics.use_kb_window[1] == 1) |
| | 1154 | shape = 2; |
| | 1155 | else if (cpe->ch[0].ics.use_kb_window[0] == 1 && |
| | 1156 | cpe->ch[0].ics.use_kb_window[1] == 0) |
| | 1157 | shape = 3; |
| | 1158 | else |
| | 1159 | shape = 3; |
| | 1160 | } else { |
| | 1161 | win = cpe->ch[0].ics.window_sequence[0] == LONG_STOP_SEQUENCE; |
| | 1162 | shape = cpe->ch[0].ics.use_kb_window[1]; |
| | 1163 | } |
| | 1164 | |
| | 1165 | return ff_aac_usac_mdst_filt_cur[win][shape]; |
| | 1166 | } |
| | 1167 | |
| | 1168 | static void spectrum_decode(AACDecContext *ac, AACUSACConfig *usac, |
| | 1169 | ChannelElement *cpe, int nb_channels) |
| | 1170 | { |
| | 1171 | AACUsacStereo *us = &cpe->us; |
| | 1172 | |
| | 1173 | for (int ch = 0; ch < nb_channels; ch++) { |
| | 1174 | SingleChannelElement *sce = &cpe->ch[ch]; |
| | 1175 | AACUsacElemData *ue = &sce->ue; |
| | 1176 | |
| | 1177 | spectrum_scale(ac, sce, ue); |
| | 1178 | } |
| | 1179 | |
| | 1180 | if (nb_channels > 1 && us->common_window) { |
| | 1181 | if (us->ms_mask_mode == 3) { |
| | 1182 | const float *filt; |
| | 1183 | complex_stereo_downmix_cur(ac, cpe, us->dmix_re); |
| | 1184 | complex_stereo_downmix_prev(ac, cpe, us->prev_dmix_re); |
| | 1185 | |
| | 1186 | filt = complex_stereo_get_filter(cpe, 0); |
| | 1187 | complex_stereo_interpolate_imag(us->dmix_im, us->dmix_re, filt, |
| | 1188 | usac->core_frame_len, 1, 1); |
| | 1189 | if (us->use_prev_frame) { |
| | 1190 | filt = complex_stereo_get_filter(cpe, 1); |
| | 1191 | complex_stereo_interpolate_imag(us->dmix_im, us->prev_dmix_re, filt, |
| | 1192 | usac->core_frame_len, -1, 1); |
| | 1193 | } |
| | 1194 | |
| | 1195 | apply_complex_stereo(ac, cpe); |
| | 1196 | } else if (us->ms_mask_mode > 0) { |
| | 1197 | ac->dsp.apply_mid_side_stereo(ac, cpe); |
| | 1198 | } |
| | 1199 | } |
| | 1200 | |
| | 1201 | /* Save coefficients and alpha values for prediction reasons */ |
| | 1202 | if (nb_channels > 1) { |
| | 1203 | AACUsacStereo *us = &cpe->us; |
| | 1204 | for (int ch = 0; ch < nb_channels; ch++) { |
| | 1205 | SingleChannelElement *sce = &cpe->ch[ch]; |
| | 1206 | memcpy(sce->prev_coeffs, sce->coeffs, sizeof(sce->coeffs)); |
| | 1207 | } |
| | 1208 | memcpy(us->prev_alpha_q_re, us->alpha_q_re, sizeof(us->alpha_q_re)); |
| | 1209 | memcpy(us->prev_alpha_q_im, us->alpha_q_im, sizeof(us->alpha_q_im)); |
| | 1210 | } |
| | 1211 | |
| | 1212 | for (int ch = 0; ch < nb_channels; ch++) { |
| | 1213 | SingleChannelElement *sce = &cpe->ch[ch]; |
| | 1214 | |
| | 1215 | /* Apply TNS */ |
| | 1216 | if (sce->tns.present) |
| | 1217 | ac->dsp.apply_tns(sce->coeffs, &sce->tns, &sce->ics, 1); |
| | 1218 | |
| | 1219 | ac->oc[1].m4ac.frame_length_short ? ac->dsp.imdct_and_windowing_768(ac, sce) : |
| | 1220 | ac->dsp.imdct_and_windowing(ac, sce); |
| | 1221 | } |
| | 1222 | } |
| | 1223 | |
| | 1224 | static int decode_usac_core_coder(AACDecContext *ac, AACUSACConfig *usac, |
| | 1225 | AACUsacElemConfig *ec, ChannelElement *che, |
| | 1226 | GetBitContext *gb, int indep_flag, int nb_channels) |
| | 1227 | { |
| | 1228 | int ret; |
| | 1229 | int arith_reset_flag; |
| | 1230 | AACUsacStereo *us = &che->us; |
| | 1231 | |
| | 1232 | /* Local symbols */ |
| | 1233 | uint8_t global_gain; |
| | 1234 | |
| | 1235 | us->common_window = 0; |
| | 1236 | che->ch[0].tns.present = che->ch[1].tns.present = 0; |
| | 1237 | |
| | 1238 | for (int ch = 0; ch < nb_channels; ch++) { |
| | 1239 | SingleChannelElement *sce = &che->ch[ch]; |
| | 1240 | AACUsacElemData *ue = &sce->ue; |
| | 1241 | |
| | 1242 | ue->core_mode = get_bits1(gb); |
| | 1243 | } |
| | 1244 | |
| | 1245 | if (nb_channels == 2) { |
| | 1246 | ret = decode_usac_stereo_info(ac, usac, ec, che, gb, indep_flag); |
| | 1247 | if (ret) |
| | 1248 | return ret; |
| | 1249 | } |
| | 1250 | |
| | 1251 | for (int ch = 0; ch < nb_channels; ch++) { |
| | 1252 | SingleChannelElement *sce = &che->ch[ch]; |
| | 1253 | IndividualChannelStream *ics = &sce->ics; |
| | 1254 | AACUsacElemData *ue = &sce->ue; |
| | 1255 | |
| | 1256 | if (ue->core_mode) { /* lpd_channel_stream */ |
| | 1257 | ret = ff_aac_ldp_parse_channel_stream(ac, usac, ue, gb); |
| | 1258 | if (ret < 0) |
| | 1259 | return ret; |
| | 1260 | } |
| | 1261 | |
| | 1262 | if ((nb_channels == 1) || |
| | 1263 | (che->ch[0].ue.core_mode != che->ch[1].ue.core_mode)) |
| | 1264 | sce->tns.present = get_bits1(gb); |
| | 1265 | |
| | 1266 | /* fd_channel_stream */ |
| | 1267 | global_gain = get_bits(gb, 8); |
| | 1268 | |
| | 1269 | ue->noise.level = 0; |
| | 1270 | if (ec->noise_fill) { |
| | 1271 | ue->noise.level = get_bits(gb, 3); |
| | 1272 | ue->noise.offset = get_bits(gb, 5); |
| | 1273 | } |
| | 1274 | |
| | 1275 | if (!us->common_window) { |
| | 1276 | /* ics_info() */ |
| | 1277 | ics->window_sequence[1] = ics->window_sequence[0]; |
| | 1278 | ics->window_sequence[0] = get_bits(gb, 2); |
| | 1279 | ics->use_kb_window[1] = ics->use_kb_window[0]; |
| | 1280 | ics->use_kb_window[0] = get_bits1(gb); |
| | 1281 | if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) { |
| | 1282 | ics->max_sfb = get_bits(gb, 4); |
| | 1283 | ue->scale_factor_grouping = get_bits(gb, 7); |
| | 1284 | } else { |
| | 1285 | ics->max_sfb = get_bits(gb, 6); |
| | 1286 | } |
| | 1287 | |
| | 1288 | ret = setup_sce(ac, sce, usac); |
| | 1289 | if (ret < 0) |
| | 1290 | return ret; |
| | 1291 | } |
| | 1292 | |
| | 1293 | if (ec->tw_mdct && !us->common_tw) { |
| | 1294 | /* tw_data() */ |
| | 1295 | if (get_bits1(gb)) { /* tw_data_present */ |
| | 1296 | /* Time warping is not supported in baseline profile streams. */ |
| | 1297 | avpriv_report_missing_feature(ac->avctx, |
| | 1298 | "AAC USAC timewarping"); |
| | 1299 | return AVERROR_PATCHWELCOME; |
| | 1300 | } |
| | 1301 | } |
| | 1302 | |
| | 1303 | ret = decode_usac_scale_factors(ac, sce, gb, global_gain); |
| | 1304 | if (ret < 0) |
| | 1305 | return ret; |
| | 1306 | |
| | 1307 | ac->dsp.dequant_scalefactors(sce); |
| | 1308 | |
| | 1309 | if (sce->tns.present) { |
| | 1310 | ret = ff_aac_decode_tns(ac, &sce->tns, gb, ics); |
| | 1311 | if (ret < 0) |
| | 1312 | return ret; |
| | 1313 | } |
| | 1314 | |
| | 1315 | /* ac_spectral_data */ |
| | 1316 | arith_reset_flag = indep_flag; |
| | 1317 | if (!arith_reset_flag) |
| | 1318 | arith_reset_flag = get_bits1(gb); |
| | 1319 | |
| | 1320 | /* Decode coeffs */ |
| | 1321 | memset(&sce->coeffs[0], 0, 1024*sizeof(float)); |
| | 1322 | for (int win = 0; win < ics->num_windows; win++) { |
| | 1323 | int lg = ics->swb_offset[ics->max_sfb]; |
| | 1324 | int N; |
| | 1325 | if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) |
| | 1326 | N = usac->core_frame_len / 8; |
| | 1327 | else |
| | 1328 | N = usac->core_frame_len; |
| | 1329 | |
| | 1330 | ret = decode_spectrum_and_dequant_ac(ac, sce->coeffs + win*128, gb, |
| | 1331 | sce->sf, &ue->ac, |
| | 1332 | arith_reset_flag && (win == 0), |
| | 1333 | lg, N); |
| | 1334 | if (ret < 0) |
| | 1335 | return ret; |
| | 1336 | } |
| | 1337 | |
| | 1338 | if (get_bits1(gb)) { /* fac_data_present */ |
| | 1339 | const uint16_t len_8 = usac->core_frame_len / 8; |
| | 1340 | const uint16_t len_16 = usac->core_frame_len / 16; |
| | 1341 | const uint16_t fac_len = ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE ? len_8 : len_16; |
| | 1342 | ret = ff_aac_parse_fac_data(ue, gb, 1, fac_len); |
| | 1343 | if (ret < 0) |
| | 1344 | return ret; |
| | 1345 | } |
| | 1346 | } |
| | 1347 | |
| | 1348 | spectrum_decode(ac, usac, che, nb_channels); |
| | 1349 | |
| | 1350 | return 0; |
| | 1351 | } |
| | 1352 | |
| | 1353 | static int parse_audio_preroll(AACDecContext *ac, GetBitContext *gb) |
| | 1354 | { |
| | 1355 | int ret = 0; |
| | 1356 | GetBitContext gbc; |
| | 1357 | OutputConfiguration *oc = &ac->oc[1]; |
| | 1358 | MPEG4AudioConfig *m4ac = &oc->m4ac; |
| | 1359 | MPEG4AudioConfig m4ac_bak = oc->m4ac; |
| | 1360 | uint8_t temp_data[512]; |
| | 1361 | uint8_t *tmp_buf = temp_data; |
| | 1362 | size_t tmp_buf_size = sizeof(temp_data); |
| | 1363 | |
| | 1364 | av_unused int crossfade; |
| | 1365 | int num_preroll_frames; |
| | 1366 | |
| | 1367 | int config_len = get_escaped_value(gb, 4, 4, 8); |
| | 1368 | |
| | 1369 | /* Implementations are free to pad the config to any length, so use a |
| | 1370 | * different reader for this. */ |
| | 1371 | gbc = *gb; |
| | 1372 | ret = ff_aac_usac_config_decode(ac, ac->avctx, &gbc, oc, m4ac->chan_config); |
| | 1373 | if (ret < 0) { |
| | 1374 | *m4ac = m4ac_bak; |
| | 1375 | return ret; |
| | 1376 | } else { |
| | 1377 | ac->oc[1].m4ac.chan_config = 0; |
| | 1378 | } |
| | 1379 | |
| | 1380 | /* 7.18.3.3 Bitrate adaption |
| | 1381 | * If configuration didn't change after applying preroll, continue |
| | 1382 | * without decoding it. */ |
| | 1383 | if (!memcmp(m4ac, &m4ac_bak, sizeof(m4ac_bak))) |
| | 1384 | return 0; |
| | 1385 | |
| | 1386 | skip_bits_long(gb, config_len*8); |
| | 1387 | |
| | 1388 | crossfade = get_bits1(gb); /* applyCrossfade */ |
| | 1389 | skip_bits1(gb); /* reserved */ |
| | 1390 | num_preroll_frames = get_escaped_value(gb, 2, 4, 0); /* numPreRollFrames */ |
| | 1391 | |
| | 1392 | for (int i = 0; i < num_preroll_frames; i++) { |
| | 1393 | int got_frame_ptr = 0; |
| | 1394 | int au_len = get_escaped_value(gb, 16, 16, 0); |
| | 1395 | |
| | 1396 | if (au_len*8 > tmp_buf_size) { |
| | 1397 | uint8_t *tmp2; |
| | 1398 | tmp_buf = tmp_buf == temp_data ? NULL : tmp_buf; |
| | 1399 | tmp2 = realloc(tmp_buf, au_len*8); |
| | 1400 | if (!tmp2) { |
| | 1401 | if (tmp_buf != temp_data) |
| | 1402 | av_free(tmp_buf); |
| | 1403 | return AVERROR(ENOMEM); |
| | 1404 | } |
| | 1405 | tmp_buf = tmp2; |
| | 1406 | } |
| | 1407 | |
| | 1408 | /* Byte alignment is not guaranteed. */ |
| | 1409 | for (int i = 0; i < au_len; i++) |
| | 1410 | tmp_buf[i] = get_bits(gb, 8); |
| | 1411 | |
| | 1412 | ret = init_get_bits8(&gbc, tmp_buf, au_len); |
| | 1413 | if (ret < 0) |
| | 1414 | break; |
| | 1415 | |
| | 1416 | ret = ff_aac_usac_decode_frame(ac->avctx, ac, &gbc, &got_frame_ptr); |
| | 1417 | if (ret < 0) |
| | 1418 | break; |
| | 1419 | } |
| | 1420 | |
| | 1421 | if (tmp_buf != temp_data) |
| | 1422 | av_free(tmp_buf); |
| | 1423 | |
| | 1424 | return 0; |
| | 1425 | } |
| | 1426 | |
| | 1427 | static int parse_ext_ele(AACDecContext *ac, AACUsacElemConfig *e, |
| | 1428 | GetBitContext *gb) |
| | 1429 | { |
| | 1430 | uint8_t *tmp; |
| | 1431 | uint8_t pl_frag_start = 1; |
| | 1432 | uint8_t pl_frag_end = 1; |
| | 1433 | uint32_t len; |
| | 1434 | |
| | 1435 | if (!get_bits1(gb)) /* usacExtElementPresent */ |
| | 1436 | return 0; |
| | 1437 | |
| | 1438 | if (get_bits1(gb)) { /* usacExtElementUseDefaultLength */ |
| | 1439 | len = e->ext.default_len; |
| | 1440 | } else { |
| | 1441 | len = get_bits(gb, 8); /* usacExtElementPayloadLength */ |
| | 1442 | if (len == 255) |
| | 1443 | len += get_bits(gb, 16) - 2; |
| | 1444 | } |
| | 1445 | |
| | 1446 | if (!len) |
| | 1447 | return 0; |
| | 1448 | |
| | 1449 | if (e->ext.payload_frag) { |
| | 1450 | pl_frag_start = get_bits1(gb); /* usacExtElementStart */ |
| | 1451 | pl_frag_end = get_bits1(gb); /* usacExtElementStop */ |
| | 1452 | } |
| | 1453 | |
| | 1454 | if (pl_frag_start) |
| | 1455 | e->ext.pl_data_offset = 0; |
| | 1456 | |
| | 1457 | /* If an extension starts and ends this packet, we can directly use it */ |
| | 1458 | if (!(pl_frag_start && pl_frag_end)) { |
| | 1459 | tmp = av_realloc(e->ext.pl_data, e->ext.pl_data_offset + len); |
| | 1460 | if (!tmp) { |
| | 1461 | av_free(e->ext.pl_data); |
| | 1462 | return AVERROR(ENOMEM); |
| | 1463 | } |
| | 1464 | e->ext.pl_data = tmp; |
| | 1465 | |
| | 1466 | /* Readout data to a buffer */ |
| | 1467 | for (int i = 0; i < len; i++) |
| | 1468 | e->ext.pl_data[e->ext.pl_data_offset + i] = get_bits(gb, 8); |
| | 1469 | } |
| | 1470 | |
| | 1471 | e->ext.pl_data_offset += len; |
| | 1472 | |
| | 1473 | if (pl_frag_end) { |
| | 1474 | int ret = 0; |
| | 1475 | int start_bits = get_bits_count(gb); |
| | 1476 | const int pl_len = e->ext.pl_data_offset; |
| | 1477 | GetBitContext *gb2 = gb; |
| | 1478 | GetBitContext gbc; |
| | 1479 | if (!(pl_frag_start && pl_frag_end)) { |
| | 1480 | ret = init_get_bits8(&gbc, e->ext.pl_data, pl_len); |
| | 1481 | if (ret < 0) |
| | 1482 | return ret; |
| | 1483 | |
| | 1484 | gb2 = &gbc; |
| | 1485 | } |
| | 1486 | |
| | 1487 | switch (e->ext.type) { |
| | 1488 | case ID_EXT_ELE_FILL: |
| | 1489 | /* Filler elements have no usable payload */ |
| | 1490 | break; |
| | 1491 | case ID_EXT_ELE_AUDIOPREROLL: |
| | 1492 | ret = parse_audio_preroll(ac, gb2); |
| | 1493 | break; |
| | 1494 | default: |
| | 1495 | /* This should never happen */ |
| | 1496 | av_assert0(0); |
| | 1497 | } |
| | 1498 | av_freep(&e->ext.pl_data); |
| | 1499 | if (ret < 0) |
| | 1500 | return ret; |
| | 1501 | |
| | 1502 | skip_bits_long(gb, pl_len*8 - (get_bits_count(gb) - start_bits)); |
| | 1503 | } |
| | 1504 | |
| | 1505 | return 0; |
| | 1506 | } |
| | 1507 | |
| | 1508 | int ff_aac_usac_decode_frame(AVCodecContext *avctx, AACDecContext *ac, |
| | 1509 | GetBitContext *gb, int *got_frame_ptr) |
| | 1510 | { |
| | 1511 | int ret, nb_ch_el, is_dmono = 0; |
| | 1512 | int indep_flag, samples = 0; |
| | 1513 | int audio_found = 0, sce_count = 0; |
| | 1514 | AVFrame *frame = ac->frame; |
| | 1515 | |
| | 1516 | ff_aac_output_configure(ac, ac->oc[1].layout_map, ac->oc[1].layout_map_tags, |
| | 1517 | ac->oc[1].status, 0); |
| | 1518 | |
| | 1519 | indep_flag = get_bits1(gb); |
| | 1520 | |
| | 1521 | nb_ch_el = 0; |
| | 1522 | for (int i = 0; i < ac->oc[1].usac.nb_elems; i++) { |
| | 1523 | AACUsacElemConfig *e = &ac->oc[1].usac.elems[i]; |
| | 1524 | ChannelElement *che; |
| | 1525 | |
| | 1526 | switch (e->type) { |
| | 1527 | case ID_USAC_LFE: |
| | 1528 | /* Fallthrough */ |
| | 1529 | case ID_USAC_SCE: |
| | 1530 | che = ff_aac_get_che(ac, TYPE_SCE, nb_ch_el++); |
| | 1531 | if (!che) { |
| | 1532 | av_log(ac->avctx, AV_LOG_ERROR, |
| | 1533 | "channel element %d.%d is not allocated\n", |
| | 1534 | TYPE_SCE, nb_ch_el - 1); |
| | 1535 | return AVERROR_INVALIDDATA; |
| | 1536 | } |
| | 1537 | |
| | 1538 | ret = decode_usac_core_coder(ac, &ac->oc[1].usac, e, che, gb, |
| | 1539 | indep_flag, 1); |
| | 1540 | if (ret < 0) |
| | 1541 | return ret; |
| | 1542 | |
| | 1543 | sce_count++; |
| | 1544 | audio_found = 1; |
| | 1545 | che->present = 1; |
| | 1546 | samples = ac->oc[1].m4ac.frame_length_short ? 768 : 1024; |
| | 1547 | break; |
| | 1548 | case ID_USAC_CPE: |
| | 1549 | che = ff_aac_get_che(ac, TYPE_CPE, nb_ch_el++); |
| | 1550 | if (!che) { |
| | 1551 | av_log(ac->avctx, AV_LOG_ERROR, |
| | 1552 | "channel element %d.%d is not allocated\n", |
| | 1553 | TYPE_SCE, nb_ch_el - 1); |
| | 1554 | return AVERROR_INVALIDDATA; |
| | 1555 | } |
| | 1556 | |
| | 1557 | ret = decode_usac_core_coder(ac, &ac->oc[1].usac, e, che, gb, |
| | 1558 | indep_flag, 2); |
| | 1559 | if (ret < 0) |
| | 1560 | return ret; |
| | 1561 | |
| | 1562 | audio_found = 1; |
| | 1563 | che->present = 1; |
| | 1564 | samples = ac->oc[1].m4ac.frame_length_short ? 768 : 1024; |
| | 1565 | break; |
| | 1566 | case ID_USAC_EXT: |
| | 1567 | ret = parse_ext_ele(ac, e, gb); |
| | 1568 | if (ret < 0) |
| | 1569 | return ret; |
| | 1570 | break; |
| | 1571 | } |
| | 1572 | } |
| | 1573 | |
| | 1574 | if (ac->oc[1].status && audio_found) { |
| | 1575 | avctx->sample_rate = ac->oc[1].m4ac.sample_rate; |
| | 1576 | avctx->frame_size = samples; |
| | 1577 | ac->oc[1].status = OC_LOCKED; |
| | 1578 | } |
| | 1579 | |
| | 1580 | if (!frame->data[0] && samples) { |
| | 1581 | av_log(avctx, AV_LOG_ERROR, "no frame data found\n"); |
| | 1582 | return AVERROR_INVALIDDATA; |
| | 1583 | } |
| | 1584 | |
| | 1585 | if (samples) { |
| | 1586 | frame->nb_samples = samples; |
| | 1587 | frame->sample_rate = avctx->sample_rate; |
| | 1588 | frame->flags = indep_flag ? AV_FRAME_FLAG_KEY : 0x0; |
| | 1589 | *got_frame_ptr = 1; |
| | 1590 | } else { |
| | 1591 | av_frame_unref(ac->frame); |
| | 1592 | frame->flags = indep_flag ? AV_FRAME_FLAG_KEY : 0x0; |
| | 1593 | *got_frame_ptr = 0; |
| | 1594 | } |
| | 1595 | |
| | 1596 | /* for dual-mono audio (SCE + SCE) */ |
| | 1597 | is_dmono = ac->dmono_mode && sce_count == 2 && |
| | 1598 | !av_channel_layout_compare(&ac->oc[1].ch_layout, |
| | 1599 | &(AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO); |
| | 1600 | if (is_dmono) { |
| | 1601 | if (ac->dmono_mode == 1) |
| | 1602 | frame->data[1] = frame->data[0]; |
| | 1603 | else if (ac->dmono_mode == 2) |
| | 1604 | frame->data[0] = frame->data[1]; |
| | 1605 | } |
| | 1606 | |
| | 1607 | return 0; |
| | 1608 | } |