Changed format Identify to fixed number of bytes

This commit is contained in:
DataHoarder 2023-01-29 12:48:40 +01:00
parent 5241fcfde7
commit 3d1ca436e7
Signed by: DataHoarder
SSH key fingerprint: SHA256:OLTRf6Fl87G52SiR7sWLGNzlJt4WOX+tfI2yxo0z7xk
13 changed files with 23 additions and 16 deletions

View file

@ -7,6 +7,7 @@ import (
"errors"
"fmt"
"git.gammaspectra.live/S.O.N.G/Kirika/audio"
"git.gammaspectra.live/S.O.N.G/Kirika/audio/format"
"git.gammaspectra.live/S.O.N.G/go-fdkaac/fdkaac"
aacAdts "github.com/Eyevinn/mp4ff/aac"
"github.com/Eyevinn/mp4ff/mp4"
@ -503,7 +504,7 @@ func (f Format) Encode(source audio.Source, writer io.WriteCloser, options map[s
return nil
}
func (f Format) Identify(_ []byte, extension string) bool {
func (f Format) Identify(_ [format.IdentifyPeekBytes]byte, extension string) bool {
//TODO: add .m4a/mp4 detection
return extension == "aac" || extension == "adts"
}

View file

@ -6,7 +6,6 @@ import (
"fmt"
"git.gammaspectra.live/S.O.N.G/Kirika/audio"
"git.gammaspectra.live/S.O.N.G/Kirika/vector"
"git.gammaspectra.live/S.O.N.G/voaac-go"
"io"
)
@ -93,6 +92,6 @@ func (f Format) Encode(source audio.Source, writer io.WriteCloser, options map[s
return nil
}
func (f Format) Identify(peek []byte, extension string) bool {
func (f Format) Identify(peek [format.IdentifyPeekBytes]byte, extension string) bool {
return extension == "aac" || extension == "adts"
}

View file

@ -136,6 +136,6 @@ func (f Format) Encode(source audio.Source, writer io.WriteCloser, options map[s
return nil
}
func (f Format) Identify(_ []byte, extension string) bool {
func (f Format) Identify(_ [format.IdentifyPeekBytes]byte, extension string) bool {
return extension == "alac" || extension == "mp4" || extension == "m4a"
}

View file

@ -224,7 +224,7 @@ func (f Format) Encode(source audio.Source, writer io.WriteCloser, options map[s
return nil
}
func (f Format) Identify(peek []byte, extension string) bool {
func (f Format) Identify(peek [format.IdentifyPeekBytes]byte, extension string) bool {
//No Ogg supported
return bytes.Compare(peek[:4], []byte{'f', 'L', 'a', 'C'}) == 0 || (bytes.Compare(peek[:3], []byte{'I', 'D', '3'}) == 0 && extension != "mp3") || extension == "flac"
}

View file

@ -161,6 +161,6 @@ func (f Format) Encode(source audio.Source, writer io.WriteCloser, options map[s
return nil
}
func (f Format) Identify(peek []byte, extension string) bool {
func (f Format) Identify(peek [format.IdentifyPeekBytes]byte, extension string) bool {
return (bytes.Compare(peek[:4], []byte{'O', 'g', 'g', 'S'}) == 0 && extension != "opus" && extension != "vorbis") || bytes.Compare(peek[:4], []byte{'f', 'L', 'a', 'C'}) == 0 || (bytes.Compare(peek[:3], []byte{'I', 'D', '3'}) == 0 && extension != "mp3") || extension == "flac" || extension == "ogg"
}

View file

@ -5,9 +5,11 @@ import (
"io"
)
const IdentifyPeekBytes = 16
type Format interface {
// Identify checks whether a format is of a type. peek includes a few first bytes, extension is the lowercase file extension without a dot.
Identify(peek []byte, extension string) bool
Identify(peek [IdentifyPeekBytes]byte, extension string) bool
// Name returns the name of the codec or format
Name() string
}

View file

@ -37,7 +37,7 @@ func (f NullFormat) Encode(audio.Source, io.WriteCloser, map[string]interface{})
return errors.New("null format")
}
func (f NullFormat) Identify([]byte, string) bool {
func (f NullFormat) Identify([format.IdentifyPeekBytes]byte, string) bool {
return false
}
@ -103,8 +103,8 @@ func GetDecoders(r io.ReadSeekCloser, pathName string) (decoders []format.Decode
if err != nil {
return
}
peek := make([]byte, 16)
if _, err = r.Read(peek); err != nil {
var peek [format.IdentifyPeekBytes]byte
if _, err = r.Read(peek[:]); err != nil {
return
}
if _, err = r.Seek(startOffset, io.SeekStart); err != nil {

View file

@ -5,6 +5,7 @@ package mp3
import (
"bytes"
"git.gammaspectra.live/S.O.N.G/Kirika/audio"
"git.gammaspectra.live/S.O.N.G/Kirika/audio/format"
mp3Lib "git.gammaspectra.live/S.O.N.G/minimp3"
"golang.org/x/exp/slices"
"io"
@ -53,7 +54,7 @@ func (f Format) Open(r io.ReadSeekCloser) (audio.Source, error) {
return source, nil
}
func (f Format) Identify(peek []byte, extension string) bool {
func (f Format) Identify(peek [format.IdentifyPeekBytes]byte, extension string) bool {
//match ID3 / sync header
return peek[0] == 0xff && (peek[1]>>5) == 0b111 || (bytes.Compare(peek[:3], []byte{'I', 'D', '3'}) == 0 && extension != "flac") || extension == "mp3"
}

View file

@ -5,6 +5,7 @@ package mp3
import (
"bytes"
"git.gammaspectra.live/S.O.N.G/Kirika/audio"
"git.gammaspectra.live/S.O.N.G/Kirika/audio/format"
mp3Lib "git.gammaspectra.live/S.O.N.G/go-mp3"
"golang.org/x/exp/slices"
"io"
@ -51,7 +52,7 @@ func (f Format) Open(r io.ReadSeekCloser) (audio.Source, error) {
return source, nil
}
func (f Format) Identify(peek []byte, extension string) bool {
func (f Format) Identify(peek [format.IdentifyPeekBytes]byte, extension string) bool {
//match ID3 / sync header
return peek[0] == 0xff && (peek[1]>>5) == 0b111 || (bytes.Compare(peek[:3], []byte{'I', 'D', '3'}) == 0 && extension != "flac") || extension == "mp3"
}

View file

@ -8,6 +8,7 @@ import (
"fmt"
"git.gammaspectra.live/S.O.N.G/Kirika/audio"
"git.gammaspectra.live/S.O.N.G/Kirika/audio/filter"
"git.gammaspectra.live/S.O.N.G/Kirika/audio/format"
libopus "git.gammaspectra.live/S.O.N.G/go-pus"
"io"
"time"
@ -177,6 +178,6 @@ func (f Format) Encode(source audio.Source, writer io.WriteCloser, options map[s
}
func (f Format) Identify(peek []byte, extension string) bool {
func (f Format) Identify(peek [format.IdentifyPeekBytes]byte, extension string) bool {
return bytes.Compare(peek[:4], []byte{'O', 'g', 'g', 'S'}) == 0 || extension == "opus" || extension == "ogg"
}

View file

@ -180,6 +180,6 @@ func (f Format) Encode(source audio.Source, writer io.WriteCloser, options map[s
return nil
}
func (f Format) Identify(peek []byte, extension string) bool {
func (f Format) Identify(peek [format.IdentifyPeekBytes]byte, extension string) bool {
return bytes.Compare(peek[:4], []byte{'T', 'T', 'A', '1'}) == 0 || extension == "tta"
}

View file

@ -6,6 +6,7 @@ import (
"bytes"
"fmt"
"git.gammaspectra.live/S.O.N.G/Kirika/audio"
"git.gammaspectra.live/S.O.N.G/Kirika/audio/format"
libvorbis "git.gammaspectra.live/S.O.N.G/go-vorbis"
"io"
)
@ -111,6 +112,6 @@ func (f Format) Encode(source audio.Source, writer io.WriteCloser, options map[s
}
func (f Format) Identify(peek []byte, extension string) bool {
func (f Format) Identify(peek [format.IdentifyPeekBytes]byte, extension string) bool {
return bytes.Compare(peek[:4], []byte{'O', 'g', 'g', 'S'}) == 0 || extension == "vorbis" || extension == "ogg"
}

View file

@ -5,6 +5,7 @@ package vorbis
import (
"bytes"
"git.gammaspectra.live/S.O.N.G/Kirika/audio"
"git.gammaspectra.live/S.O.N.G/Kirika/audio/format"
libvorbis "github.com/jfreymuth/oggvorbis"
"io"
)
@ -53,6 +54,6 @@ func (f Format) Open(r io.ReadSeekCloser) (audio.Source, error) {
return source, nil
}
func (f Format) Identify(peek []byte, extension string) bool {
func (f Format) Identify(peek [format.IdentifyPeekBytes]byte, extension string) bool {
return bytes.Compare(peek[:4], []byte{'O', 'g', 'g', 'S'}) == 0 || extension == "vorbis" || extension == "ogg"
}