查看: 18965|回复: 0

rk3399pro硬解码(vpu,rtsp)使用

[复制链接]

425

主题

252

回帖

2万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
20611
发表于 2020-10-16 10:35:03 | 显示全部楼层 |阅读模式
源文地址:https://www.jianshu.com/p/6e86980aa563
系统debian10,摄像头:海康;
鉴于瑞芯微官网论坛介绍硬解码使用不够详细,在踩了许多坑的情况下总结如下,供大家参考。
系统软件包升级
  • 更新源:sudo apt update --fix-missing
  • 升级软件包:sudo apt -y upgrade
    注意:升级过去中会有提示确认是否提供/etc/apt/sources.list.d/toybrick.list,请输入"Y"
  • 再次更新源:sudo apt update
说明:上述步骤只需要执行一次即可,后续软件包升级只需要执行命令
sudo apt update; sudo apt upgrade
参考:
瑞芯微社区关于toybrick系列debian10系统软件包升级说明
一、 python方式install toybrick-0.3.0-py3-none-any.whl   下载地址:链接: https://pan.baidu.com/s/1AkJ70nTTIIXbgDYbF6IPUQ 提取码: b54n
安装以下依赖

  1. sudo apt-get install g++ binutils-gold xorg-dev libglu1-mesa-dev  
  2. sudo apt install libgbm-dev  
  3. sudo apt install rockchip-mpp  
  4. sudo apt install toybrick-gbm-dev  
  5. sudo toybrick-mali.sh link   

  6. sudo pip3 install toybrick-0.3.0-py3-none-any.whl  
复制代码
验证安装是否成功

  1. import toybrick as toy
复制代码

python掉用代码
  1. import toybrick as toy
  2. import time
  3. import cv2

  4. url = "rtsp://admin:123456@192.168.1.200:554/h264/ch1/main/av_stream"
  5. username = "admin"
  6. pwd = "123456"
  7. #rtsp = toy.input.createRtspClient(url, username, pwd, False)
  8. rtsp = toy.input.createRtspClient(url)
  9. rtsp.connect()
  10. last = time.time()
  11. gl = toy.output.createGLDrmDisplay(toy.DisplayPort.HDMI_A)
  12. idx0 = gl.add_view(50, 600, 768, 432)
  13. frame_index = 0
  14. while rtsp.is_opened():
  15.     frame = rtsp.read_rgb(768, 432)
  16.     now = time.time()
  17.     gl.show(idx0, frame)
  18.     print (frame_index, "----------------------------",now - last)
  19.     frame = frame.array()
  20.     cv2.imwrite("images/4_" + str(frame_index) + ".jpg", frame)
  21.     # cv2.imshow('Carplate demo', cv2.resize(frame, (960, 540)))  #
  22.     frame_index += 1
  23.     last = now
复制代码
运行效果


参考:
瑞芯微社区RK3399Pro入门教程(8)6路1080P30帧解码显示范例
二.、C++方式
安装依赖
  1. sudo apt-get install curl  
  2. sudo apt-get install libcurl4-openssl-dev  
  3. sudo apt-get install libopencv-dev  
  4. sudo apt install rockchip-drm-dev libdrm-dev  
  5. sudo apt install rockchip-rtsp-dev  
  6. sudo apt install rockchip-mpp-dev  
  7. sudo apt install rockchip-rga-dev  
复制代码
rtsp_ssd .cpp
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <iostream>
  4. #include <string.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <fcntl.h>
  8. #include <thread>
  9. #include <memory.h>
  10. #include <sys/time.h>
  11. #include <queue>

  12. using namespace std;

  13. #include "opencv2/core/core.hpp"
  14. #include "opencv2/highgui/highgui.hpp"

  15. #include <rockchip/rockchip_rtsp.h>
  16. #include <rockchip/rockchip_mpp.h>
  17. extern "C" {
  18. #include <rockchip/rockchip_rga.h>
  19. }

  20. #define RTSP_URL    "rtsp://192.168.1.200/h264/ch1/main/av_stream"
  21. #define RTSP_USER   "admin"
  22. #define RTSP_PWD    "123456"

  23. static MppDecoder *mpp_dec = NULL;
  24. static std::queue<DecFrame *> frame_queue;
  25. static int run_flag = 0;

  26. unsigned long get_time(void)
  27. {
  28.     struct timeval ts;
  29.     gettimeofday(&ts, NULL);
  30.     return (ts.tv_sec * 1000 + ts.tv_usec / 1000);
  31. }

  32. void onRtspHandle(unsigned char *buf, size_t len)
  33. {
  34.     std::cout << "frame recived " << len << std::endl;

  35.     mpp_dec->ops->enqueue(mpp_dec, buf, len);
  36. }

  37. void inference_thread(RockchipRga *rga, int width, int height)
  38. {
  39.     int ret;
  40.     int resize_w = 1920, resize_h = 1080;
  41.     static int frame_size = 0;
  42.     unsigned char *frame_rgb = NULL;

  43.     rga->ops->initCtx(rga);
  44.     rga->ops->setRotate(rga, RGA_ROTATE_NONE);
  45.     rga->ops->setSrcFormat(rga, V4L2_PIX_FMT_NV12, width, height);
  46.     rga->ops->setDstFormat(rga, V4L2_PIX_FMT_BGR24, resize_w, resize_h);
  47.    
  48.     frame_size = resize_w * resize_h * 3;
  49.     frame_rgb = (unsigned char *)malloc(frame_size);
  50.     cv::Mat img(resize_h , resize_w , CV_8UC3, frame_rgb);
  51.     if (!frame_rgb)
  52.         goto exit;
  53.    
  54.     rga->ops->setDstBufferPtr(rga, frame_rgb);

  55.     while (run_flag) {
  56.         if (frame_queue.empty()) {
  57.             usleep(1000);
  58.             continue;
  59.         }

  60.         auto frame = frame_queue.front();

  61.         rga->ops->setSrcBufferPtr(rga, frame->data);

  62.         ret = rga->ops->go(rga);
  63.         printf("inference_thread ................\n");
  64.         if (!ret) {
  65.             ///do something with frame_rgb

  66.             cv::imshow("test", img);
  67.             cv::waitKey(10);
  68.         }

  69.         frame_queue.pop();
  70.         mpp_dec->ops->freeFrame(frame);
  71.     }

  72. exit:
  73.     run_flag = 0;

  74.     while (!frame_queue.empty()) {
  75.         auto frame = frame_queue.front();
  76.         mpp_dec->ops->freeFrame(frame);
  77.         frame_queue.pop();
  78.     }

  79.     if (frame_rgb)
  80.         free(frame_rgb);
  81. }

  82. void decode_thread(RockchipRga *rga)
  83. {
  84.     int ret;
  85.     int first_frame = 0;
  86.     std::thread t_inference;

  87.     while (run_flag) {
  88.         DecFrame *frame = mpp_dec->ops->dequeue_timeout(mpp_dec, 300);
  89.         if (frame != NULL) {
  90.             std::cout << "decode frame" << frame->width << "x" << frame->height << std::endl;
  91.         
  92.             if (!first_frame) {
  93.                 std::cout << "first_frame" << std::endl;
  94.                 t_inference = std::thread(inference_thread, rga, frame->width, frame->height);
  95.                 first_frame = 1;
  96.             }

  97.             if (frame_queue.size() < 30)
  98.                 frame_queue.push(frame);
  99.             else
  100.                 mpp_dec->ops->freeFrame(frame);
  101.         }
  102.     }

  103. exit:
  104.     run_flag = 0;

  105.     t_inference.join();

  106.     while (!frame_queue.empty()) {
  107.         auto frame = frame_queue.front();
  108.         mpp_dec->ops->freeFrame(frame);
  109.         frame_queue.pop();
  110.     }

  111. }

  112. int main(int argc, char **argv)
  113. {
  114.     int ret;
  115.     RockchipRga *rga;
  116.     unsigned char ready[5] = {'r', 'e', 'a', 'd', 'y'};
  117.     RtspClient rtsp_client(RTSP_URL, RTSP_USER, RTSP_PWD);

  118.     rtsp_client.setDataCallback(onRtspHandle);

  119.     mpp_dec = MppDecoderCreate(DECODE_TYPE_H264);
  120.     if (!mpp_dec) {
  121.         std::cout << "MppDecoderCreate error!\n" << std::endl;
  122.         return -1;
  123.     }

  124.     rga = RgaCreate();
  125.     if (!rga) {
  126.         MppDecoderDestroy(mpp_dec);
  127.         std::cout << "rgaCreate error!\n" << std::endl;
  128.         return -1;
  129.     }

  130.     run_flag = 1;

  131.     rtsp_client.enable();

  132.     std::thread t_decode(decode_thread, rga);

  133.     while (run_flag) {
  134.         usleep(10000);
  135.     }

  136.     rtsp_client.disable();

  137.     run_flag = 0;

  138.     t_decode.join();

  139.     RgaDestroy(rga);

  140.     MppDecoderDestroy(mpp_dec);

  141.     return 0;
  142. }
复制代码
CMakeLists.txt
  1. cmake_minimum_required(VERSION 2.8)

  2. set(CMAKE_SYSTEM_NAME Linux)
  3. set(CMAKE_BUILD_TYPE Release)

  4. set(CMAKE_C_COMPILER gcc)
  5. set(CMAKE_CXX_COMPILER g++)

  6. find_package(CURL REQUIRED)

  7. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
  8. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

  9. set(link_libs rockchip_rtsp rockchip_mpp curl rockchip_rga pthread
  10. opencv_core opencv_highgui opencv_imgcodecs)

  11. add_executable(rtsp_ssd rtsp_ssd.cpp)
  12. target_link_libraries(rtsp_ssd ${link_libs})
复制代码
编译
  1. cd local_rtsp/build  
  2. cmake ..  
  3. make  
  4. ./rtsp_ssd  
复制代码
运行效果

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|风火轮WIKI|手机版|小黑屋|深圳风火轮团队 ( 粤ICP备17095099号 )

GMT+8, 2024-11-21 23:54 , Processed in 0.065827 second(s), 19 queries .

快速回复 返回顶部 返回列表
 
【客服1】 商务合作 15289193
【客服2】 业务洽谈 13257599
【客服3】 售前咨询 510313198
【邮箱】
smartfire@smartfire.cn