Skip to content
This repository
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 518 lines (469 sloc) 19.171 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
using Posix, ImpPrev;
#if (RRAEW)
using rraew;
#endif

[DBus (name = "com.ridgerun.dm365ipipeInterface")]
public class Ipiped_dm365 : AbstcVideoProcessor{

    /** Flag to indicate if the previewer is initialized*/
    private static bool initialized = false;

    /**
* Create a new instance of a Ipipe daemon
*/
    public Ipiped_dm365(){
        /* Initialize private variables */
        initialized = false;
        this.debug = false;
        previewer_fd = -1;
        aew_fd = -1;
        owner_previewer_fd = 1;
        owner_aew_fd = 1;
    }

    /**
* Open previewer
* Open the video processor, obtain the correspondly file
* descriptor */
    private bool open_previewer(int *previewer_fd, char *owner_previewer_fd){
        *previewer_fd = open("/dev/davinci_previewer", O_RDWR);
        if (*previewer_fd < 0) {
            Posix.stderr.printf("\n Failed to open dm365 previewer device file");
            return false;
        }
        *owner_previewer_fd = 0;
        return true;
    }

    /**
* Configure the previewer mode with the default configuration
* @param mode_s indicates if configure the previewer on continuos
* mode or single shot mode
*/
    public bool set_previewer_mode(string mode_s) throws IOError{
        ChannelConfig chan_config = ChannelConfig();
        uint mode;
        owner_previewer_fd = 0;

        if (mode_s == "cont"){
            mode = MODE_CONTINUOUS;
        } else if (mode_s == "one-shot"){
            mode = MODE_SINGLE_SHOT;
        } else {
            Posix.stderr.printf("Ipiped: Invalid previewer operation mode\n");
            return false;
        }

        if (initialized){
            if (debug)
                Posix.stderr.printf("Ipiped:Previewer is already configured\n");
            return true;
        }

        previewer_fd = Posix.open("/dev/davinci_previewer", Posix.O_RDWR);
        if (previewer_fd <0)
            return false;
        /*Set operation mode*/
        if (Posix.ioctl(previewer_fd , PREV_S_OPER_MODE, &mode)<0) {
            if (debug)
                Posix.stderr.printf("Ipiped:Fail to set operation mode\n");
            return false;
        }
        /*Set default configuration*/
        chan_config.oper_mode = mode;
        chan_config.len = 0;
        chan_config.config = null;
        set_previewer_config(&chan_config);
        initialized = true;
        return true;
    }

    /* Set previewer config
* Call the ioctl PREV_S_CONFIG to configure the previewer
*/
    private bool set_previewer_config(ChannelConfig* chan_config){
        if (Posix.ioctl(previewer_fd , PREV_S_CONFIG, chan_config)<0) {
            if (debug)
                Posix.stderr.printf("Ipiped:Fail to set configuration\n");
            return false;
        }
        return true;
    }

    /* Get previewer config
* Call the ioctl PREV_G_CONFIG to get the actual previewer
* configuration
*/
    private bool get_previewer_config(ChannelConfig* chan_config){
        if (Posix.ioctl(previewer_fd , PREV_G_CONFIG, chan_config)<0) {
            if (debug)
                Posix.stderr.printf("Ipiped:Fail to get configuration\n");
            return false;
        }
        return true;
    }

    /**
* Set color pattern
* Configure in the previewer the color pattern passed by the user
* @param colorptn is a string that represents the RGB color order*/
    public bool set_color_pattern(string colorptn) throws IOError{
        ChannelConfig chan_config = ChannelConfig();
        PreContinuosConfig prev_config= PreContinuosConfig();
        chan_config.oper_mode = MODE_CONTINUOUS;
        chan_config.len =(ushort)sizeof(PreContinuosConfig);
        chan_config.config = &prev_config;
        get_previewer_config(&chan_config);
        if (colorptn == "GrRBGb"){
            /** Gr R
* B Gb
*/
            prev_config.input.colp_olop = IpipeColpat_t.GREEN_RED;
            prev_config.input.colp_olep = IpipeColpat_t.RED;
            prev_config.input.colp_elop = IpipeColpat_t.BLUE;
            prev_config.input.colp_elep = IpipeColpat_t.GREEN_BLUE;
        } else if (colorptn == "BGbGrR") {
            /** B Gb
* Gr R
*/
            prev_config.input.colp_olop = IpipeColpat_t.BLUE;
            prev_config.input.colp_olep = IpipeColpat_t.GREEN_BLUE;
            prev_config.input.colp_elop = IpipeColpat_t.GREEN_RED;
            prev_config.input.colp_elep = IpipeColpat_t.RED;
        } else if (colorptn == "RGrGbB") {
            /** R Gr
* Gb B
*/
            prev_config.input.colp_olop = IpipeColpat_t.RED;
            prev_config.input.colp_olep = IpipeColpat_t.GREEN_RED;
            prev_config.input.colp_elop = IpipeColpat_t.GREEN_BLUE;
            prev_config.input.colp_elep = IpipeColpat_t.BLUE;
        } else if (colorptn == "GbBRGr"){
            /** Gb B
* R Gr
*/
            prev_config.input.colp_olop = IpipeColpat_t.GREEN_BLUE;
            prev_config.input.colp_olep = IpipeColpat_t.BLUE;
            prev_config.input.colp_elop = IpipeColpat_t.RED;
            prev_config.input.colp_elep = IpipeColpat_t.GREEN_RED;
        }else{
            Posix.stderr.printf("Ipiped: Invalid bayer pattern\n");
            return false;
        }
        set_previewer_config(&chan_config);
        return true;
    }

   /* Convertion gain from q10
* Converts the gain from a int on fixed-point q10 into 2 ints, the integer
* part and the decimal part(9 bits)
*/
    private void convertion_gains_from_q10(uint32 gain, int *decimal_gain, int *integer_gain)
    {
        *integer_gain = (int) (gain >> 10);
        *decimal_gain = (int) ((gain & 0x3ff) >> 1);
        return;
    }

    /* Convertion gain to q10
* Converts a number separate on an integer and decimal parts into a int on
* fixed point
*/
    private void
    convertion_gains_to_q10(uint32 *gain, int decimal_gain, int integer_gain)
    {
        *gain = ((decimal_gain & 0x3ff)<<1)| ((integer_gain & 0xf)<<10);
        return;
    }

    /**
* Sets digital gain in the IPIPE, adjust a ratio gain of each color
* in the pattern(RGB)
* Each gain component can range from 0 to 15.998 in steps of 1/512
* @param fd structure with file descriptors and owner flags
* @param q10r_gain, in white balance module value for red component[0, 15.998]
* @param q10g_gain, in white balance module value for green component[0, 15.998]
* @param q10b_gain, in white balance module value for blue component[0, 15.998]
*/
    private bool set_digital_gain_i(int *previewer_fd, char *owner_previewer_fd,
        uint32 q10r_gain, uint32 q10g_gain, uint32 q10b_gain)
    {
        ModuleParam mod_param = ModuleParam();
        Cap cap = Cap();
        WhiteBalance wb = WhiteBalance();

        int gain_r_integer = 0, gain_g_integer = 0, gain_b_integer = 0;
        int gain_r_decimal = 0, gain_g_decimal = 0, gain_b_decimal = 0;

        if (q10r_gain > 0x3FFF)
            q10r_gain = 0x3FFF;
        if (q10g_gain > 0x3FFF)
            q10g_gain = 0x3FFF;
        if (q10b_gain > 0x3FFF)
            q10b_gain = 0x3FFF;

        convertion_gains_from_q10(q10r_gain, &gain_r_decimal, &gain_r_integer);
        convertion_gains_from_q10(q10g_gain, &gain_g_decimal, &gain_g_integer);
        convertion_gains_from_q10(q10b_gain, &gain_b_decimal, &gain_b_integer);

        cap.index = 0;
        if (*previewer_fd < 0) {
            if (!open_previewer(previewer_fd, owner_previewer_fd))
                return false;
        }
        /* Set operation mode */
        if (Posix.ioctl(*previewer_fd, PREV_ENUM_CAP, &cap) < 0) {
            Posix.stderr.printf("\n Failed to get previewer capabilities\n");
            return false;
        }

        cap.module_id = PREV_WB;
        strcpy(mod_param.version, cap.version);
        mod_param.module_id = cap.module_id;
        /*Sets red gain */
        wb.gain_r.integer = (ushort) gain_r_integer;
        wb.gain_r.decimal = (ushort) gain_r_decimal;
        /*Sets green-red gain */
        wb.gain_gr.integer = (ushort) gain_g_integer;
        wb.gain_gr.decimal = (ushort) gain_g_decimal;
        /*Sets green-blue gain */
        wb.gain_gb.integer = (ushort) gain_g_integer;
        wb.gain_gb.decimal = (ushort) gain_g_decimal;
        /*Sets blue gain */
        wb.gain_b.integer = (ushort) gain_b_integer;
        wb.gain_b.decimal = (ushort) gain_b_decimal;
        /*Sets offsets to zero */
        wb.ofst_r = 0;
        wb.ofst_gr = 0;
        wb.ofst_gb = 0;
        wb.ofst_b = 0;

        mod_param.len = (uint8)sizeof(WhiteBalance);
        mod_param.param = &wb;

        if (Posix.ioctl(*previewer_fd, PREV_S_PARAM, &mod_param) < 0) {
            Posix.stderr.printf("Error setting params to the previewer\n");
            if (*owner_previewer_fd == 0) {
                close(*previewer_fd);
            }
            return false;
        }
        if (debug){
            Posix.stdout.printf("Set red gain to %f\n", (float) q10r_gain / 1024);
            Posix.stdout.printf("Set green gain to %f\n", (float) q10g_gain / 1024);
            Posix.stdout.printf("Set blue gain to %f\n", (float) q10b_gain / 1024);
        }
        return true;
    }

    /**
* Sets digital gain
*/
    public bool set_digital_gain(double _red_gain, double _green_gain,
        double _blue_gain) throws IOError{

        uint32 q10r_gain = (uint32)((_red_gain + 1/512) * 1024);
        uint32 q10g_gain = (uint32)((_green_gain + 1/512) * 1024);
        uint32 q10b_gain = (uint32)((_blue_gain+ 1/512) * 1024);

        return set_digital_gain_i(&this.previewer_fd, &this.owner_previewer_fd,
            q10r_gain, q10g_gain, q10b_gain);
    }

    /** Get the gain of each color component (RGB) of the white balance module. The
* returned gain values are on fixed-point Q10
* Each gain component can range on float point from 0 to 15.998 in steps of 1/512
* @param fd structure with file descriptors and owner flags
* @param q10r_gain, in white balance module value for red component[0, 15.998]
* @param q10g_gain, in white balance module value for green component[0, 15.998]
* @param q10b_gain, in white balance module value for blue component[0, 15.998]
*/
    private bool get_digital_gain_i(int *previewer_fd, char *owner_previewer_fd,
        uint32 *red_gain, uint32 *green_gain, uint32 *blue_gain)
    {
        ModuleParam mod_param = ModuleParam();
        WhiteBalance wb = WhiteBalance();
        Cap cap = Cap();
        cap.index = 0;
        if (*previewer_fd < 0) {
            if (!open_previewer(previewer_fd, owner_previewer_fd))
                return false;
        }
        /* Set operation mode */
        if (Posix.ioctl(*previewer_fd, PREV_ENUM_CAP, &cap) < 0) {
            Posix.stderr.printf("\n Failed to get previewer capabilities\n");
            return false;
        }
        strcpy(mod_param.version, cap.version);
        mod_param.module_id = PREV_WB;
        mod_param.len = (uint8)sizeof(WhiteBalance);
        mod_param.param = &wb;

        if (Posix.ioctl(*previewer_fd, PREV_G_PARAM, &mod_param) < 0) {
            Posix.stderr.printf("Ipiped: Error in Setting params from driver\n");
            return false;
        }
        convertion_gains_to_q10(red_gain, wb.gain_r.decimal, wb.gain_r.integer);
        convertion_gains_to_q10(green_gain, wb.gain_gr.decimal,
            wb.gain_gr.integer);
        convertion_gains_to_q10(blue_gain, wb.gain_b.decimal,
            wb.gain_b.integer);
        return true;
    }

    /**
* Gets digital gain
*/
    public bool get_digital_gain(out double _red_gain, out double _green_gain,
        out double _blue_gain) throws IOError{
        uint32 q10red_gain=0, q10green_gain=0, q10blue_gain=0;

        if (!get_digital_gain_i(&this.previewer_fd, &this.owner_previewer_fd,
                   &q10red_gain, &q10green_gain, &q10blue_gain)) {
            Posix.stderr.printf("Error:\n Failed to get the digital gain\n");
            return false;
        } else {
            _red_gain = ((double)(q10red_gain))/1024;
            _green_gain= ((double)(q10green_gain))/1024;
            _blue_gain = ((double)(q10blue_gain))/1024;
            return true;
        }
    }

    /**
* Sets luminance adjustment
* Sets the values for the brightness and the contrast on the ipipe's
* luminance adjusment module.
* @param bright brightness Offset value for brightness control(U8 = 0 - +255)
* @param contr contrast(U4.4 = 0 - +15.94)
*/
    public bool set_luminance_adj(int bright, double contr) throws IOError{
        ModuleParam mod_param = ModuleParam();
        LumAdj lum_adj = LumAdj();
        Cap cap = Cap();
        cap.index =0 ;
        uint8 q4contr;

        if (this.previewer_fd < 0) {
            if (!open_previewer(&this.previewer_fd, &this.owner_previewer_fd))
                return false;
        }
        if (Posix.ioctl(this.previewer_fd , PREV_ENUM_CAP, &cap)<0) {
            if (debug)
                Posix.stderr.printf("Ipiped:Error in Setting cap from driver\n");
            return false;
        }

        cap.module_id = PREV_LUM_ADJ;
        strcpy(mod_param.version,cap.version);
        mod_param.module_id = cap.module_id;

        if (bright > 0xFF){
            if (debug)
                Posix.stdout.printf("Ipiped:Brightness value is greater than" +
                " 255. 255 will be written\n");
            bright = 0xFF;
        }
        if (contr > 15.94){
            if (debug)
                Posix.stdout.printf("Ipiped:Brightness value is greater than" +
                " 15.94. 15.94 will be written\n");
            contr = 15.94;
        }
        q4contr = (uint8)(contr * 16);
        lum_adj.brightness =(uchar)bright;
        lum_adj.contrast =(uchar)q4contr;

        mod_param.len =(uint8)sizeof(LumAdj);
        mod_param.param = &lum_adj;

        if (Posix.ioctl(this.previewer_fd , PREV_S_PARAM, &mod_param) < 0) {
            if (debug)
                Posix.stderr.printf("Ipiped:Error in Setting params from driver\n");
            Posix.close(this.previewer_fd );
            return false;
        }
        if (debug)
            Posix.stdout.printf("Set luminance adjustment to Brightness=%d," +
            " Contrast=%f\n",bright,contr);
        return true;
    }

    /**
* Gets luminance adjustment
* Gets the values for the brightness and the contrast on the ipipe's
* luminance adjusment module
*/
    public bool get_luminance_adj(out int bright, out double contr) throws IOError{
        ModuleParam mod_param = ModuleParam();
        LumAdj lum_adj = LumAdj();
        Cap cap = Cap();
        cap.index =0 ;

        if (this.previewer_fd < 0) {
            if (!open_previewer(&this.previewer_fd, &this.owner_previewer_fd))
                return false;
        }

        if (Posix.ioctl(this.previewer_fd , PREV_ENUM_CAP, &cap)<0) {
            if (debug)
                Posix.stderr.printf("Ipiped:Error in getting cap from driver\n");
            return false;
        }
        strcpy(mod_param.version,cap.version);
        mod_param.module_id = PREV_LUM_ADJ;
        mod_param.len =(uint8)sizeof(LumAdj);
        mod_param.param = &lum_adj;

        if (Posix.ioctl(this.previewer_fd , PREV_G_PARAM, &mod_param) < 0){
            if (debug)
                Posix.stderr.printf("Ipiped:Error in getting params from driver\n");
            Posix.close(this.previewer_fd );
            return false;
        }

        bright = lum_adj.brightness;
        contr = ((double)lum_adj.contrast)/16;

        return true;
    }

#if BSC_ENABLE
    /**
* Sets Boundary Signal Calculator parameters
*/
    public bool config_bsc(uint inwidth, uint inheight) throws IOError{
        ModuleParam mod_param = ModuleParam();
        PrevBSC bsc_mod = PrevBSC();
        Cap cap = Cap();
        uint rowlh =0, collv=0;
        cap.index =0 ;

        if (Posix.ioctl(this.previewer_fd , PREV_ENUM_CAP, &cap)<0) {
            if (debug)
                Posix.stderr.printf("Ipiped:Error in getting cap from driver\n");
            return false;
        }
        /* Enable bsc in continous mode, using the Y component */
        bsc_mod.en = 1;
        bsc_mod.mode = 0;
        bsc_mod.col_en = 1;
        bsc_mod.row_en = 1;
        bsc_mod.y_cb_cr = IpipeBscIn.Y;
        /* Using the maximun number of vectors and try to cover the
* major image area */

        /*Calculating skip, we are using 4 vectors (vct=3)*/
        uint hskip = (4 * inwidth)/1920;
        uint vskip = (4 * inheight)/1920;
  
        bsc_mod.row_vct = 3;
        bsc_mod.row_shf = 0;
        bsc_mod.row_vpos = 0;
        bsc_mod.row_vskip = vskip;
        bsc_mod.row_vnum = inheight/(bsc_mod.row_vskip + 1) - 1;
        bsc_mod.row_hpos = 0;
        rowlh = inwidth/(bsc_mod.row_vct + 1);
        bsc_mod.row_hskip = hskip;
        bsc_mod.row_hnum = rowlh / (bsc_mod.row_vskip + 1) - 1;

        bsc_mod.col_vct = 3;
        bsc_mod.col_shf = 0;
        bsc_mod.col_hpos = 0;
        bsc_mod.col_hskip = hskip;
        bsc_mod.col_hnum = inwidth/(bsc_mod.col_hskip + 1) - 1;
        bsc_mod.col_vpos = 0;
        collv = inheight/(bsc_mod.col_vct + 1);
        bsc_mod.col_vskip = vskip;
        bsc_mod.col_vnum = collv / (bsc_mod.col_hskip + 1) - 1;

        cap.module_id = PREV_BSC;
        strcpy(mod_param.version,cap.version);
        mod_param.module_id = cap.module_id;
        mod_param.len =(uint8)sizeof(PrevBSC);
        mod_param.param = &bsc_mod;

        if (Posix.ioctl(this.previewer_fd , PREV_S_PARAM, &mod_param) < 0) {
            if (debug)
                Posix.stderr.printf("Ipiped:Error in Setting params to driver\n");
            Posix.close(this.previewer_fd );
            return false;
        }

        if (debug) {
            Posix.stdout.printf(" \nIPIPE BSC Info,\n");
            Posix.stdout.printf(" inwidth = %u,\n", inwidth);
            Posix.stdout.printf(" inheight = %u,\n", inheight);
            Posix.stdout.printf(" rowNumV = %u,\n", bsc_mod.row_vnum);
            Posix.stdout.printf(" rowSkipV = %u,\n", bsc_mod.row_vskip);
            Posix.stdout.printf(" rowNumH = %u,\n", bsc_mod.row_hnum);
            Posix.stdout.printf(" rowSkipH = %u,\n", bsc_mod.row_hskip);
            Posix.stdout.printf(" colNumVectors = %u,\n", bsc_mod.col_vct);
            Posix.stdout.printf(" colNumV = %u,\n", bsc_mod.col_vnum);
            Posix.stdout.printf(" colSkipV = %u,\n", bsc_mod.col_vskip);
            Posix.stdout.printf(" colNumH = %u,\n", bsc_mod.col_hnum);
            Posix.stdout.printf(" colSkipH = %u,\n\n", bsc_mod.col_hskip);
        }
        return true;
    }
#endif

#if (RRAEW)
    public override bool get_video_processor_data(RraewInterface *interf){
        *interf = dm365_vpfe_interface;
        return true;
    }
#endif
}
Something went wrong with that request. Please try again.