2017-06-11 11:12:12 +02:00
|
|
|
// Copyright 2017 The Ebiten Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
// +build !js
|
|
|
|
|
|
|
|
package mp3
|
|
|
|
|
|
|
|
import (
|
2017-06-17 12:33:49 +02:00
|
|
|
"errors"
|
2017-06-11 11:12:12 +02:00
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
reader io.Reader
|
|
|
|
readerCache []uint8
|
|
|
|
readerPos int
|
|
|
|
readerEOF bool
|
|
|
|
)
|
|
|
|
|
2017-06-17 16:33:29 +02:00
|
|
|
func (f *frame) numberOfChannels() int {
|
2017-06-17 11:42:43 +02:00
|
|
|
if f.header.mode == mpeg1ModeSingleChannel {
|
2017-06-17 16:33:29 +02:00
|
|
|
return 1
|
2017-06-16 23:28:25 +02:00
|
|
|
}
|
2017-06-17 16:33:29 +02:00
|
|
|
return 2
|
|
|
|
}
|
|
|
|
|
2017-06-17 17:02:51 +02:00
|
|
|
func (f *frame) decodeL3() []uint8 {
|
|
|
|
out := make([]uint8, 576*4*2)
|
2017-06-17 16:33:29 +02:00
|
|
|
nch := f.numberOfChannels()
|
2017-06-16 23:28:25 +02:00
|
|
|
for gr := 0; gr < 2; gr++ {
|
|
|
|
for ch := 0; ch < nch; ch++ {
|
2017-06-17 11:42:43 +02:00
|
|
|
f.l3Requantize(gr, ch)
|
2017-06-16 23:43:50 +02:00
|
|
|
// Reorder short blocks
|
2017-06-17 11:42:43 +02:00
|
|
|
f.l3Reorder(gr, ch)
|
2017-06-16 23:28:25 +02:00
|
|
|
}
|
2017-06-17 11:42:43 +02:00
|
|
|
f.l3Stereo(gr)
|
2017-06-16 23:28:25 +02:00
|
|
|
for ch := 0; ch < nch; ch++ {
|
2017-06-17 11:42:43 +02:00
|
|
|
f.l3Antialias(gr, ch)
|
2017-06-16 23:28:25 +02:00
|
|
|
// (IMDCT,windowing,overlapp add)
|
2017-06-17 11:42:43 +02:00
|
|
|
f.l3HybridSynthesis(gr, ch)
|
|
|
|
f.l3FrequencyInversion(gr, ch)
|
2017-06-16 23:28:25 +02:00
|
|
|
// Polyphase subband synthesis
|
2017-06-17 17:02:51 +02:00
|
|
|
f.l3SubbandSynthesis(gr, ch, out[576*4*gr:])
|
2017-06-16 23:28:25 +02:00
|
|
|
}
|
|
|
|
}
|
2017-06-17 17:02:51 +02:00
|
|
|
return out
|
2017-06-16 23:28:25 +02:00
|
|
|
}
|
|
|
|
|
2017-06-16 18:36:58 +02:00
|
|
|
func getByte() (uint8, error) {
|
2017-06-11 11:12:12 +02:00
|
|
|
for len(readerCache) == 0 && !readerEOF {
|
|
|
|
buf := make([]uint8, 4096)
|
|
|
|
n, err := reader.Read(buf)
|
2017-06-16 18:32:17 +02:00
|
|
|
readerCache = append(readerCache, buf[:n]...)
|
2017-06-11 11:12:12 +02:00
|
|
|
if err != nil {
|
|
|
|
if err == io.EOF {
|
|
|
|
readerEOF = true
|
|
|
|
} else {
|
2017-06-16 18:36:58 +02:00
|
|
|
return 0, err
|
2017-06-11 11:12:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-06-16 17:34:54 +02:00
|
|
|
if len(readerCache) == 0 {
|
2017-06-16 18:36:58 +02:00
|
|
|
return 0, io.EOF
|
2017-06-11 11:12:12 +02:00
|
|
|
}
|
|
|
|
b := readerCache[0]
|
|
|
|
readerCache = readerCache[1:]
|
|
|
|
readerPos++
|
2017-06-16 18:36:58 +02:00
|
|
|
return b, nil
|
2017-06-11 11:12:12 +02:00
|
|
|
}
|
|
|
|
|
2017-06-16 17:34:54 +02:00
|
|
|
func getBytes(buf []int) (int, error) {
|
|
|
|
for i := range buf {
|
2017-06-16 18:36:58 +02:00
|
|
|
v, err := getByte()
|
|
|
|
buf[i] = int(v)
|
|
|
|
if err == io.EOF {
|
2017-06-16 17:34:54 +02:00
|
|
|
return i, io.EOF
|
2017-06-12 19:49:15 +02:00
|
|
|
}
|
|
|
|
}
|
2017-06-16 17:34:54 +02:00
|
|
|
return len(buf), nil
|
2017-06-12 19:49:15 +02:00
|
|
|
}
|
|
|
|
|
2017-06-16 23:59:27 +02:00
|
|
|
func getFilepos() int {
|
2017-06-16 23:48:27 +02:00
|
|
|
return readerPos
|
2017-06-11 11:12:12 +02:00
|
|
|
}
|
|
|
|
|
2017-06-17 12:33:49 +02:00
|
|
|
var eof = errors.New("mp3: expected EOF")
|
|
|
|
|
2017-06-11 11:12:12 +02:00
|
|
|
func decode(r io.Reader, w io.Writer) error {
|
|
|
|
reader = r
|
2017-06-18 09:45:36 +02:00
|
|
|
var f *frame
|
2017-06-17 12:33:49 +02:00
|
|
|
for {
|
2017-06-18 09:45:36 +02:00
|
|
|
var err error
|
|
|
|
f, err = f.readNextFrame()
|
2017-06-16 19:43:13 +02:00
|
|
|
if err == nil {
|
2017-06-17 17:02:51 +02:00
|
|
|
out := f.decodeL3()
|
|
|
|
if _, err := w.Write(out); err != nil {
|
2017-06-16 23:28:25 +02:00
|
|
|
return err
|
|
|
|
}
|
2017-06-11 11:12:12 +02:00
|
|
|
continue
|
|
|
|
}
|
2017-06-17 12:33:49 +02:00
|
|
|
if err == eof {
|
2017-06-11 11:12:12 +02:00
|
|
|
break
|
|
|
|
}
|
2017-06-16 20:01:39 +02:00
|
|
|
return err
|
2017-06-11 11:12:12 +02:00
|
|
|
}
|
2017-06-16 20:01:39 +02:00
|
|
|
return nil
|
2017-06-11 11:12:12 +02:00
|
|
|
}
|