Page 1 of 1

Very dark image

Posted: 2024-03-25 2:44
by rhoki
Hello, I am experiencing trouble capturing image. I have started with simple program, everything worked, but after short time it stopped. I am getting very dark image, almost nothing can be seen. I have created simple system check program to see if the problem persists, and it does. Rkipc program shows correct picture on video stream, but for testing I have removed lines starting rkipc from RkLunch.sh. I am pretty sure encoding works properly and the issue is with isp algorithm. Lines initializing isp were copied from SAMPLE_COMM_isp_init() and SAMPLE_COMM_isp_run() functions. I am sorry I have to paste the whole code here, but the forum doesn't let me attach cxx files.

Code: Select all

#include <string>
#include <chrono>
#include <fstream>
#include <iostream>

#include <turbojpeg.h>

extern "C" {
#include <rk_mpi_sys.h>
#include <rk_mpi_venc.h>
#include <rk_mpi_vi.h>
#include <rk_mpi_vpss.h>
#include <rk_mpi_mb.h>

#include <rk_aiq_user_api2_sysctl.h>
#include <rk_aiq_user_api2_ae.h>
#include <rk_aiq.h>
#include <rk_aiq_comm.h>
#include <ae/rk_aiq_uapi_ae_int_types_v2.h>

#include <sys/types.h>
#include <pwd.h>
#include <grp.h>
}

// #define JPEG_BACKEND_TURBO
#define JPEG_BACKEND_ROCKIT

typedef struct bytes_s {
	RK_UCHAR* bytes;
	size_t size;
} bytes_t;

void write_file( std::string _filename, bytes_t _data )
{
    uid_t uid{ 1000 };
    gid_t gid{ 1000 };
    std::cout << _filename << std::endl;
    std::ofstream outfile;
    outfile.open( _filename, std::ofstream::out );
    outfile.write( ( char* )_data.bytes, _data.size );
    outfile.flush();
    outfile.close();

    if ( chown(_filename.c_str(), uid, gid) == -1) std::cerr << "Chown failed\n";
}

int main(int argc, char **argv) {
    const rk_aiq_working_mode_t hdr_mode = RK_AIQ_WORKING_MODE_NORMAL;
    rk_aiq_sys_ctx_t* aiq_ctx = nullptr;

    const RK_S32    camera_id = 0;
    const VI_PIPE   vi_pipe = 0;
    const VI_CHN    vi_chn = VI_CHN0;
    const VENC_CHN  venc_chn = 0;

    const uint32_t      width = 2304;
    const uint32_t      height = 1296;
    rkPIXEL_FORMAT_E    rk_pix_fmt = RK_FMT_YUV420SP;

    VIDEO_FRAME_INFO_S  frame;
    VENC_STREAM_S       packet;

    //isp
    char hdr_str[ 16 ];
    snprintf( hdr_str, sizeof( hdr_str ), "%d", ( int )hdr_mode);
    setenv( "HDR_MODE", hdr_str, 1 );

    rk_aiq_static_info_t aiq_static_info;
    rk_aiq_uapi2_sysctl_enumStaticMetasByPhyId( camera_id, &aiq_static_info );
    rk_aiq_uapi2_sysctl_preInit_devBufCnt( aiq_static_info.sensor_info.sensor_name, "rkraw_rx", 2 );
    aiq_ctx = rk_aiq_uapi2_sysctl_init( aiq_static_info.sensor_info.sensor_name,
                                        "/oem/usr/share/iqfiles",
                                        nullptr,
                                        nullptr );

    rk_aiq_uapi2_sysctl_prepare( aiq_ctx, width, height, hdr_mode );
    rk_aiq_uapi2_sysctl_start( aiq_ctx );

    //mpi
    RK_S32 retval;

    retval = RK_MPI_SYS_Init();
    if( retval != RK_SUCCESS ) std::cerr << "MPI sys init failed\n";

    // vi dev
    VI_DEV_ATTR_S videv_attr;
    memset( &videv_attr, 0, sizeof( videv_attr ) );
    retval = RK_MPI_VI_GetDevAttr( camera_id, &videv_attr );
    if( retval != RK_SUCCESS ) std::cerr << "MPI vi GetDevAttr failed\n";
    videv_attr.enIntfMode = VI_MODE_MIPI_YUV420_NORMAL;
    // videv_attr.enWorkMode = VI_WORK_MODE_1Multiplex;
    retval = RK_MPI_VI_SetDevAttr( camera_id, &videv_attr );
    if( retval != RK_SUCCESS ) std::cerr << "MPI vi SetDevAttr failed\n";
    retval = RK_MPI_VI_EnableDev( camera_id );
    if( retval != RK_SUCCESS ) std::cerr << "MPI vi EnableDev failed\n";

    VI_DEV_BIND_PIPE_S videv_pipe;
    memset( &videv_pipe, 0, sizeof( videv_pipe ) );
    videv_pipe.u32Num = 1;
    videv_pipe.PipeId[ vi_pipe ] = camera_id;
    retval = RK_MPI_VI_SetDevBindPipe( camera_id, &videv_pipe );
    if( retval != RK_SUCCESS ) std::cerr << "MPI vi SetDevBindPipe failed\n";

    // vi chn
    VI_CHN_ATTR_S vichn_attr;
    memset( &vichn_attr, 0, sizeof( vichn_attr ) );
    vichn_attr.stIspOpt.u32BufCount = 2;
    vichn_attr.stIspOpt.enMemoryType = VI_V4L2_MEMORY_TYPE_DMABUF;
    vichn_attr.enPixelFormat = rk_pix_fmt;
    vichn_attr.stIspOpt.stMaxSize.u32Width = width;
    vichn_attr.stIspOpt.stMaxSize.u32Height = height;
    vichn_attr.stSize.u32Width = width;
    vichn_attr.stSize.u32Height = height;
    vichn_attr.enCompressMode = COMPRESS_MODE_NONE;
    vichn_attr.stFrameRate.s32SrcFrameRate = 25;
    vichn_attr.stFrameRate.s32DstFrameRate = 25;
    // vichn_attr.u32Depth = 2;
    retval = RK_MPI_VI_SetChnAttr( vi_pipe, vi_chn, &vichn_attr );
    if( retval != RK_SUCCESS ) std::cerr << "MPI vi SetChnAttr failed\n";
    retval = RK_MPI_VI_EnableChn( vi_pipe, vi_chn );
    if( retval != RK_SUCCESS ) std::cerr << "MPI vi EnableChn failed\n";

    // venc chn
    VENC_CHN_ATTR_S vencchn_attr;
    memset( &vencchn_attr, 0, sizeof( vencchn_attr ) );
    vencchn_attr.stVencAttr.u32StreamBufCnt = 2;
    vencchn_attr.stVencAttr.u32BufSize = width * height * 3 / 2;
    vencchn_attr.stVencAttr.enType = RK_VIDEO_ID_JPEG;
    vencchn_attr.stVencAttr.enPixelFormat = rk_pix_fmt;
    vencchn_attr.stVencAttr.u32MaxPicWidth = width;
    vencchn_attr.stVencAttr.u32MaxPicHeight = height;
    vencchn_attr.stVencAttr.u32PicWidth = width;
    vencchn_attr.stVencAttr.u32PicHeight = height;
    vencchn_attr.stVencAttr.u32VirWidth = width;
    vencchn_attr.stVencAttr.u32VirHeight = height;
    vencchn_attr.stVencAttr.enMirror = MIRROR_NONE;
    vencchn_attr.stVencAttr.stAttrJpege.bSupportDCF = RK_FALSE;
    vencchn_attr.stVencAttr.stAttrJpege.stMPFCfg.u8LargeThumbNailNum = 0;
    vencchn_attr.stVencAttr.stAttrJpege.enReceiveMode = VENC_PIC_RECEIVE_SINGLE;
    retval = RK_MPI_VENC_CreateChn( venc_chn, &vencchn_attr );
    if( retval != RK_SUCCESS ) std::cerr << "MPI venc CreateChn failed\n";

    VENC_JPEG_PARAM_S jpeg_param;
    memset( &jpeg_param, 0, sizeof( jpeg_param ) );
    jpeg_param.u32Qfactor = 70;
    retval = RK_MPI_VENC_SetJpegParam( venc_chn, &jpeg_param );
    if( retval != RK_SUCCESS ) std::cerr << "MPI venc  SetJpegParamfailed\n";

    // venc start
    VENC_RECV_PIC_PARAM_S vencrecv_param;
    memset( &vencrecv_param, 0, sizeof( vencrecv_param ) );
    vencrecv_param.s32RecvPicNum = 2;
    retval = RK_MPI_VENC_StartRecvFrame( venc_chn, &vencrecv_param );
    if( retval != RK_SUCCESS ) std::cerr << "MPI venc StartRecvFrame failed\n";

    packet.pstPack = ( VENC_PACK_S* )malloc( sizeof( VENC_PACK_S ) );
    bytes_t picture;

    retval = RK_MPI_VI_GetChnFrame( vi_pipe, vi_chn, &frame , 1000 );
    if( retval != RK_SUCCESS ) std::cerr << "MPI vi GetChnFrame failed" << retval << '\n';
    retval = RK_MPI_VENC_SendFrame( venc_chn, &frame, 1000 );
    if( retval != RK_SUCCESS ) std::cerr << "MPI venc SendFrame failed\n";
    retval = RK_MPI_VENC_GetStream( venc_chn, &packet, 1000 );
    if( retval != RK_SUCCESS ) std::cerr << "MPI venc GetStream failed\n";
    // drop first frame
    RK_MPI_VI_ReleaseChnFrame( vi_pipe, vi_chn, &frame );

    retval = RK_MPI_VI_GetChnFrame( vi_pipe, vi_chn, &frame , 1000 );
    if( retval != RK_SUCCESS ) std::cerr << "MPI vi GetChnFrame failed" << retval << '\n';
    retval = RK_MPI_VENC_SendFrame( venc_chn, &frame, 1000 );
    if( retval != RK_SUCCESS ) std::cerr << "MPI venc SendFrame failed\n";
    retval = RK_MPI_VENC_GetStream( venc_chn, &packet, 1000 );
    if( retval != RK_SUCCESS ) std::cerr << "MPI venc GetStream failed\n";

    picture.bytes = ( RK_UCHAR* )RK_MPI_MB_Handle2VirAddr( packet.pstPack->pMbBlk );
    picture.size = packet.pstPack->u32Len;

    const auto now = std::chrono::system_clock::now();
    std::string datetime = std::to_string( std::chrono::system_clock::to_time_t( now ) );
    std::string filename{ std::string( "/mnt/sdcard/" ).append( datetime ).append( ".jpg" ) };
    write_file( filename, picture );

    RK_MPI_VI_DisableChn( vi_pipe, vi_chn );
    RK_MPI_VENC_StopRecvFrame( venc_chn );
    RK_MPI_VENC_ReleaseStream( venc_chn, &packet );
    free( packet.pstPack );
    RK_MPI_VENC_DestroyChn( venc_chn );
    RK_MPI_VI_DisableDev( camera_id );

    RK_MPI_SYS_Exit();

    return 0;
}

Code: Select all

Process /mnt/sdcard/oem/usr/bin/system_chec created; pid = 458
Listening on port 8888
Remote debugging from host 172.32.0.100, port 52934
RTVersion        12:05:23-913 {dump              :064} ---------------------------------------------------------
RTVersion        12:05:23-913 {dump              :065} rockit version: git-8cb4d25b8 Tue Feb 28 11:12:39 2023 +0800
RTVersion        12:05:23-913 {dump              :066} rockit building: built- 2023-02-28 15:23:19
RTVersion        12:05:23-913 {dump              :067} ---------------------------------------------------------
(null)           12:05:23-913 {log_level_init    :203}

 please use echo name=level > /tmp/rt_log_level set log level
	name: all cmpi mb sys vdec venc rgn vpss vgs tde avs wbc vo vi ai ao aenc adec
	log_level: 0 1 2 3 4 5 6

(null)           12:05:23-913 {read_log_level    :093} text is vi=6
venc=6

(null)           12:05:23-914 {read_log_level    :095} module is vi, log_level is 6
(null)           12:05:23-916 {monitor_log_level :144} #Start monitor_log_level thread, arg:(nil)
rkaiq log level ff0
rockit log path (null), log_size = 0, can use export rt_log_path=, export rt_log_size= change
log_file = (nil)
rockit default level 4, can use export rt_log_level=x, x=0,1,2,3,4,5,6 change
RTIsp3x          12:05:23-921 {ispInitDevice     :208} sensor name = m00_b_sc3336 4-0030
RTIsp3x          12:05:23-921 {ispInitDevice     :211} sensor_index = 0
RTIsp3x          12:05:23-923 {getLinkVideoNode  :164} find /dev/media1: rkcif-mipi-lvds
RTIsp3x          12:05:23-925 {ispInitDevice     :208} sensor name = m00_b_sc3336 4-0030
RTIsp3x          12:05:23-925 {ispInitDevice     :211} sensor_index = 0
RTIsp3x          12:05:23-927 {getLinkVideoNode  :164} find /dev/media1: rkcif-mipi-lvds
RKViDev          12:05:23-929 {vi_get_dev_pipe_st:273} attr.devStatus = 00000000
MPI vi GetDevAttr failed
RKViChn          12:05:23-932 {prepareRuntime    :334} ---------------------------------------------------------
RKViChn          12:05:23-932 {prepareRuntime    :336} vi version: 1.86, name:vvi
RKViChn          12:05:23-935 {prepareRuntime    :337} rockit-ko version: vmpi:fb2eed2be49e
RKViChn          12:05:23-935 {prepareRuntime    :338} rockit-ko building: -2023-02-09-11:04:49
RKViChn          12:05:23-935 {prepareRuntime    :339} ---------------------------------------------------------
RKViChn          12:05:23-935 {prepareRuntime    :358} mb pool create success, MBCnt= 2
RKViDev          12:05:23-931 {vi_get_dev_pipe_st:273} attr.devStatus = 00000000
RKViDev          12:05:23-931 {vi_set_dev_attr   :440} VI_DEV_ATTR_S all parameter reserved
RKViDev          12:05:23-931 {vi_set_status     :286} attr.devStatus = 00000001
RKViDev          12:05:23-931 {vi_get_dev_pipe_st:273} attr.devStatus = 00000001
RTIsp3x          12:05:23-931 {getIspHwInfo      :247} getIspHwInfo enter >>>
RTIsp3x          12:05:23-931 {getIspHwInfo      :249} find isp count: 1, Current selected: 0
RTIsp3x          12:05:23-931 {getVideoName      :287} entityName = rkisp_mainpath
RTIsp3x          12:05:23-931 {getIspHwInfo      :247} getIspHwInfo enter >>>
RTIsp3x          12:05:23-931 {getIspHwInfo      :249} find isp count: 1, Current selected: 0
RTIsp3x          12:05:23-931 {getVideoName      :287} entityName = rkisp_selfpath
RTIsp3x          12:05:23-931 {getIspHwInfo      :247} getIspHwInfo enter >>>
RTIsp3x          12:05:23-931 {getIspHwInfo      :249} find isp count: 1, Current selected: 0
RTIsp3x          12:05:23-931 {getVideoName      :287} entityName = rkisp_bypasspath
RKViDev          12:05:23-931 {vi_set_status     :286} attr.devStatus = 00000003
  === VI ATTRS INFO: ===
	devId  :      0
	chnId  :      0
	buffcnt:      0
	buffSize:     0
	width:        0
	height:       0
	Maxwidth:     0
	Maxwidth:     0
	streaming:    0
RKViChn          12:05:23-931 {RKVIChannel       :197} vi chn:0 mChnState = 1 (not create yet)
cmpi             12:05:23-932 {prepareRuntime    :329} VVI_IOCTL_VERSION
RKViDev          12:05:23-935 {vi_get_ch_status  :304} pipe = 0, ch = 0, enable = 0, streamOn=0
cmpi             12:05:23-935 {createRuntime     :546} [non-WRAP MODE]: buff size = 4478976
cmpi             12:05:23-935 {createRuntime     :557} viNodeInf.buffSize = 4478976, viNodeInf.buffCnt = 2
cmpi             12:05:23-935 {createRuntime     :559} VVI_IOCTL_NODE_CREATE
RKViChn          12:05:23-939 {open              :640} enCaptureType = 1
RKViChn          12:05:23-940 {open              :740} vi chn:0, mChnState = 1
RTDeviceV4L2     12:05:23-940 {ispInitParams     :660} camera init paremeters1:
		 resolution   : 2304 * 1296
		 bufferCnt    : 0
		 useLibV4l2   : 1
		 pixFormat    : 0x0
		 memoryType   : 0x4
		 captureType  : 0x1
		 colorSpace   : 0
RTDeviceV4L2     12:05:23-940 {ispInitParams     :675} camera init paremeters2:
		 entityName   : rkisp_mainpath
		 cameraIndex  : 0
		 fbcMode      : 0x0
		 runInko      : 1
		 chnState     : 1
		 wrapLine     : 0
		 fps          : 25

RTIsp3x          12:05:23-941 {ispInitDevice     :208} sensor name = m00_b_sc3336 4-0030
RTIsp3x          12:05:23-941 {ispInitDevice     :211} sensor_index = 0
RTIsp3x          12:05:23-942 {getLinkVideoNode  :164} find /dev/media1: rkcif-mipi-lvds
RTIsp3x          12:05:23-943 {getIspHwInfo      :247} getIspHwInfo enter >>>
RTIsp3x          12:05:23-943 {getIspHwInfo      :249} find isp count: 1, Current selected: 0
RTIsp3x          12:05:23-943 {getVideoName      :287} entityName = rkisp_mainpath
RTDeviceV4L2     12:05:23-943 {open              :138} open video name(/dev/video11)
RTDeviceV4L2     12:05:23-943 {ispOpenDevice     :521} v4l2_open(/dev/video11), handler=47
RTIsp3x          12:05:23-943 {setIspFps         :505} setIspFps enter >>>
RTIsp3x          12:05:23-943 {setIspFps         :512} isp set fps:25
RTIsp3x          12:05:23-943 {setIspFps         :515} setIspFps leave <<<
RKViChn          12:05:23-944 {prepare           :878} start dev:0 chn:0 prepare
RTDeviceV4L2     12:05:23-944 {ispCameraInfo     :538} camera information:
RTDeviceV4L2     12:05:23-944 {ispCameraInfo     :539} device name : /dev/video11
RTDeviceV4L2     12:05:23-944 {ispCameraInfo     :540} driver name : rkisp_v7
RTDeviceV4L2     12:05:23-944 {ispCameraInfo     :541} card name   : rkisp_mainpath
RTDeviceV4L2     12:05:23-944 {ispCameraInfo     :542} bus info    : platform:rkisp-vir0
RTDeviceV4L2     12:05:23-944 {ispCameraInfo     :544} version     : 2.0.0
RTDeviceV4L2     12:05:23-945 {ispCameraInfo     :549} current device:/dev/video11 isn't compatible(cap:0x84201000) device,memoryType:4, retry:0
RTDeviceV4L2     12:05:23-945 {prepare           :419} mV4L2Ctx->wrapInfo->height = 0 width = 0
RTDeviceV4L2     12:05:23-945 {ispInitFormat     :726} ioctl VIDIOC_S_FMT OK
cmpi             12:05:23-945 {startRuntime      :584} VVI_IOCTL_NODE_START
RKViChn          12:05:23-946 {start             :813} =========== vi Start startRuntime ===========
RKViChn          12:05:23-946 {streamOn          :900} start dev:0 chn:0 stream on
RTDeviceV4L2     12:05:23-946 {ispStreamOn       :440} do ispStreamOn start
RTDeviceV4L2     12:05:23-946 {ispStreamOn       :456} setIspFps 25 to isp driver
RTIsp3x          12:05:23-946 {setIspFps         :505} setIspFps enter >>>
RTIsp3x          12:05:23-946 {setIspFps         :512} isp set fps:25
RTIsp3x          12:05:23-946 {setIspFps         :515} setIspFps leave <<<
RTIsp3x          12:05:23-946 {setIspMirrorFlip  :485} setIspMirrorFlip enter >>>
RTIsp3x          12:05:23-946 {setIspMirrorFlip  :486} mirror = 0 flip = 0
RTIsp3x          12:05:23-946 {setIspMirrorFlip  :494} isp set mirror flip: mirror:0 fip: 0
RTIsp3x          12:05:23-946 {setIspMirrorFlip  :497} setIspMirrorFlip leave <<<
RTDeviceV4L2     12:05:23-965 {ispStreamOn       :493} do ispStreamOn done
RKViDev          12:05:23-966 {vi_set_ch_status  :320} pipe = 0, ch = 0, enable = 1, streamOn=1
MpiAdapterEncode 12:05:23-966 {ma_encode_create  :1653} ringbuf buf_size 4481024
cmpi             12:05:24-046 {mb_get_buffer_by_i:422} allocated buffer(this=0xf6e40, data=(nil), size=0, id=-1)
RKViChn          12:05:24-046 {getFrameRuntime   :1018} buffer fd = -1 mpi_id = 138
RKViChn          12:05:24-048 {getFrameRuntime   :1025} get fd = 50 from id = 138
RKViChn          12:05:24-049 {getFrameRuntime   :1031} -------------------------------vviBuf->buf_size: 4481024
MpiAdapterEncode 12:05:24-057 {ma_encode_get_pack:1978} map ptr: 0xa42c1000
cmpi             12:05:24-086 {mb_get_buffer_by_i:422} allocated buffer(this=0xf90f8, data=(nil), size=0, id=-1)
RKViChn          12:05:24-086 {getFrameRuntime   :1018} buffer fd = -1 mpi_id = 139
RKViChn          12:05:24-087 {getFrameRuntime   :1025} get fd = 52 from id = 139
RKViChn          12:05:24-087 {getFrameRuntime   :1031} -------------------------------vviBuf->buf_size: 4481024
/mnt/sdcard/1609502724.jpg
mpp[458]: mpp_chan: ctx is no found in chan server

RKSockServer     12:05:24-926 {start             :162} accept failed
cmpi             12:05:24-095 {stopRuntime       :833} VVI_IOCTL_NODE_STOP:  dev:0, chn:0
RKViChn          12:05:24-095 {streamOff         :915} start dev:0 chn:0 stream off
RTDeviceV4L2     12:05:24-095 {ispStreamOff      :503} do ispStreamOff start
RTDeviceV4L2     12:05:24-171 {ispStreamOff      :514} do ispStreamOff done
RKViChn          12:05:24-171 {close             :934} start dev:0 chn:0 close
RTDeviceV4L2     12:05:24-171 {close             :365} do RTDeviceV4L2 close
cmpi             12:05:24-172 {deleteRuntime     :308} VVI_IOCTL_NODE_DELETE: dev:0, chn:0
RKViDev          12:05:24-172 {vi_set_ch_status  :320} pipe = 0, ch = 0, enable = 0, streamOn=0
cmpi             12:05:24-172 {stopRuntime       :833} VVI_IOCTL_NODE_STOP:  dev:0, chn:0
MpiAdapterEncode 12:05:24-175 {ma_encode_destroy :2222} unmap ptr: 0xa42c1000
RKViDev          12:05:24-176 {vi_set_status     :286} attr.devStatus = 00000000

Child exited with status 0
*** Exited normally ***

Re: Very dark image

Posted: 2024-03-25 10:59
by Crocodile
Thank you for your exploration of luckfox-pico usage.

Just as you speculated,the issue you encountered with dark image capture in the example code seems to stem from the loading of the ISP algorithm on the VI pipe, rather than being an encoding problem.

While it's possible to set venc to receive single images in the example code, VI captures images in a stream format independently of venc.
simple_dark.jpg
Therefore, when loading the ISP algorithm, it doesn't process individual frames but rather the entire stream. The ISP algorithm documentation (unfortunately, only available in Chinese) indicates that after 3A statistics and 3A calculations, exposure parameters are updated to the sensor driver. Consequently, the exposure parameters for the first few frames captured by VI are not properly set, resulting in darker images.
simple_dark02.jpg
The solution is to introduce a delay or manually skip the initial frames.
simple_dark03.jpg

Code: Select all

sleep(1);
or

Code: Select all

    for(int i = 0 ;i < 6;i++)
    {
        retval = RK_MPI_VI_GetChnFrame( vi_pipe, vi_chn, &frame , 1000 );
        if( retval != RK_SUCCESS ) std::cerr << "MPI vi GetChnFrame failed" << retval << '\n';
        RK_MPI_VI_ReleaseChnFrame( vi_pipe, vi_chn, &frame );
    }
I hope this can answer your doubts.

Furthermore, if you need attachments, you can try compressing them into a .zip or .7z file and then upload them.

Re: Very dark image

Posted: 2024-03-27 20:25
by rhoki
Thank you sir, this was most helpful.