Forums [Archives] Forums [Archives] (
-   Avisynth Scripting (
-   -   Still having problems with CQ prediction (

p2phunter 03-30-2004 12:44 PM

Still having problems with CQ prediction
I've been into KVCDs for quite some time now. (Actually.. its nothing when compared to many other ppl in these forums :D )

But one thing which has still evaded me is the CQ prediction. I've tried ToK, ACP, CQMatic and the Manual Prediction from here. But nothing has given me correct CQ.

Most of the time my final MPG file will be smaller than expected. Like yesterday, I converted one of my XVID files to KVCD. I used CQMatic to predict the CQ. And when I used the CQ predicted by CQMatic, I got a mpg file which was of size ~ 700MB(after multiplexing with Audio). This is like 100MBs shorter than the desired size. Later, I got to know that CQMatic isnt accurate when it comes to AVI files. Still, when I burn it on a CD as KVCD and play it on my standalone player, both the audio and video quality was pretty good. But I guess it would have better if the size of the MPG was around 800MB.

Later, I tried to predict the CQ required for some other XVID file that I had using the manual prediction method given here. The AVS script that I used is given below.


## DLL Section ##
LoadPlugin("D:\KVCD\AviSynth\AviSynth 2.5\plugins\MPEG2Dec3.dll")
LoadPlugin("D:\KVCD\AviSynth\AviSynth 2.5\plugins\GripFit_YV12.dll")
LoadPlugin("D:\KVCD\AviSynth\AviSynth 2.5\plugins\STMedianFilter.dll")
LoadPlugin("D:\KVCD\AviSynth\AviSynth 2.5\plugins\asharp.dll")
LoadPlugin("D:\KVCD\AviSynth\AviSynth 2.5\plugins\UnFilter.dll")
LoadPlugin("D:\KVCD\AviSynth\AviSynth 2.5\plugins\UnDot.dll")

#You might need to change this
Import("D:\KVCD\AviSynth\AviSynth 2.5\Functions\functions.avs")

#Variables to be set
movie="Sample Movie" 

## Main section and static filters ###
AviSource("Q:\Sample Movie.avi",false)
asharp(1, 4)
GripCrop(352, 240, overscan=2, source_anamorphic=false)
STMedianFilter(3, 3, 1, 1 )

## Linear Motion Adaptive Filtering ##
# ( Portions from AviSynth's manual )
# This will apply variable temporalsoften
# and variable blur.
# Both filters are active at all times, and work inversely proportional to the
# activity, measured from current frame to next frame.

ScriptClip(" nf = YDifferenceToNext()" +chr(13)+ "unfilter( -(fmin(round(nf)*2, 100)), -(fmin(round(nf)*2, 100)) ).TemporalSoften( fmin( round(2/nf), 6), round(1/nf) , round(3/nf) , 1, 1)  ")


#LetterBox( Your_Values_Here ) # Depends on situation. Use MovieStacker!

prediction(movie, cds , cdtime, factor, SampleSize, CQ, MinAudioBitrate). Sampler(length=24)
## Functions ###

function fmin( int f1, int f2) {
  return ( f1<f2 ) ? f1 : f2

The file functions.avs is given below


# Prediction Function #

function Prediction (clip c, string "movie", int "cds", int "cdtime", float "factor", float "SampleSize", float "CQ", int "MinAudioBitrate")

movie = default(movie,"No_Title_Defined")
factor = default(factor,0.98)
cds = default(cds,0)
cdtime = default(cdtime,80)
SampleSize = default(SampleSize,0)
CQ = default(CQ,0)
MinAudioBitrate = default(MinAudioBitrate,128) # lowest audio bitrate wanted

MovieLength = framecount(c)/framerate(c)
FramesTotal = framecount(c)
FramesSample = framecount(Sampler(c,length=24))
CD_Size = (((cdtime*10)-5)*1024)*cds

MinAudio = (((((CD_Size-((MinAudioBitrate/8)*Movielength))*FramesSample)/FramesTotal)*factor))/1000
EncodedSample = (SampleSize==0)? false: true #--------------------------------------------------------------------
SampleSize = (SampleSize==0)? 1: SampleSize # if sample size =0 mb then no previous sample.
CQ = (CQ<=0)? 1 :CQ # set cq between 1-100
CQ = (CQ>100)?100 : CQ #---------------------------------------------------------------------
SampleSizeMessage=(EncodedSample==false)?"no sample encoded yet **********": "Sample Size (mb) : "
SampleCQMessage=(EncodedSample==false)?"no sample encoded yet **********":"Sample CQ : "
TryCQValueMessage=(EncodedSample==false)?"no sample encoded yet ******":"Try CQ value of : "


\Subtitle(movie, 272,40, halo_color=$5500FF, text_color=$FFFFFF, font="Courier", size=27, align=5).

\Subtitle("Total Frames : " + String(FramesTotal), 80,60).
\Subtitle("Sample Frames : " + String(FramesSample), 80,80).
\Subtitle("Time (sec) : " + String(round(Movielength)), 80,100).
\Subtitle("Time (min) : " + String(round(Movielength/60)), 80,120).

\Subtitle(SampleSizeMessage+LeftStr(String(SampleSize),5), 270,60,text_color=$FF0000).
\Subtitle(SampleCQMessage + LeftStr(String(CQ),4),270,80,text_color=$FF0000).
\Subtitle("Min. Audio Bitrate : "+ String(MinAudioBitrate)+" kbps",270,100,text_color=$FF0000).
\Subtitle("Desired Sample Size (mb) : " + LeftStr(String(MinAudio),5),270,120,text_color=$FF0000).
\Subtitle(TryCQValueMessage + LeftStr(String((MinAudio*CQ)/SampleSize),4),270,140,text_color=$FF0000).

\Subtitle("Desired Sample Size (mb) for " + String(cds) + " CD(s):", 80,165, text_color=$FFFFFF).
\Subtitle("- audio at 96 kbps : " + LeftStr(String((((((CD_Size-((096/8)*Movielength))*FramesSample)/FramesTotal)*factor))/1000),5),80,185).
\Subtitle("- audio at 112 kbps : " + LeftStr(String((((((CD_Size-((112/8)*Movielength))*FramesSample)/FramesTotal)*factor))/1000),5),80,205).
\Subtitle("- audio at 128 kbps : " + LeftStr(String((((((CD_Size-((128/8)*Movielength))*FramesSample)/FramesTotal)*factor))/1000),5),80,225).
\Subtitle("- audio at 160 kbps : " + LeftStr(String((((((CD_Size-((160/8)*Movielength))*FramesSample)/FramesTotal)*factor))/1000),5),80,245).
\Subtitle("- audio at 192 kbps : " + LeftStr(String((((((CD_Size-((192/8)*Movielength))*FramesSample)/FramesTotal)*factor))/1000),5),80,265).
\Subtitle("- audio at 224 kbps : " + LeftStr(String((((((CD_Size-((224/8)*Movielength))*FramesSample)/FramesTotal)*factor))/1000),5),80,285).

\Subtitle("Average Bitrate (kbps) : "+ String(Round(((((CD_Size-((128/8)*Movielength)))/Movielength)*8)*1)), 80,328).

\Subtitle("Predicted Audio File Sizes (mb):", 80,373, text_color=$FFFFFF).
\Subtitle(" 96 kbps = " + String(Round(((( 96*1024)/8)*Movielength)/1048576)),80,395).
\Subtitle("112 kbps = " + String(Round((((112*1024)/8)*Movielength)/1048576)),80,416).
\Subtitle("128 kbps = " + String(Round((((128*1024)/8)*Movielength)/1048576)),80,437).
\Subtitle("160 kbps = " + String(Round((((160*1024)/8)*Movielength)/1048576)),270,395).
\Subtitle("192 kbps = " + String(Round((((192*1024)/8)*Movielength)/1048576)),270,416).
\Subtitle("224 kbps = " + String(Round((((224*1024)/8)*Movielength)/1048576)),270,437).

\BilinearResize(width(c), height(c))
return d+c+d

I was able to view the avs file in WMP. Now, the first time, I set the variables SampleSize and CQ to 0 when I called the function prediction. And once encoding is done, I got the file size and CQ and substituted them to the old values. The suggested CQ (displayed as a subtitle on the first screen) was 90. I did it again and now the suggested CQ was 68. Then it started to oscillate between ~69 and ~91. The required sample size was 11.6Megs. But with CQ of 68, the sample size size was ~8Megs and with CQ of 91, the sample size was ~13-14Megs.

(Edit: When I encoded with TMPGenc, I had set the stream type as Video Only. Does it make any difference?)

So, I stopped using the suggested CQ (that was displayed as a subtitle on the screen) and made my own trial and error encodings with different CQs until I got a file size ~ equal to the required sample size. And, I used this CQ to encode my movie. I got a nice MPEG file of size 787MB.

But, the formula that kwag has suggested didnt work. What could be the reason. Could it be because the source was AVI?. What am I doing wrong.

Dialhot 03-30-2004 01:18 PM

I really don't know where you find this script (and I apologize to the author) but there are more things to change or remove in it than lines to keep (I mean, if you are encoding an xvid) ! So just forget it.

What was the script you used when you tried CQMatic and other tools ?

jorel 03-30-2004 06:28 PM

(and I apologize to the author).....

:lol: :lol: :lol: :lol:

p2phunter 03-30-2004 07:10 PM

Actually, I got the script from here

I got the prediction part of the script from here

And.. I used the same script when predicting using CQMatic :oops: which I guess isnt right :x

But... now I notice that the script was for DVD source. So I guess, I must use some other script for AVIs. Can some one point me to a good and optimal AVS script for AVI source?

thanks for your help!

Prodater64 03-30-2004 07:29 PM

Please, read ALL in this thread. Is very usefull

Dialhot 03-30-2004 07:31 PM

Trust me, I didn't say all this just because I provided the optimal scripts for avi :-)

You'll find them here :

p2phunter 03-30-2004 07:48 PM

thanks pal :D

Thanks to you too prodater!

p2phunter 03-31-2004 12:38 PM

I got ur (DialHot's) optimal script for AVI's and used it to convert one of my AVI's to KVCD. This is the script (v4) I used

(the width and height of the movie were divisible by 16)


## DLL Section ##
LoadPlugin("D:\KVCD\AviSynth\AviSynth 2.5\plugins\MPEG2Dec3.dll")
LoadPlugin("D:\KVCD\AviSynth\AviSynth 2.5\plugins\GripFit_YV12.dll")
LoadPlugin("D:\KVCD\AviSynth\AviSynth 2.5\plugins\UnDot.dll")
LoadPlugin("D:\KVCD\AviSynth\AviSynth 2.5\plugins\atc.dll")
LoadPlugin("D:\KVCD\AviSynth\AviSynth 2.5\plugins\blockbuster.dll")
LoadPlugin("D:\KVCD\AviSynth\AviSynth 2.5\plugins\DctFilter.dll")
LoadPlugin("D:\KVCD\AviSynth\AviSynth 2.5\plugins\Deen.dll")
LoadPlugin("D:\KVCD\AviSynth\AviSynth 2.5\plugins\Convolution3DYV12.dll")

Convolution3D(1, 6, 12, 6, 8, 2.8, 0)
GripCrop(352, 288, overscan=2, source_anamorphic=false)

And.. I used CQMatic to do the CQ prediction (X3 prediction). It took 86minutes for it to decide a CQ. The final CQ I got was 89.6. I used it to encode the movie. I got a 660MB .m1v file (still shorter than expected). So, I converted the source Wav file to 160kbps, 48KHz (is this ok.. or do I need to convert it to 44.1Khz?) MP2 file and in the end, I got a MPEG file of size 783 MB. This is a lot better result than my previous tries.

What I need to know is... is there a way I can reduce the prediction time when using CQMatic? Or, is there an alternative?. I still havent looked up the ping-pong method described in the link given by Prodater.


Dialhot 03-31-2004 12:53 PM

I guess that all filesize are negatives in the log of cqmatic and that means that the prediction failed !

That is what it took so much time and you ended up with a CQ of 89.

Please read CARREFULLY the hints given with CQmatic.

p2phunter 03-31-2004 01:10 PM

is the CQMatic Log file stored somewhere? I cant find it!

The size of the sample created by CQMatic during its last prediction (with CQ 89.7) was 38.9 MB (named as Sample.m1v.sample) . Does this information help in anyway?

And.. I think the prediction was actually successful!

All times are GMT -5. The time now is 09:03 AM  —  vBulletin Jelsoft Enterprises Ltd

Site design, images and content © 2002-2021 The Digital FAQ,
Forum Software by vBulletin · Copyright © 2021 Jelsoft Enterprises Ltd.