You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

236 lines
5.9KB

  1. /*
  2. * Simple IDCT
  3. *
  4. * Copyright (c) 2001 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * This file is part of Libav.
  7. *
  8. * Libav is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * Libav is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with Libav; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * simpleidct in C.
  25. */
  26. #include "libavutil/intreadwrite.h"
  27. #include "avcodec.h"
  28. #include "mathops.h"
  29. #include "simple_idct.h"
  30. #define BIT_DEPTH 8
  31. #include "simple_idct_template.c"
  32. #undef BIT_DEPTH
  33. #define BIT_DEPTH 10
  34. #include "simple_idct_template.c"
  35. #undef BIT_DEPTH
  36. /* 2x4x8 idct */
  37. #define CN_SHIFT 12
  38. #define C_FIX(x) ((int)((x) * (1 << CN_SHIFT) + 0.5))
  39. #define C1 C_FIX(0.6532814824)
  40. #define C2 C_FIX(0.2705980501)
  41. /* row idct is multiple by 16 * sqrt(2.0), col idct4 is normalized,
  42. and the butterfly must be multiplied by 0.5 * sqrt(2.0) */
  43. #define C_SHIFT (4+1+12)
  44. static inline void idct4col_put(uint8_t *dest, ptrdiff_t line_size, const int16_t *col)
  45. {
  46. int c0, c1, c2, c3, a0, a1, a2, a3;
  47. a0 = col[8*0];
  48. a1 = col[8*2];
  49. a2 = col[8*4];
  50. a3 = col[8*6];
  51. c0 = ((a0 + a2) << (CN_SHIFT - 1)) + (1 << (C_SHIFT - 1));
  52. c2 = ((a0 - a2) << (CN_SHIFT - 1)) + (1 << (C_SHIFT - 1));
  53. c1 = a1 * C1 + a3 * C2;
  54. c3 = a1 * C2 - a3 * C1;
  55. dest[0] = av_clip_uint8((c0 + c1) >> C_SHIFT);
  56. dest += line_size;
  57. dest[0] = av_clip_uint8((c2 + c3) >> C_SHIFT);
  58. dest += line_size;
  59. dest[0] = av_clip_uint8((c2 - c3) >> C_SHIFT);
  60. dest += line_size;
  61. dest[0] = av_clip_uint8((c0 - c1) >> C_SHIFT);
  62. }
  63. #define BF(k) \
  64. {\
  65. int a0, a1;\
  66. a0 = ptr[k];\
  67. a1 = ptr[8 + k];\
  68. ptr[k] = a0 + a1;\
  69. ptr[8 + k] = a0 - a1;\
  70. }
  71. /* only used by DV codec. The input must be interlaced. 128 is added
  72. to the pixels before clamping to avoid systematic error
  73. (1024*sqrt(2)) offset would be needed otherwise. */
  74. /* XXX: I think a 1.0/sqrt(2) normalization should be needed to
  75. compensate the extra butterfly stage - I don't have the full DV
  76. specification */
  77. void ff_simple_idct248_put(uint8_t *dest, ptrdiff_t line_size, int16_t *block)
  78. {
  79. int i;
  80. int16_t *ptr;
  81. /* butterfly */
  82. ptr = block;
  83. for(i=0;i<4;i++) {
  84. BF(0);
  85. BF(1);
  86. BF(2);
  87. BF(3);
  88. BF(4);
  89. BF(5);
  90. BF(6);
  91. BF(7);
  92. ptr += 2 * 8;
  93. }
  94. /* IDCT8 on each line */
  95. for(i=0; i<8; i++) {
  96. idctRowCondDC_8(block + i*8, 0);
  97. }
  98. /* IDCT4 and store */
  99. for(i=0;i<8;i++) {
  100. idct4col_put(dest + i, 2 * line_size, block + i);
  101. idct4col_put(dest + line_size + i, 2 * line_size, block + 8 + i);
  102. }
  103. }
  104. /* 8x4 & 4x8 WMV2 IDCT */
  105. #undef CN_SHIFT
  106. #undef C_SHIFT
  107. #undef C_FIX
  108. #undef C1
  109. #undef C2
  110. #define CN_SHIFT 12
  111. #define C_FIX(x) ((int)((x) * 1.414213562 * (1 << CN_SHIFT) + 0.5))
  112. #define C1 C_FIX(0.6532814824)
  113. #define C2 C_FIX(0.2705980501)
  114. #define C3 C_FIX(0.5)
  115. #define C_SHIFT (4+1+12)
  116. static inline void idct4col_add(uint8_t *dest, ptrdiff_t line_size, const int16_t *col)
  117. {
  118. int c0, c1, c2, c3, a0, a1, a2, a3;
  119. a0 = col[8*0];
  120. a1 = col[8*1];
  121. a2 = col[8*2];
  122. a3 = col[8*3];
  123. c0 = (a0 + a2)*C3 + (1 << (C_SHIFT - 1));
  124. c2 = (a0 - a2)*C3 + (1 << (C_SHIFT - 1));
  125. c1 = a1 * C1 + a3 * C2;
  126. c3 = a1 * C2 - a3 * C1;
  127. dest[0] = av_clip_uint8(dest[0] + ((c0 + c1) >> C_SHIFT));
  128. dest += line_size;
  129. dest[0] = av_clip_uint8(dest[0] + ((c2 + c3) >> C_SHIFT));
  130. dest += line_size;
  131. dest[0] = av_clip_uint8(dest[0] + ((c2 - c3) >> C_SHIFT));
  132. dest += line_size;
  133. dest[0] = av_clip_uint8(dest[0] + ((c0 - c1) >> C_SHIFT));
  134. }
  135. #define RN_SHIFT 15
  136. #define R_FIX(x) ((int)((x) * 1.414213562 * (1 << RN_SHIFT) + 0.5))
  137. #define R1 R_FIX(0.6532814824)
  138. #define R2 R_FIX(0.2705980501)
  139. #define R3 R_FIX(0.5)
  140. #define R_SHIFT 11
  141. static inline void idct4row(int16_t *row)
  142. {
  143. int c0, c1, c2, c3, a0, a1, a2, a3;
  144. a0 = row[0];
  145. a1 = row[1];
  146. a2 = row[2];
  147. a3 = row[3];
  148. c0 = (a0 + a2)*R3 + (1 << (R_SHIFT - 1));
  149. c2 = (a0 - a2)*R3 + (1 << (R_SHIFT - 1));
  150. c1 = a1 * R1 + a3 * R2;
  151. c3 = a1 * R2 - a3 * R1;
  152. row[0]= (c0 + c1) >> R_SHIFT;
  153. row[1]= (c2 + c3) >> R_SHIFT;
  154. row[2]= (c2 - c3) >> R_SHIFT;
  155. row[3]= (c0 - c1) >> R_SHIFT;
  156. }
  157. void ff_simple_idct84_add(uint8_t *dest, ptrdiff_t line_size, int16_t *block)
  158. {
  159. int i;
  160. /* IDCT8 on each line */
  161. for(i=0; i<4; i++) {
  162. idctRowCondDC_8(block + i*8, 0);
  163. }
  164. /* IDCT4 and store */
  165. for(i=0;i<8;i++) {
  166. idct4col_add(dest + i, line_size, block + i);
  167. }
  168. }
  169. void ff_simple_idct48_add(uint8_t *dest, ptrdiff_t line_size, int16_t *block)
  170. {
  171. int i;
  172. /* IDCT4 on each line */
  173. for(i=0; i<8; i++) {
  174. idct4row(block + i*8);
  175. }
  176. /* IDCT8 and store */
  177. for(i=0; i<4; i++){
  178. idctSparseColAdd_8(dest + i, line_size, block + i);
  179. }
  180. }
  181. void ff_simple_idct44_add(uint8_t *dest, ptrdiff_t line_size, int16_t *block)
  182. {
  183. int i;
  184. /* IDCT4 on each line */
  185. for(i=0; i<4; i++) {
  186. idct4row(block + i*8);
  187. }
  188. /* IDCT4 and store */
  189. for(i=0; i<4; i++){
  190. idct4col_add(dest + i, line_size, block + i);
  191. }
  192. }
  193. #if CONFIG_PRORES_DECODER
  194. void ff_prores_idct(int16_t *block, const int16_t *qmat)
  195. {
  196. int i;
  197. for (i = 0; i < 64; i++)
  198. block[i] *= qmat[i];
  199. for (i = 0; i < 8; i++)
  200. idctRowCondDC_10(block + i*8, 2);
  201. for (i = 0; i < 8; i++)
  202. idctSparseCol_10(block + i);
  203. }
  204. #endif /* CONFIG_PRORES_DECODER */