h264idct.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. * @file libavcodec/h264idct.c
  23. * H.264 IDCT.
  24. * @author Michael Niedermayer <michaelni@gmx.at>
  25. */
  26. #include "dsputil.h"
  27. static av_always_inline void idct_internal(uint8_t *dst, DCTELEM *block, int stride, int block_stride, int shift, int add){
  28. int i;
  29. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  30. block[0] += 1<<(shift-1);
  31. for(i=0; i<4; i++){
  32. const int z0= block[0 + block_stride*i] + block[2 + block_stride*i];
  33. const int z1= block[0 + block_stride*i] - block[2 + block_stride*i];
  34. const int z2= (block[1 + block_stride*i]>>1) - block[3 + block_stride*i];
  35. const int z3= block[1 + block_stride*i] + (block[3 + block_stride*i]>>1);
  36. block[0 + block_stride*i]= z0 + z3;
  37. block[1 + block_stride*i]= z1 + z2;
  38. block[2 + block_stride*i]= z1 - z2;
  39. block[3 + block_stride*i]= z0 - z3;
  40. }
  41. for(i=0; i<4; i++){
  42. const int z0= block[i + block_stride*0] + block[i + block_stride*2];
  43. const int z1= block[i + block_stride*0] - block[i + block_stride*2];
  44. const int z2= (block[i + block_stride*1]>>1) - block[i + block_stride*3];
  45. const int z3= block[i + block_stride*1] + (block[i + block_stride*3]>>1);
  46. dst[i + 0*stride]= cm[ add*dst[i + 0*stride] + ((z0 + z3) >> shift) ];
  47. dst[i + 1*stride]= cm[ add*dst[i + 1*stride] + ((z1 + z2) >> shift) ];
  48. dst[i + 2*stride]= cm[ add*dst[i + 2*stride] + ((z1 - z2) >> shift) ];
  49. dst[i + 3*stride]= cm[ add*dst[i + 3*stride] + ((z0 - z3) >> shift) ];
  50. }
  51. }
  52. void ff_h264_idct_add_c(uint8_t *dst, DCTELEM *block, int stride){
  53. idct_internal(dst, block, stride, 4, 6, 1);
  54. }
  55. void ff_h264_lowres_idct_add_c(uint8_t *dst, int stride, DCTELEM *block){
  56. idct_internal(dst, block, stride, 8, 3, 1);
  57. }
  58. void ff_h264_lowres_idct_put_c(uint8_t *dst, int stride, DCTELEM *block){
  59. idct_internal(dst, block, stride, 8, 3, 0);
  60. }
  61. void ff_h264_idct8_add_c(uint8_t *dst, DCTELEM *block, int stride){
  62. int i;
  63. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  64. block[0] += 32;
  65. for( i = 0; i < 8; i++ )
  66. {
  67. const int a0 = block[0+i*8] + block[4+i*8];
  68. const int a2 = block[0+i*8] - block[4+i*8];
  69. const int a4 = (block[2+i*8]>>1) - block[6+i*8];
  70. const int a6 = (block[6+i*8]>>1) + block[2+i*8];
  71. const int b0 = a0 + a6;
  72. const int b2 = a2 + a4;
  73. const int b4 = a2 - a4;
  74. const int b6 = a0 - a6;
  75. const int a1 = -block[3+i*8] + block[5+i*8] - block[7+i*8] - (block[7+i*8]>>1);
  76. const int a3 = block[1+i*8] + block[7+i*8] - block[3+i*8] - (block[3+i*8]>>1);
  77. const int a5 = -block[1+i*8] + block[7+i*8] + block[5+i*8] + (block[5+i*8]>>1);
  78. const int a7 = block[3+i*8] + block[5+i*8] + block[1+i*8] + (block[1+i*8]>>1);
  79. const int b1 = (a7>>2) + a1;
  80. const int b3 = a3 + (a5>>2);
  81. const int b5 = (a3>>2) - a5;
  82. const int b7 = a7 - (a1>>2);
  83. block[0+i*8] = b0 + b7;
  84. block[7+i*8] = b0 - b7;
  85. block[1+i*8] = b2 + b5;
  86. block[6+i*8] = b2 - b5;
  87. block[2+i*8] = b4 + b3;
  88. block[5+i*8] = b4 - b3;
  89. block[3+i*8] = b6 + b1;
  90. block[4+i*8] = b6 - b1;
  91. }
  92. for( i = 0; i < 8; i++ )
  93. {
  94. const int a0 = block[i+0*8] + block[i+4*8];
  95. const int a2 = block[i+0*8] - block[i+4*8];
  96. const int a4 = (block[i+2*8]>>1) - block[i+6*8];
  97. const int a6 = (block[i+6*8]>>1) + block[i+2*8];
  98. const int b0 = a0 + a6;
  99. const int b2 = a2 + a4;
  100. const int b4 = a2 - a4;
  101. const int b6 = a0 - a6;
  102. const int a1 = -block[i+3*8] + block[i+5*8] - block[i+7*8] - (block[i+7*8]>>1);
  103. const int a3 = block[i+1*8] + block[i+7*8] - block[i+3*8] - (block[i+3*8]>>1);
  104. const int a5 = -block[i+1*8] + block[i+7*8] + block[i+5*8] + (block[i+5*8]>>1);
  105. const int a7 = block[i+3*8] + block[i+5*8] + block[i+1*8] + (block[i+1*8]>>1);
  106. const int b1 = (a7>>2) + a1;
  107. const int b3 = a3 + (a5>>2);
  108. const int b5 = (a3>>2) - a5;
  109. const int b7 = a7 - (a1>>2);
  110. dst[i + 0*stride] = cm[ dst[i + 0*stride] + ((b0 + b7) >> 6) ];
  111. dst[i + 1*stride] = cm[ dst[i + 1*stride] + ((b2 + b5) >> 6) ];
  112. dst[i + 2*stride] = cm[ dst[i + 2*stride] + ((b4 + b3) >> 6) ];
  113. dst[i + 3*stride] = cm[ dst[i + 3*stride] + ((b6 + b1) >> 6) ];
  114. dst[i + 4*stride] = cm[ dst[i + 4*stride] + ((b6 - b1) >> 6) ];
  115. dst[i + 5*stride] = cm[ dst[i + 5*stride] + ((b4 - b3) >> 6) ];
  116. dst[i + 6*stride] = cm[ dst[i + 6*stride] + ((b2 - b5) >> 6) ];
  117. dst[i + 7*stride] = cm[ dst[i + 7*stride] + ((b0 - b7) >> 6) ];
  118. }
  119. }
  120. // assumes all AC coefs are 0
  121. void ff_h264_idct_dc_add_c(uint8_t *dst, DCTELEM *block, int stride){
  122. int i, j;
  123. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  124. int dc = (block[0] + 32) >> 6;
  125. for( j = 0; j < 4; j++ )
  126. {
  127. for( i = 0; i < 4; i++ )
  128. dst[i] = cm[ dst[i] + dc ];
  129. dst += stride;
  130. }
  131. }
  132. void ff_h264_idct8_dc_add_c(uint8_t *dst, DCTELEM *block, int stride){
  133. int i, j;
  134. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  135. int dc = (block[0] + 32) >> 6;
  136. for( j = 0; j < 8; j++ )
  137. {
  138. for( i = 0; i < 8; i++ )
  139. dst[i] = cm[ dst[i] + dc ];
  140. dst += stride;
  141. }
  142. }
  143. //FIXME this table is a duplicate from h264data.h, and will be removed once the tables from, h264 have been split
  144. static const uint8_t scan8[16 + 2*4]={
  145. 4+1*8, 5+1*8, 4+2*8, 5+2*8,
  146. 6+1*8, 7+1*8, 6+2*8, 7+2*8,
  147. 4+3*8, 5+3*8, 4+4*8, 5+4*8,
  148. 6+3*8, 7+3*8, 6+4*8, 7+4*8,
  149. 1+1*8, 2+1*8,
  150. 1+2*8, 2+2*8,
  151. 1+4*8, 2+4*8,
  152. 1+5*8, 2+5*8,
  153. };
  154. void ff_h264_idct_add16_c(uint8_t *dst, const int *block_offset, DCTELEM *block, int stride, const uint8_t nnzc[6*8]){
  155. int i;
  156. for(i=0; i<16; i++){
  157. int nnz = nnzc[ scan8[i] ];
  158. if(nnz){
  159. if(nnz==1 && block[i*16]) ff_h264_idct_dc_add_c(dst + block_offset[i], block + i*16, stride);
  160. else idct_internal (dst + block_offset[i], block + i*16, stride, 4, 6, 1);
  161. }
  162. }
  163. }
  164. void ff_h264_idct_add16intra_c(uint8_t *dst, const int *block_offset, DCTELEM *block, int stride, const uint8_t nnzc[6*8]){
  165. int i;
  166. for(i=0; i<16; i++){
  167. if(nnzc[ scan8[i] ]) idct_internal (dst + block_offset[i], block + i*16, stride, 4, 6, 1);
  168. else if(block[i*16]) ff_h264_idct_dc_add_c(dst + block_offset[i], block + i*16, stride);
  169. }
  170. }
  171. void ff_h264_idct8_add4_c(uint8_t *dst, const int *block_offset, DCTELEM *block, int stride, const uint8_t nnzc[6*8]){
  172. int i;
  173. for(i=0; i<16; i+=4){
  174. int nnz = nnzc[ scan8[i] ];
  175. if(nnz){
  176. if(nnz==1 && block[i*16]) ff_h264_idct8_dc_add_c(dst + block_offset[i], block + i*16, stride);
  177. else ff_h264_idct8_add_c (dst + block_offset[i], block + i*16, stride);
  178. }
  179. }
  180. }
  181. void ff_h264_idct_add8_c(uint8_t **dest, const int *block_offset, DCTELEM *block, int stride, const uint8_t nnzc[6*8]){
  182. int i;
  183. for(i=16; i<16+8; i++){
  184. if(nnzc[ scan8[i] ])
  185. ff_h264_idct_add_c (dest[(i&4)>>2] + block_offset[i], block + i*16, stride);
  186. else if(block[i*16])
  187. ff_h264_idct_dc_add_c(dest[(i&4)>>2] + block_offset[i], block + i*16, stride);
  188. }
  189. }