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.

291 lines
9.3KB

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