From 4feedede4e76bf806c5b83dc0d493864dc82f4d4 Mon Sep 17 00:00:00 2001 From: lxowalle Date: Thu, 23 May 2024 13:58:11 +0800 Subject: [PATCH] * fix venc bugs --- components/vision/include/maix_video.hpp | 2 +- .../vision/port/maixcam/maix_video_mmf.cpp | 27 ++++++++++++------- examples/video_demo/main/src/main.cpp | 4 ++- 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/components/vision/include/maix_video.hpp b/components/vision/include/maix_video.hpp index a344b18c..8c69042a 100644 --- a/components/vision/include/maix_video.hpp +++ b/components/vision/include/maix_video.hpp @@ -120,7 +120,7 @@ namespace maix::video * @return raw data * @maixpy maix.video.Frame.to_bytes */ - Bytes *to_bytes(bool copy) { + Bytes *to_bytes(bool copy = false) { Bytes *b = NULL; if (copy) { b = new Bytes(_data, _data_size, true, true); diff --git a/components/vision/port/maixcam/maix_video_mmf.cpp b/components/vision/port/maixcam/maix_video_mmf.cpp index 9181ee84..40040555 100644 --- a/components/vision/port/maixcam/maix_video_mmf.cpp +++ b/components/vision/port/maixcam/maix_video_mmf.cpp @@ -48,6 +48,10 @@ namespace maix::video switch (_type) { case VIDEO_H265_CBR: { + if (0 != mmf_init()) { + err::check_raise(err::ERR_RUNTIME, "init mmf failed!"); + } + if (0 != mmf_enc_h265_init(MMF_VENC_CHN, _width, _height)) { err::check_raise(err::ERR_RUNTIME, "init mmf enc failed!"); } @@ -64,8 +68,13 @@ namespace maix::video .gop = _gop, .intput_fps = _framerate, .output_fps = _framerate, - .bitrate = _bitrate, + .bitrate = _bitrate / 1000, }; + + if (0 != mmf_init()) { + err::check_raise(err::ERR_RUNTIME, "init mmf failed!"); + } + if (0 != mmf_add_venc_channel(MMF_VENC_CHN, &cfg)) { err::check_raise(err::ERR_RUNTIME, "mmf venc init failed!"); } @@ -205,7 +214,6 @@ namespace maix::video } } else { // encode from camera if (!this->_bind_camera) { - log::warn("You need use bind_camera() function to bind the camera!\r\n"); goto _exit; } @@ -315,23 +323,23 @@ namespace maix::video err::check_raise(err::ERR_RUNTIME, "get venc config failed!\r\n"); } - int img_w = img->width(); - int img_h = img->height(); - image::Format img_fmt = img->format(); + int img_w = width; + int img_h = height; + int mmf_fmt = format; if (img_w != cfg.w - || img->height() != cfg.h - || img->format() != mmf_invert_format_to_maix(cfg.fmt)) { + || img_h != cfg.h + || mmf_fmt != cfg.fmt) { log::warn("image size or format is incorrect, try to reinit venc!\r\n"); mmf_del_venc_channel(MMF_VENC_CHN); cfg.w = img_w; cfg.h = img_h; - cfg.fmt = mmf_invert_format_to_mmf(img_fmt); + cfg.fmt = mmf_invert_format_to_mmf(mmf_fmt); if (0 != mmf_add_venc_channel(MMF_VENC_CHN, &cfg)) { err::check_raise(err::ERR_RUNTIME, "mmf venc init failed!\r\n"); } _width = img_w; _height = img_h; - _format = img_fmt; + _format = (image::Format)mmf_invert_format_to_maix(mmf_fmt); } if (mmf_venc_push(MMF_VENC_CHN, (uint8_t *)data, width, height, format)) { @@ -819,7 +827,6 @@ namespace maix::video } } else { // encode from camera if (!this->_bind_camera) { - log::warn("You need use bind_camera() function to bind the camera!\r\n"); goto _exit; } diff --git a/examples/video_demo/main/src/main.cpp b/examples/video_demo/main/src/main.cpp index b6200156..871bda60 100644 --- a/examples/video_demo/main/src/main.cpp +++ b/examples/video_demo/main/src/main.cpp @@ -245,7 +245,7 @@ int _main(int argc, char* argv[]) int width = 640; int height = 480; video::VideoType type = video::VIDEO_H264_CBR; - video::Encoder e = video::Encoder(width, height, image::Format::FMT_YVU420SP, type); + video::Encoder e = video::Encoder(2560, 1440, image::Format::FMT_YVU420SP, type); camera::Camera cam = camera::Camera(width, height, image::Format::FMT_YVU420SP); while(!app::need_exit()) { @@ -270,6 +270,7 @@ int _main(int argc, char* argv[]) bool capture = true; video::Encoder e = video::Encoder(width, height, image::Format::FMT_YVU420SP, type, framerate, gop, bitrate, time_base, capture); camera::Camera cam = camera::Camera(width, height, image::Format::FMT_YVU420SP); + display::Display disp = display::Display(); e.bind_camera(&cam); char *file = (char *)"output.h264"; @@ -279,6 +280,7 @@ int _main(int argc, char* argv[]) while(!app::need_exit()) { video::Frame *frame = e.encode(); image::Image *img = e.capture(); + disp.show(*img); printf("frame data:%p size:%ld pts:%ld dts:%ld\r\n", frame->data(), frame->size(), frame->get_pts(), frame->get_dts()); printf("image size:%d\r\n", img->data_size());