constant frame rate/CFR flagging in h265 bitstreams

General questions or discussion about HandBrake, Video and/or audio transcoding, trends etc.
Post Reply
xaeiou
Posts: 3
Joined: Sun Jun 24, 2018 8:08 am

constant frame rate/CFR flagging in h265 bitstreams

Post by xaeiou »

Hi,

If a video source has a constant frame rate, and in the Handbrake GUI I select "constant frame rate" and "same as source", when encoding I believe Handbrake sets the h264 stream's "fixed_frame_rate_flag" to 1/true. This gives a better indication of the original source than mediainfo which, for 24fps in particular, is confused by small jitter due to the 90khz timebase and reports "variable" frame rate for the h264 stream.

Hopefully my understanding is correct...at least it matches my observations/testing. Trust the flag, not the mediainfo estimate :wink:

I'm confused about what happens with h265 as it doesn't have the "fixed_frame_rate_flag" as far as I can tell. I was trying to understand from the h265 standard which has confusing stuff about "tick rate" and "poc_proportional_to_timing_flag" being used to indicate constant frame rate. But it's not clear to me how it should be used.

In Handbrake, is there a flag, or combination of flags, set in h265 streams and used equivalently to the "fixed_frame_rate_flag" in a h264 stream? I'd like to go in a take a look just to make sure I understand at least a little about what's going on :D

Thanks for any advice.
User avatar
BradleyS
Moderator
Posts: 1860
Joined: Thu Aug 09, 2007 12:16 pm

Re: constant frame rate/CFR flagging in h265 bitstreams

Post by BradleyS »

@jstebbins would know.
xaeiou
Posts: 3
Joined: Sun Jun 24, 2018 8:08 am

Re: constant frame rate/CFR flagging in h265 bitstreams

Post by xaeiou »

Thanks Bradley. This isn't an urgent issue or causing any problems of course, just something of interest. And with the regular posts about "why is my bitrate variable according to mediainfo?" I thought it would be useful to also know flagging behavior in the case of h265.

John is very busy...if he sheds some light on this at some point that would great, but in the meantime I'll keep doing some detective work looking at the output of the bitstream parser and see if I can see a consistent pattern of behavior in Handbrake's output with various inputs :)

EDIT: Ah I notice there is a source tree available! I don't know why but, somehow I was under the misconception source was no longer released for HB. I'll set it up and take a look at that as well, the exercise will do me good :D

EDIT2: Grabbed the HB source and the only relevant looking references I could find to the h265 flags were actually in the ffmpeg tree, so it seems my question is answered within libavcodec/libavformat rather than HB-specific source. I'll report back on further findings...
Deleted User 11865

Re: constant frame rate/CFR flagging in h265 bitstreams

Post by Deleted User 11865 »

More likely in libx265 ;)

The real, definitive answer would be there: https://www.itu.int/rec/T-REC-H.265
User avatar
JohnAStebbins
HandBrake Team
Posts: 5712
Joined: Sat Feb 09, 2008 7:21 pm

Re: constant frame rate/CFR flagging in h265 bitstreams

Post by JohnAStebbins »

I haven't read the H.265 spec to see what it supports. But it wouldn't surprise me if it does not have a specific flag to indicate VFR vs CFR. It's not strictly necessary. In H.264 what the flag really indicates is the meaning of the timebase. When set to CFR, the timebase is the framerate. When set to VFR the timebase is the minimum increment that the the framerate can vary by. So for CFR, HandBrake sets this timebase to the framerate and for VFR HandBrake sets this timebase to 1 / 90000 (one clock tick). HandBrake's H.264 VFR output used to cause problems on a number of players that didn't implement the spec correctly. They interpreted the timebase to always mean framerate and they pretty much ignored timestamps, so they would play back the video stupid fast.

Anyway, back to H.265 and x265. There's really no technical reason to have such a CFR/VFR flag. The encoder does all the work regarding CFR vs VFR. The decoder doesn't care. For many stream types (e.g. MPEG-2 transport and program streams) the only way to determine if they are CFR or VFR is to inspect every single frame's duration.

x265 doesn't actually implement VFR rate control yet. So if you have a source that actually varies framerate a lot HandBrake's H.265 VFR output may not conform exactly to the set H.265 level and profile or VBV settings. But truly VFR sources are rare. Most sources that are "VFR" are actually mostly CFR with brief segments that are some other framerate. Examples:
  • Scene transition cut points on DVD where the framerate will go from 24 fps to 30 fps and back to 24 fps. The 30 fps part is only a single frame long.
  • TV broadcasts where a movie is broadcast at 24 fps, but commercials are at 30 fps
User avatar
JohnAStebbins
HandBrake Team
Posts: 5712
Joined: Sat Feb 09, 2008 7:21 pm

Re: constant frame rate/CFR flagging in h265 bitstreams

Post by JohnAStebbins »

I forgot to mention the other factor that trips up mediainfo and other such apps frequently. Every framerate can't be represented perfectly by the clock HandBrake uses internally. HandBrake uses the standard 90khz clock used by all transport streams and program streams. This is the same clock used by all broadcast TV, Blurays (including UHD), and DVDs. The number of ticks of this clock necessary to represent the duration of a frame is not always an integer. For video with such a framerate, the duration of each frame will jitter by 1 tick in order to maintain the correct average framerate. All the standards that use this 90khz clock do the same thing, so it's very common but for some reason not handled well by these apps.
xaeiou
Posts: 3
Joined: Sun Jun 24, 2018 8:08 am

Re: constant frame rate/CFR flagging in h265 bitstreams

Post by xaeiou »

Thanks for taking the time to answer, I really appreciate it. At the end of the day, as you say, it's irrelevant other than some clue as to what was passed to the encoder...which really shouldn't matter to the player, only to the curious :wink:
More likely in libx265 ;)
hehehe yeah I realise that now - this was my first time looking at the code and I just grepped the final source tree within handbrake without knowing how all the dependencies fit together. I still don't...but I know more than before. Which just makes me all the more appreciative of the app and all the work you folks put in :)
User avatar
BradleyS
Moderator
Posts: 1860
Joined: Thu Aug 09, 2007 12:16 pm

Re: constant frame rate/CFR flagging in h265 bitstreams

Post by BradleyS »

Mindsplosion
Post Reply