{"id":54312,"date":"2019-12-23T00:00:00","date_gmt":"2019-12-22T21:00:00","guid":{"rendered":"https:\/\/prohoster.info\/blog\/blog_prohoster\/kak-rabotaet-videokodek-chast-1-osnovy"},"modified":"2020-02-18T14:02:18","modified_gmt":"2020-02-18T11:02:18","slug":"kak-rabotaet-videokodek-chast-1-osnovy","status":"publish","type":"post","link":"https:\/\/prohoster.info\/en\/blog\/news\/kak-rabotaet-videokodek-chast-1-osnovy","title":{"rendered":"How a video codec works. Part 1. Fundamentals","gt_translate_keys":[{"key":"rendered","format":"text"}]},"content":{"rendered":"<h3>Part Two:<noindex><a rel=\"nofollow\" href=\"https:\/\/habr.com\/ru\/company\/edison\/blog\/480430\/\"> Principles of Video Codec Operation<\/a><\/noindex><\/h3>\n<p>Any raster <b>image<\/b> can be represented as a <b>two-dimensional matrix<\/b>. When it comes to colors, the idea can be expanded by considering the image as a <b>three-dimensional matrix<\/b>, where additional dimensions are used to store data for each of the colors.<\/p>\n<p>If we consider the final color as a combination of so-called primary colors (red, green, and blue), in our three-dimensional matrix we define three planes: the first for red, the second for green, and the last for blue.<br \/>\n<noindex><a rel=\"nofollow\" href=\"https:\/\/habr.com\/ru\/company\/edison\/blog\/481418\/\"><img decoding=\"async\" alt=\"How a video codec works. Part 1. Fundamentals\" src=\"\/wp-content\/uploads\/2019\/12\/ce37dd683a4c3ed434dae1d488646716.png\" style=\"display:block;margin: 0 auto;\" \/><\/a><\/noindex><br \/>\nWe will call each point in this matrix a pixel (element of the image). Each pixel contains information about the intensity (usually in the form of a numerical value) of each color. For example, <b>a red pixel<\/b> means that it contains 0 green, 0 blue, and maximum red. <b>A pink pixel<\/b> can be formed using a combination of three colors. Using a numerical range from 0 to 255, a pink pixel is defined as <b>Red = 255<\/b>, <b>Green = 192<\/b> and <b>Blue = 203<\/b>.<noindex><a rel=\"nofollow\" name=\"habracut\"><\/a><\/noindex><\/p>\n<blockquote><p><noindex><a rel=\"nofollow\" href=\"https:\/\/www.edsd.ru\/\" title=\"EDISON Software - web-development\"><img decoding=\"async\" alt=\"How a video codec works. Part 1. Fundamentals\" src=\"\/wp-content\/uploads\/2019\/12\/e5063b9a32248767cb0702e7aa2e7f9e.png\" style=\"display:block;margin: 0 auto;\" \/><\/a><\/noindex><br clear=\"right\"><br \/>\nThe article is published with the support of EDISON.<\/p>\n<p>We develop <noindex><a rel=\"nofollow\" href=\"https:\/\/www.edsd.ru\/ru\/portfolio\/tehnologiya\/video\">applications for video surveillance, streaming video<\/a><\/noindex>, and we also specialize in <noindex><a rel=\"nofollow\" href=\"https:\/\/www.edsd.ru\/videozapis-v-bolnichnoj-operacionnoj-s-vozmozhnostyu-obsuzhdeniya-na-forume\">video recording in the operating room<\/a><\/noindex>.<\/p><\/blockquote>\n<h3>Alternative Methods of Encoding Color Images<\/h3>\n<p>\nThere are several other models for representing the colors that make up the image. For example, one could use an indexed palette, which requires only one byte to represent each pixel, instead of the three needed when using the RGB model. In such a model, a 2D matrix can be used instead of a 3D matrix to represent each color. This saves memory but offers a smaller color range.<\/p>\n<p><img decoding=\"async\" alt=\"How a video codec works. Part 1. Fundamentals\" src=\"\/wp-content\/uploads\/2019\/12\/573a39c53527b4ec0403f019becf2d22.png\" style=\"display:block;margin: 0 auto;\" \/><br \/>\n<\/p>\n<h2>RGB<\/h2>\n<p>For example, take a look at the image below. The first face is fully colored. The others are the red, green, and blue planes (the intensity of the respective colors is shown in grayscale).<\/p>\n<p><img decoding=\"async\" alt=\"How a video codec works. Part 1. Fundamentals\" src=\"\/wp-content\/uploads\/2019\/12\/cf1f802d4381e062005490f5f9f26911.png\" style=\"display:block;margin: 0 auto;\" \/><br \/>\n<br \/>\nWe observe that the shades of red in the original will be in the same places where the brightest parts of the second face are observed. In contrast, the contribution of blue can primarily be seen only in Mario's eyes (the last face) and elements of his clothing. Note where all three color planes contribute the least (the darkest parts of the images) \u2014 this is in Mario's mustache.<\/p>\n<p>A certain number of bits is required to store the intensity of each color \u2014 this value is called <b>bit depth<\/b>. Let\u2019s assume 8 bits (ranging from 0 to 255) are spent on one color plane. Then we have a color depth of 24 bits (8 bits * 3 planes R\/G\/B).<\/p>\n<p>Another property of an image is <b>resolution<\/b>, which represents the number of pixels in one dimension. It is often denoted as <b>width \u00d7 height<\/b>, as demonstrated below in the image example of 4 by 4.<br \/>\n<img decoding=\"async\" alt=\"How a video codec works. Part 1. Fundamentals\" src=\"\/wp-content\/uploads\/2019\/12\/51a4d948e6d44e4058e346fdbaf9d6c2.png\" style=\"display:block;margin: 0 auto;\" \/><br \/>\n<br \/>\nAnother property we deal with when working with images\/videos is <b>aspect ratio<\/b>, describing the usual proportional relationship between the width and height of an image or pixel.<\/p>\n<p>When a movie or picture is said to have a size of 16 by 9, it usually refers to the <b>display aspect ratio<\/b> (<b>DAR<\/b> \u2014 from <i>Display Aspect Ratio<\/i>). However, there may sometimes be different forms of individual pixels \u2014 in this case, we refer to the <b>pixel aspect ratio<\/b> (<b>PAR<\/b> \u2014 from <i>Pixel Aspect Ratio<\/i>).<\/p>\n<p><img decoding=\"async\" alt=\"How a video codec works. Part 1. Fundamentals\" src=\"\/wp-content\/uploads\/2019\/12\/1158c6e98247a1a1084440969f9400c6.png\" style=\"display:block;margin: 0 auto;\" \/><br \/>\n<br \/>\n<img decoding=\"async\" alt=\"How a video codec works. Part 1. Fundamentals\" src=\"\/wp-content\/uploads\/2019\/12\/a610f0f77bdb245c05799d3de4147302.png\" style=\"display:block;margin: 0 auto;\" \/><\/p>\n<blockquote><p>A note for the housekeeper: <b>DVD<\/b> corresponds to <b>DAR 4 by 3<\/b><\/p>\n<p>Although the actual resolution of a DVD is 704\u00d7480, it maintains an aspect ratio of 4:3 because the Pixel Aspect Ratio (PAR) is 10:11 (704\u00d710 \/ 480\u00d711).<\/p><\/blockquote>\n<p>\nFinally, we can define <b>video<\/b> as a sequence of <b>n<\/b> frames over a period <b>time<\/b>, which can be considered an additional dimension. And <b>n<\/b> then \u2014 this is the frame rate or number of frames per second (<b>FPS<\/b> \u2014 from <i>Frames per Second<\/i>).<\/p>\n<p><img decoding=\"async\" alt=\"How a video codec works. Part 1. Fundamentals\" src=\"\/wp-content\/uploads\/2019\/12\/f940932bbd66aa361c7966f8b44ed431.png\" style=\"display:block;margin: 0 auto;\" \/><br \/>\n<br \/>\nThe number of bits per second required to display video is its <b>bitrate<\/b> \u2014 <b>bitrate = width * height * bit depth * frames per second<\/b>.<\/p>\n<blockquote><p>For example, for a video with 30 frames per second, 24 bits per pixel, and a resolution of 480\u00d7240, it will require 82,944,000 bits per second or 82,944 Mbps (30x480x240x24) \u2014 but this is without using any compression methods.<\/p><\/blockquote>\n<p>\nFor example, for video at 30 frames per second, 24 bits per pixel, with a resolution of 480\u00d7240, it would require 82,944,000 bits per second or 82,944 Mbps (30x480x240x24) \u2014 but this is without using any compression methods.<\/p>\n<p>is almost constant <i>, it is called<\/i>constant bitrate <b>CBR<\/b> (<b>constant bit rate<\/b> \u2014 from <i>). But it can also vary; in this case, it is called<\/i>). But it can also vary; in this case, it's referred to as <b>variable bit rate<\/b> (<b>VBR<\/b> \u2014 from <i>variable bit rate<\/i>).<\/p>\n<p>This graph shows limited VBR, where not too many bits are spent in the case of a completely dark frame.<\/p>\n<p><img decoding=\"async\" alt=\"How a video codec works. Part 1. Fundamentals\" src=\"\/wp-content\/uploads\/2019\/12\/f307dde8da9ef1e017646ebcad0c4062.png\" style=\"display:block;margin: 0 auto;\" \/><br \/>\n<br \/>\nInitially, engineers developed a method to double the perceived frame rate of a display without using additional bandwidth. This method is known as <b>interlaced video<\/b>; primarily, it sends half of the screen in the first \"frame\" and the other half in the next \"frame.\"<\/p>\n<p>Currently, scene visualization mainly occurs using <b>progressive scanning technology<\/b>. This is a method of displaying, storing, or transmitting moving images, where all lines of each frame are drawn sequentially.<\/p>\n<p><img decoding=\"async\" alt=\"How a video codec works. Part 1. Fundamentals\" src=\"\/wp-content\/uploads\/2019\/12\/434afd828d803ec1e4c87a3827c07dde.png\" style=\"display:block;margin: 0 auto;\" \/><br \/>\n<br \/>\nWell, now we know how an image is represented digitally, how its colors are arranged, how many bits per second we spend to show video, whether the bit rate is constant (CBR) or variable (VBR). We understand the specified resolution using a given frame rate and are acquainted with many other terms, such as interlaced video, PAR, and some others.<\/p>\n<h2>Removing redundancy<\/h2>\n<p>\nIt is known that uncompressed video cannot be used effectively. An hour of video at 720p resolution and 30 frames per second would occupy 278 GB. We arrive at this value by multiplying 1280 x 720 x 24 x 30 x 3600 (width, height, bits per pixel, FPS, and time in seconds).<\/p>\n<p>Using <b>lossless compression algorithms<\/b>, like DEFLATE (used in PKZIP, Gzip, and PNG), will not provide sufficient reduction in required bandwidth. Therefore, we must look for other video compression methods.<\/p>\n<p>To this end, we can leverage the characteristics of our vision. We perceive brightness better than color. Video is a series of sequential images repeated over time. The differences between adjacent frames in the same scene are minor. Furthermore, each frame contains many areas using the same (or similar) color.<\/p>\n<h2>Color, brightness, and our eyes<\/h2>\n<p>\nOur eyes are more sensitive to brightness than to color. You can verify this yourself by looking at this image.<\/p>\n<p><img decoding=\"async\" alt=\"How a video codec works. Part 1. Fundamentals\" src=\"\/wp-content\/uploads\/2019\/12\/bf50b7dfd54ebbdb66556bbf5e304090.png\" style=\"display:block;margin: 0 auto;\" \/><\/p>\n<p>If you cannot see what colors the squares are on the left half of the image. <b>A<\/b> and <b>B<\/b> If they are actually the same, that's normal. Our brain causes us to pay more attention to light and shadow rather than color. On the right side between the marked squares, there is a bridge of the same color \u2014 hence we (i.e., our brain) can easily determine that, in fact, there is the same color there.<\/p>\n<blockquote><p>Let's break down (in simplified terms) how our eyes work. The eye is a complex organ made up of many parts. However, we are most interested in the cones and rods. The eye contains about 120 million rods and 6 million cones.<\/p>\n<p>Let's consider color and brightness perception as separate functions of certain parts of the eye (in reality, it's a bit more complex, but we'll simplify). Rod cells primarily respond to brightness, while cone cells respond to color. Cones are divided into three types, depending on the pigment they contain: S-cones (blue), M-cones (green), and L-cones (red).<\/p>\n<p>Since we have significantly more rods (brightness) than cones (color), it can be concluded that we are better at distinguishing transitions between darkness and light than colors.<\/p>\n<p><img decoding=\"async\" alt=\"How a video codec works. Part 1. Fundamentals\" src=\"\/wp-content\/uploads\/2019\/12\/d344365d45e43b36635ce29baa1132b4.png\" style=\"display:block;margin: 0 auto;\" \/><br \/>\n<\/p>\n<h2>Contrast Sensitivity Functions<\/h2>\n<p>\nResearchers in experimental psychology and many other fields have developed numerous theories of human vision. One of them is called <b>contrast sensitivity functions<\/b>. They are related to spatial and temporal illumination. In short, it concerns how many changes are needed before an observer notices them. Note the plural form of the word 'function.' This is because we can measure contrast sensitivity functions not only for black-and-white images but also for color images. Results from these experiments show that in most cases, our eyes are more sensitive to brightness than to color.<\/p><\/blockquote>\n<p>Since it is known that we are more sensitive to the brightness of an image, we can attempt to use this fact.<\/p>\n<h2>Color Model<\/h2>\n<p>\nWe have somewhat understood how to work with color images using RGB scheme. There are other models as well. There is a model that separates brightness from chroma, known as <b>YCbCr<\/b>. By the way, there are other models that perform a similar separation, but we will only consider this one.<\/p>\n<p>In this color model <b>Y<\/b> \u2014 it represents brightness, and two color channels are used: <b>Cb<\/b> (saturated blue) and <b>Cr<\/b> (saturated red). YCbCr can be derived from RGB, and the reverse transformation is also possible. Using this model, we can create full-color images, as shown below:<\/p>\n<p><img decoding=\"async\" alt=\"How a video codec works. Part 1. Fundamentals\" src=\"\/wp-content\/uploads\/2019\/12\/e7458471caf7ef071bb0362e96b44f11.png\" style=\"display:block;margin: 0 auto;\" \/><br \/>\n<\/p>\n<h2>Conversion between YCbCr and RGB<\/h2>\n<p>\nSomeone might argue: how is it possible to obtain all colors without using green?<\/p>\n<p>To answer this question, let\u2019s convert RGB to YCbCr. We will use the coefficients accepted in the standard <b>BT.601<\/b>, which was recommended by the <b>ITU-R<\/b>. This division defines standards for digital video. For example: what is 4K? What should be the frame rate, resolution, color model?<\/p>\n<p>First, we will calculate the brightness. We will use the constants proposed by the ITU and substitute the RGB values.<\/p>\n<p><b>Y<\/b> = 0.299<b>R<\/b> + 0.587<b>G<\/b> + 0.114<b>B<\/b> <\/p>\n<p>After obtaining the brightness, we will separate the blue and red colors:<\/p>\n<p><b>Cb<\/b> = 0.564(<b>B<\/b> \u2014 <b>Y<\/b>)<\/p>\n<p><b>Cr<\/b> = 0.713(<b>R<\/b> \u2014 <b>Y<\/b>) <\/p>\n<p>And we can also convert back and even obtain green using YCbCr:<\/p>\n<p><b>R<\/b> = <b>Y<\/b> + 1.402<b>Cr<\/b><\/p>\n<p><b>B<\/b> = <b>Y<\/b> + 1.772<b>Cb<\/b><\/p>\n<p><b>G<\/b> = <b>Y<\/b> \u2014 0.344<b>Cb<\/b> \u2014 0.714<b>Cr<\/b><\/p>\n<p>Generally, displays (monitors, TVs, screens, etc.) use only the RGB model. But this model can be organized in different ways:<\/p>\n<p><img decoding=\"async\" alt=\"How a video codec works. Part 1. Fundamentals\" src=\"\/wp-content\/uploads\/2019\/12\/8736e59d3a7606494fe6a7718d5b4aa2.png\" style=\"display:block;margin: 0 auto;\" \/><br \/>\n<\/p>\n<h2>Color Subsampling<\/h2>\n<p>\nWith an image represented as a combination of brightness and chrominance, we can take advantage of the human visual system's higher sensitivity to brightness than to chrominance when selectively removing information. Color subsampling is a method of encoding images, using lower resolution for chrominance than for brightness.<\/p>\n<p><img decoding=\"async\" alt=\"How a video codec works. Part 1. Fundamentals\" src=\"\/wp-content\/uploads\/2019\/12\/11b5b602ab1374896df296f787932ace.png\" style=\"display:block;margin: 0 auto;\" \/><\/p>\n<p>How much can we reduce chrominance resolution?! It turns out there are already some schemes that describe how to handle resolution and merging (Final color = Y + Cb + Cr).<\/p>\n<p>These schemes are known as <b>subsampling systems<\/b> and are expressed as a 3-to-1 ratio\u2014 <b>a<\/b>:<b>x<\/b>:<b>y<\/b>, which defines the number of samples of luminance and chrominance signals.<\/p>\n<p><b>a<\/b> \u2014 a reference horizontal sampling (usually equal to 4)<br \/>\n<b>x<\/b> \u2014 the number of chrominance samples in the first row of pixels (horizontal resolution relative to <b>a<\/b>)<br \/>\n<b>y<\/b> \u2014 the number of chrominance sample variations between the first and second rows of pixels.<\/p>\n<blockquote><p>An exception is <b>4<\/b>:<b>1<\/b>:<b>0<\/b>, which provides one chrominance sample in every block of luminance resolution 4 by 4.<\/p><\/blockquote>\n<p>Common schemes used in modern codecs:<\/p>\n<ul>\n<li><b>4<\/b>:<b>4<\/b>:<b>4<\/b> (without subsampling)<\/li>\n<li><b>4<\/b>:<b>2<\/b>:<b>2<\/b><\/li>\n<li><b>4<\/b>:<b>1<\/b>:<b>1<\/b><\/li>\n<li><b>4<\/b>:<b>2<\/b>:<b>0<\/b><\/li>\n<li><b>4<\/b>:<b>1<\/b>:<b>0<\/b><\/li>\n<li><b>3<\/b>:<b>1<\/b>:<b>1<\/b><\/li>\n<\/ul>\n<blockquote>\n<h3>YCbCr 4:2:0 \u2014 an example of merging<\/h3>\n<p>\nHere is a combined fragment of an image using YCbCr 4:2:0. Note that we only spend 12 bits per pixel.<\/p>\n<p><img decoding=\"async\" alt=\"How a video codec works. Part 1. Fundamentals\" src=\"\/wp-content\/uploads\/2019\/12\/67e288b0a46fca88452addaef736f4cd.png\" style=\"display:block;margin: 0 auto;\" \/><\/p><\/blockquote>\n<p>This is how the same image looks encoded with the main types of color subsampling. The top row shows the final YCbCr, while the bottom row displays the chroma resolution. Quite respectable results, considering the minor quality losses.<\/p>\n<p><img decoding=\"async\" alt=\"How a video codec works. Part 1. Fundamentals\" src=\"\/wp-content\/uploads\/2019\/12\/fb54b6fcdbbb63cb19d19b89df9f6d85.png\" style=\"display:block;margin: 0 auto;\" \/><br \/>\n<br \/>\nRemember, we calculated 278 GB of disk space to store an hour of video at 720p resolution and 30 frames per second? If we use YCbCr 4:2:0, that size will be halved to 139 GB. We're still a long way from an acceptable outcome.<\/p>\n<p>You can create a YCbCr histogram using FFmpeg. In this image, blue prevails over red, which is clearly visible in the histogram itself.<\/p>\n<p><img decoding=\"async\" alt=\"How a video codec works. Part 1. Fundamentals\" src=\"\/wp-content\/uploads\/2019\/12\/6e74cb646cabc77079aea7eaee846fd1.png\" style=\"display:block;margin: 0 auto;\" \/><\/p>\n<blockquote>\n<h4>Chroma, brightness, color spectrum \u2014 a video overview<\/h4>\n<p>\nI highly recommend watching this amazing video. It explains what brightness is, and essentially clarifies everything about <i>brightness<\/i> and color.<\/p>\n<p><center><div class=\"youtube-placeholder\" data-id=\"Ymt47wXUDEU\" onclick=\"loadVideo(this)\">\r\n        <img decoding=\"async\" src=\"https:\/\/img.youtube.com\/vi\/Ymt47wXUDEU\/hqdefault.jpg\" alt=\"Play video\" loading=\"lazy\" width=\"480\" height=\"360\" style=\"width:100%;height:auto;\">\r\n        <div class=\"play-button\"><\/div>\r\n    <\/div><\/center><\/p><\/blockquote>\n<h2>Types of frames<\/h2>\n<p>\nLet's move on. We'll try to eliminate temporal redundancy. But first, let's define some basic terminology. Let's assume we have a movie with 30 frames per second; here are its first 4 frames:<\/p>\n<p><img decoding=\"async\" alt=\"How a video codec works. Part 1. Fundamentals\" src=\"\/wp-content\/uploads\/2019\/12\/103dc714d756d014e1d2c1bfb33751b1.png\" style=\"display:block;margin: 0 auto;\" \/> <img decoding=\"async\" alt=\"How a video codec works. Part 1. Fundamentals\" src=\"\/wp-content\/uploads\/2019\/12\/fdd7d89b884b2f77fea7c386c0d29fef.png\" style=\"display:block;margin: 0 auto;\" \/> <img decoding=\"async\" alt=\"How a video codec works. Part 1. Fundamentals\" src=\"\/wp-content\/uploads\/2019\/12\/61746b829025d5abcb7db072a54400c0.png\" style=\"display:block;margin: 0 auto;\" \/> <img decoding=\"async\" alt=\"How a video codec works. Part 1. Fundamentals\" src=\"\/wp-content\/uploads\/2019\/12\/c65ee526ec4539996f6bb94463d0cf2d.png\" style=\"display:block;margin: 0 auto;\" \/><br \/>\n<br \/>\nWe can see a lot of repetitions in the frames: for example, the blue background that doesn't change from frame to frame. To address this issue, we can abstractly classify them into three types of frames.<\/p>\n<h3>I-frame (<b>I<\/b>Intro Frame)<\/h3>\n<p>\nAn I-frame (intra-frame, keyframe, internal frame) is standalone. Regardless of what needs to be visualized, an I-frame is essentially a static photo. The first frame is usually an I-frame, though we will regularly encounter I-frames among frames that are not first.<\/p>\n<p><img decoding=\"async\" alt=\"How a video codec works. Part 1. Fundamentals\" src=\"\/wp-content\/uploads\/2019\/12\/44f2a7d6154f1a40fdb1174a443a9c3f.png\" style=\"display:block;margin: 0 auto;\" \/><br \/>\n<\/p>\n<h3>P-frame (<b>P<\/b>Predicted Frame)<\/h3>\n<p>\nA P-frame (predicted frame) takes advantage of the fact that the current image can almost always be constructed using the previous frame. For example, in the second frame, the only change is the ball moving forward. We can get frame 2 simply by slightly modifying frame 1, only using the difference between these frames. To construct frame 2, we refer back to the preceding frame 1.<\/p>\n<p><img decoding=\"async\" alt=\"How a video codec works. Part 1. Fundamentals\" src=\"\/wp-content\/uploads\/2019\/12\/1245857fd8406a24da02d0c71beb7b4d.png\" style=\"display:block;margin: 0 auto;\" \/> \u2190 <img decoding=\"async\" alt=\"How a video codec works. Part 1. Fundamentals\" src=\"\/wp-content\/uploads\/2019\/12\/ab8e8f6020825830c4d03004528af1a4.png\" style=\"display:block;margin: 0 auto;\" \/><br \/>\n<\/p>\n<h3>B-frame (<b>B<\/b>i-predictive Frame)<\/h3>\n<p>\nWhat about links not only to past frames but also to future ones, to ensure even better compression?! This is essentially what a B-frame (bidirectional frame) is.<\/p>\n<p><img decoding=\"async\" alt=\"How a video codec works. Part 1. Fundamentals\" src=\"\/wp-content\/uploads\/2019\/12\/f28d4e02836e9b37f0f21937dd1ba660.png\" style=\"display:block;margin: 0 auto;\" \/> \u2190 <img decoding=\"async\" alt=\"How a video codec works. Part 1. Fundamentals\" src=\"\/wp-content\/uploads\/2019\/12\/7a6b3a329db155c94a91f58a99874bb9.png\" style=\"display:block;margin: 0 auto;\" \/> \u2192 <img decoding=\"async\" alt=\"How a video codec works. Part 1. Fundamentals\" src=\"\/wp-content\/uploads\/2019\/12\/09df4bc5a35df89575c7b51349be4340.png\" style=\"display:block;margin: 0 auto;\" \/><br \/>\n<\/p>\n<h2>Intermediate output<\/h2>\n<p>\nThese types of frames are used to achieve the best compression. We will explain how this works in the next section. For now, note that the most 'expensive' in terms of memory used is the I-frame, while the P-frame is significantly cheaper, and the most efficient option for video is the B-frame.<\/p>\n<p><img decoding=\"async\" alt=\"How a video codec works. Part 1. Fundamentals\" src=\"\/wp-content\/uploads\/2019\/12\/97b9177ffff302881deb1c86ce416166.png\" style=\"display:block;margin: 0 auto;\" \/><br \/>\n<\/p>\n<h2>Temporal redundancy (inter-frame prediction)<\/h2>\n<p>\nLet\u2019s consider what options we have to minimize temporal repetitions. This type of redundancy will be resolved through mutual prediction methods.<\/p>\n<p>We will try to use as few bits as possible to encode the sequence of frames 0 and 1.<\/p>\n<p><img decoding=\"async\" alt=\"How a video codec works. Part 1. Fundamentals\" src=\"\/wp-content\/uploads\/2019\/12\/f0dc2df33a5297622a77a4ca09b19af8.png\" style=\"display:block;margin: 0 auto;\" \/><br \/>\n<br \/>\nWe can perform <b>subtraction<\/b>, simply subtracting frame 1 from frame 0. We obtain frame 1 using only the difference between it and the previous frame, effectively encoding just the resulting remainder.<\/p>\n<p><img decoding=\"async\" alt=\"How a video codec works. Part 1. Fundamentals\" src=\"\/wp-content\/uploads\/2019\/12\/c14981c3a3a0459a02620805c4a0f2db.png\" style=\"display:block;margin: 0 auto;\" \/><br \/>\n<br \/>\nBut what if I told you that there\u2019s an even better method that uses even fewer bits?! First, let\u2019s break frame 0 into a clear grid made up of blocks. Then, we will try to match the blocks from frame 0 with those from frame 1. In other words, we will assess the motion between frames.<\/p>\n<blockquote><p> From Wikipedia \u2014 <b>block motion compensation<\/b><\/p>\n<p>Block motion compensation divides the current frame into non-overlapping blocks, and the motion compensation vector indicates the origin of the blocks (a common misconception is that <i>the previous<\/i> frame is divided into non-overlapping blocks, and the motion compensation vectors indicate where those blocks are moving. In fact, it\u2019s the other way around \u2014 the analysis is done not on the previous frame but on the subsequent one, determining not where the blocks move, but where they originated from). Typically, the original blocks overlap in the source frame. Some video compression algorithms reconstruct the current frame from parts of not just one but several previously transmitted frames.<\/p><\/blockquote>\n<p><img decoding=\"async\" alt=\"How a video codec works. Part 1. Fundamentals\" src=\"\/wp-content\/uploads\/2019\/12\/0c34e84b318be2d250bade240fad80ed.png\" style=\"display:block;margin: 0 auto;\" \/><br \/>\n<br \/>\nDuring the evaluation, we see that the ball has moved from (<b>x<\/b>=0, <b>y<\/b>=25) to (<b>x<\/b>=6, <b>y<\/b>=26), values <b>x<\/b> and <b>y<\/b> define the motion vector. Another step we can take to preserve bits is to encode only the difference in motion vectors between the block's last position and the predicted one, so the final motion vector will be (x=6-0=6, y=26-25=1).<\/p>\n<p>In a real scenario, this ball would be divided into <b>n<\/b> blocks, but that doesn't change the essence of the matter.<\/p>\n<p>Objects in the frame move in three dimensions, so when the ball moves, it may appear visually smaller (or larger if moving towards the viewer). It's normal that there won't be a perfect match between the blocks. Here is a combined view of our estimation and the real picture.<\/p>\n<p><img decoding=\"async\" alt=\"How a video codec works. Part 1. Fundamentals\" src=\"\/wp-content\/uploads\/2019\/12\/0fb4737e1076221e786fd6a78357422d.png\" style=\"display:block;margin: 0 auto;\" \/><br \/>\n<br \/>\nBut we see that when we apply motion estimation, the data for encoding is noticeably less than when using the simpler method of calculating the delta between frames.<\/p>\n<p><img decoding=\"async\" alt=\"How a video codec works. Part 1. Fundamentals\" src=\"\/wp-content\/uploads\/2019\/12\/319507ad7197b7693efff11dcd7e4df4.png\" style=\"display:block;margin: 0 auto;\" \/><br \/>\n<\/p>\n<h2>What real motion compensation will look like<\/h2>\n<p>\nThis technique is applied to all blocks at once. Often our hypothetical moving ball will be split into several blocks.<\/p>\n<p><img decoding=\"async\" alt=\"How a video codec works. Part 1. Fundamentals\" src=\"\/wp-content\/uploads\/2019\/12\/978b5612dbd164d2865ccdec4474fdb4.png\" style=\"display:block;margin: 0 auto;\" \/><br \/>\n<br \/>\nYou can experiment with these concepts by using <noindex><a rel=\"nofollow\" href=\"https:\/\/jupyter.org\/\">Jupyter<\/a><\/noindex>.<\/p>\n<p>To visualize motion vectors, you can create a video with external prediction using <noindex><a rel=\"nofollow\" href=\"https:\/\/www.ffmpeg.org\/\">ffmpeg<\/a><\/noindex>.<\/p>\n<p><img decoding=\"async\" alt=\"How a video codec works. Part 1. Fundamentals\" src=\"\/wp-content\/uploads\/2019\/12\/056d6dfd2c18baaabc70655edfe3d609.png\" style=\"display:block;margin: 0 auto;\" \/><br \/>\n<br \/>\nYou can also use <noindex><a rel=\"nofollow\" href=\"https:\/\/software.intel.com\/en-us\/video-pro-analyzer\">Intel Video Pro Analyzer<\/a><\/noindex> (it's paid, but there is a free trial version limited to just the first ten frames).<\/p>\n<p><img decoding=\"async\" alt=\"How a video codec works. Part 1. Fundamentals\" src=\"\/wp-content\/uploads\/2019\/12\/5e28ccc6e6101b8d613f94d326a685f2.png\" style=\"display:block;margin: 0 auto;\" \/><br \/>\n<\/p>\n<h2>Spatial redundancy (internal prediction)<\/h2>\n<p>\nIf we analyze each frame in the video, we will find many interrelated areas.<\/p>\n<p><img decoding=\"async\" alt=\"How a video codec works. Part 1. Fundamentals\" src=\"\/wp-content\/uploads\/2019\/12\/24bff6d64704cf0c8e1010832ea8f6ea.png\" style=\"display:block;margin: 0 auto;\" \/><br \/>\n<br \/>\nLet's walk through this example. This scene mainly consists of blue and white colors.<\/p>\n<p><img decoding=\"async\" alt=\"How a video codec works. Part 1. Fundamentals\" src=\"\/wp-content\/uploads\/2019\/12\/12edf221c61e064674ad363db9f3342c.png\" style=\"display:block;margin: 0 auto;\" \/><br \/>\n<br \/>\nThis is an I-frame. We cannot use previous frames for prediction, but we can compress it. We will encode the red block's selection. If we look at its neighbors, we notice that there are some color trends around it.<\/p>\n<p><img decoding=\"async\" alt=\"How a video codec works. Part 1. Fundamentals\" src=\"\/wp-content\/uploads\/2019\/12\/b67b3fba3d1792db81d0f1cb52c7c29b.png\" style=\"display:block;margin: 0 auto;\" \/><br \/>\n<br \/>\nWe assume that colors in the frame spread vertically. This means that the color values of unknown pixels will be influenced by the values of their neighbors.<\/p>\n<p><img decoding=\"async\" alt=\"How a video codec works. Part 1. Fundamentals\" src=\"\/wp-content\/uploads\/2019\/12\/6fe8f6ed7ebc66940fca27b8cd2c5915.png\" style=\"display:block;margin: 0 auto;\" \/><br \/>\n<br \/>\nSuch a prediction may also be incorrect. For this reason, we need to apply this method (internal prediction) and then subtract the actual values. This will give us a residual block, leading to a much more compact matrix compared to the original.<\/p>\n<p><img decoding=\"async\" alt=\"How a video codec works. Part 1. Fundamentals\" src=\"\/wp-content\/uploads\/2019\/12\/52fd77c96aa8445af12fca468c72c844.png\" style=\"display:block;margin: 0 auto;\" \/><br \/>\n<br \/>\nIf you want to practice with internal predictions, you can create a video with macro blocks and their predictions using ffmpeg. To understand the meaning of each block color, you'll need to refer to the ffmpeg documentation.<\/p>\n<p><img decoding=\"async\" alt=\"How a video codec works. Part 1. Fundamentals\" src=\"\/wp-content\/uploads\/2019\/12\/0399473c538a550f9317fd59c347a565.png\" style=\"display:block;margin: 0 auto;\" \/><br \/>\n<br \/>\nAlternatively, you can use Intel Video Pro Analyzer (as I mentioned earlier, the trial version is limited to the first 10 frames, but that should be sufficient for your initial needs).<\/p>\n<p><img decoding=\"async\" alt=\"How a video codec works. Part 1. Fundamentals\" src=\"\/wp-content\/uploads\/2019\/12\/abcdf1eb4b7dbdcf2a7a64154f3052ba.png\" style=\"display:block;margin: 0 auto;\" \/><\/p>\n<h3>Part Two:<noindex><a rel=\"nofollow\" href=\"https:\/\/habr.com\/ru\/company\/edison\/blog\/480430\/\"> Principles of Video Codec Operation<\/a><\/noindex><\/h3>\n<p>Source: <a content=\"nofollow\" rel=\"nofollow\" href=\"https:\/\/habr.com\/ru\/company\/edison\/blog\/481418\/\">habr.com<\/a><\/p>","protected":false,"gt_translate_keys":[{"key":"rendered","format":"html"}]},"excerpt":{"rendered":"<p>\u0412\u0442\u043e\u0440\u0430\u044f \u0447\u0430\u0441\u0442\u044c: \u041f\u0440\u0438\u043d\u0446\u0438\u043f\u044b \u0440\u0430\u0431\u043e\u0442\u044b \u0432\u0438\u0434\u0435\u043e\u043a\u043e\u0434\u0435\u043a\u0430 \u041b\u044e\u0431\u043e\u0435 \u0440\u0430\u0441\u0442\u0440\u043e\u0432\u043e\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0432 \u0432\u0438\u0434\u0435 \u0434\u0432\u0443\u043c\u0435\u0440\u043d\u043e\u0439 \u043c\u0430\u0442\u0440\u0438\u0446\u044b. \u041a\u043e\u0433\u0434\u0430 \u0440\u0435\u0447\u044c \u0437\u0430\u0445\u043e\u0434\u0438\u0442 \u043e \u0446\u0432\u0435\u0442\u0430\u0445, \u0438\u0434\u0435\u044e \u043c\u043e\u0436\u043d\u043e \u0440\u0430\u0437\u0432\u0438\u0442\u044c, \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u044f \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0432 \u0432\u0438\u0434\u0435 \u0442\u0440\u0435\u0445\u043c\u0435\u0440\u043d\u043e\u0439 \u043c\u0430\u0442\u0440\u0438\u0446\u044b, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0438\u0437\u043c\u0435\u0440\u0435\u043d\u0438\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445 \u043f\u043e \u043a\u0430\u0436\u0434\u043e\u043c\u0443 \u0438\u0437 \u0446\u0432\u0435\u0442\u043e\u0432. \u0415\u0441\u043b\u0438 \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0442\u044c \u0438\u0442\u043e\u0433\u043e\u0432\u044b\u0439 \u0446\u0432\u0435\u0442 \u043a\u0430\u043a \u043a\u043e\u043c\u0431\u0438\u043d\u0430\u0446\u0438\u044e \u0442.\u043d. \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0445 \u0446\u0432\u0435\u0442\u043e\u0432 (\u043a\u0440\u0430\u0441\u043d\u043e\u0433\u043e, \u0437\u0435\u043b\u0435\u043d\u043e\u0433\u043e \u0438 \u0441\u0438\u043d\u0435\u0433\u043e), \u0432 \u043d\u0430\u0448\u0435\u0439 [&hellip;]<\/p>\n","protected":false,"gt_translate_keys":[{"key":"rendered","format":"html"}]},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[702],"tags":[],"class_list":["post-54312","post","type-post","status-publish","format-standard","hentry","category-news"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.2 - aioseo.com -->\n\t<meta name=\"description\" content=\"\u0412\u0442\u043e\u0440\u0430\u044f \u0447\u0430\u0441\u0442\u044c: \u041f\u0440\u0438\u043d\u0446\u0438\u043f\u044b \u0440\u0430\u0431\u043e\u0442\u044b \u0432\u0438\u0434\u0435\u043e\u043a\u043e\u0434\u0435\u043a\u0430 \u041b\u044e\u0431\u043e\u0435 \u0440\u0430\u0441\u0442\u0440\u043e\u0432\u043e\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0432 \u0432\u0438\u0434\u0435.\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Yuri Gagarin\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/prohoster.info\/en\/blog\/news\/kak-rabotaet-videokodek-chast-1-osnovy\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.2\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"ProHoster | \u041a\u0443\u043f\u0438\u0442\u044c \u043d\u0430\u0434\u0435\u0436\u043d\u044b\u0439 \u0445\u043e\u0441\u0442\u0438\u043d\u0433 \u0434\u043b\u044f \u0441\u0430\u0439\u0442\u043e\u0432 \u0441 \u0437\u0430\u0449\u0438\u0442\u043e\u0439 \u043e\u0442 DDoS, VPS VDS \u0441\u0435\u0440\u0432\u0435\u0440\u044b\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"\ud83e\udd47\u041a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0432\u0438\u0434\u0435\u043e\u043a\u043e\u0434\u0435\u043a. \u0427\u0430\u0441\u0442\u044c 1. \u041e\u0441\u043d\u043e\u0432\u044b | ProHoster\" \/>\n\t\t<meta property=\"og:description\" content=\"\u0412\u0442\u043e\u0440\u0430\u044f \u0447\u0430\u0441\u0442\u044c: \u041f\u0440\u0438\u043d\u0446\u0438\u043f\u044b \u0440\u0430\u0431\u043e\u0442\u044b \u0432\u0438\u0434\u0435\u043e\u043a\u043e\u0434\u0435\u043a\u0430 \u041b\u044e\u0431\u043e\u0435 \u0440\u0430\u0441\u0442\u0440\u043e\u0432\u043e\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0432 \u0432\u0438\u0434\u0435.\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/prohoster.info\/en\/blog\/news\/kak-rabotaet-videokodek-chast-1-osnovy\" \/>\n\t\t<meta property=\"og:image\" content=\"https:\/\/prohoster.info\/wp-content\/uploads\/2021\/11\/logo-350.jpg\" \/>\n\t\t<meta property=\"og:image:secure_url\" content=\"https:\/\/prohoster.info\/wp-content\/uploads\/2021\/11\/logo-350.jpg\" \/>\n\t\t<meta property=\"og:image:width\" content=\"350\" \/>\n\t\t<meta property=\"og:image:height\" content=\"350\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2019-12-22T21:00:00+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2020-02-18T11:02:18+00:00\" \/>\n\t\t<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prohoster\" \/>\n\t\t<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/prohoster\" \/>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"\ud83e\udd47How a Video Codec Works. Part 1. Basics | ProHoster","description":"Part Two: Principles of Video Codec Operation Any raster image can be represented in the form.","canonical_url":"https:\/\/prohoster.info\/en\/blog\/news\/kak-rabotaet-videokodek-chast-1-osnovy","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":null,"og:locale":"en_US","og:site_name":"ProHoster | \u041a\u0443\u043f\u0438\u0442\u044c \u043d\u0430\u0434\u0435\u0436\u043d\u044b\u0439 \u0445\u043e\u0441\u0442\u0438\u043d\u0433 \u0434\u043b\u044f \u0441\u0430\u0439\u0442\u043e\u0432 \u0441 \u0437\u0430\u0449\u0438\u0442\u043e\u0439 \u043e\u0442 DDoS, VPS VDS \u0441\u0435\u0440\u0432\u0435\u0440\u044b","og:type":"article","og:title":"\ud83e\udd47\u041a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0432\u0438\u0434\u0435\u043e\u043a\u043e\u0434\u0435\u043a. \u0427\u0430\u0441\u0442\u044c 1. \u041e\u0441\u043d\u043e\u0432\u044b | ProHoster","og:description":"\u0412\u0442\u043e\u0440\u0430\u044f \u0447\u0430\u0441\u0442\u044c: \u041f\u0440\u0438\u043d\u0446\u0438\u043f\u044b \u0440\u0430\u0431\u043e\u0442\u044b \u0432\u0438\u0434\u0435\u043e\u043a\u043e\u0434\u0435\u043a\u0430 \u041b\u044e\u0431\u043e\u0435 \u0440\u0430\u0441\u0442\u0440\u043e\u0432\u043e\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0432 \u0432\u0438\u0434\u0435.","og:url":"https:\/\/prohoster.info\/en\/blog\/news\/kak-rabotaet-videokodek-chast-1-osnovy","og:image":"https:\/\/prohoster.info\/wp-content\/uploads\/2021\/11\/logo-350.jpg","og:image:secure_url":"https:\/\/prohoster.info\/wp-content\/uploads\/2021\/11\/logo-350.jpg","og:image:width":350,"og:image:height":350,"article:published_time":"2019-12-22T21:00:00+00:00","article:modified_time":"2020-02-18T11:02:18+00:00","article:publisher":"https:\/\/www.facebook.com\/prohoster","article:author":"https:\/\/www.facebook.com\/prohoster"},"aioseo_meta_data":{"post_id":"54312","title":null,"description":null,"keywords":null,"keyphrases":null,"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":null,"og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"","isEnabled":true},"graphs":[]},"schema_type":null,"schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"local_seo":null,"seo_analyzer_scan_date":"2026-01-24 10:53:20","breadcrumb_settings":null,"limit_modified_date":false,"reviewed_by":null,"ai":null,"created":"2021-02-28 15:31:31","updated":"2026-01-24 10:53:20","focus_keyword":null,"additional_keywords":null,"truseo_locale":null},"gt_translate_keys":[{"key":"link","format":"url"}],"_links":{"self":[{"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/posts\/54312","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/comments?post=54312"}],"version-history":[{"count":0,"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/posts\/54312\/revisions"}],"wp:attachment":[{"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/media?parent=54312"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/categories?post=54312"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/tags?post=54312"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}