【问题标题】:How to get h264 frames via gstreamer如何通过 gstreamer 获取 h264 帧
【发布时间】:2021-02-02 10:47:59
【问题描述】:

我熟悉 ffmpeg,但不熟悉 GStreamer。我知道如何通过ffmpeg获取H264帧,例如我可以通过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;

}

这里是代码源的链接 link

【问题讨论】:

    标签: c gstreamer h.264 codec


    【解决方案1】:

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

    您需要(我猜这里)是从文件中读取数据 - 比如说 movie.mp4 并将解码后的数据放入您的应用程序(?)

    我相信你有两个选择:

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

    为了演示这种拆分并仍然向您展示实际发生的情况,我将使用调试元素标识,它能够转储通过它的数据。 通过这种方式,您将学会使用这个方便的工具进行实验和验证您知道自己在做什么。这不是您需要的解决方案。

    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 的标准输出中看到的是实际的 h264 帧(但您看不到边界或任何东西。它只是真正的原始转储)。

    要了解 gst-launch 语法(主要是带有 name= 的别名),请查看文档的 this part

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

    这方面有很好的例子,我不会试图给你完整的解决方案。 This example 演示如何从 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, 第二种解决方案是仅使用为缓冲区注册的焊盘探针。填充探针是一种在管道中任何元素的任何填充上注册回调并告诉 GStreamer 您对该探针感兴趣的信息的方法。您可以要求它在每个事件、任何下游事件或通过该探针的任何缓冲区上调用回调。在 pad probe 调用的回调中,您将提取缓冲区和该缓冲区中的实际数据。

    还有很多关于如何使用焊盘探针的示例。 found here

    重要的部分:

    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);
    

    【讨论】:

      猜你喜欢
      • 2015-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多