1 /*****************************************************************************
2 * macroblock.c: h264 encoder library
3 *****************************************************************************
4 * Copyright (C) 2003-2008 x264 project
6 * Authors: Fiona Glaser <fiona@x264.com>
7 * Laurent Aimar <fenrir@via.ecp.fr>
8 * Loren Merritt <lorenm@u.washington.edu>
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 static NOINLINE void x264_mb_mc_0xywh( x264_t *h, int x, int y, int width, int height )
30 int i8 = x264_scan8[0]+x+8*y;
31 int i_ref = h->mb.cache.ref[0][i8];
32 int mvx = x264_clip3( h->mb.cache.mv[0][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] ) + 4*4*x;
33 int mvy = x264_clip3( h->mb.cache.mv[0][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] ) + 4*4*y;
35 h->mc.mc_luma( &h->mb.pic.p_fdec[0][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE,
36 h->mb.pic.p_fref[0][i_ref], h->mb.pic.i_stride[0],
37 mvx, mvy, 4*width, 4*height, &h->sh.weight[i_ref][0] );
39 // chroma is offset if MCing from a field of opposite parity
40 if( h->mb.b_interlaced & i_ref )
41 mvy += (h->mb.i_mb_y & 1)*4 - 2;
43 h->mc.mc_chroma( &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
44 h->mb.pic.p_fref[0][i_ref][4], h->mb.pic.i_stride[1],
45 mvx, mvy, 2*width, 2*height );
47 if( h->sh.weight[i_ref][1].weightfn )
48 h->sh.weight[i_ref][1].weightfn[width>>1]( &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
49 &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
50 &h->sh.weight[i_ref][1], height*2 );
52 h->mc.mc_chroma( &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
53 h->mb.pic.p_fref[0][i_ref][5], h->mb.pic.i_stride[2],
54 mvx, mvy, 2*width, 2*height );
56 if( h->sh.weight[i_ref][2].weightfn )
57 h->sh.weight[i_ref][2].weightfn[width>>1]( &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
58 &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
59 &h->sh.weight[i_ref][2],height*2 );
62 static NOINLINE void x264_mb_mc_1xywh( x264_t *h, int x, int y, int width, int height )
64 int i8 = x264_scan8[0]+x+8*y;
65 int i_ref = h->mb.cache.ref[1][i8];
66 int mvx = x264_clip3( h->mb.cache.mv[1][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] ) + 4*4*x;
67 int mvy = x264_clip3( h->mb.cache.mv[1][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] ) + 4*4*y;
69 h->mc.mc_luma( &h->mb.pic.p_fdec[0][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE,
70 h->mb.pic.p_fref[1][i_ref], h->mb.pic.i_stride[0],
71 mvx, mvy, 4*width, 4*height, weight_none );
73 if( h->mb.b_interlaced & i_ref )
74 mvy += (h->mb.i_mb_y & 1)*4 - 2;
76 h->mc.mc_chroma( &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
77 h->mb.pic.p_fref[1][i_ref][4], h->mb.pic.i_stride[1],
78 mvx, mvy, 2*width, 2*height );
80 h->mc.mc_chroma( &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
81 h->mb.pic.p_fref[1][i_ref][5], h->mb.pic.i_stride[2],
82 mvx, mvy, 2*width, 2*height );
85 static NOINLINE void x264_mb_mc_01xywh( x264_t *h, int x, int y, int width, int height )
87 int i8 = x264_scan8[0]+x+8*y;
88 int i_ref0 = h->mb.cache.ref[0][i8];
89 int i_ref1 = h->mb.cache.ref[1][i8];
90 int weight = h->mb.bipred_weight[i_ref0][i_ref1];
91 int mvx0 = x264_clip3( h->mb.cache.mv[0][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] ) + 4*4*x;
92 int mvx1 = x264_clip3( h->mb.cache.mv[1][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] ) + 4*4*x;
93 int mvy0 = x264_clip3( h->mb.cache.mv[0][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] ) + 4*4*y;
94 int mvy1 = x264_clip3( h->mb.cache.mv[1][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] ) + 4*4*y;
95 int i_mode = x264_size2pixel[height][width];
96 int i_stride0 = 16, i_stride1 = 16;
97 ALIGNED_ARRAY_16( uint8_t, tmp0,[16*16] );
98 ALIGNED_ARRAY_16( uint8_t, tmp1,[16*16] );
101 src0 = h->mc.get_ref( tmp0, &i_stride0, h->mb.pic.p_fref[0][i_ref0], h->mb.pic.i_stride[0],
102 mvx0, mvy0, 4*width, 4*height, weight_none );
103 src1 = h->mc.get_ref( tmp1, &i_stride1, h->mb.pic.p_fref[1][i_ref1], h->mb.pic.i_stride[0],
104 mvx1, mvy1, 4*width, 4*height, weight_none );
105 h->mc.avg[i_mode]( &h->mb.pic.p_fdec[0][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE,
106 src0, i_stride0, src1, i_stride1, weight );
108 if( h->mb.b_interlaced & i_ref0 )
109 mvy0 += (h->mb.i_mb_y & 1)*4 - 2;
110 if( h->mb.b_interlaced & i_ref1 )
111 mvy1 += (h->mb.i_mb_y & 1)*4 - 2;
113 h->mc.mc_chroma( tmp0, 16, h->mb.pic.p_fref[0][i_ref0][4], h->mb.pic.i_stride[1],
114 mvx0, mvy0, 2*width, 2*height );
115 h->mc.mc_chroma( tmp1, 16, h->mb.pic.p_fref[1][i_ref1][4], h->mb.pic.i_stride[1],
116 mvx1, mvy1, 2*width, 2*height );
117 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 );
118 h->mc.mc_chroma( tmp0, 16, h->mb.pic.p_fref[0][i_ref0][5], h->mb.pic.i_stride[2],
119 mvx0, mvy0, 2*width, 2*height );
120 h->mc.mc_chroma( tmp1, 16, h->mb.pic.p_fref[1][i_ref1][5], h->mb.pic.i_stride[2],
121 mvx1, mvy1, 2*width, 2*height );
122 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 );
125 void x264_mb_mc_8x8( x264_t *h, int i8 )
130 if( h->sh.i_type == SLICE_TYPE_P )
132 switch( h->mb.i_sub_partition[i8] )
135 x264_mb_mc_0xywh( h, x, y, 2, 2 );
138 x264_mb_mc_0xywh( h, x, y+0, 2, 1 );
139 x264_mb_mc_0xywh( h, x, y+1, 2, 1 );
142 x264_mb_mc_0xywh( h, x+0, y, 1, 2 );
143 x264_mb_mc_0xywh( h, x+1, y, 1, 2 );
146 x264_mb_mc_0xywh( h, x+0, y+0, 1, 1 );
147 x264_mb_mc_0xywh( h, x+1, y+0, 1, 1 );
148 x264_mb_mc_0xywh( h, x+0, y+1, 1, 1 );
149 x264_mb_mc_0xywh( h, x+1, y+1, 1, 1 );
155 int scan8 = x264_scan8[0] + x + 8*y;
157 if( h->mb.cache.ref[0][scan8] >= 0 )
158 if( h->mb.cache.ref[1][scan8] >= 0 )
159 x264_mb_mc_01xywh( h, x, y, 2, 2 );
161 x264_mb_mc_0xywh( h, x, y, 2, 2 );
163 x264_mb_mc_1xywh( h, x, y, 2, 2 );
167 void x264_mb_mc( x264_t *h )
169 if( h->mb.i_partition == D_8x8 )
171 for( int i = 0; i < 4; i++ )
172 x264_mb_mc_8x8( h, i );
176 int ref0a = h->mb.cache.ref[0][x264_scan8[ 0]];
177 int ref0b = h->mb.cache.ref[0][x264_scan8[12]];
178 int ref1a = h->mb.cache.ref[1][x264_scan8[ 0]];
179 int ref1b = h->mb.cache.ref[1][x264_scan8[12]];
181 if( h->mb.i_partition == D_16x16 )
184 if( ref1a >= 0 ) x264_mb_mc_01xywh( h, 0, 0, 4, 4 );
185 else x264_mb_mc_0xywh ( h, 0, 0, 4, 4 );
186 else x264_mb_mc_1xywh ( h, 0, 0, 4, 4 );
188 else if( h->mb.i_partition == D_16x8 )
191 if( ref1a >= 0 ) x264_mb_mc_01xywh( h, 0, 0, 4, 2 );
192 else x264_mb_mc_0xywh ( h, 0, 0, 4, 2 );
193 else x264_mb_mc_1xywh ( h, 0, 0, 4, 2 );
196 if( ref1b >= 0 ) x264_mb_mc_01xywh( h, 0, 2, 4, 2 );
197 else x264_mb_mc_0xywh ( h, 0, 2, 4, 2 );
198 else x264_mb_mc_1xywh ( h, 0, 2, 4, 2 );
200 else if( h->mb.i_partition == D_8x16 )
203 if( ref1a >= 0 ) x264_mb_mc_01xywh( h, 0, 0, 2, 4 );
204 else x264_mb_mc_0xywh ( h, 0, 0, 2, 4 );
205 else x264_mb_mc_1xywh ( h, 0, 0, 2, 4 );
208 if( ref1b >= 0 ) x264_mb_mc_01xywh( h, 2, 0, 2, 4 );
209 else x264_mb_mc_0xywh ( h, 2, 0, 2, 4 );
210 else x264_mb_mc_1xywh ( h, 2, 0, 2, 4 );
215 int x264_macroblock_cache_allocate( x264_t *h )
217 int i_mb_count = h->mb.i_mb_count;
219 h->mb.i_mb_stride = h->sps->i_mb_width;
220 h->mb.i_b8_stride = h->sps->i_mb_width * 2;
221 h->mb.i_b4_stride = h->sps->i_mb_width * 4;
223 h->mb.b_interlaced = h->param.b_interlaced;
225 CHECKED_MALLOC( h->mb.qp, i_mb_count * sizeof(int8_t) );
226 CHECKED_MALLOC( h->mb.cbp, i_mb_count * sizeof(int16_t) );
227 CHECKED_MALLOC( h->mb.skipbp, i_mb_count * sizeof(int8_t) );
228 CHECKED_MALLOC( h->mb.mb_transform_size, i_mb_count * sizeof(int8_t) );
229 CHECKED_MALLOC( h->mb.slice_table, i_mb_count * sizeof(uint16_t) );
230 memset( h->mb.slice_table, -1, i_mb_count * sizeof(uint16_t) );
232 /* 0 -> 3 top(4), 4 -> 6 : left(3) */
233 CHECKED_MALLOC( h->mb.intra4x4_pred_mode, i_mb_count * 8 * sizeof(int8_t) );
236 CHECKED_MALLOC( h->mb.non_zero_count, i_mb_count * 24 * sizeof(uint8_t) );
238 if( h->param.b_cabac )
240 CHECKED_MALLOC( h->mb.chroma_pred_mode, i_mb_count * sizeof(int8_t) );
241 CHECKED_MALLOC( h->mb.mvd[0], i_mb_count * sizeof( **h->mb.mvd ) );
242 CHECKED_MALLOC( h->mb.mvd[1], i_mb_count * sizeof( **h->mb.mvd ) );
245 for( int i = 0; i < 2; i++ )
247 int i_refs = X264_MIN(16, (i ? 1 + !!h->param.i_bframe_pyramid : h->param.i_frame_reference) ) << h->param.b_interlaced;
248 if( h->param.analyse.i_weighted_pred == X264_WEIGHTP_SMART )
249 i_refs = X264_MIN(16, i_refs + 2); //smart weights add two duplicate frames
250 else if( h->param.analyse.i_weighted_pred == X264_WEIGHTP_BLIND )
251 i_refs = X264_MIN(16, i_refs + 1); //blind weights add one duplicate frame
253 for( int j = !i; j < i_refs; j++ )
254 CHECKED_MALLOC( h->mb.mvr[i][j], 2 * i_mb_count * sizeof(int16_t) );
257 if( h->param.analyse.i_weighted_pred )
259 int i_padv = PADV << h->param.b_interlaced;
260 #define ALIGN(x,a) (((x)+((a)-1))&~((a)-1))
261 int align = h->param.cpu&X264_CPU_CACHELINE_64 ? 64 : h->param.cpu&X264_CPU_CACHELINE_32 ? 32 : 16;
262 int i_stride, luma_plane_size = 0;
265 if( h->param.analyse.i_weighted_pred == X264_WEIGHTP_FAKE )
267 // only need buffer for lookahead
268 if( !h->param.i_sync_lookahead || h == h->thread[h->param.i_threads] )
270 // Fake analysis only works on lowres
271 i_stride = ALIGN( h->sps->i_mb_width*8 + 2*PADH, align );
272 luma_plane_size = i_stride * (h->sps->i_mb_height*8+2*i_padv);
273 // Only need 1 buffer for analysis
281 i_stride = ALIGN( h->sps->i_mb_width*16 + 2*PADH, align );
282 luma_plane_size = i_stride * (h->sps->i_mb_height*16+2*i_padv);
284 if( h->param.analyse.i_weighted_pred == X264_WEIGHTP_SMART )
285 //SMART can weight one ref and one offset -1
288 //blind only has one weighted copy (offset -1)
292 for( int i = 0; i < numweightbuf; i++ )
293 CHECKED_MALLOC( h->mb.p_weight_buf[i], luma_plane_size );
301 void x264_macroblock_cache_free( x264_t *h )
303 for( int i = 0; i < 2; i++ )
304 for( int j = !i; j < 32; j++ )
305 x264_free( h->mb.mvr[i][j] );
306 for( int i = 0; i < 16; i++ )
307 x264_free( h->mb.p_weight_buf[i] );
309 if( h->param.b_cabac )
311 x264_free( h->mb.chroma_pred_mode );
312 x264_free( h->mb.mvd[0] );
313 x264_free( h->mb.mvd[1] );
315 x264_free( h->mb.slice_table );
316 x264_free( h->mb.intra4x4_pred_mode );
317 x264_free( h->mb.non_zero_count );
318 x264_free( h->mb.mb_transform_size );
319 x264_free( h->mb.skipbp );
320 x264_free( h->mb.cbp );
321 x264_free( h->mb.qp );
324 int x264_macroblock_thread_allocate( x264_t *h, int b_lookahead )
327 for( int i = 0; i <= h->param.b_interlaced; i++ )
328 for( int j = 0; j < 3; j++ )
330 /* shouldn't really be initialized, just silences a valgrind false-positive in predict_8x8_filter_mmx */
331 CHECKED_MALLOCZERO( h->intra_border_backup[i][j], (h->sps->i_mb_width*16+32)>>!!j );
332 h->intra_border_backup[i][j] += 8;
335 /* Allocate scratch buffer */
336 int scratch_size = 0;
339 int buf_hpel = (h->thread[0]->fdec->i_width[0]+48) * sizeof(int16_t);
340 int buf_ssim = h->param.analyse.b_ssim * 8 * (h->param.i_width/4+3) * sizeof(int);
341 int me_range = X264_MIN(h->param.analyse.i_me_range, h->param.analyse.i_mv_range);
342 int buf_tesa = (h->param.analyse.i_me_method >= X264_ME_ESA) *
343 ((me_range*2+18) * sizeof(int16_t) + (me_range+4) * (me_range+1) * 4 * sizeof(mvsad_t));
344 int buf_nnz = !h->param.b_cabac * h->pps->b_transform_8x8_mode * (h->sps->i_mb_width * 4 * 16 * sizeof(uint8_t));
345 scratch_size = X264_MAX4( buf_hpel, buf_ssim, buf_tesa, buf_nnz );
347 int buf_mbtree = h->param.rc.b_mb_tree * ((h->sps->i_mb_width+3)&~3) * sizeof(int);
348 scratch_size = X264_MAX( scratch_size, buf_mbtree );
349 CHECKED_MALLOC( h->scratch_buffer, scratch_size );
356 void x264_macroblock_thread_free( x264_t *h, int b_lookahead )
359 for( int i = 0; i <= h->param.b_interlaced; i++ )
360 for( int j = 0; j < 3; j++ )
361 x264_free( h->intra_border_backup[i][j] - 8 );
362 x264_free( h->scratch_buffer );
365 void x264_macroblock_slice_init( x264_t *h )
367 h->mb.mv[0] = h->fdec->mv[0];
368 h->mb.mv[1] = h->fdec->mv[1];
369 h->mb.mvr[0][0] = h->fdec->mv16x16;
370 h->mb.ref[0] = h->fdec->ref[0];
371 h->mb.ref[1] = h->fdec->ref[1];
372 h->mb.type = h->fdec->mb_type;
373 h->mb.partition = h->fdec->mb_partition;
375 h->fdec->i_ref[0] = h->i_ref0;
376 h->fdec->i_ref[1] = h->i_ref1;
377 for( int i = 0; i < h->i_ref0; i++ )
378 h->fdec->ref_poc[0][i] = h->fref0[i]->i_poc;
379 if( h->sh.i_type == SLICE_TYPE_B )
381 for( int i = 0; i < h->i_ref1; i++ )
382 h->fdec->ref_poc[1][i] = h->fref1[i]->i_poc;
384 map_col_to_list0(-1) = -1;
385 map_col_to_list0(-2) = -2;
386 for( int i = 0; i < h->fref1[0]->i_ref[0]; i++ )
388 int poc = h->fref1[0]->ref_poc[0][i];
389 map_col_to_list0(i) = -2;
390 for( int j = 0; j < h->i_ref0; j++ )
391 if( h->fref0[j]->i_poc == poc )
393 map_col_to_list0(i) = j;
398 if( h->sh.i_type == SLICE_TYPE_P )
399 memset( h->mb.cache.skip, 0, sizeof( h->mb.cache.skip ) );
401 /* init with not available (for top right idx=7,15) */
402 memset( h->mb.cache.ref, -2, sizeof( h->mb.cache.ref ) );
405 for( int field = 0; field <= h->sh.b_mbaff; field++ )
407 int curpoc = h->fdec->i_poc + field*h->sh.i_delta_poc_bottom;
408 int refpoc = h->fref0[0]->i_poc;
409 if( h->sh.b_mbaff && field )
410 refpoc += h->sh.i_delta_poc_bottom;
411 int delta = curpoc - refpoc;
413 h->fdec->inv_ref_poc[field] = (256 + delta/2) / delta;
416 h->mb.i_neighbour4[6] =
417 h->mb.i_neighbour4[9] =
418 h->mb.i_neighbour4[12] =
419 h->mb.i_neighbour4[14] = MB_LEFT|MB_TOP|MB_TOPLEFT|MB_TOPRIGHT;
420 h->mb.i_neighbour4[3] =
421 h->mb.i_neighbour4[7] =
422 h->mb.i_neighbour4[11] =
423 h->mb.i_neighbour4[13] =
424 h->mb.i_neighbour4[15] =
425 h->mb.i_neighbour8[3] = MB_LEFT|MB_TOP|MB_TOPLEFT;
428 void x264_macroblock_thread_init( x264_t *h )
430 h->mb.i_me_method = h->param.analyse.i_me_method;
431 h->mb.i_subpel_refine = h->param.analyse.i_subpel_refine;
432 if( h->sh.i_type == SLICE_TYPE_B && (h->mb.i_subpel_refine == 6 || h->mb.i_subpel_refine == 8) )
433 h->mb.i_subpel_refine--;
434 h->mb.b_chroma_me = h->param.analyse.b_chroma_me && h->sh.i_type == SLICE_TYPE_P
435 && h->mb.i_subpel_refine >= 5;
436 h->mb.b_dct_decimate = h->sh.i_type == SLICE_TYPE_B ||
437 (h->param.analyse.b_dct_decimate && h->sh.i_type != SLICE_TYPE_I);
450 h->mb.pic.p_fenc[0] = h->mb.pic.fenc_buf;
451 h->mb.pic.p_fenc[1] = h->mb.pic.fenc_buf + 16*FENC_STRIDE;
452 h->mb.pic.p_fenc[2] = h->mb.pic.fenc_buf + 16*FENC_STRIDE + 8;
453 h->mb.pic.p_fdec[0] = h->mb.pic.fdec_buf + 2*FDEC_STRIDE;
454 h->mb.pic.p_fdec[1] = h->mb.pic.fdec_buf + 19*FDEC_STRIDE;
455 h->mb.pic.p_fdec[2] = h->mb.pic.fdec_buf + 19*FDEC_STRIDE + 16;
458 void x264_prefetch_fenc( x264_t *h, x264_frame_t *fenc, int i_mb_x, int i_mb_y )
460 int stride_y = fenc->i_stride[0];
461 int stride_uv = fenc->i_stride[1];
462 int off_y = 16 * (i_mb_x + i_mb_y * stride_y);
463 int off_uv = 8 * (i_mb_x + i_mb_y * stride_uv);
464 h->mc.prefetch_fenc( fenc->plane[0]+off_y, stride_y,
465 fenc->plane[1+(i_mb_x&1)]+off_uv, stride_uv, i_mb_x );
468 static NOINLINE void copy_column8( uint8_t *dst, uint8_t *src )
470 // input pointers are offset by 4 rows because that's faster (smaller instruction size on x86)
471 for( int i = -4; i < 4; i++ )
472 dst[i*FDEC_STRIDE] = src[i*FDEC_STRIDE];
475 static void ALWAYS_INLINE x264_macroblock_load_pic_pointers( x264_t *h, int mb_x, int mb_y, int i )
477 const int w = (i == 0 ? 16 : 8);
478 const int i_stride = h->fdec->i_stride[!!i];
479 const int i_stride2 = i_stride << h->mb.b_interlaced;
480 const int i_pix_offset = h->mb.b_interlaced
481 ? w * (mb_x + (mb_y&~1) * i_stride) + (mb_y&1) * i_stride
482 : w * (mb_x + mb_y * i_stride);
483 const uint8_t *plane_fdec = &h->fdec->plane[i][i_pix_offset];
484 const uint8_t *intra_fdec = &h->intra_border_backup[mb_y & h->sh.b_mbaff][i][mb_x*16>>!!i];
485 int ref_pix_offset[2] = { i_pix_offset, i_pix_offset };
486 x264_frame_t **fref[2] = { h->fref0, h->fref1 };
487 if( h->mb.b_interlaced )
488 ref_pix_offset[1] += (1-2*(mb_y&1)) * i_stride;
489 h->mb.pic.i_stride[i] = i_stride2;
490 h->mb.pic.p_fenc_plane[i] = &h->fenc->plane[i][i_pix_offset];
491 h->mc.copy[i?PIXEL_8x8:PIXEL_16x16]( h->mb.pic.p_fenc[i], FENC_STRIDE,
492 h->mb.pic.p_fenc_plane[i], i_stride2, w );
493 memcpy( &h->mb.pic.p_fdec[i][-1-FDEC_STRIDE], intra_fdec-1, w*3/2+1 );
494 if( h->mb.b_interlaced )
495 for( int j = 0; j < w; j++ )
496 h->mb.pic.p_fdec[i][-1+j*FDEC_STRIDE] = plane_fdec[-1+j*i_stride2];
497 for( int j = 0; j < h->mb.pic.i_fref[0]; j++ )
499 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]];
502 for( int k = 1; k < 4; k++ )
503 h->mb.pic.p_fref[0][j][k] = &fref[0][j >> h->mb.b_interlaced]->filtered[k][ref_pix_offset[j&1]];
504 if( h->sh.weight[j][0].weightfn )
505 h->mb.pic.p_fref_w[j] = &h->fenc->weighted[j >> h->mb.b_interlaced][ref_pix_offset[j&1]];
507 h->mb.pic.p_fref_w[j] = h->mb.pic.p_fref[0][j][0];
510 if( h->sh.i_type == SLICE_TYPE_B )
511 for( int j = 0; j < h->mb.pic.i_fref[1]; j++ )
513 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]];
515 for( int k = 1; k < 4; k++ )
516 h->mb.pic.p_fref[1][j][k] = &fref[1][j >> h->mb.b_interlaced]->filtered[k][ref_pix_offset[j&1]];
520 static void inline x264_macroblock_cache_load_neighbours( x264_t *h, int mb_x, int mb_y )
522 int top = (mb_y - (1 << h->mb.b_interlaced)) * h->mb.i_mb_stride + mb_x;
526 h->mb.i_mb_xy = mb_y * h->mb.i_mb_stride + mb_x;
527 h->mb.i_b8_xy = 2*(mb_y * h->mb.i_b8_stride + mb_x);
528 h->mb.i_b4_xy = 4*(mb_y * h->mb.i_b4_stride + mb_x);
529 h->mb.i_neighbour = 0;
530 h->mb.i_neighbour_intra = 0;
531 h->mb.i_neighbour_frame = 0;
532 h->mb.i_mb_top_xy = -1;
533 h->mb.i_mb_left_xy = -1;
534 h->mb.i_mb_topleft_xy = -1;
535 h->mb.i_mb_topright_xy = -1;
536 h->mb.i_mb_type_top = -1;
537 h->mb.i_mb_type_left = -1;
538 h->mb.i_mb_type_topleft = -1;
539 h->mb.i_mb_type_topright = -1;
543 h->mb.i_neighbour_frame |= MB_LEFT;
544 h->mb.i_mb_left_xy = h->mb.i_mb_xy - 1;
545 h->mb.i_mb_type_left = h->mb.type[h->mb.i_mb_left_xy];
546 if( h->mb.i_mb_xy > h->sh.i_first_mb )
548 h->mb.i_neighbour |= MB_LEFT;
550 if( !h->param.b_constrained_intra || IS_INTRA( h->mb.i_mb_type_left ) )
551 h->mb.i_neighbour_intra |= MB_LEFT;
555 /* We can't predict from the previous threadslice since it hasn't been encoded yet. */
556 if( (h->i_threadslice_start >> h->mb.b_interlaced) != (mb_y >> h->mb.b_interlaced) )
560 h->mb.i_neighbour_frame |= MB_TOP;
561 h->mb.i_mb_top_xy = top;
562 h->mb.i_mb_type_top = h->mb.type[h->mb.i_mb_top_xy];
563 if( top >= h->sh.i_first_mb )
565 h->mb.i_neighbour |= MB_TOP;
567 if( !h->param.b_constrained_intra || IS_INTRA( h->mb.i_mb_type_top ) )
568 h->mb.i_neighbour_intra |= MB_TOP;
570 /* We only need to prefetch the top blocks because the left was just written
571 * to as part of the previous cache_save. Since most target CPUs use write-allocate
572 * caches, left blocks are near-guaranteed to be in L1 cache. Top--not so much. */
573 x264_prefetch( &h->mb.cbp[top] );
574 x264_prefetch( h->mb.intra4x4_pred_mode[top] );
575 x264_prefetch( &h->mb.non_zero_count[top][12] );
576 /* These aren't always allocated, but prefetching an invalid address can't hurt. */
577 x264_prefetch( &h->mb.mb_transform_size[top] );
578 x264_prefetch( &h->mb.skipbp[top] );
582 if( mb_x > 0 && top - 1 >= 0 )
584 h->mb.i_neighbour_frame |= MB_TOPLEFT;
585 h->mb.i_mb_topleft_xy = top - 1;
586 h->mb.i_mb_type_topleft = h->mb.type[h->mb.i_mb_topleft_xy];
587 if( top - 1 >= h->sh.i_first_mb )
589 h->mb.i_neighbour |= MB_TOPLEFT;
591 if( !h->param.b_constrained_intra || IS_INTRA( h->mb.i_mb_type_topleft ) )
592 h->mb.i_neighbour_intra |= MB_TOPLEFT;
596 if( mb_x < h->sps->i_mb_width - 1 && top + 1 >= 0 )
598 h->mb.i_neighbour_frame |= MB_TOPRIGHT;
599 h->mb.i_mb_topright_xy = top + 1;
600 h->mb.i_mb_type_topright = h->mb.type[h->mb.i_mb_topright_xy];
601 if( top + 1 >= h->sh.i_first_mb )
603 h->mb.i_neighbour |= MB_TOPRIGHT;
605 if( !h->param.b_constrained_intra || IS_INTRA( h->mb.i_mb_type_topright ) )
606 h->mb.i_neighbour_intra |= MB_TOPRIGHT;
612 void x264_macroblock_cache_load( x264_t *h, int mb_x, int mb_y )
614 x264_macroblock_cache_load_neighbours( h, mb_x, mb_y );
616 int left = h->mb.i_mb_left_xy;
617 int top = h->mb.i_mb_top_xy;
618 int top_y = mb_y - (1 << h->mb.b_interlaced);
619 int top_8x8 = (2*top_y+1) * h->mb.i_b8_stride + 2*mb_x;
620 int top_4x4 = (4*top_y+3) * h->mb.i_b4_stride + 4*mb_x;
622 /* GCC pessimizes direct loads from heap-allocated arrays due to aliasing. */
623 /* By only dereferencing them once, we avoid this issue. */
624 int8_t (*i4x4)[8] = h->mb.intra4x4_pred_mode;
625 uint8_t (*nnz)[24] = h->mb.non_zero_count;
626 int16_t *cbp = h->mb.cbp;
629 if( h->mb.i_neighbour & MB_TOP )
631 h->mb.cache.i_cbp_top = cbp[top];
633 CP32( &h->mb.cache.intra4x4_pred_mode[x264_scan8[0] - 8], &i4x4[top][0] );
635 /* load non_zero_count */
636 CP32( &h->mb.cache.non_zero_count[x264_scan8[0] - 8], &nnz[top][12] );
637 /* shift because x264_scan8[16] is misaligned */
638 M32( &h->mb.cache.non_zero_count[x264_scan8[16+0] - 9] ) = M16( &nnz[top][18] ) << 8;
639 M32( &h->mb.cache.non_zero_count[x264_scan8[16+4] - 9] ) = M16( &nnz[top][22] ) << 8;
643 h->mb.cache.i_cbp_top = -1;
646 M32( &h->mb.cache.intra4x4_pred_mode[x264_scan8[0] - 8] ) = 0xFFFFFFFFU;
648 /* load non_zero_count */
649 M32( &h->mb.cache.non_zero_count[x264_scan8[ 0] - 8] ) = 0x80808080U;
650 M32( &h->mb.cache.non_zero_count[x264_scan8[16+0] - 9] ) = 0x80808080U;
651 M32( &h->mb.cache.non_zero_count[x264_scan8[16+4] - 9] ) = 0x80808080U;
654 if( h->mb.i_neighbour & MB_LEFT )
656 h->mb.cache.i_cbp_left = cbp[left];
659 h->mb.cache.intra4x4_pred_mode[x264_scan8[0 ] - 1] = i4x4[left][4];
660 h->mb.cache.intra4x4_pred_mode[x264_scan8[2 ] - 1] = i4x4[left][5];
661 h->mb.cache.intra4x4_pred_mode[x264_scan8[8 ] - 1] = i4x4[left][6];
662 h->mb.cache.intra4x4_pred_mode[x264_scan8[10] - 1] = i4x4[left][3];
664 /* load non_zero_count */
665 h->mb.cache.non_zero_count[x264_scan8[0 ] - 1] = nnz[left][3];
666 h->mb.cache.non_zero_count[x264_scan8[2 ] - 1] = nnz[left][7];
667 h->mb.cache.non_zero_count[x264_scan8[8 ] - 1] = nnz[left][11];
668 h->mb.cache.non_zero_count[x264_scan8[10] - 1] = nnz[left][15];
670 h->mb.cache.non_zero_count[x264_scan8[16+0] - 1] = nnz[left][16+1];
671 h->mb.cache.non_zero_count[x264_scan8[16+2] - 1] = nnz[left][16+3];
673 h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 1] = nnz[left][16+4+1];
674 h->mb.cache.non_zero_count[x264_scan8[16+4+2] - 1] = nnz[left][16+4+3];
676 /* Finish the prefetching */
677 if( h->sh.i_type != SLICE_TYPE_I )
678 for( int l = 0; l < (h->sh.i_type == SLICE_TYPE_B) + 1; l++ )
680 x264_prefetch( &h->mb.mv[l][top_4x4-1] );
681 /* Top right being not in the same cacheline as top left will happen
682 * once every 4 MBs, so one extra prefetch is worthwhile */
683 x264_prefetch( &h->mb.mv[l][top_4x4+4] );
684 x264_prefetch( &h->mb.ref[l][top_8x8-1] );
685 x264_prefetch( &h->mb.mvd[l][top] );
690 h->mb.cache.i_cbp_left = -1;
692 h->mb.cache.intra4x4_pred_mode[x264_scan8[0 ] - 1] =
693 h->mb.cache.intra4x4_pred_mode[x264_scan8[2 ] - 1] =
694 h->mb.cache.intra4x4_pred_mode[x264_scan8[8 ] - 1] =
695 h->mb.cache.intra4x4_pred_mode[x264_scan8[10] - 1] = -1;
697 /* load non_zero_count */
698 h->mb.cache.non_zero_count[x264_scan8[0 ] - 1] =
699 h->mb.cache.non_zero_count[x264_scan8[2 ] - 1] =
700 h->mb.cache.non_zero_count[x264_scan8[8 ] - 1] =
701 h->mb.cache.non_zero_count[x264_scan8[10] - 1] =
702 h->mb.cache.non_zero_count[x264_scan8[16+0] - 1] =
703 h->mb.cache.non_zero_count[x264_scan8[16+2] - 1] =
704 h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 1] =
705 h->mb.cache.non_zero_count[x264_scan8[16+4+2] - 1] = 0x80;
708 if( h->pps->b_transform_8x8_mode )
710 h->mb.cache.i_neighbour_transform_size =
711 ( (h->mb.i_neighbour & MB_LEFT) && h->mb.mb_transform_size[left] )
712 + ( (h->mb.i_neighbour & MB_TOP) && h->mb.mb_transform_size[top] );
717 h->mb.pic.i_fref[0] = h->i_ref0 << h->mb.b_interlaced;
718 h->mb.pic.i_fref[1] = h->i_ref1 << h->mb.b_interlaced;
719 h->mb.cache.i_neighbour_interlaced =
720 !!(h->mb.i_neighbour & MB_LEFT)
721 + !!(h->mb.i_neighbour & MB_TOP);
724 if( !h->mb.b_interlaced )
726 copy_column8( h->mb.pic.p_fdec[0]-1+ 4*FDEC_STRIDE, h->mb.pic.p_fdec[0]+15+ 4*FDEC_STRIDE );
727 copy_column8( h->mb.pic.p_fdec[0]-1+12*FDEC_STRIDE, h->mb.pic.p_fdec[0]+15+12*FDEC_STRIDE );
728 copy_column8( h->mb.pic.p_fdec[1]-1+ 4*FDEC_STRIDE, h->mb.pic.p_fdec[1]+ 7+ 4*FDEC_STRIDE );
729 copy_column8( h->mb.pic.p_fdec[2]-1+ 4*FDEC_STRIDE, h->mb.pic.p_fdec[2]+ 7+ 4*FDEC_STRIDE );
732 /* load picture pointers */
733 x264_macroblock_load_pic_pointers( h, mb_x, mb_y, 0 );
734 x264_macroblock_load_pic_pointers( h, mb_x, mb_y, 1 );
735 x264_macroblock_load_pic_pointers( h, mb_x, mb_y, 2 );
737 if( h->fdec->integral )
739 int offset = 16 * (mb_x + mb_y * h->fdec->i_stride[0]);
740 for( int i = 0; i < h->mb.pic.i_fref[0]; i++ )
741 h->mb.pic.p_integral[0][i] = &h->fref0[i]->integral[offset];
742 for( int i = 0; i < h->mb.pic.i_fref[1]; i++ )
743 h->mb.pic.p_integral[1][i] = &h->fref1[i]->integral[offset];
746 x264_prefetch_fenc( h, h->fenc, mb_x, mb_y );
748 /* load ref/mv/mvd */
749 if( h->sh.i_type != SLICE_TYPE_I )
751 int s8x8 = h->mb.i_b8_stride;
752 int s4x4 = h->mb.i_b4_stride;
754 for( int l = 0; l < (h->sh.i_type == SLICE_TYPE_B) + 1; l++ )
756 int16_t (*mv)[2] = h->mb.mv[l];
757 int8_t *ref = h->mb.ref[l];
759 int i8 = x264_scan8[0] - 1 - 1*8;
760 if( h->mb.i_neighbour & MB_TOPLEFT )
762 h->mb.cache.ref[l][i8] = ref[top_8x8 - 1];
763 CP32( h->mb.cache.mv[l][i8], mv[top_4x4 - 1] );
767 h->mb.cache.ref[l][i8] = -2;
768 M32( h->mb.cache.mv[l][i8] ) = 0;
771 i8 = x264_scan8[0] - 8;
772 if( h->mb.i_neighbour & MB_TOP )
774 h->mb.cache.ref[l][i8+0] =
775 h->mb.cache.ref[l][i8+1] = ref[top_8x8 + 0];
776 h->mb.cache.ref[l][i8+2] =
777 h->mb.cache.ref[l][i8+3] = ref[top_8x8 + 1];
778 CP128( h->mb.cache.mv[l][i8], mv[top_4x4] );
782 M128( h->mb.cache.mv[l][i8] ) = M128_ZERO;
783 M32( &h->mb.cache.ref[l][i8] ) = (uint8_t)(-2) * 0x01010101U;
786 i8 = x264_scan8[0] + 4 - 1*8;
787 if( h->mb.i_neighbour & MB_TOPRIGHT )
789 h->mb.cache.ref[l][i8] = ref[top_8x8 + 2];
790 CP32( h->mb.cache.mv[l][i8], mv[top_4x4 + 4] );
793 h->mb.cache.ref[l][i8] = -2;
795 i8 = x264_scan8[0] - 1;
796 if( h->mb.i_neighbour & MB_LEFT )
798 const int ir = h->mb.i_b8_xy - 1;
799 const int iv = h->mb.i_b4_xy - 1;
800 h->mb.cache.ref[l][i8+0*8] =
801 h->mb.cache.ref[l][i8+1*8] = ref[ir + 0*s8x8];
802 h->mb.cache.ref[l][i8+2*8] =
803 h->mb.cache.ref[l][i8+3*8] = ref[ir + 1*s8x8];
805 CP32( h->mb.cache.mv[l][i8+0*8], mv[iv + 0*s4x4] );
806 CP32( h->mb.cache.mv[l][i8+1*8], mv[iv + 1*s4x4] );
807 CP32( h->mb.cache.mv[l][i8+2*8], mv[iv + 2*s4x4] );
808 CP32( h->mb.cache.mv[l][i8+3*8], mv[iv + 3*s4x4] );
812 for( int i = 0; i < 4; i++ )
814 h->mb.cache.ref[l][i8+i*8] = -2;
815 M32( h->mb.cache.mv[l][i8+i*8] ) = 0;
819 if( h->param.b_cabac )
821 uint8_t (*mvd)[8][2] = h->mb.mvd[l];
822 if( h->mb.i_neighbour & MB_TOP )
823 CP64( h->mb.cache.mvd[l][x264_scan8[0] - 8], mvd[top][0] );
825 M64( h->mb.cache.mvd[l][x264_scan8[0] - 8] ) = 0;
827 if( h->mb.i_neighbour & MB_LEFT )
829 CP16( h->mb.cache.mvd[l][x264_scan8[0 ] - 1], mvd[left][4] );
830 CP16( h->mb.cache.mvd[l][x264_scan8[2 ] - 1], mvd[left][5] );
831 CP16( h->mb.cache.mvd[l][x264_scan8[8 ] - 1], mvd[left][6] );
832 CP16( h->mb.cache.mvd[l][x264_scan8[10] - 1], mvd[left][3] );
835 for( int i = 0; i < 4; i++ )
836 M16( h->mb.cache.mvd[l][x264_scan8[0]-1+i*8] ) = 0;
841 if( h->sh.i_type == SLICE_TYPE_B )
843 h->mb.bipred_weight = h->mb.bipred_weight_buf[h->mb.b_interlaced&(mb_y&1)];
844 h->mb.dist_scale_factor = h->mb.dist_scale_factor_buf[h->mb.b_interlaced&(mb_y&1)];
845 if( h->param.b_cabac )
848 x264_macroblock_cache_skip( h, 0, 0, 4, 4, 0 );
849 skipbp = (h->mb.i_neighbour & MB_LEFT) ? h->mb.skipbp[left] : 0;
850 h->mb.cache.skip[x264_scan8[0] - 1] = skipbp & 0x2;
851 h->mb.cache.skip[x264_scan8[8] - 1] = skipbp & 0x8;
852 skipbp = (h->mb.i_neighbour & MB_TOP) ? h->mb.skipbp[top] : 0;
853 h->mb.cache.skip[x264_scan8[0] - 8] = skipbp & 0x4;
854 h->mb.cache.skip[x264_scan8[4] - 8] = skipbp & 0x8;
858 if( h->sh.i_type == SLICE_TYPE_P )
859 x264_mb_predict_mv_pskip( h, h->mb.cache.pskip_mv );
862 h->mb.i_neighbour4[0] =
863 h->mb.i_neighbour8[0] = (h->mb.i_neighbour_intra & (MB_TOP|MB_LEFT|MB_TOPLEFT))
864 | ((h->mb.i_neighbour_intra & MB_TOP) ? MB_TOPRIGHT : 0);
865 h->mb.i_neighbour4[4] =
866 h->mb.i_neighbour4[1] = MB_LEFT | ((h->mb.i_neighbour_intra & MB_TOP) ? (MB_TOP|MB_TOPLEFT|MB_TOPRIGHT) : 0);
867 h->mb.i_neighbour4[2] =
868 h->mb.i_neighbour4[8] =
869 h->mb.i_neighbour4[10] =
870 h->mb.i_neighbour8[2] = MB_TOP|MB_TOPRIGHT | ((h->mb.i_neighbour_intra & MB_LEFT) ? (MB_LEFT|MB_TOPLEFT) : 0);
871 h->mb.i_neighbour4[5] =
872 h->mb.i_neighbour8[1] = MB_LEFT | (h->mb.i_neighbour_intra & MB_TOPRIGHT)
873 | ((h->mb.i_neighbour_intra & MB_TOP) ? MB_TOP|MB_TOPLEFT : 0);
876 static void ALWAYS_INLINE x264_macroblock_store_pic( x264_t *h, int i )
879 int i_stride = h->fdec->i_stride[!!i];
880 int i_stride2 = i_stride << h->mb.b_interlaced;
881 int i_pix_offset = h->mb.b_interlaced
882 ? w * (h->mb.i_mb_x + (h->mb.i_mb_y&~1) * i_stride) + (h->mb.i_mb_y&1) * i_stride
883 : w * (h->mb.i_mb_x + h->mb.i_mb_y * i_stride);
884 h->mc.copy[i?PIXEL_8x8:PIXEL_16x16]( &h->fdec->plane[i][i_pix_offset], i_stride2,
885 h->mb.pic.p_fdec[i], FDEC_STRIDE, w );
888 void x264_macroblock_cache_save( x264_t *h )
890 const int i_mb_xy = h->mb.i_mb_xy;
891 const int i_mb_type = x264_mb_type_fix[h->mb.i_type];
892 const int s8x8 = h->mb.i_b8_stride;
893 const int s4x4 = h->mb.i_b4_stride;
894 const int i_mb_4x4 = h->mb.i_b4_xy;
895 const int i_mb_8x8 = h->mb.i_b8_xy;
897 /* GCC pessimizes direct stores to heap-allocated arrays due to aliasing. */
898 /* By only dereferencing them once, we avoid this issue. */
899 int8_t *i4x4 = h->mb.intra4x4_pred_mode[i_mb_xy];
900 uint8_t *nnz = h->mb.non_zero_count[i_mb_xy];
902 x264_macroblock_store_pic( h, 0 );
903 x264_macroblock_store_pic( h, 1 );
904 x264_macroblock_store_pic( h, 2 );
906 x264_prefetch_fenc( h, h->fdec, h->mb.i_mb_x, h->mb.i_mb_y );
908 h->mb.type[i_mb_xy] = i_mb_type;
909 h->mb.slice_table[i_mb_xy] = h->sh.i_first_mb;
910 h->mb.partition[i_mb_xy] = IS_INTRA( i_mb_type ) ? D_16x16 : h->mb.i_partition;
911 h->mb.i_mb_prev_xy = i_mb_xy;
914 if( i_mb_type == I_4x4 )
916 CP32( &i4x4[0], &h->mb.cache.intra4x4_pred_mode[x264_scan8[10]] );
917 M32( &i4x4[4] ) = pack8to32( h->mb.cache.intra4x4_pred_mode[x264_scan8[5] ],
918 h->mb.cache.intra4x4_pred_mode[x264_scan8[7] ],
919 h->mb.cache.intra4x4_pred_mode[x264_scan8[13] ], 0);
921 else if( !h->param.b_constrained_intra || IS_INTRA(i_mb_type) )
922 M64( i4x4 ) = I_PRED_4x4_DC * 0x0101010101010101ULL;
924 M64( i4x4 ) = (uint8_t)(-1) * 0x0101010101010101ULL;
927 if( i_mb_type == I_PCM )
929 h->mb.qp[i_mb_xy] = 0;
930 h->mb.i_last_dqp = 0;
931 h->mb.i_cbp_chroma = 2;
932 h->mb.i_cbp_luma = 0xf;
933 h->mb.cbp[i_mb_xy] = 0x72f; /* all set */
934 h->mb.b_transform_8x8 = 0;
935 memset( nnz, 16, sizeof( *h->mb.non_zero_count ) );
939 /* save non zero count */
940 CP32( &nnz[0*4], &h->mb.cache.non_zero_count[x264_scan8[0]+0*8] );
941 CP32( &nnz[1*4], &h->mb.cache.non_zero_count[x264_scan8[0]+1*8] );
942 CP32( &nnz[2*4], &h->mb.cache.non_zero_count[x264_scan8[0]+2*8] );
943 CP32( &nnz[3*4], &h->mb.cache.non_zero_count[x264_scan8[0]+3*8] );
944 M16( &nnz[16+0*2] ) = M32( &h->mb.cache.non_zero_count[x264_scan8[16+0*2]-1] ) >> 8;
945 M16( &nnz[16+1*2] ) = M32( &h->mb.cache.non_zero_count[x264_scan8[16+1*2]-1] ) >> 8;
946 M16( &nnz[16+2*2] ) = M32( &h->mb.cache.non_zero_count[x264_scan8[16+2*2]-1] ) >> 8;
947 M16( &nnz[16+3*2] ) = M32( &h->mb.cache.non_zero_count[x264_scan8[16+3*2]-1] ) >> 8;
949 if( h->mb.i_type != I_16x16 && h->mb.i_cbp_luma == 0 && h->mb.i_cbp_chroma == 0 )
950 h->mb.i_qp = h->mb.i_last_qp;
951 h->mb.qp[i_mb_xy] = h->mb.i_qp;
952 h->mb.i_last_dqp = h->mb.i_qp - h->mb.i_last_qp;
953 h->mb.i_last_qp = h->mb.i_qp;
956 if( h->mb.i_cbp_luma == 0 && h->mb.i_type != I_8x8 )
957 h->mb.b_transform_8x8 = 0;
958 h->mb.mb_transform_size[i_mb_xy] = h->mb.b_transform_8x8;
960 if( h->sh.i_type != SLICE_TYPE_I )
962 int16_t (*mv0)[2] = &h->mb.mv[0][i_mb_4x4];
963 int16_t (*mv1)[2] = &h->mb.mv[1][i_mb_4x4];
964 int8_t *ref0 = &h->mb.ref[0][i_mb_8x8];
965 int8_t *ref1 = &h->mb.ref[1][i_mb_8x8];
966 if( !IS_INTRA( i_mb_type ) )
968 ref0[0+0*s8x8] = h->mb.cache.ref[0][x264_scan8[0]];
969 ref0[1+0*s8x8] = h->mb.cache.ref[0][x264_scan8[4]];
970 ref0[0+1*s8x8] = h->mb.cache.ref[0][x264_scan8[8]];
971 ref0[1+1*s8x8] = h->mb.cache.ref[0][x264_scan8[12]];
972 CP128( &mv0[0*s4x4], h->mb.cache.mv[0][x264_scan8[0]+8*0] );
973 CP128( &mv0[1*s4x4], h->mb.cache.mv[0][x264_scan8[0]+8*1] );
974 CP128( &mv0[2*s4x4], h->mb.cache.mv[0][x264_scan8[0]+8*2] );
975 CP128( &mv0[3*s4x4], h->mb.cache.mv[0][x264_scan8[0]+8*3] );
976 if( h->sh.i_type == SLICE_TYPE_B )
978 ref1[0+0*s8x8] = h->mb.cache.ref[1][x264_scan8[0]];
979 ref1[1+0*s8x8] = h->mb.cache.ref[1][x264_scan8[4]];
980 ref1[0+1*s8x8] = h->mb.cache.ref[1][x264_scan8[8]];
981 ref1[1+1*s8x8] = h->mb.cache.ref[1][x264_scan8[12]];
982 CP128( &mv1[0*s4x4], h->mb.cache.mv[1][x264_scan8[0]+8*0] );
983 CP128( &mv1[1*s4x4], h->mb.cache.mv[1][x264_scan8[0]+8*1] );
984 CP128( &mv1[2*s4x4], h->mb.cache.mv[1][x264_scan8[0]+8*2] );
985 CP128( &mv1[3*s4x4], h->mb.cache.mv[1][x264_scan8[0]+8*3] );
990 M16( &ref0[0*s8x8] ) = (uint8_t)(-1) * 0x0101;
991 M16( &ref0[1*s8x8] ) = (uint8_t)(-1) * 0x0101;
992 M128( &mv0[0*s4x4] ) = M128_ZERO;
993 M128( &mv0[1*s4x4] ) = M128_ZERO;
994 M128( &mv0[2*s4x4] ) = M128_ZERO;
995 M128( &mv0[3*s4x4] ) = M128_ZERO;
996 if( h->sh.i_type == SLICE_TYPE_B )
998 M16( &ref1[0*s8x8] ) = (uint8_t)(-1) * 0x0101;
999 M16( &ref1[1*s8x8] ) = (uint8_t)(-1) * 0x0101;
1000 M128( &mv1[0*s4x4] ) = M128_ZERO;
1001 M128( &mv1[1*s4x4] ) = M128_ZERO;
1002 M128( &mv1[2*s4x4] ) = M128_ZERO;
1003 M128( &mv1[3*s4x4] ) = M128_ZERO;
1008 if( h->param.b_cabac )
1010 uint8_t (*mvd0)[2] = h->mb.mvd[0][i_mb_xy];
1011 uint8_t (*mvd1)[2] = h->mb.mvd[1][i_mb_xy];
1012 if( IS_INTRA(i_mb_type) && i_mb_type != I_PCM )
1013 h->mb.chroma_pred_mode[i_mb_xy] = x264_mb_pred_mode8x8c_fix[ h->mb.i_chroma_pred_mode ];
1015 h->mb.chroma_pred_mode[i_mb_xy] = I_PRED_CHROMA_DC;
1017 if( !IS_INTRA( i_mb_type ) && !IS_SKIP( i_mb_type ) && !IS_DIRECT( i_mb_type ) )
1019 CP64( mvd0[0], h->mb.cache.mvd[0][x264_scan8[10]] );
1020 CP16( mvd0[4], h->mb.cache.mvd[0][x264_scan8[5 ]] );
1021 CP16( mvd0[5], h->mb.cache.mvd[0][x264_scan8[7 ]] );
1022 CP16( mvd0[6], h->mb.cache.mvd[0][x264_scan8[13]] );
1023 if( h->sh.i_type == SLICE_TYPE_B )
1025 CP64( mvd1[0], h->mb.cache.mvd[1][x264_scan8[10]] );
1026 CP16( mvd1[4], h->mb.cache.mvd[1][x264_scan8[5 ]] );
1027 CP16( mvd1[5], h->mb.cache.mvd[1][x264_scan8[7 ]] );
1028 CP16( mvd1[6], h->mb.cache.mvd[1][x264_scan8[13]] );
1033 M128( mvd0[0] ) = M128_ZERO;
1034 if( h->sh.i_type == SLICE_TYPE_B )
1035 M128( mvd1[0] ) = M128_ZERO;
1038 if( h->sh.i_type == SLICE_TYPE_B )
1040 if( i_mb_type == B_SKIP || i_mb_type == B_DIRECT )
1041 h->mb.skipbp[i_mb_xy] = 0xf;
1042 else if( i_mb_type == B_8x8 )
1044 int skipbp = ( h->mb.i_sub_partition[0] == D_DIRECT_8x8 ) << 0;
1045 skipbp |= ( h->mb.i_sub_partition[1] == D_DIRECT_8x8 ) << 1;
1046 skipbp |= ( h->mb.i_sub_partition[2] == D_DIRECT_8x8 ) << 2;
1047 skipbp |= ( h->mb.i_sub_partition[3] == D_DIRECT_8x8 ) << 3;
1048 h->mb.skipbp[i_mb_xy] = skipbp;
1051 h->mb.skipbp[i_mb_xy] = 0;
1057 void x264_macroblock_bipred_init( x264_t *h )
1059 for( int field = 0; field <= h->sh.b_mbaff; field++ )
1060 for( int i_ref0 = 0; i_ref0 < (h->i_ref0<<h->sh.b_mbaff); i_ref0++ )
1062 int poc0 = h->fref0[i_ref0>>h->sh.b_mbaff]->i_poc;
1063 if( h->sh.b_mbaff && field^(i_ref0&1) )
1064 poc0 += h->sh.i_delta_poc_bottom;
1065 for( int i_ref1 = 0; i_ref1 < (h->i_ref1<<h->sh.b_mbaff); i_ref1++ )
1067 int dist_scale_factor;
1068 int poc1 = h->fref1[i_ref1>>h->sh.b_mbaff]->i_poc;
1069 if( h->sh.b_mbaff && field^(i_ref1&1) )
1070 poc1 += h->sh.i_delta_poc_bottom;
1071 int cur_poc = h->fdec->i_poc + field*h->sh.i_delta_poc_bottom;
1072 int td = x264_clip3( poc1 - poc0, -128, 127 );
1073 if( td == 0 /* || pic0 is a long-term ref */ )
1074 dist_scale_factor = 256;
1077 int tb = x264_clip3( cur_poc - poc0, -128, 127 );
1078 int tx = (16384 + (abs(td) >> 1)) / td;
1079 dist_scale_factor = x264_clip3( (tb * tx + 32) >> 6, -1024, 1023 );
1082 h->mb.dist_scale_factor_buf[field][i_ref0][i_ref1] = dist_scale_factor;
1084 dist_scale_factor >>= 2;
1085 if( h->param.analyse.b_weighted_bipred
1086 && dist_scale_factor >= -64
1087 && dist_scale_factor <= 128 )
1089 h->mb.bipred_weight_buf[field][i_ref0][i_ref1] = 64 - dist_scale_factor;
1090 // ssse3 implementation of biweight doesn't support the extrema.
1091 // if we ever generate them, we'll have to drop that optimization.
1092 assert( dist_scale_factor >= -63 && dist_scale_factor <= 127 );
1095 h->mb.bipred_weight_buf[field][i_ref0][i_ref1] = 32;