sharpyuv_gamma.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. // Copyright 2022 Google Inc. All Rights Reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style license
  4. // that can be found in the COPYING file in the root of the source
  5. // tree. An additional intellectual property rights grant can be found
  6. // in the file PATENTS. All contributing project authors may
  7. // be found in the AUTHORS file in the root of the source tree.
  8. // -----------------------------------------------------------------------------
  9. //
  10. // Gamma correction utilities.
  11. #include "sharpyuv/sharpyuv_gamma.h"
  12. #include <assert.h>
  13. #include <float.h>
  14. #include <math.h>
  15. #include "sharpyuv/sharpyuv.h"
  16. #include "src/webp/types.h"
  17. // Gamma correction compensates loss of resolution during chroma subsampling.
  18. // Size of pre-computed table for converting from gamma to linear.
  19. #define GAMMA_TO_LINEAR_TAB_BITS 10
  20. #define GAMMA_TO_LINEAR_TAB_SIZE (1 << GAMMA_TO_LINEAR_TAB_BITS)
  21. static uint32_t kGammaToLinearTabS[GAMMA_TO_LINEAR_TAB_SIZE + 2];
  22. #define LINEAR_TO_GAMMA_TAB_BITS 9
  23. #define LINEAR_TO_GAMMA_TAB_SIZE (1 << LINEAR_TO_GAMMA_TAB_BITS)
  24. static uint32_t kLinearToGammaTabS[LINEAR_TO_GAMMA_TAB_SIZE + 2];
  25. static const double kGammaF = 1. / 0.45;
  26. #define GAMMA_TO_LINEAR_BITS 16
  27. static volatile int kGammaTablesSOk = 0;
  28. void SharpYuvInitGammaTables(void) {
  29. assert(GAMMA_TO_LINEAR_BITS <= 16);
  30. if (!kGammaTablesSOk) {
  31. int v;
  32. const double a = 0.09929682680944;
  33. const double thresh = 0.018053968510807;
  34. const double final_scale = 1 << GAMMA_TO_LINEAR_BITS;
  35. // Precompute gamma to linear table.
  36. {
  37. const double norm = 1. / GAMMA_TO_LINEAR_TAB_SIZE;
  38. const double a_rec = 1. / (1. + a);
  39. for (v = 0; v <= GAMMA_TO_LINEAR_TAB_SIZE; ++v) {
  40. const double g = norm * v;
  41. double value;
  42. if (g <= thresh * 4.5) {
  43. value = g / 4.5;
  44. } else {
  45. value = pow(a_rec * (g + a), kGammaF);
  46. }
  47. kGammaToLinearTabS[v] = (uint32_t)(value * final_scale + .5);
  48. }
  49. // to prevent small rounding errors to cause read-overflow:
  50. kGammaToLinearTabS[GAMMA_TO_LINEAR_TAB_SIZE + 1] =
  51. kGammaToLinearTabS[GAMMA_TO_LINEAR_TAB_SIZE];
  52. }
  53. // Precompute linear to gamma table.
  54. {
  55. const double scale = 1. / LINEAR_TO_GAMMA_TAB_SIZE;
  56. for (v = 0; v <= LINEAR_TO_GAMMA_TAB_SIZE; ++v) {
  57. const double g = scale * v;
  58. double value;
  59. if (g <= thresh) {
  60. value = 4.5 * g;
  61. } else {
  62. value = (1. + a) * pow(g, 1. / kGammaF) - a;
  63. }
  64. kLinearToGammaTabS[v] =
  65. (uint32_t)(final_scale * value + 0.5);
  66. }
  67. // to prevent small rounding errors to cause read-overflow:
  68. kLinearToGammaTabS[LINEAR_TO_GAMMA_TAB_SIZE + 1] =
  69. kLinearToGammaTabS[LINEAR_TO_GAMMA_TAB_SIZE];
  70. }
  71. kGammaTablesSOk = 1;
  72. }
  73. }
  74. static WEBP_INLINE int Shift(int v, int shift) {
  75. return (shift >= 0) ? (v << shift) : (v >> -shift);
  76. }
  77. static WEBP_INLINE uint32_t FixedPointInterpolation(int v, uint32_t* tab,
  78. int tab_pos_shift_right,
  79. int tab_value_shift) {
  80. const uint32_t tab_pos = Shift(v, -tab_pos_shift_right);
  81. // fractional part, in 'tab_pos_shift' fixed-point precision
  82. const uint32_t x = v - (tab_pos << tab_pos_shift_right); // fractional part
  83. // v0 / v1 are in kGammaToLinearBits fixed-point precision (range [0..1])
  84. const uint32_t v0 = Shift(tab[tab_pos + 0], tab_value_shift);
  85. const uint32_t v1 = Shift(tab[tab_pos + 1], tab_value_shift);
  86. // Final interpolation.
  87. const uint32_t v2 = (v1 - v0) * x; // note: v1 >= v0.
  88. const int half =
  89. (tab_pos_shift_right > 0) ? 1 << (tab_pos_shift_right - 1) : 0;
  90. const uint32_t result = v0 + ((v2 + half) >> tab_pos_shift_right);
  91. return result;
  92. }
  93. static uint32_t ToLinearSrgb(uint16_t v, int bit_depth) {
  94. const int shift = GAMMA_TO_LINEAR_TAB_BITS - bit_depth;
  95. if (shift > 0) {
  96. return kGammaToLinearTabS[v << shift];
  97. }
  98. return FixedPointInterpolation(v, kGammaToLinearTabS, -shift, 0);
  99. }
  100. static uint16_t FromLinearSrgb(uint32_t value, int bit_depth) {
  101. return FixedPointInterpolation(
  102. value, kLinearToGammaTabS,
  103. (GAMMA_TO_LINEAR_BITS - LINEAR_TO_GAMMA_TAB_BITS),
  104. bit_depth - GAMMA_TO_LINEAR_BITS);
  105. }
  106. ////////////////////////////////////////////////////////////////////////////////
  107. #define CLAMP(x, low, high) \
  108. (((x) < (low)) ? (low) : (((high) < (x)) ? (high) : (x)))
  109. #define MIN(a, b) (((a) < (b)) ? (a) : (b))
  110. #define MAX(a, b) (((a) > (b)) ? (a) : (b))
  111. static WEBP_INLINE float Roundf(float x) {
  112. if (x < 0)
  113. return (float)ceil((double)(x - 0.5f));
  114. else
  115. return (float)floor((double)(x + 0.5f));
  116. }
  117. static WEBP_INLINE float Powf(float base, float exp) {
  118. return (float)pow((double)base, (double)exp);
  119. }
  120. static WEBP_INLINE float Log10f(float x) { return (float)log10((double)x); }
  121. static float ToLinear709(float gamma) {
  122. if (gamma < 0.f) {
  123. return 0.f;
  124. } else if (gamma < 4.5f * 0.018053968510807f) {
  125. return gamma / 4.5f;
  126. } else if (gamma < 1.f) {
  127. return Powf((gamma + 0.09929682680944f) / 1.09929682680944f, 1.f / 0.45f);
  128. }
  129. return 1.f;
  130. }
  131. static float FromLinear709(float linear) {
  132. if (linear < 0.f) {
  133. return 0.f;
  134. } else if (linear < 0.018053968510807f) {
  135. return linear * 4.5f;
  136. } else if (linear < 1.f) {
  137. return 1.09929682680944f * Powf(linear, 0.45f) - 0.09929682680944f;
  138. }
  139. return 1.f;
  140. }
  141. static float ToLinear470M(float gamma) {
  142. return Powf(CLAMP(gamma, 0.f, 1.f), 2.2f);
  143. }
  144. static float FromLinear470M(float linear) {
  145. return Powf(CLAMP(linear, 0.f, 1.f), 1.f / 2.2f);
  146. }
  147. static float ToLinear470Bg(float gamma) {
  148. return Powf(CLAMP(gamma, 0.f, 1.f), 2.8f);
  149. }
  150. static float FromLinear470Bg(float linear) {
  151. return Powf(CLAMP(linear, 0.f, 1.f), 1.f / 2.8f);
  152. }
  153. static float ToLinearSmpte240(float gamma) {
  154. if (gamma < 0.f) {
  155. return 0.f;
  156. } else if (gamma < 4.f * 0.022821585529445f) {
  157. return gamma / 4.f;
  158. } else if (gamma < 1.f) {
  159. return Powf((gamma + 0.111572195921731f) / 1.111572195921731f, 1.f / 0.45f);
  160. }
  161. return 1.f;
  162. }
  163. static float FromLinearSmpte240(float linear) {
  164. if (linear < 0.f) {
  165. return 0.f;
  166. } else if (linear < 0.022821585529445f) {
  167. return linear * 4.f;
  168. } else if (linear < 1.f) {
  169. return 1.111572195921731f * Powf(linear, 0.45f) - 0.111572195921731f;
  170. }
  171. return 1.f;
  172. }
  173. static float ToLinearLog100(float gamma) {
  174. // The function is non-bijective so choose the middle of [0, 0.01].
  175. const float mid_interval = 0.01f / 2.f;
  176. return (gamma <= 0.0f) ? mid_interval
  177. : Powf(10.0f, 2.f * (MIN(gamma, 1.f) - 1.0f));
  178. }
  179. static float FromLinearLog100(float linear) {
  180. return (linear < 0.01f) ? 0.0f : 1.0f + Log10f(MIN(linear, 1.f)) / 2.0f;
  181. }
  182. static float ToLinearLog100Sqrt10(float gamma) {
  183. // The function is non-bijective so choose the middle of [0, 0.00316227766f[.
  184. const float mid_interval = 0.00316227766f / 2.f;
  185. return (gamma <= 0.0f) ? mid_interval
  186. : Powf(10.0f, 2.5f * (MIN(gamma, 1.f) - 1.0f));
  187. }
  188. static float FromLinearLog100Sqrt10(float linear) {
  189. return (linear < 0.00316227766f) ? 0.0f
  190. : 1.0f + Log10f(MIN(linear, 1.f)) / 2.5f;
  191. }
  192. static float ToLinearIec61966(float gamma) {
  193. if (gamma <= -4.5f * 0.018053968510807f) {
  194. return Powf((-gamma + 0.09929682680944f) / -1.09929682680944f, 1.f / 0.45f);
  195. } else if (gamma < 4.5f * 0.018053968510807f) {
  196. return gamma / 4.5f;
  197. }
  198. return Powf((gamma + 0.09929682680944f) / 1.09929682680944f, 1.f / 0.45f);
  199. }
  200. static float FromLinearIec61966(float linear) {
  201. if (linear <= -0.018053968510807f) {
  202. return -1.09929682680944f * Powf(-linear, 0.45f) + 0.09929682680944f;
  203. } else if (linear < 0.018053968510807f) {
  204. return linear * 4.5f;
  205. }
  206. return 1.09929682680944f * Powf(linear, 0.45f) - 0.09929682680944f;
  207. }
  208. static float ToLinearBt1361(float gamma) {
  209. if (gamma < -0.25f) {
  210. return -0.25f;
  211. } else if (gamma < 0.f) {
  212. return Powf((gamma - 0.02482420670236f) / -0.27482420670236f, 1.f / 0.45f) /
  213. -4.f;
  214. } else if (gamma < 4.5f * 0.018053968510807f) {
  215. return gamma / 4.5f;
  216. } else if (gamma < 1.f) {
  217. return Powf((gamma + 0.09929682680944f) / 1.09929682680944f, 1.f / 0.45f);
  218. }
  219. return 1.f;
  220. }
  221. static float FromLinearBt1361(float linear) {
  222. if (linear < -0.25f) {
  223. return -0.25f;
  224. } else if (linear < 0.f) {
  225. return -0.27482420670236f * Powf(-4.f * linear, 0.45f) + 0.02482420670236f;
  226. } else if (linear < 0.018053968510807f) {
  227. return linear * 4.5f;
  228. } else if (linear < 1.f) {
  229. return 1.09929682680944f * Powf(linear, 0.45f) - 0.09929682680944f;
  230. }
  231. return 1.f;
  232. }
  233. static float ToLinearPq(float gamma) {
  234. if (gamma > 0.f) {
  235. const float pow_gamma = Powf(gamma, 32.f / 2523.f);
  236. const float num = MAX(pow_gamma - 107.f / 128.f, 0.0f);
  237. const float den = MAX(2413.f / 128.f - 2392.f / 128.f * pow_gamma, FLT_MIN);
  238. return Powf(num / den, 4096.f / 653.f);
  239. }
  240. return 0.f;
  241. }
  242. static float FromLinearPq(float linear) {
  243. if (linear > 0.f) {
  244. const float pow_linear = Powf(linear, 653.f / 4096.f);
  245. const float num = 107.f / 128.f + 2413.f / 128.f * pow_linear;
  246. const float den = 1.0f + 2392.f / 128.f * pow_linear;
  247. return Powf(num / den, 2523.f / 32.f);
  248. }
  249. return 0.f;
  250. }
  251. static float ToLinearSmpte428(float gamma) {
  252. return Powf(MAX(gamma, 0.f), 2.6f) / 0.91655527974030934f;
  253. }
  254. static float FromLinearSmpte428(float linear) {
  255. return Powf(0.91655527974030934f * MAX(linear, 0.f), 1.f / 2.6f);
  256. }
  257. // Conversion in BT.2100 requires RGB info. Simplify to gamma correction here.
  258. static float ToLinearHlg(float gamma) {
  259. if (gamma < 0.f) {
  260. return 0.f;
  261. } else if (gamma <= 0.5f) {
  262. return Powf((gamma * gamma) * (1.f / 3.f), 1.2f);
  263. }
  264. return Powf((expf((gamma - 0.55991073f) / 0.17883277f) + 0.28466892f) / 12.0f,
  265. 1.2f);
  266. }
  267. static float FromLinearHlg(float linear) {
  268. linear = Powf(linear, 1.f / 1.2f);
  269. if (linear < 0.f) {
  270. return 0.f;
  271. } else if (linear <= (1.f / 12.f)) {
  272. return sqrtf(3.f * linear);
  273. }
  274. return 0.17883277f * logf(12.f * linear - 0.28466892f) + 0.55991073f;
  275. }
  276. uint32_t SharpYuvGammaToLinear(uint16_t v, int bit_depth,
  277. SharpYuvTransferFunctionType transfer_type) {
  278. float v_float, linear;
  279. if (transfer_type == kSharpYuvTransferFunctionSrgb) {
  280. return ToLinearSrgb(v, bit_depth);
  281. }
  282. v_float = (float)v / ((1 << bit_depth) - 1);
  283. switch (transfer_type) {
  284. case kSharpYuvTransferFunctionBt709:
  285. case kSharpYuvTransferFunctionBt601:
  286. case kSharpYuvTransferFunctionBt2020_10Bit:
  287. case kSharpYuvTransferFunctionBt2020_12Bit:
  288. linear = ToLinear709(v_float);
  289. break;
  290. case kSharpYuvTransferFunctionBt470M:
  291. linear = ToLinear470M(v_float);
  292. break;
  293. case kSharpYuvTransferFunctionBt470Bg:
  294. linear = ToLinear470Bg(v_float);
  295. break;
  296. case kSharpYuvTransferFunctionSmpte240:
  297. linear = ToLinearSmpte240(v_float);
  298. break;
  299. case kSharpYuvTransferFunctionLinear:
  300. return v;
  301. case kSharpYuvTransferFunctionLog100:
  302. linear = ToLinearLog100(v_float);
  303. break;
  304. case kSharpYuvTransferFunctionLog100_Sqrt10:
  305. linear = ToLinearLog100Sqrt10(v_float);
  306. break;
  307. case kSharpYuvTransferFunctionIec61966:
  308. linear = ToLinearIec61966(v_float);
  309. break;
  310. case kSharpYuvTransferFunctionBt1361:
  311. linear = ToLinearBt1361(v_float);
  312. break;
  313. case kSharpYuvTransferFunctionSmpte2084:
  314. linear = ToLinearPq(v_float);
  315. break;
  316. case kSharpYuvTransferFunctionSmpte428:
  317. linear = ToLinearSmpte428(v_float);
  318. break;
  319. case kSharpYuvTransferFunctionHlg:
  320. linear = ToLinearHlg(v_float);
  321. break;
  322. default:
  323. assert(0);
  324. linear = 0;
  325. break;
  326. }
  327. return (uint32_t)Roundf(linear * ((1 << 16) - 1));
  328. }
  329. uint16_t SharpYuvLinearToGamma(uint32_t v, int bit_depth,
  330. SharpYuvTransferFunctionType transfer_type) {
  331. float v_float, linear;
  332. if (transfer_type == kSharpYuvTransferFunctionSrgb) {
  333. return FromLinearSrgb(v, bit_depth);
  334. }
  335. v_float = (float)v / ((1 << 16) - 1);
  336. switch (transfer_type) {
  337. case kSharpYuvTransferFunctionBt709:
  338. case kSharpYuvTransferFunctionBt601:
  339. case kSharpYuvTransferFunctionBt2020_10Bit:
  340. case kSharpYuvTransferFunctionBt2020_12Bit:
  341. linear = FromLinear709(v_float);
  342. break;
  343. case kSharpYuvTransferFunctionBt470M:
  344. linear = FromLinear470M(v_float);
  345. break;
  346. case kSharpYuvTransferFunctionBt470Bg:
  347. linear = FromLinear470Bg(v_float);
  348. break;
  349. case kSharpYuvTransferFunctionSmpte240:
  350. linear = FromLinearSmpte240(v_float);
  351. break;
  352. case kSharpYuvTransferFunctionLinear:
  353. return v;
  354. case kSharpYuvTransferFunctionLog100:
  355. linear = FromLinearLog100(v_float);
  356. break;
  357. case kSharpYuvTransferFunctionLog100_Sqrt10:
  358. linear = FromLinearLog100Sqrt10(v_float);
  359. break;
  360. case kSharpYuvTransferFunctionIec61966:
  361. linear = FromLinearIec61966(v_float);
  362. break;
  363. case kSharpYuvTransferFunctionBt1361:
  364. linear = FromLinearBt1361(v_float);
  365. break;
  366. case kSharpYuvTransferFunctionSmpte2084:
  367. linear = FromLinearPq(v_float);
  368. break;
  369. case kSharpYuvTransferFunctionSmpte428:
  370. linear = FromLinearSmpte428(v_float);
  371. break;
  372. case kSharpYuvTransferFunctionHlg:
  373. linear = FromLinearHlg(v_float);
  374. break;
  375. default:
  376. assert(0);
  377. linear = 0;
  378. break;
  379. }
  380. return (uint16_t)Roundf(linear * ((1 << bit_depth) - 1));
  381. }