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 /* This just improves encoder performance, it's not part of the spec */
484 void x264_mb_predict_mv_ref16x16( x264_t *h, int i_list, int i_ref, int16_t mvc[8][2], int *i_mvc )
486 int16_t (*mvr)[2] = h->mb.mvr[i_list][i_ref];
489 #define SET_MVP(mvp) { \
490 *(uint32_t*)mvc[i] = *(uint32_t*)mvp; \
495 if( h->sh.i_type == SLICE_TYPE_B
496 && h->mb.cache.ref[i_list][x264_scan8[12]] == i_ref )
498 SET_MVP( h->mb.cache.mv[i_list][x264_scan8[12]] );
501 /* spatial predictors */
502 if( h->mb.i_neighbour & MB_LEFT )
504 int i_mb_l = h->mb.i_mb_xy - 1;
505 /* skip MBs didn't go through the whole search process, so mvr is undefined */
506 if( !IS_SKIP( h->mb.type[i_mb_l] ) )
507 SET_MVP( mvr[i_mb_l] );
509 if( h->mb.i_neighbour & MB_TOP )
511 int i_mb_t = h->mb.i_mb_top_xy;
512 if( !IS_SKIP( h->mb.type[i_mb_t] ) )
513 SET_MVP( mvr[i_mb_t] );
515 if( h->mb.i_neighbour & MB_TOPLEFT && !IS_SKIP( h->mb.type[i_mb_t - 1] ) )
516 SET_MVP( mvr[i_mb_t-1] );
517 if( h->mb.i_mb_x < h->mb.i_mb_stride - 1 && !IS_SKIP( h->mb.type[i_mb_t + 1] ) )
518 SET_MVP( mvr[i_mb_t+1] );
522 /* temporal predictors */
523 /* FIXME temporal scaling w/ interlace */
524 if( h->fref0[0]->i_ref[0] > 0 && !h->sh.b_mbaff )
526 x264_frame_t *l0 = h->fref0[0];
528 #define SET_TMVP(dx, dy) { \
529 int i_b4 = h->mb.i_b4_xy + dx*4 + dy*4*h->mb.i_b4_stride; \
530 int i_b8 = h->mb.i_b8_xy + dx*2 + dy*2*h->mb.i_b8_stride; \
531 int ref_col = l0->ref[0][i_b8]; \
534 int scale = (h->fdec->i_poc - h->fdec->ref_poc[0][i_ref]) * l0->inv_ref_poc[ref_col];\
535 mvc[i][0] = (l0->mv[0][i_b4][0]*scale + 128) >> 8;\
536 mvc[i][1] = (l0->mv[0][i_b4][1]*scale + 128) >> 8;\
542 if( h->mb.i_mb_x < h->sps->i_mb_width-1 )
544 if( h->mb.i_mb_y < h->sps->i_mb_height-1 )
550 *(uint32_t*)mvc[i] = 0;
555 /* Set up a lookup table for delta pocs to reduce an IDIV to an IMUL */
556 static void setup_inverse_delta_pocs( x264_t *h )
559 for( i = 0; i < h->i_ref0; i++ )
561 int delta = h->fdec->i_poc - h->fref0[i]->i_poc;
562 h->fdec->inv_ref_poc[i] = (256 + delta/2) / delta;
566 static inline void x264_mb_mc_0xywh( x264_t *h, int x, int y, int width, int height )
568 const int i8 = x264_scan8[0]+x+8*y;
569 const int i_ref = h->mb.cache.ref[0][i8];
570 const int mvx = x264_clip3( h->mb.cache.mv[0][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] );
571 int mvy = x264_clip3( h->mb.cache.mv[0][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] );
573 h->mc.mc_luma( &h->mb.pic.p_fdec[0][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE,
574 h->mb.pic.p_fref[0][i_ref], h->mb.pic.i_stride[0],
575 mvx + 4*4*x, mvy + 4*4*y, 4*width, 4*height );
577 // chroma is offset if MCing from a field of opposite parity
578 if( h->mb.b_interlaced & i_ref )
579 mvy += (h->mb.i_mb_y & 1)*4 - 2;
581 h->mc.mc_chroma( &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
582 &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],
583 mvx, mvy, 2*width, 2*height );
585 h->mc.mc_chroma( &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
586 &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],
587 mvx, mvy, 2*width, 2*height );
589 static inline void x264_mb_mc_1xywh( x264_t *h, int x, int y, int width, int height )
591 const int i8 = x264_scan8[0]+x+8*y;
592 const int i_ref = h->mb.cache.ref[1][i8];
593 const int mvx = x264_clip3( h->mb.cache.mv[1][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] );
594 int mvy = x264_clip3( h->mb.cache.mv[1][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] );
596 h->mc.mc_luma( &h->mb.pic.p_fdec[0][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE,
597 h->mb.pic.p_fref[1][i_ref], h->mb.pic.i_stride[0],
598 mvx + 4*4*x, mvy + 4*4*y, 4*width, 4*height );
600 if( h->mb.b_interlaced & i_ref )
601 mvy += (h->mb.i_mb_y & 1)*4 - 2;
603 h->mc.mc_chroma( &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
604 &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],
605 mvx, mvy, 2*width, 2*height );
607 h->mc.mc_chroma( &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
608 &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],
609 mvx, mvy, 2*width, 2*height );
612 static inline void x264_mb_mc_01xywh( x264_t *h, int x, int y, int width, int height )
614 const int i8 = x264_scan8[0]+x+8*y;
616 const int i_ref1 = h->mb.cache.ref[1][i8];
617 const int mvx1 = x264_clip3( h->mb.cache.mv[1][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] );
618 int mvy1 = x264_clip3( h->mb.cache.mv[1][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] );
619 DECLARE_ALIGNED_16( uint8_t tmp[16*16] );
620 int i_mode = x264_size2pixel[height][width];
622 x264_mb_mc_0xywh( h, x, y, width, height );
624 h->mc.mc_luma( tmp, 16, h->mb.pic.p_fref[1][i_ref1], h->mb.pic.i_stride[0],
625 mvx1 + 4*4*x, mvy1 + 4*4*y, 4*width, 4*height );
627 if( h->mb.b_interlaced & i_ref1 )
628 mvy1 += (h->mb.i_mb_y & 1)*4 - 2;
630 if( h->param.analyse.b_weighted_bipred )
632 const int i_ref0 = h->mb.cache.ref[0][i8];
633 const int weight = h->mb.bipred_weight[i_ref0][i_ref1];
635 h->mc.avg_weight[i_mode]( &h->mb.pic.p_fdec[0][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE, tmp, 16, weight );
637 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],
638 mvx1, mvy1, 2*width, 2*height );
639 h->mc.avg_weight[i_mode+3]( &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE, tmp, 16, weight );
641 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],
642 mvx1, mvy1, 2*width, 2*height );
643 h->mc.avg_weight[i_mode+3]( &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE, tmp, 16, weight );
647 h->mc.avg[i_mode]( &h->mb.pic.p_fdec[0][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE, tmp, 16 );
649 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],
650 mvx1, mvy1, 2*width, 2*height );
651 h->mc.avg[i_mode+3]( &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE, tmp, 16 );
653 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],
654 mvx1, mvy1, 2*width, 2*height );
655 h->mc.avg[i_mode+3]( &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE, tmp, 16 );
659 static void x264_mb_mc_direct8x8( x264_t *h, int x, int y )
661 const int i8 = x264_scan8[0] + x + 8*y;
663 /* FIXME: optimize based on current block size, not global settings? */
664 if( h->sps->b_direct8x8_inference )
666 if( h->mb.cache.ref[0][i8] >= 0 )
667 if( h->mb.cache.ref[1][i8] >= 0 )
668 x264_mb_mc_01xywh( h, x, y, 2, 2 );
670 x264_mb_mc_0xywh( h, x, y, 2, 2 );
672 x264_mb_mc_1xywh( h, x, y, 2, 2 );
676 if( h->mb.cache.ref[0][i8] >= 0 )
678 if( h->mb.cache.ref[1][i8] >= 0 )
680 x264_mb_mc_01xywh( h, x+0, y+0, 1, 1 );
681 x264_mb_mc_01xywh( h, x+1, y+0, 1, 1 );
682 x264_mb_mc_01xywh( h, x+0, y+1, 1, 1 );
683 x264_mb_mc_01xywh( h, x+1, y+1, 1, 1 );
687 x264_mb_mc_0xywh( h, x+0, y+0, 1, 1 );
688 x264_mb_mc_0xywh( h, x+1, y+0, 1, 1 );
689 x264_mb_mc_0xywh( h, x+0, y+1, 1, 1 );
690 x264_mb_mc_0xywh( h, x+1, y+1, 1, 1 );
695 x264_mb_mc_1xywh( h, x+0, y+0, 1, 1 );
696 x264_mb_mc_1xywh( h, x+1, y+0, 1, 1 );
697 x264_mb_mc_1xywh( h, x+0, y+1, 1, 1 );
698 x264_mb_mc_1xywh( h, x+1, y+1, 1, 1 );
703 void x264_mb_mc_8x8( x264_t *h, int i8 )
705 const int x = 2*(i8&1);
706 const int y = 2*(i8>>1);
707 switch( h->mb.i_sub_partition[i8] )
710 x264_mb_mc_0xywh( h, x, y, 2, 2 );
713 x264_mb_mc_0xywh( h, x, y+0, 2, 1 );
714 x264_mb_mc_0xywh( h, x, y+1, 2, 1 );
717 x264_mb_mc_0xywh( h, x+0, y, 1, 2 );
718 x264_mb_mc_0xywh( h, x+1, y, 1, 2 );
721 x264_mb_mc_0xywh( h, x+0, y+0, 1, 1 );
722 x264_mb_mc_0xywh( h, x+1, y+0, 1, 1 );
723 x264_mb_mc_0xywh( h, x+0, y+1, 1, 1 );
724 x264_mb_mc_0xywh( h, x+1, y+1, 1, 1 );
727 x264_mb_mc_1xywh( h, x, y, 2, 2 );
730 x264_mb_mc_1xywh( h, x, y+0, 2, 1 );
731 x264_mb_mc_1xywh( h, x, y+1, 2, 1 );
734 x264_mb_mc_1xywh( h, x+0, y, 1, 2 );
735 x264_mb_mc_1xywh( h, x+1, y, 1, 2 );
738 x264_mb_mc_1xywh( h, x+0, y+0, 1, 1 );
739 x264_mb_mc_1xywh( h, x+1, y+0, 1, 1 );
740 x264_mb_mc_1xywh( h, x+0, y+1, 1, 1 );
741 x264_mb_mc_1xywh( h, x+1, y+1, 1, 1 );
744 x264_mb_mc_01xywh( h, x, y, 2, 2 );
747 x264_mb_mc_01xywh( h, x, y+0, 2, 1 );
748 x264_mb_mc_01xywh( h, x, y+1, 2, 1 );
751 x264_mb_mc_01xywh( h, x+0, y, 1, 2 );
752 x264_mb_mc_01xywh( h, x+1, y, 1, 2 );
755 x264_mb_mc_01xywh( h, x+0, y+0, 1, 1 );
756 x264_mb_mc_01xywh( h, x+1, y+0, 1, 1 );
757 x264_mb_mc_01xywh( h, x+0, y+1, 1, 1 );
758 x264_mb_mc_01xywh( h, x+1, y+1, 1, 1 );
761 x264_mb_mc_direct8x8( h, x, y );
766 void x264_mb_mc( x264_t *h )
768 if( h->mb.i_type == P_L0 )
770 if( h->mb.i_partition == D_16x16 )
772 x264_mb_mc_0xywh( h, 0, 0, 4, 4 );
774 else if( h->mb.i_partition == D_16x8 )
776 x264_mb_mc_0xywh( h, 0, 0, 4, 2 );
777 x264_mb_mc_0xywh( h, 0, 2, 4, 2 );
779 else if( h->mb.i_partition == D_8x16 )
781 x264_mb_mc_0xywh( h, 0, 0, 2, 4 );
782 x264_mb_mc_0xywh( h, 2, 0, 2, 4 );
785 else if( h->mb.i_type == P_8x8 || h->mb.i_type == B_8x8 )
788 for( i = 0; i < 4; i++ )
789 x264_mb_mc_8x8( h, i );
791 else if( h->mb.i_type == B_SKIP || h->mb.i_type == B_DIRECT )
793 x264_mb_mc_direct8x8( h, 0, 0 );
794 x264_mb_mc_direct8x8( h, 2, 0 );
795 x264_mb_mc_direct8x8( h, 0, 2 );
796 x264_mb_mc_direct8x8( h, 2, 2 );
805 /* init ref list utilisations */
806 for( i = 0; i < 2; i++ )
808 b_list0[i] = x264_mb_type_list0_table[h->mb.i_type][i];
809 b_list1[i] = x264_mb_type_list1_table[h->mb.i_type][i];
811 if( h->mb.i_partition == D_16x16 )
813 if( b_list0[0] && b_list1[0] ) x264_mb_mc_01xywh( h, 0, 0, 4, 4 );
814 else if( b_list0[0] ) x264_mb_mc_0xywh ( h, 0, 0, 4, 4 );
815 else if( b_list1[0] ) x264_mb_mc_1xywh ( h, 0, 0, 4, 4 );
817 else if( h->mb.i_partition == D_16x8 )
819 if( b_list0[0] && b_list1[0] ) x264_mb_mc_01xywh( h, 0, 0, 4, 2 );
820 else if( b_list0[0] ) x264_mb_mc_0xywh ( h, 0, 0, 4, 2 );
821 else if( b_list1[0] ) x264_mb_mc_1xywh ( h, 0, 0, 4, 2 );
823 if( b_list0[1] && b_list1[1] ) x264_mb_mc_01xywh( h, 0, 2, 4, 2 );
824 else if( b_list0[1] ) x264_mb_mc_0xywh ( h, 0, 2, 4, 2 );
825 else if( b_list1[1] ) x264_mb_mc_1xywh ( h, 0, 2, 4, 2 );
827 else if( h->mb.i_partition == D_8x16 )
829 if( b_list0[0] && b_list1[0] ) x264_mb_mc_01xywh( h, 0, 0, 2, 4 );
830 else if( b_list0[0] ) x264_mb_mc_0xywh ( h, 0, 0, 2, 4 );
831 else if( b_list1[0] ) x264_mb_mc_1xywh ( h, 0, 0, 2, 4 );
833 if( b_list0[1] && b_list1[1] ) x264_mb_mc_01xywh( h, 2, 0, 2, 4 );
834 else if( b_list0[1] ) x264_mb_mc_0xywh ( h, 2, 0, 2, 4 );
835 else if( b_list1[1] ) x264_mb_mc_1xywh ( h, 2, 0, 2, 4 );
840 int x264_macroblock_cache_init( x264_t *h )
843 int i_mb_count = h->mb.i_mb_count;
845 h->mb.i_mb_stride = h->sps->i_mb_width;
846 h->mb.i_b8_stride = h->sps->i_mb_width * 2;
847 h->mb.i_b4_stride = h->sps->i_mb_width * 4;
849 h->mb.b_interlaced = h->param.b_interlaced;
851 CHECKED_MALLOC( h->mb.qp, i_mb_count * sizeof(int8_t) );
852 CHECKED_MALLOC( h->mb.cbp, i_mb_count * sizeof(int16_t) );
853 CHECKED_MALLOC( h->mb.skipbp, i_mb_count * sizeof(int8_t) );
854 CHECKED_MALLOC( h->mb.mb_transform_size, i_mb_count * sizeof(int8_t) );
856 /* 0 -> 3 top(4), 4 -> 6 : left(3) */
857 CHECKED_MALLOC( h->mb.intra4x4_pred_mode, i_mb_count * 8 * sizeof(int8_t) );
860 CHECKED_MALLOC( h->mb.non_zero_count, i_mb_count * 24 * sizeof(uint8_t) );
861 CHECKED_MALLOC( h->mb.nnz_backup, h->sps->i_mb_width * 4 * 16 * sizeof(uint8_t) );
863 if( h->param.b_cabac )
865 CHECKED_MALLOC( h->mb.chroma_pred_mode, i_mb_count * sizeof(int8_t) );
866 CHECKED_MALLOC( h->mb.mvd[0], 2*16 * i_mb_count * sizeof(int16_t) );
867 CHECKED_MALLOC( h->mb.mvd[1], 2*16 * i_mb_count * sizeof(int16_t) );
872 int i_refs = X264_MIN(16, (i ? 1 : h->param.i_frame_reference) + h->param.b_bframe_pyramid) << h->param.b_interlaced;
873 for( j=0; j < i_refs; j++ )
874 CHECKED_MALLOC( h->mb.mvr[i][j], 2 * i_mb_count * sizeof(int16_t) );
877 for( i=0; i<=h->param.b_interlaced; i++ )
880 CHECKED_MALLOC( h->mb.intra_border_backup[i][j], h->fdec->i_stride[j] );
881 h->mb.intra_border_backup[i][j] += 8;
884 /* init with not available (for top right idx=7,15) */
885 memset( h->mb.cache.ref[0], -2, X264_SCAN8_SIZE * sizeof( int8_t ) );
886 memset( h->mb.cache.ref[1], -2, X264_SCAN8_SIZE * sizeof( int8_t ) );
891 void x264_macroblock_cache_end( x264_t *h )
894 for( i=0; i<=h->param.b_interlaced; i++ )
896 x264_free( h->mb.intra_border_backup[i][j] - 8 );
898 for( j=0; j<32; j++ )
899 x264_free( h->mb.mvr[i][j] );
900 if( h->param.b_cabac )
902 x264_free( h->mb.chroma_pred_mode );
903 x264_free( h->mb.mvd[0] );
904 x264_free( h->mb.mvd[1] );
906 x264_free( h->mb.intra4x4_pred_mode );
907 x264_free( h->mb.non_zero_count );
908 x264_free( h->mb.nnz_backup );
909 x264_free( h->mb.mb_transform_size );
910 x264_free( h->mb.skipbp );
911 x264_free( h->mb.cbp );
912 x264_free( h->mb.qp );
914 void x264_macroblock_slice_init( x264_t *h )
918 h->mb.mv[0] = h->fdec->mv[0];
919 h->mb.mv[1] = h->fdec->mv[1];
920 h->mb.ref[0] = h->fdec->ref[0];
921 h->mb.ref[1] = h->fdec->ref[1];
922 h->mb.type = h->fdec->mb_type;
924 h->fdec->i_ref[0] = h->i_ref0;
925 h->fdec->i_ref[1] = h->i_ref1;
926 for( i = 0; i < h->i_ref0; i++ )
927 h->fdec->ref_poc[0][i] = h->fref0[i]->i_poc;
928 if( h->sh.i_type == SLICE_TYPE_B )
930 for( i = 0; i < h->i_ref1; i++ )
931 h->fdec->ref_poc[1][i] = h->fref1[i]->i_poc;
933 h->mb.map_col_to_list0[-1] = -1;
934 h->mb.map_col_to_list0[-2] = -2;
935 for( i = 0; i < h->fref1[0]->i_ref[0]; i++ )
937 int poc = h->fref1[0]->ref_poc[0][i];
938 h->mb.map_col_to_list0[i] = -2;
939 for( j = 0; j < h->i_ref0; j++ )
940 if( h->fref0[j]->i_poc == poc )
942 h->mb.map_col_to_list0[i] = j;
947 if( h->sh.i_type == SLICE_TYPE_P )
948 memset( h->mb.cache.skip, 0, X264_SCAN8_SIZE * sizeof( int8_t ) );
950 setup_inverse_delta_pocs( h );
953 void x264_prefetch_fenc( x264_t *h, x264_frame_t *fenc, int i_mb_x, int i_mb_y )
955 int stride_y = fenc->i_stride[0];
956 int stride_uv = fenc->i_stride[1];
957 int off_y = 16 * (i_mb_x + i_mb_y * stride_y);
958 int off_uv = 8 * (i_mb_x + i_mb_y * stride_uv);
959 h->mc.prefetch_fenc( fenc->plane[0]+off_y, stride_y,
960 fenc->plane[1+(i_mb_x&1)]+off_uv, stride_uv, i_mb_x );
963 static NOINLINE void copy_column8( uint8_t *dst, uint8_t *src )
967 dst[i*FDEC_STRIDE] = src[i*FDEC_STRIDE];
970 static void ALWAYS_INLINE x264_macroblock_load_pic_pointers( x264_t *h, int i_mb_x, int i_mb_y, int i)
972 const int w = (i == 0 ? 16 : 8);
973 const int i_stride = h->fdec->i_stride[i];
974 const int i_stride2 = i_stride << h->mb.b_interlaced;
975 const int i_pix_offset = h->mb.b_interlaced
976 ? w * (i_mb_x + (i_mb_y&~1) * i_stride) + (i_mb_y&1) * i_stride
977 : w * (i_mb_x + i_mb_y * i_stride);
978 int ref_pix_offset[2] = { i_pix_offset, i_pix_offset };
979 const uint8_t *intra_fdec = &h->mb.intra_border_backup[i_mb_y & h->sh.b_mbaff][i][i_mb_x*16>>!!i];
980 x264_frame_t **fref[2] = { h->fref0, h->fref1 };
982 if( h->mb.b_interlaced )
983 ref_pix_offset[1] += (1-2*(i_mb_y&1)) * i_stride;
984 h->mb.pic.i_stride[i] = i_stride2;
985 h->mc.copy[i?PIXEL_8x8:PIXEL_16x16]( h->mb.pic.p_fenc[i], FENC_STRIDE,
986 &h->fenc->plane[i][i_pix_offset], i_stride2, w );
987 memcpy( &h->mb.pic.p_fdec[i][-1-FDEC_STRIDE], intra_fdec-1, w*3/2+1 );
988 if( h->mb.b_interlaced )
990 const uint8_t *plane_fdec = &h->fdec->plane[i][i_pix_offset];
991 for( j = 0; j < w; j++ )
992 h->mb.pic.p_fdec[i][-1+j*FDEC_STRIDE] = plane_fdec[-1+j*i_stride2];
994 for( j = 0; j < h->mb.pic.i_fref[0]; j++ )
996 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]];
998 for( k = 1; k < 4; k++ )
999 h->mb.pic.p_fref[0][j][k] = &fref[0][j >> h->mb.b_interlaced]->filtered[k][ref_pix_offset[j&1]];
1001 if( h->sh.i_type == SLICE_TYPE_B )
1002 for( j = 0; j < h->mb.pic.i_fref[1]; j++ )
1004 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]];
1006 for( k = 1; k < 4; k++ )
1007 h->mb.pic.p_fref[1][j][k] = &fref[1][j >> h->mb.b_interlaced]->filtered[k][ref_pix_offset[j&1]];
1011 void x264_macroblock_cache_load( x264_t *h, int i_mb_x, int i_mb_y )
1013 int i_mb_xy = i_mb_y * h->mb.i_mb_stride + i_mb_x;
1014 int i_mb_4x4 = 4*(i_mb_y * h->mb.i_b4_stride + i_mb_x);
1015 int i_mb_8x8 = 2*(i_mb_y * h->mb.i_b8_stride + i_mb_x);
1016 int i_top_y = i_mb_y - (1 << h->mb.b_interlaced);
1017 int i_top_xy = i_top_y * h->mb.i_mb_stride + i_mb_x;
1018 int i_top_4x4 = (4*i_top_y+3) * h->mb.i_b4_stride + 4*i_mb_x;
1019 int i_top_8x8 = (2*i_top_y+1) * h->mb.i_b8_stride + 2*i_mb_x;
1021 int i_top_type = -1; /* gcc warn */
1022 int i_left_type= -1;
1026 assert( h->mb.i_b8_stride == 2*h->mb.i_mb_stride );
1027 assert( h->mb.i_b4_stride == 4*h->mb.i_mb_stride );
1030 h->mb.i_mb_x = i_mb_x;
1031 h->mb.i_mb_y = i_mb_y;
1032 h->mb.i_mb_xy = i_mb_xy;
1033 h->mb.i_b8_xy = i_mb_8x8;
1034 h->mb.i_b4_xy = i_mb_4x4;
1035 h->mb.i_mb_top_xy = i_top_xy;
1036 h->mb.i_neighbour = 0;
1039 if( i_top_xy >= h->sh.i_first_mb )
1041 h->mb.i_mb_type_top =
1042 i_top_type= h->mb.type[i_top_xy];
1044 h->mb.i_neighbour |= MB_TOP;
1047 *(uint32_t*)&h->mb.cache.intra4x4_pred_mode[x264_scan8[0] - 8] = *(uint32_t*)&h->mb.intra4x4_pred_mode[i_top_xy][0];
1049 /* load non_zero_count */
1050 *(uint32_t*)&h->mb.cache.non_zero_count[x264_scan8[0] - 8] = *(uint32_t*)&h->mb.non_zero_count[i_top_xy][12];
1051 /* shift because x264_scan8[16] is misaligned */
1052 *(uint32_t*)&h->mb.cache.non_zero_count[x264_scan8[16] - 9] = *(uint16_t*)&h->mb.non_zero_count[i_top_xy][18] << 8;
1053 *(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;
1057 h->mb.i_mb_type_top = -1;
1060 h->mb.cache.intra4x4_pred_mode[x264_scan8[0] - 8] =
1061 h->mb.cache.intra4x4_pred_mode[x264_scan8[1] - 8] =
1062 h->mb.cache.intra4x4_pred_mode[x264_scan8[4] - 8] =
1063 h->mb.cache.intra4x4_pred_mode[x264_scan8[5] - 8] = -1;
1065 /* load non_zero_count */
1066 h->mb.cache.non_zero_count[x264_scan8[0] - 8] =
1067 h->mb.cache.non_zero_count[x264_scan8[1] - 8] =
1068 h->mb.cache.non_zero_count[x264_scan8[4] - 8] =
1069 h->mb.cache.non_zero_count[x264_scan8[5] - 8] =
1070 h->mb.cache.non_zero_count[x264_scan8[16+0] - 8] =
1071 h->mb.cache.non_zero_count[x264_scan8[16+1] - 8] =
1072 h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 8] =
1073 h->mb.cache.non_zero_count[x264_scan8[16+4+1] - 8] = 0x80;
1076 if( i_mb_x > 0 && i_mb_xy > h->sh.i_first_mb )
1078 i_left_xy = i_mb_xy - 1;
1079 h->mb.i_mb_type_left =
1080 i_left_type = h->mb.type[i_left_xy];
1082 h->mb.i_neighbour |= MB_LEFT;
1085 h->mb.cache.intra4x4_pred_mode[x264_scan8[0 ] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][4];
1086 h->mb.cache.intra4x4_pred_mode[x264_scan8[2 ] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][5];
1087 h->mb.cache.intra4x4_pred_mode[x264_scan8[8 ] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][6];
1088 h->mb.cache.intra4x4_pred_mode[x264_scan8[10] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][3];
1090 /* load non_zero_count */
1091 h->mb.cache.non_zero_count[x264_scan8[0 ] - 1] = h->mb.non_zero_count[i_left_xy][3];
1092 h->mb.cache.non_zero_count[x264_scan8[2 ] - 1] = h->mb.non_zero_count[i_left_xy][7];
1093 h->mb.cache.non_zero_count[x264_scan8[8 ] - 1] = h->mb.non_zero_count[i_left_xy][11];
1094 h->mb.cache.non_zero_count[x264_scan8[10] - 1] = h->mb.non_zero_count[i_left_xy][15];
1096 h->mb.cache.non_zero_count[x264_scan8[16+0] - 1] = h->mb.non_zero_count[i_left_xy][16+1];
1097 h->mb.cache.non_zero_count[x264_scan8[16+2] - 1] = h->mb.non_zero_count[i_left_xy][16+3];
1099 h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 1] = h->mb.non_zero_count[i_left_xy][16+4+1];
1100 h->mb.cache.non_zero_count[x264_scan8[16+4+2] - 1] = h->mb.non_zero_count[i_left_xy][16+4+3];
1104 h->mb.i_mb_type_left = -1;
1106 h->mb.cache.intra4x4_pred_mode[x264_scan8[0 ] - 1] =
1107 h->mb.cache.intra4x4_pred_mode[x264_scan8[2 ] - 1] =
1108 h->mb.cache.intra4x4_pred_mode[x264_scan8[8 ] - 1] =
1109 h->mb.cache.intra4x4_pred_mode[x264_scan8[10] - 1] = -1;
1111 /* load non_zero_count */
1112 h->mb.cache.non_zero_count[x264_scan8[0 ] - 1] =
1113 h->mb.cache.non_zero_count[x264_scan8[2 ] - 1] =
1114 h->mb.cache.non_zero_count[x264_scan8[8 ] - 1] =
1115 h->mb.cache.non_zero_count[x264_scan8[10] - 1] =
1116 h->mb.cache.non_zero_count[x264_scan8[16+0] - 1] =
1117 h->mb.cache.non_zero_count[x264_scan8[16+2] - 1] =
1118 h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 1] =
1119 h->mb.cache.non_zero_count[x264_scan8[16+4+2] - 1] = 0x80;
1122 if( i_mb_x < h->sps->i_mb_width - 1 && i_top_xy + 1 >= h->sh.i_first_mb )
1124 h->mb.i_neighbour |= MB_TOPRIGHT;
1125 h->mb.i_mb_type_topright = h->mb.type[ i_top_xy + 1 ];
1128 h->mb.i_mb_type_topright = -1;
1129 if( i_mb_x > 0 && i_top_xy - 1 >= h->sh.i_first_mb )
1131 h->mb.i_neighbour |= MB_TOPLEFT;
1132 h->mb.i_mb_type_topleft = h->mb.type[ i_top_xy - 1 ];
1135 h->mb.i_mb_type_topleft = -1;
1137 if( h->pps->b_transform_8x8_mode )
1139 h->mb.cache.i_neighbour_transform_size =
1140 ( i_left_type >= 0 && h->mb.mb_transform_size[i_left_xy] )
1141 + ( i_top_type >= 0 && h->mb.mb_transform_size[i_top_xy] );
1146 h->mb.pic.i_fref[0] = h->i_ref0 << h->mb.b_interlaced;
1147 h->mb.pic.i_fref[1] = h->i_ref1 << h->mb.b_interlaced;
1148 h->mb.cache.i_neighbour_interlaced =
1149 !!(h->mb.i_neighbour & MB_LEFT)
1150 + !!(h->mb.i_neighbour & MB_TOP);
1163 h->mb.pic.p_fenc[0] = h->mb.pic.fenc_buf;
1164 h->mb.pic.p_fenc[1] = h->mb.pic.fenc_buf + 16*FENC_STRIDE;
1165 h->mb.pic.p_fenc[2] = h->mb.pic.fenc_buf + 16*FENC_STRIDE + 8;
1166 h->mb.pic.p_fdec[0] = h->mb.pic.fdec_buf + 2*FDEC_STRIDE;
1167 h->mb.pic.p_fdec[1] = h->mb.pic.fdec_buf + 19*FDEC_STRIDE;
1168 h->mb.pic.p_fdec[2] = h->mb.pic.fdec_buf + 19*FDEC_STRIDE + 16;
1170 if( !h->mb.b_interlaced )
1172 copy_column8( h->mb.pic.p_fdec[0]-1, h->mb.pic.p_fdec[0]+15 );
1173 copy_column8( h->mb.pic.p_fdec[0]-1+8*FDEC_STRIDE, h->mb.pic.p_fdec[0]+15+8*FDEC_STRIDE );
1174 copy_column8( h->mb.pic.p_fdec[1]-1, h->mb.pic.p_fdec[1]+7 );
1175 copy_column8( h->mb.pic.p_fdec[2]-1, h->mb.pic.p_fdec[2]+7 );
1178 /* load picture pointers */
1179 x264_macroblock_load_pic_pointers( h, i_mb_x, i_mb_y, 0 );
1180 x264_macroblock_load_pic_pointers( h, i_mb_x, i_mb_y, 1 );
1181 x264_macroblock_load_pic_pointers( h, i_mb_x, i_mb_y, 2 );
1183 if( h->fdec->integral )
1185 assert( !h->mb.b_interlaced );
1186 for( i = 0; i < h->mb.pic.i_fref[0]; i++ )
1187 h->mb.pic.p_integral[0][i] = &h->fref0[i]->integral[ 16 * ( i_mb_x + i_mb_y * h->fdec->i_stride[0] )];
1188 for( i = 0; i < h->mb.pic.i_fref[1]; i++ )
1189 h->mb.pic.p_integral[1][i] = &h->fref1[i]->integral[ 16 * ( i_mb_x + i_mb_y * h->fdec->i_stride[0] )];
1192 x264_prefetch_fenc( h, h->fenc, i_mb_x, i_mb_y );
1194 /* load ref/mv/mvd */
1195 if( h->sh.i_type != SLICE_TYPE_I )
1197 const int s8x8 = h->mb.i_b8_stride;
1198 const int s4x4 = h->mb.i_b4_stride;
1202 for( i_list = 0; i_list < (h->sh.i_type == SLICE_TYPE_B ? 2 : 1 ); i_list++ )
1205 h->mb.cache.ref[i_list][x264_scan8[5 ]+1] =
1206 h->mb.cache.ref[i_list][x264_scan8[7 ]+1] =
1207 h->mb.cache.ref[i_list][x264_scan8[13]+1] = -2;
1210 if( h->mb.i_neighbour & MB_TOPLEFT )
1212 const int i8 = x264_scan8[0] - 1 - 1*8;
1213 const int ir = i_top_8x8 - 1;
1214 const int iv = i_top_4x4 - 1;
1215 h->mb.cache.ref[i_list][i8] = h->mb.ref[i_list][ir];
1216 *(uint32_t*)h->mb.cache.mv[i_list][i8] = *(uint32_t*)h->mb.mv[i_list][iv];
1220 const int i8 = x264_scan8[0] - 1 - 1*8;
1221 h->mb.cache.ref[i_list][i8] = -2;
1222 *(uint32_t*)h->mb.cache.mv[i_list][i8] = 0;
1225 if( h->mb.i_neighbour & MB_TOP )
1227 const int i8 = x264_scan8[0] - 8;
1228 const int ir = i_top_8x8;
1229 const int iv = i_top_4x4;
1230 h->mb.cache.ref[i_list][i8+0] =
1231 h->mb.cache.ref[i_list][i8+1] = h->mb.ref[i_list][ir + 0];
1232 h->mb.cache.ref[i_list][i8+2] =
1233 h->mb.cache.ref[i_list][i8+3] = h->mb.ref[i_list][ir + 1];
1234 *(uint64_t*)h->mb.cache.mv[i_list][i8+0] = *(uint64_t*)h->mb.mv[i_list][iv+0];
1235 *(uint64_t*)h->mb.cache.mv[i_list][i8+2] = *(uint64_t*)h->mb.mv[i_list][iv+2];
1239 const int i8 = x264_scan8[0] - 8;
1240 *(uint64_t*)h->mb.cache.mv[i_list][i8+0] = 0;
1241 *(uint64_t*)h->mb.cache.mv[i_list][i8+2] = 0;
1242 *(uint32_t*)&h->mb.cache.ref[i_list][i8] = (uint8_t)(-2) * 0x01010101U;
1245 if( h->mb.i_neighbour & MB_TOPRIGHT )
1247 const int i8 = x264_scan8[0] + 4 - 1*8;
1248 const int ir = i_top_8x8 + 2;
1249 const int iv = i_top_4x4 + 4;
1250 h->mb.cache.ref[i_list][i8] = h->mb.ref[i_list][ir];
1251 *(uint32_t*)h->mb.cache.mv[i_list][i8] = *(uint32_t*)h->mb.mv[i_list][iv];
1255 const int i8 = x264_scan8[0] + 4 - 1*8;
1256 h->mb.cache.ref[i_list][i8] = -2;
1257 *(uint32_t*)h->mb.cache.mv[i_list][i8] = 0;
1260 if( h->mb.i_neighbour & MB_LEFT )
1262 const int i8 = x264_scan8[0] - 1;
1263 const int ir = i_mb_8x8 - 1;
1264 const int iv = i_mb_4x4 - 1;
1265 h->mb.cache.ref[i_list][i8+0*8] =
1266 h->mb.cache.ref[i_list][i8+1*8] = h->mb.ref[i_list][ir + 0*s8x8];
1267 h->mb.cache.ref[i_list][i8+2*8] =
1268 h->mb.cache.ref[i_list][i8+3*8] = h->mb.ref[i_list][ir + 1*s8x8];
1270 for( i = 0; i < 4; i++ )
1271 *(uint32_t*)h->mb.cache.mv[i_list][i8+i*8] = *(uint32_t*)h->mb.mv[i_list][iv + i*s4x4];
1275 const int i8 = x264_scan8[0] - 1;
1276 for( i = 0; i < 4; i++ )
1278 h->mb.cache.ref[i_list][i8+i*8] = -2;
1279 *(uint32_t*)h->mb.cache.mv[i_list][i8+i*8] = 0;
1283 if( h->param.b_cabac )
1285 if( i_top_type >= 0 )
1287 const int i8 = x264_scan8[0] - 8;
1288 const int iv = i_top_4x4;
1289 *(uint64_t*)h->mb.cache.mvd[i_list][i8+0] = *(uint64_t*)h->mb.mvd[i_list][iv+0];
1290 *(uint64_t*)h->mb.cache.mvd[i_list][i8+2] = *(uint64_t*)h->mb.mvd[i_list][iv+2];
1294 const int i8 = x264_scan8[0] - 8;
1295 *(uint64_t*)h->mb.cache.mvd[i_list][i8+0] =
1296 *(uint64_t*)h->mb.cache.mvd[i_list][i8+2] = 0;
1299 if( i_left_type >= 0 )
1301 const int i8 = x264_scan8[0] - 1;
1302 const int iv = i_mb_4x4 - 1;
1303 for( i = 0; i < 4; i++ )
1304 *(uint32_t*)h->mb.cache.mvd[i_list][i8+i*8] = *(uint32_t*)h->mb.mvd[i_list][iv + i*s4x4];
1308 const int i8 = x264_scan8[0] - 1;
1309 for( i = 0; i < 4; i++ )
1310 *(uint32_t*)h->mb.cache.mvd[i_list][i8+i*8] = 0;
1316 if( h->sh.i_type == SLICE_TYPE_B && h->param.b_cabac )
1318 memset( h->mb.cache.skip, 0, X264_SCAN8_SIZE * sizeof( int8_t ) );
1319 if( i_left_type >= 0 )
1321 uint8_t skipbp = h->mb.skipbp[i_left_xy];
1322 h->mb.cache.skip[x264_scan8[0] - 1] = skipbp & 0x2;
1323 h->mb.cache.skip[x264_scan8[8] - 1] = skipbp & 0x8;
1325 if( i_top_type >= 0 )
1327 uint8_t skipbp = h->mb.skipbp[i_top_xy];
1328 h->mb.cache.skip[x264_scan8[0] - 8] = skipbp & 0x4;
1329 h->mb.cache.skip[x264_scan8[4] - 8] = skipbp & 0x8;
1333 if( h->sh.i_type == SLICE_TYPE_P )
1334 x264_mb_predict_mv_pskip( h, h->mb.cache.pskip_mv );
1337 h->mb.i_neighbour4[0] =
1338 h->mb.i_neighbour8[0] = (h->mb.i_neighbour & (MB_TOP|MB_LEFT|MB_TOPLEFT))
1339 | ((h->mb.i_neighbour & MB_TOP) ? MB_TOPRIGHT : 0);
1340 h->mb.i_neighbour4[4] =
1341 h->mb.i_neighbour4[1] = MB_LEFT | ((h->mb.i_neighbour & MB_TOP) ? (MB_TOP|MB_TOPLEFT|MB_TOPRIGHT) : 0);
1342 h->mb.i_neighbour4[2] =
1343 h->mb.i_neighbour4[8] =
1344 h->mb.i_neighbour4[10] =
1345 h->mb.i_neighbour8[2] = MB_TOP|MB_TOPRIGHT | ((h->mb.i_neighbour & MB_LEFT) ? (MB_LEFT|MB_TOPLEFT) : 0);
1346 h->mb.i_neighbour4[3] =
1347 h->mb.i_neighbour4[7] =
1348 h->mb.i_neighbour4[11] =
1349 h->mb.i_neighbour4[13] =
1350 h->mb.i_neighbour4[15] =
1351 h->mb.i_neighbour8[3] = MB_LEFT|MB_TOP|MB_TOPLEFT;
1352 h->mb.i_neighbour4[5] =
1353 h->mb.i_neighbour8[1] = MB_LEFT | (h->mb.i_neighbour & MB_TOPRIGHT)
1354 | ((h->mb.i_neighbour & MB_TOP) ? MB_TOP|MB_TOPLEFT : 0);
1355 h->mb.i_neighbour4[6] =
1356 h->mb.i_neighbour4[9] =
1357 h->mb.i_neighbour4[12] =
1358 h->mb.i_neighbour4[14] = MB_LEFT|MB_TOP|MB_TOPLEFT|MB_TOPRIGHT;
1361 static void ALWAYS_INLINE x264_macroblock_store_pic( x264_t *h, int i)
1364 int i_stride = h->fdec->i_stride[i];
1365 int i_stride2 = i_stride << h->mb.b_interlaced;
1366 int i_pix_offset = h->mb.b_interlaced
1367 ? w * (h->mb.i_mb_x + (h->mb.i_mb_y&~1) * i_stride) + (h->mb.i_mb_y&1) * i_stride
1368 : w * (h->mb.i_mb_x + h->mb.i_mb_y * i_stride);
1369 h->mc.copy[i?PIXEL_8x8:PIXEL_16x16](
1370 &h->fdec->plane[i][i_pix_offset], i_stride2,
1371 h->mb.pic.p_fdec[i], FDEC_STRIDE, w );
1374 void x264_macroblock_cache_save( x264_t *h )
1376 const int i_mb_xy = h->mb.i_mb_xy;
1377 const int i_mb_type = x264_mb_type_fix[h->mb.i_type];
1378 const int s8x8 = h->mb.i_b8_stride;
1379 const int s4x4 = h->mb.i_b4_stride;
1380 const int i_mb_4x4 = h->mb.i_b4_xy;
1381 const int i_mb_8x8 = h->mb.i_b8_xy;
1383 /* GCC pessimizes direct stores to heap-allocated 8-bit arrays due to aliasing.*/
1384 /* By only dereferencing them once, we avoid this issue. */
1385 int8_t *intra4x4_pred_mode = h->mb.intra4x4_pred_mode[i_mb_xy];
1386 uint8_t *non_zero_count = h->mb.non_zero_count[i_mb_xy];
1390 x264_macroblock_store_pic( h, 0 );
1391 x264_macroblock_store_pic( h, 1 );
1392 x264_macroblock_store_pic( h, 2 );
1394 x264_prefetch_fenc( h, h->fdec, h->mb.i_mb_x, h->mb.i_mb_y );
1396 h->mb.type[i_mb_xy] = i_mb_type;
1398 if( h->mb.i_type == I_PCM || (h->mb.i_type != I_16x16 && h->mb.i_cbp_luma == 0 && h->mb.i_cbp_chroma == 0) )
1399 h->mb.i_qp = h->mb.i_last_qp;
1400 h->mb.qp[i_mb_xy] = i_mb_type != I_PCM ? h->mb.i_qp : 0;
1402 h->mb.i_last_dqp = h->mb.i_qp - h->mb.i_last_qp;
1403 h->mb.i_last_qp = h->mb.i_qp;
1404 h->mb.i_mb_prev_xy = h->mb.i_mb_xy;
1407 if( i_mb_type == I_4x4 )
1409 *(uint32_t*)&intra4x4_pred_mode[0] = *(uint32_t*)&h->mb.cache.intra4x4_pred_mode[x264_scan8[10] ];
1410 *(uint32_t*)&intra4x4_pred_mode[4] = pack8to32(h->mb.cache.intra4x4_pred_mode[x264_scan8[5] ],
1411 h->mb.cache.intra4x4_pred_mode[x264_scan8[7] ],
1412 h->mb.cache.intra4x4_pred_mode[x264_scan8[13] ], 0);
1415 *(uint64_t*)intra4x4_pred_mode = I_PRED_4x4_DC * 0x0101010101010101ULL;
1417 if( i_mb_type == I_PCM )
1419 h->mb.i_cbp_chroma = 2;
1420 h->mb.i_cbp_luma = 0xf;
1421 h->mb.cbp[i_mb_xy] = 0x72f; /* all set */
1422 h->mb.b_transform_8x8 = 0;
1423 for( i = 0; i < 16 + 2*4; i++ )
1424 non_zero_count[i] = 16;
1428 /* save non zero count */
1429 for( y = 0; y < 4; y++ )
1430 *(uint32_t*)&non_zero_count[y*4] = *(uint32_t*)&h->mb.cache.non_zero_count[x264_scan8[0]+y*8];
1431 for( y = 0; y < 4; y++ )
1432 *(uint16_t*)&non_zero_count[16+y*2] = *(uint32_t*)&h->mb.cache.non_zero_count[x264_scan8[16+y*2]-1] >> 8;
1436 if( h->mb.i_cbp_luma == 0 && h->mb.i_type != I_8x8 )
1437 h->mb.b_transform_8x8 = 0;
1438 h->mb.mb_transform_size[i_mb_xy] = h->mb.b_transform_8x8;
1440 if( !IS_INTRA( i_mb_type ) )
1442 h->mb.ref[0][i_mb_8x8+0+0*s8x8] = h->mb.cache.ref[0][x264_scan8[0]];
1443 h->mb.ref[0][i_mb_8x8+1+0*s8x8] = h->mb.cache.ref[0][x264_scan8[4]];
1444 h->mb.ref[0][i_mb_8x8+0+1*s8x8] = h->mb.cache.ref[0][x264_scan8[8]];
1445 h->mb.ref[0][i_mb_8x8+1+1*s8x8] = h->mb.cache.ref[0][x264_scan8[12]];
1446 for( y = 0; y < 4; y++ )
1448 *(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];
1449 *(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];
1451 if(h->sh.i_type == SLICE_TYPE_B)
1453 h->mb.ref[1][i_mb_8x8+0+0*s8x8] = h->mb.cache.ref[1][x264_scan8[0]];
1454 h->mb.ref[1][i_mb_8x8+1+0*s8x8] = h->mb.cache.ref[1][x264_scan8[4]];
1455 h->mb.ref[1][i_mb_8x8+0+1*s8x8] = h->mb.cache.ref[1][x264_scan8[8]];
1456 h->mb.ref[1][i_mb_8x8+1+1*s8x8] = h->mb.cache.ref[1][x264_scan8[12]];
1457 for( y = 0; y < 4; y++ )
1459 *(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];
1460 *(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];
1467 for( i_list = 0; i_list < (h->sh.i_type == SLICE_TYPE_B ? 2 : 1 ); i_list++ )
1469 *(uint16_t*)&h->mb.ref[i_list][i_mb_8x8+0*s8x8] = (uint8_t)(-1) * 0x0101;
1470 *(uint16_t*)&h->mb.ref[i_list][i_mb_8x8+1*s8x8] = (uint8_t)(-1) * 0x0101;
1471 for( y = 0; y < 4; y++ )
1473 *(uint64_t*)h->mb.mv[i_list][i_mb_4x4+y*s4x4+0] = 0;
1474 *(uint64_t*)h->mb.mv[i_list][i_mb_4x4+y*s4x4+2] = 0;
1479 if( h->param.b_cabac )
1481 if( i_mb_type == I_4x4 || i_mb_type == I_16x16 )
1482 h->mb.chroma_pred_mode[i_mb_xy] = x264_mb_pred_mode8x8c_fix[ h->mb.i_chroma_pred_mode ];
1484 h->mb.chroma_pred_mode[i_mb_xy] = I_PRED_CHROMA_DC;
1486 if( !IS_INTRA( i_mb_type ) && !IS_SKIP( i_mb_type ) && !IS_DIRECT( i_mb_type ) )
1488 for( y = 0; y < 4; y++ )
1490 *(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];
1491 *(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];
1493 if( h->sh.i_type == SLICE_TYPE_B )
1494 for( y = 0; y < 4; y++ )
1496 *(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];
1497 *(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];
1502 for( y = 0; y < 4; y++ )
1504 *(uint64_t*)h->mb.mvd[0][i_mb_4x4+y*s4x4+0] = 0;
1505 *(uint64_t*)h->mb.mvd[0][i_mb_4x4+y*s4x4+2] = 0;
1507 if( h->sh.i_type == SLICE_TYPE_B )
1508 for( y = 0; y < 4; y++ )
1510 *(uint64_t*)h->mb.mvd[1][i_mb_4x4+y*s4x4+0] = 0;
1511 *(uint64_t*)h->mb.mvd[1][i_mb_4x4+y*s4x4+2] = 0;
1515 if( h->sh.i_type == SLICE_TYPE_B )
1517 if( i_mb_type == B_SKIP || i_mb_type == B_DIRECT )
1518 h->mb.skipbp[i_mb_xy] = 0xf;
1519 else if( i_mb_type == B_8x8 )
1522 for( i = 0; i < 4; i++ )
1523 skipbp |= ( h->mb.i_sub_partition[i] == D_DIRECT_8x8 ) << i;
1524 h->mb.skipbp[i_mb_xy] = skipbp;
1527 h->mb.skipbp[i_mb_xy] = 0;
1532 void x264_macroblock_bipred_init( x264_t *h )
1535 for( i_ref0 = 0; i_ref0 < h->i_ref0; i_ref0++ )
1537 int poc0 = h->fref0[i_ref0]->i_poc;
1538 for( i_ref1 = 0; i_ref1 < h->i_ref1; i_ref1++ )
1540 int dist_scale_factor;
1541 int poc1 = h->fref1[i_ref1]->i_poc;
1542 int td = x264_clip3( poc1 - poc0, -128, 127 );
1543 if( td == 0 /* || pic0 is a long-term ref */ )
1544 dist_scale_factor = 256;
1547 int tb = x264_clip3( h->fdec->i_poc - poc0, -128, 127 );
1548 int tx = (16384 + (abs(td) >> 1)) / td;
1549 dist_scale_factor = x264_clip3( (tb * tx + 32) >> 6, -1024, 1023 );
1551 h->mb.dist_scale_factor[i_ref0][i_ref1] = dist_scale_factor;
1553 dist_scale_factor >>= 2;
1554 if( h->param.analyse.b_weighted_bipred
1555 && dist_scale_factor >= -64
1556 && dist_scale_factor <= 128 )
1557 h->mb.bipred_weight[i_ref0][i_ref1] = 64 - dist_scale_factor;
1559 h->mb.bipred_weight[i_ref0][i_ref1] = 32;
1564 for( i_ref0 = 2*h->i_ref0-1; i_ref0 >= 0; i_ref0-- )
1565 for( i_ref1 = 2*h->i_ref1-1; i_ref1 >= 0; i_ref1-- )
1566 h->mb.bipred_weight[i_ref0][i_ref1] = h->mb.bipred_weight[i_ref0>>1][i_ref1>>1];