Digital Video Introduction

repository·master·Indexed 12 days ago

https://github.com/leandromoreira/digital_video_introduction

A pedagogical resource for software developers and engineers covering fundamental digital video technology. Topics include color models (RGB, YUV), chroma subsampling, bit rate calculations, and the 4-step compression pipeline: picture partitioning, predictions (intra and inter), Discrete Cosine Transform (DCT), and quantization. Includes guidance on using FFmpeg and Intel Video Pro Analyzer to visualize motion vectors and macroblocks.

Tokens
38K
Snippets
72
Records
209
Agent score
92%

What's inside Digital Video Introduction

  1. Review Video Codec History

    master

    The evolution of video compression includes several key standards:

    • H.261 (1990): Designed for 64 kbit/s; introduced chroma subsampling and macro blocks.
    • H.263 (1995): An extension of H.261.
    • H.264/AVC (2003): A major industry standard.
    • VP8 (2008): Released by Google (formerly On2 Technologies).
    • VP9 (2012): Released by Google; widely supported in browsers.
    • AV1: A modern, royalty-free, open-source codec developed by the Alliance for Open Media (AOMedia) (including Google, Amazon, Netflix, Microsoft, etc.).

    You can analyze the AV1 codec directly in your browser at arewecompressedyet.com/analyzer/.

  2. Understand the steps of a generic video codec

    master

    A generic video codec operates through three main stages to compress video data:

    1. Image Partitioning: Dividing the frame into partitions, sub-partitions, slices (or tiles), and macroblocks (or coding tree units). This allows for more precise predictions (e.g., small partitions for moving parts and large partitions for static backgrounds).
      • HEVC maximum partition size: 64x64
      • AVC maximum partition size: 16x16
      • Sub-partitions can reach as small as 4x4.
    2. Prediction: Making predictions on the partitions.
      • Inter prediction (temporal redundancy): Sends motion vectors and the residual.
      • Intra prediction (spatial redundancy): Sends the prediction direction and the residual.
    3. Transformation: Transforming the residual block (calculated as predicted partition - actual partition) to identify which pixels can be discarded while maintaining overall quality.
  3. Entropy Coding: VLC (Variable Length Coding)

    master

    Entropy coding is the final step in video compression, applied after quantization to compress data losslessly.

    Variable Length Coding (VLC) works by assigning shorter binary codes to symbols with higher probabilities and longer codes to symbols with lower probabilities.

    Key Requirements:

    • Prefix Property: Every binary code must have a unique prefix so that the decoder can identify symbol boundaries without separators.
    • Symbol-Code Table: Both the encoder and the decoder must possess the same symbol-to-code mapping table to ensure successful reconstruction.

    Example:

    For the word eat with specific symbol probabilities, instead of using fixed 8-bit characters (24 bits total), VLC can represent the sequence using much fewer bits (e.g., 7 bits) by using variable-length binary strings.

    |                | a   | e   | r   | t    |
    |----------------|-----|-----|-----|------|
    | probabilità    | 0,3 | 0,3 | 0,2 | 0,2  |
    | codice binario | 0   | 10  | 110 | 1110 |
  4. How Inter-prediction and Motion Estimation work

    master

    Inter-prediction is a technique used to reduce temporal redundancy (changes between consecutive frames).

    Instead of simply subtracting one frame from another to get a delta, modern codecs use Motion Estimation:

    1. The current frame is divided into blocks.
    2. The algorithm attempts to match these blocks to blocks in a previous frame.
    3. A Motion Vector (x, y) is calculated to represent the movement of the block.
    4. To save even more bits, the codec can encode only the difference between the current motion vector and the previous one.

    This method is significantly more efficient than simple frame subtraction because it describes movement rather than just pixel changes.

  5. Understand Digital Rights Management (DRM) workflows

    master

    DRM is used to protect digital media from unauthorized distribution. While token-based authentication protects access to a stream, DRM protects the content itself via encryption.

    Major DRM Systems

    • FPS (FairPlay Streaming) - Apple
    • PR (PlayReady) - Microsoft
    • WV (Widevine) - Google

    The DRM Workflow

    1. Encryption: Content ($C_1$) is encrypted by a DRM system using a symmetric key ($K_1$) to produce encrypted content ($C'_1$).
    2. Key Exchange: The player ($P_1$) on a device ($D_1$) sends its public key ($PUK_1$) to the DRM system. The DRM system returns the symmetric key $K_1$ encrypted with the player's public key: $K_{1P1D1} = ext{enc}(K_1, PUK_1)$.
    3. Decryption: The device uses its protected private key ($PRK_1$) to decrypt the symmetric key: $K_1 = ext{dec}(K_{1P1D1}, PRK_1)$.
    4. Playback: The player uses $K_1$ to decrypt the content: $P_1. ext{play}( ext{dec}(C'_1, K_1))$.

    Protection can be implemented via software or more securely via hardware (e.g., storing keys in a secure chip/SoC where they cannot be exported from memory).

  6. How spatial redundancy (intra-prediction) works

    master

    Spatial redundancy refers to the correlation between neighboring pixels within a single frame. To reduce data, codecs use intra-prediction (帧内预测).

    In an I-frame (where no previous frames are used for prediction), the codec analyzes a block of pixels and estimates the color of unknown pixels based on their neighbors (e.g., assuming color remains consistent vertically or horizontally).

    Because the prediction is rarely perfect, the codec calculates a residual (the difference between the predicted color and the actual color). Encoding this residual matrix is much more efficient than encoding the raw pixel data.

  7. Protect content using Token Systems vs DRM

    master

    Content protection can be implemented at different levels of security:

    1. Token Systems: A simple authentication mechanism where a user must provide a valid token to request a video. The CDN blocks unauthorized requests. While this handles authorization, it does not prevent a user from downloading and redistributing the video once they have access.
    2. DRM (Digital Rights Management): A more robust method used to add copyright protection to digital content (video and audio) to prevent unauthorized copying and redistribution.

    Real-world production systems typically use both techniques together to provide both authorization and authentication.

  8. Understand Chroma Subsampling ratios and schemes

    master

    Chroma subsampling is a technique used to reduce video data size by encoding chrominance (color) at a lower resolution than luminance (brightness). This exploits the human eye's lower sensitivity to color detail.

    Subsampling schemes are expressed as a three-part ratio a:x:y relative to a block of a x 2 luminance pixels:

    • a: Horizontal sampling reference (usually 4).
    • x: Number of chrominance samples in the first row of a pixels.
    • y: Number of changes in chrominance samples between the first and second rows of a pixels.

    Common schemes used in modern codecs:

    • 4:4:4: No subsampling (highest quality).
    • 4:2:2, 4:1:1, 4:2:0, 4:1:0, 3:1:1.

    Note on 4:1:0: This is an exception that provides a single chrominance sample within every 4 x 4 luma block.

    Impact on Data Size: Using YCbCr 4:2:0 can reduce the data size by half compared to uncompressed RGB. For example, a 720p 30fps video drops from 24 bits per pixel to 12 bits per pixel, reducing a 1-hour file from 278 GB to 139 GB.

  9. Understand Temporal Redundancy (Inter Prediction)

    master

    Temporal redundancy refers to repetitions in time between consecutive frames. This is addressed using inter prediction techniques to reduce the number of bits required to encode a sequence of frames.

    Key methods include:

    • Delta Encoding: Subtracting one frame from another (e.g., frame 0 - frame 1) to encode only the residual (the difference).
    • Motion Estimation: Dividing a frame into partitions (blocks) and finding where those blocks moved in the next frame. The movement is described by motion vectors (x, y values).
    • Motion Vector Difference: To save even more bits, codecs can encode only the difference between the last known block position and the predicted position.

    Note that motion estimation is rarely perfect due to 3D movement (e.g., objects changing size as they move to the background), so a residual is still calculated to correct the prediction.

    # Conceptual example of motion vector difference
    # If a block moves from x=0, y=25 to x=7, y=26:
    # Motion Vector = (7, 1)
    # Encoded as the difference: (7-0, 26-25) = (7, 1)
  10. Understand Chroma Subsampling (Color Discretization)

    master

    Chroma subsampling is a technique used to reduce the amount of data required for an image by using a lower resolution for color components than for the brightness (luma) component. This is possible because the human eye is less sensitive to color detail.

    Subsampling schemes are typically expressed as a ratio a:x:y, which defines the color resolution relative to a block of a * 2 luma pixels:

    • a: Horizontal width (usually 4).
    • x: Number of color samples in the first row.
    • y: Number of color changes between the first and second row.

    Note: In the 4:1:0 scheme, a single color is chosen for every 4 x 4 luma resolution block.

    Commonly used formats in modern codecs include:

    • 4:4:4 (No subsampling)
    • **4:2:2$
    • **4:1:1$
    • **4:2:0$
    • **4:1:0$
    • **3:1:1$

    Using YCbCr 4:2:0 can significantly reduce file size. For example, a 720p/30fps video that might require 278GB of raw data can be reduced to approximately 139GB by using 12 bits per pixel instead of 24.

  11. Use Arithmetic Coding for lossless compression

    master

    Arithmetic Coding is a sophisticated lossless compression method that represents an entire sequence of symbols as a single fractional number within a range (typically between 0 and 1).

    How it works:

    1. Interval Partitioning: The entire range [0, 1) is partitioned into sub-intervals based on the probability of each symbol.
    2. Encoding: For each symbol in the stream, the current interval is narrowed down to the sub-interval corresponding to that symbol. This process repeats for every symbol.
    3. Result: The final compressed data is a single number (e.g., 0.36) that falls within the final narrowed interval.
    4. Decoding: The decoder uses the number and the original probability table to iteratively determine which sub-intervals the number falls into, thereby reconstructing the original symbol stream.

    Requirement: Like VLC, both the encoder and decoder must have access to the symbol probability table.

    Note: Many modern video codecs use advanced versions of this, such as CABAC (Context-adaptive binary arithmetic coding).

  12. Compare H.264 (AVC) and H.265 (HEVC) compression

    master

    H.265 (HEVC) achieves significantly better compression efficiency (often 50% or more) compared to H.264 (AVC) by using more complex computational methods.

    Key improvements in HEVC include:

    • Larger and more varied partition and sub-partition options.
    • More advanced directional intra prediction and angular intra prediction.
    • Improved entropy coding.

    Note that the increased compression efficiency comes at the cost of higher CPU complexity (more cycles required for encoding/decoding).