如何通过GStreamer获取H264帧

5

我熟悉ffmpeg,但不熟悉GStreamer。例如,我知道如何通过AVPacket获取H264帧,但不知道如何使用GStreamer获取h264帧。我不想直接将H264数据保存为本地文件,因为我需要进行其他处理。是否有人可以提供一些样例代码?非常感谢。以下是我从别人的代码中学到的。

#include <stdio.h>
#include <string.h>
#include <fstream>
#include <unistd.h>
#include <gst/gst.h>
#include <gst/app/gstappsrc.h>

typedef struct {
    GstPipeline *pipeline;
    GstAppSrc  *src;
    GstElement *filter1;
    GstElement *encoder;
    GstElement *filter2;
    GstElement *parser;
    GstElement *qtmux;
    GstElement *sink;

    GstClockTime timestamp;
    guint sourceid;
} gst_app_t;

static gst_app_t gst_app;

int main()
{
    gst_app_t *app = &gst_app;
    GstStateChangeReturn state_ret;
    gst_init(NULL, NULL); //Initialize Gstreamer
    app->timestamp = 0; //Set timestamp to 0

    //Create pipeline, and pipeline elements
    app->pipeline = (GstPipeline*)gst_pipeline_new("mypipeline");
    app->src    =   (GstAppSrc*)gst_element_factory_make("appsrc", "mysrc");
    app->filter1 =  gst_element_factory_make ("capsfilter", "myfilter1");
    app->encoder =  gst_element_factory_make ("omxh264enc", "myomx");
    app->filter2 =  gst_element_factory_make ("capsfilter", "myfilter2");
    app->parser =   gst_element_factory_make("h264parse"  , "myparser");
    app->qtmux =    gst_element_factory_make("qtmux"      , "mymux");
    app->sink =     gst_element_factory_make ("filesink"  , NULL);
    
    if( !app->pipeline || 
        !app->src      || !app->filter1 || 
        !app->encoder  || !app->filter2 || 
        !app->parser   || !app->qtmux    || 
        !app->sink    )  {
        printf("Error creating pipeline elements!\n");
        exit(2);
    }

    //Attach elements to pipeline
    gst_bin_add_many(
        GST_BIN(app->pipeline), 
        (GstElement*)app->src,
        app->filter1,   
        app->encoder,
        app->filter2,   
        app->parser,
        app->qtmux,
        app->sink,
        NULL);

    //Set pipeline element attributes
    g_object_set (app->src, "format", GST_FORMAT_TIME, NULL);
    GstCaps *filtercaps1 = gst_caps_new_simple ("video/x-raw",
        "format", G_TYPE_STRING, "I420",
        "width", G_TYPE_INT, 1280,
        "height", G_TYPE_INT, 720,
        "framerate", GST_TYPE_FRACTION, 1, 1,
        NULL);
    g_object_set (G_OBJECT (app->filter1), "caps", filtercaps1, NULL);
    GstCaps *filtercaps2 = gst_caps_new_simple ("video/x-h264",
        "stream-format", G_TYPE_STRING, "byte-stream",
        NULL);
    g_object_set (G_OBJECT (app->filter2), "caps", filtercaps2, NULL);
    g_object_set (G_OBJECT (app->sink), "location", "output.h264", NULL);

    //Link elements together
    g_assert( gst_element_link_many(
        (GstElement*)app->src, 
        app->filter1,
        app->encoder,
        app->filter2,
        app->parser,
        app->qtmux,
        app->sink,
        NULL ) );

    //Play the pipeline
    state_ret = gst_element_set_state((GstElement*)app->pipeline, GST_STATE_PLAYING);
    g_assert(state_ret == GST_STATE_CHANGE_ASYNC);

    //Get a pointer to the test input
    FILE *testfile = fopen("test.yuv", "rb");   
    g_assert(testfile != NULL);

    //Push the data from buffer to gstpipeline 100 times
    for(int i = 0; i < 100; i++) {
        char* filebuffer = (char*)malloc (1382400); //Allocate memory for framebuffer
        if (filebuffer == NULL) {printf("Memory error\n"); exit (2);} //Errorcheck
        size_t bytesread = fread(filebuffer, 1 , (1382400), testfile); //Read to filebuffer
        //printf("File Read: %zu bytes\n", bytesread);

        GstBuffer *pushbuffer; //Actual databuffer
        GstFlowReturn ret; //Return value
        pushbuffer = gst_buffer_new_wrapped (filebuffer, 1382400); //Wrap the data

        //Set frame timestamp
        GST_BUFFER_PTS      (pushbuffer) = app->timestamp;
        GST_BUFFER_DTS      (pushbuffer) = app->timestamp;  
        GST_BUFFER_DURATION (pushbuffer) = gst_util_uint64_scale_int (1, GST_SECOND, 1);
        app->timestamp += GST_BUFFER_DURATION (pushbuffer);
        //printf("Frame is at %lu\n", app->timestamp);

        ret = gst_app_src_push_buffer( app->src, pushbuffer); //Push data into pipeline

        g_assert(ret ==  GST_FLOW_OK);
    }
    usleep(100000);
    
    //Declare end of stream
    gst_app_src_end_of_stream (GST_APP_SRC (app->src));
    printf("End Program.\n");

return 0;

}

这里有代码源的链接:链接

1个回答

7

你的示例用于从应用程序中提供数据给GStreamer,希望使用x264进行编码,并将结果输出到文件。

我猜想你需要做的是从文件中读取数据(比如movie.mp4),并将解码后的数据传输到你的应用程序中。

我认为你有两个选择:

1、 使用appsink而不是filesink,并使用filesrc从文件中提供数据。因此,如果您除了抓取h264帧(例如播放或通过网络发送)之外还需要进行其他处理,则必须使用tee将管道分成两个输出分支,例如以下gst-launch示例。其中一个输出管道分支将进入窗口输出autovideosink,另一部分将进入您的应用程序。

为了演示这个拆分并向您展示真正发生的事情,我将使用调试元素identity,它能够转储通过它的数据。 这样,您就可以学习使用这个方便的工具进行实验和验证您所做的事情。这不是你需要的解决方案。

gst-launch-1.0 -q filesrc location= movie.mp4 ! qtdemux name=qt ! video/x-h264 ! h264parse ! tee name=t t. ! queue ! avdec_h264 ! videoconvert ! autovideosink t. ! queue ! identity dump=1 ! fakesink sync=true

此管道将视频播放到窗口(autovideosink),tee的另一个分支进入称为identity的调试元素,该元素以hexdump方式转储帧(包括地址、字符表示和所有内容)。 因此,在gst-launch的stdout中看到的是实际的h264帧(但您不会看到边界或任何其他内容...它只是真正的原始转储)。

要理解gst-launch语法(主要是使用name=别名),请查看文档中的此部分

在实际代码中,您不会使用identity和fakesink,而是链接appsink并将appsink信号连接到您C源代码中的回调函数。

有很好的示例,我不会尝试为您提供完整的解决方案。这个示例演示了如何从appsink中获取样本。 重要的部分是:

/* The appsink has received a buffer */
static GstFlowReturn new_sample (GstElement *sink, CustomData *data) {
  GstSample *sample;

  /* Retrieve the buffer */
  g_signal_emit_by_name (sink, "pull-sample", &sample);
  if (sample) {
    /* The only thing we do in this example is print a * to indicate a received buffer */
    g_print ("*");
    gst_sample_unref (sample);
    return GST_FLOW_OK;
  }

  return GST_FLOW_ERROR;
}

// somewhere in main()
// construction and linkage of elements
g_signal_connect (data.app_sink, "new-sample", G_CALLBACK (new_sample), &data);

2、 第二种解决方案是使用仅针对缓冲区注册的pad probe。Pad probe是一种在管道中任何元素的任何pad上注册回调函数的方法,并告诉GStreamer您在哪些probe信息上感兴趣。您可以要求它在每个事件、任何下游事件或通过该探针的任何缓冲区上调用回调函数。在pad probe调用的回调函数中,您将提取缓冲区和实际数据。

同样,有许多关于如何使用pad probes的示例。 一个非常好的示例包含几乎完全符合您需求的逻辑可以在这里找到。

重点:

static GstPadProbeReturn
cb_have_data (GstPad          *pad,
              GstPadProbeInfo *info,
              gpointer         user_data)
{
// ... the code for writing the buffer data somewhere ..
}

// ... later in main()
pad = gst_element_get_static_pad (src, "src");
gst_pad_add_probe (pad, GST_PAD_PROBE_TYPE_BUFFER,
      (GstPadProbeCallback) cb_have_data, NULL, NULL);

这个解释非常棒。非常感谢。 - reed

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接