DirectDraw Surface image decoder

Signed-off-by: Vittorio Giovara <vittorio.giovara@gmail.com>
This commit is contained in:
Vittorio Giovara 2015-05-27 00:30:51 +01:00
parent 7ca3e5203f
commit 5c018ee188
54 changed files with 774 additions and 1 deletions

View File

@ -35,6 +35,7 @@ version <next>:
- Multipart JPEG demuxer
- Support the extended vp9 profiles in the libvpx wrapper
- Hap decoder and encoder
- DirectDraw Surface image/texture decoder
version 11:

1
configure vendored
View File

@ -1823,6 +1823,7 @@ cook_decoder_select="audiodsp mdct sinewin"
cscd_decoder_select="lzo"
cscd_decoder_suggest="zlib"
dca_decoder_select="fmtconvert mdct"
dds_decoder_select="texturedsp"
dnxhd_decoder_select="blockdsp idctdsp"
dnxhd_encoder_select="aandcttables blockdsp fdctdsp idctdsp mpegvideoenc pixblockdsp"
dvvideo_decoder_select="dvprofile idctdsp"

View File

@ -245,6 +245,7 @@ library:
@item Deluxe Paint Animation @tab @tab X
@item DFA @tab @tab X
@tab This format is used in Chronomaster game
@item DirectDraw Surface @tab @tab X
@item DV video @tab X @tab X
@item DXA @tab @tab X
@tab This format is used in the non-Windows version of the Feeble Files

View File

@ -171,6 +171,7 @@ OBJS-$(CONFIG_CYUV_DECODER) += cyuv.o
OBJS-$(CONFIG_DCA_DECODER) += dcadec.o dca.o dcadsp.o \
dcadata.o dca_exss.o \
dca_xll.o synth_filter.o
OBJS-$(CONFIG_DDS_DECODER) += dds.o
OBJS-$(CONFIG_DFA_DECODER) += dfa.o
OBJS-$(CONFIG_DNXHD_DECODER) += dnxhddec.o dnxhddata.o
OBJS-$(CONFIG_DNXHD_ENCODER) += dnxhdenc.o dnxhddata.o

View File

@ -132,6 +132,7 @@ void avcodec_register_all(void)
REGISTER_ENCDEC (COMFORTNOISE, comfortnoise);
REGISTER_DECODER(CSCD, cscd);
REGISTER_DECODER(CYUV, cyuv);
REGISTER_DECODER(DDS, dds);
REGISTER_DECODER(DFA, dfa);
REGISTER_ENCDEC (DNXHD, dnxhd);
REGISTER_ENCDEC (DPX, dpx);

View File

@ -297,6 +297,7 @@ enum AVCodecID {
AV_CODEC_ID_TDSC,
AV_CODEC_ID_HQ_HQA,
AV_CODEC_ID_HAP,
AV_CODEC_ID_DDS,
/* various PCM "codecs" */
AV_CODEC_ID_FIRST_AUDIO = 0x10000, ///< A dummy id pointing at the start of audio codecs

View File

@ -1178,6 +1178,14 @@ static const AVCodecDescriptor codec_descriptors[] = {
.long_name = NULL_IF_CONFIG_SMALL("BRender PIX image"),
.props = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSLESS,
},
{
.id = AV_CODEC_ID_DDS,
.type = AVMEDIA_TYPE_VIDEO,
.name = "dds",
.long_name = NULL_IF_CONFIG_SMALL("DirectDraw Surface image decoder"),
.props = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSY |
AV_CODEC_PROP_LOSSLESS,
},
{
.id = AV_CODEC_ID_DPX,
.type = AVMEDIA_TYPE_VIDEO,

660
libavcodec/dds.c Normal file
View File

@ -0,0 +1,660 @@
/*
* DirectDraw Surface image decoder
* Copyright (C) 2015 Vittorio Giovara <vittorio.giovara@gmail.com>
*
* This file is part of Libav.
*
* Libav is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* Libav is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with Libav; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file
* DDS decoder
*
* https://msdn.microsoft.com/en-us/library/bb943982%28v=vs.85%29.aspx
*/
#include <stdint.h>
#include "libavutil/imgutils.h"
#include "avcodec.h"
#include "bytestream.h"
#include "internal.h"
#include "texturedsp.h"
#include "thread.h"
#define DDPF_FOURCC (1 << 2)
#define DDPF_PALETTE (1 << 5)
#define DDPF_NORMALMAP (1 << 31)
enum DDSPostProc {
DDS_NONE = 0,
DDS_ALPHA_EXP,
DDS_NORMAL_MAP,
DDS_RAW_YCOCG,
DDS_SWAP_ALPHA,
DDS_SWIZZLE_A2XY,
DDS_SWIZZLE_RBXG,
DDS_SWIZZLE_RGXB,
DDS_SWIZZLE_RXBG,
DDS_SWIZZLE_RXGB,
DDS_SWIZZLE_XGBR,
DDS_SWIZZLE_XRBG,
DDS_SWIZZLE_XGXR,
} DDSPostProc;
enum DDSDXGIFormat {
DXGI_FORMAT_R16G16B16A16_TYPELESS = 9,
DXGI_FORMAT_R16G16B16A16_FLOAT = 10,
DXGI_FORMAT_R16G16B16A16_UNORM = 11,
DXGI_FORMAT_R16G16B16A16_UINT = 12,
DXGI_FORMAT_R16G16B16A16_SNORM = 13,
DXGI_FORMAT_R16G16B16A16_SINT = 14,
DXGI_FORMAT_R8G8B8A8_TYPELESS = 27,
DXGI_FORMAT_R8G8B8A8_UNORM = 28,
DXGI_FORMAT_R8G8B8A8_UNORM_SRGB = 29,
DXGI_FORMAT_R8G8B8A8_UINT = 30,
DXGI_FORMAT_R8G8B8A8_SNORM = 31,
DXGI_FORMAT_R8G8B8A8_SINT = 32,
DXGI_FORMAT_BC1_TYPELESS = 70,
DXGI_FORMAT_BC1_UNORM = 71,
DXGI_FORMAT_BC1_UNORM_SRGB = 72,
DXGI_FORMAT_BC2_TYPELESS = 73,
DXGI_FORMAT_BC2_UNORM = 74,
DXGI_FORMAT_BC2_UNORM_SRGB = 75,
DXGI_FORMAT_BC3_TYPELESS = 76,
DXGI_FORMAT_BC3_UNORM = 77,
DXGI_FORMAT_BC3_UNORM_SRGB = 78,
DXGI_FORMAT_BC4_TYPELESS = 79,
DXGI_FORMAT_BC4_UNORM = 80,
DXGI_FORMAT_BC4_SNORM = 81,
DXGI_FORMAT_BC5_TYPELESS = 82,
DXGI_FORMAT_BC5_UNORM = 83,
DXGI_FORMAT_BC5_SNORM = 84,
DXGI_FORMAT_B5G6R5_UNORM = 85,
DXGI_FORMAT_B8G8R8A8_UNORM = 87,
DXGI_FORMAT_B8G8R8X8_UNORM = 88,
DXGI_FORMAT_B8G8R8A8_TYPELESS = 90,
DXGI_FORMAT_B8G8R8A8_UNORM_SRGB = 91,
DXGI_FORMAT_B8G8R8X8_TYPELESS = 92,
DXGI_FORMAT_B8G8R8X8_UNORM_SRGB = 93,
} DDSDXGIFormat;
typedef struct DDSContext {
TextureDSPContext texdsp;
GetByteContext gbc;
int compressed;
int paletted;
enum DDSPostProc postproc;
const uint8_t *tex_data; // Compressed texture
int tex_ratio; // Compression ratio
/* Pointer to the selected compress or decompress function. */
int (*tex_funct)(uint8_t *dst, ptrdiff_t stride, const uint8_t *block);
} DDSContext;
static int parse_pixel_format(AVCodecContext *avctx)
{
DDSContext *ctx = avctx->priv_data;
GetByteContext *gbc = &ctx->gbc;
char buf[32];
uint32_t flags, fourcc, gimp_tag;
enum DDSDXGIFormat dxgi;
int size, bpp, r, g, b, a;
int alpha_exponent, ycocg_classic, ycocg_scaled, normal_map, array;
/* Alternative DDS implementations use reserved1 as custom header. */
bytestream2_skip(gbc, 4 * 3);
gimp_tag = bytestream2_get_le32(gbc);
alpha_exponent = gimp_tag == MKTAG('A', 'E', 'X', 'P');
ycocg_classic = gimp_tag == MKTAG('Y', 'C', 'G', '1');
ycocg_scaled = gimp_tag == MKTAG('Y', 'C', 'G', '2');
bytestream2_skip(gbc, 4 * 7);
/* Now the real DDPF starts. */
size = bytestream2_get_le32(gbc);
if (size != 32) {
av_log(avctx, AV_LOG_ERROR, "Invalid pixel format header %d.\n", size);
return AVERROR_INVALIDDATA;
}
flags = bytestream2_get_le32(gbc);
ctx->compressed = flags & DDPF_FOURCC;
ctx->paletted = flags & DDPF_PALETTE;
normal_map = flags & DDPF_NORMALMAP;
fourcc = bytestream2_get_le32(gbc);
bpp = bytestream2_get_le32(gbc); // rgbbitcount
r = bytestream2_get_le32(gbc); // rbitmask
g = bytestream2_get_le32(gbc); // gbitmask
b = bytestream2_get_le32(gbc); // bbitmask
a = bytestream2_get_le32(gbc); // abitmask
bytestream2_skip(gbc, 4); // caps
bytestream2_skip(gbc, 4); // caps2
bytestream2_skip(gbc, 4); // caps3
bytestream2_skip(gbc, 4); // caps4
bytestream2_skip(gbc, 4); // reserved2
av_get_codec_tag_string(buf, sizeof(buf), fourcc);
av_log(avctx, AV_LOG_VERBOSE, "fourcc %s bpp %d "
"r 0x%x g 0x%x b 0x%x a 0x%x\n", buf, bpp, r, g, b, a);
if (gimp_tag) {
av_get_codec_tag_string(buf, sizeof(buf), gimp_tag);
av_log(avctx, AV_LOG_VERBOSE, "and GIMP-DDS tag %s\n", buf);
}
if (ctx->compressed)
avctx->pix_fmt = AV_PIX_FMT_RGBA;
if (ctx->compressed) {
switch (fourcc) {
case MKTAG('D', 'X', 'T', '1'):
ctx->tex_ratio = 8;
ctx->tex_funct = ctx->texdsp.dxt1a_block;
break;
case MKTAG('D', 'X', 'T', '2'):
ctx->tex_ratio = 16;
ctx->tex_funct = ctx->texdsp.dxt2_block;
break;
case MKTAG('D', 'X', 'T', '3'):
ctx->tex_ratio = 16;
ctx->tex_funct = ctx->texdsp.dxt3_block;
break;
case MKTAG('D', 'X', 'T', '4'):
ctx->tex_ratio = 16;
ctx->tex_funct = ctx->texdsp.dxt4_block;
break;
case MKTAG('D', 'X', 'T', '5'):
ctx->tex_ratio = 16;
if (ycocg_scaled)
ctx->tex_funct = ctx->texdsp.dxt5ys_block;
else if (ycocg_classic)
ctx->tex_funct = ctx->texdsp.dxt5y_block;
else
ctx->tex_funct = ctx->texdsp.dxt5_block;
break;
case MKTAG('R', 'X', 'G', 'B'):
ctx->tex_ratio = 16;
ctx->tex_funct = ctx->texdsp.dxt5_block;
/* This format may be considered as a normal map,
* but it is handled differently in a separate postproc. */
ctx->postproc = DDS_SWIZZLE_RXGB;
normal_map = 0;
break;
case MKTAG('A', 'T', 'I', '1'):
case MKTAG('B', 'C', '4', 'U'):
ctx->tex_ratio = 8;
ctx->tex_funct = ctx->texdsp.rgtc1u_block;
break;
case MKTAG('B', 'C', '4', 'S'):
ctx->tex_ratio = 8;
ctx->tex_funct = ctx->texdsp.rgtc1s_block;
break;
case MKTAG('A', 'T', 'I', '2'):
/* RGT2 variant with swapped R and G (3Dc)*/
ctx->tex_ratio = 16;
ctx->tex_funct = ctx->texdsp.dxn3dc_block;
break;
case MKTAG('B', 'C', '5', 'U'):
ctx->tex_ratio = 16;
ctx->tex_funct = ctx->texdsp.rgtc2u_block;
break;
case MKTAG('B', 'C', '5', 'S'):
ctx->tex_ratio = 16;
ctx->tex_funct = ctx->texdsp.rgtc2s_block;
break;
case MKTAG('U', 'Y', 'V', 'Y'):
ctx->compressed = 0;
avctx->pix_fmt = AV_PIX_FMT_UYVY422;
break;
case MKTAG('Y', 'U', 'Y', '2'):
ctx->compressed = 0;
avctx->pix_fmt = AV_PIX_FMT_YUYV422;
break;
case MKTAG('P', '8', ' ', ' '):
/* ATI Palette8, same as normal palette */
ctx->compressed = 0;
ctx->paletted = 1;
avctx->pix_fmt = AV_PIX_FMT_PAL8;
break;
case MKTAG('D', 'X', '1', '0'):
/* DirectX 10 extra header */
dxgi = bytestream2_get_le32(gbc);
bytestream2_skip(gbc, 4); // resourceDimension
bytestream2_skip(gbc, 4); // miscFlag
array = bytestream2_get_le32(gbc);
bytestream2_skip(gbc, 4); // miscFlag2
if (array != 0)
av_log(avctx, AV_LOG_VERBOSE,
"Found array of size %d (ignored).\n", array);
/* Only BC[1-5] are actually compressed. */
ctx->compressed = (dxgi >= 70) && (dxgi <= 84);
av_log(avctx, AV_LOG_VERBOSE, "DXGI format %d.\n", dxgi);
switch (dxgi) {
/* RGB types. */
case DXGI_FORMAT_R16G16B16A16_TYPELESS:
case DXGI_FORMAT_R16G16B16A16_FLOAT:
case DXGI_FORMAT_R16G16B16A16_UNORM:
case DXGI_FORMAT_R16G16B16A16_UINT:
case DXGI_FORMAT_R16G16B16A16_SNORM:
case DXGI_FORMAT_R16G16B16A16_SINT:
avctx->pix_fmt = AV_PIX_FMT_BGRA64;
break;
case DXGI_FORMAT_R8G8B8A8_UNORM_SRGB:
avctx->colorspace = AVCOL_SPC_RGB;
case DXGI_FORMAT_R8G8B8A8_TYPELESS:
case DXGI_FORMAT_R8G8B8A8_UNORM:
case DXGI_FORMAT_R8G8B8A8_UINT:
case DXGI_FORMAT_R8G8B8A8_SNORM:
case DXGI_FORMAT_R8G8B8A8_SINT:
avctx->pix_fmt = AV_PIX_FMT_BGRA;
break;
case DXGI_FORMAT_B8G8R8A8_UNORM_SRGB:
avctx->colorspace = AVCOL_SPC_RGB;
case DXGI_FORMAT_B8G8R8A8_TYPELESS:
case DXGI_FORMAT_B8G8R8A8_UNORM:
avctx->pix_fmt = AV_PIX_FMT_RGBA;
break;
case DXGI_FORMAT_B8G8R8X8_UNORM_SRGB:
avctx->colorspace = AVCOL_SPC_RGB;
case DXGI_FORMAT_B8G8R8X8_TYPELESS:
case DXGI_FORMAT_B8G8R8X8_UNORM:
avctx->pix_fmt = AV_PIX_FMT_RGBA; // opaque
break;
case DXGI_FORMAT_B5G6R5_UNORM:
avctx->pix_fmt = AV_PIX_FMT_RGB565LE;
break;
/* Texture types. */
case DXGI_FORMAT_BC1_UNORM_SRGB:
avctx->colorspace = AVCOL_SPC_RGB;
case DXGI_FORMAT_BC1_TYPELESS:
case DXGI_FORMAT_BC1_UNORM:
ctx->tex_ratio = 8;
ctx->tex_funct = ctx->texdsp.dxt1a_block;
break;
case DXGI_FORMAT_BC2_UNORM_SRGB:
avctx->colorspace = AVCOL_SPC_RGB;
case DXGI_FORMAT_BC2_TYPELESS:
case DXGI_FORMAT_BC2_UNORM:
ctx->tex_ratio = 16;
ctx->tex_funct = ctx->texdsp.dxt3_block;
break;
case DXGI_FORMAT_BC3_UNORM_SRGB:
avctx->colorspace = AVCOL_SPC_RGB;
case DXGI_FORMAT_BC3_TYPELESS:
case DXGI_FORMAT_BC3_UNORM:
ctx->tex_ratio = 16;
ctx->tex_funct = ctx->texdsp.dxt5_block;
break;
case DXGI_FORMAT_BC4_TYPELESS:
case DXGI_FORMAT_BC4_UNORM:
ctx->tex_ratio = 8;
ctx->tex_funct = ctx->texdsp.rgtc1u_block;
break;
case DXGI_FORMAT_BC4_SNORM:
ctx->tex_ratio = 8;
ctx->tex_funct = ctx->texdsp.rgtc1s_block;
break;
case DXGI_FORMAT_BC5_TYPELESS:
case DXGI_FORMAT_BC5_UNORM:
ctx->tex_ratio = 16;
ctx->tex_funct = ctx->texdsp.rgtc2u_block;
break;
case DXGI_FORMAT_BC5_SNORM:
ctx->tex_ratio = 16;
ctx->tex_funct = ctx->texdsp.rgtc2s_block;
break;
default:
av_log(avctx, AV_LOG_ERROR,
"Unsupported DXGI format %d.\n", dxgi);
return AVERROR_INVALIDDATA;
}
break;
default:
av_log(avctx, AV_LOG_ERROR, "Unsupported %s fourcc.\n", buf);
return AVERROR_INVALIDDATA;
}
} else if (ctx->paletted) {
if (bpp == 8) {
avctx->pix_fmt = AV_PIX_FMT_PAL8;
} else {
av_log(avctx, AV_LOG_ERROR, "Unsupported palette bpp %d.\n", bpp);
return AVERROR_INVALIDDATA;
}
} else {
/* 8 bpp */
if (bpp == 8 && r == 0xff && g == 0 && b == 0 && a == 0)
avctx->pix_fmt = AV_PIX_FMT_GRAY8;
/* 16 bpp */
else if (bpp == 16 && r == 0xff && g == 0 && b == 0 && a == 0xff00)
avctx->pix_fmt = AV_PIX_FMT_YA8;
else if (bpp == 16 && r == 0xffff && g == 0 && b == 0 && a == 0)
avctx->pix_fmt = AV_PIX_FMT_GRAY16LE;
else if (bpp == 16 && r == 0xf800 && g == 0x7e0 && b == 0x1f && a == 0)
avctx->pix_fmt = AV_PIX_FMT_RGB565LE;
/* 24 bpp */
else if (bpp == 24 && r == 0xff0000 && g == 0xff00 && b == 0xff && a == 0)
avctx->pix_fmt = AV_PIX_FMT_BGR24;
/* 32 bpp */
else if (bpp == 32 && r == 0xff0000 && g == 0xff00 && b == 0xff && a == 0)
avctx->pix_fmt = AV_PIX_FMT_RGBA; // opaque
else if (bpp == 32 && r == 0xff && g == 0xff00 && b == 0xff0000 && a == 0)
avctx->pix_fmt = AV_PIX_FMT_BGRA; // opaque
else if (bpp == 32 && r == 0xff0000 && g == 0xff00 && b == 0xff && a == 0xff000000)
avctx->pix_fmt = AV_PIX_FMT_RGBA;
else if (bpp == 32 && r == 0xff && g == 0xff00 && b == 0xff0000 && a == 0xff000000)
avctx->pix_fmt = AV_PIX_FMT_BGRA;
/* give up */
else {
av_log(avctx, AV_LOG_ERROR, "Unknown pixel format "
"[bpp %d r 0x%x g 0x%x b 0x%x a 0x%x].\n", bpp, r, g, b, a);
return AVERROR_INVALIDDATA;
}
}
/* Set any remaining post-proc that should happen before frame is ready. */
if (alpha_exponent)
ctx->postproc = DDS_ALPHA_EXP;
else if (normal_map)
ctx->postproc = DDS_NORMAL_MAP;
else if (ycocg_classic && !ctx->compressed)
ctx->postproc = DDS_RAW_YCOCG;
else if (avctx->pix_fmt == AV_PIX_FMT_YA8)
ctx->postproc = DDS_SWAP_ALPHA;
/* ATI/NVidia variants sometimes add swizzling in bpp. */
switch (bpp) {
case MKTAG('A', '2', 'X', 'Y'):
ctx->postproc = DDS_SWIZZLE_A2XY;
break;
case MKTAG('x', 'G', 'B', 'R'):
ctx->postproc = DDS_SWIZZLE_XGBR;
break;
case MKTAG('x', 'R', 'B', 'G'):
ctx->postproc = DDS_SWIZZLE_XRBG;
break;
case MKTAG('R', 'B', 'x', 'G'):
ctx->postproc = DDS_SWIZZLE_RBXG;
break;
case MKTAG('R', 'G', 'x', 'B'):
ctx->postproc = DDS_SWIZZLE_RGXB;
break;
case MKTAG('R', 'x', 'B', 'G'):
ctx->postproc = DDS_SWIZZLE_RXBG;
break;
case MKTAG('x', 'G', 'x', 'R'):
ctx->postproc = DDS_SWIZZLE_XGXR;
break;
case MKTAG('A', '2', 'D', '5'):
ctx->postproc = DDS_NORMAL_MAP;
break;
}
return 0;
}
static int decompress_texture_thread(AVCodecContext *avctx, void *arg,
int block_nb, int thread_nb)
{
DDSContext *ctx = avctx->priv_data;
AVFrame *frame = arg;
int x = (TEXTURE_BLOCK_W * block_nb) % avctx->coded_width;
int y = TEXTURE_BLOCK_H * (TEXTURE_BLOCK_W * block_nb / avctx->coded_width);
uint8_t *p = frame->data[0] + x * 4 + y * frame->linesize[0];
const uint8_t *d = ctx->tex_data + block_nb * ctx->tex_ratio;
ctx->tex_funct(p, frame->linesize[0], d);
return 0;
}
static void do_swizzle(AVFrame *frame, int x, int y)
{
int i;
for (i = 0; i < frame->linesize[0] * frame->height; i += 4) {
uint8_t *src = frame->data[0] + i;
FFSWAP(uint8_t, src[x], src[y]);
}
}
static void run_postproc(AVCodecContext *avctx, AVFrame *frame)
{
DDSContext *ctx = avctx->priv_data;
int i, x_off;
switch (ctx->postproc) {
case DDS_ALPHA_EXP:
/* Alpha-exponential mode divides each channel by the maximum
* R, G or B value, and stores the multiplying factor in the
* alpha channel. */
av_log(avctx, AV_LOG_DEBUG, "Post-processing alpha exponent.\n");
for (i = 0; i < frame->linesize[0] * frame->height; i += 4) {
uint8_t *src = frame->data[0] + i;
int r = src[0];
int g = src[1];
int b = src[2];
int a = src[3];
src[0] = r * a / 255;
src[1] = g * a / 255;
src[2] = b * a / 255;
src[3] = 255;
}
break;
case DDS_NORMAL_MAP:
/* Normal maps work in the XYZ color space and they encode
* X in R or in A, depending on the texture type, Y in G and
* derive Z with a square root of the distance.
*
* http://www.realtimecollisiondetection.net/blog/?p=28 */
av_log(avctx, AV_LOG_DEBUG, "Post-processing normal map.\n");
x_off = ctx->tex_ratio == 8 ? 0 : 3;
for (i = 0; i < frame->linesize[0] * frame->height; i += 4) {
uint8_t *src = frame->data[0] + i;
int x = src[x_off];
int y = src[1];
int z = 127;
int d = (255 * 255 - x * x - y * y) / 2;
if (d > 0)
z = rint(sqrtf(d));
src[0] = x;
src[1] = y;
src[2] = z;
src[3] = 255;
}
break;
case DDS_RAW_YCOCG:
/* Data is Y-Co-Cg-A and not RGBA, but they are represented
* with the same masks in the DDPF header. */
av_log(avctx, AV_LOG_DEBUG, "Post-processing raw YCoCg.\n");
for (i = 0; i < frame->linesize[0] * frame->height; i += 4) {
uint8_t *src = frame->data[0] + i;
int a = src[0];
int cg = src[1] - 128;
int co = src[2] - 128;
int y = src[3];
src[0] = av_clip_uint8(y + co - cg);
src[1] = av_clip_uint8(y + cg);
src[2] = av_clip_uint8(y - co - cg);
src[3] = a;
}
break;
case DDS_SWAP_ALPHA:
/* Alpha and Luma are stored swapped. */
av_log(avctx, AV_LOG_DEBUG, "Post-processing swapped Luma/Alpha.\n");
for (i = 0; i < frame->linesize[0] * frame->height; i += 2) {
uint8_t *src = frame->data[0] + i;
FFSWAP(uint8_t, src[0], src[1]);
}
break;
case DDS_SWIZZLE_A2XY:
/* Swap R and G, often used to restore a standard RGTC2. */
av_log(avctx, AV_LOG_DEBUG, "Post-processing A2XY swizzle.\n");
do_swizzle(frame, 0, 1);
break;
case DDS_SWIZZLE_RBXG:
/* Swap G and A, then B and new A (G). */
av_log(avctx, AV_LOG_DEBUG, "Post-processing RBXG swizzle.\n");
do_swizzle(frame, 1, 3);
do_swizzle(frame, 2, 3);
break;
case DDS_SWIZZLE_RGXB:
/* Swap B and A. */
av_log(avctx, AV_LOG_DEBUG, "Post-processing RGXB swizzle.\n");
do_swizzle(frame, 2, 3);
break;
case DDS_SWIZZLE_RXBG:
/* Swap G and A. */
av_log(avctx, AV_LOG_DEBUG, "Post-processing RXBG swizzle.\n");
do_swizzle(frame, 1, 3);
break;
case DDS_SWIZZLE_RXGB:
/* Swap R and A (misleading name). */
av_log(avctx, AV_LOG_DEBUG, "Post-processing RXGB swizzle.\n");
do_swizzle(frame, 0, 3);
break;
case DDS_SWIZZLE_XGBR:
/* Swap B and A, then R and new A (B). */
av_log(avctx, AV_LOG_DEBUG, "Post-processing XGBR swizzle.\n");
do_swizzle(frame, 2, 3);
do_swizzle(frame, 0, 3);
break;
case DDS_SWIZZLE_XGXR:
/* Swap G and A, then R and new A (G), then new R (G) and new G (A).
* This variant does not store any B component. */
av_log(avctx, AV_LOG_DEBUG, "Post-processing XGXR swizzle.\n");
do_swizzle(frame, 1, 3);
do_swizzle(frame, 0, 3);
do_swizzle(frame, 0, 1);
break;
case DDS_SWIZZLE_XRBG:
/* Swap G and A, then R and new A (G). */
av_log(avctx, AV_LOG_DEBUG, "Post-processing XRBG swizzle.\n");
do_swizzle(frame, 1, 3);
do_swizzle(frame, 0, 3);
break;
}
}
static int dds_decode(AVCodecContext *avctx, void *data,
int *got_frame, AVPacket *avpkt)
{
DDSContext *ctx = avctx->priv_data;
GetByteContext *gbc = &ctx->gbc;
AVFrame *frame = data;
int blocks, mipmap;
int ret;
ff_texturedsp_init(&ctx->texdsp);
bytestream2_init(gbc, avpkt->data, avpkt->size);
if (bytestream2_get_bytes_left(gbc) < 128) {
av_log(avctx, AV_LOG_ERROR, "Frame is too small (%d).",
bytestream2_get_bytes_left(gbc));
return AVERROR_INVALIDDATA;
}
if (bytestream2_get_le32(gbc) != MKTAG('D', 'D', 'S', ' ') ||
bytestream2_get_le32(gbc) != 124) { // header size
av_log(avctx, AV_LOG_ERROR, "Invalid DDS header.");
return AVERROR_INVALIDDATA;
}
bytestream2_skip(gbc, 4); // flags
avctx->height = bytestream2_get_le32(gbc);
avctx->width = bytestream2_get_le32(gbc);
ret = av_image_check_size(avctx->width, avctx->height, 0, avctx);
if (ret < 0) {
av_log(avctx, AV_LOG_ERROR, "Invalid image size %dx%d.\n",
avctx->width, avctx->height);
return ret;
}
/* Since codec is based on 4x4 blocks, size is aligned to 4. */
avctx->coded_width = FFALIGN(avctx->width, TEXTURE_BLOCK_W);
avctx->coded_height = FFALIGN(avctx->height, TEXTURE_BLOCK_H);
bytestream2_skip(gbc, 4); // pitch
bytestream2_skip(gbc, 4); // depth
mipmap = bytestream2_get_le32(gbc);
if (mipmap != 0)
av_log(avctx, AV_LOG_VERBOSE, "Found %d mipmaps (ignored).\n", mipmap);
/* Extract pixel format information, considering additional elements
* in reserved1 and reserved2. */
ret = parse_pixel_format(avctx);
if (ret < 0)
return ret;
ret = ff_get_buffer(avctx, frame, 0);
if (ret < 0)
return ret;
if (ctx->compressed) {
/* Use the decompress function on the texture, one block per thread. */
ctx->tex_data = gbc->buffer;
blocks = avctx->coded_width * avctx->coded_height / (TEXTURE_BLOCK_W * TEXTURE_BLOCK_H);
avctx->execute2(avctx, decompress_texture_thread, frame, NULL, blocks);
} else {
int linesize = av_image_get_linesize(avctx->pix_fmt, frame->width, 0);
if (ctx->paletted) {
/* Use the first 1024 bytes as palette, then copy the rest. */
bytestream2_get_buffer(gbc, frame->data[1], 256 * 4);
frame->palette_has_changed = 1;
}
av_image_copy_plane(frame->data[0], frame->linesize[0],
gbc->buffer, linesize,
linesize, frame->height);
}
/* Run any post processing here if needed. */
if (avctx->pix_fmt == AV_PIX_FMT_RGBA || avctx->pix_fmt == AV_PIX_FMT_YA8)
run_postproc(avctx, frame);
/* Frame is ready to be output. */
frame->pict_type = AV_PICTURE_TYPE_I;
frame->key_frame = 1;
*got_frame = 1;
return avpkt->size;
}
AVCodec ff_dds_decoder = {
.name = "dds",
.long_name = NULL_IF_CONFIG_SMALL("DirectDraw Surface image decoder"),
.type = AVMEDIA_TYPE_VIDEO,
.id = AV_CODEC_ID_DDS,
.decode = dds_decode,
.priv_data_size = sizeof(DDSContext),
.capabilities = CODEC_CAP_DR1 | CODEC_CAP_SLICE_THREADS,
.caps_internal = FF_CODEC_CAP_INIT_THREADSAFE
};

View File

@ -29,7 +29,7 @@
#include "libavutil/version.h"
#define LIBAVCODEC_VERSION_MAJOR 56
#define LIBAVCODEC_VERSION_MINOR 27
#define LIBAVCODEC_VERSION_MINOR 28
#define LIBAVCODEC_VERSION_MICRO 0
#define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \

View File

@ -44,6 +44,7 @@ static const IdStrMap img_tags[] = {
{ AV_CODEC_ID_PBM, "pbm" },
{ AV_CODEC_ID_PAM, "pam" },
{ AV_CODEC_ID_ALIAS_PIX, "pix" },
{ AV_CODEC_ID_DDS, "dds" },
{ AV_CODEC_ID_MPEG1VIDEO, "mpg1-img" },
{ AV_CODEC_ID_MPEG2VIDEO, "mpg2-img" },
{ AV_CODEC_ID_MPEG4, "mpg4-img" },

View File

@ -30,6 +30,18 @@ fate-brenderpix: $(FATE_BRENDERPIX-yes)
FATE_SAMPLES_AVCONV-$(call PARSERDEMDEC, BMP, IMAGE2PIPE, BMP) += fate-bmpparser
fate-bmpparser: CMD = framecrc -f image2pipe -i $(TARGET_SAMPLES)/bmp/libav_4x_concat.bmp -pix_fmt rgb24
define FATE_IMGSUITE_DDS
FATE_DDS += fate-dds-$(1)
fate-dds-$(1): CMD = framecrc -i $(TARGET_SAMPLES)/dds/libav_$(1).dds -sws_flags +accurate_rnd+bitexact -pix_fmt rgba
endef
DDS_FMT = argb argb-aexp dx10-bc1 dx10-bc1a dx10-bc2 dx10-bc3 dx10-bc4 dx10-bc5 dxt1 dxt1a dxt1-normalmap dxt2 dxt3 dxt4 dxt5 dxt5-aexp dxt5-normalmap dxt5-normalmap-ati dxt5-rbxg dxt5-rgxb dxt5-rxbg dxt5-rxgb dxt5-xgbr dxt5-xgxr dxt5-xrbg dxt5-ycocg dxt5-ycocg-scaled pal pal-ati rgb16 rgb24 rgtc1s rgtc1u rgtc2s rgtc2u rgtc2u-xy uyvy xbgr xrgb y ya ycocg yuyv
$(foreach FMT,$(DDS_FMT),$(eval $(call FATE_IMGSUITE_DDS,$(FMT))))
FATE_DDS-$(call DEMDEC, IMAGE2, DDS) += $(FATE_DDS)
FATE_SAMPLES_AVCONV += $(FATE_DDS-yes)
fate-dds: $(FATE_DDS-yes)
FATE_SAMPLES_AVCONV-$(call DEMDEC, IMAGE2, DPX) += fate-dpx
fate-dpx: CMD = framecrc -i $(TARGET_SAMPLES)/dpx/lighthouse_rgb48.dpx

2
tests/ref/fate/dds-argb Normal file
View File

@ -0,0 +1,2 @@
#tb 0: 1/25
0, 0, 0, 1, 56320, 0xfcaa920b

View File

@ -0,0 +1,2 @@
#tb 0: 1/25
0, 0, 0, 1, 16384, 0xfdd37c43

View File

@ -0,0 +1,2 @@
#tb 0: 1/25
0, 0, 0, 1, 16384, 0xed2f850f

View File

@ -0,0 +1,2 @@
#tb 0: 1/25
0, 0, 0, 1, 16384, 0x473b484b

View File

@ -0,0 +1,2 @@
#tb 0: 1/25
0, 0, 0, 1, 16384, 0x2bce3baa

View File

@ -0,0 +1,2 @@
#tb 0: 1/25
0, 0, 0, 1, 16384, 0xb91d3c62

View File

@ -0,0 +1,2 @@
#tb 0: 1/25
0, 0, 0, 1, 16384, 0xfdfd1751

View File

@ -0,0 +1,2 @@
#tb 0: 1/25
0, 0, 0, 1, 16384, 0x79bd207e

2
tests/ref/fate/dds-dxt1 Normal file
View File

@ -0,0 +1,2 @@
#tb 0: 1/25
0, 0, 0, 1, 32768, 0xf52adc80

View File

@ -0,0 +1,2 @@
#tb 0: 1/25
0, 0, 0, 1, 16384, 0x2b411855

2
tests/ref/fate/dds-dxt1a Normal file
View File

@ -0,0 +1,2 @@
#tb 0: 1/25
0, 0, 0, 1, 16384, 0xcabb50ea

2
tests/ref/fate/dds-dxt2 Normal file
View File

@ -0,0 +1,2 @@
#tb 0: 1/25
0, 0, 0, 1, 16384, 0x11cebeb0

2
tests/ref/fate/dds-dxt3 Normal file
View File

@ -0,0 +1,2 @@
#tb 0: 1/25
0, 0, 0, 1, 16384, 0x729b74ba

2
tests/ref/fate/dds-dxt4 Normal file
View File

@ -0,0 +1,2 @@
#tb 0: 1/25
0, 0, 0, 1, 16384, 0x31aaacd6

2
tests/ref/fate/dds-dxt5 Normal file
View File

@ -0,0 +1,2 @@
#tb 0: 1/25
0, 0, 0, 1, 16384, 0xfd3166aa

View File

@ -0,0 +1,2 @@
#tb 0: 1/25
0, 0, 0, 1, 16384, 0x3c987914

View File

@ -0,0 +1,2 @@
#tb 0: 1/25
0, 0, 0, 1, 16384, 0xd29b1ea1

View File

@ -0,0 +1,2 @@
#tb 0: 1/25
0, 0, 0, 1, 16384, 0x54cb212f

View File

@ -0,0 +1,2 @@
#tb 0: 1/25
0, 0, 0, 1, 16384, 0x37588d12

View File

@ -0,0 +1,2 @@
#tb 0: 1/25
0, 0, 0, 1, 16384, 0xfb6b91cf

View File

@ -0,0 +1,2 @@
#tb 0: 1/25
0, 0, 0, 1, 16384, 0xc6cb8cbe

View File

@ -0,0 +1,2 @@
#tb 0: 1/25
0, 0, 0, 1, 16384, 0xb67d936a

View File

@ -0,0 +1,2 @@
#tb 0: 1/25
0, 0, 0, 1, 16384, 0xb0e69293

View File

@ -0,0 +1,2 @@
#tb 0: 1/25
0, 0, 0, 1, 16384, 0xa93de1ad

View File

@ -0,0 +1,2 @@
#tb 0: 1/25
0, 0, 0, 1, 16384, 0xf20d8b69

View File

@ -0,0 +1,2 @@
#tb 0: 1/25
0, 0, 0, 1, 16384, 0x0d0c8d20

View File

@ -0,0 +1,2 @@
#tb 0: 1/25
0, 0, 0, 1, 16384, 0x5a089973

2
tests/ref/fate/dds-pal Normal file
View File

@ -0,0 +1,2 @@
#tb 0: 1/25
0, 0, 0, 1, 65536, 0xbffee5cd

View File

@ -0,0 +1,2 @@
#tb 0: 1/25
0, 0, 0, 1, 16384, 0x23068060

2
tests/ref/fate/dds-rgb16 Normal file
View File

@ -0,0 +1,2 @@
#tb 0: 1/25
0, 0, 0, 1, 32768, 0x7991785d

2
tests/ref/fate/dds-rgb24 Normal file
View File

@ -0,0 +1,2 @@
#tb 0: 1/25
0, 0, 0, 1, 32768, 0xcf0f7833

View File

@ -0,0 +1,2 @@
#tb 0: 1/25
0, 0, 0, 1, 16384, 0xcda652a4

View File

@ -0,0 +1,2 @@
#tb 0: 1/25
0, 0, 0, 1, 16384, 0x629a1b44

View File

@ -0,0 +1,2 @@
#tb 0: 1/25
0, 0, 0, 1, 16384, 0xa0135ca8

View File

@ -0,0 +1,2 @@
#tb 0: 1/25
0, 0, 0, 1, 16384, 0x38cb221a

View File

@ -0,0 +1,2 @@
#tb 0: 1/25
0, 0, 0, 1, 16384, 0x79bd207e

2
tests/ref/fate/dds-uyvy Normal file
View File

@ -0,0 +1,2 @@
#tb 0: 1/25
0, 0, 0, 1, 16384, 0xd579a59e

2
tests/ref/fate/dds-xbgr Normal file
View File

@ -0,0 +1,2 @@
#tb 0: 1/25
0, 0, 0, 1, 16384, 0x702f6a28

2
tests/ref/fate/dds-xrgb Normal file
View File

@ -0,0 +1,2 @@
#tb 0: 1/25
0, 0, 0, 1, 16384, 0x702f6a28

2
tests/ref/fate/dds-y Normal file
View File

@ -0,0 +1,2 @@
#tb 0: 1/25
0, 0, 0, 1, 32768, 0xc51add1c

2
tests/ref/fate/dds-ya Normal file
View File

@ -0,0 +1,2 @@
#tb 0: 1/25
0, 0, 0, 1, 56320, 0xca81315f

2
tests/ref/fate/dds-ycocg Normal file
View File

@ -0,0 +1,2 @@
#tb 0: 1/25
0, 0, 0, 1, 16384, 0x4cb08d0a

2
tests/ref/fate/dds-yuyv Normal file
View File

@ -0,0 +1,2 @@
#tb 0: 1/25
0, 0, 0, 1, 16384, 0xd579a59e