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 void x264_mb_predict_mv_ref16x16( x264_t *h, int i_list, int i_ref, int mvc[4][2], int *i_mvc )
354 int16_t (*mvr)[2] = h->mb.mvr[i_list][i_ref];
357 if( h->mb.i_mb_x > 0 )
359 int i_mb_l = h->mb.i_mb_xy - 1;
360 mvc[i][0] = mvr[i_mb_l][0];
361 mvc[i][1] = mvr[i_mb_l][1];
364 if( h->mb.i_mb_y > 0 )
366 int i_mb_t = h->mb.i_mb_xy - h->mb.i_mb_stride;
367 mvc[i][0] = mvr[i_mb_t][0];
368 mvc[i][1] = mvr[i_mb_t][1];
371 if( h->mb.i_mb_x > 0 )
373 mvc[i][0] = mvr[i_mb_t - 1][0];
374 mvc[i][1] = mvr[i_mb_t - 1][1];
377 if( h->mb.i_mb_x < h->mb.i_mb_stride - 1 )
379 mvc[i][0] = mvr[i_mb_t + 1][0];
380 mvc[i][1] = mvr[i_mb_t + 1][1];
387 static inline void x264_mb_mc_0xywh( x264_t *h, int x, int y, int width, int height )
389 const int i8 = x264_scan8[0]+x+8*y;
390 const int i_ref = h->mb.cache.ref[0][i8];
391 const int mvx = h->mb.cache.mv[0][i8][0];
392 const int mvy = h->mb.cache.mv[0][i8][1];
394 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],
395 &h->mb.pic.p_fdec[0][4*y * h->mb.pic.i_stride[0]+4*x], h->mb.pic.i_stride[0],
396 mvx, mvy, 4*width, 4*height );
398 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],
399 &h->mb.pic.p_fdec[1][2*y*h->mb.pic.i_stride[1]+2*x], h->mb.pic.i_stride[1],
400 mvx, mvy, 2*width, 2*height );
402 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],
403 &h->mb.pic.p_fdec[2][2*y*h->mb.pic.i_stride[2]+2*x], h->mb.pic.i_stride[2],
404 mvx, mvy, 2*width, 2*height );
406 static inline void x264_mb_mc_1xywh( x264_t *h, int x, int y, int width, int height )
408 const int i8 = x264_scan8[0]+x+8*y;
409 const int i_ref = h->mb.cache.ref[1][i8];
410 const int mvx = h->mb.cache.mv[1][i8][0];
411 const int mvy = h->mb.cache.mv[1][i8][1];
413 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],
414 &h->mb.pic.p_fdec[0][4*y *h->mb.pic.i_stride[0]+4*x], h->mb.pic.i_stride[0],
415 mvx, mvy, 4*width, 4*height );
417 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],
418 &h->mb.pic.p_fdec[1][2*y*h->mb.pic.i_stride[1]+2*x], h->mb.pic.i_stride[1],
419 mvx, mvy, 2*width, 2*height );
421 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],
422 &h->mb.pic.p_fdec[2][2*y*h->mb.pic.i_stride[2]+2*x], h->mb.pic.i_stride[2],
423 mvx, mvy, 2*width, 2*height );
426 static inline void x264_mb_mc_01xywh( x264_t *h, int x, int y, int width, int height )
428 const int i8 = x264_scan8[0]+x+8*y;
430 const int i_ref0 = h->mb.cache.ref[0][i8];
431 const int mvx0 = h->mb.cache.mv[0][i8][0];
432 const int mvy0 = h->mb.cache.mv[0][i8][1];
434 const int i_ref1 = h->mb.cache.ref[1][i8];
435 const int mvx1 = h->mb.cache.mv[1][i8][0];
436 const int mvy1 = h->mb.cache.mv[1][i8][1];
437 DECLARE_ALIGNED( uint8_t, tmp[16*16], 16 );
440 if( width == 4 && height == 4 ) i_mode = PIXEL_16x16;
441 else if( width == 4 && height == 2 ) i_mode = PIXEL_16x8;
442 else if( width == 2 && height == 4 ) i_mode = PIXEL_8x16;
443 else if( width == 2 && height == 2 ) i_mode = PIXEL_8x8;
444 else if( width == 2 && height == 1 ) i_mode = PIXEL_8x4;
445 else if( width == 1 && height == 2 ) i_mode = PIXEL_4x8;
446 else if( width == 1 && height == 1 ) i_mode = PIXEL_4x4;
448 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],
449 &h->mb.pic.p_fdec[0][4*y *h->mb.pic.i_stride[0]+4*x], h->mb.pic.i_stride[0],
450 mvx0, mvy0, 4*width, 4*height );
451 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],
452 &h->mb.pic.p_fdec[1][2*y*h->mb.pic.i_stride[1]+2*x], h->mb.pic.i_stride[1],
453 mvx0, mvy0, 2*width, 2*height );
454 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],
455 &h->mb.pic.p_fdec[2][2*y*h->mb.pic.i_stride[2]+2*x], h->mb.pic.i_stride[2],
456 mvx0, mvy0, 2*width, 2*height );
459 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],
460 tmp, 16, mvx1, mvy1, 4*width, 4*height );
461 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 );
463 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],
464 tmp, 16, mvx1, mvy1, 2*width, 2*height );
465 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 );
467 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],
468 tmp, 16, mvx1, mvy1, 2*width, 2*height );
469 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 );
473 void x264_mb_mc( x264_t *h )
475 if( h->mb.i_type == P_L0 )
477 if( h->mb.i_partition == D_16x16 )
479 x264_mb_mc_0xywh( h, 0, 0, 4, 4 );
481 else if( h->mb.i_partition == D_16x8 )
483 x264_mb_mc_0xywh( h, 0, 0, 4, 2 );
484 x264_mb_mc_0xywh( h, 0, 2, 4, 2 );
486 else if( h->mb.i_partition == D_8x16 )
488 x264_mb_mc_0xywh( h, 0, 0, 2, 4 );
489 x264_mb_mc_0xywh( h, 2, 0, 2, 4 );
492 else if( h->mb.i_type == P_8x8 )
495 for( i = 0; i < 4; i++ )
497 const int x = 2*(i%2);
498 const int y = 2*(i/2);
499 switch( h->mb.i_sub_partition[i] )
502 x264_mb_mc_0xywh( h, x, y, 2, 2 );
505 x264_mb_mc_0xywh( h, x, y+0, 2, 1 );
506 x264_mb_mc_0xywh( h, x, y+1, 2, 1 );
509 x264_mb_mc_0xywh( h, x+0, y, 1, 2 );
510 x264_mb_mc_0xywh( h, x+1, y, 1, 2 );
513 x264_mb_mc_0xywh( h, x+0, y+0, 1, 1 );
514 x264_mb_mc_0xywh( h, x+1, y+0, 1, 1 );
515 x264_mb_mc_0xywh( h, x+0, y+1, 1, 1 );
516 x264_mb_mc_0xywh( h, x+1, y+1, 1, 1 );
521 else if( h->mb.i_type == B_8x8 || h->mb.i_type == B_DIRECT )
523 x264_log( h, X264_LOG_ERROR, "mc_luma with unsupported mb\n" );
533 /* init ref list utilisations */
534 for( i = 0; i < 2; i++ )
536 b_list0[i] = x264_mb_type_list0_table[h->mb.i_type][i];
537 b_list1[i] = x264_mb_type_list1_table[h->mb.i_type][i];
539 if( h->mb.i_partition == D_16x16 )
541 if( b_list0[0] && b_list1[0] ) x264_mb_mc_01xywh( h, 0, 0, 4, 4 );
542 else if( b_list0[0] ) x264_mb_mc_0xywh ( h, 0, 0, 4, 4 );
543 else if( b_list1[0] ) x264_mb_mc_1xywh ( h, 0, 0, 4, 4 );
545 else if( h->mb.i_partition == D_16x8 )
547 if( b_list0[0] && b_list1[0] ) x264_mb_mc_01xywh( h, 0, 0, 4, 2 );
548 else if( b_list0[0] ) x264_mb_mc_0xywh ( h, 0, 0, 4, 2 );
549 else if( b_list1[0] ) x264_mb_mc_1xywh ( h, 0, 0, 4, 2 );
551 if( b_list0[1] && b_list1[1] ) x264_mb_mc_01xywh( h, 0, 2, 4, 2 );
552 else if( b_list0[1] ) x264_mb_mc_0xywh ( h, 0, 2, 4, 2 );
553 else if( b_list1[1] ) x264_mb_mc_1xywh ( h, 0, 2, 4, 2 );
555 else if( h->mb.i_partition == D_8x16 )
557 if( b_list0[0] && b_list1[0] ) x264_mb_mc_01xywh( h, 0, 0, 2, 4 );
558 else if( b_list0[0] ) x264_mb_mc_0xywh ( h, 0, 0, 2, 4 );
559 else if( b_list1[0] ) x264_mb_mc_1xywh ( h, 0, 0, 2, 4 );
561 if( b_list0[1] && b_list1[1] ) x264_mb_mc_01xywh( h, 2, 0, 2, 4 );
562 else if( b_list0[1] ) x264_mb_mc_0xywh ( h, 2, 0, 2, 4 );
563 else if( b_list1[1] ) x264_mb_mc_1xywh ( h, 2, 0, 2, 4 );
568 void x264_macroblock_cache_init( x264_t *h )
571 int i_mb_count = h->sps->i_mb_width * h->sps->i_mb_height;
573 h->mb.i_mb_stride = h->sps->i_mb_width;
575 h->mb.type= x264_malloc( i_mb_count * sizeof( int8_t) );
576 h->mb.qp = x264_malloc( i_mb_count * sizeof( int8_t) );
577 h->mb.cbp = x264_malloc( i_mb_count * sizeof( int16_t) );
579 /* 0 -> 3 top(4), 4 -> 6 : left(3) */
580 h->mb.intra4x4_pred_mode = x264_malloc( i_mb_count * 7 * sizeof( int8_t ) );
583 h->mb.non_zero_count = x264_malloc( i_mb_count * 24 * sizeof( uint8_t ) );
585 h->mb.mv[0] = x264_malloc( 2*16 * i_mb_count * sizeof( int16_t ) );
586 h->mb.mv[1] = x264_malloc( 2*16 * i_mb_count * sizeof( int16_t ) );
587 h->mb.ref[0] = x264_malloc( 4 * i_mb_count * sizeof( int16_t ) );
588 h->mb.ref[1] = x264_malloc( 4 * i_mb_count * sizeof( int16_t ) );
590 if( h->param.b_cabac )
592 h->mb.chroma_pred_mode = x264_malloc( i_mb_count * sizeof( int8_t) );
593 h->mb.mvd[0] = x264_malloc( 2*16 * i_mb_count * sizeof( int16_t ) );
594 h->mb.mvd[1] = x264_malloc( 2*16 * i_mb_count * sizeof( int16_t ) );
598 for( j=0; j<16; j++ ) /* FIXME: alloc no more than param.i_frame_reference */
599 h->mb.mvr[i][j] = x264_malloc( 2 * i_mb_count * sizeof( int16_t ) );
601 /* init with not avaiable (for top right idx=7,15) */
602 memset( h->mb.cache.ref[0], -2, X264_SCAN8_SIZE * sizeof( int8_t ) );
603 memset( h->mb.cache.ref[1], -2, X264_SCAN8_SIZE * sizeof( int8_t ) );
605 void x264_macroblock_cache_end( x264_t *h )
609 for( j=0; j<16; j++ )
610 x264_free( h->mb.mvr[i][j] );
611 if( h->param.b_cabac )
613 x264_free( h->mb.chroma_pred_mode );
614 x264_free( h->mb.mvd[0] );
615 x264_free( h->mb.mvd[1] );
617 x264_free( h->mb.mv[0] );
618 x264_free( h->mb.mv[1] );
619 x264_free( h->mb.ref[0] );
620 x264_free( h->mb.ref[1] );
621 x264_free( h->mb.intra4x4_pred_mode );
622 x264_free( h->mb.non_zero_count );
623 x264_free( h->mb.cbp );
624 x264_free( h->mb.qp );
625 x264_free( h->mb.type );
629 void x264_macroblock_cache_load( x264_t *h, int i_mb_x, int i_mb_y )
631 const int i_mb_4x4 = 16 * h->mb.i_mb_stride *i_mb_y + 4 * i_mb_x;
632 const int i_mb_8x8 = 4 * h->mb.i_mb_stride *i_mb_y + 2 * i_mb_x;
636 int i_top_type = -1; /* gcc warn */
642 h->mb.i_mb_x = i_mb_x;
643 h->mb.i_mb_y = i_mb_y;
644 h->mb.i_mb_xy = i_mb_y * h->mb.i_mb_stride + i_mb_x;
645 h->mb.i_neighbour = 0;
647 /* load picture pointers */
648 for( i = 0; i < 3; i++ )
650 const int w = (i == 0 ? 16 : 8);
651 const int i_stride = h->fdec->i_stride[i];
654 h->mb.pic.i_stride[i] = i_stride;
656 h->mb.pic.p_fenc[i] = &h->fenc->plane[i][ w * ( i_mb_x + i_mb_y * i_stride )];
658 h->mb.pic.p_fdec[i] = &h->fdec->plane[i][ w * ( i_mb_x + i_mb_y * i_stride )];
660 for( j = 0; j < h->i_ref0; j++ )
662 h->mb.pic.p_fref[0][j][i] = &h->fref0[j]->plane[i][ w * ( i_mb_x + i_mb_y * i_stride )];
664 for( j = 0; j < h->i_ref1; j++ )
666 h->mb.pic.p_fref[1][j][i] = &h->fref1[j]->plane[i][ w * ( i_mb_x + i_mb_y * i_stride )];
673 i_top_xy = h->mb.i_mb_xy - h->mb.i_mb_stride;
674 i_top_type= h->mb.type[i_top_xy];
676 h->mb.i_neighbour |= MB_TOP;
679 h->mb.cache.intra4x4_pred_mode[x264_scan8[0] - 8] = h->mb.intra4x4_pred_mode[i_top_xy][0];
680 h->mb.cache.intra4x4_pred_mode[x264_scan8[1] - 8] = h->mb.intra4x4_pred_mode[i_top_xy][1];
681 h->mb.cache.intra4x4_pred_mode[x264_scan8[4] - 8] = h->mb.intra4x4_pred_mode[i_top_xy][2];
682 h->mb.cache.intra4x4_pred_mode[x264_scan8[5] - 8] = h->mb.intra4x4_pred_mode[i_top_xy][3];
684 /* load non_zero_count */
685 h->mb.cache.non_zero_count[x264_scan8[0] - 8] = h->mb.non_zero_count[i_top_xy][10];
686 h->mb.cache.non_zero_count[x264_scan8[1] - 8] = h->mb.non_zero_count[i_top_xy][11];
687 h->mb.cache.non_zero_count[x264_scan8[4] - 8] = h->mb.non_zero_count[i_top_xy][14];
688 h->mb.cache.non_zero_count[x264_scan8[5] - 8] = h->mb.non_zero_count[i_top_xy][15];
690 h->mb.cache.non_zero_count[x264_scan8[16+0] - 8] = h->mb.non_zero_count[i_top_xy][16+2];
691 h->mb.cache.non_zero_count[x264_scan8[16+1] - 8] = h->mb.non_zero_count[i_top_xy][16+3];
693 h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 8] = h->mb.non_zero_count[i_top_xy][16+4+2];
694 h->mb.cache.non_zero_count[x264_scan8[16+4+1] - 8] = h->mb.non_zero_count[i_top_xy][16+4+3];
699 h->mb.cache.intra4x4_pred_mode[x264_scan8[0] - 8] =
700 h->mb.cache.intra4x4_pred_mode[x264_scan8[1] - 8] =
701 h->mb.cache.intra4x4_pred_mode[x264_scan8[4] - 8] =
702 h->mb.cache.intra4x4_pred_mode[x264_scan8[5] - 8] = -1;
704 /* load non_zero_count */
705 h->mb.cache.non_zero_count[x264_scan8[0] - 8] =
706 h->mb.cache.non_zero_count[x264_scan8[1] - 8] =
707 h->mb.cache.non_zero_count[x264_scan8[4] - 8] =
708 h->mb.cache.non_zero_count[x264_scan8[5] - 8] =
709 h->mb.cache.non_zero_count[x264_scan8[16+0] - 8] =
710 h->mb.cache.non_zero_count[x264_scan8[16+1] - 8] =
711 h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 8] =
712 h->mb.cache.non_zero_count[x264_scan8[16+4+1] - 8] = 0x80;
718 i_left_xy = h->mb.i_mb_xy - 1;
719 i_left_type= h->mb.type[i_left_xy];
721 h->mb.i_neighbour |= MB_LEFT;
724 h->mb.cache.intra4x4_pred_mode[x264_scan8[0 ] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][4];
725 h->mb.cache.intra4x4_pred_mode[x264_scan8[2 ] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][5];
726 h->mb.cache.intra4x4_pred_mode[x264_scan8[8 ] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][6];
727 h->mb.cache.intra4x4_pred_mode[x264_scan8[10] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][3];
729 /* load non_zero_count */
730 h->mb.cache.non_zero_count[x264_scan8[0 ] - 1] = h->mb.non_zero_count[i_left_xy][5];
731 h->mb.cache.non_zero_count[x264_scan8[2 ] - 1] = h->mb.non_zero_count[i_left_xy][7];
732 h->mb.cache.non_zero_count[x264_scan8[8 ] - 1] = h->mb.non_zero_count[i_left_xy][13];
733 h->mb.cache.non_zero_count[x264_scan8[10] - 1] = h->mb.non_zero_count[i_left_xy][15];
735 h->mb.cache.non_zero_count[x264_scan8[16+0] - 1] = h->mb.non_zero_count[i_left_xy][16+1];
736 h->mb.cache.non_zero_count[x264_scan8[16+2] - 1] = h->mb.non_zero_count[i_left_xy][16+3];
738 h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 1] = h->mb.non_zero_count[i_left_xy][16+4+1];
739 h->mb.cache.non_zero_count[x264_scan8[16+4+2] - 1] = h->mb.non_zero_count[i_left_xy][16+4+3];
743 h->mb.cache.intra4x4_pred_mode[x264_scan8[0 ] - 1] =
744 h->mb.cache.intra4x4_pred_mode[x264_scan8[2 ] - 1] =
745 h->mb.cache.intra4x4_pred_mode[x264_scan8[8 ] - 1] =
746 h->mb.cache.intra4x4_pred_mode[x264_scan8[10] - 1] = -1;
748 /* load non_zero_count */
749 h->mb.cache.non_zero_count[x264_scan8[0 ] - 1] =
750 h->mb.cache.non_zero_count[x264_scan8[2 ] - 1] =
751 h->mb.cache.non_zero_count[x264_scan8[8 ] - 1] =
752 h->mb.cache.non_zero_count[x264_scan8[10] - 1] =
753 h->mb.cache.non_zero_count[x264_scan8[16+0] - 1] =
754 h->mb.cache.non_zero_count[x264_scan8[16+2] - 1] =
755 h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 1] =
756 h->mb.cache.non_zero_count[x264_scan8[16+4+2] - 1] = 0x80;
759 if( i_mb_y > 0 && i_mb_x < h->sps->i_mb_width - 1 )
761 h->mb.i_neighbour |= MB_TOPRIGHT;
764 /* load ref/mv/mvd */
765 if( h->sh.i_type != SLICE_TYPE_I )
767 int s8x8 = 2 * h->mb.i_mb_stride;
768 int s4x4 = 4 * h->mb.i_mb_stride;
770 int i_top_left_xy = -1;
771 int i_top_right_xy = -1;
775 if( h->mb.i_mb_y > 0 && h->mb.i_mb_x > 0 )
777 i_top_left_xy = i_top_xy - 1;
779 if( h->mb.i_mb_y > 0 && h->mb.i_mb_x < h->sps->i_mb_width - 1 )
781 i_top_right_xy = i_top_xy + 1;
784 for( i_list = 0; i_list < (h->sh.i_type == SLICE_TYPE_P ? 1 : 2 ); i_list++ )
787 h->mb.cache.ref[i_list][x264_scan8[5 ]+1] =
788 h->mb.cache.ref[i_list][x264_scan8[7 ]+1] =
789 h->mb.cache.ref[i_list][x264_scan8[13]+1] = -2;
792 if( i_top_left_xy >= 0 )
794 const int i8 = x264_scan8[0] - 1 - 1*8;
795 const int ir = i_mb_8x8 - s8x8 - 1;
796 const int iv = i_mb_4x4 - s4x4 - 1;
797 h->mb.cache.ref[i_list][i8] = h->mb.ref[i_list][ir];
798 h->mb.cache.mv[i_list][i8][0] = h->mb.mv[i_list][iv][0];
799 h->mb.cache.mv[i_list][i8][1] = h->mb.mv[i_list][iv][1];
803 const int i8 = x264_scan8[0] - 1 - 1*8;
804 h->mb.cache.ref[i_list][i8] = -2;
805 h->mb.cache.mv[i_list][i8][0] = 0;
806 h->mb.cache.mv[i_list][i8][1] = 0;
811 const int i8 = x264_scan8[0] - 8;
812 const int ir = i_mb_8x8 - s8x8;
813 const int iv = i_mb_4x4 - s4x4;
815 h->mb.cache.ref[i_list][i8+0] =
816 h->mb.cache.ref[i_list][i8+1] = h->mb.ref[i_list][ir + 0];
817 h->mb.cache.ref[i_list][i8+2] =
818 h->mb.cache.ref[i_list][i8+3] = h->mb.ref[i_list][ir + 1];
820 for( i = 0; i < 4; i++ )
822 h->mb.cache.mv[i_list][i8+i][0] = h->mb.mv[i_list][iv + i][0];
823 h->mb.cache.mv[i_list][i8+i][1] = h->mb.mv[i_list][iv + i][1];
828 const int i8 = x264_scan8[0] - 8;
829 for( i = 0; i < 4; i++ )
831 h->mb.cache.ref[i_list][i8+i] = -2;
832 h->mb.cache.mv[i_list][i8+i][0] =
833 h->mb.cache.mv[i_list][i8+i][1] = 0;
837 if( i_top_right_xy >= 0 )
839 const int i8 = x264_scan8[0] + 4 - 1*8;
840 const int ir = i_mb_8x8 - s8x8 + 2;
841 const int iv = i_mb_4x4 - s4x4 + 4;
843 h->mb.cache.ref[i_list][i8] = h->mb.ref[i_list][ir];
844 h->mb.cache.mv[i_list][i8][0] = h->mb.mv[i_list][iv][0];
845 h->mb.cache.mv[i_list][i8][1] = h->mb.mv[i_list][iv][1];
849 const int i8 = x264_scan8[0] + 4 - 1*8;
850 h->mb.cache.ref[i_list][i8] = -2;
851 h->mb.cache.mv[i_list][i8][0] = 0;
852 h->mb.cache.mv[i_list][i8][1] = 0;
857 const int i8 = x264_scan8[0] - 1;
858 const int ir = i_mb_8x8 - 1;
859 const int iv = i_mb_4x4 - 1;
861 h->mb.cache.ref[i_list][i8+0*8] =
862 h->mb.cache.ref[i_list][i8+1*8] = h->mb.ref[i_list][ir + 0*s8x8];
863 h->mb.cache.ref[i_list][i8+2*8] =
864 h->mb.cache.ref[i_list][i8+3*8] = h->mb.ref[i_list][ir + 1*s8x8];
866 for( i = 0; i < 4; i++ )
868 h->mb.cache.mv[i_list][i8+i*8][0] = h->mb.mv[i_list][iv + i*s4x4][0];
869 h->mb.cache.mv[i_list][i8+i*8][1] = h->mb.mv[i_list][iv + i*s4x4][1];
874 const int i8 = x264_scan8[0] - 1;
875 for( i = 0; i < 4; i++ )
877 h->mb.cache.ref[i_list][i8+i*8] = -2;
878 h->mb.cache.mv[i_list][i8+i*8][0] =
879 h->mb.cache.mv[i_list][i8+i*8][1] = 0;
883 if( h->param.b_cabac )
887 const int i8 = x264_scan8[0] - 8;
888 const int iv = i_mb_4x4 - s4x4;
889 for( i = 0; i < 4; i++ )
891 h->mb.cache.mvd[i_list][i8+i][0] = h->mb.mvd[i_list][iv + i][0];
892 h->mb.cache.mvd[i_list][i8+i][1] = h->mb.mvd[i_list][iv + i][1];
897 const int i8 = x264_scan8[0] - 8;
898 for( i = 0; i < 4; i++ )
900 h->mb.cache.mvd[i_list][i8+i][0] =
901 h->mb.cache.mvd[i_list][i8+i][1] = 0;
907 const int i8 = x264_scan8[0] - 1;
908 const int iv = i_mb_4x4 - 1;
909 for( i = 0; i < 4; i++ )
911 h->mb.cache.mvd[i_list][i8+i*8][0] = h->mb.mvd[i_list][iv + i*s4x4][0];
912 h->mb.cache.mvd[i_list][i8+i*8][1] = h->mb.mvd[i_list][iv + i*s4x4][1];
917 const int i8 = x264_scan8[0] - 1;
918 for( i = 0; i < 4; i++ )
920 h->mb.cache.mvd[i_list][i8+i*8][0] =
921 h->mb.cache.mvd[i_list][i8+i*8][1] = 0;
929 void x264_macroblock_cache_save( x264_t *h )
931 const int i_mb_xy = h->mb.i_mb_xy;
932 const int i_mb_type = h->mb.i_type;
933 const int i_mb_4x4 = 16 * h->mb.i_mb_stride * h->mb.i_mb_y + 4 * h->mb.i_mb_x;
934 const int i_mb_8x8 = 4 * h->mb.i_mb_stride * h->mb.i_mb_y + 2 * h->mb.i_mb_x;
938 if( IS_SKIP( h->mb.i_type ) )
939 h->mb.qp[i_mb_xy] = h->mb.i_last_qp;
941 h->mb.i_last_dqp = h->mb.qp[i_mb_xy] - h->mb.i_last_qp;
942 h->mb.i_last_qp = h->mb.qp[i_mb_xy];
945 if( i_mb_type == I_4x4 )
947 h->mb.intra4x4_pred_mode[i_mb_xy][0] = h->mb.cache.intra4x4_pred_mode[x264_scan8[10] ];
948 h->mb.intra4x4_pred_mode[i_mb_xy][1] = h->mb.cache.intra4x4_pred_mode[x264_scan8[11] ];
949 h->mb.intra4x4_pred_mode[i_mb_xy][2] = h->mb.cache.intra4x4_pred_mode[x264_scan8[14] ];
950 h->mb.intra4x4_pred_mode[i_mb_xy][3] = h->mb.cache.intra4x4_pred_mode[x264_scan8[15] ];
951 h->mb.intra4x4_pred_mode[i_mb_xy][4] = h->mb.cache.intra4x4_pred_mode[x264_scan8[5] ];
952 h->mb.intra4x4_pred_mode[i_mb_xy][5] = h->mb.cache.intra4x4_pred_mode[x264_scan8[7] ];
953 h->mb.intra4x4_pred_mode[i_mb_xy][6] = h->mb.cache.intra4x4_pred_mode[x264_scan8[13] ];
957 h->mb.intra4x4_pred_mode[i_mb_xy][0] =
958 h->mb.intra4x4_pred_mode[i_mb_xy][1] =
959 h->mb.intra4x4_pred_mode[i_mb_xy][2] =
960 h->mb.intra4x4_pred_mode[i_mb_xy][3] =
961 h->mb.intra4x4_pred_mode[i_mb_xy][4] =
962 h->mb.intra4x4_pred_mode[i_mb_xy][5] =
963 h->mb.intra4x4_pred_mode[i_mb_xy][6] = I_PRED_4x4_DC;
966 if( i_mb_type == I_PCM )
968 h->mb.cbp[i_mb_xy] = 0x72f; /* all set */
969 for( i = 0; i < 16 + 2*4; i++ )
971 h->mb.non_zero_count[i_mb_xy][i] = 16;
976 /* save non zero count */
977 for( i = 0; i < 16 + 2*4; i++ )
979 h->mb.non_zero_count[i_mb_xy][i] = h->mb.cache.non_zero_count[x264_scan8[i]];
983 if( !IS_INTRA( i_mb_type ) )
986 for( i_list = 0; i_list < (h->sh.i_type == SLICE_TYPE_P ? 1 : 2 ); i_list++ )
988 const int s8x8 = 2 * h->mb.i_mb_stride;
989 const int s4x4 = 4 * h->mb.i_mb_stride;
992 h->mb.ref[i_list][i_mb_8x8+0+0*s8x8] = h->mb.cache.ref[i_list][x264_scan8[0]];
993 h->mb.ref[i_list][i_mb_8x8+1+0*s8x8] = h->mb.cache.ref[i_list][x264_scan8[4]];
994 h->mb.ref[i_list][i_mb_8x8+0+1*s8x8] = h->mb.cache.ref[i_list][x264_scan8[8]];
995 h->mb.ref[i_list][i_mb_8x8+1+1*s8x8] = h->mb.cache.ref[i_list][x264_scan8[12]];
997 for( y = 0; y < 4; y++ )
999 for( x = 0; x < 4; x++ )
1001 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];
1002 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];
1010 for( i_list = 0; i_list < (h->sh.i_type == SLICE_TYPE_P ? 1 : 2 ); i_list++ )
1012 const int s8x8 = 2 * h->mb.i_mb_stride;
1013 const int s4x4 = 4 * h->mb.i_mb_stride;
1016 h->mb.ref[i_list][i_mb_8x8+0+0*s8x8] =
1017 h->mb.ref[i_list][i_mb_8x8+1+0*s8x8] =
1018 h->mb.ref[i_list][i_mb_8x8+0+1*s8x8] =
1019 h->mb.ref[i_list][i_mb_8x8+1+1*s8x8] = -1;
1021 for( y = 0; y < 4; y++ )
1023 for( x = 0; x < 4; x++ )
1025 h->mb.mv[i_list][i_mb_4x4+x+y*s4x4][0] = 0;
1026 h->mb.mv[i_list][i_mb_4x4+x+y*s4x4][1] = 0;
1032 if( h->param.b_cabac )
1034 if( i_mb_type == I_4x4 || i_mb_type == I_16x16 )
1035 h->mb.chroma_pred_mode[i_mb_xy] = h->mb.i_chroma_pred_mode;
1037 h->mb.chroma_pred_mode[i_mb_xy] = I_PRED_CHROMA_DC;
1039 if( !IS_INTRA( i_mb_type ) && !IS_SKIP( i_mb_type ) )
1042 for( i_list = 0; i_list < 2; i_list++ )
1044 const int s4x4 = 4 * h->mb.i_mb_stride;
1046 for( y = 0; y < 4; y++ )
1048 for( x = 0; x < 4; x++ )
1050 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];
1051 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];
1059 for( i_list = 0; i_list < 2; i_list++ )
1061 const int s4x4 = 4 * h->mb.i_mb_stride;
1063 for( y = 0; y < 4; y++ )
1065 for( x = 0; x < 4; x++ )
1067 h->mb.mvd[i_list][i_mb_4x4+x+y*s4x4][0] = 0;
1068 h->mb.mvd[i_list][i_mb_4x4+x+y*s4x4][1] = 0;