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 *****************************************************************************/
30 #include "macroblock.h"
32 static const uint8_t block_idx_x[16] =
34 0, 1, 0, 1, 2, 3, 2, 3, 0, 1, 0, 1, 2, 3, 2, 3
36 static const uint8_t block_idx_y[16] =
38 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, 3, 3, 2, 2, 3, 3
40 static const uint8_t block_idx_xy[4][4] =
48 static const int dequant_mf[6][4][4] =
50 { {10, 13, 10, 13}, {13, 16, 13, 16}, {10, 13, 10, 13}, {13, 16, 13, 16} },
51 { {11, 14, 11, 14}, {14, 18, 14, 18}, {11, 14, 11, 14}, {14, 18, 14, 18} },
52 { {13, 16, 13, 16}, {16, 20, 16, 20}, {13, 16, 13, 16}, {16, 20, 16, 20} },
53 { {14, 18, 14, 18}, {18, 23, 18, 23}, {14, 18, 14, 18}, {18, 23, 18, 23} },
54 { {16, 20, 16, 20}, {20, 25, 20, 25}, {16, 20, 16, 20}, {20, 25, 20, 25} },
55 { {18, 23, 18, 23}, {23, 29, 23, 29}, {18, 23, 18, 23}, {23, 29, 23, 29} }
59 static const int i_chroma_qp_table[52] =
61 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
62 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
63 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
64 29, 30, 31, 32, 32, 33, 34, 34, 35, 35,
65 36, 36, 37, 37, 37, 38, 38, 38, 39, 39,
70 int x264_mb_predict_intra4x4_mode( x264_t *h, int idx )
72 const int ma = h->mb.cache.intra4x4_pred_mode[x264_scan8[idx] - 1];
73 const int mb = h->mb.cache.intra4x4_pred_mode[x264_scan8[idx] - 8];
74 const int m = X264_MIN( ma, mb );
82 int x264_mb_predict_non_zero_code( x264_t *h, int idx )
84 const int za = h->mb.cache.non_zero_count[x264_scan8[idx] - 1];
85 const int zb = h->mb.cache.non_zero_count[x264_scan8[idx] - 8];
91 i_ret = ( i_ret + 1 ) >> 1;
96 /****************************************************************************
97 * Scan and Quant functions
98 ****************************************************************************/
99 void x264_mb_dequant_2x2_dc( int16_t dct[2][2], int i_qscale )
101 const int i_qbits = i_qscale/6 - 1;
105 const int i_dmf = dequant_mf[i_qscale%6][0][0] << i_qbits;
107 dct[0][0] = dct[0][0] * i_dmf;
108 dct[0][1] = dct[0][1] * i_dmf;
109 dct[1][0] = dct[1][0] * i_dmf;
110 dct[1][1] = dct[1][1] * i_dmf;
114 const int i_dmf = dequant_mf[i_qscale%6][0][0];
116 dct[0][0] = ( dct[0][0] * i_dmf ) >> 1;
117 dct[0][1] = ( dct[0][1] * i_dmf ) >> 1;
118 dct[1][0] = ( dct[1][0] * i_dmf ) >> 1;
119 dct[1][1] = ( dct[1][1] * i_dmf ) >> 1;
123 void x264_mb_dequant_4x4_dc( int16_t dct[4][4], int i_qscale )
125 const int i_qbits = i_qscale/6 - 2;
130 const int i_dmf = dequant_mf[i_qscale%6][0][0] << i_qbits;
132 for( y = 0; y < 4; y++ )
134 for( x = 0; x < 4; x++ )
136 dct[y][x] = dct[y][x] * i_dmf;
142 const int i_dmf = dequant_mf[i_qscale%6][0][0];
143 const int f = 1 << ( 1 + i_qbits );
145 for( y = 0; y < 4; y++ )
147 for( x = 0; x < 4; x++ )
149 dct[y][x] = ( dct[y][x] * i_dmf + f ) >> (-i_qbits);
155 void x264_mb_dequant_4x4( int16_t dct[4][4], int i_qscale )
157 const int i_mf = i_qscale%6;
158 const int i_qbits = i_qscale/6;
161 for( y = 0; y < 4; y++ )
163 dct[y][0] = ( dct[y][0] * dequant_mf[i_mf][y][0] ) << i_qbits;
164 dct[y][1] = ( dct[y][1] * dequant_mf[i_mf][y][1] ) << i_qbits;
165 dct[y][2] = ( dct[y][2] * dequant_mf[i_mf][y][2] ) << i_qbits;
166 dct[y][3] = ( dct[y][3] * dequant_mf[i_mf][y][3] ) << i_qbits;
170 static inline int x264_median( int a, int b, int c )
176 max = b; /* no need to do 'b > max' (more consuming than always doing affectation) */
183 return a + b + c - min - max;
186 void x264_mb_predict_mv( x264_t *h, int i_list, int idx, int i_width, int mvp[2] )
188 const int i8 = x264_scan8[idx];
189 const int i_ref= h->mb.cache.ref[i_list][i8];
190 int i_refa = h->mb.cache.ref[i_list][i8 - 1];
191 int16_t *mv_a = h->mb.cache.mv[i_list][i8 - 1];
192 int i_refb = h->mb.cache.ref[i_list][i8 - 8];
193 int16_t *mv_b = h->mb.cache.mv[i_list][i8 - 8];
194 int i_refc = h->mb.cache.ref[i_list][i8 - 8 + i_width ];
195 int16_t *mv_c = h->mb.cache.mv[i_list][i8 - 8 + i_width];
199 if( (idx&0x03) == 3 || ( i_width == 2 && (idx&0x3) == 2 )|| i_refc == -2 )
201 i_refc = h->mb.cache.ref[i_list][i8 - 8 - 1];
202 mv_c = h->mb.cache.mv[i_list][i8 - 8 - 1];
205 if( h->mb.i_partition == D_16x8 )
207 if( idx == 0 && i_refb == i_ref )
213 else if( idx != 0 && i_refa == i_ref )
220 else if( h->mb.i_partition == D_8x16 )
222 if( idx == 0 && i_refa == i_ref )
228 else if( idx != 0 && i_refc == i_ref )
237 if( i_refa == i_ref ) i_count++;
238 if( i_refb == i_ref ) i_count++;
239 if( i_refc == i_ref ) i_count++;
243 mvp[0] = x264_median( mv_a[0], mv_b[0], mv_c[0] );
244 mvp[1] = x264_median( mv_a[1], mv_b[1], mv_c[1] );
246 else if( i_count == 1 )
248 if( i_refa == i_ref )
253 else if( i_refb == i_ref )
264 else if( i_refb == -2 && i_refc == -2 && i_refa != -2 )
271 mvp[0] = x264_median( mv_a[0], mv_b[0], mv_c[0] );
272 mvp[1] = x264_median( mv_a[1], mv_b[1], mv_c[1] );
276 void x264_mb_predict_mv_16x16( x264_t *h, int i_list, int i_ref, int mvp[2] )
278 int i_refa = h->mb.cache.ref[i_list][X264_SCAN8_0 - 1];
279 int16_t *mv_a = h->mb.cache.mv[i_list][X264_SCAN8_0 - 1];
280 int i_refb = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8];
281 int16_t *mv_b = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8];
282 int i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 + 4];
283 int16_t *mv_c = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8 + 4];
289 i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 - 1];
290 mv_c = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8 - 1];
294 if( i_refa == i_ref ) i_count++;
295 if( i_refb == i_ref ) i_count++;
296 if( i_refc == i_ref ) i_count++;
300 mvp[0] = x264_median( mv_a[0], mv_b[0], mv_c[0] );
301 mvp[1] = x264_median( mv_a[1], mv_b[1], mv_c[1] );
303 else if( i_count == 1 )
305 if( i_refa == i_ref )
310 else if( i_refb == i_ref )
321 else if( i_refb == -2 && i_refc == -2 && i_refa != -2 )
328 mvp[0] = x264_median( mv_a[0], mv_b[0], mv_c[0] );
329 mvp[1] = x264_median( mv_a[1], mv_b[1], mv_c[1] );
334 void x264_mb_predict_mv_pskip( x264_t *h, int mv[2] )
336 int i_refa = h->mb.cache.ref[0][X264_SCAN8_0 - 1];
337 int i_refb = h->mb.cache.ref[0][X264_SCAN8_0 - 8];
338 int16_t *mv_a = h->mb.cache.mv[0][X264_SCAN8_0 - 1];
339 int16_t *mv_b = h->mb.cache.mv[0][X264_SCAN8_0 - 8];
341 if( i_refa == -2 || i_refb == -2 ||
342 ( i_refa == 0 && mv_a[0] == 0 && mv_a[1] == 0 ) ||
343 ( i_refb == 0 && mv_b[0] == 0 && mv_b[1] == 0 ) )
349 x264_mb_predict_mv_16x16( h, 0, 0, mv );
353 static inline void x264_mb_mc_0xywh( x264_t *h, int x, int y, int width, int height )
355 const int i8 = x264_scan8[0]+x+8*y;
356 const int i_ref = h->mb.cache.ref[0][i8];
357 const int mvx = h->mb.cache.mv[0][i8][0];
358 const int mvy = h->mb.cache.mv[0][i8][1];
360 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],
361 &h->mb.pic.p_fdec[0][4*y * h->mb.pic.i_stride[0]+4*x], h->mb.pic.i_stride[0],
362 mvx, mvy, 4*width, 4*height );
364 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],
365 &h->mb.pic.p_fdec[1][2*y*h->mb.pic.i_stride[1]+2*x], h->mb.pic.i_stride[1],
366 mvx, mvy, 2*width, 2*height );
368 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],
369 &h->mb.pic.p_fdec[2][2*y*h->mb.pic.i_stride[2]+2*x], h->mb.pic.i_stride[2],
370 mvx, mvy, 2*width, 2*height );
372 static inline void x264_mb_mc_1xywh( x264_t *h, int x, int y, int width, int height )
374 const int i8 = x264_scan8[0]+x+8*y;
375 const int i_ref = h->mb.cache.ref[1][i8];
376 const int mvx = h->mb.cache.mv[1][i8][0];
377 const int mvy = h->mb.cache.mv[1][i8][1];
379 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],
380 &h->mb.pic.p_fdec[0][4*y *h->mb.pic.i_stride[0]+4*x], h->mb.pic.i_stride[0],
381 mvx, mvy, 4*width, 4*height );
383 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],
384 &h->mb.pic.p_fdec[1][2*y*h->mb.pic.i_stride[1]+2*x], h->mb.pic.i_stride[1],
385 mvx, mvy, 2*width, 2*height );
387 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],
388 &h->mb.pic.p_fdec[2][2*y*h->mb.pic.i_stride[2]+2*x], h->mb.pic.i_stride[2],
389 mvx, mvy, 2*width, 2*height );
392 static inline void x264_mb_mc_01xywh( x264_t *h, int x, int y, int width, int height )
394 const int i8 = x264_scan8[0]+x+8*y;
396 const int i_ref0 = h->mb.cache.ref[0][i8];
397 const int mvx0 = h->mb.cache.mv[0][i8][0];
398 const int mvy0 = h->mb.cache.mv[0][i8][1];
400 const int i_ref1 = h->mb.cache.ref[1][i8];
401 const int mvx1 = h->mb.cache.mv[1][i8][0];
402 const int mvy1 = h->mb.cache.mv[1][i8][1];
403 DECLARE_ALIGNED( uint8_t, tmp[16*16], 16 );
406 if( width == 4 && height == 4 ) i_mode = PIXEL_16x16;
407 else if( width == 4 && height == 2 ) i_mode = PIXEL_16x8;
408 else if( width == 2 && height == 4 ) i_mode = PIXEL_8x16;
409 else if( width == 2 && height == 2 ) i_mode = PIXEL_8x8;
410 else if( width == 2 && height == 1 ) i_mode = PIXEL_8x4;
411 else if( width == 1 && height == 2 ) i_mode = PIXEL_4x8;
412 else if( width == 1 && height == 1 ) i_mode = PIXEL_4x4;
414 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],
415 &h->mb.pic.p_fdec[0][4*y *h->mb.pic.i_stride[0]+4*x], h->mb.pic.i_stride[0],
416 mvx0, mvy0, 4*width, 4*height );
417 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],
418 &h->mb.pic.p_fdec[1][2*y*h->mb.pic.i_stride[1]+2*x], h->mb.pic.i_stride[1],
419 mvx0, mvy0, 2*width, 2*height );
420 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],
421 &h->mb.pic.p_fdec[2][2*y*h->mb.pic.i_stride[2]+2*x], h->mb.pic.i_stride[2],
422 mvx0, mvy0, 2*width, 2*height );
425 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],
426 tmp, 16, mvx1, mvy1, 4*width, 4*height );
427 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 );
429 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],
430 tmp, 16, mvx1, mvy1, 2*width, 2*height );
431 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 );
433 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],
434 tmp, 16, mvx1, mvy1, 2*width, 2*height );
435 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 );
439 void x264_mb_mc( x264_t *h )
441 if( h->mb.i_type == P_L0 )
443 if( h->mb.i_partition == D_16x16 )
445 x264_mb_mc_0xywh( h, 0, 0, 4, 4 );
447 else if( h->mb.i_partition == D_16x8 )
449 x264_mb_mc_0xywh( h, 0, 0, 4, 2 );
450 x264_mb_mc_0xywh( h, 0, 2, 4, 2 );
452 else if( h->mb.i_partition == D_8x16 )
454 x264_mb_mc_0xywh( h, 0, 0, 2, 4 );
455 x264_mb_mc_0xywh( h, 2, 0, 2, 4 );
458 else if( h->mb.i_type == P_8x8 )
461 for( i = 0; i < 4; i++ )
463 const int x = 2*(i%2);
464 const int y = 2*(i/2);
465 switch( h->mb.i_sub_partition[i] )
468 x264_mb_mc_0xywh( h, x, y, 2, 2 );
471 x264_mb_mc_0xywh( h, x, y+0, 2, 1 );
472 x264_mb_mc_0xywh( h, x, y+1, 2, 1 );
475 x264_mb_mc_0xywh( h, x+0, y, 1, 2 );
476 x264_mb_mc_0xywh( h, x+1, y, 1, 2 );
479 x264_mb_mc_0xywh( h, x+0, y+0, 1, 1 );
480 x264_mb_mc_0xywh( h, x+1, y+0, 1, 1 );
481 x264_mb_mc_0xywh( h, x+0, y+1, 1, 1 );
482 x264_mb_mc_0xywh( h, x+1, y+1, 1, 1 );
487 else if( h->mb.i_type == B_8x8 || h->mb.i_type == B_DIRECT )
489 fprintf( stderr, "mc_luma with unsupported mb\n" );
499 /* init ref list utilisations */
500 for( i = 0; i < 2; i++ )
502 b_list0[i] = x264_mb_type_list0_table[h->mb.i_type][i];
503 b_list1[i] = x264_mb_type_list1_table[h->mb.i_type][i];
505 if( h->mb.i_partition == D_16x16 )
507 if( b_list0[0] && b_list1[0] ) x264_mb_mc_01xywh( h, 0, 0, 4, 4 );
508 else if( b_list0[0] ) x264_mb_mc_0xywh ( h, 0, 0, 4, 4 );
509 else if( b_list1[0] ) x264_mb_mc_1xywh ( h, 0, 0, 4, 4 );
511 else if( h->mb.i_partition == D_16x8 )
513 if( b_list0[0] && b_list1[0] ) x264_mb_mc_01xywh( h, 0, 0, 4, 2 );
514 else if( b_list0[0] ) x264_mb_mc_0xywh ( h, 0, 0, 4, 2 );
515 else if( b_list1[0] ) x264_mb_mc_1xywh ( h, 0, 0, 4, 2 );
517 if( b_list0[1] && b_list1[1] ) x264_mb_mc_01xywh( h, 0, 2, 4, 2 );
518 else if( b_list0[1] ) x264_mb_mc_0xywh ( h, 0, 2, 4, 2 );
519 else if( b_list1[1] ) x264_mb_mc_1xywh ( h, 0, 2, 4, 2 );
521 else if( h->mb.i_partition == D_8x16 )
523 if( b_list0[0] && b_list1[0] ) x264_mb_mc_01xywh( h, 0, 0, 2, 4 );
524 else if( b_list0[0] ) x264_mb_mc_0xywh ( h, 0, 0, 2, 4 );
525 else if( b_list1[0] ) x264_mb_mc_1xywh ( h, 0, 0, 2, 4 );
527 if( b_list0[1] && b_list1[1] ) x264_mb_mc_01xywh( h, 2, 0, 2, 4 );
528 else if( b_list0[1] ) x264_mb_mc_0xywh ( h, 2, 0, 2, 4 );
529 else if( b_list1[1] ) x264_mb_mc_1xywh ( h, 2, 0, 2, 4 );
534 void x264_macroblock_cache_init( x264_t *h )
536 int i_mb_count = h->sps->i_mb_width * h->sps->i_mb_height;
538 h->mb.i_mb_stride = h->sps->i_mb_width;
540 h->mb.type= x264_malloc( i_mb_count * sizeof( int8_t) );
541 h->mb.qp = x264_malloc( i_mb_count * sizeof( int8_t) );
542 h->mb.cbp = x264_malloc( i_mb_count * sizeof( int16_t) );
544 /* 0 -> 3 top(4), 4 -> 6 : left(3) */
545 h->mb.intra4x4_pred_mode = x264_malloc( i_mb_count * 7 * sizeof( int8_t ) );
548 h->mb.non_zero_count = x264_malloc( i_mb_count * 24 * sizeof( uint8_t ) );
550 h->mb.mv[0] = x264_malloc( 2*16 * i_mb_count * sizeof( int16_t ) );
551 h->mb.mv[1] = x264_malloc( 2*16 * i_mb_count * sizeof( int16_t ) );
552 h->mb.ref[0] = x264_malloc( 4 * i_mb_count * sizeof( int16_t ) );
553 h->mb.ref[1] = x264_malloc( 4 * i_mb_count * sizeof( int16_t ) );
555 if( h->param.b_cabac )
557 h->mb.chroma_pred_mode = x264_malloc( i_mb_count * sizeof( int8_t) );
558 h->mb.mvd[0] = x264_malloc( 2*16 * i_mb_count * sizeof( int16_t ) );
559 h->mb.mvd[1] = x264_malloc( 2*16 * i_mb_count * sizeof( int16_t ) );
562 /* init with not avaiable (for top right idx=7,15) */
563 memset( h->mb.cache.ref[0], -2, X264_SCAN8_SIZE * sizeof( int8_t ) );
564 memset( h->mb.cache.ref[1], -2, X264_SCAN8_SIZE * sizeof( int8_t ) );
566 void x264_macroblock_cache_end( x264_t *h )
568 if( h->param.b_cabac )
570 x264_free( h->mb.chroma_pred_mode );
571 x264_free( h->mb.mvd[0] );
572 x264_free( h->mb.mvd[1] );
574 x264_free( h->mb.mv[0] );
575 x264_free( h->mb.mv[1] );
576 x264_free( h->mb.ref[0] );
577 x264_free( h->mb.ref[1] );
578 x264_free( h->mb.intra4x4_pred_mode );
579 x264_free( h->mb.non_zero_count );
580 x264_free( h->mb.cbp );
581 x264_free( h->mb.qp );
582 x264_free( h->mb.type );
586 void x264_macroblock_cache_load( x264_t *h, int i_mb_x, int i_mb_y )
588 const int i_mb_4x4 = 16 * h->mb.i_mb_stride *i_mb_y + 4 * i_mb_x;
589 const int i_mb_8x8 = 4 * h->mb.i_mb_stride *i_mb_y + 2 * i_mb_x;
593 int i_top_type = -1; /* gcc warn */
599 h->mb.i_mb_x = i_mb_x;
600 h->mb.i_mb_y = i_mb_y;
601 h->mb.i_mb_xy = i_mb_y * h->mb.i_mb_stride + i_mb_x;
602 h->mb.i_neighbour = 0;
604 /* load picture pointers */
605 for( i = 0; i < 3; i++ )
607 const int w = (i == 0 ? 16 : 8);
608 const int i_stride = h->fdec->i_stride[i];
611 h->mb.pic.i_stride[i] = i_stride;
613 h->mb.pic.p_fenc[i] = &h->fenc->plane[i][ w * ( i_mb_x + i_mb_y * i_stride )];
615 h->mb.pic.p_fdec[i] = &h->fdec->plane[i][ w * ( i_mb_x + i_mb_y * i_stride )];
617 for( j = 0; j < h->i_ref0; j++ )
619 h->mb.pic.p_fref[0][j][i] = &h->fref0[j]->plane[i][ w * ( i_mb_x + i_mb_y * i_stride )];
621 for( j = 0; j < h->i_ref1; j++ )
623 h->mb.pic.p_fref[1][j][i] = &h->fref1[j]->plane[i][ w * ( i_mb_x + i_mb_y * i_stride )];
630 i_top_xy = h->mb.i_mb_xy - h->mb.i_mb_stride;
631 i_top_type= h->mb.type[i_top_xy];
633 h->mb.i_neighbour |= MB_TOP;
636 h->mb.cache.intra4x4_pred_mode[x264_scan8[0] - 8] = h->mb.intra4x4_pred_mode[i_top_xy][0];
637 h->mb.cache.intra4x4_pred_mode[x264_scan8[1] - 8] = h->mb.intra4x4_pred_mode[i_top_xy][1];
638 h->mb.cache.intra4x4_pred_mode[x264_scan8[4] - 8] = h->mb.intra4x4_pred_mode[i_top_xy][2];
639 h->mb.cache.intra4x4_pred_mode[x264_scan8[5] - 8] = h->mb.intra4x4_pred_mode[i_top_xy][3];
641 /* load non_zero_count */
642 h->mb.cache.non_zero_count[x264_scan8[0] - 8] = h->mb.non_zero_count[i_top_xy][10];
643 h->mb.cache.non_zero_count[x264_scan8[1] - 8] = h->mb.non_zero_count[i_top_xy][11];
644 h->mb.cache.non_zero_count[x264_scan8[4] - 8] = h->mb.non_zero_count[i_top_xy][14];
645 h->mb.cache.non_zero_count[x264_scan8[5] - 8] = h->mb.non_zero_count[i_top_xy][15];
647 h->mb.cache.non_zero_count[x264_scan8[16+0] - 8] = h->mb.non_zero_count[i_top_xy][16+2];
648 h->mb.cache.non_zero_count[x264_scan8[16+1] - 8] = h->mb.non_zero_count[i_top_xy][16+3];
650 h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 8] = h->mb.non_zero_count[i_top_xy][16+4+2];
651 h->mb.cache.non_zero_count[x264_scan8[16+4+1] - 8] = h->mb.non_zero_count[i_top_xy][16+4+3];
656 h->mb.cache.intra4x4_pred_mode[x264_scan8[0] - 8] =
657 h->mb.cache.intra4x4_pred_mode[x264_scan8[1] - 8] =
658 h->mb.cache.intra4x4_pred_mode[x264_scan8[4] - 8] =
659 h->mb.cache.intra4x4_pred_mode[x264_scan8[5] - 8] = -1;
661 /* load non_zero_count */
662 h->mb.cache.non_zero_count[x264_scan8[0] - 8] =
663 h->mb.cache.non_zero_count[x264_scan8[1] - 8] =
664 h->mb.cache.non_zero_count[x264_scan8[4] - 8] =
665 h->mb.cache.non_zero_count[x264_scan8[5] - 8] =
666 h->mb.cache.non_zero_count[x264_scan8[16+0] - 8] =
667 h->mb.cache.non_zero_count[x264_scan8[16+1] - 8] =
668 h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 8] =
669 h->mb.cache.non_zero_count[x264_scan8[16+4+1] - 8] = 0x80;
675 i_left_xy = h->mb.i_mb_xy - 1;
676 i_left_type= h->mb.type[i_left_xy];
678 h->mb.i_neighbour |= MB_LEFT;
681 h->mb.cache.intra4x4_pred_mode[x264_scan8[0 ] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][4];
682 h->mb.cache.intra4x4_pred_mode[x264_scan8[2 ] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][5];
683 h->mb.cache.intra4x4_pred_mode[x264_scan8[8 ] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][6];
684 h->mb.cache.intra4x4_pred_mode[x264_scan8[10] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][3];
686 /* load non_zero_count */
687 h->mb.cache.non_zero_count[x264_scan8[0 ] - 1] = h->mb.non_zero_count[i_left_xy][5];
688 h->mb.cache.non_zero_count[x264_scan8[2 ] - 1] = h->mb.non_zero_count[i_left_xy][7];
689 h->mb.cache.non_zero_count[x264_scan8[8 ] - 1] = h->mb.non_zero_count[i_left_xy][13];
690 h->mb.cache.non_zero_count[x264_scan8[10] - 1] = h->mb.non_zero_count[i_left_xy][15];
692 h->mb.cache.non_zero_count[x264_scan8[16+0] - 1] = h->mb.non_zero_count[i_left_xy][16+1];
693 h->mb.cache.non_zero_count[x264_scan8[16+2] - 1] = h->mb.non_zero_count[i_left_xy][16+3];
695 h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 1] = h->mb.non_zero_count[i_left_xy][16+4+1];
696 h->mb.cache.non_zero_count[x264_scan8[16+4+2] - 1] = h->mb.non_zero_count[i_left_xy][16+4+3];
700 h->mb.cache.intra4x4_pred_mode[x264_scan8[0 ] - 1] =
701 h->mb.cache.intra4x4_pred_mode[x264_scan8[2 ] - 1] =
702 h->mb.cache.intra4x4_pred_mode[x264_scan8[8 ] - 1] =
703 h->mb.cache.intra4x4_pred_mode[x264_scan8[10] - 1] = -1;
705 /* load non_zero_count */
706 h->mb.cache.non_zero_count[x264_scan8[0 ] - 1] =
707 h->mb.cache.non_zero_count[x264_scan8[2 ] - 1] =
708 h->mb.cache.non_zero_count[x264_scan8[8 ] - 1] =
709 h->mb.cache.non_zero_count[x264_scan8[10] - 1] =
710 h->mb.cache.non_zero_count[x264_scan8[16+0] - 1] =
711 h->mb.cache.non_zero_count[x264_scan8[16+2] - 1] =
712 h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 1] =
713 h->mb.cache.non_zero_count[x264_scan8[16+4+2] - 1] = 0x80;
716 if( i_mb_y > 0 && i_mb_x < h->sps->i_mb_width - 1 )
718 h->mb.i_neighbour |= MB_TOPRIGHT;
721 /* load ref/mv/mvd */
722 if( h->sh.i_type != SLICE_TYPE_I )
724 int s8x8 = 2 * h->mb.i_mb_stride;
725 int s4x4 = 4 * h->mb.i_mb_stride;
727 int i_top_left_xy = -1;
728 int i_top_right_xy = -1;
732 if( h->mb.i_mb_y > 0 && h->mb.i_mb_x > 0 )
734 i_top_left_xy = i_top_xy - 1;
736 if( h->mb.i_mb_y > 0 && h->mb.i_mb_x < h->sps->i_mb_width - 1 )
738 i_top_right_xy = i_top_xy + 1;
741 for( i_list = 0; i_list < (h->sh.i_type == SLICE_TYPE_P ? 1 : 2 ); i_list++ )
744 h->mb.cache.ref[i_list][x264_scan8[5 ]+1] =
745 h->mb.cache.ref[i_list][x264_scan8[7 ]+1] =
746 h->mb.cache.ref[i_list][x264_scan8[13]+1] = -2;
749 if( i_top_left_xy >= 0 )
751 const int i8 = x264_scan8[0] - 1 - 1*8;
752 const int ir = i_mb_8x8 - s8x8 - 1;
753 const int iv = i_mb_4x4 - s4x4 - 1;
754 h->mb.cache.ref[i_list][i8] = h->mb.ref[i_list][ir];
755 h->mb.cache.mv[i_list][i8][0] = h->mb.mv[i_list][iv][0];
756 h->mb.cache.mv[i_list][i8][1] = h->mb.mv[i_list][iv][1];
760 const int i8 = x264_scan8[0] - 1 - 1*8;
761 h->mb.cache.ref[i_list][i8] = -2;
762 h->mb.cache.mv[i_list][i8][0] = 0;
763 h->mb.cache.mv[i_list][i8][1] = 0;
768 const int i8 = x264_scan8[0] - 8;
769 const int ir = i_mb_8x8 - s8x8;
770 const int iv = i_mb_4x4 - s4x4;
772 h->mb.cache.ref[i_list][i8+0] =
773 h->mb.cache.ref[i_list][i8+1] = h->mb.ref[i_list][ir + 0];
774 h->mb.cache.ref[i_list][i8+2] =
775 h->mb.cache.ref[i_list][i8+3] = h->mb.ref[i_list][ir + 1];
777 for( i = 0; i < 4; i++ )
779 h->mb.cache.mv[i_list][i8+i][0] = h->mb.mv[i_list][iv + i][0];
780 h->mb.cache.mv[i_list][i8+i][1] = h->mb.mv[i_list][iv + i][1];
785 const int i8 = x264_scan8[0] - 8;
786 for( i = 0; i < 4; i++ )
788 h->mb.cache.ref[i_list][i8+i] = -2;
789 h->mb.cache.mv[i_list][i8+i][0] =
790 h->mb.cache.mv[i_list][i8+i][1] = 0;
794 if( i_top_right_xy >= 0 )
796 const int i8 = x264_scan8[0] + 4 - 1*8;
797 const int ir = i_mb_8x8 - s8x8 + 2;
798 const int iv = i_mb_4x4 - s4x4 + 4;
800 h->mb.cache.ref[i_list][i8] = h->mb.ref[i_list][ir];
801 h->mb.cache.mv[i_list][i8][0] = h->mb.mv[i_list][iv][0];
802 h->mb.cache.mv[i_list][i8][1] = h->mb.mv[i_list][iv][1];
806 const int i8 = x264_scan8[0] + 4 - 1*8;
807 h->mb.cache.ref[i_list][i8] = -2;
808 h->mb.cache.mv[i_list][i8][0] = 0;
809 h->mb.cache.mv[i_list][i8][1] = 0;
814 const int i8 = x264_scan8[0] - 1;
815 const int ir = i_mb_8x8 - 1;
816 const int iv = i_mb_4x4 - 1;
818 h->mb.cache.ref[i_list][i8+0*8] =
819 h->mb.cache.ref[i_list][i8+1*8] = h->mb.ref[i_list][ir + 0*s8x8];
820 h->mb.cache.ref[i_list][i8+2*8] =
821 h->mb.cache.ref[i_list][i8+3*8] = h->mb.ref[i_list][ir + 1*s8x8];
823 for( i = 0; i < 4; i++ )
825 h->mb.cache.mv[i_list][i8+i*8][0] = h->mb.mv[i_list][iv + i*s4x4][0];
826 h->mb.cache.mv[i_list][i8+i*8][1] = h->mb.mv[i_list][iv + i*s4x4][1];
831 const int i8 = x264_scan8[0] - 1;
832 for( i = 0; i < 4; i++ )
834 h->mb.cache.ref[i_list][i8+i*8] = -2;
835 h->mb.cache.mv[i_list][i8+i*8][0] =
836 h->mb.cache.mv[i_list][i8+i*8][1] = 0;
840 if( h->param.b_cabac )
844 const int i8 = x264_scan8[0] - 8;
845 const int iv = i_mb_4x4 - s4x4;
846 for( i = 0; i < 4; i++ )
848 h->mb.cache.mvd[i_list][i8+i][0] = h->mb.mvd[i_list][iv + i][0];
849 h->mb.cache.mvd[i_list][i8+i][1] = h->mb.mvd[i_list][iv + i][1];
854 const int i8 = x264_scan8[0] - 8;
855 for( i = 0; i < 4; i++ )
857 h->mb.cache.mvd[i_list][i8+i][0] =
858 h->mb.cache.mvd[i_list][i8+i][1] = 0;
864 const int i8 = x264_scan8[0] - 1;
865 const int iv = i_mb_4x4 - 1;
866 for( i = 0; i < 4; i++ )
868 h->mb.cache.mvd[i_list][i8+i*8][0] = h->mb.mvd[i_list][iv + i*s4x4][0];
869 h->mb.cache.mvd[i_list][i8+i*8][1] = h->mb.mvd[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.mvd[i_list][i8+i*8][0] =
878 h->mb.cache.mvd[i_list][i8+i*8][1] = 0;
886 void x264_macroblock_cache_save( x264_t *h )
888 const int i_mb_xy = h->mb.i_mb_xy;
889 const int i_mb_type = h->mb.i_type;
890 const int i_mb_4x4 = 16 * h->mb.i_mb_stride * h->mb.i_mb_y + 4 * h->mb.i_mb_x;
891 const int i_mb_8x8 = 4 * h->mb.i_mb_stride * h->mb.i_mb_y + 2 * h->mb.i_mb_x;
895 h->mb.i_last_dqp = h->mb.qp[i_mb_xy] - h->mb.i_last_qp;
896 h->mb.i_last_qp = h->mb.qp[i_mb_xy];
899 if( i_mb_type == I_4x4 )
901 h->mb.intra4x4_pred_mode[i_mb_xy][0] = h->mb.cache.intra4x4_pred_mode[x264_scan8[10] ];
902 h->mb.intra4x4_pred_mode[i_mb_xy][1] = h->mb.cache.intra4x4_pred_mode[x264_scan8[11] ];
903 h->mb.intra4x4_pred_mode[i_mb_xy][2] = h->mb.cache.intra4x4_pred_mode[x264_scan8[14] ];
904 h->mb.intra4x4_pred_mode[i_mb_xy][3] = h->mb.cache.intra4x4_pred_mode[x264_scan8[15] ];
905 h->mb.intra4x4_pred_mode[i_mb_xy][4] = h->mb.cache.intra4x4_pred_mode[x264_scan8[5] ];
906 h->mb.intra4x4_pred_mode[i_mb_xy][5] = h->mb.cache.intra4x4_pred_mode[x264_scan8[7] ];
907 h->mb.intra4x4_pred_mode[i_mb_xy][6] = h->mb.cache.intra4x4_pred_mode[x264_scan8[13] ];
911 h->mb.intra4x4_pred_mode[i_mb_xy][0] =
912 h->mb.intra4x4_pred_mode[i_mb_xy][1] =
913 h->mb.intra4x4_pred_mode[i_mb_xy][2] =
914 h->mb.intra4x4_pred_mode[i_mb_xy][3] =
915 h->mb.intra4x4_pred_mode[i_mb_xy][4] =
916 h->mb.intra4x4_pred_mode[i_mb_xy][5] =
917 h->mb.intra4x4_pred_mode[i_mb_xy][6] = I_PRED_4x4_DC;
920 if( i_mb_type == I_PCM )
922 h->mb.cbp[i_mb_xy] = 0x72f; /* all set */
923 for( i = 0; i < 16 + 2*4; i++ )
925 h->mb.non_zero_count[i_mb_xy][i] = 16;
930 /* save non zero count */
931 for( i = 0; i < 16 + 2*4; i++ )
933 h->mb.non_zero_count[i_mb_xy][i] = h->mb.cache.non_zero_count[x264_scan8[i]];
937 if( !IS_INTRA( i_mb_type ) )
940 for( i_list = 0; i_list < (h->sh.i_type == SLICE_TYPE_P ? 1 : 2 ); i_list++ )
942 const int s8x8 = 2 * h->mb.i_mb_stride;
943 const int s4x4 = 4 * h->mb.i_mb_stride;
946 h->mb.ref[i_list][i_mb_8x8+0+0*s8x8] = h->mb.cache.ref[i_list][x264_scan8[0]];
947 h->mb.ref[i_list][i_mb_8x8+1+0*s8x8] = h->mb.cache.ref[i_list][x264_scan8[4]];
948 h->mb.ref[i_list][i_mb_8x8+0+1*s8x8] = h->mb.cache.ref[i_list][x264_scan8[8]];
949 h->mb.ref[i_list][i_mb_8x8+1+1*s8x8] = h->mb.cache.ref[i_list][x264_scan8[12]];
951 for( y = 0; y < 4; y++ )
953 for( x = 0; x < 4; x++ )
955 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];
956 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];
964 for( i_list = 0; i_list < (h->sh.i_type == SLICE_TYPE_P ? 1 : 2 ); i_list++ )
966 const int s8x8 = 2 * h->mb.i_mb_stride;
967 const int s4x4 = 4 * h->mb.i_mb_stride;
970 h->mb.ref[i_list][i_mb_8x8+0+0*s8x8] =
971 h->mb.ref[i_list][i_mb_8x8+1+0*s8x8] =
972 h->mb.ref[i_list][i_mb_8x8+0+1*s8x8] =
973 h->mb.ref[i_list][i_mb_8x8+1+1*s8x8] = -1;
975 for( y = 0; y < 4; y++ )
977 for( x = 0; x < 4; x++ )
979 h->mb.mv[i_list][i_mb_4x4+x+y*s4x4][0] = 0;
980 h->mb.mv[i_list][i_mb_4x4+x+y*s4x4][1] = 0;
986 if( h->param.b_cabac )
988 if( i_mb_type == I_4x4 || i_mb_type == I_16x16 )
989 h->mb.chroma_pred_mode[i_mb_xy] = h->mb.i_chroma_pred_mode;
991 h->mb.chroma_pred_mode[i_mb_xy] = I_PRED_CHROMA_DC;
993 if( !IS_INTRA( i_mb_type ) && !IS_SKIP( i_mb_type ) )
996 for( i_list = 0; i_list < 2; i_list++ )
998 const int s4x4 = 4 * h->mb.i_mb_stride;
1000 for( y = 0; y < 4; y++ )
1002 for( x = 0; x < 4; x++ )
1004 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];
1005 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];
1013 for( i_list = 0; i_list < 2; i_list++ )
1015 const int s4x4 = 4 * h->mb.i_mb_stride;
1017 for( y = 0; y < 4; y++ )
1019 for( x = 0; x < 4; x++ )
1021 h->mb.mvd[i_list][i_mb_4x4+x+y*s4x4][0] = 0;
1022 h->mb.mvd[i_list][i_mb_4x4+x+y*s4x4][1] = 0;