+ int nz, nz_dc;
+ int b_decimate = b_inter && h->mb.b_dct_decimate;
+ int (*dequant_mf)[16] = h->dequant4_mf[CQM_4IC + b_inter];
+ ALIGNED_ARRAY_16( dctcoef, dct_dc,[8] );
+ h->mb.i_cbp_chroma = 0;
+ h->nr_count[2] += h->mb.b_noise_reduction * 4;
+
+ M16( &h->mb.cache.non_zero_count[x264_scan8[16]] ) = 0;
+ M16( &h->mb.cache.non_zero_count[x264_scan8[18]] ) = 0;
+ M16( &h->mb.cache.non_zero_count[x264_scan8[32]] ) = 0;
+ M16( &h->mb.cache.non_zero_count[x264_scan8[34]] ) = 0;
+ if( chroma422 )
+ {
+ M16( &h->mb.cache.non_zero_count[x264_scan8[24]] ) = 0;
+ M16( &h->mb.cache.non_zero_count[x264_scan8[26]] ) = 0;
+ M16( &h->mb.cache.non_zero_count[x264_scan8[40]] ) = 0;
+ M16( &h->mb.cache.non_zero_count[x264_scan8[42]] ) = 0;
+ }
+
+ /* Early termination: check variance of chroma residual before encoding.
+ * Don't bother trying early termination at low QPs.
+ * Values are experimentally derived. */
+ if( b_decimate && i_qp >= (h->mb.b_trellis ? 12 : 18) && !h->mb.b_noise_reduction )
+ {
+ int thresh = chroma422 ? (x264_lambda2_tab[i_qp] + 16) >> 5 : (x264_lambda2_tab[i_qp] + 32) >> 6;
+ int ssd[2];
+ int chromapix = chroma422 ? PIXEL_8x16 : PIXEL_8x8;
+
+ int score = h->pixf.var2[chromapix]( h->mb.pic.p_fenc[1], FENC_STRIDE, h->mb.pic.p_fdec[1], FDEC_STRIDE, &ssd[0] );
+ if( score < thresh*4 )
+ score += h->pixf.var2[chromapix]( h->mb.pic.p_fenc[2], FENC_STRIDE, h->mb.pic.p_fdec[2], FDEC_STRIDE, &ssd[1] );
+ if( score < thresh*4 )
+ {
+ h->mb.cache.non_zero_count[x264_scan8[CHROMA_DC+0]] = 0;
+ h->mb.cache.non_zero_count[x264_scan8[CHROMA_DC+1]] = 0;
+
+ for( int ch = 0; ch < 2; ch++ )
+ {
+ if( ssd[ch] > thresh )
+ {
+ pixel *p_src = h->mb.pic.p_fenc[1+ch];
+ pixel *p_dst = h->mb.pic.p_fdec[1+ch];
+
+ if( chroma422 )
+ /* Cannot be replaced by two calls to sub8x8_dct_dc since the hadamard transform is different */
+ h->dctf.sub8x16_dct_dc( dct_dc, p_src, p_dst );
+ else
+ h->dctf.sub8x8_dct_dc( dct_dc, p_src, p_dst );
+
+ if( h->mb.b_trellis )
+ nz_dc = x264_quant_chroma_dc_trellis( h, dct_dc, i_qp+3*chroma422, !b_inter, CHROMA_DC+ch );
+ else
+ {
+ nz_dc = 0;
+ for( int i = 0; i <= chroma422; i++ )
+ nz_dc |= h->quantf.quant_2x2_dc( &dct_dc[4*i], h->quant4_mf[CQM_4IC+b_inter][i_qp+3*chroma422][0] >> 1,
+ h->quant4_bias[CQM_4IC+b_inter][i_qp+3*chroma422][0] << 1 );
+ }
+
+ if( nz_dc )
+ {
+ if( !x264_mb_optimize_chroma_dc( h, dct_dc, dequant_mf, i_qp+3*chroma422, chroma422 ) )
+ continue;
+ h->mb.cache.non_zero_count[x264_scan8[CHROMA_DC+ch]] = 1;
+ if( chroma422 )
+ {
+ zigzag_scan_2x4_dc( h->dct.chroma_dc[ch], dct_dc );
+ h->quantf.idct_dequant_2x4_dconly( dct_dc, dequant_mf, i_qp+3 );
+ }
+ else
+ {
+ zigzag_scan_2x2_dc( h->dct.chroma_dc[ch], dct_dc );
+ idct_dequant_2x2_dconly( dct_dc, dequant_mf, i_qp );
+ }
+
+ for( int i = 0; i <= chroma422; i++ )
+ h->dctf.add8x8_idct_dc( p_dst + 8*i*FDEC_STRIDE, &dct_dc[4*i] );
+ h->mb.i_cbp_chroma = 1;
+ }
+ }
+ }
+ return;
+ }
+ }