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.

313 lines
8.9KB

  1. /*
  2. * Copyright (C) 2004 the ffmpeg project
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. /**
  19. * @file vp3dsp.c
  20. * Standard C DSP-oriented functions cribbed from the original VP3
  21. * source code.
  22. */
  23. #include "common.h"
  24. #include "avcodec.h"
  25. #include "dsputil.h"
  26. #include "vp3data.h"
  27. #define IdctAdjustBeforeShift 8
  28. #define xC1S7 64277
  29. #define xC2S6 60547
  30. #define xC3S5 54491
  31. #define xC4S4 46341
  32. #define xC5S3 36410
  33. #define xC6S2 25080
  34. #define xC7S1 12785
  35. static always_inline void idct(uint8_t *dst, int stride, int16_t *input, int type)
  36. {
  37. int16_t *ip = input;
  38. uint8_t *cm = cropTbl + MAX_NEG_CROP;
  39. int A_, B_, C_, D_, _Ad, _Bd, _Cd, _Dd, E_, F_, G_, H_;
  40. int _Ed, _Gd, _Add, _Bdd, _Fd, _Hd;
  41. int t1, t2;
  42. int i, j;
  43. /* Inverse DCT on the rows now */
  44. for (i = 0; i < 8; i++) {
  45. /* Check for non-zero values */
  46. if ( ip[0] | ip[1] | ip[2] | ip[3] | ip[4] | ip[5] | ip[6] | ip[7] ) {
  47. t1 = (int32_t)(xC1S7 * ip[1]);
  48. t2 = (int32_t)(xC7S1 * ip[7]);
  49. t1 >>= 16;
  50. t2 >>= 16;
  51. A_ = t1 + t2;
  52. t1 = (int32_t)(xC7S1 * ip[1]);
  53. t2 = (int32_t)(xC1S7 * ip[7]);
  54. t1 >>= 16;
  55. t2 >>= 16;
  56. B_ = t1 - t2;
  57. t1 = (int32_t)(xC3S5 * ip[3]);
  58. t2 = (int32_t)(xC5S3 * ip[5]);
  59. t1 >>= 16;
  60. t2 >>= 16;
  61. C_ = t1 + t2;
  62. t1 = (int32_t)(xC3S5 * ip[5]);
  63. t2 = (int32_t)(xC5S3 * ip[3]);
  64. t1 >>= 16;
  65. t2 >>= 16;
  66. D_ = t1 - t2;
  67. t1 = (int32_t)(xC4S4 * (A_ - C_));
  68. t1 >>= 16;
  69. _Ad = t1;
  70. t1 = (int32_t)(xC4S4 * (B_ - D_));
  71. t1 >>= 16;
  72. _Bd = t1;
  73. _Cd = A_ + C_;
  74. _Dd = B_ + D_;
  75. t1 = (int32_t)(xC4S4 * (ip[0] + ip[4]));
  76. t1 >>= 16;
  77. E_ = t1;
  78. t1 = (int32_t)(xC4S4 * (ip[0] - ip[4]));
  79. t1 >>= 16;
  80. F_ = t1;
  81. t1 = (int32_t)(xC2S6 * ip[2]);
  82. t2 = (int32_t)(xC6S2 * ip[6]);
  83. t1 >>= 16;
  84. t2 >>= 16;
  85. G_ = t1 + t2;
  86. t1 = (int32_t)(xC6S2 * ip[2]);
  87. t2 = (int32_t)(xC2S6 * ip[6]);
  88. t1 >>= 16;
  89. t2 >>= 16;
  90. H_ = t1 - t2;
  91. _Ed = E_ - G_;
  92. _Gd = E_ + G_;
  93. _Add = F_ + _Ad;
  94. _Bdd = _Bd - H_;
  95. _Fd = F_ - _Ad;
  96. _Hd = _Bd + H_;
  97. /* Final sequence of operations over-write original inputs. */
  98. ip[0] = _Gd + _Cd ;
  99. ip[7] = _Gd - _Cd ;
  100. ip[1] = _Add + _Hd;
  101. ip[2] = _Add - _Hd;
  102. ip[3] = _Ed + _Dd ;
  103. ip[4] = _Ed - _Dd ;
  104. ip[5] = _Fd + _Bdd;
  105. ip[6] = _Fd - _Bdd;
  106. }
  107. ip += 8; /* next row */
  108. }
  109. ip = input;
  110. for ( i = 0; i < 8; i++) {
  111. /* Check for non-zero values (bitwise or faster than ||) */
  112. if ( ip[1 * 8] | ip[2 * 8] | ip[3 * 8] |
  113. ip[4 * 8] | ip[5 * 8] | ip[6 * 8] | ip[7 * 8] ) {
  114. t1 = (int32_t)(xC1S7 * ip[1*8]);
  115. t2 = (int32_t)(xC7S1 * ip[7*8]);
  116. t1 >>= 16;
  117. t2 >>= 16;
  118. A_ = t1 + t2;
  119. t1 = (int32_t)(xC7S1 * ip[1*8]);
  120. t2 = (int32_t)(xC1S7 * ip[7*8]);
  121. t1 >>= 16;
  122. t2 >>= 16;
  123. B_ = t1 - t2;
  124. t1 = (int32_t)(xC3S5 * ip[3*8]);
  125. t2 = (int32_t)(xC5S3 * ip[5*8]);
  126. t1 >>= 16;
  127. t2 >>= 16;
  128. C_ = t1 + t2;
  129. t1 = (int32_t)(xC3S5 * ip[5*8]);
  130. t2 = (int32_t)(xC5S3 * ip[3*8]);
  131. t1 >>= 16;
  132. t2 >>= 16;
  133. D_ = t1 - t2;
  134. t1 = (int32_t)(xC4S4 * (A_ - C_));
  135. t1 >>= 16;
  136. _Ad = t1;
  137. t1 = (int32_t)(xC4S4 * (B_ - D_));
  138. t1 >>= 16;
  139. _Bd = t1;
  140. _Cd = A_ + C_;
  141. _Dd = B_ + D_;
  142. t1 = (int32_t)(xC4S4 * (ip[0*8] + ip[4*8]));
  143. t1 >>= 16;
  144. E_ = t1;
  145. t1 = (int32_t)(xC4S4 * (ip[0*8] - ip[4*8]));
  146. t1 >>= 16;
  147. F_ = t1;
  148. t1 = (int32_t)(xC2S6 * ip[2*8]);
  149. t2 = (int32_t)(xC6S2 * ip[6*8]);
  150. t1 >>= 16;
  151. t2 >>= 16;
  152. G_ = t1 + t2;
  153. t1 = (int32_t)(xC6S2 * ip[2*8]);
  154. t2 = (int32_t)(xC2S6 * ip[6*8]);
  155. t1 >>= 16;
  156. t2 >>= 16;
  157. H_ = t1 - t2;
  158. _Ed = E_ - G_;
  159. _Gd = E_ + G_;
  160. _Add = F_ + _Ad;
  161. _Bdd = _Bd - H_;
  162. _Fd = F_ - _Ad;
  163. _Hd = _Bd + H_;
  164. if(type==1){ //HACK
  165. _Gd += 16*128;
  166. _Add+= 16*128;
  167. _Ed += 16*128;
  168. _Fd += 16*128;
  169. }
  170. _Gd += IdctAdjustBeforeShift;
  171. _Add += IdctAdjustBeforeShift;
  172. _Ed += IdctAdjustBeforeShift;
  173. _Fd += IdctAdjustBeforeShift;
  174. /* Final sequence of operations over-write original inputs. */
  175. if(type==0){
  176. ip[0*8] = (_Gd + _Cd ) >> 4;
  177. ip[7*8] = (_Gd - _Cd ) >> 4;
  178. ip[1*8] = (_Add + _Hd ) >> 4;
  179. ip[2*8] = (_Add - _Hd ) >> 4;
  180. ip[3*8] = (_Ed + _Dd ) >> 4;
  181. ip[4*8] = (_Ed - _Dd ) >> 4;
  182. ip[5*8] = (_Fd + _Bdd ) >> 4;
  183. ip[6*8] = (_Fd - _Bdd ) >> 4;
  184. }else if(type==1){
  185. dst[0*stride] = cm[(_Gd + _Cd ) >> 4];
  186. dst[7*stride] = cm[(_Gd - _Cd ) >> 4];
  187. dst[1*stride] = cm[(_Add + _Hd ) >> 4];
  188. dst[2*stride] = cm[(_Add - _Hd ) >> 4];
  189. dst[3*stride] = cm[(_Ed + _Dd ) >> 4];
  190. dst[4*stride] = cm[(_Ed - _Dd ) >> 4];
  191. dst[5*stride] = cm[(_Fd + _Bdd ) >> 4];
  192. dst[6*stride] = cm[(_Fd - _Bdd ) >> 4];
  193. }else{
  194. dst[0*stride] = cm[dst[0*stride] + ((_Gd + _Cd ) >> 4)];
  195. dst[7*stride] = cm[dst[7*stride] + ((_Gd - _Cd ) >> 4)];
  196. dst[1*stride] = cm[dst[1*stride] + ((_Add + _Hd ) >> 4)];
  197. dst[2*stride] = cm[dst[2*stride] + ((_Add - _Hd ) >> 4)];
  198. dst[3*stride] = cm[dst[3*stride] + ((_Ed + _Dd ) >> 4)];
  199. dst[4*stride] = cm[dst[4*stride] + ((_Ed - _Dd ) >> 4)];
  200. dst[5*stride] = cm[dst[5*stride] + ((_Fd + _Bdd ) >> 4)];
  201. dst[6*stride] = cm[dst[6*stride] + ((_Fd - _Bdd ) >> 4)];
  202. }
  203. } else {
  204. if(type==0){
  205. ip[0*8] =
  206. ip[1*8] =
  207. ip[2*8] =
  208. ip[3*8] =
  209. ip[4*8] =
  210. ip[5*8] =
  211. ip[6*8] =
  212. ip[7*8] = ((xC4S4 * ip[0*8] + (IdctAdjustBeforeShift<<16))>>20);
  213. }else if(type==1){
  214. dst[0*stride]=
  215. dst[1*stride]=
  216. dst[2*stride]=
  217. dst[3*stride]=
  218. dst[4*stride]=
  219. dst[5*stride]=
  220. dst[6*stride]=
  221. dst[7*stride]= 128 + ((xC4S4 * ip[0*8] + (IdctAdjustBeforeShift<<16))>>20);
  222. }else{
  223. if(ip[0*8]){
  224. int v= ((xC4S4 * ip[0*8] + (IdctAdjustBeforeShift<<16))>>20);
  225. dst[0*stride] = cm[dst[0*stride] + v];
  226. dst[1*stride] = cm[dst[1*stride] + v];
  227. dst[2*stride] = cm[dst[2*stride] + v];
  228. dst[3*stride] = cm[dst[3*stride] + v];
  229. dst[4*stride] = cm[dst[4*stride] + v];
  230. dst[5*stride] = cm[dst[5*stride] + v];
  231. dst[6*stride] = cm[dst[6*stride] + v];
  232. dst[7*stride] = cm[dst[7*stride] + v];
  233. }
  234. }
  235. }
  236. ip++; /* next column */
  237. dst++;
  238. }
  239. }
  240. void ff_vp3_idct_c(DCTELEM *block/* align 16*/){
  241. idct(NULL, 0, block, 0);
  242. }
  243. void ff_vp3_idct_put_c(uint8_t *dest/*align 8*/, int line_size, DCTELEM *block/*align 16*/){
  244. idct(dest, line_size, block, 1);
  245. }
  246. void ff_vp3_idct_add_c(uint8_t *dest/*align 8*/, int line_size, DCTELEM *block/*align 16*/){
  247. idct(dest, line_size, block, 2);
  248. }