vp3dsp.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 libavcodec/vp3dsp.c
  22. * Standard C DSP-oriented functions cribbed from the original VP3
  23. * source code.
  24. */
  25. #include "avcodec.h"
  26. #include "dsputil.h"
  27. #define IdctAdjustBeforeShift 8
  28. #define xC1S7 64277
  29. #define xC2S6 60547
  30. #define xC3S5 54491
  31. #define xC4S4 46341
  32. #define xC5S3 36410
  33. #define xC6S2 25080
  34. #define xC7S1 12785
  35. #define M(a,b) (((a) * (b))>>16)
  36. static av_always_inline void idct(uint8_t *dst, int stride, int16_t *input, int type)
  37. {
  38. int16_t *ip = input;
  39. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  40. int A, B, C, D, Ad, Bd, Cd, Dd, E, F, G, H;
  41. int Ed, Gd, Add, Bdd, Fd, Hd;
  42. int i;
  43. /* Inverse DCT on the rows now */
  44. for (i = 0; i < 8; i++) {
  45. /* Check for non-zero values */
  46. if ( ip[0] | ip[1] | ip[2] | ip[3] | ip[4] | ip[5] | ip[6] | ip[7] ) {
  47. A = M(xC1S7, ip[1]) + M(xC7S1, ip[7]);
  48. B = M(xC7S1, ip[1]) - M(xC1S7, ip[7]);
  49. C = M(xC3S5, ip[3]) + M(xC5S3, ip[5]);
  50. D = M(xC3S5, ip[5]) - M(xC5S3, ip[3]);
  51. Ad = M(xC4S4, (A - C));
  52. Bd = M(xC4S4, (B - D));
  53. Cd = A + C;
  54. Dd = B + D;
  55. E = M(xC4S4, (ip[0] + ip[4]));
  56. F = M(xC4S4, (ip[0] - ip[4]));
  57. G = M(xC2S6, ip[2]) + M(xC6S2, ip[6]);
  58. H = M(xC6S2, ip[2]) - M(xC2S6, ip[6]);
  59. Ed = E - G;
  60. Gd = E + G;
  61. Add = F + Ad;
  62. Bdd = Bd - H;
  63. Fd = F - Ad;
  64. Hd = Bd + H;
  65. /* Final sequence of operations over-write original inputs. */
  66. ip[0] = Gd + Cd ;
  67. ip[7] = Gd - Cd ;
  68. ip[1] = Add + Hd;
  69. ip[2] = Add - Hd;
  70. ip[3] = Ed + Dd ;
  71. ip[4] = Ed - Dd ;
  72. ip[5] = Fd + Bdd;
  73. ip[6] = Fd - Bdd;
  74. }
  75. ip += 8; /* next row */
  76. }
  77. ip = input;
  78. for ( i = 0; i < 8; i++) {
  79. /* Check for non-zero values (bitwise or faster than ||) */
  80. if ( ip[1 * 8] | ip[2 * 8] | ip[3 * 8] |
  81. ip[4 * 8] | ip[5 * 8] | ip[6 * 8] | ip[7 * 8] ) {
  82. A = M(xC1S7, ip[1*8]) + M(xC7S1, ip[7*8]);
  83. B = M(xC7S1, ip[1*8]) - M(xC1S7, ip[7*8]);
  84. C = M(xC3S5, ip[3*8]) + M(xC5S3, ip[5*8]);
  85. D = M(xC3S5, ip[5*8]) - M(xC5S3, ip[3*8]);
  86. Ad = M(xC4S4, (A - C));
  87. Bd = M(xC4S4, (B - D));
  88. Cd = A + C;
  89. Dd = B + D;
  90. E = M(xC4S4, (ip[0*8] + ip[4*8])) + 8;
  91. F = M(xC4S4, (ip[0*8] - ip[4*8])) + 8;
  92. if(type==1){ //HACK
  93. E += 16*128;
  94. F += 16*128;
  95. }
  96. G = M(xC2S6, ip[2*8]) + M(xC6S2, ip[6*8]);
  97. H = M(xC6S2, ip[2*8]) - M(xC2S6, ip[6*8]);
  98. Ed = E - G;
  99. Gd = E + G;
  100. Add = F + Ad;
  101. Bdd = Bd - H;
  102. Fd = F - Ad;
  103. Hd = Bd + H;
  104. /* Final sequence of operations over-write original inputs. */
  105. if(type==0){
  106. ip[0*8] = (Gd + Cd ) >> 4;
  107. ip[7*8] = (Gd - Cd ) >> 4;
  108. ip[1*8] = (Add + Hd ) >> 4;
  109. ip[2*8] = (Add - Hd ) >> 4;
  110. ip[3*8] = (Ed + Dd ) >> 4;
  111. ip[4*8] = (Ed - Dd ) >> 4;
  112. ip[5*8] = (Fd + Bdd ) >> 4;
  113. ip[6*8] = (Fd - Bdd ) >> 4;
  114. }else if(type==1){
  115. dst[0*stride] = cm[(Gd + Cd ) >> 4];
  116. dst[7*stride] = cm[(Gd - Cd ) >> 4];
  117. dst[1*stride] = cm[(Add + Hd ) >> 4];
  118. dst[2*stride] = cm[(Add - Hd ) >> 4];
  119. dst[3*stride] = cm[(Ed + Dd ) >> 4];
  120. dst[4*stride] = cm[(Ed - Dd ) >> 4];
  121. dst[5*stride] = cm[(Fd + Bdd ) >> 4];
  122. dst[6*stride] = cm[(Fd - Bdd ) >> 4];
  123. }else{
  124. dst[0*stride] = cm[dst[0*stride] + ((Gd + Cd ) >> 4)];
  125. dst[7*stride] = cm[dst[7*stride] + ((Gd - Cd ) >> 4)];
  126. dst[1*stride] = cm[dst[1*stride] + ((Add + Hd ) >> 4)];
  127. dst[2*stride] = cm[dst[2*stride] + ((Add - Hd ) >> 4)];
  128. dst[3*stride] = cm[dst[3*stride] + ((Ed + Dd ) >> 4)];
  129. dst[4*stride] = cm[dst[4*stride] + ((Ed - Dd ) >> 4)];
  130. dst[5*stride] = cm[dst[5*stride] + ((Fd + Bdd ) >> 4)];
  131. dst[6*stride] = cm[dst[6*stride] + ((Fd - Bdd ) >> 4)];
  132. }
  133. } else {
  134. if(type==0){
  135. ip[0*8] =
  136. ip[1*8] =
  137. ip[2*8] =
  138. ip[3*8] =
  139. ip[4*8] =
  140. ip[5*8] =
  141. ip[6*8] =
  142. ip[7*8] = ((xC4S4 * ip[0*8] + (IdctAdjustBeforeShift<<16))>>20);
  143. }else if(type==1){
  144. dst[0*stride]=
  145. dst[1*stride]=
  146. dst[2*stride]=
  147. dst[3*stride]=
  148. dst[4*stride]=
  149. dst[5*stride]=
  150. dst[6*stride]=
  151. dst[7*stride]= cm[128 + ((xC4S4 * ip[0*8] + (IdctAdjustBeforeShift<<16))>>20)];
  152. }else{
  153. if(ip[0*8]){
  154. int v= ((xC4S4 * ip[0*8] + (IdctAdjustBeforeShift<<16))>>20);
  155. dst[0*stride] = cm[dst[0*stride] + v];
  156. dst[1*stride] = cm[dst[1*stride] + v];
  157. dst[2*stride] = cm[dst[2*stride] + v];
  158. dst[3*stride] = cm[dst[3*stride] + v];
  159. dst[4*stride] = cm[dst[4*stride] + v];
  160. dst[5*stride] = cm[dst[5*stride] + v];
  161. dst[6*stride] = cm[dst[6*stride] + v];
  162. dst[7*stride] = cm[dst[7*stride] + v];
  163. }
  164. }
  165. }
  166. ip++; /* next column */
  167. dst++;
  168. }
  169. }
  170. void ff_vp3_idct_c(DCTELEM *block/* align 16*/){
  171. idct(NULL, 0, block, 0);
  172. }
  173. void ff_vp3_idct_put_c(uint8_t *dest/*align 8*/, int line_size, DCTELEM *block/*align 16*/){
  174. idct(dest, line_size, block, 1);
  175. }
  176. void ff_vp3_idct_add_c(uint8_t *dest/*align 8*/, int line_size, DCTELEM *block/*align 16*/){
  177. idct(dest, line_size, block, 2);
  178. }
  179. void ff_vp3_v_loop_filter_c(uint8_t *first_pixel, int stride, int *bounding_values)
  180. {
  181. unsigned char *end;
  182. int filter_value;
  183. const int nstride= -stride;
  184. for (end= first_pixel + 8; first_pixel < end; first_pixel++) {
  185. filter_value =
  186. (first_pixel[2 * nstride] - first_pixel[ stride])
  187. +3*(first_pixel[0 ] - first_pixel[nstride]);
  188. filter_value = bounding_values[(filter_value + 4) >> 3];
  189. first_pixel[nstride] = av_clip_uint8(first_pixel[nstride] + filter_value);
  190. first_pixel[0] = av_clip_uint8(first_pixel[0] - filter_value);
  191. }
  192. }
  193. void ff_vp3_h_loop_filter_c(uint8_t *first_pixel, int stride, int *bounding_values)
  194. {
  195. unsigned char *end;
  196. int filter_value;
  197. for (end= first_pixel + 8*stride; first_pixel != end; first_pixel += stride) {
  198. filter_value =
  199. (first_pixel[-2] - first_pixel[ 1])
  200. +3*(first_pixel[ 0] - first_pixel[-1]);
  201. filter_value = bounding_values[(filter_value + 4) >> 3];
  202. first_pixel[-1] = av_clip_uint8(first_pixel[-1] + filter_value);
  203. first_pixel[ 0] = av_clip_uint8(first_pixel[ 0] - filter_value);
  204. }
  205. }