# Dirac - A Video Codec

Created by the British Broadcasting Corporation.

# motion.h

Go to the documentation of this file.

    00001 /* ***** BEGIN LICENSE BLOCK *****
    00002 *
    00003 * $Id: motion.h,v 1.21 2007/04/11 08:08:49 tjdwave Exp $ $Name: Dirac_0_7_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     enum MvElement { HORIZONTAL , VERTICAL };
    00055
    00057     template <class T>
    00058     class MotionVector
    00059     {
    00060     public:
    00061
    00063         MotionVector<T>(T a, T b) : x(a), y(b) {};
    00065         MotionVector<T>() : x(0), y(0) {};
    00067         MotionVector<T>(T a) : x(a), y(a) {};
    00068
    00070         inline MotionVector<T> operator+(const MotionVector<T>& argument) const;
    00071
    00073         inline MotionVector<T> operator-(const MotionVector<T>& argument) const;
    00074
    00076         inline MotionVector<T> operator*(const float argument) const;
    00077
    00079         inline MotionVector<T> operator*(const int argument) const;
    00080
    00082         inline MotionVector<T> operator<<(const int argument) const;
    00083
    00085         inline MotionVector<T> operator>>(const int argument) const;
    00086
    00088         T& operator[](const int pos){return ( ( pos==0) ? x : y );}
    00089
    00091         const T& operator[](const int pos) const {return ( ( pos==0) ? x : y );}
    00092
    00093
    00095         T x,y;
    00096
    00097     };
    00098
    00099
    00100     template <class T>
    00101     inline MotionVector<T> MotionVector<T>::operator+(const MotionVector<T>& argument) const
    00102     {
    00103         MotionVector<T> temp;
    00104         temp.x = x + argument.x;
    00105         temp.y = y + argument.y;
    00106
    00107         return temp;
    00108     }
    00109
    00110     template <class T>
    00111     inline MotionVector<T>  MotionVector<T>::operator-(const MotionVector<T>& argument) const
    00112     {
    00113         MotionVector<T> temp;
    00114         temp.x = x-argument.x;
    00115         temp.y = y-argument.y;
    00116
    00117         return temp;
    00118     }
    00119
    00120     template <class T>
    00121     inline MotionVector<T>  MotionVector<T>::operator*(const float argument) const
    00122     {
    00123         MotionVector<T> temp;
    00124         temp.x = x*argument;
    00125         temp.y = y*argument;
    00126
    00127         return temp;
    00128     }
    00129
    00130     template <class T>
    00131     inline MotionVector<T>  MotionVector<T>::operator*(const int argument) const
    00132     {
    00133         MotionVector<T> temp;
    00134         temp.x = x*argument;
    00135         temp.y = y*argument;
    00136
    00137         return temp;
    00138     }
    00139
    00140     template <class T>
    00141     inline MotionVector<T>  MotionVector<T>::operator<<(const int argument) const
    00142     {
    00143         MotionVector<T> temp;
    00144         temp.x = x<<argument;
    00145         temp.y = y<<argument;
    00146
    00147         return temp;
    00148     }
    00149
    00150     template <class T>
    00151     inline MotionVector<T>  MotionVector<T>::operator>>(const int argument) const
    00152     {
    00153         MotionVector<T> temp;
    00154         temp.x = x>>argument;
    00155         temp.y = y>>argument;
    00156
    00157         return temp;
    00158     }
    00159
    00161     template <class T>
    00162     std::ostream & operator<< (std::ostream & stream, MotionVector<T> & mv)
    00163     {
    00164         stream << mv.x << " " << mv.y;
    00165
    00166         return stream;
    00167     }
    00168
    00170     template <class T>
    00171     std::istream & operator>> (std::istream & stream, MotionVector<T> & mv)
    00172     {
    00173         stream >> mv.x;
    00174         stream >> mv.y;
    00175
    00176         return stream;
    00177     }
    00178
    00180     typedef MotionVector<int> MVector;
    00181
    00183     typedef MotionVector<int> ImageCoords;
    00184
    00186     typedef TwoDArray<MVector> MvArray;
    00187
    00189     typedef TwoDArray< MotionVector<float> > MvFloatArray;
    00190
    00192     class MvCostData
    00193     {
    00194     public:
    00196         MvCostData():
    00197         SAD(0.0),
    00198         mvcost(0.0),
    00199         total(0.0){}
    00200
    00201         void SetTotal( const float lambda ){total = SAD + lambda*mvcost;}
    00202
    00204         float SAD;
    00205
    00207         float mvcost;
    00208
    00210         float total;
    00211     };
    00212
    00213
    00215
    00219     class MvData
    00220     {
    00221     public:
    00223
    00231         MvData( const int xnumMB, const int ynumMB ,
    00232                 const int xnumblocks, const int ynumblocks ,  const int num_refs);
    00233
    00235
    00241         MvData( const int xnumMB, const int ynumMB ,  const int num_refs);
    00242
    00244         ~MvData();
    00245
    00247         MvArray& Vectors(const int ref_id){return *( m_vectors[ref_id] );}
    00248
    00250         const MvArray& Vectors(const int ref_id) const {return *( m_vectors[ref_id] );}
    00251
    00253         MvArray& GlobalMotionVectors(const int ref_id){return *( m_gm_vectors[ref_id] );}
    00254
    00256         const MvArray& GlobalMotionVectors(const int ref_id) const {return *( m_gm_vectors[ref_id] );}
    00257
    00259         TwoDArray<ValueType>& DC(CompSort cs){return *( m_dc[cs] );}
    00260
    00262         const TwoDArray<ValueType>& DC(CompSort cs) const {return *( m_dc[cs] );}
    00263
    00265         const OneDArray< TwoDArray<ValueType>* >& DC() const {return m_dc;}
    00266
    00268         TwoDArray<PredMode>& Mode(){return m_modes;}
    00269
    00271         const TwoDArray<PredMode>& Mode() const {return m_modes;}
    00272
    00274         TwoDArray<int>& MBSplit(){return m_mb_split;}
    00275
    00277         const TwoDArray<int>& MBSplit() const{return m_mb_split;}
    00278
    00280         OneDArray<float>& GlobalMotionParameters(const int ref_id) { return *( m_gm_params[ref_id] ); }
    00281
    00283         const OneDArray<float>& GlobalMotionParameters(const int ref_id) const { return *( m_gm_params[ref_id] ); }
    00284
    00286         const unsigned int NumRefs()const {return m_num_refs;}
    00287
    00288     private:
    00289         // Initialises the arrays of data
    00290         void InitMvData();
    00291
    00292         // The motion vectors
    00293         OneDArray<MvArray*> m_vectors;
    00294
    00295         // The global motion vectors
    00296         OneDArray<MvArray*> m_gm_vectors;
    00297
    00298         // The block modes
    00299         TwoDArray<PredMode> m_modes;
    00300
    00301         // The DC values
    00302         OneDArray< TwoDArray<ValueType>* > m_dc;
    00303
    00304         // The MB split levels
    00305         TwoDArray<int> m_mb_split;
    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     int Median( const int val1, const int val2, const int val3);
    00424
    00426     MVector MvMedian(const MVector& mv1,const MVector& mv2,const MVector& mv3);
    00427
    00428
    00430     int Median(const std::vector<int>& val_list);
    00431
    00433     MVector MvMedian(const std::vector<MVector>& vect_list);
    00434
    00436     MVector MvMean(const MVector& mv1, const MVector& mv2);
    00437
    00439     inline int Norm2(const MVector& mv){//L^2 norm of a motion vector
    00440         return mv.x*mv.x+mv.y*mv.y;
    00441     }
    00442
    00444     inline int Norm1(const MVector& mv){//L^1 norm of a motion vector
    00445         return abs(mv.x)+abs(mv.y);
    00446     }
    00447
    00449     unsigned int GetUMean(std::vector<unsigned int>& values);
    00450
    00452     int GetSMean(std::vector<int>& values);
    00453
    00454 } // namespace dirac
    00455
    00456 #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.