Coverage Report

Created: 2026-05-16 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/x265/source/encoder/rdcost.h
Line
Count
Source
1
/*****************************************************************************
2
* Copyright (C) 2013-2020 MulticoreWare, Inc
3
*
4
* Authors: Steve Borho <steve@borho.org>
5
*          Min Chen <chenm003@163.com>
6
*
7
* This program is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as published by
9
* the Free Software Foundation; either version 2 of the License, or
10
* (at your option) any later version.
11
*
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
* GNU General Public License for more details.
16
*
17
* You should have received a copy of the GNU General Public License
18
* along with this program; if not, write to the Free Software
19
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
20
*
21
* This program is also available under a commercial proprietary license.
22
* For more information, contact us at license @ x265.com.
23
*****************************************************************************/
24
25
#ifndef X265_RDCOST_H
26
#define X265_RDCOST_H
27
28
#include "common.h"
29
#include "slice.h"
30
31
namespace X265_NS {
32
// private namespace
33
34
class RDCost
35
{
36
public:
37
38
    /* all weights and factors stored as FIX8 */
39
    uint64_t  m_lambda2;
40
    uint64_t  m_lambda;
41
    uint32_t  m_chromaDistWeight[2];
42
    uint32_t  m_psyRdBase;
43
    uint32_t  m_psyRd;
44
    uint32_t  m_ssimRd;
45
    int       m_qp; /* QP used to configure lambda, may be higher than QP_MAX_SPEC but <= QP_MAX_MAX */
46
47
0
    void setPsyRdScale(double scale)                { m_psyRdBase = (uint32_t)floor(65536.0 * scale * 0.33); }
48
0
    void setSsimRd(int ssimRd) { m_ssimRd = ssimRd; };
49
50
    void setQP(const Slice& slice, int qp)
51
0
    {
52
0
        x265_emms(); /* TODO: if the lambda tables were ints, this would not be necessary */
53
0
        m_qp = qp;
54
0
        setLambda(x265_lambda2_tab[qp], x265_lambda_tab[qp]);
55
56
        /* Scale PSY RD factor by a slice type factor */
57
0
        static const uint32_t psyScaleFix8[3] = { 300, 256, 96 }; /* B, P, I */
58
0
        m_psyRd = (m_psyRdBase * psyScaleFix8[slice.m_sliceType]) >> 8;
59
60
        /* Scale PSY RD factor by QP, at high QP psy-rd can cause artifacts */
61
0
        if (qp >= 40)
62
0
        {
63
0
            int scale = qp >= QP_MAX_SPEC ? 0 : (QP_MAX_SPEC - qp) * 23;
64
0
            m_psyRd = (m_psyRd * scale) >> 8;
65
0
        }
66
67
0
        int qpCb, qpCr;
68
0
        if (slice.m_sps->chromaFormatIdc == X265_CSP_I420)
69
0
        {
70
0
            qpCb = (int)g_chromaScale[x265_clip3(QP_MIN, QP_MAX_MAX, qp + slice.m_pps->chromaQpOffset[0] + slice.m_chromaQpOffset[0])];
71
0
            qpCr = (int)g_chromaScale[x265_clip3(QP_MIN, QP_MAX_MAX, qp + slice.m_pps->chromaQpOffset[1] + slice.m_chromaQpOffset[1])];
72
0
        }
73
0
        else
74
0
        {
75
0
            qpCb = x265_clip3(QP_MIN, QP_MAX_SPEC, qp + slice.m_pps->chromaQpOffset[0] + slice.m_chromaQpOffset[0]);
76
0
            qpCr = x265_clip3(QP_MIN, QP_MAX_SPEC, qp + slice.m_pps->chromaQpOffset[1] + slice.m_chromaQpOffset[1]);
77
0
        }
78
79
0
        int chroma_offset_idx = X265_MIN(qp - qpCb + 12, MAX_CHROMA_LAMBDA_OFFSET);
80
0
        uint16_t lambdaOffset = x265_chroma_lambda2_offset_tab[chroma_offset_idx];
81
0
        m_chromaDistWeight[0] = lambdaOffset;
82
83
0
        chroma_offset_idx = X265_MIN(qp - qpCr + 12, MAX_CHROMA_LAMBDA_OFFSET);
84
0
        lambdaOffset = x265_chroma_lambda2_offset_tab[chroma_offset_idx];
85
0
        m_chromaDistWeight[1] = lambdaOffset;
86
0
    }
87
88
    void setLambda(double lambda2, double lambda)
89
0
    {
90
0
        m_lambda2 = (uint64_t)floor(256.0 * lambda2);
91
0
        m_lambda = (uint64_t)floor(256.0 * lambda);
92
0
    }
93
94
    inline uint64_t calcRdCost(sse_t distortion, uint32_t bits) const
95
0
    {
96
0
#if X265_DEPTH < 10
97
0
        X265_CHECK(bits <= (UINT64_MAX - 128) / m_lambda2,
98
0
                   "calcRdCost wrap detected dist: %u, bits %u, lambda: " X265_LL "\n",
99
0
                   distortion, bits, m_lambda2);
100
#else
101
        X265_CHECK(bits <= (UINT64_MAX - 128) / m_lambda2,
102
                   "calcRdCost wrap detected dist: " X265_LL ", bits %u, lambda: " X265_LL "\n",
103
                   distortion, bits, m_lambda2);
104
#endif
105
0
        return distortion + ((bits * m_lambda2 + 128) >> 8);
106
0
    }
107
108
    /* return the difference in energy between the source block and the recon block */
109
    inline int psyCost(int size, const pixel* source, intptr_t sstride, const pixel* recon, intptr_t rstride) const
110
0
    {
111
0
        return primitives.cu[size].psy_cost_pp(source, sstride, recon, rstride);
112
0
    }
113
114
    /* return the RD cost of this prediction, including the effect of psy-rd */
115
    inline uint64_t calcPsyRdCost(sse_t distortion, uint32_t bits, uint32_t psycost) const
116
0
    {
117
0
#if X265_DEPTH < 10
118
0
        X265_CHECK((bits <= (UINT64_MAX / m_lambda2)) && (psycost <= UINT64_MAX / (m_lambda * m_psyRd)),
119
0
                   "calcPsyRdCost wrap detected dist: %u, bits: %u, lambda: " X265_LL ", lambda2: " X265_LL "\n",
120
0
                   distortion, bits, m_lambda, m_lambda2);
121
#else
122
        X265_CHECK((bits <= (UINT64_MAX / m_lambda2)) && (psycost <= UINT64_MAX / (m_lambda * m_psyRd)),
123
                   "calcPsyRdCost wrap detected dist: " X265_LL ", bits: %u, lambda: " X265_LL ", lambda2: " X265_LL "\n",
124
                   distortion, bits, m_lambda, m_lambda2);
125
#endif
126
0
        return distortion + ((m_lambda * m_psyRd * psycost) >> 24) + ((bits * m_lambda2) >> 8);
127
0
    }
128
129
    inline uint64_t calcSsimRdCost(uint64_t distortion, uint32_t bits, uint32_t ssimCost) const
130
0
    {
131
0
#if X265_DEPTH < 10
132
0
        X265_CHECK((bits <= (UINT64_MAX / m_lambda2)) && (ssimCost <= UINT64_MAX / m_lambda),
133
0
                   "calcPsyRdCost wrap detected dist: " X265_LL " bits: %u, lambda: " X265_LL ", lambda2: " X265_LL "\n",
134
0
                   distortion, bits, m_lambda, m_lambda2);
135
#else
136
        X265_CHECK((bits <= (UINT64_MAX / m_lambda2)) && (ssimCost <= UINT64_MAX / m_lambda),
137
                   "calcPsyRdCost wrap detected dist: " X265_LL ", bits: %u, lambda: " X265_LL ", lambda2: " X265_LL "\n",
138
                   distortion, bits, m_lambda, m_lambda2);
139
#endif
140
0
        return distortion + ((m_lambda * ssimCost) >> 14) + ((bits * m_lambda2) >> 8);
141
0
    }
142
143
    inline uint64_t calcRdSADCost(uint32_t sadCost, uint32_t bits) const
144
0
    {
145
0
        X265_CHECK(bits <= (UINT64_MAX - 128) / m_lambda,
146
0
                   "calcRdSADCost wrap detected dist: %u, bits %u, lambda: " X265_LL "\n", sadCost, bits, m_lambda);
147
0
        return sadCost + ((bits * m_lambda + 128) >> 8);
148
0
    }
149
150
    inline sse_t scaleChromaDist(uint32_t plane, sse_t dist) const
151
0
    {
152
0
#if X265_DEPTH < 10
153
0
        X265_CHECK(dist <= (UINT64_MAX - 128) / m_chromaDistWeight[plane - 1],
154
0
                   "scaleChromaDist wrap detected dist: %u, lambda: %u\n",
155
0
                   dist, m_chromaDistWeight[plane - 1]);
156
#else
157
        X265_CHECK(dist <= (UINT64_MAX - 128) / m_chromaDistWeight[plane - 1],
158
                   "scaleChromaDist wrap detected dist: " X265_LL " lambda: %u\n",
159
                   dist, m_chromaDistWeight[plane - 1]);
160
#endif
161
0
        return (sse_t)((dist * (uint64_t)m_chromaDistWeight[plane - 1] + 128) >> 8);
162
0
    }
163
164
    inline uint32_t getCost(uint32_t bits) const
165
0
    {
166
0
        X265_CHECK(bits <= (UINT64_MAX - 128) / m_lambda,
167
0
                   "getCost wrap detected bits: %u, lambda: " X265_LL "\n", bits, m_lambda);
168
0
        return (uint32_t)((bits * m_lambda + 128) >> 8);
169
0
    }
170
};
171
}
172
173
#endif // ifndef X265_TCOMRDCOST_H