2007-05-07 15:15:24 +02:00
|
|
|
/*
|
|
|
|
* S3 Texture Compression (S3TC) decoding functions
|
|
|
|
* Copyright (c) 2007 by Ivo van Poorten
|
|
|
|
*
|
2007-07-05 12:37:29 +02:00
|
|
|
* see also: http://wiki.multimedia.cx/index.php?title=S3TC
|
|
|
|
*
|
2007-05-07 15:15:24 +02:00
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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 FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "avcodec.h"
|
2013-09-10 08:30:54 +02:00
|
|
|
#include "bytestream.h"
|
2007-05-07 15:15:24 +02:00
|
|
|
#include "s3tc.h"
|
|
|
|
|
2012-03-12 18:37:02 +01:00
|
|
|
static inline void dxt1_decode_pixels(GetByteContext *gb, uint32_t *d,
|
2015-05-07 17:17:10 +02:00
|
|
|
unsigned int w, unsigned int h,
|
2007-05-07 15:15:24 +02:00
|
|
|
unsigned int qstride, unsigned int flag,
|
|
|
|
uint64_t alpha) {
|
2011-11-26 17:51:44 +01:00
|
|
|
unsigned int x, y, c0, c1, a = (!flag * 255u) << 24;
|
2007-05-07 15:15:24 +02:00
|
|
|
unsigned int rb0, rb1, rb2, rb3, g0, g1, g2, g3;
|
|
|
|
uint32_t colors[4], pixels;
|
|
|
|
|
2012-03-12 18:37:02 +01:00
|
|
|
c0 = bytestream2_get_le16(gb);
|
|
|
|
c1 = bytestream2_get_le16(gb);
|
2007-05-07 15:15:24 +02:00
|
|
|
|
|
|
|
rb0 = (c0<<3 | c0<<8) & 0xf800f8;
|
|
|
|
rb1 = (c1<<3 | c1<<8) & 0xf800f8;
|
|
|
|
rb0 += (rb0>>5) & 0x070007;
|
|
|
|
rb1 += (rb1>>5) & 0x070007;
|
|
|
|
g0 = (c0 <<5) & 0x00fc00;
|
|
|
|
g1 = (c1 <<5) & 0x00fc00;
|
|
|
|
g0 += (g0 >>6) & 0x000300;
|
|
|
|
g1 += (g1 >>6) & 0x000300;
|
|
|
|
|
|
|
|
colors[0] = rb0 + g0 + a;
|
|
|
|
colors[1] = rb1 + g1 + a;
|
|
|
|
|
|
|
|
if (c0 > c1 || flag) {
|
|
|
|
rb2 = (((2*rb0+rb1) * 21) >> 6) & 0xff00ff;
|
|
|
|
rb3 = (((2*rb1+rb0) * 21) >> 6) & 0xff00ff;
|
|
|
|
g2 = (((2*g0 +g1 ) * 21) >> 6) & 0x00ff00;
|
|
|
|
g3 = (((2*g1 +g0 ) * 21) >> 6) & 0x00ff00;
|
|
|
|
colors[3] = rb3 + g3 + a;
|
|
|
|
} else {
|
|
|
|
rb2 = ((rb0+rb1) >> 1) & 0xff00ff;
|
|
|
|
g2 = ((g0 +g1 ) >> 1) & 0x00ff00;
|
|
|
|
colors[3] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
colors[2] = rb2 + g2 + a;
|
|
|
|
|
2012-03-12 18:37:02 +01:00
|
|
|
pixels = bytestream2_get_le32(gb);
|
2015-05-07 17:17:10 +02:00
|
|
|
for (y=0; y<h; y++) {
|
|
|
|
for (x=0; x<w; x++) {
|
2007-05-07 15:15:24 +02:00
|
|
|
a = (alpha & 0x0f) << 28;
|
|
|
|
a += a >> 4;
|
|
|
|
d[x] = a + colors[pixels&3];
|
|
|
|
pixels >>= 2;
|
|
|
|
alpha >>= 4;
|
|
|
|
}
|
2015-05-07 17:17:10 +02:00
|
|
|
for (; x<4; x++)
|
|
|
|
pixels >>= 2;
|
2007-05-07 15:15:24 +02:00
|
|
|
d += qstride;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-12 18:37:02 +01:00
|
|
|
void ff_decode_dxt1(GetByteContext *gb, uint8_t *dst,
|
2007-05-07 15:15:24 +02:00
|
|
|
const unsigned int w, const unsigned int h,
|
|
|
|
const unsigned int stride) {
|
2015-05-07 17:17:10 +02:00
|
|
|
unsigned int x, y, qstride = stride/4;
|
2007-05-07 15:15:24 +02:00
|
|
|
uint32_t *d = (uint32_t *) dst;
|
|
|
|
|
2015-05-07 17:17:10 +02:00
|
|
|
for (y=0; y < h; y += 4, d += stride-w)
|
|
|
|
for (x = 0; x < w; d += FFMIN(4, w-x), x += 4)
|
|
|
|
dxt1_decode_pixels(gb, d, FFMIN(4, w-x), FFMIN(4, h-y), qstride, 0, 0LL);
|
2007-05-07 15:15:24 +02:00
|
|
|
}
|
|
|
|
|
2012-03-12 18:37:02 +01:00
|
|
|
void ff_decode_dxt3(GetByteContext *gb, uint8_t *dst,
|
2007-05-07 15:15:24 +02:00
|
|
|
const unsigned int w, const unsigned int h,
|
|
|
|
const unsigned int stride) {
|
2015-05-07 17:17:10 +02:00
|
|
|
unsigned int x, y, qstride = stride/4;
|
2007-05-07 15:15:24 +02:00
|
|
|
uint32_t *d = (uint32_t *) dst;
|
|
|
|
|
2015-05-07 17:17:10 +02:00
|
|
|
for (y=0; y < h; y += 4, d += stride-w)
|
|
|
|
for (x = 0; x < w; d += FFMIN(4, w-x), x += 4)
|
|
|
|
dxt1_decode_pixels(gb, d, FFMIN(4, w-x), FFMIN(4, h-y), qstride, 1, bytestream2_get_le64(gb));
|
2007-05-07 15:15:24 +02:00
|
|
|
}
|