2020-06-08 03:29:51 +03:00
|
|
|
package flac
|
|
|
|
|
|
|
|
// https://xiph.org/flac/format.html
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/md5"
|
|
|
|
"fmt"
|
2021-08-17 13:06:32 +03:00
|
|
|
|
|
|
|
"github.com/wader/fq/format"
|
|
|
|
"github.com/wader/fq/format/registry"
|
|
|
|
"github.com/wader/fq/internal/num"
|
|
|
|
"github.com/wader/fq/pkg/decode"
|
2020-06-08 03:29:51 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var flacMetadatablockFormat []*decode.Format
|
|
|
|
var flacFrameFormat []*decode.Format
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
registry.MustRegister(&decode.Format{
|
|
|
|
Name: format.FLAC,
|
|
|
|
Description: "Free Lossless Audio Codec file",
|
|
|
|
Groups: []string{format.PROBE},
|
|
|
|
DecodeFn: flacDecode,
|
|
|
|
Dependencies: []decode.Dependency{
|
|
|
|
{Names: []string{format.FLAC_METADATABLOCK}, Formats: &flacMetadatablockFormat},
|
|
|
|
{Names: []string{format.FLAC_FRAME}, Formats: &flacFrameFormat},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func flacDecode(d *decode.D, in interface{}) interface{} {
|
|
|
|
d.FieldValidateUTF8("magic", "fLaC")
|
|
|
|
|
|
|
|
var streamInfo format.FlacMetadatablockStreamInfo
|
|
|
|
var flacFrameIn format.FlacFrameIn
|
|
|
|
var framesNDecodedSamples uint64
|
|
|
|
var streamTotalSamples uint64
|
|
|
|
var streamDecodedSamples uint64
|
|
|
|
|
|
|
|
d.FieldArrayFn("metadatablocks", func(d *decode.D) {
|
|
|
|
for {
|
2021-08-16 13:46:59 +03:00
|
|
|
_, v := d.FieldFormat("metadatablock", flacMetadatablockFormat)
|
2020-06-08 03:29:51 +03:00
|
|
|
flacMetadatablockOut, ok := v.(format.FlacMetadatablockOut)
|
|
|
|
if !ok {
|
|
|
|
panic(fmt.Sprintf("expected FlacMetadatablockOut got %#+v", v))
|
|
|
|
}
|
|
|
|
if flacMetadatablockOut.HasStreamInfo {
|
|
|
|
streamInfo = flacMetadatablockOut.StreamInfo
|
|
|
|
streamTotalSamples = streamInfo.TotalSamplesInStream
|
|
|
|
flacFrameIn = format.FlacFrameIn{StreamInfo: streamInfo}
|
|
|
|
}
|
|
|
|
if flacMetadatablockOut.IsLastBlock {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
md5Samples := md5.New()
|
|
|
|
d.FieldArrayFn("frames", func(d *decode.D) {
|
|
|
|
for d.NotEnd() {
|
|
|
|
// flac frame might need some fields from stream info to decode
|
2021-08-16 13:46:59 +03:00
|
|
|
_, v := d.FieldFormat("frame", flacFrameFormat, decode.FormatOptions{InArg: flacFrameIn})
|
2020-06-08 03:29:51 +03:00
|
|
|
ffo, ok := v.(format.FlacFrameOut)
|
|
|
|
if !ok {
|
|
|
|
panic(fmt.Sprintf("expected FlacFrameOut got %#+v", v))
|
|
|
|
}
|
|
|
|
|
|
|
|
frameStreamSamplesBuf := ffo.SamplesBuf
|
|
|
|
if streamTotalSamples > 0 {
|
|
|
|
samplesInFrame := num.MinUInt64(streamTotalSamples-streamDecodedSamples, ffo.Samples)
|
|
|
|
frameStreamSamplesBuf = frameStreamSamplesBuf[0 : samplesInFrame*uint64(ffo.Channels*ffo.BitsPerSample/8)]
|
|
|
|
framesNDecodedSamples += ffo.Samples
|
|
|
|
}
|
|
|
|
|
2021-09-14 19:54:59 +03:00
|
|
|
decode.MustCopy(d, md5Samples, bytes.NewReader(frameStreamSamplesBuf))
|
2020-06-08 03:29:51 +03:00
|
|
|
streamDecodedSamples += ffo.Samples
|
|
|
|
|
|
|
|
// reuse buffer if possible
|
|
|
|
flacFrameIn.SamplesBuf = ffo.SamplesBuf
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
_ = streamInfo
|
|
|
|
// if streamInfo.D != nil {
|
|
|
|
// md5Value := streamInfo.D.FieldGet("md5")
|
|
|
|
// d.FieldChecksumRange("md5_calculated", md5Value.Range.Start, md5Value.Range.Len, md5Samples.Sum(nil), decode.BigEndian)
|
|
|
|
// }
|
|
|
|
d.FieldValueBytes("md5_calculated", md5Samples.Sum(nil), "")
|
|
|
|
d.FieldValueU("decoded_samples", framesNDecodedSamples, "")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|