sbr_syntax.c

00001 /*
00002 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
00003 ** Copyright (C) 2003-2005 M. Bakker, Ahead Software AG, http://www.nero.com
00004 **  
00005 ** This program is free software; you can redistribute it and/or modify
00006 ** it under the terms of the GNU General Public License as published by
00007 ** the Free Software Foundation; either version 2 of the License, or
00008 ** (at your option) any later version.
00009 ** 
00010 ** This program is distributed in the hope that it will be useful,
00011 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 ** GNU General Public License for more details.
00014 ** 
00015 ** You should have received a copy of the GNU General Public License
00016 ** along with this program; if not, write to the Free Software 
00017 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00018 **
00019 ** Any non-GPL usage of this software or parts of this software is strictly
00020 ** forbidden.
00021 **
00022 ** Software using this code must display the following message visibly in the
00023 ** software:
00024 ** "FAAD2 AAC/HE-AAC/HE-AACv2/DRM decoder (c) Ahead Software, www.nero.com"
00025 ** in, for example, the about-box or help/startup screen.
00026 **
00027 ** Commercial non-GPL licensing of this software is possible.
00028 ** For more info contact Ahead Software through [email protected].
00029 **
00030 ** $Id: sbr_syntax.c,v 1.3 2005/11/01 21:41:43 gabest Exp $
00031 **/
00032 
00033 #include "common.h"
00034 #include "structs.h"
00035 
00036 #ifdef SBR_DEC
00037 
00038 #include "sbr_syntax.h"
00039 #include "syntax.h"
00040 #include "sbr_huff.h"
00041 #include "sbr_fbt.h"
00042 #include "sbr_tf_grid.h"
00043 #include "sbr_e_nf.h"
00044 #include "bits.h"
00045 #ifdef PS_DEC
00046 #include "ps_dec.h"
00047 #endif
00048 #ifdef DRM_PS
00049 #include "drm_dec.h"
00050 #endif
00051 #include "analysis.h"
00052 
00053 /* static function declarations */
00054 static void sbr_header(bitfile *ld, sbr_info *sbr);
00055 static uint8_t calc_sbr_tables(sbr_info *sbr, uint8_t start_freq, uint8_t stop_freq,
00056                                uint8_t samplerate_mode, uint8_t freq_scale,
00057                                uint8_t alter_scale, uint8_t xover_band);
00058 static uint8_t sbr_data(bitfile *ld, sbr_info *sbr);
00059 static uint16_t sbr_extension(bitfile *ld, sbr_info *sbr,
00060                               uint8_t bs_extension_id, uint16_t num_bits_left);
00061 static uint8_t sbr_single_channel_element(bitfile *ld, sbr_info *sbr);
00062 static uint8_t sbr_channel_pair_element(bitfile *ld, sbr_info *sbr);
00063 static uint8_t sbr_grid(bitfile *ld, sbr_info *sbr, uint8_t ch);
00064 static void sbr_dtdf(bitfile *ld, sbr_info *sbr, uint8_t ch);
00065 static void invf_mode(bitfile *ld, sbr_info *sbr, uint8_t ch);
00066 static void sinusoidal_coding(bitfile *ld, sbr_info *sbr, uint8_t ch);
00067 
00068 
00069 static void sbr_reset(sbr_info *sbr)
00070 {
00071 #if 0
00072     printf("%d\n", sbr->bs_start_freq_prev);
00073     printf("%d\n", sbr->bs_stop_freq_prev);
00074     printf("%d\n", sbr->bs_freq_scale_prev);
00075     printf("%d\n", sbr->bs_alter_scale_prev);
00076     printf("%d\n", sbr->bs_xover_band_prev);
00077     printf("%d\n\n", sbr->bs_noise_bands_prev);
00078 #endif
00079 
00080     /* if these are different from the previous frame: Reset = 1 */
00081     if ((sbr->bs_start_freq != sbr->bs_start_freq_prev) ||
00082         (sbr->bs_stop_freq != sbr->bs_stop_freq_prev) ||
00083         (sbr->bs_freq_scale != sbr->bs_freq_scale_prev) ||
00084         (sbr->bs_alter_scale != sbr->bs_alter_scale_prev) ||
00085         (sbr->bs_xover_band != sbr->bs_xover_band_prev) ||
00086         (sbr->bs_noise_bands != sbr->bs_noise_bands_prev))
00087     {
00088         sbr->Reset = 1;
00089     } else {
00090         sbr->Reset = 0;
00091     }
00092 
00093     sbr->bs_start_freq_prev = sbr->bs_start_freq;
00094     sbr->bs_stop_freq_prev = sbr->bs_stop_freq;
00095     sbr->bs_freq_scale_prev = sbr->bs_freq_scale;
00096     sbr->bs_alter_scale_prev = sbr->bs_alter_scale;
00097     sbr->bs_xover_band_prev = sbr->bs_xover_band;
00098     sbr->bs_noise_bands_prev = sbr->bs_noise_bands;
00099 }
00100 
00101 static uint8_t calc_sbr_tables(sbr_info *sbr, uint8_t start_freq, uint8_t stop_freq,
00102                                uint8_t samplerate_mode, uint8_t freq_scale,
00103                                uint8_t alter_scale, uint8_t xover_band)
00104 {
00105     uint8_t result = 0;
00106     uint8_t k2;
00107 
00108     /* calculate the Master Frequency Table */
00109     sbr->k0 = qmf_start_channel(start_freq, samplerate_mode, sbr->sample_rate);
00110     k2 = qmf_stop_channel(stop_freq, sbr->sample_rate, sbr->k0);
00111 
00112     /* check k0 and k2 */
00113     if (sbr->sample_rate >= 48000)
00114     {
00115         if ((k2 - sbr->k0) > 32)
00116             result += 1;
00117     } else if (sbr->sample_rate <= 32000) {
00118         if ((k2 - sbr->k0) > 48)
00119             result += 1;
00120     } else { /* (sbr->sample_rate == 44100) */
00121         if ((k2 - sbr->k0) > 45)
00122             result += 1;
00123     }
00124 
00125     if (freq_scale == 0)
00126     {
00127         result += master_frequency_table_fs0(sbr, sbr->k0, k2, alter_scale);
00128     } else {
00129         result += master_frequency_table(sbr, sbr->k0, k2, freq_scale, alter_scale);
00130     }
00131     result += derived_frequency_table(sbr, xover_band, k2);
00132 
00133     result = (result > 0) ? 1 : 0;
00134 
00135     return result;
00136 }
00137 
00138 /* table 2 */
00139 uint8_t sbr_extension_data(bitfile *ld, sbr_info *sbr, uint16_t cnt)
00140 {
00141     uint8_t result = 0;
00142     uint16_t num_align_bits = 0;
00143     uint16_t num_sbr_bits = (uint16_t)faad_get_processed_bits(ld);
00144 
00145     uint8_t saved_start_freq, saved_samplerate_mode;
00146     uint8_t saved_stop_freq, saved_freq_scale;
00147     uint8_t saved_alter_scale, saved_xover_band;
00148 
00149 #ifdef DRM
00150     if (!sbr->Is_DRM_SBR)
00151 #endif
00152     {
00153         uint8_t bs_extension_type = (uint8_t)faad_getbits(ld, 4
00154             DEBUGVAR(1,198,"sbr_bitstream(): bs_extension_type"));
00155 
00156         if (bs_extension_type == EXT_SBR_DATA_CRC)
00157         {
00158             sbr->bs_sbr_crc_bits = (uint16_t)faad_getbits(ld, 10
00159                 DEBUGVAR(1,199,"sbr_bitstream(): bs_sbr_crc_bits"));
00160         }
00161     }
00162 
00163     /* save old header values, in case the new ones are corrupted */
00164     saved_start_freq = sbr->bs_start_freq;
00165     saved_samplerate_mode = sbr->bs_samplerate_mode;
00166     saved_stop_freq = sbr->bs_stop_freq;
00167     saved_freq_scale = sbr->bs_freq_scale;
00168     saved_alter_scale = sbr->bs_alter_scale;
00169     saved_xover_band = sbr->bs_xover_band;
00170 
00171     sbr->bs_header_flag = faad_get1bit(ld
00172         DEBUGVAR(1,200,"sbr_bitstream(): bs_header_flag"));
00173 
00174     if (sbr->bs_header_flag)
00175         sbr_header(ld, sbr);
00176 
00177     /* Reset? */
00178     sbr_reset(sbr);
00179 
00180     /* first frame should have a header */
00181     //if (!(sbr->frame == 0 && sbr->bs_header_flag == 0))
00182     if (sbr->header_count != 0)
00183     {
00184         if (sbr->Reset || (sbr->bs_header_flag && sbr->just_seeked))
00185         {
00186             uint8_t rt = calc_sbr_tables(sbr, sbr->bs_start_freq, sbr->bs_stop_freq,
00187                 sbr->bs_samplerate_mode, sbr->bs_freq_scale,
00188                 sbr->bs_alter_scale, sbr->bs_xover_band);
00189 
00190             /* if an error occured with the new header values revert to the old ones */
00191             if (rt > 0)
00192             {
00193                 calc_sbr_tables(sbr, saved_start_freq, saved_stop_freq,
00194                     saved_samplerate_mode, saved_freq_scale,
00195                     saved_alter_scale, saved_xover_band);
00196             }
00197         }
00198 
00199         if (result == 0)
00200         {
00201             result = sbr_data(ld, sbr);
00202 
00203             /* sbr_data() returning an error means that there was an error in
00204                envelope_time_border_vector().
00205                In this case the old time border vector is saved and all the previous
00206                data normally read after sbr_grid() is saved.
00207             */
00208             /* to be on the safe side, calculate old sbr tables in case of error */
00209             if ((result > 0) &&
00210                 (sbr->Reset || (sbr->bs_header_flag && sbr->just_seeked)))
00211             {
00212                 calc_sbr_tables(sbr, saved_start_freq, saved_stop_freq,
00213                     saved_samplerate_mode, saved_freq_scale,
00214                     saved_alter_scale, saved_xover_band);          
00215             }
00216 
00217             /* we should be able to safely set result to 0 now, */
00218             /* but practise indicates this doesn't work well */
00219         }
00220     } else {
00221         result = 1;
00222     }     
00223            
00224      
00225     num_sbr_bits = (uint16_t)faad_get_processed_bits(ld) - num_sbr_bits;
00226 
00227     /* check if we read more bits then were available for sbr */
00228     if (8*cnt < num_sbr_bits) {
00229             return 1;
00230     }
00231 
00232 #ifdef DRM
00233     if (!sbr->Is_DRM_SBR)
00234 #endif
00235     {       
00236         /* -4 does not apply, bs_extension_type is re-read in this function */
00237         num_align_bits = 8*cnt /*- 4*/ - num_sbr_bits;
00238 
00239         while (num_align_bits > 7)
00240         {
00241             faad_getbits(ld, 8
00242                 DEBUGVAR(1,999,"sbr_bitstream(): num_align_bits"));
00243             num_align_bits -= 8;
00244         }
00245         faad_getbits(ld, num_align_bits
00246             DEBUGVAR(1,999,"sbr_bitstream(): num_align_bits"));
00247     }
00248 
00249     return result;
00250 }
00251 
00252 /* table 3 */
00253 static void sbr_header(bitfile *ld, sbr_info *sbr)
00254 {
00255     uint8_t bs_header_extra_1, bs_header_extra_2;
00256 
00257     sbr->header_count++;
00258 
00259     sbr->bs_amp_res = faad_get1bit(ld
00260         DEBUGVAR(1,203,"sbr_header(): bs_amp_res"));
00261 
00262     /* bs_start_freq and bs_stop_freq must define a fequency band that does
00263        not exceed 48 channels */
00264     sbr->bs_start_freq = (uint8_t)faad_getbits(ld, 4
00265         DEBUGVAR(1,204,"sbr_header(): bs_start_freq"));
00266     sbr->bs_stop_freq = (uint8_t)faad_getbits(ld, 4
00267         DEBUGVAR(1,205,"sbr_header(): bs_stop_freq"));
00268     sbr->bs_xover_band = (uint8_t)faad_getbits(ld, 3
00269         DEBUGVAR(1,206,"sbr_header(): bs_xover_band"));
00270     faad_getbits(ld, 2
00271         DEBUGVAR(1,207,"sbr_header(): bs_reserved_bits_hdr"));
00272     bs_header_extra_1 = (uint8_t)faad_get1bit(ld
00273         DEBUGVAR(1,208,"sbr_header(): bs_header_extra_1"));
00274     bs_header_extra_2 = (uint8_t)faad_get1bit(ld
00275         DEBUGVAR(1,209,"sbr_header(): bs_header_extra_2"));
00276 
00277     if (bs_header_extra_1)
00278     {
00279         sbr->bs_freq_scale = (uint8_t)faad_getbits(ld, 2
00280             DEBUGVAR(1,211,"sbr_header(): bs_freq_scale"));
00281         sbr->bs_alter_scale = (uint8_t)faad_get1bit(ld
00282             DEBUGVAR(1,212,"sbr_header(): bs_alter_scale"));
00283         sbr->bs_noise_bands = (uint8_t)faad_getbits(ld, 2
00284             DEBUGVAR(1,213,"sbr_header(): bs_noise_bands"));
00285     } else {
00286         /* Default values */
00287         sbr->bs_freq_scale = 2;
00288         sbr->bs_alter_scale = 1;
00289         sbr->bs_noise_bands = 2;
00290     }
00291 
00292     if (bs_header_extra_2)
00293     {
00294         sbr->bs_limiter_bands = (uint8_t)faad_getbits(ld, 2
00295             DEBUGVAR(1,214,"sbr_header(): bs_limiter_bands"));
00296         sbr->bs_limiter_gains = (uint8_t)faad_getbits(ld, 2
00297             DEBUGVAR(1,215,"sbr_header(): bs_limiter_gains"));
00298         sbr->bs_interpol_freq = (uint8_t)faad_get1bit(ld
00299             DEBUGVAR(1,216,"sbr_header(): bs_interpol_freq"));
00300         sbr->bs_smoothing_mode = (uint8_t)faad_get1bit(ld
00301             DEBUGVAR(1,217,"sbr_header(): bs_smoothing_mode"));
00302     } else {
00303         /* Default values */
00304         sbr->bs_limiter_bands = 2;
00305         sbr->bs_limiter_gains = 2;
00306         sbr->bs_interpol_freq = 1;
00307         sbr->bs_smoothing_mode = 1;
00308     }
00309 
00310 #if 0
00311     /* print the header to screen */
00312     printf("bs_amp_res: %d\n", sbr->bs_amp_res);
00313     printf("bs_start_freq: %d\n", sbr->bs_start_freq);
00314     printf("bs_stop_freq: %d\n", sbr->bs_stop_freq);
00315     printf("bs_xover_band: %d\n", sbr->bs_xover_band);
00316     if (bs_header_extra_1)
00317     {
00318         printf("bs_freq_scale: %d\n", sbr->bs_freq_scale);
00319         printf("bs_alter_scale: %d\n", sbr->bs_alter_scale);
00320         printf("bs_noise_bands: %d\n", sbr->bs_noise_bands);
00321     }
00322     if (bs_header_extra_2)
00323     {
00324         printf("bs_limiter_bands: %d\n", sbr->bs_limiter_bands);
00325         printf("bs_limiter_gains: %d\n", sbr->bs_limiter_gains);
00326         printf("bs_interpol_freq: %d\n", sbr->bs_interpol_freq);
00327         printf("bs_smoothing_mode: %d\n", sbr->bs_smoothing_mode);
00328     }
00329     printf("\n");
00330 #endif
00331 }
00332 
00333 /* table 4 */
00334 static uint8_t sbr_data(bitfile *ld, sbr_info *sbr)
00335 {
00336     uint8_t result;
00337 #if 0
00338     sbr->bs_samplerate_mode = faad_get1bit(ld
00339         DEBUGVAR(1,219,"sbr_data(): bs_samplerate_mode"));
00340 #endif
00341 
00342     sbr->rate = (sbr->bs_samplerate_mode) ? 2 : 1;
00343 
00344     switch (sbr->id_aac)
00345     {
00346     case ID_SCE:
00347                 if ((result = sbr_single_channel_element(ld, sbr)) > 0)
00348                         return result;
00349         break;
00350     case ID_CPE:
00351                 if ((result = sbr_channel_pair_element(ld, sbr)) > 0)
00352                         return result;
00353         break;
00354     }
00355 
00356         return 0;
00357 }
00358 
00359 /* table 5 */
00360 static uint8_t sbr_single_channel_element(bitfile *ld, sbr_info *sbr)
00361 {
00362     uint8_t result;
00363 
00364     if (faad_get1bit(ld
00365         DEBUGVAR(1,220,"sbr_single_channel_element(): bs_data_extra")))
00366     {
00367         faad_getbits(ld, 4
00368             DEBUGVAR(1,221,"sbr_single_channel_element(): bs_reserved_bits_data"));
00369     }
00370 
00371 #ifdef DRM
00372     /* bs_coupling, from sbr_channel_pair_base_element(bs_amp_res) */
00373     if (sbr->Is_DRM_SBR)
00374     {
00375         faad_get1bit(ld);
00376     }
00377 #endif
00378 
00379     if ((result = sbr_grid(ld, sbr, 0)) > 0)
00380         return result;
00381 
00382     sbr_dtdf(ld, sbr, 0);
00383     invf_mode(ld, sbr, 0);
00384     sbr_envelope(ld, sbr, 0);
00385     sbr_noise(ld, sbr, 0);
00386 
00387 #ifndef FIXED_POINT
00388     envelope_noise_dequantisation(sbr, 0);
00389 #endif
00390 
00391     memset(sbr->bs_add_harmonic[0], 0, 64*sizeof(uint8_t));
00392 
00393     sbr->bs_add_harmonic_flag[0] = faad_get1bit(ld
00394         DEBUGVAR(1,223,"sbr_single_channel_element(): bs_add_harmonic_flag[0]"));
00395     if (sbr->bs_add_harmonic_flag[0])
00396         sinusoidal_coding(ld, sbr, 0);
00397 
00398     sbr->bs_extended_data = faad_get1bit(ld
00399         DEBUGVAR(1,224,"sbr_single_channel_element(): bs_extended_data[0]"));
00400 
00401     if (sbr->bs_extended_data)
00402     {
00403         uint16_t nr_bits_left;
00404 #if (defined(PS_DEC) || defined(DRM_PS))
00405         uint8_t ps_ext_read = 0;
00406 #endif
00407         uint16_t cnt = (uint16_t)faad_getbits(ld, 4
00408             DEBUGVAR(1,225,"sbr_single_channel_element(): bs_extension_size"));
00409         if (cnt == 15)
00410         {
00411             cnt += (uint16_t)faad_getbits(ld, 8
00412                 DEBUGVAR(1,226,"sbr_single_channel_element(): bs_esc_count"));
00413         }
00414 
00415         nr_bits_left = 8 * cnt;
00416         while (nr_bits_left > 7)
00417         {
00418             uint16_t tmp_nr_bits = 0;
00419 
00420             sbr->bs_extension_id = (uint8_t)faad_getbits(ld, 2
00421                 DEBUGVAR(1,227,"sbr_single_channel_element(): bs_extension_id"));
00422             tmp_nr_bits += 2;
00423 
00424             /* allow only 1 PS extension element per extension data */
00425 #if (defined(PS_DEC) || defined(DRM_PS))
00426 #if (defined(PS_DEC) && defined(DRM_PS))
00427             if (sbr->bs_extension_id == EXTENSION_ID_PS || sbr->bs_extension_id == DRM_PARAMETRIC_STEREO)
00428 #else
00429 #ifdef PS_DEC
00430             if (sbr->bs_extension_id == EXTENSION_ID_PS)
00431 #else
00432 #ifdef DRM_PS
00433             if (sbr->bs_extension_id == DRM_PARAMETRIC_STEREO)
00434 #endif
00435 #endif
00436 #endif
00437             {
00438                 if (ps_ext_read == 0)
00439                 {
00440                     ps_ext_read = 1;
00441                 } else {
00442                     /* to be safe make it 3, will switch to "default"
00443                      * in sbr_extension() */
00444 #ifdef DRM
00445                     return 1;
00446 #else
00447                     sbr->bs_extension_id = 3;
00448 #endif
00449                 }
00450             }
00451 #endif
00452 
00453             tmp_nr_bits += sbr_extension(ld, sbr, sbr->bs_extension_id, nr_bits_left);
00454 
00455             /* check if the data read is bigger than the number of available bits */
00456             if (tmp_nr_bits > nr_bits_left)
00457                 return 1;
00458 
00459             nr_bits_left -= tmp_nr_bits;
00460         }
00461 
00462         /* Corrigendum */
00463         if (nr_bits_left > 0)
00464         {
00465             faad_getbits(ld, nr_bits_left
00466                 DEBUGVAR(1,280,"sbr_single_channel_element(): nr_bits_left"));
00467         }
00468     }
00469 
00470     return 0;
00471 }
00472 
00473 /* table 6 */
00474 static uint8_t sbr_channel_pair_element(bitfile *ld, sbr_info *sbr)
00475 {
00476     uint8_t n, result;
00477 
00478     if (faad_get1bit(ld
00479         DEBUGVAR(1,228,"sbr_single_channel_element(): bs_data_extra")))
00480     {
00481         faad_getbits(ld, 4
00482             DEBUGVAR(1,228,"sbr_channel_pair_element(): bs_reserved_bits_data"));
00483         faad_getbits(ld, 4
00484             DEBUGVAR(1,228,"sbr_channel_pair_element(): bs_reserved_bits_data"));
00485     }
00486 
00487     sbr->bs_coupling = faad_get1bit(ld
00488         DEBUGVAR(1,228,"sbr_channel_pair_element(): bs_coupling"));
00489 
00490     if (sbr->bs_coupling)
00491     {
00492         if ((result = sbr_grid(ld, sbr, 0)) > 0)
00493             return result;
00494 
00495         /* need to copy some data from left to right */
00496         sbr->bs_frame_class[1] = sbr->bs_frame_class[0];
00497         sbr->L_E[1] = sbr->L_E[0];
00498         sbr->L_Q[1] = sbr->L_Q[0];
00499         sbr->bs_pointer[1] = sbr->bs_pointer[0];
00500 
00501         for (n = 0; n <= sbr->L_E[0]; n++)
00502         {
00503             sbr->t_E[1][n] = sbr->t_E[0][n];
00504             sbr->f[1][n] = sbr->f[0][n];
00505         }
00506         for (n = 0; n <= sbr->L_Q[0]; n++)
00507             sbr->t_Q[1][n] = sbr->t_Q[0][n];
00508 
00509         sbr_dtdf(ld, sbr, 0);
00510         sbr_dtdf(ld, sbr, 1);
00511         invf_mode(ld, sbr, 0);
00512 
00513         /* more copying */
00514         for (n = 0; n < sbr->N_Q; n++)
00515             sbr->bs_invf_mode[1][n] = sbr->bs_invf_mode[0][n];
00516 
00517         sbr_envelope(ld, sbr, 0);
00518         sbr_noise(ld, sbr, 0);
00519         sbr_envelope(ld, sbr, 1);
00520         sbr_noise(ld, sbr, 1);
00521 
00522         memset(sbr->bs_add_harmonic[0], 0, 64*sizeof(uint8_t));
00523         memset(sbr->bs_add_harmonic[1], 0, 64*sizeof(uint8_t));
00524 
00525         sbr->bs_add_harmonic_flag[0] = faad_get1bit(ld
00526             DEBUGVAR(1,231,"sbr_channel_pair_element(): bs_add_harmonic_flag[0]"));
00527         if (sbr->bs_add_harmonic_flag[0])
00528             sinusoidal_coding(ld, sbr, 0);
00529 
00530         sbr->bs_add_harmonic_flag[1] = faad_get1bit(ld
00531             DEBUGVAR(1,232,"sbr_channel_pair_element(): bs_add_harmonic_flag[1]"));
00532         if (sbr->bs_add_harmonic_flag[1])
00533             sinusoidal_coding(ld, sbr, 1);
00534     } else {
00535         uint8_t saved_t_E[6] = {0}, saved_t_Q[3] = {0};
00536         uint8_t saved_L_E = sbr->L_E[0];
00537         uint8_t saved_L_Q = sbr->L_Q[0];
00538         uint8_t saved_frame_class = sbr->bs_frame_class[0];
00539 
00540         for (n = 0; n < saved_L_E; n++)
00541             saved_t_E[n] = sbr->t_E[0][n];
00542         for (n = 0; n < saved_L_Q; n++)
00543             saved_t_Q[n] = sbr->t_Q[0][n];
00544 
00545         if ((result = sbr_grid(ld, sbr, 0)) > 0)
00546             return result;
00547         if ((result = sbr_grid(ld, sbr, 1)) > 0)
00548         {
00549             /* restore first channel data as well */
00550             sbr->bs_frame_class[0] = saved_frame_class;
00551             sbr->L_E[0] = saved_L_E;
00552             sbr->L_Q[0] = saved_L_Q;
00553             for (n = 0; n < 6; n++)
00554                 sbr->t_E[0][n] = saved_t_E[n];
00555             for (n = 0; n < 3; n++)
00556                 sbr->t_Q[0][n] = saved_t_Q[n];
00557 
00558             return result;
00559         }
00560         sbr_dtdf(ld, sbr, 0);
00561         sbr_dtdf(ld, sbr, 1);
00562         invf_mode(ld, sbr, 0);
00563         invf_mode(ld, sbr, 1);
00564         sbr_envelope(ld, sbr, 0);
00565         sbr_envelope(ld, sbr, 1);
00566         sbr_noise(ld, sbr, 0);
00567         sbr_noise(ld, sbr, 1);
00568 
00569         memset(sbr->bs_add_harmonic[0], 0, 64*sizeof(uint8_t));
00570         memset(sbr->bs_add_harmonic[1], 0, 64*sizeof(uint8_t));
00571 
00572         sbr->bs_add_harmonic_flag[0] = faad_get1bit(ld
00573             DEBUGVAR(1,239,"sbr_channel_pair_element(): bs_add_harmonic_flag[0]"));
00574         if (sbr->bs_add_harmonic_flag[0])
00575             sinusoidal_coding(ld, sbr, 0);
00576 
00577         sbr->bs_add_harmonic_flag[1] = faad_get1bit(ld
00578             DEBUGVAR(1,240,"sbr_channel_pair_element(): bs_add_harmonic_flag[1]"));
00579         if (sbr->bs_add_harmonic_flag[1])
00580             sinusoidal_coding(ld, sbr, 1);
00581     }
00582 #ifndef FIXED_POINT
00583     envelope_noise_dequantisation(sbr, 0);
00584     envelope_noise_dequantisation(sbr, 1);
00585 
00586     if (sbr->bs_coupling)
00587         unmap_envelope_noise(sbr);
00588 #endif
00589 
00590     sbr->bs_extended_data = faad_get1bit(ld
00591         DEBUGVAR(1,233,"sbr_channel_pair_element(): bs_extended_data[0]"));
00592     if (sbr->bs_extended_data)
00593     {
00594         uint16_t nr_bits_left;
00595         uint16_t cnt = (uint16_t)faad_getbits(ld, 4
00596             DEBUGVAR(1,234,"sbr_channel_pair_element(): bs_extension_size"));
00597         if (cnt == 15)
00598         {
00599             cnt += (uint16_t)faad_getbits(ld, 8
00600                 DEBUGVAR(1,235,"sbr_channel_pair_element(): bs_esc_count"));
00601         }
00602 
00603         nr_bits_left = 8 * cnt;
00604         while (nr_bits_left > 7)
00605         {
00606             uint16_t tmp_nr_bits = 0;
00607 
00608             sbr->bs_extension_id = (uint8_t)faad_getbits(ld, 2
00609                 DEBUGVAR(1,236,"sbr_channel_pair_element(): bs_extension_id"));
00610             tmp_nr_bits += 2;
00611             tmp_nr_bits += sbr_extension(ld, sbr, sbr->bs_extension_id, nr_bits_left);
00612 
00613             /* check if the data read is bigger than the number of available bits */
00614             if (tmp_nr_bits > nr_bits_left)
00615                 return 1;
00616 
00617             nr_bits_left -= tmp_nr_bits;
00618         }
00619 
00620         /* Corrigendum */
00621         if (nr_bits_left > 0)
00622         {
00623             faad_getbits(ld, nr_bits_left
00624                 DEBUGVAR(1,280,"sbr_channel_pair_element(): nr_bits_left"));
00625         }
00626     }
00627 
00628     return 0;
00629 }
00630 
00631 /* integer log[2](x): input range [0,10) */
00632 static int8_t sbr_log2(const int8_t val)
00633 {
00634     int8_t log2tab[] = { 0, 0, 1, 2, 2, 3, 3, 3, 3, 4 };
00635     if (val < 10 && val >= 0)
00636         return log2tab[val];
00637     else
00638         return 0;
00639 }
00640 
00641 
00642 /* table 7 */
00643 static uint8_t sbr_grid(bitfile *ld, sbr_info *sbr, uint8_t ch)
00644 {
00645     uint8_t i, env, rel, result;
00646     uint8_t bs_abs_bord, bs_abs_bord_1;
00647     uint8_t bs_num_env = 0;
00648     uint8_t saved_L_E = sbr->L_E[ch];
00649     uint8_t saved_L_Q = sbr->L_Q[ch];
00650     uint8_t saved_frame_class = sbr->bs_frame_class[ch];
00651 
00652     sbr->bs_frame_class[ch] = (uint8_t)faad_getbits(ld, 2
00653         DEBUGVAR(1,248,"sbr_grid(): bs_frame_class"));
00654 
00655     switch (sbr->bs_frame_class[ch])
00656     {
00657     case FIXFIX:
00658         i = (uint8_t)faad_getbits(ld, 2
00659             DEBUGVAR(1,249,"sbr_grid(): bs_num_env_raw"));
00660 
00661         bs_num_env = min(1 << i, 5);
00662 
00663         i = (uint8_t)faad_get1bit(ld
00664             DEBUGVAR(1,250,"sbr_grid(): bs_freq_res_flag"));
00665         for (env = 0; env < bs_num_env; env++)
00666             sbr->f[ch][env] = i;
00667 
00668         sbr->abs_bord_lead[ch] = 0;
00669         sbr->abs_bord_trail[ch] = sbr->numTimeSlots;
00670         sbr->n_rel_lead[ch] = bs_num_env - 1;
00671         sbr->n_rel_trail[ch] = 0;
00672         break;
00673 
00674     case FIXVAR:
00675         bs_abs_bord = (uint8_t)faad_getbits(ld, 2
00676             DEBUGVAR(1,251,"sbr_grid(): bs_abs_bord")) + sbr->numTimeSlots;
00677         bs_num_env = (uint8_t)faad_getbits(ld, 2
00678             DEBUGVAR(1,252,"sbr_grid(): bs_num_env")) + 1;
00679 
00680         for (rel = 0; rel < bs_num_env-1; rel++)
00681         {
00682             sbr->bs_rel_bord[ch][rel] = 2 * (uint8_t)faad_getbits(ld, 2
00683                 DEBUGVAR(1,253,"sbr_grid(): bs_rel_bord")) + 2;
00684         }
00685         i = sbr_log2(bs_num_env + 1);
00686         sbr->bs_pointer[ch] = (uint8_t)faad_getbits(ld, i
00687             DEBUGVAR(1,254,"sbr_grid(): bs_pointer"));
00688 
00689         for (env = 0; env < bs_num_env; env++)
00690         {
00691             sbr->f[ch][bs_num_env - env - 1] = (uint8_t)faad_get1bit(ld
00692                 DEBUGVAR(1,255,"sbr_grid(): bs_freq_res"));
00693         }
00694 
00695         sbr->abs_bord_lead[ch] = 0;
00696         sbr->abs_bord_trail[ch] = bs_abs_bord;
00697         sbr->n_rel_lead[ch] = 0;
00698         sbr->n_rel_trail[ch] = bs_num_env - 1;
00699         break;
00700 
00701     case VARFIX:
00702         bs_abs_bord = (uint8_t)faad_getbits(ld, 2
00703             DEBUGVAR(1,256,"sbr_grid(): bs_abs_bord"));
00704         bs_num_env = (uint8_t)faad_getbits(ld, 2
00705             DEBUGVAR(1,257,"sbr_grid(): bs_num_env")) + 1;
00706 
00707         for (rel = 0; rel < bs_num_env-1; rel++)
00708         {
00709             sbr->bs_rel_bord[ch][rel] = 2 * (uint8_t)faad_getbits(ld, 2
00710                 DEBUGVAR(1,258,"sbr_grid(): bs_rel_bord")) + 2;
00711         }
00712         i = sbr_log2(bs_num_env + 1);
00713         sbr->bs_pointer[ch] = (uint8_t)faad_getbits(ld, i
00714             DEBUGVAR(1,259,"sbr_grid(): bs_pointer"));
00715 
00716         for (env = 0; env < bs_num_env; env++)
00717         {
00718             sbr->f[ch][env] = (uint8_t)faad_get1bit(ld
00719                 DEBUGVAR(1,260,"sbr_grid(): bs_freq_res"));
00720         }
00721 
00722         sbr->abs_bord_lead[ch] = bs_abs_bord;
00723         sbr->abs_bord_trail[ch] = sbr->numTimeSlots;
00724         sbr->n_rel_lead[ch] = bs_num_env - 1;
00725         sbr->n_rel_trail[ch] = 0;
00726         break;
00727 
00728     case VARVAR:
00729         bs_abs_bord = (uint8_t)faad_getbits(ld, 2
00730             DEBUGVAR(1,261,"sbr_grid(): bs_abs_bord_0"));
00731         bs_abs_bord_1 = (uint8_t)faad_getbits(ld, 2
00732             DEBUGVAR(1,262,"sbr_grid(): bs_abs_bord_1")) + sbr->numTimeSlots;
00733         sbr->bs_num_rel_0[ch] = (uint8_t)faad_getbits(ld, 2
00734             DEBUGVAR(1,263,"sbr_grid(): bs_num_rel_0"));
00735         sbr->bs_num_rel_1[ch] = (uint8_t)faad_getbits(ld, 2
00736             DEBUGVAR(1,264,"sbr_grid(): bs_num_rel_1"));
00737 
00738         bs_num_env = min(5, sbr->bs_num_rel_0[ch] + sbr->bs_num_rel_1[ch] + 1);
00739 
00740         for (rel = 0; rel < sbr->bs_num_rel_0[ch]; rel++)
00741         {
00742             sbr->bs_rel_bord_0[ch][rel] = 2 * (uint8_t)faad_getbits(ld, 2
00743                 DEBUGVAR(1,265,"sbr_grid(): bs_rel_bord")) + 2;
00744         }
00745         for(rel = 0; rel < sbr->bs_num_rel_1[ch]; rel++)
00746         {
00747             sbr->bs_rel_bord_1[ch][rel] = 2 * (uint8_t)faad_getbits(ld, 2
00748                 DEBUGVAR(1,266,"sbr_grid(): bs_rel_bord")) + 2;
00749         }
00750         i = sbr_log2(sbr->bs_num_rel_0[ch] + sbr->bs_num_rel_1[ch] + 2);
00751         sbr->bs_pointer[ch] = (uint8_t)faad_getbits(ld, i
00752             DEBUGVAR(1,267,"sbr_grid(): bs_pointer"));
00753 
00754         for (env = 0; env < bs_num_env; env++)
00755         {
00756             sbr->f[ch][env] = (uint8_t)faad_get1bit(ld
00757                 DEBUGVAR(1,268,"sbr_grid(): bs_freq_res"));
00758         }
00759 
00760         sbr->abs_bord_lead[ch] = bs_abs_bord;
00761         sbr->abs_bord_trail[ch] = bs_abs_bord_1;
00762         sbr->n_rel_lead[ch] = sbr->bs_num_rel_0[ch];
00763         sbr->n_rel_trail[ch] = sbr->bs_num_rel_1[ch];
00764         break;
00765     }
00766 
00767     if (sbr->bs_frame_class[ch] == VARVAR)
00768         sbr->L_E[ch] = min(bs_num_env, 5);
00769     else
00770         sbr->L_E[ch] = min(bs_num_env, 4);
00771 
00772     if (sbr->L_E[ch] <= 0)
00773         return 1;
00774 
00775     if (sbr->L_E[ch] > 1)
00776         sbr->L_Q[ch] = 2;
00777     else
00778         sbr->L_Q[ch] = 1;
00779 
00780     /* TODO: this code can probably be integrated into the code above! */
00781     if ((result = envelope_time_border_vector(sbr, ch)) > 0)
00782     {
00783         sbr->bs_frame_class[ch] = saved_frame_class;
00784         sbr->L_E[ch] = saved_L_E;
00785         sbr->L_Q[ch] = saved_L_Q;
00786         return result;
00787     }
00788     noise_floor_time_border_vector(sbr, ch);
00789 
00790 #if 0
00791     for (env = 0; env < bs_num_env; env++)
00792     {
00793         printf("freq_res[ch:%d][env:%d]: %d\n", ch, env, sbr->f[ch][env]);
00794     }
00795 #endif
00796 
00797     return 0;
00798 }
00799 
00800 /* table 8 */
00801 static void sbr_dtdf(bitfile *ld, sbr_info *sbr, uint8_t ch)
00802 {
00803     uint8_t i;
00804 
00805     for (i = 0; i < sbr->L_E[ch]; i++)
00806     {
00807         sbr->bs_df_env[ch][i] = faad_get1bit(ld
00808             DEBUGVAR(1,269,"sbr_dtdf(): bs_df_env"));
00809     }
00810 
00811     for (i = 0; i < sbr->L_Q[ch]; i++)
00812     {
00813         sbr->bs_df_noise[ch][i] = faad_get1bit(ld
00814             DEBUGVAR(1,270,"sbr_dtdf(): bs_df_noise"));
00815     }
00816 }
00817 
00818 /* table 9 */
00819 static void invf_mode(bitfile *ld, sbr_info *sbr, uint8_t ch)
00820 {
00821     uint8_t n;
00822 
00823     for (n = 0; n < sbr->N_Q; n++)
00824     {
00825         sbr->bs_invf_mode[ch][n] = (uint8_t)faad_getbits(ld, 2
00826             DEBUGVAR(1,271,"invf_mode(): bs_invf_mode"));
00827     }
00828 }
00829 
00830 static uint16_t sbr_extension(bitfile *ld, sbr_info *sbr,
00831                               uint8_t bs_extension_id, uint16_t num_bits_left)
00832 {
00833 #ifdef PS_DEC
00834     uint8_t header;
00835     uint16_t ret;
00836 #endif
00837 
00838     switch (bs_extension_id)
00839     {
00840 #ifdef PS_DEC
00841     case EXTENSION_ID_PS:
00842         if (!sbr->ps)
00843         {
00844             sbr->ps = ps_init(get_sr_index(sbr->sample_rate));
00845         }
00846         ret = ps_data(sbr->ps, ld, &header);
00847 
00848         /* enable PS if and only if: a header has been decoded */
00849         if (sbr->ps_used == 0 && header == 1)
00850         {
00851             sbr->ps_used = 1;
00852         }
00853 
00854         return ret;
00855 #endif
00856 #ifdef DRM_PS
00857     case DRM_PARAMETRIC_STEREO:
00858         sbr->ps_used = 1;
00859         if (!sbr->drm_ps)
00860         {
00861             sbr->drm_ps = drm_ps_init();
00862         }
00863         return drm_ps_data(sbr->drm_ps, ld);
00864 #endif
00865     default:
00866         sbr->bs_extension_data = (uint8_t)faad_getbits(ld, 6
00867             DEBUGVAR(1,279,"sbr_single_channel_element(): bs_extension_data"));
00868         return 6;
00869     }
00870 }
00871 
00872 /* table 12 */
00873 static void sinusoidal_coding(bitfile *ld, sbr_info *sbr, uint8_t ch)
00874 {
00875     uint8_t n;
00876 
00877     for (n = 0; n < sbr->N_high; n++)
00878     {
00879         sbr->bs_add_harmonic[ch][n] = faad_get1bit(ld
00880             DEBUGVAR(1,278,"sinusoidal_coding(): bs_add_harmonic"));
00881     }
00882 }
00883 
00884 
00885 #endif /* SBR_DEC */

Generated on Tue Dec 13 14:47:46 2005 for guliverkli by  doxygen 1.4.5