📄 fast_me.c
字号:
/*!
************************************************************************
*
* \file fast_me.c
*
* \brief
* Fast integer pel motion estimation and fractional pel motion estimation
* algorithms are described in this file.
* 1. get_mem_FME() and free_mem_FME() are functions for allocation and release
* of memories about motion estimation
* 2. FME_BlockMotionSearch() is the function for fast integer pel motion
* estimation and fractional pel motion estimation
* 3. DefineThreshold() defined thresholds for early termination
* \author
* Main contributors: (see contributors.h for copyright, address and affiliation details)
* - Zhibo Chen <chenzhibo@tsinghua.org.cn>
* - JianFeng Xu <fenax@video.mdc.tsinghua.edu.cn>
* - Wenfang Fu <fwf@video.mdc.tsinghua.edu.cn>
* \date
* 2003.8
************************************************************************
*/
#include <stdlib.h>
#include <string.h>
#include "global.h"
#include "memalloc.h"
#include "fast_me.h"
#include "refbuf.h"
#define Q_BITS 15
extern unsigned int* byte_abs;
extern int* mvbits;
extern short* spiral_search_x;
extern short* spiral_search_y;
static pel_t *(*get_line) (pel_t**, int, int, int, int);
static const int quant_coef[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}}
};
void DefineThreshold()
{
AlphaSec[1] = 0.01f;
AlphaSec[2] = 0.01f;
AlphaSec[3] = 0.01f;
AlphaSec[4] = 0.02f;
AlphaSec[5] = 0.03f;
AlphaSec[6] = 0.03f;
AlphaSec[7] = 0.04f;
AlphaThird[1] = 0.06f;
AlphaThird[2] = 0.07f;
AlphaThird[3] = 0.07f;
AlphaThird[4] = 0.08f;
AlphaThird[5] = 0.12f;
AlphaThird[6] = 0.11f;
AlphaThird[7] = 0.15f;
DefineThresholdMB();
return;
}
void DefineThresholdMB()
{
int gb_qp_per = (input->qpN-MIN_QP)/6;
int gb_qp_rem = (input->qpN-MIN_QP)%6;
int gb_q_bits = Q_BITS+gb_qp_per;
int gb_qp_const,Thresh4x4;
float Quantize_step;
gb_qp_const=(1<<gb_q_bits)/6;
Thresh4x4 = ((1<<gb_q_bits) - gb_qp_const)/quant_coef[gb_qp_rem][0][0];
Quantize_step = Thresh4x4/(4*5.61f);
Bsize[7]=(16*16)*Quantize_step;
Bsize[6]=Bsize[7]*4;
Bsize[5]=Bsize[7]*4;
Bsize[4]=Bsize[5]*4;
Bsize[3]=Bsize[4]*4;
Bsize[2]=Bsize[4]*4;
Bsize[1]=Bsize[2]*4;
}
int get_mem_FME()
{
int memory_size = 0;
if (NULL==(flag_intra = calloc ((img->width>>4)+1,sizeof(byte)))) no_mem_exit("get_mem_FME: flag_intra"); //fwf 20050330
memory_size += get_mem2D(&McostState, 2*input->search_range+1, 2*input->search_range+1);
memory_size += get_mem4Dint(&(fastme_ref_cost), img->max_num_references, 9, 4, 4);
memory_size += get_mem3Dint(&(fastme_l0_cost), 9, img->height/4, img->width/4);
memory_size += get_mem3Dint(&(fastme_l1_cost), 9, img->height/4, img->width/4);
memory_size += get_mem2D(&SearchState,7,7);
return memory_size;
}
void free_mem_FME()
{
free_mem2D(McostState);
free_mem4Dint(fastme_ref_cost, img->max_num_references, 9);
free_mem3Dint(fastme_l0_cost, 9);
free_mem3Dint(fastme_l1_cost, 9);
free_mem2D(SearchState);
free (flag_intra);
}
int PartCalMad(pel_t *ref_pic,pel_t** orig_pic,pel_t *(*get_ref_line)(int, pel_t*, int, int, int, int), int blocksize_y,int blocksize_x, int blocksize_x4,int mcost,int min_mcost,int cand_x,int cand_y)
{
int y,x4;
int height=((img->MbaffFrameFlag)&&(img->mb_data[img->current_mb_nr].mb_field))?img->height/2:img->height;
pel_t *orig_line, *ref_line;
for (y=0; y<blocksize_y; y++)
{
ref_line = get_ref_line (blocksize_x, ref_pic, cand_y+y, cand_x, height, img->width);//2004.3.3
orig_line = orig_pic [y];
for (x4=0; x4<blocksize_x4; x4++)
{
mcost += byte_abs[ *orig_line++ - *ref_line++ ];
mcost += byte_abs[ *orig_line++ - *ref_line++ ];
mcost += byte_abs[ *orig_line++ - *ref_line++ ];
mcost += byte_abs[ *orig_line++ - *ref_line++ ];
}
if (mcost >= min_mcost)
{
break;
}
}
return mcost;
}
/*!
************************************************************************
* \brief
* FastIntegerPelBlockMotionSearch: fast pixel block motion search
* this algrithm is called UMHexagonS(see JVT-D016),which includes
* four steps with different kinds of search patterns
* \par Input:
* pel_t** orig_pic, // <-- original picture
* int ref, // <-- reference frame (0... or -1 (backward))
* int pic_pix_x, // <-- absolute x-coordinate of regarded AxB block
* int pic_pix_y, // <-- absolute y-coordinate of regarded AxB block
* int blocktype, // <-- block type (1-16x16 ... 7-4x4)
* int pred_mv_x, // <-- motion vector predictor (x) in sub-pel units
* int pred_mv_y, // <-- motion vector predictor (y) in sub-pel units
* int* mv_x, // --> motion vector (x) - in pel units
* int* mv_y, // --> motion vector (y) - in pel units
* int search_range, // <-- 1-d search range in pel units
* int min_mcost, // <-- minimum motion cost (cost for center or huge value)
* int lambda_factor // <-- lagrangian parameter for determining motion cost
* \par
* Three macro definitions defined in this program:
* 1. EARLY_TERMINATION: early termination algrithm, refer to JVT-D016.doc
* 2. SEARCH_ONE_PIXEL: search one pixel in search range
* 3. SEARCH_ONE_PIXEL1(value_iAbort): search one pixel in search range,
* but give a parameter to show if mincost refeshed
* \author
* Main contributors: (see contributors.h for copyright, address and affiliation details)
* - Zhibo Chen <chenzhibo@tsinghua.org.cn>
* - JianFeng Xu <fenax@video.mdc.tsinghua.edu.cn>
* \date :
* 2003.8
************************************************************************
*/
int // ==> minimum motion cost after search
FastIntegerPelBlockMotionSearch (pel_t** orig_pic, // <-- not used
short ref, // <-- reference frame (0... or -1 (backward))
int list,
int pic_pix_x, // <-- absolute x-coordinate of regarded AxB block
int pic_pix_y, // <-- absolute y-coordinate of regarded AxB block
int blocktype, // <-- block type (1-16x16 ... 7-4x4)
short pred_mv_x, // <-- motion vector predictor (x) in sub-pel units
short pred_mv_y, // <-- motion vector predictor (y) in sub-pel units
short* mv_x, // --> motion vector (x) - in pel units
short* mv_y, // --> motion vector (y) - in pel units
int search_range, // <-- 1-d search range in pel units
int min_mcost, // <-- minimum motion cost (cost for center or huge value)
int lambda_factor) // <-- lagrangian parameter for determining motion cost
{
static int Diamond_x[4] = {-1, 0, 1, 0};
static int Diamond_y[4] = {0, 1, 0, -1};
static int Hexagon_x[6] = {2, 1, -1, -2, -1, 1};
static int Hexagon_y[6] = {0, -2, -2, 0, 2, 2};
static int Big_Hexagon_x[16] = {0,-2, -4,-4,-4, -4, -4, -2, 0, 2, 4, 4, 4, 4, 4, 2};
static int Big_Hexagon_y[16] = {4, 3, 2, 1, 0, -1, -2, -3, -4, -3, -2, -1, 0, 1, 2, 3};
int pos, cand_x, cand_y, mcost;
pel_t *(*get_ref_line)(int, pel_t*, int, int, int, int);
int list_offset = ((img->MbaffFrameFlag)&&(img->mb_data[img->current_mb_nr].mb_field))? img->current_mb_nr%2 ? 4 : 2 : 0;
int mvshift = 2; // motion vector shift for getting sub-pel units
int blocksize_y = input->blc_size[blocktype][1]; // vertical block size
int blocksize_x = input->blc_size[blocktype][0]; // horizontal block size
int blocksize_x4 = blocksize_x >> 2; // horizontal block size in 4-pel units
int pred_x = (pic_pix_x << mvshift) + pred_mv_x; // predicted position x (in sub-pel units)
int pred_y = (pic_pix_y << mvshift) + pred_mv_y; // predicted position y (in sub-pel units)
int center_x = pic_pix_x + *mv_x; // center position x (in pel units)
int center_y = pic_pix_y + *mv_y; // center position y (in pel units)
int best_x = 0, best_y = 0;
int search_step,iYMinNow, iXMinNow;
int i,m;
int iAbort;
float betaSec,betaThird;
int height=((img->MbaffFrameFlag)&&(img->mb_data[img->current_mb_nr].mb_field))?img->height/2:img->height;
//===== Use weighted Reference for ME ====
pel_t* ref_pic;
int apply_weights = ( (active_pps->weighted_pred_flag && (img->type == P_SLICE || img->type == SP_SLICE)) ||
(active_pps->weighted_bipred_idc && (img->type == B_SLICE)));
if (apply_weights && input->UseWeightedReferenceME)
ref_pic = listX[list+list_offset][ref]->imgY_11_w;
else
ref_pic = listX[list+list_offset][ref]->imgY_11;
//===== set function for getting reference picture lines =====
if ((center_x > search_range) && (center_x < img->width -1-search_range-blocksize_x) &&
(center_y > search_range) && (center_y < height-1-search_range-blocksize_y) )
{
get_ref_line = FastLineX;
}
else
{
get_ref_line = UMVLineX;
}
//////allocate memory for search state//////////////////////////
memset(McostState[0],0,(2*input->search_range+1)*(2*input->search_range+1));
///////Threshold defined for early termination///////////////////
if(list==0 && ref>0)
{
if(pred_SAD_ref!=0)
{
betaSec = Bsize[blocktype]/(pred_SAD_ref*pred_SAD_ref)-AlphaSec[blocktype];
betaThird = Bsize[blocktype]/(pred_SAD_ref*pred_SAD_ref)-AlphaThird[blocktype];
}
else
{
betaSec = 0;
betaThird = 0;
}
}
else
{
if(blocktype==1)
{
if(pred_SAD_space !=0)
{
betaSec = Bsize[blocktype]/(pred_SAD_space*pred_SAD_space)-AlphaSec[blocktype];
betaThird = Bsize[blocktype]/(pred_SAD_space*pred_SAD_space)-AlphaThird[blocktype];
}
else
{
betaSec = 0;
betaThird = 0;
}
}
else
{
if(pred_SAD_uplayer !=0)
{
betaSec = Bsize[blocktype]/(pred_SAD_uplayer*pred_SAD_uplayer)-AlphaSec[blocktype];
betaThird = Bsize[blocktype]/(pred_SAD_uplayer*pred_SAD_uplayer)-AlphaThird[blocktype];
}
else
{
betaSec = 0;
betaThird = 0;
}
}
}
/*****************************/
//check the center median predictor
cand_x = center_x ;
cand_y = center_y ;
mcost = MV_COST (lambda_factor, mvshift, cand_x, cand_y, pred_x, pred_y);
mcost = PartCalMad(ref_pic, orig_pic, get_ref_line,blocksize_y,blocksize_x,blocksize_x4,mcost,min_mcost,cand_x,cand_y);
McostState[search_range][search_range] = 1;
if (mcost < min_mcost)
{
min_mcost = mcost;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -