2020-06-08 03:29:51 +03:00
|
|
|
package flac
|
|
|
|
|
|
|
|
// https://xiph.org/flac/format.html
|
2021-10-19 19:05:22 +03:00
|
|
|
// https://wiki.hydrogenaud.io/index.php?title=FLAC_decoder_testbench
|
|
|
|
// TODO:
|
|
|
|
// 16 - Part 6 of Ladybug Castle (partition order 8 with escape codes)
|
|
|
|
// 32 - Part 5 of The Four of Us Are Dying (partition order 8 with escape codes)
|
2020-06-08 03:29:51 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/md5"
|
|
|
|
"fmt"
|
2021-08-17 13:06:32 +03:00
|
|
|
|
|
|
|
"github.com/wader/fq/format"
|
|
|
|
"github.com/wader/fq/format/registry"
|
2022-01-15 20:55:57 +03:00
|
|
|
"github.com/wader/fq/internal/mathextra"
|
2021-11-24 14:29:45 +03:00
|
|
|
"github.com/wader/fq/pkg/bitio"
|
2021-08-17 13:06:32 +03:00
|
|
|
"github.com/wader/fq/pkg/decode"
|
2021-12-02 00:48:25 +03:00
|
|
|
"github.com/wader/fq/pkg/scalar"
|
2020-06-08 03:29:51 +03:00
|
|
|
)
|
|
|
|
|
2021-11-24 23:20:46 +03:00
|
|
|
var flacMetadatablocksFormat decode.Group
|
2021-11-17 18:46:10 +03:00
|
|
|
var flacFrameFormat decode.Group
|
2020-06-08 03:29:51 +03:00
|
|
|
|
|
|
|
func init() {
|
2021-11-17 18:46:10 +03:00
|
|
|
registry.MustRegister(decode.Format{
|
2020-06-08 03:29:51 +03:00
|
|
|
Name: format.FLAC,
|
|
|
|
Description: "Free Lossless Audio Codec file",
|
|
|
|
Groups: []string{format.PROBE},
|
|
|
|
DecodeFn: flacDecode,
|
|
|
|
Dependencies: []decode.Dependency{
|
2021-11-24 23:20:46 +03:00
|
|
|
{Names: []string{format.FLAC_METADATABLOCKS}, Group: &flacMetadatablocksFormat},
|
2021-11-17 18:46:10 +03:00
|
|
|
{Names: []string{format.FLAC_FRAME}, Group: &flacFrameFormat},
|
2020-06-08 03:29:51 +03:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func flacDecode(d *decode.D, in interface{}) interface{} {
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldUTF8("magic", 4, d.AssertStr("fLaC"))
|
2020-06-08 03:29:51 +03:00
|
|
|
|
2021-10-28 02:44:09 +03:00
|
|
|
var streamInfo format.FlacStreamInfo
|
2020-06-08 03:29:51 +03:00
|
|
|
var flacFrameIn format.FlacFrameIn
|
|
|
|
var framesNDecodedSamples uint64
|
|
|
|
var streamTotalSamples uint64
|
|
|
|
var streamDecodedSamples uint64
|
|
|
|
|
2021-11-24 23:20:46 +03:00
|
|
|
_, v := d.FieldFormat("metadatablocks", flacMetadatablocksFormat, nil)
|
2021-09-17 16:46:13 +03:00
|
|
|
flacMetadatablockOut, ok := v.(format.FlacMetadatablocksOut)
|
|
|
|
if !ok {
|
|
|
|
panic(fmt.Sprintf("expected FlacMetadatablockOut got %#+v", v))
|
|
|
|
}
|
|
|
|
if flacMetadatablockOut.HasStreamInfo {
|
|
|
|
streamInfo = flacMetadatablockOut.StreamInfo
|
|
|
|
streamTotalSamples = streamInfo.TotalSamplesInStream
|
|
|
|
flacFrameIn = format.FlacFrameIn{StreamInfo: streamInfo}
|
|
|
|
}
|
2020-06-08 03:29:51 +03:00
|
|
|
|
|
|
|
md5Samples := md5.New()
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldArray("frames", func(d *decode.D) {
|
2020-06-08 03:29:51 +03:00
|
|
|
for d.NotEnd() {
|
|
|
|
// flac frame might need some fields from stream info to decode
|
2021-09-16 16:29:11 +03:00
|
|
|
_, v := d.FieldFormat("frame", flacFrameFormat, flacFrameIn)
|
2020-06-08 03:29:51 +03:00
|
|
|
ffo, ok := v.(format.FlacFrameOut)
|
|
|
|
if !ok {
|
|
|
|
panic(fmt.Sprintf("expected FlacFrameOut got %#+v", v))
|
|
|
|
}
|
|
|
|
|
2021-10-19 19:05:22 +03:00
|
|
|
samplesInFrame := ffo.Samples
|
2020-06-08 03:29:51 +03:00
|
|
|
if streamTotalSamples > 0 {
|
2022-01-15 20:55:57 +03:00
|
|
|
samplesInFrame = mathextra.MinUInt64(streamTotalSamples-streamDecodedSamples, ffo.Samples)
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
2021-10-19 19:05:22 +03:00
|
|
|
frameStreamSamplesBuf := ffo.SamplesBuf[0 : samplesInFrame*uint64(ffo.Channels*ffo.BitsPerSample/8)]
|
|
|
|
framesNDecodedSamples += ffo.Samples
|
2020-06-08 03:29:51 +03:00
|
|
|
|
2021-11-18 03:17:41 +03:00
|
|
|
d.MustCopy(md5Samples, bytes.NewReader(frameStreamSamplesBuf))
|
2020-06-08 03:29:51 +03:00
|
|
|
streamDecodedSamples += ffo.Samples
|
|
|
|
|
|
|
|
// reuse buffer if possible
|
|
|
|
flacFrameIn.SamplesBuf = ffo.SamplesBuf
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-11-24 14:29:45 +03:00
|
|
|
md5CalcValue := d.FieldRootBitBuf("md5_calculated", bitio.NewBufferFromBytes(md5Samples.Sum(nil), -1))
|
2021-12-02 00:48:25 +03:00
|
|
|
_ = md5CalcValue.TryScalarFn(d.ValidateBitBuf(streamInfo.MD5), scalar.RawHex)
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldValueU("decoded_samples", framesNDecodedSamples)
|
2020-06-08 03:29:51 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|