blob: caa629a486cab3ccfc6022c6a4b94335c4d31cc0 [file] [log] [blame]
Vittorio Giovara5c018ee2015-05-26 23:30:511/*
2 * DirectDraw Surface image decoder
3 * Copyright (C) 2015 Vittorio Giovara <[email protected]>
4 *
5 * This file is part of Libav.
6 *
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.
11 *
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.
16 *
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
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
31#include "libavutil/imgutils.h"
32
33#include "avcodec.h"
34#include "bytestream.h"
35#include "internal.h"
36#include "texturedsp.h"
37#include "thread.h"
38
39#define DDPF_FOURCC (1 << 2)
40#define DDPF_PALETTE (1 << 5)
41#define DDPF_NORMALMAP (1 << 31)
42
43enum DDSPostProc {
44 DDS_NONE = 0,
45 DDS_ALPHA_EXP,
46 DDS_NORMAL_MAP,
47 DDS_RAW_YCOCG,
48 DDS_SWAP_ALPHA,
49 DDS_SWIZZLE_A2XY,
50 DDS_SWIZZLE_RBXG,
51 DDS_SWIZZLE_RGXB,
52 DDS_SWIZZLE_RXBG,
53 DDS_SWIZZLE_RXGB,
54 DDS_SWIZZLE_XGBR,
55 DDS_SWIZZLE_XRBG,
56 DDS_SWIZZLE_XGXR,
Vittorio Giovaraea4d46e2015-07-22 12:20:0757};
Vittorio Giovara5c018ee2015-05-26 23:30:5158
59enum DDSDXGIFormat {
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,
66
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,
73
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,
Vittorio Giovaraea4d46e2015-07-22 12:20:0796};
Vittorio Giovara5c018ee2015-05-26 23:30:5197
98typedef struct DDSContext {
99 TextureDSPContext texdsp;
100 GetByteContext gbc;
101
102 int compressed;
103 int paletted;
104 enum DDSPostProc postproc;
105
106 const uint8_t *tex_data; // Compressed texture
107 int tex_ratio; // Compression ratio
Tom Butterworthebe8b5d2015-07-22 23:03:29108 int slice_count; // Number of slices for threaded operations
Vittorio Giovara5c018ee2015-05-26 23:30:51109
110 /* Pointer to the selected compress or decompress function. */
111 int (*tex_funct)(uint8_t *dst, ptrdiff_t stride, const uint8_t *block);
112} DDSContext;
113
114static int parse_pixel_format(AVCodecContext *avctx)
115{
116 DDSContext *ctx = avctx->priv_data;
117 GetByteContext *gbc = &ctx->gbc;
118 char buf[32];
119 uint32_t flags, fourcc, gimp_tag;
120 enum DDSDXGIFormat dxgi;
121 int size, bpp, r, g, b, a;
122 int alpha_exponent, ycocg_classic, ycocg_scaled, normal_map, array;
123
124 /* Alternative DDS implementations use reserved1 as custom header. */
125 bytestream2_skip(gbc, 4 * 3);
126 gimp_tag = bytestream2_get_le32(gbc);
127 alpha_exponent = gimp_tag == MKTAG('A', 'E', 'X', 'P');
128 ycocg_classic = gimp_tag == MKTAG('Y', 'C', 'G', '1');
129 ycocg_scaled = gimp_tag == MKTAG('Y', 'C', 'G', '2');
130 bytestream2_skip(gbc, 4 * 7);
131
132 /* Now the real DDPF starts. */
133 size = bytestream2_get_le32(gbc);
134 if (size != 32) {
135 av_log(avctx, AV_LOG_ERROR, "Invalid pixel format header %d.\n", size);
136 return AVERROR_INVALIDDATA;
137 }
138 flags = bytestream2_get_le32(gbc);
139 ctx->compressed = flags & DDPF_FOURCC;
140 ctx->paletted = flags & DDPF_PALETTE;
141 normal_map = flags & DDPF_NORMALMAP;
142 fourcc = bytestream2_get_le32(gbc);
143
Andreas Cadhalpun0a8bff72015-11-13 20:48:27144 if (ctx->compressed && ctx->paletted) {
145 av_log(avctx, AV_LOG_WARNING,
146 "Disabling invalid palette flag for compressed dds.\n");
147 ctx->paletted = 0;
148 }
149
Vittorio Giovara5c018ee2015-05-26 23:30:51150 bpp = bytestream2_get_le32(gbc); // rgbbitcount
151 r = bytestream2_get_le32(gbc); // rbitmask
152 g = bytestream2_get_le32(gbc); // gbitmask
153 b = bytestream2_get_le32(gbc); // bbitmask
154 a = bytestream2_get_le32(gbc); // abitmask
155
156 bytestream2_skip(gbc, 4); // caps
157 bytestream2_skip(gbc, 4); // caps2
158 bytestream2_skip(gbc, 4); // caps3
159 bytestream2_skip(gbc, 4); // caps4
160 bytestream2_skip(gbc, 4); // reserved2
161
162 av_get_codec_tag_string(buf, sizeof(buf), fourcc);
163 av_log(avctx, AV_LOG_VERBOSE, "fourcc %s bpp %d "
164 "r 0x%x g 0x%x b 0x%x a 0x%x\n", buf, bpp, r, g, b, a);
165 if (gimp_tag) {
166 av_get_codec_tag_string(buf, sizeof(buf), gimp_tag);
167 av_log(avctx, AV_LOG_VERBOSE, "and GIMP-DDS tag %s\n", buf);
168 }
169
170 if (ctx->compressed)
171 avctx->pix_fmt = AV_PIX_FMT_RGBA;
172
173 if (ctx->compressed) {
174 switch (fourcc) {
175 case MKTAG('D', 'X', 'T', '1'):
176 ctx->tex_ratio = 8;
177 ctx->tex_funct = ctx->texdsp.dxt1a_block;
178 break;
179 case MKTAG('D', 'X', 'T', '2'):
180 ctx->tex_ratio = 16;
181 ctx->tex_funct = ctx->texdsp.dxt2_block;
182 break;
183 case MKTAG('D', 'X', 'T', '3'):
184 ctx->tex_ratio = 16;
185 ctx->tex_funct = ctx->texdsp.dxt3_block;
186 break;
187 case MKTAG('D', 'X', 'T', '4'):
188 ctx->tex_ratio = 16;
189 ctx->tex_funct = ctx->texdsp.dxt4_block;
190 break;
191 case MKTAG('D', 'X', 'T', '5'):
192 ctx->tex_ratio = 16;
193 if (ycocg_scaled)
194 ctx->tex_funct = ctx->texdsp.dxt5ys_block;
195 else if (ycocg_classic)
196 ctx->tex_funct = ctx->texdsp.dxt5y_block;
197 else
198 ctx->tex_funct = ctx->texdsp.dxt5_block;
199 break;
200 case MKTAG('R', 'X', 'G', 'B'):
201 ctx->tex_ratio = 16;
202 ctx->tex_funct = ctx->texdsp.dxt5_block;
203 /* This format may be considered as a normal map,
204 * but it is handled differently in a separate postproc. */
205 ctx->postproc = DDS_SWIZZLE_RXGB;
206 normal_map = 0;
207 break;
208 case MKTAG('A', 'T', 'I', '1'):
209 case MKTAG('B', 'C', '4', 'U'):
210 ctx->tex_ratio = 8;
211 ctx->tex_funct = ctx->texdsp.rgtc1u_block;
212 break;
213 case MKTAG('B', 'C', '4', 'S'):
214 ctx->tex_ratio = 8;
215 ctx->tex_funct = ctx->texdsp.rgtc1s_block;
216 break;
217 case MKTAG('A', 'T', 'I', '2'):
218 /* RGT2 variant with swapped R and G (3Dc)*/
219 ctx->tex_ratio = 16;
220 ctx->tex_funct = ctx->texdsp.dxn3dc_block;
221 break;
222 case MKTAG('B', 'C', '5', 'U'):
223 ctx->tex_ratio = 16;
224 ctx->tex_funct = ctx->texdsp.rgtc2u_block;
225 break;
226 case MKTAG('B', 'C', '5', 'S'):
227 ctx->tex_ratio = 16;
228 ctx->tex_funct = ctx->texdsp.rgtc2s_block;
229 break;
230 case MKTAG('U', 'Y', 'V', 'Y'):
231 ctx->compressed = 0;
232 avctx->pix_fmt = AV_PIX_FMT_UYVY422;
233 break;
234 case MKTAG('Y', 'U', 'Y', '2'):
235 ctx->compressed = 0;
236 avctx->pix_fmt = AV_PIX_FMT_YUYV422;
237 break;
238 case MKTAG('P', '8', ' ', ' '):
239 /* ATI Palette8, same as normal palette */
240 ctx->compressed = 0;
241 ctx->paletted = 1;
242 avctx->pix_fmt = AV_PIX_FMT_PAL8;
243 break;
244 case MKTAG('D', 'X', '1', '0'):
245 /* DirectX 10 extra header */
246 dxgi = bytestream2_get_le32(gbc);
247 bytestream2_skip(gbc, 4); // resourceDimension
248 bytestream2_skip(gbc, 4); // miscFlag
249 array = bytestream2_get_le32(gbc);
250 bytestream2_skip(gbc, 4); // miscFlag2
251
252 if (array != 0)
253 av_log(avctx, AV_LOG_VERBOSE,
254 "Found array of size %d (ignored).\n", array);
255
256 /* Only BC[1-5] are actually compressed. */
257 ctx->compressed = (dxgi >= 70) && (dxgi <= 84);
258
259 av_log(avctx, AV_LOG_VERBOSE, "DXGI format %d.\n", dxgi);
260 switch (dxgi) {
261 /* RGB types. */
262 case DXGI_FORMAT_R16G16B16A16_TYPELESS:
263 case DXGI_FORMAT_R16G16B16A16_FLOAT:
264 case DXGI_FORMAT_R16G16B16A16_UNORM:
265 case DXGI_FORMAT_R16G16B16A16_UINT:
266 case DXGI_FORMAT_R16G16B16A16_SNORM:
267 case DXGI_FORMAT_R16G16B16A16_SINT:
268 avctx->pix_fmt = AV_PIX_FMT_BGRA64;
269 break;
270 case DXGI_FORMAT_R8G8B8A8_UNORM_SRGB:
271 avctx->colorspace = AVCOL_SPC_RGB;
272 case DXGI_FORMAT_R8G8B8A8_TYPELESS:
273 case DXGI_FORMAT_R8G8B8A8_UNORM:
274 case DXGI_FORMAT_R8G8B8A8_UINT:
275 case DXGI_FORMAT_R8G8B8A8_SNORM:
276 case DXGI_FORMAT_R8G8B8A8_SINT:
277 avctx->pix_fmt = AV_PIX_FMT_BGRA;
278 break;
279 case DXGI_FORMAT_B8G8R8A8_UNORM_SRGB:
280 avctx->colorspace = AVCOL_SPC_RGB;
281 case DXGI_FORMAT_B8G8R8A8_TYPELESS:
282 case DXGI_FORMAT_B8G8R8A8_UNORM:
283 avctx->pix_fmt = AV_PIX_FMT_RGBA;
284 break;
285 case DXGI_FORMAT_B8G8R8X8_UNORM_SRGB:
286 avctx->colorspace = AVCOL_SPC_RGB;
287 case DXGI_FORMAT_B8G8R8X8_TYPELESS:
288 case DXGI_FORMAT_B8G8R8X8_UNORM:
289 avctx->pix_fmt = AV_PIX_FMT_RGBA; // opaque
290 break;
291 case DXGI_FORMAT_B5G6R5_UNORM:
292 avctx->pix_fmt = AV_PIX_FMT_RGB565LE;
293 break;
294 /* Texture types. */
295 case DXGI_FORMAT_BC1_UNORM_SRGB:
296 avctx->colorspace = AVCOL_SPC_RGB;
297 case DXGI_FORMAT_BC1_TYPELESS:
298 case DXGI_FORMAT_BC1_UNORM:
299 ctx->tex_ratio = 8;
300 ctx->tex_funct = ctx->texdsp.dxt1a_block;
301 break;
302 case DXGI_FORMAT_BC2_UNORM_SRGB:
303 avctx->colorspace = AVCOL_SPC_RGB;
304 case DXGI_FORMAT_BC2_TYPELESS:
305 case DXGI_FORMAT_BC2_UNORM:
306 ctx->tex_ratio = 16;
307 ctx->tex_funct = ctx->texdsp.dxt3_block;
308 break;
309 case DXGI_FORMAT_BC3_UNORM_SRGB:
310 avctx->colorspace = AVCOL_SPC_RGB;
311 case DXGI_FORMAT_BC3_TYPELESS:
312 case DXGI_FORMAT_BC3_UNORM:
313 ctx->tex_ratio = 16;
314 ctx->tex_funct = ctx->texdsp.dxt5_block;
315 break;
316 case DXGI_FORMAT_BC4_TYPELESS:
317 case DXGI_FORMAT_BC4_UNORM:
318 ctx->tex_ratio = 8;
319 ctx->tex_funct = ctx->texdsp.rgtc1u_block;
320 break;
321 case DXGI_FORMAT_BC4_SNORM:
322 ctx->tex_ratio = 8;
323 ctx->tex_funct = ctx->texdsp.rgtc1s_block;
324 break;
325 case DXGI_FORMAT_BC5_TYPELESS:
326 case DXGI_FORMAT_BC5_UNORM:
327 ctx->tex_ratio = 16;
328 ctx->tex_funct = ctx->texdsp.rgtc2u_block;
329 break;
330 case DXGI_FORMAT_BC5_SNORM:
331 ctx->tex_ratio = 16;
332 ctx->tex_funct = ctx->texdsp.rgtc2s_block;
333 break;
334 default:
335 av_log(avctx, AV_LOG_ERROR,
336 "Unsupported DXGI format %d.\n", dxgi);
337 return AVERROR_INVALIDDATA;
338 }
339 break;
340 default:
341 av_log(avctx, AV_LOG_ERROR, "Unsupported %s fourcc.\n", buf);
342 return AVERROR_INVALIDDATA;
343 }
344 } else if (ctx->paletted) {
345 if (bpp == 8) {
346 avctx->pix_fmt = AV_PIX_FMT_PAL8;
347 } else {
348 av_log(avctx, AV_LOG_ERROR, "Unsupported palette bpp %d.\n", bpp);
349 return AVERROR_INVALIDDATA;
350 }
351 } else {
352 /* 8 bpp */
353 if (bpp == 8 && r == 0xff && g == 0 && b == 0 && a == 0)
354 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
Vittorio Giovara02538632016-04-01 13:11:50355 else if (bpp == 8 && r == 0 && g == 0 && b == 0 && a == 0xff)
356 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
Vittorio Giovara5c018ee2015-05-26 23:30:51357 /* 16 bpp */
358 else if (bpp == 16 && r == 0xff && g == 0 && b == 0 && a == 0xff00)
359 avctx->pix_fmt = AV_PIX_FMT_YA8;
360 else if (bpp == 16 && r == 0xffff && g == 0 && b == 0 && a == 0)
361 avctx->pix_fmt = AV_PIX_FMT_GRAY16LE;
Vittorio Giovara9a9fb712016-03-29 19:00:43362 else if (bpp == 16 && r == 0x7c00 && g == 0x3e0 && b == 0x1f && a == 0)
363 avctx->pix_fmt = AV_PIX_FMT_RGB555LE;
364 else if (bpp == 16 && r == 0x7c00 && g == 0x3e0 && b == 0x1f && a == 0x8000)
365 avctx->pix_fmt = AV_PIX_FMT_RGB555LE; // alpha ignored
Vittorio Giovara5c018ee2015-05-26 23:30:51366 else if (bpp == 16 && r == 0xf800 && g == 0x7e0 && b == 0x1f && a == 0)
367 avctx->pix_fmt = AV_PIX_FMT_RGB565LE;
368 /* 24 bpp */
369 else if (bpp == 24 && r == 0xff0000 && g == 0xff00 && b == 0xff && a == 0)
370 avctx->pix_fmt = AV_PIX_FMT_BGR24;
371 /* 32 bpp */
372 else if (bpp == 32 && r == 0xff0000 && g == 0xff00 && b == 0xff && a == 0)
Vittorio Giovara5c018ee2015-05-26 23:30:51373 avctx->pix_fmt = AV_PIX_FMT_BGRA; // opaque
Michael Niedermayerd08d8b62015-07-20 22:47:13374 else if (bpp == 32 && r == 0xff && g == 0xff00 && b == 0xff0000 && a == 0)
375 avctx->pix_fmt = AV_PIX_FMT_RGBA; // opaque
Vittorio Giovara5c018ee2015-05-26 23:30:51376 else if (bpp == 32 && r == 0xff0000 && g == 0xff00 && b == 0xff && a == 0xff000000)
Vittorio Giovara5c018ee2015-05-26 23:30:51377 avctx->pix_fmt = AV_PIX_FMT_BGRA;
Michael Niedermayerd08d8b62015-07-20 22:47:13378 else if (bpp == 32 && r == 0xff && g == 0xff00 && b == 0xff0000 && a == 0xff000000)
379 avctx->pix_fmt = AV_PIX_FMT_RGBA;
Vittorio Giovara5c018ee2015-05-26 23:30:51380 /* give up */
381 else {
382 av_log(avctx, AV_LOG_ERROR, "Unknown pixel format "
383 "[bpp %d r 0x%x g 0x%x b 0x%x a 0x%x].\n", bpp, r, g, b, a);
384 return AVERROR_INVALIDDATA;
385 }
386 }
387
388 /* Set any remaining post-proc that should happen before frame is ready. */
389 if (alpha_exponent)
390 ctx->postproc = DDS_ALPHA_EXP;
391 else if (normal_map)
392 ctx->postproc = DDS_NORMAL_MAP;
393 else if (ycocg_classic && !ctx->compressed)
394 ctx->postproc = DDS_RAW_YCOCG;
395 else if (avctx->pix_fmt == AV_PIX_FMT_YA8)
396 ctx->postproc = DDS_SWAP_ALPHA;
397
398 /* ATI/NVidia variants sometimes add swizzling in bpp. */
399 switch (bpp) {
400 case MKTAG('A', '2', 'X', 'Y'):
401 ctx->postproc = DDS_SWIZZLE_A2XY;
402 break;
403 case MKTAG('x', 'G', 'B', 'R'):
404 ctx->postproc = DDS_SWIZZLE_XGBR;
405 break;
406 case MKTAG('x', 'R', 'B', 'G'):
407 ctx->postproc = DDS_SWIZZLE_XRBG;
408 break;
409 case MKTAG('R', 'B', 'x', 'G'):
410 ctx->postproc = DDS_SWIZZLE_RBXG;
411 break;
412 case MKTAG('R', 'G', 'x', 'B'):
413 ctx->postproc = DDS_SWIZZLE_RGXB;
414 break;
415 case MKTAG('R', 'x', 'B', 'G'):
416 ctx->postproc = DDS_SWIZZLE_RXBG;
417 break;
418 case MKTAG('x', 'G', 'x', 'R'):
419 ctx->postproc = DDS_SWIZZLE_XGXR;
420 break;
421 case MKTAG('A', '2', 'D', '5'):
422 ctx->postproc = DDS_NORMAL_MAP;
423 break;
424 }
425
426 return 0;
427}
428
429static int decompress_texture_thread(AVCodecContext *avctx, void *arg,
Luca Barbato6b2b26e2015-07-17 01:07:08430 int slice, int thread_nb)
Vittorio Giovara5c018ee2015-05-26 23:30:51431{
432 DDSContext *ctx = avctx->priv_data;
433 AVFrame *frame = arg;
Luca Barbato6b2b26e2015-07-17 01:07:08434 const uint8_t *d = ctx->tex_data;
435 int w_block = avctx->coded_width / TEXTURE_BLOCK_W;
Tom Butterworthebe8b5d2015-07-22 23:03:29436 int h_block = avctx->coded_height / TEXTURE_BLOCK_H;
Luca Barbato6b2b26e2015-07-17 01:07:08437 int x, y;
438 int start_slice, end_slice;
Tom Butterworthebe8b5d2015-07-22 23:03:29439 int base_blocks_per_slice = h_block / ctx->slice_count;
440 int remainder_blocks = h_block % ctx->slice_count;
Vittorio Giovara5c018ee2015-05-26 23:30:51441
Tom Butterworthebe8b5d2015-07-22 23:03:29442 /* When the frame height (in blocks) doesn't divide evenly between the
443 * number of slices, spread the remaining blocks evenly between the first
444 * operations */
445 start_slice = slice * base_blocks_per_slice;
446 /* Add any extra blocks (one per slice) that have been added before this slice */
447 start_slice += FFMIN(slice, remainder_blocks);
Luca Barbato6b2b26e2015-07-17 01:07:08448
Tom Butterworthebe8b5d2015-07-22 23:03:29449 end_slice = start_slice + base_blocks_per_slice;
450 /* Add an extra block if there are still remainder blocks to be accounted for */
451 if (slice < remainder_blocks)
452 end_slice++;
Luca Barbato6b2b26e2015-07-17 01:07:08453
454 for (y = start_slice; y < end_slice; y++) {
455 uint8_t *p = frame->data[0] + y * frame->linesize[0] * TEXTURE_BLOCK_H;
456 int off = y * w_block;
457 for (x = 0; x < w_block; x++) {
458 ctx->tex_funct(p + x * 16, frame->linesize[0],
459 d + (off + x) * ctx->tex_ratio);
460 }
461 }
462
Vittorio Giovara5c018ee2015-05-26 23:30:51463 return 0;
464}
465
466static void do_swizzle(AVFrame *frame, int x, int y)
467{
468 int i;
469 for (i = 0; i < frame->linesize[0] * frame->height; i += 4) {
470 uint8_t *src = frame->data[0] + i;
471 FFSWAP(uint8_t, src[x], src[y]);
472 }
473}
474
475static void run_postproc(AVCodecContext *avctx, AVFrame *frame)
476{
477 DDSContext *ctx = avctx->priv_data;
478 int i, x_off;
479
480 switch (ctx->postproc) {
481 case DDS_ALPHA_EXP:
482 /* Alpha-exponential mode divides each channel by the maximum
483 * R, G or B value, and stores the multiplying factor in the
484 * alpha channel. */
485 av_log(avctx, AV_LOG_DEBUG, "Post-processing alpha exponent.\n");
486
487 for (i = 0; i < frame->linesize[0] * frame->height; i += 4) {
488 uint8_t *src = frame->data[0] + i;
489 int r = src[0];
490 int g = src[1];
491 int b = src[2];
492 int a = src[3];
493
494 src[0] = r * a / 255;
495 src[1] = g * a / 255;
496 src[2] = b * a / 255;
497 src[3] = 255;
498 }
499 break;
500 case DDS_NORMAL_MAP:
501 /* Normal maps work in the XYZ color space and they encode
502 * X in R or in A, depending on the texture type, Y in G and
503 * derive Z with a square root of the distance.
504 *
505 * http://www.realtimecollisiondetection.net/blog/?p=28 */
506 av_log(avctx, AV_LOG_DEBUG, "Post-processing normal map.\n");
507
508 x_off = ctx->tex_ratio == 8 ? 0 : 3;
509 for (i = 0; i < frame->linesize[0] * frame->height; i += 4) {
510 uint8_t *src = frame->data[0] + i;
511 int x = src[x_off];
512 int y = src[1];
513 int z = 127;
514
515 int d = (255 * 255 - x * x - y * y) / 2;
516 if (d > 0)
517 z = rint(sqrtf(d));
518
519 src[0] = x;
520 src[1] = y;
521 src[2] = z;
522 src[3] = 255;
523 }
524 break;
525 case DDS_RAW_YCOCG:
526 /* Data is Y-Co-Cg-A and not RGBA, but they are represented
527 * with the same masks in the DDPF header. */
528 av_log(avctx, AV_LOG_DEBUG, "Post-processing raw YCoCg.\n");
529
530 for (i = 0; i < frame->linesize[0] * frame->height; i += 4) {
531 uint8_t *src = frame->data[0] + i;
532 int a = src[0];
533 int cg = src[1] - 128;
534 int co = src[2] - 128;
535 int y = src[3];
536
537 src[0] = av_clip_uint8(y + co - cg);
538 src[1] = av_clip_uint8(y + cg);
539 src[2] = av_clip_uint8(y - co - cg);
540 src[3] = a;
541 }
542 break;
543 case DDS_SWAP_ALPHA:
544 /* Alpha and Luma are stored swapped. */
545 av_log(avctx, AV_LOG_DEBUG, "Post-processing swapped Luma/Alpha.\n");
546
547 for (i = 0; i < frame->linesize[0] * frame->height; i += 2) {
548 uint8_t *src = frame->data[0] + i;
549 FFSWAP(uint8_t, src[0], src[1]);
550 }
551 break;
552 case DDS_SWIZZLE_A2XY:
553 /* Swap R and G, often used to restore a standard RGTC2. */
554 av_log(avctx, AV_LOG_DEBUG, "Post-processing A2XY swizzle.\n");
555 do_swizzle(frame, 0, 1);
556 break;
557 case DDS_SWIZZLE_RBXG:
558 /* Swap G and A, then B and new A (G). */
559 av_log(avctx, AV_LOG_DEBUG, "Post-processing RBXG swizzle.\n");
560 do_swizzle(frame, 1, 3);
561 do_swizzle(frame, 2, 3);
562 break;
563 case DDS_SWIZZLE_RGXB:
564 /* Swap B and A. */
565 av_log(avctx, AV_LOG_DEBUG, "Post-processing RGXB swizzle.\n");
566 do_swizzle(frame, 2, 3);
567 break;
568 case DDS_SWIZZLE_RXBG:
569 /* Swap G and A. */
570 av_log(avctx, AV_LOG_DEBUG, "Post-processing RXBG swizzle.\n");
571 do_swizzle(frame, 1, 3);
572 break;
573 case DDS_SWIZZLE_RXGB:
574 /* Swap R and A (misleading name). */
575 av_log(avctx, AV_LOG_DEBUG, "Post-processing RXGB swizzle.\n");
576 do_swizzle(frame, 0, 3);
577 break;
578 case DDS_SWIZZLE_XGBR:
579 /* Swap B and A, then R and new A (B). */
580 av_log(avctx, AV_LOG_DEBUG, "Post-processing XGBR swizzle.\n");
581 do_swizzle(frame, 2, 3);
582 do_swizzle(frame, 0, 3);
583 break;
584 case DDS_SWIZZLE_XGXR:
585 /* Swap G and A, then R and new A (G), then new R (G) and new G (A).
586 * This variant does not store any B component. */
587 av_log(avctx, AV_LOG_DEBUG, "Post-processing XGXR swizzle.\n");
588 do_swizzle(frame, 1, 3);
589 do_swizzle(frame, 0, 3);
590 do_swizzle(frame, 0, 1);
591 break;
592 case DDS_SWIZZLE_XRBG:
593 /* Swap G and A, then R and new A (G). */
594 av_log(avctx, AV_LOG_DEBUG, "Post-processing XRBG swizzle.\n");
595 do_swizzle(frame, 1, 3);
596 do_swizzle(frame, 0, 3);
597 break;
598 }
599}
600
601static int dds_decode(AVCodecContext *avctx, void *data,
602 int *got_frame, AVPacket *avpkt)
603{
604 DDSContext *ctx = avctx->priv_data;
605 GetByteContext *gbc = &ctx->gbc;
606 AVFrame *frame = data;
Luca Barbato6b2b26e2015-07-17 01:07:08607 int mipmap;
Vittorio Giovara5c018ee2015-05-26 23:30:51608 int ret;
609
610 ff_texturedsp_init(&ctx->texdsp);
611 bytestream2_init(gbc, avpkt->data, avpkt->size);
612
613 if (bytestream2_get_bytes_left(gbc) < 128) {
Andreas Cadhalpun7b2211b2015-11-11 00:16:09614 av_log(avctx, AV_LOG_ERROR, "Frame is too small (%d).\n",
Vittorio Giovara5c018ee2015-05-26 23:30:51615 bytestream2_get_bytes_left(gbc));
616 return AVERROR_INVALIDDATA;
617 }
618
619 if (bytestream2_get_le32(gbc) != MKTAG('D', 'D', 'S', ' ') ||
620 bytestream2_get_le32(gbc) != 124) { // header size
Andreas Cadhalpun7b2211b2015-11-11 00:16:09621 av_log(avctx, AV_LOG_ERROR, "Invalid DDS header.\n");
Vittorio Giovara5c018ee2015-05-26 23:30:51622 return AVERROR_INVALIDDATA;
623 }
624
625 bytestream2_skip(gbc, 4); // flags
626
627 avctx->height = bytestream2_get_le32(gbc);
628 avctx->width = bytestream2_get_le32(gbc);
629 ret = av_image_check_size(avctx->width, avctx->height, 0, avctx);
630 if (ret < 0) {
631 av_log(avctx, AV_LOG_ERROR, "Invalid image size %dx%d.\n",
632 avctx->width, avctx->height);
633 return ret;
634 }
635
636 /* Since codec is based on 4x4 blocks, size is aligned to 4. */
637 avctx->coded_width = FFALIGN(avctx->width, TEXTURE_BLOCK_W);
638 avctx->coded_height = FFALIGN(avctx->height, TEXTURE_BLOCK_H);
639
640 bytestream2_skip(gbc, 4); // pitch
641 bytestream2_skip(gbc, 4); // depth
642 mipmap = bytestream2_get_le32(gbc);
643 if (mipmap != 0)
644 av_log(avctx, AV_LOG_VERBOSE, "Found %d mipmaps (ignored).\n", mipmap);
645
646 /* Extract pixel format information, considering additional elements
647 * in reserved1 and reserved2. */
648 ret = parse_pixel_format(avctx);
649 if (ret < 0)
650 return ret;
651
652 ret = ff_get_buffer(avctx, frame, 0);
653 if (ret < 0)
654 return ret;
655
656 if (ctx->compressed) {
Andreas Cadhalpun29b17522015-11-11 00:15:55657 int size = (avctx->coded_height / TEXTURE_BLOCK_H) *
658 (avctx->coded_width / TEXTURE_BLOCK_W) * ctx->tex_ratio;
Tom Butterworthebe8b5d2015-07-22 23:03:29659 ctx->slice_count = av_clip(avctx->thread_count, 1,
660 avctx->coded_height / TEXTURE_BLOCK_H);
Luca Barbato6b2b26e2015-07-17 01:07:08661
Andreas Cadhalpun29b17522015-11-11 00:15:55662 if (bytestream2_get_bytes_left(gbc) < size) {
663 av_log(avctx, AV_LOG_ERROR,
664 "Compressed Buffer is too small (%d < %d).\n",
665 bytestream2_get_bytes_left(gbc), size);
666 return AVERROR_INVALIDDATA;
667 }
668
Vittorio Giovara5c018ee2015-05-26 23:30:51669 /* Use the decompress function on the texture, one block per thread. */
670 ctx->tex_data = gbc->buffer;
Tom Butterworthebe8b5d2015-07-22 23:03:29671 avctx->execute2(avctx, decompress_texture_thread, frame, NULL, ctx->slice_count);
Vittorio Giovara5c018ee2015-05-26 23:30:51672 } else {
673 int linesize = av_image_get_linesize(avctx->pix_fmt, frame->width, 0);
674
675 if (ctx->paletted) {
Luca Barbato4d554842015-06-23 12:48:10676 int i;
Martin Storsjö44f7df02015-07-23 14:59:44677 uint32_t *p = (uint32_t*) frame->data[1];
Luca Barbato4d554842015-06-23 12:48:10678
Vittorio Giovara5c018ee2015-05-26 23:30:51679 /* Use the first 1024 bytes as palette, then copy the rest. */
Vittorio Giovara57214b22015-07-20 22:47:12680 for (i = 0; i < 256; i++) {
Martin Storsjö44f7df02015-07-23 14:59:44681 uint32_t rgba = 0;
682 rgba |= bytestream2_get_byte(gbc) << 16;
683 rgba |= bytestream2_get_byte(gbc) << 8;
684 rgba |= bytestream2_get_byte(gbc) << 0;
685 rgba |= bytestream2_get_byte(gbc) << 24;
686 p[i] = rgba;
Vittorio Giovara57214b22015-07-20 22:47:12687 }
Luca Barbato4d554842015-06-23 12:48:10688
Vittorio Giovara5c018ee2015-05-26 23:30:51689 frame->palette_has_changed = 1;
690 }
691
Andreas Cadhalpune6459c62015-11-11 00:14:57692 if (bytestream2_get_bytes_left(gbc) < frame->height * linesize) {
693 av_log(avctx, AV_LOG_ERROR, "Buffer is too small (%d < %d).\n",
694 bytestream2_get_bytes_left(gbc), frame->height * linesize);
695 return AVERROR_INVALIDDATA;
696 }
697
Vittorio Giovara5c018ee2015-05-26 23:30:51698 av_image_copy_plane(frame->data[0], frame->linesize[0],
699 gbc->buffer, linesize,
700 linesize, frame->height);
701 }
702
703 /* Run any post processing here if needed. */
Michael Niedermayerd08d8b62015-07-20 22:47:13704 if (avctx->pix_fmt == AV_PIX_FMT_BGRA ||
705 avctx->pix_fmt == AV_PIX_FMT_RGBA ||
706 avctx->pix_fmt == AV_PIX_FMT_YA8)
Vittorio Giovara5c018ee2015-05-26 23:30:51707 run_postproc(avctx, frame);
708
709 /* Frame is ready to be output. */
710 frame->pict_type = AV_PICTURE_TYPE_I;
711 frame->key_frame = 1;
712 *got_frame = 1;
713
714 return avpkt->size;
715}
716
717AVCodec ff_dds_decoder = {
718 .name = "dds",
719 .long_name = NULL_IF_CONFIG_SMALL("DirectDraw Surface image decoder"),
720 .type = AVMEDIA_TYPE_VIDEO,
721 .id = AV_CODEC_ID_DDS,
722 .decode = dds_decode,
723 .priv_data_size = sizeof(DDSContext),
Vittorio Giovaradef97852015-07-07 00:41:27724 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_SLICE_THREADS,
Vittorio Giovara5c018ee2015-05-26 23:30:51725 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE
726};