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.

298 lines
9.6KB

  1. /*
  2. * Copyright (C) 2004 The FFmpeg project
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * Libav is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with Libav; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * Standard C DSP-oriented functions cribbed from the original VP3
  23. * source code.
  24. */
  25. #include "libavutil/attributes.h"
  26. #include "libavutil/intreadwrite.h"
  27. #include "libavutil/common.h"
  28. #include "libavutil/intreadwrite.h"
  29. #include "avcodec.h"
  30. #include "rnd_avg.h"
  31. #include "vp3dsp.h"
  32. #define IdctAdjustBeforeShift 8
  33. #define xC1S7 64277
  34. #define xC2S6 60547
  35. #define xC3S5 54491
  36. #define xC4S4 46341
  37. #define xC5S3 36410
  38. #define xC6S2 25080
  39. #define xC7S1 12785
  40. #define M(a, b) (((a) * (b)) >> 16)
  41. static av_always_inline void idct(uint8_t *dst, ptrdiff_t stride,
  42. int16_t *input, int type)
  43. {
  44. int16_t *ip = input;
  45. int A, B, C, D, Ad, Bd, Cd, Dd, E, F, G, H;
  46. int Ed, Gd, Add, Bdd, Fd, Hd;
  47. int i;
  48. /* Inverse DCT on the rows now */
  49. for (i = 0; i < 8; i++) {
  50. /* Check for non-zero values */
  51. if (ip[0 * 8] | ip[1 * 8] | ip[2 * 8] | ip[3 * 8] |
  52. ip[4 * 8] | ip[5 * 8] | ip[6 * 8] | ip[7 * 8]) {
  53. A = M(xC1S7, ip[1 * 8]) + M(xC7S1, ip[7 * 8]);
  54. B = M(xC7S1, ip[1 * 8]) - M(xC1S7, ip[7 * 8]);
  55. C = M(xC3S5, ip[3 * 8]) + M(xC5S3, ip[5 * 8]);
  56. D = M(xC3S5, ip[5 * 8]) - M(xC5S3, ip[3 * 8]);
  57. Ad = M(xC4S4, (A - C));
  58. Bd = M(xC4S4, (B - D));
  59. Cd = A + C;
  60. Dd = B + D;
  61. E = M(xC4S4, (ip[0 * 8] + ip[4 * 8]));
  62. F = M(xC4S4, (ip[0 * 8] - ip[4 * 8]));
  63. G = M(xC2S6, ip[2 * 8]) + M(xC6S2, ip[6 * 8]);
  64. H = M(xC6S2, ip[2 * 8]) - M(xC2S6, ip[6 * 8]);
  65. Ed = E - G;
  66. Gd = E + G;
  67. Add = F + Ad;
  68. Bdd = Bd - H;
  69. Fd = F - Ad;
  70. Hd = Bd + H;
  71. /* Final sequence of operations over-write original inputs. */
  72. ip[0 * 8] = Gd + Cd;
  73. ip[7 * 8] = Gd - Cd;
  74. ip[1 * 8] = Add + Hd;
  75. ip[2 * 8] = Add - Hd;
  76. ip[3 * 8] = Ed + Dd;
  77. ip[4 * 8] = Ed - Dd;
  78. ip[5 * 8] = Fd + Bdd;
  79. ip[6 * 8] = Fd - Bdd;
  80. }
  81. ip += 1; /* next row */
  82. }
  83. ip = input;
  84. for (i = 0; i < 8; i++) {
  85. /* Check for non-zero values (bitwise or faster than ||) */
  86. if (ip[1] | ip[2] | ip[3] |
  87. ip[4] | ip[5] | ip[6] | ip[7]) {
  88. A = M(xC1S7, ip[1]) + M(xC7S1, ip[7]);
  89. B = M(xC7S1, ip[1]) - M(xC1S7, ip[7]);
  90. C = M(xC3S5, ip[3]) + M(xC5S3, ip[5]);
  91. D = M(xC3S5, ip[5]) - M(xC5S3, ip[3]);
  92. Ad = M(xC4S4, (A - C));
  93. Bd = M(xC4S4, (B - D));
  94. Cd = A + C;
  95. Dd = B + D;
  96. E = M(xC4S4, (ip[0] + ip[4])) + 8;
  97. F = M(xC4S4, (ip[0] - ip[4])) + 8;
  98. if (type == 1) { // HACK
  99. E += 16 * 128;
  100. F += 16 * 128;
  101. }
  102. G = M(xC2S6, ip[2]) + M(xC6S2, ip[6]);
  103. H = M(xC6S2, ip[2]) - M(xC2S6, ip[6]);
  104. Ed = E - G;
  105. Gd = E + G;
  106. Add = F + Ad;
  107. Bdd = Bd - H;
  108. Fd = F - Ad;
  109. Hd = Bd + H;
  110. /* Final sequence of operations over-write original inputs. */
  111. if (type == 1) {
  112. dst[0 * stride] = av_clip_uint8((Gd + Cd) >> 4);
  113. dst[7 * stride] = av_clip_uint8((Gd - Cd) >> 4);
  114. dst[1 * stride] = av_clip_uint8((Add + Hd) >> 4);
  115. dst[2 * stride] = av_clip_uint8((Add - Hd) >> 4);
  116. dst[3 * stride] = av_clip_uint8((Ed + Dd) >> 4);
  117. dst[4 * stride] = av_clip_uint8((Ed - Dd) >> 4);
  118. dst[5 * stride] = av_clip_uint8((Fd + Bdd) >> 4);
  119. dst[6 * stride] = av_clip_uint8((Fd - Bdd) >> 4);
  120. } else {
  121. dst[0 * stride] = av_clip_uint8(dst[0 * stride] + ((Gd + Cd) >> 4));
  122. dst[7 * stride] = av_clip_uint8(dst[7 * stride] + ((Gd - Cd) >> 4));
  123. dst[1 * stride] = av_clip_uint8(dst[1 * stride] + ((Add + Hd) >> 4));
  124. dst[2 * stride] = av_clip_uint8(dst[2 * stride] + ((Add - Hd) >> 4));
  125. dst[3 * stride] = av_clip_uint8(dst[3 * stride] + ((Ed + Dd) >> 4));
  126. dst[4 * stride] = av_clip_uint8(dst[4 * stride] + ((Ed - Dd) >> 4));
  127. dst[5 * stride] = av_clip_uint8(dst[5 * stride] + ((Fd + Bdd) >> 4));
  128. dst[6 * stride] = av_clip_uint8(dst[6 * stride] + ((Fd - Bdd) >> 4));
  129. }
  130. } else {
  131. if (type == 1) {
  132. dst[0*stride] =
  133. dst[1*stride] =
  134. dst[2*stride] =
  135. dst[3*stride] =
  136. dst[4*stride] =
  137. dst[5*stride] =
  138. dst[6*stride] =
  139. dst[7*stride] = av_clip_uint8(128 + ((xC4S4 * ip[0] + (IdctAdjustBeforeShift << 16)) >> 20));
  140. } else {
  141. if (ip[0]) {
  142. int v = (xC4S4 * ip[0] + (IdctAdjustBeforeShift << 16)) >> 20;
  143. dst[0 * stride] = av_clip_uint8(dst[0 * stride] + v);
  144. dst[1 * stride] = av_clip_uint8(dst[1 * stride] + v);
  145. dst[2 * stride] = av_clip_uint8(dst[2 * stride] + v);
  146. dst[3 * stride] = av_clip_uint8(dst[3 * stride] + v);
  147. dst[4 * stride] = av_clip_uint8(dst[4 * stride] + v);
  148. dst[5 * stride] = av_clip_uint8(dst[5 * stride] + v);
  149. dst[6 * stride] = av_clip_uint8(dst[6 * stride] + v);
  150. dst[7 * stride] = av_clip_uint8(dst[7 * stride] + v);
  151. }
  152. }
  153. }
  154. ip += 8; /* next column */
  155. dst++;
  156. }
  157. }
  158. static void vp3_idct_put_c(uint8_t *dest /* align 8 */, ptrdiff_t stride,
  159. int16_t *block /* align 16 */)
  160. {
  161. idct(dest, stride, block, 1);
  162. memset(block, 0, sizeof(*block) * 64);
  163. }
  164. static void vp3_idct_add_c(uint8_t *dest /* align 8 */, ptrdiff_t stride,
  165. int16_t *block /* align 16 */)
  166. {
  167. idct(dest, stride, block, 2);
  168. memset(block, 0, sizeof(*block) * 64);
  169. }
  170. static void vp3_idct_dc_add_c(uint8_t *dest /* align 8 */, ptrdiff_t stride,
  171. int16_t *block /* align 16 */)
  172. {
  173. int i, dc = (block[0] + 15) >> 5;
  174. for (i = 0; i < 8; i++) {
  175. dest[0] = av_clip_uint8(dest[0] + dc);
  176. dest[1] = av_clip_uint8(dest[1] + dc);
  177. dest[2] = av_clip_uint8(dest[2] + dc);
  178. dest[3] = av_clip_uint8(dest[3] + dc);
  179. dest[4] = av_clip_uint8(dest[4] + dc);
  180. dest[5] = av_clip_uint8(dest[5] + dc);
  181. dest[6] = av_clip_uint8(dest[6] + dc);
  182. dest[7] = av_clip_uint8(dest[7] + dc);
  183. dest += stride;
  184. }
  185. block[0] = 0;
  186. }
  187. static void vp3_v_loop_filter_c(uint8_t *first_pixel, ptrdiff_t stride,
  188. int *bounding_values)
  189. {
  190. unsigned char *end;
  191. int filter_value;
  192. const ptrdiff_t nstride = -stride;
  193. for (end = first_pixel + 8; first_pixel < end; first_pixel++) {
  194. filter_value = (first_pixel[2 * nstride] - first_pixel[stride]) +
  195. (first_pixel[0] - first_pixel[nstride]) * 3;
  196. filter_value = bounding_values[(filter_value + 4) >> 3];
  197. first_pixel[nstride] = av_clip_uint8(first_pixel[nstride] + filter_value);
  198. first_pixel[0] = av_clip_uint8(first_pixel[0] - filter_value);
  199. }
  200. }
  201. static void vp3_h_loop_filter_c(uint8_t *first_pixel, ptrdiff_t stride,
  202. int *bounding_values)
  203. {
  204. unsigned char *end;
  205. int filter_value;
  206. for (end = first_pixel + 8 * stride; first_pixel != end; first_pixel += stride) {
  207. filter_value = (first_pixel[-2] - first_pixel[1]) +
  208. (first_pixel[ 0] - first_pixel[-1]) * 3;
  209. filter_value = bounding_values[(filter_value + 4) >> 3];
  210. first_pixel[-1] = av_clip_uint8(first_pixel[-1] + filter_value);
  211. first_pixel[ 0] = av_clip_uint8(first_pixel[ 0] - filter_value);
  212. }
  213. }
  214. static void put_no_rnd_pixels_l2(uint8_t *dst, const uint8_t *src1,
  215. const uint8_t *src2, ptrdiff_t stride, int h)
  216. {
  217. int i;
  218. for (i = 0; i < h; i++) {
  219. uint32_t a, b;
  220. a = AV_RN32(&src1[i * stride]);
  221. b = AV_RN32(&src2[i * stride]);
  222. AV_WN32A(&dst[i * stride], no_rnd_avg32(a, b));
  223. a = AV_RN32(&src1[i * stride + 4]);
  224. b = AV_RN32(&src2[i * stride + 4]);
  225. AV_WN32A(&dst[i * stride + 4], no_rnd_avg32(a, b));
  226. }
  227. }
  228. av_cold void ff_vp3dsp_init(VP3DSPContext *c, int flags)
  229. {
  230. c->put_no_rnd_pixels_l2 = put_no_rnd_pixels_l2;
  231. c->idct_put = vp3_idct_put_c;
  232. c->idct_add = vp3_idct_add_c;
  233. c->idct_dc_add = vp3_idct_dc_add_c;
  234. c->v_loop_filter = vp3_v_loop_filter_c;
  235. c->h_loop_filter = vp3_h_loop_filter_c;
  236. if (ARCH_ARM)
  237. ff_vp3dsp_init_arm(c, flags);
  238. if (ARCH_PPC)
  239. ff_vp3dsp_init_ppc(c, flags);
  240. if (ARCH_X86)
  241. ff_vp3dsp_init_x86(c, flags);
  242. }