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