1#!/usr/bin/python
2
3# Audio Tools, a module and set of tools for manipulating audio data
4# Copyright (C) 2007-2014  Brian Langenberger
5
6# This program is free software; you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation; either version 2 of the License, or
9# (at your option) any later version.
10
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15
16# You should have received a copy of the GNU General Public License
17# along with this program; if not, write to the Free Software
18# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19
20from audiotools.bitstream import BitstreamReader
21from audiotools.pcm import from_channels, empty_framelist, from_list
22from math import log
23from hashlib import md5
24
25
26def sub_blocks(reader, sub_blocks_size):
27    while sub_blocks_size > 0:
28        sub_block = Sub_Block.read(reader)
29        yield sub_block
30        sub_blocks_size -= sub_block.total_size()
31
32
33class WavPackDecoder(object):
34    def __init__(self, filename):
35        self.reader = BitstreamReader(open(filename, "rb"), True)
36
37        stream_start = self.reader.getpos()
38
39        # read initial block to populate
40        # sample_rate, bits_per_sample, channels, and channel_mask
41        block_header = Block_Header.read(self.reader)
42        sub_blocks_size = block_header.block_size - 24
43        sub_blocks_data = self.reader.substream(sub_blocks_size)
44        if block_header.sample_rate != 15:
45            self.sample_rate = [6000,  8000,  9600,  11025, 12000,
46                                16000, 22050, 24000, 32000, 44100,
47                                48000, 64000, 88200, 96000,
48                                192000][block_header.sample_rate]
49        else:
50            sub_blocks_start = sub_blocks_data.getpos()
51            try:
52                for sub_block in sub_blocks(sub_blocks_data,
53                                            sub_blocks_size):
54                    if (((sub_block.metadata_function == 7) and
55                         (sub_block.nondecoder_data == 1))):
56                        self.sample_rate = sub_block.data.read(
57                            sub_block.data_size() * 8)
58                        break
59                else:
60                    raise ValueError("invalid sample rate")
61            finally:
62                sub_blocks_data.setpos(sub_blocks_start)
63
64        self.bits_per_sample = \
65            [8, 16, 24, 32][block_header.bits_per_sample]
66
67        if block_header.initial_block and block_header.final_block:
68            if (((block_header.mono_output == 0) or
69                 (block_header.false_stereo == 1))):
70                self.channels = 2
71                self.channel_mask = 0x3
72            else:
73                self.channels = 1
74                self.channel_mask = 0x4
75        else:
76            # look for channel mask sub block
77            sub_blocks_start = sub_blocks_data.getpos()
78            try:
79                for sub_block in sub_blocks(sub_blocks_data, sub_blocks_size):
80                    if (((sub_block.metadata_function == 13) and
81                         (sub_block.nondecoder_data == 0))):
82                        self.channels = sub_block.data.read(8)
83                        self.channel_mask = sub_block.data.read(
84                            (sub_block.data_size() - 1) * 8)
85                        break
86                else:
87                    # FIXME - handle case of no channel mask sub block
88                    raise NotImplementedError()
89            finally:
90                sub_blocks_data.setpos(sub_blocks_start)
91
92        self.reader.setpos(stream_start)
93
94        self.pcm_finished = False
95        self.md5_checked = False
96        self.md5sum = md5()
97
98    def read(self, pcm_frames):
99        if self.pcm_finished:
100            if not self.md5_checked:
101                block_start = self.reader.getpos()
102                try:
103                    try:
104                        header = Block_Header.read(self.reader)
105                        sub_blocks_size = header.block_size - 24
106                        sub_blocks_data = \
107                            self.reader.substream(sub_blocks_size)
108                        for sub_block in sub_blocks(sub_blocks_data,
109                                                    sub_blocks_size):
110                            if (((sub_block.metadata_function == 6) and
111                                 (sub_block.nondecoder_data == 1))):
112                                if ((sub_block.data.read_bytes(16) !=
113                                     self.md5sum.digest())):
114                                    raise ValueError("invalid stream MD5 sum")
115                    except (IOError, ValueError):
116                        # no error if a block isn't found
117                        pass
118                finally:
119                    self.reader.setpos(block_start)
120
121            return empty_framelist(self.channels, self.bits_per_sample)
122
123        channels = []
124
125        while True:  # in place of a do-while loop
126            try:
127                block_header = Block_Header.read(self.reader)
128            except (ValueError, IOError):
129                self.pcm_finished = True
130                return empty_framelist(self.channels, self.bits_per_sample)
131            sub_blocks_size = block_header.block_size - 24
132            sub_blocks_data = self.reader.substream(sub_blocks_size)
133            channels.extend(read_block(block_header,
134                                       sub_blocks_size,
135                                       sub_blocks_data))
136
137            if block_header.final_block == 1:
138                break
139
140        if ((block_header.block_index +
141             block_header.block_samples) >= block_header.total_samples):
142            self.pcm_finished = True
143
144        # combine channels of audio data into single block
145        block = from_channels([from_list(ch, 1, self.bits_per_sample, True)
146                               for ch in channels])
147
148        # update MD5 sum
149        self.md5sum.update(block.to_bytes(False, self.bits_per_sample > 8))
150
151        # return single block of audio data
152        return block
153
154    def close(self):
155        self.reader.close()
156
157    def __enter__(self):
158        return self
159
160    def __exit__(self, exc_type, exc_value, traceback):
161        self.close()
162
163
164class Block_Header(object):
165    def __init__(self,
166                 block_id, block_size, version, track_number, index_number,
167                 total_samples, block_index, block_samples, bits_per_sample,
168                 mono_output, hybrid_mode, joint_stereo, channel_decorrelation,
169                 hybrid_noise_shaping, floating_point_data,
170                 extended_size_integers, hybrid_controls_bitrate,
171                 hybrid_noise_balanced, initial_block, final_block,
172                 left_shift_data, maximum_magnitude, sample_rate,
173                 use_IIR, false_stereo, CRC):
174        if block_id != b"wvpk":
175            raise ValueError("invalid WavPack block ID")
176        self.block_size = block_size
177        self.version = version
178        self.track_number = track_number
179        self.index_number = index_number
180        self.total_samples = total_samples
181        self.block_index = block_index
182        self.block_samples = block_samples
183        self.bits_per_sample = bits_per_sample
184        self.mono_output = mono_output
185        self.hybrid_mode = hybrid_mode
186        self.joint_stereo = joint_stereo
187        self.channel_decorrelation = channel_decorrelation
188        self.hybrid_noise_shaping = hybrid_noise_shaping
189        self.floating_point_data = floating_point_data
190        self.extended_size_integers = extended_size_integers
191        self.hybrid_controls_bitrate = hybrid_controls_bitrate
192        self.hybrid_noise_balanced = hybrid_noise_balanced
193        self.initial_block = initial_block
194        self.final_block = final_block
195        self.left_shift_data = left_shift_data
196        self.maximum_magnitude = maximum_magnitude
197        self.sample_rate = sample_rate
198        self.use_IIR = use_IIR
199        self.false_stereo = false_stereo
200        self.CRC = CRC
201
202    def __repr__(self):
203        return "Block_Header(%s)" % \
204            ", ".join(["%s=%s" % (attr, getattr(self, attr))
205                       for attr in
206                       ["block_size", "version", "track_number",
207                        "index_number", "total_samples", "block_index",
208                        "block_samples", "bits_per_sample", "mono_output",
209                        "hybrid_mode", "joint_stereo",
210                        "channel_decorrelation", "hybrid_noise_shaping",
211                        "floating_point_data", "extended_size_integers",
212                        "hybrid_controls_bitrate", "hybrid_noise_balanced",
213                        "initial_block", "final_block", "left_shift_data",
214                        "maximum_magnitude", "sample_rate",
215                        "use_IIR", "false_stereo", "CRC"]])
216
217    @classmethod
218    def read(cls, reader):
219        return cls(*reader.parse("4b 32u 16u 8u 8u 32u 32u 32u" +
220                                 "2u 11* 1u 5u 5u 4u 2p 1u 1u 1p" +
221                                 "32u"))
222
223
224class Sub_Block(object):
225    def __init__(self, metadata_function, nondecoder_data,
226                 actual_size_1_less, large_block, sub_block_size,
227                 data):
228        self.metadata_function = metadata_function
229        self.nondecoder_data = nondecoder_data
230        self.actual_size_1_less = actual_size_1_less
231        self.large_block = large_block
232        self.sub_block_size = sub_block_size
233        self.data = data
234
235    def __repr__(self):
236        return "Sub_Block(%s)" % \
237            ", ".join(["%s=%s" % (attr, getattr(self, attr))
238                       for attr in
239                       ["metadata_function", "nondecoder_data",
240                        "actual_size_1_less", "large_block",
241                        "sub_block_size", "data"]])
242
243    def total_size(self):
244        if self.large_block:
245            return 1 + 3 + (self.sub_block_size * 2)
246        else:
247            return 1 + 1 + (self.sub_block_size * 2)
248
249    def data_size(self):
250        if self.actual_size_1_less:
251            return self.sub_block_size * 2 - 1
252        else:
253            return self.sub_block_size * 2
254
255    @classmethod
256    def read(cls, reader):
257        (metadata_function,
258         nondecoder_data,
259         actual_size_1_less,
260         large_block) = reader.parse("5u 1u 1u 1u")
261
262        if large_block == 0:
263            sub_block_size = reader.read(8)
264        else:
265            sub_block_size = reader.read(24)
266
267        if actual_size_1_less == 0:
268            data = reader.substream(sub_block_size * 2)
269        else:
270            data = reader.substream(sub_block_size * 2 - 1)
271            reader.skip(8)
272
273        return cls(metadata_function,
274                   nondecoder_data,
275                   actual_size_1_less,
276                   large_block,
277                   sub_block_size,
278                   data)
279
280
281def read_block(block_header, sub_blocks_size, sub_blocks_data):
282    """returns 1 or 2 channels of PCM data integers"""
283
284    decorrelation_terms_read = False
285    decorrelation_weights_read = False
286    decorrelation_samples_read = False
287    entropies_read = False
288    residuals_read = False
289    extended_integers_read = False
290
291    while sub_blocks_size > 0:
292        (metadata_function,
293         nondecoder_data,
294         actual_size_1_less,
295         large_sub_block) = sub_blocks_data.parse("5u 1u 1u 1u")
296        if large_sub_block == 0:
297            sub_block_size = sub_blocks_data.read(8)
298        else:
299            sub_block_size = sub_blocks_data.read(24)
300        if actual_size_1_less == 0:
301            sub_block_data = sub_blocks_data.substream(sub_block_size * 2)
302        else:
303            sub_block_data = sub_blocks_data.substream(sub_block_size * 2 - 1)
304            sub_blocks_data.skip(8)
305
306        if nondecoder_data == 0:
307            if metadata_function == 2:
308                (decorrelation_terms,
309                 decorrelation_deltas) = read_decorrelation_terms(
310                    sub_block_size, actual_size_1_less, sub_block_data)
311                decorrelation_terms_read = True
312            if metadata_function == 3:
313                if not decorrelation_terms_read:
314                    raise ValueError(
315                        "weights sub block found before terms sub block")
316                decorrelation_weights = read_decorrelation_weights(
317                    block_header, len(decorrelation_terms),
318                    sub_block_size, actual_size_1_less, sub_block_data)
319                decorrelation_weights_read = True
320            if metadata_function == 4:
321                if not decorrelation_terms_read:
322                    raise ValueError(
323                        "samples sub block found before terms sub block")
324                if actual_size_1_less:
325                    raise ValueError(
326                        "decorrelation samples must have an even byte count")
327                decorrelation_samples = read_decorrelation_samples(
328                    block_header, decorrelation_terms,
329                    sub_block_size, sub_block_data)
330                decorrelation_samples_read = True
331            if metadata_function == 5:
332                entropies = read_entropy_variables(block_header,
333                                                   sub_block_data)
334                entropies_read = True
335            if metadata_function == 9:
336                (zero_bits,
337                 one_bits,
338                 duplicate_bits) = read_extended_integers(sub_block_data)
339                extended_integers_read = True
340            if metadata_function == 10:
341                if not entropies_read:
342                    raise ValueError("bitstream sub block before " +
343                                     "entropy variables sub block")
344                residuals = read_bitstream(block_header, entropies,
345                                           sub_block_data)
346                residuals_read = True
347
348        if large_sub_block == 0:
349            sub_blocks_size -= (2 + 2 * sub_block_size)
350        else:
351            sub_blocks_size -= (4 + 2 * sub_block_size)
352
353    if decorrelation_terms_read:
354        if not decorrelation_weights_read:
355            raise ValueError("decorrelation weights sub block not found")
356        if not decorrelation_samples_read:
357            raise ValueError("decorrelation samples sub block not found")
358
359    if not residuals_read:
360        raise ValueError("bitstream sub block not found")
361
362    if (block_header.mono_output == 0) and (block_header.false_stereo == 0):
363        if decorrelation_terms_read and len(decorrelation_terms) > 0:
364            decorrelated = decorrelate_channels(residuals,
365                                                decorrelation_terms,
366                                                decorrelation_deltas,
367                                                decorrelation_weights,
368                                                decorrelation_samples)
369        else:
370            decorrelated = residuals
371
372        if block_header.joint_stereo == 1:
373            left_right = undo_joint_stereo(decorrelated)
374        else:
375            left_right = decorrelated
376
377        channels_crc = calculate_crc(left_right)
378        if channels_crc != block_header.CRC:
379            raise ValueError("CRC mismatch (0x%8.8X != 0x%8.8X)" %
380                             (channels_crc, block_header.CRC))
381
382        if block_header.extended_size_integers == 1:
383            un_shifted = undo_extended_integers(zero_bits,
384                                                one_bits,
385                                                duplicate_bits,
386                                                left_right)
387        else:
388            un_shifted = left_right
389
390        return un_shifted
391    else:
392        if decorrelation_terms_read and len(decorrelation_terms) > 0:
393            decorrelated = decorrelate_channels(residuals,
394                                                decorrelation_terms,
395                                                decorrelation_deltas,
396                                                decorrelation_weights,
397                                                decorrelation_samples)
398        else:
399            decorrelated = residuals
400
401        channels_crc = calculate_crc(decorrelated)
402        if channels_crc != block_header.CRC:
403            raise ValueError("CRC mismatch (0x%8.8X != 0x%8.8X)" %
404                             (channels_crc, block_header.CRC))
405
406        if block_header.extended_size_integers == 1:
407            un_shifted = undo_extended_integers(zero_bits,
408                                                one_bits,
409                                                duplicate_bits,
410                                                decorrelated)
411        else:
412            un_shifted = decorrelated
413
414        if block_header.false_stereo == 0:
415            return un_shifted
416        else:
417            return (un_shifted[0], un_shifted[0])
418
419
420def read_decorrelation_terms(sub_block_size,
421                             actual_size_1_less,
422                             sub_block_data):
423    """returns a list of decorrelation terms
424    and a list of decorrelation deltas per decorrelation pass
425
426    term[pass] , delta[pass]"""
427
428    if actual_size_1_less == 0:
429        passes = sub_block_size * 2
430    else:
431        passes = sub_block_size * 2 - 1
432
433    if passes > 16:
434        raise ValueError("invalid decorrelation passes count")
435
436    decorrelation_terms = []
437    decorrelation_deltas = []
438    for i in range(passes):
439        decorrelation_terms.append(sub_block_data.read(5) - 5)
440        if (not (((1 <= decorrelation_terms[-1]) and
441                  (decorrelation_terms[-1] <= 18)) or
442                 ((-3 <= decorrelation_terms[-1]) and
443                  (decorrelation_terms[-1] <= -1)))):
444            raise ValueError("invalid decorrelation term")
445        decorrelation_deltas.append(sub_block_data.read(3))
446
447    decorrelation_terms.reverse()
448    decorrelation_deltas.reverse()
449
450    return (decorrelation_terms, decorrelation_deltas)
451
452
453def read_decorrelation_weights(block_header, decorrelation_terms_count,
454                               sub_block_size, actual_size_1_less,
455                               sub_block_data):
456    """returns one tuple of decorrelation weights per decorrelation pass
457    the number of weights in each tuple equals the number of channels
458
459    weight[pass][channel]
460    """
461
462    if actual_size_1_less == 0:
463        weight_count = sub_block_size * 2
464    else:
465        weight_count = sub_block_size * 2 - 1
466
467    weight_values = []
468    for i in range(weight_count):
469        value_i = sub_block_data.read_signed(8)
470        if value_i > 0:
471            weight_values.append((value_i * 2 ** 3) +
472                                 ((value_i * 2 ** 3 + 2 ** 6) // 2 ** 7))
473        elif(value_i == 0):
474            weight_values.append(0)
475        else:
476            weight_values.append(value_i * 2 ** 3)
477
478    weights = []
479    if (block_header.mono_output == 0) and (block_header.false_stereo == 0):
480        # two channels
481        if (weight_count // 2) > decorrelation_terms_count:
482            raise ValueError("invalid number of decorrelation weights")
483
484        for i in range(weight_count // 2):
485            weights.append((weight_values[i * 2],
486                            weight_values[i * 2 + 1]))
487        for i in range(weight_count // 2, decorrelation_terms_count):
488            weights.append((0, 0))
489
490        weights.reverse()
491    else:
492        # one channel
493        if weight_count > decorrelation_terms_count:
494            raise ValueError("invalid number of decorrelation weights")
495
496        for i in range(weight_count):
497            weights.append((weight_values[i], ))
498        for i in range(weight_count, decorrelation_terms_count):
499            weights.append((0, 0))
500        weights.reverse()
501
502    return weights
503
504
505def read_decorrelation_samples(block_header, decorrelation_terms,
506                               sub_block_size, sub_block_data):
507    """returns one tuple of decorrelation samples lists
508    per decorrelation pass
509
510    sample[pass][channel][s]"""
511
512    sub_block_bytes = sub_block_size * 2
513
514    samples = []
515    if (block_header.mono_output == 0) and (block_header.false_stereo == 0):
516        # two channels
517        for term in reversed(decorrelation_terms):
518            if (17 <= term) and (term <= 18):
519                if sub_block_bytes >= 8:
520                    samples.append(([read_exp2(sub_block_data),
521                                     read_exp2(sub_block_data)],
522                                    [read_exp2(sub_block_data),
523                                     read_exp2(sub_block_data)]))
524                    sub_block_bytes -= 8
525                else:
526                    samples.append(([0, 0], [0, 0]))
527                    sub_block_bytes = 0
528            elif (1 <= term) and (term <= 8):
529                term_samples = ([], [])
530                if sub_block_bytes >= (term * 4):
531                    for s in range(term):
532                        term_samples[0].append(read_exp2(sub_block_data))
533                        term_samples[1].append(read_exp2(sub_block_data))
534                    sub_block_bytes -= (term * 4)
535                else:
536                    for s in range(term):
537                        term_samples[0].append(0)
538                        term_samples[1].append(0)
539                    sub_block_bytes = 0
540                samples.append(term_samples)
541            elif (-3 <= term) and (term <= -1):
542                if sub_block_bytes >= 4:
543                    samples.append(([read_exp2(sub_block_data)],
544                                    [read_exp2(sub_block_data)]))
545                    sub_block_bytes -= 4
546                else:
547                    samples.append(([0], [0]))
548                    sub_block_bytes = 0
549            else:
550                raise ValueError("invalid decorrelation term")
551
552        samples.reverse()
553        return samples
554    else:
555        # one channel
556        for term in reversed(decorrelation_terms):
557            if (17 <= term) and (term <= 18):
558                if sub_block_bytes >= 4:
559                    samples.append(([read_exp2(sub_block_data),
560                                     read_exp2(sub_block_data)],))
561                    sub_block_bytes -= 4
562                else:
563                    samples[0].append(([0, 0],))
564                    sub_block_bytes = 0
565            elif (1 <= term) and (term <= 8):
566                term_samples = ([],)
567                if sub_block_bytes >= (term * 2):
568                    for s in range(term):
569                        term_samples[0].append(read_exp2(sub_block_data))
570                    sub_block_bytes -= (term * 2)
571                else:
572                    for s in range(term):
573                        term_samples[0].append(0)
574                    sub_block_bytes = 0
575                samples.append(term_samples)
576            else:
577                raise ValueError("invalid decorrelation term")
578
579        samples.reverse()
580        return samples
581
582
583def read_entropy_variables(block_header, sub_block_data):
584    entropies = ([], [])
585    for i in range(3):
586        entropies[0].append(read_exp2(sub_block_data))
587
588    if (block_header.mono_output == 0) and (block_header.false_stereo == 0):
589        for i in range(3):
590            entropies[1].append(read_exp2(sub_block_data))
591    else:
592        entropies[1].extend([0, 0, 0])
593
594    return entropies
595
596
597def read_bitstream(block_header, entropies, sub_block_data):
598    if (block_header.mono_output == 0) and (block_header.false_stereo == 0):
599        channel_count = 2
600        residuals = ([], [])
601    else:
602        channel_count = 1
603        residuals = ([], )
604
605    u = None
606    i = 0
607    while i < (block_header.block_samples * channel_count):
608        if (u is None) and (entropies[0][0] < 2) and (entropies[1][0] < 2):
609            # handle long run of 0 residuals
610            zeroes = read_egc(sub_block_data)
611            if zeroes > 0:
612                for j in range(zeroes):
613                    residuals[i % channel_count].append(0)
614                    i += 1
615                entropies[0][0] = entropies[0][1] = entropies[0][2] = 0
616                entropies[1][0] = entropies[1][1] = entropies[1][2] = 0
617            if i < (block_header.block_samples * channel_count):
618                (residual, u) = read_residual(
619                    sub_block_data,
620                    u,
621                    entropies[i % channel_count])
622                residuals[i % channel_count].append(residual)
623                i += 1
624        else:
625            (residual, u) = read_residual(
626                sub_block_data,
627                u,
628                entropies[i % channel_count])
629            residuals[i % channel_count].append(residual)
630            i += 1
631
632    return residuals
633
634
635def read_egc(reader):
636    t = reader.unary(0)
637    if t > 0:
638        p = reader.read(t - 1)
639        return 2 ** (t - 1) + p
640    else:
641        return t
642
643
644def read_residual(reader, last_u, entropies):
645    if last_u is None:
646        u = reader.unary(0)
647        if u == 16:
648            u += read_egc(reader)
649        m = u // 2
650    elif (last_u % 2) == 1:
651        u = reader.unary(0)
652        if u == 16:
653            u += read_egc(reader)
654        m = (u // 2) + 1
655    else:
656        u = None
657        m = 0
658
659    if m == 0:
660        base = 0
661        add = entropies[0] >> 4
662        entropies[0] -= ((entropies[0] + 126) >> 7) * 2
663    elif m == 1:
664        base = (entropies[0] >> 4) + 1
665        add = entropies[1] >> 4
666        entropies[0] += ((entropies[0] + 128) >> 7) * 5
667        entropies[1] -= ((entropies[1] + 62) >> 6) * 2
668    elif m == 2:
669        base = ((entropies[0] >> 4) + 1) + ((entropies[1] >> 4) + 1)
670        add = entropies[2] >> 4
671        entropies[0] += ((entropies[0] + 128) >> 7) * 5
672        entropies[1] += ((entropies[1] + 64) >> 6) * 5
673        entropies[2] -= ((entropies[2] + 30) >> 5) * 2
674    else:
675        base = (((entropies[0] >> 4) + 1) +
676                ((entropies[1] >> 4) + 1) +
677                (((entropies[2] >> 4) + 1) * (m - 2)))
678        add = entropies[2] >> 4
679        entropies[0] += ((entropies[0] + 128) >> 7) * 5
680        entropies[1] += ((entropies[1] + 64) >> 6) * 5
681        entropies[2] += ((entropies[2] + 32) >> 5) * 5
682
683    if add == 0:
684        unsigned = base
685    else:
686        p = int(log(add) / log(2))
687        e = 2 ** (p + 1) - add - 1
688        r = reader.read(p)
689        if r >= e:
690            b = reader.read(1)
691            unsigned = base + (r * 2) - e + b
692        else:
693            unsigned = base + r
694
695    sign = reader.read(1)
696    if sign == 1:
697        return (-unsigned - 1, u)
698    else:
699        return (unsigned, u)
700
701
702def undo_joint_stereo(samples):
703    assert(len(samples) == 2)
704    assert(len(samples[0]) == len(samples[1]))
705
706    stereo = [[], []]
707    for (mid, side) in zip(*samples):
708        right = side - (mid >> 1)
709        left = mid + right
710        stereo[0].append(left)
711        stereo[1].append(right)
712
713    return stereo
714
715
716def read_extended_integers(sub_block_data):
717    (sent_bits,
718     zero_bits,
719     one_bits,
720     duplicate_bits) = sub_block_data.parse("8u 8u 8u 8u")
721    return (zero_bits, one_bits, duplicate_bits)
722
723
724def undo_extended_integers(zero_bits, one_bits, duplicate_bits,
725                           channels):
726    un_shifted = []
727    for channel in channels:
728        if zero_bits > 0:
729            un_shifted.append([s << zero_bits for s in channel])
730        elif one_bits > 0:
731            ones = (1 << one_bits) - 1
732            un_shifted.append([(s << one_bits) + ones for s in channel])
733        elif duplicate_bits > 0:
734            dupes = []
735            ones = (1 << duplicate_bits) - 1
736            for s in channel:
737                if (s % 2) == 0:
738                    dupes.append(s << duplicate_bits)
739                else:
740                    dupes.append((s << duplicate_bits) + ones)
741            un_shifted.append(dupes)
742        else:
743            un_shifted.append(channel)
744
745    return tuple(un_shifted)
746
747
748EXP2 = [0x100, 0x101, 0x101, 0x102, 0x103, 0x103, 0x104, 0x105,
749        0x106, 0x106, 0x107, 0x108, 0x108, 0x109, 0x10a, 0x10b,
750        0x10b, 0x10c, 0x10d, 0x10e, 0x10e, 0x10f, 0x110, 0x110,
751        0x111, 0x112, 0x113, 0x113, 0x114, 0x115, 0x116, 0x116,
752        0x117, 0x118, 0x119, 0x119, 0x11a, 0x11b, 0x11c, 0x11d,
753        0x11d, 0x11e, 0x11f, 0x120, 0x120, 0x121, 0x122, 0x123,
754        0x124, 0x124, 0x125, 0x126, 0x127, 0x128, 0x128, 0x129,
755        0x12a, 0x12b, 0x12c, 0x12c, 0x12d, 0x12e, 0x12f, 0x130,
756        0x130, 0x131, 0x132, 0x133, 0x134, 0x135, 0x135, 0x136,
757        0x137, 0x138, 0x139, 0x13a, 0x13a, 0x13b, 0x13c, 0x13d,
758        0x13e, 0x13f, 0x140, 0x141, 0x141, 0x142, 0x143, 0x144,
759        0x145, 0x146, 0x147, 0x148, 0x148, 0x149, 0x14a, 0x14b,
760        0x14c, 0x14d, 0x14e, 0x14f, 0x150, 0x151, 0x151, 0x152,
761        0x153, 0x154, 0x155, 0x156, 0x157, 0x158, 0x159, 0x15a,
762        0x15b, 0x15c, 0x15d, 0x15e, 0x15e, 0x15f, 0x160, 0x161,
763        0x162, 0x163, 0x164, 0x165, 0x166, 0x167, 0x168, 0x169,
764        0x16a, 0x16b, 0x16c, 0x16d, 0x16e, 0x16f, 0x170, 0x171,
765        0x172, 0x173, 0x174, 0x175, 0x176, 0x177, 0x178, 0x179,
766        0x17a, 0x17b, 0x17c, 0x17d, 0x17e, 0x17f, 0x180, 0x181,
767        0x182, 0x183, 0x184, 0x185, 0x187, 0x188, 0x189, 0x18a,
768        0x18b, 0x18c, 0x18d, 0x18e, 0x18f, 0x190, 0x191, 0x192,
769        0x193, 0x195, 0x196, 0x197, 0x198, 0x199, 0x19a, 0x19b,
770        0x19c, 0x19d, 0x19f, 0x1a0, 0x1a1, 0x1a2, 0x1a3, 0x1a4,
771        0x1a5, 0x1a6, 0x1a8, 0x1a9, 0x1aa, 0x1ab, 0x1ac, 0x1ad,
772        0x1af, 0x1b0, 0x1b1, 0x1b2, 0x1b3, 0x1b4, 0x1b6, 0x1b7,
773        0x1b8, 0x1b9, 0x1ba, 0x1bc, 0x1bd, 0x1be, 0x1bf, 0x1c0,
774        0x1c2, 0x1c3, 0x1c4, 0x1c5, 0x1c6, 0x1c8, 0x1c9, 0x1ca,
775        0x1cb, 0x1cd, 0x1ce, 0x1cf, 0x1d0, 0x1d2, 0x1d3, 0x1d4,
776        0x1d6, 0x1d7, 0x1d8, 0x1d9, 0x1db, 0x1dc, 0x1dd, 0x1de,
777        0x1e0, 0x1e1, 0x1e2, 0x1e4, 0x1e5, 0x1e6, 0x1e8, 0x1e9,
778        0x1ea, 0x1ec, 0x1ed, 0x1ee, 0x1f0, 0x1f1, 0x1f2, 0x1f4,
779        0x1f5, 0x1f6, 0x1f8, 0x1f9, 0x1fa, 0x1fc, 0x1fd, 0x1ff]
780
781
782def read_exp2(reader):
783    value = reader.read_signed(16)
784    if (-32768 <= value) and (value < -2304):
785        return -(EXP2[-value & 0xFF] << ((-value >> 8) - 9))
786    elif (-2304 <= value) and (value < 0):
787        return -(EXP2[-value & 0xFF] >> (9 - (-value >> 8)))
788    elif (0 <= value) and (value <= 2304):
789        return EXP2[value & 0xFF] >> (9 - (value >> 8))
790    elif (2304 < value) and (value <= 32767):
791        return EXP2[value & 0xFF] << ((value >> 8) - 9)
792
793
794def decorrelate_channels(residuals,
795                         decorrelation_terms, decorrelation_deltas,
796                         decorrelation_weights, decorrelation_samples):
797    """returns a tuple of 1 or 2 lists of decorrelated channel data"""
798
799    if len(residuals) == 2:
800        latest_pass = [r[:] for r in residuals]
801        for (term,
802             delta,
803             weights,
804             samples) in zip(decorrelation_terms,
805                             decorrelation_deltas,
806                             decorrelation_weights,
807                             decorrelation_samples):
808            latest_pass = decorrelation_pass_2ch(latest_pass,
809                                                 term,
810                                                 delta,
811                                                 weights,
812                                                 samples)
813        return latest_pass
814    else:
815        latest_pass = residuals[0][:]
816        for (term,
817             delta,
818             weight,
819             samples) in zip(decorrelation_terms,
820                             decorrelation_deltas,
821                             decorrelation_weights,
822                             decorrelation_samples):
823            latest_pass = decorrelation_pass_1ch(latest_pass,
824                                                 term,
825                                                 delta,
826                                                 weight[0],
827                                                 samples[0])
828        return (latest_pass, )
829
830
831def decorrelation_pass_1ch(correlated_samples,
832                           term, delta, weight, decorrelation_samples):
833    if term == 18:
834        assert(len(decorrelation_samples) == 2)
835        decorrelated = decorrelation_samples[:]
836        decorrelated.reverse()
837        for i in range(len(correlated_samples)):
838            temp = (3 * decorrelated[i + 1] - decorrelated[i]) // 2
839            decorrelated.append(apply_weight(weight, temp) +
840                                correlated_samples[i])
841            weight += update_weight(temp, correlated_samples[i], delta)
842        return decorrelated[2:]
843    elif term == 17:
844        assert(len(decorrelation_samples) == 2)
845        decorrelated = decorrelation_samples[:]
846        decorrelated.reverse()
847        for i in range(len(correlated_samples)):
848            temp = 2 * decorrelated[i + 1] - decorrelated[i]
849            decorrelated.append(apply_weight(weight, temp) +
850                                correlated_samples[i])
851            weight += update_weight(temp, correlated_samples[i], delta)
852        return decorrelated[2:]
853    elif (1 <= term) and (term <= 8):
854        assert(len(decorrelation_samples) == term)
855        decorrelated = decorrelation_samples[:]
856        for i in range(len(correlated_samples)):
857            decorrelated.append(apply_weight(weight, decorrelated[i]) +
858                                correlated_samples[i])
859            weight += update_weight(decorrelated[i],
860                                    correlated_samples[i],
861                                    delta)
862        return decorrelated[term:]
863    else:
864        raise ValueError("unsupported term")
865
866
867def decorrelation_pass_2ch(correlated,
868                           term, delta, weights, decorrelation_samples):
869    assert(len(correlated) == 2)
870    assert(len(correlated[0]) == len(correlated[1]))
871    assert(len(weights) == 2)
872
873    if ((17 <= term) and (term <= 18)) or ((1 <= term) and (term <= 8)):
874        return (decorrelation_pass_1ch(correlated[0],
875                                       term, delta, weights[0],
876                                       decorrelation_samples[0]),
877                decorrelation_pass_1ch(correlated[1],
878                                       term, delta, weights[1],
879                                       decorrelation_samples[1]))
880    elif (-3 <= term) and (term <= -1):
881        assert(len(decorrelation_samples[0]) == 1)
882        decorrelated = ([decorrelation_samples[1][0]],
883                        [decorrelation_samples[0][0]])
884        weights = list(weights)
885        if term == -1:
886            for i in range(len(correlated[0])):
887                decorrelated[0].append(apply_weight(weights[0],
888                                                    decorrelated[1][i]) +
889                                       correlated[0][i])
890                decorrelated[1].append(apply_weight(weights[1],
891                                                    decorrelated[0][i + 1]) +
892                                       correlated[1][i])
893
894                weights[0] += update_weight(decorrelated[1][i],
895                                            correlated[0][i],
896                                            delta)
897                weights[1] += update_weight(decorrelated[0][i + 1],
898                                            correlated[1][i],
899                                            delta)
900                weights[0] = max(min(weights[0], 1024), -1024)
901                weights[1] = max(min(weights[1], 1024), -1024)
902        elif term == -2:
903            for i in range(len(correlated[0])):
904                decorrelated[1].append(apply_weight(weights[1],
905                                                    decorrelated[0][i]) +
906                                       correlated[1][i])
907                decorrelated[0].append(apply_weight(weights[0],
908                                                    decorrelated[1][i + 1]) +
909                                       correlated[0][i])
910
911                weights[1] += update_weight(decorrelated[0][i],
912                                            correlated[1][i],
913                                            delta)
914
915                weights[0] += update_weight(decorrelated[1][i + 1],
916                                            correlated[0][i],
917                                            delta)
918                weights[1] = max(min(weights[1], 1024), -1024)
919                weights[0] = max(min(weights[0], 1024), -1024)
920        elif term == -3:
921            for i in range(len(correlated[0])):
922                decorrelated[0].append(apply_weight(weights[0],
923                                                    decorrelated[1][i]) +
924                                       correlated[0][i])
925                decorrelated[1].append(apply_weight(weights[1],
926                                                    decorrelated[0][i]) +
927                                       correlated[1][i])
928
929                weights[0] += update_weight(decorrelated[1][i],
930                                            correlated[0][i],
931                                            delta)
932                weights[1] += update_weight(decorrelated[0][i],
933                                            correlated[1][i],
934                                            delta)
935                weights[0] = max(min(weights[0], 1024), -1024)
936                weights[1] = max(min(weights[1], 1024), -1024)
937
938        assert(len(decorrelated[0]) == len(decorrelated[1]))
939        return (decorrelated[0][1:], decorrelated[1][1:])
940    else:
941        raise ValueError("unsupported term")
942
943
944def apply_weight(weight, sample):
945    return ((weight * sample) + 512) >> 10
946
947
948def update_weight(source, result, delta):
949    if (source == 0) or (result == 0):
950        return 0
951    elif (source ^ result) >= 0:
952        return delta
953    else:
954        return -delta
955
956
957def calculate_crc(samples):
958    crc = 0xFFFFFFFF
959
960    for frame in zip(*samples):
961        for s in frame:
962            crc = 3 * crc + s
963
964    if crc >= 0:
965        return crc % 0x100000000
966    else:
967        return (2 ** 32 - (-crc)) % 0x100000000
968