Skip to content

Commit

Permalink
decode: Refactor and add symbol struct and mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
wader committed Sep 12, 2021
1 parent f4b11b4 commit 9f55b6e
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 119 deletions.
146 changes: 67 additions & 79 deletions format/jpeg/jpeg.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ func init() {
})
}

type marker struct {
symbol string
description string // TODO: use as description later
}

const (
SOF0 = 0xc0
SOF1 = 0xc1
Expand Down Expand Up @@ -103,71 +98,71 @@ const (
TEM = 0x01
)

var markers = map[uint]marker{
SOF0: {"SOF0", "Baseline DCT"},
SOF1: {"SOF1", "Extended sequential DCT"},
SOF2: {"SOF2", "Progressive DCT"},
SOF3: {"SOF3", "Lossless (sequential)"},
SOF5: {"SOF5", "Differential sequential DCT"},
SOF6: {"SOF6", "Differential progressive DCT"},
SOF7: {"SOF7", "Differential lossless (sequential)"},
JPG: {"JPG", "Reserved for JPEG extensions"},
SOF9: {"SOF9", "Extended sequential DCT"},
SOF10: {"SOF10", "Progressive DCT"},
SOF11: {"SOF11", "Lossless (sequential)"},
SOF13: {"SOF13", "Differential sequential DCT"},
SOF14: {"SOF14", "Differential progressive DCT"},
SOF15: {"SOF15", "Differential lossless (sequential)"},
DHT: {"DHT", "Define Huffman table(s)"},
DAC: {"DAC", "Define arithmetic coding conditioning(s)"},
RST0: {"RST0", "Restart with modulo 8 count 0"},
RST1: {"RST1", "Restart with modulo 8 count 1"},
RST2: {"RST2", "Restart with modulo 8 count 2"},
RST3: {"RST3", "Restart with modulo 8 count 3"},
RST4: {"RST4", "Restart with modulo 8 count 4"},
RST5: {"RST5", "Restart with modulo 8 count 5"},
RST6: {"RST6", "Restart with modulo 8 count 6"},
RST7: {"RST7", "Restart with modulo 8 count 7"},
SOI: {"SOI", "Start of image"},
EOI: {"EOI", "End of image true"},
SOS: {"SOS", "Start of scan"},
DQT: {"DQT", "Define quantization table(s)"},
DNL: {"DNL", "Define number of lines"},
DRI: {"DRI", "Define restart interval"},
DHP: {"DHP", "Define hierarchical progression"},
EXP: {"EXP", "Expand reference component(s)"},
APP0: {"APP0", "Reserved for application segments"},
APP1: {"APP1", "Reserved for application segments"},
APP2: {"APP2", "Reserved for application segments"},
APP3: {"APP3", "Reserved for application segments"},
APP4: {"APP4", "Reserved for application segments"},
APP5: {"APP5", "Reserved for application segments"},
APP6: {"APP6", "Reserved for application segments"},
APP7: {"APP7", "Reserved for application segments"},
APP8: {"APP8", "Reserved for application segments"},
APP9: {"APP9", "Reserved for application segments"},
APP10: {"APP10", "Reserved for application segments"},
APP11: {"APP11", "Reserved for application segments"},
APP12: {"APP12", "Reserved for application segments"},
APP13: {"APP13", "Reserved for application segments"},
APP14: {"APP14", "Reserved for application segments"},
APP15: {"APP15", "Reserved for application segments"},
JPG0: {"JPG0", "Reserved for JPEG extensions"},
JPG1: {"JPG1", "Reserved for JPEG extensions"},
JPG2: {"JPG2", "Reserved for JPEG extensions"},
JPG3: {"JPG3", "Reserved for JPEG extensions"},
JPG4: {"JPG4", "Reserved for JPEG extensions"},
JPG5: {"JPG5", "Reserved for JPEG extensions"},
JPG6: {"JPG6", "Reserved for JPEG extensions"},
JPG7: {"JPG7", "Reserved for JPEG extensions"},
JPG8: {"JPG8", "Reserved for JPEG extensions"},
JPG9: {"JPG9", "Reserved for JPEG extensions"},
JPG10: {"JPG10", "Reserved for JPEG extensions"},
JPG11: {"JPG11", "Reserved for JPEG extensions"},
JPG12: {"JPG12", "Reserved for JPEG extensions"},
JPG13: {"JPG13", "Reserved for JPEG extensions"},
COM: {"COM", "Comment"},
TEM: {"TEM", "For temporary private use in arithmetic coding"},
var markers = map[uint64]decode.Symbol{
SOF0: {Name: "SOF0", Desc: "Baseline DCT"},
SOF1: {Name: "SOF1", Desc: "Extended sequential DCT"},
SOF2: {Name: "SOF2", Desc: "Progressive DCT"},
SOF3: {Name: "SOF3", Desc: "Lossless (sequential)"},
SOF5: {Name: "SOF5", Desc: "Differential sequential DCT"},
SOF6: {Name: "SOF6", Desc: "Differential progressive DCT"},
SOF7: {Name: "SOF7", Desc: "Differential lossless (sequential)"},
JPG: {Name: "JPG", Desc: "Reserved for JPEG extensions"},
SOF9: {Name: "SOF9", Desc: "Extended sequential DCT"},
SOF10: {Name: "SOF10", Desc: "Progressive DCT"},
SOF11: {Name: "SOF11", Desc: "Lossless (sequential)"},
SOF13: {Name: "SOF13", Desc: "Differential sequential DCT"},
SOF14: {Name: "SOF14", Desc: "Differential progressive DCT"},
SOF15: {Name: "SOF15", Desc: "Differential lossless (sequential)"},
DHT: {Name: "DHT", Desc: "Define Huffman table(s)"},
DAC: {Name: "DAC", Desc: "Define arithmetic coding conditioning(s)"},
RST0: {Name: "RST0", Desc: "Restart with modulo 8 count 0"},
RST1: {Name: "RST1", Desc: "Restart with modulo 8 count 1"},
RST2: {Name: "RST2", Desc: "Restart with modulo 8 count 2"},
RST3: {Name: "RST3", Desc: "Restart with modulo 8 count 3"},
RST4: {Name: "RST4", Desc: "Restart with modulo 8 count 4"},
RST5: {Name: "RST5", Desc: "Restart with modulo 8 count 5"},
RST6: {Name: "RST6", Desc: "Restart with modulo 8 count 6"},
RST7: {Name: "RST7", Desc: "Restart with modulo 8 count 7"},
SOI: {Name: "SOI", Desc: "Start of image"},
EOI: {Name: "EOI", Desc: "End of image true"},
SOS: {Name: "SOS", Desc: "Start of scan"},
DQT: {Name: "DQT", Desc: "Define quantization table(s)"},
DNL: {Name: "DNL", Desc: "Define number of lines"},
DRI: {Name: "DRI", Desc: "Define restart interval"},
DHP: {Name: "DHP", Desc: "Define hierarchical progression"},
EXP: {Name: "EXP", Desc: "Expand reference component(s)"},
APP0: {Name: "APP0", Desc: "Reserved for application segments"},
APP1: {Name: "APP1", Desc: "Reserved for application segments"},
APP2: {Name: "APP2", Desc: "Reserved for application segments"},
APP3: {Name: "APP3", Desc: "Reserved for application segments"},
APP4: {Name: "APP4", Desc: "Reserved for application segments"},
APP5: {Name: "APP5", Desc: "Reserved for application segments"},
APP6: {Name: "APP6", Desc: "Reserved for application segments"},
APP7: {Name: "APP7", Desc: "Reserved for application segments"},
APP8: {Name: "APP8", Desc: "Reserved for application segments"},
APP9: {Name: "APP9", Desc: "Reserved for application segments"},
APP10: {Name: "APP10", Desc: "Reserved for application segments"},
APP11: {Name: "APP11", Desc: "Reserved for application segments"},
APP12: {Name: "APP12", Desc: "Reserved for application segments"},
APP13: {Name: "APP13", Desc: "Reserved for application segments"},
APP14: {Name: "APP14", Desc: "Reserved for application segments"},
APP15: {Name: "APP15", Desc: "Reserved for application segments"},
JPG0: {Name: "JPG0", Desc: "Reserved for JPEG extensions"},
JPG1: {Name: "JPG1", Desc: "Reserved for JPEG extensions"},
JPG2: {Name: "JPG2", Desc: "Reserved for JPEG extensions"},
JPG3: {Name: "JPG3", Desc: "Reserved for JPEG extensions"},
JPG4: {Name: "JPG4", Desc: "Reserved for JPEG extensions"},
JPG5: {Name: "JPG5", Desc: "Reserved for JPEG extensions"},
JPG6: {Name: "JPG6", Desc: "Reserved for JPEG extensions"},
JPG7: {Name: "JPG7", Desc: "Reserved for JPEG extensions"},
JPG8: {Name: "JPG8", Desc: "Reserved for JPEG extensions"},
JPG9: {Name: "JPG9", Desc: "Reserved for JPEG extensions"},
JPG10: {Name: "JPG10", Desc: "Reserved for JPEG extensions"},
JPG11: {Name: "JPG11", Desc: "Reserved for JPEG extensions"},
JPG12: {Name: "JPG12", Desc: "Reserved for JPEG extensions"},
JPG13: {Name: "JPG13", Desc: "Reserved for JPEG extensions"},
COM: {Name: "COM", Desc: "Comment"},
TEM: {Name: "TEM", Desc: "For temporary private use in arithmetic coding"},
}

func jpegDecode(d *decode.D, in interface{}) interface{} {
Expand Down Expand Up @@ -199,15 +194,8 @@ func jpegDecode(d *decode.D, in interface{}) interface{} {
d.FieldStructFn("marker", func(d *decode.D) {
prefixLen := d.PeekFindByte(0xff, -1) + 1
d.FieldBytesLen("prefix", int(prefixLen))
markerFound := false
markerCode := d.FieldUDescFn("code", func() (uint64, decode.DisplayFormat, string, string) {
n := uint(d.U8())
if m, ok := markers[n]; ok {
markerFound = true
return uint64(n), decode.NumberDecimal, m.symbol, m.description
}
return uint64(n), decode.NumberDecimal, "RES", "Reset"
})
markerCode, markerFound := d.FieldSymbolMapFn(
"code", markers, decode.Symbol{Name: "RES", Desc: "Reset"}, d.U8)

// RST*, SOI, EOI, TEM does not have a length field. All others have a
// 2 byte length read as "Lf", "Ls" etc or in the default case as "length".
Expand Down
8 changes: 4 additions & 4 deletions format/matroska/testdata/avc.fqtest
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ $ fq -d matroska verbose /avc.mkv
0x010| 2c| | ,| | rbsp_trailing_bits: 80 0x15.5-0x15.7 (0.3)
0x0180| 67 | g | forbidden_zero_bit: false 0x189-0x189 (0.1)
0x0180| 67 | g | nal_ref_idc: 3 0x189.1-0x189.2 (0.2)
0x0180| 67 | g | nal_unit_type: Sequence parameter set (7) 0x189.3-0x189.7 (0.5)
0x0180| 67 | g | nal_unit_type: SPS (7) (Sequence parameter set) 0x189.3-0x189.7 (0.5)
0x0180| f4 00 0d 91 9b 28| .....(| data: f4000d919b28283f6022000003000200... 0x18a-0x1a1.7 (24)
0x0190|28 3f 60 22 00 00 03 00 02 00 00 03 00 64 1e 28|(?`".........d.(|
0x01a0|53 2c |S, |
Expand Down Expand Up @@ -337,7 +337,7 @@ $ fq -d matroska verbose /avc.mkv
0x000| 44| | D| | rbsp_trailing_bits: 80 0x4.5-0x4.7 (0.3)
0x01a0| 68 | h | forbidden_zero_bit: false 0x1a5-0x1a5 (0.1)
0x01a0| 68 | h | nal_ref_idc: 3 0x1a5.1-0x1a5.2 (0.2)
0x01a0| 68 | h | nal_unit_type: Picture parameter set (8) 0x1a5.3-0x1a5.7 (0.5)
0x01a0| 68 | h | nal_unit_type: PPS (8) (Picture parameter set) 0x1a5.3-0x1a5.7 (0.5)
0x01a0| eb e3 c4 48 44 | ...HD | data: ebe3c44844 0x1a6-0x1aa.7 (5)
0x01a0| ff f8 f8 00 | .... | data: fff8f800 0x1ab-0x1ae.7 (4)
| | | [4]: element {} 0x1af-0x254.7 (166)
Expand Down Expand Up @@ -472,15 +472,15 @@ $ fq -d matroska verbose /avc.mkv
0x2a0| 80| | .| | rbsp_trailing_bits: 80 0x2ab-0x2ab.7 (1)
0x0260| 06| .| forbidden_zero_bit: false 0x26f-0x26f (0.1)
0x0260| 06| .| nal_ref_idc: 0 0x26f.1-0x26f.2 (0.2)
0x0260| 06| .| nal_unit_type: Supplemental enhancement information (SEI) (6) 0x26f.3-0x26f.7 (0.5)
0x0260| 06| .| nal_unit_type: SEI (6) (Supplemental enhancement information) 0x26f.3-0x26f.7 (0.5)
0x0270|05 ff ff a9 dc 45 e9 bd e6 d9 48 b7 96 2c d8 20|.....E....H..,. | data: 05ffffa9dc45e9bde6d948b7962cd820... 0x270-0x51b.7 (684)
* |until 0x51b.7 (684) | |
| | | [1]: nalu {} 0x51c-0xd2a.7 (2063)
0x0510| 00 00 08 0b| ....| length: 2059 0x51c-0x51f.7 (4)
| | | nalu: {} (avc_nalu) 0x520-0xd2a.7 (2059)
0x0520|65 |e | forbidden_zero_bit: false 0x520-0x520 (0.1)
0x0520|65 |e | nal_ref_idc: 3 0x520.1-0x520.2 (0.2)
0x0520|65 |e | nal_unit_type: Coded slice of an IDR picture (5) 0x520.3-0x520.7 (0.5)
0x0520|65 |e | nal_unit_type: IDR_SLICE (5) (Coded slice of an IDR picture) 0x520.3-0x520.7 (0.5)
| | | slice_header: {} 0x521-0x522 (1.1)
0x0520| 88 | . | first_mb_in_slice: 0 0x521-0x521 (0.1)
0x0520| 88 | . | slice_type: I (7) 0x521.1-0x521.7 (0.7)
Expand Down
8 changes: 4 additions & 4 deletions format/mp4/testdata/avc.fqtest
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ $ fq -d mp4 verbose /avc.mp4
0x010| 2c| | ,| | rbsp_trailing_bits: 80 0x15.5-0x15.7 (0.3)
0x0cf0| 67| g| forbidden_zero_bit: false 0xcff-0xcff (0.1)
0x0cf0| 67| g| nal_ref_idc: 3 0xcff.1-0xcff.2 (0.2)
0x0cf0| 67| g| nal_unit_type: Sequence parameter set (7) 0xcff.3-0xcff.7 (0.5)
0x0cf0| 67| g| nal_unit_type: SPS (7) (Sequence parameter set) 0xcff.3-0xcff.7 (0.5)
0x0d00|f4 00 0d 91 9b 28 28 3f 60 22 00 00 03 00 02 00|.....((?`"......| data: f4000d919b28283f6022000003000200... 0xd00-0xd17.7 (24)
0x0d10|00 03 00 64 1e 28 53 2c |...d.(S, |
0x0d10| 01 | . | num_of_picture_parameter_sets: 1 0xd18-0xd18.7 (1)
Expand Down Expand Up @@ -296,7 +296,7 @@ $ fq -d mp4 verbose /avc.mp4
0x000| 44| | D| | rbsp_trailing_bits: 80 0x4.5-0x4.7 (0.3)
0x0d10| 68 | h | forbidden_zero_bit: false 0xd1b-0xd1b (0.1)
0x0d10| 68 | h | nal_ref_idc: 3 0xd1b.1-0xd1b.2 (0.2)
0x0d10| 68 | h | nal_unit_type: Picture parameter set (8) 0xd1b.3-0xd1b.7 (0.5)
0x0d10| 68 | h | nal_unit_type: PPS (8) (Picture parameter set) 0xd1b.3-0xd1b.7 (0.5)
0x0d10| eb e3 c4 48| ...H| data: ebe3c44844 0xd1c-0xd20.7 (5)
0x0d20|44 |D |
0x0d20| ff f8 f8 00 | .... | data: fff8f800 0xd21-0xd24.7 (4)
Expand Down Expand Up @@ -408,7 +408,7 @@ $ fq -d mp4 verbose /avc.mp4
0x2a0| 80| | .| | rbsp_trailing_bits: 80 0x2ab-0x2ab.7 (1)
0x0030| 06 | . | forbidden_zero_bit: false 0x34-0x34 (0.1)
0x0030| 06 | . | nal_ref_idc: 0 0x34.1-0x34.2 (0.2)
0x0030| 06 | . | nal_unit_type: Supplemental enhancement information (SEI) (6) 0x34.3-0x34.7 (0.5)
0x0030| 06 | . | nal_unit_type: SEI (6) (Supplemental enhancement information) 0x34.3-0x34.7 (0.5)
0x0030| 05 ff ff a9 dc 45 e9 bd e6 d9 48| .....E....H| data: 05ffffa9dc45e9bde6d948b7962cd820... 0x35-0x2e0.7 (684)
0x0040|b7 96 2c d8 20 d9 23 ee ef 78 32 36 34 20 2d 20|..,. .#..x264 - |
* |until 0x2e0.7 (684) | |
Expand All @@ -417,7 +417,7 @@ $ fq -d mp4 verbose /avc.mp4
| | | nalu: {} (avc_nalu) 0x2e5-0xaef.7 (2059)
0x02e0| 65 | e | forbidden_zero_bit: false 0x2e5-0x2e5 (0.1)
0x02e0| 65 | e | nal_ref_idc: 3 0x2e5.1-0x2e5.2 (0.2)
0x02e0| 65 | e | nal_unit_type: Coded slice of an IDR picture (5) 0x2e5.3-0x2e5.7 (0.5)
0x02e0| 65 | e | nal_unit_type: IDR_SLICE (5) (Coded slice of an IDR picture) 0x2e5.3-0x2e5.7 (0.5)
| | | slice_header: {} 0x2e6-0x2e7 (1.1)
0x02e0| 88 | . | first_mb_in_slice: 0 0x2e6-0x2e6 (0.1)
0x02e0| 88 | . | slice_type: I (7) 0x2e6.1-0x2e6.7 (0.7)
Expand Down

0 comments on commit 9f55b6e

Please sign in to comment.