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>
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.
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
28 #include "libavutil/attributes.h"
29 #include "libavutil/common.h"
30 #include "libavutil/intreadwrite.h"
31 #include "libavutil/libm.h"
33 #include "texturedsp.h"
35 #define RGBA(r, g, b, a) (((uint8_t)(r) << 0) | \
36 ((uint8_t)(g) << 8) | \
37 ((uint8_t)(b) << 16) | \
38 ((unsigned)(uint8_t)(a) << 24))
40 static av_always_inline void extract_color(uint32_t colors[4],
46 uint8_t r0, g0, b0, r1, g1, b1;
47 uint8_t a = dxtn ? 0 : 255;
49 tmp = (color0 >> 11) * 255 + 16;
50 r0 = (uint8_t) ((tmp / 32 + tmp) / 32);
51 tmp = ((color0 & 0x07E0) >> 5) * 255 + 32;
52 g0 = (uint8_t) ((tmp / 64 + tmp) / 64);
53 tmp = (color0 & 0x001F) * 255 + 16;
54 b0 = (uint8_t) ((tmp / 32 + tmp) / 32);
56 tmp = (color1 >> 11) * 255 + 16;
57 r1 = (uint8_t) ((tmp / 32 + tmp) / 32);
58 tmp = ((color1 & 0x07E0) >> 5) * 255 + 32;
59 g1 = (uint8_t) ((tmp / 64 + tmp) / 64);
60 tmp = (color1 & 0x001F) * 255 + 16;
61 b1 = (uint8_t) ((tmp / 32 + tmp) / 32);
63 if (dxtn || color0 > color1) {
64 colors[0] = RGBA(r0, g0, b0, a);
65 colors[1] = RGBA(r1, g1, b1, a);
66 colors[2] = RGBA((2 * r0 + r1) / 3,
70 colors[3] = RGBA((2 * r1 + r0) / 3,
75 colors[0] = RGBA(r0, g0, b0, a);
76 colors[1] = RGBA(r1, g1, b1, a);
77 colors[2] = RGBA((r0 + r1) / 2,
81 colors[3] = RGBA(0, 0, 0, alpha);
85 static inline void dxt1_block_internal(uint8_t *dst, ptrdiff_t stride,
86 const uint8_t *block, uint8_t alpha)
90 uint16_t color0 = AV_RL16(block + 0);
91 uint16_t color1 = AV_RL16(block + 2);
92 uint32_t code = AV_RL32(block + 4);
94 extract_color(colors, color0, color1, 0, alpha);
96 for (y = 0; y < 4; y++) {
97 for (x = 0; x < 4; x++) {
98 uint32_t pixel = colors[code & 3];
100 AV_WL32(dst + x * 4, pixel);
107 * Decompress one block of a DXT1 texture and store the resulting
108 * RGBA pixels in 'dst'. Alpha component is fully opaque.
110 * @param dst output buffer.
111 * @param stride scanline in bytes.
112 * @param block block to decompress.
113 * @return how much texture data has been consumed.
115 static int dxt1_block(uint8_t *dst, ptrdiff_t stride, const uint8_t *block)
117 dxt1_block_internal(dst, stride, block, 255);
123 * Decompress one block of a DXT1 with 1-bit alpha texture and store
124 * the resulting RGBA pixels in 'dst'. Alpha is either fully opaque or
127 * @param dst output buffer.
128 * @param stride scanline in bytes.
129 * @param block block to decompress.
130 * @return how much texture data has been consumed.
132 static int dxt1a_block(uint8_t *dst, ptrdiff_t stride, const uint8_t *block)
134 dxt1_block_internal(dst, stride, block, 0);
139 static inline void dxt3_block_internal(uint8_t *dst, ptrdiff_t stride,
140 const uint8_t *block)
144 uint16_t color0 = AV_RL16(block + 8);
145 uint16_t color1 = AV_RL16(block + 10);
146 uint32_t code = AV_RL32(block + 12);
148 extract_color(colors, color0, color1, 1, 0);
150 for (y = 0; y < 4; y++) {
151 const uint16_t alpha_code = AV_RL16(block + 2 * y);
152 uint8_t alpha_values[4];
154 alpha_values[0] = ((alpha_code >> 0) & 0x0F) * 17;
155 alpha_values[1] = ((alpha_code >> 4) & 0x0F) * 17;
156 alpha_values[2] = ((alpha_code >> 8) & 0x0F) * 17;
157 alpha_values[3] = ((alpha_code >> 12) & 0x0F) * 17;
159 for (x = 0; x < 4; x++) {
160 uint8_t alpha = alpha_values[x];
161 uint32_t pixel = colors[code & 3] | ((unsigned)alpha << 24);
164 AV_WL32(dst + x * 4, pixel);
170 /** Convert a premultiplied alpha pixel to a straight alpha pixel. */
171 static av_always_inline void premult2straight(uint8_t *src)
176 int a = src[3]; /* unchanged */
178 src[0] = (uint8_t) r * a / 255;
179 src[1] = (uint8_t) g * a / 255;
180 src[2] = (uint8_t) b * a / 255;
184 * Decompress one block of a DXT2 texture and store the resulting
185 * RGBA pixels in 'dst'.
187 * @param dst output buffer.
188 * @param stride scanline in bytes.
189 * @param block block to decompress.
190 * @return how much texture data has been consumed.
192 static int dxt2_block(uint8_t *dst, ptrdiff_t stride, const uint8_t *block)
196 dxt3_block_internal(dst, stride, block);
198 /* This format is DXT3, but returns premultiplied alpha. It needs to be
199 * converted because it's what lavc outputs (and swscale expects). */
200 for (y = 0; y < 4; y++)
201 for (x = 0; x < 4; x++)
202 premult2straight(dst + x * 4 + y * stride);
208 * Decompress one block of a DXT3 texture and store the resulting
209 * RGBA pixels in 'dst'.
211 * @param dst output buffer.
212 * @param stride scanline in bytes.
213 * @param block block to decompress.
214 * @return how much texture data has been consumed.
216 static int dxt3_block(uint8_t *dst, ptrdiff_t stride, const uint8_t *block)
218 dxt3_block_internal(dst, stride, block);
224 * Decompress a BC 16x3 index block stored as
231 * | h | g | f | e | d | c | b | a | // Entry
232 * |765 432 107 654 321 076 543 210| // Bit
233 * |0000000000111111111112222222222| // Byte
235 * into 16 8-bit indices.
237 static void decompress_indices(uint8_t *dst, const uint8_t *src)
241 for (block = 0; block < 2; block++) {
242 int tmp = AV_RL24(src);
244 /* Unpack 8x3 bit from last 3 byte block */
245 for (i = 0; i < 8; i++)
246 dst[i] = (tmp >> (i * 3)) & 0x7;
253 static inline void dxt5_block_internal(uint8_t *dst, ptrdiff_t stride,
254 const uint8_t *block)
258 uint8_t alpha_indices[16];
259 uint16_t color0 = AV_RL16(block + 8);
260 uint16_t color1 = AV_RL16(block + 10);
261 uint32_t code = AV_RL32(block + 12);
262 uint8_t alpha0 = *(block);
263 uint8_t alpha1 = *(block + 1);
265 decompress_indices(alpha_indices, block + 2);
267 extract_color(colors, color0, color1, 1, 0);
269 for (y = 0; y < 4; y++) {
270 for (x = 0; x < 4; x++) {
271 int alpha_code = alpha_indices[x + y * 4];
275 if (alpha_code == 0) {
277 } else if (alpha_code == 1) {
280 if (alpha0 > alpha1) {
281 alpha = (uint8_t) (((8 - alpha_code) * alpha0 +
282 (alpha_code - 1) * alpha1) / 7);
284 if (alpha_code == 6) {
286 } else if (alpha_code == 7) {
289 alpha = (uint8_t) (((6 - alpha_code) * alpha0 +
290 (alpha_code - 1) * alpha1) / 5);
294 pixel = colors[code & 3] | ((unsigned)alpha << 24);
296 AV_WL32(dst + x * 4, pixel);
303 * Decompress one block of a DXT4 texture and store the resulting
304 * RGBA pixels in 'dst'.
306 * @param dst output buffer.
307 * @param stride scanline in bytes.
308 * @param block block to decompress.
309 * @return how much texture data has been consumed.
311 static int dxt4_block(uint8_t *dst, ptrdiff_t stride, const uint8_t *block)
315 dxt5_block_internal(dst, stride, block);
317 /* This format is DXT5, but returns premultiplied alpha. It needs to be
318 * converted because it's what lavc outputs (and swscale expects). */
319 for (y = 0; y < 4; y++)
320 for (x = 0; x < 4; x++)
321 premult2straight(dst + x * 4 + y * stride);
327 * Decompress one block of a DXT5 texture and store the resulting
328 * RGBA pixels in 'dst'.
330 * @param dst output buffer.
331 * @param stride scanline in bytes.
332 * @param block block to decompress.
333 * @return how much texture data has been consumed.
335 static int dxt5_block(uint8_t *dst, ptrdiff_t stride, const uint8_t *block)
337 dxt5_block_internal(dst, stride, block);
343 * Convert a YCoCg buffer to RGBA.
345 * @param src input buffer.
346 * @param scaled variant with scaled chroma components and opaque alpha.
348 static av_always_inline void ycocg2rgba(uint8_t *src, int scaled)
355 int s = scaled ? (b >> 3) + 1 : 1;
357 int co = (r - 128) / s;
358 int cg = (g - 128) / s;
360 src[0] = av_clip_uint8(y + co - cg);
361 src[1] = av_clip_uint8(y + cg);
362 src[2] = av_clip_uint8(y - co - cg);
363 src[3] = scaled ? 255 : b;
367 * Decompress one block of a DXT5 texture with classic YCoCg and store
368 * the resulting RGBA pixels in 'dst'. Alpha component is fully opaque.
370 * @param dst output buffer.
371 * @param stride scanline in bytes.
372 * @param block block to decompress.
373 * @return how much texture data has been consumed.
375 static int dxt5y_block(uint8_t *dst, ptrdiff_t stride, const uint8_t *block)
379 /* This format is basically DXT5, with luma stored in alpha.
380 * Run a normal decompress and then reorder the components. */
381 dxt5_block_internal(dst, stride, block);
383 for (y = 0; y < 4; y++)
384 for (x = 0; x < 4; x++)
385 ycocg2rgba(dst + x * 4 + y * stride, 0);
391 * Decompress one block of a DXT5 texture with scaled YCoCg and store
392 * the resulting RGBA pixels in 'dst'. Alpha component is fully opaque.
394 * @param dst output buffer.
395 * @param stride scanline in bytes.
396 * @param block block to decompress.
397 * @return how much texture data has been consumed.
399 static int dxt5ys_block(uint8_t *dst, ptrdiff_t stride, const uint8_t *block)
403 /* This format is basically DXT5, with luma stored in alpha.
404 * Run a normal decompress and then reorder the components. */
405 dxt5_block_internal(dst, stride, block);
407 for (y = 0; y < 4; y++)
408 for (x = 0; x < 4; x++)
409 ycocg2rgba(dst + x * 4 + y * stride, 1);
414 static inline void rgtc_block_internal(uint8_t *dst, ptrdiff_t stride,
415 const uint8_t *block,
416 const int *color_tab, int alpha)
421 decompress_indices(indices, block + 2);
423 /* Only one or two channels are stored at most, since it only used to
424 * compress specular (black and white) or normal (red and green) maps.
425 * Although the standard says to zero out unused components, many
426 * implementations fill all of them with the same value. */
427 for (y = 0; y < 4; y++) {
428 for (x = 0; x < 4; x++) {
429 int i = indices[x + y * 4];
430 /* Interval expansion from [-1 1] or [0 1] to [0 255]. */
431 int c = color_tab[i];
434 dst [x * 4 + y * stride + 3] = (uint8_t)c;
437 uint32_t pixel = RGBA(c, c, c, 255U);
438 AV_WL32(dst + x * 4 + y * stride, pixel);
444 static inline void rgtc1_block_internal(uint8_t *dst, ptrdiff_t stride,
445 const uint8_t *block, int sign, int alpha)
451 /* signed data is in [-128 127] so just offset it to unsigned
452 * and it can be treated exactly the same */
453 r0 = ((int8_t) block[0]) + 128;
454 r1 = ((int8_t) block[1]) + 128;
464 /* 6 interpolated color values */
465 color_table[2] = (6 * r0 + 1 * r1) / 7; // bit code 010
466 color_table[3] = (5 * r0 + 2 * r1) / 7; // bit code 011
467 color_table[4] = (4 * r0 + 3 * r1) / 7; // bit code 100
468 color_table[5] = (3 * r0 + 4 * r1) / 7; // bit code 101
469 color_table[6] = (2 * r0 + 5 * r1) / 7; // bit code 110
470 color_table[7] = (1 * r0 + 6 * r1) / 7; // bit code 111
472 /* 4 interpolated color values */
473 color_table[2] = (4 * r0 + 1 * r1) / 5; // bit code 010
474 color_table[3] = (3 * r0 + 2 * r1) / 5; // bit code 011
475 color_table[4] = (2 * r0 + 3 * r1) / 5; // bit code 100
476 color_table[5] = (1 * r0 + 4 * r1) / 5; // bit code 101
477 color_table[6] = 0; /* min range */ // bit code 110
478 color_table[7] = 255; /* max range */ // bit code 111
481 rgtc_block_internal(dst, stride, block, color_table, alpha);
485 * Decompress one block of a RGRC1 texture with signed components
486 * and store the resulting RGBA pixels in 'dst'.
488 * @param dst output buffer.
489 * @param stride scanline in bytes.
490 * @param block block to decompress.
491 * @return how much texture data has been consumed.
493 static int rgtc1s_block(uint8_t *dst, ptrdiff_t stride, const uint8_t *block)
495 rgtc1_block_internal(dst, stride, block, 1, 0);
501 * Decompress one block of a RGRC1 texture with unsigned components
502 * and store the resulting RGBA pixels in 'dst'.
504 * @param dst output buffer.
505 * @param stride scanline in bytes.
506 * @param block block to decompress.
507 * @return how much texture data has been consumed.
509 static int rgtc1u_block(uint8_t *dst, ptrdiff_t stride, const uint8_t *block)
511 rgtc1_block_internal(dst, stride, block, 0, 0);
517 * Decompress one block of a RGRC1 texture with unsigned components
518 * and overwrite the alpha component in 'dst' (RGBA data).
520 * @param dst output buffer.
521 * @param stride scanline in bytes.
522 * @param block block to decompress.
523 * @return how much texture data has been consumed.
525 static int rgtc1u_alpha_block(uint8_t *dst, ptrdiff_t stride, const uint8_t *block)
527 rgtc1_block_internal(dst, stride, block, 0, 1);
532 static inline void rgtc2_block_internal(uint8_t *dst, ptrdiff_t stride,
533 const uint8_t *block, int sign)
535 /* 4x4 block containing 4 component pixels. */
536 uint8_t c0[4 * 4 * 4];
537 uint8_t c1[4 * 4 * 4];
540 /* Decompress the two channels separately and interleave them afterwards. */
541 rgtc1_block_internal(c0, 16, block, sign, 0);
542 rgtc1_block_internal(c1, 16, block + 8, sign, 0);
544 /* B is rebuilt exactly like a normal map. */
545 for (y = 0; y < 4; y++) {
546 for (x = 0; x < 4; x++) {
547 uint8_t *p = dst + x * 4 + y * stride;
548 int r = c0[x * 4 + y * 16];
549 int g = c1[x * 4 + y * 16];
552 int d = (255 * 255 - r * r - g * g) / 2;
565 * Decompress one block of a RGRC2 texture with signed components
566 * and store the resulting RGBA pixels in 'dst'. Alpha is fully opaque.
568 * @param dst output buffer.
569 * @param stride scanline in bytes.
570 * @param block block to decompress.
571 * @return how much texture data has been consumed.
573 static int rgtc2s_block(uint8_t *dst, ptrdiff_t stride, const uint8_t *block)
575 rgtc2_block_internal(dst, stride, block, 1);
581 * Decompress one block of a RGRC2 texture with unsigned components
582 * and store the resulting RGBA pixels in 'dst'. Alpha is fully opaque.
584 * @param dst output buffer.
585 * @param stride scanline in bytes.
586 * @param block block to decompress.
587 * @return how much texture data has been consumed.
589 static int rgtc2u_block(uint8_t *dst, ptrdiff_t stride, const uint8_t *block)
591 rgtc2_block_internal(dst, stride, block, 0);
597 * Decompress one block of a 3Dc texture with unsigned components
598 * and store the resulting RGBA pixels in 'dst'. Alpha is fully opaque.
600 * @param dst output buffer.
601 * @param stride scanline in bytes.
602 * @param block block to decompress.
603 * @return how much texture data has been consumed.
605 static int dxn3dc_block(uint8_t *dst, ptrdiff_t stride, const uint8_t *block)
608 rgtc2_block_internal(dst, stride, block, 0);
610 /* This is the 3Dc variant of RGTC2, with swapped R and G. */
611 for (y = 0; y < 4; y++) {
612 for (x = 0; x < 4; x++) {
613 uint8_t *p = dst + x * 4 + y * stride;
614 FFSWAP(uint8_t, p[0], p[1]);
621 av_cold void ff_texturedsp_init(TextureDSPContext *c)
623 c->dxt1_block = dxt1_block;
624 c->dxt1a_block = dxt1a_block;
625 c->dxt2_block = dxt2_block;
626 c->dxt3_block = dxt3_block;
627 c->dxt4_block = dxt4_block;
628 c->dxt5_block = dxt5_block;
629 c->dxt5y_block = dxt5y_block;
630 c->dxt5ys_block = dxt5ys_block;
631 c->rgtc1s_block = rgtc1s_block;
632 c->rgtc1u_block = rgtc1u_block;
633 c->rgtc1u_alpha_block = rgtc1u_alpha_block;
634 c->rgtc2s_block = rgtc2s_block;
635 c->rgtc2u_block = rgtc2u_block;
636 c->dxn3dc_block = dxn3dc_block;