src/main/java/net/sourceforge/jaad/aac/sbr/SBR.java
public int decode(IBitStream ld, int cnt) throws AACException {
		int result = 0;
		int num_align_bits = 0;
		long num_sbr_bits1 = ld.getPosition();
		int num_sbr_bits2;

		int saved_start_freq, saved_samplerate_mode;
		int saved_stop_freq, saved_freq_scale;
		int saved_xover_band;
		boolean saved_alter_scale;

		int bs_extension_type = ld.readBits(4);

		if(bs_extension_type==EXT_SBR_DATA_CRC) {
			this.bs_sbr_crc_bits = ld.readBits(10);
		}

		/* save old header values, in case the new ones are corrupted */
		saved_start_freq = this.bs_start_freq;
		saved_samplerate_mode = this.bs_samplerate_mode;
		saved_stop_freq = this.bs_stop_freq;
		saved_freq_scale = this.bs_freq_scale;
		saved_alter_scale = this.bs_alter_scale;
		saved_xover_band = this.bs_xover_band;

		this.bs_header_flag = ld.readBool();

		if(this.bs_header_flag)
			sbr_header(ld);

		/* Reset? */
		sbr_reset();

		/* first frame should have a header */
		//if (!(sbr.frame == 0 && sbr.bs_header_flag == 0))
		if(this.header_count!=0) {
			if(this.Reset||(this.bs_header_flag&&this.just_seeked)) {
				int rt = calc_sbr_tables(this.bs_start_freq, this.bs_stop_freq,
					this.bs_samplerate_mode, this.bs_freq_scale,
					this.bs_alter_scale, this.bs_xover_band);

				/* if an error occured with the new header values revert to the old ones */
				if(rt>0) {
					calc_sbr_tables(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_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)
					&&(this.Reset||(this.bs_header_flag&&this.just_seeked))) {
					calc_sbr_tables(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 = (int) (ld.getPosition()-num_sbr_bits1);

		/* check if we read more bits then were available for sbr */
		if(8*cnt<num_sbr_bits2) {
			throw new AACException("frame overread");
			//faad_resetbits(ld, num_sbr_bits1+8*cnt);
			//num_sbr_bits2 = 8*cnt;

			/* turn off PS for the unfortunate case that we randomly read some
			 * PS data that looks correct */
			//this.ps_used = 0;

			/* Make sure it doesn't decode SBR in this frame, or we'll get glitches */
			//return 1;
		}

		{
			/* -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) {
				ld.readBits(8);
				num_align_bits -= 8;
			}
			ld.readBits(num_align_bits);
		}

		return result;
	}
public int decode(IBitStream ld, int cnt) throws AACException {
		int result = 0;
		int num_align_bits = 0;
		long num_sbr_bits1 = ld.getPosition();
		int num_sbr_bits2;

		int saved_start_freq, saved_samplerate_mode;
		int saved_stop_freq, saved_freq_scale;
		int saved_xover_band;
		boolean saved_alter_scale;

		int bs_extension_type = ld.readBits(4);

		if(bs_extension_type==EXT_SBR_DATA_CRC) {
			this.bs_sbr_crc_bits = ld.readBits(10);
		}

		/* save old header values, in case the new ones are corrupted */
		saved_start_freq = this.bs_start_freq;
		saved_samplerate_mode = this.bs_samplerate_mode;
		saved_stop_freq = this.bs_stop_freq;
		saved_freq_scale = this.bs_freq_scale;
		saved_alter_scale = this.bs_alter_scale;
		saved_xover_band = this.bs_xover_band;

		this.bs_header_flag = ld.readBool();

		if(this.bs_header_flag)
			sbr_header(ld);

		/* Reset? */
		sbr_reset();

		/* first frame should have a header */
		//if (!(sbr.frame == 0 && sbr.bs_header_flag == 0))
		if(this.header_count!=0) {
			if(this.Reset||(this.bs_header_flag&&this.just_seeked)) {
				int rt = calc_sbr_tables(this.bs_start_freq, this.bs_stop_freq,
					this.bs_samplerate_mode, this.bs_freq_scale,
					this.bs_alter_scale, this.bs_xover_band);

				/* if an error occured with the new header values revert to the old ones */
				if(rt>0) {
					calc_sbr_tables(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_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)
					&&(this.Reset||(this.bs_header_flag&&this.just_seeked))) {
					calc_sbr_tables(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 = (int) (ld.getPosition()-num_sbr_bits1);

		/* check if we read more bits then were available for sbr */
		if(8*cnt<num_sbr_bits2) {
			throw new AACException("frame overread");
			//faad_resetbits(ld, num_sbr_bits1+8*cnt);
			//num_sbr_bits2 = 8*cnt;

			/* turn off PS for the unfortunate case that we randomly read some
			 * PS data that looks correct */
			//this.ps_used = 0;

			/* Make sure it doesn't decode SBR in this frame, or we'll get glitches */
			//return 1;
		}

		{
			/* -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) {
				ld.readBits(8);
				num_align_bits -= 8;
			}
			ld.readBits(num_align_bits);
		}

		return result;
	}
/* table 2