annotate s3tc.c @ 7352:c2318e551ff5 libavcodec

When picking a "high utility centroid" do not pick one that has no corresponding points. Not only it is the worst possible pick, but also the code was written without this case in mind.
author vitor
date Wed, 23 Jul 2008 03:55:37 +0000
parents 23c0704067f6
children 2acf0ae7b041
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4933
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
1 /*
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
2 * S3 Texture Compression (S3TC) decoding functions
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
3 * Copyright (c) 2007 by Ivo van Poorten
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
4 *
5214
470601203f44 Group all copyright and author notices together.
diego
parents: 4933
diff changeset
5 * see also: http://wiki.multimedia.cx/index.php?title=S3TC
470601203f44 Group all copyright and author notices together.
diego
parents: 4933
diff changeset
6 *
4933
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
7 * This file is part of FFmpeg.
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
8 *
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
9 * FFmpeg is free software; you can redistribute it and/or
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
10 * modify it under the terms of the GNU Lesser General Public
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
11 * License as published by the Free Software Foundation; either
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
12 * version 2.1 of the License, or (at your option) any later version.
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
13 *
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
14 * FFmpeg is distributed in the hope that it will be useful,
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
17 * Lesser General Public License for more details.
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
18 *
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
19 * You should have received a copy of the GNU Lesser General Public
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
20 * License along with FFmpeg; if not, write to the Free Software
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
22 */
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
23
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
24 #include "avcodec.h"
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
25 #include "s3tc.h"
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
26
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
27 static inline void dxt1_decode_pixels(const uint8_t *s, uint32_t *d,
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
28 unsigned int qstride, unsigned int flag,
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
29 uint64_t alpha) {
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
30 unsigned int x, y, c0, c1, a = (!flag * 255) << 24;
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
31 unsigned int rb0, rb1, rb2, rb3, g0, g1, g2, g3;
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
32 uint32_t colors[4], pixels;
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
33
6306
23c0704067f6 use AV_RL* instead of le2me_*
aurel
parents: 5214
diff changeset
34 c0 = AV_RL16(s);
23c0704067f6 use AV_RL* instead of le2me_*
aurel
parents: 5214
diff changeset
35 c1 = AV_RL16(s+2);
4933
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
36
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
37 rb0 = (c0<<3 | c0<<8) & 0xf800f8;
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
38 rb1 = (c1<<3 | c1<<8) & 0xf800f8;
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
39 rb0 += (rb0>>5) & 0x070007;
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
40 rb1 += (rb1>>5) & 0x070007;
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
41 g0 = (c0 <<5) & 0x00fc00;
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
42 g1 = (c1 <<5) & 0x00fc00;
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
43 g0 += (g0 >>6) & 0x000300;
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
44 g1 += (g1 >>6) & 0x000300;
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
45
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
46 colors[0] = rb0 + g0 + a;
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
47 colors[1] = rb1 + g1 + a;
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
48
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
49 if (c0 > c1 || flag) {
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
50 rb2 = (((2*rb0+rb1) * 21) >> 6) & 0xff00ff;
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
51 rb3 = (((2*rb1+rb0) * 21) >> 6) & 0xff00ff;
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
52 g2 = (((2*g0 +g1 ) * 21) >> 6) & 0x00ff00;
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
53 g3 = (((2*g1 +g0 ) * 21) >> 6) & 0x00ff00;
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
54 colors[3] = rb3 + g3 + a;
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
55 } else {
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
56 rb2 = ((rb0+rb1) >> 1) & 0xff00ff;
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
57 g2 = ((g0 +g1 ) >> 1) & 0x00ff00;
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
58 colors[3] = 0;
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
59 }
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
60
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
61 colors[2] = rb2 + g2 + a;
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
62
6306
23c0704067f6 use AV_RL* instead of le2me_*
aurel
parents: 5214
diff changeset
63 pixels = AV_RL32(s+4);
4933
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
64 for (y=0; y<4; y++) {
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
65 for (x=0; x<4; x++) {
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
66 a = (alpha & 0x0f) << 28;
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
67 a += a >> 4;
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
68 d[x] = a + colors[pixels&3];
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
69 pixels >>= 2;
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
70 alpha >>= 4;
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
71 }
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
72 d += qstride;
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
73 }
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
74 }
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
75
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
76 void ff_decode_dxt1(const uint8_t *s, uint8_t *dst,
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
77 const unsigned int w, const unsigned int h,
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
78 const unsigned int stride) {
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
79 unsigned int bx, by, qstride = stride/4;
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
80 uint32_t *d = (uint32_t *) dst;
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
81
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
82 for (by=0; by < h/4; by++, d += stride-w)
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
83 for (bx=0; bx < w/4; bx++, s+=8, d+=4)
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
84 dxt1_decode_pixels(s, d, qstride, 0, 0LL);
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
85 }
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
86
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
87 void ff_decode_dxt3(const uint8_t *s, uint8_t *dst,
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
88 const unsigned int w, const unsigned int h,
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
89 const unsigned int stride) {
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
90 unsigned int bx, by, qstride = stride/4;
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
91 uint32_t *d = (uint32_t *) dst;
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
92
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
93 for (by=0; by < h/4; by++, d += stride-w)
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
94 for (bx=0; bx < w/4; bx++, s+=16, d+=4)
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
95 dxt1_decode_pixels(s+8, d, qstride, 1, AV_RL64(s));
ed67837533b0 generic S3TC DXT1 and DXT3 decoding functions
ivo
parents:
diff changeset
96 }