quant.go

 1// Copyright 2011 The Go Authors. All rights reserved.
 2// Use of this source code is governed by a BSD-style
 3// license that can be found in the LICENSE file.
 4
 5package vp8
 6
 7// This file implements parsing the quantization factors.
 8
 9// quant are DC/AC quantization factors.
10type quant struct {
11	y1 [2]uint16
12	y2 [2]uint16
13	uv [2]uint16
14}
15
16// clip clips x to the range [min, max] inclusive.
17func clip(x, min, max int32) int32 {
18	if x < min {
19		return min
20	}
21	if x > max {
22		return max
23	}
24	return x
25}
26
27// parseQuant parses the quantization factors, as specified in section 9.6.
28func (d *Decoder) parseQuant() {
29	baseQ0 := d.fp.readUint(uniformProb, 7)
30	dqy1DC := d.fp.readOptionalInt(uniformProb, 4)
31	const dqy1AC = 0
32	dqy2DC := d.fp.readOptionalInt(uniformProb, 4)
33	dqy2AC := d.fp.readOptionalInt(uniformProb, 4)
34	dquvDC := d.fp.readOptionalInt(uniformProb, 4)
35	dquvAC := d.fp.readOptionalInt(uniformProb, 4)
36	for i := 0; i < nSegment; i++ {
37		q := int32(baseQ0)
38		if d.segmentHeader.useSegment {
39			if d.segmentHeader.relativeDelta {
40				q += int32(d.segmentHeader.quantizer[i])
41			} else {
42				q = int32(d.segmentHeader.quantizer[i])
43			}
44		}
45		d.quant[i].y1[0] = dequantTableDC[clip(q+dqy1DC, 0, 127)]
46		d.quant[i].y1[1] = dequantTableAC[clip(q+dqy1AC, 0, 127)]
47		d.quant[i].y2[0] = dequantTableDC[clip(q+dqy2DC, 0, 127)] * 2
48		d.quant[i].y2[1] = dequantTableAC[clip(q+dqy2AC, 0, 127)] * 155 / 100
49		if d.quant[i].y2[1] < 8 {
50			d.quant[i].y2[1] = 8
51		}
52		// The 117 is not a typo. The dequant_init function in the spec's Reference
53		// Decoder Source Code (http://tools.ietf.org/html/rfc6386#section-9.6 Page 145)
54		// says to clamp the LHS value at 132, which is equal to dequantTableDC[117].
55		d.quant[i].uv[0] = dequantTableDC[clip(q+dquvDC, 0, 117)]
56		d.quant[i].uv[1] = dequantTableAC[clip(q+dquvAC, 0, 127)]
57	}
58}
59
60// The dequantization tables are specified in section 14.1.
61var (
62	dequantTableDC = [128]uint16{
63		4, 5, 6, 7, 8, 9, 10, 10,
64		11, 12, 13, 14, 15, 16, 17, 17,
65		18, 19, 20, 20, 21, 21, 22, 22,
66		23, 23, 24, 25, 25, 26, 27, 28,
67		29, 30, 31, 32, 33, 34, 35, 36,
68		37, 37, 38, 39, 40, 41, 42, 43,
69		44, 45, 46, 46, 47, 48, 49, 50,
70		51, 52, 53, 54, 55, 56, 57, 58,
71		59, 60, 61, 62, 63, 64, 65, 66,
72		67, 68, 69, 70, 71, 72, 73, 74,
73		75, 76, 76, 77, 78, 79, 80, 81,
74		82, 83, 84, 85, 86, 87, 88, 89,
75		91, 93, 95, 96, 98, 100, 101, 102,
76		104, 106, 108, 110, 112, 114, 116, 118,
77		122, 124, 126, 128, 130, 132, 134, 136,
78		138, 140, 143, 145, 148, 151, 154, 157,
79	}
80	dequantTableAC = [128]uint16{
81		4, 5, 6, 7, 8, 9, 10, 11,
82		12, 13, 14, 15, 16, 17, 18, 19,
83		20, 21, 22, 23, 24, 25, 26, 27,
84		28, 29, 30, 31, 32, 33, 34, 35,
85		36, 37, 38, 39, 40, 41, 42, 43,
86		44, 45, 46, 47, 48, 49, 50, 51,
87		52, 53, 54, 55, 56, 57, 58, 60,
88		62, 64, 66, 68, 70, 72, 74, 76,
89		78, 80, 82, 84, 86, 88, 90, 92,
90		94, 96, 98, 100, 102, 104, 106, 108,
91		110, 112, 114, 116, 119, 122, 125, 128,
92		131, 134, 137, 140, 143, 146, 149, 152,
93		155, 158, 161, 164, 167, 170, 173, 177,
94		181, 185, 189, 193, 197, 201, 205, 209,
95		213, 217, 221, 225, 229, 234, 239, 245,
96		249, 254, 259, 264, 269, 274, 279, 284,
97	}
98)