Commit 00b0fbdb28050ddecec6db2f21a697ec6373e30a

Authored by Hu Chunming
1 parent d248da62

编译nvdec

.vscode/launch.json
... ... @@ -19,6 +19,24 @@
19 19 "ignoreFailures": true
20 20 }
21 21 ]
  22 + },{
  23 + "name": "nvdec",
  24 + "type": "cppdbg",
  25 + "request": "launch",
  26 + "program": "${workspaceFolder}/bin/lib/demo",
  27 + "args": ["/mnt/data/cmhu/data/caishenkezhan.mp4","0", "0"],
  28 + "stopAtEntry": false,
  29 + "cwd": "${workspaceFolder}/bin/lib",
  30 + "environment": [],
  31 + "externalConsole": false,
  32 + "MIMode": "gdb",
  33 + "setupCommands": [
  34 + {
  35 + "description": "Enable pretty-printing for gdb",
  36 + "text": "-enable-pretty-printing",
  37 + "ignoreFailures": true
  38 + }
  39 + ]
22 40 }
23 41 ]
24 42 }
25 43 \ No newline at end of file
... ...
src/Makefile
... ... @@ -7,7 +7,7 @@ BUILD_DIR:=$(TOP_DIR)/build
7 7 BIN_DIR:=$(BUILD_DIR)/bin
8 8 export BUILD_DIR
9 9  
10   -PROJECT_ROOT= /home/huchunming/FFNvDecoder
  10 +PROJECT_ROOT= /mnt/data/cmhu/FFNvDecoder
11 11  
12 12 DEPEND_DIR = $(PROJECT_ROOT)/bin
13 13 SRC_ROOT = $(PROJECT_ROOT)/src
... ... @@ -29,11 +29,12 @@ LDFLAGS:=
29 29 LIBS:= -L $(SPDLOG_ROOT)/lib -l:libspdlog.a \
30 30  
31 31 # 各个模块
32   -MODULES:= dvpp interface demo
  32 +MODULES:= nvdecoder gb28181 interface demo
33 33  
34 34 # 各个模块对应的库
35   -MODULE_LIBS:=$(BUILD_DIR)/dvpp/lib/libdvpp.a\
36   - $(BUILD_DIR)/interface/lib/interface.a\
  35 +# MODULE_LIBS:=$(BUILD_DIR)/nvdecoder/lib/nvdecoder.a\
  36 +# $(BUILD_DIR)/nvdecoder/lib/gb28181.a\
  37 +# $(BUILD_DIR)/interface/lib/interface.a\
37 38  
38 39 # 最终目标文件
39 40 TARGET:=$(BIN_DIR)/test
... ... @@ -45,7 +46,7 @@ all:$(TARGET)
45 46 # 最终目标依赖关系
46 47 $(TARGET):FORCE | $(BIN_DIR)
47 48 @for n in $(MODULES); do make -s -f $(TOP_DIR)/$$n/Makefile MODULE=$$n || exit "$$?"; done
48   - @echo -e "\e[32m""Linking executable $(TARGET)""\e[0m"
  49 +# @echo -e "\e[32m""Linking executable $(TARGET)""\e[0m"
49 50 #@$(LD) $(LDFLAGS) -o $@ $(MODULE_LIBS) $(LIBS)
50 51  
51 52 # 若没有bin目录则自动生成
... ...
src/Makefile.bak
... ... @@ -8,12 +8,10 @@ PROJECT_ROOT= /home/huchunming/FFNvDecoder
8 8  
9 9 DEPEND_DIR = $(PROJECT_ROOT)/bin
10 10 SRC_ROOT = $(PROJECT_ROOT)/src
11   -THIRDPARTY_ROOT = $(PROJECT_ROOT)/3rdparty
12   -
13 11  
14 12 TARGET= $(DEPEND_DIR)/lib/test
15 13  
16   -
  14 +THIRDPARTY_ROOT = $(PROJECT_ROOT)/3rdparty
17 15 SPDLOG_ROOT = $(THIRDPARTY_ROOT)/spdlog-1.9.2/release
18 16 JRTP_ROOT = $(THIRDPARTY_ROOT)/jrtp_export
19 17  
... ...
src/Makefile.bak0308 deleted
1   -# 项目根目录
2   -TOP_DIR:=$(patsubst %/, %, $(dir $(abspath $(lastword $(MAKEFILE_LIST)))))
3   -export TOP_DIR
4   -
5   -# 各项目录
6   -BUILD_DIR:=$(TOP_DIR)/build
7   -BIN_DIR:=$(BUILD_DIR)/bin
8   -export BUILD_DIR
9   -
10   -PROJECT_ROOT= /home/huchunming/FFNvDecoder
11   -
12   -DEPEND_DIR = $(PROJECT_ROOT)/bin
13   -SRC_ROOT = $(PROJECT_ROOT)/src
14   -THIRDPARTY_ROOT = $(PROJECT_ROOT)/3rdparty
15   -SPDLOG_ROOT = $(THIRDPARTY_ROOT)/spdlog-1.9.2/release
16   -
17   -#编译器
18   -CXX:=g++
19   -CXXFLAGS:=-std=c++11 -Wall -Wextra -Og -g
20   -INCS:=-I $(TOP_DIR) \
21   - -I $(SPDLOG_ROOT)/include \
22   -
23   -MACROS:=
24   -export CXX CXXFLAGS INCS MACROS
25   -
26   -# 链接器
27   -LD:=g++
28   -LDFLAGS:=
29   -LIBS:= -L $(SPDLOG_ROOT)/lib -l:libspdlog.a \
30   -
31   -# 各个模块
32   -MODULES:= dvpp interface
33   -
34   -# 各个模块对应的库
35   -MODULE_LIBS:=$(BUILD_DIR)/dvpp/lib/libdvpp.a\
36   - $(BUILD_DIR)/interface/lib/interface.a\
37   -
38   -# 最终目标文件
39   -TARGET:=$(BIN_DIR)/test
40   -
41   -# 默认最终目标
42   -.PHONY:all
43   -all:$(TARGET)
44   -
45   -# 最终目标依赖关系
46   -$(TARGET):FORCE | $(BIN_DIR)
47   - @for n in $(MODULES); do make -s -f $(TOP_DIR)/$$n/Makefile MODULE=$$n || exit "$$?"; done
48   - @echo -e "\e[32m""Linking executable $(TARGET)""\e[0m"
49   -#@$(LD) $(LDFLAGS) -o $@ $(MODULE_LIBS) $(LIBS)
50   -
51   -# 若没有bin目录则自动生成
52   -$(BIN_DIR):
53   - @mkdir -p $@
54   -
55   -# 强制执行命令
56   -.PHONY:FORCE
57   -FORCE:
58   -
59   -# make clean直接删除整个build目录
60   -.PHONY:clean
61   -clean:
62   - @rm -rf $(BUILD_DIR)
src/demo/Makefile
1 1 XX = g++
2 2  
3 3  
4   -PROJECT_ROOT= /home/huchunming/FFNvDecoder
  4 +PROJECT_ROOT= /mnt/data/cmhu/FFNvDecoder
  5 +
  6 +CUDA_ROOT = /usr/local/cuda-11.1
5 7  
6 8 DEPEND_DIR = $(PROJECT_ROOT)/bin
7   -SRC_ROOT = $(PROJECT_ROOT)/src
8 9 THIRDPARTY_ROOT = $(PROJECT_ROOT)/3rdparty
9   -
10   -
11   -TARGET= /home/huchunming/FFNvDecoder/src/build/bin/demo
12   -
13   -
14 10 SPDLOG_ROOT = $(THIRDPARTY_ROOT)/spdlog-1.9.2/release
15 11 JRTP_ROOT = $(THIRDPARTY_ROOT)/jrtp_export
16 12  
  13 +SRC_ROOT = $(PROJECT_ROOT)/src
17 14  
18   -include_dir=-I/usr/local/Ascend/ascend-toolkit/6.3.RC1.alpha001/runtime/include
19   -lib_dir=-L/usr/lib \
20   - -L/usr/local/lib \
21   - -L/usr/local/Ascend/driver/lib64 \
22   - -L/usr/local/Ascend/ascend-toolkit/6.3.RC1.alpha001/atc/lib64\
23   - -L/usr/local/Ascend/ascend-toolkit/6.3.RC1.alpha001/runtime/lib64 \
24   - -L/usr/local/Ascend/ascend-toolkit/6.3.RC1.alpha001/runtime/lib64/stub \
25   - -L/usr/local/Ascend/ascend-toolkit/6.3.RC1.alpha001/lib64 \
26   - -L/usr/local/Ascend/driver/lib64/driver
27   -
28   -lib=-lacl_dvpp -lascendcl -lmmpa -lglog -lgflags -lpthread -lz -lacl_dvpp_mpi -lruntime -lascendalog -lc_sec -lmsprofiler -lgert -lge_executor -lge_common \
29   - -lgraph -lascend_protobuf -lprofapi -lerror_manager -lexe_graph -lregister -lplatform
  15 +TARGET= $(PROJECT_ROOT)/bin/lib/demo
30 16  
31 17 DEFS = -DENABLE_DVPP_INTERFACE
32 18  
33 19 INCLUDE= -I $(SRC_ROOT)/interface \
34 20 -I $(SRC_ROOT)/dvpp \
  21 + -I $(SRC_ROOT)/gb28181 \
  22 + -I $(SRC_ROOT)/nvdecoder \
  23 + -I $(DEPEND_DIR)/include \
  24 + -I $(CUDA_ROOT)/include \
  25 + -I $(TOP_DIR)/common/inc \
  26 + -I $(TOP_DIR)/common/UtilNPP \
  27 + -I $(TOP_DIR)/ \
  28 + -I $(SPDLOG_ROOT)/include \
  29 + -I $(JRTP_ROOT)/jrtplib/include/jrtplib3 \
  30 + -I $(JRTP_ROOT)/jthread/include/jthread
  31 +
  32 +LIBSPATH= -L $(DEPEND_DIR)/lib -lavformat -lavcodec -lswscale -lavutil -lavfilter -lswresample -lavdevice \
  33 + -L $(CUDA_ROOT)/lib64 -lcuda -lcudart -lnvcuvid -lcurand -lcublas -lnvjpeg \
  34 + -L $(SPDLOG_ROOT) -l:libspdlog.a \
  35 + -L $(JRTP_ROOT)/jthread/lib -l:libjthread.a \
  36 + -L $(JRTP_ROOT)/jrtplib/lib -l:libjrtp.a
  37 +
  38 +
  39 +# include_dir=-I/usr/local/Ascend/ascend-toolkit/6.3.RC1.alpha001/runtime/include
  40 +
  41 +# lib_dir=-L/usr/lib \
  42 +# -L/usr/local/lib \
  43 +# -L/usr/local/Ascend/driver/lib64 \
  44 +# -L/usr/local/Ascend/ascend-toolkit/6.3.RC1.alpha001/atc/lib64\
  45 +# -L/usr/local/Ascend/ascend-toolkit/6.3.RC1.alpha001/runtime/lib64 \
  46 +# -L/usr/local/Ascend/ascend-toolkit/6.3.RC1.alpha001/runtime/lib64/stub \
  47 +# -L/usr/local/Ascend/ascend-toolkit/6.3.RC1.alpha001/lib64 \
  48 +# -L/usr/local/Ascend/driver/lib64/driver
  49 +
  50 +# lib=-lacl_dvpp -lascendcl -lmmpa -lglog -lgflags -lpthread -lz -lacl_dvpp_mpi -lruntime -lascendalog -lc_sec -lmsprofiler -lgert -lge_executor -lge_common \
  51 +# -lgraph -lascend_protobuf -lprofapi -lerror_manager -lexe_graph -lregister -lplatform
  52 +# LIBS= -lavformat -lavcodec -lswscale -lavutil -lavfilter -lswresample -lavdevice
  53 +# CXXFLAGS= -g -O0 -fPIC $(INCLUDE) $(include_dir) $(DEFS) -lpthread -lrt -lz -fexceptions -std=c++11 -fvisibility=hidden -Wl,-Bsymbolic -ldl
35 54  
36   -LIBSPATH=
37   -
38   -LIBS= -lavformat -lavcodec -lswscale -lavutil -lavfilter -lswresample -lavdevice
39   -
40   -CXXFLAGS= -g -O0 -fPIC $(INCLUDE) $(include_dir) $(DEFS) -lpthread -lrt -lz -fexceptions -std=c++11 -fvisibility=hidden -Wl,-Bsymbolic -ldl
  55 +CXXFLAGS= -g -O0 -fPIC $(INCLUDE) $(DEFS) -lpthread -lrt -lz -fexceptions -std=c++11 -fvisibility=hidden -Wl,-Bsymbolic -ldl
41 56  
42 57 SRCS:=$(wildcard $(SRC_ROOT)/demo/*.cpp)
43 58 OBJS = $(patsubst %.cpp, %.o, $(notdir $(SRCS)))
44 59  
45   -OBJ_ROOT = /home/huchunming/FFNvDecoder/src/build
46   -DVPP_SRCS:=$(wildcard $(OBJ_ROOT)/dvpp/obj/*.o)
  60 +OBJ_ROOT = $(PROJECT_ROOT)/src/build
  61 +# DVPP_SRCS:=$(wildcard $(OBJ_ROOT)/dvpp/obj/*.o)
47 62 INTEFACE_SRCS:=$(wildcard $(OBJ_ROOT)/interface/obj/*.o)
  63 +NVDECODER_SRCS:=$(wildcard $(OBJ_ROOT)/nvdecoder/obj/*.o)
  64 +GB28181_SRCS:=$(wildcard $(OBJ_ROOT)/gb28181/obj/*.o)
48 65  
49 66  
50   -$(TARGET):$(OBJS) $(INTEFACE_SRCS) $(DVPP_SRCS)
  67 +$(TARGET):$(OBJS) $(INTEFACE_SRCS) $(NVDECODER_SRCS) $(GB28181_SRCS)
51 68 rm -f $(TARGET)
52   - @echo -e "\e[33m""Building object $@""\e[0m"
53   - $(XX) -o $@ $^ $(CXXFLAGS) $(LIBS) $(lib_dir) $(lib) -Wwrite-strings
  69 +# @echo -e "\e[33m""Building object $@""\e[0m"
  70 +# $(XX) -o $@ $^ $(CXXFLAGS) $(LIBS) $(lib_dir) $(lib) -Wwrite-strings
  71 + $(XX) -o $@ $^ $(CXXFLAGS) $(LIBSPATH) -Wwrite-strings
54 72 rm -f *.o
55 73  
56 74 %.o:$(SRC_ROOT)/demo/%.cpp
57   - $(XX) $(CFLAGS) -c $<
  75 + $(XX) $(CXXFLAGS) -c $<
58 76  
59 77  
60 78 clean:
... ...
src/demo/Makefile.BK0308 deleted
1   -XX = g++
2   -
3   -
4   -PROJECT_ROOT= /home/huchunming/FFNvDecoder
5   -
6   -DEPEND_DIR = $(PROJECT_ROOT)/bin
7   -SRC_ROOT = $(PROJECT_ROOT)/src
8   -THIRDPARTY_ROOT = $(PROJECT_ROOT)/3rdparty
9   -
10   -
11   -TARGET= /home/huchunming/FFNvDecoder/src/build/bin/test
12   -
13   -
14   -SPDLOG_ROOT = $(THIRDPARTY_ROOT)/spdlog-1.9.2/release
15   -JRTP_ROOT = $(THIRDPARTY_ROOT)/jrtp_export
16   -
17   -
18   -INCLUDE= -I $(SRC_ROOT)/interface \
19   -
20   -LIBSPATH= -L /home/huchunming/FFNvDecoder/src/build/interface/lib -l:interface.a \
21   - -L /home/huchunming/FFNvDecoder/src/build/dvpp/lib -l:libdvpp.a \
22   -
23   -
24   -LIBS= -lavformat -lavcodec -lswscale -lavutil -lavfilter -lswresample -lavdevice
25   -
26   -CFLAGS= -g -fPIC -O0 $(INCLUDE) -pthread -lrt -lz -std=c++11 -fvisibility=hidden -Wl,-Bsymbolic -ldl
27   - # -DUNICODE -D_UNICODE
28   -
29   -SRCS:=$(wildcard $(SRC_ROOT)/demo/*.cpp)
30   -OBJS = $(patsubst %.cpp, %.o, $(notdir $(SRCS)))
31   -
32   -
33   -$(TARGET):$(OBJS) $(CU_OBJS)
34   - rm -f $(TARGET)
35   - $(XX) -o $@ $^ $(CFLAGS) $(LIBSPATH) $(LIBS) -Wwrite-strings
36   - rm -f *.o
37   -
38   -%.o:$(SRC_ROOT)/demo/%.cpp
39   - $(XX) $(CFLAGS) -c $<
40   -
41   -
42   -clean:
43   - rm -f *.o $(TARGET)
44 0 \ No newline at end of file
src/demo/Makefile.dvpp 0 → 100644
  1 +XX = g++
  2 +
  3 +
  4 +PROJECT_ROOT= /mnt/data/cmhu/FFNvDecoder
  5 +
  6 +SRC_ROOT = $(PROJECT_ROOT)/src
  7 +
  8 +TARGET= $(PROJECT_ROOT)/src/build/bin/demo
  9 +
  10 +DEFS = -DENABLE_DVPP_INTERFACE
  11 +
  12 +
  13 +include_dir=-I/usr/local/Ascend/ascend-toolkit/6.3.RC1.alpha001/runtime/include
  14 +
  15 +lib_dir=-L/usr/lib \
  16 + -L/usr/local/lib \
  17 + -L/usr/local/Ascend/driver/lib64 \
  18 + -L/usr/local/Ascend/ascend-toolkit/6.3.RC1.alpha001/atc/lib64\
  19 + -L/usr/local/Ascend/ascend-toolkit/6.3.RC1.alpha001/runtime/lib64 \
  20 + -L/usr/local/Ascend/ascend-toolkit/6.3.RC1.alpha001/runtime/lib64/stub \
  21 + -L/usr/local/Ascend/ascend-toolkit/6.3.RC1.alpha001/lib64 \
  22 + -L/usr/local/Ascend/driver/lib64/driver
  23 +
  24 +lib=-lacl_dvpp -lascendcl -lmmpa -lglog -lgflags -lpthread -lz -lacl_dvpp_mpi -lruntime -lascendalog -lc_sec -lmsprofiler -lgert -lge_executor -lge_common \
  25 + -lgraph -lascend_protobuf -lprofapi -lerror_manager -lexe_graph -lregister -lplatform
  26 +LIBS= -lavformat -lavcodec -lswscale -lavutil -lavfilter -lswresample -lavdevice
  27 +CXXFLAGS= -g -O0 -fPIC $(INCLUDE) $(include_dir) $(DEFS) -lpthread -lrt -lz -fexceptions -std=c++11 -fvisibility=hidden -Wl,-Bsymbolic -ldl
  28 +
  29 +SRCS:=$(wildcard $(SRC_ROOT)/demo/*.cpp)
  30 +OBJS = $(patsubst %.cpp, %.o, $(notdir $(SRCS)))
  31 +
  32 +OBJ_ROOT = $(PROJECT_ROOT)/src/build
  33 +DVPP_SRCS:=$(wildcard $(OBJ_ROOT)/dvpp/obj/*.o)
  34 +INTEFACE_SRCS:=$(wildcard $(OBJ_ROOT)/interface/obj/*.o)
  35 +
  36 +
  37 +$(TARGET):$(OBJS) $(INTEFACE_SRCS) $(NVDECODER_SRCS) $(GB28181_SRCS)
  38 + rm -f $(TARGET)
  39 + @echo -e "\e[33m""Building object $@""\e[0m"
  40 + $(XX) -o $@ $^ $(CXXFLAGS) $(LIBS) $(lib_dir) $(lib) -Wwrite-strings
  41 + rm -f *.o
  42 +
  43 +%.o:$(SRC_ROOT)/demo/%.cpp
  44 + $(XX) $(CXXFLAGS) -c $<
  45 +
  46 +
  47 +clean:
  48 + rm -f *.o $(TARGET)
0 49 \ No newline at end of file
... ...
src/demo/main_dvpp.cpp renamed to src/demo/main_dvpp.cpp1
... ... @@ -276,17 +276,6 @@ int main(int argc, char* argv[]){
276 276 FFNvDecoderManager* pDecManager = FFNvDecoderManager::getInstance();
277 277 int i = 0;
278 278  
279   - createDvppDecoder(i, gpuid, 0);
280   - i++;
281   - createDvppDecoder(i, gpuid, 0);
282   - i++;
283   - createDvppDecoder(i, gpuid, 0);
284   - i++;
285   -
286   - for(;i<30;i++){
287   - createDvppDecoder(i, gpuid, 0);
288   - }
289   -
290 279 while (true)
291 280 {
292 281 int ch = getchar();
... ...
src/demo/main_nvdec.cpp 0 → 100644
  1 +#include "FFNvDecoderManager.h"
  2 +#include <iostream>
  3 +
  4 +#include "cuda_kernels.h"
  5 +
  6 +#include "NvJpegEncoder.h"
  7 +
  8 +#include <pthread.h>
  9 +#include <thread>
  10 +
  11 +#include <chrono>
  12 +
  13 +#include <unistd.h>
  14 +
  15 +
  16 +#ifdef _WIN32
  17 +#include "Winsock2.h"
  18 +#pragma comment(lib, "ws2_32.lib")
  19 +#endif
  20 +
  21 +#ifdef __linux__
  22 +#include "arpa/inet.h"
  23 +#endif
  24 +
  25 +#include "utiltools.hpp"
  26 +
  27 +#define MIN_RTP_PORT 10000
  28 +#define MAX_RTP_PORT 60000
  29 +
  30 +// ȡ MIN_RTP_PORT(10000)~MAX_RTP_PORT(60000)֮�������˿�(ż���������������˿ڿ���)
  31 +int allocRtpPort() {
  32 +
  33 + static int s_rtpPort = MIN_RTP_PORT;
  34 + if (MIN_RTP_PORT == s_rtpPort)
  35 + {
  36 + srand((unsigned int)time(NULL));
  37 + s_rtpPort = MIN_RTP_PORT + (rand() % MIN_RTP_PORT);
  38 + }
  39 +
  40 + if (s_rtpPort % 2)
  41 + ++s_rtpPort;
  42 +
  43 + while (true)
  44 + {
  45 + s_rtpPort += 2;
  46 + s_rtpPort = s_rtpPort >= MAX_RTP_PORT ? MIN_RTP_PORT : s_rtpPort;
  47 +
  48 + int i = 0;
  49 + for (; i < 2; i++)
  50 + {
  51 + sockaddr_in sRecvAddr;
  52 + int s = socket(AF_INET, SOCK_DGRAM, 0);
  53 +
  54 + sRecvAddr.sin_family = AF_INET;
  55 + sRecvAddr.sin_addr.s_addr = htonl(INADDR_ANY);
  56 + sRecvAddr.sin_port = htons(s_rtpPort + i);
  57 +
  58 + int nResult = bind(s, (sockaddr *)&sRecvAddr, sizeof(sRecvAddr));
  59 + if (nResult != 0)
  60 + {
  61 + break;
  62 + }
  63 +
  64 + nResult = close(s);
  65 + if (nResult != 0)
  66 + {
  67 + printf("closesocket failed:%d\n", nResult);
  68 + break;
  69 + }
  70 + }
  71 +
  72 + if (i == 2)
  73 + break;
  74 + }
  75 +
  76 + return s_rtpPort;
  77 +}
  78 +
  79 +
  80 +
  81 +
  82 +
  83 +unsigned char *pHwRgb[2] = {nullptr, nullptr};
  84 +
  85 +int sum1 = 0;
  86 +int sum2 = 0;
  87 +
  88 +cudaStream_t stream[2];
  89 +
  90 +string data_home = "/mnt/data/cmhu/tmp/";
  91 +
  92 +
  93 +#define checkCudaErrors(S) do {CUresult status; \
  94 + status = S; \
  95 + if (status != CUDA_SUCCESS ) std::cout << __LINE__ <<" checkCudaErrors - status = " << status << std::endl; \
  96 + } while (false)
  97 +
  98 +
  99 +static void gpu_helper(int gpuid)
  100 +{
  101 + cudaSetDevice(gpuid);
  102 +
  103 + // int *dn;
  104 + // cudaMalloc((void **)&dn, 1 * sizeof(int));
  105 +
  106 + size_t free_byte;
  107 + size_t total_byte;
  108 +
  109 + CUresult cuda_status = cuMemGetInfo(&free_byte, &total_byte);
  110 +
  111 + const char *pStr = nullptr;
  112 + if (CUDA_SUCCESS != cuda_status) {
  113 + cuGetErrorString(cuda_status, &pStr);
  114 + printf("Error: cudaMemGetInfo fails, %s \n", pStr);
  115 + return;
  116 + }
  117 +
  118 + double free_db = (double)free_byte;
  119 + double total_db = (double)total_byte;
  120 + double used_db_1 = (total_db - free_db) / 1024.0 / 1024.0;
  121 +
  122 + std::cout <<"显存已使用 " << used_db_1 << " MB\n";
  123 +
  124 + // cudaFree(dn);
  125 +}
  126 +
  127 +int CheckCUDAProperty( int devId )
  128 +{
  129 + cuInit(0);
  130 +
  131 + CUdevice dev = devId;
  132 + size_t memSize = 0;
  133 + char devName[256] = {0};
  134 + int major = 0, minor = 0;
  135 + CUresult rlt = CUDA_SUCCESS;
  136 +
  137 + rlt = cuDeviceGetAttribute(&major, CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MAJOR, dev);
  138 + checkCudaErrors( rlt );
  139 +
  140 + rlt = cuDeviceGetAttribute(&minor, CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MINOR, dev);
  141 + checkCudaErrors( rlt );
  142 +
  143 + rlt = cuDeviceGetName( devName, sizeof( devName ), dev );
  144 + checkCudaErrors( rlt );
  145 +
  146 + printf( "Using GPU Device %d: %s has SM %d.%d compute capability\n",
  147 + dev, devName, major, minor );
  148 +
  149 + rlt = cuDeviceTotalMem( &memSize, dev );
  150 + checkCudaErrors( rlt );
  151 +
  152 + printf( "Total amount of global memory: %4.4f MB\n",
  153 + (float)memSize / ( 1024 * 1024 ) );
  154 +
  155 + return 0;
  156 +}
  157 +
  158 +/**
  159 + * 注意: gpuFrame 在解码器设置的显卡上,后续操作要十分注意这一点,尤其是多线程情况
  160 + * */
  161 +static long lastpts = 0;
  162 +void postDecoded(const void * userPtr, DeviceRgbMemory * gpuFrame){
  163 + AbstractDecoder* decoder = (AbstractDecoder*)userPtr;
  164 + if (decoder!= nullptr)
  165 + {
  166 + long curpts = UtilTools::get_cur_time_ms();
  167 + cout << decoder->getName() << " " << gpuFrame->getWidth() << "x" << gpuFrame->getHeight() << " " << curpts - lastpts << endl;
  168 + lastpts = curpts;
  169 + delete gpuFrame;
  170 + gpuFrame = nullptr;
  171 +
  172 + // const char* gpu_pixfmt = av_get_pix_fmt_name((AVPixelFormat)gpuFrame->format);
  173 + // cout << "pixfmt: " << gpu_pixfmt << endl;
  174 + // cout << "keyframe: " << gpuFrame->key_frame << " width: " << gpuFrame->width << " height: "<< gpuFrame->height << endl;
  175 + // cout << "decode successed ✿✿ヽ(°▽°)ノ✿ " << endl;
  176 +
  177 + // int sum = sum1;
  178 + // if (decoder->getName() == "dec0")
  179 + // {
  180 + // sum1 ++ ;
  181 + // sum = sum1;
  182 +
  183 + // if (gpuFrame->format == AV_PIX_FMT_CUDA)
  184 + // {
  185 + // // cout << "gpuid = " << atoi(decoder->m_cfg.gpuid.c_str()) << endl;
  186 + // cudaSetDevice(atoi(decoder->m_cfg.gpuid.c_str()));
  187 + // cudaError_t cudaStatus;
  188 + // if(pHwRgb[0] == nullptr){
  189 + // // cudaStreamCreate(&stream[0]);
  190 + // cuda_common::setColorSpace( ITU_709, 0 );
  191 + // cudaStatus = cudaMalloc((void **)&pHwRgb[0], 3 * gpuFrame->width * gpuFrame->height * sizeof(unsigned char));
  192 + // }
  193 + // cudaStatus = cuda_common::CUDAToBGR((CUdeviceptr)gpuFrame->data[0],(CUdeviceptr)gpuFrame->data[1], gpuFrame->linesize[0], gpuFrame->linesize[1], pHwRgb[0], gpuFrame->width, gpuFrame->height);
  194 + // cudaDeviceSynchronize();
  195 + // if (cudaStatus != cudaSuccess) {
  196 + // cout << "CUDAToBGR failed !!!" << endl;
  197 + // return;
  198 + // }
  199 +
  200 + // string path = data_home + decoder->getName() + ".jpg";
  201 + // saveJpeg(path.c_str(), pHwRgb[0], gpuFrame->width, gpuFrame->height, stream[0]); // 验证 CUDAToRGB
  202 + // }
  203 + // } else if (decoder->getName() == "dec2")
  204 + // {
  205 + // sum2 ++ ;
  206 + // sum = sum2;
  207 +
  208 + // if (gpuFrame->format == AV_PIX_FMT_CUDA)
  209 + // {
  210 + // // cout << "gpuid = " << atoi(decoder->m_cfg.gpuid.c_str()) << endl;
  211 + // cudaSetDevice(atoi(decoder->m_cfg.gpuid.c_str()));
  212 + // cudaError_t cudaStatus;
  213 + // if(pHwRgb[1] == nullptr){
  214 + // // cudaStreamCreate(&stream[1]);
  215 + // cuda_common::setColorSpace( ITU_709, 0 );
  216 + // cudaStatus = cudaMalloc((void **)&pHwRgb[1], 3 * gpuFrame->width * gpuFrame->height * sizeof(unsigned char));
  217 + // }
  218 + // cudaStatus = cuda_common::CUDAToBGR((CUdeviceptr)gpuFrame->data[0],(CUdeviceptr)gpuFrame->data[1], gpuFrame->linesize[0], gpuFrame->linesize[1], pHwRgb[1], gpuFrame->width, gpuFrame->height);
  219 + // cudaDeviceSynchronize();
  220 + // if (cudaStatus != cudaSuccess) {
  221 + // cout << "CUDAToBGR failed !!!" << endl;
  222 + // return;
  223 + // }
  224 +
  225 + // string path = data_home + decoder->getName() + ".jpg";
  226 + // saveJpeg(path.c_str(), pHwRgb[1], gpuFrame->width, gpuFrame->height, stream[1]); // 验证 CUDAToRGB
  227 + // }
  228 + // }
  229 + }
  230 +}
  231 +
  232 +long start_time = 0;
  233 +long end_time = 0;
  234 +bool count_flag = false;
  235 +int count = 0;
  236 +int count_std = 100;
  237 +
  238 +
  239 +static int sum = 0;
  240 +unsigned char *pHwData = nullptr;
  241 +
  242 +void postDecoded0(const void * userPtr, DeviceRgbMemory* gpuFrame){
  243 + // std::this_thread::sleep_for(std::chrono::milliseconds(30000));
  244 +
  245 + AbstractDecoder* decoder = (AbstractDecoder*)userPtr;
  246 + if (decoder!= nullptr)
  247 + {
  248 + // cout << "decode name: " << decoder->getName() << endl;
  249 + // if (decoder->getName() == "dec")
  250 + // {
  251 + // if (! count_flag)
  252 + // {
  253 + // count_flag = true;
  254 + // count = 0;
  255 + // end_time = start_time = UtilTools::get_cur_time_ms();
  256 + // }
  257 + // count++;
  258 + // sum ++ ;
  259 + // if (count >= count_std)
  260 + // {
  261 + // // end_time = UtilTools::get_cur_time_ms();
  262 + // // long time_using = end_time - start_time;
  263 + // // double time_per_frame = double(time_using)/count_std ;
  264 + // // cout << count_std << "帧用时:" << time_using << "ms 每帧用时:" << time_per_frame << "ms" << endl;
  265 + // cout << decoder->getName() << " keyframe: " << gpuFrame->key_frame << " width: " << gpuFrame->width << " height: "<< gpuFrame->height << endl;
  266 + // // cout << gpuFrame->pts << endl;
  267 +
  268 + // count_flag = false;
  269 + // }
  270 + // // cout << "帧数:" << sum << endl;
  271 +
  272 + // if (gpuFrame->format == AV_PIX_FMT_CUDA)
  273 + // {
  274 + // cudaSetDevice(atoi(decoder->m_cfg.gpuid.c_str()));
  275 + // // cout << "gpu id : " << decoder->m_cfg.gpuid.c_str() << endl;
  276 + // cudaError_t cudaStatus;
  277 + // if(pHwData == nullptr){
  278 + // cuda_common::setColorSpace( ITU_709, 0 );
  279 + // cudaStatus = cudaMalloc((void **)&pHwData, 3 * gpuFrame->width * gpuFrame->height * sizeof(unsigned char));
  280 + // }
  281 + // cudaStatus = cuda_common::CUDAToBGR((CUdeviceptr)gpuFrame->data[0],(CUdeviceptr)gpuFrame->data[1], gpuFrame->linesize[0], gpuFrame->linesize[1], pHwData, gpuFrame->width, gpuFrame->height);
  282 + // cudaDeviceSynchronize();
  283 + // if (cudaStatus != cudaSuccess) {
  284 + // cout << "CUDAToBGR failed !!!" << endl;
  285 + // return;
  286 + // }
  287 +
  288 + // string path = data_home + decoder->getName() + ".jpg";
  289 + // saveJpeg(path.c_str(), pHwData, gpuFrame->width, gpuFrame->height, nullptr); // 验证 CUDAToRGB
  290 + // }
  291 + // }
  292 + }
  293 +}
  294 +
  295 +void decode_finished_cbk(const void* userPtr){
  296 + cout << "当前时间戳: " << UtilTools::get_cur_time_ms() << endl;
  297 +}
  298 +
  299 +bool decode_request_stream_cbk(const char* deviceId){
  300 + cout << "需在此请求流" << endl;
  301 + return true;
  302 +}
  303 +
  304 +// string test_uri = "rtmp://192.168.10.56:1935/objecteye/1";
  305 +// string test_uri = "/home/cmhu/data/output_800x480.mp4";
  306 +// string test_uri = "/home/cmhu/data/output_1920x1080.mp4";
  307 +// string test_uri = "rtsp://176.10.0.2:8554/stream";
  308 +// string test_uri = "/mnt/f/fiss/test_data/h265.mp4";
  309 +string test_uri = "rtsp://176.10.0.4:8554/stream";
  310 +
  311 +void createDecode(int index, const char* gpu_id){
  312 + FFNvDecoderManager* pDecManager = FFNvDecoderManager::getInstance();
  313 + MgrDecConfig config;
  314 + config.name = "dec" + to_string(index);
  315 + config.cfg.uri = test_uri;
  316 + config.cfg.post_decoded_cbk = postDecoded;
  317 + config.cfg.decode_finished_cbk = decode_finished_cbk;
  318 + config.cfg.force_tcp = true;
  319 + config.dec_type = DECODER_TYPE_FFMPEG;
  320 +
  321 + config.cfg.gpuid = gpu_id;
  322 + // if (index % 2 == 0)
  323 + // {
  324 + // config.cfg.gpuid = "0";
  325 + // }
  326 + // else
  327 + // {
  328 + // config.cfg.gpuid = "0";
  329 + // }
  330 +
  331 + AbstractDecoder* decoder = pDecManager->createDecoder(config);
  332 + if (!decoder)
  333 + {
  334 + return ;
  335 + }
  336 + pDecManager->setPostDecArg(config.name, decoder);
  337 + pDecManager->setFinishedDecArg(config.name, decoder);
  338 + pDecManager->startDecodeByName(config.name);
  339 +}
  340 +
  341 +void createGB28181Decode(int index, char* gpu_id, int port){
  342 + FFNvDecoderManager* pDecManager = FFNvDecoderManager::getInstance();
  343 + MgrDecConfig config;
  344 + config.name = "dec" + to_string(index);
  345 + config.cfg.uri = config.name;
  346 + config.cfg.post_decoded_cbk = postDecoded;
  347 + config.cfg.decode_finished_cbk = decode_finished_cbk;
  348 + config.cfg.request_stream_cbk = decode_request_stream_cbk;
  349 + config.cfg.force_tcp = true;
  350 +
  351 + config.dec_type = DECODER_TYPE_GB28181;
  352 + config.cfg.port = port;//allocRtpPort();
  353 +
  354 + config.cfg.gpuid = gpu_id;
  355 +
  356 + AbstractDecoder* decoder = pDecManager->createDecoder(config);
  357 + if (!decoder)
  358 + {
  359 + return ;
  360 + }
  361 + pDecManager->setPostDecArg(config.name, decoder);
  362 + pDecManager->setFinishedDecArg(config.name, decoder);
  363 + pDecManager->startDecodeByName(config.name);
  364 +}
  365 +
  366 +void logFF(void *, int level, const char *fmt, va_list ap)
  367 +{
  368 + vfprintf(stdout, fmt, ap);
  369 +}
  370 +
  371 +
  372 +int main(int argc, char* argv[]){
  373 +
  374 + test_uri = "rtsp://admin:admin@123456@192.168.60.176:554/cam/realmonitor?channel=1&subtype=0";//argv[1];
  375 + char* gpuid = argv[2];
  376 + int port = atoi(argv[3]);
  377 + cout << test_uri << " gpu_id:" << gpuid << " port:" << port << endl;
  378 +
  379 + // av_log_set_callback(&logFF);
  380 +
  381 + CheckCUDAProperty(atoi(gpuid));
  382 +
  383 + pthread_t m_decode_thread;
  384 + pthread_create(&m_decode_thread,0,
  385 + [](void* arg)
  386 + {
  387 + // cudaSetDevice(atoi(gpuid));
  388 + while (true)
  389 + {
  390 + std::this_thread::sleep_for(std::chrono::minutes(1));
  391 + FFNvDecoderManager* pDecManager = FFNvDecoderManager::getInstance();
  392 + int count = pDecManager->count();
  393 + cout << "当前时间:" << UtilTools::get_cur_time_ms() << " 当前运行路数: " << pDecManager->count() << endl;
  394 + }
  395 +
  396 + return (void*)0;
  397 + }
  398 + ,nullptr);
  399 +
  400 +
  401 + FFNvDecoderManager* pDecManager = FFNvDecoderManager::getInstance();
  402 + int i = 0;
  403 +
  404 + while (true)
  405 + {
  406 + int ch = getchar();
  407 + if (ch == 'q')
  408 + {
  409 + break;
  410 + }
  411 +
  412 + switch (ch)
  413 + {
  414 + case 'f':
  415 + case 'F':
  416 + createDecode(i, gpuid);
  417 + i++;
  418 + break;
  419 + case 'g':
  420 + case 'G':
  421 + createGB28181Decode(i, gpuid, port);
  422 + i++;
  423 + break;
  424 + case 'r':
  425 + case 'R':
  426 + pDecManager->resumeDecoder("dec0");
  427 + break;
  428 + case 'p':
  429 + case 'P':
  430 + pDecManager->pauseDecoder("dec0");
  431 + break;
  432 +
  433 + case 'c':
  434 + case 'C':
  435 + i--;
  436 + pDecManager->closeDecoderByName("dec" + to_string(i));
  437 + break;
  438 +
  439 + case 'i':
  440 + case 'I':
  441 + {
  442 + int w,h;
  443 + pDecManager->getResolution("dec0", w,h);
  444 + printf( "%s : %dx%d\n", "dec0" , w,h );
  445 + }
  446 + break;
  447 +
  448 + default:
  449 + break;
  450 + }
  451 +
  452 + /* code */
  453 + }
  454 +
  455 + cout << "总共帧数:" << sum << endl;
  456 + pDecManager->closeAllDecoder();
  457 +}
0 458 \ No newline at end of file
... ...
src/demo/main_nvdec.cpp1 deleted
1   -// #include "FFNvDecoderManager.h"
2   -// #include <iostream>
3   -
4   -// #include "cuda_kernels.h"
5   -
6   -// #include "NvJpegEncoder.h"
7   -
8   -// #include <pthread.h>
9   -// #include <thread>
10   -
11   -// #include <chrono>
12   -
13   -// #include <unistd.h>
14   -
15   -
16   -// #ifdef _WIN32
17   -// #include "Winsock2.h"
18   -// #pragma comment(lib, "ws2_32.lib")
19   -// #endif
20   -
21   -// #ifdef __linux__
22   -// #include "arpa/inet.h"
23   -// #endif
24   -
25   -// #include "utiltools.hpp"
26   -
27   -// #define MIN_RTP_PORT 10000
28   -// #define MAX_RTP_PORT 60000
29   -
30   -// // ȡ MIN_RTP_PORT(10000)~MAX_RTP_PORT(60000)֮�������˿�(ż���������������˿ڿ���)
31   -// int allocRtpPort() {
32   -
33   -// static int s_rtpPort = MIN_RTP_PORT;
34   -// if (MIN_RTP_PORT == s_rtpPort)
35   -// {
36   -// srand((unsigned int)time(NULL));
37   -// s_rtpPort = MIN_RTP_PORT + (rand() % MIN_RTP_PORT);
38   -// }
39   -
40   -// if (s_rtpPort % 2)
41   -// ++s_rtpPort;
42   -
43   -// while (true)
44   -// {
45   -// s_rtpPort += 2;
46   -// s_rtpPort = s_rtpPort >= MAX_RTP_PORT ? MIN_RTP_PORT : s_rtpPort;
47   -
48   -// int i = 0;
49   -// for (; i < 2; i++)
50   -// {
51   -// sockaddr_in sRecvAddr;
52   -// int s = socket(AF_INET, SOCK_DGRAM, 0);
53   -
54   -// sRecvAddr.sin_family = AF_INET;
55   -// sRecvAddr.sin_addr.s_addr = htonl(INADDR_ANY);
56   -// sRecvAddr.sin_port = htons(s_rtpPort + i);
57   -
58   -// int nResult = bind(s, (sockaddr *)&sRecvAddr, sizeof(sRecvAddr));
59   -// if (nResult != 0)
60   -// {
61   -// break;
62   -// }
63   -
64   -// nResult = close(s);
65   -// if (nResult != 0)
66   -// {
67   -// printf("closesocket failed:%d\n", nResult);
68   -// break;
69   -// }
70   -// }
71   -
72   -// if (i == 2)
73   -// break;
74   -// }
75   -
76   -// return s_rtpPort;
77   -// }
78   -
79   -
80   -
81   -
82   -
83   -// unsigned char *pHwRgb[2] = {nullptr, nullptr};
84   -
85   -// int sum1 = 0;
86   -// int sum2 = 0;
87   -
88   -// cudaStream_t stream[2];
89   -
90   -// string data_home = "/mnt/data/cmhu/tmp/";
91   -
92   -
93   -// #define checkCudaErrors(S) do {CUresult status; \
94   -// status = S; \
95   -// if (status != CUDA_SUCCESS ) std::cout << __LINE__ <<" checkCudaErrors - status = " << status << std::endl; \
96   -// } while (false)
97   -
98   -
99   -// static void gpu_helper(int gpuid)
100   -// {
101   -// cudaSetDevice(gpuid);
102   -
103   -// // int *dn;
104   -// // cudaMalloc((void **)&dn, 1 * sizeof(int));
105   -
106   -// size_t free_byte;
107   -// size_t total_byte;
108   -
109   -// CUresult cuda_status = cuMemGetInfo(&free_byte, &total_byte);
110   -
111   -// const char *pStr = nullptr;
112   -// if (CUDA_SUCCESS != cuda_status) {
113   -// cuGetErrorString(cuda_status, &pStr);
114   -// printf("Error: cudaMemGetInfo fails, %s \n", pStr);
115   -// return;
116   -// }
117   -
118   -// double free_db = (double)free_byte;
119   -// double total_db = (double)total_byte;
120   -// double used_db_1 = (total_db - free_db) / 1024.0 / 1024.0;
121   -
122   -// std::cout <<"显存已使用 " << used_db_1 << " MB\n";
123   -
124   -// // cudaFree(dn);
125   -// }
126   -
127   -// int CheckCUDAProperty( int devId )
128   -// {
129   -// cuInit(0);
130   -
131   -// CUdevice dev = devId;
132   -// size_t memSize = 0;
133   -// char devName[256] = {0};
134   -// int major = 0, minor = 0;
135   -// CUresult rlt = CUDA_SUCCESS;
136   -
137   -// rlt = cuDeviceGetAttribute(&major, CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MAJOR, dev);
138   -// checkCudaErrors( rlt );
139   -
140   -// rlt = cuDeviceGetAttribute(&minor, CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MINOR, dev);
141   -// checkCudaErrors( rlt );
142   -
143   -// rlt = cuDeviceGetName( devName, sizeof( devName ), dev );
144   -// checkCudaErrors( rlt );
145   -
146   -// printf( "Using GPU Device %d: %s has SM %d.%d compute capability\n",
147   -// dev, devName, major, minor );
148   -
149   -// rlt = cuDeviceTotalMem( &memSize, dev );
150   -// checkCudaErrors( rlt );
151   -
152   -// printf( "Total amount of global memory: %4.4f MB\n",
153   -// (float)memSize / ( 1024 * 1024 ) );
154   -
155   -// return 0;
156   -// }
157   -
158   -// /**
159   -// * 注意: gpuFrame 在解码器设置的显卡上,后续操作要十分注意这一点,尤其是多线程情况
160   -// * */
161   -// void postDecoded(const void * userPtr, AVFrame * gpuFrame){
162   -// AbstractDecoder* decoder = (AbstractDecoder*)userPtr;
163   -// if (decoder!= nullptr)
164   -// {
165   -// // cout << "decode name: " << decoder->getName() << endl;
166   -
167   -// // const char* gpu_pixfmt = av_get_pix_fmt_name((AVPixelFormat)gpuFrame->format);
168   -// // cout << "pixfmt: " << gpu_pixfmt << endl;
169   -// // cout << "keyframe: " << gpuFrame->key_frame << " width: " << gpuFrame->width << " height: "<< gpuFrame->height << endl;
170   -// // cout << "decode successed ✿✿ヽ(°▽°)ノ✿ " << endl;
171   -
172   -// int sum = sum1;
173   -// if (decoder->getName() == "dec0")
174   -// {
175   -// sum1 ++ ;
176   -// sum = sum1;
177   -
178   -// if (gpuFrame->format == AV_PIX_FMT_CUDA)
179   -// {
180   -// // cout << "gpuid = " << atoi(decoder->m_cfg.gpuid.c_str()) << endl;
181   -// cudaSetDevice(atoi(decoder->m_cfg.gpuid.c_str()));
182   -// cudaError_t cudaStatus;
183   -// if(pHwRgb[0] == nullptr){
184   -// // cudaStreamCreate(&stream[0]);
185   -// cuda_common::setColorSpace( ITU_709, 0 );
186   -// cudaStatus = cudaMalloc((void **)&pHwRgb[0], 3 * gpuFrame->width * gpuFrame->height * sizeof(unsigned char));
187   -// }
188   -// cudaStatus = cuda_common::CUDAToBGR((CUdeviceptr)gpuFrame->data[0],(CUdeviceptr)gpuFrame->data[1], gpuFrame->linesize[0], gpuFrame->linesize[1], pHwRgb[0], gpuFrame->width, gpuFrame->height);
189   -// cudaDeviceSynchronize();
190   -// if (cudaStatus != cudaSuccess) {
191   -// cout << "CUDAToBGR failed !!!" << endl;
192   -// return;
193   -// }
194   -
195   -// string path = data_home + decoder->getName() + ".jpg";
196   -// saveJpeg(path.c_str(), pHwRgb[0], gpuFrame->width, gpuFrame->height, stream[0]); // 验证 CUDAToRGB
197   -// }
198   -// } else if (decoder->getName() == "dec2")
199   -// {
200   -// sum2 ++ ;
201   -// sum = sum2;
202   -
203   -// if (gpuFrame->format == AV_PIX_FMT_CUDA)
204   -// {
205   -// // cout << "gpuid = " << atoi(decoder->m_cfg.gpuid.c_str()) << endl;
206   -// cudaSetDevice(atoi(decoder->m_cfg.gpuid.c_str()));
207   -// cudaError_t cudaStatus;
208   -// if(pHwRgb[1] == nullptr){
209   -// // cudaStreamCreate(&stream[1]);
210   -// cuda_common::setColorSpace( ITU_709, 0 );
211   -// cudaStatus = cudaMalloc((void **)&pHwRgb[1], 3 * gpuFrame->width * gpuFrame->height * sizeof(unsigned char));
212   -// }
213   -// cudaStatus = cuda_common::CUDAToBGR((CUdeviceptr)gpuFrame->data[0],(CUdeviceptr)gpuFrame->data[1], gpuFrame->linesize[0], gpuFrame->linesize[1], pHwRgb[1], gpuFrame->width, gpuFrame->height);
214   -// cudaDeviceSynchronize();
215   -// if (cudaStatus != cudaSuccess) {
216   -// cout << "CUDAToBGR failed !!!" << endl;
217   -// return;
218   -// }
219   -
220   -// string path = data_home + decoder->getName() + ".jpg";
221   -// saveJpeg(path.c_str(), pHwRgb[1], gpuFrame->width, gpuFrame->height, stream[1]); // 验证 CUDAToRGB
222   -// }
223   -// }
224   -// }
225   -// }
226   -
227   -// long start_time = 0;
228   -// long end_time = 0;
229   -// bool count_flag = false;
230   -// int count = 0;
231   -// int count_std = 100;
232   -
233   -
234   -// static int sum = 0;
235   -// unsigned char *pHwData = nullptr;
236   -
237   -// void postDecoded0(const void * userPtr, AVFrame * gpuFrame){
238   -// // std::this_thread::sleep_for(std::chrono::milliseconds(30000));
239   -
240   -// AbstractDecoder* decoder = (AbstractDecoder*)userPtr;
241   -// if (decoder!= nullptr)
242   -// {
243   -// // cout << "decode name: " << decoder->getName() << endl;
244   -// if (decoder->getName() == "dec")
245   -// {
246   -// if (! count_flag)
247   -// {
248   -// count_flag = true;
249   -// count = 0;
250   -// end_time = start_time = UtilTools::get_cur_time_ms();
251   -// }
252   -// count++;
253   -// sum ++ ;
254   -// if (count >= count_std)
255   -// {
256   -// // end_time = UtilTools::get_cur_time_ms();
257   -// // long time_using = end_time - start_time;
258   -// // double time_per_frame = double(time_using)/count_std ;
259   -// // cout << count_std << "帧用时:" << time_using << "ms 每帧用时:" << time_per_frame << "ms" << endl;
260   -// cout << decoder->getName() << " keyframe: " << gpuFrame->key_frame << " width: " << gpuFrame->width << " height: "<< gpuFrame->height << endl;
261   -// // cout << gpuFrame->pts << endl;
262   -
263   -// count_flag = false;
264   -// }
265   -// // cout << "帧数:" << sum << endl;
266   -
267   -// if (gpuFrame->format == AV_PIX_FMT_CUDA)
268   -// {
269   -// cudaSetDevice(atoi(decoder->m_cfg.gpuid.c_str()));
270   -// // cout << "gpu id : " << decoder->m_cfg.gpuid.c_str() << endl;
271   -// cudaError_t cudaStatus;
272   -// if(pHwData == nullptr){
273   -// cuda_common::setColorSpace( ITU_709, 0 );
274   -// cudaStatus = cudaMalloc((void **)&pHwData, 3 * gpuFrame->width * gpuFrame->height * sizeof(unsigned char));
275   -// }
276   -// cudaStatus = cuda_common::CUDAToBGR((CUdeviceptr)gpuFrame->data[0],(CUdeviceptr)gpuFrame->data[1], gpuFrame->linesize[0], gpuFrame->linesize[1], pHwData, gpuFrame->width, gpuFrame->height);
277   -// cudaDeviceSynchronize();
278   -// if (cudaStatus != cudaSuccess) {
279   -// cout << "CUDAToBGR failed !!!" << endl;
280   -// return;
281   -// }
282   -
283   -// string path = data_home + decoder->getName() + ".jpg";
284   -// saveJpeg(path.c_str(), pHwData, gpuFrame->width, gpuFrame->height, nullptr); // 验证 CUDAToRGB
285   -// }
286   -// }
287   -// }
288   -// }
289   -
290   -// void decode_finished_cbk(const void* userPtr){
291   -// cout << "当前时间戳: " << UtilTools::get_cur_time_ms() << endl;
292   -// }
293   -
294   -// bool decode_request_stream_cbk(const char* deviceId){
295   -// cout << "需在此请求流" << endl;
296   -// return true;
297   -// }
298   -
299   -// // string test_uri = "rtmp://192.168.10.56:1935/objecteye/1";
300   -// // string test_uri = "/home/cmhu/data/output_800x480.mp4";
301   -// // string test_uri = "/home/cmhu/data/output_1920x1080.mp4";
302   -// // string test_uri = "rtsp://176.10.0.2:8554/stream";
303   -// // string test_uri = "/mnt/f/fiss/test_data/h265.mp4";
304   -// string test_uri = "rtsp://176.10.0.4:8554/stream";
305   -
306   -// void createDecode(int index, const char* gpu_id){
307   -// FFNvDecoderManager* pDecManager = FFNvDecoderManager::getInstance();
308   -// MgrDecConfig config;
309   -// config.name = "dec" + to_string(index);
310   -// config.cfg.uri = test_uri;
311   -// config.cfg.post_decoded_cbk = postDecoded;
312   -// config.cfg.decode_finished_cbk = decode_finished_cbk;
313   -// config.cfg.force_tcp = true;
314   -// config.dec_type = DECODER_TYPE_FFMPEG;
315   -
316   -// config.cfg.gpuid = gpu_id;
317   -// // if (index % 2 == 0)
318   -// // {
319   -// // config.cfg.gpuid = "0";
320   -// // }
321   -// // else
322   -// // {
323   -// // config.cfg.gpuid = "0";
324   -// // }
325   -
326   -// AbstractDecoder* decoder = pDecManager->createDecoder(config);
327   -// if (!decoder)
328   -// {
329   -// return ;
330   -// }
331   -// pDecManager->setPostDecArg(config.name, decoder);
332   -// pDecManager->setFinishedDecArg(config.name, decoder);
333   -// pDecManager->startDecodeByName(config.name);
334   -// }
335   -
336   -// void createGB28181Decode(int index, char* gpu_id, int port){
337   -// FFNvDecoderManager* pDecManager = FFNvDecoderManager::getInstance();
338   -// MgrDecConfig config;
339   -// config.name = "dec" + to_string(index);
340   -// config.cfg.uri = config.name;
341   -// config.cfg.post_decoded_cbk = postDecoded;
342   -// config.cfg.decode_finished_cbk = decode_finished_cbk;
343   -// config.cfg.request_stream_cbk = decode_request_stream_cbk;
344   -// config.cfg.force_tcp = true;
345   -
346   -// config.dec_type = DECODER_TYPE_GB28181;
347   -// config.cfg.port = port;//allocRtpPort();
348   -
349   -// config.cfg.gpuid = gpu_id;
350   -
351   -// AbstractDecoder* decoder = pDecManager->createDecoder(config);
352   -// if (!decoder)
353   -// {
354   -// return ;
355   -// }
356   -// pDecManager->setPostDecArg(config.name, decoder);
357   -// pDecManager->setFinishedDecArg(config.name, decoder);
358   -// pDecManager->startDecodeByName(config.name);
359   -// }
360   -
361   -// void logFF(void *, int level, const char *fmt, va_list ap)
362   -// {
363   -// vfprintf(stdout, fmt, ap);
364   -// }
365   -
366   -
367   -// int main(int argc, char* argv[]){
368   -
369   -// test_uri = "rtsp://admin:admin@123456@192.168.60.176:554/cam/realmonitor?channel=1&subtype=0";//argv[1];
370   -// char* gpuid = argv[2];
371   -// int port = atoi(argv[3]);
372   -// cout << test_uri << " gpu_id:" << gpuid << " port:" << port << endl;
373   -
374   -// // av_log_set_callback(&logFF);
375   -
376   -// CheckCUDAProperty(atoi(gpuid));
377   -
378   -// pthread_t m_decode_thread;
379   -// pthread_create(&m_decode_thread,0,
380   -// [](void* arg)
381   -// {
382   -// // cudaSetDevice(atoi(gpuid));
383   -// while (true)
384   -// {
385   -// std::this_thread::sleep_for(std::chrono::minutes(1));
386   -// FFNvDecoderManager* pDecManager = FFNvDecoderManager::getInstance();
387   -// int count = pDecManager->count();
388   -// cout << "当前时间:" << UtilTools::get_cur_time_ms() << " 当前运行路数: " << pDecManager->count() << endl;
389   -// }
390   -
391   -// return (void*)0;
392   -// }
393   -// ,nullptr);
394   -
395   -
396   -// FFNvDecoderManager* pDecManager = FFNvDecoderManager::getInstance();
397   -// int i = 0;
398   -
399   -// while (true)
400   -// {
401   -// int ch = getchar();
402   -// if (ch == 'q')
403   -// {
404   -// break;
405   -// }
406   -
407   -// switch (ch)
408   -// {
409   -// case 'f':
410   -// case 'F':
411   -// createDecode(i, gpuid);
412   -// i++;
413   -// break;
414   -// case 'g':
415   -// case 'G':
416   -// createGB28181Decode(i, gpuid, port);
417   -// i++;
418   -// break;
419   -// case 'r':
420   -// case 'R':
421   -// pDecManager->resumeDecoder("dec0");
422   -// break;
423   -// case 'p':
424   -// case 'P':
425   -// pDecManager->pauseDecoder("dec0");
426   -// break;
427   -
428   -// case 'c':
429   -// case 'C':
430   -// i--;
431   -// pDecManager->closeDecoderByName("dec" + to_string(i));
432   -// break;
433   -
434   -// case 'i':
435   -// case 'I':
436   -// {
437   -// int w,h;
438   -// pDecManager->getResolution("dec0", w,h);
439   -// printf( "%s : %dx%d\n", "dec0" , w,h );
440   -// }
441   -// break;
442   -
443   -// default:
444   -// break;
445   -// }
446   -
447   -// /* code */
448   -// }
449   -
450   -// cout << "总共帧数:" << sum << endl;
451   -// pDecManager->closeAllDecoder();
452   -// }
453 0 \ No newline at end of file
src/dvpp/DvppDecoderApi.h
... ... @@ -8,7 +8,7 @@ using namespace std;
8 8  
9 9 class DvppDecoder;
10 10  
11   -class DvppDecoderApi : public AbstractDecoder{
  11 +class DvppDecoderApi : public AbstractDecoder {
12 12 public:
13 13 DvppDecoderApi();
14 14 ~DvppDecoderApi();
... ...
src/dvpp/depend_headers.h
... ... @@ -23,10 +23,10 @@
23 23  
24 24 // ffmpeg 是c库 所以编译的时候要加入从 extern导入的C 来声明否则连接失败
25 25 extern "C" {
26   -#include "libavutil/imgutils.h"
27   -#include "libavutil/samplefmt.h"
28   -#include "libavformat/avformat.h"
29   -#include "libavcodec/avcodec.h"
  26 + #include "libavutil/imgutils.h"
  27 + #include "libavutil/samplefmt.h"
  28 + #include "libavformat/avformat.h"
  29 + #include "libavcodec/avcodec.h"
30 30 }
31 31  
32 32  
... ...
src/gb28181/FFGB28181Decoder.cpp
... ... @@ -2,7 +2,7 @@
2 2 #include <iostream>
3 3 #include "FFGB28181Decoder.h"
4 4  
5   -#include "../FFCuContextManager.h"
  5 +#include "../nvdecoder/FFCuContextManager.h"
6 6  
7 7 extern "C" {
8 8 #include "libavutil/avstring.h"
... ... @@ -296,7 +296,7 @@ void FFGB28181Decoder::post_decode_thread(){
296 296 m_queue_mutex.unlock();
297 297 // 跳帧
298 298 if (m_frameSkip == 1 || index % m_frameSkip == 0){
299   - post_decoded_cbk(m_postDecArg, gpuFrame);
  299 + post_decoded_cbk(m_postDecArg, convert2bgr(gpuFrame));
300 300 }
301 301  
302 302 av_frame_free(&gpuFrame);
... ... @@ -320,6 +320,14 @@ void FFGB28181Decoder::stream_end_callback()
320 320 return;
321 321 }
322 322  
  323 +void FFGB28181Decoder::setPostDecArg(const void* postDecArg){
  324 + m_postDecArg = postDecArg;
  325 +}
  326 +
  327 +void FFGB28181Decoder::setFinishedDecArg(const void* finishedDecArg){
  328 + m_finishedDecArg = finishedDecArg;
  329 +}
  330 +
323 331 void FFGB28181Decoder::pause() {
324 332 m_status = EPAUSE;
325 333 LOG_INFO("pause --{}", m_dec_name);
... ... @@ -370,6 +378,36 @@ int FFGB28181Decoder::getCachedQueueLength(){
370 378 return m_rtpPtr->GetPsFrameListSize();
371 379 }
372 380  
  381 +DeviceRgbMemory* FFGB28181Decoder::convert2bgr(AVFrame * gpuFrame){
  382 + if (gpuFrame != nullptr && gpuFrame->format == AV_PIX_FMT_CUDA ){
  383 + LOG_DEBUG("decode task: gpuid: {} width: {} height: {}", m_cfg.gpuid, gpuFrame->width, gpuFrame->height);
  384 + GpuRgbMemory* gpuMem = new GpuRgbMemory(3, gpuFrame->width, gpuFrame->height, getName(), m_cfg.gpuid, false, true);
  385 +
  386 + do{
  387 + if (gpuMem->getMem() == nullptr){
  388 + LOG_ERROR("new GpuRgbMemory failed !!!");
  389 + break;
  390 + }
  391 +
  392 + cudaSetDevice(atoi(m_cfg.gpuid.c_str()));
  393 + cuda_common::setColorSpace( ITU_709, 0 );
  394 + cudaError_t cudaStatus = cuda_common::CUDAToBGR((CUdeviceptr)gpuFrame->data[0],(CUdeviceptr)gpuFrame->data[1], gpuFrame->linesize[0], gpuFrame->linesize[1], gpuMem->getMem(), gpuFrame->width, gpuFrame->height);
  395 + cudaDeviceSynchronize();
  396 + if (cudaStatus != cudaSuccess) {
  397 + LOG_ERROR("CUDAToBGR failed failed !!!");
  398 + break;
  399 + }
  400 +
  401 + return gpuMem;
  402 + }while(0);
  403 +
  404 + delete gpuMem;
  405 + gpuMem = nullptr;
  406 + }
  407 +
  408 + return nullptr;
  409 +}
  410 +
373 411 FFImgInfo* FFGB28181Decoder::snapshot(){
374 412  
375 413 // 锁住停止队列消耗
... ... @@ -401,7 +439,7 @@ FFImgInfo* FFGB28181Decoder::snapshot(){
401 439  
402 440 if (gpuFrame != nullptr && gpuFrame->format == AV_PIX_FMT_CUDA ){
403 441 LOG_DEBUG("decode task: gpuid: {} width: {} height: {}", m_cfg.gpuid, gpuFrame->width, gpuFrame->height);
404   - GpuRgbMemory* gpuMem = new GpuRgbMemory(3, gpuFrame->width, gpuFrame->height, getName(), m_cfg.gpuid , true);
  442 + GpuRgbMemory* gpuMem = new GpuRgbMemory(3, gpuFrame->width, gpuFrame->height, getName(), m_cfg.gpuid , false, true);
405 443  
406 444 if (gpuMem->getMem() == nullptr){
407 445 LOG_ERROR("new GpuRgbMemory failed !!!");
... ...
src/gb28181/FFGB28181Decoder.h
... ... @@ -3,7 +3,8 @@
3 3  
4 4 #include "RTPReceiver.h"
5 5  
6   -#include "../AbstractDecoder.h"
  6 +#include "common_header.h"
  7 +#include "../interface/AbstractDecoder.h"
7 8  
8 9 #include <atomic>
9 10 #include <mutex>
... ... @@ -14,6 +15,7 @@ struct AVCodec;
14 15 struct AVFrame;
15 16 struct AVPacket;
16 17 struct SwsContext;
  18 +struct AVDictionary;
17 19  
18 20 using namespace std;
19 21  
... ... @@ -46,12 +48,29 @@ public:
46 48  
47 49 FFImgInfo* snapshot();
48 50  
  51 + void setName(string nm){
  52 + m_dec_name = nm;
  53 + }
  54 +
  55 + string getName(){
  56 + return m_dec_name;
  57 + }
  58 +
  59 + void setPostDecArg(const void* postDecArg);
  60 + void setFinishedDecArg(const void* finishedDecArg);
  61 +
49 62 public:
50 63 void stream_callback(int videoType, char* data, int len, int isKey, uint64_t pts, uint64_t localPts);
51 64 void stream_end_callback();
52 65 void post_decode_thread();
53 66  
54 67 private:
  68 + DeviceRgbMemory* convert2bgr(AVFrame * gpuFrame);
  69 +
  70 +private:
  71 + string m_dec_name;
  72 + FFDecConfig m_cfg;
  73 +
55 74 AVCodecContext* m_pAVCodecCtx {};
56 75 const AVCodec* m_pAVCodec {};
57 76  
... ... @@ -76,10 +95,17 @@ private:
76 95 AVDictionary *gpu_options = nullptr;
77 96  
78 97 pthread_t m_post_decode_thread;
  98 + const void * m_postDecArg;
  99 + POST_DECODE_CALLBACK post_decoded_cbk; // 解码数据回调接口
  100 +
  101 + const void * m_finishedDecArg;
  102 + DECODE_FINISHED_CALLBACK decode_finished_cbk;
79 103  
80 104 queue<AVFrame*> mFrameQueue;
81 105 mutex m_queue_mutex;
82 106 mutex m_snapshot_mutex;
  107 +
  108 + bool m_dec_keyframe;
83 109 };
84 110  
85 111 #endif // _GB28181_DECODER_H_
... ...
src/gb28181/Makefile
... ... @@ -12,6 +12,34 @@ DEPS:=$(addprefix $(DEP_DIR)/, $(patsubst %.cpp, %.d,a $(SRCS)))
12 12 # 自动生成头文件依赖选项
13 13 DEPFLAGS=-MT $@ -MMD -MP -MF $(DEP_DIR)/$*.d
14 14  
  15 +CUDA_ROOT = /usr/local/cuda-11.1
  16 +
  17 +PROJECT_ROOT= /mnt/data/cmhu/FFNvDecoder
  18 +
  19 +DEPEND_DIR = $(PROJECT_ROOT)/bin
  20 +THIRDPARTY_ROOT = $(PROJECT_ROOT)/3rdparty
  21 +SPDLOG_ROOT = $(THIRDPARTY_ROOT)/spdlog-1.9.2/release
  22 +JRTP_ROOT = $(THIRDPARTY_ROOT)/jrtp_export
  23 +
  24 +
  25 +INCLUDE= -I $(DEPEND_DIR)/include \
  26 + -I $(CUDA_ROOT)/include \
  27 + -I $(TOP_DIR)/common/inc \
  28 + -I $(TOP_DIR)/common/UtilNPP \
  29 + -I $(TOP_DIR)/ \
  30 + -I $(SPDLOG_ROOT)/include \
  31 + -I $(JRTP_ROOT)/jrtplib/include/jrtplib3 \
  32 + -I $(JRTP_ROOT)/jthread/include/jthread
  33 +
  34 +LIBSPATH= -L $(DEPEND_DIR)/lib -lavformat -lavcodec -lswscale -lavutil -lavfilter -lswresample -lavdevice \
  35 + -L $(CUDA_ROOT)/lib64 -lcuda -lcudart -lnvcuvid -lcurand -lcublas -lnvjpeg \
  36 + -L $(SPDLOG_ROOT) -l:libspdlog.a \
  37 + -L $(JRTP_ROOT)/jthread/lib -l:libjthread.a \
  38 + -L $(JRTP_ROOT)/jrtplib/lib -l:libjrtp.a
  39 +
  40 +
  41 +CXXFLAGS= -g -O0 -fPIC $(INCLUDE) $(DEFS) -lpthread -lrt -lz -fexceptions -std=c++11 -fvisibility=hidden -Wl,-Bsymbolic -ldl -Wwrite-strings
  42 +
15 43 # 最终目标文件
16 44 TARGET:=$(LIB_DIR)/$(MODULE).a
17 45  
... ... @@ -22,7 +50,7 @@ all:$(TARGET)
22 50 # 生成最终目标
23 51 $(TARGET):$(OBJS) | $(LIB_DIR)
24 52 @echo -e "\e[32m""Linking static library $(TARGET)""\e[0m"
25   - @ar -rc $@ $^
  53 +# @ar -rc $@ $^
26 54  
27 55 # 若没有lib目录则自动生成
28 56 $(LIB_DIR):
... ... @@ -31,7 +59,7 @@ $(LIB_DIR):
31 59 # 生成中间目标文件
32 60 $(OBJ_DIR)/%.o:$(SRC_DIR)/%.cpp $(DEP_DIR)/%.d | $(OBJ_DIR) $(DEP_DIR)
33 61 @echo -e "\e[33m""Building object $@""\e[0m"
34   - @$(CXX) -c $(DEPFLAGS) $(CXXFLAGS) $(INCS) $(MACROS) -o $@ $<
  62 + @$(CXX) -c $(DEPFLAGS) $(CXXFLAGS) $(INCS) $(LIBSPATH) $(MACROS) -o $@ $<
35 63  
36 64 # 若没有obj目录则自动生成
37 65 $(OBJ_DIR):
... ...
src/interface/FFNvDecoderManager.h
... ... @@ -14,8 +14,8 @@ struct MgrDecConfig
14 14 string name{""}; // 解码器名称
15 15 };
16 16  
17   -// #define USE_NVDEC
18   -#define USE_DVPP
  17 +#define USE_NVDEC
  18 +// #define USE_DVPP
19 19 /**
20 20 * 解码器管理类,单例类
21 21 * 谨防死锁
... ...
src/interface/Makefile
... ... @@ -12,10 +12,41 @@ DEPS:=$(addprefix $(DEP_DIR)/, $(patsubst %.cpp, %.d,a $(SRCS)))
12 12 # 自动生成头文件依赖选项
13 13 DEPFLAGS=-MT $@ -MMD -MP -MF $(DEP_DIR)/$*.d
14 14  
  15 +
  16 +PROJECT_ROOT= /mnt/data/cmhu/FFNvDecoder
  17 +
  18 +DEPEND_DIR = $(PROJECT_ROOT)/bin
  19 +THIRDPARTY_ROOT = $(PROJECT_ROOT)/3rdparty
  20 +SPDLOG_ROOT = $(THIRDPARTY_ROOT)/spdlog-1.9.2/release
  21 +JRTP_ROOT = $(THIRDPARTY_ROOT)/jrtp_export
  22 +
  23 +
  24 +INCLUDE= -I $(DEPEND_DIR)/include \
  25 + -I $(CUDA_ROOT)/include \
  26 + -I $(TOP_DIR)/common/inc \
  27 + -I $(TOP_DIR)/common/UtilNPP \
  28 + -I $(TOP_DIR)/ \
  29 + -I $(SPDLOG_ROOT)/include \
  30 + -I $(JRTP_ROOT)/jrtplib/include/jrtplib3 \
  31 + -I $(JRTP_ROOT)/jthread/include/jthread \
  32 + -I $(TOP_DIR)/src/gb28181 \
  33 + -I $(TOP_DIR)/src/nvdecoder \
  34 +
  35 +LIBSPATH= -L $(DEPEND_DIR)/lib -lavformat -lavcodec -lswscale -lavutil -lavfilter -lswresample -lavdevice \
  36 + -L $(CUDA_ROOT)/lib64 -lcuda -lcudart -lnvcuvid -lcurand -lcublas -lnvjpeg \
  37 + -L $(SPDLOG_ROOT) -l:libspdlog.a \
  38 + -L $(JRTP_ROOT)/jthread/lib -l:libjthread.a \
  39 + -L $(JRTP_ROOT)/jrtplib/lib -l:libjrtp.a
  40 +
  41 +
  42 +CXXFLAGS= -g -O0 -fPIC $(INCLUDE) $(DEFS) -lpthread -lrt -lz -fexceptions -std=c++11 -fvisibility=hidden -Wl,-Bsymbolic -ldl -Wwrite-strings
  43 +
  44 +
15 45 # 最终目标文件
16 46 TARGET:=$(LIB_DIR)/$(MODULE).a
17 47  
18   -MODULE_LIBS:=$(BUILD_DIR)/dvpp/lib/libdvpp.a\
  48 +# MODULE_LIBS:=$(BUILD_DIR)/nvdecoder/lib/nvdecoder.a \
  49 +# $(BUILD_DIR)/gb28181/lib/gb28181.a\
19 50  
20 51 # 默认最终目标
21 52 .PHONY:all
... ... @@ -23,8 +54,8 @@ all:$(TARGET)
23 54  
24 55 # 生成最终目标
25 56 $(TARGET):$(OBJS) | $(LIB_DIR)
26   - @echo -e "\e[32m""Linking static library $(TARGET)""\e[0m"
27   - @ar -rc $@ $^
  57 + # @echo -e "\e[32m""Linking static library $(TARGET)""\e[0m"
  58 +# @ar -rc $@ $^
28 59  
29 60 # 若没有lib目录则自动生成
30 61 $(LIB_DIR):
... ... @@ -33,7 +64,8 @@ $(LIB_DIR):
33 64 # 生成中间目标文件
34 65 $(OBJ_DIR)/%.o:$(SRC_DIR)/%.cpp $(DEP_DIR)/%.d | $(OBJ_DIR) $(DEP_DIR)
35 66 @echo -e "\e[33m""Building object $@""\e[0m"
36   - @$(CXX) -c $(DEPFLAGS) $(CXXFLAGS) $(INCS) $(MACROS) -o $@ $(MODULE_LIBS) $<
  67 +# @$(CXX) -c $(DEPFLAGS) $(CXXFLAGS) $(INCS) $(LIBSPATH) $(MACROS) -o $@ $(MODULE_LIBS) $<
  68 + @$(CXX) -c $(DEPFLAGS) $(CXXFLAGS) $(INCS) $(LIBSPATH) $(MACROS) -o $@ $<
37 69  
38 70 # 若没有obj目录则自动生成
39 71 $(OBJ_DIR):
... ...
src/interface/utiltools.hpp
1 1 #ifndef _UTIL_TOOLS_HPP_
2 2 #define _UTIL_TOOLS_HPP_
3 3  
4   -#include<chrono>
  4 +#include <chrono>
5 5  
6 6 using namespace std;
7 7  
... ...
src/nvdecoder/DrawImageOnGPU.cu
1 1 #include "cuda_kernels.h"
2 2  
3   -#include "logger.hpp"
  3 +#include "../interface/logger.hpp"
4 4  
5 5 typedef unsigned char uchar;
6 6 typedef unsigned int uint32;
... ...
src/nvdecoder/FFCuContextManager.cpp
... ... @@ -4,6 +4,18 @@
4 4  
5 5 using namespace std;
6 6  
  7 +extern "C"
  8 +{
  9 + #include <libavcodec/avcodec.h>
  10 + #include <libavdevice/avdevice.h>
  11 + #include <libavformat/avformat.h>
  12 + #include <libavfilter/avfilter.h>
  13 + #include <libavutil/avutil.h>
  14 + #include <libavutil/pixdesc.h>
  15 + #include <libswscale/swscale.h>
  16 + #include <libavutil/imgutils.h>
  17 +}
  18 +
7 19 FFCuContextManager::~FFCuContextManager()
8 20 {
9 21 for(auto iter = ctxMap.begin(); iter != ctxMap.end(); iter++){
... ...
src/nvdecoder/FFCuContextManager.h
... ... @@ -2,19 +2,10 @@
2 2 #include<map>
3 3 #include<string>
4 4  
5   -extern "C"
6   -{
7   - #include <libavcodec/avcodec.h>
8   - #include <libavdevice/avdevice.h>
9   - #include <libavformat/avformat.h>
10   - #include <libavfilter/avfilter.h>
11   - #include <libavutil/avutil.h>
12   - #include <libavutil/pixdesc.h>
13   - #include <libswscale/swscale.h>
14   -}
15   -
16 5 using namespace std;
17 6  
  7 +struct AVBufferRef;
  8 +
18 9 class FFCuContextManager{
19 10 public:
20 11 static FFCuContextManager* getInstance(){
... ...
src/nvdecoder/FFNvDecoder.cpp
... ... @@ -63,6 +63,7 @@ FFNvDecoder::~FFNvDecoder()
63 63 bool FFNvDecoder::init(FFDecConfig& cfg)
64 64 {
65 65 m_cfg = cfg;
  66 + m_dec_name = cfg.dec_name;
66 67  
67 68 fstream infile(cfg.uri);
68 69 if (infile.is_open()){
... ... @@ -315,7 +316,7 @@ void FFNvDecoder::post_decode_thread(){
315 316 m_queue_mutex.unlock();
316 317 // 跳帧
317 318 if (skip_frame == 1 || index % skip_frame == 0){
318   - post_decoded_cbk(m_postDecArg, gpuFrame);
  319 + post_decoded_cbk(m_postDecArg, convert2bgr(gpuFrame));
319 320 index = 0;
320 321 }
321 322  
... ... @@ -387,6 +388,44 @@ float FFNvDecoder::fps(){
387 388 return m_fps;
388 389 }
389 390  
  391 +void FFNvDecoder::setPostDecArg(const void* postDecArg){
  392 + m_postDecArg = postDecArg;
  393 +}
  394 +
  395 +void FFNvDecoder::setFinishedDecArg(const void* finishedDecArg){
  396 + m_finishedDecArg = finishedDecArg;
  397 +}
  398 +
  399 +DeviceRgbMemory* FFNvDecoder::convert2bgr(AVFrame * gpuFrame){
  400 + if (gpuFrame != nullptr && gpuFrame->format == AV_PIX_FMT_CUDA ){
  401 + LOG_DEBUG("decode task: gpuid: {} width: {} height: {}", m_cfg.gpuid, gpuFrame->width, gpuFrame->height);
  402 + GpuRgbMemory* gpuMem = new GpuRgbMemory(3, gpuFrame->width, gpuFrame->height, getName(), m_cfg.gpuid, false, true);
  403 +
  404 + do{
  405 + if (gpuMem->getMem() == nullptr){
  406 + LOG_ERROR("new GpuRgbMemory failed !!!");
  407 + break;
  408 + }
  409 +
  410 + cudaSetDevice(atoi(m_cfg.gpuid.c_str()));
  411 + cuda_common::setColorSpace( ITU_709, 0 );
  412 + cudaError_t cudaStatus = cuda_common::CUDAToBGR((CUdeviceptr)gpuFrame->data[0],(CUdeviceptr)gpuFrame->data[1], gpuFrame->linesize[0], gpuFrame->linesize[1], gpuMem->getMem(), gpuFrame->width, gpuFrame->height);
  413 + cudaDeviceSynchronize();
  414 + if (cudaStatus != cudaSuccess) {
  415 + LOG_ERROR("CUDAToBGR failed failed !!!");
  416 + break;
  417 + }
  418 +
  419 + return gpuMem;
  420 + }while(0);
  421 +
  422 + delete gpuMem;
  423 + gpuMem = nullptr;
  424 + }
  425 +
  426 + return nullptr;
  427 +}
  428 +
390 429 FFImgInfo* FFNvDecoder::snapshot(){
391 430  
392 431 // 锁住停止队列消耗
... ... @@ -418,7 +457,7 @@ FFImgInfo* FFNvDecoder::snapshot(){
418 457  
419 458 if (gpuFrame != nullptr && gpuFrame->format == AV_PIX_FMT_CUDA ){
420 459 LOG_DEBUG("decode task: gpuid: {} width: {} height: {}", m_cfg.gpuid, gpuFrame->width, gpuFrame->height);
421   - GpuRgbMemory* gpuMem = new GpuRgbMemory(3, gpuFrame->width, gpuFrame->height, getName(), m_cfg.gpuid , true);
  460 + GpuRgbMemory* gpuMem = new GpuRgbMemory(3, gpuFrame->width, gpuFrame->height, getName(), m_cfg.gpuid, false, true);
422 461  
423 462 if (gpuMem->getMem() == nullptr){
424 463 LOG_ERROR("new GpuRgbMemory failed !!!");
... ...
src/nvdecoder/FFNvDecoder.h
1 1 #include<string>
2 2 #include <pthread.h>
3 3  
4   -#include "../AbstractDecoder.h"
5   -
6 4 #include <mutex>
7 5  
  6 +extern "C"
  7 +{
  8 + #include <libavcodec/avcodec.h>
  9 + #include <libavdevice/avdevice.h>
  10 + #include <libavformat/avformat.h>
  11 + #include <libavfilter/avfilter.h>
  12 + #include <libavutil/avutil.h>
  13 + #include <libavutil/pixdesc.h>
  14 + #include <libswscale/swscale.h>
  15 + #include <libavutil/imgutils.h>
  16 +}
  17 +
  18 +#include "common_header.h"
  19 +
  20 +#include "../interface/AbstractDecoder.h"
  21 +
8 22 using namespace std;
9 23  
10   -class FFNvDecoder : public AbstractDecoder{
  24 +class FFNvDecoder : public AbstractDecoder {
11 25 public:
12 26 FFNvDecoder();
13 27 ~FFNvDecoder();
... ... @@ -34,6 +48,17 @@ public:
34 48  
35 49 FFImgInfo* snapshot();
36 50  
  51 + void setName(string nm){
  52 + m_dec_name = nm;
  53 + }
  54 +
  55 + string getName(){
  56 + return m_dec_name;
  57 + }
  58 +
  59 + void setPostDecArg(const void* postDecArg);
  60 + void setFinishedDecArg(const void* finishedDecArg);
  61 +
37 62 public:
38 63 AVPixelFormat getHwPixFmt();
39 64  
... ... @@ -43,7 +68,12 @@ private:
43 68 bool init(const char* uri, const char* gpuid, bool force_tcp);
44 69 void decode_finished();
45 70  
  71 + DeviceRgbMemory* convert2bgr(AVFrame * gpuFrame);
  72 +
46 73 private:
  74 + string m_dec_name;
  75 + FFDecConfig m_cfg;
  76 +
47 77 AVStream* stream;
48 78 AVCodecContext *avctx;
49 79 int stream_index;
... ... @@ -65,4 +95,13 @@ private:
65 95 queue<AVFrame*> mFrameQueue;
66 96 mutex m_queue_mutex;
67 97 mutex m_snapshot_mutex;
  98 + long m_index{0};
  99 +
  100 + bool m_dec_keyframe;
  101 +
  102 + const void * m_postDecArg;
  103 + POST_DECODE_CALLBACK post_decoded_cbk; // 解码数据回调接口
  104 +
  105 + const void * m_finishedDecArg;
  106 + DECODE_FINISHED_CALLBACK decode_finished_cbk;
68 107 };
69 108 \ No newline at end of file
... ...
src/nvdecoder/GpuRgbMemory.hpp
1 1 #include<string>
2 2  
3   -#include "../DeviceRgbMemory.hpp"
  3 +#include "../interface/DeviceRgbMemory.hpp"
4 4 #include "cuda_kernels.h"
5 5 #include "define.hpp"
6   -#include "utiltools.hpp"
  6 +#include "common_header.h"
7 7  
8 8 using namespace std;
9 9  
10 10 class GpuRgbMemory : public DeviceRgbMemory{
11 11  
12 12 public:
13   - GpuRgbMemory(int _channel, int _width, int _height, string _id, string _gpuid, bool _isused)
14   - :DeviceRgbMemory(_channel, _width, _height, _id, _gpuid, _isused){
  13 + GpuRgbMemory(int _channel, int _width, int _height, string _id, string _gpuid, bool _key_frame, bool _isused)
  14 + :DeviceRgbMemory(_channel, _width, _height, _id, _gpuid, _key_frame, _isused){
15 15 gpuid = _gpuid;
16 16 cudaSetDevice(atoi(gpuid.c_str()));
17 17 CHECK_CUDA(cudaMalloc((void **)&pHwRgb, data_size * sizeof(unsigned char)));
... ...
src/nvdecoder/Makefile 0 → 100644
  1 +# 各项目录
  2 +LIB_DIR:=$(BUILD_DIR)/$(MODULE)/lib
  3 +DEP_DIR:=$(BUILD_DIR)/$(MODULE)/.dep
  4 +OBJ_DIR:=$(BUILD_DIR)/$(MODULE)/obj
  5 +SRC_DIR:=$(TOP_DIR)/$(MODULE)
  6 +
  7 +# 源文件以及中间目标文件和依赖文件
  8 +SRCS:=$(notdir $(wildcard $(SRC_DIR)/*.cpp))
  9 +OBJS:=$(addprefix $(OBJ_DIR)/, $(patsubst %.cpp, %.o, $(SRCS)))
  10 +DEPS:=$(addprefix $(DEP_DIR)/, $(patsubst %.cpp, %.d,a $(SRCS)))
  11 +
  12 +CUDA_ROOT = /usr/local/cuda-11.1
  13 +NVCC = $(CUDA_ROOT)/bin/nvcc
  14 +
  15 +# 自动生成头文件依赖选项
  16 +DEPFLAGS=-MT $@ -MMD -MP -MF $(DEP_DIR)/$*.d
  17 +
  18 +DEFS = -DENABLE_DVPP_INTERFACE
  19 +
  20 +# 最终目标文件
  21 +TARGET:=$(LIB_DIR)/$(MODULE).a
  22 +
  23 +
  24 +PROJECT_ROOT= /mnt/data/cmhu/FFNvDecoder
  25 +
  26 +DEPEND_DIR = $(PROJECT_ROOT)/bin
  27 +THIRDPARTY_ROOT = $(PROJECT_ROOT)/3rdparty
  28 +SPDLOG_ROOT = $(THIRDPARTY_ROOT)/spdlog-1.9.2/release
  29 +JRTP_ROOT = $(THIRDPARTY_ROOT)/jrtp_export
  30 +
  31 +
  32 +INCLUDE= -I $(DEPEND_DIR)/include \
  33 + -I $(CUDA_ROOT)/include \
  34 + -I $(TOP_DIR)/common/inc \
  35 + -I $(TOP_DIR)/common/UtilNPP \
  36 + -I $(TOP_DIR)/ \
  37 + -I $(SPDLOG_ROOT)/include \
  38 + -I $(JRTP_ROOT)/jrtplib/include/jrtplib3 \
  39 + -I $(JRTP_ROOT)/jthread/include/jthread
  40 +
  41 +LIBSPATH= -L $(DEPEND_DIR)/lib -lavformat -lavcodec -lswscale -lavutil -lavfilter -lswresample -lavdevice \
  42 + -L $(CUDA_ROOT)/lib64 -lcuda -lcudart -lnvcuvid -lcurand -lcublas -lnvjpeg \
  43 + -L $(SPDLOG_ROOT) -l:libspdlog.a \
  44 + -L $(JRTP_ROOT)/jthread/lib -l:libjthread.a \
  45 + -L $(JRTP_ROOT)/jrtplib/lib -l:libjrtp.a
  46 +
  47 +
  48 +CXXFLAGS= -g -O0 -fPIC $(INCLUDE) $(DEFS) -lpthread -lrt -lz -fexceptions -std=c++11 -fvisibility=hidden -Wl,-Bsymbolic -ldl -Wwrite-strings
  49 +# CFLAGS= -g -fPIC -O0 $(INCLUDE) -pthread -lrt -lz -std=c++11 -fvisibility=hidden -Wl,-Bsymbolic -ldl
  50 + # -DUNICODE -D_UNICODE
  51 +
  52 +NFLAGS_LIB=-g -c -shared -Xcompiler -fPIC -Xcompiler -fvisibility=hidden
  53 +NFLAGS = $(NFLAGS_LIB) $(INCLUDE) -std=c++11
  54 +
  55 +# CU_SOURCES = $(wildcard ${SRC_DIR}/*.cu)
  56 +# CU_OBJS = $(patsubst %.cu, %.o, $(notdir $(CU_SOURCES)))
  57 +
  58 +CU_SOURCES:=$(notdir $(wildcard $(SRC_DIR)/*.cu))
  59 +CU_OBJS:=$(addprefix $(OBJ_DIR)/, $(patsubst %.cu, %.o, $(CU_SOURCES)))
  60 +
  61 +
  62 +# 默认最终目标
  63 +.PHONY:all
  64 +all:$(TARGET)
  65 +
  66 +# 生成最终目标
  67 +$(TARGET):$(OBJS) $(CU_OBJS) | $(LIB_DIR)
  68 + @echo -e "\e[32m""Linking static library $(TARGET)""\e[0m"
  69 +# @ar -rc $@ $^
  70 +
  71 +# 若没有lib目录则自动生成
  72 +$(LIB_DIR):
  73 + @mkdir -p $@
  74 +
  75 +# 生成中间目标文件
  76 +$(OBJ_DIR)/%.o:$(SRC_DIR)/%.cpp $(DEP_DIR)/%.d | $(OBJ_DIR) $(DEP_DIR)
  77 +# @echo -e "\e[33m""Building object $@""\e[0m"
  78 + @$(CXX) -c $(DEPFLAGS) $(CXXFLAGS) $(INCS) $(LIBSPATH) $(MACROS) -o $@ $<
  79 +
  80 +$(OBJ_DIR)%.o:$(SRC_DIR)/%.cu
  81 + @echo "#######################CU_OBJS:$@###############"
  82 + $(NVCC) $(NFLAGS) -o $@ $<
  83 +
  84 +
  85 +# 若没有obj目录则自动生成
  86 +$(OBJ_DIR):
  87 + @mkdir -p $@
  88 +
  89 +# 若没有.dep目录则自动生成
  90 +$(DEP_DIR):
  91 + @mkdir -p $@
  92 +
  93 +# 依赖文件会在生成中间文件的时候自动生成,这里只是为了防止报错
  94 +$(DEPS):
  95 +
  96 +# 引入中间目标文件头文件依赖关系
  97 +include $(wildcard $(DEPS))
  98 +
  99 +# 直接删除组件build目录
  100 +.PHONY:clean
  101 +clean:
  102 + @rm -rf $(BUILD_DIR)/$(MODULE)
... ...
src/nvdecoder/NV12ToRGB.cu
... ... @@ -2,7 +2,7 @@
2 2 #include "cuda_kernels.h"
3 3  
4 4 #include <builtin_types.h>
5   -#include "common/inc/helper_cuda_drvapi.h"
  5 +#include "helper_cuda_drvapi.h"
6 6  
7 7 typedef unsigned char uint8;
8 8 typedef unsigned int uint32;
... ...
src/nvdecoder/NvDecoderApi.cpp 0 → 100644
  1 +#include "NvDecoderApi.h"
  2 +#include "FFNvDecoder.h"
  3 +
  4 +NvDecoderApi::NvDecoderApi(){
  5 + m_pDecoder = nullptr;
  6 +}
  7 +
  8 +NvDecoderApi::~NvDecoderApi(){
  9 + if(m_pDecoder != nullptr){
  10 + delete m_pDecoder;
  11 + m_pDecoder = nullptr;
  12 + }
  13 +}
  14 +
  15 +bool NvDecoderApi::init(FFDecConfig& cfg){
  16 + m_pDecoder = new FFNvDecoder();
  17 + if(m_pDecoder != nullptr){
  18 + return m_pDecoder->init(cfg);
  19 + }
  20 + return false;
  21 +}
  22 +
  23 +void NvDecoderApi::close(){
  24 + if(m_pDecoder != nullptr){
  25 + return m_pDecoder->close();
  26 + }
  27 +}
  28 +
  29 +bool NvDecoderApi::start(){
  30 + if(m_pDecoder != nullptr){
  31 + return m_pDecoder->start();
  32 + }
  33 + return false;
  34 +}
  35 +
  36 +void NvDecoderApi::pause(){
  37 + if(m_pDecoder != nullptr){
  38 + return m_pDecoder->pause();
  39 + }
  40 +}
  41 +
  42 +void NvDecoderApi::resume(){
  43 + if(m_pDecoder != nullptr){
  44 + return m_pDecoder->resume();
  45 + }
  46 +}
  47 +
  48 +void NvDecoderApi::setDecKeyframe(bool bKeyframe){
  49 + if(m_pDecoder != nullptr){
  50 + return m_pDecoder->setDecKeyframe(bKeyframe);
  51 + }
  52 +}
  53 +
  54 +bool NvDecoderApi::isRunning(){
  55 + if(m_pDecoder != nullptr){
  56 + return m_pDecoder->isRunning();
  57 + }
  58 + return false;
  59 +}
  60 +
  61 +bool NvDecoderApi::isFinished(){
  62 + if(m_pDecoder != nullptr){
  63 + return m_pDecoder->isFinished();
  64 + }
  65 + return false;
  66 +}
  67 +
  68 +bool NvDecoderApi::isPausing(){
  69 + if(m_pDecoder != nullptr){
  70 + return m_pDecoder->isPausing();
  71 + }
  72 + return false;
  73 +}
  74 +
  75 +bool NvDecoderApi::getResolution(int &width, int &height){
  76 + if(m_pDecoder != nullptr){
  77 + return m_pDecoder->getResolution(width, height);
  78 + }
  79 + return false;
  80 +}
  81 +
  82 +bool NvDecoderApi::isSurport(FFDecConfig& cfg){
  83 + if(m_pDecoder != nullptr){
  84 + return m_pDecoder->isSurport(cfg);
  85 + }
  86 + return false;
  87 +}
  88 +
  89 +float NvDecoderApi::fps(){
  90 + if(m_pDecoder != nullptr){
  91 + return m_pDecoder->fps();
  92 + }
  93 + return 0.0;
  94 +}
  95 +
  96 +int NvDecoderApi::getCachedQueueLength(){
  97 + if(m_pDecoder != nullptr){
  98 + return m_pDecoder->getCachedQueueLength();
  99 + }
  100 + return 0;
  101 +}
  102 +
  103 +void NvDecoderApi::setName(string nm){
  104 + if(m_pDecoder != nullptr){
  105 + return m_pDecoder->setName(nm);
  106 + }
  107 +}
  108 +
  109 +string NvDecoderApi::getName(){
  110 + if(m_pDecoder != nullptr){
  111 + return m_pDecoder->getName();
  112 + }
  113 + return nullptr;
  114 +}
  115 +
  116 +FFImgInfo* NvDecoderApi::snapshot(){
  117 + if(m_pDecoder != nullptr){
  118 + return m_pDecoder->snapshot();
  119 + }
  120 + return nullptr;
  121 +}
  122 +
  123 +void NvDecoderApi::setPostDecArg(const void* postDecArg){
  124 + if(m_pDecoder != nullptr){
  125 + return m_pDecoder->setPostDecArg(postDecArg);
  126 + }
  127 +}
  128 +
  129 +void NvDecoderApi::setFinishedDecArg(const void* finishedDecArg){
  130 + if(m_pDecoder != nullptr){
  131 + return m_pDecoder->setFinishedDecArg(finishedDecArg);
  132 + }
  133 +}
0 134 \ No newline at end of file
... ...
src/nvdecoder/NvDecoderApi.h 0 → 100644
  1 +#include<string>
  2 +#include <pthread.h>
  3 +
  4 +#include "common_header.h"
  5 +#include "../interface/AbstractDecoder.h"
  6 +
  7 +using namespace std;
  8 +
  9 +class FFNvDecoder;
  10 +
  11 +class NvDecoderApi : public AbstractDecoder{
  12 +public:
  13 + NvDecoderApi();
  14 + ~NvDecoderApi();
  15 + bool init(FFDecConfig& cfg);
  16 + void close();
  17 + bool start();
  18 + void pause();
  19 + void resume();
  20 +
  21 + void setDecKeyframe(bool bKeyframe);
  22 +
  23 + bool isRunning();
  24 + bool isFinished();
  25 + bool isPausing();
  26 + bool getResolution( int &width, int &height );
  27 +
  28 + bool isSurport(FFDecConfig& cfg);
  29 +
  30 + int getCachedQueueLength();
  31 +
  32 + float fps();
  33 +
  34 + FFImgInfo* snapshot();
  35 +
  36 + DECODER_TYPE getDecoderType(){ return DECODER_TYPE_DVPP; }
  37 + void setName(string nm);
  38 + string getName();
  39 +
  40 + void setPostDecArg(const void* postDecArg);
  41 + void setFinishedDecArg(const void* finishedDecArg);
  42 +private:
  43 + FFNvDecoder* m_pDecoder;
  44 +};
0 45 \ No newline at end of file
... ...
src/nvdecoder/common_header.h
... ... @@ -4,5 +4,6 @@
4 4  
5 5 #include "../interface/logger.hpp"
6 6 #include "../interface/utiltools.hpp"
  7 +#include "../interface/interface_headers.h"
7 8  
8 9 #endif
9 10 \ No newline at end of file
... ...