📄 macroblock-dz.c
字号:
/***************************************************************************** * macroblock.c: h264 encoder library ***************************************************************************** * Copyright (C) 2003 Laurent Aimar * $Id: macroblock-dz.c,v 1.1 2004/06/03 19:27:08 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 <stdint.h>#include "../common/common.h"#include "../common/vlc.h"#include "macroblock.h"static const uint8_t intra4x4_cbp_to_golomb[48]={ 3, 29, 30, 17, 31, 18, 37, 8, 32, 38, 19, 9, 20, 10, 11, 2, 16, 33, 34, 21, 35, 22, 39, 4, 36, 40, 23, 5, 24, 6, 7, 1, 41, 42, 43, 25, 44, 26, 46, 12, 45, 47, 27, 13, 28, 14, 15, 0};static const uint8_t inter_cbp_to_golomb[48]={ 0, 2, 3, 7, 4, 8, 17, 13, 5, 18, 9, 14, 10, 15, 16, 11, 1, 32, 33, 36, 34, 37, 44, 40, 35, 45, 38, 41, 39, 42, 43, 19, 6, 24, 25, 20, 26, 21, 46, 28, 27, 47, 22, 29, 23, 30, 31, 12};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 quant_mf[6][4][4] ={ { { 13107, 8066, 13107, 8066}, { 8066, 5243, 8066, 5243}, { 13107, 8066, 13107, 8066}, { 8066, 5243, 8066, 5243} }, { { 11916, 7490, 11916, 7490}, { 7490, 4660, 7490, 4660}, { 11916, 7490, 11916, 7490}, { 7490, 4660, 7490, 4660} }, { { 10082, 6554, 10082, 6554}, { 6554, 4194, 6554, 4194}, { 10082, 6554, 10082, 6554}, { 6554, 4194, 6554, 4194} }, { { 9362, 5825, 9362, 5825}, { 5825, 3647, 5825, 3647}, { 9362, 5825, 9362, 5825}, { 5825, 3647, 5825, 3647} }, { { 8192, 5243, 8192, 5243}, { 5243, 3355, 5243, 3355}, { 8192, 5243, 8192, 5243}, { 5243, 3355, 5243, 3355} }, { { 7282, 4559, 7282, 4559}, { 4559, 2893, 4559, 2893}, { 7282, 4559, 7282, 4559}, { 4559, 2893, 4559, 2893} }};static 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};static const int f_deadzone_intra[4][4][2] = /* [num][den] */{ { {1,2}, {3,7}, {2,5}, {1,3} }, { {3,7}, {2,5}, {1,3}, {1,4} }, { {2,5}, {1,3}, {1,4}, {1,5} }, { {1,3}, {1,4}, {1,5}, {1,5} }};static const int f_deadzone_inter[4][4][2] = /* [num][den] */{ { {1,3}, {2,7}, {4,15},{2,9} }, { {2,7}, {4,15},{2,9}, {1,6} }, { {4,15},{2,9}, {1,6}, {1,7} }, { {2,9}, {1,6}, {1,7}, {2,15} }};/**************************************************************************** * Scan and Quant functions ****************************************************************************/static const int scan_zigzag_x[16]={0, 1, 0, 0, 1, 2, 3, 2, 1, 0, 1, 2, 3, 3, 2, 3};static const int scan_zigzag_y[16]={0, 0, 1, 2, 1, 0, 0, 1, 2, 3, 3, 2, 1, 2, 3, 3};static inline void scan_zigzag_4x4full( int level[16], int16_t dct[4][4] ){ int i; for( i = 0; i < 16; i++ ) { level[i] = dct[scan_zigzag_y[i]][scan_zigzag_x[i]]; }}static inline void scan_zigzag_4x4( int level[15], int16_t dct[4][4] ){ int i; for( i = 1; i < 16; i++ ) { level[i - 1] = dct[scan_zigzag_y[i]][scan_zigzag_x[i]]; }}static inline void scan_zigzag_2x2_dc( int level[4], int16_t dct[2][2] ){ level[0] = dct[0][0]; level[1] = dct[0][1]; level[2] = dct[1][0]; level[3] = dct[1][1];}#if 0static void quant_4x4( int16_t dct[4][4], int i_qscale, int b_intra ){ const int i_qbits = 15 + i_qscale / 6; const int i_mf = i_qscale % 6; const int f = ( 1 << i_qbits ) / ( b_intra ? 3 : 6 ); int x,y; for( y = 0; y < 4; y++ ) { for( x = 0; x < 4; x++ ) { if( dct[y][x] > 0 ) { dct[y][x] =( f + dct[y][x] * quant_mf[i_mf][y][x] ) >> i_qbits; } else { dct[y][x] = - ( ( f - dct[y][x] * quant_mf[i_mf][y][x] ) >> i_qbits ); } } }}static void quant_4x4_dc( int16_t dct[4][4], int i_qscale ){ const int i_qbits = 15 + i_qscale / 6; const int f2 = ( 2 << i_qbits ) / 3; const int i_qmf = quant_mf[i_qscale%6][0][0]; int x,y; for( y = 0; y < 4; y++ ) { for( x = 0; x < 4; x++ ) { if( dct[y][x] > 0 ) { dct[y][x] =( f2 + dct[y][x] * i_qmf) >> ( 1 + i_qbits ); } else { dct[y][x] = - ( ( f2 - dct[y][x] * i_qmf ) >> (1 + i_qbits ) ); } } }}static void quant_2x2_dc( int16_t dct[2][2], int i_qscale, int b_intra ){ int const i_qbits = 15 + i_qscale / 6; const int f2 = ( 2 << i_qbits ) / ( b_intra ? 3 : 6 ); const int i_qmf = quant_mf[i_qscale%6][0][0]; int x,y; for( y = 0; y < 2; y++ ) { for( x = 0; x < 2; x++ ) { if( dct[y][x] > 0 ) { dct[y][x] =( f2 + dct[y][x] * i_qmf) >> ( 1 + i_qbits ); } else { dct[y][x] = - ( ( f2 - dct[y][x] * i_qmf ) >> (1 + i_qbits ) ); } } }}#endifstatic void quant_4x4( int16_t dct[4][4], int i_qscale, int b_intra ){ const int i_qbits = 15 + i_qscale / 6; const int i_mf = i_qscale % 6; int x,y; for( y = 0; y < 4; y++ ) { for( x = 0; x < 4; x++ ) { const int f = b_intra ? (f_deadzone_intra[y][x][0] * ( 1 << i_qbits ) / f_deadzone_intra[y][x][1]) : (f_deadzone_inter[y][x][0] * ( 1 << i_qbits ) / f_deadzone_inter[y][x][1]); if( dct[y][x] > 0 ) { dct[y][x] =( f + dct[y][x] * quant_mf[i_mf][y][x] ) >> i_qbits; } else { dct[y][x] = - ( ( f - dct[y][x] * quant_mf[i_mf][y][x] ) >> i_qbits ); } } }}static void quant_4x4_dc( int16_t dct[4][4], int i_qscale ){ const int i_qbits = 15 + i_qscale / 6; const int i_qmf = quant_mf[i_qscale%6][0][0]; const int f2 = f_deadzone_intra[0][0][0] * ( 2 << i_qbits ) / f_deadzone_intra[0][0][1]; int x,y; for( y = 0; y < 4; y++ ) { for( x = 0; x < 4; x++ ) { if( dct[y][x] > 0 ) { dct[y][x] =( f2 + dct[y][x] * i_qmf) >> ( 1 + i_qbits ); } else { dct[y][x] = - ( ( f2 - dct[y][x] * i_qmf ) >> (1 + i_qbits ) ); } } }}static void quant_2x2_dc( int16_t dct[2][2], int i_qscale, int b_intra ){ int const i_qbits = 15 + i_qscale / 6; const int i_qmf = quant_mf[i_qscale%6][0][0]; const int f2 = b_intra ? (f_deadzone_intra[0][0][0] * ( 2 << i_qbits ) / f_deadzone_intra[0][0][1]) : (f_deadzone_inter[0][0][0] * ( 2 << i_qbits ) / f_deadzone_inter[0][0][1]); int x,y; for( y = 0; y < 2; y++ ) { for( x = 0; x < 2; x++ ) { if( dct[y][x] > 0 ) { dct[y][x] =( f2 + dct[y][x] * i_qmf) >> ( 1 + i_qbits ); } else { dct[y][x] = - ( ( f2 - dct[y][x] * i_qmf ) >> (1 + i_qbits ) ); } } }}static inline int array_non_zero_count( int *v, int i_count ){ int i; int i_nz; for( i = 0, i_nz = 0; i < i_count; i++ ) { if( v[i] ) { i_nz++; } } return i_nz;}void x264_mb_partition_mvd( x264_macroblock_t *mb, int i_list, int i_part, int i_sub, int mvd[2]){ int mvp[2]; int x, y; int w, h; int dx, dy; x264_mb_partition_getxy( mb, i_part, i_sub, &x, &y ); x264_mb_partition_size ( mb, i_part, i_sub, &w, &h ); x264_mb_predict_mv( mb, i_list, i_part, i_sub, mvp ); mvd[0] = mb->partition[x][y].mv[i_list][0] - mvp[0]; mvd[1] = mb->partition[x][y].mv[i_list][1] - mvp[1]; for( dx = 0; dx < w; dx++ ) { for( dy = 0; dy < h; dy++ ) { mb->partition[x+dx][y+dy].mvd[i_list][0] = mvd[0]; mb->partition[x+dx][y+dy].mvd[i_list][1] = mvd[1]; } }}/* (ref: JVT-B118) * x264_mb_decimate_score: given dct coeffs it returns a score to see if we could empty this dct coeffs * to 0 (low score means set it to null) * Used in inter macroblock (luma and chroma) * luma: for a 8x8 block: if score < 4 -> null * for the complete mb: if score < 6 -> null * chroma: for the complete mb: if score < 7 -> null */static int x264_mb_decimate_score( int *dct, int i_max ){ static const int i_ds_table[16] = { 3, 2, 2, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; int i_score = 0; int idx = i_max - 1; while( idx >= 0 && dct[idx] == 0 ) { idx--; } while( idx >= 0 ) { int i_run; if( abs( dct[idx--] ) > 1 ) { return 9; } i_run = 0; while( idx >= 0 && dct[idx] == 0 ) { idx--; i_run++; } i_score += i_ds_table[i_run]; } return i_score;}void x264_mb_encode_i4x4( x264_t *h, x264_macroblock_t *mb, int idx, int i_qscale ){ x264_mb_context_t *ctx = mb->context; uint8_t *p_src = ctx->p_img[0] + 4 * block_idx_x[idx] + 4 * block_idx_y[idx] * ctx->i_img[0]; int i_src = ctx->i_img[0]; uint8_t *p_dst = ctx->p_fdec[0] + 4 * block_idx_x[idx] + 4 * block_idx_y[idx] * ctx->i_fdec[0]; int i_dst = ctx->i_fdec[0]; int16_t luma[4][4]; int16_t dct4x4[4][4]; /* we calculate diff */ h->pixf.sub4x4( luma, p_src, i_src, p_dst, i_dst ); /* calculate dct coeffs */ h->dctf.dct4x4( dct4x4, luma ); quant_4x4( dct4x4, i_qscale, 1 ); scan_zigzag_4x4full( mb->block[idx].luma4x4, dct4x4 ); /* output samples to fdec */ x264_mb_dequant_4x4( dct4x4, i_qscale ); h->dctf.idct4x4( luma, dct4x4 ); /* put pixel to fdec */ h->pixf.add4x4( p_dst, i_dst, luma );}static void x264_mb_encode_i16x16( x264_t *h, x264_macroblock_t *mb, int i_qscale ){ x264_mb_context_t *ctx = mb->context; uint8_t *p_src = ctx->p_img[0]; int i_src = ctx->i_img[0]; uint8_t *p_dst = ctx->p_fdec[0]; int i_dst = ctx->i_fdec[0]; int16_t luma[16][4][4]; int16_t dct4x4[16+1][4][4]; int i; /* calculate the diff */ h->pixf.sub16x16( luma, p_src, i_src, p_dst, i_dst ); /* calculate dct coeffs */ for( i = 0; i < 16; i++ ) { h->dctf.dct4x4( dct4x4[i+1], luma[i] ); /* copy dc coeff */ dct4x4[0][block_idx_y[i]][block_idx_x[i]] = dct4x4[1+i][0][0]; quant_4x4( dct4x4[1+i], i_qscale, 1 ); scan_zigzag_4x4( mb->block[i].residual_ac, dct4x4[1+i] ); } h->dctf.dct4x4dc( dct4x4[0], dct4x4[0] ); quant_4x4_dc( dct4x4[0], i_qscale ); scan_zigzag_4x4full( mb->luma16x16_dc, dct4x4[0] ); /* output samples to fdec */ h->dctf.idct4x4dc( dct4x4[0], dct4x4[0] ); x264_mb_dequant_4x4_dc( dct4x4[0], i_qscale ); /* XXX not inversed */ /* calculate dct coeffs */ for( i = 0; i < 16; i++ ) { x264_mb_dequant_4x4( dct4x4[1+i], i_qscale ); /* copy dc coeff */ dct4x4[1+i][0][0] = dct4x4[0][block_idx_y[i]][block_idx_x[i]]; h->dctf.idct4x4( luma[i], dct4x4[i+1] ); } /* put pixels to fdec */ h->pixf.add16x16( p_dst, i_dst, luma );}static void x264_mb_encode_8x8( x264_t *h, x264_macroblock_t *mb, int b_inter, int i_qscale ){ x264_mb_context_t *ctx = mb->context; uint8_t *p_src, *p_dst; int i_src, i_dst; int i, ch; int i_decimate_score = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -