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.

343 lines
13KB

  1. /*
  2. * H.26L/H.264/AVC/JVT/14496-10/... encoder/decoder
  3. * Copyright (c) 2003-2010 Michael Niedermayer <michaelni@gmx.at>
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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 / AVC / MPEG4 part10 DSP functions.
  24. * @author Michael Niedermayer <michaelni@gmx.at>
  25. */
  26. #include <stdint.h>
  27. #include "avcodec.h"
  28. #include "h264dsp.h"
  29. #define op_scale1(x) block[x] = av_clip_uint8( (block[x]*weight + offset) >> log2_denom )
  30. #define op_scale2(x) dst[x] = av_clip_uint8( (src[x]*weights + dst[x]*weightd + offset) >> (log2_denom+1))
  31. #define H264_WEIGHT(W,H) \
  32. static void weight_h264_pixels ## W ## x ## H ## _c(uint8_t *block, int stride, int log2_denom, int weight, int offset){ \
  33. int y; \
  34. offset <<= log2_denom; \
  35. if(log2_denom) offset += 1<<(log2_denom-1); \
  36. for(y=0; y<H; y++, block += stride){ \
  37. op_scale1(0); \
  38. op_scale1(1); \
  39. if(W==2) continue; \
  40. op_scale1(2); \
  41. op_scale1(3); \
  42. if(W==4) continue; \
  43. op_scale1(4); \
  44. op_scale1(5); \
  45. op_scale1(6); \
  46. op_scale1(7); \
  47. if(W==8) continue; \
  48. op_scale1(8); \
  49. op_scale1(9); \
  50. op_scale1(10); \
  51. op_scale1(11); \
  52. op_scale1(12); \
  53. op_scale1(13); \
  54. op_scale1(14); \
  55. op_scale1(15); \
  56. } \
  57. } \
  58. static void biweight_h264_pixels ## W ## x ## H ## _c(uint8_t *dst, uint8_t *src, int stride, int log2_denom, int weightd, int weights, int offset){ \
  59. int y; \
  60. offset = ((offset + 1) | 1) << log2_denom; \
  61. for(y=0; y<H; y++, dst += stride, src += stride){ \
  62. op_scale2(0); \
  63. op_scale2(1); \
  64. if(W==2) continue; \
  65. op_scale2(2); \
  66. op_scale2(3); \
  67. if(W==4) continue; \
  68. op_scale2(4); \
  69. op_scale2(5); \
  70. op_scale2(6); \
  71. op_scale2(7); \
  72. if(W==8) continue; \
  73. op_scale2(8); \
  74. op_scale2(9); \
  75. op_scale2(10); \
  76. op_scale2(11); \
  77. op_scale2(12); \
  78. op_scale2(13); \
  79. op_scale2(14); \
  80. op_scale2(15); \
  81. } \
  82. }
  83. H264_WEIGHT(16,16)
  84. H264_WEIGHT(16,8)
  85. H264_WEIGHT(8,16)
  86. H264_WEIGHT(8,8)
  87. H264_WEIGHT(8,4)
  88. H264_WEIGHT(4,8)
  89. H264_WEIGHT(4,4)
  90. H264_WEIGHT(4,2)
  91. H264_WEIGHT(2,4)
  92. H264_WEIGHT(2,2)
  93. #undef op_scale1
  94. #undef op_scale2
  95. #undef H264_WEIGHT
  96. static av_always_inline av_flatten void h264_loop_filter_luma_c(uint8_t *pix, int xstride, int ystride, int inner_iters, int alpha, int beta, int8_t *tc0)
  97. {
  98. int i, d;
  99. for( i = 0; i < 4; i++ ) {
  100. if( tc0[i] < 0 ) {
  101. pix += inner_iters*ystride;
  102. continue;
  103. }
  104. for( d = 0; d < inner_iters; d++ ) {
  105. const int p0 = pix[-1*xstride];
  106. const int p1 = pix[-2*xstride];
  107. const int p2 = pix[-3*xstride];
  108. const int q0 = pix[0];
  109. const int q1 = pix[1*xstride];
  110. const int q2 = pix[2*xstride];
  111. if( FFABS( p0 - q0 ) < alpha &&
  112. FFABS( p1 - p0 ) < beta &&
  113. FFABS( q1 - q0 ) < beta ) {
  114. int tc = tc0[i];
  115. int i_delta;
  116. if( FFABS( p2 - p0 ) < beta ) {
  117. if(tc0[i])
  118. pix[-2*xstride] = p1 + av_clip( (( p2 + ( ( p0 + q0 + 1 ) >> 1 ) ) >> 1) - p1, -tc0[i], tc0[i] );
  119. tc++;
  120. }
  121. if( FFABS( q2 - q0 ) < beta ) {
  122. if(tc0[i])
  123. pix[ xstride] = q1 + av_clip( (( q2 + ( ( p0 + q0 + 1 ) >> 1 ) ) >> 1) - q1, -tc0[i], tc0[i] );
  124. tc++;
  125. }
  126. i_delta = av_clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
  127. pix[-xstride] = av_clip_uint8( p0 + i_delta ); /* p0' */
  128. pix[0] = av_clip_uint8( q0 - i_delta ); /* q0' */
  129. }
  130. pix += ystride;
  131. }
  132. }
  133. }
  134. static void h264_v_loop_filter_luma_c(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0)
  135. {
  136. h264_loop_filter_luma_c(pix, stride, 1, 4, alpha, beta, tc0);
  137. }
  138. static void h264_h_loop_filter_luma_c(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0)
  139. {
  140. h264_loop_filter_luma_c(pix, 1, stride, 4, alpha, beta, tc0);
  141. }
  142. static void h264_h_loop_filter_luma_mbaff_c(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0)
  143. {
  144. h264_loop_filter_luma_c(pix, 1, stride, 2, alpha, beta, tc0);
  145. }
  146. static av_always_inline av_flatten void h264_loop_filter_luma_intra_c(uint8_t *pix, int xstride, int ystride, int inner_iters, int alpha, int beta)
  147. {
  148. int d;
  149. for( d = 0; d < 4 * inner_iters; d++ ) {
  150. const int p2 = pix[-3*xstride];
  151. const int p1 = pix[-2*xstride];
  152. const int p0 = pix[-1*xstride];
  153. const int q0 = pix[ 0*xstride];
  154. const int q1 = pix[ 1*xstride];
  155. const int q2 = pix[ 2*xstride];
  156. if( FFABS( p0 - q0 ) < alpha &&
  157. FFABS( p1 - p0 ) < beta &&
  158. FFABS( q1 - q0 ) < beta ) {
  159. if(FFABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
  160. if( FFABS( p2 - p0 ) < beta)
  161. {
  162. const int p3 = pix[-4*xstride];
  163. /* p0', p1', p2' */
  164. pix[-1*xstride] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
  165. pix[-2*xstride] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
  166. pix[-3*xstride] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
  167. } else {
  168. /* p0' */
  169. pix[-1*xstride] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  170. }
  171. if( FFABS( q2 - q0 ) < beta)
  172. {
  173. const int q3 = pix[3*xstride];
  174. /* q0', q1', q2' */
  175. pix[0*xstride] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
  176. pix[1*xstride] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
  177. pix[2*xstride] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
  178. } else {
  179. /* q0' */
  180. pix[0*xstride] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  181. }
  182. }else{
  183. /* p0', q0' */
  184. pix[-1*xstride] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  185. pix[ 0*xstride] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  186. }
  187. }
  188. pix += ystride;
  189. }
  190. }
  191. static void h264_v_loop_filter_luma_intra_c(uint8_t *pix, int stride, int alpha, int beta)
  192. {
  193. h264_loop_filter_luma_intra_c(pix, stride, 1, 4, alpha, beta);
  194. }
  195. static void h264_h_loop_filter_luma_intra_c(uint8_t *pix, int stride, int alpha, int beta)
  196. {
  197. h264_loop_filter_luma_intra_c(pix, 1, stride, 4, alpha, beta);
  198. }
  199. static void h264_h_loop_filter_luma_mbaff_intra_c(uint8_t *pix, int stride, int alpha, int beta)
  200. {
  201. h264_loop_filter_luma_intra_c(pix, 1, stride, 2, alpha, beta);
  202. }
  203. static av_always_inline av_flatten void h264_loop_filter_chroma_c(uint8_t *pix, int xstride, int ystride, int inner_iters, int alpha, int beta, int8_t *tc0)
  204. {
  205. int i, d;
  206. for( i = 0; i < 4; i++ ) {
  207. const int tc = tc0[i];
  208. if( tc <= 0 ) {
  209. pix += inner_iters*ystride;
  210. continue;
  211. }
  212. for( d = 0; d < inner_iters; d++ ) {
  213. const int p0 = pix[-1*xstride];
  214. const int p1 = pix[-2*xstride];
  215. const int q0 = pix[0];
  216. const int q1 = pix[1*xstride];
  217. if( FFABS( p0 - q0 ) < alpha &&
  218. FFABS( p1 - p0 ) < beta &&
  219. FFABS( q1 - q0 ) < beta ) {
  220. int delta = av_clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
  221. pix[-xstride] = av_clip_uint8( p0 + delta ); /* p0' */
  222. pix[0] = av_clip_uint8( q0 - delta ); /* q0' */
  223. }
  224. pix += ystride;
  225. }
  226. }
  227. }
  228. static void h264_v_loop_filter_chroma_c(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0)
  229. {
  230. h264_loop_filter_chroma_c(pix, stride, 1, 2, alpha, beta, tc0);
  231. }
  232. static void h264_h_loop_filter_chroma_c(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0)
  233. {
  234. h264_loop_filter_chroma_c(pix, 1, stride, 2, alpha, beta, tc0);
  235. }
  236. static void h264_h_loop_filter_chroma_mbaff_c(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0)
  237. {
  238. h264_loop_filter_chroma_c(pix, 1, stride, 1, alpha, beta, tc0);
  239. }
  240. static av_always_inline av_flatten void h264_loop_filter_chroma_intra_c(uint8_t *pix, int xstride, int ystride, int inner_iters, int alpha, int beta)
  241. {
  242. int d;
  243. for( d = 0; d < 4 * inner_iters; d++ ) {
  244. const int p0 = pix[-1*xstride];
  245. const int p1 = pix[-2*xstride];
  246. const int q0 = pix[0];
  247. const int q1 = pix[1*xstride];
  248. if( FFABS( p0 - q0 ) < alpha &&
  249. FFABS( p1 - p0 ) < beta &&
  250. FFABS( q1 - q0 ) < beta ) {
  251. pix[-xstride] = ( 2*p1 + p0 + q1 + 2 ) >> 2; /* p0' */
  252. pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2; /* q0' */
  253. }
  254. pix += ystride;
  255. }
  256. }
  257. static void h264_v_loop_filter_chroma_intra_c(uint8_t *pix, int stride, int alpha, int beta)
  258. {
  259. h264_loop_filter_chroma_intra_c(pix, stride, 1, 2, alpha, beta);
  260. }
  261. static void h264_h_loop_filter_chroma_intra_c(uint8_t *pix, int stride, int alpha, int beta)
  262. {
  263. h264_loop_filter_chroma_intra_c(pix, 1, stride, 2, alpha, beta);
  264. }
  265. static void h264_h_loop_filter_chroma_mbaff_intra_c(uint8_t *pix, int stride, int alpha, int beta)
  266. {
  267. h264_loop_filter_chroma_intra_c(pix, 1, stride, 1, alpha, beta);
  268. }
  269. void ff_h264dsp_init(H264DSPContext *c)
  270. {
  271. c->h264_idct_add= ff_h264_idct_add_c;
  272. c->h264_idct8_add= ff_h264_idct8_add_c;
  273. c->h264_idct_dc_add= ff_h264_idct_dc_add_c;
  274. c->h264_idct8_dc_add= ff_h264_idct8_dc_add_c;
  275. c->h264_idct_add16 = ff_h264_idct_add16_c;
  276. c->h264_idct8_add4 = ff_h264_idct8_add4_c;
  277. c->h264_idct_add8 = ff_h264_idct_add8_c;
  278. c->h264_idct_add16intra= ff_h264_idct_add16intra_c;
  279. c->h264_luma_dc_dequant_idct= ff_h264_luma_dc_dequant_idct_c;
  280. c->h264_chroma_dc_dequant_idct= ff_h264_chroma_dc_dequant_idct_c;
  281. c->weight_h264_pixels_tab[0]= weight_h264_pixels16x16_c;
  282. c->weight_h264_pixels_tab[1]= weight_h264_pixels16x8_c;
  283. c->weight_h264_pixels_tab[2]= weight_h264_pixels8x16_c;
  284. c->weight_h264_pixels_tab[3]= weight_h264_pixels8x8_c;
  285. c->weight_h264_pixels_tab[4]= weight_h264_pixels8x4_c;
  286. c->weight_h264_pixels_tab[5]= weight_h264_pixels4x8_c;
  287. c->weight_h264_pixels_tab[6]= weight_h264_pixels4x4_c;
  288. c->weight_h264_pixels_tab[7]= weight_h264_pixels4x2_c;
  289. c->weight_h264_pixels_tab[8]= weight_h264_pixels2x4_c;
  290. c->weight_h264_pixels_tab[9]= weight_h264_pixels2x2_c;
  291. c->biweight_h264_pixels_tab[0]= biweight_h264_pixels16x16_c;
  292. c->biweight_h264_pixels_tab[1]= biweight_h264_pixels16x8_c;
  293. c->biweight_h264_pixels_tab[2]= biweight_h264_pixels8x16_c;
  294. c->biweight_h264_pixels_tab[3]= biweight_h264_pixels8x8_c;
  295. c->biweight_h264_pixels_tab[4]= biweight_h264_pixels8x4_c;
  296. c->biweight_h264_pixels_tab[5]= biweight_h264_pixels4x8_c;
  297. c->biweight_h264_pixels_tab[6]= biweight_h264_pixels4x4_c;
  298. c->biweight_h264_pixels_tab[7]= biweight_h264_pixels4x2_c;
  299. c->biweight_h264_pixels_tab[8]= biweight_h264_pixels2x4_c;
  300. c->biweight_h264_pixels_tab[9]= biweight_h264_pixels2x2_c;
  301. c->h264_v_loop_filter_luma= h264_v_loop_filter_luma_c;
  302. c->h264_h_loop_filter_luma= h264_h_loop_filter_luma_c;
  303. c->h264_h_loop_filter_luma_mbaff= h264_h_loop_filter_luma_mbaff_c;
  304. c->h264_v_loop_filter_luma_intra= h264_v_loop_filter_luma_intra_c;
  305. c->h264_h_loop_filter_luma_intra= h264_h_loop_filter_luma_intra_c;
  306. c->h264_h_loop_filter_luma_mbaff_intra= h264_h_loop_filter_luma_mbaff_intra_c;
  307. c->h264_v_loop_filter_chroma= h264_v_loop_filter_chroma_c;
  308. c->h264_h_loop_filter_chroma= h264_h_loop_filter_chroma_c;
  309. c->h264_h_loop_filter_chroma_mbaff= h264_h_loop_filter_chroma_mbaff_c;
  310. c->h264_v_loop_filter_chroma_intra= h264_v_loop_filter_chroma_intra_c;
  311. c->h264_h_loop_filter_chroma_intra= h264_h_loop_filter_chroma_intra_c;
  312. c->h264_h_loop_filter_chroma_mbaff_intra= h264_h_loop_filter_chroma_mbaff_intra_c;
  313. c->h264_loop_filter_strength= NULL;
  314. if (ARCH_ARM) ff_h264dsp_init_arm(c);
  315. if (HAVE_ALTIVEC) ff_h264dsp_init_ppc(c);
  316. if (HAVE_MMX) ff_h264dsp_init_x86(c);
  317. }