Skip to content

Commit

Permalink
matroska: Add decode_samples option
Browse files Browse the repository at this point in the history
Also change the option help a bit, maybe medid and support is confusing, we
might evetually decode non-audio/video things.
  • Loading branch information
wader committed Feb 8, 2023
1 parent 5b3ed33 commit b60acec
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 9 deletions.
22 changes: 20 additions & 2 deletions doc/formats.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ Decode value as avc_au

|Name |Default|Description|
|- |- |-|
|`decode_samples`|true |Decode supported media samples|
|`decode_samples`|true |Decode samples|

### Examples

Expand Down Expand Up @@ -668,6 +668,24 @@ $ fq -d markdown '[.. | select(.type=="heading" and .level<=2)?.children[0]]' fi
```
## matroska

### Options

|Name |Default|Description|
|- |- |-|
|`decode_samples`|true |Decode samples|

### Examples

Decode file using matroska options
```
$ fq -d matroska -o decode_samples=true . file
```

Decode value as matroska
```
... | matroska({decode_samples:true})
```

### Lookup element using path

```sh
Expand Down Expand Up @@ -716,7 +734,7 @@ Decode value as mp3
|Name |Default|Description|
|- |- |-|
|`allow_truncated`|false |Allow box to be truncated|
|`decode_samples` |true |Decode supported media samples|
|`decode_samples` |true |Decode samples|

### Examples

Expand Down
8 changes: 6 additions & 2 deletions format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,10 @@ type ProtoBufIn struct {
Message ProtoBufMessage
}

type MatroskaIn struct {
DecodeSamples bool `doc:"Decode samples"`
}

type MpegDecoderConfig struct {
ObjectType int
ASCObjectType int
Expand Down Expand Up @@ -296,12 +300,12 @@ func (t TCPStreamIn) MustIsPort(fn func(format string, a ...any), ports ...int)
}

type Mp4In struct {
DecodeSamples bool `doc:"Decode supported media samples"`
DecodeSamples bool `doc:"Decode samples"`
AllowTruncated bool `doc:"Allow box to be truncated"`
}

type AviIn struct {
DecodeSamples bool `doc:"Decode supported media samples"`
DecodeSamples bool `doc:"Decode samples"`
}

type ZipIn struct {
Expand Down
15 changes: 12 additions & 3 deletions format/matroska/matroska.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ func init() {
Description: "Matroska file",
Groups: []string{format.PROBE},
DecodeFn: matroskaDecode,
DecodeInArg: format.MatroskaIn{
DecodeSamples: true,
},
Dependencies: []decode.Dependency{
{Names: []string{format.AAC_FRAME}, Group: &aacFrameFormat},
{Names: []string{format.AV1_CCR}, Group: &av1CCRFormat},
Expand Down Expand Up @@ -412,7 +415,9 @@ func decodeMaster(d *decode.D, bitsLimit int64, tag ebml.Tag, dc *decodeContext)

}

func matroskaDecode(d *decode.D, _ any) any {
func matroskaDecode(d *decode.D, in any) any {
mi, _ := in.(format.MatroskaIn)

ebmlHeaderID := uint64(0x1a45dfa3)
if d.PeekBits(32) != ebmlHeaderID {
d.Errorf("no EBML header found")
Expand All @@ -437,7 +442,11 @@ func matroskaDecode(d *decode.D, _ any) any {
case "A_VORBIS":
t.parentD.RangeFn(t.codecPrivatePos, t.codecPrivateTagSize, func(d *decode.D) {
decodeLacingFn(d, lacingTypeXiph, func(d *decode.D) {
d.FieldFormat("packet", vorbisPacketFormat, nil)
if mi.DecodeSamples {
d.FieldFormat("packet", vorbisPacketFormat, nil)
} else {
d.FieldRawLen("packet", d.BitsLeft())
}
})
})
case "A_AAC":
Expand Down Expand Up @@ -519,7 +528,7 @@ func matroskaDecode(d *decode.D, _ any) any {
}

decodeLacingFn(d, int(lacing), func(d *decode.D) {
if f != nil {
if mi.DecodeSamples && f != nil {
d.FieldFormat("packet", *f, track.formatInArg)
} else {
d.FieldRawLen("packet", d.BitsLeft())
Expand Down
56 changes: 56 additions & 0 deletions format/matroska/testdata/decode_samples.fqtest
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
$ fq -o decode_samples=true 'first(grep_by(.id=="simple_block")) | dv' aac.mkv
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|.elements[1].elements[5].elements[2]{}: element 0x227-0x2fa.7 (212)
0x220| a3 | . | id: "simple_block" (0xa3) (Similar to Block, see (#block-structure), but without all the extra information, mostly used to reduced overhead when no extra feature is needed; see (#simpleblock-structure) on SimpleBlock Structure.) 0x227-0x227.7 (1)
| | | type: "binary" 0x228-NA (0)
0x220| 40 d1 | @. | size: 209 0x228-0x229.7 (2)
0x220| 81 | . | track_number: 1 0x22a-0x22a.7 (1)
0x220| 00 00 | .. | timestamp: 0 0x22b-0x22c.7 (2)
| | | flags{}: 0x22d-0x22d.7 (1)
0x220| 80 | . | key_frame: true 0x22d-0x22d (0.1)
0x220| 80 | . | reserved: 0 0x22d.1-0x22d.3 (0.3)
0x220| 80 | . | invisible: false 0x22d.4-0x22d.4 (0.1)
0x220| 80 | . | lacing: "none" (0) 0x22d.5-0x22d.6 (0.2)
0x220| 80 | . | discardable: false 0x22d.7-0x22d.7 (0.1)
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef| packet[0:4]: (aac_frame) 0x22e-0x2fa.7 (205)
| | | [0]{}: element 0x22e-0x23e.6 (16.7)
0x220| de | . | syntax_element: "FIL" (6) 0x22e-0x22e.2 (0.3)
| | | cnt{}: 0x22e.3-0x22f.6 (1.4)
0x220| de | . | count: 15 0x22e.3-0x22e.6 (0.4)
0x220| de 02| ..| esc_count: 1 0x22e.7-0x22f.6 (1)
| | | payload_length: 15 0x22f.7-NA (0)
| | | extension_payload{}: 0x22f.7-0x23e.6 (15)
0x220| 02| .| extension_type: "EXT_FILL" (0) 0x22f.7-0x230.2 (0.4)
0x230|00 |. |
0x230|00 |. | fill_nibble: 0 0x230.3-0x230.6 (0.4)
0x230|00 4c 61 76 63 35 38 2e 39 31 2e 31 30 30 00 |.Lavc58.91.100. | fill_byte: raw bits 0x230.7-0x23e.6 (14)
| | | [1]{}: element 0x23e.7-0x242 (3.2)
0x230| 00 02| ..| syntax_element: "SCE" (0) 0x23e.7-0x23f.1 (0.3)
0x230| 02| .| element_instance_tag: 0 0x23f.2-0x23f.5 (0.4)
0x230| 02| .| global_gain: 151 0x23f.6-0x240.5 (1)
0x240|5c |\ |
| | | ics_info{}: 0x240.6-0x242 (1.3)
0x240|5c |\ | ics_reserved_bit: 0 0x240.6-0x240.6 (0.1)
0x240|5c ab |\. | window_sequence: "LONG_START_SEQUENCE" (1) 0x240.7-0x241 (0.2)
0x240| ab | . | window_shape: 0 0x241.1-0x241.1 (0.1)
0x240| ab | . | max_sfb: 43 0x241.2-0x241.7 (0.6)
0x240| 59 | Y | predictor_data_present: false 0x242-0x242 (0.1)
0x240| 59 | Y | [2]: raw bits byte_align 0x242.1-0x242.7 (0.7)
0x240| a9 8c 72 50 8b 4c aa de 1d 71 72 5c 88| ..rP.L...qr\.| [3]: raw bits data 0x243-0x2fa.7 (184)
0x250|42 08 10 0e 80 0c d5 9f 71 6c 47 12 cb b6 16 71|B.......qlG....q|
* |until 0x2fa.7 (184) | |
$ fq -o decode_samples=false 'first(grep_by(.id=="simple_block")) | dv' aac.mkv
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|.elements[1].elements[5].elements[2]{}: element 0x227-0x2fa.7 (212)
0x220| a3 | . | id: "simple_block" (0xa3) (Similar to Block, see (#block-structure), but without all the extra information, mostly used to reduced overhead when no extra feature is needed; see (#simpleblock-structure) on SimpleBlock Structure.) 0x227-0x227.7 (1)
| | | type: "binary" 0x228-NA (0)
0x220| 40 d1 | @. | size: 209 0x228-0x229.7 (2)
0x220| 81 | . | track_number: 1 0x22a-0x22a.7 (1)
0x220| 00 00 | .. | timestamp: 0 0x22b-0x22c.7 (2)
| | | flags{}: 0x22d-0x22d.7 (1)
0x220| 80 | . | key_frame: true 0x22d-0x22d (0.1)
0x220| 80 | . | reserved: 0 0x22d.1-0x22d.3 (0.3)
0x220| 80 | . | invisible: false 0x22d.4-0x22d.4 (0.1)
0x220| 80 | . | lacing: "none" (0) 0x22d.5-0x22d.6 (0.2)
0x220| 80 | . | discardable: false 0x22d.7-0x22d.7 (0.1)
0x220| de 02| ..| packet: raw bits 0x22e-0x2fa.7 (205)
0x230|00 4c 61 76 63 35 38 2e 39 31 2e 31 30 30 00 02|.Lavc58.91.100..|
* |until 0x2fa.7 (205) | |
9 changes: 9 additions & 0 deletions format/matroska/testdata/help_matroska.fqtest
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
$ fq -h matroska
matroska: Matroska file decoder

Options
=======

decode_samples=true Decode samples

Decode examples
===============

# Decode file as matroska
$ fq -d matroska . file
# Decode value as matroska
... | matroska
# Decode file using matroska options
$ fq -d matroska -o decode_samples=true . file
# Decode value as matroska
... | matroska({decode_samples:true})

Lookup element using path
=========================
Expand Down
2 changes: 1 addition & 1 deletion format/mp4/testdata/help_mp4.fqtest
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Options
=======

allow_truncated=false Allow box to be truncated
decode_samples=true Decode supported media samples
decode_samples=true Decode samples

Decode examples
===============
Expand Down
2 changes: 1 addition & 1 deletion format/riff/testdata/help_avi.fqtest
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ avi: Audio Video Interleaved decoder
Options
=======

decode_samples=true Decode supported media samples
decode_samples=true Decode samples

Decode examples
===============
Expand Down

0 comments on commit b60acec

Please sign in to comment.