Updating Contact email
[BrunelResearch-dirac.git] / libdirac_motionest / block_match.h
blob21839b227a70ef72f541a0c811e5a1d0d919c0ae
1 /* ***** BEGIN LICENSE BLOCK *****
3 * $Id$ $Name$
5 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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.
16 * The Original Code is BBC Research and Development code.
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.
23 * Contributor(s): Thomas Davies (Original Author)
25 * Alternatively, the contents of this file may be used under the terms of
26 * the GNU General Public License Version 2 (the "GPL"), or the GNU Lesser
27 * Public License Version 2.1 (the "LGPL"), in which case the provisions of
28 * the GPL or the LGPL are applicable instead of those above. If you wish to
29 * allow use of your version of this file only under the terms of the either
30 * the GPL or LGPL and not to allow others to use your version of this file
31 * under the MPL, indicate your decision by deleting the provisions above
32 * and replace them with the notice and other provisions required by the GPL
33 * or LGPL. If you do not delete the provisions above, a recipient may use
34 * your version of this file under the terms of any one of the MPL, the GPL
35 * or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #ifndef _BLOCK_MATCH_H_
39 #define _BLOCK_MATCH_H_
41 #include <libdirac_motionest/me_utils.h>
42 #include <vector>
43 //handles the business of finding the best block match
45 namespace dirac
48 typedef std::vector< std::vector< MVector > > CandidateList;
50 //! Add a new motion vector list of neighbours of a vector to the set of lists
52 Add a new motion vector list to the set of lists consisting of the
53 square neighbourhood [mv.x-xr,mv.x+xr] by
54 [mv.y-yr,mv.y+yr]. Vectors that already occur in previous lists are
55 not added.
57 void AddNewVlist( CandidateList& vect_list , const MVector& mv , const int xr , const int yr );
59 //! Add a new motion vector list to the set of lists for sub-pixel matching
61 Add a new motion vector list to the set of lists consisting of the
62 vectors of the form (mv.x+m*step,mv.y+n*step) where m lies between
63 -xr and xr and n lies between -yr and yr. Vectors that already occur
64 in previous lists are not added.
66 void AddNewVlist( CandidateList& vect_list , const MVector& mv , const int xr , const int yr , const int step );
68 //! Add a new motion vector list of diagnonal neighbours of a vector to the set of lists
70 Add a new motion vector list to the set of lists consisting of the
71 diagonal neighbourhood of height 2yr+1 pixels and width 2xr+1 centred
72 on \param mv.
73 Vectors that already occur in previous lists are not added.
75 void AddNewVlistD( CandidateList& vect_list , const MVector& mv , const int xr, const int yr);
77 //! Add a motion vector to the set of motion vector lists
78 /*!
79 Add a motion vector to the set of motion vector lists, making sure
80 it's not a duplicate.
82 void AddVect( CandidateList& vect_list , const MVector& mv , const int list_num);
84 //! Get the (absolute) variation between two motion vectors
85 /*!
86 Return the variation between two motion vectors, computed as the sum
87 of absolute differences of their components.
89 ValueType GetVar(const MVector& mv1,const MVector& mv2);
91 //! Get the (absolute) variation between a motion vector and a list of motion vectors
92 /*!
93 Return the variation between a motion vector and a list of motion
94 vectos, computed as the sum of absolute differences between the
95 components of the vector and the median vector produced by the list of
96 vectors
98 ValueType GetVar(const std::vector<MVector>& pred_list,const MVector& mv);
101 //! Class to do block matching
103 // Subsumes FindBestMatch and FindBestMatchSubpel
104 class BlockMatcher
106 public:
107 //! Constructor
109 Constructor
110 \param ref_data the reference picture component
111 \param pic_data the picture being matched
112 \param bparams the (overlapped) block parameters to be used for the matching
113 \param precision the number of bits of precision being used for estimation
114 \param mv_array the array of vectors we're going to write into
115 \param cost_array the array of costs we're going to write into
118 BlockMatcher( const PicArray& ref_data ,
119 const PicArray& pic_data ,
120 const OLBParams& bparams ,
121 const int precision ,
122 const MvArray& mv_array ,
123 const TwoDArray< MvCostData >& cost_array);
125 ~BlockMatcher();
127 //! Find the best matching vector from a list of candidates
129 Find the best matching vector from a list of candidates.
130 \param xpos the horizontal location of the block being matched
131 \param ypos the vertical location of the block being matched
132 \param cand_list the list of candidate vectors
133 \param mv_prediction Prediction used for each block used to control the variation in the motion vector field.
134 \param list_start index into the candidate vectors list
136 void FindBestMatchPel( const int xpos , const int ypos ,
137 const CandidateList& cand_list,
138 const MVector& mv_prediction,
139 const int list_start);
141 //! Find the best matching vector from a list of candidates, to sub-pixel accuracy (TBC: merge with FindBestMatch)
143 Find the best matching vector from a list of candidates.
144 \param xpos the horizontal location of the block being matched
145 \param ypos the vertical location of the block being matched
146 \param cand_list the list of candidate vectors
147 \param mv_prediction the prediction for the motion vector
148 \param lambda the Lagrangian parameter
150 void FindBestMatchSubp( const int xpos, const int ypos,
151 const CandidateList& cand_list,
152 const MVector& mv_prediction,
153 const float lambda);
155 void RefineMatchSubp(const int xpos, const int ypos,
156 const MVector& mv_prediction,
157 const float lambda);
159 //! Get a measure of the difference between a motion vector and a prediction
161 Get a measure of the difference between a motion vector and a prediction
162 \param predmv the predicting motion vector
163 \param mv the motion vector
165 ValueType GetVar( const MVector& predmv , const MVector& mv ) const;
167 //! Get a measure of the difference between a motion vector and a prediction, to 1/8pel accuracy
169 Get a measure of the difference between a motion vector and a prediction, to 1/8pel accuracy
170 \param predmv the predicting motion vector
171 \param mv the motion vector
173 ValueType GetVarUp( const MVector& predmv , const MVector& mv ) const;
175 void SetPrecision( const int n ){ m_precision = n; }
177 private:
178 // Local copies of the picture and reference
179 const PicArray& m_pic_data;
180 const PicArray& m_ref_data;
182 // Local copy of the motion vector array being populated
183 const MvArray& m_mv_array;
185 // Local copy of the costs being determined through the matching
186 const TwoDArray< MvCostData >& m_cost_array;
188 // Block difference elements. Will choose between them depending
189 // on whether we're at the edge of the picture
190 PelBlockDiff m_peldiff;
192 OneDArray<BlockDiffUp* > m_subpeldiff;
194 // The block parameters we're using
195 OLBParams m_bparams;
197 // The maximum variations allowed in calculating motion vector costs
198 const int m_var_max;
199 const int m_var_max_up;
201 // The motion vector precision
202 int m_precision;
206 } // namespace dirac
207 #endif