First commit

This commit is contained in:
Hajime Hoshi 2017-06-18 23:18:01 +09:00
commit a865f1b860
11 changed files with 2469 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
*~

201
LICENSE Normal file
View file

@ -0,0 +1,201 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "{}"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright {yyyy} {name of copyright owner}
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

3
README.md Normal file
View file

@ -0,0 +1,3 @@
# go-mp3
A MP3 decoder in pure Go based on [PDMP3](https://github.com/technosaurus/PDMP3).

198
decode.go Normal file
View file

@ -0,0 +1,198 @@
// Copyright 2017 Hajime Hoshi
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package mp3
import (
"io"
)
func (f *frame) decodeL3() []uint8 {
out := make([]uint8, 576*4*2)
nch := f.header.numberOfChannels()
for gr := 0; gr < 2; gr++ {
for ch := 0; ch < nch; ch++ {
f.l3Requantize(gr, ch)
// Reorder short blocks
f.l3Reorder(gr, ch)
}
f.l3Stereo(gr)
for ch := 0; ch < nch; ch++ {
f.l3Antialias(gr, ch)
// (IMDCT,windowing,overlapp add)
f.l3HybridSynthesis(gr, ch)
f.l3FrequencyInversion(gr, ch)
// Polyphase subband synthesis
f.l3SubbandSynthesis(gr, ch, out[576*4*gr:])
}
}
return out
}
type source struct {
reader io.ReadCloser
readerCache []uint8
readerPos int
readerEOF bool
}
func (s *source) Close() error {
return s.reader.Close()
}
func (s *source) rewind() error {
seeker := s.reader.(io.Seeker)
if _, err := seeker.Seek(0, io.SeekStart); err != nil {
return err
}
s.readerCache = nil
s.readerPos = 0
s.readerEOF = false
return nil
}
func (s *source) getByte() (uint8, error) {
for len(s.readerCache) == 0 && !s.readerEOF {
buf := make([]uint8, 4096)
n, err := s.reader.Read(buf)
s.readerCache = append(s.readerCache, buf[:n]...)
if err != nil {
if err == io.EOF {
s.readerEOF = true
break
}
return 0, err
}
}
if len(s.readerCache) == 0 {
return 0, io.EOF
}
b := s.readerCache[0]
s.readerCache = s.readerCache[1:]
s.readerPos++
return b, nil
}
func (s *source) getBytes(buf []int) (int, error) {
for i := range buf {
v, err := s.getByte()
buf[i] = int(v)
if err == io.EOF {
return i, io.EOF
}
}
return len(buf), nil
}
func (s *source) getFilepos() int {
return s.readerPos
}
type Decoded struct {
source *source
sampleRate int
length int64
buf []uint8
frame *frame
eof bool
}
func (d *Decoded) read() error {
var err error
d.frame, err = d.source.readNextFrame(d.frame)
if err != nil {
if err == io.EOF {
d.eof = true
}
return err
}
d.buf = append(d.buf, d.frame.decodeL3()...)
return nil
}
// Read is io.Reader's Read.
func (d *Decoded) Read(buf []uint8) (int, error) {
for len(d.buf) == 0 && !d.eof {
if err := d.read(); err != nil {
return 0, err
}
}
if d.eof {
return 0, io.EOF
}
n := copy(buf, d.buf)
d.buf = d.buf[n:]
return n, nil
}
// Close is io.Closer's Close.
func (d *Decoded) Close() error {
return d.source.Close()
}
// SampleRate returns the sample rate like 44100.
//
// Note that the sample rate is retrieved from the first frame.
func (d *Decoded) SampleRate() int {
return d.sampleRate
}
// Length returns the total size in bytes.
//
// Length returns -1 when the total size is not available
// e.g. when the given source is not io.Seeker.
func (d *Decoded) Length() int64 {
return d.length
}
// Decode decodes the given io.ReadCLoser and returns a decoded stream.
//
// The stream is always formatted as 16bit (little endian) 2 channels
// even if the source is single channel MP3.
// Thus, a sample always consists of 4 bytes.
//
// If r is io.Seeker, a decoded stream checks its length and Length returns a valid value.
func Decode(r io.ReadCloser) (*Decoded, error) {
s := &source{
reader: r,
}
d := &Decoded{
source: s,
length: -1,
}
if _, ok := r.(io.Seeker); ok {
l := int64(0)
var f *frame
for {
var err error
f, err = s.readNextFrame(f)
if err != nil {
if err == io.EOF {
break
}
return nil, err
}
l += 576 * 4 * 2
}
if err := s.rewind(); err != nil {
return nil, err
}
d.length = l
}
if err := d.read(); err != nil {
return nil, err
}
d.sampleRate = samplingFrequency[d.frame.header.sampling_frequency]
return d, nil
}

417
huffman.go Normal file
View file

@ -0,0 +1,417 @@
// Copyright 2017 Hajime Hoshi
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package mp3
import (
"fmt"
)
var huffmanTable = []uint16{
// 1
0x0201, 0x0000, 0x0201, 0x0010, 0x0201, 0x0001, 0x0011,
// 2
0x0201, 0x0000, 0x0401, 0x0201, 0x0010, 0x0001, 0x0201, 0x0011, 0x0401, 0x0201, 0x0020,
0x0021, 0x0201, 0x0012, 0x0201, 0x0002, 0x0022,
// 3
0x0401, 0x0201, 0x0000, 0x0001, 0x0201, 0x0011, 0x0201, 0x0010, 0x0401, 0x0201, 0x0020,
0x0021, 0x0201, 0x0012, 0x0201, 0x0002, 0x0022,
// 5
0x0201, 0x0000, 0x0401, 0x0201, 0x0010, 0x0001, 0x0201, 0x0011, 0x0801, 0x0401, 0x0201,
0x0020, 0x0002, 0x0201, 0x0021, 0x0012, 0x0801, 0x0401, 0x0201, 0x0022, 0x0030, 0x0201,
0x0003, 0x0013, 0x0201, 0x0031, 0x0201, 0x0032, 0x0201, 0x0023, 0x0033,
// 6
0x0601, 0x0401, 0x0201, 0x0000, 0x0010, 0x0011, 0x0601, 0x0201, 0x0001, 0x0201, 0x0020,
0x0021, 0x0601, 0x0201, 0x0012, 0x0201, 0x0002, 0x0022, 0x0401, 0x0201, 0x0031, 0x0013,
0x0401, 0x0201, 0x0030, 0x0032, 0x0201, 0x0023, 0x0201, 0x0003, 0x0033,
// 7
0x0201, 0x0000, 0x0401, 0x0201, 0x0010, 0x0001, 0x0801, 0x0201, 0x0011, 0x0401, 0x0201,
0x0020, 0x0002, 0x0021, 0x1201, 0x0601, 0x0201, 0x0012, 0x0201, 0x0022, 0x0030, 0x0401,
0x0201, 0x0031, 0x0013, 0x0401, 0x0201, 0x0003, 0x0032, 0x0201, 0x0023, 0x0004, 0x0a01,
0x0401, 0x0201, 0x0040, 0x0041, 0x0201, 0x0014, 0x0201, 0x0042, 0x0024, 0x0c01, 0x0601,
0x0401, 0x0201, 0x0033, 0x0043, 0x0050, 0x0401, 0x0201, 0x0034, 0x0005, 0x0051, 0x0601,
0x0201, 0x0015, 0x0201, 0x0052, 0x0025, 0x0401, 0x0201, 0x0044, 0x0035, 0x0401, 0x0201,
0x0053, 0x0054, 0x0201, 0x0045, 0x0055,
// 8
0x0601, 0x0201, 0x0000, 0x0201, 0x0010, 0x0001, 0x0201, 0x0011, 0x0401, 0x0201, 0x0021,
0x0012, 0x0e01, 0x0401, 0x0201, 0x0020, 0x0002, 0x0201, 0x0022, 0x0401, 0x0201, 0x0030,
0x0003, 0x0201, 0x0031, 0x0013, 0x0e01, 0x0801, 0x0401, 0x0201, 0x0032, 0x0023, 0x0201,
0x0040, 0x0004, 0x0201, 0x0041, 0x0201, 0x0014, 0x0042, 0x0c01, 0x0601, 0x0201, 0x0024,
0x0201, 0x0033, 0x0050, 0x0401, 0x0201, 0x0043, 0x0034, 0x0051, 0x0601, 0x0201, 0x0015,
0x0201, 0x0005, 0x0052, 0x0601, 0x0201, 0x0025, 0x0201, 0x0044, 0x0035, 0x0201, 0x0053,
0x0201, 0x0045, 0x0201, 0x0054, 0x0055,
// 9
0x0801, 0x0401, 0x0201, 0x0000, 0x0010, 0x0201, 0x0001, 0x0011, 0x0a01, 0x0401, 0x0201,
0x0020, 0x0021, 0x0201, 0x0012, 0x0201, 0x0002, 0x0022, 0x0c01, 0x0601, 0x0401, 0x0201,
0x0030, 0x0003, 0x0031, 0x0201, 0x0013, 0x0201, 0x0032, 0x0023, 0x0c01, 0x0401, 0x0201,
0x0041, 0x0014, 0x0401, 0x0201, 0x0040, 0x0033, 0x0201, 0x0042, 0x0024, 0x0a01, 0x0601,
0x0401, 0x0201, 0x0004, 0x0050, 0x0043, 0x0201, 0x0034, 0x0051, 0x0801, 0x0401, 0x0201,
0x0015, 0x0052, 0x0201, 0x0025, 0x0044, 0x0601, 0x0401, 0x0201, 0x0005, 0x0054, 0x0053,
0x0201, 0x0035, 0x0201, 0x0045, 0x0055,
// 10
0x0201, 0x0000, 0x0401, 0x0201, 0x0010, 0x0001, 0x0a01, 0x0201, 0x0011, 0x0401, 0x0201,
0x0020, 0x0002, 0x0201, 0x0021, 0x0012, 0x1c01, 0x0801, 0x0401, 0x0201, 0x0022, 0x0030,
0x0201, 0x0031, 0x0013, 0x0801, 0x0401, 0x0201, 0x0003, 0x0032, 0x0201, 0x0023, 0x0040,
0x0401, 0x0201, 0x0041, 0x0014, 0x0401, 0x0201, 0x0004, 0x0033, 0x0201, 0x0042, 0x0024,
0x1c01, 0x0a01, 0x0601, 0x0401, 0x0201, 0x0050, 0x0005, 0x0060, 0x0201, 0x0061, 0x0016,
0x0c01, 0x0601, 0x0401, 0x0201, 0x0043, 0x0034, 0x0051, 0x0201, 0x0015, 0x0201, 0x0052,
0x0025, 0x0401, 0x0201, 0x0026, 0x0036, 0x0071, 0x1401, 0x0801, 0x0201, 0x0017, 0x0401,
0x0201, 0x0044, 0x0053, 0x0006, 0x0601, 0x0401, 0x0201, 0x0035, 0x0045, 0x0062, 0x0201,
0x0070, 0x0201, 0x0007, 0x0064, 0x0e01, 0x0401, 0x0201, 0x0072, 0x0027, 0x0601, 0x0201,
0x0063, 0x0201, 0x0054, 0x0055, 0x0201, 0x0046, 0x0073, 0x0801, 0x0401, 0x0201, 0x0037,
0x0065, 0x0201, 0x0056, 0x0074, 0x0601, 0x0201, 0x0047, 0x0201, 0x0066, 0x0075, 0x0401,
0x0201, 0x0057, 0x0076, 0x0201, 0x0067, 0x0077,
// 11
0x0601, 0x0201, 0x0000, 0x0201, 0x0010, 0x0001, 0x0801, 0x0201, 0x0011, 0x0401, 0x0201,
0x0020, 0x0002, 0x0012, 0x1801, 0x0801, 0x0201, 0x0021, 0x0201, 0x0022, 0x0201, 0x0030,
0x0003, 0x0401, 0x0201, 0x0031, 0x0013, 0x0401, 0x0201, 0x0032, 0x0023, 0x0401, 0x0201,
0x0040, 0x0004, 0x0201, 0x0041, 0x0014, 0x1e01, 0x1001, 0x0a01, 0x0401, 0x0201, 0x0042,
0x0024, 0x0401, 0x0201, 0x0033, 0x0043, 0x0050, 0x0401, 0x0201, 0x0034, 0x0051, 0x0061,
0x0601, 0x0201, 0x0016, 0x0201, 0x0006, 0x0026, 0x0201, 0x0062, 0x0201, 0x0015, 0x0201,
0x0005, 0x0052, 0x1001, 0x0a01, 0x0601, 0x0401, 0x0201, 0x0025, 0x0044, 0x0060, 0x0201,
0x0063, 0x0036, 0x0401, 0x0201, 0x0070, 0x0017, 0x0071, 0x1001, 0x0601, 0x0401, 0x0201,
0x0007, 0x0064, 0x0072, 0x0201, 0x0027, 0x0401, 0x0201, 0x0053, 0x0035, 0x0201, 0x0054,
0x0045, 0x0a01, 0x0401, 0x0201, 0x0046, 0x0073, 0x0201, 0x0037, 0x0201, 0x0065, 0x0056,
0x0a01, 0x0601, 0x0401, 0x0201, 0x0055, 0x0057, 0x0074, 0x0201, 0x0047, 0x0066, 0x0401,
0x0201, 0x0075, 0x0076, 0x0201, 0x0067, 0x0077,
// 12
0x0c01, 0x0401, 0x0201, 0x0010, 0x0001, 0x0201, 0x0011, 0x0201, 0x0000, 0x0201, 0x0020,
0x0002, 0x1001, 0x0401, 0x0201, 0x0021, 0x0012, 0x0401, 0x0201, 0x0022, 0x0031, 0x0201,
0x0013, 0x0201, 0x0030, 0x0201, 0x0003, 0x0040, 0x1a01, 0x0801, 0x0401, 0x0201, 0x0032,
0x0023, 0x0201, 0x0041, 0x0033, 0x0a01, 0x0401, 0x0201, 0x0014, 0x0042, 0x0201, 0x0024,
0x0201, 0x0004, 0x0050, 0x0401, 0x0201, 0x0043, 0x0034, 0x0201, 0x0051, 0x0015, 0x1c01,
0x0e01, 0x0801, 0x0401, 0x0201, 0x0052, 0x0025, 0x0201, 0x0053, 0x0035, 0x0401, 0x0201,
0x0060, 0x0016, 0x0061, 0x0401, 0x0201, 0x0062, 0x0026, 0x0601, 0x0401, 0x0201, 0x0005,
0x0006, 0x0044, 0x0201, 0x0054, 0x0045, 0x1201, 0x0a01, 0x0401, 0x0201, 0x0063, 0x0036,
0x0401, 0x0201, 0x0070, 0x0007, 0x0071, 0x0401, 0x0201, 0x0017, 0x0064, 0x0201, 0x0046,
0x0072, 0x0a01, 0x0601, 0x0201, 0x0027, 0x0201, 0x0055, 0x0073, 0x0201, 0x0037, 0x0056,
0x0801, 0x0401, 0x0201, 0x0065, 0x0074, 0x0201, 0x0047, 0x0066, 0x0401, 0x0201, 0x0075,
0x0057, 0x0201, 0x0076, 0x0201, 0x0067, 0x0077,
// 13
0x0201, 0x0000, 0x0601, 0x0201, 0x0010, 0x0201, 0x0001, 0x0011, 0x1c01, 0x0801, 0x0401,
0x0201, 0x0020, 0x0002, 0x0201, 0x0021, 0x0012, 0x0801, 0x0401, 0x0201, 0x0022, 0x0030,
0x0201, 0x0003, 0x0031, 0x0601, 0x0201, 0x0013, 0x0201, 0x0032, 0x0023, 0x0401, 0x0201,
0x0040, 0x0004, 0x0041, 0x4601, 0x1c01, 0x0e01, 0x0601, 0x0201, 0x0014, 0x0201, 0x0033,
0x0042, 0x0401, 0x0201, 0x0024, 0x0050, 0x0201, 0x0043, 0x0034, 0x0401, 0x0201, 0x0051,
0x0015, 0x0401, 0x0201, 0x0005, 0x0052, 0x0201, 0x0025, 0x0201, 0x0044, 0x0053, 0x0e01,
0x0801, 0x0401, 0x0201, 0x0060, 0x0006, 0x0201, 0x0061, 0x0016, 0x0401, 0x0201, 0x0080,
0x0008, 0x0081, 0x1001, 0x0801, 0x0401, 0x0201, 0x0035, 0x0062, 0x0201, 0x0026, 0x0054,
0x0401, 0x0201, 0x0045, 0x0063, 0x0201, 0x0036, 0x0070, 0x0601, 0x0401, 0x0201, 0x0007,
0x0055, 0x0071, 0x0201, 0x0017, 0x0201, 0x0027, 0x0037, 0x4801, 0x1801, 0x0c01, 0x0401,
0x0201, 0x0018, 0x0082, 0x0201, 0x0028, 0x0401, 0x0201, 0x0064, 0x0046, 0x0072, 0x0801,
0x0401, 0x0201, 0x0084, 0x0048, 0x0201, 0x0090, 0x0009, 0x0201, 0x0091, 0x0019, 0x1801,
0x0e01, 0x0801, 0x0401, 0x0201, 0x0073, 0x0065, 0x0201, 0x0056, 0x0074, 0x0401, 0x0201,
0x0047, 0x0066, 0x0083, 0x0601, 0x0201, 0x0038, 0x0201, 0x0075, 0x0057, 0x0201, 0x0092,
0x0029, 0x0e01, 0x0801, 0x0401, 0x0201, 0x0067, 0x0085, 0x0201, 0x0058, 0x0039, 0x0201,
0x0093, 0x0201, 0x0049, 0x0086, 0x0601, 0x0201, 0x00a0, 0x0201, 0x0068, 0x000a, 0x0201,
0x00a1, 0x001a, 0x4401, 0x1801, 0x0c01, 0x0401, 0x0201, 0x00a2, 0x002a, 0x0401, 0x0201,
0x0095, 0x0059, 0x0201, 0x00a3, 0x003a, 0x0801, 0x0401, 0x0201, 0x004a, 0x0096, 0x0201,
0x00b0, 0x000b, 0x0201, 0x00b1, 0x001b, 0x1401, 0x0801, 0x0201, 0x00b2, 0x0401, 0x0201,
0x0076, 0x0077, 0x0094, 0x0601, 0x0401, 0x0201, 0x0087, 0x0078, 0x00a4, 0x0401, 0x0201,
0x0069, 0x00a5, 0x002b, 0x0c01, 0x0601, 0x0401, 0x0201, 0x005a, 0x0088, 0x00b3, 0x0201,
0x003b, 0x0201, 0x0079, 0x00a6, 0x0601, 0x0401, 0x0201, 0x006a, 0x00b4, 0x00c0, 0x0401,
0x0201, 0x000c, 0x0098, 0x00c1, 0x3c01, 0x1601, 0x0a01, 0x0601, 0x0201, 0x001c, 0x0201,
0x0089, 0x00b5, 0x0201, 0x005b, 0x00c2, 0x0401, 0x0201, 0x002c, 0x003c, 0x0401, 0x0201,
0x00b6, 0x006b, 0x0201, 0x00c4, 0x004c, 0x1001, 0x0801, 0x0401, 0x0201, 0x00a8, 0x008a,
0x0201, 0x00d0, 0x000d, 0x0201, 0x00d1, 0x0201, 0x004b, 0x0201, 0x0097, 0x00a7, 0x0c01,
0x0601, 0x0201, 0x00c3, 0x0201, 0x007a, 0x0099, 0x0401, 0x0201, 0x00c5, 0x005c, 0x00b7,
0x0401, 0x0201, 0x001d, 0x00d2, 0x0201, 0x002d, 0x0201, 0x007b, 0x00d3, 0x3401, 0x1c01,
0x0c01, 0x0401, 0x0201, 0x003d, 0x00c6, 0x0401, 0x0201, 0x006c, 0x00a9, 0x0201, 0x009a,
0x00d4, 0x0801, 0x0401, 0x0201, 0x00b8, 0x008b, 0x0201, 0x004d, 0x00c7, 0x0401, 0x0201,
0x007c, 0x00d5, 0x0201, 0x005d, 0x00e0, 0x0a01, 0x0401, 0x0201, 0x00e1, 0x001e, 0x0401,
0x0201, 0x000e, 0x002e, 0x00e2, 0x0801, 0x0401, 0x0201, 0x00e3, 0x006d, 0x0201, 0x008c,
0x00e4, 0x0401, 0x0201, 0x00e5, 0x00ba, 0x00f0, 0x2601, 0x1001, 0x0401, 0x0201, 0x00f1,
0x001f, 0x0601, 0x0401, 0x0201, 0x00aa, 0x009b, 0x00b9, 0x0201, 0x003e, 0x0201, 0x00d6,
0x00c8, 0x0c01, 0x0601, 0x0201, 0x004e, 0x0201, 0x00d7, 0x007d, 0x0201, 0x00ab, 0x0201,
0x005e, 0x00c9, 0x0601, 0x0201, 0x000f, 0x0201, 0x009c, 0x006e, 0x0201, 0x00f2, 0x002f,
0x2001, 0x1001, 0x0601, 0x0401, 0x0201, 0x00d8, 0x008d, 0x003f, 0x0601, 0x0201, 0x00f3,
0x0201, 0x00e6, 0x00ca, 0x0201, 0x00f4, 0x004f, 0x0801, 0x0401, 0x0201, 0x00bb, 0x00ac,
0x0201, 0x00e7, 0x00f5, 0x0401, 0x0201, 0x00d9, 0x009d, 0x0201, 0x005f, 0x00e8, 0x1e01,
0x0c01, 0x0601, 0x0201, 0x006f, 0x0201, 0x00f6, 0x00cb, 0x0401, 0x0201, 0x00bc, 0x00ad,
0x00da, 0x0801, 0x0201, 0x00f7, 0x0401, 0x0201, 0x007e, 0x007f, 0x008e, 0x0601, 0x0401,
0x0201, 0x009e, 0x00ae, 0x00cc, 0x0201, 0x00f8, 0x008f, 0x1201, 0x0801, 0x0401, 0x0201,
0x00db, 0x00bd, 0x0201, 0x00ea, 0x00f9, 0x0401, 0x0201, 0x009f, 0x00eb, 0x0201, 0x00be,
0x0201, 0x00cd, 0x00fa, 0x0e01, 0x0401, 0x0201, 0x00dd, 0x00ec, 0x0601, 0x0401, 0x0201,
0x00e9, 0x00af, 0x00dc, 0x0201, 0x00ce, 0x00fb, 0x0801, 0x0401, 0x0201, 0x00bf, 0x00de,
0x0201, 0x00cf, 0x00ee, 0x0401, 0x0201, 0x00df, 0x00ef, 0x0201, 0x00ff, 0x0201, 0x00ed,
0x0201, 0x00fd, 0x0201, 0x00fc, 0x00fe,
// 15
0x1001, 0x0601, 0x0201, 0x0000, 0x0201, 0x0010, 0x0001, 0x0201, 0x0011, 0x0401, 0x0201,
0x0020, 0x0002, 0x0201, 0x0021, 0x0012, 0x3201, 0x1001, 0x0601, 0x0201, 0x0022, 0x0201,
0x0030, 0x0031, 0x0601, 0x0201, 0x0013, 0x0201, 0x0003, 0x0040, 0x0201, 0x0032, 0x0023,
0x0e01, 0x0601, 0x0401, 0x0201, 0x0004, 0x0014, 0x0041, 0x0401, 0x0201, 0x0033, 0x0042,
0x0201, 0x0024, 0x0043, 0x0a01, 0x0601, 0x0201, 0x0034, 0x0201, 0x0050, 0x0005, 0x0201,
0x0051, 0x0015, 0x0401, 0x0201, 0x0052, 0x0025, 0x0401, 0x0201, 0x0044, 0x0053, 0x0061,
0x5a01, 0x2401, 0x1201, 0x0a01, 0x0601, 0x0201, 0x0035, 0x0201, 0x0060, 0x0006, 0x0201,
0x0016, 0x0062, 0x0401, 0x0201, 0x0026, 0x0054, 0x0201, 0x0045, 0x0063, 0x0a01, 0x0601,
0x0201, 0x0036, 0x0201, 0x0070, 0x0007, 0x0201, 0x0071, 0x0055, 0x0401, 0x0201, 0x0017,
0x0064, 0x0201, 0x0072, 0x0027, 0x1801, 0x1001, 0x0801, 0x0401, 0x0201, 0x0046, 0x0073,
0x0201, 0x0037, 0x0065, 0x0401, 0x0201, 0x0056, 0x0080, 0x0201, 0x0008, 0x0074, 0x0401,
0x0201, 0x0081, 0x0018, 0x0201, 0x0082, 0x0028, 0x1001, 0x0801, 0x0401, 0x0201, 0x0047,
0x0066, 0x0201, 0x0083, 0x0038, 0x0401, 0x0201, 0x0075, 0x0057, 0x0201, 0x0084, 0x0048,
0x0601, 0x0401, 0x0201, 0x0090, 0x0019, 0x0091, 0x0401, 0x0201, 0x0092, 0x0076, 0x0201,
0x0067, 0x0029, 0x5c01, 0x2401, 0x1201, 0x0a01, 0x0401, 0x0201, 0x0085, 0x0058, 0x0401,
0x0201, 0x0009, 0x0077, 0x0093, 0x0401, 0x0201, 0x0039, 0x0094, 0x0201, 0x0049, 0x0086,
0x0a01, 0x0601, 0x0201, 0x0068, 0x0201, 0x00a0, 0x000a, 0x0201, 0x00a1, 0x001a, 0x0401,
0x0201, 0x00a2, 0x002a, 0x0201, 0x0095, 0x0059, 0x1a01, 0x0e01, 0x0601, 0x0201, 0x00a3,
0x0201, 0x003a, 0x0087, 0x0401, 0x0201, 0x0078, 0x00a4, 0x0201, 0x004a, 0x0096, 0x0601,
0x0401, 0x0201, 0x0069, 0x00b0, 0x00b1, 0x0401, 0x0201, 0x001b, 0x00a5, 0x00b2, 0x0e01,
0x0801, 0x0401, 0x0201, 0x005a, 0x002b, 0x0201, 0x0088, 0x0097, 0x0201, 0x00b3, 0x0201,
0x0079, 0x003b, 0x0801, 0x0401, 0x0201, 0x006a, 0x00b4, 0x0201, 0x004b, 0x00c1, 0x0401,
0x0201, 0x0098, 0x0089, 0x0201, 0x001c, 0x00b5, 0x5001, 0x2201, 0x1001, 0x0601, 0x0401,
0x0201, 0x005b, 0x002c, 0x00c2, 0x0601, 0x0401, 0x0201, 0x000b, 0x00c0, 0x00a6, 0x0201,
0x00a7, 0x007a, 0x0a01, 0x0401, 0x0201, 0x00c3, 0x003c, 0x0401, 0x0201, 0x000c, 0x0099,
0x00b6, 0x0401, 0x0201, 0x006b, 0x00c4, 0x0201, 0x004c, 0x00a8, 0x1401, 0x0a01, 0x0401,
0x0201, 0x008a, 0x00c5, 0x0401, 0x0201, 0x00d0, 0x005c, 0x00d1, 0x0401, 0x0201, 0x00b7,
0x007b, 0x0201, 0x001d, 0x0201, 0x000d, 0x002d, 0x0c01, 0x0401, 0x0201, 0x00d2, 0x00d3,
0x0401, 0x0201, 0x003d, 0x00c6, 0x0201, 0x006c, 0x00a9, 0x0601, 0x0401, 0x0201, 0x009a,
0x00b8, 0x00d4, 0x0401, 0x0201, 0x008b, 0x004d, 0x0201, 0x00c7, 0x007c, 0x4401, 0x2201,
0x1201, 0x0a01, 0x0401, 0x0201, 0x00d5, 0x005d, 0x0401, 0x0201, 0x00e0, 0x000e, 0x00e1,
0x0401, 0x0201, 0x001e, 0x00e2, 0x0201, 0x00aa, 0x002e, 0x0801, 0x0401, 0x0201, 0x00b9,
0x009b, 0x0201, 0x00e3, 0x00d6, 0x0401, 0x0201, 0x006d, 0x003e, 0x0201, 0x00c8, 0x008c,
0x1001, 0x0801, 0x0401, 0x0201, 0x00e4, 0x004e, 0x0201, 0x00d7, 0x007d, 0x0401, 0x0201,
0x00e5, 0x00ba, 0x0201, 0x00ab, 0x005e, 0x0801, 0x0401, 0x0201, 0x00c9, 0x009c, 0x0201,
0x00f1, 0x001f, 0x0601, 0x0401, 0x0201, 0x00f0, 0x006e, 0x00f2, 0x0201, 0x002f, 0x00e6,
0x2601, 0x1201, 0x0801, 0x0401, 0x0201, 0x00d8, 0x00f3, 0x0201, 0x003f, 0x00f4, 0x0601,
0x0201, 0x004f, 0x0201, 0x008d, 0x00d9, 0x0201, 0x00bb, 0x00ca, 0x0801, 0x0401, 0x0201,
0x00ac, 0x00e7, 0x0201, 0x007e, 0x00f5, 0x0801, 0x0401, 0x0201, 0x009d, 0x005f, 0x0201,
0x00e8, 0x008e, 0x0201, 0x00f6, 0x00cb, 0x2201, 0x1201, 0x0a01, 0x0601, 0x0401, 0x0201,
0x000f, 0x00ae, 0x006f, 0x0201, 0x00bc, 0x00da, 0x0401, 0x0201, 0x00ad, 0x00f7, 0x0201,
0x007f, 0x00e9, 0x0801, 0x0401, 0x0201, 0x009e, 0x00cc, 0x0201, 0x00f8, 0x008f, 0x0401,
0x0201, 0x00db, 0x00bd, 0x0201, 0x00ea, 0x00f9, 0x1001, 0x0801, 0x0401, 0x0201, 0x009f,
0x00dc, 0x0201, 0x00cd, 0x00eb, 0x0401, 0x0201, 0x00be, 0x00fa, 0x0201, 0x00af, 0x00dd,
0x0e01, 0x0601, 0x0401, 0x0201, 0x00ec, 0x00ce, 0x00fb, 0x0401, 0x0201, 0x00bf, 0x00ed,
0x0201, 0x00de, 0x00fc, 0x0601, 0x0401, 0x0201, 0x00cf, 0x00fd, 0x00ee, 0x0401, 0x0201,
0x00df, 0x00fe, 0x0201, 0x00ef, 0x00ff,
// 16
0x0201, 0x0000, 0x0601, 0x0201, 0x0010, 0x0201, 0x0001, 0x0011, 0x2a01, 0x0801, 0x0401,
0x0201, 0x0020, 0x0002, 0x0201, 0x0021, 0x0012, 0x0a01, 0x0601, 0x0201, 0x0022, 0x0201,
0x0030, 0x0003, 0x0201, 0x0031, 0x0013, 0x0a01, 0x0401, 0x0201, 0x0032, 0x0023, 0x0401,
0x0201, 0x0040, 0x0004, 0x0041, 0x0601, 0x0201, 0x0014, 0x0201, 0x0033, 0x0042, 0x0401,
0x0201, 0x0024, 0x0050, 0x0201, 0x0043, 0x0034, 0x8a01, 0x2801, 0x1001, 0x0601, 0x0401,
0x0201, 0x0005, 0x0015, 0x0051, 0x0401, 0x0201, 0x0052, 0x0025, 0x0401, 0x0201, 0x0044,
0x0035, 0x0053, 0x0a01, 0x0601, 0x0401, 0x0201, 0x0060, 0x0006, 0x0061, 0x0201, 0x0016,
0x0062, 0x0801, 0x0401, 0x0201, 0x0026, 0x0054, 0x0201, 0x0045, 0x0063, 0x0401, 0x0201,
0x0036, 0x0070, 0x0071, 0x2801, 0x1201, 0x0801, 0x0201, 0x0017, 0x0201, 0x0007, 0x0201,
0x0055, 0x0064, 0x0401, 0x0201, 0x0072, 0x0027, 0x0401, 0x0201, 0x0046, 0x0065, 0x0073,
0x0a01, 0x0601, 0x0201, 0x0037, 0x0201, 0x0056, 0x0008, 0x0201, 0x0080, 0x0081, 0x0601,
0x0201, 0x0018, 0x0201, 0x0074, 0x0047, 0x0201, 0x0082, 0x0201, 0x0028, 0x0066, 0x1801,
0x0e01, 0x0801, 0x0401, 0x0201, 0x0083, 0x0038, 0x0201, 0x0075, 0x0084, 0x0401, 0x0201,
0x0048, 0x0090, 0x0091, 0x0601, 0x0201, 0x0019, 0x0201, 0x0009, 0x0076, 0x0201, 0x0092,
0x0029, 0x0e01, 0x0801, 0x0401, 0x0201, 0x0085, 0x0058, 0x0201, 0x0093, 0x0039, 0x0401,
0x0201, 0x00a0, 0x000a, 0x001a, 0x0801, 0x0201, 0x00a2, 0x0201, 0x0067, 0x0201, 0x0057,
0x0049, 0x0601, 0x0201, 0x0094, 0x0201, 0x0077, 0x0086, 0x0201, 0x00a1, 0x0201, 0x0068,
0x0095, 0xdc01, 0x7e01, 0x3201, 0x1a01, 0x0c01, 0x0601, 0x0201, 0x002a, 0x0201, 0x0059,
0x003a, 0x0201, 0x00a3, 0x0201, 0x0087, 0x0078, 0x0801, 0x0401, 0x0201, 0x00a4, 0x004a,
0x0201, 0x0096, 0x0069, 0x0401, 0x0201, 0x00b0, 0x000b, 0x00b1, 0x0a01, 0x0401, 0x0201,
0x001b, 0x00b2, 0x0201, 0x002b, 0x0201, 0x00a5, 0x005a, 0x0601, 0x0201, 0x00b3, 0x0201,
0x00a6, 0x006a, 0x0401, 0x0201, 0x00b4, 0x004b, 0x0201, 0x000c, 0x00c1, 0x1e01, 0x0e01,
0x0601, 0x0401, 0x0201, 0x00b5, 0x00c2, 0x002c, 0x0401, 0x0201, 0x00a7, 0x00c3, 0x0201,
0x006b, 0x00c4, 0x0801, 0x0201, 0x001d, 0x0401, 0x0201, 0x0088, 0x0097, 0x003b, 0x0401,
0x0201, 0x00d1, 0x00d2, 0x0201, 0x002d, 0x00d3, 0x1201, 0x0601, 0x0401, 0x0201, 0x001e,
0x002e, 0x00e2, 0x0601, 0x0401, 0x0201, 0x0079, 0x0098, 0x00c0, 0x0201, 0x001c, 0x0201,
0x0089, 0x005b, 0x0e01, 0x0601, 0x0201, 0x003c, 0x0201, 0x007a, 0x00b6, 0x0401, 0x0201,
0x004c, 0x0099, 0x0201, 0x00a8, 0x008a, 0x0601, 0x0201, 0x000d, 0x0201, 0x00c5, 0x005c,
0x0401, 0x0201, 0x003d, 0x00c6, 0x0201, 0x006c, 0x009a, 0x5801, 0x5601, 0x2401, 0x1001,
0x0801, 0x0401, 0x0201, 0x008b, 0x004d, 0x0201, 0x00c7, 0x007c, 0x0401, 0x0201, 0x00d5,
0x005d, 0x0201, 0x00e0, 0x000e, 0x0801, 0x0201, 0x00e3, 0x0401, 0x0201, 0x00d0, 0x00b7,
0x007b, 0x0601, 0x0401, 0x0201, 0x00a9, 0x00b8, 0x00d4, 0x0201, 0x00e1, 0x0201, 0x00aa,
0x00b9, 0x1801, 0x0a01, 0x0601, 0x0401, 0x0201, 0x009b, 0x00d6, 0x006d, 0x0201, 0x003e,
0x00c8, 0x0601, 0x0401, 0x0201, 0x008c, 0x00e4, 0x004e, 0x0401, 0x0201, 0x00d7, 0x00e5,
0x0201, 0x00ba, 0x00ab, 0x0c01, 0x0401, 0x0201, 0x009c, 0x00e6, 0x0401, 0x0201, 0x006e,
0x00d8, 0x0201, 0x008d, 0x00bb, 0x0801, 0x0401, 0x0201, 0x00e7, 0x009d, 0x0201, 0x00e8,
0x008e, 0x0401, 0x0201, 0x00cb, 0x00bc, 0x009e, 0x00f1, 0x0201, 0x001f, 0x0201, 0x000f,
0x002f, 0x4201, 0x3801, 0x0201, 0x00f2, 0x3401, 0x3201, 0x1401, 0x0801, 0x0201, 0x00bd,
0x0201, 0x005e, 0x0201, 0x007d, 0x00c9, 0x0601, 0x0201, 0x00ca, 0x0201, 0x00ac, 0x007e,
0x0401, 0x0201, 0x00da, 0x00ad, 0x00cc, 0x0a01, 0x0601, 0x0201, 0x00ae, 0x0201, 0x00db,
0x00dc, 0x0201, 0x00cd, 0x00be, 0x0601, 0x0401, 0x0201, 0x00eb, 0x00ed, 0x00ee, 0x0601,
0x0401, 0x0201, 0x00d9, 0x00ea, 0x00e9, 0x0201, 0x00de, 0x0401, 0x0201, 0x00dd, 0x00ec,
0x00ce, 0x003f, 0x00f0, 0x0401, 0x0201, 0x00f3, 0x00f4, 0x0201, 0x004f, 0x0201, 0x00f5,
0x005f, 0x0a01, 0x0201, 0x00ff, 0x0401, 0x0201, 0x00f6, 0x006f, 0x0201, 0x00f7, 0x007f,
0x0c01, 0x0601, 0x0201, 0x008f, 0x0201, 0x00f8, 0x00f9, 0x0401, 0x0201, 0x009f, 0x00fa,
0x00af, 0x0801, 0x0401, 0x0201, 0x00fb, 0x00bf, 0x0201, 0x00fc, 0x00cf, 0x0401, 0x0201,
0x00fd, 0x00df, 0x0201, 0x00fe, 0x00ef,
// 24
0x3c01, 0x0801, 0x0401, 0x0201, 0x0000, 0x0010, 0x0201, 0x0001, 0x0011, 0x0e01, 0x0601,
0x0401, 0x0201, 0x0020, 0x0002, 0x0021, 0x0201, 0x0012, 0x0201, 0x0022, 0x0201, 0x0030,
0x0003, 0x0e01, 0x0401, 0x0201, 0x0031, 0x0013, 0x0401, 0x0201, 0x0032, 0x0023, 0x0401,
0x0201, 0x0040, 0x0004, 0x0041, 0x0801, 0x0401, 0x0201, 0x0014, 0x0033, 0x0201, 0x0042,
0x0024, 0x0601, 0x0401, 0x0201, 0x0043, 0x0034, 0x0051, 0x0601, 0x0401, 0x0201, 0x0050,
0x0005, 0x0015, 0x0201, 0x0052, 0x0025, 0xfa01, 0x6201, 0x2201, 0x1201, 0x0a01, 0x0401,
0x0201, 0x0044, 0x0053, 0x0201, 0x0035, 0x0201, 0x0060, 0x0006, 0x0401, 0x0201, 0x0061,
0x0016, 0x0201, 0x0062, 0x0026, 0x0801, 0x0401, 0x0201, 0x0054, 0x0045, 0x0201, 0x0063,
0x0036, 0x0401, 0x0201, 0x0071, 0x0055, 0x0201, 0x0064, 0x0046, 0x2001, 0x0e01, 0x0601,
0x0201, 0x0072, 0x0201, 0x0027, 0x0037, 0x0201, 0x0073, 0x0401, 0x0201, 0x0070, 0x0007,
0x0017, 0x0a01, 0x0401, 0x0201, 0x0065, 0x0056, 0x0401, 0x0201, 0x0080, 0x0008, 0x0081,
0x0401, 0x0201, 0x0074, 0x0047, 0x0201, 0x0018, 0x0082, 0x1001, 0x0801, 0x0401, 0x0201,
0x0028, 0x0066, 0x0201, 0x0083, 0x0038, 0x0401, 0x0201, 0x0075, 0x0057, 0x0201, 0x0084,
0x0048, 0x0801, 0x0401, 0x0201, 0x0091, 0x0019, 0x0201, 0x0092, 0x0076, 0x0401, 0x0201,
0x0067, 0x0029, 0x0201, 0x0085, 0x0058, 0x5c01, 0x2201, 0x1001, 0x0801, 0x0401, 0x0201,
0x0093, 0x0039, 0x0201, 0x0094, 0x0049, 0x0401, 0x0201, 0x0077, 0x0086, 0x0201, 0x0068,
0x00a1, 0x0801, 0x0401, 0x0201, 0x00a2, 0x002a, 0x0201, 0x0095, 0x0059, 0x0401, 0x0201,
0x00a3, 0x003a, 0x0201, 0x0087, 0x0201, 0x0078, 0x004a, 0x1601, 0x0c01, 0x0401, 0x0201,
0x00a4, 0x0096, 0x0401, 0x0201, 0x0069, 0x00b1, 0x0201, 0x001b, 0x00a5, 0x0601, 0x0201,
0x00b2, 0x0201, 0x005a, 0x002b, 0x0201, 0x0088, 0x00b3, 0x1001, 0x0a01, 0x0601, 0x0201,
0x0090, 0x0201, 0x0009, 0x00a0, 0x0201, 0x0097, 0x0079, 0x0401, 0x0201, 0x00a6, 0x006a,
0x00b4, 0x0c01, 0x0601, 0x0201, 0x001a, 0x0201, 0x000a, 0x00b0, 0x0201, 0x003b, 0x0201,
0x000b, 0x00c0, 0x0401, 0x0201, 0x004b, 0x00c1, 0x0201, 0x0098, 0x0089, 0x4301, 0x2201,
0x1001, 0x0801, 0x0401, 0x0201, 0x001c, 0x00b5, 0x0201, 0x005b, 0x00c2, 0x0401, 0x0201,
0x002c, 0x00a7, 0x0201, 0x007a, 0x00c3, 0x0a01, 0x0601, 0x0201, 0x003c, 0x0201, 0x000c,
0x00d0, 0x0201, 0x00b6, 0x006b, 0x0401, 0x0201, 0x00c4, 0x004c, 0x0201, 0x0099, 0x00a8,
0x1001, 0x0801, 0x0401, 0x0201, 0x008a, 0x00c5, 0x0201, 0x005c, 0x00d1, 0x0401, 0x0201,
0x00b7, 0x007b, 0x0201, 0x001d, 0x00d2, 0x0901, 0x0401, 0x0201, 0x002d, 0x00d3, 0x0201,
0x003d, 0x00c6, 0x55fa, 0x0401, 0x0201, 0x006c, 0x00a9, 0x0201, 0x009a, 0x00d4, 0x2001,
0x1001, 0x0801, 0x0401, 0x0201, 0x00b8, 0x008b, 0x0201, 0x004d, 0x00c7, 0x0401, 0x0201,
0x007c, 0x00d5, 0x0201, 0x005d, 0x00e1, 0x0801, 0x0401, 0x0201, 0x001e, 0x00e2, 0x0201,
0x00aa, 0x00b9, 0x0401, 0x0201, 0x009b, 0x00e3, 0x0201, 0x00d6, 0x006d, 0x1401, 0x0a01,
0x0601, 0x0201, 0x003e, 0x0201, 0x002e, 0x004e, 0x0201, 0x00c8, 0x008c, 0x0401, 0x0201,
0x00e4, 0x00d7, 0x0401, 0x0201, 0x007d, 0x00ab, 0x00e5, 0x0a01, 0x0401, 0x0201, 0x00ba,
0x005e, 0x0201, 0x00c9, 0x0201, 0x009c, 0x006e, 0x0801, 0x0201, 0x00e6, 0x0201, 0x000d,
0x0201, 0x00e0, 0x000e, 0x0401, 0x0201, 0x00d8, 0x008d, 0x0201, 0x00bb, 0x00ca, 0x4a01,
0x0201, 0x00ff, 0x4001, 0x3a01, 0x2001, 0x1001, 0x0801, 0x0401, 0x0201, 0x00ac, 0x00e7,
0x0201, 0x007e, 0x00d9, 0x0401, 0x0201, 0x009d, 0x00e8, 0x0201, 0x008e, 0x00cb, 0x0801,
0x0401, 0x0201, 0x00bc, 0x00da, 0x0201, 0x00ad, 0x00e9, 0x0401, 0x0201, 0x009e, 0x00cc,
0x0201, 0x00db, 0x00bd, 0x1001, 0x0801, 0x0401, 0x0201, 0x00ea, 0x00ae, 0x0201, 0x00dc,
0x00cd, 0x0401, 0x0201, 0x00eb, 0x00be, 0x0201, 0x00dd, 0x00ec, 0x0801, 0x0401, 0x0201,
0x00ce, 0x00ed, 0x0201, 0x00de, 0x00ee, 0x000f, 0x0401, 0x0201, 0x00f0, 0x001f, 0x00f1,
0x0401, 0x0201, 0x00f2, 0x002f, 0x0201, 0x00f3, 0x003f, 0x1201, 0x0801, 0x0401, 0x0201,
0x00f4, 0x004f, 0x0201, 0x00f5, 0x005f, 0x0401, 0x0201, 0x00f6, 0x006f, 0x0201, 0x00f7,
0x0201, 0x007f, 0x008f, 0x0a01, 0x0401, 0x0201, 0x00f8, 0x00f9, 0x0401, 0x0201, 0x009f,
0x00af, 0x00fa, 0x0801, 0x0401, 0x0201, 0x00fb, 0x00bf, 0x0201, 0x00fc, 0x00cf, 0x0401,
0x0201, 0x00fd, 0x00df, 0x0201, 0x00fe, 0x00ef,
// 32
0x0201, 0x0000, 0x0801, 0x0401, 0x0201, 0x0008, 0x0004, 0x0201, 0x0001, 0x0002, 0x0801,
0x0401, 0x0201, 0x000c, 0x000a, 0x0201, 0x0003, 0x0006, 0x0601, 0x0201, 0x0009, 0x0201,
0x0005, 0x0007, 0x0401, 0x0201, 0x000e, 0x000d, 0x0201, 0x000f, 0x000b,
// 33
0x1001, 0x0801, 0x0401, 0x0201, 0x0000, 0x0001, 0x0201, 0x0002, 0x0003, 0x0401, 0x0201,
0x0004, 0x0005, 0x0201, 0x0006, 0x0007, 0x0801, 0x0401, 0x0201, 0x0008, 0x0009, 0x0201,
0x000a, 0x000b, 0x0401, 0x0201, 0x000c, 0x000d, 0x0201, 0x000e, 0x000f,
}
type huffTables struct {
hufftable []uint16
treelen int
linbits int
}
var huffmanMain = [...]huffTables{
{nil, 0, 0}, /* Table 0 */
{huffmanTable, 7, 0}, /* Table 1 */
{huffmanTable[7:], 17, 0}, /* Table 2 */
{huffmanTable[24:], 17, 0}, /* Table 3 */
{nil, 0, 0}, /* Table 4 */
{huffmanTable[41:], 31, 0}, /* Table 5 */
{huffmanTable[72:], 31, 0}, /* Table 6 */
{huffmanTable[103:], 71, 0}, /* Table 7 */
{huffmanTable[174:], 71, 0}, /* Table 8 */
{huffmanTable[245:], 71, 0}, /* Table 9 */
{huffmanTable[316:], 127, 0}, /* Table 10 */
{huffmanTable[443:], 127, 0}, /* Table 11 */
{huffmanTable[570:], 127, 0}, /* Table 12 */
{huffmanTable[697:], 511, 0}, /* Table 13 */
{nil, 0, 0}, /* Table 14 */
{huffmanTable[1208:], 511, 0}, /* Table 15 */
{huffmanTable[1719:], 511, 1}, /* Table 16 */
{huffmanTable[1719:], 511, 2}, /* Table 17 */
{huffmanTable[1719:], 511, 3}, /* Table 18 */
{huffmanTable[1719:], 511, 4}, /* Table 19 */
{huffmanTable[1719:], 511, 6}, /* Table 20 */
{huffmanTable[1719:], 511, 8}, /* Table 21 */
{huffmanTable[1719:], 511, 10}, /* Table 22 */
{huffmanTable[1719:], 511, 13}, /* Table 23 */
{huffmanTable[2230:], 512, 4}, /* Table 24 */
{huffmanTable[2230:], 512, 5}, /* Table 25 */
{huffmanTable[2230:], 512, 6}, /* Table 26 */
{huffmanTable[2230:], 512, 7}, /* Table 27 */
{huffmanTable[2230:], 512, 8}, /* Table 28 */
{huffmanTable[2230:], 512, 9}, /* Table 29 */
{huffmanTable[2230:], 512, 11}, /* Table 30 */
{huffmanTable[2230:], 512, 13}, /* Table 31 */
{huffmanTable[2742:], 31, 0}, /* Table 32 */
{huffmanTable[2261:], 31, 0}, /* Table 33 */
}
func huffmanDecode(m *mainDataBytes, table_num int) (x, y, v, w int, err error) {
point := 0
error := 1
bitsleft := 32
treelen := huffmanMain[table_num].treelen
linbits := huffmanMain[table_num].linbits
if treelen == 0 { /* Check for empty tables */
return 0, 0, 0, 0, nil
}
htptr := huffmanMain[table_num].hufftable
for { /* Start reading the Huffman code word,bit by bit */
/* Check if we've matched a code word */
if (htptr[point] & 0xff00) == 0 {
error = 0
x = int((htptr[point] >> 4) & 0xf)
y = int(htptr[point] & 0xf)
break
}
if m.getMainBit() != 0 { /* Go right in tree */
for (htptr[point] & 0xff) >= 250 {
point += int(htptr[point]) & 0xff
}
point += int(htptr[point]) & 0xff
} else { /* Go left in tree */
for (htptr[point] >> 8) >= 250 {
point += int(htptr[point]) >> 8
}
point += int(htptr[point]) >> 8
}
bitsleft--
if !((bitsleft > 0) && (point < treelen)) {
break
}
}
if error != 0 { /* Check for error. */
err := fmt.Errorf("mp3: illegal Huff code in data. bleft = %d, point = %d. tab = %d.",
bitsleft, point, table_num)
return 0, 0, 0, 0, err
}
if table_num > 31 { /* Process sign encodings for quadruples tables. */
v = (y >> 3) & 1
w = (y >> 2) & 1
x = (y >> 1) & 1
y = y & 1
if (v > 0) && (m.getMainBit() == 1) {
v = -v
}
if (w > 0) && (m.getMainBit() == 1) {
w = -w
}
if (x > 0) && (m.getMainBit() == 1) {
x = -x
}
if (y > 0) && (m.getMainBit() == 1) {
y = -y
}
} else {
if (linbits > 0) && (x == 15) {
x += m.getMainBits(linbits) /* Get linbits */
}
if (x > 0) && (m.getMainBit() == 1) {
x = -x /* Get sign bit */
}
if (linbits > 0) && (y == 15) {
y += m.getMainBits(linbits) /* Get linbits */
}
if (y > 0) && (m.getMainBit() == 1) {
y = -y /* Get sign bit */
}
}
return x, y, v, w, nil
}

250
imdct.go Normal file
View file

@ -0,0 +1,250 @@
// Copyright 2017 Hajime Hoshi
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package mp3
var imdctWinData = [4][36]float32{
{
0.043619, 0.130526, 0.216440, 0.300706, 0.382683, 0.461749,
0.537300, 0.608761, 0.675590, 0.737277, 0.793353, 0.843391,
0.887011, 0.923880, 0.953717, 0.976296, 0.991445, 0.999048,
0.999048, 0.991445, 0.976296, 0.953717, 0.923879, 0.887011,
0.843391, 0.793353, 0.737277, 0.675590, 0.608761, 0.537299,
0.461748, 0.382683, 0.300706, 0.216439, 0.130526, 0.043619,
},
{
0.043619, 0.130526, 0.216440, 0.300706, 0.382683, 0.461749,
0.537300, 0.608761, 0.675590, 0.737277, 0.793353, 0.843391,
0.887011, 0.923880, 0.953717, 0.976296, 0.991445, 0.999048,
1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
0.991445, 0.923880, 0.793353, 0.608761, 0.382683, 0.130526,
0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000,
},
{
0.130526, 0.382683, 0.608761, 0.793353, 0.923880, 0.991445,
0.991445, 0.923880, 0.793353, 0.608761, 0.382683, 0.130526,
0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000,
0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000,
0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000,
0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000,
},
{
0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000,
0.130526, 0.382683, 0.608761, 0.793353, 0.923880, 0.991445,
1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
0.999048, 0.991445, 0.976296, 0.953717, 0.923879, 0.887011,
0.843391, 0.793353, 0.737277, 0.675590, 0.608761, 0.537299,
0.461748, 0.382683, 0.300706, 0.216439, 0.130526, 0.043619,
},
}
var cosN12 = [6][12]float32{
{
0.608761, 0.382683, 0.130526, -0.130526, -0.382683, -0.608761,
-0.793353, -0.923880, -0.991445, -0.991445, -0.923879, -0.793353,
},
{
-0.923880, -0.923879, -0.382683, 0.382684, 0.923880, 0.923879,
0.382683, -0.382684, -0.923880, -0.923879, -0.382683, 0.382684,
},
{
-0.130526, 0.923880, 0.608761, -0.608762, -0.923879, 0.130526,
0.991445, 0.382683, -0.793354, -0.793353, 0.382684, 0.991445,
},
{
0.991445, -0.382684, -0.793353, 0.793354, 0.382683, -0.991445,
0.130527, 0.923879, -0.608762, -0.608761, 0.923880, 0.130525,
},
{
-0.382684, -0.382683, 0.923879, -0.923880, 0.382684, 0.382683,
-0.923879, 0.923880, -0.382684, -0.382683, 0.923879, -0.923880,
},
{
-0.793353, 0.923879, -0.991445, 0.991445, -0.923880, 0.793354,
-0.608762, 0.382684, -0.130527, -0.130525, 0.382682, -0.608761,
},
}
var cosN36 = [18][36]float32{
{
0.675590, 0.608761, 0.537300, 0.461749, 0.382683, 0.300706,
0.216440, 0.130526, 0.043619, -0.043619, -0.130526, -0.216440,
-0.300706, -0.382684, -0.461749, -0.537300, -0.608762, -0.675590,
-0.737277, -0.793353, -0.843392, -0.887011, -0.923880, -0.953717,
-0.976296, -0.991445, -0.999048, -0.999048, -0.991445, -0.976296,
-0.953717, -0.923879, -0.887011, -0.843391, -0.793353, -0.737277,
},
{
-0.793353, -0.923880, -0.991445, -0.991445, -0.923879, -0.793353,
-0.608761, -0.382683, -0.130526, 0.130526, 0.382684, 0.608762,
0.793354, 0.923880, 0.991445, 0.991445, 0.923879, 0.793353,
0.608761, 0.382683, 0.130526, -0.130527, -0.382684, -0.608762,
-0.793354, -0.923880, -0.991445, -0.991445, -0.923879, -0.793353,
-0.608761, -0.382683, -0.130526, 0.130527, 0.382684, 0.608762,
},
{
-0.537299, -0.130526, 0.300706, 0.675590, 0.923880, 0.999048,
0.887011, 0.608761, 0.216439, -0.216440, -0.608762, -0.887011,
-0.999048, -0.923879, -0.675590, -0.300705, 0.130527, 0.537300,
0.843392, 0.991445, 0.953717, 0.737277, 0.382683, -0.043620,
-0.461749, -0.793354, -0.976296, -0.976296, -0.793353, -0.461748,
-0.043618, 0.382684, 0.737278, 0.953717, 0.991445, 0.843391,
},
{
0.887011, 0.991445, 0.737277, 0.216439, -0.382684, -0.843392,
-0.999048, -0.793353, -0.300705, 0.300706, 0.793354, 0.999048,
0.843391, 0.382683, -0.216440, -0.737278, -0.991445, -0.887010,
-0.461748, 0.130527, 0.675591, 0.976296, 0.923879, 0.537299,
-0.043621, -0.608762, -0.953717, -0.953717, -0.608760, -0.043618,
0.537301, 0.923880, 0.976296, 0.675589, 0.130525, -0.461750,
},
{
0.382683, -0.382684, -0.923880, -0.923879, -0.382683, 0.382684,
0.923880, 0.923879, 0.382683, -0.382684, -0.923880, -0.923879,
-0.382683, 0.382684, 0.923880, 0.923879, 0.382682, -0.382685,
-0.923880, -0.923879, -0.382682, 0.382685, 0.923880, 0.923879,
0.382682, -0.382685, -0.923880, -0.923879, -0.382682, 0.382685,
0.923880, 0.923879, 0.382682, -0.382685, -0.923880, -0.923879,
},
{
0.953717, -0.793353, 0.043620, 0.843392, 0.923879, 0.216439,
-0.675591, -0.991445, -0.461748, 0.461749, 0.991445, 0.675589,
-0.216441, -0.923880, -0.843391, -0.043618, 0.793354, 0.953717,
0.300704, -0.608763, -0.999048, -0.537298, 0.382685, 0.976296,
0.737276, -0.130528, -0.887012, -0.887010, -0.130524, 0.737279,
0.976296, 0.382681, -0.537301, -0.999048, -0.608760, 0.300708,
},
{
-0.216439, 0.793354, 0.887010, -0.043620, -0.923880, -0.737277,
0.300707, 0.991445, 0.537299, -0.537301, -0.991445, -0.300705,
0.737278, 0.923879, 0.043618, -0.887012, -0.793352, 0.216441,
0.976296, 0.608760, -0.461750, -0.999048, -0.382682, 0.675592,
0.953716, 0.130524, -0.843393, -0.843390, 0.130529, 0.953718,
0.675588, -0.382686, -0.999048, -0.461746, 0.608764, 0.976295,
},
{
0.991445, 0.382683, -0.793354, -0.793353, 0.382684, 0.991445,
0.130525, -0.923880, -0.608760, 0.608763, 0.923879, -0.130528,
-0.991445, -0.382682, 0.793354, 0.793352, -0.382685, -0.991445,
-0.130524, 0.923880, 0.608760, -0.608763, -0.923879, 0.130529,
0.991445, 0.382681, -0.793355, -0.793352, 0.382686, 0.991444,
0.130523, -0.923881, -0.608759, 0.608764, 0.923878, -0.130529,
},
{
0.043619, -0.991445, -0.216439, 0.953717, 0.382682, -0.887011,
-0.537299, 0.793354, 0.675589, -0.675591, -0.793352, 0.537301,
0.887010, -0.382685, -0.953716, 0.216442, 0.991445, -0.043622,
-0.999048, -0.130524, 0.976297, 0.300703, -0.923881, -0.461746,
0.843393, 0.608759, -0.737279, -0.737275, 0.608764, 0.843390,
-0.461752, -0.923878, 0.300709, 0.976295, -0.130530, -0.999048,
},
{
-0.999048, 0.130527, 0.976296, -0.300707, -0.923879, 0.461750,
0.843391, -0.608763, -0.737276, 0.737279, 0.608760, -0.843392,
-0.461747, 0.923880, 0.300704, -0.976297, -0.130524, 0.999048,
-0.043622, -0.991445, 0.216442, 0.953716, -0.382686, -0.887009,
0.537302, 0.793351, -0.675593, -0.675588, 0.793355, 0.537297,
-0.887013, -0.382680, 0.953718, 0.216436, -0.991445, -0.043615,
},
{
0.130527, 0.923879, -0.608762, -0.608760, 0.923880, 0.130525,
-0.991445, 0.382685, 0.793352, -0.793355, -0.382682, 0.991445,
-0.130528, -0.923879, 0.608763, 0.608759, -0.923881, -0.130523,
0.991444, -0.382686, -0.793351, 0.793355, 0.382680, -0.991445,
0.130530, 0.923878, -0.608764, -0.608758, 0.923881, 0.130522,
-0.991444, 0.382687, 0.793351, -0.793356, -0.382679, 0.991445,
},
{
0.976296, -0.608762, -0.461747, 0.999048, -0.382685, -0.675589,
0.953717, -0.130528, -0.843390, 0.843393, 0.130524, -0.953716,
0.675592, 0.382681, -0.999048, 0.461751, 0.608759, -0.976297,
0.216443, 0.793351, -0.887012, -0.043616, 0.923878, -0.737280,
-0.300702, 0.991444, -0.537303, -0.537296, 0.991445, -0.300710,
-0.737274, 0.923881, -0.043624, -0.887009, 0.793356, 0.216435,
},
{
-0.300707, -0.608760, 0.999048, -0.537301, -0.382682, 0.976296,
-0.737279, -0.130524, 0.887010, -0.887012, 0.130529, 0.737276,
-0.976297, 0.382686, 0.537297, -0.999048, 0.608764, 0.300703,
-0.953716, 0.793355, 0.043616, -0.843389, 0.923881, -0.216444,
-0.675587, 0.991445, -0.461752, -0.461745, 0.991444, -0.675594,
-0.216435, 0.923878, -0.843394, 0.043625, 0.793350, -0.953719,
},
{
-0.923879, 0.923880, -0.382685, -0.382682, 0.923879, -0.923880,
0.382685, 0.382681, -0.923879, 0.923880, -0.382686, -0.382681,
0.923878, -0.923881, 0.382686, 0.382680, -0.923878, 0.923881,
-0.382687, -0.382680, 0.923878, -0.923881, 0.382687, 0.382679,
-0.923878, 0.923881, -0.382688, -0.382679, 0.923878, -0.923881,
0.382688, 0.382678, -0.923877, 0.923882, -0.382689, -0.382678,
},
{
0.461750, 0.130525, -0.675589, 0.976296, -0.923880, 0.537301,
0.043617, -0.608760, 0.953716, -0.953718, 0.608764, -0.043622,
-0.537297, 0.923878, -0.976297, 0.675593, -0.130530, -0.461745,
0.887009, -0.991445, 0.737280, -0.216444, -0.382679, 0.843389,
-0.999048, 0.793356, -0.300711, -0.300701, 0.793350, -0.999048,
0.843394, -0.382689, -0.216434, 0.737273, -0.991444, 0.887014,
},
{
0.843391, -0.991445, 0.953717, -0.737279, 0.382685, 0.043617,
-0.461747, 0.793352, -0.976295, 0.976297, -0.793355, 0.461751,
-0.043623, -0.382680, 0.737275, -0.953716, 0.991445, -0.843394,
0.537303, -0.130530, -0.300702, 0.675587, -0.923878, 0.999048,
-0.887013, 0.608766, -0.216445, -0.216434, 0.608757, -0.887008,
0.999048, -0.923882, 0.675595, -0.300712, -0.130520, 0.537294,
},
{
-0.608763, 0.382685, -0.130528, -0.130524, 0.382681, -0.608760,
0.793352, -0.923879, 0.991444, -0.991445, 0.923881, -0.793355,
0.608764, -0.382687, 0.130530, 0.130522, -0.382680, 0.608758,
-0.793351, 0.923878, -0.991444, 0.991446, -0.923881, 0.793357,
-0.608766, 0.382689, -0.130532, -0.130520, 0.382678, -0.608756,
0.793349, -0.923877, 0.991444, -0.991446, 0.923882, -0.793358,
},
{
-0.737276, 0.793352, -0.843390, 0.887010, -0.923879, 0.953716,
-0.976295, 0.991444, -0.999048, 0.999048, -0.991445, 0.976297,
-0.953718, 0.923881, -0.887013, 0.843394, -0.793356, 0.737280,
-0.675594, 0.608765, -0.537304, 0.461753, -0.382688, 0.300711,
-0.216445, 0.130532, -0.043625, -0.043613, 0.130520, -0.216433,
0.300699, -0.382677, 0.461742, -0.537293, 0.608755, -0.675585,
},
}
func imdctWin(in []float32, blockType int) []float32 {
out := make([]float32, 36)
if blockType == 2 {
N := 12
for i := 0; i < 3; i++ {
for p := 0; p < N; p++ {
sum := float32(0.0)
for m := 0; m < N/2; m++ {
sum += in[i+3*m] * cosN12[m][p]
}
out[6*i+p+6] += sum * imdctWinData[blockType][p]
}
}
return out
}
N := 36
for p := 0; p < N; p++ {
sum := float32(0.0)
for m := 0; m < N/2; m++ {
sum += in[m] * cosN36[m][p]
}
out[p] = sum * imdctWinData[blockType][p]
}
return out
}

609
l3.go Normal file
View file

@ -0,0 +1,609 @@
// Copyright 2017 Hajime Hoshi
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package mp3
import (
"math"
)
var (
powtab34 = make([]float64, 8207)
pretab = []float64{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 3, 3, 2}
)
func init() {
for i := range powtab34 {
powtab34[i] = math.Pow(float64(i), 4.0/3.0)
}
}
func (f *frame) requantizeProcessLong(gr, ch, is_pos, sfb int) {
sf_mult := 0.5
if f.sideInfo.scalefac_scale[gr][ch] != 0 {
sf_mult = 1.0
}
tmp1 := 1.0
// https://github.com/technosaurus/PDMP3/issues/4
if sfb < 21 {
pf_x_pt := float64(f.sideInfo.preflag[gr][ch]) * pretab[sfb]
tmp1 = math.Pow(2.0, -(sf_mult * (float64(f.mainData.scalefac_l[gr][ch][sfb]) + pf_x_pt)))
}
tmp2 := math.Pow(2.0, 0.25*(float64(f.sideInfo.global_gain[gr][ch])-210))
tmp3 := 0.0
if f.mainData.is[gr][ch][is_pos] < 0.0 {
tmp3 = -powtab34[int(-f.mainData.is[gr][ch][is_pos])]
} else {
tmp3 = powtab34[int(f.mainData.is[gr][ch][is_pos])]
}
f.mainData.is[gr][ch][is_pos] = float32(tmp1 * tmp2 * tmp3)
}
func (f *frame) requantizeProcessShort(gr, ch, is_pos, sfb, win int) {
sf_mult := 0.5
if f.sideInfo.scalefac_scale[gr][ch] != 0 {
sf_mult = 1.0
}
tmp1 := 1.0
// https://github.com/technosaurus/PDMP3/issues/4
if sfb < 12 {
tmp1 = math.Pow(2.0, -(sf_mult * float64(f.mainData.scalefac_s[gr][ch][sfb][win])))
}
tmp2 := math.Pow(2.0, 0.25*(float64(f.sideInfo.global_gain[gr][ch])-210.0-
8.0*float64(f.sideInfo.subblock_gain[gr][ch][win])))
tmp3 := 0.0
if f.mainData.is[gr][ch][is_pos] < 0 {
tmp3 = -powtab34[int(-f.mainData.is[gr][ch][is_pos])]
} else {
tmp3 = powtab34[int(f.mainData.is[gr][ch][is_pos])]
}
f.mainData.is[gr][ch][is_pos] = float32(tmp1 * tmp2 * tmp3)
}
type sfBandIndices struct {
l []int
s []int
}
var (
sfBandIndicesSet = []sfBandIndices{
{
l: []int{0, 4, 8, 12, 16, 20, 24, 30, 36, 44, 52, 62, 74, 90, 110, 134, 162, 196, 238, 288, 342, 418, 576},
s: []int{0, 4, 8, 12, 16, 22, 30, 40, 52, 66, 84, 106, 136, 192},
},
{
l: []int{0, 4, 8, 12, 16, 20, 24, 30, 36, 42, 50, 60, 72, 88, 106, 128, 156, 190, 230, 276, 330, 384, 576},
s: []int{0, 4, 8, 12, 16, 22, 28, 38, 50, 64, 80, 100, 126, 192},
},
{
l: []int{0, 4, 8, 12, 16, 20, 24, 30, 36, 44, 54, 66, 82, 102, 126, 156, 194, 240, 296, 364, 448, 550, 576},
s: []int{0, 4, 8, 12, 16, 22, 30, 42, 58, 78, 104, 138, 180, 192},
},
}
)
func (f *frame) l3Requantize(gr int, ch int) {
// Setup sampling frequency index
sfreq := f.header.sampling_frequency
// Determine type of block to process
if (f.sideInfo.win_switch_flag[gr][ch] == 1) && (f.sideInfo.block_type[gr][ch] == 2) { // Short blocks
// Check if the first two subbands
// (=2*18 samples = 8 long or 3 short sfb's) uses long blocks
if f.sideInfo.mixed_block_flag[gr][ch] != 0 { // 2 longbl. sb first
// First process the 2 long block subbands at the start
sfb := 0
next_sfb := sfBandIndicesSet[sfreq].l[sfb+1]
for i := 0; i < 36; i++ {
if i == next_sfb {
sfb++
next_sfb = sfBandIndicesSet[sfreq].l[sfb+1]
}
f.requantizeProcessLong(gr, ch, i, sfb)
}
// And next the remaining,non-zero,bands which uses short blocks
sfb = 3
next_sfb = sfBandIndicesSet[sfreq].s[sfb+1] * 3
win_len := sfBandIndicesSet[sfreq].s[sfb+1] -
sfBandIndicesSet[sfreq].s[sfb]
for i := 36; i < int(f.sideInfo.count1[gr][ch]); /* i++ done below! */ {
// Check if we're into the next scalefac band
if i == next_sfb {
sfb++
next_sfb = sfBandIndicesSet[sfreq].s[sfb+1] * 3
win_len = sfBandIndicesSet[sfreq].s[sfb+1] -
sfBandIndicesSet[sfreq].s[sfb]
}
for win := 0; win < 3; win++ {
for j := 0; j < win_len; j++ {
f.requantizeProcessShort(gr, ch, i, sfb, win)
i++
}
}
}
} else { // Only short blocks
sfb := 0
next_sfb := sfBandIndicesSet[sfreq].s[sfb+1] * 3
win_len := sfBandIndicesSet[sfreq].s[sfb+1] -
sfBandIndicesSet[sfreq].s[sfb]
for i := 0; i < int(f.sideInfo.count1[gr][ch]); /* i++ done below! */ {
// Check if we're into the next scalefac band
if i == next_sfb {
sfb++
next_sfb = sfBandIndicesSet[sfreq].s[sfb+1] * 3
win_len = sfBandIndicesSet[sfreq].s[sfb+1] -
sfBandIndicesSet[sfreq].s[sfb]
}
for win := 0; win < 3; win++ {
for j := 0; j < win_len; j++ {
f.requantizeProcessShort(gr, ch, i, sfb, win)
i++
}
}
}
}
} else { // Only long blocks
sfb := 0
next_sfb := sfBandIndicesSet[sfreq].l[sfb+1]
for i := 0; i < int(f.sideInfo.count1[gr][ch]); i++ {
if i == next_sfb {
sfb++
next_sfb = sfBandIndicesSet[sfreq].l[sfb+1]
}
f.requantizeProcessLong(gr, ch, i, sfb)
}
}
}
func (f *frame) l3Reorder(gr int, ch int) {
re := make([]float32, 576)
sfreq := f.header.sampling_frequency // Setup sampling freq index
// Only reorder short blocks
if (f.sideInfo.win_switch_flag[gr][ch] == 1) && (f.sideInfo.block_type[gr][ch] == 2) { // Short blocks
// Check if the first two subbands
// (=2*18 samples = 8 long or 3 short sfb's) uses long blocks
sfb := 0
// 2 longbl. sb first
if f.sideInfo.mixed_block_flag[gr][ch] != 0 {
sfb = 3
}
next_sfb := sfBandIndicesSet[sfreq].s[sfb+1] * 3
win_len := sfBandIndicesSet[sfreq].s[sfb+1] - sfBandIndicesSet[sfreq].s[sfb]
i := 36
if sfb == 0 {
i = 0
}
for i < 576 {
// Check if we're into the next scalefac band
if i == next_sfb {
// Copy reordered data back to the original vector
for j := 0; j < 3*win_len; j++ {
f.mainData.is[gr][ch][3*sfBandIndicesSet[sfreq].s[sfb]+j] = re[j]
}
// Check if this band is above the rzero region,if so we're done
if i >= f.sideInfo.count1[gr][ch] {
return
}
sfb++
next_sfb = sfBandIndicesSet[sfreq].s[sfb+1] * 3
win_len = sfBandIndicesSet[sfreq].s[sfb+1] - sfBandIndicesSet[sfreq].s[sfb]
}
for win := 0; win < 3; win++ { // Do the actual reordering
for j := 0; j < win_len; j++ {
re[j*3+win] = f.mainData.is[gr][ch][i]
i++
}
}
}
// Copy reordered data of last band back to original vector
for j := 0; j < 3*win_len; j++ {
f.mainData.is[gr][ch][3*sfBandIndicesSet[sfreq].s[12]+j] = re[j]
}
}
}
var (
isRatios = []float32{0.000000, 0.267949, 0.577350, 1.000000, 1.732051, 3.732051}
)
func (f *frame) stereoProcessIntensityLong(gr int, sfb int) {
is_ratio_l := float32(0)
is_ratio_r := float32(0)
// Check that((is_pos[sfb]=scalefac) != 7) => no intensity stereo
is_pos := f.mainData.scalefac_l[gr][0][sfb]
if is_pos != 7 {
sfreq := f.header.sampling_frequency // Setup sampling freq index
sfb_start := sfBandIndicesSet[sfreq].l[sfb]
sfb_stop := sfBandIndicesSet[sfreq].l[sfb+1]
if is_pos == 6 { // tan((6*PI)/12 = PI/2) needs special treatment!
is_ratio_l = 1.0
is_ratio_r = 0.0
} else {
is_ratio_l = isRatios[is_pos] / (1.0 + isRatios[is_pos])
is_ratio_r = 1.0 / (1.0 + isRatios[is_pos])
}
// Now decode all samples in this scale factor band
for i := sfb_start; i < sfb_stop; i++ {
f.mainData.is[gr][0][i] *= is_ratio_l
f.mainData.is[gr][1][i] *= is_ratio_r
}
}
}
func (f *frame) stereoProcessIntensityShort(gr int, sfb int) {
is_ratio_l := float32(0)
is_ratio_r := float32(0)
sfreq := f.header.sampling_frequency // Setup sampling freq index
// The window length
win_len := sfBandIndicesSet[sfreq].s[sfb+1] - sfBandIndicesSet[sfreq].s[sfb]
// The three windows within the band has different scalefactors
for win := 0; win < 3; win++ {
// Check that((is_pos[sfb]=scalefac) != 7) => no intensity stereo
is_pos := f.mainData.scalefac_s[gr][0][sfb][win]
if is_pos != 7 {
sfb_start := sfBandIndicesSet[sfreq].s[sfb]*3 + win_len*win
sfb_stop := sfb_start + win_len
if is_pos == 6 { // tan((6*PI)/12 = PI/2) needs special treatment!
is_ratio_l = 1.0
is_ratio_r = 0.0
} else {
is_ratio_l = isRatios[is_pos] / (1.0 + isRatios[is_pos])
is_ratio_r = 1.0 / (1.0 + isRatios[is_pos])
}
// Now decode all samples in this scale factor band
for i := sfb_start; i < sfb_stop; i++ {
// https://github.com/technosaurus/PDMP3/issues/3
f.mainData.is[gr][0][i] *= is_ratio_l
f.mainData.is[gr][1][i] *= is_ratio_r
}
}
}
}
func (f *frame) l3Stereo(gr int) {
// Do nothing if joint stereo is not enabled
if (f.header.mode != 1) || (f.header.mode_extension == 0) {
return
}
// Do Middle/Side("normal") stereo processing
if (f.header.mode_extension & 0x2) != 0 {
// Determine how many frequency lines to transform
i := 0
if f.sideInfo.count1[gr][0] > f.sideInfo.count1[gr][1] {
i = 1
}
max_pos := int(f.sideInfo.count1[gr][i])
// Do the actual processing
const invSqrt2 = math.Sqrt2 / 2
for i := 0; i < max_pos; i++ {
left := (f.mainData.is[gr][0][i] + f.mainData.is[gr][1][i]) * invSqrt2
right := (f.mainData.is[gr][0][i] - f.mainData.is[gr][1][i]) * invSqrt2
f.mainData.is[gr][0][i] = left
f.mainData.is[gr][1][i] = right
}
}
// Do intensity stereo processing
if (f.header.mode_extension & 0x1) != 0 {
// Setup sampling frequency index
sfreq := f.header.sampling_frequency
// First band that is intensity stereo encoded is first band scale factor
// band on or above count1 frequency line. N.B.: Intensity stereo coding is
// only done for higher subbands, but logic is here for lower subbands.
// Determine type of block to process
if (f.sideInfo.win_switch_flag[gr][0] == 1) &&
(f.sideInfo.block_type[gr][0] == 2) { // Short blocks
// Check if the first two subbands
// (=2*18 samples = 8 long or 3 short sfb's) uses long blocks
if f.sideInfo.mixed_block_flag[gr][0] != 0 { // 2 longbl. sb first
for sfb := 0; sfb < 8; sfb++ { // First process 8 sfb's at start
// Is this scale factor band above count1 for the right channel?
if sfBandIndicesSet[sfreq].l[sfb] >= f.sideInfo.count1[gr][1] {
f.stereoProcessIntensityLong(gr, sfb)
}
}
// And next the remaining bands which uses short blocks
for sfb := 3; sfb < 12; sfb++ {
// Is this scale factor band above count1 for the right channel?
if sfBandIndicesSet[sfreq].s[sfb]*3 >= f.sideInfo.count1[gr][1] {
f.stereoProcessIntensityShort(gr, sfb) // intensity stereo processing
}
}
} else { // Only short blocks
for sfb := 0; sfb < 12; sfb++ {
// Is this scale factor band above count1 for the right channel?
if sfBandIndicesSet[sfreq].s[sfb]*3 >= f.sideInfo.count1[gr][1] {
f.stereoProcessIntensityShort(gr, sfb) // intensity stereo processing
}
}
}
} else { // Only long blocks
for sfb := 0; sfb < 21; sfb++ {
// Is this scale factor band above count1 for the right channel?
if sfBandIndicesSet[sfreq].l[sfb] >= f.sideInfo.count1[gr][1] {
// Perform the intensity stereo processing
f.stereoProcessIntensityLong(gr, sfb)
}
}
}
}
}
var (
cs = []float32{0.857493, 0.881742, 0.949629, 0.983315, 0.995518, 0.999161, 0.999899, 0.999993}
ca = []float32{-0.514496, -0.471732, -0.313377, -0.181913, -0.094574, -0.040966, -0.014199, -0.003700}
)
func (f *frame) l3Antialias(gr int, ch int) {
// No antialiasing is done for short blocks
if (f.sideInfo.win_switch_flag[gr][ch] == 1) &&
(f.sideInfo.block_type[gr][ch] == 2) &&
(f.sideInfo.mixed_block_flag[gr][ch]) == 0 {
return
}
// Setup the limit for how many subbands to transform
sblim := 32
if (f.sideInfo.win_switch_flag[gr][ch] == 1) &&
(f.sideInfo.block_type[gr][ch] == 2) &&
(f.sideInfo.mixed_block_flag[gr][ch] == 1) {
sblim = 2
}
// Do the actual antialiasing
for sb := 1; sb < sblim; sb++ {
for i := 0; i < 8; i++ {
li := 18*sb - 1 - i
ui := 18*sb + i
lb := f.mainData.is[gr][ch][li]*cs[i] - f.mainData.is[gr][ch][ui]*ca[i]
ub := f.mainData.is[gr][ch][ui]*cs[i] + f.mainData.is[gr][ch][li]*ca[i]
f.mainData.is[gr][ch][li] = lb
f.mainData.is[gr][ch][ui] = ub
}
}
}
func (f *frame) l3HybridSynthesis(gr int, ch int) {
// Loop through all 32 subbands
for sb := 0; sb < 32; sb++ {
// Determine blocktype for this subband
bt := int(f.sideInfo.block_type[gr][ch])
if (f.sideInfo.win_switch_flag[gr][ch] == 1) &&
(f.sideInfo.mixed_block_flag[gr][ch] == 1) && (sb < 2) {
bt = 0
}
// Do the inverse modified DCT and windowing
in := make([]float32, 18)
for i := range in {
in[i] = f.mainData.is[gr][ch][sb*18+i]
}
rawout := imdctWin(in, bt)
// Overlapp add with stored vector into main_data vector
for i := 0; i < 18; i++ {
f.mainData.is[gr][ch][sb*18+i] = rawout[i] + f.store[ch][sb][i]
f.store[ch][sb][i] = rawout[i+18]
}
}
}
func (f *frame) l3FrequencyInversion(gr int, ch int) {
for sb := 1; sb < 32; sb += 2 { //OPT? : for(sb = 18; sb < 576; sb += 36)
for i := 1; i < 18; i += 2 {
f.mainData.is[gr][ch][sb*18+i] = -f.mainData.is[gr][ch][sb*18+i]
}
}
}
var synthNWin = [64][32]float32{}
func init() {
for i := 0; i < 64; i++ {
for j := 0; j < 32; j++ {
synthNWin[i][j] =
float32(math.Cos(float64((16+i)*(2*j+1)) * (math.Pi / 64.0)))
}
}
}
var synthDtbl = [512]float32{
0.000000000, -0.000015259, -0.000015259, -0.000015259,
-0.000015259, -0.000015259, -0.000015259, -0.000030518,
-0.000030518, -0.000030518, -0.000030518, -0.000045776,
-0.000045776, -0.000061035, -0.000061035, -0.000076294,
-0.000076294, -0.000091553, -0.000106812, -0.000106812,
-0.000122070, -0.000137329, -0.000152588, -0.000167847,
-0.000198364, -0.000213623, -0.000244141, -0.000259399,
-0.000289917, -0.000320435, -0.000366211, -0.000396729,
-0.000442505, -0.000473022, -0.000534058, -0.000579834,
-0.000625610, -0.000686646, -0.000747681, -0.000808716,
-0.000885010, -0.000961304, -0.001037598, -0.001113892,
-0.001205444, -0.001296997, -0.001388550, -0.001480103,
-0.001586914, -0.001693726, -0.001785278, -0.001907349,
-0.002014160, -0.002120972, -0.002243042, -0.002349854,
-0.002456665, -0.002578735, -0.002685547, -0.002792358,
-0.002899170, -0.002990723, -0.003082275, -0.003173828,
0.003250122, 0.003326416, 0.003387451, 0.003433228,
0.003463745, 0.003479004, 0.003479004, 0.003463745,
0.003417969, 0.003372192, 0.003280640, 0.003173828,
0.003051758, 0.002883911, 0.002700806, 0.002487183,
0.002227783, 0.001937866, 0.001617432, 0.001266479,
0.000869751, 0.000442505, -0.000030518, -0.000549316,
-0.001098633, -0.001693726, -0.002334595, -0.003005981,
-0.003723145, -0.004486084, -0.005294800, -0.006118774,
-0.007003784, -0.007919312, -0.008865356, -0.009841919,
-0.010848999, -0.011886597, -0.012939453, -0.014022827,
-0.015121460, -0.016235352, -0.017349243, -0.018463135,
-0.019577026, -0.020690918, -0.021789551, -0.022857666,
-0.023910522, -0.024932861, -0.025909424, -0.026840210,
-0.027725220, -0.028533936, -0.029281616, -0.029937744,
-0.030532837, -0.031005859, -0.031387329, -0.031661987,
-0.031814575, -0.031845093, -0.031738281, -0.031478882,
0.031082153, 0.030517578, 0.029785156, 0.028884888,
0.027801514, 0.026535034, 0.025085449, 0.023422241,
0.021575928, 0.019531250, 0.017257690, 0.014801025,
0.012115479, 0.009231567, 0.006134033, 0.002822876,
-0.000686646, -0.004394531, -0.008316040, -0.012420654,
-0.016708374, -0.021179199, -0.025817871, -0.030609131,
-0.035552979, -0.040634155, -0.045837402, -0.051132202,
-0.056533813, -0.061996460, -0.067520142, -0.073059082,
-0.078628540, -0.084182739, -0.089706421, -0.095169067,
-0.100540161, -0.105819702, -0.110946655, -0.115921021,
-0.120697021, -0.125259399, -0.129562378, -0.133590698,
-0.137298584, -0.140670776, -0.143676758, -0.146255493,
-0.148422241, -0.150115967, -0.151306152, -0.151962280,
-0.152069092, -0.151596069, -0.150497437, -0.148773193,
-0.146362305, -0.143264771, -0.139450073, -0.134887695,
-0.129577637, -0.123474121, -0.116577148, -0.108856201,
0.100311279, 0.090927124, 0.080688477, 0.069595337,
0.057617188, 0.044784546, 0.031082153, 0.016510010,
0.001068115, -0.015228271, -0.032379150, -0.050354004,
-0.069168091, -0.088775635, -0.109161377, -0.130310059,
-0.152206421, -0.174789429, -0.198059082, -0.221984863,
-0.246505737, -0.271591187, -0.297210693, -0.323318481,
-0.349868774, -0.376800537, -0.404083252, -0.431655884,
-0.459472656, -0.487472534, -0.515609741, -0.543823242,
-0.572036743, -0.600219727, -0.628295898, -0.656219482,
-0.683914185, -0.711318970, -0.738372803, -0.765029907,
-0.791213989, -0.816864014, -0.841949463, -0.866363525,
-0.890090942, -0.913055420, -0.935195923, -0.956481934,
-0.976852417, -0.996246338, -1.014617920, -1.031936646,
-1.048156738, -1.063217163, -1.077117920, -1.089782715,
-1.101211548, -1.111373901, -1.120223999, -1.127746582,
-1.133926392, -1.138763428, -1.142211914, -1.144287109,
1.144989014, 1.144287109, 1.142211914, 1.138763428,
1.133926392, 1.127746582, 1.120223999, 1.111373901,
1.101211548, 1.089782715, 1.077117920, 1.063217163,
1.048156738, 1.031936646, 1.014617920, 0.996246338,
0.976852417, 0.956481934, 0.935195923, 0.913055420,
0.890090942, 0.866363525, 0.841949463, 0.816864014,
0.791213989, 0.765029907, 0.738372803, 0.711318970,
0.683914185, 0.656219482, 0.628295898, 0.600219727,
0.572036743, 0.543823242, 0.515609741, 0.487472534,
0.459472656, 0.431655884, 0.404083252, 0.376800537,
0.349868774, 0.323318481, 0.297210693, 0.271591187,
0.246505737, 0.221984863, 0.198059082, 0.174789429,
0.152206421, 0.130310059, 0.109161377, 0.088775635,
0.069168091, 0.050354004, 0.032379150, 0.015228271,
-0.001068115, -0.016510010, -0.031082153, -0.044784546,
-0.057617188, -0.069595337, -0.080688477, -0.090927124,
0.100311279, 0.108856201, 0.116577148, 0.123474121,
0.129577637, 0.134887695, 0.139450073, 0.143264771,
0.146362305, 0.148773193, 0.150497437, 0.151596069,
0.152069092, 0.151962280, 0.151306152, 0.150115967,
0.148422241, 0.146255493, 0.143676758, 0.140670776,
0.137298584, 0.133590698, 0.129562378, 0.125259399,
0.120697021, 0.115921021, 0.110946655, 0.105819702,
0.100540161, 0.095169067, 0.089706421, 0.084182739,
0.078628540, 0.073059082, 0.067520142, 0.061996460,
0.056533813, 0.051132202, 0.045837402, 0.040634155,
0.035552979, 0.030609131, 0.025817871, 0.021179199,
0.016708374, 0.012420654, 0.008316040, 0.004394531,
0.000686646, -0.002822876, -0.006134033, -0.009231567,
-0.012115479, -0.014801025, -0.017257690, -0.019531250,
-0.021575928, -0.023422241, -0.025085449, -0.026535034,
-0.027801514, -0.028884888, -0.029785156, -0.030517578,
0.031082153, 0.031478882, 0.031738281, 0.031845093,
0.031814575, 0.031661987, 0.031387329, 0.031005859,
0.030532837, 0.029937744, 0.029281616, 0.028533936,
0.027725220, 0.026840210, 0.025909424, 0.024932861,
0.023910522, 0.022857666, 0.021789551, 0.020690918,
0.019577026, 0.018463135, 0.017349243, 0.016235352,
0.015121460, 0.014022827, 0.012939453, 0.011886597,
0.010848999, 0.009841919, 0.008865356, 0.007919312,
0.007003784, 0.006118774, 0.005294800, 0.004486084,
0.003723145, 0.003005981, 0.002334595, 0.001693726,
0.001098633, 0.000549316, 0.000030518, -0.000442505,
-0.000869751, -0.001266479, -0.001617432, -0.001937866,
-0.002227783, -0.002487183, -0.002700806, -0.002883911,
-0.003051758, -0.003173828, -0.003280640, -0.003372192,
-0.003417969, -0.003463745, -0.003479004, -0.003479004,
-0.003463745, -0.003433228, -0.003387451, -0.003326416,
0.003250122, 0.003173828, 0.003082275, 0.002990723,
0.002899170, 0.002792358, 0.002685547, 0.002578735,
0.002456665, 0.002349854, 0.002243042, 0.002120972,
0.002014160, 0.001907349, 0.001785278, 0.001693726,
0.001586914, 0.001480103, 0.001388550, 0.001296997,
0.001205444, 0.001113892, 0.001037598, 0.000961304,
0.000885010, 0.000808716, 0.000747681, 0.000686646,
0.000625610, 0.000579834, 0.000534058, 0.000473022,
0.000442505, 0.000396729, 0.000366211, 0.000320435,
0.000289917, 0.000259399, 0.000244141, 0.000213623,
0.000198364, 0.000167847, 0.000152588, 0.000137329,
0.000122070, 0.000106812, 0.000106812, 0.000091553,
0.000076294, 0.000076294, 0.000061035, 0.000061035,
0.000045776, 0.000045776, 0.000030518, 0.000030518,
0.000030518, 0.000030518, 0.000015259, 0.000015259,
0.000015259, 0.000015259, 0.000015259, 0.000015259,
}
func (f *frame) l3SubbandSynthesis(gr int, ch int, out []uint8) {
u_vec := make([]float32, 512)
s_vec := make([]float32, 32)
nch := f.header.numberOfChannels()
// Setup the n_win windowing vector and the v_vec intermediate vector
for ss := 0; ss < 18; ss++ { // Loop through 18 samples in 32 subbands
for i := 1023; i > 63; i-- { // Shift up the V vector
f.v_vec[ch][i] = f.v_vec[ch][i-64]
}
for i := 0; i < 32; i++ { // Copy next 32 time samples to a temp vector
s_vec[i] = f.mainData.is[gr][ch][i*18+ss]
}
for i := 0; i < 64; i++ { // Matrix multiply input with n_win[][] matrix
sum := float32(0)
for j := 0; j < 32; j++ {
sum += synthNWin[i][j] * s_vec[j]
}
f.v_vec[ch][i] = sum
}
for i := 0; i < 8; i++ { // Build the U vector
for j := 0; j < 32; j++ { // <<7 == *128
u_vec[(i<<6)+j] = f.v_vec[ch][(i<<7)+j]
u_vec[(i<<6)+j+32] = f.v_vec[ch][(i<<7)+j+96]
}
}
for i := 0; i < 512; i++ { // Window by u_vec[i] with synthDtbl[i]
u_vec[i] *= synthDtbl[i]
}
for i := 0; i < 32; i++ { // Calc 32 samples,store in outdata vector
sum := float32(0)
for j := 0; j < 16; j++ { // sum += u_vec[j*32 + i];
sum += u_vec[(j<<5)+i]
}
// sum now contains time sample 32*ss+i. Convert to 16-bit signed int
samp := int(sum * 32767)
if samp > 32767 {
samp = 32767
} else if samp < -32767 {
samp = -32767
}
samp &= 0xffff
s := uint32(samp)
if nch == 1 {
// We always run in stereo mode and duplicate channels here for mono.
out[4*(32*ss+i)] = uint8(s)
out[4*(32*ss+i)+1] = uint8(s >> 8)
out[4*(32*ss+i)+2] = uint8(s)
out[4*(32*ss+i)+3] = uint8(s >> 8)
continue
}
if ch == 0 {
out[4*(32*ss+i)] = uint8(s)
out[4*(32*ss+i)+1] = uint8(s >> 8)
} else {
out[4*(32*ss+i)+2] = uint8(s)
out[4*(32*ss+i)+3] = uint8(s >> 8)
}
}
}
}

247
maindata.go Normal file
View file

@ -0,0 +1,247 @@
// Copyright 2017 Hajime Hoshi
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package mp3
import (
"fmt"
"io"
)
var mpeg1ScalefacSizes = [16][2]int{
{0, 0}, {0, 1}, {0, 2}, {0, 3}, {3, 0}, {1, 1}, {1, 2}, {1, 3},
{2, 1}, {2, 2}, {2, 3}, {3, 1}, {3, 2}, {3, 3}, {4, 2}, {4, 3},
}
func (s *source) readMainL3(prev *mainDataBytes, header *mpeg1FrameHeader, sideInfo *mpeg1SideInfo) (*mpeg1MainData, *mainDataBytes, error) {
nch := header.numberOfChannels()
// Calculate header audio data size
framesize := header.frameSize()
if framesize > 2000 {
return nil, nil, fmt.Errorf("mp3: framesize = %d", framesize)
}
// Sideinfo is 17 bytes for one channel and 32 bytes for two
sideinfo_size := 32
if nch == 1 {
sideinfo_size = 17
}
// Main data size is the rest of the frame,including ancillary data
main_data_size := framesize - sideinfo_size - 4 // sync+header
// CRC is 2 bytes
if header.protection_bit == 0 {
main_data_size -= 2
}
// Assemble main data buffer with data from this frame and the previous
// two frames. main_data_begin indicates how many bytes from previous
// frames that should be used. This buffer is later accessed by the
// getMainBits function in the same way as the side info is.
m, err := s.getMainData(prev, main_data_size, sideInfo.main_data_begin)
if err != nil {
// This could be due to not enough data in reservoir
return nil, nil, err
}
md := &mpeg1MainData{}
for gr := 0; gr < 2; gr++ {
for ch := 0; ch < nch; ch++ {
part_2_start := m.getMainPos()
// Number of bits in the bitstream for the bands
slen1 := mpeg1ScalefacSizes[sideInfo.scalefac_compress[gr][ch]][0]
slen2 := mpeg1ScalefacSizes[sideInfo.scalefac_compress[gr][ch]][1]
if (sideInfo.win_switch_flag[gr][ch] != 0) && (sideInfo.block_type[gr][ch] == 2) {
if sideInfo.mixed_block_flag[gr][ch] != 0 {
for sfb := 0; sfb < 8; sfb++ {
md.scalefac_l[gr][ch][sfb] = m.getMainBits(slen1)
}
for sfb := 3; sfb < 12; sfb++ {
//slen1 for band 3-5,slen2 for 6-11
nbits := slen2
if sfb < 6 {
nbits = slen1
}
for win := 0; win < 3; win++ {
md.scalefac_s[gr][ch][sfb][win] = m.getMainBits(nbits)
}
}
} else {
for sfb := 0; sfb < 12; sfb++ {
//slen1 for band 3-5,slen2 for 6-11
nbits := slen2
if sfb < 6 {
nbits = slen1
}
for win := 0; win < 3; win++ {
md.scalefac_s[gr][ch][sfb][win] = m.getMainBits(nbits)
}
}
}
} else { // block_type == 0 if winswitch == 0
// Scale factor bands 0-5
if (sideInfo.scfsi[ch][0] == 0) || (gr == 0) {
for sfb := 0; sfb < 6; sfb++ {
md.scalefac_l[gr][ch][sfb] = m.getMainBits(slen1)
}
} else if (sideInfo.scfsi[ch][0] == 1) && (gr == 1) {
// Copy scalefactors from granule 0 to granule 1
for sfb := 0; sfb < 6; sfb++ {
md.scalefac_l[1][ch][sfb] = md.scalefac_l[0][ch][sfb]
}
}
// Scale factor bands 6-10
if (sideInfo.scfsi[ch][1] == 0) || (gr == 0) {
for sfb := 6; sfb < 11; sfb++ {
md.scalefac_l[gr][ch][sfb] = m.getMainBits(slen1)
}
} else if (sideInfo.scfsi[ch][1] == 1) && (gr == 1) {
// Copy scalefactors from granule 0 to granule 1
for sfb := 6; sfb < 11; sfb++ {
md.scalefac_l[1][ch][sfb] = md.scalefac_l[0][ch][sfb]
}
}
// Scale factor bands 11-15
if (sideInfo.scfsi[ch][2] == 0) || (gr == 0) {
for sfb := 11; sfb < 16; sfb++ {
md.scalefac_l[gr][ch][sfb] = m.getMainBits(slen2)
}
} else if (sideInfo.scfsi[ch][2] == 1) && (gr == 1) {
// Copy scalefactors from granule 0 to granule 1
for sfb := 11; sfb < 16; sfb++ {
md.scalefac_l[1][ch][sfb] = md.scalefac_l[0][ch][sfb]
}
}
// Scale factor bands 16-20
if (sideInfo.scfsi[ch][3] == 0) || (gr == 0) {
for sfb := 16; sfb < 21; sfb++ {
md.scalefac_l[gr][ch][sfb] = m.getMainBits(slen2)
}
} else if (sideInfo.scfsi[ch][3] == 1) && (gr == 1) {
// Copy scalefactors from granule 0 to granule 1
for sfb := 16; sfb < 21; sfb++ {
md.scalefac_l[1][ch][sfb] = md.scalefac_l[0][ch][sfb]
}
}
}
// Read Huffman coded data. Skip stuffing bits.
if err := m.readHuffman(header, sideInfo, md, part_2_start, gr, ch); err != nil {
return nil, nil, err
}
}
}
// The ancillary data is stored here,but we ignore it.
return md, m, nil
}
type mainDataBytes struct {
// Large static data
vec []int
// Index into the current byte(0-7)
idx int
pos int
}
func (s *source) getMainData(prev *mainDataBytes, size int, offset int) (*mainDataBytes, error) {
if size > 1500 {
return nil, fmt.Errorf("mp3: size = %d", size)
}
// Check that there's data available from previous frames if needed
if prev != nil && offset > len(prev.vec) {
// No,there is not, so we skip decoding this frame, but we have to
// read the main_data bits from the bitstream in case they are needed
// for decoding the next frame.
buf := make([]int, size)
n := 0
var err error
for n < size && err == nil {
nn, err2 := s.getBytes(buf)
n += nn
err = err2
}
if n < size {
if err == io.EOF {
return nil, fmt.Errorf("mp3: unexpected EOF at getMainData")
}
return nil, err
}
m := &mainDataBytes{
vec: append(prev.vec, buf...),
}
// TODO: Define a special error and enable to continue the next frame.
return m, fmt.Errorf("mp3: frame can't be decoded")
}
// Copy data from previous frames
vec := []int{}
if prev != nil {
v := prev.vec
vec = v[len(v)-offset:]
}
// Read the main_data from file
buf := make([]int, size)
n := 0
var err error
for n < size && err == nil {
nn, err2 := s.getBytes(buf)
n += nn
err = err2
}
if n < size {
if err == io.EOF {
return nil, fmt.Errorf("mp3: unexpected EOF at getMainData")
}
return nil, err
}
m := &mainDataBytes{
vec: append(vec, buf...),
}
return m, nil
}
func (m *mainDataBytes) getMainBit() int {
tmp := uint(m.vec[m.pos]) >> (7 - uint(m.idx))
tmp &= 0x01
m.pos += (m.idx + 1) >> 3
m.idx = (m.idx + 1) & 0x07
return int(tmp)
}
func (m *mainDataBytes) getMainBits(num int) int {
if num == 0 {
return 0
}
// Form a word of the next four bytes
b := make([]int, 4)
copy(b, m.vec[m.pos:])
tmp := (uint32(b[0]) << 24) | (uint32(b[1]) << 16) | (uint32(b[2]) << 8) | (uint32(b[3]) << 0)
// Remove bits already used
tmp = tmp << uint(m.idx)
// Remove bits after the desired bits
tmp = tmp >> (32 - uint(num))
// Update pointers
m.pos += (m.idx + num) >> 3
m.idx = (m.idx + num) & 0x07
return int(tmp)
}
func (m *mainDataBytes) getMainPos() int {
pos := m.pos
pos *= 8 // Multiply by 8 to get number of bits
pos += m.idx
return pos
}
func (m *mainDataBytes) setMainPos(bit_pos int) {
m.pos = bit_pos >> 3
m.idx = bit_pos & 0x7
}

271
read.go Normal file
View file

@ -0,0 +1,271 @@
// Copyright 2017 Hajime Hoshi
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package mp3
import (
"fmt"
"io"
)
func (s *source) readCRC() error {
buf := make([]int, 2)
n := 0
var err error
for n < 2 && err == nil {
nn, err2 := s.getBytes(buf[n:])
n += nn
err = err2
}
if err == io.EOF {
if n < 2 {
return fmt.Errorf("mp3: unexpected EOF at readCRC")
}
return nil
}
if err != nil {
return err
}
return nil
}
func (s *source) readNextFrame(prev *frame) (*frame, error) {
h, err := s.readHeader()
if err != nil {
return nil, err
}
// Get CRC word if present
if h.protection_bit == 0 {
if err := s.readCRC(); err != nil {
return nil, err
}
}
if h.layer != mpeg1Layer3 {
return nil, fmt.Errorf("mp3: only layer3 (want %d; got %d) is supported!", mpeg1Layer3, h.layer)
}
// Get side info
si, err := s.readSideInfo(h)
if err != nil {
return nil, err
}
// If there's not enough main data in the bit reservoir,
// signal to calling function so that decoding isn't done!
// Get main data(scalefactors and Huffman coded frequency data)
var prevM *mainDataBytes
if prev != nil {
prevM = prev.mainDataBytes
}
md, mdb, err := s.readMainL3(prevM, h, si)
if err != nil {
return nil, err
}
nf := &frame{
prev: prev,
header: h,
sideInfo: si,
mainData: md,
mainDataBytes: mdb,
}
if nf.prev != nil {
nf.store = nf.prev.store
nf.v_vec = nf.prev.v_vec
}
return nf, nil
}
func isHeader(header uint32) bool {
const C_SYNC = 0xffe00000
if (header & C_SYNC) != C_SYNC {
return false
}
// Bitrate must not be 15.
if (header & (0xf << 12)) == 0xf<<12 {
return false
}
// Sample Frequency must not be 3.
if (header & (3 << 10)) == 3<<10 {
return false
}
return true
}
func (s *source) readHeader() (*mpeg1FrameHeader, error) {
// Get the next four bytes from the bitstream
buf := make([]int, 4)
n := 0
var err error
for n < 4 && err == nil {
nn, err2 := s.getBytes(buf[n:])
n += nn
err = err2
}
if n < 4 {
if err == io.EOF {
if n == 0 {
// Expected EOF
return nil, io.EOF
}
return nil, fmt.Errorf("mp3: unexpected EOF at readHeader")
}
return nil, err
}
b1 := uint32(buf[0])
b2 := uint32(buf[1])
b3 := uint32(buf[2])
b4 := uint32(buf[3])
header := (b1 << 24) | (b2 << 16) | (b3 << 8) | (b4 << 0)
for !isHeader(uint32(header)) {
// No,so scan the bitstream one byte at a time until we find it or EOF
// Shift the values one byte to the left
b1 = b2
b2 = b3
b3 = b4
// Get one new byte from the bitstream
b, err := s.getByte()
if err != nil {
if err == io.EOF {
return nil, fmt.Errorf("mp3: unexpected EOF at readHeader")
}
return nil, err
}
b4 = uint32(b)
header = (b1 << 24) | (b2 << 16) | (b3 << 8) | (b4 << 0)
}
// If we get here we've found the sync word,and can decode the header
// which is in the low 20 bits of the 32-bit sync+header word.
// Decode the header
h := &mpeg1FrameHeader{}
h.id = int((header & 0x00180000) >> 19)
h.layer = mpeg1Layer((header & 0x00060000) >> 17)
h.protection_bit = int((header & 0x00010000) >> 16)
h.bitrate_index = int((header & 0x0000f000) >> 12)
h.sampling_frequency = int((header & 0x00000c00) >> 10)
h.padding_bit = int((header & 0x00000200) >> 9)
h.private_bit = int((header & 0x00000100) >> 8)
h.mode = mpeg1Mode((header & 0x000000c0) >> 6)
h.mode_extension = int((header & 0x00000030) >> 4)
h.copyright = int((header & 0x00000008) >> 3)
h.original_or_copy = int((header & 0x00000004) >> 2)
h.emphasis = int((header & 0x00000003) >> 0)
// Check for invalid values and impossible combinations
if h.id != 3 {
return nil, fmt.Errorf("mp3: ID must be 3. Header word is 0x%08x at file pos %d",
header, s.getFilepos())
}
if h.bitrate_index == 0 {
return nil, fmt.Errorf("mp3: Free bitrate format NIY! Header word is 0x%08x at file pos %d",
header, s.getFilepos())
}
if h.bitrate_index == 15 {
return nil, fmt.Errorf("mp3: bitrate_index = 15 is invalid! Header word is 0x%08x at file pos %d",
header, s.getFilepos())
}
if h.sampling_frequency == 3 {
return nil, fmt.Errorf("mp3: sampling_frequency = 3 is invalid! Header word is 0x%08x at file pos %d",
header, s.getFilepos())
}
if h.layer == mpeg1LayerReserved {
return nil, fmt.Errorf("mp3: layer = %d is invalid! Header word is 0x%08x at file pos %d",
mpeg1LayerReserved, header, s.getFilepos())
}
return h, nil
}
func (m *mainDataBytes) readHuffman(header *mpeg1FrameHeader, sideInfo *mpeg1SideInfo, mainData *mpeg1MainData, part_2_start, gr, ch int) error {
// Check that there is any data to decode. If not,zero the array.
if sideInfo.part2_3_length[gr][ch] == 0 {
for is_pos := 0; is_pos < 576; is_pos++ {
mainData.is[gr][ch][is_pos] = 0.0
}
return nil
}
// Calculate bit_pos_end which is the index of the last bit for this part.
bit_pos_end := part_2_start + sideInfo.part2_3_length[gr][ch] - 1
// Determine region boundaries
region_1_start := 0
region_2_start := 0
if (sideInfo.win_switch_flag[gr][ch] == 1) && (sideInfo.block_type[gr][ch] == 2) {
region_1_start = 36 // sfb[9/3]*3=36
region_2_start = 576 // No Region2 for short block case.
} else {
sfreq := header.sampling_frequency
region_1_start =
sfBandIndicesSet[sfreq].l[sideInfo.region0_count[gr][ch]+1]
region_2_start =
sfBandIndicesSet[sfreq].l[sideInfo.region0_count[gr][ch]+
sideInfo.region1_count[gr][ch]+2]
}
// Read big_values using tables according to region_x_start
for is_pos := 0; is_pos < sideInfo.big_values[gr][ch]*2; is_pos++ {
table_num := 0
if is_pos < region_1_start {
table_num = sideInfo.table_select[gr][ch][0]
} else if is_pos < region_2_start {
table_num = sideInfo.table_select[gr][ch][1]
} else {
table_num = sideInfo.table_select[gr][ch][2]
}
// Get next Huffman coded words
x, y, _, _, err := huffmanDecode(m, table_num)
if err != nil {
return err
}
// In the big_values area there are two freq lines per Huffman word
mainData.is[gr][ch][is_pos] = float32(x)
is_pos++
mainData.is[gr][ch][is_pos] = float32(y)
}
// Read small values until is_pos = 576 or we run out of huffman data
table_num := sideInfo.count1table_select[gr][ch] + 32
is_pos := sideInfo.big_values[gr][ch] * 2
for (is_pos <= 572) && (m.getMainPos() <= bit_pos_end) {
// Get next Huffman coded words
x, y, v, w, err := huffmanDecode(m, table_num)
if err != nil {
return err
}
mainData.is[gr][ch][is_pos] = float32(v)
is_pos++
if is_pos >= 576 {
break
}
mainData.is[gr][ch][is_pos] = float32(w)
is_pos++
if is_pos >= 576 {
break
}
mainData.is[gr][ch][is_pos] = float32(x)
is_pos++
if is_pos >= 576 {
break
}
mainData.is[gr][ch][is_pos] = float32(y)
is_pos++
}
// Check that we didn't read past the end of this section
if m.getMainPos() > (bit_pos_end + 1) {
// Remove last words read
is_pos -= 4
}
// Setup count1 which is the index of the first sample in the rzero reg.
sideInfo.count1[gr][ch] = is_pos
// Zero out the last part if necessary
for is_pos < 576 {
mainData.is[gr][ch][is_pos] = 0.0
is_pos++
}
// Set the bitpos to point to the next part to read
m.setMainPos(bit_pos_end + 1)
return nil
}

148
sideinfo.go Normal file
View file

@ -0,0 +1,148 @@
// Copyright 2017 Hajime Hoshi
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package mp3
import (
"fmt"
"io"
)
func (src *source) readSideInfo(header *mpeg1FrameHeader) (*mpeg1SideInfo, error) {
nch := header.numberOfChannels()
// Calculate header audio data size
framesize := header.frameSize()
if framesize > 2000 {
return nil, fmt.Errorf("mp3: framesize = %d\n", framesize)
}
// Sideinfo is 17 bytes for one channel and 32 bytes for two
sideinfo_size := 32
if nch == 1 {
sideinfo_size = 17
}
// Main data size is the rest of the frame,including ancillary data
main_data_size := framesize - sideinfo_size - 4 // sync+header
// CRC is 2 bytes
if header.protection_bit == 0 {
main_data_size -= 2
}
// Read sideinfo from bitstream into buffer used by getSideBits()
s, err := src.getSideinfo(sideinfo_size)
if err != nil {
return nil, err
}
// Parse audio data
// Pointer to where we should start reading main data
si := &mpeg1SideInfo{}
si.main_data_begin = s.getSideBits(9)
// Get private bits. Not used for anything.
if header.mode == mpeg1ModeSingleChannel {
si.private_bits = s.getSideBits(5)
} else {
si.private_bits = s.getSideBits(3)
}
// Get scale factor selection information
for ch := 0; ch < nch; ch++ {
for scfsi_band := 0; scfsi_band < 4; scfsi_band++ {
si.scfsi[ch][scfsi_band] = s.getSideBits(1)
}
}
// Get the rest of the side information
for gr := 0; gr < 2; gr++ {
for ch := 0; ch < nch; ch++ {
si.part2_3_length[gr][ch] = s.getSideBits(12)
si.big_values[gr][ch] = s.getSideBits(9)
si.global_gain[gr][ch] = s.getSideBits(8)
si.scalefac_compress[gr][ch] = s.getSideBits(4)
si.win_switch_flag[gr][ch] = s.getSideBits(1)
if si.win_switch_flag[gr][ch] == 1 {
si.block_type[gr][ch] = s.getSideBits(2)
si.mixed_block_flag[gr][ch] = s.getSideBits(1)
for region := 0; region < 2; region++ {
si.table_select[gr][ch][region] = s.getSideBits(5)
}
for window := 0; window < 3; window++ {
si.subblock_gain[gr][ch][window] = s.getSideBits(3)
}
if (si.block_type[gr][ch] == 2) && (si.mixed_block_flag[gr][ch] == 0) {
si.region0_count[gr][ch] = 8 // Implicit
} else {
si.region0_count[gr][ch] = 7 // Implicit
}
// The standard is wrong on this!!!
// Implicit
si.region1_count[gr][ch] = 20 - si.region0_count[gr][ch]
} else {
for region := 0; region < 3; region++ {
si.table_select[gr][ch][region] = s.getSideBits(5)
}
si.region0_count[gr][ch] = s.getSideBits(4)
si.region1_count[gr][ch] = s.getSideBits(3)
si.block_type[gr][ch] = 0 // Implicit
}
si.preflag[gr][ch] = s.getSideBits(1)
si.scalefac_scale[gr][ch] = s.getSideBits(1)
si.count1table_select[gr][ch] = s.getSideBits(1)
}
}
return si, nil
}
// A sideInfoBytes is a bit reservoir for side info
type sideInfoBytes struct {
vec []int
idx int // Index into the current byte(0-7)
}
func (src *source) getSideinfo(size int) (*sideInfoBytes, error) {
buf := make([]int, size)
n := 0
var err error
for n < size && err == nil {
nn, err2 := src.getBytes(buf[n:])
n += nn
err = err2
}
if n < size {
if err == io.EOF {
return nil, fmt.Errorf("mp3: unexpected EOF at getSideinfo")
}
return nil, fmt.Errorf("mp3: couldn't read sideinfo %d bytes at pos %d: %v",
size, src.getFilepos(), err)
}
s := &sideInfoBytes{
vec: buf[:n],
}
return s, nil
}
func (s *sideInfoBytes) getSideBits(num int) int {
// Form a word of the next four bytes
// TODO: endianness?
b := make([]int, 4)
for i := range b {
if len(s.vec) > i {
b[i] = s.vec[i]
}
}
tmp := (uint32(b[0]) << 24) | (uint32(b[1]) << 16) | (uint32(b[2]) << 8) | (uint32(b[3]) << 0)
// Remove bits already used
tmp = tmp << uint(s.idx)
// Remove bits after the desired bits
tmp = tmp >> (32 - uint(num))
// Update pointers
s.vec = s.vec[(s.idx+int(num))>>3:]
s.idx = (s.idx + int(num)) & 0x07
return int(tmp)
}

124
types.go Normal file
View file

@ -0,0 +1,124 @@
// Copyright 2017 Hajime Hoshi
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package mp3
type mpeg1Layer int
const (
mpeg1LayerReserved mpeg1Layer = 0
mpeg1Layer3 mpeg1Layer = 1
mpeg1Layer2 mpeg1Layer = 2
mpeg1Layer1 mpeg1Layer = 3
)
type mpeg1Mode int
const (
mpeg1ModeStereo mpeg1Mode = iota
mpeg1ModeJointStereo
mpeg1ModeDualChannel
mpeg1ModeSingleChannel
)
// A mepg1FrameHeader is MPEG1 Layer 1-3 frame header
type mpeg1FrameHeader struct {
id int // 1 bit
layer mpeg1Layer // 2 bits
protection_bit int // 1 bit
bitrate_index int // 4 bits
sampling_frequency int // 2 bits
padding_bit int // 1 bit
private_bit int // 1 bit
mode mpeg1Mode // 2 bits
mode_extension int // 2 bits
copyright int // 1 bit
original_or_copy int // 1 bit
emphasis int // 2 bits
}
// A mpeg1SideInfo is MPEG1 Layer 3 Side Information.
// [2][2] means [gr][ch].
type mpeg1SideInfo struct {
main_data_begin int // 9 bits
private_bits int // 3 bits in mono, 5 in stereo
scfsi [2][4]int // 1 bit
part2_3_length [2][2]int // 12 bits
big_values [2][2]int // 9 bits
global_gain [2][2]int // 8 bits
scalefac_compress [2][2]int // 4 bits
win_switch_flag [2][2]int // 1 bit
block_type [2][2]int // 2 bits
mixed_block_flag [2][2]int // 1 bit
table_select [2][2][3]int // 5 bits
subblock_gain [2][2][3]int // 3 bits
region0_count [2][2]int // 4 bits
region1_count [2][2]int // 3 bits
preflag [2][2]int // 1 bit
scalefac_scale [2][2]int // 1 bit
count1table_select [2][2]int // 1 bit
count1 [2][2]int // Not in file,calc. by huff.dec.!
}
// A mpeg1MainData is MPEG1 Layer 3 Main Data.
type mpeg1MainData struct {
scalefac_l [2][2][21]int // 0-4 bits
scalefac_s [2][2][12][3]int // 0-4 bits
is [2][2][576]float32 // Huffman coded freq. lines
}
type frame struct {
prev *frame
header *mpeg1FrameHeader
sideInfo *mpeg1SideInfo
mainData *mpeg1MainData
mainDataBytes *mainDataBytes
store [2][32][18]float32
v_vec [2][1024]float32
}
var mpeg1Bitrates = map[mpeg1Layer][15]int{
mpeg1Layer1: {
0, 32000, 64000, 96000, 128000, 160000, 192000, 224000,
256000, 288000, 320000, 352000, 384000, 416000, 448000,
},
mpeg1Layer2: {
0, 32000, 48000, 56000, 64000, 80000, 96000, 112000,
128000, 160000, 192000, 224000, 256000, 320000, 384000,
},
mpeg1Layer3: {
0, 32000, 40000, 48000, 56000, 64000, 80000, 96000,
112000, 128000, 160000, 192000, 224000, 256000, 320000,
},
}
var samplingFrequency = []int{44100, 48000, 32000}
func (h *mpeg1FrameHeader) frameSize() int {
return (144*mpeg1Bitrates[h.layer][h.bitrate_index])/
samplingFrequency[h.sampling_frequency] +
int(h.padding_bit)
}
func (h *mpeg1FrameHeader) numberOfChannels() int {
if h.mode == mpeg1ModeSingleChannel {
return 1
}
return 2
}