color_utils.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * Copyright (c) 2015 Kevin Wheatley <kevin.j.wheatley@gmail.com>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg 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 GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <stddef.h>
  21. #include <math.h>
  22. #include "common.h"
  23. #include "libavutil/color_utils.h"
  24. #include "libavutil/pixfmt.h"
  25. double avpriv_get_gamma_from_trc(enum AVColorTransferCharacteristic trc)
  26. {
  27. double gamma;
  28. switch (trc) {
  29. case AVCOL_TRC_BT709:
  30. case AVCOL_TRC_SMPTE170M:
  31. case AVCOL_TRC_SMPTE240M:
  32. case AVCOL_TRC_BT1361_ECG:
  33. case AVCOL_TRC_BT2020_10:
  34. case AVCOL_TRC_BT2020_12:
  35. /* these share a segmented TRC, but gamma 1.961 is a close
  36. approximation, and also more correct for decoding content */
  37. gamma = 1.961;
  38. break;
  39. case AVCOL_TRC_GAMMA22:
  40. case AVCOL_TRC_IEC61966_2_1:
  41. gamma = 2.2;
  42. break;
  43. case AVCOL_TRC_GAMMA28:
  44. gamma = 2.8;
  45. break;
  46. case AVCOL_TRC_LINEAR:
  47. gamma = 1.0;
  48. break;
  49. default:
  50. gamma = 0.0; // Unknown value representation
  51. }
  52. return gamma;
  53. }
  54. #define BT709_alpha 1.099296826809442
  55. #define BT709_beta 0.018053968510807
  56. static double avpriv_trc_bt709(double Lc)
  57. {
  58. const double a = BT709_alpha;
  59. const double b = BT709_beta;
  60. return (0.0 > Lc) ? 0.0
  61. : ( b > Lc) ? 4.500 * Lc
  62. : a * pow(Lc, 0.45) - (a - 1.0);
  63. }
  64. static double avpriv_trc_gamma22(double Lc)
  65. {
  66. return (0.0 > Lc) ? 0.0 : pow(Lc, 1.0/ 2.2);
  67. }
  68. static double avpriv_trc_gamma28(double Lc)
  69. {
  70. return (0.0 > Lc) ? 0.0 : pow(Lc, 1.0/ 2.8);
  71. }
  72. static double avpriv_trc_smpte240M(double Lc)
  73. {
  74. const double a = 1.1115;
  75. const double b = 0.0228;
  76. return (0.0 > Lc) ? 0.0
  77. : ( b > Lc) ? 4.000 * Lc
  78. : a * pow(Lc, 0.45) - (a - 1.0);
  79. }
  80. static double avpriv_trc_linear(double Lc)
  81. {
  82. return Lc;
  83. }
  84. static double avpriv_trc_log(double Lc)
  85. {
  86. return (0.01 > Lc) ? 0.0 : 1.0 + log10(Lc) / 2.0;
  87. }
  88. static double avpriv_trc_log_sqrt(double Lc)
  89. {
  90. // sqrt(10) / 1000
  91. return (0.00316227766 > Lc) ? 0.0 : 1.0 + log10(Lc) / 2.5;
  92. }
  93. static double avpriv_trc_iec61966_2_4(double Lc)
  94. {
  95. const double a = BT709_alpha;
  96. const double b = BT709_beta;
  97. return (-b >= Lc) ? -a * pow(-Lc, 0.45) + (a - 1.0)
  98. : ( b > Lc) ? 4.500 * Lc
  99. : a * pow( Lc, 0.45) - (a - 1.0);
  100. }
  101. static double avpriv_trc_bt1361(double Lc)
  102. {
  103. const double a = BT709_alpha;
  104. const double b = BT709_beta;
  105. return (-0.0045 >= Lc) ? -(a * pow(-4.0 * Lc, 0.45) + (a - 1.0)) / 4.0
  106. : ( b > Lc) ? 4.500 * Lc
  107. : a * pow( Lc, 0.45) - (a - 1.0);
  108. }
  109. static double avpriv_trc_iec61966_2_1(double Lc)
  110. {
  111. const double a = 1.055;
  112. const double b = 0.0031308;
  113. return (0.0 > Lc) ? 0.0
  114. : ( b > Lc) ? 12.92 * Lc
  115. : a * pow(Lc, 1.0 / 2.4) - (a - 1.0);
  116. }
  117. static double avpriv_trc_smpte_st2084(double Lc)
  118. {
  119. const double c1 = 3424.0 / 4096.0; // c3-c2 + 1
  120. const double c2 = 32.0 * 2413.0 / 4096.0;
  121. const double c3 = 32.0 * 2392.0 / 4096.0;
  122. const double m = 128.0 * 2523.0 / 4096.0;
  123. const double n = 0.25 * 2610.0 / 4096.0;
  124. const double L = Lc / 10000.0;
  125. const double Ln = pow(L, n);
  126. return (0.0 > Lc) ? 0.0
  127. : pow((c1 + c2 * Ln) / (1.0 + c3 * Ln), m);
  128. }
  129. static double avpriv_trc_smpte_st428_1(double Lc)
  130. {
  131. return (0.0 > Lc) ? 0.0
  132. : pow(48.0 * Lc / 52.37, 1.0 / 2.6);
  133. }
  134. static double avpriv_trc_arib_std_b67(double Lc) {
  135. // The function uses the definition from HEVC, which assumes that the peak
  136. // white is input level = 1. (this is equivalent to scaling E = Lc * 12 and
  137. // using the definition from the ARIB STD-B67 spec)
  138. const double a = 0.17883277;
  139. const double b = 0.28466892;
  140. const double c = 0.55991073;
  141. return (0.0 > Lc) ? 0.0 :
  142. (Lc <= 1.0 / 12.0 ? sqrt(3.0 * Lc) : a * log(12.0 * Lc - b) + c);
  143. }
  144. avpriv_trc_function avpriv_get_trc_function_from_trc(enum AVColorTransferCharacteristic trc)
  145. {
  146. avpriv_trc_function func = NULL;
  147. switch (trc) {
  148. case AVCOL_TRC_BT709:
  149. case AVCOL_TRC_SMPTE170M:
  150. case AVCOL_TRC_BT2020_10:
  151. case AVCOL_TRC_BT2020_12:
  152. func = avpriv_trc_bt709;
  153. break;
  154. case AVCOL_TRC_GAMMA22:
  155. func = avpriv_trc_gamma22;
  156. break;
  157. case AVCOL_TRC_GAMMA28:
  158. func = avpriv_trc_gamma28;
  159. break;
  160. case AVCOL_TRC_SMPTE240M:
  161. func = avpriv_trc_smpte240M;
  162. break;
  163. case AVCOL_TRC_LINEAR:
  164. func = avpriv_trc_linear;
  165. break;
  166. case AVCOL_TRC_LOG:
  167. func = avpriv_trc_log;
  168. break;
  169. case AVCOL_TRC_LOG_SQRT:
  170. func = avpriv_trc_log_sqrt;
  171. break;
  172. case AVCOL_TRC_IEC61966_2_4:
  173. func = avpriv_trc_iec61966_2_4;
  174. break;
  175. case AVCOL_TRC_BT1361_ECG:
  176. func = avpriv_trc_bt1361;
  177. break;
  178. case AVCOL_TRC_IEC61966_2_1:
  179. func = avpriv_trc_iec61966_2_1;
  180. break;
  181. case AVCOL_TRC_SMPTEST2084:
  182. func = avpriv_trc_smpte_st2084;
  183. break;
  184. case AVCOL_TRC_SMPTEST428_1:
  185. func = avpriv_trc_smpte_st428_1;
  186. break;
  187. case AVCOL_TRC_ARIB_STD_B67:
  188. func = avpriv_trc_arib_std_b67;
  189. break;
  190. case AVCOL_TRC_RESERVED0:
  191. case AVCOL_TRC_UNSPECIFIED:
  192. case AVCOL_TRC_RESERVED:
  193. default:
  194. break;
  195. }
  196. return func;
  197. }