root/core/autoiso.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. live_histogram_read_y
  2. live_histogram_get_range
  3. shooting_calc_autoiso_coef
  4. shooting_recalc_conf_autoiso_values
  5. shooting_set_autoiso

   1 #include "stdlib.h"
   2 #include "camera_info.h"
   3 #include "math.h"
   4 #include "conf.h"
   5 #include "viewport.h"
   6 #include "shooting.h"
   7 #include "modes.h"
   8 #include "lens.h"
   9 
  10 //-------------------------------------------------------------------
  11 // AUTO ISO
  12 
  13 //////////////////////////////////////////////////////////////////////////////////////////////
  14 // @tsv
  15 // Module below calculate live histogram in same way as OSD histogram
  16 // Difference from shot_histogram family is that live_histogram give answer before shot
  17 // Regular histogram_process cannot be used, because raw non-summarized 0.255 values required
  18 // This module is used in AutoISO2 mechanizm.
  19 //////////////////////////////////////////////////////////////////////////////////////////////
  20 
  21 
  22 
  23 #ifdef THUMB_FW
  24 // Define how many viewport blocks to step in each loop iteration.
  25 // digic use 2x larger to keep overall number similar to older cams
  26 #define HISTO_STEP_SIZE 12
  27 // Digic 6: Each block is 4 bytes (UYVY) 2 Y values
  28 #define HISTO_BLOCK_BYTES 4
  29 #else
  30 // Define how many viewport blocks to step in each loop iteration.
  31 #define HISTO_STEP_SIZE 6
  32 // Each block is 6 bytes (UYVYYY) / 4 Y values
  33 #define HISTO_BLOCK_BYTES 6
  34 #endif
  35 
  36 // Coincidentally same value for both digic 6 and earlier
  37 // D6 viewport_width* is specified as the number of Y values, each block 2 Y values
  38 // pre-D6, width is half the number of Y values, each block contains 4 y values
  39 #define HISTO_BLOCK_SCALE 2
  40 
  41 static unsigned short live_histogram_proc[256]; // Buffer for histogram
  42 
  43 /*
  44 build histogram of viewport Y values (downsampled by HISTO_STEP_SIZE)
  45 NOTE also used by lua get_live_histo
  46 */
  47 int live_histogram_read_y(unsigned short *h)
  48 {
  49     int total;
  50 
  51     int vp_width = vid_get_viewport_width();
  52     int vp_height = vid_get_viewport_height();
  53     int vp_offset = vid_get_viewport_row_offset();
  54 
  55     total = (vp_width * vp_height) / (HISTO_STEP_SIZE * HISTO_BLOCK_SCALE);
  56     memset(h, 0, sizeof(unsigned short)*256);
  57 
  58     unsigned char *img = vid_get_viewport_active_buffer();
  59     if (!img) return total;
  60 
  61     img += vid_get_viewport_image_offset() + 1;
  62 
  63     int y;
  64     for (y=0; y<vp_height; y++, img += vp_offset)
  65     {
  66         int x;
  67         for (x=0; x<vp_width; x += HISTO_STEP_SIZE*HISTO_BLOCK_SCALE, img+=HISTO_STEP_SIZE*HISTO_BLOCK_BYTES)
  68         {
  69             ++h[*img];
  70         }
  71     }
  72 
  73     return total;
  74 }
  75 
  76 static int live_histogram_get_range(int total,int from, int to)
  77 {
  78     if (from < 0) from = 0;
  79     if (to > 255) to = 255;
  80 
  81     int rv = 0;
  82     for(; from<=to; from++)
  83         rv += live_histogram_proc[from];
  84 
  85     return (rv * 100) / total;
  86 }
  87 
  88 //-------------------------------------------------------------------
  89 
  90 static const int shutter1_values[] = { 0, 2, 4, 6, 8, 15, 30, 60, 125, 250, 500, 1000, 2000 };
  91 static const int shutter2_values[] = { 0, 1, 2, 4, 6, 8, 12, 15, 20, 25, 30, 40, 50, 60, 80, 100, 125, 160, 200, 250, 500, 1000, 2000 };
  92 
  93 static void shooting_calc_autoiso_coef( int min_shutter )
  94 {
  95     if (shutter2_values[conf.autoiso2_shutter_enum] >= min_shutter)
  96     {
  97         conf.autoiso2_coef = 0.0;
  98     }
  99     else
 100     {
 101         conf.autoiso2_coef = (float)(conf.autoiso2_max_iso_auto_real - conf.autoiso_max_iso_auto_real) / 
 102             (float)( shutter2_values[conf.autoiso2_shutter_enum] - min_shutter);
 103     }
 104 }
 105 
 106 static void shooting_recalc_conf_autoiso_values()
 107 {
 108     // convert market to real iso
 109     conf.autoiso_max_iso_hi_real    = shooting_iso_market_to_real(conf.autoiso_max_iso_hi) ;
 110     conf.autoiso_max_iso_auto_real  = shooting_iso_market_to_real(conf.autoiso_max_iso_auto) ; 
 111     conf.autoiso_min_iso_real       = shooting_iso_market_to_real(conf.autoiso_min_iso) ;      
 112     conf.autoiso2_max_iso_auto_real = shooting_iso_market_to_real(conf.autoiso2_max_iso_auto) ;
 113 
 114     // There are two exceptional situation: 
 115     // 1. shutter_numerator2 should be < shutter_numerator1, otherwise exceptional situation 
 116     // 2. autoiso2 <= autoiso1
 117     if ( !shutter2_values[conf.autoiso2_shutter_enum] )
 118         conf.autoiso2_max_iso_auto_real = conf.autoiso_max_iso_auto_real;
 119 
 120     // C2=( iso2_max_auto_real - iso_max_auto_real) / ( tv_num[autoiso2_shutter] - tv_numerator[autoiso_shutter])
 121     shooting_calc_autoiso_coef( shutter1_values[conf.autoiso_shutter_enum] );
 122 }
 123 
 124 void shooting_set_autoiso(int iso_mode)
 125 {
 126     short max_iso;
 127 
 128     if (iso_mode<=0)
 129         shooting_recalc_conf_autoiso_values();
 130 
 131     switch (iso_mode)
 132     {
 133     case -1: // ISO HI
 134         //max_iso = conf.autoiso_max_iso_hi*10;
 135         max_iso = conf.autoiso_max_iso_hi_real;
 136         break;
 137     case 0: // ISO AUTO
 138         //max_iso = conf.autoiso_max_iso_auto*10;
 139         max_iso = conf.autoiso_max_iso_auto_real;
 140         break;
 141     default:
 142         return;
 143     }
 144 
 145     // TODO also long shutter ?
 146     if (camera_info.state.mode_shooting==MODE_M || camera_info.state.mode_shooting==MODE_TV || camera_info.state.mode_shooting==MODE_STITCH)
 147         return; //Only operate outside of M and Tv
 148 
 149     int ev_overexp = 0;
 150     if (conf.overexp_ev_enum)
 151     {
 152         // No shoot_histogram exist here because no future shot exist yet :)
 153         int total = live_histogram_read_y(live_histogram_proc);
 154 
 155         // step 32 is 1/3ev for tv96
 156         if (live_histogram_get_range(total,255-conf.autoiso2_over,255) >= conf.overexp_threshold)
 157             ev_overexp = conf.overexp_ev_enum << 5; 
 158     }
 159 
 160     float current_shutter = shooting_get_shutter_speed_from_tv96(shooting_get_tv96());
 161 
 162     short current_iso = shooting_get_iso_real();
 163 
 164     short min_shutter = shutter1_values[conf.autoiso_shutter_enum];
 165     if (min_shutter == 0)
 166     {
 167         short IS_factor = (shooting_get_is_mode()<=1)?conf.autoiso_is_factor:1;
 168         min_shutter = get_focal_length(lens_get_zoom_point())*conf.autoiso_user_factor / (IS_factor*1000);
 169         //min_shutter is NOT 1/Xs but optimized for the calculation.
 170         if (shutter2_values[conf.autoiso2_shutter_enum])
 171             shooting_calc_autoiso_coef( min_shutter );
 172     }
 173 
 174     short target_iso = current_iso * min_shutter * current_shutter;
 175     short min_iso = conf.autoiso_min_iso_real;
 176 
 177     if (target_iso > max_iso)
 178     {
 179         ev_overexp=0;
 180 
 181         // AutoISO2 if
 182         //      it is turned on (C2!=0.0)
 183         //      and it has valid iso2/shutter2 ( C2<0)
 184         //       and non-IsoHI mode
 185         if ( !iso_mode && conf.autoiso2_coef < 0.0 )
 186         {
 187             target_iso = (max_iso - min_shutter*conf.autoiso2_coef) / ( 1.0 - conf.autoiso2_coef  / (current_shutter * current_iso) );
 188             if ( target_iso > conf.autoiso2_max_iso_auto_real )
 189                 target_iso = conf.autoiso2_max_iso_auto_real;
 190         }
 191         else
 192         {
 193             target_iso = max_iso;
 194         }
 195     }
 196     else if (target_iso < min_iso)
 197     {
 198         target_iso = min_iso;
 199     }
 200 
 201     float target_shutter = current_shutter *  current_iso / target_iso;
 202 
 203         if (target_shutter > 0)
 204         shooting_set_tv96_direct(shooting_get_tv96_from_shutter_speed(target_shutter) + ev_overexp, SET_NOW);
 205 
 206     shooting_set_iso_real(target_iso, SET_NOW);
 207 }
 208 
 209 //-------------------------------------------------------------------

/* [<][>][^][v][top][bottom][index][help] */