Skip to content

Rockchip flowunit #345

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 30 additions & 17 deletions CMake/FindROCKCHIP.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,47 @@ if(DEFINED ENV{ROCKCHIP_PATH})

message(STATUS "DEFINED ${HINTS_ROCKCHIP_PATH}")
else()
set(HINTS_ROCKCHIP_PATH "/opt/rockchip")
set(HINTS_ROCKCHIP_PATH "/usr/local/rockchip")

message(STATUS "set default search path: /opt/rockchip")
message(STATUS "set default search path: /usr/local/rockchip")
endif()

find_path(ROCKCHIP_RGA_INCLUDE NAMES im2d.h rga.h
HINTS ${HINTS_ROCKCHIP_PATH}/rk-rga/include)
HINTS ${HINTS_ROCKCHIP_PATH}/rga/include)
mark_as_advanced(ROCKCHIP_RGA_INCLUDE)

find_path(ROCKCHIP_MPP_INCLUDE NAMES rk_mpi.h rk_type.h
HINTS ${HINTS_ROCKCHIP_PATH}/rkmpp/include/rockchip)
HINTS ${HINTS_ROCKCHIP_PATH}/rkmpp/include)
mark_as_advanced(ROCKCHIP_MPP_INCLUDE)

find_path(RKNN_INCLUDE NAMES rknn_api.h
HINTS ${HINTS_ROCKCHIP_PATH}/rknn/include)
mark_as_advanced(RKNN_INCLUDE)
find_path(RKNPU_INCLUDE NAMES rknn_api.h
HINTS ${HINTS_ROCKCHIP_PATH}/rknpu/rknn-api/librknn_api/include)
mark_as_advanced(RKNPU_INCLUDE)

find_path(RKNPU2_INCLUDE NAMES rknn_api.h
HINTS ${HINTS_ROCKCHIP_PATH}/rknnrt/include)
mark_as_advanced(RKNPU2_INCLUDE)
find_library(RKNPU_LIBRARY NAMES rknn_api HINTS ${HINTS_ROCKCHIP_PATH}/rknpu/rknn-api/librknn_api/Linux/lib64/)
mark_as_advanced(RKNPU_LIBRARY)

find_library(RKNN_LIBRARY NAMES rknn_api HINTS ${HINTS_ROCKCHIP_PATH}/rknn/lib)
mark_as_advanced(RKNN_LIBRARY)
if(EXISTS ${HINTS_ROCKCHIP_PATH}/rknpu2/runtime/RK3588)
find_path(RKNPU2_INCLUDE NAMES rknn_api.h
HINTS ${HINTS_ROCKCHIP_PATH}/rknpu2/runtime/RK3588/Linux/librknn_api/include)
mark_as_advanced(RKNPU2_INCLUDE)

find_library(RKNPU2_LIBRARY NAMES rknnrt HINTS ${HINTS_ROCKCHIP_PATH}/rknnrt/lib)
mark_as_advanced(RKNPU2_LIBRARY)
find_library(RKNPU2_LIBRARY NAMES rknnrt HINTS ${HINTS_ROCKCHIP_PATH}/rknpu2/runtime/RK3588/Linux/librknn_api/aarch64)
mark_as_advanced(RKNPU2_LIBRARY)
message(STATUS "rk3588 platform")
elseif(EXISTS ${HINTS_ROCKCHIP_PATH}/rknpu2/runtime/RK356X)
find_path(RKNPU2_INCLUDE NAMES rknn_api.h
HINTS ${HINTS_ROCKCHIP_PATH}/rknpu2/runtime/RK356X/Linux/librknn_api/include)
mark_as_advanced(RKNPU2_INCLUDE)

find_library(RKRGA_LIBRARY NAMES rga HINTS ${HINTS_ROCKCHIP_PATH}/rk-rga/lib)
find_library(RKNPU2_LIBRARY NAMES rknnrt HINTS ${HINTS_ROCKCHIP_PATH}/rknpu2/runtime/RK356X/Linux/librknn_api/aarch64)
mark_as_advanced(RKNPU2_LIBRARY)
message(STATUS "rk356x platform")
else()
message(STATUS "other platform")
endif()

find_library(RKRGA_LIBRARY NAMES rga HINTS ${HINTS_ROCKCHIP_PATH}/rga/libs/Linux/gcc-aarch64)
mark_as_advanced(RKRGA_LIBRARY)

find_library(RKMPP_LIBRARY NAMES rockchip_mpp HINTS ${HINTS_ROCKCHIP_PATH}/rkmpp/lib)
Expand All @@ -42,8 +55,8 @@ FIND_PACKAGE_HANDLE_STANDARD_ARGS(ROCKCHIP
VERSION_VAR ROCKCHIP_VERSION_STRING)

if(ROCKCHIP_FOUND)
set(RKNN_LIBRARIES ${RKNN_LIBRARY})
set(RKNN_INCLUDE_DIR ${RKNN_INCLUDE})
set(RKNPU_LIBRARIES ${RKNPU_LIBRARY})
set(RKNPU_INCLUDE_DIR ${RKNPU_INCLUDE})
set(RKNPU2_INCLUDE_DIR ${RKNPU2_INCLUDE})
set(RKNPU2_LIBRARIES ${RKNPU2_LIBRARY})
set(RKRGA_LIBRARIES ${RKRGA_LIBRARY})
Expand Down
1 change: 0 additions & 1 deletion docker/Dockerfile.rknnrt.build.ubuntu
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ RUN (cd /lib/systemd/system/sysinit.target.wants/; for i in *; \
systemctl enable ssh

RUN echo "/usr/local/rockchip/rga/libs/Linux/gcc-aarch64" >> /etc/ld.so.conf.d/rockchip.conf && \
echo "/usr/local/rockchip/rknpu/rknn/rknn_api/librknn_api/lib64" >> /etc/ld.so.conf.d/rockchip.conf && \
if [ -d /usr/local/rockchip/rknpu2/runtime/RK356X ]; then echo "/usr/local/rockchip/rknpu2/runtime/RK356X/Linux/librknn_api/aarch64" >> /etc/ld.so.conf.d/rockchip.conf; elif [ -d /usr/local/rockchip/rknpu2/runtime/RK3588 ]; then echo "/usr/local/rockchip/rknpu2/runtime/RK3588/Linux/librknn_api/aarch64" >> /etc/ld.so.conf.d/rockchip.conf; fi && \
echo "/usr/local/rockchip/rkmpp/lib" >> /etc/ld.so.conf.d/rockchip.conf

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ modelbox::Status FfmpegVideoMuxer::Init(
const std::shared_ptr<FfmpegWriter> &writer) {
destination_url_ = writer->GetDestinationURL();
format_ctx_ = writer->GetCtx();

if (format_ctx_ == nullptr) {
const auto *msg = "FfmpegVideoMuxer.Init fail format ctx is nullptr";
MBLOG_ERROR << msg;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,4 @@ modelbox::Status FfmpegWriter::Open(const std::string &format_name,

std::string FfmpegWriter::GetFormatName() { return format_name_; }
std::string FfmpegWriter::GetDestinationURL() { return destination_url_; }
std::shared_ptr<AVFormatContext> FfmpegWriter::GetCtx() { return format_ctx_; }
std::shared_ptr<AVFormatContext> FfmpegWriter::GetCtx() { return format_ctx_; }
2 changes: 1 addition & 1 deletion src/drivers/devices/rockchip/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ include_directories(${LIBMODELBOX_INCLUDE})
include_directories(${LIBMODELBOX_BASE_INCLUDE})
include_directories(${LIBMODELBOX_DEVICE_ROCKCHIP_INCLUDE})
include_directories(${ROCKCHIP_MPP_INCLUDE})
include_directories(${RKNN_INCLUDE_DIR})
include_directories(${RKNPU_INCLUDE_DIR})
include_directories(${ROCKCHIP_RGA_INCLUDE})

set(HEADER
Expand Down
10 changes: 10 additions & 0 deletions src/drivers/devices/rockchip/core/rockchip_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,16 @@ Status CopyNVMemory(uint8_t *psrc, uint8_t *pdst, int w, int h, int ws,

Status CopyRGBMemory(uint8_t *psrc, uint8_t *pdst, int w, int h, int ws,
int hs) {
if (psrc == nullptr) {
MBLOG_ERROR << "psrc is nullptr";
return {STATUS_FAULT, "psrc is nullptr"};
}

if (pdst == nullptr) {
MBLOG_ERROR << "pdst is nullptr";
return {STATUS_FAULT, "pdst is nullptr"};
}

uint8_t *rgbsrc = psrc;
uint8_t *rgbdst = pdst;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@ MODELBOX_FLOWUNIT(RockchipCropFlowUnit, rk_crop_desc) {
rk_crop_desc.AddFlowUnitInput({IN_REGION, "cpu"});
rk_crop_desc.AddFlowUnitOutput({OUT_IMG, modelbox::DEVICE_TYPE});
rk_crop_desc.SetFlowType(modelbox::NORMAL);
rk_crop_desc.SetInputContiguous(false);
rk_crop_desc.SetDescription(FLOWUNIT_DESC);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,6 @@ MODELBOX_FLOWUNIT(ImageDecoderFlowUnit, rk_imgdec_desc) {
"the output pixel format"));

rk_imgdec_desc.SetFlowType(modelbox::NORMAL);
rk_imgdec_desc.SetInputContiguous(false);
rk_imgdec_desc.SetDescription(FLOWUNIT_DESC);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ modelbox::Status modelbox::RKNPU2Inference::Init(
return {modelbox::STATUS_FAULT, "query input_output error"};
}

MBLOG_INFO << "model input num: " << rknpu2_io_num.n_input
<< ", output num: " << rknpu2_io_num.n_output << std::endl;

if (npu2model_input_list_.size() != rknpu2_io_num.n_input ||
npu2model_output_list_.size() != rknpu2_io_num.n_output) {
MBLOG_ERROR << "model input output num mismatch: input num in graph is "
Expand Down Expand Up @@ -285,9 +288,10 @@ size_t modelbox::RKNPU2Inference::GetInputBuffer(
input_params->in_width_ = in_image->GetBytes();
}

if (input_buf_list->GetDevice()->GetType() == "rknpu") {
if (input_buf_list->GetDevice()->GetType() == "rockchip") {
return CopyFromAlignMemory(input_buf_list, input_buf, input_params);
}

input_buf.reset((uint8_t *)input_buf_list->ConstData(), [](uint8_t *p) {});
return input_buf_list->GetBytes();
}
Expand Down Expand Up @@ -326,6 +330,7 @@ modelbox::Status modelbox::RKNPU2Inference::Infer(
<< " " << inputs_size_[i];
return modelbox::STATUS_FAULT;
}

one_input.pass_through = false;
one_input.type = (rknn_tensor_type)inputs_type_[i];
one_input.fmt = RKNN_TENSOR_NHWC;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

#include "rknpu2_inference.h"

constexpr const char *RKNPU2_FLOWUNIT_TYPE = "rknpu2";
constexpr const char *RKNPU2_FLOWUNIT_TYPE = "rockchip";
constexpr const char *RKNPU2_INFERENCE_TYPE = "rknpu2";

class RKNPU2InferenceFlowUnit : public modelbox::FlowUnit {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ MODELBOX_FLOWUNIT(ResizeFlowUnit, rk_resize_desc) {
rk_resize_desc.AddFlowUnitInput({IN_IMG});
rk_resize_desc.AddFlowUnitOutput({OUT_IMG});
rk_resize_desc.SetFlowType(modelbox::NORMAL);
rk_resize_desc.SetInputContiguous(false);
rk_resize_desc.SetDescription(FLOWUNIT_DESC);
rk_resize_desc.AddFlowUnitOption(modelbox::FlowUnitOption(
"image_width", "int", true, "0", "the resize width"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ constexpr const char *FLOWUNIT_DESC =
"\t@Constraint: The field value range of this flowunit supports: "
"'pix_fmt': "
"[rgb_packed,bgr_packed], 'layout': [hwc]. ";

constexpr const char *IN_IMG = "in_image";
constexpr const char *OUT_IMG = "out_image";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ MODELBOX_FLOWUNIT(VideoDecoderFlowUnit, desc) {
desc.AddFlowUnitInput({VIDEO_PACKET_INPUT, "cpu"});
desc.AddFlowUnitOutput({FRAME_INFO_OUTPUT, modelbox::DEVICE_TYPE});
desc.SetFlowType(modelbox::STREAM);
desc.SetInputContiguous(false);
desc.SetResourceNice(false);
desc.SetDescription(FLOWUNIT_DESC);
desc.AddFlowUnitOption(modelbox::FlowUnitOption(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "rga.h"

constexpr const char *FLOWUNIT_NAME = "video_decoder";

constexpr const char *FLOWUNIT_DESC =
"\n\t@Brief: A video decoder flowunit on rockchip. \n"
"\t@Port parameter: The input port buffer type is video_packet, the output "
Expand Down Expand Up @@ -64,6 +65,7 @@ constexpr const char *FLOWUNIT_DESC =
"\t@Constraint: The flowuint 'video_decoder' must be used pair "
"with 'video_demuxer. the output buffer meta fields 'pix_fmt' is "
"'brg_packed' or 'rgb_packed', 'layout' is 'hcw'.";

constexpr const char *VIDEO_PACKET_INPUT = "in_video_packet";
constexpr const char *FRAME_INFO_OUTPUT = "out_video_frame";
constexpr const char *CODEC_META = "codec_meta";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ std::shared_ptr<AVPacket> FfmpegVideoEncoder::NewPacket(MppPacket &packet) {
size_t len = mpp_packet_get_length(packet);

auto *av_packet_ptr = av_packet_alloc();

Defer { mpp_packet_deinit(&packet); };
if (av_packet_ptr == nullptr) {
MBLOG_ERROR << "av packet alloc failed";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,25 +317,37 @@ modelbox::Status VideoOutFlowUnit::GetDestUrl(
const std::shared_ptr<modelbox::DataContext> &data_ctx,
std::string &dest_url) {
dest_url = default_dest_url_;

Defer {
std::regex url_auth_pattern("://[^ /]*?:[^ /]*?@");
auto result = std::regex_replace(dest_url, url_auth_pattern, "://*:*@");
MBLOG_INFO << "video_out url is " << result;
};

if (data_ctx == nullptr) {
MBLOG_ERROR << "data ctx is nullptr";
return modelbox::STATUS_INVALID;
}

// 3种方式获取
auto stream_meta = data_ctx->GetInputMeta(FRAME_INFO_INPUT);
if (stream_meta != nullptr) {
auto meta_dest_url = stream_meta->GetMeta(DEST_URL);
auto dest_url_ptr =
std::static_pointer_cast<std::string>(stream_meta->GetMeta(DEST_URL));
meta_dest_url == nullptr
? nullptr
: std::static_pointer_cast<std::string>(meta_dest_url);
if (dest_url_ptr != nullptr && !(*dest_url_ptr).empty()) {
dest_url = *dest_url_ptr;
return modelbox::STATUS_SUCCESS;
}
}

auto config = data_ctx->GetSessionConfig();
if (config == nullptr) {
MBLOG_ERROR << "data ctx session config is empty";
return modelbox::STATUS_INVALID;
}

auto cfg_str = config->GetString("iva_task_output");
if (cfg_str.empty()) {
return modelbox::STATUS_SUCCESS;
Expand Down Expand Up @@ -368,7 +380,6 @@ MODELBOX_FLOWUNIT(VideoOutFlowUnit, desc) {
desc.SetFlowUnitGroupType("Video");
desc.AddFlowUnitInput({FRAME_INFO_INPUT, "cpu"});
desc.SetFlowType(modelbox::STREAM);
desc.SetInputContiguous(false);
// 禁止异步执行,编码必须一帧帧的编码
desc.SetResourceNice(false);
desc.SetDescription(FLOWUNIT_DESC);
Expand Down