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.

235 lines
5.8KB

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