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