1 /*
2  * Copyright 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <memory>
20 #include <vector>
21 #include "array2d.h"
22 
23 namespace android {
24 
25 template <typename Torig, typename Tdct>
26 class FastDCT {
27 public:
28     explicit FastDCT(int block_size);
29 
30     void ForwardTransform1D(const std::vector<Torig> &v_in, std::vector<Tdct> *v_out) const;
31     void ReverseTransform1D(const std::vector<Tdct> &v_in, std::vector<Torig> *v_out) const;
32     void ForwardTransform2D(const Array2D<Torig> &m_in, Array2D<Tdct> *m_out) const;
33     void ReverseTransform2D(const Array2D<Tdct> &m_in, Array2D<Torig> *m_out) const;
34 
block_size()35     int block_size() const { return block_size_; }
intermediate_scale_factor()36     double intermediate_scale_factor() const { return intermediate_scale_factor_; }
37 
set_intermediate_scale_factor(double val)38     void set_intermediate_scale_factor(double val) { intermediate_scale_factor_ = val; }
39 
40 private:
41     // Dimensionality of input/output space for 1D transform.
42     const int block_size_;
43 
44     // DCT coefficients will be scaled by this factor during the forward
45     // transform, then divided by this factor during the reverse transform.
46     double intermediate_scale_factor_;
47 
48     // Scaling factors for making transform orthonormal.
49     const double orth_scale_factor_;
50     const double sqrt_orth_scale_factor_;
51 
52     // Square root of one-half gets used a lot so define it as a constant.
53     const double sqrt_one_half_;
54 
55     // Array for passing data into and out of external C ddct2d routine.
56     std::unique_ptr<double *[]> ddct2d_in_out_ptr_array_;
57 
58     // Above pointer array will point into this contiguous block.
59     std::unique_ptr<double[]> contiguous_double_array_;
60 
61     // Table of cosine and sine values used by external C ddct routine.
62     std::unique_ptr<double[]> cos_sin_table_;
63 
64     // Array used for passing data into and out of ddct.
65     std::unique_ptr<double[]> ddct_in_out_;
66 
67     // Scratch workspace for use by ddct2d.
68     std::unique_ptr<double[]> t_workspace_;
69 
70     // Array used to compute the bit-reversal patterns in ddct.
71     std::unique_ptr<int[]> bit_reversal_workspace_;
72 };
73 } // namespace android
74