main.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. #include <QCoreApplication>
  2. #include <thread>
  3. #include <opencv2/opencv.hpp>
  4. #include <opencv2/core.hpp>
  5. extern "C"
  6. {
  7. #include <libavcodec/avcodec.h>
  8. #include <libavutil/opt.h>
  9. #include <libavutil/imgutils.h>
  10. #include <libavutil/common.h>
  11. }
  12. #ifdef USE_OPENCV4
  13. #include "opencv2/imgcodecs/legacy/constants_c.h" //OpenCV4 use this line
  14. #include <opencv2/imgproc/types_c.h> //OpenCV4 use this line
  15. #endif
  16. #include <opencv2/videoio.hpp>
  17. #include <QTime>
  18. #include <QFile>
  19. #define TESTYUV 0
  20. #include "modulecomm.h"
  21. #include "rawpic.pb.h"
  22. #include "xmlparam.h"
  23. #include "ivversion.h"
  24. void * gpa;
  25. int gindex = 0;
  26. int gwidth = 1920;
  27. int gheight = 1080;
  28. int gcamindex = 0;
  29. int gcompress = 1;
  30. int gcompquality = 95;
  31. std::string gmsgname = "image00";
  32. std::string gstrdevname = "video0";
  33. std::string video_path = "/home/nvidia/Videos/video.mp4";
  34. int time_sleep = 0;
  35. #ifndef CV_CAP_PROP_FRAME_WIDTH
  36. #define CV_CAP_PROP_FRAME_WIDTH cv::CAP_PROP_FRAME_WIDTH
  37. #define CV_CAP_PROP_FRAME_HEIGHT cv::CAP_PROP_FRAME_HEIGHT
  38. #define CV_CAP_PROP_FPS CAP_PROP_FPS
  39. #define CV_CAP_PROP_FOURCC CAP_PROP_FOURCC
  40. #define CV_BGR2GRAY cv::COLOR_BGR2GRAY
  41. #endif
  42. static void encode(AVCodecContext *enc_ctx, AVFrame *frame, AVPacket *pkt,
  43. FILE *outfile)
  44. {
  45. int ret;
  46. /* send the frame to the encoder */
  47. if (frame)
  48. printf("Send frame %3"PRId64"\n", frame->pts);
  49. ret = avcodec_send_frame(enc_ctx, frame);
  50. if (ret < 0) {
  51. fprintf(stderr, "Error sending a frame for encoding\n");
  52. exit(1);
  53. }
  54. while (ret >= 0) {
  55. ret = avcodec_receive_packet(enc_ctx, pkt);
  56. if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
  57. return;
  58. else if (ret < 0) {
  59. fprintf(stderr, "Error during encoding\n");
  60. exit(1);
  61. }
  62. printf("Write packet %3"PRId64" (size=%5d)\n", pkt->pts, pkt->size);
  63. fwrite(pkt->data, 1, pkt->size, outfile);
  64. av_packet_unref(pkt);
  65. }
  66. }
  67. void VideoThread(int n)
  68. {
  69. QTime xT;
  70. xT.start();
  71. int ncount = 0;
  72. int nserbufsize = 20000000;
  73. char * strser = new char[nserbufsize];
  74. cv::VideoCapture capture;
  75. #ifdef TESTYUV
  76. QFile xFileYUV;
  77. xFileYUV.setFileName("/home/nvidia/testYUV.yuv");
  78. bool bTestYUV = false;
  79. if(xFileYUV.open(QIODevice::ReadWrite))
  80. {
  81. bTestYUV = true;
  82. }
  83. else
  84. {
  85. std::cout<<" Open YUV File Fail."<<std::endl;
  86. }
  87. #endif
  88. ////If OpenCV4 use this
  89. // //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";
  90. // std::string gstr_f = "/home/nvidia/Videos/video.mp4";
  91. //#ifdef USE_OPENCV4
  92. // if(gstrcamera.length() > 2)
  93. // {
  94. // //gstr_f = "v4l2src device=/dev/"+gstrcamera+" ! video/x-raw, width=(int)"+std::to_string(gwidth)+", height=(int)"+std::to_string(gheight)+" ! videoconvert ! appsink";
  95. // gstr_f = "/home/nvidia/Videos/video.mp4";
  96. // std::cout<<gstr_f<<std::endl;
  97. // std::cout<<gstr_f<<std::endl;
  98. // capture.open(gstr_f);
  99. // }
  100. // else
  101. // capture.open(gcamindex);
  102. //#else
  103. // capture.open(gcamindex);
  104. //#endif
  105. // capture.set(CV_CAP_PROP_FRAME_WIDTH,gwidth);
  106. // capture.set(CV_CAP_PROP_FRAME_HEIGHT,gheight);
  107. std::cout<<video_path<<std::endl;
  108. capture.open(video_path);
  109. while(true)
  110. {
  111. cv::Mat mat, mat1;
  112. capture>>mat;
  113. cv::resize(mat, mat1, cv::Size(1280, 720), 0, 0, cv::INTER_LINEAR);
  114. cv::imshow("video_replay",mat1);
  115. if(!mat1.empty())
  116. {
  117. #ifdef TESTYUV
  118. if(bTestYUV &&(ncount<61))
  119. {
  120. cv::Mat dstYuvImage;
  121. QTime xTime;
  122. xTime.start();
  123. cv::cvtColor(mat1, dstYuvImage, CV_BGR2YUV_I420);
  124. std::cout<<" cvt time: "<<xTime.elapsed()<<std::endl;
  125. xFileYUV.write((char *)dstYuvImage.data,gwidth*gheight*3/2);
  126. xFileYUV.flush();
  127. }
  128. else
  129. {
  130. xFileYUV.close();
  131. }
  132. #endif
  133. ncount++;
  134. std::cout<<" count "<<ncount<<std::endl;
  135. QTime timex;
  136. timex.start();
  137. iv::vision::rawpic pic;
  138. qint64 time = QDateTime::currentMSecsSinceEpoch();
  139. QTime xg;
  140. xg.start();
  141. pic.set_time(time);
  142. pic.set_index(gindex);gindex++;
  143. pic.set_elemsize(mat1.elemSize());
  144. pic.set_width(mat1.cols);
  145. pic.set_height(mat1.rows);
  146. pic.set_mattype(mat1.type());
  147. // cv::Mat matcompress;
  148. // cv::resize(mat1,matcompress,cv::Size(640,360));
  149. std::vector<int> param = std::vector<int>(2);
  150. param[0] = CV_IMWRITE_JPEG_QUALITY;
  151. param[1] = gcompquality; // default(95) 0-100
  152. std::vector<unsigned char> buff;
  153. if(gcompress == 1)
  154. {
  155. cv::imencode(".jpg", mat1, buff, param);
  156. // cv::imencode(".jpg", matcompress, buff, param);
  157. pic.set_picdata(buff.data(),buff.size());
  158. buff.clear();
  159. pic.set_type(2);
  160. }
  161. else
  162. {
  163. pic.set_picdata(mat1.data,mat1.rows*mat1.cols*mat1.elemSize());
  164. // pic.set_picdata(matcompress.data,matcompress.rows*matcompress.cols*matcompress.elemSize());
  165. pic.set_type(1);
  166. }
  167. // qDebug("buff size is %d",buff.size());
  168. int nSize = pic.ByteSize();
  169. // char * strser = new char[nSize];
  170. bool bser = pic.SerializeToArray(strser,nSize);
  171. qDebug("time is %ld pac time is %d size is %d",QDateTime::currentMSecsSinceEpoch(),xg.elapsed(),nSize);
  172. // iv::modulecomm::ModuleSendMsg(gpa,str.data(),str.length());
  173. if(bser)iv::modulecomm::ModuleSendMsg(gpa,strser,nSize);
  174. else
  175. {
  176. std::cout<<"usbcamera "<< "serialize error. "<<std::endl;
  177. }
  178. // delete strser;
  179. // delete strout;
  180. // qDebug("%d have a pic. len is %d",xT.elapsed(),str.length());
  181. // qDebug("now is %d",xT.elapsed());
  182. // gw->mMutex.lock();
  183. // mat1.copyTo(gw->msrcImage);
  184. // gw->mMutex.unlock();
  185. // gMutexInput.lock();
  186. // mat1.copyTo(gsrcImage);
  187. // g_CapIndex++;
  188. // gMutexInput.unlock();
  189. }
  190. else
  191. {
  192. qDebug("no data.");
  193. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  194. }
  195. std::this_thread::sleep_for(std::chrono::milliseconds(time_sleep));
  196. }
  197. }
  198. #include <thread>
  199. void testthread()
  200. {
  201. void * mpx = iv::modulecomm::RegisterSend("test121",10000000,1);
  202. char * str = new char[10000000];
  203. while(1)
  204. {
  205. iv::modulecomm::ModuleSendMsg(mpx,str,9000000);
  206. }
  207. }
  208. int main(int argc, char *argv[])
  209. {
  210. cv::namedWindow("video_replay",cv::WINDOW_NORMAL);
  211. showversion("video_replay");
  212. QCoreApplication a(argc, argv);
  213. // std::thread * pthread = new std::thread(testthread);
  214. // return a.exec();
  215. QString strpath = QCoreApplication::applicationDirPath();
  216. // QString apppath = strpath;
  217. if(argc < 2)
  218. strpath = strpath + "/video_replay.xml";
  219. else
  220. strpath = argv[1];
  221. std::cout<<strpath.toStdString()<<std::endl;
  222. iv::xmlparam::Xmlparam xp(strpath.toStdString());
  223. std::string xmlmsgname = xp.GetParam("msgname","image00");
  224. gmsgname = xmlmsgname;
  225. std::string strwidth = xp.GetParam("width","1280");
  226. std::string strheight = xp.GetParam("height","720");
  227. std::string strcompress = xp.GetParam("bcompress","1");
  228. std::string strcompquality = xp.GetParam("compquality","95");
  229. std::string videoPath = xp.GetParam("video_path","/home/nvidia/Videos/video.mp4");
  230. std::string timeSleep = xp.GetParam("time_sleep","0");
  231. gcompquality = atoi(strcompquality.data());
  232. gwidth = atoi(strwidth.data());
  233. gheight = atoi(strheight.data());
  234. gcompress = atoi(strcompress.data());
  235. time_sleep = atoi(timeSleep.data());
  236. video_path = videoPath;
  237. std::cout<<"video_path:"<<video_path<<std::endl;
  238. gpa = iv::modulecomm::RegisterSend(gmsgname.data(),20000000,1);
  239. std::thread * mthread = new std::thread(VideoThread,0);
  240. return a.exec();
  241. }