]> git.sesse.net Git - x264/blob - encoder/macroblock.c
4:4:4 encoding support
[x264] / encoder / macroblock.c
1 /*****************************************************************************
2  * macroblock.c: macroblock encoding
3  *****************************************************************************
4  * Copyright (C) 2003-2011 x264 project
5  *
6  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
7  *          Loren Merritt <lorenm@u.washington.edu>
8  *          Fiona Glaser <fiona@x264.com>
9  *
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.
14  *
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.
19  *
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  *
24  * This program is also available under a commercial proprietary license.
25  * For more information, contact us at licensing@x264.com.
26  *****************************************************************************/
27
28 #include "common/common.h"
29 #include "macroblock.h"
30
31 /* These chroma DC functions don't have assembly versions and are only used here. */
32
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] )
35 {
36     ZIG(0,0,0)
37     ZIG(1,0,1)
38     ZIG(2,1,0)
39     ZIG(3,1,1)
40 }
41 #undef ZIG
42
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;
49
50 static inline void idct_dequant_2x2_dc( dctcoef dct[4], dctcoef dct4x4[4][16], int dequant_mf[6][16], int i_qp )
51 {
52     IDCT_DEQUANT_START
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;
57 }
58
59 static inline void idct_dequant_2x2_dconly( dctcoef out[4], dctcoef dct[4], int dequant_mf[6][16], int i_qp )
60 {
61     IDCT_DEQUANT_START
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;
66 }
67
68 static inline void dct2x2dc( dctcoef d[4], dctcoef dct4x4[4][16] )
69 {
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];
74     d[0] = d0 + d1;
75     d[2] = d2 + d3;
76     d[1] = d0 - d1;
77     d[3] = d2 - d3;
78     dct4x4[0][0] = 0;
79     dct4x4[1][0] = 0;
80     dct4x4[2][0] = 0;
81     dct4x4[3][0] = 0;
82 }
83
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 )
85 {
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 );
89     if( h->mb.b_trellis )
90         return x264_quant_4x4_trellis( h, dct, i_quant_cat, i_qp, ctx_block_cat, b_intra, !!p, idx+p*16 );
91     else
92         return h->quantf.quant_4x4( dct, h->quant4_mf[i_quant_cat][i_qp], h->quant4_bias[i_quant_cat][i_qp] );
93 }
94
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 )
96 {
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 );
102     else
103         return h->quantf.quant_8x8( dct, h->quant8_mf[i_quant_cat][i_qp], h->quant8_bias[i_quant_cat][i_qp] );
104 }
105
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. */
113
114 /* This means that decimation can be done merely by adjusting the CBP and NNZ
115  * rather than memsetting the coefficients. */
116
117 void x264_mb_encode_i4x4( x264_t *h, int p, int idx, int i_qp, int i_mode )
118 {
119     int nz;
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] );
123
124     if( h->mb.b_lossless )
125         x264_predict_lossless_4x4( h, p_dst, p, idx, i_mode );
126     else
127         h->predict_4x4[i_mode]( p_dst );
128
129     if( h->mb.b_lossless )
130     {
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);
134         return;
135     }
136
137     h->dctf.sub4x4_dct( dct4x4, p_src, p_dst );
138
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;
141     if( nz )
142     {
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 );
147     }
148 }
149
150 #define STORE_8x8_NNZ( p, idx, nz )\
151 do\
152 {\
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;\
155 } while(0)
156
157 #define CLEAR_16x16_NNZ( p ) \
158 do\
159 {\
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;\
164 } while(0)
165
166 void x264_mb_encode_i8x8( x264_t *h, int p, int idx, int i_qp, int i_mode, pixel *edge )
167 {
168     int x = idx&1;
169     int y = idx>>1;
170     int nz;
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] );
175
176     if( !edge )
177     {
178         h->predict_8x8_filter( p_dst, edge_buf, h->mb.i_neighbour8[idx], x264_pred_i4x4_neighbors[i_mode] );
179         edge = edge_buf;
180     }
181
182     if( h->mb.b_lossless )
183         x264_predict_lossless_8x8( h, p_dst, p, idx, i_mode, edge );
184     else
185         h->predict_8x8[i_mode]( p_dst, edge );
186
187     if( h->mb.b_lossless )
188     {
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;
192         return;
193     }
194
195     h->dctf.sub8x8_dct8( dct8x8, p_src, p_dst );
196
197     nz = x264_quant_8x8( h, dct8x8, i_qp, ctx_cat_plane[DCT_LUMA_8x8][p], 1, p, idx );
198     if( nz )
199     {
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 );
205     }
206     else
207         STORE_8x8_NNZ( p, idx, 0 );
208 }
209
210 static void x264_mb_encode_i16x16( x264_t *h, int p, int i_qp )
211 {
212     pixel *p_src = h->mb.pic.p_fenc[p];
213     pixel *p_dst = h->mb.pic.p_fdec[p];
214
215     ALIGNED_ARRAY_16( dctcoef, dct4x4,[16],[16] );
216     ALIGNED_ARRAY_16( dctcoef, dct_dc4x4,[16] );
217
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;
222
223     if( h->mb.b_lossless )
224         x264_predict_lossless_16x16( h, p, i_mode );
225     else
226         h->predict_16x16[i_mode]( h->mb.pic.p_fdec[p] );
227
228     if( h->mb.b_lossless )
229     {
230         for( int i = 0; i < 16; i++ )
231         {
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;
236             block_cbp |= nz;
237         }
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 );
241         return;
242     }
243
244     h->dctf.sub16x16_dct( dct4x4, p_src, p_dst );
245
246     for( int i = 0; i < 16; i++ )
247     {
248         /* copy dc coeff */
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];
252         dct4x4[i][0] = 0;
253
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 );
257         else
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;
260         if( nz )
261         {
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] );
265             block_cbp = 0xf;
266         }
267     }
268
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 )
272     {
273         CLEAR_16x16_NNZ( p );
274         block_cbp = 0;
275     }
276     else
277         h->mb.i_cbp_luma |= block_cbp;
278
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 );
282     else
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 );
284
285     h->mb.cache.non_zero_count[x264_scan8[LUMA_DC+p]] = nz;
286     if( nz )
287     {
288         h->zigzagf.scan_4x4( h->dct.luma16x16_dc[p], dct_dc4x4 );
289
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 */
293         if( block_cbp )
294             for( int i = 0; i < 16; i++ )
295                 dct4x4[i][0] = dct_dc4x4[block_idx_xy_1d[i]];
296     }
297
298     /* put pixels to fdec */
299     if( block_cbp )
300         h->dctf.add16x16_idct( p_dst, dct4x4 );
301     else if( nz )
302         h->dctf.add16x16_idct_dc( p_dst, dct_dc4x4 );
303 }
304
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 )
310 {
311     int dmf = dequant_mf[i_qp%6][0] << i_qp/6;
312
313     /* If the QP is too high, there's no benefit to rounding optimization. */
314     if( dmf > 32*64 )
315         return 1;
316
317     return h->quantf.optimize_chroma_dc( dct2x2, dmf );
318 }
319
320 void x264_mb_encode_8x8_chroma( x264_t *h, int b_inter, int i_qp )
321 {
322     int nz, nz_dc;
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;
327
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 )
332     {
333         int thresh = (x264_lambda2_tab[i_qp] + 32) >> 6;
334         int ssd[2];
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 )
339         {
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;
346
347             for( int ch = 0; ch < 2; ch++ )
348             {
349                 if( ssd[ch] > thresh )
350                 {
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 );
354                     else
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 );
356
357                     if( nz_dc )
358                     {
359                         if( !x264_mb_optimize_chroma_dc( h, dct2x2, h->dequant4_mf[CQM_4IC + b_inter], i_qp ) )
360                             continue;
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;
366                     }
367                 }
368             }
369             return;
370         }
371     }
372
373     for( int ch = 0; ch < 2; ch++ )
374     {
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;
378         int nz_ac = 0;
379
380         ALIGNED_ARRAY_16( dctcoef, dct4x4,[4],[16] );
381
382         if( h->mb.b_lossless )
383         {
384             for( int i = 0; i < 4; i++ )
385             {
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;
391             }
392             h->mb.cache.non_zero_count[x264_scan8[CHROMA_DC+ch]] = array_non_zero( h->dct.chroma_dc[ch] );
393             continue;
394         }
395
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++ )
403         {
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 );
406             else
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;
409             if( nz )
410             {
411                 nz_ac = 1;
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 );
414                 if( b_decimate )
415                     i_decimate_score += h->quantf.decimate_score15( h->dct.luma4x4[16+i+ch*16] );
416             }
417         }
418
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 );
421         else
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 );
423
424         h->mb.cache.non_zero_count[x264_scan8[CHROMA_DC+ch]] = nz_dc;
425
426         if( (b_decimate && i_decimate_score < 7) || !nz_ac )
427         {
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 */
432                 continue;
433             if( !x264_mb_optimize_chroma_dc( h, dct2x2, h->dequant4_mf[CQM_4IC + b_inter], i_qp ) )
434             {
435                 h->mb.cache.non_zero_count[x264_scan8[CHROMA_DC+ch]] = 0;
436                 continue;
437             }
438             /* DC-only */
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 );
442         }
443         else
444         {
445             h->mb.i_cbp_chroma = 1;
446             if( nz_dc )
447             {
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 );
450             }
451             h->dctf.add8x8_idct( p_dst, dct4x4 );
452         }
453     }
454
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);
458 }
459
460 static void x264_macroblock_encode_skip( x264_t *h )
461 {
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;
470     if( CHROMA444 )
471     {
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;
476     }
477     h->mb.i_cbp_luma = 0;
478     h->mb.i_cbp_chroma = 0;
479     h->mb.cbp[h->mb.i_mb_xy] = 0;
480 }
481
482 /*****************************************************************************
483  * Intra prediction for predictive lossless mode.
484  *****************************************************************************/
485
486 void x264_predict_lossless_8x8_chroma( x264_t *h, int i_mode )
487 {
488     if( i_mode == I_PRED_CHROMA_V )
489     {
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) );
494     }
495     else if( i_mode == I_PRED_CHROMA_H )
496     {
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 );
501     }
502     else
503     {
504         h->predict_8x8c[i_mode]( h->mb.pic.p_fdec[1] );
505         h->predict_8x8c[i_mode]( h->mb.pic.p_fdec[2] );
506     }
507 }
508
509 void x264_predict_lossless_4x4( x264_t *h, pixel *p_dst, int p, int idx, int i_mode )
510 {
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;
513
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 );
518     else
519         h->predict_4x4[i_mode]( p_dst );
520 }
521
522 void x264_predict_lossless_8x8( x264_t *h, pixel *p_dst, int p, int idx, int i_mode, pixel edge[33] )
523 {
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;
526
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 );
531     else
532         h->predict_8x8[i_mode]( p_dst, edge );
533 }
534
535 void x264_predict_lossless_16x16( x264_t *h, int p, int i_mode )
536 {
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 );
542     else
543         h->predict_16x16[i_mode]( h->mb.pic.p_fdec[p] );
544 }
545
546 /*****************************************************************************
547  * x264_macroblock_encode:
548  *****************************************************************************/
549 static ALWAYS_INLINE void x264_macroblock_encode_internal( x264_t *h, int plane_count, int chroma )
550 {
551     int i_qp = h->mb.i_qp;
552     int b_decimate = h->mb.b_dct_decimate;
553     int b_force_no_skip = 0;
554     int nz;
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;
558
559     if( h->mb.i_type == I_PCM )
560     {
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 );
564         if( chroma )
565         {
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 );
568         }
569         return;
570     }
571
572     if( !h->mb.b_allow_skip )
573     {
574         b_force_no_skip = 1;
575         if( IS_SKIP(h->mb.i_type) )
576         {
577             if( h->mb.i_type == P_SKIP )
578                 h->mb.i_type = P_L0;
579             else if( h->mb.i_type == B_SKIP )
580                 h->mb.i_type = B_DIRECT;
581         }
582     }
583
584     if( h->mb.i_type == P_SKIP )
585     {
586         /* don't do pskip motion compensation if it was already done in macroblock_analyse */
587         if( !h->mb.b_skip_mc )
588         {
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] );
593
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] );
598
599             if( chroma )
600             {
601                 /* Special case for mv0, which is (of course) very common in P-skip mode. */
602                 if( mvx | mvy )
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],
605                                      mvx, mvy, 8, 8 );
606                 else
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] );
608
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 );
617             }
618         }
619
620         x264_macroblock_encode_skip( h );
621         return;
622     }
623     if( h->mb.i_type == B_SKIP )
624     {
625         /* don't do bskip motion compensation if it was already done in macroblock_analyse */
626         if( !h->mb.b_skip_mc )
627             x264_mb_mc( h );
628         x264_macroblock_encode_skip( h );
629         return;
630     }
631
632     if( h->mb.i_type == I_16x16 )
633     {
634         h->mb.b_transform_8x8 = 0;
635
636         for( int p = 0; p < plane_count; p++ )
637         {
638             x264_mb_encode_i16x16( h, p, i_qp );
639             i_qp = h->mb.i_chroma_qp;
640         }
641     }
642     else if( h->mb.i_type == I_8x8 )
643     {
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 )
647         {
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) );
657         }
658         for( int p = 0; p < plane_count; p++ )
659         {
660             for( int i = (p == 0 && h->mb.i_skip_intra) ? 3 : 0 ; i < 4; i++ )
661             {
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 );
664             }
665             i_qp = h->mb.i_chroma_qp;
666         }
667     }
668     else if( h->mb.i_type == I_4x4 )
669     {
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 )
673         {
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) );
683         }
684         for( int p = 0; p < plane_count; p++ )
685         {
686             for( int i = (p == 0 && h->mb.i_skip_intra) ? 15 : 0 ; i < 16; i++ )
687             {
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]];
690
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] );
694
695                 x264_mb_encode_i4x4( h, p, i, i_qp, i_mode );
696             }
697             i_qp = h->mb.i_chroma_qp;
698         }
699     }
700     else    /* Inter MB */
701     {
702         int i_decimate_mb = 0;
703
704         /* Don't repeat motion compensation if it was already done in non-RD transform analysis */
705         if( !h->mb.b_skip_mc )
706             x264_mb_mc( h );
707
708         if( h->mb.b_lossless )
709         {
710             if( h->mb.b_transform_8x8 )
711                 for( int p = 0; p < plane_count; p++ )
712                     for( int i8x8 = 0; i8x8 < 4; i8x8++ )
713                     {
714                         int x = i8x8&1;
715                         int y = i8x8>>1;
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;
720                     }
721             else
722                 for( int p = 0; p < plane_count; p++ )
723                     for( int i4x4 = 0; i4x4 < 16; i4x4++ )
724                     {
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);
730                     }
731         }
732         else if( h->mb.b_transform_8x8 )
733         {
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
736
737             for( int p = 0; p < plane_count; p++ )
738             {
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;
741
742                 int plane_cbp = 0;
743                 for( int idx = 0; idx < 4; idx++ )
744                 {
745                     nz = x264_quant_8x8( h, dct8x8[idx], i_qp, ctx_cat_plane[DCT_LUMA_8x8][p], 0, p, idx );
746
747                     if( nz )
748                     {
749                         h->zigzagf.scan_8x8( h->dct.luma8x8[p*4+idx], dct8x8[idx] );
750                         if( b_decimate )
751                         {
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 )
755                                 plane_cbp |= 1<<idx;
756                         }
757                         else
758                             plane_cbp |= 1<<idx;
759                     }
760                 }
761
762                 if( i_decimate_mb < 6 && b_decimate )
763                 {
764                     plane_cbp = 0;
765                     CLEAR_16x16_NNZ( p );
766                 }
767                 else
768                 {
769                     for( int idx = 0; idx < 4; idx++ )
770                     {
771                         int x = idx&1;
772                         int y = idx>>1;
773
774                         if( plane_cbp&(1<<idx) )
775                         {
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 );
779                         }
780                         else
781                             STORE_8x8_NNZ( p, idx, 0 );
782                     }
783                 }
784                 h->mb.i_cbp_luma |= plane_cbp;
785                 i_qp = h->mb.i_chroma_qp;
786             }
787         }
788         else
789         {
790             ALIGNED_ARRAY_16( dctcoef, dct4x4,[16],[16] );
791             for( int p = 0; p < plane_count; p++ )
792             {
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;
795
796                 int plane_cbp = 0;
797                 for( int i8x8 = 0; i8x8 < 4; i8x8++ )
798                 {
799                     int i_decimate_8x8 = 0;
800                     int cbp = 0;
801
802                     /* encode one 4x4 block */
803                     for( int i4x4 = 0; i4x4 < 4; i4x4++ )
804                     {
805                         int idx = i8x8 * 4 + i4x4;
806
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;
809
810                         if( nz )
811                         {
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] );
816                             cbp = 1;
817                         }
818                     }
819
820                     int x = i8x8&1;
821                     int y = i8x8>>1;
822
823                     /* decimate this 8x8 block */
824                     i_decimate_mb += i_decimate_8x8;
825                     if( b_decimate )
826                     {
827                         if( i_decimate_8x8 < 4 )
828                             STORE_8x8_NNZ( p, i8x8, 0 );
829                         else
830                             plane_cbp |= 1<<i8x8;
831                     }
832                     else if( cbp )
833                     {
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;
836                     }
837                 }
838
839                 if( b_decimate )
840                 {
841                     if( i_decimate_mb < 6 )
842                     {
843                         plane_cbp = 0;
844                         CLEAR_16x16_NNZ( p );
845                     }
846                     else
847                     {
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] );
851                     }
852                 }
853                 h->mb.i_cbp_luma |= plane_cbp;
854                 i_qp = h->mb.i_chroma_qp;
855             }
856         }
857     }
858
859     /* encode chroma */
860     if( chroma )
861     {
862         if( IS_INTRA( h->mb.i_type ) )
863         {
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 );
867             else
868             {
869                 h->predict_8x8c[i_mode]( h->mb.pic.p_fdec[1] );
870                 h->predict_8x8c[i_mode]( h->mb.pic.p_fdec[2] );
871             }
872         }
873
874         /* encode the 8x8 blocks */
875         x264_mb_encode_8x8_chroma( h, !IS_INTRA( h->mb.i_type ), h->mb.i_chroma_qp );
876     }
877     else
878         h->mb.i_cbp_chroma = 0;
879
880     /* store cbp */
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;
887
888     /* Check for P_SKIP
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 )
892     {
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 )
897         {
898             h->mb.i_type = P_SKIP;
899         }
900
901         /* Check for B_SKIP */
902         if( h->mb.i_type == B_DIRECT && !(h->mb.i_cbp_luma | h->mb.i_cbp_chroma) )
903         {
904             h->mb.i_type = B_SKIP;
905         }
906     }
907 }
908
909 void x264_macroblock_encode( x264_t *h )
910 {
911     if( CHROMA444 )
912         x264_macroblock_encode_internal( h, 3, 0 );
913     else
914         x264_macroblock_encode_internal( h, 1, 1 );
915 }
916
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 )
922 {
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] );
927
928     int i_qp = h->mb.i_qp;
929     int thresh, ssd;
930
931     for( int p = 0; p < plane_count; p++ )
932     {
933         int quant_cat = p ? CQM_4PC : CQM_4PY;
934         if( !b_bidir )
935         {
936             /* Get the MV */
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] );
939
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] );
944         }
945
946         for( int i8x8 = 0, i_decimate_mb = 0; i8x8 < 4; i8x8++ )
947         {
948             int fenc_offset = (i8x8&1) * 8 + (i8x8>>1) * FENC_STRIDE * 8;
949             int fdec_offset = (i8x8&1) * 8 + (i8x8>>1) * FDEC_STRIDE * 8;
950             /* get luma diff */
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++ )
955             {
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] ) )
959                     continue;
960                 h->zigzagf.scan_4x4( dctscan, dct4x4[i4x4] );
961                 i_decimate_mb += h->quantf.decimate_score16( dctscan );
962                 if( i_decimate_mb >= 6 )
963                     return 0;
964             }
965         }
966         i_qp = h->mb.i_chroma_qp;
967     }
968
969     if( chroma )
970     {
971         /* encode chroma */
972         i_qp = h->mb.i_chroma_qp;
973         thresh = (x264_lambda2_tab[i_qp] + 32) >> 6;
974
975         if( !b_bidir )
976         {
977             /* Special case for mv0, which is (of course) very common in P-skip mode. */
978             if( M32( mvp ) )
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 );
982             else
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] );
984         }
985
986         for( int ch = 0; ch < 2; ch++ )
987         {
988             pixel *p_src = h->mb.pic.p_fenc[1+ch];
989             pixel *p_dst = h->mb.pic.p_fdec[1+ch];
990
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 );
995
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 );
999             if( ssd < thresh )
1000                 continue;
1001
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 )
1005             {
1006                 h->dctf.sub8x8_dct( dct4x4, p_src, p_dst );
1007                 for( int i4x4 = 0; i4x4 < 4; i4x4++ )
1008                 {
1009                     h->quantf.denoise_dct( dct4x4[i4x4], h->nr_residual_sum[2], h->nr_offset[2], 16 );
1010                     dct2x2[i4x4] = dct4x4[i4x4][0];
1011                 }
1012             }
1013             else
1014                 h->dctf.sub8x8_dct_dc( dct2x2, p_src, p_dst );
1015
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 ) )
1017                 return 0;
1018
1019             /* If there wasn't a termination in DC, we can check against a much higher threshold. */
1020             if( ssd < thresh*4 )
1021                 continue;
1022
1023             if( !h->mb.b_noise_reduction )
1024                 h->dctf.sub8x8_dct( dct4x4, p_src, p_dst );
1025
1026             /* calculate dct coeffs */
1027             for( int i4x4 = 0, i_decimate_mb = 0; i4x4 < 4; i4x4++ )
1028             {
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] ) )
1033                     continue;
1034                 h->zigzagf.scan_4x4( dctscan, dct4x4[i4x4] );
1035                 i_decimate_mb += h->quantf.decimate_score15( dctscan );
1036                 if( i_decimate_mb >= 7 )
1037                     return 0;
1038             }
1039         }
1040     }
1041
1042     h->mb.b_skip_mc = 1;
1043     return 1;
1044 }
1045
1046 int x264_macroblock_probe_skip( x264_t *h, int b_bidir )
1047 {
1048     if( CHROMA444 )
1049         return x264_macroblock_probe_skip_internal( h, b_bidir, 3, 0 );
1050     else
1051         return x264_macroblock_probe_skip_internal( h, b_bidir, 1, 1 );
1052 }
1053
1054 /****************************************************************************
1055  * DCT-domain noise reduction / adaptive deadzone
1056  * from libavcodec
1057  ****************************************************************************/
1058
1059 void x264_noise_reduction_update( x264_t *h )
1060 {
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++ )
1065     {
1066         int dct8x8 = cat&1;
1067         int size = dct8x8 ? 64 : 16;
1068         const uint16_t *weight = dct8x8 ? x264_dct8_weight2_tab : x264_dct4_weight2_tab;
1069
1070         if( h->nr_count[cat] > (dct8x8 ? (1<<16) : (1<<18)) )
1071         {
1072             for( int i = 0; i < size; i++ )
1073                 h->nr_residual_sum[cat][i] >>= 1;
1074             h->nr_count[cat] >>= 1;
1075         }
1076
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);
1082
1083         /* Don't denoise DC coefficients */
1084         h->nr_offset[cat][0] = 0;
1085     }
1086 }
1087
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 )
1093 {
1094     int b_decimate = h->mb.b_dct_decimate;
1095     int i_qp = h->mb.i_qp;
1096     int x = i8&1;
1097     int y = i8>>1;
1098     int nz;
1099
1100     h->mb.i_cbp_chroma = 0;
1101     h->mb.i_cbp_luma &= ~(1 << i8);
1102
1103     if( !h->mb.b_skip_mc )
1104         x264_mb_mc_8x8( h, i8 );
1105
1106     if( h->mb.b_lossless )
1107     {
1108         for( int p = 0; p < plane_count; p++ )
1109         {
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;
1112             int nnz8x8 = 0;
1113             if( h->mb.b_transform_8x8 )
1114             {
1115                 nnz8x8 = h->zigzagf.sub_8x8( h->dct.luma8x8[4*p+i8], p_fenc, p_fdec );
1116                 STORE_8x8_NNZ( p, i8, nnz8x8 );
1117             }
1118             else
1119             {
1120                 for( int i4 = i8*4; i4 < i8*4+4; i4++ )
1121                 {
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;
1126                     nnz8x8 |= nz;
1127                 }
1128             }
1129             h->mb.i_cbp_luma |= nnz8x8 << i8;
1130         }
1131         if( chroma )
1132         {
1133             for( int ch = 0; ch < 2; ch++ )
1134             {
1135                 dctcoef dc;
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;
1140             }
1141             h->mb.i_cbp_chroma = 0x02;
1142         }
1143     }
1144     else
1145     {
1146         if( h->mb.b_transform_8x8 )
1147         {
1148             for( int p = 0; p < plane_count; p++ )
1149             {
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 );
1156                 if( nnz8x8 )
1157                 {
1158                     h->zigzagf.scan_8x8( h->dct.luma8x8[4*p+i8], dct8x8 );
1159
1160                     if( b_decimate && !h->mb.b_trellis )
1161                         nnz8x8 = 4 <= h->quantf.decimate_score64( h->dct.luma8x8[4*p+i8] );
1162
1163                     if( nnz8x8 )
1164                     {
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 );
1168                     }
1169                     else
1170                         STORE_8x8_NNZ( p, i8, 0 );
1171                 }
1172                 else
1173                     STORE_8x8_NNZ( p, i8, 0 );
1174                 h->mb.i_cbp_luma |= nnz8x8 << i8;
1175                 i_qp = h->mb.i_chroma_qp;
1176             }
1177         }
1178         else
1179         {
1180             for( int p = 0; p < plane_count; p++ )
1181             {
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++ )
1189                 {
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;
1192                     if( nz )
1193                     {
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 );
1196                         if( b_decimate )
1197                             i_decimate_8x8 += h->quantf.decimate_score16( h->dct.luma4x4[p*16+i8*4+i4] );
1198                         nnz8x8 = 1;
1199                     }
1200                 }
1201
1202                 if( b_decimate && i_decimate_8x8 < 4 )
1203                     nnz8x8 = 0;
1204
1205                 if( nnz8x8 )
1206                     h->dctf.add8x8_idct( p_fdec, dct4x4 );
1207                 else
1208                     STORE_8x8_NNZ( p, i8, 0 );
1209
1210                 h->mb.i_cbp_luma |= nnz8x8 << i8;
1211                 i_qp = h->mb.i_chroma_qp;
1212             }
1213         }
1214
1215         if( chroma )
1216         {
1217             i_qp = h->mb.i_chroma_qp;
1218             for( int ch = 0; ch < 2; ch++ )
1219             {
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 );
1226                 dct4x4[0] = 0;
1227
1228                 if( h->mb.b_trellis )
1229                     nz = x264_quant_4x4_trellis( h, dct4x4, CQM_4PC, i_qp, DCT_CHROMA_AC, 0, 1, 0 );
1230                 else
1231                     nz = h->quantf.quant_4x4( dct4x4, h->quant4_mf[CQM_4PC][i_qp], h->quant4_bias[CQM_4PC][i_qp] );
1232
1233                 h->mb.cache.non_zero_count[x264_scan8[16+i8+ch*16]] = nz;
1234                 if( nz )
1235                 {
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 );
1239                 }
1240             }
1241             h->mb.i_cbp_chroma = 0x02;
1242         }
1243     }
1244 }
1245
1246 void x264_macroblock_encode_p8x8( x264_t *h, int i8 )
1247 {
1248     if( CHROMA444 )
1249         x264_macroblock_encode_p8x8_internal( h, i8, 3, 0 );
1250     else
1251         x264_macroblock_encode_p8x8_internal( h, i8, 1, 1 );
1252 }
1253
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 )
1258 {
1259     int i_qp = h->mb.i_qp;
1260
1261     for( int p = 0; p < plane_count; p++ )
1262     {
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]];
1266         int nz;
1267
1268         /* Don't need motion compensation as this function is only used in qpel-RD, which caches pixel data. */
1269
1270         if( h->mb.b_lossless )
1271         {
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;
1274         }
1275         else
1276         {
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;
1281             if( nz )
1282             {
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 );
1286             }
1287         }
1288         i_qp = h->mb.i_chroma_qp;
1289     }
1290 }
1291
1292 void x264_macroblock_encode_p4x4( x264_t *h, int i8 )
1293 {
1294     if( CHROMA444 )
1295         x264_macroblock_encode_p4x4_internal( h, i8, 3 );
1296     else
1297         x264_macroblock_encode_p4x4_internal( h, i8, 1 );
1298 }