frame: Fix decoding of FLAC files with wasted bits-per-sample.
Fixes issue #12.
This commit is contained in:
parent
be533874f4
commit
154b8f6f7f
|
@ -26,6 +26,11 @@ var golden = []struct {
|
||||||
{
|
{
|
||||||
name: "../testdata/189983.flac",
|
name: "../testdata/189983.flac",
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// i=3
|
||||||
|
{
|
||||||
|
name: "../testdata/love.flac",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFrameHash(t *testing.T) {
|
func TestFrameHash(t *testing.T) {
|
||||||
|
|
|
@ -35,6 +35,8 @@ func (frame *Frame) parseSubframe(bps uint) (subframe *Subframe, err error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return subframe, err
|
return subframe, err
|
||||||
}
|
}
|
||||||
|
// Adjust bps of subframe for wasted bits-per-sample.
|
||||||
|
bps -= subframe.Wasted
|
||||||
|
|
||||||
// Decode subframe audio samples.
|
// Decode subframe audio samples.
|
||||||
subframe.NSamples = int(frame.BlockSize)
|
subframe.NSamples = int(frame.BlockSize)
|
||||||
|
@ -52,8 +54,7 @@ func (frame *Frame) parseSubframe(bps uint) (subframe *Subframe, err error) {
|
||||||
|
|
||||||
// Left shift to accout for wasted bits-per-sample.
|
// Left shift to accout for wasted bits-per-sample.
|
||||||
for i, sample := range subframe.Samples {
|
for i, sample := range subframe.Samples {
|
||||||
// TODO: Verify after a FLAC file with wasted bits-per-sample has been found.
|
subframe.Samples[i] = sample << subframe.Wasted
|
||||||
subframe.Samples[i] = sample << subframe.WastedBits
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return subframe, err
|
return subframe, err
|
||||||
|
@ -69,7 +70,7 @@ type SubHeader struct {
|
||||||
// Prediction order used by fixed and FIR linear prediction decoding.
|
// Prediction order used by fixed and FIR linear prediction decoding.
|
||||||
Order int
|
Order int
|
||||||
// Wasted bits-per-sample.
|
// Wasted bits-per-sample.
|
||||||
WastedBits uint
|
Wasted uint
|
||||||
}
|
}
|
||||||
|
|
||||||
// parseHeader reads and parses the header of a subframe.
|
// parseHeader reads and parses the header of a subframe.
|
||||||
|
@ -138,14 +139,13 @@ func (subframe *Subframe) parseHeader(br *bits.Reader) error {
|
||||||
return unexpected(err)
|
return unexpected(err)
|
||||||
}
|
}
|
||||||
if x != 0 {
|
if x != 0 {
|
||||||
// The number of wasted bits-per-sample is unary coded.
|
// k wasted bits-per-sample in source subblock, k-1 follows, unary coded;
|
||||||
|
// e.g. k=3 => 001 follows, k=7 => 0000001 follows.
|
||||||
x, err = br.ReadUnary()
|
x, err = br.ReadUnary()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return unexpected(err)
|
return unexpected(err)
|
||||||
}
|
}
|
||||||
subframe.WastedBits = uint(x)
|
subframe.Wasted = uint(x) + 1
|
||||||
log.Printf("frame.Subframe.parseHeader: Never seen a FLAC file contain wasted-bits-per-sample before. I want to dissect one of those files. Please send it to me :)")
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
11
testdata/README.md
vendored
11
testdata/README.md
vendored
|
@ -1,15 +1,14 @@
|
||||||
public domain
|
## Public domain
|
||||||
-------------
|
|
||||||
|
|
||||||
The testcase sounds have all been released into the *[public domain][]*.
|
The testcase sounds have all been released into the [public domain].
|
||||||
|
|
||||||
[public domain]: https://creativecommons.org/publicdomain/zero/1.0/
|
[public domain]: https://creativecommons.org/publicdomain/zero/1.0/
|
||||||
|
|
||||||
The source of each sound is listed below:
|
The source of each sound is listed below:
|
||||||
|
|
||||||
* [59996.flac][]
|
* [59996.flac]
|
||||||
* [172960.flac][]
|
* [172960.flac]
|
||||||
* [189983.flac][]
|
* [189983.flac]
|
||||||
|
|
||||||
[59996.flac]: http://www.freesound.org/people/qubodup/sounds/59996/
|
[59996.flac]: http://www.freesound.org/people/qubodup/sounds/59996/
|
||||||
[172960.flac]: http://www.freesound.org/people/qubodup/sounds/172960/
|
[172960.flac]: http://www.freesound.org/people/qubodup/sounds/172960/
|
||||||
|
|
BIN
testdata/love.flac
vendored
Normal file
BIN
testdata/love.flac
vendored
Normal file
Binary file not shown.
Loading…
Reference in a new issue