sharpyuv_gamma.c 13 KB

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