├── .gitignore ├── AUTHORS ├── LICENSE ├── README.md ├── bench_test.go ├── decode.go ├── example ├── classic.mp3 ├── license.md ├── main.go └── mpeg2.mp3 ├── fuzzing_test.go ├── go.mod ├── go.sum ├── internal ├── bits │ ├── bits.go │ └── bits_test.go ├── consts │ └── consts.go ├── frame │ └── frame.go ├── frameheader │ └── frameheader.go ├── huffman │ └── huffman.go ├── imdct │ └── imdct.go ├── maindata │ ├── huffman.go │ └── maindata.go └── sideinfo │ └── sideinfo.go └── source.go /.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | .DS_Store 3 | -------------------------------------------------------------------------------- /AUTHORS: -------------------------------------------------------------------------------- 1 | Christopher Cooper 2 | Hajime Hoshi 3 | Sergei Dudka 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # This project is no longer maintained. 2 | 3 | # go-mp3 4 | 5 | [![Go Reference](https://pkg.go.dev/badge/github.com/hajimehoshi/go-mp3.svg)](https://pkg.go.dev/github.com/hajimehoshi/go-mp3) 6 | 7 | An MP3 decoder in pure Go based on [PDMP3](https://github.com/technosaurus/PDMP3). 8 | 9 | [Slide at golang.tokyo #11](https://docs.google.com/presentation/d/e/2PACX-1vTTXf-LWNRvMVGQ7GI4Wh8EKohot_9CMtlF4dswpYGpuYKOek5NeNP-_QZnNcRFZp9Cwm0pCcykjqDN/pub?start=false&loop=false&delayms=3000) 10 | -------------------------------------------------------------------------------- /bench_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2017 The go-mp3 Authors 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package mp3 16 | 17 | import ( 18 | "bytes" 19 | "io" 20 | "io/ioutil" 21 | "testing" 22 | ) 23 | 24 | func BenchmarkDecode(b *testing.B) { 25 | buf, err := ioutil.ReadFile("example/classic.mp3") 26 | if err != nil { 27 | b.Fatal(err) 28 | } 29 | src := bytes.NewReader(buf) 30 | for i := 0; i < b.N; i++ { 31 | if _, err := src.Seek(0, io.SeekStart); err != nil { 32 | b.Fatal(err) 33 | } 34 | d, err := NewDecoder(src) 35 | if err != nil { 36 | b.Fatal(err) 37 | } 38 | if _, err := ioutil.ReadAll(d); err != nil { 39 | b.Fatal(err) 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /decode.go: -------------------------------------------------------------------------------- 1 | // Copyright 2017 Hajime Hoshi 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package mp3 16 | 17 | import ( 18 | "errors" 19 | "io" 20 | 21 | "github.com/hajimehoshi/go-mp3/internal/consts" 22 | "github.com/hajimehoshi/go-mp3/internal/frame" 23 | "github.com/hajimehoshi/go-mp3/internal/frameheader" 24 | ) 25 | 26 | // A Decoder is a MP3-decoded stream. 27 | // 28 | // Decoder decodes its underlying source on the fly. 29 | type Decoder struct { 30 | source *source 31 | sampleRate int 32 | length int64 33 | frameStarts []int64 34 | buf []byte 35 | frame *frame.Frame 36 | pos int64 37 | bytesPerFrame int64 38 | } 39 | 40 | func (d *Decoder) readFrame() error { 41 | var err error 42 | d.frame, _, err = frame.Read(d.source, d.source.pos, d.frame) 43 | if err != nil { 44 | if err == io.EOF { 45 | return io.EOF 46 | } 47 | if _, ok := err.(*consts.UnexpectedEOF); ok { 48 | // TODO: Log here? 49 | return io.EOF 50 | } 51 | return err 52 | } 53 | d.buf = append(d.buf, d.frame.Decode()...) 54 | return nil 55 | } 56 | 57 | // Read is io.Reader's Read. 58 | func (d *Decoder) Read(buf []byte) (int, error) { 59 | for len(d.buf) == 0 { 60 | if err := d.readFrame(); err != nil { 61 | return 0, err 62 | } 63 | } 64 | n := copy(buf, d.buf) 65 | d.buf = d.buf[n:] 66 | d.pos += int64(n) 67 | return n, nil 68 | } 69 | 70 | // Seek is io.Seeker's Seek. 71 | // 72 | // Seek returns an error when the underlying source is not io.Seeker. 73 | // 74 | // Note that seek uses a byte offset but samples are aligned to 4 bytes (2 75 | // channels, 2 bytes each). Be careful to seek to an offset that is divisible by 76 | // 4 if you want to read at full sample boundaries. 77 | func (d *Decoder) Seek(offset int64, whence int) (int64, error) { 78 | if offset == 0 && whence == io.SeekCurrent { 79 | // Handle the special case of asking for the current position specially. 80 | return d.pos, nil 81 | } 82 | 83 | npos := int64(0) 84 | switch whence { 85 | case io.SeekStart: 86 | npos = offset 87 | case io.SeekCurrent: 88 | npos = d.pos + offset 89 | case io.SeekEnd: 90 | npos = d.Length() + offset 91 | default: 92 | return 0, errors.New("mp3: invalid whence") 93 | } 94 | d.pos = npos 95 | d.buf = nil 96 | d.frame = nil 97 | f := d.pos / d.bytesPerFrame 98 | // If the frame is not first, read the previous ahead of reading that 99 | // because the previous frame can affect the targeted frame. 100 | if f > 0 { 101 | f-- 102 | if _, err := d.source.Seek(d.frameStarts[f], 0); err != nil { 103 | return 0, err 104 | } 105 | if err := d.readFrame(); err != nil { 106 | return 0, err 107 | } 108 | if err := d.readFrame(); err != nil { 109 | return 0, err 110 | } 111 | d.buf = d.buf[d.bytesPerFrame+(d.pos%d.bytesPerFrame):] 112 | } else { 113 | if _, err := d.source.Seek(d.frameStarts[f], 0); err != nil { 114 | return 0, err 115 | } 116 | if err := d.readFrame(); err != nil { 117 | return 0, err 118 | } 119 | d.buf = d.buf[d.pos:] 120 | } 121 | return npos, nil 122 | } 123 | 124 | // SampleRate returns the sample rate like 44100. 125 | // 126 | // Note that the sample rate is retrieved from the first frame. 127 | func (d *Decoder) SampleRate() int { 128 | return d.sampleRate 129 | } 130 | 131 | func (d *Decoder) ensureFrameStartsAndLength() error { 132 | if d.length != invalidLength { 133 | return nil 134 | } 135 | 136 | if _, ok := d.source.reader.(io.Seeker); !ok { 137 | return nil 138 | } 139 | 140 | // Keep the current position. 141 | pos, err := d.source.Seek(0, io.SeekCurrent) 142 | if err != nil { 143 | return err 144 | } 145 | if err := d.source.rewind(); err != nil { 146 | return err 147 | } 148 | 149 | if err := d.source.skipTags(); err != nil { 150 | return err 151 | } 152 | l := int64(0) 153 | for { 154 | h, pos, err := frameheader.Read(d.source, d.source.pos) 155 | if err != nil { 156 | if err == io.EOF { 157 | break 158 | } 159 | if _, ok := err.(*consts.UnexpectedEOF); ok { 160 | // TODO: Log here? 161 | break 162 | } 163 | return err 164 | } 165 | d.frameStarts = append(d.frameStarts, pos) 166 | d.bytesPerFrame = int64(h.BytesPerFrame()) 167 | l += d.bytesPerFrame 168 | 169 | framesize, err := h.FrameSize() 170 | if err != nil { 171 | return err 172 | } 173 | buf := make([]byte, framesize-4) 174 | if _, err := d.source.ReadFull(buf); err != nil { 175 | if err == io.EOF { 176 | break 177 | } 178 | return err 179 | } 180 | } 181 | d.length = l 182 | 183 | if _, err := d.source.Seek(pos, io.SeekStart); err != nil { 184 | return err 185 | } 186 | return nil 187 | } 188 | 189 | const invalidLength = -1 190 | 191 | // Length returns the total size in bytes. 192 | // 193 | // Length returns -1 when the total size is not available 194 | // e.g. when the given source is not io.Seeker. 195 | func (d *Decoder) Length() int64 { 196 | return d.length 197 | } 198 | 199 | // NewDecoder decodes the given io.Reader and returns a decoded stream. 200 | // 201 | // The stream is always formatted as 16bit (little endian) 2 channels 202 | // even if the source is single channel MP3. 203 | // Thus, a sample always consists of 4 bytes. 204 | func NewDecoder(r io.Reader) (*Decoder, error) { 205 | s := &source{ 206 | reader: r, 207 | } 208 | d := &Decoder{ 209 | source: s, 210 | length: invalidLength, 211 | } 212 | 213 | if err := s.skipTags(); err != nil { 214 | return nil, err 215 | } 216 | // TODO: Is readFrame here really needed? 217 | if err := d.readFrame(); err != nil { 218 | return nil, err 219 | } 220 | freq, err := d.frame.SamplingFrequency() 221 | if err != nil { 222 | return nil, err 223 | } 224 | d.sampleRate = freq 225 | 226 | if err := d.ensureFrameStartsAndLength(); err != nil { 227 | return nil, err 228 | } 229 | 230 | return d, nil 231 | } 232 | -------------------------------------------------------------------------------- /example/classic.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hajimehoshi/go-mp3/fa70610237c8c313ef920c7cff6c31efbbc55ffa/example/classic.mp3 -------------------------------------------------------------------------------- /example/license.md: -------------------------------------------------------------------------------- 1 | # classic.mp3 2 | 3 | Licensed under the EFF OAL License 4 | 5 | http://freemusicarchive.org/music/Advent_Chamber_Orchestra/Selections_from_the_2005-2006_Season/Advent_Chamber_Orchestra_-_04_-_Mozart_-_A_Little_Night_Music_allegro 6 | 7 | # mpeg2.mp3 8 | 9 | This audio file contains speech synthesized parts of Alice's Adventures in Wonderland by Lewis Carroll, published in 1865. Due to the release date this work is under public domain. -------------------------------------------------------------------------------- /example/main.go: -------------------------------------------------------------------------------- 1 | // Copyright 2017 Hajime Hoshi 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package main 16 | 17 | import ( 18 | "fmt" 19 | "log" 20 | "os" 21 | "time" 22 | 23 | "github.com/hajimehoshi/oto/v2" 24 | 25 | "github.com/hajimehoshi/go-mp3" 26 | ) 27 | 28 | func run() error { 29 | f, err := os.Open("classic.mp3") 30 | if err != nil { 31 | return err 32 | } 33 | defer f.Close() 34 | 35 | d, err := mp3.NewDecoder(f) 36 | if err != nil { 37 | return err 38 | } 39 | 40 | c, ready, err := oto.NewContext(d.SampleRate(), 2, 2) 41 | if err != nil { 42 | return err 43 | } 44 | <-ready 45 | 46 | p := c.NewPlayer(d) 47 | defer p.Close() 48 | p.Play() 49 | 50 | fmt.Printf("Length: %d[bytes]\n", d.Length()) 51 | for { 52 | time.Sleep(time.Second) 53 | if !p.IsPlaying() { 54 | break 55 | } 56 | } 57 | 58 | return nil 59 | } 60 | 61 | func main() { 62 | if err := run(); err != nil { 63 | log.Fatal(err) 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /example/mpeg2.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hajimehoshi/go-mp3/fa70610237c8c313ef920c7cff6c31efbbc55ffa/example/mpeg2.mp3 -------------------------------------------------------------------------------- /fuzzing_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2017 Hajime Hoshi 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package mp3 16 | 17 | import ( 18 | "bytes" 19 | "testing" 20 | ) 21 | 22 | func TestFuzzing(t *testing.T) { 23 | inputs := []string{ 24 | // #3 25 | "\xff\xfa500000000000\xff\xff0000" + 26 | "00000000000000000000" + 27 | "00000000000000000000" + 28 | "00000000000000000000" + 29 | "00000000000000000000" + 30 | "00000000000000000000" + 31 | "00000000000000000000" + 32 | "0000", 33 | "\xff\xfb\x100004000094\xff000000" + 34 | "00000000000000000000" + 35 | "00\u007f0\xff\xee\u007f\xff\xee\u007f\xff\xff\u007f\xff\xff\xee\u007f\xff\xff0" + 36 | "\xff\xff00\xff\xee\u007f\xff0000\u007f00\xff00\xee0" + 37 | "000\xff000\xff\xff\xee\u007f0\xff0000\u007f\xff0" + 38 | "00\xff0", 39 | "\xff\xfb\x100004000094\xff000000" + 40 | "00000000000000000000" + 41 | "00\u007f0\xff\xee\u007f\xff\xee\u007f\xff\xff\u007f\xff\xff\xee\u007f\xff\xff\u007f" + 42 | "\xff\xff\u007f0\xff\xee\u007f\xff0000\u007f00\xff\xff\xee\xee0" + 43 | "0\xee\u007f\xff000\xff\xff\xee\u007f0\xff0000\u007f\xff0" + 44 | "0\xff\xff0", 45 | "\xff\xfa\x1000000000000000000" + 46 | "00000000000000000000" + 47 | "000000000000000000\xff\xff" + 48 | "0\u007f\xff\xff\u007f\xff\xff\u007f\xff\xff\xfc0\xff\xef\xbf0\xef\xbf00" + 49 | "0\xff\xee\u007f\xff\xff\u007f\xff\xff\xee\u007f\xff\xff\u007f\xff\xff\u007f\xff00" + 50 | "\xff\xff00", 51 | "\xff\xfa00000031000000000n" + 52 | "s0f00000000000000000" + 53 | "00000000000000000000" + 54 | "00000000000000000000" + 55 | "00000000000000000000" + 56 | "00000000000000000000" + 57 | "00000000000000000000" + 58 | "00000000\u007f\xff\xff000\xff\xee", 59 | "\xff\xfa\x1000000000000000000" + 60 | "00000000000000000000" + 61 | "00000000000000000000" + 62 | "00000000000000\xbf0\xef\xbf00" + 63 | "0\xff\xee0\xff\xff\u007f\xff\xff\xee\u007f\xff\xff\u007f\xff\xff\u007f\xff00" + 64 | "\xff0\xee0", 65 | "\xff\xfa\x100000050000000000\u007f" + 66 | "00000000000000000000" + 67 | "0000000000\xee\u007f0\xff\xff\xff\xff\u007f\xff\xff" + 68 | "\xee\u007f\xff\xff\u007f\xff\xff\u007f\xff\xff\xfc\xee\xff\xef\xbf0\xef\xbf00" + 69 | "0\xff\xee\u007f\xff\xff\u007f\xff\xff\xee\u007f\xff\xff\u007f\xff\xff\u007f\xff0\t" + 70 | "\xff\xff\xee\xee", 71 | // #22 72 | "\xff\xfa%00000000000000000" + 73 | "000000000000s0000000" + 74 | "00000000000000000000" + 75 | "00000000000000000000" + 76 | "00000000000000000000" + 77 | "00000000000000000000", 78 | // #23 79 | "\xff\xfb%S000000v000\x00\x010000" + 80 | "00000000000000000000" + 81 | "0000\xf4000000000000000" + 82 | "00000000000000000000" + 83 | "00000000000000000000" + 84 | "00000000000000000000", 85 | // #24 86 | "\xff\xfb0x000000\xf9000\x00\x030000" + 87 | "000000000000\xf70000000" + 88 | "\x900000000000000000000" + 89 | "00000000000000000000" + 90 | "00000000000000000000" + 91 | "00000000000000000000" + 92 | "00000000000000000000" + 93 | "00000000000000000000" + 94 | "00000000000000000000" + 95 | "00000000000000000000" + 96 | "00000000000000000000" + 97 | "00000000000000000000" + 98 | "00000000000000000000" + 99 | "00000000000000000000" + 100 | "00000000000000000000" + 101 | "0000000000000", 102 | } 103 | for _, input := range inputs { 104 | b := bytes.NewReader([]byte(input)) 105 | _, _ = NewDecoder(b) 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/hajimehoshi/go-mp3 2 | 3 | go 1.14 4 | 5 | require github.com/hajimehoshi/oto/v2 v2.3.1 6 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/hajimehoshi/oto/v2 v2.3.1 h1:qrLKpNus2UfD674oxckKjNJmesp9hMh7u7QCrStB3Rc= 2 | github.com/hajimehoshi/oto/v2 v2.3.1/go.mod h1:seWLbgHH7AyUMYKfKYT9pg7PhUu9/SisyJvNTT+ASQo= 3 | golang.org/x/sys v0.0.0-20220712014510-0a85c31ab51e h1:NHvCuwuS43lGnYhten69ZWqi2QOj/CiDNcKbVqwVoew= 4 | golang.org/x/sys v0.0.0-20220712014510-0a85c31ab51e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 5 | -------------------------------------------------------------------------------- /internal/bits/bits.go: -------------------------------------------------------------------------------- 1 | // Copyright 2017 Hajime Hoshi 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package bits 16 | 17 | type Bits struct { 18 | vec []byte 19 | bitPos int 20 | bytePos int 21 | } 22 | 23 | func New(vec []byte) *Bits { 24 | return &Bits{ 25 | vec: vec, 26 | } 27 | } 28 | 29 | func Append(bits *Bits, buf []byte) *Bits { 30 | return New(append(bits.vec, buf...)) 31 | } 32 | 33 | func (b *Bits) Bit() int { 34 | if len(b.vec) <= b.bytePos { 35 | // TODO: Should this return error? 36 | return 0 37 | } 38 | tmp := uint(b.vec[b.bytePos]) >> (7 - uint(b.bitPos)) 39 | tmp &= 0x01 40 | b.bytePos += (b.bitPos + 1) >> 3 41 | b.bitPos = (b.bitPos + 1) & 0x07 42 | return int(tmp) 43 | } 44 | 45 | func (b *Bits) Bits(num int) int { 46 | if num == 0 { 47 | return 0 48 | } 49 | if len(b.vec) <= b.bytePos { 50 | // TODO: Should this return error? 51 | return 0 52 | } 53 | bb := make([]byte, 4) 54 | copy(bb, b.vec[b.bytePos:]) 55 | tmp := (uint32(bb[0]) << 24) | (uint32(bb[1]) << 16) | (uint32(bb[2]) << 8) | (uint32(bb[3])) 56 | tmp <<= uint(b.bitPos) 57 | tmp >>= (32 - uint(num)) 58 | b.bytePos += (b.bitPos + num) >> 3 59 | b.bitPos = (b.bitPos + num) & 0x07 60 | return int(tmp) 61 | } 62 | 63 | func (b *Bits) BitPos() int { 64 | return b.bytePos<<3 + b.bitPos 65 | } 66 | 67 | func (b *Bits) SetPos(pos int) { 68 | b.bytePos = pos >> 3 69 | b.bitPos = pos & 0x7 70 | } 71 | 72 | func (b *Bits) LenInBytes() int { 73 | return len(b.vec) 74 | } 75 | 76 | func (b *Bits) Tail(offset int) []byte { 77 | return b.vec[len(b.vec)-offset:] 78 | } 79 | -------------------------------------------------------------------------------- /internal/bits/bits_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2017 Hajime Hoshi 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package bits_test 16 | 17 | import ( 18 | "testing" 19 | 20 | . "github.com/hajimehoshi/go-mp3/internal/bits" 21 | ) 22 | 23 | func TestBits(t *testing.T) { 24 | b1 := byte(85) // 01010101 25 | b2 := byte(170) // 10101010 26 | b3 := byte(204) // 11001100 27 | b4 := byte(51) // 00110011 28 | b := New([]byte{b1, b2, b3, b4}) 29 | if b.Bits(1) != 0 { 30 | t.Fail() 31 | } 32 | if b.Bits(1) != 1 { 33 | t.Fail() 34 | } 35 | if b.Bits(1) != 0 { 36 | t.Fail() 37 | } 38 | if b.Bits(1) != 1 { 39 | t.Fail() 40 | } 41 | if b.Bits(8) != 90 /* 01011010 */ { 42 | t.Fail() 43 | } 44 | if b.Bits(12) != 2764 /* 101011001100 */ { 45 | t.Fail() 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /internal/consts/consts.go: -------------------------------------------------------------------------------- 1 | // Copyright 2017 Hajime Hoshi 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package consts 16 | 17 | import ( 18 | "fmt" 19 | ) 20 | 21 | type UnexpectedEOF struct { 22 | At string 23 | } 24 | 25 | func (u *UnexpectedEOF) Error() string { 26 | return fmt.Sprintf("mp3: unexpected EOF at %s", u.At) 27 | } 28 | 29 | type Version int 30 | 31 | const ( 32 | Version2_5 Version = 0 33 | VersionReserved Version = 1 34 | Version2 Version = 2 35 | Version1 Version = 3 36 | ) 37 | 38 | type Layer int 39 | 40 | const ( 41 | LayerReserved Layer = 0 42 | Layer3 Layer = 1 43 | Layer2 Layer = 2 44 | Layer1 Layer = 3 45 | ) 46 | 47 | type Mode int 48 | 49 | const ( 50 | ModeStereo Mode = 0 51 | ModeJointStereo Mode = 1 52 | ModeDualChannel Mode = 2 53 | ModeSingleChannel Mode = 3 54 | ) 55 | 56 | const ( 57 | SamplesPerGr = 576 58 | GranulesMpeg1 = 2 59 | ) 60 | 61 | type SamplingFrequency int 62 | 63 | const ( 64 | SamplingFrequencyReserved SamplingFrequency = 3 65 | ) 66 | 67 | const ( 68 | SfBandIndicesLong = 0 69 | SfBandIndicesShort = 1 70 | ) 71 | 72 | var SfBandIndices = [2][3][2][]int{ 73 | { // MPEG 1 74 | { // Layer 3 75 | {0, 4, 8, 12, 16, 20, 24, 30, 36, 44, 52, 62, 74, 90, 110, 134, 162, 196, 238, 288, 342, 418, 576}, 76 | {0, 4, 8, 12, 16, 22, 30, 40, 52, 66, 84, 106, 136, 192}, 77 | }, 78 | { // Layer 2 79 | {0, 4, 8, 12, 16, 20, 24, 30, 36, 42, 50, 60, 72, 88, 106, 128, 156, 190, 230, 276, 330, 384, 576}, 80 | {0, 4, 8, 12, 16, 22, 28, 38, 50, 64, 80, 100, 126, 192}, 81 | }, 82 | { // Layer 1 83 | {0, 4, 8, 12, 16, 20, 24, 30, 36, 44, 54, 66, 82, 102, 126, 156, 194, 240, 296, 364, 448, 550, 576}, 84 | {0, 4, 8, 12, 16, 22, 30, 42, 58, 78, 104, 138, 180, 192}, 85 | }, 86 | }, 87 | { // MPEG 2 88 | { // Layer 3 89 | {0, 6, 12, 18, 24, 30, 36, 44, 54, 66, 80, 96, 116, 140, 168, 200, 238, 284, 336, 396, 464, 522, 576}, 90 | {0, 4, 8, 12, 18, 24, 32, 42, 56, 74, 100, 132, 174, 192}, 91 | }, 92 | { // Layer 2 93 | {0, 6, 12, 18, 24, 30, 36, 44, 54, 66, 80, 96, 114, 136, 162, 194, 232, 278, 332, 394, 464, 540, 576}, 94 | {0, 4, 8, 12, 18, 26, 36, 48, 62, 80, 104, 136, 180, 192}, 95 | }, 96 | { // Layer 1 97 | {0, 6, 12, 18, 24, 30, 36, 44, 54, 66, 80, 96, 116, 140, 168, 200, 238, 284, 336, 396, 464, 522, 576}, 98 | {0, 4, 8, 12, 18, 26, 36, 48, 62, 80, 104, 134, 174, 192}, 99 | }, 100 | }, 101 | } 102 | -------------------------------------------------------------------------------- /internal/frame/frame.go: -------------------------------------------------------------------------------- 1 | // Copyright 2017 Hajime Hoshi 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package frame 16 | 17 | import ( 18 | "fmt" 19 | "io" 20 | "math" 21 | 22 | "github.com/hajimehoshi/go-mp3/internal/bits" 23 | "github.com/hajimehoshi/go-mp3/internal/consts" 24 | "github.com/hajimehoshi/go-mp3/internal/frameheader" 25 | "github.com/hajimehoshi/go-mp3/internal/imdct" 26 | "github.com/hajimehoshi/go-mp3/internal/maindata" 27 | "github.com/hajimehoshi/go-mp3/internal/sideinfo" 28 | ) 29 | 30 | var ( 31 | powtab34 = make([]float64, 8207) 32 | pretab = []float64{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 3, 3, 2, 0} 33 | ) 34 | 35 | func init() { 36 | for i := range powtab34 { 37 | powtab34[i] = math.Pow(float64(i), 4.0/3.0) 38 | } 39 | } 40 | 41 | type Frame struct { 42 | header frameheader.FrameHeader 43 | sideInfo *sideinfo.SideInfo 44 | mainData *maindata.MainData 45 | 46 | mainDataBits *bits.Bits 47 | store [2][32][18]float32 48 | v_vec [2][1024]float32 49 | } 50 | 51 | type FullReader interface { 52 | ReadFull([]byte) (int, error) 53 | } 54 | 55 | func readCRC(source FullReader) error { 56 | buf := make([]byte, 2) 57 | if n, err := source.ReadFull(buf); n < 2 { 58 | if err == io.EOF { 59 | return &consts.UnexpectedEOF{"readCRC"} 60 | } 61 | return fmt.Errorf("mp3: error at readCRC: %v", err) 62 | } 63 | return nil 64 | } 65 | 66 | func Read(source FullReader, position int64, prev *Frame) (frame *Frame, startPosition int64, err error) { 67 | h, pos, err := frameheader.Read(source, position) 68 | if err != nil { 69 | return nil, 0, err 70 | } 71 | 72 | if h.ProtectionBit() == 0 { 73 | if err := readCRC(source); err != nil { 74 | return nil, 0, err 75 | } 76 | } 77 | 78 | if h.ID() == consts.Version2_5 { 79 | return nil, 0, fmt.Errorf("mp3: MPEG version 2.5 is not supported") 80 | } 81 | if h.Layer() != consts.Layer3 { 82 | return nil, 0, fmt.Errorf("mp3: only layer3 (want %d; got %d) is supported", consts.Layer3, h.Layer()) 83 | } 84 | 85 | si, err := sideinfo.Read(source, h) 86 | if err != nil { 87 | return nil, 0, err 88 | } 89 | 90 | // If there's not enough main data in the bit reservoir, 91 | // signal to calling function so that decoding isn't done! 92 | // Get main data (scalefactors and Huffman coded frequency data) 93 | var prevM *bits.Bits 94 | if prev != nil { 95 | prevM = prev.mainDataBits 96 | } 97 | md, mdb, err := maindata.Read(source, prevM, h, si) 98 | if err != nil { 99 | return nil, 0, err 100 | } 101 | nf := &Frame{ 102 | header: h, 103 | sideInfo: si, 104 | mainData: md, 105 | mainDataBits: mdb, 106 | } 107 | if prev != nil { 108 | nf.store = prev.store 109 | nf.v_vec = prev.v_vec 110 | } 111 | return nf, pos, nil 112 | } 113 | 114 | func (f *Frame) SamplingFrequency() (int, error) { 115 | return f.header.SamplingFrequencyValue() 116 | } 117 | 118 | func (f *Frame) Decode() []byte { 119 | out := make([]byte, f.header.BytesPerFrame()) 120 | nch := f.header.NumberOfChannels() 121 | for gr := 0; gr < f.header.Granules(); gr++ { 122 | for ch := 0; ch < nch; ch++ { 123 | f.requantize(gr, ch) 124 | f.reorder(gr, ch) 125 | } 126 | f.stereo(gr) 127 | for ch := 0; ch < nch; ch++ { 128 | f.antialias(gr, ch) 129 | f.hybridSynthesis(gr, ch) 130 | f.frequencyInversion(gr, ch) 131 | f.subbandSynthesis(gr, ch, out[consts.SamplesPerGr*4*gr:]) 132 | } 133 | } 134 | return out 135 | } 136 | 137 | func (f *Frame) requantizeProcessLong(gr, ch, is_pos, sfb int) { 138 | sf_mult := 0.5 139 | if f.sideInfo.ScalefacScale[gr][ch] != 0 { 140 | sf_mult = 1.0 141 | } 142 | pf_x_pt := float64(f.sideInfo.Preflag[gr][ch]) * pretab[sfb] 143 | idx := -(sf_mult * (float64(f.mainData.ScalefacL[gr][ch][sfb]) + pf_x_pt)) + 144 | 0.25*(float64(f.sideInfo.GlobalGain[gr][ch])-210) 145 | tmp1 := math.Pow(2.0, idx) 146 | tmp2 := 0.0 147 | if f.mainData.Is[gr][ch][is_pos] < 0.0 { 148 | tmp2 = -powtab34[int(-f.mainData.Is[gr][ch][is_pos])] 149 | } else { 150 | tmp2 = powtab34[int(f.mainData.Is[gr][ch][is_pos])] 151 | } 152 | f.mainData.Is[gr][ch][is_pos] = float32(tmp1 * tmp2) 153 | } 154 | 155 | func (f *Frame) requantizeProcessShort(gr, ch, is_pos, sfb, win int) { 156 | sf_mult := 0.5 157 | if f.sideInfo.ScalefacScale[gr][ch] != 0 { 158 | sf_mult = 1.0 159 | } 160 | idx := -(sf_mult * float64(f.mainData.ScalefacS[gr][ch][sfb][win])) + 161 | 0.25*(float64(f.sideInfo.GlobalGain[gr][ch])-210.0- 162 | 8.0*float64(f.sideInfo.SubblockGain[gr][ch][win])) 163 | tmp1 := math.Pow(2.0, idx) 164 | tmp2 := 0.0 165 | if f.mainData.Is[gr][ch][is_pos] < 0 { 166 | tmp2 = -powtab34[int(-f.mainData.Is[gr][ch][is_pos])] 167 | } else { 168 | tmp2 = powtab34[int(f.mainData.Is[gr][ch][is_pos])] 169 | } 170 | f.mainData.Is[gr][ch][is_pos] = float32(tmp1 * tmp2) 171 | } 172 | 173 | func getSfBandIndicesArray(header *frameheader.FrameHeader) ([]int, []int) { 174 | sfreq := header.SamplingFrequency() // Setup sampling frequency index 175 | lsf := header.LowSamplingFrequency() 176 | sfBandIndicesShort := consts.SfBandIndices[lsf][sfreq][consts.SfBandIndicesShort] 177 | sfBandIndicesLong := consts.SfBandIndices[lsf][sfreq][consts.SfBandIndicesLong] 178 | return sfBandIndicesLong, sfBandIndicesShort 179 | } 180 | 181 | func (f *Frame) requantize(gr int, ch int) { 182 | sfBandIndicesLong, sfBandIndicesShort := getSfBandIndicesArray(&f.header) 183 | // Determine type of block to process 184 | if f.sideInfo.WinSwitchFlag[gr][ch] == 1 && f.sideInfo.BlockType[gr][ch] == 2 { // Short blocks 185 | // Check if the first two subbands 186 | // (=2*18 samples = 8 long or 3 short sfb's) uses long blocks 187 | if f.sideInfo.MixedBlockFlag[gr][ch] != 0 { // 2 longbl. sb first 188 | // First process the 2 long block subbands at the start 189 | sfb := 0 190 | next_sfb := sfBandIndicesLong[sfb+1] 191 | for i := 0; i < 36; i++ { 192 | if i == next_sfb { 193 | sfb++ 194 | next_sfb = sfBandIndicesLong[sfb+1] 195 | } 196 | f.requantizeProcessLong(gr, ch, i, sfb) 197 | } 198 | // And next the remaining,non-zero,bands which uses short blocks 199 | sfb = 3 200 | next_sfb = sfBandIndicesShort[sfb+1] * 3 201 | win_len := sfBandIndicesShort[sfb+1] - 202 | sfBandIndicesShort[sfb] 203 | 204 | for i := 36; i < int(f.sideInfo.Count1[gr][ch]); /* i++ done below! */ { 205 | // Check if we're into the next scalefac band 206 | if i == next_sfb { 207 | sfb++ 208 | next_sfb = sfBandIndicesShort[sfb+1] * 3 209 | win_len = sfBandIndicesShort[sfb+1] - 210 | sfBandIndicesShort[sfb] 211 | } 212 | for win := 0; win < 3; win++ { 213 | for j := 0; j < win_len; j++ { 214 | f.requantizeProcessShort(gr, ch, i, sfb, win) 215 | i++ 216 | } 217 | } 218 | 219 | } 220 | } else { // Only short blocks 221 | sfb := 0 222 | next_sfb := sfBandIndicesShort[sfb+1] * 3 223 | win_len := sfBandIndicesShort[sfb+1] - 224 | sfBandIndicesShort[sfb] 225 | for i := 0; i < int(f.sideInfo.Count1[gr][ch]); /* i++ done below! */ { 226 | // Check if we're into the next scalefac band 227 | if i == next_sfb { 228 | sfb++ 229 | next_sfb = sfBandIndicesShort[sfb+1] * 3 230 | win_len = sfBandIndicesShort[sfb+1] - 231 | sfBandIndicesShort[sfb] 232 | } 233 | for win := 0; win < 3; win++ { 234 | for j := 0; j < win_len; j++ { 235 | f.requantizeProcessShort(gr, ch, i, sfb, win) 236 | i++ 237 | } 238 | } 239 | } 240 | } 241 | } else { // Only long blocks 242 | sfb := 0 243 | next_sfb := sfBandIndicesLong[sfb+1] 244 | for i := 0; i < int(f.sideInfo.Count1[gr][ch]); i++ { 245 | if i == next_sfb { 246 | sfb++ 247 | next_sfb = sfBandIndicesLong[sfb+1] 248 | } 249 | f.requantizeProcessLong(gr, ch, i, sfb) 250 | } 251 | } 252 | } 253 | 254 | func (f *Frame) reorder(gr int, ch int) { 255 | re := make([]float32, consts.SamplesPerGr) 256 | 257 | _, sfBandIndicesShort := getSfBandIndicesArray(&f.header) 258 | 259 | // Only reorder short blocks 260 | if (f.sideInfo.WinSwitchFlag[gr][ch] == 1) && (f.sideInfo.BlockType[gr][ch] == 2) { // Short blocks 261 | // Check if the first two subbands 262 | // (=2*18 samples = 8 long or 3 short sfb's) uses long blocks 263 | sfb := 0 264 | // 2 longbl. sb first 265 | if f.sideInfo.MixedBlockFlag[gr][ch] != 0 { 266 | sfb = 3 267 | } 268 | next_sfb := sfBandIndicesShort[sfb+1] * 3 269 | win_len := sfBandIndicesShort[sfb+1] - sfBandIndicesShort[sfb] 270 | i := 36 271 | if sfb == 0 { 272 | i = 0 273 | } 274 | for i < consts.SamplesPerGr { 275 | // Check if we're into the next scalefac band 276 | if i == next_sfb { 277 | // Copy reordered data back to the original vector 278 | j := 3 * sfBandIndicesShort[sfb] 279 | copy(f.mainData.Is[gr][ch][j:j+3*win_len], re[0:3*win_len]) 280 | // Check if this band is above the rzero region,if so we're done 281 | if i >= f.sideInfo.Count1[gr][ch] { 282 | return 283 | } 284 | sfb++ 285 | next_sfb = sfBandIndicesShort[sfb+1] * 3 286 | win_len = sfBandIndicesShort[sfb+1] - sfBandIndicesShort[sfb] 287 | } 288 | for win := 0; win < 3; win++ { // Do the actual reordering 289 | for j := 0; j < win_len; j++ { 290 | re[j*3+win] = f.mainData.Is[gr][ch][i] 291 | i++ 292 | } 293 | } 294 | } 295 | // Copy reordered data of last band back to original vector 296 | j := 3 * sfBandIndicesShort[12] 297 | copy(f.mainData.Is[gr][ch][j:j+3*win_len], re[0:3*win_len]) 298 | } 299 | } 300 | 301 | var ( 302 | isRatios = []float32{0.000000, 0.267949, 0.577350, 1.000000, 1.732051, 3.732051} 303 | ) 304 | 305 | func (f *Frame) stereoProcessIntensityLong(gr int, sfb int) { 306 | is_ratio_l := float32(0) 307 | is_ratio_r := float32(0) 308 | // Check that((is_pos[sfb]=scalefac) < 7) => no intensity stereo 309 | if is_pos := f.mainData.ScalefacL[gr][0][sfb]; is_pos < 7 { 310 | sfBandIndicesLong, _ := getSfBandIndicesArray(&f.header) 311 | sfb_start := sfBandIndicesLong[sfb] 312 | sfb_stop := sfBandIndicesLong[sfb+1] 313 | if is_pos == 6 { // tan((6*PI)/12 = PI/2) needs special treatment! 314 | is_ratio_l = 1.0 315 | is_ratio_r = 0.0 316 | } else { 317 | is_ratio_l = isRatios[is_pos] / (1.0 + isRatios[is_pos]) 318 | is_ratio_r = 1.0 / (1.0 + isRatios[is_pos]) 319 | } 320 | // Now decode all samples in this scale factor band 321 | for i := sfb_start; i < sfb_stop; i++ { 322 | f.mainData.Is[gr][0][i] *= is_ratio_l 323 | f.mainData.Is[gr][1][i] *= is_ratio_r 324 | } 325 | } 326 | } 327 | 328 | func (f *Frame) stereoProcessIntensityShort(gr int, sfb int) { 329 | is_ratio_l := float32(0) 330 | is_ratio_r := float32(0) 331 | _, sfBandIndicesShort := getSfBandIndicesArray(&f.header) 332 | // The window length 333 | win_len := sfBandIndicesShort[sfb+1] - sfBandIndicesShort[sfb] 334 | // The three windows within the band has different scalefactors 335 | for win := 0; win < 3; win++ { 336 | // Check that((is_pos[sfb]=scalefac) < 7) => no intensity stereo 337 | is_pos := f.mainData.ScalefacS[gr][0][sfb][win] 338 | if is_pos < 7 { 339 | sfb_start := sfBandIndicesShort[sfb]*3 + win_len*win 340 | sfb_stop := sfb_start + win_len 341 | if is_pos == 6 { // tan((6*PI)/12 = PI/2) needs special treatment! 342 | is_ratio_l = 1.0 343 | is_ratio_r = 0.0 344 | } else { 345 | is_ratio_l = isRatios[is_pos] / (1.0 + isRatios[is_pos]) 346 | is_ratio_r = 1.0 / (1.0 + isRatios[is_pos]) 347 | } 348 | // Now decode all samples in this scale factor band 349 | for i := sfb_start; i < sfb_stop; i++ { 350 | // https://github.com/technosaurus/PDMP3/issues/3 351 | f.mainData.Is[gr][0][i] *= is_ratio_l 352 | f.mainData.Is[gr][1][i] *= is_ratio_r 353 | } 354 | } 355 | } 356 | } 357 | 358 | func (f *Frame) stereo(gr int) { 359 | if f.header.UseMSStereo() { 360 | // Determine how many frequency lines to transform 361 | i := 1 362 | if f.sideInfo.Count1[gr][0] > f.sideInfo.Count1[gr][1] { 363 | i = 0 364 | } 365 | max_pos := int(f.sideInfo.Count1[gr][i]) 366 | // Do the actual processing 367 | const invSqrt2 = math.Sqrt2 / 2 368 | for i := 0; i < max_pos; i++ { 369 | left := (f.mainData.Is[gr][0][i] + f.mainData.Is[gr][1][i]) * invSqrt2 370 | right := (f.mainData.Is[gr][0][i] - f.mainData.Is[gr][1][i]) * invSqrt2 371 | f.mainData.Is[gr][0][i] = left 372 | f.mainData.Is[gr][1][i] = right 373 | } 374 | } 375 | 376 | if f.header.UseIntensityStereo() { 377 | sfBandIndicesLong, sfBandIndicesShort := getSfBandIndicesArray(&f.header) 378 | // First band that is intensity stereo encoded is first band scale factor 379 | // band on or above count1 frequency line. N.B.: Intensity stereo coding is 380 | // only done for higher subbands, but logic is here for lower subbands. 381 | // Determine type of block to process 382 | if (f.sideInfo.WinSwitchFlag[gr][0] == 1) && 383 | (f.sideInfo.BlockType[gr][0] == 2) { // Short blocks 384 | // Check if the first two subbands 385 | // (=2*18 samples = 8 long or 3 short sfb's) uses long blocks 386 | if f.sideInfo.MixedBlockFlag[gr][0] != 0 { // 2 longbl. sb first 387 | for sfb := 0; sfb < 8; sfb++ { // First process 8 sfb's at start 388 | // Is this scale factor band above count1 for the right channel? 389 | if sfBandIndicesLong[sfb] >= f.sideInfo.Count1[gr][1] { 390 | f.stereoProcessIntensityLong(gr, sfb) 391 | } 392 | } 393 | // And next the remaining bands which uses short blocks 394 | for sfb := 3; sfb < 12; sfb++ { 395 | // Is this scale factor band above count1 for the right channel? 396 | if sfBandIndicesShort[sfb]*3 >= f.sideInfo.Count1[gr][1] { 397 | f.stereoProcessIntensityShort(gr, sfb) 398 | } 399 | } 400 | } else { // Only short blocks 401 | for sfb := 0; sfb < 12; sfb++ { 402 | // Is this scale factor band above count1 for the right channel? 403 | if sfBandIndicesShort[sfb]*3 >= f.sideInfo.Count1[gr][1] { 404 | f.stereoProcessIntensityShort(gr, sfb) 405 | } 406 | } 407 | } 408 | } else { // Only long blocks 409 | for sfb := 0; sfb < 21; sfb++ { 410 | // Is this scale factor band above count1 for the right channel? 411 | if sfBandIndicesLong[sfb] >= f.sideInfo.Count1[gr][1] { 412 | f.stereoProcessIntensityLong(gr, sfb) 413 | } 414 | } 415 | } 416 | } 417 | } 418 | 419 | var ( 420 | cs = []float32{0.857493, 0.881742, 0.949629, 0.983315, 0.995518, 0.999161, 0.999899, 0.999993} 421 | ca = []float32{-0.514496, -0.471732, -0.313377, -0.181913, -0.094574, -0.040966, -0.014199, -0.003700} 422 | ) 423 | 424 | func (f *Frame) antialias(gr int, ch int) { 425 | // No antialiasing is done for short blocks 426 | if (f.sideInfo.WinSwitchFlag[gr][ch] == 1) && 427 | (f.sideInfo.BlockType[gr][ch] == 2) && 428 | (f.sideInfo.MixedBlockFlag[gr][ch]) == 0 { 429 | return 430 | } 431 | // Setup the limit for how many subbands to transform 432 | sblim := 32 433 | if (f.sideInfo.WinSwitchFlag[gr][ch] == 1) && 434 | (f.sideInfo.BlockType[gr][ch] == 2) && 435 | (f.sideInfo.MixedBlockFlag[gr][ch] == 1) { 436 | sblim = 2 437 | } 438 | // Do the actual antialiasing 439 | for sb := 1; sb < sblim; sb++ { 440 | for i := 0; i < 8; i++ { 441 | li := 18*sb - 1 - i 442 | ui := 18*sb + i 443 | lb := f.mainData.Is[gr][ch][li]*cs[i] - f.mainData.Is[gr][ch][ui]*ca[i] 444 | ub := f.mainData.Is[gr][ch][ui]*cs[i] + f.mainData.Is[gr][ch][li]*ca[i] 445 | f.mainData.Is[gr][ch][li] = lb 446 | f.mainData.Is[gr][ch][ui] = ub 447 | } 448 | } 449 | } 450 | 451 | func (f *Frame) hybridSynthesis(gr int, ch int) { 452 | // Loop through all 32 subbands 453 | for sb := 0; sb < 32; sb++ { 454 | // Determine blocktype for this subband 455 | bt := int(f.sideInfo.BlockType[gr][ch]) 456 | if (f.sideInfo.WinSwitchFlag[gr][ch] == 1) && 457 | (f.sideInfo.MixedBlockFlag[gr][ch] == 1) && (sb < 2) { 458 | bt = 0 459 | } 460 | // Do the inverse modified DCT and windowing 461 | in := make([]float32, 18) 462 | for i := range in { 463 | in[i] = f.mainData.Is[gr][ch][sb*18+i] 464 | } 465 | rawout := imdct.Win(in, bt) 466 | // Overlapp add with stored vector into main_data vector 467 | for i := 0; i < 18; i++ { 468 | f.mainData.Is[gr][ch][sb*18+i] = rawout[i] + f.store[ch][sb][i] 469 | f.store[ch][sb][i] = rawout[i+18] 470 | } 471 | } 472 | } 473 | 474 | func (f *Frame) frequencyInversion(gr int, ch int) { 475 | for sb := 1; sb < 32; sb += 2 { 476 | for i := 1; i < 18; i += 2 { 477 | f.mainData.Is[gr][ch][sb*18+i] = -f.mainData.Is[gr][ch][sb*18+i] 478 | } 479 | } 480 | } 481 | 482 | var synthNWin = [64][32]float32{} 483 | 484 | func init() { 485 | for i := 0; i < 64; i++ { 486 | for j := 0; j < 32; j++ { 487 | synthNWin[i][j] = 488 | float32(math.Cos(float64((16+i)*(2*j+1)) * (math.Pi / 64.0))) 489 | } 490 | } 491 | } 492 | 493 | var synthDtbl = [512]float32{ 494 | 0.000000000, -0.000015259, -0.000015259, -0.000015259, 495 | -0.000015259, -0.000015259, -0.000015259, -0.000030518, 496 | -0.000030518, -0.000030518, -0.000030518, -0.000045776, 497 | -0.000045776, -0.000061035, -0.000061035, -0.000076294, 498 | -0.000076294, -0.000091553, -0.000106812, -0.000106812, 499 | -0.000122070, -0.000137329, -0.000152588, -0.000167847, 500 | -0.000198364, -0.000213623, -0.000244141, -0.000259399, 501 | -0.000289917, -0.000320435, -0.000366211, -0.000396729, 502 | -0.000442505, -0.000473022, -0.000534058, -0.000579834, 503 | -0.000625610, -0.000686646, -0.000747681, -0.000808716, 504 | -0.000885010, -0.000961304, -0.001037598, -0.001113892, 505 | -0.001205444, -0.001296997, -0.001388550, -0.001480103, 506 | -0.001586914, -0.001693726, -0.001785278, -0.001907349, 507 | -0.002014160, -0.002120972, -0.002243042, -0.002349854, 508 | -0.002456665, -0.002578735, -0.002685547, -0.002792358, 509 | -0.002899170, -0.002990723, -0.003082275, -0.003173828, 510 | 0.003250122, 0.003326416, 0.003387451, 0.003433228, 511 | 0.003463745, 0.003479004, 0.003479004, 0.003463745, 512 | 0.003417969, 0.003372192, 0.003280640, 0.003173828, 513 | 0.003051758, 0.002883911, 0.002700806, 0.002487183, 514 | 0.002227783, 0.001937866, 0.001617432, 0.001266479, 515 | 0.000869751, 0.000442505, -0.000030518, -0.000549316, 516 | -0.001098633, -0.001693726, -0.002334595, -0.003005981, 517 | -0.003723145, -0.004486084, -0.005294800, -0.006118774, 518 | -0.007003784, -0.007919312, -0.008865356, -0.009841919, 519 | -0.010848999, -0.011886597, -0.012939453, -0.014022827, 520 | -0.015121460, -0.016235352, -0.017349243, -0.018463135, 521 | -0.019577026, -0.020690918, -0.021789551, -0.022857666, 522 | -0.023910522, -0.024932861, -0.025909424, -0.026840210, 523 | -0.027725220, -0.028533936, -0.029281616, -0.029937744, 524 | -0.030532837, -0.031005859, -0.031387329, -0.031661987, 525 | -0.031814575, -0.031845093, -0.031738281, -0.031478882, 526 | 0.031082153, 0.030517578, 0.029785156, 0.028884888, 527 | 0.027801514, 0.026535034, 0.025085449, 0.023422241, 528 | 0.021575928, 0.019531250, 0.017257690, 0.014801025, 529 | 0.012115479, 0.009231567, 0.006134033, 0.002822876, 530 | -0.000686646, -0.004394531, -0.008316040, -0.012420654, 531 | -0.016708374, -0.021179199, -0.025817871, -0.030609131, 532 | -0.035552979, -0.040634155, -0.045837402, -0.051132202, 533 | -0.056533813, -0.061996460, -0.067520142, -0.073059082, 534 | -0.078628540, -0.084182739, -0.089706421, -0.095169067, 535 | -0.100540161, -0.105819702, -0.110946655, -0.115921021, 536 | -0.120697021, -0.125259399, -0.129562378, -0.133590698, 537 | -0.137298584, -0.140670776, -0.143676758, -0.146255493, 538 | -0.148422241, -0.150115967, -0.151306152, -0.151962280, 539 | -0.152069092, -0.151596069, -0.150497437, -0.148773193, 540 | -0.146362305, -0.143264771, -0.139450073, -0.134887695, 541 | -0.129577637, -0.123474121, -0.116577148, -0.108856201, 542 | 0.100311279, 0.090927124, 0.080688477, 0.069595337, 543 | 0.057617188, 0.044784546, 0.031082153, 0.016510010, 544 | 0.001068115, -0.015228271, -0.032379150, -0.050354004, 545 | -0.069168091, -0.088775635, -0.109161377, -0.130310059, 546 | -0.152206421, -0.174789429, -0.198059082, -0.221984863, 547 | -0.246505737, -0.271591187, -0.297210693, -0.323318481, 548 | -0.349868774, -0.376800537, -0.404083252, -0.431655884, 549 | -0.459472656, -0.487472534, -0.515609741, -0.543823242, 550 | -0.572036743, -0.600219727, -0.628295898, -0.656219482, 551 | -0.683914185, -0.711318970, -0.738372803, -0.765029907, 552 | -0.791213989, -0.816864014, -0.841949463, -0.866363525, 553 | -0.890090942, -0.913055420, -0.935195923, -0.956481934, 554 | -0.976852417, -0.996246338, -1.014617920, -1.031936646, 555 | -1.048156738, -1.063217163, -1.077117920, -1.089782715, 556 | -1.101211548, -1.111373901, -1.120223999, -1.127746582, 557 | -1.133926392, -1.138763428, -1.142211914, -1.144287109, 558 | 1.144989014, 1.144287109, 1.142211914, 1.138763428, 559 | 1.133926392, 1.127746582, 1.120223999, 1.111373901, 560 | 1.101211548, 1.089782715, 1.077117920, 1.063217163, 561 | 1.048156738, 1.031936646, 1.014617920, 0.996246338, 562 | 0.976852417, 0.956481934, 0.935195923, 0.913055420, 563 | 0.890090942, 0.866363525, 0.841949463, 0.816864014, 564 | 0.791213989, 0.765029907, 0.738372803, 0.711318970, 565 | 0.683914185, 0.656219482, 0.628295898, 0.600219727, 566 | 0.572036743, 0.543823242, 0.515609741, 0.487472534, 567 | 0.459472656, 0.431655884, 0.404083252, 0.376800537, 568 | 0.349868774, 0.323318481, 0.297210693, 0.271591187, 569 | 0.246505737, 0.221984863, 0.198059082, 0.174789429, 570 | 0.152206421, 0.130310059, 0.109161377, 0.088775635, 571 | 0.069168091, 0.050354004, 0.032379150, 0.015228271, 572 | -0.001068115, -0.016510010, -0.031082153, -0.044784546, 573 | -0.057617188, -0.069595337, -0.080688477, -0.090927124, 574 | 0.100311279, 0.108856201, 0.116577148, 0.123474121, 575 | 0.129577637, 0.134887695, 0.139450073, 0.143264771, 576 | 0.146362305, 0.148773193, 0.150497437, 0.151596069, 577 | 0.152069092, 0.151962280, 0.151306152, 0.150115967, 578 | 0.148422241, 0.146255493, 0.143676758, 0.140670776, 579 | 0.137298584, 0.133590698, 0.129562378, 0.125259399, 580 | 0.120697021, 0.115921021, 0.110946655, 0.105819702, 581 | 0.100540161, 0.095169067, 0.089706421, 0.084182739, 582 | 0.078628540, 0.073059082, 0.067520142, 0.061996460, 583 | 0.056533813, 0.051132202, 0.045837402, 0.040634155, 584 | 0.035552979, 0.030609131, 0.025817871, 0.021179199, 585 | 0.016708374, 0.012420654, 0.008316040, 0.004394531, 586 | 0.000686646, -0.002822876, -0.006134033, -0.009231567, 587 | -0.012115479, -0.014801025, -0.017257690, -0.019531250, 588 | -0.021575928, -0.023422241, -0.025085449, -0.026535034, 589 | -0.027801514, -0.028884888, -0.029785156, -0.030517578, 590 | 0.031082153, 0.031478882, 0.031738281, 0.031845093, 591 | 0.031814575, 0.031661987, 0.031387329, 0.031005859, 592 | 0.030532837, 0.029937744, 0.029281616, 0.028533936, 593 | 0.027725220, 0.026840210, 0.025909424, 0.024932861, 594 | 0.023910522, 0.022857666, 0.021789551, 0.020690918, 595 | 0.019577026, 0.018463135, 0.017349243, 0.016235352, 596 | 0.015121460, 0.014022827, 0.012939453, 0.011886597, 597 | 0.010848999, 0.009841919, 0.008865356, 0.007919312, 598 | 0.007003784, 0.006118774, 0.005294800, 0.004486084, 599 | 0.003723145, 0.003005981, 0.002334595, 0.001693726, 600 | 0.001098633, 0.000549316, 0.000030518, -0.000442505, 601 | -0.000869751, -0.001266479, -0.001617432, -0.001937866, 602 | -0.002227783, -0.002487183, -0.002700806, -0.002883911, 603 | -0.003051758, -0.003173828, -0.003280640, -0.003372192, 604 | -0.003417969, -0.003463745, -0.003479004, -0.003479004, 605 | -0.003463745, -0.003433228, -0.003387451, -0.003326416, 606 | 0.003250122, 0.003173828, 0.003082275, 0.002990723, 607 | 0.002899170, 0.002792358, 0.002685547, 0.002578735, 608 | 0.002456665, 0.002349854, 0.002243042, 0.002120972, 609 | 0.002014160, 0.001907349, 0.001785278, 0.001693726, 610 | 0.001586914, 0.001480103, 0.001388550, 0.001296997, 611 | 0.001205444, 0.001113892, 0.001037598, 0.000961304, 612 | 0.000885010, 0.000808716, 0.000747681, 0.000686646, 613 | 0.000625610, 0.000579834, 0.000534058, 0.000473022, 614 | 0.000442505, 0.000396729, 0.000366211, 0.000320435, 615 | 0.000289917, 0.000259399, 0.000244141, 0.000213623, 616 | 0.000198364, 0.000167847, 0.000152588, 0.000137329, 617 | 0.000122070, 0.000106812, 0.000106812, 0.000091553, 618 | 0.000076294, 0.000076294, 0.000061035, 0.000061035, 619 | 0.000045776, 0.000045776, 0.000030518, 0.000030518, 620 | 0.000030518, 0.000030518, 0.000015259, 0.000015259, 621 | 0.000015259, 0.000015259, 0.000015259, 0.000015259, 622 | } 623 | 624 | func (f *Frame) subbandSynthesis(gr int, ch int, out []byte) { 625 | u_vec := make([]float32, 512) 626 | s_vec := make([]float32, 32) 627 | 628 | nch := f.header.NumberOfChannels() 629 | // Setup the n_win windowing vector and the v_vec intermediate vector 630 | for ss := 0; ss < 18; ss++ { // Loop through 18 samples in 32 subbands 631 | copy(f.v_vec[ch][64:1024], f.v_vec[ch][0:1024-64]) 632 | d := f.mainData.Is[gr][ch] 633 | for i := 0; i < 32; i++ { // Copy next 32 time samples to a temp vector 634 | s_vec[i] = d[i*18+ss] 635 | } 636 | for i := 0; i < 64; i++ { // Matrix multiply input with n_win[][] matrix 637 | sum := float32(0) 638 | for j := 0; j < 32; j++ { 639 | sum += synthNWin[i][j] * s_vec[j] 640 | } 641 | f.v_vec[ch][i] = sum 642 | } 643 | v := f.v_vec[ch] 644 | for i := 0; i < 512; i += 64 { // Build the U vector 645 | copy(u_vec[i:i+32], v[(i<<1):(i<<1)+32]) 646 | copy(u_vec[i+32:i+64], v[(i<<1)+96:(i<<1)+128]) 647 | } 648 | for i := 0; i < 512; i++ { // Window by u_vec[i] with synthDtbl[i] 649 | u_vec[i] *= synthDtbl[i] 650 | } 651 | for i := 0; i < 32; i++ { // Calc 32 samples,store in outdata vector 652 | sum := float32(0) 653 | for j := 0; j < 512; j += 32 { 654 | sum += u_vec[j+i] 655 | } 656 | // sum now contains time sample 32*ss+i. Convert to 16-bit signed int 657 | samp := int(sum * 32767) 658 | if samp > 32767 { 659 | samp = 32767 660 | } else if samp < -32767 { 661 | samp = -32767 662 | } 663 | s := int16(samp) 664 | idx := 4 * (32*ss + i) 665 | if nch == 1 { 666 | // We always run in stereo mode and duplicate channels here for mono. 667 | out[idx] = byte(s) 668 | out[idx+1] = byte(s >> 8) 669 | out[idx+2] = byte(s) 670 | out[idx+3] = byte(s >> 8) 671 | continue 672 | } 673 | if ch == 0 { 674 | out[idx] = byte(s) 675 | out[idx+1] = byte(s >> 8) 676 | } else { 677 | out[idx+2] = byte(s) 678 | out[idx+3] = byte(s >> 8) 679 | } 680 | } 681 | } 682 | } 683 | -------------------------------------------------------------------------------- /internal/frameheader/frameheader.go: -------------------------------------------------------------------------------- 1 | // Copyright 2017 Hajime Hoshi 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package frameheader 16 | 17 | import ( 18 | "errors" 19 | "fmt" 20 | "io" 21 | 22 | "github.com/hajimehoshi/go-mp3/internal/consts" 23 | ) 24 | 25 | // A mepg1FrameHeader is MPEG1 Layer 1-3 frame header 26 | type FrameHeader uint32 27 | 28 | // ID returns this header's ID stored in position 20,19 29 | func (f FrameHeader) ID() consts.Version { 30 | return consts.Version((f & 0x00180000) >> 19) 31 | } 32 | 33 | // Layer returns the mpeg layer of this frame stored in position 18,17 34 | func (f FrameHeader) Layer() consts.Layer { 35 | return consts.Layer((f & 0x00060000) >> 17) 36 | } 37 | 38 | // ProtectionBit returns the protection bit stored in position 16 39 | func (f FrameHeader) ProtectionBit() int { 40 | return int(f&0x00010000) >> 16 41 | } 42 | 43 | // BirateIndex returns the bitrate index stored in position 15,12 44 | func (f FrameHeader) BitrateIndex() int { 45 | return int(f&0x0000f000) >> 12 46 | } 47 | 48 | // SamplingFrequency returns the SamplingFrequency in Hz stored in position 11,10 49 | func (f FrameHeader) SamplingFrequency() consts.SamplingFrequency { 50 | return consts.SamplingFrequency(int(f&0x00000c00) >> 10) 51 | } 52 | 53 | func (f FrameHeader) SamplingFrequencyValue() (int, error) { 54 | switch f.SamplingFrequency() { 55 | case 0: 56 | return 44100 >> uint(f.LowSamplingFrequency()), nil 57 | case 1: 58 | return 48000 >> uint(f.LowSamplingFrequency()), nil 59 | case 2: 60 | return 32000 >> uint(f.LowSamplingFrequency()), nil 61 | } 62 | return 0, errors.New("mp3: frame header has invalid sample frequency") 63 | } 64 | 65 | // PaddingBit returns the padding bit stored in position 9 66 | func (f FrameHeader) PaddingBit() int { 67 | return int(f&0x00000200) >> 9 68 | } 69 | 70 | // PrivateBit returns the private bit stored in position 8 - this bit may be used to store arbitrary data to be used 71 | // by an application 72 | func (f FrameHeader) PrivateBit() int { 73 | return int(f&0x00000100) >> 8 74 | } 75 | 76 | // Mode returns the channel mode, stored in position 7,6 77 | func (f FrameHeader) Mode() consts.Mode { 78 | return consts.Mode((f & 0x000000c0) >> 6) 79 | } 80 | 81 | // modeExtension returns the mode_extension - for use with Joint Stereo - stored in position 4,5 82 | func (f FrameHeader) modeExtension() int { 83 | return int(f&0x00000030) >> 4 84 | } 85 | 86 | // UseMSStereo returns a boolean value indicating whether the frame uses middle/side stereo. 87 | func (f FrameHeader) UseMSStereo() bool { 88 | if f.Mode() != consts.ModeJointStereo { 89 | return false 90 | } 91 | return f.modeExtension()&0x2 != 0 92 | } 93 | 94 | // UseIntensityStereo returns a boolean value indicating whether the frame uses intensity stereo. 95 | func (f FrameHeader) UseIntensityStereo() bool { 96 | if f.Mode() != consts.ModeJointStereo { 97 | return false 98 | } 99 | return f.modeExtension()&0x1 != 0 100 | } 101 | 102 | // Copyright returns whether or not this recording is copywritten - stored in position 3 103 | func (f FrameHeader) Copyright() int { 104 | return int(f&0x00000008) >> 3 105 | } 106 | 107 | // OriginalOrCopy returns whether or not this is an Original recording or a copy of one - stored in position 2 108 | func (f FrameHeader) OriginalOrCopy() int { 109 | return int(f&0x00000004) >> 2 110 | } 111 | 112 | // Emphasis returns emphasis - the emphasis indication is here to tell the decoder that the file must be de-emphasized - stored in position 0,1 113 | func (f FrameHeader) Emphasis() int { 114 | return int(f&0x00000003) >> 0 115 | } 116 | 117 | // LowSamplingFrequency returns whether the frame is encoded in a low sampling frequency => 0 = MPEG-1, 1 = MPEG-2/2.5 118 | func (f FrameHeader) LowSamplingFrequency() int { 119 | if f.ID() == consts.Version1 { 120 | return 0 121 | } 122 | return 1 123 | } 124 | 125 | func (f FrameHeader) BytesPerFrame() int { 126 | return consts.SamplesPerGr * f.Granules() * 4 127 | } 128 | 129 | func (f FrameHeader) Granules() int { 130 | return consts.GranulesMpeg1 >> uint(f.LowSamplingFrequency()) // MPEG2 uses only 1 granule 131 | } 132 | 133 | // IsValid returns a boolean value indicating whether the header is valid or not. 134 | func (f FrameHeader) IsValid() bool { 135 | const sync = 0xffe00000 136 | if (f & sync) != sync { 137 | return false 138 | } 139 | if f.ID() == consts.VersionReserved { 140 | return false 141 | } 142 | if f.BitrateIndex() == 15 { 143 | return false 144 | } 145 | if f.SamplingFrequency() == consts.SamplingFrequencyReserved { 146 | return false 147 | } 148 | if f.Layer() == consts.LayerReserved { 149 | return false 150 | } 151 | if f.Emphasis() == 2 { 152 | return false 153 | } 154 | return true 155 | } 156 | 157 | func (f FrameHeader) Bitrate() int { 158 | bitrates := [2][3][16]int{ 159 | { 160 | // MPEG 1 Layer 3 161 | {0, 32000, 40000, 48000, 56000, 64000, 80000, 96000, 162 | 112000, 128000, 160000, 192000, 224000, 256000, 320000}, 163 | 164 | // MPEG 1 Layer 2 165 | {0, 32000, 48000, 56000, 64000, 80000, 96000, 112000, 166 | 128000, 160000, 192000, 224000, 256000, 320000, 384000}, 167 | 168 | // MPEG 1 Layer 1 169 | {0, 32000, 64000, 96000, 128000, 160000, 192000, 224000, 170 | 256000, 288000, 320000, 352000, 384000, 416000, 448000}, 171 | }, 172 | { 173 | // MPEG2 2 Layer 3 174 | {0, 8000, 16000, 24000, 32000, 40000, 48000, 56000, 175 | 64000, 80000, 96000, 112000, 128000, 144000, 160000}, 176 | 177 | // MPEG 2 Layer 2 178 | {0, 8000, 16000, 24000, 32000, 40000, 48000, 56000, 179 | 64000, 80000, 96000, 112000, 128000, 144000, 160000}, 180 | 181 | // MPEG 2 Layer 1 182 | {0, 32000, 48000, 56000, 64000, 80000, 96000, 112000, 183 | 128000, 144000, 160000, 176000, 192000, 224000, 256000}, 184 | }, 185 | } 186 | return bitrates[f.LowSamplingFrequency()][f.Layer()-1][f.BitrateIndex()] 187 | } 188 | 189 | func (f FrameHeader) FrameSize() (int, error) { 190 | freq, err := f.SamplingFrequencyValue() 191 | if err != nil { 192 | return 0, err 193 | } 194 | size := ((144*f.Bitrate())/freq + 195 | int(f.PaddingBit())) >> uint(f.LowSamplingFrequency()) 196 | return size, nil 197 | } 198 | 199 | func (f FrameHeader) SideInfoSize() int { 200 | mono := f.Mode() == consts.ModeSingleChannel 201 | var sideinfo_size int 202 | if f.LowSamplingFrequency() == 1 { 203 | if mono { 204 | sideinfo_size = 9 205 | } else { 206 | sideinfo_size = 17 207 | } 208 | } else { 209 | if mono { 210 | sideinfo_size = 17 211 | } else { 212 | sideinfo_size = 32 213 | } 214 | } 215 | return sideinfo_size 216 | } 217 | 218 | func (f FrameHeader) NumberOfChannels() int { 219 | if f.Mode() == consts.ModeSingleChannel { 220 | return 1 221 | } 222 | return 2 223 | } 224 | 225 | type FullReader interface { 226 | ReadFull([]byte) (int, error) 227 | } 228 | 229 | func Read(source FullReader, position int64) (h FrameHeader, startPosition int64, err error) { 230 | buf := make([]byte, 4) 231 | if n, err := source.ReadFull(buf); n < 4 { 232 | if err == io.EOF { 233 | if n == 0 { 234 | // Expected EOF 235 | return 0, 0, io.EOF 236 | } 237 | return 0, 0, &consts.UnexpectedEOF{"readHeader (1)"} 238 | } 239 | return 0, 0, err 240 | } 241 | 242 | b1 := uint32(buf[0]) 243 | b2 := uint32(buf[1]) 244 | b3 := uint32(buf[2]) 245 | b4 := uint32(buf[3]) 246 | header := FrameHeader((b1 << 24) | (b2 << 16) | (b3 << 8) | (b4 << 0)) 247 | for !header.IsValid() { 248 | b1 = b2 249 | b2 = b3 250 | b3 = b4 251 | 252 | buf := make([]byte, 1) 253 | if _, err := source.ReadFull(buf); err != nil { 254 | if err == io.EOF { 255 | return 0, 0, &consts.UnexpectedEOF{"readHeader (2)"} 256 | } 257 | return 0, 0, err 258 | } 259 | b4 = uint32(buf[0]) 260 | header = FrameHeader((b1 << 24) | (b2 << 16) | (b3 << 8) | (b4 << 0)) 261 | position++ 262 | } 263 | 264 | // If we get here we've found the sync word, and can decode the header 265 | // which is in the low 20 bits of the 32-bit sync+header word. 266 | 267 | if header.BitrateIndex() == 0 { 268 | return 0, 0, fmt.Errorf("mp3: free bitrate format is not supported. Header word is 0x%08x at position %d", 269 | header, position) 270 | } 271 | return header, position, nil 272 | } 273 | -------------------------------------------------------------------------------- /internal/huffman/huffman.go: -------------------------------------------------------------------------------- 1 | // Copyright 2017 Hajime Hoshi 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package huffman 16 | 17 | import ( 18 | "fmt" 19 | 20 | "github.com/hajimehoshi/go-mp3/internal/bits" 21 | ) 22 | 23 | var huffmanTable = []uint16{ 24 | // 1 25 | 0x0201, 0x0000, 0x0201, 0x0010, 0x0201, 0x0001, 0x0011, 26 | // 2 27 | 0x0201, 0x0000, 0x0401, 0x0201, 0x0010, 0x0001, 0x0201, 0x0011, 0x0401, 0x0201, 0x0020, 28 | 0x0021, 0x0201, 0x0012, 0x0201, 0x0002, 0x0022, 29 | // 3 30 | 0x0401, 0x0201, 0x0000, 0x0001, 0x0201, 0x0011, 0x0201, 0x0010, 0x0401, 0x0201, 0x0020, 31 | 0x0021, 0x0201, 0x0012, 0x0201, 0x0002, 0x0022, 32 | // 5 33 | 0x0201, 0x0000, 0x0401, 0x0201, 0x0010, 0x0001, 0x0201, 0x0011, 0x0801, 0x0401, 0x0201, 34 | 0x0020, 0x0002, 0x0201, 0x0021, 0x0012, 0x0801, 0x0401, 0x0201, 0x0022, 0x0030, 0x0201, 35 | 0x0003, 0x0013, 0x0201, 0x0031, 0x0201, 0x0032, 0x0201, 0x0023, 0x0033, 36 | // 6 37 | 0x0601, 0x0401, 0x0201, 0x0000, 0x0010, 0x0011, 0x0601, 0x0201, 0x0001, 0x0201, 0x0020, 38 | 0x0021, 0x0601, 0x0201, 0x0012, 0x0201, 0x0002, 0x0022, 0x0401, 0x0201, 0x0031, 0x0013, 39 | 0x0401, 0x0201, 0x0030, 0x0032, 0x0201, 0x0023, 0x0201, 0x0003, 0x0033, 40 | // 7 41 | 0x0201, 0x0000, 0x0401, 0x0201, 0x0010, 0x0001, 0x0801, 0x0201, 0x0011, 0x0401, 0x0201, 42 | 0x0020, 0x0002, 0x0021, 0x1201, 0x0601, 0x0201, 0x0012, 0x0201, 0x0022, 0x0030, 0x0401, 43 | 0x0201, 0x0031, 0x0013, 0x0401, 0x0201, 0x0003, 0x0032, 0x0201, 0x0023, 0x0004, 0x0a01, 44 | 0x0401, 0x0201, 0x0040, 0x0041, 0x0201, 0x0014, 0x0201, 0x0042, 0x0024, 0x0c01, 0x0601, 45 | 0x0401, 0x0201, 0x0033, 0x0043, 0x0050, 0x0401, 0x0201, 0x0034, 0x0005, 0x0051, 0x0601, 46 | 0x0201, 0x0015, 0x0201, 0x0052, 0x0025, 0x0401, 0x0201, 0x0044, 0x0035, 0x0401, 0x0201, 47 | 0x0053, 0x0054, 0x0201, 0x0045, 0x0055, 48 | // 8 49 | 0x0601, 0x0201, 0x0000, 0x0201, 0x0010, 0x0001, 0x0201, 0x0011, 0x0401, 0x0201, 0x0021, 50 | 0x0012, 0x0e01, 0x0401, 0x0201, 0x0020, 0x0002, 0x0201, 0x0022, 0x0401, 0x0201, 0x0030, 51 | 0x0003, 0x0201, 0x0031, 0x0013, 0x0e01, 0x0801, 0x0401, 0x0201, 0x0032, 0x0023, 0x0201, 52 | 0x0040, 0x0004, 0x0201, 0x0041, 0x0201, 0x0014, 0x0042, 0x0c01, 0x0601, 0x0201, 0x0024, 53 | 0x0201, 0x0033, 0x0050, 0x0401, 0x0201, 0x0043, 0x0034, 0x0051, 0x0601, 0x0201, 0x0015, 54 | 0x0201, 0x0005, 0x0052, 0x0601, 0x0201, 0x0025, 0x0201, 0x0044, 0x0035, 0x0201, 0x0053, 55 | 0x0201, 0x0045, 0x0201, 0x0054, 0x0055, 56 | // 9 57 | 0x0801, 0x0401, 0x0201, 0x0000, 0x0010, 0x0201, 0x0001, 0x0011, 0x0a01, 0x0401, 0x0201, 58 | 0x0020, 0x0021, 0x0201, 0x0012, 0x0201, 0x0002, 0x0022, 0x0c01, 0x0601, 0x0401, 0x0201, 59 | 0x0030, 0x0003, 0x0031, 0x0201, 0x0013, 0x0201, 0x0032, 0x0023, 0x0c01, 0x0401, 0x0201, 60 | 0x0041, 0x0014, 0x0401, 0x0201, 0x0040, 0x0033, 0x0201, 0x0042, 0x0024, 0x0a01, 0x0601, 61 | 0x0401, 0x0201, 0x0004, 0x0050, 0x0043, 0x0201, 0x0034, 0x0051, 0x0801, 0x0401, 0x0201, 62 | 0x0015, 0x0052, 0x0201, 0x0025, 0x0044, 0x0601, 0x0401, 0x0201, 0x0005, 0x0054, 0x0053, 63 | 0x0201, 0x0035, 0x0201, 0x0045, 0x0055, 64 | // 10 65 | 0x0201, 0x0000, 0x0401, 0x0201, 0x0010, 0x0001, 0x0a01, 0x0201, 0x0011, 0x0401, 0x0201, 66 | 0x0020, 0x0002, 0x0201, 0x0021, 0x0012, 0x1c01, 0x0801, 0x0401, 0x0201, 0x0022, 0x0030, 67 | 0x0201, 0x0031, 0x0013, 0x0801, 0x0401, 0x0201, 0x0003, 0x0032, 0x0201, 0x0023, 0x0040, 68 | 0x0401, 0x0201, 0x0041, 0x0014, 0x0401, 0x0201, 0x0004, 0x0033, 0x0201, 0x0042, 0x0024, 69 | 0x1c01, 0x0a01, 0x0601, 0x0401, 0x0201, 0x0050, 0x0005, 0x0060, 0x0201, 0x0061, 0x0016, 70 | 0x0c01, 0x0601, 0x0401, 0x0201, 0x0043, 0x0034, 0x0051, 0x0201, 0x0015, 0x0201, 0x0052, 71 | 0x0025, 0x0401, 0x0201, 0x0026, 0x0036, 0x0071, 0x1401, 0x0801, 0x0201, 0x0017, 0x0401, 72 | 0x0201, 0x0044, 0x0053, 0x0006, 0x0601, 0x0401, 0x0201, 0x0035, 0x0045, 0x0062, 0x0201, 73 | 0x0070, 0x0201, 0x0007, 0x0064, 0x0e01, 0x0401, 0x0201, 0x0072, 0x0027, 0x0601, 0x0201, 74 | 0x0063, 0x0201, 0x0054, 0x0055, 0x0201, 0x0046, 0x0073, 0x0801, 0x0401, 0x0201, 0x0037, 75 | 0x0065, 0x0201, 0x0056, 0x0074, 0x0601, 0x0201, 0x0047, 0x0201, 0x0066, 0x0075, 0x0401, 76 | 0x0201, 0x0057, 0x0076, 0x0201, 0x0067, 0x0077, 77 | // 11 78 | 0x0601, 0x0201, 0x0000, 0x0201, 0x0010, 0x0001, 0x0801, 0x0201, 0x0011, 0x0401, 0x0201, 79 | 0x0020, 0x0002, 0x0012, 0x1801, 0x0801, 0x0201, 0x0021, 0x0201, 0x0022, 0x0201, 0x0030, 80 | 0x0003, 0x0401, 0x0201, 0x0031, 0x0013, 0x0401, 0x0201, 0x0032, 0x0023, 0x0401, 0x0201, 81 | 0x0040, 0x0004, 0x0201, 0x0041, 0x0014, 0x1e01, 0x1001, 0x0a01, 0x0401, 0x0201, 0x0042, 82 | 0x0024, 0x0401, 0x0201, 0x0033, 0x0043, 0x0050, 0x0401, 0x0201, 0x0034, 0x0051, 0x0061, 83 | 0x0601, 0x0201, 0x0016, 0x0201, 0x0006, 0x0026, 0x0201, 0x0062, 0x0201, 0x0015, 0x0201, 84 | 0x0005, 0x0052, 0x1001, 0x0a01, 0x0601, 0x0401, 0x0201, 0x0025, 0x0044, 0x0060, 0x0201, 85 | 0x0063, 0x0036, 0x0401, 0x0201, 0x0070, 0x0017, 0x0071, 0x1001, 0x0601, 0x0401, 0x0201, 86 | 0x0007, 0x0064, 0x0072, 0x0201, 0x0027, 0x0401, 0x0201, 0x0053, 0x0035, 0x0201, 0x0054, 87 | 0x0045, 0x0a01, 0x0401, 0x0201, 0x0046, 0x0073, 0x0201, 0x0037, 0x0201, 0x0065, 0x0056, 88 | 0x0a01, 0x0601, 0x0401, 0x0201, 0x0055, 0x0057, 0x0074, 0x0201, 0x0047, 0x0066, 0x0401, 89 | 0x0201, 0x0075, 0x0076, 0x0201, 0x0067, 0x0077, 90 | // 12 91 | 0x0c01, 0x0401, 0x0201, 0x0010, 0x0001, 0x0201, 0x0011, 0x0201, 0x0000, 0x0201, 0x0020, 92 | 0x0002, 0x1001, 0x0401, 0x0201, 0x0021, 0x0012, 0x0401, 0x0201, 0x0022, 0x0031, 0x0201, 93 | 0x0013, 0x0201, 0x0030, 0x0201, 0x0003, 0x0040, 0x1a01, 0x0801, 0x0401, 0x0201, 0x0032, 94 | 0x0023, 0x0201, 0x0041, 0x0033, 0x0a01, 0x0401, 0x0201, 0x0014, 0x0042, 0x0201, 0x0024, 95 | 0x0201, 0x0004, 0x0050, 0x0401, 0x0201, 0x0043, 0x0034, 0x0201, 0x0051, 0x0015, 0x1c01, 96 | 0x0e01, 0x0801, 0x0401, 0x0201, 0x0052, 0x0025, 0x0201, 0x0053, 0x0035, 0x0401, 0x0201, 97 | 0x0060, 0x0016, 0x0061, 0x0401, 0x0201, 0x0062, 0x0026, 0x0601, 0x0401, 0x0201, 0x0005, 98 | 0x0006, 0x0044, 0x0201, 0x0054, 0x0045, 0x1201, 0x0a01, 0x0401, 0x0201, 0x0063, 0x0036, 99 | 0x0401, 0x0201, 0x0070, 0x0007, 0x0071, 0x0401, 0x0201, 0x0017, 0x0064, 0x0201, 0x0046, 100 | 0x0072, 0x0a01, 0x0601, 0x0201, 0x0027, 0x0201, 0x0055, 0x0073, 0x0201, 0x0037, 0x0056, 101 | 0x0801, 0x0401, 0x0201, 0x0065, 0x0074, 0x0201, 0x0047, 0x0066, 0x0401, 0x0201, 0x0075, 102 | 0x0057, 0x0201, 0x0076, 0x0201, 0x0067, 0x0077, 103 | // 13 104 | 0x0201, 0x0000, 0x0601, 0x0201, 0x0010, 0x0201, 0x0001, 0x0011, 0x1c01, 0x0801, 0x0401, 105 | 0x0201, 0x0020, 0x0002, 0x0201, 0x0021, 0x0012, 0x0801, 0x0401, 0x0201, 0x0022, 0x0030, 106 | 0x0201, 0x0003, 0x0031, 0x0601, 0x0201, 0x0013, 0x0201, 0x0032, 0x0023, 0x0401, 0x0201, 107 | 0x0040, 0x0004, 0x0041, 0x4601, 0x1c01, 0x0e01, 0x0601, 0x0201, 0x0014, 0x0201, 0x0033, 108 | 0x0042, 0x0401, 0x0201, 0x0024, 0x0050, 0x0201, 0x0043, 0x0034, 0x0401, 0x0201, 0x0051, 109 | 0x0015, 0x0401, 0x0201, 0x0005, 0x0052, 0x0201, 0x0025, 0x0201, 0x0044, 0x0053, 0x0e01, 110 | 0x0801, 0x0401, 0x0201, 0x0060, 0x0006, 0x0201, 0x0061, 0x0016, 0x0401, 0x0201, 0x0080, 111 | 0x0008, 0x0081, 0x1001, 0x0801, 0x0401, 0x0201, 0x0035, 0x0062, 0x0201, 0x0026, 0x0054, 112 | 0x0401, 0x0201, 0x0045, 0x0063, 0x0201, 0x0036, 0x0070, 0x0601, 0x0401, 0x0201, 0x0007, 113 | 0x0055, 0x0071, 0x0201, 0x0017, 0x0201, 0x0027, 0x0037, 0x4801, 0x1801, 0x0c01, 0x0401, 114 | 0x0201, 0x0018, 0x0082, 0x0201, 0x0028, 0x0401, 0x0201, 0x0064, 0x0046, 0x0072, 0x0801, 115 | 0x0401, 0x0201, 0x0084, 0x0048, 0x0201, 0x0090, 0x0009, 0x0201, 0x0091, 0x0019, 0x1801, 116 | 0x0e01, 0x0801, 0x0401, 0x0201, 0x0073, 0x0065, 0x0201, 0x0056, 0x0074, 0x0401, 0x0201, 117 | 0x0047, 0x0066, 0x0083, 0x0601, 0x0201, 0x0038, 0x0201, 0x0075, 0x0057, 0x0201, 0x0092, 118 | 0x0029, 0x0e01, 0x0801, 0x0401, 0x0201, 0x0067, 0x0085, 0x0201, 0x0058, 0x0039, 0x0201, 119 | 0x0093, 0x0201, 0x0049, 0x0086, 0x0601, 0x0201, 0x00a0, 0x0201, 0x0068, 0x000a, 0x0201, 120 | 0x00a1, 0x001a, 0x4401, 0x1801, 0x0c01, 0x0401, 0x0201, 0x00a2, 0x002a, 0x0401, 0x0201, 121 | 0x0095, 0x0059, 0x0201, 0x00a3, 0x003a, 0x0801, 0x0401, 0x0201, 0x004a, 0x0096, 0x0201, 122 | 0x00b0, 0x000b, 0x0201, 0x00b1, 0x001b, 0x1401, 0x0801, 0x0201, 0x00b2, 0x0401, 0x0201, 123 | 0x0076, 0x0077, 0x0094, 0x0601, 0x0401, 0x0201, 0x0087, 0x0078, 0x00a4, 0x0401, 0x0201, 124 | 0x0069, 0x00a5, 0x002b, 0x0c01, 0x0601, 0x0401, 0x0201, 0x005a, 0x0088, 0x00b3, 0x0201, 125 | 0x003b, 0x0201, 0x0079, 0x00a6, 0x0601, 0x0401, 0x0201, 0x006a, 0x00b4, 0x00c0, 0x0401, 126 | 0x0201, 0x000c, 0x0098, 0x00c1, 0x3c01, 0x1601, 0x0a01, 0x0601, 0x0201, 0x001c, 0x0201, 127 | 0x0089, 0x00b5, 0x0201, 0x005b, 0x00c2, 0x0401, 0x0201, 0x002c, 0x003c, 0x0401, 0x0201, 128 | 0x00b6, 0x006b, 0x0201, 0x00c4, 0x004c, 0x1001, 0x0801, 0x0401, 0x0201, 0x00a8, 0x008a, 129 | 0x0201, 0x00d0, 0x000d, 0x0201, 0x00d1, 0x0201, 0x004b, 0x0201, 0x0097, 0x00a7, 0x0c01, 130 | 0x0601, 0x0201, 0x00c3, 0x0201, 0x007a, 0x0099, 0x0401, 0x0201, 0x00c5, 0x005c, 0x00b7, 131 | 0x0401, 0x0201, 0x001d, 0x00d2, 0x0201, 0x002d, 0x0201, 0x007b, 0x00d3, 0x3401, 0x1c01, 132 | 0x0c01, 0x0401, 0x0201, 0x003d, 0x00c6, 0x0401, 0x0201, 0x006c, 0x00a9, 0x0201, 0x009a, 133 | 0x00d4, 0x0801, 0x0401, 0x0201, 0x00b8, 0x008b, 0x0201, 0x004d, 0x00c7, 0x0401, 0x0201, 134 | 0x007c, 0x00d5, 0x0201, 0x005d, 0x00e0, 0x0a01, 0x0401, 0x0201, 0x00e1, 0x001e, 0x0401, 135 | 0x0201, 0x000e, 0x002e, 0x00e2, 0x0801, 0x0401, 0x0201, 0x00e3, 0x006d, 0x0201, 0x008c, 136 | 0x00e4, 0x0401, 0x0201, 0x00e5, 0x00ba, 0x00f0, 0x2601, 0x1001, 0x0401, 0x0201, 0x00f1, 137 | 0x001f, 0x0601, 0x0401, 0x0201, 0x00aa, 0x009b, 0x00b9, 0x0201, 0x003e, 0x0201, 0x00d6, 138 | 0x00c8, 0x0c01, 0x0601, 0x0201, 0x004e, 0x0201, 0x00d7, 0x007d, 0x0201, 0x00ab, 0x0201, 139 | 0x005e, 0x00c9, 0x0601, 0x0201, 0x000f, 0x0201, 0x009c, 0x006e, 0x0201, 0x00f2, 0x002f, 140 | 0x2001, 0x1001, 0x0601, 0x0401, 0x0201, 0x00d8, 0x008d, 0x003f, 0x0601, 0x0201, 0x00f3, 141 | 0x0201, 0x00e6, 0x00ca, 0x0201, 0x00f4, 0x004f, 0x0801, 0x0401, 0x0201, 0x00bb, 0x00ac, 142 | 0x0201, 0x00e7, 0x00f5, 0x0401, 0x0201, 0x00d9, 0x009d, 0x0201, 0x005f, 0x00e8, 0x1e01, 143 | 0x0c01, 0x0601, 0x0201, 0x006f, 0x0201, 0x00f6, 0x00cb, 0x0401, 0x0201, 0x00bc, 0x00ad, 144 | 0x00da, 0x0801, 0x0201, 0x00f7, 0x0401, 0x0201, 0x007e, 0x007f, 0x008e, 0x0601, 0x0401, 145 | 0x0201, 0x009e, 0x00ae, 0x00cc, 0x0201, 0x00f8, 0x008f, 0x1201, 0x0801, 0x0401, 0x0201, 146 | 0x00db, 0x00bd, 0x0201, 0x00ea, 0x00f9, 0x0401, 0x0201, 0x009f, 0x00eb, 0x0201, 0x00be, 147 | 0x0201, 0x00cd, 0x00fa, 0x0e01, 0x0401, 0x0201, 0x00dd, 0x00ec, 0x0601, 0x0401, 0x0201, 148 | 0x00e9, 0x00af, 0x00dc, 0x0201, 0x00ce, 0x00fb, 0x0801, 0x0401, 0x0201, 0x00bf, 0x00de, 149 | 0x0201, 0x00cf, 0x00ee, 0x0401, 0x0201, 0x00df, 0x00ef, 0x0201, 0x00ff, 0x0201, 0x00ed, 150 | 0x0201, 0x00fd, 0x0201, 0x00fc, 0x00fe, 151 | // 15 152 | 0x1001, 0x0601, 0x0201, 0x0000, 0x0201, 0x0010, 0x0001, 0x0201, 0x0011, 0x0401, 0x0201, 153 | 0x0020, 0x0002, 0x0201, 0x0021, 0x0012, 0x3201, 0x1001, 0x0601, 0x0201, 0x0022, 0x0201, 154 | 0x0030, 0x0031, 0x0601, 0x0201, 0x0013, 0x0201, 0x0003, 0x0040, 0x0201, 0x0032, 0x0023, 155 | 0x0e01, 0x0601, 0x0401, 0x0201, 0x0004, 0x0014, 0x0041, 0x0401, 0x0201, 0x0033, 0x0042, 156 | 0x0201, 0x0024, 0x0043, 0x0a01, 0x0601, 0x0201, 0x0034, 0x0201, 0x0050, 0x0005, 0x0201, 157 | 0x0051, 0x0015, 0x0401, 0x0201, 0x0052, 0x0025, 0x0401, 0x0201, 0x0044, 0x0053, 0x0061, 158 | 0x5a01, 0x2401, 0x1201, 0x0a01, 0x0601, 0x0201, 0x0035, 0x0201, 0x0060, 0x0006, 0x0201, 159 | 0x0016, 0x0062, 0x0401, 0x0201, 0x0026, 0x0054, 0x0201, 0x0045, 0x0063, 0x0a01, 0x0601, 160 | 0x0201, 0x0036, 0x0201, 0x0070, 0x0007, 0x0201, 0x0071, 0x0055, 0x0401, 0x0201, 0x0017, 161 | 0x0064, 0x0201, 0x0072, 0x0027, 0x1801, 0x1001, 0x0801, 0x0401, 0x0201, 0x0046, 0x0073, 162 | 0x0201, 0x0037, 0x0065, 0x0401, 0x0201, 0x0056, 0x0080, 0x0201, 0x0008, 0x0074, 0x0401, 163 | 0x0201, 0x0081, 0x0018, 0x0201, 0x0082, 0x0028, 0x1001, 0x0801, 0x0401, 0x0201, 0x0047, 164 | 0x0066, 0x0201, 0x0083, 0x0038, 0x0401, 0x0201, 0x0075, 0x0057, 0x0201, 0x0084, 0x0048, 165 | 0x0601, 0x0401, 0x0201, 0x0090, 0x0019, 0x0091, 0x0401, 0x0201, 0x0092, 0x0076, 0x0201, 166 | 0x0067, 0x0029, 0x5c01, 0x2401, 0x1201, 0x0a01, 0x0401, 0x0201, 0x0085, 0x0058, 0x0401, 167 | 0x0201, 0x0009, 0x0077, 0x0093, 0x0401, 0x0201, 0x0039, 0x0094, 0x0201, 0x0049, 0x0086, 168 | 0x0a01, 0x0601, 0x0201, 0x0068, 0x0201, 0x00a0, 0x000a, 0x0201, 0x00a1, 0x001a, 0x0401, 169 | 0x0201, 0x00a2, 0x002a, 0x0201, 0x0095, 0x0059, 0x1a01, 0x0e01, 0x0601, 0x0201, 0x00a3, 170 | 0x0201, 0x003a, 0x0087, 0x0401, 0x0201, 0x0078, 0x00a4, 0x0201, 0x004a, 0x0096, 0x0601, 171 | 0x0401, 0x0201, 0x0069, 0x00b0, 0x00b1, 0x0401, 0x0201, 0x001b, 0x00a5, 0x00b2, 0x0e01, 172 | 0x0801, 0x0401, 0x0201, 0x005a, 0x002b, 0x0201, 0x0088, 0x0097, 0x0201, 0x00b3, 0x0201, 173 | 0x0079, 0x003b, 0x0801, 0x0401, 0x0201, 0x006a, 0x00b4, 0x0201, 0x004b, 0x00c1, 0x0401, 174 | 0x0201, 0x0098, 0x0089, 0x0201, 0x001c, 0x00b5, 0x5001, 0x2201, 0x1001, 0x0601, 0x0401, 175 | 0x0201, 0x005b, 0x002c, 0x00c2, 0x0601, 0x0401, 0x0201, 0x000b, 0x00c0, 0x00a6, 0x0201, 176 | 0x00a7, 0x007a, 0x0a01, 0x0401, 0x0201, 0x00c3, 0x003c, 0x0401, 0x0201, 0x000c, 0x0099, 177 | 0x00b6, 0x0401, 0x0201, 0x006b, 0x00c4, 0x0201, 0x004c, 0x00a8, 0x1401, 0x0a01, 0x0401, 178 | 0x0201, 0x008a, 0x00c5, 0x0401, 0x0201, 0x00d0, 0x005c, 0x00d1, 0x0401, 0x0201, 0x00b7, 179 | 0x007b, 0x0201, 0x001d, 0x0201, 0x000d, 0x002d, 0x0c01, 0x0401, 0x0201, 0x00d2, 0x00d3, 180 | 0x0401, 0x0201, 0x003d, 0x00c6, 0x0201, 0x006c, 0x00a9, 0x0601, 0x0401, 0x0201, 0x009a, 181 | 0x00b8, 0x00d4, 0x0401, 0x0201, 0x008b, 0x004d, 0x0201, 0x00c7, 0x007c, 0x4401, 0x2201, 182 | 0x1201, 0x0a01, 0x0401, 0x0201, 0x00d5, 0x005d, 0x0401, 0x0201, 0x00e0, 0x000e, 0x00e1, 183 | 0x0401, 0x0201, 0x001e, 0x00e2, 0x0201, 0x00aa, 0x002e, 0x0801, 0x0401, 0x0201, 0x00b9, 184 | 0x009b, 0x0201, 0x00e3, 0x00d6, 0x0401, 0x0201, 0x006d, 0x003e, 0x0201, 0x00c8, 0x008c, 185 | 0x1001, 0x0801, 0x0401, 0x0201, 0x00e4, 0x004e, 0x0201, 0x00d7, 0x007d, 0x0401, 0x0201, 186 | 0x00e5, 0x00ba, 0x0201, 0x00ab, 0x005e, 0x0801, 0x0401, 0x0201, 0x00c9, 0x009c, 0x0201, 187 | 0x00f1, 0x001f, 0x0601, 0x0401, 0x0201, 0x00f0, 0x006e, 0x00f2, 0x0201, 0x002f, 0x00e6, 188 | 0x2601, 0x1201, 0x0801, 0x0401, 0x0201, 0x00d8, 0x00f3, 0x0201, 0x003f, 0x00f4, 0x0601, 189 | 0x0201, 0x004f, 0x0201, 0x008d, 0x00d9, 0x0201, 0x00bb, 0x00ca, 0x0801, 0x0401, 0x0201, 190 | 0x00ac, 0x00e7, 0x0201, 0x007e, 0x00f5, 0x0801, 0x0401, 0x0201, 0x009d, 0x005f, 0x0201, 191 | 0x00e8, 0x008e, 0x0201, 0x00f6, 0x00cb, 0x2201, 0x1201, 0x0a01, 0x0601, 0x0401, 0x0201, 192 | 0x000f, 0x00ae, 0x006f, 0x0201, 0x00bc, 0x00da, 0x0401, 0x0201, 0x00ad, 0x00f7, 0x0201, 193 | 0x007f, 0x00e9, 0x0801, 0x0401, 0x0201, 0x009e, 0x00cc, 0x0201, 0x00f8, 0x008f, 0x0401, 194 | 0x0201, 0x00db, 0x00bd, 0x0201, 0x00ea, 0x00f9, 0x1001, 0x0801, 0x0401, 0x0201, 0x009f, 195 | 0x00dc, 0x0201, 0x00cd, 0x00eb, 0x0401, 0x0201, 0x00be, 0x00fa, 0x0201, 0x00af, 0x00dd, 196 | 0x0e01, 0x0601, 0x0401, 0x0201, 0x00ec, 0x00ce, 0x00fb, 0x0401, 0x0201, 0x00bf, 0x00ed, 197 | 0x0201, 0x00de, 0x00fc, 0x0601, 0x0401, 0x0201, 0x00cf, 0x00fd, 0x00ee, 0x0401, 0x0201, 198 | 0x00df, 0x00fe, 0x0201, 0x00ef, 0x00ff, 199 | // 16 200 | 0x0201, 0x0000, 0x0601, 0x0201, 0x0010, 0x0201, 0x0001, 0x0011, 0x2a01, 0x0801, 0x0401, 201 | 0x0201, 0x0020, 0x0002, 0x0201, 0x0021, 0x0012, 0x0a01, 0x0601, 0x0201, 0x0022, 0x0201, 202 | 0x0030, 0x0003, 0x0201, 0x0031, 0x0013, 0x0a01, 0x0401, 0x0201, 0x0032, 0x0023, 0x0401, 203 | 0x0201, 0x0040, 0x0004, 0x0041, 0x0601, 0x0201, 0x0014, 0x0201, 0x0033, 0x0042, 0x0401, 204 | 0x0201, 0x0024, 0x0050, 0x0201, 0x0043, 0x0034, 0x8a01, 0x2801, 0x1001, 0x0601, 0x0401, 205 | 0x0201, 0x0005, 0x0015, 0x0051, 0x0401, 0x0201, 0x0052, 0x0025, 0x0401, 0x0201, 0x0044, 206 | 0x0035, 0x0053, 0x0a01, 0x0601, 0x0401, 0x0201, 0x0060, 0x0006, 0x0061, 0x0201, 0x0016, 207 | 0x0062, 0x0801, 0x0401, 0x0201, 0x0026, 0x0054, 0x0201, 0x0045, 0x0063, 0x0401, 0x0201, 208 | 0x0036, 0x0070, 0x0071, 0x2801, 0x1201, 0x0801, 0x0201, 0x0017, 0x0201, 0x0007, 0x0201, 209 | 0x0055, 0x0064, 0x0401, 0x0201, 0x0072, 0x0027, 0x0401, 0x0201, 0x0046, 0x0065, 0x0073, 210 | 0x0a01, 0x0601, 0x0201, 0x0037, 0x0201, 0x0056, 0x0008, 0x0201, 0x0080, 0x0081, 0x0601, 211 | 0x0201, 0x0018, 0x0201, 0x0074, 0x0047, 0x0201, 0x0082, 0x0201, 0x0028, 0x0066, 0x1801, 212 | 0x0e01, 0x0801, 0x0401, 0x0201, 0x0083, 0x0038, 0x0201, 0x0075, 0x0084, 0x0401, 0x0201, 213 | 0x0048, 0x0090, 0x0091, 0x0601, 0x0201, 0x0019, 0x0201, 0x0009, 0x0076, 0x0201, 0x0092, 214 | 0x0029, 0x0e01, 0x0801, 0x0401, 0x0201, 0x0085, 0x0058, 0x0201, 0x0093, 0x0039, 0x0401, 215 | 0x0201, 0x00a0, 0x000a, 0x001a, 0x0801, 0x0201, 0x00a2, 0x0201, 0x0067, 0x0201, 0x0057, 216 | 0x0049, 0x0601, 0x0201, 0x0094, 0x0201, 0x0077, 0x0086, 0x0201, 0x00a1, 0x0201, 0x0068, 217 | 0x0095, 0xdc01, 0x7e01, 0x3201, 0x1a01, 0x0c01, 0x0601, 0x0201, 0x002a, 0x0201, 0x0059, 218 | 0x003a, 0x0201, 0x00a3, 0x0201, 0x0087, 0x0078, 0x0801, 0x0401, 0x0201, 0x00a4, 0x004a, 219 | 0x0201, 0x0096, 0x0069, 0x0401, 0x0201, 0x00b0, 0x000b, 0x00b1, 0x0a01, 0x0401, 0x0201, 220 | 0x001b, 0x00b2, 0x0201, 0x002b, 0x0201, 0x00a5, 0x005a, 0x0601, 0x0201, 0x00b3, 0x0201, 221 | 0x00a6, 0x006a, 0x0401, 0x0201, 0x00b4, 0x004b, 0x0201, 0x000c, 0x00c1, 0x1e01, 0x0e01, 222 | 0x0601, 0x0401, 0x0201, 0x00b5, 0x00c2, 0x002c, 0x0401, 0x0201, 0x00a7, 0x00c3, 0x0201, 223 | 0x006b, 0x00c4, 0x0801, 0x0201, 0x001d, 0x0401, 0x0201, 0x0088, 0x0097, 0x003b, 0x0401, 224 | 0x0201, 0x00d1, 0x00d2, 0x0201, 0x002d, 0x00d3, 0x1201, 0x0601, 0x0401, 0x0201, 0x001e, 225 | 0x002e, 0x00e2, 0x0601, 0x0401, 0x0201, 0x0079, 0x0098, 0x00c0, 0x0201, 0x001c, 0x0201, 226 | 0x0089, 0x005b, 0x0e01, 0x0601, 0x0201, 0x003c, 0x0201, 0x007a, 0x00b6, 0x0401, 0x0201, 227 | 0x004c, 0x0099, 0x0201, 0x00a8, 0x008a, 0x0601, 0x0201, 0x000d, 0x0201, 0x00c5, 0x005c, 228 | 0x0401, 0x0201, 0x003d, 0x00c6, 0x0201, 0x006c, 0x009a, 0x5801, 0x5601, 0x2401, 0x1001, 229 | 0x0801, 0x0401, 0x0201, 0x008b, 0x004d, 0x0201, 0x00c7, 0x007c, 0x0401, 0x0201, 0x00d5, 230 | 0x005d, 0x0201, 0x00e0, 0x000e, 0x0801, 0x0201, 0x00e3, 0x0401, 0x0201, 0x00d0, 0x00b7, 231 | 0x007b, 0x0601, 0x0401, 0x0201, 0x00a9, 0x00b8, 0x00d4, 0x0201, 0x00e1, 0x0201, 0x00aa, 232 | 0x00b9, 0x1801, 0x0a01, 0x0601, 0x0401, 0x0201, 0x009b, 0x00d6, 0x006d, 0x0201, 0x003e, 233 | 0x00c8, 0x0601, 0x0401, 0x0201, 0x008c, 0x00e4, 0x004e, 0x0401, 0x0201, 0x00d7, 0x00e5, 234 | 0x0201, 0x00ba, 0x00ab, 0x0c01, 0x0401, 0x0201, 0x009c, 0x00e6, 0x0401, 0x0201, 0x006e, 235 | 0x00d8, 0x0201, 0x008d, 0x00bb, 0x0801, 0x0401, 0x0201, 0x00e7, 0x009d, 0x0201, 0x00e8, 236 | 0x008e, 0x0401, 0x0201, 0x00cb, 0x00bc, 0x009e, 0x00f1, 0x0201, 0x001f, 0x0201, 0x000f, 237 | 0x002f, 0x4201, 0x3801, 0x0201, 0x00f2, 0x3401, 0x3201, 0x1401, 0x0801, 0x0201, 0x00bd, 238 | 0x0201, 0x005e, 0x0201, 0x007d, 0x00c9, 0x0601, 0x0201, 0x00ca, 0x0201, 0x00ac, 0x007e, 239 | 0x0401, 0x0201, 0x00da, 0x00ad, 0x00cc, 0x0a01, 0x0601, 0x0201, 0x00ae, 0x0201, 0x00db, 240 | 0x00dc, 0x0201, 0x00cd, 0x00be, 0x0601, 0x0401, 0x0201, 0x00eb, 0x00ed, 0x00ee, 0x0601, 241 | 0x0401, 0x0201, 0x00d9, 0x00ea, 0x00e9, 0x0201, 0x00de, 0x0401, 0x0201, 0x00dd, 0x00ec, 242 | 0x00ce, 0x003f, 0x00f0, 0x0401, 0x0201, 0x00f3, 0x00f4, 0x0201, 0x004f, 0x0201, 0x00f5, 243 | 0x005f, 0x0a01, 0x0201, 0x00ff, 0x0401, 0x0201, 0x00f6, 0x006f, 0x0201, 0x00f7, 0x007f, 244 | 0x0c01, 0x0601, 0x0201, 0x008f, 0x0201, 0x00f8, 0x00f9, 0x0401, 0x0201, 0x009f, 0x00fa, 245 | 0x00af, 0x0801, 0x0401, 0x0201, 0x00fb, 0x00bf, 0x0201, 0x00fc, 0x00cf, 0x0401, 0x0201, 246 | 0x00fd, 0x00df, 0x0201, 0x00fe, 0x00ef, 247 | // 24 248 | 0x3c01, 0x0801, 0x0401, 0x0201, 0x0000, 0x0010, 0x0201, 0x0001, 0x0011, 0x0e01, 0x0601, 249 | 0x0401, 0x0201, 0x0020, 0x0002, 0x0021, 0x0201, 0x0012, 0x0201, 0x0022, 0x0201, 0x0030, 250 | 0x0003, 0x0e01, 0x0401, 0x0201, 0x0031, 0x0013, 0x0401, 0x0201, 0x0032, 0x0023, 0x0401, 251 | 0x0201, 0x0040, 0x0004, 0x0041, 0x0801, 0x0401, 0x0201, 0x0014, 0x0033, 0x0201, 0x0042, 252 | 0x0024, 0x0601, 0x0401, 0x0201, 0x0043, 0x0034, 0x0051, 0x0601, 0x0401, 0x0201, 0x0050, 253 | 0x0005, 0x0015, 0x0201, 0x0052, 0x0025, 0xfa01, 0x6201, 0x2201, 0x1201, 0x0a01, 0x0401, 254 | 0x0201, 0x0044, 0x0053, 0x0201, 0x0035, 0x0201, 0x0060, 0x0006, 0x0401, 0x0201, 0x0061, 255 | 0x0016, 0x0201, 0x0062, 0x0026, 0x0801, 0x0401, 0x0201, 0x0054, 0x0045, 0x0201, 0x0063, 256 | 0x0036, 0x0401, 0x0201, 0x0071, 0x0055, 0x0201, 0x0064, 0x0046, 0x2001, 0x0e01, 0x0601, 257 | 0x0201, 0x0072, 0x0201, 0x0027, 0x0037, 0x0201, 0x0073, 0x0401, 0x0201, 0x0070, 0x0007, 258 | 0x0017, 0x0a01, 0x0401, 0x0201, 0x0065, 0x0056, 0x0401, 0x0201, 0x0080, 0x0008, 0x0081, 259 | 0x0401, 0x0201, 0x0074, 0x0047, 0x0201, 0x0018, 0x0082, 0x1001, 0x0801, 0x0401, 0x0201, 260 | 0x0028, 0x0066, 0x0201, 0x0083, 0x0038, 0x0401, 0x0201, 0x0075, 0x0057, 0x0201, 0x0084, 261 | 0x0048, 0x0801, 0x0401, 0x0201, 0x0091, 0x0019, 0x0201, 0x0092, 0x0076, 0x0401, 0x0201, 262 | 0x0067, 0x0029, 0x0201, 0x0085, 0x0058, 0x5c01, 0x2201, 0x1001, 0x0801, 0x0401, 0x0201, 263 | 0x0093, 0x0039, 0x0201, 0x0094, 0x0049, 0x0401, 0x0201, 0x0077, 0x0086, 0x0201, 0x0068, 264 | 0x00a1, 0x0801, 0x0401, 0x0201, 0x00a2, 0x002a, 0x0201, 0x0095, 0x0059, 0x0401, 0x0201, 265 | 0x00a3, 0x003a, 0x0201, 0x0087, 0x0201, 0x0078, 0x004a, 0x1601, 0x0c01, 0x0401, 0x0201, 266 | 0x00a4, 0x0096, 0x0401, 0x0201, 0x0069, 0x00b1, 0x0201, 0x001b, 0x00a5, 0x0601, 0x0201, 267 | 0x00b2, 0x0201, 0x005a, 0x002b, 0x0201, 0x0088, 0x00b3, 0x1001, 0x0a01, 0x0601, 0x0201, 268 | 0x0090, 0x0201, 0x0009, 0x00a0, 0x0201, 0x0097, 0x0079, 0x0401, 0x0201, 0x00a6, 0x006a, 269 | 0x00b4, 0x0c01, 0x0601, 0x0201, 0x001a, 0x0201, 0x000a, 0x00b0, 0x0201, 0x003b, 0x0201, 270 | 0x000b, 0x00c0, 0x0401, 0x0201, 0x004b, 0x00c1, 0x0201, 0x0098, 0x0089, 0x4301, 0x2201, 271 | 0x1001, 0x0801, 0x0401, 0x0201, 0x001c, 0x00b5, 0x0201, 0x005b, 0x00c2, 0x0401, 0x0201, 272 | 0x002c, 0x00a7, 0x0201, 0x007a, 0x00c3, 0x0a01, 0x0601, 0x0201, 0x003c, 0x0201, 0x000c, 273 | 0x00d0, 0x0201, 0x00b6, 0x006b, 0x0401, 0x0201, 0x00c4, 0x004c, 0x0201, 0x0099, 0x00a8, 274 | 0x1001, 0x0801, 0x0401, 0x0201, 0x008a, 0x00c5, 0x0201, 0x005c, 0x00d1, 0x0401, 0x0201, 275 | 0x00b7, 0x007b, 0x0201, 0x001d, 0x00d2, 0x0901, 0x0401, 0x0201, 0x002d, 0x00d3, 0x0201, 276 | 0x003d, 0x00c6, 0x55fa, 0x0401, 0x0201, 0x006c, 0x00a9, 0x0201, 0x009a, 0x00d4, 0x2001, 277 | 0x1001, 0x0801, 0x0401, 0x0201, 0x00b8, 0x008b, 0x0201, 0x004d, 0x00c7, 0x0401, 0x0201, 278 | 0x007c, 0x00d5, 0x0201, 0x005d, 0x00e1, 0x0801, 0x0401, 0x0201, 0x001e, 0x00e2, 0x0201, 279 | 0x00aa, 0x00b9, 0x0401, 0x0201, 0x009b, 0x00e3, 0x0201, 0x00d6, 0x006d, 0x1401, 0x0a01, 280 | 0x0601, 0x0201, 0x003e, 0x0201, 0x002e, 0x004e, 0x0201, 0x00c8, 0x008c, 0x0401, 0x0201, 281 | 0x00e4, 0x00d7, 0x0401, 0x0201, 0x007d, 0x00ab, 0x00e5, 0x0a01, 0x0401, 0x0201, 0x00ba, 282 | 0x005e, 0x0201, 0x00c9, 0x0201, 0x009c, 0x006e, 0x0801, 0x0201, 0x00e6, 0x0201, 0x000d, 283 | 0x0201, 0x00e0, 0x000e, 0x0401, 0x0201, 0x00d8, 0x008d, 0x0201, 0x00bb, 0x00ca, 0x4a01, 284 | 0x0201, 0x00ff, 0x4001, 0x3a01, 0x2001, 0x1001, 0x0801, 0x0401, 0x0201, 0x00ac, 0x00e7, 285 | 0x0201, 0x007e, 0x00d9, 0x0401, 0x0201, 0x009d, 0x00e8, 0x0201, 0x008e, 0x00cb, 0x0801, 286 | 0x0401, 0x0201, 0x00bc, 0x00da, 0x0201, 0x00ad, 0x00e9, 0x0401, 0x0201, 0x009e, 0x00cc, 287 | 0x0201, 0x00db, 0x00bd, 0x1001, 0x0801, 0x0401, 0x0201, 0x00ea, 0x00ae, 0x0201, 0x00dc, 288 | 0x00cd, 0x0401, 0x0201, 0x00eb, 0x00be, 0x0201, 0x00dd, 0x00ec, 0x0801, 0x0401, 0x0201, 289 | 0x00ce, 0x00ed, 0x0201, 0x00de, 0x00ee, 0x000f, 0x0401, 0x0201, 0x00f0, 0x001f, 0x00f1, 290 | 0x0401, 0x0201, 0x00f2, 0x002f, 0x0201, 0x00f3, 0x003f, 0x1201, 0x0801, 0x0401, 0x0201, 291 | 0x00f4, 0x004f, 0x0201, 0x00f5, 0x005f, 0x0401, 0x0201, 0x00f6, 0x006f, 0x0201, 0x00f7, 292 | 0x0201, 0x007f, 0x008f, 0x0a01, 0x0401, 0x0201, 0x00f8, 0x00f9, 0x0401, 0x0201, 0x009f, 293 | 0x00af, 0x00fa, 0x0801, 0x0401, 0x0201, 0x00fb, 0x00bf, 0x0201, 0x00fc, 0x00cf, 0x0401, 294 | 0x0201, 0x00fd, 0x00df, 0x0201, 0x00fe, 0x00ef, 295 | // 32 296 | 0x0201, 0x0000, 0x0801, 0x0401, 0x0201, 0x0008, 0x0004, 0x0201, 0x0001, 0x0002, 0x0801, 297 | 0x0401, 0x0201, 0x000c, 0x000a, 0x0201, 0x0003, 0x0006, 0x0601, 0x0201, 0x0009, 0x0201, 298 | 0x0005, 0x0007, 0x0401, 0x0201, 0x000e, 0x000d, 0x0201, 0x000f, 0x000b, 299 | // 33 300 | 0x1001, 0x0801, 0x0401, 0x0201, 0x0000, 0x0001, 0x0201, 0x0002, 0x0003, 0x0401, 0x0201, 301 | 0x0004, 0x0005, 0x0201, 0x0006, 0x0007, 0x0801, 0x0401, 0x0201, 0x0008, 0x0009, 0x0201, 302 | 0x000a, 0x000b, 0x0401, 0x0201, 0x000c, 0x000d, 0x0201, 0x000e, 0x000f, 303 | } 304 | 305 | type huffTables struct { 306 | hufftable []uint16 307 | treelen int 308 | linbits int 309 | } 310 | 311 | var huffmanMain = [...]huffTables{ 312 | {nil, 0, 0}, // Table 0 313 | {huffmanTable, 7, 0}, // Table 1 314 | {huffmanTable[7:], 17, 0}, // Table 2 315 | {huffmanTable[24:], 17, 0}, // Table 3 316 | {nil, 0, 0}, // Table 4 317 | {huffmanTable[41:], 31, 0}, // Table 5 318 | {huffmanTable[72:], 31, 0}, // Table 6 319 | {huffmanTable[103:], 71, 0}, // Table 7 320 | {huffmanTable[174:], 71, 0}, // Table 8 321 | {huffmanTable[245:], 71, 0}, // Table 9 322 | {huffmanTable[316:], 127, 0}, // Table 10 323 | {huffmanTable[443:], 127, 0}, // Table 11 324 | {huffmanTable[570:], 127, 0}, // Table 12 325 | {huffmanTable[697:], 511, 0}, // Table 13 326 | {nil, 0, 0}, // Table 14 327 | {huffmanTable[1208:], 511, 0}, // Table 15 328 | {huffmanTable[1719:], 511, 1}, // Table 16 329 | {huffmanTable[1719:], 511, 2}, // Table 17 330 | {huffmanTable[1719:], 511, 3}, // Table 18 331 | {huffmanTable[1719:], 511, 4}, // Table 19 332 | {huffmanTable[1719:], 511, 6}, // Table 20 333 | {huffmanTable[1719:], 511, 8}, // Table 21 334 | {huffmanTable[1719:], 511, 10}, // Table 22 335 | {huffmanTable[1719:], 511, 13}, // Table 23 336 | {huffmanTable[2230:], 512, 4}, // Table 24 337 | {huffmanTable[2230:], 512, 5}, // Table 25 338 | {huffmanTable[2230:], 512, 6}, // Table 26 339 | {huffmanTable[2230:], 512, 7}, // Table 27 340 | {huffmanTable[2230:], 512, 8}, // Table 28 341 | {huffmanTable[2230:], 512, 9}, // Table 29 342 | {huffmanTable[2230:], 512, 11}, // Table 30 343 | {huffmanTable[2230:], 512, 13}, // Table 31 344 | {huffmanTable[2742:], 31, 0}, // Table 32 345 | {huffmanTable[2773:], 31, 0}, // Table 33 346 | } 347 | 348 | func Decode(m *bits.Bits, table_num int) (x, y, v, w int, err error) { 349 | point := 0 350 | error := 1 351 | bitsleft := 32 352 | treelen := huffmanMain[table_num].treelen 353 | linbits := huffmanMain[table_num].linbits 354 | if treelen == 0 { // Check for empty tables 355 | return 0, 0, 0, 0, nil 356 | } 357 | htptr := huffmanMain[table_num].hufftable 358 | for { // Start reading the Huffman code word,bit by bit 359 | // Check if we've matched a code word 360 | if (htptr[point] & 0xff00) == 0 { 361 | error = 0 362 | x = int((htptr[point] >> 4) & 0xf) 363 | y = int(htptr[point] & 0xf) 364 | break 365 | } 366 | if m.Bit() != 0 { // Go right in tree 367 | for (htptr[point] & 0xff) >= 250 { 368 | point += int(htptr[point]) & 0xff 369 | } 370 | point += int(htptr[point]) & 0xff 371 | } else { // Go left in tree 372 | for (htptr[point] >> 8) >= 250 { 373 | point += int(htptr[point]) >> 8 374 | } 375 | point += int(htptr[point]) >> 8 376 | } 377 | bitsleft-- 378 | if bitsleft <= 0 || point >= treelen { 379 | break 380 | } 381 | } 382 | if error != 0 { // Check for error. 383 | err := fmt.Errorf("mp3: illegal Huff code in data. bleft = %d, point = %d. tab = %d.", 384 | bitsleft, point, table_num) 385 | return 0, 0, 0, 0, err 386 | } 387 | if table_num > 31 { // Process sign encodings for quadruples tables. 388 | v = (y >> 3) & 1 389 | w = (y >> 2) & 1 390 | x = (y >> 1) & 1 391 | y = y & 1 392 | if (v != 0) && (m.Bit() == 1) { 393 | v = -v 394 | } 395 | if (w != 0) && (m.Bit() == 1) { 396 | w = -w 397 | } 398 | if (x != 0) && (m.Bit() == 1) { 399 | x = -x 400 | } 401 | if (y != 0) && (m.Bit() == 1) { 402 | y = -y 403 | } 404 | } else { 405 | if (linbits != 0) && (x == 15) { 406 | x += m.Bits(linbits) // Get linbits 407 | } 408 | if (x != 0) && (m.Bit() == 1) { 409 | x = -x // Get sign bit 410 | } 411 | if (linbits != 0) && (y == 15) { 412 | y += m.Bits(linbits) // Get linbits 413 | } 414 | if (y != 0) && (m.Bit() == 1) { 415 | y = -y // Get sign bit 416 | } 417 | } 418 | return x, y, v, w, nil 419 | } 420 | -------------------------------------------------------------------------------- /internal/imdct/imdct.go: -------------------------------------------------------------------------------- 1 | // Copyright 2017 Hajime Hoshi 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package imdct 16 | 17 | import ( 18 | "math" 19 | ) 20 | 21 | var imdctWinData = [4][36]float32{} 22 | 23 | func init() { 24 | for i := 0; i < 36; i++ { 25 | imdctWinData[0][i] = float32(math.Sin(math.Pi / 36 * (float64(i) + 0.5))) 26 | } 27 | for i := 0; i < 18; i++ { 28 | imdctWinData[1][i] = float32(math.Sin(math.Pi / 36 * (float64(i) + 0.5))) 29 | } 30 | for i := 18; i < 24; i++ { 31 | imdctWinData[1][i] = 1.0 32 | } 33 | for i := 24; i < 30; i++ { 34 | imdctWinData[1][i] = float32(math.Sin(math.Pi / 12 * (float64(i) + 0.5 - 18.0))) 35 | } 36 | for i := 30; i < 36; i++ { 37 | imdctWinData[1][i] = 0.0 38 | } 39 | for i := 0; i < 12; i++ { 40 | imdctWinData[2][i] = float32(math.Sin(math.Pi / 12 * (float64(i) + 0.5))) 41 | } 42 | for i := 12; i < 36; i++ { 43 | imdctWinData[2][i] = 0.0 44 | } 45 | for i := 0; i < 6; i++ { 46 | imdctWinData[3][i] = 0.0 47 | } 48 | for i := 6; i < 12; i++ { 49 | imdctWinData[3][i] = float32(math.Sin(math.Pi / 12 * (float64(i) + 0.5 - 6.0))) 50 | } 51 | for i := 12; i < 18; i++ { 52 | imdctWinData[3][i] = 1.0 53 | } 54 | for i := 18; i < 36; i++ { 55 | imdctWinData[3][i] = float32(math.Sin(math.Pi / 36 * (float64(i) + 0.5))) 56 | } 57 | } 58 | 59 | var cosN12 = [6][12]float32{} 60 | 61 | func init() { 62 | const N = 12 63 | for i := 0; i < 6; i++ { 64 | for j := 0; j < 12; j++ { 65 | cosN12[i][j] = float32(math.Cos(math.Pi / (2 * N) * (2*float64(j) + 1 + N/2) * (2*float64(i) + 1))) 66 | } 67 | } 68 | } 69 | 70 | var cosN36 = [18][36]float32{} 71 | 72 | func init() { 73 | const N = 36 74 | for i := 0; i < 18; i++ { 75 | for j := 0; j < 36; j++ { 76 | cosN36[i][j] = float32(math.Cos(math.Pi / (2 * N) * (2*float64(j) + 1 + N/2) * (2*float64(i) + 1))) 77 | } 78 | } 79 | } 80 | 81 | func Win(in []float32, blockType int) []float32 { 82 | out := make([]float32, 36) 83 | if blockType == 2 { 84 | iwd := imdctWinData[blockType] 85 | const N = 12 86 | for i := 0; i < 3; i++ { 87 | for p := 0; p < N; p++ { 88 | sum := float32(0.0) 89 | for m := 0; m < N/2; m++ { 90 | sum += in[i+3*m] * cosN12[m][p] 91 | } 92 | out[6*i+p+6] += sum * iwd[p] 93 | } 94 | } 95 | return out 96 | } 97 | const N = 36 98 | iwd := imdctWinData[blockType] 99 | for p := 0; p < N; p++ { 100 | sum := float32(0.0) 101 | for m := 0; m < N/2; m++ { 102 | sum += in[m] * cosN36[m][p] 103 | } 104 | out[p] = sum * iwd[p] 105 | } 106 | return out 107 | } 108 | -------------------------------------------------------------------------------- /internal/maindata/huffman.go: -------------------------------------------------------------------------------- 1 | // Copyright 2017 Hajime Hoshi 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package maindata 16 | 17 | import ( 18 | "fmt" 19 | 20 | "github.com/hajimehoshi/go-mp3/internal/bits" 21 | "github.com/hajimehoshi/go-mp3/internal/consts" 22 | "github.com/hajimehoshi/go-mp3/internal/frameheader" 23 | "github.com/hajimehoshi/go-mp3/internal/huffman" 24 | "github.com/hajimehoshi/go-mp3/internal/sideinfo" 25 | ) 26 | 27 | func readHuffman(m *bits.Bits, header frameheader.FrameHeader, sideInfo *sideinfo.SideInfo, mainData *MainData, part_2_start, gr, ch int) error { 28 | // Check that there is any data to decode. If not, zero the array. 29 | if sideInfo.Part2_3Length[gr][ch] == 0 { 30 | for i := 0; i < consts.SamplesPerGr; i++ { 31 | mainData.Is[gr][ch][i] = 0.0 32 | } 33 | return nil 34 | } 35 | 36 | // Calculate bit_pos_end which is the index of the last bit for this part. 37 | bit_pos_end := part_2_start + sideInfo.Part2_3Length[gr][ch] - 1 38 | // Determine region boundaries 39 | region_1_start := 0 40 | region_2_start := 0 41 | if (sideInfo.WinSwitchFlag[gr][ch] == 1) && (sideInfo.BlockType[gr][ch] == 2) { 42 | region_1_start = 36 // sfb[9/3]*3=36 43 | region_2_start = consts.SamplesPerGr // No Region2 for short block case. 44 | } else { 45 | sfreq := header.SamplingFrequency() 46 | lsf := header.LowSamplingFrequency() 47 | l := consts.SfBandIndices[lsf][sfreq][consts.SfBandIndicesLong] 48 | i := sideInfo.Region0Count[gr][ch] + 1 49 | if i < 0 || len(l) <= i { 50 | // TODO: Better error messages (#3) 51 | return fmt.Errorf("mp3: readHuffman failed: invalid index i: %d", i) 52 | } 53 | region_1_start = l[i] 54 | j := sideInfo.Region0Count[gr][ch] + sideInfo.Region1Count[gr][ch] + 2 55 | if j < 0 || len(l) <= j { 56 | // TODO: Better error messages (#3) 57 | return fmt.Errorf("mp3: readHuffman failed: invalid index j: %d", j) 58 | } 59 | region_2_start = l[j] 60 | } 61 | // Read big_values using tables according to region_x_start 62 | for is_pos := 0; is_pos < sideInfo.BigValues[gr][ch]*2; is_pos++ { 63 | // #22 64 | if is_pos >= len(mainData.Is[gr][ch]) { 65 | return fmt.Errorf("mp3: is_pos was too big: %d", is_pos) 66 | } 67 | table_num := 0 68 | if is_pos < region_1_start { 69 | table_num = sideInfo.TableSelect[gr][ch][0] 70 | } else if is_pos < region_2_start { 71 | table_num = sideInfo.TableSelect[gr][ch][1] 72 | } else { 73 | table_num = sideInfo.TableSelect[gr][ch][2] 74 | } 75 | // Get next Huffman coded words 76 | x, y, _, _, err := huffman.Decode(m, table_num) 77 | if err != nil { 78 | return err 79 | } 80 | // In the big_values area there are two freq lines per Huffman word 81 | mainData.Is[gr][ch][is_pos] = float32(x) 82 | is_pos++ 83 | mainData.Is[gr][ch][is_pos] = float32(y) 84 | } 85 | // Read small values until is_pos = 576 or we run out of huffman data 86 | // TODO: Is this comment wrong? 87 | table_num := sideInfo.Count1TableSelect[gr][ch] + 32 88 | is_pos := sideInfo.BigValues[gr][ch] * 2 89 | for is_pos <= 572 && m.BitPos() <= bit_pos_end { 90 | // Get next Huffman coded words 91 | x, y, v, w, err := huffman.Decode(m, table_num) 92 | if err != nil { 93 | return err 94 | } 95 | mainData.Is[gr][ch][is_pos] = float32(v) 96 | is_pos++ 97 | if is_pos >= consts.SamplesPerGr { 98 | break 99 | } 100 | mainData.Is[gr][ch][is_pos] = float32(w) 101 | is_pos++ 102 | if is_pos >= consts.SamplesPerGr { 103 | break 104 | } 105 | mainData.Is[gr][ch][is_pos] = float32(x) 106 | is_pos++ 107 | if is_pos >= consts.SamplesPerGr { 108 | break 109 | } 110 | mainData.Is[gr][ch][is_pos] = float32(y) 111 | is_pos++ 112 | } 113 | // Check that we didn't read past the end of this section 114 | if m.BitPos() > (bit_pos_end + 1) { 115 | // Remove last words read 116 | is_pos -= 4 117 | } 118 | if is_pos < 0 { 119 | is_pos = 0 120 | } 121 | 122 | // Setup count1 which is the index of the first sample in the rzero reg. 123 | sideInfo.Count1[gr][ch] = is_pos 124 | 125 | // Zero out the last part if necessary 126 | for is_pos < consts.SamplesPerGr { 127 | mainData.Is[gr][ch][is_pos] = 0.0 128 | is_pos++ 129 | } 130 | // Set the bitpos to point to the next part to read 131 | m.SetPos(bit_pos_end + 1) 132 | return nil 133 | } 134 | -------------------------------------------------------------------------------- /internal/maindata/maindata.go: -------------------------------------------------------------------------------- 1 | // Copyright 2017 Hajime Hoshi 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package maindata 16 | 17 | import ( 18 | "fmt" 19 | "io" 20 | 21 | "github.com/hajimehoshi/go-mp3/internal/bits" 22 | "github.com/hajimehoshi/go-mp3/internal/consts" 23 | "github.com/hajimehoshi/go-mp3/internal/frameheader" 24 | "github.com/hajimehoshi/go-mp3/internal/sideinfo" 25 | ) 26 | 27 | type FullReader interface { 28 | ReadFull([]byte) (int, error) 29 | } 30 | 31 | // A MainData is MPEG1 Layer 3 Main Data. 32 | type MainData struct { 33 | ScalefacL [2][2][22]int // 0-4 bits 34 | ScalefacS [2][2][13][3]int // 0-4 bits 35 | Is [2][2][576]float32 // Huffman coded freq. lines 36 | } 37 | 38 | var scalefacSizesMpeg1 = [16][2]int{ 39 | {0, 0}, {0, 1}, {0, 2}, {0, 3}, {3, 0}, {1, 1}, {1, 2}, {1, 3}, 40 | {2, 1}, {2, 2}, {2, 3}, {3, 1}, {3, 2}, {3, 3}, {4, 2}, {4, 3}, 41 | } 42 | 43 | var scalefacSizesMpeg2 = [3][6][4]int{ 44 | {{6, 5, 5, 5}, {6, 5, 7, 3}, {11, 10, 0, 0}, 45 | {7, 7, 7, 0}, {6, 6, 6, 3}, {8, 8, 5, 0}}, 46 | {{9, 9, 9, 9}, {9, 9, 12, 6}, {18, 18, 0, 0}, 47 | {12, 12, 12, 0}, {12, 9, 9, 6}, {15, 12, 9, 0}}, 48 | {{6, 9, 9, 9}, {6, 9, 12, 6}, {15, 18, 0, 0}, 49 | {6, 15, 12, 0}, {6, 12, 9, 6}, {6, 18, 9, 0}}} 50 | 51 | var nSlen2 = initSlen() /* MPEG 2.0 slen for 'normal' mode */ 52 | 53 | func initSlen() (nSlen2 [512]int) { 54 | for i := 0; i < 4; i++ { 55 | for j := 0; j < 3; j++ { 56 | n := j + i*3 57 | nSlen2[n+500] = i | (j << 3) | (2 << 12) | (1 << 15) 58 | } 59 | } 60 | 61 | for i := 0; i < 5; i++ { 62 | for j := 0; j < 5; j++ { 63 | for k := 0; k < 4; k++ { 64 | for l := 0; l < 4; l++ { 65 | n := l + k*4 + j*16 + i*80 66 | nSlen2[n] = i | (j << 3) | (k << 6) | (l << 9) | (0 << 12) 67 | } 68 | } 69 | } 70 | } 71 | for i := 0; i < 5; i++ { 72 | for j := 0; j < 5; j++ { 73 | for k := 0; k < 4; k++ { 74 | n := k + j*4 + i*20 75 | nSlen2[n+400] = i | (j << 3) | (k << 6) | (1 << 12) 76 | } 77 | } 78 | } 79 | return 80 | } 81 | 82 | func Read(source FullReader, prev *bits.Bits, header frameheader.FrameHeader, sideInfo *sideinfo.SideInfo) (*MainData, *bits.Bits, error) { 83 | nch := header.NumberOfChannels() 84 | // Calculate header audio data size 85 | framesize, err := header.FrameSize() 86 | if err != nil { 87 | return nil, nil, err 88 | } 89 | if framesize > 2000 { 90 | return nil, nil, fmt.Errorf("mp3: framesize = %d", framesize) 91 | } 92 | sideinfo_size := header.SideInfoSize() 93 | 94 | // Main data size is the rest of the frame,including ancillary data 95 | main_data_size := framesize - sideinfo_size - 4 // sync+header 96 | // CRC is 2 bytes 97 | if header.ProtectionBit() == 0 { 98 | main_data_size -= 2 99 | } 100 | // Assemble main data buffer with data from this frame and the previous 101 | // two frames. main_data_begin indicates how many bytes from previous 102 | // frames that should be used. This buffer is later accessed by the 103 | // Bits function in the same way as the side info is. 104 | m, err := read(source, prev, main_data_size, sideInfo.MainDataBegin) 105 | if err != nil { 106 | // This could be due to not enough data in reservoir 107 | return nil, nil, err 108 | } 109 | 110 | if header.LowSamplingFrequency() == 1 { 111 | return getScaleFactorsMpeg2(m, header, sideInfo) 112 | } 113 | return getScaleFactorsMpeg1(nch, m, header, sideInfo) 114 | } 115 | 116 | func getScaleFactorsMpeg2(m *bits.Bits, header frameheader.FrameHeader, sideInfo *sideinfo.SideInfo) (*MainData, *bits.Bits, error) { 117 | 118 | nch := header.NumberOfChannels() 119 | 120 | md := &MainData{} 121 | 122 | for ch := 0; ch < nch; ch++ { 123 | part_2_start := m.BitPos() 124 | numbits := 0 125 | slen := nSlen2[sideInfo.ScalefacCompress[0][ch]] 126 | sideInfo.Preflag[0][ch] = (slen >> 15) & 0x1 127 | 128 | n := 0 129 | if sideInfo.BlockType[0][ch] == 2 { 130 | n++ 131 | if sideInfo.MixedBlockFlag[0][ch] != 0 { 132 | n++ 133 | } 134 | } 135 | 136 | var scaleFactors []int 137 | d := (slen >> 12) & 0x7 138 | 139 | for i := 0; i < 4; i++ { 140 | num := slen & 0x7 141 | slen >>= 3 142 | if num > 0 { 143 | for j := 0; j < scalefacSizesMpeg2[n][d][i]; j++ { 144 | scaleFactors = append(scaleFactors, m.Bits(num)) 145 | } 146 | numbits += scalefacSizesMpeg2[n][d][i] * num 147 | } else { 148 | for j := 0; j < scalefacSizesMpeg2[n][d][i]; j++ { 149 | scaleFactors = append(scaleFactors, 0) 150 | } 151 | } 152 | } 153 | 154 | n = (n << 1) + 1 155 | for i := 0; i < n; i++ { 156 | scaleFactors = append(scaleFactors, 0) 157 | } 158 | 159 | if len(scaleFactors) == 22 { 160 | for i := 0; i < 22; i++ { 161 | md.ScalefacL[0][ch][i] = scaleFactors[i] 162 | } 163 | } else { 164 | for x := 0; x < 13; x++ { 165 | for i := 0; i < 3; i++ { 166 | md.ScalefacS[0][ch][x][i] = scaleFactors[(x*3)+i] 167 | } 168 | } 169 | } 170 | 171 | // Read Huffman coded data. Skip stuffing bits. 172 | if err := readHuffman(m, header, sideInfo, md, part_2_start, 0, ch); err != nil { 173 | return nil, nil, err 174 | } 175 | } 176 | // The ancillary data is stored here,but we ignore it. 177 | return md, m, nil 178 | } 179 | 180 | func getScaleFactorsMpeg1(nch int, m *bits.Bits, header frameheader.FrameHeader, sideInfo *sideinfo.SideInfo) (*MainData, *bits.Bits, error) { 181 | md := &MainData{} 182 | for gr := 0; gr < 2; gr++ { 183 | for ch := 0; ch < nch; ch++ { 184 | part_2_start := m.BitPos() 185 | // Number of bits in the bitstream for the bands 186 | slen1 := scalefacSizesMpeg1[sideInfo.ScalefacCompress[gr][ch]][0] 187 | slen2 := scalefacSizesMpeg1[sideInfo.ScalefacCompress[gr][ch]][1] 188 | if sideInfo.WinSwitchFlag[gr][ch] == 1 && sideInfo.BlockType[gr][ch] == 2 { 189 | if sideInfo.MixedBlockFlag[gr][ch] != 0 { 190 | for sfb := 0; sfb < 8; sfb++ { 191 | md.ScalefacL[gr][ch][sfb] = m.Bits(slen1) 192 | } 193 | for sfb := 3; sfb < 12; sfb++ { 194 | //slen1 for band 3-5,slen2 for 6-11 195 | nbits := slen2 196 | if sfb < 6 { 197 | nbits = slen1 198 | } 199 | for win := 0; win < 3; win++ { 200 | md.ScalefacS[gr][ch][sfb][win] = m.Bits(nbits) 201 | } 202 | } 203 | } else { 204 | for sfb := 0; sfb < 12; sfb++ { 205 | //slen1 for band 3-5,slen2 for 6-11 206 | nbits := slen2 207 | if sfb < 6 { 208 | nbits = slen1 209 | } 210 | for win := 0; win < 3; win++ { 211 | md.ScalefacS[gr][ch][sfb][win] = m.Bits(nbits) 212 | } 213 | } 214 | } 215 | } else { 216 | // Scale factor bands 0-5 217 | if sideInfo.Scfsi[ch][0] == 0 || gr == 0 { 218 | for sfb := 0; sfb < 6; sfb++ { 219 | md.ScalefacL[gr][ch][sfb] = m.Bits(slen1) 220 | } 221 | } else if sideInfo.Scfsi[ch][0] == 1 && gr == 1 { 222 | // Copy scalefactors from granule 0 to granule 1 223 | // TODO: This is not listed on the spec. 224 | for sfb := 0; sfb < 6; sfb++ { 225 | md.ScalefacL[1][ch][sfb] = md.ScalefacL[0][ch][sfb] 226 | } 227 | } 228 | // Scale factor bands 6-10 229 | if sideInfo.Scfsi[ch][1] == 0 || gr == 0 { 230 | for sfb := 6; sfb < 11; sfb++ { 231 | md.ScalefacL[gr][ch][sfb] = m.Bits(slen1) 232 | } 233 | } else if sideInfo.Scfsi[ch][1] == 1 && gr == 1 { 234 | // Copy scalefactors from granule 0 to granule 1 235 | for sfb := 6; sfb < 11; sfb++ { 236 | md.ScalefacL[1][ch][sfb] = md.ScalefacL[0][ch][sfb] 237 | } 238 | } 239 | // Scale factor bands 11-15 240 | if sideInfo.Scfsi[ch][2] == 0 || gr == 0 { 241 | for sfb := 11; sfb < 16; sfb++ { 242 | md.ScalefacL[gr][ch][sfb] = m.Bits(slen2) 243 | } 244 | } else if sideInfo.Scfsi[ch][2] == 1 && gr == 1 { 245 | // Copy scalefactors from granule 0 to granule 1 246 | for sfb := 11; sfb < 16; sfb++ { 247 | md.ScalefacL[1][ch][sfb] = md.ScalefacL[0][ch][sfb] 248 | } 249 | } 250 | // Scale factor bands 16-20 251 | if sideInfo.Scfsi[ch][3] == 0 || gr == 0 { 252 | for sfb := 16; sfb < 21; sfb++ { 253 | md.ScalefacL[gr][ch][sfb] = m.Bits(slen2) 254 | } 255 | } else if sideInfo.Scfsi[ch][3] == 1 && gr == 1 { 256 | // Copy scalefactors from granule 0 to granule 1 257 | for sfb := 16; sfb < 21; sfb++ { 258 | md.ScalefacL[1][ch][sfb] = md.ScalefacL[0][ch][sfb] 259 | } 260 | } 261 | } 262 | // Read Huffman coded data. Skip stuffing bits. 263 | if err := readHuffman(m, header, sideInfo, md, part_2_start, gr, ch); err != nil { 264 | return nil, nil, err 265 | } 266 | } 267 | } 268 | // The ancillary data is stored here,but we ignore it. 269 | return md, m, nil 270 | } 271 | 272 | func read(source FullReader, prev *bits.Bits, size int, offset int) (*bits.Bits, error) { 273 | if size > 1500 { 274 | return nil, fmt.Errorf("mp3: size = %d", size) 275 | } 276 | // Check that there's data available from previous frames if needed 277 | if prev != nil && offset > prev.LenInBytes() { 278 | // No, there is not, so we skip decoding this frame, but we have to 279 | // read the main_data bits from the bitstream in case they are needed 280 | // for decoding the next frame. 281 | buf := make([]byte, size) 282 | if n, err := source.ReadFull(buf); n < size { 283 | if err == io.EOF { 284 | return nil, &consts.UnexpectedEOF{"maindata.Read (1)"} 285 | } 286 | return nil, err 287 | } 288 | // TODO: Define a special error and enable to continue the next frame. 289 | return bits.Append(prev, buf), nil 290 | } 291 | // Copy data from previous frames 292 | vec := []byte{} 293 | if prev != nil { 294 | vec = prev.Tail(offset) 295 | } 296 | // Read the main_data from file 297 | buf := make([]byte, size) 298 | if n, err := source.ReadFull(buf); n < size { 299 | if err == io.EOF { 300 | return nil, &consts.UnexpectedEOF{"maindata.Read (2)"} 301 | } 302 | return nil, err 303 | } 304 | return bits.New(append(vec, buf...)), nil 305 | } 306 | -------------------------------------------------------------------------------- /internal/sideinfo/sideinfo.go: -------------------------------------------------------------------------------- 1 | // Copyright 2017 Hajime Hoshi 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package sideinfo 16 | 17 | import ( 18 | "fmt" 19 | "io" 20 | 21 | "github.com/hajimehoshi/go-mp3/internal/bits" 22 | "github.com/hajimehoshi/go-mp3/internal/consts" 23 | "github.com/hajimehoshi/go-mp3/internal/frameheader" 24 | ) 25 | 26 | type FullReader interface { 27 | ReadFull([]byte) (int, error) 28 | } 29 | 30 | // A SideInfo is MPEG1 Layer 3 Side Information. 31 | // [2][2] means [gr][ch]. 32 | type SideInfo struct { 33 | MainDataBegin int // 9 bits 34 | PrivateBits int // 3 bits in mono, 5 in stereo 35 | Scfsi [2][4]int // 1 bit 36 | Part2_3Length [2][2]int // 12 bits 37 | BigValues [2][2]int // 9 bits 38 | GlobalGain [2][2]int // 8 bits 39 | ScalefacCompress [2][2]int // 4 bits 40 | WinSwitchFlag [2][2]int // 1 bit 41 | 42 | BlockType [2][2]int // 2 bits 43 | MixedBlockFlag [2][2]int // 1 bit 44 | TableSelect [2][2][3]int // 5 bits 45 | SubblockGain [2][2][3]int // 3 bits 46 | 47 | Region0Count [2][2]int // 4 bits 48 | Region1Count [2][2]int // 3 bits 49 | 50 | Preflag [2][2]int // 1 bit 51 | ScalefacScale [2][2]int // 1 bit 52 | Count1TableSelect [2][2]int // 1 bit 53 | Count1 [2][2]int // Not in file, calc by huffman decoder 54 | } 55 | 56 | var sideInfoBitsToRead = [2][4]int{ 57 | { // MPEG 1 58 | 9, 5, 3, 4, 59 | }, 60 | { // MPEG 2 61 | 8, 1, 2, 9, 62 | }, 63 | } 64 | 65 | func Read(source FullReader, header frameheader.FrameHeader) (*SideInfo, error) { 66 | nch := header.NumberOfChannels() 67 | framesize, err := header.FrameSize() 68 | if err != nil { 69 | return nil, err 70 | } 71 | if framesize > 2000 { 72 | return nil, fmt.Errorf("mp3: framesize = %d\n", framesize) 73 | } 74 | sideinfo_size := header.SideInfoSize() 75 | 76 | // Main data size is the rest of the frame,including ancillary data 77 | main_data_size := framesize - sideinfo_size - 4 // sync+header 78 | // CRC is 2 bytes 79 | if header.ProtectionBit() == 0 { 80 | main_data_size -= 2 81 | } 82 | // Read sideinfo from bitstream into buffer used by Bits() 83 | buf := make([]byte, sideinfo_size) 84 | n, err := source.ReadFull(buf) 85 | if n < sideinfo_size { 86 | if err == io.EOF { 87 | return nil, &consts.UnexpectedEOF{"sideinfo.Read"} 88 | } 89 | return nil, fmt.Errorf("mp3: couldn't read sideinfo %d bytes: %v", sideinfo_size, err) 90 | } 91 | s := bits.New(buf) 92 | 93 | mpeg1Frame := header.LowSamplingFrequency() == 0 94 | bitsToRead := sideInfoBitsToRead[header.LowSamplingFrequency()] 95 | 96 | // Parse audio data 97 | // Pointer to where we should start reading main data 98 | si := &SideInfo{} 99 | si.MainDataBegin = s.Bits(bitsToRead[0]) 100 | // Get private bits. Not used for anything. 101 | if header.Mode() == consts.ModeSingleChannel { 102 | si.PrivateBits = s.Bits(bitsToRead[1]) 103 | } else { 104 | si.PrivateBits = s.Bits(bitsToRead[2]) 105 | } 106 | 107 | if mpeg1Frame { 108 | // Get scale factor selection information 109 | for ch := 0; ch < nch; ch++ { 110 | for scfsi_band := 0; scfsi_band < 4; scfsi_band++ { 111 | si.Scfsi[ch][scfsi_band] = s.Bits(1) 112 | } 113 | } 114 | } 115 | // Get the rest of the side information 116 | for gr := 0; gr < header.Granules(); gr++ { 117 | for ch := 0; ch < nch; ch++ { 118 | si.Part2_3Length[gr][ch] = s.Bits(12) 119 | si.BigValues[gr][ch] = s.Bits(9) 120 | si.GlobalGain[gr][ch] = s.Bits(8) 121 | si.ScalefacCompress[gr][ch] = s.Bits(bitsToRead[3]) 122 | si.WinSwitchFlag[gr][ch] = s.Bits(1) 123 | if si.WinSwitchFlag[gr][ch] == 1 { 124 | si.BlockType[gr][ch] = s.Bits(2) 125 | si.MixedBlockFlag[gr][ch] = s.Bits(1) 126 | for region := 0; region < 2; region++ { 127 | si.TableSelect[gr][ch][region] = s.Bits(5) 128 | } 129 | for window := 0; window < 3; window++ { 130 | si.SubblockGain[gr][ch][window] = s.Bits(3) 131 | } 132 | 133 | // TODO: This is not listed on the spec. Is this correct?? 134 | if si.BlockType[gr][ch] == 2 && si.MixedBlockFlag[gr][ch] == 0 { 135 | si.Region0Count[gr][ch] = 8 // Implicit 136 | } else { 137 | si.Region0Count[gr][ch] = 7 // Implicit 138 | } 139 | // The standard is wrong on this!!! 140 | // Implicit 141 | si.Region1Count[gr][ch] = 20 - si.Region0Count[gr][ch] 142 | } else { 143 | for region := 0; region < 3; region++ { 144 | si.TableSelect[gr][ch][region] = s.Bits(5) 145 | } 146 | si.Region0Count[gr][ch] = s.Bits(4) 147 | si.Region1Count[gr][ch] = s.Bits(3) 148 | si.BlockType[gr][ch] = 0 // Implicit 149 | if !mpeg1Frame { 150 | si.MixedBlockFlag[0][ch] = 0 151 | } 152 | } 153 | if mpeg1Frame { 154 | si.Preflag[gr][ch] = s.Bits(1) 155 | } 156 | si.ScalefacScale[gr][ch] = s.Bits(1) 157 | si.Count1TableSelect[gr][ch] = s.Bits(1) 158 | } 159 | } 160 | return si, nil 161 | } 162 | -------------------------------------------------------------------------------- /source.go: -------------------------------------------------------------------------------- 1 | // Copyright 2017 Hajime Hoshi 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package mp3 16 | 17 | import ( 18 | "errors" 19 | "io" 20 | ) 21 | 22 | type source struct { 23 | reader io.Reader 24 | buf []byte 25 | pos int64 26 | } 27 | 28 | func (s *source) Seek(position int64, whence int) (int64, error) { 29 | seeker, ok := s.reader.(io.Seeker) 30 | if !ok { 31 | return 0, errors.New("mp3: source must be io.Seeker") 32 | } 33 | s.buf = nil 34 | n, err := seeker.Seek(position, whence) 35 | if err != nil { 36 | return 0, err 37 | } 38 | s.pos = n 39 | return n, nil 40 | } 41 | 42 | func (s *source) skipTags() error { 43 | buf := make([]byte, 3) 44 | if _, err := s.ReadFull(buf); err != nil { 45 | return err 46 | } 47 | switch string(buf) { 48 | case "TAG": 49 | buf := make([]byte, 125) 50 | if _, err := s.ReadFull(buf); err != nil { 51 | return err 52 | } 53 | 54 | case "ID3": 55 | // Skip version (2 bytes) and flag (1 byte) 56 | buf := make([]byte, 3) 57 | if _, err := s.ReadFull(buf); err != nil { 58 | return err 59 | } 60 | 61 | buf = make([]byte, 4) 62 | n, err := s.ReadFull(buf) 63 | if err != nil { 64 | return err 65 | } 66 | if n != 4 { 67 | return nil 68 | } 69 | size := (uint32(buf[0]) << 21) | (uint32(buf[1]) << 14) | 70 | (uint32(buf[2]) << 7) | uint32(buf[3]) 71 | buf = make([]byte, size) 72 | if _, err := s.ReadFull(buf); err != nil { 73 | return err 74 | } 75 | 76 | default: 77 | s.Unread(buf) 78 | } 79 | 80 | return nil 81 | } 82 | 83 | func (s *source) rewind() error { 84 | if _, err := s.Seek(0, io.SeekStart); err != nil { 85 | return err 86 | } 87 | s.pos = 0 88 | s.buf = nil 89 | return nil 90 | } 91 | 92 | func (s *source) Unread(buf []byte) { 93 | s.buf = append(s.buf, buf...) 94 | s.pos -= int64(len(buf)) 95 | } 96 | 97 | func (s *source) ReadFull(buf []byte) (int, error) { 98 | read := 0 99 | if s.buf != nil { 100 | read = copy(buf, s.buf) 101 | if len(s.buf) > read { 102 | s.buf = s.buf[read:] 103 | } else { 104 | s.buf = nil 105 | } 106 | if len(buf) == read { 107 | return read, nil 108 | } 109 | } 110 | 111 | n, err := io.ReadFull(s.reader, buf[read:]) 112 | if err != nil { 113 | // Allow if all data can't be read. This is common. 114 | if err == io.ErrUnexpectedEOF { 115 | err = io.EOF 116 | } 117 | } 118 | s.pos += int64(n) 119 | return n + read, err 120 | } 121 | --------------------------------------------------------------------------------