h264idct_template.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. * H.264 IDCT
  3. * Copyright (c) 2004-2011 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
  23. * H.264 IDCT.
  24. * @author Michael Niedermayer <michaelni@gmx.at>
  25. */
  26. #include "bit_depth_template.c"
  27. #ifndef AVCODEC_H264IDCT_INTERNAL_H
  28. #define AVCODEC_H264IDCT_INTERNAL_H
  29. //FIXME this table is a duplicate from h264data.h, and will be removed once the tables from, h264 have been split
  30. static const uint8_t scan8[16*3]={
  31. 4+ 1*8, 5+ 1*8, 4+ 2*8, 5+ 2*8,
  32. 6+ 1*8, 7+ 1*8, 6+ 2*8, 7+ 2*8,
  33. 4+ 3*8, 5+ 3*8, 4+ 4*8, 5+ 4*8,
  34. 6+ 3*8, 7+ 3*8, 6+ 4*8, 7+ 4*8,
  35. 4+ 6*8, 5+ 6*8, 4+ 7*8, 5+ 7*8,
  36. 6+ 6*8, 7+ 6*8, 6+ 7*8, 7+ 7*8,
  37. 4+ 8*8, 5+ 8*8, 4+ 9*8, 5+ 9*8,
  38. 6+ 8*8, 7+ 8*8, 6+ 9*8, 7+ 9*8,
  39. 4+11*8, 5+11*8, 4+12*8, 5+12*8,
  40. 6+11*8, 7+11*8, 6+12*8, 7+12*8,
  41. 4+13*8, 5+13*8, 4+14*8, 5+14*8,
  42. 6+13*8, 7+13*8, 6+14*8, 7+14*8
  43. };
  44. #endif
  45. static av_always_inline void FUNCC(idct_internal)(uint8_t *p_dst, DCTELEM *p_block, int stride, int block_stride, int shift, int add){
  46. int i;
  47. INIT_CLIP
  48. pixel *dst = (pixel*)p_dst;
  49. dctcoef *block = (dctcoef*)p_block;
  50. stride >>= sizeof(pixel)-1;
  51. block[0] += 1<<(shift-1);
  52. for(i=0; i<4; i++){
  53. const int z0= block[i + block_stride*0] + block[i + block_stride*2];
  54. const int z1= block[i + block_stride*0] - block[i + block_stride*2];
  55. const int z2= (block[i + block_stride*1]>>1) - block[i + block_stride*3];
  56. const int z3= block[i + block_stride*1] + (block[i + block_stride*3]>>1);
  57. block[i + block_stride*0]= z0 + z3;
  58. block[i + block_stride*1]= z1 + z2;
  59. block[i + block_stride*2]= z1 - z2;
  60. block[i + block_stride*3]= z0 - z3;
  61. }
  62. for(i=0; i<4; i++){
  63. const int z0= block[0 + block_stride*i] + block[2 + block_stride*i];
  64. const int z1= block[0 + block_stride*i] - block[2 + block_stride*i];
  65. const int z2= (block[1 + block_stride*i]>>1) - block[3 + block_stride*i];
  66. const int z3= block[1 + block_stride*i] + (block[3 + block_stride*i]>>1);
  67. dst[i + 0*stride]= CLIP(add*dst[i + 0*stride] + ((z0 + z3) >> shift));
  68. dst[i + 1*stride]= CLIP(add*dst[i + 1*stride] + ((z1 + z2) >> shift));
  69. dst[i + 2*stride]= CLIP(add*dst[i + 2*stride] + ((z1 - z2) >> shift));
  70. dst[i + 3*stride]= CLIP(add*dst[i + 3*stride] + ((z0 - z3) >> shift));
  71. }
  72. }
  73. void FUNCC(ff_h264_idct_add)(uint8_t *dst, DCTELEM *block, int stride){
  74. FUNCC(idct_internal)(dst, block, stride, 4, 6, 1);
  75. }
  76. void FUNCC(ff_h264_lowres_idct_add)(uint8_t *dst, int stride, DCTELEM *block){
  77. FUNCC(idct_internal)(dst, block, stride, 8, 3, 1);
  78. }
  79. void FUNCC(ff_h264_lowres_idct_put)(uint8_t *dst, int stride, DCTELEM *block){
  80. FUNCC(idct_internal)(dst, block, stride, 8, 3, 0);
  81. }
  82. void FUNCC(ff_h264_idct8_add)(uint8_t *p_dst, DCTELEM *p_block, int stride){
  83. int i;
  84. INIT_CLIP
  85. pixel *dst = (pixel*)p_dst;
  86. dctcoef *block = (dctcoef*)p_block;
  87. stride >>= sizeof(pixel)-1;
  88. block[0] += 32;
  89. for( i = 0; i < 8; i++ )
  90. {
  91. const int a0 = block[i+0*8] + block[i+4*8];
  92. const int a2 = block[i+0*8] - block[i+4*8];
  93. const int a4 = (block[i+2*8]>>1) - block[i+6*8];
  94. const int a6 = (block[i+6*8]>>1) + block[i+2*8];
  95. const int b0 = a0 + a6;
  96. const int b2 = a2 + a4;
  97. const int b4 = a2 - a4;
  98. const int b6 = a0 - a6;
  99. const int a1 = -block[i+3*8] + block[i+5*8] - block[i+7*8] - (block[i+7*8]>>1);
  100. const int a3 = block[i+1*8] + block[i+7*8] - block[i+3*8] - (block[i+3*8]>>1);
  101. const int a5 = -block[i+1*8] + block[i+7*8] + block[i+5*8] + (block[i+5*8]>>1);
  102. const int a7 = block[i+3*8] + block[i+5*8] + block[i+1*8] + (block[i+1*8]>>1);
  103. const int b1 = (a7>>2) + a1;
  104. const int b3 = a3 + (a5>>2);
  105. const int b5 = (a3>>2) - a5;
  106. const int b7 = a7 - (a1>>2);
  107. block[i+0*8] = b0 + b7;
  108. block[i+7*8] = b0 - b7;
  109. block[i+1*8] = b2 + b5;
  110. block[i+6*8] = b2 - b5;
  111. block[i+2*8] = b4 + b3;
  112. block[i+5*8] = b4 - b3;
  113. block[i+3*8] = b6 + b1;
  114. block[i+4*8] = b6 - b1;
  115. }
  116. for( i = 0; i < 8; i++ )
  117. {
  118. const int a0 = block[0+i*8] + block[4+i*8];
  119. const int a2 = block[0+i*8] - block[4+i*8];
  120. const int a4 = (block[2+i*8]>>1) - block[6+i*8];
  121. const int a6 = (block[6+i*8]>>1) + block[2+i*8];
  122. const int b0 = a0 + a6;
  123. const int b2 = a2 + a4;
  124. const int b4 = a2 - a4;
  125. const int b6 = a0 - a6;
  126. const int a1 = -block[3+i*8] + block[5+i*8] - block[7+i*8] - (block[7+i*8]>>1);
  127. const int a3 = block[1+i*8] + block[7+i*8] - block[3+i*8] - (block[3+i*8]>>1);
  128. const int a5 = -block[1+i*8] + block[7+i*8] + block[5+i*8] + (block[5+i*8]>>1);
  129. const int a7 = block[3+i*8] + block[5+i*8] + block[1+i*8] + (block[1+i*8]>>1);
  130. const int b1 = (a7>>2) + a1;
  131. const int b3 = a3 + (a5>>2);
  132. const int b5 = (a3>>2) - a5;
  133. const int b7 = a7 - (a1>>2);
  134. dst[i + 0*stride] = CLIP( dst[i + 0*stride] + ((b0 + b7) >> 6) );
  135. dst[i + 1*stride] = CLIP( dst[i + 1*stride] + ((b2 + b5) >> 6) );
  136. dst[i + 2*stride] = CLIP( dst[i + 2*stride] + ((b4 + b3) >> 6) );
  137. dst[i + 3*stride] = CLIP( dst[i + 3*stride] + ((b6 + b1) >> 6) );
  138. dst[i + 4*stride] = CLIP( dst[i + 4*stride] + ((b6 - b1) >> 6) );
  139. dst[i + 5*stride] = CLIP( dst[i + 5*stride] + ((b4 - b3) >> 6) );
  140. dst[i + 6*stride] = CLIP( dst[i + 6*stride] + ((b2 - b5) >> 6) );
  141. dst[i + 7*stride] = CLIP( dst[i + 7*stride] + ((b0 - b7) >> 6) );
  142. }
  143. }
  144. // assumes all AC coefs are 0
  145. void FUNCC(ff_h264_idct_dc_add)(uint8_t *p_dst, DCTELEM *block, int stride){
  146. int i, j;
  147. int dc = (((dctcoef*)block)[0] + 32) >> 6;
  148. INIT_CLIP
  149. pixel *dst = (pixel*)p_dst;
  150. stride >>= sizeof(pixel)-1;
  151. for( j = 0; j < 4; j++ )
  152. {
  153. for( i = 0; i < 4; i++ )
  154. dst[i] = CLIP( dst[i] + dc );
  155. dst += stride;
  156. }
  157. }
  158. void FUNCC(ff_h264_idct8_dc_add)(uint8_t *p_dst, DCTELEM *block, int stride){
  159. int i, j;
  160. int dc = (((dctcoef*)block)[0] + 32) >> 6;
  161. INIT_CLIP
  162. pixel *dst = (pixel*)p_dst;
  163. stride >>= sizeof(pixel)-1;
  164. for( j = 0; j < 8; j++ )
  165. {
  166. for( i = 0; i < 8; i++ )
  167. dst[i] = CLIP( dst[i] + dc );
  168. dst += stride;
  169. }
  170. }
  171. void FUNCC(ff_h264_idct_add16)(uint8_t *dst, const int *block_offset, DCTELEM *block, int stride, const uint8_t nnzc[15*8]){
  172. int i;
  173. for(i=0; i<16; i++){
  174. int nnz = nnzc[ scan8[i] ];
  175. if(nnz){
  176. if(nnz==1 && ((dctcoef*)block)[i*16]) FUNCC(ff_h264_idct_dc_add)(dst + block_offset[i], block + i*16*sizeof(pixel), stride);
  177. else FUNCC(idct_internal )(dst + block_offset[i], block + i*16*sizeof(pixel), stride, 4, 6, 1);
  178. }
  179. }
  180. }
  181. void FUNCC(ff_h264_idct_add16intra)(uint8_t *dst, const int *block_offset, DCTELEM *block, int stride, const uint8_t nnzc[15*8]){
  182. int i;
  183. for(i=0; i<16; i++){
  184. if(nnzc[ scan8[i] ]) FUNCC(idct_internal )(dst + block_offset[i], block + i*16*sizeof(pixel), stride, 4, 6, 1);
  185. else if(((dctcoef*)block)[i*16]) FUNCC(ff_h264_idct_dc_add)(dst + block_offset[i], block + i*16*sizeof(pixel), stride);
  186. }
  187. }
  188. void FUNCC(ff_h264_idct8_add4)(uint8_t *dst, const int *block_offset, DCTELEM *block, int stride, const uint8_t nnzc[15*8]){
  189. int i;
  190. for(i=0; i<16; i+=4){
  191. int nnz = nnzc[ scan8[i] ];
  192. if(nnz){
  193. if(nnz==1 && ((dctcoef*)block)[i*16]) FUNCC(ff_h264_idct8_dc_add)(dst + block_offset[i], block + i*16*sizeof(pixel), stride);
  194. else FUNCC(ff_h264_idct8_add )(dst + block_offset[i], block + i*16*sizeof(pixel), stride);
  195. }
  196. }
  197. }
  198. void FUNCC(ff_h264_idct_add8)(uint8_t **dest, const int *block_offset, DCTELEM *block, int stride, const uint8_t nnzc[15*8]){
  199. int i, j;
  200. for(j=1; j<3; j++){
  201. for(i=j*16; i<j*16+4; i++){
  202. if(nnzc[ scan8[i] ])
  203. FUNCC(ff_h264_idct_add )(dest[j-1] + block_offset[i], block + i*16*sizeof(pixel), stride);
  204. else if(((dctcoef*)block)[i*16])
  205. FUNCC(ff_h264_idct_dc_add)(dest[j-1] + block_offset[i], block + i*16*sizeof(pixel), stride);
  206. }
  207. }
  208. }
  209. /**
  210. * IDCT transforms the 16 dc values and dequantizes them.
  211. * @param qmul quantization parameter
  212. */
  213. void FUNCC(ff_h264_luma_dc_dequant_idct)(DCTELEM *p_output, DCTELEM *p_input, int qmul){
  214. #define stride 16
  215. int i;
  216. int temp[16];
  217. static const uint8_t x_offset[4]={0, 2*stride, 8*stride, 10*stride};
  218. dctcoef *input = (dctcoef*)p_input;
  219. dctcoef *output = (dctcoef*)p_output;
  220. for(i=0; i<4; i++){
  221. const int z0= input[4*i+0] + input[4*i+1];
  222. const int z1= input[4*i+0] - input[4*i+1];
  223. const int z2= input[4*i+2] - input[4*i+3];
  224. const int z3= input[4*i+2] + input[4*i+3];
  225. temp[4*i+0]= z0+z3;
  226. temp[4*i+1]= z0-z3;
  227. temp[4*i+2]= z1-z2;
  228. temp[4*i+3]= z1+z2;
  229. }
  230. for(i=0; i<4; i++){
  231. const int offset= x_offset[i];
  232. const int z0= temp[4*0+i] + temp[4*2+i];
  233. const int z1= temp[4*0+i] - temp[4*2+i];
  234. const int z2= temp[4*1+i] - temp[4*3+i];
  235. const int z3= temp[4*1+i] + temp[4*3+i];
  236. output[stride* 0+offset]= ((((z0 + z3)*qmul + 128 ) >> 8));
  237. output[stride* 1+offset]= ((((z1 + z2)*qmul + 128 ) >> 8));
  238. output[stride* 4+offset]= ((((z1 - z2)*qmul + 128 ) >> 8));
  239. output[stride* 5+offset]= ((((z0 - z3)*qmul + 128 ) >> 8));
  240. }
  241. #undef stride
  242. }
  243. void FUNCC(ff_h264_chroma_dc_dequant_idct)(DCTELEM *p_block, int qmul){
  244. const int stride= 16*2;
  245. const int xStride= 16;
  246. int a,b,c,d,e;
  247. dctcoef *block = (dctcoef*)p_block;
  248. a= block[stride*0 + xStride*0];
  249. b= block[stride*0 + xStride*1];
  250. c= block[stride*1 + xStride*0];
  251. d= block[stride*1 + xStride*1];
  252. e= a-b;
  253. a= a+b;
  254. b= c-d;
  255. c= c+d;
  256. block[stride*0 + xStride*0]= ((a+c)*qmul) >> 7;
  257. block[stride*0 + xStride*1]= ((e+b)*qmul) >> 7;
  258. block[stride*1 + xStride*0]= ((a-c)*qmul) >> 7;
  259. block[stride*1 + xStride*1]= ((e-b)*qmul) >> 7;
  260. }