背景
业务需要对原始YUV数据进行硬编码然后通过第三方库推送到Web端。网上找了很多资料,没有找到对Gstreamer硬编码的详细介绍。这里记录下自己的实现,附完整代码。
一、视频编码基础知识
像素点的英文叫Pixel(缩写为PX)。Pixel是由 Picture(图像) 和 Element(元素)这两个单词简化而来。像素是图像显示的基本单位。
我们通常说一幅图片的大小,例如是1920×1080,就是长度为1920个像素点,宽度为1080个像素点。乘积是2,073,600,也就是说,这个图片是两百万像素的。1920×1080,这个也被称为这幅图片的分辨率。
那么视频是怎么来的? 相信大家从小都看过动画片, 没错,大量的图片连续起来快速播放,就是我们看到的视频了。衡量视频用的是什么指标参数呢? 最主要的一个,就是帧率(Frame Rate)。在视频中,一个帧(Frame)就是指一幅静止的画面。帧率,就是指视频每秒钟包括的画面数量(FPS,Frame per second)。
二、什么是视频编码?
视频编码:就是将一种视频格式,转换成另一种视频格式。
有了视频之后,就涉及到两个问题:一个是如何存储视频数据; 另一个是如何传输视频数据。视频编码主要为了压缩,减少体积,利于传输。
为什么要对视频进行编码呢?不编码可以吗?为了解释这个问题,我们通过计算来了解下未经编码的原始音视频,数据量到底有多大,以一个分辨率 1920×1280,帧率为 30 的视频为例:
每帧图片:1920×1280=2,073,600(Pixels 像素),每个像素点是 24bit(RGB三原色,每种颜色占1个字节,总共24 bits);总共:2073600 × 24 = 49766400 bit,1 byte(字节)= 8 bit(位);
也就是说一个分辨率 1920×1280大小的图片就是:49766400 bit = 6220800 byte ≈ 6.22 MB,再乘以帧率 30,每秒视频的大小是 186.6MB,每分钟大约是 11GB,一部 90 分钟的电影,约是 1000GB。
恐怖吧?就算你现在电脑硬盘是4TB的(实际也就3600GB),也放不下几部学习资料,手动狗头。不仅要存储,还要传输,不然视频从哪来呢? 如果按照百兆带宽网速(顶格下载速率=12.5MB/s),下刚才那部电影,整整需要22个小时!!!
二、完整代码示例
#include <iostream> #include <string> #include <gst/gst.h> typedef struct _custom_data { GstElement *pipeline, *source, *h264_encoder, *filter, *video_tee, *save_queue, *push_queue, *file_sink, *app_sink; string ChannelId; int frameCnt; } CustomData; GstFlowReturn InitGstElements(CustomData *data) { GstFlowReturn retVal; GstPad *tee_save_pad, *tee_push_pad; GstPad *queue_save_pad, *queue_push_pad; /* Create the elements */ data->source = gst_element_factory_make("appsrc", "source"); data->video_tee = gst_element_factory_make("tee", "video_tee"); // nvh264enc: Encode H.264 video streams using NVIDIA's hardware-accelerated NVENC encoder API data->h264_encoder = gst_element_factory_make("nvh264enc", "h264_encoder"); // 硬编码 GstCaps *Cap = gst_caps_from_string("video/x-raw, width=640, height=480, framerate=25/1, format=YV12, interlace-mode=progressive"); data->filter = gst_element_factory_make("capsfilter", "filter"); g_object_set(data->filter, "caps", Cap, NULL); data->save_queue = gst_element_factory_make("queue", "save_queue"); data->file_sink = gst_element_factory_make("filesink", "file_sink"); // autovideosink, fakesink, appsink, filesink data->push_queue = gst_element_factory_make("queue", "push_queue"); data->app_sink = gst_element_factory_make("appsink", "app_sink"); /* Create the empty pipeline */ data->pipeline = gst_pipeline_new("rtsp-encode-pipeline"); if (!data->pipeline || !data->source || !data->h264_encoder || !data->filter || !data->file_sink || !data->video_tee || \ !data->save_queue || !data->push_queue || !data->app_sink) { g_printerr("@@@ OpenRtsp: Not all elements could be created.\n"); return GST_FLOW_ERROR; } g_print("================ All elements created success. ===================== \n"); /* Modify element properties */ // g_object_set(G_OBJECT(source), "location", "./test.yuv", "blocksize", 3110400, NULL); g_object_set(G_OBJECT(data->file_sink), "location", "./output.264", NULL); // g_object_set(G_OBJECT(pic_sink), "location", "./pic.264", NULL); g_object_set(G_OBJECT(data->app_sink), "sync", FALSE, "emit-signals", TRUE, // "caps", gst_caps_new_simple("video/x-raw", // // "width", G_TYPE_INT, 640, // // "height", G_TYPE_INT, 480, // // "framerate", GST_TYPE_FRACTION, 25, 1, // "format", G_TYPE_STRING, VIDEO_FORMAT, NULL), NULL); // audio_queue, *video_queue, *tee; /* 创建pipeline,注意此时各个组件还没有连接,只是add到管道,也就是说,add要在link之前 */ // gst_bin_add_many(GST_BIN(data->pipeline), data->source, data->h264_encoder, data->encode_queue, data->video_tee, // data->save_queue ,data->file_sink,data->push_queue, data->app_sink, NULL); gst_bin_add_many(GST_BIN(data->pipeline), data->source, data->filter, data->h264_encoder, data->video_tee, \ data->save_queue, data->file_sink, data->push_queue, data->app_sink, NULL); if (gst_element_link_many(data->source, data->filter, data->h264_encoder, data->video_tee, NULL) != TRUE || gst_element_link_many(data->save_queue, data->file_sink, NULL) != TRUE || gst_element_link_many(data->push_queue, data->app_sink, NULL) != TRUE) { g_printerr("Elements source --> file_sink could not be linked.\n"); gst_object_unref(data->pipeline); return GST_FLOW_ERROR; } /* Manually link the Tee, which has "Request" pads */ tee_save_pad = gst_element_request_pad_simple(data->video_tee, "src_%u"); g_print("Obtained request pad %s for save branch success...\n", gst_pad_get_name(tee_save_pad)); queue_save_pad = gst_element_get_static_pad(data->save_queue, "sink"); tee_push_pad = gst_element_request_pad_simple(data->video_tee, "src_%u"); g_print("Obtained request pad %s for push branch success...\n", gst_pad_get_name(tee_push_pad)); queue_push_pad = gst_element_get_static_pad(data->push_queue, "sink"); if (gst_pad_link(tee_save_pad, queue_save_pad) != GST_PAD_LINK_OK || gst_pad_link(tee_push_pad, queue_push_pad) != GST_PAD_LINK_OK) { g_printerr("Tee tee--> queue linked failed.\n"); gst_object_unref(data->pipeline); return GST_FLOW_ERROR; } gst_object_unref(queue_save_pad); gst_object_unref(queue_push_pad); g_print("=================== All elements linked success ==================\n"); /* 设置 pipeline 状态为 Playing */ GstStateChangeReturn ret = gst_element_set_state(data->pipeline, GST_STATE_PLAYING); if (ret == GST_STATE_CHANGE_FAILURE) { g_printerr("@@@ InitGst: Unable to set the pipeline to the playing state.\n"); return GST_FLOW_ERROR; } g_print("$$$ Start playing... \n"); /* 从编码器获取编码后的数据 */ g_signal_connect(data->app_sink, "new-sample", G_CALLBACK(ReadEncodeFrame_callback), data); return GST_FLOW_OK; } /* Read 1 frame */ void GstStreamEncoder::ReadEncodeFrame_callback(GstElement *sink, gpointer user_data) { if (!encoder) return; CustomData *data = (CustomData *)user_data; GstSample *sample = nullptr; gsize data_size = 0; gsize stream_size = 0; // 待填数据 CameraStream *stream = nullptr; char timeStamp[16] = {0}; int heigth = 0; int width = 0; char video_format[32] = {0}; int framerate[2] = {0}; // 使用pull-sample拉取视频帧,并映射到map变量,通过map拷贝出frame数据 g_signal_emit_by_name(sink, "pull-sample", &sample); if (sample) { // g_print("sample = 0x%0x\n", sample); GstBuffer *buffer = gst_sample_get_buffer(sample); if (buffer) { // copy buffer data into CameraStream GstMapInfo map; if (gst_buffer_map(buffer, &map, GST_MAP_READ)) { g_print("map data size = %lu\n", map.size); // 获取 frame 信息 { GstCaps *caps = gst_sample_get_caps(sample); GstStructure *info = gst_caps_get_structure(caps, 0); gst_structure_get_int(info, "width", &width); gst_structure_get_int(info, "height", &heigth); // const char *format = gst_structure_get_string(info, "format"); gst_structure_get_fraction(info, "framerate", &framerate[0], &framerate[1]); data_size = map.size; // 注意这里buffer大小,需要设置为data_size,或者malloc(data_size) char buf[4096] = {0}; memcpy(buf, map.data, data_size); // 获取解码后的数据到buf // release buffer mapping gst_buffer_unmap(buffer, &map) } gst_buffer_unmap(buffer, &map); } else { g_printerr("@@@ ReadOneDecodedFrame: fgst_buffer_map error\n"); } } else { g_printerr("@@@ ReadOneDecodedFrame: gst_sample_get_buffer fail\n"); } // release sample reference gst_sample_unref(sample); } else { g_printerr("@@@ sample is null \n"); } }