codec/h264/h264dec: created tokenMap type to make things more readable

This commit is contained in:
Saxon 2019-09-07 17:55:34 +09:30
parent 8316a621b9
commit 92eb3e85a5
1 changed files with 19 additions and 14 deletions

View File

@ -49,7 +49,7 @@ func init() {
panic(fmt.Sprintf("could not read lines from coeftokenmap.csv file, failed with error: %v", err)) panic(fmt.Sprintf("could not read lines from coeftokenmap.csv file, failed with error: %v", err))
} }
coeffTokenMap, err = formCoeffTokenMap(lines) coeffTokenMaps, err = formCoeffTokenMap(lines)
if err != nil { if err != nil {
panic(fmt.Sprintf("could not form coeff_token map, failed with err: %v", err)) panic(fmt.Sprintf("could not form coeff_token map, failed with err: %v", err))
} }
@ -202,7 +202,7 @@ func readCoeffToken(br *bits.BitReader, nC int) (trailingOnes, totalCoeff, coeff
val := b val := b
nBits := nZeros nBits := nZeros
for { for {
vars, ok := coeffTokenMap[nCIdx][nZeros][int(val)] vars, ok := coeffTokenMaps[nCIdx][nZeros][int(val)]
if ok { if ok {
trailingOnes = vars[0] trailingOnes = vars[0]
totalCoeff = vars[1] totalCoeff = vars[1]
@ -247,31 +247,36 @@ func available(b block) bool {
// representative of the number of defined nC ranges defined in table 9-5. // representative of the number of defined nC ranges defined in table 9-5.
const nColumns = 6 const nColumns = 6
// coeffMapping maps values of coeff_token to values of TrailingOnes(coeff_token)
// and TotalCoeff(coeff_token) given as map[ coeff_token val ][ 0 for trailing ones
// and 1 for totalCoef ]
type tokenMap map[int]map[int][2]int
// coeffTokenMap will a representation of table 9-5 from the specifications, and // coeffTokenMap will a representation of table 9-5 from the specifications, and
// is indexed as follows, coeffToken[ nC group ][ number of coeff_token leading // is indexed as follows, coeffToken[ nC group ][ number of coeff_token leading
// zeros ][ value of coeff_token ][ 0 for TrailingOnes(coeff_token) and 1 for // zeros ][ value of coeff_token ][ 0 for TrailingOnes(coeff_token) and 1 for
// TotalCoef(coeff_token) ]. // TotalCoef(coeff_token) ].
var coeffTokenMap [nColumns]map[int]map[int][2]int var coeffTokenMaps [nColumns]tokenMap
// formCoeffTokenMap populates the global [nColumns]map[int]map[int][2]int, // formCoeffTokenMap populates the global [nColumns]map[int]map[int][2]int,
// coeffTokenMap representation of table 9-5 from the specifications using lines // coeffTokenMap representation of table 9-5 from the specifications using lines
// read from a corresponding CSV file coefftokenmap.csv. // read from a corresponding CSV file coefftokenmap.csv.
func formCoeffTokenMap(lines [][]string) ([nColumns]map[int]map[int][2]int, error) { func formCoeffTokenMap(lines [][]string) ([nColumns]tokenMap, error) {
var tokenMap [nColumns]map[int]map[int][2]int var maps [nColumns]tokenMap
for i := range tokenMap { for i := range maps {
tokenMap[i] = make(map[int]map[int][2]int) maps[i] = make(tokenMap)
} }
for _, line := range lines { for _, line := range lines {
trailingOnes, err := strconv.Atoi(line[0]) trailingOnes, err := strconv.Atoi(line[0])
if err != nil { if err != nil {
return tokenMap, fmt.Errorf("could not convert trailingOnes string to int, failed with error: %v", err) return maps, fmt.Errorf("could not convert trailingOnes string to int, failed with error: %v", err)
} }
totalCoeff, err := strconv.Atoi(line[1]) totalCoeff, err := strconv.Atoi(line[1])
if err != nil { if err != nil {
return tokenMap, fmt.Errorf("could not convert totalCoeff string to int, failed with error: %v", err) return maps, fmt.Errorf("could not convert totalCoeff string to int, failed with error: %v", err)
} }
// For each column in this row, therefore each nC category, load the // For each column in this row, therefore each nC category, load the
@ -298,18 +303,18 @@ func formCoeffTokenMap(lines [][]string) ([nColumns]map[int]map[int][2]int, erro
// This will be the value of the coeff_token (without leading zeros). // This will be the value of the coeff_token (without leading zeros).
val, err := binToInt(v[nZeros:]) val, err := binToInt(v[nZeros:])
if err != nil { if err != nil {
return tokenMap, fmt.Errorf("could not get value of remaining binary, failed with error: %v", err) return maps, fmt.Errorf("could not get value of remaining binary, failed with error: %v", err)
} }
// Add the TrailingOnes(coeff_token) and TotalCoeff(coeff_token) values // Add the TrailingOnes(coeff_token) and TotalCoeff(coeff_token) values
// into the map for the coeff_token leading zeros and value. // into the map for the coeff_token leading zeros and value.
if tokenMap[j][nZeros] == nil { if maps[j][nZeros] == nil {
tokenMap[j][nZeros] = make(map[int][2]int) maps[j][nZeros] = make(map[int][2]int)
} }
tokenMap[j][nZeros][val] = [2]int{trailingOnes, totalCoeff} maps[j][nZeros][val] = [2]int{trailingOnes, totalCoeff}
} }
} }
return tokenMap, nil return maps, nil
} }
// parseLevelPrefix parses the level_prefix variable as specified by the process // parseLevelPrefix parses the level_prefix variable as specified by the process