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 * Jason Garrett-Glaser <darkshikari@gmail.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 *****************************************************************************/
26 #include "encoder/me.h"
28 void x264_mb_predict_mv( x264_t *h, int i_list, int idx, int i_width, int16_t mvp[2] )
30 const int i8 = x264_scan8[idx];
31 const int i_ref= h->mb.cache.ref[i_list][i8];
32 int i_refa = h->mb.cache.ref[i_list][i8 - 1];
33 int16_t *mv_a = h->mb.cache.mv[i_list][i8 - 1];
34 int i_refb = h->mb.cache.ref[i_list][i8 - 8];
35 int16_t *mv_b = h->mb.cache.mv[i_list][i8 - 8];
36 int i_refc = h->mb.cache.ref[i_list][i8 - 8 + i_width ];
37 int16_t *mv_c = h->mb.cache.mv[i_list][i8 - 8 + i_width];
41 if( (idx&0x03) == 3 || ( i_width == 2 && (idx&0x3) == 2 )|| i_refc == -2 )
43 i_refc = h->mb.cache.ref[i_list][i8 - 8 - 1];
44 mv_c = h->mb.cache.mv[i_list][i8 - 8 - 1];
47 if( h->mb.i_partition == D_16x8 )
49 if( idx == 0 && i_refb == i_ref )
51 *(uint32_t*)mvp = *(uint32_t*)mv_b;
54 else if( idx != 0 && i_refa == i_ref )
56 *(uint32_t*)mvp = *(uint32_t*)mv_a;
60 else if( h->mb.i_partition == D_8x16 )
62 if( idx == 0 && i_refa == i_ref )
64 *(uint32_t*)mvp = *(uint32_t*)mv_a;
67 else if( idx != 0 && i_refc == i_ref )
69 *(uint32_t*)mvp = *(uint32_t*)mv_c;
75 if( i_refa == i_ref ) i_count++;
76 if( i_refb == i_ref ) i_count++;
77 if( i_refc == i_ref ) i_count++;
80 x264_median_mv( mvp, mv_a, mv_b, mv_c );
81 else if( i_count == 1 )
84 *(uint32_t*)mvp = *(uint32_t*)mv_a;
85 else if( i_refb == i_ref )
86 *(uint32_t*)mvp = *(uint32_t*)mv_b;
88 *(uint32_t*)mvp = *(uint32_t*)mv_c;
90 else if( i_refb == -2 && i_refc == -2 && i_refa != -2 )
91 *(uint32_t*)mvp = *(uint32_t*)mv_a;
93 x264_median_mv( mvp, mv_a, mv_b, mv_c );
96 void x264_mb_predict_mv_16x16( x264_t *h, int i_list, int i_ref, int16_t mvp[2] )
98 int i_refa = h->mb.cache.ref[i_list][X264_SCAN8_0 - 1];
99 int16_t *mv_a = h->mb.cache.mv[i_list][X264_SCAN8_0 - 1];
100 int i_refb = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8];
101 int16_t *mv_b = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8];
102 int i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 + 4];
103 int16_t *mv_c = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8 + 4];
109 i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 - 1];
110 mv_c = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8 - 1];
114 if( i_refa == i_ref ) i_count++;
115 if( i_refb == i_ref ) i_count++;
116 if( i_refc == i_ref ) i_count++;
119 x264_median_mv( mvp, mv_a, mv_b, mv_c );
120 else if( i_count == 1 )
122 if( i_refa == i_ref )
123 *(uint32_t*)mvp = *(uint32_t*)mv_a;
124 else if( i_refb == i_ref )
125 *(uint32_t*)mvp = *(uint32_t*)mv_b;
127 *(uint32_t*)mvp = *(uint32_t*)mv_c;
129 else if( i_refb == -2 && i_refc == -2 && i_refa != -2 )
130 *(uint32_t*)mvp = *(uint32_t*)mv_a;
132 x264_median_mv( mvp, mv_a, mv_b, mv_c );
136 void x264_mb_predict_mv_pskip( x264_t *h, int16_t mv[2] )
138 int i_refa = h->mb.cache.ref[0][X264_SCAN8_0 - 1];
139 int i_refb = h->mb.cache.ref[0][X264_SCAN8_0 - 8];
140 int16_t *mv_a = h->mb.cache.mv[0][X264_SCAN8_0 - 1];
141 int16_t *mv_b = h->mb.cache.mv[0][X264_SCAN8_0 - 8];
143 if( i_refa == -2 || i_refb == -2 ||
144 !( i_refa | *(uint32_t*)mv_a ) ||
145 !( i_refb | *(uint32_t*)mv_b ) )
151 x264_mb_predict_mv_16x16( h, 0, 0, mv );
155 static int x264_mb_predict_mv_direct16x16_temporal( x264_t *h )
157 int i_mb_4x4 = 16 * h->mb.i_mb_stride * h->mb.i_mb_y + 4 * h->mb.i_mb_x;
158 int i_mb_8x8 = 4 * h->mb.i_mb_stride * h->mb.i_mb_y + 2 * h->mb.i_mb_x;
161 const int type_col = h->fref1[0]->mb_type[ h->mb.i_mb_xy ];
163 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, 0 );
165 if( IS_INTRA( type_col ) )
167 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, 0 );
168 x264_macroblock_cache_mv( h, 0, 0, 4, 4, 0, 0 );
169 x264_macroblock_cache_mv( h, 0, 0, 4, 4, 1, 0 );
172 b8x8 = h->sps->b_direct8x8_inference ||
173 (type_col != P_8x8 && type_col != B_SKIP && type_col != B_DIRECT && type_col != B_8x8);
175 for( i8 = 0; i8 < 4; i8++ )
179 const int i_part_8x8 = i_mb_8x8 + x8 + y8 * h->mb.i_b8_stride;
180 const int i_ref = h->mb.map_col_to_list0[ h->fref1[0]->ref[0][ i_part_8x8 ] ];
184 const int dist_scale_factor = h->mb.dist_scale_factor[i_ref][0];
186 x264_macroblock_cache_ref( h, 2*x8, 2*y8, 2, 2, 0, i_ref );
190 const int16_t *mv_col = h->fref1[0]->mv[0][ i_mb_4x4 + 3*x8 + 3*y8 * h->mb.i_b4_stride];
191 const int l0x = ( dist_scale_factor * mv_col[0] + 128 ) >> 8;
192 const int l0y = ( dist_scale_factor * mv_col[1] + 128 ) >> 8;
193 x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 0, pack16to32_mask(l0x, l0y) );
194 x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 1, pack16to32_mask(l0x-mv_col[0], l0y-mv_col[1]) );
198 for( i4 = 0; i4 < 4; i4++ )
200 const int x4 = i4%2 + 2*x8;
201 const int y4 = i4/2 + 2*y8;
202 const int16_t *mv_col = h->fref1[0]->mv[0][ i_mb_4x4 + x4 + y4 * h->mb.i_b4_stride ];
203 const int l0x = ( dist_scale_factor * mv_col[0] + 128 ) >> 8;
204 const int l0y = ( dist_scale_factor * mv_col[1] + 128 ) >> 8;
205 x264_macroblock_cache_mv( h, x4, y4, 1, 1, 0, pack16to32_mask(l0x, l0y) );
206 x264_macroblock_cache_mv( h, x4, y4, 1, 1, 1, pack16to32_mask(l0x-mv_col[0], l0y-mv_col[1]) );
212 /* the collocated ref isn't in the current list0 */
213 /* FIXME: we might still be able to use direct_8x8 on some partitions */
214 /* FIXME: with B-pyramid + extensive ref list reordering
215 * (not currently used), we would also have to check
216 * l1mv1 like in spatial mode */
221 if( h->param.i_threads > 1 )
223 int di = b8x8 ? 4 : 1;
224 for( i4=0; i4<16; i4+=di )
226 if( h->mb.cache.mv[0][x264_scan8[i4]][1] > h->mb.mv_max_spel[1]
227 || h->mb.cache.mv[1][x264_scan8[i4]][1] > h->mb.mv_max_spel[1] )
230 fprintf(stderr, "direct_temporal: (%d,%d) (%d,%d) > %d \n",
231 h->mb.cache.mv[0][x264_scan8[i4]][0],
232 h->mb.cache.mv[0][x264_scan8[i4]][1],
233 h->mb.cache.mv[1][x264_scan8[i4]][0],
234 h->mb.cache.mv[1][x264_scan8[i4]][1],
235 h->mb.mv_max_spel[1]);
245 static int x264_mb_predict_mv_direct16x16_spatial( x264_t *h )
248 DECLARE_ALIGNED_8( int16_t mv[2][2] );
252 const int8_t *l1ref0 = &h->fref1[0]->ref[0][ h->mb.i_b8_xy ];
253 const int8_t *l1ref1 = &h->fref1[0]->ref[1][ h->mb.i_b8_xy ];
254 const int16_t (*l1mv0)[2] = (const int16_t (*)[2]) &h->fref1[0]->mv[0][ h->mb.i_b4_xy ];
255 const int16_t (*l1mv1)[2] = (const int16_t (*)[2]) &h->fref1[0]->mv[1][ h->mb.i_b4_xy ];
256 const int type_col = h->fref1[0]->mb_type[ h->mb.i_mb_xy ];
258 for( i_list=0; i_list<2; i_list++ )
260 int i_refa = h->mb.cache.ref[i_list][X264_SCAN8_0 - 1];
261 int i_refb = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8];
262 int i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 + 4];
264 i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 - 1];
266 ref[i_list] = i_refa;
267 if( ref[i_list] < 0 || ( i_refb < ref[i_list] && i_refb >= 0 ))
268 ref[i_list] = i_refb;
269 if( ref[i_list] < 0 || ( i_refc < ref[i_list] && i_refc >= 0 ))
270 ref[i_list] = i_refc;
271 if( ref[i_list] < 0 )
275 if( ref[0] < 0 && ref[1] < 0 )
277 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, 0 );
278 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, 0 );
279 x264_macroblock_cache_mv( h, 0, 0, 4, 4, 0, 0 );
280 x264_macroblock_cache_mv( h, 0, 0, 4, 4, 1, 0 );
285 x264_mb_predict_mv_16x16( h, 0, ref[0], mv[0] );
287 *(uint32_t*)mv[0] = 0;
289 x264_mb_predict_mv_16x16( h, 1, ref[1], mv[1] );
291 *(uint32_t*)mv[1] = 0;
293 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, ref[0] );
294 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, ref[1] );
295 x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 0, mv[0] );
296 x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 1, mv[1] );
298 if( h->param.i_threads > 1
299 && ( mv[0][1] > h->mb.mv_max_spel[1]
300 || mv[1][1] > h->mb.mv_max_spel[1] ) )
303 fprintf(stderr, "direct_spatial: (%d,%d) (%d,%d) > %d \n",
304 mv[0][0], mv[0][1], mv[1][0], mv[1][1],
305 h->mb.mv_max_spel[1]);
310 if( IS_INTRA( type_col ) || (ref[0]&&ref[1]) )
313 b8x8 = h->sps->b_direct8x8_inference ||
314 (type_col != P_8x8 && type_col != B_SKIP && type_col != B_DIRECT && type_col != B_8x8);
317 for( i8=0; i8<4; i8++ )
321 const int o8 = x8 + y8 * h->mb.i_b8_stride;
322 if( l1ref0[o8] == 0 || ( l1ref0[o8] < 0 && l1ref1[o8] == 0 ) )
324 const int16_t (*l1mv)[2] = (l1ref0[o8] == 0) ? l1mv0 : l1mv1;
327 const int16_t *mvcol = l1mv[3*x8 + 3*y8 * h->mb.i_b4_stride];
328 if( abs( mvcol[0] ) <= 1 && abs( mvcol[1] ) <= 1 )
331 x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 0, 0 );
333 x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 1, 0 );
338 for( i4=0; i4<4; i4++ )
340 const int x4 = i4%2 + 2*x8;
341 const int y4 = i4/2 + 2*y8;
342 const int16_t *mvcol = l1mv[x4 + y4 * h->mb.i_b4_stride];
343 if( abs( mvcol[0] ) <= 1 && abs( mvcol[1] ) <= 1 )
346 x264_macroblock_cache_mv( h, x4, y4, 1, 1, 0, 0 );
348 x264_macroblock_cache_mv( h, x4, y4, 1, 1, 1, 0 );
358 int x264_mb_predict_mv_direct16x16( x264_t *h, int *b_changed )
361 if( h->param.analyse.i_direct_mv_pred == X264_DIRECT_PRED_NONE )
363 else if( h->sh.b_direct_spatial_mv_pred )
364 b_available = x264_mb_predict_mv_direct16x16_spatial( h );
366 b_available = x264_mb_predict_mv_direct16x16_temporal( h );
368 if( b_changed != NULL && b_available )
370 int type_col = h->fref1[0]->mb_type[ h->mb.i_mb_xy ];
371 if( IS_INTRA(type_col) || type_col == P_SKIP )
373 *b_changed = h->mb.cache.direct_ref[0][0] != h->mb.cache.ref[0][X264_SCAN8_0]
374 || h->mb.cache.direct_ref[1][0] != h->mb.cache.ref[1][X264_SCAN8_0]
375 || *(uint32_t*)h->mb.cache.direct_mv[0][X264_SCAN8_0] != *(uint32_t*)h->mb.cache.mv[0][X264_SCAN8_0]
376 || *(uint32_t*)h->mb.cache.direct_mv[1][X264_SCAN8_0] != *(uint32_t*)h->mb.cache.mv[1][X264_SCAN8_0];
382 for( l = 0; l < 2; l++ )
383 for( i = 0; i < 4; i++ )
384 *b_changed |= h->mb.cache.direct_ref[l][i] != h->mb.cache.ref[l][x264_scan8[i*4]];
385 *b_changed = *b_changed || memcmp(h->mb.cache.direct_mv, h->mb.cache.mv, sizeof(h->mb.cache.mv));
395 for( l = 0; l < 2; l++ )
396 for( i = 0; i < 4; i++ )
397 h->mb.cache.direct_ref[l][i] = h->mb.cache.ref[l][x264_scan8[i*4]];
398 h->mc.memcpy_aligned(h->mb.cache.direct_mv, h->mb.cache.mv, sizeof(h->mb.cache.mv));
404 void x264_mb_load_mv_direct8x8( x264_t *h, int idx )
406 const int x = 2*(idx%2);
407 const int y = 2*(idx/2);
408 x264_macroblock_cache_ref( h, x, y, 2, 2, 0, h->mb.cache.direct_ref[0][idx] );
409 x264_macroblock_cache_ref( h, x, y, 2, 2, 1, h->mb.cache.direct_ref[1][idx] );
410 *(uint64_t*)h->mb.cache.mv[0][x264_scan8[idx*4]] =
411 *(uint64_t*)h->mb.cache.direct_mv[0][x264_scan8[idx*4]];
412 *(uint64_t*)h->mb.cache.mv[0][x264_scan8[idx*4]+8] =
413 *(uint64_t*)h->mb.cache.direct_mv[0][x264_scan8[idx*4]+8];
414 *(uint64_t*)h->mb.cache.mv[1][x264_scan8[idx*4]] =
415 *(uint64_t*)h->mb.cache.direct_mv[1][x264_scan8[idx*4]];
416 *(uint64_t*)h->mb.cache.mv[1][x264_scan8[idx*4]+8] =
417 *(uint64_t*)h->mb.cache.direct_mv[1][x264_scan8[idx*4]+8];
420 /* This just improves encoder performance, it's not part of the spec */
421 void x264_mb_predict_mv_ref16x16( x264_t *h, int i_list, int i_ref, int16_t mvc[9][2], int *i_mvc )
423 int16_t (*mvr)[2] = h->mb.mvr[i_list][i_ref];
426 #define SET_MVP(mvp) { \
427 *(uint32_t*)mvc[i] = *(uint32_t*)mvp; \
432 if( h->sh.i_type == SLICE_TYPE_B
433 && h->mb.cache.ref[i_list][x264_scan8[12]] == i_ref )
435 SET_MVP( h->mb.cache.mv[i_list][x264_scan8[12]] );
438 if( i_ref == 0 && h->frames.b_have_lowres )
440 int16_t (*lowres_mv)[2] = i_list ? h->fenc->lowres_mvs[1][h->fref1[0]->i_frame-h->fenc->i_frame-1]
441 : h->fenc->lowres_mvs[0][h->fenc->i_frame-h->fref0[0]->i_frame-1];
442 if( lowres_mv[0][0] != 0x7fff ) *(uint32_t*)mvc[i++] = (*(uint32_t*)lowres_mv[h->mb.i_mb_xy]*2)&0xfffeffff;
445 /* spatial predictors */
446 if( h->mb.i_neighbour & MB_LEFT )
448 int i_mb_l = h->mb.i_mb_xy - 1;
449 /* skip MBs didn't go through the whole search process, so mvr is undefined */
450 if( !IS_SKIP( h->mb.type[i_mb_l] ) )
451 SET_MVP( mvr[i_mb_l] );
453 if( h->mb.i_neighbour & MB_TOP )
455 int i_mb_t = h->mb.i_mb_top_xy;
456 if( !IS_SKIP( h->mb.type[i_mb_t] ) )
457 SET_MVP( mvr[i_mb_t] );
459 if( h->mb.i_neighbour & MB_TOPLEFT && !IS_SKIP( h->mb.type[i_mb_t - 1] ) )
460 SET_MVP( mvr[i_mb_t-1] );
461 if( h->mb.i_mb_x < h->mb.i_mb_stride - 1 && !IS_SKIP( h->mb.type[i_mb_t + 1] ) )
462 SET_MVP( mvr[i_mb_t+1] );
466 /* temporal predictors */
467 /* FIXME temporal scaling w/ interlace */
468 if( h->fref0[0]->i_ref[0] > 0 && !h->sh.b_mbaff )
470 x264_frame_t *l0 = h->fref0[0];
472 #define SET_TMVP(dx, dy) { \
473 int i_b4 = h->mb.i_b4_xy + dx*4 + dy*4*h->mb.i_b4_stride; \
474 int i_b8 = h->mb.i_b8_xy + dx*2 + dy*2*h->mb.i_b8_stride; \
475 int ref_col = l0->ref[0][i_b8]; \
478 int scale = (h->fdec->i_poc - h->fdec->ref_poc[0][i_ref]) * l0->inv_ref_poc[ref_col];\
479 mvc[i][0] = (l0->mv[0][i_b4][0]*scale + 128) >> 8;\
480 mvc[i][1] = (l0->mv[0][i_b4][1]*scale + 128) >> 8;\
486 if( h->mb.i_mb_x < h->sps->i_mb_width-1 )
488 if( h->mb.i_mb_y < h->sps->i_mb_height-1 )
496 /* Set up a lookup table for delta pocs to reduce an IDIV to an IMUL */
497 static void setup_inverse_delta_pocs( x264_t *h )
500 for( i = 0; i < h->i_ref0; i++ )
502 int delta = h->fdec->i_poc - h->fref0[i]->i_poc;
503 h->fdec->inv_ref_poc[i] = (256 + delta/2) / delta;
507 static inline void x264_mb_mc_0xywh( x264_t *h, int x, int y, int width, int height )
509 const int i8 = x264_scan8[0]+x+8*y;
510 const int i_ref = h->mb.cache.ref[0][i8];
511 const int mvx = x264_clip3( h->mb.cache.mv[0][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] );
512 int mvy = x264_clip3( h->mb.cache.mv[0][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] );
514 h->mc.mc_luma( &h->mb.pic.p_fdec[0][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE,
515 h->mb.pic.p_fref[0][i_ref], h->mb.pic.i_stride[0],
516 mvx + 4*4*x, mvy + 4*4*y, 4*width, 4*height );
518 // chroma is offset if MCing from a field of opposite parity
519 if( h->mb.b_interlaced & i_ref )
520 mvy += (h->mb.i_mb_y & 1)*4 - 2;
522 h->mc.mc_chroma( &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
523 &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],
524 mvx, mvy, 2*width, 2*height );
526 h->mc.mc_chroma( &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
527 &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],
528 mvx, mvy, 2*width, 2*height );
530 static inline void x264_mb_mc_1xywh( x264_t *h, int x, int y, int width, int height )
532 const int i8 = x264_scan8[0]+x+8*y;
533 const int i_ref = h->mb.cache.ref[1][i8];
534 const int mvx = x264_clip3( h->mb.cache.mv[1][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] );
535 int mvy = x264_clip3( h->mb.cache.mv[1][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] );
537 h->mc.mc_luma( &h->mb.pic.p_fdec[0][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE,
538 h->mb.pic.p_fref[1][i_ref], h->mb.pic.i_stride[0],
539 mvx + 4*4*x, mvy + 4*4*y, 4*width, 4*height );
541 if( h->mb.b_interlaced & i_ref )
542 mvy += (h->mb.i_mb_y & 1)*4 - 2;
544 h->mc.mc_chroma( &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
545 &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],
546 mvx, mvy, 2*width, 2*height );
548 h->mc.mc_chroma( &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
549 &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],
550 mvx, mvy, 2*width, 2*height );
553 static inline void x264_mb_mc_01xywh( x264_t *h, int x, int y, int width, int height )
555 const int i8 = x264_scan8[0]+x+8*y;
556 const int i_ref0 = h->mb.cache.ref[0][i8];
557 const int i_ref1 = h->mb.cache.ref[1][i8];
558 const int weight = h->mb.bipred_weight[i_ref0][i_ref1];
559 const int mvx0 = x264_clip3( h->mb.cache.mv[0][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] );
560 const int mvx1 = x264_clip3( h->mb.cache.mv[1][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] );
561 int mvy0 = x264_clip3( h->mb.cache.mv[0][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] );
562 int mvy1 = x264_clip3( h->mb.cache.mv[1][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] );
563 int i_mode = x264_size2pixel[height][width];
564 int i_stride0 = 16, i_stride1 = 16;
565 DECLARE_ALIGNED_16( uint8_t tmp0[16*16] );
566 DECLARE_ALIGNED_16( uint8_t tmp1[16*16] );
567 uint8_t *src0, *src1;
569 src0 = h->mc.get_ref( tmp0, &i_stride0, h->mb.pic.p_fref[0][i_ref0], h->mb.pic.i_stride[0],
570 mvx0 + 4*4*x, mvy0 + 4*4*y, 4*width, 4*height );
571 src1 = h->mc.get_ref( tmp1, &i_stride1, h->mb.pic.p_fref[1][i_ref1], h->mb.pic.i_stride[0],
572 mvx1 + 4*4*x, mvy1 + 4*4*y, 4*width, 4*height );
573 h->mc.avg[i_mode]( &h->mb.pic.p_fdec[0][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE,
574 src0, i_stride0, src1, i_stride1, weight );
576 if( h->mb.b_interlaced & i_ref0 )
577 mvy0 += (h->mb.i_mb_y & 1)*4 - 2;
578 if( h->mb.b_interlaced & i_ref1 )
579 mvy1 += (h->mb.i_mb_y & 1)*4 - 2;
581 h->mc.mc_chroma( tmp0, 16, &h->mb.pic.p_fref[0][i_ref0][4][2*y*h->mb.pic.i_stride[1]+2*x], h->mb.pic.i_stride[1],
582 mvx0, mvy0, 2*width, 2*height );
583 h->mc.mc_chroma( tmp1, 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],
584 mvx1, mvy1, 2*width, 2*height );
585 h->mc.avg[i_mode+3]( &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE, tmp0, 16, tmp1, 16, weight );
586 h->mc.mc_chroma( tmp0, 16, &h->mb.pic.p_fref[0][i_ref0][5][2*y*h->mb.pic.i_stride[2]+2*x], h->mb.pic.i_stride[2],
587 mvx0, mvy0, 2*width, 2*height );
588 h->mc.mc_chroma( tmp1, 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],
589 mvx1, mvy1, 2*width, 2*height );
590 h->mc.avg[i_mode+3]( &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE, tmp0, 16, tmp1, 16, weight );
593 static void x264_mb_mc_direct8x8( x264_t *h, int x, int y )
595 const int i8 = x264_scan8[0] + x + 8*y;
597 /* FIXME: optimize based on current block size, not global settings? */
598 if( h->sps->b_direct8x8_inference )
600 if( h->mb.cache.ref[0][i8] >= 0 )
601 if( h->mb.cache.ref[1][i8] >= 0 )
602 x264_mb_mc_01xywh( h, x, y, 2, 2 );
604 x264_mb_mc_0xywh( h, x, y, 2, 2 );
606 x264_mb_mc_1xywh( h, x, y, 2, 2 );
610 if( h->mb.cache.ref[0][i8] >= 0 )
612 if( h->mb.cache.ref[1][i8] >= 0 )
614 x264_mb_mc_01xywh( h, x+0, y+0, 1, 1 );
615 x264_mb_mc_01xywh( h, x+1, y+0, 1, 1 );
616 x264_mb_mc_01xywh( h, x+0, y+1, 1, 1 );
617 x264_mb_mc_01xywh( h, x+1, y+1, 1, 1 );
621 x264_mb_mc_0xywh( h, x+0, y+0, 1, 1 );
622 x264_mb_mc_0xywh( h, x+1, y+0, 1, 1 );
623 x264_mb_mc_0xywh( h, x+0, y+1, 1, 1 );
624 x264_mb_mc_0xywh( h, x+1, y+1, 1, 1 );
629 x264_mb_mc_1xywh( h, x+0, y+0, 1, 1 );
630 x264_mb_mc_1xywh( h, x+1, y+0, 1, 1 );
631 x264_mb_mc_1xywh( h, x+0, y+1, 1, 1 );
632 x264_mb_mc_1xywh( h, x+1, y+1, 1, 1 );
637 void x264_mb_mc_8x8( x264_t *h, int i8 )
639 const int x = 2*(i8&1);
640 const int y = 2*(i8>>1);
641 switch( h->mb.i_sub_partition[i8] )
644 x264_mb_mc_0xywh( h, x, y, 2, 2 );
647 x264_mb_mc_0xywh( h, x, y+0, 2, 1 );
648 x264_mb_mc_0xywh( h, x, y+1, 2, 1 );
651 x264_mb_mc_0xywh( h, x+0, y, 1, 2 );
652 x264_mb_mc_0xywh( h, x+1, y, 1, 2 );
655 x264_mb_mc_0xywh( h, x+0, y+0, 1, 1 );
656 x264_mb_mc_0xywh( h, x+1, y+0, 1, 1 );
657 x264_mb_mc_0xywh( h, x+0, y+1, 1, 1 );
658 x264_mb_mc_0xywh( h, x+1, y+1, 1, 1 );
661 x264_mb_mc_1xywh( h, x, y, 2, 2 );
664 x264_mb_mc_1xywh( h, x, y+0, 2, 1 );
665 x264_mb_mc_1xywh( h, x, y+1, 2, 1 );
668 x264_mb_mc_1xywh( h, x+0, y, 1, 2 );
669 x264_mb_mc_1xywh( h, x+1, y, 1, 2 );
672 x264_mb_mc_1xywh( h, x+0, y+0, 1, 1 );
673 x264_mb_mc_1xywh( h, x+1, y+0, 1, 1 );
674 x264_mb_mc_1xywh( h, x+0, y+1, 1, 1 );
675 x264_mb_mc_1xywh( h, x+1, y+1, 1, 1 );
678 x264_mb_mc_01xywh( h, x, y, 2, 2 );
681 x264_mb_mc_01xywh( h, x, y+0, 2, 1 );
682 x264_mb_mc_01xywh( h, x, y+1, 2, 1 );
685 x264_mb_mc_01xywh( h, x+0, y, 1, 2 );
686 x264_mb_mc_01xywh( h, x+1, y, 1, 2 );
689 x264_mb_mc_01xywh( h, x+0, y+0, 1, 1 );
690 x264_mb_mc_01xywh( h, x+1, y+0, 1, 1 );
691 x264_mb_mc_01xywh( h, x+0, y+1, 1, 1 );
692 x264_mb_mc_01xywh( h, x+1, y+1, 1, 1 );
695 x264_mb_mc_direct8x8( h, x, y );
700 void x264_mb_mc( x264_t *h )
702 if( h->mb.i_type == P_L0 )
704 if( h->mb.i_partition == D_16x16 )
706 x264_mb_mc_0xywh( h, 0, 0, 4, 4 );
708 else if( h->mb.i_partition == D_16x8 )
710 x264_mb_mc_0xywh( h, 0, 0, 4, 2 );
711 x264_mb_mc_0xywh( h, 0, 2, 4, 2 );
713 else if( h->mb.i_partition == D_8x16 )
715 x264_mb_mc_0xywh( h, 0, 0, 2, 4 );
716 x264_mb_mc_0xywh( h, 2, 0, 2, 4 );
719 else if( h->mb.i_type == P_8x8 || h->mb.i_type == B_8x8 )
722 for( i = 0; i < 4; i++ )
723 x264_mb_mc_8x8( h, i );
725 else if( h->mb.i_type == B_SKIP || h->mb.i_type == B_DIRECT )
727 x264_mb_mc_direct8x8( h, 0, 0 );
728 x264_mb_mc_direct8x8( h, 2, 0 );
729 x264_mb_mc_direct8x8( h, 0, 2 );
730 x264_mb_mc_direct8x8( h, 2, 2 );
734 const uint8_t *b_list0 = x264_mb_type_list_table[h->mb.i_type][0];
735 const uint8_t *b_list1 = x264_mb_type_list_table[h->mb.i_type][1];
737 if( h->mb.i_partition == D_16x16 )
739 if( b_list0[0] && b_list1[0] ) x264_mb_mc_01xywh( h, 0, 0, 4, 4 );
740 else if( b_list0[0] ) x264_mb_mc_0xywh ( h, 0, 0, 4, 4 );
741 else if( b_list1[0] ) x264_mb_mc_1xywh ( h, 0, 0, 4, 4 );
743 else if( h->mb.i_partition == D_16x8 )
745 if( b_list0[0] && b_list1[0] ) x264_mb_mc_01xywh( h, 0, 0, 4, 2 );
746 else if( b_list0[0] ) x264_mb_mc_0xywh ( h, 0, 0, 4, 2 );
747 else if( b_list1[0] ) x264_mb_mc_1xywh ( h, 0, 0, 4, 2 );
749 if( b_list0[1] && b_list1[1] ) x264_mb_mc_01xywh( h, 0, 2, 4, 2 );
750 else if( b_list0[1] ) x264_mb_mc_0xywh ( h, 0, 2, 4, 2 );
751 else if( b_list1[1] ) x264_mb_mc_1xywh ( h, 0, 2, 4, 2 );
753 else if( h->mb.i_partition == D_8x16 )
755 if( b_list0[0] && b_list1[0] ) x264_mb_mc_01xywh( h, 0, 0, 2, 4 );
756 else if( b_list0[0] ) x264_mb_mc_0xywh ( h, 0, 0, 2, 4 );
757 else if( b_list1[0] ) x264_mb_mc_1xywh ( h, 0, 0, 2, 4 );
759 if( b_list0[1] && b_list1[1] ) x264_mb_mc_01xywh( h, 2, 0, 2, 4 );
760 else if( b_list0[1] ) x264_mb_mc_0xywh ( h, 2, 0, 2, 4 );
761 else if( b_list1[1] ) x264_mb_mc_1xywh ( h, 2, 0, 2, 4 );
766 int x264_macroblock_cache_init( x264_t *h )
769 int i_mb_count = h->mb.i_mb_count;
771 h->mb.i_mb_stride = h->sps->i_mb_width;
772 h->mb.i_b8_stride = h->sps->i_mb_width * 2;
773 h->mb.i_b4_stride = h->sps->i_mb_width * 4;
775 h->mb.b_interlaced = h->param.b_interlaced;
777 CHECKED_MALLOC( h->mb.qp, i_mb_count * sizeof(int8_t) );
778 CHECKED_MALLOC( h->mb.cbp, i_mb_count * sizeof(int16_t) );
779 CHECKED_MALLOC( h->mb.skipbp, i_mb_count * sizeof(int8_t) );
780 CHECKED_MALLOC( h->mb.mb_transform_size, i_mb_count * sizeof(int8_t) );
782 /* 0 -> 3 top(4), 4 -> 6 : left(3) */
783 CHECKED_MALLOC( h->mb.intra4x4_pred_mode, i_mb_count * 8 * sizeof(int8_t) );
786 CHECKED_MALLOC( h->mb.non_zero_count, i_mb_count * 24 * sizeof(uint8_t) );
787 CHECKED_MALLOC( h->mb.nnz_backup, h->sps->i_mb_width * 4 * 16 * sizeof(uint8_t) );
789 if( h->param.b_cabac )
791 CHECKED_MALLOC( h->mb.chroma_pred_mode, i_mb_count * sizeof(int8_t) );
792 CHECKED_MALLOC( h->mb.mvd[0], 2*16 * i_mb_count * sizeof(int16_t) );
793 CHECKED_MALLOC( h->mb.mvd[1], 2*16 * i_mb_count * sizeof(int16_t) );
798 int i_refs = X264_MIN(16, (i ? 1 : h->param.i_frame_reference) + h->param.b_bframe_pyramid) << h->param.b_interlaced;
799 for( j=0; j < i_refs; j++ )
800 CHECKED_MALLOC( h->mb.mvr[i][j], 2 * i_mb_count * sizeof(int16_t) );
803 for( i=0; i<=h->param.b_interlaced; i++ )
806 CHECKED_MALLOC( h->mb.intra_border_backup[i][j], h->fdec->i_stride[j] );
807 h->mb.intra_border_backup[i][j] += 8;
810 /* init with not available (for top right idx=7,15) */
811 memset( h->mb.cache.ref[0], -2, X264_SCAN8_SIZE * sizeof( int8_t ) );
812 memset( h->mb.cache.ref[1], -2, X264_SCAN8_SIZE * sizeof( int8_t ) );
824 h->mb.pic.p_fenc[0] = h->mb.pic.fenc_buf;
825 h->mb.pic.p_fenc[1] = h->mb.pic.fenc_buf + 16*FENC_STRIDE;
826 h->mb.pic.p_fenc[2] = h->mb.pic.fenc_buf + 16*FENC_STRIDE + 8;
827 h->mb.pic.p_fdec[0] = h->mb.pic.fdec_buf + 2*FDEC_STRIDE;
828 h->mb.pic.p_fdec[1] = h->mb.pic.fdec_buf + 19*FDEC_STRIDE;
829 h->mb.pic.p_fdec[2] = h->mb.pic.fdec_buf + 19*FDEC_STRIDE + 16;
831 h->mb.i_neighbour4[6] =
832 h->mb.i_neighbour4[9] =
833 h->mb.i_neighbour4[12] =
834 h->mb.i_neighbour4[14] = MB_LEFT|MB_TOP|MB_TOPLEFT|MB_TOPRIGHT;
835 h->mb.i_neighbour4[3] =
836 h->mb.i_neighbour4[7] =
837 h->mb.i_neighbour4[11] =
838 h->mb.i_neighbour4[13] =
839 h->mb.i_neighbour4[15] =
840 h->mb.i_neighbour8[3] = MB_LEFT|MB_TOP|MB_TOPLEFT;
842 int buf_hpel = (h->param.i_width+48) * sizeof(int16_t);
843 int buf_ssim = h->param.analyse.b_ssim * 8 * (h->param.i_width/4+3) * sizeof(int);
844 int me_range = X264_MIN(h->param.analyse.i_me_range, h->param.analyse.i_mv_range);
845 int buf_tesa = (h->param.analyse.i_me_method >= X264_ME_ESA) *
846 ((me_range*2+18) * sizeof(int16_t) + (me_range+4) * (me_range+1) * 4 * sizeof(mvsad_t));
847 CHECKED_MALLOC( h->scratch_buffer, X264_MAX3( buf_hpel, buf_ssim, buf_tesa ) );
852 void x264_macroblock_cache_end( x264_t *h )
855 for( i=0; i<=h->param.b_interlaced; i++ )
857 x264_free( h->mb.intra_border_backup[i][j] - 8 );
859 for( j=0; j<32; j++ )
860 x264_free( h->mb.mvr[i][j] );
861 if( h->param.b_cabac )
863 x264_free( h->mb.chroma_pred_mode );
864 x264_free( h->mb.mvd[0] );
865 x264_free( h->mb.mvd[1] );
867 x264_free( h->mb.intra4x4_pred_mode );
868 x264_free( h->mb.non_zero_count );
869 x264_free( h->mb.nnz_backup );
870 x264_free( h->mb.mb_transform_size );
871 x264_free( h->mb.skipbp );
872 x264_free( h->mb.cbp );
873 x264_free( h->mb.qp );
874 x264_free( h->scratch_buffer );
876 void x264_macroblock_slice_init( x264_t *h )
880 h->mb.mv[0] = h->fdec->mv[0];
881 h->mb.mv[1] = h->fdec->mv[1];
882 h->mb.ref[0] = h->fdec->ref[0];
883 h->mb.ref[1] = h->fdec->ref[1];
884 h->mb.type = h->fdec->mb_type;
886 h->fdec->i_ref[0] = h->i_ref0;
887 h->fdec->i_ref[1] = h->i_ref1;
888 for( i = 0; i < h->i_ref0; i++ )
889 h->fdec->ref_poc[0][i] = h->fref0[i]->i_poc;
890 if( h->sh.i_type == SLICE_TYPE_B )
892 for( i = 0; i < h->i_ref1; i++ )
893 h->fdec->ref_poc[1][i] = h->fref1[i]->i_poc;
895 h->mb.map_col_to_list0[-1] = -1;
896 h->mb.map_col_to_list0[-2] = -2;
897 for( i = 0; i < h->fref1[0]->i_ref[0]; i++ )
899 int poc = h->fref1[0]->ref_poc[0][i];
900 h->mb.map_col_to_list0[i] = -2;
901 for( j = 0; j < h->i_ref0; j++ )
902 if( h->fref0[j]->i_poc == poc )
904 h->mb.map_col_to_list0[i] = j;
909 if( h->sh.i_type == SLICE_TYPE_P )
910 memset( h->mb.cache.skip, 0, X264_SCAN8_SIZE * sizeof( int8_t ) );
912 setup_inverse_delta_pocs( h );
915 void x264_prefetch_fenc( x264_t *h, x264_frame_t *fenc, int i_mb_x, int i_mb_y )
917 int stride_y = fenc->i_stride[0];
918 int stride_uv = fenc->i_stride[1];
919 int off_y = 16 * (i_mb_x + i_mb_y * stride_y);
920 int off_uv = 8 * (i_mb_x + i_mb_y * stride_uv);
921 h->mc.prefetch_fenc( fenc->plane[0]+off_y, stride_y,
922 fenc->plane[1+(i_mb_x&1)]+off_uv, stride_uv, i_mb_x );
925 static NOINLINE void copy_column8( uint8_t *dst, uint8_t *src )
927 // input pointers are offset by 4 rows because that's faster (smaller instruction size on x86)
929 for( i = -4; i < 4; i++ )
930 dst[i*FDEC_STRIDE] = src[i*FDEC_STRIDE];
933 static void ALWAYS_INLINE x264_macroblock_load_pic_pointers( x264_t *h, int i_mb_x, int i_mb_y, int i)
935 const int w = (i == 0 ? 16 : 8);
936 const int i_stride = h->fdec->i_stride[!!i];
937 const int i_stride2 = i_stride << h->mb.b_interlaced;
938 const int i_pix_offset = h->mb.b_interlaced
939 ? w * (i_mb_x + (i_mb_y&~1) * i_stride) + (i_mb_y&1) * i_stride
940 : w * (i_mb_x + i_mb_y * i_stride);
941 int ref_pix_offset[2] = { i_pix_offset, i_pix_offset };
942 const uint8_t *intra_fdec = &h->mb.intra_border_backup[i_mb_y & h->sh.b_mbaff][i][i_mb_x*16>>!!i];
943 x264_frame_t **fref[2] = { h->fref0, h->fref1 };
945 if( h->mb.b_interlaced )
946 ref_pix_offset[1] += (1-2*(i_mb_y&1)) * i_stride;
947 h->mb.pic.i_stride[i] = i_stride2;
948 h->mb.pic.p_fenc_plane[i] = &h->fenc->plane[i][i_pix_offset];
949 h->mc.copy[i?PIXEL_8x8:PIXEL_16x16]( h->mb.pic.p_fenc[i], FENC_STRIDE,
950 h->mb.pic.p_fenc_plane[i], i_stride2, w );
951 memcpy( &h->mb.pic.p_fdec[i][-1-FDEC_STRIDE], intra_fdec-1, w*3/2+1 );
952 if( h->mb.b_interlaced )
954 const uint8_t *plane_fdec = &h->fdec->plane[i][i_pix_offset];
955 for( j = 0; j < w; j++ )
956 h->mb.pic.p_fdec[i][-1+j*FDEC_STRIDE] = plane_fdec[-1+j*i_stride2];
958 for( j = 0; j < h->mb.pic.i_fref[0]; j++ )
960 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]];
962 for( k = 1; k < 4; k++ )
963 h->mb.pic.p_fref[0][j][k] = &fref[0][j >> h->mb.b_interlaced]->filtered[k][ref_pix_offset[j&1]];
965 if( h->sh.i_type == SLICE_TYPE_B )
966 for( j = 0; j < h->mb.pic.i_fref[1]; j++ )
968 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]];
970 for( k = 1; k < 4; k++ )
971 h->mb.pic.p_fref[1][j][k] = &fref[1][j >> h->mb.b_interlaced]->filtered[k][ref_pix_offset[j&1]];
975 void x264_macroblock_cache_load( x264_t *h, int i_mb_x, int i_mb_y )
977 int i_mb_xy = i_mb_y * h->mb.i_mb_stride + i_mb_x;
978 int i_mb_4x4 = 4*(i_mb_y * h->mb.i_b4_stride + i_mb_x);
979 int i_mb_8x8 = 2*(i_mb_y * h->mb.i_b8_stride + i_mb_x);
980 int i_top_y = i_mb_y - (1 << h->mb.b_interlaced);
981 int i_top_xy = i_top_y * h->mb.i_mb_stride + i_mb_x;
982 int i_top_4x4 = (4*i_top_y+3) * h->mb.i_b4_stride + 4*i_mb_x;
983 int i_top_8x8 = (2*i_top_y+1) * h->mb.i_b8_stride + 2*i_mb_x;
985 int i_top_type = -1; /* gcc warn */
991 h->mb.i_mb_x = i_mb_x;
992 h->mb.i_mb_y = i_mb_y;
993 h->mb.i_mb_xy = i_mb_xy;
994 h->mb.i_b8_xy = i_mb_8x8;
995 h->mb.i_b4_xy = i_mb_4x4;
996 h->mb.i_mb_top_xy = i_top_xy;
997 h->mb.i_neighbour = 0;
1000 if( i_top_xy >= h->sh.i_first_mb )
1002 h->mb.i_mb_type_top =
1003 i_top_type= h->mb.type[i_top_xy];
1005 h->mb.i_neighbour |= MB_TOP;
1008 *(uint32_t*)&h->mb.cache.intra4x4_pred_mode[x264_scan8[0] - 8] = *(uint32_t*)&h->mb.intra4x4_pred_mode[i_top_xy][0];
1010 /* load non_zero_count */
1011 *(uint32_t*)&h->mb.cache.non_zero_count[x264_scan8[0] - 8] = *(uint32_t*)&h->mb.non_zero_count[i_top_xy][12];
1012 /* shift because x264_scan8[16] is misaligned */
1013 *(uint32_t*)&h->mb.cache.non_zero_count[x264_scan8[16] - 9] = *(uint16_t*)&h->mb.non_zero_count[i_top_xy][18] << 8;
1014 *(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;
1018 h->mb.i_mb_type_top = -1;
1021 h->mb.cache.intra4x4_pred_mode[x264_scan8[0] - 8] =
1022 h->mb.cache.intra4x4_pred_mode[x264_scan8[1] - 8] =
1023 h->mb.cache.intra4x4_pred_mode[x264_scan8[4] - 8] =
1024 h->mb.cache.intra4x4_pred_mode[x264_scan8[5] - 8] = -1;
1026 /* load non_zero_count */
1027 h->mb.cache.non_zero_count[x264_scan8[0] - 8] =
1028 h->mb.cache.non_zero_count[x264_scan8[1] - 8] =
1029 h->mb.cache.non_zero_count[x264_scan8[4] - 8] =
1030 h->mb.cache.non_zero_count[x264_scan8[5] - 8] =
1031 h->mb.cache.non_zero_count[x264_scan8[16+0] - 8] =
1032 h->mb.cache.non_zero_count[x264_scan8[16+1] - 8] =
1033 h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 8] =
1034 h->mb.cache.non_zero_count[x264_scan8[16+4+1] - 8] = 0x80;
1037 if( i_mb_x > 0 && i_mb_xy > h->sh.i_first_mb )
1039 i_left_xy = i_mb_xy - 1;
1040 h->mb.i_mb_type_left =
1041 i_left_type = h->mb.type[i_left_xy];
1043 h->mb.i_neighbour |= MB_LEFT;
1046 h->mb.cache.intra4x4_pred_mode[x264_scan8[0 ] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][4];
1047 h->mb.cache.intra4x4_pred_mode[x264_scan8[2 ] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][5];
1048 h->mb.cache.intra4x4_pred_mode[x264_scan8[8 ] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][6];
1049 h->mb.cache.intra4x4_pred_mode[x264_scan8[10] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][3];
1051 /* load non_zero_count */
1052 h->mb.cache.non_zero_count[x264_scan8[0 ] - 1] = h->mb.non_zero_count[i_left_xy][3];
1053 h->mb.cache.non_zero_count[x264_scan8[2 ] - 1] = h->mb.non_zero_count[i_left_xy][7];
1054 h->mb.cache.non_zero_count[x264_scan8[8 ] - 1] = h->mb.non_zero_count[i_left_xy][11];
1055 h->mb.cache.non_zero_count[x264_scan8[10] - 1] = h->mb.non_zero_count[i_left_xy][15];
1057 h->mb.cache.non_zero_count[x264_scan8[16+0] - 1] = h->mb.non_zero_count[i_left_xy][16+1];
1058 h->mb.cache.non_zero_count[x264_scan8[16+2] - 1] = h->mb.non_zero_count[i_left_xy][16+3];
1060 h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 1] = h->mb.non_zero_count[i_left_xy][16+4+1];
1061 h->mb.cache.non_zero_count[x264_scan8[16+4+2] - 1] = h->mb.non_zero_count[i_left_xy][16+4+3];
1065 h->mb.i_mb_type_left = -1;
1067 h->mb.cache.intra4x4_pred_mode[x264_scan8[0 ] - 1] =
1068 h->mb.cache.intra4x4_pred_mode[x264_scan8[2 ] - 1] =
1069 h->mb.cache.intra4x4_pred_mode[x264_scan8[8 ] - 1] =
1070 h->mb.cache.intra4x4_pred_mode[x264_scan8[10] - 1] = -1;
1072 /* load non_zero_count */
1073 h->mb.cache.non_zero_count[x264_scan8[0 ] - 1] =
1074 h->mb.cache.non_zero_count[x264_scan8[2 ] - 1] =
1075 h->mb.cache.non_zero_count[x264_scan8[8 ] - 1] =
1076 h->mb.cache.non_zero_count[x264_scan8[10] - 1] =
1077 h->mb.cache.non_zero_count[x264_scan8[16+0] - 1] =
1078 h->mb.cache.non_zero_count[x264_scan8[16+2] - 1] =
1079 h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 1] =
1080 h->mb.cache.non_zero_count[x264_scan8[16+4+2] - 1] = 0x80;
1083 if( i_mb_x < h->sps->i_mb_width - 1 && i_top_xy + 1 >= h->sh.i_first_mb )
1085 h->mb.i_neighbour |= MB_TOPRIGHT;
1086 h->mb.i_mb_type_topright = h->mb.type[ i_top_xy + 1 ];
1089 h->mb.i_mb_type_topright = -1;
1090 if( i_mb_x > 0 && i_top_xy - 1 >= h->sh.i_first_mb )
1092 h->mb.i_neighbour |= MB_TOPLEFT;
1093 h->mb.i_mb_type_topleft = h->mb.type[ i_top_xy - 1 ];
1096 h->mb.i_mb_type_topleft = -1;
1098 if( h->pps->b_transform_8x8_mode )
1100 h->mb.cache.i_neighbour_transform_size =
1101 ( i_left_type >= 0 && h->mb.mb_transform_size[i_left_xy] )
1102 + ( i_top_type >= 0 && h->mb.mb_transform_size[i_top_xy] );
1107 h->mb.pic.i_fref[0] = h->i_ref0 << h->mb.b_interlaced;
1108 h->mb.pic.i_fref[1] = h->i_ref1 << h->mb.b_interlaced;
1109 h->mb.cache.i_neighbour_interlaced =
1110 !!(h->mb.i_neighbour & MB_LEFT)
1111 + !!(h->mb.i_neighbour & MB_TOP);
1114 if( !h->mb.b_interlaced )
1116 copy_column8( h->mb.pic.p_fdec[0]-1+ 4*FDEC_STRIDE, h->mb.pic.p_fdec[0]+15+ 4*FDEC_STRIDE );
1117 copy_column8( h->mb.pic.p_fdec[0]-1+12*FDEC_STRIDE, h->mb.pic.p_fdec[0]+15+12*FDEC_STRIDE );
1118 copy_column8( h->mb.pic.p_fdec[1]-1+ 4*FDEC_STRIDE, h->mb.pic.p_fdec[1]+ 7+ 4*FDEC_STRIDE );
1119 copy_column8( h->mb.pic.p_fdec[2]-1+ 4*FDEC_STRIDE, h->mb.pic.p_fdec[2]+ 7+ 4*FDEC_STRIDE );
1122 /* load picture pointers */
1123 x264_macroblock_load_pic_pointers( h, i_mb_x, i_mb_y, 0 );
1124 x264_macroblock_load_pic_pointers( h, i_mb_x, i_mb_y, 1 );
1125 x264_macroblock_load_pic_pointers( h, i_mb_x, i_mb_y, 2 );
1127 if( h->fdec->integral )
1129 assert( !h->mb.b_interlaced );
1130 for( i = 0; i < h->mb.pic.i_fref[0]; i++ )
1131 h->mb.pic.p_integral[0][i] = &h->fref0[i]->integral[ 16 * ( i_mb_x + i_mb_y * h->fdec->i_stride[0] )];
1132 for( i = 0; i < h->mb.pic.i_fref[1]; i++ )
1133 h->mb.pic.p_integral[1][i] = &h->fref1[i]->integral[ 16 * ( i_mb_x + i_mb_y * h->fdec->i_stride[0] )];
1136 x264_prefetch_fenc( h, h->fenc, i_mb_x, i_mb_y );
1138 /* load ref/mv/mvd */
1139 if( h->sh.i_type != SLICE_TYPE_I )
1141 const int s8x8 = h->mb.i_b8_stride;
1142 const int s4x4 = h->mb.i_b4_stride;
1146 for( i_list = 0; i_list < (h->sh.i_type == SLICE_TYPE_B ? 2 : 1 ); i_list++ )
1149 h->mb.cache.ref[i_list][x264_scan8[5 ]+1] =
1150 h->mb.cache.ref[i_list][x264_scan8[7 ]+1] =
1151 h->mb.cache.ref[i_list][x264_scan8[13]+1] = -2;
1154 if( h->mb.i_neighbour & MB_TOPLEFT )
1156 const int i8 = x264_scan8[0] - 1 - 1*8;
1157 const int ir = i_top_8x8 - 1;
1158 const int iv = i_top_4x4 - 1;
1159 h->mb.cache.ref[i_list][i8] = h->mb.ref[i_list][ir];
1160 *(uint32_t*)h->mb.cache.mv[i_list][i8] = *(uint32_t*)h->mb.mv[i_list][iv];
1164 const int i8 = x264_scan8[0] - 1 - 1*8;
1165 h->mb.cache.ref[i_list][i8] = -2;
1166 *(uint32_t*)h->mb.cache.mv[i_list][i8] = 0;
1169 if( h->mb.i_neighbour & MB_TOP )
1171 const int i8 = x264_scan8[0] - 8;
1172 const int ir = i_top_8x8;
1173 const int iv = i_top_4x4;
1174 h->mb.cache.ref[i_list][i8+0] =
1175 h->mb.cache.ref[i_list][i8+1] = h->mb.ref[i_list][ir + 0];
1176 h->mb.cache.ref[i_list][i8+2] =
1177 h->mb.cache.ref[i_list][i8+3] = h->mb.ref[i_list][ir + 1];
1178 *(uint64_t*)h->mb.cache.mv[i_list][i8+0] = *(uint64_t*)h->mb.mv[i_list][iv+0];
1179 *(uint64_t*)h->mb.cache.mv[i_list][i8+2] = *(uint64_t*)h->mb.mv[i_list][iv+2];
1183 const int i8 = x264_scan8[0] - 8;
1184 *(uint64_t*)h->mb.cache.mv[i_list][i8+0] = 0;
1185 *(uint64_t*)h->mb.cache.mv[i_list][i8+2] = 0;
1186 *(uint32_t*)&h->mb.cache.ref[i_list][i8] = (uint8_t)(-2) * 0x01010101U;
1189 if( h->mb.i_neighbour & MB_TOPRIGHT )
1191 const int i8 = x264_scan8[0] + 4 - 1*8;
1192 const int ir = i_top_8x8 + 2;
1193 const int iv = i_top_4x4 + 4;
1194 h->mb.cache.ref[i_list][i8] = h->mb.ref[i_list][ir];
1195 *(uint32_t*)h->mb.cache.mv[i_list][i8] = *(uint32_t*)h->mb.mv[i_list][iv];
1199 const int i8 = x264_scan8[0] + 4 - 1*8;
1200 h->mb.cache.ref[i_list][i8] = -2;
1201 *(uint32_t*)h->mb.cache.mv[i_list][i8] = 0;
1204 if( h->mb.i_neighbour & MB_LEFT )
1206 const int i8 = x264_scan8[0] - 1;
1207 const int ir = i_mb_8x8 - 1;
1208 const int iv = i_mb_4x4 - 1;
1209 h->mb.cache.ref[i_list][i8+0*8] =
1210 h->mb.cache.ref[i_list][i8+1*8] = h->mb.ref[i_list][ir + 0*s8x8];
1211 h->mb.cache.ref[i_list][i8+2*8] =
1212 h->mb.cache.ref[i_list][i8+3*8] = h->mb.ref[i_list][ir + 1*s8x8];
1214 *(uint32_t*)h->mb.cache.mv[i_list][i8+0*8] = *(uint32_t*)h->mb.mv[i_list][iv + 0*s4x4];
1215 *(uint32_t*)h->mb.cache.mv[i_list][i8+1*8] = *(uint32_t*)h->mb.mv[i_list][iv + 1*s4x4];
1216 *(uint32_t*)h->mb.cache.mv[i_list][i8+2*8] = *(uint32_t*)h->mb.mv[i_list][iv + 2*s4x4];
1217 *(uint32_t*)h->mb.cache.mv[i_list][i8+3*8] = *(uint32_t*)h->mb.mv[i_list][iv + 3*s4x4];
1221 const int i8 = x264_scan8[0] - 1;
1222 for( i = 0; i < 4; i++ )
1224 h->mb.cache.ref[i_list][i8+i*8] = -2;
1225 *(uint32_t*)h->mb.cache.mv[i_list][i8+i*8] = 0;
1229 if( h->param.b_cabac )
1231 if( i_top_type >= 0 )
1233 const int i8 = x264_scan8[0] - 8;
1234 const int iv = i_top_4x4;
1235 *(uint64_t*)h->mb.cache.mvd[i_list][i8+0] = *(uint64_t*)h->mb.mvd[i_list][iv+0];
1236 *(uint64_t*)h->mb.cache.mvd[i_list][i8+2] = *(uint64_t*)h->mb.mvd[i_list][iv+2];
1240 const int i8 = x264_scan8[0] - 8;
1241 *(uint64_t*)h->mb.cache.mvd[i_list][i8+0] =
1242 *(uint64_t*)h->mb.cache.mvd[i_list][i8+2] = 0;
1245 if( i_left_type >= 0 )
1247 const int i8 = x264_scan8[0] - 1;
1248 const int iv = i_mb_4x4 - 1;
1249 *(uint32_t*)h->mb.cache.mvd[i_list][i8+0*8] = *(uint32_t*)h->mb.mvd[i_list][iv + 0*s4x4];
1250 *(uint32_t*)h->mb.cache.mvd[i_list][i8+1*8] = *(uint32_t*)h->mb.mvd[i_list][iv + 1*s4x4];
1251 *(uint32_t*)h->mb.cache.mvd[i_list][i8+2*8] = *(uint32_t*)h->mb.mvd[i_list][iv + 2*s4x4];
1252 *(uint32_t*)h->mb.cache.mvd[i_list][i8+3*8] = *(uint32_t*)h->mb.mvd[i_list][iv + 3*s4x4];
1256 const int i8 = x264_scan8[0] - 1;
1257 for( i = 0; i < 4; i++ )
1258 *(uint32_t*)h->mb.cache.mvd[i_list][i8+i*8] = 0;
1264 if( h->sh.i_type == SLICE_TYPE_B && h->param.b_cabac )
1267 x264_macroblock_cache_skip( h, 0, 0, 4, 4, 0 );
1268 skipbp = i_left_type >= 0 ? h->mb.skipbp[i_left_xy] : 0;
1269 h->mb.cache.skip[x264_scan8[0] - 1] = skipbp & 0x2;
1270 h->mb.cache.skip[x264_scan8[8] - 1] = skipbp & 0x8;
1271 skipbp = i_top_type >= 0 ? h->mb.skipbp[i_top_xy] : 0;
1272 h->mb.cache.skip[x264_scan8[0] - 8] = skipbp & 0x4;
1273 h->mb.cache.skip[x264_scan8[4] - 8] = skipbp & 0x8;
1276 if( h->sh.i_type == SLICE_TYPE_P )
1277 x264_mb_predict_mv_pskip( h, h->mb.cache.pskip_mv );
1280 h->mb.i_neighbour4[0] =
1281 h->mb.i_neighbour8[0] = (h->mb.i_neighbour & (MB_TOP|MB_LEFT|MB_TOPLEFT))
1282 | ((h->mb.i_neighbour & MB_TOP) ? MB_TOPRIGHT : 0);
1283 h->mb.i_neighbour4[4] =
1284 h->mb.i_neighbour4[1] = MB_LEFT | ((h->mb.i_neighbour & MB_TOP) ? (MB_TOP|MB_TOPLEFT|MB_TOPRIGHT) : 0);
1285 h->mb.i_neighbour4[2] =
1286 h->mb.i_neighbour4[8] =
1287 h->mb.i_neighbour4[10] =
1288 h->mb.i_neighbour8[2] = MB_TOP|MB_TOPRIGHT | ((h->mb.i_neighbour & MB_LEFT) ? (MB_LEFT|MB_TOPLEFT) : 0);
1289 h->mb.i_neighbour4[5] =
1290 h->mb.i_neighbour8[1] = MB_LEFT | (h->mb.i_neighbour & MB_TOPRIGHT)
1291 | ((h->mb.i_neighbour & MB_TOP) ? MB_TOP|MB_TOPLEFT : 0);
1294 static void ALWAYS_INLINE x264_macroblock_store_pic( x264_t *h, int i)
1297 int i_stride = h->fdec->i_stride[!!i];
1298 int i_stride2 = i_stride << h->mb.b_interlaced;
1299 int i_pix_offset = h->mb.b_interlaced
1300 ? w * (h->mb.i_mb_x + (h->mb.i_mb_y&~1) * i_stride) + (h->mb.i_mb_y&1) * i_stride
1301 : w * (h->mb.i_mb_x + h->mb.i_mb_y * i_stride);
1302 h->mc.copy[i?PIXEL_8x8:PIXEL_16x16](
1303 &h->fdec->plane[i][i_pix_offset], i_stride2,
1304 h->mb.pic.p_fdec[i], FDEC_STRIDE, w );
1307 void x264_macroblock_cache_save( x264_t *h )
1309 const int i_mb_xy = h->mb.i_mb_xy;
1310 const int i_mb_type = x264_mb_type_fix[h->mb.i_type];
1311 const int s8x8 = h->mb.i_b8_stride;
1312 const int s4x4 = h->mb.i_b4_stride;
1313 const int i_mb_4x4 = h->mb.i_b4_xy;
1314 const int i_mb_8x8 = h->mb.i_b8_xy;
1316 /* GCC pessimizes direct stores to heap-allocated 8-bit arrays due to aliasing.*/
1317 /* By only dereferencing them once, we avoid this issue. */
1318 int8_t *intra4x4_pred_mode = h->mb.intra4x4_pred_mode[i_mb_xy];
1319 uint8_t *non_zero_count = h->mb.non_zero_count[i_mb_xy];
1323 x264_macroblock_store_pic( h, 0 );
1324 x264_macroblock_store_pic( h, 1 );
1325 x264_macroblock_store_pic( h, 2 );
1327 x264_prefetch_fenc( h, h->fdec, h->mb.i_mb_x, h->mb.i_mb_y );
1329 h->mb.type[i_mb_xy] = i_mb_type;
1330 h->mb.i_mb_prev_xy = i_mb_xy;
1333 if( i_mb_type == I_4x4 )
1335 *(uint32_t*)&intra4x4_pred_mode[0] = *(uint32_t*)&h->mb.cache.intra4x4_pred_mode[x264_scan8[10] ];
1336 *(uint32_t*)&intra4x4_pred_mode[4] = pack8to32(h->mb.cache.intra4x4_pred_mode[x264_scan8[5] ],
1337 h->mb.cache.intra4x4_pred_mode[x264_scan8[7] ],
1338 h->mb.cache.intra4x4_pred_mode[x264_scan8[13] ], 0);
1341 *(uint64_t*)intra4x4_pred_mode = I_PRED_4x4_DC * 0x0101010101010101ULL;
1343 if( i_mb_type == I_PCM )
1345 h->mb.qp[i_mb_xy] = 0;
1346 h->mb.i_last_dqp = 0;
1347 h->mb.i_cbp_chroma = 2;
1348 h->mb.i_cbp_luma = 0xf;
1349 h->mb.cbp[i_mb_xy] = 0x72f; /* all set */
1350 h->mb.b_transform_8x8 = 0;
1351 for( i = 0; i < 16 + 2*4; i++ )
1352 non_zero_count[i] = 16;
1356 /* save non zero count */
1357 *(uint32_t*)&non_zero_count[0*4] = *(uint32_t*)&h->mb.cache.non_zero_count[x264_scan8[0]+0*8];
1358 *(uint32_t*)&non_zero_count[1*4] = *(uint32_t*)&h->mb.cache.non_zero_count[x264_scan8[0]+1*8];
1359 *(uint32_t*)&non_zero_count[2*4] = *(uint32_t*)&h->mb.cache.non_zero_count[x264_scan8[0]+2*8];
1360 *(uint32_t*)&non_zero_count[3*4] = *(uint32_t*)&h->mb.cache.non_zero_count[x264_scan8[0]+3*8];
1361 *(uint16_t*)&non_zero_count[16+0*2] = *(uint32_t*)&h->mb.cache.non_zero_count[x264_scan8[16+0*2]-1] >> 8;
1362 *(uint16_t*)&non_zero_count[16+1*2] = *(uint32_t*)&h->mb.cache.non_zero_count[x264_scan8[16+1*2]-1] >> 8;
1363 *(uint16_t*)&non_zero_count[16+2*2] = *(uint32_t*)&h->mb.cache.non_zero_count[x264_scan8[16+2*2]-1] >> 8;
1364 *(uint16_t*)&non_zero_count[16+3*2] = *(uint32_t*)&h->mb.cache.non_zero_count[x264_scan8[16+3*2]-1] >> 8;
1366 if( h->mb.i_type != I_16x16 && h->mb.i_cbp_luma == 0 && h->mb.i_cbp_chroma == 0 )
1367 h->mb.i_qp = h->mb.i_last_qp;
1368 h->mb.qp[i_mb_xy] = h->mb.i_qp;
1369 h->mb.i_last_dqp = h->mb.i_qp - h->mb.i_last_qp;
1370 h->mb.i_last_qp = h->mb.i_qp;
1373 if( h->mb.i_cbp_luma == 0 && h->mb.i_type != I_8x8 )
1374 h->mb.b_transform_8x8 = 0;
1375 h->mb.mb_transform_size[i_mb_xy] = h->mb.b_transform_8x8;
1377 if( h->sh.i_type != SLICE_TYPE_I )
1379 if( !IS_INTRA( i_mb_type ) )
1381 h->mb.ref[0][i_mb_8x8+0+0*s8x8] = h->mb.cache.ref[0][x264_scan8[0]];
1382 h->mb.ref[0][i_mb_8x8+1+0*s8x8] = h->mb.cache.ref[0][x264_scan8[4]];
1383 h->mb.ref[0][i_mb_8x8+0+1*s8x8] = h->mb.cache.ref[0][x264_scan8[8]];
1384 h->mb.ref[0][i_mb_8x8+1+1*s8x8] = h->mb.cache.ref[0][x264_scan8[12]];
1385 for( y = 0; y < 4; y++ )
1387 *(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];
1388 *(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];
1390 if( h->sh.i_type == SLICE_TYPE_B )
1392 h->mb.ref[1][i_mb_8x8+0+0*s8x8] = h->mb.cache.ref[1][x264_scan8[0]];
1393 h->mb.ref[1][i_mb_8x8+1+0*s8x8] = h->mb.cache.ref[1][x264_scan8[4]];
1394 h->mb.ref[1][i_mb_8x8+0+1*s8x8] = h->mb.cache.ref[1][x264_scan8[8]];
1395 h->mb.ref[1][i_mb_8x8+1+1*s8x8] = h->mb.cache.ref[1][x264_scan8[12]];
1396 for( y = 0; y < 4; y++ )
1398 *(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];
1399 *(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];
1406 for( i_list = 0; i_list < (h->sh.i_type == SLICE_TYPE_B ? 2 : 1 ); i_list++ )
1408 *(uint16_t*)&h->mb.ref[i_list][i_mb_8x8+0*s8x8] = (uint8_t)(-1) * 0x0101;
1409 *(uint16_t*)&h->mb.ref[i_list][i_mb_8x8+1*s8x8] = (uint8_t)(-1) * 0x0101;
1410 for( y = 0; y < 4; y++ )
1412 *(uint64_t*)h->mb.mv[i_list][i_mb_4x4+y*s4x4+0] = 0;
1413 *(uint64_t*)h->mb.mv[i_list][i_mb_4x4+y*s4x4+2] = 0;
1419 if( h->param.b_cabac )
1421 if( IS_INTRA(i_mb_type) && i_mb_type != I_PCM )
1422 h->mb.chroma_pred_mode[i_mb_xy] = x264_mb_pred_mode8x8c_fix[ h->mb.i_chroma_pred_mode ];
1424 h->mb.chroma_pred_mode[i_mb_xy] = I_PRED_CHROMA_DC;
1426 if( !IS_INTRA( i_mb_type ) && !IS_SKIP( i_mb_type ) && !IS_DIRECT( i_mb_type ) )
1428 for( y = 0; y < 4; y++ )
1430 *(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];
1431 *(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];
1433 if( h->sh.i_type == SLICE_TYPE_B )
1434 for( y = 0; y < 4; y++ )
1436 *(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];
1437 *(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];
1442 for( y = 0; y < 4; y++ )
1444 *(uint64_t*)h->mb.mvd[0][i_mb_4x4+y*s4x4+0] = 0;
1445 *(uint64_t*)h->mb.mvd[0][i_mb_4x4+y*s4x4+2] = 0;
1447 if( h->sh.i_type == SLICE_TYPE_B )
1448 for( y = 0; y < 4; y++ )
1450 *(uint64_t*)h->mb.mvd[1][i_mb_4x4+y*s4x4+0] = 0;
1451 *(uint64_t*)h->mb.mvd[1][i_mb_4x4+y*s4x4+2] = 0;
1455 if( h->sh.i_type == SLICE_TYPE_B )
1457 if( i_mb_type == B_SKIP || i_mb_type == B_DIRECT )
1458 h->mb.skipbp[i_mb_xy] = 0xf;
1459 else if( i_mb_type == B_8x8 )
1462 for( i = 0; i < 4; i++ )
1463 skipbp |= ( h->mb.i_sub_partition[i] == D_DIRECT_8x8 ) << i;
1464 h->mb.skipbp[i_mb_xy] = skipbp;
1467 h->mb.skipbp[i_mb_xy] = 0;
1472 void x264_macroblock_bipred_init( x264_t *h )
1475 for( i_ref0 = 0; i_ref0 < h->i_ref0; i_ref0++ )
1477 int poc0 = h->fref0[i_ref0]->i_poc;
1478 for( i_ref1 = 0; i_ref1 < h->i_ref1; i_ref1++ )
1480 int dist_scale_factor;
1481 int poc1 = h->fref1[i_ref1]->i_poc;
1482 int td = x264_clip3( poc1 - poc0, -128, 127 );
1483 if( td == 0 /* || pic0 is a long-term ref */ )
1484 dist_scale_factor = 256;
1487 int tb = x264_clip3( h->fdec->i_poc - poc0, -128, 127 );
1488 int tx = (16384 + (abs(td) >> 1)) / td;
1489 dist_scale_factor = x264_clip3( (tb * tx + 32) >> 6, -1024, 1023 );
1491 h->mb.dist_scale_factor[i_ref0][i_ref1] = dist_scale_factor;
1493 dist_scale_factor >>= 2;
1494 if( h->param.analyse.b_weighted_bipred
1495 && dist_scale_factor >= -64
1496 && dist_scale_factor <= 128 )
1498 h->mb.bipred_weight[i_ref0][i_ref1] = 64 - dist_scale_factor;
1499 // ssse3 implementation of biweight doesn't support the extrema.
1500 // if we ever generate them, we'll have to drop that optimization.
1501 assert( dist_scale_factor >= -63 && dist_scale_factor <= 127 );
1504 h->mb.bipred_weight[i_ref0][i_ref1] = 32;
1509 for( i_ref0 = 2*h->i_ref0-1; i_ref0 >= 0; i_ref0-- )
1510 for( i_ref1 = 2*h->i_ref1-1; i_ref1 >= 0; i_ref1-- )
1511 h->mb.bipred_weight[i_ref0][i_ref1] = h->mb.bipred_weight[i_ref0>>1][i_ref1>>1];