|
板凳
楼主 |
发表于 2020-12-22 16:14:05
|
只看该作者
代码如下:
- #include <stdio.h>
- #include <unistd.h>
- #include <iostream>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <thread>
- #include <memory.h>
- #include <sys/time.h>
- #include <queue>
- using namespace std;
- #include "opencv2/core/core.hpp"
- #include "opencv2/highgui/highgui.hpp"
- #include <rockchip/rockchip_rtsp.h>
- #include <rockchip/rockchip_mpp.h>
- extern "C" {
- #include <rockchip/rockchip_rga.h>
- }
- #define RTSP_URL "rtsp://192.168.1.85:8554/4.mkv"
- #define RTSP_USER ""
- #define RTSP_PWD ""
- static MppDecoder *mpp_dec = NULL;
- static std::queue<DecFrame *> frame_queue;
- static int run_flag = 0;
- int cnt=0;
- unsigned long get_time(void)
- {
- struct timeval ts;
- gettimeofday(&ts, NULL);
- return (ts.tv_sec * 1000 + ts.tv_usec / 1000);
- }
- void onRtspHandle(unsigned char *buf, size_t len)
- {
- std::cout << "frame recived " << len << std::endl;
- mpp_dec->ops->enqueue(mpp_dec, buf, len);
- }
- void inference_thread(RockchipRga *rga, int width, int height)
- {
- int ret;
- int resize_w = 640, resize_h = 480;
- static int frame_size = 0;
- unsigned char *frame_rgb = NULL;
- rga->ops->initCtx(rga);
- rga->ops->setRotate(rga, RGA_ROTATE_NONE);
- rga->ops->setSrcFormat(rga, V4L2_PIX_FMT_NV12, width, height);
- rga->ops->setDstFormat(rga, V4L2_PIX_FMT_RGB24, resize_w, resize_h);
-
- frame_size = resize_w * resize_h * 3;
- frame_rgb = (unsigned char *)malloc(frame_size);
- cv::Mat img(resize_h , resize_w , CV_8UC3, frame_rgb);
- if (!frame_rgb)
- goto exit;
-
- rga->ops->setDstBufferPtr(rga, frame_rgb);
- while (run_flag)
- {
- if (frame_queue.empty())
- {
- usleep(1000);
- continue;
- }
- auto frame = frame_queue.front();
- rga->ops->setSrcBufferPtr(rga, frame->data);
- ret = rga->ops->go(rga);
- printf("inference_thread ................\n");
- if (!ret) {
- ///do something with frame_rgb
- std::string jpg="./image/frame_"+std::to_string(cnt)+".jpg";
- cv::imwrite(jpg,img);
- //cv::imshow("test", img);
- std::cout<<"image width="<<img.cols<<std::endl;
- std::cout<<"image height="<<img.rows<<std::endl;
- cv::waitKey(30);
- }
- frame_queue.pop();
- mpp_dec->ops->freeFrame(frame);
- }
- exit:
- run_flag = 0;
- while (!frame_queue.empty()) {
- auto frame = frame_queue.front();
- mpp_dec->ops->freeFrame(frame);
- frame_queue.pop();
- }
- if (frame_rgb)
- free(frame_rgb);
- }
- void decode_thread(RockchipRga *rga)
- {
- int ret;
- int first_frame = 0;
- std::thread t_inference;
- while (run_flag)
- {
- //std::cout<<"$$$$$$$$$$$"<<std::endl;
- DecFrame *frame = mpp_dec->ops->dequeue_timeout(mpp_dec, 300);
- if (frame != NULL)
- {
- std::cout << "decode frame" << frame->width << "x" << frame->height << std::endl;
-
- if (!first_frame)
- {
- std::cout << "first_frame" << std::endl;
- t_inference = std::thread(inference_thread, rga, frame->width, frame->height);
- first_frame = 1;
- }
- if (frame_queue.size() < 30)
- frame_queue.push(frame);
- else
- mpp_dec->ops->freeFrame(frame);
- }
- // else
- // {
- // std::cout<<"frame=NULL"<<std::endl;
- // }
-
- }
- exit:
- run_flag = 0;
- t_inference.join();
- while (!frame_queue.empty()) {
- auto frame = frame_queue.front();
- mpp_dec->ops->freeFrame(frame);
- frame_queue.pop();
- }
- }
- int main(int argc, char **argv)
- {
- int ret;
- RockchipRga *rga;
- unsigned char ready[5] = {'r', 'e', 'a', 'd', 'y'};
- RtspClient rtsp_client(RTSP_URL, RTSP_USER, RTSP_PWD);
- rtsp_client.setDataCallback(onRtspHandle);
- mpp_dec = MppDecoderCreate(DECODE_TYPE_H264);
- if (!mpp_dec)
- {
- std::cout << "MppDecoderCreate error!\n" << std::endl;
- return -1;
- }
- rga = RgaCreate();
- if (!rga)
- {
- MppDecoderDestroy(mpp_dec);
- std::cout << "rgaCreate error!\n" << std::endl;
- return -1;
- }
- run_flag = 1;
- rtsp_client.enable();
- std::thread t_decode(decode_thread, rga);
- while (run_flag) {
- usleep(10000);
- }
- rtsp_client.disable();
- run_flag = 0;
- t_decode.join();
- RgaDestroy(rga);
- MppDecoderDestroy(mpp_dec);
- return 0;
- }
[url=链接:https://pan.baidu.com/s/1ZfNvL93sQT5RlYhQGNp6Ig 提取码:5one ]4.mp4的视频链接[/url] |
|