sharpyuv_gamma.c 13 KB

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