H264中的SPS、PPS提取与作用

这篇文章说了对sps和pps的提取方法:http://wmnmtm.blog.163.com/blog/static/38245714201192491746701/

使用RTP传输H264的时候,需要用到sdp协议描述,其中有两项:Sequence Parameter Sets (SPS) 和Picture Parameter Set (PPS)需要用到,那么这两项从哪里获取呢?答案是从H264码流中获取.在H264码流中,都是以"0x00 0x00 0x01"或者"0x00 0x00 0x00 0x01"为开始码的,找到开始码之后,使用开始码之后的第一个字节的低5位判断是否为7(sps)或者8(pps), 及data[4] & 0x1f == 7 || data[4] & 0x1f == 8.然后对获取的nal去掉开始码之后进行base64编码,得到的信息就可以用于sdp.sps和pps需要用逗号分隔开来.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用GStreamer API提取H.264的SPSPPS信息,需要使用h264parse元素来解析H.264码流,并从提取SPSPPS。以下是使用GStreamer API提取H.264SPSPPS信息的示例代码: ```c #include <gst/gst.h> int main(int argc, char *argv[]) { GstElement *pipeline, *h264parse; GstCaps *caps; GstBuffer *sps_buf, *pps_buf; GstMapInfo map_info; gboolean ret; /* Initialize GStreamer */ gst_init(&argc, &argv); /* Create the elements */ pipeline = gst_pipeline_new("mypipeline"); h264parse = gst_element_factory_make("h264parse", "myh264parse"); /* Set the caps for h264parse element */ caps = gst_caps_new_simple("video/x-h264", "stream-format", G_TYPE_STRING, "byte-stream", NULL); g_object_set(G_OBJECT(h264parse), "caps", caps, NULL); gst_caps_unref(caps); /* Add h264parse element to the pipeline */ gst_bin_add(GST_BIN(pipeline), h264parse); /* Link the elements */ if (!gst_element_link_many(h264parse, NULL)) { g_printerr("Failed to link elements\n"); return -1; } /* Start the pipeline */ gst_element_set_state(pipeline, GST_STATE_PLAYING); /* Wait for the SPS and PPS buffers to be emitted */ ret = gst_element_get_state(h264parse, NULL, NULL, GST_CLOCK_TIME_NONE); if (ret != GST_STATE_CHANGE_SUCCESS) { g_printerr("Failed to get state of h264parse element\n"); return -1; } ret = gst_element_query_position(h264parse, GST_FORMAT_TIME, NULL); if (ret != TRUE) { g_printerr("Failed to query position of h264parse element\n"); return -1; } ret = gst_element_query_duration(h264parse, GST_FORMAT_TIME, NULL); if (ret != TRUE) { g_printerr("Failed to query duration of h264parse element\n"); return -1; } ret = gst_element_query_latency(h264parse, GST_FORMAT_TIME, NULL, NULL); if (ret != TRUE) { g_printerr("Failed to query latency of h264parse element\n"); return -1; } gst_element_send_event(h264parse, gst_event_new_seek(1.0, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE, GST_SEEK_TYPE_NONE, 0, GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE)); ret = gst_element_get_state(h264parse, NULL, NULL, GST_CLOCK_TIME_NONE); if (ret != GST_STATE_CHANGE_SUCCESS) { g_printerr("Failed to get state of h264parse element\n"); return -1; } sps_buf = gst_buffer_new(); pps_buf = gst_buffer_new(); ret = gst_element_query_convert(h264parse, GST_FORMAT_TIME, GST_FORMAT_BUFFER, GST_SECOND, &sps_buf); if (ret != TRUE) { g_printerr("Failed to query convert SPS buffer\n"); return -1; } ret = gst_element_query_convert(h264parse, GST_FORMAT_TIME, GST_FORMAT_BUFFER, 2 * GST_SECOND, &pps_buf); if (ret != TRUE) { g_printerr("Failed to query convert PPS buffer\n"); return -1; } /* Extract the SPS and PPS data */ gst_buffer_map(sps_buf, &map_info, GST_MAP_READ); /* map_info.data contains SPS data */ gst_buffer_unmap(sps_buf, &map_info); gst_buffer_map(pps_buf, &map_info, GST_MAP_READ); /* map_info.data contains PPS data */ gst_buffer_unmap(pps_buf, &map_info); /* Stop and cleanup the pipeline */ gst_element_set_state(pipeline, GST_STATE_NULL); gst_object_unref(pipeline); return 0; } ``` 在上述代码,我们创建了一个GStreamer管道,其包含了h264parse元素。我们设置了h264parse的caps,然后将h264parse元素添加到管道,并启动了管道。 我们使用gst_element_send_event()来发送一个seek事件,让h264parse元素从H.264码流提取SPSPPS信息。我们使用gst_element_query_convert()来获取SPSPPS的缓冲区,并使用gst_buffer_map()和gst_buffer_unmap()来提取SPSPPS数据。 请注意,在实际使用,你需要根据需要修改代码,并添加适当的错误处理和释放资源的代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值