惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Cisco Talos Blog
Cisco Talos Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Vercel News
Vercel News
B
Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
S
Schneier on Security
Blog — PlanetScale
Blog — PlanetScale
Google DeepMind News
Google DeepMind News
博客园 - 司徒正美
NISL@THU
NISL@THU
T
Threat Research - Cisco Blogs
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Latest news
Latest news
H
Help Net Security
雷峰网
雷峰网
Spread Privacy
Spread Privacy
Cyberwarzone
Cyberwarzone
Project Zero
Project Zero
Security Latest
Security Latest
Know Your Adversary
Know Your Adversary
人人都是产品经理
人人都是产品经理
P
Privacy & Cybersecurity Law Blog
M
MIT News - Artificial intelligence
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
P
Proofpoint News Feed
U
Unit 42
大猫的无限游戏
大猫的无限游戏
A
Arctic Wolf
博客园 - 三生石上(FineUI控件)
Stack Overflow Blog
Stack Overflow Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
Cybersecurity and Infrastructure Security Agency CISA
量子位
C
Cyber Attacks, Cyber Crime and Cyber Security
S
Securelist
S
Security @ Cisco Blogs
T
Threatpost
P
Palo Alto Networks Blog
C
Check Point Blog
V
Vulnerabilities – Threatpost
T
Tailwind CSS Blog
B
Blog RSS Feed
Recorded Future
Recorded Future
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
W
WeLiveSecurity
P
Proofpoint News Feed
P
Privacy International News Feed
AWS News Blog
AWS News Blog
博客园 - 叶小钗
WordPress大学
WordPress大学

博客园 - yeren2046

RedCoins,一个免费的类似bluecoins的个人财务管理软件 记录一个栈溢出导致的崩溃问题 一个免费的图片数据标注工具 常用zip命令 git版本导致的"Permission denied (publickey). fatal: Could not read from remote repository." 只显示全部特定进程名的top信息的shell脚本 linux 上用 core 文件定位线上问题 ffmpeg 时基转换 昇腾卡通道号范围 ffmpeg视频截取 英伟达硬解码错误汇总 DVPP问题汇总 TensorRT生成INT8校准文件 结构体指定初始化 ffmpeg命令行基于英伟达显卡编解码的转码 C++11 获取当前时间戳 基于CUDA查询显卡型号和显存大小 AV_PIX_FMT_CUDA 数据转 RGB C++ do{ } while(0)
nvjpeg 简单使用
yeren2046 · 2022-09-21 · via 博客园 - yeren2046

nvjpeg是英伟达提供的一个可以将显存中的数据直接压缩成jpg图片的sdk,包含在cuda套件中。

官方接口文档 https://docs.nvidia.com/cuda/nvjpeg/index.html#nvjpeg-encode-image

一个写得挺好的博客 nvJPEG压缩图像, 本文代码基本来自该博客。

代码:

#include <nvjpeg.h>

#include <fstream>
#include <vector>
#include <iostream>


#define CHECK_NVJPEG(S) do {nvjpegStatus_t  status; \
        status = S; \
        if (status != NVJPEG_STATUS_SUCCESS ) std::cout << __LINE__ <<" CHECK_NVJPEG - status = " << status << std::endl; \
        } while (false)


int saveJpeg(const char * filepath, unsigned char* d_srcBGR, int width, int height)
{
    nvjpegHandle_t nvjpeg_handle;
    nvjpegEncoderState_t encoder_state;
    nvjpegEncoderParams_t encoder_params;

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

    nvjpegImage_t input;
    nvjpegInputFormat_t input_format = NVJPEG_INPUT_BGRI;
    int image_width = width;
    int image_height = height;

    // 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));
    //     (cudaMemset(input.channel[i], 50 * 40 * i, channel_size));
    // }

    input.channel[0] = d_srcBGR;
    input.pitch[0] = image_width * 3;

    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
    CHECK_NVJPEG(nvjpegEncoderParamsSetEncoding(encoder_params, nvjpegJpegEncoding_t::NVJPEG_ENCODING_PROGRESSIVE_DCT_HUFFMAN, NULL));
    CHECK_NVJPEG(nvjpegEncoderParamsSetOptimizedHuffman(encoder_params, 1, NULL));
    CHECK_NVJPEG(nvjpegEncoderParamsSetQuality(encoder_params, 70, NULL));
    CHECK_NVJPEG(nvjpegEncoderParamsSetSamplingFactors(encoder_params, nvjpegChromaSubsampling_t::NVJPEG_CSS_420, NULL));

    cudaEventRecord(ev_start);
    CHECK_NVJPEG(nvjpegEncodeImage(nvjpeg_handle, encoder_state, encoder_params, &input, input_format, image_width, image_height, NULL));
    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));

    cudaEventSynchronize(ev_end);

    // 用完销毁,避免显存泄露
    nvjpegEncoderParamsDestroy(encoder_params);
    nvjpegEncoderStateDestroy(encoder_state);
    nvjpegDestroy(nvjpeg_handle);

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

    std::ofstream outputFile(filepath, std::ios::out | std::ios::binary);
    outputFile.write(reinterpret_cast<const char *>(obuffer.data()), static_cast<int>(length));
    outputFile.close();
    
    return 0;
}

需要说明的是,输入数据的形式由 input_format 指定,

image

image

从文档可知, NVJPEG_INPUT_BGRI 和 NVJPEG_INPUT_RGBI 是单数组形式存放的数据,赋值给channel[0],指定pitch[0]作为输入;

NVJPEG_INPUT_BGR 和 NVJPEG_INPUT_RGB 是分通道分别存放R、G、B 数据,pitch也分别对应。

测试效果样例:

image