2020-06-08 03:29:51 +03:00
|
|
|
package vorbis
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
2021-11-20 18:22:00 +03:00
|
|
|
"io"
|
2020-06-08 03:29:51 +03:00
|
|
|
"strings"
|
2021-08-17 13:06:32 +03:00
|
|
|
|
|
|
|
"github.com/wader/fq/format"
|
|
|
|
"github.com/wader/fq/format/registry"
|
|
|
|
"github.com/wader/fq/pkg/decode"
|
2020-06-08 03:29:51 +03:00
|
|
|
)
|
|
|
|
|
2021-11-17 18:46:10 +03:00
|
|
|
var flacPicture decode.Group
|
2020-06-08 03:29:51 +03:00
|
|
|
|
|
|
|
func init() {
|
2021-11-17 18:46:10 +03:00
|
|
|
registry.MustRegister(decode.Format{
|
2020-06-08 03:29:51 +03:00
|
|
|
Name: format.VORBIS_COMMENT,
|
|
|
|
Description: "Vorbis comment",
|
|
|
|
DecodeFn: commentDecode,
|
|
|
|
Dependencies: []decode.Dependency{
|
2021-11-17 18:46:10 +03:00
|
|
|
{Names: []string{format.FLAC_PICTURE}, Group: &flacPicture},
|
2020-06-08 03:29:51 +03:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-05-20 16:10:41 +03:00
|
|
|
func commentDecode(d *decode.D, in any) any {
|
2021-12-04 21:04:22 +03:00
|
|
|
d.Endian = decode.LittleEndian
|
|
|
|
|
|
|
|
vendorLen := d.FieldU32("vendor_length")
|
2020-06-08 03:29:51 +03:00
|
|
|
d.FieldUTF8("vendor", int(vendorLen))
|
2021-12-04 21:04:22 +03:00
|
|
|
userCommentListLength := d.FieldU32("user_comment_list_length")
|
2020-06-08 03:29:51 +03:00
|
|
|
i := uint64(0)
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldStructArrayLoop("user_comments", "user_comment", func() bool { return i < userCommentListLength }, func(d *decode.D) {
|
2021-12-04 21:04:22 +03:00
|
|
|
userCommentLength := d.FieldU32("length")
|
2021-11-20 18:22:00 +03:00
|
|
|
userCommentStart := d.Pos()
|
2021-10-06 13:01:07 +03:00
|
|
|
userComment := d.FieldUTF8("comment", int(userCommentLength))
|
2022-04-11 23:45:46 +03:00
|
|
|
var metadataBlockPicturePrefix = "METADATA_BLOCK_PICTURE="
|
2021-11-20 18:22:00 +03:00
|
|
|
var metadataBlockPicturePrefixLower = "metadata_block_picture="
|
|
|
|
|
2022-04-11 23:45:46 +03:00
|
|
|
if strings.HasPrefix(userComment, metadataBlockPicturePrefix) ||
|
2021-11-20 18:22:00 +03:00
|
|
|
strings.HasPrefix(userComment, metadataBlockPicturePrefixLower) {
|
|
|
|
|
2022-04-11 23:45:46 +03:00
|
|
|
base64Offset := int64(len(metadataBlockPicturePrefix)) * 8
|
2021-11-20 18:22:00 +03:00
|
|
|
base64Len := int64(len(userComment))*8 - base64Offset
|
2022-01-24 23:21:48 +03:00
|
|
|
_, base64Br, dv, _, _ := d.TryFieldReaderRangeFormat(
|
2021-12-04 21:04:22 +03:00
|
|
|
"picture",
|
|
|
|
userCommentStart+base64Offset, base64Len,
|
|
|
|
func(r io.Reader) io.Reader { return base64.NewDecoder(base64.StdEncoding, r) },
|
|
|
|
flacPicture, nil,
|
|
|
|
)
|
2022-01-24 23:21:48 +03:00
|
|
|
if dv == nil && base64Br != nil {
|
|
|
|
d.FieldRootBitBuf("picture", base64Br)
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|