📄 estimation_common.c
字号:
/*****************************************************************************
*
* XVID MPEG-4 VIDEO CODEC
* - Motion Estimation shared functions -
*
* Copyright(C) 2002 Christoph Lampert <gruel@web.de>
* 2002 Michael Militzer <michael@xvid.org>
* 2002-2003 Radoslaw Czyz <xvid@syskin.cjb.net>
*
* 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-1307 USA
*
* $Id: estimation_common.c,v 1.13 2005/12/09 04:39:49 syskin Exp $
*
****************************************************************************/
#include "../encoder.h"
#include "../global.h"
#include "../image/interpolate8x8.h"
#include "estimation.h"
#include "motion.h"
#include "sad.h"
#include "motion_inlines.h"
/*****************************************************************************
* Modified rounding tables
* Original tables see ISO spec tables 7-6 -> 7-9
****************************************************************************/
const uint32_t roundtab[16] =
{0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2 };
/* K = 4 */
const uint32_t roundtab_76[16] =
{ 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1 };
/* K = 2 */
const uint32_t roundtab_78[8] =
{ 0, 0, 1, 1, 0, 0, 0, 1 };
/* K = 1 */
const uint32_t roundtab_79[4] =
{ 0, 1, 0, 0 };
const int xvid_me_lambda_vec16[32] =
{ 0 ,(int)(1.0 * NEIGH_TEND_16X16 + 0.5),
(int)(2.0*NEIGH_TEND_16X16 + 0.5), (int)(3.0*NEIGH_TEND_16X16 + 0.5),
(int)(4.0*NEIGH_TEND_16X16 + 0.5), (int)(5.0*NEIGH_TEND_16X16 + 0.5),
(int)(6.0*NEIGH_TEND_16X16 + 0.5), (int)(7.0*NEIGH_TEND_16X16 + 0.5),
(int)(8.0*NEIGH_TEND_16X16 + 0.5), (int)(9.0*NEIGH_TEND_16X16 + 0.5),
(int)(10.0*NEIGH_TEND_16X16 + 0.5), (int)(11.0*NEIGH_TEND_16X16 + 0.5),
(int)(12.0*NEIGH_TEND_16X16 + 0.5), (int)(13.0*NEIGH_TEND_16X16 + 0.5),
(int)(14.0*NEIGH_TEND_16X16 + 0.5), (int)(15.0*NEIGH_TEND_16X16 + 0.5),
(int)(16.0*NEIGH_TEND_16X16 + 0.5), (int)(17.0*NEIGH_TEND_16X16 + 0.5),
(int)(18.0*NEIGH_TEND_16X16 + 0.5), (int)(19.0*NEIGH_TEND_16X16 + 0.5),
(int)(20.0*NEIGH_TEND_16X16 + 0.5), (int)(21.0*NEIGH_TEND_16X16 + 0.5),
(int)(22.0*NEIGH_TEND_16X16 + 0.5), (int)(23.0*NEIGH_TEND_16X16 + 0.5),
(int)(24.0*NEIGH_TEND_16X16 + 0.5), (int)(25.0*NEIGH_TEND_16X16 + 0.5),
(int)(26.0*NEIGH_TEND_16X16 + 0.5), (int)(27.0*NEIGH_TEND_16X16 + 0.5),
(int)(28.0*NEIGH_TEND_16X16 + 0.5), (int)(29.0*NEIGH_TEND_16X16 + 0.5),
(int)(30.0*NEIGH_TEND_16X16 + 0.5), (int)(31.0*NEIGH_TEND_16X16 + 0.5)
};
/*****************************************************************************
* Code
****************************************************************************/
int32_t
xvid_me_ChromaSAD(const int dx, const int dy, SearchData * const data)
{
int sad;
const uint32_t stride = data->iEdgedWidth/2;
int offset = (dx>>1) + (dy>>1)*stride;
int next = 1;
if (dx == data->chromaX && dy == data->chromaY)
return data->chromaSAD; /* it has been checked recently */
data->chromaX = dx; data->chromaY = dy; /* backup */
switch (((dx & 1) << 1) | (dy & 1)) {
case 0:
sad = sad8(data->CurU, data->RefP[4] + offset, stride);
sad += sad8(data->CurV, data->RefP[5] + offset, stride);
break;
case 1:
next = stride;
case 2:
sad = sad8bi(data->CurU, data->RefP[4] + offset, data->RefP[4] + offset + next, stride);
sad += sad8bi(data->CurV, data->RefP[5] + offset, data->RefP[5] + offset + next, stride);
break;
default:
interpolate8x8_halfpel_hv(data->RefQ, data->RefP[4] + offset, stride, data->rounding);
sad = sad8(data->CurU, data->RefQ, stride);
interpolate8x8_halfpel_hv(data->RefQ, data->RefP[5] + offset, stride, data->rounding);
sad += sad8(data->CurV, data->RefQ, stride);
break;
}
data->chromaSAD = sad; /* backup, part 2 */
return sad;
}
uint8_t *
xvid_me_interpolate8x8qpel(const int x, const int y, const uint32_t block, const uint32_t dir, const SearchData * const data)
{
/* create or find a qpel-precision reference picture; return pointer to it */
uint8_t * Reference = data->RefQ + 16*dir;
const uint32_t iEdgedWidth = data->iEdgedWidth;
const uint32_t rounding = data->rounding;
const int halfpel_x = x/2;
const int halfpel_y = y/2;
const uint8_t *ref1, *ref2, *ref3, *ref4;
ref1 = GetReferenceB(halfpel_x, halfpel_y, dir, data);
ref1 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth;
switch( ((x&1)<<1) + (y&1) ) {
case 3: /* x and y in qpel resolution - the "corners" (top left/right and */
/* bottom left/right) during qpel refinement */
ref2 = GetReferenceB(halfpel_x, y - halfpel_y, dir, data);
ref3 = GetReferenceB(x - halfpel_x, halfpel_y, dir, data);
ref4 = GetReferenceB(x - halfpel_x, y - halfpel_y, dir, data);
ref2 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth;
ref3 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth;
ref4 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth;
interpolate8x8_avg4(Reference, ref1, ref2, ref3, ref4, iEdgedWidth, rounding);
break;
case 1: /* x halfpel, y qpel - top or bottom during qpel refinement */
ref2 = GetReferenceB(halfpel_x, y - halfpel_y, dir, data);
ref2 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth;
interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding, 8);
break;
case 2: /* x qpel, y halfpel - left or right during qpel refinement */
ref2 = GetReferenceB(x - halfpel_x, halfpel_y, dir, data);
ref2 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth;
interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding, 8);
break;
default: /* pure halfpel position */
return (uint8_t *) ref1;
}
return Reference;
}
uint8_t *
xvid_me_interpolate16x16qpel(const int x, const int y, const uint32_t dir, const SearchData * const data)
{
/* create or find a qpel-precision reference picture; return pointer to it */
uint8_t * Reference = data->RefQ + 16*dir;
const uint32_t iEdgedWidth = data->iEdgedWidth;
const uint32_t rounding = data->rounding;
const int halfpel_x = x/2;
const int halfpel_y = y/2;
const uint8_t *ref1, *ref2, *ref3, *ref4;
ref1 = GetReferenceB(halfpel_x, halfpel_y, dir, data);
switch( ((x&1)<<1) + (y&1) ) {
case 3:
/*
* x and y in qpel resolution - the "corners" (top left/right and
* bottom left/right) during qpel refinement
*/
ref2 = GetReferenceB(halfpel_x, y - halfpel_y, dir, data);
ref3 = GetReferenceB(x - halfpel_x, halfpel_y, dir, data);
ref4 = GetReferenceB(x - halfpel_x, y - halfpel_y, dir, data);
interpolate8x8_avg4(Reference, ref1, ref2, ref3, ref4, iEdgedWidth, rounding);
interpolate8x8_avg4(Reference+8, ref1+8, ref2+8, ref3+8, ref4+8, iEdgedWidth, rounding);
interpolate8x8_avg4(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, ref3+8*iEdgedWidth, ref4+8*iEdgedWidth, iEdgedWidth, rounding);
interpolate8x8_avg4(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, ref3+8*iEdgedWidth+8, ref4+8*iEdgedWidth+8, iEdgedWidth, rounding);
break;
case 1: /* x halfpel, y qpel - top or bottom during qpel refinement */
ref2 = GetReferenceB(halfpel_x, y - halfpel_y, dir, data);
interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding, 8);
interpolate8x8_avg2(Reference+8, ref1+8, ref2+8, iEdgedWidth, rounding, 8);
interpolate8x8_avg2(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, rounding, 8);
interpolate8x8_avg2(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, rounding, 8);
break;
case 2: /* x qpel, y halfpel - left or right during qpel refinement */
ref2 = GetReferenceB(x - halfpel_x, halfpel_y, dir, data);
interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding, 8);
interpolate8x8_avg2(Reference+8, ref1+8, ref2+8, iEdgedWidth, rounding, 8);
interpolate8x8_avg2(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, iEdgedWidth, rounding, 8);
interpolate8x8_avg2(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, rounding, 8);
break;
default: /* pure halfpel position */
return (uint8_t *) ref1;
}
return Reference;
}
void
xvid_me_AdvDiamondSearch(int x, int y, SearchData * const data,
int bDirection, CheckFunc * const CheckCandidate)
{
/* directions: 1 - left (x-1); 2 - right (x+1), 4 - up (y-1); 8 - down (y+1) */
unsigned int * const iDirection = &data->dir;
for(;;) { /* forever */
*iDirection = 0;
if (bDirection & 1) CHECK_CANDIDATE(x - iDiamondSize, y, 1);
if (bDirection & 2) CHECK_CANDIDATE(x + iDiamondSize, y, 2);
if (bDirection & 4) CHECK_CANDIDATE(x, y - iDiamondSize, 4);
if (bDirection & 8) CHECK_CANDIDATE(x, y + iDiamondSize, 8);
/* now we're doing diagonal checks near our candidate */
if (*iDirection) { /* if anything found */
bDirection = *iDirection;
*iDirection = 0;
x = data->currentMV->x; y = data->currentMV->y;
if (bDirection & 3) { /* our candidate is left or right */
CHECK_CANDIDATE(x, y + iDiamondSize, 8);
CHECK_CANDIDATE(x, y - iDiamondSize, 4);
} else { /* what remains here is up or down */
CHECK_CANDIDATE(x + iDiamondSize, y, 2);
CHECK_CANDIDATE(x - iDiamondSize, y, 1);
}
if (*iDirection) {
bDirection += *iDirection;
x = data->currentMV->x; y = data->currentMV->y;
}
} else { /* about to quit, eh? not so fast.... */
switch (bDirection) {
case 2:
CHECK_CANDIDATE(x + iDiamondSize, y - iDiamondSize, 2 + 4);
CHECK_CANDIDATE(x + iDiamondSize, y + iDiamondSize, 2 + 8);
break;
case 1:
CHECK_CANDIDATE(x - iDiamondSize, y - iDiamondSize, 1 + 4);
CHECK_CANDIDATE(x - iDiamondSize, y + iDiamondSize, 1 + 8);
break;
case 2 + 4:
CHECK_CANDIDATE(x - iDiamondSize, y - iDiamondSize, 1 + 4);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -