s3tc.c 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. #include "libavutil/intreadwrite.h"
  24. #include "avcodec.h"
  25. #include "s3tc.h"
  26. static inline void dxt1_decode_pixels(const uint8_t *s, uint32_t *d,
  27. unsigned int qstride, unsigned int flag,
  28. uint64_t alpha) {
  29. unsigned int x, y, c0, c1, a = (!flag * 255) << 24;
  30. unsigned int rb0, rb1, rb2, rb3, g0, g1, g2, g3;
  31. uint32_t colors[4], pixels;
  32. c0 = AV_RL16(s);
  33. c1 = AV_RL16(s+2);
  34. rb0 = (c0<<3 | c0<<8) & 0xf800f8;
  35. rb1 = (c1<<3 | c1<<8) & 0xf800f8;
  36. rb0 += (rb0>>5) & 0x070007;
  37. rb1 += (rb1>>5) & 0x070007;
  38. g0 = (c0 <<5) & 0x00fc00;
  39. g1 = (c1 <<5) & 0x00fc00;
  40. g0 += (g0 >>6) & 0x000300;
  41. g1 += (g1 >>6) & 0x000300;
  42. colors[0] = rb0 + g0 + a;
  43. colors[1] = rb1 + g1 + a;
  44. if (c0 > c1 || flag) {
  45. rb2 = (((2*rb0+rb1) * 21) >> 6) & 0xff00ff;
  46. rb3 = (((2*rb1+rb0) * 21) >> 6) & 0xff00ff;
  47. g2 = (((2*g0 +g1 ) * 21) >> 6) & 0x00ff00;
  48. g3 = (((2*g1 +g0 ) * 21) >> 6) & 0x00ff00;
  49. colors[3] = rb3 + g3 + a;
  50. } else {
  51. rb2 = ((rb0+rb1) >> 1) & 0xff00ff;
  52. g2 = ((g0 +g1 ) >> 1) & 0x00ff00;
  53. colors[3] = 0;
  54. }
  55. colors[2] = rb2 + g2 + a;
  56. pixels = AV_RL32(s+4);
  57. for (y=0; y<4; y++) {
  58. for (x=0; x<4; x++) {
  59. a = (alpha & 0x0f) << 28;
  60. a += a >> 4;
  61. d[x] = a + colors[pixels&3];
  62. pixels >>= 2;
  63. alpha >>= 4;
  64. }
  65. d += qstride;
  66. }
  67. }
  68. void ff_decode_dxt1(const uint8_t *s, uint8_t *dst,
  69. const unsigned int w, const unsigned int h,
  70. const unsigned int stride) {
  71. unsigned int bx, by, qstride = stride/4;
  72. uint32_t *d = (uint32_t *) dst;
  73. for (by=0; by < h/4; by++, d += stride-w)
  74. for (bx=0; bx < w/4; bx++, s+=8, d+=4)
  75. dxt1_decode_pixels(s, d, qstride, 0, 0LL);
  76. }
  77. void ff_decode_dxt3(const uint8_t *s, uint8_t *dst,
  78. const unsigned int w, const unsigned int h,
  79. const unsigned int stride) {
  80. unsigned int bx, by, qstride = stride/4;
  81. uint32_t *d = (uint32_t *) dst;
  82. for (by=0; by < h/4; by++, d += stride-w)
  83. for (bx=0; bx < w/4; bx++, s+=16, d+=4)
  84. dxt1_decode_pixels(s+8, d, qstride, 1, AV_RL64(s));
  85. }