C++ ffmpeg硬件解碼的實(shí)現(xiàn)方法
什么是硬件解碼
普通解碼是利用cpu去解碼也就是軟件解碼 硬件解碼就是利用gpu去解碼
為什么要使用硬件解碼
首先最大的好處 快硬解播放出來的視頻較為流暢,并且能夠延長(zhǎng)移動(dòng)設(shè)備播放視頻的時(shí)間; 而軟解由于軟解加大CPU工作負(fù)荷,會(huì)占用過多的移動(dòng)CPU資源,如果CPU能力不足,則軟件也將受到影響 最主要就是一個(gè)字 快
怎樣使用硬件解碼
ffmpeg內(nèi)部為我們提供了友好的接口去實(shí)現(xiàn)硬件解碼
注意事項(xiàng)
ffmpeg內(nèi)部有很多編解碼器 并不是所有的編解碼器都支持硬件解碼 并且就算支持硬件解碼的編解碼器也不一定能支持你的顯卡 也就是說在使用硬件解碼時(shí)我們首先要去判斷這個(gè)解碼器是否支持在這個(gè)平臺(tái)對(duì)這個(gè)顯卡進(jìn)行硬件編解碼 不然是無法使用的
對(duì)顯卡廠家SDK進(jìn)行封裝和集成,實(shí)現(xiàn)部分的硬件編解碼
其次在ffmpeg中軟件編解碼器可以實(shí)現(xiàn)相關(guān)硬解加速。如在h264解碼器中可以使用cuda 加速,qsv加速,dxva2 加速,d3d11va加速,opencl加速等。cuda qsv等就是不同公司推出的針對(duì)gpu編程的工具包
AV_CODEC_ID_H264;代表是h264編解碼器。而name代表某一個(gè)編碼器或解碼器。通常我們使用avcodec_find_decoder(ID)和avcodec_find_encoder(ID)來解碼器和編碼器。默認(rèn)采用的軟件編解碼。如果我們需要使用硬件編解碼,采用avcodec_find_encoder_by_name(name)和avcodec_find_decoder_by_name(name)來指定編碼器。其他代碼流程與軟件編解碼一致。
//codec = avcodec_find_decoder(AV_CODEC_ID_H264); codec = avcodec_find_decoder_by_name("h264_cuvid"); if (!codec) { fprintf(stderr, "Codec not found\n"); exit(1); }
通過id找到的可能并不是你預(yù)期中的編解碼器 通過name找到的一定是你想要的
下面是ffmpeg官方的硬件解碼例子 我加上了中文注釋方便理解
#include <stdio.h> #include <libavcodec/avcodec.h> #include <libavformat/avformat.h> #include <libavutil/pixdesc.h> #include <libavutil/hwcontext.h> #include <libavutil/opt.h> #include <libavutil/avassert.h> #include <libavutil/imgutils.h> static AVBufferRef *hw_device_ctx = NULL; static enum AVPixelFormat hw_pix_fmt; static FILE *output_file = NULL; static int hw_decoder_init(AVCodecContext *ctx, const enum AVHWDeviceType type) { int err = 0; //創(chuàng)建硬件設(shè)備信息上下文 if ((err = av_hwdevice_ctx_create(&hw_device_ctx, type, NULL, NULL, 0)) < 0) { fprintf(stderr, "Failed to create specified HW device.\n"); return err; } //綁定編解碼器上下文和硬件設(shè)備信息上下文 ctx->hw_device_ctx = av_buffer_ref(hw_device_ctx); return err; } static enum AVPixelFormat get_hw_format(AVCodecContext *ctx, const enum AVPixelFormat *pix_fmts) { const enum AVPixelFormat *p; for (p = pix_fmts; *p != -1; p++) { if (*p == hw_pix_fmt) return *p; } fprintf(stderr, "Failed to get HW surface format.\n"); return AV_PIX_FMT_NONE; } static int decode_write(AVCodecContext *avctx, AVPacket *packet) { AVFrame *frame = NULL, *sw_frame = NULL; AVFrame *tmp_frame = NULL; uint8_t *buffer = NULL; int size; int ret = 0; ret = avcodec_send_packet(avctx, packet); if (ret < 0) { fprintf(stderr, "Error during decoding\n"); return ret; } while (1) { if (!(frame = av_frame_alloc()) || !(sw_frame = av_frame_alloc())) { fprintf(stderr, "Can not alloc frame\n"); ret = AVERROR(ENOMEM); goto fail; } ret = avcodec_receive_frame(avctx, frame); if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) { av_frame_free(&frame); av_frame_free(&sw_frame); return 0; } else if (ret < 0) { fprintf(stderr, "Error while decoding\n"); goto fail; } if (frame->format == hw_pix_fmt) { /* retrieve data from GPU to CPU */ if ((ret = av_hwframe_transfer_data(sw_frame, frame, 0)) < 0) { fprintf(stderr, "Error transferring the data to system memory\n"); goto fail; } tmp_frame = sw_frame; } else tmp_frame = frame; size = av_image_get_buffer_size(tmp_frame->format, tmp_frame->width, tmp_frame->height, 1); buffer = av_malloc(size); if (!buffer) { fprintf(stderr, "Can not alloc buffer\n"); ret = AVERROR(ENOMEM); goto fail; } ret = av_image_copy_to_buffer(buffer, size, (const uint8_t * const *)tmp_frame->data, (const int *)tmp_frame->linesize, tmp_frame->format, tmp_frame->width, tmp_frame->height, 1); if (ret < 0) { fprintf(stderr, "Can not copy image to buffer\n"); goto fail; } if ((ret = fwrite(buffer, 1, size, output_file)) < 0) { fprintf(stderr, "Failed to dump raw data.\n"); goto fail; } fail: av_frame_free(&frame); av_frame_free(&sw_frame); av_freep(&buffer); if (ret < 0) return ret; } } int main(int argc, char *argv[]) { AVFormatContext *input_ctx = NULL; int video_stream, ret; AVStream *video = NULL; AVCodecContext *decoder_ctx = NULL; AVCodec *decoder = NULL; AVPacket packet; enum AVHWDeviceType type; int i; if (argc < 4) { fprintf(stderr, "Usage: %s <device type> <input file> <output file>\n", argv[0]); return -1; } //通過你傳入的名字來找到對(duì)應(yīng)的硬件解碼類型 type = av_hwdevice_find_type_by_name(argv[1]); if (type == AV_HWDEVICE_TYPE_NONE) { fprintf(stderr, "Device type %s is not supported.\n", argv[1]); fprintf(stderr, "Available device types:"); while ((type = av_hwdevice_iterate_types(type)) != AV_HWDEVICE_TYPE_NONE) fprintf(stderr, " %s", av_hwdevice_get_type_name(type)); fprintf(stderr, "\n"); return -1; } /* open the input file */ if (avformat_open_input(&input_ctx, argv[2], NULL, NULL) != 0) { fprintf(stderr, "Cannot open input file '%s'\n", argv[2]); return -1; } if (avformat_find_stream_info(input_ctx, NULL) < 0) { fprintf(stderr, "Cannot find input stream information.\n"); return -1; } /* find the video stream information */ ret = av_find_best_stream(input_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, &decoder, 0); if (ret < 0) { fprintf(stderr, "Cannot find a video stream in the input file\n"); return -1; } video_stream = ret; //去遍歷所有編解碼器支持的硬件解碼配置 如果和之前你指定的是一樣的 那么就可以繼續(xù)執(zhí)行了 不然就找不到 for (i = 0;; i++) { const AVCodecHWConfig *config = avcodec_get_hw_config(decoder, i); if (!config) { fprintf(stderr, "Decoder %s does not support device type %s.\n", decoder->name, av_hwdevice_get_type_name(type)); return -1; } if (config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX && config->device_type == type) { //把硬件支持的像素格式設(shè)置進(jìn)去 hw_pix_fmt = config->pix_fmt; break; } } if (!(decoder_ctx = avcodec_alloc_context3(decoder))) return AVERROR(ENOMEM); video = input_ctx->streams[video_stream]; if (avcodec_parameters_to_context(decoder_ctx, video->codecpar) < 0) return -1; //填入回調(diào)函數(shù) 通過這個(gè)函數(shù) 編解碼器能夠知道顯卡支持的像素格式 decoder_ctx->get_format = get_hw_format; if (hw_decoder_init(decoder_ctx, type) < 0) return -1; //綁定完成后 打開編解碼器 if ((ret = avcodec_open2(decoder_ctx, decoder, NULL)) < 0) { fprintf(stderr, "Failed to open codec for stream #%u\n", video_stream); return -1; } /* open the file to dump raw data */ output_file = fopen(argv[3], "w+"); /* actual decoding and dump the raw data */ while (ret >= 0) { if ((ret = av_read_frame(input_ctx, &packet)) < 0) break; if (video_stream == packet.stream_index) ret = decode_write(decoder_ctx, &packet); av_packet_unref(&packet); } /* flush the decoder */ packet.data = NULL; packet.size = 0; ret = decode_write(decoder_ctx, &packet); av_packet_unref(&packet); if (output_file) fclose(output_file); avcodec_free_context(&decoder_ctx); avformat_close_input(&input_ctx); av_buffer_unref(&hw_device_ctx); return 0; }
關(guān)鍵函數(shù)解析
enum AVHWDeviceType av_hwdevice_find_type_by_name(const char *name);
通過傳入的參數(shù)查找對(duì)應(yīng)的硬件類型 其中 AVHWDeviceType值如下
const AVCodecHWConfig *avcodec_get_hw_config(const AVCodec *codec, int index);
拿到編解碼器支持的硬件配置比如硬件支持的像素格式等等
static enum AVPixelFormat get_hw_format(AVCodecContext *ctx, const enum AVPixelFormat *pix_fmts) { const enum AVPixelFormat *p; for (p = pix_fmts; *p != -1; p++) { if (*p == hw_pix_fmt) return *p; } fprintf(stderr, "Failed to get HW surface format.\n"); return AV_PIX_FMT_NONE; }
這是一個(gè)回調(diào)函數(shù),它的作用就是告訴解碼器codec自己的目標(biāo)像素格式是什么。在上一步驟獲取到了硬解碼器codec可以支持的目標(biāo)格式之后,就通過這個(gè)回調(diào)函數(shù)告知給codec
- fmt是這個(gè)解碼器codec支持的像素格式,且按照質(zhì)量?jī)?yōu)劣進(jìn)行排序;
- 如果沒有特別的需要,這個(gè)步驟是可以省略的。內(nèi)部默認(rèn)會(huì)使用“native”的格式。
int av_hwdevice_ctx_create(AVBufferRef **pdevice_ref, enum AVHWDeviceType type, const char *device, AVDictionary *opts, int flags)
這個(gè)函數(shù)的作用是,創(chuàng)建硬件設(shè)備相關(guān)的上下文信息AVHWDeviceContext,包括分配內(nèi)存資源、對(duì)硬件設(shè)備進(jìn)行初始化。
準(zhǔn)備好硬件設(shè)備上下文AVHWDeviceContext后,需要把這個(gè)信息綁定到AVCodecContext,就可以像軟解一樣的流程執(zhí)行解碼操作了。綁定操作如下
注意這里硬件設(shè)備信息上下文是通過AVBuffer來存儲(chǔ)的引用 并不是實(shí)體
int av_hwframe_transfer_data(AVFrame *dst, const AVFrame *src, int flags)
這個(gè)函數(shù)是負(fù)責(zé)在cpu內(nèi)存和硬件內(nèi)存(原文是hw surface)之間做數(shù)據(jù)交換的。也就是說,它不但可以把數(shù)據(jù)從硬件surface上搬回系統(tǒng)內(nèi)存,反向操作也支持;甚至可以直接在硬件內(nèi)存之間做數(shù)據(jù)交互。
到此這篇關(guān)于C++ ffmpeg硬件解碼的實(shí)現(xiàn)方法的文章就介紹到這了,更多相關(guān)C++ ffmpeg硬件解碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解C++內(nèi)存的代碼區(qū),全局區(qū),棧區(qū)和堆區(qū)
這篇文章主要為大家介紹了C++內(nèi)存的代碼區(qū),全局區(qū),棧區(qū)和堆區(qū),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2021-12-12C++實(shí)現(xiàn)LeetCode(47.全排列之二)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(47.全排列之二),本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07Matlab實(shí)現(xiàn)三維投影繪制的示例代碼
這篇文章系小編為大家?guī)砹艘粋€(gè)三維投影繪制函數(shù)(三視圖繪制),函數(shù)支持三維曲線、曲面、三維多邊形、參數(shù)方程曲線、參數(shù)方程曲面的投影繪制,需要的可以參考一下2022-08-08一文帶你了解C語言中static關(guān)鍵字的3個(gè)作用
static這個(gè)關(guān)鍵字是“靜態(tài)”的意思,在C語言里主要有3個(gè)作用。這篇文章主要通過一些簡(jiǎn)單示例為大家詳細(xì)講講這3個(gè)左右,感興趣的小伙伴可以了解一下2023-04-04