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

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

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

什么是硬件解碼

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

為什么要使用硬件解碼

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

怎樣使用硬件解碼

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

注意事項

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

對顯卡廠家SDK進(jìn)行封裝和集成,實現(xiàn)部分的硬件編解碼

其次在ffmpeg中軟件編解碼器可以實現(xiàn)相關(guān)硬解加速。如在h264解碼器中可以使用cuda 加速,qsv加速,dxva2 加速,d3d11va加速,opencl加速等。cuda qsv等就是不同公司推出的針對gpu編程的工具包

AV_CODEC_ID_H264;代表是h264編解碼器。而name代表某一個編碼器或解碼器。通常我們使用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;
	}
	//通過你傳入的名字來找到對應(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ù) 通過這個函數(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ù)查找對應(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;
}

這是一個回調(diào)函數(shù),它的作用就是告訴解碼器codec自己的目標(biāo)像素格式是什么。在上一步驟獲取到了硬解碼器codec可以支持的目標(biāo)格式之后,就通過這個回調(diào)函數(shù)告知給codec

  • fmt是這個解碼器codec支持的像素格式,且按照質(zhì)量優(yōu)劣進(jìn)行排序;
  • 如果沒有特別的需要,這個步驟是可以省略的。內(nèi)部默認(rèn)會使用“native”的格式。

int av_hwdevice_ctx_create(AVBufferRef **pdevice_ref, enum AVHWDeviceType type, const char *device, AVDictionary *opts, int flags)

這個函數(shù)的作用是,創(chuàng)建硬件設(shè)備相關(guān)的上下文信息AVHWDeviceContext,包括分配內(nèi)存資源、對硬件設(shè)備進(jìn)行初始化。

準(zhǔn)備好硬件設(shè)備上下文AVHWDeviceContext后,需要把這個信息綁定到AVCodecContext,就可以像軟解一樣的流程執(zhí)行解碼操作了。綁定操作如下

注意這里硬件設(shè)備信息上下文是通過AVBuffer來存儲的引用 并不是實體

int av_hwframe_transfer_data(AVFrame *dst, const AVFrame *src, int flags)

這個函數(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硬件解碼的實現(xiàn)方法的文章就介紹到這了,更多相關(guān)C++ ffmpeg硬件解碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

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

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

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

    通過c語言調(diào)用系統(tǒng)curl動態(tài)庫的示例詳解

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

    C語言圖文并茂詳解鏈接過程

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

    C++ 二叉樹的鏡像實例詳解

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

    C語言實現(xiàn)猜拳游戲

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

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

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

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

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

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

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

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

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

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

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

最新評論

两当县| 舒城县| 枝江市| 涪陵区| 甘肃省| 达拉特旗| 石河子市| 青河县| 广丰县| 三亚市| 历史| 金秀| 扎兰屯市| 沙河市| 庄浪县| 吴忠市| 伊宁市| 怀远县| 瑞金市| 沈阳市| 德格县| 新竹县| 虞城县| 正镶白旗| 洪泽县| 高雄市| 北京市| 平山县| 桦甸市| 恩施市| 咸阳市| 神木县| 东山县| 梁山县| 昭觉县| 普宁市| 宜宾市| 鹤壁市| 东乡县| 沙田区| 肇州县|