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.

653 lines
30KB

  1. /*
  2. * H.26L/H.264/AVC/JVT/14496-10/... loop filter
  3. * Copyright (c) 2003 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 loop filter.
  24. * @author Michael Niedermayer <michaelni@gmx.at>
  25. */
  26. #include "libavutil/intreadwrite.h"
  27. #include "internal.h"
  28. #include "dsputil.h"
  29. #include "avcodec.h"
  30. #include "mpegvideo.h"
  31. #include "h264.h"
  32. #include "mathops.h"
  33. #include "rectangle.h"
  34. //#undef NDEBUG
  35. #include <assert.h>
  36. /* Deblocking filter (p153) */
  37. static const uint8_t alpha_table[52*3] = {
  38. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  39. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  40. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  41. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  42. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  43. 0, 0, 0, 0, 0, 0, 4, 4, 5, 6,
  44. 7, 8, 9, 10, 12, 13, 15, 17, 20, 22,
  45. 25, 28, 32, 36, 40, 45, 50, 56, 63, 71,
  46. 80, 90,101,113,127,144,162,182,203,226,
  47. 255,255,
  48. 255,255,255,255,255,255,255,255,255,255,255,255,255,
  49. 255,255,255,255,255,255,255,255,255,255,255,255,255,
  50. 255,255,255,255,255,255,255,255,255,255,255,255,255,
  51. 255,255,255,255,255,255,255,255,255,255,255,255,255,
  52. };
  53. static const uint8_t beta_table[52*3] = {
  54. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  55. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  56. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  57. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  58. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  59. 0, 0, 0, 0, 0, 0, 2, 2, 2, 3,
  60. 3, 3, 3, 4, 4, 4, 6, 6, 7, 7,
  61. 8, 8, 9, 9, 10, 10, 11, 11, 12, 12,
  62. 13, 13, 14, 14, 15, 15, 16, 16, 17, 17,
  63. 18, 18,
  64. 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
  65. 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
  66. 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
  67. 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
  68. };
  69. static const uint8_t tc0_table[52*3][4] = {
  70. {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 },
  71. {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 },
  72. {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 },
  73. {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 },
  74. {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 },
  75. {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 },
  76. {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 },
  77. {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 },
  78. {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 },
  79. {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 },
  80. {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 },
  81. {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 0 }, {-1, 0, 0, 1 },
  82. {-1, 0, 0, 1 }, {-1, 0, 0, 1 }, {-1, 0, 0, 1 }, {-1, 0, 1, 1 }, {-1, 0, 1, 1 }, {-1, 1, 1, 1 },
  83. {-1, 1, 1, 1 }, {-1, 1, 1, 1 }, {-1, 1, 1, 1 }, {-1, 1, 1, 2 }, {-1, 1, 1, 2 }, {-1, 1, 1, 2 },
  84. {-1, 1, 1, 2 }, {-1, 1, 2, 3 }, {-1, 1, 2, 3 }, {-1, 2, 2, 3 }, {-1, 2, 2, 4 }, {-1, 2, 3, 4 },
  85. {-1, 2, 3, 4 }, {-1, 3, 3, 5 }, {-1, 3, 4, 6 }, {-1, 3, 4, 6 }, {-1, 4, 5, 7 }, {-1, 4, 5, 8 },
  86. {-1, 4, 6, 9 }, {-1, 5, 7,10 }, {-1, 6, 8,11 }, {-1, 6, 8,13 }, {-1, 7,10,14 }, {-1, 8,11,16 },
  87. {-1, 9,12,18 }, {-1,10,13,20 }, {-1,11,15,23 }, {-1,13,17,25 },
  88. {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 },
  89. {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 },
  90. {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 },
  91. {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 },
  92. {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 },
  93. {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 },
  94. {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 },
  95. {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 },
  96. {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 }, {-1,13,17,25 },
  97. };
  98. static void av_always_inline filter_mb_edgev( uint8_t *pix, int stride, int16_t bS[4], unsigned int qp, H264Context *h) {
  99. const unsigned int index_a = qp + h->slice_alpha_c0_offset;
  100. const int alpha = alpha_table[index_a];
  101. const int beta = beta_table[qp + h->slice_beta_offset];
  102. if (alpha ==0 || beta == 0) return;
  103. if( bS[0] < 4 ) {
  104. int8_t tc[4];
  105. tc[0] = tc0_table[index_a][bS[0]];
  106. tc[1] = tc0_table[index_a][bS[1]];
  107. tc[2] = tc0_table[index_a][bS[2]];
  108. tc[3] = tc0_table[index_a][bS[3]];
  109. h->h264dsp.h264_h_loop_filter_luma(pix, stride, alpha, beta, tc);
  110. } else {
  111. h->h264dsp.h264_h_loop_filter_luma_intra(pix, stride, alpha, beta);
  112. }
  113. }
  114. static void av_always_inline filter_mb_edgecv( uint8_t *pix, int stride, int16_t bS[4], unsigned int qp, H264Context *h ) {
  115. const unsigned int index_a = qp + h->slice_alpha_c0_offset;
  116. const int alpha = alpha_table[index_a];
  117. const int beta = beta_table[qp + h->slice_beta_offset];
  118. if (alpha ==0 || beta == 0) return;
  119. if( bS[0] < 4 ) {
  120. int8_t tc[4];
  121. tc[0] = tc0_table[index_a][bS[0]]+1;
  122. tc[1] = tc0_table[index_a][bS[1]]+1;
  123. tc[2] = tc0_table[index_a][bS[2]]+1;
  124. tc[3] = tc0_table[index_a][bS[3]]+1;
  125. h->h264dsp.h264_h_loop_filter_chroma(pix, stride, alpha, beta, tc);
  126. } else {
  127. h->h264dsp.h264_h_loop_filter_chroma_intra(pix, stride, alpha, beta);
  128. }
  129. }
  130. static void filter_mb_mbaff_edgev( H264Context *h, uint8_t *pix, int stride, int16_t bS[7], int bsi, int qp ) {
  131. int index_a = qp + h->slice_alpha_c0_offset;
  132. int alpha = alpha_table[index_a];
  133. int beta = beta_table[qp + h->slice_beta_offset];
  134. if (alpha ==0 || beta == 0) return;
  135. if( bS[0] < 4 ) {
  136. int8_t tc[4];
  137. tc[0] = tc0_table[index_a][bS[0*bsi]];
  138. tc[1] = tc0_table[index_a][bS[1*bsi]];
  139. tc[2] = tc0_table[index_a][bS[2*bsi]];
  140. tc[3] = tc0_table[index_a][bS[3*bsi]];
  141. h->h264dsp.h264_h_loop_filter_luma_mbaff(pix, stride, alpha, beta, tc);
  142. } else {
  143. h->h264dsp.h264_h_loop_filter_luma_mbaff_intra(pix, stride, alpha, beta);
  144. }
  145. }
  146. static void filter_mb_mbaff_edgecv( H264Context *h, uint8_t *pix, int stride, int16_t bS[7], int bsi, int qp ) {
  147. int index_a = qp + h->slice_alpha_c0_offset;
  148. int alpha = alpha_table[index_a];
  149. int beta = beta_table[qp + h->slice_beta_offset];
  150. if (alpha ==0 || beta == 0) return;
  151. if( bS[0] < 4 ) {
  152. int8_t tc[4];
  153. tc[0] = tc0_table[index_a][bS[0*bsi]] + 1;
  154. tc[1] = tc0_table[index_a][bS[1*bsi]] + 1;
  155. tc[2] = tc0_table[index_a][bS[2*bsi]] + 1;
  156. tc[3] = tc0_table[index_a][bS[3*bsi]] + 1;
  157. h->h264dsp.h264_h_loop_filter_chroma_mbaff(pix, stride, alpha, beta, tc);
  158. } else {
  159. h->h264dsp.h264_h_loop_filter_chroma_mbaff_intra(pix, stride, alpha, beta);
  160. }
  161. }
  162. static void av_always_inline filter_mb_edgeh( uint8_t *pix, int stride, int16_t bS[4], unsigned int qp, H264Context *h ) {
  163. const unsigned int index_a = qp + h->slice_alpha_c0_offset;
  164. const int alpha = alpha_table[index_a];
  165. const int beta = beta_table[qp + h->slice_beta_offset];
  166. if (alpha ==0 || beta == 0) return;
  167. if( bS[0] < 4 ) {
  168. int8_t tc[4];
  169. tc[0] = tc0_table[index_a][bS[0]];
  170. tc[1] = tc0_table[index_a][bS[1]];
  171. tc[2] = tc0_table[index_a][bS[2]];
  172. tc[3] = tc0_table[index_a][bS[3]];
  173. h->h264dsp.h264_v_loop_filter_luma(pix, stride, alpha, beta, tc);
  174. } else {
  175. h->h264dsp.h264_v_loop_filter_luma_intra(pix, stride, alpha, beta);
  176. }
  177. }
  178. static void av_always_inline filter_mb_edgech( uint8_t *pix, int stride, int16_t bS[4], unsigned int qp, H264Context *h ) {
  179. const unsigned int index_a = qp + h->slice_alpha_c0_offset;
  180. const int alpha = alpha_table[index_a];
  181. const int beta = beta_table[qp + h->slice_beta_offset];
  182. if (alpha ==0 || beta == 0) return;
  183. if( bS[0] < 4 ) {
  184. int8_t tc[4];
  185. tc[0] = tc0_table[index_a][bS[0]]+1;
  186. tc[1] = tc0_table[index_a][bS[1]]+1;
  187. tc[2] = tc0_table[index_a][bS[2]]+1;
  188. tc[3] = tc0_table[index_a][bS[3]]+1;
  189. h->h264dsp.h264_v_loop_filter_chroma(pix, stride, alpha, beta, tc);
  190. } else {
  191. h->h264dsp.h264_v_loop_filter_chroma_intra(pix, stride, alpha, beta);
  192. }
  193. }
  194. void ff_h264_filter_mb_fast( H264Context *h, int mb_x, int mb_y, uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr, unsigned int linesize, unsigned int uvlinesize) {
  195. MpegEncContext * const s = &h->s;
  196. int mb_xy;
  197. int mb_type, left_type;
  198. int qp, qp0, qp1, qpc, qpc0, qpc1, qp_thresh;
  199. mb_xy = h->mb_xy;
  200. if(!h->top_type || !h->h264dsp.h264_loop_filter_strength || h->pps.chroma_qp_diff) {
  201. ff_h264_filter_mb(h, mb_x, mb_y, img_y, img_cb, img_cr, linesize, uvlinesize);
  202. return;
  203. }
  204. assert(!FRAME_MBAFF);
  205. left_type= h->left_type[0];
  206. mb_type = s->current_picture.mb_type[mb_xy];
  207. qp = s->current_picture.qscale_table[mb_xy];
  208. qp0 = s->current_picture.qscale_table[mb_xy-1];
  209. qp1 = s->current_picture.qscale_table[h->top_mb_xy];
  210. qpc = get_chroma_qp( h, 0, qp );
  211. qpc0 = get_chroma_qp( h, 0, qp0 );
  212. qpc1 = get_chroma_qp( h, 0, qp1 );
  213. qp0 = (qp + qp0 + 1) >> 1;
  214. qp1 = (qp + qp1 + 1) >> 1;
  215. qpc0 = (qpc + qpc0 + 1) >> 1;
  216. qpc1 = (qpc + qpc1 + 1) >> 1;
  217. qp_thresh = 15+52 - h->slice_alpha_c0_offset;
  218. if(qp <= qp_thresh && qp0 <= qp_thresh && qp1 <= qp_thresh &&
  219. qpc <= qp_thresh && qpc0 <= qp_thresh && qpc1 <= qp_thresh)
  220. return;
  221. if( IS_INTRA(mb_type) ) {
  222. int16_t bS4[4] = {4,4,4,4};
  223. int16_t bS3[4] = {3,3,3,3};
  224. int16_t *bSH = FIELD_PICTURE ? bS3 : bS4;
  225. if(left_type)
  226. filter_mb_edgev( &img_y[4*0], linesize, bS4, qp0, h);
  227. if( IS_8x8DCT(mb_type) ) {
  228. filter_mb_edgev( &img_y[4*2], linesize, bS3, qp, h);
  229. filter_mb_edgeh( &img_y[4*0*linesize], linesize, bSH, qp1, h);
  230. filter_mb_edgeh( &img_y[4*2*linesize], linesize, bS3, qp, h);
  231. } else {
  232. filter_mb_edgev( &img_y[4*1], linesize, bS3, qp, h);
  233. filter_mb_edgev( &img_y[4*2], linesize, bS3, qp, h);
  234. filter_mb_edgev( &img_y[4*3], linesize, bS3, qp, h);
  235. filter_mb_edgeh( &img_y[4*0*linesize], linesize, bSH, qp1, h);
  236. filter_mb_edgeh( &img_y[4*1*linesize], linesize, bS3, qp, h);
  237. filter_mb_edgeh( &img_y[4*2*linesize], linesize, bS3, qp, h);
  238. filter_mb_edgeh( &img_y[4*3*linesize], linesize, bS3, qp, h);
  239. }
  240. if(left_type){
  241. filter_mb_edgecv( &img_cb[2*0], uvlinesize, bS4, qpc0, h);
  242. filter_mb_edgecv( &img_cr[2*0], uvlinesize, bS4, qpc0, h);
  243. }
  244. filter_mb_edgecv( &img_cb[2*2], uvlinesize, bS3, qpc, h);
  245. filter_mb_edgecv( &img_cr[2*2], uvlinesize, bS3, qpc, h);
  246. filter_mb_edgech( &img_cb[2*0*uvlinesize], uvlinesize, bSH, qpc1, h);
  247. filter_mb_edgech( &img_cb[2*2*uvlinesize], uvlinesize, bS3, qpc, h);
  248. filter_mb_edgech( &img_cr[2*0*uvlinesize], uvlinesize, bSH, qpc1, h);
  249. filter_mb_edgech( &img_cr[2*2*uvlinesize], uvlinesize, bS3, qpc, h);
  250. return;
  251. } else {
  252. LOCAL_ALIGNED_8(int16_t, bS, [2], [4][4]);
  253. int edges;
  254. if( IS_8x8DCT(mb_type) && (h->cbp&7) == 7 ) {
  255. edges = 4;
  256. AV_WN64A(bS[0][0], 0x0002000200020002ULL);
  257. AV_WN64A(bS[0][2], 0x0002000200020002ULL);
  258. AV_WN64A(bS[1][0], 0x0002000200020002ULL);
  259. AV_WN64A(bS[1][2], 0x0002000200020002ULL);
  260. } else {
  261. int mask_edge1 = (3*(((5*mb_type)>>5)&1)) | (mb_type>>4); //(mb_type & (MB_TYPE_16x16 | MB_TYPE_8x16)) ? 3 : (mb_type & MB_TYPE_16x8) ? 1 : 0;
  262. int mask_edge0 = 3*((mask_edge1>>1) & ((5*left_type)>>5)&1); // (mb_type & (MB_TYPE_16x16 | MB_TYPE_8x16)) && (h->left_type[0] & (MB_TYPE_16x16 | MB_TYPE_8x16)) ? 3 : 0;
  263. int step = 1+(mb_type>>24); //IS_8x8DCT(mb_type) ? 2 : 1;
  264. edges = 4 - 3*((mb_type>>3) & !(h->cbp & 15)); //(mb_type & MB_TYPE_16x16) && !(h->cbp & 15) ? 1 : 4;
  265. h->h264dsp.h264_loop_filter_strength( bS, h->non_zero_count_cache, h->ref_cache, h->mv_cache,
  266. h->list_count==2, edges, step, mask_edge0, mask_edge1, FIELD_PICTURE);
  267. }
  268. if( IS_INTRA(left_type) )
  269. AV_WN64A(bS[0][0], 0x0004000400040004ULL);
  270. if( IS_INTRA(h->top_type) )
  271. AV_WN64A(bS[1][0], FIELD_PICTURE ? 0x0003000300030003ULL : 0x0004000400040004ULL);
  272. #define FILTER(hv,dir,edge)\
  273. if(AV_RN64A(bS[dir][edge])) { \
  274. filter_mb_edge##hv( &img_y[4*edge*(dir?linesize:1)], linesize, bS[dir][edge], edge ? qp : qp##dir, h );\
  275. if(!(edge&1)) {\
  276. filter_mb_edgec##hv( &img_cb[2*edge*(dir?uvlinesize:1)], uvlinesize, bS[dir][edge], edge ? qpc : qpc##dir, h );\
  277. filter_mb_edgec##hv( &img_cr[2*edge*(dir?uvlinesize:1)], uvlinesize, bS[dir][edge], edge ? qpc : qpc##dir, h );\
  278. }\
  279. }
  280. if(left_type)
  281. FILTER(v,0,0);
  282. if( edges == 1 ) {
  283. FILTER(h,1,0);
  284. } else if( IS_8x8DCT(mb_type) ) {
  285. FILTER(v,0,2);
  286. FILTER(h,1,0);
  287. FILTER(h,1,2);
  288. } else {
  289. FILTER(v,0,1);
  290. FILTER(v,0,2);
  291. FILTER(v,0,3);
  292. FILTER(h,1,0);
  293. FILTER(h,1,1);
  294. FILTER(h,1,2);
  295. FILTER(h,1,3);
  296. }
  297. #undef FILTER
  298. }
  299. }
  300. static int check_mv(H264Context *h, long b_idx, long bn_idx, int mvy_limit){
  301. int v;
  302. v= h->ref_cache[0][b_idx] != h->ref_cache[0][bn_idx];
  303. if(!v && h->ref_cache[0][b_idx]!=-1)
  304. v= h->mv_cache[0][b_idx][0] - h->mv_cache[0][bn_idx][0] + 3 >= 7U |
  305. FFABS( h->mv_cache[0][b_idx][1] - h->mv_cache[0][bn_idx][1] ) >= mvy_limit;
  306. if(h->list_count==2){
  307. if(!v)
  308. v = h->ref_cache[1][b_idx] != h->ref_cache[1][bn_idx] |
  309. h->mv_cache[1][b_idx][0] - h->mv_cache[1][bn_idx][0] + 3 >= 7U |
  310. FFABS( h->mv_cache[1][b_idx][1] - h->mv_cache[1][bn_idx][1] ) >= mvy_limit;
  311. if(v){
  312. if(h->ref_cache[0][b_idx] != h->ref_cache[1][bn_idx] |
  313. h->ref_cache[1][b_idx] != h->ref_cache[0][bn_idx])
  314. return 1;
  315. return
  316. h->mv_cache[0][b_idx][0] - h->mv_cache[1][bn_idx][0] + 3 >= 7U |
  317. FFABS( h->mv_cache[0][b_idx][1] - h->mv_cache[1][bn_idx][1] ) >= mvy_limit |
  318. h->mv_cache[1][b_idx][0] - h->mv_cache[0][bn_idx][0] + 3 >= 7U |
  319. FFABS( h->mv_cache[1][b_idx][1] - h->mv_cache[0][bn_idx][1] ) >= mvy_limit;
  320. }
  321. }
  322. return v;
  323. }
  324. static av_always_inline void filter_mb_dir(H264Context *h, int mb_x, int mb_y, uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr, unsigned int linesize, unsigned int uvlinesize, int mb_xy, int mb_type, int mvy_limit, int first_vertical_edge_done, int dir) {
  325. MpegEncContext * const s = &h->s;
  326. int edge;
  327. const int mbm_xy = dir == 0 ? mb_xy -1 : h->top_mb_xy;
  328. const int mbm_type = dir == 0 ? h->left_type[0] : h->top_type;
  329. // how often to recheck mv-based bS when iterating between edges
  330. static const uint8_t mask_edge_tab[2][8]={{0,3,3,3,1,1,1,1},
  331. {0,3,1,1,3,3,3,3}};
  332. const int mask_edge = mask_edge_tab[dir][(mb_type>>3)&7];
  333. const int edges = mask_edge== 3 && !(h->cbp&15) ? 1 : 4;
  334. // how often to recheck mv-based bS when iterating along each edge
  335. const int mask_par0 = mb_type & (MB_TYPE_16x16 | (MB_TYPE_8x16 >> dir));
  336. if(mbm_type && !first_vertical_edge_done){
  337. if (FRAME_MBAFF && (dir == 1) && ((mb_y&1) == 0)
  338. && IS_INTERLACED(mbm_type&~mb_type)
  339. ) {
  340. // This is a special case in the norm where the filtering must
  341. // be done twice (one each of the field) even if we are in a
  342. // frame macroblock.
  343. //
  344. unsigned int tmp_linesize = 2 * linesize;
  345. unsigned int tmp_uvlinesize = 2 * uvlinesize;
  346. int mbn_xy = mb_xy - 2 * s->mb_stride;
  347. int j;
  348. for(j=0; j<2; j++, mbn_xy += s->mb_stride){
  349. DECLARE_ALIGNED(8, int16_t, bS)[4];
  350. int qp;
  351. if( IS_INTRA(mb_type|s->current_picture.mb_type[mbn_xy]) ) {
  352. AV_WN64A(bS, 0x0003000300030003ULL);
  353. } else {
  354. if(!CABAC && IS_8x8DCT(s->current_picture.mb_type[mbn_xy])){
  355. bS[0]= 1+((h->cbp_table[mbn_xy] & 4)||h->non_zero_count_cache[scan8[0]+0]);
  356. bS[1]= 1+((h->cbp_table[mbn_xy] & 4)||h->non_zero_count_cache[scan8[0]+1]);
  357. bS[2]= 1+((h->cbp_table[mbn_xy] & 8)||h->non_zero_count_cache[scan8[0]+2]);
  358. bS[3]= 1+((h->cbp_table[mbn_xy] & 8)||h->non_zero_count_cache[scan8[0]+3]);
  359. }else{
  360. const uint8_t *mbn_nnz = h->non_zero_count[mbn_xy] + 4+3*8;
  361. int i;
  362. for( i = 0; i < 4; i++ ) {
  363. bS[i] = 1 + !!(h->non_zero_count_cache[scan8[0]+i] | mbn_nnz[i]);
  364. }
  365. }
  366. }
  367. // Do not use s->qscale as luma quantizer because it has not the same
  368. // value in IPCM macroblocks.
  369. qp = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[mbn_xy] + 1 ) >> 1;
  370. tprintf(s->avctx, "filter mb:%d/%d dir:%d edge:%d, QPy:%d ls:%d uvls:%d", mb_x, mb_y, dir, edge, qp, tmp_linesize, tmp_uvlinesize);
  371. { int i; for (i = 0; i < 4; i++) tprintf(s->avctx, " bS[%d]:%d", i, bS[i]); tprintf(s->avctx, "\n"); }
  372. filter_mb_edgeh( &img_y[j*linesize], tmp_linesize, bS, qp, h );
  373. filter_mb_edgech( &img_cb[j*uvlinesize], tmp_uvlinesize, bS,
  374. ( h->chroma_qp[0] + get_chroma_qp( h, 0, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1, h);
  375. filter_mb_edgech( &img_cr[j*uvlinesize], tmp_uvlinesize, bS,
  376. ( h->chroma_qp[1] + get_chroma_qp( h, 1, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1, h);
  377. }
  378. }else{
  379. DECLARE_ALIGNED(8, int16_t, bS)[4];
  380. int qp;
  381. if( IS_INTRA(mb_type|mbm_type)) {
  382. AV_WN64A(bS, 0x0003000300030003ULL);
  383. if ( (!IS_INTERLACED(mb_type|mbm_type))
  384. || ((FRAME_MBAFF || (s->picture_structure != PICT_FRAME)) && (dir == 0))
  385. )
  386. AV_WN64A(bS, 0x0004000400040004ULL);
  387. } else {
  388. int i;
  389. int mv_done;
  390. if( dir && FRAME_MBAFF && IS_INTERLACED(mb_type ^ mbm_type)) {
  391. AV_WN64A(bS, 0x0001000100010001ULL);
  392. mv_done = 1;
  393. }
  394. else if( mask_par0 && ((mbm_type & (MB_TYPE_16x16 | (MB_TYPE_8x16 >> dir)))) ) {
  395. int b_idx= 8 + 4;
  396. int bn_idx= b_idx - (dir ? 8:1);
  397. bS[0] = bS[1] = bS[2] = bS[3] = check_mv(h, 8 + 4, bn_idx, mvy_limit);
  398. mv_done = 1;
  399. }
  400. else
  401. mv_done = 0;
  402. for( i = 0; i < 4; i++ ) {
  403. int x = dir == 0 ? 0 : i;
  404. int y = dir == 0 ? i : 0;
  405. int b_idx= 8 + 4 + x + 8*y;
  406. int bn_idx= b_idx - (dir ? 8:1);
  407. if( h->non_zero_count_cache[b_idx] |
  408. h->non_zero_count_cache[bn_idx] ) {
  409. bS[i] = 2;
  410. }
  411. else if(!mv_done)
  412. {
  413. bS[i] = check_mv(h, b_idx, bn_idx, mvy_limit);
  414. }
  415. }
  416. }
  417. /* Filter edge */
  418. // Do not use s->qscale as luma quantizer because it has not the same
  419. // value in IPCM macroblocks.
  420. if(bS[0]+bS[1]+bS[2]+bS[3]){
  421. qp = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[mbm_xy] + 1 ) >> 1;
  422. //tprintf(s->avctx, "filter mb:%d/%d dir:%d edge:%d, QPy:%d, QPc:%d, QPcn:%d\n", mb_x, mb_y, dir, edge, qp, h->chroma_qp[0], s->current_picture.qscale_table[mbn_xy]);
  423. tprintf(s->avctx, "filter mb:%d/%d dir:%d edge:%d, QPy:%d ls:%d uvls:%d", mb_x, mb_y, dir, edge, qp, linesize, uvlinesize);
  424. //{ int i; for (i = 0; i < 4; i++) tprintf(s->avctx, " bS[%d]:%d", i, bS[i]); tprintf(s->avctx, "\n"); }
  425. if( dir == 0 ) {
  426. filter_mb_edgev( &img_y[0], linesize, bS, qp, h );
  427. {
  428. int qp= ( h->chroma_qp[0] + get_chroma_qp( h, 0, s->current_picture.qscale_table[mbm_xy] ) + 1 ) >> 1;
  429. filter_mb_edgecv( &img_cb[0], uvlinesize, bS, qp, h);
  430. if(h->pps.chroma_qp_diff)
  431. qp= ( h->chroma_qp[1] + get_chroma_qp( h, 1, s->current_picture.qscale_table[mbm_xy] ) + 1 ) >> 1;
  432. filter_mb_edgecv( &img_cr[0], uvlinesize, bS, qp, h);
  433. }
  434. } else {
  435. filter_mb_edgeh( &img_y[0], linesize, bS, qp, h );
  436. {
  437. int qp= ( h->chroma_qp[0] + get_chroma_qp( h, 0, s->current_picture.qscale_table[mbm_xy] ) + 1 ) >> 1;
  438. filter_mb_edgech( &img_cb[0], uvlinesize, bS, qp, h);
  439. if(h->pps.chroma_qp_diff)
  440. qp= ( h->chroma_qp[1] + get_chroma_qp( h, 1, s->current_picture.qscale_table[mbm_xy] ) + 1 ) >> 1;
  441. filter_mb_edgech( &img_cr[0], uvlinesize, bS, qp, h);
  442. }
  443. }
  444. }
  445. }
  446. }
  447. /* Calculate bS */
  448. for( edge = 1; edge < edges; edge++ ) {
  449. DECLARE_ALIGNED(8, int16_t, bS)[4];
  450. int qp;
  451. if( IS_8x8DCT(mb_type & (edge<<24)) ) // (edge&1) && IS_8x8DCT(mb_type)
  452. continue;
  453. if( IS_INTRA(mb_type)) {
  454. AV_WN64A(bS, 0x0003000300030003ULL);
  455. } else {
  456. int i;
  457. int mv_done;
  458. if( edge & mask_edge ) {
  459. AV_ZERO64(bS);
  460. mv_done = 1;
  461. }
  462. else if( mask_par0 ) {
  463. int b_idx= 8 + 4 + edge * (dir ? 8:1);
  464. int bn_idx= b_idx - (dir ? 8:1);
  465. bS[0] = bS[1] = bS[2] = bS[3] = check_mv(h, b_idx, bn_idx, mvy_limit);
  466. mv_done = 1;
  467. }
  468. else
  469. mv_done = 0;
  470. for( i = 0; i < 4; i++ ) {
  471. int x = dir == 0 ? edge : i;
  472. int y = dir == 0 ? i : edge;
  473. int b_idx= 8 + 4 + x + 8*y;
  474. int bn_idx= b_idx - (dir ? 8:1);
  475. if( h->non_zero_count_cache[b_idx] |
  476. h->non_zero_count_cache[bn_idx] ) {
  477. bS[i] = 2;
  478. }
  479. else if(!mv_done)
  480. {
  481. bS[i] = check_mv(h, b_idx, bn_idx, mvy_limit);
  482. }
  483. }
  484. if(bS[0]+bS[1]+bS[2]+bS[3] == 0)
  485. continue;
  486. }
  487. /* Filter edge */
  488. // Do not use s->qscale as luma quantizer because it has not the same
  489. // value in IPCM macroblocks.
  490. qp = s->current_picture.qscale_table[mb_xy];
  491. //tprintf(s->avctx, "filter mb:%d/%d dir:%d edge:%d, QPy:%d, QPc:%d, QPcn:%d\n", mb_x, mb_y, dir, edge, qp, h->chroma_qp[0], s->current_picture.qscale_table[mbn_xy]);
  492. tprintf(s->avctx, "filter mb:%d/%d dir:%d edge:%d, QPy:%d ls:%d uvls:%d", mb_x, mb_y, dir, edge, qp, linesize, uvlinesize);
  493. //{ int i; for (i = 0; i < 4; i++) tprintf(s->avctx, " bS[%d]:%d", i, bS[i]); tprintf(s->avctx, "\n"); }
  494. if( dir == 0 ) {
  495. filter_mb_edgev( &img_y[4*edge], linesize, bS, qp, h );
  496. if( (edge&1) == 0 ) {
  497. filter_mb_edgecv( &img_cb[2*edge], uvlinesize, bS, h->chroma_qp[0], h);
  498. filter_mb_edgecv( &img_cr[2*edge], uvlinesize, bS, h->chroma_qp[1], h);
  499. }
  500. } else {
  501. filter_mb_edgeh( &img_y[4*edge*linesize], linesize, bS, qp, h );
  502. if( (edge&1) == 0 ) {
  503. filter_mb_edgech( &img_cb[2*edge*uvlinesize], uvlinesize, bS, h->chroma_qp[0], h);
  504. filter_mb_edgech( &img_cr[2*edge*uvlinesize], uvlinesize, bS, h->chroma_qp[1], h);
  505. }
  506. }
  507. }
  508. }
  509. void ff_h264_filter_mb( H264Context *h, int mb_x, int mb_y, uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr, unsigned int linesize, unsigned int uvlinesize) {
  510. MpegEncContext * const s = &h->s;
  511. const int mb_xy= mb_x + mb_y*s->mb_stride;
  512. const int mb_type = s->current_picture.mb_type[mb_xy];
  513. const int mvy_limit = IS_INTERLACED(mb_type) ? 2 : 4;
  514. int first_vertical_edge_done = 0;
  515. av_unused int dir;
  516. if (FRAME_MBAFF
  517. // and current and left pair do not have the same interlaced type
  518. && IS_INTERLACED(mb_type^h->left_type[0])
  519. // and left mb is in available to us
  520. && h->left_type[0]) {
  521. /* First vertical edge is different in MBAFF frames
  522. * There are 8 different bS to compute and 2 different Qp
  523. */
  524. DECLARE_ALIGNED(8, int16_t, bS)[8];
  525. int qp[2];
  526. int bqp[2];
  527. int rqp[2];
  528. int mb_qp, mbn0_qp, mbn1_qp;
  529. int i;
  530. first_vertical_edge_done = 1;
  531. if( IS_INTRA(mb_type) ) {
  532. AV_WN64A(&bS[0], 0x0004000400040004ULL);
  533. AV_WN64A(&bS[4], 0x0004000400040004ULL);
  534. } else {
  535. static const uint8_t offset[2][2][8]={
  536. {
  537. {7+8*0, 7+8*0, 7+8*0, 7+8*0, 7+8*1, 7+8*1, 7+8*1, 7+8*1},
  538. {7+8*2, 7+8*2, 7+8*2, 7+8*2, 7+8*3, 7+8*3, 7+8*3, 7+8*3},
  539. },{
  540. {7+8*0, 7+8*1, 7+8*2, 7+8*3, 7+8*0, 7+8*1, 7+8*2, 7+8*3},
  541. {7+8*0, 7+8*1, 7+8*2, 7+8*3, 7+8*0, 7+8*1, 7+8*2, 7+8*3},
  542. }
  543. };
  544. const uint8_t *off= offset[MB_FIELD][mb_y&1];
  545. for( i = 0; i < 8; i++ ) {
  546. int j= MB_FIELD ? i>>2 : i&1;
  547. int mbn_xy = h->left_mb_xy[j];
  548. int mbn_type= h->left_type[j];
  549. if( IS_INTRA( mbn_type ) )
  550. bS[i] = 4;
  551. else{
  552. bS[i] = 1 + !!(h->non_zero_count_cache[12+8*(i>>1)] |
  553. ((!h->pps.cabac && IS_8x8DCT(mbn_type)) ?
  554. (h->cbp_table[mbn_xy] & ((MB_FIELD ? (i&2) : (mb_y&1)) ? 8 : 2))
  555. :
  556. h->non_zero_count[mbn_xy][ off[i] ]));
  557. }
  558. }
  559. }
  560. mb_qp = s->current_picture.qscale_table[mb_xy];
  561. mbn0_qp = s->current_picture.qscale_table[h->left_mb_xy[0]];
  562. mbn1_qp = s->current_picture.qscale_table[h->left_mb_xy[1]];
  563. qp[0] = ( mb_qp + mbn0_qp + 1 ) >> 1;
  564. bqp[0] = ( get_chroma_qp( h, 0, mb_qp ) +
  565. get_chroma_qp( h, 0, mbn0_qp ) + 1 ) >> 1;
  566. rqp[0] = ( get_chroma_qp( h, 1, mb_qp ) +
  567. get_chroma_qp( h, 1, mbn0_qp ) + 1 ) >> 1;
  568. qp[1] = ( mb_qp + mbn1_qp + 1 ) >> 1;
  569. bqp[1] = ( get_chroma_qp( h, 0, mb_qp ) +
  570. get_chroma_qp( h, 0, mbn1_qp ) + 1 ) >> 1;
  571. rqp[1] = ( get_chroma_qp( h, 1, mb_qp ) +
  572. get_chroma_qp( h, 1, mbn1_qp ) + 1 ) >> 1;
  573. /* Filter edge */
  574. tprintf(s->avctx, "filter mb:%d/%d MBAFF, QPy:%d/%d, QPb:%d/%d QPr:%d/%d ls:%d uvls:%d", mb_x, mb_y, qp[0], qp[1], bqp[0], bqp[1], rqp[0], rqp[1], linesize, uvlinesize);
  575. { int i; for (i = 0; i < 8; i++) tprintf(s->avctx, " bS[%d]:%d", i, bS[i]); tprintf(s->avctx, "\n"); }
  576. if(MB_FIELD){
  577. filter_mb_mbaff_edgev ( h, img_y , linesize, bS , 1, qp [0] );
  578. filter_mb_mbaff_edgev ( h, img_y + 8* linesize, linesize, bS+4, 1, qp [1] );
  579. filter_mb_mbaff_edgecv( h, img_cb, uvlinesize, bS , 1, bqp[0] );
  580. filter_mb_mbaff_edgecv( h, img_cb + 4*uvlinesize, uvlinesize, bS+4, 1, bqp[1] );
  581. filter_mb_mbaff_edgecv( h, img_cr, uvlinesize, bS , 1, rqp[0] );
  582. filter_mb_mbaff_edgecv( h, img_cr + 4*uvlinesize, uvlinesize, bS+4, 1, rqp[1] );
  583. }else{
  584. filter_mb_mbaff_edgev ( h, img_y , 2* linesize, bS , 2, qp [0] );
  585. filter_mb_mbaff_edgev ( h, img_y + linesize, 2* linesize, bS+1, 2, qp [1] );
  586. filter_mb_mbaff_edgecv( h, img_cb, 2*uvlinesize, bS , 2, bqp[0] );
  587. filter_mb_mbaff_edgecv( h, img_cb + uvlinesize, 2*uvlinesize, bS+1, 2, bqp[1] );
  588. filter_mb_mbaff_edgecv( h, img_cr, 2*uvlinesize, bS , 2, rqp[0] );
  589. filter_mb_mbaff_edgecv( h, img_cr + uvlinesize, 2*uvlinesize, bS+1, 2, rqp[1] );
  590. }
  591. }
  592. #if CONFIG_SMALL
  593. for( dir = 0; dir < 2; dir++ )
  594. filter_mb_dir(h, mb_x, mb_y, img_y, img_cb, img_cr, linesize, uvlinesize, mb_xy, mb_type, mvy_limit, dir ? 0 : first_vertical_edge_done, dir);
  595. #else
  596. filter_mb_dir(h, mb_x, mb_y, img_y, img_cb, img_cr, linesize, uvlinesize, mb_xy, mb_type, mvy_limit, first_vertical_edge_done, 0);
  597. filter_mb_dir(h, mb_x, mb_y, img_y, img_cb, img_cr, linesize, uvlinesize, mb_xy, mb_type, mvy_limit, 0, 1);
  598. #endif
  599. }