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.

321 lines
10KB

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