867f28671ecb8d8cc2b4083517700c64785452b6
[x262.git] / encoder / macroblock.c
1 /*****************************************************************************
2  * macroblock.c: macroblock encoding
3  *****************************************************************************
4  * Copyright (C) 2003-2013 x264 project
5  *
6  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
7  *          Loren Merritt <lorenm@u.washington.edu>
8  *          Jason Garrett-Glaser <darkshikari@gmail.com>
9  *          Henrik Gramner <hengar-6@student.ltu.se>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
24  *
25  * This program is also available under a commercial proprietary license.
26  * For more information, contact us at licensing@x264.com.
27  *****************************************************************************/
28
29 #include "common/common.h"
30 #include "macroblock.h"
31
32 /* These chroma DC functions don't have assembly versions and are only used here. */
33
34 #define ZIG(i,y,x) level[i] = dct[x*2+y];
35 static inline void zigzag_scan_2x2_dc( dctcoef level[4], dctcoef dct[4] )
36 {
37     ZIG(0,0,0)
38     ZIG(1,0,1)
39     ZIG(2,1,0)
40     ZIG(3,1,1)
41 }
42 #undef ZIG
43
44 static inline void zigzag_scan_2x4_dc( dctcoef level[8], dctcoef dct[8] )
45 {
46     level[0] = dct[0];
47     level[1] = dct[2];
48     level[2] = dct[1];
49     level[3] = dct[4];
50     level[4] = dct[6];
51     level[5] = dct[3];
52     level[6] = dct[5];
53     level[7] = dct[7];
54 }
55
56 #define IDCT_DEQUANT_2X2_START \
57     int d0 = dct[0] + dct[1]; \
58     int d1 = dct[2] + dct[3]; \
59     int d2 = dct[0] - dct[1]; \
60     int d3 = dct[2] - dct[3]; \
61     int dmf = dequant_mf[i_qp%6][0] << i_qp/6;
62
63 static inline void idct_dequant_2x2_dc( dctcoef dct[4], dctcoef dct4x4[4][16], int dequant_mf[6][16], int i_qp )
64 {
65     IDCT_DEQUANT_2X2_START
66     dct4x4[0][0] = (d0 + d1) * dmf >> 5;
67     dct4x4[1][0] = (d0 - d1) * dmf >> 5;
68     dct4x4[2][0] = (d2 + d3) * dmf >> 5;
69     dct4x4[3][0] = (d2 - d3) * dmf >> 5;
70 }
71
72 static inline void idct_dequant_2x2_dconly( dctcoef dct[4], int dequant_mf[6][16], int i_qp )
73 {
74     IDCT_DEQUANT_2X2_START
75     dct[0] = (d0 + d1) * dmf >> 5;
76     dct[1] = (d0 - d1) * dmf >> 5;
77     dct[2] = (d2 + d3) * dmf >> 5;
78     dct[3] = (d2 - d3) * dmf >> 5;
79 }
80 #undef IDCT_2X2_DEQUANT_START
81
82 static inline void dct2x2dc( dctcoef d[4], dctcoef dct4x4[4][16] )
83 {
84     int d0 = dct4x4[0][0] + dct4x4[1][0];
85     int d1 = dct4x4[2][0] + dct4x4[3][0];
86     int d2 = dct4x4[0][0] - dct4x4[1][0];
87     int d3 = dct4x4[2][0] - dct4x4[3][0];
88     d[0] = d0 + d1;
89     d[2] = d2 + d3;
90     d[1] = d0 - d1;
91     d[3] = d2 - d3;
92     dct4x4[0][0] = 0;
93     dct4x4[1][0] = 0;
94     dct4x4[2][0] = 0;
95     dct4x4[3][0] = 0;
96 }
97
98 static ALWAYS_INLINE int array_non_zero( dctcoef *v, int i_count )
99 {
100     if( WORD_SIZE == 8 )
101     {
102         for( int i = 0; i < i_count; i += 8/sizeof(dctcoef) )
103             if( M64( &v[i] ) )
104                 return 1;
105     }
106     else
107     {
108         for( int i = 0; i < i_count; i += 4/sizeof(dctcoef) )
109             if( M32( &v[i] ) )
110                 return 1;
111     }
112     return 0;
113 }
114
115 /* All encoding functions must output the correct CBP and NNZ values.
116  * The entropy coding functions will check CBP first, then NNZ, before
117  * actually reading the DCT coefficients.  NNZ still must be correct even
118  * if CBP is zero because of the use of NNZ values for context selection.
119  * "NNZ" need only be 0 or 1 rather than the exact coefficient count because
120  * that is only needed in CAVLC, and will be calculated by CAVLC's residual
121  * coding and stored as necessary. */
122
123 /* This means that decimation can be done merely by adjusting the CBP and NNZ
124  * rather than memsetting the coefficients. */
125
126 static void x264_mb_encode_i16x16( x264_t *h, int p, int i_qp )
127 {
128     pixel *p_src = h->mb.pic.p_fenc[p];
129     pixel *p_dst = h->mb.pic.p_fdec[p];
130
131     ALIGNED_ARRAY_N( dctcoef, dct4x4,[16],[16] );
132     ALIGNED_ARRAY_N( dctcoef, dct_dc4x4,[16] );
133
134     int nz, block_cbp = 0;
135     int decimate_score = h->mb.b_dct_decimate ? 0 : 9;
136     int i_quant_cat = p ? CQM_4IC : CQM_4IY;
137     int i_mode = h->mb.i_intra16x16_pred_mode;
138
139     if( h->mb.b_lossless )
140         x264_predict_lossless_16x16( h, p, i_mode );
141     else
142         h->predict_16x16[i_mode]( h->mb.pic.p_fdec[p] );
143
144     if( h->mb.b_lossless )
145     {
146         for( int i = 0; i < 16; i++ )
147         {
148             int oe = block_idx_xy_fenc[i];
149             int od = block_idx_xy_fdec[i];
150             nz = h->zigzagf.sub_4x4ac( h->dct.luma4x4[16*p+i], p_src+oe, p_dst+od, &dct_dc4x4[block_idx_yx_1d[i]] );
151             h->mb.cache.non_zero_count[x264_scan8[16*p+i]] = nz;
152             block_cbp |= nz;
153         }
154         h->mb.i_cbp_luma |= block_cbp * 0xf;
155         h->mb.cache.non_zero_count[x264_scan8[LUMA_DC+p]] = array_non_zero( dct_dc4x4, 16 );
156         h->zigzagf.scan_4x4( h->dct.luma16x16_dc[p], dct_dc4x4 );
157         return;
158     }
159
160     M32( &h->mb.cache.non_zero_count[x264_scan8[ 0+p*16]] ) = 0;
161     M32( &h->mb.cache.non_zero_count[x264_scan8[ 2+p*16]] ) = 0;
162     M32( &h->mb.cache.non_zero_count[x264_scan8[ 8+p*16]] ) = 0;
163     M32( &h->mb.cache.non_zero_count[x264_scan8[10+p*16]] ) = 0;
164
165     h->dctf.sub16x16_dct( dct4x4, p_src, p_dst );
166
167     if( h->mb.b_noise_reduction )
168         for( int idx = 0; idx < 16; idx++ )
169             h->quantf.denoise_dct( dct4x4[idx], h->nr_residual_sum[0], h->nr_offset[0], 16 );
170
171     for( int idx = 0; idx < 16; idx++ )
172     {
173         dct_dc4x4[block_idx_xy_1d[idx]] = dct4x4[idx][0];
174         dct4x4[idx][0] = 0;
175     }
176
177     if( h->mb.b_trellis )
178     {
179         for( int idx = 0; idx < 16; idx++ )
180             if( x264_quant_4x4_trellis( h, dct4x4[idx], i_quant_cat, i_qp, ctx_cat_plane[DCT_LUMA_AC][p], 1, !!p, idx ) )
181             {
182                 block_cbp = 0xf;
183                 h->zigzagf.scan_4x4( h->dct.luma4x4[16*p+idx], dct4x4[idx] );
184                 h->quantf.dequant_4x4( dct4x4[idx], h->dequant4_mf[i_quant_cat], i_qp );
185                 if( decimate_score < 6 ) decimate_score += h->quantf.decimate_score15( h->dct.luma4x4[16*p+idx] );
186                 h->mb.cache.non_zero_count[x264_scan8[16*p+idx]] = 1;
187             }
188     }
189     else
190     {
191         for( int i8x8 = 0; i8x8 < 4; i8x8++ )
192         {
193             nz = h->quantf.quant_4x4x4( &dct4x4[i8x8*4], h->quant4_mf[i_quant_cat][i_qp], h->quant4_bias[i_quant_cat][i_qp] );
194             if( nz )
195             {
196                 block_cbp = 0xf;
197                 FOREACH_BIT( idx, i8x8*4, nz )
198                 {
199                     h->zigzagf.scan_4x4( h->dct.luma4x4[16*p+idx], dct4x4[idx] );
200                     h->quantf.dequant_4x4( dct4x4[idx], h->dequant4_mf[i_quant_cat], i_qp );
201                     if( decimate_score < 6 ) decimate_score += h->quantf.decimate_score15( h->dct.luma4x4[16*p+idx] );
202                     h->mb.cache.non_zero_count[x264_scan8[16*p+idx]] = 1;
203                 }
204             }
205         }
206     }
207
208     /* Writing the 16 CBFs in an i16x16 block is quite costly, so decimation can save many bits. */
209     /* More useful with CAVLC, but still useful with CABAC. */
210     if( decimate_score < 6 )
211     {
212         CLEAR_16x16_NNZ( p );
213         block_cbp = 0;
214     }
215     else
216         h->mb.i_cbp_luma |= block_cbp;
217
218     h->dctf.dct4x4dc( dct_dc4x4 );
219     if( h->mb.b_trellis )
220         nz = x264_quant_luma_dc_trellis( h, dct_dc4x4, i_quant_cat, i_qp, ctx_cat_plane[DCT_LUMA_DC][p], 1, LUMA_DC+p );
221     else
222         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 );
223
224     h->mb.cache.non_zero_count[x264_scan8[LUMA_DC+p]] = nz;
225     if( nz )
226     {
227         h->zigzagf.scan_4x4( h->dct.luma16x16_dc[p], dct_dc4x4 );
228
229         /* output samples to fdec */
230         h->dctf.idct4x4dc( dct_dc4x4 );
231         h->quantf.dequant_4x4_dc( dct_dc4x4, h->dequant4_mf[i_quant_cat], i_qp );  /* XXX not inversed */
232         if( block_cbp )
233             for( int i = 0; i < 16; i++ )
234                 dct4x4[i][0] = dct_dc4x4[block_idx_xy_1d[i]];
235     }
236
237     /* put pixels to fdec */
238     if( block_cbp )
239         h->dctf.add16x16_idct( p_dst, dct4x4 );
240     else if( nz )
241         h->dctf.add16x16_idct_dc( p_dst, dct_dc4x4 );
242 }
243
244 /* Round down coefficients losslessly in DC-only chroma blocks.
245  * Unlike luma blocks, this can't be done with a lookup table or
246  * other shortcut technique because of the interdependencies
247  * between the coefficients due to the chroma DC transform. */
248 static ALWAYS_INLINE int x264_mb_optimize_chroma_dc( x264_t *h, dctcoef *dct_dc, int dequant_mf[6][16], int i_qp, int chroma422 )
249 {
250     int dmf = dequant_mf[i_qp%6][0] << i_qp/6;
251
252     /* If the QP is too high, there's no benefit to rounding optimization. */
253     if( dmf > 32*64 )
254         return 1;
255
256     if( chroma422 )
257         return h->quantf.optimize_chroma_2x4_dc( dct_dc, dmf );
258     else
259         return h->quantf.optimize_chroma_2x2_dc( dct_dc, dmf );
260 }
261
262 static ALWAYS_INLINE void x264_mb_encode_chroma_internal( x264_t *h, int b_inter, int i_qp, int chroma422 )
263 {
264     int nz, nz_dc;
265     int b_decimate = b_inter && h->mb.b_dct_decimate;
266     int (*dequant_mf)[16] = h->dequant4_mf[CQM_4IC + b_inter];
267     ALIGNED_ARRAY_16( dctcoef, dct_dc,[8] );
268     h->mb.i_cbp_chroma = 0;
269     h->nr_count[2] += h->mb.b_noise_reduction * 4;
270
271     M16( &h->mb.cache.non_zero_count[x264_scan8[16]] ) = 0;
272     M16( &h->mb.cache.non_zero_count[x264_scan8[18]] ) = 0;
273     M16( &h->mb.cache.non_zero_count[x264_scan8[32]] ) = 0;
274     M16( &h->mb.cache.non_zero_count[x264_scan8[34]] ) = 0;
275     if( chroma422 )
276     {
277         M16( &h->mb.cache.non_zero_count[x264_scan8[24]] ) = 0;
278         M16( &h->mb.cache.non_zero_count[x264_scan8[26]] ) = 0;
279         M16( &h->mb.cache.non_zero_count[x264_scan8[40]] ) = 0;
280         M16( &h->mb.cache.non_zero_count[x264_scan8[42]] ) = 0;
281     }
282
283     /* Early termination: check variance of chroma residual before encoding.
284      * Don't bother trying early termination at low QPs.
285      * Values are experimentally derived. */
286     if( b_decimate && i_qp >= (h->mb.b_trellis ? 12 : 18) && !h->mb.b_noise_reduction )
287     {
288         int thresh = chroma422 ? (x264_lambda2_tab[i_qp] + 16) >> 5 : (x264_lambda2_tab[i_qp] + 32) >> 6;
289         int ssd[2];
290         int chromapix = chroma422 ? PIXEL_8x16 : PIXEL_8x8;
291
292         int score  = h->pixf.var2[chromapix]( h->mb.pic.p_fenc[1], FENC_STRIDE, h->mb.pic.p_fdec[1], FDEC_STRIDE, &ssd[0] );
293         if( score < thresh*4 )
294             score += h->pixf.var2[chromapix]( h->mb.pic.p_fenc[2], FENC_STRIDE, h->mb.pic.p_fdec[2], FDEC_STRIDE, &ssd[1] );
295         if( score < thresh*4 )
296         {
297             h->mb.cache.non_zero_count[x264_scan8[CHROMA_DC+0]] = 0;
298             h->mb.cache.non_zero_count[x264_scan8[CHROMA_DC+1]] = 0;
299
300             for( int ch = 0; ch < 2; ch++ )
301             {
302                 if( ssd[ch] > thresh )
303                 {
304                     pixel *p_src = h->mb.pic.p_fenc[1+ch];
305                     pixel *p_dst = h->mb.pic.p_fdec[1+ch];
306
307                     if( chroma422 )
308                         /* Cannot be replaced by two calls to sub8x8_dct_dc since the hadamard transform is different */
309                         h->dctf.sub8x16_dct_dc( dct_dc, p_src, p_dst );
310                     else
311                         h->dctf.sub8x8_dct_dc( dct_dc, p_src, p_dst );
312
313                     if( h->mb.b_trellis )
314                         nz_dc = x264_quant_chroma_dc_trellis( h, dct_dc, i_qp+3*chroma422, !b_inter, CHROMA_DC+ch );
315                     else
316                     {
317                         nz_dc = 0;
318                         for( int i = 0; i <= chroma422; i++ )
319                             nz_dc |= h->quantf.quant_2x2_dc( &dct_dc[4*i], h->quant4_mf[CQM_4IC+b_inter][i_qp+3*chroma422][0] >> 1,
320                                                              h->quant4_bias[CQM_4IC+b_inter][i_qp+3*chroma422][0] << 1 );
321                     }
322
323                     if( nz_dc )
324                     {
325                         if( !x264_mb_optimize_chroma_dc( h, dct_dc, dequant_mf, i_qp+3*chroma422, chroma422 ) )
326                             continue;
327                         h->mb.cache.non_zero_count[x264_scan8[CHROMA_DC+ch]] = 1;
328                         if( chroma422 )
329                         {
330                             zigzag_scan_2x4_dc( h->dct.chroma_dc[ch], dct_dc );
331                             h->quantf.idct_dequant_2x4_dconly( dct_dc, dequant_mf, i_qp+3 );
332                         }
333                         else
334                         {
335                             zigzag_scan_2x2_dc( h->dct.chroma_dc[ch], dct_dc );
336                             idct_dequant_2x2_dconly( dct_dc, dequant_mf, i_qp );
337                         }
338
339                         for( int i = 0; i <= chroma422; i++ )
340                             h->dctf.add8x8_idct_dc( p_dst + 8*i*FDEC_STRIDE, &dct_dc[4*i] );
341                         h->mb.i_cbp_chroma = 1;
342                     }
343                 }
344             }
345             return;
346         }
347     }
348
349     for( int ch = 0; ch < 2; ch++ )
350     {
351         pixel *p_src = h->mb.pic.p_fenc[1+ch];
352         pixel *p_dst = h->mb.pic.p_fdec[1+ch];
353         int i_decimate_score = b_decimate ? 0 : 7;
354         int nz_ac = 0;
355
356         ALIGNED_ARRAY_N( dctcoef, dct4x4,[8],[16] );
357
358         if( h->mb.b_lossless )
359         {
360             static const uint8_t chroma422_scan[8] = { 0, 2, 1, 5, 3, 6, 4, 7 };
361
362             for( int i = 0; i < (chroma422?8:4); i++ )
363             {
364                 int oe = 4*(i&1) + 4*(i>>1)*FENC_STRIDE;
365                 int od = 4*(i&1) + 4*(i>>1)*FDEC_STRIDE;
366                 nz = h->zigzagf.sub_4x4ac( h->dct.luma4x4[16+i+(chroma422?i&4:0)+ch*16], p_src+oe, p_dst+od,
367                                            &h->dct.chroma_dc[ch][chroma422?chroma422_scan[i]:i] );
368                 h->mb.cache.non_zero_count[x264_scan8[16+i+(chroma422?i&4:0)+ch*16]] = nz;
369                 h->mb.i_cbp_chroma |= nz;
370             }
371             h->mb.cache.non_zero_count[x264_scan8[CHROMA_DC+ch]] = array_non_zero( h->dct.chroma_dc[ch], chroma422?8:4 );
372             continue;
373         }
374
375         for( int i = 0; i <= chroma422; i++ )
376             h->dctf.sub8x8_dct( &dct4x4[4*i], p_src + 8*i*FENC_STRIDE, p_dst + 8*i*FDEC_STRIDE );
377
378         if( h->mb.b_noise_reduction )
379             for( int i = 0; i < (chroma422?8:4); i++ )
380                 h->quantf.denoise_dct( dct4x4[i], h->nr_residual_sum[2], h->nr_offset[2], 16 );
381
382         if( chroma422 )
383             h->dctf.dct2x4dc( dct_dc, dct4x4 );
384         else
385             dct2x2dc( dct_dc, dct4x4 );
386
387         /* calculate dct coeffs */
388         for( int i8x8 = 0; i8x8 < (chroma422?2:1); i8x8++ )
389         {
390             if( h->mb.b_trellis )
391             {
392                 for( int i4x4 = 0; i4x4 < 4; i4x4++ )
393                 {
394                     if( x264_quant_4x4_trellis( h, dct4x4[i8x8*4+i4x4], CQM_4IC+b_inter, i_qp, DCT_CHROMA_AC, !b_inter, 1, 0 ) )
395                     {
396                         int idx = 16+ch*16+i8x8*8+i4x4;
397                         h->zigzagf.scan_4x4( h->dct.luma4x4[idx], dct4x4[i8x8*4+i4x4] );
398                         h->quantf.dequant_4x4( dct4x4[i8x8*4+i4x4], dequant_mf, i_qp );
399                         if( i_decimate_score < 7 )
400                             i_decimate_score += h->quantf.decimate_score15( h->dct.luma4x4[idx] );
401                         h->mb.cache.non_zero_count[x264_scan8[idx]] = 1;
402                         nz_ac = 1;
403                     }
404                 }
405             }
406             else
407             {
408                 nz = h->quantf.quant_4x4x4( &dct4x4[i8x8*4], h->quant4_mf[CQM_4IC+b_inter][i_qp],
409                                             h->quant4_bias[CQM_4IC+b_inter][i_qp] );
410                 nz_ac |= nz;
411
412                 FOREACH_BIT( i4x4, 0, nz )
413                 {
414                     int idx = 16+ch*16+i8x8*8+i4x4;
415
416                     h->zigzagf.scan_4x4( h->dct.luma4x4[idx], dct4x4[i8x8*4+i4x4] );
417                     h->quantf.dequant_4x4( dct4x4[i8x8*4+i4x4], dequant_mf, i_qp );
418                     if( i_decimate_score < 7 )
419                         i_decimate_score += h->quantf.decimate_score15( h->dct.luma4x4[idx] );
420                     h->mb.cache.non_zero_count[x264_scan8[idx]] = 1;
421                 }
422             }
423         }
424
425         if( h->mb.b_trellis )
426             nz_dc = x264_quant_chroma_dc_trellis( h, dct_dc, i_qp+3*chroma422, !b_inter, CHROMA_DC+ch );
427         else
428         {
429             nz_dc = 0;
430             for( int i = 0; i <= chroma422; i++ )
431                 nz_dc |= h->quantf.quant_2x2_dc( &dct_dc[4*i], h->quant4_mf[CQM_4IC+b_inter][i_qp+3*chroma422][0] >> 1,
432                                                  h->quant4_bias[CQM_4IC+b_inter][i_qp+3*chroma422][0] << 1 );
433         }
434
435         h->mb.cache.non_zero_count[x264_scan8[CHROMA_DC+ch]] = nz_dc;
436
437         if( i_decimate_score < 7 || !nz_ac )
438         {
439             /* Decimate the block */
440             M16( &h->mb.cache.non_zero_count[x264_scan8[16+16*ch]] ) = 0;
441             M16( &h->mb.cache.non_zero_count[x264_scan8[18+16*ch]] ) = 0;
442             if( chroma422 )
443             {
444                 M16( &h->mb.cache.non_zero_count[x264_scan8[24+16*ch]] ) = 0;
445                 M16( &h->mb.cache.non_zero_count[x264_scan8[26+16*ch]] ) = 0;
446             }
447
448             if( !nz_dc ) /* Whole block is empty */
449                 continue;
450             if( !x264_mb_optimize_chroma_dc( h, dct_dc, dequant_mf, i_qp+3*chroma422, chroma422 ) )
451             {
452                 h->mb.cache.non_zero_count[x264_scan8[CHROMA_DC+ch]] = 0;
453                 continue;
454             }
455             /* DC-only */
456             if( chroma422 )
457             {
458                 zigzag_scan_2x4_dc( h->dct.chroma_dc[ch], dct_dc );
459                 h->quantf.idct_dequant_2x4_dconly( dct_dc, dequant_mf, i_qp+3 );
460             }
461             else
462             {
463                 zigzag_scan_2x2_dc( h->dct.chroma_dc[ch], dct_dc );
464                 idct_dequant_2x2_dconly( dct_dc, dequant_mf, i_qp );
465             }
466
467             for( int i = 0; i <= chroma422; i++ )
468                 h->dctf.add8x8_idct_dc( p_dst + 8*i*FDEC_STRIDE, &dct_dc[4*i] );
469         }
470         else
471         {
472             h->mb.i_cbp_chroma = 1;
473
474             if( nz_dc )
475             {
476                 if( chroma422 )
477                 {
478                     zigzag_scan_2x4_dc( h->dct.chroma_dc[ch], dct_dc );
479                     h->quantf.idct_dequant_2x4_dc( dct_dc, dct4x4, dequant_mf, i_qp+3 );
480                 }
481                 else
482                 {
483                     zigzag_scan_2x2_dc( h->dct.chroma_dc[ch], dct_dc );
484                     idct_dequant_2x2_dc( dct_dc, dct4x4, dequant_mf, i_qp );
485                 }
486             }
487
488             for( int i = 0; i <= chroma422; i++ )
489                 h->dctf.add8x8_idct( p_dst + 8*i*FDEC_STRIDE, &dct4x4[4*i] );
490         }
491     }
492
493     /* 0 = none, 1 = DC only, 2 = DC+AC */
494     h->mb.i_cbp_chroma += (h->mb.cache.non_zero_count[x264_scan8[CHROMA_DC+0]] |
495                            h->mb.cache.non_zero_count[x264_scan8[CHROMA_DC+1]] | h->mb.i_cbp_chroma);
496 }
497
498 void x264_mb_encode_chroma( x264_t *h, int b_inter, int i_qp )
499 {
500     if( CHROMA_FORMAT == CHROMA_420 )
501         x264_mb_encode_chroma_internal( h, b_inter, i_qp, 0 );
502     else
503         x264_mb_encode_chroma_internal( h, b_inter, i_qp, 1 );
504 }
505
506 static void x264_macroblock_encode_skip( x264_t *h )
507 {
508     M32( &h->mb.cache.non_zero_count[x264_scan8[ 0]] ) = 0;
509     M32( &h->mb.cache.non_zero_count[x264_scan8[ 2]] ) = 0;
510     M32( &h->mb.cache.non_zero_count[x264_scan8[ 8]] ) = 0;
511     M32( &h->mb.cache.non_zero_count[x264_scan8[10]] ) = 0;
512     M32( &h->mb.cache.non_zero_count[x264_scan8[16+ 0]] ) = 0;
513     M32( &h->mb.cache.non_zero_count[x264_scan8[16+ 2]] ) = 0;
514     M32( &h->mb.cache.non_zero_count[x264_scan8[32+ 0]] ) = 0;
515     M32( &h->mb.cache.non_zero_count[x264_scan8[32+ 2]] ) = 0;
516     if( CHROMA_FORMAT >= CHROMA_422 )
517     {
518         M32( &h->mb.cache.non_zero_count[x264_scan8[16+ 8]] ) = 0;
519         M32( &h->mb.cache.non_zero_count[x264_scan8[16+10]] ) = 0;
520         M32( &h->mb.cache.non_zero_count[x264_scan8[32+ 8]] ) = 0;
521         M32( &h->mb.cache.non_zero_count[x264_scan8[32+10]] ) = 0;
522     }
523     h->mb.i_cbp_luma = 0;
524     h->mb.i_cbp_chroma = 0;
525     h->mb.cbp[h->mb.i_mb_xy] = 0;
526 }
527
528 /*****************************************************************************
529  * Intra prediction for predictive lossless mode.
530  *****************************************************************************/
531
532 void x264_predict_lossless_chroma( x264_t *h, int i_mode )
533 {
534     int height = 16 >> CHROMA_V_SHIFT;
535     if( i_mode == I_PRED_CHROMA_V )
536     {
537         h->mc.copy[PIXEL_8x8]( h->mb.pic.p_fdec[1], FDEC_STRIDE, h->mb.pic.p_fenc[1]-FENC_STRIDE, FENC_STRIDE, height );
538         h->mc.copy[PIXEL_8x8]( h->mb.pic.p_fdec[2], FDEC_STRIDE, h->mb.pic.p_fenc[2]-FENC_STRIDE, FENC_STRIDE, height );
539         memcpy( h->mb.pic.p_fdec[1], h->mb.pic.p_fdec[1]-FDEC_STRIDE, 8*sizeof(pixel) );
540         memcpy( h->mb.pic.p_fdec[2], h->mb.pic.p_fdec[2]-FDEC_STRIDE, 8*sizeof(pixel) );
541     }
542     else if( i_mode == I_PRED_CHROMA_H )
543     {
544         h->mc.copy[PIXEL_8x8]( h->mb.pic.p_fdec[1], FDEC_STRIDE, h->mb.pic.p_fenc[1]-1, FENC_STRIDE, height );
545         h->mc.copy[PIXEL_8x8]( h->mb.pic.p_fdec[2], FDEC_STRIDE, h->mb.pic.p_fenc[2]-1, FENC_STRIDE, height );
546         x264_copy_column8( h->mb.pic.p_fdec[1]+4*FDEC_STRIDE, h->mb.pic.p_fdec[1]+4*FDEC_STRIDE-1 );
547         x264_copy_column8( h->mb.pic.p_fdec[2]+4*FDEC_STRIDE, h->mb.pic.p_fdec[2]+4*FDEC_STRIDE-1 );
548         if( CHROMA_FORMAT == CHROMA_422 )
549         {
550             x264_copy_column8( h->mb.pic.p_fdec[1]+12*FDEC_STRIDE, h->mb.pic.p_fdec[1]+12*FDEC_STRIDE-1 );
551             x264_copy_column8( h->mb.pic.p_fdec[2]+12*FDEC_STRIDE, h->mb.pic.p_fdec[2]+12*FDEC_STRIDE-1 );
552         }
553     }
554     else
555     {
556         h->predict_chroma[i_mode]( h->mb.pic.p_fdec[1] );
557         h->predict_chroma[i_mode]( h->mb.pic.p_fdec[2] );
558     }
559 }
560
561 void x264_predict_lossless_4x4( x264_t *h, pixel *p_dst, int p, int idx, int i_mode )
562 {
563     int stride = h->fenc->i_stride[p] << MB_INTERLACED;
564     pixel *p_src = h->mb.pic.p_fenc_plane[p] + block_idx_x[idx]*4 + block_idx_y[idx]*4 * stride;
565
566     if( i_mode == I_PRED_4x4_V )
567         h->mc.copy[PIXEL_4x4]( p_dst, FDEC_STRIDE, p_src-stride, stride, 4 );
568     else if( i_mode == I_PRED_4x4_H )
569         h->mc.copy[PIXEL_4x4]( p_dst, FDEC_STRIDE, p_src-1, stride, 4 );
570     else
571         h->predict_4x4[i_mode]( p_dst );
572 }
573
574 void x264_predict_lossless_8x8( x264_t *h, pixel *p_dst, int p, int idx, int i_mode, pixel edge[36] )
575 {
576     int stride = h->fenc->i_stride[p] << MB_INTERLACED;
577     pixel *p_src = h->mb.pic.p_fenc_plane[p] + (idx&1)*8 + (idx>>1)*8*stride;
578
579     if( i_mode == I_PRED_8x8_V )
580         h->mc.copy[PIXEL_8x8]( p_dst, FDEC_STRIDE, p_src-stride, stride, 8 );
581     else if( i_mode == I_PRED_8x8_H )
582         h->mc.copy[PIXEL_8x8]( p_dst, FDEC_STRIDE, p_src-1, stride, 8 );
583     else
584         h->predict_8x8[i_mode]( p_dst, edge );
585 }
586
587 void x264_predict_lossless_16x16( x264_t *h, int p, int i_mode )
588 {
589     int stride = h->fenc->i_stride[p] << MB_INTERLACED;
590     if( i_mode == I_PRED_16x16_V )
591         h->mc.copy[PIXEL_16x16]( h->mb.pic.p_fdec[p], FDEC_STRIDE, h->mb.pic.p_fenc_plane[p]-stride, stride, 16 );
592     else if( i_mode == I_PRED_16x16_H )
593         h->mc.copy_16x16_unaligned( h->mb.pic.p_fdec[p], FDEC_STRIDE, h->mb.pic.p_fenc_plane[p]-1, stride, 16 );
594     else
595         h->predict_16x16[i_mode]( h->mb.pic.p_fdec[p] );
596 }
597
598 /*****************************************************************************
599  * x264_macroblock_encode:
600  *****************************************************************************/
601 static ALWAYS_INLINE void x264_macroblock_encode_internal( x264_t *h, int plane_count, int chroma )
602 {
603     int i_qp = h->mb.i_qp;
604     int b_decimate = h->mb.b_dct_decimate;
605     int b_force_no_skip = 0;
606     int nz;
607     h->mb.i_cbp_luma = 0;
608     for( int p = 0; p < plane_count; p++ )
609         h->mb.cache.non_zero_count[x264_scan8[LUMA_DC+p]] = 0;
610
611     if( h->mb.i_type == I_PCM )
612     {
613         /* if PCM is chosen, we need to store reconstructed frame data */
614         for( int p = 0; p < plane_count; p++ )
615             h->mc.copy[PIXEL_16x16]( h->mb.pic.p_fdec[p], FDEC_STRIDE, h->mb.pic.p_fenc[p], FENC_STRIDE, 16 );
616         if( chroma )
617         {
618             int height = 16 >> CHROMA_V_SHIFT;
619             h->mc.copy[PIXEL_8x8]  ( h->mb.pic.p_fdec[1], FDEC_STRIDE, h->mb.pic.p_fenc[1], FENC_STRIDE, height );
620             h->mc.copy[PIXEL_8x8]  ( h->mb.pic.p_fdec[2], FDEC_STRIDE, h->mb.pic.p_fenc[2], FENC_STRIDE, height );
621         }
622         return;
623     }
624
625     if( !h->mb.b_allow_skip )
626     {
627         b_force_no_skip = 1;
628         if( IS_SKIP(h->mb.i_type) )
629         {
630             if( h->mb.i_type == P_SKIP )
631                 h->mb.i_type = P_L0;
632             else if( h->mb.i_type == B_SKIP )
633                 h->mb.i_type = B_DIRECT;
634         }
635     }
636
637     if( h->mb.i_type == P_SKIP )
638     {
639         /* don't do pskip motion compensation if it was already done in macroblock_analyse */
640         if( !h->mb.b_skip_mc )
641         {
642             int mvx = x264_clip3( h->mb.cache.mv[0][x264_scan8[0]][0],
643                                   h->mb.mv_min[0], h->mb.mv_max[0] );
644             int mvy = x264_clip3( h->mb.cache.mv[0][x264_scan8[0]][1],
645                                   h->mb.mv_min[1], h->mb.mv_max[1] );
646
647             for( int p = 0; p < plane_count; p++ )
648                 h->mc.mc_luma( h->mb.pic.p_fdec[p], FDEC_STRIDE,
649                                &h->mb.pic.p_fref[0][0][p*4], h->mb.pic.i_stride[p],
650                                mvx, mvy, 16, 16, &h->sh.weight[0][p] );
651
652             if( chroma )
653             {
654                 int v_shift = CHROMA_V_SHIFT;
655                 int height = 16 >> v_shift;
656
657                 /* Special case for mv0, which is (of course) very common in P-skip mode. */
658                 if( mvx | mvy )
659                     h->mc.mc_chroma( h->mb.pic.p_fdec[1], h->mb.pic.p_fdec[2], FDEC_STRIDE,
660                                      h->mb.pic.p_fref[0][0][4], h->mb.pic.i_stride[1],
661                                      mvx, 2*mvy>>v_shift, 8, height );
662                 else
663                     h->mc.load_deinterleave_chroma_fdec( h->mb.pic.p_fdec[1], h->mb.pic.p_fref[0][0][4],
664                                                          h->mb.pic.i_stride[1], height );
665
666                 if( h->sh.weight[0][1].weightfn )
667                     h->sh.weight[0][1].weightfn[8>>2]( h->mb.pic.p_fdec[1], FDEC_STRIDE,
668                                                        h->mb.pic.p_fdec[1], FDEC_STRIDE,
669                                                        &h->sh.weight[0][1], height );
670                 if( h->sh.weight[0][2].weightfn )
671                     h->sh.weight[0][2].weightfn[8>>2]( h->mb.pic.p_fdec[2], FDEC_STRIDE,
672                                                        h->mb.pic.p_fdec[2], FDEC_STRIDE,
673                                                        &h->sh.weight[0][2], height );
674             }
675         }
676
677         x264_macroblock_encode_skip( h );
678         return;
679     }
680     if( h->mb.i_type == B_SKIP )
681     {
682         /* don't do bskip motion compensation if it was already done in macroblock_analyse */
683         if( !h->mb.b_skip_mc )
684             x264_mb_mc( h );
685         x264_macroblock_encode_skip( h );
686         return;
687     }
688
689     if( h->mb.i_type == I_16x16 )
690     {
691         h->mb.b_transform_8x8 = 0;
692
693         for( int p = 0; p < plane_count; p++, i_qp = h->mb.i_chroma_qp )
694             x264_mb_encode_i16x16( h, p, i_qp );
695     }
696     else if( h->mb.i_type == I_8x8 )
697     {
698         h->mb.b_transform_8x8 = 1;
699         /* If we already encoded 3 of the 4 i8x8 blocks, we don't have to do them again. */
700         if( h->mb.i_skip_intra )
701         {
702             h->mc.copy[PIXEL_16x16]( h->mb.pic.p_fdec[0], FDEC_STRIDE, h->mb.pic.i8x8_fdec_buf, 16, 16 );
703             M32( &h->mb.cache.non_zero_count[x264_scan8[ 0]] ) = h->mb.pic.i8x8_nnz_buf[0];
704             M32( &h->mb.cache.non_zero_count[x264_scan8[ 2]] ) = h->mb.pic.i8x8_nnz_buf[1];
705             M32( &h->mb.cache.non_zero_count[x264_scan8[ 8]] ) = h->mb.pic.i8x8_nnz_buf[2];
706             M32( &h->mb.cache.non_zero_count[x264_scan8[10]] ) = h->mb.pic.i8x8_nnz_buf[3];
707             h->mb.i_cbp_luma = h->mb.pic.i8x8_cbp;
708             /* In RD mode, restore the now-overwritten DCT data. */
709             if( h->mb.i_skip_intra == 2 )
710                 h->mc.memcpy_aligned( h->dct.luma8x8, h->mb.pic.i8x8_dct_buf, sizeof(h->mb.pic.i8x8_dct_buf) );
711         }
712         for( int p = 0; p < plane_count; p++, i_qp = h->mb.i_chroma_qp )
713         {
714             for( int i = (p == 0 && h->mb.i_skip_intra) ? 3 : 0 ; i < 4; i++ )
715             {
716                 int i_mode = h->mb.cache.intra4x4_pred_mode[x264_scan8[4*i]];
717                 x264_mb_encode_i8x8( h, p, i, i_qp, i_mode, NULL, 1 );
718             }
719         }
720     }
721     else if( h->mb.i_type == I_4x4 )
722     {
723         h->mb.b_transform_8x8 = 0;
724         /* If we already encoded 15 of the 16 i4x4 blocks, we don't have to do them again. */
725         if( h->mb.i_skip_intra )
726         {
727             h->mc.copy[PIXEL_16x16]( h->mb.pic.p_fdec[0], FDEC_STRIDE, h->mb.pic.i4x4_fdec_buf, 16, 16 );
728             M32( &h->mb.cache.non_zero_count[x264_scan8[ 0]] ) = h->mb.pic.i4x4_nnz_buf[0];
729             M32( &h->mb.cache.non_zero_count[x264_scan8[ 2]] ) = h->mb.pic.i4x4_nnz_buf[1];
730             M32( &h->mb.cache.non_zero_count[x264_scan8[ 8]] ) = h->mb.pic.i4x4_nnz_buf[2];
731             M32( &h->mb.cache.non_zero_count[x264_scan8[10]] ) = h->mb.pic.i4x4_nnz_buf[3];
732             h->mb.i_cbp_luma = h->mb.pic.i4x4_cbp;
733             /* In RD mode, restore the now-overwritten DCT data. */
734             if( h->mb.i_skip_intra == 2 )
735                 h->mc.memcpy_aligned( h->dct.luma4x4, h->mb.pic.i4x4_dct_buf, sizeof(h->mb.pic.i4x4_dct_buf) );
736         }
737         for( int p = 0; p < plane_count; p++, i_qp = h->mb.i_chroma_qp )
738         {
739             for( int i = (p == 0 && h->mb.i_skip_intra) ? 15 : 0 ; i < 16; i++ )
740             {
741                 pixel *p_dst = &h->mb.pic.p_fdec[p][block_idx_xy_fdec[i]];
742                 int i_mode = h->mb.cache.intra4x4_pred_mode[x264_scan8[i]];
743
744                 if( (h->mb.i_neighbour4[i] & (MB_TOPRIGHT|MB_TOP)) == MB_TOP )
745                     /* emulate missing topright samples */
746                     MPIXEL_X4( &p_dst[4-FDEC_STRIDE] ) = PIXEL_SPLAT_X4( p_dst[3-FDEC_STRIDE] );
747
748                 x264_mb_encode_i4x4( h, p, i, i_qp, i_mode, 1 );
749             }
750         }
751     }
752     else    /* Inter MB */
753     {
754         int i_decimate_mb = 0;
755
756         /* Don't repeat motion compensation if it was already done in non-RD transform analysis */
757         if( !h->mb.b_skip_mc )
758             x264_mb_mc( h );
759
760         if( h->mb.b_lossless )
761         {
762             if( h->mb.b_transform_8x8 )
763                 for( int p = 0; p < plane_count; p++ )
764                     for( int i8x8 = 0; i8x8 < 4; i8x8++ )
765                     {
766                         int x = i8x8&1;
767                         int y = i8x8>>1;
768                         nz = h->zigzagf.sub_8x8( h->dct.luma8x8[p*4+i8x8], h->mb.pic.p_fenc[p] + 8*x + 8*y*FENC_STRIDE,
769                                                                            h->mb.pic.p_fdec[p] + 8*x + 8*y*FDEC_STRIDE );
770                         STORE_8x8_NNZ( p, i8x8, nz );
771                         h->mb.i_cbp_luma |= nz << i8x8;
772                     }
773             else
774                 for( int p = 0; p < plane_count; p++ )
775                     for( int i4x4 = 0; i4x4 < 16; i4x4++ )
776                     {
777                         nz = h->zigzagf.sub_4x4( h->dct.luma4x4[p*16+i4x4],
778                                                  h->mb.pic.p_fenc[p]+block_idx_xy_fenc[i4x4],
779                                                  h->mb.pic.p_fdec[p]+block_idx_xy_fdec[i4x4] );
780                         h->mb.cache.non_zero_count[x264_scan8[p*16+i4x4]] = nz;
781                         h->mb.i_cbp_luma |= nz << (i4x4>>2);
782                     }
783         }
784         else if( h->mb.b_transform_8x8 )
785         {
786             ALIGNED_ARRAY_N( dctcoef, dct8x8,[4],[64] );
787             b_decimate &= !h->mb.b_trellis || !h->param.b_cabac; // 8x8 trellis is inherently optimal decimation for CABAC
788
789             for( int p = 0; p < plane_count; p++, i_qp = h->mb.i_chroma_qp )
790             {
791                 CLEAR_16x16_NNZ( p );
792                 h->dctf.sub16x16_dct8( dct8x8, h->mb.pic.p_fenc[p], h->mb.pic.p_fdec[p] );
793                 h->nr_count[1+!!p*2] += h->mb.b_noise_reduction * 4;
794
795                 int plane_cbp = 0;
796                 for( int idx = 0; idx < 4; idx++ )
797                 {
798                     nz = x264_quant_8x8( h, dct8x8[idx], i_qp, ctx_cat_plane[DCT_LUMA_8x8][p], 0, p, idx );
799
800                     if( nz )
801                     {
802                         h->zigzagf.scan_8x8( h->dct.luma8x8[p*4+idx], dct8x8[idx] );
803                         if( b_decimate )
804                         {
805                             int i_decimate_8x8 = h->quantf.decimate_score64( h->dct.luma8x8[p*4+idx] );
806                             i_decimate_mb += i_decimate_8x8;
807                             if( i_decimate_8x8 >= 4 )
808                                 plane_cbp |= 1<<idx;
809                         }
810                         else
811                             plane_cbp |= 1<<idx;
812                     }
813                 }
814
815                 if( i_decimate_mb >= 6 || !b_decimate )
816                 {
817                     h->mb.i_cbp_luma |= plane_cbp;
818                     FOREACH_BIT( idx, 0, plane_cbp )
819                     {
820                         h->quantf.dequant_8x8( dct8x8[idx], h->dequant8_mf[p?CQM_8PC:CQM_8PY], i_qp );
821                         h->dctf.add8x8_idct8( &h->mb.pic.p_fdec[p][8*(idx&1) + 8*(idx>>1)*FDEC_STRIDE], dct8x8[idx] );
822                         STORE_8x8_NNZ( p, idx, 1 );
823                     }
824                 }
825             }
826         }
827         else
828         {
829             ALIGNED_ARRAY_N( dctcoef, dct4x4,[16],[16] );
830             for( int p = 0; p < plane_count; p++, i_qp = h->mb.i_chroma_qp )
831             {
832                 CLEAR_16x16_NNZ( p );
833                 h->dctf.sub16x16_dct( dct4x4, h->mb.pic.p_fenc[p], h->mb.pic.p_fdec[p] );
834
835                 if( h->mb.b_noise_reduction )
836                 {
837                     h->nr_count[0+!!p*2] += 16;
838                     for( int idx = 0; idx < 16; idx++ )
839                         h->quantf.denoise_dct( dct4x4[idx], h->nr_residual_sum[0+!!p*2], h->nr_offset[0+!!p*2], 16 );
840                 }
841
842                 int plane_cbp = 0;
843                 for( int i8x8 = 0; i8x8 < 4; i8x8++ )
844                 {
845                     int i_decimate_8x8 = b_decimate ? 0 : 6;
846                     int nnz8x8 = 0;
847                     if( h->mb.b_trellis )
848                     {
849                         for( int i4x4 = 0; i4x4 < 4; i4x4++ )
850                         {
851                             int idx = i8x8*4+i4x4;
852                             if( x264_quant_4x4_trellis( h, dct4x4[idx], CQM_4PY, i_qp, ctx_cat_plane[DCT_LUMA_4x4][p], 0, !!p, p*16+idx ) )
853                             {
854                                 h->zigzagf.scan_4x4( h->dct.luma4x4[p*16+idx], dct4x4[idx] );
855                                 h->quantf.dequant_4x4( dct4x4[idx], h->dequant4_mf[p?CQM_4PC:CQM_4PY], i_qp );
856                                 if( i_decimate_8x8 < 6 )
857                                     i_decimate_8x8 += h->quantf.decimate_score16( h->dct.luma4x4[p*16+idx] );
858                                 h->mb.cache.non_zero_count[x264_scan8[p*16+idx]] = 1;
859                                 nnz8x8 = 1;
860                             }
861                         }
862                     }
863                     else
864                     {
865                         nnz8x8 = nz = h->quantf.quant_4x4x4( &dct4x4[i8x8*4], h->quant4_mf[CQM_4PY][i_qp], h->quant4_bias[CQM_4PY][i_qp] );
866                         if( nz )
867                         {
868                             FOREACH_BIT( idx, i8x8*4, nz )
869                             {
870                                 h->zigzagf.scan_4x4( h->dct.luma4x4[p*16+idx], dct4x4[idx] );
871                                 h->quantf.dequant_4x4( dct4x4[idx], h->dequant4_mf[p?CQM_4PC:CQM_4PY], i_qp );
872                                 if( i_decimate_8x8 < 6 )
873                                     i_decimate_8x8 += h->quantf.decimate_score16( h->dct.luma4x4[p*16+idx] );
874                                 h->mb.cache.non_zero_count[x264_scan8[p*16+idx]] = 1;
875                             }
876                         }
877                     }
878                     if( nnz8x8 )
879                     {
880                         i_decimate_mb += i_decimate_8x8;
881                         if( i_decimate_8x8 < 4 )
882                             STORE_8x8_NNZ( p, i8x8, 0 );
883                         else
884                             plane_cbp |= 1<<i8x8;
885                     }
886                 }
887
888                 if( i_decimate_mb < 6 )
889                 {
890                     plane_cbp = 0;
891                     CLEAR_16x16_NNZ( p );
892                 }
893                 else
894                 {
895                     h->mb.i_cbp_luma |= plane_cbp;
896                     FOREACH_BIT( i8x8, 0, plane_cbp )
897                     {
898                         h->dctf.add8x8_idct( &h->mb.pic.p_fdec[p][(i8x8&1)*8 + (i8x8>>1)*8*FDEC_STRIDE], &dct4x4[i8x8*4] );
899                     }
900                 }
901             }
902         }
903     }
904
905     /* encode chroma */
906     if( chroma )
907     {
908         if( IS_INTRA( h->mb.i_type ) )
909         {
910             int i_mode = h->mb.i_chroma_pred_mode;
911             if( h->mb.b_lossless )
912                 x264_predict_lossless_chroma( h, i_mode );
913             else
914             {
915                 h->predict_chroma[i_mode]( h->mb.pic.p_fdec[1] );
916                 h->predict_chroma[i_mode]( h->mb.pic.p_fdec[2] );
917             }
918         }
919
920         /* encode the 8x8 blocks */
921         x264_mb_encode_chroma( h, !IS_INTRA( h->mb.i_type ), h->mb.i_chroma_qp );
922     }
923     else
924         h->mb.i_cbp_chroma = 0;
925
926     /* store cbp */
927     int cbp = h->mb.i_cbp_chroma << 4 | h->mb.i_cbp_luma;
928     if( h->param.b_cabac )
929         cbp |= h->mb.cache.non_zero_count[x264_scan8[LUMA_DC    ]] << 8
930             |  h->mb.cache.non_zero_count[x264_scan8[CHROMA_DC+0]] << 9
931             |  h->mb.cache.non_zero_count[x264_scan8[CHROMA_DC+1]] << 10;
932     h->mb.cbp[h->mb.i_mb_xy] = cbp;
933
934     /* Check for P_SKIP
935      * XXX: in the me perhaps we should take x264_mb_predict_mv_pskip into account
936      *      (if multiple mv give same result)*/
937     if( !b_force_no_skip )
938     {
939         if( h->mb.i_type == P_L0 && h->mb.i_partition == D_16x16 &&
940             !(h->mb.i_cbp_luma | h->mb.i_cbp_chroma) &&
941             M32( h->mb.cache.mv[0][x264_scan8[0]] ) == M32( h->mb.cache.pskip_mv )
942             && h->mb.cache.ref[0][x264_scan8[0]] == 0 )
943         {
944             h->mb.i_type = P_SKIP;
945         }
946
947         /* Check for B_SKIP */
948         if( h->mb.i_type == B_DIRECT && !(h->mb.i_cbp_luma | h->mb.i_cbp_chroma) )
949         {
950             h->mb.i_type = B_SKIP;
951         }
952     }
953 }
954
955 void x264_macroblock_encode( x264_t *h )
956 {
957     if( CHROMA444 )
958         x264_macroblock_encode_internal( h, 3, 0 );
959     else
960         x264_macroblock_encode_internal( h, 1, 1 );
961 }
962
963 /*****************************************************************************
964  * x264_macroblock_probe_skip:
965  *  Check if the current MB could be encoded as a [PB]_SKIP
966  *****************************************************************************/
967 static ALWAYS_INLINE int x264_macroblock_probe_skip_internal( x264_t *h, int b_bidir, int plane_count, int chroma )
968 {
969     ALIGNED_ARRAY_N( dctcoef, dct4x4,[8],[16] );
970     ALIGNED_ARRAY_16( dctcoef, dctscan,[16] );
971     ALIGNED_4( int16_t mvp[2] );
972     int i_qp = h->mb.i_qp;
973
974     for( int p = 0; p < plane_count; p++, i_qp = h->mb.i_chroma_qp )
975     {
976         int quant_cat = p ? CQM_4PC : CQM_4PY;
977         if( !b_bidir )
978         {
979             /* Get the MV */
980             mvp[0] = x264_clip3( h->mb.cache.pskip_mv[0], h->mb.mv_min[0], h->mb.mv_max[0] );
981             mvp[1] = x264_clip3( h->mb.cache.pskip_mv[1], h->mb.mv_min[1], h->mb.mv_max[1] );
982
983             /* Motion compensation */
984             h->mc.mc_luma( h->mb.pic.p_fdec[p],    FDEC_STRIDE,
985                            &h->mb.pic.p_fref[0][0][p*4], h->mb.pic.i_stride[p],
986                            mvp[0], mvp[1], 16, 16, &h->sh.weight[0][p] );
987         }
988
989         for( int i8x8 = 0, i_decimate_mb = 0; i8x8 < 4; i8x8++ )
990         {
991             int fenc_offset = (i8x8&1) * 8 + (i8x8>>1) * FENC_STRIDE * 8;
992             int fdec_offset = (i8x8&1) * 8 + (i8x8>>1) * FDEC_STRIDE * 8;
993
994             h->dctf.sub8x8_dct( dct4x4, h->mb.pic.p_fenc[p] + fenc_offset,
995                                         h->mb.pic.p_fdec[p] + fdec_offset );
996
997             if( h->mb.b_noise_reduction )
998                 for( int i4x4 = 0; i4x4 < 4; i4x4++ )
999                     h->quantf.denoise_dct( dct4x4[i4x4], h->nr_residual_sum[0+!!p*2], h->nr_offset[0+!!p*2], 16 );
1000
1001             int nz = h->quantf.quant_4x4x4( dct4x4, h->quant4_mf[quant_cat][i_qp], h->quant4_bias[quant_cat][i_qp] );
1002             FOREACH_BIT( idx, 0, nz )
1003             {
1004                 h->zigzagf.scan_4x4( dctscan, dct4x4[idx] );
1005                 i_decimate_mb += h->quantf.decimate_score16( dctscan );
1006                 if( i_decimate_mb >= 6 )
1007                     return 0;
1008             }
1009         }
1010     }
1011
1012     if( chroma == CHROMA_420 || chroma == CHROMA_422 )
1013     {
1014         i_qp = h->mb.i_chroma_qp;
1015         int chroma422 = chroma == CHROMA_422;
1016         int thresh = chroma422 ? (x264_lambda2_tab[i_qp] + 16) >> 5 : (x264_lambda2_tab[i_qp] + 32) >> 6;
1017         int ssd;
1018         ALIGNED_ARRAY_16( dctcoef, dct_dc,[8] );
1019
1020         if( !b_bidir )
1021         {
1022             /* Special case for mv0, which is (of course) very common in P-skip mode. */
1023             if( M32( mvp ) )
1024                 h->mc.mc_chroma( h->mb.pic.p_fdec[1], h->mb.pic.p_fdec[2], FDEC_STRIDE,
1025                                  h->mb.pic.p_fref[0][0][4], h->mb.pic.i_stride[1],
1026                                  mvp[0], mvp[1]<<chroma422, 8, chroma422?16:8 );
1027             else
1028                 h->mc.load_deinterleave_chroma_fdec( h->mb.pic.p_fdec[1], h->mb.pic.p_fref[0][0][4],
1029                                                      h->mb.pic.i_stride[1], chroma422?16:8 );
1030         }
1031
1032         for( int ch = 0; ch < 2; ch++ )
1033         {
1034             pixel *p_src = h->mb.pic.p_fenc[1+ch];
1035             pixel *p_dst = h->mb.pic.p_fdec[1+ch];
1036
1037             if( !b_bidir && h->sh.weight[0][1+ch].weightfn )
1038                 h->sh.weight[0][1+ch].weightfn[8>>2]( h->mb.pic.p_fdec[1+ch], FDEC_STRIDE,
1039                                                       h->mb.pic.p_fdec[1+ch], FDEC_STRIDE,
1040                                                       &h->sh.weight[0][1+ch], chroma422?16:8 );
1041
1042             /* there is almost never a termination during chroma, but we can't avoid the check entirely */
1043             /* so instead we check SSD and skip the actual check if the score is low enough. */
1044             ssd = h->pixf.ssd[chroma422?PIXEL_8x16:PIXEL_8x8]( p_dst, FDEC_STRIDE, p_src, FENC_STRIDE );
1045             if( ssd < thresh )
1046                 continue;
1047
1048             /* The vast majority of chroma checks will terminate during the DC check or the higher
1049              * threshold check, so we can save time by doing a DC-only DCT. */
1050             if( h->mb.b_noise_reduction )
1051             {
1052                 for( int i = 0; i <= chroma422; i++ )
1053                     h->dctf.sub8x8_dct( &dct4x4[4*i], p_src + 8*i*FENC_STRIDE, p_dst + 8*i*FDEC_STRIDE );
1054
1055                 for( int i4x4 = 0; i4x4 < (chroma422?8:4); i4x4++ )
1056                 {
1057                     h->quantf.denoise_dct( dct4x4[i4x4], h->nr_residual_sum[2], h->nr_offset[2], 16 );
1058                     dct_dc[i4x4] = dct4x4[i4x4][0];
1059                     dct4x4[i4x4][0] = 0;
1060                 }
1061             }
1062             else
1063             {
1064                 if( chroma422 )
1065                     h->dctf.sub8x16_dct_dc( dct_dc, p_src, p_dst );
1066                 else
1067                     h->dctf.sub8x8_dct_dc( dct_dc, p_src, p_dst );
1068             }
1069
1070             for( int i = 0; i <= chroma422; i++ )
1071                 if( h->quantf.quant_2x2_dc( &dct_dc[4*i], h->quant4_mf[CQM_4PC][i_qp+3*chroma422][0] >> 1,
1072                                             h->quant4_bias[CQM_4PC][i_qp+3*chroma422][0] << 1 ) )
1073                     return 0;
1074
1075             /* If there wasn't a termination in DC, we can check against a much higher threshold. */
1076             if( ssd < thresh*4 )
1077                 continue;
1078
1079             if( !h->mb.b_noise_reduction )
1080                 for( int i = 0; i <= chroma422; i++ )
1081                 {
1082                     h->dctf.sub8x8_dct( &dct4x4[4*i], p_src + 8*i*FENC_STRIDE, p_dst + 8*i*FDEC_STRIDE );
1083                     dct4x4[i*4+0][0] = 0;
1084                     dct4x4[i*4+1][0] = 0;
1085                     dct4x4[i*4+2][0] = 0;
1086                     dct4x4[i*4+3][0] = 0;
1087                 }
1088
1089             /* calculate dct coeffs */
1090             for( int i8x8 = 0, i_decimate_mb = 0; i8x8 < (chroma422?2:1); i8x8++ )
1091             {
1092                 int nz = h->quantf.quant_4x4x4( &dct4x4[i8x8*4], h->quant4_mf[CQM_4PC][i_qp], h->quant4_bias[CQM_4PC][i_qp] );
1093                 FOREACH_BIT( idx, i8x8*4, nz )
1094                 {
1095                     h->zigzagf.scan_4x4( dctscan, dct4x4[idx] );
1096                     i_decimate_mb += h->quantf.decimate_score15( dctscan );
1097                     if( i_decimate_mb >= 7 )
1098                         return 0;
1099                 }
1100             }
1101         }
1102     }
1103
1104     h->mb.b_skip_mc = 1;
1105     return 1;
1106 }
1107
1108 int x264_macroblock_probe_skip( x264_t *h, int b_bidir )
1109 {
1110     if( CHROMA_FORMAT == CHROMA_444 )
1111         return x264_macroblock_probe_skip_internal( h, b_bidir, 3, CHROMA_444 );
1112     else if( CHROMA_FORMAT == CHROMA_422 )
1113         return x264_macroblock_probe_skip_internal( h, b_bidir, 1, CHROMA_422 );
1114     else
1115         return x264_macroblock_probe_skip_internal( h, b_bidir, 1, CHROMA_420 );
1116 }
1117
1118 /****************************************************************************
1119  * DCT-domain noise reduction / adaptive deadzone
1120  * from libavcodec
1121  ****************************************************************************/
1122
1123 void x264_noise_reduction_update( x264_t *h )
1124 {
1125     h->nr_offset = h->nr_offset_denoise;
1126     h->nr_residual_sum = h->nr_residual_sum_buf[0];
1127     h->nr_count = h->nr_count_buf[0];
1128     for( int cat = 0; cat < 3 + CHROMA444; cat++ )
1129     {
1130         int dct8x8 = cat&1;
1131         int size = dct8x8 ? 64 : 16;
1132         const uint32_t *weight = dct8x8 ? x264_dct8_weight2_tab : x264_dct4_weight2_tab;
1133
1134         if( h->nr_count[cat] > (dct8x8 ? (1<<16) : (1<<18)) )
1135         {
1136             for( int i = 0; i < size; i++ )
1137                 h->nr_residual_sum[cat][i] >>= 1;
1138             h->nr_count[cat] >>= 1;
1139         }
1140
1141         for( int i = 0; i < size; i++ )
1142             h->nr_offset[cat][i] =
1143                 ((uint64_t)h->param.analyse.i_noise_reduction * h->nr_count[cat]
1144                  + h->nr_residual_sum[cat][i]/2)
1145               / ((uint64_t)h->nr_residual_sum[cat][i] * weight[i]/256 + 1);
1146
1147         /* Don't denoise DC coefficients */
1148         h->nr_offset[cat][0] = 0;
1149     }
1150 }
1151
1152 /*****************************************************************************
1153  * RD only; 4 calls to this do not make up for one macroblock_encode.
1154  * doesn't transform chroma dc.
1155  *****************************************************************************/
1156 static ALWAYS_INLINE void x264_macroblock_encode_p8x8_internal( x264_t *h, int i8, int plane_count, int chroma )
1157 {
1158     int b_decimate = h->mb.b_dct_decimate;
1159     int i_qp = h->mb.i_qp;
1160     int x = i8&1;
1161     int y = i8>>1;
1162     int nz;
1163     int chroma422 = chroma == CHROMA_422;
1164
1165     h->mb.i_cbp_chroma = 0;
1166     h->mb.i_cbp_luma &= ~(1 << i8);
1167
1168     if( !h->mb.b_skip_mc )
1169         x264_mb_mc_8x8( h, i8 );
1170
1171     if( h->mb.b_lossless )
1172     {
1173         for( int p = 0; p < plane_count; p++ )
1174         {
1175             pixel *p_fenc = h->mb.pic.p_fenc[p] + 8*x + 8*y*FENC_STRIDE;
1176             pixel *p_fdec = h->mb.pic.p_fdec[p] + 8*x + 8*y*FDEC_STRIDE;
1177             int nnz8x8 = 0;
1178             if( h->mb.b_transform_8x8 )
1179             {
1180                 nnz8x8 = h->zigzagf.sub_8x8( h->dct.luma8x8[4*p+i8], p_fenc, p_fdec );
1181                 STORE_8x8_NNZ( p, i8, nnz8x8 );
1182             }
1183             else
1184             {
1185                 for( int i4 = i8*4; i4 < i8*4+4; i4++ )
1186                 {
1187                     nz = h->zigzagf.sub_4x4( h->dct.luma4x4[16*p+i4],
1188                                              h->mb.pic.p_fenc[p]+block_idx_xy_fenc[i4],
1189                                              h->mb.pic.p_fdec[p]+block_idx_xy_fdec[i4] );
1190                     h->mb.cache.non_zero_count[x264_scan8[16*p+i4]] = nz;
1191                     nnz8x8 |= nz;
1192                 }
1193             }
1194             h->mb.i_cbp_luma |= nnz8x8 << i8;
1195         }
1196         if( chroma == CHROMA_420 || chroma == CHROMA_422 )
1197         {
1198             for( int ch = 0; ch < 2; ch++ )
1199             {
1200                 dctcoef dc;
1201                 pixel *p_fenc = h->mb.pic.p_fenc[1+ch] + 4*x + (chroma422?8:4)*y*FENC_STRIDE;
1202                 pixel *p_fdec = h->mb.pic.p_fdec[1+ch] + 4*x + (chroma422?8:4)*y*FDEC_STRIDE;
1203
1204                 for( int i4x4 = 0; i4x4 <= chroma422; i4x4++ )
1205                 {
1206                     int offset = chroma422 ? 8*y + 2*i4x4 + x : i8;
1207                     nz = h->zigzagf.sub_4x4ac( h->dct.luma4x4[16+offset+ch*16], p_fenc+4*i4x4*FENC_STRIDE, p_fdec+4*i4x4*FDEC_STRIDE, &dc );
1208                     h->mb.cache.non_zero_count[x264_scan8[16+offset+ch*16]] = nz;
1209                 }
1210             }
1211             h->mb.i_cbp_chroma = 0x02;
1212         }
1213     }
1214     else
1215     {
1216         if( h->mb.b_transform_8x8 )
1217         {
1218             for( int p = 0; p < plane_count; p++, i_qp = h->mb.i_chroma_qp )
1219             {
1220                 int quant_cat = p ? CQM_8PC : CQM_8PY;
1221                 pixel *p_fenc = h->mb.pic.p_fenc[p] + 8*x + 8*y*FENC_STRIDE;
1222                 pixel *p_fdec = h->mb.pic.p_fdec[p] + 8*x + 8*y*FDEC_STRIDE;
1223                 ALIGNED_ARRAY_N( dctcoef, dct8x8,[64] );
1224
1225                 h->dctf.sub8x8_dct8( dct8x8, p_fenc, p_fdec );
1226                 int nnz8x8 = x264_quant_8x8( h, dct8x8, i_qp, ctx_cat_plane[DCT_LUMA_8x8][p], 0, p, i8 );
1227                 if( nnz8x8 )
1228                 {
1229                     h->zigzagf.scan_8x8( h->dct.luma8x8[4*p+i8], dct8x8 );
1230
1231                     if( b_decimate && !h->mb.b_trellis )
1232                         nnz8x8 = 4 <= h->quantf.decimate_score64( h->dct.luma8x8[4*p+i8] );
1233
1234                     if( nnz8x8 )
1235                     {
1236                         h->quantf.dequant_8x8( dct8x8, h->dequant8_mf[quant_cat], i_qp );
1237                         h->dctf.add8x8_idct8( p_fdec, dct8x8 );
1238                         STORE_8x8_NNZ( p, i8, 1 );
1239                         h->mb.i_cbp_luma |= 1 << i8;
1240                     }
1241                     else
1242                         STORE_8x8_NNZ( p, i8, 0 );
1243                 }
1244                 else
1245                     STORE_8x8_NNZ( p, i8, 0 );
1246             }
1247         }
1248         else
1249         {
1250             for( int p = 0; p < plane_count; p++, i_qp = h->mb.i_chroma_qp )
1251             {
1252                 int quant_cat = p ? CQM_4PC : CQM_4PY;
1253                 pixel *p_fenc = h->mb.pic.p_fenc[p] + 8*x + 8*y*FENC_STRIDE;
1254                 pixel *p_fdec = h->mb.pic.p_fdec[p] + 8*x + 8*y*FDEC_STRIDE;
1255                 int i_decimate_8x8 = b_decimate ? 0 : 4;
1256                 ALIGNED_ARRAY_N( dctcoef, dct4x4,[4],[16] );
1257                 int nnz8x8 = 0;
1258
1259                 h->dctf.sub8x8_dct( dct4x4, p_fenc, p_fdec );
1260                 STORE_8x8_NNZ( p, i8, 0 );
1261
1262                 if( h->mb.b_noise_reduction )
1263                     for( int idx = 0; idx < 4; idx++ )
1264                         h->quantf.denoise_dct( dct4x4[idx], h->nr_residual_sum[0+!!p*2], h->nr_offset[0+!!p*2], 16 );
1265
1266                 if( h->mb.b_trellis )
1267                 {
1268                     for( int i4x4 = 0; i4x4 < 4; i4x4++ )
1269                     {
1270                         if( x264_quant_4x4_trellis( h, dct4x4[i4x4], quant_cat, i_qp, ctx_cat_plane[DCT_LUMA_4x4][p], 0, !!p, i8*4+i4x4+p*16 ) )
1271                         {
1272                             h->zigzagf.scan_4x4( h->dct.luma4x4[p*16+i8*4+i4x4], dct4x4[i4x4] );
1273                             h->quantf.dequant_4x4( dct4x4[i4x4], h->dequant4_mf[quant_cat], i_qp );
1274                             if( i_decimate_8x8 < 4 )
1275                                 i_decimate_8x8 += h->quantf.decimate_score16( h->dct.luma4x4[p*16+i8*4+i4x4] );
1276                             h->mb.cache.non_zero_count[x264_scan8[p*16+i8*4+i4x4]] = 1;
1277                             nnz8x8 = 1;
1278                         }
1279                     }
1280                 }
1281                 else
1282                 {
1283                     nnz8x8 = nz = h->quantf.quant_4x4x4( dct4x4, h->quant4_mf[quant_cat][i_qp], h->quant4_bias[quant_cat][i_qp] );
1284                     if( nz )
1285                     {
1286                         FOREACH_BIT( i4x4, 0, nz )
1287                         {
1288                             h->zigzagf.scan_4x4( h->dct.luma4x4[p*16+i8*4+i4x4], dct4x4[i4x4] );
1289                             h->quantf.dequant_4x4( dct4x4[i4x4], h->dequant4_mf[quant_cat], i_qp );
1290                             if( i_decimate_8x8 < 4 )
1291                                 i_decimate_8x8 += h->quantf.decimate_score16( h->dct.luma4x4[p*16+i8*4+i4x4] );
1292                             h->mb.cache.non_zero_count[x264_scan8[p*16+i8*4+i4x4]] = 1;
1293                         }
1294                     }
1295                 }
1296                 if( nnz8x8 )
1297                 {
1298                     /* decimate this 8x8 block */
1299                     if( i_decimate_8x8 < 4 )
1300                         STORE_8x8_NNZ( p, i8, 0 );
1301                     else
1302                     {
1303                         h->dctf.add8x8_idct( p_fdec, dct4x4 );
1304                         h->mb.i_cbp_luma |= 1 << i8;
1305                     }
1306                 }
1307             }
1308         }
1309
1310         if( chroma == CHROMA_420 || chroma == CHROMA_422 )
1311         {
1312             i_qp = h->mb.i_chroma_qp;
1313             for( int ch = 0; ch < 2; ch++ )
1314             {
1315                 ALIGNED_ARRAY_N( dctcoef, dct4x4,[2],[16] );
1316                 pixel *p_fenc = h->mb.pic.p_fenc[1+ch] + 4*x + (chroma422?8:4)*y*FENC_STRIDE;
1317                 pixel *p_fdec = h->mb.pic.p_fdec[1+ch] + 4*x + (chroma422?8:4)*y*FDEC_STRIDE;
1318
1319                 for( int i4x4 = 0; i4x4 <= chroma422; i4x4++ )
1320                 {
1321                     h->dctf.sub4x4_dct( dct4x4[i4x4], p_fenc + 4*i4x4*FENC_STRIDE, p_fdec + 4*i4x4*FDEC_STRIDE );
1322
1323                     if( h->mb.b_noise_reduction )
1324                         h->quantf.denoise_dct( dct4x4[i4x4], h->nr_residual_sum[2], h->nr_offset[2], 16 );
1325                     dct4x4[i4x4][0] = 0;
1326
1327                     if( h->mb.b_trellis )
1328                         nz = x264_quant_4x4_trellis( h, dct4x4[i4x4], CQM_4PC, i_qp, DCT_CHROMA_AC, 0, 1, 0 );
1329                     else
1330                         nz = h->quantf.quant_4x4( dct4x4[i4x4], h->quant4_mf[CQM_4PC][i_qp], h->quant4_bias[CQM_4PC][i_qp] );
1331
1332                     int offset = chroma422 ? ((5*i8) & 0x09) + 2*i4x4 : i8;
1333                     h->mb.cache.non_zero_count[x264_scan8[16+offset+ch*16]] = nz;
1334                     if( nz )
1335                     {
1336                         h->zigzagf.scan_4x4( h->dct.luma4x4[16+offset+ch*16], dct4x4[i4x4] );
1337                         h->quantf.dequant_4x4( dct4x4[i4x4], h->dequant4_mf[CQM_4PC], i_qp );
1338                         h->dctf.add4x4_idct( p_fdec + 4*i4x4*FDEC_STRIDE, dct4x4[i4x4] );
1339                     }
1340                 }
1341             }
1342             h->mb.i_cbp_chroma = 0x02;
1343         }
1344     }
1345 }
1346
1347 void x264_macroblock_encode_p8x8( x264_t *h, int i8 )
1348 {
1349     if( CHROMA444 )
1350         x264_macroblock_encode_p8x8_internal( h, i8, 3, CHROMA_444 );
1351     else if( CHROMA_FORMAT == CHROMA_422 )
1352         x264_macroblock_encode_p8x8_internal( h, i8, 1, CHROMA_422 );
1353     else
1354         x264_macroblock_encode_p8x8_internal( h, i8, 1, CHROMA_420 );
1355 }
1356
1357 /*****************************************************************************
1358  * RD only, luma only (for 4:2:0)
1359  *****************************************************************************/
1360 static ALWAYS_INLINE void x264_macroblock_encode_p4x4_internal( x264_t *h, int i4, int plane_count )
1361 {
1362     int i_qp = h->mb.i_qp;
1363
1364     for( int p = 0; p < plane_count; p++, i_qp = h->mb.i_chroma_qp )
1365     {
1366         int quant_cat = p ? CQM_4PC : CQM_4PY;
1367         pixel *p_fenc = &h->mb.pic.p_fenc[p][block_idx_xy_fenc[i4]];
1368         pixel *p_fdec = &h->mb.pic.p_fdec[p][block_idx_xy_fdec[i4]];
1369         int nz;
1370
1371         /* Don't need motion compensation as this function is only used in qpel-RD, which caches pixel data. */
1372
1373         if( h->mb.b_lossless )
1374         {
1375             nz = h->zigzagf.sub_4x4( h->dct.luma4x4[p*16+i4], p_fenc, p_fdec );
1376             h->mb.cache.non_zero_count[x264_scan8[p*16+i4]] = nz;
1377         }
1378         else
1379         {
1380             ALIGNED_ARRAY_N( dctcoef, dct4x4,[16] );
1381             h->dctf.sub4x4_dct( dct4x4, p_fenc, p_fdec );
1382             nz = x264_quant_4x4( h, dct4x4, i_qp, ctx_cat_plane[DCT_LUMA_4x4][p], 0, p, i4 );
1383             h->mb.cache.non_zero_count[x264_scan8[p*16+i4]] = nz;
1384             if( nz )
1385             {
1386                 h->zigzagf.scan_4x4( h->dct.luma4x4[p*16+i4], dct4x4 );
1387                 h->quantf.dequant_4x4( dct4x4, h->dequant4_mf[quant_cat], i_qp );
1388                 h->dctf.add4x4_idct( p_fdec, dct4x4 );
1389             }
1390         }
1391     }
1392 }
1393
1394 void x264_macroblock_encode_p4x4( x264_t *h, int i8 )
1395 {
1396     if( CHROMA444 )
1397         x264_macroblock_encode_p4x4_internal( h, i8, 3 );
1398     else
1399         x264_macroblock_encode_p4x4_internal( h, i8, 1 );
1400 }