diff --git a/doc/formats.md b/doc/formats.md index 35cf94ff..12740f09 100644 --- a/doc/formats.md +++ b/doc/formats.md @@ -2,7 +2,7 @@ [./formats_table.jq]: sh-start -|Name |Description |Uses| +|Name |Description |Dependencies| |- |- |-| |`aac_frame` |Advanced Audio Coding frame || |`adts` |Audio Data Transport Stream |`adts_frame`| diff --git a/doc/formats_table.jq b/doc/formats_table.jq index cc0f416b..fe416755 100755 --- a/doc/formats_table.jq +++ b/doc/formats_table.jq @@ -6,7 +6,7 @@ def nbsp: gsub(" "; " "); [ { name: "Name", desc: "Description", - uses: "Uses" + uses: "Dependencies" }, { name: "-", diff --git a/format/all/all.go b/format/all/all.go index 1c845339..306ed80f 100644 --- a/format/all/all.go +++ b/format/all/all.go @@ -1,4 +1,4 @@ -// Package all imports and registers all formats in the default registry +// Package all registers all builtin formats with the default registry //nolint:revive package all diff --git a/format/ape/apev2.go b/format/ape/apev2.go index 41bd1fd9..20ad480a 100644 --- a/format/ape/apev2.go +++ b/format/ape/apev2.go @@ -8,15 +8,15 @@ import ( "github.com/wader/fq/pkg/decode" ) -var imageFormat []*decode.Format +var imageFormat decode.Group func init() { - registry.MustRegister(&decode.Format{ + registry.MustRegister(decode.Format{ Name: format.APEV2, Description: "APEv2 metadata tag", DecodeFn: apev2Decode, Dependencies: []decode.Dependency{ - {Names: []string{format.IMAGE}, Formats: &imageFormat}, + {Names: []string{format.IMAGE}, Group: &imageFormat}, }, }) } diff --git a/format/av1/av1_ccr.go b/format/av1/av1_ccr.go index 6f0389a4..fb3da8ca 100644 --- a/format/av1/av1_ccr.go +++ b/format/av1/av1_ccr.go @@ -11,7 +11,7 @@ import ( ) func init() { - registry.MustRegister(&decode.Format{ + registry.MustRegister(decode.Format{ Name: format.AV1_CCR, Description: "AV1 Codec Configuration Record", DecodeFn: ccrDecode, diff --git a/format/av1/av1_frame.go b/format/av1/av1_frame.go index b6b7163f..6582e359 100644 --- a/format/av1/av1_frame.go +++ b/format/av1/av1_frame.go @@ -10,17 +10,17 @@ import ( "github.com/wader/fq/pkg/decode" ) -var obuFormat []*decode.Format +var obuFormat decode.Group func init() { - registry.MustRegister(&decode.Format{ + registry.MustRegister(decode.Format{ Name: format.AV1_FRAME, Description: "AV1 frame", DecodeFn: frameDecode, RootArray: true, RootName: "frame", Dependencies: []decode.Dependency{ - {Names: []string{format.AV1_OBU}, Formats: &obuFormat}, + {Names: []string{format.AV1_OBU}, Group: &obuFormat}, }, }) } diff --git a/format/av1/av1_obu.go b/format/av1/av1_obu.go index f2303e55..f38f9938 100644 --- a/format/av1/av1_obu.go +++ b/format/av1/av1_obu.go @@ -7,7 +7,7 @@ import ( ) func init() { - registry.MustRegister(&decode.Format{ + registry.MustRegister(decode.Format{ Name: format.AV1_OBU, Description: "AV1 Open Bitstream Unit", DecodeFn: obuDecode, diff --git a/format/bzip2/bzip2.go b/format/bzip2/bzip2.go index 3886e28f..7eccfcb6 100644 --- a/format/bzip2/bzip2.go +++ b/format/bzip2/bzip2.go @@ -17,16 +17,16 @@ import ( "github.com/wader/fq/pkg/decode" ) -var probeFormat []*decode.Format +var probeGroup decode.Group func init() { - registry.MustRegister(&decode.Format{ + registry.MustRegister(decode.Format{ Name: format.BZIP2, Description: "bzip2 compression", Groups: []string{format.PROBE}, DecodeFn: gzDecode, Dependencies: []decode.Dependency{ - {Names: []string{format.PROBE}, Formats: &probeFormat}, + {Names: []string{format.PROBE}, Group: &probeGroup}, }, }) } @@ -96,7 +96,7 @@ func gzDecode(d *decode.D, in interface{}) interface{} { } // calculatedCRC32 := crc32W.Sum(nil) uncompressedBB := bitio.NewBufferFromBytes(uncompressed.Bytes(), -1) - dv, _, _ := d.FieldTryFormatBitBuf("uncompressed", uncompressedBB, probeFormat, nil) + dv, _, _ := d.FieldTryFormatBitBuf("uncompressed", uncompressedBB, probeGroup, nil) if dv == nil { d.FieldRootBitBuf("uncompressed", uncompressedBB) } diff --git a/format/dns/dns.go b/format/dns/dns.go index 7cfbf663..6a8174b3 100644 --- a/format/dns/dns.go +++ b/format/dns/dns.go @@ -13,7 +13,7 @@ import ( ) func init() { - registry.MustRegister(&decode.Format{ + registry.MustRegister(decode.Format{ Name: format.DNS, Description: "DNS packet", DecodeFn: dnsDecode, diff --git a/format/elf/elf.go b/format/elf/elf.go index 3d0989c7..90aa44fb 100644 --- a/format/elf/elf.go +++ b/format/elf/elf.go @@ -15,7 +15,7 @@ import ( // TODO: p_type hi/lo func init() { - registry.MustRegister(&decode.Format{ + registry.MustRegister(decode.Format{ Name: format.ELF, Description: "Executable and Linkable Format", Groups: []string{format.PROBE}, diff --git a/format/flac/flac.go b/format/flac/flac.go index b7a72de3..2a5b7150 100644 --- a/format/flac/flac.go +++ b/format/flac/flac.go @@ -17,18 +17,18 @@ import ( "github.com/wader/fq/pkg/decode" ) -var flacMetadatablockFormat []*decode.Format -var flacFrameFormat []*decode.Format +var flacMetadatablockFormat decode.Group +var flacFrameFormat decode.Group func init() { - registry.MustRegister(&decode.Format{ + registry.MustRegister(decode.Format{ Name: format.FLAC, Description: "Free Lossless Audio Codec file", Groups: []string{format.PROBE}, DecodeFn: flacDecode, Dependencies: []decode.Dependency{ - {Names: []string{format.FLAC_METADATABLOCKS}, Formats: &flacMetadatablockFormat}, - {Names: []string{format.FLAC_FRAME}, Formats: &flacFrameFormat}, + {Names: []string{format.FLAC_METADATABLOCKS}, Group: &flacMetadatablockFormat}, + {Names: []string{format.FLAC_FRAME}, Group: &flacFrameFormat}, }, }) } diff --git a/format/flac/flac_frame.go b/format/flac/flac_frame.go index a8e29eef..8c0b248a 100644 --- a/format/flac/flac_frame.go +++ b/format/flac/flac_frame.go @@ -12,7 +12,7 @@ import ( ) func init() { - registry.MustRegister(&decode.Format{ + registry.MustRegister(decode.Format{ Name: format.FLAC_FRAME, Description: "FLAC frame", DecodeFn: frameDecode, diff --git a/format/flac/flac_metadatablock.go b/format/flac/flac_metadatablock.go index 38ade798..5327a3d7 100644 --- a/format/flac/flac_metadatablock.go +++ b/format/flac/flac_metadatablock.go @@ -11,19 +11,19 @@ import ( "github.com/wader/fq/pkg/decode" ) -var flacStreaminfoFormat []*decode.Format -var flacPicture []*decode.Format -var vorbisCommentFormat []*decode.Format +var flacStreaminfoFormat decode.Group +var flacPicture decode.Group +var vorbisCommentFormat decode.Group func init() { - registry.MustRegister(&decode.Format{ + registry.MustRegister(decode.Format{ Name: format.FLAC_METADATABLOCK, Description: "FLAC metadatablock", DecodeFn: metadatablockDecode, Dependencies: []decode.Dependency{ - {Names: []string{format.FLAC_STREAMINFO}, Formats: &flacStreaminfoFormat}, - {Names: []string{format.FLAC_PICTURE}, Formats: &flacPicture}, - {Names: []string{format.VORBIS_COMMENT}, Formats: &vorbisCommentFormat}, + {Names: []string{format.FLAC_STREAMINFO}, Group: &flacStreaminfoFormat}, + {Names: []string{format.FLAC_PICTURE}, Group: &flacPicture}, + {Names: []string{format.VORBIS_COMMENT}, Group: &vorbisCommentFormat}, }, }) } diff --git a/format/flac/flac_metadatablocks.go b/format/flac/flac_metadatablocks.go index 4c74582e..156f3f76 100644 --- a/format/flac/flac_metadatablocks.go +++ b/format/flac/flac_metadatablocks.go @@ -8,17 +8,17 @@ import ( "github.com/wader/fq/pkg/decode" ) -var flacMetadatablockForamt []*decode.Format +var flacMetadatablockForamt decode.Group func init() { - registry.MustRegister(&decode.Format{ + registry.MustRegister(decode.Format{ Name: format.FLAC_METADATABLOCKS, Description: "FLAC metadatablocks", DecodeFn: metadatablocksDecode, RootArray: true, RootName: "metadatablocks", Dependencies: []decode.Dependency{ - {Names: []string{format.FLAC_METADATABLOCK}, Formats: &flacMetadatablockForamt}, + {Names: []string{format.FLAC_METADATABLOCK}, Group: &flacMetadatablockForamt}, }, }) } diff --git a/format/flac/flac_picture.go b/format/flac/flac_picture.go index 53e8cfc5..ed36ea66 100644 --- a/format/flac/flac_picture.go +++ b/format/flac/flac_picture.go @@ -6,7 +6,7 @@ import ( "github.com/wader/fq/pkg/decode" ) -var images []*decode.Format +var images decode.Group var pictureTypeNames = decode.UToStr{ 0: "Other", @@ -33,12 +33,12 @@ var pictureTypeNames = decode.UToStr{ } func init() { - registry.MustRegister(&decode.Format{ + registry.MustRegister(decode.Format{ Name: format.FLAC_PICTURE, Description: "FLAC metadatablock picture", DecodeFn: pictureDecode, Dependencies: []decode.Dependency{ - {Names: []string{format.IMAGE}, Formats: &images}, + {Names: []string{format.IMAGE}, Group: &images}, }, }) } diff --git a/format/flac/flac_streaminfo.go b/format/flac/flac_streaminfo.go index 206c97bf..7b6a9734 100644 --- a/format/flac/flac_streaminfo.go +++ b/format/flac/flac_streaminfo.go @@ -7,7 +7,7 @@ import ( ) func init() { - registry.MustRegister(&decode.Format{ + registry.MustRegister(decode.Format{ Name: format.FLAC_STREAMINFO, Description: "FLAC streaminfo", DecodeFn: streaminfoDecode, diff --git a/format/flv/flv.go b/format/flv/flv.go index e6429f25..f6c341f1 100644 --- a/format/flv/flv.go +++ b/format/flv/flv.go @@ -13,7 +13,7 @@ import ( ) func init() { - registry.MustRegister(&decode.Format{ + registry.MustRegister(decode.Format{ Name: format.FLV, Description: "Flash video", Groups: []string{format.PROBE}, diff --git a/format/gif/gif.go b/format/gif/gif.go index 2dae0889..e77671ce 100644 --- a/format/gif/gif.go +++ b/format/gif/gif.go @@ -16,7 +16,7 @@ import ( ) func init() { - registry.MustRegister(&decode.Format{ + registry.MustRegister(decode.Format{ Name: format.GIF, Description: "Graphics Interchange Format", Groups: []string{format.PROBE, format.IMAGE}, diff --git a/format/gzip/gzip.go b/format/gzip/gzip.go index 3dc66ef7..fd1e4cf3 100644 --- a/format/gzip/gzip.go +++ b/format/gzip/gzip.go @@ -16,16 +16,16 @@ import ( "github.com/wader/fq/pkg/decode" ) -var probeFormat []*decode.Format +var probeFormat decode.Group func init() { - registry.MustRegister(&decode.Format{ + registry.MustRegister(decode.Format{ Name: format.GZIP, Description: "gzip compression", Groups: []string{format.PROBE}, DecodeFn: gzDecode, Dependencies: []decode.Dependency{ - {Names: []string{format.PROBE}, Formats: &probeFormat}, + {Names: []string{format.PROBE}, Group: &probeFormat}, }, }) } diff --git a/format/icc/icc_profile.go b/format/icc/icc_profile.go index b04fecd2..4029c956 100644 --- a/format/icc/icc_profile.go +++ b/format/icc/icc_profile.go @@ -11,7 +11,7 @@ import ( ) func init() { - registry.MustRegister(&decode.Format{ + registry.MustRegister(decode.Format{ Name: format.ICC_PROFILE, Description: "International Color Consortium profile", DecodeFn: iccProfileDecode, diff --git a/format/id3/id3v1.go b/format/id3/id3v1.go index 88c79b2e..db40f217 100644 --- a/format/id3/id3v1.go +++ b/format/id3/id3v1.go @@ -9,7 +9,7 @@ import ( // TODO: comment 28 long, zero byte, track number func init() { - registry.MustRegister(&decode.Format{ + registry.MustRegister(decode.Format{ Name: format.ID3V1, Description: "ID3v1 metadata", DecodeFn: id3v1Decode, diff --git a/format/id3/id3v11.go b/format/id3/id3v11.go index 2b93dcb1..13a06ab0 100644 --- a/format/id3/id3v11.go +++ b/format/id3/id3v11.go @@ -7,7 +7,7 @@ import ( ) func init() { - registry.MustRegister(&decode.Format{ + registry.MustRegister(decode.Format{ Name: format.ID3V11, Description: "ID3v1.1 metadata", DecodeFn: id3v11Decode, diff --git a/format/id3/id3v2.go b/format/id3/id3v2.go index 79f25061..76892c0d 100644 --- a/format/id3/id3v2.go +++ b/format/id3/id3v2.go @@ -17,15 +17,15 @@ import ( "golang.org/x/text/encoding/unicode" ) -var imageFormat []*decode.Format +var imageFormat decode.Group func init() { - registry.MustRegister(&decode.Format{ + registry.MustRegister(decode.Format{ Name: format.ID3V2, Description: "ID3v2 metadata", DecodeFn: id3v2Decode, Dependencies: []decode.Dependency{ - {Names: []string{format.IMAGE}, Formats: &imageFormat}, + {Names: []string{format.IMAGE}, Group: &imageFormat}, }, }) } diff --git a/format/jpeg/jpeg.go b/format/jpeg/jpeg.go index c34cb98e..acb14d44 100644 --- a/format/jpeg/jpeg.go +++ b/format/jpeg/jpeg.go @@ -13,18 +13,18 @@ import ( "github.com/wader/fq/pkg/decode" ) -var exifFormat []*decode.Format -var iccProfileFormat []*decode.Format +var exifFormat decode.Group +var iccProfileFormat decode.Group func init() { - registry.MustRegister(&decode.Format{ + registry.MustRegister(decode.Format{ Name: format.JPEG, Description: "Joint Photographic Experts Group file", Groups: []string{format.PROBE, format.IMAGE}, DecodeFn: jpegDecode, Dependencies: []decode.Dependency{ - {Names: []string{format.EXIF}, Formats: &exifFormat}, - {Names: []string{format.ICC_PROFILE}, Formats: &iccProfileFormat}, + {Names: []string{format.EXIF}, Group: &exifFormat}, + {Names: []string{format.ICC_PROFILE}, Group: &iccProfileFormat}, }, }) } diff --git a/format/json/json.go b/format/json/json.go index e610509a..4b70c807 100644 --- a/format/json/json.go +++ b/format/json/json.go @@ -9,7 +9,7 @@ import ( ) func init() { - registry.MustRegister(&decode.Format{ + registry.MustRegister(decode.Format{ Name: format.JSON, Description: "JSON", ProbeOrder: 100, // last diff --git a/format/matroska/matroska.go b/format/matroska/matroska.go index 00e34370..63669869 100644 --- a/format/matroska/matroska.go +++ b/format/matroska/matroska.go @@ -28,59 +28,59 @@ import ( //go:embed *.jq var matroskaFS embed.FS -var aacFrameFormat []*decode.Format -var av1CCRFormat []*decode.Format -var av1FrameFormat []*decode.Format -var flacFrameFormat []*decode.Format -var flacMetadatablocksFormat []*decode.Format -var imageFormat []*decode.Format -var mp3FrameFormat []*decode.Format -var mpegASCFrameFormat []*decode.Format -var mpegAVCAUFormat []*decode.Format -var mpegAVCDCRFormat []*decode.Format -var mpegHEVCDCRFormat []*decode.Format -var mpegHEVCSampleFormat []*decode.Format -var mpegPESPacketSampleFormat []*decode.Format -var mpegSPUFrameFormat []*decode.Format -var opusPacketFrameFormat []*decode.Format -var vorbisPacketFormat []*decode.Format -var vp8FrameFormat []*decode.Format -var vp9CFMFormat []*decode.Format -var vp9FrameFormat []*decode.Format +var aacFrameFormat decode.Group +var av1CCRFormat decode.Group +var av1FrameFormat decode.Group +var flacFrameFormat decode.Group +var flacMetadatablocksFormat decode.Group +var imageFormat decode.Group +var mp3FrameFormat decode.Group +var mpegASCFrameFormat decode.Group +var mpegAVCAUFormat decode.Group +var mpegAVCDCRFormat decode.Group +var mpegHEVCDCRFormat decode.Group +var mpegHEVCSampleFormat decode.Group +var mpegPESPacketSampleFormat decode.Group +var mpegSPUFrameFormat decode.Group +var opusPacketFrameFormat decode.Group +var vorbisPacketFormat decode.Group +var vp8FrameFormat decode.Group +var vp9CFMFormat decode.Group +var vp9FrameFormat decode.Group -var codecToFormat map[string]*[]*decode.Format +var codecToFormat map[string]*decode.Group func init() { - registry.MustRegister(&decode.Format{ + registry.MustRegister(decode.Format{ Name: format.MATROSKA, Description: "Matroska file", Groups: []string{format.PROBE}, DecodeFn: matroskaDecode, Dependencies: []decode.Dependency{ - {Names: []string{format.AAC_FRAME}, Formats: &aacFrameFormat}, - {Names: []string{format.AV1_CCR}, Formats: &av1CCRFormat}, - {Names: []string{format.AV1_FRAME}, Formats: &av1FrameFormat}, - {Names: []string{format.AVC_AU}, Formats: &mpegAVCAUFormat}, - {Names: []string{format.AVC_DCR}, Formats: &mpegAVCDCRFormat}, - {Names: []string{format.FLAC_FRAME}, Formats: &flacFrameFormat}, - {Names: []string{format.FLAC_METADATABLOCKS}, Formats: &flacMetadatablocksFormat}, - {Names: []string{format.HEVC_AU}, Formats: &mpegHEVCSampleFormat}, - {Names: []string{format.HEVC_DCR}, Formats: &mpegHEVCDCRFormat}, - {Names: []string{format.IMAGE}, Formats: &imageFormat}, - {Names: []string{format.MP3_FRAME}, Formats: &mp3FrameFormat}, - {Names: []string{format.MPEG_ASC}, Formats: &mpegASCFrameFormat}, - {Names: []string{format.MPEG_PES_PACKET}, Formats: &mpegPESPacketSampleFormat}, - {Names: []string{format.MPEG_SPU}, Formats: &mpegSPUFrameFormat}, - {Names: []string{format.OPUS_PACKET}, Formats: &opusPacketFrameFormat}, - {Names: []string{format.VORBIS_PACKET}, Formats: &vorbisPacketFormat}, - {Names: []string{format.VP8_FRAME}, Formats: &vp8FrameFormat}, - {Names: []string{format.VP9_CFM}, Formats: &vp9CFMFormat}, - {Names: []string{format.VP9_FRAME}, Formats: &vp9FrameFormat}, + {Names: []string{format.AAC_FRAME}, Group: &aacFrameFormat}, + {Names: []string{format.AV1_CCR}, Group: &av1CCRFormat}, + {Names: []string{format.AV1_FRAME}, Group: &av1FrameFormat}, + {Names: []string{format.AVC_AU}, Group: &mpegAVCAUFormat}, + {Names: []string{format.AVC_DCR}, Group: &mpegAVCDCRFormat}, + {Names: []string{format.FLAC_FRAME}, Group: &flacFrameFormat}, + {Names: []string{format.FLAC_METADATABLOCKS}, Group: &flacMetadatablocksFormat}, + {Names: []string{format.HEVC_AU}, Group: &mpegHEVCSampleFormat}, + {Names: []string{format.HEVC_DCR}, Group: &mpegHEVCDCRFormat}, + {Names: []string{format.IMAGE}, Group: &imageFormat}, + {Names: []string{format.MP3_FRAME}, Group: &mp3FrameFormat}, + {Names: []string{format.MPEG_ASC}, Group: &mpegASCFrameFormat}, + {Names: []string{format.MPEG_PES_PACKET}, Group: &mpegPESPacketSampleFormat}, + {Names: []string{format.MPEG_SPU}, Group: &mpegSPUFrameFormat}, + {Names: []string{format.OPUS_PACKET}, Group: &opusPacketFrameFormat}, + {Names: []string{format.VORBIS_PACKET}, Group: &vorbisPacketFormat}, + {Names: []string{format.VP8_FRAME}, Group: &vp8FrameFormat}, + {Names: []string{format.VP9_CFM}, Group: &vp9CFMFormat}, + {Names: []string{format.VP9_FRAME}, Group: &vp9FrameFormat}, }, Files: matroskaFS, }) - codecToFormat = map[string]*[]*decode.Format{ + codecToFormat = map[string]*decode.Group{ "A_VORBIS": &vorbisPacketFormat, "A_MPEG/L3": &mp3FrameFormat, "A_FLAC": &flacFrameFormat, diff --git a/format/mp3/mp3.go b/format/mp3/mp3.go index b1442841..5441f593 100644 --- a/format/mp3/mp3.go +++ b/format/mp3/mp3.go @@ -9,25 +9,25 @@ import ( "github.com/wader/fq/pkg/decode" ) -var headerFormat []*decode.Format -var footerFormat []*decode.Format -var mp3Frame []*decode.Format +var headerFormat decode.Group +var footerFormat decode.Group +var mp3Frame decode.Group func init() { - registry.MustRegister(&decode.Format{ + registry.MustRegister(decode.Format{ Name: format.MP3, ProbeOrder: 20, // after most others (silent samples and jpeg header can look like mp3 sync) Description: "MP3 file", Groups: []string{format.PROBE}, DecodeFn: mp3Decode, Dependencies: []decode.Dependency{ - {Names: []string{format.ID3V2}, Formats: &headerFormat}, + {Names: []string{format.ID3V2}, Group: &headerFormat}, {Names: []string{ format.ID3V1, format.ID3V11, format.APEV2, - }, Formats: &footerFormat}, - {Names: []string{format.MP3_FRAME}, Formats: &mp3Frame}, + }, Group: &footerFormat}, + {Names: []string{format.MP3_FRAME}, Group: &mp3Frame}, }, }) } diff --git a/format/mp3/xing.go b/format/mp3/xing.go index a45322ae..6677646a 100644 --- a/format/mp3/xing.go +++ b/format/mp3/xing.go @@ -9,7 +9,7 @@ import ( ) func init() { - registry.MustRegister(&decode.Format{ + registry.MustRegister(decode.Format{ Name: format.XING, Description: "Xing header", DecodeFn: xingDecode, diff --git a/format/mp4/mp4.go b/format/mp4/mp4.go index ef53b3c0..5a43e16f 100644 --- a/format/mp4/mp4.go +++ b/format/mp4/mp4.go @@ -27,30 +27,30 @@ import ( //go:embed *.jq var mp4FS embed.FS -var aacFrameFormat []*decode.Format -var av1CCRFormat []*decode.Format -var av1FrameFormat []*decode.Format -var flacFrameFormat []*decode.Format -var flacMetadatablocksFormat []*decode.Format -var id3v2Format []*decode.Format -var imageFormat []*decode.Format -var jpegFormat []*decode.Format -var mp3FrameFormat []*decode.Format -var mpegAVCAUFormat []*decode.Format -var mpegAVCDCRFormat []*decode.Format -var mpegESFormat []*decode.Format -var mpegHEVCDCRFrameFormat []*decode.Format -var mpegHEVCSampleFormat []*decode.Format -var mpegPESPacketSampleFormat []*decode.Format -var opusPacketFrameFormat []*decode.Format -var protoBufWidevineFormat []*decode.Format -var psshPlayreadyFormat []*decode.Format -var vorbisPacketFormat []*decode.Format -var vp9FrameFormat []*decode.Format -var vpxCCRFormat []*decode.Format +var aacFrameFormat decode.Group +var av1CCRFormat decode.Group +var av1FrameFormat decode.Group +var flacFrameFormat decode.Group +var flacMetadatablocksFormat decode.Group +var id3v2Format decode.Group +var imageFormat decode.Group +var jpegFormat decode.Group +var mp3FrameFormat decode.Group +var mpegAVCAUFormat decode.Group +var mpegAVCDCRFormat decode.Group +var mpegESFormat decode.Group +var mpegHEVCDCRFrameFormat decode.Group +var mpegHEVCSampleFormat decode.Group +var mpegPESPacketSampleFormat decode.Group +var opusPacketFrameFormat decode.Group +var protoBufWidevineFormat decode.Group +var psshPlayreadyFormat decode.Group +var vorbisPacketFormat decode.Group +var vp9FrameFormat decode.Group +var vpxCCRFormat decode.Group func init() { - registry.MustRegister(&decode.Format{ + registry.MustRegister(decode.Format{ Name: format.MP4, Description: "MPEG-4 file and similar", Groups: []string{ @@ -59,27 +59,27 @@ func init() { }, DecodeFn: mp4Decode, Dependencies: []decode.Dependency{ - {Names: []string{format.AAC_FRAME}, Formats: &aacFrameFormat}, - {Names: []string{format.AV1_CCR}, Formats: &av1CCRFormat}, - {Names: []string{format.AV1_FRAME}, Formats: &av1FrameFormat}, - {Names: []string{format.FLAC_FRAME}, Formats: &flacFrameFormat}, - {Names: []string{format.FLAC_METADATABLOCKS}, Formats: &flacMetadatablocksFormat}, - {Names: []string{format.ID3V2}, Formats: &id3v2Format}, - {Names: []string{format.IMAGE}, Formats: &imageFormat}, - {Names: []string{format.JPEG}, Formats: &jpegFormat}, - {Names: []string{format.MP3_FRAME}, Formats: &mp3FrameFormat}, - {Names: []string{format.AVC_AU}, Formats: &mpegAVCAUFormat}, - {Names: []string{format.AVC_DCR}, Formats: &mpegAVCDCRFormat}, - {Names: []string{format.MPEG_ES}, Formats: &mpegESFormat}, - {Names: []string{format.HEVC_AU}, Formats: &mpegHEVCSampleFormat}, - {Names: []string{format.HEVC_DCR}, Formats: &mpegHEVCDCRFrameFormat}, - {Names: []string{format.MPEG_PES_PACKET}, Formats: &mpegPESPacketSampleFormat}, - {Names: []string{format.OPUS_PACKET}, Formats: &opusPacketFrameFormat}, - {Names: []string{format.PROTOBUF_WIDEVINE}, Formats: &protoBufWidevineFormat}, - {Names: []string{format.PSSH_PLAYREADY}, Formats: &psshPlayreadyFormat}, - {Names: []string{format.VORBIS_PACKET}, Formats: &vorbisPacketFormat}, - {Names: []string{format.VP9_FRAME}, Formats: &vp9FrameFormat}, - {Names: []string{format.VPX_CCR}, Formats: &vpxCCRFormat}, + {Names: []string{format.AAC_FRAME}, Group: &aacFrameFormat}, + {Names: []string{format.AV1_CCR}, Group: &av1CCRFormat}, + {Names: []string{format.AV1_FRAME}, Group: &av1FrameFormat}, + {Names: []string{format.FLAC_FRAME}, Group: &flacFrameFormat}, + {Names: []string{format.FLAC_METADATABLOCKS}, Group: &flacMetadatablocksFormat}, + {Names: []string{format.ID3V2}, Group: &id3v2Format}, + {Names: []string{format.IMAGE}, Group: &imageFormat}, + {Names: []string{format.JPEG}, Group: &jpegFormat}, + {Names: []string{format.MP3_FRAME}, Group: &mp3FrameFormat}, + {Names: []string{format.AVC_AU}, Group: &mpegAVCAUFormat}, + {Names: []string{format.AVC_DCR}, Group: &mpegAVCDCRFormat}, + {Names: []string{format.MPEG_ES}, Group: &mpegESFormat}, + {Names: []string{format.HEVC_AU}, Group: &mpegHEVCSampleFormat}, + {Names: []string{format.HEVC_DCR}, Group: &mpegHEVCDCRFrameFormat}, + {Names: []string{format.MPEG_PES_PACKET}, Group: &mpegPESPacketSampleFormat}, + {Names: []string{format.OPUS_PACKET}, Group: &opusPacketFrameFormat}, + {Names: []string{format.PROTOBUF_WIDEVINE}, Group: &protoBufWidevineFormat}, + {Names: []string{format.PSSH_PLAYREADY}, Group: &psshPlayreadyFormat}, + {Names: []string{format.VORBIS_PACKET}, Group: &vorbisPacketFormat}, + {Names: []string{format.VP9_FRAME}, Group: &vp9FrameFormat}, + {Names: []string{format.VPX_CCR}, Group: &vpxCCRFormat}, }, Files: mp4FS, }) diff --git a/format/mp4/pssh_playready.go b/format/mp4/pssh_playready.go index a2ceed71..9b6f02f3 100644 --- a/format/mp4/pssh_playready.go +++ b/format/mp4/pssh_playready.go @@ -7,7 +7,7 @@ import ( ) func init() { - registry.MustRegister(&decode.Format{ + registry.MustRegister(decode.Format{ Name: format.PSSH_PLAYREADY, Description: "PlayReady PSSH", DecodeFn: playreadyPsshDecode, diff --git a/format/mpeg/aac_frame.go b/format/mpeg/aac_frame.go index 02fb485f..923aecd2 100644 --- a/format/mpeg/aac_frame.go +++ b/format/mpeg/aac_frame.go @@ -12,7 +12,7 @@ import ( ) func init() { - registry.MustRegister(&decode.Format{ + registry.MustRegister(decode.Format{ Name: format.AAC_FRAME, Description: "Advanced Audio Coding frame", DecodeFn: aacDecode, diff --git a/format/mpeg/adts.go b/format/mpeg/adts.go index 70804a7d..cdee2b2a 100644 --- a/format/mpeg/adts.go +++ b/format/mpeg/adts.go @@ -6,10 +6,10 @@ import ( "github.com/wader/fq/pkg/decode" ) -var adtsFrame []*decode.Format +var adtsFrame decode.Group func init() { - registry.MustRegister(&decode.Format{ + registry.MustRegister(decode.Format{ Name: format.ADTS, Description: "Audio Data Transport Stream", Groups: []string{format.PROBE}, @@ -17,7 +17,7 @@ func init() { RootArray: true, RootName: "frames", Dependencies: []decode.Dependency{ - {Names: []string{format.ADTS_FRAME}, Formats: &adtsFrame}, + {Names: []string{format.ADTS_FRAME}, Group: &adtsFrame}, }, }) } diff --git a/format/mpeg/adts_frame.go b/format/mpeg/adts_frame.go index 758fa168..9f0d29fe 100644 --- a/format/mpeg/adts_frame.go +++ b/format/mpeg/adts_frame.go @@ -10,15 +10,15 @@ import ( "github.com/wader/fq/pkg/decode" ) -var aacFrameFormat []*decode.Format +var aacFrameFormat decode.Group func init() { - registry.MustRegister(&decode.Format{ + registry.MustRegister(decode.Format{ Name: format.ADTS_FRAME, Description: "Audio Data Transport Stream frame", DecodeFn: adtsFrameDecoder, Dependencies: []decode.Dependency{ - {Names: []string{format.AAC_FRAME}, Formats: &aacFrameFormat}, + {Names: []string{format.AAC_FRAME}, Group: &aacFrameFormat}, }, }) } diff --git a/format/mpeg/annexb.go b/format/mpeg/annexb.go index 0a209f3a..c10f12c5 100644 --- a/format/mpeg/annexb.go +++ b/format/mpeg/annexb.go @@ -22,7 +22,7 @@ func annexBDecodeStartCodeLen(v uint64) int64 { } } -func annexBDecode(d *decode.D, _ interface{}, format []*decode.Format) interface{} { +func annexBDecode(d *decode.D, _ interface{}, format decode.Group) interface{} { currentOffset, currentPrefixLen, err := annexBFindStartCode(d) // TODO: really restrict to 0? if err != nil || currentOffset != 0 { diff --git a/format/mpeg/avc_annexb.go b/format/mpeg/avc_annexb.go index 39bac690..968f4569 100644 --- a/format/mpeg/avc_annexb.go +++ b/format/mpeg/avc_annexb.go @@ -6,10 +6,10 @@ import ( "github.com/wader/fq/pkg/decode" ) -var annexBAVCNALUFormat []*decode.Format +var annexBAVCNALUFormat decode.Group func init() { - registry.MustRegister(&decode.Format{ + registry.MustRegister(decode.Format{ Name: format.AVC_ANNEXB, Description: "H.264/AVC Annex B", DecodeFn: func(d *decode.D, in interface{}) interface{} { @@ -18,7 +18,7 @@ func init() { RootArray: true, RootName: "stream", Dependencies: []decode.Dependency{ - {Names: []string{format.AVC_NALU}, Formats: &annexBAVCNALUFormat}, + {Names: []string{format.AVC_NALU}, Group: &annexBAVCNALUFormat}, }, }) } diff --git a/format/mpeg/avc_au.go b/format/mpeg/avc_au.go index 4f7ba09a..0d83d824 100644 --- a/format/mpeg/avc_au.go +++ b/format/mpeg/avc_au.go @@ -8,17 +8,17 @@ import ( "github.com/wader/fq/pkg/decode" ) -var avcNALUFormat []*decode.Format +var avcNALUFormat decode.Group func init() { - registry.MustRegister(&decode.Format{ + registry.MustRegister(decode.Format{ Name: format.AVC_AU, Description: "H.264/AVC Access Unit", DecodeFn: avcAUDecode, RootArray: true, RootName: "access_unit", Dependencies: []decode.Dependency{ - {Names: []string{format.AVC_NALU}, Formats: &avcNALUFormat}, + {Names: []string{format.AVC_NALU}, Group: &avcNALUFormat}, }, }) } diff --git a/format/mpeg/avc_dcr.go b/format/mpeg/avc_dcr.go index d60f4e77..b183e310 100644 --- a/format/mpeg/avc_dcr.go +++ b/format/mpeg/avc_dcr.go @@ -11,15 +11,15 @@ import ( "github.com/wader/fq/pkg/decode" ) -var avcDCRNALFormat []*decode.Format +var avcDCRNALFormat decode.Group func init() { - registry.MustRegister(&decode.Format{ + registry.MustRegister(decode.Format{ Name: format.AVC_DCR, Description: "H.264/AVC Decoder Configuration Record", DecodeFn: avcDcrDecode, Dependencies: []decode.Dependency{ - {Names: []string{format.AVC_NALU}, Formats: &avcDCRNALFormat}, + {Names: []string{format.AVC_NALU}, Group: &avcDCRNALFormat}, }, }) } diff --git a/format/mpeg/avc_nalu.go b/format/mpeg/avc_nalu.go index ed1b6b05..84a38b50 100644 --- a/format/mpeg/avc_nalu.go +++ b/format/mpeg/avc_nalu.go @@ -9,19 +9,19 @@ import ( "github.com/wader/fq/pkg/decode" ) -var avcSPSFormat []*decode.Format -var avcPPSFormat []*decode.Format -var avcSEIFormat []*decode.Format +var avcSPSFormat decode.Group +var avcPPSFormat decode.Group +var avcSEIFormat decode.Group func init() { - registry.MustRegister(&decode.Format{ + registry.MustRegister(decode.Format{ Name: format.AVC_NALU, Description: "H.264/AVC Network Access Layer Unit", DecodeFn: avcNALUDecode, Dependencies: []decode.Dependency{ - {Names: []string{format.AVC_SPS}, Formats: &avcSPSFormat}, - {Names: []string{format.AVC_PPS}, Formats: &avcPPSFormat}, - {Names: []string{format.AVC_SEI}, Formats: &avcSEIFormat}, + {Names: []string{format.AVC_SPS}, Group: &avcSPSFormat}, + {Names: []string{format.AVC_PPS}, Group: &avcPPSFormat}, + {Names: []string{format.AVC_SEI}, Group: &avcSEIFormat}, }, }) } diff --git a/format/mpeg/avc_pps.go b/format/mpeg/avc_pps.go index 885a0537..77bc9c37 100644 --- a/format/mpeg/avc_pps.go +++ b/format/mpeg/avc_pps.go @@ -7,7 +7,7 @@ import ( ) func init() { - registry.MustRegister(&decode.Format{ + registry.MustRegister(decode.Format{ Name: format.AVC_PPS, Description: "H.264/AVC Picture Parameter Set", DecodeFn: avcPPSDecode, diff --git a/format/mpeg/avc_sei.go b/format/mpeg/avc_sei.go index 601cdf3d..444269bf 100644 --- a/format/mpeg/avc_sei.go +++ b/format/mpeg/avc_sei.go @@ -7,7 +7,7 @@ import ( ) func init() { - registry.MustRegister(&decode.Format{ + registry.MustRegister(decode.Format{ Name: format.AVC_SEI, Description: "H.264/AVC Supplemental Enhancement Information", DecodeFn: avcSEIDecode, diff --git a/format/mpeg/avc_sps.go b/format/mpeg/avc_sps.go index 509656a6..a7a925a0 100644 --- a/format/mpeg/avc_sps.go +++ b/format/mpeg/avc_sps.go @@ -7,7 +7,7 @@ import ( ) func init() { - registry.MustRegister(&decode.Format{ + registry.MustRegister(decode.Format{ Name: format.AVC_SPS, Description: "H.264/AVC Sequence Parameter Set", DecodeFn: avcSPSDecode, diff --git a/format/mpeg/hevc_annexb.go b/format/mpeg/hevc_annexb.go index a543bf50..2ec15fd9 100644 --- a/format/mpeg/hevc_annexb.go +++ b/format/mpeg/hevc_annexb.go @@ -6,10 +6,10 @@ import ( "github.com/wader/fq/pkg/decode" ) -var annexBHEVCNALUFormat []*decode.Format +var annexBHEVCNALUFormat decode.Group func init() { - registry.MustRegister(&decode.Format{ + registry.MustRegister(decode.Format{ Name: format.HEVC_ANNEXB, Description: "H.265/HEVC Annex B", DecodeFn: func(d *decode.D, in interface{}) interface{} { @@ -18,7 +18,7 @@ func init() { RootArray: true, RootName: "stream", Dependencies: []decode.Dependency{ - {Names: []string{format.HEVC_NALU}, Formats: &annexBHEVCNALUFormat}, + {Names: []string{format.HEVC_NALU}, Group: &annexBHEVCNALUFormat}, }, }) } diff --git a/format/mpeg/hevc_au.go b/format/mpeg/hevc_au.go index 295cc11f..b0a88d87 100644 --- a/format/mpeg/hevc_au.go +++ b/format/mpeg/hevc_au.go @@ -6,17 +6,17 @@ import ( "github.com/wader/fq/pkg/decode" ) -var hevcAUNALFormat []*decode.Format +var hevcAUNALFormat decode.Group func init() { - registry.MustRegister(&decode.Format{ + registry.MustRegister(decode.Format{ Name: format.HEVC_AU, Description: "H.265/HEVC Access Unit", DecodeFn: hevcAUDecode, RootArray: true, RootName: "access_unit", Dependencies: []decode.Dependency{ - {Names: []string{format.HEVC_NALU}, Formats: &hevcAUNALFormat}, + {Names: []string{format.HEVC_NALU}, Group: &hevcAUNALFormat}, }, }) } diff --git a/format/mpeg/hevc_dcr.go b/format/mpeg/hevc_dcr.go index 86815a34..6165750f 100644 --- a/format/mpeg/hevc_dcr.go +++ b/format/mpeg/hevc_dcr.go @@ -6,15 +6,15 @@ import ( "github.com/wader/fq/pkg/decode" ) -var hevcDCRNALFormat []*decode.Format +var hevcDCRNALFormat decode.Group func init() { - registry.MustRegister(&decode.Format{ + registry.MustRegister(decode.Format{ Name: format.HEVC_DCR, Description: "H.265/HEVC Decoder Configuration Record", DecodeFn: hevcDcrDecode, Dependencies: []decode.Dependency{ - {Names: []string{format.HEVC_NALU}, Formats: &hevcDCRNALFormat}, + {Names: []string{format.HEVC_NALU}, Group: &hevcDCRNALFormat}, }, }) } diff --git a/format/mpeg/hevc_nalu.go b/format/mpeg/hevc_nalu.go index 6aa9f415..66feb95b 100644 --- a/format/mpeg/hevc_nalu.go +++ b/format/mpeg/hevc_nalu.go @@ -7,7 +7,7 @@ import ( ) func init() { - registry.MustRegister(&decode.Format{ + registry.MustRegister(decode.Format{ Name: format.HEVC_NALU, Description: "H.265/HEVC Network Access Layer Unit", DecodeFn: hevcNALUDecode, diff --git a/format/mpeg/mp3_frame.go b/format/mpeg/mp3_frame.go index d2568520..87606ad0 100644 --- a/format/mpeg/mp3_frame.go +++ b/format/mpeg/mp3_frame.go @@ -19,15 +19,15 @@ import ( "github.com/wader/fq/pkg/decode" ) -var xingHeader []*decode.Format +var xingHeader decode.Group func init() { - registry.MustRegister(&decode.Format{ + registry.MustRegister(decode.Format{ Name: format.MP3_FRAME, Description: "MPEG audio layer 3 frame", DecodeFn: frameDecode, Dependencies: []decode.Dependency{ - {Names: []string{format.XING}, Formats: &xingHeader}, + {Names: []string{format.XING}, Group: &xingHeader}, }, }) } diff --git a/format/mpeg/mpeg_asc.go b/format/mpeg/mpeg_asc.go index 2b1e4ca3..64e05418 100644 --- a/format/mpeg/mpeg_asc.go +++ b/format/mpeg/mpeg_asc.go @@ -9,7 +9,7 @@ import ( ) func init() { - registry.MustRegister(&decode.Format{ + registry.MustRegister(decode.Format{ Name: format.MPEG_ASC, Description: "MPEG-4 Audio Specific Config", DecodeFn: ascDecoder, diff --git a/format/mpeg/mpeg_es.go b/format/mpeg/mpeg_es.go index 21fcc520..5201a15d 100644 --- a/format/mpeg/mpeg_es.go +++ b/format/mpeg/mpeg_es.go @@ -9,17 +9,17 @@ import ( "github.com/wader/fq/pkg/decode" ) -var mpegASCFormat []*decode.Format -var vorbisPacketFormat []*decode.Format +var mpegASCFormat decode.Group +var vorbisPacketFormat decode.Group func init() { - registry.MustRegister(&decode.Format{ + registry.MustRegister(decode.Format{ Name: format.MPEG_ES, Description: "MPEG Elementary Stream", DecodeFn: esDecode, Dependencies: []decode.Dependency{ - {Names: []string{format.MPEG_ASC}, Formats: &mpegASCFormat}, - {Names: []string{format.VORBIS_PACKET}, Formats: &vorbisPacketFormat}, + {Names: []string{format.MPEG_ASC}, Group: &mpegASCFormat}, + {Names: []string{format.VORBIS_PACKET}, Group: &vorbisPacketFormat}, }, }) } diff --git a/format/mpeg/mpeg_pes.go b/format/mpeg/mpeg_pes.go index e669e579..f8256d16 100644 --- a/format/mpeg/mpeg_pes.go +++ b/format/mpeg/mpeg_pes.go @@ -13,19 +13,19 @@ import ( "github.com/wader/fq/pkg/decode" ) -var pesPacketFormat []*decode.Format -var spuFormat []*decode.Format +var pesPacketFormat decode.Group +var spuFormat decode.Group func init() { - registry.MustRegister(&decode.Format{ + registry.MustRegister(decode.Format{ Name: format.MPEG_PES, Description: "MPEG Packetized elementary stream", DecodeFn: pesDecode, RootArray: true, RootName: "packets", Dependencies: []decode.Dependency{ - {Names: []string{format.MPEG_PES_PACKET}, Formats: &pesPacketFormat}, - {Names: []string{format.MPEG_SPU}, Formats: &spuFormat}, + {Names: []string{format.MPEG_PES_PACKET}, Group: &pesPacketFormat}, + {Names: []string{format.MPEG_SPU}, Group: &spuFormat}, }, }) } diff --git a/format/mpeg/mpeg_pes_packet.go b/format/mpeg/mpeg_pes_packet.go index aa1cdab0..129c4569 100644 --- a/format/mpeg/mpeg_pes_packet.go +++ b/format/mpeg/mpeg_pes_packet.go @@ -11,7 +11,7 @@ import ( ) func init() { - registry.MustRegister(&decode.Format{ + registry.MustRegister(decode.Format{ Name: format.MPEG_PES_PACKET, Description: "MPEG Packetized elementary stream packet", DecodeFn: pesPacketDecode, diff --git a/format/mpeg/mpeg_spu.go b/format/mpeg/mpeg_spu.go index 7b3c7cd7..dc36f10b 100644 --- a/format/mpeg/mpeg_spu.go +++ b/format/mpeg/mpeg_spu.go @@ -14,7 +14,7 @@ import ( ) func init() { - registry.MustRegister(&decode.Format{ + registry.MustRegister(decode.Format{ Name: format.MPEG_SPU, Description: "Sub Picture Unit (DVD subtitle)", DecodeFn: spuDecode, diff --git a/format/mpeg/mpeg_ts.go b/format/mpeg/mpeg_ts.go index cb4c6484..a6073627 100644 --- a/format/mpeg/mpeg_ts.go +++ b/format/mpeg/mpeg_ts.go @@ -7,7 +7,7 @@ import ( ) func init() { - registry.MustRegister(&decode.Format{ + registry.MustRegister(decode.Format{ Name: format.MPEG_TS, ProbeOrder: 10, // make sure to be after gif, both start with 0x47 Description: "MPEG Transport Stream", diff --git a/format/ogg/ogg.go b/format/ogg/ogg.go index a4344fed..69f2a50d 100644 --- a/format/ogg/ogg.go +++ b/format/ogg/ogg.go @@ -12,26 +12,26 @@ import ( "github.com/wader/fq/pkg/decode" ) -var oggPageFormat []*decode.Format -var vorbisPacketFormat []*decode.Format -var vorbisCommentFormat []*decode.Format -var opusPacketFormat []*decode.Format -var flacMetadatablockFormat []*decode.Format -var flacFrameFormat []*decode.Format +var oggPageFormat decode.Group +var vorbisPacketFormat decode.Group +var vorbisCommentFormat decode.Group +var opusPacketFormat decode.Group +var flacMetadatablockFormat decode.Group +var flacFrameFormat decode.Group func init() { - registry.MustRegister(&decode.Format{ + registry.MustRegister(decode.Format{ Name: format.OGG, Description: "OGG file", Groups: []string{format.PROBE}, DecodeFn: decodeOgg, Dependencies: []decode.Dependency{ - {Names: []string{format.OGG_PAGE}, Formats: &oggPageFormat}, - {Names: []string{format.VORBIS_PACKET}, Formats: &vorbisPacketFormat}, - {Names: []string{format.VORBIS_COMMENT}, Formats: &vorbisCommentFormat}, - {Names: []string{format.OPUS_PACKET}, Formats: &opusPacketFormat}, - {Names: []string{format.FLAC_METADATABLOCK}, Formats: &flacMetadatablockFormat}, - {Names: []string{format.FLAC_FRAME}, Formats: &flacFrameFormat}, + {Names: []string{format.OGG_PAGE}, Group: &oggPageFormat}, + {Names: []string{format.VORBIS_PACKET}, Group: &vorbisPacketFormat}, + {Names: []string{format.VORBIS_COMMENT}, Group: &vorbisCommentFormat}, + {Names: []string{format.OPUS_PACKET}, Group: &opusPacketFormat}, + {Names: []string{format.FLAC_METADATABLOCK}, Group: &flacMetadatablockFormat}, + {Names: []string{format.FLAC_FRAME}, Group: &flacFrameFormat}, }, }) } diff --git a/format/ogg/ogg_page.go b/format/ogg/ogg_page.go index 22214715..39c5aabb 100644 --- a/format/ogg/ogg_page.go +++ b/format/ogg/ogg_page.go @@ -11,7 +11,7 @@ import ( ) func init() { - registry.MustRegister(&decode.Format{ + registry.MustRegister(decode.Format{ Name: format.OGG_PAGE, Description: "OGG page", DecodeFn: pageDecode, diff --git a/format/opus/opus_packet.go b/format/opus/opus_packet.go index 08cdaae9..b857c837 100644 --- a/format/opus/opus_packet.go +++ b/format/opus/opus_packet.go @@ -10,15 +10,15 @@ import ( "github.com/wader/fq/pkg/decode" ) -var vorbisComment []*decode.Format +var vorbisComment decode.Group func init() { - registry.MustRegister(&decode.Format{ + registry.MustRegister(decode.Format{ Name: format.OPUS_PACKET, Description: "Opus packet", DecodeFn: opusDecode, Dependencies: []decode.Dependency{ - {Names: []string{format.VORBIS_COMMENT}, Formats: &vorbisComment}, + {Names: []string{format.VORBIS_COMMENT}, Group: &vorbisComment}, }, }) } diff --git a/format/png/png.go b/format/png/png.go index d0d4ca3a..b04d011c 100644 --- a/format/png/png.go +++ b/format/png/png.go @@ -15,18 +15,18 @@ import ( "github.com/wader/fq/pkg/ranges" ) -var iccProfileFormat []*decode.Format -var exifFormat []*decode.Format +var iccProfileFormat decode.Group +var exifFormat decode.Group func init() { - registry.MustRegister(&decode.Format{ + registry.MustRegister(decode.Format{ Name: format.PNG, Description: "Portable Network Graphics file", Groups: []string{format.PROBE, format.IMAGE}, DecodeFn: pngDecode, Dependencies: []decode.Dependency{ - {Names: []string{format.ICC_PROFILE}, Formats: &iccProfileFormat}, - {Names: []string{format.EXIF}, Formats: &exifFormat}, + {Names: []string{format.ICC_PROFILE}, Group: &iccProfileFormat}, + {Names: []string{format.EXIF}, Group: &exifFormat}, }, }) } diff --git a/format/protobuf/protobuf.go b/format/protobuf/protobuf.go index bab8b500..21c9dbfe 100644 --- a/format/protobuf/protobuf.go +++ b/format/protobuf/protobuf.go @@ -8,7 +8,7 @@ import ( ) func init() { - registry.MustRegister(&decode.Format{ + registry.MustRegister(decode.Format{ Name: format.PROTOBUF, Description: "Protobuf", DecodeFn: protobufDecode, diff --git a/format/protobuf/protobuf_widevine.go b/format/protobuf/protobuf_widevine.go index 78fc4184..3ef2de34 100644 --- a/format/protobuf/protobuf_widevine.go +++ b/format/protobuf/protobuf_widevine.go @@ -8,15 +8,15 @@ import ( "github.com/wader/fq/pkg/decode" ) -var widevineProtoBufFormat []*decode.Format +var widevineProtoBufFormat decode.Group func init() { - registry.MustRegister(&decode.Format{ + registry.MustRegister(decode.Format{ Name: format.PROTOBUF_WIDEVINE, Description: "Widevine protobuf", DecodeFn: widevineDecode, Dependencies: []decode.Dependency{ - {Names: []string{format.PROTOBUF}, Formats: &widevineProtoBufFormat}, + {Names: []string{format.PROTOBUF}, Group: &widevineProtoBufFormat}, }, }) } diff --git a/format/raw/raw.go b/format/raw/raw.go index 01bdd3c2..661d6841 100644 --- a/format/raw/raw.go +++ b/format/raw/raw.go @@ -7,7 +7,7 @@ import ( ) func init() { - registry.MustRegister(&decode.Format{ + registry.MustRegister(decode.Format{ Name: format.RAW, Description: "Raw bits", DecodeFn: func(d *decode.D, in interface{}) interface{} { return nil }, diff --git a/format/registry/default.go b/format/registry/default.go deleted file mode 100644 index f7066084..00000000 --- a/format/registry/default.go +++ /dev/null @@ -1,12 +0,0 @@ -package registry - -import ( - "github.com/wader/fq/pkg/decode" -) - -// Default global registry that all standard formats register with -var Default = New() - -func MustRegister(format *decode.Format) *decode.Format { - return Default.MustRegister(format) -} diff --git a/format/registry/registry.go b/format/registry/registry.go index 954e5128..4d6dfd91 100644 --- a/format/registry/registry.go +++ b/format/registry/registry.go @@ -1,115 +1,13 @@ package registry import ( - "errors" - "fmt" - "sort" - "sync" - "github.com/wader/fq/pkg/decode" + "github.com/wader/fq/pkg/decode/registry" ) -type Registry struct { - Groups map[string][]*decode.Format - resolveOnce sync.Once - resolved bool -} - -func New() *Registry { - return &Registry{ - Groups: map[string][]*decode.Format{}, - resolveOnce: sync.Once{}, - } -} - -func (r *Registry) register(groupName string, format *decode.Format, single bool) *decode.Format { //nolint:unparam - if r.resolved { - // for now can't change after resolved - panic("registry already resolved") - } - - formats, ok := r.Groups[groupName] - if ok { - if !single { - panic(fmt.Sprintf("%s: format already registered", groupName)) - } - } else { - formats = []*decode.Format{} - } - - r.Groups[groupName] = append(formats, format) - - return format -} - -func (r *Registry) MustRegister(format *decode.Format) *decode.Format { - r.register(format.Name, format, false) - for _, g := range format.Groups { - r.register(g, format, true) - } - r.register("all", format, true) - - return format -} - -func sortFormats(fs []*decode.Format) { - sort.Slice(fs, func(i, j int) bool { - if fs[i].ProbeOrder == fs[j].ProbeOrder { - return fs[i].Name < fs[j].Name - } - return fs[i].ProbeOrder < fs[j].ProbeOrder - }) -} - -func (r *Registry) resolve() error { - for _, fs := range r.Groups { - for _, f := range fs { - for _, d := range f.Dependencies { - var formats []*decode.Format - for _, dName := range d.Names { - df, ok := r.Groups[dName] - if !ok { - return fmt.Errorf("%s: can't find format dependency %s", f.Name, dName) - } - formats = append(formats, df...) - } - - sortFormats(formats) - *d.Formats = formats - } - } - } - - for _, fs := range r.Groups { - sortFormats(fs) - } - - r.resolved = true - - return nil -} - -func (r *Registry) Group(name string) ([]*decode.Format, error) { - r.resolveOnce.Do(func() { - if err := r.resolve(); err != nil { - panic(err) - } - }) - - if g, ok := r.Groups[name]; ok { - return g, nil - } - return nil, errors.New("format group not found") -} - -func (r *Registry) MustGroup(name string) []*decode.Format { - g, err := r.Group(name) - if err == nil { - return g - } - panic(err) -} - -func (r *Registry) MustAll() []*decode.Format { - return r.MustGroup("all") +// Default global registry that all builtin formats register with +var Default = registry.New() + +func MustRegister(format decode.Format) { + Default.MustRegister(format) } diff --git a/format/tar/tar.go b/format/tar/tar.go index 65db6ffb..efc5c957 100644 --- a/format/tar/tar.go +++ b/format/tar/tar.go @@ -13,16 +13,16 @@ import ( "github.com/wader/fq/pkg/decode" ) -var probeFormat []*decode.Format +var probeFormat decode.Group func init() { - registry.MustRegister(&decode.Format{ + registry.MustRegister(decode.Format{ Name: format.TAR, Description: "Tar archive", Groups: []string{format.PROBE}, DecodeFn: tarDecode, Dependencies: []decode.Dependency{ - {Names: []string{format.PROBE}, Formats: &probeFormat}, + {Names: []string{format.PROBE}, Group: &probeFormat}, }, }) } diff --git a/format/tiff/exif.go b/format/tiff/exif.go index ccd26bad..93a2009a 100644 --- a/format/tiff/exif.go +++ b/format/tiff/exif.go @@ -12,7 +12,7 @@ import ( // currently just a alias for tiff func init() { - registry.MustRegister(&decode.Format{ + registry.MustRegister(decode.Format{ Name: format.EXIF, Description: "Exchangeable Image File Format", Groups: []string{}, diff --git a/format/tiff/tiff.go b/format/tiff/tiff.go index 3ce1bc01..f2ebadb6 100644 --- a/format/tiff/tiff.go +++ b/format/tiff/tiff.go @@ -8,16 +8,16 @@ import ( "github.com/wader/fq/pkg/decode" ) -var tiffIccProfile []*decode.Format +var tiffIccProfile decode.Group func init() { - registry.MustRegister(&decode.Format{ + registry.MustRegister(decode.Format{ Name: format.TIFF, Description: "Tag Image File Format", Groups: []string{format.PROBE, format.IMAGE}, DecodeFn: tiffDecode, Dependencies: []decode.Dependency{ - {Names: []string{format.ICC_PROFILE}, Formats: &tiffIccProfile}, + {Names: []string{format.ICC_PROFILE}, Group: &tiffIccProfile}, }, }) } diff --git a/format/vorbis/vorbis_comment.go b/format/vorbis/vorbis_comment.go index 2b26bd7d..56b29bc2 100644 --- a/format/vorbis/vorbis_comment.go +++ b/format/vorbis/vorbis_comment.go @@ -10,15 +10,15 @@ import ( "github.com/wader/fq/pkg/decode" ) -var flacPicture []*decode.Format +var flacPicture decode.Group func init() { - registry.MustRegister(&decode.Format{ + registry.MustRegister(decode.Format{ Name: format.VORBIS_COMMENT, Description: "Vorbis comment", DecodeFn: commentDecode, Dependencies: []decode.Dependency{ - {Names: []string{format.FLAC_PICTURE}, Formats: &flacPicture}, + {Names: []string{format.FLAC_PICTURE}, Group: &flacPicture}, }, }) } diff --git a/format/vorbis/vorbis_packet.go b/format/vorbis/vorbis_packet.go index 140f2f9e..aba530ee 100644 --- a/format/vorbis/vorbis_packet.go +++ b/format/vorbis/vorbis_packet.go @@ -10,15 +10,15 @@ import ( "github.com/wader/fq/pkg/decode" ) -var vorbisComment []*decode.Format +var vorbisComment decode.Group func init() { - registry.MustRegister(&decode.Format{ + registry.MustRegister(decode.Format{ Name: format.VORBIS_PACKET, Description: "Vorbis packet", DecodeFn: vorbisDecode, Dependencies: []decode.Dependency{ - {Names: []string{format.VORBIS_COMMENT}, Formats: &vorbisComment}, + {Names: []string{format.VORBIS_COMMENT}, Group: &vorbisComment}, }, }) } diff --git a/format/vpx/vp8_frame.go b/format/vpx/vp8_frame.go index 6dfddef5..548ea2c5 100644 --- a/format/vpx/vp8_frame.go +++ b/format/vpx/vp8_frame.go @@ -11,7 +11,7 @@ import ( // TODO: vpx frame? func init() { - registry.MustRegister(&decode.Format{ + registry.MustRegister(decode.Format{ Name: format.VP8_FRAME, Description: "VP8 frame", DecodeFn: vp8Decode, diff --git a/format/vpx/vp9_cfm.go b/format/vpx/vp9_cfm.go index 2042e1be..fb370655 100644 --- a/format/vpx/vp9_cfm.go +++ b/format/vpx/vp9_cfm.go @@ -9,7 +9,7 @@ import ( ) func init() { - registry.MustRegister(&decode.Format{ + registry.MustRegister(decode.Format{ Name: format.VP9_CFM, Description: "VP9 Codec Feature Metadata", DecodeFn: vp9CFMDecode, diff --git a/format/vpx/vp9_frame.go b/format/vpx/vp9_frame.go index 1df89eba..cc4bc9ac 100644 --- a/format/vpx/vp9_frame.go +++ b/format/vpx/vp9_frame.go @@ -48,7 +48,7 @@ var vp9ColorSpaceNames = decode.UToStr{ } func init() { - registry.MustRegister(&decode.Format{ + registry.MustRegister(decode.Format{ Name: format.VP9_FRAME, Description: "VP9 frame", DecodeFn: vp9Decode, diff --git a/format/vpx/vpx_ccr.go b/format/vpx/vpx_ccr.go index 8c694ec3..75d7dba9 100644 --- a/format/vpx/vpx_ccr.go +++ b/format/vpx/vpx_ccr.go @@ -9,7 +9,7 @@ import ( ) func init() { - registry.MustRegister(&decode.Format{ + registry.MustRegister(decode.Format{ Name: format.VPX_CCR, Description: "VPX Codec Configuration Record", DecodeFn: vpxCCRDecode, diff --git a/format/wav/wav.go b/format/wav/wav.go index 9419d240..64787664 100644 --- a/format/wav/wav.go +++ b/format/wav/wav.go @@ -16,19 +16,19 @@ import ( "github.com/wader/fq/pkg/decode" ) -var headerFormat []*decode.Format -var footerFormat []*decode.Format +var headerFormat decode.Group +var footerFormat decode.Group func init() { - registry.MustRegister(&decode.Format{ + registry.MustRegister(decode.Format{ Name: format.WAV, ProbeOrder: 10, // after most others (overlap some with webp) Description: "WAV file", Groups: []string{format.PROBE}, DecodeFn: wavDecode, Dependencies: []decode.Dependency{ - {Names: []string{format.ID3V2}, Formats: &headerFormat}, - {Names: []string{format.ID3V1, format.ID3V11}, Formats: &footerFormat}, + {Names: []string{format.ID3V2}, Group: &headerFormat}, + {Names: []string{format.ID3V1, format.ID3V11}, Group: &footerFormat}, }, }) } diff --git a/format/webp/webp.go b/format/webp/webp.go index 437297be..2a115f1d 100644 --- a/format/webp/webp.go +++ b/format/webp/webp.go @@ -10,16 +10,16 @@ import ( "github.com/wader/fq/pkg/decode" ) -var vp8Frame []*decode.Format +var vp8Frame decode.Group func init() { - registry.MustRegister(&decode.Format{ + registry.MustRegister(decode.Format{ Name: format.WEBP, Description: "WebP image", Groups: []string{format.PROBE, format.IMAGE}, DecodeFn: webpDecode, Dependencies: []decode.Dependency{ - {Names: []string{format.VP8_FRAME}, Formats: &vp8Frame}, + {Names: []string{format.VP8_FRAME}, Group: &vp8Frame}, }, }) } diff --git a/pkg/cli/cli.go b/pkg/cli/cli.go index 96534f76..eddfd577 100644 --- a/pkg/cli/cli.go +++ b/pkg/cli/cli.go @@ -12,7 +12,7 @@ import ( "os/signal" "path/filepath" - "github.com/wader/fq/format/registry" + "github.com/wader/fq/pkg/decode/registry" "github.com/wader/fq/pkg/interp" "github.com/wader/readline" diff --git a/pkg/decode/decode.go b/pkg/decode/decode.go index 373f8522..0248d969 100644 --- a/pkg/decode/decode.go +++ b/pkg/decode/decode.go @@ -33,34 +33,34 @@ type Options struct { ReadBuf *[]byte } -// Decode try decode formats and return first success and all other decoder errors -func Decode(ctx context.Context, bb *bitio.Buffer, formats []*Format, opts Options) (*Value, interface{}, error) { - return decode(ctx, bb, formats, opts) +// Decode try decode group and return first success and all other decoder errors +func Decode(ctx context.Context, bb *bitio.Buffer, group Group, opts Options) (*Value, interface{}, error) { + return decode(ctx, bb, group, opts) } -func decode(ctx context.Context, bb *bitio.Buffer, formats []*Format, opts Options) (*Value, interface{}, error) { +func decode(ctx context.Context, bb *bitio.Buffer, group Group, opts Options) (*Value, interface{}, error) { decodeRange := opts.Range if decodeRange.IsZero() { decodeRange = ranges.Range{Len: bb.Len()} } - if formats == nil { - panic("formats is nil, failed to register format?") + if group == nil { + panic("group is nil, failed to register format?") } - decodeErr := FormatsError{} + formatsErr := FormatsError{} - for _, f := range formats { + for _, g := range group { cbb, err := bb.BitBufRange(decodeRange.Start, decodeRange.Len) if err != nil { return nil, nil, IOError{Err: err, Op: "BitBufRange", ReadSize: decodeRange.Len, Pos: decodeRange.Start} } - d := newDecoder(ctx, f, cbb, opts) + d := newDecoder(ctx, g, cbb, opts) var decodeV interface{} r, rOk := recoverfn.Run(func() { - decodeV = f.DecodeFn(d, opts.FormatInArg) + decodeV = g.DecodeFn(d, opts.FormatInArg) }) if ctx != nil && ctx.Err() != nil { @@ -72,10 +72,10 @@ func decode(ctx context.Context, bb *bitio.Buffer, formats []*Format, opts Optio panicErr, _ := re.(error) formatErr := FormatError{ Err: panicErr, - Format: f, + Format: g, Stacktrace: r, } - decodeErr.Errs = append(decodeErr.Errs, formatErr) + formatsErr.Errs = append(formatsErr.Errs, formatErr) switch vv := d.Value.V.(type) { case Compound: @@ -84,7 +84,7 @@ func decode(ctx context.Context, bb *bitio.Buffer, formats []*Format, opts Optio d.Value.V = vv } - if len(formats) != 1 { + if len(group) != 1 { continue } } else { @@ -113,10 +113,10 @@ func decode(ctx context.Context, bb *bitio.Buffer, formats []*Format, opts Optio d.Value.postProcess() } - return d.Value, decodeV, decodeErr + return d.Value, decodeV, formatsErr } - return nil, nil, decodeErr + return nil, nil, formatsErr } type D struct { @@ -132,7 +132,7 @@ type D struct { // TODO: new struct decoder? // note bb is assumed to be a non-shared buffer -func newDecoder(ctx context.Context, format *Format, bb *bitio.Buffer, opts Options) *D { +func newDecoder(ctx context.Context, format Format, bb *bitio.Buffer, opts Options) *D { name := format.RootName if opts.Name != "" { name = opts.Name @@ -141,7 +141,7 @@ func newDecoder(ctx context.Context, format *Format, bb *bitio.Buffer, opts Opti IsArray: format.RootArray, Children: new([]*Value), Description: opts.Description, - Format: format, + Format: &format, } return &D{ @@ -652,8 +652,8 @@ func (d *D) RangeFn(firstBit int64, nBits int64, fn func(d *D)) { } } -func (d *D) Format(formats []*Format, inArg interface{}) interface{} { - dv, v, err := decode(d.Ctx, d.bitBuf, formats, Options{ +func (d *D) Format(group Group, inArg interface{}) interface{} { + dv, v, err := decode(d.Ctx, d.bitBuf, group, Options{ Force: d.Options.Force, FillGaps: false, IsRoot: false, @@ -681,8 +681,8 @@ func (d *D) Format(formats []*Format, inArg interface{}) interface{} { return v } -func (d *D) FieldTryFormat(name string, formats []*Format, inArg interface{}) (*Value, interface{}, error) { - dv, v, err := decode(d.Ctx, d.bitBuf, formats, Options{ +func (d *D) FieldTryFormat(name string, group Group, inArg interface{}) (*Value, interface{}, error) { + dv, v, err := decode(d.Ctx, d.bitBuf, group, Options{ Name: name, Force: d.Options.Force, FillGaps: false, @@ -703,16 +703,16 @@ func (d *D) FieldTryFormat(name string, formats []*Format, inArg interface{}) (* return dv, v, err } -func (d *D) FieldFormat(name string, formats []*Format, inArg interface{}) (*Value, interface{}) { - dv, v, err := d.FieldTryFormat(name, formats, inArg) +func (d *D) FieldFormat(name string, group Group, inArg interface{}) (*Value, interface{}) { + dv, v, err := d.FieldTryFormat(name, group, inArg) if dv == nil || dv.Errors() != nil { panic(err) } return dv, v } -func (d *D) FieldTryFormatLen(name string, nBits int64, formats []*Format, inArg interface{}) (*Value, interface{}, error) { - dv, v, err := decode(d.Ctx, d.bitBuf, formats, Options{ +func (d *D) FieldTryFormatLen(name string, nBits int64, group Group, inArg interface{}) (*Value, interface{}, error) { + dv, v, err := decode(d.Ctx, d.bitBuf, group, Options{ Name: name, Force: d.Options.Force, FillGaps: true, @@ -733,8 +733,8 @@ func (d *D) FieldTryFormatLen(name string, nBits int64, formats []*Format, inArg return dv, v, err } -func (d *D) FieldFormatLen(name string, nBits int64, formats []*Format, inArg interface{}) (*Value, interface{}) { - dv, v, err := d.FieldTryFormatLen(name, nBits, formats, inArg) +func (d *D) FieldFormatLen(name string, nBits int64, group Group, inArg interface{}) (*Value, interface{}) { + dv, v, err := d.FieldTryFormatLen(name, nBits, group, inArg) if dv == nil || dv.Errors() != nil { panic(err) } @@ -742,8 +742,8 @@ func (d *D) FieldFormatLen(name string, nBits int64, formats []*Format, inArg in } // TODO: return decooder? -func (d *D) FieldTryFormatRange(name string, firstBit int64, nBits int64, formats []*Format, inArg interface{}) (*Value, interface{}, error) { - dv, v, err := decode(d.Ctx, d.bitBuf, formats, Options{ +func (d *D) FieldTryFormatRange(name string, firstBit int64, nBits int64, group Group, inArg interface{}) (*Value, interface{}, error) { + dv, v, err := decode(d.Ctx, d.bitBuf, group, Options{ Name: name, Force: d.Options.Force, FillGaps: true, @@ -761,8 +761,8 @@ func (d *D) FieldTryFormatRange(name string, firstBit int64, nBits int64, format return dv, v, err } -func (d *D) FieldFormatRange(name string, firstBit int64, nBits int64, formats []*Format, inArg interface{}) (*Value, interface{}) { - dv, v, err := d.FieldTryFormatRange(name, firstBit, nBits, formats, inArg) +func (d *D) FieldFormatRange(name string, firstBit int64, nBits int64, group Group, inArg interface{}) (*Value, interface{}) { + dv, v, err := d.FieldTryFormatRange(name, firstBit, nBits, group, inArg) if dv == nil || dv.Errors() != nil { panic(err) } @@ -770,8 +770,8 @@ func (d *D) FieldFormatRange(name string, firstBit int64, nBits int64, formats [ return dv, v } -func (d *D) FieldTryFormatBitBuf(name string, bb *bitio.Buffer, formats []*Format, inArg interface{}) (*Value, interface{}, error) { - dv, v, err := decode(d.Ctx, bb, formats, Options{ +func (d *D) FieldTryFormatBitBuf(name string, bb *bitio.Buffer, group Group, inArg interface{}) (*Value, interface{}, error) { + dv, v, err := decode(d.Ctx, bb, group, Options{ Name: name, Force: d.Options.Force, FillGaps: true, @@ -788,8 +788,8 @@ func (d *D) FieldTryFormatBitBuf(name string, bb *bitio.Buffer, formats []*Forma return dv, v, err } -func (d *D) FieldFormatBitBuf(name string, bb *bitio.Buffer, formats []*Format, inArg interface{}) (*Value, interface{}) { - dv, v, err := d.FieldTryFormatBitBuf(name, bb, formats, inArg) +func (d *D) FieldFormatBitBuf(name string, bb *bitio.Buffer, group Group, inArg interface{}) (*Value, interface{}) { + dv, v, err := d.FieldTryFormatBitBuf(name, bb, group, inArg) if dv == nil || dv.Errors() != nil { panic(err) } @@ -819,7 +819,7 @@ func (d *D) FieldStructRootBitBufFn(name string, bb *bitio.Buffer, fn func(d *D) } // TODO: range? -func (d *D) FieldFormatReaderLen(name string, nBits int64, fn func(r io.Reader) (io.ReadCloser, error), formats []*Format) (*Value, interface{}) { +func (d *D) FieldFormatReaderLen(name string, nBits int64, fn func(r io.Reader) (io.ReadCloser, error), group Group) (*Value, interface{}) { bb, err := d.bitBuf.BitBufLen(nBits) if err != nil { panic(err) @@ -834,5 +834,5 @@ func (d *D) FieldFormatReaderLen(name string, nBits int64, fn func(r io.Reader) } zbb := bitio.NewBufferFromBytes(zd, -1) - return d.FieldFormatBitBuf(name, zbb, formats, nil) + return d.FieldFormatBitBuf(name, zbb, group, nil) } diff --git a/pkg/decode/errors.go b/pkg/decode/errors.go index 6be9bd97..3458ae8e 100644 --- a/pkg/decode/errors.go +++ b/pkg/decode/errors.go @@ -14,7 +14,7 @@ type RecoverableErrorer interface { type FormatError struct { Err error - Format *Format + Format Format Stacktrace recoverfn.Raw } diff --git a/pkg/decode/format.go b/pkg/decode/format.go index 410b5667..b29dbf93 100644 --- a/pkg/decode/format.go +++ b/pkg/decode/format.go @@ -2,9 +2,11 @@ package decode import "io/fs" +type Group []Format + type Dependency struct { - Names []string - Formats *[]*Format // TODO: rename to outFormats to make it clear it's used to assign? + Names []string + Group *Group } type Format struct { @@ -19,8 +21,8 @@ type Format struct { Files fs.ReadDirFS } -func FormatFn(d func(d *D, in interface{}) interface{}) []*Format { - return []*Format{{ +func FormatFn(d func(d *D, in interface{}) interface{}) Group { + return Group{{ DecodeFn: d, }} } diff --git a/pkg/decode/registry/registry.go b/pkg/decode/registry/registry.go new file mode 100644 index 00000000..6394d7b0 --- /dev/null +++ b/pkg/decode/registry/registry.go @@ -0,0 +1,115 @@ +package registry + +import ( + "errors" + "fmt" + "sort" + "sync" + + "github.com/wader/fq/pkg/decode" +) + +type Registry struct { + Groups map[string]decode.Group + resolveOnce sync.Once + resolved bool +} + +func New() *Registry { + return &Registry{ + Groups: map[string]decode.Group{}, + resolveOnce: sync.Once{}, + } +} + +func (r *Registry) register(groupName string, format decode.Format, single bool) decode.Format { //nolint:unparam + if r.resolved { + // for now can't change after resolved + panic("registry already resolved") + } + + group, ok := r.Groups[groupName] + if ok { + if !single { + panic(fmt.Sprintf("%s: format already registered", groupName)) + } + } else { + group = decode.Group{} + } + + r.Groups[groupName] = append(group, format) + + return format +} + +func (r *Registry) MustRegister(format decode.Format) decode.Format { + r.register(format.Name, format, false) + for _, g := range format.Groups { + r.register(g, format, true) + } + r.register("all", format, true) + + return format +} + +func sortFormats(g decode.Group) { + sort.Slice(g, func(i, j int) bool { + if g[i].ProbeOrder == g[j].ProbeOrder { + return g[i].Name < g[j].Name + } + return g[i].ProbeOrder < g[j].ProbeOrder + }) +} + +func (r *Registry) resolve() error { + for _, fs := range r.Groups { + for _, f := range fs { + for _, d := range f.Dependencies { + var group decode.Group + for _, dName := range d.Names { + df, ok := r.Groups[dName] + if !ok { + return fmt.Errorf("%s: can't find format dependency %s", f.Name, dName) + } + group = append(group, df...) + } + + sortFormats(group) + *d.Group = group + } + } + } + + for _, fs := range r.Groups { + sortFormats(fs) + } + + r.resolved = true + + return nil +} + +func (r *Registry) Group(name string) (decode.Group, error) { + r.resolveOnce.Do(func() { + if err := r.resolve(); err != nil { + panic(err) + } + }) + + if g, ok := r.Groups[name]; ok { + return g, nil + } + return nil, errors.New("format group not found") +} + +func (r *Registry) MustGroup(name string) decode.Group { + g, err := r.Group(name) + if err == nil { + return g + } + panic(err) +} + +func (r *Registry) MustAll() decode.Group { + return r.MustGroup("all") +} diff --git a/pkg/fqtest/fqtest.go b/pkg/fqtest/fqtest.go index 7ce41b88..6100a196 100644 --- a/pkg/fqtest/fqtest.go +++ b/pkg/fqtest/fqtest.go @@ -7,9 +7,9 @@ import ( "strconv" "testing" - "github.com/wader/fq/format/registry" "github.com/wader/fq/internal/difftest" "github.com/wader/fq/internal/script" + "github.com/wader/fq/pkg/decode/registry" "github.com/wader/fq/pkg/interp" ) diff --git a/pkg/interp/funcs.go b/pkg/interp/funcs.go index c9480623..5ad18f83 100644 --- a/pkg/interp/funcs.go +++ b/pkg/interp/funcs.go @@ -155,7 +155,7 @@ func makeHashFn(fn func() (hash.Hash, error)) func(c interface{}, a []interface{ } func (i *Interp) _registry(c interface{}, a []interface{}) interface{} { - uniqueFormats := map[string]*decode.Format{} + uniqueFormats := map[string]decode.Format{} groups := map[string]interface{}{} formats := map[string]interface{}{} diff --git a/pkg/interp/interp.go b/pkg/interp/interp.go index 01707f86..f28f3ff7 100644 --- a/pkg/interp/interp.go +++ b/pkg/interp/interp.go @@ -18,7 +18,6 @@ import ( "time" "github.com/mitchellh/mapstructure" - "github.com/wader/fq/format/registry" "github.com/wader/fq/internal/ansi" "github.com/wader/fq/internal/colorjson" "github.com/wader/fq/internal/ctxstack" @@ -27,6 +26,7 @@ import ( "github.com/wader/fq/internal/pos" "github.com/wader/fq/pkg/bitio" "github.com/wader/fq/pkg/decode" + "github.com/wader/fq/pkg/decode/registry" "github.com/wader/gojq" )