1 /*****************************************************************************
2 * macroblock.c: h264 encoder library
3 *****************************************************************************
4 * Copyright (C) 2003 Laurent Aimar
5 * $Id: macroblock.c,v 1.1 2004/06/03 19:27:06 fenrir Exp $
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
26 int x264_mb_predict_intra4x4_mode( x264_t *h, int idx )
28 const int ma = h->mb.cache.intra4x4_pred_mode[x264_scan8[idx] - 1];
29 const int mb = h->mb.cache.intra4x4_pred_mode[x264_scan8[idx] - 8];
30 const int m = X264_MIN( x264_mb_pred_mode4x4_fix(ma),
31 x264_mb_pred_mode4x4_fix(mb) );
39 int x264_mb_predict_non_zero_code( x264_t *h, int idx )
41 const int za = h->mb.cache.non_zero_count[x264_scan8[idx] - 1];
42 const int zb = h->mb.cache.non_zero_count[x264_scan8[idx] - 8];
48 i_ret = ( i_ret + 1 ) >> 1;
53 int x264_mb_transform_8x8_allowed( x264_t *h )
55 // intra and skip are disallowed
56 // large partitions are allowed
57 // direct and 8x8 are conditional
58 static const uint8_t partition_tab[X264_MBTYPE_MAX] = {
59 0,0,0,0,1,2,0,2,1,1,1,1,1,1,1,1,1,2,0,
63 if( !h->pps->b_transform_8x8_mode )
65 p = partition_tab[h->mb.i_type];
68 else if( h->mb.i_type == B_DIRECT )
69 return h->sps->b_direct8x8_inference;
70 else if( h->mb.i_type == P_8x8 )
72 if( !(h->param.analyse.inter & X264_ANALYSE_PSUB8x8) )
75 if( h->mb.i_sub_partition[i] != D_L0_8x8 )
81 // x264 currently doesn't use sub-8x8 B partitions, so don't check for them
82 if( h->sps->b_direct8x8_inference )
85 if( h->mb.i_sub_partition[i] == D_DIRECT_8x8 )
91 void x264_mb_predict_mv( x264_t *h, int i_list, int idx, int i_width, int16_t mvp[2] )
93 const int i8 = x264_scan8[idx];
94 const int i_ref= h->mb.cache.ref[i_list][i8];
95 int i_refa = h->mb.cache.ref[i_list][i8 - 1];
96 int16_t *mv_a = h->mb.cache.mv[i_list][i8 - 1];
97 int i_refb = h->mb.cache.ref[i_list][i8 - 8];
98 int16_t *mv_b = h->mb.cache.mv[i_list][i8 - 8];
99 int i_refc = h->mb.cache.ref[i_list][i8 - 8 + i_width ];
100 int16_t *mv_c = h->mb.cache.mv[i_list][i8 - 8 + i_width];
104 if( (idx&0x03) == 3 || ( i_width == 2 && (idx&0x3) == 2 )|| i_refc == -2 )
106 i_refc = h->mb.cache.ref[i_list][i8 - 8 - 1];
107 mv_c = h->mb.cache.mv[i_list][i8 - 8 - 1];
110 if( h->mb.i_partition == D_16x8 )
112 if( idx == 0 && i_refb == i_ref )
114 *(uint32_t*)mvp = *(uint32_t*)mv_b;
117 else if( idx != 0 && i_refa == i_ref )
119 *(uint32_t*)mvp = *(uint32_t*)mv_a;
123 else if( h->mb.i_partition == D_8x16 )
125 if( idx == 0 && i_refa == i_ref )
127 *(uint32_t*)mvp = *(uint32_t*)mv_a;
130 else if( idx != 0 && i_refc == i_ref )
132 *(uint32_t*)mvp = *(uint32_t*)mv_c;
138 if( i_refa == i_ref ) i_count++;
139 if( i_refb == i_ref ) i_count++;
140 if( i_refc == i_ref ) i_count++;
143 x264_median_mv( mvp, mv_a, mv_b, mv_c );
144 else if( i_count == 1 )
146 if( i_refa == i_ref )
147 *(uint32_t*)mvp = *(uint32_t*)mv_a;
148 else if( i_refb == i_ref )
149 *(uint32_t*)mvp = *(uint32_t*)mv_b;
151 *(uint32_t*)mvp = *(uint32_t*)mv_c;
153 else if( i_refb == -2 && i_refc == -2 && i_refa != -2 )
154 *(uint32_t*)mvp = *(uint32_t*)mv_a;
156 x264_median_mv( mvp, mv_a, mv_b, mv_c );
159 void x264_mb_predict_mv_16x16( x264_t *h, int i_list, int i_ref, int16_t mvp[2] )
161 int i_refa = h->mb.cache.ref[i_list][X264_SCAN8_0 - 1];
162 int16_t *mv_a = h->mb.cache.mv[i_list][X264_SCAN8_0 - 1];
163 int i_refb = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8];
164 int16_t *mv_b = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8];
165 int i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 + 4];
166 int16_t *mv_c = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8 + 4];
172 i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 - 1];
173 mv_c = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8 - 1];
177 if( i_refa == i_ref ) i_count++;
178 if( i_refb == i_ref ) i_count++;
179 if( i_refc == i_ref ) i_count++;
182 x264_median_mv( mvp, mv_a, mv_b, mv_c );
183 else if( i_count == 1 )
185 if( i_refa == i_ref )
186 *(uint32_t*)mvp = *(uint32_t*)mv_a;
187 else if( i_refb == i_ref )
188 *(uint32_t*)mvp = *(uint32_t*)mv_b;
190 *(uint32_t*)mvp = *(uint32_t*)mv_c;
192 else if( i_refb == -2 && i_refc == -2 && i_refa != -2 )
193 *(uint32_t*)mvp = *(uint32_t*)mv_a;
195 x264_median_mv( mvp, mv_a, mv_b, mv_c );
199 void x264_mb_predict_mv_pskip( x264_t *h, int16_t mv[2] )
201 int i_refa = h->mb.cache.ref[0][X264_SCAN8_0 - 1];
202 int i_refb = h->mb.cache.ref[0][X264_SCAN8_0 - 8];
203 int16_t *mv_a = h->mb.cache.mv[0][X264_SCAN8_0 - 1];
204 int16_t *mv_b = h->mb.cache.mv[0][X264_SCAN8_0 - 8];
206 if( i_refa == -2 || i_refb == -2 ||
207 ( i_refa == 0 && *(uint32_t*)mv_a == 0 ) ||
208 ( i_refb == 0 && *(uint32_t*)mv_b == 0 ) )
214 x264_mb_predict_mv_16x16( h, 0, 0, mv );
218 static int x264_mb_predict_mv_direct16x16_temporal( x264_t *h )
220 int i_mb_4x4 = 16 * h->mb.i_mb_stride * h->mb.i_mb_y + 4 * h->mb.i_mb_x;
221 int i_mb_8x8 = 4 * h->mb.i_mb_stride * h->mb.i_mb_y + 2 * h->mb.i_mb_x;
224 const int type_col = h->fref1[0]->mb_type[ h->mb.i_mb_xy ];
226 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, 0 );
228 if( IS_INTRA( type_col ) )
230 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, 0 );
231 x264_macroblock_cache_mv( h, 0, 0, 4, 4, 0, 0 );
232 x264_macroblock_cache_mv( h, 0, 0, 4, 4, 1, 0 );
235 b8x8 = h->sps->b_direct8x8_inference ||
236 (type_col != P_8x8 && type_col != B_SKIP && type_col != B_DIRECT && type_col != B_8x8);
238 for( i8 = 0; i8 < 4; i8++ )
242 const int i_part_8x8 = i_mb_8x8 + x8 + y8 * h->mb.i_b8_stride;
243 const int i_ref = h->mb.map_col_to_list0[ h->fref1[0]->ref[0][ i_part_8x8 ] ];
247 const int dist_scale_factor = h->mb.dist_scale_factor[i_ref][0];
249 x264_macroblock_cache_ref( h, 2*x8, 2*y8, 2, 2, 0, i_ref );
253 const int16_t *mv_col = h->fref1[0]->mv[0][ i_mb_4x4 + 3*x8 + 3*y8 * h->mb.i_b4_stride];
254 const int l0x = ( dist_scale_factor * mv_col[0] + 128 ) >> 8;
255 const int l0y = ( dist_scale_factor * mv_col[1] + 128 ) >> 8;
256 x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 0, pack16to32_mask(l0x, l0y) );
257 x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 1, pack16to32_mask(l0x-mv_col[0], l0y-mv_col[1]) );
261 for( i4 = 0; i4 < 4; i4++ )
263 const int x4 = i4%2 + 2*x8;
264 const int y4 = i4/2 + 2*y8;
265 const int16_t *mv_col = h->fref1[0]->mv[0][ i_mb_4x4 + x4 + y4 * h->mb.i_b4_stride ];
266 const int l0x = ( dist_scale_factor * mv_col[0] + 128 ) >> 8;
267 const int l0y = ( dist_scale_factor * mv_col[1] + 128 ) >> 8;
268 x264_macroblock_cache_mv( h, x4, y4, 1, 1, 0, pack16to32_mask(l0x, l0y) );
269 x264_macroblock_cache_mv( h, x4, y4, 1, 1, 1, pack16to32_mask(l0x-mv_col[0], l0y-mv_col[1]) );
275 /* the collocated ref isn't in the current list0 */
276 /* FIXME: we might still be able to use direct_8x8 on some partitions */
277 /* FIXME: with B-pyramid + extensive ref list reordering
278 * (not currently used), we would also have to check
279 * l1mv1 like in spatial mode */
284 if( h->param.i_threads > 1 )
286 int di = b8x8 ? 4 : 1;
287 for( i4=0; i4<16; i4+=di )
289 if( h->mb.cache.mv[0][x264_scan8[i4]][1] > h->mb.mv_max_spel[1]
290 || h->mb.cache.mv[1][x264_scan8[i4]][1] > h->mb.mv_max_spel[1] )
293 fprintf(stderr, "direct_temporal: (%d,%d) (%d,%d) > %d \n",
294 h->mb.cache.mv[0][x264_scan8[i4]][0],
295 h->mb.cache.mv[0][x264_scan8[i4]][1],
296 h->mb.cache.mv[1][x264_scan8[i4]][0],
297 h->mb.cache.mv[1][x264_scan8[i4]][1],
298 h->mb.mv_max_spel[1]);
308 static int x264_mb_predict_mv_direct16x16_spatial( x264_t *h )
311 DECLARE_ALIGNED_8( int16_t mv[2][2] );
315 const int8_t *l1ref0 = &h->fref1[0]->ref[0][ h->mb.i_b8_xy ];
316 const int8_t *l1ref1 = &h->fref1[0]->ref[1][ h->mb.i_b8_xy ];
317 const int16_t (*l1mv0)[2] = (const int16_t (*)[2]) &h->fref1[0]->mv[0][ h->mb.i_b4_xy ];
318 const int16_t (*l1mv1)[2] = (const int16_t (*)[2]) &h->fref1[0]->mv[1][ h->mb.i_b4_xy ];
319 const int type_col = h->fref1[0]->mb_type[ h->mb.i_mb_xy ];
321 for( i_list=0; i_list<2; i_list++ )
323 int i_refa = h->mb.cache.ref[i_list][X264_SCAN8_0 - 1];
324 int i_refb = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8];
325 int i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 + 4];
327 i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 - 1];
329 ref[i_list] = i_refa;
330 if( ref[i_list] < 0 || ( i_refb < ref[i_list] && i_refb >= 0 ))
331 ref[i_list] = i_refb;
332 if( ref[i_list] < 0 || ( i_refc < ref[i_list] && i_refc >= 0 ))
333 ref[i_list] = i_refc;
334 if( ref[i_list] < 0 )
338 if( ref[0] < 0 && ref[1] < 0 )
342 *(uint64_t*)mv[0] = 0;
346 for( i_list=0; i_list<2; i_list++ )
348 if( ref[i_list] >= 0 )
349 x264_mb_predict_mv_16x16( h, i_list, ref[i_list], mv[i_list] );
351 *(uint32_t*)mv[i_list] = 0;
355 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, ref[0] );
356 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, ref[1] );
357 x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 0, mv[0] );
358 x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, 1, mv[1] );
360 if( IS_INTRA( type_col ) )
363 if( h->param.i_threads > 1
364 && ( mv[0][1] > h->mb.mv_max_spel[1]
365 || mv[1][1] > h->mb.mv_max_spel[1] ) )
368 fprintf(stderr, "direct_spatial: (%d,%d) (%d,%d) > %d \n",
369 mv[0][0], mv[0][1], mv[1][0], mv[1][1],
370 h->mb.mv_max_spel[1]);
375 b8x8 = h->sps->b_direct8x8_inference ||
376 (type_col != P_8x8 && type_col != B_SKIP && type_col != B_DIRECT && type_col != B_8x8);
379 for( i8=0; i8<4; i8++ )
383 const int o8 = x8 + y8 * h->mb.i_b8_stride;
384 if( l1ref0[o8] == 0 || ( l1ref0[o8] < 0 && l1ref1[o8] == 0 ) )
386 const int16_t (*l1mv)[2] = (l1ref0[o8] == 0) ? l1mv0 : l1mv1;
389 const int16_t *mvcol = l1mv[3*x8 + 3*y8 * h->mb.i_b4_stride];
390 if( abs( mvcol[0] ) <= 1 && abs( mvcol[1] ) <= 1 )
393 x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 0, 0 );
395 x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 1, 0 );
400 for( i4=0; i4<4; i4++ )
402 const int x4 = i4%2 + 2*x8;
403 const int y4 = i4/2 + 2*y8;
404 const int16_t *mvcol = l1mv[x4 + y4 * h->mb.i_b4_stride];
405 if( abs( mvcol[0] ) <= 1 && abs( mvcol[1] ) <= 1 )
408 x264_macroblock_cache_mv( h, x4, y4, 1, 1, 0, 0 );
410 x264_macroblock_cache_mv( h, x4, y4, 1, 1, 1, 0 );
420 int x264_mb_predict_mv_direct16x16( x264_t *h, int *b_changed )
423 if( h->param.analyse.i_direct_mv_pred == X264_DIRECT_PRED_NONE )
425 else if( h->sh.b_direct_spatial_mv_pred )
426 b_available = x264_mb_predict_mv_direct16x16_spatial( h );
428 b_available = x264_mb_predict_mv_direct16x16_temporal( h );
430 if( b_changed != NULL && b_available )
432 int type_col = h->fref1[0]->mb_type[ h->mb.i_mb_xy ];
433 if( IS_INTRA(type_col) || type_col == P_SKIP )
435 *b_changed = h->mb.cache.direct_ref[0][0] != h->mb.cache.ref[0][X264_SCAN8_0]
436 || h->mb.cache.direct_ref[1][0] != h->mb.cache.ref[1][X264_SCAN8_0]
437 || *(uint32_t*)h->mb.cache.direct_mv[0][X264_SCAN8_0] != *(uint32_t*)h->mb.cache.mv[0][X264_SCAN8_0]
438 || *(uint32_t*)h->mb.cache.direct_mv[1][X264_SCAN8_0] != *(uint32_t*)h->mb.cache.mv[1][X264_SCAN8_0];
444 for( l = 0; l < 2; l++ )
445 for( i = 0; i < 4; i++ )
446 *b_changed |= h->mb.cache.direct_ref[l][i] != h->mb.cache.ref[l][x264_scan8[i*4]];
447 *b_changed = *b_changed || memcmp(h->mb.cache.direct_mv, h->mb.cache.mv, sizeof(h->mb.cache.mv));
457 for( l = 0; l < 2; l++ )
458 for( i = 0; i < 4; i++ )
459 h->mb.cache.direct_ref[l][i] = h->mb.cache.ref[l][x264_scan8[i*4]];
460 h->mc.memcpy_aligned(h->mb.cache.direct_mv, h->mb.cache.mv, sizeof(h->mb.cache.mv));
466 void x264_mb_load_mv_direct8x8( x264_t *h, int idx )
468 const int x = 2*(idx%2);
469 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 for( l = 0; l < 2; l++ )
475 *(uint64_t*)h->mb.cache.mv[l][x264_scan8[idx*4]] =
476 *(uint64_t*)h->mb.cache.direct_mv[l][x264_scan8[idx*4]];
477 *(uint64_t*)h->mb.cache.mv[l][x264_scan8[idx*4]+8] =
478 *(uint64_t*)h->mb.cache.direct_mv[l][x264_scan8[idx*4]+8];
482 #define FIXED_SCALE 256
484 /* This just improves encoder performance, it's not part of the spec */
485 void x264_mb_predict_mv_ref16x16( x264_t *h, int i_list, int i_ref, int16_t mvc[8][2], int *i_mvc )
487 int16_t (*mvr)[2] = h->mb.mvr[i_list][i_ref];
490 #define SET_MVP(mvp) { \
491 *(uint32_t*)mvc[i] = *(uint32_t*)mvp; \
496 if( h->sh.i_type == SLICE_TYPE_B
497 && h->mb.cache.ref[i_list][x264_scan8[12]] == i_ref )
499 SET_MVP( h->mb.cache.mv[i_list][x264_scan8[12]] );
502 /* spatial predictors */
503 if( h->mb.i_neighbour & MB_LEFT )
505 int i_mb_l = h->mb.i_mb_xy - 1;
506 /* skip MBs didn't go through the whole search process, so mvr is undefined */
507 if( !IS_SKIP( h->mb.type[i_mb_l] ) )
508 SET_MVP( mvr[i_mb_l] );
510 if( h->mb.i_neighbour & MB_TOP )
512 int i_mb_t = h->mb.i_mb_top_xy;
513 if( !IS_SKIP( h->mb.type[i_mb_t] ) )
514 SET_MVP( mvr[i_mb_t] );
516 if( h->mb.i_neighbour & MB_TOPLEFT && !IS_SKIP( h->mb.type[i_mb_t - 1] ) )
517 SET_MVP( mvr[i_mb_t-1] );
518 if( h->mb.i_mb_x < h->mb.i_mb_stride - 1 && !IS_SKIP( h->mb.type[i_mb_t + 1] ) )
519 SET_MVP( mvr[i_mb_t+1] );
523 /* temporal predictors */
524 /* FIXME temporal scaling w/ interlace */
525 if( h->fref0[0]->i_ref[0] > 0 && !h->sh.b_mbaff )
527 x264_frame_t *l0 = h->fref0[0];
529 #define SET_TMVP(dx, dy) { \
530 int i_b4 = h->mb.i_b4_xy + dx*4 + dy*4*h->mb.i_b4_stride; \
531 int i_b8 = h->mb.i_b8_xy + dx*2 + dy*2*h->mb.i_b8_stride; \
532 int ref_col = l0->ref[0][i_b8]; \
535 int scale = (h->fdec->i_poc - h->fdec->ref_poc[0][i_ref]) * l0->inv_ref_poc[ref_col];\
536 mvc[i][0] = l0->mv[0][i_b4][0] * scale / FIXED_SCALE; \
537 mvc[i][1] = l0->mv[0][i_b4][1] * scale / FIXED_SCALE; \
543 if( h->mb.i_mb_x < h->sps->i_mb_width-1 )
545 if( h->mb.i_mb_y < h->sps->i_mb_height-1 )
553 /* Set up a lookup table for delta pocs to reduce an IDIV to an IMUL */
554 static void setup_inverse_delta_pocs( x264_t *h )
557 for( i = 0; i < h->i_ref0; i++ )
559 int delta = h->fdec->i_poc - h->fref0[i]->i_poc;
560 h->fdec->inv_ref_poc[i] = (FIXED_SCALE + delta/2) / delta;
564 static inline void x264_mb_mc_0xywh( x264_t *h, int x, int y, int width, int height )
566 const int i8 = x264_scan8[0]+x+8*y;
567 const int i_ref = h->mb.cache.ref[0][i8];
568 const int mvx = x264_clip3( h->mb.cache.mv[0][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] );
569 int mvy = x264_clip3( h->mb.cache.mv[0][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] );
571 h->mc.mc_luma( &h->mb.pic.p_fdec[0][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE,
572 h->mb.pic.p_fref[0][i_ref], h->mb.pic.i_stride[0],
573 mvx + 4*4*x, mvy + 4*4*y, 4*width, 4*height );
575 // chroma is offset if MCing from a field of opposite parity
576 if( h->mb.b_interlaced & i_ref )
577 mvy += (h->mb.i_mb_y & 1)*4 - 2;
579 h->mc.mc_chroma( &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
580 &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],
581 mvx, mvy, 2*width, 2*height );
583 h->mc.mc_chroma( &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
584 &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],
585 mvx, mvy, 2*width, 2*height );
587 static inline void x264_mb_mc_1xywh( x264_t *h, int x, int y, int width, int height )
589 const int i8 = x264_scan8[0]+x+8*y;
590 const int i_ref = h->mb.cache.ref[1][i8];
591 const int mvx = x264_clip3( h->mb.cache.mv[1][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] );
592 int mvy = x264_clip3( h->mb.cache.mv[1][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] );
594 h->mc.mc_luma( &h->mb.pic.p_fdec[0][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE,
595 h->mb.pic.p_fref[1][i_ref], h->mb.pic.i_stride[0],
596 mvx + 4*4*x, mvy + 4*4*y, 4*width, 4*height );
598 if( h->mb.b_interlaced & i_ref )
599 mvy += (h->mb.i_mb_y & 1)*4 - 2;
601 h->mc.mc_chroma( &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
602 &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],
603 mvx, mvy, 2*width, 2*height );
605 h->mc.mc_chroma( &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
606 &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],
607 mvx, mvy, 2*width, 2*height );
610 static inline void x264_mb_mc_01xywh( x264_t *h, int x, int y, int width, int height )
612 const int i8 = x264_scan8[0]+x+8*y;
614 const int i_ref1 = h->mb.cache.ref[1][i8];
615 const int mvx1 = x264_clip3( h->mb.cache.mv[1][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] );
616 int mvy1 = x264_clip3( h->mb.cache.mv[1][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] );
617 DECLARE_ALIGNED_16( uint8_t tmp[16*16] );
618 int i_mode = x264_size2pixel[height][width];
620 x264_mb_mc_0xywh( h, x, y, width, height );
622 h->mc.mc_luma( tmp, 16, h->mb.pic.p_fref[1][i_ref1], h->mb.pic.i_stride[0],
623 mvx1 + 4*4*x, mvy1 + 4*4*y, 4*width, 4*height );
625 if( h->mb.b_interlaced & i_ref1 )
626 mvy1 += (h->mb.i_mb_y & 1)*4 - 2;
628 if( h->param.analyse.b_weighted_bipred )
630 const int i_ref0 = h->mb.cache.ref[0][i8];
631 const int weight = h->mb.bipred_weight[i_ref0][i_ref1];
633 h->mc.avg_weight[i_mode]( &h->mb.pic.p_fdec[0][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE, tmp, 16, weight );
635 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],
636 mvx1, mvy1, 2*width, 2*height );
637 h->mc.avg_weight[i_mode+3]( &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE, tmp, 16, weight );
639 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],
640 mvx1, mvy1, 2*width, 2*height );
641 h->mc.avg_weight[i_mode+3]( &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE, tmp, 16, weight );
645 h->mc.avg[i_mode]( &h->mb.pic.p_fdec[0][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE, tmp, 16 );
647 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],
648 mvx1, mvy1, 2*width, 2*height );
649 h->mc.avg[i_mode+3]( &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE, tmp, 16 );
651 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],
652 mvx1, mvy1, 2*width, 2*height );
653 h->mc.avg[i_mode+3]( &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE, tmp, 16 );
657 static void x264_mb_mc_direct8x8( x264_t *h, int x, int y )
659 const int i8 = x264_scan8[0] + x + 8*y;
661 /* FIXME: optimize based on current block size, not global settings? */
662 if( h->sps->b_direct8x8_inference )
664 if( h->mb.cache.ref[0][i8] >= 0 )
665 if( h->mb.cache.ref[1][i8] >= 0 )
666 x264_mb_mc_01xywh( h, x, y, 2, 2 );
668 x264_mb_mc_0xywh( h, x, y, 2, 2 );
670 x264_mb_mc_1xywh( h, x, y, 2, 2 );
674 if( h->mb.cache.ref[0][i8] >= 0 )
676 if( h->mb.cache.ref[1][i8] >= 0 )
678 x264_mb_mc_01xywh( h, x+0, y+0, 1, 1 );
679 x264_mb_mc_01xywh( h, x+1, y+0, 1, 1 );
680 x264_mb_mc_01xywh( h, x+0, y+1, 1, 1 );
681 x264_mb_mc_01xywh( h, x+1, y+1, 1, 1 );
685 x264_mb_mc_0xywh( h, x+0, y+0, 1, 1 );
686 x264_mb_mc_0xywh( h, x+1, y+0, 1, 1 );
687 x264_mb_mc_0xywh( h, x+0, y+1, 1, 1 );
688 x264_mb_mc_0xywh( h, x+1, y+1, 1, 1 );
693 x264_mb_mc_1xywh( h, x+0, y+0, 1, 1 );
694 x264_mb_mc_1xywh( h, x+1, y+0, 1, 1 );
695 x264_mb_mc_1xywh( h, x+0, y+1, 1, 1 );
696 x264_mb_mc_1xywh( h, x+1, y+1, 1, 1 );
701 void x264_mb_mc_8x8( x264_t *h, int i8 )
703 const int x = 2*(i8&1);
704 const int y = 2*(i8>>1);
705 switch( h->mb.i_sub_partition[i8] )
708 x264_mb_mc_0xywh( h, x, y, 2, 2 );
711 x264_mb_mc_0xywh( h, x, y+0, 2, 1 );
712 x264_mb_mc_0xywh( h, x, y+1, 2, 1 );
715 x264_mb_mc_0xywh( h, x+0, y, 1, 2 );
716 x264_mb_mc_0xywh( h, x+1, y, 1, 2 );
719 x264_mb_mc_0xywh( h, x+0, y+0, 1, 1 );
720 x264_mb_mc_0xywh( h, x+1, y+0, 1, 1 );
721 x264_mb_mc_0xywh( h, x+0, y+1, 1, 1 );
722 x264_mb_mc_0xywh( h, x+1, y+1, 1, 1 );
725 x264_mb_mc_1xywh( h, x, y, 2, 2 );
728 x264_mb_mc_1xywh( h, x, y+0, 2, 1 );
729 x264_mb_mc_1xywh( h, x, y+1, 2, 1 );
732 x264_mb_mc_1xywh( h, x+0, y, 1, 2 );
733 x264_mb_mc_1xywh( h, x+1, y, 1, 2 );
736 x264_mb_mc_1xywh( h, x+0, y+0, 1, 1 );
737 x264_mb_mc_1xywh( h, x+1, y+0, 1, 1 );
738 x264_mb_mc_1xywh( h, x+0, y+1, 1, 1 );
739 x264_mb_mc_1xywh( h, x+1, y+1, 1, 1 );
742 x264_mb_mc_01xywh( h, x, y, 2, 2 );
745 x264_mb_mc_01xywh( h, x, y+0, 2, 1 );
746 x264_mb_mc_01xywh( h, x, y+1, 2, 1 );
749 x264_mb_mc_01xywh( h, x+0, y, 1, 2 );
750 x264_mb_mc_01xywh( h, x+1, y, 1, 2 );
753 x264_mb_mc_01xywh( h, x+0, y+0, 1, 1 );
754 x264_mb_mc_01xywh( h, x+1, y+0, 1, 1 );
755 x264_mb_mc_01xywh( h, x+0, y+1, 1, 1 );
756 x264_mb_mc_01xywh( h, x+1, y+1, 1, 1 );
759 x264_mb_mc_direct8x8( h, x, y );
764 void x264_mb_mc( x264_t *h )
766 if( h->mb.i_type == P_L0 )
768 if( h->mb.i_partition == D_16x16 )
770 x264_mb_mc_0xywh( h, 0, 0, 4, 4 );
772 else if( h->mb.i_partition == D_16x8 )
774 x264_mb_mc_0xywh( h, 0, 0, 4, 2 );
775 x264_mb_mc_0xywh( h, 0, 2, 4, 2 );
777 else if( h->mb.i_partition == D_8x16 )
779 x264_mb_mc_0xywh( h, 0, 0, 2, 4 );
780 x264_mb_mc_0xywh( h, 2, 0, 2, 4 );
783 else if( h->mb.i_type == P_8x8 || h->mb.i_type == B_8x8 )
786 for( i = 0; i < 4; i++ )
787 x264_mb_mc_8x8( h, i );
789 else if( h->mb.i_type == B_SKIP || h->mb.i_type == B_DIRECT )
791 x264_mb_mc_direct8x8( h, 0, 0 );
792 x264_mb_mc_direct8x8( h, 2, 0 );
793 x264_mb_mc_direct8x8( h, 0, 2 );
794 x264_mb_mc_direct8x8( h, 2, 2 );
803 /* init ref list utilisations */
804 for( i = 0; i < 2; i++ )
806 b_list0[i] = x264_mb_type_list0_table[h->mb.i_type][i];
807 b_list1[i] = x264_mb_type_list1_table[h->mb.i_type][i];
809 if( h->mb.i_partition == D_16x16 )
811 if( b_list0[0] && b_list1[0] ) x264_mb_mc_01xywh( h, 0, 0, 4, 4 );
812 else if( b_list0[0] ) x264_mb_mc_0xywh ( h, 0, 0, 4, 4 );
813 else if( b_list1[0] ) x264_mb_mc_1xywh ( h, 0, 0, 4, 4 );
815 else if( h->mb.i_partition == D_16x8 )
817 if( b_list0[0] && b_list1[0] ) x264_mb_mc_01xywh( h, 0, 0, 4, 2 );
818 else if( b_list0[0] ) x264_mb_mc_0xywh ( h, 0, 0, 4, 2 );
819 else if( b_list1[0] ) x264_mb_mc_1xywh ( h, 0, 0, 4, 2 );
821 if( b_list0[1] && b_list1[1] ) x264_mb_mc_01xywh( h, 0, 2, 4, 2 );
822 else if( b_list0[1] ) x264_mb_mc_0xywh ( h, 0, 2, 4, 2 );
823 else if( b_list1[1] ) x264_mb_mc_1xywh ( h, 0, 2, 4, 2 );
825 else if( h->mb.i_partition == D_8x16 )
827 if( b_list0[0] && b_list1[0] ) x264_mb_mc_01xywh( h, 0, 0, 2, 4 );
828 else if( b_list0[0] ) x264_mb_mc_0xywh ( h, 0, 0, 2, 4 );
829 else if( b_list1[0] ) x264_mb_mc_1xywh ( h, 0, 0, 2, 4 );
831 if( b_list0[1] && b_list1[1] ) x264_mb_mc_01xywh( h, 2, 0, 2, 4 );
832 else if( b_list0[1] ) x264_mb_mc_0xywh ( h, 2, 0, 2, 4 );
833 else if( b_list1[1] ) x264_mb_mc_1xywh ( h, 2, 0, 2, 4 );
838 int x264_macroblock_cache_init( x264_t *h )
841 int i_mb_count = h->mb.i_mb_count;
843 h->mb.i_mb_stride = h->sps->i_mb_width;
844 h->mb.i_b8_stride = h->sps->i_mb_width * 2;
845 h->mb.i_b4_stride = h->sps->i_mb_width * 4;
847 h->mb.b_interlaced = h->param.b_interlaced;
849 CHECKED_MALLOC( h->mb.qp, i_mb_count * sizeof(int8_t) );
850 CHECKED_MALLOC( h->mb.cbp, i_mb_count * sizeof(int16_t) );
851 CHECKED_MALLOC( h->mb.skipbp, i_mb_count * sizeof(int8_t) );
852 CHECKED_MALLOC( h->mb.mb_transform_size, i_mb_count * sizeof(int8_t) );
854 /* 0 -> 3 top(4), 4 -> 6 : left(3) */
855 CHECKED_MALLOC( h->mb.intra4x4_pred_mode, i_mb_count * 7 * sizeof(int8_t) );
858 CHECKED_MALLOC( h->mb.non_zero_count, i_mb_count * 24 * sizeof(uint8_t) );
859 CHECKED_MALLOC( h->mb.nnz_backup, h->sps->i_mb_width * 4 * 16 * sizeof(uint8_t) );
861 if( h->param.b_cabac )
863 CHECKED_MALLOC( h->mb.chroma_pred_mode, i_mb_count * sizeof(int8_t) );
864 CHECKED_MALLOC( h->mb.mvd[0], 2*16 * i_mb_count * sizeof(int16_t) );
865 CHECKED_MALLOC( h->mb.mvd[1], 2*16 * i_mb_count * sizeof(int16_t) );
870 int i_refs = X264_MIN(16, (i ? 1 : h->param.i_frame_reference) + h->param.b_bframe_pyramid) << h->param.b_interlaced;
871 for( j=0; j < i_refs; j++ )
872 CHECKED_MALLOC( h->mb.mvr[i][j], 2 * i_mb_count * sizeof(int16_t) );
875 for( i=0; i<=h->param.b_interlaced; i++ )
878 CHECKED_MALLOC( h->mb.intra_border_backup[i][j], h->fdec->i_stride[j] );
879 h->mb.intra_border_backup[i][j] += 8;
882 /* init with not available (for top right idx=7,15) */
883 memset( h->mb.cache.ref[0], -2, X264_SCAN8_SIZE * sizeof( int8_t ) );
884 memset( h->mb.cache.ref[1], -2, X264_SCAN8_SIZE * sizeof( int8_t ) );
889 void x264_macroblock_cache_end( x264_t *h )
892 for( i=0; i<=h->param.b_interlaced; i++ )
894 x264_free( h->mb.intra_border_backup[i][j] - 8 );
896 for( j=0; j<32; j++ )
897 x264_free( h->mb.mvr[i][j] );
898 if( h->param.b_cabac )
900 x264_free( h->mb.chroma_pred_mode );
901 x264_free( h->mb.mvd[0] );
902 x264_free( h->mb.mvd[1] );
904 x264_free( h->mb.intra4x4_pred_mode );
905 x264_free( h->mb.non_zero_count );
906 x264_free( h->mb.nnz_backup );
907 x264_free( h->mb.mb_transform_size );
908 x264_free( h->mb.skipbp );
909 x264_free( h->mb.cbp );
910 x264_free( h->mb.qp );
912 void x264_macroblock_slice_init( x264_t *h )
916 h->mb.mv[0] = h->fdec->mv[0];
917 h->mb.mv[1] = h->fdec->mv[1];
918 h->mb.ref[0] = h->fdec->ref[0];
919 h->mb.ref[1] = h->fdec->ref[1];
920 h->mb.type = h->fdec->mb_type;
922 h->fdec->i_ref[0] = h->i_ref0;
923 h->fdec->i_ref[1] = h->i_ref1;
924 for( i = 0; i < h->i_ref0; i++ )
925 h->fdec->ref_poc[0][i] = h->fref0[i]->i_poc;
926 if( h->sh.i_type == SLICE_TYPE_B )
928 for( i = 0; i < h->i_ref1; i++ )
929 h->fdec->ref_poc[1][i] = h->fref1[i]->i_poc;
931 h->mb.map_col_to_list0[-1] = -1;
932 h->mb.map_col_to_list0[-2] = -2;
933 for( i = 0; i < h->fref1[0]->i_ref[0]; i++ )
935 int poc = h->fref1[0]->ref_poc[0][i];
936 h->mb.map_col_to_list0[i] = -2;
937 for( j = 0; j < h->i_ref0; j++ )
938 if( h->fref0[j]->i_poc == poc )
940 h->mb.map_col_to_list0[i] = j;
945 if( h->sh.i_type == SLICE_TYPE_P )
946 memset( h->mb.cache.skip, 0, X264_SCAN8_SIZE * sizeof( int8_t ) );
948 setup_inverse_delta_pocs( h );
951 void x264_prefetch_fenc( x264_t *h, x264_frame_t *fenc, int i_mb_x, int i_mb_y )
953 int stride_y = fenc->i_stride[0];
954 int stride_uv = fenc->i_stride[1];
955 int off_y = 16 * (i_mb_x + i_mb_y * stride_y);
956 int off_uv = 8 * (i_mb_x + i_mb_y * stride_uv);
957 h->mc.prefetch_fenc( fenc->plane[0]+off_y, stride_y,
958 fenc->plane[1+(i_mb_x&1)]+off_uv, stride_uv, i_mb_x );
961 static NOINLINE void copy_column8( uint8_t *dst, uint8_t *src )
965 dst[i*FDEC_STRIDE] = src[i*FDEC_STRIDE];
968 static void ALWAYS_INLINE x264_macroblock_load_pic_pointers( x264_t *h, int i_mb_x, int i_mb_y, int i)
970 const int w = (i == 0 ? 16 : 8);
971 const int i_stride = h->fdec->i_stride[i];
972 const int i_stride2 = i_stride << h->mb.b_interlaced;
973 const int i_pix_offset = h->mb.b_interlaced
974 ? w * (i_mb_x + (i_mb_y&~1) * i_stride) + (i_mb_y&1) * i_stride
975 : w * (i_mb_x + i_mb_y * i_stride);
976 int ref_pix_offset[2] = { i_pix_offset, i_pix_offset };
977 const uint8_t *intra_fdec = &h->mb.intra_border_backup[i_mb_y & h->sh.b_mbaff][i][i_mb_x*16>>!!i];
978 x264_frame_t **fref[2] = { h->fref0, h->fref1 };
980 if( h->mb.b_interlaced )
981 ref_pix_offset[1] += (1-2*(i_mb_y&1)) * i_stride;
982 h->mb.pic.i_stride[i] = i_stride2;
983 h->mc.copy[i?PIXEL_8x8:PIXEL_16x16]( h->mb.pic.p_fenc[i], FENC_STRIDE,
984 &h->fenc->plane[i][i_pix_offset], i_stride2, w );
985 memcpy( &h->mb.pic.p_fdec[i][-1-FDEC_STRIDE], intra_fdec-1, w*3/2+1 );
986 if( h->mb.b_interlaced )
988 const uint8_t *plane_fdec = &h->fdec->plane[i][i_pix_offset];
989 for( j = 0; j < w; j++ )
990 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[l]; j++ )
996 h->mb.pic.p_fref[l][j][i==0 ? 0:i+3] = &fref[l][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[l][j][k] = &fref[l][j >> h->mb.b_interlaced]->filtered[k][ref_pix_offset[j&1]];
1004 void x264_macroblock_cache_load( x264_t *h, int i_mb_x, int i_mb_y )
1006 int i_mb_xy = i_mb_y * h->mb.i_mb_stride + i_mb_x;
1007 int i_mb_4x4 = 4*(i_mb_y * h->mb.i_b4_stride + i_mb_x);
1008 int i_mb_8x8 = 2*(i_mb_y * h->mb.i_b8_stride + i_mb_x);
1009 int i_top_y = i_mb_y - (1 << h->mb.b_interlaced);
1010 int i_top_xy = i_top_y * h->mb.i_mb_stride + i_mb_x;
1011 int i_top_4x4 = (4*i_top_y+3) * h->mb.i_b4_stride + 4*i_mb_x;
1012 int i_top_8x8 = (2*i_top_y+1) * h->mb.i_b8_stride + 2*i_mb_x;
1014 int i_top_type = -1; /* gcc warn */
1015 int i_left_type= -1;
1019 assert( h->mb.i_b8_stride == 2*h->mb.i_mb_stride );
1020 assert( h->mb.i_b4_stride == 4*h->mb.i_mb_stride );
1023 h->mb.i_mb_x = i_mb_x;
1024 h->mb.i_mb_y = i_mb_y;
1025 h->mb.i_mb_xy = i_mb_xy;
1026 h->mb.i_b8_xy = i_mb_8x8;
1027 h->mb.i_b4_xy = i_mb_4x4;
1028 h->mb.i_mb_top_xy = i_top_xy;
1029 h->mb.i_neighbour = 0;
1032 if( i_top_xy >= h->sh.i_first_mb )
1034 h->mb.i_mb_type_top =
1035 i_top_type= h->mb.type[i_top_xy];
1037 h->mb.i_neighbour |= MB_TOP;
1040 h->mb.cache.intra4x4_pred_mode[x264_scan8[0] - 8] = h->mb.intra4x4_pred_mode[i_top_xy][0];
1041 h->mb.cache.intra4x4_pred_mode[x264_scan8[1] - 8] = h->mb.intra4x4_pred_mode[i_top_xy][1];
1042 h->mb.cache.intra4x4_pred_mode[x264_scan8[4] - 8] = h->mb.intra4x4_pred_mode[i_top_xy][2];
1043 h->mb.cache.intra4x4_pred_mode[x264_scan8[5] - 8] = h->mb.intra4x4_pred_mode[i_top_xy][3];
1045 /* load non_zero_count */
1046 h->mb.cache.non_zero_count[x264_scan8[0] - 8] = h->mb.non_zero_count[i_top_xy][10];
1047 h->mb.cache.non_zero_count[x264_scan8[1] - 8] = h->mb.non_zero_count[i_top_xy][11];
1048 h->mb.cache.non_zero_count[x264_scan8[4] - 8] = h->mb.non_zero_count[i_top_xy][14];
1049 h->mb.cache.non_zero_count[x264_scan8[5] - 8] = h->mb.non_zero_count[i_top_xy][15];
1051 h->mb.cache.non_zero_count[x264_scan8[16+0] - 8] = h->mb.non_zero_count[i_top_xy][16+2];
1052 h->mb.cache.non_zero_count[x264_scan8[16+1] - 8] = h->mb.non_zero_count[i_top_xy][16+3];
1054 h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 8] = h->mb.non_zero_count[i_top_xy][16+4+2];
1055 h->mb.cache.non_zero_count[x264_scan8[16+4+1] - 8] = h->mb.non_zero_count[i_top_xy][16+4+3];
1059 h->mb.i_mb_type_top = -1;
1062 h->mb.cache.intra4x4_pred_mode[x264_scan8[0] - 8] =
1063 h->mb.cache.intra4x4_pred_mode[x264_scan8[1] - 8] =
1064 h->mb.cache.intra4x4_pred_mode[x264_scan8[4] - 8] =
1065 h->mb.cache.intra4x4_pred_mode[x264_scan8[5] - 8] = -1;
1067 /* load non_zero_count */
1068 h->mb.cache.non_zero_count[x264_scan8[0] - 8] =
1069 h->mb.cache.non_zero_count[x264_scan8[1] - 8] =
1070 h->mb.cache.non_zero_count[x264_scan8[4] - 8] =
1071 h->mb.cache.non_zero_count[x264_scan8[5] - 8] =
1072 h->mb.cache.non_zero_count[x264_scan8[16+0] - 8] =
1073 h->mb.cache.non_zero_count[x264_scan8[16+1] - 8] =
1074 h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 8] =
1075 h->mb.cache.non_zero_count[x264_scan8[16+4+1] - 8] = 0x80;
1079 if( i_mb_x > 0 && i_mb_xy > h->sh.i_first_mb )
1081 i_left_xy = i_mb_xy - 1;
1082 h->mb.i_mb_type_left =
1083 i_left_type = h->mb.type[i_left_xy];
1085 h->mb.i_neighbour |= MB_LEFT;
1088 h->mb.cache.intra4x4_pred_mode[x264_scan8[0 ] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][4];
1089 h->mb.cache.intra4x4_pred_mode[x264_scan8[2 ] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][5];
1090 h->mb.cache.intra4x4_pred_mode[x264_scan8[8 ] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][6];
1091 h->mb.cache.intra4x4_pred_mode[x264_scan8[10] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][3];
1093 /* load non_zero_count */
1094 h->mb.cache.non_zero_count[x264_scan8[0 ] - 1] = h->mb.non_zero_count[i_left_xy][5];
1095 h->mb.cache.non_zero_count[x264_scan8[2 ] - 1] = h->mb.non_zero_count[i_left_xy][7];
1096 h->mb.cache.non_zero_count[x264_scan8[8 ] - 1] = h->mb.non_zero_count[i_left_xy][13];
1097 h->mb.cache.non_zero_count[x264_scan8[10] - 1] = h->mb.non_zero_count[i_left_xy][15];
1099 h->mb.cache.non_zero_count[x264_scan8[16+0] - 1] = h->mb.non_zero_count[i_left_xy][16+1];
1100 h->mb.cache.non_zero_count[x264_scan8[16+2] - 1] = h->mb.non_zero_count[i_left_xy][16+3];
1102 h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 1] = h->mb.non_zero_count[i_left_xy][16+4+1];
1103 h->mb.cache.non_zero_count[x264_scan8[16+4+2] - 1] = h->mb.non_zero_count[i_left_xy][16+4+3];
1107 h->mb.i_mb_type_left = -1;
1109 h->mb.cache.intra4x4_pred_mode[x264_scan8[0 ] - 1] =
1110 h->mb.cache.intra4x4_pred_mode[x264_scan8[2 ] - 1] =
1111 h->mb.cache.intra4x4_pred_mode[x264_scan8[8 ] - 1] =
1112 h->mb.cache.intra4x4_pred_mode[x264_scan8[10] - 1] = -1;
1114 /* load non_zero_count */
1115 h->mb.cache.non_zero_count[x264_scan8[0 ] - 1] =
1116 h->mb.cache.non_zero_count[x264_scan8[2 ] - 1] =
1117 h->mb.cache.non_zero_count[x264_scan8[8 ] - 1] =
1118 h->mb.cache.non_zero_count[x264_scan8[10] - 1] =
1119 h->mb.cache.non_zero_count[x264_scan8[16+0] - 1] =
1120 h->mb.cache.non_zero_count[x264_scan8[16+2] - 1] =
1121 h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 1] =
1122 h->mb.cache.non_zero_count[x264_scan8[16+4+2] - 1] = 0x80;
1125 if( i_mb_x < h->sps->i_mb_width - 1 && i_top_xy + 1 >= h->sh.i_first_mb )
1127 h->mb.i_neighbour |= MB_TOPRIGHT;
1128 h->mb.i_mb_type_topright = h->mb.type[ i_top_xy + 1 ];
1131 h->mb.i_mb_type_topright = -1;
1132 if( i_mb_x > 0 && i_top_xy - 1 >= h->sh.i_first_mb )
1134 h->mb.i_neighbour |= MB_TOPLEFT;
1135 h->mb.i_mb_type_topleft = h->mb.type[ i_top_xy - 1 ];
1138 h->mb.i_mb_type_topleft = -1;
1140 if( h->pps->b_transform_8x8_mode )
1142 h->mb.cache.i_neighbour_transform_size =
1143 ( i_left_type >= 0 && h->mb.mb_transform_size[i_left_xy] )
1144 + ( i_top_type >= 0 && h->mb.mb_transform_size[i_top_xy] );
1149 h->mb.pic.i_fref[0] = h->i_ref0 << h->mb.b_interlaced;
1150 h->mb.pic.i_fref[1] = h->i_ref1 << h->mb.b_interlaced;
1151 h->mb.cache.i_neighbour_interlaced =
1152 !!(h->mb.i_neighbour & MB_LEFT)
1153 + !!(h->mb.i_neighbour & MB_TOP);
1166 h->mb.pic.p_fenc[0] = h->mb.pic.fenc_buf;
1167 h->mb.pic.p_fenc[1] = h->mb.pic.fenc_buf + 16*FENC_STRIDE;
1168 h->mb.pic.p_fenc[2] = h->mb.pic.fenc_buf + 16*FENC_STRIDE + 8;
1169 h->mb.pic.p_fdec[0] = h->mb.pic.fdec_buf + 2*FDEC_STRIDE;
1170 h->mb.pic.p_fdec[1] = h->mb.pic.fdec_buf + 19*FDEC_STRIDE;
1171 h->mb.pic.p_fdec[2] = h->mb.pic.fdec_buf + 19*FDEC_STRIDE + 16;
1173 if( !h->mb.b_interlaced )
1175 copy_column8( h->mb.pic.p_fdec[0]-1, h->mb.pic.p_fdec[0]+15 );
1176 copy_column8( h->mb.pic.p_fdec[0]-1+8*FDEC_STRIDE, h->mb.pic.p_fdec[0]+15+8*FDEC_STRIDE );
1177 copy_column8( h->mb.pic.p_fdec[1]-1, h->mb.pic.p_fdec[1]+7 );
1178 copy_column8( h->mb.pic.p_fdec[2]-1, h->mb.pic.p_fdec[2]+7 );
1181 /* load picture pointers */
1182 x264_macroblock_load_pic_pointers( h, i_mb_x, i_mb_y, 0 );
1183 x264_macroblock_load_pic_pointers( h, i_mb_x, i_mb_y, 1 );
1184 x264_macroblock_load_pic_pointers( h, i_mb_x, i_mb_y, 2 );
1186 if( h->fdec->integral )
1188 assert( !h->mb.b_interlaced );
1189 for( i = 0; i < h->mb.pic.i_fref[0]; i++ )
1190 h->mb.pic.p_integral[0][i] = &h->fref0[i]->integral[ 16 * ( i_mb_x + i_mb_y * h->fdec->i_stride[0] )];
1191 for( i = 0; i < h->mb.pic.i_fref[1]; i++ )
1192 h->mb.pic.p_integral[1][i] = &h->fref1[i]->integral[ 16 * ( i_mb_x + i_mb_y * h->fdec->i_stride[0] )];
1195 x264_prefetch_fenc( h, h->fenc, i_mb_x, i_mb_y );
1197 /* load ref/mv/mvd */
1198 if( h->sh.i_type != SLICE_TYPE_I )
1200 const int s8x8 = h->mb.i_b8_stride;
1201 const int s4x4 = h->mb.i_b4_stride;
1205 for( i_list = 0; i_list < (h->sh.i_type == SLICE_TYPE_B ? 2 : 1 ); i_list++ )
1208 h->mb.cache.ref[i_list][x264_scan8[5 ]+1] =
1209 h->mb.cache.ref[i_list][x264_scan8[7 ]+1] =
1210 h->mb.cache.ref[i_list][x264_scan8[13]+1] = -2;
1213 if( h->mb.i_neighbour & MB_TOPLEFT )
1215 const int i8 = x264_scan8[0] - 1 - 1*8;
1216 const int ir = i_top_8x8 - 1;
1217 const int iv = i_top_4x4 - 1;
1218 h->mb.cache.ref[i_list][i8] = h->mb.ref[i_list][ir];
1219 *(uint32_t*)h->mb.cache.mv[i_list][i8] = *(uint32_t*)h->mb.mv[i_list][iv];
1223 const int i8 = x264_scan8[0] - 1 - 1*8;
1224 h->mb.cache.ref[i_list][i8] = -2;
1225 *(uint32_t*)h->mb.cache.mv[i_list][i8] = 0;
1228 if( h->mb.i_neighbour & MB_TOP )
1230 const int i8 = x264_scan8[0] - 8;
1231 const int ir = i_top_8x8;
1232 const int iv = i_top_4x4;
1233 h->mb.cache.ref[i_list][i8+0] =
1234 h->mb.cache.ref[i_list][i8+1] = h->mb.ref[i_list][ir + 0];
1235 h->mb.cache.ref[i_list][i8+2] =
1236 h->mb.cache.ref[i_list][i8+3] = h->mb.ref[i_list][ir + 1];
1237 *(uint64_t*)h->mb.cache.mv[i_list][i8+0] = *(uint64_t*)h->mb.mv[i_list][iv+0];
1238 *(uint64_t*)h->mb.cache.mv[i_list][i8+2] = *(uint64_t*)h->mb.mv[i_list][iv+2];
1242 const int i8 = x264_scan8[0] - 8;
1243 *(uint64_t*)h->mb.cache.mv[i_list][i8+0] = 0;
1244 *(uint64_t*)h->mb.cache.mv[i_list][i8+2] = 0;
1245 *(uint32_t*)&h->mb.cache.ref[i_list][i8] = (uint8_t)(-2) * 0x01010101U;
1248 if( h->mb.i_neighbour & MB_TOPRIGHT )
1250 const int i8 = x264_scan8[0] + 4 - 1*8;
1251 const int ir = i_top_8x8 + 2;
1252 const int iv = i_top_4x4 + 4;
1253 h->mb.cache.ref[i_list][i8] = h->mb.ref[i_list][ir];
1254 *(uint32_t*)h->mb.cache.mv[i_list][i8] = *(uint32_t*)h->mb.mv[i_list][iv];
1258 const int i8 = x264_scan8[0] + 4 - 1*8;
1259 h->mb.cache.ref[i_list][i8] = -2;
1260 *(uint32_t*)h->mb.cache.mv[i_list][i8] = 0;
1263 if( h->mb.i_neighbour & MB_LEFT )
1265 const int i8 = x264_scan8[0] - 1;
1266 const int ir = i_mb_8x8 - 1;
1267 const int iv = i_mb_4x4 - 1;
1268 h->mb.cache.ref[i_list][i8+0*8] =
1269 h->mb.cache.ref[i_list][i8+1*8] = h->mb.ref[i_list][ir + 0*s8x8];
1270 h->mb.cache.ref[i_list][i8+2*8] =
1271 h->mb.cache.ref[i_list][i8+3*8] = h->mb.ref[i_list][ir + 1*s8x8];
1273 for( i = 0; i < 4; i++ )
1274 *(uint32_t*)h->mb.cache.mv[i_list][i8+i*8] = *(uint32_t*)h->mb.mv[i_list][iv + i*s4x4];
1278 const int i8 = x264_scan8[0] - 1;
1279 for( i = 0; i < 4; i++ )
1281 h->mb.cache.ref[i_list][i8+i*8] = -2;
1282 *(uint32_t*)h->mb.cache.mv[i_list][i8+i*8] = 0;
1286 if( h->param.b_cabac )
1288 if( i_top_type >= 0 )
1290 const int i8 = x264_scan8[0] - 8;
1291 const int iv = i_top_4x4;
1292 *(uint64_t*)h->mb.cache.mvd[i_list][i8+0] = *(uint64_t*)h->mb.mvd[i_list][iv+0];
1293 *(uint64_t*)h->mb.cache.mvd[i_list][i8+2] = *(uint64_t*)h->mb.mvd[i_list][iv+2];
1297 const int i8 = x264_scan8[0] - 8;
1298 *(uint64_t*)h->mb.cache.mvd[i_list][i8+0] =
1299 *(uint64_t*)h->mb.cache.mvd[i_list][i8+2] = 0;
1302 if( i_left_type >= 0 )
1304 const int i8 = x264_scan8[0] - 1;
1305 const int iv = i_mb_4x4 - 1;
1306 for( i = 0; i < 4; i++ )
1307 *(uint32_t*)h->mb.cache.mvd[i_list][i8+i*8] = *(uint32_t*)h->mb.mvd[i_list][iv + i*s4x4];
1311 const int i8 = x264_scan8[0] - 1;
1312 for( i = 0; i < 4; i++ )
1313 *(uint32_t*)h->mb.cache.mvd[i_list][i8+i*8] = 0;
1319 if( h->sh.i_type == SLICE_TYPE_B && h->param.b_cabac )
1321 memset( h->mb.cache.skip, 0, X264_SCAN8_SIZE * sizeof( int8_t ) );
1322 if( i_left_type >= 0 )
1324 h->mb.cache.skip[x264_scan8[0] - 1] = h->mb.skipbp[i_left_xy] & 0x2;
1325 h->mb.cache.skip[x264_scan8[8] - 1] = h->mb.skipbp[i_left_xy] & 0x8;
1327 if( i_top_type >= 0 )
1329 h->mb.cache.skip[x264_scan8[0] - 8] = h->mb.skipbp[i_top_xy] & 0x4;
1330 h->mb.cache.skip[x264_scan8[4] - 8] = h->mb.skipbp[i_top_xy] & 0x8;
1334 if( h->sh.i_type == SLICE_TYPE_P )
1335 x264_mb_predict_mv_pskip( h, h->mb.cache.pskip_mv );
1338 h->mb.i_neighbour4[0] =
1339 h->mb.i_neighbour8[0] = (h->mb.i_neighbour & (MB_TOP|MB_LEFT|MB_TOPLEFT))
1340 | ((h->mb.i_neighbour & MB_TOP) ? MB_TOPRIGHT : 0);
1341 h->mb.i_neighbour4[4] =
1342 h->mb.i_neighbour4[1] = MB_LEFT | ((h->mb.i_neighbour & MB_TOP) ? (MB_TOP|MB_TOPLEFT|MB_TOPRIGHT) : 0);
1343 h->mb.i_neighbour4[2] =
1344 h->mb.i_neighbour4[8] =
1345 h->mb.i_neighbour4[10] =
1346 h->mb.i_neighbour8[2] = MB_TOP|MB_TOPRIGHT | ((h->mb.i_neighbour & MB_LEFT) ? (MB_LEFT|MB_TOPLEFT) : 0);
1347 h->mb.i_neighbour4[3] =
1348 h->mb.i_neighbour4[7] =
1349 h->mb.i_neighbour4[11] =
1350 h->mb.i_neighbour4[13] =
1351 h->mb.i_neighbour4[15] =
1352 h->mb.i_neighbour8[3] = MB_LEFT|MB_TOP|MB_TOPLEFT;
1353 h->mb.i_neighbour4[5] =
1354 h->mb.i_neighbour8[1] = MB_LEFT | (h->mb.i_neighbour & MB_TOPRIGHT)
1355 | ((h->mb.i_neighbour & MB_TOP) ? MB_TOP|MB_TOPLEFT : 0);
1356 h->mb.i_neighbour4[6] =
1357 h->mb.i_neighbour4[9] =
1358 h->mb.i_neighbour4[12] =
1359 h->mb.i_neighbour4[14] = MB_LEFT|MB_TOP|MB_TOPLEFT|MB_TOPRIGHT;
1362 void x264_macroblock_cache_save( x264_t *h )
1364 const int i_mb_xy = h->mb.i_mb_xy;
1365 const int i_mb_type = x264_mb_type_fix[h->mb.i_type];
1366 const int s8x8 = h->mb.i_b8_stride;
1367 const int s4x4 = h->mb.i_b4_stride;
1368 const int i_mb_4x4 = h->mb.i_b4_xy;
1369 const int i_mb_8x8 = h->mb.i_b8_xy;
1373 for( i = 0; i < 3; i++ )
1376 int i_stride = h->fdec->i_stride[i];
1377 int i_stride2 = i_stride << h->mb.b_interlaced;
1378 int i_pix_offset = h->mb.b_interlaced
1379 ? w * (h->mb.i_mb_x + (h->mb.i_mb_y&~1) * i_stride) + (h->mb.i_mb_y&1) * i_stride
1380 : w * (h->mb.i_mb_x + h->mb.i_mb_y * i_stride);
1381 h->mc.copy[i?PIXEL_8x8:PIXEL_16x16](
1382 &h->fdec->plane[i][i_pix_offset], i_stride2,
1383 h->mb.pic.p_fdec[i], FDEC_STRIDE, w );
1386 x264_prefetch_fenc( h, h->fdec, h->mb.i_mb_x, h->mb.i_mb_y );
1388 h->mb.type[i_mb_xy] = i_mb_type;
1390 if( h->mb.i_type != I_16x16 && h->mb.i_cbp_luma == 0 && h->mb.i_cbp_chroma == 0 )
1391 h->mb.i_qp = h->mb.i_last_qp;
1392 h->mb.qp[i_mb_xy] = h->mb.i_qp;
1394 h->mb.i_last_dqp = h->mb.i_qp - h->mb.i_last_qp;
1395 h->mb.i_last_qp = h->mb.i_qp;
1396 h->mb.i_mb_prev_xy = h->mb.i_mb_xy;
1399 if( i_mb_type == I_4x4 )
1401 h->mb.intra4x4_pred_mode[i_mb_xy][0] = h->mb.cache.intra4x4_pred_mode[x264_scan8[10] ];
1402 h->mb.intra4x4_pred_mode[i_mb_xy][1] = h->mb.cache.intra4x4_pred_mode[x264_scan8[11] ];
1403 h->mb.intra4x4_pred_mode[i_mb_xy][2] = h->mb.cache.intra4x4_pred_mode[x264_scan8[14] ];
1404 h->mb.intra4x4_pred_mode[i_mb_xy][3] = h->mb.cache.intra4x4_pred_mode[x264_scan8[15] ];
1405 h->mb.intra4x4_pred_mode[i_mb_xy][4] = h->mb.cache.intra4x4_pred_mode[x264_scan8[5] ];
1406 h->mb.intra4x4_pred_mode[i_mb_xy][5] = h->mb.cache.intra4x4_pred_mode[x264_scan8[7] ];
1407 h->mb.intra4x4_pred_mode[i_mb_xy][6] = h->mb.cache.intra4x4_pred_mode[x264_scan8[13] ];
1411 h->mb.intra4x4_pred_mode[i_mb_xy][0] =
1412 h->mb.intra4x4_pred_mode[i_mb_xy][1] =
1413 h->mb.intra4x4_pred_mode[i_mb_xy][2] =
1414 h->mb.intra4x4_pred_mode[i_mb_xy][3] =
1415 h->mb.intra4x4_pred_mode[i_mb_xy][4] =
1416 h->mb.intra4x4_pred_mode[i_mb_xy][5] =
1417 h->mb.intra4x4_pred_mode[i_mb_xy][6] = I_PRED_4x4_DC;
1420 if( i_mb_type == I_PCM )
1422 h->mb.cbp[i_mb_xy] = 0x72f; /* all set */
1423 for( i = 0; i < 16 + 2*4; i++ )
1425 h->mb.non_zero_count[i_mb_xy][i] = 16;
1430 /* save non zero count */
1431 for( i = 0; i < 16 + 2*4; i++ )
1433 h->mb.non_zero_count[i_mb_xy][i] = h->mb.cache.non_zero_count[x264_scan8[i]];
1437 if( h->mb.i_cbp_luma == 0 && h->mb.i_type != I_8x8 )
1438 h->mb.b_transform_8x8 = 0;
1439 h->mb.mb_transform_size[i_mb_xy] = h->mb.b_transform_8x8;
1441 if( !IS_INTRA( i_mb_type ) )
1444 for( i_list = 0; i_list < (h->sh.i_type == SLICE_TYPE_B ? 2 : 1 ); i_list++ )
1448 h->mb.ref[i_list][i_mb_8x8+0+0*s8x8] = h->mb.cache.ref[i_list][x264_scan8[0]];
1449 h->mb.ref[i_list][i_mb_8x8+1+0*s8x8] = h->mb.cache.ref[i_list][x264_scan8[4]];
1450 h->mb.ref[i_list][i_mb_8x8+0+1*s8x8] = h->mb.cache.ref[i_list][x264_scan8[8]];
1451 h->mb.ref[i_list][i_mb_8x8+1+1*s8x8] = h->mb.cache.ref[i_list][x264_scan8[12]];
1453 for( y = 0; y < 4; y++ )
1455 *(uint64_t*)h->mb.mv[i_list][i_mb_4x4+y*s4x4+0] = *(uint64_t*)h->mb.cache.mv[i_list][x264_scan8[0]+8*y+0];
1456 *(uint64_t*)h->mb.mv[i_list][i_mb_4x4+y*s4x4+2] = *(uint64_t*)h->mb.cache.mv[i_list][x264_scan8[0]+8*y+2];
1463 for( i_list = 0; i_list < (h->sh.i_type == SLICE_TYPE_B ? 2 : 1 ); i_list++ )
1467 *(uint16_t*)&h->mb.ref[i_list][i_mb_8x8+0*s8x8] = (uint8_t)(-1) * 0x0101;
1468 *(uint16_t*)&h->mb.ref[i_list][i_mb_8x8+1*s8x8] = (uint8_t)(-1) * 0x0101;
1470 for( y = 0; y < 4; y++ )
1472 *(uint64_t*)h->mb.mv[i_list][i_mb_4x4+y*s4x4+0] = 0;
1473 *(uint64_t*)h->mb.mv[i_list][i_mb_4x4+y*s4x4+2] = 0;
1478 if( h->param.b_cabac )
1480 if( i_mb_type == I_4x4 || i_mb_type == I_16x16 )
1481 h->mb.chroma_pred_mode[i_mb_xy] = x264_mb_pred_mode8x8c_fix[ h->mb.i_chroma_pred_mode ];
1483 h->mb.chroma_pred_mode[i_mb_xy] = I_PRED_CHROMA_DC;
1485 if( !IS_INTRA( i_mb_type ) && !IS_SKIP( i_mb_type ) && !IS_DIRECT( i_mb_type ) )
1488 for( i_list = 0; i_list < 2; i_list++ )
1490 const int s4x4 = 4 * h->mb.i_mb_stride;
1492 for( y = 0; y < 4; y++ )
1494 *(uint64_t*)h->mb.mvd[i_list][i_mb_4x4+y*s4x4+0] = *(uint64_t*)h->mb.cache.mvd[i_list][x264_scan8[0]+8*y+0];
1495 *(uint64_t*)h->mb.mvd[i_list][i_mb_4x4+y*s4x4+2] = *(uint64_t*)h->mb.cache.mvd[i_list][x264_scan8[0]+8*y+2];
1502 for( i_list = 0; i_list < 2; i_list++ )
1504 const int s4x4 = 4 * h->mb.i_mb_stride;
1506 for( y = 0; y < 4; y++ )
1508 *(uint64_t*)h->mb.mvd[i_list][i_mb_4x4+y*s4x4+0] = 0;
1509 *(uint64_t*)h->mb.mvd[i_list][i_mb_4x4+y*s4x4+2] = 0;
1513 if( h->sh.i_type == SLICE_TYPE_B )
1515 if( i_mb_type == B_SKIP || i_mb_type == B_DIRECT )
1516 h->mb.skipbp[i_mb_xy] = 0xf;
1517 else if( i_mb_type == B_8x8 )
1520 for( i = 0; i < 4; i++ )
1521 skipbp |= ( h->mb.i_sub_partition[i] == D_DIRECT_8x8 ) << i;
1522 h->mb.skipbp[i_mb_xy] = skipbp;
1525 h->mb.skipbp[i_mb_xy] = 0;
1530 void x264_macroblock_bipred_init( x264_t *h )
1533 for( i_ref0 = 0; i_ref0 < h->i_ref0; i_ref0++ )
1535 int poc0 = h->fref0[i_ref0]->i_poc;
1536 for( i_ref1 = 0; i_ref1 < h->i_ref1; i_ref1++ )
1538 int dist_scale_factor;
1539 int poc1 = h->fref1[i_ref1]->i_poc;
1540 int td = x264_clip3( poc1 - poc0, -128, 127 );
1541 if( td == 0 /* || pic0 is a long-term ref */ )
1542 dist_scale_factor = 256;
1545 int tb = x264_clip3( h->fdec->i_poc - poc0, -128, 127 );
1546 int tx = (16384 + (abs(td) >> 1)) / td;
1547 dist_scale_factor = x264_clip3( (tb * tx + 32) >> 6, -1024, 1023 );
1549 h->mb.dist_scale_factor[i_ref0][i_ref1] = dist_scale_factor;
1551 dist_scale_factor >>= 2;
1552 if( h->param.analyse.b_weighted_bipred
1553 && dist_scale_factor >= -64
1554 && dist_scale_factor <= 128 )
1555 h->mb.bipred_weight[i_ref0][i_ref1] = 64 - dist_scale_factor;
1557 h->mb.bipred_weight[i_ref0][i_ref1] = 32;
1562 for( i_ref0 = 2*h->i_ref0-1; i_ref0 >= 0; i_ref0-- )
1563 for( i_ref1 = 2*h->i_ref1-1; i_ref1 >= 0; i_ref1-- )
1564 h->mb.bipred_weight[i_ref0][i_ref1] = h->mb.bipred_weight[i_ref0>>1][i_ref1>>1];