diff --git a/libavcodec/dds.c b/libavcodec/dds.c index 763371a3aa..43319a8441 100644 --- a/libavcodec/dds.c +++ b/libavcodec/dds.c @@ -102,6 +102,7 @@ typedef struct DDSContext { int compressed; int paletted; + int bpp; enum DDSPostProc postproc; const uint8_t *tex_data; // Compressed texture @@ -148,7 +149,7 @@ static int parse_pixel_format(AVCodecContext *avctx) ctx->paletted = 0; } - bpp = bytestream2_get_le32(gbc); // rgbbitcount + 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 @@ -354,8 +355,11 @@ static int parse_pixel_format(AVCodecContext *avctx) 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; @@ -676,6 +680,36 @@ static int dds_decode(AVCodecContext *avctx, void *data, /* Use the decompress function on the texture, one block per thread. */ ctx->tex_data = gbc->buffer; avctx->execute2(avctx, decompress_texture_thread, frame, NULL, ctx->slice_count); + } else if (!ctx->paletted && ctx->bpp == 4) { + 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);