1 /*****************************************************************************
2 * macroblock.c: macroblock encoding
3 *****************************************************************************
4 * Copyright (C) 2003-2011 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.
24 * This program is also available under a commercial proprietary license.
25 * For more information, contact us at licensing@x264.com.
26 *****************************************************************************/
28 #include "common/common.h"
29 #include "macroblock.h"
31 /* These chroma DC functions don't have assembly versions and are only used here. */
33 #define ZIG(i,y,x) level[i] = dct[x*2+y];
34 static inline void zigzag_scan_2x2_dc( dctcoef level[4], dctcoef dct[4] )
43 #define IDCT_DEQUANT_START \
44 int d0 = dct[0] + dct[1]; \
45 int d1 = dct[2] + dct[3]; \
46 int d2 = dct[0] - dct[1]; \
47 int d3 = dct[2] - dct[3]; \
48 int dmf = dequant_mf[i_qp%6][0] << i_qp/6;
50 static inline void idct_dequant_2x2_dc( dctcoef dct[4], dctcoef dct4x4[4][16], int dequant_mf[6][16], int i_qp )
53 dct4x4[0][0] = (d0 + d1) * dmf >> 5;
54 dct4x4[1][0] = (d0 - d1) * dmf >> 5;
55 dct4x4[2][0] = (d2 + d3) * dmf >> 5;
56 dct4x4[3][0] = (d2 - d3) * dmf >> 5;
59 static inline void idct_dequant_2x2_dconly( dctcoef out[4], dctcoef dct[4], int dequant_mf[6][16], int i_qp )
62 out[0] = (d0 + d1) * dmf >> 5;
63 out[1] = (d0 - d1) * dmf >> 5;
64 out[2] = (d2 + d3) * dmf >> 5;
65 out[3] = (d2 - d3) * dmf >> 5;
68 static inline void dct2x2dc( dctcoef d[4], dctcoef dct4x4[4][16] )
70 int d0 = dct4x4[0][0] + dct4x4[1][0];
71 int d1 = dct4x4[2][0] + dct4x4[3][0];
72 int d2 = dct4x4[0][0] - dct4x4[1][0];
73 int d3 = dct4x4[2][0] - dct4x4[3][0];
84 static ALWAYS_INLINE int x264_quant_4x4( x264_t *h, dctcoef dct[16], int i_qp, int ctx_block_cat, int b_intra, int p, int idx )
86 int i_quant_cat = b_intra ? (p?CQM_4IC:CQM_4IY) : (p?CQM_4PC:CQM_4PY);
87 if( h->mb.b_noise_reduction )
88 h->quantf.denoise_dct( dct, h->nr_residual_sum[0+!!p*2], h->nr_offset[0+!!p*2], 16 );
90 return x264_quant_4x4_trellis( h, dct, i_quant_cat, i_qp, ctx_block_cat, b_intra, !!p, idx+p*16 );
92 return h->quantf.quant_4x4( dct, h->quant4_mf[i_quant_cat][i_qp], h->quant4_bias[i_quant_cat][i_qp] );
95 static ALWAYS_INLINE int x264_quant_8x8( x264_t *h, dctcoef dct[64], int i_qp, int ctx_block_cat, int b_intra, int p, int idx )
97 int i_quant_cat = b_intra ? (p?CQM_8IC:CQM_8IY) : (p?CQM_8PC:CQM_8PY);
98 if( h->mb.b_noise_reduction )
99 h->quantf.denoise_dct( dct, h->nr_residual_sum[1+!!p*2], h->nr_offset[1+!!p*2], 64 );
100 if( h->mb.b_trellis )
101 return x264_quant_8x8_trellis( h, dct, i_quant_cat, i_qp, ctx_block_cat, b_intra, !!p, idx+p*4 );
103 return h->quantf.quant_8x8( dct, h->quant8_mf[i_quant_cat][i_qp], h->quant8_bias[i_quant_cat][i_qp] );
106 /* All encoding functions must output the correct CBP and NNZ values.
107 * The entropy coding functions will check CBP first, then NNZ, before
108 * actually reading the DCT coefficients. NNZ still must be correct even
109 * if CBP is zero because of the use of NNZ values for context selection.
110 * "NNZ" need only be 0 or 1 rather than the exact coefficient count because
111 * that is only needed in CAVLC, and will be calculated by CAVLC's residual
112 * coding and stored as necessary. */
114 /* This means that decimation can be done merely by adjusting the CBP and NNZ
115 * rather than memsetting the coefficients. */
117 void x264_mb_encode_i4x4( x264_t *h, int p, int idx, int i_qp, int i_mode )
120 pixel *p_src = &h->mb.pic.p_fenc[p][block_idx_xy_fenc[idx]];
121 pixel *p_dst = &h->mb.pic.p_fdec[p][block_idx_xy_fdec[idx]];
122 ALIGNED_ARRAY_16( dctcoef, dct4x4,[16] );
124 if( h->mb.b_lossless )
125 x264_predict_lossless_4x4( h, p_dst, p, idx, i_mode );
127 h->predict_4x4[i_mode]( p_dst );
129 if( h->mb.b_lossless )
131 nz = h->zigzagf.sub_4x4( h->dct.luma4x4[p*16+idx], p_src, p_dst );
132 h->mb.cache.non_zero_count[x264_scan8[p*16+idx]] = nz;
133 h->mb.i_cbp_luma |= nz<<(idx>>2);
137 h->dctf.sub4x4_dct( dct4x4, p_src, p_dst );
139 nz = x264_quant_4x4( h, dct4x4, i_qp, ctx_cat_plane[DCT_LUMA_4x4][p], 1, p, idx );
140 h->mb.cache.non_zero_count[x264_scan8[p*16+idx]] = nz;
143 h->mb.i_cbp_luma |= 1<<(idx>>2);
144 h->zigzagf.scan_4x4( h->dct.luma4x4[p*16+idx], dct4x4 );
145 h->quantf.dequant_4x4( dct4x4, h->dequant4_mf[p?CQM_4IC:CQM_4IY], i_qp );
146 h->dctf.add4x4_idct( p_dst, dct4x4 );
150 #define STORE_8x8_NNZ( p, idx, nz )\
153 M16( &h->mb.cache.non_zero_count[x264_scan8[p*16+idx*4]+0] ) = (nz) * 0x0101;\
154 M16( &h->mb.cache.non_zero_count[x264_scan8[p*16+idx*4]+8] ) = (nz) * 0x0101;\
157 #define CLEAR_16x16_NNZ( p ) \
160 M32( &h->mb.cache.non_zero_count[x264_scan8[16*p+ 0]] ) = 0;\
161 M32( &h->mb.cache.non_zero_count[x264_scan8[16*p+ 2]] ) = 0;\
162 M32( &h->mb.cache.non_zero_count[x264_scan8[16*p+ 8]] ) = 0;\
163 M32( &h->mb.cache.non_zero_count[x264_scan8[16*p+10]] ) = 0;\
166 void x264_mb_encode_i8x8( x264_t *h, int p, int idx, int i_qp, int i_mode, pixel *edge )
171 pixel *p_src = &h->mb.pic.p_fenc[p][8*x + 8*y*FENC_STRIDE];
172 pixel *p_dst = &h->mb.pic.p_fdec[p][8*x + 8*y*FDEC_STRIDE];
173 ALIGNED_ARRAY_16( dctcoef, dct8x8,[64] );
174 ALIGNED_ARRAY_16( pixel, edge_buf,[33] );
178 h->predict_8x8_filter( p_dst, edge_buf, h->mb.i_neighbour8[idx], x264_pred_i4x4_neighbors[i_mode] );
182 if( h->mb.b_lossless )
183 x264_predict_lossless_8x8( h, p_dst, p, idx, i_mode, edge );
185 h->predict_8x8[i_mode]( p_dst, edge );
187 if( h->mb.b_lossless )
189 nz = h->zigzagf.sub_8x8( h->dct.luma8x8[p*4+idx], p_src, p_dst );
190 STORE_8x8_NNZ( p, idx, nz );
191 h->mb.i_cbp_luma |= nz<<idx;
195 h->dctf.sub8x8_dct8( dct8x8, p_src, p_dst );
197 nz = x264_quant_8x8( h, dct8x8, i_qp, ctx_cat_plane[DCT_LUMA_8x8][p], 1, p, idx );
200 h->mb.i_cbp_luma |= 1<<idx;
201 h->zigzagf.scan_8x8( h->dct.luma8x8[p*4+idx], dct8x8 );
202 h->quantf.dequant_8x8( dct8x8, h->dequant8_mf[p?CQM_8IC:CQM_8IY], i_qp );
203 h->dctf.add8x8_idct8( p_dst, dct8x8 );
204 STORE_8x8_NNZ( p, idx, 1 );
207 STORE_8x8_NNZ( p, idx, 0 );
210 static void x264_mb_encode_i16x16( x264_t *h, int p, int i_qp )
212 pixel *p_src = h->mb.pic.p_fenc[p];
213 pixel *p_dst = h->mb.pic.p_fdec[p];
215 ALIGNED_ARRAY_16( dctcoef, dct4x4,[16],[16] );
216 ALIGNED_ARRAY_16( dctcoef, dct_dc4x4,[16] );
218 int nz, block_cbp = 0;
219 int decimate_score = h->mb.b_dct_decimate ? 0 : 9;
220 int i_quant_cat = p ? CQM_4IC : CQM_4IY;
221 int i_mode = h->mb.i_intra16x16_pred_mode;
223 if( h->mb.b_lossless )
224 x264_predict_lossless_16x16( h, p, i_mode );
226 h->predict_16x16[i_mode]( h->mb.pic.p_fdec[p] );
228 if( h->mb.b_lossless )
230 for( int i = 0; i < 16; i++ )
232 int oe = block_idx_xy_fenc[i];
233 int od = block_idx_xy_fdec[i];
234 nz = h->zigzagf.sub_4x4ac( h->dct.luma4x4[16*p+i], p_src+oe, p_dst+od, &dct_dc4x4[block_idx_yx_1d[i]] );
235 h->mb.cache.non_zero_count[x264_scan8[16*p+i]] = nz;
238 h->mb.i_cbp_luma |= block_cbp * 0xf;
239 h->mb.cache.non_zero_count[x264_scan8[LUMA_DC+p]] = array_non_zero( dct_dc4x4 );
240 h->zigzagf.scan_4x4( h->dct.luma16x16_dc[p], dct_dc4x4 );
244 h->dctf.sub16x16_dct( dct4x4, p_src, p_dst );
246 for( int i = 0; i < 16; i++ )
249 if( h->mb.b_noise_reduction )
250 h->quantf.denoise_dct( dct4x4[i], h->nr_residual_sum[0], h->nr_offset[0], 16 );
251 dct_dc4x4[block_idx_xy_1d[i]] = dct4x4[i][0];
254 /* quant/scan/dequant */
255 if( h->mb.b_trellis )
256 nz = x264_quant_4x4_trellis( h, dct4x4[i], i_quant_cat, i_qp, ctx_cat_plane[DCT_LUMA_AC][p], 1, !!p, i );
258 nz = h->quantf.quant_4x4( dct4x4[i], h->quant4_mf[i_quant_cat][i_qp], h->quant4_bias[i_quant_cat][i_qp] );
259 h->mb.cache.non_zero_count[x264_scan8[16*p+i]] = nz;
262 h->zigzagf.scan_4x4( h->dct.luma4x4[16*p+i], dct4x4[i] );
263 h->quantf.dequant_4x4( dct4x4[i], h->dequant4_mf[i_quant_cat], i_qp );
264 if( decimate_score < 6 ) decimate_score += h->quantf.decimate_score15( h->dct.luma4x4[16*p+i] );
269 /* Writing the 16 CBFs in an i16x16 block is quite costly, so decimation can save many bits. */
270 /* More useful with CAVLC, but still useful with CABAC. */
271 if( decimate_score < 6 )
273 CLEAR_16x16_NNZ( p );
277 h->mb.i_cbp_luma |= block_cbp;
279 h->dctf.dct4x4dc( dct_dc4x4 );
280 if( h->mb.b_trellis )
281 nz = x264_quant_dc_trellis( h, dct_dc4x4, i_quant_cat, i_qp, ctx_cat_plane[DCT_LUMA_DC][p], 1, 0, LUMA_DC+p );
283 nz = h->quantf.quant_4x4_dc( dct_dc4x4, h->quant4_mf[i_quant_cat][i_qp][0]>>1, h->quant4_bias[i_quant_cat][i_qp][0]<<1 );
285 h->mb.cache.non_zero_count[x264_scan8[LUMA_DC+p]] = nz;
288 h->zigzagf.scan_4x4( h->dct.luma16x16_dc[p], dct_dc4x4 );
290 /* output samples to fdec */
291 h->dctf.idct4x4dc( dct_dc4x4 );
292 h->quantf.dequant_4x4_dc( dct_dc4x4, h->dequant4_mf[i_quant_cat], i_qp ); /* XXX not inversed */
294 for( int i = 0; i < 16; i++ )
295 dct4x4[i][0] = dct_dc4x4[block_idx_xy_1d[i]];
298 /* put pixels to fdec */
300 h->dctf.add16x16_idct( p_dst, dct4x4 );
302 h->dctf.add16x16_idct_dc( p_dst, dct_dc4x4 );
305 /* Round down coefficients losslessly in DC-only chroma blocks.
306 * Unlike luma blocks, this can't be done with a lookup table or
307 * other shortcut technique because of the interdependencies
308 * between the coefficients due to the chroma DC transform. */
309 static ALWAYS_INLINE int x264_mb_optimize_chroma_dc( x264_t *h, dctcoef dct2x2[4], int dequant_mf[6][16], int i_qp )
311 int dmf = dequant_mf[i_qp%6][0] << i_qp/6;
313 /* If the QP is too high, there's no benefit to rounding optimization. */
317 return h->quantf.optimize_chroma_dc( dct2x2, dmf );
320 void x264_mb_encode_8x8_chroma( x264_t *h, int b_inter, int i_qp )
323 int b_decimate = b_inter && h->mb.b_dct_decimate;
324 ALIGNED_ARRAY_16( dctcoef, dct2x2,[4] );
325 h->mb.i_cbp_chroma = 0;
326 h->nr_count[2] += h->mb.b_noise_reduction * 4;
328 /* Early termination: check variance of chroma residual before encoding.
329 * Don't bother trying early termination at low QPs.
330 * Values are experimentally derived. */
331 if( b_decimate && i_qp >= (h->mb.b_trellis ? 12 : 18) && !h->mb.b_noise_reduction )
333 int thresh = (x264_lambda2_tab[i_qp] + 32) >> 6;
335 int score = h->pixf.var2_8x8( h->mb.pic.p_fenc[1], FENC_STRIDE, h->mb.pic.p_fdec[1], FDEC_STRIDE, &ssd[0] );
336 if( score < thresh*4 )
337 score += h->pixf.var2_8x8( h->mb.pic.p_fenc[2], FENC_STRIDE, h->mb.pic.p_fdec[2], FDEC_STRIDE, &ssd[1] );
338 if( score < thresh*4 )
340 M16( &h->mb.cache.non_zero_count[x264_scan8[16]] ) = 0;
341 M16( &h->mb.cache.non_zero_count[x264_scan8[18]] ) = 0;
342 M16( &h->mb.cache.non_zero_count[x264_scan8[32]] ) = 0;
343 M16( &h->mb.cache.non_zero_count[x264_scan8[34]] ) = 0;
344 h->mb.cache.non_zero_count[x264_scan8[CHROMA_DC+0]] = 0;
345 h->mb.cache.non_zero_count[x264_scan8[CHROMA_DC+1]] = 0;
347 for( int ch = 0; ch < 2; ch++ )
349 if( ssd[ch] > thresh )
351 h->dctf.sub8x8_dct_dc( dct2x2, h->mb.pic.p_fenc[1+ch], h->mb.pic.p_fdec[1+ch] );
352 if( h->mb.b_trellis )
353 nz_dc = x264_quant_dc_trellis( h, dct2x2, CQM_4IC+b_inter, i_qp, DCT_CHROMA_DC, !b_inter, 1, CHROMA_DC+ch );
355 nz_dc = 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 );
359 if( !x264_mb_optimize_chroma_dc( h, dct2x2, h->dequant4_mf[CQM_4IC + b_inter], i_qp ) )
361 h->mb.cache.non_zero_count[x264_scan8[CHROMA_DC+ch]] = 1;
362 zigzag_scan_2x2_dc( h->dct.chroma_dc[ch], dct2x2 );
363 idct_dequant_2x2_dconly( dct2x2, dct2x2, h->dequant4_mf[CQM_4IC + b_inter], i_qp );
364 h->dctf.add8x8_idct_dc( h->mb.pic.p_fdec[1+ch], dct2x2 );
365 h->mb.i_cbp_chroma = 1;
373 for( int ch = 0; ch < 2; ch++ )
375 pixel *p_src = h->mb.pic.p_fenc[1+ch];
376 pixel *p_dst = h->mb.pic.p_fdec[1+ch];
377 int i_decimate_score = 0;
380 ALIGNED_ARRAY_16( dctcoef, dct4x4,[4],[16] );
382 if( h->mb.b_lossless )
384 for( int i = 0; i < 4; i++ )
386 int oe = block_idx_x[i]*4 + block_idx_y[i]*4*FENC_STRIDE;
387 int od = block_idx_x[i]*4 + block_idx_y[i]*4*FDEC_STRIDE;
388 nz = h->zigzagf.sub_4x4ac( h->dct.luma4x4[16+i+ch*16], p_src+oe, p_dst+od, &h->dct.chroma_dc[ch][i] );
389 h->mb.cache.non_zero_count[x264_scan8[16+i+ch*16]] = nz;
390 h->mb.i_cbp_chroma |= nz;
392 h->mb.cache.non_zero_count[x264_scan8[CHROMA_DC+ch]] = array_non_zero( h->dct.chroma_dc[ch] );
396 h->dctf.sub8x8_dct( dct4x4, p_src, p_dst );
397 if( h->mb.b_noise_reduction )
398 for( int i = 0; i < 4; i++ )
399 h->quantf.denoise_dct( dct4x4[i], h->nr_residual_sum[2], h->nr_offset[2], 16 );
400 dct2x2dc( dct2x2, dct4x4 );
401 /* calculate dct coeffs */
402 for( int i = 0; i < 4; i++ )
404 if( h->mb.b_trellis )
405 nz = x264_quant_4x4_trellis( h, dct4x4[i], CQM_4IC+b_inter, i_qp, DCT_CHROMA_AC, !b_inter, 1, 0 );
407 nz = h->quantf.quant_4x4( dct4x4[i], h->quant4_mf[CQM_4IC+b_inter][i_qp], h->quant4_bias[CQM_4IC+b_inter][i_qp] );
408 h->mb.cache.non_zero_count[x264_scan8[16+i+ch*16]] = nz;
412 h->zigzagf.scan_4x4( h->dct.luma4x4[16+i+ch*16], dct4x4[i] );
413 h->quantf.dequant_4x4( dct4x4[i], h->dequant4_mf[CQM_4IC + b_inter], i_qp );
415 i_decimate_score += h->quantf.decimate_score15( h->dct.luma4x4[16+i+ch*16] );
419 if( h->mb.b_trellis )
420 nz_dc = x264_quant_dc_trellis( h, dct2x2, CQM_4IC+b_inter, i_qp, DCT_CHROMA_DC, !b_inter, 1, CHROMA_DC+ch );
422 nz_dc = 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 );
424 h->mb.cache.non_zero_count[x264_scan8[CHROMA_DC+ch]] = nz_dc;
426 if( (b_decimate && i_decimate_score < 7) || !nz_ac )
428 /* Decimate the block */
429 M16( &h->mb.cache.non_zero_count[x264_scan8[16+0+16*ch]] ) = 0;
430 M16( &h->mb.cache.non_zero_count[x264_scan8[16+2+16*ch]] ) = 0;
431 if( !nz_dc ) /* Whole block is empty */
433 if( !x264_mb_optimize_chroma_dc( h, dct2x2, h->dequant4_mf[CQM_4IC + b_inter], i_qp ) )
435 h->mb.cache.non_zero_count[x264_scan8[CHROMA_DC+ch]] = 0;
439 zigzag_scan_2x2_dc( h->dct.chroma_dc[ch], dct2x2 );
440 idct_dequant_2x2_dconly( dct2x2, dct2x2, h->dequant4_mf[CQM_4IC + b_inter], i_qp );
441 h->dctf.add8x8_idct_dc( p_dst, dct2x2 );
445 h->mb.i_cbp_chroma = 1;
448 zigzag_scan_2x2_dc( h->dct.chroma_dc[ch], dct2x2 );
449 idct_dequant_2x2_dc( dct2x2, dct4x4, h->dequant4_mf[CQM_4IC + b_inter], i_qp );
451 h->dctf.add8x8_idct( p_dst, dct4x4 );
455 /* 0 = none, 1 = DC only, 2 = DC+AC */
456 h->mb.i_cbp_chroma += (h->mb.cache.non_zero_count[x264_scan8[CHROMA_DC+0]] |
457 h->mb.cache.non_zero_count[x264_scan8[CHROMA_DC+1]] | h->mb.i_cbp_chroma);
460 static void x264_macroblock_encode_skip( x264_t *h )
462 M32( &h->mb.cache.non_zero_count[x264_scan8[ 0]] ) = 0;
463 M32( &h->mb.cache.non_zero_count[x264_scan8[ 2]] ) = 0;
464 M32( &h->mb.cache.non_zero_count[x264_scan8[ 8]] ) = 0;
465 M32( &h->mb.cache.non_zero_count[x264_scan8[10]] ) = 0;
466 M32( &h->mb.cache.non_zero_count[x264_scan8[16+ 0]] ) = 0;
467 M32( &h->mb.cache.non_zero_count[x264_scan8[16+ 2]] ) = 0;
468 M32( &h->mb.cache.non_zero_count[x264_scan8[32+ 0]] ) = 0;
469 M32( &h->mb.cache.non_zero_count[x264_scan8[32+ 2]] ) = 0;
472 M32( &h->mb.cache.non_zero_count[x264_scan8[16+ 8]] ) = 0;
473 M32( &h->mb.cache.non_zero_count[x264_scan8[16+10]] ) = 0;
474 M32( &h->mb.cache.non_zero_count[x264_scan8[32+ 8]] ) = 0;
475 M32( &h->mb.cache.non_zero_count[x264_scan8[32+10]] ) = 0;
477 h->mb.i_cbp_luma = 0;
478 h->mb.i_cbp_chroma = 0;
479 h->mb.cbp[h->mb.i_mb_xy] = 0;
482 /*****************************************************************************
483 * Intra prediction for predictive lossless mode.
484 *****************************************************************************/
486 void x264_predict_lossless_8x8_chroma( x264_t *h, int i_mode )
488 if( i_mode == I_PRED_CHROMA_V )
490 h->mc.copy[PIXEL_8x8]( h->mb.pic.p_fdec[1], FDEC_STRIDE, h->mb.pic.p_fenc[1]-FENC_STRIDE, FENC_STRIDE, 8 );
491 h->mc.copy[PIXEL_8x8]( h->mb.pic.p_fdec[2], FDEC_STRIDE, h->mb.pic.p_fenc[2]-FENC_STRIDE, FENC_STRIDE, 8 );
492 memcpy( h->mb.pic.p_fdec[1], h->mb.pic.p_fdec[1]-FDEC_STRIDE, 8*sizeof(pixel) );
493 memcpy( h->mb.pic.p_fdec[2], h->mb.pic.p_fdec[2]-FDEC_STRIDE, 8*sizeof(pixel) );
495 else if( i_mode == I_PRED_CHROMA_H )
497 h->mc.copy[PIXEL_8x8]( h->mb.pic.p_fdec[1], FDEC_STRIDE, h->mb.pic.p_fenc[1]-1, FENC_STRIDE, 8 );
498 h->mc.copy[PIXEL_8x8]( h->mb.pic.p_fdec[2], FDEC_STRIDE, h->mb.pic.p_fenc[2]-1, FENC_STRIDE, 8 );
499 x264_copy_column8( h->mb.pic.p_fdec[1]+4*FDEC_STRIDE, h->mb.pic.p_fdec[1]+4*FDEC_STRIDE-1 );
500 x264_copy_column8( h->mb.pic.p_fdec[2]+4*FDEC_STRIDE, h->mb.pic.p_fdec[2]+4*FDEC_STRIDE-1 );
504 h->predict_8x8c[i_mode]( h->mb.pic.p_fdec[1] );
505 h->predict_8x8c[i_mode]( h->mb.pic.p_fdec[2] );
509 void x264_predict_lossless_4x4( x264_t *h, pixel *p_dst, int p, int idx, int i_mode )
511 int stride = h->fenc->i_stride[p] << MB_INTERLACED;
512 pixel *p_src = h->mb.pic.p_fenc_plane[p] + block_idx_x[idx]*4 + block_idx_y[idx]*4 * stride;
514 if( i_mode == I_PRED_4x4_V )
515 h->mc.copy[PIXEL_4x4]( p_dst, FDEC_STRIDE, p_src-stride, stride, 4 );
516 else if( i_mode == I_PRED_4x4_H )
517 h->mc.copy[PIXEL_4x4]( p_dst, FDEC_STRIDE, p_src-1, stride, 4 );
519 h->predict_4x4[i_mode]( p_dst );
522 void x264_predict_lossless_8x8( x264_t *h, pixel *p_dst, int p, int idx, int i_mode, pixel edge[33] )
524 int stride = h->fenc->i_stride[p] << MB_INTERLACED;
525 pixel *p_src = h->mb.pic.p_fenc_plane[p] + (idx&1)*8 + (idx>>1)*8*stride;
527 if( i_mode == I_PRED_8x8_V )
528 h->mc.copy[PIXEL_8x8]( p_dst, FDEC_STRIDE, p_src-stride, stride, 8 );
529 else if( i_mode == I_PRED_8x8_H )
530 h->mc.copy[PIXEL_8x8]( p_dst, FDEC_STRIDE, p_src-1, stride, 8 );
532 h->predict_8x8[i_mode]( p_dst, edge );
535 void x264_predict_lossless_16x16( x264_t *h, int p, int i_mode )
537 int stride = h->fenc->i_stride[p] << MB_INTERLACED;
538 if( i_mode == I_PRED_16x16_V )
539 h->mc.copy[PIXEL_16x16]( h->mb.pic.p_fdec[p], FDEC_STRIDE, h->mb.pic.p_fenc_plane[p]-stride, stride, 16 );
540 else if( i_mode == I_PRED_16x16_H )
541 h->mc.copy_16x16_unaligned( h->mb.pic.p_fdec[p], FDEC_STRIDE, h->mb.pic.p_fenc_plane[p]-1, stride, 16 );
543 h->predict_16x16[i_mode]( h->mb.pic.p_fdec[p] );
546 /*****************************************************************************
547 * x264_macroblock_encode:
548 *****************************************************************************/
549 static ALWAYS_INLINE void x264_macroblock_encode_internal( x264_t *h, int plane_count, int chroma )
551 int i_qp = h->mb.i_qp;
552 int b_decimate = h->mb.b_dct_decimate;
553 int b_force_no_skip = 0;
555 h->mb.i_cbp_luma = 0;
556 for( int p = 0; p < plane_count; p++ )
557 h->mb.cache.non_zero_count[x264_scan8[LUMA_DC+p]] = 0;
559 if( h->mb.i_type == I_PCM )
561 /* if PCM is chosen, we need to store reconstructed frame data */
562 for( int p = 0; p < plane_count; p++ )
563 h->mc.copy[PIXEL_16x16]( h->mb.pic.p_fdec[p], FDEC_STRIDE, h->mb.pic.p_fenc[p], FENC_STRIDE, 16 );
566 h->mc.copy[PIXEL_8x8] ( h->mb.pic.p_fdec[1], FDEC_STRIDE, h->mb.pic.p_fenc[1], FENC_STRIDE, 8 );
567 h->mc.copy[PIXEL_8x8] ( h->mb.pic.p_fdec[2], FDEC_STRIDE, h->mb.pic.p_fenc[2], FENC_STRIDE, 8 );
572 if( !h->mb.b_allow_skip )
575 if( IS_SKIP(h->mb.i_type) )
577 if( h->mb.i_type == P_SKIP )
579 else if( h->mb.i_type == B_SKIP )
580 h->mb.i_type = B_DIRECT;
584 if( h->mb.i_type == P_SKIP )
586 /* don't do pskip motion compensation if it was already done in macroblock_analyse */
587 if( !h->mb.b_skip_mc )
589 int mvx = x264_clip3( h->mb.cache.mv[0][x264_scan8[0]][0],
590 h->mb.mv_min[0], h->mb.mv_max[0] );
591 int mvy = x264_clip3( h->mb.cache.mv[0][x264_scan8[0]][1],
592 h->mb.mv_min[1], h->mb.mv_max[1] );
594 for( int p = 0; p < plane_count; p++ )
595 h->mc.mc_luma( h->mb.pic.p_fdec[p], FDEC_STRIDE,
596 &h->mb.pic.p_fref[0][0][p*4], h->mb.pic.i_stride[p],
597 mvx, mvy, 16, 16, &h->sh.weight[0][p] );
601 /* Special case for mv0, which is (of course) very common in P-skip mode. */
603 h->mc.mc_chroma( h->mb.pic.p_fdec[1], h->mb.pic.p_fdec[2], FDEC_STRIDE,
604 h->mb.pic.p_fref[0][0][4], h->mb.pic.i_stride[1],
607 h->mc.load_deinterleave_8x8x2_fdec( h->mb.pic.p_fdec[1], h->mb.pic.p_fref[0][0][4], h->mb.pic.i_stride[1] );
609 if( h->sh.weight[0][1].weightfn )
610 h->sh.weight[0][1].weightfn[8>>2]( h->mb.pic.p_fdec[1], FDEC_STRIDE,
611 h->mb.pic.p_fdec[1], FDEC_STRIDE,
612 &h->sh.weight[0][1], 8 );
613 if( h->sh.weight[0][2].weightfn )
614 h->sh.weight[0][2].weightfn[8>>2]( h->mb.pic.p_fdec[2], FDEC_STRIDE,
615 h->mb.pic.p_fdec[2], FDEC_STRIDE,
616 &h->sh.weight[0][2], 8 );
620 x264_macroblock_encode_skip( h );
623 if( h->mb.i_type == B_SKIP )
625 /* don't do bskip motion compensation if it was already done in macroblock_analyse */
626 if( !h->mb.b_skip_mc )
628 x264_macroblock_encode_skip( h );
632 if( h->mb.i_type == I_16x16 )
634 h->mb.b_transform_8x8 = 0;
636 for( int p = 0; p < plane_count; p++ )
638 x264_mb_encode_i16x16( h, p, i_qp );
639 i_qp = h->mb.i_chroma_qp;
642 else if( h->mb.i_type == I_8x8 )
644 h->mb.b_transform_8x8 = 1;
645 /* If we already encoded 3 of the 4 i8x8 blocks, we don't have to do them again. */
646 if( h->mb.i_skip_intra )
648 h->mc.copy[PIXEL_16x16]( h->mb.pic.p_fdec[0], FDEC_STRIDE, h->mb.pic.i8x8_fdec_buf, 16, 16 );
649 M32( &h->mb.cache.non_zero_count[x264_scan8[ 0]] ) = h->mb.pic.i8x8_nnz_buf[0];
650 M32( &h->mb.cache.non_zero_count[x264_scan8[ 2]] ) = h->mb.pic.i8x8_nnz_buf[1];
651 M32( &h->mb.cache.non_zero_count[x264_scan8[ 8]] ) = h->mb.pic.i8x8_nnz_buf[2];
652 M32( &h->mb.cache.non_zero_count[x264_scan8[10]] ) = h->mb.pic.i8x8_nnz_buf[3];
653 h->mb.i_cbp_luma = h->mb.pic.i8x8_cbp;
654 /* In RD mode, restore the now-overwritten DCT data. */
655 if( h->mb.i_skip_intra == 2 )
656 h->mc.memcpy_aligned( h->dct.luma8x8, h->mb.pic.i8x8_dct_buf, sizeof(h->mb.pic.i8x8_dct_buf) );
658 for( int p = 0; p < plane_count; p++ )
660 for( int i = (p == 0 && h->mb.i_skip_intra) ? 3 : 0 ; i < 4; i++ )
662 int i_mode = h->mb.cache.intra4x4_pred_mode[x264_scan8[4*i]];
663 x264_mb_encode_i8x8( h, p, i, i_qp, i_mode, NULL );
665 i_qp = h->mb.i_chroma_qp;
668 else if( h->mb.i_type == I_4x4 )
670 h->mb.b_transform_8x8 = 0;
671 /* If we already encoded 15 of the 16 i4x4 blocks, we don't have to do them again. */
672 if( h->mb.i_skip_intra )
674 h->mc.copy[PIXEL_16x16]( h->mb.pic.p_fdec[0], FDEC_STRIDE, h->mb.pic.i4x4_fdec_buf, 16, 16 );
675 M32( &h->mb.cache.non_zero_count[x264_scan8[ 0]] ) = h->mb.pic.i4x4_nnz_buf[0];
676 M32( &h->mb.cache.non_zero_count[x264_scan8[ 2]] ) = h->mb.pic.i4x4_nnz_buf[1];
677 M32( &h->mb.cache.non_zero_count[x264_scan8[ 8]] ) = h->mb.pic.i4x4_nnz_buf[2];
678 M32( &h->mb.cache.non_zero_count[x264_scan8[10]] ) = h->mb.pic.i4x4_nnz_buf[3];
679 h->mb.i_cbp_luma = h->mb.pic.i4x4_cbp;
680 /* In RD mode, restore the now-overwritten DCT data. */
681 if( h->mb.i_skip_intra == 2 )
682 h->mc.memcpy_aligned( h->dct.luma4x4, h->mb.pic.i4x4_dct_buf, sizeof(h->mb.pic.i4x4_dct_buf) );
684 for( int p = 0; p < plane_count; p++ )
686 for( int i = (p == 0 && h->mb.i_skip_intra) ? 15 : 0 ; i < 16; i++ )
688 pixel *p_dst = &h->mb.pic.p_fdec[p][block_idx_xy_fdec[i]];
689 int i_mode = h->mb.cache.intra4x4_pred_mode[x264_scan8[i]];
691 if( (h->mb.i_neighbour4[i] & (MB_TOPRIGHT|MB_TOP)) == MB_TOP )
692 /* emulate missing topright samples */
693 MPIXEL_X4( &p_dst[4-FDEC_STRIDE] ) = PIXEL_SPLAT_X4( p_dst[3-FDEC_STRIDE] );
695 x264_mb_encode_i4x4( h, p, i, i_qp, i_mode );
697 i_qp = h->mb.i_chroma_qp;
702 int i_decimate_mb = 0;
704 /* Don't repeat motion compensation if it was already done in non-RD transform analysis */
705 if( !h->mb.b_skip_mc )
708 if( h->mb.b_lossless )
710 if( h->mb.b_transform_8x8 )
711 for( int p = 0; p < plane_count; p++ )
712 for( int i8x8 = 0; i8x8 < 4; i8x8++ )
716 nz = h->zigzagf.sub_8x8( h->dct.luma8x8[p*4+i8x8], h->mb.pic.p_fenc[p] + 8*x + 8*y*FENC_STRIDE,
717 h->mb.pic.p_fdec[p] + 8*x + 8*y*FDEC_STRIDE );
718 STORE_8x8_NNZ( p, i8x8, nz );
719 h->mb.i_cbp_luma |= nz << i8x8;
722 for( int p = 0; p < plane_count; p++ )
723 for( int i4x4 = 0; i4x4 < 16; i4x4++ )
725 nz = h->zigzagf.sub_4x4( h->dct.luma4x4[p*16+i4x4],
726 h->mb.pic.p_fenc[p]+block_idx_xy_fenc[i4x4],
727 h->mb.pic.p_fdec[p]+block_idx_xy_fdec[i4x4] );
728 h->mb.cache.non_zero_count[x264_scan8[p*16+i4x4]] = nz;
729 h->mb.i_cbp_luma |= nz << (i4x4>>2);
732 else if( h->mb.b_transform_8x8 )
734 ALIGNED_ARRAY_16( dctcoef, dct8x8,[4],[64] );
735 b_decimate &= !h->mb.b_trellis || !h->param.b_cabac; // 8x8 trellis is inherently optimal decimation for CABAC
737 for( int p = 0; p < plane_count; p++ )
739 h->dctf.sub16x16_dct8( dct8x8, h->mb.pic.p_fenc[p], h->mb.pic.p_fdec[p] );
740 h->nr_count[1+!!p*2] += h->mb.b_noise_reduction * 4;
743 for( int idx = 0; idx < 4; idx++ )
745 nz = x264_quant_8x8( h, dct8x8[idx], i_qp, ctx_cat_plane[DCT_LUMA_8x8][p], 0, p, idx );
749 h->zigzagf.scan_8x8( h->dct.luma8x8[p*4+idx], dct8x8[idx] );
752 int i_decimate_8x8 = h->quantf.decimate_score64( h->dct.luma8x8[p*4+idx] );
753 i_decimate_mb += i_decimate_8x8;
754 if( i_decimate_8x8 >= 4 )
762 if( i_decimate_mb < 6 && b_decimate )
765 CLEAR_16x16_NNZ( p );
769 for( int idx = 0; idx < 4; idx++ )
774 if( plane_cbp&(1<<idx) )
776 h->quantf.dequant_8x8( dct8x8[idx], h->dequant8_mf[p?CQM_8PC:CQM_8PY], i_qp );
777 h->dctf.add8x8_idct8( &h->mb.pic.p_fdec[p][8*x + 8*y*FDEC_STRIDE], dct8x8[idx] );
778 STORE_8x8_NNZ( p, idx, 1 );
781 STORE_8x8_NNZ( p, idx, 0 );
784 h->mb.i_cbp_luma |= plane_cbp;
785 i_qp = h->mb.i_chroma_qp;
790 ALIGNED_ARRAY_16( dctcoef, dct4x4,[16],[16] );
791 for( int p = 0; p < plane_count; p++ )
793 h->dctf.sub16x16_dct( dct4x4, h->mb.pic.p_fenc[p], h->mb.pic.p_fdec[p] );
794 h->nr_count[0+!!p*2] += h->mb.b_noise_reduction * 16;
797 for( int i8x8 = 0; i8x8 < 4; i8x8++ )
799 int i_decimate_8x8 = 0;
802 /* encode one 4x4 block */
803 for( int i4x4 = 0; i4x4 < 4; i4x4++ )
805 int idx = i8x8 * 4 + i4x4;
807 nz = x264_quant_4x4( h, dct4x4[idx], i_qp, ctx_cat_plane[DCT_LUMA_4x4][p], 0, p, idx );
808 h->mb.cache.non_zero_count[x264_scan8[p*16+idx]] = nz;
812 h->zigzagf.scan_4x4( h->dct.luma4x4[p*16+idx], dct4x4[idx] );
813 h->quantf.dequant_4x4( dct4x4[idx], h->dequant4_mf[p?CQM_4PC:CQM_4PY], i_qp );
814 if( b_decimate && i_decimate_8x8 < 6 )
815 i_decimate_8x8 += h->quantf.decimate_score16( h->dct.luma4x4[p*16+idx] );
823 /* decimate this 8x8 block */
824 i_decimate_mb += i_decimate_8x8;
827 if( i_decimate_8x8 < 4 )
828 STORE_8x8_NNZ( p, i8x8, 0 );
830 plane_cbp |= 1<<i8x8;
834 h->dctf.add8x8_idct( &h->mb.pic.p_fdec[p][8*x + 8*y*FDEC_STRIDE], &dct4x4[i8x8*4] );
835 plane_cbp |= 1<<i8x8;
841 if( i_decimate_mb < 6 )
844 CLEAR_16x16_NNZ( p );
848 for( int i8x8 = 0; i8x8 < 4; i8x8++ )
849 if( plane_cbp&(1<<i8x8) )
850 h->dctf.add8x8_idct( &h->mb.pic.p_fdec[p][(i8x8&1)*8 + (i8x8>>1)*8*FDEC_STRIDE], &dct4x4[i8x8*4] );
853 h->mb.i_cbp_luma |= plane_cbp;
854 i_qp = h->mb.i_chroma_qp;
862 if( IS_INTRA( h->mb.i_type ) )
864 const int i_mode = h->mb.i_chroma_pred_mode;
865 if( h->mb.b_lossless )
866 x264_predict_lossless_8x8_chroma( h, i_mode );
869 h->predict_8x8c[i_mode]( h->mb.pic.p_fdec[1] );
870 h->predict_8x8c[i_mode]( h->mb.pic.p_fdec[2] );
874 /* encode the 8x8 blocks */
875 x264_mb_encode_8x8_chroma( h, !IS_INTRA( h->mb.i_type ), h->mb.i_chroma_qp );
878 h->mb.i_cbp_chroma = 0;
881 int cbp = h->mb.i_cbp_chroma << 4 | h->mb.i_cbp_luma;
882 if( h->param.b_cabac )
883 cbp |= h->mb.cache.non_zero_count[x264_scan8[LUMA_DC ]] << 8
884 | h->mb.cache.non_zero_count[x264_scan8[CHROMA_DC+0]] << 9
885 | h->mb.cache.non_zero_count[x264_scan8[CHROMA_DC+1]] << 10;
886 h->mb.cbp[h->mb.i_mb_xy] = cbp;
889 * XXX: in the me perhaps we should take x264_mb_predict_mv_pskip into account
890 * (if multiple mv give same result)*/
891 if( !b_force_no_skip )
893 if( h->mb.i_type == P_L0 && h->mb.i_partition == D_16x16 &&
894 !(h->mb.i_cbp_luma | h->mb.i_cbp_chroma) &&
895 M32( h->mb.cache.mv[0][x264_scan8[0]] ) == M32( h->mb.cache.pskip_mv )
896 && h->mb.cache.ref[0][x264_scan8[0]] == 0 )
898 h->mb.i_type = P_SKIP;
901 /* Check for B_SKIP */
902 if( h->mb.i_type == B_DIRECT && !(h->mb.i_cbp_luma | h->mb.i_cbp_chroma) )
904 h->mb.i_type = B_SKIP;
909 void x264_macroblock_encode( x264_t *h )
912 x264_macroblock_encode_internal( h, 3, 0 );
914 x264_macroblock_encode_internal( h, 1, 1 );
917 /*****************************************************************************
918 * x264_macroblock_probe_skip:
919 * Check if the current MB could be encoded as a [PB]_SKIP
920 *****************************************************************************/
921 static ALWAYS_INLINE int x264_macroblock_probe_skip_internal( x264_t *h, int b_bidir, int plane_count, int chroma )
923 ALIGNED_ARRAY_16( dctcoef, dct4x4,[4],[16] );
924 ALIGNED_ARRAY_16( dctcoef, dct2x2,[4] );
925 ALIGNED_ARRAY_16( dctcoef, dctscan,[16] );
926 ALIGNED_4( int16_t mvp[2] );
928 int i_qp = h->mb.i_qp;
931 for( int p = 0; p < plane_count; p++ )
933 int quant_cat = p ? CQM_4PC : CQM_4PY;
937 mvp[0] = x264_clip3( h->mb.cache.pskip_mv[0], h->mb.mv_min[0], h->mb.mv_max[0] );
938 mvp[1] = x264_clip3( h->mb.cache.pskip_mv[1], h->mb.mv_min[1], h->mb.mv_max[1] );
940 /* Motion compensation */
941 h->mc.mc_luma( h->mb.pic.p_fdec[p], FDEC_STRIDE,
942 &h->mb.pic.p_fref[0][0][p*4], h->mb.pic.i_stride[p],
943 mvp[0], mvp[1], 16, 16, &h->sh.weight[0][p] );
946 for( int i8x8 = 0, i_decimate_mb = 0; i8x8 < 4; i8x8++ )
948 int fenc_offset = (i8x8&1) * 8 + (i8x8>>1) * FENC_STRIDE * 8;
949 int fdec_offset = (i8x8&1) * 8 + (i8x8>>1) * FDEC_STRIDE * 8;
951 h->dctf.sub8x8_dct( dct4x4, h->mb.pic.p_fenc[p] + fenc_offset,
952 h->mb.pic.p_fdec[p] + fdec_offset );
953 /* encode one 4x4 block */
954 for( int i4x4 = 0; i4x4 < 4; i4x4++ )
956 if( h->mb.b_noise_reduction )
957 h->quantf.denoise_dct( dct4x4[i4x4], h->nr_residual_sum[0+!!p*2], h->nr_offset[0+!!p*2], 16 );
958 if( !h->quantf.quant_4x4( dct4x4[i4x4], h->quant4_mf[quant_cat][i_qp], h->quant4_bias[quant_cat][i_qp] ) )
960 h->zigzagf.scan_4x4( dctscan, dct4x4[i4x4] );
961 i_decimate_mb += h->quantf.decimate_score16( dctscan );
962 if( i_decimate_mb >= 6 )
966 i_qp = h->mb.i_chroma_qp;
972 i_qp = h->mb.i_chroma_qp;
973 thresh = (x264_lambda2_tab[i_qp] + 32) >> 6;
977 /* Special case for mv0, which is (of course) very common in P-skip mode. */
979 h->mc.mc_chroma( h->mb.pic.p_fdec[1], h->mb.pic.p_fdec[2], FDEC_STRIDE,
980 h->mb.pic.p_fref[0][0][4], h->mb.pic.i_stride[1],
981 mvp[0], mvp[1], 8, 8 );
983 h->mc.load_deinterleave_8x8x2_fdec( h->mb.pic.p_fdec[1], h->mb.pic.p_fref[0][0][4], h->mb.pic.i_stride[1] );
986 for( int ch = 0; ch < 2; ch++ )
988 pixel *p_src = h->mb.pic.p_fenc[1+ch];
989 pixel *p_dst = h->mb.pic.p_fdec[1+ch];
991 if( !b_bidir && h->sh.weight[0][1+ch].weightfn )
992 h->sh.weight[0][1+ch].weightfn[8>>2]( h->mb.pic.p_fdec[1+ch], FDEC_STRIDE,
993 h->mb.pic.p_fdec[1+ch], FDEC_STRIDE,
994 &h->sh.weight[0][1+ch], 8 );
996 /* there is almost never a termination during chroma, but we can't avoid the check entirely */
997 /* so instead we check SSD and skip the actual check if the score is low enough. */
998 ssd = h->pixf.ssd[PIXEL_8x8]( p_dst, FDEC_STRIDE, p_src, FENC_STRIDE );
1002 /* The vast majority of chroma checks will terminate during the DC check or the higher
1003 * threshold check, so we can save time by doing a DC-only DCT. */
1004 if( h->mb.b_noise_reduction )
1006 h->dctf.sub8x8_dct( dct4x4, p_src, p_dst );
1007 for( int i4x4 = 0; i4x4 < 4; i4x4++ )
1009 h->quantf.denoise_dct( dct4x4[i4x4], h->nr_residual_sum[2], h->nr_offset[2], 16 );
1010 dct2x2[i4x4] = dct4x4[i4x4][0];
1014 h->dctf.sub8x8_dct_dc( dct2x2, p_src, p_dst );
1016 if( h->quantf.quant_2x2_dc( dct2x2, h->quant4_mf[CQM_4PC][i_qp][0]>>1, h->quant4_bias[CQM_4PC][i_qp][0]<<1 ) )
1019 /* If there wasn't a termination in DC, we can check against a much higher threshold. */
1020 if( ssd < thresh*4 )
1023 if( !h->mb.b_noise_reduction )
1024 h->dctf.sub8x8_dct( dct4x4, p_src, p_dst );
1026 /* calculate dct coeffs */
1027 for( int i4x4 = 0, i_decimate_mb = 0; i4x4 < 4; i4x4++ )
1029 dct4x4[i4x4][0] = 0;
1030 if( h->mb.b_noise_reduction )
1031 h->quantf.denoise_dct( dct4x4[i4x4], h->nr_residual_sum[2], h->nr_offset[2], 16 );
1032 if( !h->quantf.quant_4x4( dct4x4[i4x4], h->quant4_mf[CQM_4PC][i_qp], h->quant4_bias[CQM_4PC][i_qp] ) )
1034 h->zigzagf.scan_4x4( dctscan, dct4x4[i4x4] );
1035 i_decimate_mb += h->quantf.decimate_score15( dctscan );
1036 if( i_decimate_mb >= 7 )
1042 h->mb.b_skip_mc = 1;
1046 int x264_macroblock_probe_skip( x264_t *h, int b_bidir )
1049 return x264_macroblock_probe_skip_internal( h, b_bidir, 3, 0 );
1051 return x264_macroblock_probe_skip_internal( h, b_bidir, 1, 1 );
1054 /****************************************************************************
1055 * DCT-domain noise reduction / adaptive deadzone
1057 ****************************************************************************/
1059 void x264_noise_reduction_update( x264_t *h )
1061 h->nr_offset = h->nr_offset_denoise;
1062 h->nr_residual_sum = h->nr_residual_sum_buf[0];
1063 h->nr_count = h->nr_count_buf[0];
1064 for( int cat = 0; cat < 3 + CHROMA444; cat++ )
1067 int size = dct8x8 ? 64 : 16;
1068 const uint16_t *weight = dct8x8 ? x264_dct8_weight2_tab : x264_dct4_weight2_tab;
1070 if( h->nr_count[cat] > (dct8x8 ? (1<<16) : (1<<18)) )
1072 for( int i = 0; i < size; i++ )
1073 h->nr_residual_sum[cat][i] >>= 1;
1074 h->nr_count[cat] >>= 1;
1077 for( int i = 0; i < size; i++ )
1078 h->nr_offset[cat][i] =
1079 ((uint64_t)h->param.analyse.i_noise_reduction * h->nr_count[cat]
1080 + h->nr_residual_sum[cat][i]/2)
1081 / ((uint64_t)h->nr_residual_sum[cat][i] * weight[i]/256 + 1);
1083 /* Don't denoise DC coefficients */
1084 h->nr_offset[cat][0] = 0;
1088 /*****************************************************************************
1089 * RD only; 4 calls to this do not make up for one macroblock_encode.
1090 * doesn't transform chroma dc.
1091 *****************************************************************************/
1092 static ALWAYS_INLINE void x264_macroblock_encode_p8x8_internal( x264_t *h, int i8, int plane_count, int chroma )
1094 int b_decimate = h->mb.b_dct_decimate;
1095 int i_qp = h->mb.i_qp;
1100 h->mb.i_cbp_chroma = 0;
1101 h->mb.i_cbp_luma &= ~(1 << i8);
1103 if( !h->mb.b_skip_mc )
1104 x264_mb_mc_8x8( h, i8 );
1106 if( h->mb.b_lossless )
1108 for( int p = 0; p < plane_count; p++ )
1110 pixel *p_fenc = h->mb.pic.p_fenc[p] + 8*x + 8*y*FENC_STRIDE;
1111 pixel *p_fdec = h->mb.pic.p_fdec[p] + 8*x + 8*y*FDEC_STRIDE;
1113 if( h->mb.b_transform_8x8 )
1115 nnz8x8 = h->zigzagf.sub_8x8( h->dct.luma8x8[4*p+i8], p_fenc, p_fdec );
1116 STORE_8x8_NNZ( p, i8, nnz8x8 );
1120 for( int i4 = i8*4; i4 < i8*4+4; i4++ )
1122 nz = h->zigzagf.sub_4x4( h->dct.luma4x4[16*p+i4],
1123 h->mb.pic.p_fenc[p]+block_idx_xy_fenc[i4],
1124 h->mb.pic.p_fdec[p]+block_idx_xy_fdec[i4] );
1125 h->mb.cache.non_zero_count[x264_scan8[16*p+i4]] = nz;
1129 h->mb.i_cbp_luma |= nnz8x8 << i8;
1133 for( int ch = 0; ch < 2; ch++ )
1136 pixel *p_fenc = h->mb.pic.p_fenc[1+ch] + 4*x + 4*y*FENC_STRIDE;
1137 pixel *p_fdec = h->mb.pic.p_fdec[1+ch] + 4*x + 4*y*FDEC_STRIDE;
1138 nz = h->zigzagf.sub_4x4ac( h->dct.luma4x4[16+i8+ch*4], p_fenc, p_fdec, &dc );
1139 h->mb.cache.non_zero_count[x264_scan8[16+i8+ch*4]] = nz;
1141 h->mb.i_cbp_chroma = 0x02;
1146 if( h->mb.b_transform_8x8 )
1148 for( int p = 0; p < plane_count; p++ )
1150 int quant_cat = p ? CQM_8PC : CQM_8PY;
1151 pixel *p_fenc = h->mb.pic.p_fenc[p] + 8*x + 8*y*FENC_STRIDE;
1152 pixel *p_fdec = h->mb.pic.p_fdec[p] + 8*x + 8*y*FDEC_STRIDE;
1153 ALIGNED_ARRAY_16( dctcoef, dct8x8,[64] );
1154 h->dctf.sub8x8_dct8( dct8x8, p_fenc, p_fdec );
1155 int nnz8x8 = x264_quant_8x8( h, dct8x8, i_qp, ctx_cat_plane[DCT_LUMA_8x8][p], 0, p, i8 );
1158 h->zigzagf.scan_8x8( h->dct.luma8x8[4*p+i8], dct8x8 );
1160 if( b_decimate && !h->mb.b_trellis )
1161 nnz8x8 = 4 <= h->quantf.decimate_score64( h->dct.luma8x8[4*p+i8] );
1165 h->quantf.dequant_8x8( dct8x8, h->dequant8_mf[quant_cat], i_qp );
1166 h->dctf.add8x8_idct8( p_fdec, dct8x8 );
1167 STORE_8x8_NNZ( p, i8, 1 );
1170 STORE_8x8_NNZ( p, i8, 0 );
1173 STORE_8x8_NNZ( p, i8, 0 );
1174 h->mb.i_cbp_luma |= nnz8x8 << i8;
1175 i_qp = h->mb.i_chroma_qp;
1180 for( int p = 0; p < plane_count; p++ )
1182 int quant_cat = p ? CQM_4PC : CQM_4PY;
1183 pixel *p_fenc = h->mb.pic.p_fenc[p] + 8*x + 8*y*FENC_STRIDE;
1184 pixel *p_fdec = h->mb.pic.p_fdec[p] + 8*x + 8*y*FDEC_STRIDE;
1185 int i_decimate_8x8 = 0, nnz8x8 = 0;
1186 ALIGNED_ARRAY_16( dctcoef, dct4x4,[4],[16] );
1187 h->dctf.sub8x8_dct( dct4x4, p_fenc, p_fdec );
1188 for( int i4 = 0; i4 < 4; i4++ )
1190 nz = x264_quant_4x4( h, dct4x4[i4], i_qp, ctx_cat_plane[DCT_LUMA_4x4][p], 0, p, 8*4+i4 );
1191 h->mb.cache.non_zero_count[x264_scan8[p*16+i8*4+i4]] = nz;
1194 h->zigzagf.scan_4x4( h->dct.luma4x4[p*16+i8*4+i4], dct4x4[i4] );
1195 h->quantf.dequant_4x4( dct4x4[i4], h->dequant4_mf[quant_cat], i_qp );
1197 i_decimate_8x8 += h->quantf.decimate_score16( h->dct.luma4x4[p*16+i8*4+i4] );
1202 if( b_decimate && i_decimate_8x8 < 4 )
1206 h->dctf.add8x8_idct( p_fdec, dct4x4 );
1208 STORE_8x8_NNZ( p, i8, 0 );
1210 h->mb.i_cbp_luma |= nnz8x8 << i8;
1211 i_qp = h->mb.i_chroma_qp;
1217 i_qp = h->mb.i_chroma_qp;
1218 for( int ch = 0; ch < 2; ch++ )
1220 ALIGNED_ARRAY_16( dctcoef, dct4x4,[16] );
1221 pixel *p_fenc = h->mb.pic.p_fenc[1+ch] + 4*x + 4*y*FENC_STRIDE;
1222 pixel *p_fdec = h->mb.pic.p_fdec[1+ch] + 4*x + 4*y*FDEC_STRIDE;
1223 h->dctf.sub4x4_dct( dct4x4, p_fenc, p_fdec );
1224 if( h->mb.b_noise_reduction )
1225 h->quantf.denoise_dct( dct4x4, h->nr_residual_sum[2], h->nr_offset[2], 16 );
1228 if( h->mb.b_trellis )
1229 nz = x264_quant_4x4_trellis( h, dct4x4, CQM_4PC, i_qp, DCT_CHROMA_AC, 0, 1, 0 );
1231 nz = h->quantf.quant_4x4( dct4x4, h->quant4_mf[CQM_4PC][i_qp], h->quant4_bias[CQM_4PC][i_qp] );
1233 h->mb.cache.non_zero_count[x264_scan8[16+i8+ch*16]] = nz;
1236 h->zigzagf.scan_4x4( h->dct.luma4x4[16+i8+ch*16], dct4x4 );
1237 h->quantf.dequant_4x4( dct4x4, h->dequant4_mf[CQM_4PC], i_qp );
1238 h->dctf.add4x4_idct( p_fdec, dct4x4 );
1241 h->mb.i_cbp_chroma = 0x02;
1246 void x264_macroblock_encode_p8x8( x264_t *h, int i8 )
1249 x264_macroblock_encode_p8x8_internal( h, i8, 3, 0 );
1251 x264_macroblock_encode_p8x8_internal( h, i8, 1, 1 );
1254 /*****************************************************************************
1255 * RD only, luma only (for 4:2:0)
1256 *****************************************************************************/
1257 static ALWAYS_INLINE void x264_macroblock_encode_p4x4_internal( x264_t *h, int i4, int plane_count )
1259 int i_qp = h->mb.i_qp;
1261 for( int p = 0; p < plane_count; p++ )
1263 int quant_cat = p ? CQM_4PC : CQM_4PY;
1264 pixel *p_fenc = &h->mb.pic.p_fenc[p][block_idx_xy_fenc[i4]];
1265 pixel *p_fdec = &h->mb.pic.p_fdec[p][block_idx_xy_fdec[i4]];
1268 /* Don't need motion compensation as this function is only used in qpel-RD, which caches pixel data. */
1270 if( h->mb.b_lossless )
1272 nz = h->zigzagf.sub_4x4( h->dct.luma4x4[p*16+i4], p_fenc, p_fdec );
1273 h->mb.cache.non_zero_count[x264_scan8[p*16+i4]] = nz;
1277 ALIGNED_ARRAY_16( dctcoef, dct4x4,[16] );
1278 h->dctf.sub4x4_dct( dct4x4, p_fenc, p_fdec );
1279 nz = x264_quant_4x4( h, dct4x4, i_qp, ctx_cat_plane[DCT_LUMA_4x4][p], 0, p, i4 );
1280 h->mb.cache.non_zero_count[x264_scan8[p*16+i4]] = nz;
1283 h->zigzagf.scan_4x4( h->dct.luma4x4[p*16+i4], dct4x4 );
1284 h->quantf.dequant_4x4( dct4x4, h->dequant4_mf[quant_cat], i_qp );
1285 h->dctf.add4x4_idct( p_fdec, dct4x4 );
1288 i_qp = h->mb.i_chroma_qp;
1292 void x264_macroblock_encode_p4x4( x264_t *h, int i8 )
1295 x264_macroblock_encode_p4x4_internal( h, i8, 3 );
1297 x264_macroblock_encode_p4x4_internal( h, i8, 1 );