Skip to content
Search

Group of pictures

lastmod 2024-05-24created 2016-11-164 edits

In video coding, a group of pictures, or GOP structure, specifies the order in which intra- and inter-frames are arranged. The GOP is a group of successive pictures within a coded video stream. Each coded video stream consists of successive GOPs. From the pictures contained in it, the visible frames are generated. Encountering a new GOP in a compressed video stream ensures the decoder that no previous frame will be needed to decode the next ones, and is what allows fast seeking through the video.

About

The most basic concept of MPEG-1/2 encoding: the collection of frames from one key frame up to the next key frame. There are three frame types, I, P, and B, and the characteristics of each are as follows.

Mpeg-gop.jpg

Intra coded frames (I‑frames; Key frames)

  • Also known as key frames.
  • A full picture compressed directly from the source in the same way as JPEG. Good quality, but also large in size.
  • Encoded independently without referencing other frames.
  • Can appear at any position in the data stream, is used for random access to the data, and is coded without reference to other images.

Predicted frames (P-frames)

  • Encoded using motion compensation with reference to the previous I-frame and previous P-frame information. (Forward prediction)
  • Based on the observation that across consecutive images the whole image does not change; rather, blocks of the image move sideways. In other words, when there is motion, objects in the previous frame mostly move sideways without much change in their shape, so the very small difference between the previous frame and the current frame is exploited and only the difference values are coded.

Bi-directional predicted frames (B-frames)

  • Predicted using both the preceding and following I-frames and P-frames. (Bidirectional prediction)
  • A B-frame holds the difference values between the preceding I-frame or P-frame and the I-frame or P-frame after the B-frame.
  • Has a high compression ratio.

Frame

There are three frame types, I, P, and B, and the characteristics of each are as follows.

I Frame (Infra Frame)
Simply put, this is the key frame. It is a full picture compressed directly from the source in a JPEG-like way. It has the best quality, but also the largest size.
P Frame (Previous; Predicted Frame)
A frame built from the information of the preceding key frame. Both quality and size are mid-range.
B Frame (Bidirectional Frame)
A frame built from the information of the I/P frames before and after it. Both quality and size are the lowest.

Their order is roughly I B B P B B P B B P B B I B B P ..., with P frames placed between I frames and B frames placed between I/P frames. Everything from one I frame up to just before the next I frame is grouped as one GOP, and the GOP size is the number of those frames. So in the case above, the GOP size is 12. MPEG encoders usually configure this by setting the number of P frames and the number of B frames: the number of P frames is how many P frames go into a GOP, and the number of B frames is how many go between I/P frames. So in the case above, the number of P frames is 3 and the number of B frames is 2.

Options

There are two options related to GOP: a technique called Scene Detection and something called Closed GOP.

Scene Detection

Found in nearly every codec these days, it detects scene changes and inserts an I frame (a key frame in the case of avi) there. Since the other frames depend on other frames, the bigger the change between those frames and the current frame, the more the quality drops. This is also why lots of blocky artifacts appear in fast-moving scenes. But if you insert a key frame at every scene change, the size grows a bit, but the quality can improve a lot.

lClosed GOP

A method that drops the B frames before the next I frame in the GOP structure. So

  • instead of I B B P B B P B B P B B I B B P ...,
  • it becomes I B B P B B P B B P I B B P ....

There are several reasons for doing this, but

The first reason for doing this is to make MPEG editing easier. When cutting a video, unless you re-encode, the first frame must be an I frame, that is, a key frame. Other frames use information from the frames before them, and since there is no frame before the first frame, such dependent frames cannot be placed there. Also, in MPEG-1/2, the last frame must be an I/P frame. For the same reason, B frames use frames that come after them, and since there are no frames after, they cannot be placed there. With the usual GOP above, however, a few frames are lost wherever you cut the video.

For example, in I B B P B B P B B P B B // I B B P ..., if you cut at the // position, the latter part is fine because its first frame is an I frame, but in the former part the last frames, shown in red, are B frames, so these two B frames are lost. Of course, some programs do partial re-encoding, and in that case they recompress that part and turn it into suitable frames, but then that one GOP (you cannot re-encode just one frame; the whole GOP containing it must be re-encoded) suffers a sharp drop in quality. When simply cutting and joining, this kind of problem is hardly welcome.

But with Closed GOP it becomes I B B P B B P B B P // I B B P ..., so cutting at // causes no problem at all. That makes cutting and joining easier.

The second reason is that in I B B P B B P B B P B B (I) B B P ..., if the scene changes at (I), the two B frames shown in red right before it use the information of the preceding P frame shown in blue and the I frame of the new scene. In that case they use information from two completely different frames, so the quality drops severely. With Closed GOP this problem disappears, so you can expect a slight improvement in quality.

For reference, with the earlier Dazzle DVC 2, those two B frames broke up completely in such situations. I don't know whether that has been fixed these days. Using this together with Scene Detection above might give even better quality. Oh, and of course, using Closed GOP will increase the overall size somewhat. (For reference, you can also put something called a Sequence Header in front of a GOP. Playback works fine without it, but if you don't put it in every GOP, a DVD made from it may freeze right after playback starts. So if you are making a DVD, be sure to set the Sequence Header interval to 1.)

ETC

There is no exact answer to which setting gives the best quality. The faster the motion, the smaller the GOP should be, and the slower the motion, the larger the GOP should be. Even though B frames have the lowest quality, if there is so little motion that they practically copy the previous frame as is, you get free quality at minimal size. In that case the overall size shrinks, so you can use higher settings and raise the overall quality further. Conversely, if you use only I frames because they have good quality, they take up a lot of space despite the good quality, so to fit the desired size the settings have to drop drastically, and as a result the overall quality falls drastically. On the other hand, no part will get especially worse no matter how much motion there is. To put it simply, when fitting a video into a fixed size, many B frames improve the overall quality but degrade it during sudden motion, while many I/P frames don't particularly degrade quality during sudden motion but lower the overall quality considerably. So the key to small size and high quality is how well you combine these three, not picking only the ones said to have good quality. If you really want higher quality, using Scene Detection and Closed GOP together should give a better result.

See also

Favorite site

author Zer0 sitemap