]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blob - libavcodec/s3tc.c
Group all copyright and author notices together.
[frescor/ffmpeg.git] / libavcodec / s3tc.c
1 /*
2  * S3 Texture Compression (S3TC) decoding functions
3  * Copyright (c) 2007 by Ivo van Poorten
4  *
5  * see also: http://wiki.multimedia.cx/index.php?title=S3TC
6  *
7  * This file is part of FFmpeg.
8  *
9  * FFmpeg is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * FFmpeg is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with FFmpeg; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23
24 #include "avcodec.h"
25 #include "s3tc.h"
26
27 static inline void dxt1_decode_pixels(const uint8_t *s, uint32_t *d,
28                                       unsigned int qstride, unsigned int flag,
29                                       uint64_t alpha) {
30     unsigned int x, y, c0, c1, a = (!flag * 255) << 24;
31     unsigned int rb0, rb1, rb2, rb3, g0, g1, g2, g3;
32     uint32_t colors[4], pixels;
33
34     c0 = le2me_16(*(uint16_t *)(s));
35     c1 = le2me_16(*(uint16_t *)(s+2));
36
37     rb0  = (c0<<3 | c0<<8) & 0xf800f8;
38     rb1  = (c1<<3 | c1<<8) & 0xf800f8;
39     rb0 +=        (rb0>>5) & 0x070007;
40     rb1 +=        (rb1>>5) & 0x070007;
41     g0   =        (c0 <<5) & 0x00fc00;
42     g1   =        (c1 <<5) & 0x00fc00;
43     g0  +=        (g0 >>6) & 0x000300;
44     g1  +=        (g1 >>6) & 0x000300;
45
46     colors[0] = rb0 + g0 + a;
47     colors[1] = rb1 + g1 + a;
48
49     if (c0 > c1 || flag) {
50         rb2 = (((2*rb0+rb1) * 21) >> 6) & 0xff00ff;
51         rb3 = (((2*rb1+rb0) * 21) >> 6) & 0xff00ff;
52         g2  = (((2*g0 +g1 ) * 21) >> 6) & 0x00ff00;
53         g3  = (((2*g1 +g0 ) * 21) >> 6) & 0x00ff00;
54         colors[3] = rb3 + g3 + a;
55     } else {
56         rb2 = ((rb0+rb1) >> 1) & 0xff00ff;
57         g2  = ((g0 +g1 ) >> 1) & 0x00ff00;
58         colors[3] = 0;
59     }
60
61     colors[2] = rb2 + g2 + a;
62
63     pixels = le2me_32(*(uint32_t *)(s+4));
64     for (y=0; y<4; y++) {
65         for (x=0; x<4; x++) {
66             a        = (alpha & 0x0f) << 28;
67             a       += a >> 4;
68             d[x]     = a + colors[pixels&3];
69             pixels >>= 2;
70             alpha  >>= 4;
71         }
72         d += qstride;
73     }
74 }
75
76 void ff_decode_dxt1(const uint8_t *s, uint8_t *dst,
77                     const unsigned int w, const unsigned int h,
78                     const unsigned int stride) {
79     unsigned int bx, by, qstride = stride/4;
80     uint32_t *d = (uint32_t *) dst;
81
82     for (by=0; by < h/4; by++, d += stride-w)
83         for (bx=0; bx < w/4; bx++, s+=8, d+=4)
84             dxt1_decode_pixels(s, d, qstride, 0, 0LL);
85 }
86
87 void ff_decode_dxt3(const uint8_t *s, uint8_t *dst,
88                     const unsigned int w, const unsigned int h,
89                     const unsigned int stride) {
90     unsigned int bx, by, qstride = stride/4;
91     uint32_t *d = (uint32_t *) dst;
92
93     for (by=0; by < h/4; by++, d += stride-w)
94         for (bx=0; bx < w/4; bx++, s+=16, d+=4)
95             dxt1_decode_pixels(s+8, d, qstride, 1, AV_RL64(s));
96 }