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.

289 lines
9.8KB

  1. /*
  2. * H.26L/H.264/AVC/JVT/14496-10/... encoder/decoder
  3. * Copyright (c) 2003-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 / AVC / MPEG4 part10 DSP functions.
  24. * @author Michael Niedermayer <michaelni@gmx.at>
  25. */
  26. #include "h264_high_depth.h"
  27. #define op_scale1(x) block[x] = av_clip_pixel( (block[x]*weight + offset) >> log2_denom )
  28. #define op_scale2(x) dst[x] = av_clip_pixel( (src[x]*weights + dst[x]*weightd + offset) >> (log2_denom+1))
  29. #define H264_WEIGHT(W,H) \
  30. static void FUNCC(weight_h264_pixels ## W ## x ## H)(uint8_t *p_block, int stride, int log2_denom, int weight, int offset){ \
  31. int y; \
  32. pixel *block = (pixel*)p_block; \
  33. stride >>= sizeof(pixel)-1; \
  34. offset <<= (log2_denom + (BIT_DEPTH-8)); \
  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 FUNCC(biweight_h264_pixels ## W ## x ## H)(uint8_t *p_dst, uint8_t *p_src, int stride, int log2_denom, int weightd, int weights, int offset){ \
  59. int y; \
  60. pixel *dst = (pixel*)p_dst; \
  61. pixel *src = (pixel*)p_src; \
  62. stride >>= sizeof(pixel)-1; \
  63. offset = ((offset + 1) | 1) << log2_denom; \
  64. for(y=0; y<H; y++, dst += stride, src += stride){ \
  65. op_scale2(0); \
  66. op_scale2(1); \
  67. if(W==2) continue; \
  68. op_scale2(2); \
  69. op_scale2(3); \
  70. if(W==4) continue; \
  71. op_scale2(4); \
  72. op_scale2(5); \
  73. op_scale2(6); \
  74. op_scale2(7); \
  75. if(W==8) continue; \
  76. op_scale2(8); \
  77. op_scale2(9); \
  78. op_scale2(10); \
  79. op_scale2(11); \
  80. op_scale2(12); \
  81. op_scale2(13); \
  82. op_scale2(14); \
  83. op_scale2(15); \
  84. } \
  85. }
  86. H264_WEIGHT(16,16)
  87. H264_WEIGHT(16,8)
  88. H264_WEIGHT(8,16)
  89. H264_WEIGHT(8,8)
  90. H264_WEIGHT(8,4)
  91. H264_WEIGHT(4,8)
  92. H264_WEIGHT(4,4)
  93. H264_WEIGHT(4,2)
  94. H264_WEIGHT(2,4)
  95. H264_WEIGHT(2,2)
  96. #undef op_scale1
  97. #undef op_scale2
  98. #undef H264_WEIGHT
  99. static av_always_inline av_flatten void FUNCC(h264_loop_filter_luma)(uint8_t *p_pix, int xstride, int ystride, int alpha, int beta, int8_t *tc0)
  100. {
  101. pixel *pix = (pixel*)p_pix;
  102. int i, d;
  103. xstride >>= sizeof(pixel)-1;
  104. ystride >>= sizeof(pixel)-1;
  105. for( i = 0; i < 4; i++ ) {
  106. if( tc0[i] < 0 ) {
  107. pix += 4*ystride;
  108. continue;
  109. }
  110. for( d = 0; d < 4; d++ ) {
  111. const int p0 = pix[-1*xstride];
  112. const int p1 = pix[-2*xstride];
  113. const int p2 = pix[-3*xstride];
  114. const int q0 = pix[0];
  115. const int q1 = pix[1*xstride];
  116. const int q2 = pix[2*xstride];
  117. if( FFABS( p0 - q0 ) < alpha &&
  118. FFABS( p1 - p0 ) < beta &&
  119. FFABS( q1 - q0 ) < beta ) {
  120. int tc = tc0[i];
  121. int i_delta;
  122. if( FFABS( p2 - p0 ) < beta ) {
  123. if(tc0[i])
  124. pix[-2*xstride] = p1 + av_clip( (( p2 + ( ( p0 + q0 + 1 ) >> 1 ) ) >> 1) - p1, -tc0[i], tc0[i] );
  125. tc++;
  126. }
  127. if( FFABS( q2 - q0 ) < beta ) {
  128. if(tc0[i])
  129. pix[ xstride] = q1 + av_clip( (( q2 + ( ( p0 + q0 + 1 ) >> 1 ) ) >> 1) - q1, -tc0[i], tc0[i] );
  130. tc++;
  131. }
  132. i_delta = av_clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
  133. pix[-xstride] = av_clip_pixel( p0 + i_delta ); /* p0' */
  134. pix[0] = av_clip_pixel( q0 - i_delta ); /* q0' */
  135. }
  136. pix += ystride;
  137. }
  138. }
  139. }
  140. static void FUNCC(h264_v_loop_filter_luma)(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0)
  141. {
  142. FUNCC(h264_loop_filter_luma)(pix, stride, sizeof(pixel), alpha, beta, tc0);
  143. }
  144. static void FUNCC(h264_h_loop_filter_luma)(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0)
  145. {
  146. FUNCC(h264_loop_filter_luma)(pix, sizeof(pixel), stride, alpha, beta, tc0);
  147. }
  148. static av_always_inline av_flatten void FUNCC(h264_loop_filter_luma_intra)(uint8_t *p_pix, int xstride, int ystride, int alpha, int beta)
  149. {
  150. pixel *pix = (pixel*)p_pix;
  151. int d;
  152. xstride >>= sizeof(pixel)-1;
  153. ystride >>= sizeof(pixel)-1;
  154. for( d = 0; d < 16; d++ ) {
  155. const int p2 = pix[-3*xstride];
  156. const int p1 = pix[-2*xstride];
  157. const int p0 = pix[-1*xstride];
  158. const int q0 = pix[ 0*xstride];
  159. const int q1 = pix[ 1*xstride];
  160. const int q2 = pix[ 2*xstride];
  161. if( FFABS( p0 - q0 ) < alpha &&
  162. FFABS( p1 - p0 ) < beta &&
  163. FFABS( q1 - q0 ) < beta ) {
  164. if(FFABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
  165. if( FFABS( p2 - p0 ) < beta)
  166. {
  167. const int p3 = pix[-4*xstride];
  168. /* p0', p1', p2' */
  169. pix[-1*xstride] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
  170. pix[-2*xstride] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
  171. pix[-3*xstride] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
  172. } else {
  173. /* p0' */
  174. pix[-1*xstride] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  175. }
  176. if( FFABS( q2 - q0 ) < beta)
  177. {
  178. const int q3 = pix[3*xstride];
  179. /* q0', q1', q2' */
  180. pix[0*xstride] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
  181. pix[1*xstride] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
  182. pix[2*xstride] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
  183. } else {
  184. /* q0' */
  185. pix[0*xstride] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  186. }
  187. }else{
  188. /* p0', q0' */
  189. pix[-1*xstride] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
  190. pix[ 0*xstride] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
  191. }
  192. }
  193. pix += ystride;
  194. }
  195. }
  196. static void FUNCC(h264_v_loop_filter_luma_intra)(uint8_t *pix, int stride, int alpha, int beta)
  197. {
  198. FUNCC(h264_loop_filter_luma_intra)(pix, stride, sizeof(pixel), alpha, beta);
  199. }
  200. static void FUNCC(h264_h_loop_filter_luma_intra)(uint8_t *pix, int stride, int alpha, int beta)
  201. {
  202. FUNCC(h264_loop_filter_luma_intra)(pix, sizeof(pixel), stride, alpha, beta);
  203. }
  204. static av_always_inline av_flatten void FUNCC(h264_loop_filter_chroma)(uint8_t *p_pix, int xstride, int ystride, int alpha, int beta, int8_t *tc0)
  205. {
  206. pixel *pix = (pixel*)p_pix;
  207. int i, d;
  208. xstride >>= sizeof(pixel)-1;
  209. ystride >>= sizeof(pixel)-1;
  210. for( i = 0; i < 4; i++ ) {
  211. const int tc = tc0[i];
  212. if( tc <= 0 ) {
  213. pix += 2*ystride;
  214. continue;
  215. }
  216. for( d = 0; d < 2; d++ ) {
  217. const int p0 = pix[-1*xstride];
  218. const int p1 = pix[-2*xstride];
  219. const int q0 = pix[0];
  220. const int q1 = pix[1*xstride];
  221. if( FFABS( p0 - q0 ) < alpha &&
  222. FFABS( p1 - p0 ) < beta &&
  223. FFABS( q1 - q0 ) < beta ) {
  224. int delta = av_clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
  225. pix[-xstride] = av_clip_pixel( p0 + delta ); /* p0' */
  226. pix[0] = av_clip_pixel( q0 - delta ); /* q0' */
  227. }
  228. pix += ystride;
  229. }
  230. }
  231. }
  232. static void FUNCC(h264_v_loop_filter_chroma)(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0)
  233. {
  234. FUNCC(h264_loop_filter_chroma)(pix, stride, sizeof(pixel), alpha, beta, tc0);
  235. }
  236. static void FUNCC(h264_h_loop_filter_chroma)(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0)
  237. {
  238. FUNCC(h264_loop_filter_chroma)(pix, sizeof(pixel), stride, alpha, beta, tc0);
  239. }
  240. static av_always_inline av_flatten void FUNCC(h264_loop_filter_chroma_intra)(uint8_t *p_pix, int xstride, int ystride, int alpha, int beta)
  241. {
  242. pixel *pix = (pixel*)p_pix;
  243. int d;
  244. xstride >>= sizeof(pixel)-1;
  245. ystride >>= sizeof(pixel)-1;
  246. for( d = 0; d < 8; d++ ) {
  247. const int p0 = pix[-1*xstride];
  248. const int p1 = pix[-2*xstride];
  249. const int q0 = pix[0];
  250. const int q1 = pix[1*xstride];
  251. if( FFABS( p0 - q0 ) < alpha &&
  252. FFABS( p1 - p0 ) < beta &&
  253. FFABS( q1 - q0 ) < beta ) {
  254. pix[-xstride] = ( 2*p1 + p0 + q1 + 2 ) >> 2; /* p0' */
  255. pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2; /* q0' */
  256. }
  257. pix += ystride;
  258. }
  259. }
  260. static void FUNCC(h264_v_loop_filter_chroma_intra)(uint8_t *pix, int stride, int alpha, int beta)
  261. {
  262. FUNCC(h264_loop_filter_chroma_intra)(pix, stride, sizeof(pixel), alpha, beta);
  263. }
  264. static void FUNCC(h264_h_loop_filter_chroma_intra)(uint8_t *pix, int stride, int alpha, int beta)
  265. {
  266. FUNCC(h264_loop_filter_chroma_intra)(pix, sizeof(pixel), stride, alpha, beta);
  267. }