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.

271 lines
8.7KB

  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 "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. int A, B, C, D, Ad, Bd, Cd, Dd, E, F, G, H;
  40. int Ed, Gd, Add, Bdd, Fd, Hd;
  41. int i;
  42. /* Inverse DCT on the rows now */
  43. for (i = 0; i < 8; i++) {
  44. /* Check for non-zero values */
  45. if ( ip[0] | ip[1] | ip[2] | ip[3] | ip[4] | ip[5] | ip[6] | ip[7] ) {
  46. A = M(xC1S7, ip[1]) + M(xC7S1, ip[7]);
  47. B = M(xC7S1, ip[1]) - M(xC1S7, ip[7]);
  48. C = M(xC3S5, ip[3]) + M(xC5S3, ip[5]);
  49. D = M(xC3S5, ip[5]) - M(xC5S3, ip[3]);
  50. Ad = M(xC4S4, (A - C));
  51. Bd = M(xC4S4, (B - D));
  52. Cd = A + C;
  53. Dd = B + D;
  54. E = M(xC4S4, (ip[0] + ip[4]));
  55. F = M(xC4S4, (ip[0] - ip[4]));
  56. G = M(xC2S6, ip[2]) + M(xC6S2, ip[6]);
  57. H = M(xC6S2, ip[2]) - M(xC2S6, ip[6]);
  58. Ed = E - G;
  59. Gd = E + G;
  60. Add = F + Ad;
  61. Bdd = Bd - H;
  62. Fd = F - Ad;
  63. Hd = Bd + H;
  64. /* Final sequence of operations over-write original inputs. */
  65. ip[0] = Gd + Cd ;
  66. ip[7] = Gd - Cd ;
  67. ip[1] = Add + Hd;
  68. ip[2] = Add - Hd;
  69. ip[3] = Ed + Dd ;
  70. ip[4] = Ed - Dd ;
  71. ip[5] = Fd + Bdd;
  72. ip[6] = Fd - Bdd;
  73. }
  74. ip += 8; /* next row */
  75. }
  76. ip = input;
  77. for ( i = 0; i < 8; i++) {
  78. /* Check for non-zero values (bitwise or faster than ||) */
  79. if ( ip[1 * 8] | ip[2 * 8] | ip[3 * 8] |
  80. ip[4 * 8] | ip[5 * 8] | ip[6 * 8] | ip[7 * 8] ) {
  81. A = M(xC1S7, ip[1*8]) + M(xC7S1, ip[7*8]);
  82. B = M(xC7S1, ip[1*8]) - M(xC1S7, ip[7*8]);
  83. C = M(xC3S5, ip[3*8]) + M(xC5S3, ip[5*8]);
  84. D = M(xC3S5, ip[5*8]) - M(xC5S3, ip[3*8]);
  85. Ad = M(xC4S4, (A - C));
  86. Bd = M(xC4S4, (B - D));
  87. Cd = A + C;
  88. Dd = B + D;
  89. E = M(xC4S4, (ip[0*8] + ip[4*8])) + 8;
  90. F = M(xC4S4, (ip[0*8] - ip[4*8])) + 8;
  91. if(type==1){ //HACK
  92. E += 16*128;
  93. F += 16*128;
  94. }
  95. G = M(xC2S6, ip[2*8]) + M(xC6S2, ip[6*8]);
  96. H = M(xC6S2, ip[2*8]) - M(xC2S6, ip[6*8]);
  97. Ed = E - G;
  98. Gd = E + G;
  99. Add = F + Ad;
  100. Bdd = Bd - H;
  101. Fd = F - Ad;
  102. Hd = Bd + H;
  103. /* Final sequence of operations over-write original inputs. */
  104. if(type==0){
  105. ip[0*8] = (Gd + Cd ) >> 4;
  106. ip[7*8] = (Gd - Cd ) >> 4;
  107. ip[1*8] = (Add + Hd ) >> 4;
  108. ip[2*8] = (Add - Hd ) >> 4;
  109. ip[3*8] = (Ed + Dd ) >> 4;
  110. ip[4*8] = (Ed - Dd ) >> 4;
  111. ip[5*8] = (Fd + Bdd ) >> 4;
  112. ip[6*8] = (Fd - Bdd ) >> 4;
  113. }else if(type==1){
  114. dst[0*stride] = av_clip_uint8((Gd + Cd ) >> 4);
  115. dst[7*stride] = av_clip_uint8((Gd - Cd ) >> 4);
  116. dst[1*stride] = av_clip_uint8((Add + Hd ) >> 4);
  117. dst[2*stride] = av_clip_uint8((Add - Hd ) >> 4);
  118. dst[3*stride] = av_clip_uint8((Ed + Dd ) >> 4);
  119. dst[4*stride] = av_clip_uint8((Ed - Dd ) >> 4);
  120. dst[5*stride] = av_clip_uint8((Fd + Bdd ) >> 4);
  121. dst[6*stride] = av_clip_uint8((Fd - Bdd ) >> 4);
  122. }else{
  123. dst[0*stride] = av_clip_uint8(dst[0*stride] + ((Gd + Cd ) >> 4));
  124. dst[7*stride] = av_clip_uint8(dst[7*stride] + ((Gd - Cd ) >> 4));
  125. dst[1*stride] = av_clip_uint8(dst[1*stride] + ((Add + Hd ) >> 4));
  126. dst[2*stride] = av_clip_uint8(dst[2*stride] + ((Add - Hd ) >> 4));
  127. dst[3*stride] = av_clip_uint8(dst[3*stride] + ((Ed + Dd ) >> 4));
  128. dst[4*stride] = av_clip_uint8(dst[4*stride] + ((Ed - Dd ) >> 4));
  129. dst[5*stride] = av_clip_uint8(dst[5*stride] + ((Fd + Bdd ) >> 4));
  130. dst[6*stride] = av_clip_uint8(dst[6*stride] + ((Fd - Bdd ) >> 4));
  131. }
  132. } else {
  133. if(type==0){
  134. ip[0*8] =
  135. ip[1*8] =
  136. ip[2*8] =
  137. ip[3*8] =
  138. ip[4*8] =
  139. ip[5*8] =
  140. ip[6*8] =
  141. ip[7*8] = ((xC4S4 * ip[0*8] + (IdctAdjustBeforeShift<<16))>>20);
  142. }else if(type==1){
  143. dst[0*stride]=
  144. dst[1*stride]=
  145. dst[2*stride]=
  146. dst[3*stride]=
  147. dst[4*stride]=
  148. dst[5*stride]=
  149. dst[6*stride]=
  150. dst[7*stride]= av_clip_uint8(128 + ((xC4S4 * ip[0*8] + (IdctAdjustBeforeShift<<16))>>20));
  151. }else{
  152. if(ip[0*8]){
  153. int v= ((xC4S4 * ip[0*8] + (IdctAdjustBeforeShift<<16))>>20);
  154. dst[0*stride] = av_clip_uint8(dst[0*stride] + v);
  155. dst[1*stride] = av_clip_uint8(dst[1*stride] + v);
  156. dst[2*stride] = av_clip_uint8(dst[2*stride] + v);
  157. dst[3*stride] = av_clip_uint8(dst[3*stride] + v);
  158. dst[4*stride] = av_clip_uint8(dst[4*stride] + v);
  159. dst[5*stride] = av_clip_uint8(dst[5*stride] + v);
  160. dst[6*stride] = av_clip_uint8(dst[6*stride] + v);
  161. dst[7*stride] = av_clip_uint8(dst[7*stride] + v);
  162. }
  163. }
  164. }
  165. ip++; /* next column */
  166. dst++;
  167. }
  168. }
  169. void ff_vp3_idct_c(DCTELEM *block/* align 16*/){
  170. idct(NULL, 0, block, 0);
  171. }
  172. void ff_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. void ff_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. void ff_vp3_idct_dc_add_c(uint8_t *dest/*align 8*/, int line_size, const DCTELEM *block/*align 16*/){
  179. int i, dc = (block[0] + 15) >> 5;
  180. for(i = 0; i < 8; i++){
  181. dest[0] = av_clip_uint8(dest[0] + dc);
  182. dest[1] = av_clip_uint8(dest[1] + dc);
  183. dest[2] = av_clip_uint8(dest[2] + dc);
  184. dest[3] = av_clip_uint8(dest[3] + dc);
  185. dest[4] = av_clip_uint8(dest[4] + dc);
  186. dest[5] = av_clip_uint8(dest[5] + dc);
  187. dest[6] = av_clip_uint8(dest[6] + dc);
  188. dest[7] = av_clip_uint8(dest[7] + dc);
  189. dest += line_size;
  190. }
  191. }
  192. void ff_vp3_v_loop_filter_c(uint8_t *first_pixel, int stride, int *bounding_values)
  193. {
  194. unsigned char *end;
  195. int filter_value;
  196. const int nstride= -stride;
  197. for (end= first_pixel + 8; first_pixel < end; first_pixel++) {
  198. filter_value =
  199. (first_pixel[2 * nstride] - first_pixel[ stride])
  200. +3*(first_pixel[0 ] - first_pixel[nstride]);
  201. filter_value = bounding_values[(filter_value + 4) >> 3];
  202. first_pixel[nstride] = av_clip_uint8(first_pixel[nstride] + filter_value);
  203. first_pixel[0] = av_clip_uint8(first_pixel[0] - filter_value);
  204. }
  205. }
  206. void ff_vp3_h_loop_filter_c(uint8_t *first_pixel, int stride, int *bounding_values)
  207. {
  208. unsigned char *end;
  209. int filter_value;
  210. for (end= first_pixel + 8*stride; first_pixel != end; first_pixel += stride) {
  211. filter_value =
  212. (first_pixel[-2] - first_pixel[ 1])
  213. +3*(first_pixel[ 0] - first_pixel[-1]);
  214. filter_value = bounding_values[(filter_value + 4) >> 3];
  215. first_pixel[-1] = av_clip_uint8(first_pixel[-1] + filter_value);
  216. first_pixel[ 0] = av_clip_uint8(first_pixel[ 0] - filter_value);
  217. }
  218. }