blob: bd16b41e49c78e509eb4a61798c3879b13e466f7 [file] [log] [blame]
Vittorio Giovara5c018ee2015-05-26 23:30:511/*
2 * DirectDraw Surface image decoder
3 * Copyright (C) 2015 Vittorio Giovara <[email protected]>
4 *
Michael Niedermayerff68b832015-06-22 21:24:105 * This file is part of FFmpeg.
Vittorio Giovara5c018ee2015-05-26 23:30:516 *
Michael Niedermayerff68b832015-06-22 21:24:107 * FFmpeg is free software; you can redistribute it and/or
Vittorio Giovara5c018ee2015-05-26 23:30:518 * 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 *
Michael Niedermayerff68b832015-06-22 21:24:1012 * FFmpeg is distributed in the hope that it will be useful,
Vittorio Giovara5c018ee2015-05-26 23:30:5113 * 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
Michael Niedermayerff68b832015-06-22 21:24:1018 * License along with FFmpeg; if not, write to the Free Software
Vittorio Giovara5c018ee2015-05-26 23:30:5119 * 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
Ganesh Ajjanagadde5979c742015-12-25 23:54:1631#include "libavutil/libm.h"
Vittorio Giovara5c018ee2015-05-26 23:30:5132#include "libavutil/imgutils.h"
33
34#include "avcodec.h"
35#include "bytestream.h"
Andreas Rheinhardta688f3c2022-03-16 17:18:2836#include "codec_internal.h"
Andreas Rheinhardt66b691f2022-08-24 19:28:1637#include "decode.h"
Vittorio Giovara5c018ee2015-05-26 23:30:5138#include "texturedsp.h"
Vittorio Giovara5c018ee2015-05-26 23:30:5139
40#define DDPF_FOURCC (1 << 2)
41#define DDPF_PALETTE (1 << 5)
Michael Niedermayer8a8335d2017-05-06 20:31:2342#define DDPF_NORMALMAP (1U << 31)
Vittorio Giovara5c018ee2015-05-26 23:30:5143
44enum DDSPostProc {
45 DDS_NONE = 0,
46 DDS_ALPHA_EXP,
47 DDS_NORMAL_MAP,
48 DDS_RAW_YCOCG,
49 DDS_SWAP_ALPHA,
50 DDS_SWIZZLE_A2XY,
51 DDS_SWIZZLE_RBXG,
52 DDS_SWIZZLE_RGXB,
53 DDS_SWIZZLE_RXBG,
54 DDS_SWIZZLE_RXGB,
55 DDS_SWIZZLE_XGBR,
56 DDS_SWIZZLE_XRBG,
57 DDS_SWIZZLE_XGXR,
Vittorio Giovaraea4d46e2015-07-22 12:20:0758};
Vittorio Giovara5c018ee2015-05-26 23:30:5159
60enum DDSDXGIFormat {
61 DXGI_FORMAT_R16G16B16A16_TYPELESS = 9,
62 DXGI_FORMAT_R16G16B16A16_FLOAT = 10,
63 DXGI_FORMAT_R16G16B16A16_UNORM = 11,
64 DXGI_FORMAT_R16G16B16A16_UINT = 12,
65 DXGI_FORMAT_R16G16B16A16_SNORM = 13,
66 DXGI_FORMAT_R16G16B16A16_SINT = 14,
67
68 DXGI_FORMAT_R8G8B8A8_TYPELESS = 27,
69 DXGI_FORMAT_R8G8B8A8_UNORM = 28,
70 DXGI_FORMAT_R8G8B8A8_UNORM_SRGB = 29,
71 DXGI_FORMAT_R8G8B8A8_UINT = 30,
72 DXGI_FORMAT_R8G8B8A8_SNORM = 31,
73 DXGI_FORMAT_R8G8B8A8_SINT = 32,
74
75 DXGI_FORMAT_BC1_TYPELESS = 70,
76 DXGI_FORMAT_BC1_UNORM = 71,
77 DXGI_FORMAT_BC1_UNORM_SRGB = 72,
78 DXGI_FORMAT_BC2_TYPELESS = 73,
79 DXGI_FORMAT_BC2_UNORM = 74,
80 DXGI_FORMAT_BC2_UNORM_SRGB = 75,
81 DXGI_FORMAT_BC3_TYPELESS = 76,
82 DXGI_FORMAT_BC3_UNORM = 77,
83 DXGI_FORMAT_BC3_UNORM_SRGB = 78,
84 DXGI_FORMAT_BC4_TYPELESS = 79,
85 DXGI_FORMAT_BC4_UNORM = 80,
86 DXGI_FORMAT_BC4_SNORM = 81,
87 DXGI_FORMAT_BC5_TYPELESS = 82,
88 DXGI_FORMAT_BC5_UNORM = 83,
89 DXGI_FORMAT_BC5_SNORM = 84,
90 DXGI_FORMAT_B5G6R5_UNORM = 85,
91 DXGI_FORMAT_B8G8R8A8_UNORM = 87,
92 DXGI_FORMAT_B8G8R8X8_UNORM = 88,
93 DXGI_FORMAT_B8G8R8A8_TYPELESS = 90,
94 DXGI_FORMAT_B8G8R8A8_UNORM_SRGB = 91,
95 DXGI_FORMAT_B8G8R8X8_TYPELESS = 92,
96 DXGI_FORMAT_B8G8R8X8_UNORM_SRGB = 93,
Vittorio Giovaraea4d46e2015-07-22 12:20:0797};
Vittorio Giovara5c018ee2015-05-26 23:30:5198
99typedef struct DDSContext {
100 TextureDSPContext texdsp;
101 GetByteContext gbc;
102
103 int compressed;
104 int paletted;
Paul B Mahol257fbc32016-09-21 11:40:04105 int bpp;
Vittorio Giovara5c018ee2015-05-26 23:30:51106 enum DDSPostProc postproc;
107
Marton Balint30fdcd22022-03-19 19:53:17108 TextureDSPThreadContext dec;
Vittorio Giovara5c018ee2015-05-26 23:30:51109} DDSContext;
110
111static int parse_pixel_format(AVCodecContext *avctx)
112{
113 DDSContext *ctx = avctx->priv_data;
114 GetByteContext *gbc = &ctx->gbc;
Vittorio Giovara5c018ee2015-05-26 23:30:51115 uint32_t flags, fourcc, gimp_tag;
116 enum DDSDXGIFormat dxgi;
117 int size, bpp, r, g, b, a;
118 int alpha_exponent, ycocg_classic, ycocg_scaled, normal_map, array;
119
120 /* Alternative DDS implementations use reserved1 as custom header. */
121 bytestream2_skip(gbc, 4 * 3);
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');
126 bytestream2_skip(gbc, 4 * 7);
127
128 /* Now the real DDPF starts. */
129 size = bytestream2_get_le32(gbc);
130 if (size != 32) {
131 av_log(avctx, AV_LOG_ERROR, "Invalid pixel format header %d.\n", size);
132 return AVERROR_INVALIDDATA;
133 }
134 flags = bytestream2_get_le32(gbc);
135 ctx->compressed = flags & DDPF_FOURCC;
136 ctx->paletted = flags & DDPF_PALETTE;
137 normal_map = flags & DDPF_NORMALMAP;
138 fourcc = bytestream2_get_le32(gbc);
139
Andreas Cadhalpun603ebab2015-11-13 20:48:27140 if (ctx->compressed && ctx->paletted) {
141 av_log(avctx, AV_LOG_WARNING,
142 "Disabling invalid palette flag for compressed dds.\n");
143 ctx->paletted = 0;
144 }
145
Paul B Mahol257fbc32016-09-21 11:40:04146 bpp = ctx->bpp = bytestream2_get_le32(gbc); // rgbbitcount
Vittorio Giovara5c018ee2015-05-26 23:30:51147 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
152 bytestream2_skip(gbc, 4); // caps
153 bytestream2_skip(gbc, 4); // caps2
154 bytestream2_skip(gbc, 4); // caps3
155 bytestream2_skip(gbc, 4); // caps4
156 bytestream2_skip(gbc, 4); // reserved2
157
Vittorio Giovara5c018ee2015-05-26 23:30:51158 av_log(avctx, AV_LOG_VERBOSE, "fourcc %s bpp %d "
Clément Bœsch67e370e2017-03-26 23:31:37159 "r 0x%x g 0x%x b 0x%x a 0x%x\n", av_fourcc2str(fourcc), bpp, r, g, b, a);
160 if (gimp_tag)
161 av_log(avctx, AV_LOG_VERBOSE, "and GIMP-DDS tag %s\n", av_fourcc2str(gimp_tag));
Vittorio Giovara5c018ee2015-05-26 23:30:51162
163 if (ctx->compressed)
164 avctx->pix_fmt = AV_PIX_FMT_RGBA;
165
166 if (ctx->compressed) {
Marton Balint30fdcd22022-03-19 19:53:17167 ctx->dec.raw_ratio = 16;
Vittorio Giovara5c018ee2015-05-26 23:30:51168 switch (fourcc) {
169 case MKTAG('D', 'X', 'T', '1'):
Marton Balint30fdcd22022-03-19 19:53:17170 ctx->dec.tex_ratio = 8;
171 ctx->dec.tex_funct = ctx->texdsp.dxt1a_block;
Vittorio Giovara5c018ee2015-05-26 23:30:51172 break;
173 case MKTAG('D', 'X', 'T', '2'):
Marton Balint30fdcd22022-03-19 19:53:17174 ctx->dec.tex_ratio = 16;
175 ctx->dec.tex_funct = ctx->texdsp.dxt2_block;
Vittorio Giovara5c018ee2015-05-26 23:30:51176 break;
177 case MKTAG('D', 'X', 'T', '3'):
Marton Balint30fdcd22022-03-19 19:53:17178 ctx->dec.tex_ratio = 16;
179 ctx->dec.tex_funct = ctx->texdsp.dxt3_block;
Vittorio Giovara5c018ee2015-05-26 23:30:51180 break;
181 case MKTAG('D', 'X', 'T', '4'):
Marton Balint30fdcd22022-03-19 19:53:17182 ctx->dec.tex_ratio = 16;
183 ctx->dec.tex_funct = ctx->texdsp.dxt4_block;
Vittorio Giovara5c018ee2015-05-26 23:30:51184 break;
185 case MKTAG('D', 'X', 'T', '5'):
Marton Balint30fdcd22022-03-19 19:53:17186 ctx->dec.tex_ratio = 16;
Vittorio Giovara5c018ee2015-05-26 23:30:51187 if (ycocg_scaled)
Marton Balint30fdcd22022-03-19 19:53:17188 ctx->dec.tex_funct = ctx->texdsp.dxt5ys_block;
Vittorio Giovara5c018ee2015-05-26 23:30:51189 else if (ycocg_classic)
Marton Balint30fdcd22022-03-19 19:53:17190 ctx->dec.tex_funct = ctx->texdsp.dxt5y_block;
Vittorio Giovara5c018ee2015-05-26 23:30:51191 else
Marton Balint30fdcd22022-03-19 19:53:17192 ctx->dec.tex_funct = ctx->texdsp.dxt5_block;
Vittorio Giovara5c018ee2015-05-26 23:30:51193 break;
194 case MKTAG('R', 'X', 'G', 'B'):
Marton Balint30fdcd22022-03-19 19:53:17195 ctx->dec.tex_ratio = 16;
196 ctx->dec.tex_funct = ctx->texdsp.dxt5_block;
Vittorio Giovara5c018ee2015-05-26 23:30:51197 /* This format may be considered as a normal map,
198 * but it is handled differently in a separate postproc. */
199 ctx->postproc = DDS_SWIZZLE_RXGB;
200 normal_map = 0;
201 break;
202 case MKTAG('A', 'T', 'I', '1'):
203 case MKTAG('B', 'C', '4', 'U'):
Marton Balint30fdcd22022-03-19 19:53:17204 ctx->dec.tex_ratio = 8;
205 ctx->dec.tex_funct = ctx->texdsp.rgtc1u_block;
Vittorio Giovara5c018ee2015-05-26 23:30:51206 break;
207 case MKTAG('B', 'C', '4', 'S'):
Marton Balint30fdcd22022-03-19 19:53:17208 ctx->dec.tex_ratio = 8;
209 ctx->dec.tex_funct = ctx->texdsp.rgtc1s_block;
Vittorio Giovara5c018ee2015-05-26 23:30:51210 break;
211 case MKTAG('A', 'T', 'I', '2'):
212 /* RGT2 variant with swapped R and G (3Dc)*/
Marton Balint30fdcd22022-03-19 19:53:17213 ctx->dec.tex_ratio = 16;
214 ctx->dec.tex_funct = ctx->texdsp.dxn3dc_block;
Vittorio Giovara5c018ee2015-05-26 23:30:51215 break;
216 case MKTAG('B', 'C', '5', 'U'):
Marton Balint30fdcd22022-03-19 19:53:17217 ctx->dec.tex_ratio = 16;
218 ctx->dec.tex_funct = ctx->texdsp.rgtc2u_block;
Vittorio Giovara5c018ee2015-05-26 23:30:51219 break;
220 case MKTAG('B', 'C', '5', 'S'):
Marton Balint30fdcd22022-03-19 19:53:17221 ctx->dec.tex_ratio = 16;
222 ctx->dec.tex_funct = ctx->texdsp.rgtc2s_block;
Vittorio Giovara5c018ee2015-05-26 23:30:51223 break;
224 case MKTAG('U', 'Y', 'V', 'Y'):
225 ctx->compressed = 0;
226 avctx->pix_fmt = AV_PIX_FMT_UYVY422;
227 break;
228 case MKTAG('Y', 'U', 'Y', '2'):
229 ctx->compressed = 0;
230 avctx->pix_fmt = AV_PIX_FMT_YUYV422;
231 break;
232 case MKTAG('P', '8', ' ', ' '):
233 /* ATI Palette8, same as normal palette */
234 ctx->compressed = 0;
235 ctx->paletted = 1;
236 avctx->pix_fmt = AV_PIX_FMT_PAL8;
237 break;
Vittorio Giovara4b2e6932016-04-22 21:28:04238 case MKTAG('G', '1', ' ', ' '):
239 ctx->compressed = 0;
240 avctx->pix_fmt = AV_PIX_FMT_MONOBLACK;
241 break;
Vittorio Giovara5c018ee2015-05-26 23:30:51242 case MKTAG('D', 'X', '1', '0'):
243 /* DirectX 10 extra header */
244 dxgi = bytestream2_get_le32(gbc);
245 bytestream2_skip(gbc, 4); // resourceDimension
246 bytestream2_skip(gbc, 4); // miscFlag
247 array = bytestream2_get_le32(gbc);
248 bytestream2_skip(gbc, 4); // miscFlag2
249
250 if (array != 0)
251 av_log(avctx, AV_LOG_VERBOSE,
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
257 av_log(avctx, AV_LOG_VERBOSE, "DXGI format %d.\n", dxgi);
258 switch (dxgi) {
259 /* RGB types. */
260 case DXGI_FORMAT_R16G16B16A16_TYPELESS:
261 case DXGI_FORMAT_R16G16B16A16_FLOAT:
262 case DXGI_FORMAT_R16G16B16A16_UNORM:
263 case DXGI_FORMAT_R16G16B16A16_UINT:
264 case DXGI_FORMAT_R16G16B16A16_SNORM:
265 case DXGI_FORMAT_R16G16B16A16_SINT:
266 avctx->pix_fmt = AV_PIX_FMT_BGRA64;
267 break;
268 case DXGI_FORMAT_R8G8B8A8_UNORM_SRGB:
269 avctx->colorspace = AVCOL_SPC_RGB;
270 case DXGI_FORMAT_R8G8B8A8_TYPELESS:
271 case DXGI_FORMAT_R8G8B8A8_UNORM:
272 case DXGI_FORMAT_R8G8B8A8_UINT:
273 case DXGI_FORMAT_R8G8B8A8_SNORM:
274 case DXGI_FORMAT_R8G8B8A8_SINT:
275 avctx->pix_fmt = AV_PIX_FMT_BGRA;
276 break;
277 case DXGI_FORMAT_B8G8R8A8_UNORM_SRGB:
278 avctx->colorspace = AVCOL_SPC_RGB;
279 case DXGI_FORMAT_B8G8R8A8_TYPELESS:
280 case DXGI_FORMAT_B8G8R8A8_UNORM:
281 avctx->pix_fmt = AV_PIX_FMT_RGBA;
282 break;
283 case DXGI_FORMAT_B8G8R8X8_UNORM_SRGB:
284 avctx->colorspace = AVCOL_SPC_RGB;
285 case DXGI_FORMAT_B8G8R8X8_TYPELESS:
286 case DXGI_FORMAT_B8G8R8X8_UNORM:
287 avctx->pix_fmt = AV_PIX_FMT_RGBA; // opaque
288 break;
289 case DXGI_FORMAT_B5G6R5_UNORM:
290 avctx->pix_fmt = AV_PIX_FMT_RGB565LE;
291 break;
292 /* Texture types. */
293 case DXGI_FORMAT_BC1_UNORM_SRGB:
294 avctx->colorspace = AVCOL_SPC_RGB;
295 case DXGI_FORMAT_BC1_TYPELESS:
296 case DXGI_FORMAT_BC1_UNORM:
Marton Balint30fdcd22022-03-19 19:53:17297 ctx->dec.tex_ratio = 8;
298 ctx->dec.tex_funct = ctx->texdsp.dxt1a_block;
Vittorio Giovara5c018ee2015-05-26 23:30:51299 break;
300 case DXGI_FORMAT_BC2_UNORM_SRGB:
301 avctx->colorspace = AVCOL_SPC_RGB;
302 case DXGI_FORMAT_BC2_TYPELESS:
303 case DXGI_FORMAT_BC2_UNORM:
Marton Balint30fdcd22022-03-19 19:53:17304 ctx->dec.tex_ratio = 16;
305 ctx->dec.tex_funct = ctx->texdsp.dxt3_block;
Vittorio Giovara5c018ee2015-05-26 23:30:51306 break;
307 case DXGI_FORMAT_BC3_UNORM_SRGB:
308 avctx->colorspace = AVCOL_SPC_RGB;
309 case DXGI_FORMAT_BC3_TYPELESS:
310 case DXGI_FORMAT_BC3_UNORM:
Marton Balint30fdcd22022-03-19 19:53:17311 ctx->dec.tex_ratio = 16;
312 ctx->dec.tex_funct = ctx->texdsp.dxt5_block;
Vittorio Giovara5c018ee2015-05-26 23:30:51313 break;
314 case DXGI_FORMAT_BC4_TYPELESS:
315 case DXGI_FORMAT_BC4_UNORM:
Marton Balint30fdcd22022-03-19 19:53:17316 ctx->dec.tex_ratio = 8;
317 ctx->dec.tex_funct = ctx->texdsp.rgtc1u_block;
Vittorio Giovara5c018ee2015-05-26 23:30:51318 break;
319 case DXGI_FORMAT_BC4_SNORM:
Marton Balint30fdcd22022-03-19 19:53:17320 ctx->dec.tex_ratio = 8;
321 ctx->dec.tex_funct = ctx->texdsp.rgtc1s_block;
Vittorio Giovara5c018ee2015-05-26 23:30:51322 break;
323 case DXGI_FORMAT_BC5_TYPELESS:
324 case DXGI_FORMAT_BC5_UNORM:
Marton Balint30fdcd22022-03-19 19:53:17325 ctx->dec.tex_ratio = 16;
326 ctx->dec.tex_funct = ctx->texdsp.rgtc2u_block;
Vittorio Giovara5c018ee2015-05-26 23:30:51327 break;
328 case DXGI_FORMAT_BC5_SNORM:
Marton Balint30fdcd22022-03-19 19:53:17329 ctx->dec.tex_ratio = 16;
330 ctx->dec.tex_funct = ctx->texdsp.rgtc2s_block;
Vittorio Giovara5c018ee2015-05-26 23:30:51331 break;
332 default:
333 av_log(avctx, AV_LOG_ERROR,
334 "Unsupported DXGI format %d.\n", dxgi);
335 return AVERROR_INVALIDDATA;
336 }
337 break;
338 default:
Clément Bœsch67e370e2017-03-26 23:31:37339 av_log(avctx, AV_LOG_ERROR, "Unsupported %s fourcc.\n", av_fourcc2str(fourcc));
Vittorio Giovara5c018ee2015-05-26 23:30:51340 return AVERROR_INVALIDDATA;
341 }
342 } else if (ctx->paletted) {
343 if (bpp == 8) {
344 avctx->pix_fmt = AV_PIX_FMT_PAL8;
345 } else {
346 av_log(avctx, AV_LOG_ERROR, "Unsupported palette bpp %d.\n", bpp);
347 return AVERROR_INVALIDDATA;
348 }
349 } else {
Paul B Mahol257fbc32016-09-21 11:40:04350 /* 4 bpp */
351 if (bpp == 4 && r == 0 && g == 0 && b == 0 && a == 0)
352 avctx->pix_fmt = AV_PIX_FMT_PAL8;
Vittorio Giovara5c018ee2015-05-26 23:30:51353 /* 8 bpp */
Paul B Mahol257fbc32016-09-21 11:40:04354 else if (bpp == 8 && r == 0xff && g == 0 && b == 0 && a == 0)
Vittorio Giovara5c018ee2015-05-26 23:30:51355 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
Vittorio Giovara02538632016-04-01 13:11:50356 else if (bpp == 8 && r == 0 && g == 0 && b == 0 && a == 0xff)
357 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
Vittorio Giovara5c018ee2015-05-26 23:30:51358 /* 16 bpp */
359 else if (bpp == 16 && r == 0xff && g == 0 && b == 0 && a == 0xff00)
360 avctx->pix_fmt = AV_PIX_FMT_YA8;
Carl Eugen Hoyos139cbeb2016-04-15 19:06:34361 else if (bpp == 16 && r == 0xff00 && g == 0 && b == 0 && a == 0xff) {
362 avctx->pix_fmt = AV_PIX_FMT_YA8;
363 ctx->postproc = DDS_SWAP_ALPHA;
364 }
Vittorio Giovara5c018ee2015-05-26 23:30:51365 else if (bpp == 16 && r == 0xffff && g == 0 && b == 0 && a == 0)
366 avctx->pix_fmt = AV_PIX_FMT_GRAY16LE;
Vittorio Giovara9a9fb712016-03-29 19:00:43367 else if (bpp == 16 && r == 0x7c00 && g == 0x3e0 && b == 0x1f && a == 0)
368 avctx->pix_fmt = AV_PIX_FMT_RGB555LE;
369 else if (bpp == 16 && r == 0x7c00 && g == 0x3e0 && b == 0x1f && a == 0x8000)
370 avctx->pix_fmt = AV_PIX_FMT_RGB555LE; // alpha ignored
Vittorio Giovara5c018ee2015-05-26 23:30:51371 else if (bpp == 16 && r == 0xf800 && g == 0x7e0 && b == 0x1f && a == 0)
372 avctx->pix_fmt = AV_PIX_FMT_RGB565LE;
373 /* 24 bpp */
374 else if (bpp == 24 && r == 0xff0000 && g == 0xff00 && b == 0xff && a == 0)
375 avctx->pix_fmt = AV_PIX_FMT_BGR24;
376 /* 32 bpp */
377 else if (bpp == 32 && r == 0xff0000 && g == 0xff00 && b == 0xff && a == 0)
Michael Niedermayer5c583b12015-07-19 22:03:11378 avctx->pix_fmt = AV_PIX_FMT_BGR0; // opaque
Vittorio Giovara5c018ee2015-05-26 23:30:51379 else if (bpp == 32 && r == 0xff && g == 0xff00 && b == 0xff0000 && a == 0)
Michael Niedermayer5c583b12015-07-19 22:03:11380 avctx->pix_fmt = AV_PIX_FMT_RGB0; // opaque
Vittorio Giovara5c018ee2015-05-26 23:30:51381 else if (bpp == 32 && r == 0xff0000 && g == 0xff00 && b == 0xff && a == 0xff000000)
Vittorio Giovara5c018ee2015-05-26 23:30:51382 avctx->pix_fmt = AV_PIX_FMT_BGRA;
Michael Niedermayer5c583b12015-07-19 22:03:11383 else if (bpp == 32 && r == 0xff && g == 0xff00 && b == 0xff0000 && a == 0xff000000)
384 avctx->pix_fmt = AV_PIX_FMT_RGBA;
Vittorio Giovara5c018ee2015-05-26 23:30:51385 /* give up */
386 else {
387 av_log(avctx, AV_LOG_ERROR, "Unknown pixel format "
388 "[bpp %d r 0x%x g 0x%x b 0x%x a 0x%x].\n", bpp, r, g, b, a);
389 return AVERROR_INVALIDDATA;
390 }
391 }
392
393 /* Set any remaining post-proc that should happen before frame is ready. */
394 if (alpha_exponent)
395 ctx->postproc = DDS_ALPHA_EXP;
396 else if (normal_map)
397 ctx->postproc = DDS_NORMAL_MAP;
398 else if (ycocg_classic && !ctx->compressed)
399 ctx->postproc = DDS_RAW_YCOCG;
Vittorio Giovara5c018ee2015-05-26 23:30:51400
401 /* ATI/NVidia variants sometimes add swizzling in bpp. */
402 switch (bpp) {
403 case MKTAG('A', '2', 'X', 'Y'):
404 ctx->postproc = DDS_SWIZZLE_A2XY;
405 break;
406 case MKTAG('x', 'G', 'B', 'R'):
407 ctx->postproc = DDS_SWIZZLE_XGBR;
408 break;
409 case MKTAG('x', 'R', 'B', 'G'):
410 ctx->postproc = DDS_SWIZZLE_XRBG;
411 break;
412 case MKTAG('R', 'B', 'x', 'G'):
413 ctx->postproc = DDS_SWIZZLE_RBXG;
414 break;
415 case MKTAG('R', 'G', 'x', 'B'):
416 ctx->postproc = DDS_SWIZZLE_RGXB;
417 break;
418 case MKTAG('R', 'x', 'B', 'G'):
419 ctx->postproc = DDS_SWIZZLE_RXBG;
420 break;
421 case MKTAG('x', 'G', 'x', 'R'):
422 ctx->postproc = DDS_SWIZZLE_XGXR;
423 break;
424 case MKTAG('A', '2', 'D', '5'):
425 ctx->postproc = DDS_NORMAL_MAP;
426 break;
427 }
428
429 return 0;
430}
431
Vittorio Giovara5c018ee2015-05-26 23:30:51432static void do_swizzle(AVFrame *frame, int x, int y)
433{
434 int i;
435 for (i = 0; i < frame->linesize[0] * frame->height; i += 4) {
436 uint8_t *src = frame->data[0] + i;
437 FFSWAP(uint8_t, src[x], src[y]);
438 }
439}
440
441static void run_postproc(AVCodecContext *avctx, AVFrame *frame)
442{
443 DDSContext *ctx = avctx->priv_data;
444 int i, x_off;
445
446 switch (ctx->postproc) {
447 case DDS_ALPHA_EXP:
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. */
451 av_log(avctx, AV_LOG_DEBUG, "Post-processing alpha exponent.\n");
452
453 for (i = 0; i < frame->linesize[0] * frame->height; i += 4) {
454 uint8_t *src = frame->data[0] + i;
455 int r = src[0];
456 int g = src[1];
457 int b = src[2];
458 int a = src[3];
459
460 src[0] = r * a / 255;
461 src[1] = g * a / 255;
462 src[2] = b * a / 255;
463 src[3] = 255;
464 }
465 break;
466 case DDS_NORMAL_MAP:
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 */
472 av_log(avctx, AV_LOG_DEBUG, "Post-processing normal map.\n");
473
Marton Balint30fdcd22022-03-19 19:53:17474 x_off = ctx->dec.tex_ratio == 8 ? 0 : 3;
Vittorio Giovara5c018ee2015-05-26 23:30:51475 for (i = 0; i < frame->linesize[0] * frame->height; i += 4) {
476 uint8_t *src = frame->data[0] + i;
477 int x = src[x_off];
478 int y = src[1];
479 int z = 127;
480
481 int d = (255 * 255 - x * x - y * y) / 2;
482 if (d > 0)
Ganesh Ajjanagadde5979c742015-12-25 23:54:16483 z = lrint(sqrtf(d));
Vittorio Giovara5c018ee2015-05-26 23:30:51484
485 src[0] = x;
486 src[1] = y;
487 src[2] = z;
488 src[3] = 255;
489 }
490 break;
491 case DDS_RAW_YCOCG:
492 /* Data is Y-Co-Cg-A and not RGBA, but they are represented
493 * with the same masks in the DDPF header. */
494 av_log(avctx, AV_LOG_DEBUG, "Post-processing raw YCoCg.\n");
495
496 for (i = 0; i < frame->linesize[0] * frame->height; i += 4) {
497 uint8_t *src = frame->data[0] + i;
498 int a = src[0];
499 int cg = src[1] - 128;
500 int co = src[2] - 128;
501 int y = src[3];
502
503 src[0] = av_clip_uint8(y + co - cg);
504 src[1] = av_clip_uint8(y + cg);
505 src[2] = av_clip_uint8(y - co - cg);
506 src[3] = a;
507 }
508 break;
509 case DDS_SWAP_ALPHA:
510 /* Alpha and Luma are stored swapped. */
511 av_log(avctx, AV_LOG_DEBUG, "Post-processing swapped Luma/Alpha.\n");
512
513 for (i = 0; i < frame->linesize[0] * frame->height; i += 2) {
514 uint8_t *src = frame->data[0] + i;
515 FFSWAP(uint8_t, src[0], src[1]);
516 }
517 break;
518 case DDS_SWIZZLE_A2XY:
519 /* Swap R and G, often used to restore a standard RGTC2. */
520 av_log(avctx, AV_LOG_DEBUG, "Post-processing A2XY swizzle.\n");
521 do_swizzle(frame, 0, 1);
522 break;
523 case DDS_SWIZZLE_RBXG:
524 /* Swap G and A, then B and new A (G). */
525 av_log(avctx, AV_LOG_DEBUG, "Post-processing RBXG swizzle.\n");
526 do_swizzle(frame, 1, 3);
527 do_swizzle(frame, 2, 3);
528 break;
529 case DDS_SWIZZLE_RGXB:
530 /* Swap B and A. */
531 av_log(avctx, AV_LOG_DEBUG, "Post-processing RGXB swizzle.\n");
532 do_swizzle(frame, 2, 3);
533 break;
534 case DDS_SWIZZLE_RXBG:
535 /* Swap G and A. */
536 av_log(avctx, AV_LOG_DEBUG, "Post-processing RXBG swizzle.\n");
537 do_swizzle(frame, 1, 3);
538 break;
539 case DDS_SWIZZLE_RXGB:
540 /* Swap R and A (misleading name). */
541 av_log(avctx, AV_LOG_DEBUG, "Post-processing RXGB swizzle.\n");
542 do_swizzle(frame, 0, 3);
543 break;
544 case DDS_SWIZZLE_XGBR:
545 /* Swap B and A, then R and new A (B). */
546 av_log(avctx, AV_LOG_DEBUG, "Post-processing XGBR swizzle.\n");
547 do_swizzle(frame, 2, 3);
548 do_swizzle(frame, 0, 3);
549 break;
550 case DDS_SWIZZLE_XGXR:
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. */
553 av_log(avctx, AV_LOG_DEBUG, "Post-processing XGXR swizzle.\n");
554 do_swizzle(frame, 1, 3);
555 do_swizzle(frame, 0, 3);
556 do_swizzle(frame, 0, 1);
557 break;
558 case DDS_SWIZZLE_XRBG:
559 /* Swap G and A, then R and new A (G). */
560 av_log(avctx, AV_LOG_DEBUG, "Post-processing XRBG swizzle.\n");
561 do_swizzle(frame, 1, 3);
562 do_swizzle(frame, 0, 3);
563 break;
564 }
565}
566
Andreas Rheinhardtce7dbd02022-03-30 19:33:24567static int dds_decode(AVCodecContext *avctx, AVFrame *frame,
Vittorio Giovara5c018ee2015-05-26 23:30:51568 int *got_frame, AVPacket *avpkt)
569{
570 DDSContext *ctx = avctx->priv_data;
571 GetByteContext *gbc = &ctx->gbc;
Luca Barbato6b2b26e2015-07-17 01:07:08572 int mipmap;
Vittorio Giovara5c018ee2015-05-26 23:30:51573 int ret;
Michael Niedermayer9cd1e932019-08-10 21:09:49574 int width, height;
Vittorio Giovara5c018ee2015-05-26 23:30:51575
576 ff_texturedsp_init(&ctx->texdsp);
577 bytestream2_init(gbc, avpkt->data, avpkt->size);
578
579 if (bytestream2_get_bytes_left(gbc) < 128) {
Andreas Cadhalpunedd0c1d2015-11-10 23:37:32580 av_log(avctx, AV_LOG_ERROR, "Frame is too small (%d).\n",
Vittorio Giovara5c018ee2015-05-26 23:30:51581 bytestream2_get_bytes_left(gbc));
582 return AVERROR_INVALIDDATA;
583 }
584
585 if (bytestream2_get_le32(gbc) != MKTAG('D', 'D', 'S', ' ') ||
586 bytestream2_get_le32(gbc) != 124) { // header size
Andreas Cadhalpunedd0c1d2015-11-10 23:37:32587 av_log(avctx, AV_LOG_ERROR, "Invalid DDS header.\n");
Vittorio Giovara5c018ee2015-05-26 23:30:51588 return AVERROR_INVALIDDATA;
589 }
590
591 bytestream2_skip(gbc, 4); // flags
592
Michael Niedermayer9cd1e932019-08-10 21:09:49593 height = bytestream2_get_le32(gbc);
594 width = bytestream2_get_le32(gbc);
595 ret = ff_set_dimensions(avctx, width, height);
Vittorio Giovara5c018ee2015-05-26 23:30:51596 if (ret < 0) {
597 av_log(avctx, AV_LOG_ERROR, "Invalid image size %dx%d.\n",
598 avctx->width, avctx->height);
599 return ret;
600 }
601
602 /* Since codec is based on 4x4 blocks, size is aligned to 4. */
603 avctx->coded_width = FFALIGN(avctx->width, TEXTURE_BLOCK_W);
604 avctx->coded_height = FFALIGN(avctx->height, TEXTURE_BLOCK_H);
605
606 bytestream2_skip(gbc, 4); // pitch
607 bytestream2_skip(gbc, 4); // depth
608 mipmap = bytestream2_get_le32(gbc);
609 if (mipmap != 0)
610 av_log(avctx, AV_LOG_VERBOSE, "Found %d mipmaps (ignored).\n", mipmap);
611
612 /* Extract pixel format information, considering additional elements
613 * in reserved1 and reserved2. */
614 ret = parse_pixel_format(avctx);
615 if (ret < 0)
616 return ret;
617
618 ret = ff_get_buffer(avctx, frame, 0);
619 if (ret < 0)
620 return ret;
621
622 if (ctx->compressed) {
Andreas Cadhalpun9a37d472015-11-10 23:20:18623 int size = (avctx->coded_height / TEXTURE_BLOCK_H) *
Marton Balint30fdcd22022-03-19 19:53:17624 (avctx->coded_width / TEXTURE_BLOCK_W) * ctx->dec.tex_ratio;
625 ctx->dec.slice_count = av_clip(avctx->thread_count, 1,
626 avctx->coded_height / TEXTURE_BLOCK_H);
Luca Barbato6b2b26e2015-07-17 01:07:08627
Andreas Cadhalpun9a37d472015-11-10 23:20:18628 if (bytestream2_get_bytes_left(gbc) < size) {
629 av_log(avctx, AV_LOG_ERROR,
630 "Compressed Buffer is too small (%d < %d).\n",
631 bytestream2_get_bytes_left(gbc), size);
632 return AVERROR_INVALIDDATA;
633 }
634
Vittorio Giovara5c018ee2015-05-26 23:30:51635 /* Use the decompress function on the texture, one block per thread. */
Marton Balint30fdcd22022-03-19 19:53:17636 ctx->dec.tex_data.in = gbc->buffer;
637 ctx->dec.frame_data.out = frame->data[0];
638 ctx->dec.stride = frame->linesize[0];
Connor Worley1487f612024-02-15 05:44:38639 ctx->dec.width = avctx->coded_width;
640 ctx->dec.height = avctx->coded_height;
Andreas Rheinhardt341d0412024-01-24 19:36:55641 ff_texturedsp_exec_decompress_threads(avctx, &ctx->dec);
Andreas Cadhalpun90ebf3c2016-11-15 21:11:05642 } else if (!ctx->paletted && ctx->bpp == 4 && avctx->pix_fmt == AV_PIX_FMT_PAL8) {
Paul B Mahol257fbc32016-09-21 11:40:04643 uint8_t *dst = frame->data[0];
644 int x, y, i;
645
646 /* Use the first 64 bytes as palette, then copy the rest. */
647 bytestream2_get_buffer(gbc, frame->data[1], 16 * 4);
648 for (i = 0; i < 16; i++) {
649 AV_WN32(frame->data[1] + i*4,
650 (frame->data[1][2+i*4]<<0)+
651 (frame->data[1][1+i*4]<<8)+
652 (frame->data[1][0+i*4]<<16)+
Michael Niedermayerc49fa2a2017-05-29 11:51:08653 ((unsigned)frame->data[1][3+i*4]<<24)
Paul B Mahol257fbc32016-09-21 11:40:04654 );
655 }
Paul B Mahol257fbc32016-09-21 11:40:04656
657 if (bytestream2_get_bytes_left(gbc) < frame->height * frame->width / 2) {
658 av_log(avctx, AV_LOG_ERROR, "Buffer is too small (%d < %d).\n",
659 bytestream2_get_bytes_left(gbc), frame->height * frame->width / 2);
660 return AVERROR_INVALIDDATA;
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);
666 dst[x ] = val & 0xF;
667 dst[x + 1] = val >> 4;
668 }
669 dst += frame->linesize[0];
670 }
Vittorio Giovara5c018ee2015-05-26 23:30:51671 } else {
672 int linesize = av_image_get_linesize(avctx->pix_fmt, frame->width, 0);
673
674 if (ctx->paletted) {
Michael Niedermayer21d2e3d2015-06-22 21:59:28675 int i;
Vittorio Giovara5c018ee2015-05-26 23:30:51676 /* Use the first 1024 bytes as palette, then copy the rest. */
677 bytestream2_get_buffer(gbc, frame->data[1], 256 * 4);
Michael Niedermayer36a87c22015-07-19 20:55:16678 for (i = 0; i < 256; i++)
679 AV_WN32(frame->data[1] + i*4,
680 (frame->data[1][2+i*4]<<0)+
681 (frame->data[1][1+i*4]<<8)+
682 (frame->data[1][0+i*4]<<16)+
Michael Niedermayerafb46322017-05-12 23:35:56683 ((unsigned)frame->data[1][3+i*4]<<24)
Michael Niedermayer36a87c22015-07-19 20:55:16684 );
Vittorio Giovara5c018ee2015-05-26 23:30:51685 }
686
Andreas Cadhalpun16758092015-11-10 23:05:02687 if (bytestream2_get_bytes_left(gbc) < frame->height * linesize) {
688 av_log(avctx, AV_LOG_ERROR, "Buffer is too small (%d < %d).\n",
689 bytestream2_get_bytes_left(gbc), frame->height * linesize);
690 return AVERROR_INVALIDDATA;
691 }
692
Vittorio Giovara5c018ee2015-05-26 23:30:51693 av_image_copy_plane(frame->data[0], frame->linesize[0],
694 gbc->buffer, linesize,
695 linesize, frame->height);
696 }
697
698 /* Run any post processing here if needed. */
Vittorio Giovara22e49e62016-03-29 19:00:45699 if (ctx->postproc != DDS_NONE)
Vittorio Giovara5c018ee2015-05-26 23:30:51700 run_postproc(avctx, frame);
701
702 /* Frame is ready to be output. */
Vittorio Giovara5c018ee2015-05-26 23:30:51703 *got_frame = 1;
704
705 return avpkt->size;
706}
707
Andreas Rheinhardt20f97272022-03-16 20:09:54708const FFCodec ff_dds_decoder = {
709 .p.name = "dds",
Andreas Rheinhardt48286d42022-08-29 11:38:02710 CODEC_LONG_NAME("DirectDraw Surface image decoder"),
Andreas Rheinhardt20f97272022-03-16 20:09:54711 .p.type = AVMEDIA_TYPE_VIDEO,
712 .p.id = AV_CODEC_ID_DDS,
Andreas Rheinhardt4243da42022-03-30 21:28:24713 FF_CODEC_DECODE_CB(dds_decode),
Vittorio Giovara5c018ee2015-05-26 23:30:51714 .priv_data_size = sizeof(DDSContext),
Andreas Rheinhardt20f97272022-03-16 20:09:54715 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_SLICE_THREADS,
Vittorio Giovara5c018ee2015-05-26 23:30:51716};