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.

319 lines
8.1KB

  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 "vp3data.h"
  26. #define IdctAdjustBeforeShift 8
  27. #define xC1S7 64277
  28. #define xC2S6 60547
  29. #define xC3S5 54491
  30. #define xC4S4 46341
  31. #define xC5S3 36410
  32. #define xC6S2 25080
  33. #define xC7S1 12785
  34. void vp3_dsp_init_c(void)
  35. {
  36. /* nop */
  37. }
  38. static void vp3_idct_c(int32_t *dequantized_data, int16_t *output_data)
  39. {
  40. int32_t *ip = dequantized_data;
  41. int16_t *op = output_data;
  42. int32_t A_, B_, C_, D_, _Ad, _Bd, _Cd, _Dd, E_, F_, G_, H_;
  43. int32_t _Ed, _Gd, _Add, _Bdd, _Fd, _Hd;
  44. int32_t t1, t2;
  45. int i;
  46. /* Inverse DCT on the rows now */
  47. for (i = 0; i < 8; i++) {
  48. /* Check for non-zero values */
  49. if ( ip[0] | ip[1] | ip[2] | ip[3] | ip[4] | ip[5] | ip[6] | ip[7] ) {
  50. t1 = (int32_t)(xC1S7 * ip[1]);
  51. t2 = (int32_t)(xC7S1 * ip[7]);
  52. t1 >>= 16;
  53. t2 >>= 16;
  54. A_ = t1 + t2;
  55. t1 = (int32_t)(xC7S1 * ip[1]);
  56. t2 = (int32_t)(xC1S7 * ip[7]);
  57. t1 >>= 16;
  58. t2 >>= 16;
  59. B_ = t1 - t2;
  60. t1 = (int32_t)(xC3S5 * ip[3]);
  61. t2 = (int32_t)(xC5S3 * ip[5]);
  62. t1 >>= 16;
  63. t2 >>= 16;
  64. C_ = t1 + t2;
  65. t1 = (int32_t)(xC3S5 * ip[5]);
  66. t2 = (int32_t)(xC5S3 * ip[3]);
  67. t1 >>= 16;
  68. t2 >>= 16;
  69. D_ = t1 - t2;
  70. t1 = (int32_t)(xC4S4 * (A_ - C_));
  71. t1 >>= 16;
  72. _Ad = t1;
  73. t1 = (int32_t)(xC4S4 * (B_ - D_));
  74. t1 >>= 16;
  75. _Bd = t1;
  76. _Cd = A_ + C_;
  77. _Dd = B_ + D_;
  78. t1 = (int32_t)(xC4S4 * (ip[0] + ip[4]));
  79. t1 >>= 16;
  80. E_ = t1;
  81. t1 = (int32_t)(xC4S4 * (ip[0] - ip[4]));
  82. t1 >>= 16;
  83. F_ = t1;
  84. t1 = (int32_t)(xC2S6 * ip[2]);
  85. t2 = (int32_t)(xC6S2 * ip[6]);
  86. t1 >>= 16;
  87. t2 >>= 16;
  88. G_ = t1 + t2;
  89. t1 = (int32_t)(xC6S2 * ip[2]);
  90. t2 = (int32_t)(xC2S6 * ip[6]);
  91. t1 >>= 16;
  92. t2 >>= 16;
  93. H_ = t1 - t2;
  94. _Ed = E_ - G_;
  95. _Gd = E_ + G_;
  96. _Add = F_ + _Ad;
  97. _Bdd = _Bd - H_;
  98. _Fd = F_ - _Ad;
  99. _Hd = _Bd + H_;
  100. /* Final sequence of operations over-write original inputs. */
  101. ip[0] = (int16_t)((_Gd + _Cd ) >> 0);
  102. ip[7] = (int16_t)((_Gd - _Cd ) >> 0);
  103. ip[1] = (int16_t)((_Add + _Hd ) >> 0);
  104. ip[2] = (int16_t)((_Add - _Hd ) >> 0);
  105. ip[3] = (int16_t)((_Ed + _Dd ) >> 0);
  106. ip[4] = (int16_t)((_Ed - _Dd ) >> 0);
  107. ip[5] = (int16_t)((_Fd + _Bdd ) >> 0);
  108. ip[6] = (int16_t)((_Fd - _Bdd ) >> 0);
  109. }
  110. ip += 8; /* next row */
  111. }
  112. ip = dequantized_data;
  113. for ( i = 0; i < 8; i++) {
  114. /* Check for non-zero values (bitwise or faster than ||) */
  115. if ( ip[0 * 8] | ip[1 * 8] | ip[2 * 8] | ip[3 * 8] |
  116. ip[4 * 8] | ip[5 * 8] | ip[6 * 8] | ip[7 * 8] ) {
  117. t1 = (int32_t)(xC1S7 * ip[1*8]);
  118. t2 = (int32_t)(xC7S1 * ip[7*8]);
  119. t1 >>= 16;
  120. t2 >>= 16;
  121. A_ = t1 + t2;
  122. t1 = (int32_t)(xC7S1 * ip[1*8]);
  123. t2 = (int32_t)(xC1S7 * ip[7*8]);
  124. t1 >>= 16;
  125. t2 >>= 16;
  126. B_ = t1 - t2;
  127. t1 = (int32_t)(xC3S5 * ip[3*8]);
  128. t2 = (int32_t)(xC5S3 * ip[5*8]);
  129. t1 >>= 16;
  130. t2 >>= 16;
  131. C_ = t1 + t2;
  132. t1 = (int32_t)(xC3S5 * ip[5*8]);
  133. t2 = (int32_t)(xC5S3 * ip[3*8]);
  134. t1 >>= 16;
  135. t2 >>= 16;
  136. D_ = t1 - t2;
  137. t1 = (int32_t)(xC4S4 * (A_ - C_));
  138. t1 >>= 16;
  139. _Ad = t1;
  140. t1 = (int32_t)(xC4S4 * (B_ - D_));
  141. t1 >>= 16;
  142. _Bd = t1;
  143. _Cd = A_ + C_;
  144. _Dd = B_ + D_;
  145. t1 = (int32_t)(xC4S4 * (ip[0*8] + ip[4*8]));
  146. t1 >>= 16;
  147. E_ = t1;
  148. t1 = (int32_t)(xC4S4 * (ip[0*8] - ip[4*8]));
  149. t1 >>= 16;
  150. F_ = t1;
  151. t1 = (int32_t)(xC2S6 * ip[2*8]);
  152. t2 = (int32_t)(xC6S2 * ip[6*8]);
  153. t1 >>= 16;
  154. t2 >>= 16;
  155. G_ = t1 + t2;
  156. t1 = (int32_t)(xC6S2 * ip[2*8]);
  157. t2 = (int32_t)(xC2S6 * ip[6*8]);
  158. t1 >>= 16;
  159. t2 >>= 16;
  160. H_ = t1 - t2;
  161. _Ed = E_ - G_;
  162. _Gd = E_ + G_;
  163. _Add = F_ + _Ad;
  164. _Bdd = _Bd - H_;
  165. _Fd = F_ - _Ad;
  166. _Hd = _Bd + H_;
  167. _Gd += IdctAdjustBeforeShift;
  168. _Add += IdctAdjustBeforeShift;
  169. _Ed += IdctAdjustBeforeShift;
  170. _Fd += IdctAdjustBeforeShift;
  171. /* Final sequence of operations over-write original inputs. */
  172. op[0*8] = (int16_t)((_Gd + _Cd ) >> 4);
  173. op[7*8] = (int16_t)((_Gd - _Cd ) >> 4);
  174. op[1*8] = (int16_t)((_Add + _Hd ) >> 4);
  175. op[2*8] = (int16_t)((_Add - _Hd ) >> 4);
  176. op[3*8] = (int16_t)((_Ed + _Dd ) >> 4);
  177. op[4*8] = (int16_t)((_Ed - _Dd ) >> 4);
  178. op[5*8] = (int16_t)((_Fd + _Bdd ) >> 4);
  179. op[6*8] = (int16_t)((_Fd - _Bdd ) >> 4);
  180. } else {
  181. op[0*8] = 0;
  182. op[7*8] = 0;
  183. op[1*8] = 0;
  184. op[2*8] = 0;
  185. op[3*8] = 0;
  186. op[4*8] = 0;
  187. op[5*8] = 0;
  188. op[6*8] = 0;
  189. }
  190. ip++; /* next column */
  191. op++;
  192. }
  193. }
  194. void vp3_idct_put_c(int16_t *input_data, int16_t *dequant_matrix,
  195. int coeff_count, uint8_t *dest, int stride)
  196. {
  197. int32_t dequantized_data[64];
  198. int16_t transformed_data[64];
  199. int16_t *op;
  200. int i, j;
  201. /* de-zigzag and dequantize */
  202. for (i = 0; i < coeff_count; i++) {
  203. j = dezigzag_index[i];
  204. dequantized_data[j] = dequant_matrix[i] * input_data[i];
  205. }
  206. vp3_idct_c(dequantized_data, transformed_data);
  207. /* place in final output */
  208. op = transformed_data;
  209. for (i = 0; i < 8; i++) {
  210. for (j = 0; j < 8; j++) {
  211. if (*op < -128)
  212. *dest = 0;
  213. else if (*op > 127)
  214. *dest = 255;
  215. else
  216. *dest = (uint8_t)(*op + 128);
  217. op++;
  218. dest++;
  219. }
  220. dest += (stride - 8);
  221. }
  222. }
  223. void vp3_idct_add_c(int16_t *input_data, int16_t *dequant_matrix,
  224. int coeff_count, uint8_t *dest, int stride)
  225. {
  226. int32_t dequantized_data[64];
  227. int16_t transformed_data[64];
  228. int16_t *op;
  229. int i, j;
  230. int16_t sample;
  231. /* de-zigzag and dequantize */
  232. for (i = 0; i < coeff_count; i++) {
  233. j = dezigzag_index[i];
  234. dequantized_data[j] = dequant_matrix[i] * input_data[i];
  235. }
  236. vp3_idct_c(dequantized_data, transformed_data);
  237. /* place in final output */
  238. op = transformed_data;
  239. for (i = 0; i < 8; i++) {
  240. for (j = 0; j < 8; j++) {
  241. sample = *dest + *op;
  242. if (sample < 0)
  243. *dest = 0;
  244. else if (sample > 255)
  245. *dest = 255;
  246. else
  247. *dest = (uint8_t)(sample & 0xFF);
  248. op++;
  249. dest++;
  250. }
  251. dest += (stride - 8);
  252. }
  253. }