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.

266 lines
9.1KB

  1. /*
  2. * This file is part of Libav.
  3. *
  4. * Libav 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.1 of the License, or (at your option) any later version.
  8. *
  9. * Libav 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 Libav; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "libavutil/attributes.h"
  19. #include "libavutil/common.h"
  20. #include "avcodec.h"
  21. #include "idctdsp.h"
  22. #include "mathops.h"
  23. #include "wmv2dsp.h"
  24. #define W0 2048
  25. #define W1 2841 /* 2048*sqrt (2)*cos (1*pi/16) */
  26. #define W2 2676 /* 2048*sqrt (2)*cos (2*pi/16) */
  27. #define W3 2408 /* 2048*sqrt (2)*cos (3*pi/16) */
  28. #define W4 2048 /* 2048*sqrt (2)*cos (4*pi/16) */
  29. #define W5 1609 /* 2048*sqrt (2)*cos (5*pi/16) */
  30. #define W6 1108 /* 2048*sqrt (2)*cos (6*pi/16) */
  31. #define W7 565 /* 2048*sqrt (2)*cos (7*pi/16) */
  32. static void wmv2_idct_row(short * b)
  33. {
  34. int s1, s2;
  35. int a0, a1, a2, a3, a4, a5, a6, a7;
  36. /* step 1 */
  37. a1 = W1 * b[1] + W7 * b[7];
  38. a7 = W7 * b[1] - W1 * b[7];
  39. a5 = W5 * b[5] + W3 * b[3];
  40. a3 = W3 * b[5] - W5 * b[3];
  41. a2 = W2 * b[2] + W6 * b[6];
  42. a6 = W6 * b[2] - W2 * b[6];
  43. a0 = W0 * b[0] + W0 * b[4];
  44. a4 = W0 * b[0] - W0 * b[4];
  45. /* step 2 */
  46. s1 = (181 * (a1 - a5 + a7 - a3) + 128) >> 8; // 1, 3, 5, 7
  47. s2 = (181 * (a1 - a5 - a7 + a3) + 128) >> 8;
  48. /* step 3 */
  49. b[0] = (a0 + a2 + a1 + a5 + (1 << 7)) >> 8;
  50. b[1] = (a4 + a6 + s1 + (1 << 7)) >> 8;
  51. b[2] = (a4 - a6 + s2 + (1 << 7)) >> 8;
  52. b[3] = (a0 - a2 + a7 + a3 + (1 << 7)) >> 8;
  53. b[4] = (a0 - a2 - a7 - a3 + (1 << 7)) >> 8;
  54. b[5] = (a4 - a6 - s2 + (1 << 7)) >> 8;
  55. b[6] = (a4 + a6 - s1 + (1 << 7)) >> 8;
  56. b[7] = (a0 + a2 - a1 - a5 + (1 << 7)) >> 8;
  57. }
  58. static void wmv2_idct_col(short * b)
  59. {
  60. int s1, s2;
  61. int a0, a1, a2, a3, a4, a5, a6, a7;
  62. /* step 1, with extended precision */
  63. a1 = (W1 * b[8 * 1] + W7 * b[8 * 7] + 4) >> 3;
  64. a7 = (W7 * b[8 * 1] - W1 * b[8 * 7] + 4) >> 3;
  65. a5 = (W5 * b[8 * 5] + W3 * b[8 * 3] + 4) >> 3;
  66. a3 = (W3 * b[8 * 5] - W5 * b[8 * 3] + 4) >> 3;
  67. a2 = (W2 * b[8 * 2] + W6 * b[8 * 6] + 4) >> 3;
  68. a6 = (W6 * b[8 * 2] - W2 * b[8 * 6] + 4) >> 3;
  69. a0 = (W0 * b[8 * 0] + W0 * b[8 * 4] ) >> 3;
  70. a4 = (W0 * b[8 * 0] - W0 * b[8 * 4] ) >> 3;
  71. /* step 2 */
  72. s1 = (181 * (a1 - a5 + a7 - a3) + 128) >> 8;
  73. s2 = (181 * (a1 - a5 - a7 + a3) + 128) >> 8;
  74. /* step 3 */
  75. b[8 * 0] = (a0 + a2 + a1 + a5 + (1 << 13)) >> 14;
  76. b[8 * 1] = (a4 + a6 + s1 + (1 << 13)) >> 14;
  77. b[8 * 2] = (a4 - a6 + s2 + (1 << 13)) >> 14;
  78. b[8 * 3] = (a0 - a2 + a7 + a3 + (1 << 13)) >> 14;
  79. b[8 * 4] = (a0 - a2 - a7 - a3 + (1 << 13)) >> 14;
  80. b[8 * 5] = (a4 - a6 - s2 + (1 << 13)) >> 14;
  81. b[8 * 6] = (a4 + a6 - s1 + (1 << 13)) >> 14;
  82. b[8 * 7] = (a0 + a2 - a1 - a5 + (1 << 13)) >> 14;
  83. }
  84. static void wmv2_idct_add_c(uint8_t *dest, int line_size, int16_t *block)
  85. {
  86. int i;
  87. for (i = 0; i < 64; i += 8)
  88. wmv2_idct_row(block + i);
  89. for (i = 0; i < 8; i++)
  90. wmv2_idct_col(block + i);
  91. for (i = 0; i < 8; i++) {
  92. dest[0] = av_clip_uint8(dest[0] + block[0]);
  93. dest[1] = av_clip_uint8(dest[1] + block[1]);
  94. dest[2] = av_clip_uint8(dest[2] + block[2]);
  95. dest[3] = av_clip_uint8(dest[3] + block[3]);
  96. dest[4] = av_clip_uint8(dest[4] + block[4]);
  97. dest[5] = av_clip_uint8(dest[5] + block[5]);
  98. dest[6] = av_clip_uint8(dest[6] + block[6]);
  99. dest[7] = av_clip_uint8(dest[7] + block[7]);
  100. dest += line_size;
  101. block += 8;
  102. }
  103. }
  104. static void wmv2_idct_put_c(uint8_t *dest, int line_size, int16_t *block)
  105. {
  106. int i;
  107. for (i = 0; i < 64; i += 8)
  108. wmv2_idct_row(block + i);
  109. for (i = 0; i < 8; i++)
  110. wmv2_idct_col(block + i);
  111. for (i = 0; i < 8; i++) {
  112. dest[0] = av_clip_uint8(block[0]);
  113. dest[1] = av_clip_uint8(block[1]);
  114. dest[2] = av_clip_uint8(block[2]);
  115. dest[3] = av_clip_uint8(block[3]);
  116. dest[4] = av_clip_uint8(block[4]);
  117. dest[5] = av_clip_uint8(block[5]);
  118. dest[6] = av_clip_uint8(block[6]);
  119. dest[7] = av_clip_uint8(block[7]);
  120. dest += line_size;
  121. block += 8;
  122. }
  123. }
  124. static void wmv2_mspel8_h_lowpass(uint8_t *dst, const uint8_t *src,
  125. int dstStride, int srcStride, int h)
  126. {
  127. const uint8_t *cm = ff_crop_tab + MAX_NEG_CROP;
  128. int i;
  129. for (i = 0; i < h; i++) {
  130. dst[0] = cm[(9 * (src[0] + src[1]) - (src[-1] + src[2]) + 8) >> 4];
  131. dst[1] = cm[(9 * (src[1] + src[2]) - (src[0] + src[3]) + 8) >> 4];
  132. dst[2] = cm[(9 * (src[2] + src[3]) - (src[1] + src[4]) + 8) >> 4];
  133. dst[3] = cm[(9 * (src[3] + src[4]) - (src[2] + src[5]) + 8) >> 4];
  134. dst[4] = cm[(9 * (src[4] + src[5]) - (src[3] + src[6]) + 8) >> 4];
  135. dst[5] = cm[(9 * (src[5] + src[6]) - (src[4] + src[7]) + 8) >> 4];
  136. dst[6] = cm[(9 * (src[6] + src[7]) - (src[5] + src[8]) + 8) >> 4];
  137. dst[7] = cm[(9 * (src[7] + src[8]) - (src[6] + src[9]) + 8) >> 4];
  138. dst += dstStride;
  139. src += srcStride;
  140. }
  141. }
  142. static void wmv2_mspel8_v_lowpass(uint8_t *dst, const uint8_t *src,
  143. int dstStride, int srcStride, int w)
  144. {
  145. const uint8_t *cm = ff_crop_tab + MAX_NEG_CROP;
  146. int i;
  147. for (i = 0; i < w; i++) {
  148. const int src_1 = src[-srcStride];
  149. const int src0 = src[0];
  150. const int src1 = src[srcStride];
  151. const int src2 = src[2 * srcStride];
  152. const int src3 = src[3 * srcStride];
  153. const int src4 = src[4 * srcStride];
  154. const int src5 = src[5 * srcStride];
  155. const int src6 = src[6 * srcStride];
  156. const int src7 = src[7 * srcStride];
  157. const int src8 = src[8 * srcStride];
  158. const int src9 = src[9 * srcStride];
  159. dst[0 * dstStride] = cm[(9 * (src0 + src1) - (src_1 + src2) + 8) >> 4];
  160. dst[1 * dstStride] = cm[(9 * (src1 + src2) - (src0 + src3) + 8) >> 4];
  161. dst[2 * dstStride] = cm[(9 * (src2 + src3) - (src1 + src4) + 8) >> 4];
  162. dst[3 * dstStride] = cm[(9 * (src3 + src4) - (src2 + src5) + 8) >> 4];
  163. dst[4 * dstStride] = cm[(9 * (src4 + src5) - (src3 + src6) + 8) >> 4];
  164. dst[5 * dstStride] = cm[(9 * (src5 + src6) - (src4 + src7) + 8) >> 4];
  165. dst[6 * dstStride] = cm[(9 * (src6 + src7) - (src5 + src8) + 8) >> 4];
  166. dst[7 * dstStride] = cm[(9 * (src7 + src8) - (src6 + src9) + 8) >> 4];
  167. src++;
  168. dst++;
  169. }
  170. }
  171. static void put_mspel8_mc10_c(uint8_t *dst, const uint8_t *src, ptrdiff_t stride)
  172. {
  173. uint8_t half[64];
  174. wmv2_mspel8_h_lowpass(half, src, 8, stride, 8);
  175. ff_put_pixels8_l2_8(dst, src, half, stride, stride, 8, 8);
  176. }
  177. static void put_mspel8_mc20_c(uint8_t *dst, const uint8_t *src, ptrdiff_t stride)
  178. {
  179. wmv2_mspel8_h_lowpass(dst, src, stride, stride, 8);
  180. }
  181. static void put_mspel8_mc30_c(uint8_t *dst, const uint8_t *src, ptrdiff_t stride)
  182. {
  183. uint8_t half[64];
  184. wmv2_mspel8_h_lowpass(half, src, 8, stride, 8);
  185. ff_put_pixels8_l2_8(dst, src + 1, half, stride, stride, 8, 8);
  186. }
  187. static void put_mspel8_mc02_c(uint8_t *dst, const uint8_t *src, ptrdiff_t stride)
  188. {
  189. wmv2_mspel8_v_lowpass(dst, src, stride, stride, 8);
  190. }
  191. static void put_mspel8_mc12_c(uint8_t *dst, const uint8_t *src, ptrdiff_t stride)
  192. {
  193. uint8_t halfH[88];
  194. uint8_t halfV[64];
  195. uint8_t halfHV[64];
  196. wmv2_mspel8_h_lowpass(halfH, src - stride, 8, stride, 11);
  197. wmv2_mspel8_v_lowpass(halfV, src, 8, stride, 8);
  198. wmv2_mspel8_v_lowpass(halfHV, halfH + 8, 8, 8, 8);
  199. ff_put_pixels8_l2_8(dst, halfV, halfHV, stride, 8, 8, 8);
  200. }
  201. static void put_mspel8_mc32_c(uint8_t *dst, const uint8_t *src, ptrdiff_t stride)
  202. {
  203. uint8_t halfH[88];
  204. uint8_t halfV[64];
  205. uint8_t halfHV[64];
  206. wmv2_mspel8_h_lowpass(halfH, src - stride, 8, stride, 11);
  207. wmv2_mspel8_v_lowpass(halfV, src + 1, 8, stride, 8);
  208. wmv2_mspel8_v_lowpass(halfHV, halfH + 8, 8, 8, 8);
  209. ff_put_pixels8_l2_8(dst, halfV, halfHV, stride, 8, 8, 8);
  210. }
  211. static void put_mspel8_mc22_c(uint8_t *dst, const uint8_t *src, ptrdiff_t stride)
  212. {
  213. uint8_t halfH[88];
  214. wmv2_mspel8_h_lowpass(halfH, src - stride, 8, stride, 11);
  215. wmv2_mspel8_v_lowpass(dst, halfH + 8, stride, 8, 8);
  216. }
  217. av_cold void ff_wmv2dsp_init(WMV2DSPContext *c)
  218. {
  219. c->idct_add = wmv2_idct_add_c;
  220. c->idct_put = wmv2_idct_put_c;
  221. c->idct_perm = FF_IDCT_PERM_NONE;
  222. c->put_mspel_pixels_tab[0] = ff_put_pixels8x8_c;
  223. c->put_mspel_pixels_tab[1] = put_mspel8_mc10_c;
  224. c->put_mspel_pixels_tab[2] = put_mspel8_mc20_c;
  225. c->put_mspel_pixels_tab[3] = put_mspel8_mc30_c;
  226. c->put_mspel_pixels_tab[4] = put_mspel8_mc02_c;
  227. c->put_mspel_pixels_tab[5] = put_mspel8_mc12_c;
  228. c->put_mspel_pixels_tab[6] = put_mspel8_mc22_c;
  229. c->put_mspel_pixels_tab[7] = put_mspel8_mc32_c;
  230. }