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.

245 lines
6.0KB

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