video-matrices.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /******************************************************************************
  2. Copyright (C) 2014 by Ruwen Hahn <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include "video-io.h"
  15. #include "../util/bmem.h"
  16. #include "../graphics/matrix3.h"
  17. #include <assert.h>
  18. //#define LOG_MATRICES
  19. static struct {
  20. float range_min[3];
  21. float range_max[3];
  22. float black_levels[2][3];
  23. float float_range_min[3];
  24. float float_range_max[3];
  25. } bpp_info[9];
  26. static struct {
  27. enum video_colorspace const color_space;
  28. float const Kb, Kr;
  29. float matrix[OBS_COUNTOF(bpp_info)][2][16];
  30. } format_info[] = {
  31. {
  32. VIDEO_CS_601,
  33. 0.114f,
  34. 0.299f,
  35. },
  36. {
  37. VIDEO_CS_709,
  38. 0.0722f,
  39. 0.2126f,
  40. },
  41. {
  42. VIDEO_CS_2100_PQ,
  43. 0.0593f,
  44. 0.2627f,
  45. },
  46. };
  47. #define NUM_FORMATS (sizeof(format_info) / sizeof(format_info[0]))
  48. #ifdef LOG_MATRICES
  49. static void log_matrix(float const matrix[16])
  50. {
  51. blog(LOG_DEBUG,
  52. "\n% f, % f, % f, % f"
  53. "\n% f, % f, % f, % f"
  54. "\n% f, % f, % f, % f"
  55. "\n% f, % f, % f, % f",
  56. matrix[0], matrix[1], matrix[2], matrix[3], matrix[4], matrix[5], matrix[6], matrix[7], matrix[8],
  57. matrix[9], matrix[10], matrix[11], matrix[12], matrix[13], matrix[14], matrix[15]);
  58. }
  59. #endif
  60. static void initialize_matrix(float const Kb, float const Kr, float bit_range_max, float const range_min[3],
  61. float const range_max[3], float const black_levels[3], float matrix[16])
  62. {
  63. struct matrix3 color_matrix;
  64. float const yvals = range_max[0] - range_min[0];
  65. float const uvals = (range_max[1] - range_min[1]) / 2.f;
  66. float const vvals = (range_max[2] - range_min[2]) / 2.f;
  67. float const yscale = bit_range_max / yvals;
  68. float const uscale = bit_range_max / uvals;
  69. float const vscale = bit_range_max / vvals;
  70. float const Kg = (1.f - Kb - Kr);
  71. vec3_set(&color_matrix.x, yscale, 0.f, vscale * (1.f - Kr));
  72. vec3_set(&color_matrix.y, yscale, uscale * (Kb - 1.f) * Kb / Kg, vscale * (Kr - 1.f) * Kr / Kg);
  73. vec3_set(&color_matrix.z, yscale, uscale * (1.f - Kb), 0.f);
  74. struct vec3 offsets, multiplied;
  75. vec3_set(&offsets, -black_levels[0] / bit_range_max, -black_levels[1] / bit_range_max,
  76. -black_levels[2] / bit_range_max);
  77. vec3_rotate(&multiplied, &offsets, &color_matrix);
  78. matrix[0] = color_matrix.x.x;
  79. matrix[1] = color_matrix.x.y;
  80. matrix[2] = color_matrix.x.z;
  81. matrix[3] = multiplied.x;
  82. matrix[4] = color_matrix.y.x;
  83. matrix[5] = color_matrix.y.y;
  84. matrix[6] = color_matrix.y.z;
  85. matrix[7] = multiplied.y;
  86. matrix[8] = color_matrix.z.x;
  87. matrix[9] = color_matrix.z.y;
  88. matrix[10] = color_matrix.z.z;
  89. matrix[11] = multiplied.z;
  90. matrix[12] = matrix[13] = matrix[14] = 0.;
  91. matrix[15] = 1.;
  92. #ifdef LOG_MATRICES
  93. log_matrix(matrix);
  94. #endif
  95. }
  96. static void initialize_matrices()
  97. {
  98. static const float full_range_min3[] = {0, 0, 0};
  99. float min_value = 16.f;
  100. float max_luma = 235.f;
  101. float max_chroma = 240.f;
  102. float range = 256.f;
  103. for (uint32_t bpp = 8; bpp <= 16; ++bpp) {
  104. const uint32_t bpp_index = bpp - 8;
  105. bpp_info[bpp_index].range_min[0] = min_value;
  106. bpp_info[bpp_index].range_min[1] = min_value;
  107. bpp_info[bpp_index].range_min[2] = min_value;
  108. bpp_info[bpp_index].range_max[0] = max_luma;
  109. bpp_info[bpp_index].range_max[1] = max_chroma;
  110. bpp_info[bpp_index].range_max[2] = max_chroma;
  111. const float mid_chroma = 0.5f * (min_value + max_chroma);
  112. bpp_info[bpp_index].black_levels[0][0] = min_value;
  113. bpp_info[bpp_index].black_levels[0][1] = mid_chroma;
  114. bpp_info[bpp_index].black_levels[0][2] = mid_chroma;
  115. bpp_info[bpp_index].black_levels[1][0] = 0.f;
  116. bpp_info[bpp_index].black_levels[1][1] = mid_chroma;
  117. bpp_info[bpp_index].black_levels[1][2] = mid_chroma;
  118. const float range_max = range - 1.f;
  119. bpp_info[bpp_index].float_range_min[0] = min_value / range_max;
  120. bpp_info[bpp_index].float_range_min[1] = min_value / range_max;
  121. bpp_info[bpp_index].float_range_min[2] = min_value / range_max;
  122. bpp_info[bpp_index].float_range_max[0] = max_luma / range_max;
  123. bpp_info[bpp_index].float_range_max[1] = max_chroma / range_max;
  124. bpp_info[bpp_index].float_range_max[2] = max_chroma / range_max;
  125. for (size_t i = 0; i < NUM_FORMATS; i++) {
  126. float full_range_max3[] = {range_max, range_max, range_max};
  127. initialize_matrix(format_info[i].Kb, format_info[i].Kr, range_max, full_range_min3,
  128. full_range_max3, bpp_info[bpp_index].black_levels[1],
  129. format_info[i].matrix[bpp_index][1]);
  130. initialize_matrix(format_info[i].Kb, format_info[i].Kr, range_max,
  131. bpp_info[bpp_index].range_min, bpp_info[bpp_index].range_max,
  132. bpp_info[bpp_index].black_levels[0], format_info[i].matrix[bpp_index][0]);
  133. }
  134. min_value *= 2.f;
  135. max_luma *= 2.f;
  136. max_chroma *= 2.f;
  137. range *= 2.f;
  138. }
  139. }
  140. static bool matrices_initialized = false;
  141. static const float full_min[3] = {0.0f, 0.0f, 0.0f};
  142. static const float full_max[3] = {1.0f, 1.0f, 1.0f};
  143. static bool video_format_get_parameters_for_bpc(enum video_colorspace color_space, enum video_range_type range,
  144. float matrix[16], float range_min[3], float range_max[3], uint32_t bpc)
  145. {
  146. if (!matrices_initialized) {
  147. initialize_matrices();
  148. matrices_initialized = true;
  149. }
  150. if ((color_space == VIDEO_CS_DEFAULT) || (color_space == VIDEO_CS_SRGB))
  151. color_space = VIDEO_CS_709;
  152. else if (color_space == VIDEO_CS_2100_HLG)
  153. color_space = VIDEO_CS_2100_PQ;
  154. if (bpc < 8)
  155. bpc = 8;
  156. if (bpc > 16)
  157. bpc = 16;
  158. const uint32_t bpc_index = bpc - 8;
  159. assert(bpc_index < OBS_COUNTOF(bpp_info));
  160. bool success = false;
  161. for (size_t i = 0; i < NUM_FORMATS; i++) {
  162. success = format_info[i].color_space == color_space;
  163. if (success) {
  164. const bool full_range = range == VIDEO_RANGE_FULL;
  165. memcpy(matrix, format_info[i].matrix[bpc_index][full_range], sizeof(float) * 16);
  166. if (range_min) {
  167. const float *src_range_min = full_range ? full_min
  168. : bpp_info[bpc_index].float_range_min;
  169. memcpy(range_min, src_range_min, sizeof(float) * 3);
  170. }
  171. if (range_max) {
  172. const float *src_range_max = full_range ? full_max
  173. : bpp_info[bpc_index].float_range_max;
  174. memcpy(range_max, src_range_max, sizeof(float) * 3);
  175. }
  176. break;
  177. }
  178. }
  179. return success;
  180. }
  181. bool video_format_get_parameters(enum video_colorspace color_space, enum video_range_type range, float matrix[16],
  182. float range_min[3], float range_max[3])
  183. {
  184. uint32_t bpc = (color_space == VIDEO_CS_2100_PQ || color_space == VIDEO_CS_2100_HLG) ? 10 : 8;
  185. return video_format_get_parameters_for_bpc(color_space, range, matrix, range_min, range_max, bpc);
  186. }
  187. bool video_format_get_parameters_for_format(enum video_colorspace color_space, enum video_range_type range,
  188. enum video_format format, float matrix[16], float range_min[3],
  189. float range_max[3])
  190. {
  191. uint32_t bpc;
  192. switch (format) {
  193. case VIDEO_FORMAT_I010:
  194. case VIDEO_FORMAT_P010:
  195. case VIDEO_FORMAT_I210:
  196. case VIDEO_FORMAT_V210:
  197. case VIDEO_FORMAT_R10L:
  198. bpc = 10;
  199. break;
  200. case VIDEO_FORMAT_I412:
  201. case VIDEO_FORMAT_YA2L:
  202. bpc = 12;
  203. break;
  204. case VIDEO_FORMAT_P216:
  205. case VIDEO_FORMAT_P416:
  206. bpc = 16;
  207. break;
  208. default:
  209. bpc = 8;
  210. break;
  211. }
  212. return video_format_get_parameters_for_bpc(color_space, range, matrix, range_min, range_max, bpc);
  213. }