1 /*****************************************************************************
2 * macroblock.c: h264 encoder library
3 *****************************************************************************
4 * Copyright (C) 2003-2008 x264 project
6 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
7 * Loren Merritt <lorenm@u.washington.edu>
8 * Fiona Glaser <fiona@x264.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
23 *****************************************************************************/
27 int x264_mb_predict_intra4x4_mode( x264_t *h, int idx )
29 const int ma = h->mb.cache.intra4x4_pred_mode[x264_scan8[idx] - 1];
30 const int mb = h->mb.cache.intra4x4_pred_mode[x264_scan8[idx] - 8];
31 const int m = X264_MIN( x264_mb_pred_mode4x4_fix(ma),
32 x264_mb_pred_mode4x4_fix(mb) );
40 int x264_mb_predict_non_zero_code( x264_t *h, int idx )
42 const int za = h->mb.cache.non_zero_count[x264_scan8[idx] - 1];
43 const int zb = h->mb.cache.non_zero_count[x264_scan8[idx] - 8];
49 i_ret = ( i_ret + 1 ) >> 1;
54 int x264_mb_transform_8x8_allowed( x264_t *h )
56 // intra and skip are disallowed
57 // large partitions are allowed
58 // direct and 8x8 are conditional
59 static const uint8_t partition_tab[X264_MBTYPE_MAX] = {
60 0,0,0,0,1,2,0,2,1,1,1,1,1,1,1,1,1,2,0,
64 if( !h->pps->b_transform_8x8_mode )
66 p = partition_tab[h->mb.i_type];
69 else if( h->mb.i_type == B_DIRECT )
70 return h->sps->b_direct8x8_inference;
71 else if( h->mb.i_type == P_8x8 )
73 if( !(h->param.analyse.inter & X264_ANALYSE_PSUB8x8) )
76 if( h->mb.i_sub_partition[i] != D_L0_8x8 )
82 // x264 currently doesn't use sub-8x8 B partitions, so don't check for them
83 if( h->sps->b_direct8x8_inference )
86 if( h->mb.i_sub_partition[i] == D_DIRECT_8x8 )
92 void x264_mb_predict_mv( x264_t *h, int i_list, int idx, int i_width, int16_t mvp[2] )
94 const int i8 = x264_scan8[idx];
95 const int i_ref= h->mb.cache.ref[i_list][i8];
96 int i_refa = h->mb.cache.ref[i_list][i8 - 1];
97 int16_t *mv_a = h->mb.cache.mv[i_list][i8 - 1];
98 int i_refb = h->mb.cache.ref[i_list][i8 - 8];
99 int16_t *mv_b = h->mb.cache.mv[i_list][i8 - 8];
100 int i_refc = h->mb.cache.ref[i_list][i8 - 8 + i_width ];
101 int16_t *mv_c = h->mb.cache.mv[i_list][i8 - 8 + i_width];
105 if( (idx&0x03) == 3 || ( i_width == 2 && (idx&0x3) == 2 )|| i_refc == -2 )
107 i_refc = h->mb.cache.ref[i_list][i8 - 8 - 1];
108 mv_c = h->mb.cache.mv[i_list][i8 - 8 - 1];
111 if( h->mb.i_partition == D_16x8 )
113 if( idx == 0 && i_refb == i_ref )
115 *(uint32_t*)mvp = *(uint32_t*)mv_b;
118 else if( idx != 0 && i_refa == i_ref )
120 *(uint32_t*)mvp = *(uint32_t*)mv_a;
124 else if( h->mb.i_partition == D_8x16 )
126 if( idx == 0 && i_refa == i_ref )
128 *(uint32_t*)mvp = *(uint32_t*)mv_a;
131 else if( idx != 0 && i_refc == i_ref )
133 *(uint32_t*)mvp = *(uint32_t*)mv_c;
139 if( i_refa == i_ref ) i_count++;
140 if( i_refb == i_ref ) i_count++;
141 if( i_refc == i_ref ) i_count++;
144 x264_median_mv( mvp, mv_a, mv_b, mv_c );
145 else if( i_count == 1 )
147 if( i_refa == i_ref )
148 *(uint32_t*)mvp = *(uint32_t*)mv_a;
149 else if( i_refb == i_ref )
150 *(uint32_t*)mvp = *(uint32_t*)mv_b;
152 *(uint32_t*)mvp = *(uint32_t*)mv_c;
154 else if( i_refb == -2 && i_refc == -2 && i_refa != -2 )
155 *(uint32_t*)mvp = *(uint32_t*)mv_a;
157 x264_median_mv( mvp, mv_a, mv_b, mv_c );
160 void x264_mb_predict_mv_16x16( x264_t *h, int i_list, int i_ref, int16_t mvp[2] )
162 int i_refa = h->mb.cache.ref[i_list][X264_SCAN8_0 - 1];
163 int16_t *mv_a = h->mb.cache.mv[i_list][X264_SCAN8_0 - 1];
164 int i_refb = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8];
165 int16_t *mv_b = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8];
166 int i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 + 4];
167 int16_t *mv_c = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8 + 4];
173 i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 - 1];
174 mv_c = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8 - 1];
178 if( i_refa == i_ref ) i_count++;
179 if( i_refb == i_ref ) i_count++;
180 if( i_refc == i_ref ) i_count++;
183 x264_median_mv( mvp, mv_a, mv_b, mv_c );
184 else if( i_count == 1 )
186 if( i_refa == i_ref )
187 *(uint32_t*)mvp = *(uint32_t*)mv_a;
188 else if( i_refb == i_ref )
189 *(uint32_t*)mvp = *(uint32_t*)mv_b;
191 *(uint32_t*)mvp = *(uint32_t*)mv_c;
193 else if( i_refb == -2 && i_refc == -2 && i_refa != -2 )
194 *(uint32_t*)mvp = *(uint32_t*)mv_a;
196 x264_median_mv( mvp, mv_a, mv_b, mv_c );
200 void x264_mb_predict_mv_pskip( x264_t *h, int16_t mv[2] )
202 int i_refa = h->mb.cache.ref[0][X264_SCAN8_0 - 1];
203 int i_refb = h->mb.cache.ref[0][X264_SCAN8_0 - 8];
204 int16_t *mv_a = h->mb.cache.mv[0][X264_SCAN8_0 - 1];
205 int16_t *mv_b = h->mb.cache.mv[0][X264_SCAN8_0 - 8];
207 if( i_refa == -2 || i_refb == -2 ||
208 ( i_refa == 0 && *(uint32_t*)mv_a == 0 ) ||
209 ( i_refb == 0 && *(uint32_t*)mv_b == 0 ) )
215 x264_mb_predict_mv_16x16( h, 0, 0, mv );
219 static int x264_mb_predict_mv_direct16x16_temporal( x264_t *h )
221 int i_mb_4x4 = 16 * h->mb.i_mb_stride * h->mb.i_mb_y + 4 * h->mb.i_mb_x;
222 int i_mb_8x8 = 4 * h->mb.i_mb_stride * h->mb.i_mb_y + 2 * h->mb.i_mb_x;
225 const int type_col = h->fref1[0]->mb_type[ h->mb.i_mb_xy ];
227 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, 0 );
229 if( IS_INTRA( type_col ) )
231 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, 0 );
232 x264_macroblock_cache_mv( h, 0, 0, 4, 4, 0, 0 );
233 x264_macroblock_cache_mv( h, 0, 0, 4, 4, 1, 0 );
236 b8x8 = h->sps->b_direct8x8_inference ||
237 (type_col != P_8x8 && type_col != B_SKIP && type_col != B_DIRECT && type_col != B_8x8);
239 for( i8 = 0; i8 < 4; i8++ )
243 const int i_part_8x8 = i_mb_8x8 + x8 + y8 * h->mb.i_b8_stride;
244 const int i_ref = h->mb.map_col_to_list0[ h->fref1[0]->ref[0][ i_part_8x8 ] ];
248 const int dist_scale_factor = h->mb.dist_scale_factor[i_ref][0];
250 x264_macroblock_cache_ref( h, 2*x8, 2*y8, 2, 2, 0, i_ref );
254 const int16_t *mv_col = h->fref1[0]->mv[0][ i_mb_4x4 + 3*x8 + 3*y8 * h->mb.i_b4_stride];
255 const int l0x = ( dist_scale_factor * mv_col[0] + 128 ) >> 8;
256 const int l0y = ( dist_scale_factor * mv_col[1] + 128 ) >> 8;
257 x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 0, pack16to32_mask(l0x, l0y) );
258 x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 1, pack16to32_mask(l0x-mv_col[0], l0y-mv_col[1]) );
262 for( i4 = 0; i4 < 4; i4++ )
264 const int x4 = i4%2 + 2*x8;
265 const int y4 = i4/2 + 2*y8;
266 const int16_t *mv_col = h->fref1[0]->mv[0][ i_mb_4x4 + x4 + y4 * h->mb.i_b4_stride ];
267 const int l0x = ( dist_scale_factor * mv_col[0] + 128 ) >> 8;
268 const int l0y = ( dist_scale_factor * mv_col[1] + 128 ) >> 8;
269 x264_macroblock_cache_mv( h, x4, y4, 1, 1, 0, pack16to32_mask(l0x, l0y) );
270 x264_macroblock_cache_mv( h, x4, y4, 1, 1, 1, pack16to32_mask(l0x-mv_col[0], l0y-mv_col[1]) );
276 /* the collocated ref isn't in the current list0 */
277 /* FIXME: we might still be able to use direct_8x8 on some partitions */
278 /* FIXME: with B-pyramid + extensive ref list reordering
279 * (not currently used), we would also have to check
280 * l1mv1 like in spatial mode */
285 if( h->param.i_threads > 1 )
287 int di = b8x8 ? 4 : 1;
288 for( i4=0; i4<16; i4+=di )
290 if( h->mb.cache.mv[0][x264_scan8[i4]][1] > h->mb.mv_max_spel[1]
291 || h->mb.cache.mv[1][x264_scan8[i4]][1] > h->mb.mv_max_spel[1] )
294 fprintf(stderr, "direct_temporal: (%d,%d) (%d,%d) > %d \n",
295 h->mb.cache.mv[0][x264_scan8[i4]][0],
296 h->mb.cache.mv[0][x264_scan8[i4]][1],
297 h->mb.cache.mv[1][x264_scan8[i4]][0],
298 h->mb.cache.mv[1][x264_scan8[i4]][1],
299 h->mb.mv_max_spel[1]);
309 static int x264_mb_predict_mv_direct16x16_spatial( x264_t *h )
312 DECLARE_ALIGNED_8( int16_t mv[2][2] );
316 const int8_t *l1ref0 = &h->fref1[0]->ref[0][ h->mb.i_b8_xy ];
317 const int8_t *l1ref1 = &h->fref1[0]->ref[1][ h->mb.i_b8_xy ];
318 const int16_t (*l1mv0)[2] = (const int16_t (*)[2]) &h->fref1[0]->mv[0][ h->mb.i_b4_xy ];
319 const int16_t (*l1mv1)[2] = (const int16_t (*)[2]) &h->fref1[0]->mv[1][ h->mb.i_b4_xy ];
320 const int type_col = h->fref1[0]->mb_type[ h->mb.i_mb_xy ];
322 for( i_list=0; i_list<2; i_list++ )
324 int i_refa = h->mb.cache.ref[i_list][X264_SCAN8_0 - 1];
325 int i_refb = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8];
326 int i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 + 4];
328 i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 - 1];
330 ref[i_list] = i_refa;
331 if( ref[i_list] < 0 || ( i_refb < ref[i_list] && i_refb >= 0 ))
332 ref[i_list] = i_refb;
333 if( ref[i_list] < 0 || ( i_refc < ref[i_list] && i_refc >= 0 ))
334 ref[i_list] = i_refc;
335 if( ref[i_list] < 0 )
339 if( ref[0] < 0 && ref[1] < 0 )
343 *(uint64_t*)mv[0] = 0;
347 for( i_list=0; i_list<2; i_list++ )
349 if( ref[i_list] >= 0 )
350 x264_mb_predict_mv_16x16( h, i_list, ref[i_list], mv[i_list] );
352 *(uint32_t*)mv[i_list] = 0;
356 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, ref[0] );
357 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, ref[1] );
358 x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 0, mv[0] );
359 x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 1, mv[1] );
361 if( IS_INTRA( type_col ) )
364 if( h->param.i_threads > 1
365 && ( mv[0][1] > h->mb.mv_max_spel[1]
366 || mv[1][1] > h->mb.mv_max_spel[1] ) )
369 fprintf(stderr, "direct_spatial: (%d,%d) (%d,%d) > %d \n",
370 mv[0][0], mv[0][1], mv[1][0], mv[1][1],
371 h->mb.mv_max_spel[1]);
376 b8x8 = h->sps->b_direct8x8_inference ||
377 (type_col != P_8x8 && type_col != B_SKIP && type_col != B_DIRECT && type_col != B_8x8);
380 for( i8=0; i8<4; i8++ )
384 const int o8 = x8 + y8 * h->mb.i_b8_stride;
385 if( l1ref0[o8] == 0 || ( l1ref0[o8] < 0 && l1ref1[o8] == 0 ) )
387 const int16_t (*l1mv)[2] = (l1ref0[o8] == 0) ? l1mv0 : l1mv1;
390 const int16_t *mvcol = l1mv[3*x8 + 3*y8 * h->mb.i_b4_stride];
391 if( abs( mvcol[0] ) <= 1 && abs( mvcol[1] ) <= 1 )
394 x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 0, 0 );
396 x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 1, 0 );
401 for( i4=0; i4<4; i4++ )
403 const int x4 = i4%2 + 2*x8;
404 const int y4 = i4/2 + 2*y8;
405 const int16_t *mvcol = l1mv[x4 + y4 * h->mb.i_b4_stride];
406 if( abs( mvcol[0] ) <= 1 && abs( mvcol[1] ) <= 1 )
409 x264_macroblock_cache_mv( h, x4, y4, 1, 1, 0, 0 );
411 x264_macroblock_cache_mv( h, x4, y4, 1, 1, 1, 0 );
421 int x264_mb_predict_mv_direct16x16( x264_t *h, int *b_changed )
424 if( h->param.analyse.i_direct_mv_pred == X264_DIRECT_PRED_NONE )
426 else if( h->sh.b_direct_spatial_mv_pred )
427 b_available = x264_mb_predict_mv_direct16x16_spatial( h );
429 b_available = x264_mb_predict_mv_direct16x16_temporal( h );
431 if( b_changed != NULL && b_available )
433 int type_col = h->fref1[0]->mb_type[ h->mb.i_mb_xy ];
434 if( IS_INTRA(type_col) || type_col == P_SKIP )
436 *b_changed = h->mb.cache.direct_ref[0][0] != h->mb.cache.ref[0][X264_SCAN8_0]
437 || h->mb.cache.direct_ref[1][0] != h->mb.cache.ref[1][X264_SCAN8_0]
438 || *(uint32_t*)h->mb.cache.direct_mv[0][X264_SCAN8_0] != *(uint32_t*)h->mb.cache.mv[0][X264_SCAN8_0]
439 || *(uint32_t*)h->mb.cache.direct_mv[1][X264_SCAN8_0] != *(uint32_t*)h->mb.cache.mv[1][X264_SCAN8_0];
445 for( l = 0; l < 2; l++ )
446 for( i = 0; i < 4; i++ )
447 *b_changed |= h->mb.cache.direct_ref[l][i] != h->mb.cache.ref[l][x264_scan8[i*4]];
448 *b_changed = *b_changed || memcmp(h->mb.cache.direct_mv, h->mb.cache.mv, sizeof(h->mb.cache.mv));
458 for( l = 0; l < 2; l++ )
459 for( i = 0; i < 4; i++ )
460 h->mb.cache.direct_ref[l][i] = h->mb.cache.ref[l][x264_scan8[i*4]];
461 h->mc.memcpy_aligned(h->mb.cache.direct_mv, h->mb.cache.mv, sizeof(h->mb.cache.mv));
467 void x264_mb_load_mv_direct8x8( x264_t *h, int idx )
469 const int x = 2*(idx%2);
470 const int y = 2*(idx/2);
471 x264_macroblock_cache_ref( h, x, y, 2, 2, 0, h->mb.cache.direct_ref[0][idx] );
472 x264_macroblock_cache_ref( h, x, y, 2, 2, 1, h->mb.cache.direct_ref[1][idx] );
473 *(uint64_t*)h->mb.cache.mv[0][x264_scan8[idx*4]] =
474 *(uint64_t*)h->mb.cache.direct_mv[0][x264_scan8[idx*4]];
475 *(uint64_t*)h->mb.cache.mv[0][x264_scan8[idx*4]+8] =
476 *(uint64_t*)h->mb.cache.direct_mv[0][x264_scan8[idx*4]+8];
477 *(uint64_t*)h->mb.cache.mv[1][x264_scan8[idx*4]] =
478 *(uint64_t*)h->mb.cache.direct_mv[1][x264_scan8[idx*4]];
479 *(uint64_t*)h->mb.cache.mv[1][x264_scan8[idx*4]+8] =
480 *(uint64_t*)h->mb.cache.direct_mv[1][x264_scan8[idx*4]+8];
483 #define FIXED_SCALE 256
485 /* This just improves encoder performance, it's not part of the spec */
486 void x264_mb_predict_mv_ref16x16( x264_t *h, int i_list, int i_ref, int16_t mvc[8][2], int *i_mvc )
488 int16_t (*mvr)[2] = h->mb.mvr[i_list][i_ref];
491 #define SET_MVP(mvp) { \
492 *(uint32_t*)mvc[i] = *(uint32_t*)mvp; \
497 if( h->sh.i_type == SLICE_TYPE_B
498 && h->mb.cache.ref[i_list][x264_scan8[12]] == i_ref )
500 SET_MVP( h->mb.cache.mv[i_list][x264_scan8[12]] );
503 /* spatial predictors */
504 if( h->mb.i_neighbour & MB_LEFT )
506 int i_mb_l = h->mb.i_mb_xy - 1;
507 /* skip MBs didn't go through the whole search process, so mvr is undefined */
508 if( !IS_SKIP( h->mb.type[i_mb_l] ) )
509 SET_MVP( mvr[i_mb_l] );
511 if( h->mb.i_neighbour & MB_TOP )
513 int i_mb_t = h->mb.i_mb_top_xy;
514 if( !IS_SKIP( h->mb.type[i_mb_t] ) )
515 SET_MVP( mvr[i_mb_t] );
517 if( h->mb.i_neighbour & MB_TOPLEFT && !IS_SKIP( h->mb.type[i_mb_t - 1] ) )
518 SET_MVP( mvr[i_mb_t-1] );
519 if( h->mb.i_mb_x < h->mb.i_mb_stride - 1 && !IS_SKIP( h->mb.type[i_mb_t + 1] ) )
520 SET_MVP( mvr[i_mb_t+1] );
524 /* temporal predictors */
525 /* FIXME temporal scaling w/ interlace */
526 if( h->fref0[0]->i_ref[0] > 0 && !h->sh.b_mbaff )
528 x264_frame_t *l0 = h->fref0[0];
530 #define SET_TMVP(dx, dy) { \
531 int i_b4 = h->mb.i_b4_xy + dx*4 + dy*4*h->mb.i_b4_stride; \
532 int i_b8 = h->mb.i_b8_xy + dx*2 + dy*2*h->mb.i_b8_stride; \
533 int ref_col = l0->ref[0][i_b8]; \
536 int scale = (h->fdec->i_poc - h->fdec->ref_poc[0][i_ref]) * l0->inv_ref_poc[ref_col];\
537 mvc[i][0] = l0->mv[0][i_b4][0] * scale / FIXED_SCALE; \
538 mvc[i][1] = l0->mv[0][i_b4][1] * scale / FIXED_SCALE; \
544 if( h->mb.i_mb_x < h->sps->i_mb_width-1 )
546 if( h->mb.i_mb_y < h->sps->i_mb_height-1 )
552 *(uint32_t*)mvc[i] = 0;
557 /* Set up a lookup table for delta pocs to reduce an IDIV to an IMUL */
558 static void setup_inverse_delta_pocs( x264_t *h )
561 for( i = 0; i < h->i_ref0; i++ )
563 int delta = h->fdec->i_poc - h->fref0[i]->i_poc;
564 h->fdec->inv_ref_poc[i] = (FIXED_SCALE + delta/2) / delta;
568 static inline void x264_mb_mc_0xywh( x264_t *h, int x, int y, int width, int height )
570 const int i8 = x264_scan8[0]+x+8*y;
571 const int i_ref = h->mb.cache.ref[0][i8];
572 const int mvx = x264_clip3( h->mb.cache.mv[0][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] );
573 int mvy = x264_clip3( h->mb.cache.mv[0][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] );
575 h->mc.mc_luma( &h->mb.pic.p_fdec[0][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE,
576 h->mb.pic.p_fref[0][i_ref], h->mb.pic.i_stride[0],
577 mvx + 4*4*x, mvy + 4*4*y, 4*width, 4*height );
579 // chroma is offset if MCing from a field of opposite parity
580 if( h->mb.b_interlaced & i_ref )
581 mvy += (h->mb.i_mb_y & 1)*4 - 2;
583 h->mc.mc_chroma( &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
584 &h->mb.pic.p_fref[0][i_ref][4][2*y*h->mb.pic.i_stride[1]+2*x], h->mb.pic.i_stride[1],
585 mvx, mvy, 2*width, 2*height );
587 h->mc.mc_chroma( &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
588 &h->mb.pic.p_fref[0][i_ref][5][2*y*h->mb.pic.i_stride[2]+2*x], h->mb.pic.i_stride[2],
589 mvx, mvy, 2*width, 2*height );
591 static inline void x264_mb_mc_1xywh( x264_t *h, int x, int y, int width, int height )
593 const int i8 = x264_scan8[0]+x+8*y;
594 const int i_ref = h->mb.cache.ref[1][i8];
595 const int mvx = x264_clip3( h->mb.cache.mv[1][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] );
596 int mvy = x264_clip3( h->mb.cache.mv[1][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] );
598 h->mc.mc_luma( &h->mb.pic.p_fdec[0][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE,
599 h->mb.pic.p_fref[1][i_ref], h->mb.pic.i_stride[0],
600 mvx + 4*4*x, mvy + 4*4*y, 4*width, 4*height );
602 if( h->mb.b_interlaced & i_ref )
603 mvy += (h->mb.i_mb_y & 1)*4 - 2;
605 h->mc.mc_chroma( &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
606 &h->mb.pic.p_fref[1][i_ref][4][2*y*h->mb.pic.i_stride[1]+2*x], h->mb.pic.i_stride[1],
607 mvx, mvy, 2*width, 2*height );
609 h->mc.mc_chroma( &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
610 &h->mb.pic.p_fref[1][i_ref][5][2*y*h->mb.pic.i_stride[2]+2*x], h->mb.pic.i_stride[2],
611 mvx, mvy, 2*width, 2*height );
614 static inline void x264_mb_mc_01xywh( x264_t *h, int x, int y, int width, int height )
616 const int i8 = x264_scan8[0]+x+8*y;
618 const int i_ref1 = h->mb.cache.ref[1][i8];
619 const int mvx1 = x264_clip3( h->mb.cache.mv[1][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] );
620 int mvy1 = x264_clip3( h->mb.cache.mv[1][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] );
621 DECLARE_ALIGNED_16( uint8_t tmp[16*16] );
622 int i_mode = x264_size2pixel[height][width];
624 x264_mb_mc_0xywh( h, x, y, width, height );
626 h->mc.mc_luma( tmp, 16, h->mb.pic.p_fref[1][i_ref1], h->mb.pic.i_stride[0],
627 mvx1 + 4*4*x, mvy1 + 4*4*y, 4*width, 4*height );
629 if( h->mb.b_interlaced & i_ref1 )
630 mvy1 += (h->mb.i_mb_y & 1)*4 - 2;
632 if( h->param.analyse.b_weighted_bipred )
634 const int i_ref0 = h->mb.cache.ref[0][i8];
635 const int weight = h->mb.bipred_weight[i_ref0][i_ref1];
637 h->mc.avg_weight[i_mode]( &h->mb.pic.p_fdec[0][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE, tmp, 16, weight );
639 h->mc.mc_chroma( tmp, 16, &h->mb.pic.p_fref[1][i_ref1][4][2*y*h->mb.pic.i_stride[1]+2*x], h->mb.pic.i_stride[1],
640 mvx1, mvy1, 2*width, 2*height );
641 h->mc.avg_weight[i_mode+3]( &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE, tmp, 16, weight );
643 h->mc.mc_chroma( tmp, 16, &h->mb.pic.p_fref[1][i_ref1][5][2*y*h->mb.pic.i_stride[2]+2*x], h->mb.pic.i_stride[2],
644 mvx1, mvy1, 2*width, 2*height );
645 h->mc.avg_weight[i_mode+3]( &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE, tmp, 16, weight );
649 h->mc.avg[i_mode]( &h->mb.pic.p_fdec[0][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE, tmp, 16 );
651 h->mc.mc_chroma( tmp, 16, &h->mb.pic.p_fref[1][i_ref1][4][2*y*h->mb.pic.i_stride[1]+2*x], h->mb.pic.i_stride[1],
652 mvx1, mvy1, 2*width, 2*height );
653 h->mc.avg[i_mode+3]( &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE, tmp, 16 );
655 h->mc.mc_chroma( tmp, 16, &h->mb.pic.p_fref[1][i_ref1][5][2*y*h->mb.pic.i_stride[2]+2*x], h->mb.pic.i_stride[2],
656 mvx1, mvy1, 2*width, 2*height );
657 h->mc.avg[i_mode+3]( &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE, tmp, 16 );
661 static void x264_mb_mc_direct8x8( x264_t *h, int x, int y )
663 const int i8 = x264_scan8[0] + x + 8*y;
665 /* FIXME: optimize based on current block size, not global settings? */
666 if( h->sps->b_direct8x8_inference )
668 if( h->mb.cache.ref[0][i8] >= 0 )
669 if( h->mb.cache.ref[1][i8] >= 0 )
670 x264_mb_mc_01xywh( h, x, y, 2, 2 );
672 x264_mb_mc_0xywh( h, x, y, 2, 2 );
674 x264_mb_mc_1xywh( h, x, y, 2, 2 );
678 if( h->mb.cache.ref[0][i8] >= 0 )
680 if( h->mb.cache.ref[1][i8] >= 0 )
682 x264_mb_mc_01xywh( h, x+0, y+0, 1, 1 );
683 x264_mb_mc_01xywh( h, x+1, y+0, 1, 1 );
684 x264_mb_mc_01xywh( h, x+0, y+1, 1, 1 );
685 x264_mb_mc_01xywh( h, x+1, y+1, 1, 1 );
689 x264_mb_mc_0xywh( h, x+0, y+0, 1, 1 );
690 x264_mb_mc_0xywh( h, x+1, y+0, 1, 1 );
691 x264_mb_mc_0xywh( h, x+0, y+1, 1, 1 );
692 x264_mb_mc_0xywh( h, x+1, y+1, 1, 1 );
697 x264_mb_mc_1xywh( h, x+0, y+0, 1, 1 );
698 x264_mb_mc_1xywh( h, x+1, y+0, 1, 1 );
699 x264_mb_mc_1xywh( h, x+0, y+1, 1, 1 );
700 x264_mb_mc_1xywh( h, x+1, y+1, 1, 1 );
705 void x264_mb_mc_8x8( x264_t *h, int i8 )
707 const int x = 2*(i8&1);
708 const int y = 2*(i8>>1);
709 switch( h->mb.i_sub_partition[i8] )
712 x264_mb_mc_0xywh( h, x, y, 2, 2 );
715 x264_mb_mc_0xywh( h, x, y+0, 2, 1 );
716 x264_mb_mc_0xywh( h, x, y+1, 2, 1 );
719 x264_mb_mc_0xywh( h, x+0, y, 1, 2 );
720 x264_mb_mc_0xywh( h, x+1, y, 1, 2 );
723 x264_mb_mc_0xywh( h, x+0, y+0, 1, 1 );
724 x264_mb_mc_0xywh( h, x+1, y+0, 1, 1 );
725 x264_mb_mc_0xywh( h, x+0, y+1, 1, 1 );
726 x264_mb_mc_0xywh( h, x+1, y+1, 1, 1 );
729 x264_mb_mc_1xywh( h, x, y, 2, 2 );
732 x264_mb_mc_1xywh( h, x, y+0, 2, 1 );
733 x264_mb_mc_1xywh( h, x, y+1, 2, 1 );
736 x264_mb_mc_1xywh( h, x+0, y, 1, 2 );
737 x264_mb_mc_1xywh( h, x+1, y, 1, 2 );
740 x264_mb_mc_1xywh( h, x+0, y+0, 1, 1 );
741 x264_mb_mc_1xywh( h, x+1, y+0, 1, 1 );
742 x264_mb_mc_1xywh( h, x+0, y+1, 1, 1 );
743 x264_mb_mc_1xywh( h, x+1, y+1, 1, 1 );
746 x264_mb_mc_01xywh( h, x, y, 2, 2 );
749 x264_mb_mc_01xywh( h, x, y+0, 2, 1 );
750 x264_mb_mc_01xywh( h, x, y+1, 2, 1 );
753 x264_mb_mc_01xywh( h, x+0, y, 1, 2 );
754 x264_mb_mc_01xywh( h, x+1, y, 1, 2 );
757 x264_mb_mc_01xywh( h, x+0, y+0, 1, 1 );
758 x264_mb_mc_01xywh( h, x+1, y+0, 1, 1 );
759 x264_mb_mc_01xywh( h, x+0, y+1, 1, 1 );
760 x264_mb_mc_01xywh( h, x+1, y+1, 1, 1 );
763 x264_mb_mc_direct8x8( h, x, y );
768 void x264_mb_mc( x264_t *h )
770 if( h->mb.i_type == P_L0 )
772 if( h->mb.i_partition == D_16x16 )
774 x264_mb_mc_0xywh( h, 0, 0, 4, 4 );
776 else if( h->mb.i_partition == D_16x8 )
778 x264_mb_mc_0xywh( h, 0, 0, 4, 2 );
779 x264_mb_mc_0xywh( h, 0, 2, 4, 2 );
781 else if( h->mb.i_partition == D_8x16 )
783 x264_mb_mc_0xywh( h, 0, 0, 2, 4 );
784 x264_mb_mc_0xywh( h, 2, 0, 2, 4 );
787 else if( h->mb.i_type == P_8x8 || h->mb.i_type == B_8x8 )
790 for( i = 0; i < 4; i++ )
791 x264_mb_mc_8x8( h, i );
793 else if( h->mb.i_type == B_SKIP || h->mb.i_type == B_DIRECT )
795 x264_mb_mc_direct8x8( h, 0, 0 );
796 x264_mb_mc_direct8x8( h, 2, 0 );
797 x264_mb_mc_direct8x8( h, 0, 2 );
798 x264_mb_mc_direct8x8( h, 2, 2 );
807 /* init ref list utilisations */
808 for( i = 0; i < 2; i++ )
810 b_list0[i] = x264_mb_type_list0_table[h->mb.i_type][i];
811 b_list1[i] = x264_mb_type_list1_table[h->mb.i_type][i];
813 if( h->mb.i_partition == D_16x16 )
815 if( b_list0[0] && b_list1[0] ) x264_mb_mc_01xywh( h, 0, 0, 4, 4 );
816 else if( b_list0[0] ) x264_mb_mc_0xywh ( h, 0, 0, 4, 4 );
817 else if( b_list1[0] ) x264_mb_mc_1xywh ( h, 0, 0, 4, 4 );
819 else if( h->mb.i_partition == D_16x8 )
821 if( b_list0[0] && b_list1[0] ) x264_mb_mc_01xywh( h, 0, 0, 4, 2 );
822 else if( b_list0[0] ) x264_mb_mc_0xywh ( h, 0, 0, 4, 2 );
823 else if( b_list1[0] ) x264_mb_mc_1xywh ( h, 0, 0, 4, 2 );
825 if( b_list0[1] && b_list1[1] ) x264_mb_mc_01xywh( h, 0, 2, 4, 2 );
826 else if( b_list0[1] ) x264_mb_mc_0xywh ( h, 0, 2, 4, 2 );
827 else if( b_list1[1] ) x264_mb_mc_1xywh ( h, 0, 2, 4, 2 );
829 else if( h->mb.i_partition == D_8x16 )
831 if( b_list0[0] && b_list1[0] ) x264_mb_mc_01xywh( h, 0, 0, 2, 4 );
832 else if( b_list0[0] ) x264_mb_mc_0xywh ( h, 0, 0, 2, 4 );
833 else if( b_list1[0] ) x264_mb_mc_1xywh ( h, 0, 0, 2, 4 );
835 if( b_list0[1] && b_list1[1] ) x264_mb_mc_01xywh( h, 2, 0, 2, 4 );
836 else if( b_list0[1] ) x264_mb_mc_0xywh ( h, 2, 0, 2, 4 );
837 else if( b_list1[1] ) x264_mb_mc_1xywh ( h, 2, 0, 2, 4 );
842 int x264_macroblock_cache_init( x264_t *h )
845 int i_mb_count = h->mb.i_mb_count;
847 h->mb.i_mb_stride = h->sps->i_mb_width;
848 h->mb.i_b8_stride = h->sps->i_mb_width * 2;
849 h->mb.i_b4_stride = h->sps->i_mb_width * 4;
851 h->mb.b_interlaced = h->param.b_interlaced;
853 CHECKED_MALLOC( h->mb.qp, i_mb_count * sizeof(int8_t) );
854 CHECKED_MALLOC( h->mb.cbp, i_mb_count * sizeof(int16_t) );
855 CHECKED_MALLOC( h->mb.skipbp, i_mb_count * sizeof(int8_t) );
856 CHECKED_MALLOC( h->mb.mb_transform_size, i_mb_count * sizeof(int8_t) );
858 /* 0 -> 3 top(4), 4 -> 6 : left(3) */
859 CHECKED_MALLOC( h->mb.intra4x4_pred_mode, i_mb_count * 8 * sizeof(int8_t) );
862 CHECKED_MALLOC( h->mb.non_zero_count, i_mb_count * 24 * sizeof(uint8_t) );
863 CHECKED_MALLOC( h->mb.nnz_backup, h->sps->i_mb_width * 4 * 16 * sizeof(uint8_t) );
865 if( h->param.b_cabac )
867 CHECKED_MALLOC( h->mb.chroma_pred_mode, i_mb_count * sizeof(int8_t) );
868 CHECKED_MALLOC( h->mb.mvd[0], 2*16 * i_mb_count * sizeof(int16_t) );
869 CHECKED_MALLOC( h->mb.mvd[1], 2*16 * i_mb_count * sizeof(int16_t) );
874 int i_refs = X264_MIN(16, (i ? 1 : h->param.i_frame_reference) + h->param.b_bframe_pyramid) << h->param.b_interlaced;
875 for( j=0; j < i_refs; j++ )
876 CHECKED_MALLOC( h->mb.mvr[i][j], 2 * i_mb_count * sizeof(int16_t) );
879 for( i=0; i<=h->param.b_interlaced; i++ )
882 CHECKED_MALLOC( h->mb.intra_border_backup[i][j], h->fdec->i_stride[j] );
883 h->mb.intra_border_backup[i][j] += 8;
886 /* init with not available (for top right idx=7,15) */
887 memset( h->mb.cache.ref[0], -2, X264_SCAN8_SIZE * sizeof( int8_t ) );
888 memset( h->mb.cache.ref[1], -2, X264_SCAN8_SIZE * sizeof( int8_t ) );
893 void x264_macroblock_cache_end( x264_t *h )
896 for( i=0; i<=h->param.b_interlaced; i++ )
898 x264_free( h->mb.intra_border_backup[i][j] - 8 );
900 for( j=0; j<32; j++ )
901 x264_free( h->mb.mvr[i][j] );
902 if( h->param.b_cabac )
904 x264_free( h->mb.chroma_pred_mode );
905 x264_free( h->mb.mvd[0] );
906 x264_free( h->mb.mvd[1] );
908 x264_free( h->mb.intra4x4_pred_mode );
909 x264_free( h->mb.non_zero_count );
910 x264_free( h->mb.nnz_backup );
911 x264_free( h->mb.mb_transform_size );
912 x264_free( h->mb.skipbp );
913 x264_free( h->mb.cbp );
914 x264_free( h->mb.qp );
916 void x264_macroblock_slice_init( x264_t *h )
920 h->mb.mv[0] = h->fdec->mv[0];
921 h->mb.mv[1] = h->fdec->mv[1];
922 h->mb.ref[0] = h->fdec->ref[0];
923 h->mb.ref[1] = h->fdec->ref[1];
924 h->mb.type = h->fdec->mb_type;
926 h->fdec->i_ref[0] = h->i_ref0;
927 h->fdec->i_ref[1] = h->i_ref1;
928 for( i = 0; i < h->i_ref0; i++ )
929 h->fdec->ref_poc[0][i] = h->fref0[i]->i_poc;
930 if( h->sh.i_type == SLICE_TYPE_B )
932 for( i = 0; i < h->i_ref1; i++ )
933 h->fdec->ref_poc[1][i] = h->fref1[i]->i_poc;
935 h->mb.map_col_to_list0[-1] = -1;
936 h->mb.map_col_to_list0[-2] = -2;
937 for( i = 0; i < h->fref1[0]->i_ref[0]; i++ )
939 int poc = h->fref1[0]->ref_poc[0][i];
940 h->mb.map_col_to_list0[i] = -2;
941 for( j = 0; j < h->i_ref0; j++ )
942 if( h->fref0[j]->i_poc == poc )
944 h->mb.map_col_to_list0[i] = j;
949 if( h->sh.i_type == SLICE_TYPE_P )
950 memset( h->mb.cache.skip, 0, X264_SCAN8_SIZE * sizeof( int8_t ) );
952 setup_inverse_delta_pocs( h );
955 void x264_prefetch_fenc( x264_t *h, x264_frame_t *fenc, int i_mb_x, int i_mb_y )
957 int stride_y = fenc->i_stride[0];
958 int stride_uv = fenc->i_stride[1];
959 int off_y = 16 * (i_mb_x + i_mb_y * stride_y);
960 int off_uv = 8 * (i_mb_x + i_mb_y * stride_uv);
961 h->mc.prefetch_fenc( fenc->plane[0]+off_y, stride_y,
962 fenc->plane[1+(i_mb_x&1)]+off_uv, stride_uv, i_mb_x );
965 static NOINLINE void copy_column8( uint8_t *dst, uint8_t *src )
969 dst[i*FDEC_STRIDE] = src[i*FDEC_STRIDE];
972 static void ALWAYS_INLINE x264_macroblock_load_pic_pointers( x264_t *h, int i_mb_x, int i_mb_y, int i)
974 const int w = (i == 0 ? 16 : 8);
975 const int i_stride = h->fdec->i_stride[i];
976 const int i_stride2 = i_stride << h->mb.b_interlaced;
977 const int i_pix_offset = h->mb.b_interlaced
978 ? w * (i_mb_x + (i_mb_y&~1) * i_stride) + (i_mb_y&1) * i_stride
979 : w * (i_mb_x + i_mb_y * i_stride);
980 int ref_pix_offset[2] = { i_pix_offset, i_pix_offset };
981 const uint8_t *intra_fdec = &h->mb.intra_border_backup[i_mb_y & h->sh.b_mbaff][i][i_mb_x*16>>!!i];
982 x264_frame_t **fref[2] = { h->fref0, h->fref1 };
984 if( h->mb.b_interlaced )
985 ref_pix_offset[1] += (1-2*(i_mb_y&1)) * i_stride;
986 h->mb.pic.i_stride[i] = i_stride2;
987 h->mc.copy[i?PIXEL_8x8:PIXEL_16x16]( h->mb.pic.p_fenc[i], FENC_STRIDE,
988 &h->fenc->plane[i][i_pix_offset], i_stride2, w );
989 memcpy( &h->mb.pic.p_fdec[i][-1-FDEC_STRIDE], intra_fdec-1, w*3/2+1 );
990 if( h->mb.b_interlaced )
992 const uint8_t *plane_fdec = &h->fdec->plane[i][i_pix_offset];
993 for( j = 0; j < w; j++ )
994 h->mb.pic.p_fdec[i][-1+j*FDEC_STRIDE] = plane_fdec[-1+j*i_stride2];
996 for( j = 0; j < h->mb.pic.i_fref[0]; j++ )
998 h->mb.pic.p_fref[0][j][i==0 ? 0:i+3] = &fref[0][j >> h->mb.b_interlaced]->plane[i][ref_pix_offset[j&1]];
1000 for( k = 1; k < 4; k++ )
1001 h->mb.pic.p_fref[0][j][k] = &fref[0][j >> h->mb.b_interlaced]->filtered[k][ref_pix_offset[j&1]];
1003 if( h->sh.i_type == SLICE_TYPE_B )
1004 for( j = 0; j < h->mb.pic.i_fref[1]; j++ )
1006 h->mb.pic.p_fref[1][j][i==0 ? 0:i+3] = &fref[1][j >> h->mb.b_interlaced]->plane[i][ref_pix_offset[j&1]];
1008 for( k = 1; k < 4; k++ )
1009 h->mb.pic.p_fref[1][j][k] = &fref[1][j >> h->mb.b_interlaced]->filtered[k][ref_pix_offset[j&1]];
1013 void x264_macroblock_cache_load( x264_t *h, int i_mb_x, int i_mb_y )
1015 int i_mb_xy = i_mb_y * h->mb.i_mb_stride + i_mb_x;
1016 int i_mb_4x4 = 4*(i_mb_y * h->mb.i_b4_stride + i_mb_x);
1017 int i_mb_8x8 = 2*(i_mb_y * h->mb.i_b8_stride + i_mb_x);
1018 int i_top_y = i_mb_y - (1 << h->mb.b_interlaced);
1019 int i_top_xy = i_top_y * h->mb.i_mb_stride + i_mb_x;
1020 int i_top_4x4 = (4*i_top_y+3) * h->mb.i_b4_stride + 4*i_mb_x;
1021 int i_top_8x8 = (2*i_top_y+1) * h->mb.i_b8_stride + 2*i_mb_x;
1023 int i_top_type = -1; /* gcc warn */
1024 int i_left_type= -1;
1028 assert( h->mb.i_b8_stride == 2*h->mb.i_mb_stride );
1029 assert( h->mb.i_b4_stride == 4*h->mb.i_mb_stride );
1032 h->mb.i_mb_x = i_mb_x;
1033 h->mb.i_mb_y = i_mb_y;
1034 h->mb.i_mb_xy = i_mb_xy;
1035 h->mb.i_b8_xy = i_mb_8x8;
1036 h->mb.i_b4_xy = i_mb_4x4;
1037 h->mb.i_mb_top_xy = i_top_xy;
1038 h->mb.i_neighbour = 0;
1041 if( i_top_xy >= h->sh.i_first_mb )
1043 h->mb.i_mb_type_top =
1044 i_top_type= h->mb.type[i_top_xy];
1046 h->mb.i_neighbour |= MB_TOP;
1049 *(uint32_t*)&h->mb.cache.intra4x4_pred_mode[x264_scan8[0] - 8] = *(uint32_t*)&h->mb.intra4x4_pred_mode[i_top_xy][0];
1051 /* load non_zero_count */
1052 *(uint32_t*)&h->mb.cache.non_zero_count[x264_scan8[0] - 8] = *(uint32_t*)&h->mb.non_zero_count[i_top_xy][12];
1053 /* shift because x264_scan8[16] is misaligned */
1054 *(uint32_t*)&h->mb.cache.non_zero_count[x264_scan8[16] - 9] = *(uint16_t*)&h->mb.non_zero_count[i_top_xy][18] << 8;
1055 *(uint32_t*)&h->mb.cache.non_zero_count[x264_scan8[16+4] - 9] = *(uint16_t*)&h->mb.non_zero_count[i_top_xy][22] << 8;
1059 h->mb.i_mb_type_top = -1;
1062 h->mb.cache.intra4x4_pred_mode[x264_scan8[0] - 8] =
1063 h->mb.cache.intra4x4_pred_mode[x264_scan8[1] - 8] =
1064 h->mb.cache.intra4x4_pred_mode[x264_scan8[4] - 8] =
1065 h->mb.cache.intra4x4_pred_mode[x264_scan8[5] - 8] = -1;
1067 /* load non_zero_count */
1068 h->mb.cache.non_zero_count[x264_scan8[0] - 8] =
1069 h->mb.cache.non_zero_count[x264_scan8[1] - 8] =
1070 h->mb.cache.non_zero_count[x264_scan8[4] - 8] =
1071 h->mb.cache.non_zero_count[x264_scan8[5] - 8] =
1072 h->mb.cache.non_zero_count[x264_scan8[16+0] - 8] =
1073 h->mb.cache.non_zero_count[x264_scan8[16+1] - 8] =
1074 h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 8] =
1075 h->mb.cache.non_zero_count[x264_scan8[16+4+1] - 8] = 0x80;
1078 if( i_mb_x > 0 && i_mb_xy > h->sh.i_first_mb )
1080 i_left_xy = i_mb_xy - 1;
1081 h->mb.i_mb_type_left =
1082 i_left_type = h->mb.type[i_left_xy];
1084 h->mb.i_neighbour |= MB_LEFT;
1087 h->mb.cache.intra4x4_pred_mode[x264_scan8[0 ] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][4];
1088 h->mb.cache.intra4x4_pred_mode[x264_scan8[2 ] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][5];
1089 h->mb.cache.intra4x4_pred_mode[x264_scan8[8 ] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][6];
1090 h->mb.cache.intra4x4_pred_mode[x264_scan8[10] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][3];
1092 /* load non_zero_count */
1093 h->mb.cache.non_zero_count[x264_scan8[0 ] - 1] = h->mb.non_zero_count[i_left_xy][3];
1094 h->mb.cache.non_zero_count[x264_scan8[2 ] - 1] = h->mb.non_zero_count[i_left_xy][7];
1095 h->mb.cache.non_zero_count[x264_scan8[8 ] - 1] = h->mb.non_zero_count[i_left_xy][11];
1096 h->mb.cache.non_zero_count[x264_scan8[10] - 1] = h->mb.non_zero_count[i_left_xy][15];
1098 h->mb.cache.non_zero_count[x264_scan8[16+0] - 1] = h->mb.non_zero_count[i_left_xy][16+1];
1099 h->mb.cache.non_zero_count[x264_scan8[16+2] - 1] = h->mb.non_zero_count[i_left_xy][16+3];
1101 h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 1] = h->mb.non_zero_count[i_left_xy][16+4+1];
1102 h->mb.cache.non_zero_count[x264_scan8[16+4+2] - 1] = h->mb.non_zero_count[i_left_xy][16+4+3];
1106 h->mb.i_mb_type_left = -1;
1108 h->mb.cache.intra4x4_pred_mode[x264_scan8[0 ] - 1] =
1109 h->mb.cache.intra4x4_pred_mode[x264_scan8[2 ] - 1] =
1110 h->mb.cache.intra4x4_pred_mode[x264_scan8[8 ] - 1] =
1111 h->mb.cache.intra4x4_pred_mode[x264_scan8[10] - 1] = -1;
1113 /* load non_zero_count */
1114 h->mb.cache.non_zero_count[x264_scan8[0 ] - 1] =
1115 h->mb.cache.non_zero_count[x264_scan8[2 ] - 1] =
1116 h->mb.cache.non_zero_count[x264_scan8[8 ] - 1] =
1117 h->mb.cache.non_zero_count[x264_scan8[10] - 1] =
1118 h->mb.cache.non_zero_count[x264_scan8[16+0] - 1] =
1119 h->mb.cache.non_zero_count[x264_scan8[16+2] - 1] =
1120 h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 1] =
1121 h->mb.cache.non_zero_count[x264_scan8[16+4+2] - 1] = 0x80;
1124 if( i_mb_x < h->sps->i_mb_width - 1 && i_top_xy + 1 >= h->sh.i_first_mb )
1126 h->mb.i_neighbour |= MB_TOPRIGHT;
1127 h->mb.i_mb_type_topright = h->mb.type[ i_top_xy + 1 ];
1130 h->mb.i_mb_type_topright = -1;
1131 if( i_mb_x > 0 && i_top_xy - 1 >= h->sh.i_first_mb )
1133 h->mb.i_neighbour |= MB_TOPLEFT;
1134 h->mb.i_mb_type_topleft = h->mb.type[ i_top_xy - 1 ];
1137 h->mb.i_mb_type_topleft = -1;
1139 if( h->pps->b_transform_8x8_mode )
1141 h->mb.cache.i_neighbour_transform_size =
1142 ( i_left_type >= 0 && h->mb.mb_transform_size[i_left_xy] )
1143 + ( i_top_type >= 0 && h->mb.mb_transform_size[i_top_xy] );
1148 h->mb.pic.i_fref[0] = h->i_ref0 << h->mb.b_interlaced;
1149 h->mb.pic.i_fref[1] = h->i_ref1 << h->mb.b_interlaced;
1150 h->mb.cache.i_neighbour_interlaced =
1151 !!(h->mb.i_neighbour & MB_LEFT)
1152 + !!(h->mb.i_neighbour & MB_TOP);
1165 h->mb.pic.p_fenc[0] = h->mb.pic.fenc_buf;
1166 h->mb.pic.p_fenc[1] = h->mb.pic.fenc_buf + 16*FENC_STRIDE;
1167 h->mb.pic.p_fenc[2] = h->mb.pic.fenc_buf + 16*FENC_STRIDE + 8;
1168 h->mb.pic.p_fdec[0] = h->mb.pic.fdec_buf + 2*FDEC_STRIDE;
1169 h->mb.pic.p_fdec[1] = h->mb.pic.fdec_buf + 19*FDEC_STRIDE;
1170 h->mb.pic.p_fdec[2] = h->mb.pic.fdec_buf + 19*FDEC_STRIDE + 16;
1172 if( !h->mb.b_interlaced )
1174 copy_column8( h->mb.pic.p_fdec[0]-1, h->mb.pic.p_fdec[0]+15 );
1175 copy_column8( h->mb.pic.p_fdec[0]-1+8*FDEC_STRIDE, h->mb.pic.p_fdec[0]+15+8*FDEC_STRIDE );
1176 copy_column8( h->mb.pic.p_fdec[1]-1, h->mb.pic.p_fdec[1]+7 );
1177 copy_column8( h->mb.pic.p_fdec[2]-1, h->mb.pic.p_fdec[2]+7 );
1180 /* load picture pointers */
1181 x264_macroblock_load_pic_pointers( h, i_mb_x, i_mb_y, 0 );
1182 x264_macroblock_load_pic_pointers( h, i_mb_x, i_mb_y, 1 );
1183 x264_macroblock_load_pic_pointers( h, i_mb_x, i_mb_y, 2 );
1185 if( h->fdec->integral )
1187 assert( !h->mb.b_interlaced );
1188 for( i = 0; i < h->mb.pic.i_fref[0]; i++ )
1189 h->mb.pic.p_integral[0][i] = &h->fref0[i]->integral[ 16 * ( i_mb_x + i_mb_y * h->fdec->i_stride[0] )];
1190 for( i = 0; i < h->mb.pic.i_fref[1]; i++ )
1191 h->mb.pic.p_integral[1][i] = &h->fref1[i]->integral[ 16 * ( i_mb_x + i_mb_y * h->fdec->i_stride[0] )];
1194 x264_prefetch_fenc( h, h->fenc, i_mb_x, i_mb_y );
1196 /* load ref/mv/mvd */
1197 if( h->sh.i_type != SLICE_TYPE_I )
1199 const int s8x8 = h->mb.i_b8_stride;
1200 const int s4x4 = h->mb.i_b4_stride;
1204 for( i_list = 0; i_list < (h->sh.i_type == SLICE_TYPE_B ? 2 : 1 ); i_list++ )
1207 h->mb.cache.ref[i_list][x264_scan8[5 ]+1] =
1208 h->mb.cache.ref[i_list][x264_scan8[7 ]+1] =
1209 h->mb.cache.ref[i_list][x264_scan8[13]+1] = -2;
1212 if( h->mb.i_neighbour & MB_TOPLEFT )
1214 const int i8 = x264_scan8[0] - 1 - 1*8;
1215 const int ir = i_top_8x8 - 1;
1216 const int iv = i_top_4x4 - 1;
1217 h->mb.cache.ref[i_list][i8] = h->mb.ref[i_list][ir];
1218 *(uint32_t*)h->mb.cache.mv[i_list][i8] = *(uint32_t*)h->mb.mv[i_list][iv];
1222 const int i8 = x264_scan8[0] - 1 - 1*8;
1223 h->mb.cache.ref[i_list][i8] = -2;
1224 *(uint32_t*)h->mb.cache.mv[i_list][i8] = 0;
1227 if( h->mb.i_neighbour & MB_TOP )
1229 const int i8 = x264_scan8[0] - 8;
1230 const int ir = i_top_8x8;
1231 const int iv = i_top_4x4;
1232 h->mb.cache.ref[i_list][i8+0] =
1233 h->mb.cache.ref[i_list][i8+1] = h->mb.ref[i_list][ir + 0];
1234 h->mb.cache.ref[i_list][i8+2] =
1235 h->mb.cache.ref[i_list][i8+3] = h->mb.ref[i_list][ir + 1];
1236 *(uint64_t*)h->mb.cache.mv[i_list][i8+0] = *(uint64_t*)h->mb.mv[i_list][iv+0];
1237 *(uint64_t*)h->mb.cache.mv[i_list][i8+2] = *(uint64_t*)h->mb.mv[i_list][iv+2];
1241 const int i8 = x264_scan8[0] - 8;
1242 *(uint64_t*)h->mb.cache.mv[i_list][i8+0] = 0;
1243 *(uint64_t*)h->mb.cache.mv[i_list][i8+2] = 0;
1244 *(uint32_t*)&h->mb.cache.ref[i_list][i8] = (uint8_t)(-2) * 0x01010101U;
1247 if( h->mb.i_neighbour & MB_TOPRIGHT )
1249 const int i8 = x264_scan8[0] + 4 - 1*8;
1250 const int ir = i_top_8x8 + 2;
1251 const int iv = i_top_4x4 + 4;
1252 h->mb.cache.ref[i_list][i8] = h->mb.ref[i_list][ir];
1253 *(uint32_t*)h->mb.cache.mv[i_list][i8] = *(uint32_t*)h->mb.mv[i_list][iv];
1257 const int i8 = x264_scan8[0] + 4 - 1*8;
1258 h->mb.cache.ref[i_list][i8] = -2;
1259 *(uint32_t*)h->mb.cache.mv[i_list][i8] = 0;
1262 if( h->mb.i_neighbour & MB_LEFT )
1264 const int i8 = x264_scan8[0] - 1;
1265 const int ir = i_mb_8x8 - 1;
1266 const int iv = i_mb_4x4 - 1;
1267 h->mb.cache.ref[i_list][i8+0*8] =
1268 h->mb.cache.ref[i_list][i8+1*8] = h->mb.ref[i_list][ir + 0*s8x8];
1269 h->mb.cache.ref[i_list][i8+2*8] =
1270 h->mb.cache.ref[i_list][i8+3*8] = h->mb.ref[i_list][ir + 1*s8x8];
1272 for( i = 0; i < 4; i++ )
1273 *(uint32_t*)h->mb.cache.mv[i_list][i8+i*8] = *(uint32_t*)h->mb.mv[i_list][iv + i*s4x4];
1277 const int i8 = x264_scan8[0] - 1;
1278 for( i = 0; i < 4; i++ )
1280 h->mb.cache.ref[i_list][i8+i*8] = -2;
1281 *(uint32_t*)h->mb.cache.mv[i_list][i8+i*8] = 0;
1285 if( h->param.b_cabac )
1287 if( i_top_type >= 0 )
1289 const int i8 = x264_scan8[0] - 8;
1290 const int iv = i_top_4x4;
1291 *(uint64_t*)h->mb.cache.mvd[i_list][i8+0] = *(uint64_t*)h->mb.mvd[i_list][iv+0];
1292 *(uint64_t*)h->mb.cache.mvd[i_list][i8+2] = *(uint64_t*)h->mb.mvd[i_list][iv+2];
1296 const int i8 = x264_scan8[0] - 8;
1297 *(uint64_t*)h->mb.cache.mvd[i_list][i8+0] =
1298 *(uint64_t*)h->mb.cache.mvd[i_list][i8+2] = 0;
1301 if( i_left_type >= 0 )
1303 const int i8 = x264_scan8[0] - 1;
1304 const int iv = i_mb_4x4 - 1;
1305 for( i = 0; i < 4; i++ )
1306 *(uint32_t*)h->mb.cache.mvd[i_list][i8+i*8] = *(uint32_t*)h->mb.mvd[i_list][iv + i*s4x4];
1310 const int i8 = x264_scan8[0] - 1;
1311 for( i = 0; i < 4; i++ )
1312 *(uint32_t*)h->mb.cache.mvd[i_list][i8+i*8] = 0;
1318 if( h->sh.i_type == SLICE_TYPE_B && h->param.b_cabac )
1320 memset( h->mb.cache.skip, 0, X264_SCAN8_SIZE * sizeof( int8_t ) );
1321 if( i_left_type >= 0 )
1323 uint8_t skipbp = h->mb.skipbp[i_left_xy];
1324 h->mb.cache.skip[x264_scan8[0] - 1] = skipbp & 0x2;
1325 h->mb.cache.skip[x264_scan8[8] - 1] = skipbp & 0x8;
1327 if( i_top_type >= 0 )
1329 uint8_t skipbp = h->mb.skipbp[i_top_xy];
1330 h->mb.cache.skip[x264_scan8[0] - 8] = skipbp & 0x4;
1331 h->mb.cache.skip[x264_scan8[4] - 8] = skipbp & 0x8;
1335 if( h->sh.i_type == SLICE_TYPE_P )
1336 x264_mb_predict_mv_pskip( h, h->mb.cache.pskip_mv );
1339 h->mb.i_neighbour4[0] =
1340 h->mb.i_neighbour8[0] = (h->mb.i_neighbour & (MB_TOP|MB_LEFT|MB_TOPLEFT))
1341 | ((h->mb.i_neighbour & MB_TOP) ? MB_TOPRIGHT : 0);
1342 h->mb.i_neighbour4[4] =
1343 h->mb.i_neighbour4[1] = MB_LEFT | ((h->mb.i_neighbour & MB_TOP) ? (MB_TOP|MB_TOPLEFT|MB_TOPRIGHT) : 0);
1344 h->mb.i_neighbour4[2] =
1345 h->mb.i_neighbour4[8] =
1346 h->mb.i_neighbour4[10] =
1347 h->mb.i_neighbour8[2] = MB_TOP|MB_TOPRIGHT | ((h->mb.i_neighbour & MB_LEFT) ? (MB_LEFT|MB_TOPLEFT) : 0);
1348 h->mb.i_neighbour4[3] =
1349 h->mb.i_neighbour4[7] =
1350 h->mb.i_neighbour4[11] =
1351 h->mb.i_neighbour4[13] =
1352 h->mb.i_neighbour4[15] =
1353 h->mb.i_neighbour8[3] = MB_LEFT|MB_TOP|MB_TOPLEFT;
1354 h->mb.i_neighbour4[5] =
1355 h->mb.i_neighbour8[1] = MB_LEFT | (h->mb.i_neighbour & MB_TOPRIGHT)
1356 | ((h->mb.i_neighbour & MB_TOP) ? MB_TOP|MB_TOPLEFT : 0);
1357 h->mb.i_neighbour4[6] =
1358 h->mb.i_neighbour4[9] =
1359 h->mb.i_neighbour4[12] =
1360 h->mb.i_neighbour4[14] = MB_LEFT|MB_TOP|MB_TOPLEFT|MB_TOPRIGHT;
1363 static void ALWAYS_INLINE x264_macroblock_store_pic( x264_t *h, int i)
1366 int i_stride = h->fdec->i_stride[i];
1367 int i_stride2 = i_stride << h->mb.b_interlaced;
1368 int i_pix_offset = h->mb.b_interlaced
1369 ? w * (h->mb.i_mb_x + (h->mb.i_mb_y&~1) * i_stride) + (h->mb.i_mb_y&1) * i_stride
1370 : w * (h->mb.i_mb_x + h->mb.i_mb_y * i_stride);
1371 h->mc.copy[i?PIXEL_8x8:PIXEL_16x16](
1372 &h->fdec->plane[i][i_pix_offset], i_stride2,
1373 h->mb.pic.p_fdec[i], FDEC_STRIDE, w );
1376 void x264_macroblock_cache_save( x264_t *h )
1378 const int i_mb_xy = h->mb.i_mb_xy;
1379 const int i_mb_type = x264_mb_type_fix[h->mb.i_type];
1380 const int s8x8 = h->mb.i_b8_stride;
1381 const int s4x4 = h->mb.i_b4_stride;
1382 const int i_mb_4x4 = h->mb.i_b4_xy;
1383 const int i_mb_8x8 = h->mb.i_b8_xy;
1385 /* GCC pessimizes direct stores to heap-allocated 8-bit arrays due to aliasing.*/
1386 /* By only dereferencing them once, we avoid this issue. */
1387 int8_t *intra4x4_pred_mode = h->mb.intra4x4_pred_mode[i_mb_xy];
1388 uint8_t *non_zero_count = h->mb.non_zero_count[i_mb_xy];
1392 x264_macroblock_store_pic( h, 0 );
1393 x264_macroblock_store_pic( h, 1 );
1394 x264_macroblock_store_pic( h, 2 );
1396 x264_prefetch_fenc( h, h->fdec, h->mb.i_mb_x, h->mb.i_mb_y );
1398 h->mb.type[i_mb_xy] = i_mb_type;
1400 if( h->mb.i_type != I_16x16 && h->mb.i_cbp_luma == 0 && h->mb.i_cbp_chroma == 0 )
1401 h->mb.i_qp = h->mb.i_last_qp;
1402 h->mb.qp[i_mb_xy] = h->mb.i_qp;
1404 h->mb.i_last_dqp = h->mb.i_qp - h->mb.i_last_qp;
1405 h->mb.i_last_qp = h->mb.i_qp;
1406 h->mb.i_mb_prev_xy = h->mb.i_mb_xy;
1409 if( i_mb_type == I_4x4 )
1411 *(uint32_t*)&intra4x4_pred_mode[0] = *(uint32_t*)&h->mb.cache.intra4x4_pred_mode[x264_scan8[10] ];
1412 *(uint32_t*)&intra4x4_pred_mode[4] = pack8to32(h->mb.cache.intra4x4_pred_mode[x264_scan8[5] ],
1413 h->mb.cache.intra4x4_pred_mode[x264_scan8[7] ],
1414 h->mb.cache.intra4x4_pred_mode[x264_scan8[13] ], 0);
1417 *(uint64_t*)intra4x4_pred_mode = I_PRED_4x4_DC * 0x0101010101010101ULL;
1419 if( i_mb_type == I_PCM )
1421 h->mb.cbp[i_mb_xy] = 0x72f; /* all set */
1422 for( i = 0; i < 16 + 2*4; i++ )
1423 non_zero_count[i] = 16;
1427 /* save non zero count */
1428 for( y = 0; y < 4; y++ )
1429 *(uint32_t*)&non_zero_count[y*4] = *(uint32_t*)&h->mb.cache.non_zero_count[x264_scan8[0]+y*8];
1430 for( y = 0; y < 4; y++ )
1431 *(uint16_t*)&non_zero_count[16+y*2] = *(uint32_t*)&h->mb.cache.non_zero_count[x264_scan8[16+y*2]-1] >> 8;
1435 if( h->mb.i_cbp_luma == 0 && h->mb.i_type != I_8x8 )
1436 h->mb.b_transform_8x8 = 0;
1437 h->mb.mb_transform_size[i_mb_xy] = h->mb.b_transform_8x8;
1439 if( !IS_INTRA( i_mb_type ) )
1441 h->mb.ref[0][i_mb_8x8+0+0*s8x8] = h->mb.cache.ref[0][x264_scan8[0]];
1442 h->mb.ref[0][i_mb_8x8+1+0*s8x8] = h->mb.cache.ref[0][x264_scan8[4]];
1443 h->mb.ref[0][i_mb_8x8+0+1*s8x8] = h->mb.cache.ref[0][x264_scan8[8]];
1444 h->mb.ref[0][i_mb_8x8+1+1*s8x8] = h->mb.cache.ref[0][x264_scan8[12]];
1445 for( y = 0; y < 4; y++ )
1447 *(uint64_t*)h->mb.mv[0][i_mb_4x4+y*s4x4+0] = *(uint64_t*)h->mb.cache.mv[0][x264_scan8[0]+8*y+0];
1448 *(uint64_t*)h->mb.mv[0][i_mb_4x4+y*s4x4+2] = *(uint64_t*)h->mb.cache.mv[0][x264_scan8[0]+8*y+2];
1450 if(h->sh.i_type == SLICE_TYPE_B)
1452 h->mb.ref[1][i_mb_8x8+0+0*s8x8] = h->mb.cache.ref[1][x264_scan8[0]];
1453 h->mb.ref[1][i_mb_8x8+1+0*s8x8] = h->mb.cache.ref[1][x264_scan8[4]];
1454 h->mb.ref[1][i_mb_8x8+0+1*s8x8] = h->mb.cache.ref[1][x264_scan8[8]];
1455 h->mb.ref[1][i_mb_8x8+1+1*s8x8] = h->mb.cache.ref[1][x264_scan8[12]];
1456 for( y = 0; y < 4; y++ )
1458 *(uint64_t*)h->mb.mv[1][i_mb_4x4+y*s4x4+0] = *(uint64_t*)h->mb.cache.mv[1][x264_scan8[0]+8*y+0];
1459 *(uint64_t*)h->mb.mv[1][i_mb_4x4+y*s4x4+2] = *(uint64_t*)h->mb.cache.mv[1][x264_scan8[0]+8*y+2];
1466 for( i_list = 0; i_list < (h->sh.i_type == SLICE_TYPE_B ? 2 : 1 ); i_list++ )
1468 *(uint16_t*)&h->mb.ref[i_list][i_mb_8x8+0*s8x8] = (uint8_t)(-1) * 0x0101;
1469 *(uint16_t*)&h->mb.ref[i_list][i_mb_8x8+1*s8x8] = (uint8_t)(-1) * 0x0101;
1470 for( y = 0; y < 4; y++ )
1472 *(uint64_t*)h->mb.mv[i_list][i_mb_4x4+y*s4x4+0] = 0;
1473 *(uint64_t*)h->mb.mv[i_list][i_mb_4x4+y*s4x4+2] = 0;
1478 if( h->param.b_cabac )
1480 if( i_mb_type == I_4x4 || i_mb_type == I_16x16 )
1481 h->mb.chroma_pred_mode[i_mb_xy] = x264_mb_pred_mode8x8c_fix[ h->mb.i_chroma_pred_mode ];
1483 h->mb.chroma_pred_mode[i_mb_xy] = I_PRED_CHROMA_DC;
1485 if( !IS_INTRA( i_mb_type ) && !IS_SKIP( i_mb_type ) && !IS_DIRECT( i_mb_type ) )
1487 for( y = 0; y < 4; y++ )
1489 *(uint64_t*)h->mb.mvd[0][i_mb_4x4+y*s4x4+0] = *(uint64_t*)h->mb.cache.mvd[0][x264_scan8[0]+8*y+0];
1490 *(uint64_t*)h->mb.mvd[0][i_mb_4x4+y*s4x4+2] = *(uint64_t*)h->mb.cache.mvd[0][x264_scan8[0]+8*y+2];
1492 if( h->sh.i_type == SLICE_TYPE_B )
1493 for( y = 0; y < 4; y++ )
1495 *(uint64_t*)h->mb.mvd[1][i_mb_4x4+y*s4x4+0] = *(uint64_t*)h->mb.cache.mvd[1][x264_scan8[0]+8*y+0];
1496 *(uint64_t*)h->mb.mvd[1][i_mb_4x4+y*s4x4+2] = *(uint64_t*)h->mb.cache.mvd[1][x264_scan8[0]+8*y+2];
1501 for( y = 0; y < 4; y++ )
1503 *(uint64_t*)h->mb.mvd[0][i_mb_4x4+y*s4x4+0] = 0;
1504 *(uint64_t*)h->mb.mvd[0][i_mb_4x4+y*s4x4+2] = 0;
1506 if( h->sh.i_type == SLICE_TYPE_B )
1507 for( y = 0; y < 4; y++ )
1509 *(uint64_t*)h->mb.mvd[1][i_mb_4x4+y*s4x4+0] = 0;
1510 *(uint64_t*)h->mb.mvd[1][i_mb_4x4+y*s4x4+2] = 0;
1514 if( h->sh.i_type == SLICE_TYPE_B )
1516 if( i_mb_type == B_SKIP || i_mb_type == B_DIRECT )
1517 h->mb.skipbp[i_mb_xy] = 0xf;
1518 else if( i_mb_type == B_8x8 )
1521 for( i = 0; i < 4; i++ )
1522 skipbp |= ( h->mb.i_sub_partition[i] == D_DIRECT_8x8 ) << i;
1523 h->mb.skipbp[i_mb_xy] = skipbp;
1526 h->mb.skipbp[i_mb_xy] = 0;
1531 void x264_macroblock_bipred_init( x264_t *h )
1534 for( i_ref0 = 0; i_ref0 < h->i_ref0; i_ref0++ )
1536 int poc0 = h->fref0[i_ref0]->i_poc;
1537 for( i_ref1 = 0; i_ref1 < h->i_ref1; i_ref1++ )
1539 int dist_scale_factor;
1540 int poc1 = h->fref1[i_ref1]->i_poc;
1541 int td = x264_clip3( poc1 - poc0, -128, 127 );
1542 if( td == 0 /* || pic0 is a long-term ref */ )
1543 dist_scale_factor = 256;
1546 int tb = x264_clip3( h->fdec->i_poc - poc0, -128, 127 );
1547 int tx = (16384 + (abs(td) >> 1)) / td;
1548 dist_scale_factor = x264_clip3( (tb * tx + 32) >> 6, -1024, 1023 );
1550 h->mb.dist_scale_factor[i_ref0][i_ref1] = dist_scale_factor;
1552 dist_scale_factor >>= 2;
1553 if( h->param.analyse.b_weighted_bipred
1554 && dist_scale_factor >= -64
1555 && dist_scale_factor <= 128 )
1556 h->mb.bipred_weight[i_ref0][i_ref1] = 64 - dist_scale_factor;
1558 h->mb.bipred_weight[i_ref0][i_ref1] = 32;
1563 for( i_ref0 = 2*h->i_ref0-1; i_ref0 >= 0; i_ref0-- )
1564 for( i_ref1 = 2*h->i_ref1-1; i_ref1 >= 0; i_ref1-- )
1565 h->mb.bipred_weight[i_ref0][i_ref1] = h->mb.bipred_weight[i_ref0>>1][i_ref1>>1];