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.

240 lines
5.9KB

  1. /*
  2. * Simple IDCT
  3. *
  4. * Copyright (c) 2001 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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. #define BIT_DEPTH 12
  37. #include "simple_idct_template.c"
  38. #undef BIT_DEPTH
  39. /* 2x4x8 idct */
  40. #define CN_SHIFT 12
  41. #define C_FIX(x) ((int)((x) * (1 << CN_SHIFT) + 0.5))
  42. #define C1 C_FIX(0.6532814824)
  43. #define C2 C_FIX(0.2705980501)
  44. /* row idct is multiple by 16 * sqrt(2.0), col idct4 is normalized,
  45. and the butterfly must be multiplied by 0.5 * sqrt(2.0) */
  46. #define C_SHIFT (4+1+12)
  47. static inline void idct4col_put(uint8_t *dest, int line_size, const int16_t *col)
  48. {
  49. int c0, c1, c2, c3, a0, a1, a2, a3;
  50. a0 = col[8*0];
  51. a1 = col[8*2];
  52. a2 = col[8*4];
  53. a3 = col[8*6];
  54. c0 = ((a0 + a2) << (CN_SHIFT - 1)) + (1 << (C_SHIFT - 1));
  55. c2 = ((a0 - a2) << (CN_SHIFT - 1)) + (1 << (C_SHIFT - 1));
  56. c1 = a1 * C1 + a3 * C2;
  57. c3 = a1 * C2 - a3 * C1;
  58. dest[0] = av_clip_uint8((c0 + c1) >> C_SHIFT);
  59. dest += line_size;
  60. dest[0] = av_clip_uint8((c2 + c3) >> C_SHIFT);
  61. dest += line_size;
  62. dest[0] = av_clip_uint8((c2 - c3) >> C_SHIFT);
  63. dest += line_size;
  64. dest[0] = av_clip_uint8((c0 - c1) >> C_SHIFT);
  65. }
  66. #define BF(k) \
  67. {\
  68. int a0, a1;\
  69. a0 = ptr[k];\
  70. a1 = ptr[8 + k];\
  71. ptr[k] = a0 + a1;\
  72. ptr[8 + k] = a0 - a1;\
  73. }
  74. /* only used by DV codec. The input must be interlaced. 128 is added
  75. to the pixels before clamping to avoid systematic error
  76. (1024*sqrt(2)) offset would be needed otherwise. */
  77. /* XXX: I think a 1.0/sqrt(2) normalization should be needed to
  78. compensate the extra butterfly stage - I don't have the full DV
  79. specification */
  80. void ff_simple_idct248_put(uint8_t *dest, int line_size, int16_t *block)
  81. {
  82. int i;
  83. int16_t *ptr;
  84. /* butterfly */
  85. ptr = block;
  86. for(i=0;i<4;i++) {
  87. BF(0);
  88. BF(1);
  89. BF(2);
  90. BF(3);
  91. BF(4);
  92. BF(5);
  93. BF(6);
  94. BF(7);
  95. ptr += 2 * 8;
  96. }
  97. /* IDCT8 on each line */
  98. for(i=0; i<8; i++) {
  99. idctRowCondDC_8(block + i*8, 0);
  100. }
  101. /* IDCT4 and store */
  102. for(i=0;i<8;i++) {
  103. idct4col_put(dest + i, 2 * line_size, block + i);
  104. idct4col_put(dest + line_size + i, 2 * line_size, block + 8 + i);
  105. }
  106. }
  107. /* 8x4 & 4x8 WMV2 IDCT */
  108. #undef CN_SHIFT
  109. #undef C_SHIFT
  110. #undef C_FIX
  111. #undef C1
  112. #undef C2
  113. #define CN_SHIFT 12
  114. #define C_FIX(x) ((int)((x) * 1.414213562 * (1 << CN_SHIFT) + 0.5))
  115. #define C1 C_FIX(0.6532814824)
  116. #define C2 C_FIX(0.2705980501)
  117. #define C3 C_FIX(0.5)
  118. #define C_SHIFT (4+1+12)
  119. static inline void idct4col_add(uint8_t *dest, int line_size, const int16_t *col)
  120. {
  121. int c0, c1, c2, c3, a0, a1, a2, a3;
  122. a0 = col[8*0];
  123. a1 = col[8*1];
  124. a2 = col[8*2];
  125. a3 = col[8*3];
  126. c0 = (a0 + a2)*C3 + (1 << (C_SHIFT - 1));
  127. c2 = (a0 - a2)*C3 + (1 << (C_SHIFT - 1));
  128. c1 = a1 * C1 + a3 * C2;
  129. c3 = a1 * C2 - a3 * C1;
  130. dest[0] = av_clip_uint8(dest[0] + ((c0 + c1) >> C_SHIFT));
  131. dest += line_size;
  132. dest[0] = av_clip_uint8(dest[0] + ((c2 + c3) >> C_SHIFT));
  133. dest += line_size;
  134. dest[0] = av_clip_uint8(dest[0] + ((c2 - c3) >> C_SHIFT));
  135. dest += line_size;
  136. dest[0] = av_clip_uint8(dest[0] + ((c0 - c1) >> C_SHIFT));
  137. }
  138. #define RN_SHIFT 15
  139. #define R_FIX(x) ((int)((x) * 1.414213562 * (1 << RN_SHIFT) + 0.5))
  140. #define R1 R_FIX(0.6532814824)
  141. #define R2 R_FIX(0.2705980501)
  142. #define R3 R_FIX(0.5)
  143. #define R_SHIFT 11
  144. static inline void idct4row(int16_t *row)
  145. {
  146. int c0, c1, c2, c3, a0, a1, a2, a3;
  147. a0 = row[0];
  148. a1 = row[1];
  149. a2 = row[2];
  150. a3 = row[3];
  151. c0 = (a0 + a2)*R3 + (1 << (R_SHIFT - 1));
  152. c2 = (a0 - a2)*R3 + (1 << (R_SHIFT - 1));
  153. c1 = a1 * R1 + a3 * R2;
  154. c3 = a1 * R2 - a3 * R1;
  155. row[0]= (c0 + c1) >> R_SHIFT;
  156. row[1]= (c2 + c3) >> R_SHIFT;
  157. row[2]= (c2 - c3) >> R_SHIFT;
  158. row[3]= (c0 - c1) >> R_SHIFT;
  159. }
  160. void ff_simple_idct84_add(uint8_t *dest, int line_size, int16_t *block)
  161. {
  162. int i;
  163. /* IDCT8 on each line */
  164. for(i=0; i<4; i++) {
  165. idctRowCondDC_8(block + i*8, 0);
  166. }
  167. /* IDCT4 and store */
  168. for(i=0;i<8;i++) {
  169. idct4col_add(dest + i, line_size, block + i);
  170. }
  171. }
  172. void ff_simple_idct48_add(uint8_t *dest, int line_size, int16_t *block)
  173. {
  174. int i;
  175. /* IDCT4 on each line */
  176. for(i=0; i<8; i++) {
  177. idct4row(block + i*8);
  178. }
  179. /* IDCT8 and store */
  180. for(i=0; i<4; i++){
  181. idctSparseColAdd_8(dest + i, line_size, block + i);
  182. }
  183. }
  184. void ff_simple_idct44_add(uint8_t *dest, int line_size, int16_t *block)
  185. {
  186. int i;
  187. /* IDCT4 on each line */
  188. for(i=0; i<4; i++) {
  189. idct4row(block + i*8);
  190. }
  191. /* IDCT4 and store */
  192. for(i=0; i<4; i++){
  193. idct4col_add(dest + i, line_size, block + i);
  194. }
  195. }
  196. void ff_prores_idct(int16_t *block, const int16_t *qmat)
  197. {
  198. int i;
  199. for (i = 0; i < 64; i++)
  200. block[i] *= qmat[i];
  201. for (i = 0; i < 8; i++)
  202. idctRowCondDC_10(block + i*8, 2);
  203. for (i = 0; i < 8; i++) {
  204. block[i] += 8192;
  205. idctSparseCol_10(block + i);
  206. }
  207. }