Mercurial > libavcodec.hg
comparison aac.c @ 7523:a3f7ffdb676d libavcodec
Sync already committed code with that in SoC and commit more OKed hunks of code
| author | superdump |
|---|---|
| date | Sat, 09 Aug 2008 10:46:27 +0000 |
| parents | d6012be599d3 |
| children | 4fca7939ad48 |
comparison
equal
deleted
inserted
replaced
| 7522:4017c5ba7bab | 7523:a3f7ffdb676d |
|---|---|
| 80 #include "bitstream.h" | 80 #include "bitstream.h" |
| 81 #include "dsputil.h" | 81 #include "dsputil.h" |
| 82 | 82 |
| 83 #include "aac.h" | 83 #include "aac.h" |
| 84 #include "aactab.h" | 84 #include "aactab.h" |
| 85 #include "aacdectab.h" | |
| 85 #include "mpeg4audio.h" | 86 #include "mpeg4audio.h" |
| 86 | 87 |
| 87 #include <assert.h> | 88 #include <assert.h> |
| 88 #include <errno.h> | 89 #include <errno.h> |
| 89 #include <math.h> | 90 #include <math.h> |
| 90 #include <string.h> | 91 #include <string.h> |
| 91 | 92 |
| 92 #ifndef CONFIG_HARDCODED_TABLES | 93 #ifndef CONFIG_HARDCODED_TABLES |
| 93 static float ff_aac_ivquant_tab[IVQUANT_SIZE]; | 94 static float ff_aac_ivquant_tab[IVQUANT_SIZE]; |
| 95 static float ff_aac_pow2sf_tab[316]; | |
| 94 #endif /* CONFIG_HARDCODED_TABLES */ | 96 #endif /* CONFIG_HARDCODED_TABLES */ |
| 95 | 97 |
| 96 static VLC vlc_scalefactors; | 98 static VLC vlc_scalefactors; |
| 97 static VLC vlc_spectral[11]; | 99 static VLC vlc_spectral[11]; |
| 98 | 100 |
| 102 num_back = get_bits(gb, 4); | 104 num_back = get_bits(gb, 4); |
| 103 num_lfe = get_bits(gb, 2); | 105 num_lfe = get_bits(gb, 2); |
| 104 num_assoc_data = get_bits(gb, 3); | 106 num_assoc_data = get_bits(gb, 3); |
| 105 num_cc = get_bits(gb, 4); | 107 num_cc = get_bits(gb, 4); |
| 106 | 108 |
| 107 newpcs->mono_mixdown_tag = get_bits1(gb) ? get_bits(gb, 4) : -1; | 109 if (get_bits1(gb)) |
| 108 newpcs->stereo_mixdown_tag = get_bits1(gb) ? get_bits(gb, 4) : -1; | 110 skip_bits(gb, 4); // mono_mixdown_tag |
| 109 | 111 if (get_bits1(gb)) |
| 110 if (get_bits1(gb)) { | 112 skip_bits(gb, 4); // stereo_mixdown_tag |
| 111 newpcs->mixdown_coeff_index = get_bits(gb, 2); | 113 |
| 112 newpcs->pseudo_surround = get_bits1(gb); | 114 if (get_bits1(gb)) |
| 113 } | 115 skip_bits(gb, 3); // mixdown_coeff_index and pseudo_surround |
| 114 | 116 |
| 115 program_config_element_parse_tags(newpcs->che_type[ID_CPE], newpcs->che_type[ID_SCE], AAC_CHANNEL_FRONT, gb, num_front); | 117 decode_channel_map(new_che_pos[TYPE_CPE], new_che_pos[TYPE_SCE], AAC_CHANNEL_FRONT, gb, num_front); |
| 116 program_config_element_parse_tags(newpcs->che_type[ID_CPE], newpcs->che_type[ID_SCE], AAC_CHANNEL_SIDE, gb, num_side ); | 118 decode_channel_map(new_che_pos[TYPE_CPE], new_che_pos[TYPE_SCE], AAC_CHANNEL_SIDE, gb, num_side ); |
| 117 program_config_element_parse_tags(newpcs->che_type[ID_CPE], newpcs->che_type[ID_SCE], AAC_CHANNEL_BACK, gb, num_back ); | 119 decode_channel_map(new_che_pos[TYPE_CPE], new_che_pos[TYPE_SCE], AAC_CHANNEL_BACK, gb, num_back ); |
| 118 program_config_element_parse_tags(NULL, newpcs->che_type[ID_LFE], AAC_CHANNEL_LFE, gb, num_lfe ); | 120 decode_channel_map(NULL, new_che_pos[TYPE_LFE], AAC_CHANNEL_LFE, gb, num_lfe ); |
| 119 | 121 |
| 120 skip_bits_long(gb, 4 * num_assoc_data); | 122 skip_bits_long(gb, 4 * num_assoc_data); |
| 121 | 123 |
| 122 program_config_element_parse_tags(newpcs->che_type[ID_CCE], newpcs->che_type[ID_CCE], AAC_CHANNEL_CC, gb, num_cc ); | 124 decode_channel_map(new_che_pos[TYPE_CCE], new_che_pos[TYPE_CCE], AAC_CHANNEL_CC, gb, num_cc ); |
| 123 | 125 |
| 124 align_get_bits(gb); | 126 align_get_bits(gb); |
| 125 | 127 |
| 126 /* comment field, first byte is length */ | 128 /* comment field, first byte is length */ |
| 127 skip_bits_long(gb, 8 * get_bits(gb, 8)); | 129 skip_bits_long(gb, 8 * get_bits(gb, 8)); |
| 130 return 0; | |
| 131 } | |
| 128 | 132 |
| 129 static av_cold int aac_decode_init(AVCodecContext * avccontext) { | 133 static av_cold int aac_decode_init(AVCodecContext * avccontext) { |
| 130 AACContext * ac = avccontext->priv_data; | 134 AACContext * ac = avccontext->priv_data; |
| 131 int i; | 135 int i; |
| 132 | 136 |
| 133 ac->avccontext = avccontext; | 137 ac->avccontext = avccontext; |
| 138 | |
| 139 if (avccontext->extradata_size <= 0 || | |
| 140 decode_audio_specific_config(ac, avccontext->extradata, avccontext->extradata_size)) | |
| 141 return -1; | |
| 134 | 142 |
| 135 avccontext->sample_rate = ac->m4ac.sample_rate; | 143 avccontext->sample_rate = ac->m4ac.sample_rate; |
| 136 avccontext->frame_size = 1024; | 144 avccontext->frame_size = 1024; |
| 137 | 145 |
| 138 AAC_INIT_VLC_STATIC( 0, 144); | 146 AAC_INIT_VLC_STATIC( 0, 144); |
| 164 } | 172 } |
| 165 | 173 |
| 166 #ifndef CONFIG_HARDCODED_TABLES | 174 #ifndef CONFIG_HARDCODED_TABLES |
| 167 for (i = 1 - IVQUANT_SIZE/2; i < IVQUANT_SIZE/2; i++) | 175 for (i = 1 - IVQUANT_SIZE/2; i < IVQUANT_SIZE/2; i++) |
| 168 ff_aac_ivquant_tab[i + IVQUANT_SIZE/2 - 1] = cbrt(fabs(i)) * i; | 176 ff_aac_ivquant_tab[i + IVQUANT_SIZE/2 - 1] = cbrt(fabs(i)) * i; |
| 177 for (i = 0; i < 316; i++) | |
| 178 ff_aac_pow2sf_tab[i] = pow(2, (i - 200)/4.); | |
| 169 #endif /* CONFIG_HARDCODED_TABLES */ | 179 #endif /* CONFIG_HARDCODED_TABLES */ |
| 170 | 180 |
| 171 INIT_VLC_STATIC(&vlc_scalefactors, 7, sizeof(ff_aac_scalefactor_code)/sizeof(ff_aac_scalefactor_code[0]), | 181 INIT_VLC_STATIC(&vlc_scalefactors, 7, sizeof(ff_aac_scalefactor_code)/sizeof(ff_aac_scalefactor_code[0]), |
| 172 ff_aac_scalefactor_bits, sizeof(ff_aac_scalefactor_bits[0]), sizeof(ff_aac_scalefactor_bits[0]), | 182 ff_aac_scalefactor_bits, sizeof(ff_aac_scalefactor_bits[0]), sizeof(ff_aac_scalefactor_bits[0]), |
| 173 ff_aac_scalefactor_code, sizeof(ff_aac_scalefactor_code[0]), sizeof(ff_aac_scalefactor_code[0]), | 183 ff_aac_scalefactor_code, sizeof(ff_aac_scalefactor_code[0]), sizeof(ff_aac_scalefactor_code[0]), |
| 198 return ff_aac_ivquant_tab[a + IVQUANT_SIZE/2 - 1]; | 208 return ff_aac_ivquant_tab[a + IVQUANT_SIZE/2 - 1]; |
| 199 else | 209 else |
| 200 return cbrtf(fabsf(a)) * a; | 210 return cbrtf(fabsf(a)) * a; |
| 201 } | 211 } |
| 202 | 212 |
| 213 int band_type_run_end[120], GetBitContext * gb, IndividualChannelStream * ics) { | |
| 214 int g, idx = 0; | |
| 215 const int bits = (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) ? 3 : 5; | |
| 216 for (g = 0; g < ics->num_window_groups; g++) { | |
| 217 int k = 0; | |
| 218 while (k < ics->max_sfb) { | |
| 219 uint8_t sect_len = k; | |
| 220 int sect_len_incr; | |
| 221 int sect_band_type = get_bits(gb, 4); | |
| 222 if (sect_band_type == 12) { | |
| 223 av_log(ac->avccontext, AV_LOG_ERROR, "invalid band type\n"); | |
| 224 return -1; | |
| 225 } | |
| 226 while ((sect_len_incr = get_bits(gb, bits)) == (1 << bits)-1) | |
| 227 sect_len += sect_len_incr; | |
| 228 sect_len += sect_len_incr; | |
| 229 if (sect_len > ics->max_sfb) { | |
| 230 av_log(ac->avccontext, AV_LOG_ERROR, | |
| 231 "Number of bands (%d) exceeds limit (%d).\n", | |
| 232 sect_len, ics->max_sfb); | |
| 233 return -1; | |
| 234 } | |
| 235 | |
| 236 * | |
| 237 * @param mix_gain channel gain (Not used by AAC bitstream.) | |
| 238 * @param global_gain first scalefactor value as scalefactors are differentially coded | |
| 239 * @param band_type array of the used band type | |
| 240 * @param band_type_run_end array of the last scalefactor band of a band type run | |
| 241 * @param sf array of scalefactors or intensity stereo positions | |
| 242 * | |
| 243 * @return Returns error status. 0 - OK, !0 - error | |
| 244 */ | |
| 245 static int decode_scalefactors(AACContext * ac, float sf[120], GetBitContext * gb, | |
| 246 float mix_gain, unsigned int global_gain, IndividualChannelStream * ics, | |
| 247 enum BandType band_type[120], int band_type_run_end[120]) { | |
| 248 const int sf_offset = ac->sf_offset + (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE ? 12 : 0); | |
| 249 int g, i, idx = 0; | |
| 250 int offset[3] = { global_gain, global_gain - 90, 100 }; | |
| 251 int noise_flag = 1; | |
| 252 static const char *sf_str[3] = { "Global gain", "Noise gain", "Intensity stereo position" }; | |
| 253 ics->intensity_present = 0; | |
| 254 for (g = 0; g < ics->num_window_groups; g++) { | |
| 255 for (i = 0; i < ics->max_sfb;) { | |
| 256 int run_end = band_type_run_end[idx]; | |
| 257 if (band_type[idx] == ZERO_BT) { | |
| 258 for(; i < run_end; i++, idx++) | |
| 259 sf[idx] = 0.; | |
| 260 }else if((band_type[idx] == INTENSITY_BT) || (band_type[idx] == INTENSITY_BT2)) { | |
| 261 ics->intensity_present = 1; | |
| 262 for(; i < run_end; i++, idx++) { | |
| 263 offset[2] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60; | |
| 264 if(offset[2] > 255U) { | |
| 265 av_log(ac->avccontext, AV_LOG_ERROR, | |
| 266 "%s (%d) out of range.\n", sf_str[2], offset[2]); | |
| 267 return -1; | |
| 268 } | |
| 269 sf[idx] = ff_aac_pow2sf_tab[-offset[2] + 300]; | |
| 270 sf[idx] *= mix_gain; | |
| 271 } | |
| 272 }else if(band_type[idx] == NOISE_BT) { | |
| 273 for(; i < run_end; i++, idx++) { | |
| 274 if(noise_flag-- > 0) | |
| 275 offset[1] += get_bits(gb, 9) - 256; | |
| 276 else | |
| 277 offset[1] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60; | |
| 278 if(offset[1] > 255U) { | |
| 279 av_log(ac->avccontext, AV_LOG_ERROR, | |
| 280 "%s (%d) out of range.\n", sf_str[1], offset[1]); | |
| 281 return -1; | |
| 282 } | |
| 283 sf[idx] = -ff_aac_pow2sf_tab[ offset[1] + sf_offset]; | |
| 284 sf[idx] *= mix_gain; | |
| 285 } | |
| 286 }else { | |
| 287 for(; i < run_end; i++, idx++) { | |
| 288 offset[0] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60; | |
| 289 if(offset[0] > 255U) { | |
| 290 av_log(ac->avccontext, AV_LOG_ERROR, | |
| 291 "%s (%d) out of range.\n", sf_str[0], offset[0]); | |
| 292 return -1; | |
| 293 } | |
| 294 sf[idx] = -ff_aac_pow2sf_tab[ offset[0] + sf_offset]; | |
| 295 sf[idx] *= mix_gain; | |
| 296 } | |
| 297 } | |
| 298 } | |
| 299 } | |
| 300 return 0; | |
| 301 } | |
| 302 | |
| 303 /** | |
| 304 * Decode pulse data; reference: table 4.7. | |
| 305 */ | |
| 306 static void decode_pulses(Pulse * pulse, GetBitContext * gb) { | |
| 307 int i; | |
| 308 pulse->num_pulse = get_bits(gb, 2) + 1; | |
| 309 pulse->start = get_bits(gb, 6); | |
| 310 for (i = 0; i < pulse->num_pulse; i++) { | |
| 311 pulse->offset[i] = get_bits(gb, 5); | |
| 312 pulse->amp [i] = get_bits(gb, 4); | |
| 313 } | |
| 314 } | |
| 315 | |
| 316 /** | |
| 317 * Add pulses with particular amplitudes to the quantized spectral data; reference: 4.6.3.3. | |
| 318 * | |
| 203 * @param pulse pointer to pulse data struct | 319 * @param pulse pointer to pulse data struct |
| 204 * @param icoef array of quantized spectral data | 320 * @param icoef array of quantized spectral data |
| 205 */ | 321 */ |
| 206 static void add_pulses(int icoef[1024], const Pulse * pulse, const IndividualChannelStream * ics) { | 322 static void add_pulses(int icoef[1024], const Pulse * pulse, const IndividualChannelStream * ics) { |
| 207 int i, off = ics->swb_offset[pulse->start]; | 323 int i, off = ics->swb_offset[pulse->start]; |
| 211 ic = (icoef[off] - 1)>>31; | 327 ic = (icoef[off] - 1)>>31; |
| 212 icoef[off] += (pulse->amp[i]^ic) - ic; | 328 icoef[off] += (pulse->amp[i]^ic) - ic; |
| 213 } | 329 } |
| 214 } | 330 } |
| 215 | 331 |
| 332 /** | |
| 333 * Parse Spectral Band Replication extension data; reference: table 4.55. | |
| 334 * | |
| 335 * @param crc flag indicating the presence of CRC checksum | |
| 336 * @param cnt length of TYPE_FIL syntactic element in bytes | |
| 337 * @return Returns number of bytes consumed from the TYPE_FIL element. | |
| 338 */ | |
| 339 static int decode_sbr_extension(AACContext * ac, GetBitContext * gb, int crc, int cnt) { | |
| 340 // TODO : sbr_extension implementation | |
| 341 av_log(ac->avccontext, AV_LOG_DEBUG, "aac: SBR not yet supported.\n"); | |
| 342 skip_bits_long(gb, 8*cnt - 4); // -4 due to reading extension type | |
| 343 return cnt; | |
| 344 } | |
| 345 | |
| 346 int crc_flag = 0; | |
| 347 int res = cnt; | |
| 348 switch (get_bits(gb, 4)) { // extension type | |
| 349 case EXT_SBR_DATA_CRC: | |
| 350 crc_flag++; | |
| 351 case EXT_SBR_DATA: | |
| 352 res = decode_sbr_extension(ac, gb, crc_flag, cnt); | |
| 353 break; | |
| 354 case EXT_DYNAMIC_RANGE: | |
| 355 res = decode_dynamic_range(&ac->che_drc, gb, cnt); | |
| 356 break; | |
| 357 case EXT_FILL: | |
| 358 case EXT_FILL_DATA: | |
| 359 case EXT_DATA_ELEMENT: | |
| 360 default: | |
| 361 skip_bits_long(gb, 8*cnt - 4); | |
| 362 break; | |
| 363 }; | |
| 364 return res; | |
| 365 } | |
| 366 | |
| 367 /** | |
| 368 * Apply dependent channel coupling (applied before IMDCT). | |
| 369 * | |
| 370 * @param index index into coupling gain array | |
| 371 */ | |
| 372 static void apply_dependent_coupling(AACContext * ac, SingleChannelElement * sce, ChannelElement * cc, int index) { | |
| 373 IndividualChannelStream * ics = &cc->ch[0].ics; | |
| 374 const uint16_t * offsets = ics->swb_offset; | |
| 375 float * dest = sce->coeffs; | |
| 376 const float * src = cc->ch[0].coeffs; | |
| 377 int g, i, group, k, idx = 0; | |
| 378 if(ac->m4ac.object_type == AOT_AAC_LTP) { | |
| 379 av_log(ac->avccontext, AV_LOG_ERROR, | |
| 380 "Dependent coupling is not supported together with LTP\n"); | |
| 381 return; | |
| 382 } | |
| 383 for (g = 0; g < ics->num_window_groups; g++) { | |
| 384 for (i = 0; i < ics->max_sfb; i++, idx++) { | |
| 385 if (cc->ch[0].band_type[idx] != ZERO_BT) { | |
| 386 float gain = cc->coup.gain[index][idx] * sce->mixing_gain; | |
| 387 for (group = 0; group < ics->group_len[g]; group++) { | |
| 388 for (k = offsets[i]; k < offsets[i+1]; k++) { | |
| 389 // XXX dsputil-ize | |
| 390 dest[group*128+k] += gain * src[group*128+k]; | |
| 391 } | |
| 392 } | |
| 393 } | |
| 394 } | |
| 395 dest += ics->group_len[g]*128; | |
| 396 src += ics->group_len[g]*128; | |
| 397 } | |
| 398 } | |
| 399 | |
| 400 /** | |
| 401 * Apply independent channel coupling (applied after IMDCT). | |
| 402 * | |
| 403 * @param index index into coupling gain array | |
| 404 */ | |
| 405 static void apply_independent_coupling(AACContext * ac, SingleChannelElement * sce, ChannelElement * cc, int index) { | |
| 406 int i; | |
| 407 float gain = cc->coup.gain[index][0] * sce->mixing_gain; | |
| 408 for (i = 0; i < 1024; i++) | |
| 409 sce->ret[i] += gain * (cc->ch[0].ret[i] - ac->add_bias); | |
| 410 } | |
| 411 | |
| 216 static av_cold int aac_decode_close(AVCodecContext * avccontext) { | 412 static av_cold int aac_decode_close(AVCodecContext * avccontext) { |
| 217 AACContext * ac = avccontext->priv_data; | 413 AACContext * ac = avccontext->priv_data; |
| 218 int i, j; | 414 int i, j; |
| 219 | 415 |
| 220 for (i = 0; i < MAX_TAGID; i++) { | 416 for (i = 0; i < MAX_ELEM_ID; i++) { |
| 221 for(j = 0; j < 4; j++) | 417 for(j = 0; j < 4; j++) |
| 222 av_freep(&ac->che[j][i]); | 418 av_freep(&ac->che[j][i]); |
| 223 } | 419 } |
| 224 | 420 |
| 225 ff_mdct_end(&ac->mdct); | 421 ff_mdct_end(&ac->mdct); |
| 226 ff_mdct_end(&ac->mdct_small); | 422 ff_mdct_end(&ac->mdct_small); |
| 227 av_freep(&ac->interleaved_output); | |
| 228 return 0 ; | 423 return 0 ; |
| 229 } | 424 } |
| 230 | 425 |
| 231 AVCodec aac_decoder = { | 426 AVCodec aac_decoder = { |
| 232 "aac", | 427 "aac", |
| 236 aac_decode_init, | 431 aac_decode_init, |
| 237 NULL, | 432 NULL, |
| 238 aac_decode_close, | 433 aac_decode_close, |
| 239 aac_decode_frame, | 434 aac_decode_frame, |
| 240 .long_name = NULL_IF_CONFIG_SMALL("Advanced Audio Coding"), | 435 .long_name = NULL_IF_CONFIG_SMALL("Advanced Audio Coding"), |
| 436 .sample_fmts = (enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE}, | |
| 241 }; | 437 }; |
