in ,

leandromoreira / digital_video_introduction, Hacker News

leandromoreira / digital_video_introduction, Hacker News


                    

        

🇨🇳🇯🇵🇮🇹🇰🇷

license

A gentle introduction to video technology, although it’s aimed at software developers / engineers, we want to make it easyfor anyone to learn. This idea was born during amini workshop for newcomers to video technology.

The goal is to introduce some digital video concepts with asimple vocabulary, lots of visual elements and practical exampleswhen possible, and make this knowledge available everywhere. Please, feel free to send corrections, suggestions and improve it.

There will behands-onsections which require you to havedocker installedand this repository cloned.

git clone https://github.com/leandromoreira/digital_video_introduction. gitCDdigital_video_introduction ./setup.sh

WARNING: when you see a./s / ffmpegor./s / mediainfocommand, it means we’re running acontainerized versionof that program, which already includes all the needed requirements.

All thehands-on should be performed from the folder you clonedthis repository. For thejupyter examplesyou must start the server./ s / start_jupyter. shand copy the URL and use it in your browser.

  • added DRM system
  • released version 1.0.0
  • added simplified Chinese translation

Animagecan be thought of as a2D matrix. If we think aboutcolors, we can extrapolate this idea seeing this image as a3D matrixwhere theadditional dimensionsare used to providecolor data.

If we chose to represent these colors using theprimary colors (red, green and blue), we define three planes: the first one forred, the second forgreen, and the last one for theblueColor.

an image is a 3d matrix RGB

We’ll call each point in this matrixa pixel(picture element). One pixel represents theintensity(usually a numeric value) of a given color. For example, ared pixelmeans 0 of green, 0 of blue and maximum of red. Thepink color pixelcan be formed with a combination of the three colors. Using a representative numeric range from 0 to 255, the pink pixel is defined byRed=255, Green=192 and Blue=203.

Other ways to encode a color image

Many other possible models may be used to represent the colors that make up an image. We could, for instance, use an indexed palette where we’d only need a single byte to represent each pixel instead of the 3 needed when using the RGB model. In such a model we could use a 2D matrix instead of a 3D matrix to represent our color, this would save on memory but yield fewer color options.

NES palette

For instance, look at the picture down below. The first face is fully colored. The others are the red, green, and blue planes (shown as gray tones).

RGB channels intensity

We can see that thered colorwill be the one thatcontributes more(the brightest parts in the second face) to the final color while the (blue color) contribution can be mostlyonly seen in Mario’s eyes(last face) and part of his clothes, see howall planes contribute less(darkest parts) to theMario’s mustache.

And each color intensity requires a certain amount of bits, this quantity is known as (bit depth. Let’s say we spend8 bits(accepting values ​​from 0 to 255) per color (plane), therefore we have acolor depthof24 bits(8 bits * 3 planes R / G / B), and we can also infer that we could use 2 to the power of (different colors.)

It’s greatto learnhow an image is captured from the world to the bits.

Another property of an image is theresolution, which is the number of pixels in one dimension. It is often presented as width × height, for example, the4 × 4image below.

image resolution

Hands-on: play around with image and color

You canplay around with image and colorsusingjupyter( python, numpy, matplotlib and etc).

You can also learnhow image filters (edge ​​detection, sharpen, blur …) work.

Another property we can see while working with images or video is theaspect ratiowhich simply describes the proportional relationship between width and height of an image or pixel.

When people says this movie or picture is16 x9they usually are referring to theDisplay Aspect Ratio (DAR), however we also can have different shapes of individual pixels, we call thisPixel Aspect R atio (PAR).

display aspect ratio

pixel aspect ratio

DVD is DAR 4: 3

Although the real resolution of a DVD is 704 x 480 it still keeps a 4: 3 aspect ratio because it has a PAR of 10: 11 ( (x) / 480 x 11)

Finally, we can define avideoas asuccession of (n) framesintimewhich can be seen as another dimension, (n) is the frame rate or frames per second (FPS).

The number of bits per second needed to show a video is itsbit rate.

bit rate=width * height * bit depth * frames per second

For example, a video with 30 frames per second, 24 bits per pixel, resolution of (x) will need82, 944, 00 0 bits per secondor 82. (Mbps) 30 x (x) **************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************** (x) ) if we don’t employ any kind of compression.

When thebit rateis nearly constant it’s called constant bit rate (CBR) but it also can vary then called variable bit rate (VBR).

This graph shows a constrained VBR which doesn’t spend too many bits while the frame is black.

constrained vbr

In the early days, engineers came up with a technique for doubling the perceived frame rate of a video displaywithout consuming extra bandwidth. This technique is known asinterlaced video; it basically sends half of the screen in 1 “frame” and the other half in the next “frame”.

Today screens render mostly usingprogressive scan technique. Progressive is a way of displaying, storing, or transmitting moving images in which all the lines of each frame are drawn in sequence.

interlaced vs progressive

Now we have an idea about how animageis represented digitally, how its (colors) are arranged, how manybits per seconddo we spend to show a video, if it’s constant (CBR ) or variable (VBR), with a givenresolutionusing a givenframe rateand many other terms such as interlaced, PAR and others.

Hands-on: Check video properties

You cancheck most of the explained properties with ffmpeg or mediainfo.

We learned that it’s not feasible to use video without any compression;a single one hour videoat 720 p resolution with 30 fps wouldrequire (GB) *. Sinceusing solely lossless data compression algorithmslike DEFLATE (used in PKZIP, Gzip, and PNG),won’tdecrease the required bandwidth sufficiently we need to find other ways to compress the video.

*We found this number by multiplying (x) ******************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************* (x) ************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************ (x) x 3600 ( width, height, bits per pixel, fps and time in seconds)

In order to do this, we canexploit how our vision works. We’re better at distinguishing brightness than colors, therepetitions in time, a video contains a lot of images with few changes, and therepetitions within the image, each frame also contains many areas using the same or similar color.

Colors, Luminance and our eyes

Our eyes aremore sensitive to brightness than colors, you can test it for yourself, look at this picture.

luminance vs color

If you are unable to see that the colors of thesquares A and B are identicalon the left side, that’s fine, it’s our brain playing tricks on us topay more attention to light and dark than color. There is a connector, with the same color, on the right side so we (our brain) can easily spot that in fact, they’re the same color.

Simplistic explanation of how our eyes workTheeye is a complex organ, it is composed of many parts but we are mostly interested in the cones and rods cells. The eyecontains about 120 million rod cells and 6 million cone cells.

ToOversimplify, let’s try to put colors and brightness in the eye’s parts function. Therod cellsare mostly responsible for brightnesswhile thecone cellsare responsible for color, there are three types of cones, each with different pigment, namely:S-cones (Blue), M-cones (Green) and L-cones (Red).

Since we have many more rod cells (brightness) than cone cells (color), one can infer that we are more capable of distinguishing dark and light than colors.

eyes composition

Contrast sensitivity functions

Researchers of experimental psychology and many other fields have developed many theories on human vision. And one of them is called Contrast sensitivity functions. They are related to spatio and temporal of the light and their value presents at given init light, how much change is required before an observer reported there was a change. Notice the plural of the word “function”, this is for the reason that we can measure Contrast sensitivity functions with not only black-white but also colors. The result of these experiments shows that in most cases our eyes are more sensitive to brightness than color.

Once we know that we’re more sensitive toluma(the brightness in an image) we can try to exploit it.

Color model

We first learnedhow to color imageswork using theRGB model, but there are other models too. In fact, there is a model that separates luma (brightness) from chrominance (colors) and it is known asYCbCr*.

*there are more models which do the same separation.

This color model usesYto represent the brightness and two color channelsCb(chroma blue) andCr(chroma red). The (YCbCr) can be derived from RGB and it also can be converted back to RGB. Using this model we can create full colored images as we can see down below.

ycbcr example

Converting between YCbCr and RGB

Some may argue, how can we produce all thecolors without using the green?

To answer this question, we’ll walk through a conversion from RGB to YCbCr. We’ll use the coefficients from thestandard BT. 601that was recommended by the(ITU-R group) *. The first step is tocalculate the luma, we’ll use the constants suggested by ITU and replace the RGB values.

Y=0.  (R   0.)  G   0. 114 B

Once we had the luma, we cansplit the colors(chroma blue and red):

Cb=0. 564 (B - Y) Cr=0.  (R - Y)

And we can alsoconvert it backand even get thegreen by using YCbCr.

R=Y   1. 402 Cr B=Y   1. 772 Cb G=Y - 0.  (Cb - 0.)  Cr

*groups and standards are common in digital video, they usually define what are the standards, for instance,what is 4K? what frame rate should we use? resolution? color model?

Generally,displays(monitors, TVs, screens and etc) utilizeonly the RGB model, organized in different manners, see some of them magnified below:

pixel geometry

Chroma subsampling

With the image represented as luma and chroma components, we can take advantage of the human visual system’s greater sensitivity for luma resolution rather than chroma to selectively remove information.Chroma subsamplingis the technique of encoding images usingless resolution for chroma than for luma.

ycbcr subsampling resolutions

How much should we reduce the chroma resolution ?! It turns out that there are already some schemas that describe how to handle resolution and the merge (final color=Y Cb Cr).

These schemas are known as subsampling systems and are expressed as a 3 part ratio –A: X: Ywhich defines the chroma resolution in relation to aax 2block of luma pixels.

  • ais the horizontal sampling reference (usually 4)
  • (x) is the number of chroma samples in the first row of (a) ********************************* (pixels) horizontal resolution in relation toa)
  • (y) is the number of changes of chroma samples between the first and seconds rows of (a) ********************************* (pixels.)

An exception to this exists with 4: 1: 0, which provides a single chroma sample within each (4 x 4) block of luma resolution.

Common schemes used in modern codecs are:4: 4: 4(no subsampling),4: 2: 2, 4: 1 : 1, 4: 2: 0, 4: 1: 0 and 3: 1: 1.

YCbCr 4: 2: 0 merge

Here’s a merged piece of an image using YCbCr 4: 2: 0, notice that we only spend 12 bits per pixel.

YCbCr 4:2:0 merge

You can see the same image encoded by the main chroma subsampling types, images in the first row are the final YCbCr while the last row of images shows the chroma resolution. It’s indeed a great win for such small loss.

chroma subsampling examples

Previously we had calculated that we needed278 GB of storage to keep a video file with one hour at 720 p resolution and 30 FPS. If we useYCbCr 4: 2: 0we can cutthis size in half (139 GB)*but it is still far from ideal.

*we found this value by multiplying width, height, bits per pixel and fps. Previously we needed 24 bits, now we only need 12.

Hands-on: Check YCbCr histogram

You cancheck the YCbCr histogram with ffmpeg.This scene has a higher blue contribution, which is showed by theHistogram.

ycbcr color histogram

ball 1ball 2ball 3ball 4

We can seelots of repetitionswithin frames likethe blue background, it doesn’t change from frame 0 to frame 3. To tackle this problem, we canabstractly categorizethem as three types of frames.

I Frame (intra, keyframe)

An I-frame (reference, keyframe, intra) is aself- contained frame. It doesn’t rely on anything to be rendered, an I-frame looks similar to a static photo. The first frame is usually an I-frame but we’ll see I-frames inserted regularly among other types of frames.

P Frame (predicted)

A P-frame takes advantage of the fact that almost always the current picture can berendered using the previous frame.For instance, in the second frame, the only change was the ball that moved forward. We canrebuild frame 1, only using the difference and referencing to the previous frame.

ball 2

Hands-on: A video with a single I -frame

Since a P-frame uses less data why can’t we encode an entirevideo with a single I-frame and all the rest being P-frames?

After you encoded this video, start to watch it and do aseek for an advancedpart of the video, you’ll noticeit takes some timeto really move to that part. That’s because aP-frame needs a reference frame(I-frame for instance) to be rendered.

Another quick test you can do is to encode a video using a single I-Frame and thenencode it inserting an I-frame each 2sandcheck the size of each rendition.

B Frame (bi-predictive)

What about referencing the past and future frames to provide even a better compression ?! That’s basically what a B-frame is.

ball 2->ball 3

Hands-on: Compare videos with B-frame

You can generate two renditions, first with B-frames and other withno B-frames at alland check the size of the file as well as the quality.

Summary

These frames types are used toprovide better compression. We’ll look how this happens in the next section, but for now we can think ofI-frame as expensive while P-frame is cheaper but the cheapest is the B-frame.

frame types example

Temporal redundancy (inter prediction)

Let’s explore the options we have to reduce therepetitions in time, this type of redundancy can be solved with techniques ofinter prediction.

We will try tospend fewer bitsto encode the sequence of frames 0 and 1.

original frames

One thing we can do it’s a subtraction, we simplysubtract frame 1 from frame 0and we get just what we need toencode the residual.

delta frames

But what if I tell you that there is abetter methodwhich uses even fewer bits ?! First, let’s treat theframe 0as a collection of well-defined partitions and then we’ll try to match the blocks from (frame 0) on (frame 1. We can think of it asmotion estimation.

delta frames

We could estimate that the ball moved fromx=0, y=25tox=6, y=26, thexandyvalues ​​are themotion vectors. Onefurther stepwe can do to save bits is toencode only the motion vector differencebetween the last block position and the predicted, so the final motion vector would be (x=6 (6-0), y=1) 26 – 25)

In a real-world situation, thisball would be sliced ​​into n partitionsbut the process is the same.

The objects on the framemove in a 3D way, the ball can become smaller when it moves to the background. It’s normal thatwe won’t find the perfect matchto the block we tried to find a match. Here’s a superposed view of our estimation vs the real picture.

motion estimation

But we can see that when we applymotion estimationthedata to encode is smallerthan using simply delta frame techniques.

motion estimation vs delta

How real motion compensation would look

This technique is applied to all blocks, very often a ball would be partitioned in more than one block.real world motion compensationSource:https://web.stanford.edu/class/ee 398 a / handouts / lectures / EE 398 a_MotionEstimation _ 2012 .pdf

You canplay around with these concepts using jupyter.

Hands-on: See the motion vectors

We cangenerate a video with the inter prediction (motion vectors) with ffmpeg.

inter prediction (motion vectors) with ffmpeg

Or we can use theIntel Video Pro Analyzer(which is paid but there is a free trial version which limits you to only the first (frames).

inter prediction intel video pro analyzer

Spatial redundancy (intra prediction)

If we analyzeeach framein a video we’ll see that there are alsomany areas that are correlated.

Let’s walk through an example. This scene is mostly composed of blue and white colors.

This is anI-frameand wecan’t use previous framesto predict from but we still can compress it. We will encode the red block selection. If welook at its neighbors, we canestimatethat there is atrend of colors around it.

We willpredictthat the frame will continue tospread the colors vertically, it means that the colors of theunknown pixels will hold the values ​​of its neighbors.

Ourprediction can be wrong, for that reason we need to apply this technique (intra prediction) and thensubtract the real values ​​which gives us the residual block, resulting in a much more compressible matrix compared to the original.

Hands-on: Check intra predictions

You cangenerate a video with macro blocks and their predictions with ffmpeg.Please check the ffmpeg documentation to understand themeaning of each block color

intra prediction (macro blocks) with ffmpeg

Or we can use theIntel Video Pro Analyzer(which is paid but there is a free trial version which limits you to only the first (frames).

intra prediction intel video pro analyzer

What? Why? How?

What?It’s a piece of software / hardware that compresses or decompresses digital video.Why?Market and society demands higher quality videos with limited bandwidth or storage. Remember when wecalculated the needed bandwidthfor 30 frames per second, 24 bits per pixel, resolution of a 480 x 240 video ? It was82. (Mbps) with no compression applied. It’s the only way to deliver HD / FullHD / 4K in TVs and the Internet.How?We’ll take a brief look at the major techniques here.

CODEC vs Container

One common mistake that beginners often do is to confuse digital video CODEC anddigital video container. We can think ofcontainersas a wrapper format which contains metadata of the video (and possible audio too), and thecompressed videocan be seen as its payload.

Usually, the extension of a video file defines its video container. For instance, the filevideo.mp4is probably a(MPEG-4 Part)container and a file namedvideo.mkvit’s probably aMatroska. To be completely sure about the codec and container format we can useffmpeg or mediainfo.

History

Before we jump into the inner workings of a generic codec, let’s look back to understand a little better about some old video codecs.

The video codec (H.) was born in 1990 (technically 1988), and it was designed to work withdata rates of 64 kbit / s. It already uses ideas such as chroma subsampling, macro block, etc. In the year of 1995, theH. 263video codec standard was published and continued to be extended until 2001.

In 2003 the first version ofH. 264 / AVCwas completed. In the same year, a company calledTrueMotionreleased their video codec as aroyalty-freelossy video compression calledVP3. In 2008,Google boughtthis company, releasing (VP8) in the same year. In December of 2012, Google released theVP9and it’ssupported by roughly ¾ of the browser market(mobile included).

AV1(is a new) royalty-freeand open source video codec that’s being designed by theAlliance for Open Media (AOMedia), which is composed of thecompanies: Google, Mozilla, Microsoft, Amazon, Netflix, AMD, ARM, NVidia, Intel and Ciscoamong others. Thefirst version0.1.0 of the reference codec waspublished on April 7,.

codec history timeline

, Xiph (Mozilla) was working on (Daalaand Cisco open-sourced its royalty-free video codec calledThor.

Then MPEG LA first announced annual caps for HEVC (H. 265) and fees 8 times higher than H. 264 but soon they changed the rules again:

  • no annual cap,
  • content fee(0.5% of revenue) and
  • per-unit fees about 10 times higher than h 264.

Thealliance for open mediawas created by companies from hardware manufacturer (Intel, AMD, ARM, Nvidia, Cisco), content delivery (Google, Netflix, Amazon), browser maintainers (Google, Mozilla), and others.

The companies had a common goal, a royalty-free video codec and then AV1 was born with a muchsimpler patent license.Timothy B. Terriberrydid an awesome presentation, which is the source of this section, about theAV1 conception, license model and its current state.

You’ll be surprised to know that you cananalyze the AV1 codec through your browser, go tohttp://aomanalyzer.org/

av1 browser analyzer

PS: If you want to learn more about the history of the codecs you must learn the basics behindvideo compression patents.

1st step – picture partitioning

The first step is todivide the frameinto severalpartitions, sub- partitionsand beyond.

picture partitioning

But why?There are many reasons, for instance, when we split the picture we can work the predictions more precisely, using small partitions for the small moving parts while using bigger partitions to a static background.

Usually, the CODECsorganize these partitionsinto slices (or tiles), macro (or coding tree units) and many sub-partitions. The max size of these partitions varies, HEVC sets (x) while AVC uses (x) but the sub-partitions can reach sizes of 4×4.

Remember that we learned howframes are typed?! Well, you canapply those ideas to blockstoo, therefore we can have I-Slice, B-Slice, I-Macroblock and etc.

Hands-on: Check partitions

We can also use theIntel Video Pro Analyzer(which is paid but there is a free trial version which limits you to only the first (frames). Here areVP9 partitionsanalyzed.

VP9 partitions view intel video pro analyzer

2nd step – predictions

Once we have the partitions, we can make predictions over them. For theinter predictionwe needto send the motion vectors and the residualand theintra predictionwe’llsend the prediction direction and the residualas well.

3rd step – transform

After we get the residual block (predicted partition - real partition), we cantransformit in a way that lets us know whichpixels we can discardwhile keeping theoverall quality. There are some transformations for this exact behavior.

Although there areother transformations, we’ll look more closely at the discrete cosine transform (DCT). TheDCTmain features are:

  • convertsblocks ofpixelsinto same-sized blocks offrequency coefficients.
  • compactsenergy, making it easy to eliminate spatial redundancy.
  • isreversible, aka you can reverse to pixels.

On 2 Feb 2017, Cintra, RJ and Bayer, F. M have published their paperDCT-like Transform for Image Compression Requires 14 Additions Only.

Don’t worry if you didn’t understand the benefits from every bullet point, we’ll try to make some experiments in order to see the real value from it.

Let’s take the followingblock of pixels(8×8):

pixel values matrix

Which renders to the following block image (8×8):

pixel values matrix

When weapply the DCTover this block of pixels and we get theblock of coefficients(8×8):

coefficients values

And if we render this block of coefficients, we’ll get this image:

dct coefficients image

As you can see it looks nothing like the original image, we might notice that thefirst coefficientis very different from all the others. This first coefficient is known as the DC coefficient which represents ofall the samplesin the input array, somethingsimilar to an average.

This block of coefficients has an interesting property which is that it separates the high-frequency components from the low frequency.

dct frequency coefficients property

In an image,most of the energywill be concentrated in thelower frequencies, so if we transform an image into its frequency components andthrow away the higher frequency coefficients, we canreduce the amount of dataneeded to describe the image without sacrificing too much image quality.

frequency means how fast a signal is  changing

Let’s try to apply the knowledge we acquired in the test by converting the original image to its frequency (block of coefficients) using DCT and then throwing away part of the least important coefficients.

First, we convert it to itsfrequency domain.

coefficients values

Next, we discard part (67%) of the coefficients, mostly the bottom right part of it.

zeroed coefficients

Finally, we reconstruct the image from this discarded block of coefficients (remember, it needs to be reversible) and compare it to the original.

original vs quantized

As we can see it resembles the original image but it introduced lots of differences from the original, wethrow away 67. %and we still were able to get at least something similar to the original. We could more intelligently discard the coefficients to have a better image quality but that’s the next topic.

Each coefficient is formed using all the pixels

It’s important to note that each coefficient doesn’t directly map to a single pixel but it’s a weighted sum of all pixels. This amazing graph shows how the first and second coefficient is calculated, using weights which are unique for each index.

dct calculation

Source:https://web.archive.org/web / 20150129171151 / https: // www.iem.thm.de/telekom-labor/zinke/mk/mpeg2beg/whatisit.htm

You can also try toVisualize the DCT by looking at a simple imageformation over the DCT basis. For instance, here’s theA character being formedusing each coefficient weight.

Hands-on: throwing away different Coefficients

You can play around with theDCT transform.

4th step – quantization

When we throw away some of the coefficients, in the last step (transform), we kinda did some form of quantization. This step is where we chose to lose information (thelossy part) or in simple terms, we’llquantize coefficients to achieve compression.

How can we quantize a block of coefficients? One simple method would be a uniform quantization, where we take a block,divide it by a single value(10) and round this value.

quantize

How can wereverse(re-quantize) this block of coefficients? We can do that bymultiplying the same value(10) we divide it first.

re-quantize

Thisapproach isn’t the bestbecause it doesn’t take into account the importance of each coefficient, we could use amatrix of quantizersinstead of a single value, this matrix can exploit the property of the DCT, quantizing most the bottom right and less the upper left, theJPEG uses a similar approach, you can checksource code to see this matrix.

Hands-on: quantization

You can play around with theQuantization.

5th step – entropy coding

After we quantized the data (image blocks / slices / frames) we still can compress it in a lossless way. There are many ways (algorithms) to compress data. We’re going to briefly experience some of them, for a deeper understanding you can read the amazing bookUnderstanding Compression: Data Compression for Modern Developers.

VLC coding:

Let’s suppose we have a stream of the symbols:a, (e) , (r) andtand their probability (from 0 to 1) is represented by this table.

(a) (e) (r) (t)

Probability 0.3 0.3 0.2 0.2

We can assign unique binary codes (preferable small) to th e most probable and bigger codes to the least probable ones.

(a) (e) (r) (t)

(binary code)

Probability 0.3 0.3 0.2 0.2
0 10 110 1110

Let’s compress the stream (eat) , assuming we would spend 8 bits for each symbol, we would spend24 bitswithout any compression. But in case we replace each symbol for its code we can save space.

The first step is to encode the symbolewhich is10and the second symbol isawhich is added (not in a mathematical way)[10] [0]and finally the third symboltwhich makes our final compressed bitstream to be[0] [1110]or1001110which only requires (7 bits) (3.4 times less space than the original).

Notice that each code must be a unique prefixed codeHuffman can help you to find these numbers. Though it has some issues there arevideo codecs that still offersthis method and it’s the algorithm for many applications which requires compression.

Both encoder and decodermust knowthe symbol table with its code, therefore, you need to send the table too.

Arithmetic coding:

Let’s suppose we have a stream of the symbols:a, (e) , (r) ,sandtand their probability is represented by this table.

(a) (e) (r)

(t)

s
Probability 0.3 0.3 0. 15 0. 05 0.2

With this table in mind, we can build ranges containing al l the possible symbols sorted by the most frequents.

initial arithmetic range

Now let’s encode the streameat, we pick the first symbol (e) which is located within the subrange0.3 to 0.6(but not included) and we take this subrange and split it again using the same proportions used before but within this new range.

second sub range

Let’s continue to encode our streameat, now we take the second symbolawhich is within the new subrange0.3 to 0. 39and then we take our last symbol (t) and we do the same process again and we get the last subrange0. 354 to 0. 372.

final arithmetic range

We just need to pick a number within the last subrange0. 354 to 0. 372, let’s choose0. 36but we could choose any number within this subrange. Withonlythis number we’ll be able to recover our original streameat. If you think about it, it’s like if we were drawing a line within ranges of ranges to encode our stream.

final range traverse

Thereverse process(AKA decoding) is equally easy, with our number0. 36and our original range we can run the same process but now using this number to reveal the stream encoded behind this number.

With the first range, we notice that our number fits at the slice, therefore, it’s our first symbol, now we split this subrange again, doing the same process as before, and we’ll notice that0. 36fits the symbolaand after we repeat th e process we came to the last symbolt(forming our original encoded streameat).

Both encoder and decodermust knowthe symbol probability table, therefore you need to send the table.

Pretty neat, isn’t it? People are damn smart to come up with a such solution, somevideo codecs usethis technique (or at least offer it as an option).

The idea is to lossless compress the quantized bitstream, for sure this article is missing tons of details, reasons, trade-offs and etc. Butyou should learn moreas a developer. Newer codecs are trying to use differententropy coding algorithms like ANS.

Hands-on: CABAC vs CAVLC

You cangenerate two streams, one with CABAC and other with CAVLCandcompare the timeit took to generate each of them as well asthe final size.

6th step – bitstream format

After we did all these steps we need topack the compressed frames and context to these steps. We need to explicitly inform to the decoder aboutthe decisions taken by the encoder, such as bit depth, color space, resolution, predictions info (motion vectors, intra prediction direction), profile, level, frame rate, frame type, frame number and much more.

We’re going to study, superficially, the H. 264 Bitstream. Our first step is togenerate a minimal H. 264* (bitstream) , we can do that using our own repository andffmpeg.

./ s / ffmpeg -i /files/i/minimal.png -pix_fmt yuv 420 p / files / v / minimal_yuv 420 .H 264

*ffmpeg adds, by default, all the encoding parameter as aSEI NAL, soon we’ll define what is a NAL.

This command will generate a raw h 264 bitstream with asingle frame, (x) , with color space yuv 420 and using the following image as the frame.

used frame to generate minimal h264 bitstream

H. 264 Bitstream

The AVC (H. 264) standard defines that the information will be sent inmacro frames(in the network sense), called(NAL)(Network Abstraction Layer). The main goal of the NAL is the provision of a “network-friendly” video representation, this standard must work on TVs (stream based), the Internet (packet based) among others.

NAL units H.264

There is asynchronization markerto define the boundaries of the NAL’s units. Each synchronization marker holds a value of0x (0x) ******************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************* (0x)except to the very first one which is0x (0x) ******************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************* (0x) ******************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************* (0x). If we run thehexdumpon the generated h 264 bitstream, we can identify at least three NALs in the beginning of the file.

synchronization marker on NAL units

As we said before, the decoder needs to know not only the picture data but also the details of the video, frame, colors, used parameters, and others. Thefirst byteof each NAL defines its category andtype.

(0)

(1)

(2)

(3)

(4)

(5)

(6)

(7)

(8)

(9)

NAL type id Description
Undefined
Coded slice of a non-IDR picture
Coded slice data partition A
Coded slice data partition B
Coded slice data partition C
IDRCoded slice of an IDR picture
SEISupplemental enhancement information
SPSSequence parameter set
PPSPicture parameter set
Access unit delimiter
10 End of sequence
11 End of stream

Usually, the first NAL of a bitstream is aSPS, this type of NAL is responsible for informing the general encoding variables likeprofile,level,resolutionand others.

If we skip the first synchronization marker we can decode thefirst byteto know whattype of NALis the first one.

For instance the first byte after the synchronization marker is01100111, where the first bit ( (0) ) is to the fieldforbidden_zero_bit, the next 2 bits11) tell us the fieldnal_ref_idcwhich indicates whether this NAL is a reference field or not and the rest 5 bits (00111) inform us the fieldnal_unit_type, in this case, it’s a (SPS) ************************ (7) NAL unit.

The second byte (binary=01100100, hex=0x 64, dec=100) of an SPS NAL is the fieldprofile_idcwhich shows the profile that the encoder has used, in this case, we used theconstrained high-profile, it’s a high profile without the support of B (bi-predictive) slices.

SPS binary view

When we read the H. 264 bitstream spec for an SPS NAL we’ll find many values ​​for the (parameter name,categoryand adescription, for instance, let’s look atpic_width_in_mbs_minus_1andpic_height_in_map_units_minus_1fields.

(Category)

Parameter name Description
pic_width_in_mbs_minus_1 0 ue (v)
pic_height_in_map_units_minus_1 0 ue (v)

ue (v): unsigned integerExp-Golomb-coded

If we do some math with the value of these fields we will end up with theresolution. We can represent a1920 x 1080using apic_width_in_mbs_minus_1with the value of119 ((119 1) * macroblock_size=120 * 16=1920), again saving space, instead of encode1920we did it with119.

If we continue to examine our created video with a binary view (ex:xxd -b -c 11 v / minimal_yuv 420 .H 264), we can skip to the last NAL which is the frame itself.

We can see its first 6 bytes values:01100101 10001000 10000100 00 00 00 00 00100001 11111111. As we already know the first byte tell us about what type of NAL it is, in this case, (00 101) it’s anIDR Slice (5)and we can further inspect it:

h264 slice header spec

Using the spec info we can decode what type of slice (slice_type), the frame number (frame_num) among others important fields.

In order to get the values ​​of some fields (ue (v), me (v), se (v) or te (v)) we need to decode it using a special decoder calledExponential-Golomb, this method isvery efficient to encode variable values ​​, mostly when there are many default values.

The values ​​ofslice_typeandframe_numof this video are 7 (I slice) and 0 (the first frame).

We can see thebitstream as a protocoland if you want or need to learn more about this bitstream please refer to the (ITU H.) spec.Here’s a macro diagram which shows where the picture data (compressed YUV) resides.

h264 bitstream macro diagram

We can explore others bitstreams like theVP9 bitstream, (H.) *********************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************** ((HEVC)or even our (new best friend)(AV1) ************************ (bitstream,do they all look similar? No, but once you learned one you can easily get the others.

Hands-on: Inspect the H. 264 Bitstream

We cangenerate a single frame videoand use (mediainfo) to inspect its H. 264 Bitstream. In fact, you can even see thesource code that parses h 264 (AVC)bitstream.

mediainfo details h264 bitstream

We can also use theIntel Video Pro Analyzerwhich is paid but there is a free trial version which limits you to only the first 10 frames but that’s okay for learning purposes.

intel video pro analyzer details h264 bitstream

Review

We’ll notice that many of themodern codecs uses this same model we learned. In fact, let’s look at the Thor video codec block diagram, it contains all the steps we studied. The idea is that you now should be able to at least understand better the innovations and papers for the area.

thor_codec_block_diagram

Previously we had calculated that we needed139 GB of storage to keep a video file with one hour at 720 p resolution and 30 FPSif we use the techniques we learned here, likeinter and intra prediction, transform, quantization, entropy coding and otherwe can achieve, assuming we are spending (0.0) bit per pixel, the same perceivable quality videorequiring only 367. (MB vs) ******************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************* (GB) of store.

We choose to use0.0 (bit per pixel) based on the example video provided here.

How does H. 265 achieve a better compression ratio than H. 264?

Now that we know more about how codecs work, then it is easy to understand how new codecs are able to deliver higher resolutions with fewer bits.

We will compare AVC and HEVC, let’s keep in mind that it is almost always a trade-off between more CPU cycles (complexity) and compression rate.

HEVC has bigger and morepartitions(andsub-partitions) options than AVC, moreintra predictions directions,improved entropy codingand more, all these improvements made H. (capable to compress) ********************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************* (% more than H.) .

h264 vs h265

General architecture

general architecture

[TODO]

Progressive download and adaptive streaming

progressive download

adaptive streaming

[TODO]

Content protection

We can usea simple token systemto protect the content. The user without a token tries to request a video and the CDN forbids her or him while a user with a valid token can play the content, it works pretty similarly to most of the web authentication systems.

token protection

The sole use of this token system still allows a user to download a video and distribute it. Then theDRM (digital rights management)systems can be used to try to avoid this.

drm

In real life production systems, people often use both techniques to provide authorization and authentication.

DRM

Main systems

What?

DRM means Digital rights management, it’s a wayto provide copyright protection for digital media, for instance, digital video and audio. Although it’s used in many places (it’s not universally accepted) .

Why?

Content creator (mostly studios) want to protect its intelectual property against copy to prevent unauthorized redistribution of digital media.

How

We’re going to describe an abstract and generic form of DRM in a very simplified way.

Given acontent C1(ie an hls or dash video streaming), with aplayer P1(ie shaka-clappr, exo-player or ios) in a (device D1) ************************ (ie a smartphone, TV, tablet or desktop / notebook (using a) DRM system DRM1(widevine, playready or FairPlay).

The content C1 is encrypted with asymmetric-key K1from the system DRM1, generating theencrypted content C’1.

drm general flow

The player P1, of a device D1, has two keys (asymmetric), aprivate key PRK1(this key is protected (1) and only known by (D1) ) and apublic key PUK1.

(1) protected: this protection can bevia hardware, for instance, this key can be stored inside a special (read-only) chip that works like (a black-box) to provide decryption, orby software(less safe), the DRM system provides means to know which type of protection a given device has.

When theplayer P1 wants to playthecontent C’1, it needs to deal with theDRM system DRM1, giving its public keyPUK1. The DRM system DRM1 returns thekey K1 encryptedwith the client”s public keyPUK1. In theory, this response is something thatonly D1 is capable of decrypting.

K1P1D1=enc (K1, PUK1)

P1uses its DRM local system (it could be aSOC, a specialized hardware or software), this system isable to decryptthe content using its private key PRK1, it can decryptthe symmetric-key K1 from the K1P1D1andplay C’1. At best case, the keys are not exposed through RAM.

K1=dec (K1P1D1, PRK1)  P1.play (dec (C'1, K1))

drm decoder flow

Make sure you havedocker installedand just run./ s / start_jupyter.shand follow the instructions on the terminal.

The richest content is here, it’s where all the info we saw in this text was extracted, based or inspired by. You can deepen your knowledge with these amazing links, books, videos and etc.

Online Courses and Tutorials:

Books:

Bitstream Specifications:

Software:

Non-ITU Codecs:

Encoding Concepts:

Video Sequences for Testing:

Miscellaneous:

  

Brave Browser
Read More
Payeer

What do you think?

Leave a Reply

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

GIPHY App Key not set. Please check settings

karlkeefer / pngr, Hacker News

karlkeefer / pngr, Hacker News

Maharashtra Govt Formation LIVE Updates: NCP top brass in huddle at Sharad Pawar's residence; BJP claims Ajit Pawar has support of all hi MLAs – Firstpost, Firstpost.com

Maharashtra Govt Formation LIVE Updates: NCP top brass in huddle at Sharad Pawar's residence; BJP claims Ajit Pawar has support of all hi MLAs – Firstpost, Firstpost.com