* DirectDraw Surface image decoder
* Copyright (C) 2015 Vittorio Giovara <vittorio.giovara@gmail.com>
*
- * This file is part of Libav.
+ * This file is part of FFmpeg.
*
- * Libav is free software; you can redistribute it and/or
+ * FFmpeg 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,
+ * FFmpeg 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
+ * License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdint.h>
+#include "libavutil/libm.h"
#include "libavutil/imgutils.h"
#include "avcodec.h"
DDS_SWIZZLE_XGBR,
DDS_SWIZZLE_XRBG,
DDS_SWIZZLE_XGXR,
-} DDSPostProc;
+};
enum DDSDXGIFormat {
DXGI_FORMAT_R16G16B16A16_TYPELESS = 9,
DXGI_FORMAT_B8G8R8A8_UNORM_SRGB = 91,
DXGI_FORMAT_B8G8R8X8_TYPELESS = 92,
DXGI_FORMAT_B8G8R8X8_UNORM_SRGB = 93,
-} DDSDXGIFormat;
+};
typedef struct DDSContext {
TextureDSPContext texdsp;
int compressed;
int paletted;
+ int bpp;
enum DDSPostProc postproc;
const uint8_t *tex_data; // Compressed texture
int tex_ratio; // Compression ratio
+ int slice_count; // Number of slices for threaded operations
/* Pointer to the selected compress or decompress function. */
int (*tex_funct)(uint8_t *dst, ptrdiff_t stride, const uint8_t *block);
{
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;
normal_map = flags & DDPF_NORMALMAP;
fourcc = bytestream2_get_le32(gbc);
- bpp = bytestream2_get_le32(gbc); // rgbbitcount
+ if (ctx->compressed && ctx->paletted) {
+ av_log(avctx, AV_LOG_WARNING,
+ "Disabling invalid palette flag for compressed dds.\n");
+ ctx->paletted = 0;
+ }
+
+ bpp = ctx->bpp = bytestream2_get_le32(gbc); // rgbbitcount
r = bytestream2_get_le32(gbc); // rbitmask
g = bytestream2_get_le32(gbc); // gbitmask
b = bytestream2_get_le32(gbc); // bbitmask
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);
- }
+ "r 0x%x g 0x%x b 0x%x a 0x%x\n", av_fourcc2str(fourcc), bpp, r, g, b, a);
+ if (gimp_tag)
+ av_log(avctx, AV_LOG_VERBOSE, "and GIMP-DDS tag %s\n", av_fourcc2str(gimp_tag));
if (ctx->compressed)
avctx->pix_fmt = AV_PIX_FMT_RGBA;
ctx->paletted = 1;
avctx->pix_fmt = AV_PIX_FMT_PAL8;
break;
+ case MKTAG('G', '1', ' ', ' '):
+ ctx->compressed = 0;
+ avctx->pix_fmt = AV_PIX_FMT_MONOBLACK;
+ break;
case MKTAG('D', 'X', '1', '0'):
/* DirectX 10 extra header */
dxgi = bytestream2_get_le32(gbc);
}
break;
default:
- av_log(avctx, AV_LOG_ERROR, "Unsupported %s fourcc.\n", buf);
+ av_log(avctx, AV_LOG_ERROR, "Unsupported %s fourcc.\n", av_fourcc2str(fourcc));
return AVERROR_INVALIDDATA;
}
} else if (ctx->paletted) {
return AVERROR_INVALIDDATA;
}
} else {
+ /* 4 bpp */
+ if (bpp == 4 && r == 0 && g == 0 && b == 0 && a == 0)
+ avctx->pix_fmt = AV_PIX_FMT_PAL8;
/* 8 bpp */
- if (bpp == 8 && r == 0xff && g == 0 && b == 0 && a == 0)
+ else if (bpp == 8 && r == 0xff && g == 0 && b == 0 && a == 0)
+ avctx->pix_fmt = AV_PIX_FMT_GRAY8;
+ else if (bpp == 8 && r == 0 && g == 0 && b == 0 && a == 0xff)
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 == 0xff00 && g == 0 && b == 0 && a == 0xff) {
+ avctx->pix_fmt = AV_PIX_FMT_YA8;
+ ctx->postproc = DDS_SWAP_ALPHA;
+ }
else if (bpp == 16 && r == 0xffff && g == 0 && b == 0 && a == 0)
avctx->pix_fmt = AV_PIX_FMT_GRAY16LE;
+ else if (bpp == 16 && r == 0x7c00 && g == 0x3e0 && b == 0x1f && a == 0)
+ avctx->pix_fmt = AV_PIX_FMT_RGB555LE;
+ else if (bpp == 16 && r == 0x7c00 && g == 0x3e0 && b == 0x1f && a == 0x8000)
+ avctx->pix_fmt = AV_PIX_FMT_RGB555LE; // alpha ignored
else if (bpp == 16 && r == 0xf800 && g == 0x7e0 && b == 0x1f && a == 0)
avctx->pix_fmt = AV_PIX_FMT_RGB565LE;
/* 24 bpp */
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
+ avctx->pix_fmt = AV_PIX_FMT_BGR0; // opaque
else if (bpp == 32 && r == 0xff && g == 0xff00 && b == 0xff0000 && a == 0)
- avctx->pix_fmt = AV_PIX_FMT_BGRA; // opaque
+ avctx->pix_fmt = AV_PIX_FMT_RGB0; // 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;
+ else if (bpp == 32 && r == 0xff && g == 0xff00 && b == 0xff0000 && a == 0xff000000)
+ avctx->pix_fmt = AV_PIX_FMT_RGBA;
/* give up */
else {
av_log(avctx, AV_LOG_ERROR, "Unknown pixel format "
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) {
}
static int decompress_texture_thread(AVCodecContext *avctx, void *arg,
- int block_nb, int thread_nb)
+ int slice, 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;
+ const uint8_t *d = ctx->tex_data;
+ int w_block = avctx->coded_width / TEXTURE_BLOCK_W;
+ int h_block = avctx->coded_height / TEXTURE_BLOCK_H;
+ int x, y;
+ int start_slice, end_slice;
+ int base_blocks_per_slice = h_block / ctx->slice_count;
+ int remainder_blocks = h_block % ctx->slice_count;
+
+ /* When the frame height (in blocks) doesn't divide evenly between the
+ * number of slices, spread the remaining blocks evenly between the first
+ * operations */
+ start_slice = slice * base_blocks_per_slice;
+ /* Add any extra blocks (one per slice) that have been added before this slice */
+ start_slice += FFMIN(slice, remainder_blocks);
+
+ end_slice = start_slice + base_blocks_per_slice;
+ /* Add an extra block if there are still remainder blocks to be accounted for */
+ if (slice < remainder_blocks)
+ end_slice++;
+
+ for (y = start_slice; y < end_slice; y++) {
+ uint8_t *p = frame->data[0] + y * frame->linesize[0] * TEXTURE_BLOCK_H;
+ int off = y * w_block;
+ for (x = 0; x < w_block; x++) {
+ ctx->tex_funct(p + x * 16, frame->linesize[0],
+ d + (off + x) * ctx->tex_ratio);
+ }
+ }
- ctx->tex_funct(p, frame->linesize[0], d);
return 0;
}
int d = (255 * 255 - x * x - y * y) / 2;
if (d > 0)
- z = rint(sqrtf(d));
+ z = lrint(sqrtf(d));
src[0] = x;
src[1] = y;
DDSContext *ctx = avctx->priv_data;
GetByteContext *gbc = &ctx->gbc;
AVFrame *frame = data;
- int blocks, mipmap;
+ int 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).",
+ av_log(avctx, AV_LOG_ERROR, "Frame is too small (%d).\n",
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.");
+ av_log(avctx, AV_LOG_ERROR, "Invalid DDS header.\n");
return AVERROR_INVALIDDATA;
}
return ret;
if (ctx->compressed) {
+ int size = (avctx->coded_height / TEXTURE_BLOCK_H) *
+ (avctx->coded_width / TEXTURE_BLOCK_W) * ctx->tex_ratio;
+ ctx->slice_count = av_clip(avctx->thread_count, 1,
+ avctx->coded_height / TEXTURE_BLOCK_H);
+
+ if (bytestream2_get_bytes_left(gbc) < size) {
+ av_log(avctx, AV_LOG_ERROR,
+ "Compressed Buffer is too small (%d < %d).\n",
+ bytestream2_get_bytes_left(gbc), size);
+ return AVERROR_INVALIDDATA;
+ }
+
/* 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);
+ avctx->execute2(avctx, decompress_texture_thread, frame, NULL, ctx->slice_count);
+ } else if (!ctx->paletted && ctx->bpp == 4 && avctx->pix_fmt == AV_PIX_FMT_PAL8) {
+ uint8_t *dst = frame->data[0];
+ int x, y, i;
+
+ /* Use the first 64 bytes as palette, then copy the rest. */
+ bytestream2_get_buffer(gbc, frame->data[1], 16 * 4);
+ for (i = 0; i < 16; i++) {
+ AV_WN32(frame->data[1] + i*4,
+ (frame->data[1][2+i*4]<<0)+
+ (frame->data[1][1+i*4]<<8)+
+ (frame->data[1][0+i*4]<<16)+
+ (frame->data[1][3+i*4]<<24)
+ );
+ }
+ frame->palette_has_changed = 1;
+
+ if (bytestream2_get_bytes_left(gbc) < frame->height * frame->width / 2) {
+ av_log(avctx, AV_LOG_ERROR, "Buffer is too small (%d < %d).\n",
+ bytestream2_get_bytes_left(gbc), frame->height * frame->width / 2);
+ return AVERROR_INVALIDDATA;
+ }
+
+ for (y = 0; y < frame->height; y++) {
+ for (x = 0; x < frame->width; x += 2) {
+ uint8_t val = bytestream2_get_byte(gbc);
+ dst[x ] = val & 0xF;
+ dst[x + 1] = val >> 4;
+ }
+ dst += frame->linesize[0];
+ }
} else {
int linesize = av_image_get_linesize(avctx->pix_fmt, frame->width, 0);
if (ctx->paletted) {
int i;
- uint32_t *p = (uint32_t *)frame->data[1];
-
/* Use the first 1024 bytes as palette, then copy the rest. */
+ bytestream2_get_buffer(gbc, frame->data[1], 256 * 4);
for (i = 0; i < 256; i++)
- p[i] = bytestream2_get_le32(gbc);
+ AV_WN32(frame->data[1] + i*4,
+ (frame->data[1][2+i*4]<<0)+
+ (frame->data[1][1+i*4]<<8)+
+ (frame->data[1][0+i*4]<<16)+
+ (frame->data[1][3+i*4]<<24)
+ );
frame->palette_has_changed = 1;
}
+ if (bytestream2_get_bytes_left(gbc) < frame->height * linesize) {
+ av_log(avctx, AV_LOG_ERROR, "Buffer is too small (%d < %d).\n",
+ bytestream2_get_bytes_left(gbc), frame->height * linesize);
+ return AVERROR_INVALIDDATA;
+ }
+
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)
+ if (ctx->postproc != DDS_NONE)
run_postproc(avctx, frame);
/* Frame is ready to be output. */
.id = AV_CODEC_ID_DDS,
.decode = dds_decode,
.priv_data_size = sizeof(DDSContext),
- .capabilities = CODEC_CAP_DR1 | CODEC_CAP_SLICE_THREADS,
+ .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_SLICE_THREADS,
.caps_internal = FF_CODEC_CAP_INIT_THREADSAFE
};