]> git.sesse.net Git - x264/blob - encoder/macroblock.c
a353ce71f4d4a8fdaf15680150e5c38607ee81bc
[x264] / encoder / macroblock.c
1 /*****************************************************************************
2  * macroblock.c: h264 encoder library
3  *****************************************************************************
4  * Copyright (C) 2003-2008 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
25 #include "common/common.h"
26 #include "macroblock.h"
27
28 #define ZIG(i,y,x) level[i] = dct[x][y];
29 static inline void zigzag_scan_2x2_dc( int16_t level[4], int16_t dct[2][2] )
30 {
31     ZIG(0,0,0)
32     ZIG(1,0,1)
33     ZIG(2,1,0)
34     ZIG(3,1,1)
35 }
36 #undef ZIG
37
38 /* (ref: JVT-B118)
39  * x264_mb_decimate_score: given dct coeffs it returns a score to see if we could empty this dct coeffs
40  * to 0 (low score means set it to null)
41  * Used in inter macroblock (luma and chroma)
42  *  luma: for a 8x8 block: if score < 4 -> null
43  *        for the complete mb: if score < 6 -> null
44  *  chroma: for the complete mb: if score < 7 -> null
45  */
46 static int x264_mb_decimate_score( int16_t *dct, int i_max )
47 {
48     static const int i_ds_table4[16] = {
49         3,2,2,1,1,1,0,0,0,0,0,0,0,0,0,0 };
50     static const int i_ds_table8[64] = {
51         3,3,3,3,2,2,2,2,2,2,2,2,1,1,1,1,
52         1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,
53         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
54         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
55
56     const int *ds_table = (i_max == 64) ? i_ds_table8 : i_ds_table4;
57     int i_score = 0;
58     int idx = i_max - 1;
59
60     while( idx >= 0 && dct[idx] == 0 )
61         idx--;
62
63     while( idx >= 0 )
64     {
65         int i_run;
66
67         if( (unsigned)(dct[idx--] + 1) > 2 )
68             return 9;
69
70         i_run = 0;
71         while( idx >= 0 && dct[idx] == 0 )
72         {
73             idx--;
74             i_run++;
75         }
76         i_score += ds_table[i_run];
77     }
78
79     return i_score;
80 }
81
82 static ALWAYS_INLINE void x264_quant_4x4( x264_t *h, int16_t dct[4][4], int i_qp, int i_ctxBlockCat, int b_intra )
83 {
84     int i_quant_cat = b_intra ? CQM_4IY : CQM_4PY;
85     if( h->mb.b_trellis )
86         x264_quant_4x4_trellis( h, dct, i_quant_cat, i_qp, i_ctxBlockCat, b_intra );
87     else
88         h->quantf.quant_4x4( dct, h->quant4_mf[i_quant_cat][i_qp], h->quant4_bias[i_quant_cat][i_qp] );
89 }
90
91 static ALWAYS_INLINE void x264_quant_8x8( x264_t *h, int16_t dct[8][8], int i_qp, int b_intra )
92 {
93     int i_quant_cat = b_intra ? CQM_8IY : CQM_8PY;
94     if( h->mb.b_trellis )
95         x264_quant_8x8_trellis( h, dct, i_quant_cat, i_qp, b_intra );
96     else
97         h->quantf.quant_8x8( dct, h->quant8_mf[i_quant_cat][i_qp], h->quant8_bias[i_quant_cat][i_qp] );
98 }
99
100 void x264_mb_encode_i4x4( x264_t *h, int idx, int i_qp )
101 {
102     uint8_t *p_src = &h->mb.pic.p_fenc[0][block_idx_xy_fenc[idx]];
103     uint8_t *p_dst = &h->mb.pic.p_fdec[0][block_idx_xy_fdec[idx]];
104     DECLARE_ALIGNED_16( int16_t dct4x4[4][4] );
105
106     if( h->mb.b_lossless )
107     {
108         h->zigzagf.sub_4x4( h->dct.luma4x4[idx], p_src, p_dst );
109         return;
110     }
111
112     h->dctf.sub4x4_dct( dct4x4, p_src, p_dst );
113
114     x264_quant_4x4( h, dct4x4, i_qp, DCT_LUMA_4x4, 1 );
115
116     if( array_non_zero( dct4x4 ) )
117     {
118         h->zigzagf.scan_4x4( h->dct.luma4x4[idx], dct4x4 );
119         h->quantf.dequant_4x4( dct4x4, h->dequant4_mf[CQM_4IY], i_qp );
120
121         /* output samples to fdec */
122         h->dctf.add4x4_idct( p_dst, dct4x4 );
123     }
124     else
125         memset( h->dct.luma4x4[idx], 0, sizeof(h->dct.luma4x4[idx]));
126 }
127
128 void x264_mb_encode_i8x8( x264_t *h, int idx, int i_qp )
129 {
130     int x = 8 * (idx&1);
131     int y = 8 * (idx>>1);
132     uint8_t *p_src = &h->mb.pic.p_fenc[0][x+y*FENC_STRIDE];
133     uint8_t *p_dst = &h->mb.pic.p_fdec[0][x+y*FDEC_STRIDE];
134     DECLARE_ALIGNED_16( int16_t dct8x8[8][8] );
135
136     h->dctf.sub8x8_dct8( dct8x8, p_src, p_dst );
137
138     x264_quant_8x8( h, dct8x8, i_qp, 1 );
139
140     h->zigzagf.scan_8x8( h->dct.luma8x8[idx], dct8x8 );
141     h->quantf.dequant_8x8( dct8x8, h->dequant8_mf[CQM_8IY], i_qp );
142     h->dctf.add8x8_idct8( p_dst, dct8x8 );
143 }
144
145 static void x264_mb_encode_i16x16( x264_t *h, int i_qp )
146 {
147     uint8_t  *p_src = h->mb.pic.p_fenc[0];
148     uint8_t  *p_dst = h->mb.pic.p_fdec[0];
149
150     DECLARE_ALIGNED_16( int16_t dct4x4[16][4][4] );
151     DECLARE_ALIGNED_16( int16_t dct_dc4x4[4][4] );
152
153     int i;
154
155     if( h->mb.b_lossless )
156     {
157         for( i = 0; i < 16; i++ )
158         {
159             int oe = block_idx_xy_fenc[i];
160             int od = block_idx_xy_fdec[i];
161             h->zigzagf.sub_4x4( h->dct.luma4x4[i], p_src+oe, p_dst+od );
162             dct_dc4x4[0][block_idx_yx_1d[i]] = h->dct.luma4x4[i][0];
163             h->dct.luma4x4[i][0] = 0;
164         }
165         h->zigzagf.scan_4x4( h->dct.luma16x16_dc, dct_dc4x4 );
166         return;
167     }
168
169     h->dctf.sub16x16_dct( dct4x4, p_src, p_dst );
170     for( i = 0; i < 16; i++ )
171     {
172         /* copy dc coeff */
173         dct_dc4x4[0][block_idx_xy_1d[i]] = dct4x4[i][0][0];
174         dct4x4[i][0][0] = 0;
175
176         /* quant/scan/dequant */
177         x264_quant_4x4( h, dct4x4[i], i_qp, DCT_LUMA_AC, 1 );
178
179         h->zigzagf.scan_4x4( h->dct.luma4x4[i], dct4x4[i] );
180         h->quantf.dequant_4x4( dct4x4[i], h->dequant4_mf[CQM_4IY], i_qp );
181     }
182
183     h->dctf.dct4x4dc( dct_dc4x4 );
184     h->quantf.quant_4x4_dc( dct_dc4x4, h->quant4_mf[CQM_4IY][i_qp][0]>>1, h->quant4_bias[CQM_4IY][i_qp][0]<<1 );
185     h->zigzagf.scan_4x4( h->dct.luma16x16_dc, dct_dc4x4 );
186
187     /* output samples to fdec */
188     h->dctf.idct4x4dc( dct_dc4x4 );
189     x264_mb_dequant_4x4_dc( dct_dc4x4, h->dequant4_mf[CQM_4IY], i_qp );  /* XXX not inversed */
190
191     /* calculate dct coeffs */
192     for( i = 0; i < 16; i++ )
193     {
194         /* copy dc coeff */
195         dct4x4[i][0][0] = dct_dc4x4[0][block_idx_xy_1d[i]];
196     }
197     /* put pixels to fdec */
198     h->dctf.add16x16_idct( p_dst, dct4x4 );
199 }
200
201 void x264_mb_encode_8x8_chroma( x264_t *h, int b_inter, int i_qp )
202 {
203     int i, ch;
204     int b_decimate = b_inter && (h->sh.i_type == SLICE_TYPE_B || h->param.analyse.b_dct_decimate);
205
206     for( ch = 0; ch < 2; ch++ )
207     {
208         uint8_t  *p_src = h->mb.pic.p_fenc[1+ch];
209         uint8_t  *p_dst = h->mb.pic.p_fdec[1+ch];
210         int i_decimate_score = 0;
211
212         DECLARE_ALIGNED_16( int16_t dct2x2[2][2]  );
213         DECLARE_ALIGNED_16( int16_t dct4x4[4][4][4] );
214
215         if( h->mb.b_lossless )
216         {
217             for( i = 0; i < 4; i++ )
218             {
219                 int oe = block_idx_x[i]*4 + block_idx_y[i]*4*FENC_STRIDE;
220                 int od = block_idx_x[i]*4 + block_idx_y[i]*4*FDEC_STRIDE;
221                 h->zigzagf.sub_4x4( h->dct.luma4x4[16+i+ch*4], p_src+oe, p_dst+od );
222                 h->dct.chroma_dc[ch][i] = h->dct.luma4x4[16+i+ch*4][0];
223                 h->dct.luma4x4[16+i+ch*4][0] = 0;
224             }
225             continue;
226         }
227
228         h->dctf.sub8x8_dct( dct4x4, p_src, p_dst );
229         /* calculate dct coeffs */
230         for( i = 0; i < 4; i++ )
231         {
232             /* copy dc coeff */
233             dct2x2[i>>1][i&1] = dct4x4[i][0][0];
234             dct4x4[i][0][0] = 0;
235
236             /* no trellis; it doesn't seem to help chroma noticeably */
237             h->quantf.quant_4x4( dct4x4[i], h->quant4_mf[CQM_4IC+b_inter][i_qp], h->quant4_bias[CQM_4IC+b_inter][i_qp] );
238             h->zigzagf.scan_4x4( h->dct.luma4x4[16+i+ch*4], dct4x4[i] );
239
240             if( b_decimate )
241                 i_decimate_score += x264_mb_decimate_score( h->dct.luma4x4[16+i+ch*4]+1, 15 );
242         }
243
244         h->dctf.dct2x2dc( dct2x2 );
245         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 );
246         zigzag_scan_2x2_dc( h->dct.chroma_dc[ch], dct2x2 );
247
248         /* output samples to fdec */
249         h->dctf.idct2x2dc( dct2x2 );
250         x264_mb_dequant_2x2_dc( dct2x2, h->dequant4_mf[CQM_4IC + b_inter], i_qp );  /* XXX not inversed */
251
252         if( b_decimate && i_decimate_score < 7 )
253         {
254             /* Near null chroma 8x8 block so make it null (bits saving) */
255             memset( &h->dct.luma4x4[16+ch*4], 0, 4 * sizeof( *h->dct.luma4x4 ) );
256             if( !array_non_zero( dct2x2 ) )
257                 continue;
258             memset( dct4x4, 0, sizeof( dct4x4 ) );
259         }
260         else
261         {
262             for( i = 0; i < 4; i++ )
263                 h->quantf.dequant_4x4( dct4x4[i], h->dequant4_mf[CQM_4IC + b_inter], i_qp );
264         }
265         dct4x4[0][0][0] = dct2x2[0][0];
266         dct4x4[1][0][0] = dct2x2[0][1];
267         dct4x4[2][0][0] = dct2x2[1][0];
268         dct4x4[3][0][0] = dct2x2[1][1];
269         h->dctf.add8x8_idct( p_dst, dct4x4 );
270     }
271
272     /* coded block pattern */
273     h->mb.i_cbp_chroma = 0;
274     for( i = 0; i < 8; i++ )
275     {
276         int nz = array_non_zero( h->dct.luma4x4[16+i] );
277         h->mb.cache.non_zero_count[x264_scan8[16+i]] = nz;
278         h->mb.i_cbp_chroma |= nz;
279     }
280     if( h->mb.i_cbp_chroma )
281         h->mb.i_cbp_chroma = 2;    /* dc+ac (we can't do only ac) */
282     else if( array_non_zero( h->dct.chroma_dc ) )
283         h->mb.i_cbp_chroma = 1;    /* dc only */
284 }
285
286 static void x264_macroblock_encode_skip( x264_t *h )
287 {
288     h->mb.i_cbp_luma = 0x00;
289     h->mb.i_cbp_chroma = 0x00;
290     memset( h->mb.cache.non_zero_count, 0, X264_SCAN8_SIZE );
291     /* store cbp */
292     h->mb.cbp[h->mb.i_mb_xy] = 0;
293 }
294
295 /*****************************************************************************
296  * x264_macroblock_encode_pskip:
297  *  Encode an already marked skip block
298  *****************************************************************************/
299 static void x264_macroblock_encode_pskip( x264_t *h )
300 {
301     const int mvx = x264_clip3( h->mb.cache.mv[0][x264_scan8[0]][0],
302                                 h->mb.mv_min[0], h->mb.mv_max[0] );
303     const int mvy = x264_clip3( h->mb.cache.mv[0][x264_scan8[0]][1],
304                                 h->mb.mv_min[1], h->mb.mv_max[1] );
305
306     /* don't do pskip motion compensation if it was already done in macroblock_analyse */
307     if( !h->mb.b_skip_mc )
308     {
309         h->mc.mc_luma( h->mb.pic.p_fdec[0],    FDEC_STRIDE,
310                        h->mb.pic.p_fref[0][0], h->mb.pic.i_stride[0],
311                        mvx, mvy, 16, 16 );
312
313         h->mc.mc_chroma( h->mb.pic.p_fdec[1],       FDEC_STRIDE,
314                          h->mb.pic.p_fref[0][0][4], h->mb.pic.i_stride[1],
315                          mvx, mvy, 8, 8 );
316
317         h->mc.mc_chroma( h->mb.pic.p_fdec[2],       FDEC_STRIDE,
318                          h->mb.pic.p_fref[0][0][5], h->mb.pic.i_stride[2],
319                          mvx, mvy, 8, 8 );
320     }
321
322     x264_macroblock_encode_skip( h );
323 }
324
325 /*****************************************************************************
326  * x264_macroblock_encode:
327  *****************************************************************************/
328 void x264_macroblock_encode( x264_t *h )
329 {
330     int i_cbp_dc = 0;
331     int i_qp = h->mb.i_qp;
332     int b_decimate = h->sh.i_type == SLICE_TYPE_B || h->param.analyse.b_dct_decimate;
333     int b_force_no_skip = 0;
334     int i,j,idx;
335     uint8_t nnz8x8[4] = {1,1,1,1};
336
337     if( h->sh.b_mbaff
338         && h->mb.i_mb_xy == h->sh.i_first_mb + h->mb.i_mb_stride
339         && IS_SKIP(h->mb.type[h->sh.i_first_mb]) )
340     {
341         /* The first skip is predicted to be a frame mb pair.
342          * We don't yet support the aff part of mbaff, so force it to non-skip
343          * so that we can pick the aff flag. */
344         b_force_no_skip = 1;
345         if( IS_SKIP(h->mb.i_type) )
346         {
347             if( h->mb.i_type == P_SKIP )
348                 h->mb.i_type = P_L0;
349             else if( h->mb.i_type == B_SKIP )
350                 h->mb.i_type = B_DIRECT;
351         }
352     }
353
354     if( h->mb.i_type == P_SKIP )
355     {
356         /* A bit special */
357         x264_macroblock_encode_pskip( h );
358         return;
359     }
360     if( h->mb.i_type == B_SKIP )
361     {
362         /* don't do bskip motion compensation if it was already done in macroblock_analyse */
363         if( !h->mb.b_skip_mc )
364             x264_mb_mc( h );
365         x264_macroblock_encode_skip( h );
366         return;
367     }
368
369     if( h->mb.i_type == I_16x16 )
370     {
371         const int i_mode = h->mb.i_intra16x16_pred_mode;
372         h->mb.b_transform_8x8 = 0;
373         /* do the right prediction */
374         h->predict_16x16[i_mode]( h->mb.pic.p_fdec[0] );
375
376         /* encode the 16x16 macroblock */
377         x264_mb_encode_i16x16( h, i_qp );
378     }
379     else if( h->mb.i_type == I_8x8 )
380     {
381         DECLARE_ALIGNED_16( uint8_t edge[33] );
382         h->mb.b_transform_8x8 = 1;
383         /* If we already encoded 3 of the 4 i8x8 blocks, we don't have to do them again. */
384         if( h->mb.i_skip_intra )
385         {
386             h->mc.copy[PIXEL_16x16]( h->mb.pic.p_fdec[0], FDEC_STRIDE, h->mb.pic.i8x8_fdec_buf, 16, 16 );
387             /* In RD mode, restore the now-overwritten DCT data. */
388             if( h->mb.i_skip_intra == 2 )
389                 h->mc.memcpy_aligned( h->dct.luma8x8, h->mb.pic.i8x8_dct_buf, sizeof(h->mb.pic.i8x8_dct_buf) );
390         }
391         for( i = h->mb.i_skip_intra ? 3 : 0 ; i < 4; i++ )
392         {
393             uint8_t  *p_dst = &h->mb.pic.p_fdec[0][8 * (i&1) + 8 * (i>>1) * FDEC_STRIDE];
394             int      i_mode = h->mb.cache.intra4x4_pred_mode[x264_scan8[4*i]];
395
396             x264_predict_8x8_filter( p_dst, edge, h->mb.i_neighbour8[i], x264_pred_i4x4_neighbors[i_mode] );
397             h->predict_8x8[i_mode]( p_dst, edge );
398             x264_mb_encode_i8x8( h, i, i_qp );
399         }
400         for( i = 0; i < 4; i++ )
401             nnz8x8[i] = array_non_zero( h->dct.luma8x8[i] );
402     }
403     else if( h->mb.i_type == I_4x4 )
404     {
405         h->mb.b_transform_8x8 = 0;
406         /* If we already encoded 15 of the 16 i4x4 blocks, we don't have to do them again. */
407         if( h->mb.i_skip_intra )
408         {
409             h->mc.copy[PIXEL_16x16]( h->mb.pic.p_fdec[0], FDEC_STRIDE, h->mb.pic.i4x4_fdec_buf, 16, 16 );
410             /* In RD mode, restore the now-overwritten DCT data. */
411             if( h->mb.i_skip_intra == 2 )
412                 h->mc.memcpy_aligned( h->dct.luma4x4, h->mb.pic.i4x4_dct_buf, sizeof(h->mb.pic.i4x4_dct_buf) );
413         }
414         for( i = h->mb.i_skip_intra ? 15 : 0 ; i < 16; i++ )
415         {
416             uint8_t  *p_dst = &h->mb.pic.p_fdec[0][block_idx_xy_fdec[i]];
417             int      i_mode = h->mb.cache.intra4x4_pred_mode[x264_scan8[i]];
418
419             if( (h->mb.i_neighbour4[i] & (MB_TOPRIGHT|MB_TOP)) == MB_TOP )
420                 /* emulate missing topright samples */
421                 *(uint32_t*) &p_dst[4-FDEC_STRIDE] = p_dst[3-FDEC_STRIDE] * 0x01010101U;
422
423             h->predict_4x4[i_mode]( p_dst );
424             x264_mb_encode_i4x4( h, i, i_qp );
425         }
426     }
427     else    /* Inter MB */
428     {
429         int i8x8, i4x4;
430         int i_decimate_mb = 0;
431
432         /* Don't repeat motion compensation if it was already done in non-RD transform analysis */
433         if( !h->mb.b_skip_mc )
434             x264_mb_mc( h );
435
436         if( h->mb.b_lossless )
437         {
438             for( i4x4 = 0; i4x4 < 16; i4x4++ )
439             {
440                 h->zigzagf.sub_4x4( h->dct.luma4x4[i4x4],
441                                     h->mb.pic.p_fenc[0]+block_idx_xy_fenc[i4x4],
442                                     h->mb.pic.p_fdec[0]+block_idx_xy_fdec[i4x4] );
443             }
444         }
445         else if( h->mb.b_transform_8x8 )
446         {
447             DECLARE_ALIGNED_16( int16_t dct8x8[4][8][8] );
448             b_decimate &= !h->mb.b_trellis; // 8x8 trellis is inherently optimal decimation
449             h->dctf.sub16x16_dct8( dct8x8, h->mb.pic.p_fenc[0], h->mb.pic.p_fdec[0] );
450             h->nr_count[1] += h->mb.b_noise_reduction * 4;
451
452             for( idx = 0; idx < 4; idx++ )
453             {
454                 if( h->mb.b_noise_reduction )
455                     h->quantf.denoise_dct( *dct8x8[idx], h->nr_residual_sum[1], h->nr_offset[1], 64 );
456                 x264_quant_8x8( h, dct8x8[idx], i_qp, 0 );
457
458                 h->zigzagf.scan_8x8( h->dct.luma8x8[idx], dct8x8[idx] );
459
460                 if( b_decimate )
461                 {
462                     int i_decimate_8x8 = x264_mb_decimate_score( h->dct.luma8x8[idx], 64 );
463                     i_decimate_mb += i_decimate_8x8;
464                     if( i_decimate_8x8 < 4 )
465                         nnz8x8[idx] = 0;
466                 }
467                 else
468                     nnz8x8[idx] = array_non_zero( dct8x8[idx] );
469             }
470
471             if( i_decimate_mb < 6 && b_decimate )
472                 *(uint32_t*)nnz8x8 = 0;
473             else
474             {
475                 for( idx = 0; idx < 4; idx++ )
476                     if( nnz8x8[idx] )
477                     {
478                         h->quantf.dequant_8x8( dct8x8[idx], h->dequant8_mf[CQM_8PY], i_qp );
479                         h->dctf.add8x8_idct8( &h->mb.pic.p_fdec[0][(idx&1)*8 + (idx>>1)*8*FDEC_STRIDE], dct8x8[idx] );
480                     }
481             }
482         }
483         else
484         {
485             DECLARE_ALIGNED_16( int16_t dct4x4[16][4][4] );
486             h->dctf.sub16x16_dct( dct4x4, h->mb.pic.p_fenc[0], h->mb.pic.p_fdec[0] );
487             h->nr_count[0] += h->mb.b_noise_reduction * 16;
488
489             for( i8x8 = 0; i8x8 < 4; i8x8++ )
490             {
491                 int i_decimate_8x8;
492
493                 /* encode one 4x4 block */
494                 i_decimate_8x8 = 0;
495                 for( i4x4 = 0; i4x4 < 4; i4x4++ )
496                 {
497                     idx = i8x8 * 4 + i4x4;
498
499                     if( h->mb.b_noise_reduction )
500                         h->quantf.denoise_dct( *dct4x4[idx], h->nr_residual_sum[0], h->nr_offset[0], 16 );
501                     x264_quant_4x4( h, dct4x4[idx], i_qp, DCT_LUMA_4x4, 0 );
502
503                     h->zigzagf.scan_4x4( h->dct.luma4x4[idx], dct4x4[idx] );
504
505                     if( b_decimate && i_decimate_8x8 <= 6 )
506                         i_decimate_8x8 += x264_mb_decimate_score( h->dct.luma4x4[idx], 16 );
507                 }
508
509                 /* decimate this 8x8 block */
510                 i_decimate_mb += i_decimate_8x8;
511                 if( i_decimate_8x8 < 4 && b_decimate )
512                     nnz8x8[i8x8] = 0;
513             }
514
515             if( i_decimate_mb < 6 && b_decimate )
516                 *(uint32_t*)nnz8x8 = 0;
517             else
518             {
519                 for( i8x8 = 0; i8x8 < 4; i8x8++ )
520                     if( nnz8x8[i8x8] )
521                     {
522                         for( i = 0; i < 4; i++ )
523                             h->quantf.dequant_4x4( dct4x4[i8x8*4+i], h->dequant4_mf[CQM_4PY], i_qp );
524                         h->dctf.add8x8_idct( &h->mb.pic.p_fdec[0][(i8x8&1)*8 + (i8x8>>1)*8*FDEC_STRIDE], &dct4x4[i8x8*4] );
525                     }
526             }
527         }
528     }
529
530     /* encode chroma */
531     if( IS_INTRA( h->mb.i_type ) )
532     {
533         const int i_mode = h->mb.i_chroma_pred_mode;
534         h->predict_8x8c[i_mode]( h->mb.pic.p_fdec[1] );
535         h->predict_8x8c[i_mode]( h->mb.pic.p_fdec[2] );
536     }
537
538     /* encode the 8x8 blocks */
539     x264_mb_encode_8x8_chroma( h, !IS_INTRA( h->mb.i_type ), h->mb.i_chroma_qp );
540
541     /* coded block pattern and non_zero_count */
542     h->mb.i_cbp_luma = 0x00;
543     if( h->mb.i_type == I_16x16 )
544     {
545         for( i = 0; i < 16; i++ )
546         {
547             int nz = array_non_zero( h->dct.luma4x4[i] );
548             h->mb.cache.non_zero_count[x264_scan8[i]] = nz;
549             h->mb.i_cbp_luma |= nz;
550         }
551         h->mb.i_cbp_luma *= 0xf;
552     }
553     else
554     {
555         for( i = 0; i < 4; i++)
556         {
557             if(!nnz8x8[i])
558             {
559                 *(uint16_t*)&h->mb.cache.non_zero_count[x264_scan8[0+i*4]] = 0;
560                 *(uint16_t*)&h->mb.cache.non_zero_count[x264_scan8[2+i*4]] = 0;
561             }
562             else if( h->mb.b_transform_8x8 )
563             {
564                 *(uint16_t*)&h->mb.cache.non_zero_count[x264_scan8[0+4*i]] = nnz8x8[i] * 0x0101;
565                 *(uint16_t*)&h->mb.cache.non_zero_count[x264_scan8[2+4*i]] = nnz8x8[i] * 0x0101;
566                 h->mb.i_cbp_luma |= nnz8x8[i] << i;
567             }
568             else
569             {
570                 int nz, cbp = 0;
571                 for( j = 0; j < 4; j++ )
572                 {
573                     nz = array_non_zero( h->dct.luma4x4[j+4*i] );
574                     h->mb.cache.non_zero_count[x264_scan8[j+4*i]] = nz;
575                     cbp |= nz;
576                 }
577                 h->mb.i_cbp_luma |= cbp << i;
578             }
579         }
580     }
581
582     if( h->param.b_cabac )
583     {
584         i_cbp_dc = ( h->mb.i_type == I_16x16 && array_non_zero( h->dct.luma16x16_dc ) )
585                  | array_non_zero( h->dct.chroma_dc[0] ) << 1
586                  | array_non_zero( h->dct.chroma_dc[1] ) << 2;
587     }
588
589     /* store cbp */
590     h->mb.cbp[h->mb.i_mb_xy] = (i_cbp_dc << 8) | (h->mb.i_cbp_chroma << 4) | h->mb.i_cbp_luma;
591
592     /* Check for P_SKIP
593      * XXX: in the me perhaps we should take x264_mb_predict_mv_pskip into account
594      *      (if multiple mv give same result)*/
595     if( !b_force_no_skip )
596     {
597         if( h->mb.i_type == P_L0 && h->mb.i_partition == D_16x16 &&
598             !(h->mb.i_cbp_luma | h->mb.i_cbp_chroma) &&
599             *(uint32_t*)h->mb.cache.mv[0][x264_scan8[0]] == *(uint32_t*)h->mb.cache.pskip_mv
600             && h->mb.cache.ref[0][x264_scan8[0]] == 0 )
601         {
602             h->mb.i_type = P_SKIP;
603         }
604
605         /* Check for B_SKIP */
606         if( h->mb.i_type == B_DIRECT && !(h->mb.i_cbp_luma | h->mb.i_cbp_chroma) )
607         {
608             h->mb.i_type = B_SKIP;
609         }
610     }
611 }
612
613 /*****************************************************************************
614  * x264_macroblock_probe_skip:
615  *  Check if the current MB could be encoded as a [PB]_SKIP (it supposes you use
616  *  the previous QP
617  *****************************************************************************/
618 int x264_macroblock_probe_skip( x264_t *h, const int b_bidir )
619 {
620     DECLARE_ALIGNED_16( int16_t dct4x4[4][4][4] );
621     DECLARE_ALIGNED_16( int16_t dct2x2[2][2] );
622     DECLARE_ALIGNED_16( int16_t dctscan[16] );
623
624     int i_qp = h->mb.i_qp;
625     int mvp[2];
626     int ch, thresh;
627
628     int i8x8, i4x4;
629     int i_decimate_mb;
630
631     if( !b_bidir )
632     {
633         /* Get the MV */
634         mvp[0] = x264_clip3( h->mb.cache.pskip_mv[0], h->mb.mv_min[0], h->mb.mv_max[0] );
635         mvp[1] = x264_clip3( h->mb.cache.pskip_mv[1], h->mb.mv_min[1], h->mb.mv_max[1] );
636
637         /* Motion compensation */
638         h->mc.mc_luma( h->mb.pic.p_fdec[0],    FDEC_STRIDE,
639                        h->mb.pic.p_fref[0][0], h->mb.pic.i_stride[0],
640                        mvp[0], mvp[1], 16, 16 );
641     }
642
643     for( i8x8 = 0, i_decimate_mb = 0; i8x8 < 4; i8x8++ )
644     {
645         int fenc_offset = (i8x8&1) * 8 + (i8x8>>1) * FENC_STRIDE * 8;
646         int fdec_offset = (i8x8&1) * 8 + (i8x8>>1) * FDEC_STRIDE * 8;
647         /* get luma diff */
648         h->dctf.sub8x8_dct( dct4x4, h->mb.pic.p_fenc[0] + fenc_offset,
649                                     h->mb.pic.p_fdec[0] + fdec_offset );
650         /* encode one 4x4 block */
651         for( i4x4 = 0; i4x4 < 4; i4x4++ )
652         {
653             h->quantf.quant_4x4( dct4x4[i4x4], h->quant4_mf[CQM_4PY][i_qp], h->quant4_bias[CQM_4PY][i_qp] );
654             if( !array_non_zero(dct4x4[i4x4]) )
655                 continue;
656             h->zigzagf.scan_4x4( dctscan, dct4x4[i4x4] );
657             i_decimate_mb += x264_mb_decimate_score( dctscan, 16 );
658             if( i_decimate_mb >= 6 )
659                 return 0;
660         }
661     }
662
663     /* encode chroma */
664     i_qp = h->mb.i_chroma_qp;
665     thresh = (x264_lambda2_tab[i_qp] + 32) >> 6;
666
667     for( ch = 0; ch < 2; ch++ )
668     {
669         uint8_t  *p_src = h->mb.pic.p_fenc[1+ch];
670         uint8_t  *p_dst = h->mb.pic.p_fdec[1+ch];
671
672         if( !b_bidir )
673         {
674             h->mc.mc_chroma( h->mb.pic.p_fdec[1+ch],       FDEC_STRIDE,
675                              h->mb.pic.p_fref[0][0][4+ch], h->mb.pic.i_stride[1+ch],
676                              mvp[0], mvp[1], 8, 8 );
677         }
678
679         /* there is almost never a termination during chroma, but we can't avoid the check entirely */
680         /* so instead we check SSD and skip the actual check if the score is low enough. */
681         if( h->pixf.ssd[PIXEL_8x8]( p_dst, FDEC_STRIDE, p_src, FENC_STRIDE ) < thresh )
682             continue;
683
684         h->dctf.sub8x8_dct( dct4x4, p_src, p_dst );
685
686         /* calculate dct DC */
687         dct2x2[0][0] = dct4x4[0][0][0];
688         dct2x2[0][1] = dct4x4[1][0][0];
689         dct2x2[1][0] = dct4x4[2][0][0];
690         dct2x2[1][1] = dct4x4[3][0][0];
691         h->dctf.dct2x2dc( dct2x2 );
692         h->quantf.quant_2x2_dc( dct2x2, h->quant4_mf[CQM_4PC][i_qp][0]>>1, h->quant4_bias[CQM_4PC][i_qp][0]<<1 );
693         if( array_non_zero(dct2x2) )
694             return 0;
695
696         /* calculate dct coeffs */
697         for( i4x4 = 0, i_decimate_mb = 0; i4x4 < 4; i4x4++ )
698         {
699             h->quantf.quant_4x4( dct4x4[i4x4], h->quant4_mf[CQM_4PC][i_qp], h->quant4_bias[CQM_4PC][i_qp] );
700             if( !array_non_zero(dct4x4[i4x4]) )
701                 continue;
702             h->zigzagf.scan_4x4( dctscan, dct4x4[i4x4] );
703             i_decimate_mb += x264_mb_decimate_score( dctscan+1, 15 );
704             if( i_decimate_mb >= 7 )
705                 return 0;
706         }
707     }
708
709     h->mb.b_skip_mc = 1;
710     return 1;
711 }
712
713 /****************************************************************************
714  * DCT-domain noise reduction / adaptive deadzone
715  * from libavcodec
716  ****************************************************************************/
717
718 void x264_noise_reduction_update( x264_t *h )
719 {
720     int cat, i;
721     for( cat = 0; cat < 2; cat++ )
722     {
723         int size = cat ? 64 : 16;
724         const uint16_t *weight = cat ? x264_dct8_weight2_tab : x264_dct4_weight2_tab;
725
726         if( h->nr_count[cat] > (cat ? (1<<16) : (1<<18)) )
727         {
728             for( i = 0; i < size; i++ )
729                 h->nr_residual_sum[cat][i] >>= 1;
730             h->nr_count[cat] >>= 1;
731         }
732
733         for( i = 0; i < size; i++ )
734             h->nr_offset[cat][i] =
735                 ((uint64_t)h->param.analyse.i_noise_reduction * h->nr_count[cat]
736                  + h->nr_residual_sum[cat][i]/2)
737               / ((uint64_t)h->nr_residual_sum[cat][i] * weight[i]/256 + 1);
738     }
739 }
740
741 /*****************************************************************************
742  * RD only; 4 calls to this do not make up for one macroblock_encode.
743  * doesn't transform chroma dc.
744  *****************************************************************************/
745 void x264_macroblock_encode_p8x8( x264_t *h, int i8 )
746 {
747     int i_qp = h->mb.i_qp;
748     uint8_t *p_fenc = h->mb.pic.p_fenc[0] + (i8&1)*8 + (i8>>1)*8*FENC_STRIDE;
749     uint8_t *p_fdec = h->mb.pic.p_fdec[0] + (i8&1)*8 + (i8>>1)*8*FDEC_STRIDE;
750     int b_decimate = h->sh.i_type == SLICE_TYPE_B || h->param.analyse.b_dct_decimate;
751     int nnz8x8 = 0;
752     int ch;
753
754     x264_mb_mc_8x8( h, i8 );
755
756     if( h->mb.b_lossless )
757     {
758         int i4;
759         for( i4 = i8*4; i4 < i8*4+4; i4++ )
760         {
761             h->zigzagf.sub_4x4( h->dct.luma4x4[i4],
762                                 h->mb.pic.p_fenc[0]+block_idx_xy_fenc[i4],
763                                 h->mb.pic.p_fdec[0]+block_idx_xy_fdec[i4] );
764             nnz8x8 |= array_non_zero( h->dct.luma4x4[i4] );
765         }
766         for( ch = 0; ch < 2; ch++ )
767         {
768             p_fenc = h->mb.pic.p_fenc[1+ch] + (i8&1)*4 + (i8>>1)*4*FENC_STRIDE;
769             p_fdec = h->mb.pic.p_fdec[1+ch] + (i8&1)*4 + (i8>>1)*4*FDEC_STRIDE;
770             h->zigzagf.sub_4x4( h->dct.luma4x4[16+i8+ch*4], p_fenc, p_fdec );
771             h->dct.luma4x4[16+i8+ch*4][0] = 0;
772         }
773     }
774     else
775     {
776         if( h->mb.b_transform_8x8 )
777         {
778             DECLARE_ALIGNED_16( int16_t dct8x8[8][8] );
779             h->dctf.sub8x8_dct8( dct8x8, p_fenc, p_fdec );
780             x264_quant_8x8( h, dct8x8, i_qp, 0 );
781             h->zigzagf.scan_8x8( h->dct.luma8x8[i8], dct8x8 );
782
783             if( b_decimate && !h->mb.b_trellis )
784                 nnz8x8 = 4 <= x264_mb_decimate_score( h->dct.luma8x8[i8], 64 );
785             else
786                 nnz8x8 = array_non_zero( dct8x8 );
787
788             if( nnz8x8 )
789             {
790                 h->quantf.dequant_8x8( dct8x8, h->dequant8_mf[CQM_8PY], i_qp );
791                 h->dctf.add8x8_idct8( p_fdec, dct8x8 );
792             }
793         }
794         else
795         {
796             int i4;
797             DECLARE_ALIGNED_16( int16_t dct4x4[4][4][4] );
798             h->dctf.sub8x8_dct( dct4x4, p_fenc, p_fdec );
799             for( i4 = 0; i4 < 4; i4++ )
800                 x264_quant_4x4( h, dct4x4[i4], i_qp, DCT_LUMA_4x4, 0 );
801
802             for( i4 = 0; i4 < 4; i4++ )
803                 h->zigzagf.scan_4x4( h->dct.luma4x4[i8*4+i4], dct4x4[i4] );
804
805             if( b_decimate )
806             {
807                 int i_decimate_8x8 = 0;
808                 for( i4 = 0; i4 < 4 && i_decimate_8x8 < 4; i4++ )
809                     i_decimate_8x8 += x264_mb_decimate_score( h->dct.luma4x4[i8*4+i4], 16 );
810                 nnz8x8 = 4 <= i_decimate_8x8;
811             }
812             else
813                 nnz8x8 = array_non_zero( dct4x4 );
814
815             if( nnz8x8 )
816             {
817                 for( i4 = 0; i4 < 4; i4++ )
818                     h->quantf.dequant_4x4( dct4x4[i4], h->dequant4_mf[CQM_4PY], i_qp );
819                 h->dctf.add8x8_idct( p_fdec, dct4x4 );
820             }
821         }
822
823         i_qp = h->mb.i_chroma_qp;
824
825         for( ch = 0; ch < 2; ch++ )
826         {
827             DECLARE_ALIGNED_16( int16_t dct4x4[4][4] );
828             p_fenc = h->mb.pic.p_fenc[1+ch] + (i8&1)*4 + (i8>>1)*4*FENC_STRIDE;
829             p_fdec = h->mb.pic.p_fdec[1+ch] + (i8&1)*4 + (i8>>1)*4*FDEC_STRIDE;
830
831             h->dctf.sub4x4_dct( dct4x4, p_fenc, p_fdec );
832             h->quantf.quant_4x4( dct4x4, h->quant4_mf[CQM_4PC][i_qp], h->quant4_bias[CQM_4PC][i_qp] );
833             h->zigzagf.scan_4x4( h->dct.luma4x4[16+i8+ch*4], dct4x4 );
834             h->dct.luma4x4[16+i8+ch*4][0] = 0;
835             if( array_non_zero( dct4x4 ) )
836             {
837                 h->quantf.dequant_4x4( dct4x4, h->dequant4_mf[CQM_4PC], i_qp );
838                 h->dctf.add4x4_idct( p_fdec, dct4x4 );
839             }
840         }
841     }
842     h->mb.i_cbp_luma &= ~(1 << i8);
843     h->mb.i_cbp_luma |= nnz8x8 << i8;
844     h->mb.i_cbp_chroma = 0x02;
845 }