nvJPEG压缩图像

定义用到的变量

nvjpegHandle_t nvjpeg_handle;         // nvjpeg句柄
nvjpegEncoderState_t encoder_state;   // 
nvjpegEncoderParams_t encoder_params; // 

准备图像数据,输入数据指针应该是显存指针,每个颜色分量分别存储

nvjpegImage_t input;
nvjpegInputFormat_t input_format = NVJPEG_INPUT_BGR;

int image_width = 2448;
int image_height = 2048;
int channel_size = image_width * image_height;
for (int i = 0; i < 3; i++)
{
	input.pitch[i] = image_width;
	cudaMalloc((void**)&(input.channel[i]), channel_size);
	// set value of this channel
	cudaMemset(input.channel[i], 50 * 40 * i, channel_size);
}

准备环境

nvjpegBackend_t backend = NVJPEG_BACKEND_DEFAULT;
nvjpegCreate(backend, nullptr, &nvjpeg_handle));
nvjpegEncoderStateCreate(nvjpeg_handle, &encoder_state, NULL);
nvjpegEncoderParamsCreate(nvjpeg_handle, &encoder_params, NULL);

设置参数

nvjpegEncoderParamsSetEncoding(encoder_params, nvjpegJpegEncoding_t::NVJPEG_ENCODING_PROGRESSIVE_DCT_HUFFMAN, NULL);
nvjpegEncoderParamsSetOptimizedHuffman(encoder_params, 1, NULL);
nvjpegEncoderParamsSetQuality(encoder_params, 70, NULL);
nvjpegEncoderParamsSetSamplingFactors(encoder_params, nvjpegChromaSubsampling_t::NVJPEG_CSS_420, NULL);

执行压缩

nvjpegEncodeImage(nvjpeg_handle, encoder_state, encoder_params, &input, input_format, image_width, image_height, NULL)

取出二进制数据

std::vector<unsigned char> obuffer;
size_t length;
nvjpegEncodeRetrieveBitstream( nvjpeg_handle, encoder_state, NULL, &length, NULL);

obuffer.resize(length);
nvjpegEncodeRetrieveBitstream( nvjpeg_handle, encoder_state, obuffer.data(), &length, NULL));

保存二进制数据

std::ofstream outputFile("result.jpg", std::ios::out | std::ios::binary);
outputFile.write(reinterpret_cast<const char *>(obuffer.data()), static_cast<int>(length));

完整代码

int testEncodeImage()
{
	nvjpegHandle_t nvjpeg_handle;
	nvjpegEncoderState_t encoder_state;
	nvjpegEncoderParams_t encoder_params;

	cudaEvent_t ev_start, ev_end;
	CHECK_CUDA(cudaEventCreate(&ev_start));
	CHECK_CUDA(cudaEventCreate(&ev_end));

	size_t pdata_len = 1024 * 1024;
	unsigned char *pdata_dev = nullptr;
	CHECK_CUDA(cudaMalloc((void**)&pdata_dev, pdata_len));
	

	nvjpegImage_t input;
	nvjpegInputFormat_t input_format = NVJPEG_INPUT_BGR;
	int image_width = 2448;
	int image_height = 2048;
	int channel_size = image_width * image_height;

	for (int i = 0; i < 3; i++)
	{
		input.pitch[i] = image_width;
		CHECK_CUDA(cudaMalloc((void**)&(input.channel[i]), channel_size));
		CHECK_CUDA(cudaMemset(input.channel[i], 50 * 40 * i, channel_size));
	}

	nvjpegBackend_t backend = NVJPEG_BACKEND_DEFAULT;

	CHECK_NVJPEG(nvjpegCreate(backend, nullptr, &nvjpeg_handle));
	
	CHECK_NVJPEG(nvjpegEncoderParamsCreate(nvjpeg_handle, &encoder_params, NULL));
	CHECK_NVJPEG(nvjpegEncoderStateCreate(nvjpeg_handle, &encoder_state, NULL));

	// set params
	nvjpegEncoderParamsSetEncoding(encoder_params, nvjpegJpegEncoding_t::NVJPEG_ENCODING_PROGRESSIVE_DCT_HUFFMAN, NULL);
	nvjpegEncoderParamsSetOptimizedHuffman(encoder_params, 1, NULL);
	nvjpegEncoderParamsSetQuality(encoder_params, 70, NULL);
	nvjpegEncoderParamsSetSamplingFactors(encoder_params, nvjpegChromaSubsampling_t::NVJPEG_CSS_420, NULL);

	CHECK_CUDA(cudaEventRecord(ev_start));
	CHECK_NVJPEG(nvjpegEncodeImage(nvjpeg_handle, encoder_state, encoder_params, &input, input_format, image_width, image_height, NULL));
	CHECK_CUDA(cudaEventRecord(ev_end));

	std::vector<unsigned char> obuffer;
	size_t length;
	CHECK_NVJPEG(nvjpegEncodeRetrieveBitstream(
		nvjpeg_handle,
		encoder_state,
		NULL,
		&length,
		NULL));

	obuffer.resize(length);
	CHECK_NVJPEG(nvjpegEncodeRetrieveBitstream(
		nvjpeg_handle,
		encoder_state,
		obuffer.data(),
		&length,
		NULL));

	std::ofstream outputFile("a.jpg", std::ios::out | std::ios::binary);
	outputFile.write(reinterpret_cast<const char *>(obuffer.data()), static_cast<int>(length));

	cudaEventSynchronize(ev_end);

	float ms;
	cudaEventElapsedTime(&ms, ev_start, ev_end);
	std::cout << "time spend " << ms << " ms" << std::endl;
	
	return 0;
}

 时间消耗

2440*2048大小的RGB三色图像压缩耗时4.33ms,使用CPU单线程压缩耗时98ms(opencv)。

posted @ 2022-03-06 09:47  陈小蓝  阅读(1221)  评论(1编辑  收藏  举报