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
41 #define DDPF_FOURCC (1 << 2)
42 #define DDPF_PALETTE (1 << 5)
43 #define DDPF_NORMALMAP (1U << 31)
119 int alpha_exponent, ycocg_classic, ycocg_scaled, normal_map,
array;
121 /* Alternative DDS implementations use reserved1 as custom header. */
123 gimp_tag = bytestream2_get_le32(gbc);
124 alpha_exponent = gimp_tag ==
MKTAG(
'A',
'E',
'X',
'P');
125 ycocg_classic = gimp_tag ==
MKTAG(
'Y',
'C',
'G',
'1');
126 ycocg_scaled = gimp_tag ==
MKTAG(
'Y',
'C',
'G',
'2');
129 /* Now the real DDPF starts. */
130 size = bytestream2_get_le32(gbc);
135 flags = bytestream2_get_le32(gbc);
139 fourcc = bytestream2_get_le32(gbc);
141 if (
ctx->compressed &&
ctx->paletted) {
143 "Disabling invalid palette flag for compressed dds.\n");
147 bpp =
ctx->bpp = bytestream2_get_le32(gbc);
// rgbbitcount
148 r = bytestream2_get_le32(gbc);
// rbitmask
149 g = bytestream2_get_le32(gbc);
// gbitmask
150 b = bytestream2_get_le32(gbc);
// bbitmask
151 a = bytestream2_get_le32(gbc);
// abitmask
167 if (
ctx->compressed) {
168 ctx->dec.raw_ratio = 16;
170 case MKTAG(
'D',
'X',
'T',
'1'):
171 ctx->dec.tex_ratio = 8;
172 ctx->dec.tex_funct =
ctx->texdsp.dxt1a_block;
174 case MKTAG(
'D',
'X',
'T',
'2'):
175 ctx->dec.tex_ratio = 16;
176 ctx->dec.tex_funct =
ctx->texdsp.dxt2_block;
178 case MKTAG(
'D',
'X',
'T',
'3'):
179 ctx->dec.tex_ratio = 16;
180 ctx->dec.tex_funct =
ctx->texdsp.dxt3_block;
182 case MKTAG(
'D',
'X',
'T',
'4'):
183 ctx->dec.tex_ratio = 16;
184 ctx->dec.tex_funct =
ctx->texdsp.dxt4_block;
186 case MKTAG(
'D',
'X',
'T',
'5'):
187 ctx->dec.tex_ratio = 16;
189 ctx->dec.tex_funct =
ctx->texdsp.dxt5ys_block;
190 else if (ycocg_classic)
191 ctx->dec.tex_funct =
ctx->texdsp.dxt5y_block;
193 ctx->dec.tex_funct =
ctx->texdsp.dxt5_block;
195 case MKTAG(
'R',
'X',
'G',
'B'):
196 ctx->dec.tex_ratio = 16;
197 ctx->dec.tex_funct =
ctx->texdsp.dxt5_block;
198 /* This format may be considered as a normal map,
199 * but it is handled differently in a separate postproc. */
203 case MKTAG(
'A',
'T',
'I',
'1'):
204 case MKTAG(
'B',
'C',
'4',
'U'):
205 ctx->dec.tex_ratio = 8;
206 ctx->dec.tex_funct =
ctx->texdsp.rgtc1u_block;
208 case MKTAG(
'B',
'C',
'4',
'S'):
209 ctx->dec.tex_ratio = 8;
210 ctx->dec.tex_funct =
ctx->texdsp.rgtc1s_block;
212 case MKTAG(
'A',
'T',
'I',
'2'):
213 /* RGT2 variant with swapped R and G (3Dc)*/
214 ctx->dec.tex_ratio = 16;
215 ctx->dec.tex_funct =
ctx->texdsp.dxn3dc_block;
217 case MKTAG(
'B',
'C',
'5',
'U'):
218 ctx->dec.tex_ratio = 16;
219 ctx->dec.tex_funct =
ctx->texdsp.rgtc2u_block;
221 case MKTAG(
'B',
'C',
'5',
'S'):
222 ctx->dec.tex_ratio = 16;
223 ctx->dec.tex_funct =
ctx->texdsp.rgtc2s_block;
225 case MKTAG(
'U',
'Y',
'V',
'Y'):
229 case MKTAG(
'Y',
'U',
'Y',
'2'):
233 case MKTAG(
'P',
'8',
' ',
' '):
234 /* ATI Palette8, same as normal palette */
239 case MKTAG(
'G',
'1',
' ',
' '):
243 case MKTAG(
'D',
'X',
'1',
'0'):
244 /* DirectX 10 extra header */
245 dxgi = bytestream2_get_le32(gbc);
248 array = bytestream2_get_le32(gbc);
253 "Found array of size %d (ignored).\n",
array);
255 /* Only BC[1-5] are actually compressed. */
256 ctx->compressed = (dxgi >= 70) && (dxgi <= 84);
302 ctx->dec.tex_ratio = 8;
303 ctx->dec.tex_funct =
ctx->texdsp.dxt1a_block;
310 ctx->dec.tex_ratio = 16;
311 ctx->dec.tex_funct =
ctx->texdsp.dxt3_block;
318 ctx->dec.tex_ratio = 16;
319 ctx->dec.tex_funct =
ctx->texdsp.dxt5_block;
323 ctx->dec.tex_ratio = 8;
324 ctx->dec.tex_funct =
ctx->texdsp.rgtc1u_block;
327 ctx->dec.tex_ratio = 8;
328 ctx->dec.tex_funct =
ctx->texdsp.rgtc1s_block;
332 ctx->dec.tex_ratio = 16;
333 ctx->dec.tex_funct =
ctx->texdsp.rgtc2u_block;
336 ctx->dec.tex_ratio = 16;
337 ctx->dec.tex_funct =
ctx->texdsp.rgtc2s_block;
341 "Unsupported DXGI format %d.\n", dxgi);
349 }
else if (
ctx->paletted) {
358 if (bpp == 4 &&
r == 0 &&
g == 0 &&
b == 0 &&
a == 0)
361 else if (bpp == 8 &&
r == 0xff &&
g == 0 &&
b == 0 &&
a == 0)
363 else if (bpp == 8 &&
r == 0 &&
g == 0 &&
b == 0 &&
a == 0xff)
366 else if (bpp == 16 &&
r == 0xff &&
g == 0 &&
b == 0 &&
a == 0xff00)
368 else if (bpp == 16 &&
r == 0xff00 &&
g == 0 &&
b == 0 &&
a == 0xff) {
372 else if (bpp == 16 &&
r == 0xffff &&
g == 0 &&
b == 0 &&
a == 0)
374 else if (bpp == 16 &&
r == 0x7c00 &&
g == 0x3e0 &&
b == 0x1f &&
a == 0)
376 else if (bpp == 16 &&
r == 0x7c00 &&
g == 0x3e0 &&
b == 0x1f &&
a == 0x8000)
378 else if (bpp == 16 &&
r == 0xf800 &&
g == 0x7e0 &&
b == 0x1f &&
a == 0)
381 else if (bpp == 24 &&
r == 0xff0000 &&
g == 0xff00 &&
b == 0xff &&
a == 0)
384 else if (bpp == 32 &&
r == 0xff0000 &&
g == 0xff00 &&
b == 0xff &&
a == 0)
386 else if (bpp == 32 &&
r == 0xff &&
g == 0xff00 &&
b == 0xff0000 &&
a == 0)
388 else if (bpp == 32 &&
r == 0xff0000 &&
g == 0xff00 &&
b == 0xff &&
a == 0xff000000)
390 else if (bpp == 32 &&
r == 0xff &&
g == 0xff00 &&
b == 0xff0000 &&
a == 0xff000000)
395 "[bpp %d r 0x%x g 0x%x b 0x%x a 0x%x].\n", bpp,
r,
g,
b,
a);
400 /* Set any remaining post-proc that should happen before frame is ready. */
405 else if (ycocg_classic && !
ctx->compressed)
408 /* ATI/NVidia variants sometimes add swizzling in bpp. */
410 case MKTAG(
'A',
'2',
'X',
'Y'):
413 case MKTAG(
'x',
'G',
'B',
'R'):
416 case MKTAG(
'x',
'R',
'B',
'G'):
419 case MKTAG(
'R',
'B',
'x',
'G'):
422 case MKTAG(
'R',
'G',
'x',
'B'):
425 case MKTAG(
'R',
'x',
'B',
'G'):
428 case MKTAG(
'x',
'G',
'x',
'R'):
431 case MKTAG(
'A',
'2',
'D',
'5'):
442 for (
i = 0;
i <
frame->linesize[0] *
frame->height;
i += 4) {
453 switch (
ctx->postproc) {
455 /* Alpha-exponential mode divides each channel by the maximum
456 * R, G or B value, and stores the multiplying factor in the
460 for (
i = 0;
i <
frame->linesize[0] *
frame->height;
i += 4) {
467 src[0] =
r *
a / 255;
468 src[1] =
g *
a / 255;
469 src[2] =
b *
a / 255;
474 /* Normal maps work in the XYZ color space and they encode
475 * X in R or in A, depending on the texture type, Y in G and
476 * derive Z with a square root of the distance.
478 * http://www.realtimecollisiondetection.net/blog/?p=28 */
481 x_off =
ctx->dec.tex_ratio == 8 ? 0 : 3;
482 for (
i = 0;
i <
frame->linesize[0] *
frame->height;
i += 4) {
488 int d = (255 * 255 - x * x - y * y) / 2;
499 /* Data is Y-Co-Cg-A and not RGBA, but they are represented
500 * with the same masks in the DDPF header. */
503 for (
i = 0;
i <
frame->linesize[0] *
frame->height;
i += 4) {
506 int cg =
src[1] - 128;
507 int co =
src[2] - 128;
517 /* Alpha and Luma are stored swapped. */
520 for (
i = 0;
i <
frame->linesize[0] *
frame->height;
i += 2) {
526 /* Swap R and G, often used to restore a standard RGTC2. */
531 /* Swap G and A, then B and new A (G). */
547 /* Swap R and A (misleading name). */
552 /* Swap B and A, then R and new A (B). */
558 /* Swap G and A, then R and new A (G), then new R (G) and new G (A).
559 * This variant does not store any B component. */
566 /* Swap G and A, then R and new A (G). */
592 if (bytestream2_get_le32(gbc) !=
MKTAG(
'D',
'D',
'S',
' ') ||
593 bytestream2_get_le32(gbc) != 124) {
// header size
600 height = bytestream2_get_le32(gbc);
601 width = bytestream2_get_le32(gbc);
609 /* Since codec is based on 4x4 blocks, size is aligned to 4. */
615 mipmap = bytestream2_get_le32(gbc);
619 /* Extract pixel format information, considering additional elements
620 * in reserved1 and reserved2. */
629 if (
ctx->compressed) {
637 "Compressed Buffer is too small (%d < %d).\n",
642 /* Use the decompress function on the texture, one block per thread. */
644 ctx->dec.frame_data.out =
frame->data[0];
645 ctx->dec.stride =
frame->linesize[0];
653 /* Use the first 64 bytes as palette, then copy the rest. */
655 for (
i = 0;
i < 16;
i++) {
657 (
frame->data[1][2+
i*4]<<0)+
658 (
frame->data[1][1+
i*4]<<8)+
659 (
frame->data[1][0+
i*4]<<16)+
660 ((
unsigned)
frame->data[1][3+
i*4]<<24)
670 for (y = 0; y <
frame->height; y++) {
671 for (x = 0; x <
frame->width; x += 2) {
672 uint8_t
val = bytestream2_get_byte(gbc);
683 /* Use the first 1024 bytes as palette, then copy the rest. */
685 for (
i = 0;
i < 256;
i++)
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)
702 linesize,
frame->height);
705 /* Run any post processing here if needed. */
709 /* Frame is ready to be output. */
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
static double val(void *priv, double ch)
const FFCodec ff_dds_decoder
static AVFormatContext * ctx
Libavcodec external API header.
static av_always_inline unsigned int bytestream2_get_buffer(GetByteContext *g, uint8_t *dst, unsigned int size)
static av_always_inline int bytestream2_get_bytes_left(const GetByteContext *g)
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
#define flags(name, subs,...)
#define i(width, name, range_min, range_max)
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
static __device__ float sqrtf(float a)
@ DXGI_FORMAT_B8G8R8A8_UNORM
@ DXGI_FORMAT_BC3_UNORM_SRGB
@ DXGI_FORMAT_BC3_TYPELESS
@ DXGI_FORMAT_R16G16B16A16_SNORM
@ DXGI_FORMAT_R16G16B16A16_UNORM
@ DXGI_FORMAT_BC1_UNORM_SRGB
@ DXGI_FORMAT_R8G8B8A8_TYPELESS
@ DXGI_FORMAT_BC2_TYPELESS
@ DXGI_FORMAT_B8G8R8A8_UNORM_SRGB
@ DXGI_FORMAT_B8G8R8X8_TYPELESS
@ DXGI_FORMAT_B8G8R8A8_TYPELESS
@ DXGI_FORMAT_R16G16B16A16_UINT
@ DXGI_FORMAT_R8G8B8A8_SINT
@ DXGI_FORMAT_R8G8B8A8_SNORM
@ DXGI_FORMAT_R8G8B8A8_UINT
@ DXGI_FORMAT_R16G16B16A16_TYPELESS
@ DXGI_FORMAT_R16G16B16A16_FLOAT
@ DXGI_FORMAT_R8G8B8A8_UNORM
@ DXGI_FORMAT_BC5_TYPELESS
@ DXGI_FORMAT_BC2_UNORM_SRGB
@ DXGI_FORMAT_BC4_TYPELESS
@ DXGI_FORMAT_R8G8B8A8_UNORM_SRGB
@ DXGI_FORMAT_R16G16B16A16_SINT
@ DXGI_FORMAT_BC1_TYPELESS
@ DXGI_FORMAT_B5G6R5_UNORM
@ DXGI_FORMAT_B8G8R8X8_UNORM_SRGB
@ DXGI_FORMAT_B8G8R8X8_UNORM
static int dds_decode(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *avpkt)
static void do_swizzle(AVFrame *frame, int x, int y)
static void run_postproc(AVCodecContext *avctx, AVFrame *frame)
static int parse_pixel_format(AVCodecContext *avctx)
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
int ff_set_dimensions(AVCodecContext *s, int width, int height)
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
#define AV_CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
#define AV_LOG_WARNING
Something somehow does not look correct.
#define AV_LOG_VERBOSE
Detailed information.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
#define av_fourcc2str(fourcc)
void av_image_copy_plane(uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize, int bytewidth, int height)
Copy image plane from src to dst.
int av_image_get_linesize(enum AVPixelFormat pix_fmt, int width, int plane)
Compute the size of an image line with format pix_fmt and width width for the plane plane.
Macro definitions for various function/variable attributes.
Replacements for frequently missing libm functions.
#define FFSWAP(type, a, b)
#define MKTAG(a, b, c, d)
@ AV_PIX_FMT_MONOBLACK
Y , 1bpp, 0 is black, 1 is white, in each byte pixels are ordered from the msb to the lsb.
@ AV_PIX_FMT_BGR0
packed BGR 8:8:8, 32bpp, BGRXBGRX... X=unused/undefined
@ AV_PIX_FMT_BGRA
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
@ AV_PIX_FMT_UYVY422
packed YUV 4:2:2, 16bpp, Cb Y0 Cr Y1
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
@ AV_PIX_FMT_RGB565LE
packed RGB 5:6:5, 16bpp, (msb) 5R 6G 5B(lsb), little-endian
@ AV_PIX_FMT_RGB555LE
packed RGB 5:5:5, 16bpp, (msb)1X 5R 5G 5B(lsb), little-endian, X=unused/undefined
@ AV_PIX_FMT_RGB0
packed RGB 8:8:8, 32bpp, RGBXRGBX... X=unused/undefined
@ AV_PIX_FMT_GRAY16LE
Y , 16bpp, little-endian.
@ AV_PIX_FMT_YUYV422
packed YUV 4:2:2, 16bpp, Y0 Cb Y1 Cr
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
@ AV_PIX_FMT_BGR24
packed RGB 8:8:8, 24bpp, BGRBGR...
@ AV_PIX_FMT_YA8
8 bits gray, 8 bits alpha
#define AV_PIX_FMT_BGRA64
@ AVCOL_SPC_RGB
order of coefficients is actually GBR, also IEC 61966-2-1 (sRGB), YZX and ST 428-1
main external API structure.
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
int width
picture width / height.
enum AVColorSpace colorspace
YUV colorspace type.
int thread_count
thread count is used to decide how many independent tasks should be passed to execute()
int coded_width
Bitstream width / height, may be different from width/height e.g.
This structure describes decoded (raw) audio or video data.
This structure stores compressed data.
enum DDSPostProc postproc
TextureDSPThreadContext dec
static int array[MAX_W *MAX_W]
av_cold void ff_texturedsp_init(TextureDSPContext *c)
Texture block (4x4) module.
int ff_texturedsp_exec_decompress_threads(struct AVCodecContext *avctx, TextureDSPThreadContext *ctx)