1 /*****************************************************************************
2 * Copyright (C) 2013 x265 project
4 * Authors: Steve Borho <steve@borho.org>
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.
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.
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.
20 * This program is also available under a commercial proprietary license.
21 * For more information, contact us at license @ x265.com.
22 *****************************************************************************/
37 /* all weights and factors stored as FIX8 */
40 uint64_t m_cbDistortionWeight
;
41 uint64_t m_crDistortionWeight
;
45 void setPsyRdScale(double scale
) { m_psyRd
= (uint32_t)floor(256.0 * scale
* 0.33); }
46 void setCbDistortionWeight(uint16_t weightFix8
) { m_cbDistortionWeight
= weightFix8
; }
47 void setCrDistortionWeight(uint16_t weightFix8
) { m_crDistortionWeight
= weightFix8
; }
49 void setQP(const Slice
& slice
, int qp
)
53 setLambda(x265_lambda2_tab
[qp
], x265_lambda_tab
[qp
]);
55 int qpCb
= Clip3(QP_MIN
, QP_MAX_MAX
, qp
+ slice
.m_pps
->chromaCbQpOffset
);
56 int chroma_offset_idx
= X265_MIN(qp
- qpCb
+ 12, MAX_CHROMA_LAMBDA_OFFSET
);
57 uint16_t lambdaOffset
= m_psyRd
? x265_chroma_lambda2_offset_tab
[chroma_offset_idx
] : 256;
58 setCbDistortionWeight(lambdaOffset
);
60 int qpCr
= Clip3(QP_MIN
, QP_MAX_MAX
, qp
+ slice
.m_pps
->chromaCrQpOffset
);
61 chroma_offset_idx
= X265_MIN(qp
- qpCr
+ 12, MAX_CHROMA_LAMBDA_OFFSET
);
62 lambdaOffset
= m_psyRd
? x265_chroma_lambda2_offset_tab
[chroma_offset_idx
] : 256;
63 setCrDistortionWeight(lambdaOffset
);
66 void setLambda(double lambda2
, double lambda
)
68 m_lambda2
= (uint64_t)floor(256.0 * lambda2
);
69 m_lambda
= (uint64_t)floor(256.0 * lambda
);
72 inline uint64_t calcRdCost(uint32_t distortion
, uint32_t bits
) const
74 X265_CHECK(bits
<= (UINT64_MAX
- 128) / m_lambda2
,
75 "calcRdCost wrap detected dist: %d, bits %d, lambda: %d\n", distortion
, bits
, (int)m_lambda2
);
76 return distortion
+ ((bits
* m_lambda2
+ 128) >> 8);
79 /* return the difference in energy between the source block and the recon block */
80 inline int psyCost(int size
, pixel
*source
, intptr_t sstride
, pixel
*recon
, intptr_t rstride
) const
82 return primitives
.psy_cost_pp
[size
](source
, sstride
, recon
, rstride
);
85 /* return the difference in energy between the source block and the recon block */
86 inline int psyCost(int size
, int16_t *source
, intptr_t sstride
, int16_t *recon
, intptr_t rstride
) const
88 return primitives
.psy_cost_ss
[size
](source
, sstride
, recon
, rstride
);
91 /* return the RD cost of this prediction, including the effect of psy-rd */
92 inline uint64_t calcPsyRdCost(uint32_t distortion
, uint32_t bits
, uint32_t psycost
) const
94 return distortion
+ ((m_lambda
* m_psyRd
* psycost
) >> 16) + ((bits
* m_lambda2
) >> 8);
97 inline uint64_t calcRdSADCost(uint32_t sadCost
, uint32_t bits
) const
99 X265_CHECK(bits
<= (UINT64_MAX
- 128) / m_lambda
,
100 "calcRdSADCost wrap detected dist: %d, bits %d, lambda: "X265_LL
"\n", sadCost
, bits
, m_lambda
);
101 return sadCost
+ ((bits
* m_lambda
+ 128) >> 8);
104 inline uint32_t scaleChromaDistCb(uint32_t dist
) const
106 X265_CHECK(dist
<= (UINT64_MAX
- 128) / m_cbDistortionWeight
,
107 "scaleChromaDistCb wrap detected dist: %d, lambda: "X265_LL
"\n", dist
, m_cbDistortionWeight
);
108 return (uint32_t)(((dist
* m_cbDistortionWeight
) + 128) >> 8);
111 inline uint32_t scaleChromaDistCr(uint32_t dist
) const
113 X265_CHECK(dist
<= (UINT64_MAX
- 128) / m_crDistortionWeight
,
114 "scaleChromaDistCr wrap detected dist: %d, lambda: "X265_LL
"\n", dist
, m_crDistortionWeight
);
115 return (uint32_t)(((dist
* m_crDistortionWeight
) + 128) >> 8);
118 inline uint32_t getCost(uint32_t bits
) const
120 return (uint32_t)((bits
* m_lambda
+ 128) >> 8);
125 #endif // ifndef X265_TCOMRDCOST_H