📄 macroblock.c.svn-base
字号:
/***************************************************************************** * macroblock.c: h264 encoder library ***************************************************************************** * Copyright (C) 2003 Laurent Aimar * $Id: macroblock.c,v 1.1 2004/06/03 19:27:06 fenrir Exp $ * * Authors: Laurent Aimar <fenrir@via.ecp.fr> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. *****************************************************************************/#include <stdlib.h>#include <stdio.h>#include <string.h>#include "common.h"#include "macroblock.h"static const uint8_t block_idx_x[16] ={ 0, 1, 0, 1, 2, 3, 2, 3, 0, 1, 0, 1, 2, 3, 2, 3};static const uint8_t block_idx_y[16] ={ 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, 3, 3, 2, 2, 3, 3};static const uint8_t block_idx_xy[4][4] ={ { 0, 2, 8, 10}, { 1, 3, 9, 11}, { 4, 6, 12, 14}, { 5, 7, 13, 15}};static const int dequant_mf[6][4][4] ={ { {10, 13, 10, 13}, {13, 16, 13, 16}, {10, 13, 10, 13}, {13, 16, 13, 16} }, { {11, 14, 11, 14}, {14, 18, 14, 18}, {11, 14, 11, 14}, {14, 18, 14, 18} }, { {13, 16, 13, 16}, {16, 20, 16, 20}, {13, 16, 13, 16}, {16, 20, 16, 20} }, { {14, 18, 14, 18}, {18, 23, 18, 23}, {14, 18, 14, 18}, {18, 23, 18, 23} }, { {16, 20, 16, 20}, {20, 25, 20, 25}, {16, 20, 16, 20}, {20, 25, 20, 25} }, { {18, 23, 18, 23}, {23, 29, 23, 29}, {18, 23, 18, 23}, {23, 29, 23, 29} }};#if 0static const int i_chroma_qp_table[52] ={ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 29, 30, 31, 32, 32, 33, 34, 34, 35, 35, 36, 36, 37, 37, 37, 38, 38, 38, 39, 39, 39, 39};#endifint x264_mb_predict_intra4x4_mode( x264_t *h, int idx ){ const int ma = h->mb.cache.intra4x4_pred_mode[x264_scan8[idx] - 1]; const int mb = h->mb.cache.intra4x4_pred_mode[x264_scan8[idx] - 8]; const int m = X264_MIN( ma, mb ); if( m < 0 ) return I_PRED_4x4_DC; return m;}int x264_mb_predict_non_zero_code( x264_t *h, int idx ){ const int za = h->mb.cache.non_zero_count[x264_scan8[idx] - 1]; const int zb = h->mb.cache.non_zero_count[x264_scan8[idx] - 8]; int i_ret = za + zb; if( i_ret < 0x80 ) { i_ret = ( i_ret + 1 ) >> 1; } return i_ret & 0x7f;}/**************************************************************************** * Scan and Quant functions ****************************************************************************/void x264_mb_dequant_2x2_dc( int16_t dct[2][2], int i_qscale ){ const int i_qbits = i_qscale/6 - 1; if( i_qbits >= 0 ) { const int i_dmf = dequant_mf[i_qscale%6][0][0] << i_qbits; dct[0][0] = dct[0][0] * i_dmf; dct[0][1] = dct[0][1] * i_dmf; dct[1][0] = dct[1][0] * i_dmf; dct[1][1] = dct[1][1] * i_dmf; } else { const int i_dmf = dequant_mf[i_qscale%6][0][0]; dct[0][0] = ( dct[0][0] * i_dmf ) >> 1; dct[0][1] = ( dct[0][1] * i_dmf ) >> 1; dct[1][0] = ( dct[1][0] * i_dmf ) >> 1; dct[1][1] = ( dct[1][1] * i_dmf ) >> 1; }}void x264_mb_dequant_4x4_dc( int16_t dct[4][4], int i_qscale ){ const int i_qbits = i_qscale/6 - 2; int x,y; if( i_qbits >= 0 ) { const int i_dmf = dequant_mf[i_qscale%6][0][0] << i_qbits; for( y = 0; y < 4; y++ ) { for( x = 0; x < 4; x++ ) { dct[y][x] = dct[y][x] * i_dmf; } } } else { const int i_dmf = dequant_mf[i_qscale%6][0][0]; const int f = 1 << ( 1 + i_qbits ); for( y = 0; y < 4; y++ ) { for( x = 0; x < 4; x++ ) { dct[y][x] = ( dct[y][x] * i_dmf + f ) >> (-i_qbits); } } }}void x264_mb_dequant_4x4( int16_t dct[4][4], int i_qscale ){ const int i_mf = i_qscale%6; const int i_qbits = i_qscale/6; int y; for( y = 0; y < 4; y++ ) { dct[y][0] = ( dct[y][0] * dequant_mf[i_mf][y][0] ) << i_qbits; dct[y][1] = ( dct[y][1] * dequant_mf[i_mf][y][1] ) << i_qbits; dct[y][2] = ( dct[y][2] * dequant_mf[i_mf][y][2] ) << i_qbits; dct[y][3] = ( dct[y][3] * dequant_mf[i_mf][y][3] ) << i_qbits; }}void x264_mb_predict_mv( x264_t *h, int i_list, int idx, int i_width, int mvp[2] ){ const int i8 = x264_scan8[idx]; const int i_ref= h->mb.cache.ref[i_list][i8]; int i_refa = h->mb.cache.ref[i_list][i8 - 1]; int16_t *mv_a = h->mb.cache.mv[i_list][i8 - 1]; int i_refb = h->mb.cache.ref[i_list][i8 - 8]; int16_t *mv_b = h->mb.cache.mv[i_list][i8 - 8]; int i_refc = h->mb.cache.ref[i_list][i8 - 8 + i_width ]; int16_t *mv_c = h->mb.cache.mv[i_list][i8 - 8 + i_width]; int i_count; if( (idx&0x03) == 3 || ( i_width == 2 && (idx&0x3) == 2 )|| i_refc == -2 ) { i_refc = h->mb.cache.ref[i_list][i8 - 8 - 1]; mv_c = h->mb.cache.mv[i_list][i8 - 8 - 1]; } if( h->mb.i_partition == D_16x8 ) { if( idx == 0 && i_refb == i_ref ) { mvp[0] = mv_b[0]; mvp[1] = mv_b[1]; return; } else if( idx != 0 && i_refa == i_ref ) { mvp[0] = mv_a[0]; mvp[1] = mv_a[1]; return; } } else if( h->mb.i_partition == D_8x16 ) { if( idx == 0 && i_refa == i_ref ) { mvp[0] = mv_a[0]; mvp[1] = mv_a[1]; return; } else if( idx != 0 && i_refc == i_ref ) { mvp[0] = mv_c[0]; mvp[1] = mv_c[1]; return; } } i_count = 0; if( i_refa == i_ref ) i_count++; if( i_refb == i_ref ) i_count++; if( i_refc == i_ref ) i_count++; if( i_count > 1 ) { mvp[0] = x264_median( mv_a[0], mv_b[0], mv_c[0] ); mvp[1] = x264_median( mv_a[1], mv_b[1], mv_c[1] ); } else if( i_count == 1 ) { if( i_refa == i_ref ) { mvp[0] = mv_a[0]; mvp[1] = mv_a[1]; } else if( i_refb == i_ref ) { mvp[0] = mv_b[0]; mvp[1] = mv_b[1]; } else { mvp[0] = mv_c[0]; mvp[1] = mv_c[1]; } } else if( i_refb == -2 && i_refc == -2 && i_refa != -2 ) { mvp[0] = mv_a[0]; mvp[1] = mv_a[1]; } else { mvp[0] = x264_median( mv_a[0], mv_b[0], mv_c[0] ); mvp[1] = x264_median( mv_a[1], mv_b[1], mv_c[1] ); }}void x264_mb_predict_mv_16x16( x264_t *h, int i_list, int i_ref, int mvp[2] ){ int i_refa = h->mb.cache.ref[i_list][X264_SCAN8_0 - 1]; int16_t *mv_a = h->mb.cache.mv[i_list][X264_SCAN8_0 - 1]; int i_refb = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8]; int16_t *mv_b = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8]; int i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 + 4]; int16_t *mv_c = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8 + 4]; int i_count; if( i_refc == -2 ) { i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 - 1]; mv_c = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8 - 1]; } i_count = 0; if( i_refa == i_ref ) i_count++; if( i_refb == i_ref ) i_count++; if( i_refc == i_ref ) i_count++; if( i_count > 1 ) { mvp[0] = x264_median( mv_a[0], mv_b[0], mv_c[0] ); mvp[1] = x264_median( mv_a[1], mv_b[1], mv_c[1] ); } else if( i_count == 1 ) { if( i_refa == i_ref ) { mvp[0] = mv_a[0]; mvp[1] = mv_a[1]; } else if( i_refb == i_ref ) { mvp[0] = mv_b[0]; mvp[1] = mv_b[1]; } else { mvp[0] = mv_c[0]; mvp[1] = mv_c[1]; } } else if( i_refb == -2 && i_refc == -2 && i_refa != -2 ) { mvp[0] = mv_a[0]; mvp[1] = mv_a[1]; } else { mvp[0] = x264_median( mv_a[0], mv_b[0], mv_c[0] ); mvp[1] = x264_median( mv_a[1], mv_b[1], mv_c[1] ); }}void x264_mb_predict_mv_pskip( x264_t *h, int mv[2] ){ int i_refa = h->mb.cache.ref[0][X264_SCAN8_0 - 1]; int i_refb = h->mb.cache.ref[0][X264_SCAN8_0 - 8]; int16_t *mv_a = h->mb.cache.mv[0][X264_SCAN8_0 - 1]; int16_t *mv_b = h->mb.cache.mv[0][X264_SCAN8_0 - 8]; if( i_refa == -2 || i_refb == -2 || ( i_refa == 0 && mv_a[0] == 0 && mv_a[1] == 0 ) || ( i_refb == 0 && mv_b[0] == 0 && mv_b[1] == 0 ) ) { mv[0] = mv[1] = 0; } else { x264_mb_predict_mv_16x16( h, 0, 0, mv ); }}static int x264_mb_predict_mv_direct16x16_temporal( x264_t *h ){ int i_mb_4x4 = 16 * h->mb.i_mb_stride * h->mb.i_mb_y + 4 * h->mb.i_mb_x; int i_mb_8x8 = 4 * h->mb.i_mb_stride * h->mb.i_mb_y + 2 * h->mb.i_mb_x; int i; x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, 0 ); if( IS_INTRA( h->fref1[0]->mb_type[ h->mb.i_mb_xy ] ) ) { x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, 0 ); x264_macroblock_cache_mv( h, 0, 0, 4, 4, 0, 0, 0 ); x264_macroblock_cache_mv( h, 0, 0, 4, 4, 1, 0, 0 ); return 1; } /* FIXME: optimize per block size */ for( i = 0; i < 4; i++ ) { const int x8 = 2*(i%2); const int y8 = 2*(i/2); const int i_part_8x8 = i_mb_8x8 + x8/2 + y8 * h->mb.i_mb_stride; const int i_ref = h->mb.map_col_to_list0[ h->fref1[0]->ref[0][ i_part_8x8 ] ];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -