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/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
108 int slice_size; // Optimal slice size
110 /* Pointer to the selected compress or decompress function. */
111 int (*tex_funct)(uint8_t *dst, ptrdiff_t stride, const uint8_t *block);
114 static int parse_pixel_format(AVCodecContext *avctx)
116 DDSContext *ctx = avctx->priv_data;
117 GetByteContext *gbc = &ctx->gbc;
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;
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);
132 /* Now the real DDPF starts. */
133 size = bytestream2_get_le32(gbc);
135 av_log(avctx, AV_LOG_ERROR, "Invalid pixel format header %d.\n", size);
136 return AVERROR_INVALIDDATA;
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);
144 bpp = bytestream2_get_le32(gbc); // rgbbitcount
145 r = bytestream2_get_le32(gbc); // rbitmask
146 g = bytestream2_get_le32(gbc); // gbitmask
147 b = bytestream2_get_le32(gbc); // bbitmask
148 a = bytestream2_get_le32(gbc); // abitmask
150 bytestream2_skip(gbc, 4); // caps
151 bytestream2_skip(gbc, 4); // caps2
152 bytestream2_skip(gbc, 4); // caps3
153 bytestream2_skip(gbc, 4); // caps4
154 bytestream2_skip(gbc, 4); // reserved2
156 av_get_codec_tag_string(buf, sizeof(buf), fourcc);
157 av_log(avctx, AV_LOG_VERBOSE, "fourcc %s bpp %d "
158 "r 0x%x g 0x%x b 0x%x a 0x%x\n", buf, bpp, r, g, b, a);
160 av_get_codec_tag_string(buf, sizeof(buf), gimp_tag);
161 av_log(avctx, AV_LOG_VERBOSE, "and GIMP-DDS tag %s\n", buf);
165 avctx->pix_fmt = AV_PIX_FMT_RGBA;
167 if (ctx->compressed) {
169 case MKTAG('D', 'X', 'T', '1'):
171 ctx->tex_funct = ctx->texdsp.dxt1a_block;
173 case MKTAG('D', 'X', 'T', '2'):
175 ctx->tex_funct = ctx->texdsp.dxt2_block;
177 case MKTAG('D', 'X', 'T', '3'):
179 ctx->tex_funct = ctx->texdsp.dxt3_block;
181 case MKTAG('D', 'X', 'T', '4'):
183 ctx->tex_funct = ctx->texdsp.dxt4_block;
185 case MKTAG('D', 'X', 'T', '5'):
188 ctx->tex_funct = ctx->texdsp.dxt5ys_block;
189 else if (ycocg_classic)
190 ctx->tex_funct = ctx->texdsp.dxt5y_block;
192 ctx->tex_funct = ctx->texdsp.dxt5_block;
194 case MKTAG('R', 'X', 'G', 'B'):
196 ctx->tex_funct = ctx->texdsp.dxt5_block;
197 /* 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;
202 case MKTAG('A', 'T', 'I', '1'):
203 case MKTAG('B', 'C', '4', 'U'):
205 ctx->tex_funct = ctx->texdsp.rgtc1u_block;
207 case MKTAG('B', 'C', '4', 'S'):
209 ctx->tex_funct = ctx->texdsp.rgtc1s_block;
211 case MKTAG('A', 'T', 'I', '2'):
212 /* RGT2 variant with swapped R and G (3Dc)*/
214 ctx->tex_funct = ctx->texdsp.dxn3dc_block;
216 case MKTAG('B', 'C', '5', 'U'):
218 ctx->tex_funct = ctx->texdsp.rgtc2u_block;
220 case MKTAG('B', 'C', '5', 'S'):
222 ctx->tex_funct = ctx->texdsp.rgtc2s_block;
224 case MKTAG('U', 'Y', 'V', 'Y'):
226 avctx->pix_fmt = AV_PIX_FMT_UYVY422;
228 case MKTAG('Y', 'U', 'Y', '2'):
230 avctx->pix_fmt = AV_PIX_FMT_YUYV422;
232 case MKTAG('P', '8', ' ', ' '):
233 /* ATI Palette8, same as normal palette */
236 avctx->pix_fmt = AV_PIX_FMT_PAL8;
238 case MKTAG('D', 'X', '1', '0'):
239 /* DirectX 10 extra header */
240 dxgi = bytestream2_get_le32(gbc);
241 bytestream2_skip(gbc, 4); // resourceDimension
242 bytestream2_skip(gbc, 4); // miscFlag
243 array = bytestream2_get_le32(gbc);
244 bytestream2_skip(gbc, 4); // miscFlag2
247 av_log(avctx, AV_LOG_VERBOSE,
248 "Found array of size %d (ignored).\n", array);
250 /* Only BC[1-5] are actually compressed. */
251 ctx->compressed = (dxgi >= 70) && (dxgi <= 84);
253 av_log(avctx, AV_LOG_VERBOSE, "DXGI format %d.\n", dxgi);
256 case DXGI_FORMAT_R16G16B16A16_TYPELESS:
257 case DXGI_FORMAT_R16G16B16A16_FLOAT:
258 case DXGI_FORMAT_R16G16B16A16_UNORM:
259 case DXGI_FORMAT_R16G16B16A16_UINT:
260 case DXGI_FORMAT_R16G16B16A16_SNORM:
261 case DXGI_FORMAT_R16G16B16A16_SINT:
262 avctx->pix_fmt = AV_PIX_FMT_BGRA64;
264 case DXGI_FORMAT_R8G8B8A8_UNORM_SRGB:
265 avctx->colorspace = AVCOL_SPC_RGB;
266 case DXGI_FORMAT_R8G8B8A8_TYPELESS:
267 case DXGI_FORMAT_R8G8B8A8_UNORM:
268 case DXGI_FORMAT_R8G8B8A8_UINT:
269 case DXGI_FORMAT_R8G8B8A8_SNORM:
270 case DXGI_FORMAT_R8G8B8A8_SINT:
271 avctx->pix_fmt = AV_PIX_FMT_BGRA;
273 case DXGI_FORMAT_B8G8R8A8_UNORM_SRGB:
274 avctx->colorspace = AVCOL_SPC_RGB;
275 case DXGI_FORMAT_B8G8R8A8_TYPELESS:
276 case DXGI_FORMAT_B8G8R8A8_UNORM:
277 avctx->pix_fmt = AV_PIX_FMT_RGBA;
279 case DXGI_FORMAT_B8G8R8X8_UNORM_SRGB:
280 avctx->colorspace = AVCOL_SPC_RGB;
281 case DXGI_FORMAT_B8G8R8X8_TYPELESS:
282 case DXGI_FORMAT_B8G8R8X8_UNORM:
283 avctx->pix_fmt = AV_PIX_FMT_RGBA; // opaque
285 case DXGI_FORMAT_B5G6R5_UNORM:
286 avctx->pix_fmt = AV_PIX_FMT_RGB565LE;
289 case DXGI_FORMAT_BC1_UNORM_SRGB:
290 avctx->colorspace = AVCOL_SPC_RGB;
291 case DXGI_FORMAT_BC1_TYPELESS:
292 case DXGI_FORMAT_BC1_UNORM:
294 ctx->tex_funct = ctx->texdsp.dxt1a_block;
296 case DXGI_FORMAT_BC2_UNORM_SRGB:
297 avctx->colorspace = AVCOL_SPC_RGB;
298 case DXGI_FORMAT_BC2_TYPELESS:
299 case DXGI_FORMAT_BC2_UNORM:
301 ctx->tex_funct = ctx->texdsp.dxt3_block;
303 case DXGI_FORMAT_BC3_UNORM_SRGB:
304 avctx->colorspace = AVCOL_SPC_RGB;
305 case DXGI_FORMAT_BC3_TYPELESS:
306 case DXGI_FORMAT_BC3_UNORM:
308 ctx->tex_funct = ctx->texdsp.dxt5_block;
310 case DXGI_FORMAT_BC4_TYPELESS:
311 case DXGI_FORMAT_BC4_UNORM:
313 ctx->tex_funct = ctx->texdsp.rgtc1u_block;
315 case DXGI_FORMAT_BC4_SNORM:
317 ctx->tex_funct = ctx->texdsp.rgtc1s_block;
319 case DXGI_FORMAT_BC5_TYPELESS:
320 case DXGI_FORMAT_BC5_UNORM:
322 ctx->tex_funct = ctx->texdsp.rgtc2u_block;
324 case DXGI_FORMAT_BC5_SNORM:
326 ctx->tex_funct = ctx->texdsp.rgtc2s_block;
329 av_log(avctx, AV_LOG_ERROR,
330 "Unsupported DXGI format %d.\n", dxgi);
331 return AVERROR_INVALIDDATA;
335 av_log(avctx, AV_LOG_ERROR, "Unsupported %s fourcc.\n", buf);
336 return AVERROR_INVALIDDATA;
338 } else if (ctx->paletted) {
340 avctx->pix_fmt = AV_PIX_FMT_PAL8;
342 av_log(avctx, AV_LOG_ERROR, "Unsupported palette bpp %d.\n", bpp);
343 return AVERROR_INVALIDDATA;
347 if (bpp == 8 && r == 0xff && g == 0 && b == 0 && a == 0)
348 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
350 else if (bpp == 16 && r == 0xff && g == 0 && b == 0 && a == 0xff00)
351 avctx->pix_fmt = AV_PIX_FMT_YA8;
352 else if (bpp == 16 && r == 0xffff && g == 0 && b == 0 && a == 0)
353 avctx->pix_fmt = AV_PIX_FMT_GRAY16LE;
354 else if (bpp == 16 && r == 0xf800 && g == 0x7e0 && b == 0x1f && a == 0)
355 avctx->pix_fmt = AV_PIX_FMT_RGB565LE;
357 else if (bpp == 24 && r == 0xff0000 && g == 0xff00 && b == 0xff && a == 0)
358 avctx->pix_fmt = AV_PIX_FMT_BGR24;
360 else if (bpp == 32 && r == 0xff0000 && g == 0xff00 && b == 0xff && a == 0)
361 avctx->pix_fmt = AV_PIX_FMT_BGR0; // opaque
362 else if (bpp == 32 && r == 0xff && g == 0xff00 && b == 0xff0000 && a == 0)
363 avctx->pix_fmt = AV_PIX_FMT_RGB0; // opaque
364 else if (bpp == 32 && r == 0xff0000 && g == 0xff00 && b == 0xff && a == 0xff000000)
365 avctx->pix_fmt = AV_PIX_FMT_BGRA;
366 else if (bpp == 32 && r == 0xff && g == 0xff00 && b == 0xff0000 && a == 0xff000000)
367 avctx->pix_fmt = AV_PIX_FMT_RGBA;
370 av_log(avctx, AV_LOG_ERROR, "Unknown pixel format "
371 "[bpp %d r 0x%x g 0x%x b 0x%x a 0x%x].\n", bpp, r, g, b, a);
372 return AVERROR_INVALIDDATA;
376 /* Set any remaining post-proc that should happen before frame is ready. */
378 ctx->postproc = DDS_ALPHA_EXP;
380 ctx->postproc = DDS_NORMAL_MAP;
381 else if (ycocg_classic && !ctx->compressed)
382 ctx->postproc = DDS_RAW_YCOCG;
383 else if (avctx->pix_fmt == AV_PIX_FMT_YA8)
384 ctx->postproc = DDS_SWAP_ALPHA;
386 /* ATI/NVidia variants sometimes add swizzling in bpp. */
388 case MKTAG('A', '2', 'X', 'Y'):
389 ctx->postproc = DDS_SWIZZLE_A2XY;
391 case MKTAG('x', 'G', 'B', 'R'):
392 ctx->postproc = DDS_SWIZZLE_XGBR;
394 case MKTAG('x', 'R', 'B', 'G'):
395 ctx->postproc = DDS_SWIZZLE_XRBG;
397 case MKTAG('R', 'B', 'x', 'G'):
398 ctx->postproc = DDS_SWIZZLE_RBXG;
400 case MKTAG('R', 'G', 'x', 'B'):
401 ctx->postproc = DDS_SWIZZLE_RGXB;
403 case MKTAG('R', 'x', 'B', 'G'):
404 ctx->postproc = DDS_SWIZZLE_RXBG;
406 case MKTAG('x', 'G', 'x', 'R'):
407 ctx->postproc = DDS_SWIZZLE_XGXR;
409 case MKTAG('A', '2', 'D', '5'):
410 ctx->postproc = DDS_NORMAL_MAP;
417 static int decompress_texture_thread(AVCodecContext *avctx, void *arg,
418 int slice, int thread_nb)
420 DDSContext *ctx = avctx->priv_data;
421 AVFrame *frame = arg;
422 const uint8_t *d = ctx->tex_data;
423 int w_block = avctx->coded_width / TEXTURE_BLOCK_W;
425 int start_slice, end_slice;
427 start_slice = slice * ctx->slice_size;
428 end_slice = FFMIN(start_slice + ctx->slice_size, avctx->coded_height);
430 start_slice /= TEXTURE_BLOCK_H;
431 end_slice /= TEXTURE_BLOCK_H;
433 for (y = start_slice; y < end_slice; y++) {
434 uint8_t *p = frame->data[0] + y * frame->linesize[0] * TEXTURE_BLOCK_H;
435 int off = y * w_block;
436 for (x = 0; x < w_block; x++) {
437 ctx->tex_funct(p + x * 16, frame->linesize[0],
438 d + (off + x) * ctx->tex_ratio);
445 static void do_swizzle(AVFrame *frame, int x, int y)
448 for (i = 0; i < frame->linesize[0] * frame->height; i += 4) {
449 uint8_t *src = frame->data[0] + i;
450 FFSWAP(uint8_t, src[x], src[y]);
454 static void run_postproc(AVCodecContext *avctx, AVFrame *frame)
456 DDSContext *ctx = avctx->priv_data;
459 switch (ctx->postproc) {
461 /* Alpha-exponential mode divides each channel by the maximum
462 * R, G or B value, and stores the multiplying factor in the
464 av_log(avctx, AV_LOG_DEBUG, "Post-processing alpha exponent.\n");
466 for (i = 0; i < frame->linesize[0] * frame->height; i += 4) {
467 uint8_t *src = frame->data[0] + i;
473 src[0] = r * a / 255;
474 src[1] = g * a / 255;
475 src[2] = b * a / 255;
480 /* Normal maps work in the XYZ color space and they encode
481 * X in R or in A, depending on the texture type, Y in G and
482 * derive Z with a square root of the distance.
484 * http://www.realtimecollisiondetection.net/blog/?p=28 */
485 av_log(avctx, AV_LOG_DEBUG, "Post-processing normal map.\n");
487 x_off = ctx->tex_ratio == 8 ? 0 : 3;
488 for (i = 0; i < frame->linesize[0] * frame->height; i += 4) {
489 uint8_t *src = frame->data[0] + i;
494 int d = (255 * 255 - x * x - y * y) / 2;
505 /* Data is Y-Co-Cg-A and not RGBA, but they are represented
506 * with the same masks in the DDPF header. */
507 av_log(avctx, AV_LOG_DEBUG, "Post-processing raw YCoCg.\n");
509 for (i = 0; i < frame->linesize[0] * frame->height; i += 4) {
510 uint8_t *src = frame->data[0] + i;
512 int cg = src[1] - 128;
513 int co = src[2] - 128;
516 src[0] = av_clip_uint8(y + co - cg);
517 src[1] = av_clip_uint8(y + cg);
518 src[2] = av_clip_uint8(y - co - cg);
523 /* Alpha and Luma are stored swapped. */
524 av_log(avctx, AV_LOG_DEBUG, "Post-processing swapped Luma/Alpha.\n");
526 for (i = 0; i < frame->linesize[0] * frame->height; i += 2) {
527 uint8_t *src = frame->data[0] + i;
528 FFSWAP(uint8_t, src[0], src[1]);
531 case DDS_SWIZZLE_A2XY:
532 /* Swap R and G, often used to restore a standard RGTC2. */
533 av_log(avctx, AV_LOG_DEBUG, "Post-processing A2XY swizzle.\n");
534 do_swizzle(frame, 0, 1);
536 case DDS_SWIZZLE_RBXG:
537 /* Swap G and A, then B and new A (G). */
538 av_log(avctx, AV_LOG_DEBUG, "Post-processing RBXG swizzle.\n");
539 do_swizzle(frame, 1, 3);
540 do_swizzle(frame, 2, 3);
542 case DDS_SWIZZLE_RGXB:
544 av_log(avctx, AV_LOG_DEBUG, "Post-processing RGXB swizzle.\n");
545 do_swizzle(frame, 2, 3);
547 case DDS_SWIZZLE_RXBG:
549 av_log(avctx, AV_LOG_DEBUG, "Post-processing RXBG swizzle.\n");
550 do_swizzle(frame, 1, 3);
552 case DDS_SWIZZLE_RXGB:
553 /* Swap R and A (misleading name). */
554 av_log(avctx, AV_LOG_DEBUG, "Post-processing RXGB swizzle.\n");
555 do_swizzle(frame, 0, 3);
557 case DDS_SWIZZLE_XGBR:
558 /* Swap B and A, then R and new A (B). */
559 av_log(avctx, AV_LOG_DEBUG, "Post-processing XGBR swizzle.\n");
560 do_swizzle(frame, 2, 3);
561 do_swizzle(frame, 0, 3);
563 case DDS_SWIZZLE_XGXR:
564 /* Swap G and A, then R and new A (G), then new R (G) and new G (A).
565 * This variant does not store any B component. */
566 av_log(avctx, AV_LOG_DEBUG, "Post-processing XGXR swizzle.\n");
567 do_swizzle(frame, 1, 3);
568 do_swizzle(frame, 0, 3);
569 do_swizzle(frame, 0, 1);
571 case DDS_SWIZZLE_XRBG:
572 /* Swap G and A, then R and new A (G). */
573 av_log(avctx, AV_LOG_DEBUG, "Post-processing XRBG swizzle.\n");
574 do_swizzle(frame, 1, 3);
575 do_swizzle(frame, 0, 3);
580 static int dds_decode(AVCodecContext *avctx, void *data,
581 int *got_frame, AVPacket *avpkt)
583 DDSContext *ctx = avctx->priv_data;
584 GetByteContext *gbc = &ctx->gbc;
585 AVFrame *frame = data;
589 ff_texturedsp_init(&ctx->texdsp);
590 bytestream2_init(gbc, avpkt->data, avpkt->size);
592 if (bytestream2_get_bytes_left(gbc) < 128) {
593 av_log(avctx, AV_LOG_ERROR, "Frame is too small (%d).",
594 bytestream2_get_bytes_left(gbc));
595 return AVERROR_INVALIDDATA;
598 if (bytestream2_get_le32(gbc) != MKTAG('D', 'D', 'S', ' ') ||
599 bytestream2_get_le32(gbc) != 124) { // header size
600 av_log(avctx, AV_LOG_ERROR, "Invalid DDS header.");
601 return AVERROR_INVALIDDATA;
604 bytestream2_skip(gbc, 4); // flags
606 avctx->height = bytestream2_get_le32(gbc);
607 avctx->width = bytestream2_get_le32(gbc);
608 ret = av_image_check_size(avctx->width, avctx->height, 0, avctx);
610 av_log(avctx, AV_LOG_ERROR, "Invalid image size %dx%d.\n",
611 avctx->width, avctx->height);
615 /* Since codec is based on 4x4 blocks, size is aligned to 4. */
616 avctx->coded_width = FFALIGN(avctx->width, TEXTURE_BLOCK_W);
617 avctx->coded_height = FFALIGN(avctx->height, TEXTURE_BLOCK_H);
619 bytestream2_skip(gbc, 4); // pitch
620 bytestream2_skip(gbc, 4); // depth
621 mipmap = bytestream2_get_le32(gbc);
623 av_log(avctx, AV_LOG_VERBOSE, "Found %d mipmaps (ignored).\n", mipmap);
625 /* Extract pixel format information, considering additional elements
626 * in reserved1 and reserved2. */
627 ret = parse_pixel_format(avctx);
631 ret = ff_get_buffer(avctx, frame, 0);
635 if (ctx->compressed) {
636 int slices = FFMIN(avctx->thread_count,
637 avctx->coded_height / TEXTURE_BLOCK_H);
638 ctx->slice_size = avctx->coded_height / slices;
640 /* Use the decompress function on the texture, one block per thread. */
641 ctx->tex_data = gbc->buffer;
642 avctx->execute2(avctx, decompress_texture_thread, frame, NULL, slices);
644 int linesize = av_image_get_linesize(avctx->pix_fmt, frame->width, 0);
648 /* Use the first 1024 bytes as palette, then copy the rest. */
649 bytestream2_get_buffer(gbc, frame->data[1], 256 * 4);
650 for (i = 0; i < 256; i++)
651 AV_WN32(frame->data[1] + i*4,
652 (frame->data[1][2+i*4]<<0)+
653 (frame->data[1][1+i*4]<<8)+
654 (frame->data[1][0+i*4]<<16)+
655 (frame->data[1][3+i*4]<<24)
658 frame->palette_has_changed = 1;
661 av_image_copy_plane(frame->data[0], frame->linesize[0],
662 gbc->buffer, linesize,
663 linesize, frame->height);
666 /* Run any post processing here if needed. */
667 if (avctx->pix_fmt == AV_PIX_FMT_BGRA ||
668 avctx->pix_fmt == AV_PIX_FMT_RGBA ||
669 avctx->pix_fmt == AV_PIX_FMT_RGB0 ||
670 avctx->pix_fmt == AV_PIX_FMT_BGR0 ||
671 avctx->pix_fmt == AV_PIX_FMT_YA8)
672 run_postproc(avctx, frame);
674 /* Frame is ready to be output. */
675 frame->pict_type = AV_PICTURE_TYPE_I;
676 frame->key_frame = 1;
682 AVCodec ff_dds_decoder = {
684 .long_name = NULL_IF_CONFIG_SMALL("DirectDraw Surface image decoder"),
685 .type = AVMEDIA_TYPE_VIDEO,
686 .id = AV_CODEC_ID_DDS,
687 .decode = dds_decode,
688 .priv_data_size = sizeof(DDSContext),
689 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_SLICE_THREADS,
690 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE