1 /*****************************************************************************
2 * macroblock.c: macroblock common functions
3 *****************************************************************************
4 * Copyright (C) 2003-2011 x264 project
6 * Authors: Jason Garrett-Glaser <darkshikari@gmail.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 int 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 <= 4*PARAM_INTERLACED; 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;
368 if( !PARAM_INTERLACED )
369 h->intra_border_backup[1][j] = h->intra_border_backup[i][j];
371 for( int i = 0; i <= PARAM_INTERLACED; i++ )
373 CHECKED_MALLOC( h->deblock_strength[i], sizeof(**h->deblock_strength) * h->mb.i_mb_width );
374 h->deblock_strength[1] = h->deblock_strength[i];
378 /* Allocate scratch buffer */
379 int scratch_size = 0;
382 int buf_hpel = (h->thread[0]->fdec->i_width[0]+48) * sizeof(int16_t);
383 int buf_ssim = h->param.analyse.b_ssim * 8 * (h->param.i_width/4+3) * sizeof(int);
384 int me_range = X264_MIN(h->param.analyse.i_me_range, h->param.analyse.i_mv_range);
385 int buf_tesa = (h->param.analyse.i_me_method >= X264_ME_ESA) *
386 ((me_range*2+24) * sizeof(int16_t) + (me_range+4) * (me_range+1) * 4 * sizeof(mvsad_t));
387 scratch_size = X264_MAX3( buf_hpel, buf_ssim, buf_tesa );
389 int buf_mbtree = h->param.rc.b_mb_tree * ((h->mb.i_mb_width+7)&~7) * sizeof(int);
390 scratch_size = X264_MAX( scratch_size, buf_mbtree );
392 CHECKED_MALLOC( h->scratch_buffer, scratch_size );
394 h->scratch_buffer = NULL;
401 void x264_macroblock_thread_free( x264_t *h, int b_lookahead )
405 for( int i = 0; i <= PARAM_INTERLACED; i++ )
406 x264_free( h->deblock_strength[i] );
407 for( int i = 0; i <= 4*PARAM_INTERLACED; i++ )
408 for( int j = 0; j < (CHROMA444 ? 3 : 2); j++ )
409 x264_free( h->intra_border_backup[i][j] - 16 );
411 x264_free( h->scratch_buffer );
414 void x264_macroblock_slice_init( x264_t *h )
416 h->mb.mv[0] = h->fdec->mv[0];
417 h->mb.mv[1] = h->fdec->mv[1];
418 h->mb.mvr[0][0] = h->fdec->mv16x16;
419 h->mb.ref[0] = h->fdec->ref[0];
420 h->mb.ref[1] = h->fdec->ref[1];
421 h->mb.type = h->fdec->mb_type;
422 h->mb.partition = h->fdec->mb_partition;
423 h->mb.field = h->fdec->field;
425 h->fdec->i_ref[0] = h->i_ref[0];
426 h->fdec->i_ref[1] = h->i_ref[1];
427 for( int i = 0; i < h->i_ref[0]; i++ )
428 h->fdec->ref_poc[0][i] = h->fref[0][i]->i_poc;
429 if( h->sh.i_type == SLICE_TYPE_B )
431 for( int i = 0; i < h->i_ref[1]; i++ )
432 h->fdec->ref_poc[1][i] = h->fref[1][i]->i_poc;
434 map_col_to_list0(-1) = -1;
435 map_col_to_list0(-2) = -2;
436 for( int i = 0; i < h->fref[1][0]->i_ref[0]; i++ )
438 int poc = h->fref[1][0]->ref_poc[0][i];
439 map_col_to_list0(i) = -2;
440 for( int j = 0; j < h->i_ref[0]; j++ )
441 if( h->fref[0][j]->i_poc == poc )
443 map_col_to_list0(i) = j;
448 else if( h->sh.i_type == SLICE_TYPE_P )
450 if( h->sh.i_disable_deblocking_filter_idc != 1 && h->param.analyse.i_weighted_pred == X264_WEIGHTP_SMART )
452 deblock_ref_table(-2) = -2;
453 deblock_ref_table(-1) = -1;
454 for( int i = 0; i < h->i_ref[0] << SLICE_MBAFF; i++ )
456 /* Mask off high bits to avoid frame num collisions with -1/-2.
457 * In current x264 frame num values don't cover a range of more
458 * than 32, so 6 bits is enough for uniqueness. */
460 deblock_ref_table(i) = h->fref[0][i]->i_frame_num&63;
462 deblock_ref_table(i) = ((h->fref[0][i>>1]->i_frame_num&63)<<1) + (i&1);
467 /* init with not available (for top right idx=7,15) */
468 memset( h->mb.cache.ref, -2, sizeof( h->mb.cache.ref ) );
470 if( h->i_ref[0] > 0 )
471 for( int field = 0; field <= SLICE_MBAFF; field++ )
473 int curpoc = h->fdec->i_poc + h->fdec->i_delta_poc[field];
474 int refpoc = h->fref[0][0]->i_poc + h->fref[0][0]->i_delta_poc[field];
475 int delta = curpoc - refpoc;
477 h->fdec->inv_ref_poc[field] = (256 + delta/2) / delta;
480 h->mb.i_neighbour4[6] =
481 h->mb.i_neighbour4[9] =
482 h->mb.i_neighbour4[12] =
483 h->mb.i_neighbour4[14] = MB_LEFT|MB_TOP|MB_TOPLEFT|MB_TOPRIGHT;
484 h->mb.i_neighbour4[3] =
485 h->mb.i_neighbour4[7] =
486 h->mb.i_neighbour4[11] =
487 h->mb.i_neighbour4[13] =
488 h->mb.i_neighbour4[15] =
489 h->mb.i_neighbour8[3] = MB_LEFT|MB_TOP|MB_TOPLEFT;
492 void x264_macroblock_thread_init( x264_t *h )
494 h->mb.i_me_method = h->param.analyse.i_me_method;
495 h->mb.i_subpel_refine = h->param.analyse.i_subpel_refine;
496 if( h->sh.i_type == SLICE_TYPE_B && (h->mb.i_subpel_refine == 6 || h->mb.i_subpel_refine == 8) )
497 h->mb.i_subpel_refine--;
498 h->mb.b_chroma_me = h->param.analyse.b_chroma_me &&
499 ((h->sh.i_type == SLICE_TYPE_P && h->mb.i_subpel_refine >= 5) ||
500 (h->sh.i_type == SLICE_TYPE_B && h->mb.i_subpel_refine >= 9));
501 h->mb.b_dct_decimate = h->sh.i_type == SLICE_TYPE_B ||
502 (h->param.analyse.b_dct_decimate && h->sh.i_type != SLICE_TYPE_I);
503 h->mb.i_mb_prev_xy = -1;
506 * fdec fenc fdec fenc fdec fenc
507 * 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
508 * 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
509 * 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
510 * 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
511 * 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
512 * 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
513 * 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
514 * 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
515 * u U U v V V u U U U U V V V V
516 * u U U v V V u U U U U V V V V
517 * v v v v v v v V V V V
523 h->mb.pic.p_fenc[0] = h->mb.pic.fenc_buf;
524 h->mb.pic.p_fdec[0] = h->mb.pic.fdec_buf + 2*FDEC_STRIDE;
525 h->mb.pic.p_fenc[1] = h->mb.pic.fenc_buf + 16*FENC_STRIDE;
526 h->mb.pic.p_fdec[1] = h->mb.pic.fdec_buf + 19*FDEC_STRIDE;
529 h->mb.pic.p_fenc[2] = h->mb.pic.fenc_buf + 32*FENC_STRIDE;
530 h->mb.pic.p_fdec[2] = h->mb.pic.fdec_buf + 36*FDEC_STRIDE;
534 h->mb.pic.p_fenc[2] = h->mb.pic.fenc_buf + 16*FENC_STRIDE + 8;
535 h->mb.pic.p_fdec[2] = h->mb.pic.fdec_buf + 19*FDEC_STRIDE + 16;
539 void x264_prefetch_fenc( x264_t *h, x264_frame_t *fenc, int i_mb_x, int i_mb_y )
541 int stride_y = fenc->i_stride[0];
542 int stride_uv = fenc->i_stride[1];
543 int off_y = 16 * i_mb_x + 16 * i_mb_y * stride_y;
544 int off_uv = 16 * i_mb_x + (16 * i_mb_y * stride_uv >> CHROMA_V_SHIFT);
545 h->mc.prefetch_fenc( fenc->plane[0]+off_y, stride_y,
546 fenc->plane[1]+off_uv, stride_uv, i_mb_x );
549 NOINLINE void x264_copy_column8( pixel *dst, pixel *src )
551 // input pointers are offset by 4 rows because that's faster (smaller instruction size on x86)
552 for( int i = -4; i < 4; i++ )
553 dst[i*FDEC_STRIDE] = src[i*FDEC_STRIDE];
556 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 )
558 int mb_interlaced = b_mbaff && MB_INTERLACED;
559 int height = b_chroma ? 16 >> CHROMA_V_SHIFT : 16;
560 int i_stride = h->fdec->i_stride[i];
561 int i_stride2 = i_stride << mb_interlaced;
562 int i_pix_offset = mb_interlaced
563 ? 16 * mb_x + height * (mb_y&~1) * i_stride + (mb_y&1) * i_stride
564 : 16 * mb_x + height * mb_y * i_stride;
565 pixel *plane_fdec = &h->fdec->plane[i][i_pix_offset];
566 int fdec_idx = b_mbaff ? (mb_interlaced ? (3 + (mb_y&1)) : (mb_y&1) ? 2 : 4) : 0;
567 pixel *intra_fdec = &h->intra_border_backup[fdec_idx][i][mb_x*16];
568 int ref_pix_offset[2] = { i_pix_offset, i_pix_offset };
569 /* ref_pix_offset[0] references the current field and [1] the opposite field. */
571 ref_pix_offset[1] += (1-2*(mb_y&1)) * i_stride;
572 h->mb.pic.i_stride[i] = i_stride2;
573 h->mb.pic.p_fenc_plane[i] = &h->fenc->plane[i][i_pix_offset];
576 h->mc.load_deinterleave_chroma_fenc( h->mb.pic.p_fenc[1], h->mb.pic.p_fenc_plane[1], i_stride2, height );
577 memcpy( h->mb.pic.p_fdec[1]-FDEC_STRIDE, intra_fdec, 8*sizeof(pixel) );
578 memcpy( h->mb.pic.p_fdec[2]-FDEC_STRIDE, intra_fdec+8, 8*sizeof(pixel) );
581 h->mb.pic.p_fdec[1][-FDEC_STRIDE-1] = intra_fdec[-1-8];
582 h->mb.pic.p_fdec[2][-FDEC_STRIDE-1] = intra_fdec[-1];
587 h->mc.copy[PIXEL_16x16]( h->mb.pic.p_fenc[i], FENC_STRIDE, h->mb.pic.p_fenc_plane[i], i_stride2, 16 );
588 memcpy( h->mb.pic.p_fdec[i]-FDEC_STRIDE, intra_fdec, 24*sizeof(pixel) );
590 h->mb.pic.p_fdec[i][-FDEC_STRIDE-1] = intra_fdec[-1];
594 for( int j = 0; j < height; j++ )
597 h->mb.pic.p_fdec[1][-1+j*FDEC_STRIDE] = plane_fdec[-2+j*i_stride2];
598 h->mb.pic.p_fdec[2][-1+j*FDEC_STRIDE] = plane_fdec[-1+j*i_stride2];
601 h->mb.pic.p_fdec[i][-1+j*FDEC_STRIDE] = plane_fdec[-1+j*i_stride2];
603 pixel *plane_src, **filtered_src;
604 for( int j = 0; j < h->mb.pic.i_fref[0]; j++ )
606 // Interpolate between pixels in same field.
609 plane_src = h->fref[0][j>>1]->plane_fld[i];
610 filtered_src = h->fref[0][j>>1]->filtered_fld[i];
614 plane_src = h->fref[0][j]->plane[i];
615 filtered_src = h->fref[0][j]->filtered[i];
617 h->mb.pic.p_fref[0][j][i*4] = plane_src + ref_pix_offset[j&1];
621 for( int k = 1; k < 4; k++ )
622 h->mb.pic.p_fref[0][j][i*4+k] = filtered_src[k] + ref_pix_offset[j&1];
625 if( h->sh.weight[j][0].weightfn )
626 h->mb.pic.p_fref_w[j] = &h->fenc->weighted[j >> mb_interlaced][ref_pix_offset[j&1]];
628 h->mb.pic.p_fref_w[j] = h->mb.pic.p_fref[0][j][0];
632 if( h->sh.i_type == SLICE_TYPE_B )
633 for( int j = 0; j < h->mb.pic.i_fref[1]; j++ )
637 plane_src = h->fref[1][j>>1]->plane_fld[i];
638 filtered_src = h->fref[1][j>>1]->filtered_fld[i];
642 plane_src = h->fref[1][j]->plane[i];
643 filtered_src = h->fref[1][j]->filtered[i];
645 h->mb.pic.p_fref[1][j][i*4] = plane_src + ref_pix_offset[j&1];
648 for( int k = 1; k < 4; k++ )
649 h->mb.pic.p_fref[1][j][i*4+k] = filtered_src[k] + ref_pix_offset[j&1];
653 static const x264_left_table_t left_indices[4] =
655 /* Current is progressive */
656 {{ 4, 4, 5, 5}, { 3, 3, 7, 7}, {16+1, 16+1, 32+1, 32+1}, {0, 0, 1, 1}, {0, 0, 0, 0}},
657 {{ 6, 6, 3, 3}, {11, 11, 15, 15}, {16+5, 16+5, 32+5, 32+5}, {2, 2, 3, 3}, {1, 1, 1, 1}},
658 /* Current is interlaced */
659 {{ 4, 6, 4, 6}, { 3, 11, 3, 11}, {16+1, 16+1, 32+1, 32+1}, {0, 2, 0, 2}, {0, 1, 0, 1}},
661 {{ 4, 5, 6, 3}, { 3, 7, 11, 15}, {16+1, 16+5, 32+1, 32+5}, {0, 1, 2, 3}, {0, 0, 1, 1}}
664 static void ALWAYS_INLINE x264_macroblock_cache_load_neighbours( x264_t *h, int mb_x, int mb_y, int b_interlaced )
666 const int mb_interlaced = b_interlaced && MB_INTERLACED;
667 int top_y = mb_y - (1 << mb_interlaced);
668 int top = top_y * h->mb.i_mb_stride + mb_x;
672 h->mb.i_mb_xy = mb_y * h->mb.i_mb_stride + mb_x;
673 h->mb.i_b8_xy = 2*(mb_y * h->mb.i_b8_stride + mb_x);
674 h->mb.i_b4_xy = 4*(mb_y * h->mb.i_b4_stride + mb_x);
676 h->mb.left_b8[1] = -1;
678 h->mb.left_b4[1] = -1;
679 h->mb.i_neighbour = 0;
680 h->mb.i_neighbour_intra = 0;
681 h->mb.i_neighbour_frame = 0;
682 h->mb.i_mb_top_xy = -1;
683 h->mb.i_mb_top_y = -1;
684 h->mb.i_mb_left_xy[0] = h->mb.i_mb_left_xy[1] = -1;
685 h->mb.i_mb_topleft_xy = -1;
686 h->mb.i_mb_topright_xy = -1;
687 h->mb.i_mb_type_top = -1;
688 h->mb.i_mb_type_left[0] = h->mb.i_mb_type_left[1] = -1;
689 h->mb.i_mb_type_topleft = -1;
690 h->mb.i_mb_type_topright = -1;
691 h->mb.left_index_table = &left_indices[3];
692 h->mb.topleft_partition = 0;
694 int topleft_y = top_y;
695 int topright_y = top_y;
698 left[0] = left[1] = h->mb.i_mb_xy - 1;
699 h->mb.left_b8[0] = h->mb.left_b8[1] = h->mb.i_b8_xy - 2;
700 h->mb.left_b4[0] = h->mb.left_b4[1] = h->mb.i_b4_xy - 4;
704 h->mb.i_mb_top_mbpair_xy = h->mb.i_mb_xy - 2*h->mb.i_mb_stride;
705 h->mb.i_mb_topleft_y = -1;
706 h->mb.i_mb_topright_y = -1;
710 if( mb_x && mb_interlaced != h->mb.field[h->mb.i_mb_xy-1] )
712 left[0] = left[1] = h->mb.i_mb_xy - 1 - h->mb.i_mb_stride;
713 h->mb.left_b8[0] = h->mb.left_b8[1] = h->mb.i_b8_xy - 2 - 2*h->mb.i_b8_stride;
714 h->mb.left_b4[0] = h->mb.left_b4[1] = h->mb.i_b4_xy - 4 - 4*h->mb.i_b4_stride;
718 h->mb.left_index_table = &left_indices[2];
719 left[1] += h->mb.i_mb_stride;
720 h->mb.left_b8[1] += 2*h->mb.i_b8_stride;
721 h->mb.left_b4[1] += 4*h->mb.i_b4_stride;
725 h->mb.left_index_table = &left_indices[1];
727 h->mb.topleft_partition = 1;
735 if( mb_interlaced && top >= 0 )
737 if( !h->mb.field[top] )
739 top += h->mb.i_mb_stride;
743 topleft_y += !h->mb.field[h->mb.i_mb_stride*topleft_y + mb_x - 1];
744 if( mb_x < h->mb.i_mb_width-1 )
745 topright_y += !h->mb.field[h->mb.i_mb_stride*topright_y + mb_x + 1];
747 if( mb_x && mb_interlaced != h->mb.field[h->mb.i_mb_xy-1] )
751 h->mb.left_index_table = &left_indices[2];
752 left[1] += h->mb.i_mb_stride;
753 h->mb.left_b8[1] += 2*h->mb.i_b8_stride;
754 h->mb.left_b4[1] += 4*h->mb.i_b4_stride;
757 h->mb.left_index_table = &left_indices[0];
764 h->mb.i_neighbour_frame |= MB_LEFT;
765 h->mb.i_mb_left_xy[0] = left[0];
766 h->mb.i_mb_left_xy[1] = left[1];
767 h->mb.i_mb_type_left[0] = h->mb.type[h->mb.i_mb_left_xy[0]];
768 h->mb.i_mb_type_left[1] = h->mb.type[h->mb.i_mb_left_xy[1]];
769 if( h->mb.slice_table[left[0]] == h->sh.i_first_mb )
771 h->mb.i_neighbour |= MB_LEFT;
773 // FIXME: We don't currently support constrained intra + mbaff.
774 if( !h->param.b_constrained_intra || IS_INTRA( h->mb.i_mb_type_left[0] ) )
775 h->mb.i_neighbour_intra |= MB_LEFT;
779 /* We can't predict from the previous threadslice since it hasn't been encoded yet. */
780 if( (h->i_threadslice_start >> mb_interlaced) != (mb_y >> mb_interlaced) )
784 h->mb.i_neighbour_frame |= MB_TOP;
785 h->mb.i_mb_top_xy = top;
786 h->mb.i_mb_top_y = top_y;
787 h->mb.i_mb_type_top = h->mb.type[h->mb.i_mb_top_xy];
788 if( h->mb.slice_table[top] == h->sh.i_first_mb )
790 h->mb.i_neighbour |= MB_TOP;
792 if( !h->param.b_constrained_intra || IS_INTRA( h->mb.i_mb_type_top ) )
793 h->mb.i_neighbour_intra |= MB_TOP;
795 /* We only need to prefetch the top blocks because the left was just written
796 * to as part of the previous cache_save. Since most target CPUs use write-allocate
797 * caches, left blocks are near-guaranteed to be in L1 cache. Top--not so much. */
798 x264_prefetch( &h->mb.cbp[top] );
799 x264_prefetch( h->mb.intra4x4_pred_mode[top] );
800 x264_prefetch( &h->mb.non_zero_count[top][12] );
801 /* These aren't always allocated, but prefetching an invalid address can't hurt. */
802 x264_prefetch( &h->mb.mb_transform_size[top] );
803 x264_prefetch( &h->mb.skipbp[top] );
807 if( mb_x > 0 && topleft_y >= 0 )
809 h->mb.i_neighbour_frame |= MB_TOPLEFT;
810 h->mb.i_mb_topleft_xy = h->mb.i_mb_stride*topleft_y + mb_x - 1;
811 h->mb.i_mb_topleft_y = topleft_y;
812 h->mb.i_mb_type_topleft = h->mb.type[h->mb.i_mb_topleft_xy];
813 if( h->mb.slice_table[h->mb.i_mb_topleft_xy] == h->sh.i_first_mb )
815 h->mb.i_neighbour |= MB_TOPLEFT;
817 if( !h->param.b_constrained_intra || IS_INTRA( h->mb.i_mb_type_topleft ) )
818 h->mb.i_neighbour_intra |= MB_TOPLEFT;
822 if( mb_x < h->mb.i_mb_width - 1 && topright_y >= 0 )
824 h->mb.i_neighbour_frame |= MB_TOPRIGHT;
825 h->mb.i_mb_topright_xy = h->mb.i_mb_stride*topright_y + mb_x + 1;
826 h->mb.i_mb_topright_y = topright_y;
827 h->mb.i_mb_type_topright = h->mb.type[h->mb.i_mb_topright_xy];
828 if( h->mb.slice_table[h->mb.i_mb_topright_xy] == h->sh.i_first_mb )
830 h->mb.i_neighbour |= MB_TOPRIGHT;
832 if( !h->param.b_constrained_intra || IS_INTRA( h->mb.i_mb_type_topright ) )
833 h->mb.i_neighbour_intra |= MB_TOPRIGHT;
846 static void ALWAYS_INLINE x264_macroblock_cache_load( x264_t *h, int mb_x, int mb_y, int b_mbaff )
848 x264_macroblock_cache_load_neighbours( h, mb_x, mb_y, b_mbaff );
850 int *left = h->mb.i_mb_left_xy;
851 int top = h->mb.i_mb_top_xy;
852 int top_y = h->mb.i_mb_top_y;
853 int s8x8 = h->mb.i_b8_stride;
854 int s4x4 = h->mb.i_b4_stride;
855 int top_8x8 = (2*top_y+1) * s8x8 + 2*mb_x;
856 int top_4x4 = (4*top_y+3) * s4x4 + 4*mb_x;
857 int lists = (1 << h->sh.i_type) & 3;
859 /* GCC pessimizes direct loads from heap-allocated arrays due to aliasing. */
860 /* By only dereferencing them once, we avoid this issue. */
861 int8_t (*i4x4)[8] = h->mb.intra4x4_pred_mode;
862 uint8_t (*nnz)[48] = h->mb.non_zero_count;
863 int16_t *cbp = h->mb.cbp;
865 const x264_left_table_t *left_index_table = h->mb.left_index_table;
868 if( h->mb.i_neighbour & MB_TOP )
870 h->mb.cache.i_cbp_top = cbp[top];
872 CP32( &h->mb.cache.intra4x4_pred_mode[x264_scan8[0] - 8], &i4x4[top][0] );
874 /* load non_zero_count */
875 CP32( &h->mb.cache.non_zero_count[x264_scan8[ 0] - 8], &nnz[top][12] );
876 CP32( &h->mb.cache.non_zero_count[x264_scan8[16] - 8], &nnz[top][16-4 + (16>>CHROMA_V_SHIFT)] );
877 CP32( &h->mb.cache.non_zero_count[x264_scan8[32] - 8], &nnz[top][32-4 + (16>>CHROMA_V_SHIFT)] );
879 /* Finish the prefetching */
880 for( int l = 0; l < lists; l++ )
882 x264_prefetch( &h->mb.mv[l][top_4x4-1] );
883 /* Top right being not in the same cacheline as top left will happen
884 * once every 4 MBs, so one extra prefetch is worthwhile */
885 x264_prefetch( &h->mb.mv[l][top_4x4+4] );
886 x264_prefetch( &h->mb.ref[l][top_8x8-1] );
887 x264_prefetch( &h->mb.mvd[l][top] );
892 h->mb.cache.i_cbp_top = -1;
895 M32( &h->mb.cache.intra4x4_pred_mode[x264_scan8[0] - 8] ) = 0xFFFFFFFFU;
897 /* load non_zero_count */
898 M32( &h->mb.cache.non_zero_count[x264_scan8[ 0] - 8] ) = 0x80808080U;
899 M32( &h->mb.cache.non_zero_count[x264_scan8[16] - 8] ) = 0x80808080U;
900 M32( &h->mb.cache.non_zero_count[x264_scan8[32] - 8] ) = 0x80808080U;
903 if( h->mb.i_neighbour & MB_LEFT )
905 int ltop = left[LTOP];
906 int lbot = b_mbaff ? left[LBOT] : ltop;
909 const int16_t top_luma = (cbp[ltop] >> (left_index_table->mv[0]&(~1))) & 2;
910 const int16_t bot_luma = (cbp[lbot] >> (left_index_table->mv[2]&(~1))) & 2;
911 h->mb.cache.i_cbp_left = (cbp[ltop] & 0xfff0) | (bot_luma<<2) | top_luma;
914 h->mb.cache.i_cbp_left = cbp[ltop];
917 h->mb.cache.intra4x4_pred_mode[x264_scan8[ 0] - 1] = i4x4[ltop][left_index_table->intra[0]];
918 h->mb.cache.intra4x4_pred_mode[x264_scan8[ 2] - 1] = i4x4[ltop][left_index_table->intra[1]];
919 h->mb.cache.intra4x4_pred_mode[x264_scan8[ 8] - 1] = i4x4[lbot][left_index_table->intra[2]];
920 h->mb.cache.intra4x4_pred_mode[x264_scan8[10] - 1] = i4x4[lbot][left_index_table->intra[3]];
922 /* load non_zero_count */
923 h->mb.cache.non_zero_count[x264_scan8[ 0] - 1] = nnz[ltop][left_index_table->nnz[0]];
924 h->mb.cache.non_zero_count[x264_scan8[ 2] - 1] = nnz[ltop][left_index_table->nnz[1]];
925 h->mb.cache.non_zero_count[x264_scan8[ 8] - 1] = nnz[lbot][left_index_table->nnz[2]];
926 h->mb.cache.non_zero_count[x264_scan8[10] - 1] = nnz[lbot][left_index_table->nnz[3]];
928 if( CHROMA_FORMAT >= CHROMA_422 )
930 int offset = (4>>CHROMA_H_SHIFT) - 4;
931 h->mb.cache.non_zero_count[x264_scan8[16+ 0] - 1] = nnz[ltop][left_index_table->nnz[0]+16+offset];
932 h->mb.cache.non_zero_count[x264_scan8[16+ 2] - 1] = nnz[ltop][left_index_table->nnz[1]+16+offset];
933 h->mb.cache.non_zero_count[x264_scan8[16+ 8] - 1] = nnz[lbot][left_index_table->nnz[2]+16+offset];
934 h->mb.cache.non_zero_count[x264_scan8[16+10] - 1] = nnz[lbot][left_index_table->nnz[3]+16+offset];
935 h->mb.cache.non_zero_count[x264_scan8[32+ 0] - 1] = nnz[ltop][left_index_table->nnz[0]+32+offset];
936 h->mb.cache.non_zero_count[x264_scan8[32+ 2] - 1] = nnz[ltop][left_index_table->nnz[1]+32+offset];
937 h->mb.cache.non_zero_count[x264_scan8[32+ 8] - 1] = nnz[lbot][left_index_table->nnz[2]+32+offset];
938 h->mb.cache.non_zero_count[x264_scan8[32+10] - 1] = nnz[lbot][left_index_table->nnz[3]+32+offset];
942 h->mb.cache.non_zero_count[x264_scan8[16+ 0] - 1] = nnz[ltop][left_index_table->nnz_chroma[0]];
943 h->mb.cache.non_zero_count[x264_scan8[16+ 2] - 1] = nnz[lbot][left_index_table->nnz_chroma[1]];
944 h->mb.cache.non_zero_count[x264_scan8[32+ 0] - 1] = nnz[ltop][left_index_table->nnz_chroma[2]];
945 h->mb.cache.non_zero_count[x264_scan8[32+ 2] - 1] = nnz[lbot][left_index_table->nnz_chroma[3]];
950 h->mb.cache.i_cbp_left = -1;
952 h->mb.cache.intra4x4_pred_mode[x264_scan8[ 0] - 1] =
953 h->mb.cache.intra4x4_pred_mode[x264_scan8[ 2] - 1] =
954 h->mb.cache.intra4x4_pred_mode[x264_scan8[ 8] - 1] =
955 h->mb.cache.intra4x4_pred_mode[x264_scan8[10] - 1] = -1;
957 /* load non_zero_count */
958 h->mb.cache.non_zero_count[x264_scan8[ 0] - 1] =
959 h->mb.cache.non_zero_count[x264_scan8[ 2] - 1] =
960 h->mb.cache.non_zero_count[x264_scan8[ 8] - 1] =
961 h->mb.cache.non_zero_count[x264_scan8[10] - 1] =
962 h->mb.cache.non_zero_count[x264_scan8[16+ 0] - 1] =
963 h->mb.cache.non_zero_count[x264_scan8[16+ 2] - 1] =
964 h->mb.cache.non_zero_count[x264_scan8[32+ 0] - 1] =
965 h->mb.cache.non_zero_count[x264_scan8[32+ 2] - 1] = 0x80;
966 if( CHROMA_FORMAT >= CHROMA_422 )
968 h->mb.cache.non_zero_count[x264_scan8[16+ 8] - 1] =
969 h->mb.cache.non_zero_count[x264_scan8[16+10] - 1] =
970 h->mb.cache.non_zero_count[x264_scan8[32+ 8] - 1] =
971 h->mb.cache.non_zero_count[x264_scan8[32+10] - 1] = 0x80;
975 if( h->pps->b_transform_8x8_mode )
977 h->mb.cache.i_neighbour_transform_size =
978 ( (h->mb.i_neighbour & MB_LEFT) && h->mb.mb_transform_size[left[0]] )
979 + ( (h->mb.i_neighbour & MB_TOP) && h->mb.mb_transform_size[top] );
984 h->mb.pic.i_fref[0] = h->i_ref[0] << MB_INTERLACED;
985 h->mb.pic.i_fref[1] = h->i_ref[1] << MB_INTERLACED;
990 x264_copy_column8( h->mb.pic.p_fdec[0]-1+ 4*FDEC_STRIDE, h->mb.pic.p_fdec[0]+15+ 4*FDEC_STRIDE );
991 x264_copy_column8( h->mb.pic.p_fdec[0]-1+12*FDEC_STRIDE, h->mb.pic.p_fdec[0]+15+12*FDEC_STRIDE );
992 x264_macroblock_load_pic_pointers( h, mb_x, mb_y, 0, 0, 0 );
995 x264_copy_column8( h->mb.pic.p_fdec[1]-1+ 4*FDEC_STRIDE, h->mb.pic.p_fdec[1]+15+ 4*FDEC_STRIDE );
996 x264_copy_column8( h->mb.pic.p_fdec[1]-1+12*FDEC_STRIDE, h->mb.pic.p_fdec[1]+15+12*FDEC_STRIDE );
997 x264_copy_column8( h->mb.pic.p_fdec[2]-1+ 4*FDEC_STRIDE, h->mb.pic.p_fdec[2]+15+ 4*FDEC_STRIDE );
998 x264_copy_column8( h->mb.pic.p_fdec[2]-1+12*FDEC_STRIDE, h->mb.pic.p_fdec[2]+15+12*FDEC_STRIDE );
999 x264_macroblock_load_pic_pointers( h, mb_x, mb_y, 1, 0, 0 );
1000 x264_macroblock_load_pic_pointers( h, mb_x, mb_y, 2, 0, 0 );
1004 x264_copy_column8( h->mb.pic.p_fdec[1]-1+ 4*FDEC_STRIDE, h->mb.pic.p_fdec[1]+ 7+ 4*FDEC_STRIDE );
1005 x264_copy_column8( h->mb.pic.p_fdec[2]-1+ 4*FDEC_STRIDE, h->mb.pic.p_fdec[2]+ 7+ 4*FDEC_STRIDE );
1006 if( CHROMA_FORMAT == CHROMA_422 )
1008 x264_copy_column8( h->mb.pic.p_fdec[1]-1+12*FDEC_STRIDE, h->mb.pic.p_fdec[1]+ 7+12*FDEC_STRIDE );
1009 x264_copy_column8( h->mb.pic.p_fdec[2]-1+12*FDEC_STRIDE, h->mb.pic.p_fdec[2]+ 7+12*FDEC_STRIDE );
1011 x264_macroblock_load_pic_pointers( h, mb_x, mb_y, 1, 1, 0 );
1016 x264_macroblock_load_pic_pointers( h, mb_x, mb_y, 0, 0, 1 );
1019 x264_macroblock_load_pic_pointers( h, mb_x, mb_y, 1, 0, 1 );
1020 x264_macroblock_load_pic_pointers( h, mb_x, mb_y, 2, 0, 1 );
1023 x264_macroblock_load_pic_pointers( h, mb_x, mb_y, 1, 1, 1 );
1026 if( h->fdec->integral )
1028 int offset = 16 * (mb_x + mb_y * h->fdec->i_stride[0]);
1029 for( int list = 0; list < 2; list++ )
1030 for( int i = 0; i < h->mb.pic.i_fref[list]; i++ )
1031 h->mb.pic.p_integral[list][i] = &h->fref[list][i]->integral[offset];
1034 x264_prefetch_fenc( h, h->fenc, mb_x, mb_y );
1036 /* load ref/mv/mvd */
1037 for( int l = 0; l < lists; l++ )
1039 int16_t (*mv)[2] = h->mb.mv[l];
1040 int8_t *ref = h->mb.ref[l];
1042 int i8 = x264_scan8[0] - 1 - 1*8;
1043 if( h->mb.i_neighbour & MB_TOPLEFT )
1045 int ir = b_mbaff ? 2*(s8x8*h->mb.i_mb_topleft_y + mb_x-1)+1+s8x8 : top_8x8 - 1;
1046 int iv = b_mbaff ? 4*(s4x4*h->mb.i_mb_topleft_y + mb_x-1)+3+3*s4x4 : top_4x4 - 1;
1047 if( b_mbaff && h->mb.topleft_partition )
1049 /* Take motion vector from the middle of macroblock instead of
1050 * the bottom right as usual. */
1054 h->mb.cache.ref[l][i8] = ref[ir];
1055 CP32( h->mb.cache.mv[l][i8], mv[iv] );
1059 h->mb.cache.ref[l][i8] = -2;
1060 M32( h->mb.cache.mv[l][i8] ) = 0;
1063 i8 = x264_scan8[0] - 8;
1064 if( h->mb.i_neighbour & MB_TOP )
1066 h->mb.cache.ref[l][i8+0] =
1067 h->mb.cache.ref[l][i8+1] = ref[top_8x8 + 0];
1068 h->mb.cache.ref[l][i8+2] =
1069 h->mb.cache.ref[l][i8+3] = ref[top_8x8 + 1];
1070 CP128( h->mb.cache.mv[l][i8], mv[top_4x4] );
1074 M128( h->mb.cache.mv[l][i8] ) = M128_ZERO;
1075 M32( &h->mb.cache.ref[l][i8] ) = (uint8_t)(-2) * 0x01010101U;
1078 i8 = x264_scan8[0] + 4 - 1*8;
1079 if( h->mb.i_neighbour & MB_TOPRIGHT )
1081 int ir = b_mbaff ? 2*(s8x8*h->mb.i_mb_topright_y + (mb_x+1))+s8x8 : top_8x8 + 2;
1082 int iv = b_mbaff ? 4*(s4x4*h->mb.i_mb_topright_y + (mb_x+1))+3*s4x4 : top_4x4 + 4;
1083 h->mb.cache.ref[l][i8] = ref[ir];
1084 CP32( h->mb.cache.mv[l][i8], mv[iv] );
1087 h->mb.cache.ref[l][i8] = -2;
1089 i8 = x264_scan8[0] - 1;
1090 if( h->mb.i_neighbour & MB_LEFT )
1094 h->mb.cache.ref[l][i8+0*8] = ref[h->mb.left_b8[LTOP] + 1 + s8x8*left_index_table->ref[0]];
1095 h->mb.cache.ref[l][i8+1*8] = ref[h->mb.left_b8[LTOP] + 1 + s8x8*left_index_table->ref[1]];
1096 h->mb.cache.ref[l][i8+2*8] = ref[h->mb.left_b8[LBOT] + 1 + s8x8*left_index_table->ref[2]];
1097 h->mb.cache.ref[l][i8+3*8] = ref[h->mb.left_b8[LBOT] + 1 + s8x8*left_index_table->ref[3]];
1099 CP32( h->mb.cache.mv[l][i8+0*8], mv[h->mb.left_b4[LTOP] + 3 + s4x4*left_index_table->mv[0]] );
1100 CP32( h->mb.cache.mv[l][i8+1*8], mv[h->mb.left_b4[LTOP] + 3 + s4x4*left_index_table->mv[1]] );
1101 CP32( h->mb.cache.mv[l][i8+2*8], mv[h->mb.left_b4[LBOT] + 3 + s4x4*left_index_table->mv[2]] );
1102 CP32( h->mb.cache.mv[l][i8+3*8], mv[h->mb.left_b4[LBOT] + 3 + s4x4*left_index_table->mv[3]] );
1106 const int ir = h->mb.i_b8_xy - 1;
1107 const int iv = h->mb.i_b4_xy - 1;
1108 h->mb.cache.ref[l][i8+0*8] =
1109 h->mb.cache.ref[l][i8+1*8] = ref[ir + 0*s8x8];
1110 h->mb.cache.ref[l][i8+2*8] =
1111 h->mb.cache.ref[l][i8+3*8] = ref[ir + 1*s8x8];
1113 CP32( h->mb.cache.mv[l][i8+0*8], mv[iv + 0*s4x4] );
1114 CP32( h->mb.cache.mv[l][i8+1*8], mv[iv + 1*s4x4] );
1115 CP32( h->mb.cache.mv[l][i8+2*8], mv[iv + 2*s4x4] );
1116 CP32( h->mb.cache.mv[l][i8+3*8], mv[iv + 3*s4x4] );
1121 for( int i = 0; i < 4; i++ )
1123 h->mb.cache.ref[l][i8+i*8] = -2;
1124 M32( h->mb.cache.mv[l][i8+i*8] ) = 0;
1128 /* Extra logic for top right mv in mbaff.
1134 * If the top right of the 4x4 partitions labeled a, b and c in the
1135 * above diagram do not exist, but the entries d, e and f exist (in
1136 * the macroblock to the left) then use those instead.
1138 if( b_mbaff && (h->mb.i_neighbour & MB_LEFT) )
1140 if( MB_INTERLACED && !h->mb.field[h->mb.i_mb_xy-1] )
1142 h->mb.cache.topright_ref[l][0] = ref[h->mb.left_b8[0] + 1 + s8x8*0];
1143 h->mb.cache.topright_ref[l][1] = ref[h->mb.left_b8[0] + 1 + s8x8*1];
1144 h->mb.cache.topright_ref[l][2] = ref[h->mb.left_b8[1] + 1 + s8x8*0];
1145 CP32( h->mb.cache.topright_mv[l][0], mv[h->mb.left_b4[0] + 3 + s4x4*(left_index_table->mv[0]+1)] );
1146 CP32( h->mb.cache.topright_mv[l][1], mv[h->mb.left_b4[0] + 3 + s4x4*(left_index_table->mv[1]+1)] );
1147 CP32( h->mb.cache.topright_mv[l][2], mv[h->mb.left_b4[1] + 3 + s4x4*(left_index_table->mv[2]+1)] );
1149 else if( !MB_INTERLACED && h->mb.field[h->mb.i_mb_xy-1] )
1151 // Looking at the bottom field so always take the bottom macroblock of the pair.
1152 h->mb.cache.topright_ref[l][0] = ref[h->mb.left_b8[0] + 1 + s8x8*2 + s8x8*left_index_table->ref[0]];
1153 h->mb.cache.topright_ref[l][1] = ref[h->mb.left_b8[0] + 1 + s8x8*2 + s8x8*left_index_table->ref[0]];
1154 h->mb.cache.topright_ref[l][2] = ref[h->mb.left_b8[0] + 1 + s8x8*2 + s8x8*left_index_table->ref[2]];
1155 CP32( h->mb.cache.topright_mv[l][0], mv[h->mb.left_b4[0] + 3 + s4x4*4 + s4x4*left_index_table->mv[0]] );
1156 CP32( h->mb.cache.topright_mv[l][1], mv[h->mb.left_b4[0] + 3 + s4x4*4 + s4x4*left_index_table->mv[1]] );
1157 CP32( h->mb.cache.topright_mv[l][2], mv[h->mb.left_b4[0] + 3 + s4x4*4 + s4x4*left_index_table->mv[2]] );
1161 if( h->param.b_cabac )
1163 uint8_t (*mvd)[8][2] = h->mb.mvd[l];
1164 if( h->mb.i_neighbour & MB_TOP )
1165 CP64( h->mb.cache.mvd[l][x264_scan8[0] - 8], mvd[top][0] );
1167 M64( h->mb.cache.mvd[l][x264_scan8[0] - 8] ) = 0;
1169 if( h->mb.i_neighbour & MB_LEFT && (!b_mbaff || h->mb.cache.ref[l][x264_scan8[0]-1] >= 0) )
1171 CP16( h->mb.cache.mvd[l][x264_scan8[0 ] - 1], mvd[left[LTOP]][left_index_table->intra[0]] );
1172 CP16( h->mb.cache.mvd[l][x264_scan8[2 ] - 1], mvd[left[LTOP]][left_index_table->intra[1]] );
1176 M16( h->mb.cache.mvd[l][x264_scan8[0]-1+0*8] ) = 0;
1177 M16( h->mb.cache.mvd[l][x264_scan8[0]-1+1*8] ) = 0;
1179 if( h->mb.i_neighbour & MB_LEFT && (!b_mbaff || h->mb.cache.ref[l][x264_scan8[0]-1+2*8] >=0) )
1181 CP16( h->mb.cache.mvd[l][x264_scan8[8 ] - 1], mvd[left[LBOT]][left_index_table->intra[2]] );
1182 CP16( h->mb.cache.mvd[l][x264_scan8[10] - 1], mvd[left[LBOT]][left_index_table->intra[3]] );
1186 M16( h->mb.cache.mvd[l][x264_scan8[0]-1+2*8] ) = 0;
1187 M16( h->mb.cache.mvd[l][x264_scan8[0]-1+3*8] ) = 0;
1191 /* If motion vectors are cached from frame macroblocks but this
1192 * macroblock is a field macroblock then the motion vector must be
1193 * halved. Similarly, motion vectors from field macroblocks are doubled. */
1197 if( FIELD_DIFFERENT(h->mb.i_mb_topleft_xy) )\
1198 MAP_F2F(mv, ref, x264_scan8[0] - 1 - 1*8)\
1199 if( FIELD_DIFFERENT(top) )\
1201 MAP_F2F(mv, ref, x264_scan8[0] + 0 - 1*8)\
1202 MAP_F2F(mv, ref, x264_scan8[0] + 1 - 1*8)\
1203 MAP_F2F(mv, ref, x264_scan8[0] + 2 - 1*8)\
1204 MAP_F2F(mv, ref, x264_scan8[0] + 3 - 1*8)\
1206 if( FIELD_DIFFERENT(h->mb.i_mb_topright_xy) )\
1207 MAP_F2F(mv, ref, x264_scan8[0] + 4 - 1*8)\
1208 if( FIELD_DIFFERENT(left[0]) )\
1210 MAP_F2F(mv, ref, x264_scan8[0] - 1 + 0*8)\
1211 MAP_F2F(mv, ref, x264_scan8[0] - 1 + 1*8)\
1212 MAP_F2F(mv, ref, x264_scan8[0] - 1 + 2*8)\
1213 MAP_F2F(mv, ref, x264_scan8[0] - 1 + 3*8)\
1214 MAP_F2F(topright_mv, topright_ref, 0)\
1215 MAP_F2F(topright_mv, topright_ref, 1)\
1216 MAP_F2F(topright_mv, topright_ref, 2)\
1221 #define FIELD_DIFFERENT(macroblock) (macroblock >= 0 && !h->mb.field[macroblock])
1222 #define MAP_F2F(varmv, varref, index)\
1223 if( h->mb.cache.varref[l][index] >= 0 )\
1225 h->mb.cache.varref[l][index] <<= 1;\
1226 h->mb.cache.varmv[l][index][1] /= 2;\
1227 h->mb.cache.mvd[l][index][1] >>= 1;\
1231 #undef FIELD_DIFFERENT
1235 #define FIELD_DIFFERENT(macroblock) (macroblock >= 0 && h->mb.field[macroblock])
1236 #define MAP_F2F(varmv, varref, index)\
1237 if( h->mb.cache.varref[l][index] >= 0 )\
1239 h->mb.cache.varref[l][index] >>= 1;\
1240 h->mb.cache.varmv[l][index][1] <<= 1;\
1241 h->mb.cache.mvd[l][index][1] <<= 1;\
1245 #undef FIELD_DIFFERENT
1250 if( b_mbaff && mb_x == 0 && !(mb_y&1) && mb_y > 0 )
1251 h->mb.field_decoding_flag = h->mb.field[h->mb.i_mb_xy - h->mb.i_mb_stride];
1253 /* Check whether skip here would cause decoder to predict interlace mode incorrectly.
1254 * FIXME: It might be better to change the interlace type rather than forcing a skip to be non-skip. */
1255 h->mb.b_allow_skip = 1;
1258 if( MB_INTERLACED != h->mb.field_decoding_flag &&
1259 h->mb.i_mb_prev_xy >= 0 && IS_SKIP(h->mb.type[h->mb.i_mb_prev_xy]) )
1260 h->mb.b_allow_skip = 0;
1261 if( (mb_y&1) && IS_SKIP(h->mb.type[h->mb.i_mb_xy - h->mb.i_mb_stride]) )
1263 if( h->mb.i_neighbour & MB_LEFT )
1265 if( h->mb.field[h->mb.i_mb_xy - 1] != MB_INTERLACED )
1266 h->mb.b_allow_skip = 0;
1268 else if( h->mb.i_neighbour & MB_TOP )
1270 if( h->mb.field[h->mb.i_mb_top_xy] != MB_INTERLACED )
1271 h->mb.b_allow_skip = 0;
1273 else // Frame mb pair is predicted
1276 h->mb.b_allow_skip = 0;
1281 if( h->param.b_cabac )
1285 int left_xy, top_xy;
1286 /* Neighbours here are calculated based on field_decoding_flag */
1287 int mb_xy = mb_x + (mb_y&~1)*h->mb.i_mb_stride;
1288 left_xy = mb_xy - 1;
1289 if( (mb_y&1) && mb_x > 0 && h->mb.field_decoding_flag == h->mb.field[left_xy] )
1290 left_xy += h->mb.i_mb_stride;
1291 if( h->mb.field_decoding_flag )
1293 top_xy = mb_xy - h->mb.i_mb_stride;
1294 if( !(mb_y&1) && top_xy >= 0 && h->mb.slice_table[top_xy] == h->sh.i_first_mb && h->mb.field[top_xy] )
1295 top_xy -= h->mb.i_mb_stride;
1298 top_xy = mb_x + (mb_y-1)*h->mb.i_mb_stride;
1300 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] ))
1301 + (top_xy >= 0 && h->mb.slice_table[top_xy] == h->sh.i_first_mb && !IS_SKIP( h->mb.type[top_xy] ));
1305 h->mb.cache.i_neighbour_skip = ((h->mb.i_neighbour & MB_LEFT) && !IS_SKIP( h->mb.i_mb_type_left[0] ))
1306 + ((h->mb.i_neighbour & MB_TOP) && !IS_SKIP( h->mb.i_mb_type_top ));
1311 if( h->sh.i_type == SLICE_TYPE_B )
1313 h->mb.bipred_weight = h->mb.bipred_weight_buf[MB_INTERLACED][MB_INTERLACED&(mb_y&1)];
1314 h->mb.dist_scale_factor = h->mb.dist_scale_factor_buf[MB_INTERLACED][MB_INTERLACED&(mb_y&1)];
1315 if( h->param.b_cabac )
1318 x264_macroblock_cache_skip( h, 0, 0, 4, 4, 0 );
1321 skipbp = (h->mb.i_neighbour & MB_LEFT) ? h->mb.skipbp[left[LTOP]] : 0;
1322 h->mb.cache.skip[x264_scan8[0] - 1] = (skipbp >> (1+(left_index_table->mv[0]&~1))) & 1;
1323 skipbp = (h->mb.i_neighbour & MB_LEFT) ? h->mb.skipbp[left[LBOT]] : 0;
1324 h->mb.cache.skip[x264_scan8[8] - 1] = (skipbp >> (1+(left_index_table->mv[2]&~1))) & 1;
1328 skipbp = (h->mb.i_neighbour & MB_LEFT) ? h->mb.skipbp[left[0]] : 0;
1329 h->mb.cache.skip[x264_scan8[0] - 1] = skipbp & 0x2;
1330 h->mb.cache.skip[x264_scan8[8] - 1] = skipbp & 0x8;
1332 skipbp = (h->mb.i_neighbour & MB_TOP) ? h->mb.skipbp[top] : 0;
1333 h->mb.cache.skip[x264_scan8[0] - 8] = skipbp & 0x4;
1334 h->mb.cache.skip[x264_scan8[4] - 8] = skipbp & 0x8;
1338 if( h->sh.i_type == SLICE_TYPE_P )
1339 x264_mb_predict_mv_pskip( h, h->mb.cache.pskip_mv );
1341 h->mb.i_neighbour4[0] =
1342 h->mb.i_neighbour8[0] = (h->mb.i_neighbour_intra & (MB_TOP|MB_LEFT|MB_TOPLEFT))
1343 | ((h->mb.i_neighbour_intra & MB_TOP) ? MB_TOPRIGHT : 0);
1344 h->mb.i_neighbour4[4] =
1345 h->mb.i_neighbour4[1] = MB_LEFT | ((h->mb.i_neighbour_intra & MB_TOP) ? (MB_TOP|MB_TOPLEFT|MB_TOPRIGHT) : 0);
1346 h->mb.i_neighbour4[2] =
1347 h->mb.i_neighbour4[8] =
1348 h->mb.i_neighbour4[10] =
1349 h->mb.i_neighbour8[2] = MB_TOP|MB_TOPRIGHT | ((h->mb.i_neighbour_intra & MB_LEFT) ? (MB_LEFT|MB_TOPLEFT) : 0);
1350 h->mb.i_neighbour4[5] =
1351 h->mb.i_neighbour8[1] = MB_LEFT | (h->mb.i_neighbour_intra & MB_TOPRIGHT)
1352 | ((h->mb.i_neighbour_intra & MB_TOP) ? MB_TOP|MB_TOPLEFT : 0);
1355 void x264_macroblock_cache_load_progressive( x264_t *h, int mb_x, int mb_y )
1357 x264_macroblock_cache_load( h, mb_x, mb_y, 0 );
1360 void x264_macroblock_cache_load_interlaced( x264_t *h, int mb_x, int mb_y )
1362 x264_macroblock_cache_load( h, mb_x, mb_y, 1 );
1365 static void x264_macroblock_deblock_strength_mbaff( x264_t *h, uint8_t (*bs)[8][4] )
1367 if( (h->mb.i_neighbour & MB_LEFT) && h->mb.field[h->mb.i_mb_left_xy[0]] != MB_INTERLACED )
1369 static const uint8_t offset[2][2][8] =
1370 { { { 0, 0, 0, 0, 1, 1, 1, 1 },
1371 { 2, 2, 2, 2, 3, 3, 3, 3 }, },
1372 { { 0, 1, 2, 3, 0, 1, 2, 3 },
1373 { 0, 1, 2, 3, 0, 1, 2, 3 }, }
1375 ALIGNED_ARRAY_8( uint8_t, tmpbs, [8] );
1377 const uint8_t *off = offset[MB_INTERLACED][h->mb.i_mb_y&1];
1378 uint8_t (*nnz)[48] = h->mb.non_zero_count;
1380 for( int i = 0; i < 8; i++ )
1382 int left = h->mb.i_mb_left_xy[MB_INTERLACED ? i>>2 : i&1];
1383 int nnz_this = h->mb.cache.non_zero_count[x264_scan8[0]+8*(i>>1)];
1384 int nnz_left = nnz[left][3 + 4*off[i]];
1385 if( !h->param.b_cabac && h->pps->b_transform_8x8_mode )
1388 if( h->mb.mb_transform_size[left] )
1389 nnz_left = !!(M16( &nnz[left][2+4*j] ) | M16( &nnz[left][2+4*(1+j)] ));
1391 tmpbs[i] = (nnz_left || nnz_this) ? 2 : 1;
1396 CP32( bs[0][0], &tmpbs[0] );
1397 CP32( bs[0][4], &tmpbs[4] );
1401 for( int i = 0; i < 4; i++ ) bs[0][0][i] = tmpbs[2*i];
1402 for( int i = 0; i < 4; i++ ) bs[0][4][i] = tmpbs[1+2*i];
1406 if( (h->mb.i_neighbour & MB_TOP) && MB_INTERLACED != h->mb.field[h->mb.i_mb_top_xy] )
1408 if( !(h->mb.i_mb_y&1) && !MB_INTERLACED )
1410 /* Need to filter both fields (even for frame macroblocks).
1411 * Filter top two rows using the top macroblock of the above
1412 * pair and then the bottom one. */
1413 int mbn_xy = h->mb.i_mb_xy - 2 * h->mb.i_mb_stride;
1414 uint8_t *nnz_cur = &h->mb.cache.non_zero_count[x264_scan8[0]];
1416 for( int j = 0; j < 2; j++, mbn_xy += h->mb.i_mb_stride )
1418 uint8_t (*nnz)[48] = h->mb.non_zero_count;
1420 ALIGNED_4( uint8_t nnz_top[4] );
1421 CP32( nnz_top, &nnz[mbn_xy][3*4] );
1423 if( !h->param.b_cabac && h->pps->b_transform_8x8_mode && h->mb.mb_transform_size[mbn_xy] )
1425 nnz_top[0] = nnz_top[1] = M16( &nnz[mbn_xy][ 8] ) || M16( &nnz[mbn_xy][12] );
1426 nnz_top[2] = nnz_top[3] = M16( &nnz[mbn_xy][10] ) || M16( &nnz[mbn_xy][14] );
1429 for( int i = 0; i < 4; i++ )
1430 bs[1][4*j][i] = (nnz_cur[i] || nnz_top[i]) ? 2 : 1;
1434 for( int i = 0; i < 4; i++ )
1435 bs[1][0][i] = X264_MAX( bs[1][0][i], 1 );
1439 void x264_macroblock_deblock_strength( x264_t *h )
1441 uint8_t (*bs)[8][4] = h->deblock_strength[h->mb.i_mb_y&1][h->mb.i_mb_x];
1442 if( IS_INTRA( h->mb.i_type ) )
1444 memset( bs[0][1], 3, 3*4*sizeof(uint8_t) );
1445 memset( bs[1][1], 3, 3*4*sizeof(uint8_t) );
1449 /* Early termination: in this case, nnz guarantees all edges use strength 2.*/
1450 if( h->mb.b_transform_8x8 && !CHROMA444 )
1452 int cbp_mask = 0xf >> CHROMA_V_SHIFT;
1453 if( (h->mb.i_cbp_luma&cbp_mask) == cbp_mask )
1455 M32( bs[0][0] ) = 0x02020202;
1456 M32( bs[0][2] ) = 0x02020202;
1457 M32( bs[0][4] ) = 0x02020202;
1458 memset( bs[1][0], 2, 5*4*sizeof(uint8_t) ); /* [1][1] and [1][3] has to be set for 4:2:2 */
1463 int neighbour_changed = 0;
1464 if( h->sh.i_disable_deblocking_filter_idc != 2 )
1466 neighbour_changed = h->mb.i_neighbour_frame&~h->mb.i_neighbour;
1467 h->mb.i_neighbour = h->mb.i_neighbour_frame;
1470 /* MBAFF deblock uses different left neighbors from encoding */
1471 if( SLICE_MBAFF && (h->mb.i_neighbour & MB_LEFT) && (h->mb.field[h->mb.i_mb_xy - 1] != MB_INTERLACED) )
1473 h->mb.i_mb_left_xy[1] =
1474 h->mb.i_mb_left_xy[0] = h->mb.i_mb_xy - 1;
1475 if( h->mb.i_mb_y&1 )
1476 h->mb.i_mb_left_xy[0] -= h->mb.i_mb_stride;
1478 h->mb.i_mb_left_xy[1] += h->mb.i_mb_stride;
1481 /* If we have multiple slices and we're deblocking on slice edges, we
1482 * have to reload neighbour data. */
1483 if( neighbour_changed )
1485 int top_y = h->mb.i_mb_top_y;
1486 int top_8x8 = (2*top_y+1) * h->mb.i_b8_stride + 2*h->mb.i_mb_x;
1487 int top_4x4 = (4*top_y+3) * h->mb.i_b4_stride + 4*h->mb.i_mb_x;
1488 int s8x8 = h->mb.i_b8_stride;
1489 int s4x4 = h->mb.i_b4_stride;
1491 uint8_t (*nnz)[48] = h->mb.non_zero_count;
1492 const x264_left_table_t *left_index_table = SLICE_MBAFF ? h->mb.left_index_table : &left_indices[3];
1494 if( neighbour_changed & MB_TOP )
1495 CP32( &h->mb.cache.non_zero_count[x264_scan8[0] - 8], &nnz[h->mb.i_mb_top_xy][12] );
1497 if( neighbour_changed & MB_LEFT )
1499 int *left = h->mb.i_mb_left_xy;
1500 h->mb.cache.non_zero_count[x264_scan8[0 ] - 1] = nnz[left[0]][left_index_table->nnz[0]];
1501 h->mb.cache.non_zero_count[x264_scan8[2 ] - 1] = nnz[left[0]][left_index_table->nnz[1]];
1502 h->mb.cache.non_zero_count[x264_scan8[8 ] - 1] = nnz[left[1]][left_index_table->nnz[2]];
1503 h->mb.cache.non_zero_count[x264_scan8[10] - 1] = nnz[left[1]][left_index_table->nnz[3]];
1506 for( int l = 0; l <= (h->sh.i_type == SLICE_TYPE_B); l++ )
1508 int16_t (*mv)[2] = h->mb.mv[l];
1509 int8_t *ref = h->mb.ref[l];
1511 int i8 = x264_scan8[0] - 8;
1512 if( neighbour_changed & MB_TOP )
1514 h->mb.cache.ref[l][i8+0] =
1515 h->mb.cache.ref[l][i8+1] = ref[top_8x8 + 0];
1516 h->mb.cache.ref[l][i8+2] =
1517 h->mb.cache.ref[l][i8+3] = ref[top_8x8 + 1];
1518 CP128( h->mb.cache.mv[l][i8], mv[top_4x4] );
1521 i8 = x264_scan8[0] - 1;
1522 if( neighbour_changed & MB_LEFT )
1524 h->mb.cache.ref[l][i8+0*8] =
1525 h->mb.cache.ref[l][i8+1*8] = ref[h->mb.left_b8[0] + 1 + s8x8*left_index_table->ref[0]];
1526 h->mb.cache.ref[l][i8+2*8] =
1527 h->mb.cache.ref[l][i8+3*8] = ref[h->mb.left_b8[1] + 1 + s8x8*left_index_table->ref[2]];
1529 CP32( h->mb.cache.mv[l][i8+0*8], mv[h->mb.left_b4[0] + 3 + s4x4*left_index_table->mv[0]] );
1530 CP32( h->mb.cache.mv[l][i8+1*8], mv[h->mb.left_b4[0] + 3 + s4x4*left_index_table->mv[1]] );
1531 CP32( h->mb.cache.mv[l][i8+2*8], mv[h->mb.left_b4[1] + 3 + s4x4*left_index_table->mv[2]] );
1532 CP32( h->mb.cache.mv[l][i8+3*8], mv[h->mb.left_b4[1] + 3 + s4x4*left_index_table->mv[3]] );
1537 if( h->param.analyse.i_weighted_pred == X264_WEIGHTP_SMART && h->sh.i_type == SLICE_TYPE_P )
1539 /* Handle reference frame duplicates */
1540 int i8 = x264_scan8[0] - 8;
1541 h->mb.cache.ref[0][i8+0] =
1542 h->mb.cache.ref[0][i8+1] = deblock_ref_table(h->mb.cache.ref[0][i8+0]);
1543 h->mb.cache.ref[0][i8+2] =
1544 h->mb.cache.ref[0][i8+3] = deblock_ref_table(h->mb.cache.ref[0][i8+2]);
1546 i8 = x264_scan8[0] - 1;
1547 h->mb.cache.ref[0][i8+0*8] =
1548 h->mb.cache.ref[0][i8+1*8] = deblock_ref_table(h->mb.cache.ref[0][i8+0*8]);
1549 h->mb.cache.ref[0][i8+2*8] =
1550 h->mb.cache.ref[0][i8+3*8] = deblock_ref_table(h->mb.cache.ref[0][i8+2*8]);
1552 int ref0 = deblock_ref_table(h->mb.cache.ref[0][x264_scan8[ 0]]);
1553 int ref1 = deblock_ref_table(h->mb.cache.ref[0][x264_scan8[ 4]]);
1554 int ref2 = deblock_ref_table(h->mb.cache.ref[0][x264_scan8[ 8]]);
1555 int ref3 = deblock_ref_table(h->mb.cache.ref[0][x264_scan8[12]]);
1556 uint32_t reftop = pack16to32( (uint8_t)ref0, (uint8_t)ref1 ) * 0x0101;
1557 uint32_t refbot = pack16to32( (uint8_t)ref2, (uint8_t)ref3 ) * 0x0101;
1559 M32( &h->mb.cache.ref[0][x264_scan8[0]+8*0] ) = reftop;
1560 M32( &h->mb.cache.ref[0][x264_scan8[0]+8*1] ) = reftop;
1561 M32( &h->mb.cache.ref[0][x264_scan8[0]+8*2] ) = refbot;
1562 M32( &h->mb.cache.ref[0][x264_scan8[0]+8*3] ) = refbot;
1565 /* Munge NNZ for cavlc + 8x8dct */
1566 if( !h->param.b_cabac && h->pps->b_transform_8x8_mode )
1568 uint8_t (*nnz)[48] = h->mb.non_zero_count;
1569 int top = h->mb.i_mb_top_xy;
1570 int *left = h->mb.i_mb_left_xy;
1572 if( (h->mb.i_neighbour & MB_TOP) && h->mb.mb_transform_size[top] )
1574 int i8 = x264_scan8[0] - 8;
1575 int nnz_top0 = M16( &nnz[top][8] ) | M16( &nnz[top][12] );
1576 int nnz_top1 = M16( &nnz[top][10] ) | M16( &nnz[top][14] );
1577 M16( &h->mb.cache.non_zero_count[i8+0] ) = nnz_top0 ? 0x0101 : 0;
1578 M16( &h->mb.cache.non_zero_count[i8+2] ) = nnz_top1 ? 0x0101 : 0;
1581 if( h->mb.i_neighbour & MB_LEFT )
1583 int i8 = x264_scan8[0] - 1;
1584 if( h->mb.mb_transform_size[left[0]] )
1586 int nnz_left0 = M16( &nnz[left[0]][2] ) | M16( &nnz[left[0]][6] );
1587 h->mb.cache.non_zero_count[i8+8*0] = !!nnz_left0;
1588 h->mb.cache.non_zero_count[i8+8*1] = !!nnz_left0;
1590 if( h->mb.mb_transform_size[left[1]] )
1592 int nnz_left1 = M16( &nnz[left[1]][10] ) | M16( &nnz[left[1]][14] );
1593 h->mb.cache.non_zero_count[i8+8*2] = !!nnz_left1;
1594 h->mb.cache.non_zero_count[i8+8*3] = !!nnz_left1;
1598 if( h->mb.b_transform_8x8 )
1600 int nnz0 = M16( &h->mb.cache.non_zero_count[x264_scan8[ 0]] ) | M16( &h->mb.cache.non_zero_count[x264_scan8[ 2]] );
1601 int nnz1 = M16( &h->mb.cache.non_zero_count[x264_scan8[ 4]] ) | M16( &h->mb.cache.non_zero_count[x264_scan8[ 6]] );
1602 int nnz2 = M16( &h->mb.cache.non_zero_count[x264_scan8[ 8]] ) | M16( &h->mb.cache.non_zero_count[x264_scan8[10]] );
1603 int nnz3 = M16( &h->mb.cache.non_zero_count[x264_scan8[12]] ) | M16( &h->mb.cache.non_zero_count[x264_scan8[14]] );
1604 uint32_t nnztop = pack16to32( !!nnz0, !!nnz1 ) * 0x0101;
1605 uint32_t nnzbot = pack16to32( !!nnz2, !!nnz3 ) * 0x0101;
1607 M32( &h->mb.cache.non_zero_count[x264_scan8[0]+8*0] ) = nnztop;
1608 M32( &h->mb.cache.non_zero_count[x264_scan8[0]+8*1] ) = nnztop;
1609 M32( &h->mb.cache.non_zero_count[x264_scan8[0]+8*2] ) = nnzbot;
1610 M32( &h->mb.cache.non_zero_count[x264_scan8[0]+8*3] ) = nnzbot;
1614 h->loopf.deblock_strength( h->mb.cache.non_zero_count, h->mb.cache.ref, h->mb.cache.mv,
1615 bs, 4 >> MB_INTERLACED, h->sh.i_type == SLICE_TYPE_B );
1618 x264_macroblock_deblock_strength_mbaff( h, bs );
1621 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 )
1623 int height = b_chroma ? 16>>CHROMA_V_SHIFT : 16;
1624 int i_stride = h->fdec->i_stride[i];
1625 int i_stride2 = i_stride << (b_mbaff && MB_INTERLACED);
1626 int i_pix_offset = (b_mbaff && MB_INTERLACED)
1627 ? 16 * mb_x + height * (mb_y&~1) * i_stride + (mb_y&1) * i_stride
1628 : 16 * mb_x + height * mb_y * i_stride;
1630 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 );
1632 h->mc.copy[PIXEL_16x16]( &h->fdec->plane[i][i_pix_offset], i_stride2, h->mb.pic.p_fdec[i], FDEC_STRIDE, 16 );
1635 static void ALWAYS_INLINE x264_macroblock_backup_intra( x264_t *h, int mb_x, int mb_y, int b_mbaff )
1637 /* In MBAFF we store the last two rows in intra_border_backup[0] and [1].
1638 * For progressive mbs this is the bottom two rows, and for interlaced the
1639 * bottom row of each field. We also store samples needed for the next
1640 * mbpair in intra_border_backup[2]. */
1641 int backup_dst = !b_mbaff ? 0 : (mb_y&1) ? 1 : MB_INTERLACED ? 0 : 2;
1642 memcpy( &h->intra_border_backup[backup_dst][0][mb_x*16 ], h->mb.pic.p_fdec[0]+FDEC_STRIDE*15, 16*sizeof(pixel) );
1645 memcpy( &h->intra_border_backup[backup_dst][1][mb_x*16 ], h->mb.pic.p_fdec[1]+FDEC_STRIDE*15, 16*sizeof(pixel) );
1646 memcpy( &h->intra_border_backup[backup_dst][2][mb_x*16 ], h->mb.pic.p_fdec[2]+FDEC_STRIDE*15, 16*sizeof(pixel) );
1650 int backup_src = (15>>CHROMA_V_SHIFT) * FDEC_STRIDE;
1651 memcpy( &h->intra_border_backup[backup_dst][1][mb_x*16 ], h->mb.pic.p_fdec[1]+backup_src, 8*sizeof(pixel) );
1652 memcpy( &h->intra_border_backup[backup_dst][1][mb_x*16+8], h->mb.pic.p_fdec[2]+backup_src, 8*sizeof(pixel) );
1658 int backup_src = (MB_INTERLACED ? 7 : 14) * FDEC_STRIDE;
1659 backup_dst = MB_INTERLACED ? 2 : 0;
1660 memcpy( &h->intra_border_backup[backup_dst][0][mb_x*16 ], h->mb.pic.p_fdec[0]+backup_src, 16*sizeof(pixel) );
1663 memcpy( &h->intra_border_backup[backup_dst][1][mb_x*16 ], h->mb.pic.p_fdec[1]+backup_src, 16*sizeof(pixel) );
1664 memcpy( &h->intra_border_backup[backup_dst][2][mb_x*16 ], h->mb.pic.p_fdec[2]+backup_src, 16*sizeof(pixel) );
1668 if( CHROMA_FORMAT == CHROMA_420 )
1669 backup_src = (MB_INTERLACED ? 3 : 6) * FDEC_STRIDE;
1670 memcpy( &h->intra_border_backup[backup_dst][1][mb_x*16 ], h->mb.pic.p_fdec[1]+backup_src, 8*sizeof(pixel) );
1671 memcpy( &h->intra_border_backup[backup_dst][1][mb_x*16+8], h->mb.pic.p_fdec[2]+backup_src, 8*sizeof(pixel) );
1677 /* In progressive we update intra_border_backup in-place, so the topleft neighbor will
1678 * no longer exist there when load_pic_pointers wants it. Move it within p_fdec instead. */
1679 h->mb.pic.p_fdec[0][-FDEC_STRIDE-1] = h->mb.pic.p_fdec[0][-FDEC_STRIDE+15];
1680 h->mb.pic.p_fdec[1][-FDEC_STRIDE-1] = h->mb.pic.p_fdec[1][-FDEC_STRIDE+(15>>CHROMA_H_SHIFT)];
1681 h->mb.pic.p_fdec[2][-FDEC_STRIDE-1] = h->mb.pic.p_fdec[2][-FDEC_STRIDE+(15>>CHROMA_H_SHIFT)];
1685 void x264_macroblock_cache_save( x264_t *h )
1687 const int i_mb_xy = h->mb.i_mb_xy;
1688 const int i_mb_type = x264_mb_type_fix[h->mb.i_type];
1689 const int s8x8 = h->mb.i_b8_stride;
1690 const int s4x4 = h->mb.i_b4_stride;
1691 const int i_mb_4x4 = h->mb.i_b4_xy;
1692 const int i_mb_8x8 = h->mb.i_b8_xy;
1694 /* GCC pessimizes direct stores to heap-allocated arrays due to aliasing. */
1695 /* By only dereferencing them once, we avoid this issue. */
1696 int8_t *i4x4 = h->mb.intra4x4_pred_mode[i_mb_xy];
1697 uint8_t *nnz = h->mb.non_zero_count[i_mb_xy];
1701 x264_macroblock_backup_intra( h, h->mb.i_mb_x, h->mb.i_mb_y, 1 );
1702 x264_macroblock_store_pic( h, h->mb.i_mb_x, h->mb.i_mb_y, 0, 0, 1 );
1705 x264_macroblock_store_pic( h, h->mb.i_mb_x, h->mb.i_mb_y, 1, 0, 1 );
1706 x264_macroblock_store_pic( h, h->mb.i_mb_x, h->mb.i_mb_y, 2, 0, 1 );
1709 x264_macroblock_store_pic( h, h->mb.i_mb_x, h->mb.i_mb_y, 1, 1, 1 );
1713 x264_macroblock_backup_intra( h, h->mb.i_mb_x, h->mb.i_mb_y, 0 );
1714 x264_macroblock_store_pic( h, h->mb.i_mb_x, h->mb.i_mb_y, 0, 0, 0 );
1717 x264_macroblock_store_pic( h, h->mb.i_mb_x, h->mb.i_mb_y, 1, 0, 0 );
1718 x264_macroblock_store_pic( h, h->mb.i_mb_x, h->mb.i_mb_y, 2, 0, 0 );
1721 x264_macroblock_store_pic( h, h->mb.i_mb_x, h->mb.i_mb_y, 1, 1, 0 );
1724 x264_prefetch_fenc( h, h->fdec, h->mb.i_mb_x, h->mb.i_mb_y );
1726 h->mb.type[i_mb_xy] = i_mb_type;
1727 h->mb.slice_table[i_mb_xy] = h->sh.i_first_mb;
1728 h->mb.partition[i_mb_xy] = IS_INTRA( i_mb_type ) ? D_16x16 : h->mb.i_partition;
1729 h->mb.i_mb_prev_xy = i_mb_xy;
1732 if( i_mb_type == I_4x4 )
1734 CP32( &i4x4[0], &h->mb.cache.intra4x4_pred_mode[x264_scan8[10]] );
1735 M32( &i4x4[4] ) = pack8to32( h->mb.cache.intra4x4_pred_mode[x264_scan8[5] ],
1736 h->mb.cache.intra4x4_pred_mode[x264_scan8[7] ],
1737 h->mb.cache.intra4x4_pred_mode[x264_scan8[13] ], 0);
1739 else if( !h->param.b_constrained_intra || IS_INTRA(i_mb_type) )
1740 M64( i4x4 ) = I_PRED_4x4_DC * 0x0101010101010101ULL;
1742 M64( i4x4 ) = (uint8_t)(-1) * 0x0101010101010101ULL;
1745 if( i_mb_type == I_PCM )
1747 h->mb.qp[i_mb_xy] = 0;
1748 h->mb.i_last_dqp = 0;
1749 h->mb.i_cbp_chroma = CHROMA444 ? 0 : 2;
1750 h->mb.i_cbp_luma = 0xf;
1751 h->mb.cbp[i_mb_xy] = (h->mb.i_cbp_chroma << 4) | h->mb.i_cbp_luma | 0x700;
1752 h->mb.b_transform_8x8 = 0;
1753 for( int i = 0; i < 48; i++ )
1754 h->mb.cache.non_zero_count[x264_scan8[i]] = h->param.b_cabac ? 1 : 16;
1758 if( h->mb.i_type != I_16x16 && h->mb.i_cbp_luma == 0 && h->mb.i_cbp_chroma == 0 )
1759 h->mb.i_qp = h->mb.i_last_qp;
1760 h->mb.qp[i_mb_xy] = h->mb.i_qp;
1761 h->mb.i_last_dqp = h->mb.i_qp - h->mb.i_last_qp;
1762 h->mb.i_last_qp = h->mb.i_qp;
1765 /* save non zero count */
1766 CP32( &nnz[ 0+0*4], &h->mb.cache.non_zero_count[x264_scan8[ 0]] );
1767 CP32( &nnz[ 0+1*4], &h->mb.cache.non_zero_count[x264_scan8[ 2]] );
1768 CP32( &nnz[ 0+2*4], &h->mb.cache.non_zero_count[x264_scan8[ 8]] );
1769 CP32( &nnz[ 0+3*4], &h->mb.cache.non_zero_count[x264_scan8[10]] );
1770 CP32( &nnz[16+0*4], &h->mb.cache.non_zero_count[x264_scan8[16+0]] );
1771 CP32( &nnz[16+1*4], &h->mb.cache.non_zero_count[x264_scan8[16+2]] );
1772 CP32( &nnz[32+0*4], &h->mb.cache.non_zero_count[x264_scan8[32+0]] );
1773 CP32( &nnz[32+1*4], &h->mb.cache.non_zero_count[x264_scan8[32+2]] );
1774 if( CHROMA_FORMAT >= CHROMA_422 )
1776 CP32( &nnz[16+2*4], &h->mb.cache.non_zero_count[x264_scan8[16+ 8]] );
1777 CP32( &nnz[16+3*4], &h->mb.cache.non_zero_count[x264_scan8[16+10]] );
1778 CP32( &nnz[32+2*4], &h->mb.cache.non_zero_count[x264_scan8[32+ 8]] );
1779 CP32( &nnz[32+3*4], &h->mb.cache.non_zero_count[x264_scan8[32+10]] );
1782 if( h->mb.i_cbp_luma == 0 && h->mb.i_type != I_8x8 )
1783 h->mb.b_transform_8x8 = 0;
1784 h->mb.mb_transform_size[i_mb_xy] = h->mb.b_transform_8x8;
1786 if( h->sh.i_type != SLICE_TYPE_I )
1788 int16_t (*mv0)[2] = &h->mb.mv[0][i_mb_4x4];
1789 int16_t (*mv1)[2] = &h->mb.mv[1][i_mb_4x4];
1790 int8_t *ref0 = &h->mb.ref[0][i_mb_8x8];
1791 int8_t *ref1 = &h->mb.ref[1][i_mb_8x8];
1792 if( !IS_INTRA( i_mb_type ) )
1794 ref0[0+0*s8x8] = h->mb.cache.ref[0][x264_scan8[0]];
1795 ref0[1+0*s8x8] = h->mb.cache.ref[0][x264_scan8[4]];
1796 ref0[0+1*s8x8] = h->mb.cache.ref[0][x264_scan8[8]];
1797 ref0[1+1*s8x8] = h->mb.cache.ref[0][x264_scan8[12]];
1798 CP128( &mv0[0*s4x4], h->mb.cache.mv[0][x264_scan8[0]+8*0] );
1799 CP128( &mv0[1*s4x4], h->mb.cache.mv[0][x264_scan8[0]+8*1] );
1800 CP128( &mv0[2*s4x4], h->mb.cache.mv[0][x264_scan8[0]+8*2] );
1801 CP128( &mv0[3*s4x4], h->mb.cache.mv[0][x264_scan8[0]+8*3] );
1802 if( h->sh.i_type == SLICE_TYPE_B )
1804 ref1[0+0*s8x8] = h->mb.cache.ref[1][x264_scan8[0]];
1805 ref1[1+0*s8x8] = h->mb.cache.ref[1][x264_scan8[4]];
1806 ref1[0+1*s8x8] = h->mb.cache.ref[1][x264_scan8[8]];
1807 ref1[1+1*s8x8] = h->mb.cache.ref[1][x264_scan8[12]];
1808 CP128( &mv1[0*s4x4], h->mb.cache.mv[1][x264_scan8[0]+8*0] );
1809 CP128( &mv1[1*s4x4], h->mb.cache.mv[1][x264_scan8[0]+8*1] );
1810 CP128( &mv1[2*s4x4], h->mb.cache.mv[1][x264_scan8[0]+8*2] );
1811 CP128( &mv1[3*s4x4], h->mb.cache.mv[1][x264_scan8[0]+8*3] );
1816 M16( &ref0[0*s8x8] ) = (uint8_t)(-1) * 0x0101;
1817 M16( &ref0[1*s8x8] ) = (uint8_t)(-1) * 0x0101;
1818 M128( &mv0[0*s4x4] ) = M128_ZERO;
1819 M128( &mv0[1*s4x4] ) = M128_ZERO;
1820 M128( &mv0[2*s4x4] ) = M128_ZERO;
1821 M128( &mv0[3*s4x4] ) = M128_ZERO;
1822 if( h->sh.i_type == SLICE_TYPE_B )
1824 M16( &ref1[0*s8x8] ) = (uint8_t)(-1) * 0x0101;
1825 M16( &ref1[1*s8x8] ) = (uint8_t)(-1) * 0x0101;
1826 M128( &mv1[0*s4x4] ) = M128_ZERO;
1827 M128( &mv1[1*s4x4] ) = M128_ZERO;
1828 M128( &mv1[2*s4x4] ) = M128_ZERO;
1829 M128( &mv1[3*s4x4] ) = M128_ZERO;
1834 if( h->param.b_cabac )
1836 uint8_t (*mvd0)[2] = h->mb.mvd[0][i_mb_xy];
1837 uint8_t (*mvd1)[2] = h->mb.mvd[1][i_mb_xy];
1838 if( IS_INTRA(i_mb_type) && i_mb_type != I_PCM )
1839 h->mb.chroma_pred_mode[i_mb_xy] = x264_mb_chroma_pred_mode_fix[h->mb.i_chroma_pred_mode];
1841 h->mb.chroma_pred_mode[i_mb_xy] = I_PRED_CHROMA_DC;
1843 if( (0x3FF30 >> i_mb_type) & 1 ) /* !INTRA && !SKIP && !DIRECT */
1845 CP64( mvd0[0], h->mb.cache.mvd[0][x264_scan8[10]] );
1846 CP16( mvd0[4], h->mb.cache.mvd[0][x264_scan8[5 ]] );
1847 CP16( mvd0[5], h->mb.cache.mvd[0][x264_scan8[7 ]] );
1848 CP16( mvd0[6], h->mb.cache.mvd[0][x264_scan8[13]] );
1849 if( h->sh.i_type == SLICE_TYPE_B )
1851 CP64( mvd1[0], h->mb.cache.mvd[1][x264_scan8[10]] );
1852 CP16( mvd1[4], h->mb.cache.mvd[1][x264_scan8[5 ]] );
1853 CP16( mvd1[5], h->mb.cache.mvd[1][x264_scan8[7 ]] );
1854 CP16( mvd1[6], h->mb.cache.mvd[1][x264_scan8[13]] );
1859 M128( mvd0[0] ) = M128_ZERO;
1860 if( h->sh.i_type == SLICE_TYPE_B )
1861 M128( mvd1[0] ) = M128_ZERO;
1864 if( h->sh.i_type == SLICE_TYPE_B )
1866 if( i_mb_type == B_SKIP || i_mb_type == B_DIRECT )
1867 h->mb.skipbp[i_mb_xy] = 0xf;
1868 else if( i_mb_type == B_8x8 )
1870 int skipbp = ( h->mb.i_sub_partition[0] == D_DIRECT_8x8 ) << 0;
1871 skipbp |= ( h->mb.i_sub_partition[1] == D_DIRECT_8x8 ) << 1;
1872 skipbp |= ( h->mb.i_sub_partition[2] == D_DIRECT_8x8 ) << 2;
1873 skipbp |= ( h->mb.i_sub_partition[3] == D_DIRECT_8x8 ) << 3;
1874 h->mb.skipbp[i_mb_xy] = skipbp;
1877 h->mb.skipbp[i_mb_xy] = 0;
1883 void x264_macroblock_bipred_init( x264_t *h )
1885 for( int mbfield = 0; mbfield <= SLICE_MBAFF; mbfield++ )
1886 for( int field = 0; field <= SLICE_MBAFF; field++ )
1887 for( int i_ref0 = 0; i_ref0 < (h->i_ref[0]<<mbfield); i_ref0++ )
1889 x264_frame_t *l0 = h->fref[0][i_ref0>>mbfield];
1890 int poc0 = l0->i_poc + mbfield*l0->i_delta_poc[field^(i_ref0&1)];
1891 for( int i_ref1 = 0; i_ref1 < (h->i_ref[1]<<mbfield); i_ref1++ )
1893 int dist_scale_factor;
1894 x264_frame_t *l1 = h->fref[1][i_ref1>>mbfield];
1895 int cur_poc = h->fdec->i_poc + mbfield*h->fdec->i_delta_poc[field];
1896 int poc1 = l1->i_poc + mbfield*l1->i_delta_poc[field^(i_ref1&1)];
1897 int td = x264_clip3( poc1 - poc0, -128, 127 );
1898 if( td == 0 /* || pic0 is a long-term ref */ )
1899 dist_scale_factor = 256;
1902 int tb = x264_clip3( cur_poc - poc0, -128, 127 );
1903 int tx = (16384 + (abs(td) >> 1)) / td;
1904 dist_scale_factor = x264_clip3( (tb * tx + 32) >> 6, -1024, 1023 );
1907 h->mb.dist_scale_factor_buf[mbfield][field][i_ref0][i_ref1] = dist_scale_factor;
1909 dist_scale_factor >>= 2;
1910 if( h->param.analyse.b_weighted_bipred
1911 && dist_scale_factor >= -64
1912 && dist_scale_factor <= 128 )
1914 h->mb.bipred_weight_buf[mbfield][field][i_ref0][i_ref1] = 64 - dist_scale_factor;
1915 // ssse3 implementation of biweight doesn't support the extrema.
1916 // if we ever generate them, we'll have to drop that optimization.
1917 assert( dist_scale_factor >= -63 && dist_scale_factor <= 127 );
1920 h->mb.bipred_weight_buf[mbfield][field][i_ref0][i_ref1] = 32;