codec/h264/h264dec/cavlc.go: removed redundant if checks in parseLevelInformation

This commit is contained in:
Saxon 2019-09-08 13:31:10 +09:30
parent ce803ba488
commit 960d41cb4f
1 changed files with 63 additions and 67 deletions

View File

@ -51,18 +51,16 @@ func parseLevelPrefix(br *bits.BitReader) (int, error) {
func parseLevelInformation(br *bits.BitReader, totalCoeff, trailingOnes int) ([]int, error) {
var levelVal []int
var i int
if trailingOnes != 0 {
for ; i < trailingOnes; i++ {
b, err := br.ReadBits(1)
if err != nil {
return nil, fmt.Errorf("could not read trailing_ones_sign_flag, failed with error: %v", err)
}
if b == 0 {
levelVal = append(levelVal, 1)
continue
}
levelVal = append(levelVal, -1)
for ; i < trailingOnes; i++ {
b, err := br.ReadBits(1)
if err != nil {
return nil, fmt.Errorf("could not read trailing_ones_sign_flag, failed with error: %v", err)
}
if b == 0 {
levelVal = append(levelVal, 1)
continue
}
levelVal = append(levelVal, -1)
}
var suffixLen int
@ -75,63 +73,61 @@ func parseLevelInformation(br *bits.BitReader, totalCoeff, trailingOnes int) ([]
return nil, errors.New("invalid TotalCoeff and TrailingOnes combination")
}
if totalCoeff-trailingOnes != 0 {
for j := 0; j < totalCoeff-trailingOnes; j++ {
levelPrefix, err := parseLevelPrefix(br)
if err != nil {
return nil, fmt.Errorf("could not parse level prefix, failed with error: %v", err)
}
var levelSuffixSize int
switch {
case levelPrefix == 14 && suffixLen == 0:
levelSuffixSize = 4
case levelPrefix >= 15:
levelSuffixSize = levelPrefix - 3
default:
levelSuffixSize = suffixLen
}
var levelSuffix int
if levelSuffixSize > 0 {
b, err := br.ReadBits(levelSuffixSize)
if err != nil {
return nil, fmt.Errorf("could not parse levelSuffix, failed with error: %v", err)
}
levelSuffix = int(b)
} else {
levelSuffix = 0
}
levelCode := (mini(15, levelPrefix) << uint(suffixLen)) + levelSuffix
if levelPrefix >= 15 && suffixLen == 0 {
levelCode += 15
}
if levelPrefix >= 16 {
levelCode += (1 << uint(levelPrefix-3)) - 4096
}
if i == trailingOnes && trailingOnes < 3 {
levelCode += 2
}
if levelCode%2 == 0 {
levelVal = append(levelVal, (levelCode+2)>>1)
} else {
levelVal = append(levelVal, (-levelCode-1)>>1)
}
if suffixLen == 0 {
suffixLen = 1
}
if absi(levelVal[i]) > (3<<uint(suffixLen-1)) && suffixLen < 6 {
suffixLen++
}
i++
for j := 0; j < totalCoeff-trailingOnes; j++ {
levelPrefix, err := parseLevelPrefix(br)
if err != nil {
return nil, fmt.Errorf("could not parse level prefix, failed with error: %v", err)
}
var levelSuffixSize int
switch {
case levelPrefix == 14 && suffixLen == 0:
levelSuffixSize = 4
case levelPrefix >= 15:
levelSuffixSize = levelPrefix - 3
default:
levelSuffixSize = suffixLen
}
var levelSuffix int
if levelSuffixSize > 0 {
b, err := br.ReadBits(levelSuffixSize)
if err != nil {
return nil, fmt.Errorf("could not parse levelSuffix, failed with error: %v", err)
}
levelSuffix = int(b)
} else {
levelSuffix = 0
}
levelCode := (mini(15, levelPrefix) << uint(suffixLen)) + levelSuffix
if levelPrefix >= 15 && suffixLen == 0 {
levelCode += 15
}
if levelPrefix >= 16 {
levelCode += (1 << uint(levelPrefix-3)) - 4096
}
if i == trailingOnes && trailingOnes < 3 {
levelCode += 2
}
if levelCode%2 == 0 {
levelVal = append(levelVal, (levelCode+2)>>1)
} else {
levelVal = append(levelVal, (-levelCode-1)>>1)
}
if suffixLen == 0 {
suffixLen = 1
}
if absi(levelVal[i]) > (3<<uint(suffixLen-1)) && suffixLen < 6 {
suffixLen++
}
i++
}
return levelVal, nil
}