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.

270 lines
6.5KB

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