1 /*
2 * DirectDraw Surface image decoder
3 * Copyright (C) 2015 Vittorio Giovara <vittorio.giovara@gmail.com>
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /**
23 * @file
24 * DDS decoder
25 *
26 * https://msdn.microsoft.com/en-us/library/bb943982%28v=vs.85%29.aspx
27 */
28
29 #include <stdint.h>
30
33
39
40 #define DDPF_FOURCC (1 << 2)
41 #define DDPF_PALETTE (1 << 5)
42 #define DDPF_NORMALMAP (1U << 31)
43
58 };
59
67
74
97 };
98
102
107
110
112 {
118 int alpha_exponent, ycocg_classic, ycocg_scaled, normal_map,
array;
119
120 /* Alternative DDS implementations use reserved1 as custom header. */
122 gimp_tag = bytestream2_get_le32(gbc);
123 alpha_exponent = gimp_tag ==
MKTAG(
'A',
'E',
'X',
'P');
124 ycocg_classic = gimp_tag ==
MKTAG(
'Y',
'C',
'G',
'1');
125 ycocg_scaled = gimp_tag ==
MKTAG(
'Y',
'C',
'G',
'2');
127
128 /* Now the real DDPF starts. */
129 size = bytestream2_get_le32(gbc);
133 }
134 flags = bytestream2_get_le32(gbc);
138 fourcc = bytestream2_get_le32(gbc);
139
140 if (
ctx->compressed &&
ctx->paletted) {
142 "Disabling invalid palette flag for compressed dds.\n");
144 }
145
146 bpp =
ctx->bpp = bytestream2_get_le32(gbc);
// rgbbitcount
147 r = bytestream2_get_le32(gbc);
// rbitmask
148 g = bytestream2_get_le32(gbc);
// gbitmask
149 b = bytestream2_get_le32(gbc);
// bbitmask
150 a = bytestream2_get_le32(gbc);
// abitmask
151
157
160 if (gimp_tag)
162
165
166 if (
ctx->compressed) {
167 ctx->dec.raw_ratio = 16;
169 case MKTAG(
'D',
'X',
'T',
'1'):
170 ctx->dec.tex_ratio = 8;
171 ctx->dec.tex_funct =
ctx->texdsp.dxt1a_block;
172 break;
173 case MKTAG(
'D',
'X',
'T',
'2'):
174 ctx->dec.tex_ratio = 16;
175 ctx->dec.tex_funct =
ctx->texdsp.dxt2_block;
176 break;
177 case MKTAG(
'D',
'X',
'T',
'3'):
178 ctx->dec.tex_ratio = 16;
179 ctx->dec.tex_funct =
ctx->texdsp.dxt3_block;
180 break;
181 case MKTAG(
'D',
'X',
'T',
'4'):
182 ctx->dec.tex_ratio = 16;
183 ctx->dec.tex_funct =
ctx->texdsp.dxt4_block;
184 break;
185 case MKTAG(
'D',
'X',
'T',
'5'):
186 ctx->dec.tex_ratio = 16;
187 if (ycocg_scaled)
188 ctx->dec.tex_funct =
ctx->texdsp.dxt5ys_block;
189 else if (ycocg_classic)
190 ctx->dec.tex_funct =
ctx->texdsp.dxt5y_block;
191 else
192 ctx->dec.tex_funct =
ctx->texdsp.dxt5_block;
193 break;
194 case MKTAG(
'R',
'X',
'G',
'B'):
195 ctx->dec.tex_ratio = 16;
196 ctx->dec.tex_funct =
ctx->texdsp.dxt5_block;
197 /* This format may be considered as a normal map,
198 * but it is handled differently in a separate postproc. */
200 normal_map = 0;
201 break;
202 case MKTAG(
'A',
'T',
'I',
'1'):
203 case MKTAG(
'B',
'C',
'4',
'U'):
204 ctx->dec.tex_ratio = 8;
205 ctx->dec.tex_funct =
ctx->texdsp.rgtc1u_block;
206 break;
207 case MKTAG(
'B',
'C',
'4',
'S'):
208 ctx->dec.tex_ratio = 8;
209 ctx->dec.tex_funct =
ctx->texdsp.rgtc1s_block;
210 break;
211 case MKTAG(
'A',
'T',
'I',
'2'):
212 /* RGT2 variant with swapped R and G (3Dc)*/
213 ctx->dec.tex_ratio = 16;
214 ctx->dec.tex_funct =
ctx->texdsp.dxn3dc_block;
215 break;
216 case MKTAG(
'B',
'C',
'5',
'U'):
217 ctx->dec.tex_ratio = 16;
218 ctx->dec.tex_funct =
ctx->texdsp.rgtc2u_block;
219 break;
220 case MKTAG(
'B',
'C',
'5',
'S'):
221 ctx->dec.tex_ratio = 16;
222 ctx->dec.tex_funct =
ctx->texdsp.rgtc2s_block;
223 break;
224 case MKTAG(
'U',
'Y',
'V',
'Y'):
227 break;
228 case MKTAG(
'Y',
'U',
'Y',
'2'):
231 break;
232 case MKTAG(
'P',
'8',
' ',
' '):
233 /* ATI Palette8, same as normal palette */
237 break;
238 case MKTAG(
'G',
'1',
' ',
' '):
241 break;
242 case MKTAG(
'D',
'X',
'1',
'0'):
243 /* DirectX 10 extra header */
244 dxgi = bytestream2_get_le32(gbc);
247 array = bytestream2_get_le32(gbc);
249
252 "Found array of size %d (ignored).\n",
array);
253
254 /* Only BC[1-5] are actually compressed. */
255 ctx->compressed = (dxgi >= 70) && (dxgi <= 84);
256
258 switch (dxgi) {
259 /* RGB types. */
267 break;
276 break;
282 break;
288 break;
291 break;
292 /* Texture types. */
297 ctx->dec.tex_ratio = 8;
298 ctx->dec.tex_funct =
ctx->texdsp.dxt1a_block;
299 break;
304 ctx->dec.tex_ratio = 16;
305 ctx->dec.tex_funct =
ctx->texdsp.dxt3_block;
306 break;
311 ctx->dec.tex_ratio = 16;
312 ctx->dec.tex_funct =
ctx->texdsp.dxt5_block;
313 break;
316 ctx->dec.tex_ratio = 8;
317 ctx->dec.tex_funct =
ctx->texdsp.rgtc1u_block;
318 break;
320 ctx->dec.tex_ratio = 8;
321 ctx->dec.tex_funct =
ctx->texdsp.rgtc1s_block;
322 break;
325 ctx->dec.tex_ratio = 16;
326 ctx->dec.tex_funct =
ctx->texdsp.rgtc2u_block;
327 break;
329 ctx->dec.tex_ratio = 16;
330 ctx->dec.tex_funct =
ctx->texdsp.rgtc2s_block;
331 break;
332 default:
334 "Unsupported DXGI format %d.\n", dxgi);
336 }
337 break;
338 default:
341 }
342 }
else if (
ctx->paletted) {
343 if (bpp == 8) {
345 } else {
348 }
349 } else {
350 /* 4 bpp */
351 if (bpp == 4 &&
r == 0 &&
g == 0 &&
b == 0 &&
a == 0)
353 /* 8 bpp */
354 else if (bpp == 8 &&
r == 0xff &&
g == 0 &&
b == 0 &&
a == 0)
356 else if (bpp == 8 &&
r == 0 &&
g == 0 &&
b == 0 &&
a == 0xff)
358 /* 16 bpp */
359 else if (bpp == 16 &&
r == 0xff &&
g == 0 &&
b == 0 &&
a == 0xff00)
361 else if (bpp == 16 &&
r == 0xff00 &&
g == 0 &&
b == 0 &&
a == 0xff) {
364 }
365 else if (bpp == 16 &&
r == 0xffff &&
g == 0 &&
b == 0 &&
a == 0)
367 else if (bpp == 16 &&
r == 0x7c00 &&
g == 0x3e0 &&
b == 0x1f &&
a == 0)
369 else if (bpp == 16 &&
r == 0x7c00 &&
g == 0x3e0 &&
b == 0x1f &&
a == 0x8000)
371 else if (bpp == 16 &&
r == 0xf800 &&
g == 0x7e0 &&
b == 0x1f &&
a == 0)
373 /* 24 bpp */
374 else if (bpp == 24 &&
r == 0xff0000 &&
g == 0xff00 &&
b == 0xff &&
a == 0)
376 /* 32 bpp */
377 else if (bpp == 32 &&
r == 0xff0000 &&
g == 0xff00 &&
b == 0xff &&
a == 0)
379 else if (bpp == 32 &&
r == 0xff &&
g == 0xff00 &&
b == 0xff0000 &&
a == 0)
381 else if (bpp == 32 &&
r == 0xff0000 &&
g == 0xff00 &&
b == 0xff &&
a == 0xff000000)
383 else if (bpp == 32 &&
r == 0xff &&
g == 0xff00 &&
b == 0xff0000 &&
a == 0xff000000)
385 /* give up */
386 else {
388 "[bpp %d r 0x%x g 0x%x b 0x%x a 0x%x].\n", bpp,
r,
g,
b,
a);
390 }
391 }
392
393 /* Set any remaining post-proc that should happen before frame is ready. */
394 if (alpha_exponent)
396 else if (normal_map)
398 else if (ycocg_classic && !
ctx->compressed)
400
401 /* ATI/NVidia variants sometimes add swizzling in bpp. */
402 switch (bpp) {
403 case MKTAG(
'A',
'2',
'X',
'Y'):
405 break;
406 case MKTAG(
'x',
'G',
'B',
'R'):
408 break;
409 case MKTAG(
'x',
'R',
'B',
'G'):
411 break;
412 case MKTAG(
'R',
'B',
'x',
'G'):
414 break;
415 case MKTAG(
'R',
'G',
'x',
'B'):
417 break;
418 case MKTAG(
'R',
'x',
'B',
'G'):
420 break;
421 case MKTAG(
'x',
'G',
'x',
'R'):
423 break;
424 case MKTAG(
'A',
'2',
'D',
'5'):
426 break;
427 }
428
429 return 0;
430 }
431
433 {
435 for (
i = 0;
i <
frame->linesize[0] *
frame->height;
i += 4) {
438 }
439 }
440
442 {
445
446 switch (
ctx->postproc) {
448 /* Alpha-exponential mode divides each channel by the maximum
449 * R, G or B value, and stores the multiplying factor in the
450 * alpha channel. */
452
453 for (
i = 0;
i <
frame->linesize[0] *
frame->height;
i += 4) {
459
460 src[0] =
r *
a / 255;
461 src[1] =
g *
a / 255;
462 src[2] =
b *
a / 255;
464 }
465 break;
467 /* Normal maps work in the XYZ color space and they encode
468 * X in R or in A, depending on the texture type, Y in G and
469 * derive Z with a square root of the distance.
470 *
471 * http://www.realtimecollisiondetection.net/blog/?p=28 */
473
474 x_off =
ctx->dec.tex_ratio == 8 ? 0 : 3;
475 for (
i = 0;
i <
frame->linesize[0] *
frame->height;
i += 4) {
479 int z = 127;
480
481 int d = (255 * 255 - x * x - y * y) / 2;
482 if (d > 0)
484
489 }
490 break;
492 /* Data is Y-Co-Cg-A and not RGBA, but they are represented
493 * with the same masks in the DDPF header. */
495
496 for (
i = 0;
i <
frame->linesize[0] *
frame->height;
i += 4) {
499 int cg =
src[1] - 128;
500 int co =
src[2] - 128;
502
507 }
508 break;
510 /* Alpha and Luma are stored swapped. */
512
513 for (
i = 0;
i <
frame->linesize[0] *
frame->height;
i += 2) {
516 }
517 break;
519 /* Swap R and G, often used to restore a standard RGTC2. */
522 break;
524 /* Swap G and A, then B and new A (G). */
528 break;
530 /* Swap B and A. */
533 break;
535 /* Swap G and A. */
538 break;
540 /* Swap R and A (misleading name). */
543 break;
545 /* Swap B and A, then R and new A (B). */
549 break;
551 /* Swap G and A, then R and new A (G), then new R (G) and new G (A).
552 * This variant does not store any B component. */
557 break;
559 /* Swap G and A, then R and new A (G). */
563 break;
564 }
565 }
566
569 {
572 int mipmap;
575
578
583 }
584
585 if (bytestream2_get_le32(gbc) !=
MKTAG(
'D',
'D',
'S',
' ') ||
586 bytestream2_get_le32(gbc) != 124) { // header size
589 }
590
592
593 height = bytestream2_get_le32(gbc);
594 width = bytestream2_get_le32(gbc);
600 }
601
602 /* Since codec is based on 4x4 blocks, size is aligned to 4. */
605
608 mipmap = bytestream2_get_le32(gbc);
609 if (mipmap != 0)
611
612 /* Extract pixel format information, considering additional elements
613 * in reserved1 and reserved2. */
617
621
622 if (
ctx->compressed) {
627
630 "Compressed Buffer is too small (%d < %d).\n",
633 }
634
635 /* Use the decompress function on the texture, one block per thread. */
637 ctx->dec.frame_data.out =
frame->data[0];
638 ctx->dec.stride =
frame->linesize[0];
645
646 /* Use the first 64 bytes as palette, then copy the rest. */
648 for (
i = 0;
i < 16;
i++) {
650 (
frame->data[1][2+
i*4]<<0)+
651 (
frame->data[1][1+
i*4]<<8)+
652 (
frame->data[1][0+
i*4]<<16)+
653 ((
unsigned)
frame->data[1][3+
i*4]<<24)
654 );
655 }
656
661 }
662
663 for (y = 0; y <
frame->height; y++) {
664 for (x = 0; x <
frame->width; x += 2) {
665 uint8_t
val = bytestream2_get_byte(gbc);
668 }
670 }
671 } else {
673
676 /* Use the first 1024 bytes as palette, then copy the rest. */
678 for (
i = 0;
i < 256;
i++)
680 (
frame->data[1][2+
i*4]<<0)+
681 (
frame->data[1][1+
i*4]<<8)+
682 (
frame->data[1][0+
i*4]<<16)+
683 ((
unsigned)
frame->data[1][3+
i*4]<<24)
684 );
685 }
686
691 }
692
695 linesize,
frame->height);
696 }
697
698 /* Run any post processing here if needed. */
701
702 /* Frame is ready to be output. */
703 *got_frame = 1;
704
706 }
707
716 };