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.

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