Exemple #1
0
/* 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;
}
Exemple #2
0
/* 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;
}