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 (1 << 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;
105 enum DDSPostProc postproc;
107 const uint8_t *tex_data; // Compressed texture
108 int tex_ratio; // Compression ratio
109 int slice_count; // Number of slices for threaded operations
111 /* Pointer to the selected compress or decompress function. */
112 int (*tex_funct)(uint8_t *dst, ptrdiff_t stride, const uint8_t *block);
115 static int parse_pixel_format(AVCodecContext *avctx)
117 DDSContext *ctx = avctx->priv_data;
118 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 = 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_get_codec_tag_string(buf, sizeof(buf), fourcc);
164 av_log(avctx, AV_LOG_VERBOSE, "fourcc %s bpp %d "
165 "r 0x%x g 0x%x b 0x%x a 0x%x\n", buf, bpp, r, g, b, a);
167 av_get_codec_tag_string(buf, sizeof(buf), gimp_tag);
168 av_log(avctx, AV_LOG_VERBOSE, "and GIMP-DDS tag %s\n", buf);
172 avctx->pix_fmt = AV_PIX_FMT_RGBA;
174 if (ctx->compressed) {
176 case MKTAG('D', 'X', 'T', '1'):
178 ctx->tex_funct = ctx->texdsp.dxt1a_block;
180 case MKTAG('D', 'X', 'T', '2'):
182 ctx->tex_funct = ctx->texdsp.dxt2_block;
184 case MKTAG('D', 'X', 'T', '3'):
186 ctx->tex_funct = ctx->texdsp.dxt3_block;
188 case MKTAG('D', 'X', 'T', '4'):
190 ctx->tex_funct = ctx->texdsp.dxt4_block;
192 case MKTAG('D', 'X', 'T', '5'):
195 ctx->tex_funct = ctx->texdsp.dxt5ys_block;
196 else if (ycocg_classic)
197 ctx->tex_funct = ctx->texdsp.dxt5y_block;
199 ctx->tex_funct = ctx->texdsp.dxt5_block;
201 case MKTAG('R', 'X', 'G', 'B'):
203 ctx->tex_funct = ctx->texdsp.dxt5_block;
204 /* This format may be considered as a normal map,
205 * but it is handled differently in a separate postproc. */
206 ctx->postproc = DDS_SWIZZLE_RXGB;
209 case MKTAG('A', 'T', 'I', '1'):
210 case MKTAG('B', 'C', '4', 'U'):
212 ctx->tex_funct = ctx->texdsp.rgtc1u_block;
214 case MKTAG('B', 'C', '4', 'S'):
216 ctx->tex_funct = ctx->texdsp.rgtc1s_block;
218 case MKTAG('A', 'T', 'I', '2'):
219 /* RGT2 variant with swapped R and G (3Dc)*/
221 ctx->tex_funct = ctx->texdsp.dxn3dc_block;
223 case MKTAG('B', 'C', '5', 'U'):
225 ctx->tex_funct = ctx->texdsp.rgtc2u_block;
227 case MKTAG('B', 'C', '5', 'S'):
229 ctx->tex_funct = ctx->texdsp.rgtc2s_block;
231 case MKTAG('U', 'Y', 'V', 'Y'):
233 avctx->pix_fmt = AV_PIX_FMT_UYVY422;
235 case MKTAG('Y', 'U', 'Y', '2'):
237 avctx->pix_fmt = AV_PIX_FMT_YUYV422;
239 case MKTAG('P', '8', ' ', ' '):
240 /* ATI Palette8, same as normal palette */
243 avctx->pix_fmt = AV_PIX_FMT_PAL8;
245 case MKTAG('D', 'X', '1', '0'):
246 /* DirectX 10 extra header */
247 dxgi = bytestream2_get_le32(gbc);
248 bytestream2_skip(gbc, 4); // resourceDimension
249 bytestream2_skip(gbc, 4); // miscFlag
250 array = bytestream2_get_le32(gbc);
251 bytestream2_skip(gbc, 4); // miscFlag2
254 av_log(avctx, AV_LOG_VERBOSE,
255 "Found array of size %d (ignored).\n", array);
257 /* Only BC[1-5] are actually compressed. */
258 ctx->compressed = (dxgi >= 70) && (dxgi <= 84);
260 av_log(avctx, AV_LOG_VERBOSE, "DXGI format %d.\n", dxgi);
263 case DXGI_FORMAT_R16G16B16A16_TYPELESS:
264 case DXGI_FORMAT_R16G16B16A16_FLOAT:
265 case DXGI_FORMAT_R16G16B16A16_UNORM:
266 case DXGI_FORMAT_R16G16B16A16_UINT:
267 case DXGI_FORMAT_R16G16B16A16_SNORM:
268 case DXGI_FORMAT_R16G16B16A16_SINT:
269 avctx->pix_fmt = AV_PIX_FMT_BGRA64;
271 case DXGI_FORMAT_R8G8B8A8_UNORM_SRGB:
272 avctx->colorspace = AVCOL_SPC_RGB;
273 case DXGI_FORMAT_R8G8B8A8_TYPELESS:
274 case DXGI_FORMAT_R8G8B8A8_UNORM:
275 case DXGI_FORMAT_R8G8B8A8_UINT:
276 case DXGI_FORMAT_R8G8B8A8_SNORM:
277 case DXGI_FORMAT_R8G8B8A8_SINT:
278 avctx->pix_fmt = AV_PIX_FMT_BGRA;
280 case DXGI_FORMAT_B8G8R8A8_UNORM_SRGB:
281 avctx->colorspace = AVCOL_SPC_RGB;
282 case DXGI_FORMAT_B8G8R8A8_TYPELESS:
283 case DXGI_FORMAT_B8G8R8A8_UNORM:
284 avctx->pix_fmt = AV_PIX_FMT_RGBA;
286 case DXGI_FORMAT_B8G8R8X8_UNORM_SRGB:
287 avctx->colorspace = AVCOL_SPC_RGB;
288 case DXGI_FORMAT_B8G8R8X8_TYPELESS:
289 case DXGI_FORMAT_B8G8R8X8_UNORM:
290 avctx->pix_fmt = AV_PIX_FMT_RGBA; // opaque
292 case DXGI_FORMAT_B5G6R5_UNORM:
293 avctx->pix_fmt = AV_PIX_FMT_RGB565LE;
296 case DXGI_FORMAT_BC1_UNORM_SRGB:
297 avctx->colorspace = AVCOL_SPC_RGB;
298 case DXGI_FORMAT_BC1_TYPELESS:
299 case DXGI_FORMAT_BC1_UNORM:
301 ctx->tex_funct = ctx->texdsp.dxt1a_block;
303 case DXGI_FORMAT_BC2_UNORM_SRGB:
304 avctx->colorspace = AVCOL_SPC_RGB;
305 case DXGI_FORMAT_BC2_TYPELESS:
306 case DXGI_FORMAT_BC2_UNORM:
308 ctx->tex_funct = ctx->texdsp.dxt3_block;
310 case DXGI_FORMAT_BC3_UNORM_SRGB:
311 avctx->colorspace = AVCOL_SPC_RGB;
312 case DXGI_FORMAT_BC3_TYPELESS:
313 case DXGI_FORMAT_BC3_UNORM:
315 ctx->tex_funct = ctx->texdsp.dxt5_block;
317 case DXGI_FORMAT_BC4_TYPELESS:
318 case DXGI_FORMAT_BC4_UNORM:
320 ctx->tex_funct = ctx->texdsp.rgtc1u_block;
322 case DXGI_FORMAT_BC4_SNORM:
324 ctx->tex_funct = ctx->texdsp.rgtc1s_block;
326 case DXGI_FORMAT_BC5_TYPELESS:
327 case DXGI_FORMAT_BC5_UNORM:
329 ctx->tex_funct = ctx->texdsp.rgtc2u_block;
331 case DXGI_FORMAT_BC5_SNORM:
333 ctx->tex_funct = ctx->texdsp.rgtc2s_block;
336 av_log(avctx, AV_LOG_ERROR,
337 "Unsupported DXGI format %d.\n", dxgi);
338 return AVERROR_INVALIDDATA;
342 av_log(avctx, AV_LOG_ERROR, "Unsupported %s fourcc.\n", buf);
343 return AVERROR_INVALIDDATA;
345 } else if (ctx->paletted) {
347 avctx->pix_fmt = AV_PIX_FMT_PAL8;
349 av_log(avctx, AV_LOG_ERROR, "Unsupported palette bpp %d.\n", bpp);
350 return AVERROR_INVALIDDATA;
354 if (bpp == 8 && r == 0xff && g == 0 && b == 0 && a == 0)
355 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
356 else if (bpp == 8 && r == 0 && g == 0 && b == 0 && a == 0xff)
357 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
359 else if (bpp == 16 && r == 0xff && g == 0 && b == 0 && a == 0xff00)
360 avctx->pix_fmt = AV_PIX_FMT_YA8;
361 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;
365 else if (bpp == 16 && r == 0xffff && g == 0 && b == 0 && a == 0)
366 avctx->pix_fmt = AV_PIX_FMT_GRAY16LE;
367 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
371 else if (bpp == 16 && r == 0xf800 && g == 0x7e0 && b == 0x1f && a == 0)
372 avctx->pix_fmt = AV_PIX_FMT_RGB565LE;
374 else if (bpp == 24 && r == 0xff0000 && g == 0xff00 && b == 0xff && a == 0)
375 avctx->pix_fmt = AV_PIX_FMT_BGR24;
377 else if (bpp == 32 && r == 0xff0000 && g == 0xff00 && b == 0xff && a == 0)
378 avctx->pix_fmt = AV_PIX_FMT_BGR0; // opaque
379 else if (bpp == 32 && r == 0xff && g == 0xff00 && b == 0xff0000 && a == 0)
380 avctx->pix_fmt = AV_PIX_FMT_RGB0; // opaque
381 else if (bpp == 32 && r == 0xff0000 && g == 0xff00 && b == 0xff && a == 0xff000000)
382 avctx->pix_fmt = AV_PIX_FMT_BGRA;
383 else if (bpp == 32 && r == 0xff && g == 0xff00 && b == 0xff0000 && a == 0xff000000)
384 avctx->pix_fmt = AV_PIX_FMT_RGBA;
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;
393 /* Set any remaining post-proc that should happen before frame is ready. */
395 ctx->postproc = DDS_ALPHA_EXP;
397 ctx->postproc = DDS_NORMAL_MAP;
398 else if (ycocg_classic && !ctx->compressed)
399 ctx->postproc = DDS_RAW_YCOCG;
401 /* ATI/NVidia variants sometimes add swizzling in bpp. */
403 case MKTAG('A', '2', 'X', 'Y'):
404 ctx->postproc = DDS_SWIZZLE_A2XY;
406 case MKTAG('x', 'G', 'B', 'R'):
407 ctx->postproc = DDS_SWIZZLE_XGBR;
409 case MKTAG('x', 'R', 'B', 'G'):
410 ctx->postproc = DDS_SWIZZLE_XRBG;
412 case MKTAG('R', 'B', 'x', 'G'):
413 ctx->postproc = DDS_SWIZZLE_RBXG;
415 case MKTAG('R', 'G', 'x', 'B'):
416 ctx->postproc = DDS_SWIZZLE_RGXB;
418 case MKTAG('R', 'x', 'B', 'G'):
419 ctx->postproc = DDS_SWIZZLE_RXBG;
421 case MKTAG('x', 'G', 'x', 'R'):
422 ctx->postproc = DDS_SWIZZLE_XGXR;
424 case MKTAG('A', '2', 'D', '5'):
425 ctx->postproc = DDS_NORMAL_MAP;
432 static int decompress_texture_thread(AVCodecContext *avctx, void *arg,
433 int slice, int thread_nb)
435 DDSContext *ctx = avctx->priv_data;
436 AVFrame *frame = arg;
437 const uint8_t *d = ctx->tex_data;
438 int w_block = avctx->coded_width / TEXTURE_BLOCK_W;
439 int h_block = avctx->coded_height / TEXTURE_BLOCK_H;
441 int start_slice, end_slice;
442 int base_blocks_per_slice = h_block / ctx->slice_count;
443 int remainder_blocks = h_block % ctx->slice_count;
445 /* When the frame height (in blocks) doesn't divide evenly between the
446 * number of slices, spread the remaining blocks evenly between the first
448 start_slice = slice * base_blocks_per_slice;
449 /* Add any extra blocks (one per slice) that have been added before this slice */
450 start_slice += FFMIN(slice, remainder_blocks);
452 end_slice = start_slice + base_blocks_per_slice;
453 /* Add an extra block if there are still remainder blocks to be accounted for */
454 if (slice < remainder_blocks)
457 for (y = start_slice; y < end_slice; y++) {
458 uint8_t *p = frame->data[0] + y * frame->linesize[0] * TEXTURE_BLOCK_H;
459 int off = y * w_block;
460 for (x = 0; x < w_block; x++) {
461 ctx->tex_funct(p + x * 16, frame->linesize[0],
462 d + (off + x) * ctx->tex_ratio);
469 static void do_swizzle(AVFrame *frame, int x, int y)
472 for (i = 0; i < frame->linesize[0] * frame->height; i += 4) {
473 uint8_t *src = frame->data[0] + i;
474 FFSWAP(uint8_t, src[x], src[y]);
478 static void run_postproc(AVCodecContext *avctx, AVFrame *frame)
480 DDSContext *ctx = avctx->priv_data;
483 switch (ctx->postproc) {
485 /* Alpha-exponential mode divides each channel by the maximum
486 * R, G or B value, and stores the multiplying factor in the
488 av_log(avctx, AV_LOG_DEBUG, "Post-processing alpha exponent.\n");
490 for (i = 0; i < frame->linesize[0] * frame->height; i += 4) {
491 uint8_t *src = frame->data[0] + i;
497 src[0] = r * a / 255;
498 src[1] = g * a / 255;
499 src[2] = b * a / 255;
504 /* Normal maps work in the XYZ color space and they encode
505 * X in R or in A, depending on the texture type, Y in G and
506 * derive Z with a square root of the distance.
508 * http://www.realtimecollisiondetection.net/blog/?p=28 */
509 av_log(avctx, AV_LOG_DEBUG, "Post-processing normal map.\n");
511 x_off = ctx->tex_ratio == 8 ? 0 : 3;
512 for (i = 0; i < frame->linesize[0] * frame->height; i += 4) {
513 uint8_t *src = frame->data[0] + i;
518 int d = (255 * 255 - x * x - y * y) / 2;
529 /* Data is Y-Co-Cg-A and not RGBA, but they are represented
530 * with the same masks in the DDPF header. */
531 av_log(avctx, AV_LOG_DEBUG, "Post-processing raw YCoCg.\n");
533 for (i = 0; i < frame->linesize[0] * frame->height; i += 4) {
534 uint8_t *src = frame->data[0] + i;
536 int cg = src[1] - 128;
537 int co = src[2] - 128;
540 src[0] = av_clip_uint8(y + co - cg);
541 src[1] = av_clip_uint8(y + cg);
542 src[2] = av_clip_uint8(y - co - cg);
547 /* Alpha and Luma are stored swapped. */
548 av_log(avctx, AV_LOG_DEBUG, "Post-processing swapped Luma/Alpha.\n");
550 for (i = 0; i < frame->linesize[0] * frame->height; i += 2) {
551 uint8_t *src = frame->data[0] + i;
552 FFSWAP(uint8_t, src[0], src[1]);
555 case DDS_SWIZZLE_A2XY:
556 /* Swap R and G, often used to restore a standard RGTC2. */
557 av_log(avctx, AV_LOG_DEBUG, "Post-processing A2XY swizzle.\n");
558 do_swizzle(frame, 0, 1);
560 case DDS_SWIZZLE_RBXG:
561 /* Swap G and A, then B and new A (G). */
562 av_log(avctx, AV_LOG_DEBUG, "Post-processing RBXG swizzle.\n");
563 do_swizzle(frame, 1, 3);
564 do_swizzle(frame, 2, 3);
566 case DDS_SWIZZLE_RGXB:
568 av_log(avctx, AV_LOG_DEBUG, "Post-processing RGXB swizzle.\n");
569 do_swizzle(frame, 2, 3);
571 case DDS_SWIZZLE_RXBG:
573 av_log(avctx, AV_LOG_DEBUG, "Post-processing RXBG swizzle.\n");
574 do_swizzle(frame, 1, 3);
576 case DDS_SWIZZLE_RXGB:
577 /* Swap R and A (misleading name). */
578 av_log(avctx, AV_LOG_DEBUG, "Post-processing RXGB swizzle.\n");
579 do_swizzle(frame, 0, 3);
581 case DDS_SWIZZLE_XGBR:
582 /* Swap B and A, then R and new A (B). */
583 av_log(avctx, AV_LOG_DEBUG, "Post-processing XGBR swizzle.\n");
584 do_swizzle(frame, 2, 3);
585 do_swizzle(frame, 0, 3);
587 case DDS_SWIZZLE_XGXR:
588 /* Swap G and A, then R and new A (G), then new R (G) and new G (A).
589 * This variant does not store any B component. */
590 av_log(avctx, AV_LOG_DEBUG, "Post-processing XGXR swizzle.\n");
591 do_swizzle(frame, 1, 3);
592 do_swizzle(frame, 0, 3);
593 do_swizzle(frame, 0, 1);
595 case DDS_SWIZZLE_XRBG:
596 /* Swap G and A, then R and new A (G). */
597 av_log(avctx, AV_LOG_DEBUG, "Post-processing XRBG swizzle.\n");
598 do_swizzle(frame, 1, 3);
599 do_swizzle(frame, 0, 3);
604 static int dds_decode(AVCodecContext *avctx, void *data,
605 int *got_frame, AVPacket *avpkt)
607 DDSContext *ctx = avctx->priv_data;
608 GetByteContext *gbc = &ctx->gbc;
609 AVFrame *frame = data;
613 ff_texturedsp_init(&ctx->texdsp);
614 bytestream2_init(gbc, avpkt->data, avpkt->size);
616 if (bytestream2_get_bytes_left(gbc) < 128) {
617 av_log(avctx, AV_LOG_ERROR, "Frame is too small (%d).\n",
618 bytestream2_get_bytes_left(gbc));
619 return AVERROR_INVALIDDATA;
622 if (bytestream2_get_le32(gbc) != MKTAG('D', 'D', 'S', ' ') ||
623 bytestream2_get_le32(gbc) != 124) { // header size
624 av_log(avctx, AV_LOG_ERROR, "Invalid DDS header.\n");
625 return AVERROR_INVALIDDATA;
628 bytestream2_skip(gbc, 4); // flags
630 avctx->height = bytestream2_get_le32(gbc);
631 avctx->width = bytestream2_get_le32(gbc);
632 ret = av_image_check_size(avctx->width, avctx->height, 0, avctx);
634 av_log(avctx, AV_LOG_ERROR, "Invalid image size %dx%d.\n",
635 avctx->width, avctx->height);
639 /* Since codec is based on 4x4 blocks, size is aligned to 4. */
640 avctx->coded_width = FFALIGN(avctx->width, TEXTURE_BLOCK_W);
641 avctx->coded_height = FFALIGN(avctx->height, TEXTURE_BLOCK_H);
643 bytestream2_skip(gbc, 4); // pitch
644 bytestream2_skip(gbc, 4); // depth
645 mipmap = bytestream2_get_le32(gbc);
647 av_log(avctx, AV_LOG_VERBOSE, "Found %d mipmaps (ignored).\n", mipmap);
649 /* Extract pixel format information, considering additional elements
650 * in reserved1 and reserved2. */
651 ret = parse_pixel_format(avctx);
655 ret = ff_get_buffer(avctx, frame, 0);
659 if (ctx->compressed) {
660 int size = (avctx->coded_height / TEXTURE_BLOCK_H) *
661 (avctx->coded_width / TEXTURE_BLOCK_W) * ctx->tex_ratio;
662 ctx->slice_count = av_clip(avctx->thread_count, 1,
663 avctx->coded_height / TEXTURE_BLOCK_H);
665 if (bytestream2_get_bytes_left(gbc) < size) {
666 av_log(avctx, AV_LOG_ERROR,
667 "Compressed Buffer is too small (%d < %d).\n",
668 bytestream2_get_bytes_left(gbc), size);
669 return AVERROR_INVALIDDATA;
672 /* Use the decompress function on the texture, one block per thread. */
673 ctx->tex_data = gbc->buffer;
674 avctx->execute2(avctx, decompress_texture_thread, frame, NULL, ctx->slice_count);
676 int linesize = av_image_get_linesize(avctx->pix_fmt, frame->width, 0);
680 /* Use the first 1024 bytes as palette, then copy the rest. */
681 bytestream2_get_buffer(gbc, frame->data[1], 256 * 4);
682 for (i = 0; i < 256; i++)
683 AV_WN32(frame->data[1] + i*4,
684 (frame->data[1][2+i*4]<<0)+
685 (frame->data[1][1+i*4]<<8)+
686 (frame->data[1][0+i*4]<<16)+
687 (frame->data[1][3+i*4]<<24)
690 frame->palette_has_changed = 1;
693 if (bytestream2_get_bytes_left(gbc) < frame->height * linesize) {
694 av_log(avctx, AV_LOG_ERROR, "Buffer is too small (%d < %d).\n",
695 bytestream2_get_bytes_left(gbc), frame->height * linesize);
696 return AVERROR_INVALIDDATA;
699 av_image_copy_plane(frame->data[0], frame->linesize[0],
700 gbc->buffer, linesize,
701 linesize, frame->height);
704 /* Run any post processing here if needed. */
705 if (ctx->postproc != DDS_NONE)
706 run_postproc(avctx, frame);
708 /* Frame is ready to be output. */
709 frame->pict_type = AV_PICTURE_TYPE_I;
710 frame->key_frame = 1;
716 AVCodec ff_dds_decoder = {
718 .long_name = NULL_IF_CONFIG_SMALL("DirectDraw Surface image decoder"),
719 .type = AVMEDIA_TYPE_VIDEO,
720 .id = AV_CODEC_ID_DDS,
721 .decode = dds_decode,
722 .priv_data_size = sizeof(DDSContext),
723 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_SLICE_THREADS,
724 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE