| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311 |
- #include <QCoreApplication>
- #include <thread>
- #include <opencv2/opencv.hpp>
- #include <opencv2/core.hpp>
- extern "C"
- {
- #include <libavcodec/avcodec.h>
- #include <libavutil/opt.h>
- #include <libavutil/imgutils.h>
- #include <libavutil/common.h>
- }
- #ifdef USE_OPENCV4
- #include "opencv2/imgcodecs/legacy/constants_c.h" //OpenCV4 use this line
- #include <opencv2/imgproc/types_c.h> //OpenCV4 use this line
- #endif
- #include <opencv2/videoio.hpp>
- #include <QTime>
- #include <QFile>
- #define TESTYUV 0
- #include "modulecomm.h"
- #include "rawpic.pb.h"
- #include "xmlparam.h"
- #include "ivversion.h"
- void * gpa;
- int gindex = 0;
- int gwidth = 1920;
- int gheight = 1080;
- int gcamindex = 0;
- int gcompress = 1;
- int gcompquality = 95;
- std::string gmsgname = "image00";
- std::string gstrdevname = "video0";
- std::string video_path = "/home/nvidia/Videos/video.mp4";
- int time_sleep = 0;
- #ifndef CV_CAP_PROP_FRAME_WIDTH
- #define CV_CAP_PROP_FRAME_WIDTH cv::CAP_PROP_FRAME_WIDTH
- #define CV_CAP_PROP_FRAME_HEIGHT cv::CAP_PROP_FRAME_HEIGHT
- #define CV_CAP_PROP_FPS CAP_PROP_FPS
- #define CV_CAP_PROP_FOURCC CAP_PROP_FOURCC
- #define CV_BGR2GRAY cv::COLOR_BGR2GRAY
- #endif
- static void encode(AVCodecContext *enc_ctx, AVFrame *frame, AVPacket *pkt,
- FILE *outfile)
- {
- int ret;
- /* send the frame to the encoder */
- if (frame)
- printf("Send frame %3"PRId64"\n", frame->pts);
- ret = avcodec_send_frame(enc_ctx, frame);
- if (ret < 0) {
- fprintf(stderr, "Error sending a frame for encoding\n");
- exit(1);
- }
- while (ret >= 0) {
- ret = avcodec_receive_packet(enc_ctx, pkt);
- if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
- return;
- else if (ret < 0) {
- fprintf(stderr, "Error during encoding\n");
- exit(1);
- }
- printf("Write packet %3"PRId64" (size=%5d)\n", pkt->pts, pkt->size);
- fwrite(pkt->data, 1, pkt->size, outfile);
- av_packet_unref(pkt);
- }
- }
- void VideoThread(int n)
- {
- QTime xT;
- xT.start();
- int ncount = 0;
- int nserbufsize = 20000000;
- char * strser = new char[nserbufsize];
- cv::VideoCapture capture;
- #ifdef TESTYUV
- QFile xFileYUV;
- xFileYUV.setFileName("/home/nvidia/testYUV.yuv");
- bool bTestYUV = false;
- if(xFileYUV.open(QIODevice::ReadWrite))
- {
- bTestYUV = true;
- }
- else
- {
- std::cout<<" Open YUV File Fail."<<std::endl;
- }
- #endif
- ////If OpenCV4 use this
- // //std::string gstr_f = "v4l2src device=/dev/video"+std::to_string(gcamindex)+" ! video/x-raw, width=(int)"+std::to_string(gwidth)+", height=(int)"+std::to_string(gheight)+" ! videoconvert ! appsink";
- // std::string gstr_f = "/home/nvidia/Videos/video.mp4";
- //#ifdef USE_OPENCV4
- // if(gstrcamera.length() > 2)
- // {
- // //gstr_f = "v4l2src device=/dev/"+gstrcamera+" ! video/x-raw, width=(int)"+std::to_string(gwidth)+", height=(int)"+std::to_string(gheight)+" ! videoconvert ! appsink";
- // gstr_f = "/home/nvidia/Videos/video.mp4";
- // std::cout<<gstr_f<<std::endl;
- // std::cout<<gstr_f<<std::endl;
- // capture.open(gstr_f);
- // }
- // else
- // capture.open(gcamindex);
- //#else
- // capture.open(gcamindex);
- //#endif
- // capture.set(CV_CAP_PROP_FRAME_WIDTH,gwidth);
- // capture.set(CV_CAP_PROP_FRAME_HEIGHT,gheight);
- std::cout<<video_path<<std::endl;
- capture.open(video_path);
- while(true)
- {
- cv::Mat mat, mat1;
- capture>>mat;
- cv::resize(mat, mat1, cv::Size(1280, 720), 0, 0, cv::INTER_LINEAR);
- cv::imshow("video_replay",mat1);
- if(!mat1.empty())
- {
- #ifdef TESTYUV
- if(bTestYUV &&(ncount<61))
- {
- cv::Mat dstYuvImage;
- QTime xTime;
- xTime.start();
- cv::cvtColor(mat1, dstYuvImage, CV_BGR2YUV_I420);
- std::cout<<" cvt time: "<<xTime.elapsed()<<std::endl;
- xFileYUV.write((char *)dstYuvImage.data,gwidth*gheight*3/2);
- xFileYUV.flush();
- }
- else
- {
- xFileYUV.close();
- }
- #endif
- ncount++;
- std::cout<<" count "<<ncount<<std::endl;
- QTime timex;
- timex.start();
- iv::vision::rawpic pic;
- qint64 time = QDateTime::currentMSecsSinceEpoch();
- QTime xg;
- xg.start();
- pic.set_time(time);
- pic.set_index(gindex);gindex++;
- pic.set_elemsize(mat1.elemSize());
- pic.set_width(mat1.cols);
- pic.set_height(mat1.rows);
- pic.set_mattype(mat1.type());
- // cv::Mat matcompress;
- // cv::resize(mat1,matcompress,cv::Size(640,360));
- std::vector<int> param = std::vector<int>(2);
- param[0] = CV_IMWRITE_JPEG_QUALITY;
- param[1] = gcompquality; // default(95) 0-100
- std::vector<unsigned char> buff;
- if(gcompress == 1)
- {
- cv::imencode(".jpg", mat1, buff, param);
- // cv::imencode(".jpg", matcompress, buff, param);
- pic.set_picdata(buff.data(),buff.size());
- buff.clear();
- pic.set_type(2);
- }
- else
- {
- pic.set_picdata(mat1.data,mat1.rows*mat1.cols*mat1.elemSize());
- // pic.set_picdata(matcompress.data,matcompress.rows*matcompress.cols*matcompress.elemSize());
- pic.set_type(1);
- }
- // qDebug("buff size is %d",buff.size());
- int nSize = pic.ByteSize();
- // char * strser = new char[nSize];
- bool bser = pic.SerializeToArray(strser,nSize);
- qDebug("time is %ld pac time is %d size is %d",QDateTime::currentMSecsSinceEpoch(),xg.elapsed(),nSize);
- // iv::modulecomm::ModuleSendMsg(gpa,str.data(),str.length());
- if(bser)iv::modulecomm::ModuleSendMsg(gpa,strser,nSize);
- else
- {
- std::cout<<"usbcamera "<< "serialize error. "<<std::endl;
- }
- // delete strser;
- // delete strout;
- // qDebug("%d have a pic. len is %d",xT.elapsed(),str.length());
- // qDebug("now is %d",xT.elapsed());
- // gw->mMutex.lock();
- // mat1.copyTo(gw->msrcImage);
- // gw->mMutex.unlock();
- // gMutexInput.lock();
- // mat1.copyTo(gsrcImage);
- // g_CapIndex++;
- // gMutexInput.unlock();
- }
- else
- {
- qDebug("no data.");
- std::this_thread::sleep_for(std::chrono::milliseconds(1));
- }
- std::this_thread::sleep_for(std::chrono::milliseconds(time_sleep));
- }
- }
- #include <thread>
- void testthread()
- {
- void * mpx = iv::modulecomm::RegisterSend("test121",10000000,1);
- char * str = new char[10000000];
- while(1)
- {
- iv::modulecomm::ModuleSendMsg(mpx,str,9000000);
- }
- }
- int main(int argc, char *argv[])
- {
- cv::namedWindow("video_replay",cv::WINDOW_NORMAL);
- showversion("video_replay");
- QCoreApplication a(argc, argv);
- // std::thread * pthread = new std::thread(testthread);
- // return a.exec();
- QString strpath = QCoreApplication::applicationDirPath();
- // QString apppath = strpath;
- if(argc < 2)
- strpath = strpath + "/video_replay.xml";
- else
- strpath = argv[1];
- std::cout<<strpath.toStdString()<<std::endl;
- iv::xmlparam::Xmlparam xp(strpath.toStdString());
- std::string xmlmsgname = xp.GetParam("msgname","image00");
- gmsgname = xmlmsgname;
- std::string strwidth = xp.GetParam("width","1280");
- std::string strheight = xp.GetParam("height","720");
- std::string strcompress = xp.GetParam("bcompress","1");
- std::string strcompquality = xp.GetParam("compquality","95");
- std::string videoPath = xp.GetParam("video_path","/home/nvidia/Videos/video.mp4");
- std::string timeSleep = xp.GetParam("time_sleep","0");
- gcompquality = atoi(strcompquality.data());
- gwidth = atoi(strwidth.data());
- gheight = atoi(strheight.data());
- gcompress = atoi(strcompress.data());
- time_sleep = atoi(timeSleep.data());
- video_path = videoPath;
- std::cout<<"video_path:"<<video_path<<std::endl;
- gpa = iv::modulecomm::RegisterSend(gmsgname.data(),20000000,1);
- std::thread * mthread = new std::thread(VideoThread,0);
- return a.exec();
- }
|