-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.m
779 lines (657 loc) · 30 KB
/
main.m
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
close all;clear all;clc;
moviepath=pwd;
allready = menu('Before you continue, you must first follow the instructions in the README file','I have done that!','Exit');
if allready == 2 || allready == 0
exit
end
AVSFileName=0;AVSPathName=0;
[AVSFileName,AVSPathName] = uigetfile('*.avs','Select the source AviSynth script');
if isequal(AVSFileName,0) == 1 || isequal(AVSPathName,0) == 1
exit
end
%WWWWWWWWWWWWWWWWWWW DELETE PNGS WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW
Source_pngs=(strrep('-\pngs\source\*.png', '-', moviepath));
Source_png_Folder=(strrep('-\pngs\source\', '-', moviepath));
Encode_pngs=(strrep('-\pngs\encode\*.png', '-', moviepath));
Encode_png_Folder=(strrep('-\pngs\encode\', '-', moviepath));
Source_dir=dir(Source_pngs);
Encode_dir=dir(Encode_pngs);
if isempty(Source_dir)==0 || isempty(Encode_dir)==0
DeleteChoise = menu('The previous generated png files should be removed. Do you want to delete them?','Yes (strongly recommended)','No');
if DeleteChoise == 1
NOS=length(Source_dir);
NOE=length(Encode_dir);
for i=1:NOE
Encode_PNG=[Encode_png_Folder Encode_dir(i).name];
delete(Encode_PNG);
end
for i=1:NOS
Source_PNG=[Source_png_Folder Source_dir(i).name];
delete(Source_PNG);
end
end
end
%WWWWWWWWWWWWWWWWWWW SELECT RANGES WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW
setranges = menu('Do you want to test the default value ranges?','Yes (recommended)','No, I will enter them myself.');
if setranges==2
prompt = {'Enter qcomp minimum value:','Enter qcomp maximum value:','Step:'};
dlg_title = 'qcomp settings';
num_lines = 1;
defaultans = {'0.5','0.8','0.05'};
QCOMPSettings = inputdlg(prompt,dlg_title,num_lines,defaultans);
QCOMPmin=str2num(cell2mat(QCOMPSettings(1)))
QCOMPmax=str2num(cell2mat(QCOMPSettings(2)))
QCOMPstep=str2num(cell2mat(QCOMPSettings(3)))
prompt = {'Enter aqmode minimum value:','Enter aqmode maximum value:'};
dlg_title = 'aqmode settings';
num_lines = 1;
defaultans = {'1','3'};
AQMODEsettings = inputdlg(prompt,dlg_title,num_lines,defaultans);
AQModemin=str2num(cell2mat(AQMODEsettings(1)))
AQModemax=str2num(cell2mat(AQMODEsettings(2)))
prompt = {'Enter aq-strength minimum value:','Enter aq-strength maximum value:','Step:'};
dlg_title = 'aq-strength settings';
num_lines = 1;
defaultans = {'0.5','1.1','0.05'};
AQSTRENGTHSettings = inputdlg(prompt,dlg_title,num_lines,defaultans);
AQSTRENGTHmin=str2num(cell2mat(AQSTRENGTHSettings(1)))
AQSTRENGTHmax=str2num(cell2mat(AQSTRENGTHSettings(2)))
AQSTRENGTHstep=str2num(cell2mat(AQSTRENGTHSettings(3)))
prompt = {'Enter psyrd minimum value:',':','Enter psyrd maximum value:',':','Step:'};
dlg_title = 'aq-strength settings';
num_lines = 1;
defaultans = {'0.6','0','1.2','0.1','0.05'};
PSYRDSettings = inputdlg(prompt,dlg_title,num_lines,defaultans);
PSYmin=str2num(cell2mat(PSYRDSettings(1)))
PSYmax=str2num(cell2mat(PSYRDSettings(3)))
RDmin=str2num(cell2mat(PSYRDSettings(2)))
RDmax=str2num(cell2mat(PSYRDSettings(4)))
PSYRDstep=str2num(cell2mat(PSYRDSettings(5)))
else
QCOMPmin=0.5
QCOMPmax=0.8
QCOMPstep=0.05
AQModemin=1
AQModemax=3
AQSTRENGTHmin=0.5
AQSTRENGTHmax=1.1
AQSTRENGTHstep=0.05
PSYmin=0.6
PSYmax=1.2
RDmin=0
RDmax=0.1
PSYRDstep=0.05
end
%WWWWWWWWWWWWWWWWWWW EXTRACT SOURCE/ZIPS WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW
if exist('x264.zip', 'file')==2
unzip('x264.zip')
delete('x264.zip')
end
cd mkvs
if exist('ffmpeg.zip', 'file')==2
unzip('ffmpeg.zip')
delete('ffmpeg.zip')
end
if exist('ffprobe.zip', 'file')==2
unzip('ffprobe.zip')
delete('ffprobe.zip')
end
copyfile('source.bat','source_new.bat')
find_and_replace('source_new.bat', 'SOURCEPATH', strrep(AVSPathName, '\', '\\'))
find_and_replace('source_new.bat', '\\source.avs', AVSFileName)
find_and_replace('source_new.bat', 'PATH', strrep(moviepath, '\', '\\'))
dos('source_new.bat')
cd ../;cd pngs/source
movefile('../../mkvs/*.png')
[heigh,width,z]=size(imread('source_00001.png'))
ref=floor( 32768 / ( ceil( width / 16 ) * ceil( heigh / 16 ) ) )
cd ../../;
knofinalbitrate = menu('Do you know what bitrate you want to use for your final encode?','No','Yes');
if knofinalbitrate~=2%-----$$$$$-----$$$$$-----$$$$$-----$$$$$-----$$$$$-----$$$$$-----$$$$$-----$$$$$-----$$$$$-----$$$$$-----$$$$$-----$$$$$-----$$$$$
skipcrf = menu('Do you want to skip Bitrate/Transparency comparisons for different CRF settings?','No (recommended)','Yes, I know what CRF I want to use.');
if skipcrf~=2%--------------------------------------------------------------------------------------------
%WWWWWWWWWWWWWWWWWWW SOURCE BITRATE QUESTION WWWWWWWWWWWWWWWWWWWWWWWWWWWWWW
Source_Bit_Rate=[];
while isempty(Source_Bit_Rate)==1
while isempty(Source_Bit_Rate)==1
prompt = {'Enter the bit-rate of the source (kbps):'};
dlg_title = 'User Input';
num_lines = 1;
defaultans = {'20000'};
Source_Bit_Rate = inputdlg(prompt,dlg_title,num_lines,defaultans);
end
Source_Bit_Rate=str2num(Source_Bit_Rate{:})
end
%WWWWWWWWWWWWWWWWWWW TEST CRF WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW
copyfile('crftest1.bat','crftest1_new.bat');
find_and_replace('crftest1_new.bat', '--ref 16', ['--ref ' num2str(ref)]);
find_and_replace('crftest1_new.bat', 'SOURCEPATH', strrep(AVSPathName, '\', '\\'))
find_and_replace('crftest1_new.bat', 'source.avs', AVSFileName)
find_and_replace('crftest1_new.bat', 'PATH', strrep(moviepath, '\', '\\'))
CRF=23;
k=1;
while CRF>13
copyfile('crftest1_new.bat','CRF_TEST.bat');
find_and_replace('CRF_TEST.bat', 'THE_CRF', num2str(CRF))
find_and_replace('CRF_TEST.bat', 'FILENAME', num2str(CRF))
dos('CRF_TEST.bat');
cd mkvs;
%%%%%%%%%%%%%% get crf.mkv bitrate %%%%%%%%%%%%%%%
copyfile('bitrate.bat','bitrate_new.bat')
find_and_replace('bitrate_new.bat', 'PATH', strrep(moviepath, '\', '\\'))
find_and_replace('bitrate_new.bat', 'THE_CRF', num2str(CRF))
dos('bitrate_new.bat')
file = textread('bitrate.txt', '%s', 'delimiter', '\n');
bit_rate=file{2,1};
bit_rate_all(k)=ceil(str2num(strrep(bit_rate, 'bit_rate=', ''))/1024)
%%%%%%%%%%%%%% explode crf.mkv %%%%%%%%%%%%%%%%%%
copyfile('batch.bat','batch_new.bat')
find_and_replace('batch_new.bat', 'PATH', strrep(moviepath, '\', '\\'))
find_and_replace('batch_new.bat', 'THE_CRF', num2str(CRF))
dos('batch_new.bat')
%%%%%%%%%%%%%% move and compare %%%%%%%%%%%%%%%%%
cd ../;cd pngs/encode
movefile('../../mkvs/*.png')
cd ../../
[MSE,PSNR,NK,AD,SC,MD,NAE,SSI,Zeros]=comparer(moviepath,0,0,0,0,0,0,0,0,1);
for l=1:length(Zeros)
CRF_Zeros(l,k)=Zeros(l);
end
k=k+1
CRF=CRF-1;
end
%WWWWWWWWWWWWWWWWWWW GET TRANSPARENCYWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW
for q=1:length(CRF_Zeros(:,1))
for e=1:length(CRF_Zeros(1,:))
Transparency(q,e)=CRF_Zeros(q,e)/(width*heigh*3);
MeanTransparency(e)=mean(Transparency(:,e));
end
end
%WWWWWWWWWWWWWWWWWWW PLOT WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW
figure('units','normalized','outerposition',[0 0 1 1])
x=linspace(21,14,length(MeanTransparency(1,:)));
x2=linspace(1,1,length(MeanTransparency(1,:)));
x3=linspace(0.998,0.998,length(MeanTransparency(1,:)));
plot(x,x2,'r',x,MeanTransparency(1,:),'b-o',x,(bit_rate_all(1,:)/Source_Bit_Rate),'g-o',x,x3,'r')
title('Transparency / Bit-rate Plot')
xlabel('CRF')
legend('Source','Transparency','Bitrate','Location','northwest')
set(gca,'XTick',fliplr(x));
set(gca, 'xdir','reverse')
set(findall(gca, 'Type', 'Line'),'LineWidth',2);saveas(gcf,'CRF_comparison.png');
saveas(gcf,'CRF_comparison.fig','fig');
end%--------------------------------------------------------------------------------------------
%WWWWWWWWWWWWWWWWWWW CRF QUESTION WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW
STOP_CRF=[];
while isempty(STOP_CRF)==1
while isempty(STOP_CRF)==1
prompt = {'Enter the desired CRF:'};
dlg_title = 'User Input';
num_lines = 1;
defaultans = {'17'};
STOP_CRF = inputdlg(prompt,dlg_title,num_lines,defaultans);
end
STOP_CRF=str2num(STOP_CRF{:})
end
if skipcrf~=2;close;end;
if skipcrf==2 || any(STOP_CRF==x)==0%--------------------------------------------------------------------------------------------
copyfile('crftest1.bat','crftest1_new.bat');
find_and_replace('crftest1_new.bat', '--ref 16', ['--ref ' num2str(ref)]);
find_and_replace('crftest1_new.bat', 'SOURCEPATH', strrep(AVSPathName, '\', '\\'))
find_and_replace('crftest1_new.bat', 'source.avs', AVSFileName)
find_and_replace('crftest1_new.bat', 'PATH', strrep(moviepath, '\', '\\'))
copyfile('crftest1_new.bat','CRF_TEST.bat');
find_and_replace('CRF_TEST.bat', 'THE_CRF', num2str(STOP_CRF))
find_and_replace('CRF_TEST.bat', 'FILENAME', num2str(STOP_CRF))
dos('CRF_TEST.bat');
cd mkvs;
%%%%%%%%%%%%%% get crf.mkv bitrate %%%%%%%%%%%%%%%
copyfile('bitrate.bat','bitrate_new.bat')
find_and_replace('bitrate_new.bat', 'PATH', strrep(moviepath, '\', '\\'))
find_and_replace('bitrate_new.bat', 'THE_CRF', num2str(STOP_CRF))
dos('bitrate_new.bat')
file = textread('bitrate.txt', '%s', 'delimiter', '\n');
bit_rate2=file{2,1};
bit_rate=num2str(ceil(str2num(strrep(bit_rate2, 'bit_rate=', ''))/1024))
cd ../
else
bit_rate=num2str(bit_rate_all((21-STOP_CRF)*2+1))
end%--------------------------------------------------------------------------------------------
else%-----$$$$$-----$$$$$-----$$$$$-----$$$$$-----$$$$$-----$$$$$-----$$$$$-----$$$$$-----$$$$$-----$$$$$-----$$$$$-----$$$$$-----$$$$$
bit_rate=[];
while isempty(bit_rate)==1
while isempty(bit_rate)==1
prompt = {'Enter the bitrate you want to use:'};
dlg_title = 'User Input';
num_lines = 1;
defaultans = {'10000'};
bit_rate = inputdlg(prompt,dlg_title,num_lines,defaultans);
end
bit_rate=num2str(bit_rate{:})
end
end%-----$$$$$-----$$$$$-----$$$$$-----$$$$$-----$$$$$-----$$$$$-----$$$$$-----$$$$$-----$$$$$-----$$$$$-----$$$$$-----$$$$$-----$$$$$
%WWWWWWWWWWWWWWWWWWWWWWWWWWWWWW START TESTS WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW
%----------------------TEST QCOMP----------------------
qcomp=QCOMPmin
u=1;
while qcomp<QCOMPmax+QCOMPstep
copyfile('crftest2.bat','crftest2_new.bat');
find_and_replace('crftest2_new.bat', 'SOURCEPATH', strrep(AVSPathName, '\', '\\'))
find_and_replace('crftest2_new.bat', 'source.avs', AVSFileName)
find_and_replace('crftest2_new.bat', 'PATH', strrep(moviepath, '\', '\\'))
find_and_replace('crftest2_new.bat', '--ref 16', ['--ref ' num2str(ref)]);
find_and_replace('crftest2_new.bat', 'BIT_RATE', bit_rate)
find_and_replace('crftest2_new.bat', 'qcomp 0.60', ['qcomp ' num2str(qcomp)])
find_and_replace('crftest2_new.bat', 'FILENAME', ['qcomp ' num2str(qcomp)])
dos('crftest2_new.bat');
cd mkvs;
copyfile('batch.bat','batch_new.bat')
find_and_replace('batch_new.bat', 'PATH', strrep(moviepath, '\', '\\'))
find_and_replace('batch_new.bat', 'THE_CRF', ['qcomp ' num2str(qcomp)])
dos('batch_new.bat')
cd ../;cd pngs/encode
movefile('../../mkvs/*.png')
cd ../../
[MSE,PSNR,NK,AD,SC,MD,NAE,SSI,Zeros]=comparer(moviepath,1,1,1,1,1,1,1,1,1);
for l=1:length(Zeros)
QCOMP_MSE(l,u)=MSE(l);
QCOMP_PSNR(l,u)=PSNR(l);
QCOMP_NK(l,u)=NK(l);
QCOMP_AD(l,u)=AD(l);
QCOMP_SC(l,u)=SC(l);
QCOMP_MD(l,u)=MD(l);
QCOMP_NAE(l,u)=NAE(l);
QCOMP_SSI(l,u)=SSI(l);
QCOMP_Zeros(l,u)=Zeros(l);
end
u=u+1;
qcomp=qcomp+QCOMPstep
end
FinalQcomp=BTPQ(moviepath,'qcomp',QCOMPmin,QCOMPmax+QCOMPstep,length(QCOMP_Zeros(1,:)),QCOMP_MSE,QCOMP_PSNR,QCOMP_NK,QCOMP_AD,QCOMP_SC,QCOMP_MD,QCOMP_NAE,QCOMP_SSI,QCOMP_Zeros,QCOMPstep)
%----------------------TEST AQ MODE ----------------------
mode=AQModemin
u=1;
while mode<AQModemax+1
copyfile('crftest2.bat','crftest2_new.bat');
find_and_replace('crftest2_new.bat', 'SOURCEPATH', strrep(AVSPathName, '\', '\\'))
find_and_replace('crftest2_new.bat', 'source.avs', AVSFileName)
find_and_replace('crftest2_new.bat', 'PATH', strrep(moviepath, '\', '\\'))
find_and_replace('crftest2_new.bat', '--ref 16', ['--aq-mode 1 --ref ' num2str(ref)]);
find_and_replace('crftest2_new.bat', 'BIT_RATE', bit_rate)
find_and_replace('crftest2_new.bat', 'qcomp 0.60', ['qcomp ' num2str(FinalQcomp)])
find_and_replace('crftest2_new.bat', '--aq-mode 1', ['--aq-mode ' num2str(mode)]);
find_and_replace('crftest2_new.bat', 'FILENAME', ['aq-mode ' num2str(mode)]);
dos('crftest2_new.bat');
cd mkvs;
copyfile('batch.bat','batch_new.bat')
find_and_replace('batch_new.bat', 'PATH', strrep(moviepath, '\', '\\'))
find_and_replace('batch_new.bat', 'THE_CRF', ['aq-mode ' num2str(mode)])
dos('batch_new.bat')
cd ../;cd pngs/encode
movefile('../../mkvs/*.png')
cd ../../
[MSE,PSNR,NK,AD,SC,MD,NAE,SSI,Zeros]=comparer(moviepath,1,1,1,1,1,1,1,1,1);
for l=1:length(Zeros)
AQMode_MSE(l,u)=MSE(l);
AQMode_PSNR(l,u)=PSNR(l);
AQMode_NK(l,u)=NK(l);
AQMode_AD(l,u)=AD(l);
AQMode_SC(l,u)=SC(l);
AQMode_MD(l,u)=MD(l);
AQMode_NAE(l,u)=NAE(l);
AQMode_SSI(l,u)=SSI(l);
AQMode_Zeros(l,u)=Zeros(l);
end
u=u+1;
mode=mode+1
end
FinalAQmode=BTPQ(moviepath,'aq-mode',AQModemin,AQModemax+1,length(AQMode_Zeros(1,:)),AQMode_MSE,AQMode_PSNR,AQMode_NK,AQMode_AD,AQMode_SC,AQMode_MD,AQMode_NAE,AQMode_SSI,AQMode_Zeros,1)
%----------------------TEST AQ STRENGTH ----------------------
strength=AQSTRENGTHmin
u=1;
while strength<AQSTRENGTHmax+AQSTRENGTHstep
copyfile('crftest2.bat','crftest2_new.bat');
find_and_replace('crftest2_new.bat', 'SOURCEPATH', strrep(AVSPathName, '\', '\\'))
find_and_replace('crftest2_new.bat', 'source.avs', AVSFileName)
find_and_replace('crftest2_new.bat', 'PATH', strrep(moviepath, '\', '\\'))
find_and_replace('crftest2_new.bat', '--ref 16', ['--aq-mode 1 --aq-strength 0.5 --ref ' num2str(ref)]);
find_and_replace('crftest2_new.bat', 'BIT_RATE', bit_rate)
find_and_replace('crftest2_new.bat', 'qcomp 0.60', ['qcomp ' num2str(FinalQcomp)])
find_and_replace('crftest2_new.bat', 'aq-strength 0.5', ['aq-strength ' num2str(strength)])
find_and_replace('crftest2_new.bat', '--aq-mode 1', ['--aq-mode ' num2str(FinalAQmode)]);
find_and_replace('crftest2_new.bat', 'FILENAME', ['aq-strength ' num2str(strength)]);
dos('crftest2_new.bat');
cd mkvs;
copyfile('batch.bat','batch_new.bat')
find_and_replace('batch_new.bat', 'PATH', strrep(moviepath, '\', '\\'))
find_and_replace('batch_new.bat', 'THE_CRF', ['aq-strength ' num2str(strength)])
dos('batch_new.bat')
cd ../;cd pngs/encode
movefile('../../mkvs/*.png')
cd ../../
[MSE,PSNR,NK,AD,SC,MD,NAE,SSI,Zeros]=comparer(moviepath,1,1,1,1,1,1,1,1,1);
for l=1:length(Zeros)
AQstrength_MSE(l,u)=MSE(l);
AQstrength_PSNR(l,u)=PSNR(l);
AQstrength_NK(l,u)=NK(l);
AQstrength_AD(l,u)=AD(l);
AQstrength_SC(l,u)=SC(l);
AQstrength_MD(l,u)=MD(l);
AQstrength_NAE(l,u)=NAE(l);
AQstrength_SSI(l,u)=SSI(l);
AQstrength_Zeros(l,u)=Zeros(l);
end
u=u+1;
strength=strength+AQSTRENGTHstep
end
FinalAQstrength=BTPQ(moviepath,'aq-strength',AQSTRENGTHmin,AQSTRENGTHmax+AQSTRENGTHstep,length(AQstrength_Zeros(1,:)),AQstrength_MSE,AQstrength_PSNR,AQstrength_NK,AQstrength_AD,AQstrength_SC,AQstrength_MD,AQstrength_NAE,AQstrength_SSI,AQstrength_Zeros,AQSTRENGTHstep)
%----------------------TEST PSYRD ----------------------
psy=PSYmin
u=1;
while psy<PSYmax+PSYRDstep
copyfile('crftest2.bat','crftest2_new.bat');
find_and_replace('crftest2_new.bat', 'SOURCEPATH', strrep(AVSPathName, '\', '\\'))
find_and_replace('crftest2_new.bat', 'source.avs', AVSFileName)
find_and_replace('crftest2_new.bat', 'PATH', strrep(moviepath, '\', '\\'))
find_and_replace('crftest2_new.bat', '--ref 16', ['--psy-rd 0.70:0.00 --aq-mode 1 --aq-strength 0.5 --ref ' num2str(ref)]);
find_and_replace('crftest2_new.bat', 'BIT_RATE', bit_rate)
find_and_replace('crftest2_new.bat', 'qcomp 0.60', ['qcomp ' num2str(FinalQcomp)])
find_and_replace('crftest2_new.bat', 'aq-strength 0.5', ['aq-strength ' num2str(FinalAQstrength)])
find_and_replace('crftest2_new.bat', '--aq-mode 1', ['--aq-mode ' num2str(FinalAQmode)]);
find_and_replace('crftest2_new.bat', 'FILENAME', ['psy-rd ' num2str(psy)]);
find_and_replace('crftest2_new.bat', '--psy-rd 0.70', ['--psy-rd ' num2str(psy)]);
dos('crftest2_new.bat');
cd mkvs;
copyfile('batch.bat','batch_new.bat')
find_and_replace('batch_new.bat', 'PATH', strrep(moviepath, '\', '\\'))
find_and_replace('batch_new.bat', 'THE_CRF', ['psy-rd ' num2str(psy)])
dos('batch_new.bat')
cd ../;cd pngs/encode
movefile('../../mkvs/*.png')
cd ../../
[MSE,PSNR,NK,AD,SC,MD,NAE,SSI,Zeros]=comparer(moviepath,1,1,1,1,1,1,1,1,1);
for l=1:length(Zeros)
PSY_MSE(l,u)=MSE(l);
PSY_PSNR(l,u)=PSNR(l);
PSY_NK(l,u)=NK(l);
PSY_AD(l,u)=AD(l);
PSY_SC(l,u)=SC(l);
PSY_MD(l,u)=MD(l);
PSY_NAE(l,u)=NAE(l);
PSY_SSI(l,u)=SSI(l);
PSY_Zeros(l,u)=Zeros(l);
end
u=u+1;
psy=psy+PSYRDstep
end
FinalPSY=BTPQ(moviepath,'psy-rd',PSYmin,PSYmax+PSYRDstep,length(PSY_Zeros(1,:)),PSY_MSE,PSY_PSNR,PSY_NK,PSY_AD,PSY_SC,PSY_MD,PSY_NAE,PSY_SSI,PSY_Zeros,PSYRDstep)
%----------------------TEST RD ----------------------
rd=RDmin
u=1;
while rd<RDmax+PSYRDstep
copyfile('crftest2.bat','crftest2_new.bat');
find_and_replace('crftest2_new.bat', 'SOURCEPATH', strrep(AVSPathName, '\', '\\'))
find_and_replace('crftest2_new.bat', 'source.avs', AVSFileName)
find_and_replace('crftest2_new.bat', 'PATH', strrep(moviepath, '\', '\\'))
find_and_replace('crftest2_new.bat', '--ref 16', ['--psy-rd 0.70:0.00 --aq-mode 1 --aq-strength 0.5 --ref ' num2str(ref)]);
find_and_replace('crftest2_new.bat', 'BIT_RATE', bit_rate)
find_and_replace('crftest2_new.bat', 'qcomp 0.60', ['qcomp ' num2str(FinalQcomp)])
find_and_replace('crftest2_new.bat', 'aq-strength 0.5', ['aq-strength ' num2str(FinalAQstrength)])
find_and_replace('crftest2_new.bat', '--aq-mode 1', ['--aq-mode ' num2str(FinalAQmode)]);
find_and_replace('crftest2_new.bat', 'FILENAME', ['rd ' num2str(rd)]);
find_and_replace('crftest2_new.bat', '--psy-rd 0.70', ['--psy-rd ' num2str(FinalPSY)]);
find_and_replace('crftest2_new.bat', ':0.00', [':' num2str(rd)]);
dos('crftest2_new.bat');
cd mkvs;
copyfile('batch.bat','batch_new.bat')
find_and_replace('batch_new.bat', 'PATH', strrep(moviepath, '\', '\\'))
find_and_replace('batch_new.bat', 'THE_CRF', ['rd ' num2str(rd)])
dos('batch_new.bat')
cd ../;cd pngs/encode
movefile('../../mkvs/*.png')
cd ../../
[MSE,PSNR,NK,AD,SC,MD,NAE,SSI,Zeros]=comparer(moviepath,1,1,1,1,1,1,1,1,1);
for l=1:length(Zeros)
RD_MSE(l,u)=MSE(l);
RD_PSNR(l,u)=PSNR(l);
RD_NK(l,u)=NK(l);
RD_AD(l,u)=AD(l);
RD_SC(l,u)=SC(l);
RD_MD(l,u)=MD(l);
RD_NAE(l,u)=NAE(l);
RD_SSI(l,u)=SSI(l);
RD_Zeros(l,u)=Zeros(l);
end
u=u+1;
rd=rd+PSYRDstep
end
FinalRD=BTPQ(moviepath,'rd',RDmin,RDmax+PSYRDstep,length(RD_Zeros(1,:)),RD_MSE,RD_PSNR,RD_NK,RD_AD,RD_SC,RD_MD,RD_NAE,RD_SSI,RD_Zeros,PSYRDstep)
%----------------------DE BLOCK ----------------------
for u=1:2
copyfile('crftest2.bat','crftest2_new.bat');
find_and_replace('crftest2_new.bat', 'SOURCEPATH', strrep(AVSPathName, '\', '\\'))
find_and_replace('crftest2_new.bat', 'source.avs', AVSFileName)
find_and_replace('crftest2_new.bat', 'PATH', strrep(moviepath, '\', '\\'))
find_and_replace('crftest2_new.bat', '--ref 16', '--psy-rd 0.70:0.00 --aq-mode 1 --aq-strength 0.5 --ref 16');
if u==2
find_and_replace('crftest2_new.bat', '--ref 16', ['--deblock -3:-3 --ref ' num2str(ref)]);
else
find_and_replace('crftest2_new.bat', '--ref 16', ['--deblock -1:-1 --ref ' num2str(ref)]);
end
find_and_replace('crftest2_new.bat', 'BIT_RATE', bit_rate)
find_and_replace('crftest2_new.bat', '--qcomp 0.60', ['--qcomp ' num2str(FinalQcomp)])
find_and_replace('crftest2_new.bat', 'FILENAME', ['deblock ' num2str(u)]);
find_and_replace('crftest2_new.bat', 'aq-strength 0.5', ['aq-strength ' num2str(FinalAQstrength)])
find_and_replace('crftest2_new.bat', '--aq-mode 1', ['--aq-mode ' num2str(FinalAQmode)]);
find_and_replace('crftest2_new.bat', '--psy-rd 0.70', ['--psy-rd ' num2str(FinalPSY)]);
find_and_replace('crftest2_new.bat', ':0.00', [':' num2str(FinalRD)]);
dos('crftest2_new.bat');
cd mkvs;
copyfile('batch.bat','batch_new.bat')
find_and_replace('batch_new.bat', 'PATH', strrep(moviepath, '\', '\\'))
find_and_replace('batch_new.bat', 'THE_CRF', ['deblock ' num2str(u)])
dos('batch_new.bat')
cd ../;cd pngs/encode
movefile('../../mkvs/*.png')
cd ../../
[MSE,PSNR,NK,AD,SC,MD,NAE,SSI,Zeros]=comparer(moviepath,0,0,0,0,0,0,0,1,0);
for l=1:length(SSI)
RD_Zeros(l,u)=SSI(l);
end
end
for e=1:length(RD_Zeros(1,:))
MeanRD(e)=mean(RD_Zeros(:,e));
end
[xRD,yRD]=max(MeanRD)
DEBLOCKSETTING=yRD
%----------------------no-mbtree ----------------------
copyfile('crftest2.bat','crftest2_new.bat');
find_and_replace('crftest2_new.bat', 'SOURCEPATH', strrep(AVSPathName, '\', '\\'))
find_and_replace('crftest2_new.bat', 'source.avs', AVSFileName)
find_and_replace('crftest2_new.bat', 'PATH', strrep(moviepath, '\', '\\'))
find_and_replace('crftest2_new.bat', '--ref 16', ['--no-mbtree --deblock -1:-1 --psy-rd 0.70:0.00 --aq-mode 1 --aq-strength 0.5 --ref ' num2str(ref)]);
if DEBLOCKSETTING==2
find_and_replace('crftest2_new.bat', '--deblock -1:-1', ['--deblock -3:-3 --ref ' num2str(ref)]);
end
find_and_replace('crftest2_new.bat', 'BIT_RATE', bit_rate)
find_and_replace('crftest2_new.bat', '--qcomp 0.60', ['--qcomp ' num2str(FinalQcomp)])
find_and_replace('crftest2_new.bat', 'FILENAME', 'mbtree_OFF');
find_and_replace('crftest2_new.bat', 'aq-strength 0.5', ['aq-strength ' num2str(FinalAQstrength)])
find_and_replace('crftest2_new.bat', '--aq-mode 1', ['--aq-mode ' num2str(FinalAQmode)]);
find_and_replace('crftest2_new.bat', '--psy-rd 0.70', ['--psy-rd ' num2str(FinalPSY)]);
find_and_replace('crftest2_new.bat', ':0.00', [':' num2str(FinalRD)]);
dos('crftest2_new.bat');
cd mkvs;
copyfile('batch.bat','batch_new.bat')
find_and_replace('batch_new.bat', 'PATH', strrep(moviepath, '\', '\\'))
find_and_replace('batch_new.bat', 'THE_CRF', 'mbtree_OFF')
dos('batch_new.bat')
cd ../;cd pngs/encode
movefile('../../mkvs/*.png')
cd ../../
[MSE,PSNR,NK,AD,SC,MD,NAE,SSI,Zeros]=comparer(moviepath,0,0,0,0,0,0,0,1,0);
MeanMB=mean(SSI);
if MeanMB>xRD
mbtree=0;newZeros=MeanMB;
else
mbtree=1;newZeros=xRD;
end
%----------------------dct-decimate----------------------
copyfile('crftest2.bat','crftest2_new.bat');
find_and_replace('crftest2_new.bat', 'SOURCEPATH', strrep(AVSPathName, '\', '\\'))
find_and_replace('crftest2_new.bat', 'source.avs', AVSFileName)
find_and_replace('crftest2_new.bat', 'PATH', strrep(moviepath, '\', '\\'))
find_and_replace('crftest2_new.bat', '--ref 16', ['--no-dct-decimate --no-mbtree --deblock -1:-1 --psy-rd 0.70:0.00 --aq-mode 1 --aq-strength 0.5 --ref ' num2str(ref)]);
if DEBLOCKSETTING==2
find_and_replace('crftest2_new.bat', '--deblock -1:-1', ['--deblock -3:-3 --ref ' num2str(ref)]);
end
if mbtree==1
find_and_replace('crftest2_new.bat', '--no-mbtree ', '');
end
find_and_replace('crftest2_new.bat', 'BIT_RATE', bit_rate)
find_and_replace('crftest2_new.bat', '--qcomp 0.60', ['--qcomp ' num2str(FinalQcomp)])
find_and_replace('crftest2_new.bat', 'FILENAME', 'decimate_OFF');
find_and_replace('crftest2_new.bat', 'aq-strength 0.5', ['aq-strength ' num2str(FinalAQstrength)])
find_and_replace('crftest2_new.bat', '--aq-mode 1', ['--aq-mode ' num2str(FinalAQmode)]);
find_and_replace('crftest2_new.bat', '--psy-rd 0.70', ['--psy-rd ' num2str(FinalPSY)]);
find_and_replace('crftest2_new.bat', ':0.00', [':' num2str(FinalRD)]);
dos('crftest2_new.bat');
cd mkvs;
copyfile('batch.bat','batch_new.bat')
find_and_replace('batch_new.bat', 'PATH', strrep(moviepath, '\', '\\'))
find_and_replace('batch_new.bat', 'THE_CRF', 'decimate_OFF')
dos('batch_new.bat')
cd ../;cd pngs/encode
movefile('../../mkvs/*.png')
cd ../../
[MSE,PSNR,NK,AD,SC,MD,NAE,SSI,Zeros]=comparer(moviepath,0,0,0,0,0,0,0,1,0);
MeanDCT=mean(SSI);
if MeanDCT>newZeros
dct=0
else
dct=1
end
%WWWWWWWWWWWWWWWWWWWWWWWWWWWWWW PRINT WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW
CreateFinal = menu('Do you want to do a test encode with the choosen settings?','Yes','No');
if CreateFinal == 1
copyfile('crftest2.bat','crftest2_new.bat');
find_and_replace('crftest2_new.bat', 'SOURCEPATH', strrep(AVSPathName, '\', '\\'))
find_and_replace('crftest2_new.bat', 'source.avs', AVSFileName)
find_and_replace('crftest2_new.bat', 'PATH', strrep(moviepath, '\', '\\'))
find_and_replace('crftest2_new.bat', 'mkvs\', '')
find_and_replace('crftest2_new.bat', '--ref 16', ['--no-dct-decimate --no-mbtree --deblock -1:-1 --psy-rd 0.70:0.00 --aq-mode 1 --aq-strength 0.5 --ref ' num2str(ref)]);
if DEBLOCKSETTING==2
find_and_replace('crftest2_new.bat', '--deblock -1:-1', ['--deblock -3:-3 --ref ' num2str(ref)]);
end
if mbtree==1
find_and_replace('crftest2_new.bat', '--no-mbtree ', '');
end
if dct==1
find_and_replace('crftest2_new.bat', '--no-dct-decimate ', '');
end
find_and_replace('crftest2_new.bat', 'BIT_RATE', bit_rate)
find_and_replace('crftest2_new.bat', '--qcomp 0.60', ['--qcomp ' num2str(FinalQcomp)])
find_and_replace('crftest2_new.bat', 'FILENAME', 'FINAL_ENCODE');
find_and_replace('crftest2_new.bat', 'aq-strength 0.5', ['aq-strength ' num2str(FinalAQstrength)])
find_and_replace('crftest2_new.bat', '--aq-mode 1', ['--aq-mode ' num2str(FinalAQmode)]);
find_and_replace('crftest2_new.bat', '--psy-rd 0.70', ['--psy-rd ' num2str(FinalPSY)]);
find_and_replace('crftest2_new.bat', ':0.00', [':' num2str(FinalRD)]);
dos('crftest2_new.bat');
end
clc
fprintf('\nThe test encode was saved in the working folder as FINAL_ENCODE.mkv\n')
Reference_Frames=ref
fprintf('\nChoosen values:\n')
Chosen_BitRate=vpa(bit_rate)
Chosen_Qcomp=vpa(FinalQcomp)
Chosen_AQMode=FinalAQmode
Chosen_AQStrength=vpa(FinalAQstrength)
Chosen_PSY=vpa(FinalPSY)
Chosen_RD=vpa(FinalRD)
fprintf('\nAlso the following values were chosen automatically considering the generated transparency:\n')
fprintf('\nDE BLOCK:\n')
if yRD==1
fprintf('--deblock -1:-1\n')
printedeblock='--deblock -1:-1';
else
fprintf('--deblock -3:-3\n')
printedeblock='--deblock -3:-3';
end
fprintf('\nmbtree:\n')
if mbtree==1
fprintf('ON\n')
printedmbtree='';
else
fprintf('OFF\n')
printedmbtree='--no-mbtree';
end
fprintf('\ndecimate:\n')
if dct==1
fprintf('ON\n')
printedct='';
else
fprintf('OFF\n')
printedct='--no-dct-decimate';
end
fprintf('\nThe diagrams were saved as png files in your folder.\n')
fid = fopen('final_settings.txt','wt');
fprintf(fid, '--ref %s --bitrate %s --qcomp %s --aq-mode %s --aq-strength %s --psy-rd %s:%s %s %s %s ',num2str(ref),bit_rate,num2str(FinalQcomp),num2str(FinalAQmode),num2str(FinalAQstrength),num2str(FinalPSY),num2str(FinalRD),char(printedeblock),char(printedmbtree),char(printedct));
fclose(fid);
fprintf('The settings were also saved as final_settings.txt in your folder.\n')
save('matlab.mat')
%WWWWWWWWWWWWWWWWWWWWWWWWWWWWWW DELETE FILES WWWWWWWWWWWWWWWWWWWWWWWWWWWWWW
ALL_mkvs=(strrep('-\mkvs\*.mkv', '-', moviepath));
ALL_mkv_Folder=(strrep('-\mkvs\', '-', moviepath));
Source_pngs=(strrep('-\pngs\source\*.png', '-', moviepath));
Source_png_Folder=(strrep('-\pngs\source\', '-', moviepath));
Encode_pngs=(strrep('-\pngs\encode\*.png', '-', moviepath));
Encode_png_Folder=(strrep('-\pngs\encode\', '-', moviepath));
Source_dir=dir(Source_pngs);
Encode_dir=dir(Encode_pngs);
MKV_dir=dir(ALL_mkvs);
NOS=length(Source_dir);
NOE=length(Encode_dir);
NOM=length(MKV_dir);
if isempty(Source_dir)==0 || isempty(Encode_dir)==0
DeletePNG = menu('Do you want to delete the generated frames (png files)?','Yes (recommended)','No');
if DeletePNG == 1
NOS=length(Source_dir);
NOE=length(Encode_dir);
for i=1:NOE
Encode_PNG=[Encode_png_Folder Encode_dir(i).name];
delete(Encode_PNG);
end
for i=1:NOS
Source_PNG=[Source_png_Folder Source_dir(i).name];
delete(Source_PNG);
end
end
end
if isempty(MKV_dir)==0
DeleteMKV = menu('Do you want to delete the generated mkv files (except the final test encode)? (Remember that you can use them to do some comparisons)','Yes','No');
if DeleteMKV == 1
for i=1:NOM
ALL_MKV=[ALL_mkv_Folder MKV_dir(i).name];
delete(ALL_MKV);
end
end
end
DeleteALL = menu('Do you want to delete all the other generated files (except the diagrams and the final_settings.txt)?','Yes (recommended)','No');
if DeleteALL == 1
if exist('crftest1_new.bat','file')==2;delete('crftest1_new.bat');end
if exist('crftest2_new.bat','file')==2;delete('crftest2_new.bat');end
if exist('aq-mode.fig','file')==2;delete('aq-mode.fig');end
if exist('aq-strength.fig','file')==2;delete('aq-strength.fig');end
if exist('Psy-RDO.fig','file')==2;delete('Psy-RDO.fig');end
if exist('Psy-Trellis.fig','file')==2;delete('Psy-Trellis.fig');end
if exist('qcomp.fig','file')==2;delete('qcomp.fig');end
if exist('CRF_comparison.fig','file')==2;delete('CRF_comparison.fig');end
if exist('matlab.mat','file')==2;delete('matlab.mat');end
if exist('CRF_TEST.bat','file')==2;delete('CRF_TEST.bat');end
cd mkvs;
if exist('batch_new.bat','file')==2;delete('batch_new.bat');end
if exist('bitrate_new.bat','file')==2;delete('bitrate_new.bat');end
if exist('source_new.bat','file')==2;delete('source_new.bat');end
if exist('bitrate.txt','file')==2;delete('bitrate.txt');end
cd ../
end