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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 #include "common/common.h"
26 #include "macroblock.h"
29 #define ZIG(i,y,x) level[i] = dct[x][y];
30 static inline void zigzag_scan_2x2_dc( int16_t level[4], int16_t dct[2][2] )
40 * x264_mb_decimate_score: given dct coeffs it returns a score to see if we could empty this dct coeffs
41 * to 0 (low score means set it to null)
42 * Used in inter macroblock (luma and chroma)
43 * luma: for a 8x8 block: if score < 4 -> null
44 * for the complete mb: if score < 6 -> null
45 * chroma: for the complete mb: if score < 7 -> null
47 static int x264_mb_decimate_score( int16_t *dct, int i_max )
49 static const int i_ds_table4[16] = {
50 3,2,2,1,1,1,0,0,0,0,0,0,0,0,0,0 };
51 static const int i_ds_table8[64] = {
52 3,3,3,3,2,2,2,2,2,2,2,2,1,1,1,1,
53 1,1,1,1,1,1,1,1,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,
55 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
57 const int *ds_table = (i_max == 64) ? i_ds_table8 : i_ds_table4;
61 while( idx >= 0 && dct[idx] == 0 )
68 if( (unsigned)(dct[idx--] + 1) > 2 )
72 while( idx >= 0 && dct[idx] == 0 )
77 i_score += ds_table[i_run];
83 void x264_mb_encode_i4x4( x264_t *h, int idx, int i_qscale )
85 int x = 4 * block_idx_x[idx];
86 int y = 4 * block_idx_y[idx];
87 uint8_t *p_src = &h->mb.pic.p_fenc[0][x+y*FENC_STRIDE];
88 uint8_t *p_dst = &h->mb.pic.p_fdec[0][x+y*FDEC_STRIDE];
89 DECLARE_ALIGNED_16( int16_t dct4x4[4][4] );
91 if( h->mb.b_lossless )
93 h->zigzagf.sub_4x4( h->dct.luma4x4[idx], p_src, p_dst );
97 h->dctf.sub4x4_dct( dct4x4, p_src, p_dst );
100 x264_quant_4x4_trellis( h, dct4x4, CQM_4IY, i_qscale, DCT_LUMA_4x4, 1 );
102 h->quantf.quant_4x4( dct4x4, h->quant4_mf[CQM_4IY][i_qscale], h->quant4_bias[CQM_4IY][i_qscale] );
104 if( array_non_zero( dct4x4 ) )
106 h->zigzagf.scan_4x4( h->dct.luma4x4[idx], dct4x4 );
107 h->quantf.dequant_4x4( dct4x4, h->dequant4_mf[CQM_4IY], i_qscale );
109 /* output samples to fdec */
110 h->dctf.add4x4_idct( p_dst, dct4x4 );
113 memset( h->dct.luma4x4[idx], 0, sizeof(h->dct.luma4x4[idx]));
116 void x264_mb_encode_i8x8( x264_t *h, int idx, int i_qscale )
119 int y = 8 * (idx>>1);
120 uint8_t *p_src = &h->mb.pic.p_fenc[0][x+y*FENC_STRIDE];
121 uint8_t *p_dst = &h->mb.pic.p_fdec[0][x+y*FDEC_STRIDE];
122 DECLARE_ALIGNED_16( int16_t dct8x8[8][8] );
124 h->dctf.sub8x8_dct8( dct8x8, p_src, p_dst );
126 if( h->mb.b_trellis )
127 x264_quant_8x8_trellis( h, dct8x8, CQM_8IY, i_qscale, 1 );
129 h->quantf.quant_8x8( dct8x8, h->quant8_mf[CQM_8IY][i_qscale], h->quant8_bias[CQM_8IY][i_qscale] );
131 h->zigzagf.scan_8x8( h->dct.luma8x8[idx], dct8x8 );
132 h->quantf.dequant_8x8( dct8x8, h->dequant8_mf[CQM_8IY], i_qscale );
133 h->dctf.add8x8_idct8( p_dst, dct8x8 );
136 static void x264_mb_encode_i16x16( x264_t *h, int i_qscale )
138 uint8_t *p_src = h->mb.pic.p_fenc[0];
139 uint8_t *p_dst = h->mb.pic.p_fdec[0];
141 DECLARE_ALIGNED_16( int16_t dct4x4[16][4][4] );
142 DECLARE_ALIGNED_16( int16_t dct_dc4x4[4][4] );
146 if( h->mb.b_lossless )
148 for( i = 0; i < 16; i++ )
150 int oe = block_idx_x[i]*4 + block_idx_y[i]*4*FENC_STRIDE;
151 int od = block_idx_x[i]*4 + block_idx_y[i]*4*FDEC_STRIDE;
152 h->zigzagf.sub_4x4( h->dct.luma4x4[i], p_src+oe, p_dst+od );
153 dct_dc4x4[block_idx_x[i]][block_idx_y[i]] = h->dct.luma4x4[i][0];
154 h->dct.luma4x4[i][0] = 0;
156 h->zigzagf.scan_4x4( h->dct.luma16x16_dc, dct_dc4x4 );
160 h->dctf.sub16x16_dct( dct4x4, p_src, p_dst );
161 for( i = 0; i < 16; i++ )
164 dct_dc4x4[block_idx_y[i]][block_idx_x[i]] = dct4x4[i][0][0];
167 /* quant/scan/dequant */
168 if( h->mb.b_trellis )
169 x264_quant_4x4_trellis( h, dct4x4[i], CQM_4IY, i_qscale, DCT_LUMA_AC, 1 );
171 h->quantf.quant_4x4( dct4x4[i], h->quant4_mf[CQM_4IY][i_qscale], h->quant4_bias[CQM_4IY][i_qscale] );
173 h->zigzagf.scan_4x4( h->dct.luma4x4[i], dct4x4[i] );
174 h->quantf.dequant_4x4( dct4x4[i], h->dequant4_mf[CQM_4IY], i_qscale );
177 h->dctf.dct4x4dc( dct_dc4x4 );
178 h->quantf.quant_4x4_dc( dct_dc4x4, h->quant4_mf[CQM_4IY][i_qscale][0]>>1, h->quant4_bias[CQM_4IY][i_qscale][0]<<1 );
179 h->zigzagf.scan_4x4( h->dct.luma16x16_dc, dct_dc4x4 );
181 /* output samples to fdec */
182 h->dctf.idct4x4dc( dct_dc4x4 );
183 x264_mb_dequant_4x4_dc( dct_dc4x4, h->dequant4_mf[CQM_4IY], i_qscale ); /* XXX not inversed */
185 /* calculate dct coeffs */
186 for( i = 0; i < 16; i++ )
189 dct4x4[i][0][0] = dct_dc4x4[block_idx_y[i]][block_idx_x[i]];
191 /* put pixels to fdec */
192 h->dctf.add16x16_idct( p_dst, dct4x4 );
195 void x264_mb_encode_8x8_chroma( x264_t *h, int b_inter, int i_qscale )
198 int b_decimate = b_inter && (h->sh.i_type == SLICE_TYPE_B || h->param.analyse.b_dct_decimate);
200 for( ch = 0; ch < 2; ch++ )
202 uint8_t *p_src = h->mb.pic.p_fenc[1+ch];
203 uint8_t *p_dst = h->mb.pic.p_fdec[1+ch];
204 int i_decimate_score = 0;
206 DECLARE_ALIGNED_16( int16_t dct2x2[2][2] );
207 DECLARE_ALIGNED_16( int16_t dct4x4[4][4][4] );
209 if( h->mb.b_lossless )
211 for( i = 0; i < 4; i++ )
213 int oe = block_idx_x[i]*4 + block_idx_y[i]*4*FENC_STRIDE;
214 int od = block_idx_x[i]*4 + block_idx_y[i]*4*FDEC_STRIDE;
215 h->zigzagf.sub_4x4( h->dct.luma4x4[16+i+ch*4], p_src+oe, p_dst+od );
216 h->dct.chroma_dc[ch][i] = h->dct.luma4x4[16+i+ch*4][0];
217 h->dct.luma4x4[16+i+ch*4][0] = 0;
222 h->dctf.sub8x8_dct( dct4x4, p_src, p_dst );
223 /* calculate dct coeffs */
224 for( i = 0; i < 4; i++ )
227 dct2x2[block_idx_y[i]][block_idx_x[i]] = dct4x4[i][0][0];
230 /* no trellis; it doesn't seem to help chroma noticeably */
231 h->quantf.quant_4x4( dct4x4[i], h->quant4_mf[CQM_4IC+b_inter][i_qscale], h->quant4_bias[CQM_4IC+b_inter][i_qscale] );
232 h->zigzagf.scan_4x4( h->dct.luma4x4[16+i+ch*4], dct4x4[i] );
236 i_decimate_score += x264_mb_decimate_score( h->dct.luma4x4[16+i+ch*4]+1, 15 );
240 h->dctf.dct2x2dc( dct2x2 );
241 h->quantf.quant_2x2_dc( dct2x2, h->quant4_mf[CQM_4IC+b_inter][i_qscale][0]>>1, h->quant4_bias[CQM_4IC+b_inter][i_qscale][0]<<1 );
242 zigzag_scan_2x2_dc( h->dct.chroma_dc[ch], dct2x2 );
244 /* output samples to fdec */
245 h->dctf.idct2x2dc( dct2x2 );
246 x264_mb_dequant_2x2_dc( dct2x2, h->dequant4_mf[CQM_4IC + b_inter], i_qscale ); /* XXX not inversed */
248 if( b_decimate && i_decimate_score < 7 )
250 /* Near null chroma 8x8 block so make it null (bits saving) */
251 memset( &h->dct.luma4x4[16+ch*4], 0, 4 * sizeof( *h->dct.luma4x4 ) );
252 if( !array_non_zero( dct2x2 ) )
254 memset( dct4x4, 0, sizeof( dct4x4 ) );
258 for( i = 0; i < 4; i++ )
259 h->quantf.dequant_4x4( dct4x4[i], h->dequant4_mf[CQM_4IC + b_inter], i_qscale );
262 for( i = 0; i < 4; i++ )
263 dct4x4[i][0][0] = dct2x2[0][i];
264 h->dctf.add8x8_idct( p_dst, dct4x4 );
267 /* coded block pattern */
268 h->mb.i_cbp_chroma = 0;
269 for( i = 0; i < 8; i++ )
271 int nz = array_non_zero( h->dct.luma4x4[16+i] );
272 h->mb.cache.non_zero_count[x264_scan8[16+i]] = nz;
273 h->mb.i_cbp_chroma |= nz;
275 if( h->mb.i_cbp_chroma )
276 h->mb.i_cbp_chroma = 2; /* dc+ac (we can't do only ac) */
277 else if( array_non_zero( h->dct.chroma_dc ) )
278 h->mb.i_cbp_chroma = 1; /* dc only */
281 static void x264_macroblock_encode_skip( x264_t *h )
283 h->mb.i_cbp_luma = 0x00;
284 h->mb.i_cbp_chroma = 0x00;
285 memset( h->mb.cache.non_zero_count, 0, X264_SCAN8_SIZE );
287 h->mb.cbp[h->mb.i_mb_xy] = 0;
290 /*****************************************************************************
291 * x264_macroblock_encode_pskip:
292 * Encode an already marked skip block
293 *****************************************************************************/
294 void x264_macroblock_encode_pskip( x264_t *h )
296 const int mvx = x264_clip3( h->mb.cache.mv[0][x264_scan8[0]][0],
297 h->mb.mv_min[0], h->mb.mv_max[0] );
298 const int mvy = x264_clip3( h->mb.cache.mv[0][x264_scan8[0]][1],
299 h->mb.mv_min[1], h->mb.mv_max[1] );
301 /* don't do pskip motion compensation if it was already done in macroblock_analyse */
302 if( !h->mb.b_skip_mc )
304 h->mc.mc_luma( h->mb.pic.p_fdec[0], FDEC_STRIDE,
305 h->mb.pic.p_fref[0][0], h->mb.pic.i_stride[0],
308 h->mc.mc_chroma( h->mb.pic.p_fdec[1], FDEC_STRIDE,
309 h->mb.pic.p_fref[0][0][4], h->mb.pic.i_stride[1],
312 h->mc.mc_chroma( h->mb.pic.p_fdec[2], FDEC_STRIDE,
313 h->mb.pic.p_fref[0][0][5], h->mb.pic.i_stride[2],
317 x264_macroblock_encode_skip( h );
320 /*****************************************************************************
321 * x264_macroblock_encode:
322 *****************************************************************************/
323 void x264_macroblock_encode( x264_t *h )
326 int i_qp = h->mb.i_qp;
327 int b_decimate = h->sh.i_type == SLICE_TYPE_B || h->param.analyse.b_dct_decimate;
328 int b_force_no_skip = 0;
330 uint8_t nnz8x8[4] = {1,1,1,1};
333 && h->mb.i_mb_xy == h->sh.i_first_mb + h->mb.i_mb_stride
334 && IS_SKIP(h->mb.type[h->sh.i_first_mb]) )
336 /* The first skip is predicted to be a frame mb pair.
337 * We don't yet support the aff part of mbaff, so force it to non-skip
338 * so that we can pick the aff flag. */
340 if( IS_SKIP(h->mb.i_type) )
342 if( h->mb.i_type == P_SKIP )
344 else if( h->mb.i_type == B_SKIP )
345 h->mb.i_type = B_DIRECT;
349 if( h->mb.i_type == P_SKIP )
352 x264_macroblock_encode_pskip( h );
355 if( h->mb.i_type == B_SKIP )
357 /* don't do bskip motion compensation if it was already done in macroblock_analyse */
358 if( !h->mb.b_skip_mc )
360 x264_macroblock_encode_skip( h );
364 if( h->mb.i_type == I_16x16 )
366 const int i_mode = h->mb.i_intra16x16_pred_mode;
367 h->mb.b_transform_8x8 = 0;
368 /* do the right prediction */
369 h->predict_16x16[i_mode]( h->mb.pic.p_fdec[0] );
371 /* encode the 16x16 macroblock */
372 x264_mb_encode_i16x16( h, i_qp );
374 else if( h->mb.i_type == I_8x8 )
376 DECLARE_ALIGNED_16( uint8_t edge[33] );
377 h->mb.b_transform_8x8 = 1;
378 /* If we already encoded 3 of the 4 i8x8 blocks, we don't have to do them again. */
379 if( h->mb.i_skip_intra )
381 h->mc.copy[PIXEL_16x16]( h->mb.pic.p_fdec[0], FDEC_STRIDE, h->mb.pic.i8x8_fdec_buf, 16, 16 );
382 /* In RD mode, restore the now-overwritten DCT data. */
383 if( h->mb.i_skip_intra == 2 )
384 h->mc.memcpy_aligned( h->dct.luma8x8, h->mb.pic.i8x8_dct_buf, sizeof(h->mb.pic.i8x8_dct_buf) );
386 for( i = h->mb.i_skip_intra ? 3 : 0 ; i < 4; i++ )
388 uint8_t *p_dst = &h->mb.pic.p_fdec[0][8 * (i&1) + 8 * (i>>1) * FDEC_STRIDE];
389 int i_mode = h->mb.cache.intra4x4_pred_mode[x264_scan8[4*i]];
391 x264_predict_8x8_filter( p_dst, edge, h->mb.i_neighbour8[i], x264_pred_i4x4_neighbors[i_mode] );
392 h->predict_8x8[i_mode]( p_dst, edge );
393 x264_mb_encode_i8x8( h, i, i_qp );
395 for( i = 0; i < 4; i++ )
396 nnz8x8[i] = array_non_zero( h->dct.luma8x8[i] );
398 else if( h->mb.i_type == I_4x4 )
400 h->mb.b_transform_8x8 = 0;
401 /* If we already encoded 15 of the 16 i4x4 blocks, we don't have to do them again. */
402 if( h->mb.i_skip_intra )
404 h->mc.copy[PIXEL_16x16]( h->mb.pic.p_fdec[0], FDEC_STRIDE, h->mb.pic.i4x4_fdec_buf, 16, 16 );
405 /* In RD mode, restore the now-overwritten DCT data. */
406 if( h->mb.i_skip_intra == 2 )
407 h->mc.memcpy_aligned( h->dct.luma4x4, h->mb.pic.i4x4_dct_buf, sizeof(h->mb.pic.i4x4_dct_buf) );
409 for( i = h->mb.i_skip_intra ? 15 : 0 ; i < 16; i++ )
411 uint8_t *p_dst = &h->mb.pic.p_fdec[0][4 * block_idx_x[i] + 4 * block_idx_y[i] * FDEC_STRIDE];
412 int i_mode = h->mb.cache.intra4x4_pred_mode[x264_scan8[i]];
414 if( (h->mb.i_neighbour4[i] & (MB_TOPRIGHT|MB_TOP)) == MB_TOP )
415 /* emulate missing topright samples */
416 *(uint32_t*) &p_dst[4-FDEC_STRIDE] = p_dst[3-FDEC_STRIDE] * 0x01010101U;
418 h->predict_4x4[i_mode]( p_dst );
419 x264_mb_encode_i4x4( h, i, i_qp );
425 int i_decimate_mb = 0;
427 /* Don't repeat motion compensation if it was already done in non-RD transform analysis */
428 if( !h->mb.b_skip_mc )
431 if( h->mb.b_lossless )
433 for( i4x4 = 0; i4x4 < 16; i4x4++ )
435 int x = 4*block_idx_x[i4x4];
436 int y = 4*block_idx_y[i4x4];
437 h->zigzagf.sub_4x4( h->dct.luma4x4[i4x4],
438 h->mb.pic.p_fenc[0]+x+y*FENC_STRIDE,
439 h->mb.pic.p_fdec[0]+x+y*FDEC_STRIDE );
442 else if( h->mb.b_transform_8x8 )
444 DECLARE_ALIGNED_16( int16_t dct8x8[4][8][8] );
445 b_decimate &= !h->mb.b_trellis; // 8x8 trellis is inherently optimal decimation
446 h->dctf.sub16x16_dct8( dct8x8, h->mb.pic.p_fenc[0], h->mb.pic.p_fdec[0] );
447 h->nr_count[1] += h->mb.b_noise_reduction * 4;
449 for( idx = 0; idx < 4; idx++ )
451 if( h->mb.b_noise_reduction )
452 h->quantf.denoise_dct_core( *dct8x8[idx], h->nr_residual_sum[1], h->nr_offset[1], 64 );
453 if( h->mb.b_trellis )
454 x264_quant_8x8_trellis( h, dct8x8[idx], CQM_8PY, i_qp, 0 );
456 h->quantf.quant_8x8( dct8x8[idx], h->quant8_mf[CQM_8PY][i_qp], h->quant8_bias[CQM_8PY][i_qp] );
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_core( *dct4x4[idx], h->nr_residual_sum[0], h->nr_offset[0], 16 );
501 if( h->mb.b_trellis )
502 x264_quant_4x4_trellis( h, dct4x4[idx], CQM_4PY, i_qp, DCT_LUMA_4x4, 0 );
504 h->quantf.quant_4x4( dct4x4[idx], h->quant4_mf[CQM_4PY][i_qp], h->quant4_bias[CQM_4PY][i_qp] );
506 h->zigzagf.scan_4x4( h->dct.luma4x4[idx], dct4x4[idx] );
508 if( b_decimate && i_decimate_8x8 <= 6 )
509 i_decimate_8x8 += x264_mb_decimate_score( h->dct.luma4x4[idx], 16 );
512 /* decimate this 8x8 block */
513 i_decimate_mb += i_decimate_8x8;
514 if( i_decimate_8x8 < 4 && b_decimate )
518 if( i_decimate_mb < 6 && b_decimate )
519 *(uint32_t*)nnz8x8 = 0;
522 for( i8x8 = 0; i8x8 < 4; i8x8++ )
525 for( i = 0; i < 4; i++ )
526 h->quantf.dequant_4x4( dct4x4[i8x8*4+i], h->dequant4_mf[CQM_4PY], i_qp );
527 h->dctf.add8x8_idct( &h->mb.pic.p_fdec[0][(i8x8&1)*8 + (i8x8>>1)*8*FDEC_STRIDE], &dct4x4[i8x8*4] );
534 if( IS_INTRA( h->mb.i_type ) )
536 const int i_mode = h->mb.i_chroma_pred_mode;
537 h->predict_8x8c[i_mode]( h->mb.pic.p_fdec[1] );
538 h->predict_8x8c[i_mode]( h->mb.pic.p_fdec[2] );
541 /* encode the 8x8 blocks */
542 x264_mb_encode_8x8_chroma( h, !IS_INTRA( h->mb.i_type ), h->mb.i_chroma_qp );
544 /* coded block pattern and non_zero_count */
545 h->mb.i_cbp_luma = 0x00;
546 if( h->mb.i_type == I_16x16 )
548 for( i = 0; i < 16; i++ )
550 int nz = array_non_zero( h->dct.luma4x4[i] );
551 h->mb.cache.non_zero_count[x264_scan8[i]] = nz;
552 h->mb.i_cbp_luma |= nz;
554 h->mb.i_cbp_luma *= 0xf;
558 for( i = 0; i < 4; i++)
562 *(uint16_t*)&h->mb.cache.non_zero_count[x264_scan8[0+i*4]] = 0;
563 *(uint16_t*)&h->mb.cache.non_zero_count[x264_scan8[2+i*4]] = 0;
565 else if( h->mb.b_transform_8x8 )
567 *(uint16_t*)&h->mb.cache.non_zero_count[x264_scan8[0+4*i]] = nnz8x8[i] * 0x0101;
568 *(uint16_t*)&h->mb.cache.non_zero_count[x264_scan8[2+4*i]] = nnz8x8[i] * 0x0101;
569 h->mb.i_cbp_luma |= nnz8x8[i] << i;
574 for( j = 0; j < 4; j++ )
576 nz = array_non_zero( h->dct.luma4x4[j+4*i] );
577 h->mb.cache.non_zero_count[x264_scan8[j+4*i]] = nz;
580 h->mb.i_cbp_luma |= cbp << i;
585 if( h->param.b_cabac )
587 i_cbp_dc = ( h->mb.i_type == I_16x16 && array_non_zero( h->dct.luma16x16_dc ) )
588 | array_non_zero( h->dct.chroma_dc[0] ) << 1
589 | array_non_zero( h->dct.chroma_dc[1] ) << 2;
593 h->mb.cbp[h->mb.i_mb_xy] = (i_cbp_dc << 8) | (h->mb.i_cbp_chroma << 4) | h->mb.i_cbp_luma;
596 * XXX: in the me perhaps we should take x264_mb_predict_mv_pskip into account
597 * (if multiple mv give same result)*/
598 if( !b_force_no_skip )
600 if( h->mb.i_type == P_L0 && h->mb.i_partition == D_16x16 &&
601 !(h->mb.i_cbp_luma | h->mb.i_cbp_chroma) &&
602 *(uint32_t*)h->mb.cache.mv[0][x264_scan8[0]] == *(uint32_t*)h->mb.cache.pskip_mv
603 && h->mb.cache.ref[0][x264_scan8[0]] == 0 )
605 h->mb.i_type = P_SKIP;
608 /* Check for B_SKIP */
609 if( h->mb.i_type == B_DIRECT && !(h->mb.i_cbp_luma | h->mb.i_cbp_chroma) )
611 h->mb.i_type = B_SKIP;
616 /*****************************************************************************
617 * x264_macroblock_probe_skip:
618 * Check if the current MB could be encoded as a [PB]_SKIP (it supposes you use
620 *****************************************************************************/
621 int x264_macroblock_probe_skip( x264_t *h, const int b_bidir )
623 DECLARE_ALIGNED_16( int16_t dct4x4[4][4][4] );
624 DECLARE_ALIGNED_16( int16_t dct2x2[2][2] );
625 DECLARE_ALIGNED_16( int16_t dctscan[16] );
627 int i_qp = h->mb.i_qp;
637 mvp[0] = x264_clip3( h->mb.cache.pskip_mv[0], h->mb.mv_min[0], h->mb.mv_max[0] );
638 mvp[1] = x264_clip3( h->mb.cache.pskip_mv[1], h->mb.mv_min[1], h->mb.mv_max[1] );
640 /* Motion compensation */
641 h->mc.mc_luma( h->mb.pic.p_fdec[0], FDEC_STRIDE,
642 h->mb.pic.p_fref[0][0], h->mb.pic.i_stride[0],
643 mvp[0], mvp[1], 16, 16 );
646 for( i8x8 = 0, i_decimate_mb = 0; i8x8 < 4; i8x8++ )
648 int fenc_offset = (i8x8&1) * 8 + (i8x8>>1) * FENC_STRIDE * 8;
649 int fdec_offset = (i8x8&1) * 8 + (i8x8>>1) * FDEC_STRIDE * 8;
651 h->dctf.sub8x8_dct( dct4x4, h->mb.pic.p_fenc[0] + fenc_offset,
652 h->mb.pic.p_fdec[0] + fdec_offset );
653 /* encode one 4x4 block */
654 for( i4x4 = 0; i4x4 < 4; i4x4++ )
656 h->quantf.quant_4x4( dct4x4[i4x4], h->quant4_mf[CQM_4PY][i_qp], h->quant4_bias[CQM_4PY][i_qp] );
657 if( !array_non_zero(dct4x4[i4x4]) )
659 h->zigzagf.scan_4x4( dctscan, dct4x4[i4x4] );
660 i_decimate_mb += x264_mb_decimate_score( dctscan, 16 );
661 if( i_decimate_mb >= 6 )
667 i_qp = h->mb.i_chroma_qp;
668 thresh = (x264_lambda2_tab[i_qp] + 32) >> 6;
670 for( ch = 0; ch < 2; ch++ )
672 uint8_t *p_src = h->mb.pic.p_fenc[1+ch];
673 uint8_t *p_dst = h->mb.pic.p_fdec[1+ch];
677 h->mc.mc_chroma( h->mb.pic.p_fdec[1+ch], FDEC_STRIDE,
678 h->mb.pic.p_fref[0][0][4+ch], h->mb.pic.i_stride[1+ch],
679 mvp[0], mvp[1], 8, 8 );
682 /* there is almost never a termination during chroma, but we can't avoid the check entirely */
683 /* so instead we check SSD and skip the actual check if the score is low enough. */
684 if( h->pixf.ssd[PIXEL_8x8]( p_dst, FDEC_STRIDE, p_src, FENC_STRIDE ) < thresh )
687 h->dctf.sub8x8_dct( dct4x4, p_src, p_dst );
689 /* calculate dct DC */
690 dct2x2[0][0] = dct4x4[0][0][0];
691 dct2x2[0][1] = dct4x4[1][0][0];
692 dct2x2[1][0] = dct4x4[2][0][0];
693 dct2x2[1][1] = dct4x4[3][0][0];
694 h->dctf.dct2x2dc( dct2x2 );
695 h->quantf.quant_2x2_dc( dct2x2, h->quant4_mf[CQM_4PC][i_qp][0]>>1, h->quant4_bias[CQM_4PC][i_qp][0]<<1 );
696 if( array_non_zero(dct2x2) )
699 /* calculate dct coeffs */
700 for( i4x4 = 0, i_decimate_mb = 0; i4x4 < 4; i4x4++ )
702 h->quantf.quant_4x4( dct4x4[i4x4], h->quant4_mf[CQM_4PC][i_qp], h->quant4_bias[CQM_4PC][i_qp] );
703 if( !array_non_zero(dct4x4[i4x4]) )
705 h->zigzagf.scan_4x4( dctscan, dct4x4[i4x4] );
706 i_decimate_mb += x264_mb_decimate_score( dctscan+1, 15 );
707 if( i_decimate_mb >= 7 )
716 /****************************************************************************
717 * DCT-domain noise reduction / adaptive deadzone
719 ****************************************************************************/
721 void x264_noise_reduction_update( x264_t *h )
724 for( cat = 0; cat < 2; cat++ )
726 int size = cat ? 64 : 16;
727 const uint16_t *weight = cat ? x264_dct8_weight2_tab : x264_dct4_weight2_tab;
729 if( h->nr_count[cat] > (cat ? (1<<16) : (1<<18)) )
731 for( i = 0; i < size; i++ )
732 h->nr_residual_sum[cat][i] >>= 1;
733 h->nr_count[cat] >>= 1;
736 for( i = 0; i < size; i++ )
737 h->nr_offset[cat][i] =
738 ((uint64_t)h->param.analyse.i_noise_reduction * h->nr_count[cat]
739 + h->nr_residual_sum[cat][i]/2)
740 / ((uint64_t)h->nr_residual_sum[cat][i] * weight[i]/256 + 1);
744 /*****************************************************************************
745 * RD only; 4 calls to this do not make up for one macroblock_encode.
746 * doesn't transform chroma dc.
747 *****************************************************************************/
748 void x264_macroblock_encode_p8x8( x264_t *h, int i8 )
750 int i_qp = h->mb.i_qp;
751 uint8_t *p_fenc = h->mb.pic.p_fenc[0] + (i8&1)*8 + (i8>>1)*8*FENC_STRIDE;
752 uint8_t *p_fdec = h->mb.pic.p_fdec[0] + (i8&1)*8 + (i8>>1)*8*FDEC_STRIDE;
753 int b_decimate = h->sh.i_type == SLICE_TYPE_B || h->param.analyse.b_dct_decimate;
757 x264_mb_mc_8x8( h, i8 );
759 if( h->mb.b_transform_8x8 )
761 DECLARE_ALIGNED_16( int16_t dct8x8[8][8] );
762 h->dctf.sub8x8_dct8( dct8x8, p_fenc, p_fdec );
763 h->quantf.quant_8x8( dct8x8, h->quant8_mf[CQM_8PY][i_qp], h->quant8_bias[CQM_8PY][i_qp] );
764 h->zigzagf.scan_8x8( h->dct.luma8x8[i8], dct8x8 );
767 nnz8x8 = 4 <= x264_mb_decimate_score( h->dct.luma8x8[i8], 64 );
769 nnz8x8 = array_non_zero( dct8x8 );
773 h->quantf.dequant_8x8( dct8x8, h->dequant8_mf[CQM_8PY], i_qp );
774 h->dctf.add8x8_idct8( p_fdec, dct8x8 );
780 DECLARE_ALIGNED_16( int16_t dct4x4[4][4][4] );
781 h->dctf.sub8x8_dct( dct4x4, p_fenc, p_fdec );
782 for( i4 = 0; i4 < 4; i4++ )
783 h->quantf.quant_4x4( dct4x4[i4], h->quant4_mf[CQM_4PY][i_qp], h->quant4_bias[CQM_4PY][i_qp] );
784 for( i4 = 0; i4 < 4; i4++ )
785 h->zigzagf.scan_4x4( h->dct.luma4x4[i8*4+i4], dct4x4[i4] );
789 int i_decimate_8x8 = 0;
790 for( i4 = 0; i4 < 4 && i_decimate_8x8 < 4; i4++ )
791 i_decimate_8x8 += x264_mb_decimate_score( h->dct.luma4x4[i8*4+i4], 16 );
792 nnz8x8 = 4 <= i_decimate_8x8;
795 nnz8x8 = array_non_zero( dct4x4 );
799 for( i4 = 0; i4 < 4; i4++ )
800 h->quantf.dequant_4x4( dct4x4[i4], h->dequant4_mf[CQM_4PY], i_qp );
801 h->dctf.add8x8_idct( p_fdec, dct4x4 );
805 i_qp = h->mb.i_chroma_qp;
807 for( ch = 0; ch < 2; ch++ )
809 DECLARE_ALIGNED_16( int16_t dct4x4[4][4] );
810 p_fenc = h->mb.pic.p_fenc[1+ch] + (i8&1)*4 + (i8>>1)*4*FENC_STRIDE;
811 p_fdec = h->mb.pic.p_fdec[1+ch] + (i8&1)*4 + (i8>>1)*4*FDEC_STRIDE;
813 h->dctf.sub4x4_dct( dct4x4, p_fenc, p_fdec );
814 h->quantf.quant_4x4( dct4x4, h->quant4_mf[CQM_4PC][i_qp], h->quant4_bias[CQM_4PC][i_qp] );
815 h->zigzagf.scan_4x4( h->dct.luma4x4[16+i8+ch*4], dct4x4 );
816 if( array_non_zero( dct4x4 ) )
818 h->quantf.dequant_4x4( dct4x4, h->dequant4_mf[CQM_4PC], i_qp );
819 h->dctf.add4x4_idct( p_fdec, dct4x4 );
823 h->mb.i_cbp_luma &= ~(1 << i8);
824 h->mb.i_cbp_luma |= nnz8x8 << i8;
825 h->mb.i_cbp_chroma = 0x02;