112 lines
2.1 KiB
Go
112 lines
2.1 KiB
Go
//go:build !disable_format_aac && disable_codec_libfdk_aac
|
|
// +build !disable_format_aac,disable_codec_libfdk_aac
|
|
|
|
package aac
|
|
|
|
import (
|
|
"fmt"
|
|
"git.gammaspectra.live/S.O.N.G/Kirika/audio"
|
|
"git.gammaspectra.live/S.O.N.G/Kirika/cgo"
|
|
"github.com/gen2brain/aac-go"
|
|
"io"
|
|
)
|
|
|
|
type Format struct {
|
|
}
|
|
|
|
func NewFormat() Format {
|
|
return Format{}
|
|
}
|
|
|
|
func (f Format) Name() string {
|
|
return "aac"
|
|
}
|
|
|
|
func (f Format) Description() string {
|
|
return "vo-aacenc (gen2brain/aac-go)"
|
|
}
|
|
|
|
func (f Format) Encode(source audio.Source, writer io.WriteCloser, options map[string]interface{}) error {
|
|
var bitrate = 128
|
|
var isHEv2 bool
|
|
|
|
if options != nil {
|
|
var val interface{}
|
|
var ok bool
|
|
var intVal int
|
|
var strVal string
|
|
if val, ok = options["bitrate"]; ok {
|
|
if strVal, ok = val.(string); ok {
|
|
switch strVal {
|
|
case "320k":
|
|
bitrate = 320
|
|
case "256k":
|
|
bitrate = 256
|
|
case "192k":
|
|
bitrate = 192
|
|
case "128k":
|
|
bitrate = 128
|
|
|
|
default:
|
|
return fmt.Errorf("unknown setting bitrate=%s", strVal)
|
|
}
|
|
} else if intVal, ok = val.(int); ok {
|
|
bitrate = intVal
|
|
}
|
|
}
|
|
if val, ok = options["mode"]; ok {
|
|
if strVal, ok = val.(string); ok {
|
|
switch strVal {
|
|
case "lc":
|
|
isHEv2 = false
|
|
case "hev2":
|
|
isHEv2 = true
|
|
|
|
default:
|
|
return fmt.Errorf("unknown setting mode=%s", strVal)
|
|
}
|
|
}
|
|
}
|
|
if val, ok = options["format"]; ok {
|
|
if strVal, ok = val.(string); ok {
|
|
if strVal != "adts" {
|
|
return fmt.Errorf("format %s not supported", strVal)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
encoder, err := aac.NewEncoder(writer, &aac.Options{
|
|
BitRate: bitrate * 1024,
|
|
NumChannels: source.Channels,
|
|
SampleRate: source.SampleRate,
|
|
})
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer encoder.Close()
|
|
|
|
pReader, pWriter := io.Pipe()
|
|
defer pWriter.Close()
|
|
|
|
go func() {
|
|
defer pWriter.Close()
|
|
for block := range source.Blocks {
|
|
if _, decodeErr := pWriter.Write(cgo.Float32ToInt16(block)); decodeErr != nil {
|
|
return nil
|
|
}
|
|
}
|
|
}()
|
|
|
|
if err = encoder.Encode(pReader); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (f Format) Identify(peek []byte, extension string) bool {
|
|
return extension == "aac" || extension == "adts"
|
|
}
|