static int8_t rvlc_huffman_esc(bitfile *ld, int8_t direction) { uint8_t i, j; uint32_t cw; rvlc_huff_table *h = book_escape; i = h->len; if (direction > 0) cw = faad_getbits(ld, i DEBUGVAR(1,0,"")); else cw = faad_getbits_rev(ld, i DEBUGVAR(1,0,"")); while ((cw != h->cw) && (i < 21)) { h++; j = h->len-i; i += j; cw <<= j; if (direction > 0) cw |= faad_getbits(ld, j DEBUGVAR(1,0,"")); else cw |= faad_getbits_rev(ld, j DEBUGVAR(1,0,"")); } return h->index; }
static INLINE int16_t huffman_getescape(bitfile *ld, int16_t sp) { uint8_t neg, i; int16_t j; int16_t off; if (sp < 0) { if (sp != -16) return sp; neg = 1; } else { if (sp != 16) return sp; neg = 0; } for (i = 4; ; i++) { if (faad_get1bit(ld DEBUGVAR(1,6,"huffman_getescape(): escape size")) == 0) { break; } } off = (int16_t)faad_getbits(ld, i DEBUGVAR(1,9,"huffman_getescape(): escape")); j = off | (1<<i); if (neg) j = -j; return j; }
uint8_t rvlc_decode_scale_factors(ic_stream *ics, bitfile *ld) { uint8_t result; uint8_t intensity_used = 0; uint8_t *rvlc_sf_buffer = NULL; uint8_t *rvlc_esc_buffer = NULL; bitfile ld_rvlc_sf, ld_rvlc_esc; // bitfile ld_rvlc_sf_rev, ld_rvlc_esc_rev; if (ics->length_of_rvlc_sf > 0) { /* We read length_of_rvlc_sf bits here to put it in a seperate bitfile. */ rvlc_sf_buffer = faad_getbitbuffer(ld, ics->length_of_rvlc_sf DEBUGVAR(1,156,"rvlc_decode_scale_factors(): bitbuffer: length_of_rvlc_sf")); faad_initbits(&ld_rvlc_sf, (void*)rvlc_sf_buffer, bit2byte(ics->length_of_rvlc_sf)); // faad_initbits_rev(&ld_rvlc_sf_rev, (void*)rvlc_sf_buffer, // ics->length_of_rvlc_sf); } if (ics->sf_escapes_present) { /* We read length_of_rvlc_escapes bits here to put it in a seperate bitfile. */ rvlc_esc_buffer = faad_getbitbuffer(ld, ics->length_of_rvlc_escapes DEBUGVAR(1,157,"rvlc_decode_scale_factors(): bitbuffer: length_of_rvlc_escapes")); faad_initbits(&ld_rvlc_esc, (void*)rvlc_esc_buffer, bit2byte(ics->length_of_rvlc_escapes)); // faad_initbits_rev(&ld_rvlc_esc_rev, (void*)rvlc_esc_buffer, // ics->length_of_rvlc_escapes); } /* decode the rvlc scale factors and escapes */ result = rvlc_decode_sf_forward(ics, &ld_rvlc_sf, &ld_rvlc_esc, &intensity_used); // result = rvlc_decode_sf_reverse(ics, &ld_rvlc_sf_rev, // &ld_rvlc_esc_rev, intensity_used); if (rvlc_esc_buffer) faad_free(rvlc_esc_buffer); if (rvlc_sf_buffer) faad_free(rvlc_sf_buffer); if (ics->length_of_rvlc_sf > 0) faad_endbits(&ld_rvlc_sf); if (ics->sf_escapes_present) faad_endbits(&ld_rvlc_esc); return result; }
static int8_t rvlc_huffman_sf(bitfile *ld_sf, bitfile *ld_esc, int8_t direction) { uint8_t i, j; int8_t index; uint32_t cw; rvlc_huff_table *h = book_rvlc; i = h->len; if (direction > 0) cw = faad_getbits(ld_sf, i DEBUGVAR(1,0,"")); else cw = faad_getbits_rev(ld_sf, i DEBUGVAR(1,0,"")); while ((cw != h->cw) && (i < 10)) { h++; j = h->len-i; i += j; cw <<= j; if (direction > 0) cw |= faad_getbits(ld_sf, j DEBUGVAR(1,0,"")); else cw |= faad_getbits_rev(ld_sf, j DEBUGVAR(1,0,"")); } index = h->index; if (index == +ESC_VAL) { int8_t esc = rvlc_huffman_esc(ld_esc, direction); if (esc == 99) return 99; index += esc; #ifdef PRINT_RVLC printf("esc: %d - ", esc); #endif } if (index == -ESC_VAL) { int8_t esc = rvlc_huffman_esc(ld_esc, direction); if (esc == 99) return 99; index -= esc; #ifdef PRINT_RVLC printf("esc: %d - ", esc); #endif } return index; }
static uint8_t huffman_binary_quad(uint8_t cb, bitfile *ld, int16_t *sp) { uint16_t offset = 0; while (!hcb3[offset].is_leaf) { uint8_t b = faad_get1bit(ld DEBUGVAR(1,255,"huffman_spectral_data():3")); offset += hcb3[offset].data[b]; } if (offset > hcb_bin_table_size[cb]) { /* printf("ERROR: offset into hcb_bin_table = %d >%d!\n", offset, hcb_bin_table_size[cb]); */ return 10; } sp[0] = hcb3[offset].data[0]; sp[1] = hcb3[offset].data[1]; sp[2] = hcb3[offset].data[2]; sp[3] = hcb3[offset].data[3]; return 0; }
static uint16_t sbr_extension(bitfile *ld, sbr_info *sbr, uint8_t bs_extension_id, uint16_t num_bits_left) { switch (bs_extension_id) { #ifdef PS_DEC case EXTENSION_ID_PS: sbr->ps_used = 1; if (!sbr->ps) { sbr->ps = ps_init(get_sr_index(sbr->sample_rate)); } return ps_data(sbr->ps, ld); #endif #ifdef DRM_PS case DRM_PARAMETRIC_STEREO: sbr->ps_used = 1; if (!sbr->drm_ps) { sbr->drm_ps = drm_ps_init(); } return drm_ps_data(sbr->drm_ps, ld); #endif default: sbr->bs_extension_data = (uint8_t)faad_getbits(ld, 6 DEBUGVAR(1,279,"sbr_single_channel_element(): bs_extension_data")); return 6; } }
/* table 8 */ static void sbr_dtdf(bitfile *ld, sbr_info *sbr, uint8_t ch) { uint8_t i; for (i = 0; i < sbr->L_E[ch]; i++) { sbr->bs_df_env[ch][i] = faad_get1bit(ld DEBUGVAR(1,269,"sbr_dtdf(): bs_df_env")); } for (i = 0; i < sbr->L_Q[ch]; i++) { sbr->bs_df_noise[ch][i] = faad_get1bit(ld DEBUGVAR(1,270,"sbr_dtdf(): bs_df_noise")); } }
/* table 12 */ static void sinusoidal_coding(bitfile *ld, sbr_info *sbr, uint8_t ch) { uint8_t n; for (n = 0; n < sbr->N_high; n++) { sbr->bs_add_harmonic[ch][n] = faad_get1bit(ld DEBUGVAR(1,278,"sinusoidal_coding(): bs_add_harmonic")); } }
/* table 11 */ void sbr_noise(bitfile *ld, sbr_info *sbr, uint8_t ch) { uint8_t noise, band; int8_t delta = 0; sbr_huff_tab t_huff, f_huff; if ((sbr->bs_coupling == 1) && (ch == 1)) { delta = 1; t_huff = t_huffman_noise_bal_3_0dB; f_huff = f_huffman_env_bal_3_0dB; } else { delta = 0; t_huff = t_huffman_noise_3_0dB; f_huff = f_huffman_env_3_0dB; } for (noise = 0; noise < sbr->L_Q[ch]; noise++) { if(sbr->bs_df_noise[ch][noise] == 0) { if ((sbr->bs_coupling == 1) && (ch == 1)) { sbr->Q[ch][0][noise] = (faad_getbits(ld, 5 DEBUGVAR(1,276,"sbr_noise(): bs_data_noise")) << delta); } else { sbr->Q[ch][0][noise] = (faad_getbits(ld, 5 DEBUGVAR(1,277,"sbr_noise(): bs_data_noise")) << delta); } for (band = 1; band < sbr->N_Q; band++) { sbr->Q[ch][band][noise] = (sbr_huff_dec(ld, f_huff) << delta); } } else { for (band = 0; band < sbr->N_Q; band++) { sbr->Q[ch][band][noise] = (sbr_huff_dec(ld, t_huff) << delta); } } } extract_noise_floor_data(sbr, ch); }
/* table 9 */ static void invf_mode(bitfile *ld, sbr_info *sbr, uint8_t ch) { uint8_t n; for (n = 0; n < sbr->N_Q; n++) { sbr->bs_invf_mode[ch][n] = (uint8_t)faad_getbits(ld, 2 DEBUGVAR(1,271,"invf_mode(): bs_invf_mode")); } }
static uint16_t ps_extension(ps_info *ps, bitfile *ld, const uint8_t ps_extension_id, const uint16_t num_bits_left) { uint8_t n; uint16_t bits = (uint16_t)faad_get_processed_bits(ld); if (ps_extension_id == 0) { ps->enable_ipdopd = (uint8_t)faad_get1bit(ld DEBUGVAR(1,1015,"ps_extension(): enable_ipdopd")); if (ps->enable_ipdopd) { for (n = 0; n < ps->num_env; n++) { ps->ipd_dt[n] = (uint8_t)faad_get1bit(ld DEBUGVAR(1,1016,"ps_extension(): ipd_dt")); /* ipd_data */ huff_data(ld, ps->ipd_dt[n], ps->nr_ipdopd_par, t_huff_ipd, f_huff_ipd, ps->ipd_index[n]); ps->opd_dt[n] = (uint8_t)faad_get1bit(ld DEBUGVAR(1,1017,"ps_extension(): opd_dt")); /* opd_data */ huff_data(ld, ps->opd_dt[n], ps->nr_ipdopd_par, t_huff_opd, f_huff_opd, ps->opd_index[n]); } } faad_get1bit(ld DEBUGVAR(1,1018,"ps_extension(): reserved_ps")); } /* return number of bits read */ bits = (uint16_t)faad_get_processed_bits(ld) - bits; return bits; }
static uint16_t sbr_extension(bitfile *ld, sbr_info *sbr, uint8_t bs_extension_id, uint16_t num_bits_left) { #ifdef PS_DEC uint8_t header; uint16_t ret; #endif switch (bs_extension_id) { #ifdef PS_DEC case EXTENSION_ID_PS: if (!sbr->ps) { sbr->ps = ps_init(get_sr_index(sbr->sample_rate)); } if (sbr->psResetFlag) { sbr->ps->header_read = 0; } ret = ps_data(sbr->ps, ld, &header); /* enable PS if and only if: a header has been decoded */ if (sbr->ps_used == 0 && header == 1) { sbr->ps_used = 1; } if (header == 1) { sbr->psResetFlag = 0; } return ret; #endif #ifdef DRM_PS case DRM_PARAMETRIC_STEREO: sbr->ps_used = 1; if (!sbr->drm_ps) { sbr->drm_ps = drm_ps_init(); } return drm_ps_data(sbr->drm_ps, ld); #endif default: sbr->bs_extension_data = (uint8_t)faad_getbits(ld, 6 DEBUGVAR(1,279,"sbr_single_channel_element(): bs_extension_data")); return 6; } }
uint8_t *faad_getbitbuffer(bitfile *ld, uint32_t bits DEBUGDEC) { uint16_t i; uint8_t temp; uint16_t bytes = (uint16_t)bits / 8; uint8_t remainder = (uint8_t)bits % 8; uint8_t *buffer = (uint8_t*)faad_malloc((bytes+1)*sizeof(uint8_t)); for (i = 0; i < bytes; i++) { buffer[i] = (uint8_t)faad_getbits(ld, 8 DEBUGVAR(print,var,dbg)); } if (remainder) { temp = (uint8_t)faad_getbits(ld, remainder DEBUGVAR(print,var,dbg)) << (8-remainder); buffer[bytes] = temp; } return buffer; }
uint8_t *faad_getbitbuffer(bitfile *ld, uint32_t bits DEBUGDEC) { int i; unsigned int temp; int bytes = bits >> 3; int remainder = bits & 0x7; uint8_t *buffer = (uint8_t*)faad_malloc((bytes+1)*sizeof(uint8_t)); for (i = 0; i < bytes; i++) { buffer[i] = (uint8_t)faad_getbits(ld, 8 DEBUGVAR(print,var,dbg)); } if (remainder) { temp = faad_getbits(ld, remainder DEBUGVAR(print,var,dbg)) << (8-remainder); buffer[bytes] = (uint8_t)temp; } return buffer; }
static INLINE void huffman_sign_bits(bitfile *ld, int16_t *sp, uint8_t len) { uint8_t i; for (i = 0; i < len; i++) { if(sp[i]) { if(faad_get1bit(ld DEBUGVAR(1,5,"huffman_sign_bits(): sign bit")) & 1) { sp[i] = -sp[i]; } } } }
int8_t huffman_scale_factor(bitfile *ld) { uint16_t offset = 0; while (hcb_sf[offset][1]) { uint8_t b = faad_get1bit(ld DEBUGVAR(1,255,"huffman_scale_factor()")); offset += hcb_sf[offset][b]; if (offset > 240) { /* printf("ERROR: offset into hcb_sf = %d >240!\n", offset); */ return -1; } } return hcb_sf[offset][0]; }
uint8_t rvlc_scale_factor_data(ic_stream *ics, bitfile *ld) { uint8_t bits = 9; ics->sf_concealment = faad_get1bit(ld DEBUGVAR(1,149,"rvlc_scale_factor_data(): sf_concealment")); ics->rev_global_gain = (uint8_t)faad_getbits(ld, 8 DEBUGVAR(1,150,"rvlc_scale_factor_data(): rev_global_gain")); if (ics->window_sequence == EIGHT_SHORT_SEQUENCE) bits = 11; /* the number of bits used for the huffman codewords */ ics->length_of_rvlc_sf = (uint16_t)faad_getbits(ld, bits DEBUGVAR(1,151,"rvlc_scale_factor_data(): length_of_rvlc_sf")); if (ics->noise_used) { ics->dpcm_noise_nrg = (uint16_t)faad_getbits(ld, 9 DEBUGVAR(1,152,"rvlc_scale_factor_data(): dpcm_noise_nrg")); ics->length_of_rvlc_sf -= 9; } ics->sf_escapes_present = faad_get1bit(ld DEBUGVAR(1,153,"rvlc_scale_factor_data(): sf_escapes_present")); if (ics->sf_escapes_present) { ics->length_of_rvlc_escapes = (uint8_t)faad_getbits(ld, 8 DEBUGVAR(1,154,"rvlc_scale_factor_data(): length_of_rvlc_escapes")); } if (ics->noise_used) { ics->dpcm_noise_last_position = (uint16_t)faad_getbits(ld, 9 DEBUGVAR(1,155,"rvlc_scale_factor_data(): dpcm_noise_last_position")); } return 0; }
/* table 4 */ static uint8_t sbr_data(bitfile *ld, sbr_info *sbr) { uint8_t result; #if 0 sbr->bs_samplerate_mode = faad_get1bit(ld DEBUGVAR(1,219,"sbr_data(): bs_samplerate_mode")); #endif sbr->rate = (sbr->bs_samplerate_mode) ? 2 : 1; switch (sbr->id_aac) { case ID_SCE: if ((result = sbr_single_channel_element(ld, sbr)) > 0) return result; break; case ID_CPE: if ((result = sbr_channel_pair_element(ld, sbr)) > 0) return result; break; } return 0; }
/* table 6 */ static uint8_t sbr_channel_pair_element(bitfile *ld, sbr_info *sbr) { uint8_t n, result; if (faad_get1bit(ld DEBUGVAR(1,228,"sbr_single_channel_element(): bs_data_extra"))) { faad_getbits(ld, 4 DEBUGVAR(1,228,"sbr_channel_pair_element(): bs_reserved_bits_data")); faad_getbits(ld, 4 DEBUGVAR(1,228,"sbr_channel_pair_element(): bs_reserved_bits_data")); } sbr->bs_coupling = faad_get1bit(ld DEBUGVAR(1,228,"sbr_channel_pair_element(): bs_coupling")); if (sbr->bs_coupling) { if ((result = sbr_grid(ld, sbr, 0)) > 0) return result; /* need to copy some data from left to right */ sbr->bs_frame_class[1] = sbr->bs_frame_class[0]; sbr->L_E[1] = sbr->L_E[0]; sbr->L_Q[1] = sbr->L_Q[0]; sbr->bs_pointer[1] = sbr->bs_pointer[0]; for (n = 0; n <= sbr->L_E[0]; n++) { sbr->t_E[1][n] = sbr->t_E[0][n]; sbr->f[1][n] = sbr->f[0][n]; } for (n = 0; n <= sbr->L_Q[0]; n++) sbr->t_Q[1][n] = sbr->t_Q[0][n]; sbr_dtdf(ld, sbr, 0); sbr_dtdf(ld, sbr, 1); invf_mode(ld, sbr, 0); /* more copying */ for (n = 0; n < sbr->N_Q; n++) sbr->bs_invf_mode[1][n] = sbr->bs_invf_mode[0][n]; sbr_envelope(ld, sbr, 0); sbr_noise(ld, sbr, 0); sbr_envelope(ld, sbr, 1); sbr_noise(ld, sbr, 1); memset(sbr->bs_add_harmonic[0], 0, 64*sizeof(uint8_t)); memset(sbr->bs_add_harmonic[1], 0, 64*sizeof(uint8_t)); sbr->bs_add_harmonic_flag[0] = faad_get1bit(ld DEBUGVAR(1,231,"sbr_channel_pair_element(): bs_add_harmonic_flag[0]")); if (sbr->bs_add_harmonic_flag[0]) sinusoidal_coding(ld, sbr, 0); sbr->bs_add_harmonic_flag[1] = faad_get1bit(ld DEBUGVAR(1,232,"sbr_channel_pair_element(): bs_add_harmonic_flag[1]")); if (sbr->bs_add_harmonic_flag[1]) sinusoidal_coding(ld, sbr, 1); } else { uint8_t saved_t_E[6] = {0}, saved_t_Q[3] = {0}; uint8_t saved_L_E = sbr->L_E[0]; uint8_t saved_L_Q = sbr->L_Q[0]; uint8_t saved_frame_class = sbr->bs_frame_class[0]; for (n = 0; n < saved_L_E; n++) saved_t_E[n] = sbr->t_E[0][n]; for (n = 0; n < saved_L_Q; n++) saved_t_Q[n] = sbr->t_Q[0][n]; if ((result = sbr_grid(ld, sbr, 0)) > 0) return result; if ((result = sbr_grid(ld, sbr, 1)) > 0) { /* restore first channel data as well */ sbr->bs_frame_class[0] = saved_frame_class; sbr->L_E[0] = saved_L_E; sbr->L_Q[0] = saved_L_Q; for (n = 0; n < 6; n++) sbr->t_E[0][n] = saved_t_E[n]; for (n = 0; n < 3; n++) sbr->t_Q[0][n] = saved_t_Q[n]; return result; } sbr_dtdf(ld, sbr, 0); sbr_dtdf(ld, sbr, 1); invf_mode(ld, sbr, 0); invf_mode(ld, sbr, 1); sbr_envelope(ld, sbr, 0); sbr_envelope(ld, sbr, 1); sbr_noise(ld, sbr, 0); sbr_noise(ld, sbr, 1); memset(sbr->bs_add_harmonic[0], 0, 64*sizeof(uint8_t)); memset(sbr->bs_add_harmonic[1], 0, 64*sizeof(uint8_t)); sbr->bs_add_harmonic_flag[0] = faad_get1bit(ld DEBUGVAR(1,239,"sbr_channel_pair_element(): bs_add_harmonic_flag[0]")); if (sbr->bs_add_harmonic_flag[0]) sinusoidal_coding(ld, sbr, 0); sbr->bs_add_harmonic_flag[1] = faad_get1bit(ld DEBUGVAR(1,240,"sbr_channel_pair_element(): bs_add_harmonic_flag[1]")); if (sbr->bs_add_harmonic_flag[1]) sinusoidal_coding(ld, sbr, 1); } #ifndef FIXED_POINT envelope_noise_dequantisation(sbr, 0); envelope_noise_dequantisation(sbr, 1); if (sbr->bs_coupling) unmap_envelope_noise(sbr); #endif sbr->bs_extended_data = faad_get1bit(ld DEBUGVAR(1,233,"sbr_channel_pair_element(): bs_extended_data[0]")); if (sbr->bs_extended_data) { uint16_t nr_bits_left; uint16_t cnt = (uint16_t)faad_getbits(ld, 4 DEBUGVAR(1,234,"sbr_channel_pair_element(): bs_extension_size")); if (cnt == 15) { cnt += (uint16_t)faad_getbits(ld, 8 DEBUGVAR(1,235,"sbr_channel_pair_element(): bs_esc_count")); } nr_bits_left = 8 * cnt; while (nr_bits_left > 7) { uint16_t tmp_nr_bits = 0; sbr->bs_extension_id = (uint8_t)faad_getbits(ld, 2 DEBUGVAR(1,236,"sbr_channel_pair_element(): bs_extension_id")); tmp_nr_bits += 2; tmp_nr_bits += sbr_extension(ld, sbr, sbr->bs_extension_id, nr_bits_left); /* check if the data read is bigger than the number of available bits */ if (tmp_nr_bits > nr_bits_left) return 1; nr_bits_left -= tmp_nr_bits; } /* Corrigendum */ if (nr_bits_left > 0) { faad_getbits(ld, nr_bits_left DEBUGVAR(1,280,"sbr_channel_pair_element(): nr_bits_left")); } } return 0; }
/* table 10 */ void sbr_envelope(bitfile *ld, sbr_info *sbr, uint8_t ch) { uint8_t env, band; int8_t delta = 0; sbr_huff_tab t_huff, f_huff; if ((sbr->L_E[ch] == 1) && (sbr->bs_frame_class[ch] == FIXFIX)) sbr->amp_res[ch] = 0; else sbr->amp_res[ch] = sbr->bs_amp_res; if ((sbr->bs_coupling) && (ch == 1)) { delta = 1; if (sbr->amp_res[ch]) { t_huff = t_huffman_env_bal_3_0dB; f_huff = f_huffman_env_bal_3_0dB; } else { t_huff = t_huffman_env_bal_1_5dB; f_huff = f_huffman_env_bal_1_5dB; } } else { delta = 0; if (sbr->amp_res[ch]) { t_huff = t_huffman_env_3_0dB; f_huff = f_huffman_env_3_0dB; } else { t_huff = t_huffman_env_1_5dB; f_huff = f_huffman_env_1_5dB; } } for (env = 0; env < sbr->L_E[ch]; env++) { if (sbr->bs_df_env[ch][env] == 0) { if ((sbr->bs_coupling == 1) && (ch == 1)) { if (sbr->amp_res[ch]) { sbr->E[ch][0][env] = (uint16_t)(faad_getbits(ld, 5 DEBUGVAR(1,272,"sbr_envelope(): bs_data_env")) << delta); } else { sbr->E[ch][0][env] = (uint16_t)(faad_getbits(ld, 6 DEBUGVAR(1,273,"sbr_envelope(): bs_data_env")) << delta); } } else { if (sbr->amp_res[ch]) { sbr->E[ch][0][env] = (uint16_t)(faad_getbits(ld, 6 DEBUGVAR(1,274,"sbr_envelope(): bs_data_env")) << delta); } else { sbr->E[ch][0][env] = (uint16_t)(faad_getbits(ld, 7 DEBUGVAR(1,275,"sbr_envelope(): bs_data_env")) << delta); } } for (band = 1; band < sbr->n[sbr->f[ch][env]]; band++) { sbr->E[ch][band][env] = (sbr_huff_dec(ld, f_huff) << delta); } } else { for (band = 0; band < sbr->n[sbr->f[ch][env]]; band++) { sbr->E[ch][band][env] = (sbr_huff_dec(ld, t_huff) << delta); } } } extract_envelope_data(sbr, ch); }
/* table 6 */ static uint8_t sbr_channel_pair_element(bitfile *ld, sbr_info *sbr) { uint8_t n, result; if (faad_get1bit(ld DEBUGVAR(1,228,"sbr_single_channel_element(): bs_data_extra"))) { faad_getbits(ld, 4 DEBUGVAR(1,228,"sbr_channel_pair_element(): bs_reserved_bits_data")); faad_getbits(ld, 4 DEBUGVAR(1,228,"sbr_channel_pair_element(): bs_reserved_bits_data")); } sbr->bs_coupling = faad_get1bit(ld DEBUGVAR(1,228,"sbr_channel_pair_element(): bs_coupling")); if (sbr->bs_coupling) { if ((result = sbr_grid(ld, sbr, 0)) > 0) return result; /* need to copy some data from left to right */ sbr->bs_frame_class[1] = sbr->bs_frame_class[0]; sbr->L_E[1] = sbr->L_E[0]; sbr->L_Q[1] = sbr->L_Q[0]; sbr->bs_pointer[1] = sbr->bs_pointer[0]; for (n = 0; n <= sbr->L_E[0]; n++) { sbr->t_E[1][n] = sbr->t_E[0][n]; sbr->f[1][n] = sbr->f[0][n]; } for (n = 0; n <= sbr->L_Q[0]; n++) sbr->t_Q[1][n] = sbr->t_Q[0][n]; sbr_dtdf(ld, sbr, 0); sbr_dtdf(ld, sbr, 1); invf_mode(ld, sbr, 0); /* more copying */ for (n = 0; n < sbr->N_Q; n++) sbr->bs_invf_mode[1][n] = sbr->bs_invf_mode[0][n]; sbr_envelope(ld, sbr, 0); sbr_noise(ld, sbr, 0); sbr_envelope(ld, sbr, 1); sbr_noise(ld, sbr, 1); memset(sbr->bs_add_harmonic[0], 0, 64*sizeof(uint8_t)); memset(sbr->bs_add_harmonic[1], 0, 64*sizeof(uint8_t)); sbr->bs_add_harmonic_flag[0] = faad_get1bit(ld DEBUGVAR(1,231,"sbr_channel_pair_element(): bs_add_harmonic_flag[0]")); if (sbr->bs_add_harmonic_flag[0]) sinusoidal_coding(ld, sbr, 0); sbr->bs_add_harmonic_flag[1] = faad_get1bit(ld DEBUGVAR(1,232,"sbr_channel_pair_element(): bs_add_harmonic_flag[1]")); if (sbr->bs_add_harmonic_flag[1]) sinusoidal_coding(ld, sbr, 1); } else { if ((result = sbr_grid(ld, sbr, 0)) > 0) return result; if ((result = sbr_grid(ld, sbr, 1)) > 0) return result; sbr_dtdf(ld, sbr, 0); sbr_dtdf(ld, sbr, 1); invf_mode(ld, sbr, 0); invf_mode(ld, sbr, 1); sbr_envelope(ld, sbr, 0); sbr_envelope(ld, sbr, 1); sbr_noise(ld, sbr, 0); sbr_noise(ld, sbr, 1); memset(sbr->bs_add_harmonic[0], 0, 64*sizeof(uint8_t)); memset(sbr->bs_add_harmonic[1], 0, 64*sizeof(uint8_t)); sbr->bs_add_harmonic_flag[0] = faad_get1bit(ld DEBUGVAR(1,239,"sbr_channel_pair_element(): bs_add_harmonic_flag[0]")); if (sbr->bs_add_harmonic_flag[0]) sinusoidal_coding(ld, sbr, 0); sbr->bs_add_harmonic_flag[1] = faad_get1bit(ld DEBUGVAR(1,240,"sbr_channel_pair_element(): bs_add_harmonic_flag[1]")); if (sbr->bs_add_harmonic_flag[1]) sinusoidal_coding(ld, sbr, 1); } envelope_noise_dequantisation(sbr, 0); envelope_noise_dequantisation(sbr, 1); if (sbr->bs_coupling) unmap_envelope_noise(sbr); sbr->bs_extended_data = faad_get1bit(ld DEBUGVAR(1,233,"sbr_channel_pair_element(): bs_extended_data[0]")); if (sbr->bs_extended_data) { uint16_t nr_bits_left; uint16_t cnt = (uint16_t)faad_getbits(ld, 4 DEBUGVAR(1,234,"sbr_channel_pair_element(): bs_extension_size")); if (cnt == 15) { cnt += (uint16_t)faad_getbits(ld, 8 DEBUGVAR(1,235,"sbr_channel_pair_element(): bs_esc_count")); } nr_bits_left = 8 * cnt; while (nr_bits_left > 7) { sbr->bs_extension_id = (uint8_t)faad_getbits(ld, 2 DEBUGVAR(1,236,"sbr_channel_pair_element(): bs_extension_id")); nr_bits_left -= 2; sbr_extension(ld, sbr, sbr->bs_extension_id, nr_bits_left); } /* Corrigendum */ if (nr_bits_left > 0) { faad_getbits(ld, nr_bits_left DEBUGVAR(1,280,"sbr_channel_pair_element(): nr_bits_left")); } } return 0; }
int8_t AudioSpecificConfig2(uint8_t *pBuffer, uint32_t buffer_size, mp4AudioSpecificConfig *mp4ASC, program_config *pce) { bitfile ld; int8_t result = 0; #ifdef SBR_DEC int8_t bits_to_decode = 0; #endif if (pBuffer == NULL) return -7; if (mp4ASC == NULL) return -8; memset(mp4ASC, 0, sizeof(mp4AudioSpecificConfig)); faad_initbits(&ld, pBuffer, buffer_size); faad_byte_align(&ld); mp4ASC->objectTypeIndex = (uint8_t)faad_getbits(&ld, 5 DEBUGVAR(1,1,"parse_audio_decoder_specific_info(): ObjectTypeIndex")); mp4ASC->samplingFrequencyIndex = (uint8_t)faad_getbits(&ld, 4 DEBUGVAR(1,2,"parse_audio_decoder_specific_info(): SamplingFrequencyIndex")); mp4ASC->channelsConfiguration = (uint8_t)faad_getbits(&ld, 4 DEBUGVAR(1,3,"parse_audio_decoder_specific_info(): ChannelsConfiguration")); mp4ASC->samplingFrequency = get_sample_rate(mp4ASC->samplingFrequencyIndex); if (ObjectTypesTable[mp4ASC->objectTypeIndex] != 1) { faad_endbits(&ld); return -1; } if (mp4ASC->samplingFrequency == 0) { faad_endbits(&ld); return -2; } if (mp4ASC->channelsConfiguration > 7) { faad_endbits(&ld); return -3; } #if (defined(PS_DEC) || defined(DRM_PS)) /* check if we have a mono file */ if (mp4ASC->channelsConfiguration == 1) { /* upMatrix to 2 channels for implicit signalling of PS */ mp4ASC->channelsConfiguration = 2; } #endif #ifdef SBR_DEC mp4ASC->sbr_present_flag = -1; if (mp4ASC->objectTypeIndex == 5) { uint8_t tmp; mp4ASC->sbr_present_flag = 1; tmp = (uint8_t)faad_getbits(&ld, 4 DEBUGVAR(1,5,"parse_audio_decoder_specific_info(): extensionSamplingFrequencyIndex")); /* check for downsampled SBR */ if (tmp == mp4ASC->samplingFrequencyIndex) mp4ASC->downSampledSBR = 1; mp4ASC->samplingFrequencyIndex = tmp; if (mp4ASC->samplingFrequencyIndex == 15) { mp4ASC->samplingFrequency = (uint32_t)faad_getbits(&ld, 24 DEBUGVAR(1,6,"parse_audio_decoder_specific_info(): extensionSamplingFrequencyIndex")); } else { mp4ASC->samplingFrequency = get_sample_rate(mp4ASC->samplingFrequencyIndex); } mp4ASC->objectTypeIndex = (uint8_t)faad_getbits(&ld, 5 DEBUGVAR(1,7,"parse_audio_decoder_specific_info(): ObjectTypeIndex")); } #endif /* get GASpecificConfig */ if (mp4ASC->objectTypeIndex == 1 || mp4ASC->objectTypeIndex == 2 || mp4ASC->objectTypeIndex == 3 || mp4ASC->objectTypeIndex == 4 || mp4ASC->objectTypeIndex == 6 || mp4ASC->objectTypeIndex == 7) { result = GASpecificConfig(&ld, mp4ASC, pce); #ifdef ERROR_RESILIENCE } else if (mp4ASC->objectTypeIndex >= ER_OBJECT_START) { /* ER */ result = GASpecificConfig(&ld, mp4ASC, pce); mp4ASC->epConfig = (uint8_t)faad_getbits(&ld, 2 DEBUGVAR(1,143,"parse_audio_decoder_specific_info(): epConfig")); if (mp4ASC->epConfig != 0) result = -5; #endif } else { result = -4; } #ifdef SSR_DEC /* shorter frames not allowed for SSR */ if ((mp4ASC->objectTypeIndex == 4) && mp4ASC->frameLengthFlag) return -6; #endif #ifdef SBR_DEC bits_to_decode = (int8_t)(buffer_size*8 - faad_get_processed_bits(&ld)); if ((mp4ASC->objectTypeIndex != 5) && (bits_to_decode >= 16)) { int16_t syncExtensionType = (int16_t)faad_getbits(&ld, 11 DEBUGVAR(1,9,"parse_audio_decoder_specific_info(): syncExtensionType")); if (syncExtensionType == 0x2b7) { uint8_t tmp_OTi = (uint8_t)faad_getbits(&ld, 5 DEBUGVAR(1,10,"parse_audio_decoder_specific_info(): extensionAudioObjectType")); if (mp4ASC->objectTypeIndex == 5) { mp4ASC->sbr_present_flag = (uint8_t)faad_get1bit(&ld DEBUGVAR(1,11,"parse_audio_decoder_specific_info(): sbr_present_flag")); if (mp4ASC->sbr_present_flag) { uint8_t tmp; /* Don't set OT to SBR until checked that it is actually there */ mp4ASC->objectTypeIndex = tmp_OTi; tmp = (uint8_t)faad_getbits(&ld, 4 DEBUGVAR(1,12,"parse_audio_decoder_specific_info(): extensionSamplingFrequencyIndex")); /* check for downsampled SBR */ if (tmp == mp4ASC->samplingFrequencyIndex) mp4ASC->downSampledSBR = 1; mp4ASC->samplingFrequencyIndex = tmp; if (mp4ASC->samplingFrequencyIndex == 15) { mp4ASC->samplingFrequency = (uint32_t)faad_getbits(&ld, 24 DEBUGVAR(1,13,"parse_audio_decoder_specific_info(): extensionSamplingFrequencyIndex")); } else { mp4ASC->samplingFrequency = get_sample_rate(mp4ASC->samplingFrequencyIndex); } } } } } /* no SBR signalled, this could mean either implicit signalling or no SBR in this file */ /* MPEG specification states: assume SBR on files with samplerate <= 24000 Hz */ if (mp4ASC->sbr_present_flag == -1) { if (mp4ASC->samplingFrequency <= 24000) { mp4ASC->samplingFrequency *= 2; mp4ASC->forceUpSampling = 1; } else /* > 24000*/ { mp4ASC->downSampledSBR = 1; } } #endif faad_endbits(&ld); return result; }
/* table 2 */ uint8_t sbr_extension_data(bitfile *ld, sbr_info *sbr, uint16_t cnt) { uint8_t result = 0; uint16_t num_align_bits = 0; uint16_t num_sbr_bits = (uint16_t)faad_get_processed_bits(ld); #ifdef DRM if (!sbr->Is_DRM_SBR) #endif { uint8_t bs_extension_type = (uint8_t)faad_getbits(ld, 4 DEBUGVAR(1,198,"sbr_bitstream(): bs_extension_type")); if (bs_extension_type == EXT_SBR_DATA_CRC) { sbr->bs_sbr_crc_bits = (uint16_t)faad_getbits(ld, 10 DEBUGVAR(1,199,"sbr_bitstream(): bs_sbr_crc_bits")); } } sbr->bs_header_flag = faad_get1bit(ld DEBUGVAR(1,200,"sbr_bitstream(): bs_header_flag")); if (sbr->bs_header_flag) sbr_header(ld, sbr); /* Reset? */ sbr_reset(sbr); /* first frame should have a header */ //if (!(sbr->frame == 0 && sbr->bs_header_flag == 0)) if (sbr->header_count != 0) { if (sbr->Reset || (sbr->bs_header_flag && sbr->just_seeked)) { uint8_t k2; /* calculate the Master Frequency Table */ sbr->k0 = qmf_start_channel(sbr->bs_start_freq, sbr->bs_samplerate_mode, sbr->sample_rate); k2 = qmf_stop_channel(sbr->bs_stop_freq, sbr->sample_rate, sbr->k0); /* check k0 and k2 */ if (sbr->sample_rate >= 48000) { if ((k2 - sbr->k0) > 32) result += 1; } else if (sbr->sample_rate <= 32000) { if ((k2 - sbr->k0) > 48) result += 1; } else { /* (sbr->sample_rate == 44100) */ if ((k2 - sbr->k0) > 45) result += 1; } if (sbr->bs_freq_scale == 0) { result += master_frequency_table_fs0(sbr, sbr->k0, k2, sbr->bs_alter_scale); } else { result += master_frequency_table(sbr, sbr->k0, k2, sbr->bs_freq_scale, sbr->bs_alter_scale); } result += derived_frequency_table(sbr, sbr->bs_xover_band, k2); result = (result > 0) ? 1 : 0; } if (result == 0) result = sbr_data(ld, sbr); } else { result = 1; } #ifdef DRM if (!sbr->Is_DRM_SBR) #endif { num_sbr_bits = (uint16_t)faad_get_processed_bits(ld) - num_sbr_bits; /* -4 does not apply, bs_extension_type is re-read in this function */ num_align_bits = 8*cnt /*- 4*/ - num_sbr_bits; while (num_align_bits > 7) { faad_getbits(ld, 8 DEBUGVAR(1,999,"sbr_bitstream(): num_align_bits")); num_align_bits -= 8; } faad_getbits(ld, num_align_bits DEBUGVAR(1,999,"sbr_bitstream(): num_align_bits")); } return result; }
/* table 3 */ static void sbr_header(bitfile *ld, sbr_info *sbr) { uint8_t bs_header_extra_1, bs_header_extra_2; sbr->header_count++; sbr->bs_amp_res = faad_get1bit(ld DEBUGVAR(1,203,"sbr_header(): bs_amp_res")); /* bs_start_freq and bs_stop_freq must define a fequency band that does not exceed 48 channels */ sbr->bs_start_freq = (uint8_t)faad_getbits(ld, 4 DEBUGVAR(1,204,"sbr_header(): bs_start_freq")); sbr->bs_stop_freq = (uint8_t)faad_getbits(ld, 4 DEBUGVAR(1,205,"sbr_header(): bs_stop_freq")); sbr->bs_xover_band = (uint8_t)faad_getbits(ld, 3 DEBUGVAR(1,206,"sbr_header(): bs_xover_band")); faad_getbits(ld, 2 DEBUGVAR(1,207,"sbr_header(): bs_reserved_bits_hdr")); bs_header_extra_1 = (uint8_t)faad_get1bit(ld DEBUGVAR(1,208,"sbr_header(): bs_header_extra_1")); bs_header_extra_2 = (uint8_t)faad_get1bit(ld DEBUGVAR(1,209,"sbr_header(): bs_header_extra_2")); if (bs_header_extra_1) { sbr->bs_freq_scale = (uint8_t)faad_getbits(ld, 2 DEBUGVAR(1,211,"sbr_header(): bs_freq_scale")); sbr->bs_alter_scale = (uint8_t)faad_get1bit(ld DEBUGVAR(1,212,"sbr_header(): bs_alter_scale")); sbr->bs_noise_bands = (uint8_t)faad_getbits(ld, 2 DEBUGVAR(1,213,"sbr_header(): bs_noise_bands")); } else { /* Default values */ sbr->bs_freq_scale = 2; sbr->bs_alter_scale = 1; sbr->bs_noise_bands = 2; } if (bs_header_extra_2) { sbr->bs_limiter_bands = (uint8_t)faad_getbits(ld, 2 DEBUGVAR(1,214,"sbr_header(): bs_limiter_bands")); sbr->bs_limiter_gains = (uint8_t)faad_getbits(ld, 2 DEBUGVAR(1,215,"sbr_header(): bs_limiter_gains")); sbr->bs_interpol_freq = (uint8_t)faad_get1bit(ld DEBUGVAR(1,216,"sbr_header(): bs_interpol_freq")); sbr->bs_smoothing_mode = (uint8_t)faad_get1bit(ld DEBUGVAR(1,217,"sbr_header(): bs_smoothing_mode")); } else { /* Default values */ sbr->bs_limiter_bands = 2; sbr->bs_limiter_gains = 2; sbr->bs_interpol_freq = 1; sbr->bs_smoothing_mode = 1; } #if 0 /* print the header to screen */ printf("bs_amp_res: %d\n", sbr->bs_amp_res); printf("bs_start_freq: %d\n", sbr->bs_start_freq); printf("bs_stop_freq: %d\n", sbr->bs_stop_freq); printf("bs_xover_band: %d\n", sbr->bs_xover_band); if (bs_header_extra_1) { printf("bs_freq_scale: %d\n", sbr->bs_freq_scale); printf("bs_alter_scale: %d\n", sbr->bs_alter_scale); printf("bs_noise_bands: %d\n", sbr->bs_noise_bands); } if (bs_header_extra_2) { printf("bs_limiter_bands: %d\n", sbr->bs_limiter_bands); printf("bs_limiter_gains: %d\n", sbr->bs_limiter_gains); printf("bs_interpol_freq: %d\n", sbr->bs_interpol_freq); printf("bs_smoothing_mode: %d\n", sbr->bs_smoothing_mode); } printf("\n"); #endif }
/* table 5 */ static uint8_t sbr_single_channel_element(bitfile *ld, sbr_info *sbr) { uint8_t result; if (faad_get1bit(ld DEBUGVAR(1,220,"sbr_single_channel_element(): bs_data_extra"))) { faad_getbits(ld, 4 DEBUGVAR(1,221,"sbr_single_channel_element(): bs_reserved_bits_data")); } #ifdef DRM /* bs_coupling, from sbr_channel_pair_base_element(bs_amp_res) */ if (sbr->Is_DRM_SBR) { faad_get1bit(ld); } #endif if ((result = sbr_grid(ld, sbr, 0)) > 0) return result; sbr_dtdf(ld, sbr, 0); invf_mode(ld, sbr, 0); sbr_envelope(ld, sbr, 0); sbr_noise(ld, sbr, 0); #ifndef FIXED_POINT envelope_noise_dequantisation(sbr, 0); #endif memset(sbr->bs_add_harmonic[0], 0, 64*sizeof(uint8_t)); sbr->bs_add_harmonic_flag[0] = faad_get1bit(ld DEBUGVAR(1,223,"sbr_single_channel_element(): bs_add_harmonic_flag[0]")); if (sbr->bs_add_harmonic_flag[0]) sinusoidal_coding(ld, sbr, 0); sbr->bs_extended_data = faad_get1bit(ld DEBUGVAR(1,224,"sbr_single_channel_element(): bs_extended_data[0]")); if (sbr->bs_extended_data) { uint16_t nr_bits_left; #if (defined(PS_DEC) || defined(DRM_PS)) uint8_t ps_ext_read = 0; #endif uint16_t cnt = (uint16_t)faad_getbits(ld, 4 DEBUGVAR(1,225,"sbr_single_channel_element(): bs_extension_size")); if (cnt == 15) { cnt += (uint16_t)faad_getbits(ld, 8 DEBUGVAR(1,226,"sbr_single_channel_element(): bs_esc_count")); } nr_bits_left = 8 * cnt; while (nr_bits_left > 7) { uint16_t tmp_nr_bits = 0; sbr->bs_extension_id = (uint8_t)faad_getbits(ld, 2 DEBUGVAR(1,227,"sbr_single_channel_element(): bs_extension_id")); tmp_nr_bits += 2; /* allow only 1 PS extension element per extension data */ #if (defined(PS_DEC) || defined(DRM_PS)) #if (defined(PS_DEC) && defined(DRM_PS)) if (sbr->bs_extension_id == EXTENSION_ID_PS || sbr->bs_extension_id == DRM_PARAMETRIC_STEREO) #else #ifdef PS_DEC if (sbr->bs_extension_id == EXTENSION_ID_PS) #else #ifdef DRM_PS if (sbr->bs_extension_id == DRM_PARAMETRIC_STEREO) #endif #endif #endif { if (ps_ext_read == 0) { ps_ext_read = 1; } else { /* to be safe make it 3, will switch to "default" * in sbr_extension() */ #ifdef DRM return 1; #else sbr->bs_extension_id = 3; #endif } } #endif tmp_nr_bits += sbr_extension(ld, sbr, sbr->bs_extension_id, nr_bits_left); /* check if the data read is bigger than the number of available bits */ if (tmp_nr_bits > nr_bits_left) return 1; nr_bits_left -= tmp_nr_bits; } /* Corrigendum */ if (nr_bits_left > 0) { faad_getbits(ld, nr_bits_left DEBUGVAR(1,280,"sbr_single_channel_element(): nr_bits_left")); } } return 0; }
/* table 7 */ static uint8_t sbr_grid(bitfile *ld, sbr_info *sbr, uint8_t ch) { uint8_t i, env, rel, result; uint8_t bs_abs_bord, bs_abs_bord_1; uint8_t bs_num_env = 0; sbr->bs_frame_class[ch] = (uint8_t)faad_getbits(ld, 2 DEBUGVAR(1,248,"sbr_grid(): bs_frame_class")); switch (sbr->bs_frame_class[ch]) { case FIXFIX: i = (uint8_t)faad_getbits(ld, 2 DEBUGVAR(1,249,"sbr_grid(): bs_num_env_raw")); bs_num_env = min(1 << i, 5); i = (uint8_t)faad_get1bit(ld DEBUGVAR(1,250,"sbr_grid(): bs_freq_res_flag")); for (env = 0; env < bs_num_env; env++) sbr->f[ch][env] = i; sbr->abs_bord_lead[ch] = 0; sbr->abs_bord_trail[ch] = sbr->numTimeSlots; sbr->n_rel_lead[ch] = bs_num_env - 1; sbr->n_rel_trail[ch] = 0; break; case FIXVAR: bs_abs_bord = (uint8_t)faad_getbits(ld, 2 DEBUGVAR(1,251,"sbr_grid(): bs_abs_bord")) + sbr->numTimeSlots; bs_num_env = (uint8_t)faad_getbits(ld, 2 DEBUGVAR(1,252,"sbr_grid(): bs_num_env")) + 1; for (rel = 0; rel < bs_num_env-1; rel++) { sbr->bs_rel_bord[ch][rel] = 2 * (uint8_t)faad_getbits(ld, 2 DEBUGVAR(1,253,"sbr_grid(): bs_rel_bord")) + 2; } i = sbr_log2(bs_num_env + 1); sbr->bs_pointer[ch] = (uint8_t)faad_getbits(ld, i DEBUGVAR(1,254,"sbr_grid(): bs_pointer")); for (env = 0; env < bs_num_env; env++) { sbr->f[ch][bs_num_env - env - 1] = (uint8_t)faad_get1bit(ld DEBUGVAR(1,255,"sbr_grid(): bs_freq_res")); } sbr->abs_bord_lead[ch] = 0; sbr->abs_bord_trail[ch] = bs_abs_bord; sbr->n_rel_lead[ch] = 0; sbr->n_rel_trail[ch] = bs_num_env - 1; break; case VARFIX: bs_abs_bord = (uint8_t)faad_getbits(ld, 2 DEBUGVAR(1,256,"sbr_grid(): bs_abs_bord")); bs_num_env = (uint8_t)faad_getbits(ld, 2 DEBUGVAR(1,257,"sbr_grid(): bs_num_env")) + 1; for (rel = 0; rel < bs_num_env-1; rel++) { sbr->bs_rel_bord[ch][rel] = 2 * (uint8_t)faad_getbits(ld, 2 DEBUGVAR(1,258,"sbr_grid(): bs_rel_bord")) + 2; } i = sbr_log2(bs_num_env + 1); sbr->bs_pointer[ch] = (uint8_t)faad_getbits(ld, i DEBUGVAR(1,259,"sbr_grid(): bs_pointer")); for (env = 0; env < bs_num_env; env++) { sbr->f[ch][env] = (uint8_t)faad_get1bit(ld DEBUGVAR(1,260,"sbr_grid(): bs_freq_res")); } sbr->abs_bord_lead[ch] = bs_abs_bord; sbr->abs_bord_trail[ch] = sbr->numTimeSlots; sbr->n_rel_lead[ch] = bs_num_env - 1; sbr->n_rel_trail[ch] = 0; break; case VARVAR: bs_abs_bord = (uint8_t)faad_getbits(ld, 2 DEBUGVAR(1,261,"sbr_grid(): bs_abs_bord_0")); bs_abs_bord_1 = (uint8_t)faad_getbits(ld, 2 DEBUGVAR(1,262,"sbr_grid(): bs_abs_bord_1")) + sbr->numTimeSlots; sbr->bs_num_rel_0[ch] = (uint8_t)faad_getbits(ld, 2 DEBUGVAR(1,263,"sbr_grid(): bs_num_rel_0")); sbr->bs_num_rel_1[ch] = (uint8_t)faad_getbits(ld, 2 DEBUGVAR(1,264,"sbr_grid(): bs_num_rel_1")); bs_num_env = min(5, sbr->bs_num_rel_0[ch] + sbr->bs_num_rel_1[ch] + 1); for (rel = 0; rel < sbr->bs_num_rel_0[ch]; rel++) { sbr->bs_rel_bord_0[ch][rel] = 2 * (uint8_t)faad_getbits(ld, 2 DEBUGVAR(1,265,"sbr_grid(): bs_rel_bord")) + 2; } for(rel = 0; rel < sbr->bs_num_rel_1[ch]; rel++) { sbr->bs_rel_bord_1[ch][rel] = 2 * (uint8_t)faad_getbits(ld, 2 DEBUGVAR(1,266,"sbr_grid(): bs_rel_bord")) + 2; } i = sbr_log2(sbr->bs_num_rel_0[ch] + sbr->bs_num_rel_1[ch] + 2); sbr->bs_pointer[ch] = (uint8_t)faad_getbits(ld, i DEBUGVAR(1,267,"sbr_grid(): bs_pointer")); for (env = 0; env < bs_num_env; env++) { sbr->f[ch][env] = (uint8_t)faad_get1bit(ld DEBUGVAR(1,268,"sbr_grid(): bs_freq_res")); } sbr->abs_bord_lead[ch] = bs_abs_bord; sbr->abs_bord_trail[ch] = bs_abs_bord_1; sbr->n_rel_lead[ch] = sbr->bs_num_rel_0[ch]; sbr->n_rel_trail[ch] = sbr->bs_num_rel_1[ch]; break; } if (sbr->bs_frame_class[ch] == VARVAR) sbr->L_E[ch] = min(bs_num_env, 5); else sbr->L_E[ch] = min(bs_num_env, 4); if (sbr->L_E[ch] <= 0) return 1; if (sbr->L_E[ch] > 1) sbr->L_Q[ch] = 2; else sbr->L_Q[ch] = 1; /* TODO: this code can probably be integrated into the code above! */ if ((result = envelope_time_border_vector(sbr, ch)) > 0) return result; noise_floor_time_border_vector(sbr, ch); return 0; }
uint16_t ps_data(ps_info *ps, bitfile *ld) { uint8_t tmp, n; uint16_t bits = (uint16_t)faad_get_processed_bits(ld); /* check for new PS header */ if (faad_get1bit(ld DEBUGVAR(1,1000,"ps_data(): enable_ps_header"))) { ps->use34hybrid_bands = 0; /* Inter-channel Intensity Difference (IID) parameters enabled */ ps->enable_iid = (uint8_t)faad_get1bit(ld DEBUGVAR(1,1001,"ps_data(): enable_iid")); if (ps->enable_iid) { ps->iid_mode = (uint8_t)faad_getbits(ld, 3 DEBUGVAR(1,1002,"ps_data(): iid_mode")); ps->nr_iid_par = nr_iid_par_tab[ps->iid_mode]; ps->nr_ipdopd_par = nr_ipdopd_par_tab[ps->iid_mode]; if (ps->iid_mode == 2 || ps->iid_mode == 5) ps->use34hybrid_bands = 1; /* IPD freq res equal to IID freq res */ ps->ipd_mode = ps->iid_mode; } /* Inter-channel Coherence (ICC) parameters enabled */ ps->enable_icc = (uint8_t)faad_get1bit(ld DEBUGVAR(1,1003,"ps_data(): enable_icc")); if (ps->enable_icc) { ps->icc_mode = (uint8_t)faad_getbits(ld, 3 DEBUGVAR(1,1004,"ps_data(): icc_mode")); ps->nr_icc_par = nr_icc_par_tab[ps->icc_mode]; if (ps->icc_mode == 2 || ps->icc_mode == 5) ps->use34hybrid_bands = 1; } /* PS extension layer enabled */ ps->enable_ext = (uint8_t)faad_get1bit(ld DEBUGVAR(1,1005,"ps_data(): enable_ext")); } ps->frame_class = (uint8_t)faad_get1bit(ld DEBUGVAR(1,1006,"ps_data(): frame_class")); tmp = (uint8_t)faad_getbits(ld, 2 DEBUGVAR(1,1007,"ps_data(): num_env_idx")); ps->num_env = num_env_tab[ps->frame_class][tmp]; if (ps->frame_class) { for (n = 1; n < ps->num_env+1; n++) { ps->border_position[n] = (uint8_t)faad_getbits(ld, 5 DEBUGVAR(1,1008,"ps_data(): border_position")); } } if (ps->enable_iid) { for (n = 0; n < ps->num_env; n++) { ps->iid_dt[n] = (uint8_t)faad_get1bit(ld DEBUGVAR(1,1009,"ps_data(): iid_dt")); /* iid_data */ if (ps->iid_mode < 3) { huff_data(ld, ps->iid_dt[n], ps->nr_iid_par, t_huff_iid_def, f_huff_iid_def, ps->iid_index[n]); } else { huff_data(ld, ps->iid_dt[n], ps->nr_iid_par, t_huff_iid_fine, f_huff_iid_fine, ps->iid_index[n]); } } } if (ps->enable_icc) { for (n = 0; n < ps->num_env; n++) { ps->icc_dt[n] = (uint8_t)faad_get1bit(ld DEBUGVAR(1,1010,"ps_data(): icc_dt")); /* icc_data */ huff_data(ld, ps->icc_dt[n], ps->nr_icc_par, t_huff_icc, f_huff_icc, ps->icc_index[n]); } } if (ps->enable_ext) { uint16_t num_bits_left; uint16_t cnt = (uint16_t)faad_getbits(ld, 4 DEBUGVAR(1,1011,"ps_data(): ps_extension_size")); if (cnt == 15) { cnt += (uint16_t)faad_getbits(ld, 8 DEBUGVAR(1,1012,"ps_data(): esc_count")); } num_bits_left = 8 * cnt; while (num_bits_left > 7) { uint8_t ps_extension_id = (uint8_t)faad_getbits(ld, 2 DEBUGVAR(1,1013,"ps_data(): ps_extension_size")); num_bits_left -= 2; num_bits_left -= ps_extension(ps, ld, ps_extension_id, num_bits_left); } faad_getbits(ld, num_bits_left DEBUGVAR(1,1014,"ps_data(): fill_bits")); } bits = (uint16_t)faad_get_processed_bits(ld) - bits; ps->ps_data_available = 1; return bits; }
/* table 2 */ uint8_t sbr_extension_data(bitfile *ld, sbr_info *sbr, uint16_t cnt, uint8_t psResetFlag) { uint8_t result = 0; uint16_t num_align_bits = 0; uint16_t num_sbr_bits1 = (uint16_t)faad_get_processed_bits(ld); uint16_t num_sbr_bits2; uint8_t saved_start_freq, saved_samplerate_mode; uint8_t saved_stop_freq, saved_freq_scale; uint8_t saved_alter_scale, saved_xover_band; #if (defined(PS_DEC) || defined(DRM_PS)) if (psResetFlag) sbr->psResetFlag = psResetFlag; #endif #ifdef DRM if (!sbr->Is_DRM_SBR) #endif { uint8_t bs_extension_type = (uint8_t)faad_getbits(ld, 4 DEBUGVAR(1,198,"sbr_bitstream(): bs_extension_type")); if (bs_extension_type == EXT_SBR_DATA_CRC) { sbr->bs_sbr_crc_bits = (uint16_t)faad_getbits(ld, 10 DEBUGVAR(1,199,"sbr_bitstream(): bs_sbr_crc_bits")); } } /* save old header values, in case the new ones are corrupted */ saved_start_freq = sbr->bs_start_freq; saved_samplerate_mode = sbr->bs_samplerate_mode; saved_stop_freq = sbr->bs_stop_freq; saved_freq_scale = sbr->bs_freq_scale; saved_alter_scale = sbr->bs_alter_scale; saved_xover_band = sbr->bs_xover_band; sbr->bs_header_flag = faad_get1bit(ld DEBUGVAR(1,200,"sbr_bitstream(): bs_header_flag")); if (sbr->bs_header_flag) sbr_header(ld, sbr); /* Reset? */ sbr_reset(sbr); /* first frame should have a header */ //if (!(sbr->frame == 0 && sbr->bs_header_flag == 0)) if (sbr->header_count != 0) { if (sbr->Reset || (sbr->bs_header_flag && sbr->just_seeked)) { uint8_t rt = calc_sbr_tables(sbr, sbr->bs_start_freq, sbr->bs_stop_freq, sbr->bs_samplerate_mode, sbr->bs_freq_scale, sbr->bs_alter_scale, sbr->bs_xover_band); /* if an error occured with the new header values revert to the old ones */ if (rt > 0) { calc_sbr_tables(sbr, saved_start_freq, saved_stop_freq, saved_samplerate_mode, saved_freq_scale, saved_alter_scale, saved_xover_band); } } if (result == 0) { result = sbr_data(ld, sbr); /* sbr_data() returning an error means that there was an error in envelope_time_border_vector(). In this case the old time border vector is saved and all the previous data normally read after sbr_grid() is saved. */ /* to be on the safe side, calculate old sbr tables in case of error */ if ((result > 0) && (sbr->Reset || (sbr->bs_header_flag && sbr->just_seeked))) { calc_sbr_tables(sbr, saved_start_freq, saved_stop_freq, saved_samplerate_mode, saved_freq_scale, saved_alter_scale, saved_xover_band); } /* we should be able to safely set result to 0 now, */ /* but practise indicates this doesn't work well */ } } else { result = 1; } num_sbr_bits2 = (uint16_t)faad_get_processed_bits(ld) - num_sbr_bits1; /* check if we read more bits then were available for sbr */ if (8*cnt < num_sbr_bits2) { faad_resetbits(ld, num_sbr_bits1 + 8*cnt); num_sbr_bits2 = 8*cnt; #ifdef PS_DEC /* turn off PS for the unfortunate case that we randomly read some * PS data that looks correct */ sbr->ps_used = 0; #endif /* Make sure it doesn't decode SBR in this frame, or we'll get glitches */ return 1; } #ifdef DRM if (!sbr->Is_DRM_SBR) #endif { /* -4 does not apply, bs_extension_type is re-read in this function */ num_align_bits = 8*cnt /*- 4*/ - num_sbr_bits2; while (num_align_bits > 7) { faad_getbits(ld, 8 DEBUGVAR(1,999,"sbr_bitstream(): num_align_bits")); num_align_bits -= 8; } faad_getbits(ld, num_align_bits DEBUGVAR(1,999,"sbr_bitstream(): num_align_bits")); } return result; }
/* table 5 */ static uint8_t sbr_single_channel_element(bitfile *ld, sbr_info *sbr) { uint8_t result; if (faad_get1bit(ld DEBUGVAR(1,220,"sbr_single_channel_element(): bs_data_extra"))) { faad_getbits(ld, 4 DEBUGVAR(1,221,"sbr_single_channel_element(): bs_reserved_bits_data")); } #ifdef DRM /* bs_coupling, from sbr_channel_pair_base_element(bs_amp_res) */ if (sbr->Is_DRM_SBR) faad_get1bit(ld); #endif if ((result = sbr_grid(ld, sbr, 0)) > 0) return result; sbr_dtdf(ld, sbr, 0); invf_mode(ld, sbr, 0); sbr_envelope(ld, sbr, 0); sbr_noise(ld, sbr, 0); envelope_noise_dequantisation(sbr, 0); memset(sbr->bs_add_harmonic[0], 0, 64*sizeof(uint8_t)); sbr->bs_add_harmonic_flag[0] = faad_get1bit(ld DEBUGVAR(1,223,"sbr_single_channel_element(): bs_add_harmonic_flag[0]")); if (sbr->bs_add_harmonic_flag[0]) sinusoidal_coding(ld, sbr, 0); sbr->bs_extended_data = faad_get1bit(ld DEBUGVAR(1,224,"sbr_single_channel_element(): bs_extended_data[0]")); if (sbr->bs_extended_data) { uint16_t nr_bits_left; uint16_t cnt = (uint16_t)faad_getbits(ld, 4 DEBUGVAR(1,225,"sbr_single_channel_element(): bs_extension_size")); if (cnt == 15) { cnt += (uint16_t)faad_getbits(ld, 8 DEBUGVAR(1,226,"sbr_single_channel_element(): bs_esc_count")); } nr_bits_left = 8 * cnt; while (nr_bits_left > 7) { sbr->bs_extension_id = (uint8_t)faad_getbits(ld, 2 DEBUGVAR(1,227,"sbr_single_channel_element(): bs_extension_id")); nr_bits_left -= 2; nr_bits_left -= sbr_extension(ld, sbr, sbr->bs_extension_id, nr_bits_left); } /* Corrigendum */ if (nr_bits_left > 0) { faad_getbits(ld, nr_bits_left DEBUGVAR(1,280,"sbr_single_channel_element(): nr_bits_left")); } } return 0; }