Vittorio Giovara | 5c018ee | 2015-05-26 23:30:51 | [diff] [blame] | 1 | /* |
| 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 | |
| 43 | enum 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, |
| 57 | } DDSPostProc; |
| 58 | |
| 59 | enum 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, |
| 96 | } DDSDXGIFormat; |
| 97 | |
| 98 | typedef 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 |
| 108 | |
| 109 | /* Pointer to the selected compress or decompress function. */ |
| 110 | int (*tex_funct)(uint8_t *dst, ptrdiff_t stride, const uint8_t *block); |
| 111 | } DDSContext; |
| 112 | |
| 113 | static int parse_pixel_format(AVCodecContext *avctx) |
| 114 | { |
| 115 | DDSContext *ctx = avctx->priv_data; |
| 116 | GetByteContext *gbc = &ctx->gbc; |
| 117 | char buf[32]; |
| 118 | uint32_t flags, fourcc, gimp_tag; |
| 119 | enum DDSDXGIFormat dxgi; |
| 120 | int size, bpp, r, g, b, a; |
| 121 | int alpha_exponent, ycocg_classic, ycocg_scaled, normal_map, array; |
| 122 | |
| 123 | /* Alternative DDS implementations use reserved1 as custom header. */ |
| 124 | bytestream2_skip(gbc, 4 * 3); |
| 125 | gimp_tag = bytestream2_get_le32(gbc); |
| 126 | alpha_exponent = gimp_tag == MKTAG('A', 'E', 'X', 'P'); |
| 127 | ycocg_classic = gimp_tag == MKTAG('Y', 'C', 'G', '1'); |
| 128 | ycocg_scaled = gimp_tag == MKTAG('Y', 'C', 'G', '2'); |
| 129 | bytestream2_skip(gbc, 4 * 7); |
| 130 | |
| 131 | /* Now the real DDPF starts. */ |
| 132 | size = bytestream2_get_le32(gbc); |
| 133 | if (size != 32) { |
| 134 | av_log(avctx, AV_LOG_ERROR, "Invalid pixel format header %d.\n", size); |
| 135 | return AVERROR_INVALIDDATA; |
| 136 | } |
| 137 | flags = bytestream2_get_le32(gbc); |
| 138 | ctx->compressed = flags & DDPF_FOURCC; |
| 139 | ctx->paletted = flags & DDPF_PALETTE; |
| 140 | normal_map = flags & DDPF_NORMALMAP; |
| 141 | fourcc = bytestream2_get_le32(gbc); |
| 142 | |
| 143 | bpp = bytestream2_get_le32(gbc); // rgbbitcount |
| 144 | r = bytestream2_get_le32(gbc); // rbitmask |
| 145 | g = bytestream2_get_le32(gbc); // gbitmask |
| 146 | b = bytestream2_get_le32(gbc); // bbitmask |
| 147 | a = bytestream2_get_le32(gbc); // abitmask |
| 148 | |
| 149 | bytestream2_skip(gbc, 4); // caps |
| 150 | bytestream2_skip(gbc, 4); // caps2 |
| 151 | bytestream2_skip(gbc, 4); // caps3 |
| 152 | bytestream2_skip(gbc, 4); // caps4 |
| 153 | bytestream2_skip(gbc, 4); // reserved2 |
| 154 | |
| 155 | av_get_codec_tag_string(buf, sizeof(buf), fourcc); |
| 156 | av_log(avctx, AV_LOG_VERBOSE, "fourcc %s bpp %d " |
| 157 | "r 0x%x g 0x%x b 0x%x a 0x%x\n", buf, bpp, r, g, b, a); |
| 158 | if (gimp_tag) { |
| 159 | av_get_codec_tag_string(buf, sizeof(buf), gimp_tag); |
| 160 | av_log(avctx, AV_LOG_VERBOSE, "and GIMP-DDS tag %s\n", buf); |
| 161 | } |
| 162 | |
| 163 | if (ctx->compressed) |
| 164 | avctx->pix_fmt = AV_PIX_FMT_RGBA; |
| 165 | |
| 166 | if (ctx->compressed) { |
| 167 | switch (fourcc) { |
| 168 | case MKTAG('D', 'X', 'T', '1'): |
| 169 | ctx->tex_ratio = 8; |
| 170 | ctx->tex_funct = ctx->texdsp.dxt1a_block; |
| 171 | break; |
| 172 | case MKTAG('D', 'X', 'T', '2'): |
| 173 | ctx->tex_ratio = 16; |
| 174 | ctx->tex_funct = ctx->texdsp.dxt2_block; |
| 175 | break; |
| 176 | case MKTAG('D', 'X', 'T', '3'): |
| 177 | ctx->tex_ratio = 16; |
| 178 | ctx->tex_funct = ctx->texdsp.dxt3_block; |
| 179 | break; |
| 180 | case MKTAG('D', 'X', 'T', '4'): |
| 181 | ctx->tex_ratio = 16; |
| 182 | ctx->tex_funct = ctx->texdsp.dxt4_block; |
| 183 | break; |
| 184 | case MKTAG('D', 'X', 'T', '5'): |
| 185 | ctx->tex_ratio = 16; |
| 186 | if (ycocg_scaled) |
| 187 | ctx->tex_funct = ctx->texdsp.dxt5ys_block; |
| 188 | else if (ycocg_classic) |
| 189 | ctx->tex_funct = ctx->texdsp.dxt5y_block; |
| 190 | else |
| 191 | ctx->tex_funct = ctx->texdsp.dxt5_block; |
| 192 | break; |
| 193 | case MKTAG('R', 'X', 'G', 'B'): |
| 194 | ctx->tex_ratio = 16; |
| 195 | ctx->tex_funct = ctx->texdsp.dxt5_block; |
| 196 | /* This format may be considered as a normal map, |
| 197 | * but it is handled differently in a separate postproc. */ |
| 198 | ctx->postproc = DDS_SWIZZLE_RXGB; |
| 199 | normal_map = 0; |
| 200 | break; |
| 201 | case MKTAG('A', 'T', 'I', '1'): |
| 202 | case MKTAG('B', 'C', '4', 'U'): |
| 203 | ctx->tex_ratio = 8; |
| 204 | ctx->tex_funct = ctx->texdsp.rgtc1u_block; |
| 205 | break; |
| 206 | case MKTAG('B', 'C', '4', 'S'): |
| 207 | ctx->tex_ratio = 8; |
| 208 | ctx->tex_funct = ctx->texdsp.rgtc1s_block; |
| 209 | break; |
| 210 | case MKTAG('A', 'T', 'I', '2'): |
| 211 | /* RGT2 variant with swapped R and G (3Dc)*/ |
| 212 | ctx->tex_ratio = 16; |
| 213 | ctx->tex_funct = ctx->texdsp.dxn3dc_block; |
| 214 | break; |
| 215 | case MKTAG('B', 'C', '5', 'U'): |
| 216 | ctx->tex_ratio = 16; |
| 217 | ctx->tex_funct = ctx->texdsp.rgtc2u_block; |
| 218 | break; |
| 219 | case MKTAG('B', 'C', '5', 'S'): |
| 220 | ctx->tex_ratio = 16; |
| 221 | ctx->tex_funct = ctx->texdsp.rgtc2s_block; |
| 222 | break; |
| 223 | case MKTAG('U', 'Y', 'V', 'Y'): |
| 224 | ctx->compressed = 0; |
| 225 | avctx->pix_fmt = AV_PIX_FMT_UYVY422; |
| 226 | break; |
| 227 | case MKTAG('Y', 'U', 'Y', '2'): |
| 228 | ctx->compressed = 0; |
| 229 | avctx->pix_fmt = AV_PIX_FMT_YUYV422; |
| 230 | break; |
| 231 | case MKTAG('P', '8', ' ', ' '): |
| 232 | /* ATI Palette8, same as normal palette */ |
| 233 | ctx->compressed = 0; |
| 234 | ctx->paletted = 1; |
| 235 | avctx->pix_fmt = AV_PIX_FMT_PAL8; |
| 236 | break; |
| 237 | case MKTAG('D', 'X', '1', '0'): |
| 238 | /* DirectX 10 extra header */ |
| 239 | dxgi = bytestream2_get_le32(gbc); |
| 240 | bytestream2_skip(gbc, 4); // resourceDimension |
| 241 | bytestream2_skip(gbc, 4); // miscFlag |
| 242 | array = bytestream2_get_le32(gbc); |
| 243 | bytestream2_skip(gbc, 4); // miscFlag2 |
| 244 | |
| 245 | if (array != 0) |
| 246 | av_log(avctx, AV_LOG_VERBOSE, |
| 247 | "Found array of size %d (ignored).\n", array); |
| 248 | |
| 249 | /* Only BC[1-5] are actually compressed. */ |
| 250 | ctx->compressed = (dxgi >= 70) && (dxgi <= 84); |
| 251 | |
| 252 | av_log(avctx, AV_LOG_VERBOSE, "DXGI format %d.\n", dxgi); |
| 253 | switch (dxgi) { |
| 254 | /* RGB types. */ |
| 255 | case DXGI_FORMAT_R16G16B16A16_TYPELESS: |
| 256 | case DXGI_FORMAT_R16G16B16A16_FLOAT: |
| 257 | case DXGI_FORMAT_R16G16B16A16_UNORM: |
| 258 | case DXGI_FORMAT_R16G16B16A16_UINT: |
| 259 | case DXGI_FORMAT_R16G16B16A16_SNORM: |
| 260 | case DXGI_FORMAT_R16G16B16A16_SINT: |
| 261 | avctx->pix_fmt = AV_PIX_FMT_BGRA64; |
| 262 | break; |
| 263 | case DXGI_FORMAT_R8G8B8A8_UNORM_SRGB: |
| 264 | avctx->colorspace = AVCOL_SPC_RGB; |
| 265 | case DXGI_FORMAT_R8G8B8A8_TYPELESS: |
| 266 | case DXGI_FORMAT_R8G8B8A8_UNORM: |
| 267 | case DXGI_FORMAT_R8G8B8A8_UINT: |
| 268 | case DXGI_FORMAT_R8G8B8A8_SNORM: |
| 269 | case DXGI_FORMAT_R8G8B8A8_SINT: |
| 270 | avctx->pix_fmt = AV_PIX_FMT_BGRA; |
| 271 | break; |
| 272 | case DXGI_FORMAT_B8G8R8A8_UNORM_SRGB: |
| 273 | avctx->colorspace = AVCOL_SPC_RGB; |
| 274 | case DXGI_FORMAT_B8G8R8A8_TYPELESS: |
| 275 | case DXGI_FORMAT_B8G8R8A8_UNORM: |
| 276 | avctx->pix_fmt = AV_PIX_FMT_RGBA; |
| 277 | break; |
| 278 | case DXGI_FORMAT_B8G8R8X8_UNORM_SRGB: |
| 279 | avctx->colorspace = AVCOL_SPC_RGB; |
| 280 | case DXGI_FORMAT_B8G8R8X8_TYPELESS: |
| 281 | case DXGI_FORMAT_B8G8R8X8_UNORM: |
| 282 | avctx->pix_fmt = AV_PIX_FMT_RGBA; // opaque |
| 283 | break; |
| 284 | case DXGI_FORMAT_B5G6R5_UNORM: |
| 285 | avctx->pix_fmt = AV_PIX_FMT_RGB565LE; |
| 286 | break; |
| 287 | /* Texture types. */ |
| 288 | case DXGI_FORMAT_BC1_UNORM_SRGB: |
| 289 | avctx->colorspace = AVCOL_SPC_RGB; |
| 290 | case DXGI_FORMAT_BC1_TYPELESS: |
| 291 | case DXGI_FORMAT_BC1_UNORM: |
| 292 | ctx->tex_ratio = 8; |
| 293 | ctx->tex_funct = ctx->texdsp.dxt1a_block; |
| 294 | break; |
| 295 | case DXGI_FORMAT_BC2_UNORM_SRGB: |
| 296 | avctx->colorspace = AVCOL_SPC_RGB; |
| 297 | case DXGI_FORMAT_BC2_TYPELESS: |
| 298 | case DXGI_FORMAT_BC2_UNORM: |
| 299 | ctx->tex_ratio = 16; |
| 300 | ctx->tex_funct = ctx->texdsp.dxt3_block; |
| 301 | break; |
| 302 | case DXGI_FORMAT_BC3_UNORM_SRGB: |
| 303 | avctx->colorspace = AVCOL_SPC_RGB; |
| 304 | case DXGI_FORMAT_BC3_TYPELESS: |
| 305 | case DXGI_FORMAT_BC3_UNORM: |
| 306 | ctx->tex_ratio = 16; |
| 307 | ctx->tex_funct = ctx->texdsp.dxt5_block; |
| 308 | break; |
| 309 | case DXGI_FORMAT_BC4_TYPELESS: |
| 310 | case DXGI_FORMAT_BC4_UNORM: |
| 311 | ctx->tex_ratio = 8; |
| 312 | ctx->tex_funct = ctx->texdsp.rgtc1u_block; |
| 313 | break; |
| 314 | case DXGI_FORMAT_BC4_SNORM: |
| 315 | ctx->tex_ratio = 8; |
| 316 | ctx->tex_funct = ctx->texdsp.rgtc1s_block; |
| 317 | break; |
| 318 | case DXGI_FORMAT_BC5_TYPELESS: |
| 319 | case DXGI_FORMAT_BC5_UNORM: |
| 320 | ctx->tex_ratio = 16; |
| 321 | ctx->tex_funct = ctx->texdsp.rgtc2u_block; |
| 322 | break; |
| 323 | case DXGI_FORMAT_BC5_SNORM: |
| 324 | ctx->tex_ratio = 16; |
| 325 | ctx->tex_funct = ctx->texdsp.rgtc2s_block; |
| 326 | break; |
| 327 | default: |
| 328 | av_log(avctx, AV_LOG_ERROR, |
| 329 | "Unsupported DXGI format %d.\n", dxgi); |
| 330 | return AVERROR_INVALIDDATA; |
| 331 | } |
| 332 | break; |
| 333 | default: |
| 334 | av_log(avctx, AV_LOG_ERROR, "Unsupported %s fourcc.\n", buf); |
| 335 | return AVERROR_INVALIDDATA; |
| 336 | } |
| 337 | } else if (ctx->paletted) { |
| 338 | if (bpp == 8) { |
| 339 | avctx->pix_fmt = AV_PIX_FMT_PAL8; |
| 340 | } else { |
| 341 | av_log(avctx, AV_LOG_ERROR, "Unsupported palette bpp %d.\n", bpp); |
| 342 | return AVERROR_INVALIDDATA; |
| 343 | } |
| 344 | } else { |
| 345 | /* 8 bpp */ |
| 346 | if (bpp == 8 && r == 0xff && g == 0 && b == 0 && a == 0) |
| 347 | avctx->pix_fmt = AV_PIX_FMT_GRAY8; |
| 348 | /* 16 bpp */ |
| 349 | else if (bpp == 16 && r == 0xff && g == 0 && b == 0 && a == 0xff00) |
| 350 | avctx->pix_fmt = AV_PIX_FMT_YA8; |
| 351 | else if (bpp == 16 && r == 0xffff && g == 0 && b == 0 && a == 0) |
| 352 | avctx->pix_fmt = AV_PIX_FMT_GRAY16LE; |
| 353 | else if (bpp == 16 && r == 0xf800 && g == 0x7e0 && b == 0x1f && a == 0) |
| 354 | avctx->pix_fmt = AV_PIX_FMT_RGB565LE; |
| 355 | /* 24 bpp */ |
| 356 | else if (bpp == 24 && r == 0xff0000 && g == 0xff00 && b == 0xff && a == 0) |
| 357 | avctx->pix_fmt = AV_PIX_FMT_BGR24; |
| 358 | /* 32 bpp */ |
| 359 | else if (bpp == 32 && r == 0xff0000 && g == 0xff00 && b == 0xff && a == 0) |
| 360 | avctx->pix_fmt = AV_PIX_FMT_RGBA; // opaque |
| 361 | else if (bpp == 32 && r == 0xff && g == 0xff00 && b == 0xff0000 && a == 0) |
| 362 | avctx->pix_fmt = AV_PIX_FMT_BGRA; // opaque |
| 363 | else if (bpp == 32 && r == 0xff0000 && g == 0xff00 && b == 0xff && a == 0xff000000) |
| 364 | avctx->pix_fmt = AV_PIX_FMT_RGBA; |
| 365 | else if (bpp == 32 && r == 0xff && g == 0xff00 && b == 0xff0000 && a == 0xff000000) |
| 366 | avctx->pix_fmt = AV_PIX_FMT_BGRA; |
| 367 | /* give up */ |
| 368 | else { |
| 369 | av_log(avctx, AV_LOG_ERROR, "Unknown pixel format " |
| 370 | "[bpp %d r 0x%x g 0x%x b 0x%x a 0x%x].\n", bpp, r, g, b, a); |
| 371 | return AVERROR_INVALIDDATA; |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | /* Set any remaining post-proc that should happen before frame is ready. */ |
| 376 | if (alpha_exponent) |
| 377 | ctx->postproc = DDS_ALPHA_EXP; |
| 378 | else if (normal_map) |
| 379 | ctx->postproc = DDS_NORMAL_MAP; |
| 380 | else if (ycocg_classic && !ctx->compressed) |
| 381 | ctx->postproc = DDS_RAW_YCOCG; |
| 382 | else if (avctx->pix_fmt == AV_PIX_FMT_YA8) |
| 383 | ctx->postproc = DDS_SWAP_ALPHA; |
| 384 | |
| 385 | /* ATI/NVidia variants sometimes add swizzling in bpp. */ |
| 386 | switch (bpp) { |
| 387 | case MKTAG('A', '2', 'X', 'Y'): |
| 388 | ctx->postproc = DDS_SWIZZLE_A2XY; |
| 389 | break; |
| 390 | case MKTAG('x', 'G', 'B', 'R'): |
| 391 | ctx->postproc = DDS_SWIZZLE_XGBR; |
| 392 | break; |
| 393 | case MKTAG('x', 'R', 'B', 'G'): |
| 394 | ctx->postproc = DDS_SWIZZLE_XRBG; |
| 395 | break; |
| 396 | case MKTAG('R', 'B', 'x', 'G'): |
| 397 | ctx->postproc = DDS_SWIZZLE_RBXG; |
| 398 | break; |
| 399 | case MKTAG('R', 'G', 'x', 'B'): |
| 400 | ctx->postproc = DDS_SWIZZLE_RGXB; |
| 401 | break; |
| 402 | case MKTAG('R', 'x', 'B', 'G'): |
| 403 | ctx->postproc = DDS_SWIZZLE_RXBG; |
| 404 | break; |
| 405 | case MKTAG('x', 'G', 'x', 'R'): |
| 406 | ctx->postproc = DDS_SWIZZLE_XGXR; |
| 407 | break; |
| 408 | case MKTAG('A', '2', 'D', '5'): |
| 409 | ctx->postproc = DDS_NORMAL_MAP; |
| 410 | break; |
| 411 | } |
| 412 | |
| 413 | return 0; |
| 414 | } |
| 415 | |
| 416 | static int decompress_texture_thread(AVCodecContext *avctx, void *arg, |
| 417 | int block_nb, int thread_nb) |
| 418 | { |
| 419 | DDSContext *ctx = avctx->priv_data; |
| 420 | AVFrame *frame = arg; |
| 421 | int x = (TEXTURE_BLOCK_W * block_nb) % avctx->coded_width; |
| 422 | int y = TEXTURE_BLOCK_H * (TEXTURE_BLOCK_W * block_nb / avctx->coded_width); |
| 423 | uint8_t *p = frame->data[0] + x * 4 + y * frame->linesize[0]; |
| 424 | const uint8_t *d = ctx->tex_data + block_nb * ctx->tex_ratio; |
| 425 | |
| 426 | ctx->tex_funct(p, frame->linesize[0], d); |
| 427 | return 0; |
| 428 | } |
| 429 | |
| 430 | static void do_swizzle(AVFrame *frame, int x, int y) |
| 431 | { |
| 432 | int i; |
| 433 | for (i = 0; i < frame->linesize[0] * frame->height; i += 4) { |
| 434 | uint8_t *src = frame->data[0] + i; |
| 435 | FFSWAP(uint8_t, src[x], src[y]); |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | static void run_postproc(AVCodecContext *avctx, AVFrame *frame) |
| 440 | { |
| 441 | DDSContext *ctx = avctx->priv_data; |
| 442 | int i, x_off; |
| 443 | |
| 444 | switch (ctx->postproc) { |
| 445 | case DDS_ALPHA_EXP: |
| 446 | /* Alpha-exponential mode divides each channel by the maximum |
| 447 | * R, G or B value, and stores the multiplying factor in the |
| 448 | * alpha channel. */ |
| 449 | av_log(avctx, AV_LOG_DEBUG, "Post-processing alpha exponent.\n"); |
| 450 | |
| 451 | for (i = 0; i < frame->linesize[0] * frame->height; i += 4) { |
| 452 | uint8_t *src = frame->data[0] + i; |
| 453 | int r = src[0]; |
| 454 | int g = src[1]; |
| 455 | int b = src[2]; |
| 456 | int a = src[3]; |
| 457 | |
| 458 | src[0] = r * a / 255; |
| 459 | src[1] = g * a / 255; |
| 460 | src[2] = b * a / 255; |
| 461 | src[3] = 255; |
| 462 | } |
| 463 | break; |
| 464 | case DDS_NORMAL_MAP: |
| 465 | /* Normal maps work in the XYZ color space and they encode |
| 466 | * X in R or in A, depending on the texture type, Y in G and |
| 467 | * derive Z with a square root of the distance. |
| 468 | * |
| 469 | * http://www.realtimecollisiondetection.net/blog/?p=28 */ |
| 470 | av_log(avctx, AV_LOG_DEBUG, "Post-processing normal map.\n"); |
| 471 | |
| 472 | x_off = ctx->tex_ratio == 8 ? 0 : 3; |
| 473 | for (i = 0; i < frame->linesize[0] * frame->height; i += 4) { |
| 474 | uint8_t *src = frame->data[0] + i; |
| 475 | int x = src[x_off]; |
| 476 | int y = src[1]; |
| 477 | int z = 127; |
| 478 | |
| 479 | int d = (255 * 255 - x * x - y * y) / 2; |
| 480 | if (d > 0) |
| 481 | z = rint(sqrtf(d)); |
| 482 | |
| 483 | src[0] = x; |
| 484 | src[1] = y; |
| 485 | src[2] = z; |
| 486 | src[3] = 255; |
| 487 | } |
| 488 | break; |
| 489 | case DDS_RAW_YCOCG: |
| 490 | /* Data is Y-Co-Cg-A and not RGBA, but they are represented |
| 491 | * with the same masks in the DDPF header. */ |
| 492 | av_log(avctx, AV_LOG_DEBUG, "Post-processing raw YCoCg.\n"); |
| 493 | |
| 494 | for (i = 0; i < frame->linesize[0] * frame->height; i += 4) { |
| 495 | uint8_t *src = frame->data[0] + i; |
| 496 | int a = src[0]; |
| 497 | int cg = src[1] - 128; |
| 498 | int co = src[2] - 128; |
| 499 | int y = src[3]; |
| 500 | |
| 501 | src[0] = av_clip_uint8(y + co - cg); |
| 502 | src[1] = av_clip_uint8(y + cg); |
| 503 | src[2] = av_clip_uint8(y - co - cg); |
| 504 | src[3] = a; |
| 505 | } |
| 506 | break; |
| 507 | case DDS_SWAP_ALPHA: |
| 508 | /* Alpha and Luma are stored swapped. */ |
| 509 | av_log(avctx, AV_LOG_DEBUG, "Post-processing swapped Luma/Alpha.\n"); |
| 510 | |
| 511 | for (i = 0; i < frame->linesize[0] * frame->height; i += 2) { |
| 512 | uint8_t *src = frame->data[0] + i; |
| 513 | FFSWAP(uint8_t, src[0], src[1]); |
| 514 | } |
| 515 | break; |
| 516 | case DDS_SWIZZLE_A2XY: |
| 517 | /* Swap R and G, often used to restore a standard RGTC2. */ |
| 518 | av_log(avctx, AV_LOG_DEBUG, "Post-processing A2XY swizzle.\n"); |
| 519 | do_swizzle(frame, 0, 1); |
| 520 | break; |
| 521 | case DDS_SWIZZLE_RBXG: |
| 522 | /* Swap G and A, then B and new A (G). */ |
| 523 | av_log(avctx, AV_LOG_DEBUG, "Post-processing RBXG swizzle.\n"); |
| 524 | do_swizzle(frame, 1, 3); |
| 525 | do_swizzle(frame, 2, 3); |
| 526 | break; |
| 527 | case DDS_SWIZZLE_RGXB: |
| 528 | /* Swap B and A. */ |
| 529 | av_log(avctx, AV_LOG_DEBUG, "Post-processing RGXB swizzle.\n"); |
| 530 | do_swizzle(frame, 2, 3); |
| 531 | break; |
| 532 | case DDS_SWIZZLE_RXBG: |
| 533 | /* Swap G and A. */ |
| 534 | av_log(avctx, AV_LOG_DEBUG, "Post-processing RXBG swizzle.\n"); |
| 535 | do_swizzle(frame, 1, 3); |
| 536 | break; |
| 537 | case DDS_SWIZZLE_RXGB: |
| 538 | /* Swap R and A (misleading name). */ |
| 539 | av_log(avctx, AV_LOG_DEBUG, "Post-processing RXGB swizzle.\n"); |
| 540 | do_swizzle(frame, 0, 3); |
| 541 | break; |
| 542 | case DDS_SWIZZLE_XGBR: |
| 543 | /* Swap B and A, then R and new A (B). */ |
| 544 | av_log(avctx, AV_LOG_DEBUG, "Post-processing XGBR swizzle.\n"); |
| 545 | do_swizzle(frame, 2, 3); |
| 546 | do_swizzle(frame, 0, 3); |
| 547 | break; |
| 548 | case DDS_SWIZZLE_XGXR: |
| 549 | /* Swap G and A, then R and new A (G), then new R (G) and new G (A). |
| 550 | * This variant does not store any B component. */ |
| 551 | av_log(avctx, AV_LOG_DEBUG, "Post-processing XGXR swizzle.\n"); |
| 552 | do_swizzle(frame, 1, 3); |
| 553 | do_swizzle(frame, 0, 3); |
| 554 | do_swizzle(frame, 0, 1); |
| 555 | break; |
| 556 | case DDS_SWIZZLE_XRBG: |
| 557 | /* Swap G and A, then R and new A (G). */ |
| 558 | av_log(avctx, AV_LOG_DEBUG, "Post-processing XRBG swizzle.\n"); |
| 559 | do_swizzle(frame, 1, 3); |
| 560 | do_swizzle(frame, 0, 3); |
| 561 | break; |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | static int dds_decode(AVCodecContext *avctx, void *data, |
| 566 | int *got_frame, AVPacket *avpkt) |
| 567 | { |
| 568 | DDSContext *ctx = avctx->priv_data; |
| 569 | GetByteContext *gbc = &ctx->gbc; |
| 570 | AVFrame *frame = data; |
| 571 | int blocks, mipmap; |
| 572 | int ret; |
| 573 | |
| 574 | ff_texturedsp_init(&ctx->texdsp); |
| 575 | bytestream2_init(gbc, avpkt->data, avpkt->size); |
| 576 | |
| 577 | if (bytestream2_get_bytes_left(gbc) < 128) { |
| 578 | av_log(avctx, AV_LOG_ERROR, "Frame is too small (%d).", |
| 579 | bytestream2_get_bytes_left(gbc)); |
| 580 | return AVERROR_INVALIDDATA; |
| 581 | } |
| 582 | |
| 583 | if (bytestream2_get_le32(gbc) != MKTAG('D', 'D', 'S', ' ') || |
| 584 | bytestream2_get_le32(gbc) != 124) { // header size |
| 585 | av_log(avctx, AV_LOG_ERROR, "Invalid DDS header."); |
| 586 | return AVERROR_INVALIDDATA; |
| 587 | } |
| 588 | |
| 589 | bytestream2_skip(gbc, 4); // flags |
| 590 | |
| 591 | avctx->height = bytestream2_get_le32(gbc); |
| 592 | avctx->width = bytestream2_get_le32(gbc); |
| 593 | ret = av_image_check_size(avctx->width, avctx->height, 0, avctx); |
| 594 | if (ret < 0) { |
| 595 | av_log(avctx, AV_LOG_ERROR, "Invalid image size %dx%d.\n", |
| 596 | avctx->width, avctx->height); |
| 597 | return ret; |
| 598 | } |
| 599 | |
| 600 | /* Since codec is based on 4x4 blocks, size is aligned to 4. */ |
| 601 | avctx->coded_width = FFALIGN(avctx->width, TEXTURE_BLOCK_W); |
| 602 | avctx->coded_height = FFALIGN(avctx->height, TEXTURE_BLOCK_H); |
| 603 | |
| 604 | bytestream2_skip(gbc, 4); // pitch |
| 605 | bytestream2_skip(gbc, 4); // depth |
| 606 | mipmap = bytestream2_get_le32(gbc); |
| 607 | if (mipmap != 0) |
| 608 | av_log(avctx, AV_LOG_VERBOSE, "Found %d mipmaps (ignored).\n", mipmap); |
| 609 | |
| 610 | /* Extract pixel format information, considering additional elements |
| 611 | * in reserved1 and reserved2. */ |
| 612 | ret = parse_pixel_format(avctx); |
| 613 | if (ret < 0) |
| 614 | return ret; |
| 615 | |
| 616 | ret = ff_get_buffer(avctx, frame, 0); |
| 617 | if (ret < 0) |
| 618 | return ret; |
| 619 | |
| 620 | if (ctx->compressed) { |
| 621 | /* Use the decompress function on the texture, one block per thread. */ |
| 622 | ctx->tex_data = gbc->buffer; |
| 623 | blocks = avctx->coded_width * avctx->coded_height / (TEXTURE_BLOCK_W * TEXTURE_BLOCK_H); |
| 624 | avctx->execute2(avctx, decompress_texture_thread, frame, NULL, blocks); |
| 625 | } else { |
| 626 | int linesize = av_image_get_linesize(avctx->pix_fmt, frame->width, 0); |
| 627 | |
| 628 | if (ctx->paletted) { |
Luca Barbato | 4d55484 | 2015-06-23 12:48:10 | [diff] [blame^] | 629 | int i; |
| 630 | uint32_t *p = (uint32_t *)frame->data[1]; |
| 631 | |
Vittorio Giovara | 5c018ee | 2015-05-26 23:30:51 | [diff] [blame] | 632 | /* Use the first 1024 bytes as palette, then copy the rest. */ |
Luca Barbato | 4d55484 | 2015-06-23 12:48:10 | [diff] [blame^] | 633 | for (i = 0; i < 256; i++) |
| 634 | p[i] = bytestream2_get_le32(gbc); |
| 635 | |
Vittorio Giovara | 5c018ee | 2015-05-26 23:30:51 | [diff] [blame] | 636 | frame->palette_has_changed = 1; |
| 637 | } |
| 638 | |
| 639 | av_image_copy_plane(frame->data[0], frame->linesize[0], |
| 640 | gbc->buffer, linesize, |
| 641 | linesize, frame->height); |
| 642 | } |
| 643 | |
| 644 | /* Run any post processing here if needed. */ |
| 645 | if (avctx->pix_fmt == AV_PIX_FMT_RGBA || avctx->pix_fmt == AV_PIX_FMT_YA8) |
| 646 | run_postproc(avctx, frame); |
| 647 | |
| 648 | /* Frame is ready to be output. */ |
| 649 | frame->pict_type = AV_PICTURE_TYPE_I; |
| 650 | frame->key_frame = 1; |
| 651 | *got_frame = 1; |
| 652 | |
| 653 | return avpkt->size; |
| 654 | } |
| 655 | |
| 656 | AVCodec ff_dds_decoder = { |
| 657 | .name = "dds", |
| 658 | .long_name = NULL_IF_CONFIG_SMALL("DirectDraw Surface image decoder"), |
| 659 | .type = AVMEDIA_TYPE_VIDEO, |
| 660 | .id = AV_CODEC_ID_DDS, |
| 661 | .decode = dds_decode, |
| 662 | .priv_data_size = sizeof(DDSContext), |
| 663 | .capabilities = CODEC_CAP_DR1 | CODEC_CAP_SLICE_THREADS, |
| 664 | .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
| 665 | }; |