Commit 615017759733017e12ae70bada5092928ebcc311
1 parent
a11c5683
添加showFrame
Showing
7 changed files
with
61 additions
and
59 deletions
FFNvDecoder/FFNvDecoder.vcxproj
... | ... | @@ -48,13 +48,13 @@ |
48 | 48 | <Optimization>Disabled</Optimization> |
49 | 49 | <PreprocessorDefinitions>WIN32;WIN64;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> |
50 | 50 | <AdditionalOptions>/utf-8</AdditionalOptions> |
51 | - <AdditionalIncludeDirectories>..\3rdparty\ffmpeg-5.0.1-win64-dev\include;./;./common/inc;./common/UtilNPP;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> | |
51 | + <AdditionalIncludeDirectories>..\3rdparty\ffmpeg-5.0.1-win64-dev\include;./;./common/inc;./common/UtilNPP;D:\win_dev\opencv\build\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> | |
52 | 52 | </ClCompile> |
53 | 53 | <Link> |
54 | 54 | <GenerateDebugInformation>true</GenerateDebugInformation> |
55 | 55 | <SubSystem>Console</SubSystem> |
56 | - <AdditionalDependencies>avcodec.lib;avdevice.lib;avfilter.lib;avformat.lib;avutil.lib;postproc.lib;swresample.lib;swscale.lib;cudart_static.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;nvjpeg.lib;%(AdditionalDependencies)</AdditionalDependencies> | |
57 | - <AdditionalLibraryDirectories>..\3rdparty\ffmpeg-5.0.1-win64-dev\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories> | |
56 | + <AdditionalDependencies>avcodec.lib;avdevice.lib;avfilter.lib;avformat.lib;avutil.lib;postproc.lib;swresample.lib;swscale.lib;cudart_static.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;nvjpeg.lib;opencv_world455d.lib;freeglut.lib;glew64.lib;%(AdditionalDependencies)</AdditionalDependencies> | |
57 | + <AdditionalLibraryDirectories>..\3rdparty\ffmpeg-5.0.1-win64-dev\lib;D:\win_dev\opencv\build\x64\vc14\lib;../3rdparty/gl;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories> | |
58 | 58 | </Link> |
59 | 59 | <CudaCompile> |
60 | 60 | <TargetMachinePlatform>64</TargetMachinePlatform> |
... | ... | @@ -80,7 +80,6 @@ |
80 | 80 | </CudaCompile> |
81 | 81 | </ItemDefinitionGroup> |
82 | 82 | <ItemGroup> |
83 | - <CudaCompile Include="cudaHeader.cu" /> | |
84 | 83 | <CudaCompile Include="NV12ToRGB.cu" /> |
85 | 84 | </ItemGroup> |
86 | 85 | <ItemGroup> |
... | ... | @@ -94,7 +93,6 @@ |
94 | 93 | </ItemGroup> |
95 | 94 | <ItemGroup> |
96 | 95 | <ClInclude Include="check_tool.h" /> |
97 | - <ClInclude Include="cudaHeader.h" /> | |
98 | 96 | <ClInclude Include="cuda_kernels.h" /> |
99 | 97 | <ClInclude Include="FFCuContextManager.h" /> |
100 | 98 | <ClInclude Include="FFNvDecoder.h" /> | ... | ... |
FFNvDecoder/FFNvDecoder.vcxproj.filters
1 | 1 | <?xml version="1.0" encoding="utf-8"?> |
2 | 2 | <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> |
3 | 3 | <ItemGroup> |
4 | - <CudaCompile Include="cudaHeader.cu"> | |
5 | - <Filter>cu_src</Filter> | |
6 | - </CudaCompile> | |
7 | 4 | <CudaCompile Include="NV12ToRGB.cu"> |
8 | 5 | <Filter>cu_src</Filter> |
9 | 6 | </CudaCompile> |
... | ... | @@ -58,9 +55,6 @@ |
58 | 55 | <ClInclude Include="FrameQueue.h"> |
59 | 56 | <Filter>include</Filter> |
60 | 57 | </ClInclude> |
61 | - <ClInclude Include="cudaHeader.h"> | |
62 | - <Filter>cu_src</Filter> | |
63 | - </ClInclude> | |
64 | 58 | <ClInclude Include="cuda_kernels.h"> |
65 | 59 | <Filter>cu_src</Filter> |
66 | 60 | </ClInclude> | ... | ... |
FFNvDecoder/NV12ToRGB.cu
... | ... | @@ -52,9 +52,9 @@ namespace cuda_common |
52 | 52 | return x; |
53 | 53 | } |
54 | 54 | } |
55 | - | |
56 | 55 | // CUDA kernel for outputing the final RGB output from NV12; |
57 | - __global__ void NV12ToRGB_drvapi2(uint32 *srcImage, size_t nSourcePitch, unsigned char *dstImage, int width, int height) | |
56 | + extern "C" | |
57 | + __global__ void NV12ToRGB_drvapi2(uint32 *srcImage, size_t nSourcePitch, unsigned char *dstImage, int width, int height) | |
58 | 58 | { |
59 | 59 | |
60 | 60 | int32 x, y; |
... | ... | @@ -169,8 +169,9 @@ namespace cuda_common |
169 | 169 | |
170 | 170 | } |
171 | 171 | |
172 | - // CUDA kernel for outputing the final RGB output from NV12; | |
173 | - __global__ void CUDAToBGR_drvapi(uint32 *dataY, uint32 *dataUV, size_t pitchY, size_t pitchUV, unsigned char *dstImage, int width, int height) | |
172 | + // CUDA kernel for outputing the final RGB output from NV12; | |
173 | + extern "C" | |
174 | + __global__ void CUDAToBGR_drvapi(uint32 *dataY, uint32 *dataUV, size_t pitchY, size_t pitchUV, unsigned char *dstImage, int width, int height) | |
174 | 175 | { |
175 | 176 | |
176 | 177 | int32 x, y; |
... | ... | @@ -307,7 +308,7 @@ namespace cuda_common |
307 | 308 | { |
308 | 309 | dim3 block(32, 16, 1); |
309 | 310 | dim3 grid((width + (2 * block.x - 1)) / (2 * block.x), (height + (block.y - 1)) / block.y, 1); |
310 | - NV12ToRGB_drvapi2 <<< grid, block >>>((uint32 *)d_srcNV12, nSourcePitch, d_dstRGB, width, height); | |
311 | + NV12ToRGB_drvapi2 << < grid, block >> >((uint32 *)d_srcNV12, nSourcePitch, d_dstRGB, width, height); | |
311 | 312 | cudaError_t cudaStatus = cudaGetLastError(); |
312 | 313 | if (cudaStatus != cudaSuccess) { |
313 | 314 | fprintf(stderr, "NV12ToRGB_drvapi launch failed: %s\n", cudaGetErrorString(cudaStatus)); |
... | ... | @@ -327,7 +328,7 @@ namespace cuda_common |
327 | 328 | { |
328 | 329 | dim3 block(32, 16, 1); |
329 | 330 | dim3 grid((width + (2 * block.x - 1)) / (2 * block.x), (height + (block.y - 1)) / block.y, 1); |
330 | - CUDAToBGR_drvapi <<< grid, block >>>((uint32 *)dataY, (uint32 *)dataUV, pitchY, pitchUV, d_dstRGB, width, height); | |
331 | + CUDAToBGR_drvapi << < grid, block >> >((uint32 *)dataY, (uint32 *)dataUV, pitchY, pitchUV, d_dstRGB, width, height); | |
331 | 332 | cudaError_t cudaStatus = cudaGetLastError(); |
332 | 333 | if (cudaStatus != cudaSuccess) { |
333 | 334 | fprintf(stderr, "NV12ToRGB_drvapi launch failed: %s\n", cudaGetErrorString(cudaStatus)); | ... | ... |
FFNvDecoder/cudaHeader.cu deleted
1 | -#include"cudaHeader.h" | |
2 | -#include<iostream> | |
3 | - | |
4 | -#include "cuda_runtime.h" | |
5 | -#include <cuda.h> | |
6 | - | |
7 | -//核函数,计算a+b | |
8 | -__global__ void add(int a,int b,int *c) | |
9 | -{ | |
10 | - //保存a+b的计算结果 | |
11 | - *c=a+b; | |
12 | -} | |
13 | - | |
14 | - | |
15 | -//cuda测试函数的实现 | |
16 | -void cudaTest() | |
17 | -{ | |
18 | - int c = 0; | |
19 | - //在gpu上开辟一个相同的内存 | |
20 | - int *deviceC; | |
21 | - cudaMalloc((void**)&deviceC,sizeof(int)); | |
22 | - //调用核函数 | |
23 | - add<<<1,1>>>(3,7,deviceC); | |
24 | - //把计算结果复制到cpu上 | |
25 | - cudaMemcpy(&c,deviceC,sizeof(int),cudaMemcpyDeviceToHost); | |
26 | - //展示计算结果 | |
27 | - std::cout<<c<<std::endl; | |
28 | - //释放内存 | |
29 | - cudaFree(deviceC); | |
30 | -} | |
31 | 0 | \ No newline at end of file |
FFNvDecoder/cudaHeader.h deleted
FFNvDecoder/dec.jpg deleted
638 KB
FFNvDecoder/main.cpp
... | ... | @@ -7,12 +7,13 @@ |
7 | 7 | |
8 | 8 | #include "check_tool.h" |
9 | 9 | |
10 | -#include "cudaHeader.h" | |
11 | - | |
12 | 10 | #include "cuda_kernels.h" |
13 | 11 | #include "NvJpegEncoder.h" |
14 | 12 | |
13 | +#include "opencv2\opencv.hpp" | |
14 | + | |
15 | 15 | using namespace std; |
16 | +using namespace cv; | |
16 | 17 | |
17 | 18 | unsigned char *pHwRgb[2] = {nullptr, nullptr}; |
18 | 19 | |
... | ... | @@ -44,6 +45,50 @@ void saveFrame(AVFrame * gpuFrame, string file_name) { |
44 | 45 | pHwData = nullptr; |
45 | 46 | } |
46 | 47 | |
48 | +mutex m_mutex_show; | |
49 | +void showFrame(AVFrame * gpuFrame) { | |
50 | + std::lock_guard<std::mutex> l(m_mutex_show); | |
51 | + | |
52 | + unsigned char *pHwData = nullptr; | |
53 | + cudaError_t cudaStatus = cudaMalloc((void **)&pHwData, 3 * gpuFrame->width * gpuFrame->height * sizeof(unsigned char)); | |
54 | + | |
55 | + cuda_common::setColorSpace(ITU709, 0); | |
56 | + cudaStatus = cuda_common::CUDAToBGR((CUdeviceptr)gpuFrame->data[0], (CUdeviceptr)gpuFrame->data[1], gpuFrame->linesize[0], gpuFrame->linesize[1], pHwData, gpuFrame->width, gpuFrame->height); | |
57 | + cudaDeviceSynchronize(); | |
58 | + if (cudaStatus != cudaSuccess) { | |
59 | + cout << "CUDAToBGR failed !!!" << endl; | |
60 | + return; | |
61 | + } | |
62 | + | |
63 | + | |
64 | + | |
65 | + unsigned char * pHwRgb = pHwData; | |
66 | + int channel = 3; | |
67 | + int width = gpuFrame->width; | |
68 | + int height = gpuFrame->height; | |
69 | + | |
70 | + if (pHwRgb != nullptr && channel > 0 && width > 0 && height > 0) { | |
71 | + int nSize = channel * height * width; | |
72 | + unsigned char* cpu_data = new unsigned char[nSize]; | |
73 | + | |
74 | + cudaMemcpy(cpu_data, pHwRgb, nSize * sizeof(unsigned char), cudaMemcpyDeviceToHost); | |
75 | + cudaDeviceSynchronize(); | |
76 | + | |
77 | + cv::Mat img_(height, width, CV_8UC3, cpu_data); | |
78 | + bool bWrite = cv::imwrite("dec0.jpg", img_); | |
79 | + | |
80 | + imshow("show", img_); | |
81 | + waitKey(0); | |
82 | + | |
83 | + delete[] cpu_data; | |
84 | + cpu_data = nullptr; | |
85 | + | |
86 | + } | |
87 | + | |
88 | + cudaFree(pHwData); | |
89 | + pHwData = nullptr; | |
90 | +} | |
91 | + | |
47 | 92 | /** |
48 | 93 | * 注意: gpuFrame 在解码器设置的显卡上,后续操作要十分注意这一点,尤其是多线程情况 |
49 | 94 | * */ |
... | ... | @@ -64,6 +109,7 @@ void postDecoded(const void * userPtr, AVFrame * gpuFrame){ |
64 | 109 | cudaSetDevice(atoi(decoder->m_cfg.gpuid.c_str())); |
65 | 110 | |
66 | 111 | saveFrame(gpuFrame, decoder->getName()); |
112 | + showFrame(gpuFrame); | |
67 | 113 | } |
68 | 114 | } |
69 | 115 | } |
... | ... | @@ -82,7 +128,7 @@ static long long get_cur_time(){ |
82 | 128 | return tpMicro.time_since_epoch().count(); |
83 | 129 | } |
84 | 130 | |
85 | -static int sum = 0; | |
131 | +static int suming = 0; | |
86 | 132 | unsigned char *pHwData = nullptr; |
87 | 133 | |
88 | 134 | void postDecoded0(const void * userPtr, AVFrame * gpuFrame){ |
... | ... | @@ -101,7 +147,7 @@ void postDecoded0(const void * userPtr, AVFrame * gpuFrame){ |
101 | 147 | end_time = start_time = get_cur_time(); |
102 | 148 | } |
103 | 149 | count_num++; |
104 | - sum ++ ; | |
150 | + suming ++ ; | |
105 | 151 | if (count_num >= count_std) |
106 | 152 | { |
107 | 153 | // end_time = get_cur_time(); |
... | ... | @@ -112,7 +158,7 @@ void postDecoded0(const void * userPtr, AVFrame * gpuFrame){ |
112 | 158 | |
113 | 159 | count_flag = false; |
114 | 160 | } |
115 | - cout << "帧数:" << sum << endl; | |
161 | + cout << "帧数:" << suming << endl; | |
116 | 162 | } |
117 | 163 | } |
118 | 164 | } |
... | ... | @@ -163,8 +209,6 @@ void logFF(void *, int level, const char *fmt, va_list ap) |
163 | 209 | |
164 | 210 | int main(int argc, char* argv[]) { |
165 | 211 | |
166 | - cudaTest(); | |
167 | - | |
168 | 212 | printf("start \n"); |
169 | 213 | if (argc != 3) { |
170 | 214 | fprintf(stderr, "./xxx uri gpu_id\n"); | ... | ... |