📄 edge-detec.c.svn-base
字号:
/***************************************************************************** * macroblock.c: h264 encoder library ***************************************************************************** * Copyright (C) 2003 Laurent Aimar * $Id: edge-detec.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 <math.h>#include "common.h"#include "me.h"#include "vlc.h"static inline int x264_median( int a, int b, int c ){ int min = a, max =a; if( b < min ) { min = b; } else { max = b; /* no need to do 'b > max' (more consuming than always doing affectation) */ } if( c < min ) { min = c; } else if( c > max ) { max = c; } return a + b + c - min - max;}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 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} }};static int predict_pred_intra4x4_mode( x264_t *h, x264_macroblock_t *mb, int idx ){ x264_macroblock_t *mba = mb->context->block[idx].mba; x264_macroblock_t *mbb = mb->context->block[idx].mbb; int i_mode_a = I_PRED_4x4_DC; int i_mode_b = I_PRED_4x4_DC; if( !mba || !mbb ) { return I_PRED_4x4_DC; } if( mba->i_type == I_4x4 ) { i_mode_a = mb->context->block[idx].bka->i_intra4x4_pred_mode; } if( mbb->i_type == I_4x4 ) { i_mode_b = mb->context->block[idx].bkb->i_intra4x4_pred_mode; } return X264_MIN( i_mode_a, i_mode_b );}static int predict_non_zero_code( x264_t *h, x264_macroblock_t *mb, int idx ){ x264_macroblock_t *mba = mb->context->block[idx].mba; x264_macroblock_t *mbb = mb->context->block[idx].mbb; int i_z_a = 0x80, i_z_b = 0x80; int i_ret; /* none avail -> 0, one avail -> this one, both -> (a+b+1)>>1 */ if( mba ) { i_z_a = mb->context->block[idx].bka->i_non_zero_count; } if( mbb ) { i_z_b = mb->context->block[idx].bkb->i_non_zero_count; } i_ret = i_z_a+i_z_b; if( i_ret < 0x80 ) { i_ret = ( i_ret + 1 ) >> 1; } return i_ret & 0x7f;}/* * Handle intra mb *//* Max = 4 */static void predict_16x16_mode_available( x264_macroblock_t *mb, int *mode, int *pi_count ){ if( ( mb->i_neighbour & (MB_LEFT|MB_TOP) ) == (MB_LEFT|MB_TOP) ) { /* top and left avaible */ *mode++ = I_PRED_16x16_DC; *mode++ = I_PRED_16x16_V; *mode++ = I_PRED_16x16_H; *mode++ = I_PRED_16x16_P; *pi_count = 4; } else if( ( mb->i_neighbour & MB_LEFT ) ) { /* left available*/ *mode++ = I_PRED_16x16_DC_LEFT; *mode++ = I_PRED_16x16_H; *pi_count = 2; } else if( ( mb->i_neighbour & MB_TOP ) ) { /* top available*/ *mode++ = I_PRED_16x16_DC_TOP; *mode++ = I_PRED_16x16_V; *pi_count = 2; } else { /* none avaible */ *mode = I_PRED_16x16_DC_128; *pi_count = 1; }}/* Max = 4 */static void predict_8x8_mode_available( x264_macroblock_t *mb, int *mode, int *pi_count ){ if( ( mb->i_neighbour & (MB_LEFT|MB_TOP) ) == (MB_LEFT|MB_TOP) ) { /* top and left avaible */ *mode++ = I_PRED_CHROMA_DC; *mode++ = I_PRED_CHROMA_V; *mode++ = I_PRED_CHROMA_H; *mode++ = I_PRED_CHROMA_P; *pi_count = 4; } else if( ( mb->i_neighbour & MB_LEFT ) ) { /* left available*/ *mode++ = I_PRED_CHROMA_DC_LEFT; *mode++ = I_PRED_CHROMA_H; *pi_count = 2; } else if( ( mb->i_neighbour & MB_TOP ) ) { /* top available*/ *mode++ = I_PRED_CHROMA_DC_TOP; *mode++ = I_PRED_CHROMA_V; *pi_count = 2; } else { /* none avaible */ *mode = I_PRED_CHROMA_DC_128; *pi_count = 1; }}/* MAX = 8 */static void predict_4x4_mode_available( x264_macroblock_t *mb, int idx, int *mode, int *pi_count ){ int b_a, b_b, b_c; static const int needmb[16] = { MB_LEFT|MB_TOP, MB_TOP, MB_LEFT, MB_PRIVATE, MB_TOP, MB_TOP|MB_TOPRIGHT, 0, MB_PRIVATE, MB_LEFT, 0, MB_LEFT, MB_PRIVATE, 0, MB_PRIVATE, 0, MB_PRIVATE }; /* FIXME even when b_c == 0 there is some case where missing pixels * are emulated and thus more mode are available TODO * analysis and encode should be fixed too */ b_a = (needmb[idx]&mb->i_neighbour&MB_LEFT) == (needmb[idx]&MB_LEFT); b_b = (needmb[idx]&mb->i_neighbour&MB_TOP) == (needmb[idx]&MB_TOP); b_c = (needmb[idx]&mb->i_neighbour&(MB_TOPRIGHT|MB_PRIVATE)) == (needmb[idx]&(MB_TOPRIGHT|MB_PRIVATE)); if( b_a && b_b ) { *mode++ = I_PRED_4x4_DC; *mode++ = I_PRED_4x4_H; *mode++ = I_PRED_4x4_V; *mode++ = I_PRED_4x4_DDR; *mode++ = I_PRED_4x4_VR; *mode++ = I_PRED_4x4_HD; *mode++ = I_PRED_4x4_HU; *pi_count = 7; if( b_c ) { *mode++ = I_PRED_4x4_DDL; *mode++ = I_PRED_4x4_VL; (*pi_count) += 2; } } else if( b_a && !b_b ) { *mode++ = I_PRED_4x4_DC_LEFT; *mode++ = I_PRED_4x4_H; *pi_count = 2; } else if( !b_a && b_b ) { *mode++ = I_PRED_4x4_DC_TOP; *mode++ = I_PRED_4x4_V; *pi_count = 2; } else { *mode++ = I_PRED_4x4_DC_128; *pi_count = 1; }}/**************************************************************************** * 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];}static void quant_4x4( int16_t dct[4][4], int i_qscale, int b_intra ){ int i_qbits = 15 + i_qscale / 6; int i_mf = i_qscale % 6; 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 + (int64_t)dct[y][x] * (int64_t)quant_mf[i_mf][y][x] ) >> i_qbits; } else { dct[y][x] = - ( ( f - (int64_t)dct[y][x] * (int64_t)quant_mf[i_mf][y][x] ) >> i_qbits ); } } }}static void quant_4x4_dc( int16_t dct[4][4], int i_qscale, int b_intra ){ int i_qbits = 15 + i_qscale / 6; int i_mf = i_qscale % 6; 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] =( 2*f + (int64_t)dct[y][x] * (int64_t)quant_mf[i_mf][0][0] ) >> ( 1 + i_qbits ); } else { dct[y][x] = - ( ( 2*f - (int64_t)dct[y][x] * (int64_t)quant_mf[i_mf][0][0] ) >> (1 + i_qbits ) ); } } }}static void quant_2x2_dc( int16_t dct[2][2], int i_qscale, int b_intra ){ int i_qbits = 15 + i_qscale / 6; int i_mf = i_qscale % 6; int f = ( 1 << i_qbits ) / ( b_intra ? 3 : 6 ); int x,y; for( y = 0; y < 2; y++ ) { for( x = 0; x < 2; x++ ) { /* XXX: is int64_t really needed ? */ if( dct[y][x] > 0 ) { dct[y][x] =( 2*f + (int64_t)dct[y][x] * (int64_t)quant_mf[i_mf][0][0] ) >> ( 1 + i_qbits ); } else { dct[y][x] = - ( ( 2*f - (int64_t)dct[y][x] * (int64_t)quant_mf[i_mf][0][0] ) >> (1 + i_qbits ) ); } } }}static void dequant_4x4_dc( int16_t dct[4][4], int i_qscale ){ int i_mf = i_qscale%6; int i_qbits = i_qscale/6; int f; int x,y; if( i_qbits <= 1 ) { f = 1 << ( 1 - i_qbits ); } else { f = 0; } for( y = 0; y < 4; y++ ) { for( x = 0; x < 4; x++ ) { if( i_qbits >= 2 ) { dct[y][x] = ( dct[y][x] * dequant_mf[i_mf][0][0] ) << (i_qbits - 2); } else { dct[y][x] = ( dct[y][x] * dequant_mf[i_mf][0][0] + f ) >> ( 2 -i_qbits ); } } }}static void dequant_2x2_dc( int16_t dct[2][2], int i_qscale ){ int i_mf = i_qscale%6; int i_qbits = i_qscale/6; int x,y; for( y = 0; y < 2; y++ ) { for( x = 0; x < 2; x++ ) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -