]> git.sesse.net Git - x264/blob - common/macroblock.c
Add mb_info API for signalling constant macroblocks
[x264] / common / macroblock.c
1 /*****************************************************************************
2  * macroblock.c: macroblock common functions
3  *****************************************************************************
4  * Copyright (C) 2003-2012 x264 project
5  *
6  * Authors: Fiona Glaser <fiona@x264.com>
7  *          Laurent Aimar <fenrir@via.ecp.fr>
8  *          Loren Merritt <lorenm@u.washington.edu>
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.h"
30 #include "encoder/me.h"
31
32 #define MC_LUMA(list,p) \
33     h->mc.mc_luma( &h->mb.pic.p_fdec[p][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE, \
34                    &h->mb.pic.p_fref[list][i_ref][p*4], h->mb.pic.i_stride[p], \
35                    mvx, mvy, 4*width, 4*height, \
36                    list ? x264_weight_none : &h->sh.weight[i_ref][p] );
37
38 static NOINLINE void x264_mb_mc_0xywh( x264_t *h, int x, int y, int width, int height )
39 {
40     int i8    = x264_scan8[0]+x+8*y;
41     int i_ref = h->mb.cache.ref[0][i8];
42     int mvx   = x264_clip3( h->mb.cache.mv[0][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] ) + 4*4*x;
43     int mvy   = x264_clip3( h->mb.cache.mv[0][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] ) + 4*4*y;
44
45     MC_LUMA( 0, 0 );
46
47     if( CHROMA444 )
48     {
49         MC_LUMA( 0, 1 );
50         MC_LUMA( 0, 2 );
51     }
52     else
53     {
54         int v_shift = CHROMA_V_SHIFT;
55         // Chroma in 4:2:0 is offset if MCing from a field of opposite parity
56         if( v_shift & MB_INTERLACED & i_ref )
57             mvy += (h->mb.i_mb_y & 1)*4 - 2;
58
59         int offset = (4*FDEC_STRIDE>>v_shift)*y + 2*x;
60         height = 4*height >> v_shift;
61
62         h->mc.mc_chroma( &h->mb.pic.p_fdec[1][offset],
63                          &h->mb.pic.p_fdec[2][offset], FDEC_STRIDE,
64                          h->mb.pic.p_fref[0][i_ref][4], h->mb.pic.i_stride[1],
65                          mvx, 2*mvy>>v_shift, 2*width, height );
66
67         if( h->sh.weight[i_ref][1].weightfn )
68             h->sh.weight[i_ref][1].weightfn[width>>1]( &h->mb.pic.p_fdec[1][offset], FDEC_STRIDE,
69                                                        &h->mb.pic.p_fdec[1][offset], FDEC_STRIDE,
70                                                        &h->sh.weight[i_ref][1], height );
71         if( h->sh.weight[i_ref][2].weightfn )
72             h->sh.weight[i_ref][2].weightfn[width>>1]( &h->mb.pic.p_fdec[2][offset], FDEC_STRIDE,
73                                                        &h->mb.pic.p_fdec[2][offset], FDEC_STRIDE,
74                                                        &h->sh.weight[i_ref][2], height );
75     }
76 }
77 static NOINLINE void x264_mb_mc_1xywh( x264_t *h, int x, int y, int width, int height )
78 {
79     int i8    = x264_scan8[0]+x+8*y;
80     int i_ref = h->mb.cache.ref[1][i8];
81     int mvx   = x264_clip3( h->mb.cache.mv[1][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] ) + 4*4*x;
82     int mvy   = x264_clip3( h->mb.cache.mv[1][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] ) + 4*4*y;
83
84     MC_LUMA( 1, 0 );
85
86     if( CHROMA444 )
87     {
88         MC_LUMA( 1, 1 );
89         MC_LUMA( 1, 2 );
90     }
91     else
92     {
93         int v_shift = CHROMA_V_SHIFT;
94         if( v_shift & MB_INTERLACED & i_ref )
95             mvy += (h->mb.i_mb_y & 1)*4 - 2;
96
97         int offset = (4*FDEC_STRIDE>>v_shift)*y + 2*x;
98         h->mc.mc_chroma( &h->mb.pic.p_fdec[1][offset],
99                          &h->mb.pic.p_fdec[2][offset], FDEC_STRIDE,
100                          h->mb.pic.p_fref[1][i_ref][4], h->mb.pic.i_stride[1],
101                          mvx, 2*mvy>>v_shift, 2*width, 4*height>>v_shift );
102     }
103 }
104
105 #define MC_LUMA_BI(p) \
106     src0 = h->mc.get_ref( tmp0, &i_stride0, &h->mb.pic.p_fref[0][i_ref0][p*4], h->mb.pic.i_stride[p], \
107                           mvx0, mvy0, 4*width, 4*height, x264_weight_none ); \
108     src1 = h->mc.get_ref( tmp1, &i_stride1, &h->mb.pic.p_fref[1][i_ref1][p*4], h->mb.pic.i_stride[p], \
109                           mvx1, mvy1, 4*width, 4*height, x264_weight_none ); \
110     h->mc.avg[i_mode]( &h->mb.pic.p_fdec[p][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE, \
111                        src0, i_stride0, src1, i_stride1, weight );
112
113 static NOINLINE void x264_mb_mc_01xywh( x264_t *h, int x, int y, int width, int height )
114 {
115     int i8 = x264_scan8[0]+x+8*y;
116     int i_ref0 = h->mb.cache.ref[0][i8];
117     int i_ref1 = h->mb.cache.ref[1][i8];
118     int weight = h->mb.bipred_weight[i_ref0][i_ref1];
119     int mvx0   = x264_clip3( h->mb.cache.mv[0][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] ) + 4*4*x;
120     int mvx1   = x264_clip3( h->mb.cache.mv[1][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] ) + 4*4*x;
121     int mvy0   = x264_clip3( h->mb.cache.mv[0][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] ) + 4*4*y;
122     int mvy1   = x264_clip3( h->mb.cache.mv[1][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] ) + 4*4*y;
123     int i_mode = x264_size2pixel[height][width];
124     intptr_t i_stride0 = 16, i_stride1 = 16;
125     ALIGNED_ARRAY_16( pixel, tmp0,[16*16] );
126     ALIGNED_ARRAY_16( pixel, tmp1,[16*16] );
127     pixel *src0, *src1;
128
129     MC_LUMA_BI( 0 );
130
131     if( CHROMA444 )
132     {
133         MC_LUMA_BI( 1 );
134         MC_LUMA_BI( 2 );
135     }
136     else
137     {
138         int v_shift = CHROMA_V_SHIFT;
139         if( v_shift & MB_INTERLACED & i_ref0 )
140             mvy0 += (h->mb.i_mb_y & 1)*4 - 2;
141         if( v_shift & MB_INTERLACED & i_ref1 )
142             mvy1 += (h->mb.i_mb_y & 1)*4 - 2;
143
144         h->mc.mc_chroma( tmp0, tmp0+8, 16, h->mb.pic.p_fref[0][i_ref0][4], h->mb.pic.i_stride[1],
145                          mvx0, 2*mvy0>>v_shift, 2*width, 4*height>>v_shift );
146         h->mc.mc_chroma( tmp1, tmp1+8, 16, h->mb.pic.p_fref[1][i_ref1][4], h->mb.pic.i_stride[1],
147                          mvx1, 2*mvy1>>v_shift, 2*width, 4*height>>v_shift );
148
149         int chromapix = h->luma2chroma_pixel[i_mode];
150         int offset = (4*FDEC_STRIDE>>v_shift)*y + 2*x;
151         h->mc.avg[chromapix]( &h->mb.pic.p_fdec[1][offset], FDEC_STRIDE, tmp0,   16, tmp1,   16, weight );
152         h->mc.avg[chromapix]( &h->mb.pic.p_fdec[2][offset], FDEC_STRIDE, tmp0+8, 16, tmp1+8, 16, weight );
153     }
154 }
155
156 #undef MC_LUMA
157 #undef MC_LUMA_BI
158
159 void x264_mb_mc_8x8( x264_t *h, int i8 )
160 {
161     int x = 2*(i8&1);
162     int y = 2*(i8>>1);
163
164     if( h->sh.i_type == SLICE_TYPE_P )
165     {
166         switch( h->mb.i_sub_partition[i8] )
167         {
168             case D_L0_8x8:
169                 x264_mb_mc_0xywh( h, x, y, 2, 2 );
170                 break;
171             case D_L0_8x4:
172                 x264_mb_mc_0xywh( h, x, y+0, 2, 1 );
173                 x264_mb_mc_0xywh( h, x, y+1, 2, 1 );
174                 break;
175             case D_L0_4x8:
176                 x264_mb_mc_0xywh( h, x+0, y, 1, 2 );
177                 x264_mb_mc_0xywh( h, x+1, y, 1, 2 );
178                 break;
179             case D_L0_4x4:
180                 x264_mb_mc_0xywh( h, x+0, y+0, 1, 1 );
181                 x264_mb_mc_0xywh( h, x+1, y+0, 1, 1 );
182                 x264_mb_mc_0xywh( h, x+0, y+1, 1, 1 );
183                 x264_mb_mc_0xywh( h, x+1, y+1, 1, 1 );
184                 break;
185         }
186     }
187     else
188     {
189         int scan8 = x264_scan8[0] + x + 8*y;
190
191         if( h->mb.cache.ref[0][scan8] >= 0 )
192             if( h->mb.cache.ref[1][scan8] >= 0 )
193                 x264_mb_mc_01xywh( h, x, y, 2, 2 );
194             else
195                 x264_mb_mc_0xywh( h, x, y, 2, 2 );
196         else
197             x264_mb_mc_1xywh( h, x, y, 2, 2 );
198     }
199 }
200
201 void x264_mb_mc( x264_t *h )
202 {
203     if( h->mb.i_partition == D_8x8 )
204     {
205         for( int i = 0; i < 4; i++ )
206             x264_mb_mc_8x8( h, i );
207     }
208     else
209     {
210         int ref0a = h->mb.cache.ref[0][x264_scan8[ 0]];
211         int ref0b = h->mb.cache.ref[0][x264_scan8[12]];
212         int ref1a = h->mb.cache.ref[1][x264_scan8[ 0]];
213         int ref1b = h->mb.cache.ref[1][x264_scan8[12]];
214
215         if( h->mb.i_partition == D_16x16 )
216         {
217             if( ref0a >= 0 )
218                 if( ref1a >= 0 ) x264_mb_mc_01xywh( h, 0, 0, 4, 4 );
219                 else             x264_mb_mc_0xywh ( h, 0, 0, 4, 4 );
220             else                 x264_mb_mc_1xywh ( h, 0, 0, 4, 4 );
221         }
222         else if( h->mb.i_partition == D_16x8 )
223         {
224             if( ref0a >= 0 )
225                 if( ref1a >= 0 ) x264_mb_mc_01xywh( h, 0, 0, 4, 2 );
226                 else             x264_mb_mc_0xywh ( h, 0, 0, 4, 2 );
227             else                 x264_mb_mc_1xywh ( h, 0, 0, 4, 2 );
228
229             if( ref0b >= 0 )
230                 if( ref1b >= 0 ) x264_mb_mc_01xywh( h, 0, 2, 4, 2 );
231                 else             x264_mb_mc_0xywh ( h, 0, 2, 4, 2 );
232             else                 x264_mb_mc_1xywh ( h, 0, 2, 4, 2 );
233         }
234         else if( h->mb.i_partition == D_8x16 )
235         {
236             if( ref0a >= 0 )
237                 if( ref1a >= 0 ) x264_mb_mc_01xywh( h, 0, 0, 2, 4 );
238                 else             x264_mb_mc_0xywh ( h, 0, 0, 2, 4 );
239             else                 x264_mb_mc_1xywh ( h, 0, 0, 2, 4 );
240
241             if( ref0b >= 0 )
242                 if( ref1b >= 0 ) x264_mb_mc_01xywh( h, 2, 0, 2, 4 );
243                 else             x264_mb_mc_0xywh ( h, 2, 0, 2, 4 );
244             else                 x264_mb_mc_1xywh ( h, 2, 0, 2, 4 );
245         }
246     }
247 }
248
249 int x264_macroblock_cache_allocate( x264_t *h )
250 {
251     int i_mb_count = h->mb.i_mb_count;
252
253     h->mb.i_mb_stride = h->mb.i_mb_width;
254     h->mb.i_b8_stride = h->mb.i_mb_width * 2;
255     h->mb.i_b4_stride = h->mb.i_mb_width * 4;
256
257     h->mb.b_interlaced = PARAM_INTERLACED;
258
259     CHECKED_MALLOC( h->mb.qp, i_mb_count * sizeof(int8_t) );
260     CHECKED_MALLOC( h->mb.cbp, i_mb_count * sizeof(int16_t) );
261     CHECKED_MALLOC( h->mb.mb_transform_size, i_mb_count * sizeof(int8_t) );
262     CHECKED_MALLOC( h->mb.slice_table, i_mb_count * sizeof(uint16_t) );
263     memset( h->mb.slice_table, -1, i_mb_count * sizeof(uint16_t) );
264
265     /* 0 -> 3 top(4), 4 -> 6 : left(3) */
266     CHECKED_MALLOC( h->mb.intra4x4_pred_mode, i_mb_count * 8 * sizeof(int8_t) );
267
268     /* all coeffs */
269     CHECKED_MALLOC( h->mb.non_zero_count, i_mb_count * 48 * sizeof(uint8_t) );
270
271     if( h->param.b_cabac )
272     {
273         CHECKED_MALLOC( h->mb.skipbp, i_mb_count * sizeof(int8_t) );
274         CHECKED_MALLOC( h->mb.chroma_pred_mode, i_mb_count * sizeof(int8_t) );
275         CHECKED_MALLOC( h->mb.mvd[0], i_mb_count * sizeof( **h->mb.mvd ) );
276         if( h->param.i_bframe )
277             CHECKED_MALLOC( h->mb.mvd[1], i_mb_count * sizeof( **h->mb.mvd ) );
278     }
279
280     for( int i = 0; i < 2; i++ )
281     {
282         int i_refs = X264_MIN(X264_REF_MAX, (i ? 1 + !!h->param.i_bframe_pyramid : h->param.i_frame_reference) ) << PARAM_INTERLACED;
283         if( h->param.analyse.i_weighted_pred == X264_WEIGHTP_SMART )
284             i_refs = X264_MIN(X264_REF_MAX, i_refs + 1 + (BIT_DEPTH == 8)); //smart weights add two duplicate frames, one in >8-bit
285
286         for( int j = !i; j < i_refs; j++ )
287         {
288             CHECKED_MALLOC( h->mb.mvr[i][j], 2 * (i_mb_count + 1) * sizeof(int16_t) );
289             M32( h->mb.mvr[i][j][0] ) = 0;
290             h->mb.mvr[i][j]++;
291         }
292     }
293
294     if( h->param.analyse.i_weighted_pred )
295     {
296         int i_padv = PADV << PARAM_INTERLACED;
297         int luma_plane_size = 0;
298         int numweightbuf;
299
300         if( h->param.analyse.i_weighted_pred == X264_WEIGHTP_FAKE )
301         {
302             // only need buffer for lookahead
303             if( !h->param.i_sync_lookahead || h == h->thread[h->param.i_threads] )
304             {
305                 // Fake analysis only works on lowres
306                 luma_plane_size = h->fdec->i_stride_lowres * (h->mb.i_mb_height*8+2*i_padv);
307                 // Only need 1 buffer for analysis
308                 numweightbuf = 1;
309             }
310             else
311                 numweightbuf = 0;
312         }
313         else
314         {
315             /* Both ref and fenc is stored for 4:2:0 and 4:2:2 which means that 4:2:0 and 4:4:4
316              * needs the same amount of space and 4:2:2 needs twice that much */
317             luma_plane_size = h->fdec->i_stride[0] * (h->mb.i_mb_height*(16<<(CHROMA_FORMAT==CHROMA_422))+2*i_padv);
318
319             if( h->param.analyse.i_weighted_pred == X264_WEIGHTP_SMART )
320                 //smart can weight one ref and one offset -1 in 8-bit
321                 numweightbuf = 1 + (BIT_DEPTH == 8);
322             else
323                 //simple only has one weighted ref
324                 numweightbuf = 1;
325         }
326
327         for( int i = 0; i < numweightbuf; i++ )
328             CHECKED_MALLOC( h->mb.p_weight_buf[i], luma_plane_size * sizeof(pixel) );
329     }
330
331     return 0;
332 fail:
333     return -1;
334 }
335 void x264_macroblock_cache_free( x264_t *h )
336 {
337     for( int i = 0; i < 2; i++ )
338         for( int j = !i; j < X264_REF_MAX*2; j++ )
339             if( h->mb.mvr[i][j] )
340                 x264_free( h->mb.mvr[i][j]-1 );
341     for( int i = 0; i < X264_REF_MAX; i++ )
342         x264_free( h->mb.p_weight_buf[i] );
343
344     if( h->param.b_cabac )
345     {
346         x264_free( h->mb.skipbp );
347         x264_free( h->mb.chroma_pred_mode );
348         x264_free( h->mb.mvd[0] );
349         x264_free( h->mb.mvd[1] );
350     }
351     x264_free( h->mb.slice_table );
352     x264_free( h->mb.intra4x4_pred_mode );
353     x264_free( h->mb.non_zero_count );
354     x264_free( h->mb.mb_transform_size );
355     x264_free( h->mb.cbp );
356     x264_free( h->mb.qp );
357 }
358
359 int x264_macroblock_thread_allocate( x264_t *h, int b_lookahead )
360 {
361     if( !b_lookahead )
362     {
363         for( int i = 0; i < (PARAM_INTERLACED ? 5 : 2); i++ )
364             for( int j = 0; j < (CHROMA444 ? 3 : 2); j++ )
365             {
366                 CHECKED_MALLOC( h->intra_border_backup[i][j], (h->sps->i_mb_width*16+32) * sizeof(pixel) );
367                 h->intra_border_backup[i][j] += 16;
368             }
369         for( int i = 0; i <= PARAM_INTERLACED; i++ )
370         {
371             if( h->param.b_sliced_threads )
372             {
373                 /* Only allocate the first one, and allocate it for the whole frame, because we
374                  * won't be deblocking until after the frame is fully encoded. */
375                 if( h == h->thread[0] && !i )
376                     CHECKED_MALLOC( h->deblock_strength[0], sizeof(**h->deblock_strength) * h->mb.i_mb_count );
377                 else
378                     h->deblock_strength[i] = h->thread[0]->deblock_strength[0];
379             }
380             else
381                 CHECKED_MALLOC( h->deblock_strength[i], sizeof(**h->deblock_strength) * h->mb.i_mb_width );
382             h->deblock_strength[1] = h->deblock_strength[i];
383         }
384     }
385
386     /* Allocate scratch buffer */
387     int scratch_size = 0;
388     if( !b_lookahead )
389     {
390         int buf_hpel = (h->thread[0]->fdec->i_width[0]+48) * sizeof(int16_t);
391         int buf_ssim = h->param.analyse.b_ssim * 8 * (h->param.i_width/4+3) * sizeof(int);
392         int me_range = X264_MIN(h->param.analyse.i_me_range, h->param.analyse.i_mv_range);
393         int buf_tesa = (h->param.analyse.i_me_method >= X264_ME_ESA) *
394             ((me_range*2+24) * sizeof(int16_t) + (me_range+4) * (me_range+1) * 4 * sizeof(mvsad_t));
395         scratch_size = X264_MAX3( buf_hpel, buf_ssim, buf_tesa );
396     }
397     int buf_mbtree = h->param.rc.b_mb_tree * ((h->mb.i_mb_width+7)&~7) * sizeof(int);
398     scratch_size = X264_MAX( scratch_size, buf_mbtree );
399     if( scratch_size )
400         CHECKED_MALLOC( h->scratch_buffer, scratch_size );
401     else
402         h->scratch_buffer = NULL;
403
404     return 0;
405 fail:
406     return -1;
407 }
408
409 void x264_macroblock_thread_free( x264_t *h, int b_lookahead )
410 {
411     if( !b_lookahead )
412     {
413         for( int i = 0; i <= PARAM_INTERLACED; i++ )
414             if( !h->param.b_sliced_threads || (h == h->thread[0] && !i) )
415                 x264_free( h->deblock_strength[i] );
416         for( int i = 0; i < (PARAM_INTERLACED ? 5 : 2); i++ )
417             for( int j = 0; j < (CHROMA444 ? 3 : 2); j++ )
418                 x264_free( h->intra_border_backup[i][j] - 16 );
419     }
420     x264_free( h->scratch_buffer );
421 }
422
423 void x264_macroblock_slice_init( x264_t *h )
424 {
425     h->mb.mv[0] = h->fdec->mv[0];
426     h->mb.mv[1] = h->fdec->mv[1];
427     h->mb.mvr[0][0] = h->fdec->mv16x16;
428     h->mb.ref[0] = h->fdec->ref[0];
429     h->mb.ref[1] = h->fdec->ref[1];
430     h->mb.type = h->fdec->mb_type;
431     h->mb.partition = h->fdec->mb_partition;
432     h->mb.field = h->fdec->field;
433
434     h->fdec->i_ref[0] = h->i_ref[0];
435     h->fdec->i_ref[1] = h->i_ref[1];
436     for( int i = 0; i < h->i_ref[0]; i++ )
437         h->fdec->ref_poc[0][i] = h->fref[0][i]->i_poc;
438     if( h->sh.i_type == SLICE_TYPE_B )
439     {
440         for( int i = 0; i < h->i_ref[1]; i++ )
441             h->fdec->ref_poc[1][i] = h->fref[1][i]->i_poc;
442
443         map_col_to_list0(-1) = -1;
444         map_col_to_list0(-2) = -2;
445         for( int i = 0; i < h->fref[1][0]->i_ref[0]; i++ )
446         {
447             int poc = h->fref[1][0]->ref_poc[0][i];
448             map_col_to_list0(i) = -2;
449             for( int j = 0; j < h->i_ref[0]; j++ )
450                 if( h->fref[0][j]->i_poc == poc )
451                 {
452                     map_col_to_list0(i) = j;
453                     break;
454                 }
455         }
456     }
457     else if( h->sh.i_type == SLICE_TYPE_P )
458     {
459         if( h->sh.i_disable_deblocking_filter_idc != 1 && h->param.analyse.i_weighted_pred == X264_WEIGHTP_SMART )
460         {
461             deblock_ref_table(-2) = -2;
462             deblock_ref_table(-1) = -1;
463             for( int i = 0; i < h->i_ref[0] << SLICE_MBAFF; i++ )
464             {
465                 /* Mask off high bits to avoid frame num collisions with -1/-2.
466                  * In current x264 frame num values don't cover a range of more
467                  * than 32, so 6 bits is enough for uniqueness. */
468                 if( !MB_INTERLACED )
469                     deblock_ref_table(i) = h->fref[0][i]->i_frame_num&63;
470                 else
471                     deblock_ref_table(i) = ((h->fref[0][i>>1]->i_frame_num&63)<<1) + (i&1);
472             }
473         }
474     }
475
476     /* init with not available (for top right idx=7,15) */
477     memset( h->mb.cache.ref, -2, sizeof( h->mb.cache.ref ) );
478
479     if( h->i_ref[0] > 0 )
480         for( int field = 0; field <= SLICE_MBAFF; field++ )
481         {
482             int curpoc = h->fdec->i_poc + h->fdec->i_delta_poc[field];
483             int refpoc = h->fref[0][0]->i_poc + h->fref[0][0]->i_delta_poc[field];
484             int delta = curpoc - refpoc;
485
486             h->fdec->inv_ref_poc[field] = (256 + delta/2) / delta;
487         }
488
489     h->mb.i_neighbour4[6] =
490     h->mb.i_neighbour4[9] =
491     h->mb.i_neighbour4[12] =
492     h->mb.i_neighbour4[14] = MB_LEFT|MB_TOP|MB_TOPLEFT|MB_TOPRIGHT;
493     h->mb.i_neighbour4[3] =
494     h->mb.i_neighbour4[7] =
495     h->mb.i_neighbour4[11] =
496     h->mb.i_neighbour4[13] =
497     h->mb.i_neighbour4[15] =
498     h->mb.i_neighbour8[3] = MB_LEFT|MB_TOP|MB_TOPLEFT;
499 }
500
501 void x264_macroblock_thread_init( x264_t *h )
502 {
503     h->mb.i_me_method = h->param.analyse.i_me_method;
504     h->mb.i_subpel_refine = h->param.analyse.i_subpel_refine;
505     if( h->sh.i_type == SLICE_TYPE_B && (h->mb.i_subpel_refine == 6 || h->mb.i_subpel_refine == 8) )
506         h->mb.i_subpel_refine--;
507     h->mb.b_chroma_me = h->param.analyse.b_chroma_me &&
508                         ((h->sh.i_type == SLICE_TYPE_P && h->mb.i_subpel_refine >= 5) ||
509                          (h->sh.i_type == SLICE_TYPE_B && h->mb.i_subpel_refine >= 9));
510     h->mb.b_dct_decimate = h->sh.i_type == SLICE_TYPE_B ||
511                           (h->param.analyse.b_dct_decimate && h->sh.i_type != SLICE_TYPE_I);
512     h->mb.i_mb_prev_xy = -1;
513
514     /*          4:2:0                      4:2:2                      4:4:4
515      * fdec            fenc       fdec            fenc       fdec            fenc
516      * y y y y y y y   Y Y Y Y    y y y y y y y   Y Y Y Y    y y y y y y y   Y Y Y Y
517      * y Y Y Y Y       Y Y Y Y    y Y Y Y Y       Y Y Y Y    y Y Y Y Y       Y Y Y Y
518      * y Y Y Y Y       Y Y Y Y    y Y Y Y Y       Y Y Y Y    y Y Y Y Y       Y Y Y Y
519      * y Y Y Y Y       Y Y Y Y    y Y Y Y Y       Y Y Y Y    y Y Y Y Y       Y Y Y Y
520      * y Y Y Y Y       U U V V    y Y Y Y Y       U U V V    y Y Y Y Y       U U U U
521      * u u u   v v v   U U V V    u u u   v v v   U U V V    u u u u u u u   U U U U
522      * u U U   v V V              u U U   v V V   U U V V    u U U U U       U U U U
523      * u U U   v V V              u U U   v V V   U U V V    u U U U U       U U U U
524      *                            u U U   v V V              u U U U U       V V V V
525      *                            u U U   v V V              u U U U U       V V V V
526      *                                                       v v v v v v v   V V V V
527      *                                                       v V V V V       V V V V
528      *                                                       v V V V V
529      *                                                       v V V V V
530      *                                                       v V V V V
531      */
532     h->mb.pic.p_fenc[0] = h->mb.pic.fenc_buf;
533     h->mb.pic.p_fdec[0] = h->mb.pic.fdec_buf + 2*FDEC_STRIDE;
534     h->mb.pic.p_fenc[1] = h->mb.pic.fenc_buf + 16*FENC_STRIDE;
535     h->mb.pic.p_fdec[1] = h->mb.pic.fdec_buf + 19*FDEC_STRIDE;
536     if( CHROMA444 )
537     {
538         h->mb.pic.p_fenc[2] = h->mb.pic.fenc_buf + 32*FENC_STRIDE;
539         h->mb.pic.p_fdec[2] = h->mb.pic.fdec_buf + 36*FDEC_STRIDE;
540     }
541     else
542     {
543         h->mb.pic.p_fenc[2] = h->mb.pic.fenc_buf + 16*FENC_STRIDE + 8;
544         h->mb.pic.p_fdec[2] = h->mb.pic.fdec_buf + 19*FDEC_STRIDE + 16;
545     }
546 }
547
548 void x264_prefetch_fenc( x264_t *h, x264_frame_t *fenc, int i_mb_x, int i_mb_y )
549 {
550     int stride_y  = fenc->i_stride[0];
551     int stride_uv = fenc->i_stride[1];
552     int off_y  = 16 * i_mb_x + 16 * i_mb_y * stride_y;
553     int off_uv = 16 * i_mb_x + (16 * i_mb_y * stride_uv >> CHROMA_V_SHIFT);
554     h->mc.prefetch_fenc( fenc->plane[0]+off_y, stride_y,
555                          fenc->plane[1]+off_uv, stride_uv, i_mb_x );
556 }
557
558 NOINLINE void x264_copy_column8( pixel *dst, pixel *src )
559 {
560     // input pointers are offset by 4 rows because that's faster (smaller instruction size on x86)
561     for( int i = -4; i < 4; i++ )
562         dst[i*FDEC_STRIDE] = src[i*FDEC_STRIDE];
563 }
564
565 static void ALWAYS_INLINE x264_macroblock_load_pic_pointers( x264_t *h, int mb_x, int mb_y, int i, int b_chroma, int b_mbaff )
566 {
567     int mb_interlaced = b_mbaff && MB_INTERLACED;
568     int height = b_chroma ? 16 >> CHROMA_V_SHIFT : 16;
569     int i_stride = h->fdec->i_stride[i];
570     int i_stride2 = i_stride << mb_interlaced;
571     int i_pix_offset = mb_interlaced
572                      ? 16 * mb_x + height * (mb_y&~1) * i_stride + (mb_y&1) * i_stride
573                      : 16 * mb_x + height * mb_y * i_stride;
574     pixel *plane_fdec = &h->fdec->plane[i][i_pix_offset];
575     int fdec_idx = b_mbaff ? (mb_interlaced ? (3 + (mb_y&1)) : (mb_y&1) ? 2 : 4) : !(mb_y&1);
576     pixel *intra_fdec = &h->intra_border_backup[fdec_idx][i][mb_x*16];
577     int ref_pix_offset[2] = { i_pix_offset, i_pix_offset };
578     /* ref_pix_offset[0] references the current field and [1] the opposite field. */
579     if( mb_interlaced )
580         ref_pix_offset[1] += (1-2*(mb_y&1)) * i_stride;
581     h->mb.pic.i_stride[i] = i_stride2;
582     h->mb.pic.p_fenc_plane[i] = &h->fenc->plane[i][i_pix_offset];
583     if( b_chroma )
584     {
585         h->mc.load_deinterleave_chroma_fenc( h->mb.pic.p_fenc[1], h->mb.pic.p_fenc_plane[1], i_stride2, height );
586         memcpy( h->mb.pic.p_fdec[1]-FDEC_STRIDE, intra_fdec, 8*sizeof(pixel) );
587         memcpy( h->mb.pic.p_fdec[2]-FDEC_STRIDE, intra_fdec+8, 8*sizeof(pixel) );
588         h->mb.pic.p_fdec[1][-FDEC_STRIDE-1] = intra_fdec[-1-8];
589         h->mb.pic.p_fdec[2][-FDEC_STRIDE-1] = intra_fdec[-1];
590     }
591     else
592     {
593         h->mc.copy[PIXEL_16x16]( h->mb.pic.p_fenc[i], FENC_STRIDE, h->mb.pic.p_fenc_plane[i], i_stride2, 16 );
594         memcpy( h->mb.pic.p_fdec[i]-FDEC_STRIDE, intra_fdec, 24*sizeof(pixel) );
595         h->mb.pic.p_fdec[i][-FDEC_STRIDE-1] = intra_fdec[-1];
596     }
597     if( b_mbaff || h->mb.b_reencode_mb )
598     {
599         for( int j = 0; j < height; j++ )
600             if( b_chroma )
601             {
602                 h->mb.pic.p_fdec[1][-1+j*FDEC_STRIDE] = plane_fdec[-2+j*i_stride2];
603                 h->mb.pic.p_fdec[2][-1+j*FDEC_STRIDE] = plane_fdec[-1+j*i_stride2];
604             }
605             else
606                 h->mb.pic.p_fdec[i][-1+j*FDEC_STRIDE] = plane_fdec[-1+j*i_stride2];
607     }
608     pixel *plane_src, **filtered_src;
609     for( int j = 0; j < h->mb.pic.i_fref[0]; j++ )
610     {
611         // Interpolate between pixels in same field.
612         if( mb_interlaced )
613         {
614             plane_src = h->fref[0][j>>1]->plane_fld[i];
615             filtered_src = h->fref[0][j>>1]->filtered_fld[i];
616         }
617         else
618         {
619             plane_src = h->fref[0][j]->plane[i];
620             filtered_src = h->fref[0][j]->filtered[i];
621         }
622         h->mb.pic.p_fref[0][j][i*4] = plane_src + ref_pix_offset[j&1];
623
624         if( !b_chroma )
625         {
626             for( int k = 1; k < 4; k++ )
627                 h->mb.pic.p_fref[0][j][i*4+k] = filtered_src[k] + ref_pix_offset[j&1];
628             if( !i )
629             {
630                 if( h->sh.weight[j][0].weightfn )
631                     h->mb.pic.p_fref_w[j] = &h->fenc->weighted[j >> mb_interlaced][ref_pix_offset[j&1]];
632                 else
633                     h->mb.pic.p_fref_w[j] = h->mb.pic.p_fref[0][j][0];
634             }
635         }
636     }
637     if( h->sh.i_type == SLICE_TYPE_B )
638         for( int j = 0; j < h->mb.pic.i_fref[1]; j++ )
639         {
640             if( mb_interlaced )
641             {
642                 plane_src = h->fref[1][j>>1]->plane_fld[i];
643                 filtered_src = h->fref[1][j>>1]->filtered_fld[i];
644             }
645             else
646             {
647                 plane_src = h->fref[1][j]->plane[i];
648                 filtered_src = h->fref[1][j]->filtered[i];
649             }
650             h->mb.pic.p_fref[1][j][i*4] = plane_src + ref_pix_offset[j&1];
651
652             if( !b_chroma )
653                 for( int k = 1; k < 4; k++ )
654                     h->mb.pic.p_fref[1][j][i*4+k] = filtered_src[k] + ref_pix_offset[j&1];
655         }
656 }
657
658 static const x264_left_table_t left_indices[4] =
659 {
660     /* Current is progressive */
661     {{ 4, 4, 5, 5}, { 3,  3,  7,  7}, {16+1, 16+1, 32+1, 32+1}, {0, 0, 1, 1}, {0, 0, 0, 0}},
662     {{ 6, 6, 3, 3}, {11, 11, 15, 15}, {16+5, 16+5, 32+5, 32+5}, {2, 2, 3, 3}, {1, 1, 1, 1}},
663     /* Current is interlaced */
664     {{ 4, 6, 4, 6}, { 3, 11,  3, 11}, {16+1, 16+1, 32+1, 32+1}, {0, 2, 0, 2}, {0, 1, 0, 1}},
665     /* Both same */
666     {{ 4, 5, 6, 3}, { 3,  7, 11, 15}, {16+1, 16+5, 32+1, 32+5}, {0, 1, 2, 3}, {0, 0, 1, 1}}
667 };
668
669 static void ALWAYS_INLINE x264_macroblock_cache_load_neighbours( x264_t *h, int mb_x, int mb_y, int b_interlaced )
670 {
671     const int mb_interlaced = b_interlaced && MB_INTERLACED;
672     int top_y = mb_y - (1 << mb_interlaced);
673     int top = top_y * h->mb.i_mb_stride + mb_x;
674
675     h->mb.i_mb_x = mb_x;
676     h->mb.i_mb_y = mb_y;
677     h->mb.i_mb_xy = mb_y * h->mb.i_mb_stride + mb_x;
678     h->mb.i_b8_xy = 2*(mb_y * h->mb.i_b8_stride + mb_x);
679     h->mb.i_b4_xy = 4*(mb_y * h->mb.i_b4_stride + mb_x);
680     h->mb.left_b8[0] =
681     h->mb.left_b8[1] = -1;
682     h->mb.left_b4[0] =
683     h->mb.left_b4[1] = -1;
684     h->mb.i_neighbour = 0;
685     h->mb.i_neighbour_intra = 0;
686     h->mb.i_neighbour_frame = 0;
687     h->mb.i_mb_top_xy = -1;
688     h->mb.i_mb_top_y = -1;
689     h->mb.i_mb_left_xy[0] = h->mb.i_mb_left_xy[1] = -1;
690     h->mb.i_mb_topleft_xy = -1;
691     h->mb.i_mb_topright_xy = -1;
692     h->mb.i_mb_type_top = -1;
693     h->mb.i_mb_type_left[0] = h->mb.i_mb_type_left[1] = -1;
694     h->mb.i_mb_type_topleft = -1;
695     h->mb.i_mb_type_topright = -1;
696     h->mb.left_index_table = &left_indices[3];
697     h->mb.topleft_partition = 0;
698
699     int topleft_y = top_y;
700     int topright_y = top_y;
701     int left[2];
702
703     left[0] = left[1] = h->mb.i_mb_xy - 1;
704     h->mb.left_b8[0] = h->mb.left_b8[1] = h->mb.i_b8_xy - 2;
705     h->mb.left_b4[0] = h->mb.left_b4[1] = h->mb.i_b4_xy - 4;
706
707     if( b_interlaced )
708     {
709         h->mb.i_mb_top_mbpair_xy = h->mb.i_mb_xy - 2*h->mb.i_mb_stride;
710         h->mb.i_mb_topleft_y = -1;
711         h->mb.i_mb_topright_y = -1;
712
713         if( mb_y&1 )
714         {
715             if( mb_x && mb_interlaced != h->mb.field[h->mb.i_mb_xy-1] )
716             {
717                 left[0] = left[1] = h->mb.i_mb_xy - 1 - h->mb.i_mb_stride;
718                 h->mb.left_b8[0] = h->mb.left_b8[1] = h->mb.i_b8_xy - 2 - 2*h->mb.i_b8_stride;
719                 h->mb.left_b4[0] = h->mb.left_b4[1] = h->mb.i_b4_xy - 4 - 4*h->mb.i_b4_stride;
720
721                 if( mb_interlaced )
722                 {
723                     h->mb.left_index_table = &left_indices[2];
724                     left[1] += h->mb.i_mb_stride;
725                     h->mb.left_b8[1] += 2*h->mb.i_b8_stride;
726                     h->mb.left_b4[1] += 4*h->mb.i_b4_stride;
727                 }
728                 else
729                 {
730                     h->mb.left_index_table = &left_indices[1];
731                     topleft_y++;
732                     h->mb.topleft_partition = 1;
733                 }
734             }
735             if( !mb_interlaced )
736                 topright_y = -1;
737         }
738         else
739         {
740             if( mb_interlaced && top >= 0 )
741             {
742                 if( !h->mb.field[top] )
743                 {
744                     top += h->mb.i_mb_stride;
745                     top_y++;
746                 }
747                 if( mb_x )
748                     topleft_y += !h->mb.field[h->mb.i_mb_stride*topleft_y + mb_x - 1];
749                 if( mb_x < h->mb.i_mb_width-1 )
750                     topright_y += !h->mb.field[h->mb.i_mb_stride*topright_y + mb_x + 1];
751             }
752             if( mb_x && mb_interlaced != h->mb.field[h->mb.i_mb_xy-1] )
753             {
754                 if( mb_interlaced )
755                 {
756                     h->mb.left_index_table = &left_indices[2];
757                     left[1] += h->mb.i_mb_stride;
758                     h->mb.left_b8[1] += 2*h->mb.i_b8_stride;
759                     h->mb.left_b4[1] += 4*h->mb.i_b4_stride;
760                 }
761                 else
762                     h->mb.left_index_table = &left_indices[0];
763             }
764         }
765     }
766
767     if( mb_x > 0 )
768     {
769         h->mb.i_neighbour_frame |= MB_LEFT;
770         h->mb.i_mb_left_xy[0] = left[0];
771         h->mb.i_mb_left_xy[1] = left[1];
772         h->mb.i_mb_type_left[0] = h->mb.type[h->mb.i_mb_left_xy[0]];
773         h->mb.i_mb_type_left[1] = h->mb.type[h->mb.i_mb_left_xy[1]];
774         if( h->mb.slice_table[left[0]] == h->sh.i_first_mb )
775         {
776             h->mb.i_neighbour |= MB_LEFT;
777
778             // FIXME: We don't currently support constrained intra + mbaff.
779             if( !h->param.b_constrained_intra || IS_INTRA( h->mb.i_mb_type_left[0] ) )
780                 h->mb.i_neighbour_intra |= MB_LEFT;
781         }
782     }
783
784     /* We can't predict from the previous threadslice since it hasn't been encoded yet. */
785     if( (h->i_threadslice_start >> mb_interlaced) != (mb_y >> mb_interlaced) )
786     {
787         if( top >= 0 )
788         {
789             h->mb.i_neighbour_frame |= MB_TOP;
790             h->mb.i_mb_top_xy = top;
791             h->mb.i_mb_top_y = top_y;
792             h->mb.i_mb_type_top = h->mb.type[h->mb.i_mb_top_xy];
793             if( h->mb.slice_table[top] == h->sh.i_first_mb )
794             {
795                 h->mb.i_neighbour |= MB_TOP;
796
797                 if( !h->param.b_constrained_intra || IS_INTRA( h->mb.i_mb_type_top ) )
798                     h->mb.i_neighbour_intra |= MB_TOP;
799
800                 /* We only need to prefetch the top blocks because the left was just written
801                  * to as part of the previous cache_save.  Since most target CPUs use write-allocate
802                  * caches, left blocks are near-guaranteed to be in L1 cache.  Top--not so much. */
803                 x264_prefetch( &h->mb.cbp[top] );
804                 x264_prefetch( h->mb.intra4x4_pred_mode[top] );
805                 x264_prefetch( &h->mb.non_zero_count[top][12] );
806                 /* These aren't always allocated, but prefetching an invalid address can't hurt. */
807                 x264_prefetch( &h->mb.mb_transform_size[top] );
808                 x264_prefetch( &h->mb.skipbp[top] );
809             }
810         }
811
812         if( mb_x > 0 && topleft_y >= 0  )
813         {
814             h->mb.i_neighbour_frame |= MB_TOPLEFT;
815             h->mb.i_mb_topleft_xy = h->mb.i_mb_stride*topleft_y + mb_x - 1;
816             h->mb.i_mb_topleft_y = topleft_y;
817             h->mb.i_mb_type_topleft = h->mb.type[h->mb.i_mb_topleft_xy];
818             if( h->mb.slice_table[h->mb.i_mb_topleft_xy] == h->sh.i_first_mb )
819             {
820                 h->mb.i_neighbour |= MB_TOPLEFT;
821
822                 if( !h->param.b_constrained_intra || IS_INTRA( h->mb.i_mb_type_topleft ) )
823                     h->mb.i_neighbour_intra |= MB_TOPLEFT;
824             }
825         }
826
827         if( mb_x < h->mb.i_mb_width - 1 && topright_y >= 0 )
828         {
829             h->mb.i_neighbour_frame |= MB_TOPRIGHT;
830             h->mb.i_mb_topright_xy = h->mb.i_mb_stride*topright_y + mb_x + 1;
831             h->mb.i_mb_topright_y = topright_y;
832             h->mb.i_mb_type_topright = h->mb.type[h->mb.i_mb_topright_xy];
833             if( h->mb.slice_table[h->mb.i_mb_topright_xy] == h->sh.i_first_mb )
834             {
835                 h->mb.i_neighbour |= MB_TOPRIGHT;
836
837                 if( !h->param.b_constrained_intra || IS_INTRA( h->mb.i_mb_type_topright ) )
838                     h->mb.i_neighbour_intra |= MB_TOPRIGHT;
839             }
840         }
841     }
842 }
843
844 #define LTOP 0
845 #if HAVE_INTERLACED
846 #   define LBOT 1
847 #else
848 #   define LBOT 0
849 #endif
850
851 static void ALWAYS_INLINE x264_macroblock_cache_load( x264_t *h, int mb_x, int mb_y, int b_mbaff )
852 {
853     x264_macroblock_cache_load_neighbours( h, mb_x, mb_y, b_mbaff );
854
855     int *left = h->mb.i_mb_left_xy;
856     int top  = h->mb.i_mb_top_xy;
857     int top_y = h->mb.i_mb_top_y;
858     int s8x8 = h->mb.i_b8_stride;
859     int s4x4 = h->mb.i_b4_stride;
860     int top_8x8 = (2*top_y+1) * s8x8 + 2*mb_x;
861     int top_4x4 = (4*top_y+3) * s4x4 + 4*mb_x;
862     int lists = (1 << h->sh.i_type) & 3;
863
864     /* GCC pessimizes direct loads from heap-allocated arrays due to aliasing. */
865     /* By only dereferencing them once, we avoid this issue. */
866     int8_t (*i4x4)[8] = h->mb.intra4x4_pred_mode;
867     uint8_t (*nnz)[48] = h->mb.non_zero_count;
868     int16_t *cbp = h->mb.cbp;
869
870     const x264_left_table_t *left_index_table = h->mb.left_index_table;
871
872     h->mb.cache.deblock_strength = h->deblock_strength[mb_y&1][h->param.b_sliced_threads?h->mb.i_mb_xy:mb_x];
873
874     /* load cache */
875     if( h->mb.i_neighbour & MB_TOP )
876     {
877         h->mb.cache.i_cbp_top = cbp[top];
878         /* load intra4x4 */
879         CP32( &h->mb.cache.intra4x4_pred_mode[x264_scan8[0] - 8], &i4x4[top][0] );
880
881         /* load non_zero_count */
882         CP32( &h->mb.cache.non_zero_count[x264_scan8[ 0] - 8], &nnz[top][12] );
883         CP32( &h->mb.cache.non_zero_count[x264_scan8[16] - 8], &nnz[top][16-4 + (16>>CHROMA_V_SHIFT)] );
884         CP32( &h->mb.cache.non_zero_count[x264_scan8[32] - 8], &nnz[top][32-4 + (16>>CHROMA_V_SHIFT)] );
885
886         /* Finish the prefetching */
887         for( int l = 0; l < lists; l++ )
888         {
889             x264_prefetch( &h->mb.mv[l][top_4x4-1] );
890             /* Top right being not in the same cacheline as top left will happen
891              * once every 4 MBs, so one extra prefetch is worthwhile */
892             x264_prefetch( &h->mb.mv[l][top_4x4+4] );
893             x264_prefetch( &h->mb.ref[l][top_8x8-1] );
894             x264_prefetch( &h->mb.mvd[l][top] );
895         }
896     }
897     else
898     {
899         h->mb.cache.i_cbp_top = -1;
900
901         /* load intra4x4 */
902         M32( &h->mb.cache.intra4x4_pred_mode[x264_scan8[0] - 8] ) = 0xFFFFFFFFU;
903
904         /* load non_zero_count */
905         M32( &h->mb.cache.non_zero_count[x264_scan8[ 0] - 8] ) = 0x80808080U;
906         M32( &h->mb.cache.non_zero_count[x264_scan8[16] - 8] ) = 0x80808080U;
907         M32( &h->mb.cache.non_zero_count[x264_scan8[32] - 8] ) = 0x80808080U;
908     }
909
910     if( h->mb.i_neighbour & MB_LEFT )
911     {
912         int ltop = left[LTOP];
913         int lbot = b_mbaff ? left[LBOT] : ltop;
914         if( b_mbaff )
915         {
916             const int16_t top_luma = (cbp[ltop] >> (left_index_table->mv[0]&(~1))) & 2;
917             const int16_t bot_luma = (cbp[lbot] >> (left_index_table->mv[2]&(~1))) & 2;
918             h->mb.cache.i_cbp_left = (cbp[ltop] & 0xfff0) | (bot_luma<<2) | top_luma;
919         }
920         else
921             h->mb.cache.i_cbp_left = cbp[ltop];
922
923         /* load intra4x4 */
924         h->mb.cache.intra4x4_pred_mode[x264_scan8[ 0] - 1] = i4x4[ltop][left_index_table->intra[0]];
925         h->mb.cache.intra4x4_pred_mode[x264_scan8[ 2] - 1] = i4x4[ltop][left_index_table->intra[1]];
926         h->mb.cache.intra4x4_pred_mode[x264_scan8[ 8] - 1] = i4x4[lbot][left_index_table->intra[2]];
927         h->mb.cache.intra4x4_pred_mode[x264_scan8[10] - 1] = i4x4[lbot][left_index_table->intra[3]];
928
929         /* load non_zero_count */
930         h->mb.cache.non_zero_count[x264_scan8[ 0] - 1] = nnz[ltop][left_index_table->nnz[0]];
931         h->mb.cache.non_zero_count[x264_scan8[ 2] - 1] = nnz[ltop][left_index_table->nnz[1]];
932         h->mb.cache.non_zero_count[x264_scan8[ 8] - 1] = nnz[lbot][left_index_table->nnz[2]];
933         h->mb.cache.non_zero_count[x264_scan8[10] - 1] = nnz[lbot][left_index_table->nnz[3]];
934
935         if( CHROMA_FORMAT >= CHROMA_422 )
936         {
937             int offset = (4>>CHROMA_H_SHIFT) - 4;
938             h->mb.cache.non_zero_count[x264_scan8[16+ 0] - 1] = nnz[ltop][left_index_table->nnz[0]+16+offset];
939             h->mb.cache.non_zero_count[x264_scan8[16+ 2] - 1] = nnz[ltop][left_index_table->nnz[1]+16+offset];
940             h->mb.cache.non_zero_count[x264_scan8[16+ 8] - 1] = nnz[lbot][left_index_table->nnz[2]+16+offset];
941             h->mb.cache.non_zero_count[x264_scan8[16+10] - 1] = nnz[lbot][left_index_table->nnz[3]+16+offset];
942             h->mb.cache.non_zero_count[x264_scan8[32+ 0] - 1] = nnz[ltop][left_index_table->nnz[0]+32+offset];
943             h->mb.cache.non_zero_count[x264_scan8[32+ 2] - 1] = nnz[ltop][left_index_table->nnz[1]+32+offset];
944             h->mb.cache.non_zero_count[x264_scan8[32+ 8] - 1] = nnz[lbot][left_index_table->nnz[2]+32+offset];
945             h->mb.cache.non_zero_count[x264_scan8[32+10] - 1] = nnz[lbot][left_index_table->nnz[3]+32+offset];
946         }
947         else
948         {
949             h->mb.cache.non_zero_count[x264_scan8[16+ 0] - 1] = nnz[ltop][left_index_table->nnz_chroma[0]];
950             h->mb.cache.non_zero_count[x264_scan8[16+ 2] - 1] = nnz[lbot][left_index_table->nnz_chroma[1]];
951             h->mb.cache.non_zero_count[x264_scan8[32+ 0] - 1] = nnz[ltop][left_index_table->nnz_chroma[2]];
952             h->mb.cache.non_zero_count[x264_scan8[32+ 2] - 1] = nnz[lbot][left_index_table->nnz_chroma[3]];
953         }
954     }
955     else
956     {
957         h->mb.cache.i_cbp_left = -1;
958
959         h->mb.cache.intra4x4_pred_mode[x264_scan8[ 0] - 1] =
960         h->mb.cache.intra4x4_pred_mode[x264_scan8[ 2] - 1] =
961         h->mb.cache.intra4x4_pred_mode[x264_scan8[ 8] - 1] =
962         h->mb.cache.intra4x4_pred_mode[x264_scan8[10] - 1] = -1;
963
964         /* load non_zero_count */
965         h->mb.cache.non_zero_count[x264_scan8[ 0] - 1] =
966         h->mb.cache.non_zero_count[x264_scan8[ 2] - 1] =
967         h->mb.cache.non_zero_count[x264_scan8[ 8] - 1] =
968         h->mb.cache.non_zero_count[x264_scan8[10] - 1] =
969         h->mb.cache.non_zero_count[x264_scan8[16+ 0] - 1] =
970         h->mb.cache.non_zero_count[x264_scan8[16+ 2] - 1] =
971         h->mb.cache.non_zero_count[x264_scan8[32+ 0] - 1] =
972         h->mb.cache.non_zero_count[x264_scan8[32+ 2] - 1] = 0x80;
973         if( CHROMA_FORMAT >= CHROMA_422 )
974         {
975             h->mb.cache.non_zero_count[x264_scan8[16+ 8] - 1] =
976             h->mb.cache.non_zero_count[x264_scan8[16+10] - 1] =
977             h->mb.cache.non_zero_count[x264_scan8[32+ 8] - 1] =
978             h->mb.cache.non_zero_count[x264_scan8[32+10] - 1] = 0x80;
979         }
980     }
981
982     if( h->pps->b_transform_8x8_mode )
983     {
984         h->mb.cache.i_neighbour_transform_size =
985             ( (h->mb.i_neighbour & MB_LEFT) && h->mb.mb_transform_size[left[0]] )
986           + ( (h->mb.i_neighbour & MB_TOP) && h->mb.mb_transform_size[top]  );
987     }
988
989     if( b_mbaff )
990     {
991         h->mb.pic.i_fref[0] = h->i_ref[0] << MB_INTERLACED;
992         h->mb.pic.i_fref[1] = h->i_ref[1] << MB_INTERLACED;
993     }
994
995     if( !b_mbaff )
996     {
997         x264_copy_column8( h->mb.pic.p_fdec[0]-1+ 4*FDEC_STRIDE, h->mb.pic.p_fdec[0]+15+ 4*FDEC_STRIDE );
998         x264_copy_column8( h->mb.pic.p_fdec[0]-1+12*FDEC_STRIDE, h->mb.pic.p_fdec[0]+15+12*FDEC_STRIDE );
999         x264_macroblock_load_pic_pointers( h, mb_x, mb_y, 0, 0, 0 );
1000         if( CHROMA444 )
1001         {
1002             x264_copy_column8( h->mb.pic.p_fdec[1]-1+ 4*FDEC_STRIDE, h->mb.pic.p_fdec[1]+15+ 4*FDEC_STRIDE );
1003             x264_copy_column8( h->mb.pic.p_fdec[1]-1+12*FDEC_STRIDE, h->mb.pic.p_fdec[1]+15+12*FDEC_STRIDE );
1004             x264_copy_column8( h->mb.pic.p_fdec[2]-1+ 4*FDEC_STRIDE, h->mb.pic.p_fdec[2]+15+ 4*FDEC_STRIDE );
1005             x264_copy_column8( h->mb.pic.p_fdec[2]-1+12*FDEC_STRIDE, h->mb.pic.p_fdec[2]+15+12*FDEC_STRIDE );
1006             x264_macroblock_load_pic_pointers( h, mb_x, mb_y, 1, 0, 0 );
1007             x264_macroblock_load_pic_pointers( h, mb_x, mb_y, 2, 0, 0 );
1008         }
1009         else
1010         {
1011             x264_copy_column8( h->mb.pic.p_fdec[1]-1+ 4*FDEC_STRIDE, h->mb.pic.p_fdec[1]+ 7+ 4*FDEC_STRIDE );
1012             x264_copy_column8( h->mb.pic.p_fdec[2]-1+ 4*FDEC_STRIDE, h->mb.pic.p_fdec[2]+ 7+ 4*FDEC_STRIDE );
1013             if( CHROMA_FORMAT == CHROMA_422 )
1014             {
1015                 x264_copy_column8( h->mb.pic.p_fdec[1]-1+12*FDEC_STRIDE, h->mb.pic.p_fdec[1]+ 7+12*FDEC_STRIDE );
1016                 x264_copy_column8( h->mb.pic.p_fdec[2]-1+12*FDEC_STRIDE, h->mb.pic.p_fdec[2]+ 7+12*FDEC_STRIDE );
1017             }
1018             x264_macroblock_load_pic_pointers( h, mb_x, mb_y, 1, 1, 0 );
1019         }
1020     }
1021     else
1022     {
1023         x264_macroblock_load_pic_pointers( h, mb_x, mb_y, 0, 0, 1 );
1024         if( CHROMA444 )
1025         {
1026             x264_macroblock_load_pic_pointers( h, mb_x, mb_y, 1, 0, 1 );
1027             x264_macroblock_load_pic_pointers( h, mb_x, mb_y, 2, 0, 1 );
1028         }
1029         else
1030             x264_macroblock_load_pic_pointers( h, mb_x, mb_y, 1, 1, 1 );
1031     }
1032
1033     if( h->fdec->integral )
1034     {
1035         int offset = 16 * (mb_x + mb_y * h->fdec->i_stride[0]);
1036         for( int list = 0; list < 2; list++ )
1037             for( int i = 0; i < h->mb.pic.i_fref[list]; i++ )
1038                 h->mb.pic.p_integral[list][i] = &h->fref[list][i]->integral[offset];
1039     }
1040
1041     x264_prefetch_fenc( h, h->fenc, mb_x, mb_y );
1042
1043     /* load ref/mv/mvd */
1044     for( int l = 0; l < lists; l++ )
1045     {
1046         int16_t (*mv)[2] = h->mb.mv[l];
1047         int8_t *ref = h->mb.ref[l];
1048
1049         int i8 = x264_scan8[0] - 1 - 1*8;
1050         if( h->mb.i_neighbour & MB_TOPLEFT )
1051         {
1052             int ir = b_mbaff ? 2*(s8x8*h->mb.i_mb_topleft_y + mb_x-1)+1+s8x8 : top_8x8 - 1;
1053             int iv = b_mbaff ? 4*(s4x4*h->mb.i_mb_topleft_y + mb_x-1)+3+3*s4x4 : top_4x4 - 1;
1054             if( b_mbaff && h->mb.topleft_partition )
1055             {
1056                 /* Take motion vector from the middle of macroblock instead of
1057                  * the bottom right as usual. */
1058                 iv -= 2*s4x4;
1059                 ir -= s8x8;
1060             }
1061             h->mb.cache.ref[l][i8] = ref[ir];
1062             CP32( h->mb.cache.mv[l][i8], mv[iv] );
1063         }
1064         else
1065         {
1066             h->mb.cache.ref[l][i8] = -2;
1067             M32( h->mb.cache.mv[l][i8] ) = 0;
1068         }
1069
1070         i8 = x264_scan8[0] - 8;
1071         if( h->mb.i_neighbour & MB_TOP )
1072         {
1073             h->mb.cache.ref[l][i8+0] =
1074             h->mb.cache.ref[l][i8+1] = ref[top_8x8 + 0];
1075             h->mb.cache.ref[l][i8+2] =
1076             h->mb.cache.ref[l][i8+3] = ref[top_8x8 + 1];
1077             CP128( h->mb.cache.mv[l][i8], mv[top_4x4] );
1078         }
1079         else
1080         {
1081             M128( h->mb.cache.mv[l][i8] ) = M128_ZERO;
1082             M32( &h->mb.cache.ref[l][i8] ) = (uint8_t)(-2) * 0x01010101U;
1083         }
1084
1085         i8 = x264_scan8[0] + 4 - 1*8;
1086         if( h->mb.i_neighbour & MB_TOPRIGHT )
1087         {
1088             int ir = b_mbaff ? 2*(s8x8*h->mb.i_mb_topright_y + (mb_x+1))+s8x8 : top_8x8 + 2;
1089             int iv = b_mbaff ? 4*(s4x4*h->mb.i_mb_topright_y + (mb_x+1))+3*s4x4 : top_4x4 + 4;
1090             h->mb.cache.ref[l][i8] = ref[ir];
1091             CP32( h->mb.cache.mv[l][i8], mv[iv] );
1092         }
1093         else
1094              h->mb.cache.ref[l][i8] = -2;
1095
1096         i8 = x264_scan8[0] - 1;
1097         if( h->mb.i_neighbour & MB_LEFT )
1098         {
1099             if( b_mbaff )
1100             {
1101                 h->mb.cache.ref[l][i8+0*8] = ref[h->mb.left_b8[LTOP] + 1 + s8x8*left_index_table->ref[0]];
1102                 h->mb.cache.ref[l][i8+1*8] = ref[h->mb.left_b8[LTOP] + 1 + s8x8*left_index_table->ref[1]];
1103                 h->mb.cache.ref[l][i8+2*8] = ref[h->mb.left_b8[LBOT] + 1 + s8x8*left_index_table->ref[2]];
1104                 h->mb.cache.ref[l][i8+3*8] = ref[h->mb.left_b8[LBOT] + 1 + s8x8*left_index_table->ref[3]];
1105
1106                 CP32( h->mb.cache.mv[l][i8+0*8], mv[h->mb.left_b4[LTOP] + 3 + s4x4*left_index_table->mv[0]] );
1107                 CP32( h->mb.cache.mv[l][i8+1*8], mv[h->mb.left_b4[LTOP] + 3 + s4x4*left_index_table->mv[1]] );
1108                 CP32( h->mb.cache.mv[l][i8+2*8], mv[h->mb.left_b4[LBOT] + 3 + s4x4*left_index_table->mv[2]] );
1109                 CP32( h->mb.cache.mv[l][i8+3*8], mv[h->mb.left_b4[LBOT] + 3 + s4x4*left_index_table->mv[3]] );
1110             }
1111             else
1112             {
1113                 const int ir = h->mb.i_b8_xy - 1;
1114                 const int iv = h->mb.i_b4_xy - 1;
1115                 h->mb.cache.ref[l][i8+0*8] =
1116                 h->mb.cache.ref[l][i8+1*8] = ref[ir + 0*s8x8];
1117                 h->mb.cache.ref[l][i8+2*8] =
1118                 h->mb.cache.ref[l][i8+3*8] = ref[ir + 1*s8x8];
1119
1120                 CP32( h->mb.cache.mv[l][i8+0*8], mv[iv + 0*s4x4] );
1121                 CP32( h->mb.cache.mv[l][i8+1*8], mv[iv + 1*s4x4] );
1122                 CP32( h->mb.cache.mv[l][i8+2*8], mv[iv + 2*s4x4] );
1123                 CP32( h->mb.cache.mv[l][i8+3*8], mv[iv + 3*s4x4] );
1124             }
1125         }
1126         else
1127         {
1128             for( int i = 0; i < 4; i++ )
1129             {
1130                 h->mb.cache.ref[l][i8+i*8] = -2;
1131                 M32( h->mb.cache.mv[l][i8+i*8] ) = 0;
1132             }
1133         }
1134
1135         /* Extra logic for top right mv in mbaff.
1136          * . . . d  . . a .
1137          * . . . e  . . . .
1138          * . . . f  b . c .
1139          * . . . .  . . . .
1140          *
1141          * If the top right of the 4x4 partitions labeled a, b and c in the
1142          * above diagram do not exist, but the entries d, e and f exist (in
1143          * the macroblock to the left) then use those instead.
1144          */
1145         if( b_mbaff && (h->mb.i_neighbour & MB_LEFT) )
1146         {
1147             if( MB_INTERLACED && !h->mb.field[h->mb.i_mb_xy-1] )
1148             {
1149                 h->mb.cache.topright_ref[l][0] = ref[h->mb.left_b8[0] + 1 + s8x8*0];
1150                 h->mb.cache.topright_ref[l][1] = ref[h->mb.left_b8[0] + 1 + s8x8*1];
1151                 h->mb.cache.topright_ref[l][2] = ref[h->mb.left_b8[1] + 1 + s8x8*0];
1152                 CP32( h->mb.cache.topright_mv[l][0], mv[h->mb.left_b4[0] + 3 + s4x4*(left_index_table->mv[0]+1)] );
1153                 CP32( h->mb.cache.topright_mv[l][1], mv[h->mb.left_b4[0] + 3 + s4x4*(left_index_table->mv[1]+1)] );
1154                 CP32( h->mb.cache.topright_mv[l][2], mv[h->mb.left_b4[1] + 3 + s4x4*(left_index_table->mv[2]+1)] );
1155             }
1156             else if( !MB_INTERLACED && h->mb.field[h->mb.i_mb_xy-1] )
1157             {
1158                 // Looking at the bottom field so always take the bottom macroblock of the pair.
1159                 h->mb.cache.topright_ref[l][0] = ref[h->mb.left_b8[0] + 1 + s8x8*2 + s8x8*left_index_table->ref[0]];
1160                 h->mb.cache.topright_ref[l][1] = ref[h->mb.left_b8[0] + 1 + s8x8*2 + s8x8*left_index_table->ref[0]];
1161                 h->mb.cache.topright_ref[l][2] = ref[h->mb.left_b8[0] + 1 + s8x8*2 + s8x8*left_index_table->ref[2]];
1162                 CP32( h->mb.cache.topright_mv[l][0], mv[h->mb.left_b4[0] + 3 + s4x4*4 + s4x4*left_index_table->mv[0]] );
1163                 CP32( h->mb.cache.topright_mv[l][1], mv[h->mb.left_b4[0] + 3 + s4x4*4 + s4x4*left_index_table->mv[1]] );
1164                 CP32( h->mb.cache.topright_mv[l][2], mv[h->mb.left_b4[0] + 3 + s4x4*4 + s4x4*left_index_table->mv[2]] );
1165             }
1166         }
1167
1168         if( h->param.b_cabac )
1169         {
1170             uint8_t (*mvd)[8][2] = h->mb.mvd[l];
1171             if( h->mb.i_neighbour & MB_TOP )
1172                 CP64( h->mb.cache.mvd[l][x264_scan8[0] - 8], mvd[top][0] );
1173             else
1174                 M64( h->mb.cache.mvd[l][x264_scan8[0] - 8] ) = 0;
1175
1176             if( h->mb.i_neighbour & MB_LEFT && (!b_mbaff || h->mb.cache.ref[l][x264_scan8[0]-1] >= 0) )
1177             {
1178                 CP16( h->mb.cache.mvd[l][x264_scan8[0 ] - 1], mvd[left[LTOP]][left_index_table->intra[0]] );
1179                 CP16( h->mb.cache.mvd[l][x264_scan8[2 ] - 1], mvd[left[LTOP]][left_index_table->intra[1]] );
1180             }
1181             else
1182             {
1183                 M16( h->mb.cache.mvd[l][x264_scan8[0]-1+0*8] ) = 0;
1184                 M16( h->mb.cache.mvd[l][x264_scan8[0]-1+1*8] ) = 0;
1185             }
1186             if( h->mb.i_neighbour & MB_LEFT && (!b_mbaff || h->mb.cache.ref[l][x264_scan8[0]-1+2*8] >=0) )
1187             {
1188                 CP16( h->mb.cache.mvd[l][x264_scan8[8 ] - 1], mvd[left[LBOT]][left_index_table->intra[2]] );
1189                 CP16( h->mb.cache.mvd[l][x264_scan8[10] - 1], mvd[left[LBOT]][left_index_table->intra[3]] );
1190             }
1191             else
1192             {
1193                 M16( h->mb.cache.mvd[l][x264_scan8[0]-1+2*8] ) = 0;
1194                 M16( h->mb.cache.mvd[l][x264_scan8[0]-1+3*8] ) = 0;
1195             }
1196         }
1197
1198         /* If motion vectors are cached from frame macroblocks but this
1199          * macroblock is a field macroblock then the motion vector must be
1200          * halved. Similarly, motion vectors from field macroblocks are doubled. */
1201         if( b_mbaff )
1202         {
1203 #define MAP_MVS\
1204                 if( FIELD_DIFFERENT(h->mb.i_mb_topleft_xy) )\
1205                     MAP_F2F(mv, ref, x264_scan8[0] - 1 - 1*8)\
1206                 if( FIELD_DIFFERENT(top) )\
1207                 {\
1208                     MAP_F2F(mv, ref, x264_scan8[0] + 0 - 1*8)\
1209                     MAP_F2F(mv, ref, x264_scan8[0] + 1 - 1*8)\
1210                     MAP_F2F(mv, ref, x264_scan8[0] + 2 - 1*8)\
1211                     MAP_F2F(mv, ref, x264_scan8[0] + 3 - 1*8)\
1212                 }\
1213                 if( FIELD_DIFFERENT(h->mb.i_mb_topright_xy) )\
1214                     MAP_F2F(mv, ref, x264_scan8[0] + 4 - 1*8)\
1215                 if( FIELD_DIFFERENT(left[0]) )\
1216                 {\
1217                     MAP_F2F(mv, ref, x264_scan8[0] - 1 + 0*8)\
1218                     MAP_F2F(mv, ref, x264_scan8[0] - 1 + 1*8)\
1219                     MAP_F2F(mv, ref, x264_scan8[0] - 1 + 2*8)\
1220                     MAP_F2F(mv, ref, x264_scan8[0] - 1 + 3*8)\
1221                     MAP_F2F(topright_mv, topright_ref, 0)\
1222                     MAP_F2F(topright_mv, topright_ref, 1)\
1223                     MAP_F2F(topright_mv, topright_ref, 2)\
1224                 }
1225
1226             if( MB_INTERLACED )
1227             {
1228 #define FIELD_DIFFERENT(macroblock) (macroblock >= 0 && !h->mb.field[macroblock])
1229 #define MAP_F2F(varmv, varref, index)\
1230                 if( h->mb.cache.varref[l][index] >= 0 )\
1231                 {\
1232                     h->mb.cache.varref[l][index] <<= 1;\
1233                     h->mb.cache.varmv[l][index][1] /= 2;\
1234                     h->mb.cache.mvd[l][index][1] >>= 1;\
1235                 }
1236                 MAP_MVS
1237 #undef MAP_F2F
1238 #undef FIELD_DIFFERENT
1239             }
1240             else
1241             {
1242 #define FIELD_DIFFERENT(macroblock) (macroblock >= 0 && h->mb.field[macroblock])
1243 #define MAP_F2F(varmv, varref, index)\
1244                 if( h->mb.cache.varref[l][index] >= 0 )\
1245                 {\
1246                     h->mb.cache.varref[l][index] >>= 1;\
1247                     h->mb.cache.varmv[l][index][1] <<= 1;\
1248                     h->mb.cache.mvd[l][index][1] <<= 1;\
1249                 }
1250                 MAP_MVS
1251 #undef MAP_F2F
1252 #undef FIELD_DIFFERENT
1253             }
1254         }
1255     }
1256
1257     if( b_mbaff && mb_x == 0 && !(mb_y&1) && mb_y > 0 )
1258         h->mb.field_decoding_flag = h->mb.field[h->mb.i_mb_xy - h->mb.i_mb_stride];
1259
1260     /* Check whether skip here would cause decoder to predict interlace mode incorrectly.
1261      * FIXME: It might be better to change the interlace type rather than forcing a skip to be non-skip. */
1262     h->mb.b_allow_skip = 1;
1263     if( b_mbaff )
1264     {
1265         if( MB_INTERLACED != h->mb.field_decoding_flag &&
1266             h->mb.i_mb_prev_xy >= 0 && IS_SKIP(h->mb.type[h->mb.i_mb_prev_xy]) )
1267             h->mb.b_allow_skip = 0;
1268         if( (mb_y&1) && IS_SKIP(h->mb.type[h->mb.i_mb_xy - h->mb.i_mb_stride]) )
1269         {
1270             if( h->mb.i_neighbour & MB_LEFT )
1271             {
1272                 if( h->mb.field[h->mb.i_mb_xy - 1] != MB_INTERLACED )
1273                     h->mb.b_allow_skip = 0;
1274             }
1275             else if( h->mb.i_neighbour & MB_TOP )
1276             {
1277                 if( h->mb.field[h->mb.i_mb_top_xy] != MB_INTERLACED )
1278                     h->mb.b_allow_skip = 0;
1279             }
1280             else // Frame mb pair is predicted
1281             {
1282                 if( MB_INTERLACED )
1283                     h->mb.b_allow_skip = 0;
1284             }
1285         }
1286     }
1287
1288     if( h->param.b_cabac )
1289     {
1290         if( b_mbaff )
1291         {
1292             int left_xy, top_xy;
1293             /* Neighbours here are calculated based on field_decoding_flag */
1294             int mb_xy = mb_x + (mb_y&~1)*h->mb.i_mb_stride;
1295             left_xy = mb_xy - 1;
1296             if( (mb_y&1) && mb_x > 0 && h->mb.field_decoding_flag == h->mb.field[left_xy] )
1297                 left_xy += h->mb.i_mb_stride;
1298             if( h->mb.field_decoding_flag )
1299             {
1300                 top_xy = mb_xy - h->mb.i_mb_stride;
1301                 if( !(mb_y&1) && top_xy >= 0 && h->mb.slice_table[top_xy] == h->sh.i_first_mb && h->mb.field[top_xy] )
1302                     top_xy -= h->mb.i_mb_stride;
1303             }
1304             else
1305                 top_xy = mb_x + (mb_y-1)*h->mb.i_mb_stride;
1306
1307             h->mb.cache.i_neighbour_skip =   (mb_x >  0 && h->mb.slice_table[left_xy] == h->sh.i_first_mb && !IS_SKIP( h->mb.type[left_xy] ))
1308                                          + (top_xy >= 0 && h->mb.slice_table[top_xy]  == h->sh.i_first_mb && !IS_SKIP( h->mb.type[top_xy] ));
1309         }
1310         else
1311         {
1312             h->mb.cache.i_neighbour_skip = ((h->mb.i_neighbour & MB_LEFT) && !IS_SKIP( h->mb.i_mb_type_left[0] ))
1313                                          + ((h->mb.i_neighbour & MB_TOP)  && !IS_SKIP( h->mb.i_mb_type_top ));
1314         }
1315     }
1316
1317     /* load skip */
1318     if( h->sh.i_type == SLICE_TYPE_B )
1319     {
1320         h->mb.bipred_weight = h->mb.bipred_weight_buf[MB_INTERLACED][MB_INTERLACED&(mb_y&1)];
1321         h->mb.dist_scale_factor = h->mb.dist_scale_factor_buf[MB_INTERLACED][MB_INTERLACED&(mb_y&1)];
1322         if( h->param.b_cabac )
1323         {
1324             uint8_t skipbp;
1325             x264_macroblock_cache_skip( h, 0, 0, 4, 4, 0 );
1326             if( b_mbaff )
1327             {
1328                 skipbp = (h->mb.i_neighbour & MB_LEFT) ? h->mb.skipbp[left[LTOP]] : 0;
1329                 h->mb.cache.skip[x264_scan8[0] - 1] = (skipbp >> (1+(left_index_table->mv[0]&~1))) & 1;
1330                 skipbp = (h->mb.i_neighbour & MB_LEFT) ? h->mb.skipbp[left[LBOT]] : 0;
1331                 h->mb.cache.skip[x264_scan8[8] - 1] = (skipbp >> (1+(left_index_table->mv[2]&~1))) & 1;
1332             }
1333             else
1334             {
1335                 skipbp = (h->mb.i_neighbour & MB_LEFT) ? h->mb.skipbp[left[0]] : 0;
1336                 h->mb.cache.skip[x264_scan8[0] - 1] = skipbp & 0x2;
1337                 h->mb.cache.skip[x264_scan8[8] - 1] = skipbp & 0x8;
1338             }
1339             skipbp = (h->mb.i_neighbour & MB_TOP) ? h->mb.skipbp[top] : 0;
1340             h->mb.cache.skip[x264_scan8[0] - 8] = skipbp & 0x4;
1341             h->mb.cache.skip[x264_scan8[4] - 8] = skipbp & 0x8;
1342         }
1343     }
1344
1345     if( h->sh.i_type == SLICE_TYPE_P )
1346         x264_mb_predict_mv_pskip( h, h->mb.cache.pskip_mv );
1347
1348     h->mb.i_neighbour4[0] =
1349     h->mb.i_neighbour8[0] = (h->mb.i_neighbour_intra & (MB_TOP|MB_LEFT|MB_TOPLEFT))
1350                             | ((h->mb.i_neighbour_intra & MB_TOP) ? MB_TOPRIGHT : 0);
1351     h->mb.i_neighbour4[4] =
1352     h->mb.i_neighbour4[1] = MB_LEFT | ((h->mb.i_neighbour_intra & MB_TOP) ? (MB_TOP|MB_TOPLEFT|MB_TOPRIGHT) : 0);
1353     h->mb.i_neighbour4[2] =
1354     h->mb.i_neighbour4[8] =
1355     h->mb.i_neighbour4[10] =
1356     h->mb.i_neighbour8[2] = MB_TOP|MB_TOPRIGHT | ((h->mb.i_neighbour_intra & MB_LEFT) ? (MB_LEFT|MB_TOPLEFT) : 0);
1357     h->mb.i_neighbour4[5] =
1358     h->mb.i_neighbour8[1] = MB_LEFT | (h->mb.i_neighbour_intra & MB_TOPRIGHT)
1359                             | ((h->mb.i_neighbour_intra & MB_TOP) ? MB_TOP|MB_TOPLEFT : 0);
1360 }
1361
1362 void x264_macroblock_cache_load_progressive( x264_t *h, int mb_x, int mb_y )
1363 {
1364     x264_macroblock_cache_load( h, mb_x, mb_y, 0 );
1365 }
1366
1367 void x264_macroblock_cache_load_interlaced( x264_t *h, int mb_x, int mb_y )
1368 {
1369     x264_macroblock_cache_load( h, mb_x, mb_y, 1 );
1370 }
1371
1372 static void x264_macroblock_deblock_strength_mbaff( x264_t *h, uint8_t (*bs)[8][4] )
1373 {
1374     if( (h->mb.i_neighbour & MB_LEFT) && h->mb.field[h->mb.i_mb_left_xy[0]] != MB_INTERLACED )
1375     {
1376         static const uint8_t offset[2][2][8] =
1377         {   {   { 0, 0, 0, 0, 1, 1, 1, 1 },
1378                 { 2, 2, 2, 2, 3, 3, 3, 3 }, },
1379             {   { 0, 1, 2, 3, 0, 1, 2, 3 },
1380                 { 0, 1, 2, 3, 0, 1, 2, 3 }, }
1381         };
1382         ALIGNED_ARRAY_8( uint8_t, tmpbs, [8] );
1383
1384         const uint8_t *off = offset[MB_INTERLACED][h->mb.i_mb_y&1];
1385         uint8_t (*nnz)[48] = h->mb.non_zero_count;
1386
1387         for( int i = 0; i < 8; i++ )
1388         {
1389             int left = h->mb.i_mb_left_xy[MB_INTERLACED ? i>>2 : i&1];
1390             int nnz_this = h->mb.cache.non_zero_count[x264_scan8[0]+8*(i>>1)];
1391             int nnz_left = nnz[left][3 + 4*off[i]];
1392             if( !h->param.b_cabac && h->pps->b_transform_8x8_mode )
1393             {
1394                 int j = off[i]&~1;
1395                 if( h->mb.mb_transform_size[left] )
1396                     nnz_left = !!(M16( &nnz[left][2+4*j] ) | M16( &nnz[left][2+4*(1+j)] ));
1397             }
1398             tmpbs[i] = (nnz_left || nnz_this) ? 2 : 1;
1399         }
1400
1401         if( MB_INTERLACED )
1402         {
1403             CP32( bs[0][0], &tmpbs[0] );
1404             CP32( bs[0][4], &tmpbs[4] );
1405         }
1406         else
1407         {
1408             for( int i = 0; i < 4; i++ ) bs[0][0][i] = tmpbs[2*i];
1409             for( int i = 0; i < 4; i++ ) bs[0][4][i] = tmpbs[1+2*i];
1410         }
1411     }
1412
1413     if( (h->mb.i_neighbour & MB_TOP) && MB_INTERLACED != h->mb.field[h->mb.i_mb_top_xy] )
1414     {
1415         if( !(h->mb.i_mb_y&1) && !MB_INTERLACED )
1416         {
1417             /* Need to filter both fields (even for frame macroblocks).
1418              * Filter top two rows using the top macroblock of the above
1419              * pair and then the bottom one. */
1420             int mbn_xy = h->mb.i_mb_xy - 2 * h->mb.i_mb_stride;
1421             uint8_t *nnz_cur = &h->mb.cache.non_zero_count[x264_scan8[0]];
1422
1423             for( int j = 0; j < 2; j++, mbn_xy += h->mb.i_mb_stride )
1424             {
1425                 uint8_t (*nnz)[48] = h->mb.non_zero_count;
1426
1427                 ALIGNED_4( uint8_t nnz_top[4] );
1428                 CP32( nnz_top, &nnz[mbn_xy][3*4] );
1429
1430                 if( !h->param.b_cabac && h->pps->b_transform_8x8_mode && h->mb.mb_transform_size[mbn_xy] )
1431                 {
1432                     nnz_top[0] = nnz_top[1] = M16( &nnz[mbn_xy][ 8] ) || M16( &nnz[mbn_xy][12] );
1433                     nnz_top[2] = nnz_top[3] = M16( &nnz[mbn_xy][10] ) || M16( &nnz[mbn_xy][14] );
1434                 }
1435
1436                 for( int i = 0; i < 4; i++ )
1437                     bs[1][4*j][i] = (nnz_cur[i] || nnz_top[i]) ? 2 : 1;
1438             }
1439         }
1440         else
1441             for( int i = 0; i < 4; i++ )
1442                 bs[1][0][i] = X264_MAX( bs[1][0][i], 1 );
1443     }
1444 }
1445
1446 void x264_macroblock_deblock_strength( x264_t *h )
1447 {
1448     uint8_t (*bs)[8][4] = h->mb.cache.deblock_strength;
1449     if( IS_INTRA( h->mb.i_type ) )
1450     {
1451         memset( bs[0][1], 3, 3*4*sizeof(uint8_t) );
1452         memset( bs[1][1], 3, 3*4*sizeof(uint8_t) );
1453         return;
1454     }
1455
1456     /* Early termination: in this case, nnz guarantees all edges use strength 2.*/
1457     if( h->mb.b_transform_8x8 && !CHROMA444 )
1458     {
1459         int cbp_mask = 0xf >> CHROMA_V_SHIFT;
1460         if( (h->mb.i_cbp_luma&cbp_mask) == cbp_mask )
1461         {
1462             M32( bs[0][0] ) = 0x02020202;
1463             M32( bs[0][2] ) = 0x02020202;
1464             M32( bs[0][4] ) = 0x02020202;
1465             memset( bs[1][0], 2, 5*4*sizeof(uint8_t) ); /* [1][1] and [1][3] has to be set for 4:2:2 */
1466             return;
1467         }
1468     }
1469
1470     int neighbour_changed = 0;
1471     if( h->sh.i_disable_deblocking_filter_idc != 2 )
1472     {
1473         neighbour_changed = h->mb.i_neighbour_frame&~h->mb.i_neighbour;
1474         h->mb.i_neighbour = h->mb.i_neighbour_frame;
1475     }
1476
1477     /* MBAFF deblock uses different left neighbors from encoding */
1478     if( SLICE_MBAFF && (h->mb.i_neighbour & MB_LEFT) && (h->mb.field[h->mb.i_mb_xy - 1] != MB_INTERLACED) )
1479     {
1480         h->mb.i_mb_left_xy[1] =
1481         h->mb.i_mb_left_xy[0] = h->mb.i_mb_xy - 1;
1482         if( h->mb.i_mb_y&1 )
1483             h->mb.i_mb_left_xy[0] -= h->mb.i_mb_stride;
1484         else
1485             h->mb.i_mb_left_xy[1] += h->mb.i_mb_stride;
1486     }
1487
1488     /* If we have multiple slices and we're deblocking on slice edges, we
1489      * have to reload neighbour data. */
1490     if( neighbour_changed )
1491     {
1492         int top_y = h->mb.i_mb_top_y;
1493         int top_8x8 = (2*top_y+1) * h->mb.i_b8_stride + 2*h->mb.i_mb_x;
1494         int top_4x4 = (4*top_y+3) * h->mb.i_b4_stride + 4*h->mb.i_mb_x;
1495         int s8x8 = h->mb.i_b8_stride;
1496         int s4x4 = h->mb.i_b4_stride;
1497
1498         uint8_t (*nnz)[48] = h->mb.non_zero_count;
1499         const x264_left_table_t *left_index_table = SLICE_MBAFF ? h->mb.left_index_table : &left_indices[3];
1500
1501         if( neighbour_changed & MB_TOP )
1502             CP32( &h->mb.cache.non_zero_count[x264_scan8[0] - 8], &nnz[h->mb.i_mb_top_xy][12] );
1503
1504         if( neighbour_changed & MB_LEFT )
1505         {
1506             int *left = h->mb.i_mb_left_xy;
1507             h->mb.cache.non_zero_count[x264_scan8[0 ] - 1] = nnz[left[0]][left_index_table->nnz[0]];
1508             h->mb.cache.non_zero_count[x264_scan8[2 ] - 1] = nnz[left[0]][left_index_table->nnz[1]];
1509             h->mb.cache.non_zero_count[x264_scan8[8 ] - 1] = nnz[left[1]][left_index_table->nnz[2]];
1510             h->mb.cache.non_zero_count[x264_scan8[10] - 1] = nnz[left[1]][left_index_table->nnz[3]];
1511         }
1512
1513         for( int l = 0; l <= (h->sh.i_type == SLICE_TYPE_B); l++ )
1514         {
1515             int16_t (*mv)[2] = h->mb.mv[l];
1516             int8_t *ref = h->mb.ref[l];
1517
1518             int i8 = x264_scan8[0] - 8;
1519             if( neighbour_changed & MB_TOP )
1520             {
1521                 h->mb.cache.ref[l][i8+0] =
1522                 h->mb.cache.ref[l][i8+1] = ref[top_8x8 + 0];
1523                 h->mb.cache.ref[l][i8+2] =
1524                 h->mb.cache.ref[l][i8+3] = ref[top_8x8 + 1];
1525                 CP128( h->mb.cache.mv[l][i8], mv[top_4x4] );
1526             }
1527
1528             i8 = x264_scan8[0] - 1;
1529             if( neighbour_changed & MB_LEFT )
1530             {
1531                 h->mb.cache.ref[l][i8+0*8] =
1532                 h->mb.cache.ref[l][i8+1*8] = ref[h->mb.left_b8[0] + 1 + s8x8*left_index_table->ref[0]];
1533                 h->mb.cache.ref[l][i8+2*8] =
1534                 h->mb.cache.ref[l][i8+3*8] = ref[h->mb.left_b8[1] + 1 + s8x8*left_index_table->ref[2]];
1535
1536                 CP32( h->mb.cache.mv[l][i8+0*8], mv[h->mb.left_b4[0] + 3 + s4x4*left_index_table->mv[0]] );
1537                 CP32( h->mb.cache.mv[l][i8+1*8], mv[h->mb.left_b4[0] + 3 + s4x4*left_index_table->mv[1]] );
1538                 CP32( h->mb.cache.mv[l][i8+2*8], mv[h->mb.left_b4[1] + 3 + s4x4*left_index_table->mv[2]] );
1539                 CP32( h->mb.cache.mv[l][i8+3*8], mv[h->mb.left_b4[1] + 3 + s4x4*left_index_table->mv[3]] );
1540             }
1541         }
1542     }
1543
1544     if( h->param.analyse.i_weighted_pred == X264_WEIGHTP_SMART && h->sh.i_type == SLICE_TYPE_P )
1545     {
1546         /* Handle reference frame duplicates */
1547         int i8 = x264_scan8[0] - 8;
1548         h->mb.cache.ref[0][i8+0] =
1549         h->mb.cache.ref[0][i8+1] = deblock_ref_table(h->mb.cache.ref[0][i8+0]);
1550         h->mb.cache.ref[0][i8+2] =
1551         h->mb.cache.ref[0][i8+3] = deblock_ref_table(h->mb.cache.ref[0][i8+2]);
1552
1553         i8 = x264_scan8[0] - 1;
1554         h->mb.cache.ref[0][i8+0*8] =
1555         h->mb.cache.ref[0][i8+1*8] = deblock_ref_table(h->mb.cache.ref[0][i8+0*8]);
1556         h->mb.cache.ref[0][i8+2*8] =
1557         h->mb.cache.ref[0][i8+3*8] = deblock_ref_table(h->mb.cache.ref[0][i8+2*8]);
1558
1559         int ref0 = deblock_ref_table(h->mb.cache.ref[0][x264_scan8[ 0]]);
1560         int ref1 = deblock_ref_table(h->mb.cache.ref[0][x264_scan8[ 4]]);
1561         int ref2 = deblock_ref_table(h->mb.cache.ref[0][x264_scan8[ 8]]);
1562         int ref3 = deblock_ref_table(h->mb.cache.ref[0][x264_scan8[12]]);
1563         uint32_t reftop = pack16to32( (uint8_t)ref0, (uint8_t)ref1 ) * 0x0101;
1564         uint32_t refbot = pack16to32( (uint8_t)ref2, (uint8_t)ref3 ) * 0x0101;
1565
1566         M32( &h->mb.cache.ref[0][x264_scan8[0]+8*0] ) = reftop;
1567         M32( &h->mb.cache.ref[0][x264_scan8[0]+8*1] ) = reftop;
1568         M32( &h->mb.cache.ref[0][x264_scan8[0]+8*2] ) = refbot;
1569         M32( &h->mb.cache.ref[0][x264_scan8[0]+8*3] ) = refbot;
1570     }
1571
1572     /* Munge NNZ for cavlc + 8x8dct */
1573     if( !h->param.b_cabac && h->pps->b_transform_8x8_mode )
1574     {
1575         uint8_t (*nnz)[48] = h->mb.non_zero_count;
1576         int top = h->mb.i_mb_top_xy;
1577         int *left = h->mb.i_mb_left_xy;
1578
1579         if( (h->mb.i_neighbour & MB_TOP) && h->mb.mb_transform_size[top] )
1580         {
1581             int i8 = x264_scan8[0] - 8;
1582             int nnz_top0 = M16( &nnz[top][8] ) | M16( &nnz[top][12] );
1583             int nnz_top1 = M16( &nnz[top][10] ) | M16( &nnz[top][14] );
1584             M16( &h->mb.cache.non_zero_count[i8+0] ) = nnz_top0 ? 0x0101 : 0;
1585             M16( &h->mb.cache.non_zero_count[i8+2] ) = nnz_top1 ? 0x0101 : 0;
1586         }
1587
1588         if( h->mb.i_neighbour & MB_LEFT )
1589         {
1590             int i8 = x264_scan8[0] - 1;
1591             if( h->mb.mb_transform_size[left[0]] )
1592             {
1593                 int nnz_left0 = M16( &nnz[left[0]][2] ) | M16( &nnz[left[0]][6] );
1594                 h->mb.cache.non_zero_count[i8+8*0] = !!nnz_left0;
1595                 h->mb.cache.non_zero_count[i8+8*1] = !!nnz_left0;
1596             }
1597             if( h->mb.mb_transform_size[left[1]] )
1598             {
1599                 int nnz_left1 = M16( &nnz[left[1]][10] ) | M16( &nnz[left[1]][14] );
1600                 h->mb.cache.non_zero_count[i8+8*2] = !!nnz_left1;
1601                 h->mb.cache.non_zero_count[i8+8*3] = !!nnz_left1;
1602             }
1603         }
1604
1605         if( h->mb.b_transform_8x8 )
1606         {
1607             int nnz0 = M16( &h->mb.cache.non_zero_count[x264_scan8[ 0]] ) | M16( &h->mb.cache.non_zero_count[x264_scan8[ 2]] );
1608             int nnz1 = M16( &h->mb.cache.non_zero_count[x264_scan8[ 4]] ) | M16( &h->mb.cache.non_zero_count[x264_scan8[ 6]] );
1609             int nnz2 = M16( &h->mb.cache.non_zero_count[x264_scan8[ 8]] ) | M16( &h->mb.cache.non_zero_count[x264_scan8[10]] );
1610             int nnz3 = M16( &h->mb.cache.non_zero_count[x264_scan8[12]] ) | M16( &h->mb.cache.non_zero_count[x264_scan8[14]] );
1611             uint32_t nnztop = pack16to32( !!nnz0, !!nnz1 ) * 0x0101;
1612             uint32_t nnzbot = pack16to32( !!nnz2, !!nnz3 ) * 0x0101;
1613
1614             M32( &h->mb.cache.non_zero_count[x264_scan8[0]+8*0] ) = nnztop;
1615             M32( &h->mb.cache.non_zero_count[x264_scan8[0]+8*1] ) = nnztop;
1616             M32( &h->mb.cache.non_zero_count[x264_scan8[0]+8*2] ) = nnzbot;
1617             M32( &h->mb.cache.non_zero_count[x264_scan8[0]+8*3] ) = nnzbot;
1618         }
1619     }
1620
1621     h->loopf.deblock_strength( h->mb.cache.non_zero_count, h->mb.cache.ref, h->mb.cache.mv,
1622                                bs, 4 >> MB_INTERLACED, h->sh.i_type == SLICE_TYPE_B );
1623
1624     if( SLICE_MBAFF )
1625         x264_macroblock_deblock_strength_mbaff( h, bs );
1626 }
1627
1628 static void ALWAYS_INLINE x264_macroblock_store_pic( x264_t *h, int mb_x, int mb_y, int i, int b_chroma, int b_mbaff )
1629 {
1630     int height = b_chroma ? 16>>CHROMA_V_SHIFT : 16;
1631     int i_stride = h->fdec->i_stride[i];
1632     int i_stride2 = i_stride << (b_mbaff && MB_INTERLACED);
1633     int i_pix_offset = (b_mbaff && MB_INTERLACED)
1634                      ? 16 * mb_x + height * (mb_y&~1) * i_stride + (mb_y&1) * i_stride
1635                      : 16 * mb_x + height * mb_y * i_stride;
1636     if( b_chroma )
1637         h->mc.store_interleave_chroma( &h->fdec->plane[1][i_pix_offset], i_stride2, h->mb.pic.p_fdec[1], h->mb.pic.p_fdec[2], height );
1638     else
1639         h->mc.copy[PIXEL_16x16]( &h->fdec->plane[i][i_pix_offset], i_stride2, h->mb.pic.p_fdec[i], FDEC_STRIDE, 16 );
1640 }
1641
1642 static void ALWAYS_INLINE x264_macroblock_backup_intra( x264_t *h, int mb_x, int mb_y, int b_mbaff )
1643 {
1644     /* In MBAFF we store the last two rows in intra_border_backup[0] and [1].
1645      * For progressive mbs this is the bottom two rows, and for interlaced the
1646      * bottom row of each field. We also store samples needed for the next
1647      * mbpair in intra_border_backup[2]. */
1648     int backup_dst = !b_mbaff ? (mb_y&1) : (mb_y&1) ? 1 : MB_INTERLACED ? 0 : 2;
1649     memcpy( &h->intra_border_backup[backup_dst][0][mb_x*16  ], h->mb.pic.p_fdec[0]+FDEC_STRIDE*15, 16*sizeof(pixel) );
1650     if( CHROMA444 )
1651     {
1652         memcpy( &h->intra_border_backup[backup_dst][1][mb_x*16  ], h->mb.pic.p_fdec[1]+FDEC_STRIDE*15, 16*sizeof(pixel) );
1653         memcpy( &h->intra_border_backup[backup_dst][2][mb_x*16  ], h->mb.pic.p_fdec[2]+FDEC_STRIDE*15, 16*sizeof(pixel) );
1654     }
1655     else
1656     {
1657         int backup_src = (15>>CHROMA_V_SHIFT) * FDEC_STRIDE;
1658         memcpy( &h->intra_border_backup[backup_dst][1][mb_x*16  ], h->mb.pic.p_fdec[1]+backup_src, 8*sizeof(pixel) );
1659         memcpy( &h->intra_border_backup[backup_dst][1][mb_x*16+8], h->mb.pic.p_fdec[2]+backup_src, 8*sizeof(pixel) );
1660     }
1661     if( b_mbaff )
1662     {
1663         if( mb_y&1 )
1664         {
1665             int backup_src = (MB_INTERLACED ? 7 : 14) * FDEC_STRIDE;
1666             backup_dst = MB_INTERLACED ? 2 : 0;
1667             memcpy( &h->intra_border_backup[backup_dst][0][mb_x*16  ], h->mb.pic.p_fdec[0]+backup_src, 16*sizeof(pixel) );
1668             if( CHROMA444 )
1669             {
1670                 memcpy( &h->intra_border_backup[backup_dst][1][mb_x*16  ], h->mb.pic.p_fdec[1]+backup_src, 16*sizeof(pixel) );
1671                 memcpy( &h->intra_border_backup[backup_dst][2][mb_x*16  ], h->mb.pic.p_fdec[2]+backup_src, 16*sizeof(pixel) );
1672             }
1673             else
1674             {
1675                 if( CHROMA_FORMAT == CHROMA_420 )
1676                     backup_src = (MB_INTERLACED ? 3 : 6) * FDEC_STRIDE;
1677                 memcpy( &h->intra_border_backup[backup_dst][1][mb_x*16  ], h->mb.pic.p_fdec[1]+backup_src,  8*sizeof(pixel) );
1678                 memcpy( &h->intra_border_backup[backup_dst][1][mb_x*16+8], h->mb.pic.p_fdec[2]+backup_src,  8*sizeof(pixel) );
1679             }
1680         }
1681     }
1682 }
1683
1684 void x264_macroblock_cache_save( x264_t *h )
1685 {
1686     const int i_mb_xy = h->mb.i_mb_xy;
1687     const int i_mb_type = x264_mb_type_fix[h->mb.i_type];
1688     const int s8x8 = h->mb.i_b8_stride;
1689     const int s4x4 = h->mb.i_b4_stride;
1690     const int i_mb_4x4 = h->mb.i_b4_xy;
1691     const int i_mb_8x8 = h->mb.i_b8_xy;
1692
1693     /* GCC pessimizes direct stores to heap-allocated arrays due to aliasing. */
1694     /* By only dereferencing them once, we avoid this issue. */
1695     int8_t *i4x4 = h->mb.intra4x4_pred_mode[i_mb_xy];
1696     uint8_t *nnz = h->mb.non_zero_count[i_mb_xy];
1697
1698     if( SLICE_MBAFF )
1699     {
1700         x264_macroblock_backup_intra( h, h->mb.i_mb_x, h->mb.i_mb_y, 1 );
1701         x264_macroblock_store_pic( h, h->mb.i_mb_x, h->mb.i_mb_y, 0, 0, 1 );
1702         if( CHROMA444 )
1703         {
1704             x264_macroblock_store_pic( h, h->mb.i_mb_x, h->mb.i_mb_y, 1, 0, 1 );
1705             x264_macroblock_store_pic( h, h->mb.i_mb_x, h->mb.i_mb_y, 2, 0, 1 );
1706         }
1707         else
1708             x264_macroblock_store_pic( h, h->mb.i_mb_x, h->mb.i_mb_y, 1, 1, 1 );
1709     }
1710     else
1711     {
1712         x264_macroblock_backup_intra( h, h->mb.i_mb_x, h->mb.i_mb_y, 0 );
1713         x264_macroblock_store_pic( h, h->mb.i_mb_x, h->mb.i_mb_y, 0, 0, 0 );
1714         if( CHROMA444 )
1715         {
1716             x264_macroblock_store_pic( h, h->mb.i_mb_x, h->mb.i_mb_y, 1, 0, 0 );
1717             x264_macroblock_store_pic( h, h->mb.i_mb_x, h->mb.i_mb_y, 2, 0, 0 );
1718         }
1719         else
1720             x264_macroblock_store_pic( h, h->mb.i_mb_x, h->mb.i_mb_y, 1, 1, 0 );
1721     }
1722
1723     x264_prefetch_fenc( h, h->fdec, h->mb.i_mb_x, h->mb.i_mb_y );
1724
1725     h->mb.type[i_mb_xy] = i_mb_type;
1726     h->mb.slice_table[i_mb_xy] = h->sh.i_first_mb;
1727     h->mb.partition[i_mb_xy] = IS_INTRA( i_mb_type ) ? D_16x16 : h->mb.i_partition;
1728     h->mb.i_mb_prev_xy = i_mb_xy;
1729
1730     /* save intra4x4 */
1731     if( i_mb_type == I_4x4 )
1732     {
1733         CP32( &i4x4[0], &h->mb.cache.intra4x4_pred_mode[x264_scan8[10]] );
1734         M32( &i4x4[4] ) = pack8to32( h->mb.cache.intra4x4_pred_mode[x264_scan8[5] ],
1735                                      h->mb.cache.intra4x4_pred_mode[x264_scan8[7] ],
1736                                      h->mb.cache.intra4x4_pred_mode[x264_scan8[13] ], 0);
1737     }
1738     else if( !h->param.b_constrained_intra || IS_INTRA(i_mb_type) )
1739         M64( i4x4 ) = I_PRED_4x4_DC * 0x0101010101010101ULL;
1740     else
1741         M64( i4x4 ) = (uint8_t)(-1) * 0x0101010101010101ULL;
1742
1743
1744     if( i_mb_type == I_PCM )
1745     {
1746         h->mb.qp[i_mb_xy] = 0;
1747         h->mb.i_last_dqp = 0;
1748         h->mb.i_cbp_chroma = CHROMA444 ? 0 : 2;
1749         h->mb.i_cbp_luma = 0xf;
1750         h->mb.cbp[i_mb_xy] = (h->mb.i_cbp_chroma << 4) | h->mb.i_cbp_luma | 0x700;
1751         h->mb.b_transform_8x8 = 0;
1752         for( int i = 0; i < 48; i++ )
1753             h->mb.cache.non_zero_count[x264_scan8[i]] = h->param.b_cabac ? 1 : 16;
1754     }
1755     else
1756     {
1757         if( h->mb.i_type != I_16x16 && h->mb.i_cbp_luma == 0 && h->mb.i_cbp_chroma == 0 )
1758             h->mb.i_qp = h->mb.i_last_qp;
1759         h->mb.qp[i_mb_xy] = h->mb.i_qp;
1760         h->mb.i_last_dqp = h->mb.i_qp - h->mb.i_last_qp;
1761         h->mb.i_last_qp = h->mb.i_qp;
1762     }
1763
1764     /* save non zero count */
1765     CP32( &nnz[ 0+0*4], &h->mb.cache.non_zero_count[x264_scan8[ 0]] );
1766     CP32( &nnz[ 0+1*4], &h->mb.cache.non_zero_count[x264_scan8[ 2]] );
1767     CP32( &nnz[ 0+2*4], &h->mb.cache.non_zero_count[x264_scan8[ 8]] );
1768     CP32( &nnz[ 0+3*4], &h->mb.cache.non_zero_count[x264_scan8[10]] );
1769     CP32( &nnz[16+0*4], &h->mb.cache.non_zero_count[x264_scan8[16+0]] );
1770     CP32( &nnz[16+1*4], &h->mb.cache.non_zero_count[x264_scan8[16+2]] );
1771     CP32( &nnz[32+0*4], &h->mb.cache.non_zero_count[x264_scan8[32+0]] );
1772     CP32( &nnz[32+1*4], &h->mb.cache.non_zero_count[x264_scan8[32+2]] );
1773     if( CHROMA_FORMAT >= CHROMA_422 )
1774     {
1775         CP32( &nnz[16+2*4], &h->mb.cache.non_zero_count[x264_scan8[16+ 8]] );
1776         CP32( &nnz[16+3*4], &h->mb.cache.non_zero_count[x264_scan8[16+10]] );
1777         CP32( &nnz[32+2*4], &h->mb.cache.non_zero_count[x264_scan8[32+ 8]] );
1778         CP32( &nnz[32+3*4], &h->mb.cache.non_zero_count[x264_scan8[32+10]] );
1779     }
1780
1781     if( h->mb.i_cbp_luma == 0 && h->mb.i_type != I_8x8 )
1782         h->mb.b_transform_8x8 = 0;
1783     h->mb.mb_transform_size[i_mb_xy] = h->mb.b_transform_8x8;
1784
1785     if( h->sh.i_type != SLICE_TYPE_I )
1786     {
1787         int16_t (*mv0)[2] = &h->mb.mv[0][i_mb_4x4];
1788         int16_t (*mv1)[2] = &h->mb.mv[1][i_mb_4x4];
1789         int8_t *ref0 = &h->mb.ref[0][i_mb_8x8];
1790         int8_t *ref1 = &h->mb.ref[1][i_mb_8x8];
1791         if( !IS_INTRA( i_mb_type ) )
1792         {
1793             ref0[0+0*s8x8] = h->mb.cache.ref[0][x264_scan8[0]];
1794             ref0[1+0*s8x8] = h->mb.cache.ref[0][x264_scan8[4]];
1795             ref0[0+1*s8x8] = h->mb.cache.ref[0][x264_scan8[8]];
1796             ref0[1+1*s8x8] = h->mb.cache.ref[0][x264_scan8[12]];
1797             CP128( &mv0[0*s4x4], h->mb.cache.mv[0][x264_scan8[0]+8*0] );
1798             CP128( &mv0[1*s4x4], h->mb.cache.mv[0][x264_scan8[0]+8*1] );
1799             CP128( &mv0[2*s4x4], h->mb.cache.mv[0][x264_scan8[0]+8*2] );
1800             CP128( &mv0[3*s4x4], h->mb.cache.mv[0][x264_scan8[0]+8*3] );
1801             if( h->sh.i_type == SLICE_TYPE_B )
1802             {
1803                 ref1[0+0*s8x8] = h->mb.cache.ref[1][x264_scan8[0]];
1804                 ref1[1+0*s8x8] = h->mb.cache.ref[1][x264_scan8[4]];
1805                 ref1[0+1*s8x8] = h->mb.cache.ref[1][x264_scan8[8]];
1806                 ref1[1+1*s8x8] = h->mb.cache.ref[1][x264_scan8[12]];
1807                 CP128( &mv1[0*s4x4], h->mb.cache.mv[1][x264_scan8[0]+8*0] );
1808                 CP128( &mv1[1*s4x4], h->mb.cache.mv[1][x264_scan8[0]+8*1] );
1809                 CP128( &mv1[2*s4x4], h->mb.cache.mv[1][x264_scan8[0]+8*2] );
1810                 CP128( &mv1[3*s4x4], h->mb.cache.mv[1][x264_scan8[0]+8*3] );
1811             }
1812         }
1813         else
1814         {
1815             M16( &ref0[0*s8x8] ) = (uint8_t)(-1) * 0x0101;
1816             M16( &ref0[1*s8x8] ) = (uint8_t)(-1) * 0x0101;
1817             M128( &mv0[0*s4x4] ) = M128_ZERO;
1818             M128( &mv0[1*s4x4] ) = M128_ZERO;
1819             M128( &mv0[2*s4x4] ) = M128_ZERO;
1820             M128( &mv0[3*s4x4] ) = M128_ZERO;
1821             if( h->sh.i_type == SLICE_TYPE_B )
1822             {
1823                 M16( &ref1[0*s8x8] ) = (uint8_t)(-1) * 0x0101;
1824                 M16( &ref1[1*s8x8] ) = (uint8_t)(-1) * 0x0101;
1825                 M128( &mv1[0*s4x4] ) = M128_ZERO;
1826                 M128( &mv1[1*s4x4] ) = M128_ZERO;
1827                 M128( &mv1[2*s4x4] ) = M128_ZERO;
1828                 M128( &mv1[3*s4x4] ) = M128_ZERO;
1829             }
1830         }
1831     }
1832
1833     if( h->param.b_cabac )
1834     {
1835         uint8_t (*mvd0)[2] = h->mb.mvd[0][i_mb_xy];
1836         uint8_t (*mvd1)[2] = h->mb.mvd[1][i_mb_xy];
1837         if( IS_INTRA(i_mb_type) && i_mb_type != I_PCM )
1838             h->mb.chroma_pred_mode[i_mb_xy] = x264_mb_chroma_pred_mode_fix[h->mb.i_chroma_pred_mode];
1839         else
1840             h->mb.chroma_pred_mode[i_mb_xy] = I_PRED_CHROMA_DC;
1841
1842         if( (0x3FF30 >> i_mb_type) & 1 ) /* !INTRA && !SKIP && !DIRECT */
1843         {
1844             CP64( mvd0[0], h->mb.cache.mvd[0][x264_scan8[10]] );
1845             CP16( mvd0[4], h->mb.cache.mvd[0][x264_scan8[5 ]] );
1846             CP16( mvd0[5], h->mb.cache.mvd[0][x264_scan8[7 ]] );
1847             CP16( mvd0[6], h->mb.cache.mvd[0][x264_scan8[13]] );
1848             if( h->sh.i_type == SLICE_TYPE_B )
1849             {
1850                 CP64( mvd1[0], h->mb.cache.mvd[1][x264_scan8[10]] );
1851                 CP16( mvd1[4], h->mb.cache.mvd[1][x264_scan8[5 ]] );
1852                 CP16( mvd1[5], h->mb.cache.mvd[1][x264_scan8[7 ]] );
1853                 CP16( mvd1[6], h->mb.cache.mvd[1][x264_scan8[13]] );
1854             }
1855         }
1856         else
1857         {
1858             M128( mvd0[0] ) = M128_ZERO;
1859             if( h->sh.i_type == SLICE_TYPE_B )
1860                 M128( mvd1[0] ) = M128_ZERO;
1861         }
1862
1863         if( h->sh.i_type == SLICE_TYPE_B )
1864         {
1865             if( i_mb_type == B_SKIP || i_mb_type == B_DIRECT )
1866                 h->mb.skipbp[i_mb_xy] = 0xf;
1867             else if( i_mb_type == B_8x8 )
1868             {
1869                 int skipbp = ( h->mb.i_sub_partition[0] == D_DIRECT_8x8 ) << 0;
1870                 skipbp    |= ( h->mb.i_sub_partition[1] == D_DIRECT_8x8 ) << 1;
1871                 skipbp    |= ( h->mb.i_sub_partition[2] == D_DIRECT_8x8 ) << 2;
1872                 skipbp    |= ( h->mb.i_sub_partition[3] == D_DIRECT_8x8 ) << 3;
1873                 h->mb.skipbp[i_mb_xy] = skipbp;
1874             }
1875             else
1876                 h->mb.skipbp[i_mb_xy] = 0;
1877         }
1878     }
1879 }
1880
1881
1882 void x264_macroblock_bipred_init( x264_t *h )
1883 {
1884     for( int mbfield = 0; mbfield <= SLICE_MBAFF; mbfield++ )
1885         for( int field = 0; field <= SLICE_MBAFF; field++ )
1886             for( int i_ref0 = 0; i_ref0 < (h->i_ref[0]<<mbfield); i_ref0++ )
1887             {
1888                 x264_frame_t *l0 = h->fref[0][i_ref0>>mbfield];
1889                 int poc0 = l0->i_poc + mbfield*l0->i_delta_poc[field^(i_ref0&1)];
1890                 for( int i_ref1 = 0; i_ref1 < (h->i_ref[1]<<mbfield); i_ref1++ )
1891                 {
1892                     int dist_scale_factor;
1893                     x264_frame_t *l1 = h->fref[1][i_ref1>>mbfield];
1894                     int cur_poc = h->fdec->i_poc + mbfield*h->fdec->i_delta_poc[field];
1895                     int poc1 = l1->i_poc + mbfield*l1->i_delta_poc[field^(i_ref1&1)];
1896                     int td = x264_clip3( poc1 - poc0, -128, 127 );
1897                     if( td == 0 /* || pic0 is a long-term ref */ )
1898                         dist_scale_factor = 256;
1899                     else
1900                     {
1901                         int tb = x264_clip3( cur_poc - poc0, -128, 127 );
1902                         int tx = (16384 + (abs(td) >> 1)) / td;
1903                         dist_scale_factor = x264_clip3( (tb * tx + 32) >> 6, -1024, 1023 );
1904                     }
1905
1906                     h->mb.dist_scale_factor_buf[mbfield][field][i_ref0][i_ref1] = dist_scale_factor;
1907
1908                     dist_scale_factor >>= 2;
1909                     if( h->param.analyse.b_weighted_bipred
1910                           && dist_scale_factor >= -64
1911                           && dist_scale_factor <= 128 )
1912                     {
1913                         h->mb.bipred_weight_buf[mbfield][field][i_ref0][i_ref1] = 64 - dist_scale_factor;
1914                         // ssse3 implementation of biweight doesn't support the extrema.
1915                         // if we ever generate them, we'll have to drop that optimization.
1916                         assert( dist_scale_factor >= -63 && dist_scale_factor <= 127 );
1917                     }
1918                     else
1919                         h->mb.bipred_weight_buf[mbfield][field][i_ref0][i_ref1] = 32;
1920                 }
1921             }
1922 }
1923