2 * DirectDraw Surface image decoder
3 * Copyright (C) 2015 Vittorio Giovara <vittorio.giovara@gmail.com>
5 * This file is part of FFmpeg.
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.
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.
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
26 * https://msdn.microsoft.com/en-us/library/bb943982%28v=vs.85%29.aspx
31 #include "libavutil/libm.h"
32 #include "libavutil/imgutils.h"
35 #include "bytestream.h"
37 #include "texturedsp.h"
40 #define DDPF_FOURCC (1 << 2)
41 #define DDPF_PALETTE (1 << 5)
42 #define DDPF_NORMALMAP (1U << 31)
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,
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,
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,
99 typedef struct DDSContext {
100 TextureDSPContext texdsp;
106 enum DDSPostProc postproc;
108 const uint8_t *tex_data; // Compressed texture
109 int tex_ratio; // Compression ratio
110 int slice_count; // Number of slices for threaded operations
112 /* Pointer to the selected compress or decompress function. */
113 int (*tex_funct)(uint8_t *dst, ptrdiff_t stride, const uint8_t *block);
116 static int parse_pixel_format(AVCodecContext *avctx)
118 DDSContext *ctx = avctx->priv_data;
119 GetByteContext *gbc = &ctx->gbc;
120 uint32_t flags, fourcc, gimp_tag;
121 enum DDSDXGIFormat dxgi;
122 int size, bpp, r, g, b, a;
123 int alpha_exponent, ycocg_classic, ycocg_scaled, normal_map, array;
125 /* Alternative DDS implementations use reserved1 as custom header. */
126 bytestream2_skip(gbc, 4 * 3);
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');
131 bytestream2_skip(gbc, 4 * 7);
133 /* Now the real DDPF starts. */
134 size = bytestream2_get_le32(gbc);
136 av_log(avctx, AV_LOG_ERROR, "Invalid pixel format header %d.\n", size);
137 return AVERROR_INVALIDDATA;
139 flags = bytestream2_get_le32(gbc);
140 ctx->compressed = flags & DDPF_FOURCC;
141 ctx->paletted = flags & DDPF_PALETTE;
142 normal_map = flags & DDPF_NORMALMAP;
143 fourcc = bytestream2_get_le32(gbc);
145 if (ctx->compressed && ctx->paletted) {
146 av_log(avctx, AV_LOG_WARNING,
147 "Disabling invalid palette flag for compressed dds.\n");
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
157 bytestream2_skip(gbc, 4); // caps
158 bytestream2_skip(gbc, 4); // caps2
159 bytestream2_skip(gbc, 4); // caps3
160 bytestream2_skip(gbc, 4); // caps4
161 bytestream2_skip(gbc, 4); // reserved2
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", av_fourcc2str(fourcc), bpp, r, g, b, a);
166 av_log(avctx, AV_LOG_VERBOSE, "and GIMP-DDS tag %s\n", av_fourcc2str(gimp_tag));
169 avctx->pix_fmt = AV_PIX_FMT_RGBA;
171 if (ctx->compressed) {
173 case MKTAG('D', 'X', 'T', '1'):
175 ctx->tex_funct = ctx->texdsp.dxt1a_block;
177 case MKTAG('D', 'X', 'T', '2'):
179 ctx->tex_funct = ctx->texdsp.dxt2_block;
181 case MKTAG('D', 'X', 'T', '3'):
183 ctx->tex_funct = ctx->texdsp.dxt3_block;
185 case MKTAG('D', 'X', 'T', '4'):
187 ctx->tex_funct = ctx->texdsp.dxt4_block;
189 case MKTAG('D', 'X', 'T', '5'):
192 ctx->tex_funct = ctx->texdsp.dxt5ys_block;
193 else if (ycocg_classic)
194 ctx->tex_funct = ctx->texdsp.dxt5y_block;
196 ctx->tex_funct = ctx->texdsp.dxt5_block;
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. */
203 ctx->postproc = DDS_SWIZZLE_RXGB;
206 case MKTAG('A', 'T', 'I', '1'):
207 case MKTAG('B', 'C', '4', 'U'):
209 ctx->tex_funct = ctx->texdsp.rgtc1u_block;
211 case MKTAG('B', 'C', '4', 'S'):
213 ctx->tex_funct = ctx->texdsp.rgtc1s_block;
215 case MKTAG('A', 'T', 'I', '2'):
216 /* RGT2 variant with swapped R and G (3Dc)*/
218 ctx->tex_funct = ctx->texdsp.dxn3dc_block;
220 case MKTAG('B', 'C', '5', 'U'):
222 ctx->tex_funct = ctx->texdsp.rgtc2u_block;
224 case MKTAG('B', 'C', '5', 'S'):
226 ctx->tex_funct = ctx->texdsp.rgtc2s_block;
228 case MKTAG('U', 'Y', 'V', 'Y'):
230 avctx->pix_fmt = AV_PIX_FMT_UYVY422;
232 case MKTAG('Y', 'U', 'Y', '2'):
234 avctx->pix_fmt = AV_PIX_FMT_YUYV422;
236 case MKTAG('P', '8', ' ', ' '):
237 /* ATI Palette8, same as normal palette */
240 avctx->pix_fmt = AV_PIX_FMT_PAL8;
242 case MKTAG('G', '1', ' ', ' '):
244 avctx->pix_fmt = AV_PIX_FMT_MONOBLACK;
246 case MKTAG('D', 'X', '1', '0'):
247 /* DirectX 10 extra header */
248 dxgi = bytestream2_get_le32(gbc);
249 bytestream2_skip(gbc, 4); // resourceDimension
250 bytestream2_skip(gbc, 4); // miscFlag
251 array = bytestream2_get_le32(gbc);
252 bytestream2_skip(gbc, 4); // miscFlag2
255 av_log(avctx, AV_LOG_VERBOSE,
256 "Found array of size %d (ignored).\n", array);
258 /* Only BC[1-5] are actually compressed. */
259 ctx->compressed = (dxgi >= 70) && (dxgi <= 84);
261 av_log(avctx, AV_LOG_VERBOSE, "DXGI format %d.\n", dxgi);
264 case DXGI_FORMAT_R16G16B16A16_TYPELESS:
265 case DXGI_FORMAT_R16G16B16A16_FLOAT:
266 case DXGI_FORMAT_R16G16B16A16_UNORM:
267 case DXGI_FORMAT_R16G16B16A16_UINT:
268 case DXGI_FORMAT_R16G16B16A16_SNORM:
269 case DXGI_FORMAT_R16G16B16A16_SINT:
270 avctx->pix_fmt = AV_PIX_FMT_BGRA64;
272 case DXGI_FORMAT_R8G8B8A8_UNORM_SRGB:
273 avctx->colorspace = AVCOL_SPC_RGB;
274 case DXGI_FORMAT_R8G8B8A8_TYPELESS:
275 case DXGI_FORMAT_R8G8B8A8_UNORM:
276 case DXGI_FORMAT_R8G8B8A8_UINT:
277 case DXGI_FORMAT_R8G8B8A8_SNORM:
278 case DXGI_FORMAT_R8G8B8A8_SINT:
279 avctx->pix_fmt = AV_PIX_FMT_BGRA;
281 case DXGI_FORMAT_B8G8R8A8_UNORM_SRGB:
282 avctx->colorspace = AVCOL_SPC_RGB;
283 case DXGI_FORMAT_B8G8R8A8_TYPELESS:
284 case DXGI_FORMAT_B8G8R8A8_UNORM:
285 avctx->pix_fmt = AV_PIX_FMT_RGBA;
287 case DXGI_FORMAT_B8G8R8X8_UNORM_SRGB:
288 avctx->colorspace = AVCOL_SPC_RGB;
289 case DXGI_FORMAT_B8G8R8X8_TYPELESS:
290 case DXGI_FORMAT_B8G8R8X8_UNORM:
291 avctx->pix_fmt = AV_PIX_FMT_RGBA; // opaque
293 case DXGI_FORMAT_B5G6R5_UNORM:
294 avctx->pix_fmt = AV_PIX_FMT_RGB565LE;
297 case DXGI_FORMAT_BC1_UNORM_SRGB:
298 avctx->colorspace = AVCOL_SPC_RGB;
299 case DXGI_FORMAT_BC1_TYPELESS:
300 case DXGI_FORMAT_BC1_UNORM:
302 ctx->tex_funct = ctx->texdsp.dxt1a_block;
304 case DXGI_FORMAT_BC2_UNORM_SRGB:
305 avctx->colorspace = AVCOL_SPC_RGB;
306 case DXGI_FORMAT_BC2_TYPELESS:
307 case DXGI_FORMAT_BC2_UNORM:
309 ctx->tex_funct = ctx->texdsp.dxt3_block;
311 case DXGI_FORMAT_BC3_UNORM_SRGB:
312 avctx->colorspace = AVCOL_SPC_RGB;
313 case DXGI_FORMAT_BC3_TYPELESS:
314 case DXGI_FORMAT_BC3_UNORM:
316 ctx->tex_funct = ctx->texdsp.dxt5_block;
318 case DXGI_FORMAT_BC4_TYPELESS:
319 case DXGI_FORMAT_BC4_UNORM:
321 ctx->tex_funct = ctx->texdsp.rgtc1u_block;
323 case DXGI_FORMAT_BC4_SNORM:
325 ctx->tex_funct = ctx->texdsp.rgtc1s_block;
327 case DXGI_FORMAT_BC5_TYPELESS:
328 case DXGI_FORMAT_BC5_UNORM:
330 ctx->tex_funct = ctx->texdsp.rgtc2u_block;
332 case DXGI_FORMAT_BC5_SNORM:
334 ctx->tex_funct = ctx->texdsp.rgtc2s_block;
337 av_log(avctx, AV_LOG_ERROR,
338 "Unsupported DXGI format %d.\n", dxgi);
339 return AVERROR_INVALIDDATA;
343 av_log(avctx, AV_LOG_ERROR, "Unsupported %s fourcc.\n", av_fourcc2str(fourcc));
344 return AVERROR_INVALIDDATA;
346 } else if (ctx->paletted) {
348 avctx->pix_fmt = AV_PIX_FMT_PAL8;
350 av_log(avctx, AV_LOG_ERROR, "Unsupported palette bpp %d.\n", bpp);
351 return AVERROR_INVALIDDATA;
355 if (bpp == 4 && r == 0 && g == 0 && b == 0 && a == 0)
356 avctx->pix_fmt = AV_PIX_FMT_PAL8;
358 else if (bpp == 8 && r == 0xff && g == 0 && b == 0 && a == 0)
359 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
360 else if (bpp == 8 && r == 0 && g == 0 && b == 0 && a == 0xff)
361 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
363 else if (bpp == 16 && r == 0xff && g == 0 && b == 0 && a == 0xff00)
364 avctx->pix_fmt = AV_PIX_FMT_YA8;
365 else if (bpp == 16 && r == 0xff00 && g == 0 && b == 0 && a == 0xff) {
366 avctx->pix_fmt = AV_PIX_FMT_YA8;
367 ctx->postproc = DDS_SWAP_ALPHA;
369 else if (bpp == 16 && r == 0xffff && g == 0 && b == 0 && a == 0)
370 avctx->pix_fmt = AV_PIX_FMT_GRAY16LE;
371 else if (bpp == 16 && r == 0x7c00 && g == 0x3e0 && b == 0x1f && a == 0)
372 avctx->pix_fmt = AV_PIX_FMT_RGB555LE;
373 else if (bpp == 16 && r == 0x7c00 && g == 0x3e0 && b == 0x1f && a == 0x8000)
374 avctx->pix_fmt = AV_PIX_FMT_RGB555LE; // alpha ignored
375 else if (bpp == 16 && r == 0xf800 && g == 0x7e0 && b == 0x1f && a == 0)
376 avctx->pix_fmt = AV_PIX_FMT_RGB565LE;
378 else if (bpp == 24 && r == 0xff0000 && g == 0xff00 && b == 0xff && a == 0)
379 avctx->pix_fmt = AV_PIX_FMT_BGR24;
381 else if (bpp == 32 && r == 0xff0000 && g == 0xff00 && b == 0xff && a == 0)
382 avctx->pix_fmt = AV_PIX_FMT_BGR0; // opaque
383 else if (bpp == 32 && r == 0xff && g == 0xff00 && b == 0xff0000 && a == 0)
384 avctx->pix_fmt = AV_PIX_FMT_RGB0; // opaque
385 else if (bpp == 32 && r == 0xff0000 && g == 0xff00 && b == 0xff && a == 0xff000000)
386 avctx->pix_fmt = AV_PIX_FMT_BGRA;
387 else if (bpp == 32 && r == 0xff && g == 0xff00 && b == 0xff0000 && a == 0xff000000)
388 avctx->pix_fmt = AV_PIX_FMT_RGBA;
391 av_log(avctx, AV_LOG_ERROR, "Unknown pixel format "
392 "[bpp %d r 0x%x g 0x%x b 0x%x a 0x%x].\n", bpp, r, g, b, a);
393 return AVERROR_INVALIDDATA;
397 /* Set any remaining post-proc that should happen before frame is ready. */
399 ctx->postproc = DDS_ALPHA_EXP;
401 ctx->postproc = DDS_NORMAL_MAP;
402 else if (ycocg_classic && !ctx->compressed)
403 ctx->postproc = DDS_RAW_YCOCG;
405 /* ATI/NVidia variants sometimes add swizzling in bpp. */
407 case MKTAG('A', '2', 'X', 'Y'):
408 ctx->postproc = DDS_SWIZZLE_A2XY;
410 case MKTAG('x', 'G', 'B', 'R'):
411 ctx->postproc = DDS_SWIZZLE_XGBR;
413 case MKTAG('x', 'R', 'B', 'G'):
414 ctx->postproc = DDS_SWIZZLE_XRBG;
416 case MKTAG('R', 'B', 'x', 'G'):
417 ctx->postproc = DDS_SWIZZLE_RBXG;
419 case MKTAG('R', 'G', 'x', 'B'):
420 ctx->postproc = DDS_SWIZZLE_RGXB;
422 case MKTAG('R', 'x', 'B', 'G'):
423 ctx->postproc = DDS_SWIZZLE_RXBG;
425 case MKTAG('x', 'G', 'x', 'R'):
426 ctx->postproc = DDS_SWIZZLE_XGXR;
428 case MKTAG('A', '2', 'D', '5'):
429 ctx->postproc = DDS_NORMAL_MAP;
436 static int decompress_texture_thread(AVCodecContext *avctx, void *arg,
437 int slice, int thread_nb)
439 DDSContext *ctx = avctx->priv_data;
440 AVFrame *frame = arg;
441 const uint8_t *d = ctx->tex_data;
442 int w_block = avctx->coded_width / TEXTURE_BLOCK_W;
443 int h_block = avctx->coded_height / TEXTURE_BLOCK_H;
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;
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
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);
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)
461 for (y = start_slice; y < end_slice; y++) {
462 uint8_t *p = frame->data[0] + y * frame->linesize[0] * TEXTURE_BLOCK_H;
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);
473 static void do_swizzle(AVFrame *frame, int x, int y)
476 for (i = 0; i < frame->linesize[0] * frame->height; i += 4) {
477 uint8_t *src = frame->data[0] + i;
478 FFSWAP(uint8_t, src[x], src[y]);
482 static void run_postproc(AVCodecContext *avctx, AVFrame *frame)
484 DDSContext *ctx = avctx->priv_data;
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
492 av_log(avctx, AV_LOG_DEBUG, "Post-processing alpha exponent.\n");
494 for (i = 0; i < frame->linesize[0] * frame->height; i += 4) {
495 uint8_t *src = frame->data[0] + i;
501 src[0] = r * a / 255;
502 src[1] = g * a / 255;
503 src[2] = b * a / 255;
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.
512 * http://www.realtimecollisiondetection.net/blog/?p=28 */
513 av_log(avctx, AV_LOG_DEBUG, "Post-processing normal map.\n");
515 x_off = ctx->tex_ratio == 8 ? 0 : 3;
516 for (i = 0; i < frame->linesize[0] * frame->height; i += 4) {
517 uint8_t *src = frame->data[0] + i;
522 int d = (255 * 255 - x * x - y * y) / 2;
533 /* Data is Y-Co-Cg-A and not RGBA, but they are represented
534 * with the same masks in the DDPF header. */
535 av_log(avctx, AV_LOG_DEBUG, "Post-processing raw YCoCg.\n");
537 for (i = 0; i < frame->linesize[0] * frame->height; i += 4) {
538 uint8_t *src = frame->data[0] + i;
540 int cg = src[1] - 128;
541 int co = src[2] - 128;
544 src[0] = av_clip_uint8(y + co - cg);
545 src[1] = av_clip_uint8(y + cg);
546 src[2] = av_clip_uint8(y - co - cg);
551 /* Alpha and Luma are stored swapped. */
552 av_log(avctx, AV_LOG_DEBUG, "Post-processing swapped Luma/Alpha.\n");
554 for (i = 0; i < frame->linesize[0] * frame->height; i += 2) {
555 uint8_t *src = frame->data[0] + i;
556 FFSWAP(uint8_t, src[0], src[1]);
559 case DDS_SWIZZLE_A2XY:
560 /* Swap R and G, often used to restore a standard RGTC2. */
561 av_log(avctx, AV_LOG_DEBUG, "Post-processing A2XY swizzle.\n");
562 do_swizzle(frame, 0, 1);
564 case DDS_SWIZZLE_RBXG:
565 /* Swap G and A, then B and new A (G). */
566 av_log(avctx, AV_LOG_DEBUG, "Post-processing RBXG swizzle.\n");
567 do_swizzle(frame, 1, 3);
568 do_swizzle(frame, 2, 3);
570 case DDS_SWIZZLE_RGXB:
572 av_log(avctx, AV_LOG_DEBUG, "Post-processing RGXB swizzle.\n");
573 do_swizzle(frame, 2, 3);
575 case DDS_SWIZZLE_RXBG:
577 av_log(avctx, AV_LOG_DEBUG, "Post-processing RXBG swizzle.\n");
578 do_swizzle(frame, 1, 3);
580 case DDS_SWIZZLE_RXGB:
581 /* Swap R and A (misleading name). */
582 av_log(avctx, AV_LOG_DEBUG, "Post-processing RXGB swizzle.\n");
583 do_swizzle(frame, 0, 3);
585 case DDS_SWIZZLE_XGBR:
586 /* Swap B and A, then R and new A (B). */
587 av_log(avctx, AV_LOG_DEBUG, "Post-processing XGBR swizzle.\n");
588 do_swizzle(frame, 2, 3);
589 do_swizzle(frame, 0, 3);
591 case DDS_SWIZZLE_XGXR:
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. */
594 av_log(avctx, AV_LOG_DEBUG, "Post-processing XGXR swizzle.\n");
595 do_swizzle(frame, 1, 3);
596 do_swizzle(frame, 0, 3);
597 do_swizzle(frame, 0, 1);
599 case DDS_SWIZZLE_XRBG:
600 /* Swap G and A, then R and new A (G). */
601 av_log(avctx, AV_LOG_DEBUG, "Post-processing XRBG swizzle.\n");
602 do_swizzle(frame, 1, 3);
603 do_swizzle(frame, 0, 3);
608 static int dds_decode(AVCodecContext *avctx, void *data,
609 int *got_frame, AVPacket *avpkt)
611 DDSContext *ctx = avctx->priv_data;
612 GetByteContext *gbc = &ctx->gbc;
613 AVFrame *frame = data;
617 ff_texturedsp_init(&ctx->texdsp);
618 bytestream2_init(gbc, avpkt->data, avpkt->size);
620 if (bytestream2_get_bytes_left(gbc) < 128) {
621 av_log(avctx, AV_LOG_ERROR, "Frame is too small (%d).\n",
622 bytestream2_get_bytes_left(gbc));
623 return AVERROR_INVALIDDATA;
626 if (bytestream2_get_le32(gbc) != MKTAG('D', 'D', 'S', ' ') ||
627 bytestream2_get_le32(gbc) != 124) { // header size
628 av_log(avctx, AV_LOG_ERROR, "Invalid DDS header.\n");
629 return AVERROR_INVALIDDATA;
632 bytestream2_skip(gbc, 4); // flags
634 avctx->height = bytestream2_get_le32(gbc);
635 avctx->width = bytestream2_get_le32(gbc);
636 ret = av_image_check_size(avctx->width, avctx->height, 0, avctx);
638 av_log(avctx, AV_LOG_ERROR, "Invalid image size %dx%d.\n",
639 avctx->width, avctx->height);
643 /* Since codec is based on 4x4 blocks, size is aligned to 4. */
644 avctx->coded_width = FFALIGN(avctx->width, TEXTURE_BLOCK_W);
645 avctx->coded_height = FFALIGN(avctx->height, TEXTURE_BLOCK_H);
647 bytestream2_skip(gbc, 4); // pitch
648 bytestream2_skip(gbc, 4); // depth
649 mipmap = bytestream2_get_le32(gbc);
651 av_log(avctx, AV_LOG_VERBOSE, "Found %d mipmaps (ignored).\n", mipmap);
653 /* Extract pixel format information, considering additional elements
654 * in reserved1 and reserved2. */
655 ret = parse_pixel_format(avctx);
659 ret = ff_get_buffer(avctx, frame, 0);
663 if (ctx->compressed) {
664 int size = (avctx->coded_height / TEXTURE_BLOCK_H) *
665 (avctx->coded_width / TEXTURE_BLOCK_W) * ctx->tex_ratio;
666 ctx->slice_count = av_clip(avctx->thread_count, 1,
667 avctx->coded_height / TEXTURE_BLOCK_H);
669 if (bytestream2_get_bytes_left(gbc) < size) {
670 av_log(avctx, AV_LOG_ERROR,
671 "Compressed Buffer is too small (%d < %d).\n",
672 bytestream2_get_bytes_left(gbc), size);
673 return AVERROR_INVALIDDATA;
676 /* Use the decompress function on the texture, one block per thread. */
677 ctx->tex_data = gbc->buffer;
678 avctx->execute2(avctx, decompress_texture_thread, frame, NULL, ctx->slice_count);
679 } else if (!ctx->paletted && ctx->bpp == 4 && avctx->pix_fmt == AV_PIX_FMT_PAL8) {
680 uint8_t *dst = frame->data[0];
683 /* Use the first 64 bytes as palette, then copy the rest. */
684 bytestream2_get_buffer(gbc, frame->data[1], 16 * 4);
685 for (i = 0; i < 16; i++) {
686 AV_WN32(frame->data[1] + i*4,
687 (frame->data[1][2+i*4]<<0)+
688 (frame->data[1][1+i*4]<<8)+
689 (frame->data[1][0+i*4]<<16)+
690 ((unsigned)frame->data[1][3+i*4]<<24)
693 frame->palette_has_changed = 1;
695 if (bytestream2_get_bytes_left(gbc) < frame->height * frame->width / 2) {
696 av_log(avctx, AV_LOG_ERROR, "Buffer is too small (%d < %d).\n",
697 bytestream2_get_bytes_left(gbc), frame->height * frame->width / 2);
698 return AVERROR_INVALIDDATA;
701 for (y = 0; y < frame->height; y++) {
702 for (x = 0; x < frame->width; x += 2) {
703 uint8_t val = bytestream2_get_byte(gbc);
705 dst[x + 1] = val >> 4;
707 dst += frame->linesize[0];
710 int linesize = av_image_get_linesize(avctx->pix_fmt, frame->width, 0);
714 /* Use the first 1024 bytes as palette, then copy the rest. */
715 bytestream2_get_buffer(gbc, frame->data[1], 256 * 4);
716 for (i = 0; i < 256; i++)
717 AV_WN32(frame->data[1] + i*4,
718 (frame->data[1][2+i*4]<<0)+
719 (frame->data[1][1+i*4]<<8)+
720 (frame->data[1][0+i*4]<<16)+
721 ((unsigned)frame->data[1][3+i*4]<<24)
724 frame->palette_has_changed = 1;
727 if (bytestream2_get_bytes_left(gbc) < frame->height * linesize) {
728 av_log(avctx, AV_LOG_ERROR, "Buffer is too small (%d < %d).\n",
729 bytestream2_get_bytes_left(gbc), frame->height * linesize);
730 return AVERROR_INVALIDDATA;
733 av_image_copy_plane(frame->data[0], frame->linesize[0],
734 gbc->buffer, linesize,
735 linesize, frame->height);
738 /* Run any post processing here if needed. */
739 if (ctx->postproc != DDS_NONE)
740 run_postproc(avctx, frame);
742 /* Frame is ready to be output. */
743 frame->pict_type = AV_PICTURE_TYPE_I;
744 frame->key_frame = 1;
750 AVCodec ff_dds_decoder = {
752 .long_name = NULL_IF_CONFIG_SMALL("DirectDraw Surface image decoder"),
753 .type = AVMEDIA_TYPE_VIDEO,
754 .id = AV_CODEC_ID_DDS,
755 .decode = dds_decode,
756 .priv_data_size = sizeof(DDSContext),
757 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_SLICE_THREADS,
758 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE