1// Copyright 2019 The libgav1 Authors 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15// Constants and utility functions used for inverse transform implementations. 16// This will be included inside an anonymous namespace on files where these are 17// necessary. 18 19// The value at index i is derived as: round(cos(pi * i / 128) * (1 << 12)). 20constexpr int16_t kCos128[65] = { 21 4096, 4095, 4091, 4085, 4076, 4065, 4052, 4036, 4017, 3996, 3973, 22 3948, 3920, 3889, 3857, 3822, 3784, 3745, 3703, 3659, 3612, 3564, 23 3513, 3461, 3406, 3349, 3290, 3229, 3166, 3102, 3035, 2967, 2896, 24 2824, 2751, 2675, 2598, 2520, 2440, 2359, 2276, 2191, 2106, 2019, 25 1931, 1842, 1751, 1660, 1567, 1474, 1380, 1285, 1189, 1092, 995, 26 897, 799, 700, 601, 501, 401, 301, 201, 101, 0}; 27 28inline int16_t Cos128(int angle) { 29 angle &= 0xff; 30 31 // If |angle| is 128, this function returns -4096 (= -2^12), which will 32 // cause the 32-bit multiplications in ButterflyRotation() to overflow if 33 // dst[a] or dst[b] is -2^19 (a possible corner case when |range| is 20): 34 // 35 // (-2^12) * (-2^19) = 2^31, which cannot be represented as an int32_t. 36 // 37 // Note: |range| is 20 when bitdepth is 12 and a row transform is performed. 38 // 39 // Assert that this angle is never used by DCT or ADST. 40 assert(angle != 128); 41 if (angle <= 64) return kCos128[angle]; 42 if (angle <= 128) return -kCos128[128 - angle]; 43 if (angle <= 192) return -kCos128[angle - 128]; 44 return kCos128[256 - angle]; 45} 46 47inline int16_t Sin128(int angle) { return Cos128(angle - 64); } 48 49// The value for index i is derived as: 50// round(sqrt(2) * sin(i * pi / 9) * 2 / 3 * (1 << 12)). 51constexpr int16_t kAdst4Multiplier[4] = {1321, 2482, 3344, 3803}; 52 53constexpr uint8_t kTransformRowShift[kNumTransformSizes] = { 54 0, 0, 1, 0, 1, 1, 2, 1, 1, 2, 1, 2, 2, 1, 2, 1, 2, 1, 2}; 55 56constexpr bool kShouldRound[kNumTransformSizes] = { 57 false, true, false, true, false, true, false, false, true, false, 58 true, false, false, true, false, true, false, true, false}; 59 60constexpr int16_t kIdentity4Multiplier /* round(2^12 * sqrt(2)) */ = 0x16A1; 61constexpr int16_t kIdentity4MultiplierFraction /* round(2^12 * (sqrt(2) - 1))*/ 62 = 0x6A1; 63constexpr int16_t kIdentity16Multiplier /* 2 * round(2^12 * sqrt(2)) */ = 11586; 64constexpr int16_t kTransformRowMultiplier /* round(2^12 / sqrt(2)) */ = 2896; 65