写一个C语言程序,详细解析h265的SPS中包含的所有的参数
时间: 2024-05-13 13:15:00 浏览: 211
H264的sps和pps解析源码
5星 · 资源好评率100%
H.265/HEVC视频编码标准中的SPS(Sequence Parameter Set)包含了视频序列的一些基本参数和编码信息,下面是一个C语言程序,可以解析SPS中包含的所有参数:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SPS_SIZE 1024
typedef struct {
int chroma_format_idc;
int pic_width_in_luma_samples;
int pic_height_in_luma_samples;
int bit_depth_luma_minus8;
int bit_depth_chroma_minus8;
int log2_max_pic_order_cnt_lsb_minus4;
int num_short_term_ref_pic_sets;
int num_long_term_ref_pics_sps;
int bit_depth_aux_minus8;
int scaling_list_enabled_flag;
int seq_scaling_list_present_flag;
int log2_min_luma_coding_block_size_minus3;
int log2_diff_max_min_luma_coding_block_size;
int log2_min_transform_block_size_minus2;
int log2_diff_max_min_transform_block_size;
int max_transform_hierarchy_depth_inter;
int max_transform_hierarchy_depth_intra;
int scaling_list_update_flag;
int scaling_list_data_present_flag;
} SpsParams;
void parse_sps(unsigned char* sps_data, int sps_size, SpsParams* sps)
{
int i, j;
int sps_id, chroma_format_idc, pic_width_in_luma_samples, pic_height_in_luma_samples;
int bit_depth_luma_minus8, bit_depth_chroma_minus8, log2_max_pic_order_cnt_lsb_minus4;
int num_short_term_ref_pic_sets, num_long_term_ref_pics_sps, bit_depth_aux_minus8;
int scaling_list_enabled_flag, seq_scaling_list_present_flag, log2_min_luma_coding_block_size_minus3;
int log2_diff_max_min_luma_coding_block_size, log2_min_transform_block_size_minus2;
int log2_diff_max_min_transform_block_size, max_transform_hierarchy_depth_inter, max_transform_hierarchy_depth_intra;
int scaling_list_update_flag, scaling_list_data_present_flag;
sps_id = sps_data[1];
chroma_format_idc = sps_data[3] & 0x03;
pic_width_in_luma_samples = ((sps_data[3] & 0xfc) >> 2) * 8 + ((sps_data[4] & 0xe0) >> 5);
pic_height_in_luma_samples = ((sps_data[4] & 0x1f) << 5) + ((sps_data[5] & 0xf8) >> 3);
bit_depth_luma_minus8 = (sps_data[5] & 0x07) + 8;
bit_depth_chroma_minus8 = (sps_data[6] & 0x07) + 8;
log2_max_pic_order_cnt_lsb_minus4 = (sps_data[7] & 0x0f);
num_short_term_ref_pic_sets = sps_data[8];
num_long_term_ref_pics_sps = sps_data[9];
bit_depth_aux_minus8 = (sps_data[10] & 0x07) + 8;
scaling_list_enabled_flag = (sps_data[11] & 0x80) >> 7;
seq_scaling_list_present_flag = (sps_data[11] & 0x20) >> 5;
log2_min_luma_coding_block_size_minus3 = (sps_data[11] & 0x18) >> 3;
log2_diff_max_min_luma_coding_block_size = (sps_data[11] & 0x07) << 1 | (sps_data[12] & 0x80) >> 7;
log2_min_transform_block_size_minus2 = (sps_data[12] & 0x78) >> 3;
log2_diff_max_min_transform_block_size = (sps_data[12] & 0x07) << 1 | (sps_data[13] & 0x80) >> 7;
max_transform_hierarchy_depth_inter = (sps_data[13] & 0x70) >> 4;
max_transform_hierarchy_depth_intra = (sps_data[13] & 0x0f);
scaling_list_update_flag = (sps_data[14] & 0x80) >> 7;
scaling_list_data_present_flag = (sps_data[14] & 0x20) >> 5;
sps->chroma_format_idc = chroma_format_idc;
sps->pic_width_in_luma_samples = pic_width_in_luma_samples;
sps->pic_height_in_luma_samples = pic_height_in_luma_samples;
sps->bit_depth_luma_minus8 = bit_depth_luma_minus8;
sps->bit_depth_chroma_minus8 = bit_depth_chroma_minus8;
sps->log2_max_pic_order_cnt_lsb_minus4 = log2_max_pic_order_cnt_lsb_minus4;
sps->num_short_term_ref_pic_sets = num_short_term_ref_pic_sets;
sps->num_long_term_ref_pics_sps = num_long_term_ref_pics_sps;
sps->bit_depth_aux_minus8 = bit_depth_aux_minus8;
sps->scaling_list_enabled_flag = scaling_list_enabled_flag;
sps->seq_scaling_list_present_flag = seq_scaling_list_present_flag;
sps->log2_min_luma_coding_block_size_minus3 = log2_min_luma_coding_block_size_minus3;
sps->log2_diff_max_min_luma_coding_block_size = log2_diff_max_min_luma_coding_block_size;
sps->log2_min_transform_block_size_minus2 = log2_min_transform_block_size_minus2;
sps->log2_diff_max_min_transform_block_size = log2_diff_max_min_transform_block_size;
sps->max_transform_hierarchy_depth_inter = max_transform_hierarchy_depth_inter;
sps->max_transform_hierarchy_depth_intra = max_transform_hierarchy_depth_intra;
sps->scaling_list_update_flag = scaling_list_update_flag;
sps->scaling_list_data_present_flag = scaling_list_data_present_flag;
printf("chroma_format_idc: %d\n", chroma_format_idc);
printf("pic_width_in_luma_samples: %d\n", pic_width_in_luma_samples);
printf("pic_height_in_luma_samples: %d\n", pic_height_in_luma_samples);
printf("bit_depth_luma_minus8: %d\n", bit_depth_luma_minus8);
printf("bit_depth_chroma_minus8: %d\n", bit_depth_chroma_minus8);
printf("log2_max_pic_order_cnt_lsb_minus4: %d\n", log2_max_pic_order_cnt_lsb_minus4);
printf("num_short_term_ref_pic_sets: %d\n", num_short_term_ref_pic_sets);
printf("num_long_term_ref_pics_sps: %d\n", num_long_term_ref_pics_sps);
printf("bit_depth_aux_minus8: %d\n", bit_depth_aux_minus8);
printf("scaling_list_enabled_flag: %d\n", scaling_list_enabled_flag);
printf("seq_scaling_list_present_flag: %d\n", seq_scaling_list_present_flag);
printf("log2_min_luma_coding_block_size_minus3: %d\n", log2_min_luma_coding_block_size_minus3);
printf("log2_diff_max_min_luma_coding_block_size: %d\n", log2_diff_max_min_luma_coding_block_size);
printf("log2_min_transform_block_size_minus2: %d\n", log2_min_transform_block_size_minus2);
printf("log2_diff_max_min_transform_block_size: %d\n", log2_diff_max_min_transform_block_size);
printf("max_transform_hierarchy_depth_inter: %d\n", max_transform_hierarchy_depth_inter);
printf("max_transform_hierarchy_depth_intra: %d\n", max_transform_hierarchy_depth_intra);
printf("scaling_list_update_flag: %d\n", scaling_list_update_flag);
printf("scaling_list_data_present_flag: %d\n", scaling_list_data_present_flag);
}
int main(int argc, char* argv[])
{
if (argc != 2) {
printf("usage: %s sps_file\n", argv[0]);
return -1;
}
FILE* fp = fopen(argv[1], "rb");
if (fp == NULL) {
printf("failed to open file %s\n", argv[1]);
return -1;
}
int sps_size;
unsigned char sps_data[MAX_SPS_SIZE];
memset(sps_data, 0, MAX_SPS_SIZE);
sps_size = fread(sps_data, 1, MAX_SPS_SIZE, fp);
if (sps_size <= 0) {
printf("failed to read sps data\n");
fclose(fp);
return -1;
}
SpsParams sps;
memset(&sps, 0, sizeof(SpsParams));
parse_sps(sps_data, sps_size, &sps);
fclose(fp);
return 0;
}
```
这个程序可以从SPS文件中读取SPS数据,并解析出其中包含的所有参数。其中,`SpsParams`结构体中包含了所有的参数,可以根据需要进行取值。在`parse_sps`函数中,先根据SPS数据的格式解析出所有的参数,然后将这些参数赋值给`SpsParams`结构体,最后输出所有的参数值。
阅读全文