From 92eb3e85a51e92e7a2f9faf8d35566f8c977ec0d Mon Sep 17 00:00:00 2001 From: Saxon Date: Sat, 7 Sep 2019 17:55:34 +0930 Subject: [PATCH] codec/h264/h264dec: created tokenMap type to make things more readable --- codec/h264/h264dec/cavlc.go | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/codec/h264/h264dec/cavlc.go b/codec/h264/h264dec/cavlc.go index faee36b0..b3d276d6 100644 --- a/codec/h264/h264dec/cavlc.go +++ b/codec/h264/h264dec/cavlc.go @@ -49,7 +49,7 @@ func init() { 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 { 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 nBits := nZeros for { - vars, ok := coeffTokenMap[nCIdx][nZeros][int(val)] + vars, ok := coeffTokenMaps[nCIdx][nZeros][int(val)] if ok { trailingOnes = vars[0] 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. 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 // 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 // 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, // coeffTokenMap representation of table 9-5 from the specifications using lines // read from a corresponding CSV file coefftokenmap.csv. -func formCoeffTokenMap(lines [][]string) ([nColumns]map[int]map[int][2]int, error) { - var tokenMap [nColumns]map[int]map[int][2]int +func formCoeffTokenMap(lines [][]string) ([nColumns]tokenMap, error) { + var maps [nColumns]tokenMap - for i := range tokenMap { - tokenMap[i] = make(map[int]map[int][2]int) + for i := range maps { + maps[i] = make(tokenMap) } for _, line := range lines { trailingOnes, err := strconv.Atoi(line[0]) 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]) 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 @@ -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). val, err := binToInt(v[nZeros:]) 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 // into the map for the coeff_token leading zeros and value. - if tokenMap[j][nZeros] == nil { - tokenMap[j][nZeros] = make(map[int][2]int) + if maps[j][nZeros] == nil { + 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