You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

656 lines
24KB

  1. /*
  2. * Texture block compression
  3. * Copyright (C) 2015 Vittorio Giovara <vittorio.giovara@gmail.com>
  4. * Based on public domain code by Fabian Giesen, Sean Barrett and Yann Collet.
  5. *
  6. * This file is part of Libav
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a
  9. * copy of this software and associated documentation files (the "Software"),
  10. * to deal in the Software without restriction, including without limitation
  11. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  12. * and/or sell copies of the Software, and to permit persons to whom the
  13. * Software is furnished to do so, subject to the following conditions:
  14. * The above copyright notice and this permission notice shall be included
  15. * in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  23. * IN THE SOFTWARE.
  24. */
  25. #include <stddef.h>
  26. #include <stdint.h>
  27. #include "libavutil/attributes.h"
  28. #include "libavutil/common.h"
  29. #include "libavutil/intreadwrite.h"
  30. #include "texturedsp.h"
  31. static const uint8_t expand5[32] = {
  32. 0, 8, 16, 24, 33, 41, 49, 57, 66, 74, 82, 90,
  33. 99, 107, 115, 123, 132, 140, 148, 156, 165, 173, 181, 189,
  34. 198, 206, 214, 222, 231, 239, 247, 255,
  35. };
  36. static const uint8_t expand6[64] = {
  37. 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44,
  38. 48, 52, 56, 60, 65, 69, 73, 77, 81, 85, 89, 93,
  39. 97, 101, 105, 109, 113, 117, 121, 125, 130, 134, 138, 142,
  40. 146, 150, 154, 158, 162, 166, 170, 174, 178, 182, 186, 190,
  41. 195, 199, 203, 207, 211, 215, 219, 223, 227, 231, 235, 239,
  42. 243, 247, 251, 255,
  43. };
  44. static const uint8_t match5[256][2] = {
  45. { 0, 0 }, { 0, 0 }, { 0, 1 }, { 0, 1 }, { 1, 0 }, { 1, 0 },
  46. { 1, 0 }, { 1, 1 }, { 1, 1 }, { 2, 0 }, { 2, 0 }, { 0, 4 },
  47. { 2, 1 }, { 2, 1 }, { 2, 1 }, { 3, 0 }, { 3, 0 }, { 3, 0 },
  48. { 3, 1 }, { 1, 5 }, { 3, 2 }, { 3, 2 }, { 4, 0 }, { 4, 0 },
  49. { 4, 1 }, { 4, 1 }, { 4, 2 }, { 4, 2 }, { 4, 2 }, { 3, 5 },
  50. { 5, 1 }, { 5, 1 }, { 5, 2 }, { 4, 4 }, { 5, 3 }, { 5, 3 },
  51. { 5, 3 }, { 6, 2 }, { 6, 2 }, { 6, 2 }, { 6, 3 }, { 5, 5 },
  52. { 6, 4 }, { 6, 4 }, { 4, 8 }, { 7, 3 }, { 7, 3 }, { 7, 3 },
  53. { 7, 4 }, { 7, 4 }, { 7, 4 }, { 7, 5 }, { 5, 9 }, { 7, 6 },
  54. { 7, 6 }, { 8, 4 }, { 8, 4 }, { 8, 5 }, { 8, 5 }, { 8, 6 },
  55. { 8, 6 }, { 8, 6 }, { 7, 9 }, { 9, 5 }, { 9, 5 }, { 9, 6 },
  56. { 8, 8 }, { 9, 7 }, { 9, 7 }, { 9, 7 }, { 10, 6 }, { 10, 6 },
  57. { 10, 6 }, { 10, 7 }, { 9, 9 }, { 10, 8 }, { 10, 8 }, { 8, 12 },
  58. { 11, 7 }, { 11, 7 }, { 11, 7 }, { 11, 8 }, { 11, 8 }, { 11, 8 },
  59. { 11, 9 }, { 9, 13 }, { 11, 10 }, { 11, 10 }, { 12, 8 }, { 12, 8 },
  60. { 12, 9 }, { 12, 9 }, { 12, 10 }, { 12, 10 }, { 12, 10 }, { 11, 13 },
  61. { 13, 9 }, { 13, 9 }, { 13, 10 }, { 12, 12 }, { 13, 11 }, { 13, 11 },
  62. { 13, 11 }, { 14, 10 }, { 14, 10 }, { 14, 10 }, { 14, 11 }, { 13, 13 },
  63. { 14, 12 }, { 14, 12 }, { 12, 16 }, { 15, 11 }, { 15, 11 }, { 15, 11 },
  64. { 15, 12 }, { 15, 12 }, { 15, 12 }, { 15, 13 }, { 13, 17 }, { 15, 14 },
  65. { 15, 14 }, { 16, 12 }, { 16, 12 }, { 16, 13 }, { 16, 13 }, { 16, 14 },
  66. { 16, 14 }, { 16, 14 }, { 15, 17 }, { 17, 13 }, { 17, 13 }, { 17, 14 },
  67. { 16, 16 }, { 17, 15 }, { 17, 15 }, { 17, 15 }, { 18, 14 }, { 18, 14 },
  68. { 18, 14 }, { 18, 15 }, { 17, 17 }, { 18, 16 }, { 18, 16 }, { 16, 20 },
  69. { 19, 15 }, { 19, 15 }, { 19, 15 }, { 19, 16 }, { 19, 16 }, { 19, 16 },
  70. { 19, 17 }, { 17, 21 }, { 19, 18 }, { 19, 18 }, { 20, 16 }, { 20, 16 },
  71. { 20, 17 }, { 20, 17 }, { 20, 18 }, { 20, 18 }, { 20, 18 }, { 19, 21 },
  72. { 21, 17 }, { 21, 17 }, { 21, 18 }, { 20, 20 }, { 21, 19 }, { 21, 19 },
  73. { 21, 19 }, { 22, 18 }, { 22, 18 }, { 22, 18 }, { 22, 19 }, { 21, 21 },
  74. { 22, 20 }, { 22, 20 }, { 20, 24 }, { 23, 19 }, { 23, 19 }, { 23, 19 },
  75. { 23, 20 }, { 23, 20 }, { 23, 20 }, { 23, 21 }, { 21, 25 }, { 23, 22 },
  76. { 23, 22 }, { 24, 20 }, { 24, 20 }, { 24, 21 }, { 24, 21 }, { 24, 22 },
  77. { 24, 22 }, { 24, 22 }, { 23, 25 }, { 25, 21 }, { 25, 21 }, { 25, 22 },
  78. { 24, 24 }, { 25, 23 }, { 25, 23 }, { 25, 23 }, { 26, 22 }, { 26, 22 },
  79. { 26, 22 }, { 26, 23 }, { 25, 25 }, { 26, 24 }, { 26, 24 }, { 24, 28 },
  80. { 27, 23 }, { 27, 23 }, { 27, 23 }, { 27, 24 }, { 27, 24 }, { 27, 24 },
  81. { 27, 25 }, { 25, 29 }, { 27, 26 }, { 27, 26 }, { 28, 24 }, { 28, 24 },
  82. { 28, 25 }, { 28, 25 }, { 28, 26 }, { 28, 26 }, { 28, 26 }, { 27, 29 },
  83. { 29, 25 }, { 29, 25 }, { 29, 26 }, { 28, 28 }, { 29, 27 }, { 29, 27 },
  84. { 29, 27 }, { 30, 26 }, { 30, 26 }, { 30, 26 }, { 30, 27 }, { 29, 29 },
  85. { 30, 28 }, { 30, 28 }, { 30, 28 }, { 31, 27 }, { 31, 27 }, { 31, 27 },
  86. { 31, 28 }, { 31, 28 }, { 31, 28 }, { 31, 29 }, { 31, 29 }, { 31, 30 },
  87. { 31, 30 }, { 31, 30 }, { 31, 31 }, { 31, 31 },
  88. };
  89. static const uint8_t match6[256][2] = {
  90. { 0, 0 }, { 0, 1 }, { 1, 0 }, { 1, 0 }, { 1, 1 }, { 2, 0 },
  91. { 2, 1 }, { 3, 0 }, { 3, 0 }, { 3, 1 }, { 4, 0 }, { 4, 0 },
  92. { 4, 1 }, { 5, 0 }, { 5, 1 }, { 6, 0 }, { 6, 0 }, { 6, 1 },
  93. { 7, 0 }, { 7, 0 }, { 7, 1 }, { 8, 0 }, { 8, 1 }, { 8, 1 },
  94. { 8, 2 }, { 9, 1 }, { 9, 2 }, { 9, 2 }, { 9, 3 }, { 10, 2 },
  95. { 10, 3 }, { 10, 3 }, { 10, 4 }, { 11, 3 }, { 11, 4 }, { 11, 4 },
  96. { 11, 5 }, { 12, 4 }, { 12, 5 }, { 12, 5 }, { 12, 6 }, { 13, 5 },
  97. { 13, 6 }, { 8, 16 }, { 13, 7 }, { 14, 6 }, { 14, 7 }, { 9, 17 },
  98. { 14, 8 }, { 15, 7 }, { 15, 8 }, { 11, 16 }, { 15, 9 }, { 15, 10 },
  99. { 16, 8 }, { 16, 9 }, { 16, 10 }, { 15, 13 }, { 17, 9 }, { 17, 10 },
  100. { 17, 11 }, { 15, 16 }, { 18, 10 }, { 18, 11 }, { 18, 12 }, { 16, 16 },
  101. { 19, 11 }, { 19, 12 }, { 19, 13 }, { 17, 17 }, { 20, 12 }, { 20, 13 },
  102. { 20, 14 }, { 19, 16 }, { 21, 13 }, { 21, 14 }, { 21, 15 }, { 20, 17 },
  103. { 22, 14 }, { 22, 15 }, { 25, 10 }, { 22, 16 }, { 23, 15 }, { 23, 16 },
  104. { 26, 11 }, { 23, 17 }, { 24, 16 }, { 24, 17 }, { 27, 12 }, { 24, 18 },
  105. { 25, 17 }, { 25, 18 }, { 28, 13 }, { 25, 19 }, { 26, 18 }, { 26, 19 },
  106. { 29, 14 }, { 26, 20 }, { 27, 19 }, { 27, 20 }, { 30, 15 }, { 27, 21 },
  107. { 28, 20 }, { 28, 21 }, { 28, 21 }, { 28, 22 }, { 29, 21 }, { 29, 22 },
  108. { 24, 32 }, { 29, 23 }, { 30, 22 }, { 30, 23 }, { 25, 33 }, { 30, 24 },
  109. { 31, 23 }, { 31, 24 }, { 27, 32 }, { 31, 25 }, { 31, 26 }, { 32, 24 },
  110. { 32, 25 }, { 32, 26 }, { 31, 29 }, { 33, 25 }, { 33, 26 }, { 33, 27 },
  111. { 31, 32 }, { 34, 26 }, { 34, 27 }, { 34, 28 }, { 32, 32 }, { 35, 27 },
  112. { 35, 28 }, { 35, 29 }, { 33, 33 }, { 36, 28 }, { 36, 29 }, { 36, 30 },
  113. { 35, 32 }, { 37, 29 }, { 37, 30 }, { 37, 31 }, { 36, 33 }, { 38, 30 },
  114. { 38, 31 }, { 41, 26 }, { 38, 32 }, { 39, 31 }, { 39, 32 }, { 42, 27 },
  115. { 39, 33 }, { 40, 32 }, { 40, 33 }, { 43, 28 }, { 40, 34 }, { 41, 33 },
  116. { 41, 34 }, { 44, 29 }, { 41, 35 }, { 42, 34 }, { 42, 35 }, { 45, 30 },
  117. { 42, 36 }, { 43, 35 }, { 43, 36 }, { 46, 31 }, { 43, 37 }, { 44, 36 },
  118. { 44, 37 }, { 44, 37 }, { 44, 38 }, { 45, 37 }, { 45, 38 }, { 40, 48 },
  119. { 45, 39 }, { 46, 38 }, { 46, 39 }, { 41, 49 }, { 46, 40 }, { 47, 39 },
  120. { 47, 40 }, { 43, 48 }, { 47, 41 }, { 47, 42 }, { 48, 40 }, { 48, 41 },
  121. { 48, 42 }, { 47, 45 }, { 49, 41 }, { 49, 42 }, { 49, 43 }, { 47, 48 },
  122. { 50, 42 }, { 50, 43 }, { 50, 44 }, { 48, 48 }, { 51, 43 }, { 51, 44 },
  123. { 51, 45 }, { 49, 49 }, { 52, 44 }, { 52, 45 }, { 52, 46 }, { 51, 48 },
  124. { 53, 45 }, { 53, 46 }, { 53, 47 }, { 52, 49 }, { 54, 46 }, { 54, 47 },
  125. { 57, 42 }, { 54, 48 }, { 55, 47 }, { 55, 48 }, { 58, 43 }, { 55, 49 },
  126. { 56, 48 }, { 56, 49 }, { 59, 44 }, { 56, 50 }, { 57, 49 }, { 57, 50 },
  127. { 60, 45 }, { 57, 51 }, { 58, 50 }, { 58, 51 }, { 61, 46 }, { 58, 52 },
  128. { 59, 51 }, { 59, 52 }, { 62, 47 }, { 59, 53 }, { 60, 52 }, { 60, 53 },
  129. { 60, 53 }, { 60, 54 }, { 61, 53 }, { 61, 54 }, { 61, 54 }, { 61, 55 },
  130. { 62, 54 }, { 62, 55 }, { 62, 55 }, { 62, 56 }, { 63, 55 }, { 63, 56 },
  131. { 63, 56 }, { 63, 57 }, { 63, 58 }, { 63, 59 }, { 63, 59 }, { 63, 60 },
  132. { 63, 61 }, { 63, 62 }, { 63, 62 }, { 63, 63 },
  133. };
  134. /* Multiplication over 8 bit emulation */
  135. #define mul8(a, b) (a * b + 128 + ((a * b + 128) >> 8)) >> 8
  136. /* Conversion from rgb24 to rgb565 */
  137. #define rgb2rgb565(r, g, b) \
  138. (mul8(r, 31) << 11) | (mul8(g, 63) << 5) | (mul8(b, 31) << 0)
  139. /* Linear interpolation at 1/3 point between a and b */
  140. #define lerp13(a, b) (2 * a + b) / 3
  141. /* Linear interpolation on an RGB pixel */
  142. static inline void lerp13rgb(uint8_t *out, uint8_t *p1, uint8_t *p2)
  143. {
  144. out[0] = lerp13(p1[0], p2[0]);
  145. out[1] = lerp13(p1[1], p2[1]);
  146. out[2] = lerp13(p1[2], p2[2]);
  147. }
  148. /* Conversion from rgb565 to rgb24 */
  149. static inline void rgb5652rgb(uint8_t *out, uint16_t v)
  150. {
  151. int rv = (v & 0xf800) >> 11;
  152. int gv = (v & 0x07e0) >> 5;
  153. int bv = (v & 0x001f) >> 0;
  154. out[0] = expand5[rv];
  155. out[1] = expand6[gv];
  156. out[2] = expand5[bv];
  157. out[3] = 0;
  158. }
  159. /* Color matching function */
  160. static unsigned int match_colors(const uint8_t *block, ptrdiff_t stride,
  161. uint16_t c0, uint16_t c1)
  162. {
  163. uint32_t mask = 0;
  164. int dirr, dirg, dirb;
  165. int dots[16];
  166. int stops[4];
  167. int x, y, k = 0;
  168. int c0_point, half_point, c3_point;
  169. uint8_t color[16];
  170. static const int indexMap[8] = {
  171. 0 << 30, 2 << 30, 0 << 30, 2 << 30,
  172. 3 << 30, 3 << 30, 1 << 30, 1 << 30,
  173. };
  174. /* Fill color and compute direction for each component */
  175. rgb5652rgb(color + 0, c0);
  176. rgb5652rgb(color + 4, c1);
  177. lerp13rgb(color + 8, color + 0, color + 4);
  178. lerp13rgb(color + 12, color + 4, color + 0);
  179. dirr = color[0 * 4 + 0] - color[1 * 4 + 0];
  180. dirg = color[0 * 4 + 1] - color[1 * 4 + 1];
  181. dirb = color[0 * 4 + 2] - color[1 * 4 + 2];
  182. for (y = 0; y < 4; y++) {
  183. for (x = 0; x < 4; x++)
  184. dots[k++] = block[0 + x * 4 + y * stride] * dirr +
  185. block[1 + x * 4 + y * stride] * dirg +
  186. block[2 + x * 4 + y * stride] * dirb;
  187. stops[y] = color[0 + y * 4] * dirr +
  188. color[1 + y * 4] * dirg +
  189. color[2 + y * 4] * dirb;
  190. }
  191. /* Think of the colors as arranged on a line; project point onto that line,
  192. * then choose next color out of available ones. we compute the crossover
  193. * points for 'best color in top half'/'best in bottom half' and then
  194. * the same inside that subinterval.
  195. *
  196. * Relying on this 1d approximation isn't always optimal in terms of
  197. * Euclidean distance, but it's very close and a lot faster.
  198. *
  199. * http://cbloomrants.blogspot.com/2008/12/12-08-08-dxtc-summary.html */
  200. c0_point = (stops[1] + stops[3]) >> 1;
  201. half_point = (stops[3] + stops[2]) >> 1;
  202. c3_point = (stops[2] + stops[0]) >> 1;
  203. for (x = 0; x < 16; x++) {
  204. int dot = dots[x];
  205. int bits = (dot < half_point ? 4 : 0) |
  206. (dot < c0_point ? 2 : 0) |
  207. (dot < c3_point ? 1 : 0);
  208. mask >>= 2;
  209. mask |= indexMap[bits];
  210. }
  211. return mask;
  212. }
  213. /* Color optimization function */
  214. static void optimize_colors(const uint8_t *block, ptrdiff_t stride,
  215. uint16_t *pmax16, uint16_t *pmin16)
  216. {
  217. const uint8_t *minp;
  218. const uint8_t *maxp;
  219. const int iter_power = 4;
  220. double magn;
  221. int v_r, v_g, v_b;
  222. float covf[6], vfr, vfg, vfb;
  223. int mind, maxd;
  224. int cov[6] = { 0 };
  225. int mu[3], min[3], max[3];
  226. int ch, iter, x, y;
  227. /* Determine color distribution */
  228. for (ch = 0; ch < 3; ch++) {
  229. const uint8_t *bp = &block[ch];
  230. int muv, minv, maxv;
  231. muv = minv = maxv = bp[0];
  232. for (y = 0; y < 4; y++) {
  233. for (x = 4; x < 4; x += 4) {
  234. muv += bp[x * 4 + y * stride];
  235. if (bp[x] < minv)
  236. minv = bp[x * 4 + y * stride];
  237. else if (bp[x] > maxv)
  238. maxv = bp[x * 4 + y * stride];
  239. }
  240. }
  241. mu[ch] = (muv + 8) >> 4;
  242. min[ch] = minv;
  243. max[ch] = maxv;
  244. }
  245. /* Determine covariance matrix */
  246. for (y = 0; y < 4; y++) {
  247. for (x = 0; x < 4; x++) {
  248. int r = block[x * 4 + stride * y + 0] - mu[0];
  249. int g = block[x * 4 + stride * y + 1] - mu[1];
  250. int b = block[x * 4 + stride * y + 2] - mu[2];
  251. cov[0] += r * r;
  252. cov[1] += r * g;
  253. cov[2] += r * b;
  254. cov[3] += g * g;
  255. cov[4] += g * b;
  256. cov[5] += b * b;
  257. }
  258. }
  259. /* Convert covariance matrix to float, find principal axis via power iter */
  260. for (x = 0; x < 6; x++)
  261. covf[x] = cov[x] / 255.0f;
  262. vfr = (float) (max[0] - min[0]);
  263. vfg = (float) (max[1] - min[1]);
  264. vfb = (float) (max[2] - min[2]);
  265. for (iter = 0; iter < iter_power; iter++) {
  266. float r = vfr * covf[0] + vfg * covf[1] + vfb * covf[2];
  267. float g = vfr * covf[1] + vfg * covf[3] + vfb * covf[4];
  268. float b = vfr * covf[2] + vfg * covf[4] + vfb * covf[5];
  269. vfr = r;
  270. vfg = g;
  271. vfb = b;
  272. }
  273. magn = fabs(vfr);
  274. if (fabs(vfg) > magn)
  275. magn = fabs(vfg);
  276. if (fabs(vfb) > magn)
  277. magn = fabs(vfb);
  278. /* if magnitude is too small, default to luminance */
  279. if (magn < 4.0f) {
  280. /* JPEG YCbCr luma coefs, scaled by 1000 */
  281. v_r = 299;
  282. v_g = 587;
  283. v_b = 114;
  284. } else {
  285. magn = 512.0 / magn;
  286. v_r = (int) (vfr * magn);
  287. v_g = (int) (vfg * magn);
  288. v_b = (int) (vfb * magn);
  289. }
  290. /* Pick colors at extreme points */
  291. mind = maxd = block[0] * v_r + block[1] * v_g + block[2] * v_b;
  292. minp = maxp = block;
  293. for (y = 0; y < 4; y++) {
  294. for (x = 0; x < 4; x++) {
  295. int dot = block[x * 4 + y * stride + 0] * v_r +
  296. block[x * 4 + y * stride + 1] * v_g +
  297. block[x * 4 + y * stride + 2] * v_b;
  298. if (dot < mind) {
  299. mind = dot;
  300. minp = block + x * 4 + y * stride;
  301. } else if (dot > maxd) {
  302. maxd = dot;
  303. maxp = block + x * 4 + y * stride;
  304. }
  305. }
  306. }
  307. *pmax16 = rgb2rgb565(maxp[0], maxp[1], maxp[2]);
  308. *pmin16 = rgb2rgb565(minp[0], minp[1], minp[2]);
  309. }
  310. /* Try to optimize colors to suit block contents better, by solving
  311. * a least squares system via normal equations + Cramer's rule. */
  312. static int refine_colors(const uint8_t *block, ptrdiff_t stride,
  313. uint16_t *pmax16, uint16_t *pmin16, uint32_t mask)
  314. {
  315. uint32_t cm = mask;
  316. uint16_t oldMin = *pmin16;
  317. uint16_t oldMax = *pmax16;
  318. uint16_t min16, max16;
  319. int x, y;
  320. /* Additional magic to save a lot of multiplies in the accumulating loop.
  321. * The tables contain precomputed products of weights for least squares
  322. * system, accumulated inside one 32-bit register */
  323. static const int w1tab[4] = { 3, 0, 2, 1 };
  324. static const int prods[4] = { 0x090000, 0x000900, 0x040102, 0x010402 };
  325. /* Check if all pixels have the same index */
  326. if ((mask ^ (mask << 2)) < 4) {
  327. /* If so, linear system would be singular; solve using optimal
  328. * single-color match on average color. */
  329. int r = 8, g = 8, b = 8;
  330. for (y = 0; y < 4; y++) {
  331. for (x = 0; x < 4; x++) {
  332. r += block[0 + x * 4 + y * stride];
  333. g += block[1 + x * 4 + y * stride];
  334. b += block[2 + x * 4 + y * stride];
  335. }
  336. }
  337. r >>= 4;
  338. g >>= 4;
  339. b >>= 4;
  340. max16 = (match5[r][0] << 11) | (match6[g][0] << 5) | match5[b][0];
  341. min16 = (match5[r][1] << 11) | (match6[g][1] << 5) | match5[b][1];
  342. } else {
  343. float fr, fg, fb;
  344. int at1_r = 0, at1_g = 0, at1_b = 0;
  345. int at2_r = 0, at2_g = 0, at2_b = 0;
  346. int akku = 0;
  347. int xx, xy, yy;
  348. for (y = 0; y < 4; y++) {
  349. for (x = 0; x < 4; x++) {
  350. int step = cm & 3;
  351. int w1 = w1tab[step];
  352. int r = block[0 + x * 4 + y * stride];
  353. int g = block[1 + x * 4 + y * stride];
  354. int b = block[2 + x * 4 + y * stride];
  355. akku += prods[step];
  356. at1_r += w1 * r;
  357. at1_g += w1 * g;
  358. at1_b += w1 * b;
  359. at2_r += r;
  360. at2_g += g;
  361. at2_b += b;
  362. cm >>= 2;
  363. }
  364. }
  365. at2_r = 3 * at2_r - at1_r;
  366. at2_g = 3 * at2_g - at1_g;
  367. at2_b = 3 * at2_b - at1_b;
  368. /* Extract solutions and decide solvability */
  369. xx = akku >> 16;
  370. yy = (akku >> 8) & 0xFF;
  371. xy = (akku >> 0) & 0xFF;
  372. fr = 3.0f * 31.0f / 255.0f / (xx * yy - xy * xy);
  373. fg = fr * 63.0f / 31.0f;
  374. fb = fr;
  375. /* Solve */
  376. max16 = av_clip_uintp2((at1_r * yy - at2_r * xy) * fr + 0.5f, 5) << 11;
  377. max16 |= av_clip_uintp2((at1_g * yy - at2_g * xy) * fg + 0.5f, 6) << 5;
  378. max16 |= av_clip_uintp2((at1_b * yy - at2_b * xy) * fb + 0.5f, 5) << 0;
  379. min16 = av_clip_uintp2((at2_r * xx - at1_r * xy) * fr + 0.5f, 5) << 11;
  380. min16 |= av_clip_uintp2((at2_g * xx - at1_g * xy) * fg + 0.5f, 6) << 5;
  381. min16 |= av_clip_uintp2((at2_b * xx - at1_b * xy) * fb + 0.5f, 5) << 0;
  382. }
  383. *pmin16 = min16;
  384. *pmax16 = max16;
  385. return oldMin != min16 || oldMax != max16;
  386. }
  387. /* Check if input block is a constant color */
  388. static int constant_color(const uint8_t *block, ptrdiff_t stride)
  389. {
  390. int x, y;
  391. uint32_t first = AV_RL32(block);
  392. for (y = 0; y < 4; y++)
  393. for (x = 0; x < 4; x++)
  394. if (first != AV_RL32(block + x * 4 + y * stride))
  395. return 0;
  396. return 1;
  397. }
  398. /* Main color compression function */
  399. static void compress_color(uint8_t *dst, ptrdiff_t stride, const uint8_t *block)
  400. {
  401. uint32_t mask;
  402. uint16_t max16, min16;
  403. int constant = constant_color(block, stride);
  404. /* Constant color will load values from tables */
  405. if (constant) {
  406. int r = block[0];
  407. int g = block[1];
  408. int b = block[2];
  409. mask = 0xAAAAAAAA;
  410. max16 = (match5[r][0] << 11) | (match6[g][0] << 5) | match5[b][0];
  411. min16 = (match5[r][1] << 11) | (match6[g][1] << 5) | match5[b][1];
  412. } else {
  413. int refine;
  414. /* Otherwise find pca and map along principal axis */
  415. optimize_colors(block, stride, &max16, &min16);
  416. if (max16 != min16)
  417. mask = match_colors(block, stride, max16, min16);
  418. else
  419. mask = 0;
  420. /* One pass refinement */
  421. refine = refine_colors(block, stride, &max16, &min16, mask);
  422. if (refine) {
  423. if (max16 != min16)
  424. mask = match_colors(block, stride, max16, min16);
  425. else
  426. mask = 0;
  427. }
  428. }
  429. /* Finally write the color block */
  430. if (max16 < min16) {
  431. FFSWAP(uint16_t, min16, max16);
  432. mask ^= 0x55555555;
  433. }
  434. AV_WL16(dst + 0, max16);
  435. AV_WL16(dst + 2, min16);
  436. AV_WL32(dst + 4, mask);
  437. }
  438. /* Alpha compression function */
  439. static void compress_alpha(uint8_t *dst, ptrdiff_t stride, const uint8_t *block)
  440. {
  441. int x, y;
  442. int dist, bias, dist4, dist2;
  443. int mn, mx;
  444. int bits = 0;
  445. int mask = 0;
  446. memset(dst, 0, 8);
  447. /* Find min/max color */
  448. mn = mx = block[3];
  449. for (y = 0; y < 4; y++) {
  450. for (x = 0; x < 4; x++) {
  451. int val = block[3 + x * 4 + y * stride];
  452. if (val < mn)
  453. mn = val;
  454. else if (val > mx)
  455. mx = val;
  456. }
  457. }
  458. /* Encode them */
  459. dst[0] = (uint8_t) mx;
  460. dst[1] = (uint8_t) mn;
  461. dst += 2;
  462. /* Mono-alpha shortcut */
  463. if (mn == mx)
  464. return;
  465. /* Determine bias and emit color indices.
  466. * Given the choice of mx/mn, these indices are optimal:
  467. * fgiesen.wordpress.com/2009/12/15/dxt5-alpha-block-index-determination */
  468. dist = mx - mn;
  469. dist4 = dist * 4;
  470. dist2 = dist * 2;
  471. if (dist < 8)
  472. bias = dist - 1 - mn * 7;
  473. else
  474. bias = dist / 2 + 2 - mn * 7;
  475. for (y = 0; y < 4; y++) {
  476. for (x = 0; x < 4; x++) {
  477. int alp = block[3 + x * 4 + y * stride] * 7 + bias;
  478. int ind, tmp;
  479. /* This is a "linear scale" lerp factor between 0 (val=min)
  480. * and 7 (val=max) to select index. */
  481. tmp = (alp >= dist4) ? -1 : 0;
  482. ind = tmp & 4;
  483. alp -= dist4 & tmp;
  484. tmp = (alp >= dist2) ? -1 : 0;
  485. ind += tmp & 2;
  486. alp -= dist2 & tmp;
  487. ind += (alp >= dist);
  488. /* Turn linear scale into DXT index (0/1 are extreme points) */
  489. ind = -ind & 7;
  490. ind ^= (2 > ind);
  491. /* Write index */
  492. mask |= ind << bits;
  493. bits += 3;
  494. if (bits >= 8) {
  495. *dst++ = mask;
  496. mask >>= 8;
  497. bits -= 8;
  498. }
  499. }
  500. }
  501. }
  502. /**
  503. * Convert a RGBA buffer to unscaled YCoCg.
  504. * Scale is usually introduced to avoid banding over a certain range of colors,
  505. * but this version of the algorithm does not introduce it as much as other
  506. * implementations, allowing for a simpler and faster conversion.
  507. */
  508. static void rgba2ycocg(uint8_t *dst, const uint8_t *pixel)
  509. {
  510. int r = pixel[0];
  511. int g = (pixel[1] + 1) >> 1;
  512. int b = pixel[2];
  513. int t = (2 + r + b) >> 2;
  514. dst[0] = av_clip_uint8(128 + ((r - b + 1) >> 1)); /* Co */
  515. dst[1] = av_clip_uint8(128 + g - t); /* Cg */
  516. dst[2] = 0;
  517. dst[3] = av_clip_uint8(g + t); /* Y */
  518. }
  519. /**
  520. * Compress one block of RGBA pixels in a DXT1 texture and store the
  521. * resulting bytes in 'dst'. Alpha is not preserved.
  522. *
  523. * @param dst output buffer.
  524. * @param stride scanline in bytes.
  525. * @param block block to compress.
  526. * @return how much texture data has been written.
  527. */
  528. static int dxt1_block(uint8_t *dst, ptrdiff_t stride, const uint8_t *block)
  529. {
  530. compress_color(dst, stride, block);
  531. return 8;
  532. }
  533. /**
  534. * Compress one block of RGBA pixels in a DXT5 texture and store the
  535. * resulting bytes in 'dst'. Alpha is preserved.
  536. *
  537. * @param dst output buffer.
  538. * @param stride scanline in bytes.
  539. * @param block block to compress.
  540. * @return how much texture data has been written.
  541. */
  542. static int dxt5_block(uint8_t *dst, ptrdiff_t stride, const uint8_t *block)
  543. {
  544. compress_alpha(dst, stride, block);
  545. compress_color(dst + 8, stride, block);
  546. return 16;
  547. }
  548. /**
  549. * Compress one block of RGBA pixels in a DXT5-YCoCg texture and store the
  550. * resulting bytes in 'dst'. Alpha is not preserved.
  551. *
  552. * @param dst output buffer.
  553. * @param stride scanline in bytes.
  554. * @param block block to compress.
  555. * @return how much texture data has been written.
  556. */
  557. static int dxt5ys_block(uint8_t *dst, ptrdiff_t stride, const uint8_t *block)
  558. {
  559. int x, y;
  560. uint8_t reorder[64];
  561. /* Reorder the components and then run a normal DXT5 compression. */
  562. for (y = 0; y < 4; y++)
  563. for (x = 0; x < 4; x++)
  564. rgba2ycocg(reorder + x * 4 + y * 16, block + x * 4 + y * stride);
  565. compress_alpha(dst + 0, 16, reorder);
  566. compress_color(dst + 8, 16, reorder);
  567. return 16;
  568. }
  569. av_cold void ff_texturedspenc_init(TextureDSPContext *c)
  570. {
  571. c->dxt1_block = dxt1_block;
  572. c->dxt5_block = dxt5_block;
  573. c->dxt5ys_block = dxt5ys_block;
  574. }