forked from pinterf/mvtools
-
Notifications
You must be signed in to change notification settings - Fork 1
/
PlaneOfBlocks.cpp
8214 lines (7017 loc) · 319 KB
/
PlaneOfBlocks.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Author: Manao
// Copyright(c)2006 A.G.Balakhnin aka Fizick - global motion, overlap, mode, refineMVs
// See legal notice in Copying.txt for more information
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA, or visit
// http://www.gnu.org/copyleft/gpl.html .
#include "AnaFlags.h"
#include "AvstpWrapper.h"
#include "commonfunctions.h"
#include "DCTClass.h"
#include "DCTFactory.h"
#include "debugprintf.h"
#include "FakePlaneOfBlocks.h"
#include "MVClip.h"
#include "MVFrame.h"
#include "MVPlane.h"
#include "PlaneOfBlocks.h"
#include "Padding.h"
#include "profile.h"
#include <emmintrin.h> // SSE2
#include <pmmintrin.h> // SSE3
#include <tmmintrin.h> // SSSE3
#include <smmintrin.h> // SSE4
#include <algorithm>
#include <cmath>
#include <stdexcept>
#include <stdint.h>
#include <map>
#include <tuple>
static unsigned int SadDummy(const uint8_t *, int , const uint8_t *, int )
{
return 0;
}
PlaneOfBlocks::PlaneOfBlocks(int _nBlkX, int _nBlkY, int _nBlkSizeX, int _nBlkSizeY, int _nPel, int _nLevel, int _nFlags, int _nOverlapX, int _nOverlapY,
int _xRatioUV, int _yRatioUV, int _pixelsize, int _bits_per_pixel,
conc::ObjPool <DCTClass> *dct_pool_ptr,
bool mt_flag, int _chromaSADscale, int _optSearchOption, float _scaleCSADfine, int _iUseSubShift, int _DMFlags,
IScriptEnvironment* env)
: nBlkX(_nBlkX)
, nBlkY(_nBlkY)
, nBlkSizeX(_nBlkSizeX)
, nBlkSizeY(_nBlkSizeY)
, nSqrtBlkSize2D((int)(std::sqrt((float)_nBlkSizeX * _nBlkSizeY) + 0.5f)) // precalc for DCT 2.7.38-
, nBlkCount(_nBlkX * _nBlkY)
, nPel(_nPel)
, nLogPel(ilog2(_nPel)) // nLogPel=0 for nPel=1, 1 for nPel=2, 2 for nPel=4, i.e. (x*nPel) = (x<<nLogPel)
, nScale(iexp2(_nLevel))
, nLogScale(_nLevel)
, nFlags(_nFlags)
, nOverlapX(_nOverlapX)
, nOverlapY(_nOverlapY)
, xRatioUV(_xRatioUV) // PF
, nLogxRatioUV(ilog2(_xRatioUV))
, yRatioUV(_yRatioUV)
, nLogyRatioUV(ilog2(_yRatioUV))
, pixelsize(_pixelsize) // PF
, pixelsize_shift(ilog2(pixelsize)) // 161201
, bits_per_pixel(_bits_per_pixel) // PF
, _mt_flag(mt_flag)
, chromaSADscale(_chromaSADscale)
, optSearchOption(_optSearchOption)
, scaleCSADfine(_scaleCSADfine)
, iUseSubShift(_iUseSubShift)
, SAD(0)
, LUMA(0)
// , VAR(0)
, BLITLUMA(0)
, BLITCHROMA(0)
, SADCHROMA(0)
, SATD(0)
// , vectors(nBlkCount)
// , vectors(nBlkCount, env)
, vectors(_nBlkX* _nBlkY, env)
, smallestPlane((_nFlags & MOTION_SMALLEST_PLANE) != 0)
, isse((_nFlags & MOTION_USE_ISSE) != 0)
, chroma((_nFlags & MOTION_USE_CHROMA_MOTION) != 0)
, dctpitch(AlignNumber(_nBlkSizeX, 16) * _pixelsize)
, _dct_pool_ptr(dct_pool_ptr)
, freqArray()
, verybigSAD(3 * _nBlkSizeX * _nBlkSizeY * (pixelsize == 4 ? 1 : (1 << bits_per_pixel))) // * 256, pixelsize==2 -> 65536. Float:1
, dctmode(0)
, _workarea_fact(nBlkSizeX, nBlkSizeY, dctpitch, nLogxRatioUV, nLogyRatioUV, pixelsize, bits_per_pixel)
, _workarea_pool()
, _gvect_estim_ptr(0)
, _gvect_result_count(0)
{
_workarea_pool.set_factory(_workarea_fact);
// half must be more than max vector length, which is (framewidth + Padding) * nPel
freqArray[0].resize(8192 * _nPel * 2);
freqArray[1].resize(8192 * _nPel * 2);
// for nFlags, we use CPU_xxxx constants instead of Avisynth's CPUF_xxx values, because there are extra bits here
sse2 = (bool)(nFlags & CPU_SSE2); // no tricks for really old processors. If SSE2 is reported, use it
sse41 = (bool)(nFlags & CPU_SSE4);
avx = (bool)(nFlags & CPU_AVX);
avx2 = (bool)(nFlags & CPU_AVX2);
avx512 = (bool)(nFlags & CPU_AVX512);
// bool ssd = (bool)(nFlags & MOTION_USE_SSD);
// bool satd = (bool)(nFlags & MOTION_USE_SATD);
// New experiment from 2.7.18.22: keep LumaSAD:chromaSAD ratio to 4:2
// luma SAD : chroma SAD
// YV12 4:(1+1) = 4:2 (this 4:2 is the new standard from 2.7.18.22 even for YV24)
// YV16 4:(2+2) = 4:4
// YV24 4:(4+4) = 4:8
// that means that nSCD1 should be normalize not by subsampling but with user's chromaSADscale
// YV12 YV16 YV24
// nLogXRatioUV 1 1 0
// nLogYRatioUV 1 0 0
// effective_chromaSADscales: (shift right chromaSAD)
// chromaSADscale=0 -> 0 1 2 // default. YV12:no change. YV24: chroma SAD is divided by 4 (shift right 2)
// =1 -> -1 0 1 // YV12: shift right -1 (=left 1, =*2) YV24: divide by 2 (shift right 1)
// =2 -> -2 -1 0 // YV12: shift right -2 (=left 2, =*4) YV24: no change
effective_chromaSADscale = (2 - (nLogxRatioUV + nLogyRatioUV));
effective_chromaSADscale -= chromaSADscale; // user parameter to have larger magnitude for chroma SAD
// effective effective_chromaSADscale can be -2..2.
// when chromaSADscale is zero (default), effective_chromaSADscale is 0..2
// effective_chromaSADscale = 0; pre-2.7.18.22 format specific chroma SAD weight
// ssd=false;
// satd=false;
// globalMVPredictor.x = zeroMV.x;
// globalMVPredictor.y = zeroMV.y;
// globalMVPredictor.sad = zeroMV.sad;
memset(&vectors[0], 0, vectors.size() * sizeof(vectors[0]));
// function's pointers
// Sad_C: SadFunction.cpp
// Var_c: Variance.h PF nowhere used!!!
// Luma_c: Variance.h
// Copy_C: CopyCode
SATD = SadDummy; //for now disable SATD if default functions are used
// in overlaps.h
// OverlapsLsbFunction
// OverlapsFunction
// in M(V)DegrainX: DenoiseXFunction
arch_t arch;
if (isse && avx512)
arch = USE_AVX512;
else if (isse && avx2)
arch = USE_AVX2;
else if (isse && avx)
arch = USE_AVX;
else if (isse && sse41)
arch = USE_SSE41;
else if (isse && sse2)
arch = USE_SSE2;
else
arch = NO_SIMD;
SAD = get_sad_function(nBlkSizeX, nBlkSizeY, bits_per_pixel, arch);
SADCHROMA = get_sad_function(nBlkSizeX / xRatioUV, nBlkSizeY / yRatioUV, bits_per_pixel, arch);
DM_Luma = new DisMetric(nBlkSizeX, nBlkSizeY, bits_per_pixel, pixelsize, arch, _DMFlags);
DM_Chroma = new DisMetric(nBlkSizeX / xRatioUV, nBlkSizeY / yRatioUV, bits_per_pixel, pixelsize, arch, _DMFlags);
BLITLUMA = get_copy_function(nBlkSizeX, nBlkSizeY, pixelsize, arch);
BLITCHROMA = get_copy_function(nBlkSizeX / xRatioUV, nBlkSizeY / yRatioUV, pixelsize, arch);
//VAR = get_var_function(nBlkSizeX/xRatioUV, nBlkSizeY/yRatioUV, pixelsize, arch); // variance.h PF: no VAR
LUMA = get_luma_function(nBlkSizeX, nBlkSizeY, pixelsize, arch); // variance.h
SATD = get_satd_function(nBlkSizeX, nBlkSizeY, pixelsize, arch); // P.F. 2.7.0.22d SATD made live
if (SATD == nullptr)
SATD = SadDummy;
if (chroma) {
if (BLITCHROMA == nullptr) {
// we don't have env ptr here
env->ThrowError("MVTools: no BLITCHROMA function for block size %dx%d", nBlkSizeX / xRatioUV, nBlkSizeY / yRatioUV);
}
if (SADCHROMA == nullptr) {
// we don't have env ptr here
env->ThrowError("MVTools: no SADCHROMA function for block size %dx%d", nBlkSizeX / xRatioUV, nBlkSizeY / yRatioUV);
}
}
if (!chroma)
{
SADCHROMA = SadDummy;
}
// DTL's new test 2.7.46
for (auto &fn : ExhaustiveSearchFunctions)
fn = nullptr;
// Get additional test functions only when optSearchOption is not 0.
// MAnalyze and MRecalculate has now an optsearchoption parameter.
if (optSearchOption != 0) {
// fill function array multiple functions, because nSearchParam can change during search
// block sizes and chroma remain the same
// not implemented ones are nullptr
if ((nBlkSizeX == 8 || nBlkSizeX == 16) && (nBlkSizeY == 8 || nBlkSizeY == 16) && pixelsize == 1 && !chroma) {
for (int iSearchParam = 0; iSearchParam <= MAX_SUPPORTED_EXH_SEARCHPARAM; iSearchParam++)
ExhaustiveSearchFunctions[iSearchParam] = get_ExhaustiveSearchFunction(nBlkSizeX, nBlkSizeY, iSearchParam, bits_per_pixel, arch);
}
}
if (optSearchOption == 2 && (/*arch != USE_AVX2 ||*/ nPel != 1)) // do not see at Rocket Lake ???
{
env->ThrowError("optSearchOption=2 require AVX2 or more CPU and pel=1");
}
// for debug:
// SAD = x264_pixel_sad_4x4_mmx2;
// VAR = Var_C<8>;
// LUMA = Luma_C<8>;
// BLITLUMA = Copy_C<16,16>;
// BLITCHROMA = Copy_C<8,8>; // idem
// SADCHROMA = x264_pixel_sad_8x8_mmx2;
#ifdef ALLOW_DCT
if (_dct_pool_ptr != 0)
{
DCTFactory & dct_fact =
dynamic_cast <DCTFactory &> (_dct_pool_ptr->use_factory());
dctmode = dct_fact.get_dctmode();
// Preallocate DCT objects using FFTW, to make sure they are allocated
// during the plug-in construction stage, to avoid as possible
// concurrency with FFTW instantiations from other plug-ins.
if (dct_fact.use_fftw())
{
const int nbr_threads =
(_mt_flag)
? AvstpWrapper::use_instance().get_nbr_threads()
: 1;
std::vector <DCTClass *> dct_stack;
dct_stack.reserve(nbr_threads);
bool err_flag = false;
for (int dct_cnt = 0; dct_cnt < nbr_threads && !err_flag; ++dct_cnt)
{
DCTClass * dct_ptr = _dct_pool_ptr->take_obj();
if (dct_ptr == 0)
{
err_flag = true;
}
else
{
dct_stack.push_back(dct_ptr);
}
}
while (!dct_stack.empty())
{
DCTClass * dct_ptr = dct_stack.back();
_dct_pool_ptr->return_obj(*dct_ptr);
dct_stack.pop_back();
}
if (err_flag)
{
throw std::runtime_error(
"MVTools: error while trying to allocate DCT objects using FFTW."
);
}
}
}
#endif
}
PlaneOfBlocks::~PlaneOfBlocks()
{
// Nothing
}
void PlaneOfBlocks::SearchMVs(
MVFrame *_pSrcFrame, MVFrame *_pRefFrame,
SearchType st, int stp, int lambda, sad_t lsad, int pnew,
int plevel, int flags, sad_t *out, const VECTOR * globalMVec,
short *outfilebuf, int fieldShift, sad_t * pmeanLumaChange,
int divideExtra, int _pzero, int _pglobal, sad_t _badSAD, int _badrange, bool meander, int *vecPrev, bool _tryMany,
int optPredictorType
)
{
// - - - - - - - - - - - - - - - - - - - - - - - - -
// Frame- and plane-related data preparation
zeroMVfieldShifted.x = 0;
zeroMVfieldShifted.y = fieldShift;
zeroMVfieldShifted.sad = 0; // vs
#ifdef ALLOW_DCT
// pMeanLumaChange is scaled by bits_per_pixel
// bit we keep this factor in the ~16 range
dctweight16 = std::min((sad_t)16, (abs(*pmeanLumaChange) >> (bits_per_pixel-8)) / (nBlkSizeX*nBlkSizeY)); //equal dct and spatial weights for meanLumaChange=8 (empirical)
#endif // ALLOW_DCT
badSAD = _badSAD;
badrange = _badrange;
_glob_mv_pred_def.x = globalMVec->x * nPel; // v1.8.2
_glob_mv_pred_def.y = globalMVec->y * nPel + fieldShift;
_glob_mv_pred_def.sad = globalMVec->sad;
// int nOutPitchY = nBlkX * (nBlkSizeX - nOverlapX) + nOverlapX;
// int nOutPitchUV = (nBlkX * (nBlkSizeX - nOverlapX) + nOverlapX) / 2; // xRatioUV=2
// char debugbuf[128];
// wsprintf(debugbuf,"MVCOMP1: nOutPitchUV=%d, nOverlap=%d, nBlkX=%d, nBlkSize=%d",nOutPitchUV, nOverlap, nBlkX, nBlkSize);
// OutputDebugString(debugbuf);
// write the plane's header
WriteHeaderToArray(out);
nFlags |= flags;
pSrcFrame = _pSrcFrame;
pRefFrame = _pRefFrame;
#if (ALIGN_SOURCEBLOCK > 1)
nSrcPitch_plane[0] = pSrcFrame->GetPlane(YPLANE)->GetPitch();
if (chroma)
{
nSrcPitch_plane[1] = pSrcFrame->GetPlane(UPLANE)->GetPitch();
nSrcPitch_plane[2] = pSrcFrame->GetPlane(VPLANE)->GetPitch();
}
nSrcPitch[0] = pixelsize * nBlkSizeX;
nSrcPitch[1] = pixelsize * nBlkSizeX / xRatioUV; // PF xRatio instead of /2: after 2.7.0.22c;
nSrcPitch[2] = pixelsize * nBlkSizeX / xRatioUV;
for (int i = 0; i < 3; i++) {
nSrcPitch[i] = AlignNumber(nSrcPitch[i], ALIGN_SOURCEBLOCK); // e.g. align reference block pitch to mod16 e.g. at blksize 24
}
#else // ALIGN_SOURCEBLOCK
nSrcPitch[0] = pSrcFrame->GetPlane(YPLANE)->GetPitch();
if (chroma)
{
nSrcPitch[1] = pSrcFrame->GetPlane(UPLANE)->GetPitch();
nSrcPitch[2] = pSrcFrame->GetPlane(VPLANE)->GetPitch();
}
#endif // ALIGN_SOURCEBLOCK
nRefPitch[0] = pRefFrame->GetPlane(YPLANE)->GetPitch();
if (chroma)
{
nRefPitch[1] = pRefFrame->GetPlane(UPLANE)->GetPitch();
nRefPitch[2] = pRefFrame->GetPlane(VPLANE)->GetPitch();
}
if (iUseSubShift > 0) // send blocksize to MVPlanes
{
pRefFrame->GetPlane(YPLANE)->SetBlockSize(nBlkSizeX, nBlkSizeY);
if (chroma)
{
pRefFrame->GetPlane(UPLANE)->SetBlockSize(nBlkSizeX >> nLogxRatioUV, nBlkSizeY >> nLogxRatioUV);
pRefFrame->GetPlane(VPLANE)->SetBlockSize(nBlkSizeX >> nLogxRatioUV, nBlkSizeY >> nLogxRatioUV);
}
}
searchType = st; // ( nLogScale == 0 ) ? st : EXHAUSTIVE;
nSearchParam = stp; // *nPel; // v1.8.2 - redesigned in v1.8.5
_lambda_level = lambda / (nPel * nPel);
if (plevel == 1)
{
_lambda_level *= nScale; // scale lambda - Fizick
}
else if (plevel == 2)
{
_lambda_level *= nScale*nScale;
}
temporal = (vecPrev != 0);
if (vecPrev)
{
vecPrev += 1; // Just skips the header
}
penaltyZero = _pzero;
pglobal = _pglobal;
badcount = 0;
tryMany = _tryMany;
planeSAD = 0;
sumLumaChange = 0;
_out = out;
_outfilebuf = outfilebuf;
_vecPrev = vecPrev;
_meander_flag = meander;
_pnew = pnew;
_lsad = lsad;
_predictorType = optPredictorType; // v2.7.46
// - - - - - - - - - - - - - - - - - - - - - - - - -
penaltyNew = _pnew; // penalty for new vector
LSAD = _lsad; // SAD limit for lambda using
Slicer slicer(_mt_flag); // fixme: mt bug
if (bits_per_pixel == 8)
{
if (optSearchOption == 2)
{
slicer.start(nBlkY, *this, &PlaneOfBlocks::search_mv_slice_SO2<uint8_t>, 4);
}
else
if (optSearchOption == 3)
{
slicer.start(nBlkY, *this, &PlaneOfBlocks::search_mv_slice_SO3<uint8_t>, 4); // AVX2 multi-block
}
else
if (optSearchOption == 4)
{
slicer.start(nBlkY, *this, &PlaneOfBlocks::search_mv_slice_SO4<uint8_t>, 4); // AVX512 multi-block
}
else
{
slicer.start(nBlkY, *this, &PlaneOfBlocks::search_mv_slice<uint8_t>, 4);
}
}
else
slicer.start(nBlkY, *this, &PlaneOfBlocks::search_mv_slice<uint16_t>, 4);
slicer.wait();
// - - - - - - - - - - - - - - - - - - - - - - - - -
if (smallestPlane)
{
*pmeanLumaChange = (sad_t)(sumLumaChange / nBlkCount); // for all finer planes
}
}
void PlaneOfBlocks::RecalculateMVs(
MVClip & mvClip, MVFrame *_pSrcFrame, MVFrame *_pRefFrame,
SearchType st, int stp, int lambda, sad_t lsad, int pnew,
int flags, int *out,
short *outfilebuf, int fieldShift, sad_t thSAD, int divideExtra, int smooth, bool meander,
int optPredictorType
)
{
// - - - - - - - - - - - - - - - - - - - - - - - - -
// Frame- and plane-related data preparation
zeroMVfieldShifted.x = 0;
zeroMVfieldShifted.y = fieldShift;
zeroMVfieldShifted.sad = 0; // vs
#ifdef ALLOW_DCT
dctweight16 = 8;//min(16,abs(*pmeanLumaChange)/(nBlkSizeX*nBlkSizeY)); //equal dct and spatial weights for meanLumaChange=8 (empirical)
#endif // ALLOW_DCT
// Actually the global predictor is not used in RecalculateMVs().
_glob_mv_pred_def.x = 0;
_glob_mv_pred_def.y = fieldShift;
_glob_mv_pred_def.sad = 9999999;
// int nOutPitchY = nBlkX * (nBlkSizeX - nOverlapX) + nOverlapX;
// int nOutPitchUV = (nBlkX * (nBlkSizeX - nOverlapX) + nOverlapX) / 2; // xRatioUV=2
// char debugbuf[128];
// wsprintf(debugbuf,"MVCOMP1: nOutPitchUV=%d, nOverlap=%d, nBlkX=%d, nBlkSize=%d",nOutPitchUV, nOverlap, nBlkX, nBlkSize);
// OutputDebugString(debugbuf);
// write the plane's header
WriteHeaderToArray(out);
nFlags |= flags;
pSrcFrame = _pSrcFrame;
pRefFrame = _pRefFrame;
#if (ALIGN_SOURCEBLOCK > 1)
nSrcPitch_plane[0] = pSrcFrame->GetPlane(YPLANE)->GetPitch();
if (chroma)
{
nSrcPitch_plane[1] = pSrcFrame->GetPlane(UPLANE)->GetPitch();
nSrcPitch_plane[2] = pSrcFrame->GetPlane(VPLANE)->GetPitch();
}
nSrcPitch[0] = pixelsize * nBlkSizeX;
nSrcPitch[1] = pixelsize * nBlkSizeX / xRatioUV; // PF after 2.7.0.22c
nSrcPitch[2] = pixelsize * nBlkSizeX / xRatioUV; // PF after 2.7.0.22c
for (int i = 0; i < 3; i++) {
nSrcPitch[i] = AlignNumber(nSrcPitch[i], ALIGN_SOURCEBLOCK); // e.g. align reference block pitch to mod16 e.g. at blksize 24
}
#else // ALIGN_SOURCEBLOCK
nSrcPitch[0] = pSrcFrame->GetPlane(YPLANE)->GetPitch();
if (chroma)
{
nSrcPitch[1] = pSrcFrame->GetPlane(UPLANE)->GetPitch();
nSrcPitch[2] = pSrcFrame->GetPlane(VPLANE)->GetPitch();
}
#endif // ALIGN_SOURCEBLOCK
nRefPitch[0] = pRefFrame->GetPlane(YPLANE)->GetPitch();
if (chroma)
{
nRefPitch[1] = pRefFrame->GetPlane(UPLANE)->GetPitch();
nRefPitch[2] = pRefFrame->GetPlane(VPLANE)->GetPitch();
}
searchType = st;
nSearchParam = stp;//*nPel; // v1.8.2 - redesigned in v1.8.5
_lambda_level = lambda / (nPel * nPel);
// if (plevel==1)
// {
// _lambda_level *= nScale;// scale lambda - Fizick
// }
// else if (plevel==2)
// {
// _lambda_level *= nScale*nScale;
// }
planeSAD = 0;
sumLumaChange = 0;
_out = out;
_outfilebuf = outfilebuf;
_meander_flag = meander;
_predictorType = optPredictorType; // 2.7.46
_pnew = pnew;
_lsad = lsad;
_mv_clip_ptr = &mvClip;
_smooth = smooth;
_thSAD = thSAD;
// - - - - - - - - - - - - - - - - - - - - - - - - -
// fixme: consider disabling internal mt, it's giving inconsistent results when used
Slicer slicer(_mt_flag);
if(pixelsize==1)
slicer.start(nBlkY, *this, &PlaneOfBlocks::recalculate_mv_slice<uint8_t>, 4);
else
slicer.start(nBlkY, *this, &PlaneOfBlocks::recalculate_mv_slice<uint16_t>, 4);
slicer.wait();
// - - - - - - - - - - - - - - - - - - - - - - - - -
}
template<typename safe_sad_t, typename smallOverlapSafeSad_t>
//void PlaneOfBlocks::InterpolatePrediction(const PlaneOfBlocks &pob)
void PlaneOfBlocks::InterpolatePrediction(PlaneOfBlocks& pob)
{
int normFactor = 3 - nLogPel + pob.nLogPel;
int mulFactor = (normFactor < 0) ? -normFactor : 0;
normFactor = (normFactor < 0) ? 0 : normFactor;
int normov = (nBlkSizeX - nOverlapX) * (nBlkSizeY - nOverlapY);
int aoddx = (nBlkSizeX * 3 - nOverlapX * 2);
int aevenx = (nBlkSizeX * 3 - nOverlapX * 4);
int aoddy = (nBlkSizeY * 3 - nOverlapY * 2);
int aeveny = (nBlkSizeY * 3 - nOverlapY * 4);
// note: overlapping is still (v2.5.7) not processed properly
// PF todo make faster
// 2.7.19.22 max safe: BlkX*BlkY: sqrt(2147483647 / 3 / 255) = 1675 ,(2147483647 = 0x7FFFFFFF)
//bool isSafeBlkSizeFor8bits = (nBlkSizeX*nBlkSizeY) < 1675;
// 2.7.35:
// the limit was too small for smallOverlap case e.g. BlkSizeX=32 BlkSizeY=32 OverLapX=0 OverLapY=4
// Worst case (approximately) ax1 * ay1: (nBlkSizeX*3 * nBlkSizeY*3)
// 32 bit usability for 8 bits (10+bits are always using bigsad_t):
// (evenOrOdd_xMax * evenOrOdd_yMax) * SadMax *4 < 0x7FFFFFFF
// *4: four components before /normov
// (nBlkSizeX*3 * nBlkSizeY*3) * SadMax *4 < 0x7FFFFFFF
// SadMax: 3 full planes (worst case 4:4:4 luma and chroma)
// (nBlkSizeX*3 * nBlkSizeY*3) * (nBlkSizeX * nBlkSizeY * 255 * 3plane) *4 < 0x7FFFFFFF
// (nBlkSizeX*nBlkSizeY)^2 *9*255*3 * 4 < 0x7FFFFFFF
// nBlkSizeX*nBlkSizeY < sqrt(...)
// nBlkSizeX*nBlkSizeY < 279.24 -> 280
// => smallOverlapSafeSad_t needs to be bigsad_t when (nBlkSizeX*nBlkSizeY) >= 280
// safe_sad_t: 16 bit worst case: 16 * sad_max: 16 * 3x32x32x65536 = 4+5+5+16 > 2^31 over limit
// in case of BlockSize > 32, e.g. 128x128x65536 is even more: 7+7+16=30 bits
// generally use big_sad_t for 10+ bits
bool bNoOverlap = (nOverlapX == 0 && nOverlapY == 0);
bool bSmallOverlap = nOverlapX <= (nBlkSizeX >> 1) && nOverlapY <= (nBlkSizeY >> 1);
int iout_x, iout_y, iout_sad;
for (int l = 0, index = 0; l < nBlkY; l++)
{
for (int k = 0; k < nBlkX; k++, index++)
{
VECTOR v1, v2, v3, v4;
int i = k;
int j = l;
if (i >= 2 * pob.nBlkX)
{
i = 2 * pob.nBlkX - 1;
}
if (j >= 2 * pob.nBlkY)
{
j = 2 * pob.nBlkY - 1;
}
int offy = -1 + 2 * (j % 2);
int offx = -1 + 2 * (i % 2);
int iper2 = i / 2;
int jper2 = j / 2;
if ((i == 0) || (i >= 2 * pob.nBlkX - 1))
{
if ((j == 0) || (j >= 2 * pob.nBlkY - 1))
{
v1 = v2 = v3 = v4 = pob.vectors[iper2 + (jper2)*pob.nBlkX];
}
else
{
v1 = v2 = pob.vectors[iper2 + (jper2)*pob.nBlkX];
v3 = v4 = pob.vectors[iper2 + (jper2 + offy) * pob.nBlkX];
}
}
else if ((j == 0) || (j >= 2 * pob.nBlkY - 1))
{
v1 = v2 = pob.vectors[iper2 + (jper2)*pob.nBlkX];
v3 = v4 = pob.vectors[iper2 + offx + (jper2)*pob.nBlkX];
}
else
{
v1 = pob.vectors[iper2 + (jper2)*pob.nBlkX];
v2 = pob.vectors[iper2 + offx + (jper2)*pob.nBlkX];
v3 = pob.vectors[iper2 + (jper2 + offy) * pob.nBlkX];
v4 = pob.vectors[iper2 + offx + (jper2 + offy) * pob.nBlkX];
}
safe_sad_t tmp_sad;
if (bNoOverlap)
{
iout_x = 9 * v1.x + 3 * v2.x + 3 * v3.x + v4.x;
iout_y = 9 * v1.y + 3 * v2.y + 3 * v3.y + v4.y;
tmp_sad = 9 * (safe_sad_t)v1.sad + 3 * (safe_sad_t)v2.sad + 3 * (safe_sad_t)v3.sad + (safe_sad_t)v4.sad + 8;
}
else if (bSmallOverlap) // corrected in v1.4.11
{
int ax1 = (offx > 0) ? aoddx : aevenx;
int ax2 = (nBlkSizeX - nOverlapX) * 4 - ax1;
int ay1 = (offy > 0) ? aoddy : aeveny;
int ay2 = (nBlkSizeY - nOverlapY) * 4 - ay1;
int a11 = ax1 * ay1, a12 = ax1 * ay2, a21 = ax2 * ay1, a22 = ax2 * ay2;
iout_x = (a11 * v1.x + a21 * v2.x + a12 * v3.x + a22 * v4.x) / normov;
iout_y = (a11 * v1.y + a21 * v2.y + a12 * v3.y + a22 * v4.y) / normov;
// generic safe_sad_t is not always safe for the next calculations
tmp_sad = (safe_sad_t)(((smallOverlapSafeSad_t)a11 * v1.sad + (smallOverlapSafeSad_t)a21 * v2.sad + (smallOverlapSafeSad_t)a12 * v3.sad + (smallOverlapSafeSad_t)a22 * v4.sad) / normov);
#if 0
if (tmp_sad < 0)
_RPT1(0, "Vector and SAD Interpolate Problem: possible SAD overflow %d\n", (sad_t)tmp_sad);
#endif
}
else // large overlap. Weights are not quite correct but let it be
{
iout_x = (v1.x + v2.x + v3.x + v4.x) << 2;
iout_y = (v1.y + v2.y + v3.y + v4.y) << 2;
tmp_sad = ((safe_sad_t)v1.sad + v2.sad + v3.sad + v4.sad + 2) << 2;
}
iout_x = (iout_x >> normFactor) << mulFactor;
iout_y = (iout_y >> normFactor) << mulFactor;
iout_sad = (sad_t)(tmp_sad >> 4);
// non-temporal store require better arrangement to 64bytes - to do.
vectors[index].x = iout_x;
vectors[index].y = iout_y;
vectors[index].sad = iout_sad;
#if 0
if (vectors[index].sad < 0)
_RPT1(0, "Vector and SAD Interpolate Problem: possible SAD overflow: %d\n", vectors[index].sad);
#endif
} // for k < nBlkX
} // for l < nBlkY
}
/*
// instantiate
template void PlaneOfBlocks::InterpolatePrediction<sad_t, sad_t>(const PlaneOfBlocks &pob);
template void PlaneOfBlocks::InterpolatePrediction<sad_t, bigsad_t>(const PlaneOfBlocks &pob);
template void PlaneOfBlocks::InterpolatePrediction<bigsad_t, bigsad_t>(const PlaneOfBlocks &pob);
*/
// instantiate
template void PlaneOfBlocks::InterpolatePrediction<sad_t, sad_t>(PlaneOfBlocks& pob);
template void PlaneOfBlocks::InterpolatePrediction<sad_t, bigsad_t>(PlaneOfBlocks& pob);
template void PlaneOfBlocks::InterpolatePrediction<bigsad_t, bigsad_t>(PlaneOfBlocks& pob);
template<typename safe_sad_t, typename smallOverlapSafeSad_t>
void PlaneOfBlocks::InterpolatePrediction_sse(PlaneOfBlocks& pob)
{
int normFactor = 3 - nLogPel + pob.nLogPel;
int mulFactor = (normFactor < 0) ? -normFactor : 0;
normFactor = (normFactor < 0) ? 0 : normFactor;
int normov = (nBlkSizeX - nOverlapX) * (nBlkSizeY - nOverlapY);
__m128 xmm_normov_rcp = _mm_rcp_ps(_mm_set1_ps((float)(normov)));
int aoddx = (nBlkSizeX * 3 - nOverlapX * 2);
int aevenx = (nBlkSizeX * 3 - nOverlapX * 4);
int aoddy = (nBlkSizeY * 3 - nOverlapY * 2);
int aeveny = (nBlkSizeY * 3 - nOverlapY * 4);
// note: overlapping is still (v2.5.7) not processed properly
// PF todo make faster
// 2.7.19.22 max safe: BlkX*BlkY: sqrt(2147483647 / 3 / 255) = 1675 ,(2147483647 = 0x7FFFFFFF)
//bool isSafeBlkSizeFor8bits = (nBlkSizeX*nBlkSizeY) < 1675;
// 2.7.35:
// the limit was too small for smallOverlap case e.g. BlkSizeX=32 BlkSizeY=32 OverLapX=0 OverLapY=4
// Worst case (approximately) ax1 * ay1: (nBlkSizeX*3 * nBlkSizeY*3)
// 32 bit usability for 8 bits (10+bits are always using bigsad_t):
// (evenOrOdd_xMax * evenOrOdd_yMax) * SadMax *4 < 0x7FFFFFFF
// *4: four components before /normov
// (nBlkSizeX*3 * nBlkSizeY*3) * SadMax *4 < 0x7FFFFFFF
// SadMax: 3 full planes (worst case 4:4:4 luma and chroma)
// (nBlkSizeX*3 * nBlkSizeY*3) * (nBlkSizeX * nBlkSizeY * 255 * 3plane) *4 < 0x7FFFFFFF
// (nBlkSizeX*nBlkSizeY)^2 *9*255*3 * 4 < 0x7FFFFFFF
// nBlkSizeX*nBlkSizeY < sqrt(...)
// nBlkSizeX*nBlkSizeY < 279.24 -> 280
// => smallOverlapSafeSad_t needs to be bigsad_t when (nBlkSizeX*nBlkSizeY) >= 280
// safe_sad_t: 16 bit worst case: 16 * sad_max: 16 * 3x32x32x65536 = 4+5+5+16 > 2^31 over limit
// in case of BlockSize > 32, e.g. 128x128x65536 is even more: 7+7+16=30 bits
// generally use big_sad_t for 10+ bits
bool bNoOverlap = (nOverlapX == 0 && nOverlapY == 0);
bool bSmallOverlap = nOverlapX <= (nBlkSizeX >> 1) && nOverlapY <= (nBlkSizeY >> 1);
int iout_x, iout_y, iout_sad;
// prefetch all vectors array
int iSize_of_vectors = pob.vectors.size() * sizeof(vectors[0]);
for (int i = 0; i < iSize_of_vectors; i += CACHE_LINE_SIZE)
{
_mm_prefetch(const_cast<const CHAR*>(reinterpret_cast<const CHAR*>(&pob.vectors[0] + i)), _MM_HINT_T0);
}
for (int l = 0, index = 0; l < nBlkY; l++)
{
for (int k = 0; k < nBlkX; k++, index++)
{
VECTOR v1, v2, v3, v4;
int i = k;
int j = l;
if (i >= 2 * pob.nBlkX)
{
i = 2 * pob.nBlkX - 1;
}
if (j >= 2 * pob.nBlkY)
{
j = 2 * pob.nBlkY - 1;
}
int offy = -1 + 2 * (j % 2);
int offx = -1 + 2 * (i % 2);
int iper2 = i / 2;
int jper2 = j / 2;
if ((i == 0) || (i >= 2 * pob.nBlkX - 1))
{
if ((j == 0) || (j >= 2 * pob.nBlkY - 1))
{
v1 = v2 = v3 = v4 = pob.vectors[iper2 + (jper2)*pob.nBlkX];
}
else
{
v1 = v2 = pob.vectors[iper2 + (jper2)*pob.nBlkX];
v3 = v4 = pob.vectors[iper2 + (jper2 + offy) * pob.nBlkX];
}
}
else if ((j == 0) || (j >= 2 * pob.nBlkY - 1))
{
v1 = v2 = pob.vectors[iper2 + (jper2)*pob.nBlkX];
v3 = v4 = pob.vectors[iper2 + offx + (jper2)*pob.nBlkX];
}
else
{
v1 = pob.vectors[iper2 + (jper2)*pob.nBlkX];
v2 = pob.vectors[iper2 + offx + (jper2)*pob.nBlkX];
v3 = pob.vectors[iper2 + (jper2 + offy) * pob.nBlkX];
v4 = pob.vectors[iper2 + offx + (jper2 + offy) * pob.nBlkX];
}
safe_sad_t tmp_sad;
if (bNoOverlap)
{
iout_x = 9 * v1.x + 3 * v2.x + 3 * v3.x + v4.x;
iout_y = 9 * v1.y + 3 * v2.y + 3 * v3.y + v4.y;
tmp_sad = 9 * (safe_sad_t)v1.sad + 3 * (safe_sad_t)v2.sad + 3 * (safe_sad_t)v3.sad + (safe_sad_t)v4.sad + 8;
}
else if (bSmallOverlap) // corrected in v1.4.11
{
int ax1 = (offx > 0) ? aoddx : aevenx;
int ax2 = (nBlkSizeX - nOverlapX) * 4 - ax1;
int ay1 = (offy > 0) ? aoddy : aeveny;
int ay2 = (nBlkSizeY - nOverlapY) * 4 - ay1;
int a11 = ax1 * ay1, a12 = ax1 * ay2, a21 = ax2 * ay1, a22 = ax2 * ay2;
// iout_x = (a11 * v1.x + a21 * v2.x + a12 * v3.x + a22 * v4.x) / normov;
// iout_y = (a11 * v1.y + a21 * v2.y + a12 * v3.y + a22 * v4.y) / normov;
__m128i xmm_x = _mm_set_epi32(v4.x, v3.x, v2.x, v1.x);
__m128i xmm_y = _mm_set_epi32(v4.y, v3.y, v2.y, v1.y);
__m128i xmm_a = _mm_set_epi32(a22, a12, a21, a11);
xmm_x = _mm_mullo_epi32(xmm_x, xmm_a); // 32bit is enough ?
xmm_y = _mm_mullo_epi32(xmm_y, xmm_a); // 32bit is enough ?
xmm_x = _mm_hadd_epi32(xmm_x, xmm_x);
xmm_y = _mm_hadd_epi32(xmm_y, xmm_y);
xmm_x = _mm_add_epi32(xmm_x, _mm_srli_si128(xmm_x, 4));
xmm_y = _mm_add_epi32(xmm_y, _mm_srli_si128(xmm_y, 4));
iout_x = _mm_cvtss_si32(_mm_mul_ss(_mm_cvtepi32_ps(xmm_x), xmm_normov_rcp));
iout_y = _mm_cvtss_si32(_mm_mul_ss(_mm_cvtepi32_ps(xmm_y), xmm_normov_rcp));
// generic safe_sad_t is not always safe for the next calculations
// tmp_sad = (safe_sad_t)(((smallOverlapSafeSad_t)a11 * v1.sad + (smallOverlapSafeSad_t)a21 * v2.sad + (smallOverlapSafeSad_t)a12 * v3.sad + (smallOverlapSafeSad_t)a22 * v4.sad) / normov);
__m128i xmm_sad = _mm_set_epi32(v4.sad, v3.sad, v2.sad, v1.sad);
xmm_sad = _mm_mullo_epi32(xmm_sad, xmm_a); // 32bit is enough ?
xmm_sad = _mm_hadd_epi32(xmm_sad, xmm_sad);
xmm_sad = _mm_add_epi32(xmm_sad, _mm_srli_si128(xmm_sad, 4));
tmp_sad = _mm_cvtss_si32(_mm_mul_ss(_mm_cvtepi32_ps(xmm_sad), xmm_normov_rcp)); // for 32bit sad only ?
#if defined _DEBUG
if (tmp_sad < 0)
_RPT1(0, "Vector and SAD Interpolate Problem: possible SAD overflow %d\n", (sad_t)tmp_sad);
#endif
}
else // large overlap. Weights are not quite correct but let it be
{
iout_x = (v1.x + v2.x + v3.x + v4.x) << 2;
iout_y = (v1.y + v2.y + v3.y + v4.y) << 2;
tmp_sad = ((safe_sad_t)v1.sad + v2.sad + v3.sad + v4.sad + 2) << 2;
}
iout_x = (iout_x >> normFactor) << mulFactor;
iout_y = (iout_y >> normFactor) << mulFactor;
iout_sad = (sad_t)(tmp_sad >> 4);
// non-temporal store require better arrangement to 64bytes - to do.
vectors[index].x = iout_x;
vectors[index].y = iout_y;
vectors[index].sad = iout_sad;
#if 0
if (vectors[index].sad < 0)
_RPT1(0, "Vector and SAD Interpolate Problem: possible SAD overflow: %d\n", vectors[index].sad);
#endif
} // for k < nBlkX
} // for l < nBlkY
}
template void PlaneOfBlocks::InterpolatePrediction_sse<sad_t, sad_t>(PlaneOfBlocks& pob);
template void PlaneOfBlocks::InterpolatePrediction_sse<sad_t, bigsad_t>(PlaneOfBlocks& pob);
template void PlaneOfBlocks::InterpolatePrediction_sse<bigsad_t, bigsad_t>(PlaneOfBlocks& pob);
void PlaneOfBlocks::WriteHeaderToArray(int *array)
{
array[0] = nBlkCount * N_PER_BLOCK + 1;
}
int PlaneOfBlocks::WriteDefaultToArray(int *array, int divideMode)
{
array[0] = nBlkCount * N_PER_BLOCK + 1;
// int verybigSAD = nBlkSizeX*nBlkSizeY*256* bits_per_pixel_factor;
for (int i = 0; i < nBlkCount*N_PER_BLOCK; i += N_PER_BLOCK)
{
array[i + 1] = 0;
array[i + 2] = 0;
array[i + 3] = verybigSAD; // float or int!!
//*(sad_t *)(&array[i + 3]) = verybigSAD; // float or int!!
}
if (nLogScale == 0)
{
array += array[0];
if (divideMode)
{
// reserve space for divided subblocks extra level
array[0] = nBlkCount * N_PER_BLOCK * 4 + 1; // 4 subblocks
for (int i = 0; i < nBlkCount * 4 * N_PER_BLOCK; i += N_PER_BLOCK)
{
array[i + 1] = 0;
array[i + 2] = 0;
array[i + 3] = verybigSAD; // float or int!!
//*(sad_t *)(&array[i + 3]) = verybigSAD; // float or int
}
array += array[0];
}
}
return GetArraySize(divideMode);
}
int PlaneOfBlocks::GetArraySize(int divideMode)
{
int size = 0;
size += 1; // mb data size storage
size += nBlkCount * N_PER_BLOCK; // vectors, sad, luma src, luma ref, var
if (nLogScale == 0)
{
if (divideMode)
{
size += 1 + nBlkCount * N_PER_BLOCK * 4; // reserve space for divided subblocks extra level
}
}
return size;
}
template<typename pixel_t>
void PlaneOfBlocks::FetchPredictors(WorkingArea &workarea)
{
// Left (or right) predictor
if ((workarea.blkScanDir == 1 && workarea.blkx > 0) || (workarea.blkScanDir == -1 && workarea.blkx < nBlkX - 1))
{
workarea.predictors[1] = ClipMV(workarea, vectors[workarea.blkIdx - workarea.blkScanDir]);
}
else
{
workarea.predictors[1] = ClipMV(workarea, zeroMVfieldShifted); // v1.11.1 - values instead of pointer
}
// fixme note:
// MAnalyze mt-inconsistency reason #1
// this is _not_ internal mt friendly, since here up or bottom predictors
// are omitted for top/bottom data. Not non-mt case this happens only for
// the most top and bottom blocks.
// In vertically sliced multithreaded case it happens an _each_ top/bottom of the sliced block
const bool isTop = workarea.blky == workarea.blky_beg;
const bool isBottom = workarea.blky == workarea.blky_end - 1;
// Up predictor
if (!isTop)
{
workarea.predictors[2] = ClipMV(workarea, vectors[workarea.blkIdx - nBlkX]);
}
else
{
workarea.predictors[2] = ClipMV(workarea, zeroMVfieldShifted);
}
// Original problem: random, small, rare, mostly irreproducible differences between multiple encodings.
// In all, I spent at least a week on the problem during a half year, losing hope
// and restarting again four times. Nasty bug it was.
// !smallestPlane: use bottom right only if a coarser level exists or else we get random
// crap from a previous frame.
// bottom-right predictor (from coarse level)
if (!isBottom &&
!smallestPlane && // v2.7.44
((workarea.blkScanDir == 1 && workarea.blkx < nBlkX - 1) || (workarea.blkScanDir == -1 && workarea.blkx > 0)))
{
workarea.predictors[3] = ClipMV(workarea, vectors[workarea.blkIdx + nBlkX + workarea.blkScanDir]);
}
// Up-right predictor
else if (!isTop && ((workarea.blkScanDir == 1 && workarea.blkx < nBlkX - 1) || (workarea.blkScanDir == -1 && workarea.blkx > 0)))
{
workarea.predictors[3] = ClipMV(workarea, vectors[workarea.blkIdx - nBlkX + workarea.blkScanDir]);
}
else
{
workarea.predictors[3] = ClipMV(workarea, zeroMVfieldShifted);
}
// Median predictor
if (!isTop) // replaced 1 by 0 - Fizick
{
workarea.predictors[0].x = Median(workarea.predictors[1].x, workarea.predictors[2].x, workarea.predictors[3].x);
workarea.predictors[0].y = Median(workarea.predictors[1].y, workarea.predictors[2].y, workarea.predictors[3].y);
// workarea.predictors[0].sad = Median(workarea.predictors[1].sad, workarea.predictors[2].sad, workarea.predictors[3].sad);
// but it is not true median vector (x and y may be mixed) and not its sad ?!
// we really do not know SAD, here is more safe estimation especially for phaseshift method - v1.6.0
workarea.predictors[0].sad = std::max(workarea.predictors[1].sad, std::max(workarea.predictors[2].sad, workarea.predictors[3].sad));
}
else
{
// workarea.predictors[0].x = (workarea.predictors[1].x + workarea.predictors[2].x + workarea.predictors[3].x);
// workarea.predictors[0].y = (workarea.predictors[1].y + workarea.predictors[2].y + workarea.predictors[3].y);
// workarea.predictors[0].sad = (workarea.predictors[1].sad + workarea.predictors[2].sad + workarea.predictors[3].sad);