# Dirac - A Video Codec

Created by the British Broadcasting Corporation.

motion.h
Go to the documentation of this file.
1 /* ***** BEGIN LICENSE BLOCK *****
2 *
3 * $Id: motion.h,v 1.30 2008/10/01 01:26:47 asuraparaju Exp $ $Name: Dirac_1_0_2 $
4 *
5 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 *
7 * The contents of this file are subject to the Mozilla Public License
8 * Version 1.1 (the "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 * http://www.mozilla.org/MPL/
11 *
12 * Software distributed under the License is distributed on an "AS IS" basis,
13 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
14 * the specific language governing rights and limitations under the License.
15 *
16 * The Original Code is BBC Research and Development code.
17 *
18 * The Initial Developer of the Original Code is the British Broadcasting
19 * Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 2004.
21 * All Rights Reserved.
22 *
23 * Contributor(s): Thomas Davies (Original Author),
24 * Chris Bowley,
25 * Tim Borer
26 *
27 * Alternatively, the contents of this file may be used under the terms of
28 * the GNU General Public License Version 2 (the "GPL"), or the GNU Lesser
29 * Public License Version 2.1 (the "LGPL"), in which case the provisions of
30 * the GPL or the LGPL are applicable instead of those above. If you wish to
31 * allow use of your version of this file only under the terms of the either
32 * the GPL or LGPL and not to allow others to use your version of this file
33 * under the MPL, indicate your decision by deleting the provisions above
34 * and replace them with the notice and other provisions required by the GPL
35 * or LGPL. If you do not delete the provisions above, a recipient may use
36 * your version of this file under the terms of any one of the MPL, the GPL
37 * or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
39
40 #include <libdirac_common/common.h>
41 #include <algorithm>
42 #ifndef _MOTION_H
43 #define _MOTION_H
44
45 namespace dirac
46 {
48 //classes and functions for motion estimation and compensation//
50
51 //classes
52
54 enum MvElement { HORIZONTAL , VERTICAL };
55
57 template <class T>
58 class MotionVector
59 {
60 public:
61
63 MotionVector<T>(T a, T b) : x(a), y(b) {};
65 MotionVector<T>() : x(0), y(0) {};
67 MotionVector<T>(T a) : x(a), y(a) {};
68
70 inline MotionVector<T> operator+(const MotionVector<T>& argument) const;
71
73 inline MotionVector<T> operator-(const MotionVector<T>& argument) const;
74
76 inline MotionVector<T> operator*(const float argument) const;
77
79 inline MotionVector<T> operator*(const int argument) const;
80
82 inline MotionVector<T> operator<<(const int argument) const;
83
85 inline MotionVector<T> operator>>(const int argument) const;
86
88 T& operator[](const int pos){return ( ( pos==0) ? x : y );}
89
91 const T& operator[](const int pos) const {return ( ( pos==0) ? x : y );}
92
93
95 T x,y;
96
97 };
98
99
100 template <class T>
101 inline MotionVector<T> MotionVector<T>::operator+(const MotionVector<T>& argument) const
102 {
103 MotionVector<T> temp;
104 temp.x = x + argument.x;
105 temp.y = y + argument.y;
106
107 return temp;
108 }
109
110 template <class T>
111 inline MotionVector<T> MotionVector<T>::operator-(const MotionVector<T>& argument) const
112 {
113 MotionVector<T> temp;
114 temp.x = x-argument.x;
115 temp.y = y-argument.y;
116
117 return temp;
118 }
119
120 template <class T>
121 inline MotionVector<T> MotionVector<T>::operator*(const float argument) const
122 {
123 MotionVector<T> temp;
124 temp.x = x*argument;
125 temp.y = y*argument;
126
127 return temp;
128 }
129
130 template <class T>
131 inline MotionVector<T> MotionVector<T>::operator*(const int argument) const
132 {
133 MotionVector<T> temp;
134 temp.x = x*argument;
135 temp.y = y*argument;
136
137 return temp;
138 }
139
140 template <class T>
141 inline MotionVector<T> MotionVector<T>::operator<<(const int argument) const
142 {
143 MotionVector<T> temp;
144 temp.x = x<<argument;
145 temp.y = y<<argument;
146
147 return temp;
148 }
149
150 template <class T>
151 inline MotionVector<T> MotionVector<T>::operator>>(const int argument) const
152 {
153 MotionVector<T> temp;
154 temp.x = x>>argument;
155 temp.y = y>>argument;
156
157 return temp;
158 }
159
161 template <class T>
162 std::ostream & operator<< (std::ostream & stream, MotionVector<T> & mv)
163 {
164 stream << mv.x << " " << mv.y;
165
166 return stream;
167 }
168
170 template <class T>
171 std::istream & operator>> (std::istream & stream, MotionVector<T> & mv)
172 {
173 stream >> mv.x;
174 stream >> mv.y;
175
176 return stream;
177 }
178
180 typedef MotionVector<int> MVector;
181
183 typedef MotionVector<int> ImageCoords;
184
186 typedef TwoDArray<MVector> MvArray;
187
189 typedef TwoDArray< MotionVector<float> > MvFloatArray;
190
192 class MvCostData
193 {
194 public:
196 MvCostData():
197 SAD(0.0),
198 mvcost(0.0),
199 total(0.0){}
200
201 void SetTotal( const float lambda ){total = SAD + lambda*mvcost;}
202
204 float SAD;
205
207 float mvcost;
208
210 float total;
211 };
212
213
215
219 class MvData
220 {
221 public:
223
228 MvData( const PicturePredParams& predparams , const int num_refs);
229
231 ~MvData();
232
234 PicturePredParams& GetPicPredParams(){return m_predparams;}
235
237 const PicturePredParams& GetPicPredParams() const{return m_predparams;}
238
240 MvArray& Vectors(const int ref_id){return *( m_vectors[ref_id] );}
241
243 const MvArray& Vectors(const int ref_id) const {return *( m_vectors[ref_id] );}
244
246 MvArray& GlobalMotionVectors(const int ref_id){return *( m_gm_vectors[ref_id] );}
247
249 const MvArray& GlobalMotionVectors(const int ref_id) const {return *( m_gm_vectors[ref_id] );}
250
252 TwoDArray<ValueType>& DC(CompSort cs){return *( m_dc[cs] );}
253
255 const TwoDArray<ValueType>& DC(CompSort cs) const {return *( m_dc[cs] );}
256
258 const OneDArray< TwoDArray<ValueType>* >& DC() const {return m_dc;}
259
261 TwoDArray<PredMode>& Mode(){return m_modes;}
262
264 const TwoDArray<PredMode>& Mode() const {return m_modes;}
265
267 TwoDArray<int>& SBSplit(){return m_sb_split;}
268
270 const TwoDArray<int>& SBSplit() const{return m_sb_split;}
271
273 OneDArray<float>& GlobalMotionParameters(const int ref_id) { return *( m_gm_params[ref_id] ); }
274
276 const OneDArray<float>& GlobalMotionParameters(const int ref_id) const { return *( m_gm_params[ref_id] ); }
277
278 protected:
279 // A local copy of the picture prediction parameters
280 PicturePredParams m_predparams;
281
282 // Initialises the arrays of data
283 void InitMvData();
284
285 // The motion vectors
286 OneDArray<MvArray*> m_vectors;
287
288 // The global motion vectors
289 OneDArray<MvArray*> m_gm_vectors;
290
291 // The block modes
292 TwoDArray<PredMode> m_modes;
293
294 // The DC values
295 OneDArray< TwoDArray<ValueType>* > m_dc;
296
297 // The SB split levels
298 TwoDArray<int> m_sb_split;
299
300 // Global motion model parameters
301 OneDArray< OneDArray<float>* > m_gm_params;
302
303 // // Number of reference frames
304 // unsigned int m_num_refs;
305 };
306
308
313 class MEData: public MvData
314 {
315 public:
316
318
323 MEData( const PicturePredParams& predparams , const int num_refs = 2);
324
326 ~MEData();
327
329 void DropRef( int ref_index );
330
332 TwoDArray<MvCostData>& PredCosts(const int ref_id){ return *( m_pred_costs[ref_id] ); }
333
335 const TwoDArray<MvCostData>& PredCosts(const int ref_id) const { return *( m_pred_costs[ref_id] ); }
336
338 TwoDArray<float>& IntraCosts(){ return m_intra_costs; }
339
341 const TwoDArray<float>& IntraCosts() const { return m_intra_costs; }
342
344 TwoDArray<MvCostData>& BiPredCosts(){ return m_bipred_costs; }
345
347 const TwoDArray<MvCostData>& BiPredCosts() const { return m_bipred_costs; }
348
350 TwoDArray<float>& SBCosts(){ return m_SB_costs; }
351
353 const TwoDArray<float>& SBCosts() const { return m_SB_costs; }
354
356 float IntraBlockRatio() const {return m_intra_block_ratio; }
357
359 void SetIntraBlockRatio(const float r){ m_intra_block_ratio = r; }
360
362 void SetLambdaMap( const int num_refs , const float lambda );
363
365 void SetLambdaMap( const int level , const TwoDArray<float>& l_map , const float wt );
366
368 const TwoDArray<float>& LambdaMap() const { return m_lambda_map; }
369
371 TwoDArray<int>& GlobalMotionInliers(const int ref_id){ return *( m_inliers[ref_id] ); }
372
374 const TwoDArray<int>& GlobalMotionInliers(const int ref_id) const { return *( m_inliers[ref_id] ); }
375
377 friend std::ostream &operator<< (std::ostream & stream, MEData & me_data);
378
380 friend std::istream &operator>> (std::istream & stream, MEData & me_data);
381
382 private:
383 // Initialises the arrays of data
384 void InitMEData();
385
386 // Finds transitions in the motion vectors
387 void FindTransitions( TwoDArray<bool>& trans_map , const int ref_num );
388
389 // The costs of predicting each block, for each reference
390 OneDArray< TwoDArray<MvCostData>* > m_pred_costs;
391
392 // The costs of predicting each block by DC
393 TwoDArray<float> m_intra_costs;
394
395 // The costs of predicting each block bidirectionally
396 TwoDArray<MvCostData> m_bipred_costs;
397
398 // The costs for each macroblock as a whole
399 TwoDArray<float> m_SB_costs;
400
401 // A map of the lambda values to use
402 TwoDArray<float> m_lambda_map;
403
404 // Global motion inliers
405 OneDArray< TwoDArray<int>* > m_inliers;
406
407 // Intra block ratio
408 float m_intra_block_ratio;
409
410 };
411
412 //motion estimation and coding stuff
413
415 int Median( const int val1, const int val2, const int val3);
416
418 MVector MvMedian(const MVector& mv1,const MVector& mv2,const MVector& mv3);
419
420
422 int Median(const std::vector<int>& val_list);
423
425 MVector MvMedian(const std::vector<MVector>& vect_list);
426
428 MVector MvMean(const MVector& mv1, const MVector& mv2);
429
431 inline int Norm2(const MVector& mv){//L^2 norm of a motion vector
432 return mv.x*mv.x+mv.y*mv.y;
433 }
434
436 inline int Norm1(const MVector& mv){//L^1 norm of a motion vector
437 return abs(mv.x)+abs(mv.y);
438 }
439
441 unsigned int GetUMean(std::vector<unsigned int>& values);
442
444 int GetSMean(std::vector<int>& values);
445
446 } // namespace dirac
447
448 #endif
dirac::MotionVector::operator*
MotionVector< T > operator*(const float argument) const
Scalar multiplication.
Definition: motion.h:121
dirac::MvData::m_predparams
PicturePredParams m_predparams
Definition: motion.h:280
dirac::MvMedian
MVector MvMedian(const MVector &mv1, const MVector &mv2, const MVector &mv3)
Return the median of three motion vectors.
dirac::MEData::m_intra_costs
TwoDArray< float > m_intra_costs
Definition: motion.h:393
dirac::MotionVector::operator<<
MotionVector< T > operator<<(const int argument) const
Bitshift of each component.
Definition: motion.h:141
dirac::MEData::operator<<
friend std::ostream & operator<<(std::ostream &stream, MEData &me_data)
Overloaded operator<< for outputing to (file) stream.
dirac::MEData::PredCosts
const TwoDArray< MvCostData > & PredCosts(const int ref_id) const
Get the block cost structures for each reference.
Definition: motion.h:335
dirac::MvData::~MvData
~MvData()
Destructor.
dirac::MvData::GlobalMotionVectors
MvArray & GlobalMotionVectors(const int ref_id)
Get the global MVs for a reference.
Definition: motion.h:246
dirac::MvCostData
Class for recording costs derived in motion estimation.
Definition: motion.h:192
dirac::MvElement
MvElement
Horizontal or vertical.
Definition: motion.h:54
dirac::HORIZONTAL
Definition: motion.h:54
dirac::MotionVector::x
T x
x and y components
Definition: motion.h:95
dirac::Norm2
int Norm2(const MVector &mv)
Return the squared length of a motion vector.
Definition: motion.h:431
dirac::TwoDArray
A template class for two-dimensional arrays.
Definition: arrays.h:284
dirac::MEData::GlobalMotionInliers
TwoDArray< int > & GlobalMotionInliers(const int ref_id)
Get the inliers for each reference.
Definition: motion.h:371
dirac::MvCostData::SAD
float SAD
The Sum of Absolute Differences - easier to compute than Sum-Squared Differences. ...
Definition: motion.h:204
dirac::MvData::GlobalMotionParameters
OneDArray< float > & GlobalMotionParameters(const int ref_id)
Get the global motion model parameters.
Definition: motion.h:273
dirac::ImageCoords
MotionVector< int > ImageCoords
ImageCoords class is a vector of ints.
Definition: motion.h:183
dirac::MvData::m_dc
OneDArray< TwoDArray< ValueType > * > m_dc
Definition: motion.h:295
dirac::MEData::SetIntraBlockRatio
void SetIntraBlockRatio(const float r)
Set the intra block ratio.
Definition: motion.h:359
dirac::Median
int Median(const int val1, const int val2, const int val3)
Return the median of 3 integers.
dirac::MvArray
TwoDArray< MVector > MvArray
MvArray is a two-D array of MVectors.
Definition: motion.h:186
dirac::VERTICAL
Definition: motion.h:54
dirac::MEData::m_inliers
OneDArray< TwoDArray< int > * > m_inliers
Definition: motion.h:405
dirac::OneDArray
A template class for one-dimensional arrays.
Definition: arrays.h:89
dirac::MEData::m_bipred_costs
TwoDArray< MvCostData > m_bipred_costs
Definition: motion.h:396
dirac::MEData::~MEData
~MEData()
Destructor.
dirac::MEData::GlobalMotionInliers
const TwoDArray< int > & GlobalMotionInliers(const int ref_id) const
Get the inliers for each reference.
Definition: motion.h:374
dirac::MvCostData::MvCostData
MvCostData()
Constructor.
Definition: motion.h:196
common.h
dirac::MvData::m_sb_split
TwoDArray< int > m_sb_split
Definition: motion.h:298
dirac::MvData::GlobalMotionVectors
const MvArray & GlobalMotionVectors(const int ref_id) const
Get the global MVs for a reference.
Definition: motion.h:249
dirac::MVector
MotionVector< int > MVector
MVector class is a vector of ints.
Definition: motion.h:180
dirac::MvData::DC
const OneDArray< TwoDArray< ValueType > * > & DC() const
Get a reference to the vector holding component DC values.
Definition: motion.h:258
dirac::MvData::DC
TwoDArray< ValueType > & DC(CompSort cs)
Get the DC values for each component.
Definition: motion.h:252
dirac::MEData::m_pred_costs
OneDArray< TwoDArray< MvCostData > * > m_pred_costs
Definition: motion.h:390
dirac::MEData::SBCosts
TwoDArray< float > & SBCosts()
Get the SB costs.
Definition: motion.h:350
dirac::MotionVector
Motion vector class - just a pair.
Definition: motion.h:58
dirac::MEData::m_lambda_map
TwoDArray< float > m_lambda_map
Definition: motion.h:402
dirac::MEData::IntraCosts
const TwoDArray< float > & IntraCosts() const
Get the intra costs.
Definition: motion.h:341
dirac::MvData::Vectors
MvArray & Vectors(const int ref_id)
Get the MVs for a reference.
Definition: motion.h:240
dirac::MotionVector::operator>>
MotionVector< T > operator>>(const int argument) const
Bitshift of each component.
Definition: motion.h:151
dirac::MEData
Class for all the motion estimation data.
Definition: motion.h:313
dirac::MEData::DropRef
void DropRef(int ref_index)
drop the data relating to one reference
dirac::MvData::Mode
TwoDArray< PredMode > & Mode()
Get the block prediction modes.
Definition: motion.h:261
dirac::MEData::BiPredCosts
TwoDArray< MvCostData > & BiPredCosts()
Get the bipred costs.
Definition: motion.h:344
dirac::MEData::m_intra_block_ratio
float m_intra_block_ratio
Definition: motion.h:408
dirac::MvCostData::total
float total
Total=SAD+mvcost.
Definition: motion.h:210
dirac::MvData::m_gm_vectors
OneDArray< MvArray * > m_gm_vectors
Definition: motion.h:289
dirac::MvFloatArray
TwoDArray< MotionVector< float > > MvFloatArray
An array of float-based motion vectors for doing global motion calcs.
Definition: motion.h:189
dirac::MEData::InitMEData
void InitMEData()
dirac::MvData::InitMvData
void InitMvData()
dirac::MotionVector::y
T y
Definition: motion.h:95
dirac::MvData::Vectors
const MvArray & Vectors(const int ref_id) const
Get the MVs for a reference.
Definition: motion.h:243
dirac::MEData::SBCosts
const TwoDArray< float > & SBCosts() const
Get the SB costs.
Definition: motion.h:353
dirac::MvData::m_gm_params
OneDArray< OneDArray< float > * > m_gm_params
Definition: motion.h:301
dirac::MEData::BiPredCosts
const TwoDArray< MvCostData > & BiPredCosts() const
Get the bipred costs.
Definition: motion.h:347
dirac::MEData::IntraCosts
TwoDArray< float > & IntraCosts()
Get the intra costs.
Definition: motion.h:338
dirac::MEData::IntraBlockRatio
float IntraBlockRatio() const
Get the proportion of intra blocks.
Definition: motion.h:356
dirac::MvMean
MVector MvMean(const MVector &mv1, const MVector &mv2)
Return the mean of two motion vectors.
dirac::MvData::SBSplit
const TwoDArray< int > & SBSplit() const
Get the SB split level.
Definition: motion.h:270
dirac::MvData::GetPicPredParams
const PicturePredParams & GetPicPredParams() const
Return a reference to the local picture prediction params.
Definition: motion.h:237
dirac::MvData::SBSplit
TwoDArray< int > & SBSplit()
Get the SB split level.
Definition: motion.h:267
dirac::PicturePredParams
Structure to hold motion parameters when motion comp is used.
Definition: common.h:967
dirac::MotionVector::operator[]
const T & operator[](const int pos) const
Array-style element access.
Definition: motion.h:91
dirac::MvData::m_vectors
OneDArray< MvArray * > m_vectors
Definition: motion.h:286
dirac::MvData::Mode
const TwoDArray< PredMode > & Mode() const
Get the block prediction modes.
Definition: motion.h:264
dirac::MvData::m_modes
TwoDArray< PredMode > m_modes
Definition: motion.h:292
dirac::MvCostData::SetTotal
void SetTotal(const float lambda)
Definition: motion.h:201
dirac::MvData::DC
const TwoDArray< ValueType > & DC(CompSort cs) const
Get the DC values for each component.
Definition: motion.h:255
dirac
Definition of class SequenceHeaderByteIO.
Definition: accessunit_byteio.h:51
dirac::GetUMean
unsigned int GetUMean(std::vector< unsigned int > &values)
Return the mean of a set of unsigned integer values.
dirac::MotionVector::operator-
MotionVector< T > operator-(const MotionVector< T > &argument) const
Subtraction.
Definition: motion.h:111
dirac::MvCostData::mvcost
float mvcost
The (Lagrangian-weighted) motion vector cost - the difference of a motion vector from its neighbourin...
Definition: motion.h:207
dirac::MEData::operator>>
friend std::istream & operator>>(std::istream &stream, MEData &me_data)
Overloaded operator>> for input of data from (file) stream.
dirac::MEData::SetLambdaMap
void SetLambdaMap(const int num_refs, const float lambda)
Set up the lambda map by detecting motion discontinuities.
dirac::MvData
Class for all the motion vector data.
Definition: motion.h:219
dirac::CompSort
CompSort
Types of picture component.
Definition: common.h:87
dirac::MEData::MEData
MEData(const PicturePredParams &predparams, const int num_refs=2)
Constructor.
dirac::MEData::FindTransitions
void FindTransitions(TwoDArray< bool > &trans_map, const int ref_num)
dirac::Norm1
int Norm1(const MVector &mv)
Return the sum of the lengths of a motion vector's componets.
Definition: motion.h:436
dirac::MotionVector::operator+
MotionVector< T > operator+(const MotionVector< T > &argument) const
Addition.
Definition: motion.h:101
dirac::MEData::LambdaMap
const TwoDArray< float > & LambdaMap() const
Get a lambda value for a given block and level.
Definition: motion.h:368
dirac::MvData::MvData
MvData(const PicturePredParams &predparams, const int num_refs)
Constructor.
dirac::MEData::m_SB_costs
TwoDArray< float > m_SB_costs
Definition: motion.h:399
dirac::GetSMean
int GetSMean(std::vector< int > &values)
Return the mean of a set of signed integer values.
dirac::MEData::PredCosts
TwoDArray< MvCostData > & PredCosts(const int ref_id)
Get the block cost structures for each reference.
Definition: motion.h:332
dirac::MotionVector::operator[]
T & operator[](const int pos)
Array-style element access.
Definition: motion.h:88
dirac::MvData::GlobalMotionParameters
const OneDArray< float > & GlobalMotionParameters(const int ref_id) const
Get the global motion model parameters.
Definition: motion.h:276
dirac::MvData::GetPicPredParams
PicturePredParams & GetPicPredParams()
Return a reference to the local picture prediction params.
Definition: motion.h:234
dirac::operator>>
std::istream & operator>>(std::istream &stream, TwoDArray< T > &array)
A function for inserting array data.
Definition: arrays.h:581

© 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.