mirror of
https://github.com/wader/fq.git
synced 2024-12-25 22:34:14 +03:00
1ddea1ada3
Move registry to interp and add support for functions and filesystems. This will be used later for allow formats to add own functions and fq code. Add gojqextra function helpers to have more comfortable API to add functions. Takes care of argument type casting and JQValue:s and some more things. Refactor interp package to use new function helper and registry. Probably fixes a bunch of JQValue bugs and other type errors. Refactor out some mpeg nal things to mpeg format. Refactor interp jq code into display.q and init.jq. Remove undocumented aes_ctr funciton, was a test. Hopefully will add more crypto things laster.
76 lines
1.6 KiB
Go
76 lines
1.6 KiB
Go
package webp
|
|
|
|
// https://developers.google.com/speed/webp/docs/riff_container
|
|
|
|
import (
|
|
"bytes"
|
|
|
|
"github.com/wader/fq/format"
|
|
"github.com/wader/fq/pkg/decode"
|
|
"github.com/wader/fq/pkg/interp"
|
|
"github.com/wader/fq/pkg/scalar"
|
|
)
|
|
|
|
var vp8Frame decode.Group
|
|
|
|
func init() {
|
|
interp.RegisterFormat(decode.Format{
|
|
Name: format.WEBP,
|
|
Description: "WebP image",
|
|
Groups: []string{format.PROBE, format.IMAGE},
|
|
DecodeFn: webpDecode,
|
|
Dependencies: []decode.Dependency{
|
|
{Names: []string{format.VP8_FRAME}, Group: &vp8Frame},
|
|
},
|
|
})
|
|
}
|
|
|
|
func decodeChunk(d *decode.D, expectedChunkID string, fn func(d *decode.D)) bool {
|
|
trimChunkID := d.FieldUTF8("id", 4, scalar.ActualTrimSpace)
|
|
if expectedChunkID != "" && trimChunkID != expectedChunkID {
|
|
return false
|
|
}
|
|
chunkLen := int64(d.FieldU32("size"))
|
|
|
|
if fn != nil {
|
|
d.FramedFn(chunkLen*8, fn)
|
|
} else {
|
|
d.FieldRawLen("data", chunkLen*8)
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
func webpDecode(d *decode.D, in any) any {
|
|
d.Endian = decode.LittleEndian
|
|
|
|
d.FieldUTF8("riff_id", 4, d.AssertStr("RIFF"))
|
|
riffLength := d.FieldU32("riff_length")
|
|
d.FieldUTF8("webp_id", 4, d.AssertStr("WEBP"))
|
|
|
|
d.FramedFn(int64(riffLength-4)*8, func(d *decode.D) {
|
|
p := d.PeekBytes(4)
|
|
|
|
// TODO: VP8X
|
|
|
|
switch {
|
|
case bytes.Equal(p, []byte("VP8 ")):
|
|
d.FieldStruct("image", func(d *decode.D) {
|
|
decodeChunk(d, "VP8", func(d *decode.D) {
|
|
d.Format(vp8Frame, nil)
|
|
})
|
|
})
|
|
case bytes.Equal(p, []byte("VP8L")):
|
|
d.FieldStruct("image", func(d *decode.D) {
|
|
decodeChunk(d, "VP8L", func(d *decode.D) {
|
|
// TODO
|
|
})
|
|
})
|
|
default:
|
|
d.Fatalf("could not find VP8 or VP8L chunk")
|
|
}
|
|
})
|
|
|
|
return nil
|
|
}
|