Open and Closed GOPs – All You Need to Know

This article defines open and closed GOPs, identifies why closed GOPs are better, and details how to produce closed GOPs in FFmpeg with x264 and x265. The level of testing and analysis detailed here is consistent with the instruction in my book, Learn to Produce Videos with FFmpeg In 30 Minutes or Less ($34.95), and my course, FFmpeg for Adaptive Bitrate Production ($29.95)  (which includes a PDF copy of the book). Don’t just learn FFmpeg; become an expert in video compression. 

Encoding files with Open-GOPs is supposed to produce better quality than closed GOPs because B and P frames can reference frames located outside the GOP. However, files produced for ABR streaming should have closed GOPs. This paper explores the defaults for x264 and x265, the quality difference between closed and open GOPs, and the RADL frame, which you may want to insert into x265 encodes to reduce I-frame strobing.

TL/DR

1. You should use closed GOPs when encoding for ABR delivery. This is recommended by Apple for HLS and appears to be what’s called for with DASH.

2. By default, x264 produces closed GOPs, so you don’t have to do anything special with x264 to produce closed GOPs. If you’re using a different H.264 codec, check its default setting.

3. By default, x265 produces open GOPs. To produce a closed GOP, add open-gop=0 to your x265 FFmpeg command string.

4. With x265, the overall quality delta between open and closed GOP is minuscule, so don’t lose any sleep over this decision.

5. According to Ben Waggoner, adding RADL frames to a closed GOP file encoded with x265 may decrease strobing. If strobing I-frames is a problem for you, try this technique.

Defining Open and Closed GOPs

Open and closed GOPs relate to the concept of an IDR-Frame, defined here:

An IDR frame is a special type of I-frame in H.264. An IDR frame specifies that no frame after the IDR frame can reference any frame before it. This makes seeking the H.264 file easier and more responsive to the player. The IDR frames are introduced to avoid any distortions in the video when you want to skip/forward to someplace in the video or start watching in the middle of the video. In short, every IDR frame is an I-frame, but not vice versa; so, there can be I-frames that aren’t IDR frames.

Open GOPs (group of pictures) are GOPs where frames can refer to frames in other GOPs for redundant blocks. You see this in Figure 1 where the last two B-frames refer to the I-frame in the next GOP for redundancies.

Figure 1. An Open GOP has frames that refer to frames outside the GOP for redundancies.

Closed GOPs are GOPs where frames can only refer to frames within the GOP for redundant blocks. You see in Figure 2 that the second GOP starts with an IDR frame, so frames within that GOP can’t refer backward to frames before that I-frame. If the next I-frame is also an IDR frame, this would be a closed GOP.

Figure 2. A closed GOP can’t refer to frames outside the GOP for redundancies (both images courtesy (Tiliam Research).

If all I-frames are IDR frames, all GOPs are closed. If all frames are not IDR frames, some GOPs will be open.

ABR Videos Should Use Closed GOPS

With adaptive bitrate video, the player switches streams depending upon bandwidth conditions and other factors. With an open GOP, when the player switches from a 360p stream to a 1080p stream, the frames before the I-frame at the start of the segment are different than the frames referred to while encoding, which can cause problems.

For HLS, the Apple HLS Authoring Specification reads, “Key frames (IDRs) SHOULD be present every two seconds.” So, all keyframes should be IDR frames, which means all GOPs are closed.

While I couldn’t find a DASH document that mandates closed GOPs, if you scan through examples of the DASH-IF documentation, you’ll notice that all segments start with a closed GOP. This makes sense given that each time the stream switches the frames in the previous GOP may have a different resolution or even frame rate.

The bottom line is that if you’re encoding for ABR video, you should use closed GOPs.

x264 GOPs are Closed by Default

With x264, GOPs are closed by default. You can tell this in MediaInfo by the open_gop=0 designation (Figure 3 on the left) vs. the open_gop=1 designation on the right. This is MediaInfo’s HTML view, by the way (View > HTML).

Figure 3. Closed GOP file on the left, open GOP on the right (click image to see in full size).

Add open-gop=1 to your command string to produce an open GOP (see article here for more details). Below are the command strings I used to create the two files above. If you don’t specify open-gop=0 in your command string, you’ll still see open_gop=0 in MediaInfo indicating that the GOP is closed. So, if you do nothing to create an open GOP, it’s closed by default.

ffmpeg -y -i Haunted_1080p.mp4 -x264-params keyint=60:min-keyint=60:scenecut=0:bframes=2:b-adapt=2:ref=2:open-gop=0 Haunted_closed2.mp4
ffmpeg -y -i Haunted_1080p.mp4 -x264-params keyint=60:min-keyint=60:scenecut=0:bframes=2:b-adapt=2:ref=2:open-gop=1  Haunted_open2.mp4

Just to prove that my tools are cooler than your tools, Figure 4 shows the open GOP file as analyzed in Zond 265. The playhead is set on frame 59, which is a B-frame (B-frame=green; I-frame=red; P-frame=blue). On the right, you see that the frame is grabbing references from three other frames, 55, 56, and 58, the last of which is the immediately preceding I-frame, which obviously isn’t an IDR. The red box on the bottom shows the frames being referenced (the faint colors beneath the frames).

Figure 4. An open GOP x264 file (click image to see in full size).

Figure 5 shows the same video encoded with a closed GOP. The selected frame is a frame 61, a P-frame and the only frame referenced is frame 60, here an I-frame. So, Frame 60 is an IDR frame and the GOP is closed.

Figure 5. A closed GOP x264 file analyzed in Zond 265 (click image to see in full size).

So, if you’re encoding with x264, all of your GOPs should be closed unless you insert a command to keep them open. If you’re encoding with another H.264 codec, check its documentation.

x265 GOPs are Open By Default

By default, x265 produces open GOPs. To produce a closed GOP, add open-gop=0to your x265 FFmpeg command string. I’ve included all x265  command strings in an Appendix below. 

Figure 6 shows a file encoded using the default settings as analyzed in Zond 265. The playhead is set on frame 360, which is a B-frame (B-frame=green;I-frame=red;P-frame=blue). On the right, you see that the frame is grabbing references from four other frames, including 359, which is the immediately preceding I-frame, and three other frames before the I-frame, which obviously isn’t an IDR. The red box on the bottom shows the frames being referenced.

Figure 6. A file encoded with open GOPs showing frames referencing frames before the I-frame that starts the new GOP (click image to see in full size).

In Figure 7 you see the same file encoded with the closed GOP flag set. The playhead is set to frame 361 which is a P-frame. The only picture it refers to is 360, the immediately preceding I-frame. So, frame 360 is an IDR frame and all GOPs are closed.

Figure 7. A file encoded with closed GOPs so frames reference frames before the I-frame starting the GOP (click image to see in full size).

The Quality Delta Between Open and Closed in Ridiculously Small

Given the apparent greater efficiency of the open GOP, you would expect a significant quality delta. As you can see in Table 1, however, which tests four 1080p60 files encoded at 3 Mbps with 2-second GOPs, the quality delta as measured by VMAF is incredibly small (I’ll explain RADL later).

Table 1. The quality delta as measured by VMAF between open and closed is insignificant.

Figure 8 shows the encoding parameters from MediaInfo. On the left we see the default open-gop flag, on the right, we see that we successfully switched to a closed GOP (click image to see in full size).

Figure 8. MediaInfo proving that we compared open vs. closed GOP files (click image to see in full size).

Ben Waggoner Says There’s More

Since this is trending towards one of the driest blog posts ever written, I’m going to include a guest appearance from celebrity compressionist Ben Waggoner, who compresses all that lovely video you see on Amazon Prime. Writing on the Doom9 forum in August 2018, Waggoner pronounced,

I use a fairly high max-keyint (sometimes 360, equals 15 or 12 seconds) and most of the time IDR is at scene cut, making open-gop not that useful. Sacrificing a bit efficiency in exchange for better compatibility is my way to go.

Open GOP is definitely more useful with shorter GOPs. It was invaluable for DVD and HD DVD, which had a 0.6 second max GOP duration (14 frames @ 24p!). If your max GOP duration is shorter than typical shot duration, that’s where Open GOP is most useful, as it reduces keyframe strobing by providing inter-GOP B-frames to blend changes.

When having to use fixed closed GOPs, the relatively new –radl feature can help a bit.

Interpreting these comments, we see:

  • There is a compatibility trade-off with open GOPs, so closed GOPs are recommended.
  • The quality delta is more pronounced in short GOPs.
  • The problem isn’t just overall quality, it’s I-frame strobing (also called pulsing).
  • Using RADL frames might help strobing

For those not up on their HEVC frame types, RADL frames are explained here as:

Leading pictures, which precede a random access point picture in output order but are coded after it in the coded video sequence. Leading pictures which are independent of pictures preceding the random access point in coding order are called Random Access Decodable Leading pictures (RADL).

Here’s what the x265 documentation tells us about RADL pictures.
Figure 9. RADL suggestions from the x265 documentation.

I added open-gop=0:radl=2 to the command string to add the RADL frames which MediaInfo confirmed. While Table 1 shows that overall quality was mostly unchanged, the point wasn’t quality, it was reducing strobing. Figure 10 shows that x265 created an extra P-frame in front of the I-frame, but I’m not 100% sure that’s a RADL frame, and I didn’t notice any difference when it came to strobing, which typically I only notice on very highly compressed files.

Figure 10. Zond 265 view of the file encoded with RADL frames.

The bottom line seems to be:

  • When encoding for ABR with x265, be sure to produce closed GOPs. If encoding with a different codec check what the default is and be sure to encode using a closed GOP.
  • Run your own tests when it comes to RADL.

Appendix I: x265 Command Strings

Open GOP

ffmpeg -y -i Football_1080p.mp4 -c:v libx265 -x265-params keyint=120:min-keyint=120:scenecut=0:ref=2:bframes=3:b-adapt=2:bitrate=3000k:vbv-maxrate=3000k:vbv-bufsize=6000k:pass=1   -f mp4 NUL && \
ffmpeg -i Football_1080p.mp4 -c:v libx265 -x265-params keyint=120:min-keyint=120:scenecut=0:ref=2:bframes=3:b-adapt=2:bitrate=3000k:vbv-maxrate=3000k:vbv-bufsize=6000k:pass=2  Football_1080p_open.mp4

Closed GOP

ffmpeg -y -i Football_1080p.mp4 -c:v libx265 -x265-params keyint=120:min-keyint=120:scenecut=0:ref=2:bframes=3:b-adapt=2:bitrate=3000k:vbv-maxrate=3000k:vbv-bufsize=6000k:pass=1:open-gop=0  -f mp4 NUL && \
ffmpeg -i Football_1080p.mp4 -c:v libx265 -x265-params keyint=120:min-keyint=120:scenecut=0:ref=2:bframes=3:b-adapt=2:bitrate=3000k:vbv-maxrate=3000k:vbv-bufsize=6000k:pass=2:open-gop=0  -an Football_1080p_closed

RADL

ffmpeg -y -i Football_1080p.mp4 -c:v libx265 -x265-params keyint=120:min-keyint=120:scenecut=0:ref=2:bframes=3:b-adapt=2:bitrate=3000k:vbv-maxrate=3000k:vbv-bufsize=6000k:pass=1:open-gop=0:radl=2  -an -f mp4 NUL && \
ffmpeg -i Football_1080p.mp4 -c:v libx265 -x265-params keyint=120:min-keyint=120:scenecut=0:ref=2:bframes=3:b-adapt=2:bitrate=3000k:vbv-maxrate=3000k:vbv-bufsize=6000k:pass=2:open-gop=0:radl=2  -an Football_1080p_closed_RADL.mp4

About Jan Ozer

Avatar photo
I help companies train new technical hires in streaming media-related positions; I also help companies optimize their codec selections and encoding stacks and evaluate new encoders and codecs. I am a contributing editor to Streaming Media Magazine, writing about codecs and encoding tools. I have written multiple authoritative books on video encoding, including Video Encoding by the Numbers: Eliminate the Guesswork from your Streaming Video (https://amzn.to/3kV6R1j) and Learn to Produce Video with FFmpeg: In Thirty Minutes or Less (https://amzn.to/3ZJih7e). I have multiple courses relating to streaming media production, all available at https://bit.ly/slc_courses. I currently work as www.netint.com as a Senior Director in Marketing.

Check Also

Mac Video Apps for Streaming Professionals

Though I work primarily on Windows computers, I also have several Macs. Here are the …

Announcing Free Course on Controlling the AMD MA35D with FFmpeg

I’m pleased to announce a new free course, MA35D & FFmpeg Quick Start: Essential Skills …

Choosing the Best Preset for Live Transcoding

When choosing a preset for VOD transcoding, it almost always makes sense to use the …

Leave a Reply

Your email address will not be published. Required fields are marked *