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.

321 lines
11KB

  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 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 / 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 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 += 4*ystride;
  102. continue;
  103. }
  104. for( d = 0; d < 4; 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, 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, alpha, beta, tc0);
  141. }
  142. static av_always_inline av_flatten void h264_loop_filter_luma_intra_c(uint8_t *pix, int xstride, int ystride, int alpha, int beta)
  143. {
  144. int d;
  145. for( d = 0; d < 16; d++ ) {
  146. const int p2 = pix[-3*xstride];
  147. const int p1 = pix[-2*xstride];
  148. const int p0 = pix[-1*xstride];
  149. const int q0 = pix[ 0*xstride];
  150. const int q1 = pix[ 1*xstride];
  151. const int q2 = pix[ 2*xstride];
  152. if( FFABS( p0 - q0 ) < alpha &&
  153. FFABS( p1 - p0 ) < beta &&
  154. FFABS( q1 - q0 ) < beta ) {
  155. if(FFABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
  156. if( FFABS( p2 - p0 ) < beta)
  157. {
  158. const int p3 = pix[-4*xstride];
  159. /* p0', p1', p2' */
  160. pix[-1*xstride] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
  161. pix[-2*xstride] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
  162. pix[-3*xstride] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
  163. } else {
  164. /* p0' */
  165. pix[-1*xstride] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  166. }
  167. if( FFABS( q2 - q0 ) < beta)
  168. {
  169. const int q3 = pix[3*xstride];
  170. /* q0', q1', q2' */
  171. pix[0*xstride] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
  172. pix[1*xstride] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
  173. pix[2*xstride] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
  174. } else {
  175. /* q0' */
  176. pix[0*xstride] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  177. }
  178. }else{
  179. /* p0', q0' */
  180. pix[-1*xstride] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  181. pix[ 0*xstride] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  182. }
  183. }
  184. pix += ystride;
  185. }
  186. }
  187. static void h264_v_loop_filter_luma_intra_c(uint8_t *pix, int stride, int alpha, int beta)
  188. {
  189. h264_loop_filter_luma_intra_c(pix, stride, 1, alpha, beta);
  190. }
  191. static void h264_h_loop_filter_luma_intra_c(uint8_t *pix, int stride, int alpha, int beta)
  192. {
  193. h264_loop_filter_luma_intra_c(pix, 1, stride, alpha, beta);
  194. }
  195. static av_always_inline av_flatten void h264_loop_filter_chroma_c(uint8_t *pix, int xstride, int ystride, int alpha, int beta, int8_t *tc0)
  196. {
  197. int i, d;
  198. for( i = 0; i < 4; i++ ) {
  199. const int tc = tc0[i];
  200. if( tc <= 0 ) {
  201. pix += 2*ystride;
  202. continue;
  203. }
  204. for( d = 0; d < 2; d++ ) {
  205. const int p0 = pix[-1*xstride];
  206. const int p1 = pix[-2*xstride];
  207. const int q0 = pix[0];
  208. const int q1 = pix[1*xstride];
  209. if( FFABS( p0 - q0 ) < alpha &&
  210. FFABS( p1 - p0 ) < beta &&
  211. FFABS( q1 - q0 ) < beta ) {
  212. int delta = av_clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
  213. pix[-xstride] = av_clip_uint8( p0 + delta ); /* p0' */
  214. pix[0] = av_clip_uint8( q0 - delta ); /* q0' */
  215. }
  216. pix += ystride;
  217. }
  218. }
  219. }
  220. static void h264_v_loop_filter_chroma_c(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0)
  221. {
  222. h264_loop_filter_chroma_c(pix, stride, 1, alpha, beta, tc0);
  223. }
  224. static void h264_h_loop_filter_chroma_c(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0)
  225. {
  226. h264_loop_filter_chroma_c(pix, 1, stride, alpha, beta, tc0);
  227. }
  228. static av_always_inline av_flatten void h264_loop_filter_chroma_intra_c(uint8_t *pix, int xstride, int ystride, int alpha, int beta)
  229. {
  230. int d;
  231. for( d = 0; d < 8; d++ ) {
  232. const int p0 = pix[-1*xstride];
  233. const int p1 = pix[-2*xstride];
  234. const int q0 = pix[0];
  235. const int q1 = pix[1*xstride];
  236. if( FFABS( p0 - q0 ) < alpha &&
  237. FFABS( p1 - p0 ) < beta &&
  238. FFABS( q1 - q0 ) < beta ) {
  239. pix[-xstride] = ( 2*p1 + p0 + q1 + 2 ) >> 2; /* p0' */
  240. pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2; /* q0' */
  241. }
  242. pix += ystride;
  243. }
  244. }
  245. static void h264_v_loop_filter_chroma_intra_c(uint8_t *pix, int stride, int alpha, int beta)
  246. {
  247. h264_loop_filter_chroma_intra_c(pix, stride, 1, alpha, beta);
  248. }
  249. static void h264_h_loop_filter_chroma_intra_c(uint8_t *pix, int stride, int alpha, int beta)
  250. {
  251. h264_loop_filter_chroma_intra_c(pix, 1, stride, alpha, beta);
  252. }
  253. void ff_h264dsp_init(H264DSPContext *c)
  254. {
  255. c->h264_idct_add= ff_h264_idct_add_c;
  256. c->h264_idct8_add= ff_h264_idct8_add_c;
  257. c->h264_idct_dc_add= ff_h264_idct_dc_add_c;
  258. c->h264_idct8_dc_add= ff_h264_idct8_dc_add_c;
  259. c->h264_idct_add16 = ff_h264_idct_add16_c;
  260. c->h264_idct8_add4 = ff_h264_idct8_add4_c;
  261. c->h264_idct_add8 = ff_h264_idct_add8_c;
  262. c->h264_idct_add16intra= ff_h264_idct_add16intra_c;
  263. c->weight_h264_pixels_tab[0]= weight_h264_pixels16x16_c;
  264. c->weight_h264_pixels_tab[1]= weight_h264_pixels16x8_c;
  265. c->weight_h264_pixels_tab[2]= weight_h264_pixels8x16_c;
  266. c->weight_h264_pixels_tab[3]= weight_h264_pixels8x8_c;
  267. c->weight_h264_pixels_tab[4]= weight_h264_pixels8x4_c;
  268. c->weight_h264_pixels_tab[5]= weight_h264_pixels4x8_c;
  269. c->weight_h264_pixels_tab[6]= weight_h264_pixels4x4_c;
  270. c->weight_h264_pixels_tab[7]= weight_h264_pixels4x2_c;
  271. c->weight_h264_pixels_tab[8]= weight_h264_pixels2x4_c;
  272. c->weight_h264_pixels_tab[9]= weight_h264_pixels2x2_c;
  273. c->biweight_h264_pixels_tab[0]= biweight_h264_pixels16x16_c;
  274. c->biweight_h264_pixels_tab[1]= biweight_h264_pixels16x8_c;
  275. c->biweight_h264_pixels_tab[2]= biweight_h264_pixels8x16_c;
  276. c->biweight_h264_pixels_tab[3]= biweight_h264_pixels8x8_c;
  277. c->biweight_h264_pixels_tab[4]= biweight_h264_pixels8x4_c;
  278. c->biweight_h264_pixels_tab[5]= biweight_h264_pixels4x8_c;
  279. c->biweight_h264_pixels_tab[6]= biweight_h264_pixels4x4_c;
  280. c->biweight_h264_pixels_tab[7]= biweight_h264_pixels4x2_c;
  281. c->biweight_h264_pixels_tab[8]= biweight_h264_pixels2x4_c;
  282. c->biweight_h264_pixels_tab[9]= biweight_h264_pixels2x2_c;
  283. c->h264_v_loop_filter_luma= h264_v_loop_filter_luma_c;
  284. c->h264_h_loop_filter_luma= h264_h_loop_filter_luma_c;
  285. c->h264_v_loop_filter_luma_intra= h264_v_loop_filter_luma_intra_c;
  286. c->h264_h_loop_filter_luma_intra= h264_h_loop_filter_luma_intra_c;
  287. c->h264_v_loop_filter_chroma= h264_v_loop_filter_chroma_c;
  288. c->h264_h_loop_filter_chroma= h264_h_loop_filter_chroma_c;
  289. c->h264_v_loop_filter_chroma_intra= h264_v_loop_filter_chroma_intra_c;
  290. c->h264_h_loop_filter_chroma_intra= h264_h_loop_filter_chroma_intra_c;
  291. c->h264_loop_filter_strength= NULL;
  292. if (ARCH_ARM) ff_h264dsp_init_arm(c);
  293. if (HAVE_ALTIVEC) ff_h264dsp_init_ppc(c);
  294. if (HAVE_MMX) ff_h264dsp_init_x86(c);
  295. }