From f08a674b88cd54d27621208e04242ed1acac4ed1 Mon Sep 17 00:00:00 2001 From: Milanka Ringwald Date: Fri, 13 May 2016 15:37:44 +0200 Subject: [PATCH] sbc: fix bit allocation for stereo --- test/sbc/sbc.py | 21 +++++++++++++++++++-- test/sbc/sbc_decoder.py | 16 +++++++++++++--- test/sbc/sbc_decoder_test.py | 7 ++++++- test/sbc/sbc_encoder_test.py | 3 +++ 4 files changed, 41 insertions(+), 6 deletions(-) diff --git a/test/sbc/sbc.py b/test/sbc/sbc.py index 1e97c3ed7..bdada9532 100644 --- a/test/sbc/sbc.py +++ b/test/sbc/sbc.py @@ -337,14 +337,29 @@ def sbc_bit_allocation_stereo_joint(frame): bitcount += 1 elif (bitneed[ch][sb] == bitslice+1) and (frame.bitpool > bitcount+1): bits[ch][sb] = 2 - bitcount += 2 - + bitcount += 2 if ch == 1: ch = 0 sb += 1 else: 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 @@ -507,12 +522,14 @@ def get_bit(fin): def drop_remaining_bits(): global ibuffer_count + #print "dropping %d bits" % ibuffer_count ibuffer_count = 0 def get_bits(fin, bit_count): bits = 0 for i in range(bit_count): bits = (bits << 1) | get_bit(fin) + # print "get bits: %d -> %02x" %(bit_count, bits) return bits diff --git a/test/sbc/sbc_decoder.py b/test/sbc/sbc_decoder.py index b72240b8e..4cbb0dc3c 100755 --- a/test/sbc/sbc_decoder.py +++ b/test/sbc/sbc_decoder.py @@ -9,11 +9,20 @@ V = np.zeros(shape = (2, 10*2*8)) def sbc_unpack_frame(fin, available_bytes, frame): if available_bytes == 0: + print "no available_bytes" raise TypeError frame.syncword = get_bits(fin,8) 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 frame.sampling_frequency = 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 sb in range(frame.nr_subbands): 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() return 0 @@ -210,7 +219,8 @@ if __name__ == "__main__": while True: sbc_decoder_frame = SBCFrame() 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) diff --git a/test/sbc/sbc_decoder_test.py b/test/sbc/sbc_decoder_test.py index ef5f78d98..0d271ad4c 100755 --- a/test/sbc/sbc_decoder_test.py +++ b/test/sbc/sbc_decoder_test.py @@ -103,6 +103,7 @@ try: if subband_frame_count % 200 == 0: print ("== Frame %d ==" % subband_frame_count) + actual_frame = get_actual_frame(fin) @@ -112,6 +113,10 @@ try: actual_frame.allocation_method) 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: print ("Headers differ \n%s\n%s" % (actual_frame, expected_frame)) exit(1) @@ -129,7 +134,7 @@ try: except TypeError: fin_expected.close() fin.close() - print ("DONE, max MSE PCM error %d" % max_error) + print ("DONE, max MSE PCM error %f" % max_error) exit(0) except IOError as e: diff --git a/test/sbc/sbc_encoder_test.py b/test/sbc/sbc_encoder_test.py index ab42a42f1..693185990 100755 --- a/test/sbc/sbc_encoder_test.py +++ b/test/sbc/sbc_encoder_test.py @@ -139,6 +139,9 @@ try: actual_frame = get_actual_frame(fin, nr_blocks, nr_subbands, nr_channels, bitpool, sampling_frequency, allocation_method) 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) if err < 0: exit(1)