2020-06-08 03:29:51 +03:00
|
|
|
|
package vpx
|
|
|
|
|
|
|
|
|
|
// https://storage.googleapis.com/downloads.webmproject.org/docs/vp9/vp9-bitstream-specification-v0.6-20160331-draft.pdf
|
|
|
|
|
|
|
|
|
|
import (
|
2021-08-17 13:06:32 +03:00
|
|
|
|
"github.com/wader/fq/format"
|
|
|
|
|
"github.com/wader/fq/pkg/decode"
|
2022-07-16 19:39:57 +03:00
|
|
|
|
"github.com/wader/fq/pkg/interp"
|
2021-12-02 00:48:25 +03:00
|
|
|
|
"github.com/wader/fq/pkg/scalar"
|
2020-06-08 03:29:51 +03:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// TODO: vpx frame?
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
vp9FeatureProfile = 1
|
|
|
|
|
vp9FeatureLevel = 2
|
|
|
|
|
vp9FeatureBitDepth = 3
|
|
|
|
|
vp9FeatureChromaSubsampling = 4
|
|
|
|
|
)
|
|
|
|
|
|
2022-09-30 14:58:23 +03:00
|
|
|
|
var vp9FeatureIDNames = scalar.UintMapSymStr{
|
2020-06-08 03:29:51 +03:00
|
|
|
|
vp9FeatureProfile: "Profile",
|
|
|
|
|
vp9FeatureLevel: "Level",
|
|
|
|
|
vp9FeatureBitDepth: "Bit Depth",
|
|
|
|
|
vp9FeatureChromaSubsampling: "Chroma Subsampling",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
CS_UNKNOWN = 0
|
|
|
|
|
CS_BT_601 = 1
|
|
|
|
|
CS_BT_709 = 2
|
|
|
|
|
CS_SMPTE_170 = 3
|
|
|
|
|
CS_SMPTE_240 = 4
|
|
|
|
|
CS_BT_2020 = 5
|
|
|
|
|
CS_RESERVED = 6
|
|
|
|
|
CS_RGB = 7
|
|
|
|
|
)
|
|
|
|
|
|
2022-09-30 14:58:23 +03:00
|
|
|
|
var vp9ColorSpaceNames = scalar.UintMapSymStr{
|
2022-04-05 14:57:55 +03:00
|
|
|
|
CS_UNKNOWN: "unknown",
|
|
|
|
|
CS_BT_601: "bt_601",
|
|
|
|
|
CS_BT_709: "bt_709",
|
|
|
|
|
CS_SMPTE_170: "smpte_170",
|
|
|
|
|
CS_SMPTE_240: "smpte_240",
|
|
|
|
|
CS_BT_2020: "bt_2020",
|
|
|
|
|
CS_RESERVED: "reserved",
|
|
|
|
|
CS_RGB: "rgb",
|
2020-06-08 03:29:51 +03:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-30 14:58:23 +03:00
|
|
|
|
var vp9ProfilesMap = scalar.UintMapDescription{
|
2022-04-07 19:25:23 +03:00
|
|
|
|
0: "8 bit/sample, chroma subsampling: 4:2:0",
|
|
|
|
|
1: "8 bit, chroma subsampling: 4:2:2, 4:4:0, 4:4:4",
|
|
|
|
|
2: "10–12 bit, chroma subsampling: 4:2:0",
|
|
|
|
|
3: "10–12 bit, chroma subsampling: 4:2:2, 4:4:0, 4:4:4",
|
2021-11-29 18:14:59 +03:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-08 03:29:51 +03:00
|
|
|
|
func init() {
|
2023-03-29 01:36:55 +03:00
|
|
|
|
interp.RegisterFormat(
|
2023-05-01 14:19:04 +03:00
|
|
|
|
format.VP9_Frame,
|
2023-03-29 01:36:55 +03:00
|
|
|
|
&decode.Format{
|
|
|
|
|
Description: "VP9 frame",
|
|
|
|
|
DecodeFn: vp9Decode,
|
|
|
|
|
})
|
2020-06-08 03:29:51 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func vp9DecodeFrameSyncCode(d *decode.D) {
|
|
|
|
|
d.FieldU8("frame_sync_byte_0")
|
|
|
|
|
d.FieldU8("frame_sync_byte_1")
|
|
|
|
|
d.FieldU8("frame_sync_byte_2")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func vp9DecodeColorConfig(d *decode.D, profile int) {
|
|
|
|
|
bitDepth := 8
|
|
|
|
|
if profile >= 2 {
|
|
|
|
|
tenOrTwelveBit := d.FieldBool("ten_or_twelve_bit")
|
|
|
|
|
if tenOrTwelveBit {
|
|
|
|
|
bitDepth = 12
|
|
|
|
|
} else {
|
|
|
|
|
bitDepth = 10
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-09-30 14:58:23 +03:00
|
|
|
|
d.FieldValueUint("bit_depth", uint64(bitDepth))
|
2021-12-02 00:48:25 +03:00
|
|
|
|
colorSpace := d.FieldU3("color_space", vp9ColorSpaceNames)
|
2021-11-05 17:04:26 +03:00
|
|
|
|
_, colorSpaceOk := vp9ColorSpaceNames[colorSpace]
|
2020-06-08 03:29:51 +03:00
|
|
|
|
if !colorSpaceOk || colorSpace != CS_RGB {
|
|
|
|
|
d.FieldU1("color_range")
|
|
|
|
|
if profile == 1 || profile == 3 {
|
|
|
|
|
d.FieldU1("subsampling_x")
|
|
|
|
|
d.FieldU1("subsampling_y")
|
2021-11-29 18:14:59 +03:00
|
|
|
|
d.FieldU1("reserved_zero1")
|
2020-06-08 03:29:51 +03:00
|
|
|
|
} else {
|
2022-09-30 14:58:23 +03:00
|
|
|
|
d.FieldValueUint("subsampling_x", 1)
|
|
|
|
|
d.FieldValueUint("subsampling_y", 1)
|
2020-06-08 03:29:51 +03:00
|
|
|
|
}
|
|
|
|
|
} else {
|
2022-09-30 14:58:23 +03:00
|
|
|
|
d.FieldValueUint("color_range", 1)
|
2020-06-08 03:29:51 +03:00
|
|
|
|
if profile == 1 || profile == 3 {
|
2022-09-30 14:58:23 +03:00
|
|
|
|
d.FieldValueUint("subsampling_x", 0)
|
|
|
|
|
d.FieldValueUint("subsampling_y", 0)
|
2021-11-29 18:14:59 +03:00
|
|
|
|
d.FieldU1("reserved_zero2")
|
2020-06-08 03:29:51 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func vp9DecodeFrameSize(d *decode.D) {
|
2022-09-30 14:58:23 +03:00
|
|
|
|
d.FieldUintFn("frame_width", func(d *decode.D) uint64 { return d.U16() + 1 })
|
|
|
|
|
d.FieldUintFn("frame_height", func(d *decode.D) uint64 { return d.U16() + 1 })
|
2020-06-08 03:29:51 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-18 23:10:48 +03:00
|
|
|
|
func vp9Decode(d *decode.D) any {
|
2020-06-08 03:29:51 +03:00
|
|
|
|
|
|
|
|
|
// TODO: header_size at end? even for show_existing_frame?
|
|
|
|
|
|
|
|
|
|
d.FieldU2("frame_marker")
|
|
|
|
|
profileLowBit := d.FieldU1("profile_low_bit")
|
|
|
|
|
profileHighBit := d.FieldU1("profile_high_bit")
|
|
|
|
|
profile := int(profileHighBit<<1 + profileLowBit)
|
2022-09-30 14:58:23 +03:00
|
|
|
|
d.FieldValueUint("profile", uint64(profile), vp9ProfilesMap)
|
2020-06-08 03:29:51 +03:00
|
|
|
|
if profile == 3 {
|
2021-11-29 18:14:59 +03:00
|
|
|
|
d.FieldU1("reserved_zero0")
|
2020-06-08 03:29:51 +03:00
|
|
|
|
}
|
|
|
|
|
showExistingFrame := d.FieldBool("show_existing_frame")
|
|
|
|
|
if showExistingFrame {
|
|
|
|
|
d.FieldU2("frame_to_show_map_idx")
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-30 14:58:23 +03:00
|
|
|
|
frameType := d.FieldBool("frame_type", scalar.BoolMapSymStr{true: "non_key_frame", false: "key_frame"})
|
2020-06-08 03:29:51 +03:00
|
|
|
|
d.FieldU1("show_frame")
|
|
|
|
|
d.FieldU1("error_resilient_mode")
|
|
|
|
|
|
|
|
|
|
if !frameType {
|
|
|
|
|
// is key frame
|
|
|
|
|
vp9DecodeFrameSyncCode(d)
|
|
|
|
|
vp9DecodeColorConfig(d, profile)
|
|
|
|
|
vp9DecodeFrameSize(d)
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-05 17:04:26 +03:00
|
|
|
|
d.FieldRawLen("data", d.BitsLeft())
|
2020-06-08 03:29:51 +03:00
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|