Author Topic: Difference between one-pass and two-pass enconding?  (Read 16062 times)

Offline Pedi

  • Member
  • Posts: 5
    • View Profile
Difference between one-pass and two-pass enconding?
« on: March 04, 2011, 09:06:05 AM »
Hey guys,

I'll be grateful if you guide me on what exactly difference is between one-pass (high-speed) and two-pass (low-speed) encoding methods for H.264 encoder?
I just found that the later lasts about twice the former!!

thanks a lot

Offline ckmox

  • Member
  • Posts: 29
    • View Profile
Re: Difference between one-pass and two-pass enconding?
« Reply #1 on: March 04, 2011, 09:14:31 PM »
1-pass encoding is considered by many as CRF (Constant Rate Factor) or Target Quality, in audio encoding its similar to VBR
2-pass encoding is considered by many as Average Bitrate Mode or Target Bitrate, in audio encoding its similar to ABR

CRF is somewhat constant quality but filesize output varies
ABR is somewhat constant filesize but quality output varies

if your targeting a specific quality and do not care about filesize then go for CRF
if your targeting a specific filesize and do not care about quality then go for ABR

Offline Pedi

  • Member
  • Posts: 5
    • View Profile
Re: Difference between one-pass and two-pass enconding?
« Reply #2 on: March 05, 2011, 12:42:49 AM »
ckmox, I was somewhat confused with the definitions you said.
If 1-pass encoding is a Constant rate factor method, but why you say it's similar to Variable bitrate for audio?!! (in VBR mode bitrate varies to keep the quality constant!!)
I guess 1-pass encoding should be a constant-bitrate mode, because its conversion time is really very shorter than 2-pass one.
Beside all, do you think the 2-pass encoding is really more effective than 1-pass to result in a better quality even lasting for twice conversion time?? and by the definition you said, even spending twice time in 2-pass encoding, to get a specific filesize, does my video quality degrades in some points of runtime??

thanks chmox
« Last Edit: March 05, 2011, 12:51:59 AM by Pedi »

Offline ckmox

  • Member
  • Posts: 29
    • View Profile
Re: Difference between one-pass and two-pass enconding?
« Reply #3 on: March 05, 2011, 01:20:28 AM »
do not be confused what all i said are true but if you want more details then read this link ->
http://www.avidemux.org/admWiki/doku.php?id=tutorial:h.264 
especially read the Encoding Mode section under Rate Control category

Offline J_Darnley

  • Global Moderator
  • Member
  • *****
  • Posts: 397
    • View Profile
Re: Difference between one-pass and two-pass enconding?
« Reply #4 on: March 05, 2011, 01:27:35 AM »
2-pass encoding is considered by many as Average Bitrate Mode or Target Bitrate, in audio encoding its similar to ABR
No, it is not ABR.  It is VBR, just like crf except that it takes a second pass to know what quality is needed to get the requested bitrate.

Quote
If you're targeting a specific quality and do not care about filesize then go for CRF.
If you're targeting a specific filesize and do not care about quality then go for 2-pass.
FTFY
Knowledgeable about: cmd.exe, ffmpeg, x264

Offline Pedi

  • Member
  • Posts: 5
    • View Profile
Re: Difference between one-pass and two-pass enconding?
« Reply #5 on: March 05, 2011, 01:54:34 AM »
ckmox, thanks for the ref.

J, could you please guide me if it's effective (practical!) a 2-pass encoding mode in comparison with 1-pass mode, to get a better quality in a closely same filesize? TIA

Offline nm

  • Member
  • Posts: 358
    • View Profile
Re: Difference between one-pass and two-pass enconding?
« Reply #6 on: March 05, 2011, 02:30:07 AM »
J, could you please guide me if it's effective (practical!) a 2-pass encoding mode in comparison with 1-pass mode, to get a better quality in a closely same filesize? TIA

If you mean 1-pass ABR, then Yes, a second pass gives you much better results.

If you mean 1-pass CRF, then a 2-pass encode does not give better quality/bitrate.

Read the tutorial that ckmox linked to.

Offline AnonCrow

  • Member
  • Posts: 36
    • View Profile
Re: Difference between one-pass and two-pass enconding?
« Reply #7 on: March 05, 2011, 02:53:10 AM »
Quote from: Pedi
a 2-pass encoding mode in comparison with 1-pass mode, to get a better quality in a closely same filesize?
If the filesizes (bitrates) are the same and the other encoding settings are identical, then there's no difference in quality - if anything,
the 1-pass CRF encode may be marginally better.

Offline yetanotherid

  • Member
  • Posts: 62
    • View Profile
Re: Difference between one-pass and two-pass enconding?
« Reply #8 on: March 18, 2011, 06:46:15 PM »
Pick a CRF value and run a single pass encode.
Use the resulting file size for a 2 pass encode.
Look in the conversion program's log files for the CRF value used by the encoder for the second pass. It should be pretty much the same as the CRF value of the single pass encode.
Therefore, the quality is the same.

Offline Dark Shikari

  • x264 developer
  • Administrator
  • Member
  • *****
  • Posts: 650
    • View Profile
Re: Difference between one-pass and two-pass enconding?
« Reply #9 on: March 19, 2011, 12:22:33 AM »
Pick a CRF value and run a single pass encode.
Use the resulting file size for a 2 pass encode.
Look in the conversion program's log files for the CRF value used by the encoder for the second pass.
x264 doesn't print such a value.

Offline yetanotherid

  • Member
  • Posts: 62
    • View Profile
Re: Difference between one-pass and two-pass enconding?
« Reply #10 on: March 25, 2011, 05:48:55 PM »
So where does MeGUI get it from, or am I making an incorrect assumption?

In the log file for the first pass, included under "Standard error stream" (I've wondered why it's called error stream, but anyway....) in amongst a whole bunch of stuff I don't understand is:
x264 [info]: final ratefactor: 18.27 (or whatever the value happens to be)
That's not the CRF value of the second pass? It certainly seems to change according to file size (for the same video) as though that's what it is.

I'm sure I've read a similar thing in the command prompt window when encoding with HDConvertToX and I assumed it was reporting the CRF value after the first pass too. No?
« Last Edit: March 25, 2011, 06:01:01 PM by yetanotherid »

Offline yetanotherid

  • Member
  • Posts: 62
    • View Profile
Re: Difference between one-pass and two-pass enconding?
« Reply #11 on: March 28, 2011, 01:33:40 PM »
By accident I encoded a video while selecting the wrong file size (700MB) an hour or so ago, and according to the log file:
x264 [info]: final ratefactor: 22.81
When I re-encoded it using a 1.45GB file size:
x264 [info]: final ratefactor: 17.86
So I guess you can see why I assumed it's the CRF value of the second pass, but if that's not what it is, what's the "final ratefactor"?
------------------------------------------------------------
Edit: Just for funsies I encoded a shortish AVI a couple of times using a single pass, then I used the resulting single pass file sizes for 2 pass encodes.
For the first single pass encode I used a CRF of 20.
For the 2 pass encode MeGUI's log file reported after the first pass, x264 [info]: final ratefactor: 19.37
The second time I used a CRF of 22.
For the 2 pass encode MeGUI's log file reported after the first pass, x264 [info]: final ratefactor: 21.38
So assuming the reported final ratefactor is the CRF value of the second pass, which it seems to be, it also seems x264 must be slightly more efficient when using 2 passes to encode, hence giving slightly better quality for the same file size? Would any of that be a fair assumption?

Offline nm

  • Member
  • Posts: 358
    • View Profile
Re: Difference between one-pass and two-pass enconding?
« Reply #12 on: March 28, 2011, 02:47:58 PM »
Edit: Just for funsies I encoded a shortish AVI a couple of times using a single pass, then I used the resulting single pass file sizes for 2 pass encodes.
For the first single pass encode I used a CRF of 20.
For the 2 pass encode MeGUI's log file reported after the first pass, x264 [info]: final ratefactor: 19.37
The second time I used a CRF of 22.
For the 2 pass encode MeGUI's log file reported after the first pass, x264 [info]: final ratefactor: 21.38
So assuming the reported final ratefactor is the CRF value of the second pass, which it seems to be

DS already said it isn't. How would x264 even know which settings you are going to use for the second pass when it prints the number at the end of the first pass. Or if it did assume certain settings, how would it estimate the CRF value without actually using those settings?

Final ratefactor is the average QP used during the first pass.

Quote
it also seems x264 must be slightly more efficient when using 2 passes to encode, hence giving slightly better quality for the same file size? Would any of that be a fair assumption?

No. The final ratefactor assumption doesn't hold and the quality is about the same.

For a valid comparison, use --tune ssim --ssim:

1. Encode with --crf 20 --tune ssim -ssim.
2. Make a two-pass encode with the same ssim parameters, targeting the average bitrate that the CRF encode arrived at. Compare running times, SSIM and the file sizes.

I tend to get slightly better quality (higher SSIM / bitrate) with CRF.

If you want to make the test more fair to CRF, compensate the speed difference caused by the first pass by using slower settings in the CRF encode. This is difficult to generalize though because different inputs have wildly different speeds. The slower the input, the higher the advantage of 1-pass CRF.

Offline yetanotherid

  • Member
  • Posts: 62
    • View Profile
Re: Difference between one-pass and two-pass enconding?
« Reply #13 on: March 28, 2011, 06:29:44 PM »
DS already said it isn't.

He said x264 doesn't print that value.

How would x264 even know which settings you are going to use for the second pass when it prints the number at the end of the first pass. Or if it did assume certain settings, how would it estimate the CRF value without actually using those settings?

I don't know. Wouldn't you generally tell it which settings you're using for the second pass before running the first pass?
What's the purpose of the first pass if not to work those things out? Maybe if I knew the answers I wouldn't have asked the question?

Final ratefactor is the average QP used during the first pass.

http://mewiki.project357.com/wiki/X264_Settings#crf
The final ratecontrol method: Constant Ratefactor. While qp targets a certain quantizer, and bitrate targets a certain filesize, crf targets a certain 'quality'. The idea is for crf n to give the same perceptual quality as qp n, just in a smaller space. The arbitrary unit of measure for crf values is the "ratefactor".

So I guess you can see why x264 reporting a final ratefactor after the first pass fooled me into thinking it was reporting the final CRF value. Maybe it's a dumb question, but couldn't the average qp and the CRF value be the same thing?

Anyway, if I'm wrong, I'm wrong.... I don't really know what I'm talking about and I'll admit your comparison examples didn't mean much to me.

What I'm now trying to understand is if a 2 pass encode and a single pass CRF encode encodes the video differently. I understand (at least basically) a CRF encode aims for a constant perceived quality throughout the video which means quality can be sacrificed where it won't be noticed.... higher quantizers for action scenes.... that sort of thing. I understand that by setting the file size you're basically setting the quality, but the first pass needs to be run in order to determine what that quality will be.

So I've encoded a video using a CRF value of 22 and the file size finished up being 915MB. I now use 915MB for a 2 pass encode. Will the video be encoded differently or will the "quality" be distributed differently etc?

Offline nm

  • Member
  • Posts: 358
    • View Profile
Re: Difference between one-pass and two-pass enconding?
« Reply #14 on: March 29, 2011, 01:06:03 AM »
He said x264 doesn't print that value.

Yet you kept wondering if the printed final ratefactor is that value.

I don't know. Wouldn't you generally tell it which settings you're using for the second pass before running the first pass?

Nope. Generally x264 lowers the settings for a fast first pass in the function where parameters are parsed. The encoder itself doesn't even get to see your original command line. And even if the first pass is encoded with --slow-firstpass, the user is free to use whatever settings she wants in the second pass.

What's the purpose of the first pass if not to work those things out? Maybe if I knew the answers I wouldn't have asked the question?

It works some things out to help second pass rate control, but it can't estimate the CRF that the second pass arrives at.

Maybe it's a dumb question, but couldn't the average qp and the CRF value be the same thing?

They could be the same value in some case, but not generally. Also, final ratefactor is the average QP of the first pass, not the second. Remember that the first pass is usually run with much faster settings.

Anyway, if I'm wrong, I'm wrong.... I don't really know what I'm talking about and I'll admit your comparison examples didn't mean much to me.

It's just about encoding at the same bitrate and comparing the quality. SSIM is used for quality comparison because your eyes couldn't tell a difference one way or the other.

What I'm now trying to understand is if a 2 pass encode and a single pass CRF encode encodes the video differently. I understand (at least basically) a CRF encode aims for a constant perceived quality throughout the video which means quality can be sacrificed where it won't be noticed.... higher quantizers for action scenes.... that sort of thing. I understand that by setting the file size you're basically setting the quality, but the first pass needs to be run in order to determine what that quality will be.

So I've encoded a video using a CRF value of 22 and the file size finished up being 915MB. I now use 915MB for a 2 pass encode. Will the video be encoded differently or will the "quality" be distributed differently etc?

Final bitrate distribution (and therefore quality) is approximately the same:

http://forum.doom9.org/showthread.php?p=1230509#post1230509

Offline yetanotherid

  • Member
  • Posts: 62
    • View Profile
Re: Difference between one-pass and two-pass enconding?
« Reply #15 on: March 29, 2011, 07:31:06 PM »
Yet you kept wondering if the printed final ratefactor is that value.

Yes.

It works some things out to help second pass rate control, but it can't estimate the CRF that the second pass arrives at.

Yet from the post you linked to, Dark Shikari wrote:
"2-pass tries to approximate CRF by using the information from the first pass to decide on a constant quality factor."

Final bitrate distribution (and therefore quality) is approximately the same:

http://forum.doom9.org/showthread.php?p=1230509#post1230509

Which is why what you're saying still doesn't make sense to me.
The encoder can't estimate the CRF that the second pass will arrive at while at the same time it tries to approximate CRF by using the information from the first pass to decide on a constant quality factor. Final bitrate distribution (and therefore quality) is approximately the same for the two pass encode even though it happens without knowing what the quality will be as the encoder distributes those bits. I guess I don't understand how it works well enough yet....

Offline nm

  • Member
  • Posts: 358
    • View Profile
Re: Difference between one-pass and two-pass enconding?
« Reply #16 on: March 30, 2011, 02:04:33 AM »
Yet from the post you linked to, Dark Shikari wrote:
"2-pass tries to approximate CRF by using the information from the first pass to decide on a constant quality factor."

Yes, the second pass of a 2-pass encode arrives at an approximation, not the first one that prints the final ratefactor number. You got mixed up on the passes.

Which is why what you're saying still doesn't make sense to me.
The encoder can't estimate the CRF that the second pass will arrive at

The first pass alone can't estimate that.

Quote
while at the same time it tries to approximate CRF by using the information from the first pass to decide on a constant quality factor.

The second pass does that, based on information collected during the first pass.