2 * DirectDraw Surface image decoder
3 * Copyright (C) 2015 Vittorio Giovara <vittorio.giovara@gmail.com>
5 * This file is part of Libav.
7 * Libav 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.
12 * Libav 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.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 * https://msdn.microsoft.com/en-us/library/bb943982%28v=vs.85%29.aspx
31 #include "libavutil/imgutils.h"
34 #include "bytestream.h"
36 #include "texturedsp.h"
39 #define DDPF_FOURCC (1 << 2)
40 #define DDPF_PALETTE (1 << 5)
41 #define DDPF_NORMALMAP (1 << 31)
60 DXGI_FORMAT_R16G16B16A16_TYPELESS
= 9,
61 DXGI_FORMAT_R16G16B16A16_FLOAT
= 10,
62 DXGI_FORMAT_R16G16B16A16_UNORM
= 11,
63 DXGI_FORMAT_R16G16B16A16_UINT
= 12,
64 DXGI_FORMAT_R16G16B16A16_SNORM
= 13,
65 DXGI_FORMAT_R16G16B16A16_SINT
= 14,
67 DXGI_FORMAT_R8G8B8A8_TYPELESS
= 27,
68 DXGI_FORMAT_R8G8B8A8_UNORM
= 28,
69 DXGI_FORMAT_R8G8B8A8_UNORM_SRGB
= 29,
70 DXGI_FORMAT_R8G8B8A8_UINT
= 30,
71 DXGI_FORMAT_R8G8B8A8_SNORM
= 31,
72 DXGI_FORMAT_R8G8B8A8_SINT
= 32,
74 DXGI_FORMAT_BC1_TYPELESS
= 70,
75 DXGI_FORMAT_BC1_UNORM
= 71,
76 DXGI_FORMAT_BC1_UNORM_SRGB
= 72,
77 DXGI_FORMAT_BC2_TYPELESS
= 73,
78 DXGI_FORMAT_BC2_UNORM
= 74,
79 DXGI_FORMAT_BC2_UNORM_SRGB
= 75,
80 DXGI_FORMAT_BC3_TYPELESS
= 76,
81 DXGI_FORMAT_BC3_UNORM
= 77,
82 DXGI_FORMAT_BC3_UNORM_SRGB
= 78,
83 DXGI_FORMAT_BC4_TYPELESS
= 79,
84 DXGI_FORMAT_BC4_UNORM
= 80,
85 DXGI_FORMAT_BC4_SNORM
= 81,
86 DXGI_FORMAT_BC5_TYPELESS
= 82,
87 DXGI_FORMAT_BC5_UNORM
= 83,
88 DXGI_FORMAT_BC5_SNORM
= 84,
89 DXGI_FORMAT_B5G6R5_UNORM
= 85,
90 DXGI_FORMAT_B8G8R8A8_UNORM
= 87,
91 DXGI_FORMAT_B8G8R8X8_UNORM
= 88,
92 DXGI_FORMAT_B8G8R8A8_TYPELESS
= 90,
93 DXGI_FORMAT_B8G8R8A8_UNORM_SRGB
= 91,
94 DXGI_FORMAT_B8G8R8X8_TYPELESS
= 92,
95 DXGI_FORMAT_B8G8R8X8_UNORM_SRGB
= 93,
98 typedef struct DDSContext
{
99 TextureDSPContext texdsp
;
104 enum DDSPostProc postproc
;
106 const uint8_t *tex_data
; // Compressed texture
107 int tex_ratio
; // Compression ratio
109 /* Pointer to the selected compress or decompress function. */
110 int (*tex_funct
)(uint8_t *dst
, ptrdiff_t stride
, const uint8_t *block
);
113 static int parse_pixel_format(AVCodecContext
*avctx
)
115 DDSContext
*ctx
= avctx
->priv_data
;
116 GetByteContext
*gbc
= &ctx
->gbc
;
118 uint32_t flags
, fourcc
, gimp_tag
;
119 enum DDSDXGIFormat dxgi
;
120 int size
, bpp
, r
, g
, b
, a
;
121 int alpha_exponent
, ycocg_classic
, ycocg_scaled
, normal_map
, array
;
123 /* Alternative DDS implementations use reserved1 as custom header. */
124 bytestream2_skip(gbc
, 4 * 3);
125 gimp_tag
= bytestream2_get_le32(gbc
);
126 alpha_exponent
= gimp_tag
== MKTAG('A', 'E', 'X', 'P');
127 ycocg_classic
= gimp_tag
== MKTAG('Y', 'C', 'G', '1');
128 ycocg_scaled
= gimp_tag
== MKTAG('Y', 'C', 'G', '2');
129 bytestream2_skip(gbc
, 4 * 7);
131 /* Now the real DDPF starts. */
132 size
= bytestream2_get_le32(gbc
);
134 av_log(avctx
, AV_LOG_ERROR
, "Invalid pixel format header %d.\n", size
);
135 return AVERROR_INVALIDDATA
;
137 flags
= bytestream2_get_le32(gbc
);
138 ctx
->compressed
= flags
& DDPF_FOURCC
;
139 ctx
->paletted
= flags
& DDPF_PALETTE
;
140 normal_map
= flags
& DDPF_NORMALMAP
;
141 fourcc
= bytestream2_get_le32(gbc
);
143 bpp
= bytestream2_get_le32(gbc
); // rgbbitcount
144 r
= bytestream2_get_le32(gbc
); // rbitmask
145 g
= bytestream2_get_le32(gbc
); // gbitmask
146 b
= bytestream2_get_le32(gbc
); // bbitmask
147 a
= bytestream2_get_le32(gbc
); // abitmask
149 bytestream2_skip(gbc
, 4); // caps
150 bytestream2_skip(gbc
, 4); // caps2
151 bytestream2_skip(gbc
, 4); // caps3
152 bytestream2_skip(gbc
, 4); // caps4
153 bytestream2_skip(gbc
, 4); // reserved2
155 av_get_codec_tag_string(buf
, sizeof(buf
), fourcc
);
156 av_log(avctx
, AV_LOG_VERBOSE
, "fourcc %s bpp %d "
157 "r 0x%x g 0x%x b 0x%x a 0x%x\n", buf
, bpp
, r
, g
, b
, a
);
159 av_get_codec_tag_string(buf
, sizeof(buf
), gimp_tag
);
160 av_log(avctx
, AV_LOG_VERBOSE
, "and GIMP-DDS tag %s\n", buf
);
164 avctx
->pix_fmt
= AV_PIX_FMT_RGBA
;
166 if (ctx
->compressed
) {
168 case MKTAG('D', 'X', 'T', '1'):
170 ctx
->tex_funct
= ctx
->texdsp
.dxt1a_block
;
172 case MKTAG('D', 'X', 'T', '2'):
174 ctx
->tex_funct
= ctx
->texdsp
.dxt2_block
;
176 case MKTAG('D', 'X', 'T', '3'):
178 ctx
->tex_funct
= ctx
->texdsp
.dxt3_block
;
180 case MKTAG('D', 'X', 'T', '4'):
182 ctx
->tex_funct
= ctx
->texdsp
.dxt4_block
;
184 case MKTAG('D', 'X', 'T', '5'):
187 ctx
->tex_funct
= ctx
->texdsp
.dxt5ys_block
;
188 else if (ycocg_classic
)
189 ctx
->tex_funct
= ctx
->texdsp
.dxt5y_block
;
191 ctx
->tex_funct
= ctx
->texdsp
.dxt5_block
;
193 case MKTAG('R', 'X', 'G', 'B'):
195 ctx
->tex_funct
= ctx
->texdsp
.dxt5_block
;
196 /* This format may be considered as a normal map,
197 * but it is handled differently in a separate postproc. */
198 ctx
->postproc
= DDS_SWIZZLE_RXGB
;
201 case MKTAG('A', 'T', 'I', '1'):
202 case MKTAG('B', 'C', '4', 'U'):
204 ctx
->tex_funct
= ctx
->texdsp
.rgtc1u_block
;
206 case MKTAG('B', 'C', '4', 'S'):
208 ctx
->tex_funct
= ctx
->texdsp
.rgtc1s_block
;
210 case MKTAG('A', 'T', 'I', '2'):
211 /* RGT2 variant with swapped R and G (3Dc)*/
213 ctx
->tex_funct
= ctx
->texdsp
.dxn3dc_block
;
215 case MKTAG('B', 'C', '5', 'U'):
217 ctx
->tex_funct
= ctx
->texdsp
.rgtc2u_block
;
219 case MKTAG('B', 'C', '5', 'S'):
221 ctx
->tex_funct
= ctx
->texdsp
.rgtc2s_block
;
223 case MKTAG('U', 'Y', 'V', 'Y'):
225 avctx
->pix_fmt
= AV_PIX_FMT_UYVY422
;
227 case MKTAG('Y', 'U', 'Y', '2'):
229 avctx
->pix_fmt
= AV_PIX_FMT_YUYV422
;
231 case MKTAG('P', '8', ' ', ' '):
232 /* ATI Palette8, same as normal palette */
235 avctx
->pix_fmt
= AV_PIX_FMT_PAL8
;
237 case MKTAG('D', 'X', '1', '0'):
238 /* DirectX 10 extra header */
239 dxgi
= bytestream2_get_le32(gbc
);
240 bytestream2_skip(gbc
, 4); // resourceDimension
241 bytestream2_skip(gbc
, 4); // miscFlag
242 array
= bytestream2_get_le32(gbc
);
243 bytestream2_skip(gbc
, 4); // miscFlag2
246 av_log(avctx
, AV_LOG_VERBOSE
,
247 "Found array of size %d (ignored).\n", array
);
249 /* Only BC[1-5] are actually compressed. */
250 ctx
->compressed
= (dxgi
>= 70) && (dxgi
<= 84);
252 av_log(avctx
, AV_LOG_VERBOSE
, "DXGI format %d.\n", dxgi
);
255 case DXGI_FORMAT_R16G16B16A16_TYPELESS
:
256 case DXGI_FORMAT_R16G16B16A16_FLOAT
:
257 case DXGI_FORMAT_R16G16B16A16_UNORM
:
258 case DXGI_FORMAT_R16G16B16A16_UINT
:
259 case DXGI_FORMAT_R16G16B16A16_SNORM
:
260 case DXGI_FORMAT_R16G16B16A16_SINT
:
261 avctx
->pix_fmt
= AV_PIX_FMT_BGRA64
;
263 case DXGI_FORMAT_R8G8B8A8_UNORM_SRGB
:
264 avctx
->colorspace
= AVCOL_SPC_RGB
;
265 case DXGI_FORMAT_R8G8B8A8_TYPELESS
:
266 case DXGI_FORMAT_R8G8B8A8_UNORM
:
267 case DXGI_FORMAT_R8G8B8A8_UINT
:
268 case DXGI_FORMAT_R8G8B8A8_SNORM
:
269 case DXGI_FORMAT_R8G8B8A8_SINT
:
270 avctx
->pix_fmt
= AV_PIX_FMT_BGRA
;
272 case DXGI_FORMAT_B8G8R8A8_UNORM_SRGB
:
273 avctx
->colorspace
= AVCOL_SPC_RGB
;
274 case DXGI_FORMAT_B8G8R8A8_TYPELESS
:
275 case DXGI_FORMAT_B8G8R8A8_UNORM
:
276 avctx
->pix_fmt
= AV_PIX_FMT_RGBA
;
278 case DXGI_FORMAT_B8G8R8X8_UNORM_SRGB
:
279 avctx
->colorspace
= AVCOL_SPC_RGB
;
280 case DXGI_FORMAT_B8G8R8X8_TYPELESS
:
281 case DXGI_FORMAT_B8G8R8X8_UNORM
:
282 avctx
->pix_fmt
= AV_PIX_FMT_RGBA
; // opaque
284 case DXGI_FORMAT_B5G6R5_UNORM
:
285 avctx
->pix_fmt
= AV_PIX_FMT_RGB565LE
;
288 case DXGI_FORMAT_BC1_UNORM_SRGB
:
289 avctx
->colorspace
= AVCOL_SPC_RGB
;
290 case DXGI_FORMAT_BC1_TYPELESS
:
291 case DXGI_FORMAT_BC1_UNORM
:
293 ctx
->tex_funct
= ctx
->texdsp
.dxt1a_block
;
295 case DXGI_FORMAT_BC2_UNORM_SRGB
:
296 avctx
->colorspace
= AVCOL_SPC_RGB
;
297 case DXGI_FORMAT_BC2_TYPELESS
:
298 case DXGI_FORMAT_BC2_UNORM
:
300 ctx
->tex_funct
= ctx
->texdsp
.dxt3_block
;
302 case DXGI_FORMAT_BC3_UNORM_SRGB
:
303 avctx
->colorspace
= AVCOL_SPC_RGB
;
304 case DXGI_FORMAT_BC3_TYPELESS
:
305 case DXGI_FORMAT_BC3_UNORM
:
307 ctx
->tex_funct
= ctx
->texdsp
.dxt5_block
;
309 case DXGI_FORMAT_BC4_TYPELESS
:
310 case DXGI_FORMAT_BC4_UNORM
:
312 ctx
->tex_funct
= ctx
->texdsp
.rgtc1u_block
;
314 case DXGI_FORMAT_BC4_SNORM
:
316 ctx
->tex_funct
= ctx
->texdsp
.rgtc1s_block
;
318 case DXGI_FORMAT_BC5_TYPELESS
:
319 case DXGI_FORMAT_BC5_UNORM
:
321 ctx
->tex_funct
= ctx
->texdsp
.rgtc2u_block
;
323 case DXGI_FORMAT_BC5_SNORM
:
325 ctx
->tex_funct
= ctx
->texdsp
.rgtc2s_block
;
328 av_log(avctx
, AV_LOG_ERROR
,
329 "Unsupported DXGI format %d.\n", dxgi
);
330 return AVERROR_INVALIDDATA
;
334 av_log(avctx
, AV_LOG_ERROR
, "Unsupported %s fourcc.\n", buf
);
335 return AVERROR_INVALIDDATA
;
337 } else if (ctx
->paletted
) {
339 avctx
->pix_fmt
= AV_PIX_FMT_PAL8
;
341 av_log(avctx
, AV_LOG_ERROR
, "Unsupported palette bpp %d.\n", bpp
);
342 return AVERROR_INVALIDDATA
;
346 if (bpp
== 8 && r
== 0xff && g
== 0 && b
== 0 && a
== 0)
347 avctx
->pix_fmt
= AV_PIX_FMT_GRAY8
;
349 else if (bpp
== 16 && r
== 0xff && g
== 0 && b
== 0 && a
== 0xff00)
350 avctx
->pix_fmt
= AV_PIX_FMT_YA8
;
351 else if (bpp
== 16 && r
== 0xffff && g
== 0 && b
== 0 && a
== 0)
352 avctx
->pix_fmt
= AV_PIX_FMT_GRAY16LE
;
353 else if (bpp
== 16 && r
== 0xf800 && g
== 0x7e0 && b
== 0x1f && a
== 0)
354 avctx
->pix_fmt
= AV_PIX_FMT_RGB565LE
;
356 else if (bpp
== 24 && r
== 0xff0000 && g
== 0xff00 && b
== 0xff && a
== 0)
357 avctx
->pix_fmt
= AV_PIX_FMT_BGR24
;
359 else if (bpp
== 32 && r
== 0xff0000 && g
== 0xff00 && b
== 0xff && a
== 0)
360 avctx
->pix_fmt
= AV_PIX_FMT_RGBA
; // opaque
361 else if (bpp
== 32 && r
== 0xff && g
== 0xff00 && b
== 0xff0000 && a
== 0)
362 avctx
->pix_fmt
= AV_PIX_FMT_BGRA
; // opaque
363 else if (bpp
== 32 && r
== 0xff0000 && g
== 0xff00 && b
== 0xff && a
== 0xff000000)
364 avctx
->pix_fmt
= AV_PIX_FMT_RGBA
;
365 else if (bpp
== 32 && r
== 0xff && g
== 0xff00 && b
== 0xff0000 && a
== 0xff000000)
366 avctx
->pix_fmt
= AV_PIX_FMT_BGRA
;
369 av_log(avctx
, AV_LOG_ERROR
, "Unknown pixel format "
370 "[bpp %d r 0x%x g 0x%x b 0x%x a 0x%x].\n", bpp
, r
, g
, b
, a
);
371 return AVERROR_INVALIDDATA
;
375 /* Set any remaining post-proc that should happen before frame is ready. */
377 ctx
->postproc
= DDS_ALPHA_EXP
;
379 ctx
->postproc
= DDS_NORMAL_MAP
;
380 else if (ycocg_classic
&& !ctx
->compressed
)
381 ctx
->postproc
= DDS_RAW_YCOCG
;
382 else if (avctx
->pix_fmt
== AV_PIX_FMT_YA8
)
383 ctx
->postproc
= DDS_SWAP_ALPHA
;
385 /* ATI/NVidia variants sometimes add swizzling in bpp. */
387 case MKTAG('A', '2', 'X', 'Y'):
388 ctx
->postproc
= DDS_SWIZZLE_A2XY
;
390 case MKTAG('x', 'G', 'B', 'R'):
391 ctx
->postproc
= DDS_SWIZZLE_XGBR
;
393 case MKTAG('x', 'R', 'B', 'G'):
394 ctx
->postproc
= DDS_SWIZZLE_XRBG
;
396 case MKTAG('R', 'B', 'x', 'G'):
397 ctx
->postproc
= DDS_SWIZZLE_RBXG
;
399 case MKTAG('R', 'G', 'x', 'B'):
400 ctx
->postproc
= DDS_SWIZZLE_RGXB
;
402 case MKTAG('R', 'x', 'B', 'G'):
403 ctx
->postproc
= DDS_SWIZZLE_RXBG
;
405 case MKTAG('x', 'G', 'x', 'R'):
406 ctx
->postproc
= DDS_SWIZZLE_XGXR
;
408 case MKTAG('A', '2', 'D', '5'):
409 ctx
->postproc
= DDS_NORMAL_MAP
;
416 static int decompress_texture_thread(AVCodecContext
*avctx
, void *arg
,
417 int block_nb
, int thread_nb
)
419 DDSContext
*ctx
= avctx
->priv_data
;
420 AVFrame
*frame
= arg
;
421 int x
= (TEXTURE_BLOCK_W
* block_nb
) % avctx
->coded_width
;
422 int y
= TEXTURE_BLOCK_H
* (TEXTURE_BLOCK_W
* block_nb
/ avctx
->coded_width
);
423 uint8_t *p
= frame
->data
[0] + x
* 4 + y
* frame
->linesize
[0];
424 const uint8_t *d
= ctx
->tex_data
+ block_nb
* ctx
->tex_ratio
;
426 ctx
->tex_funct(p
, frame
->linesize
[0], d
);
430 static void do_swizzle(AVFrame
*frame
, int x
, int y
)
433 for (i
= 0; i
< frame
->linesize
[0] * frame
->height
; i
+= 4) {
434 uint8_t *src
= frame
->data
[0] + i
;
435 FFSWAP(uint8_t, src
[x
], src
[y
]);
439 static void run_postproc(AVCodecContext
*avctx
, AVFrame
*frame
)
441 DDSContext
*ctx
= avctx
->priv_data
;
444 switch (ctx
->postproc
) {
446 /* Alpha-exponential mode divides each channel by the maximum
447 * R, G or B value, and stores the multiplying factor in the
449 av_log(avctx
, AV_LOG_DEBUG
, "Post-processing alpha exponent.\n");
451 for (i
= 0; i
< frame
->linesize
[0] * frame
->height
; i
+= 4) {
452 uint8_t *src
= frame
->data
[0] + i
;
458 src
[0] = r
* a
/ 255;
459 src
[1] = g
* a
/ 255;
460 src
[2] = b
* a
/ 255;
465 /* Normal maps work in the XYZ color space and they encode
466 * X in R or in A, depending on the texture type, Y in G and
467 * derive Z with a square root of the distance.
469 * http://www.realtimecollisiondetection.net/blog/?p=28 */
470 av_log(avctx
, AV_LOG_DEBUG
, "Post-processing normal map.\n");
472 x_off
= ctx
->tex_ratio
== 8 ?
0 : 3;
473 for (i
= 0; i
< frame
->linesize
[0] * frame
->height
; i
+= 4) {
474 uint8_t *src
= frame
->data
[0] + i
;
479 int d
= (255 * 255 - x
* x
- y
* y
) / 2;
490 /* Data is Y-Co-Cg-A and not RGBA, but they are represented
491 * with the same masks in the DDPF header. */
492 av_log(avctx
, AV_LOG_DEBUG
, "Post-processing raw YCoCg.\n");
494 for (i
= 0; i
< frame
->linesize
[0] * frame
->height
; i
+= 4) {
495 uint8_t *src
= frame
->data
[0] + i
;
497 int cg
= src
[1] - 128;
498 int co
= src
[2] - 128;
501 src
[0] = av_clip_uint8(y
+ co
- cg
);
502 src
[1] = av_clip_uint8(y
+ cg
);
503 src
[2] = av_clip_uint8(y
- co
- cg
);
508 /* Alpha and Luma are stored swapped. */
509 av_log(avctx
, AV_LOG_DEBUG
, "Post-processing swapped Luma/Alpha.\n");
511 for (i
= 0; i
< frame
->linesize
[0] * frame
->height
; i
+= 2) {
512 uint8_t *src
= frame
->data
[0] + i
;
513 FFSWAP(uint8_t, src
[0], src
[1]);
516 case DDS_SWIZZLE_A2XY
:
517 /* Swap R and G, often used to restore a standard RGTC2. */
518 av_log(avctx
, AV_LOG_DEBUG
, "Post-processing A2XY swizzle.\n");
519 do_swizzle(frame
, 0, 1);
521 case DDS_SWIZZLE_RBXG
:
522 /* Swap G and A, then B and new A (G). */
523 av_log(avctx
, AV_LOG_DEBUG
, "Post-processing RBXG swizzle.\n");
524 do_swizzle(frame
, 1, 3);
525 do_swizzle(frame
, 2, 3);
527 case DDS_SWIZZLE_RGXB
:
529 av_log(avctx
, AV_LOG_DEBUG
, "Post-processing RGXB swizzle.\n");
530 do_swizzle(frame
, 2, 3);
532 case DDS_SWIZZLE_RXBG
:
534 av_log(avctx
, AV_LOG_DEBUG
, "Post-processing RXBG swizzle.\n");
535 do_swizzle(frame
, 1, 3);
537 case DDS_SWIZZLE_RXGB
:
538 /* Swap R and A (misleading name). */
539 av_log(avctx
, AV_LOG_DEBUG
, "Post-processing RXGB swizzle.\n");
540 do_swizzle(frame
, 0, 3);
542 case DDS_SWIZZLE_XGBR
:
543 /* Swap B and A, then R and new A (B). */
544 av_log(avctx
, AV_LOG_DEBUG
, "Post-processing XGBR swizzle.\n");
545 do_swizzle(frame
, 2, 3);
546 do_swizzle(frame
, 0, 3);
548 case DDS_SWIZZLE_XGXR
:
549 /* Swap G and A, then R and new A (G), then new R (G) and new G (A).
550 * This variant does not store any B component. */
551 av_log(avctx
, AV_LOG_DEBUG
, "Post-processing XGXR swizzle.\n");
552 do_swizzle(frame
, 1, 3);
553 do_swizzle(frame
, 0, 3);
554 do_swizzle(frame
, 0, 1);
556 case DDS_SWIZZLE_XRBG
:
557 /* Swap G and A, then R and new A (G). */
558 av_log(avctx
, AV_LOG_DEBUG
, "Post-processing XRBG swizzle.\n");
559 do_swizzle(frame
, 1, 3);
560 do_swizzle(frame
, 0, 3);
565 static int dds_decode(AVCodecContext
*avctx
, void *data
,
566 int *got_frame
, AVPacket
*avpkt
)
568 DDSContext
*ctx
= avctx
->priv_data
;
569 GetByteContext
*gbc
= &ctx
->gbc
;
570 AVFrame
*frame
= data
;
574 ff_texturedsp_init(&ctx
->texdsp
);
575 bytestream2_init(gbc
, avpkt
->data
, avpkt
->size
);
577 if (bytestream2_get_bytes_left(gbc
) < 128) {
578 av_log(avctx
, AV_LOG_ERROR
, "Frame is too small (%d).",
579 bytestream2_get_bytes_left(gbc
));
580 return AVERROR_INVALIDDATA
;
583 if (bytestream2_get_le32(gbc
) != MKTAG('D', 'D', 'S', ' ') ||
584 bytestream2_get_le32(gbc
) != 124) { // header size
585 av_log(avctx
, AV_LOG_ERROR
, "Invalid DDS header.");
586 return AVERROR_INVALIDDATA
;
589 bytestream2_skip(gbc
, 4); // flags
591 avctx
->height
= bytestream2_get_le32(gbc
);
592 avctx
->width
= bytestream2_get_le32(gbc
);
593 ret
= av_image_check_size(avctx
->width
, avctx
->height
, 0, avctx
);
595 av_log(avctx
, AV_LOG_ERROR
, "Invalid image size %dx%d.\n",
596 avctx
->width
, avctx
->height
);
600 /* Since codec is based on 4x4 blocks, size is aligned to 4. */
601 avctx
->coded_width
= FFALIGN(avctx
->width
, TEXTURE_BLOCK_W
);
602 avctx
->coded_height
= FFALIGN(avctx
->height
, TEXTURE_BLOCK_H
);
604 bytestream2_skip(gbc
, 4); // pitch
605 bytestream2_skip(gbc
, 4); // depth
606 mipmap
= bytestream2_get_le32(gbc
);
608 av_log(avctx
, AV_LOG_VERBOSE
, "Found %d mipmaps (ignored).\n", mipmap
);
610 /* Extract pixel format information, considering additional elements
611 * in reserved1 and reserved2. */
612 ret
= parse_pixel_format(avctx
);
616 ret
= ff_get_buffer(avctx
, frame
, 0);
620 if (ctx
->compressed
) {
621 /* Use the decompress function on the texture, one block per thread. */
622 ctx
->tex_data
= gbc
->buffer
;
623 blocks
= avctx
->coded_width
* avctx
->coded_height
/ (TEXTURE_BLOCK_W
* TEXTURE_BLOCK_H
);
624 avctx
->execute2(avctx
, decompress_texture_thread
, frame
, NULL
, blocks
);
626 int linesize
= av_image_get_linesize(avctx
->pix_fmt
, frame
->width
, 0);
630 uint32_t *p
= (uint32_t *)frame
->data
[1];
632 /* Use the first 1024 bytes as palette, then copy the rest. */
633 for (i
= 0; i
< 256; i
++)
634 p
[i
] = bytestream2_get_le32(gbc
);
636 frame
->palette_has_changed
= 1;
639 av_image_copy_plane(frame
->data
[0], frame
->linesize
[0],
640 gbc
->buffer
, linesize
,
641 linesize
, frame
->height
);
644 /* Run any post processing here if needed. */
645 if (avctx
->pix_fmt
== AV_PIX_FMT_RGBA
|| avctx
->pix_fmt
== AV_PIX_FMT_YA8
)
646 run_postproc(avctx
, frame
);
648 /* Frame is ready to be output. */
649 frame
->pict_type
= AV_PICTURE_TYPE_I
;
650 frame
->key_frame
= 1;
656 AVCodec ff_dds_decoder
= {
658 .long_name
= NULL_IF_CONFIG_SMALL("DirectDraw Surface image decoder"),
659 .type
= AVMEDIA_TYPE_VIDEO
,
660 .id
= AV_CODEC_ID_DDS
,
661 .decode
= dds_decode
,
662 .priv_data_size
= sizeof(DDSContext
),
663 .capabilities
= CODEC_CAP_DR1
| CODEC_CAP_SLICE_THREADS
,
664 .caps_internal
= FF_CODEC_CAP_INIT_THREADSAFE