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.

169 lines
5.9KB

  1. /*
  2. * H.264 IDCT
  3. * Copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. */
  22. /**
  23. * @file h264-idct.c
  24. * H.264 IDCT.
  25. * @author Michael Niedermayer <michaelni@gmx.at>
  26. */
  27. #include "dsputil.h"
  28. static av_always_inline void idct_internal(uint8_t *dst, DCTELEM *block, int stride, int block_stride, int shift, int add){
  29. int i;
  30. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  31. block[0] += 1<<(shift-1);
  32. for(i=0; i<4; i++){
  33. const int z0= block[0 + block_stride*i] + block[2 + block_stride*i];
  34. const int z1= block[0 + block_stride*i] - block[2 + block_stride*i];
  35. const int z2= (block[1 + block_stride*i]>>1) - block[3 + block_stride*i];
  36. const int z3= block[1 + block_stride*i] + (block[3 + block_stride*i]>>1);
  37. block[0 + block_stride*i]= z0 + z3;
  38. block[1 + block_stride*i]= z1 + z2;
  39. block[2 + block_stride*i]= z1 - z2;
  40. block[3 + block_stride*i]= z0 - z3;
  41. }
  42. for(i=0; i<4; i++){
  43. const int z0= block[i + block_stride*0] + block[i + block_stride*2];
  44. const int z1= block[i + block_stride*0] - block[i + block_stride*2];
  45. const int z2= (block[i + block_stride*1]>>1) - block[i + block_stride*3];
  46. const int z3= block[i + block_stride*1] + (block[i + block_stride*3]>>1);
  47. dst[i + 0*stride]= cm[ add*dst[i + 0*stride] + ((z0 + z3) >> shift) ];
  48. dst[i + 1*stride]= cm[ add*dst[i + 1*stride] + ((z1 + z2) >> shift) ];
  49. dst[i + 2*stride]= cm[ add*dst[i + 2*stride] + ((z1 - z2) >> shift) ];
  50. dst[i + 3*stride]= cm[ add*dst[i + 3*stride] + ((z0 - z3) >> shift) ];
  51. }
  52. }
  53. void ff_h264_idct_add_c(uint8_t *dst, DCTELEM *block, int stride){
  54. idct_internal(dst, block, stride, 4, 6, 1);
  55. }
  56. void ff_h264_lowres_idct_add_c(uint8_t *dst, int stride, DCTELEM *block){
  57. idct_internal(dst, block, stride, 8, 3, 1);
  58. }
  59. void ff_h264_lowres_idct_put_c(uint8_t *dst, int stride, DCTELEM *block){
  60. idct_internal(dst, block, stride, 8, 3, 0);
  61. }
  62. void ff_h264_idct8_add_c(uint8_t *dst, DCTELEM *block, int stride){
  63. int i;
  64. DCTELEM (*src)[8] = (DCTELEM(*)[8])block;
  65. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  66. block[0] += 32;
  67. for( i = 0; i < 8; i++ )
  68. {
  69. const int a0 = src[i][0] + src[i][4];
  70. const int a2 = src[i][0] - src[i][4];
  71. const int a4 = (src[i][2]>>1) - src[i][6];
  72. const int a6 = (src[i][6]>>1) + src[i][2];
  73. const int b0 = a0 + a6;
  74. const int b2 = a2 + a4;
  75. const int b4 = a2 - a4;
  76. const int b6 = a0 - a6;
  77. const int a1 = -src[i][3] + src[i][5] - src[i][7] - (src[i][7]>>1);
  78. const int a3 = src[i][1] + src[i][7] - src[i][3] - (src[i][3]>>1);
  79. const int a5 = -src[i][1] + src[i][7] + src[i][5] + (src[i][5]>>1);
  80. const int a7 = src[i][3] + src[i][5] + src[i][1] + (src[i][1]>>1);
  81. const int b1 = (a7>>2) + a1;
  82. const int b3 = a3 + (a5>>2);
  83. const int b5 = (a3>>2) - a5;
  84. const int b7 = a7 - (a1>>2);
  85. src[i][0] = b0 + b7;
  86. src[i][7] = b0 - b7;
  87. src[i][1] = b2 + b5;
  88. src[i][6] = b2 - b5;
  89. src[i][2] = b4 + b3;
  90. src[i][5] = b4 - b3;
  91. src[i][3] = b6 + b1;
  92. src[i][4] = b6 - b1;
  93. }
  94. for( i = 0; i < 8; i++ )
  95. {
  96. const int a0 = src[0][i] + src[4][i];
  97. const int a2 = src[0][i] - src[4][i];
  98. const int a4 = (src[2][i]>>1) - src[6][i];
  99. const int a6 = (src[6][i]>>1) + src[2][i];
  100. const int b0 = a0 + a6;
  101. const int b2 = a2 + a4;
  102. const int b4 = a2 - a4;
  103. const int b6 = a0 - a6;
  104. const int a1 = -src[3][i] + src[5][i] - src[7][i] - (src[7][i]>>1);
  105. const int a3 = src[1][i] + src[7][i] - src[3][i] - (src[3][i]>>1);
  106. const int a5 = -src[1][i] + src[7][i] + src[5][i] + (src[5][i]>>1);
  107. const int a7 = src[3][i] + src[5][i] + src[1][i] + (src[1][i]>>1);
  108. const int b1 = (a7>>2) + a1;
  109. const int b3 = a3 + (a5>>2);
  110. const int b5 = (a3>>2) - a5;
  111. const int b7 = a7 - (a1>>2);
  112. dst[i + 0*stride] = cm[ dst[i + 0*stride] + ((b0 + b7) >> 6) ];
  113. dst[i + 1*stride] = cm[ dst[i + 1*stride] + ((b2 + b5) >> 6) ];
  114. dst[i + 2*stride] = cm[ dst[i + 2*stride] + ((b4 + b3) >> 6) ];
  115. dst[i + 3*stride] = cm[ dst[i + 3*stride] + ((b6 + b1) >> 6) ];
  116. dst[i + 4*stride] = cm[ dst[i + 4*stride] + ((b6 - b1) >> 6) ];
  117. dst[i + 5*stride] = cm[ dst[i + 5*stride] + ((b4 - b3) >> 6) ];
  118. dst[i + 6*stride] = cm[ dst[i + 6*stride] + ((b2 - b5) >> 6) ];
  119. dst[i + 7*stride] = cm[ dst[i + 7*stride] + ((b0 - b7) >> 6) ];
  120. }
  121. }
  122. // assumes all AC coefs are 0
  123. void ff_h264_idct_dc_add_c(uint8_t *dst, DCTELEM *block, int stride){
  124. int i, j;
  125. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  126. int dc = (block[0] + 32) >> 6;
  127. for( j = 0; j < 4; j++ )
  128. {
  129. for( i = 0; i < 4; i++ )
  130. dst[i] = cm[ dst[i] + dc ];
  131. dst += stride;
  132. }
  133. }
  134. void ff_h264_idct8_dc_add_c(uint8_t *dst, DCTELEM *block, int stride){
  135. int i, j;
  136. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  137. int dc = (block[0] + 32) >> 6;
  138. for( j = 0; j < 8; j++ )
  139. {
  140. for( i = 0; i < 8; i++ )
  141. dst[i] = cm[ dst[i] + dc ];
  142. dst += stride;
  143. }
  144. }