Created by the British Broadcasting Corporation.
00001 /* ***** BEGIN LICENSE BLOCK ***** 00002 * 00003 * $Id: motion.h,v 1.18 2006/04/20 10:41:57 asuraparaju Exp $ $Name: Dirac_0_6_0 $ 00004 * 00005 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 00006 * 00007 * The contents of this file are subject to the Mozilla Public License 00008 * Version 1.1 (the "License"); you may not use this file except in compliance 00009 * with the License. You may obtain a copy of the License at 00010 * http://www.mozilla.org/MPL/ 00011 * 00012 * Software distributed under the License is distributed on an "AS IS" basis, 00013 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for 00014 * the specific language governing rights and limitations under the License. 00015 * 00016 * The Original Code is BBC Research and Development code. 00017 * 00018 * The Initial Developer of the Original Code is the British Broadcasting 00019 * Corporation. 00020 * Portions created by the Initial Developer are Copyright (C) 2004. 00021 * All Rights Reserved. 00022 * 00023 * Contributor(s): Thomas Davies (Original Author), 00024 * Chris Bowley, 00025 * Tim Borer 00026 * 00027 * Alternatively, the contents of this file may be used under the terms of 00028 * the GNU General Public License Version 2 (the "GPL"), or the GNU Lesser 00029 * Public License Version 2.1 (the "LGPL"), in which case the provisions of 00030 * the GPL or the LGPL are applicable instead of those above. If you wish to 00031 * allow use of your version of this file only under the terms of the either 00032 * the GPL or LGPL and not to allow others to use your version of this file 00033 * under the MPL, indicate your decision by deleting the provisions above 00034 * and replace them with the notice and other provisions required by the GPL 00035 * or LGPL. If you do not delete the provisions above, a recipient may use 00036 * your version of this file under the terms of any one of the MPL, the GPL 00037 * or the LGPL. 00038 * ***** END LICENSE BLOCK ***** */ 00039 00040 #include <libdirac_common/common.h> 00041 #include <algorithm> 00042 #ifndef _MOTION_H 00043 #define _MOTION_H 00044 00045 namespace dirac 00046 { 00048 //classes and functions for motion estimation and compensation// 00050 00051 //classes 00052 00054 template <class T> 00055 class MotionVector 00056 { 00057 public: 00058 00060 MotionVector<T>(T a, T b) : x(a), y(b) {}; 00062 MotionVector<T>() : x(0), y(0) {}; 00064 MotionVector<T>(T a) : x(a), y(a) {}; 00065 00067 inline MotionVector<T> operator+(const MotionVector<T>& argument) const; 00068 00070 inline MotionVector<T> operator-(const MotionVector<T>& argument) const; 00071 00073 inline MotionVector<T> operator*(const float argument) const; 00074 00076 inline MotionVector<T> operator*(const int argument) const; 00077 00079 inline MotionVector<T> operator<<(const int argument) const; 00080 00082 inline MotionVector<T> operator>>(const int argument) const; 00083 00084 00086 T x,y; 00087 00088 }; 00089 00090 00091 template <class T> 00092 inline MotionVector<T> MotionVector<T>::operator+(const MotionVector<T>& argument) const 00093 { 00094 MotionVector<T> temp; 00095 temp.x = x + argument.x; 00096 temp.y = y + argument.y; 00097 00098 return temp; 00099 } 00100 00101 template <class T> 00102 inline MotionVector<T> MotionVector<T>::operator-(const MotionVector<T>& argument) const 00103 { 00104 MotionVector<T> temp; 00105 temp.x = x-argument.x; 00106 temp.y = y-argument.y; 00107 00108 return temp; 00109 } 00110 00111 template <class T> 00112 inline MotionVector<T> MotionVector<T>::operator*(const float argument) const 00113 { 00114 MotionVector<T> temp; 00115 temp.x = x*argument; 00116 temp.y = y*argument; 00117 00118 return temp; 00119 } 00120 00121 template <class T> 00122 inline MotionVector<T> MotionVector<T>::operator*(const int argument) const 00123 { 00124 MotionVector<T> temp; 00125 temp.x = x*argument; 00126 temp.y = y*argument; 00127 00128 return temp; 00129 } 00130 00131 template <class T> 00132 inline MotionVector<T> MotionVector<T>::operator<<(const int argument) const 00133 { 00134 MotionVector<T> temp; 00135 temp.x = x<<argument; 00136 temp.y = y<<argument; 00137 00138 return temp; 00139 } 00140 00141 template <class T> 00142 inline MotionVector<T> MotionVector<T>::operator>>(const int argument) const 00143 { 00144 MotionVector<T> temp; 00145 temp.x = x>>argument; 00146 temp.y = y>>argument; 00147 00148 return temp; 00149 } 00150 00152 template <class T> 00153 std::ostream & operator<< (std::ostream & stream, MotionVector<T> & mv) 00154 { 00155 stream << mv.x << " " << mv.y; 00156 00157 return stream; 00158 } 00159 00161 template <class T> 00162 std::istream & operator>> (std::istream & stream, MotionVector<T> & mv) 00163 { 00164 stream >> mv.x; 00165 stream >> mv.y; 00166 00167 return stream; 00168 } 00169 00171 typedef MotionVector<int> MVector; 00172 00174 typedef MotionVector<int> ImageCoords; 00175 00177 typedef TwoDArray<MVector> MvArray; 00178 00180 typedef TwoDArray< MotionVector<float> > MvFloatArray; 00181 00183 class MvCostData 00184 { 00185 public: 00187 MvCostData(): 00188 SAD(0.0), 00189 mvcost(0.0), 00190 total(0.0){} 00191 00192 void SetTotal( const float lambda ){total = SAD + lambda*mvcost;} 00193 00195 float SAD; 00196 00198 float mvcost; 00199 00201 float total; 00202 }; 00203 00204 00206 00210 class MvData 00211 { 00212 public: 00214 00222 MvData( const int xnumMB, const int ynumMB , 00223 const int xnumblocks, const int ynumblocks , const int num_refs); 00224 00226 00232 MvData( const int xnumMB, const int ynumMB , const int num_refs); 00233 00235 ~MvData(); 00236 00238 MvArray& Vectors(const int ref_id){return *( m_vectors[ref_id] );} 00239 00241 const MvArray& Vectors(const int ref_id) const {return *( m_vectors[ref_id] );} 00242 00244 MvArray& GlobalMotionVectors(const int ref_id){return *( m_gm_vectors[ref_id] );} 00245 00247 const MvArray& GlobalMotionVectors(const int ref_id) const {return *( m_gm_vectors[ref_id] );} 00248 00250 TwoDArray<ValueType>& DC(CompSort cs){return *( m_dc[cs] );} 00251 00253 const TwoDArray<ValueType>& DC(CompSort cs) const {return *( m_dc[cs] );} 00254 00256 const OneDArray< TwoDArray<ValueType>* >& DC() const {return m_dc;} 00257 00259 TwoDArray<PredMode>& Mode(){return m_modes;} 00260 00262 const TwoDArray<PredMode>& Mode() const {return m_modes;} 00263 00265 TwoDArray<int>& MBSplit(){return m_mb_split;} 00266 00268 const TwoDArray<int>& MBSplit() const{return m_mb_split;} 00269 00271 TwoDArray<bool>& MBCommonMode(){return m_mb_common;} 00272 00274 const TwoDArray<bool>& MBCommonMode() const{return m_mb_common;} 00275 00277 OneDArray<float>& GlobalMotionParameters(const int ref_id) { return *( m_gm_params[ref_id] ); } 00278 00280 const OneDArray<float>& GlobalMotionParameters(const int ref_id) const { return *( m_gm_params[ref_id] ); } 00281 00283 const unsigned int NumRefs()const {return m_num_refs;} 00284 00285 private: 00286 // Initialises the arrays of data 00287 void InitMvData(); 00288 00289 // The motion vectors 00290 OneDArray<MvArray*> m_vectors; 00291 00292 // The global motion vectors 00293 OneDArray<MvArray*> m_gm_vectors; 00294 00295 // The block modes 00296 TwoDArray<PredMode> m_modes; 00297 00298 // The DC values 00299 OneDArray< TwoDArray<ValueType>* > m_dc; 00300 00301 // The MB split levels 00302 TwoDArray<int> m_mb_split; 00303 00304 // The MB common mode indicators 00305 TwoDArray<bool> m_mb_common; 00306 00307 // Global motion model parameters 00308 OneDArray< OneDArray<float>* > m_gm_params; 00309 00310 // Number of reference frames 00311 const unsigned int m_num_refs; 00312 }; 00313 00315 00320 class MEData: public MvData 00321 { 00322 public: 00323 00325 00333 MEData( const int xnumMB, const int ynumMB , 00334 const int xnumblocks, const int ynumblocks , const int num_refs = 2); 00335 00337 00343 MEData( const int xnumMB, const int ynumMB , const int num_refs = 2); 00344 00346 ~MEData(); 00347 00349 TwoDArray<MvCostData>& PredCosts(const int ref_id){ return *( m_pred_costs[ref_id] ); } 00350 00352 const TwoDArray<MvCostData>& PredCosts(const int ref_id) const { return *( m_pred_costs[ref_id] ); } 00353 00355 TwoDArray<float>& IntraCosts(){ return m_intra_costs; } 00356 00358 const TwoDArray<float>& IntraCosts() const { return m_intra_costs; } 00359 00361 TwoDArray<MvCostData>& BiPredCosts(){ return m_bipred_costs; } 00362 00364 const TwoDArray<MvCostData>& BiPredCosts() const { return m_bipred_costs; } 00365 00367 TwoDArray<float>& MBCosts(){ return m_MB_costs; } 00368 00370 const TwoDArray<float>& MBCosts() const { return m_MB_costs; } 00371 00373 void SetLambdaMap( const int num_refs , const float lambda ); 00374 00376 void SetLambdaMap( const int level , const TwoDArray<float>& l_map , const float wt ); 00377 00379 const TwoDArray<float>& LambdaMap() const { return m_lambda_map; } 00380 00382 TwoDArray<int>& GlobalMotionInliers(const int ref_id){ return *( m_inliers[ref_id] ); } 00383 00385 const TwoDArray<int>& GlobalMotionInliers(const int ref_id) const { return *( m_inliers[ref_id] ); } 00386 00388 friend std::ostream &operator<< (std::ostream & stream, MEData & me_data); 00389 00391 friend std::istream &operator>> (std::istream & stream, MEData & me_data); 00392 00393 private: 00394 // Initialises the arrays of data 00395 void InitMEData(); 00396 00397 // Finds transitions in the motion vectors 00398 void FindTransitions( TwoDArray<bool>& trans_map , const int ref_num ); 00399 00400 // The costs of predicting each block, for each reference 00401 OneDArray< TwoDArray<MvCostData>* > m_pred_costs; 00402 00403 // The costs of predicting each block by DC 00404 TwoDArray<float> m_intra_costs; 00405 00406 // The costs of predicting each block bidirectionally 00407 TwoDArray<MvCostData> m_bipred_costs; 00408 00409 // The costs for each macroblock as a whole 00410 TwoDArray<float> m_MB_costs; 00411 00412 // A map of the lambda values to use 00413 TwoDArray<float> m_lambda_map; 00414 00415 // Global motion inliers 00416 OneDArray< TwoDArray<int>* > m_inliers; 00417 00418 }; 00419 00420 //motion estimation and coding stuff 00421 00423 MVector MvMedian(const MVector& mv1,const MVector& mv2,const MVector& mv3); 00424 00426 MVector MvMedian(const std::vector<MVector>& vect_list); 00427 00429 MVector MvMean(MVector& mv1,MVector& mv2); 00430 00432 inline int Norm2(const MVector& mv){//L^2 norm of a motion vector 00433 return mv.x*mv.x+mv.y*mv.y; 00434 } 00435 00437 inline int Norm1(const MVector& mv){//L^1 norm of a motion vector 00438 return abs(mv.x)+abs(mv.y); 00439 } 00440 00442 inline unsigned int GetMean(std::vector<unsigned int>& values){ 00443 int sum=0; 00444 for (unsigned int I=0;I<values.size();++I) 00445 sum+=values[I]; 00446 sum+=(values.size()>>1); 00447 sum/=values.size(); 00448 return sum; 00449 } 00450 00451 } // namespace dirac 00452 00453 #endif
© 2004 British Broadcasting Corporation.
Dirac code licensed under the Mozilla Public License (MPL) Version 1.1.
HTML documentation generated by Dimitri van Heesch's
excellent Doxygen tool.