vp3dsp.c 6.8 KB

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