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.

615 lines
18KB

  1. /*
  2. * Texture block decompression
  3. * Copyright (C) 2009 Benjamin Dobell, Glass Echidna
  4. * Copyright (C) 2012 Matthäus G. "Anteru" Chajdas (http://anteru.net)
  5. * Copyright (C) 2015 Vittorio Giovara <vittorio.giovara@gmail.com>
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the "Software"),
  9. * to deal in the Software without restriction, including without limitation
  10. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  11. * and/or sell copies of the Software, and to permit persons to whom the
  12. * Software is furnished to do so, subject to the following conditions:
  13. * The above copyright notice and this permission notice shall be included
  14. * in all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  22. * IN THE SOFTWARE.
  23. */
  24. #include <stddef.h>
  25. #include <stdint.h>
  26. #include "libavutil/attributes.h"
  27. #include "libavutil/common.h"
  28. #include "libavutil/intreadwrite.h"
  29. #include "libavutil/libm.h"
  30. #include "texturedsp.h"
  31. #define RGBA(r, g, b, a) (((uint8_t)(r) << 0) | \
  32. ((uint8_t)(g) << 8) | \
  33. ((uint8_t)(b) << 16) | \
  34. ((unsigned)(uint8_t)(a) << 24))
  35. static av_always_inline void extract_color(uint32_t colors[4],
  36. uint16_t color0,
  37. uint16_t color1,
  38. int dxtn, int alpha)
  39. {
  40. int tmp;
  41. uint8_t r0, g0, b0, r1, g1, b1;
  42. uint8_t a = dxtn ? 0 : 255;
  43. tmp = (color0 >> 11) * 255 + 16;
  44. r0 = (uint8_t) ((tmp / 32 + tmp) / 32);
  45. tmp = ((color0 & 0x07E0) >> 5) * 255 + 32;
  46. g0 = (uint8_t) ((tmp / 64 + tmp) / 64);
  47. tmp = (color0 & 0x001F) * 255 + 16;
  48. b0 = (uint8_t) ((tmp / 32 + tmp) / 32);
  49. tmp = (color1 >> 11) * 255 + 16;
  50. r1 = (uint8_t) ((tmp / 32 + tmp) / 32);
  51. tmp = ((color1 & 0x07E0) >> 5) * 255 + 32;
  52. g1 = (uint8_t) ((tmp / 64 + tmp) / 64);
  53. tmp = (color1 & 0x001F) * 255 + 16;
  54. b1 = (uint8_t) ((tmp / 32 + tmp) / 32);
  55. if (dxtn || color0 > color1) {
  56. colors[0] = RGBA(r0, g0, b0, a);
  57. colors[1] = RGBA(r1, g1, b1, a);
  58. colors[2] = RGBA((2 * r0 + r1) / 3,
  59. (2 * g0 + g1) / 3,
  60. (2 * b0 + b1) / 3,
  61. a);
  62. colors[3] = RGBA((2 * r1 + r0) / 3,
  63. (2 * g1 + g0) / 3,
  64. (2 * b1 + b0) / 3,
  65. a);
  66. } else {
  67. colors[0] = RGBA(r0, g0, b0, a);
  68. colors[1] = RGBA(r1, g1, b1, a);
  69. colors[2] = RGBA((r0 + r1) / 2,
  70. (g0 + g1) / 2,
  71. (b0 + b1) / 2,
  72. a);
  73. colors[3] = RGBA(0, 0, 0, alpha);
  74. }
  75. }
  76. static inline void dxt1_block_internal(uint8_t *dst, ptrdiff_t stride,
  77. const uint8_t *block, uint8_t alpha)
  78. {
  79. int x, y;
  80. uint32_t colors[4];
  81. uint16_t color0 = AV_RL16(block + 0);
  82. uint16_t color1 = AV_RL16(block + 2);
  83. uint32_t code = AV_RL32(block + 4);
  84. extract_color(colors, color0, color1, 0, alpha);
  85. for (y = 0; y < 4; y++) {
  86. for (x = 0; x < 4; x++) {
  87. uint32_t pixel = colors[code & 3];
  88. code >>= 2;
  89. AV_WL32(dst + x * 4, pixel);
  90. }
  91. dst += stride;
  92. }
  93. }
  94. /**
  95. * Decompress one block of a DXT1 texture and store the resulting
  96. * RGBA pixels in 'dst'. Alpha component is fully opaque.
  97. *
  98. * @param dst output buffer.
  99. * @param stride scanline in bytes.
  100. * @param block block to decompress.
  101. * @return how much texture data has been consumed.
  102. */
  103. static int dxt1_block(uint8_t *dst, ptrdiff_t stride, const uint8_t *block)
  104. {
  105. dxt1_block_internal(dst, stride, block, 255);
  106. return 8;
  107. }
  108. /**
  109. * Decompress one block of a DXT1 with 1-bit alpha texture and store
  110. * the resulting RGBA pixels in 'dst'. Alpha is either fully opaque or
  111. * fully transparent.
  112. *
  113. * @param dst output buffer.
  114. * @param stride scanline in bytes.
  115. * @param block block to decompress.
  116. * @return how much texture data has been consumed.
  117. */
  118. static int dxt1a_block(uint8_t *dst, ptrdiff_t stride, const uint8_t *block)
  119. {
  120. dxt1_block_internal(dst, stride, block, 0);
  121. return 8;
  122. }
  123. static inline void dxt3_block_internal(uint8_t *dst, ptrdiff_t stride,
  124. const uint8_t *block)
  125. {
  126. int x, y;
  127. uint32_t colors[4];
  128. uint16_t color0 = AV_RL16(block + 8);
  129. uint16_t color1 = AV_RL16(block + 10);
  130. uint32_t code = AV_RL32(block + 12);
  131. extract_color(colors, color0, color1, 1, 0);
  132. for (y = 0; y < 4; y++) {
  133. const uint16_t alpha_code = AV_RL16(block + 2 * y);
  134. uint8_t alpha_values[4];
  135. alpha_values[0] = ((alpha_code >> 0) & 0x0F) * 17;
  136. alpha_values[1] = ((alpha_code >> 4) & 0x0F) * 17;
  137. alpha_values[2] = ((alpha_code >> 8) & 0x0F) * 17;
  138. alpha_values[3] = ((alpha_code >> 12) & 0x0F) * 17;
  139. for (x = 0; x < 4; x++) {
  140. uint8_t alpha = alpha_values[x];
  141. uint32_t pixel = colors[code & 3] | (alpha << 24);
  142. code >>= 2;
  143. AV_WL32(dst + x * 4, pixel);
  144. }
  145. dst += stride;
  146. }
  147. }
  148. /** Convert a premultiplied alpha pixel to a straight alpha pixel. */
  149. static av_always_inline void premult2straight(uint8_t *src)
  150. {
  151. int r = src[0];
  152. int g = src[1];
  153. int b = src[2];
  154. int a = src[3]; /* unchanged */
  155. src[0] = (uint8_t) r * a / 255;
  156. src[1] = (uint8_t) g * a / 255;
  157. src[2] = (uint8_t) b * a / 255;
  158. }
  159. /**
  160. * Decompress one block of a DXT2 texture and store the resulting
  161. * RGBA pixels in 'dst'.
  162. *
  163. * @param dst output buffer.
  164. * @param stride scanline in bytes.
  165. * @param block block to decompress.
  166. * @return how much texture data has been consumed.
  167. */
  168. static int dxt2_block(uint8_t *dst, ptrdiff_t stride, const uint8_t *block)
  169. {
  170. int x, y;
  171. dxt3_block_internal(dst, stride, block);
  172. /* This format is DXT3, but returns premultiplied alpha. It needs to be
  173. * converted because it's what lavc outputs (and swscale expects). */
  174. for (y = 0; y < 4; y++)
  175. for (x = 0; x < 4; x++)
  176. premult2straight(dst + x * 4 + y * stride);
  177. return 16;
  178. }
  179. /**
  180. * Decompress one block of a DXT3 texture and store the resulting
  181. * RGBA pixels in 'dst'.
  182. *
  183. * @param dst output buffer.
  184. * @param stride scanline in bytes.
  185. * @param block block to decompress.
  186. * @return how much texture data has been consumed.
  187. */
  188. static int dxt3_block(uint8_t *dst, ptrdiff_t stride, const uint8_t *block)
  189. {
  190. dxt3_block_internal(dst, stride, block);
  191. return 16;
  192. }
  193. /**
  194. * Decompress a BC 16x3 index block stored as
  195. * h g f e
  196. * d c b a
  197. * p o n m
  198. * l k j i
  199. *
  200. * Bits packed as
  201. * | h | g | f | e | d | c | b | a | // Entry
  202. * |765 432 107 654 321 076 543 210| // Bit
  203. * |0000000000111111111112222222222| // Byte
  204. *
  205. * into 16 8-bit indices.
  206. */
  207. static void decompress_indices(uint8_t *dst, const uint8_t *src)
  208. {
  209. int block, i;
  210. for (block = 0; block < 2; block++) {
  211. int tmp = AV_RL24(src);
  212. /* Unpack 8x3 bit from last 3 byte block */
  213. for (i = 0; i < 8; i++)
  214. dst[i] = (tmp >> (i * 3)) & 0x7;
  215. src += 3;
  216. dst += 8;
  217. }
  218. }
  219. static inline void dxt5_block_internal(uint8_t *dst, ptrdiff_t stride,
  220. const uint8_t *block)
  221. {
  222. int x, y;
  223. uint32_t colors[4];
  224. uint8_t alpha_indices[16];
  225. uint16_t color0 = AV_RL16(block + 8);
  226. uint16_t color1 = AV_RL16(block + 10);
  227. uint32_t code = AV_RL32(block + 12);
  228. uint8_t alpha0 = *(block);
  229. uint8_t alpha1 = *(block + 1);
  230. decompress_indices(alpha_indices, block + 2);
  231. extract_color(colors, color0, color1, 1, 0);
  232. for (y = 0; y < 4; y++) {
  233. for (x = 0; x < 4; x++) {
  234. int alpha_code = alpha_indices[x + y * 4];
  235. uint32_t pixel;
  236. uint8_t alpha;
  237. if (alpha_code == 0) {
  238. alpha = alpha0;
  239. } else if (alpha_code == 1) {
  240. alpha = alpha1;
  241. } else {
  242. if (alpha0 > alpha1) {
  243. alpha = (uint8_t) (((8 - alpha_code) * alpha0 +
  244. (alpha_code - 1) * alpha1) / 7);
  245. } else {
  246. if (alpha_code == 6) {
  247. alpha = 0;
  248. } else if (alpha_code == 7) {
  249. alpha = 255;
  250. } else {
  251. alpha = (uint8_t) (((6 - alpha_code) * alpha0 +
  252. (alpha_code - 1) * alpha1) / 5);
  253. }
  254. }
  255. }
  256. pixel = colors[code & 3] | (alpha << 24);
  257. code >>= 2;
  258. AV_WL32(dst + x * 4, pixel);
  259. }
  260. dst += stride;
  261. }
  262. }
  263. /**
  264. * Decompress one block of a DXT4 texture and store the resulting
  265. * RGBA pixels in 'dst'.
  266. *
  267. * @param dst output buffer.
  268. * @param stride scanline in bytes.
  269. * @param block block to decompress.
  270. * @return how much texture data has been consumed.
  271. */
  272. static int dxt4_block(uint8_t *dst, ptrdiff_t stride, const uint8_t *block)
  273. {
  274. int x, y;
  275. dxt5_block_internal(dst, stride, block);
  276. /* This format is DXT5, but returns premultiplied alpha. It needs to be
  277. * converted because it's what lavc outputs (and swscale expects). */
  278. for (y = 0; y < 4; y++)
  279. for (x = 0; x < 4; x++)
  280. premult2straight(dst + x * 4 + y * stride);
  281. return 16;
  282. }
  283. /**
  284. * Decompress one block of a DXT5 texture and store the resulting
  285. * RGBA pixels in 'dst'.
  286. *
  287. * @param dst output buffer.
  288. * @param stride scanline in bytes.
  289. * @param block block to decompress.
  290. * @return how much texture data has been consumed.
  291. */
  292. static int dxt5_block(uint8_t *dst, ptrdiff_t stride, const uint8_t *block)
  293. {
  294. dxt5_block_internal(dst, stride, block);
  295. return 16;
  296. }
  297. /**
  298. * Convert a YCoCg buffer to RGBA.
  299. *
  300. * @param src input buffer.
  301. * @param scaled variant with scaled chroma components and opaque alpha.
  302. */
  303. static av_always_inline void ycocg2rgba(uint8_t *src, int scaled)
  304. {
  305. int r = src[0];
  306. int g = src[1];
  307. int b = src[2];
  308. int a = src[3];
  309. int s = scaled ? (b >> 3) + 1 : 1;
  310. int y = a;
  311. int co = (r - 128) / s;
  312. int cg = (g - 128) / s;
  313. src[0] = av_clip_uint8(y + co - cg);
  314. src[1] = av_clip_uint8(y + cg);
  315. src[2] = av_clip_uint8(y - co - cg);
  316. src[3] = scaled ? 255 : b;
  317. }
  318. /**
  319. * Decompress one block of a DXT5 texture with classic YCoCg and store
  320. * the resulting RGBA pixels in 'dst'. Alpha component is fully opaque.
  321. *
  322. * @param dst output buffer.
  323. * @param stride scanline in bytes.
  324. * @param block block to decompress.
  325. * @return how much texture data has been consumed.
  326. */
  327. static int dxt5y_block(uint8_t *dst, ptrdiff_t stride, const uint8_t *block)
  328. {
  329. int x, y;
  330. /* This format is basically DXT5, with luma stored in alpha.
  331. * Run a normal decompress and then reorder the components. */
  332. dxt5_block_internal(dst, stride, block);
  333. for (y = 0; y < 4; y++)
  334. for (x = 0; x < 4; x++)
  335. ycocg2rgba(dst + x * 4 + y * stride, 0);
  336. return 16;
  337. }
  338. /**
  339. * Decompress one block of a DXT5 texture with scaled YCoCg and store
  340. * the resulting RGBA pixels in 'dst'. Alpha component is fully opaque.
  341. *
  342. * @param dst output buffer.
  343. * @param stride scanline in bytes.
  344. * @param block block to decompress.
  345. * @return how much texture data has been consumed.
  346. */
  347. static int dxt5ys_block(uint8_t *dst, ptrdiff_t stride, const uint8_t *block)
  348. {
  349. int x, y;
  350. /* This format is basically DXT5, with luma stored in alpha.
  351. * Run a normal decompress and then reorder the components. */
  352. dxt5_block_internal(dst, stride, block);
  353. for (y = 0; y < 4; y++)
  354. for (x = 0; x < 4; x++)
  355. ycocg2rgba(dst + x * 4 + y * stride, 1);
  356. return 16;
  357. }
  358. static inline void rgtc_block_internal(uint8_t *dst, ptrdiff_t stride,
  359. const uint8_t *block,
  360. const int *color_tab)
  361. {
  362. uint8_t indices[16];
  363. int x, y;
  364. decompress_indices(indices, block + 2);
  365. /* Only one or two channels are stored at most, since it only used to
  366. * compress specular (black and white) or normal (red and green) maps.
  367. * Although the standard says to zero out unused components, many
  368. * implementations fill all of them with the same value. */
  369. for (y = 0; y < 4; y++) {
  370. for (x = 0; x < 4; x++) {
  371. int i = indices[x + y * 4];
  372. /* Interval expansion from [-1 1] or [0 1] to [0 255]. */
  373. int c = color_tab[i];
  374. uint32_t pixel = RGBA(c, c, c, 255U);
  375. AV_WL32(dst + x * 4 + y * stride, pixel);
  376. }
  377. }
  378. }
  379. static inline void rgtc1_block_internal(uint8_t *dst, ptrdiff_t stride,
  380. const uint8_t *block, int sign)
  381. {
  382. int color_table[8];
  383. int r0, r1;
  384. if (sign) {
  385. /* signed data is in [-128 127] so just offset it to unsigned
  386. * and it can be treated exactly the same */
  387. r0 = ((int8_t) block[0]) + 128;
  388. r1 = ((int8_t) block[1]) + 128;
  389. } else {
  390. r0 = block[0];
  391. r1 = block[1];
  392. }
  393. color_table[0] = r0;
  394. color_table[1] = r1;
  395. if (r0 > r1) {
  396. /* 6 interpolated color values */
  397. color_table[2] = (6 * r0 + 1 * r1) / 7; // bit code 010
  398. color_table[3] = (5 * r0 + 2 * r1) / 7; // bit code 011
  399. color_table[4] = (4 * r0 + 3 * r1) / 7; // bit code 100
  400. color_table[5] = (3 * r0 + 4 * r1) / 7; // bit code 101
  401. color_table[6] = (2 * r0 + 5 * r1) / 7; // bit code 110
  402. color_table[7] = (1 * r0 + 6 * r1) / 7; // bit code 111
  403. } else {
  404. /* 4 interpolated color values */
  405. color_table[2] = (4 * r0 + 1 * r1) / 5; // bit code 010
  406. color_table[3] = (3 * r0 + 2 * r1) / 5; // bit code 011
  407. color_table[4] = (2 * r0 + 3 * r1) / 5; // bit code 100
  408. color_table[5] = (1 * r0 + 4 * r1) / 5; // bit code 101
  409. color_table[6] = 0; /* min range */ // bit code 110
  410. color_table[7] = 255; /* max range */ // bit code 111
  411. }
  412. rgtc_block_internal(dst, stride, block, color_table);
  413. }
  414. /**
  415. * Decompress one block of a RGRC1 texture with signed components
  416. * and store the resulting RGBA pixels in 'dst'.
  417. *
  418. * @param dst output buffer.
  419. * @param stride scanline in bytes.
  420. * @param block block to decompress.
  421. * @return how much texture data has been consumed.
  422. */
  423. static int rgtc1s_block(uint8_t *dst, ptrdiff_t stride, const uint8_t *block)
  424. {
  425. rgtc1_block_internal(dst, stride, block, 1);
  426. return 8;
  427. }
  428. /**
  429. * Decompress one block of a RGRC1 texture with unsigned components
  430. * and store the resulting RGBA pixels in 'dst'.
  431. *
  432. * @param dst output buffer.
  433. * @param stride scanline in bytes.
  434. * @param block block to decompress.
  435. * @return how much texture data has been consumed.
  436. */
  437. static int rgtc1u_block(uint8_t *dst, ptrdiff_t stride, const uint8_t *block)
  438. {
  439. rgtc1_block_internal(dst, stride, block, 0);
  440. return 8;
  441. }
  442. static inline void rgtc2_block_internal(uint8_t *dst, ptrdiff_t stride,
  443. const uint8_t *block, int sign)
  444. {
  445. /* 4x4 block containing 4 component pixels. */
  446. uint8_t c0[4 * 4 * 4];
  447. uint8_t c1[4 * 4 * 4];
  448. int x, y;
  449. /* Decompress the two channels separately and interleave them afterwards. */
  450. rgtc1_block_internal(c0, 16, block, sign);
  451. rgtc1_block_internal(c1, 16, block + 8, sign);
  452. /* B is rebuilt exactly like a normal map. */
  453. for (y = 0; y < 4; y++) {
  454. for (x = 0; x < 4; x++) {
  455. uint8_t *p = dst + x * 4 + y * stride;
  456. int r = c0[x * 4 + y * 16];
  457. int g = c1[x * 4 + y * 16];
  458. int b = 127;
  459. int d = (255 * 255 - r * r - g * g) / 2;
  460. if (d > 0)
  461. b = lrint(sqrtf(d));
  462. p[0] = r;
  463. p[1] = g;
  464. p[2] = b;
  465. p[3] = 255;
  466. }
  467. }
  468. }
  469. /**
  470. * Decompress one block of a RGRC2 texture with signed components
  471. * and store the resulting RGBA pixels in 'dst'. Alpha is fully opaque.
  472. *
  473. * @param dst output buffer.
  474. * @param stride scanline in bytes.
  475. * @param block block to decompress.
  476. * @return how much texture data has been consumed.
  477. */
  478. static int rgtc2s_block(uint8_t *dst, ptrdiff_t stride, const uint8_t *block)
  479. {
  480. rgtc2_block_internal(dst, stride, block, 1);
  481. return 16;
  482. }
  483. /**
  484. * Decompress one block of a RGRC2 texture with unsigned components
  485. * and store the resulting RGBA pixels in 'dst'. Alpha is fully opaque.
  486. *
  487. * @param dst output buffer.
  488. * @param stride scanline in bytes.
  489. * @param block block to decompress.
  490. * @return how much texture data has been consumed.
  491. */
  492. static int rgtc2u_block(uint8_t *dst, ptrdiff_t stride, const uint8_t *block)
  493. {
  494. rgtc2_block_internal(dst, stride, block, 0);
  495. return 16;
  496. }
  497. /**
  498. * Decompress one block of a 3Dc texture with unsigned components
  499. * and store the resulting RGBA pixels in 'dst'. Alpha is fully opaque.
  500. *
  501. * @param dst output buffer.
  502. * @param stride scanline in bytes.
  503. * @param block block to decompress.
  504. * @return how much texture data has been consumed.
  505. */
  506. static int dxn3dc_block(uint8_t *dst, ptrdiff_t stride, const uint8_t *block)
  507. {
  508. int x, y;
  509. rgtc2_block_internal(dst, stride, block, 0);
  510. /* This is the 3Dc variant of RGTC2, with swapped R and G. */
  511. for (y = 0; y < 4; y++) {
  512. for (x = 0; x < 4; x++) {
  513. uint8_t *p = dst + x * 4 + y * stride;
  514. FFSWAP(uint8_t, p[0], p[1]);
  515. }
  516. }
  517. return 16;
  518. }
  519. av_cold void ff_texturedsp_init(TextureDSPContext *c)
  520. {
  521. c->dxt1_block = dxt1_block;
  522. c->dxt1a_block = dxt1a_block;
  523. c->dxt2_block = dxt2_block;
  524. c->dxt3_block = dxt3_block;
  525. c->dxt4_block = dxt4_block;
  526. c->dxt5_block = dxt5_block;
  527. c->dxt5y_block = dxt5y_block;
  528. c->dxt5ys_block = dxt5ys_block;
  529. c->rgtc1s_block = rgtc1s_block;
  530. c->rgtc1u_block = rgtc1u_block;
  531. c->rgtc2s_block = rgtc2s_block;
  532. c->rgtc2u_block = rgtc2u_block;
  533. c->dxn3dc_block = dxn3dc_block;
  534. }