音视频开发—H264 SPS 和 PPS 参数说明

在H.264视频编码标准中,SPS和PPS是关键的参数集,它们提供了解码所需的各种配置信息。它们分别存储在单独的NALU中,并用于初始化解码器的参数。

序列参数集 (SPS, Sequence Parameter Set)

SPS包含关于整个视频序列的全局参数,包括视频的分辨率、帧率、颜色空间等信息。SPS NALU的 nal_unit_type 为7。

SPS的主要内容:
  1. profile_idc (8 bits)

    • 指定编码器使用的特性集(Profile),如Baseline、Main、High等。

    如下图所示

    Profile描述了H.264编码器使用的特性集,定义了编码过程中允许使用的技术和工具。不同的Profile适用于不同的应用场景,主要包括以下几种:

    1. Baseline Profile
      • 适用于低延迟和低复杂度的应用,如视频会议、移动视频等。
      • 特点:支持I帧和P帧,不支持B帧;支持逐行扫描,不支持隔行扫描;支持基本的熵编码(CAVLC),不支持高级的CABAC。
    2. Main Profile
      • 适用于广播电视和存储等应用。
      • 特点:支持I帧、P帧和B帧;支持隔行扫描;支持高级的熵编码(CABAC),提供更高的压缩效率。
    3. High Profile
      • 适用于高清电视、蓝光光盘等高质量视频应用。
      • 特点:在Main Profile的基础上增加了8x8整数变换、更多的预测模式和量化矩阵等高级功能,进一步提高压缩效率和视频质量。

    此外,还有其他更专业的Profile,如Extended Profile、High 10 Profile、High 4:2:2 Profile、High 4:4:4 Predictive Profile等,用于特定的专业应用场景。

    在这里插入图片描述

  2. constraint_set_flags (6 bits)

    • 用于指示某些编码限制和特性。
  3. level_idc (8 bits)

    • 指定解码器的性能要求(Level),如1, 1.1, 1.2, …, 5.1等。

    不同的level对应的分辨率,帧率如下图所示

    在这里插入图片描述

  4. seq_parameter_set_id

    • 标识符,用于区分不同的SPS。
  5. log2_max_frame_num_minus4

    • 定义 MaxFrameNum 的值,即最大帧编号。
  6. pic_order_cnt_type

    • 指定帧内图像顺序计数类型。
  7. log2_max_pic_order_cnt_lsb_minus4(当 pic_order_cnt_type 为0时有效):

    • 定义 MaxPicOrderCntLsb 的值。
  8. max_num_ref_frames

    • 最大参考帧数量。
  9. gaps_in_frame_num_value_allowed_flag

    • 指示是否允许帧编号之间有空隙。
  10. frame_mbs_only_flag

    • 指示视频序列是否仅包含帧宏块(不包含场宏块)。
  11. direct_8x8_inference_flag

    • 指示是否可以在直接模式中使用8x8块。
  12. frame_cropping_flag

    • 指示是否存在帧裁剪参数。
  13. vui_parameters_present_flag

    • 指示是否存在VUI(视频可用性信息)参数。
  14. VUI参数(如果 vui_parameters_present_flag 为1):

    • 包含色彩描述、时间尺度、比特率、视频格式等额外信息。

一些补充的参数如下图所示:

在这里插入图片描述

图像参数集 (PPS, Picture Parameter Set)

PPS包含关于每个图像或图像组的具体参数,指导解码器如何处理图像数据。PPS NALU的 nal_unit_type 为8。

PPS的主要内容:
  1. pic_parameter_set_id
    • 标识符,用于区分不同的PPS。
  2. seq_parameter_set_id
    • 关联的SPS的标识符。
  3. entropy_coding_mode_flag
    • 指示是否使用CABAC(上下文自适应二进制算术编码)熵编码模式。
  4. bottom_field_pic_order_in_frame_present_flag
    • 指示是否在帧内包含底部场顺序信息。
  5. num_slice_groups_minus1
    • 指定切片组的数量。
  6. slice_group_map_type(如果 num_slice_groups_minus1 > 0):
    • 指定切片组的映射类型。
  7. num_ref_idx_l0_default_active_minus1 和 num_ref_idx_l1_default_active_minus1
    • 指定参考帧列表的默认数量。
  8. weighted_pred_flag
    • 指示是否使用加权预测。
  9. weighted_bipred_idc
    • 指定双向预测中加权预测的方法。
  10. pic_init_qp_minus26
    • 初始量化参数(QP)相对于基准值26的偏移量。
  11. deblocking_filter_control_present_flag
    • 指示是否存在去块滤波参数。
  12. constrained_intra_pred_flag
    • 指示是否限制帧内预测。
  13. redundant_pic_cnt_present_flag
    • 指示是否存在冗余图像计数。

Slice Header 结构

Slice Header的结构因Slice类型(I-Slice, P-Slice, B-Slice)和某些参数的不同而略有变化。以下是Slice Header中主要字段的详解:

  1. first_mb_in_slice
    • 表示该Slice中第一个宏块的地址(从0开始)。
  2. slice_type
    • 指示Slice的类型(I-Slice, P-Slice, B-Slice等)。
    • 常见值包括:
      • 0:P-Slice
      • 1:B-Slice
      • 2:I-Slice
      • 3:SP-Slice
      • 4:SI-Slice
  3. pic_parameter_set_id
    • 指示用于该Slice的PPS的标识符。
  4. frame_num
    • 表示该Slice所属帧的帧编号。
  5. field_pic_flag(可选)
    • 指示是否为场图像(field picture)。
    • 当场图像存在时,还需要bottom_field_flag
  6. bottom_field_flag(可选)
    • 指示是否为底场(bottom field)。
  7. idr_pic_id(当Slice为IDR片时)
    • 表示IDR片的标识符。
  8. pic_order_cnt_lsb
    • 表示该Slice所属帧的图像顺序计数(LSB部分)。
  9. delta_pic_order_cnt_bottom(当pic_order_cnt_type为0时)
    • 用于计算图像顺序计数的底部增量。
  10. delta_pic_order_cnt[0] 和 delta_pic_order_cnt[1](当pic_order_cnt_type为1时)
    • 用于计算图像顺序计数的增量。
  11. redundant_pic_cnt
    • 表示冗余图像计数。
  12. direct_spatial_mv_pred_flag(当Slice类型为B-Slice时)
    • 指示是否使用直接空间运动矢量预测。
  13. num_ref_idx_active_override_flag
    • 指示是否覆盖PPS中参考索引的默认数量。
  14. num_ref_idx_l0_active_minus1 和 num_ref_idx_l1_active_minus1
    • 指示L0和L1列表中活动参考索引的数量减1。
  15. ref_pic_list_modification_flag_l0 和 ref_pic_list_modification_flag_l1
    • 指示是否修改参考图像列表。
  16. dec_ref_pic_marking
    • 包含参考图像标记信息,用于管理参考图像缓存。
  17. cabac_init_idc
    • 表示CABAC初始化索引。
  18. slice_qp_delta
    • 表示该Slice的量化参数(QP)的增量。
  19. sp_for_switch_flag(当Slice类型为SP-Slice时)
    • 指示是否为切换SP-Slice。
  20. slice_qs_delta(当Slice类型为SP-Slice或SI-Slice时)
    • 表示该Slice的QS(量化步长)的增量。
  21. disable_deblocking_filter_idc
    • 指示是否禁用去块滤波。
  22. slice_alpha_c0_offset_div2 和 slice_beta_offset_div2
    • 表示去块滤波器的α和β参数偏移量。
  23. slice_group_change_cycle
    • 指示切片组更改周期。
  • 27
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
常用的H.265码流解析开源库有: 1. FFmpeg:FFmpeg是一个开源的跨平台音视频解决方案,支持包括H.265在内的多种音视频格式的解码和编码。它提供了一套完整的API接口,可以方便地进行H.265码流解析和处理。 2. x265:x265是一个开源的H.265编码器,支持从YUV文件或者其他编码格式的视频文件中提取H.265码流。它提供了一些命令行工具,可以方便地进行码流解析和处理。 下面是使用FFmpeg库获取H.265流的I帧、P帧、B帧和SPSPPS帧的示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdint.h> #include <libavcodec/avcodec.h> int main(int argc, char** argv) { AVCodec* codec; AVCodecContext* codec_ctx; AVFrame* frame; AVPacket pkt; int ret, got_frame; // Initialize FFmpeg library av_register_all(); avcodec_register_all(); // Open H.265 codec codec = avcodec_find_decoder(AV_CODEC_ID_HEVC); if (!codec) { fprintf(stderr, "Codec not found\n"); exit(EXIT_FAILURE); } // Allocate codec context codec_ctx = avcodec_alloc_context3(codec); if (!codec_ctx) { fprintf(stderr, "Could not allocate codec context\n"); exit(EXIT_FAILURE); } // Open codec context if (avcodec_open2(codec_ctx, codec, NULL) < 0) { fprintf(stderr, "Could not open codec\n"); exit(EXIT_FAILURE); } // Allocate frame frame = av_frame_alloc(); if (!frame) { fprintf(stderr, "Could not allocate frame\n"); exit(EXIT_FAILURE); } // Initialize packet av_init_packet(&pkt); // Read H.265 stream from file FILE* fp = fopen("input.h265", "rb"); if (!fp) { fprintf(stderr, "Could not open input file\n"); exit(EXIT_FAILURE); } // Parse H.265 stream while (1) { // Read packet from file ret = fread(pkt.data, 1, 4096, fp); if (ret == 0) { break; } pkt.size = ret; // Decode packet ret = avcodec_decode_video2(codec_ctx, frame, &got_frame, &pkt); if (ret < 0) { fprintf(stderr, "Error decoding packet\n"); exit(EXIT_FAILURE); } if (got_frame) { // Process frame if (frame->pict_type == AV_PICTURE_TYPE_I) { printf("I frame\n"); } else if (frame->pict_type == AV_PICTURE_TYPE_P) { printf("P frame\n"); } else if (frame->pict_type == AV_PICTURE_TYPE_B) { printf("B frame\n"); } if (frame->metadata) { // Process SPS, PPS const char* key = "H264_PPS"; AVDictionaryEntry* entry = av_dict_get(frame->metadata, key, NULL, 0); if (entry) { printf("PPS frame\n"); } key = "H264_SPS"; entry = av_dict_get(frame->metadata, key, NULL, 0); if (entry) { printf("SPS frame\n"); } } } } // Clean up resources fclose(fp); av_frame_free(&frame); avcodec_close(codec_ctx); av_free(codec_ctx); return 0; } ``` 该示例代码使用FFmpeg库解析H.265流,并根据帧类型输出I帧、P帧、B帧以及SPSPPS帧的信息。其中,avcodec_decode_video2()函数用于解码H.265码流,AV_PICTURE_TYPE_I、AV_PICTURE_TYPE_P、AV_PICTURE_TYPE_B分别代表I帧、P帧、B帧,frame->metadata用于获取SPSPPS帧的信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Trump. yang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值