video-matrices.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 "../util/bmem.h"
  15. #include "video-io.h"
  16. //#define COMPUTE_MATRICES
  17. #ifdef COMPUTE_MATRICES
  18. #include "../graphics/matrix3.h"
  19. #endif
  20. static struct {
  21. enum video_colorspace const color_space;
  22. float const Kb, Kr;
  23. int const range_min[3];
  24. int const range_max[3];
  25. int const black_levels[2][3];
  26. float float_range_min[3];
  27. float float_range_max[3];
  28. float matrix[2][16];
  29. } format_info[] = {
  30. {VIDEO_CS_601,
  31. 0.114f, 0.299f, {16, 16, 16}, {235, 240, 240},
  32. {{16, 128, 128}, {0, 128, 128}},
  33. #ifndef COMPUTE_MATRICES
  34. { 16.0f/255.0f, 16.0f/255.0f, 16.0f/255.0f},
  35. {235.0f/255.0f, 240.0f/255.0f, 240.0f/255.0f},
  36. {
  37. {
  38. 1.164384f, 0.000000f, 1.596027f, -0.874202f,
  39. 1.164384f, -0.391762f, -0.812968f, 0.531668f,
  40. 1.164384f, 2.017232f, 0.000000f, -1.085631f,
  41. 0.000000f, 0.000000f, 0.000000f, 1.000000f
  42. },
  43. {
  44. 1.000000f, 0.000000f, 1.407520f, -0.706520f,
  45. 1.000000f, -0.345491f, -0.716948f, 0.533303f,
  46. 1.000000f, 1.778976f, 0.000000f, -0.892976f,
  47. 0.000000f, 0.000000f, 0.000000f, 1.000000f
  48. }
  49. }
  50. #endif
  51. },
  52. {VIDEO_CS_709,
  53. 0.0722f, 0.2126f, {16, 16, 16}, {235, 240, 240},
  54. {{16, 128, 128}, {0, 128, 128}},
  55. #ifndef COMPUTE_MATRICES
  56. { 16.0f/255.0f, 16.0f/255.0f, 16.0f/255.0f},
  57. {235.0f/255.0f, 240.0f/255.0f, 240.0f/255.0f},
  58. {
  59. {
  60. 1.164384f, 0.000000f, 1.792741f, -0.972945f,
  61. 1.164384f, -0.213249f, -0.532909f, 0.301483f,
  62. 1.164384f, 2.112402f, 0.000000f, -1.133402f,
  63. 0.000000f, 0.000000f, 0.000000f, 1.000000f
  64. },
  65. {
  66. 1.000000f, 0.000000f, 1.581000f, -0.793600f,
  67. 1.000000f, -0.188062f, -0.469967f, 0.330305f,
  68. 1.000000f, 1.862906f, 0.000000f, -0.935106f,
  69. 0.000000f, 0.000000f, 0.000000f, 1.000000f
  70. }
  71. }
  72. #endif
  73. },
  74. };
  75. #define NUM_FORMATS (sizeof(format_info)/sizeof(format_info[0]))
  76. #ifdef COMPUTE_MATRICES
  77. static void log_matrix(float const matrix[16])
  78. {
  79. blog(LOG_DEBUG, "\n% f, % f, % f, % f" \
  80. "\n% f, % f, % f, % f" \
  81. "\n% f, % f, % f, % f" \
  82. "\n% f, % f, % f, % f",
  83. matrix[ 0], matrix[ 1], matrix[ 2], matrix[ 3],
  84. matrix[ 4], matrix[ 5], matrix[ 6], matrix[ 7],
  85. matrix[ 8], matrix[ 9], matrix[10], matrix[11],
  86. matrix[12], matrix[13], matrix[14], matrix[15]);
  87. }
  88. static void initialize_matrix(float const Kb, float const Kr,
  89. int const range_min[3], int const range_max[3],
  90. int const black_levels[3], float matrix[16])
  91. {
  92. struct matrix3 color_matrix;
  93. int yvals = range_max[0] - range_min[0];
  94. int uvals = (range_max[1] - range_min[1]) / 2;
  95. int vvals = (range_max[2] - range_min[2]) / 2;
  96. vec3_set(&color_matrix.x, 255./yvals,
  97. 0.,
  98. 255./vvals * (1. - Kr));
  99. vec3_set(&color_matrix.y, 255./yvals,
  100. 255./uvals * (Kb - 1.) * Kb / (1. - Kb - Kr),
  101. 255./vvals * (Kr - 1.) * Kr / (1. - Kb - Kr));
  102. vec3_set(&color_matrix.z, 255./yvals,
  103. 255./uvals * (1. - Kb),
  104. 0.);
  105. struct vec3 offsets, multiplied;
  106. vec3_set(&offsets,
  107. -black_levels[0]/255.,
  108. -black_levels[1]/255.,
  109. -black_levels[2]/255.);
  110. vec3_rotate(&multiplied, &offsets, &color_matrix);
  111. matrix[ 0] = color_matrix.x.x;
  112. matrix[ 1] = color_matrix.x.y;
  113. matrix[ 2] = color_matrix.x.z;
  114. matrix[ 3] = multiplied.x;
  115. matrix[ 4] = color_matrix.y.x;
  116. matrix[ 5] = color_matrix.y.y;
  117. matrix[ 6] = color_matrix.y.z;
  118. matrix[ 7] = multiplied.y;
  119. matrix[ 8] = color_matrix.z.x;
  120. matrix[ 9] = color_matrix.z.y;
  121. matrix[10] = color_matrix.z.z;
  122. matrix[11] = multiplied.z;
  123. matrix[12] = matrix[13] = matrix[14] = 0.;
  124. matrix[15] = 1.;
  125. log_matrix(matrix);
  126. }
  127. static void initialize_matrices()
  128. {
  129. static int range_min[] = { 0, 0, 0};
  130. static int range_max[] = {255, 255, 255};
  131. for (size_t i = 0; i < NUM_FORMATS; i++) {
  132. initialize_matrix(format_info[i].Kb, format_info[i].Kr,
  133. range_min, range_max,
  134. format_info[i].black_levels[1],
  135. format_info[i].matrix[1]);
  136. initialize_matrix(format_info[i].Kb, format_info[i].Kr,
  137. format_info[i].range_min,
  138. format_info[i].range_max,
  139. format_info[i].black_levels[0],
  140. format_info[i].matrix[0]);
  141. for (int j = 0; j < 3; j++) {
  142. format_info[i].float_range_min[j] =
  143. format_info[i].range_min[j]/255.;
  144. format_info[i].float_range_max[j] =
  145. format_info[i].range_max[j]/255.;
  146. }
  147. }
  148. }
  149. static bool matrices_initialized = false;
  150. #endif
  151. bool video_format_get_parameters(enum video_colorspace color_space,
  152. enum video_range_type range, float matrix[16],
  153. float range_min[3], float range_max[3])
  154. {
  155. #ifdef COMPUTE_MATRICES
  156. if (!matrices_initialized) {
  157. initialize_matrices();
  158. matrices_initialized = true;
  159. }
  160. #endif
  161. for (size_t i = 0; i < NUM_FORMATS; i++) {
  162. if (format_info[i].color_space != color_space)
  163. continue;
  164. int full_range = range == VIDEO_RANGE_FULL ? 1 : 0;
  165. memcpy(matrix, format_info[i].matrix[full_range],
  166. sizeof(float) * 16);
  167. if (range == VIDEO_RANGE_FULL)
  168. return true;
  169. if (range_min)
  170. memcpy(range_min, format_info[i].float_range_min,
  171. sizeof(float) * 3);
  172. if (range_max)
  173. memcpy(range_max, format_info[i].float_range_max,
  174. sizeof(float) * 3);
  175. return true;
  176. }
  177. return false;
  178. }