Commit e65720d49bbaf4c333cb079cf529c4eac88c32a1
1 parent
f40cc409
优化demo
Showing
4 changed files
with
105 additions
and
45 deletions
.vscode/settings.json
src/NvJpegEncoder.cpp
... | ... | @@ -11,7 +11,7 @@ |
11 | 11 | } while (false) |
12 | 12 | |
13 | 13 | |
14 | -int saveJpeg(const char * filepath, unsigned char* d_srcBGR, int width, int height) | |
14 | +int saveJpeg(const char * filepath, unsigned char* d_srcBGR, int width, int height, cudaStream_t stream) | |
15 | 15 | { |
16 | 16 | nvjpegHandle_t nvjpeg_handle; |
17 | 17 | nvjpegEncoderState_t encoder_state; |
... | ... | @@ -41,17 +41,17 @@ int saveJpeg(const char * filepath, unsigned char* d_srcBGR, int width, int heig |
41 | 41 | |
42 | 42 | CHECK_NVJPEG(nvjpegCreate(backend, nullptr, &nvjpeg_handle)); |
43 | 43 | |
44 | - CHECK_NVJPEG(nvjpegEncoderParamsCreate(nvjpeg_handle, &encoder_params, NULL)); | |
45 | - CHECK_NVJPEG(nvjpegEncoderStateCreate(nvjpeg_handle, &encoder_state, NULL)); | |
44 | + CHECK_NVJPEG(nvjpegEncoderParamsCreate(nvjpeg_handle, &encoder_params, stream)); | |
45 | + CHECK_NVJPEG(nvjpegEncoderStateCreate(nvjpeg_handle, &encoder_state, stream)); | |
46 | 46 | |
47 | 47 | // set params |
48 | - CHECK_NVJPEG(nvjpegEncoderParamsSetEncoding(encoder_params, nvjpegJpegEncoding_t::NVJPEG_ENCODING_PROGRESSIVE_DCT_HUFFMAN, NULL)); | |
49 | - CHECK_NVJPEG(nvjpegEncoderParamsSetOptimizedHuffman(encoder_params, 1, NULL)); | |
50 | - CHECK_NVJPEG(nvjpegEncoderParamsSetQuality(encoder_params, 70, NULL)); | |
51 | - CHECK_NVJPEG(nvjpegEncoderParamsSetSamplingFactors(encoder_params, nvjpegChromaSubsampling_t::NVJPEG_CSS_420, NULL)); | |
48 | + CHECK_NVJPEG(nvjpegEncoderParamsSetEncoding(encoder_params, nvjpegJpegEncoding_t::NVJPEG_ENCODING_PROGRESSIVE_DCT_HUFFMAN, stream)); | |
49 | + CHECK_NVJPEG(nvjpegEncoderParamsSetOptimizedHuffman(encoder_params, 1, stream)); | |
50 | + CHECK_NVJPEG(nvjpegEncoderParamsSetQuality(encoder_params, 70, stream)); | |
51 | + CHECK_NVJPEG(nvjpegEncoderParamsSetSamplingFactors(encoder_params, nvjpegChromaSubsampling_t::NVJPEG_CSS_420, stream)); | |
52 | 52 | |
53 | 53 | cudaEventRecord(ev_start); |
54 | - CHECK_NVJPEG(nvjpegEncodeImage(nvjpeg_handle, encoder_state, encoder_params, &input, input_format, image_width, image_height, NULL)); | |
54 | + CHECK_NVJPEG(nvjpegEncodeImage(nvjpeg_handle, encoder_state, encoder_params, &input, input_format, image_width, image_height, stream)); | |
55 | 55 | cudaEventRecord(ev_end); |
56 | 56 | |
57 | 57 | std::vector<unsigned char> obuffer; |
... | ... | @@ -61,7 +61,7 @@ int saveJpeg(const char * filepath, unsigned char* d_srcBGR, int width, int heig |
61 | 61 | encoder_state, |
62 | 62 | NULL, |
63 | 63 | &length, |
64 | - NULL)); | |
64 | + stream)); | |
65 | 65 | |
66 | 66 | obuffer.resize(length); |
67 | 67 | CHECK_NVJPEG(nvjpegEncodeRetrieveBitstream( |
... | ... | @@ -69,7 +69,7 @@ int saveJpeg(const char * filepath, unsigned char* d_srcBGR, int width, int heig |
69 | 69 | encoder_state, |
70 | 70 | obuffer.data(), |
71 | 71 | &length, |
72 | - NULL)); | |
72 | + stream)); | |
73 | 73 | |
74 | 74 | cudaEventSynchronize(ev_end); |
75 | 75 | |
... | ... | @@ -80,7 +80,7 @@ int saveJpeg(const char * filepath, unsigned char* d_srcBGR, int width, int heig |
80 | 80 | |
81 | 81 | float ms; |
82 | 82 | cudaEventElapsedTime(&ms, ev_start, ev_end); |
83 | - std::cout << "time spend " << ms << " ms" << std::endl; | |
83 | + // std::cout << "time spend " << ms << " ms" << std::endl; | |
84 | 84 | |
85 | 85 | std::ofstream outputFile(filepath, std::ios::out | std::ios::binary); |
86 | 86 | outputFile.write(reinterpret_cast<const char *>(obuffer.data()), static_cast<int>(length)); | ... | ... |
src/NvJpegEncoder.h
src/main.cpp
... | ... | @@ -10,7 +10,12 @@ |
10 | 10 | |
11 | 11 | #include <chrono> |
12 | 12 | |
13 | -unsigned char *pHwRgb = nullptr; | |
13 | +unsigned char *pHwRgb[2] = {nullptr, nullptr}; | |
14 | + | |
15 | +int sum1 = 0; | |
16 | +int sum2 = 0; | |
17 | + | |
18 | +cudaStream_t stream[2]; | |
14 | 19 | |
15 | 20 | /** |
16 | 21 | * 注意: gpuFrame 在解码器设置的显卡上,后续操作要十分注意这一点,尤其是多线程情况 |
... | ... | @@ -21,35 +26,63 @@ void postDecoded(const void * userPtr, AVFrame * gpuFrame){ |
21 | 26 | { |
22 | 27 | // cout << "decode name: " << decoder->getName() << endl; |
23 | 28 | |
24 | - if (decoder->getName() == "dec1") | |
25 | - { | |
26 | - /* code */ | |
27 | - } | |
28 | - | |
29 | - // const char* gpu_pixfmt = av_get_pix_fmt_name((AVPixelFormat)gpuFrame->format); | |
30 | - // cout << "pixfmt: " << gpu_pixfmt << endl; | |
31 | - // cout << "keyframe: " << gpuFrame->key_frame << " width: " << gpuFrame->width << " height: "<< gpuFrame->height << endl; | |
32 | - // cout << "decode successed ✿✿ヽ(°▽°)ノ✿ " << endl; | |
33 | - | |
34 | - if (gpuFrame->format == AV_PIX_FMT_CUDA) | |
35 | - { | |
36 | - // cout << "gpuid = " << atoi(decoder->m_cfg.gpuid.c_str()) << endl; | |
37 | - // cudaSetDevice(atoi(decoder->m_cfg.gpuid.c_str())); | |
38 | - // cudaError_t cudaStatus; | |
39 | - // if(pHwRgb == nullptr){ | |
40 | - // cuda_common::setColorSpace2( ITU709, 0 ); | |
41 | - // cudaStatus = cudaMalloc((void **)&pHwRgb, 3 * gpuFrame->width * gpuFrame->height * sizeof(unsigned char)); | |
42 | - // } | |
43 | - // cudaStatus = cuda_common::CUDAToBGR((CUdeviceptr)gpuFrame->data[0],(CUdeviceptr)gpuFrame->data[1], gpuFrame->linesize[0], gpuFrame->linesize[1], pHwRgb, gpuFrame->width, gpuFrame->height); | |
44 | - // cudaDeviceSynchronize(); | |
45 | - // if (cudaStatus != cudaSuccess) { | |
46 | - // cout << "CUDAToBGR failed !!!" << endl; | |
47 | - // return; | |
48 | - // } | |
49 | - | |
50 | - // string path = "/home/cmhu/FFNvDecoder/" + decoder->m_cfg.gpuid + ".jpg"; | |
51 | - // saveJpeg(path.c_str(), pHwRgb, gpuFrame->width, gpuFrame->height); // 验证 CUDAToRGB | |
52 | - } | |
29 | + // const char* gpu_pixfmt = av_get_pix_fmt_name((AVPixelFormat)gpuFrame->format); | |
30 | + // cout << "pixfmt: " << gpu_pixfmt << endl; | |
31 | + // cout << "keyframe: " << gpuFrame->key_frame << " width: " << gpuFrame->width << " height: "<< gpuFrame->height << endl; | |
32 | + // cout << "decode successed ✿✿ヽ(°▽°)ノ✿ " << endl; | |
33 | + | |
34 | + int sum = sum1; | |
35 | + if (decoder->getName() == "dec1") | |
36 | + { | |
37 | + sum1 ++ ; | |
38 | + sum = sum1; | |
39 | + | |
40 | + if (gpuFrame->format == AV_PIX_FMT_CUDA) | |
41 | + { | |
42 | + cout << "gpuid = " << atoi(decoder->m_cfg.gpuid.c_str()) << endl; | |
43 | + cudaSetDevice(atoi(decoder->m_cfg.gpuid.c_str())); | |
44 | + cudaError_t cudaStatus; | |
45 | + if(pHwRgb[0] == nullptr){ | |
46 | + // cudaStreamCreate(&stream[0]); | |
47 | + cuda_common::setColorSpace2( ITU709, 0 ); | |
48 | + cudaStatus = cudaMalloc((void **)&pHwRgb[0], 3 * gpuFrame->width * gpuFrame->height * sizeof(unsigned char)); | |
49 | + } | |
50 | + cudaStatus = cuda_common::CUDAToBGR((CUdeviceptr)gpuFrame->data[0],(CUdeviceptr)gpuFrame->data[1], gpuFrame->linesize[0], gpuFrame->linesize[1], pHwRgb[0], gpuFrame->width, gpuFrame->height); | |
51 | + cudaDeviceSynchronize(); | |
52 | + if (cudaStatus != cudaSuccess) { | |
53 | + cout << "CUDAToBGR failed !!!" << endl; | |
54 | + return; | |
55 | + } | |
56 | + | |
57 | + string path = "/home/cmhu/data/" + decoder->getName() + "/" + to_string(sum) + ".jpg"; | |
58 | + saveJpeg(path.c_str(), pHwRgb[0], gpuFrame->width, gpuFrame->height, stream[0]); // 验证 CUDAToRGB | |
59 | + } | |
60 | + } else if (decoder->getName() == "dec2") | |
61 | + { | |
62 | + sum2 ++ ; | |
63 | + sum = sum2; | |
64 | + | |
65 | + if (gpuFrame->format == AV_PIX_FMT_CUDA) | |
66 | + { | |
67 | + cout << "gpuid = " << atoi(decoder->m_cfg.gpuid.c_str()) << endl; | |
68 | + cudaSetDevice(atoi(decoder->m_cfg.gpuid.c_str())); | |
69 | + cudaError_t cudaStatus; | |
70 | + if(pHwRgb[1] == nullptr){ | |
71 | + // cudaStreamCreate(&stream[1]); | |
72 | + cuda_common::setColorSpace2( ITU709, 0 ); | |
73 | + cudaStatus = cudaMalloc((void **)&pHwRgb[1], 3 * gpuFrame->width * gpuFrame->height * sizeof(unsigned char)); | |
74 | + } | |
75 | + cudaStatus = cuda_common::CUDAToBGR((CUdeviceptr)gpuFrame->data[0],(CUdeviceptr)gpuFrame->data[1], gpuFrame->linesize[0], gpuFrame->linesize[1], pHwRgb[1], gpuFrame->width, gpuFrame->height); | |
76 | + cudaDeviceSynchronize(); | |
77 | + if (cudaStatus != cudaSuccess) { | |
78 | + cout << "CUDAToBGR failed !!!" << endl; | |
79 | + return; | |
80 | + } | |
81 | + | |
82 | + string path = "/home/cmhu/data/" + decoder->getName() + "/" + to_string(sum) + ".jpg"; | |
83 | + saveJpeg(path.c_str(), pHwRgb[1], gpuFrame->width, gpuFrame->height, stream[1]); // 验证 CUDAToRGB | |
84 | + } | |
85 | + } | |
53 | 86 | } |
54 | 87 | } |
55 | 88 | |
... | ... | @@ -72,6 +105,8 @@ long long get_cur_time(){ |
72 | 105 | } |
73 | 106 | |
74 | 107 | int sum = 0; |
108 | +unsigned char *pHwData = nullptr; | |
109 | + | |
75 | 110 | void postDecoded0(const void * userPtr, AVFrame * gpuFrame){ |
76 | 111 | FFNvDecoder* decoder = (FFNvDecoder*)userPtr; |
77 | 112 | if (decoder!= nullptr) |
... | ... | @@ -98,12 +133,33 @@ void postDecoded0(const void * userPtr, AVFrame * gpuFrame){ |
98 | 133 | |
99 | 134 | count_flag = false; |
100 | 135 | } |
136 | + | |
137 | + if (gpuFrame->format == AV_PIX_FMT_CUDA) | |
138 | + { | |
139 | + cudaSetDevice(atoi(decoder->m_cfg.gpuid.c_str())); | |
140 | + cout << "gpu id : " << decoder->m_cfg.gpuid.c_str() << endl; | |
141 | + cudaError_t cudaStatus; | |
142 | + if(pHwData == nullptr){ | |
143 | + cuda_common::setColorSpace2( ITU709, 0 ); | |
144 | + cudaStatus = cudaMalloc((void **)&pHwData, 3 * gpuFrame->width * gpuFrame->height * sizeof(unsigned char)); | |
145 | + } | |
146 | + cudaStatus = cuda_common::CUDAToBGR((CUdeviceptr)gpuFrame->data[0],(CUdeviceptr)gpuFrame->data[1], gpuFrame->linesize[0], gpuFrame->linesize[1], pHwData, gpuFrame->width, gpuFrame->height); | |
147 | + cudaDeviceSynchronize(); | |
148 | + if (cudaStatus != cudaSuccess) { | |
149 | + cout << "CUDAToBGR failed !!!" << endl; | |
150 | + return; | |
151 | + } | |
152 | + | |
153 | + string path = "/home/cmhu/data/test/" + to_string(sum) + ".jpg"; | |
154 | + saveJpeg(path.c_str(), pHwData, gpuFrame->width, gpuFrame->height, nullptr); // 验证 CUDAToRGB | |
155 | + } | |
101 | 156 | } |
102 | 157 | } |
103 | 158 | } |
104 | 159 | |
105 | 160 | // string test_uri = "rtmp://192.168.10.56:1935/objecteye/1"; |
106 | -string test_uri = "/home/cmhu/data/test.mp4"; | |
161 | +// string test_uri = "/home/cmhu/data/output_800x480.mp4"; | |
162 | +string test_uri = "/home/cmhu/data/output_1920x1080.mp4"; | |
107 | 163 | |
108 | 164 | void createDecode(int index){ |
109 | 165 | FFNvDecoderManager* pDecManager = FFNvDecoderManager::getInstance(); |
... | ... | @@ -170,7 +226,7 @@ int main(){ |
170 | 226 | |
171 | 227 | FFNvDecoderManager* pDecManager = FFNvDecoderManager::getInstance(); |
172 | 228 | |
173 | - int count = 105; | |
229 | + int count = 99; | |
174 | 230 | for (size_t i = 0; i < count ; i++) |
175 | 231 | { |
176 | 232 | createDecode(i); | ... | ... |