
VP8 是 Google 推出的开源免费无损专利视频编码标准2010 年发布前身 On2 TrueMotion VP7主要配套容器WebM.webm也可封装进 MKV。VP8 基于块的混合视频编码标准仅支持 YUV 4:2:0 8 比特采样无 B 帧仅 I/P 帧。 整套码流由独立帧构成每一帧划分为若干码流分区 (Partition)分区间可并行解码。 解码器核心流水线BoolCoder 熵解码 → 预测模式 / MV 解码 → 残差反量化逆变换 → 像素预测叠加 → 环路滤波输出重建图像。一、整体结构层次VP8 码流层级从大到小码流 → 帧段 (Frame) → 宏块行 (Macroblock Row) → 宏块 (MB 16x16) → 子块 (4x4/8x8)┌─────────────────────────────────────────────────────────────────┐ │ VP8 Frame Bitstream Layout │ ├─────────────────────────────────────────────────────────────────┤ │ │ │ ┌──────────────┐ ┌──────────────────────────┐ │ │ │ VP8_HEADER │ │ Partition 0 │ │ │ │ (3 bytes) │ │ (first_partition_length) │ │ │ ├──────────────┤ ├──────────────────────────┤ │ │ │ type0/1 │ │ - Frame Tag (4 bytes) │ │ │ │ version │ │ - Frame Dimensions │ │ │ │ show_frame │ │ - Quantization Params │ │ │ │ partition_len│ │ - Loop Filter Params │ │ │ └──────────────┘ │ - Probability Tables │ │ │ └──────────────────────────┘ │ │ │ │ ┌─────────────────────────────────────────────────────────┐ │ │ │ Partitions 1-3 (Macroblock Data) │ │ │ │ ┌──────────┬──────────┬──────────┐ │ │ │ │ │ MB 0 │ MB 1 │ MB N-1 │ │ │ │ │ │ ModeMV │ ModeMV │ ModeMV │ │ │ │ │ │ Residual │ Residual │ Residual │ │ │ │ │ └──────────┴──────────┴──────────┘ │ │ │ └─────────────────────────────────────────────────────────┘ │ │ │ └─────────────────────────────────────────────────────────────────┘1.1 FrameHEADERtypedef struct { unsigned int type : 1; // 帧类型: 0关键帧, 1非关键帧 unsigned int version : 3; // 版本号 unsigned int show_frame : 1; // 是否显示此帧 unsigned int first_partition_length_in_bytes : 19; // 第一分区长度(最大8Mb) } VP8_HEADER;字段位宽值范围说明type10 或 1帧类型0关键帧Key Frame1非关键帧Inter Frameversion30-7版本号当前 VP8 版本通常为 0show_frame10 或 1显示标志0不显示1显示此帧first_partition_length_in_bytes190-524,287第一分区长度最大约 512KB2^19 524,288 字节1.2 key frame独立解码不依赖任何参考帧完整图像包含完整的帧内预测信息重置状态重置所有参考帧和概率表I 帧类似于 H.264 的 I 帧┌─────────────────────────────────────────────────────────────────┐ │ VP8 Key Frame Structure │ ├─────────────────────────────────────────────────────────────────┤ │ │ │ ┌─────────────────────────────────────────────────────────┐ │ │ │ VP8_HEADER (3 bytes) │ │ │ │ type0 (KEY_FRAME), version, show_frame, part_len │ │ │ └───────────────────┬─────────────────────────────────────┘ │ │ │ │ │ ▼ │ │ ┌─────────────────────────────────────────────────────────┐ │ │ │ Partition 0 (帧头分区) │ │ │ │ ┌───────────────────────────────────────────────────┐ │ │ │ │ │ Start Code: 0x9D 0x01 0x2A │ │ │ │ │ │ (3 bytes) │ │ │ │ │ ├───────────────────────────────────────────────────┤ │ │ │ │ │ Frame Width (14 bits) Horizontal Scale (2 bits)│ │ │ │ │ │ Frame Height (14 bits) Vertical Scale (2 bits) │ │ │ │ │ │ (4 bytes total) │ │ │ │ │ ├───────────────────────────────────────────────────┤ │ │ │ │ │ Clamp Type (1 bit) │ │ │ │ │ ├───────────────────────────────────────────────────┤ │ │ │ │ │ Segmentation Enabled (1 bit) │ │ │ │ │ │ Segmentation Data (optional) │ │ │ │ │ ├───────────────────────────────────────────────────┤ │ │ │ │ │ Loop Filter Parameters │ │ │ │ │ │ - filter_type (1 bit) │ │ │ │ │ │ - filter_level (6 bits) │ │ │ │ │ │ - sharpness_level (3 bits) │ │ │ │ │ ├───────────────────────────────────────────────────┤ │ │ │ │ │ Quantization Parameters │ │ │ │ │ │ - base_qindex (7 bits) │ │ │ │ │ │ - delta-q values (optional) │ │ │ │ │ ├───────────────────────────────────────────────────┤ │ │ │ │ │ Entropy Probability Tables │ │ │ │ │ │ - Coefficient Probs │ │ │ │ │ │ - Mode Probs │ │ │ │ │ └───────────────────────────────────────────────────┘ │ │ │ └───────────────────┬─────────────────────────────────────┘ │ │ │ │ │ ▼ │ │ ┌─────────────────────────────────────────────────────────┐ │ │ │ Partitions 1-N (宏块数据) │ │ │ │ ┌───────────────────────────────────────────────────┐ │ │ │ │ │ Macroblock Mode Info │ │ │ │ │ │ - Intra Prediction Mode │ │ │ │ │ │ - Y Mode, UV Mode │ │ │ │ │ ├───────────────────────────────────────────────────┤ │ │ │ │ │ Residual Data (DCT Coefficients) │ │ │ │ │ │ - Y plane (16x16) │ │ │ │ │ │ - U plane (8x8) │ │ │ │ │ │ - V plane (8x8) │ │ │ │ │ └───────────────────────────────────────────────────┘ │ │ │ └─────────────────────────────────────────────────────────┘ │ │ │ └─────────────────────────────────────────────────────────────────┘1.3.1 起始码Start Code作为关键帧的同步标记解码器通过此序列识别关键帧的开始。/* Start / synch code */ cx_data[0] 0x9D; cx_data[1] 0x01; cx_data[2] 0x2a;1.3.2 帧尺寸与缩放因子┌─────────────────────────────────────────────────────────┐ │ 字节 3-4: 宽度信息 │ │ ┌─────────────────────────────────────────────────┐ │ │ │ bit 15-14: Horizontal Scale (0-3) │ │ │ │ bit 13-0: Width (0-16383) │ │ │ └─────────────────────────────────────────────────┘ │ ├─────────────────────────────────────────────────────────┤ │ 字节 5-6: 高度信息 │ │ ┌─────────────────────────────────────────────────┐ │ │ │ bit 15-14: Vertical Scale (0-3) │ │ │ │ bit 13-0: Height (0-16383) │ │ │ └─────────────────────────────────────────────────┘ │ └─────────────────────────────────────────────────────────┘缩放因子说明值缩放比例说明01/1无缩放11/2水平/垂直缩小一半21/4缩小到 1/431/8缩小到 1/81.3.3 Clamp Type钳位类型钳位类型RECON_CLAMP_REQUIRED (0)重建像素需要钳位RECON_CLAMP_NOTREQUIRED (1)重建像素不需要钳位1.3.4 环路滤波参数参数位宽值范围说明filter_type10-1滤波类型0普通1简单filter_level60-63滤波强度0禁用63最强sharpness_level30-7锐化程度1.3.5 量化参数关键帧使用基础量化索引base_qindex量化索引范围0最高质量~ 127最高压缩率1.3 非关键帧Inter Frame非关键帧Inter Frame是 VP8 编码流中的依赖帧需要参考之前解码的帧进行预测。它通过帧间预测和运动补偿实现高效压缩是 VP8 实现高压缩率的核心。依赖参考帧使用 Last/Golden/AltRef 参考帧差分编码只编码与参考帧的差异P 帧类似于 H.264 的 P 帧非关键帧的设计体现了 VP8 的高效压缩策略帧间预测利用时间冗余只编码与参考帧的差异多参考帧支持 Last/Golden/AltRef 三个参考帧提高预测精度灵活的参考帧更新选择性更新参考帧平衡压缩效率和错误恢复能力运动向量编码优化小 MV 用查表大 MV 直接编码┌─────────────────────────────────────────────────────────────────┐ │ VP8 Inter Frame Structure │ ├─────────────────────────────────────────────────────────────────┤ │ │ │ ┌─────────────────────────────────────────────────────────┐ │ │ │ VP8_HEADER (3 bytes) │ │ │ │ type1 (INTER_FRAME), version, show_frame, part_len │ │ │ └───────────────────┬─────────────────────────────────────┘ │ │ │ │ │ ▼ │ │ ┌─────────────────────────────────────────────────────────┐ │ │ │ Partition 0 (帧头分区) │ │ │ │ ┌───────────────────────────────────────────────────┐ │ │ │ │ │ Segmentation Enabled (1 bit) │ │ │ │ │ │ Segmentation Data (optional) │ │ │ │ │ ├───────────────────────────────────────────────────┤ │ │ │ │ │ Loop Filter Parameters │ │ │ │ │ │ - filter_type (1 bit) │ │ │ │ │ │ - filter_level (6 bits) │ │ │ │ │ │ - sharpness_level (3 bits) │ │ │ │ │ ├───────────────────────────────────────────────────┤ │ │ │ │ │ Reference Frame Updates │ │ │ │ │ │ - refresh_golden_frame (1 bit) │ │ │ │ │ │ - refresh_alt_ref_frame (1 bit) │ │ │ │ │ │ - copy_buffer_to_gf (2 bits) │ │ │ │ │ │ - copy_buffer_to_arf (2 bits) │ │ │ │ │ │ - ref_frame_sign_bias (2 bits) │ │ │ │ │ ├───────────────────────────────────────────────────┤ │ │ │ │ │ refresh_entropy_probs (1 bit) │ │ │ │ │ ├───────────────────────────────────────────────────┤ │ │ │ │ │ refresh_last_frame (1 bit) │ │ │ │ │ ├───────────────────────────────────────────────────┤ │ │ │ │ │ Quantization Parameters │ │ │ │ │ │ - base_qindex (7 bits) │ │ │ │ │ │ - delta-q values (optional) │ │ │ │ │ └───────────────────────────────────────────────────┘ │ │ │ └───────────────────┬─────────────────────────────────────┘ │ │ │ │ │ ▼ │ │ ┌─────────────────────────────────────────────────────────┐ │ │ │ Partitions 1-N (宏块数据) │ │ │ │ ┌───────────────────────────────────────────────────┐ │ │ │ │ │ Macroblock Mode Info │ │ │ │ │ │ - Prediction Mode (NEARESTMV/NEARMV/ZEROMV/ │ │ │ │ │ │ NEWMV/SPLITMV) │ │ │ │ │ │ - Reference Frame (Last/Golden/AltRef) │ │ │ │ │ │ - Motion Vectors (row, col) │ │ │ │ │ ├───────────────────────────────────────────────────┤ │ │ │ │ │ Residual Data (DCT Coefficients) │ │ │ │ │ │ - Y plane (16x16) │ │ │ │ │ │ - U plane (8x8) │ │ │ │ │ │ - V plane (8x8) │ │ │ │ │ └───────────────────────────────────────────────────┘ │ │ │ └─────────────────────────────────────────────────────────┘ │ │ │ └─────────────────────────────────────────────────────────────────┘1.3.1 参考帧更新控制参数说明refresh_golden_frame是否用当前帧更新 Golden 参考帧refresh_alt_ref_frame是否用当前帧更新 AltRef 参考帧copy_buffer_to_gf不从当前帧更新时从哪个缓冲区复制到 Goldencopy_buffer_to_arf不从当前帧更新时从哪个缓冲区复制到 AltRefcopy_buffer_to_gf/arf 值含义值来源0不复制1从 Last 帧复制2从 Golden/AltRef 帧复制1.3.2 Last帧更新refresh_last_frame是否用当前帧更新 Last 参考帧最近解码帧1.3.3 熵概率表更新refresh_entropy_probs是否重置/更新熵编码概率表1.3.4 参考帧类型typedef enum { INTRA_FRAME 0, // 帧内预测无参考 LAST_FRAME 1, // 最近解码帧 GOLDEN_FRAME 2, // 黄金参考帧 ALTREF_FRAME 3, // 备用参考帧 MAX_REF_FRAMES 4 } MV_REFERENCE_FRAME;┌─────────────────────────────────────────────────────────┐ │ Reference Frame Management │ ├─────────────────────────────────────────────────────────┤ │ │ │ Frame Buffer Pool │ │ ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐ │ │ │ Buffer0 │ │ Buffer1 │ │ Buffer2 │ │ Buffer3 │ │ │ └────┬────┘ └────┬────┘ └────┬────┘ └────┬────┘ │ │ │ │ │ │ │ │ ▼ ▼ ▼ ▼ │ │ ┌───────────────────────────────────────────────┐ │ │ │ lst_fb_idx → Last Frame (最近解码帧) │ │ │ │ gld_fb_idx → Golden Frame (高质量参考帧) │ │ │ │ alt_fb_idx → AltRef Frame (备用参考帧) │ │ │ └───────────────────────────────────────────────┘ │ │ │ │ 更新策略示例: │ │ ├── refresh_last_frame1 → 当前帧 → Last │ │ ├── refresh_golden_frame1 → 当前帧 → Golden │ │ ├── copy_buffer_to_gf1 → Last → Golden │ │ └── copy_buffer_to_arf2 → Golden → AltRef │ │ │ └─────────────────────────────────────────────────────────┘1.4 宏块Macroblock宏块Macroblock是 VP8 编码的基本处理单元每个宏块大小为16x16 像素。它是帧内预测、帧间预测、变换编码和熵编码的核心操作单元。┌─────────────────────────────────────────────────────────────────┐ │ Macroblock Data Structures │ ├─────────────────────────────────────────────────────────────────┤ │ │ │ ┌─────────────────────────────────────────────────────────┐ │ │ │ MACROBLOCKD │ │ │ │ (宏块解码状态) │ │ │ │ ┌───────────────────────────────────────────────────┐ │ │ │ │ │ MODE_INFO *mode_info_context │ │ │ │ │ │ └── MB_MODE_INFO mbmi (宏块模式信息) │ │ │ │ │ │ └── union b_mode_info bmi[16] (块模式信息) │ │ │ │ │ ├───────────────────────────────────────────────────┤ │ │ │ │ │ BLOCKD block[25] (25个块的解码状态) │ │ │ │ │ │ ├── block[0-15]: Y平面 4x4 块 │ │ │ │ │ │ ├── block[16-19]: U平面 4x4 块 │ │ │ │ │ │ ├── block[20-23]: V平面 4x4 块 │ │ │ │ │ │ └── block[24]: DC二阶变换块 │ │ │ │ │ ├───────────────────────────────────────────────────┤ │ │ │ │ │ short qcoeff[400] (量化系数) │ │ │ │ │ │ short dqcoeff[400] (反量化系数) │ │ │ │ │ │ char eobs[25] (每块非零系数数) │ │ │ │ │ ├───────────────────────────────────────────────────┤ │ │ │ │ │ short dequant_y1[16] (Y1反量化表) │ │ │ │ │ │ short dequant_y2[16] (Y2反量化表) │ │ │ │ │ │ short dequant_uv[16] (UV反量化表) │ │ │ │ │ └───────────────────────────────────────────────────┘ │ │ │ └─────────────────────────────────────────────────────────┘ │ │ │ └─────────────────────────────────────────────────────────────────┘1.4.1宏块结构信息typedef struct { uint8_t mode, uv_mode; // Y 和 UV 的预测模式 uint8_t ref_frame; // 参考帧类型Intra/Last/Golden/AltRef uint8_t is_4x4; // 是否为 4x4 块模式 int_mv mv; // 运动向量 uint8_t partitioning; // 分区类型 uint8_t mb_skip_coeff; // 是否跳过系数解码0需要解码1跳过 uint8_t need_to_clamp_mvs; // 是否需要钳位运动向量 uint8_t segment_id; // 分段 ID } MB_MODE_INFO;1.4.2 宏块像素布局┌─────────────────────────────────────────────────────────┐ │ Macroblock Pixel Structure (16x16) │ ├─────────────────────────────────────────────────────────┤ │ │ │ Y Plane (16x16 256 pixels) │ │ ┌──────┬──────┬──────┬──────┐ │ │ │ 0 │ 1 │ 2 │ 3 │ │ │ │ 4x4 │ 4x4 │ 4x4 │ 4x4 │ ← BLOCKD block[0-3] │ │ ├──────┼──────┼──────┼──────┤ │ │ │ 4 │ 5 │ 6 │ 7 │ │ │ │ 4x4 │ 4x4 │ 4x4 │ 4x4 │ ← BLOCKD block[4-7] │ │ ├──────┼──────┼──────┼──────┤ │ │ │ 8 │ 9 │ 10 │ 11 │ │ │ │ 4x4 │ 4x4 │ 4x4 │ 4x4 │ ← BLOCKD block[8-11] │ │ ├──────┼──────┼──────┼──────┤ │ │ │ 12 │ 13 │ 14 │ 15 │ │ │ │ 4x4 │ 4x4 │ 4x4 │ 4x4 │ ← BLOCKD block[12-15]│ │ └──────┴──────┴──────┴──────┘ │ │ │ │ U Plane (8x8 64 pixels) V Plane (8x8 64 pixels)│ │ ┌──────┬──────┐ ┌──────┬──────┐ │ │ │ 16 │ 17 │ │ 20 │ 21 │ │ │ │ 4x4 │ 4x4 │ │ 4x4 │ 4x4 │ │ │ ├──────┼──────┤ ├──────┼──────┤ │ │ │ 18 │ 19 │ │ 22 │ 23 │ │ │ │ 4x4 │ 4x4 │ │ 4x4 │ 4x4 │ │ │ └──────┴──────┘ └──────┴──────┘ │ │ ← BLOCKD block[16-19] ← BLOCKD block[20-23] │ │ │ │ DC Second-Order Block │ │ ┌──────────────────────────────────────┐ │ │ │ block[24] (DC变换块) │ │ │ └──────────────────────────────────────┘ │ │ │ │ Total: 25 blocks │ │ Total pixels: 256 (Y) 64 (U) 64 (V) 384 │ │ │ └─────────────────────────────────────────────────────────┘块编号与平面映射块索引平面位置尺寸0-15Y16x16 区域4x416-19U8x8 区域4x420-23V8x8 区域4x424Y2 (DC)-4x4二阶变换二 宏块特征2.1 帧内预测模式typedef enum { DC_PRED, // DC 预测平均值 V_PRED, // 垂直预测 H_PRED, // 水平预测 TM_PRED, // TrueMotion 预测 B_PRED, // 4x4 块预测每个块独立 // ... 帧间模式 } MB_PREDICTION_MODE;帧内预测方向示意DC_PRED: ┌───┬───┐ V_PRED: ┌───┬───┐ H_PRED: ┌───┬───┐ │ A │ B │ │ A │ A │ │ A │ B │ ├───┼───┤ ├───┼───┤ ├───┼───┤ │ C │ X │ X(ABC)/3│ C │ C │ XA │ C │ C │ └───┴───┘ └───┴───┘ └───┴───┘2.2 帧间预测模式模式说明NEARESTMV使用最近邻宏块的运动向量NEARMV使用次近邻宏块的运动向量ZEROMV使用零运动向量NEWMV编码新的运动向量SPLITMV宏块分成 4 个子块各有独立运动向量2.2 系数存储与Zigzag扫描2.2.1 系数布局每个 4x4 块包含 16 个 DCT 系数按 Zigzag 顺序存储const uint8_t kZigzag[16] { 0, 1, 4, 8, 5, 2, 3, 6, 9, 12, 13, 10, 7, 11, 14, 15 };Zigzag 扫描顺序原始顺序: Zigzag顺序: ┌───┬───┬───┬───┐ ┌───┬───┬───┬───┐ │ 0 │ 1 │ 2 │ 3 │ │ 0 │ 1 │ 4 │ 8 │ ├───┼───┼───┼───┤ ├───┼───┼───┼───┤ │ 4 │ 5 │ 6 │ 7 │ │ 5 │ 2 │ 3 │ 6 │ ├───┼───┼───┼───┤ → ├───┼───┼───┼───┤ │ 8 │ 9 │10 │11 │ │ 9 │12 │13 │10 │ ├───┼───┼───┼───┤ ├───┼───┼───┼───┤ │12 │13 │14 │15 │ │ 7 │11 │14 │15 │ └───┴───┴───┴───┘ └───┴───┴───┴───┘2.2.2 系数缓冲区// MACROBLOCKD 中的系数缓冲区 DECLARE_ALIGNED(16, short, qcoeff[400]); // 量化系数 (25块 × 16系数) DECLARE_ALIGNED(16, short, dqcoeff[400]); // 反量化系数 DECLARE_ALIGNED(16, char, eobs[25]); // 每块非零系数数 (End of Block)2.3 宏块级特性2.3.1 分段SegmentationVP8 支持将帧分成最多 4 个分段每个分段可使用不同的量化参数和环路滤波强度unsigned char segmentation_enabled; unsigned char segment_id; // 当前宏块的分段 ID signed char segment_feature_data[MB_LVL_MAX][MAX_MB_SEGMENTS]; // 分段特征数据2.3.2 环路滤波 Delta根据预测模式和参考帧类型调整滤波强度signed char ref_lf_deltas[MAX_REF_LF_DELTAS]; // 参考帧相关 Delta signed char mode_lf_deltas[MAX_MODE_LF_DELTAS]; // 模式相关 Delta2.3.3 跳过系数当mb_skip_coeff 1时表示该宏块没有残差数据只需进行预测if (xd-mode_info_context-mbmi.mb_skip_coeff) { vp8_reset_mb_tokens_context(xd); // 跳过系数解码 }2.3.4 主要特性一览特性说明16x16 基本单元平衡计算复杂度和压缩效率25 个 4x4 块16Y 4U 4V 1DC 二阶变换帧内/帧间预测灵活的预测模式选择Zigzag 扫描优化零系数的熵编码分段支持自适应量化和滤波跳过系数对于简单区域跳过残差编码