1 /*****************************************************************************
2 * macroblock.c: macroblock common functions
3 *****************************************************************************
4 * Copyright (C) 2003-2013 x264 project
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>
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.
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.
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.
25 * This program is also available under a commercial proprietary license.
26 * For more information, contact us at licensing@x264.com.
27 *****************************************************************************/
30 #include "encoder/me.h"
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] );
38 static NOINLINE void x264_mb_mc_0xywh( x264_t *h, int x, int y, int width, int height )
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;
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;
59 int offset = (4*FDEC_STRIDE>>v_shift)*y + 2*x;
60 height = 4*height >> v_shift;
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 );
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 );
77 static NOINLINE void x264_mb_mc_1xywh( x264_t *h, int x, int y, int width, int height )
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;
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;
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 );
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 );
113 static NOINLINE void x264_mb_mc_01xywh( x264_t *h, int x, int y, int width, int height )
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] );
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;
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 );
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 );
159 void x264_mb_mc_8x8( x264_t *h, int i8 )
164 if( h->sh.i_type == SLICE_TYPE_P )
166 switch( h->mb.i_sub_partition[i8] )
169 x264_mb_mc_0xywh( h, x, y, 2, 2 );
172 x264_mb_mc_0xywh( h, x, y+0, 2, 1 );
173 x264_mb_mc_0xywh( h, x, y+1, 2, 1 );
176 x264_mb_mc_0xywh( h, x+0, y, 1, 2 );
177 x264_mb_mc_0xywh( h, x+1, y, 1, 2 );
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 );
189 int scan8 = x264_scan8[0] + x + 8*y;
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 );
195 x264_mb_mc_0xywh( h, x, y, 2, 2 );
197 x264_mb_mc_1xywh( h, x, y, 2, 2 );
201 void x264_mb_mc( x264_t *h )
203 if( h->mb.i_partition == D_8x8 )
205 for( int i = 0; i < 4; i++ )
206 x264_mb_mc_8x8( h, i );
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]];
215 if( h->mb.i_partition == D_16x16 )
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 );
222 else if( h->mb.i_partition == D_16x8 )
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 );
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 );
234 else if( h->mb.i_partition == D_8x16 )
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 );
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 );
249 int x264_macroblock_cache_allocate( x264_t *h )
251 int i_mb_count = h->mb.i_mb_count;
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;
257 h->mb.b_interlaced = PARAM_INTERLACED;
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) );
265 /* 0 -> 3 top(4), 4 -> 6 : left(3) */
266 CHECKED_MALLOC( h->mb.intra4x4_pred_mode, i_mb_count * 8 * sizeof(int8_t) );
269 CHECKED_MALLOC( h->mb.non_zero_count, i_mb_count * 48 * sizeof(uint8_t) );
271 if( h->param.b_cabac )
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 ) );
280 for( int i = 0; i < 2; i++ )
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
286 for( int j = !i; j < i_refs; j++ )
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;
294 if( h->param.analyse.i_weighted_pred )
296 int i_padv = PADV << PARAM_INTERLACED;
297 int luma_plane_size = 0;
300 if( h->param.analyse.i_weighted_pred == X264_WEIGHTP_FAKE )
302 // only need buffer for lookahead
303 if( !h->param.i_sync_lookahead || h == h->thread[h->param.i_threads] )
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
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);
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);
323 //simple only has one weighted ref
327 for( int i = 0; i < numweightbuf; i++ )
328 CHECKED_MALLOC( h->mb.p_weight_buf[i], luma_plane_size * sizeof(pixel) );
335 void x264_macroblock_cache_free( x264_t *h )
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] );
344 if( h->param.b_cabac )
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] );
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 );
359 int x264_macroblock_thread_allocate( x264_t *h, int b_lookahead )
363 for( int i = 0; i < (PARAM_INTERLACED ? 5 : 2); i++ )
364 for( int j = 0; j < (CHROMA444 ? 3 : 2); j++ )
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;
369 for( int i = 0; i <= PARAM_INTERLACED; i++ )
371 if( h->param.b_sliced_threads )
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 );
378 h->deblock_strength[i] = h->thread[0]->deblock_strength[0];
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];
386 /* Allocate scratch buffer */
387 int scratch_size = 0;
390 int buf_hpel = (h->thread[0]->fdec->i_width[0]+48+32) * 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 );
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 );
400 CHECKED_MALLOC( h->scratch_buffer, scratch_size );
402 h->scratch_buffer = NULL;
404 int buf_lookahead_threads = (h->mb.i_mb_height + (4 + 32) * h->param.i_lookahead_threads) * sizeof(int) * 2;
405 CHECKED_MALLOC( h->scratch_buffer2, buf_lookahead_threads );
412 void x264_macroblock_thread_free( x264_t *h, int b_lookahead )
416 for( int i = 0; i <= PARAM_INTERLACED; i++ )
417 if( !h->param.b_sliced_threads || (h == h->thread[0] && !i) )
418 x264_free( h->deblock_strength[i] );
419 for( int i = 0; i < (PARAM_INTERLACED ? 5 : 2); i++ )
420 for( int j = 0; j < (CHROMA444 ? 3 : 2); j++ )
421 x264_free( h->intra_border_backup[i][j] - 16 );
423 x264_free( h->scratch_buffer );
424 x264_free( h->scratch_buffer2 );
427 void x264_macroblock_slice_init( x264_t *h )
429 h->mb.mv[0] = h->fdec->mv[0];
430 h->mb.mv[1] = h->fdec->mv[1];
431 h->mb.mvr[0][0] = h->fdec->mv16x16;
432 h->mb.ref[0] = h->fdec->ref[0];
433 h->mb.ref[1] = h->fdec->ref[1];
434 h->mb.type = h->fdec->mb_type;
435 h->mb.partition = h->fdec->mb_partition;
436 h->mb.field = h->fdec->field;
438 h->fdec->i_ref[0] = h->i_ref[0];
439 h->fdec->i_ref[1] = h->i_ref[1];
440 for( int i = 0; i < h->i_ref[0]; i++ )
441 h->fdec->ref_poc[0][i] = h->fref[0][i]->i_poc;
442 if( h->sh.i_type == SLICE_TYPE_B )
444 for( int i = 0; i < h->i_ref[1]; i++ )
445 h->fdec->ref_poc[1][i] = h->fref[1][i]->i_poc;
447 map_col_to_list0(-1) = -1;
448 map_col_to_list0(-2) = -2;
449 for( int i = 0; i < h->fref[1][0]->i_ref[0]; i++ )
451 int poc = h->fref[1][0]->ref_poc[0][i];
452 map_col_to_list0(i) = -2;
453 for( int j = 0; j < h->i_ref[0]; j++ )
454 if( h->fref[0][j]->i_poc == poc )
456 map_col_to_list0(i) = j;
461 else if( h->sh.i_type == SLICE_TYPE_P )
463 if( h->sh.i_disable_deblocking_filter_idc != 1 && h->param.analyse.i_weighted_pred == X264_WEIGHTP_SMART )
465 deblock_ref_table(-2) = -2;
466 deblock_ref_table(-1) = -1;
467 for( int i = 0; i < h->i_ref[0] << SLICE_MBAFF; i++ )
469 /* Mask off high bits to avoid frame num collisions with -1/-2.
470 * In current x264 frame num values don't cover a range of more
471 * than 32, so 6 bits is enough for uniqueness. */
473 deblock_ref_table(i) = h->fref[0][i]->i_frame_num&63;
475 deblock_ref_table(i) = ((h->fref[0][i>>1]->i_frame_num&63)<<1) + (i&1);
480 /* init with not available (for top right idx=7,15) */
481 memset( h->mb.cache.ref, -2, sizeof( h->mb.cache.ref ) );
483 if( h->i_ref[0] > 0 )
484 for( int field = 0; field <= SLICE_MBAFF; field++ )
486 int curpoc = h->fdec->i_poc + h->fdec->i_delta_poc[field];
487 int refpoc = h->fref[0][0]->i_poc + h->fref[0][0]->i_delta_poc[field];
488 int delta = curpoc - refpoc;
490 h->fdec->inv_ref_poc[field] = (256 + delta/2) / delta;
493 h->mb.i_neighbour4[6] =
494 h->mb.i_neighbour4[9] =
495 h->mb.i_neighbour4[12] =
496 h->mb.i_neighbour4[14] = MB_LEFT|MB_TOP|MB_TOPLEFT|MB_TOPRIGHT;
497 h->mb.i_neighbour4[3] =
498 h->mb.i_neighbour4[7] =
499 h->mb.i_neighbour4[11] =
500 h->mb.i_neighbour4[13] =
501 h->mb.i_neighbour4[15] =
502 h->mb.i_neighbour8[3] = MB_LEFT|MB_TOP|MB_TOPLEFT;
505 void x264_macroblock_thread_init( x264_t *h )
507 h->mb.i_me_method = h->param.analyse.i_me_method;
508 h->mb.i_subpel_refine = h->param.analyse.i_subpel_refine;
509 if( h->sh.i_type == SLICE_TYPE_B && (h->mb.i_subpel_refine == 6 || h->mb.i_subpel_refine == 8) )
510 h->mb.i_subpel_refine--;
511 h->mb.b_chroma_me = h->param.analyse.b_chroma_me &&
512 ((h->sh.i_type == SLICE_TYPE_P && h->mb.i_subpel_refine >= 5) ||
513 (h->sh.i_type == SLICE_TYPE_B && h->mb.i_subpel_refine >= 9));
514 h->mb.b_dct_decimate = h->sh.i_type == SLICE_TYPE_B ||
515 (h->param.analyse.b_dct_decimate && h->sh.i_type != SLICE_TYPE_I);
516 h->mb.i_mb_prev_xy = -1;
519 * fdec fenc fdec fenc fdec fenc
520 * 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
521 * 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
522 * 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
523 * 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
524 * 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
525 * 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
526 * 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
527 * 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
528 * u U U v V V u U U U U V V V V
529 * u U U v V V u U U U U V V V V
530 * v v v v v v v V V V V
536 h->mb.pic.p_fenc[0] = h->mb.pic.fenc_buf;
537 h->mb.pic.p_fdec[0] = h->mb.pic.fdec_buf + 2*FDEC_STRIDE;
538 h->mb.pic.p_fenc[1] = h->mb.pic.fenc_buf + 16*FENC_STRIDE;
539 h->mb.pic.p_fdec[1] = h->mb.pic.fdec_buf + 19*FDEC_STRIDE;
542 h->mb.pic.p_fenc[2] = h->mb.pic.fenc_buf + 32*FENC_STRIDE;
543 h->mb.pic.p_fdec[2] = h->mb.pic.fdec_buf + 36*FDEC_STRIDE;
547 h->mb.pic.p_fenc[2] = h->mb.pic.fenc_buf + 16*FENC_STRIDE + 8;
548 h->mb.pic.p_fdec[2] = h->mb.pic.fdec_buf + 19*FDEC_STRIDE + 16;
552 void x264_prefetch_fenc( x264_t *h, x264_frame_t *fenc, int i_mb_x, int i_mb_y )
554 int stride_y = fenc->i_stride[0];
555 int stride_uv = fenc->i_stride[1];
556 int off_y = 16 * i_mb_x + 16 * i_mb_y * stride_y;
557 int off_uv = 16 * i_mb_x + (16 * i_mb_y * stride_uv >> CHROMA_V_SHIFT);
558 h->mc.prefetch_fenc( fenc->plane[0]+off_y, stride_y,
559 fenc->plane[1]+off_uv, stride_uv, i_mb_x );
562 NOINLINE void x264_copy_column8( pixel *dst, pixel *src )
564 // input pointers are offset by 4 rows because that's faster (smaller instruction size on x86)
565 for( int i = -4; i < 4; i++ )
566 dst[i*FDEC_STRIDE] = src[i*FDEC_STRIDE];
569 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 )
571 int mb_interlaced = b_mbaff && MB_INTERLACED;
572 int height = b_chroma ? 16 >> CHROMA_V_SHIFT : 16;
573 int i_stride = h->fdec->i_stride[i];
574 int i_stride2 = i_stride << mb_interlaced;
575 int i_pix_offset = mb_interlaced
576 ? 16 * mb_x + height * (mb_y&~1) * i_stride + (mb_y&1) * i_stride
577 : 16 * mb_x + height * mb_y * i_stride;
578 pixel *plane_fdec = &h->fdec->plane[i][i_pix_offset];
579 int fdec_idx = b_mbaff ? (mb_interlaced ? (3 + (mb_y&1)) : (mb_y&1) ? 2 : 4) : !(mb_y&1);
580 pixel *intra_fdec = &h->intra_border_backup[fdec_idx][i][mb_x*16];
581 int ref_pix_offset[2] = { i_pix_offset, i_pix_offset };
582 /* ref_pix_offset[0] references the current field and [1] the opposite field. */
584 ref_pix_offset[1] += (1-2*(mb_y&1)) * i_stride;
585 h->mb.pic.i_stride[i] = i_stride2;
586 h->mb.pic.p_fenc_plane[i] = &h->fenc->plane[i][i_pix_offset];
589 h->mc.load_deinterleave_chroma_fenc( h->mb.pic.p_fenc[1], h->mb.pic.p_fenc_plane[1], i_stride2, height );
590 memcpy( h->mb.pic.p_fdec[1]-FDEC_STRIDE, intra_fdec, 8*sizeof(pixel) );
591 memcpy( h->mb.pic.p_fdec[2]-FDEC_STRIDE, intra_fdec+8, 8*sizeof(pixel) );
592 h->mb.pic.p_fdec[1][-FDEC_STRIDE-1] = intra_fdec[-1-8];
593 h->mb.pic.p_fdec[2][-FDEC_STRIDE-1] = intra_fdec[-1];
597 h->mc.copy[PIXEL_16x16]( h->mb.pic.p_fenc[i], FENC_STRIDE, h->mb.pic.p_fenc_plane[i], i_stride2, 16 );
598 memcpy( h->mb.pic.p_fdec[i]-FDEC_STRIDE, intra_fdec, 24*sizeof(pixel) );
599 h->mb.pic.p_fdec[i][-FDEC_STRIDE-1] = intra_fdec[-1];
601 if( b_mbaff || h->mb.b_reencode_mb )
603 for( int j = 0; j < height; j++ )
606 h->mb.pic.p_fdec[1][-1+j*FDEC_STRIDE] = plane_fdec[-2+j*i_stride2];
607 h->mb.pic.p_fdec[2][-1+j*FDEC_STRIDE] = plane_fdec[-1+j*i_stride2];
610 h->mb.pic.p_fdec[i][-1+j*FDEC_STRIDE] = plane_fdec[-1+j*i_stride2];
612 pixel *plane_src, **filtered_src;
613 for( int j = 0; j < h->mb.pic.i_fref[0]; j++ )
615 // Interpolate between pixels in same field.
618 plane_src = h->fref[0][j>>1]->plane_fld[i];
619 filtered_src = h->fref[0][j>>1]->filtered_fld[i];
623 plane_src = h->fref[0][j]->plane[i];
624 filtered_src = h->fref[0][j]->filtered[i];
626 h->mb.pic.p_fref[0][j][i*4] = plane_src + ref_pix_offset[j&1];
630 for( int k = 1; k < 4; k++ )
631 h->mb.pic.p_fref[0][j][i*4+k] = filtered_src[k] + ref_pix_offset[j&1];
634 if( h->sh.weight[j][0].weightfn )
635 h->mb.pic.p_fref_w[j] = &h->fenc->weighted[j >> mb_interlaced][ref_pix_offset[j&1]];
637 h->mb.pic.p_fref_w[j] = h->mb.pic.p_fref[0][j][0];
641 if( h->sh.i_type == SLICE_TYPE_B )
642 for( int j = 0; j < h->mb.pic.i_fref[1]; j++ )
646 plane_src = h->fref[1][j>>1]->plane_fld[i];
647 filtered_src = h->fref[1][j>>1]->filtered_fld[i];
651 plane_src = h->fref[1][j]->plane[i];
652 filtered_src = h->fref[1][j]->filtered[i];
654 h->mb.pic.p_fref[1][j][i*4] = plane_src + ref_pix_offset[j&1];
657 for( int k = 1; k < 4; k++ )
658 h->mb.pic.p_fref[1][j][i*4+k] = filtered_src[k] + ref_pix_offset[j&1];
662 static const x264_left_table_t left_indices[4] =
664 /* Current is progressive */
665 {{ 4, 4, 5, 5}, { 3, 3, 7, 7}, {16+1, 16+1, 32+1, 32+1}, {0, 0, 1, 1}, {0, 0, 0, 0}},
666 {{ 6, 6, 3, 3}, {11, 11, 15, 15}, {16+5, 16+5, 32+5, 32+5}, {2, 2, 3, 3}, {1, 1, 1, 1}},
667 /* Current is interlaced */
668 {{ 4, 6, 4, 6}, { 3, 11, 3, 11}, {16+1, 16+1, 32+1, 32+1}, {0, 2, 0, 2}, {0, 1, 0, 1}},
670 {{ 4, 5, 6, 3}, { 3, 7, 11, 15}, {16+1, 16+5, 32+1, 32+5}, {0, 1, 2, 3}, {0, 0, 1, 1}}
673 static void ALWAYS_INLINE x264_macroblock_cache_load_neighbours( x264_t *h, int mb_x, int mb_y, int b_interlaced )
675 const int mb_interlaced = b_interlaced && MB_INTERLACED;
676 int top_y = mb_y - (1 << mb_interlaced);
677 int top = top_y * h->mb.i_mb_stride + mb_x;
681 h->mb.i_mb_xy = mb_y * h->mb.i_mb_stride + mb_x;
682 h->mb.i_b8_xy = 2*(mb_y * h->mb.i_b8_stride + mb_x);
683 h->mb.i_b4_xy = 4*(mb_y * h->mb.i_b4_stride + mb_x);
685 h->mb.left_b8[1] = -1;
687 h->mb.left_b4[1] = -1;
688 h->mb.i_neighbour = 0;
689 h->mb.i_neighbour_intra = 0;
690 h->mb.i_neighbour_frame = 0;
691 h->mb.i_mb_top_xy = -1;
692 h->mb.i_mb_top_y = -1;
693 h->mb.i_mb_left_xy[0] = h->mb.i_mb_left_xy[1] = -1;
694 h->mb.i_mb_topleft_xy = -1;
695 h->mb.i_mb_topright_xy = -1;
696 h->mb.i_mb_type_top = -1;
697 h->mb.i_mb_type_left[0] = h->mb.i_mb_type_left[1] = -1;
698 h->mb.i_mb_type_topleft = -1;
699 h->mb.i_mb_type_topright = -1;
700 h->mb.left_index_table = &left_indices[3];
701 h->mb.topleft_partition = 0;
703 int topleft_y = top_y;
704 int topright_y = top_y;
707 left[0] = left[1] = h->mb.i_mb_xy - 1;
708 h->mb.left_b8[0] = h->mb.left_b8[1] = h->mb.i_b8_xy - 2;
709 h->mb.left_b4[0] = h->mb.left_b4[1] = h->mb.i_b4_xy - 4;
713 h->mb.i_mb_top_mbpair_xy = h->mb.i_mb_xy - 2*h->mb.i_mb_stride;
714 h->mb.i_mb_topleft_y = -1;
715 h->mb.i_mb_topright_y = -1;
719 if( mb_x && mb_interlaced != h->mb.field[h->mb.i_mb_xy-1] )
721 left[0] = left[1] = h->mb.i_mb_xy - 1 - h->mb.i_mb_stride;
722 h->mb.left_b8[0] = h->mb.left_b8[1] = h->mb.i_b8_xy - 2 - 2*h->mb.i_b8_stride;
723 h->mb.left_b4[0] = h->mb.left_b4[1] = h->mb.i_b4_xy - 4 - 4*h->mb.i_b4_stride;
727 h->mb.left_index_table = &left_indices[2];
728 left[1] += h->mb.i_mb_stride;
729 h->mb.left_b8[1] += 2*h->mb.i_b8_stride;
730 h->mb.left_b4[1] += 4*h->mb.i_b4_stride;
734 h->mb.left_index_table = &left_indices[1];
736 h->mb.topleft_partition = 1;
744 if( mb_interlaced && top >= 0 )
746 if( !h->mb.field[top] )
748 top += h->mb.i_mb_stride;
752 topleft_y += !h->mb.field[h->mb.i_mb_stride*topleft_y + mb_x - 1];
753 if( mb_x < h->mb.i_mb_width-1 )
754 topright_y += !h->mb.field[h->mb.i_mb_stride*topright_y + mb_x + 1];
756 if( mb_x && mb_interlaced != h->mb.field[h->mb.i_mb_xy-1] )
760 h->mb.left_index_table = &left_indices[2];
761 left[1] += h->mb.i_mb_stride;
762 h->mb.left_b8[1] += 2*h->mb.i_b8_stride;
763 h->mb.left_b4[1] += 4*h->mb.i_b4_stride;
766 h->mb.left_index_table = &left_indices[0];
773 h->mb.i_neighbour_frame |= MB_LEFT;
774 h->mb.i_mb_left_xy[0] = left[0];
775 h->mb.i_mb_left_xy[1] = left[1];
776 h->mb.i_mb_type_left[0] = h->mb.type[h->mb.i_mb_left_xy[0]];
777 h->mb.i_mb_type_left[1] = h->mb.type[h->mb.i_mb_left_xy[1]];
778 if( h->mb.slice_table[left[0]] == h->sh.i_first_mb )
780 h->mb.i_neighbour |= MB_LEFT;
782 // FIXME: We don't currently support constrained intra + mbaff.
783 if( !h->param.b_constrained_intra || IS_INTRA( h->mb.i_mb_type_left[0] ) )
784 h->mb.i_neighbour_intra |= MB_LEFT;
788 /* We can't predict from the previous threadslice since it hasn't been encoded yet. */
789 if( (h->i_threadslice_start >> mb_interlaced) != (mb_y >> mb_interlaced) )
793 h->mb.i_neighbour_frame |= MB_TOP;
794 h->mb.i_mb_top_xy = top;
795 h->mb.i_mb_top_y = top_y;
796 h->mb.i_mb_type_top = h->mb.type[h->mb.i_mb_top_xy];
797 if( h->mb.slice_table[top] == h->sh.i_first_mb )
799 h->mb.i_neighbour |= MB_TOP;
801 if( !h->param.b_constrained_intra || IS_INTRA( h->mb.i_mb_type_top ) )
802 h->mb.i_neighbour_intra |= MB_TOP;
804 /* We only need to prefetch the top blocks because the left was just written
805 * to as part of the previous cache_save. Since most target CPUs use write-allocate
806 * caches, left blocks are near-guaranteed to be in L1 cache. Top--not so much. */
807 x264_prefetch( &h->mb.cbp[top] );
808 x264_prefetch( h->mb.intra4x4_pred_mode[top] );
809 x264_prefetch( &h->mb.non_zero_count[top][12] );
810 /* These aren't always allocated, but prefetching an invalid address can't hurt. */
811 x264_prefetch( &h->mb.mb_transform_size[top] );
812 x264_prefetch( &h->mb.skipbp[top] );
816 if( mb_x > 0 && topleft_y >= 0 )
818 h->mb.i_neighbour_frame |= MB_TOPLEFT;
819 h->mb.i_mb_topleft_xy = h->mb.i_mb_stride*topleft_y + mb_x - 1;
820 h->mb.i_mb_topleft_y = topleft_y;
821 h->mb.i_mb_type_topleft = h->mb.type[h->mb.i_mb_topleft_xy];
822 if( h->mb.slice_table[h->mb.i_mb_topleft_xy] == h->sh.i_first_mb )
824 h->mb.i_neighbour |= MB_TOPLEFT;
826 if( !h->param.b_constrained_intra || IS_INTRA( h->mb.i_mb_type_topleft ) )
827 h->mb.i_neighbour_intra |= MB_TOPLEFT;
831 if( mb_x < h->mb.i_mb_width - 1 && topright_y >= 0 )
833 h->mb.i_neighbour_frame |= MB_TOPRIGHT;
834 h->mb.i_mb_topright_xy = h->mb.i_mb_stride*topright_y + mb_x + 1;
835 h->mb.i_mb_topright_y = topright_y;
836 h->mb.i_mb_type_topright = h->mb.type[h->mb.i_mb_topright_xy];
837 if( h->mb.slice_table[h->mb.i_mb_topright_xy] == h->sh.i_first_mb )
839 h->mb.i_neighbour |= MB_TOPRIGHT;
841 if( !h->param.b_constrained_intra || IS_INTRA( h->mb.i_mb_type_topright ) )
842 h->mb.i_neighbour_intra |= MB_TOPRIGHT;
855 static void ALWAYS_INLINE x264_macroblock_cache_load( x264_t *h, int mb_x, int mb_y, int b_mbaff )
857 x264_macroblock_cache_load_neighbours( h, mb_x, mb_y, b_mbaff );
859 int *left = h->mb.i_mb_left_xy;
860 int top = h->mb.i_mb_top_xy;
861 int top_y = h->mb.i_mb_top_y;
862 int s8x8 = h->mb.i_b8_stride;
863 int s4x4 = h->mb.i_b4_stride;
864 int top_8x8 = (2*top_y+1) * s8x8 + 2*mb_x;
865 int top_4x4 = (4*top_y+3) * s4x4 + 4*mb_x;
866 int lists = (1 << h->sh.i_type) & 3;
868 /* GCC pessimizes direct loads from heap-allocated arrays due to aliasing. */
869 /* By only dereferencing them once, we avoid this issue. */
870 int8_t (*i4x4)[8] = h->mb.intra4x4_pred_mode;
871 uint8_t (*nnz)[48] = h->mb.non_zero_count;
872 int16_t *cbp = h->mb.cbp;
874 const x264_left_table_t *left_index_table = h->mb.left_index_table;
876 h->mb.cache.deblock_strength = h->deblock_strength[mb_y&1][h->param.b_sliced_threads?h->mb.i_mb_xy:mb_x];
879 if( h->mb.i_neighbour & MB_TOP )
881 h->mb.cache.i_cbp_top = cbp[top];
883 CP32( &h->mb.cache.intra4x4_pred_mode[x264_scan8[0] - 8], &i4x4[top][0] );
885 /* load non_zero_count */
886 CP32( &h->mb.cache.non_zero_count[x264_scan8[ 0] - 8], &nnz[top][12] );
887 CP32( &h->mb.cache.non_zero_count[x264_scan8[16] - 8], &nnz[top][16-4 + (16>>CHROMA_V_SHIFT)] );
888 CP32( &h->mb.cache.non_zero_count[x264_scan8[32] - 8], &nnz[top][32-4 + (16>>CHROMA_V_SHIFT)] );
890 /* Finish the prefetching */
891 for( int l = 0; l < lists; l++ )
893 x264_prefetch( &h->mb.mv[l][top_4x4-1] );
894 /* Top right being not in the same cacheline as top left will happen
895 * once every 4 MBs, so one extra prefetch is worthwhile */
896 x264_prefetch( &h->mb.mv[l][top_4x4+4] );
897 x264_prefetch( &h->mb.ref[l][top_8x8-1] );
898 x264_prefetch( &h->mb.mvd[l][top] );
903 h->mb.cache.i_cbp_top = -1;
906 M32( &h->mb.cache.intra4x4_pred_mode[x264_scan8[0] - 8] ) = 0xFFFFFFFFU;
908 /* load non_zero_count */
909 M32( &h->mb.cache.non_zero_count[x264_scan8[ 0] - 8] ) = 0x80808080U;
910 M32( &h->mb.cache.non_zero_count[x264_scan8[16] - 8] ) = 0x80808080U;
911 M32( &h->mb.cache.non_zero_count[x264_scan8[32] - 8] ) = 0x80808080U;
914 if( h->mb.i_neighbour & MB_LEFT )
916 int ltop = left[LTOP];
917 int lbot = b_mbaff ? left[LBOT] : ltop;
920 const int16_t top_luma = (cbp[ltop] >> (left_index_table->mv[0]&(~1))) & 2;
921 const int16_t bot_luma = (cbp[lbot] >> (left_index_table->mv[2]&(~1))) & 2;
922 h->mb.cache.i_cbp_left = (cbp[ltop] & 0xfff0) | (bot_luma<<2) | top_luma;
925 h->mb.cache.i_cbp_left = cbp[ltop];
928 h->mb.cache.intra4x4_pred_mode[x264_scan8[ 0] - 1] = i4x4[ltop][left_index_table->intra[0]];
929 h->mb.cache.intra4x4_pred_mode[x264_scan8[ 2] - 1] = i4x4[ltop][left_index_table->intra[1]];
930 h->mb.cache.intra4x4_pred_mode[x264_scan8[ 8] - 1] = i4x4[lbot][left_index_table->intra[2]];
931 h->mb.cache.intra4x4_pred_mode[x264_scan8[10] - 1] = i4x4[lbot][left_index_table->intra[3]];
933 /* load non_zero_count */
934 h->mb.cache.non_zero_count[x264_scan8[ 0] - 1] = nnz[ltop][left_index_table->nnz[0]];
935 h->mb.cache.non_zero_count[x264_scan8[ 2] - 1] = nnz[ltop][left_index_table->nnz[1]];
936 h->mb.cache.non_zero_count[x264_scan8[ 8] - 1] = nnz[lbot][left_index_table->nnz[2]];
937 h->mb.cache.non_zero_count[x264_scan8[10] - 1] = nnz[lbot][left_index_table->nnz[3]];
939 if( CHROMA_FORMAT >= CHROMA_422 )
941 int offset = (4>>CHROMA_H_SHIFT) - 4;
942 h->mb.cache.non_zero_count[x264_scan8[16+ 0] - 1] = nnz[ltop][left_index_table->nnz[0]+16+offset];
943 h->mb.cache.non_zero_count[x264_scan8[16+ 2] - 1] = nnz[ltop][left_index_table->nnz[1]+16+offset];
944 h->mb.cache.non_zero_count[x264_scan8[16+ 8] - 1] = nnz[lbot][left_index_table->nnz[2]+16+offset];
945 h->mb.cache.non_zero_count[x264_scan8[16+10] - 1] = nnz[lbot][left_index_table->nnz[3]+16+offset];
946 h->mb.cache.non_zero_count[x264_scan8[32+ 0] - 1] = nnz[ltop][left_index_table->nnz[0]+32+offset];
947 h->mb.cache.non_zero_count[x264_scan8[32+ 2] - 1] = nnz[ltop][left_index_table->nnz[1]+32+offset];
948 h->mb.cache.non_zero_count[x264_scan8[32+ 8] - 1] = nnz[lbot][left_index_table->nnz[2]+32+offset];
949 h->mb.cache.non_zero_count[x264_scan8[32+10] - 1] = nnz[lbot][left_index_table->nnz[3]+32+offset];
953 h->mb.cache.non_zero_count[x264_scan8[16+ 0] - 1] = nnz[ltop][left_index_table->nnz_chroma[0]];
954 h->mb.cache.non_zero_count[x264_scan8[16+ 2] - 1] = nnz[lbot][left_index_table->nnz_chroma[1]];
955 h->mb.cache.non_zero_count[x264_scan8[32+ 0] - 1] = nnz[ltop][left_index_table->nnz_chroma[2]];
956 h->mb.cache.non_zero_count[x264_scan8[32+ 2] - 1] = nnz[lbot][left_index_table->nnz_chroma[3]];
961 h->mb.cache.i_cbp_left = -1;
963 h->mb.cache.intra4x4_pred_mode[x264_scan8[ 0] - 1] =
964 h->mb.cache.intra4x4_pred_mode[x264_scan8[ 2] - 1] =
965 h->mb.cache.intra4x4_pred_mode[x264_scan8[ 8] - 1] =
966 h->mb.cache.intra4x4_pred_mode[x264_scan8[10] - 1] = -1;
968 /* load non_zero_count */
969 h->mb.cache.non_zero_count[x264_scan8[ 0] - 1] =
970 h->mb.cache.non_zero_count[x264_scan8[ 2] - 1] =
971 h->mb.cache.non_zero_count[x264_scan8[ 8] - 1] =
972 h->mb.cache.non_zero_count[x264_scan8[10] - 1] =
973 h->mb.cache.non_zero_count[x264_scan8[16+ 0] - 1] =
974 h->mb.cache.non_zero_count[x264_scan8[16+ 2] - 1] =
975 h->mb.cache.non_zero_count[x264_scan8[32+ 0] - 1] =
976 h->mb.cache.non_zero_count[x264_scan8[32+ 2] - 1] = 0x80;
977 if( CHROMA_FORMAT >= CHROMA_422 )
979 h->mb.cache.non_zero_count[x264_scan8[16+ 8] - 1] =
980 h->mb.cache.non_zero_count[x264_scan8[16+10] - 1] =
981 h->mb.cache.non_zero_count[x264_scan8[32+ 8] - 1] =
982 h->mb.cache.non_zero_count[x264_scan8[32+10] - 1] = 0x80;
986 if( h->pps->b_transform_8x8_mode )
988 h->mb.cache.i_neighbour_transform_size =
989 ( (h->mb.i_neighbour & MB_LEFT) && h->mb.mb_transform_size[left[0]] )
990 + ( (h->mb.i_neighbour & MB_TOP) && h->mb.mb_transform_size[top] );
995 h->mb.pic.i_fref[0] = h->i_ref[0] << MB_INTERLACED;
996 h->mb.pic.i_fref[1] = h->i_ref[1] << MB_INTERLACED;
1001 x264_copy_column8( h->mb.pic.p_fdec[0]-1+ 4*FDEC_STRIDE, h->mb.pic.p_fdec[0]+15+ 4*FDEC_STRIDE );
1002 x264_copy_column8( h->mb.pic.p_fdec[0]-1+12*FDEC_STRIDE, h->mb.pic.p_fdec[0]+15+12*FDEC_STRIDE );
1003 x264_macroblock_load_pic_pointers( h, mb_x, mb_y, 0, 0, 0 );
1006 x264_copy_column8( h->mb.pic.p_fdec[1]-1+ 4*FDEC_STRIDE, h->mb.pic.p_fdec[1]+15+ 4*FDEC_STRIDE );
1007 x264_copy_column8( h->mb.pic.p_fdec[1]-1+12*FDEC_STRIDE, h->mb.pic.p_fdec[1]+15+12*FDEC_STRIDE );
1008 x264_copy_column8( h->mb.pic.p_fdec[2]-1+ 4*FDEC_STRIDE, h->mb.pic.p_fdec[2]+15+ 4*FDEC_STRIDE );
1009 x264_copy_column8( h->mb.pic.p_fdec[2]-1+12*FDEC_STRIDE, h->mb.pic.p_fdec[2]+15+12*FDEC_STRIDE );
1010 x264_macroblock_load_pic_pointers( h, mb_x, mb_y, 1, 0, 0 );
1011 x264_macroblock_load_pic_pointers( h, mb_x, mb_y, 2, 0, 0 );
1015 x264_copy_column8( h->mb.pic.p_fdec[1]-1+ 4*FDEC_STRIDE, h->mb.pic.p_fdec[1]+ 7+ 4*FDEC_STRIDE );
1016 x264_copy_column8( h->mb.pic.p_fdec[2]-1+ 4*FDEC_STRIDE, h->mb.pic.p_fdec[2]+ 7+ 4*FDEC_STRIDE );
1017 if( CHROMA_FORMAT == CHROMA_422 )
1019 x264_copy_column8( h->mb.pic.p_fdec[1]-1+12*FDEC_STRIDE, h->mb.pic.p_fdec[1]+ 7+12*FDEC_STRIDE );
1020 x264_copy_column8( h->mb.pic.p_fdec[2]-1+12*FDEC_STRIDE, h->mb.pic.p_fdec[2]+ 7+12*FDEC_STRIDE );
1022 x264_macroblock_load_pic_pointers( h, mb_x, mb_y, 1, 1, 0 );
1027 x264_macroblock_load_pic_pointers( h, mb_x, mb_y, 0, 0, 1 );
1030 x264_macroblock_load_pic_pointers( h, mb_x, mb_y, 1, 0, 1 );
1031 x264_macroblock_load_pic_pointers( h, mb_x, mb_y, 2, 0, 1 );
1034 x264_macroblock_load_pic_pointers( h, mb_x, mb_y, 1, 1, 1 );
1037 if( h->fdec->integral )
1039 int offset = 16 * (mb_x + mb_y * h->fdec->i_stride[0]);
1040 for( int list = 0; list < 2; list++ )
1041 for( int i = 0; i < h->mb.pic.i_fref[list]; i++ )
1042 h->mb.pic.p_integral[list][i] = &h->fref[list][i]->integral[offset];
1045 x264_prefetch_fenc( h, h->fenc, mb_x, mb_y );
1047 /* load ref/mv/mvd */
1048 for( int l = 0; l < lists; l++ )
1050 int16_t (*mv)[2] = h->mb.mv[l];
1051 int8_t *ref = h->mb.ref[l];
1053 int i8 = x264_scan8[0] - 1 - 1*8;
1054 if( h->mb.i_neighbour & MB_TOPLEFT )
1056 int ir = b_mbaff ? 2*(s8x8*h->mb.i_mb_topleft_y + mb_x-1)+1+s8x8 : top_8x8 - 1;
1057 int iv = b_mbaff ? 4*(s4x4*h->mb.i_mb_topleft_y + mb_x-1)+3+3*s4x4 : top_4x4 - 1;
1058 if( b_mbaff && h->mb.topleft_partition )
1060 /* Take motion vector from the middle of macroblock instead of
1061 * the bottom right as usual. */
1065 h->mb.cache.ref[l][i8] = ref[ir];
1066 CP32( h->mb.cache.mv[l][i8], mv[iv] );
1070 h->mb.cache.ref[l][i8] = -2;
1071 M32( h->mb.cache.mv[l][i8] ) = 0;
1074 i8 = x264_scan8[0] - 8;
1075 if( h->mb.i_neighbour & MB_TOP )
1077 h->mb.cache.ref[l][i8+0] =
1078 h->mb.cache.ref[l][i8+1] = ref[top_8x8 + 0];
1079 h->mb.cache.ref[l][i8+2] =
1080 h->mb.cache.ref[l][i8+3] = ref[top_8x8 + 1];
1081 CP128( h->mb.cache.mv[l][i8], mv[top_4x4] );
1085 M128( h->mb.cache.mv[l][i8] ) = M128_ZERO;
1086 M32( &h->mb.cache.ref[l][i8] ) = (uint8_t)(-2) * 0x01010101U;
1089 i8 = x264_scan8[0] + 4 - 1*8;
1090 if( h->mb.i_neighbour & MB_TOPRIGHT )
1092 int ir = b_mbaff ? 2*(s8x8*h->mb.i_mb_topright_y + (mb_x+1))+s8x8 : top_8x8 + 2;
1093 int iv = b_mbaff ? 4*(s4x4*h->mb.i_mb_topright_y + (mb_x+1))+3*s4x4 : top_4x4 + 4;
1094 h->mb.cache.ref[l][i8] = ref[ir];
1095 CP32( h->mb.cache.mv[l][i8], mv[iv] );
1098 h->mb.cache.ref[l][i8] = -2;
1100 i8 = x264_scan8[0] - 1;
1101 if( h->mb.i_neighbour & MB_LEFT )
1105 h->mb.cache.ref[l][i8+0*8] = ref[h->mb.left_b8[LTOP] + 1 + s8x8*left_index_table->ref[0]];
1106 h->mb.cache.ref[l][i8+1*8] = ref[h->mb.left_b8[LTOP] + 1 + s8x8*left_index_table->ref[1]];
1107 h->mb.cache.ref[l][i8+2*8] = ref[h->mb.left_b8[LBOT] + 1 + s8x8*left_index_table->ref[2]];
1108 h->mb.cache.ref[l][i8+3*8] = ref[h->mb.left_b8[LBOT] + 1 + s8x8*left_index_table->ref[3]];
1110 CP32( h->mb.cache.mv[l][i8+0*8], mv[h->mb.left_b4[LTOP] + 3 + s4x4*left_index_table->mv[0]] );
1111 CP32( h->mb.cache.mv[l][i8+1*8], mv[h->mb.left_b4[LTOP] + 3 + s4x4*left_index_table->mv[1]] );
1112 CP32( h->mb.cache.mv[l][i8+2*8], mv[h->mb.left_b4[LBOT] + 3 + s4x4*left_index_table->mv[2]] );
1113 CP32( h->mb.cache.mv[l][i8+3*8], mv[h->mb.left_b4[LBOT] + 3 + s4x4*left_index_table->mv[3]] );
1117 const int ir = h->mb.i_b8_xy - 1;
1118 const int iv = h->mb.i_b4_xy - 1;
1119 h->mb.cache.ref[l][i8+0*8] =
1120 h->mb.cache.ref[l][i8+1*8] = ref[ir + 0*s8x8];
1121 h->mb.cache.ref[l][i8+2*8] =
1122 h->mb.cache.ref[l][i8+3*8] = ref[ir + 1*s8x8];
1124 CP32( h->mb.cache.mv[l][i8+0*8], mv[iv + 0*s4x4] );
1125 CP32( h->mb.cache.mv[l][i8+1*8], mv[iv + 1*s4x4] );
1126 CP32( h->mb.cache.mv[l][i8+2*8], mv[iv + 2*s4x4] );
1127 CP32( h->mb.cache.mv[l][i8+3*8], mv[iv + 3*s4x4] );
1132 for( int i = 0; i < 4; i++ )
1134 h->mb.cache.ref[l][i8+i*8] = -2;
1135 M32( h->mb.cache.mv[l][i8+i*8] ) = 0;
1139 /* Extra logic for top right mv in mbaff.
1145 * If the top right of the 4x4 partitions labeled a, b and c in the
1146 * above diagram do not exist, but the entries d, e and f exist (in
1147 * the macroblock to the left) then use those instead.
1149 if( b_mbaff && (h->mb.i_neighbour & MB_LEFT) )
1151 if( MB_INTERLACED && !h->mb.field[h->mb.i_mb_xy-1] )
1153 h->mb.cache.topright_ref[l][0] = ref[h->mb.left_b8[0] + 1 + s8x8*0];
1154 h->mb.cache.topright_ref[l][1] = ref[h->mb.left_b8[0] + 1 + s8x8*1];
1155 h->mb.cache.topright_ref[l][2] = ref[h->mb.left_b8[1] + 1 + s8x8*0];
1156 CP32( h->mb.cache.topright_mv[l][0], mv[h->mb.left_b4[0] + 3 + s4x4*(left_index_table->mv[0]+1)] );
1157 CP32( h->mb.cache.topright_mv[l][1], mv[h->mb.left_b4[0] + 3 + s4x4*(left_index_table->mv[1]+1)] );
1158 CP32( h->mb.cache.topright_mv[l][2], mv[h->mb.left_b4[1] + 3 + s4x4*(left_index_table->mv[2]+1)] );
1160 else if( !MB_INTERLACED && h->mb.field[h->mb.i_mb_xy-1] )
1162 // Looking at the bottom field so always take the bottom macroblock of the pair.
1163 h->mb.cache.topright_ref[l][0] = ref[h->mb.left_b8[0] + 1 + s8x8*2 + s8x8*left_index_table->ref[0]];
1164 h->mb.cache.topright_ref[l][1] = ref[h->mb.left_b8[0] + 1 + s8x8*2 + s8x8*left_index_table->ref[0]];
1165 h->mb.cache.topright_ref[l][2] = ref[h->mb.left_b8[0] + 1 + s8x8*2 + s8x8*left_index_table->ref[2]];
1166 CP32( h->mb.cache.topright_mv[l][0], mv[h->mb.left_b4[0] + 3 + s4x4*4 + s4x4*left_index_table->mv[0]] );
1167 CP32( h->mb.cache.topright_mv[l][1], mv[h->mb.left_b4[0] + 3 + s4x4*4 + s4x4*left_index_table->mv[1]] );
1168 CP32( h->mb.cache.topright_mv[l][2], mv[h->mb.left_b4[0] + 3 + s4x4*4 + s4x4*left_index_table->mv[2]] );
1172 if( h->param.b_cabac )
1174 uint8_t (*mvd)[8][2] = h->mb.mvd[l];
1175 if( h->mb.i_neighbour & MB_TOP )
1176 CP64( h->mb.cache.mvd[l][x264_scan8[0] - 8], mvd[top][0] );
1178 M64( h->mb.cache.mvd[l][x264_scan8[0] - 8] ) = 0;
1180 if( h->mb.i_neighbour & MB_LEFT && (!b_mbaff || h->mb.cache.ref[l][x264_scan8[0]-1] >= 0) )
1182 CP16( h->mb.cache.mvd[l][x264_scan8[0 ] - 1], mvd[left[LTOP]][left_index_table->intra[0]] );
1183 CP16( h->mb.cache.mvd[l][x264_scan8[2 ] - 1], mvd[left[LTOP]][left_index_table->intra[1]] );
1187 M16( h->mb.cache.mvd[l][x264_scan8[0]-1+0*8] ) = 0;
1188 M16( h->mb.cache.mvd[l][x264_scan8[0]-1+1*8] ) = 0;
1190 if( h->mb.i_neighbour & MB_LEFT && (!b_mbaff || h->mb.cache.ref[l][x264_scan8[0]-1+2*8] >=0) )
1192 CP16( h->mb.cache.mvd[l][x264_scan8[8 ] - 1], mvd[left[LBOT]][left_index_table->intra[2]] );
1193 CP16( h->mb.cache.mvd[l][x264_scan8[10] - 1], mvd[left[LBOT]][left_index_table->intra[3]] );
1197 M16( h->mb.cache.mvd[l][x264_scan8[0]-1+2*8] ) = 0;
1198 M16( h->mb.cache.mvd[l][x264_scan8[0]-1+3*8] ) = 0;
1202 /* If motion vectors are cached from frame macroblocks but this
1203 * macroblock is a field macroblock then the motion vector must be
1204 * halved. Similarly, motion vectors from field macroblocks are doubled. */
1208 if( FIELD_DIFFERENT(h->mb.i_mb_topleft_xy) )\
1209 MAP_F2F(mv, ref, x264_scan8[0] - 1 - 1*8)\
1210 if( FIELD_DIFFERENT(top) )\
1212 MAP_F2F(mv, ref, x264_scan8[0] + 0 - 1*8)\
1213 MAP_F2F(mv, ref, x264_scan8[0] + 1 - 1*8)\
1214 MAP_F2F(mv, ref, x264_scan8[0] + 2 - 1*8)\
1215 MAP_F2F(mv, ref, x264_scan8[0] + 3 - 1*8)\
1217 if( FIELD_DIFFERENT(h->mb.i_mb_topright_xy) )\
1218 MAP_F2F(mv, ref, x264_scan8[0] + 4 - 1*8)\
1219 if( FIELD_DIFFERENT(left[0]) )\
1221 MAP_F2F(mv, ref, x264_scan8[0] - 1 + 0*8)\
1222 MAP_F2F(mv, ref, x264_scan8[0] - 1 + 1*8)\
1223 MAP_F2F(mv, ref, x264_scan8[0] - 1 + 2*8)\
1224 MAP_F2F(mv, ref, x264_scan8[0] - 1 + 3*8)\
1225 MAP_F2F(topright_mv, topright_ref, 0)\
1226 MAP_F2F(topright_mv, topright_ref, 1)\
1227 MAP_F2F(topright_mv, topright_ref, 2)\
1232 #define FIELD_DIFFERENT(macroblock) (macroblock >= 0 && !h->mb.field[macroblock])
1233 #define MAP_F2F(varmv, varref, index)\
1234 if( h->mb.cache.varref[l][index] >= 0 )\
1236 h->mb.cache.varref[l][index] <<= 1;\
1237 h->mb.cache.varmv[l][index][1] /= 2;\
1238 h->mb.cache.mvd[l][index][1] >>= 1;\
1242 #undef FIELD_DIFFERENT
1246 #define FIELD_DIFFERENT(macroblock) (macroblock >= 0 && h->mb.field[macroblock])
1247 #define MAP_F2F(varmv, varref, index)\
1248 if( h->mb.cache.varref[l][index] >= 0 )\
1250 h->mb.cache.varref[l][index] >>= 1;\
1251 h->mb.cache.varmv[l][index][1] <<= 1;\
1252 h->mb.cache.mvd[l][index][1] <<= 1;\
1256 #undef FIELD_DIFFERENT
1261 if( b_mbaff && mb_x == 0 && !(mb_y&1) && mb_y > 0 )
1262 h->mb.field_decoding_flag = h->mb.field[h->mb.i_mb_xy - h->mb.i_mb_stride];
1264 /* Check whether skip here would cause decoder to predict interlace mode incorrectly.
1265 * FIXME: It might be better to change the interlace type rather than forcing a skip to be non-skip. */
1266 h->mb.b_allow_skip = 1;
1269 if( MB_INTERLACED != h->mb.field_decoding_flag &&
1270 h->mb.i_mb_prev_xy >= 0 && IS_SKIP(h->mb.type[h->mb.i_mb_prev_xy]) )
1271 h->mb.b_allow_skip = 0;
1272 if( (mb_y&1) && IS_SKIP(h->mb.type[h->mb.i_mb_xy - h->mb.i_mb_stride]) )
1274 if( h->mb.i_neighbour & MB_LEFT )
1276 if( h->mb.field[h->mb.i_mb_xy - 1] != MB_INTERLACED )
1277 h->mb.b_allow_skip = 0;
1279 else if( h->mb.i_neighbour & MB_TOP )
1281 if( h->mb.field[h->mb.i_mb_top_xy] != MB_INTERLACED )
1282 h->mb.b_allow_skip = 0;
1284 else // Frame mb pair is predicted
1287 h->mb.b_allow_skip = 0;
1292 if( h->param.b_cabac )
1296 int left_xy, top_xy;
1297 /* Neighbours here are calculated based on field_decoding_flag */
1298 int mb_xy = mb_x + (mb_y&~1)*h->mb.i_mb_stride;
1299 left_xy = mb_xy - 1;
1300 if( (mb_y&1) && mb_x > 0 && h->mb.field_decoding_flag == h->mb.field[left_xy] )
1301 left_xy += h->mb.i_mb_stride;
1302 if( h->mb.field_decoding_flag )
1304 top_xy = mb_xy - h->mb.i_mb_stride;
1305 if( !(mb_y&1) && top_xy >= 0 && h->mb.slice_table[top_xy] == h->sh.i_first_mb && h->mb.field[top_xy] )
1306 top_xy -= h->mb.i_mb_stride;
1309 top_xy = mb_x + (mb_y-1)*h->mb.i_mb_stride;
1311 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] ))
1312 + (top_xy >= 0 && h->mb.slice_table[top_xy] == h->sh.i_first_mb && !IS_SKIP( h->mb.type[top_xy] ));
1316 h->mb.cache.i_neighbour_skip = ((h->mb.i_neighbour & MB_LEFT) && !IS_SKIP( h->mb.i_mb_type_left[0] ))
1317 + ((h->mb.i_neighbour & MB_TOP) && !IS_SKIP( h->mb.i_mb_type_top ));
1322 if( h->sh.i_type == SLICE_TYPE_B )
1324 h->mb.bipred_weight = h->mb.bipred_weight_buf[MB_INTERLACED][MB_INTERLACED&(mb_y&1)];
1325 h->mb.dist_scale_factor = h->mb.dist_scale_factor_buf[MB_INTERLACED][MB_INTERLACED&(mb_y&1)];
1326 if( h->param.b_cabac )
1329 x264_macroblock_cache_skip( h, 0, 0, 4, 4, 0 );
1332 skipbp = (h->mb.i_neighbour & MB_LEFT) ? h->mb.skipbp[left[LTOP]] : 0;
1333 h->mb.cache.skip[x264_scan8[0] - 1] = (skipbp >> (1+(left_index_table->mv[0]&~1))) & 1;
1334 skipbp = (h->mb.i_neighbour & MB_LEFT) ? h->mb.skipbp[left[LBOT]] : 0;
1335 h->mb.cache.skip[x264_scan8[8] - 1] = (skipbp >> (1+(left_index_table->mv[2]&~1))) & 1;
1339 skipbp = (h->mb.i_neighbour & MB_LEFT) ? h->mb.skipbp[left[0]] : 0;
1340 h->mb.cache.skip[x264_scan8[0] - 1] = skipbp & 0x2;
1341 h->mb.cache.skip[x264_scan8[8] - 1] = skipbp & 0x8;
1343 skipbp = (h->mb.i_neighbour & MB_TOP) ? h->mb.skipbp[top] : 0;
1344 h->mb.cache.skip[x264_scan8[0] - 8] = skipbp & 0x4;
1345 h->mb.cache.skip[x264_scan8[4] - 8] = skipbp & 0x8;
1349 if( h->sh.i_type == SLICE_TYPE_P )
1350 x264_mb_predict_mv_pskip( h, h->mb.cache.pskip_mv );
1352 h->mb.i_neighbour4[0] =
1353 h->mb.i_neighbour8[0] = (h->mb.i_neighbour_intra & (MB_TOP|MB_LEFT|MB_TOPLEFT))
1354 | ((h->mb.i_neighbour_intra & MB_TOP) ? MB_TOPRIGHT : 0);
1355 h->mb.i_neighbour4[4] =
1356 h->mb.i_neighbour4[1] = MB_LEFT | ((h->mb.i_neighbour_intra & MB_TOP) ? (MB_TOP|MB_TOPLEFT|MB_TOPRIGHT) : 0);
1357 h->mb.i_neighbour4[2] =
1358 h->mb.i_neighbour4[8] =
1359 h->mb.i_neighbour4[10] =
1360 h->mb.i_neighbour8[2] = MB_TOP|MB_TOPRIGHT | ((h->mb.i_neighbour_intra & MB_LEFT) ? (MB_LEFT|MB_TOPLEFT) : 0);
1361 h->mb.i_neighbour4[5] =
1362 h->mb.i_neighbour8[1] = MB_LEFT | (h->mb.i_neighbour_intra & MB_TOPRIGHT)
1363 | ((h->mb.i_neighbour_intra & MB_TOP) ? MB_TOP|MB_TOPLEFT : 0);
1366 void x264_macroblock_cache_load_progressive( x264_t *h, int mb_x, int mb_y )
1368 x264_macroblock_cache_load( h, mb_x, mb_y, 0 );
1371 void x264_macroblock_cache_load_interlaced( x264_t *h, int mb_x, int mb_y )
1373 x264_macroblock_cache_load( h, mb_x, mb_y, 1 );
1376 static void x264_macroblock_deblock_strength_mbaff( x264_t *h, uint8_t (*bs)[8][4] )
1378 if( (h->mb.i_neighbour & MB_LEFT) && h->mb.field[h->mb.i_mb_left_xy[0]] != MB_INTERLACED )
1380 static const uint8_t offset[2][2][8] =
1381 { { { 0, 0, 0, 0, 1, 1, 1, 1 },
1382 { 2, 2, 2, 2, 3, 3, 3, 3 }, },
1383 { { 0, 1, 2, 3, 0, 1, 2, 3 },
1384 { 0, 1, 2, 3, 0, 1, 2, 3 }, }
1386 ALIGNED_ARRAY_8( uint8_t, tmpbs, [8] );
1388 const uint8_t *off = offset[MB_INTERLACED][h->mb.i_mb_y&1];
1389 uint8_t (*nnz)[48] = h->mb.non_zero_count;
1391 for( int i = 0; i < 8; i++ )
1393 int left = h->mb.i_mb_left_xy[MB_INTERLACED ? i>>2 : i&1];
1394 int nnz_this = h->mb.cache.non_zero_count[x264_scan8[0]+8*(i>>1)];
1395 int nnz_left = nnz[left][3 + 4*off[i]];
1396 if( !h->param.b_cabac && h->pps->b_transform_8x8_mode )
1399 if( h->mb.mb_transform_size[left] )
1400 nnz_left = !!(M16( &nnz[left][2+4*j] ) | M16( &nnz[left][2+4*(1+j)] ));
1402 tmpbs[i] = (nnz_left || nnz_this) ? 2 : 1;
1407 CP32( bs[0][0], &tmpbs[0] );
1408 CP32( bs[0][4], &tmpbs[4] );
1412 for( int i = 0; i < 4; i++ ) bs[0][0][i] = tmpbs[2*i];
1413 for( int i = 0; i < 4; i++ ) bs[0][4][i] = tmpbs[1+2*i];
1417 if( (h->mb.i_neighbour & MB_TOP) && MB_INTERLACED != h->mb.field[h->mb.i_mb_top_xy] )
1419 if( !(h->mb.i_mb_y&1) && !MB_INTERLACED )
1421 /* Need to filter both fields (even for frame macroblocks).
1422 * Filter top two rows using the top macroblock of the above
1423 * pair and then the bottom one. */
1424 int mbn_xy = h->mb.i_mb_xy - 2 * h->mb.i_mb_stride;
1425 uint8_t *nnz_cur = &h->mb.cache.non_zero_count[x264_scan8[0]];
1427 for( int j = 0; j < 2; j++, mbn_xy += h->mb.i_mb_stride )
1429 uint8_t (*nnz)[48] = h->mb.non_zero_count;
1431 ALIGNED_4( uint8_t nnz_top[4] );
1432 CP32( nnz_top, &nnz[mbn_xy][3*4] );
1434 if( !h->param.b_cabac && h->pps->b_transform_8x8_mode && h->mb.mb_transform_size[mbn_xy] )
1436 nnz_top[0] = nnz_top[1] = M16( &nnz[mbn_xy][ 8] ) || M16( &nnz[mbn_xy][12] );
1437 nnz_top[2] = nnz_top[3] = M16( &nnz[mbn_xy][10] ) || M16( &nnz[mbn_xy][14] );
1440 for( int i = 0; i < 4; i++ )
1441 bs[1][4*j][i] = (nnz_cur[i] || nnz_top[i]) ? 2 : 1;
1445 for( int i = 0; i < 4; i++ )
1446 bs[1][0][i] = X264_MAX( bs[1][0][i], 1 );
1450 void x264_macroblock_deblock_strength( x264_t *h )
1452 uint8_t (*bs)[8][4] = h->mb.cache.deblock_strength;
1453 if( IS_INTRA( h->mb.i_type ) )
1455 memset( bs[0][1], 3, 3*4*sizeof(uint8_t) );
1456 memset( bs[1][1], 3, 3*4*sizeof(uint8_t) );
1460 /* Early termination: in this case, nnz guarantees all edges use strength 2.*/
1461 if( h->mb.b_transform_8x8 && !CHROMA444 )
1463 int cbp_mask = 0xf >> CHROMA_V_SHIFT;
1464 if( (h->mb.i_cbp_luma&cbp_mask) == cbp_mask )
1466 M32( bs[0][0] ) = 0x02020202;
1467 M32( bs[0][2] ) = 0x02020202;
1468 M32( bs[0][4] ) = 0x02020202;
1469 memset( bs[1][0], 2, 5*4*sizeof(uint8_t) ); /* [1][1] and [1][3] has to be set for 4:2:2 */
1474 int neighbour_changed = 0;
1475 if( h->sh.i_disable_deblocking_filter_idc != 2 )
1477 neighbour_changed = h->mb.i_neighbour_frame&~h->mb.i_neighbour;
1478 h->mb.i_neighbour = h->mb.i_neighbour_frame;
1481 /* MBAFF deblock uses different left neighbors from encoding */
1482 if( SLICE_MBAFF && (h->mb.i_neighbour & MB_LEFT) && (h->mb.field[h->mb.i_mb_xy - 1] != MB_INTERLACED) )
1484 h->mb.i_mb_left_xy[1] =
1485 h->mb.i_mb_left_xy[0] = h->mb.i_mb_xy - 1;
1486 if( h->mb.i_mb_y&1 )
1487 h->mb.i_mb_left_xy[0] -= h->mb.i_mb_stride;
1489 h->mb.i_mb_left_xy[1] += h->mb.i_mb_stride;
1492 /* If we have multiple slices and we're deblocking on slice edges, we
1493 * have to reload neighbour data. */
1494 if( neighbour_changed )
1496 int top_y = h->mb.i_mb_top_y;
1497 int top_8x8 = (2*top_y+1) * h->mb.i_b8_stride + 2*h->mb.i_mb_x;
1498 int top_4x4 = (4*top_y+3) * h->mb.i_b4_stride + 4*h->mb.i_mb_x;
1499 int s8x8 = h->mb.i_b8_stride;
1500 int s4x4 = h->mb.i_b4_stride;
1502 uint8_t (*nnz)[48] = h->mb.non_zero_count;
1503 const x264_left_table_t *left_index_table = SLICE_MBAFF ? h->mb.left_index_table : &left_indices[3];
1505 if( neighbour_changed & MB_TOP )
1506 CP32( &h->mb.cache.non_zero_count[x264_scan8[0] - 8], &nnz[h->mb.i_mb_top_xy][12] );
1508 if( neighbour_changed & MB_LEFT )
1510 int *left = h->mb.i_mb_left_xy;
1511 h->mb.cache.non_zero_count[x264_scan8[0 ] - 1] = nnz[left[0]][left_index_table->nnz[0]];
1512 h->mb.cache.non_zero_count[x264_scan8[2 ] - 1] = nnz[left[0]][left_index_table->nnz[1]];
1513 h->mb.cache.non_zero_count[x264_scan8[8 ] - 1] = nnz[left[1]][left_index_table->nnz[2]];
1514 h->mb.cache.non_zero_count[x264_scan8[10] - 1] = nnz[left[1]][left_index_table->nnz[3]];
1517 for( int l = 0; l <= (h->sh.i_type == SLICE_TYPE_B); l++ )
1519 int16_t (*mv)[2] = h->mb.mv[l];
1520 int8_t *ref = h->mb.ref[l];
1522 int i8 = x264_scan8[0] - 8;
1523 if( neighbour_changed & MB_TOP )
1525 h->mb.cache.ref[l][i8+0] =
1526 h->mb.cache.ref[l][i8+1] = ref[top_8x8 + 0];
1527 h->mb.cache.ref[l][i8+2] =
1528 h->mb.cache.ref[l][i8+3] = ref[top_8x8 + 1];
1529 CP128( h->mb.cache.mv[l][i8], mv[top_4x4] );
1532 i8 = x264_scan8[0] - 1;
1533 if( neighbour_changed & MB_LEFT )
1535 h->mb.cache.ref[l][i8+0*8] =
1536 h->mb.cache.ref[l][i8+1*8] = ref[h->mb.left_b8[0] + 1 + s8x8*left_index_table->ref[0]];
1537 h->mb.cache.ref[l][i8+2*8] =
1538 h->mb.cache.ref[l][i8+3*8] = ref[h->mb.left_b8[1] + 1 + s8x8*left_index_table->ref[2]];
1540 CP32( h->mb.cache.mv[l][i8+0*8], mv[h->mb.left_b4[0] + 3 + s4x4*left_index_table->mv[0]] );
1541 CP32( h->mb.cache.mv[l][i8+1*8], mv[h->mb.left_b4[0] + 3 + s4x4*left_index_table->mv[1]] );
1542 CP32( h->mb.cache.mv[l][i8+2*8], mv[h->mb.left_b4[1] + 3 + s4x4*left_index_table->mv[2]] );
1543 CP32( h->mb.cache.mv[l][i8+3*8], mv[h->mb.left_b4[1] + 3 + s4x4*left_index_table->mv[3]] );
1548 if( h->param.analyse.i_weighted_pred == X264_WEIGHTP_SMART && h->sh.i_type == SLICE_TYPE_P )
1550 /* Handle reference frame duplicates */
1551 int i8 = x264_scan8[0] - 8;
1552 h->mb.cache.ref[0][i8+0] =
1553 h->mb.cache.ref[0][i8+1] = deblock_ref_table(h->mb.cache.ref[0][i8+0]);
1554 h->mb.cache.ref[0][i8+2] =
1555 h->mb.cache.ref[0][i8+3] = deblock_ref_table(h->mb.cache.ref[0][i8+2]);
1557 i8 = x264_scan8[0] - 1;
1558 h->mb.cache.ref[0][i8+0*8] =
1559 h->mb.cache.ref[0][i8+1*8] = deblock_ref_table(h->mb.cache.ref[0][i8+0*8]);
1560 h->mb.cache.ref[0][i8+2*8] =
1561 h->mb.cache.ref[0][i8+3*8] = deblock_ref_table(h->mb.cache.ref[0][i8+2*8]);
1563 int ref0 = deblock_ref_table(h->mb.cache.ref[0][x264_scan8[ 0]]);
1564 int ref1 = deblock_ref_table(h->mb.cache.ref[0][x264_scan8[ 4]]);
1565 int ref2 = deblock_ref_table(h->mb.cache.ref[0][x264_scan8[ 8]]);
1566 int ref3 = deblock_ref_table(h->mb.cache.ref[0][x264_scan8[12]]);
1567 uint32_t reftop = pack16to32( (uint8_t)ref0, (uint8_t)ref1 ) * 0x0101;
1568 uint32_t refbot = pack16to32( (uint8_t)ref2, (uint8_t)ref3 ) * 0x0101;
1570 M32( &h->mb.cache.ref[0][x264_scan8[0]+8*0] ) = reftop;
1571 M32( &h->mb.cache.ref[0][x264_scan8[0]+8*1] ) = reftop;
1572 M32( &h->mb.cache.ref[0][x264_scan8[0]+8*2] ) = refbot;
1573 M32( &h->mb.cache.ref[0][x264_scan8[0]+8*3] ) = refbot;
1576 /* Munge NNZ for cavlc + 8x8dct */
1577 if( !h->param.b_cabac && h->pps->b_transform_8x8_mode )
1579 uint8_t (*nnz)[48] = h->mb.non_zero_count;
1580 int top = h->mb.i_mb_top_xy;
1581 int *left = h->mb.i_mb_left_xy;
1583 if( (h->mb.i_neighbour & MB_TOP) && h->mb.mb_transform_size[top] )
1585 int i8 = x264_scan8[0] - 8;
1586 int nnz_top0 = M16( &nnz[top][8] ) | M16( &nnz[top][12] );
1587 int nnz_top1 = M16( &nnz[top][10] ) | M16( &nnz[top][14] );
1588 M16( &h->mb.cache.non_zero_count[i8+0] ) = nnz_top0 ? 0x0101 : 0;
1589 M16( &h->mb.cache.non_zero_count[i8+2] ) = nnz_top1 ? 0x0101 : 0;
1592 if( h->mb.i_neighbour & MB_LEFT )
1594 int i8 = x264_scan8[0] - 1;
1595 if( h->mb.mb_transform_size[left[0]] )
1597 int nnz_left0 = M16( &nnz[left[0]][2] ) | M16( &nnz[left[0]][6] );
1598 h->mb.cache.non_zero_count[i8+8*0] = !!nnz_left0;
1599 h->mb.cache.non_zero_count[i8+8*1] = !!nnz_left0;
1601 if( h->mb.mb_transform_size[left[1]] )
1603 int nnz_left1 = M16( &nnz[left[1]][10] ) | M16( &nnz[left[1]][14] );
1604 h->mb.cache.non_zero_count[i8+8*2] = !!nnz_left1;
1605 h->mb.cache.non_zero_count[i8+8*3] = !!nnz_left1;
1609 if( h->mb.b_transform_8x8 )
1611 int nnz0 = M16( &h->mb.cache.non_zero_count[x264_scan8[ 0]] ) | M16( &h->mb.cache.non_zero_count[x264_scan8[ 2]] );
1612 int nnz1 = M16( &h->mb.cache.non_zero_count[x264_scan8[ 4]] ) | M16( &h->mb.cache.non_zero_count[x264_scan8[ 6]] );
1613 int nnz2 = M16( &h->mb.cache.non_zero_count[x264_scan8[ 8]] ) | M16( &h->mb.cache.non_zero_count[x264_scan8[10]] );
1614 int nnz3 = M16( &h->mb.cache.non_zero_count[x264_scan8[12]] ) | M16( &h->mb.cache.non_zero_count[x264_scan8[14]] );
1615 uint32_t nnztop = pack16to32( !!nnz0, !!nnz1 ) * 0x0101;
1616 uint32_t nnzbot = pack16to32( !!nnz2, !!nnz3 ) * 0x0101;
1618 M32( &h->mb.cache.non_zero_count[x264_scan8[0]+8*0] ) = nnztop;
1619 M32( &h->mb.cache.non_zero_count[x264_scan8[0]+8*1] ) = nnztop;
1620 M32( &h->mb.cache.non_zero_count[x264_scan8[0]+8*2] ) = nnzbot;
1621 M32( &h->mb.cache.non_zero_count[x264_scan8[0]+8*3] ) = nnzbot;
1625 h->loopf.deblock_strength( h->mb.cache.non_zero_count, h->mb.cache.ref, h->mb.cache.mv,
1626 bs, 4 >> MB_INTERLACED, h->sh.i_type == SLICE_TYPE_B );
1629 x264_macroblock_deblock_strength_mbaff( h, bs );
1632 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 )
1634 int height = b_chroma ? 16>>CHROMA_V_SHIFT : 16;
1635 int i_stride = h->fdec->i_stride[i];
1636 int i_stride2 = i_stride << (b_mbaff && MB_INTERLACED);
1637 int i_pix_offset = (b_mbaff && MB_INTERLACED)
1638 ? 16 * mb_x + height * (mb_y&~1) * i_stride + (mb_y&1) * i_stride
1639 : 16 * mb_x + height * mb_y * i_stride;
1641 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 );
1643 h->mc.copy[PIXEL_16x16]( &h->fdec->plane[i][i_pix_offset], i_stride2, h->mb.pic.p_fdec[i], FDEC_STRIDE, 16 );
1646 static void ALWAYS_INLINE x264_macroblock_backup_intra( x264_t *h, int mb_x, int mb_y, int b_mbaff )
1648 /* In MBAFF we store the last two rows in intra_border_backup[0] and [1].
1649 * For progressive mbs this is the bottom two rows, and for interlaced the
1650 * bottom row of each field. We also store samples needed for the next
1651 * mbpair in intra_border_backup[2]. */
1652 int backup_dst = !b_mbaff ? (mb_y&1) : (mb_y&1) ? 1 : MB_INTERLACED ? 0 : 2;
1653 memcpy( &h->intra_border_backup[backup_dst][0][mb_x*16 ], h->mb.pic.p_fdec[0]+FDEC_STRIDE*15, 16*sizeof(pixel) );
1656 memcpy( &h->intra_border_backup[backup_dst][1][mb_x*16 ], h->mb.pic.p_fdec[1]+FDEC_STRIDE*15, 16*sizeof(pixel) );
1657 memcpy( &h->intra_border_backup[backup_dst][2][mb_x*16 ], h->mb.pic.p_fdec[2]+FDEC_STRIDE*15, 16*sizeof(pixel) );
1661 int backup_src = (15>>CHROMA_V_SHIFT) * FDEC_STRIDE;
1662 memcpy( &h->intra_border_backup[backup_dst][1][mb_x*16 ], h->mb.pic.p_fdec[1]+backup_src, 8*sizeof(pixel) );
1663 memcpy( &h->intra_border_backup[backup_dst][1][mb_x*16+8], h->mb.pic.p_fdec[2]+backup_src, 8*sizeof(pixel) );
1669 int backup_src = (MB_INTERLACED ? 7 : 14) * FDEC_STRIDE;
1670 backup_dst = MB_INTERLACED ? 2 : 0;
1671 memcpy( &h->intra_border_backup[backup_dst][0][mb_x*16 ], h->mb.pic.p_fdec[0]+backup_src, 16*sizeof(pixel) );
1674 memcpy( &h->intra_border_backup[backup_dst][1][mb_x*16 ], h->mb.pic.p_fdec[1]+backup_src, 16*sizeof(pixel) );
1675 memcpy( &h->intra_border_backup[backup_dst][2][mb_x*16 ], h->mb.pic.p_fdec[2]+backup_src, 16*sizeof(pixel) );
1679 if( CHROMA_FORMAT == CHROMA_420 )
1680 backup_src = (MB_INTERLACED ? 3 : 6) * FDEC_STRIDE;
1681 memcpy( &h->intra_border_backup[backup_dst][1][mb_x*16 ], h->mb.pic.p_fdec[1]+backup_src, 8*sizeof(pixel) );
1682 memcpy( &h->intra_border_backup[backup_dst][1][mb_x*16+8], h->mb.pic.p_fdec[2]+backup_src, 8*sizeof(pixel) );
1688 void x264_macroblock_cache_save( x264_t *h )
1690 const int i_mb_xy = h->mb.i_mb_xy;
1691 const int i_mb_type = x264_mb_type_fix[h->mb.i_type];
1692 const int s8x8 = h->mb.i_b8_stride;
1693 const int s4x4 = h->mb.i_b4_stride;
1694 const int i_mb_4x4 = h->mb.i_b4_xy;
1695 const int i_mb_8x8 = h->mb.i_b8_xy;
1697 /* GCC pessimizes direct stores to heap-allocated arrays due to aliasing. */
1698 /* By only dereferencing them once, we avoid this issue. */
1699 int8_t *i4x4 = h->mb.intra4x4_pred_mode[i_mb_xy];
1700 uint8_t *nnz = h->mb.non_zero_count[i_mb_xy];
1704 x264_macroblock_backup_intra( h, h->mb.i_mb_x, h->mb.i_mb_y, 1 );
1705 x264_macroblock_store_pic( h, h->mb.i_mb_x, h->mb.i_mb_y, 0, 0, 1 );
1708 x264_macroblock_store_pic( h, h->mb.i_mb_x, h->mb.i_mb_y, 1, 0, 1 );
1709 x264_macroblock_store_pic( h, h->mb.i_mb_x, h->mb.i_mb_y, 2, 0, 1 );
1712 x264_macroblock_store_pic( h, h->mb.i_mb_x, h->mb.i_mb_y, 1, 1, 1 );
1716 x264_macroblock_backup_intra( h, h->mb.i_mb_x, h->mb.i_mb_y, 0 );
1717 x264_macroblock_store_pic( h, h->mb.i_mb_x, h->mb.i_mb_y, 0, 0, 0 );
1720 x264_macroblock_store_pic( h, h->mb.i_mb_x, h->mb.i_mb_y, 1, 0, 0 );
1721 x264_macroblock_store_pic( h, h->mb.i_mb_x, h->mb.i_mb_y, 2, 0, 0 );
1724 x264_macroblock_store_pic( h, h->mb.i_mb_x, h->mb.i_mb_y, 1, 1, 0 );
1727 x264_prefetch_fenc( h, h->fdec, h->mb.i_mb_x, h->mb.i_mb_y );
1729 h->mb.type[i_mb_xy] = i_mb_type;
1730 h->mb.slice_table[i_mb_xy] = h->sh.i_first_mb;
1731 h->mb.partition[i_mb_xy] = IS_INTRA( i_mb_type ) ? D_16x16 : h->mb.i_partition;
1732 h->mb.i_mb_prev_xy = i_mb_xy;
1735 if( i_mb_type == I_4x4 )
1737 CP32( &i4x4[0], &h->mb.cache.intra4x4_pred_mode[x264_scan8[10]] );
1738 M32( &i4x4[4] ) = pack8to32( h->mb.cache.intra4x4_pred_mode[x264_scan8[5] ],
1739 h->mb.cache.intra4x4_pred_mode[x264_scan8[7] ],
1740 h->mb.cache.intra4x4_pred_mode[x264_scan8[13] ], 0);
1742 else if( !h->param.b_constrained_intra || IS_INTRA(i_mb_type) )
1743 M64( i4x4 ) = I_PRED_4x4_DC * 0x0101010101010101ULL;
1745 M64( i4x4 ) = (uint8_t)(-1) * 0x0101010101010101ULL;
1748 if( i_mb_type == I_PCM )
1750 h->mb.qp[i_mb_xy] = 0;
1751 h->mb.i_last_dqp = 0;
1752 h->mb.i_cbp_chroma = CHROMA444 ? 0 : 2;
1753 h->mb.i_cbp_luma = 0xf;
1754 h->mb.cbp[i_mb_xy] = (h->mb.i_cbp_chroma << 4) | h->mb.i_cbp_luma | 0x700;
1755 h->mb.b_transform_8x8 = 0;
1756 for( int i = 0; i < 48; i++ )
1757 h->mb.cache.non_zero_count[x264_scan8[i]] = h->param.b_cabac ? 1 : 16;
1761 if( h->mb.i_type != I_16x16 && h->mb.i_cbp_luma == 0 && h->mb.i_cbp_chroma == 0 )
1762 h->mb.i_qp = h->mb.i_last_qp;
1763 h->mb.qp[i_mb_xy] = h->mb.i_qp;
1764 h->mb.i_last_dqp = h->mb.i_qp - h->mb.i_last_qp;
1765 h->mb.i_last_qp = h->mb.i_qp;
1768 /* save non zero count */
1769 CP32( &nnz[ 0+0*4], &h->mb.cache.non_zero_count[x264_scan8[ 0]] );
1770 CP32( &nnz[ 0+1*4], &h->mb.cache.non_zero_count[x264_scan8[ 2]] );
1771 CP32( &nnz[ 0+2*4], &h->mb.cache.non_zero_count[x264_scan8[ 8]] );
1772 CP32( &nnz[ 0+3*4], &h->mb.cache.non_zero_count[x264_scan8[10]] );
1773 CP32( &nnz[16+0*4], &h->mb.cache.non_zero_count[x264_scan8[16+0]] );
1774 CP32( &nnz[16+1*4], &h->mb.cache.non_zero_count[x264_scan8[16+2]] );
1775 CP32( &nnz[32+0*4], &h->mb.cache.non_zero_count[x264_scan8[32+0]] );
1776 CP32( &nnz[32+1*4], &h->mb.cache.non_zero_count[x264_scan8[32+2]] );
1777 if( CHROMA_FORMAT >= CHROMA_422 )
1779 CP32( &nnz[16+2*4], &h->mb.cache.non_zero_count[x264_scan8[16+ 8]] );
1780 CP32( &nnz[16+3*4], &h->mb.cache.non_zero_count[x264_scan8[16+10]] );
1781 CP32( &nnz[32+2*4], &h->mb.cache.non_zero_count[x264_scan8[32+ 8]] );
1782 CP32( &nnz[32+3*4], &h->mb.cache.non_zero_count[x264_scan8[32+10]] );
1785 if( h->mb.i_cbp_luma == 0 && h->mb.i_type != I_8x8 )
1786 h->mb.b_transform_8x8 = 0;
1787 h->mb.mb_transform_size[i_mb_xy] = h->mb.b_transform_8x8;
1789 if( h->sh.i_type != SLICE_TYPE_I )
1791 int16_t (*mv0)[2] = &h->mb.mv[0][i_mb_4x4];
1792 int16_t (*mv1)[2] = &h->mb.mv[1][i_mb_4x4];
1793 int8_t *ref0 = &h->mb.ref[0][i_mb_8x8];
1794 int8_t *ref1 = &h->mb.ref[1][i_mb_8x8];
1795 if( !IS_INTRA( i_mb_type ) )
1797 ref0[0+0*s8x8] = h->mb.cache.ref[0][x264_scan8[0]];
1798 ref0[1+0*s8x8] = h->mb.cache.ref[0][x264_scan8[4]];
1799 ref0[0+1*s8x8] = h->mb.cache.ref[0][x264_scan8[8]];
1800 ref0[1+1*s8x8] = h->mb.cache.ref[0][x264_scan8[12]];
1801 CP128( &mv0[0*s4x4], h->mb.cache.mv[0][x264_scan8[0]+8*0] );
1802 CP128( &mv0[1*s4x4], h->mb.cache.mv[0][x264_scan8[0]+8*1] );
1803 CP128( &mv0[2*s4x4], h->mb.cache.mv[0][x264_scan8[0]+8*2] );
1804 CP128( &mv0[3*s4x4], h->mb.cache.mv[0][x264_scan8[0]+8*3] );
1805 if( h->sh.i_type == SLICE_TYPE_B )
1807 ref1[0+0*s8x8] = h->mb.cache.ref[1][x264_scan8[0]];
1808 ref1[1+0*s8x8] = h->mb.cache.ref[1][x264_scan8[4]];
1809 ref1[0+1*s8x8] = h->mb.cache.ref[1][x264_scan8[8]];
1810 ref1[1+1*s8x8] = h->mb.cache.ref[1][x264_scan8[12]];
1811 CP128( &mv1[0*s4x4], h->mb.cache.mv[1][x264_scan8[0]+8*0] );
1812 CP128( &mv1[1*s4x4], h->mb.cache.mv[1][x264_scan8[0]+8*1] );
1813 CP128( &mv1[2*s4x4], h->mb.cache.mv[1][x264_scan8[0]+8*2] );
1814 CP128( &mv1[3*s4x4], h->mb.cache.mv[1][x264_scan8[0]+8*3] );
1819 M16( &ref0[0*s8x8] ) = (uint8_t)(-1) * 0x0101;
1820 M16( &ref0[1*s8x8] ) = (uint8_t)(-1) * 0x0101;
1821 M128( &mv0[0*s4x4] ) = M128_ZERO;
1822 M128( &mv0[1*s4x4] ) = M128_ZERO;
1823 M128( &mv0[2*s4x4] ) = M128_ZERO;
1824 M128( &mv0[3*s4x4] ) = M128_ZERO;
1825 if( h->sh.i_type == SLICE_TYPE_B )
1827 M16( &ref1[0*s8x8] ) = (uint8_t)(-1) * 0x0101;
1828 M16( &ref1[1*s8x8] ) = (uint8_t)(-1) * 0x0101;
1829 M128( &mv1[0*s4x4] ) = M128_ZERO;
1830 M128( &mv1[1*s4x4] ) = M128_ZERO;
1831 M128( &mv1[2*s4x4] ) = M128_ZERO;
1832 M128( &mv1[3*s4x4] ) = M128_ZERO;
1837 if( h->param.b_cabac )
1839 uint8_t (*mvd0)[2] = h->mb.mvd[0][i_mb_xy];
1840 uint8_t (*mvd1)[2] = h->mb.mvd[1][i_mb_xy];
1841 if( IS_INTRA(i_mb_type) && i_mb_type != I_PCM )
1842 h->mb.chroma_pred_mode[i_mb_xy] = x264_mb_chroma_pred_mode_fix[h->mb.i_chroma_pred_mode];
1844 h->mb.chroma_pred_mode[i_mb_xy] = I_PRED_CHROMA_DC;
1846 if( (0x3FF30 >> i_mb_type) & 1 ) /* !INTRA && !SKIP && !DIRECT */
1848 CP64( mvd0[0], h->mb.cache.mvd[0][x264_scan8[10]] );
1849 CP16( mvd0[4], h->mb.cache.mvd[0][x264_scan8[5 ]] );
1850 CP16( mvd0[5], h->mb.cache.mvd[0][x264_scan8[7 ]] );
1851 CP16( mvd0[6], h->mb.cache.mvd[0][x264_scan8[13]] );
1852 if( h->sh.i_type == SLICE_TYPE_B )
1854 CP64( mvd1[0], h->mb.cache.mvd[1][x264_scan8[10]] );
1855 CP16( mvd1[4], h->mb.cache.mvd[1][x264_scan8[5 ]] );
1856 CP16( mvd1[5], h->mb.cache.mvd[1][x264_scan8[7 ]] );
1857 CP16( mvd1[6], h->mb.cache.mvd[1][x264_scan8[13]] );
1862 M128( mvd0[0] ) = M128_ZERO;
1863 if( h->sh.i_type == SLICE_TYPE_B )
1864 M128( mvd1[0] ) = M128_ZERO;
1867 if( h->sh.i_type == SLICE_TYPE_B )
1869 if( i_mb_type == B_SKIP || i_mb_type == B_DIRECT )
1870 h->mb.skipbp[i_mb_xy] = 0xf;
1871 else if( i_mb_type == B_8x8 )
1873 int skipbp = ( h->mb.i_sub_partition[0] == D_DIRECT_8x8 ) << 0;
1874 skipbp |= ( h->mb.i_sub_partition[1] == D_DIRECT_8x8 ) << 1;
1875 skipbp |= ( h->mb.i_sub_partition[2] == D_DIRECT_8x8 ) << 2;
1876 skipbp |= ( h->mb.i_sub_partition[3] == D_DIRECT_8x8 ) << 3;
1877 h->mb.skipbp[i_mb_xy] = skipbp;
1880 h->mb.skipbp[i_mb_xy] = 0;
1886 void x264_macroblock_bipred_init( x264_t *h )
1888 for( int mbfield = 0; mbfield <= SLICE_MBAFF; mbfield++ )
1889 for( int field = 0; field <= SLICE_MBAFF; field++ )
1890 for( int i_ref0 = 0; i_ref0 < (h->i_ref[0]<<mbfield); i_ref0++ )
1892 x264_frame_t *l0 = h->fref[0][i_ref0>>mbfield];
1893 int poc0 = l0->i_poc + mbfield*l0->i_delta_poc[field^(i_ref0&1)];
1894 for( int i_ref1 = 0; i_ref1 < (h->i_ref[1]<<mbfield); i_ref1++ )
1896 int dist_scale_factor;
1897 x264_frame_t *l1 = h->fref[1][i_ref1>>mbfield];
1898 int cur_poc = h->fdec->i_poc + mbfield*h->fdec->i_delta_poc[field];
1899 int poc1 = l1->i_poc + mbfield*l1->i_delta_poc[field^(i_ref1&1)];
1900 int td = x264_clip3( poc1 - poc0, -128, 127 );
1901 if( td == 0 /* || pic0 is a long-term ref */ )
1902 dist_scale_factor = 256;
1905 int tb = x264_clip3( cur_poc - poc0, -128, 127 );
1906 int tx = (16384 + (abs(td) >> 1)) / td;
1907 dist_scale_factor = x264_clip3( (tb * tx + 32) >> 6, -1024, 1023 );
1910 h->mb.dist_scale_factor_buf[mbfield][field][i_ref0][i_ref1] = dist_scale_factor;
1912 dist_scale_factor >>= 2;
1913 if( h->param.analyse.b_weighted_bipred
1914 && dist_scale_factor >= -64
1915 && dist_scale_factor <= 128 )
1917 h->mb.bipred_weight_buf[mbfield][field][i_ref0][i_ref1] = 64 - dist_scale_factor;
1918 // ssse3 implementation of biweight doesn't support the extrema.
1919 // if we ever generate them, we'll have to drop that optimization.
1920 assert( dist_scale_factor >= -63 && dist_scale_factor <= 127 );
1923 h->mb.bipred_weight_buf[mbfield][field][i_ref0][i_ref1] = 32;