sbc.py (5665ea35c6aeb92912f2adba24f1fcfce0aa3616) | sbc.py (1522543de84c04e141f181431af0e80f00b6b718) |
---|---|
1#!/usr/bin/env python 2import numpy as np 3import wave 4import struct 5import sys 6 7# channel mode 8MONO = 0 --- 110 unchanged lines hidden (view full) --- 119 120# offset tables for 8-subbands 121offset8 = np.array([[ -2, 0, 0, 0, 0, 0, 0, 1 ], 122 [ -3, 0, 0, 0, 0, 0, 1, 2 ], 123 [ -4, 0, 0, 0, 0, 0, 1, 2 ], 124 [ -4, 0, 0, 0, 0, 0, 1, 2 ] 125 ]) 126 | 1#!/usr/bin/env python 2import numpy as np 3import wave 4import struct 5import sys 6 7# channel mode 8MONO = 0 --- 110 unchanged lines hidden (view full) --- 119 120# offset tables for 8-subbands 121offset8 = np.array([[ -2, 0, 0, 0, 0, 0, 0, 1 ], 122 [ -3, 0, 0, 0, 0, 0, 1, 2 ], 123 [ -4, 0, 0, 0, 0, 0, 1, 2 ], 124 [ -4, 0, 0, 0, 0, 0, 1, 2 ] 125 ]) 126 |
127def calculate_scalefactor(max_subbandsample): 128 x = 0 129 while True: 130 y = 1 << x + 1 131 if y > max_subbandsample: 132 break 133 x += 1 134 return (x,y) 135 136 137def calculate_max_subbandsample(nr_blocks, nr_channels, nr_subbands, sb_sample): 138 max_subbandsample = np.zeros(shape = (nr_channels, nr_subbands)) 139 140 for blk in range(nr_blocks): 141 for ch in range(nr_channels): 142 for sb in range(nr_subbands): 143 m = abs(sb_sample[blk][ch][sb]) 144 if max_subbandsample[ch][sb] < m: 145 max_subbandsample[ch][sb] = m 146 return max_subbandsample 147 148def calculate_scalefactors(nr_blocks, nr_channels, nr_subbands, sb_sample): 149 scale_factor = np.zeros(shape=(nr_channels, nr_subbands), dtype = np.int32) 150 scalefactor = np.zeros(shape=(nr_channels, nr_subbands), dtype = np.int32) 151 152 # max_subbandsample = calculate_max_subbandsample(nr_blocks, nr_channels, nr_subbands, sb_sample) 153 # for ch in range(nr_channels): 154 # for sb in range(nr_subbands): 155 # (scale_factor[ch][sb], scalefactor[ch][sb]) = calculate_scalefactor(max_subbandsample[ch][sb]) 156 157 for ch in range(nr_channels): 158 for sb in range(nr_subbands): 159 scale_factor[ch][sb] = 0 160 scalefactor[ch][sb] = 2 161 for blk in range(nr_blocks): 162 while scalefactor[ch][sb] < abs(sb_sample[blk][ch][sb]): 163 scale_factor[ch][sb]+=1 164 scalefactor[ch][sb] *= 2 165 166 return scale_factor, scalefactor 167 168def calculate_scalefactors_and_channel_mode(frame): 169 frame.scale_factor, frame.scalefactor = calculate_scalefactors(frame.nr_blocks, frame.nr_channels, frame.nr_subbands, frame.sb_sample) 170 #print "calculate_scalefactors_and_channel_mode1 ", frame.scale_factor 171 172 if frame.nr_channels == 1: 173 frame.channel_mode = MONO 174 else: 175 sb_sample1 = np.zeros(shape = (frame.nr_blocks,2,frame.nr_subbands), dtype = np.uint16) 176 177 for blk in range(frame.nr_blocks): 178 for sb in range(frame.nr_subbands): 179 sb_sample1[blk][0][sb] = frame.sb_sample[blk][0][sb] + frame.sb_sample[blk][1][sb] 180 sb_sample1[blk][1][sb] = frame.sb_sample[blk][0][sb] - frame.sb_sample[blk][1][sb] 181 182 scale_factor, scalefactor = calculate_scalefactors(frame.nr_blocks, frame.nr_channels, frame.nr_subbands, sb_sample1) 183 #print "calculate_scalefactors_and_channel_mode 2", scale_factor 184 sumb = 0 185 suma = 0 186 for sb in range(frame.nr_subbands): 187 suma += frame.scale_factor[0][sb] + frame.scale_factor[1][sb] 188 sumb += scale_factor[0][sb] + scale_factor[1][sb] 189 190 #print "calculate_scalefactors_and_channel_mode 3", suma, sumb 191 if suma > sumb: 192 frame.channel_mode = JOINT_STEREO 193 else: 194 frame.channel_mode = STEREO 195 196 |
|
127class SBCFrame: 128 syncword = 0 129 sampling_frequency = 0 130 nr_blocks = 0 131 channel_mode = 0 132 nr_channels = 0 133 allocation_method = 0 134 nr_subbands = 0 135 bitpool = 0 136 crc_check = 0 137 # pro subband - 1 138 join = np.zeros(8, dtype = np.uint8) 139 scale_factor = np.zeros(shape=(2, 8), dtype = np.int32) 140 scalefactor = np.zeros(shape=(2, 8), dtype = np.int32) 141 audio_sample = np.zeros(shape = (16,2,8), dtype = np.uint16) 142 sb_sample = np.zeros(shape = (16,2,8), dtype = np.uint16) 143 X = np.zeros(8, dtype = np.int16) 144 EX = np.zeros(8) | 197class SBCFrame: 198 syncword = 0 199 sampling_frequency = 0 200 nr_blocks = 0 201 channel_mode = 0 202 nr_channels = 0 203 allocation_method = 0 204 nr_subbands = 0 205 bitpool = 0 206 crc_check = 0 207 # pro subband - 1 208 join = np.zeros(8, dtype = np.uint8) 209 scale_factor = np.zeros(shape=(2, 8), dtype = np.int32) 210 scalefactor = np.zeros(shape=(2, 8), dtype = np.int32) 211 audio_sample = np.zeros(shape = (16,2,8), dtype = np.uint16) 212 sb_sample = np.zeros(shape = (16,2,8), dtype = np.uint16) 213 X = np.zeros(8, dtype = np.int16) 214 EX = np.zeros(8) |
145 pcm = np.array([], dtype = np.int16) | 215 pcm = np.zeros(shape=(2, 8*16), dtype = np.int16) |
146 bits = np.zeros(shape=(2, 8)) 147 levels = np.zeros(shape=(2, 8), dtype = np.int32) 148 149 150 def __init__(self, nr_blocks=16, nr_subbands=4, nr_channels=1, bitpool=31, sampling_frequency=44100, allocation_method = 0): 151 self.nr_blocks = nr_blocks 152 self.nr_subbands = nr_subbands 153 self.nr_channels = nr_channels --- 37 unchanged lines hidden (view full) --- 191 res += "\n - channel mode %s" % channel_mode_to_str(self.channel_mode) 192 res += "\n - allocation method %s" % allocation_method_to_str(self.allocation_method) 193 194 res += "\n - bitpool %d" % self.bitpool 195 res += "\n - crc check %d" % self.crc_check 196 return res 197 198 | 216 bits = np.zeros(shape=(2, 8)) 217 levels = np.zeros(shape=(2, 8), dtype = np.int32) 218 219 220 def __init__(self, nr_blocks=16, nr_subbands=4, nr_channels=1, bitpool=31, sampling_frequency=44100, allocation_method = 0): 221 self.nr_blocks = nr_blocks 222 self.nr_subbands = nr_subbands 223 self.nr_channels = nr_channels --- 37 unchanged lines hidden (view full) --- 261 res += "\n - channel mode %s" % channel_mode_to_str(self.channel_mode) 262 res += "\n - allocation method %s" % allocation_method_to_str(self.allocation_method) 263 264 res += "\n - bitpool %d" % self.bitpool 265 res += "\n - crc check %d" % self.crc_check 266 return res 267 268 |
199def sbc_bit_allocation_stereo_joint(frame, ch): 200 bitneed = np.zeros(shape=(frame.nr_channels, frame.nr_subbands)) 201 bits = np.zeros(shape=(frame.nr_channels, frame.nr_subbands)) | 269def sbc_bit_allocation_stereo_joint(frame): 270 bitneed = np.zeros(shape=(frame.nr_channels, frame.nr_subbands), dtype = np.int32) 271 bits = np.zeros(shape=(frame.nr_channels, frame.nr_subbands), dtype = np.int32) 272 |
202 loudness = 0 203 204 if frame.allocation_method == SNR: 205 for ch in range(frame.nr_channels): 206 for sb in range(frame.nr_subbands): 207 bitneed[ch][sb] = frame.scale_factor[ch][sb] 208 else: 209 for ch in range(frame.nr_channels): 210 for sb in range(frame.nr_subbands): 211 if frame.scale_factor[ch][sb] == 0: 212 bitneed[ch][sb] = -5 213 else: 214 if frame.nr_subbands == 4: 215 loudness = scale_factor[ch][sb] - offset4[frame.sampling_frequency][sb] 216 else: | 273 loudness = 0 274 275 if frame.allocation_method == SNR: 276 for ch in range(frame.nr_channels): 277 for sb in range(frame.nr_subbands): 278 bitneed[ch][sb] = frame.scale_factor[ch][sb] 279 else: 280 for ch in range(frame.nr_channels): 281 for sb in range(frame.nr_subbands): 282 if frame.scale_factor[ch][sb] == 0: 283 bitneed[ch][sb] = -5 284 else: 285 if frame.nr_subbands == 4: 286 loudness = scale_factor[ch][sb] - offset4[frame.sampling_frequency][sb] 287 else: |
217 if frame.nr_subbands == 4: 218 loudness = frame.scale_factor[ch][sb] - offset4[frame.sampling_frequency][sb] 219 else: 220 loudness = frame.scale_factor[ch][sb] - offset8[frame.sampling_frequency][sb] 221 if loudness > 0: 222 bitneed[ch][sb] = loudness/2 223 else: 224 bitneed[ch][sb] = loudness | 288 loudness = frame.scale_factor[ch][sb] - offset8[frame.sampling_frequency][sb] 289 290 if loudness > 0: 291 bitneed[ch][sb] = loudness/2 292 else: 293 bitneed[ch][sb] = loudness |
225 226 # search the maximum bitneed index 227 max_bitneed = 0 228 for ch in range(frame.nr_channels): 229 for sb in range(frame.nr_subbands): 230 if bitneed[ch][sb] > max_bitneed: 231 max_bitneed = bitneed[ch][sb] 232 233 # calculate how many bitslices fit into the bitpool 234 bitcount = 0 235 slicecount = 0 236 bitslice = max_bitneed + 1 #/* init just above the largest sf */ 237 238 while True: | 294 295 # search the maximum bitneed index 296 max_bitneed = 0 297 for ch in range(frame.nr_channels): 298 for sb in range(frame.nr_subbands): 299 if bitneed[ch][sb] > max_bitneed: 300 max_bitneed = bitneed[ch][sb] 301 302 # calculate how many bitslices fit into the bitpool 303 bitcount = 0 304 slicecount = 0 305 bitslice = max_bitneed + 1 #/* init just above the largest sf */ 306 307 while True: |
239 bitslice = bitslice - 1 240 bitcount = bitcount + slicecount | 308 bitslice -= 1 309 bitcount += slicecount |
241 slicecount = 0 242 for ch in range(frame.nr_channels): 243 for sb in range(frame.nr_subbands): 244 if (bitneed[ch][sb] > bitslice+1) and (bitneed[ch][sb] < bitslice+16): | 310 slicecount = 0 311 for ch in range(frame.nr_channels): 312 for sb in range(frame.nr_subbands): 313 if (bitneed[ch][sb] > bitslice+1) and (bitneed[ch][sb] < bitslice+16): |
245 slicecount = slicecount + 1 | 314 slicecount += 1 |
246 elif bitneed[ch][sb] == bitslice + 1: | 315 elif bitneed[ch][sb] == bitslice + 1: |
247 slicecount = slicecount + 2 | 316 slicecount += 2 |
248 if bitcount + slicecount >= frame.bitpool: 249 break 250 251 if bitcount + slicecount == frame.bitpool: | 317 if bitcount + slicecount >= frame.bitpool: 318 break 319 320 if bitcount + slicecount == frame.bitpool: |
252 bitcount = bitcount + slicecount 253 bitslice = bitslice - 1 | 321 bitcount += slicecount 322 bitslice -= 1 |
254 255 # bits are distributed until the last bitslice is reached 256 for ch in range(frame.nr_channels): 257 for sb in range(frame.nr_subbands): 258 if bitneed[ch][sb] < bitslice+2 : 259 bits[ch][sb]=0; 260 else: 261 bits[ch][sb] = min(bitneed[ch][sb]-bitslice,16) 262 263 ch = 0 264 sb = 0 265 while bitcount < frame.bitpool and sb < frame.nr_subbands: 266 if bits[ch][sb] >= 2 and bits[ch][sb] < 16: | 323 324 # bits are distributed until the last bitslice is reached 325 for ch in range(frame.nr_channels): 326 for sb in range(frame.nr_subbands): 327 if bitneed[ch][sb] < bitslice+2 : 328 bits[ch][sb]=0; 329 else: 330 bits[ch][sb] = min(bitneed[ch][sb]-bitslice,16) 331 332 ch = 0 333 sb = 0 334 while bitcount < frame.bitpool and sb < frame.nr_subbands: 335 if bits[ch][sb] >= 2 and bits[ch][sb] < 16: |
267 bits[ch][sb] = bits[ch][sb] + 1 268 bitcount = bitcount + 1 269 | 336 bits[ch][sb] += 1 337 bitcount += 1 |
270 elif (bitneed[ch][sb] == bitslice+1) and (frame.bitpool > bitcount+1): 271 bits[ch][sb] = 2 272 bitcount += 2 273 274 if ch == 1: 275 ch = 0 | 338 elif (bitneed[ch][sb] == bitslice+1) and (frame.bitpool > bitcount+1): 339 bits[ch][sb] = 2 340 bitcount += 2 341 342 if ch == 1: 343 ch = 0 |
276 sb = sb + 1 | 344 sb += 1 |
277 else: 278 ch = 1 279 | 345 else: 346 ch = 1 347 |
280 ch = 0 281 sb = 0 282 while bitcount < frame.bitpool and sb < frame.nr_subbands: 283 if bits[ch][sb] < 16: 284 bits[ch][sb] = bits[ch][sb] + 1 285 bitcount = bitcount + 1 286 if ch == 1: 287 ch = 0 288 sb = sb + 1 289 else: 290 ch = 1 291 | |
292 return bits 293 294 295def sbc_bit_allocation_mono_dual(frame): 296 #print "Bit allocation for mono/dual channel" 297 bitneed = np.zeros(shape=(frame.nr_channels, frame.nr_subbands), dtype = np.int32) 298 bits = np.zeros(shape=(frame.nr_channels, frame.nr_subbands), dtype = np.int32) 299 loudness = 0 --- 228 unchanged lines hidden --- | 348 return bits 349 350 351def sbc_bit_allocation_mono_dual(frame): 352 #print "Bit allocation for mono/dual channel" 353 bitneed = np.zeros(shape=(frame.nr_channels, frame.nr_subbands), dtype = np.int32) 354 bits = np.zeros(shape=(frame.nr_channels, frame.nr_subbands), dtype = np.int32) 355 loudness = 0 --- 228 unchanged lines hidden --- |