PDA

View Full Version : CRF Encoding mystery


meshaun
12th January 2012, 01:26
I was roaming around the web searching for knol on the x264 CRF encoding and most cases said that it's better to use it. I tried to encode a BluRay of 25GB with the x264 CRF 20, and got a video file size of 2.95 GiB (93%) that's 4 144 Kbps of video bitrate.
The thing is, this movie is having a no action and all. Just a drama movie.

The encoding was only 4Hours. If I tried the 2 pass encoding, then sometimes I go on with 8 hours or so long video transcoding time. Is this an advantage of CRF or some problem that we can't be sure of? I mean, what about a high action movie encode? Will it take in the max quality with the bluray to CRF20 conversion or what?

LoRd_MuldeR
12th January 2012, 02:26
First of all: The difference between a "2-Pass" encode and a "CRF" encode of the same average bitrate (i.e. same final file size) is negligible. So none of the two is "better" per se! The biggest difference therefore is that with 2-Pass mode you can know the resulting (average) bitrate beforehand, while with CRF mode you can not know the resulting bitrate beforehand. However, with 2-Pass mode it will be difficult to predict which (average) bitrate will be required (at least) to hit the desired level of quality for a particular source. At the same time CRF mode hits a specific level of quality (roughly!) and uses "as many bits as needed" for the individual source. So once you have found out the (highest possible) CRF value that satisfies your personal quality needs, you can simply run all your future encodes with that CRF value - instead of having to re-adjust the target bitrate for every source again and again. In the special case, where you must hit a specific file size (i.e. specific average bitrate) and want to retain the best quality for that predetermined file size, 2-Pass mode is the way to go. For all the rest, I would prefer CRF mode, for the reasons mentioned before.

About encoding time: I think the second pass of a 2-Pass encode and a CRF encode should run at the same speed, as long as you don't change other "speed -vs- quality" settings. If you see a significant difference, you probably changed other settings as well, not just the RC mode! The 2-Pass encode, however, will need an additional first pass, which CRF mode doesn't need (obviously). That's another advantage of CRF mode. Though the first pass of a 2-Pass encode will run much faster than the second pass, as x264 uses "turbo" settings for the first one. This means that 2-Pass won't need twice the time, compared to CRF mode. But 2-Pass will take more time than CRF for sure...

See also:
http://git.videolan.org/gitweb.cgi?p=x264.git;a=blob_plain;f=doc/ratecontrol.txt;h=e93ced2aa374a4a36fa1f34121358d943a28f6bc;hb=HEAD

sneaker_ger
12th January 2012, 09:00
About encoding time: I think the second pass of a 2-Pass encode and a CRF encode should run at the same speed, as long as you don't change other "speed -vs- quality" settings.

No, frame decision is done on the first pass only for example.

Ghitulescu
12th January 2012, 09:23
It's no mystery at all.

Two-passes, as Lord_Mulder said, is for people having a bit budget (ie they have to fill a target size, usually a BDR) because two-passes allows a better distribution of the available bits over the places where they are really needed. CRF mode is for people caring less about size (or having plenty of space, like a 60 min home video for a whole BDR) but valuing the quality and duration.

People looking for best quality at smallest size should never consider CRF :)

Audionut
12th January 2012, 09:31
CRF mode is for people caring less about size (or having plenty of space, like a 60 min home video for a whole BDR) but valuing the quality and duration.

People looking for best quality at smallest size should never consider CRF :)

This is wrong.

2 pass guarantees to hit the bitrate requested, while CRF will hit the quality requested (to a certain extent).

But both ways of encoding will produce the same quality at the same bitrate.

Encode 2 pass at 2000kbps, then create another encode in CRF mode, at the required value to produce the same bitrate as the 2000kbps encode and they will both be the same quality.

This holds true whether you perform this at 2000kbps I used above, or any other bitrate, low or high.

Ghitulescu
12th January 2012, 09:35
You're partially right but you misunderstood what I said.

In CRF I don't know the average bitrate beforehand, only afterwards :) which is futile should the movie be 5 MB longer than the BDR which means I have to reencode it again (another lost day).

Audionut
12th January 2012, 09:38
You're partially right but you misunderstood what I said.

No, what I said is correct, and you need to explain better, because this quote below is just plain wrong however you meant it.

People looking for best quality at smallest size should never consider CRF

Now to continue.

In CRF I don't know the average bitrate beforehand, only afterwards :) which is futile should the movie be 5 MB longer than the BDR which means I have to reencode it again (another lost day).

This has nothing to do with the "quality" of CRF mode. Want to hit a specific bitrate, use 2 pass. Want to hit a specific "quality", use CRF.

nibus
12th January 2012, 10:22
CRF is nice because to an extent it takes away the guessing from picking an appropriate bitrate for your source. Run some test encodes and find a quality value that is transparent to your eyes, then use it. :) It's also nice because it only takes one pass, with the downside of not knowing the final output size.

Ghitulescu
12th January 2012, 10:22
This has nothing to do with the "quality" of CRF mode. Want to hit a specific bitrate, use 2 pass. Want to hit a specific "quality", use CRF.

Two-passes, as Lord_Mulder said, is for people having a bit budget (ie they have to fill a target size, usually a BDR) because two-passes allows a better distribution of the available bits over the places where they are really needed. CRF mode is for people caring less about size (or having plenty of space, like a 60 min home video for a whole BDR) but valuing the quality and duration

Are you sure you didn't miss anything?

Some definitions may help you:
CRF in a nutshell

The way constant quality encoding is usually done, it keeps up a constant quality by compressing every frame of the same type the same amount. In tech speak, that's maintaining a constant QP (quantization parameter).

Constant Rate Factor, on the other hand, will compress different frames by different amounts. It does this by taking motion into account.

The eye perceives more detail in still objects than when they're in motion. Because of this, a video compressor can apply more compression (drop more detail) when things are moving, and apply less compression (retain more detail) when things are still. Subjectively, the video will seem to have higher quality.
Bit budgeting provides a target video data rate for the project.
Both quotes from wide used software programs, to avoid any accusations of biasing.

I'm not saying CRF is providing automatically the best quality, it only lays out the speedier path to achieve it, should the bitrate be non-important or secondary. Since the bits needed to encode the frames in CRF mode do not depend on the average bitrate, one cannot estimate the final size. In two-passes one can impose a final size by setting the average bitrate, and this size is within technical tolerances reached.

hello_hello
12th January 2012, 10:25
Two-passes, as Lord_Mulder said, is for people having a bit budget (ie they have to fill a target size, usually a BDR) because two-passes allows a better distribution of the available bits over the places where they are really needed. CRF mode is for people caring less about size (or having plenty of space, like a 60 min home video for a whole BDR) but valuing the quality and duration.

People looking for best quality at smallest size should never consider CRF :)

The last statement on it's own doesn't even make sense given "best quality" and "smallest size" are generally mutually exclusive.
It might be more accurate to say that people looking for a particular size regardless of quality should never use CRF.

Saying "two-passes allows a better distribution of the available bits over the places where they are really needed" is misleading because it implies CRF encoding doesn't distribute more bits where they're needed the same way.


In CRF I don't know the average bitrate beforehand, only afterwards :) which is futile should the movie be 5 MB longer than the BDR which means I have to reencode it again (another lost day).

Likewise with 2 pass encoding you know the average bitrate beforehand but have no idea as to the final quality, which is futile should the encode have too many compression artifacts which means you have to pick a larger file size and re-encode it again (another lost couple of minutes setting up a fresh encode).
Not to mention the potential for wasting bits on quality you can't see by choosing too large a file size.
Do people still often tailor their encodes to disc sizes rather than quality? I thought the 700mb AVI days were over. ;)


For meshaun, to help alleviate any introduced confusion....

2 Pass encoding lets you pick the file size. Final quality is unknown.
CRF encoding lets you pick the quality. Final file size is unknown.

As you may discover from the example of your encode in your original post, final size for a given quality can be very unpredictable. 2.9GB is around the average file size I get when resizing down to 720p and using a CRF value of 20, so if you encoded at 1080p I guess the video in question was very easy to compress. The next one might be 9GB.
For a given file size or bitrate, CRF encoding and 2 pass encoding produce almost identical quality so it's simply a matter of deciding before you encode whether to pick the quality (CRF encoding) or whether to pick the file size (2 pass). Most people probably use the former these days.

meshaun
12th January 2012, 10:29
Thanks for the replies guys. The thing I want to know is, is CRF 20 some good value? will it automatically give the quality for the action movies and higher bitrates and also lower bitrates for the drama type movies?

hello_hello
12th January 2012, 10:45
Are you sure you didn't miss anything?

LOL! You quoted yourself. Lord_Mulder said nothing about two-passes allowing a better distribution of the available bits over the places where they are really needed. That's something you made up and your own misunderstanding is achieving nothing but to add confusion to the thread.
What Lord_Mulder actually said was:
"The difference between a "2-Pass" encode and a "CRF" encode of the same average bitrate (i.e. same final file size) is negligible. So none of the two is "better" per se!"

Both quotes from wide used software programs, to avoid any accusations of biasing.

No doubt. I'm sure it explains CRF encoding fairly well, as opposed to the old single pass method of encoding: "constant quantizer" or "target quantizer".

How does that relate to a CRF v 2 pass discussion? Doesn't 2 pass encoding compress frames by different amounts just like constant rate factor encoding?

I'm not saying CRF is providing automatically the best quality, it only lays out the speedier path to achieve it, should the bitrate be non-important or secondary. Since the bits needed to encode the frames in CRF mode do not depend on the average bitrate, one cannot estimate the final size. In two-passes one can impose a final size by setting the average bitrate, and this size is within technical tolerances reached.

Which doesn't explain your previous claim that "two-passes allows a better distribution of the available bits", because CRF encoding isn't limited to "available bits".
How can 2 pass encoding distribute available bits better than an encoding method which isn't confined to "available bits"? Edit: I should add.... when all else is equal or as Lord_Mulder said, when the difference between the two is negligible.

hello_hello
12th January 2012, 10:58
Thanks for the replies guys. The thing I want to know is, is CRF 20 some good value? will it automatically give the quality for the action movies and higher bitrates and also lower bitrates for the drama type movies?

I use CRF 20 for all my 720p encodes regardless of the type of movie. The file sizes range from around 2GB to 5GB depending on the length of the movie and how hard it is to compress. The amount of action in a movie is obviously one of those factors, but a given CRF value should give you basically the same quality relative to the original whether it be a love story or an action movie.

CRF 18 is supposed to be around where an encode should look transparent. The higher the CRF value, the lower the quality. Personally I can't pick the difference between CRF 18 and CRF 20 for 720p encodes so I go with CRF 20. Which value you use is up to to. Once you've picked a CRF value you're happy with, you should be able to use it for everything. File sizes will vary accordingly.

PS I have read in a few places that CRF values are a little resolution dependent. ie 1080p can get away with slightly higher CRF values than 720p which can get away with slightly higher CRF values than 480p etc.

I haven't tested it thoroughly but based partly on average file size as well, I use CRF 20 for 720p and CRF 19 for DVD encodes. I haven't done much experimenting with 1080p encodes, but each to their own anyway....

meshaun
12th January 2012, 11:33
hello_hello,
Thanks for the reply mate. I shall try encoding more on the CRF20 then. yes I tried a 1080p to a 720p.

Keiyakusha
12th January 2012, 12:14
In this thread there was a quote about CRF:
The eye perceives more detail in still objects than when they're in motion. Because of this, a video compressor can apply more compression (drop more detail) when things are moving, and apply less compression (retain more detail) when things are still. Subjectively, the video will seem to have higher quality.
By the way, this is interesting thing. I never did any tests about this, but it feels that for me this method is not always good.
For example when I encode animation I have one minimum requirement to my encode - all still parts should look perfect. And I always pick needed settings for this. In this case I want still frames to drop more details to make moving parts better (if encoder thinks this is really needed to achieve constant quality). And if because of that still parts won't be perfect anymore, I'll just rise overall bitrate/quality value.

hello_hello
12th January 2012, 12:42
I don't get it. If moving parts don't seem to have a high enough quality, why not just raise the overall bitrate/quality now anyway?

Do you use "--tune animation" when encoding? Not that I have the slightest idea whether it'd have an effect on the amount of compression used for still vs moving objects as I rarely encode animation.

Keiyakusha
12th January 2012, 12:47
I don't get it. If moving parts don't seem to have a high enough quality, why not just raise the overall bitrate/quality now anyway?
If i'll do so, moving parts will look good but still parts will eat like twice more bitrate than needed to look perfect. Especially when I use debanding, and I use it almost always. I don't really care if some of the debanding will be lost, it won't have big impact on visual quality but encoder can't know it is good or bad. It sees some details are going to be lost and gives them more bits. Bit not only banding. So called "texture" or gradient which used to fill some parts of the defined shape plays very similar same role.

Do you use "--tune animation" when encoding? Not that I have the slightest idea whether it'd have an effect on the amount of compression used for still vs moving objects as I rarely encode animation.
I do, but if i'll use something different, I don't think it will change how things work.

sneaker_ger
12th January 2012, 12:53
In this thread there was a quote about CRF:

By the way, this is interesting thing. I never did any tests about this, but it feels that for me this method is not always good.
For example when I encode animation I have one minimum requirement to my encode - all still parts should look perfect. And I always pick needed settings for this. In this case I want still frames to drop more details to make moving parts better (if encoder thinks this is really needed to achieve constant quality). And if because of that still parts won't be perfect anymore, I'll just rise overall bitrate/quality value.

So you want exactly what it already does: decreased quality in moving parts compared to stills? :confused:

Keiyakusha
12th January 2012, 13:07
So you want exactly what it already does: decreased quality in moving parts compared to stills? :confused:

No, I want it backwards. ^__^
If i'll use moving parts to decide on quality/bitrate setting then i'll end up with oversized output cause it rises bitrate in still parts but this is not needed.
I'm sorry I can't explain it better, its hard for me to write in English.

nm
12th January 2012, 13:09
If i'll do so, moving parts will look good but still parts will eat like twice more bitrate than needed to look perfect.

You can raise qcomp to increase quality in moving parts compared to low-complexity parts.

But how exactly are you watching the video? Do the moving parts look bad at normal playback speed?

I recommend reading this blog post too: http://x264dev.multimedia.cx/archives/98

Keiyakusha
12th January 2012, 13:21
You can raise qcomp to increase quality in moving parts compared to low-complexity parts.

I recommend reading this blog post too: http://x264dev.multimedia.cx/archives/98

Thanks, I'll read it later when I'll be able to do some encoding. I'll also try qcomp.

But how exactly are you watching the video? Do the moving parts look bad at normal playback speed?
It is hard to answer your question. Usually I encode animation in the way I can't see the differences in any of the parts. (believe it or not, I don't have time to watch live action so I usually watch animation only.) I'm only concerned about output size. I think it can be lowered without letting encoder to give more bits to still parts. Because with my, relatively high bitrate to begin with, details that are receiving bitrate by using this "CRF's method" are not really needed. These are some things I can throw away. There are probably no such things in live action movies.

sneaker_ger
12th January 2012, 13:25
No, I want it backwards. ^__^
If i'll use moving parts to decide on quality/bitrate setting then i'll end up with oversized output cause it rises bitrate in still parts but this is not needed.
I'm sorry I can't explain it better, its hard for me to write in English.

I kinda get the idea of what you are trying to say, but your preferences do in no way contradict the basic principle you originally quoted, "dropping more detail on moving objects compared to stills is possible while maintaining a constant perceived quality" that is.
The only thing contradicting is your post itself. First you say "stills should be perfect", then "stills should drop more details".

hello_hello
12th January 2012, 13:26
If i'll do so, moving parts will look good but still parts will eat like twice more bitrate than needed to look perfect.

How do you come to that conclusion?
As I said, I don't encode animation much, but for normal video however x264 decides to distribute the bits, it seems to do a fairly good job to me. If movement shows some compression artifacts or quality loss then generally so too will still objects to a similar degree. Overall quality is usually fairly even between the two.

Especially when I use debanding, and I use it almost always. I don't really care if some of the debanding will be lost, it won't have big impact on visual quality but encoder can't know it is good or bad. It sees some details are going to be lost and gives them more bits. Bit not only banding. So called "texture" or gradient which used to fill some parts of the defined shape plays very similar same role.

What you're saying still doesn't make sense to me. You seem to be saying you don't care if some of the debanding will be lost or some texture or gradients may not be reproduced exactly if the quality isn't high enough, but how does increasing the quality so all of the above is reproduced faithfully eat twice the bitrate needed if it doesn't look perfect without it?
It seems to be inline with what I said earlier. If motion shows imperfections, generally so too will still objects and visa versa.

Obviously you seem to be saying you'd prefer the encoder to give slightly less weight to still motion and more to movement in terms of quality. Maybe animation doesn't encode the same as film, but I'm not sure too many people would agree with you.

Keiyakusha
12th January 2012, 13:34
How do you come to that conclusion?

Observations. As I said I never did any tests to check exactly this behavior. So this may be not really "2 times". Don't take all my words literally.

The only thing contradicting is your post itself. First you say "stills should be perfect", then "stills should drop more details".

Well, I mean stills should be perfect in a final result. I use them to decide which settings to choose. But I want moving parts to look good too. That's where "stills should drop more details" comes from.


Thank you for answers guys. I'll try things you suggested. I feel like from this point it becomes just more and more confusing to all of us so lets better end this discussion.

hello_hello
12th January 2012, 13:35
Have you tried a constant quansizer encode rather than a constant quality (CRF) encode? Maybe using the former would give you the exact result you're after. The former should (the way I understand it) compress the entire video by the same amount regardless of motion.

LoRd_MuldeR
12th January 2012, 13:41
No, frame decision is done on the first pass only for example.

Sure. But I didn't claim the opposite :)

Nor does the lack of frame-type decision in the second pass of a 2-Pass encode (usually) make a significant speed-difference to a single-pass CRF encode.

(In my quick test with 576p video and "slower" preset, I got 9.26 fps for the CRF pass and 9.45 fps for the second 2-Pass pass)

People looking for best quality at smallest size should never consider CRF

This is wrong.

2 pass guarantees to hit the bitrate requested, while CRF will hit the quality requested (to a certain extent).

But both ways of encoding will produce the same quality at the same bitrate.

Indeed. As said before, neither CRF nor 2-Pass is "better" per se. But they cover different scenarios.

One should simply use the mode that is most suitable for what he/she want to achieve. And for most things that probably will be CRF.

The thing I want to know is, is CRF 20 some good value? will it automatically give the quality for the action movies and higher bitrates and also lower bitrates for the drama type movies?

As explained before, the same CRF value (e.g. 20) will retain the same level of quality (roughly!) for different sources. And it will use "as many bits as needed" for each source.

So yes, it should do what you want. And CRF=20 sounds like a reasonable value to start. Though I would recommend to do some tests yourself.

Maybe you can get away with an even higher value. It all depends on your personal quality requirements!

However be aware that the "same CRF value -> same quality (roughly)" rule only applies as long as you don't change other settings. You may have to rescale your CRF value after changing other settings.

Also the final size will depend on the "complexity" of a movie, not so much on the genre. Even the "drama" movie can be very "complex", e.g. when there is a lot of noise/grain.

sneaker_ger
12th January 2012, 13:58
Sure. But I didn't claim the opposite :)

Nor does the lack of frame-type decision in the second pass of a 2-Pass encode make a significant speed-difference to a single-pass CRF encode.

You didn't limit it to significant differences in your first post. ;)

But it's setting dependent, of course. If you're bottlenecked by b-adapt 2, the difference could be larger. I was kinda inclined to do a short test myself and it (1080p, preset medium, CRF 18) came out at 11.14 fps vs 12.11, which equates to 8.7% and isn't that insignificant, I'd say.

meshaun
13th January 2012, 00:41
Hi guys. was encoding @ CRF20 and the 1st encode came cool. The second encode was,6.03 GiB.... Thats huge.
the movie is Black hawk Down . seems absurb

LoRd_MuldeR
13th January 2012, 01:11
Hi guys. was encoding @ CRF20 and the 1st encode came cool. The second encode was,6.03 GiB.... Thats huge.
the movie is Black hawk Down . seems absurb

In CRF mode it's expected that different sources can come out at a different file size (i.e. a different average bitrate) for the same CRF value.

And I think this thread has already explained why that is...

Sapo84
13th January 2012, 01:23
If i'll do so, moving parts will look good but still parts will eat like twice more bitrate than needed to look perfect. Especially when I use debanding, and I use it almost always. I don't really care if some of the debanding will be lost, it won't have big impact on visual quality but encoder can't know it is good or bad. It sees some details are going to be lost and gives them more bits. Bit not only banding. So called "texture" or gradient which used to fill some parts of the defined shape plays very similar same role.
Well, you should definitely try changing the default settings.
Mb-tree is usually too strong, and really degrades fast moving parts (visible blocking even at crf18 and 50% less bitrate than no-mbtree, I have around two screens I made some time ago, this (http://img847.imageshack.us/img847/1515/twin666enc0.png) is crf 18.2 10bit encoding, this (http://img151.imageshack.us/img151/6360/twin666enc3.png) is with no-mbtree), qcomp between 0.7 and 0.8 usually helps.
BugMaster's AutoVAQ (aq-mode 3) also helps retaining details on textures/gradients in dark scenes (well, aq helps in general).
Using zones for the more problematic parts is the last solution but it usually works wonders when the source just doesn't turn out good at reasonable settings (at least with 8bit, 10 is a lot safer).
--tune animation is not that good (for anime) if you actually want perfect or near perfect quality, because if that is the case you actually want to retain a bit more grain.
Increasing psy-rd (~0.8 is usually enough) and, maybe, psy-trellis (a tiny bit if the source is good) actually helps a fair bit.

sneaker_ger
13th January 2012, 08:48
Less quality at 50% the bitrate, who would have expected that. :rolleyes:

Sapo84
13th January 2012, 14:16
Thanks for unneeded sarcasm.
If you have some insight on why mbtree has such an effect on bitrate and quality you can just say it instead on using sarcastic oneliners.

sneaker_ger
13th January 2012, 14:36
If you have some insight on why mbtree has such an effect on bitrate and quality you can just say it instead on using sarcastic oneliners.

"bitrate and quality"? You're saying it like that would be unexpected behavior. Constant CRF does not guarantee constant quality for different settings - that goes for the majority of x264's settings, not just mbtree. Though admittedly mbtree does change the way x264 measures quality IIRC.

What I'm saying is: your comparison method is seriously flawed. Compare the quality at the same bitrate (2pass) instead, if you want to argue about optimizing the settings.

Asmodian
16th January 2012, 21:40
"bitrate and quality"? You're saying it like that would be unexpected behavior. Constant CRF does not guarantee constant quality for different settings - that goes for the majority of x264's settings, not just mbtree. Though admittedly mbtree does change the way x264 measures quality IIRC.

What I'm saying is: your comparison method is seriously flawed. Compare the quality at the same bitrate (2pass) instead, if you want to argue about optimizing the settings.

Another way to say this; it would be better to drop the crf value to get the same bitrate instead of messing with mbtree.