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.

292 lines
10KB

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