Updating Contact email
[BrunelResearch-dirac.git] / libdirac_motionest / me_subpel.cpp
blobc662083f23861e147a10481434431be1f03ef609
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 #include <libdirac_motionest/me_subpel.h>
39 #include <libdirac_encoder/enc_queue.h>
40 using namespace dirac;
42 #include <iostream>
44 using std::vector;
46 SubpelRefine::SubpelRefine(const EncoderParams& encp):
47 m_encparams(encp),
48 m_nshift(4)
50 //define the relative coordinates of the four neighbours
51 m_nshift[0].x = -1;
52 m_nshift[0].y = 0;
54 m_nshift[1].x = -1;
55 m_nshift[1].y = -1;
57 m_nshift[2].x = 0;
58 m_nshift[2].y = -1;
60 m_nshift[3].x = 1;
61 m_nshift[3].y = -1;
65 void SubpelRefine::DoSubpel( EncQueue& my_buffer,int pic_num )
67 m_predparams = &(my_buffer.GetPicture(pic_num).GetMEData().GetPicPredParams() );
69 //main loop for the subpel refinement
70 int ref1,ref2;
72 const PictureSort psort = my_buffer.GetPicture(pic_num).GetPparams().PicSort();
74 if (psort.IsInter())
76 // Get the references
77 const vector<int>& refs = my_buffer.GetPicture(pic_num).GetPparams().Refs();
79 int num_refs = refs.size();
80 ref1 = refs[0];
81 if (num_refs>1)
82 ref2 = refs[1];
83 else
84 ref2 = ref1;
86 const PicArray& pic_data = my_buffer.GetPicture(pic_num).DataForME(m_encparams.CombinedME());
87 const PicArray& refup1_data = my_buffer.GetPicture(ref1).UpDataForME(m_encparams.CombinedME());
88 const PicArray& refup2_data = my_buffer.GetPicture(ref2).UpDataForME(m_encparams.CombinedME());
90 MEData& me_data = my_buffer.GetPicture(pic_num).GetMEData();
92 // Now match the pictures
93 MatchPic( pic_data , refup1_data , me_data ,1 );
95 if (ref1 != ref2 )
96 MatchPic( pic_data , refup2_data , me_data ,2 );
101 void SubpelRefine::MatchPic(const PicArray& pic_data , const PicArray& refup_data , MEData& me_data ,
102 int ref_id)
104 // Match a picture against a single reference. Loop over all the blocks
105 // doing the matching
107 // Initialisation //
108 ////////////////////
110 // Provide aliases for the appropriate motion vector data components
111 MvArray& mv_array = me_data.Vectors( ref_id );
112 TwoDArray<MvCostData>& pred_costs = me_data.PredCosts( ref_id );
114 // Provide a block matching object to do the work
115 BlockMatcher my_bmatch( pic_data , refup_data , m_predparams->LumaBParams(2) ,
116 m_predparams->MVPrecision() , mv_array , pred_costs );
118 // Do the work //
119 /////////////////
121 // Loop over all the blocks, doing the work
123 for (int yblock=0 ; yblock<m_predparams->YNumBlocks() ; ++yblock){
124 for (int xblock=0 ; xblock<m_predparams->XNumBlocks() ; ++xblock){
125 DoBlock(xblock , yblock , my_bmatch , me_data , ref_id );
126 }// xblock
127 }// yblock
131 void SubpelRefine::DoBlock(const int xblock , const int yblock ,
132 BlockMatcher& my_bmatch, MEData& me_data , const int ref_id )
134 // For each block, home into the sub-pixel vector
136 // Provide aliases for the appropriate motion vector data components
137 MvArray& mv_array = me_data.Vectors( ref_id );
139 const MVector mv_pred = GetPred( xblock , yblock , mv_array );
140 const float loc_lambda = me_data.LambdaMap()[yblock][xblock];
142 my_bmatch.RefineMatchSubp( xblock , yblock , mv_pred, loc_lambda );
145 MVector SubpelRefine::GetPred(int xblock,int yblock,const MvArray& mvarray)
147 MVector mv_pred;
148 ImageCoords n_coords;
149 vector<MVector> neighbours;
151 if (xblock>0 && yblock>0 && xblock<mvarray.LastX())
154 for (int i=0 ; i<m_nshift.Length() ; ++i)
156 n_coords.x = xblock+m_nshift[i].x;
157 n_coords.y = yblock+m_nshift[i].y;
158 neighbours.push_back(mvarray[n_coords.y][n_coords.x]);
160 }// i
162 else
164 for (int i=0 ; i<m_nshift.Length(); ++i )
166 n_coords.x = xblock+m_nshift[i].x;
167 n_coords.y = yblock+m_nshift[i].y;
168 if (n_coords.x>=0 && n_coords.y>=0 && n_coords.x<mvarray.LengthX() && n_coords.y<mvarray.LengthY())
169 neighbours.push_back(mvarray[n_coords.y][n_coords.x]);
170 }// i
173 mv_pred = MvMedian(neighbours);
175 return mv_pred;