mirror of
https://github.com/bluekitchen/btstack.git
synced 2025-04-25 09:02:30 +00:00
sbc: fix bit allocation for stereo
This commit is contained in:
parent
1522543de8
commit
f08a674b88
@ -338,13 +338,28 @@ def sbc_bit_allocation_stereo_joint(frame):
|
|||||||
elif (bitneed[ch][sb] == bitslice+1) and (frame.bitpool > bitcount+1):
|
elif (bitneed[ch][sb] == bitslice+1) and (frame.bitpool > bitcount+1):
|
||||||
bits[ch][sb] = 2
|
bits[ch][sb] = 2
|
||||||
bitcount += 2
|
bitcount += 2
|
||||||
|
|
||||||
if ch == 1:
|
if ch == 1:
|
||||||
ch = 0
|
ch = 0
|
||||||
sb += 1
|
sb += 1
|
||||||
else:
|
else:
|
||||||
ch = 1
|
ch = 1
|
||||||
|
|
||||||
|
|
||||||
|
ch = 0
|
||||||
|
sb = 0
|
||||||
|
while bitcount < frame.bitpool and sb < frame.nr_subbands:
|
||||||
|
if bits[ch][sb] < 16:
|
||||||
|
bits[ch][sb]+=1
|
||||||
|
bitcount+=1
|
||||||
|
if ch == 1:
|
||||||
|
ch = 0
|
||||||
|
sb += 1
|
||||||
|
else:
|
||||||
|
ch = 1
|
||||||
|
|
||||||
|
if bits.sum() != frame.bitpool:
|
||||||
|
print "bit allocation failed, bitpool %d, allocated %d" % (bits.sum() , frame.bitpool)
|
||||||
|
exit(1)
|
||||||
return bits
|
return bits
|
||||||
|
|
||||||
|
|
||||||
@ -507,12 +522,14 @@ def get_bit(fin):
|
|||||||
|
|
||||||
def drop_remaining_bits():
|
def drop_remaining_bits():
|
||||||
global ibuffer_count
|
global ibuffer_count
|
||||||
|
#print "dropping %d bits" % ibuffer_count
|
||||||
ibuffer_count = 0
|
ibuffer_count = 0
|
||||||
|
|
||||||
def get_bits(fin, bit_count):
|
def get_bits(fin, bit_count):
|
||||||
bits = 0
|
bits = 0
|
||||||
for i in range(bit_count):
|
for i in range(bit_count):
|
||||||
bits = (bits << 1) | get_bit(fin)
|
bits = (bits << 1) | get_bit(fin)
|
||||||
|
# print "get bits: %d -> %02x" %(bit_count, bits)
|
||||||
return bits
|
return bits
|
||||||
|
|
||||||
|
|
||||||
|
@ -9,11 +9,20 @@ V = np.zeros(shape = (2, 10*2*8))
|
|||||||
|
|
||||||
def sbc_unpack_frame(fin, available_bytes, frame):
|
def sbc_unpack_frame(fin, available_bytes, frame):
|
||||||
if available_bytes == 0:
|
if available_bytes == 0:
|
||||||
|
print "no available_bytes"
|
||||||
raise TypeError
|
raise TypeError
|
||||||
|
|
||||||
frame.syncword = get_bits(fin,8)
|
frame.syncword = get_bits(fin,8)
|
||||||
if frame.syncword != 156:
|
if frame.syncword != 156:
|
||||||
print "incorrect syncword ", frame.syncword
|
# i = 0
|
||||||
|
# while available_bytes:
|
||||||
|
# if i%10 == 0:
|
||||||
|
# print
|
||||||
|
# bt = get_bits(fin,8)
|
||||||
|
# print "0x%0x "% bt,
|
||||||
|
# available_bytes -= 1
|
||||||
|
# i+=1
|
||||||
|
print ("out of sync %02x" % frame.syncword)
|
||||||
return -1
|
return -1
|
||||||
frame.sampling_frequency = get_bits(fin,2)
|
frame.sampling_frequency = get_bits(fin,2)
|
||||||
frame.nr_blocks = nr_blocks[get_bits(fin,2)]
|
frame.nr_blocks = nr_blocks[get_bits(fin,2)]
|
||||||
@ -64,7 +73,7 @@ def sbc_unpack_frame(fin, available_bytes, frame):
|
|||||||
for ch in range(frame.nr_channels):
|
for ch in range(frame.nr_channels):
|
||||||
for sb in range(frame.nr_subbands):
|
for sb in range(frame.nr_subbands):
|
||||||
frame.audio_sample[blk][ch][sb] = get_bits(fin, frame.bits[ch][sb])
|
frame.audio_sample[blk][ch][sb] = get_bits(fin, frame.bits[ch][sb])
|
||||||
# print "block %2d - audio sample: %s" % (blk, frame.audio_sample[blk][0])
|
#print "block %2d - audio sample: %s" % (blk, frame.audio_sample[blk][0])
|
||||||
|
|
||||||
drop_remaining_bits()
|
drop_remaining_bits()
|
||||||
return 0
|
return 0
|
||||||
@ -210,7 +219,8 @@ if __name__ == "__main__":
|
|||||||
while True:
|
while True:
|
||||||
sbc_decoder_frame = SBCFrame()
|
sbc_decoder_frame = SBCFrame()
|
||||||
if frame_count % 200 == 0:
|
if frame_count % 200 == 0:
|
||||||
print "== Frame %d ==" % (frame_count)
|
print "== Frame %d == %d" % (frame_count, fin.tell())
|
||||||
|
|
||||||
|
|
||||||
err = sbc_unpack_frame(fin, file_size - fin.tell(), sbc_decoder_frame)
|
err = sbc_unpack_frame(fin, file_size - fin.tell(), sbc_decoder_frame)
|
||||||
|
|
||||||
|
@ -103,6 +103,7 @@ try:
|
|||||||
if subband_frame_count % 200 == 0:
|
if subband_frame_count % 200 == 0:
|
||||||
print ("== Frame %d ==" % subband_frame_count)
|
print ("== Frame %d ==" % subband_frame_count)
|
||||||
|
|
||||||
|
|
||||||
actual_frame = get_actual_frame(fin)
|
actual_frame = get_actual_frame(fin)
|
||||||
|
|
||||||
|
|
||||||
@ -112,6 +113,10 @@ try:
|
|||||||
actual_frame.allocation_method)
|
actual_frame.allocation_method)
|
||||||
|
|
||||||
err = sbc_compare_headers(subband_frame_count, actual_frame, expected_frame)
|
err = sbc_compare_headers(subband_frame_count, actual_frame, expected_frame)
|
||||||
|
print ("%03d : %s %s"%( subband_frame_count,
|
||||||
|
channel_mode_to_str(actual_frame.channel_mode),
|
||||||
|
channel_mode_to_str(expected_frame.channel_mode)))
|
||||||
|
|
||||||
if err < 0:
|
if err < 0:
|
||||||
print ("Headers differ \n%s\n%s" % (actual_frame, expected_frame))
|
print ("Headers differ \n%s\n%s" % (actual_frame, expected_frame))
|
||||||
exit(1)
|
exit(1)
|
||||||
@ -129,7 +134,7 @@ try:
|
|||||||
except TypeError:
|
except TypeError:
|
||||||
fin_expected.close()
|
fin_expected.close()
|
||||||
fin.close()
|
fin.close()
|
||||||
print ("DONE, max MSE PCM error %d" % max_error)
|
print ("DONE, max MSE PCM error %f" % max_error)
|
||||||
exit(0)
|
exit(0)
|
||||||
|
|
||||||
except IOError as e:
|
except IOError as e:
|
||||||
|
@ -139,6 +139,9 @@ try:
|
|||||||
actual_frame = get_actual_frame(fin, nr_blocks, nr_subbands, nr_channels, bitpool, sampling_frequency, allocation_method)
|
actual_frame = get_actual_frame(fin, nr_blocks, nr_subbands, nr_channels, bitpool, sampling_frequency, allocation_method)
|
||||||
expected_frame = get_expected_frame(fin_expected)
|
expected_frame = get_expected_frame(fin_expected)
|
||||||
|
|
||||||
|
print actual_frame.sb_sample
|
||||||
|
print expected_frame.sb_sample
|
||||||
|
|
||||||
err = sbc_compare_headers(subband_frame_count, actual_frame, expected_frame)
|
err = sbc_compare_headers(subband_frame_count, actual_frame, expected_frame)
|
||||||
if err < 0:
|
if err < 0:
|
||||||
exit(1)
|
exit(1)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user