📄 estimation_pvop.c
字号:
/***************************************************************************** * * XVID MPEG-4 VIDEO CODEC * - Motion Estimation for P- and S- VOPs - * * 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$ * ****************************************************************************/#include <assert.h>#include <stdio.h>#include <stdlib.h>#include <string.h> /* memcpy */#include "../encoder.h"#include "../prediction/mbprediction.h"#include "../global.h"#include "../utils/timer.h"#include "../image/interpolate8x8.h"#include "estimation.h"#include "motion.h"#include "sad.h"#include "motion_inlines.h"static const int xvid_me_lambda_vec8[32] = { 0 ,(int)(1.00235 * NEIGH_TEND_8X8 + 0.5), (int)(1.15582*NEIGH_TEND_8X8 + 0.5), (int)(1.31976*NEIGH_TEND_8X8 + 0.5), (int)(1.49591*NEIGH_TEND_8X8 + 0.5), (int)(1.68601*NEIGH_TEND_8X8 + 0.5), (int)(1.89187*NEIGH_TEND_8X8 + 0.5), (int)(2.11542*NEIGH_TEND_8X8 + 0.5), (int)(2.35878*NEIGH_TEND_8X8 + 0.5), (int)(2.62429*NEIGH_TEND_8X8 + 0.5), (int)(2.91455*NEIGH_TEND_8X8 + 0.5), (int)(3.23253*NEIGH_TEND_8X8 + 0.5), (int)(3.58158*NEIGH_TEND_8X8 + 0.5), (int)(3.96555*NEIGH_TEND_8X8 + 0.5), (int)(4.38887*NEIGH_TEND_8X8 + 0.5), (int)(4.85673*NEIGH_TEND_8X8 + 0.5), (int)(5.37519*NEIGH_TEND_8X8 + 0.5), (int)(5.95144*NEIGH_TEND_8X8 + 0.5), (int)(6.59408*NEIGH_TEND_8X8 + 0.5), (int)(7.31349*NEIGH_TEND_8X8 + 0.5), (int)(8.12242*NEIGH_TEND_8X8 + 0.5), (int)(9.03669*NEIGH_TEND_8X8 + 0.5), (int)(10.0763*NEIGH_TEND_8X8 + 0.5), (int)(11.2669*NEIGH_TEND_8X8 + 0.5), (int)(12.6426*NEIGH_TEND_8X8 + 0.5), (int)(14.2493*NEIGH_TEND_8X8 + 0.5), (int)(16.1512*NEIGH_TEND_8X8 + 0.5), (int)(18.442*NEIGH_TEND_8X8 + 0.5), (int)(21.2656*NEIGH_TEND_8X8 + 0.5), (int)(24.8580*NEIGH_TEND_8X8 + 0.5), (int)(29.6436*NEIGH_TEND_8X8 + 0.5), (int)(36.4949*NEIGH_TEND_8X8 + 0.5)};static voidCheckCandidate16(const int x, const int y, SearchData * const data, const unsigned int Direction){ const uint8_t * Reference; int32_t sad; uint32_t t; if ( (x > data->max_dx) || (x < data->min_dx) || (y > data->max_dy) || (y < data->min_dy) ) return; Reference = GetReference(x, y, data); sad = sad16v(data->Cur, Reference, data->iEdgedWidth, data->temp); t = d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel, 0); sad += (data->lambda16 * t * sad)>>10; data->temp[0] += (data->lambda8 * t * (data->temp[0] + NEIGH_8X8_BIAS))>>10; if (data->chroma) { if (sad >= data->iMinSAD[0]) goto no16; sad += xvid_me_ChromaSAD((x >> 1) + roundtab_79[x & 0x3], (y >> 1) + roundtab_79[y & 0x3], data); } if (sad < data->iMinSAD[0]) { data->iMinSAD[0] = sad; data->currentMV[0].x = x; data->currentMV[0].y = y; data->dir = Direction; }no16: if (data->temp[0] < data->iMinSAD[1]) { data->iMinSAD[1] = data->temp[0]; data->currentMV[1].x = x; data->currentMV[1].y = y; } if (data->temp[1] < data->iMinSAD[2]) { data->iMinSAD[2] = data->temp[1]; data->currentMV[2].x = x; data->currentMV[2].y = y; } if (data->temp[2] < data->iMinSAD[3]) { data->iMinSAD[3] = data->temp[2]; data->currentMV[3].x = x; data->currentMV[3].y = y; } if (data->temp[3] < data->iMinSAD[4]) { data->iMinSAD[4] = data->temp[3]; data->currentMV[4].x = x; data->currentMV[4].y = y; }}static voidCheckCandidate16_qpel(const int x, const int y, SearchData * const data, const unsigned int Direction){ const uint8_t *Reference; int32_t sad; uint32_t t; if ( (x > data->max_dx) || (x < data->min_dx) || (y > data->max_dy) || (y < data->min_dy) ) return; Reference = xvid_me_interpolate16x16qpel(x, y, 0, data); sad = sad16v(data->Cur, Reference, data->iEdgedWidth, data->temp); t = d_mv_bits(x, y, data->predMV, data->iFcode, 0, 0); sad += (data->lambda16 * t * sad)>>10; data->temp[0] += (data->lambda8 * t * (data->temp[0] + NEIGH_8X8_BIAS))>>10; if (data->chroma && (sad < data->iMinSAD[0] || sad < data->iMinSAD2) ) sad += xvid_me_ChromaSAD(((x/2) >> 1) + roundtab_79[(x/2) & 0x3], ((y/2) >> 1) + roundtab_79[(y/2) & 0x3], data); if (data->temp[0] < data->iMinSAD[1]) { data->iMinSAD[1] = data->temp[0]; data->currentQMV[1].x = x; data->currentQMV[1].y = y; } if (data->temp[1] < data->iMinSAD[2]) { data->iMinSAD[2] = data->temp[1]; data->currentQMV[2].x = x; data->currentQMV[2].y = y; } if (data->temp[2] < data->iMinSAD[3]) { data->iMinSAD[3] = data->temp[2]; data->currentQMV[3].x = x; data->currentQMV[3].y = y; } if (data->temp[3] < data->iMinSAD[4]) { data->iMinSAD[4] = data->temp[3]; data->currentQMV[4].x = x; data->currentQMV[4].y = y; } if (sad < data->iMinSAD[0]) { data->iMinSAD2 = *(data->iMinSAD); data->currentQMV2.x = data->currentQMV->x; data->currentQMV2.y = data->currentQMV->y; data->iMinSAD[0] = sad; data->currentQMV[0].x = x; data->currentQMV[0].y = y; } else if (sad < data->iMinSAD2) { data->iMinSAD2 = sad; data->currentQMV2.x = x; data->currentQMV2.y = y; }}static voidCheckCandidate8(const int x, const int y, SearchData * const data, const unsigned int Direction){ int32_t sad; uint32_t t; const uint8_t * Reference; VECTOR * current; if ( (x > data->max_dx) || (x < data->min_dx) || (y > data->max_dy) || (y < data->min_dy) ) return; if (!data->qpel_precision) { Reference = GetReference(x, y, data); current = data->currentMV; } else { /* x and y are in 1/4 precision */ Reference = xvid_me_interpolate8x8qpel(x, y, 0, 0, data); current = data->currentQMV; } sad = sad8(data->Cur, Reference, data->iEdgedWidth); t = d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0); sad += (data->lambda8 * t * (sad+NEIGH_8X8_BIAS))>>10; if (sad < *(data->iMinSAD)) { *(data->iMinSAD) = sad; current->x = x; current->y = y; data->dir = Direction; }}static voidCheckCandidate8_qpel(const int x, const int y, SearchData * const data, const unsigned int Direction){ int32_t sad; uint32_t t; const uint8_t * Reference; VECTOR * current; if ( (x > data->max_dx) || (x < data->min_dx) || (y > data->max_dy) || (y < data->min_dy) ) return; /* x and y are in 1/4 precision */ Reference = xvid_me_interpolate8x8qpel(x, y, 0, 0, data); current = data->currentQMV; sad = sad8(data->Cur, Reference, data->iEdgedWidth); t = d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0); sad += (data->lambda8 * t * (sad+NEIGH_8X8_BIAS))>>10; if (sad < *(data->iMinSAD)) { data->iMinSAD2 = *(data->iMinSAD); data->currentQMV2.x = data->currentQMV->x; data->currentQMV2.y = data->currentQMV->y; *(data->iMinSAD) = sad; data->currentQMV->x = x; data->currentQMV->y = y; data->dir = Direction; } else if (sad < data->iMinSAD2) { data->iMinSAD2 = sad; data->currentQMV2.x = x; data->currentQMV2.y = y; }}static voidCheckCandidate32(const int x, const int y, SearchData * const data, const unsigned int Direction){ uint32_t t; const uint8_t * Reference; int sad; if ( (!(x&1) && x !=0) || (!(y&1) && y !=0) || /* non-zero even value */ (x > data->max_dx) || (x < data->min_dx) || (y > data->max_dy) || (y < data->min_dy) ) return; Reference = GetReference(x, y, data); t = d_mv_bits(x, y, data->predMV, data->iFcode, 0, 1); sad = sad32v_c(data->Cur, Reference, data->iEdgedWidth, data->temp); sad += (data->lambda16 * t * sad) >> 10; data->temp[0] += (data->lambda8 * t * (data->temp[0] + NEIGH_8X8_BIAS))>>10; if (sad < data->iMinSAD[0]) { data->iMinSAD[0] = sad; data->currentMV[0].x = x; data->currentMV[0].y = y; data->dir = Direction; } if (data->temp[0] < data->iMinSAD[1]) { data->iMinSAD[1] = data->temp[0]; data->currentMV[1].x = x; data->currentMV[1].y = y; } if (data->temp[1] < data->iMinSAD[2]) { data->iMinSAD[2] = data->temp[1]; data->currentMV[2].x = x; data->currentMV[2].y = y; } if (data->temp[2] < data->iMinSAD[3]) { data->iMinSAD[3] = data->temp[2]; data->currentMV[3].x = x; data->currentMV[3].y = y; } if (data->temp[3] < data->iMinSAD[4]) { data->iMinSAD[4] = data->temp[3]; data->currentMV[4].x = x; data->currentMV[4].y = y; }}intxvid_me_SkipDecisionP(const IMAGE * current, const IMAGE * reference, const int x, const int y, const uint32_t stride, const uint32_t iQuant, int rrv){ int offset = (x + y*stride)*8; if(!rrv) { uint32_t sadC = sad8(current->u + offset, reference->u + offset, stride); if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP) return 0; sadC += sad8(current->v + offset, reference->v + offset, stride); if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP) return 0; return 1; } else { uint32_t sadC = sad16(current->u + 2*offset, reference->u + 2*offset, stride, 256*4096); if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP*4) return 0; sadC += sad16(current->v + 2*offset, reference->v + 2*offset, stride, 256*4096); if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP*4) return 0; return 1; }} /* * pmv are filled with: * [0]: Median (or whatever is correct in a special case) * [1]: left neighbour * [2]: top neighbour * [3]: topright neighbour * psad are filled with: * [0]: minimum of [1] to [3] * [1]: left neighbour's SAD (NB:[1] to [3] are actually not needed) * [2]: top neighbour's SAD * [3]: topright neighbour's SAD */static __inline voidget_pmvdata2(const MACROBLOCK * const mbs, const int mb_width, const int bound, const int x, const int y, VECTOR * const pmv, int32_t * const psad){ int lx, ly, lz; /* left */ int tx, ty, tz; /* top */ int rx, ry, rz; /* top-right */ int lpos, tpos, rpos; int num_cand = 0, last_cand = 1; lx = x - 1; ly = y; lz = 1; tx = x; ty = y - 1; tz = 2; rx = x + 1; ry = y - 1; rz = 2; lpos = lx + ly * mb_width; rpos = rx + ry * mb_width; tpos = tx + ty * mb_width; if (lpos >= bound && lx >= 0) { num_cand++; last_cand = 1; pmv[1] = mbs[lpos].mvs[lz]; psad[1] = mbs[lpos].sad8[lz]; } else { pmv[1] = zeroMV; psad[1] = MV_MAX_ERROR; } if (tpos >= bound) { num_cand++; last_cand = 2; pmv[2]= mbs[tpos].mvs[tz]; psad[2] = mbs[tpos].sad8[tz]; } else { pmv[2] = zeroMV; psad[2] = MV_MAX_ERROR; } if (rpos >= bound && rx < mb_width) { num_cand++; last_cand = 3; pmv[3] = mbs[rpos].mvs[rz]; psad[3] = mbs[rpos].sad8[rz]; } else { pmv[3] = zeroMV; psad[3] = MV_MAX_ERROR; } /* original pmvdata() compatibility hack */ if (x == 0 && y == 0) { pmv[0] = pmv[1] = pmv[2] = pmv[3] = zeroMV; psad[0] = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -