Skip to content

Commit

Permalink
flac: Add option to see sample details like residuals and verbatim va…
Browse files Browse the repository at this point in the history
…lues
  • Loading branch information
wader committed Oct 30, 2023
1 parent 7c8fa05 commit 5273c6f
Show file tree
Hide file tree
Showing 19 changed files with 1,027 additions and 984 deletions.
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
64 changes: 52 additions & 12 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 Down Expand Up @@ -516,7 +540,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 < subframeSampleSize; i++ {
d.FieldS("sample", subframeSampleSize)
}
})
} else {
d.FieldRawLen("samples", d.BitsLeft())
}
})

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

if ffi.SampleDetails {
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

0 comments on commit 5273c6f

Please sign in to comment.