Imported Upstream version 1.4
[deb_x265.git] / source / common / quant.h
1 /*****************************************************************************
2 * Copyright (C) 2014 x265 project
3 *
4 * Authors: Steve Borho <steve@borho.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
19 *
20 * This program is also available under a commercial proprietary license.
21 * For more information, contact us at license @ x265.com.
22 *****************************************************************************/
23
24 #ifndef X265_QUANT_H
25 #define X265_QUANT_H
26
27 #include "common.h"
28 #include "scalinglist.h"
29 #include "contexts.h"
30
31 namespace x265 {
32 // private namespace
33
34 class CUData;
35 class Entropy;
36 struct TUEntropyCodingParameters;
37
38 struct QpParam
39 {
40 int rem;
41 int per;
42 int qp;
43 int64_t lambda2; /* FIX8 */
44 int64_t lambda; /* FIX8 */
45
46 QpParam() : qp(MAX_INT) {}
47
48 void setQpParam(int qpScaled)
49 {
50 if (qp != qpScaled)
51 {
52 rem = qpScaled % 6;
53 per = qpScaled / 6;
54 qp = qpScaled;
55 lambda2 = (int64_t)(x265_lambda2_tab[qp - QP_BD_OFFSET] * 256. + 0.5);
56 lambda = (int64_t)(x265_lambda_tab[qp - QP_BD_OFFSET] * 256. + 0.5);
57 }
58 }
59 };
60
61 class Quant
62 {
63 protected:
64
65 const ScalingList* m_scalingList;
66 Entropy* m_entropyCoder;
67
68 QpParam m_qpParam[3];
69
70 bool m_useRDOQ;
71 int64_t m_psyRdoqScale;
72 int32_t* m_resiDctCoeff;
73 int32_t* m_fencDctCoeff;
74 int16_t* m_fencShortBuf;
75
76 enum { IEP_RATE = 32768 }; /* FIX15 cost of an equal probable bit */
77
78 public:
79
80 NoiseReduction* m_nr;
81 NoiseReduction* m_frameNr; // Array of NR structures, one for each frameEncoder
82
83 Quant();
84 ~Quant();
85
86 /* one-time setup */
87 bool init(bool useRDOQ, double psyScale, const ScalingList& scalingList, Entropy& entropy);
88 bool allocNoiseReduction(const x265_param& param);
89
90 /* CU setup */
91 void setQPforQuant(const CUData& ctu);
92
93 uint32_t transformNxN(CUData& cu, pixel *fenc, uint32_t fencstride, int16_t* residual, uint32_t stride, coeff_t* coeff,
94 uint32_t log2TrSize, TextType ttype, uint32_t absPartIdx, bool useTransformSkip);
95
96 void invtransformNxN(bool transQuantBypass, int16_t* residual, uint32_t stride, coeff_t* coeff,
97 uint32_t log2TrSize, TextType ttype, bool bIntra, bool useTransformSkip, uint32_t numSig);
98
99 /* static methods shared with entropy.cpp */
100 static uint32_t calcPatternSigCtx(uint64_t sigCoeffGroupFlag64, uint32_t cgPosX, uint32_t cgPosY, uint32_t log2TrSizeCG);
101 static uint32_t getSigCtxInc(uint32_t patternSigCtx, uint32_t log2TrSize, uint32_t trSize, uint32_t blkPos, bool bIsLuma, uint32_t firstSignificanceMapContext);
102 static uint32_t getSigCoeffGroupCtxInc(uint64_t sigCoeffGroupFlag64, uint32_t cgPosX, uint32_t cgPosY, uint32_t log2TrSizeCG);
103
104 protected:
105
106 void setChromaQP(int qpin, TextType ttype, int chFmt);
107
108 uint32_t signBitHidingHDQ(int16_t* qcoeff, int32_t* deltaU, uint32_t numSig, const TUEntropyCodingParameters &codingParameters);
109
110 uint32_t rdoQuant(CUData& cu, int16_t* dstCoeff, uint32_t log2TrSize, TextType ttype, uint32_t absPartIdx, bool usePsy);
111 inline uint32_t getRateLast(uint32_t posx, uint32_t posy) const;
112 };
113
114 static inline uint32_t getGroupIdx(const uint32_t idx)
115 {
116 // TODO: Why is this not a table lookup?
117
118 uint32_t group = (idx >> 3);
119
120 if (idx >= 24)
121 group = 2;
122 uint32_t groupIdx = ((idx >> (group + 1)) - 2) + 4 + (group << 1);
123 if (idx <= 3)
124 groupIdx = idx;
125
126 #ifdef _DEBUG
127 static const uint8_t g_groupIdx[32] = { 0, 1, 2, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9 };
128 assert(groupIdx == g_groupIdx[idx]);
129 #endif
130
131 return groupIdx;
132 }
133
134 }
135
136 #endif // ifndef X265_QUANT_H