📄 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>
* - Xiaozhong Xu <xxz@video.mdc.tsinghua.edu.cn>
* \date
* 2006.1
************************************************************************
*/
#include <stdlib.h>
#include <string.h>
#include "global.h"
#include "limits.h"
#include "memalloc.h"
#include "fast_me.h"
#include "refbuf.h"
#include "mb_access.h"
#include "image.h"
#define Q_BITS 15
#define MIN_IMG_WIDTH 176
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 pel_t* ref_pic;
static pel_t *(*get_ref_line)(int, pel_t*, int, int, int, int);
static const int Diamond_x[4] = {-1, 0, 1, 0};
static const int Diamond_y[4] = {0, 1, 0, -1};
static const int Hexagon_x[6] = {2, 1, -1, -2, -1, 1};
static const int Hexagon_y[6] = {0, -2, -2, 0, 2, 2};
static const int Big_Hexagon_x[16] = {0,-2, -4,-4,-4, -4, -4, -2, 0, 2, 4, 4, 4, 4, 4, 2};
static const int Big_Hexagon_y[16] = {4, 3, 2, 1, 0, -1, -2, -3, -4, -3, -2, -1, 0, 1, 2, 3};
static const int Quater_x[8] = {2, 1, 0, -1, -2, -1, 0, 1};
static const int Quater_y[8] = {0, 1, 2, 1, 0, -1, -2, -1};
// for bipred mode
static int height,width;
static int pred_MV_ref_flag;
static short weightSpic, weightRpic, offsetBi;
int (*PartCalMadBiPred)(pel_t **, int, int, int, int, int, int, int, int, int);
static pel_t *(*get_ref_line1)(int, pel_t *, int, int, int, int);
static pel_t *(*get_ref_line2)(int, pel_t *, int, int, int, int);
static pel_t *ref1_pic;
static pel_t *ref2_pic;
static const int Multi_Ref_Thd[8] = {0, 300, 120, 120, 60, 30, 30, 15};
static const int Big_Hexagon_Thd[8] = {0, 3000, 1500, 1500, 800, 400, 400, 200};
static const int Median_Pred_Thd[8] = {0, 750, 350, 350, 170, 80, 80, 40};
static const int Threshold_DSR[8] = {0, 2200, 1000, 1000, 500, 250, 250, 120};
static int Median_Pred_Thd_MB[8];
static int Big_Hexagon_Thd_MB[8];
static int Multi_Ref_Thd_MB[8];
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()
{
AlphaFourth_1[1] = 0.01f;
AlphaFourth_1[2] = 0.01f;
AlphaFourth_1[3] = 0.01f;
AlphaFourth_1[4] = 0.02f;
AlphaFourth_1[5] = 0.03f;
AlphaFourth_1[6] = 0.03f;
AlphaFourth_1[7] = 0.04f;
AlphaFourth_2[1] = 0.06f;
AlphaFourth_2[2] = 0.07f;
AlphaFourth_2[3] = 0.07f;
AlphaFourth_2[4] = 0.08f;
AlphaFourth_2[5] = 0.12f;
AlphaFourth_2[6] = 0.11f;
AlphaFourth_2[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;
int i;
// scale factor: defined for different image sizes
float scale_factor = (float)((1-input->FMEScale*0.1)+input->FMEScale*0.1*(img->width/MIN_IMG_WIDTH));
// QP factor: defined for different quantization steps
float QP_factor = (float)((1.0-0.90*(input->qpN/51.0f)));
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)*2.0f*scale_factor;
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;
for(i=1;i<8;i++)
{
//ET_Thd1: early termination after median prediction
Median_Pred_Thd_MB[i] = (int) (Median_Pred_Thd[i]* scale_factor*QP_factor);
//ET_thd2: early termination after every circle of 16 points Big-Hex Search
Big_Hexagon_Thd_MB[i] = (int) (Big_Hexagon_Thd[i]* scale_factor*QP_factor);
//threshold for multi ref case
Multi_Ref_Thd_MB[i] = (int) (Multi_Ref_Thd[i] * scale_factor*QP_factor);
//threshold for usage of DSR technique. DSR ref to JVT-R088
Threshold_DSR_MB[i] = (int) (Threshold_DSR[i] * scale_factor*QP_factor);
}
}
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);
memory_size += get_mem2Dint(&(fastme_best_cost), 7, img->width/4);
if(input->BiPredMotionEstimation == 1)//memory allocation for bipred mode
{
memory_size += get_mem3Dint(&(fastme_l0_cost_bipred), 9, img->height/4, img->width/4);//for bipred
memory_size += get_mem3Dint(&(fastme_l1_cost_bipred), 9, img->height/4, img->width/4);//for bipred
}
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_mem2Dint(fastme_best_cost);
free (flag_intra);
if(input->BiPredMotionEstimation == 1)
{
free_mem3Dint(fastme_l0_cost_bipred, 9);//for bipred
free_mem3Dint(fastme_l1_cost_bipred, 9);//for bipred
}
}
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
* BiPred Mode SAD computation (without weights)
************************************************************************
*/
int PartCalMadBiPred1(pel_t** cur_pic,
int blocksize_y,
int blocksize_x,
int blocksize_x4,
int mcost,
int min_mcost,
int cand_x1, int cand_y1,
int cand_x2, int cand_y2)
{
pel_t *cur_line, *ref1_line, *ref2_line;
int bi_diff;
int y,x4;
for (y = 0; y < blocksize_y; y++)
{
ref2_line = get_ref_line2 (blocksize_x, ref2_pic, cand_y2 + y, cand_x2, height, width);
ref1_line = get_ref_line1 (blocksize_x, ref1_pic, cand_y1 + y, cand_x1, height, width);
cur_line = cur_pic [y];
for (x4 = 0; x4 < blocksize_x4; x4++)
{
bi_diff = (*cur_line++) - ((*ref1_line++ + *ref2_line++)>>1);
mcost += byte_abs[bi_diff];
bi_diff = (*cur_line++) - ((*ref1_line++ + *ref2_line++)>>1);
mcost += byte_abs[bi_diff];
bi_diff = (*cur_line++) - ((*ref1_line++ + *ref2_line++)>>1);
mcost += byte_abs[bi_diff];
bi_diff = (*cur_line++) - ((*ref1_line++ + *ref2_line++)>>1);
mcost += byte_abs[bi_diff];
}
if (mcost >= min_mcost) break;
}
return mcost;
}
/*!
************************************************************************
* \brief
* BiPred Mode SAD computation (with weights)
************************************************************************
*/
int PartCalMadBiPred2(pel_t** cur_pic,
int blocksize_y,
int blocksize_x,
int blocksize_x4,
int mcost,
int min_mcost,
int cand_x1, int cand_y1,
int cand_x2, int cand_y2)
{
pel_t *cur_line, *ref1_line, *ref2_line;
int bi_diff;
int denom = luma_log_weight_denom + 1;
int lround = 2 * wp_luma_round;
int y,x4;
int weightedpel, pixel1, pixel2;
for (y=0; y<blocksize_y; y++)
{
ref2_line = get_ref_line2 (blocksize_x, ref2_pic, cand_y2 + y, cand_x2, height, width);
ref1_line = get_ref_line1 (blocksize_x, ref1_pic, cand_y1 + y, cand_x1, height, width);
cur_line = cur_pic [y];
for (x4 = 0; x4 < blocksize_x4; x4++)
{
pixel1 = weightSpic * (*ref1_line++);
pixel2 = weightRpic * (*ref2_line++);
weightedpel = Clip3 (0, img->max_imgpel_value ,((pixel1 + pixel2 + lround) >> denom) + offsetBi);
bi_diff = (*cur_line++) - weightedpel;
mcost += byte_abs[bi_diff];
pixel1 = weightSpic * (*ref1_line++);
pixel2 = weightRpic * (*ref2_line++);
weightedpel = Clip3 (0, img->max_imgpel_value ,((pixel1 + pixel2 + lround) >> denom) + offsetBi);
bi_diff = (*cur_line++) - weightedpel;
mcost += byte_abs[bi_diff];
pixel1 = weightSpic * (*ref1_line++);
pixel2 = weightRpic * (*ref2_line++);
weightedpel = Clip3 (0, img->max_imgpel_value ,((pixel1 + pixel2 + lround) >> denom) + offsetBi);
bi_diff = (*cur_line++) - weightedpel;
mcost += byte_abs[bi_diff];
pixel1 = weightSpic * (*ref1_line++);
pixel2 = weightRpic * (*ref2_line++);
weightedpel = Clip3 (0, img->max_imgpel_value ,((pixel1 + pixel2 + lround) >> denom) + offsetBi);
bi_diff = (*cur_line++) - weightedpel;
mcost += byte_abs[bi_diff];
if (mcost >= min_mcost) break;
}
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
* Two 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
* \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>
* - Xiaozhong Xu <xxz@video.mdc.tsinghua.edu.cn>
* \date :
* 2006.1
************************************************************************
*/
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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -