最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++ ffmpeg硬件解碼的實(shí)現(xiàn)方法

 更新時(shí)間:2022年08月25日 09:33:40   作者:殺神李  
這篇文章主要介紹了C++ ffmpeg硬件解碼的實(shí)現(xiàn),對(duì)FFmpeg多媒體解決方案中的視頻編解碼流程進(jìn)行研究。為嵌入式多媒體開(kāi)發(fā)提供參考,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

什么是硬件解碼

普通解碼是利用cpu去解碼也就是軟件解碼 硬件解碼就是利用gpu去解碼

為什么要使用硬件解碼

首先最大的好處 快硬解播放出來(lái)的視頻較為流暢,并且能夠延長(zhǎng)移動(dòng)設(shè)備播放視頻的時(shí)間; 而軟解由于軟解加大CPU工作負(fù)荷,會(huì)占用過(guò)多的移動(dòng)CPU資源,如果CPU能力不足,則軟件也將受到影響 最主要就是一個(gè)字 快

怎樣使用硬件解碼

ffmpeg內(nèi)部為我們提供了友好的接口去實(shí)現(xiàn)硬件解碼

注意事項(xiàng)

ffmpeg內(nèi)部有很多編解碼器 并不是所有的編解碼器都支持硬件解碼 并且就算支持硬件解碼的編解碼器也不一定能支持你的顯卡 也就是說(shuō)在使用硬件解碼時(shí)我們首先要去判斷這個(gè)解碼器是否支持在這個(gè)平臺(tái)對(duì)這個(gè)顯卡進(jìn)行硬件編解碼 不然是無(wú)法使用的

對(duì)顯卡廠(chǎng)家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)來(lái)解碼器和編碼器。默認(rèn)采用的軟件編解碼。如果我們需要使用硬件編解碼,采用avcodec_find_encoder_by_name(name)和avcodec_find_decoder_by_name(name)來(lái)指定編碼器。其他代碼流程與軟件編解碼一致。

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

通過(guò)id找到的可能并不是你預(yù)期中的編解碼器 通過(guò)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;
	}
	//通過(guò)你傳入的名字來(lái)找到對(duì)應(yīng)的硬件解碼類(lèi)型
	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ù) 通過(guò)這個(gè)函數(shù) 編解碼器能夠知道顯卡支持的像素格式
	decoder_ctx->get_format = get_hw_format;
	if (hw_decoder_init(decoder_ctx, type) < 0)
		return -1;
   //綁定完成后 打開(kāi)編解碼器
	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);

通過(guò)傳入的參數(shù)查找對(duì)應(yīng)的硬件類(lèi)型 其中 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)格式之后,就通過(guò)這個(gè)回調(diào)函數(shù)告知給codec

  • fmt是這個(gè)解碼器codec支持的像素格式,且按照質(zhì)量?jī)?yōu)劣進(jìn)行排序;
  • 如果沒(méi)有特別的需要,這個(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è)備信息上下文是通過(guò)AVBuffer來(lái)存儲(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ù)交換的。也就是說(shuō),它不但可以把數(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++基礎(chǔ)入門(mén)教程(九):函數(shù)指針之回調(diào)

    C++基礎(chǔ)入門(mén)教程(九):函數(shù)指針之回調(diào)

    這篇文章主要介紹了C++基礎(chǔ)入門(mén)教程(九):函數(shù)指針之回調(diào),本文講解了函數(shù)的地址、聲明函數(shù)指針、歷史原因、typedef挽救復(fù)雜的函數(shù)指針等內(nèi)容,需要的朋友可以參考下
    2014-11-11
  • 通過(guò)c語(yǔ)言調(diào)用系統(tǒng)curl動(dòng)態(tài)庫(kù)的示例詳解

    通過(guò)c語(yǔ)言調(diào)用系統(tǒng)curl動(dòng)態(tài)庫(kù)的示例詳解

    這篇文章中我們將通過(guò)一個(gè)簡(jiǎn)單的示例來(lái)講解如何在Ubuntu系統(tǒng)中通過(guò)C語(yǔ)言調(diào)用動(dòng)態(tài)庫(kù)(共享庫(kù))的方法,我們將使用libcurl庫(kù),這是一個(gè)基于客戶(hù)端的URL傳輸庫(kù),廣泛用于各種程序和應(yīng)用中以訪(fǎng)問(wèn)網(wǎng)頁(yè)和服務(wù)器數(shù)據(jù),需要的朋友可以參考下
    2024-03-03
  • C語(yǔ)言圖文并茂詳解鏈接過(guò)程

    C語(yǔ)言圖文并茂詳解鏈接過(guò)程

    首先來(lái)思考一個(gè)問(wèn)題:工程中的每個(gè)C語(yǔ)言源文件被編譯后生成的目標(biāo)文件,這些目標(biāo)文件如何生成最終的可執(zhí)行程序? 這就需要這節(jié)我們將要分析的鏈接器
    2022-04-04
  • C++ 二叉樹(shù)的鏡像實(shí)例詳解

    C++ 二叉樹(shù)的鏡像實(shí)例詳解

    這篇文章主要介紹了C++ 二叉樹(shù)的鏡像實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • C語(yǔ)言實(shí)現(xiàn)猜拳游戲

    C語(yǔ)言實(shí)現(xiàn)猜拳游戲

    這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)猜拳游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-02-02
  • 關(guān)于C語(yǔ)言qsort函數(shù)詳解

    關(guān)于C語(yǔ)言qsort函數(shù)詳解

    這篇文章主要介紹了關(guān)于C語(yǔ)言qsort函數(shù)詳解的相關(guān)資料,需要的朋友可以參考下面文章內(nèi)容
    2021-09-09
  • C++如何解決rand()函數(shù)生成的隨機(jī)數(shù)每次都一樣的問(wèn)題

    C++如何解決rand()函數(shù)生成的隨機(jī)數(shù)每次都一樣的問(wèn)題

    這篇文章主要介紹了C++如何解決rand()函數(shù)生成的隨機(jī)數(shù)每次都一樣的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • Qt?事件處理機(jī)制的深入理解

    Qt?事件處理機(jī)制的深入理解

    本文主要介紹了Qt?事件處理機(jī)制的深入理解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-04-04
  • C++超詳細(xì)講解模擬實(shí)現(xiàn)vector

    C++超詳細(xì)講解模擬實(shí)現(xiàn)vector

    這篇文章主要介紹了C++ 容器 Vector 的使用方法,Vector 是一個(gè)能夠存放任意類(lèi)型的動(dòng)態(tài)數(shù)組,有點(diǎn)類(lèi)似數(shù)組,是一個(gè)連續(xù)地址空間,下文更多詳細(xì)內(nèi)容的介紹,需要的小伙伴可以參考一下
    2022-07-07
  • C++中引用和const關(guān)鍵字介紹

    C++中引用和const關(guān)鍵字介紹

    大家好,本篇文章主要講的是C++中引用和const關(guān)鍵字介紹,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話(huà)記得收藏一下
    2022-02-02

最新評(píng)論

元江| 广汉市| 砀山县| 如东县| 嘉峪关市| 石棉县| 城市| 且末县| 天台县| 镇平县| 宣恩县| 独山县| 黎平县| 巴林右旗| 海城市| 伊金霍洛旗| 府谷县| 井陉县| 阜康市| 九龙坡区| 龙陵县| 米脂县| 南溪县| 浦江县| 洮南市| 黔西| 通道| 吴忠市| 大邑县| 宜章县| 卓资县| 垫江县| 陵川县| 浏阳市| 昭苏县| 祁东县| 花莲县| 巧家县| 松江区| 盐亭县| 刚察县|