-
Notifications
You must be signed in to change notification settings - Fork 2
/
fruc_main.cpp
1829 lines (1586 loc) · 61.9 KB
/
fruc_main.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
/****************************************************************************/
/* Motion-Compensated Frame-Rate Up-Conversion for Hardware */
/****************************************************************************/
/* */
/* This software which contains modifications over original MC-FRUC */
/* software to comply with the hardware design of ATME algorithm */
/* additions include: */
/* - 32 update vectors instead of 25 */
/* - inversion of MVs for ease at hardware */
/* - MV ordering before calculation to comply with HW */
/* - 15 bit LFSR for pseudo-random number generation */
/* */
/*Features: */
/*Motion Estimation Methods: */
/* 3DRS */
/* An Adaptive True Motion Estimation (ATME) */
/* Bi-lateral Search (with initial vector selection) */
/* Full-Search */
/*Up-Conversion Methods: */
/* -Motion Compensated Field Averaging */
/* -Motion Compensated Field Averaging for Bilateral Search */
/* -Dynamic Median Filtering */
/* -Static Median Filtering */
/* -Two Mode Interpolation with Occlusion Detection using the Vector Field */
/* -OBMC */
/* -Motion Compensation for Video Coding */
/* */
/*Option to up-convert input video by a conversion factor of 2 */
/*or re-generate and overwrite the even numbered frames for testing */
/* */
/* Coder: Mert CETIN (mertc at sabanciuniv dot edu) */
/* */
/* Sabanci University */
/* Faculty of Engineering and Natural Sciences */
/* System on Chip Design and Testing Group */
/* http://fens.sabanciuniv.edu/soclab/ */
/* */
/* All Rights Reserved */
/* Please Do Not Use Without This Header */
/* */
/****************************************************************************/
#include "config.h"
#include <iostream>
#include <string>
#include "randgen.h"
#include <math.h>
#include <algorithm>
#include "fruc_def.h"
//#include "rand31-park-miller-carta-int.c"
#include "lfsr15.h"
using namespace std;
/*--------------------------------------------------------------------------*/
/* program entry point */
/* opens input and output files sets relevant file pointers */
/* reads input video file data into 2D arrays inside a loop and */
/* calls relevant motion estimation and up-conversion functions accordingly */
/* writes image data into output files */
/* runs comparison procedure and outputs the results */
/*--------------------------------------------------------------------------*/
int main()
{
Config::reload("config.txt");
FILE *infile;
FILE *MVs_File_out;
FILE *SAD_File_out;
FILE *bi_MVs_File_out;
FILE *outvid;
FILE *MV_X;
FILE *MV_Y;
FILE *resized_test_out;
mvectors = (vector*) malloc((Config::image_height/Config::blocksize)*(Config::image_width/Config::blocksize)*sizeof vector);
interMVs = (vector*) malloc((Config::image_height/Config::blocksize)*(Config::image_width/Config::blocksize)*sizeof vector);
SAD_Values = new unsigned int[(Config::image_height/Config::blocksize)*(Config::image_width/Config::blocksize)] ;
if((infile = fopen(Config::input_video.c_str(), "rb")) == NULL) {
printf("Error : Input file could not be opened\n");
return 1;
}
if((MVs_File_out = fopen(Config::MVs_filename.c_str(), "w")) == NULL) {
printf("Error : Output file could not be opened\n");
return 1;
}
if((SAD_File_out = fopen("SADs.txt", "w")) == NULL) {
printf("Error : Output file could not be opened\n");
return 1;
}
if((MV_X = fopen(Config::MV_X_file.c_str(), "w")) == NULL) {
printf("Error : Output file could not be opened\n");
return 1;
}
if((MV_Y = fopen(Config::MV_Y_file.c_str(), "w")) == NULL) {
printf("Error : Output file could not be opened\n");
return 1;
}
if((bi_MVs_File_out = fopen(Config::Bi_MVs_filename.c_str(), "w")) == NULL) {
printf("Error : Output file could not be opened\n");
return 1;
}
if((outvid = fopen(Config::output_video.c_str(), "wb")) == NULL) {
printf("Error : Output file could not be opened\n");
return 1;
}
if((resized_test_out = fopen("resizedframe.yuv", "wb")) == NULL) {
printf("Error : Output file could not be opened\n");
return 1;
}
int i,j;
unsigned char* data1 = new unsigned char[Config::image_height*Config::image_width];
unsigned char* data2 = new unsigned char[Config::image_height*Config::image_width];
unsigned char *ResizedFrame1, *ResizedFrame2;
//rand31pmc_seedi(1); // seed LFSR with 1
int s_count = Config::candidate_count;
int ext_s_count = Config::ext_candidate_count;
int up_conv_algo = Config::up_conversion_algo;
vector* s_locs;
s_locs = new vector[s_count];
for(i=0;i<s_count;i++){
s_locs[i].x = Config::search_location_list[2*i];
s_locs[i].y = Config::search_location_list[2*i+1];
}
vector* ext_s_locs;
ext_s_locs = new vector[ext_s_count];
for(i=0;i<ext_s_count;i++){
ext_s_locs[i].x = Config::ext_search_location_list[2*i];
ext_s_locs[i].y = Config::ext_search_location_list[2*i+1];
}
firstframe = true;
secondframe = true;
// write first frame to output directly
for (i=0;i<Config::image_height;i++){
fread(&data1[INDEX(i,0)], sizeof(unsigned char), Config::image_width, infile);
fwrite(&data1[INDEX(i,0)],sizeof(unsigned char), Config::image_width, outvid);
}
ResizedFrame1 = ResizeFrame(data1);
/* DEBUG - write first resized frame to file */
/*for (i=0;i<(Config::image_height+2*Config::fs_win);i++){
fwrite(&ResizedFrame1[i*(Config::image_width+2*Config::fs_win)],sizeof(unsigned char), (Config::image_width+2*Config::fs_win), resized_test_out);
}*/
/* */
int loopcount;
if(Config::enable_rep)
loopcount = Config::framecount/4;
else
loopcount = Config::framecount/2;
if (Config::enable_rep) // if replace even numbered frames with up-converted ones
{
fseek(infile,Config::total_y,SEEK_CUR); // skip the second frame
}
printf("Interpolating: ");
for (j=0;j<loopcount;j++)
{
printf("\b\b\b%03d",j*4); // display current frame number
// read the next frame
for (int i=0;i<Config::image_height;i++){
fread(&data2[INDEX(i,0)], sizeof(unsigned char), Config::image_width, infile);
}
// resize
ResizedFrame2 = ResizeFrame(data2);
/* DEBUG - write first resized frame to file */
//for (i=0;i<(Config::image_height+2*Config::fs_win);i++){
// fwrite(&ResizedFrame2[i*(Config::image_width+2*Config::fs_win)],sizeof(unsigned char), (Config::image_width+2*Config::fs_win), resized_test_out);
//}
/* */
// process motion estimation stage
if (Config::bi_fs)
{
if (Config::first_fs && firstframe)
frameFullSearch(ResizedFrame2,ResizedFrame1,MVs_File_out,MV_X,MV_Y);
else
{
calcFrame3DRS(ResizedFrame2,ResizedFrame1,MVs_File_out,MV_X,MV_Y,s_count,ext_s_count,s_locs,ext_s_locs,SAD_File_out);
}
frameBiFullSearch(ResizedFrame2,ResizedFrame1,bi_MVs_File_out);
}
else if (Config::all_fs)
{
frameFullSearch(ResizedFrame2,ResizedFrame1,MVs_File_out,MV_X,MV_Y);
}
else if(Config::first_fs && firstframe)
frameFullSearch(ResizedFrame2,ResizedFrame1,MVs_File_out,MV_X,MV_Y);
else
{
firstpass = true;
for (i=0;i<Config::passcount;i++){
calcFrame3DRS(ResizedFrame2,ResizedFrame1,MVs_File_out,MV_X,MV_Y,s_count,ext_s_count,s_locs,ext_s_locs,SAD_File_out);
firstpass = false;
}
}
// process frame interpolation
if(up_conv_algo == 1)
{
mc_field_average(ResizedFrame2,ResizedFrame1,outvid);
}
else if(up_conv_algo == 2)
{
DynMedian(ResizedFrame2,ResizedFrame1,outvid);
}
else if (up_conv_algo == 3)
{
two_mode_interpolate(ResizedFrame2,ResizedFrame1,outvid);
}
else if (up_conv_algo == 4)
{
bi_mc_field_average(ResizedFrame2,ResizedFrame1,outvid);
}
else if (up_conv_algo == 5)
{
obmc(ResizedFrame2,ResizedFrame1,outvid);
}
else if (up_conv_algo == 6)
{
motion_compensate(ResizedFrame2,ResizedFrame1,outvid);
}
else if(up_conv_algo == 7)
{
StaMedian(ResizedFrame2,ResizedFrame1,outvid);
}
// write next frame to output directly (odd numbered frame)
if(up_conv_algo != 6)
{
for (i=0;i<Config::image_height;i++){
fwrite(&data2[INDEX(i,0)],sizeof(unsigned char), Config::image_width, outvid);
}
}
// if last frame
if (j == loopcount-1){
if(up_conv_algo != 6)
{
for (i=0;i<Config::image_height;i++){
fread(&data1[INDEX(i,0)], sizeof(unsigned char), Config::image_width, infile);
fwrite(&data1[INDEX(i,0)],sizeof(unsigned char), Config::image_width, outvid);
}
}
break;
}
// if not, exchange the image data pointers and continue the process
else {
if (Config::enable_rep) // if replace even numbered frames with up-converted ones
{
fseek(infile,Config::total_y,SEEK_CUR); // skip the second frame
}
for (i=0;i<Config::image_height;i++){
fread(&data1[INDEX(i,0)], sizeof(unsigned char), Config::image_width, infile);
}
ResizedFrame1 = ResizeFrame(data1);
if (Config::all_fs)
{
frameFullSearch(ResizedFrame1,ResizedFrame2,MVs_File_out,MV_X,MV_Y);
}
else if (Config::bi_fs)
{
calcFrame3DRS(ResizedFrame1,ResizedFrame2,MVs_File_out,MV_X,MV_Y,s_count,ext_s_count,s_locs,ext_s_locs,SAD_File_out);
frameBiFullSearch(ResizedFrame1,ResizedFrame2,bi_MVs_File_out);
}
else
{
firstpass = true;
for (i=0;i<Config::passcount;i++){
calcFrame3DRS(ResizedFrame1,ResizedFrame2,MVs_File_out,MV_X,MV_Y,s_count,ext_s_count,s_locs,ext_s_locs,SAD_File_out);
firstpass = false;
}
}
if(up_conv_algo == 1)
{
mc_field_average(ResizedFrame1,ResizedFrame2,outvid);
}
else if(up_conv_algo == 2)
{
DynMedian(ResizedFrame1,ResizedFrame2,outvid);
}
else if (up_conv_algo == 3)
{
two_mode_interpolate(ResizedFrame1,ResizedFrame2,outvid);
}
else if (up_conv_algo == 4)
{
bi_mc_field_average(ResizedFrame1,ResizedFrame2,outvid);
}
else if (up_conv_algo == 5)
{
obmc(ResizedFrame1,ResizedFrame2,outvid);
}
else if (up_conv_algo == 6)
{
motion_compensate(ResizedFrame1,ResizedFrame2,outvid);
}
else if(up_conv_algo == 7)
{
StaMedian(ResizedFrame1,ResizedFrame2,outvid);
}
if(up_conv_algo != 6)
{
for (i=0;i<Config::image_height;i++){
fwrite(&data1[INDEX(i,0)],sizeof(unsigned char), Config::image_width, outvid);
}
}
if (Config::enable_rep) // if replace even numbered frames with up-converted ones
{
fseek(infile,Config::total_y,SEEK_CUR); // skip the second frame
}
}
}
// close file pointers
fclose(infile);
fclose(MVs_File_out);
fclose(SAD_File_out);
fclose(MV_X);
fclose(MV_Y);
fclose(bi_MVs_File_out);
fclose(outvid);
fclose(resized_test_out);
/************************************************************************/
/* Comparison Test and PSNR Output */
/************************************************************************/
FILE *results;
//FILE *resultsSAD;
FILE *infile1;
FILE *infile2;
if((results = fopen(Config::results_file.c_str(), "a")) == NULL) {
printf("Error : Output file could not be opened\n");
return 1;
}
//if((resultsSAD = fopen("SAD.txt", "w")) == NULL) {
// printf("Error : Output file could not be opened\n");
// return 1;
//}
if((infile1 = fopen(Config::input_video.c_str(), "rb")) == NULL) {
printf("Error : Input file 1 could not be opened\n");
return 1;
}
if((infile2 = fopen(Config::output_video.c_str(), "rb")) == NULL) {
printf("Error : Input file 2 could not be opened\n");
return 1;
}
int f,framesgenerated;
double SqError,MSE,TotMSE;
TotMSE = 0.0;
framesgenerated = 0;
printf("\nComparing: ");
for (f=0;f<Config::framecount;f++){
SqError = 0.0;
MSE = 0.0;
printf("\b\b\b%03d",f);
for (i=0;i<Config::image_height;i++){
fread(&data1[INDEX(i,0)], sizeof(unsigned char), Config::image_width, infile1);
fread(&data2[INDEX(i,0)], sizeof(unsigned char), Config::image_width, infile2);
}
/* for (i=0;i<Config::image_height;i++){
if(Config::up_conversion_algo==6 && Config::enable_rep == 1)
{
if(f == 0 || f == Config::framecount/2)
{
fread(&data1[INDEX(i,0)], sizeof(unsigned char), Config::image_width, infile1);
}
else
{
fseek(infile1,Config::total_y,SEEK_CUR);
fread(&data1[INDEX(i,0)], sizeof(unsigned char), Config::image_width, infile1);
}
}
else
fread(&data1[INDEX(i,0)], sizeof(unsigned char), Config::image_width, infile1);
fread(&data2[INDEX(i,0)], sizeof(unsigned char), Config::image_width, infile2);
}*/
for (i=0;i<Config::image_height;i++){
for (j=0;j<Config::image_width;j++){
SqError = SqError + pow(double(data1[INDEX(i,j)] - data2[INDEX(i,j)]),2);
}
}
MSE = SqError / (Config::image_height*Config::image_width);
if (MSE != 0) framesgenerated++;
TotMSE += MSE;
/*if( Config::up_conversion_algo==6 && Config::enable_rep == 1 && f == (Config::framecount / 2))
break;*/
}
//printf("\n");
if(TotMSE == 0)
fprintf (results,"PSNR: %s\t\tSAD Count: %d\n",Config::enable_rep?"infinity":"N/A",SADcount);
else
{
if(Config::enable_rep || up_conv_algo == 6)
{
fprintf(results,"PSNR: %3.4f\t\tSAD Count: %d\n",10.0*log10((255.0*255.0)/(TotMSE/framesgenerated)),SADcount);
/*fprintf(results,"%3.4f",10.0*log10((255.0*255.0)/(TotMSE/framesgenerated)));
fprintf(resultsSAD,"%d",SADcount);*/
// fprintf(results,"Total PSNR: %3.4f\t\t Generated Frames Only PSNR: %3.4f\t\t%d\n",10.0*log10((255.0*255.0)/(TotMSE/f)),10.0*log10((255.0*255.0)/(TotMSE/framesgenerated)),SADcount);
}
else
fprintf(results,"PSNR: N/A\t\tSAD Count: %d\t cycle: %d\n",SADcount,cyclecount);
}
delete [] data1 ;
delete [] data2 ;
delete [] ResizedFrame1;
delete [] ResizedFrame2;
free(interMVs);
free(mvectors);
fclose(infile1);
fclose(infile2);
fclose(results);
//fclose(resultsSAD);
return(0);
}
/*------------------------------------------------------------------------------*/
/*Dynamic Median Filter caller function */
/*Calls Dynamic Median Filter calculator function for every pixel in the frame */
/*and writes generated image data into output video file */
/*------------------------------------------------------------------------------*/
void DynMedian(unsigned char* curData, unsigned char* refData, FILE* outvid){
unsigned char* tempframe = new unsigned char[Config::image_width*Config::image_height];
int i,j;
for (i=0;i<Config::image_height;i++){
for (j=0;j<Config::image_width;j++){
if (i<1072)
{
tempframe[INDEX(i,j)] = DynMedian_Pixel(curData,refData,i,j);
}
else
{
tempframe[INDEX(i,j)] = (refData[R_INDEX(i,j)]+curData[R_INDEX(i,j)])/2;
}
}
}
for (i=0;i<Config::image_height;i++){
fwrite(&tempframe[INDEX(i,0)],sizeof(unsigned char), Config::image_width,outvid);
}
delete [] tempframe;
}
/*------------------------------------------------------------------------------*/
/*Static Median Filter caller function */
/*Calls Static Median Filter calculator function for every pixel in the frame */
/*and writes generated image data into output video file */
/*------------------------------------------------------------------------------*/
void StaMedian(unsigned char* curData, unsigned char* refData, FILE* outvid){
unsigned char* tempframe = new unsigned char[Config::image_width*Config::image_height];
int i,j;
for (i=0;i<Config::image_height;i++){
for (j=0;j<Config::image_width;j++){
if (i<1072)
{
tempframe[INDEX(i,j)] = StaMedian_Pixel(curData,refData,i,j);
}
else
{
tempframe[INDEX(i,j)] = (refData[R_INDEX(i,j)]+curData[R_INDEX(i,j)])/2;
}
}
}
for (i=0;i<Config::image_height;i++){
fwrite(&tempframe[INDEX(i,0)],sizeof(unsigned char), Config::image_width,outvid);
}
delete [] tempframe;
}
/*------------------------------------------------------------------------------------------------------*/
/*Dynamic Median Filter calculator function */
/*calculate two motion compensated pixel data one from previous one from current frames */
/*and median them with non-motion compensated interpolated pixel data from previous and current frames */
/*------------------------------------------------------------------------------------------------------*/
unsigned char DynMedian_Pixel(unsigned char* curData, unsigned char* refData, int rowpxpos, int colpxpos){
unsigned char result[3];
vector avg_vector;
avg_vector=mvectors[((rowpxpos/Config::blocksize)*(BLOCK_COUNT_OF_ROW))+(colpxpos/Config::blocksize)];
result[0]= refData[R_INDEX((rowpxpos-(avg_vector.y/2)),(colpxpos-(avg_vector.x/2)))];
result[1]= curData[R_INDEX((rowpxpos+(avg_vector.y/2)),(colpxpos+(avg_vector.x/2)))];
result[2]= (refData[R_INDEX(rowpxpos,colpxpos)] + curData[R_INDEX(rowpxpos,colpxpos)])/2;
if (result[0] > result[1]){
if (result[0] > result[2]){
if (result[1] > result[2]) return result[1];
else return result[2];
}
else return result[0];
}
else{
if (result[0] < result[2]){
if (result[1] < result[2]) return result[1];
else return result[2];
}
else return result[0];
}
}
/*------------------------------------------------------------------------------------------------------*/
/*Static Median Filter calculator function */
/*calculate two non-motion compensated pixel data one from previous one from current frames */
/*and median them with motion compensated pixel data from previous and current frames */
/*------------------------------------------------------------------------------------------------------*/
unsigned char StaMedian_Pixel(unsigned char* curData, unsigned char* refData, int rowpxpos, int colpxpos){
unsigned char result[3];
vector avg_vector;
avg_vector=mvectors[((rowpxpos/Config::blocksize)*(BLOCK_COUNT_OF_ROW))+(colpxpos/Config::blocksize)];
result[0]= refData[R_INDEX((rowpxpos-(avg_vector.y/2)),(colpxpos-(avg_vector.x/2)))];
result[1]= curData[R_INDEX((rowpxpos+(avg_vector.y/2)),(colpxpos+(avg_vector.x/2)))];
result[2]= (refData[R_INDEX(rowpxpos,colpxpos)] + curData[R_INDEX(rowpxpos,colpxpos)])/2;
result[0]= refData[R_INDEX(rowpxpos,colpxpos)];
result[1]= curData[R_INDEX(rowpxpos,colpxpos)];
result[2]= ((refData[R_INDEX((rowpxpos-(avg_vector.y/2)),(colpxpos-(avg_vector.x/2)))])+curData[R_INDEX((rowpxpos+(avg_vector.y/2)),(colpxpos+(avg_vector.x/2)))])/2;
if (result[0] > result[1]){
if (result[0] > result[2]){
if (result[1] > result[2]) return result[1];
else return result[2];
}
else return result[0];
}
else{
if (result[0] < result[2]){
if (result[1] < result[2]) return result[1];
else return result[2];
}
else return result[0];
}
}
/*------------------------------------------------------------------------------------------------------*/
/*Motion Compensated Field Averaging calculator function */
/*calculate two motion compensated pixel data one from previous one from current frames */
/*and interpolate them by 50% */
/*------------------------------------------------------------------------------------------------------*/
unsigned char mc_field_average_pixel(unsigned char* curData, unsigned char* refData, int rowpxpos, int colpxpos){
unsigned char field_average;
vector avg_vector;
avg_vector=mvectors[((rowpxpos/Config::blocksize)*(BLOCK_COUNT_OF_ROW))+(colpxpos/Config::blocksize)];
if(rowpxpos<1072)
{
field_average = ((refData[R_INDEX((rowpxpos-(avg_vector.y/2)),(colpxpos-(avg_vector.x/2)))])+curData[R_INDEX((rowpxpos+(avg_vector.y/2)),(colpxpos+(avg_vector.x/2)))])/2;
}
else
field_average = (refData[R_INDEX(rowpxpos,colpxpos)]+curData[R_INDEX(rowpxpos,colpxpos)])/2;
return field_average;
}
/*------------------------------------------------------------------------------*/
/*Motion Compensation for Video Coding caller function */
/*Calls Motion Compensation calculator function for every pixel in the frame */
/*and writes generated image data into output video file */
/*------------------------------------------------------------------------------*/
void motion_compensate(unsigned char* curData, unsigned char* refData, FILE* outvid){
unsigned char* tempframe = new unsigned char[Config::image_width*Config::image_height];
int i,j;
for (i=0;i<Config::image_height;i++){
for (j=0;j<Config::image_width;j++){
if (i<1072)
{
tempframe[INDEX(i,j)] = motion_compensate_pixel(curData,refData,i,j);
}
else
{
tempframe[INDEX(i,j)] = refData[R_INDEX(i,j)];
}
}
}
for (i=0;i<Config::image_height;i++){
fwrite(&tempframe[INDEX(i,0)],sizeof(unsigned char), Config::image_width,outvid);
}
delete [] tempframe;
}
/*------------------------------------------------------------------------------------------------------*/
/*Motion Compensation for Video Coding calculator function */
/*use the MV data for the current frame to interpolate using only the previous frame's image data */
/*------------------------------------------------------------------------------------------------------*/
unsigned char motion_compensate_pixel(unsigned char* curData, unsigned char* refData, int rowpxpos, int colpxpos){
unsigned char compensated;
vector avg_vector;
avg_vector=mvectors[((rowpxpos/Config::blocksize)*(BLOCK_COUNT_OF_ROW))+(colpxpos/Config::blocksize)];
compensated = refData[R_INDEX((rowpxpos-(avg_vector.y)),(colpxpos-(avg_vector.x)))];
return compensated;
}
/*------------------------------------------------------------------------------------------------------*/
/*Motion Compensated Field Averaging for Bilateral ME calculator function */
/*calculate two motion compensated pixel data one from previous one from current frames */
/*and interpolate them by 50% */
/*this function should only be used when MVs returned from bilateral ME function are not multiplied by 2*/
/*------------------------------------------------------------------------------------------------------*/
unsigned char bi_mc_field_average_pixel(unsigned char* curData, unsigned char* refData, int rowpxpos, int colpxpos){
unsigned char field_average;
vector avg_vector;
avg_vector=interMVs[((rowpxpos/Config::blocksize)*(BLOCK_COUNT_OF_ROW))+(colpxpos/Config::blocksize)];
//avg_vector=mvectors[((rowpxpos/Config::blocksize)*(BLOCK_COUNT_OF_ROW))+(colpxpos/Config::blocksize)];
field_average = ((refData[R_INDEX((rowpxpos-(avg_vector.y)),(colpxpos-(avg_vector.x)))])+curData[R_INDEX((rowpxpos+(avg_vector.y)),(colpxpos+(avg_vector.x)))])/2;
return field_average;
}
/*------------------------------------------------------------------------------*/
/*Motion Compensated Field Averaging caller function */
/*Calls MC Field Averaging calculator function for every pixel in the frame */
/*and writes generated image data into output video file */
/*------------------------------------------------------------------------------*/
void mc_field_average(unsigned char* curData, unsigned char* refData, FILE* outvid){
unsigned char* tempframe = new unsigned char[Config::image_width*Config::image_height];
int i,j;
for (i=0;i<Config::image_height;i++){
for (j=0;j<Config::image_width;j++){
tempframe[INDEX(i,j)] = mc_field_average_pixel(curData,refData,i,j);
}
}
for (i=0;i<Config::image_height;i++){
fwrite(&tempframe[INDEX(i,0)],sizeof(unsigned char), Config::image_width,outvid);
}
delete [] tempframe;
}
/***************************************************************/
/* MC Field Averaging Function for Bilateral Motion Estimation */
/* !!USE ONLY IF MOTION VECTORS ARE NOT MULTIPLIED BY 2 */
/* INSIDE THE BILATERAL SEARCH FUNCTIONS!!!!!!!!!!!!!*/
/***************************************************************/
void bi_mc_field_average(unsigned char* curData, unsigned char* refData, FILE* outvid){
unsigned char* tempframe = new unsigned char[Config::image_width*Config::image_height];
int i,j;
for (i=0;i<Config::image_height;i++){
for (j=0;j<Config::image_width;j++){
if (i<1072)
{
tempframe[INDEX(i,j)] = bi_mc_field_average_pixel(curData,refData,i,j);
}
else
{
tempframe[INDEX(i,j)] = (refData[R_INDEX(i,j)]+curData[R_INDEX(i,j)])/2;
}
}
}
for (i=0;i<Config::image_height;i++){
fwrite(&tempframe[INDEX(i,0)],sizeof(unsigned char), Config::image_width,outvid);
}
delete [] tempframe;
}
/*------------------------------------------------------------------------------*/
/*Two Mode Interpolator decision function */
/*For every pixel in the image frame run occlusion detection algorithm */
/*if it's decided that there is occlusion situation use dynamic median filtering*/
/*if there's no occlusion then use MC Field averaging */
/*------------------------------------------------------------------------------*/
void two_mode_interpolate(unsigned char* curData, unsigned char* refData, FILE* outvid){
//unsigned char tempframe[Config::image_height][Config::image_width];
unsigned char* tempframe = new unsigned char[Config::image_width*Config::image_height];
int i,j;
for (i=0;i<Config::image_height;i++){
for (j=0;j<Config::image_width;j++){
if (i<1072)
{
if (detect_occlusion((i/Config::blocksize),(j/Config::blocksize)))
tempframe[INDEX(i,j)] = DynMedian_Pixel(curData,refData,i,j);
else
tempframe[INDEX(i,j)] = mc_field_average_pixel(curData,refData,i,j);
}
else
{
tempframe[INDEX(i,j)] = (refData[R_INDEX(i,j)]+curData[R_INDEX(i,j)])/2;
}
}
}
for (i=0;i<Config::image_height;i++){
fwrite(&tempframe[INDEX(i,0)],sizeof(unsigned char), Config::image_width,outvid);
}
delete [] tempframe;
}
/*--------------------------------------------------------------------------------------*/
/*Occlusion detection function */
/*If the previous block's motion vector is larger than the next block's motion vector */
/*more than a pre-defined threshold then it is decided that there is occlusion */
/*--------------------------------------------------------------------------------------*/
bool detect_occlusion(int rowpos, int colpos){
if (rowpos == 0) rowpos += 1;
else if (rowpos == BLOCK_COUNT_OF_COLUMN) rowpos -= 1;
else if (colpos == 0) colpos += 1;
else if (colpos == BLOCK_COUNT_OF_ROW) colpos -= 1;
if(abs(mvectors[rowpos*(BLOCK_COUNT_OF_ROW)+colpos+1].x - mvectors[rowpos*(BLOCK_COUNT_OF_ROW)+colpos-1].x) > Config::occlusion_th ||
abs(mvectors[(rowpos+1)*(BLOCK_COUNT_OF_ROW)+colpos].y - mvectors[(rowpos-1)*(BLOCK_COUNT_OF_ROW)+colpos].y) > Config::occlusion_th)
return true;
else
return false;
}
/*------------------------------------------------------*/
/* Overloaded Updater Function version 1 */
/* if input is a vector pointer than update data where */
/* it points with a random vector */
/*------------------------------------------------------*/
vector* update(vector* mvector){
if(Config::enable_update){
vector randvector;
long unsigned int randomint;
//randomint = rand31pmc_ranlui()%25;
randomint = myLFSR.LFSR15Bit()%32;
//RandGen rand;
//randvector = updateSet[rand.RandInt(0,24)];
randvector = updateSet[randomint];
mvector->x += randvector.x;
mvector->y += randvector.y;
}
return mvector;
}
/*------------------------------------------------------*/
/* Overloaded Updater Function version 2 */
/* if input is a vector than update that vector's data */
/* with a random vector */
/*------------------------------------------------------*/
vector update(vector mvector){
if(Config::enable_update){
vector randvector;
//RandGen rand;
long unsigned int randomint;
//randomint = rand31pmc_ranlui()%25;
randomint = myLFSR.LFSR15Bit()%32;
//randvector = updateSet[rand.RandInt(0,24)];
randvector = updateSet[randomint];
mvector.x += randvector.x;
mvector.y += randvector.y;
}
return mvector;
}
/*----------------------------------------------------------------------------------------------*/
/*3DRS block matcher function (Two updated candidates) */
/*----------------------------------------------------------------------------------------------*/
vector calcBlock3DRS(unsigned char* curData, unsigned char* refData, int rowpos, int colpos,int s_count, vector* s_locs){
vector mvector = {0,0};
vector* tempvector = new vector[s_count+1];
unsigned int* SAD;
SAD = new unsigned int[s_count];
int i;
int row,col;
unsigned int min = Config::blocksize*Config::blocksize*255;
if (Config::enable_early_t && calculateSAD(&curData[R_INDEX(rowpos*Config::blocksize,colpos*Config::blocksize)],&refData[R_INDEX(rowpos*Config::blocksize,colpos*Config::blocksize)]) == 0){
mvector.x = 0;
mvector.y = 0;
}
else if (firstframe){
update(&mvector);
}
else{
for(i=0;i<s_count;i++){ //use for(i=0;i<=s_count;i++) to include zero motion vector
row = rowpos + s_locs[i].y;
col = colpos + s_locs[i].x;
if (row < 0) row = 0;
else if (row > (BLOCK_COUNT_OF_COLUMN - 1)) row = BLOCK_COUNT_OF_COLUMN -1;
if (col < 0) col = 0;
else if (col > (BLOCK_COUNT_OF_ROW - 1)) col = BLOCK_COUNT_OF_ROW -1;
if (i == s_count-1)
{
tempvector[i] = update(mvectors[row*(BLOCK_COUNT_OF_ROW)+col]);
}
else if (i == s_count-2)
{
//tempvector[i].x = mvectors[row*(BLOCK_COUNT_OF_ROW)+col].x;
//tempvector[i].y = mvectors[row*(BLOCK_COUNT_OF_ROW)+col].y;
tempvector[i] = update(mvectors[row*(BLOCK_COUNT_OF_ROW)+col]); //comment out this line and uncomment the above 2 lines for single updated candidate
}
else if (i == s_count)
{
tempvector[i].x = 0;
tempvector[i].y = 0;
}
else
{
tempvector[i].x = mvectors[row*(BLOCK_COUNT_OF_ROW)+col].x;
tempvector[i].y = mvectors[row*(BLOCK_COUNT_OF_ROW)+col].y;
}
/*if (!firstpass && !secondframe && (tempvector[i].x == mvectors[rowpos*(BLOCK_COUNT_OF_ROW)+colpos].x && tempvector[i].y == mvectors[rowpos*(BLOCK_COUNT_OF_ROW)+colpos].y))
{
SAD[i] = SAD_Values[rowpos*(BLOCK_COUNT_OF_ROW)+colpos];
}
else
{*/
SAD[i] = calculateSAD(&curData[R_INDEX(rowpos*Config::blocksize,colpos*Config::blocksize)],&refData[R_INDEX((rowpos*Config::blocksize-tempvector[i].y),colpos*Config::blocksize-tempvector[i].x)]);
//}
}
for(i=0;i<s_count;i++){ //use for(i=0;i<=s_count;i++) to include zero motion vector
if (SAD[i] < min){
mvector = tempvector[i];
min = SAD[i];
}
}
}
min_SAD = min;
delete [] SAD;
delete [] tempvector;
return mvector;
}
/****************************************************************************************************************/
/*ATME Algorithm calculator function */
/* IMPORTANT!! READ BEAFORE USE !! */
/* In this function the resulting MVs are pointing from the current block to the previous block */
/* In the original software MVs point from previous block to current block */
/* This modification reduces the complexity of the hardware design of the algorithm */
/* To match the results with HW simulation use the functions as given */
/* However to match the original software results change all of the lines containing: */
/* refData[R_INDEX((rowpos*Config::blocksize+candidates[i].y),colpos*Config::blocksize+candidates[i].x)]); */
/* to: */
/* refData[R_INDEX((rowpos*Config::blocksize-candidates[i].y),colpos*Config::blocksize-candidates[i].x)]); */
/* and the lines containing: */
/* refData[R_INDEX((rowpos*Config::blocksize+tempvector[i].y),colpos*Config::blocksize+tempvector[i].x)]); */
/* to: */
/* refData[R_INDEX((rowpos*Config::blocksize-tempvector[i].y),colpos*Config::blocksize-tempvector[i].x)]); */
/****************************************************************************************************************/
vector calcNewBlock3DRS(unsigned char* curData, unsigned char* refData, int rowpos, int colpos,int s_count,int ext_s_count, vector* s_locs, vector* ext_s_locs){
vector mvector = {0,0};
vector* tempvector = new vector[ext_s_count+1];
vector updatevector;
vector UWpos;
unsigned int* SAD;
SAD = new unsigned int[ext_s_count+1];
int i;
int row,col;
unsigned int min = Config::blocksize*Config::blocksize*255;
bool under_threshold = false;
float* weight = new float[ext_s_count+1];
vector* candidates = new vector[2];
vector vmedian;
vector* sortarray = new vector[3];
vector sorttemp,tempv;
int whichvector;
if (Config::enable_early_t && calculateSAD(&curData[R_INDEX(rowpos*Config::blocksize,colpos*Config::blocksize)],&refData[R_INDEX(rowpos*Config::blocksize,colpos*Config::blocksize)]) == 0){
mvector.x = 0;
mvector.y = 0;
}
else if (firstframe){
update(&mvector);
}
else{
for(i=0;i<s_count;i++){
row = rowpos + s_locs[i].y;
col = colpos + s_locs[i].x;
if (row < 0) row = 0;
else if (row > (BLOCK_COUNT_OF_COLUMN - 1)) row = BLOCK_COUNT_OF_COLUMN -1;
if (col < 0) col = 0;
else if (col > (BLOCK_COUNT_OF_ROW - 1)) col = BLOCK_COUNT_OF_ROW -1;
// uncomment below lines for giving weights to candidates
/*if (i == s_count-1)
{
tempvector[i] = update(mvectors[row*(BLOCK_COUNT_OF_ROW)+col]);
weight[i] = Config::update_weight;
}
else if (i == s_count-2)
{
weight[i] = Config::temporal_weight;
tempvector[i].x = mvectors[row*(BLOCK_COUNT_OF_ROW)+col].x;
tempvector[i].y = mvectors[row*(BLOCK_COUNT_OF_ROW)+col].y;
}
else
{
weight[i] = Config::spatial_weight;
tempvector[i].x = mvectors[row*(BLOCK_COUNT_OF_ROW)+col].x;
tempvector[i].y = mvectors[row*(BLOCK_COUNT_OF_ROW)+col].y;
}*/
if (i == s_count-1)
{
tempvector[i] = mvectors[row*(BLOCK_COUNT_OF_ROW)+col];
//tempvector[i] = update(mvectors[row*(BLOCK_COUNT_OF_ROW)+col]); //uncomment this line for secondary update vector added candidate
//updatevector = update(mvectors[row*(BLOCK_COUNT_OF_ROW)+col]);
}
else
{
tempvector[i].x = mvectors[row*(BLOCK_COUNT_OF_ROW)+col].x;
tempvector[i].y = mvectors[row*(BLOCK_COUNT_OF_ROW)+col].y;
}
}