算能開發(fā)環(huán)境搭建

下載安裝sdk

wget https://sophon-assets.sophon.cn/sophon-prod-s3/drive/25/11/21/14/sophonsdk_edge_v2.1_official_release.zip -O /data/xzl/tools/
unzip sophonsdk_edge_v2.1_official_release.zip 
dpkg -i /data/xzl/tools/sophonsdk_edge_v2.1_official_release/sophon-img/bsp-debs/sophon-media-soc-sophon-ffmpeg-dev_2.1.0_arm64.deb
dpkg -i /data/xzl/tools/sophonsdk_edge_v2.1_official_release/sophon-img/bsp-debs/sophon-media-soc-sophon-opencv-dev_2.1.0_arm64.deb

編譯libsrtp

git clone https://github.com/cisco/libsrtp.git -b 2_4_x_throttle
cd libsrtp
./configure --enable-openssl
make -j $(nproc)
make install

編譯ZLMediaKit

# 下載zlm-pro源碼, 替換為您真實(shí)的路徑
git clone git@github.com:xia-chu/zlmediakit-promax.git -b feature/all
cd zlmediakit-promax
git submodule update --init
mkdir build
cd build
cmake .. -DENABLE_FFMPEG=ON \
 -DFFMPEG_PATH_ROOT=/opt/sophon/sophon-ffmpeg-latest \
 -DENABLE_TESTS=OFF \
 -DENABLE_API=OFF
make -j $(nproc)

修改ZLMediaKit的配置文件


[transcode]
audio_bitrate=32000
audio_channel=1
audio_codec=mpeg4-generic
audio_samplerate=8000

# 編解碼器白名單
decoder_h264=h264_bm
decoder_h265=hevc_bm
encoder_h264=h264_bm
encoder_h265=h265_bm

decoder_threads=4
enable_ffmpeg_log=0
encoder_threads=4
filter=
filter_threads=4
force=0
hw_decoder=1
hw_encoder=1
# 全硬件轉(zhuǎn)碼,開啟ai推理時(shí)需要置空
hwaccel=bmcodec
percent=100
# 開啟透明轉(zhuǎn)碼
suffix=null
video_bitrate=4000000
video_codec=H264
video_encoder_opt=

啟動(dòng)ZLMediaKit

export LD_LIBRARY_PATH=/opt/sophon/sophon-ffmpeg-latest/lib
ulimit -n 10240
ulimit -c unlimited
./MediaServer -l0 -c ./config.ini

編解碼器配置

ffmpeg -h decoder=h264_bm

Decoder h264_bm [bm H.264 decoder wrapper]:
    General capabilities: delay avoidprobe hardware
    Threading capabilities: none
    Supported hardware devices: bmcodec
h264_bm_decoder AVOptions:
  -mode_bitstream    <int>        .D.V....... mode of bit stream in the bitmain decoder context (from 0 to 2) (default 2)
  -output_format     <int>        .D.V....... the output data format: uncompressed(0, at default) or compressed(101) (from 0 to INT_MAX) (default 0)
  -cbcr_interleave   <int>        .D.V....... cbcr interleave of output frame in the bitmain decoder context (from 0 to 2) (default 1)
  -extra_frame_buffer_num <int>        .D.V....... number of extra frame buffer in the bitmain decoder context (from 0 to INT_MAX) (default 7)
  -extra_data_flag   <int>        .D.V....... process extra data flag of avpkt in the bitmain decoder context (from 0 to INT_MAX) (default 1)
  -frame_delay       <int>        .D.V....... return a display frame after frame_delay frames decoding in the bitmain decoder context (from INT_MIN to INT_MAX) (default -1)
  -pcie_board_id     <int>        .D.V....... board id when running in pcie mode (DEPRECATED) (from 0 to 256) (default 0)
  -pcie_no_copyback  <flags>      .D.V....... don't copy the decoded image back to CPU in pcie mode (DEPRECATED) (default 1)
  -sophon_idx        <int>        .D.V....... sophon device index when running in pcie mode (from 0 to 256) (default 0)
  -zero_copy         <flags>      .D.V....... don't copy the decoded image back to CPU in pcie mode (default 1)
  -enable_cache      <int>        .D.V....... enable output yuv frame buffer cache in the bitmain decoder context (from INT_MIN to INT_MAX) (default 0)
  -handle_packet_loss <int>        .D.V....... enable packet loss handling in the bitmain decoder context (from INT_MIN to INT_MAX) (default 0)
  -skip_non_idr      <int>        .D.V....... enable skip non idr frame in the bitmain decoder context (from 0 to 2) (default 0)
  -perf              <flags>      .D.V....... indicate if profile the performance (default 0)
  -secondary_axi     <int>        .D.V....... enable second axi in the bitmain decoder. 0: disable, 1: enable, 2: auto (from 0 to 2) (default 2)
  -use_gst_flag      <int>        .D.V....... When ffmpeg is called with GST (from 0 to 1) (default 0)
  -core_idx          <int>        .D.V....... Specify a decoding core in a soc to decode. 1684 max = 3, 1684x max = 1 (from -1 to 3) (default -1)
  -size_input_buffer <int>        .D.V....... input buffer size of video decoder bitstream (0x200000, 0x700000) (from 2.09715e+06 to 7.34003e+06) (default 4194304)
  -dec_cmd_queue     <int>        .D.V....... decoder command queue depth (from 1 to 4) (default 4)
  -timeout           <int>        .D.V....... decoder timeout max count (from 0 to INT_MAX) (default 1000)
  -pts_calibrate     <flags>      .D.V....... use dts to correct frame's pts (default 0)
ffmpeg -h encoder=h264_bm

Encoder h264_bm [BM H.264 encoder]:
    General capabilities: delay hardware
    Threading capabilities: none
    Supported hardware devices: bmcodec
    Supported pixel formats: bmcodec yuv420p yuvj420p nv12 nv21
h264_bm_encoder AVOptions:
  -is_dma_buffer     <flags>      E..V....... flag to indicate if the input frame buffer is continuous physical buffer (default 1)
  -preset            <int>        E..V....... Set the encoding preset : 0 - fast, 1 - medium, 2 - slow(default). (deprecated, please set it in enc-params) (from 0 to 2) (default 2)
  -gop_preset        <int>        E..V....... gop preset idx: (deprecated, please set it in enc-params)
                        1 - all I, gopsize 1;
                        2 - IPP, cyclic gopsize 1;
                        3 - IBB, cyclic gopsize 1;
                        4 - IBPBP, cyclic gopsize 2;
                        5 - IBBBP, cyclic gopsize 4;
                        6 - IPPPP, cyclic gopsize 4;
                        7 - IBBBB, cyclic gopsize 4;
                        8 - random access, IBBBBBBBB, cyclic gopsize 8;
                        9 - IPP,   cyclic gopsize 1; (from 1 to 9) (default 2)
  -qp                <int>        E..V....... Constant quantization parameter rate control method (from -1 to 51) (default -1)
  -enc-params        <string>     E..V....... Override the configuration using a :-separated list of key=value parameters
  -perf              <flags>      E..V....... flag to indicate if do the performance testing (default 0)
  -roi_enable        <int>        E..V....... roi enable: 0 disable roi encoding; 1 use roi encoding.  default 0. (from 0 to 1) (default 0)
  -enc_cmd_queue     <int>        E..V....... vpu enc command queue depth (from 1 to 16) (default 4)

下載ai插件

# 替換為真實(shí)路徑
git clone https://github.com/xia-chu/yolov8_trt.git
cd yolov8_trt
git submodule update --init
mkdir build
cd build
apt install libeigen3-dev fonts-noto-cjk
cmake .. -DOpenCV_DIR=/opt/sophon/sophon-opencv-latest/lib/cmake/opencv4/ -DFFMPEG_PATH_ROOT=/opt/sophon/sophon-ffmpeg-latest -DENABLE_BM=ON -DCMAKE_BUILD_TYPE=Debug

為什么算能不能全硬件編解碼+推理

也就是:為什么bm硬件解碼器輸出的hwaccel類型的幀無法轉(zhuǎn)換為bm_image進(jìn)行推理

  • 只有非hwaccel類型的幀才可能設(shè)置channel_layout為101
image.png
  • AVFrame轉(zhuǎn)換為cv::Mat后,再轉(zhuǎn)換為bm_image,使用toBMI有兩個(gè)邏輯分支:


    image.png
  • cv::Mat::avComp() 實(shí)現(xiàn)如下:


    image.png
  • 也就是hwaccel類型的幀,只會(huì)走toBMI_AVFrame路徑:


    image.png
  • 但是map_avformat_to_bmformat不支持AV_PIX_FMT_BMCODEC:


    image.png
  • toBMI_COMP邏輯如下, 必須設(shè)置cv::Mat::u::addr字段:


    image.png
  • 另外,壓縮后的hwaccel幀,也不能直接編碼:


    image.png

    image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容