Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

flac: Add option to see sample details like residuals and verbatim va… #802

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions doc/formats.md
Original file line number Diff line number Diff line change
Expand Up @@ -574,17 +574,18 @@ $ fq -d csv '.[0] as $t | .[1:] | map(with_entries(.key = $t[.key]))' file.csv
|Name |Default|Description|
|- |- |-|
|`bits_per_sample`|16 |Bits per sample|
|`sample_details` |false |Decode more sample details like residuals etc|

### Examples

Decode file using flac_frame options
```
$ fq -d flac_frame -o bits_per_sample=16 . file
$ fq -d flac_frame -o bits_per_sample=16 -o sample_details=false . file
```

Decode value as flac_frame
```
... | flac_frame({bits_per_sample:16})
... | flac_frame({bits_per_sample:16,sample_details:false})
```

## hevc_au
Expand Down
68 changes: 55 additions & 13 deletions format/flac/flac_frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func init() {
DecodeFn: frameDecode,
DefaultInArg: format.FLAC_Frame_In{
BitsPerSample: 16, // TODO: maybe should not have a default value?
SampleDetails: false,
},
})
}
Expand Down Expand Up @@ -466,25 +467,48 @@ func frameDecode(d *decode.D) any {
n += count
} else {
d.RangeFn(d.Pos(), int64(count*escapeSampleSize), func(d *decode.D) {
d.FieldRawLen("samples", int64(count*escapeSampleSize))
if ffi.SampleDetails {
d.FieldArray("residuals", func(d *decode.D) {
for i := 0; i < count; i++ {
d.FieldS("residual", escapeSampleSize)
}
})
} else {
d.FieldRawLen("residuals", int64(count*escapeSampleSize))
}
})
for j := 0; j < count; j++ {
samples[n] = d.S(escapeSampleSize)
n++
}
}
} else {
samplesStart := d.Pos()
for j := 0; j < count; j++ {
high := d.Unary(0)
low := d.U(riceParameter)
samples[n] = mathex.ZigZag[uint64, int64](high<<riceParameter | low)
n++
if ffi.SampleDetails {
d.FieldArray("residuals", func(d *decode.D) {
for i := 0; i < count; i++ {
d.FieldStruct("residual", func(d *decode.D) {
high := d.FieldUnary("high", 0)
low := d.FieldU("low", riceParameter)
residual := mathex.ZigZag[uint64, int64](high<<riceParameter | low)
d.FieldValueSint("value", residual)
samples[n] = residual
})
n++
}
})
} else {
samplesStart := d.Pos()
for j := 0; j < count; j++ {
high := d.Unary(0)
low := d.U(riceParameter)
samples[n] = mathex.ZigZag[uint64, int64](high<<riceParameter | low)
n++
}
samplesStop := d.Pos()
d.RangeFn(samplesStart, samplesStop-samplesStart, func(d *decode.D) {
d.FieldRawLen("residuals", d.BitsLeft())
})
}
samplesStop := d.Pos()
d.RangeFn(samplesStart, samplesStop-samplesStart, func(d *decode.D) {
d.FieldRawLen("samples", d.BitsLeft())
})
}
})
}
Expand All @@ -505,7 +529,8 @@ func frameDecode(d *decode.D) any {
}

samples := make([]int64, blockSize)
switch subframeTypeUint.SymStr() {
subframeType := subframeTypeUint.SymStr()
switch subframeType {
case SubframeConstant:
// <n> Unencoded constant value of the subblock, n = frame's bits-per-sample.
v := d.FieldS("value", subframeSampleSize)
Expand All @@ -516,7 +541,15 @@ func frameDecode(d *decode.D) any {
// <n*i> Unencoded subblock; n = frame's bits-per-sample, i = frame's blocksize.
// TODO: refactor into some kind of FieldBitBufLenFn?
d.RangeFn(d.Pos(), int64(blockSize*subframeSampleSize), func(d *decode.D) {
d.FieldRawLen("samples", d.BitsLeft())
if ffi.SampleDetails {
d.FieldArray("samples", func(d *decode.D) {
for i := 0; i < blockSize; i++ {
d.FieldS("sample", subframeSampleSize)
}
})
} else {
d.FieldRawLen("samples", d.BitsLeft())
}
})

for i := 0; i < blockSize; i++ {
Expand Down Expand Up @@ -565,6 +598,15 @@ func frameDecode(d *decode.D) any {
}
}

// for verbatim we already added samples above
if ffi.SampleDetails && subframeType != SubframeVerbatim {
d.FieldArray("samples", func(d *decode.D) {
for i := 0; i < len(samples); i++ {
d.FieldValueSint("sample", samples[i])
}
})
}

channelSamples = append(channelSamples, samples)
})
}
Expand Down
4 changes: 2 additions & 2 deletions format/flac/testdata/frame.fqtest
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ $ fq '.frames[1] | tobytes | flac_frame | d' mono8.flac
| | | [0]{}: partition
| | | count: 2047
0x000| 04 3f | .? | rice_parameter: 0
0x000| 3f ff ff ff ff ff ff| ?......| samples: raw bits
0x000| 3f ff ff ff ff ff ff| ?......| residuals: raw bits
0x010|ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff|................|
* |until 0x196.1 (397) | |
| | | [1]{}: partition
| | | count: 2048
0x190| c1 | . | rice_parameter: 0
0x190| c1 bb ff 39 99 32 4c 92 64 99| ...9.2L.d.| samples: raw bits
0x190| c1 bb ff 39 99 32 4c 92 64 99| ...9.2L.d.| residuals: raw bits
0x1a0|33 39 ff bb 6d 6d b6 df fe 66 66 4c 93 26 4c 99|39..mm...ffL.&L.|
* |until 0x33d.7 (424) | |
| | | byte_align: 0 (valid)
Expand Down
7 changes: 4 additions & 3 deletions format/flac/testdata/help_flac_frame.fqtest
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ flac_frame: FLAC frame decoder
Options
=======

bits_per_sample=16 Bits per sample
bits_per_sample=16 Bits per sample
sample_details=false Decode more sample details like residuals etc

Decode examples
===============
Expand All @@ -14,7 +15,7 @@ Decode examples
# Decode value as flac_frame
... | flac_frame
# Decode file using flac_frame options
$ fq -d flac_frame -o bits_per_sample=16 . file
$ fq -d flac_frame -o bits_per_sample=16 -o sample_details=false . file
# Decode value as flac_frame
... | flac_frame({bits_per_sample:16})
... | flac_frame({bits_per_sample:16,sample_details:false})

Loading