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.

297 lines
9.5KB

  1. /*
  2. * Copyright (c) 2004 The FFmpeg Project
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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/common.h"
  27. #include "libavutil/intreadwrite.h"
  28. #include "avcodec.h"
  29. #include "rnd_avg.h"
  30. #include "vp3dsp.h"
  31. #define IdctAdjustBeforeShift 8
  32. #define xC1S7 64277
  33. #define xC2S6 60547
  34. #define xC3S5 54491
  35. #define xC4S4 46341
  36. #define xC5S3 36410
  37. #define xC6S2 25080
  38. #define xC7S1 12785
  39. #define M(a, b) (((a) * (b)) >> 16)
  40. static av_always_inline void idct(uint8_t *dst, int stride,
  41. int16_t *input, int type)
  42. {
  43. int16_t *ip = input;
  44. int A, B, C, D, Ad, Bd, Cd, Dd, E, F, G, H;
  45. int Ed, Gd, Add, Bdd, Fd, Hd;
  46. int i;
  47. /* Inverse DCT on the rows now */
  48. for (i = 0; i < 8; i++) {
  49. /* Check for non-zero values */
  50. if (ip[0 * 8] | ip[1 * 8] | ip[2 * 8] | ip[3 * 8] |
  51. ip[4 * 8] | ip[5 * 8] | ip[6 * 8] | ip[7 * 8]) {
  52. A = M(xC1S7, ip[1 * 8]) + M(xC7S1, ip[7 * 8]);
  53. B = M(xC7S1, ip[1 * 8]) - M(xC1S7, ip[7 * 8]);
  54. C = M(xC3S5, ip[3 * 8]) + M(xC5S3, ip[5 * 8]);
  55. D = M(xC3S5, ip[5 * 8]) - M(xC5S3, ip[3 * 8]);
  56. Ad = M(xC4S4, (A - C));
  57. Bd = M(xC4S4, (B - D));
  58. Cd = A + C;
  59. Dd = B + D;
  60. E = M(xC4S4, (ip[0 * 8] + ip[4 * 8]));
  61. F = M(xC4S4, (ip[0 * 8] - ip[4 * 8]));
  62. G = M(xC2S6, ip[2 * 8]) + M(xC6S2, ip[6 * 8]);
  63. H = M(xC6S2, ip[2 * 8]) - M(xC2S6, ip[6 * 8]);
  64. Ed = E - G;
  65. Gd = E + G;
  66. Add = F + Ad;
  67. Bdd = Bd - H;
  68. Fd = F - Ad;
  69. Hd = Bd + H;
  70. /* Final sequence of operations over-write original inputs. */
  71. ip[0 * 8] = Gd + Cd;
  72. ip[7 * 8] = Gd - Cd;
  73. ip[1 * 8] = Add + Hd;
  74. ip[2 * 8] = Add - Hd;
  75. ip[3 * 8] = Ed + Dd;
  76. ip[4 * 8] = Ed - Dd;
  77. ip[5 * 8] = Fd + Bdd;
  78. ip[6 * 8] = Fd - Bdd;
  79. }
  80. ip += 1; /* next row */
  81. }
  82. ip = input;
  83. for (i = 0; i < 8; i++) {
  84. /* Check for non-zero values (bitwise or faster than ||) */
  85. if (ip[1] | ip[2] | ip[3] |
  86. ip[4] | ip[5] | ip[6] | ip[7]) {
  87. A = M(xC1S7, ip[1]) + M(xC7S1, ip[7]);
  88. B = M(xC7S1, ip[1]) - M(xC1S7, ip[7]);
  89. C = M(xC3S5, ip[3]) + M(xC5S3, ip[5]);
  90. D = M(xC3S5, ip[5]) - M(xC5S3, ip[3]);
  91. Ad = M(xC4S4, (A - C));
  92. Bd = M(xC4S4, (B - D));
  93. Cd = A + C;
  94. Dd = B + D;
  95. E = M(xC4S4, (ip[0] + ip[4])) + 8;
  96. F = M(xC4S4, (ip[0] - ip[4])) + 8;
  97. if (type == 1) { // HACK
  98. E += 16 * 128;
  99. F += 16 * 128;
  100. }
  101. G = M(xC2S6, ip[2]) + M(xC6S2, ip[6]);
  102. H = M(xC6S2, ip[2]) - M(xC2S6, ip[6]);
  103. Ed = E - G;
  104. Gd = E + G;
  105. Add = F + Ad;
  106. Bdd = Bd - H;
  107. Fd = F - Ad;
  108. Hd = Bd + H;
  109. /* Final sequence of operations over-write original inputs. */
  110. if (type == 1) {
  111. dst[0 * stride] = av_clip_uint8((Gd + Cd) >> 4);
  112. dst[7 * stride] = av_clip_uint8((Gd - Cd) >> 4);
  113. dst[1 * stride] = av_clip_uint8((Add + Hd) >> 4);
  114. dst[2 * stride] = av_clip_uint8((Add - Hd) >> 4);
  115. dst[3 * stride] = av_clip_uint8((Ed + Dd) >> 4);
  116. dst[4 * stride] = av_clip_uint8((Ed - Dd) >> 4);
  117. dst[5 * stride] = av_clip_uint8((Fd + Bdd) >> 4);
  118. dst[6 * stride] = av_clip_uint8((Fd - Bdd) >> 4);
  119. } else {
  120. dst[0 * stride] = av_clip_uint8(dst[0 * stride] + ((Gd + Cd) >> 4));
  121. dst[7 * stride] = av_clip_uint8(dst[7 * stride] + ((Gd - Cd) >> 4));
  122. dst[1 * stride] = av_clip_uint8(dst[1 * stride] + ((Add + Hd) >> 4));
  123. dst[2 * stride] = av_clip_uint8(dst[2 * stride] + ((Add - Hd) >> 4));
  124. dst[3 * stride] = av_clip_uint8(dst[3 * stride] + ((Ed + Dd) >> 4));
  125. dst[4 * stride] = av_clip_uint8(dst[4 * stride] + ((Ed - Dd) >> 4));
  126. dst[5 * stride] = av_clip_uint8(dst[5 * stride] + ((Fd + Bdd) >> 4));
  127. dst[6 * stride] = av_clip_uint8(dst[6 * stride] + ((Fd - Bdd) >> 4));
  128. }
  129. } else {
  130. if (type == 1) {
  131. dst[0*stride] =
  132. dst[1*stride] =
  133. dst[2*stride] =
  134. dst[3*stride] =
  135. dst[4*stride] =
  136. dst[5*stride] =
  137. dst[6*stride] =
  138. dst[7*stride] = av_clip_uint8(128 + ((xC4S4 * ip[0] + (IdctAdjustBeforeShift << 16)) >> 20));
  139. } else {
  140. if (ip[0]) {
  141. int v = (xC4S4 * ip[0] + (IdctAdjustBeforeShift << 16)) >> 20;
  142. dst[0 * stride] = av_clip_uint8(dst[0 * stride] + v);
  143. dst[1 * stride] = av_clip_uint8(dst[1 * stride] + v);
  144. dst[2 * stride] = av_clip_uint8(dst[2 * stride] + v);
  145. dst[3 * stride] = av_clip_uint8(dst[3 * stride] + v);
  146. dst[4 * stride] = av_clip_uint8(dst[4 * stride] + v);
  147. dst[5 * stride] = av_clip_uint8(dst[5 * stride] + v);
  148. dst[6 * stride] = av_clip_uint8(dst[6 * stride] + v);
  149. dst[7 * stride] = av_clip_uint8(dst[7 * stride] + v);
  150. }
  151. }
  152. }
  153. ip += 8; /* next column */
  154. dst++;
  155. }
  156. }
  157. static void vp3_idct_put_c(uint8_t *dest /* align 8 */, int line_size,
  158. int16_t *block /* align 16 */)
  159. {
  160. idct(dest, line_size, block, 1);
  161. memset(block, 0, sizeof(*block) * 64);
  162. }
  163. static void vp3_idct_add_c(uint8_t *dest /* align 8 */, int line_size,
  164. int16_t *block /* align 16 */)
  165. {
  166. idct(dest, line_size, block, 2);
  167. memset(block, 0, sizeof(*block) * 64);
  168. }
  169. static void vp3_idct_dc_add_c(uint8_t *dest /* align 8 */, int line_size,
  170. int16_t *block /* align 16 */)
  171. {
  172. int i, dc = (block[0] + 15) >> 5;
  173. for (i = 0; i < 8; i++) {
  174. dest[0] = av_clip_uint8(dest[0] + dc);
  175. dest[1] = av_clip_uint8(dest[1] + dc);
  176. dest[2] = av_clip_uint8(dest[2] + dc);
  177. dest[3] = av_clip_uint8(dest[3] + dc);
  178. dest[4] = av_clip_uint8(dest[4] + dc);
  179. dest[5] = av_clip_uint8(dest[5] + dc);
  180. dest[6] = av_clip_uint8(dest[6] + dc);
  181. dest[7] = av_clip_uint8(dest[7] + dc);
  182. dest += line_size;
  183. }
  184. block[0] = 0;
  185. }
  186. static void vp3_v_loop_filter_c(uint8_t *first_pixel, int stride,
  187. int *bounding_values)
  188. {
  189. unsigned char *end;
  190. int filter_value;
  191. const int nstride = -stride;
  192. for (end = first_pixel + 8; first_pixel < end; first_pixel++) {
  193. filter_value = (first_pixel[2 * nstride] - first_pixel[stride]) +
  194. (first_pixel[0] - first_pixel[nstride]) * 3;
  195. filter_value = bounding_values[(filter_value + 4) >> 3];
  196. first_pixel[nstride] = av_clip_uint8(first_pixel[nstride] + filter_value);
  197. first_pixel[0] = av_clip_uint8(first_pixel[0] - filter_value);
  198. }
  199. }
  200. static void vp3_h_loop_filter_c(uint8_t *first_pixel, int stride,
  201. int *bounding_values)
  202. {
  203. unsigned char *end;
  204. int filter_value;
  205. for (end = first_pixel + 8 * stride; first_pixel != end; first_pixel += stride) {
  206. filter_value = (first_pixel[-2] - first_pixel[1]) +
  207. (first_pixel[ 0] - first_pixel[-1]) * 3;
  208. filter_value = bounding_values[(filter_value + 4) >> 3];
  209. first_pixel[-1] = av_clip_uint8(first_pixel[-1] + filter_value);
  210. first_pixel[ 0] = av_clip_uint8(first_pixel[ 0] - filter_value);
  211. }
  212. }
  213. static void put_no_rnd_pixels_l2(uint8_t *dst, const uint8_t *src1,
  214. const uint8_t *src2, ptrdiff_t stride, int h)
  215. {
  216. int i;
  217. for (i = 0; i < h; i++) {
  218. uint32_t a, b;
  219. a = AV_RN32(&src1[i * stride]);
  220. b = AV_RN32(&src2[i * stride]);
  221. AV_WN32A(&dst[i * stride], no_rnd_avg32(a, b));
  222. a = AV_RN32(&src1[i * stride + 4]);
  223. b = AV_RN32(&src2[i * stride + 4]);
  224. AV_WN32A(&dst[i * stride + 4], no_rnd_avg32(a, b));
  225. }
  226. }
  227. av_cold void ff_vp3dsp_init(VP3DSPContext *c, int flags)
  228. {
  229. c->put_no_rnd_pixels_l2 = put_no_rnd_pixels_l2;
  230. c->idct_put = vp3_idct_put_c;
  231. c->idct_add = vp3_idct_add_c;
  232. c->idct_dc_add = vp3_idct_dc_add_c;
  233. c->v_loop_filter = vp3_v_loop_filter_c;
  234. c->h_loop_filter = vp3_h_loop_filter_c;
  235. if (ARCH_ARM)
  236. ff_vp3dsp_init_arm(c, flags);
  237. if (ARCH_PPC)
  238. ff_vp3dsp_init_ppc(c, flags);
  239. if (ARCH_X86)
  240. ff_vp3dsp_init_x86(c, flags);
  241. }