sbc: joint stereo in progress

This commit is contained in:
Milanka Ringwald 2016-05-18 23:00:51 +02:00
parent 9462b3da58
commit 57f2bc22c3
3 changed files with 23 additions and 21 deletions

View File

@ -127,7 +127,7 @@ offset8 = np.array([[ -2, 0, 0, 0, 0, 0, 0, 1 ],
def calculate_scalefactor(max_subbandsample):
x = 0
while True:
y = 1 << x + 1
y = (1 << x) + 1
if y > max_subbandsample:
break
x += 1
@ -157,7 +157,7 @@ def calculate_scalefactors(nr_blocks, nr_channels, nr_subbands, sb_sample):
def calculate_channel_mode_and_scale_factors(frame):
frame.scale_factor, frame.scalefactor = calculate_scalefactors(frame.nr_blocks, frame.nr_channels, frame.nr_subbands, frame.sb_sample)
if frame.nr_channels == 1:
frame.channel_mode = MONO
return
@ -165,20 +165,19 @@ def calculate_channel_mode_and_scale_factors(frame):
frame.channel_mode = STEREO
frame.join = np.zeros(frame.nr_subbands, dtype = np.uint8)
return
sb_sample = np.zeros(shape = (frame.nr_blocks,2,frame.nr_subbands), dtype = np.uint16)
sb_sample = np.zeros(shape = (frame.nr_blocks,2,frame.nr_subbands), dtype = np.int32)
for blk in range(frame.nr_blocks):
for sb in range(frame.nr_subbands):
sb_sample[blk][0][sb] = np.uint16(frame.sb_sample[blk][0][sb] + frame.sb_sample[blk][1][sb])/2
sb_sample[blk][1][sb] = np.uint16(frame.sb_sample[blk][0][sb] - frame.sb_sample[blk][1][sb])/2
sb_sample[blk][0][sb] = (frame.sb_sample[blk][0][sb] + frame.sb_sample[blk][1][sb])/2
sb_sample[blk][1][sb] = (frame.sb_sample[blk][0][sb] - frame.sb_sample[blk][1][sb])/2
scale_factor, scalefactor = calculate_scalefactors(frame.nr_blocks, frame.nr_channels, frame.nr_subbands, sb_sample)
for sb in range(frame.nr_subbands):
for sb in range(frame.nr_subbands-1):
suma = frame.scale_factor[0][sb] + frame.scale_factor[1][sb]
sumb = scale_factor[0][sb] + scale_factor[1][sb]
if suma > sumb:
if suma >= sumb:
frame.channel_mode = JOINT_STEREO
frame.join[sb] = 1
@ -190,7 +189,7 @@ def calculate_channel_mode_and_scale_factors(frame):
for blk in range(frame.nr_blocks):
frame.sb_sample[blk][0][sb] = sb_sample[blk][0][sb]
frame.sb_sample[blk][1][sb] = sb_sample[blk][1][sb]
class SBCFrame:
syncword = 0
@ -399,7 +398,6 @@ def sbc_bit_allocation_mono_dual(frame):
if bitneed[ch][sb] > max_bitneed:
max_bitneed = bitneed[ch][sb]
print "mono: bitneed", bitneed, max_bitneed
# calculate how many bitslices fit into the bitpool
bitcount = 0
slicecount = 0
@ -421,7 +419,6 @@ def sbc_bit_allocation_mono_dual(frame):
bitcount = bitcount + slicecount
bitslice = bitslice - 1
print "mono: bitslice", bitslice
for sb in range(frame.nr_subbands):
if bitneed[ch][sb] < bitslice+2 :
bits[ch][sb]=0;
@ -440,7 +437,6 @@ def sbc_bit_allocation_mono_dual(frame):
sb = sb + 1
sb = 0
while bitcount < frame.bitpool and sb < frame.nr_subbands:
if bits[ch][sb] < 16:

View File

@ -78,15 +78,16 @@ def sbc_reconstruct_subband_samples(frame):
for sb in range(frame.nr_subbands):
frame.levels[ch][sb] = pow(2.0, frame.bits[ch][sb]) - 1
joint_stereo_const = 1
for blk in range(frame.nr_blocks):
for ch in range(frame.nr_channels):
for sb in range(frame.nr_subbands):
if frame.levels[ch][sb] > 0:
AS = frame.audio_sample[blk][ch][sb]
SF = frame.scalefactor[ch][sb]
L = frame.levels[ch][sb]
frame.sb_sample[blk][ch][sb] = SF * ((AS*2.0+1.0) / L -1.0 )
SF = frame.scalefactor[ch][sb]
frame.sb_sample[blk][ch][sb] = SF * ((AS*2.0+1.0) / (L) -1.0 )
else:
frame.sb_sample[blk][ch][sb] = 0

View File

@ -89,17 +89,22 @@ def sbc_quantization(frame):
for sb in range(frame.nr_subbands):
frame.levels[ch][sb] = (1 << frame.bits[ch][sb]) - 1 #pow(2.0, frame.bits[ch][sb]) - 1
frame.syncword = 156
frame.syncword = 0x9c
frame.crc_check = calculate_crc(frame)
for blk in range(frame.nr_blocks):
for ch in range(frame.nr_channels):
for sb in range(frame.nr_subbands):
if frame.levels[ch][sb] > 0:
SB = frame.sb_sample[blk][ch][sb]
L = frame.levels[ch][sb]
SF = frame.scalefactor[ch][sb]
frame.audio_sample[blk][ch][sb] = np.uint16(((SB * L / SF + L) - 1.0)/2.0)
L = frame.levels[ch][sb]
SF = frame.scalefactor[ch][sb]
# if frame.channel_mode == JOINT_STEREO and frame.join[sb]:
# SB = SB * 2
frame.audio_sample[blk][ch][sb] = np.uint16(((SB * L / SF + L) - 1.0)/2.0)
else:
frame.audio_sample[blk][ch][sb] = 0
@ -153,8 +158,8 @@ if __name__ == "__main__":
sbc_encode(sbc_encoder_frame)
sbc_write_frame(fout, sbc_encoder_frame)
if subband_frame_count == 1:
exit(0)
# if subband_frame_count == 1:
# exit(0)
audio_frame_count += nr_samples
subband_frame_count += 1