avframes/pkg/mpeg/mpeg.go

154 lines
2.7 KiB
Go

package mpeg
import "fmt"
type (
AudioVersionId int
Layer int
)
type Decoder struct{}
const (
AudioVersionMPEG25 AudioVersionId = iota
AudioVersionReserved
AudioVersionMPEG2
AudioVersionMPEG1
)
const (
LayerReserved Layer = iota
LayerIII
LayerII
LayerI
)
var (
bitRates = [5][16]int{
{0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448, 0},
{0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384, 0},
{0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 0},
{0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256, 0},
{0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 0},
}
sampleRates = [4][4]int{
{11025, 12000, 8000, 0},
{0, 0, 0, 0},
{22050, 24000, 16000, 0},
{44100, 48000, 32000, 0},
}
samplesPerFrame = [4][4]int{
{0, 576, 1152, 384},
{0, 0, 0, 0},
{0, 576, 1152, 384},
{0, 1152, 1152, 384},
}
)
type Header []byte
func (h Header) HasSyncWord() bool {
return h[0] == 0xff && h[1]&0xe0 == 0xe0
}
func (h Header) AudioVersionId() AudioVersionId {
return AudioVersionId((h[1] >> 3) & 0x3)
}
func (h Header) Layer() Layer {
return Layer((h[1] >> 1) & 0x3)
}
func (h Header) IsProtected() bool {
return h[1]&1 == 1
}
func (h Header) String() string {
return fmt.Sprintf("%08b", h)
}
func (h Header) SamplesPerFrame() int {
return samplesPerFrame[h.AudioVersionId()][h.Layer()]
}
func (h Header) SampleRate() int {
i := h.AudioVersionId()
j := (h[2] >> 2) & 0x3
return sampleRates[i][j]
}
func (h Header) PaddingBytes() int {
if isPadded := (h[2] >> 1) & 1; isPadded == 0 {
return 0
}
switch h.Layer() {
case LayerI:
return 4
case LayerII, LayerIII:
return 1
default:
return 0
}
}
func (h Header) IsValid() bool {
if len(h) < 4 {
return false
}
if !h.HasSyncWord() {
return false
}
if h.AudioVersionId() == AudioVersionReserved {
return false
}
if h.Layer() == LayerReserved {
return false
}
if h.BitRate() == 0 {
return false
}
if h.SampleRate() == 0 {
return false
}
return true
}
func (h Header) Len() int {
// TODO premultiply bitrates?
bps := float64(h.SamplesPerFrame()) / 8
return int((bps*float64(h.BitRate()*1000))/float64(h.SampleRate()) + float64(h.PaddingBytes()))
}
func (h Header) BitRate() int {
// TODO: improve this
var j int
switch h.AudioVersionId() {
case AudioVersionMPEG1:
switch h.Layer() {
case LayerIII:
j = 2
case LayerII:
j = 1
case LayerI:
j = 0
default:
return 0
}
case AudioVersionReserved:
return 0
case AudioVersionMPEG2, AudioVersionMPEG25:
switch h.Layer() {
case LayerIII, LayerII:
j = 4
case LayerI:
j = 3
default:
return 0
}
}
return bitRates[j][h[2]>>4]
}