root/core/shooting.c

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

DEFINITIONS

This source file includes following definitions.
  1. shooting_get_prop
  2. shooting_get_prop_int
  3. shooting_set_prop
  4. shooting_get_is_mode
  5. shooting_get_bv96
  6. shooting_get_canon_overexposure_value
  7. shooting_get_flash_mode
  8. shooting_get_focus_mode
  9. shooting_get_ev_correction1
  10. shooting_get_ev_correction2
  11. shooting_get_resolution
  12. shooting_get_display_mode
  13. shooting_get_exif_subject_dist
  14. shooting_is_flash
  15. shooting_in_progress
  16. shooting_get_canon_raw_enabled
  17. shooting_get_imager_active
  18. shooting_get_digital_zoom_mode
  19. shooting_get_digital_zoom_state
  20. shooting_get_real_focus_mode
  21. shooting_get_focus_state
  22. shooting_get_focus_ok
  23. shooting_get_drive_mode
  24. shooting_get_zoom
  25. shooting_get_canon_iso_mode
  26. shooting_get_sv96_market
  27. shooting_get_sv96_real
  28. shooting_get_sv96_delta
  29. shooting_get_sv96_from_iso
  30. shooting_get_iso_from_sv96
  31. shooting_iso_market_to_real
  32. shooting_iso_real_to_market
  33. shooting_sv96_market_to_real
  34. shooting_sv96_real_to_market
  35. shooting_get_iso_override_value
  36. shooting_get_iso_mode
  37. shooting_get_iso_real
  38. shooting_get_iso_market
  39. set_iso_mode
  40. shooting_set_iso_mode
  41. shooting_set_sv96
  42. shooting_set_iso_real
  43. iso_init
  44. shooting_get_tv96
  45. shooting_get_tv96_from_shutter_speed
  46. shooting_get_shutter_speed_from_tv96
  47. shooting_get_tv96_override_value
  48. find_nearest_shutter_speed_entry
  49. find_canon_shutter_speed
  50. shooting_set_tv96_direct
  51. shooting_set_tv96
  52. shooting_get_user_tv96
  53. shooting_get_user_tv_id
  54. shooting_set_user_tv_by_id
  55. shooting_set_user_tv_by_id_rel
  56. shooting_set_user_tv96
  57. shooting_get_tv_str
  58. shooting_get_av96
  59. shooting_get_aperture_sizes_table_size
  60. shooting_get_aperture_from_av96
  61. shooting_get_av96_from_aperture
  62. shooting_get_av96_override_value
  63. shooting_get_real_aperture
  64. shooting_get_min_real_aperture
  65. find_nearest_aperture_entry
  66. find_canon_aperture
  67. shooting_set_av96_direct
  68. shooting_set_av96
  69. shooting_get_user_av96
  70. shooting_get_user_av_id
  71. shooting_set_user_av_by_id
  72. shooting_set_user_av_by_id_rel
  73. shooting_set_user_av96
  74. shooting_get_av_str
  75. shooting_set_nd_filter_state
  76. shooting_get_min_av96
  77. shooting_get_max_av96
  78. shooting_init
  79. get_file_counter
  80. get_exposure_counter
  81. get_file_counter
  82. get_exposure_counter
  83. get_target_dir_name
  84. shooting_get_luminance
  85. shooting_get_subject_distance_override_value
  86. shooting_mode_canon2chdk
  87. shooting_mode_chdk2canon
  88. sd_over_modes
  89. shooting_get_lens_to_focal_plane_width
  90. shooting_get_hyperfocal_distance_1e3_f
  91. shooting_get_near_limit_f
  92. shooting_get_far_limit_f
  93. shooting_is_infinity_distance
  94. shooting_get_canon_subject_distance
  95. shooting_get_subject_distance_
  96. shooting_update_dof_values
  97. shooting_get_subject_distance
  98. shooting_get_hyperfocal_distance
  99. shooting_can_focus
  100. shooting_get_common_focus_mode
  101. shooting_is_flash_ready
  102. shooting_set_image_quality
  103. shooting_set_zoom
  104. shooting_set_zoom_rel
  105. shooting_set_zoom_speed
  106. set_focus_bypass
  107. shooting_set_focus
  108. shooting_video_bitrate_change
  109. shooting_video_minbitrate_change
  110. shooting_get_video_recorded_size_kb
  111. shooting_set_flash_sync_curtain
  112. unlock_optical_zoom
  113. shooting_set_mode_canon
  114. shooting_set_mode_chdk
  115. shooting_tv_bracketing
  116. shooting_av_bracketing
  117. shooting_iso_bracketing
  118. shooting_subject_distance_bracketing
  119. bracketing_reset
  120. bracketing_step
  121. shooting_bracketing
  122. captseq_hack_override_active
  123. set_movie_status
  124. get_movie_status
  125. is_video_recording
  126. mode_is_video
  127. rec_mode_active
  128. mode_get
  129. get_ev_video_avail
  130. get_ev_video
  131. set_ev_video_avail
  132. set_ev_video
  133. shooting_set_flash_override
  134. shooting_expo_param_override_thumb
  135. shooting_expo_iso_override_thumb
  136. shooting_set_playrec_mode

   1 #include "platform.h"
   2 #include "keyboard.h"
   3 #include "math.h"
   4 #include "stdlib.h"
   5 #include "conf.h"
   6 #include "histogram.h"
   7 #include "usb_remote.h"
   8 #include "autoiso.h"
   9 #include "levent.h"
  10 #include "script_api.h"
  11 
  12 // Shooting function that don't need to be ARM code
  13 // ARM code shooting functions are in platform/generic/shooting.c
  14 
  15 // This file needs more comments / documentation !!!!
  16 
  17 //-------------------------------------------------------------------
  18 // Forward references
  19 
  20 // returns 0 if in play, nonzero if rec
  21 int rec_mode_active(void); 
  22 
  23 //-------------------------------------------------------------------
  24 // From lolevel.h
  25 extern volatile long focus_busy;
  26 extern long playrec_mode;
  27 
  28 // Array & sizes stored in platform/generic/shooting.c
  29 extern const ShutterSpeed shutter_speeds_table[];
  30 extern const unsigned int SS_SIZE;
  31 extern const ApertureSize aperture_sizes_table[];
  32 extern const unsigned int AS_SIZE;
  33 extern const CapturemodeMap modemap[];
  34 extern const unsigned int MODESCNT;
  35 
  36 static short *min_av96_zoom_point_tbl = NULL;
  37 
  38 // Storage for delayed shooting overrides
  39 #define PHOTO_PARAM_TV_NONE 32767 // ~ 1/(2^341) seconds, safe marker for "no value"
  40 
  41 typedef struct {
  42     short av96;
  43     short tv96;
  44     short sv96;
  45     short subj_dist;
  46     short nd_filter;
  47 } PHOTO_PARAM;
  48 
  49 static PHOTO_PARAM photo_param_put_off;
  50 
  51 //-------------------------------------------------------------------
  52 // Convert values to/from APEX 96
  53 
  54 //static const double log_2 = 0.6931471805599;      // natural logarithm of 2
  55 static const double inv_log_2 = 1.44269504088906;   // 1 / log_2
  56 
  57 //-------------------------------------------------------------------
  58 // Functions to access Canon properties
  59 
  60 int shooting_get_prop(int id)
  61 {
  62     short vv;
  63     get_property_case(id, &vv, sizeof(vv));
  64     return vv;
  65 }
  66 
  67 static int shooting_get_prop_int(int id)
  68 {
  69     int vv;
  70     get_property_case(id, &vv, sizeof(vv));
  71     return vv;
  72 }
  73 
  74 void shooting_set_prop(int id, int v)
  75 {
  76     short vv = v;
  77     set_property_case(id, &vv, sizeof(vv));
  78     return;
  79 }
  80 
  81 short shooting_get_is_mode()                    { return shooting_get_prop(PROPCASE_IS_MODE); }
  82 short shooting_get_bv96()                       { return shooting_get_prop(PROPCASE_BV); }
  83 short shooting_get_canon_overexposure_value()   { return shooting_get_prop(PROPCASE_OVEREXPOSURE); }
  84 short shooting_get_flash_mode()                 { return shooting_get_prop(PROPCASE_FLASH_MODE); }
  85 short shooting_get_focus_mode()                 { return shooting_get_prop(PROPCASE_FOCUS_MODE); }
  86 short shooting_get_ev_correction1()             { return shooting_get_prop(PROPCASE_EV_CORRECTION_1); }
  87 short shooting_get_ev_correction2()             { return shooting_get_prop(PROPCASE_EV_CORRECTION_2); }
  88 short shooting_get_resolution()                 { return shooting_get_prop(PROPCASE_RESOLUTION); }
  89 short shooting_get_display_mode()               { return shooting_get_prop(PROPCASE_DISPLAY_MODE); }
  90 
  91 int   shooting_get_exif_subject_dist()          { return shooting_get_prop_int(PROPCASE_SUBJECT_DIST1); }
  92 int   shooting_is_flash()                       { return shooting_get_prop_int(PROPCASE_IS_FLASH_READY); }
  93 int   shooting_in_progress()                    { return shooting_get_prop_int(PROPCASE_SHOOTING); }
  94 
  95 // TODO should maybe jpeg, raw, raw+jpeg?
  96 int shooting_get_canon_raw_enabled() {
  97 #ifdef CAM_HAS_CANON_RAW
  98 #ifdef PROPCASE_IMAGE_FORMAT
  99     return (shooting_get_prop(PROPCASE_IMAGE_FORMAT) != 1); // 0 = raw, 2 = raw+jpeg
 100 #else
 101     return (shooting_get_prop(PROPCASE_RESOLUTION) == 5); // resolution 5 = raw, jpeg controlled with another prop
 102 #endif
 103 #else
 104     return 0;
 105 #endif
 106 }
 107 
 108 int shooting_get_imager_active() {
 109   extern int imager_active;
 110   return imager_active;
 111 }
 112 
 113 // translate digital zoom propcase values to match pre-propset 7 values
 114 // mode: 0 = off or standard digital zoom, 2 or 3 digital tele
 115 // PROPCASE_DIGITAL_ZOOM_MODE not defined for propset 1, ports configured to not need these functions
 116 #if CAM_PROPSET > 1
 117 int shooting_get_digital_zoom_mode(void)
 118 {
 119     int x=shooting_get_prop(PROPCASE_DIGITAL_ZOOM_MODE);
 120 #if CAM_PROPSET == 7 || CAM_PROPSET == 9 || CAM_PROPSET == 10|| CAM_PROPSET == 11
 121     if(x==1) {
 122         return 0;
 123     }
 124 #endif
 125     return x;
 126 }
 127 #endif // CAM_PROPSET > 1
 128 // state: 0 = off or digital tele, 1 = standard
 129 int shooting_get_digital_zoom_state(void)
 130 {
 131 #if CAM_PROPSET == 7 || CAM_PROPSET == 9 || CAM_PROPSET == 10 || CAM_PROPSET == 11
 132     // PS7 doesn't have _STATE, combines values
 133     int x=shooting_get_prop(PROPCASE_DIGITAL_ZOOM_MODE);
 134     if(x==1) {
 135         return 1;
 136     }
 137     return 0;
 138 #else
 139     return shooting_get_prop(PROPCASE_DIGITAL_ZOOM_STATE);
 140 #endif
 141 }
 142 /*
 143 get focus mode as used in script
 144 essentially returns PROPCASE_REAL_FOCUS_MODE,
 145 except MF and Macro values are swapped (presumably for backward compatiblity)
 146 resulting in:
 147 0=auto, 1=MF, 3=inf., 4=macro, 5=supermacro
 148 CHDK set_mf is correctly reported as MF, because set_mf overrides PROPCASE_FOCUS_MODE if needed
 149 */
 150 short shooting_get_real_focus_mode()
 151 {
 152     short f = shooting_get_focus_mode();
 153     short m = shooting_get_prop(PROPCASE_REAL_FOCUS_MODE);
 154     if (f==0 && m!=0) f=(m==1)?4:m;
 155     return f;
 156 }
 157 
 158 short shooting_get_focus_state()
 159 {
 160     if (shooting_get_focus_mode()==1) return -1;
 161     int m = shooting_get_prop_int(PROPCASE_FOCUS_STATE);
 162     return (short) m;
 163 }
 164  
 165 short shooting_get_focus_ok()
 166 {
 167     return ((shooting_get_focus_state()!=0) && shooting_in_progress());
 168 }
 169 
 170 short shooting_get_drive_mode()
 171 {
 172     // these cameras do not set PROPCASE_DRIVE_MODE when in custom timer mode
 173     // SX 200 IS 0,1,2,3,4=Off, 2 Second, Custom, Face Detection
 174 #if defined (CAM_DRIVE_MODE_FROM_TIMER_MODE)
 175     short m = shooting_get_prop(PROPCASE_TIMER_MODE);
 176     // note we return this only in custom mode.
 177     // Other cameras would return 3 if any timer mode is set (?) even if it doesn't do multiple exposures
 178     // note that face detect does multiple exposure
 179     if (m == 3)
 180         return m;
 181 #endif
 182 
 183     return shooting_get_prop(PROPCASE_DRIVE_MODE);
 184 }
 185 
 186 int shooting_get_zoom()
 187 {
 188     return lens_get_zoom_point();
 189 }
 190 
 191 //-------------------------------------------------------------------
 192 // ISO section - start
 193 //-------------------------------------------------------------------
 194 
 195 /*
 196     Notes on Canon ISO & CHDK ISO overrides.
 197     ========================================
 198     (philmoz Aug 2013)
 199 
 200     Canon P&S cameras use two different values for ISO in the firmware.
 201         - the ISO value displayed on screen & stored in the image EXIF, generally referred to as the 'market' ISO
 202         - the value used to program the sensitivity of the sensor and/or set the image tone curve, the 'real' ISO
 203 
 204     The firmware uses the APEX system for ISO (as well as aperture, shutter speed and luminance).
 205     See - http://dougkerr.net/pumpkin/#Optics - APEX - The Additive System of Photographic Exposure.
 206     The Canon firmware scales the Sv (speed value) by multiplying values by 96 - referred to as Sv96 below.
 207 
 208     Conversion of ISO to/from Sv96 (from the above linked PDF)
 209     ----------------------------------------------------------
 210         Sv = log2(ISO / 3.125)      ==>     Sv96 = log2(ISO / 3.125) * 96
 211         ISO = pow(2, Sv * 3.125)    ==>     ISO = pow(2, Sv96 / 96 * 3.125)
 212 
 213     In the implementation below 3.125 is represented as 100/32, 
 214     and log2(x) is calculated as log(x)/log(2), where log is the natural log function found in the firmware.
 215 
 216     Relationship of 'real' to 'market' values.
 217     ------------------------------------------
 218     In most cases the following equation holds:
 219         Sv96_real = Sv96_market - 69
 220     In other words the 'real' value is approx. 1/3 stop lower than the market value.
 221     On some cameras, when the lowest ISO is set manually on the camera, the equation is:
 222         Sv96_real = Sv96_market - 78
 223     The difference is less than 1/10th of a stop, so the value 69 is used in the CHDK override code.
 224 
 225     Note: some cameras have a different offset (e.g. IXUS700 uses 'real = market - 20')
 226           in this case override the conversion values in the platform_camera.h file
 227 
 228     Based on the above equations we can determine that:
 229         ISO_real = ISO_market * pow(2, -69/96) ~= ISO_market * 1.65
 230     and ISO_market = ISO_real * pow(2,  69/96) ~= ISO_real * 0.61
 231     The derivation of this equation is left as an exercise for the reader :)
 232 
 233     Setting ISO on the camera.
 234     --------------------------
 235     Four PROPCASES are used for ISO settings
 236         PROPCASE_ISO_MODE       = ISO value when ISO set manually, 0 for AUTO ISO
 237         PROPCASE_SV             = 'real' Sv96 value for ISO
 238         PROPCASE_SV_MARKET      = 'market' Sv96 value when ISO set manually
 239                                 = Sv96 value for camera 'base' ISO (50,100 or 200) when using AUTO ISO
 240         PROPCASE_DELTA_SV       = 0 when ISO set manually
 241                                 = 'market' Sv96 - PROPCASE_SV_MARKET when using AUTO ISO
 242 
 243     From the above:
 244         'market' Sv96 = PROPCASE_SV_MARKET + PROPCASE_DELTA_SV
 245  */
 246 
 247 extern const ISOTable iso_table[];
 248 extern const unsigned int ISO_SIZE;
 249 
 250 short shooting_get_canon_iso_mode()             { return shooting_get_prop(PROPCASE_ISO_MODE); }
 251 short shooting_get_sv96_market()                { return shooting_get_prop(PROPCASE_SV_MARKET); }
 252 short shooting_get_sv96_real()                  { return shooting_get_prop(PROPCASE_SV); }
 253 short shooting_get_sv96_delta()                 { return shooting_get_prop(PROPCASE_DELTA_SV); }
 254 
 255 // Local shooting calculation variables
 256 static short canon_iso_base=0;
 257 static short canon_sv96_base=0;
 258 
 259 #if !defined(SV96_MARKET_OFFSET)                // Can be overriden in platform_camera.h (see IXUS700 for example)
 260 // "real" to "market" conversion definitions
 261 #define SV96_MARKET_OFFSET          69          // market-real sv96 conversion value
 262 
 263 // Conversion values for pow(2,-69/96) 'market' to 'real', and pow(2,69/96) 'real' to 'market'
 264 // Uses integer arithmetic to avoid floating point calculations. Values choses to get as close
 265 // to the desired multiplication factor as possible within normal ISO range.
 266 #define ISO_MARKET_TO_REAL_MULT     9955
 267 #define ISO_MARKET_TO_REAL_SHIFT    14
 268 #define ISO_MARKET_TO_REAL_ROUND    8192
 269 #define ISO_REAL_TO_MARKET_MULT     3371
 270 #define ISO_REAL_TO_MARKET_SHIFT    11
 271 #define ISO_REAL_TO_MARKET_ROUND    1024
 272 #endif
 273 
 274 #define ISO_MARKET_TO_REAL(x)       (((int)x * ISO_MARKET_TO_REAL_MULT + ISO_MARKET_TO_REAL_ROUND) >> ISO_MARKET_TO_REAL_SHIFT)
 275 #define ISO_REAL_TO_MARKET(x)       (((int)x * ISO_REAL_TO_MARKET_MULT + ISO_REAL_TO_MARKET_ROUND) >> ISO_REAL_TO_MARKET_SHIFT)
 276 
 277 short shooting_get_sv96_from_iso(short iso)
 278 {
 279     // Equivalent to (short)(log2(iso/3.125)*96+0.5) [APEX equation]
 280     if (iso > 0)
 281         return (short)( log((double)(iso)*32.0/100.0)*96.0*(inv_log_2)+0.5 );
 282     return 0;
 283 }
 284 
 285 short shooting_get_iso_from_sv96(short sv96)
 286 {
 287     // APEX equation --> (int)(POWER(2,(iso/96)) * 3.125) + 0.5)
 288     return (short)( (double)pow(2, (((double)sv96)/96.0))*100.0/32.0 + 0.5 );
 289 }
 290 
 291 short shooting_iso_market_to_real(short isom)
 292 {
 293    return ISO_MARKET_TO_REAL(isom);
 294 }
 295 
 296 short shooting_iso_real_to_market(short isor)
 297 {
 298    return ISO_REAL_TO_MARKET(isor);
 299 }
 300 
 301 short shooting_sv96_market_to_real(short sv96)
 302 {
 303    return sv96 - SV96_MARKET_OFFSET;
 304 }
 305 
 306 short shooting_sv96_real_to_market(short sv96)
 307 {
 308    return sv96 + SV96_MARKET_OFFSET;
 309 }
 310 
 311 short shooting_get_iso_override_value()
 312 {
 313     short iso = conf.iso_override_value;        // Start with market value
 314     // Apply limits if needed
 315 #ifdef CAM_ISO_LIMIT_IN_HQ_BURST
 316     // Limit max ISO in HQ burst mode (also done in shooting_set_iso_real; but done here so OSD display value is correct)
 317     if (camera_info.state.mode_shooting == MODE_HIGHSPEED_BURST)
 318         if (iso > CAM_ISO_LIMIT_IN_HQ_BURST) iso = CAM_ISO_LIMIT_IN_HQ_BURST;
 319 #endif
 320 #ifdef CAM_MIN_ISO_OVERRIDE
 321     // Limit min (non-zero) ISO
 322     // Some cameras will crash if flash used and ISO set lower than this value (most easily tested in AUTO mode)
 323     if ((iso > 0) && (iso < CAM_MIN_ISO_OVERRIDE)) iso = CAM_MIN_ISO_OVERRIDE;
 324 #endif
 325 #ifdef CAM_MAX_ISO_OVERRIDE
 326     // Limit max ISO
 327     // Some cameras will crash if ISO set higher than this value (dependence on flash is unclear)
 328     if (iso > CAM_MAX_ISO_OVERRIDE) iso = CAM_MAX_ISO_OVERRIDE;
 329 #endif
 330     return shooting_iso_market_to_real(iso);    // return real value (after limits applied)
 331 }
 332 
 333 int shooting_get_iso_mode()
 334 {
 335     short isov = shooting_get_canon_iso_mode();
 336     long i;
 337     for (i=0;i<ISO_SIZE;i++)
 338     {
 339         if (iso_table[i].prop_id == isov)
 340             return iso_table[i].id;
 341     }
 342     return 0;
 343 }
 344 
 345 short shooting_get_iso_real()
 346 {
 347     short sv = shooting_get_sv96_real();
 348     if (sv == 0)
 349         return 0;
 350     return shooting_get_iso_from_sv96(sv);
 351 }
 352 
 353 // AUTOISO:EXIF
 354 short shooting_get_iso_market()
 355 {
 356     short sv = shooting_get_sv96_market() + shooting_get_sv96_delta();
 357     if (sv == 0)
 358         return 0;
 359     return shooting_get_iso_from_sv96(sv);
 360 }
 361 
 362 static void set_iso_mode(int i)
 363 {
 364     short vv = iso_table[i].prop_id;
 365     set_property_case(PROPCASE_ISO_MODE, &vv, sizeof(vv));
 366 }
 367 
 368 void shooting_set_iso_mode(int v)
 369 {
 370     int i;
 371     if (v < 50) // CHDK ID
 372     {
 373         for (i=0; i<ISO_SIZE; i++)
 374         {
 375             if (iso_table[i].id == v)
 376             {
 377                 set_iso_mode(i);
 378                 return;
 379             }
 380         }
 381     }
 382     else        // ISO value - find nearest entry in iso_table and set iso mode to that value
 383     {
 384         if (v <= iso_table[0].prop_id)    // In case no AUTO (0) entry
 385         {
 386             set_iso_mode(0);
 387             return;
 388         }
 389         for (i=0; i<ISO_SIZE-1; i++)
 390         {
 391             if ((v > iso_table[i].prop_id) && (v <= iso_table[i+1].prop_id))
 392             {
 393                 if ((v - iso_table[i].prop_id) < (iso_table[i+1].prop_id - v))
 394                     set_iso_mode(i);
 395                 else
 396                     set_iso_mode(i+1);
 397                 return;
 398             }
 399         }
 400         set_iso_mode(ISO_SIZE-1);
 401     }
 402 }
 403 
 404 void shooting_set_sv96(short sv96, short is_now)
 405 {
 406     if (!camera_info.state.mode_play)
 407     {
 408         if (is_now)
 409         {
 410             while ((shooting_is_flash_ready()!=1) || (focus_busy)) msleep(10);
 411 
 412             short iso_mode = shooting_get_canon_iso_mode();
 413             if (iso_mode >= 50)
 414                 shooting_set_iso_mode(0);   // Force AUTO mode on camera
 415 
 416             short dsv96 = sv96 + SV96_MARKET_OFFSET - canon_sv96_base;
 417 
 418             set_property_case(PROPCASE_SV_MARKET, &canon_sv96_base, sizeof(canon_sv96_base));
 419             set_property_case(PROPCASE_SV,        &sv96, sizeof(sv96));
 420             set_property_case(PROPCASE_DELTA_SV,  &dsv96, sizeof(dsv96));
 421         }
 422         else   
 423             photo_param_put_off.sv96 = sv96;
 424     }
 425 }
 426 
 427 void shooting_set_iso_real(short iso, short is_now)
 428 {
 429     if (!camera_info.state.mode_play)
 430     {
 431         if (iso > 0)
 432         {
 433 #ifdef CAM_ISO_LIMIT_IN_HQ_BURST
 434             // Limit max ISO in HQ burst mode
 435             if (camera_info.state.mode_shooting == MODE_HIGHSPEED_BURST)
 436                 if (iso > ISO_MARKET_TO_REAL(CAM_ISO_LIMIT_IN_HQ_BURST)) iso = ISO_MARKET_TO_REAL(CAM_ISO_LIMIT_IN_HQ_BURST);
 437 #endif
 438 #ifdef CAM_MIN_ISO_OVERRIDE
 439             // Limit min (non-zero) ISO
 440             if ((iso > 0) && (iso < ISO_MARKET_TO_REAL(CAM_MIN_ISO_OVERRIDE))) iso = ISO_MARKET_TO_REAL(CAM_MIN_ISO_OVERRIDE);
 441 #endif
 442 #ifdef CAM_MAX_ISO_OVERRIDE
 443             // Limit max ISO
 444             if (iso > ISO_MARKET_TO_REAL(CAM_MAX_ISO_OVERRIDE)) iso = ISO_MARKET_TO_REAL(CAM_MAX_ISO_OVERRIDE);
 445 #endif
 446             shooting_set_sv96(shooting_get_sv96_from_iso(iso), is_now);
 447         }
 448     }
 449 }
 450 
 451 static void iso_init()
 452 {
 453     // Get the camera ISO base value, store in global variable 'canon_iso_base'
 454     if (iso_table[1-iso_table[0].id].prop_id == 50)
 455         canon_iso_base = 50;
 456     else
 457         canon_iso_base = CAM_MARKET_ISO_BASE;
 458 
 459     // Get the SV96 value corresponding to the base Canon ISO for the camera
 460     // Store in global variable 'canon_sv96_base'
 461     canon_sv96_base = shooting_get_sv96_from_iso(canon_iso_base);
 462 }
 463 //-------------------------------------------------------------------
 464 // ISO section - end
 465 //-------------------------------------------------------------------
 466 
 467 //-------------------------------------------------------------------
 468 // Tv section - start
 469 //-------------------------------------------------------------------
 470 
 471 short shooting_get_tv96()                       { return shooting_get_prop(PROPCASE_TV); }
 472 
 473 // APEX96 conversion
 474 
 475 short shooting_get_tv96_from_shutter_speed(float t)
 476 {
 477     if (t > 0)
 478     {
 479         t = ((96.0 * -log(t)) * inv_log_2);
 480         if (t < 0)
 481             return (short)(t - 0.5);
 482         return (short)(t + 0.5);
 483     }
 484     return SHOOTING_TV96_INVALID;
 485 }
 486 
 487 float shooting_get_shutter_speed_from_tv96(short tv96)
 488 {
 489     return pow(2,((float)(-tv96))/96.0);
 490 }
 491 
 492 // Index of '0' entry in the tv_override array (1 sec) (see gui.c for tv_override array)
 493 #if CAM_EXT_TV_RANGE
 494 #define tv_override_zero_shift  (18+15)
 495 #else
 496 #define tv_override_zero_shift  18
 497 #endif
 498 
 499 // Get Tv override value (APEX96)
 500 static int shooting_get_tv96_override_value()
 501 {
 502     // Calculate the tv96 value for the tv override
 503     if (conf.tv_enum_type==TV_OVERRIDE_EV_STEP)
 504         return 32*(conf.tv_override_value-tv_override_zero_shift);
 505     else if (conf.tv_enum_type==TV_OVERRIDE_SHORT_EXP)
 506         return shooting_get_tv96_from_shutter_speed(((float)conf.tv_override_short_exp)/100000.0);
 507     else
 508         return shooting_get_tv96_from_shutter_speed((float)conf.tv_override_long_exp);
 509 }
 510 
 511 // Overrides
 512 
 513 // Find nearest entry in 'shutter_speeds_table' to the given 'tv96' value
 514 static int find_nearest_shutter_speed_entry(short tv96)
 515 {
 516     if (tv96 <= shutter_speeds_table[0].prop_id)
 517         return 0;
 518 
 519     int i;
 520     for (i=0; i<SS_SIZE-1; i++)
 521     {
 522         if ((tv96 > shutter_speeds_table[i].prop_id) && (tv96 <= shutter_speeds_table[i+1].prop_id))
 523         {
 524             if ((tv96 - shutter_speeds_table[i].prop_id) < (shutter_speeds_table[i+1].prop_id - tv96))
 525                 return i;
 526             else
 527                 return i+1;
 528         }
 529     }
 530 
 531     return SS_SIZE-1;
 532 }
 533 
 534 // Convert 'tv96' value to nearest Canon value from shutter_speeds_table
 535 static short find_canon_shutter_speed(short tv96)
 536 {
 537     return shutter_speeds_table[find_nearest_shutter_speed_entry(tv96)].prop_id;
 538 }
 539 
 540 void shooting_set_tv96_direct(short tv96, short is_now)
 541 {
 542     if (!camera_info.state.mode_play)
 543     {
 544         if(is_now)
 545         {
 546             set_property_case(PROPCASE_TV, &tv96, sizeof(tv96));
 547 #ifdef PROPCASE_TV2
 548             set_property_case(PROPCASE_TV2, &tv96, sizeof(tv96));   // Save override to property that will update JPEG header & Canon OSD
 549 #endif
 550         }
 551         else
 552             photo_param_put_off.tv96 = tv96;
 553     }
 554 }
 555 
 556 void shooting_set_tv96(short tv96, short is_now)
 557 {
 558     if (!camera_info.state.mode_play)
 559         shooting_set_tv96_direct(find_canon_shutter_speed(tv96), is_now);
 560 }
 561 
 562 // User Tv
 563 
 564 short shooting_get_user_tv96()
 565 {
 566 #if CAM_HAS_USER_TV_MODES
 567     return shooting_get_prop(PROPCASE_USER_TV);
 568 #else
 569     return 0;
 570 #endif
 571 }
 572 
 573 int shooting_get_user_tv_id()
 574 {
 575 #if CAM_HAS_USER_TV_MODES
 576     return shutter_speeds_table[find_nearest_shutter_speed_entry(shooting_get_user_tv96())].id;
 577 #else
 578     return 0;
 579 #endif
 580 }
 581 
 582 void shooting_set_user_tv_by_id(int v)
 583 {
 584 #if CAM_HAS_USER_TV_MODES
 585     if (!camera_info.state.mode_play)
 586     {
 587         long i;
 588         for (i=0;i<SS_SIZE;i++)
 589         {
 590             if (shutter_speeds_table[i].id == v)
 591             {
 592                 short vv = shutter_speeds_table[i].prop_id;
 593                 set_property_case(PROPCASE_USER_TV, &vv, sizeof(vv));
 594                 return;
 595             }
 596         }
 597     }
 598 #endif
 599 }
 600 
 601 void shooting_set_user_tv_by_id_rel(int v)
 602 {
 603 #if CAM_HAS_USER_TV_MODES
 604     if (!camera_info.state.mode_play)
 605     {
 606         int cv = shooting_get_user_tv_id();
 607         shooting_set_user_tv_by_id(cv+v);
 608     }
 609 #endif
 610 }
 611 
 612 void shooting_set_user_tv96(short tv96)
 613 {
 614 #if CAM_HAS_USER_TV_MODES
 615     if (!camera_info.state.mode_play)
 616     {
 617         tv96 = find_canon_shutter_speed(tv96);
 618         set_property_case(PROPCASE_USER_TV, &tv96, sizeof(tv96));
 619     }
 620 #endif
 621 }
 622 
 623 // Only needed on VxWorks
 624 #if CAM_DRAW_EXPOSITION
 625 // compare PROPCASE_TV with shutter_speeds_table
 626 char* shooting_get_tv_str()
 627 {
 628     return (char*)shutter_speeds_table[find_nearest_shutter_speed_entry(shooting_get_tv96())].name;
 629 }
 630 #endif
 631 
 632 //-------------------------------------------------------------------
 633 // Tv section - end
 634 //-------------------------------------------------------------------
 635 
 636 //-------------------------------------------------------------------
 637 // Av section - start
 638 //-------------------------------------------------------------------
 639 
 640 #define AV96_MIN (aperture_sizes_table[0].prop_id)
 641 #define AV96_MAX (aperture_sizes_table[AS_SIZE-1].prop_id)
 642 
 643 short shooting_get_av96()                       { return shooting_get_prop(PROPCASE_AV); }
 644 short shooting_get_aperture_sizes_table_size()  { return AS_SIZE; }
 645 
 646 // APEX96 conversion
 647 
 648 int shooting_get_aperture_from_av96(short av96)
 649 {
 650     if (av96)
 651         return (short)((pow(2, ((double)av96)/192.0))*1000.0 + 0.5);
 652     return -1;
 653 }
 654 
 655 short shooting_get_av96_from_aperture(int aperture)
 656 {
 657     return (int)((log((double)aperture/1000.0) * 192 * inv_log_2) + 0.5);
 658 }
 659 
 660 // Get Av override value (APEX96)
 661 short shooting_get_av96_override_value()
 662 {
 663     if (conf.av_override_value<AS_SIZE)
 664         return (short) aperture_sizes_table[conf.av_override_value].prop_id;
 665     return (short) (AV96_MAX+32*(conf.av_override_value-AS_SIZE+1));
 666 }
 667 
 668 int shooting_get_real_aperture()
 669 {
 670     return shooting_get_aperture_from_av96(shooting_get_current_av96());
 671 }
 672 
 673 static int shooting_get_min_real_aperture()
 674 {
 675     short av96;
 676     get_property_case(PROPCASE_MIN_AV, &av96, sizeof(av96));
 677     if (av96)
 678         return shooting_get_aperture_from_av96(av96);
 679     return shooting_get_real_aperture();
 680 }
 681 
 682 // Overrides
 683 
 684 // Find nearest entry in 'aperture_sizes_table' to the given 'av96' value
 685 static int find_nearest_aperture_entry(short av96)
 686 {
 687     if (av96 <= aperture_sizes_table[0].prop_id)
 688         return 0;
 689 
 690     int i;
 691     for (i=0; i<AS_SIZE-1; i++)
 692     {
 693         if ((av96 > aperture_sizes_table[i].prop_id) && (av96 <= aperture_sizes_table[i+1].prop_id))
 694         {
 695             if ((av96 - aperture_sizes_table[i].prop_id) < (aperture_sizes_table[i+1].prop_id - av96))
 696                 return i;
 697             else
 698                 return i+1;
 699         }
 700     }
 701 
 702     return AS_SIZE-1;
 703 }
 704 
 705 // Convert 'av96' value to nearest Canon value from aperture_sizes_table
 706 #if CAM_HAS_IRIS_DIAPHRAGM
 707 short find_canon_aperture(short av96)
 708 {
 709     return aperture_sizes_table[find_nearest_aperture_entry(av96)].prop_id;
 710 }
 711 #endif
 712 
 713 void shooting_set_av96_direct(short av96, short is_now)
 714 {
 715 #if CAM_HAS_IRIS_DIAPHRAGM
 716     if (!camera_info.state.mode_play)
 717     {
 718         if(is_now)
 719         {
 720             set_property_case(PROPCASE_AV, &av96, sizeof(av96));
 721 #ifdef PROPCASE_AV2
 722             set_property_case(PROPCASE_AV2, &av96, sizeof(av96));   // Save override to property that will update JPEG header & Canon OSD
 723 #endif
 724         }
 725         else
 726             photo_param_put_off.av96 = av96;
 727     }
 728 #endif
 729 }
 730 
 731 void shooting_set_av96(short av96, short is_now)
 732 {
 733 #if CAM_HAS_IRIS_DIAPHRAGM
 734     if (!camera_info.state.mode_play)
 735         shooting_set_av96_direct(find_canon_aperture(av96), is_now);
 736 #endif
 737 }
 738 
 739 // User Av
 740 
 741 short shooting_get_user_av96()
 742 {
 743 #if CAM_HAS_IRIS_DIAPHRAGM
 744     return shooting_get_prop(PROPCASE_USER_AV);
 745 #else
 746     return 0;
 747 #endif
 748 }
 749 
 750 int shooting_get_user_av_id()
 751 {
 752 #if CAM_HAS_IRIS_DIAPHRAGM
 753     return aperture_sizes_table[find_nearest_aperture_entry(shooting_get_user_av96())].prop_id;
 754 #else
 755     return 0;
 756 #endif
 757 }
 758 
 759 void shooting_set_user_av_by_id(int v)
 760 {
 761 #if CAM_HAS_IRIS_DIAPHRAGM
 762     long i;
 763     if (!camera_info.state.mode_play)
 764     {
 765         for (i=0;i<AS_SIZE;i++)
 766         {
 767             if (aperture_sizes_table[i].id == v)
 768             {
 769                 short vv = aperture_sizes_table[i].prop_id;
 770                 set_property_case(PROPCASE_USER_AV, &vv, sizeof(vv));
 771                 return;
 772             }
 773         }
 774     }
 775 #endif
 776 }
 777 
 778 void shooting_set_user_av_by_id_rel(int v)
 779 {
 780 #if CAM_HAS_IRIS_DIAPHRAGM
 781     if (!camera_info.state.mode_play)
 782     {
 783         int cv = shooting_get_user_av_id();
 784         shooting_set_user_av_by_id(cv+v);
 785     }
 786 #endif
 787 }
 788 
 789 void shooting_set_user_av96(short av96)
 790 {
 791 #if CAM_HAS_IRIS_DIAPHRAGM
 792     if (!camera_info.state.mode_play)
 793     {
 794         av96 = find_canon_aperture(av96);
 795         set_property_case(PROPCASE_USER_AV, &av96, sizeof(av96));
 796     }
 797 #endif
 798 }
 799 
 800 // Only needed on VxWorks
 801 #if CAM_DRAW_EXPOSITION
 802 // compare PROPCASE_AV with aperture_sizes_table
 803 char* shooting_get_av_str()
 804 {
 805     return (char*)aperture_sizes_table[find_nearest_aperture_entry(shooting_get_av96())].name;
 806 }
 807 #endif
 808 
 809 void shooting_set_nd_filter_state(short v, short is_now)
 810 {
 811 #if CAM_HAS_ND_FILTER
 812     if (!camera_info.state.mode_play)
 813     {
 814         if (is_now)
 815         {
 816 #if defined(CAM_ND_SET_AV_VALUE)
 817             short av;
 818             get_property_case(PROPCASE_MIN_AV,&av,sizeof(av));
 819             if(v==1) {
 820                 av += shooting_get_nd_value_ev96();
 821             }
 822             set_property_case(PROPCASE_AV, &av, sizeof(av));
 823 #if defined(PROPCASE_AV2)
 824             set_property_case(PROPCASE_AV2, &av, sizeof(av));
 825 #endif
 826 #endif
 827             if (v==1) {
 828                 PutInNdFilter();
 829             }
 830             else if (v==2)
 831                 PutOutNdFilter();
 832 #if defined(CAM_HAS_NATIVE_ND_FILTER) && defined(PROPCASE_ND_FILTER_STATE)
 833             int n = (v==1) ? 1 : 0;
 834             set_property_case(PROPCASE_ND_FILTER_STATE, &n, sizeof(n));
 835 #endif
 836         }
 837         else
 838             photo_param_put_off.nd_filter = v;
 839     }
 840 #endif
 841 }
 842 
 843 
 844 // get usable Av range (iris only)
 845 // Appears to be the full range, including smaller (higher F/ number) than available in Canon UI.
 846 // NOTE canon functions Min and Max refer to aperture size, CHDK refer to av96 values
 847 // returns -1 if unavailable (0 is a valid Av = F/1.0)
 848 short shooting_get_min_av96()
 849 {
 850 #if CAM_HAS_IRIS_DIAPHRAGM
 851     extern short GetUsableMaxAv(void);
 852     if (camera_info.state.mode_play)
 853     {
 854         return -1;
 855     }
 856     return GetUsableMaxAv();
 857 #else
 858     return -1;
 859 #endif
 860 }
 861 
 862 short shooting_get_max_av96()
 863 {
 864 #if CAM_HAS_IRIS_DIAPHRAGM
 865     extern short GetUsableMinAv(void);
 866     if (camera_info.state.mode_play)
 867     {
 868         return -1;
 869     }
 870     return GetUsableMinAv();
 871 #else
 872     return -1;
 873 #endif
 874 }
 875 
 876 //-------------------------------------------------------------------
 877 // Av section - end
 878 //-------------------------------------------------------------------
 879 
 880 //-------------------------------------------------------------------
 881 // Init
 882 
 883 void shooting_init()
 884 {
 885     photo_param_put_off.tv96 = PHOTO_PARAM_TV_NONE;
 886     iso_init();
 887 }
 888 
 889 //-------------------------------------------------------------------
 890 // Get file related info
 891 
 892 #ifdef CAM_FILE_COUNTER_IS_VAR
 893 extern long file_counter_var;
 894 long get_file_counter()
 895 {
 896     return file_counter_var;
 897 }
 898 long get_exposure_counter()
 899 {
 900     long v = get_file_counter();
 901     v = ((v>>4)&0x3FFF);
 902     return v;
 903 }
 904 #else
 905 extern const unsigned int param_file_counter;
 906 
 907 long get_file_counter()
 908 {
 909     long v = 0;
 910     get_parameter_data(param_file_counter, &v, 4);
 911     return v;
 912 }
 913 
 914 long get_exposure_counter()
 915 {
 916     long v = 0;
 917     get_parameter_data(param_file_counter, &v, 4);
 918     v = ((v>>4)&0x3FFF);
 919     return v;
 920 }
 921 #endif
 922 
 923 #ifndef CAM_DATE_FOLDER_NAMING
 924 void get_target_dir_name(char *dir) {
 925     extern long get_target_dir_num();
 926     sprintf(dir,"A/DCIM/%03dCANON",get_target_dir_num());
 927 }
 928 #endif
 929 
 930 //-------------------------------------------------------------------
 931 // Convert values to/from APEX 96
 932 
 933 static const double K = 12.5;               // K is the reflected-light meter calibration constant
 934 
 935 int shooting_get_luminance()                // http://en.wikipedia.org/wiki/APEX_system
 936 {
 937     short bv = shooting_get_bv96();
 938     int b = (int)(100 * K * pow(2.0,((double)(bv-168)/96.0)));
 939     return b;
 940 }
 941 
 942 //-------------------------------------------------------------------
 943 // Get override values
 944 
 945 int shooting_get_subject_distance_override_value()
 946 {
 947     if (conf.subj_dist_override_koef != SD_OVERRIDE_INFINITY)
 948         return (conf.subj_dist_override_value < shooting_get_lens_to_focal_plane_width()?0:(conf.subj_dist_override_value - shooting_get_lens_to_focal_plane_width()));
 949     else
 950         return INFINITY_DIST;
 951 }
 952 
 953 //-------------------------------------------------------------------
 954 // Get shooting value, with conversion / calculation
 955 
 956 int shooting_mode_canon2chdk(int canonmode)
 957 {
 958         int i;
 959         for (i=0; i < MODESCNT; i++)
 960     {
 961                 if (modemap[i].canonmode == canonmode)
 962                         return modemap[i].hackmode;
 963         }
 964         return 0;
 965 }
 966 
 967 int shooting_mode_chdk2canon(int hackmode)
 968 {
 969         int i;
 970         for (i=0; i < MODESCNT; i++)
 971     {
 972                 if (modemap[i].hackmode == hackmode)
 973                         return modemap[i].canonmode;
 974         }
 975         return -1; // 0 is a valid mode on old cameras!
 976 }
 977 
 978 
 979 
 980 int sd_over_modes()
 981 {  return ( 0
 982 #ifdef CAM_SD_OVER_IN_AF 
 983           + 1
 984 #endif
 985 #ifdef CAM_SD_OVER_IN_AFL
 986           + 2 
 987 #endif
 988 #ifdef CAM_SD_OVER_IN_MF
 989           + 4
 990 #endif
 991 );  }
 992 
 993 int shooting_get_lens_to_focal_plane_width()
 994 {
 995     return (int)(lens_get_focus_pos()-lens_get_focus_pos_from_lens());
 996 }
 997 
 998 int shooting_get_hyperfocal_distance_1e3_f(int av, int fl)
 999 {
1000   if ((av>0) && (fl>0) && (camera_info.circle_of_confusion>0))
1001     return (int)(((((double)fl*fl)/(av*camera_info.circle_of_confusion)*1000)+fl)+0.5);
1002   else return (-1);
1003 }
1004 
1005 int shooting_get_near_limit_f(int s, int av, int fl)
1006 {
1007   if (shooting_is_infinity_distance()) return (-1);
1008   int h_1e3 = shooting_get_hyperfocal_distance_1e3_f(av, fl);
1009   double m = ((double)(h_1e3 - fl)+500)/1000*s;
1010   int v = ((h_1e3 - 2*fl + 1000*s)+500)/1000;
1011   if ((m>0) && (v>0)) return (int)((2*m/v+1)/2);
1012   else return (-1);
1013 }
1014 
1015 int shooting_get_far_limit_f(int s, int av, int fl)
1016 {
1017   if (shooting_is_infinity_distance()) return (-1);
1018   int h_1e3 = shooting_get_hyperfocal_distance_1e3_f(av, fl);
1019   double m = ((double)(h_1e3 - fl)+500)/1000*s;
1020   int v = ((h_1e3 - 1000*s)+500)/1000;
1021   if ((m>0) && (v>0)) return (int)((2*m/v+1)/2);
1022   else return (-1); 
1023 }
1024 
1025 short shooting_is_infinity_distance()
1026 {
1027   return (shooting_get_lens_to_focal_plane_width() == 0)?1:0;
1028 }
1029 
1030 int shooting_get_canon_subject_distance()
1031 {
1032         if (conf.dof_use_exif_subj_dist) 
1033           return shooting_get_exif_subject_dist();
1034         else
1035           return (lens_get_focus_pos_from_lens());
1036 }
1037 
1038 int shooting_get_subject_distance_()
1039 {
1040    if (!conf.dof_subj_dist_as_near_limit) return shooting_get_canon_subject_distance();
1041    else return shooting_get_far_limit_f(shooting_get_canon_subject_distance(),
1042                                          shooting_get_min_real_aperture(), 
1043                                          get_focal_length(lens_get_zoom_point()));
1044 }
1045 
1046 void shooting_update_dof_values()
1047 {
1048   int hyp, hyp_1e3, av_1e3, v;
1049   int dist = shooting_get_subject_distance_();
1050   int zoom_point = lens_get_zoom_point();
1051   int fl = get_focal_length(zoom_point);
1052   short f_focus_ok = shooting_get_focus_ok();
1053   short f_hyp_calc = 0, f_dist_calc = 0;
1054   short min_av96_zoom_point = min_av96_zoom_point_tbl[zoom_point];
1055   short av96 = shooting_get_user_av96();
1056   short curr_av96 = shooting_get_current_av96();
1057   short prop_av96 = shooting_get_av96();
1058   short min_av96;
1059 
1060   if (!min_av96_zoom_point_tbl) {
1061     min_av96_zoom_point_tbl = (short *) malloc(zoom_points * sizeof(short));
1062     if (min_av96_zoom_point_tbl) {
1063       memset(min_av96_zoom_point_tbl, 0, zoom_points * sizeof(short));
1064       min_av96_zoom_point = 0;
1065     }
1066   } else min_av96_zoom_point = min_av96_zoom_point_tbl[zoom_point]; 
1067 
1068   if (min_av96_zoom_point==0 && shooting_in_progress()) { 
1069     get_property_case(PROPCASE_MIN_AV, &min_av96, sizeof(min_av96));
1070     min_av96_zoom_point_tbl[zoom_point] = min_av96;
1071   }
1072 
1073   if ((camera_info.state.mode_shooting==MODE_M || camera_info.state.mode_shooting==MODE_AV) && (av96>0) && !f_focus_ok) { 
1074     if (av96 < min_av96_zoom_point) av96 = min_av96_zoom_point;
1075   }
1076   else av96 = (abs(curr_av96-prop_av96)<2)?prop_av96:curr_av96;
1077 
1078   av_1e3 = shooting_get_aperture_from_av96(av96);
1079   hyp_1e3 = camera_info.dof_values.hyperfocal_distance_1e3;
1080   hyp = camera_info.dof_values.hyperfocal_distance;
1081   
1082   if (camera_info.dof_values.aperture_value!=av_1e3 || camera_info.dof_values.focal_length!=fl || (hyp_1e3<0)) {
1083     //calc new hyperfocal distance and min stack distance
1084     f_hyp_calc = 1;
1085     hyp_1e3 = -1;
1086     hyp = -1;
1087     camera_info.dof_values.aperture_value = av_1e3;
1088     camera_info.dof_values.focal_length = fl;
1089     hyp_1e3 = shooting_get_hyperfocal_distance_1e3_f(av_1e3, fl);
1090     if (hyp_1e3>0) {
1091       hyp = (hyp_1e3+500)/1000;
1092       camera_info.dof_values.min_stack_distance = CAMERA_MAX_DIST;
1093       v = ((hyp_1e3 - fl)/250 + 2 + 1)/2;
1094       if (v>0) {
1095         int m = ((fl*((fl - hyp_1e3)/1000 - 1)/500)/v + 1)/2;
1096         int m2 = (int)((((double)hyp*(2*fl - hyp_1e3)/1000))/v + 0.5);
1097         camera_info.dof_values.min_stack_distance = sqrt(m*m - m2) - m;
1098       }  
1099     }
1100   }
1101 
1102   if ((camera_info.dof_values.subject_distance!=dist || (camera_info.dof_values.hyperfocal_distance_1e3!=hyp_1e3)) && (hyp_1e3>0)) {
1103     //calc new NEAR, FAR, DOF values
1104     f_dist_calc = 1;
1105     camera_info.dof_values.subject_distance = dist;
1106     camera_info.dof_values.near_limit = -1;
1107     camera_info.dof_values.far_limit = -1;
1108     camera_info.dof_values.depth_of_field = -1;
1109     if ((av_1e3>0) && (fl>0) && (dist>0) && (shooting_is_infinity_distance()==0) && (hyp_1e3>0)) {
1110       double m = ((double)(hyp_1e3 - fl)/1000 * dist) + 0.5;
1111       if (conf.dof_subj_dist_as_near_limit) { 
1112           camera_info.dof_values.near_limit = dist;
1113       } else {
1114         int v = ((((hyp_1e3 - 2*fl + 1000*dist)/500) + 1)/2);
1115             if (v>0) camera_info.dof_values.near_limit = (int)(m/v);
1116           }
1117       int v = ((((hyp_1e3 - 1000*dist)/500) + 1)/2);
1118       if (v>0) camera_info.dof_values.far_limit = (int)(m/v);
1119       if ((camera_info.dof_values.near_limit>0) && (camera_info.dof_values.far_limit>0)) {
1120           camera_info.dof_values.depth_of_field = camera_info.dof_values.far_limit - camera_info.dof_values.near_limit;
1121       }
1122     }
1123   }
1124   camera_info.dof_values.hyperfocal_distance_1e3 = hyp_1e3;
1125   camera_info.dof_values.hyperfocal_distance = hyp;
1126   f_focus_ok = (f_focus_ok && shooting_get_focus_ok());
1127   camera_info.dof_values.hyperfocal_valid = (f_focus_ok || (camera_info.dof_values.hyperfocal_valid && !f_hyp_calc));
1128   camera_info.dof_values.distance_valid = (f_focus_ok || (camera_info.dof_values.distance_valid && !f_dist_calc)||(camera_info.dof_values.hyperfocal_valid && shooting_get_focus_mode()));
1129   return;
1130 }
1131 
1132 int shooting_get_subject_distance()
1133 {
1134   shooting_update_dof_values();
1135   return camera_info.dof_values.subject_distance;
1136 }
1137 
1138 int shooting_get_hyperfocal_distance()
1139 {
1140   shooting_update_dof_values();
1141   return camera_info.dof_values.hyperfocal_distance;
1142 }
1143 
1144 static int focus_interlock_bypass = 0;
1145 
1146 short shooting_can_focus()
1147 {
1148     if(camera_info.state.mode_play) return 0 ;                 // don't focus in playback mode
1149 
1150     if(focus_interlock_bypass) return 1;                       // checks disabled, allow
1151 
1152     if( camera_info.state.mode_video == 1) return 1;           // FIXME : default to MF enabled in video mode for now
1153 
1154 #ifdef CAM_SD_OVER_IN_AFL
1155     if (shooting_get_prop(PROPCASE_AF_LOCK)==1 ) return 1;     // allow focus if AFL enabled and camera can focus that way?
1156 #endif
1157 #ifdef CAM_SD_OVER_IN_MF
1158     if (shooting_get_prop(PROPCASE_FOCUS_MODE)==1 ) return 1;  // allow focus if MF enabled and camera can focus that way?
1159 #endif
1160 
1161 #ifdef CAM_SD_OVER_IN_AF 
1162 #ifdef PROPCASE_CONTINUOUS_AF
1163     if (shooting_get_prop(PROPCASE_CONTINUOUS_AF)) return 0;   // don't focus in continuous AF mode,
1164 #endif
1165 #ifdef PROPCASE_SERVO_AF
1166     if (shooting_get_prop(PROPCASE_SERVO_AF)) return 0;        // don't focus in servo AF mode
1167 #endif
1168     if (    (shooting_get_prop(PROPCASE_AF_LOCK)==0)           // allow focus when in AF mode (i.e AFL or MF not enabled)?
1169          && (shooting_get_prop(PROPCASE_FOCUS_MODE)==0 )) return 1;
1170 #endif
1171     return 0;
1172 }
1173 
1174 short shooting_get_common_focus_mode()
1175 {
1176 #if !CAM_HAS_MANUAL_FOCUS
1177     return conf.subj_dist_override_koef;         // SD override state 0=OFF, 1=ON, 2=Infinity if camera has no native MF mode
1178 #else
1179     return shooting_get_focus_mode();            // 0=Auto, 1=manual
1180 #endif
1181 }
1182 
1183 int shooting_is_flash_ready()
1184 {
1185     int t = shooting_get_flash_mode();
1186     if ((t != 2) && (shooting_in_progress()))
1187         if (shooting_is_flash()) 
1188             return IsStrobeChargeCompleted();
1189     return 1;
1190 }
1191 
1192 //-------------------------------------------------------------------
1193 // Overrides for camera settings
1194 
1195 #if CAM_QUALITY_OVERRIDE
1196 void shooting_set_image_quality(int imq)
1197 {
1198     if (imq != 3)
1199     {
1200         set_property_case(PROPCASE_QUALITY, &imq, sizeof(imq));
1201     }
1202 }
1203 #endif
1204 
1205 void shooting_set_zoom(int v)
1206 {
1207     int dist;
1208     if (!camera_info.state.mode_play)
1209     {
1210         dist = shooting_get_subject_distance();
1211         lens_set_zoom_point(v);
1212 #if defined(CAM_NEED_SET_ZOOM_DELAY)
1213         msleep(CAM_NEED_SET_ZOOM_DELAY);
1214 #endif
1215         shooting_set_focus(dist, SET_NOW);
1216     }
1217 }
1218 
1219 void shooting_set_zoom_rel(int v)
1220 {
1221     int cv = shooting_get_zoom();
1222     if (!camera_info.state.mode_play)
1223     {
1224         shooting_set_zoom(cv+v);
1225     }
1226 }
1227 
1228 void shooting_set_zoom_speed(int v) {
1229     if (!camera_info.state.mode_play)
1230     {
1231         lens_set_zoom_speed(v);
1232     }
1233 }
1234 
1235 void set_focus_bypass(int m)
1236 {
1237         focus_interlock_bypass = m ;
1238 }
1239 
1240 void shooting_set_focus(int v, short is_now)
1241 {
1242     int s=v;
1243     if (!camera_info.state.mode_play)
1244     {
1245         if (is_now && shooting_can_focus()) 
1246         {
1247             if (conf.dof_subj_dist_as_near_limit)
1248             {
1249                 s=shooting_get_near_limit_f(v,shooting_get_min_real_aperture(),get_focal_length(lens_get_zoom_point()));
1250             }
1251             if (!conf.dof_use_exif_subj_dist && (s != INFINITY_DIST)) 
1252                 s+=shooting_get_lens_to_focal_plane_width();
1253             lens_set_focus_pos(s); 
1254         }
1255         else
1256             photo_param_put_off.subj_dist=v;
1257     }
1258 }
1259 
1260 void shooting_video_bitrate_change(int v)
1261 {
1262 #if CAM_CHDK_HAS_EXT_VIDEO_MENU
1263     int m[]={1,2,3,4,5,6,7,8,10,12};  // m[v]/4 = bitrate*1x
1264     if (v>=(sizeof(m)/sizeof(m[0])))
1265         v=(sizeof(m)/sizeof(m[0]))-1;
1266     change_video_tables(m[v],4);
1267 #endif
1268 }
1269 #ifdef CAM_MOVIEREC_NEWSTYLE
1270 void shooting_video_minbitrate_change(int v)
1271 {
1272     char m[]={1,2,3,4,5,6,7,8,9,10};  // m[v]/10 = bitrate*1x
1273     if (v>=(sizeof(m)/sizeof(m[0])))
1274         v=(sizeof(m)/sizeof(m[0]))-1;
1275     change_video_minbitrate(m[v],10);
1276 }
1277 unsigned int shooting_get_video_recorded_size_kb()
1278 {
1279     return get_video_recorded_size_kb();
1280 }
1281 #endif
1282 
1283 //-------------------------------------------------------------------
1284 
1285 #if CAM_REAR_CURTAIN
1286 void shooting_set_flash_sync_curtain(int curtain)
1287 {
1288     set_property_case(PROPCASE_FLASH_SYNC_CURTAIN, &curtain, sizeof(curtain));
1289 }
1290 #endif
1291 
1292 void unlock_optical_zoom(void)
1293 {
1294 #if CAM_CAN_UNLOCK_OPTICAL_ZOOM_IN_VIDEO
1295     extern void UnsetZoomForMovie();
1296     if (conf.unlock_optical_zoom_for_video)
1297         UnsetZoomForMovie();
1298 #endif
1299 
1300 #if CAM_EV_IN_VIDEO
1301     set_ev_video_avail(0);
1302 #endif
1303 }
1304 
1305 int shooting_set_mode_canon(int canonmode)
1306 {
1307     if (canonmode == -1 || !rec_mode_active())
1308         return 0;
1309     SetCurrentCaptureModeType(canonmode);
1310     // updated cached mode information immediately
1311     mode_get();
1312     return 1;
1313 }
1314 
1315 int shooting_set_mode_chdk(int mode)
1316 {
1317     return shooting_set_mode_canon(shooting_mode_chdk2canon(mode));
1318 }
1319 
1320 //-------------------------------------------------------------------
1321 // Bracketing functions
1322 
1323 EXPO_BRACKETING_VALUES bracketing;
1324 
1325 // Adjustment multipliers for bracketing types
1326 static int bracket_steps[4][2] = {
1327     {  1, -1 },     // +/- option - shoot at 0, +1, -1
1328     { -1, -1 },     // -   option - shoot at 0, -1, -2
1329     {  1,  1 },     // +   option - shoot at 0, +1, +2
1330     { -1,  1 },     // -/+ option - shoot at 0, -1, +1
1331 };
1332 static int bracket_delta[4][2] = {
1333     {  1,  0 },     // +/- option - shoot at 0, +1, -1
1334     {  1,  1 },     // -   option - shoot at 0, -1, -2
1335     {  1,  1 },     // +   option - shoot at 0, +1, +2
1336     {  1,  0 },     // -/+ option - shoot at 0, -1, +1
1337 };
1338 
1339 static void shooting_tv_bracketing(int when)
1340 {
1341     // first shot? (actually this is called just after the first shot has been taken)
1342     if (bracketing.shoot_counter == 0)
1343     {
1344         // if Tv override is enabled... (this was adapted from function shooting_expo_param_override() )
1345         if (is_tv_override_enabled)
1346         {
1347             // ...use Tv override value as seed for bracketing:
1348             bracketing.tv96 = shooting_get_tv96_override_value();
1349         }
1350         // Tv override is disabled, use camera's opinion of Tv for bracketing seed value.
1351         else 
1352         {
1353             if (!(camera_info.state.mode_shooting==MODE_M || camera_info.state.mode_shooting==MODE_TV || camera_info.state.mode_shooting==MODE_LONG_SHUTTER))
1354                 bracketing.tv96=shooting_get_tv96();
1355             else
1356                 bracketing.tv96=shooting_get_user_tv96();
1357         }
1358         bracketing.tv96_step = 32*conf.tv_bracket_value;
1359     }
1360 
1361     // Adjust delta TV value for shot based on shot number
1362     bracketing.dtv96 += (bracketing.tv96_step * bracket_delta[conf.bracket_type][bracketing.shoot_counter&1]);
1363     // Calculate new TV (note, need to subtract delta because higher values are shorter shutter speeds / darker images)
1364     short value = bracketing.tv96 - (bracketing.dtv96 * bracket_steps[conf.bracket_type][bracketing.shoot_counter&1]);
1365 
1366     // Inc for next shot
1367     bracketing.shoot_counter++;
1368 
1369     // Apply value for next shot to be taken
1370     shooting_set_tv96_direct(value, when);
1371 }
1372 
1373 static void shooting_av_bracketing(int when)
1374 {
1375     // first shot? (actually this is called just after the first shot has been taken)
1376     if (bracketing.shoot_counter == 0)
1377     {
1378         if (!(camera_info.state.mode_shooting==MODE_M || camera_info.state.mode_shooting==MODE_AV))
1379             bracketing.av96 = shooting_get_av96();
1380         else
1381             bracketing.av96 = shooting_get_user_av96();
1382         bracketing.av96_step = 32*conf.av_bracket_value;
1383     }
1384 
1385     // Adjust delta AV value for shot based on shot number
1386     bracketing.dav96 += (bracketing.av96_step * bracket_delta[conf.bracket_type][bracketing.shoot_counter&1]);
1387     // Calculate new AV (note, need to subtract delta because higher values are smaller apertures / darker images)
1388     short value = bracketing.av96 - (bracketing.dav96 * bracket_steps[conf.bracket_type][bracketing.shoot_counter&1]);
1389 
1390     // Inc for next shot
1391     bracketing.shoot_counter++;
1392 
1393     // Apply value for next shot to be taken
1394     if (value != bracketing.av96)
1395     {
1396         shooting_set_av96_direct(value, when);
1397 #ifdef CAM_AV_OVERRIDE_IRIS_FIX
1398         extern int MoveIrisWithAv(short*);
1399         MoveIrisWithAv(&value);
1400 #endif
1401     }
1402 }
1403 
1404 static void shooting_iso_bracketing(int when)
1405 {
1406     // first shot? (actually this is called just after the first shot has been taken)
1407     if (bracketing.shoot_counter == 0)
1408     {
1409         bracketing.iso = shooting_get_iso_real();
1410         bracketing.iso_step = shooting_iso_market_to_real(conf.iso_bracket_value);
1411     }
1412 
1413     // Adjust delta ISO value for shot based on shot number
1414     bracketing.diso += (bracketing.iso_step * bracket_delta[conf.bracket_type][bracketing.shoot_counter&1]);
1415     // Calculate new ISO (higher ISO = brighter image)
1416     short value = bracketing.iso + (bracketing.diso * bracket_steps[conf.bracket_type][bracketing.shoot_counter&1]);
1417     if (value <= 0) value = 50;
1418 
1419     // Inc for next shot
1420     bracketing.shoot_counter++;
1421 
1422     // Apply value for next shot to be taken
1423     shooting_set_iso_real(value, when);
1424 }
1425 
1426 static void shooting_subject_distance_bracketing(int when)
1427 {
1428     // first shot? (actually this is called just after the first shot has been taken)
1429     if (bracketing.shoot_counter == 0)
1430     {
1431         bracketing.subj_dist = shooting_get_subject_distance();
1432         bracketing.subj_dist_step = conf.subj_dist_bracket_value;
1433     }
1434 
1435     // Adjust delta SD value for shot based on shot number
1436     bracketing.dsubj_dist += (bracketing.subj_dist_step * bracket_delta[conf.bracket_type][bracketing.shoot_counter&1]);
1437     // Calculate new SD
1438     int value = bracketing.subj_dist + (bracketing.dsubj_dist * bracket_steps[conf.bracket_type][bracketing.shoot_counter&1]);
1439     if (value < CAMERA_MIN_DIST) value = CAMERA_MIN_DIST;
1440     else if (value > CAMERA_MAX_DIST) value = CAMERA_MAX_DIST;
1441 
1442     // Inc for next shot
1443     bracketing.shoot_counter++;
1444 
1445     // Apply value for next shot to be taken
1446     shooting_set_focus(value, when);
1447 }
1448 
1449 void bracketing_reset()
1450 {
1451     memset(&bracketing,0,sizeof(bracketing));
1452     memset(&photo_param_put_off,0,sizeof(photo_param_put_off));
1453     photo_param_put_off.tv96=PHOTO_PARAM_TV_NONE;
1454 }
1455 
1456 void bracketing_step(int when)
1457 {
1458     if (is_tv_bracketing_enabled)       shooting_tv_bracketing(when);
1459     else if (is_av_bracketing_enabled)  shooting_av_bracketing(when);
1460     else if (is_iso_bracketing_enabled) shooting_iso_bracketing(when);
1461     else if (is_sd_bracketing_enabled)  shooting_subject_distance_bracketing(when);           
1462 }
1463 
1464 void shooting_bracketing(void)
1465 {  
1466     if (shooting_get_drive_mode()!=0)  
1467     {
1468         if (camera_info.state.mode_shooting!=MODE_STITCH && camera_info.state.mode_shooting!=MODE_BEST_IMAGE)
1469         {
1470             if (camera_info.state.state_shooting_progress != SHOOTING_PROGRESS_PROCESSING)
1471                 bracketing_reset() ;
1472             bracketing_step(SET_NOW) ;
1473         }
1474     }
1475 }
1476 
1477 //-------------------------------------------------------------------
1478 // Other state test functions. 
1479 // Not strictly shooting related; but somehow ended up here.
1480 
1481 // Check if any shutter, aperture or ISO overrides active, for cameras
1482 // that require capt seq hack to override canon "quick press" behavior
1483 // caller must save regs
1484 int captseq_hack_override_active()
1485 {
1486     if (camera_info.state.state_kbd_script_run) {
1487         if ( photo_param_put_off.tv96 != PHOTO_PARAM_TV_NONE || photo_param_put_off.sv96 )
1488             return 1;
1489 #if CAM_HAS_IRIS_DIAPHRAGM
1490         if(photo_param_put_off.av96)
1491             return 1;
1492 #endif
1493     }
1494     if(conf.override_disable==1)
1495         return 0;
1496     if(is_iso_override_enabled)
1497         return 1;
1498     if(is_tv_override_enabled)
1499         return 1;
1500 #if CAM_HAS_IRIS_DIAPHRAGM
1501     if(is_av_override_enabled)
1502         return 1;
1503 #endif
1504     return 0;
1505 }
1506 
1507 #ifdef CAM_SIMPLE_MOVIE_STATUS
1508 extern int simple_movie_status;
1509 #else
1510 extern int movie_status;
1511 #endif
1512 
1513 void set_movie_status(int status)
1514 {
1515 #ifndef CAM_SIMPLE_MOVIE_STATUS
1516     switch(status)
1517     {
1518         case 1:
1519             if (movie_status == VIDEO_RECORD_IN_PROGRESS)
1520             {
1521                 movie_status = VIDEO_RECORD_STOPPED;
1522             }
1523             break;
1524         case 2:
1525             if (movie_status == VIDEO_RECORD_STOPPED)
1526             {
1527                 movie_status = VIDEO_RECORD_IN_PROGRESS;
1528             }
1529             break;
1530         case 3:
1531             if (movie_status == VIDEO_RECORD_STOPPED || movie_status == VIDEO_RECORD_IN_PROGRESS)
1532             {
1533                 movie_status = VIDEO_RECORD_STOPPING;
1534             }
1535             break;
1536     }
1537 #else // CAM_SIMPLE_MOVIE_STATUS
1538       // no known way to control the recording process
1539 #endif
1540 }
1541 
1542 int get_movie_status()
1543 {
1544 #ifndef CAM_SIMPLE_MOVIE_STATUS
1545     return movie_status;
1546 #else
1547     // firmware movie_status interpreted as: zero - not recording, nonzero - recording
1548     return simple_movie_status?VIDEO_RECORD_IN_PROGRESS:VIDEO_RECORD_STOPPED;
1549 #endif
1550 }
1551 
1552 // Return whether video is being recorded
1553 int is_video_recording()
1554 {
1555 #ifndef CAM_SIMPLE_MOVIE_STATUS
1556 #if defined(CAM_HAS_MOVIE_DIGEST_MODE)
1557     // If camera has movie digest mode then movie_status values are different than previous models
1558     // 'movie_status' values
1559     //      0 - after startup
1560     //      1 - movie recording stopped
1561     //      4 - movie recording in progress, or in 'movie digest' scene mode
1562     //      5 - movie recording stopping
1563     //      6 - in video mode, not recording
1564     return ((movie_status == VIDEO_RECORD_IN_PROGRESS) && (camera_info.state.mode_shooting != MODE_VIDEO_MOVIE_DIGEST));
1565 #else
1566     // 'movie_status' values
1567     //      0 - after startup
1568     //      1 - movie recording stopped
1569     //      4 - movie recording in progress
1570     //      5 - movie recording stopping
1571     return (movie_status > 1);
1572 #endif
1573 #else // CAM_SIMPLE_MOVIE_STATUS
1574     return (simple_movie_status!=0);
1575 #endif
1576 }
1577 
1578 // Converted from MODE_IS_VIDEO macro (philmoz July 2011)
1579 int mode_is_video(int m)
1580 {
1581     m = m & MODE_SHOOTING_MASK;
1582     return (m==MODE_VIDEO_STD ||
1583             m==MODE_VIDEO_SPEED ||
1584             m==MODE_VIDEO_COMPACT ||
1585             m==MODE_VIDEO_MY_COLORS ||
1586             m==MODE_VIDEO_COLOR_ACCENT ||
1587             m==MODE_VIDEO_COLOR_SWAP ||
1588             m==MODE_VIDEO_MINIATURE ||
1589             m==MODE_VIDEO_TIME_LAPSE ||
1590             m==MODE_VIDEO_IFRAME_MOVIE ||
1591             m==MODE_VIDEO_M ||
1592             m==MODE_VIDEO_STAR_TIME_LAPSE ||
1593             m==MODE_VIDEO_SHORT_CLIP
1594         // not clear if this should be considered a video mode ?
1595         //  m==MODE_VIDEO_MOVIE_DIGEST
1596         );
1597 }
1598 
1599 // override in platform/<cam>/main.c if playrec_mode is not found or different
1600 int __attribute__((weak)) rec_mode_active(void)
1601 {
1602     return (playrec_mode==2 || playrec_mode==4 || playrec_mode==5);
1603 }
1604 
1605 int mode_get(void)
1606 {
1607     int mode, t=0xFF;
1608 
1609     mode = (rec_mode_active())?MODE_REC:MODE_PLAY;
1610 
1611 #ifdef CAM_SWIVEL_SCREEN
1612     extern int screen_opened(void);     // 0 not open, non-zero open
1613     extern int screen_rotated(void);    // 0 not rotated, non-zero rotated
1614 
1615     mode |= (screen_opened())?MODE_SCREEN_OPENED:0;
1616     mode |= (screen_rotated())?MODE_SCREEN_ROTATED:0;
1617 #endif
1618 
1619 #ifdef CAM_SUPPORT_BITMAP_RES_CHANGE
1620     extern void update_screen_dimensions();
1621     update_screen_dimensions();
1622 #endif
1623 
1624     get_property_case(PROPCASE_SHOOTING_MODE, &t, 4);
1625 #ifdef CAM_MASK_VID_REC_ACTIVE
1626     mode |= shooting_mode_canon2chdk(t & (~CAM_MASK_VID_REC_ACTIVE));
1627 #else
1628     mode |= shooting_mode_canon2chdk(t);
1629 #endif
1630 
1631     // Set camera state variables from mode info
1632     camera_info.state.mode = mode;
1633     camera_info.state.mode_shooting = mode & MODE_SHOOTING_MASK;
1634     camera_info.state.mode_video = mode_is_video(mode);
1635     camera_info.state.mode_rec = ((mode & MODE_MASK) == MODE_REC);
1636     camera_info.state.mode_rec_or_review = camera_info.state.mode_rec && (recreview_hold==0 || conf.show_osd_in_review);
1637     camera_info.state.mode_play = ((mode & MODE_MASK) == MODE_PLAY);
1638     camera_info.state.mode_photo = (camera_info.state.mode_play || !(camera_info.state.mode_video || (mode&MODE_SHOOTING_MASK)==MODE_STITCH));
1639 
1640     return (mode);
1641 }
1642 
1643 //-------------------------------------------------------------------
1644 // Exposure control in video
1645 
1646 #if CAM_EV_IN_VIDEO
1647 
1648 int ev_video_avail;
1649 int ev_video;
1650 int save_tv_video;
1651 int tv_min_video;
1652 
1653 int get_ev_video_avail(void)
1654 {
1655     return ev_video_avail;
1656 }
1657 
1658 int get_ev_video(void)
1659 {
1660     return ev_video;
1661 }
1662 
1663 void set_ev_video_avail(int x)
1664 {
1665     extern void ExpCtrlTool_StartContiAE(int, int);
1666     extern void ExpCtrlTool_StopContiAE(int, int);
1667 
1668     if (ev_video_avail==x) return;
1669     ev_video_avail=x;
1670     if (x)
1671     {
1672         ev_video=0;
1673         ExpCtrlTool_StopContiAE(0,0);
1674         get_property_case(PROPCASE_TV,&save_tv_video,2);
1675         if (camera_info.state.mode_shooting==MODE_VIDEO_SPEED)
1676             tv_min_video=577;   // 1/60
1677         else
1678             tv_min_video=480;   //1/30
1679     }
1680     else
1681         ExpCtrlTool_StartContiAE(0,0);
1682 }
1683 
1684 void set_ev_video(int x)
1685 {
1686     short ev, tv;
1687     if ((x<-4)||(x>4))
1688         return;
1689 
1690     ev=48*x;
1691     tv=save_tv_video-ev;
1692     if (tv>=tv_min_video)
1693     {
1694         ev_video=x;
1695         extern short SetAE_ShutterSpeed(short* tv);
1696         SetAE_ShutterSpeed(&tv);
1697     }
1698 }
1699 
1700 #endif
1701 
1702 //-------------------------------------------------------------------
1703 // Main exposure override function called from capt_seq_task()
1704 
1705 static void shooting_set_flash_override()
1706 {
1707     int mode, flash, power;
1708     flash = 1;
1709 
1710     // Set flash mode & override
1711     if ((conf.flash_video_override && is_video_recording()) || (!conf.flash_video_override))
1712     {
1713         if (conf.flash_manual_override)
1714         {
1715             mode = 1;   // Manual flash
1716             power = conf.flash_video_override_power;
1717             set_property_case(PROPCASE_FLASH_ADJUST_MODE, &mode, sizeof(mode));
1718             set_property_case(PROPCASE_FLASH_FIRE, &flash, sizeof(flash));
1719             set_property_case(PROPCASE_FLASH_MANUAL_OUTPUT, &power, sizeof(power));
1720         }
1721         else if (conf.flash_enable_exp_comp)
1722         {
1723             mode = 0;   // Auto flash
1724             power = (conf.flash_exp_comp - 9) * 32;
1725             set_property_case(PROPCASE_FLASH_ADJUST_MODE, &mode, sizeof(mode));
1726             set_property_case(PROPCASE_FLASH_FIRE, &flash, sizeof(flash));
1727 #if defined(PROPCASE_FLASH_EXP_COMP)
1728             set_property_case(PROPCASE_FLASH_EXP_COMP, &power, sizeof(power));
1729 #endif
1730 #if defined(PROPCASE_EXT_FLASH_EXP_COMP)
1731             set_property_case(PROPCASE_EXT_FLASH_EXP_COMP, &power, sizeof(power));
1732 #endif
1733         }
1734     }
1735 }
1736 
1737 void shooting_expo_param_override_thumb(void)
1738 {
1739     if (((camera_info.state.state_kbd_script_run) || (usb_remote_active)) && (photo_param_put_off.tv96 != PHOTO_PARAM_TV_NONE))
1740     {
1741         shooting_set_tv96_direct(photo_param_put_off.tv96, SET_NOW);
1742         photo_param_put_off.tv96=PHOTO_PARAM_TV_NONE;
1743     }
1744     else if (is_tv_override_enabled)
1745     {
1746         shooting_set_tv96_direct(shooting_get_tv96_override_value(),SET_NOW);
1747     }
1748 
1749     if (((camera_info.state.state_kbd_script_run) || (usb_remote_active)) && (photo_param_put_off.sv96))
1750     {
1751         shooting_set_sv96(photo_param_put_off.sv96, SET_NOW);
1752         photo_param_put_off.sv96=0;
1753     }
1754     else if (is_iso_override_enabled)
1755         shooting_set_iso_real(shooting_get_iso_override_value(), SET_NOW);
1756     else if (conf.autoiso_enable && shooting_get_flash_mode()/*NOT FOR FLASH AUTO MODE*/ && autoiso_and_bracketing_overrides_are_enabled)
1757         shooting_set_autoiso(shooting_get_iso_mode());
1758 
1759     if (((camera_info.state.state_kbd_script_run) || (usb_remote_active)) && (photo_param_put_off.av96))
1760     {
1761         shooting_set_av96_direct(photo_param_put_off.av96, SET_NOW);
1762         photo_param_put_off.av96=0;
1763     }
1764     else if (is_av_override_enabled)
1765         shooting_set_av96_direct(shooting_get_av96_override_value(), SET_NOW);
1766 
1767     if (((camera_info.state.state_kbd_script_run) || (usb_remote_active)) && (photo_param_put_off.subj_dist))
1768     {
1769         shooting_set_focus(photo_param_put_off.subj_dist, SET_NOW);
1770         photo_param_put_off.subj_dist=0;
1771     }
1772     else if (is_sd_override_enabled)
1773         shooting_set_focus(shooting_get_subject_distance_override_value(), SET_NOW);
1774 
1775 #if CAM_HAS_ND_FILTER
1776     if ((camera_info.state.state_kbd_script_run) && (photo_param_put_off.nd_filter))
1777     {
1778         shooting_set_nd_filter_state(photo_param_put_off.nd_filter, SET_NOW);
1779         photo_param_put_off.nd_filter=0;
1780     }
1781     else if (conf.nd_filter_state && !(conf.override_disable==1))
1782         shooting_set_nd_filter_state(conf.nd_filter_state, SET_NOW);
1783 #endif
1784 
1785 #if CAM_REAR_CURTAIN
1786     shooting_set_flash_sync_curtain(conf.flash_sync_curtain);
1787 #endif
1788 
1789     // Set flash mode & override
1790     shooting_set_flash_override();
1791 
1792 #if CAM_QUALITY_OVERRIDE
1793     // this doesn't really need to be set in the override hook.
1794     // should only need to be set once if the users doesn't change back, but doing it here ensures it is set
1795     shooting_set_image_quality(conf.fast_image_quality);
1796 #endif
1797 
1798     libscriptapi->shoot_hook(SCRIPT_SHOOT_HOOK_PRESHOOT);
1799 }
1800 
1801 // Override ISO settings only
1802 // need to do this before exposure calc for ISO, as well as after on some cameras
1803 void shooting_expo_iso_override_thumb(void)
1804 {
1805     if ((camera_info.state.state_kbd_script_run) && (photo_param_put_off.sv96))
1806     {
1807         shooting_set_sv96(photo_param_put_off.sv96, SET_NOW);
1808         // photo_param_put_off.sv96 is not reset here, it will be reset in next call to shooting_expo_param_override
1809     }
1810     else if (is_iso_override_enabled)
1811         shooting_set_iso_real(shooting_get_iso_override_value(), SET_NOW);
1812     else if (conf.autoiso_enable && shooting_get_flash_mode()/*NOT FOR FLASH AUTO MODE*/ && autoiso_and_bracketing_overrides_are_enabled)
1813         shooting_set_autoiso(shooting_get_iso_mode());
1814 
1815 #if defined(CAM_HAS_ND_FILTER) && defined(CAM_HAS_NATIVE_ND_FILTER)
1816     if ((camera_info.state.state_kbd_script_run) && (photo_param_put_off.nd_filter))
1817     {
1818         shooting_set_nd_filter_state(photo_param_put_off.nd_filter, SET_NOW);
1819         //photo_param_put_off.nd_filter=0;
1820     }
1821     else if (conf.nd_filter_state && !(conf.override_disable==1))
1822         shooting_set_nd_filter_state(conf.nd_filter_state, SET_NOW);
1823 #endif
1824 
1825     // Set flash mode & override
1826     shooting_set_flash_override();
1827 }
1828 
1829 /*
1830 switch camera between playback (0) and record(1)
1831 uses switch_mode_usb if a usb connection is present
1832 */
1833 void shooting_set_playrec_mode(int mode)
1834 {
1835     // use PTP compatible switch if usb bit power detected, and USB remote not configured to use USB
1836     // TODO should just check the USB bit seen by canon firmware (after masking)
1837     if (get_usb_bit() && !(conf.remote_enable == 1 && conf.remote_input_channel == REMOTE_INPUT_USB))
1838     {
1839         switch_mode_usb(mode);
1840         return;
1841     }
1842     if(mode)
1843         levent_set_record();
1844     else
1845         levent_set_play();
1846 }

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