📄 fast_me.c
字号:
/*
***********************************************************************
* COPYRIGHT AND WARRANTY INFORMATION
*
* Copyright 2001, International Telecommunications Union, Geneva
*
* DISCLAIMER OF WARRANTY
*
* These software programs are available to the user without any
* license fee or royalty on an "as is" basis. The ITU disclaims
* any and all warranties, whether express, implied, or
* statutory, including any implied warranties of merchantability
* or of fitness for a particular purpose. In no event shall the
* contributor or the ITU be liable for any incidental, punitive, or
* consequential damages of any kind whatsoever arising from the
* use of these programs.
*
* This disclaimer of warranty extends to the user of these programs
* and user's customers, employees, agents, transferees, successors,
* and assigns.
*
* The ITU does not represent or warrant that the programs furnished
* hereunder are free of infringement of any third-party patents.
* Commercial implementations of ITU-T Recommendations, including
* shareware, may be subject to royalty fees to patent holders.
* Information regarding the ITU-T patent policy is available from
* the ITU Web site at http://www.itu.int.
*
* THIS IS NOT A GRANT OF PATENT RIGHTS - SEE THE ITU-T PATENT POLICY.
************************************************************************
*/
/*!
************************************************************************
* \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
* \ 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
************************************************************************
*/
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <memory.h>
#include <assert.h>
#include "memalloc.h"
#include "fast_me.h"
#include "refbuf.h"
#include "mbuffer.h"
#include "image.h"
#ifdef _Fast_ME_
#define Q_BITS 15
extern int* byte_abs;
extern int* mvbits;
extern int* spiral_search_x;
extern int* spiral_search_y;
static pel_t (*PelY_14) (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()
{
static float ThresholdFac[8] = {0,8,4,4,2.5,1.5,1.5,1};
static int ThreshUp[8] = {0, 1024,512,512,448,384,384,384};
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;
if (img->type == I_SLICE)
gb_qp_const=(1<<gb_q_bits)/3; // intra
else
gb_qp_const=(1<<gb_q_bits)/6; // inter
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;
}
/*!
************************************************************************
* \brief
* Dynamic memory allocation of all infomation needed for Fast ME
* \par Input:
* \return Number of allocated bytes
* \Date: 2003/3
************************************************************************
*/
int get_mem_mincost (int****** mv)
{
int i, j, k, l;
if ((*mv = (int*****)calloc(input->img_width/4,sizeof(int****))) == NULL)
no_mem_exit ("get_mem_mv: mv");
for (i=0; i<input->img_width/4; i++)
{
if (((*mv)[i] = (int****)calloc(input->img_height/4,sizeof(int***))) == NULL)
no_mem_exit ("get_mem_mv: mv");
for (j=0; j<input->img_height/4; j++)
{
if (((*mv)[i][j] = (int***)calloc(img->max_num_references, sizeof(int**))) == NULL)
no_mem_exit ("get_mem_mv: mv");
for (k=0; k<img->max_num_references; k++)
{
if (((*mv)[i][j][k] = (int**)calloc(9,sizeof(int*))) == NULL)
no_mem_exit ("get_mem_mv: mv");
for (l=0; l<9; l++)
if (((*mv)[i][j][k][l] = (int*)calloc(3,sizeof(int))) == NULL)
no_mem_exit ("get_mem_mv: mv");
}
}
}
return input->img_width/4*input->img_height/4*img->max_num_references*9*3*sizeof(int);
}
/*!
*******************************************************************************
* \brief
* Dynamic memory allocation of all infomation needed for backward prediction
* \par Input:
* \return Number of allocated bytes
* \Date: 2003/3
*******************************************************************************
*/
int get_mem_bwmincost (int****** mv)
{
int i, j, k, l;
if ((*mv = (int*****)calloc(input->img_width/4,sizeof(int****))) == NULL)
no_mem_exit ("get_mem_mv: mv");
for (i=0; i<input->img_width/4; i++)
{
if (((*mv)[i] = (int****)calloc(input->img_height/4,sizeof(int***))) == NULL)
no_mem_exit ("get_mem_mv: mv");
for (j=0; j<input->img_height/4; j++)
{
if (((*mv)[i][j] = (int***)calloc(img->max_num_references,sizeof(int**))) == NULL)
no_mem_exit ("get_mem_mv: mv");
for (k=0; k<img->max_num_references; k++)
{
if (((*mv)[i][j][k] = (int**)calloc(9,sizeof(int*))) == NULL)
no_mem_exit ("get_mem_mv: mv");
for (l=0; l<9; l++)
if (((*mv)[i][j][k][l] = (int*)calloc(3,sizeof(int))) == NULL)
no_mem_exit ("get_mem_mv: mv");
}
}
}
return input->img_width/4*input->img_height/4*img->max_num_references*9*3*sizeof(int);
}
int get_mem_FME()
{
int memory_size = 0;
memory_size += get_mem2Dint(&McostState, 2*input->search_range+1, 2*input->search_range+1);
memory_size += get_mem_mincost (&(all_mincost));
memory_size += get_mem_bwmincost(&(all_bwmincost));
memory_size += get_mem2D(&SearchState,7,7);
return memory_size;
}
/*!
************************************************************************
* \brief
* free the memory allocated for of all infomation needed for Fast ME
* \par Input:
* \Date: 2003/3
************************************************************************
*/
void free_mem_mincost (int***** mv)
{
int i, j, k, l;
for (i=0; i<input->img_width/4; i++)
{
for (j=0; j<input->img_height/4; j++)
{
for (k=0; k<img->max_num_references; k++)
{
for (l=0; l<9; l++)
free (mv[i][j][k][l]);
free (mv[i][j][k]);
}
free (mv[i][j]);
}
free (mv[i]);
}
free (mv);
}
/*!
***********************************************************************************
* \brief
* free the memory allocated for of all infomation needed for backward prediction
* \par Input:
* \Date: 2003/3
***********************************************************************************
*/
void free_mem_bwmincost (int***** mv)
{
int i, j, k, l;
for (i=0; i<input->img_width/4; i++)
{
for (j=0; j<input->img_height/4; j++)
{
for (k=0; k<img->max_num_references; k++)
{
for (l=0; l<9; l++)
free (mv[i][j][k][l]);
free (mv[i][j][k]);
}
free (mv[i][j]);
}
free (mv[i]);
}
free (mv);
}
void free_mem_FME()
{
free_mem2Dint(McostState);
free_mem_mincost (all_mincost);
free_mem_bwmincost(all_bwmincost);
free_mem2D(SearchState);
}
void
FME_SetMotionVectorPredictor (int pmv[2],
int **refFrArray,
int ***tmp_mv,
int ref_frame,
int list,
int mb_x,
int mb_y,
int blockshape_x,
int blockshape_y,
int blocktype,
int ref)
{
int pic_block_x = img->block_x + (mb_x>>2);
int pic_block_y = img->block_y + (mb_y>>2);
int mb_nr = img->current_mb_nr;
int mb_width = img->width/16;
int mb_available_up = (img->mb_y == 0 ) ? 0 : (img->mb_data[mb_nr].slice_nr == img->mb_data[mb_nr-mb_width ].slice_nr);
int mb_available_left = (img->mb_x == 0 ) ? 0 : (img->mb_data[mb_nr].slice_nr == img->mb_data[mb_nr-1 ].slice_nr);
int mb_available_upleft = (img->mb_x == 0 ||
img->mb_y == 0 ) ? 0 : (img->mb_data[mb_nr].slice_nr == img->mb_data[mb_nr-mb_width-1].slice_nr);
int mb_available_upright = (img->mb_x >= mb_width-1 ||
img->mb_y == 0 ) ? 0 : (img->mb_data[mb_nr].slice_nr == img->mb_data[mb_nr-mb_width+1].slice_nr);
int block_available_up, block_available_left, block_available_upright, block_available_upleft;
int mv_a, mv_b, mv_c, mv_d, pred_vec=0;
int mvPredType, rFrameL, rFrameU, rFrameUR;
int hv;
//FAST MOTION ESTIMATION. ZHIBO CHEN 2003.3
int SAD_a, SAD_b, SAD_c, SAD_d;
int temp_pred_SAD[2];
pred_SAD_space = 0;
//REMARK: interlace can not work now.2004.3.3
if(input->MbInterlace && mb_adaptive && img->field_mode)
{
pic_block_y = (img->block_y>>1) + (mb_y>>2);
mb_available_up = (img->mb_y == 0 ) ? 0 : (img->mb_data[mb_nr].slice_nr == img->mb_data[mb_nr-mb_width ].slice_nr);
mb_available_left = (img->mb_x == 0 ) ? 0 : (img->mb_data[mb_nr].slice_nr == img->mb_data[mb_nr-1 ].slice_nr);
mb_available_upleft = (img->mb_x == 0 ||
img->mb_y == 0 ) ? 0 : (img->mb_data[mb_nr].slice_nr == img->mb_data[mb_nr-mb_width-1].slice_nr);
mb_available_upright = (img->mb_x >= mb_width-1 ||
img->mb_y == 0 ) ? 0 : (img->mb_data[mb_nr].slice_nr == img->mb_data[mb_nr-mb_width+1].slice_nr);
}
if(input->MbInterlace && mb_adaptive)
{
if(img->field_mode && !img->top_field)
mb_available_upright=0; // set mb_available_upright to 0 for bottom MBs in a MB pair
else if(!img->field_mode && img->mb_y%2)
mb_available_upright=0; // set mb_available_upright to 0 for bottom MBs in a MB pair
}
/* D B C */
/* A X */
/* 1 A, B, D are set to 0 if unavailable */
/* 2 If C is not available it is replaced by D */
block_available_up = mb_available_up || (mb_y > 0);
block_available_left = mb_available_left || (mb_x > 0);
if (mb_y > 0)
{
if (mb_x < 8) // first column of 8x8 blocks
{
if (mb_y==8)
{
if (blockshape_x == 16) block_available_upright = 0;
else block_available_upright = 1;
}
else
{
if (mb_x+blockshape_x != 8) block_available_upright = 1;
else block_available_upright = 0;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -