Skip to content

Commit 2f2bf49

Browse files
authored
rockchip flowunit code (#323)
* rockchip flowunit code
1 parent b31c10e commit 2f2bf49

34 files changed

+4782
-3
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* Copyright 2022 The Modelbox Project Authors. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "ffmpeg_video_muxer.h"
18+
19+
#include <modelbox/base/log.h>
20+
21+
#include "video_decode_common.h"
22+
23+
modelbox::Status FfmpegVideoMuxer::Init(
24+
const std::shared_ptr<AVCodecContext> &codec_ctx,
25+
const std::shared_ptr<FfmpegWriter> &writer) {
26+
destination_url_ = writer->GetDestinationURL();
27+
format_ctx_ = writer->GetCtx();
28+
if (format_ctx_ == nullptr) {
29+
const auto *msg = "FfmpegVideoMuxer.Init fail format ctx is nullptr";
30+
MBLOG_ERROR << msg;
31+
return {modelbox::STATUS_FAULT, msg};
32+
}
33+
34+
auto ret = SetupStreamParam(codec_ctx);
35+
if (ret != modelbox::STATUS_SUCCESS) {
36+
auto msg =
37+
"FfmpegVideoMuxer.Init SetupStreamParam fail reason: " + ret.Errormsg();
38+
MBLOG_ERROR << msg;
39+
return ret;
40+
}
41+
42+
return modelbox::STATUS_SUCCESS;
43+
}
44+
45+
modelbox::Status FfmpegVideoMuxer::SetupStreamParam(
46+
const std::shared_ptr<AVCodecContext> &codec_ctx) {
47+
stream_ = avformat_new_stream(format_ctx_.get(), codec_ctx->codec);
48+
if (stream_ == nullptr) {
49+
MBLOG_ERROR << "Create video stream failed";
50+
return {modelbox::STATUS_FAULT, "Create video stream failed"};
51+
}
52+
53+
stream_->time_base = codec_ctx->time_base;
54+
auto ret =
55+
avcodec_parameters_from_context(stream_->codecpar, codec_ctx.get());
56+
if (ret < 0) {
57+
GET_FFMPEG_ERR(ret, ffmpeg_err);
58+
MBLOG_ERROR << "avcodec_parameters_from_context err " << ffmpeg_err;
59+
return {modelbox::STATUS_FAULT,
60+
"avcodec_parameters_from_context err " + std::string(ffmpeg_err)};
61+
}
62+
63+
return modelbox::STATUS_SUCCESS;
64+
}
65+
66+
modelbox::Status FfmpegVideoMuxer::Mux(
67+
const AVRational &time_base, const std::shared_ptr<AVPacket> &av_packet) {
68+
av_packet_rescale_ts(av_packet.get(), time_base, stream_->time_base);
69+
av_packet->stream_index = stream_->index;
70+
if (!is_header_wrote_) {
71+
AVDictionary *format_opts = nullptr;
72+
av_dict_set(&format_opts, "rtsp_transport", "tcp", 0);
73+
auto ret = avformat_write_header(format_ctx_.get(), &format_opts);
74+
av_dict_free(&format_opts);
75+
if (ret < 0) {
76+
GET_FFMPEG_ERR(ret, ffmpeg_err);
77+
MBLOG_ERROR << "avformat_write_header failed, ret " << ffmpeg_err;
78+
return {modelbox::STATUS_FAULT,
79+
"avformat_write_header failed, ret " + std::string(ffmpeg_err)};
80+
}
81+
82+
is_header_wrote_ = true;
83+
}
84+
85+
auto ret = av_interleaved_write_frame(format_ctx_.get(), av_packet.get());
86+
if (ret < 0) {
87+
GET_FFMPEG_ERR(ret, ffmpeg_err);
88+
MBLOG_ERROR << "av_write_frame failed, ret " << ffmpeg_err;
89+
return {modelbox::STATUS_FAULT,
90+
"av_write_frame failed, ret " + std::string(ffmpeg_err)};
91+
}
92+
93+
return modelbox::STATUS_SUCCESS;
94+
}
95+
96+
FfmpegVideoMuxer::~FfmpegVideoMuxer() {
97+
if (is_header_wrote_) {
98+
auto ret = av_write_trailer(format_ctx_.get());
99+
if (ret < 0) {
100+
GET_FFMPEG_ERR(ret, ffmpeg_err);
101+
MBLOG_ERROR << "av_write_trailer failed, ret " << ffmpeg_err;
102+
}
103+
}
104+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2022 The Modelbox Project Authors. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef MODELBOX_FLOWUNIT_FFMPEG_MUXER_H_
18+
#define MODELBOX_FLOWUNIT_FFMPEG_MUXER_H_
19+
20+
#include <modelbox/base/status.h>
21+
22+
#include <memory>
23+
#include <string>
24+
25+
#include "ffmpeg_writer.h"
26+
27+
class FfmpegVideoMuxer {
28+
public:
29+
modelbox::Status Init(const std::shared_ptr<AVCodecContext> &codec_ctx,
30+
const std::shared_ptr<FfmpegWriter> &writer);
31+
32+
modelbox::Status Mux(const AVRational &time_base,
33+
const std::shared_ptr<AVPacket> &av_packet);
34+
35+
virtual ~FfmpegVideoMuxer();
36+
37+
private:
38+
modelbox::Status SetupStreamParam(
39+
const std::shared_ptr<AVCodecContext> &codec_ctx);
40+
41+
std::shared_ptr<AVFormatContext> format_ctx_;
42+
std::string destination_url_;
43+
AVStream *stream_{nullptr};
44+
bool is_header_wrote_{false};
45+
};
46+
47+
#endif // MODELBOX_FLOWUNIT_FFMPEG_MUXER_H_
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright 2022 The Modelbox Project Authors. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "ffmpeg_writer.h"
18+
19+
#include <modelbox/base/log.h>
20+
21+
#include "video_decode_common.h"
22+
23+
extern "C" {
24+
#include <libavutil/opt.h>
25+
}
26+
27+
modelbox::Status FfmpegWriter::Open(const std::string &format_name,
28+
const std::string &destination_url) {
29+
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 9, 100)
30+
av_register_all();
31+
#endif
32+
auto ret = avformat_network_init();
33+
if (ret < 0) {
34+
GET_FFMPEG_ERR(ret, ffmpeg_err);
35+
MBLOG_ERROR << "avformat_network_init, err " << ffmpeg_err;
36+
return {modelbox::STATUS_FAULT,
37+
"avformat_network_init, err " + std::string(ffmpeg_err)};
38+
}
39+
40+
format_name_ = format_name;
41+
destination_url_ = destination_url;
42+
43+
AVFormatContext *format_ctx = nullptr;
44+
ret = avformat_alloc_output_context2(
45+
&format_ctx, nullptr, format_name.c_str(), destination_url.c_str());
46+
if (ret < 0 || format_ctx == nullptr) {
47+
GET_FFMPEG_ERR(ret, ffmpeg_err);
48+
auto msg = "avformat_alloc_output_context2 failed, format " + format_name +
49+
", dest_url " + destination_url + ", ret " +
50+
std::string(ffmpeg_err);
51+
MBLOG_ERROR << msg;
52+
return {modelbox::STATUS_FAULT, msg};
53+
}
54+
55+
format_ctx_.reset(format_ctx,
56+
[](AVFormatContext *ctx) { avformat_free_context(ctx); });
57+
if (format_name_ != "rtsp") {
58+
ret = avio_open2(&format_ctx_->pb, destination_url.c_str(), AVIO_FLAG_WRITE,
59+
nullptr, nullptr);
60+
if (ret < 0) {
61+
GET_FFMPEG_ERR(ret, ffmpeg_err);
62+
auto msg = "avio_open2 failed, url " + destination_url + ", format " +
63+
format_name + ", ret " + std::string(ffmpeg_err);
64+
MBLOG_ERROR << msg;
65+
return {modelbox::STATUS_FAULT, msg};
66+
}
67+
}
68+
69+
MBLOG_INFO << "Open url " << destination_url << ", format " << format_name
70+
<< " success";
71+
return modelbox::STATUS_SUCCESS;
72+
}
73+
74+
std::string FfmpegWriter::GetFormatName() { return format_name_; }
75+
std::string FfmpegWriter::GetDestinationURL() { return destination_url_; }
76+
std::shared_ptr<AVFormatContext> FfmpegWriter::GetCtx() { return format_ctx_; }
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2022 The Modelbox Project Authors. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef MODELBOX_FLOWUNIT_FFMPEG_WRITER_H_
18+
#define MODELBOX_FLOWUNIT_FFMPEG_WRITER_H_
19+
20+
#include <modelbox/base/status.h>
21+
22+
#include <memory>
23+
#include <string>
24+
25+
extern "C" {
26+
#include <libavformat/avformat.h>
27+
}
28+
29+
class FfmpegWriter {
30+
public:
31+
modelbox::Status Open(const std::string &format_name,
32+
const std::string &destination_url);
33+
34+
std::string GetFormatName();
35+
36+
std::string GetDestinationURL();
37+
38+
std::shared_ptr<AVFormatContext> GetCtx();
39+
40+
private:
41+
std::string format_name_;
42+
std::string destination_url_;
43+
std::shared_ptr<AVFormatContext> format_ctx_;
44+
};
45+
46+
#endif // MODELBOX_FLOWUNIT_FFMPEG_WRITER_H_

src/drivers/devices/rockchip/flowunit/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#
2-
# Copyright 2021 The Modelbox Project Authors. All Rights Reserved.
2+
# Copyright 2022 The Modelbox Project Authors. All Rights Reserved.
33
#
44
# Licensed under the Apache License, Version 2.0 (the "License");
55
# you may not use this file except in compliance with the License.
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#
2+
# Copyright 2022 The Modelbox Project Authors. All Rights Reserved.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
cmake_minimum_required(VERSION 3.10)
17+
18+
set(UNIT_DEVICE "rockchip")
19+
set(UNIT_NAME "inference_rknpu2")
20+
21+
project(modelbox-flowunit-${UNIT_DEVICE}-${UNIT_NAME})
22+
23+
file(GLOB_RECURSE UNIT_SOURCE *.cpp *.cc *.c)
24+
group_source_test_files(MODELBOX_UNIT_SOURCE MODELBOX_UNIT_TEST_SOURCE "_test.c*" ${UNIT_SOURCE})
25+
26+
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/modelbox.test.rknpu2.inference.in ${CMAKE_BINARY_DIR}/test/test-working-dir/data/virtual_rknpu_infer_test.toml @ONLY)
27+
28+
include_directories(${CMAKE_CURRENT_LIST_DIR})
29+
include_directories(${LIBMODELBOX_INCLUDE})
30+
include_directories(${LIBMODELBOX_BASE_INCLUDE})
31+
include_directories(${LIBMODELBOX_DEVICE_ROCKCHIP_INCLUDE})
32+
include_directories(${LIBMODELBOX_VIRTUALDRIVER_INFERENCE_INCLUDE})
33+
include_directories(${MODELBOX_COMMON_INFERENCE_INCLUDE})
34+
include_directories(${RKNPU2_INCLUDE_DIR})
35+
include_directories(${ROCKCHIP_MPP_INCLUDE})
36+
include_directories(${ROCKCHIP_RGA_INCLUDE})
37+
38+
set(MODELBOX_UNIT_SHARED libmodelbox-unit-${UNIT_DEVICE}-${UNIT_NAME}-shared)
39+
set(MODELBOX_UNIT_SOURCE_INCLUDE ${CMAKE_CURRENT_LIST_DIR})
40+
41+
add_library(${MODELBOX_UNIT_SHARED} SHARED ${MODELBOX_UNIT_SOURCE})
42+
set(LIBMODELBOX_FLOWUNIT_INFERENCE_ROCKCHIP_SHARED ${MODELBOX_UNIT_SHARED})
43+
44+
set_target_properties(${MODELBOX_UNIT_SHARED} PROPERTIES
45+
SOVERSION ${MODELBOX_VERSION_MAJOR}
46+
VERSION ${MODELBOX_VERSION_MAJOR}.${MODELBOX_VERSION_MINOR}.${MODELBOX_VERSION_PATCH}
47+
)
48+
49+
target_link_libraries(${MODELBOX_UNIT_SHARED} pthread)
50+
target_link_libraries(${MODELBOX_UNIT_SHARED} rt)
51+
target_link_libraries(${MODELBOX_UNIT_SHARED} dl)
52+
target_link_libraries(${MODELBOX_UNIT_SHARED} ${LIBMODELBOX_DEVICE_ROCKCHIP_SHARED})
53+
target_link_libraries(${MODELBOX_UNIT_SHARED} ${RKNPU2_LIBRARY})
54+
target_link_libraries(${MODELBOX_UNIT_SHARED} ${LIBMODELBOX_SHARED})
55+
target_link_libraries(${MODELBOX_UNIT_SHARED} ${LIBMODELBOX_VIRTUALDRIVER_INFERENCE_SHARED})
56+
target_link_libraries(${MODELBOX_UNIT_SHARED} ${MODELBOX_COMMON_INFERENCE_LIBRARY})
57+
58+
set_target_properties(${MODELBOX_UNIT_SHARED} PROPERTIES OUTPUT_NAME "modelbox-unit-${UNIT_DEVICE}-${UNIT_NAME}")
59+
60+
install(TARGETS ${MODELBOX_UNIT_SHARED}
61+
COMPONENT rockchip-device-flowunit
62+
RUNTIME DESTINATION ${CMAKE_INSTALL_FULL_BINDIR}
63+
LIBRARY DESTINATION ${CMAKE_INSTALL_FULL_LIBDIR}
64+
ARCHIVE DESTINATION ${CMAKE_INSTALL_FULL_LIBDIR}
65+
OPTIONAL)
66+
install(DIRECTORY ${HEADER}
67+
DESTINATION ${CMAKE_INSTALL_FULL_INCLUDEDIR}
68+
COMPONENT rockchip-device-flowunit-devel)
69+
70+
set(LIBMODELBOX_FLOWUNIT_INFERENCE_ROCKCHIP_SHARED ${MODELBOX_UNIT_SHARED} CACHE INTERNAL "")
71+
set(LIBMODELBOX_FLOWUNIT_INFERENCE_ROCKCHIP_INCLUDE ${MODELBOX_UNIT_SOURCE_INCLUDE} CACHE INTERNAL "")
72+
set(LIBMODELBOX_FLOWUNIT_INFERENCE_ROCKCHIP_SOURCES ${MODELBOX_UNIT_SOURCE} CACHE INTERNAL "")
73+
set(LIBMODELBOX_FLOWUNIT_INFERENCE_ROCKCHIP_SO_PATH ${CMAKE_CURRENT_BINARY_DIR}/libmodelbox-unit-${UNIT_DEVICE}-${UNIT_NAME}.so CACHE INTERNAL "")
74+
75+
# driver test
76+
list(APPEND DRIVER_UNIT_TEST_SOURCE ${MODELBOX_UNIT_TEST_SOURCE})
77+
list(APPEND DRIVER_UNIT_TEST_TARGET ${MODELBOX_UNIT_SHARED})
78+
list(APPEND DRIVER_UNIT_TEST_LINK_LIBRARIES ${RKMPP_LIBRARIES})
79+
list(APPEND TEST_INCLUDE ${ROCKCHIP_MPP_INCLUDE})
80+
list(APPEND TEST_INCLUDE ${ROCKCHIP_RGA_INCLUDE})
81+
list(APPEND TEST_INCLUDE ${LIBMODELBOX_DEVICE_ROCKCHIP_INCLUDE})
82+
set(DRIVER_UNIT_TEST_SOURCE ${DRIVER_UNIT_TEST_SOURCE} CACHE INTERNAL "")
83+
set(DRIVER_UNIT_TEST_TARGET ${DRIVER_UNIT_TEST_TARGET} CACHE INTERNAL "")
84+
set(TEST_INCLUDE ${TEST_INCLUDE} CACHE INTERNAL "")
85+
set(DRIVER_UNIT_TEST_LINK_LIBRARIES ${DRIVER_UNIT_TEST_LINK_LIBRARIES} CACHE INTERNAL "")

0 commit comments

Comments
 (0)