【VC开源代码栏目提醒】:网学会员为广大网友收集整理了,mot_est.cpp,希望对大家有所帮助!
////////////////////////////////////////////////////////////////////////////
//
//
// Project : VideoNet version 1.1.
// Description : Peer to Peer Video Conferencing over the LAN.
// Author : Nagareshwar Y Talekar ( nsry2002@yahoo.co.in)
// Date : 15-6-2004.
//
// I have converted origional fast h.263 encoder library from C to C++
// so that it can be integrated into any windows application easily.
// I have removed some of unnecessary codes/files from the
// fast h263 library.Also moved definitions and declarations
// in their proper .h and .cpp files.
//
// File description :
// Name : mot_est.cpp
//
//
/////////////////////////////////////////////////////////////////////////////
/*************************************************
* libr263: fast H.263 encoder library
*
* Copyright (C) 1996, Roalt Aalmoes, Twente University
* SPA multimedia group
*
* Based on Telenor TMN 1.6 encoder (Copyright (C) 1995, Telenor R&D)
* created by Karl Lillevold
*
* Author encoder: Roalt Aalmoes, <aalmoes@huygens.nl>
*
* Date: 31-07-96
**************************************************/
#include "Global.h"
/* Efficient macroblock comparison op. */
/* The comparison is between a non-interpolated lowres block of pixel
and a interpolated hires block of pixel. This might be confusing:
Example: compare 3x3 block with 3x3 block, then the 1s of block 1 are
compared with the 2s of block 2.
Block 1: Block 2:
1.1.1. 222
...... 222
1.1.1. 222
......
1.1.1.
You see, for block one (ii), you have 4 times more data!
*/
//inline int SAD_HalfPixelMacroblock(unsigned int *ii,
// unsigned int *curr,
// int pixels_on_line, int Min_SAD)
int SAD_HalfPixelMacroblock(unsigned int *ii,
unsigned int *curr,
int pixels_on_line, int Min_SAD)
{
int j = 16;
int sad = 0;
while(j--) {
sad += abs(*ii-(*curr++));
ii += 2;
sad += abs(*ii-(*curr++));
ii += 2;
sad += abs(*ii-(*curr++));
ii += 2;
sad += abs(*ii- (*curr++));
ii += 2;
sad += abs(*ii-(*curr++));
ii += 2;
sad += abs(*ii-(*curr++));
ii += 2;
sad += abs(*ii-(*curr++));
ii += 2;
sad += abs(*ii-(*curr++));
ii += 2;
sad += abs(*ii-(*curr++));
ii += 2;
sad += abs(*ii-(*curr++));
ii += 2;
sad += abs(*ii-(*curr++));
ii += 2;
sad += abs(*ii-(*curr++));
ii += 2;
sad += abs(*ii-(*curr++));
ii += 2;
sad += abs(*ii-(*curr++));
ii += 2;
sad += abs(*ii-(*curr++));
ii += 2;
sad += abs(*ii-*curr);
if(sad > Min_SAD)
return INT_MAX;
ii += pixels_on_line*4 - 30;
/* Interpolated image, that means two times */
/* We also take only one of the two pixels */
/* This means another two times ... */
curr += pixels_on_line - 15;
} /* end while */
return sad;
}
/* This implementation contains two mayor algorithms: exhaustive and
logarithmic. The exhaustive algorithm was adapted to be applied on
half-pixel luminance, and the logarithmic algorithm was implemented
by Roalt Aalmoes and is functional equivalent to the one in berkeley's
MPEG encoder. */
void FullMotionEstimation(unsigned int *curr, unsigned int *prev_ipol,
int seek_dist, MotionVector *current_MV, int x_curr,
int y_curr)
/* x_curr and y_curr are whole-pixel
values to indicate upperleft (0,0)
pixel of this MB in whole frame curr */
{
int Min_FRAME;
MotionVector MV_FRAME;
unsigned int *curr_image_MB;
int sxy,xblock,k,yblock,l;
int RightXBorder,LeftXBorder,RightYBorder,LeftYBorder;
int xmax,ymax,sad;
int CenterX, CenterY, origCenterX, origCenterY, newCenterX, newCenterY;
int StepSizeX, StepSizeY;
curr_image_MB = curr + Global::pels*y_curr + x_curr;
/* from now on, all pixel related operations are based on prev_ipol */
/* way: thus resolution is enhanced by 2 for halfpixel search */
CenterX = 2*x_curr;
CenterY = 2*y_curr;
xmax = 2*Global::pels;
ymax = 2*Global::lines;
sxy = mmin(31, seek_dist);
LeftXBorder = CenterX - sxy;
RightXBorder = CenterX + sxy;
LeftYBorder = CenterY - sxy;
RightYBorder = CenterY + sxy;
if (LeftXBorder<0) LeftXBorder = 0;
if (RightXBorder>xmax-32) RightXBorder = xmax-32;
/* Comparison is still on 16x16 blocks */
if (LeftYBorder<0) LeftYBorder = 0;
/* but this is interleaved by 1 pixel! */
if (RightYBorder>ymax-32) RightYBorder = ymax-32;
/* e.g. comp. pixno.1 3 5 7 or 2 4 6 8 */
/*but never 1 2 3 4 as this is halfres.*/
Min_FRAME = INT_MAX;
MV_FRAME.x = 0;
MV_FRAME.y = 0;
MV_FRAME.x_half = 0;
MV_FRAME.y_half = 0;
if(Global::search_p_frames == CPARAM_EXHAUSTIVE) {
/* This is the spriral search variant of exhaustive seach */
/* First determine zero MV value SAD */
xblock = CenterX; yblock = Cen