1 /* ***** BEGIN LICENSE BLOCK *****
2 *
3 * $Id: motion_comp_test.cpp,v 1.18 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 Steve Bearcroft's code.
17 *
18 * The Initial Developer of the Original Code is Steve Bearcroft.
19 * Portions created by the Initial Developer are Copyright (C) 2004.
20 * All Rights Reserved.
21 *
22 * Contributor(s): Steve Bearcroft (Original Author)
23 *                 Anuradha Suraparaju
24 *
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 ***** */
37 #include "core_suite.h"
38 #include "motion_comp_test.h"
39 #include "frames_test.h"
40 
41 #include <libdirac_common/picture.h>
42 #include <libdirac_common/picture_buffer.h>
43 #include <libdirac_common/mot_comp.h>
44 using namespace dirac;
45 
46 #include <memory>
47 
48 //NOTE: ensure that the suite is added to the default registry in
49 //cppunit_testsuite.cpp
50 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION (MotionCompTest, coreSuiteName());
51 
52 #define X_SIZE  352
53 #define Y_SIZE  288
54 
MotionCompTest()55 MotionCompTest::MotionCompTest()
56 {
57 }
58 
~MotionCompTest()59 MotionCompTest::~MotionCompTest()
60 {
61 }
62 
setUp()63 void MotionCompTest::setUp()
64 {
65 }
66 
tearDown()67 void MotionCompTest::tearDown()
68 {
69 }
70 
71 
setupMV1Data(const PicturePredParams & ppp,int mv_x,int mv_y,PredMode mode)72 MvData* setupMV1Data(const PicturePredParams& ppp, int mv_x, int mv_y, PredMode mode)
73 {
74     MvData* mv_data = new MvData(ppp, 2);
75     MvArray& arr = mv_data->Vectors(1);
76     for (int i =arr.FirstY(); i <= arr.LastY(); i++)
77     {
78         for (int j =arr.FirstX(); j <= arr.LastX(); j++)
79         {
80             arr[i][j].x = mv_x;
81             arr[i][j].y = mv_y;
82             mv_data->Mode()[i][j] = mode;
83         }
84     }
85     return mv_data;
86 }
87 
setupMV2Data(MvData * mv_data,int mv_x,int mv_y)88 void setupMV2Data(MvData* mv_data, int mv_x, int mv_y)
89 {
90     MvArray& arr = mv_data->Vectors(2);
91     for (int i =arr.FirstY(); i <= arr.LastY(); i++)
92     {
93         for (int j =arr.FirstX(); j <= arr.LastX(); j++)
94         {
95             arr[i][j].x = mv_x;
96             arr[i][j].y = mv_y;
97         }
98     }
99 }
100 
testZeroMotionComp()101 void MotionCompTest::testZeroMotionComp()
102 {
103     for (int i = 0; i < 4; ++i)
104     {
105         testZeroMotionComp(static_cast<MVPrecisionType>(i));
106     }
107 }
108 
testZeroMotionComp(MVPrecisionType precision)109 void MotionCompTest::testZeroMotionComp(MVPrecisionType precision)
110 {
111     PictureBuffer pbuffer;
112     CodecParams cp(VIDEO_FORMAT_CIF, INTER_PICTURE, 1, true);
113     PicturePredParams &ppp = cp.GetPicPredParams();
114     OLBParams bparams(12, 12, 8, 8);
115 
116     ppp.SetMVPrecision(precision);
117     ppp.SetBlockSizes(bparams, format420 );
118     ppp.SetXNumSB( X_SIZE / ppp.LumaBParams(0).Xbsep() );
119     ppp.SetYNumSB( Y_SIZE / ppp.LumaBParams(0).Ybsep() );
120     ppp.SetYNumSB( Y_SIZE / ppp.LumaBParams(0).Ybsep() );
121 
122     ppp.SetXNumBlocks( 4*ppp.XNumSB() );
123     ppp.SetYNumBlocks( 4*ppp.YNumSB() );
124 
125     // MotionCompensator mc( cp );
126 
127     MvData* mv_data = setupMV1Data(ppp, 0, 0, REF1_ONLY);
128 
129     PictureParams pp(format420, X_SIZE, Y_SIZE, 8, 8);
130 
131     pp.SetPicSort(PictureSort::IntraRefPictureSort());
132     pp.SetPictureNum(0);
133     pbuffer.PushPicture(pp);
134     PicturesTest::setupPicture(pbuffer.GetPicture(0),0);
135 
136     pp.SetPicSort(PictureSort::InterRefPictureSort());
137     pp.SetPictureNum(1);
138     pp.Refs().push_back(0);
139     pbuffer.PushPicture(pp);
140     PicturesTest::zeroPicture(pbuffer.GetPicture(1));
141 
142     pp.SetPictureNum(2);
143     pbuffer.PushPicture(pp);
144     PicturesTest::zeroPicture(pbuffer.GetPicture(2));
145 
146     Picture* ref_pics[2] = { &pbuffer.GetPicture(1), &pbuffer.GetPicture(2) };
147 
148     // mc.CompensatePicture(ADD, pbuffer, 1, *mv_data);
149     MotionCompensator::CompensatePicture(ppp, ADD, *mv_data, &pbuffer.GetPicture(0), ref_pics );
150 
151     // MotionCompensator mc2( cp );
152 
153     //too many rounding errors for this to be exactly true;
154     //CPPUNIT_ASSERT (PicturesTest::equalPictures (pbuffer.GetPicture(0), pbuffer.GetPicture(1)));
155     // mc2.CompensatePicture(SUBTRACT, pbuffer, 1, *mv_data);
156     MotionCompensator::CompensatePicture(ppp, SUBTRACT, *mv_data, &pbuffer.GetPicture(0), ref_pics );
157 
158     CPPUNIT_ASSERT (PicturesTest::equalPictures (pbuffer.GetPicture(2), pbuffer.GetPicture(1)));
159     delete mv_data;
160 }
161 
testAddandSubMotionComp()162 void MotionCompTest::testAddandSubMotionComp()
163 {
164     for (int i = 0; i < 4; ++i)
165     {
166         testAddandSubMotionComp(static_cast<MVPrecisionType>(i));
167     }
168 }
169 
testAddandSubMotionComp(MVPrecisionType precision)170 void MotionCompTest::testAddandSubMotionComp(MVPrecisionType precision)
171 {
172     PictureBuffer pbuffer;
173     CodecParams cp(VIDEO_FORMAT_CIF, INTER_PICTURE, 1, true);
174     PicturePredParams &ppp = cp.GetPicPredParams();
175     OLBParams bparams(12, 12, 8, 8);
176     ppp.SetMVPrecision(precision);
177     ppp.SetBlockSizes(bparams, format420 );
178     ppp.SetXNumSB( X_SIZE / ppp.LumaBParams(0).Xbsep() );
179     ppp.SetYNumSB( Y_SIZE / ppp.LumaBParams(0).Ybsep() );
180 
181     ppp.SetXNumBlocks( 4*ppp.XNumSB() );
182     ppp.SetYNumBlocks( 4*ppp.YNumSB() );
183 
184 
185     MvData* mv_data = setupMV1Data(ppp, 5, 5, REF1_ONLY);
186 
187     PictureParams pp(format420, X_SIZE, Y_SIZE, 8, 8);
188 
189     pp.SetPicSort(PictureSort::IntraRefPictureSort());
190     pp.SetPictureNum(0);
191     pbuffer.PushPicture(pp);
192     PicturesTest::setupPicture(pbuffer.GetPicture(0),0);
193 
194     pp.SetPicSort(PictureSort::InterRefPictureSort());
195     pp.SetPictureNum(1);
196     pp.Refs().push_back(0);
197     pbuffer.PushPicture(pp);
198     PicturesTest::zeroPicture(pbuffer.GetPicture(1));
199 
200     pp.SetPictureNum(2);
201     pbuffer.PushPicture(pp);
202     PicturesTest::zeroPicture(pbuffer.GetPicture(2));
203 
204     Picture* ref_pics[2] = { &pbuffer.GetPicture(1), &pbuffer.GetPicture(2) };
205 
206     // MotionCompensator mc( cp );
207     // mc.CompensatePicture(ADD, pbuffer, 1, *mv_data);
208     MotionCompensator::CompensatePicture(ppp, ADD, *mv_data, &pbuffer.GetPicture(0), ref_pics );
209 
210     // MotionCompensator mc2( cp );
211     // mc2.CompensatePicture(SUBTRACT, pbuffer, 1, *mv_data);
212     MotionCompensator::CompensatePicture(ppp, SUBTRACT, *mv_data, &pbuffer.GetPicture(0), ref_pics );
213 
214     CPPUNIT_ASSERT (PicturesTest::equalPictures (pbuffer.GetPicture(2), pbuffer.GetPicture(1)));
215     delete mv_data;
216 }
217 
testL2_picture()218 void MotionCompTest::testL2_picture()
219 {
220     for (int i = 0; i < 4; ++i)
221     {
222         testL2_picture(static_cast<MVPrecisionType>(i));
223     }
224 }
225 
testL2_picture(MVPrecisionType precision)226 void MotionCompTest::testL2_picture(MVPrecisionType precision)
227 {
228     PictureBuffer pbuffer;
229     CodecParams cp(VIDEO_FORMAT_CIF, INTER_PICTURE, 1, true);
230     PicturePredParams &ppp = cp.GetPicPredParams();
231     OLBParams bparams(12, 12, 8, 8);
232     ppp.SetMVPrecision(precision);
233     ppp.SetBlockSizes(bparams, format420 );
234     ppp.SetXNumSB( X_SIZE / ppp.LumaBParams(0).Xbsep() );
235     ppp.SetYNumSB( Y_SIZE / ppp.LumaBParams(0).Ybsep() );
236 
237     ppp.SetXNumBlocks( 4*ppp.XNumSB() );
238     ppp.SetYNumBlocks( 4*ppp.YNumSB() );
239 
240 
241     MvData* mv_data = setupMV1Data(ppp, 5, 5, REF1_ONLY);
242 
243     PictureParams pp(format420, X_SIZE, Y_SIZE, 8, 8);
244 
245     pp.SetPicSort(PictureSort::IntraRefPictureSort());
246     pp.SetPictureNum(0);
247     pbuffer.PushPicture(pp);
248     PicturesTest::setupPicture(pbuffer.GetPicture(0),0);
249 
250     pp.SetPicSort(PictureSort::InterNonRefPictureSort());
251     pp.SetPictureNum(1);
252     pp.Refs().push_back(0);
253     pbuffer.PushPicture(pp);
254     PicturesTest::zeroPicture(pbuffer.GetPicture(1));
255 
256     pp.SetPictureNum(2);
257     pbuffer.PushPicture(pp);
258     PicturesTest::zeroPicture(pbuffer.GetPicture(2));
259 
260     Picture* ref_pics[2] = { &pbuffer.GetPicture(1), &pbuffer.GetPicture(2) };
261 
262     // MotionCompensator mc( cp );
263     // mc.CompensatePicture(ADD, pbuffer, 1, *mv_data);
264     MotionCompensator::CompensatePicture(ppp, ADD, *mv_data, &pbuffer.GetPicture(0), ref_pics );
265 
266     // MotionCompensator mc2( cp );
267     // mc2.CompensatePicture(SUBTRACT, pbuffer, 1, *mv_data);
268     MotionCompensator::CompensatePicture(ppp, SUBTRACT, *mv_data, &pbuffer.GetPicture(0), ref_pics );
269 
270     CPPUNIT_ASSERT (PicturesTest::equalPictures (pbuffer.GetPicture(2), pbuffer.GetPicture(1)));
271     delete mv_data;
272 }
273 
274 
testI_picture()275 void MotionCompTest::testI_picture()
276 {
277     PictureBuffer pbuffer;
278     CodecParams cp(VIDEO_FORMAT_CIF, INTER_PICTURE, 2, true);
279     PicturePredParams &ppp = cp.GetPicPredParams();
280     OLBParams bparams(12, 12, 8, 8);
281     ppp.SetBlockSizes(bparams, format420 );
282     ppp.SetXNumSB( X_SIZE / ppp.LumaBParams(0).Xbsep() );
283     ppp.SetYNumSB( Y_SIZE / ppp.LumaBParams(0).Ybsep() );
284 
285     ppp.SetXNumBlocks( 4*ppp.XNumSB() );
286     ppp.SetYNumBlocks( 4*ppp.YNumSB() );
287 
288 
289 
290     MvData* mv_data = setupMV1Data(ppp, 5, 5, REF1_ONLY);
291 
292     PictureParams pp(format420, X_SIZE, Y_SIZE, 8, 8);
293 
294     pp.SetPicSort(PictureSort::IntraRefPictureSort());
295     pp.SetPictureNum(0);
296     pbuffer.PushPicture(pp);
297     PicturesTest::setupPicture(pbuffer.GetPicture(0),0);
298 
299     pp.SetPicSort(PictureSort::IntraRefPictureSort());
300     pp.SetPictureNum(1);
301     pp.Refs().push_back(0);
302     pbuffer.PushPicture(pp);
303     PicturesTest::setupPicture(pbuffer.GetPicture(1),0);
304 
305     Picture* ref_pics[2] = { &pbuffer.GetPicture(1), &pbuffer.GetPicture(1) };
306 
307     // MotionCompensator mc( cp );
308     // mc.CompensatePicture(ADD, pbuffer, 1, *mv_data);
309     MotionCompensator::CompensatePicture(ppp, ADD, *mv_data, &pbuffer.GetPicture(0), ref_pics );
310 
311     CPPUNIT_ASSERT (PicturesTest::equalPictures (pbuffer.GetPicture(0), pbuffer.GetPicture(1)));
312     delete mv_data;
313 }
314 
315 
testRef2()316 void MotionCompTest::testRef2()
317 {
318     for (int i = 0; i < 4; ++i)
319     {
320         testRef2(static_cast<MVPrecisionType>(i));
321     }
322 }
323 
testRef2(MVPrecisionType precision)324 void MotionCompTest::testRef2(MVPrecisionType precision)
325 {
326     PictureBuffer pbuffer;
327     CodecParams cp(VIDEO_FORMAT_CIF, INTER_PICTURE, 2, true);
328     PicturePredParams &ppp = cp.GetPicPredParams();
329     OLBParams bparams(12, 12, 8, 8);
330     ppp.SetMVPrecision(precision);
331     ppp.SetBlockSizes(bparams, format420 );
332     ppp.SetXNumSB( X_SIZE / ppp.LumaBParams(0).Xbsep() );
333     ppp.SetYNumSB( Y_SIZE / ppp.LumaBParams(0).Ybsep() );
334 
335     ppp.SetXNumBlocks( 4*ppp.XNumSB() );
336     ppp.SetYNumBlocks( 4*ppp.YNumSB() );
337 
338 
339 
340     MvData* mv_data = setupMV1Data(ppp, 5, 5, REF2_ONLY);
341     setupMV2Data(mv_data, 0, 0);
342 
343     PictureParams pp(format420, X_SIZE, Y_SIZE, 8, 8);
344 
345     pp.SetPicSort(PictureSort::IntraRefPictureSort());
346     pp.SetPictureNum(0);
347     pbuffer.PushPicture(pp);
348     PicturesTest::setupPicture(pbuffer.GetPicture(0),0);
349 
350     pp.SetPicSort(PictureSort::InterRefPictureSort());
351     pp.SetPictureNum(1);
352     pp.Refs().push_back(2);
353     pp.Refs().push_back(0);
354     pbuffer.PushPicture(pp);
355     PicturesTest::zeroPicture(pbuffer.GetPicture(1));
356 
357     pp.SetPictureNum(2);
358     pbuffer.PushPicture(pp);
359     PicturesTest::zeroPicture(pbuffer.GetPicture(2));
360 
361     Picture* ref_pics[2] = { &pbuffer.GetPicture(1), &pbuffer.GetPicture(2) };
362 
363     // MotionCompensator mc( cp );
364     // mc.CompensatePicture(ADD, pbuffer, 1, *mv_data);
365     MotionCompensator::CompensatePicture(ppp, ADD, *mv_data, &pbuffer.GetPicture(0), ref_pics );
366 
367     //too many rounding errors for this to be exactly true;
368     //CPPUNIT_ASSERT (PicturesTest::equalPictures (pbuffer.GetPicture(0), pbuffer.GetPicture(1)));
369 
370     // MotionCompensator mc2( cp );
371     // mc2.CompensatePicture(SUBTRACT, pbuffer, 1, *mv_data);
372     MotionCompensator::CompensatePicture(ppp, SUBTRACT, *mv_data, &pbuffer.GetPicture(0), ref_pics );
373 
374     CPPUNIT_ASSERT (PicturesTest::equalPictures (pbuffer.GetPicture(2), pbuffer.GetPicture(1)));
375     delete mv_data;
376 }
377 
testRef1and2()378 void MotionCompTest::testRef1and2()
379 {
380     for (int i = 0; i < 4; ++i)
381     {
382         testRef1and2(static_cast<MVPrecisionType>(i));
383     }
384 }
385 
testRef1and2(MVPrecisionType precision)386 void MotionCompTest::testRef1and2(MVPrecisionType precision)
387 {
388     PictureBuffer pbuffer;
389     CodecParams cp(VIDEO_FORMAT_CIF, INTER_PICTURE, 2, true);
390     PicturePredParams &ppp = cp.GetPicPredParams();
391     OLBParams bparams(12, 12, 8, 8);
392     ppp.SetMVPrecision(precision);
393     ppp.SetBlockSizes(bparams, format420 );
394     ppp.SetXNumSB( X_SIZE / ppp.LumaBParams(0).Xbsep() );
395     ppp.SetYNumSB( Y_SIZE / ppp.LumaBParams(0).Ybsep() );
396 
397     ppp.SetXNumBlocks( 4*ppp.XNumSB() );
398     ppp.SetYNumBlocks( 4*ppp.YNumSB() );
399 
400     MvData* mv_data = setupMV1Data(ppp, 5, 5, REF1_ONLY);
401     setupMV2Data(mv_data, 5, 5);
402 
403     MvData* mv_data1 = setupMV1Data(ppp, 7, 3, REF2_ONLY);
404     setupMV2Data(mv_data1, 7, 3);
405 
406     MvData* mv_data2 = setupMV1Data(ppp, 5, 5, REF1AND2);
407     setupMV2Data(mv_data2, 7, 3);
408 
409     PictureParams pp(format420, X_SIZE, Y_SIZE, 8, 8);
410 
411     pp.SetPicSort(PictureSort::IntraRefPictureSort());
412     pp.SetPictureNum(0);
413     pbuffer.PushPicture(pp);
414     PicturesTest::setupPicture(pbuffer.GetPicture(0),0);
415 
416     pp.SetPicSort(PictureSort::IntraRefPictureSort());
417     pp.SetPictureNum(1);
418     pbuffer.PushPicture(pp);
419     PicturesTest::setupPicture(pbuffer.GetPicture(1),50);
420 
421     pp.SetPicSort(PictureSort::InterRefPictureSort());
422     pp.SetPictureNum(2);
423     pp.Refs().push_back(0);
424     pp.Refs().push_back(1);
425     pbuffer.PushPicture(pp);
426     PicturesTest::zeroPicture(pbuffer.GetPicture(2));
427 
428     pp.SetPictureNum(3);
429     pbuffer.PushPicture(pp);
430     PicturesTest::zeroPicture(pbuffer.GetPicture(3));
431 
432     //MotionCompensator mc( cp );
433 
434     Picture* ref_pics[2];
435     ref_pics[0] = &pbuffer.GetPicture(0);
436     ref_pics[1] = &pbuffer.GetPicture(1);
437 
438     //mc.CompensatePicture(ADD, pbuffer, 2, *mv_data);
439     MotionCompensator::CompensatePicture(ppp, ADD, *mv_data, &pbuffer.GetPicture(2), ref_pics);
440 
441     //MotionCompensator mc2( cp );
442 
443     //mc2.CompensatePicture(ADD, pbuffer, 2, *mv_data1);
444     MotionCompensator::CompensatePicture(ppp, ADD, *mv_data1, &pbuffer.GetPicture(2), ref_pics);
445 
446     // MotionCompensator mc3( cp );
447 
448     // mc3.CompensatePicture(ADD, pbuffer, 3, *mv_data2);
449     MotionCompensator::CompensatePicture(ppp, ADD, *mv_data2, &pbuffer.GetPicture(3), ref_pics);
450 
451     //MotionCompensator mc4( cp );
452 
453     //mc4.CompensatePicture(ADD, pbuffer, 3, *mv_data2);
454     MotionCompensator::CompensatePicture(ppp, ADD, *mv_data2, &pbuffer.GetPicture(3), ref_pics);
455 
456     CPPUNIT_ASSERT (PicturesTest::almostEqualPictures (pbuffer.GetPicture(2), pbuffer.GetPicture(3), 5    ));
457     delete mv_data;
458     delete mv_data1;
459     delete mv_data2;
460 }
461