/src/libwebp/src/dec/quant_dec.c
Line | Count | Source |
1 | | // Copyright 2010 Google Inc. All Rights Reserved. |
2 | | // |
3 | | // Use of this source code is governed by a BSD-style license |
4 | | // that can be found in the COPYING file in the root of the source |
5 | | // tree. An additional intellectual property rights grant can be found |
6 | | // in the file PATENTS. All contributing project authors may |
7 | | // be found in the AUTHORS file in the root of the source tree. |
8 | | // ----------------------------------------------------------------------------- |
9 | | // |
10 | | // Quantizer initialization |
11 | | // |
12 | | // Author: Skal (pascal.massimino@gmail.com) |
13 | | |
14 | | #include "src/dec/common_dec.h" |
15 | | #include "src/dec/vp8_dec.h" |
16 | | #include "src/dec/vp8i_dec.h" |
17 | | #include "src/utils/bit_reader_utils.h" |
18 | | #include "src/webp/types.h" |
19 | | |
20 | | WEBP_ASSUME_UNSAFE_INDEXABLE_ABI |
21 | | |
22 | 16.5k | static WEBP_INLINE int clip(int v, int M) { return v < 0 ? 0 : v > M ? M : v; } |
23 | | |
24 | | // Paragraph 14.1 |
25 | | static const uint8_t kDcTable[128] = { |
26 | | 4, 5, 6, 7, 8, 9, 10, 10, 11, 12, 13, 14, 15, 16, 17, |
27 | | 17, 18, 19, 20, 20, 21, 21, 22, 22, 23, 23, 24, 25, 25, 26, |
28 | | 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 37, 38, 39, 40, |
29 | | 41, 42, 43, 44, 45, 46, 46, 47, 48, 49, 50, 51, 52, 53, 54, |
30 | | 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, |
31 | | 70, 71, 72, 73, 74, 75, 76, 76, 77, 78, 79, 80, 81, 82, 83, |
32 | | 84, 85, 86, 87, 88, 89, 91, 93, 95, 96, 98, 100, 101, 102, 104, |
33 | | 106, 108, 110, 112, 114, 116, 118, 122, 124, 126, 128, 130, 132, 134, 136, |
34 | | 138, 140, 143, 145, 148, 151, 154, 157}; |
35 | | |
36 | | static const uint16_t kAcTable[128] = { |
37 | | 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, |
38 | | 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, |
39 | | 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, |
40 | | 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 60, 62, 64, 66, 68, |
41 | | 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, |
42 | | 100, 102, 104, 106, 108, 110, 112, 114, 116, 119, 122, 125, 128, 131, 134, |
43 | | 137, 140, 143, 146, 149, 152, 155, 158, 161, 164, 167, 170, 173, 177, 181, |
44 | | 185, 189, 193, 197, 201, 205, 209, 213, 217, 221, 225, 229, 234, 239, 245, |
45 | | 249, 254, 259, 264, 269, 274, 279, 284}; |
46 | | |
47 | | //------------------------------------------------------------------------------ |
48 | | // Paragraph 9.6 |
49 | | |
50 | 1.78k | void VP8ParseQuant(VP8Decoder* const dec) { |
51 | 1.78k | VP8BitReader* const br = &dec->br; |
52 | 1.78k | const int base_q0 = VP8GetValue(br, 7, "global-header"); |
53 | 1.78k | const int dqy1_dc = VP8Get(br, "global-header") |
54 | 1.78k | ? VP8GetSignedValue(br, 4, "global-header") |
55 | 1.78k | : 0; |
56 | 1.78k | const int dqy2_dc = VP8Get(br, "global-header") |
57 | 1.78k | ? VP8GetSignedValue(br, 4, "global-header") |
58 | 1.78k | : 0; |
59 | 1.78k | const int dqy2_ac = VP8Get(br, "global-header") |
60 | 1.78k | ? VP8GetSignedValue(br, 4, "global-header") |
61 | 1.78k | : 0; |
62 | 1.78k | const int dquv_dc = VP8Get(br, "global-header") |
63 | 1.78k | ? VP8GetSignedValue(br, 4, "global-header") |
64 | 1.78k | : 0; |
65 | 1.78k | const int dquv_ac = VP8Get(br, "global-header") |
66 | 1.78k | ? VP8GetSignedValue(br, 4, "global-header") |
67 | 1.78k | : 0; |
68 | | |
69 | 1.78k | const VP8SegmentHeader* const hdr = &dec->segment_hdr; |
70 | 1.78k | int i; |
71 | | |
72 | 8.90k | for (i = 0; i < NUM_MB_SEGMENTS; ++i) { |
73 | 7.12k | int q; |
74 | 7.12k | if (hdr->use_segment) { |
75 | 1.31k | q = hdr->quantizer[i]; |
76 | 1.31k | if (!hdr->absolute_delta) { |
77 | 396 | q += base_q0; |
78 | 396 | } |
79 | 5.80k | } else { |
80 | 5.80k | if (i > 0) { |
81 | 4.35k | dec->dqm[i] = dec->dqm[0]; |
82 | 4.35k | continue; |
83 | 4.35k | } else { |
84 | 1.45k | q = base_q0; |
85 | 1.45k | } |
86 | 5.80k | } |
87 | 2.76k | { |
88 | 2.76k | VP8QuantMatrix* const m = &dec->dqm[i]; |
89 | 2.76k | m->y1_mat[0] = kDcTable[clip(q + dqy1_dc, 127)]; |
90 | 2.76k | m->y1_mat[1] = kAcTable[clip(q + 0, 127)]; |
91 | | |
92 | 2.76k | m->y2_mat[0] = kDcTable[clip(q + dqy2_dc, 127)] * 2; |
93 | | // For all x in [0..284], x*155/100 is bitwise equal to (x*101581) >> 16. |
94 | | // The smallest precision for that is '(x*6349) >> 12' but 16 is a good |
95 | | // word size. |
96 | 2.76k | m->y2_mat[1] = (kAcTable[clip(q + dqy2_ac, 127)] * 101581) >> 16; |
97 | 2.76k | if (m->y2_mat[1] < 8) m->y2_mat[1] = 8; |
98 | | |
99 | 2.76k | m->uv_mat[0] = kDcTable[clip(q + dquv_dc, 117)]; |
100 | 2.76k | m->uv_mat[1] = kAcTable[clip(q + dquv_ac, 127)]; |
101 | | |
102 | 2.76k | m->uv_quant = q + dquv_ac; // for dithering strength evaluation |
103 | 2.76k | } |
104 | 2.76k | } |
105 | 1.78k | } |
106 | | |
107 | | //------------------------------------------------------------------------------ |