00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include <libdirac_motionest/pixel_match.h>
00039 #include <libdirac_motionest/block_match.h>
00040 #include <libdirac_common/motion.h>
00041 #include <libdirac_common/frame_buffer.h>
00042 #include <libdirac_motionest/downconvert.h>
00043 #include <libdirac_motionest/me_mode_decn.h>
00044 #include <libdirac_motionest/me_subpel.h>
00045 using namespace dirac;
00046
00047 #include <cmath>
00048 #include <vector>
00049
00050 using std::vector;
00051 using std::log;
00052
00053 PixelMatcher::PixelMatcher( const EncoderParams& encp):
00054 m_encparams(encp)
00055 {}
00056
00057
00058 void PixelMatcher::DoSearch(const FrameBuffer& my_buffer, int frame_num, MEData& me_data)
00059 {
00060
00061
00062
00063
00064 int ref1,ref2;
00065
00066
00067 const PicArray& pic_data = my_buffer.GetComponent( frame_num , Y_COMP );
00068
00069 const vector<int>& refs = my_buffer.GetFrame( frame_num ).GetFparams().Refs();
00070 ref1 = refs[0];
00071 if (refs.size()>1)
00072 ref2 = refs[1];
00073 else
00074 ref2 = ref1;
00075
00076
00077 const PicArray& ref1_data = my_buffer.GetComponent(ref1 , Y_COMP);
00078 const PicArray& ref2_data = my_buffer.GetComponent(ref2 , Y_COMP);
00079
00080
00081 m_fsort = my_buffer.GetFrame(frame_num).GetFparams().FSort();
00082
00083
00084 m_depth = ( int) std::min( log(((double) pic_data.LengthX())/12.0)/log(2.0) ,
00085 log(((double) pic_data.LengthY())/12.0)/log(2.0) );
00086
00087
00088 OneDArray<PicArray*> ref1_down( Range( 1 , m_depth ) );
00089 OneDArray<PicArray*> ref2_down( Range( 1 , m_depth ) );
00090 OneDArray<PicArray*> pic_down( Range( 1 , m_depth ) );
00091 OneDArray<MEData*> me_data_set( Range( 1 , m_depth ) );
00092
00093
00094 MakePicHierarchy( pic_data , pic_down );
00095 MakePicHierarchy( ref1_data , ref1_down );
00096 if (ref1 != ref2)
00097 MakePicHierarchy( ref2_data , ref2_down );
00098
00099 MakeMEDataHierarchy( pic_down , me_data_set );
00100
00101
00103
00104
00105 m_level = m_depth;
00106
00107 MatchPic( *(pic_down[m_depth]) , *(ref1_down[m_depth]) , *(me_data_set[m_depth]) ,
00108 *(me_data_set[m_depth]) , 1 );
00109 if ( ref1 != ref2 )
00110 MatchPic( *(pic_down[m_depth]) , *(ref2_down[m_depth]) , *(me_data_set[m_depth]) ,
00111 *(me_data_set[m_depth]) , 2 );
00112
00113
00114 for ( m_level=m_depth-1 ; m_level>=1 ; --m_level )
00115 {
00116 MatchPic( *(pic_down[m_level]) , *(ref1_down[m_level]) , *(me_data_set[m_level]) ,
00117 *(me_data_set[m_level+1]) , 1 );
00118 if (ref1!=ref2)
00119 MatchPic( *(pic_down[m_level]) , *(ref2_down[m_level]) , *(me_data_set[m_level]) ,
00120 *(me_data_set[m_level+1]) , 2 );
00121
00122 }
00123
00124
00125 m_level = 0;
00126 MatchPic( pic_data , ref1_data, me_data , *(me_data_set[1]) , 1 );
00127 if ( ref1 != ref2 )
00128 MatchPic( pic_data , ref2_data , me_data , *(me_data_set[1]) , 2 );
00129
00130
00131 TidyPics( pic_down );
00132 TidyPics( ref1_down );
00133 if (ref1 != ref2)
00134 TidyPics( ref2_down );
00135 TidyMEData( me_data_set );
00136
00137 }
00138
00139 void PixelMatcher::MakePicHierarchy(const PicArray& data ,
00140 OneDArray< PicArray* >& down_data)
00141 {
00142
00143 DownConverter mydcon;
00144
00145
00146 int scale_factor = 1;
00147 for (int i=1 ; i<=m_depth;++i)
00148 {
00149
00150 scale_factor*=2;
00151 down_data[i] = new PicArray( data.LengthY()/scale_factor , data.LengthX()/scale_factor);
00152 }
00153
00154
00155 if (m_depth>0)
00156 {
00157 mydcon.DoDownConvert( data , *(down_data[1]) );
00158
00159 for (int i=1 ; i<m_depth ; ++i)
00160 mydcon.DoDownConvert( *(down_data[i]) , *(down_data[i+1]) );
00161
00162 }
00163 }
00164
00165 void PixelMatcher::MakeMEDataHierarchy(const OneDArray< PicArray*>& down_data,
00166 OneDArray< MEData* >& me_data_set )
00167 {
00168
00169 int xnumblocks , ynumblocks;
00170 const OLBParams bparams = m_encparams.LumaBParams(2);
00171
00172 for (int i=1 ; i<=m_depth;++i)
00173 {
00174
00175 xnumblocks = down_data[i]->LengthX()/bparams.Xbsep();
00176 ynumblocks = down_data[i]->LengthY()/bparams.Ybsep();
00177
00178 if (( down_data[i]->LengthX() )%bparams.Xbsep() != 0)
00179 xnumblocks++;
00180
00181 if (( down_data[i]->LengthY() )%bparams.Ybsep() != 0)
00182 ynumblocks++;
00183
00184 me_data_set[i] = new MEData( 0 , 0 , xnumblocks , ynumblocks );
00185 }
00186
00187 }
00188
00189 void PixelMatcher::TidyPics( OneDArray< PicArray*>& down_data )
00190 {
00191 for (int i=1 ; i <= m_depth ; ++i)
00192 {
00193 delete down_data[i];
00194 }
00195
00196 }
00197
00198 void PixelMatcher::TidyMEData( OneDArray< MEData*>& me_data_set )
00199 {
00200 for (int i=1 ; i <= m_depth ; ++i)
00201 {
00202 delete me_data_set[i];
00203 }
00204
00205 }
00206
00207
00208
00209 void PixelMatcher::MatchPic(const PicArray& pic_data , const PicArray& ref_data , MEData& me_data ,
00210 const MvData& guide_data, int ref_id)
00211 {
00212
00213
00215
00216
00217 if ( m_level == m_depth )
00218 {
00219 m_xr = 5;
00220 m_yr = 5;
00221 }
00222 else if ( m_level == m_depth-1 )
00223 {
00224 m_xr = 4;
00225 m_yr = 4;
00226 }
00227 else
00228 {
00229 m_xr = 2;
00230 m_yr = 2;
00231 }
00232
00233
00234
00235 MvArray& mv_array = me_data.Vectors( ref_id );
00236 const MvArray& guide_array = guide_data.Vectors( ref_id );
00237 TwoDArray<MvCostData>& pred_costs = me_data.PredCosts( ref_id );
00238
00239
00240 BlockMatcher my_bmatch( pic_data , ref_data , m_encparams.LumaBParams(2) , mv_array , pred_costs );
00241
00242 float loc_lambda( 0.0 );
00243
00244
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256 m_cand_list.clear();
00257 MVector zero_mv( 0 , 0 );
00258 AddNewVlistD( m_cand_list , zero_mv , m_xr , m_yr);
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268 m_mv_prediction = zero_mv;
00269
00270
00271 m_lambda = 0.0;
00272 DoBlock(0, 0 , pred_costs , mv_array, guide_array , my_bmatch);
00273
00274
00275
00276 m_lambda = loc_lambda / float( m_encparams.YNumBlocks() );
00277 for ( int xpos=1 ; xpos<mv_array.LengthX() ; ++xpos )
00278 {
00279 m_mv_prediction = mv_array[0][xpos-1];
00280 DoBlock(xpos, 0 , pred_costs , mv_array, guide_array , my_bmatch);
00281 }
00282
00283
00284 for ( int ypos=1 ; ypos<mv_array.LengthY() ; ++ypos )
00285 {
00286
00287
00288 m_mv_prediction = mv_array[ypos-1][0];
00289 m_lambda = loc_lambda/float(m_encparams.XNumBlocks());
00290 DoBlock(0, ypos , pred_costs , mv_array, guide_array , my_bmatch );
00291
00292
00293 m_lambda = loc_lambda;
00294 for ( int xpos=1 ; xpos<mv_array.LastX() ; ++xpos )
00295 {
00296 m_mv_prediction = MvMedian( mv_array[ypos][xpos-1],
00297 mv_array[ypos-1][xpos],
00298 mv_array[ypos-1][xpos+1]);
00299 DoBlock(xpos, ypos , pred_costs , mv_array, guide_array , my_bmatch );
00300
00301 }
00302
00303
00304 m_lambda = loc_lambda/float( m_encparams.XNumBlocks() );
00305 m_mv_prediction = MvMean( mv_array[ypos-1][ mv_array.LastX() ],
00306 mv_array[ypos][ mv_array.LastX()-1 ]);
00307 DoBlock(mv_array.LastX() , ypos , pred_costs , mv_array, guide_array , my_bmatch );
00308
00309 }
00310
00311 }
00312
00313 void PixelMatcher::DoBlock(int xpos, int ypos ,
00314 TwoDArray<MvCostData>& pred_costs,
00315 MvArray& mv_array,
00316 const MvArray& guide_array,
00317 BlockMatcher& block_match)
00318 {
00319
00320
00321
00322
00323 if ( m_level<m_depth )
00324 AddNewVlistD( m_cand_list , guide_array[ ypos>>1 ][ xpos>>1 ] * 2 , m_xr , m_yr );
00325
00326
00327 AddNewVlistD( m_cand_list , m_mv_prediction , m_xr , m_yr );
00328
00329
00331
00332 block_match.FindBestMatch( xpos , ypos , m_cand_list, m_mv_prediction, m_lambda );
00333
00334
00335
00336 m_cand_list.erase( m_cand_list.begin()+1 , m_cand_list.end() );
00337 }