瀏覽代碼

complete driver_cameara_tz.

yuchuli 3 年之前
父節點
當前提交
a0e0ec0093

+ 30 - 2
src/driver/driver_camera_tz/driver_camera_tz.pro

@@ -15,16 +15,44 @@ DEFINES += QT_DEPRECATED_WARNINGS
 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
 
 SOURCES += main.cpp \
-    v4l2cam.cpp
+    ../../common/common/nvenc/classes/NvApplicationProfiler.cpp \
+    ../../common/common/nvenc/classes/NvBuffer.cpp \
+    ../../common/common/nvenc/classes/NvDrmRenderer.cpp \
+    ../../common/common/nvenc/classes/NvEglRenderer.cpp \
+    ../../common/common/nvenc/classes/NvElement.cpp \
+    ../../common/common/nvenc/classes/NvElementProfiler.cpp \
+    ../../common/common/nvenc/classes/NvLogging.cpp \
+    ../../common/common/nvenc/classes/NvUtils.cpp \
+    ../../common/common/nvenc/classes/NvV4l2Element.cpp \
+    ../../common/common/nvenc/classes/NvV4l2ElementPlane.cpp \
+    ../../common/common/nvenc/classes/NvVideoConverter.cpp \
+    ../../common/common/nvenc/classes/NvVideoDecoder.cpp \
+    ../../common/common/nvenc/classes/NvVideoEncoder.cpp \
+    v4l2cam.cpp \
+    ../driver_h264_nvenc/video_encode_csvparser.cpp \
+    ../driver_h264_nvenc/video_encode_main.cpp \
+    ../driver_h264_nvenc/zerobuffer.cpp
 
 HEADERS += \
     libyuv.h \
-    v4l2cam.hpp
+    v4l2cam.hpp \
+    ../driver_h264_nvenc/zerobuffer.h
 
 
 !include(../../../include/common.pri ) {
     error( "Couldn't find the common.pri file!" )
 }
 
+INCLUDEPATH += ../../common/common/nvenc/include
+INCLUDEPATH += /usr/include/drm
+INCLUDEPATH += $$PWD/../driver_h264_nvenc
+
+
+DEFINES += TZTEK_V4L2_DMA
 
 LIBS += -L$$PWD -lyuv
+
+LIBS += -L/usr/lib/aarch64-linux-gnu/tegra -lnvbuf_utils
+
+
+LIBS += -L/usr/lib/aarch64-linux-gnu/tegra -lv4l2 -lnvbuf_utils -lEGL -ldrm -lX11 -lGLESv2

+ 12 - 0
src/driver/driver_camera_tz/driver_camera_tz.xml

@@ -0,0 +1,12 @@
+<xml>	
+	<node name="driver_camera_tz">
+		<param name="framename" value="h264frame" />
+		<param name="dev" value = "/dev/video4" />
+		<param name="width" value="1280" />
+		<param name="height" value = "720" />
+		<param name="name" value="cameara0" />
+		<param name="bitrate" value="2000000" />
+		<param name="GOP" value="10" />
+		<param name="Rotate" value="false" />
+	</node>
+</xml>

+ 145 - 14
src/driver/driver_camera_tz/main.cpp

@@ -18,20 +18,64 @@ static bool gbRun = true;
 #define MAX_HEIGHT 2160
 
 
-void threadcap()
-{
+#include "modulecomm.h"
 
-    std::string dev;                     //dev
-    std::string name;                     //设备名称
-    std::int32_t width;
-    std::int32_t height;
+#include <QMutex>
+#include <QWaitCondition>
+#include <thread>
 
+#include "zerobuffer.h"
 
-    iv::xmlparam::Xmlparam xp("./driver_camera_tz.xml");
-    xp.GetParam("dev", dev, "/dev/video0");
-    xp.GetParam("name", name, "camera0");
-    xp.GetParam("width", width, 1280);
-    xp.GetParam("height", height, 720);
+ZeroBuffer * gbuffer;
+
+extern char * gstrbuffer;
+extern QMutex gMutexLock;
+extern bool gbNewData;
+extern QWaitCondition gwc;
+
+static char * gstrwidth ="1280";
+static char * gstrheight = "720";
+int gnwidth,gnheight;
+
+extern void StartNVENC(char * strwidth,char * strheight);
+
+extern bool gbNewOut;
+extern QWaitCondition gwcout;
+extern char * gstrout;
+extern QMutex gWaitMutexout;
+extern QMutex gMutexout;
+extern int gnoutsize ;
+
+void * gpaout;
+
+void yuv_rotate_180(uint8_t* yuvbuf, uint8_t* dstbuf,int width,int height){
+
+    int idx = 0;
+    //旋转180:将右下角的点作为第一个点,从右往左,从下往上取点
+    //Y 宽:[0,w-1]  高:[0,h-1]
+    for (int i = height-1; i >=0; i--){
+        for (int j = width-1 ; j >= 0; j--){
+            dstbuf[idx++] = *(yuvbuf+(i*width+j));
+        }
+    }
+    uint8_t* uheader = yuvbuf + width*height;
+    //U
+    for (int i = height/2 - 1; i >= 0; i--){
+        for (int j = width/2-1 ; j >= 0; j--){
+            dstbuf[idx++] = *(uheader + (i*width / 2 + j));
+        }
+    }
+    uint8_t* vheader = uheader + width*height/4;
+    //V
+    for (int i = height / 2 - 1; i >= 0; i--){
+        for (int j = width / 2-1 ; j >=0; j--){
+            dstbuf[idx++] = *(vheader + (i*width / 2 + j));
+        }
+    }
+}
+
+void threadcap( std::string dev,std::string name,std::int32_t width,std::int32_t height,bool bRotate)
+{
 
     printf("width=%d,height=%d\n",width,height);
     CV4l2Cam  cam(dev,width,height);
@@ -50,6 +94,7 @@ void threadcap()
 
     cam.startAcquire();
     static unsigned char yuv[3*MAX_HEIGHT*MAX_WIDHT/2];
+    unsigned char * yuvrotate = new unsigned  char[3*MAX_HEIGHT*MAX_WIDHT/2];
     unsigned char *yuyv = NULL;
     int yuyvlen = 0;
 
@@ -65,22 +110,108 @@ void threadcap()
                     width,
                     height);
 
-
+ //           std::cout<<" grab a img. time: "<<std::chrono::system_clock::now().time_since_epoch().count()/1000000<<"ms"<<std::endl;
+
+
+            char * p;
+            int nrtn = gbuffer->RequireBuffer(&p,1000);
+            if(nrtn < 0)
+            {
+                std::cout<<" Get Zero Buffer Fail."<<std::endl;
+                continue;
+            }
+            if(bRotate == false)
+                memcpy(p,(char *)yuv,width*height*3/2);
+            else
+            {
+ //               int64_t time1 = std::chrono::system_clock::now().time_since_epoch().count();
+                yuv_rotate_180(yuv,yuvrotate,width,height);
+ //               int64_t time2 = std::chrono::system_clock::now().time_since_epoch().count();
+  //              std::cout<<" rotate use "<<(time2 - time1)/1000<<" us "<<std::endl;
+                memcpy(p,(char *)yuvrotate,width*height*3/2);
+            }
+            gbuffer->UpdateDataSize(width*height*3/2);
+            gbuffer->ReleaseBuffer();
 //                    imgpub.Send(yuv,3*width*height/2);
         }
-        std::cout<<" time: "<<std::chrono::system_clock::now().time_since_epoch().count()/1000000<<"ms"<<std::endl;
+
 
     }
     cam.stopAcquire();
     cam.Release();
 }
 
+
+void threadout()
+{
+    char * strout = new char[10000000];
+    int nout = 0;
+    while(1)
+    {
+        gWaitMutexout.lock();
+        gwcout.wait(&gWaitMutexout,10);
+        gWaitMutexout.unlock();
+        if(gbNewOut)
+        {
+            gMutexout.lock();
+            memcpy(strout, gstrout,gnoutsize);
+            nout = gnoutsize;
+            gbNewOut = false;
+            gMutexout.unlock();
+//            std::cout<<"send out. "<<std::endl;
+            iv::modulecomm::ModuleSendMsg(gpaout,strout,nout);
+        }
+    }
+}
+
 int main(int argc, char *argv[])
 {
     QCoreApplication a(argc, argv);
 
-    std::thread * pthreadcap = new std::thread(threadcap);
+    QString strpath = QCoreApplication::applicationDirPath();
+//    QString apppath = strpath;
+    if(argc < 2)
+        strpath = strpath + "/driver_camera_tz.xml";
+    else
+        strpath = argv[1];
+    std::cout<<strpath.toStdString()<<std::endl;
+
+
+    std::string dev;                     //dev
+    std::string name;                     //设备名称
+    std::int32_t width;
+    std::int32_t height;
+    std::string strframename;
+    int ngop;
+    int nbitrate;
+    bool bRotate;
+
+    iv::xmlparam::Xmlparam xp(strpath.toStdString());
+    xp.GetParam("dev", dev, "/dev/video4");
+    xp.GetParam("name", name, "camera0");
+    xp.GetParam("width", width, 1280);
+    xp.GetParam("height", height, 720);
+    xp.GetParam("framename",strframename,"h264frame");
+    xp.GetParam("bitrate",nbitrate,2000000);
+    xp.GetParam("GOP",ngop,10);
+    xp.GetParam("Rotate",bRotate,false);
+
+
+    gbuffer = new ZeroBuffer(30000000);
+    gstrbuffer = new char[30000000];
+    gstrout = new char[10000000];
+    gnwidth = width;
+    gnheight = height;
+
+    gpaout = iv::modulecomm::RegisterSend(strframename.data(),1000000,1);
+
+
+    std::thread * pthreadcap = new std::thread(threadcap,dev,name,width,height,bRotate);
+    void * pthreadout = new std::thread(threadout);
     (void )pthreadcap;
+    (void)pthreadout;
+
+    StartNVENC(gstrwidth,gstrheight);
 
     return a.exec();
 }

+ 1 - 1
src/driver/driver_cloud_grpc_client_h264/grpcclient.cpp

@@ -8,7 +8,7 @@ void ListenData(const char * strdata,const unsigned int nSize,const unsigned int
 
 }
 
-#define TESTUP
+//#define TESTUP
 void ListenPicData(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname)
 {
 #ifndef TESTUP

+ 17 - 6
src/driver/driver_h264_nvenc/video_encode_main.cpp

@@ -327,7 +327,7 @@ encoder_capture_plane_dq_callback(struct v4l2_buffer *v4l2_buf, NvBuffer * buffe
     if (!ctx->stats)
     {
         std::cout<<" out a buffer, size: "<<buffer->planes[0].bytesused<<std::endl;
-        if(a1 < 30)
+ //       if(a1 < 30)
             write_encoder_output_frame(ctx->out_file, buffer);
         a1++;
         share_encoder_output_frame(buffer);
@@ -619,7 +619,7 @@ set_defaults(context_t * ctx)
     ctx->peak_bitrate = 0;
     ctx->profile = V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE;
     ctx->ratecontrol = V4L2_MPEG_VIDEO_BITRATE_MODE_CBR;
-    ctx->iframe_interval = 30;
+    ctx->iframe_interval = 10;
     ctx->externalRPS = false;
     ctx->enableGDR = false;
     ctx->enableROI = false;
@@ -629,7 +629,7 @@ set_defaults(context_t * ctx)
     ctx->enableLossless = false;
     ctx->nH264FrameNumBits = 0;
     ctx->nH265PocLsbBits = 0;
-    ctx->idr_interval = 256;
+    ctx->idr_interval = 30;//256;
     ctx->level = -1;
     ctx->fps_n = 30;//30;
     ctx->fps_d = 1;
@@ -1403,8 +1403,18 @@ encode_proc(context_t& ctx, int argc, char *argv[])
     /* Set default values for encoder context members. */
     set_defaults(&ctx);
 
+    int targc = 6;
+    char * targv[7];
+    targv[0] = "videoc_encode";
+    targv[1] = "/home/nvidia/testYUV.yuv";//"/home/nvidia/code/jetson_multimedia_api/samples/00_video_decode/x.yuv";
+    targv[2] = argv[2];
+    targv[3] = argv[3];
+    targv[4] = "H264";
+    targv[5] = "/home/nvidia/testh264.mp4";
+    targv[6] = 0;
+
     /* Parse application command line options. */
-    ret = parse_csv_args(&ctx, argc, argv);
+    ret = parse_csv_args(&ctx, targc, targv);
     ctx.insert_sps_pps_at_idr = true;
     TEST_ERROR(ret < 0, "Error parsing commandline arguments", cleanup);
 
@@ -1436,8 +1446,8 @@ encode_proc(context_t& ctx, int argc, char *argv[])
     }
 
     /* Open input file  for raw yuv */
-    ctx.in_file = new ifstream(ctx.in_file_path);
-    TEST_ERROR(!ctx.in_file->is_open(), "Could not open input file", cleanup);
+//    ctx.in_file = new ifstream(ctx.in_file_path);
+//    TEST_ERROR(!ctx.in_file->is_open(), "Could not open input file", cleanup);
 
 //    ctx.stats = true;
     if (!ctx.stats)
@@ -1591,6 +1601,7 @@ encode_proc(context_t& ctx, int argc, char *argv[])
     }
 
     /* Set IDR frame interval for encoder */
+
     ret = ctx.enc->setIDRInterval(ctx.idr_interval);
     TEST_ERROR(ret < 0, "Could not set encoder IDR interval", cleanup);