1 /*****************************************************************************
2 * macroblock.c: h264 encoder library
3 *****************************************************************************
4 * Copyright (C) 2003-2008 x264 project
6 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
7 * Loren Merritt <lorenm@u.washington.edu>
8 * Fiona Glaser <fiona@x264.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
23 *****************************************************************************/
25 #include "common/common.h"
26 #include "macroblock.h"
28 #define ZIG(i,y,x) level[i] = dct[x][y];
29 static inline void zigzag_scan_2x2_dc( int16_t level[4], int16_t dct[2][2] )
39 * x264_mb_decimate_score: given dct coeffs it returns a score to see if we could empty this dct coeffs
40 * to 0 (low score means set it to null)
41 * Used in inter macroblock (luma and chroma)
42 * luma: for a 8x8 block: if score < 4 -> null
43 * for the complete mb: if score < 6 -> null
44 * chroma: for the complete mb: if score < 7 -> null
46 static int x264_mb_decimate_score( int16_t *dct, int i_max )
48 static const int i_ds_table4[16] = {
49 3,2,2,1,1,1,0,0,0,0,0,0,0,0,0,0 };
50 static const int i_ds_table8[64] = {
51 3,3,3,3,2,2,2,2,2,2,2,2,1,1,1,1,
52 1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,
53 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
54 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
56 const int *ds_table = (i_max == 64) ? i_ds_table8 : i_ds_table4;
60 while( idx >= 0 && dct[idx] == 0 )
67 if( (unsigned)(dct[idx--] + 1) > 2 )
71 while( idx >= 0 && dct[idx] == 0 )
76 i_score += ds_table[i_run];
82 static ALWAYS_INLINE void x264_quant_4x4( x264_t *h, int16_t dct[4][4], int i_qp, int i_ctxBlockCat, int b_intra, int idx )
84 int i_quant_cat = b_intra ? CQM_4IY : CQM_4PY;
86 x264_quant_4x4_trellis( h, dct, i_quant_cat, i_qp, i_ctxBlockCat, b_intra, idx );
88 h->quantf.quant_4x4( dct, h->quant4_mf[i_quant_cat][i_qp], h->quant4_bias[i_quant_cat][i_qp] );
91 static ALWAYS_INLINE void x264_quant_8x8( x264_t *h, int16_t dct[8][8], int i_qp, int b_intra, int idx )
93 int i_quant_cat = b_intra ? CQM_8IY : CQM_8PY;
95 x264_quant_8x8_trellis( h, dct, i_quant_cat, i_qp, b_intra, idx );
97 h->quantf.quant_8x8( dct, h->quant8_mf[i_quant_cat][i_qp], h->quant8_bias[i_quant_cat][i_qp] );
100 void x264_mb_encode_i4x4( x264_t *h, int idx, int i_qp )
102 uint8_t *p_src = &h->mb.pic.p_fenc[0][block_idx_xy_fenc[idx]];
103 uint8_t *p_dst = &h->mb.pic.p_fdec[0][block_idx_xy_fdec[idx]];
104 DECLARE_ALIGNED_16( int16_t dct4x4[4][4] );
106 if( h->mb.b_lossless )
108 h->zigzagf.sub_4x4( h->dct.luma4x4[idx], p_src, p_dst );
112 h->dctf.sub4x4_dct( dct4x4, p_src, p_dst );
114 x264_quant_4x4( h, dct4x4, i_qp, DCT_LUMA_4x4, 1, idx );
116 if( array_non_zero( dct4x4 ) )
118 h->zigzagf.scan_4x4( h->dct.luma4x4[idx], dct4x4 );
119 h->quantf.dequant_4x4( dct4x4, h->dequant4_mf[CQM_4IY], i_qp );
121 /* output samples to fdec */
122 h->dctf.add4x4_idct( p_dst, dct4x4 );
125 memset( h->dct.luma4x4[idx], 0, sizeof(h->dct.luma4x4[idx]));
128 void x264_mb_encode_i8x8( x264_t *h, int idx, int i_qp )
131 int y = 8 * (idx>>1);
132 uint8_t *p_src = &h->mb.pic.p_fenc[0][x+y*FENC_STRIDE];
133 uint8_t *p_dst = &h->mb.pic.p_fdec[0][x+y*FDEC_STRIDE];
134 DECLARE_ALIGNED_16( int16_t dct8x8[8][8] );
136 h->dctf.sub8x8_dct8( dct8x8, p_src, p_dst );
138 x264_quant_8x8( h, dct8x8, i_qp, 1, idx );
140 h->zigzagf.scan_8x8( h->dct.luma8x8[idx], dct8x8 );
141 h->quantf.dequant_8x8( dct8x8, h->dequant8_mf[CQM_8IY], i_qp );
142 h->dctf.add8x8_idct8( p_dst, dct8x8 );
145 static void x264_mb_encode_i16x16( x264_t *h, int i_qp )
147 uint8_t *p_src = h->mb.pic.p_fenc[0];
148 uint8_t *p_dst = h->mb.pic.p_fdec[0];
150 DECLARE_ALIGNED_16( int16_t dct4x4[16][4][4] );
151 DECLARE_ALIGNED_16( int16_t dct_dc4x4[4][4] );
155 if( h->mb.b_lossless )
157 for( i = 0; i < 16; i++ )
159 int oe = block_idx_xy_fenc[i];
160 int od = block_idx_xy_fdec[i];
161 h->zigzagf.sub_4x4( h->dct.luma4x4[i], p_src+oe, p_dst+od );
162 dct_dc4x4[0][block_idx_yx_1d[i]] = h->dct.luma4x4[i][0];
163 h->dct.luma4x4[i][0] = 0;
165 h->zigzagf.scan_4x4( h->dct.luma16x16_dc, dct_dc4x4 );
169 h->dctf.sub16x16_dct( dct4x4, p_src, p_dst );
170 for( i = 0; i < 16; i++ )
173 dct_dc4x4[0][block_idx_xy_1d[i]] = dct4x4[i][0][0];
176 /* quant/scan/dequant */
177 x264_quant_4x4( h, dct4x4[i], i_qp, DCT_LUMA_AC, 1, i );
179 h->zigzagf.scan_4x4( h->dct.luma4x4[i], dct4x4[i] );
180 h->quantf.dequant_4x4( dct4x4[i], h->dequant4_mf[CQM_4IY], i_qp );
183 h->dctf.dct4x4dc( dct_dc4x4 );
184 h->quantf.quant_4x4_dc( dct_dc4x4, h->quant4_mf[CQM_4IY][i_qp][0]>>1, h->quant4_bias[CQM_4IY][i_qp][0]<<1 );
185 h->zigzagf.scan_4x4( h->dct.luma16x16_dc, dct_dc4x4 );
187 /* output samples to fdec */
188 h->dctf.idct4x4dc( dct_dc4x4 );
189 x264_mb_dequant_4x4_dc( dct_dc4x4, h->dequant4_mf[CQM_4IY], i_qp ); /* XXX not inversed */
191 /* calculate dct coeffs */
192 for( i = 0; i < 16; i++ )
195 dct4x4[i][0][0] = dct_dc4x4[0][block_idx_xy_1d[i]];
197 /* put pixels to fdec */
198 h->dctf.add16x16_idct( p_dst, dct4x4 );
201 void x264_mb_encode_8x8_chroma( x264_t *h, int b_inter, int i_qp )
204 int b_decimate = b_inter && (h->sh.i_type == SLICE_TYPE_B || h->param.analyse.b_dct_decimate);
206 for( ch = 0; ch < 2; ch++ )
208 uint8_t *p_src = h->mb.pic.p_fenc[1+ch];
209 uint8_t *p_dst = h->mb.pic.p_fdec[1+ch];
210 int i_decimate_score = 0;
212 DECLARE_ALIGNED_16( int16_t dct2x2[2][2] );
213 DECLARE_ALIGNED_16( int16_t dct4x4[4][4][4] );
215 if( h->mb.b_lossless )
217 for( i = 0; i < 4; i++ )
219 int oe = block_idx_x[i]*4 + block_idx_y[i]*4*FENC_STRIDE;
220 int od = block_idx_x[i]*4 + block_idx_y[i]*4*FDEC_STRIDE;
221 h->zigzagf.sub_4x4( h->dct.luma4x4[16+i+ch*4], p_src+oe, p_dst+od );
222 h->dct.chroma_dc[ch][i] = h->dct.luma4x4[16+i+ch*4][0];
223 h->dct.luma4x4[16+i+ch*4][0] = 0;
228 h->dctf.sub8x8_dct( dct4x4, p_src, p_dst );
229 /* calculate dct coeffs */
230 for( i = 0; i < 4; i++ )
233 dct2x2[i>>1][i&1] = dct4x4[i][0][0];
236 /* no trellis; it doesn't seem to help chroma noticeably */
237 h->quantf.quant_4x4( dct4x4[i], h->quant4_mf[CQM_4IC+b_inter][i_qp], h->quant4_bias[CQM_4IC+b_inter][i_qp] );
238 h->zigzagf.scan_4x4( h->dct.luma4x4[16+i+ch*4], dct4x4[i] );
241 i_decimate_score += x264_mb_decimate_score( h->dct.luma4x4[16+i+ch*4]+1, 15 );
244 h->dctf.dct2x2dc( dct2x2 );
245 h->quantf.quant_2x2_dc( dct2x2, h->quant4_mf[CQM_4IC+b_inter][i_qp][0]>>1, h->quant4_bias[CQM_4IC+b_inter][i_qp][0]<<1 );
246 zigzag_scan_2x2_dc( h->dct.chroma_dc[ch], dct2x2 );
248 /* output samples to fdec */
249 h->dctf.idct2x2dc( dct2x2 );
250 x264_mb_dequant_2x2_dc( dct2x2, h->dequant4_mf[CQM_4IC + b_inter], i_qp ); /* XXX not inversed */
252 if( b_decimate && i_decimate_score < 7 )
254 /* Near null chroma 8x8 block so make it null (bits saving) */
255 memset( &h->dct.luma4x4[16+ch*4], 0, 4 * sizeof( *h->dct.luma4x4 ) );
256 if( !array_non_zero( dct2x2 ) )
258 memset( dct4x4, 0, sizeof( dct4x4 ) );
262 for( i = 0; i < 4; i++ )
263 h->quantf.dequant_4x4( dct4x4[i], h->dequant4_mf[CQM_4IC + b_inter], i_qp );
265 dct4x4[0][0][0] = dct2x2[0][0];
266 dct4x4[1][0][0] = dct2x2[0][1];
267 dct4x4[2][0][0] = dct2x2[1][0];
268 dct4x4[3][0][0] = dct2x2[1][1];
269 h->dctf.add8x8_idct( p_dst, dct4x4 );
272 /* coded block pattern */
273 h->mb.i_cbp_chroma = 0;
274 for( i = 0; i < 8; i++ )
276 int nz = array_non_zero( h->dct.luma4x4[16+i] );
277 h->mb.cache.non_zero_count[x264_scan8[16+i]] = nz;
278 h->mb.i_cbp_chroma |= nz;
280 if( h->mb.i_cbp_chroma )
281 h->mb.i_cbp_chroma = 2; /* dc+ac (we can't do only ac) */
282 else if( array_non_zero( h->dct.chroma_dc ) )
283 h->mb.i_cbp_chroma = 1; /* dc only */
286 static void x264_macroblock_encode_skip( x264_t *h )
288 h->mb.i_cbp_luma = 0x00;
289 h->mb.i_cbp_chroma = 0x00;
290 memset( h->mb.cache.non_zero_count, 0, X264_SCAN8_SIZE );
292 h->mb.cbp[h->mb.i_mb_xy] = 0;
295 /*****************************************************************************
296 * x264_macroblock_encode_pskip:
297 * Encode an already marked skip block
298 *****************************************************************************/
299 static void x264_macroblock_encode_pskip( x264_t *h )
301 const int mvx = x264_clip3( h->mb.cache.mv[0][x264_scan8[0]][0],
302 h->mb.mv_min[0], h->mb.mv_max[0] );
303 const int mvy = x264_clip3( h->mb.cache.mv[0][x264_scan8[0]][1],
304 h->mb.mv_min[1], h->mb.mv_max[1] );
306 /* don't do pskip motion compensation if it was already done in macroblock_analyse */
307 if( !h->mb.b_skip_mc )
309 h->mc.mc_luma( h->mb.pic.p_fdec[0], FDEC_STRIDE,
310 h->mb.pic.p_fref[0][0], h->mb.pic.i_stride[0],
313 h->mc.mc_chroma( h->mb.pic.p_fdec[1], FDEC_STRIDE,
314 h->mb.pic.p_fref[0][0][4], h->mb.pic.i_stride[1],
317 h->mc.mc_chroma( h->mb.pic.p_fdec[2], FDEC_STRIDE,
318 h->mb.pic.p_fref[0][0][5], h->mb.pic.i_stride[2],
322 x264_macroblock_encode_skip( h );
325 /*****************************************************************************
326 * x264_macroblock_encode:
327 *****************************************************************************/
328 void x264_macroblock_encode( x264_t *h )
331 int i_qp = h->mb.i_qp;
332 int b_decimate = h->sh.i_type == SLICE_TYPE_B || h->param.analyse.b_dct_decimate;
333 int b_force_no_skip = 0;
335 uint8_t nnz8x8[4] = {1,1,1,1};
338 && h->mb.i_mb_xy == h->sh.i_first_mb + h->mb.i_mb_stride
339 && IS_SKIP(h->mb.type[h->sh.i_first_mb]) )
341 /* The first skip is predicted to be a frame mb pair.
342 * We don't yet support the aff part of mbaff, so force it to non-skip
343 * so that we can pick the aff flag. */
345 if( IS_SKIP(h->mb.i_type) )
347 if( h->mb.i_type == P_SKIP )
349 else if( h->mb.i_type == B_SKIP )
350 h->mb.i_type = B_DIRECT;
354 if( h->mb.i_type == P_SKIP )
357 x264_macroblock_encode_pskip( h );
360 if( h->mb.i_type == B_SKIP )
362 /* don't do bskip motion compensation if it was already done in macroblock_analyse */
363 if( !h->mb.b_skip_mc )
365 x264_macroblock_encode_skip( h );
369 if( h->mb.i_type == I_16x16 )
371 const int i_mode = h->mb.i_intra16x16_pred_mode;
372 h->mb.b_transform_8x8 = 0;
373 /* do the right prediction */
374 h->predict_16x16[i_mode]( h->mb.pic.p_fdec[0] );
376 /* encode the 16x16 macroblock */
377 x264_mb_encode_i16x16( h, i_qp );
379 else if( h->mb.i_type == I_8x8 )
381 DECLARE_ALIGNED_16( uint8_t edge[33] );
382 h->mb.b_transform_8x8 = 1;
383 /* If we already encoded 3 of the 4 i8x8 blocks, we don't have to do them again. */
384 if( h->mb.i_skip_intra )
386 h->mc.copy[PIXEL_16x16]( h->mb.pic.p_fdec[0], FDEC_STRIDE, h->mb.pic.i8x8_fdec_buf, 16, 16 );
387 /* In RD mode, restore the now-overwritten DCT data. */
388 if( h->mb.i_skip_intra == 2 )
389 h->mc.memcpy_aligned( h->dct.luma8x8, h->mb.pic.i8x8_dct_buf, sizeof(h->mb.pic.i8x8_dct_buf) );
391 for( i = h->mb.i_skip_intra ? 3 : 0 ; i < 4; i++ )
393 uint8_t *p_dst = &h->mb.pic.p_fdec[0][8 * (i&1) + 8 * (i>>1) * FDEC_STRIDE];
394 int i_mode = h->mb.cache.intra4x4_pred_mode[x264_scan8[4*i]];
396 x264_predict_8x8_filter( p_dst, edge, h->mb.i_neighbour8[i], x264_pred_i4x4_neighbors[i_mode] );
397 h->predict_8x8[i_mode]( p_dst, edge );
398 x264_mb_encode_i8x8( h, i, i_qp );
400 for( i = 0; i < 4; i++ )
401 nnz8x8[i] = array_non_zero( h->dct.luma8x8[i] );
403 else if( h->mb.i_type == I_4x4 )
405 h->mb.b_transform_8x8 = 0;
406 /* If we already encoded 15 of the 16 i4x4 blocks, we don't have to do them again. */
407 if( h->mb.i_skip_intra )
409 h->mc.copy[PIXEL_16x16]( h->mb.pic.p_fdec[0], FDEC_STRIDE, h->mb.pic.i4x4_fdec_buf, 16, 16 );
410 /* In RD mode, restore the now-overwritten DCT data. */
411 if( h->mb.i_skip_intra == 2 )
412 h->mc.memcpy_aligned( h->dct.luma4x4, h->mb.pic.i4x4_dct_buf, sizeof(h->mb.pic.i4x4_dct_buf) );
414 for( i = h->mb.i_skip_intra ? 15 : 0 ; i < 16; i++ )
416 uint8_t *p_dst = &h->mb.pic.p_fdec[0][block_idx_xy_fdec[i]];
417 int i_mode = h->mb.cache.intra4x4_pred_mode[x264_scan8[i]];
419 if( (h->mb.i_neighbour4[i] & (MB_TOPRIGHT|MB_TOP)) == MB_TOP )
420 /* emulate missing topright samples */
421 *(uint32_t*) &p_dst[4-FDEC_STRIDE] = p_dst[3-FDEC_STRIDE] * 0x01010101U;
423 h->predict_4x4[i_mode]( p_dst );
424 x264_mb_encode_i4x4( h, i, i_qp );
430 int i_decimate_mb = 0;
432 /* Don't repeat motion compensation if it was already done in non-RD transform analysis */
433 if( !h->mb.b_skip_mc )
436 if( h->mb.b_lossless )
438 for( i4x4 = 0; i4x4 < 16; i4x4++ )
440 h->zigzagf.sub_4x4( h->dct.luma4x4[i4x4],
441 h->mb.pic.p_fenc[0]+block_idx_xy_fenc[i4x4],
442 h->mb.pic.p_fdec[0]+block_idx_xy_fdec[i4x4] );
445 else if( h->mb.b_transform_8x8 )
447 DECLARE_ALIGNED_16( int16_t dct8x8[4][8][8] );
448 b_decimate &= !h->mb.b_trellis; // 8x8 trellis is inherently optimal decimation
449 h->dctf.sub16x16_dct8( dct8x8, h->mb.pic.p_fenc[0], h->mb.pic.p_fdec[0] );
450 h->nr_count[1] += h->mb.b_noise_reduction * 4;
452 for( idx = 0; idx < 4; idx++ )
454 if( h->mb.b_noise_reduction )
455 h->quantf.denoise_dct( *dct8x8[idx], h->nr_residual_sum[1], h->nr_offset[1], 64 );
456 x264_quant_8x8( h, dct8x8[idx], i_qp, 0, idx );
458 h->zigzagf.scan_8x8( h->dct.luma8x8[idx], dct8x8[idx] );
462 int i_decimate_8x8 = x264_mb_decimate_score( h->dct.luma8x8[idx], 64 );
463 i_decimate_mb += i_decimate_8x8;
464 if( i_decimate_8x8 < 4 )
468 nnz8x8[idx] = array_non_zero( dct8x8[idx] );
471 if( i_decimate_mb < 6 && b_decimate )
472 *(uint32_t*)nnz8x8 = 0;
475 for( idx = 0; idx < 4; idx++ )
478 h->quantf.dequant_8x8( dct8x8[idx], h->dequant8_mf[CQM_8PY], i_qp );
479 h->dctf.add8x8_idct8( &h->mb.pic.p_fdec[0][(idx&1)*8 + (idx>>1)*8*FDEC_STRIDE], dct8x8[idx] );
485 DECLARE_ALIGNED_16( int16_t dct4x4[16][4][4] );
486 h->dctf.sub16x16_dct( dct4x4, h->mb.pic.p_fenc[0], h->mb.pic.p_fdec[0] );
487 h->nr_count[0] += h->mb.b_noise_reduction * 16;
489 for( i8x8 = 0; i8x8 < 4; i8x8++ )
493 /* encode one 4x4 block */
495 for( i4x4 = 0; i4x4 < 4; i4x4++ )
497 idx = i8x8 * 4 + i4x4;
499 if( h->mb.b_noise_reduction )
500 h->quantf.denoise_dct( *dct4x4[idx], h->nr_residual_sum[0], h->nr_offset[0], 16 );
501 x264_quant_4x4( h, dct4x4[idx], i_qp, DCT_LUMA_4x4, 0, idx );
503 h->zigzagf.scan_4x4( h->dct.luma4x4[idx], dct4x4[idx] );
505 if( b_decimate && i_decimate_8x8 <= 6 )
506 i_decimate_8x8 += x264_mb_decimate_score( h->dct.luma4x4[idx], 16 );
509 /* decimate this 8x8 block */
510 i_decimate_mb += i_decimate_8x8;
511 if( i_decimate_8x8 < 4 && b_decimate )
515 if( i_decimate_mb < 6 && b_decimate )
516 *(uint32_t*)nnz8x8 = 0;
519 for( i8x8 = 0; i8x8 < 4; i8x8++ )
522 for( i = 0; i < 4; i++ )
523 h->quantf.dequant_4x4( dct4x4[i8x8*4+i], h->dequant4_mf[CQM_4PY], i_qp );
524 h->dctf.add8x8_idct( &h->mb.pic.p_fdec[0][(i8x8&1)*8 + (i8x8>>1)*8*FDEC_STRIDE], &dct4x4[i8x8*4] );
531 if( IS_INTRA( h->mb.i_type ) )
533 const int i_mode = h->mb.i_chroma_pred_mode;
534 h->predict_8x8c[i_mode]( h->mb.pic.p_fdec[1] );
535 h->predict_8x8c[i_mode]( h->mb.pic.p_fdec[2] );
538 /* encode the 8x8 blocks */
539 x264_mb_encode_8x8_chroma( h, !IS_INTRA( h->mb.i_type ), h->mb.i_chroma_qp );
541 /* coded block pattern and non_zero_count */
542 h->mb.i_cbp_luma = 0x00;
543 if( h->mb.i_type == I_16x16 )
545 for( i = 0; i < 16; i++ )
547 int nz = array_non_zero( h->dct.luma4x4[i] );
548 h->mb.cache.non_zero_count[x264_scan8[i]] = nz;
549 h->mb.i_cbp_luma |= nz;
551 h->mb.i_cbp_luma *= 0xf;
555 for( i = 0; i < 4; i++)
559 *(uint16_t*)&h->mb.cache.non_zero_count[x264_scan8[0+i*4]] = 0;
560 *(uint16_t*)&h->mb.cache.non_zero_count[x264_scan8[2+i*4]] = 0;
562 else if( h->mb.b_transform_8x8 )
564 *(uint16_t*)&h->mb.cache.non_zero_count[x264_scan8[0+4*i]] = nnz8x8[i] * 0x0101;
565 *(uint16_t*)&h->mb.cache.non_zero_count[x264_scan8[2+4*i]] = nnz8x8[i] * 0x0101;
566 h->mb.i_cbp_luma |= nnz8x8[i] << i;
571 for( j = 0; j < 4; j++ )
573 nz = array_non_zero( h->dct.luma4x4[j+4*i] );
574 h->mb.cache.non_zero_count[x264_scan8[j+4*i]] = nz;
577 h->mb.i_cbp_luma |= cbp << i;
582 if( h->param.b_cabac )
584 i_cbp_dc = ( h->mb.i_type == I_16x16 && array_non_zero( h->dct.luma16x16_dc ) )
585 | array_non_zero( h->dct.chroma_dc[0] ) << 1
586 | array_non_zero( h->dct.chroma_dc[1] ) << 2;
590 h->mb.cbp[h->mb.i_mb_xy] = (i_cbp_dc << 8) | (h->mb.i_cbp_chroma << 4) | h->mb.i_cbp_luma;
593 * XXX: in the me perhaps we should take x264_mb_predict_mv_pskip into account
594 * (if multiple mv give same result)*/
595 if( !b_force_no_skip )
597 if( h->mb.i_type == P_L0 && h->mb.i_partition == D_16x16 &&
598 !(h->mb.i_cbp_luma | h->mb.i_cbp_chroma) &&
599 *(uint32_t*)h->mb.cache.mv[0][x264_scan8[0]] == *(uint32_t*)h->mb.cache.pskip_mv
600 && h->mb.cache.ref[0][x264_scan8[0]] == 0 )
602 h->mb.i_type = P_SKIP;
605 /* Check for B_SKIP */
606 if( h->mb.i_type == B_DIRECT && !(h->mb.i_cbp_luma | h->mb.i_cbp_chroma) )
608 h->mb.i_type = B_SKIP;
613 /*****************************************************************************
614 * x264_macroblock_probe_skip:
615 * Check if the current MB could be encoded as a [PB]_SKIP (it supposes you use
617 *****************************************************************************/
618 int x264_macroblock_probe_skip( x264_t *h, const int b_bidir )
620 DECLARE_ALIGNED_16( int16_t dct4x4[4][4][4] );
621 DECLARE_ALIGNED_16( int16_t dct2x2[2][2] );
622 DECLARE_ALIGNED_16( int16_t dctscan[16] );
624 int i_qp = h->mb.i_qp;
634 mvp[0] = x264_clip3( h->mb.cache.pskip_mv[0], h->mb.mv_min[0], h->mb.mv_max[0] );
635 mvp[1] = x264_clip3( h->mb.cache.pskip_mv[1], h->mb.mv_min[1], h->mb.mv_max[1] );
637 /* Motion compensation */
638 h->mc.mc_luma( h->mb.pic.p_fdec[0], FDEC_STRIDE,
639 h->mb.pic.p_fref[0][0], h->mb.pic.i_stride[0],
640 mvp[0], mvp[1], 16, 16 );
643 for( i8x8 = 0, i_decimate_mb = 0; i8x8 < 4; i8x8++ )
645 int fenc_offset = (i8x8&1) * 8 + (i8x8>>1) * FENC_STRIDE * 8;
646 int fdec_offset = (i8x8&1) * 8 + (i8x8>>1) * FDEC_STRIDE * 8;
648 h->dctf.sub8x8_dct( dct4x4, h->mb.pic.p_fenc[0] + fenc_offset,
649 h->mb.pic.p_fdec[0] + fdec_offset );
650 /* encode one 4x4 block */
651 for( i4x4 = 0; i4x4 < 4; i4x4++ )
653 h->quantf.quant_4x4( dct4x4[i4x4], h->quant4_mf[CQM_4PY][i_qp], h->quant4_bias[CQM_4PY][i_qp] );
654 if( !array_non_zero(dct4x4[i4x4]) )
656 h->zigzagf.scan_4x4( dctscan, dct4x4[i4x4] );
657 i_decimate_mb += x264_mb_decimate_score( dctscan, 16 );
658 if( i_decimate_mb >= 6 )
664 i_qp = h->mb.i_chroma_qp;
665 thresh = (x264_lambda2_tab[i_qp] + 32) >> 6;
667 for( ch = 0; ch < 2; ch++ )
669 uint8_t *p_src = h->mb.pic.p_fenc[1+ch];
670 uint8_t *p_dst = h->mb.pic.p_fdec[1+ch];
674 h->mc.mc_chroma( h->mb.pic.p_fdec[1+ch], FDEC_STRIDE,
675 h->mb.pic.p_fref[0][0][4+ch], h->mb.pic.i_stride[1+ch],
676 mvp[0], mvp[1], 8, 8 );
679 /* there is almost never a termination during chroma, but we can't avoid the check entirely */
680 /* so instead we check SSD and skip the actual check if the score is low enough. */
681 if( h->pixf.ssd[PIXEL_8x8]( p_dst, FDEC_STRIDE, p_src, FENC_STRIDE ) < thresh )
684 h->dctf.sub8x8_dct( dct4x4, p_src, p_dst );
686 /* calculate dct DC */
687 dct2x2[0][0] = dct4x4[0][0][0];
688 dct2x2[0][1] = dct4x4[1][0][0];
689 dct2x2[1][0] = dct4x4[2][0][0];
690 dct2x2[1][1] = dct4x4[3][0][0];
691 h->dctf.dct2x2dc( dct2x2 );
692 h->quantf.quant_2x2_dc( dct2x2, h->quant4_mf[CQM_4PC][i_qp][0]>>1, h->quant4_bias[CQM_4PC][i_qp][0]<<1 );
693 if( array_non_zero(dct2x2) )
696 /* calculate dct coeffs */
697 for( i4x4 = 0, i_decimate_mb = 0; i4x4 < 4; i4x4++ )
699 h->quantf.quant_4x4( dct4x4[i4x4], h->quant4_mf[CQM_4PC][i_qp], h->quant4_bias[CQM_4PC][i_qp] );
700 if( !array_non_zero(dct4x4[i4x4]) )
702 h->zigzagf.scan_4x4( dctscan, dct4x4[i4x4] );
703 i_decimate_mb += x264_mb_decimate_score( dctscan+1, 15 );
704 if( i_decimate_mb >= 7 )
713 /****************************************************************************
714 * DCT-domain noise reduction / adaptive deadzone
716 ****************************************************************************/
718 void x264_noise_reduction_update( x264_t *h )
721 for( cat = 0; cat < 2; cat++ )
723 int size = cat ? 64 : 16;
724 const uint16_t *weight = cat ? x264_dct8_weight2_tab : x264_dct4_weight2_tab;
726 if( h->nr_count[cat] > (cat ? (1<<16) : (1<<18)) )
728 for( i = 0; i < size; i++ )
729 h->nr_residual_sum[cat][i] >>= 1;
730 h->nr_count[cat] >>= 1;
733 for( i = 0; i < size; i++ )
734 h->nr_offset[cat][i] =
735 ((uint64_t)h->param.analyse.i_noise_reduction * h->nr_count[cat]
736 + h->nr_residual_sum[cat][i]/2)
737 / ((uint64_t)h->nr_residual_sum[cat][i] * weight[i]/256 + 1);
741 /*****************************************************************************
742 * RD only; 4 calls to this do not make up for one macroblock_encode.
743 * doesn't transform chroma dc.
744 *****************************************************************************/
745 void x264_macroblock_encode_p8x8( x264_t *h, int i8 )
747 int i_qp = h->mb.i_qp;
748 uint8_t *p_fenc = h->mb.pic.p_fenc[0] + (i8&1)*8 + (i8>>1)*8*FENC_STRIDE;
749 uint8_t *p_fdec = h->mb.pic.p_fdec[0] + (i8&1)*8 + (i8>>1)*8*FDEC_STRIDE;
750 int b_decimate = h->sh.i_type == SLICE_TYPE_B || h->param.analyse.b_dct_decimate;
754 x264_mb_mc_8x8( h, i8 );
756 if( h->mb.b_lossless )
759 for( i4 = i8*4; i4 < i8*4+4; i4++ )
761 h->zigzagf.sub_4x4( h->dct.luma4x4[i4],
762 h->mb.pic.p_fenc[0]+block_idx_xy_fenc[i4],
763 h->mb.pic.p_fdec[0]+block_idx_xy_fdec[i4] );
764 nnz8x8 |= array_non_zero( h->dct.luma4x4[i4] );
766 for( ch = 0; ch < 2; ch++ )
768 p_fenc = h->mb.pic.p_fenc[1+ch] + (i8&1)*4 + (i8>>1)*4*FENC_STRIDE;
769 p_fdec = h->mb.pic.p_fdec[1+ch] + (i8&1)*4 + (i8>>1)*4*FDEC_STRIDE;
770 h->zigzagf.sub_4x4( h->dct.luma4x4[16+i8+ch*4], p_fenc, p_fdec );
771 h->dct.luma4x4[16+i8+ch*4][0] = 0;
776 if( h->mb.b_transform_8x8 )
778 DECLARE_ALIGNED_16( int16_t dct8x8[8][8] );
779 h->dctf.sub8x8_dct8( dct8x8, p_fenc, p_fdec );
780 x264_quant_8x8( h, dct8x8, i_qp, 0, i8 );
781 h->zigzagf.scan_8x8( h->dct.luma8x8[i8], dct8x8 );
783 if( b_decimate && !h->mb.b_trellis )
784 nnz8x8 = 4 <= x264_mb_decimate_score( h->dct.luma8x8[i8], 64 );
786 nnz8x8 = array_non_zero( dct8x8 );
790 h->quantf.dequant_8x8( dct8x8, h->dequant8_mf[CQM_8PY], i_qp );
791 h->dctf.add8x8_idct8( p_fdec, dct8x8 );
797 DECLARE_ALIGNED_16( int16_t dct4x4[4][4][4] );
798 h->dctf.sub8x8_dct( dct4x4, p_fenc, p_fdec );
799 for( i4 = 0; i4 < 4; i4++ )
800 x264_quant_4x4( h, dct4x4[i4], i_qp, DCT_LUMA_4x4, 0, i8*4+i4 );
802 for( i4 = 0; i4 < 4; i4++ )
803 h->zigzagf.scan_4x4( h->dct.luma4x4[i8*4+i4], dct4x4[i4] );
807 int i_decimate_8x8 = 0;
808 for( i4 = 0; i4 < 4 && i_decimate_8x8 < 4; i4++ )
809 i_decimate_8x8 += x264_mb_decimate_score( h->dct.luma4x4[i8*4+i4], 16 );
810 nnz8x8 = 4 <= i_decimate_8x8;
813 nnz8x8 = array_non_zero( dct4x4 );
817 for( i4 = 0; i4 < 4; i4++ )
818 h->quantf.dequant_4x4( dct4x4[i4], h->dequant4_mf[CQM_4PY], i_qp );
819 h->dctf.add8x8_idct( p_fdec, dct4x4 );
823 i_qp = h->mb.i_chroma_qp;
825 for( ch = 0; ch < 2; ch++ )
827 DECLARE_ALIGNED_16( int16_t dct4x4[4][4] );
828 p_fenc = h->mb.pic.p_fenc[1+ch] + (i8&1)*4 + (i8>>1)*4*FENC_STRIDE;
829 p_fdec = h->mb.pic.p_fdec[1+ch] + (i8&1)*4 + (i8>>1)*4*FDEC_STRIDE;
831 h->dctf.sub4x4_dct( dct4x4, p_fenc, p_fdec );
832 h->quantf.quant_4x4( dct4x4, h->quant4_mf[CQM_4PC][i_qp], h->quant4_bias[CQM_4PC][i_qp] );
833 h->zigzagf.scan_4x4( h->dct.luma4x4[16+i8+ch*4], dct4x4 );
834 h->dct.luma4x4[16+i8+ch*4][0] = 0;
835 if( array_non_zero( dct4x4 ) )
837 h->quantf.dequant_4x4( dct4x4, h->dequant4_mf[CQM_4PC], i_qp );
838 h->dctf.add4x4_idct( p_fdec, dct4x4 );
842 h->mb.i_cbp_luma &= ~(1 << i8);
843 h->mb.i_cbp_luma |= nnz8x8 << i8;
844 h->mb.i_cbp_chroma = 0x02;