gstreamer获取h264码流中SPS和PPS信息的方法 举个C语言例子
时间: 2023-08-30 18:06:28 浏览: 197
H264中SPS与PPS的获取
5星 · 资源好评率100%
以下是使用 C 语言的 GStreamer API 获取 H264 码流中 SPS 和 PPS 信息的示例代码:
```c
#include <gst/gst.h>
int main(int argc, char *argv[]) {
GstElement *pipeline, *src, *h264parse, *capsfilter, *splitter, *queue_sps, *queue_pps, *sink;
GstBus *bus;
GstMessage *msg;
GstCaps *caps;
GstBuffer *buf;
gboolean ret;
GError *err = NULL;
guint8 *sps_data = NULL, *pps_data = NULL;
gsize sps_size = 0, pps_size = 0;
/* Initialize GStreamer */
gst_init(&argc, &argv);
/* Create elements */
pipeline = gst_pipeline_new("pipeline");
src = gst_element_factory_make("filesrc", "src");
h264parse = gst_element_factory_make("h264parse", "parse");
capsfilter = gst_element_factory_make("capsfilter", "caps");
splitter = gst_element_factory_make("splitter", "split");
queue_sps = gst_element_factory_make("queue", "sps");
queue_pps = gst_element_factory_make("queue", "pps");
sink = gst_element_factory_make("fakesink", "sink");
/* Set properties */
g_object_set(G_OBJECT(src), "location", "test.h264", NULL);
caps = gst_caps_from_string("video/x-h264, stream-format=byte-stream");
g_object_set(G_OBJECT(capsfilter), "caps", caps, NULL);
/* Build the pipeline */
gst_bin_add_many(GST_BIN(pipeline), src, h264parse, capsfilter, splitter, queue_sps, queue_pps, sink, NULL);
gst_element_link_many(src, h264parse, capsfilter, splitter, NULL);
gst_element_link_many(splitter, queue_sps, sink, NULL);
gst_element_link_many(splitter, queue_pps, sink, NULL);
/* Start playing */
ret = gst_element_set_state(pipeline, GST_STATE_PLAYING);
if (ret == GST_STATE_CHANGE_FAILURE) {
g_printerr("Failed to start pipeline\n");
return -1;
}
/* Wait until error or state change */
bus = gst_element_get_bus(pipeline);
msg = gst_bus_timed_pop_filtered(bus, GST_CLOCK_TIME_NONE, GST_MESSAGE_ERROR | GST_MESSAGE_STATE_CHANGED);
if (msg != NULL) {
gst_message_unref(msg);
}
/* Get SPS and PPS data */
while (sps_data == NULL || pps_data == NULL) {
GstSample *sample;
GstBuffer *buf;
sample = gst_app_sink_pull_sample(GST_APP_SINK(queue_sps));
if (sample != NULL) {
buf = gst_sample_get_buffer(sample);
sps_data = gst_buffer_extract_dup(buf, 0, gst_buffer_get_size(buf), &err);
sps_size = gst_buffer_get_size(buf);
gst_sample_unref(sample);
}
sample = gst_app_sink_pull_sample(GST_APP_SINK(queue_pps));
if (sample != NULL) {
buf = gst_sample_get_buffer(sample);
pps_data = gst_buffer_extract_dup(buf, 0, gst_buffer_get_size(buf), &err);
pps_size = gst_buffer_get_size(buf);
gst_sample_unref(sample);
}
}
/* Output SPS and PPS data */
g_print("SPS:");
for (int i = 0; i < sps_size; i++) {
g_print(" %02x", sps_data[i]);
}
g_print("\n");
g_print("PPS:");
for (int i = 0; i < pps_size; i++) {
g_print(" %02x", pps_data[i]);
}
g_print("\n");
/* Stop playing */
gst_element_set_state(pipeline, GST_STATE_NULL);
gst_object_unref(pipeline);
return 0;
}
```
这段代码与前面的 Python 代码类似,使用了 `h264parse` 元素将 H264 码流解析为 NAL 单元,使用 `capsfilter` 元素设置媒体格式,并使用 `splitter` 元素将 NAL 单元分离为 SPS、PPS 和 IDR 帧等单元。不同之处在于,这里使用了 `gst_app_sink_pull_sample()` 方法从队列中获取数据。由于 C 语言没有 Python 中的 `bytearray` 类型,所以这里使用了 `guint8 *` 类型的指针来保存 SPS 和 PPS 数据,并使用 `gsize` 类型的变量保存数据的大小。
阅读全文