Как обработать кадры из видео, полученного из файла? - PullRequest
0 голосов
/ 03 марта 2020

Я хочу прочитать видео из файла. Но перед отправкой фрейма в приемник я хочу записать его в буфер и обработать. Например, я хочу как-то проанализировать каждый кадр. Я нашел и усовершенствовал пример того, как генерировать кадры и затем обрабатывать их.

#include <gst/gst.h>
#include <string.h>
#include <gst/app/gstappsrc.h>
#include <gst/app/gstappsink.h>


#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"


#define CHUNK_SIZE 1280*720*3   /* Amount of bytes we are sending in each buffer */

/* Structure to contain all our information, so we can pass it to callbacks */
typedef struct _CustomData {
    GstElement* pipeline, * app_source, * app_sink;
    guint sourceid;        /* To control the GSource */
    GMainLoop* main_loop;  /* GLib's Main Loop */
} CustomData;

/* This method is called by the idle GSource in the mainloop, to feed CHUNK_SIZE bytes into appsrc.
* The ide handler is added to the mainloop when appsrc requests us to start sending data (need-data signal)
* and is removed when appsrc has enough data (enough-data signal).
*/
static gboolean push_data(CustomData* data) {
    GstBuffer* buffer;
    GstFlowReturn ret;
    int i;
    GstMapInfo map;
    gint8* raw;
    g_print("push_data!\n");
    /* Create a new empty buffer */
    buffer = gst_buffer_new_and_alloc(CHUNK_SIZE);

    /* Generate simple uint8 buffer */
    gst_buffer_map(buffer, &map, GST_MAP_WRITE);
    raw = (gint8*)map.data;
    for (i = 0; i < CHUNK_SIZE; i++) {
        raw[i] = (gint8)(i % 256);  //Push simple uint8
    }
    gst_buffer_unmap(buffer, &map);

    /* Push the buffer into the appsrc */
    g_signal_emit_by_name(data->app_source, "push-buffer", buffer, &ret);

    /* Free the buffer now that we are done with it */
    gst_buffer_unref(buffer);


    if (ret != GST_FLOW_OK) {
        /* We got some error, stop sending data */
        return FALSE;
    }

    return TRUE;
}

/* This signal callback triggers when appsrc needs data. Here, we add an idle handler
* to the mainloop to start pushing data into the appsrc */
static void start_feed(GstElement* source, guint size, CustomData* data) {
    if (data->sourceid == 0) {
        g_print("Start feeding\n");
        data->sourceid = g_idle_add((GSourceFunc)push_data, data);
    }
}

/* This callback triggers when appsrc has enough data and we can stop sending.
* We remove the idle handler from the mainloop */
static void stop_feed(GstElement* source, CustomData* data) {
    if (data->sourceid != 0) {
        g_print("Stop feeding\n");
        g_source_remove(data->sourceid);
        data->sourceid = 0;
    }
}

/**
 * @brief Check preroll to get a new frame using callback
 *  https://gstreamer.freedesktop.org/documentation/design/preroll.html
 * @return GstFlowReturn
 */
GstFlowReturn new_preroll(GstAppSink* appsink, gpointer data)
{
    return GST_FLOW_OK;
}

/* The appsink has received a buffer */
GstFlowReturn new_sample(GstAppSink* sink, gpointer data)
{
    GstSample* sample;

    /* Retrieve the buffer */
    g_signal_emit_by_name(sink, "pull-sample", &sample);
    static framecount = 0;
    if (sample) {

        GstBuffer* buffer = gst_sample_get_buffer(sample);
        GstMapInfo map;
        gboolean mapping_succ = gst_buffer_map(buffer, &map, GST_MAP_READ);

        if (!(framecount % 30)) {
            char name[50];
            sprintf(name, "E:/frames/%d.png", framecount);
            stbi_write_png(name, 1280, 720, 3, (char*)map.data, 1280 * 3);
        }
        // Confirm that correct buffer was received...
        //g_print("Appsink: Buffer Received: Content = %u!\n", *info.data);

        g_print("received something...\n");
        gst_buffer_unmap(buffer, &map);
        gst_buffer_unref((GstBuffer*)sample);
        framecount++;
    }
    return GST_FLOW_OK;
}

/* This function is called when an error message is posted on the bus */
static void error_cb(GstBus* bus, GstMessage* msg, CustomData* data) {
    GError* err;
    gchar* debug_info;

    /* Print error details on the screen */
    gst_message_parse_error(msg, &err, &debug_info);
    g_printerr("Error received from element %s: %s\n", GST_OBJECT_NAME(msg->src), err->message);
    g_printerr("Debugging information: %s\n", debug_info ? debug_info : "none");
    g_clear_error(&err);
    g_free(debug_info);

    g_main_loop_quit(data->main_loop);
}

/* This function is called when an error message is posted on the bus */
static void eos_cb(GstBus* bus, GstMessage* msg, CustomData* data) {
    g_print("End-Of-Stream reached.\n");
}

int main(int argc, char* argv[]) {

    CustomData data;
    GstBus* bus;

    /* Initialize cumstom data structure */
    memset(&data, 0, sizeof(data));

    /* Initialize GStreamer */
    gst_init(&argc, &argv);

    /* Create the elements */
    data.app_source = gst_element_factory_make("appsrc", "app_source");
    data.app_sink = gst_element_factory_make("appsink", "app_sink");

    /* Create the empty pipeline */
    data.pipeline = gst_pipeline_new("custom-pipeline");

    if (!data.pipeline || !data.app_source || !data.app_sink) {
        g_printerr("Not all elements could be created.\n");
        return -1;
    }

    /* Create custom properties*/
    g_object_set(G_OBJECT(data.app_source), "caps",
        gst_caps_new_simple("custom",
            "format", G_TYPE_STRING, "RGB",
            "width", G_TYPE_INT, 1280,
            "height", G_TYPE_INT, 720,
            "framerate", GST_TYPE_FRACTION, 25, 1,
            NULL), NULL);

    g_object_set(G_OBJECT(data.app_sink), "caps",
        gst_caps_new_simple("custom",
            "format", G_TYPE_STRING, "RGB",
            "width", G_TYPE_INT, 1280,
            "height", G_TYPE_INT, 720,
            "framerate", GST_TYPE_FRACTION, 25, 1,
            NULL), NULL);

    /* Configure appsrc */
    //g_object_set(data.app_source, "format", GST_FORMAT_TIME, NULL);
    g_signal_connect(data.app_source, "need-data", G_CALLBACK(start_feed), &data);
    //g_signal_connect(data.app_source, "enough-data", G_CALLBACK(stop_feed), &data);


    /* Configure appsink */
    g_object_set(data.app_sink, "emit-signals", TRUE, NULL);
    GstAppSinkCallbacks callbacks = { NULL, new_preroll, new_sample };
    gst_app_sink_set_callbacks(GST_APP_SINK(data.app_sink), &callbacks, &data, NULL);


    /* Link all elements that can be automatically linked because they have "Always" pads */
    gst_bin_add_many(GST_BIN(data.pipeline), data.app_source, data.app_sink, NULL);
    if (gst_element_link_many(data.app_source, data.app_sink, NULL) != TRUE) {
        g_printerr("Elements could not be linked.\n");
        gst_object_unref(data.pipeline);
        return -1;
    }

    /* Instruct the bus to emit signals for each received message, and connect to the interesting signals */
    /* gst_element_get_bus is the same as gst_pipeline_get_but -> In this example pipeline is element, therfore the former...*/
    bus = gst_element_get_bus(data.pipeline);
    gst_bus_add_signal_watch(bus);
    g_signal_connect(G_OBJECT(bus), "message::error", (GCallback)error_cb, &data);
    g_signal_connect(G_OBJECT(bus), "message::eos", (GCallback)eos_cb, &data);

    gst_object_unref(bus);

    /* Start playing the pipeline */
    gst_element_set_state(data.pipeline, GST_STATE_PLAYING);

    if (!(data.app_source->current_state == GST_STATE_PLAYING));
    {
        g_print("Wait for transition to playing state!\n");
    }

    GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS((GstBin*)data.pipeline, GST_DEBUG_GRAPH_SHOW_ALL, "show_pipeline");

    /* Create a GLib Main Loop and set it to run */
    data.main_loop = g_main_loop_new(NULL, FALSE);

    g_main_loop_run(data.main_loop);


    /* Free resources */
    gst_element_set_state(data.pipeline, GST_STATE_NULL);
    gst_object_unref(data.pipeline);
    return 0;
}

Но я хочу не генерировать их, а получать из файла, а затем обрабатывать их. Как я могу это сделать?

...