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 *****************************************************************************/
29 #include "macroblock.h"
31 static const uint8_t block_idx_x[16] =
33 0, 1, 0, 1, 2, 3, 2, 3, 0, 1, 0, 1, 2, 3, 2, 3
35 static const uint8_t block_idx_y[16] =
37 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, 3, 3, 2, 2, 3, 3
39 static const uint8_t block_idx_xy[4][4] =
47 static const int dequant_mf[6][4][4] =
49 { {10, 13, 10, 13}, {13, 16, 13, 16}, {10, 13, 10, 13}, {13, 16, 13, 16} },
50 { {11, 14, 11, 14}, {14, 18, 14, 18}, {11, 14, 11, 14}, {14, 18, 14, 18} },
51 { {13, 16, 13, 16}, {16, 20, 16, 20}, {13, 16, 13, 16}, {16, 20, 16, 20} },
52 { {14, 18, 14, 18}, {18, 23, 18, 23}, {14, 18, 14, 18}, {18, 23, 18, 23} },
53 { {16, 20, 16, 20}, {20, 25, 20, 25}, {16, 20, 16, 20}, {20, 25, 20, 25} },
54 { {18, 23, 18, 23}, {23, 29, 23, 29}, {18, 23, 18, 23}, {23, 29, 23, 29} }
58 static const int i_chroma_qp_table[52] =
60 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
61 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
62 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
63 29, 30, 31, 32, 32, 33, 34, 34, 35, 35,
64 36, 36, 37, 37, 37, 38, 38, 38, 39, 39,
69 int x264_mb_predict_intra4x4_mode( x264_t *h, int idx )
71 const int ma = h->mb.cache.intra4x4_pred_mode[x264_scan8[idx] - 1];
72 const int mb = h->mb.cache.intra4x4_pred_mode[x264_scan8[idx] - 8];
73 const int m = X264_MIN( ma, mb );
81 int x264_mb_predict_non_zero_code( x264_t *h, int idx )
83 const int za = h->mb.cache.non_zero_count[x264_scan8[idx] - 1];
84 const int zb = h->mb.cache.non_zero_count[x264_scan8[idx] - 8];
90 i_ret = ( i_ret + 1 ) >> 1;
95 /****************************************************************************
96 * Scan and Quant functions
97 ****************************************************************************/
98 void x264_mb_dequant_2x2_dc( int16_t dct[2][2], int i_qscale )
100 const int i_qbits = i_qscale/6 - 1;
104 const int i_dmf = dequant_mf[i_qscale%6][0][0] << i_qbits;
106 dct[0][0] = dct[0][0] * i_dmf;
107 dct[0][1] = dct[0][1] * i_dmf;
108 dct[1][0] = dct[1][0] * i_dmf;
109 dct[1][1] = dct[1][1] * i_dmf;
113 const int i_dmf = dequant_mf[i_qscale%6][0][0];
115 dct[0][0] = ( dct[0][0] * i_dmf ) >> 1;
116 dct[0][1] = ( dct[0][1] * i_dmf ) >> 1;
117 dct[1][0] = ( dct[1][0] * i_dmf ) >> 1;
118 dct[1][1] = ( dct[1][1] * i_dmf ) >> 1;
122 void x264_mb_dequant_4x4_dc( int16_t dct[4][4], int i_qscale )
124 const int i_qbits = i_qscale/6 - 2;
129 const int i_dmf = dequant_mf[i_qscale%6][0][0] << i_qbits;
131 for( y = 0; y < 4; y++ )
133 for( x = 0; x < 4; x++ )
135 dct[y][x] = dct[y][x] * i_dmf;
141 const int i_dmf = dequant_mf[i_qscale%6][0][0];
142 const int f = 1 << ( 1 + i_qbits );
144 for( y = 0; y < 4; y++ )
146 for( x = 0; x < 4; x++ )
148 dct[y][x] = ( dct[y][x] * i_dmf + f ) >> (-i_qbits);
154 void x264_mb_dequant_4x4( int16_t dct[4][4], int i_qscale )
156 const int i_mf = i_qscale%6;
157 const int i_qbits = i_qscale/6;
160 for( y = 0; y < 4; y++ )
162 dct[y][0] = ( dct[y][0] * dequant_mf[i_mf][y][0] ) << i_qbits;
163 dct[y][1] = ( dct[y][1] * dequant_mf[i_mf][y][1] ) << i_qbits;
164 dct[y][2] = ( dct[y][2] * dequant_mf[i_mf][y][2] ) << i_qbits;
165 dct[y][3] = ( dct[y][3] * dequant_mf[i_mf][y][3] ) << i_qbits;
169 static inline int x264_median( int a, int b, int c )
175 max = b; /* no need to do 'b > max' (more consuming than always doing affectation) */
182 return a + b + c - min - max;
185 void x264_mb_predict_mv( x264_t *h, int i_list, int idx, int i_width, int mvp[2] )
187 const int i8 = x264_scan8[idx];
188 const int i_ref= h->mb.cache.ref[i_list][i8];
189 int i_refa = h->mb.cache.ref[i_list][i8 - 1];
190 int16_t *mv_a = h->mb.cache.mv[i_list][i8 - 1];
191 int i_refb = h->mb.cache.ref[i_list][i8 - 8];
192 int16_t *mv_b = h->mb.cache.mv[i_list][i8 - 8];
193 int i_refc = h->mb.cache.ref[i_list][i8 - 8 + i_width ];
194 int16_t *mv_c = h->mb.cache.mv[i_list][i8 - 8 + i_width];
198 if( (idx&0x03) == 3 || ( i_width == 2 && (idx&0x3) == 2 )|| i_refc == -2 )
200 i_refc = h->mb.cache.ref[i_list][i8 - 8 - 1];
201 mv_c = h->mb.cache.mv[i_list][i8 - 8 - 1];
204 if( h->mb.i_partition == D_16x8 )
206 if( idx == 0 && i_refb == i_ref )
212 else if( idx != 0 && i_refa == i_ref )
219 else if( h->mb.i_partition == D_8x16 )
221 if( idx == 0 && i_refa == i_ref )
227 else if( idx != 0 && i_refc == i_ref )
236 if( i_refa == i_ref ) i_count++;
237 if( i_refb == i_ref ) i_count++;
238 if( i_refc == i_ref ) i_count++;
242 mvp[0] = x264_median( mv_a[0], mv_b[0], mv_c[0] );
243 mvp[1] = x264_median( mv_a[1], mv_b[1], mv_c[1] );
245 else if( i_count == 1 )
247 if( i_refa == i_ref )
252 else if( i_refb == i_ref )
263 else if( i_refb == -2 && i_refc == -2 && i_refa != -2 )
270 mvp[0] = x264_median( mv_a[0], mv_b[0], mv_c[0] );
271 mvp[1] = x264_median( mv_a[1], mv_b[1], mv_c[1] );
275 void x264_mb_predict_mv_16x16( x264_t *h, int i_list, int i_ref, int mvp[2] )
277 int i_refa = h->mb.cache.ref[i_list][X264_SCAN8_0 - 1];
278 int16_t *mv_a = h->mb.cache.mv[i_list][X264_SCAN8_0 - 1];
279 int i_refb = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8];
280 int16_t *mv_b = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8];
281 int i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 + 4];
282 int16_t *mv_c = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8 + 4];
288 i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 - 1];
289 mv_c = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8 - 1];
293 if( i_refa == i_ref ) i_count++;
294 if( i_refb == i_ref ) i_count++;
295 if( i_refc == i_ref ) i_count++;
299 mvp[0] = x264_median( mv_a[0], mv_b[0], mv_c[0] );
300 mvp[1] = x264_median( mv_a[1], mv_b[1], mv_c[1] );
302 else if( i_count == 1 )
304 if( i_refa == i_ref )
309 else if( i_refb == i_ref )
320 else if( i_refb == -2 && i_refc == -2 && i_refa != -2 )
327 mvp[0] = x264_median( mv_a[0], mv_b[0], mv_c[0] );
328 mvp[1] = x264_median( mv_a[1], mv_b[1], mv_c[1] );
333 void x264_mb_predict_mv_pskip( x264_t *h, int mv[2] )
335 int i_refa = h->mb.cache.ref[0][X264_SCAN8_0 - 1];
336 int i_refb = h->mb.cache.ref[0][X264_SCAN8_0 - 8];
337 int16_t *mv_a = h->mb.cache.mv[0][X264_SCAN8_0 - 1];
338 int16_t *mv_b = h->mb.cache.mv[0][X264_SCAN8_0 - 8];
340 if( i_refa == -2 || i_refb == -2 ||
341 ( i_refa == 0 && mv_a[0] == 0 && mv_a[1] == 0 ) ||
342 ( i_refb == 0 && mv_b[0] == 0 && mv_b[1] == 0 ) )
348 x264_mb_predict_mv_16x16( h, 0, 0, mv );
352 static int x264_mb_predict_mv_direct16x16_temporal( x264_t *h )
354 int i_mb_4x4 = 16 * h->mb.i_mb_stride * h->mb.i_mb_y + 4 * h->mb.i_mb_x;
355 int i_mb_8x8 = 4 * h->mb.i_mb_stride * h->mb.i_mb_y + 2 * h->mb.i_mb_x;
358 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, 0 );
360 /* FIXME: optimize per block size */
361 for( i = 0; i < 4; i++ )
363 const int x8 = 2*(i%2);
364 const int y8 = 2*(i/2);
365 /* TODO: MapColToList0 */
366 const int i_ref = h->fref1[0]->ref[0][ i_mb_8x8 + x8/2 + y8 * h->mb.i_mb_stride ];
370 x264_macroblock_cache_ref( h, x8, y8, 2, 2, 0, 0 );
371 x264_macroblock_cache_mv( h, x8, y8, 2, 2, 0, 0, 0 );
372 x264_macroblock_cache_mv( h, x8, y8, 2, 2, 1, 0, 0 );
376 int poc0 = h->fref0[i_ref]->i_poc;
377 int poc1 = h->fref1[0]->i_poc;
378 int td = x264_clip3( poc1 - poc0, -128, 127 );
379 int dist_scale_factor;
381 if( td == 0 /* || pic0 is a long-term ref */ )
382 dist_scale_factor = 256;
385 int tb = x264_clip3( h->fdec->i_poc - poc0, -128, 127 );
386 int tx = (16384 + (abs(td) >> 1)) / td;
387 dist_scale_factor = x264_clip3( (tb * tx + 32) >> 6, -1024, 1023 );
390 x264_macroblock_cache_ref( h, x8, y8, 2, 2, 0, i_ref );
392 for( y4 = y8; y4 < y8+2; y4++ )
393 for( x4 = x8; x4 < x8+2; x4++ )
395 const int16_t *mv_col = h->fref1[0]->mv[0][ i_mb_4x4 + x4 + y4 * 4 * h->mb.i_mb_stride ];
397 mv_l0[0] = ( dist_scale_factor * mv_col[0] + 128 ) >> 8;
398 mv_l0[1] = ( dist_scale_factor * mv_col[1] + 128 ) >> 8;
399 x264_macroblock_cache_mv( h, x4, y4, 1, 1, 0, mv_l0[0], mv_l0[1] );
400 x264_macroblock_cache_mv( h, x4, y4, 1, 1, 1, mv_l0[0] - mv_col[0], mv_l0[1] - mv_col[1] );
408 static int x264_mb_predict_mv_direct16x16_spatial( x264_t *h )
414 const int8_t *l1ref = &h->fref1[0]->ref[0][ h->mb.i_b8_xy ];
415 const int16_t (*l1mv)[2] = (const int16_t (*)[2]) &h->fref1[0]->mv[0][ h->mb.i_b4_xy ];
417 for( i_list=0; i_list<2; i_list++ )
419 int i_refa = h->mb.cache.ref[i_list][X264_SCAN8_0 - 1];
420 int i_refb = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8];
421 int i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 + 4];
423 i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 - 1];
425 ref[i_list] = i_refa;
426 if( ref[i_list] < 0 || ( i_refb < ref[i_list] && i_refb >= 0 ))
427 ref[i_list] = i_refb;
428 if( ref[i_list] < 0 || ( i_refc < ref[i_list] && i_refc >= 0 ))
429 ref[i_list] = i_refc;
430 if( ref[i_list] < 0 )
434 if( ref[0] < 0 && ref[1] < 0 )
445 for( i_list=0; i_list<2; i_list++ )
447 if( ref[i_list] >= 0 )
448 x264_mb_predict_mv_16x16( h, i_list, ref[i_list], mv[i_list] );
450 mv[i_list][0] = mv[i_list][1] = 0;
454 /* FIXME: clip mv ? */
456 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, ref[0] );
457 x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, ref[1] );
458 x264_macroblock_cache_mv( h, 0, 0, 4, 4, 0, mv[0][0], mv[0][1] );
459 x264_macroblock_cache_mv( h, 0, 0, 4, 4, 1, mv[1][0], mv[1][1] );
462 for( i8=0; i8<4; i8++ )
466 if( l1ref[ x8 + y8 * h->mb.i_b8_stride ] == 0 )
468 for( i4=0; i4<4; i4++ )
470 const int x4 = i4%2 + 2*x8;
471 const int y4 = i4/2 + 2*y8;
472 const int16_t *mvcol = l1mv[x4 + y4 * h->mb.i_b4_stride];
473 if( abs( mvcol[0] ) <= 1 && abs( mvcol[1] ) <= 1 )
476 x264_macroblock_cache_mv( h, x4, y4, 1, 1, 0, 0, 0 );
478 x264_macroblock_cache_mv( h, x4, y4, 1, 1, 1, 0, 0 );
487 int x264_mb_predict_mv_direct16x16( x264_t *h )
490 if( h->param.analyse.i_direct_mv_pred == X264_DIRECT_PRED_NONE )
492 else if( h->sh.b_direct_spatial_mv_pred )
493 b_available = x264_mb_predict_mv_direct16x16_spatial( h );
495 b_available = x264_mb_predict_mv_direct16x16_temporal( h );
501 for( l = 0; l < 2; l++ )
502 for( i = 0; i < 4; i++ )
503 h->mb.cache.direct_ref[l][i] = h->mb.cache.ref[l][x264_scan8[i*4]];
504 memcpy(h->mb.cache.direct_mv, h->mb.cache.mv, sizeof(h->mb.cache.mv));
510 void x264_mb_load_mv_direct8x8( x264_t *h, int idx )
512 const int x = 2*(idx%2);
513 const int y = 2*(idx/2);
515 x264_macroblock_cache_ref( h, x, y, 2, 2, 0, h->mb.cache.direct_ref[0][idx] );
516 x264_macroblock_cache_ref( h, x, y, 2, 2, 1, h->mb.cache.direct_ref[1][idx] );
517 for( l = 0; l < 2; l++ )
519 *(uint64_t*)h->mb.cache.mv[l][x264_scan8[idx*4]] =
520 *(uint64_t*)h->mb.cache.direct_mv[l][x264_scan8[idx*4]];
521 *(uint64_t*)h->mb.cache.mv[l][x264_scan8[idx*4]+8] =
522 *(uint64_t*)h->mb.cache.direct_mv[l][x264_scan8[idx*4]+8];
526 /* This just improves encoder performance, it's not part of the spec */
527 void x264_mb_predict_mv_ref16x16( x264_t *h, int i_list, int i_ref, int mvc[5][2], int *i_mvc )
529 int16_t (*mvr)[2] = h->mb.mvr[i_list][i_ref];
533 if( h->sh.i_type == SLICE_TYPE_B )
535 if( h->mb.cache.ref[i_list][x264_scan8[12]] == i_ref )
537 int16_t *mvp = h->mb.cache.mv[i_list][x264_scan8[12]];
545 if( h->mb.i_mb_x > 0 )
547 int i_mb_l = h->mb.i_mb_xy - 1;
548 mvc[i][0] = mvr[i_mb_l][0];
549 mvc[i][1] = mvr[i_mb_l][1];
552 if( h->mb.i_mb_y > 0 )
554 int i_mb_t = h->mb.i_mb_xy - h->mb.i_mb_stride;
555 mvc[i][0] = mvr[i_mb_t][0];
556 mvc[i][1] = mvr[i_mb_t][1];
559 if( h->mb.i_mb_x > 0 )
561 mvc[i][0] = mvr[i_mb_t - 1][0];
562 mvc[i][1] = mvr[i_mb_t - 1][1];
565 if( h->mb.i_mb_x < h->mb.i_mb_stride - 1 )
567 mvc[i][0] = mvr[i_mb_t + 1][0];
568 mvc[i][1] = mvr[i_mb_t + 1][1];
575 static inline void x264_mb_mc_0xywh( x264_t *h, int x, int y, int width, int height )
577 const int i8 = x264_scan8[0]+x+8*y;
578 const int i_ref = h->mb.cache.ref[0][i8];
579 const int mvx = h->mb.cache.mv[0][i8][0];
580 const int mvy = h->mb.cache.mv[0][i8][1];
582 h->mc[MC_LUMA]( &h->mb.pic.p_fref[0][i_ref][0][4*y * h->mb.pic.i_stride[0]+4*x], h->mb.pic.i_stride[0],
583 &h->mb.pic.p_fdec[0][4*y * h->mb.pic.i_stride[0]+4*x], h->mb.pic.i_stride[0],
584 mvx, mvy, 4*width, 4*height );
586 h->mc[MC_CHROMA]( &h->mb.pic.p_fref[0][i_ref][1][2*y*h->mb.pic.i_stride[1]+2*x], h->mb.pic.i_stride[1],
587 &h->mb.pic.p_fdec[1][2*y*h->mb.pic.i_stride[1]+2*x], h->mb.pic.i_stride[1],
588 mvx, mvy, 2*width, 2*height );
590 h->mc[MC_CHROMA]( &h->mb.pic.p_fref[0][i_ref][2][2*y*h->mb.pic.i_stride[2]+2*x], h->mb.pic.i_stride[2],
591 &h->mb.pic.p_fdec[2][2*y*h->mb.pic.i_stride[2]+2*x], h->mb.pic.i_stride[2],
592 mvx, mvy, 2*width, 2*height );
594 static inline void x264_mb_mc_1xywh( x264_t *h, int x, int y, int width, int height )
596 const int i8 = x264_scan8[0]+x+8*y;
597 const int i_ref = h->mb.cache.ref[1][i8];
598 const int mvx = h->mb.cache.mv[1][i8][0];
599 const int mvy = h->mb.cache.mv[1][i8][1];
601 h->mc[MC_LUMA]( &h->mb.pic.p_fref[1][i_ref][0][4*y * h->mb.pic.i_stride[0]+4*x], h->mb.pic.i_stride[0],
602 &h->mb.pic.p_fdec[0][4*y *h->mb.pic.i_stride[0]+4*x], h->mb.pic.i_stride[0],
603 mvx, mvy, 4*width, 4*height );
605 h->mc[MC_CHROMA]( &h->mb.pic.p_fref[1][i_ref][1][2*y*h->mb.pic.i_stride[1]+2*x], h->mb.pic.i_stride[1],
606 &h->mb.pic.p_fdec[1][2*y*h->mb.pic.i_stride[1]+2*x], h->mb.pic.i_stride[1],
607 mvx, mvy, 2*width, 2*height );
609 h->mc[MC_CHROMA]( &h->mb.pic.p_fref[1][i_ref][2][2*y*h->mb.pic.i_stride[2]+2*x], h->mb.pic.i_stride[2],
610 &h->mb.pic.p_fdec[2][2*y*h->mb.pic.i_stride[2]+2*x], h->mb.pic.i_stride[2],
611 mvx, mvy, 2*width, 2*height );
614 static inline void x264_mb_mc_01xywh( x264_t *h, int x, int y, int width, int height )
616 const int i8 = x264_scan8[0]+x+8*y;
618 const int i_ref0 = h->mb.cache.ref[0][i8];
619 const int mvx0 = h->mb.cache.mv[0][i8][0];
620 const int mvy0 = h->mb.cache.mv[0][i8][1];
622 const int i_ref1 = h->mb.cache.ref[1][i8];
623 const int mvx1 = h->mb.cache.mv[1][i8][0];
624 const int mvy1 = h->mb.cache.mv[1][i8][1];
625 DECLARE_ALIGNED( uint8_t, tmp[16*16], 16 );
628 if( width == 4 && height == 4 ) i_mode = PIXEL_16x16;
629 else if( width == 4 && height == 2 ) i_mode = PIXEL_16x8;
630 else if( width == 2 && height == 4 ) i_mode = PIXEL_8x16;
631 else if( width == 2 && height == 2 ) i_mode = PIXEL_8x8;
632 else if( width == 2 && height == 1 ) i_mode = PIXEL_8x4;
633 else if( width == 1 && height == 2 ) i_mode = PIXEL_4x8;
634 else if( width == 1 && height == 1 ) i_mode = PIXEL_4x4;
636 h->mc[MC_LUMA]( &h->mb.pic.p_fref[0][i_ref0][0][4*y * h->mb.pic.i_stride[0]+4*x], h->mb.pic.i_stride[0],
637 &h->mb.pic.p_fdec[0][4*y *h->mb.pic.i_stride[0]+4*x], h->mb.pic.i_stride[0],
638 mvx0, mvy0, 4*width, 4*height );
639 h->mc[MC_CHROMA]( &h->mb.pic.p_fref[0][i_ref0][1][2*y*h->mb.pic.i_stride[1]+2*x], h->mb.pic.i_stride[1],
640 &h->mb.pic.p_fdec[1][2*y*h->mb.pic.i_stride[1]+2*x], h->mb.pic.i_stride[1],
641 mvx0, mvy0, 2*width, 2*height );
642 h->mc[MC_CHROMA]( &h->mb.pic.p_fref[0][i_ref0][2][2*y*h->mb.pic.i_stride[2]+2*x], h->mb.pic.i_stride[2],
643 &h->mb.pic.p_fdec[2][2*y*h->mb.pic.i_stride[2]+2*x], h->mb.pic.i_stride[2],
644 mvx0, mvy0, 2*width, 2*height );
647 h->mc[MC_LUMA]( &h->mb.pic.p_fref[1][i_ref1][0][4*y * h->mb.pic.i_stride[0]+4*x], h->mb.pic.i_stride[0],
648 tmp, 16, mvx1, mvy1, 4*width, 4*height );
649 h->pixf.avg[i_mode]( &h->mb.pic.p_fdec[0][4*y *h->mb.pic.i_stride[0]+4*x], h->mb.pic.i_stride[0], tmp, 16 );
651 h->mc[MC_CHROMA]( &h->mb.pic.p_fref[1][i_ref1][1][2*y*h->mb.pic.i_stride[1]+2*x], h->mb.pic.i_stride[1],
652 tmp, 16, mvx1, mvy1, 2*width, 2*height );
653 h->pixf.avg[i_mode]( &h->mb.pic.p_fdec[1][2*y*h->mb.pic.i_stride[1]+2*x], h->mb.pic.i_stride[1], tmp, 16 );
655 h->mc[MC_CHROMA]( &h->mb.pic.p_fref[1][i_ref1][2][2*y*h->mb.pic.i_stride[2]+2*x], h->mb.pic.i_stride[2],
656 tmp, 16, mvx1, mvy1, 2*width, 2*height );
657 h->pixf.avg[i_mode]( &h->mb.pic.p_fdec[2][2*y*h->mb.pic.i_stride[2]+2*x], h->mb.pic.i_stride[2], tmp, 16 );
660 static void x264_mb_mc_direct8x8( x264_t *h, int x, int y )
662 const int i8 = x264_scan8[0] + x + 8*y;
664 /* FIXME: optimize based on current block size, not global settings? */
665 if( h->sps->b_direct8x8_inference )
667 if( h->mb.cache.ref[0][i8] >= 0 )
668 if( h->mb.cache.ref[1][i8] >= 0 )
669 x264_mb_mc_01xywh( h, x, y, 2, 2 );
671 x264_mb_mc_0xywh( h, x, y, 2, 2 );
673 x264_mb_mc_1xywh( h, x, y, 2, 2 );
677 if( h->mb.cache.ref[0][i8] >= 0 )
679 if( h->mb.cache.ref[1][i8] >= 0 )
681 x264_mb_mc_01xywh( h, x+0, y+0, 1, 1 );
682 x264_mb_mc_01xywh( h, x+1, y+0, 1, 1 );
683 x264_mb_mc_01xywh( h, x+0, y+1, 1, 1 );
684 x264_mb_mc_01xywh( h, x+1, y+1, 1, 1 );
688 x264_mb_mc_0xywh( h, x+0, y+0, 1, 1 );
689 x264_mb_mc_0xywh( h, x+1, y+0, 1, 1 );
690 x264_mb_mc_0xywh( h, x+0, y+1, 1, 1 );
691 x264_mb_mc_0xywh( h, x+1, y+1, 1, 1 );
696 x264_mb_mc_1xywh( h, x+0, y+0, 1, 1 );
697 x264_mb_mc_1xywh( h, x+1, y+0, 1, 1 );
698 x264_mb_mc_1xywh( h, x+0, y+1, 1, 1 );
699 x264_mb_mc_1xywh( h, x+1, y+1, 1, 1 );
704 void x264_mb_mc( x264_t *h )
706 if( h->mb.i_type == P_L0 )
708 if( h->mb.i_partition == D_16x16 )
710 x264_mb_mc_0xywh( h, 0, 0, 4, 4 );
712 else if( h->mb.i_partition == D_16x8 )
714 x264_mb_mc_0xywh( h, 0, 0, 4, 2 );
715 x264_mb_mc_0xywh( h, 0, 2, 4, 2 );
717 else if( h->mb.i_partition == D_8x16 )
719 x264_mb_mc_0xywh( h, 0, 0, 2, 4 );
720 x264_mb_mc_0xywh( h, 2, 0, 2, 4 );
723 else if( h->mb.i_type == P_8x8 || h->mb.i_type == B_8x8 )
726 for( i = 0; i < 4; i++ )
728 const int x = 2*(i%2);
729 const int y = 2*(i/2);
730 switch( h->mb.i_sub_partition[i] )
733 x264_mb_mc_0xywh( h, x, y, 2, 2 );
736 x264_mb_mc_0xywh( h, x, y+0, 2, 1 );
737 x264_mb_mc_0xywh( h, x, y+1, 2, 1 );
740 x264_mb_mc_0xywh( h, x+0, y, 1, 2 );
741 x264_mb_mc_0xywh( h, x+1, y, 1, 2 );
744 x264_mb_mc_0xywh( h, x+0, y+0, 1, 1 );
745 x264_mb_mc_0xywh( h, x+1, y+0, 1, 1 );
746 x264_mb_mc_0xywh( h, x+0, y+1, 1, 1 );
747 x264_mb_mc_0xywh( h, x+1, y+1, 1, 1 );
750 x264_mb_mc_1xywh( h, x, y, 2, 2 );
753 x264_mb_mc_1xywh( h, x, y+0, 2, 1 );
754 x264_mb_mc_1xywh( h, x, y+1, 2, 1 );
757 x264_mb_mc_1xywh( h, x+0, y, 1, 2 );
758 x264_mb_mc_1xywh( h, x+1, y, 1, 2 );
761 x264_mb_mc_1xywh( h, x+0, y+0, 1, 1 );
762 x264_mb_mc_1xywh( h, x+1, y+0, 1, 1 );
763 x264_mb_mc_1xywh( h, x+0, y+1, 1, 1 );
764 x264_mb_mc_1xywh( h, x+1, y+1, 1, 1 );
767 x264_mb_mc_01xywh( h, x, y, 2, 2 );
770 x264_mb_mc_01xywh( h, x, y+0, 2, 1 );
771 x264_mb_mc_01xywh( h, x, y+1, 2, 1 );
774 x264_mb_mc_01xywh( h, x+0, y, 1, 2 );
775 x264_mb_mc_01xywh( h, x+1, y, 1, 2 );
778 x264_mb_mc_01xywh( h, x+0, y+0, 1, 1 );
779 x264_mb_mc_01xywh( h, x+1, y+0, 1, 1 );
780 x264_mb_mc_01xywh( h, x+0, y+1, 1, 1 );
781 x264_mb_mc_01xywh( h, x+1, y+1, 1, 1 );
784 x264_mb_mc_direct8x8( h, x, y );
789 else if( h->mb.i_type == B_SKIP || h->mb.i_type == B_DIRECT )
792 for( i = 0; i < 4; i++ )
794 const int x = 2*(i%2);
795 const int y = 2*(i/2);
796 x264_mb_mc_direct8x8( h, x, y );
806 /* init ref list utilisations */
807 for( i = 0; i < 2; i++ )
809 b_list0[i] = x264_mb_type_list0_table[h->mb.i_type][i];
810 b_list1[i] = x264_mb_type_list1_table[h->mb.i_type][i];
812 if( h->mb.i_partition == D_16x16 )
814 if( b_list0[0] && b_list1[0] ) x264_mb_mc_01xywh( h, 0, 0, 4, 4 );
815 else if( b_list0[0] ) x264_mb_mc_0xywh ( h, 0, 0, 4, 4 );
816 else if( b_list1[0] ) x264_mb_mc_1xywh ( h, 0, 0, 4, 4 );
818 else if( h->mb.i_partition == D_16x8 )
820 if( b_list0[0] && b_list1[0] ) x264_mb_mc_01xywh( h, 0, 0, 4, 2 );
821 else if( b_list0[0] ) x264_mb_mc_0xywh ( h, 0, 0, 4, 2 );
822 else if( b_list1[0] ) x264_mb_mc_1xywh ( h, 0, 0, 4, 2 );
824 if( b_list0[1] && b_list1[1] ) x264_mb_mc_01xywh( h, 0, 2, 4, 2 );
825 else if( b_list0[1] ) x264_mb_mc_0xywh ( h, 0, 2, 4, 2 );
826 else if( b_list1[1] ) x264_mb_mc_1xywh ( h, 0, 2, 4, 2 );
828 else if( h->mb.i_partition == D_8x16 )
830 if( b_list0[0] && b_list1[0] ) x264_mb_mc_01xywh( h, 0, 0, 2, 4 );
831 else if( b_list0[0] ) x264_mb_mc_0xywh ( h, 0, 0, 2, 4 );
832 else if( b_list1[0] ) x264_mb_mc_1xywh ( h, 0, 0, 2, 4 );
834 if( b_list0[1] && b_list1[1] ) x264_mb_mc_01xywh( h, 2, 0, 2, 4 );
835 else if( b_list0[1] ) x264_mb_mc_0xywh ( h, 2, 0, 2, 4 );
836 else if( b_list1[1] ) x264_mb_mc_1xywh ( h, 2, 0, 2, 4 );
841 void x264_macroblock_cache_init( x264_t *h )
844 int i_mb_count = h->mb.i_mb_count;
846 h->mb.i_mb_stride = h->sps->i_mb_width;
847 h->mb.i_b8_stride = h->sps->i_mb_width * 2;
848 h->mb.i_b4_stride = h->sps->i_mb_width * 4;
850 h->mb.type= x264_malloc( i_mb_count * sizeof( int8_t) );
851 h->mb.qp = x264_malloc( i_mb_count * sizeof( int8_t) );
852 h->mb.cbp = x264_malloc( i_mb_count * sizeof( int16_t) );
853 h->mb.skipbp = x264_malloc( i_mb_count * sizeof( int8_t) );
855 /* 0 -> 3 top(4), 4 -> 6 : left(3) */
856 h->mb.intra4x4_pred_mode = x264_malloc( i_mb_count * 7 * sizeof( int8_t ) );
859 h->mb.non_zero_count = x264_malloc( i_mb_count * 24 * sizeof( uint8_t ) );
861 if( h->param.b_cabac )
863 h->mb.chroma_pred_mode = x264_malloc( i_mb_count * sizeof( int8_t) );
864 h->mb.mvd[0] = x264_malloc( 2*16 * i_mb_count * sizeof( int16_t ) );
865 h->mb.mvd[1] = x264_malloc( 2*16 * i_mb_count * sizeof( int16_t ) );
869 for( j=0; j < ( i ? 1 : h->param.i_frame_reference ); j++ )
870 h->mb.mvr[i][j] = x264_malloc( 2 * i_mb_count * sizeof( int16_t ) );
872 /* init with not avaiable (for top right idx=7,15) */
873 memset( h->mb.cache.ref[0], -2, X264_SCAN8_SIZE * sizeof( int8_t ) );
874 memset( h->mb.cache.ref[1], -2, X264_SCAN8_SIZE * sizeof( int8_t ) );
876 void x264_macroblock_cache_end( x264_t *h )
880 for( j=0; j < ( i ? 1 : h->param.i_frame_reference ); j++ )
881 x264_free( h->mb.mvr[i][j] );
882 if( h->param.b_cabac )
884 x264_free( h->mb.chroma_pred_mode );
885 x264_free( h->mb.mvd[0] );
886 x264_free( h->mb.mvd[1] );
888 x264_free( h->mb.intra4x4_pred_mode );
889 x264_free( h->mb.non_zero_count );
890 x264_free( h->mb.skipbp );
891 x264_free( h->mb.cbp );
892 x264_free( h->mb.qp );
893 x264_free( h->mb.type );
895 void x264_macroblock_slice_init( x264_t *h )
899 h->mb.mv[0] = h->fdec->mv[0];
900 h->mb.mv[1] = h->fdec->mv[1];
901 h->mb.ref[0] = h->fdec->ref[0];
902 h->mb.ref[1] = h->fdec->ref[1];
904 h->fdec->i_ref[0] = h->i_ref0;
905 h->fdec->i_ref[1] = h->i_ref1;
906 for( i = 0; i < h->i_ref0; i++ )
907 h->fdec->ref_poc[0][i] = h->fref0[i]->i_poc;
908 if( h->sh.i_type == SLICE_TYPE_B )
910 for( i = 0; i < h->i_ref1; i++ )
911 h->fdec->ref_poc[1][i] = h->fref1[i]->i_poc;
916 void x264_macroblock_cache_load( x264_t *h, int i_mb_x, int i_mb_y )
918 const int i_mb_4x4 = 4*(i_mb_y * h->mb.i_b4_stride + i_mb_x);
919 const int i_mb_8x8 = 2*(i_mb_y * h->mb.i_b8_stride + i_mb_x);
923 int i_top_type = -1; /* gcc warn */
929 h->mb.i_mb_x = i_mb_x;
930 h->mb.i_mb_y = i_mb_y;
931 h->mb.i_mb_xy = i_mb_y * h->mb.i_mb_stride + i_mb_x;
932 h->mb.i_b8_xy = i_mb_8x8;
933 h->mb.i_b4_xy = i_mb_4x4;
934 h->mb.i_neighbour = 0;
936 /* load picture pointers */
937 for( i = 0; i < 3; i++ )
939 const int w = (i == 0 ? 16 : 8);
940 const int i_stride = h->fdec->i_stride[i];
943 h->mb.pic.i_stride[i] = i_stride;
945 h->mb.pic.p_fenc[i] = &h->fenc->plane[i][ w * ( i_mb_x + i_mb_y * i_stride )];
947 h->mb.pic.p_fdec[i] = &h->fdec->plane[i][ w * ( i_mb_x + i_mb_y * i_stride )];
949 for( j = 0; j < h->i_ref0; j++ )
951 h->mb.pic.p_fref[0][j][i] = &h->fref0[j]->plane[i][ w * ( i_mb_x + i_mb_y * i_stride )];
953 for( j = 0; j < h->i_ref1; j++ )
955 h->mb.pic.p_fref[1][j][i] = &h->fref1[j]->plane[i][ w * ( i_mb_x + i_mb_y * i_stride )];
962 i_top_xy = h->mb.i_mb_xy - h->mb.i_mb_stride;
963 i_top_type= h->mb.type[i_top_xy];
965 h->mb.i_neighbour |= MB_TOP;
968 h->mb.cache.intra4x4_pred_mode[x264_scan8[0] - 8] = h->mb.intra4x4_pred_mode[i_top_xy][0];
969 h->mb.cache.intra4x4_pred_mode[x264_scan8[1] - 8] = h->mb.intra4x4_pred_mode[i_top_xy][1];
970 h->mb.cache.intra4x4_pred_mode[x264_scan8[4] - 8] = h->mb.intra4x4_pred_mode[i_top_xy][2];
971 h->mb.cache.intra4x4_pred_mode[x264_scan8[5] - 8] = h->mb.intra4x4_pred_mode[i_top_xy][3];
973 /* load non_zero_count */
974 h->mb.cache.non_zero_count[x264_scan8[0] - 8] = h->mb.non_zero_count[i_top_xy][10];
975 h->mb.cache.non_zero_count[x264_scan8[1] - 8] = h->mb.non_zero_count[i_top_xy][11];
976 h->mb.cache.non_zero_count[x264_scan8[4] - 8] = h->mb.non_zero_count[i_top_xy][14];
977 h->mb.cache.non_zero_count[x264_scan8[5] - 8] = h->mb.non_zero_count[i_top_xy][15];
979 h->mb.cache.non_zero_count[x264_scan8[16+0] - 8] = h->mb.non_zero_count[i_top_xy][16+2];
980 h->mb.cache.non_zero_count[x264_scan8[16+1] - 8] = h->mb.non_zero_count[i_top_xy][16+3];
982 h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 8] = h->mb.non_zero_count[i_top_xy][16+4+2];
983 h->mb.cache.non_zero_count[x264_scan8[16+4+1] - 8] = h->mb.non_zero_count[i_top_xy][16+4+3];
988 h->mb.cache.intra4x4_pred_mode[x264_scan8[0] - 8] =
989 h->mb.cache.intra4x4_pred_mode[x264_scan8[1] - 8] =
990 h->mb.cache.intra4x4_pred_mode[x264_scan8[4] - 8] =
991 h->mb.cache.intra4x4_pred_mode[x264_scan8[5] - 8] = -1;
993 /* load non_zero_count */
994 h->mb.cache.non_zero_count[x264_scan8[0] - 8] =
995 h->mb.cache.non_zero_count[x264_scan8[1] - 8] =
996 h->mb.cache.non_zero_count[x264_scan8[4] - 8] =
997 h->mb.cache.non_zero_count[x264_scan8[5] - 8] =
998 h->mb.cache.non_zero_count[x264_scan8[16+0] - 8] =
999 h->mb.cache.non_zero_count[x264_scan8[16+1] - 8] =
1000 h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 8] =
1001 h->mb.cache.non_zero_count[x264_scan8[16+4+1] - 8] = 0x80;
1007 i_left_xy = h->mb.i_mb_xy - 1;
1008 i_left_type= h->mb.type[i_left_xy];
1010 h->mb.i_neighbour |= MB_LEFT;
1013 h->mb.cache.intra4x4_pred_mode[x264_scan8[0 ] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][4];
1014 h->mb.cache.intra4x4_pred_mode[x264_scan8[2 ] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][5];
1015 h->mb.cache.intra4x4_pred_mode[x264_scan8[8 ] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][6];
1016 h->mb.cache.intra4x4_pred_mode[x264_scan8[10] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][3];
1018 /* load non_zero_count */
1019 h->mb.cache.non_zero_count[x264_scan8[0 ] - 1] = h->mb.non_zero_count[i_left_xy][5];
1020 h->mb.cache.non_zero_count[x264_scan8[2 ] - 1] = h->mb.non_zero_count[i_left_xy][7];
1021 h->mb.cache.non_zero_count[x264_scan8[8 ] - 1] = h->mb.non_zero_count[i_left_xy][13];
1022 h->mb.cache.non_zero_count[x264_scan8[10] - 1] = h->mb.non_zero_count[i_left_xy][15];
1024 h->mb.cache.non_zero_count[x264_scan8[16+0] - 1] = h->mb.non_zero_count[i_left_xy][16+1];
1025 h->mb.cache.non_zero_count[x264_scan8[16+2] - 1] = h->mb.non_zero_count[i_left_xy][16+3];
1027 h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 1] = h->mb.non_zero_count[i_left_xy][16+4+1];
1028 h->mb.cache.non_zero_count[x264_scan8[16+4+2] - 1] = h->mb.non_zero_count[i_left_xy][16+4+3];
1032 h->mb.cache.intra4x4_pred_mode[x264_scan8[0 ] - 1] =
1033 h->mb.cache.intra4x4_pred_mode[x264_scan8[2 ] - 1] =
1034 h->mb.cache.intra4x4_pred_mode[x264_scan8[8 ] - 1] =
1035 h->mb.cache.intra4x4_pred_mode[x264_scan8[10] - 1] = -1;
1037 /* load non_zero_count */
1038 h->mb.cache.non_zero_count[x264_scan8[0 ] - 1] =
1039 h->mb.cache.non_zero_count[x264_scan8[2 ] - 1] =
1040 h->mb.cache.non_zero_count[x264_scan8[8 ] - 1] =
1041 h->mb.cache.non_zero_count[x264_scan8[10] - 1] =
1042 h->mb.cache.non_zero_count[x264_scan8[16+0] - 1] =
1043 h->mb.cache.non_zero_count[x264_scan8[16+2] - 1] =
1044 h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 1] =
1045 h->mb.cache.non_zero_count[x264_scan8[16+4+2] - 1] = 0x80;
1048 if( i_mb_y > 0 && i_mb_x < h->sps->i_mb_width - 1 )
1050 h->mb.i_neighbour |= MB_TOPRIGHT;
1053 /* load ref/mv/mvd */
1054 if( h->sh.i_type != SLICE_TYPE_I )
1056 const int s8x8 = h->mb.i_b8_stride;
1057 const int s4x4 = h->mb.i_b4_stride;
1059 int i_top_left_xy = -1;
1060 int i_top_right_xy = -1;
1064 if( h->mb.i_mb_y > 0 && h->mb.i_mb_x > 0 )
1066 i_top_left_xy = i_top_xy - 1;
1068 if( h->mb.i_mb_y > 0 && h->mb.i_mb_x < h->sps->i_mb_width - 1 )
1070 i_top_right_xy = i_top_xy + 1;
1073 for( i_list = 0; i_list < (h->sh.i_type == SLICE_TYPE_B ? 2 : 1 ); i_list++ )
1076 h->mb.cache.ref[i_list][x264_scan8[5 ]+1] =
1077 h->mb.cache.ref[i_list][x264_scan8[7 ]+1] =
1078 h->mb.cache.ref[i_list][x264_scan8[13]+1] = -2;
1081 if( i_top_left_xy >= 0 )
1083 const int i8 = x264_scan8[0] - 1 - 1*8;
1084 const int ir = i_mb_8x8 - s8x8 - 1;
1085 const int iv = i_mb_4x4 - s4x4 - 1;
1086 h->mb.cache.ref[i_list][i8] = h->mb.ref[i_list][ir];
1087 h->mb.cache.mv[i_list][i8][0] = h->mb.mv[i_list][iv][0];
1088 h->mb.cache.mv[i_list][i8][1] = h->mb.mv[i_list][iv][1];
1092 const int i8 = x264_scan8[0] - 1 - 1*8;
1093 h->mb.cache.ref[i_list][i8] = -2;
1094 h->mb.cache.mv[i_list][i8][0] = 0;
1095 h->mb.cache.mv[i_list][i8][1] = 0;
1100 const int i8 = x264_scan8[0] - 8;
1101 const int ir = i_mb_8x8 - s8x8;
1102 const int iv = i_mb_4x4 - s4x4;
1104 h->mb.cache.ref[i_list][i8+0] =
1105 h->mb.cache.ref[i_list][i8+1] = h->mb.ref[i_list][ir + 0];
1106 h->mb.cache.ref[i_list][i8+2] =
1107 h->mb.cache.ref[i_list][i8+3] = h->mb.ref[i_list][ir + 1];
1109 for( i = 0; i < 4; i++ )
1111 h->mb.cache.mv[i_list][i8+i][0] = h->mb.mv[i_list][iv + i][0];
1112 h->mb.cache.mv[i_list][i8+i][1] = h->mb.mv[i_list][iv + i][1];
1117 const int i8 = x264_scan8[0] - 8;
1118 for( i = 0; i < 4; i++ )
1120 h->mb.cache.ref[i_list][i8+i] = -2;
1121 h->mb.cache.mv[i_list][i8+i][0] =
1122 h->mb.cache.mv[i_list][i8+i][1] = 0;
1126 if( i_top_right_xy >= 0 )
1128 const int i8 = x264_scan8[0] + 4 - 1*8;
1129 const int ir = i_mb_8x8 - s8x8 + 2;
1130 const int iv = i_mb_4x4 - s4x4 + 4;
1132 h->mb.cache.ref[i_list][i8] = h->mb.ref[i_list][ir];
1133 h->mb.cache.mv[i_list][i8][0] = h->mb.mv[i_list][iv][0];
1134 h->mb.cache.mv[i_list][i8][1] = h->mb.mv[i_list][iv][1];
1138 const int i8 = x264_scan8[0] + 4 - 1*8;
1139 h->mb.cache.ref[i_list][i8] = -2;
1140 h->mb.cache.mv[i_list][i8][0] = 0;
1141 h->mb.cache.mv[i_list][i8][1] = 0;
1144 if( i_left_xy >= 0 )
1146 const int i8 = x264_scan8[0] - 1;
1147 const int ir = i_mb_8x8 - 1;
1148 const int iv = i_mb_4x4 - 1;
1150 h->mb.cache.ref[i_list][i8+0*8] =
1151 h->mb.cache.ref[i_list][i8+1*8] = h->mb.ref[i_list][ir + 0*s8x8];
1152 h->mb.cache.ref[i_list][i8+2*8] =
1153 h->mb.cache.ref[i_list][i8+3*8] = h->mb.ref[i_list][ir + 1*s8x8];
1155 for( i = 0; i < 4; i++ )
1157 h->mb.cache.mv[i_list][i8+i*8][0] = h->mb.mv[i_list][iv + i*s4x4][0];
1158 h->mb.cache.mv[i_list][i8+i*8][1] = h->mb.mv[i_list][iv + i*s4x4][1];
1163 const int i8 = x264_scan8[0] - 1;
1164 for( i = 0; i < 4; i++ )
1166 h->mb.cache.ref[i_list][i8+i*8] = -2;
1167 h->mb.cache.mv[i_list][i8+i*8][0] =
1168 h->mb.cache.mv[i_list][i8+i*8][1] = 0;
1172 if( h->param.b_cabac )
1176 const int i8 = x264_scan8[0] - 8;
1177 const int iv = i_mb_4x4 - s4x4;
1178 for( i = 0; i < 4; i++ )
1180 h->mb.cache.mvd[i_list][i8+i][0] = h->mb.mvd[i_list][iv + i][0];
1181 h->mb.cache.mvd[i_list][i8+i][1] = h->mb.mvd[i_list][iv + i][1];
1186 const int i8 = x264_scan8[0] - 8;
1187 for( i = 0; i < 4; i++ )
1189 h->mb.cache.mvd[i_list][i8+i][0] =
1190 h->mb.cache.mvd[i_list][i8+i][1] = 0;
1194 if( i_left_xy >= 0 )
1196 const int i8 = x264_scan8[0] - 1;
1197 const int iv = i_mb_4x4 - 1;
1198 for( i = 0; i < 4; i++ )
1200 h->mb.cache.mvd[i_list][i8+i*8][0] = h->mb.mvd[i_list][iv + i*s4x4][0];
1201 h->mb.cache.mvd[i_list][i8+i*8][1] = h->mb.mvd[i_list][iv + i*s4x4][1];
1206 const int i8 = x264_scan8[0] - 1;
1207 for( i = 0; i < 4; i++ )
1209 h->mb.cache.mvd[i_list][i8+i*8][0] =
1210 h->mb.cache.mvd[i_list][i8+i*8][1] = 0;
1217 if( h->param.b_cabac )
1219 if( h->sh.i_type == SLICE_TYPE_B )
1221 memset( h->mb.cache.skip, 0, X264_SCAN8_SIZE * sizeof( int8_t ) );
1222 if( i_left_xy >= 0 )
1224 h->mb.cache.skip[x264_scan8[0] - 1] = h->mb.skipbp[i_left_xy] & 0x2;
1225 h->mb.cache.skip[x264_scan8[8] - 1] = h->mb.skipbp[i_left_xy] & 0x8;
1229 h->mb.cache.skip[x264_scan8[0] - 8] = h->mb.skipbp[i_top_xy] & 0x4;
1230 h->mb.cache.skip[x264_scan8[4] - 8] = h->mb.skipbp[i_top_xy] & 0x8;
1233 else if( h->mb.i_mb_xy == 0 && h->sh.i_type == SLICE_TYPE_P )
1235 memset( h->mb.cache.skip, 0, X264_SCAN8_SIZE * sizeof( int8_t ) );
1241 void x264_macroblock_cache_save( x264_t *h )
1243 const int i_mb_xy = h->mb.i_mb_xy;
1244 const int i_mb_type = h->mb.i_type;
1245 const int s8x8 = h->mb.i_b8_stride;
1246 const int s4x4 = h->mb.i_b4_stride;
1247 const int i_mb_4x4 = h->mb.i_b4_xy;
1248 const int i_mb_8x8 = h->mb.i_b8_xy;
1252 if( IS_SKIP( h->mb.i_type ) )
1253 h->mb.qp[i_mb_xy] = h->mb.i_last_qp;
1255 h->mb.i_last_dqp = h->mb.qp[i_mb_xy] - h->mb.i_last_qp;
1256 h->mb.i_last_qp = h->mb.qp[i_mb_xy];
1259 if( i_mb_type == I_4x4 )
1261 h->mb.intra4x4_pred_mode[i_mb_xy][0] = h->mb.cache.intra4x4_pred_mode[x264_scan8[10] ];
1262 h->mb.intra4x4_pred_mode[i_mb_xy][1] = h->mb.cache.intra4x4_pred_mode[x264_scan8[11] ];
1263 h->mb.intra4x4_pred_mode[i_mb_xy][2] = h->mb.cache.intra4x4_pred_mode[x264_scan8[14] ];
1264 h->mb.intra4x4_pred_mode[i_mb_xy][3] = h->mb.cache.intra4x4_pred_mode[x264_scan8[15] ];
1265 h->mb.intra4x4_pred_mode[i_mb_xy][4] = h->mb.cache.intra4x4_pred_mode[x264_scan8[5] ];
1266 h->mb.intra4x4_pred_mode[i_mb_xy][5] = h->mb.cache.intra4x4_pred_mode[x264_scan8[7] ];
1267 h->mb.intra4x4_pred_mode[i_mb_xy][6] = h->mb.cache.intra4x4_pred_mode[x264_scan8[13] ];
1271 h->mb.intra4x4_pred_mode[i_mb_xy][0] =
1272 h->mb.intra4x4_pred_mode[i_mb_xy][1] =
1273 h->mb.intra4x4_pred_mode[i_mb_xy][2] =
1274 h->mb.intra4x4_pred_mode[i_mb_xy][3] =
1275 h->mb.intra4x4_pred_mode[i_mb_xy][4] =
1276 h->mb.intra4x4_pred_mode[i_mb_xy][5] =
1277 h->mb.intra4x4_pred_mode[i_mb_xy][6] = I_PRED_4x4_DC;
1280 if( i_mb_type == I_PCM )
1282 h->mb.cbp[i_mb_xy] = 0x72f; /* all set */
1283 for( i = 0; i < 16 + 2*4; i++ )
1285 h->mb.non_zero_count[i_mb_xy][i] = 16;
1290 /* save non zero count */
1291 for( i = 0; i < 16 + 2*4; i++ )
1293 h->mb.non_zero_count[i_mb_xy][i] = h->mb.cache.non_zero_count[x264_scan8[i]];
1297 if( !IS_INTRA( i_mb_type ) )
1300 for( i_list = 0; i_list < (h->sh.i_type == SLICE_TYPE_B ? 2 : 1 ); i_list++ )
1304 h->mb.ref[i_list][i_mb_8x8+0+0*s8x8] = h->mb.cache.ref[i_list][x264_scan8[0]];
1305 h->mb.ref[i_list][i_mb_8x8+1+0*s8x8] = h->mb.cache.ref[i_list][x264_scan8[4]];
1306 h->mb.ref[i_list][i_mb_8x8+0+1*s8x8] = h->mb.cache.ref[i_list][x264_scan8[8]];
1307 h->mb.ref[i_list][i_mb_8x8+1+1*s8x8] = h->mb.cache.ref[i_list][x264_scan8[12]];
1309 for( y = 0; y < 4; y++ )
1311 for( x = 0; x < 4; x++ )
1313 h->mb.mv[i_list][i_mb_4x4+x+y*s4x4][0] = h->mb.cache.mv[i_list][x264_scan8[0]+x+8*y][0];
1314 h->mb.mv[i_list][i_mb_4x4+x+y*s4x4][1] = h->mb.cache.mv[i_list][x264_scan8[0]+x+8*y][1];
1322 for( i_list = 0; i_list < (h->sh.i_type == SLICE_TYPE_B ? 2 : 1 ); i_list++ )
1326 h->mb.ref[i_list][i_mb_8x8+0+0*s8x8] =
1327 h->mb.ref[i_list][i_mb_8x8+1+0*s8x8] =
1328 h->mb.ref[i_list][i_mb_8x8+0+1*s8x8] =
1329 h->mb.ref[i_list][i_mb_8x8+1+1*s8x8] = -1;
1331 for( y = 0; y < 4; y++ )
1333 for( x = 0; x < 4; x++ )
1335 h->mb.mv[i_list][i_mb_4x4+x+y*s4x4][0] = 0;
1336 h->mb.mv[i_list][i_mb_4x4+x+y*s4x4][1] = 0;
1342 if( h->param.b_cabac )
1344 if( i_mb_type == I_4x4 || i_mb_type == I_16x16 )
1345 h->mb.chroma_pred_mode[i_mb_xy] = h->mb.i_chroma_pred_mode;
1347 h->mb.chroma_pred_mode[i_mb_xy] = I_PRED_CHROMA_DC;
1349 if( !IS_INTRA( i_mb_type ) && !IS_SKIP( i_mb_type ) && !IS_DIRECT( i_mb_type ) )
1352 for( i_list = 0; i_list < 2; i_list++ )
1354 const int s4x4 = 4 * h->mb.i_mb_stride;
1356 for( y = 0; y < 4; y++ )
1358 for( x = 0; x < 4; x++ )
1360 h->mb.mvd[i_list][i_mb_4x4+x+y*s4x4][0] = h->mb.cache.mvd[i_list][x264_scan8[0]+x+8*y][0];
1361 h->mb.mvd[i_list][i_mb_4x4+x+y*s4x4][1] = h->mb.cache.mvd[i_list][x264_scan8[0]+x+8*y][1];
1369 for( i_list = 0; i_list < 2; i_list++ )
1371 const int s4x4 = 4 * h->mb.i_mb_stride;
1373 for( y = 0; y < 4; y++ )
1375 for( x = 0; x < 4; x++ )
1377 h->mb.mvd[i_list][i_mb_4x4+x+y*s4x4][0] = 0;
1378 h->mb.mvd[i_list][i_mb_4x4+x+y*s4x4][1] = 0;
1383 if( h->sh.i_type == SLICE_TYPE_B )
1385 if( i_mb_type == B_SKIP || i_mb_type == B_DIRECT )
1386 h->mb.skipbp[i_mb_xy] = 0xf;
1387 else if( i_mb_type == B_8x8 )
1390 for( i = 0; i < 4; i++ )
1391 skipbp |= ( h->mb.i_sub_partition[i] == D_DIRECT_8x8 ) << i;
1392 h->mb.skipbp[i_mb_xy] = skipbp;
1395 h->mb.skipbp[i_mb_xy] = 0;