src/main/java/net/sourceforge/jaad/aac/sbr/SBR.java
private int sbr_grid(IBitStream ld, int ch) throws AACException {
		int i, env, rel, result;
		int bs_abs_bord, bs_abs_bord_1;
		int bs_num_env = 0;
		int saved_L_E = this.L_E[ch];
		int saved_L_Q = this.L_Q[ch];
		int saved_frame_class = this.bs_frame_class[ch];

		this.bs_frame_class[ch] = ld.readBits(2);

		switch(this.bs_frame_class[ch]) {
			case FIXFIX:
				i = ld.readBits(2);

				bs_num_env = Math.min(1<<i, 5);

				i = ld.readBit();
				for(env = 0; env<bs_num_env; env++) {
					this.f[ch][env] = i;
				}

				this.abs_bord_lead[ch] = 0;
				this.abs_bord_trail[ch] = this.numTimeSlots;
				this.n_rel_lead[ch] = bs_num_env-1;
				this.n_rel_trail[ch] = 0;
				break;

			case FIXVAR:
				bs_abs_bord = ld.readBits(2)+this.numTimeSlots;
				bs_num_env = ld.readBits(2)+1;

				for(rel = 0; rel<bs_num_env-1; rel++) {
					this.bs_rel_bord[ch][rel] = 2*ld.readBits(2)+2;
				}
				i = sbr_log2(bs_num_env+1);
				this.bs_pointer[ch] = ld.readBits(i);

				for(env = 0; env<bs_num_env; env++) {
					this.f[ch][bs_num_env-env-1] = ld.readBit();
				}

				this.abs_bord_lead[ch] = 0;
				this.abs_bord_trail[ch] = bs_abs_bord;
				this.n_rel_lead[ch] = 0;
				this.n_rel_trail[ch] = bs_num_env-1;
				break;

			case VARFIX:
				bs_abs_bord = ld.readBits(2);
				bs_num_env = ld.readBits(2)+1;

				for(rel = 0; rel<bs_num_env-1; rel++) {
					this.bs_rel_bord[ch][rel] = 2*ld.readBits(2)+2;
				}
				i = sbr_log2(bs_num_env+1);
				this.bs_pointer[ch] = ld.readBits(i);

				for(env = 0; env<bs_num_env; env++) {
					this.f[ch][env] = ld.readBit();
				}

				this.abs_bord_lead[ch] = bs_abs_bord;
				this.abs_bord_trail[ch] = this.numTimeSlots;
				this.n_rel_lead[ch] = bs_num_env-1;
				this.n_rel_trail[ch] = 0;
				break;

			case VARVAR:
				bs_abs_bord = ld.readBits(2);
				bs_abs_bord_1 = ld.readBits(2)+this.numTimeSlots;
				this.bs_num_rel_0[ch] = ld.readBits(2);
				this.bs_num_rel_1[ch] = ld.readBits(2);

				bs_num_env = Math.min(5, this.bs_num_rel_0[ch]+this.bs_num_rel_1[ch]+1);

				for(rel = 0; rel<this.bs_num_rel_0[ch]; rel++) {
					this.bs_rel_bord_0[ch][rel] = 2*ld.readBits(2)+2;
				}
				for(rel = 0; rel<this.bs_num_rel_1[ch]; rel++) {
					this.bs_rel_bord_1[ch][rel] = 2*ld.readBits(2)+2;
				}
				i = sbr_log2(this.bs_num_rel_0[ch]+this.bs_num_rel_1[ch]+2);
				this.bs_pointer[ch] = ld.readBits(i);

				for(env = 0; env<bs_num_env; env++) {
					this.f[ch][env] = ld.readBit();
				}

				this.abs_bord_lead[ch] = bs_abs_bord;
				this.abs_bord_trail[ch] = bs_abs_bord_1;
				this.n_rel_lead[ch] = this.bs_num_rel_0[ch];
				this.n_rel_trail[ch] = this.bs_num_rel_1[ch];
				break;
		}

		if(this.bs_frame_class[ch]==VARVAR)
			this.L_E[ch] = Math.min(bs_num_env, 5);
		else
			this.L_E[ch] = Math.min(bs_num_env, 4);

		if(this.L_E[ch]<=0)
			return 1;

		if(this.L_E[ch]>1)
			this.L_Q[ch] = 2;
		else
			this.L_Q[ch] = 1;

		/* TODO: this code can probably be integrated into the code above! */
		if((result = TFGrid.envelope_time_border_vector(this, ch))>0) {
			this.bs_frame_class[ch] = saved_frame_class;
			this.L_E[ch] = saved_L_E;
			this.L_Q[ch] = saved_L_Q;
			return result;
		}
		TFGrid.noise_floor_time_border_vector(this, ch);

		return 0;
	}
private int sbr_grid(IBitStream ld, int ch) throws AACException {
		int i, env, rel, result;
		int bs_abs_bord, bs_abs_bord_1;
		int bs_num_env = 0;
		int saved_L_E = this.L_E[ch];
		int saved_L_Q = this.L_Q[ch];
		int saved_frame_class = this.bs_frame_class[ch];

		this.bs_frame_class[ch] = ld.readBits(2);

		switch(this.bs_frame_class[ch]) {
			case FIXFIX:
				i = ld.readBits(2);

				bs_num_env = Math.min(1<<i, 5);

				i = ld.readBit();
				for(env = 0; env<bs_num_env; env++) {
					this.f[ch][env] = i;
				}

				this.abs_bord_lead[ch] = 0;
				this.abs_bord_trail[ch] = this.numTimeSlots;
				this.n_rel_lead[ch] = bs_num_env-1;
				this.n_rel_trail[ch] = 0;
				break;

			case FIXVAR:
				bs_abs_bord = ld.readBits(2)+this.numTimeSlots;
				bs_num_env = ld.readBits(2)+1;

				for(rel = 0; rel<bs_num_env-1; rel++) {
					this.bs_rel_bord[ch][rel] = 2*ld.readBits(2)+2;
				}
				i = sbr_log2(bs_num_env+1);
				this.bs_pointer[ch] = ld.readBits(i);

				for(env = 0; env<bs_num_env; env++) {
					this.f[ch][bs_num_env-env-1] = ld.readBit();
				}

				this.abs_bord_lead[ch] = 0;
				this.abs_bord_trail[ch] = bs_abs_bord;
				this.n_rel_lead[ch] = 0;
				this.n_rel_trail[ch] = bs_num_env-1;
				break;

			case VARFIX:
				bs_abs_bord = ld.readBits(2);
				bs_num_env = ld.readBits(2)+1;

				for(rel = 0; rel<bs_num_env-1; rel++) {
					this.bs_rel_bord[ch][rel] = 2*ld.readBits(2)+2;
				}
				i = sbr_log2(bs_num_env+1);
				this.bs_pointer[ch] = ld.readBits(i);

				for(env = 0; env<bs_num_env; env++) {
					this.f[ch][env] = ld.readBit();
				}

				this.abs_bord_lead[ch] = bs_abs_bord;
				this.abs_bord_trail[ch] = this.numTimeSlots;
				this.n_rel_lead[ch] = bs_num_env-1;
				this.n_rel_trail[ch] = 0;
				break;

			case VARVAR:
				bs_abs_bord = ld.readBits(2);
				bs_abs_bord_1 = ld.readBits(2)+this.numTimeSlots;
				this.bs_num_rel_0[ch] = ld.readBits(2);
				this.bs_num_rel_1[ch] = ld.readBits(2);

				bs_num_env = Math.min(5, this.bs_num_rel_0[ch]+this.bs_num_rel_1[ch]+1);

				for(rel = 0; rel<this.bs_num_rel_0[ch]; rel++) {
					this.bs_rel_bord_0[ch][rel] = 2*ld.readBits(2)+2;
				}
				for(rel = 0; rel<this.bs_num_rel_1[ch]; rel++) {
					this.bs_rel_bord_1[ch][rel] = 2*ld.readBits(2)+2;
				}
				i = sbr_log2(this.bs_num_rel_0[ch]+this.bs_num_rel_1[ch]+2);
				this.bs_pointer[ch] = ld.readBits(i);

				for(env = 0; env<bs_num_env; env++) {
					this.f[ch][env] = ld.readBit();
				}

				this.abs_bord_lead[ch] = bs_abs_bord;
				this.abs_bord_trail[ch] = bs_abs_bord_1;
				this.n_rel_lead[ch] = this.bs_num_rel_0[ch];
				this.n_rel_trail[ch] = this.bs_num_rel_1[ch];
				break;
		}

		if(this.bs_frame_class[ch]==VARVAR)
			this.L_E[ch] = Math.min(bs_num_env, 5);
		else
			this.L_E[ch] = Math.min(bs_num_env, 4);

		if(this.L_E[ch]<=0)
			return 1;

		if(this.L_E[ch]>1)
			this.L_Q[ch] = 2;
		else
			this.L_Q[ch] = 1;

		/* TODO: this code can probably be integrated into the code above! */
		if((result = TFGrid.envelope_time_border_vector(this, ch))>0) {
			this.bs_frame_class[ch] = saved_frame_class;
			this.L_E[ch] = saved_L_E;
			this.L_Q[ch] = saved_L_Q;
			return result;
		}
		TFGrid.noise_floor_time_border_vector(this, ch);

		return 0;
	}
/* table 7