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
111
112 /* Pointer to the selected compress or decompress function. */
115
117 {
123 int alpha_exponent, ycocg_classic, ycocg_scaled, normal_map,
array;
124
125 /* Alternative DDS implementations use reserved1 as custom header. */
127 gimp_tag = bytestream2_get_le32(gbc);
128 alpha_exponent = gimp_tag ==
MKTAG(
'A',
'E',
'X',
'P');
129 ycocg_classic = gimp_tag ==
MKTAG(
'Y',
'C',
'G',
'1');
130 ycocg_scaled = gimp_tag ==
MKTAG(
'Y',
'C',
'G',
'2');
132
133 /* Now the real DDPF starts. */
134 size = bytestream2_get_le32(gbc);
138 }
139 flags = bytestream2_get_le32(gbc);
143 fourcc = bytestream2_get_le32(gbc);
144
145 if (
ctx->compressed &&
ctx->paletted) {
147 "Disabling invalid palette flag for compressed dds.\n");
149 }
150
151 bpp =
ctx->bpp = bytestream2_get_le32(gbc);
// rgbbitcount
152 r = bytestream2_get_le32(gbc);
// rbitmask
153 g = bytestream2_get_le32(gbc);
// gbitmask
154 b = bytestream2_get_le32(gbc);
// bbitmask
155 a = bytestream2_get_le32(gbc);
// abitmask
156
162
165 if (gimp_tag)
167
170
171 if (
ctx->compressed) {
173 case MKTAG(
'D',
'X',
'T',
'1'):
175 ctx->tex_funct =
ctx->texdsp.dxt1a_block;
176 break;
177 case MKTAG(
'D',
'X',
'T',
'2'):
179 ctx->tex_funct =
ctx->texdsp.dxt2_block;
180 break;
181 case MKTAG(
'D',
'X',
'T',
'3'):
183 ctx->tex_funct =
ctx->texdsp.dxt3_block;
184 break;
185 case MKTAG(
'D',
'X',
'T',
'4'):
187 ctx->tex_funct =
ctx->texdsp.dxt4_block;
188 break;
189 case MKTAG(
'D',
'X',
'T',
'5'):
191 if (ycocg_scaled)
192 ctx->tex_funct =
ctx->texdsp.dxt5ys_block;
193 else if (ycocg_classic)
194 ctx->tex_funct =
ctx->texdsp.dxt5y_block;
195 else
196 ctx->tex_funct =
ctx->texdsp.dxt5_block;
197 break;
198 case MKTAG(
'R',
'X',
'G',
'B'):
200 ctx->tex_funct =
ctx->texdsp.dxt5_block;
201 /* This format may be considered as a normal map,
202 * but it is handled differently in a separate postproc. */
204 normal_map = 0;
205 break;
206 case MKTAG(
'A',
'T',
'I',
'1'):
207 case MKTAG(
'B',
'C',
'4',
'U'):
209 ctx->tex_funct =
ctx->texdsp.rgtc1u_block;
210 break;
211 case MKTAG(
'B',
'C',
'4',
'S'):
213 ctx->tex_funct =
ctx->texdsp.rgtc1s_block;
214 break;
215 case MKTAG(
'A',
'T',
'I',
'2'):
216 /* RGT2 variant with swapped R and G (3Dc)*/
218 ctx->tex_funct =
ctx->texdsp.dxn3dc_block;
219 break;
220 case MKTAG(
'B',
'C',
'5',
'U'):
222 ctx->tex_funct =
ctx->texdsp.rgtc2u_block;
223 break;
224 case MKTAG(
'B',
'C',
'5',
'S'):
226 ctx->tex_funct =
ctx->texdsp.rgtc2s_block;
227 break;
228 case MKTAG(
'U',
'Y',
'V',
'Y'):
231 break;
232 case MKTAG(
'Y',
'U',
'Y',
'2'):
235 break;
236 case MKTAG(
'P',
'8',
' ',
' '):
237 /* ATI Palette8, same as normal palette */
241 break;
242 case MKTAG(
'G',
'1',
' ',
' '):
245 break;
246 case MKTAG(
'D',
'X',
'1',
'0'):
247 /* DirectX 10 extra header */
248 dxgi = bytestream2_get_le32(gbc);
251 array = bytestream2_get_le32(gbc);
253
256 "Found array of size %d (ignored).\n",
array);
257
258 /* Only BC[1-5] are actually compressed. */
259 ctx->compressed = (dxgi >= 70) && (dxgi <= 84);
260
262 switch (dxgi) {
263 /* RGB types. */
271 break;
280 break;
286 break;
292 break;
295 break;
296 /* Texture types. */
302 ctx->tex_funct =
ctx->texdsp.dxt1a_block;
303 break;
309 ctx->tex_funct =
ctx->texdsp.dxt3_block;
310 break;
316 ctx->tex_funct =
ctx->texdsp.dxt5_block;
317 break;
321 ctx->tex_funct =
ctx->texdsp.rgtc1u_block;
322 break;
325 ctx->tex_funct =
ctx->texdsp.rgtc1s_block;
326 break;
330 ctx->tex_funct =
ctx->texdsp.rgtc2u_block;
331 break;
334 ctx->tex_funct =
ctx->texdsp.rgtc2s_block;
335 break;
336 default:
338 "Unsupported DXGI format %d.\n", dxgi);
340 }
341 break;
342 default:
345 }
346 }
else if (
ctx->paletted) {
347 if (bpp == 8) {
349 } else {
352 }
353 } else {
354 /* 4 bpp */
355 if (bpp == 4 &&
r == 0 &&
g == 0 &&
b == 0 &&
a == 0)
357 /* 8 bpp */
358 else if (bpp == 8 &&
r == 0xff &&
g == 0 &&
b == 0 &&
a == 0)
360 else if (bpp == 8 &&
r == 0 &&
g == 0 &&
b == 0 &&
a == 0xff)
362 /* 16 bpp */
363 else if (bpp == 16 &&
r == 0xff &&
g == 0 &&
b == 0 &&
a == 0xff00)
365 else if (bpp == 16 &&
r == 0xff00 &&
g == 0 &&
b == 0 &&
a == 0xff) {
368 }
369 else if (bpp == 16 &&
r == 0xffff &&
g == 0 &&
b == 0 &&
a == 0)
371 else if (bpp == 16 &&
r == 0x7c00 &&
g == 0x3e0 &&
b == 0x1f &&
a == 0)
373 else if (bpp == 16 &&
r == 0x7c00 &&
g == 0x3e0 &&
b == 0x1f &&
a == 0x8000)
375 else if (bpp == 16 &&
r == 0xf800 &&
g == 0x7e0 &&
b == 0x1f &&
a == 0)
377 /* 24 bpp */
378 else if (bpp == 24 &&
r == 0xff0000 &&
g == 0xff00 &&
b == 0xff &&
a == 0)
380 /* 32 bpp */
381 else if (bpp == 32 &&
r == 0xff0000 &&
g == 0xff00 &&
b == 0xff &&
a == 0)
383 else if (bpp == 32 &&
r == 0xff &&
g == 0xff00 &&
b == 0xff0000 &&
a == 0)
385 else if (bpp == 32 &&
r == 0xff0000 &&
g == 0xff00 &&
b == 0xff &&
a == 0xff000000)
387 else if (bpp == 32 &&
r == 0xff &&
g == 0xff00 &&
b == 0xff0000 &&
a == 0xff000000)
389 /* give up */
390 else {
392 "[bpp %d r 0x%x g 0x%x b 0x%x a 0x%x].\n", bpp,
r,
g,
b,
a);
394 }
395 }
396
397 /* Set any remaining post-proc that should happen before frame is ready. */
398 if (alpha_exponent)
400 else if (normal_map)
402 else if (ycocg_classic && !
ctx->compressed)
404
405 /* ATI/NVidia variants sometimes add swizzling in bpp. */
406 switch (bpp) {
407 case MKTAG(
'A',
'2',
'X',
'Y'):
409 break;
410 case MKTAG(
'x',
'G',
'B',
'R'):
412 break;
413 case MKTAG(
'x',
'R',
'B',
'G'):
415 break;
416 case MKTAG(
'R',
'B',
'x',
'G'):
418 break;
419 case MKTAG(
'R',
'G',
'x',
'B'):
421 break;
422 case MKTAG(
'R',
'x',
'B',
'G'):
424 break;
425 case MKTAG(
'x',
'G',
'x',
'R'):
427 break;
428 case MKTAG(
'A',
'2',
'D',
'5'):
430 break;
431 }
432
433 return 0;
434 }
435
437 int slice, int thread_nb)
438 {
441 const uint8_t *
d =
ctx->tex_data;
444 int x, y;
445 int start_slice, end_slice;
446 int base_blocks_per_slice = h_block /
ctx->slice_count;
447 int remainder_blocks = h_block %
ctx->slice_count;
448
449 /* When the frame height (in blocks) doesn't divide evenly between the
450 * number of slices, spread the remaining blocks evenly between the first
451 * operations */
452 start_slice = slice * base_blocks_per_slice;
453 /* Add any extra blocks (one per slice) that have been added before this slice */
454 start_slice +=
FFMIN(slice, remainder_blocks);
455
456 end_slice = start_slice + base_blocks_per_slice;
457 /* Add an extra block if there are still remainder blocks to be accounted for */
458 if (slice < remainder_blocks)
459 end_slice++;
460
461 for (y = start_slice; y < end_slice; y++) {
463 int off = y * w_block;
464 for (x = 0; x < w_block; x++) {
465 ctx->tex_funct(p + x * 16,
frame->linesize[0],
466 d + (off + x) *
ctx->tex_ratio);
467 }
468 }
469
470 return 0;
471 }
472
474 {
476 for (
i = 0;
i <
frame->linesize[0] *
frame->height;
i += 4) {
479 }
480 }
481
483 {
486
487 switch (
ctx->postproc) {
489 /* Alpha-exponential mode divides each channel by the maximum
490 * R, G or B value, and stores the multiplying factor in the
491 * alpha channel. */
493
494 for (
i = 0;
i <
frame->linesize[0] *
frame->height;
i += 4) {
500
501 src[0] =
r *
a / 255;
502 src[1] =
g *
a / 255;
503 src[2] =
b *
a / 255;
505 }
506 break;
508 /* Normal maps work in the XYZ color space and they encode
509 * X in R or in A, depending on the texture type, Y in G and
510 * derive Z with a square root of the distance.
511 *
512 * http://www.realtimecollisiondetection.net/blog/?p=28 */
514
515 x_off =
ctx->tex_ratio == 8 ? 0 : 3;
516 for (
i = 0;
i <
frame->linesize[0] *
frame->height;
i += 4) {
520 int z = 127;
521
522 int d = (255 * 255 - x * x - y * y) / 2;
525
530 }
531 break;
533 /* Data is Y-Co-Cg-A and not RGBA, but they are represented
534 * with the same masks in the DDPF header. */
536
537 for (
i = 0;
i <
frame->linesize[0] *
frame->height;
i += 4) {
540 int cg =
src[1] - 128;
541 int co =
src[2] - 128;
543
548 }
549 break;
551 /* Alpha and Luma are stored swapped. */
553
554 for (
i = 0;
i <
frame->linesize[0] *
frame->height;
i += 2) {
557 }
558 break;
560 /* Swap R and G, often used to restore a standard RGTC2. */
563 break;
565 /* Swap G and A, then B and new A (G). */
569 break;
571 /* Swap B and A. */
574 break;
576 /* Swap G and A. */
579 break;
581 /* Swap R and A (misleading name). */
584 break;
586 /* Swap B and A, then R and new A (B). */
590 break;
592 /* Swap G and A, then R and new A (G), then new R (G) and new G (A).
593 * This variant does not store any B component. */
598 break;
600 /* Swap G and A, then R and new A (G). */
604 break;
605 }
606 }
607
610 {
614 int mipmap;
617
620
625 }
626
627 if (bytestream2_get_le32(gbc) !=
MKTAG(
'D',
'D',
'S',
' ') ||
628 bytestream2_get_le32(gbc) != 124) { // header size
631 }
632
634
635 height = bytestream2_get_le32(gbc);
636 width = bytestream2_get_le32(gbc);
642 }
643
644 /* Since codec is based on 4x4 blocks, size is aligned to 4. */
647
650 mipmap = bytestream2_get_le32(gbc);
651 if (mipmap != 0)
653
654 /* Extract pixel format information, considering additional elements
655 * in reserved1 and reserved2. */
659
663
664 if (
ctx->compressed) {
669
672 "Compressed Buffer is too small (%d < %d).\n",
675 }
676
677 /* Use the decompress function on the texture, one block per thread. */
681 uint8_t *dst =
frame->data[0];
683
684 /* Use the first 64 bytes as palette, then copy the rest. */
686 for (
i = 0;
i < 16;
i++) {
688 (
frame->data[1][2+
i*4]<<0)+
689 (
frame->data[1][1+
i*4]<<8)+
690 (
frame->data[1][0+
i*4]<<16)+
691 ((
unsigned)
frame->data[1][3+
i*4]<<24)
692 );
693 }
694 frame->palette_has_changed = 1;
695
700 }
701
702 for (y = 0; y <
frame->height; y++) {
703 for (x = 0; x <
frame->width; x += 2) {
704 uint8_t
val = bytestream2_get_byte(gbc);
706 dst[x + 1] =
val >> 4;
707 }
708 dst +=
frame->linesize[0];
709 }
710 } else {
712
715 /* Use the first 1024 bytes as palette, then copy the rest. */
717 for (
i = 0;
i < 256;
i++)
719 (
frame->data[1][2+
i*4]<<0)+
720 (
frame->data[1][1+
i*4]<<8)+
721 (
frame->data[1][0+
i*4]<<16)+
722 ((
unsigned)
frame->data[1][3+
i*4]<<24)
723 );
724
725 frame->palette_has_changed = 1;
726 }
727
732 }
733
736 linesize,
frame->height);
737 }
738
739 /* Run any post processing here if needed. */
742
743 /* Frame is ready to be output. */
745 frame->key_frame = 1;
746 *got_frame = 1;
747
749 }
750
760 };