首页 > 代码库 > 海思Hi3516A(4)HDR模式开发

海思Hi3516A(4)HDR模式开发

1. HDR概述

  HDR(High-Dynamic Range)算法根据不同曝光的图片,合成最终HDR图片,比一般图片具有更宽的动态范围和图像细节。

  一般HDR算法有两种:一种是基于不同曝光时间的连续几幅图片合现;第二种是基于同一行不同曝光,最终成一幅图片,这种方法实现简单,只要能调曝光的sensor都可以实合成一幅图片,这种方法需要sensor支持行间长短曝光。

  这里使用smartsens公司的SC5035 500万分辨率摄像头芯片实现行间长短曝光实现HDR算法。


2. HDR模式传输方式

  在SC5035中,视频信号通过MIPI接口传输,根据MIPI协议可将长短曝光行数据,通过两个virtual channel来传输。

  SC5035行间HDR数据读出时序如下:

技术分享

  • Fram_longFram_short输出偏差为max short exposure

  • max long exposure={帧长}-max short exposure


3. sensor的库文件生成(.so)

  Sensor的库文件需要在Linux服务器中的SDK包中编译得到,将生成的.so文件放到SDK包中的stream软件包中的Hi3516A_Stream_xxx/libs目录下。

sc5035_sensor_ctl.c实现sensor的初始化:

void sensor_init_5M_2lan_10bit_hdr()
{
	sensor_write_register(0x0100,0x00);sensor_write_register(0x363f,0x1c);
	sensor_write_register(0x3e03,0x03);sensor_write_register(0x3e08,0x00);
	sensor_write_register(0x3e09,0x10);sensor_write_register(0x3400,0x52);
	sensor_write_register(0x5000,0x21);sensor_write_register(0x5001,0x04);
	sensor_write_register(0x3627,0x02);sensor_write_register(0x320c,0x06);
	sensor_write_register(0x3f00,0xc6);sensor_write_register(0x3f01,0x04);
	sensor_write_register(0x3306,0x20);sensor_write_register(0x330b,0x38);
	sensor_write_register(0x4503,0xbb);sensor_write_register(0x3623,0x00);
	sensor_write_register(0x3622,0x0e);sensor_write_register(0x3630,0x34);
	sensor_write_register(0x3303,0x30);sensor_write_register(0x3309,0x30);
	sensor_write_register(0x331e,0x28);sensor_write_register(0x331f,0x28);
	sensor_write_register(0x3320,0x2c);sensor_write_register(0x3321,0x2c);
	sensor_write_register(0x3322,0x2c);sensor_write_register(0x3323,0x2c);
	sensor_write_register(0x5001,0x05);sensor_write_register(0x3902,0x05);
	sensor_write_register(0x3909,0x02);sensor_write_register(0x3907,0x00);
	sensor_write_register(0x3908,0x40);sensor_write_register(0x3416,0x00);
	sensor_write_register(0x3621,0x18);
	sensor_write_register(0x3627,0x04);sensor_write_register(0x3333,0x00);
	sensor_write_register(0x3334,0xf0);sensor_write_register(0x3662,0x82);
	sensor_write_register(0x3e09,0x50);sensor_write_register(0x3908,0x00);
	sensor_write_register(0x3620,0x62);sensor_write_register(0x5000,0x21);
	sensor_write_register(0x3e09,0xf0);sensor_write_register(0x3400,0x52);
	sensor_write_register(0x3638,0x84);sensor_write_register(0x3637,0x82);
	sensor_write_register(0x363f,0x0c);sensor_write_register(0x3622,0x02);
	sensor_write_register(0x330b,0x3c);sensor_write_register(0x363a,0x1c);
	sensor_write_register(0x3c00,0x41);sensor_write_register(0x3e01,0x10);
	sensor_write_register(0x3e05,0x00);sensor_write_register(0x3e06,0xf0);
	sensor_write_register(0x3221,0x80);sensor_write_register(0x4816,0x11);
	sensor_write_register(0x3e04,0x03);sensor_write_register(0x6000,0x21);
	sensor_write_register(0x320e,0x0f); //max inttime
	sensor_write_register(0x320f,0x80); //max inttime
	sensor_write_register(0x3e15,0x00); //max short exposure
	sensor_write_register(0x3e16,0x40); //max short exposure
	sensor_write_register(0x3c00,0x00); //[2]: 0  mipi->fifo
	sensor_write_register(0x303f,0x02); //[7]: 0  sel pll_pclk
	sensor_write_register(0x3031,0x0a); //[3:0] 10bit
	sensor_write_register(0x3018,0x33); //[7:5]  sel lane=reg+1
	sensor_write_register(0x3c03,0x10); //fifo for 4lane
	sensor_write_register(0x3030,0x04);
	sensor_write_register(0x303d,0x01);sensor_write_register(0x303b,0x02);
	sensor_write_register(0x303c,0x08);sensor_write_register(0x3650,0x37);
	sensor_write_register(0x363f,0x0c);sensor_write_register(0x3300,0x10);
	sensor_write_register(0x3654,0x45);sensor_write_register(0x3902,0x45);
	sensor_write_register(0x3909,0x00);sensor_write_register(0x390a,0x00);
	sensor_write_register(0x3400,0x53);sensor_write_register(0x3039,0x10);
	sensor_write_register(0x303a,0x18);sensor_write_register(0x330a,0x01);
	sensor_write_register(0x330b,0x40);sensor_write_register(0x3306,0x80);
	sensor_write_register(0x3308,0x08);sensor_write_register(0x3416,0x20);
	sensor_write_register(0x320c,0x04);sensor_write_register(0x3907,0x03);
	sensor_write_register(0x3626,0x30); //0909
	sensor_write_register(0x5000,0x21);sensor_write_register(0x330b,0xa0);
	sensor_write_register(0x3630,0x24); //0x14
	sensor_write_register(0x3620,0x48);sensor_write_register(0x3f05,0xf0);
	sensor_write_register(0x320c,0x05);sensor_write_register(0x3e09,0x10);
	sensor_write_register(0x320d,0xa0); //25fps
	sensor_write_register(0x3c00,0x41); //fifo configsensor_write_register(0x3622,0x02);
	sensor_write_register(0x3638,0x83);sensor_write_register(0x3306,0x90);
	sensor_write_register(0x3f05,0xe0);sensor_write_register(0X3620,0X42);
	sensor_write_register(0x363a,0x14);sensor_write_register(0x3416,0x11);
	sensor_write_register(0x3334,0xa0);sensor_write_register(0x3333,0x90);
	sensor_write_register(0x3620,0x46); //0x66  gain<2
	sensor_write_register(0x3303,0x48);sensor_write_register(0x3309,0x48);
	sensor_write_register(0x3627,0x08);
	sensor_write_register(0x335d,0x00);sensor_write_register(0x330e,0x10);
	sensor_write_register(0x303c,0x08); //half mipi speed
	sensor_write_register(0x363c,0xc3);
	sensor_write_register(0x3010,0xe0); //close dvp out
	sensor_write_register(0x300f,0xff);sensor_write_register(0x300e,0x0f);
	sensor_write_register(0x3635,0x38);sensor_write_register(0x3636,0x38);
	sensor_write_register(0x363f,0x80);sensor_write_register(0x3631,0x02);
	sensor_write_register(0x3637,0x82);sensor_write_register(0x3035,0xca);
	sensor_write_register(0x3300,0x30);sensor_write_register(0x330b,0x60);
	sensor_write_register(0x3f04,0x03);sensor_write_register(0x3f05,0x30);
	sensor_write_register(0x3633,0x1e);sensor_write_register(0x3630,0x1c);
	sensor_write_register(0x3622,0x06);sensor_write_register(0x0100,0x01);
}

  sc5035_cmos.c主要实现ISP需要的回调函数,传递参数,包括ISP、AE、AWB等。还需将变量genSensorMode赋值为WDR_MODE_2To1_LINE,以选择行间HDR模式。

//ISP function
HI_S32 cmos_init_sensor_exp_function(ISP_SENSOR_EXP_FUNC_S *pstSensorExpFunc)
{
    memset(pstSensorExpFunc, 0, sizeof(ISP_SENSOR_EXP_FUNC_S));
  
    pstSensorExpFunc->pfn_cmos_sensor_init = sensor_init;
    pstSensorExpFunc->pfn_cmos_sensor_exit = sensor_exit;
    pstSensorExpFunc->pfn_cmos_sensor_global_init = sensor_global_init;
    pstSensorExpFunc->pfn_cmos_set_image_mode = cmos_set_image_mode;
    pstSensorExpFunc->pfn_cmos_set_wdr_mode = cmos_set_wdr_mode;
      
    pstSensorExpFunc->pfn_cmos_get_isp_default = cmos_get_isp_default;
    pstSensorExpFunc->pfn_cmos_get_isp_black_level = cmos_get_isp_black_level;
    pstSensorExpFunc->pfn_cmos_set_pixel_detect = cmos_set_pixel_detect;
    pstSensorExpFunc->pfn_cmos_get_sns_reg_info = cmos_get_sns_regs_info;
  
    return 0;
}
  
//AE function
HI_S32 cmos_init_ae_exp_function(AE_SENSOR_EXP_FUNC_S *pstExpFuncs)
{
    memset(pstExpFuncs, 0, sizeof(AE_SENSOR_EXP_FUNC_S));
  
    pstExpFuncs->pfn_cmos_get_ae_default    = cmos_get_ae_default;
    pstExpFuncs->pfn_cmos_fps_set           = cmos_fps_set;
    pstExpFuncs->pfn_cmos_slow_framerate_set= cmos_slow_framerate_set;    
    pstExpFuncs->pfn_cmos_inttime_update    = cmos_inttime_update;
    pstExpFuncs->pfn_cmos_gains_update      = cmos_gains_update;
    pstExpFuncs->pfn_cmos_again_calc_table  = cmos_again_calc_table;
    pstExpFuncs->pfn_cmos_get_inttime_max   = cmos_get_inttime_max; 
  
    return 0;
}
  
//AWB function
HI_S32 cmos_init_awb_exp_function(AWB_SENSOR_EXP_FUNC_S *pstExpFuncs)
{
    memset(pstExpFuncs, 0, sizeof(AWB_SENSOR_EXP_FUNC_S));
  
    pstExpFuncs->pfn_cmos_get_awb_default = cmos_get_awb_default;
  
    return 0;
}

HI_U8 gu8SensorImageMode = SENSOR_5M_25FPS_MODE;
WDR_MODE_E genSensorMode = WDR_MODE_2To1_LINE;

  与一般sensor设置曝光不同,HDR行间模式需要行间交替实现曝光时间设置,也是在sc5035_cmos.c中实现,具体函数如下:

static HI_VOID cmos_inttime_update(HI_U32 u32IntTime)
{
    static HI_BOOL bFirst = HI_TRUE;

    if (WDR_MODE_2To1_LINE == genSensorMode)
    {
        if (bFirst) //short exposure
        {
            au32WDRIntTime[0] = u32IntTime;
            g_stSnsRegsInfo.astI2cData[4].u32Data = (u32IntTime >> 4) & 0xFF; ;
            g_stSnsRegsInfo.astI2cData[5].u32Data = (u32IntTime<<4) & 0xF0;
            bFirst = HI_FALSE;
        }
        else //long exposure 
        {
            au32WDRIntTime[1] = u32IntTime;
            g_stSnsRegsInfo.astI2cData[0].u32Data = (u32IntTime >> 4) & 0xFF; ;
            g_stSnsRegsInfo.astI2cData[1].u32Data = (u32IntTime<<4) & 0xF0;
            bFirst = HI_TRUE;
        }
    }
    else
    {
        g_stSnsRegsInfo.astI2cData[0].u32Data = (u32IntTime >> 4) & 0xFF; ;
        g_stSnsRegsInfo.astI2cData[1].u32Data = (u32IntTime<<4) & 0xF0;
        bFirst = HI_TRUE;
    }

    return;
}


4. 修改stream软件包的.ini文件

[sensor]
Sensor_type   =sc5035                   ;sensor name
Mode          =2                        ;WDR_MODE_NONE = 0
                                        ;WDR_MODE_BUILT_IN = 1
                                        ;WDR_MODE_2To1_LINE = 2
                                        ;WDR_MODE_2To1_FRAME = 3
                                        ;WDR_MODE_2To1_FRAME_FULL_RATE =4 ...etc
DllFile   =libs/libsns_sc5035_2lan.so        ;sensor lib path
 
[mode]                               
input_mode =0                           ;INPUT_MODE_MIPI = 0
                                        ;INPUT_MODE_SUBLVDS = 1
                                        ;INPUT_MODE_LVDS = 2 ...etc
 
dev_attr = 0                             ;mipi_dev_attr_t = 0
                                        ;lvds_dev_attr_t = 1
                                        ;NULL =2
 
[mipi]    
;----------only for mipi_dev---------               
data_type = 1                         ;raw data type: 8/10/12/14 bit
                                        ;RAW_DATA_8BIT = 0
                                        ;RAW_DATA_10BIT = 1
                                        ;RAW_DATA_12BIT = 2 
                                        ;RAW_DATA_14BIT = 3 
lane_id = 0| 1 | -1 | -1 |-1|-1|-1|-1|        ;lane_id: -1 - disable
 
[isp_image]
Isp_x      =0
Isp_y      =0
Isp_W      =2560
Isp_H      =1920
Isp_FrameRate=25
Isp_Bayer  =3   ;BAYER_RGGB=0, BAYER_GRBG=1, BAYER_GBRG=2, BAYER_BGGR=3


5. 运行stream软件

  运行stream软件包中的HiIspTool.sh脚本,通过以太网卡与上位机的PQTools建立通信。

cd Hi3516A_Stream_V1.0.5.0
./HiIspTools.sh -a -p sc5035_5m_2lan.ini


6. 运行PQ Tools

  双击PQ Tools图片,弹出如下对话框,设置IP地址。

技术分享

  在下拉菜单中选择TTP_Stream.exe选项.

技术分享


7. 结果对比

  正常光照下的图片,日光灯显示模糊。

技术分享

  打开灯板后,无法捕捉宽动态范围的的场景,盒中的水果不能正常显示(偏黑),灯板的LED阵列无法识别,背景也非常昏暗。

技术分享

  开启行间HDR功能后,盒中中的水果可以清晰辨认,灯板的LED阵列也可清晰辨认,背景可见度有很大提升。

技术分享

本文出自 “shugenyin的博客” 博客,请务必保留此出处http://shugenyin.blog.51cto.com/4259554/1878262

海思Hi3516A(4)HDR模式开发