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.

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