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     int subj_dist;
  43     short av96;
  44     short tv96;
  45     short sv96;
  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 chosen 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 #if (CAM_HAS_IRIS_DIAPHRAGM || CAM_DRAW_EXPOSITION)
 686 static int find_nearest_aperture_entry(short av96)
 687 {
 688     if (av96 <= aperture_sizes_table[0].prop_id)
 689         return 0;
 690 
 691     int i;
 692     for (i=0; i<AS_SIZE-1; i++)
 693     {
 694         if ((av96 > aperture_sizes_table[i].prop_id) && (av96 <= aperture_sizes_table[i+1].prop_id))
 695         {
 696             if ((av96 - aperture_sizes_table[i].prop_id) < (aperture_sizes_table[i+1].prop_id - av96))
 697                 return i;
 698             else
 699                 return i+1;
 700         }
 701     }
 702 
 703     return AS_SIZE-1;
 704 }
 705 #endif
 706 
 707 // Convert 'av96' value to nearest Canon value from aperture_sizes_table
 708 #if CAM_HAS_IRIS_DIAPHRAGM
 709 short find_canon_aperture(short av96)
 710 {
 711     return aperture_sizes_table[find_nearest_aperture_entry(av96)].prop_id;
 712 }
 713 #endif
 714 
 715 void shooting_set_av96_direct(short av96, short is_now)
 716 {
 717 #if CAM_HAS_IRIS_DIAPHRAGM
 718     if (!camera_info.state.mode_play)
 719     {
 720         if(is_now)
 721         {
 722             set_property_case(PROPCASE_AV, &av96, sizeof(av96));
 723 #ifdef PROPCASE_AV2
 724             set_property_case(PROPCASE_AV2, &av96, sizeof(av96));   // Save override to property that will update JPEG header & Canon OSD
 725 #endif
 726         }
 727         else
 728             photo_param_put_off.av96 = av96;
 729     }
 730 #endif
 731 }
 732 
 733 void shooting_set_av96(short av96, short is_now)
 734 {
 735 #if CAM_HAS_IRIS_DIAPHRAGM
 736     if (!camera_info.state.mode_play)
 737         shooting_set_av96_direct(find_canon_aperture(av96), is_now);
 738 #endif
 739 }
 740 
 741 // User Av
 742 
 743 short shooting_get_user_av96()
 744 {
 745 #if CAM_HAS_IRIS_DIAPHRAGM
 746     return shooting_get_prop(PROPCASE_USER_AV);
 747 #else
 748     return 0;
 749 #endif
 750 }
 751 
 752 int shooting_get_user_av_id()
 753 {
 754 #if CAM_HAS_IRIS_DIAPHRAGM
 755     return aperture_sizes_table[find_nearest_aperture_entry(shooting_get_user_av96())].prop_id;
 756 #else
 757     return 0;
 758 #endif
 759 }
 760 
 761 void shooting_set_user_av_by_id(int v)
 762 {
 763 #if CAM_HAS_IRIS_DIAPHRAGM
 764     long i;
 765     if (!camera_info.state.mode_play)
 766     {
 767         for (i=0;i<AS_SIZE;i++)
 768         {
 769             if (aperture_sizes_table[i].id == v)
 770             {
 771                 short vv = aperture_sizes_table[i].prop_id;
 772                 set_property_case(PROPCASE_USER_AV, &vv, sizeof(vv));
 773                 return;
 774             }
 775         }
 776     }
 777 #endif
 778 }
 779 
 780 void shooting_set_user_av_by_id_rel(int v)
 781 {
 782 #if CAM_HAS_IRIS_DIAPHRAGM
 783     if (!camera_info.state.mode_play)
 784     {
 785         int cv = shooting_get_user_av_id();
 786         shooting_set_user_av_by_id(cv+v);
 787     }
 788 #endif
 789 }
 790 
 791 void shooting_set_user_av96(short av96)
 792 {
 793 #if CAM_HAS_IRIS_DIAPHRAGM
 794     if (!camera_info.state.mode_play)
 795     {
 796         av96 = find_canon_aperture(av96);
 797         set_property_case(PROPCASE_USER_AV, &av96, sizeof(av96));
 798     }
 799 #endif
 800 }
 801 
 802 // Only needed on VxWorks
 803 #if CAM_DRAW_EXPOSITION
 804 // compare PROPCASE_AV with aperture_sizes_table
 805 char* shooting_get_av_str()
 806 {
 807     return (char*)aperture_sizes_table[find_nearest_aperture_entry(shooting_get_av96())].name;
 808 }
 809 #endif
 810 
 811 void shooting_set_nd_filter_state(short v, short is_now)
 812 {
 813 #if CAM_HAS_ND_FILTER
 814     if (!camera_info.state.mode_play)
 815     {
 816         if (is_now)
 817         {
 818 #if defined(CAM_ND_SET_AV_VALUE)
 819             short av;
 820             get_property_case(PROPCASE_MIN_AV,&av,sizeof(av));
 821             if(v==1) {
 822                 av += shooting_get_nd_value_ev96();
 823             }
 824             set_property_case(PROPCASE_AV, &av, sizeof(av));
 825 #if defined(PROPCASE_AV2)
 826             set_property_case(PROPCASE_AV2, &av, sizeof(av));
 827 #endif
 828 #endif
 829             if (v==1) {
 830                 PutInNdFilter();
 831             }
 832             else if (v==2)
 833                 PutOutNdFilter();
 834 #if defined(CAM_HAS_NATIVE_ND_FILTER) && defined(PROPCASE_ND_FILTER_STATE)
 835             int n = (v==1) ? 1 : 0;
 836             set_property_case(PROPCASE_ND_FILTER_STATE, &n, sizeof(n));
 837 #endif
 838         }
 839         else
 840             photo_param_put_off.nd_filter = v;
 841     }
 842 #endif
 843 }
 844 
 845 
 846 // get usable Av range (iris only)
 847 // Appears to be the full range, including smaller (higher F/ number) than available in Canon UI.
 848 // NOTE canon functions Min and Max refer to aperture size, CHDK refer to av96 values
 849 // returns -1 if unavailable (0 is a valid Av = F/1.0)
 850 short shooting_get_min_av96()
 851 {
 852 #if CAM_HAS_IRIS_DIAPHRAGM
 853     extern short GetUsableMaxAv(void);
 854     if (camera_info.state.mode_play)
 855     {
 856         return -1;
 857     }
 858     return GetUsableMaxAv();
 859 #else
 860     return -1;
 861 #endif
 862 }
 863 
 864 short shooting_get_max_av96()
 865 {
 866 #if CAM_HAS_IRIS_DIAPHRAGM
 867     extern short GetUsableMinAv(void);
 868     if (camera_info.state.mode_play)
 869     {
 870         return -1;
 871     }
 872     return GetUsableMinAv();
 873 #else
 874     return -1;
 875 #endif
 876 }
 877 
 878 //-------------------------------------------------------------------
 879 // Av section - end
 880 //-------------------------------------------------------------------
 881 
 882 //-------------------------------------------------------------------
 883 // Init
 884 
 885 void shooting_init()
 886 {
 887     photo_param_put_off.tv96 = PHOTO_PARAM_TV_NONE;
 888     iso_init();
 889 }
 890 
 891 //-------------------------------------------------------------------
 892 // Get file related info
 893 
 894 #ifdef CAM_FILE_COUNTER_IS_VAR
 895 extern long file_counter_var;
 896 long get_file_counter()
 897 {
 898     return file_counter_var;
 899 }
 900 long get_exposure_counter()
 901 {
 902     long v = get_file_counter();
 903     v = ((v>>4)&0x3FFF);
 904     return v;
 905 }
 906 #else
 907 extern const unsigned int param_file_counter;
 908 
 909 long get_file_counter()
 910 {
 911     long v = 0;
 912     get_parameter_data(param_file_counter, &v, 4);
 913     return v;
 914 }
 915 
 916 long get_exposure_counter()
 917 {
 918     long v = 0;
 919     get_parameter_data(param_file_counter, &v, 4);
 920     v = ((v>>4)&0x3FFF);
 921     return v;
 922 }
 923 #endif
 924 
 925 #ifndef CAM_DATE_FOLDER_NAMING
 926 void get_target_dir_name(char *dir) {
 927     extern long get_target_dir_num();
 928     sprintf(dir,"A/DCIM/%03dCANON",get_target_dir_num());
 929 }
 930 #endif
 931 
 932 //-------------------------------------------------------------------
 933 // Convert values to/from APEX 96
 934 
 935 static const double K = 12.5;               // K is the reflected-light meter calibration constant
 936 
 937 int shooting_get_luminance()                // http://en.wikipedia.org/wiki/APEX_system
 938 {
 939     short bv = shooting_get_bv96();
 940     int b = (int)(100 * K * pow(2.0,((double)(bv-168)/96.0)));
 941     return b;
 942 }
 943 
 944 //-------------------------------------------------------------------
 945 // Get override values
 946 
 947 int shooting_get_subject_distance_override_value()
 948 {
 949     if (conf.subj_dist_override_koef != SD_OVERRIDE_INFINITY)
 950         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()));
 951     else
 952         return INFINITY_DIST;
 953 }
 954 
 955 //-------------------------------------------------------------------
 956 // Get shooting value, with conversion / calculation
 957 
 958 int shooting_mode_canon2chdk(int canonmode)
 959 {
 960         int i;
 961         for (i=0; i < MODESCNT; i++)
 962     {
 963                 if (modemap[i].canonmode == canonmode)
 964                         return modemap[i].hackmode;
 965         }
 966         return 0;
 967 }
 968 
 969 int shooting_mode_chdk2canon(int hackmode)
 970 {
 971         int i;
 972         for (i=0; i < MODESCNT; i++)
 973     {
 974                 if (modemap[i].hackmode == hackmode)
 975                         return modemap[i].canonmode;
 976         }
 977         return -1; // 0 is a valid mode on old cameras!
 978 }
 979 
 980 
 981 
 982 int sd_over_modes()
 983 {  return ( 0
 984 #ifdef CAM_SD_OVER_IN_AF 
 985           + 1
 986 #endif
 987 #ifdef CAM_SD_OVER_IN_AFL
 988           + 2 
 989 #endif
 990 #ifdef CAM_SD_OVER_IN_MF
 991           + 4
 992 #endif
 993 );  }
 994 
 995 int shooting_get_lens_to_focal_plane_width()
 996 {
 997     return (int)(lens_get_focus_pos()-lens_get_focus_pos_from_lens());
 998 }
 999 
1000 int shooting_get_hyperfocal_distance_1e3_f(int av, int fl)
1001 {
1002   if ((av>0) && (fl>0) && (camera_info.circle_of_confusion>0))
1003     return (int)(((((double)fl*fl)/(av*camera_info.circle_of_confusion)*1000)+fl)+0.5);
1004   else return (-1);
1005 }
1006 
1007 int shooting_get_near_limit_f(int s, int av, int fl)
1008 {
1009   if (shooting_is_infinity_distance()) return (-1);
1010   int h_1e3 = shooting_get_hyperfocal_distance_1e3_f(av, fl);
1011   double m = ((double)(h_1e3 - fl)+500)/1000*s;
1012   int v = ((h_1e3 - 2*fl + 1000*s)+500)/1000;
1013   if ((m>0) && (v>0)) return (int)((2*m/v+1)/2);
1014   else return (-1);
1015 }
1016 
1017 int shooting_get_far_limit_f(int s, int av, int fl)
1018 {
1019   if (shooting_is_infinity_distance()) return (-1);
1020   int h_1e3 = shooting_get_hyperfocal_distance_1e3_f(av, fl);
1021   double m = ((double)(h_1e3 - fl)+500)/1000*s;
1022   int v = ((h_1e3 - 1000*s)+500)/1000;
1023   if ((m>0) && (v>0)) return (int)((2*m/v+1)/2);
1024   else return (-1); 
1025 }
1026 
1027 short shooting_is_infinity_distance()
1028 {
1029   return (shooting_get_lens_to_focal_plane_width() == 0)?1:0;
1030 }
1031 
1032 int shooting_get_canon_subject_distance()
1033 {
1034         if (conf.dof_use_exif_subj_dist) 
1035           return shooting_get_exif_subject_dist();
1036         else
1037           return (lens_get_focus_pos_from_lens());
1038 }
1039 
1040 int shooting_get_subject_distance_()
1041 {
1042    if (!conf.dof_subj_dist_as_near_limit) return shooting_get_canon_subject_distance();
1043    else return shooting_get_far_limit_f(shooting_get_canon_subject_distance(),
1044                                          shooting_get_min_real_aperture(), 
1045                                          get_focal_length(lens_get_zoom_point()));
1046 }
1047 
1048 void shooting_update_dof_values()
1049 {
1050   int hyp, hyp_1e3, av_1e3, v;
1051   int dist = shooting_get_subject_distance_();
1052   int zoom_point = lens_get_zoom_point();
1053   int fl = get_focal_length(zoom_point);
1054   short f_focus_ok = shooting_get_focus_ok();
1055   short f_hyp_calc = 0, f_dist_calc = 0;
1056   short min_av96_zoom_point = min_av96_zoom_point_tbl[zoom_point];
1057   short av96 = shooting_get_user_av96();
1058   short curr_av96 = shooting_get_current_av96();
1059   short prop_av96 = shooting_get_av96();
1060   short min_av96;
1061 
1062   if (!min_av96_zoom_point_tbl) {
1063     min_av96_zoom_point_tbl = (short *) malloc(zoom_points * sizeof(short));
1064     if (min_av96_zoom_point_tbl) {
1065       memset(min_av96_zoom_point_tbl, 0, zoom_points * sizeof(short));
1066       min_av96_zoom_point = 0;
1067     }
1068   } else min_av96_zoom_point = min_av96_zoom_point_tbl[zoom_point]; 
1069 
1070   if (min_av96_zoom_point==0 && shooting_in_progress()) { 
1071     get_property_case(PROPCASE_MIN_AV, &min_av96, sizeof(min_av96));
1072     min_av96_zoom_point_tbl[zoom_point] = min_av96;
1073   }
1074 
1075   if ((camera_info.state.mode_shooting==MODE_M || camera_info.state.mode_shooting==MODE_AV) && (av96>0) && !f_focus_ok) { 
1076     if (av96 < min_av96_zoom_point) av96 = min_av96_zoom_point;
1077   }
1078   else av96 = (abs(curr_av96-prop_av96)<2)?prop_av96:curr_av96;
1079 
1080   av_1e3 = shooting_get_aperture_from_av96(av96);
1081   hyp_1e3 = camera_info.dof_values.hyperfocal_distance_1e3;
1082   hyp = camera_info.dof_values.hyperfocal_distance;
1083   
1084   if (camera_info.dof_values.aperture_value!=av_1e3 || camera_info.dof_values.focal_length!=fl || (hyp_1e3<0)) {
1085     //calc new hyperfocal distance and min stack distance
1086     f_hyp_calc = 1;
1087     hyp_1e3 = -1;
1088     hyp = -1;
1089     camera_info.dof_values.aperture_value = av_1e3;
1090     camera_info.dof_values.focal_length = fl;
1091     hyp_1e3 = shooting_get_hyperfocal_distance_1e3_f(av_1e3, fl);
1092     if (hyp_1e3>0) {
1093       hyp = (hyp_1e3+500)/1000;
1094       camera_info.dof_values.min_stack_distance = CAMERA_MAX_DIST;
1095       v = ((hyp_1e3 - fl)/250 + 2 + 1)/2;
1096       if (v>0) {
1097         int m = ((fl*((fl - hyp_1e3)/1000 - 1)/500)/v + 1)/2;
1098         int m2 = (int)((((double)hyp*(2*fl - hyp_1e3)/1000))/v + 0.5);
1099         camera_info.dof_values.min_stack_distance = sqrt(m*m - m2) - m;
1100       }  
1101     }
1102   }
1103 
1104   if ((camera_info.dof_values.subject_distance!=dist || (camera_info.dof_values.hyperfocal_distance_1e3!=hyp_1e3)) && (hyp_1e3>0)) {
1105     //calc new NEAR, FAR, DOF values
1106     f_dist_calc = 1;
1107     camera_info.dof_values.subject_distance = dist;
1108     camera_info.dof_values.near_limit = -1;
1109     camera_info.dof_values.far_limit = -1;
1110     camera_info.dof_values.depth_of_field = -1;
1111     if ((av_1e3>0) && (fl>0) && (dist>0) && (shooting_is_infinity_distance()==0) && (hyp_1e3>0)) {
1112       double m = ((double)(hyp_1e3 - fl)/1000 * dist) + 0.5;
1113       if (conf.dof_subj_dist_as_near_limit) { 
1114           camera_info.dof_values.near_limit = dist;
1115       } else {
1116         int v = ((((hyp_1e3 - 2*fl + 1000*dist)/500) + 1)/2);
1117             if (v>0) camera_info.dof_values.near_limit = (int)(m/v);
1118           }
1119       int v = ((((hyp_1e3 - 1000*dist)/500) + 1)/2);
1120       if (v>0) camera_info.dof_values.far_limit = (int)(m/v);
1121       if ((camera_info.dof_values.near_limit>0) && (camera_info.dof_values.far_limit>0)) {
1122           camera_info.dof_values.depth_of_field = camera_info.dof_values.far_limit - camera_info.dof_values.near_limit;
1123       }
1124     }
1125   }
1126   camera_info.dof_values.hyperfocal_distance_1e3 = hyp_1e3;
1127   camera_info.dof_values.hyperfocal_distance = hyp;
1128   f_focus_ok = (f_focus_ok && shooting_get_focus_ok());
1129   camera_info.dof_values.hyperfocal_valid = (f_focus_ok || (camera_info.dof_values.hyperfocal_valid && !f_hyp_calc));
1130   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()));
1131   return;
1132 }
1133 
1134 int shooting_get_subject_distance()
1135 {
1136   shooting_update_dof_values();
1137   return camera_info.dof_values.subject_distance;
1138 }
1139 
1140 int shooting_get_hyperfocal_distance()
1141 {
1142   shooting_update_dof_values();
1143   return camera_info.dof_values.hyperfocal_distance;
1144 }
1145 
1146 static int focus_interlock_bypass = 0;
1147 
1148 short shooting_can_focus()
1149 {
1150     if(camera_info.state.mode_play) return 0 ;                 // don't focus in playback mode
1151 
1152     if(focus_interlock_bypass) return 1;                       // checks disabled, allow
1153 
1154     if( camera_info.state.mode_video == 1) return 1;           // FIXME : default to MF enabled in video mode for now
1155 
1156 #ifdef CAM_SD_OVER_IN_AFL
1157     if (shooting_get_prop(PROPCASE_AF_LOCK)==1 ) return 1;     // allow focus if AFL enabled and camera can focus that way?
1158 #endif
1159 #ifdef CAM_SD_OVER_IN_MF
1160     if (shooting_get_prop(PROPCASE_FOCUS_MODE)==1 ) return 1;  // allow focus if MF enabled and camera can focus that way?
1161 #endif
1162 
1163 #ifdef CAM_SD_OVER_IN_AF 
1164 #ifdef PROPCASE_CONTINUOUS_AF
1165     if (shooting_get_prop(PROPCASE_CONTINUOUS_AF)) return 0;   // don't focus in continuous AF mode,
1166 #endif
1167 #ifdef PROPCASE_SERVO_AF
1168     if (shooting_get_prop(PROPCASE_SERVO_AF)) return 0;        // don't focus in servo AF mode
1169 #endif
1170     if (    (shooting_get_prop(PROPCASE_AF_LOCK)==0)           // allow focus when in AF mode (i.e AFL or MF not enabled)?
1171          && (shooting_get_prop(PROPCASE_FOCUS_MODE)==0 )) return 1;
1172 #endif
1173     return 0;
1174 }
1175 
1176 short shooting_get_common_focus_mode()
1177 {
1178 #if !CAM_HAS_MANUAL_FOCUS
1179     return conf.subj_dist_override_koef;         // SD override state 0=OFF, 1=ON, 2=Infinity if camera has no native MF mode
1180 #else
1181     return shooting_get_focus_mode();            // 0=Auto, 1=manual
1182 #endif
1183 }
1184 
1185 int shooting_is_flash_ready()
1186 {
1187     int t = shooting_get_flash_mode();
1188     if ((t != 2) && (shooting_in_progress()))
1189         if (shooting_is_flash()) 
1190             return IsStrobeChargeCompleted();
1191     return 1;
1192 }
1193 
1194 //-------------------------------------------------------------------
1195 // Overrides for camera settings
1196 
1197 #if CAM_QUALITY_OVERRIDE
1198 void shooting_set_image_quality(int imq)
1199 {
1200     if (imq != 3)
1201     {
1202         set_property_case(PROPCASE_QUALITY, &imq, sizeof(imq));
1203     }
1204 }
1205 #endif
1206 
1207 void shooting_set_zoom(int v)
1208 {
1209     int dist;
1210     if (!camera_info.state.mode_play)
1211     {
1212         dist = shooting_get_subject_distance();
1213         lens_set_zoom_point(v);
1214 #if defined(CAM_NEED_SET_ZOOM_DELAY)
1215         msleep(CAM_NEED_SET_ZOOM_DELAY);
1216 #endif
1217         shooting_set_focus(dist, SET_NOW);
1218     }
1219 }
1220 
1221 void shooting_set_zoom_rel(int v)
1222 {
1223     int cv = shooting_get_zoom();
1224     if (!camera_info.state.mode_play)
1225     {
1226         shooting_set_zoom(cv+v);
1227     }
1228 }
1229 
1230 void shooting_set_zoom_speed(int v) {
1231     if (!camera_info.state.mode_play)
1232     {
1233         lens_set_zoom_speed(v);
1234     }
1235 }
1236 
1237 void set_focus_bypass(int m)
1238 {
1239         focus_interlock_bypass = m ;
1240 }
1241 
1242 void shooting_set_focus(int v, short is_now)
1243 {
1244     int s=v;
1245     if (!camera_info.state.mode_play)
1246     {
1247         if (is_now && shooting_can_focus()) 
1248         {
1249             if (conf.dof_subj_dist_as_near_limit)
1250             {
1251                 s=shooting_get_near_limit_f(v,shooting_get_min_real_aperture(),get_focal_length(lens_get_zoom_point()));
1252             }
1253             if (!conf.dof_use_exif_subj_dist && (s != INFINITY_DIST)) 
1254                 s+=shooting_get_lens_to_focal_plane_width();
1255             lens_set_focus_pos(s); 
1256         }
1257         else
1258             photo_param_put_off.subj_dist=v;
1259     }
1260 }
1261 
1262 void shooting_video_bitrate_change(int v)
1263 {
1264 #if CAM_CHDK_HAS_EXT_VIDEO_MENU
1265     int m[]={1,2,3,4,5,6,7,8,10,12};  // m[v]/4 = bitrate*1x
1266     if (v>=(sizeof(m)/sizeof(m[0])))
1267         v=(sizeof(m)/sizeof(m[0]))-1;
1268     change_video_tables(m[v],4);
1269 #endif
1270 }
1271 #ifdef CAM_MOVIEREC_NEWSTYLE
1272 void shooting_video_minbitrate_change(int v)
1273 {
1274     char m[]={1,2,3,4,5,6,7,8,9,10};  // m[v]/10 = bitrate*1x
1275     if (v>=(sizeof(m)/sizeof(m[0])))
1276         v=(sizeof(m)/sizeof(m[0]))-1;
1277     change_video_minbitrate(m[v],10);
1278 }
1279 unsigned int shooting_get_video_recorded_size_kb()
1280 {
1281     return get_video_recorded_size_kb();
1282 }
1283 #endif
1284 
1285 //-------------------------------------------------------------------
1286 
1287 #if CAM_REAR_CURTAIN
1288 void shooting_set_flash_sync_curtain(int curtain)
1289 {
1290     set_property_case(PROPCASE_FLASH_SYNC_CURTAIN, &curtain, sizeof(curtain));
1291 }
1292 #endif
1293 
1294 void unlock_optical_zoom(void)
1295 {
1296 #if CAM_CAN_UNLOCK_OPTICAL_ZOOM_IN_VIDEO
1297     extern void UnsetZoomForMovie();
1298     if (conf.unlock_optical_zoom_for_video)
1299         UnsetZoomForMovie();
1300 #endif
1301 
1302 #if CAM_EV_IN_VIDEO
1303     set_ev_video_avail(0);
1304 #endif
1305 }
1306 
1307 int shooting_set_mode_canon(int canonmode)
1308 {
1309     if (canonmode == -1 || !rec_mode_active())
1310         return 0;
1311     SetCurrentCaptureModeType(canonmode);
1312     // updated cached mode information immediately
1313     mode_get();
1314     return 1;
1315 }
1316 
1317 int shooting_set_mode_chdk(int mode)
1318 {
1319     return shooting_set_mode_canon(shooting_mode_chdk2canon(mode));
1320 }
1321 
1322 //-------------------------------------------------------------------
1323 // Bracketing functions
1324 
1325 EXPO_BRACKETING_VALUES bracketing;
1326 
1327 // Adjustment multipliers for bracketing types
1328 static int bracket_steps[4][2] = {
1329     {  1, -1 },     // +/- option - shoot at 0, +1, -1
1330     { -1, -1 },     // -   option - shoot at 0, -1, -2
1331     {  1,  1 },     // +   option - shoot at 0, +1, +2
1332     { -1,  1 },     // -/+ option - shoot at 0, -1, +1
1333 };
1334 static int bracket_delta[4][2] = {
1335     {  1,  0 },     // +/- option - shoot at 0, +1, -1
1336     {  1,  1 },     // -   option - shoot at 0, -1, -2
1337     {  1,  1 },     // +   option - shoot at 0, +1, +2
1338     {  1,  0 },     // -/+ option - shoot at 0, -1, +1
1339 };
1340 
1341 static void shooting_tv_bracketing(int when)
1342 {
1343     // first shot? (actually this is called just after the first shot has been taken)
1344     if (bracketing.shoot_counter == 0)
1345     {
1346         // if Tv override is enabled... (this was adapted from function shooting_expo_param_override() )
1347         if (is_tv_override_enabled)
1348         {
1349             // ...use Tv override value as seed for bracketing:
1350             bracketing.tv96 = shooting_get_tv96_override_value();
1351         }
1352         // Tv override is disabled, use camera's opinion of Tv for bracketing seed value.
1353         else 
1354         {
1355             if (!(camera_info.state.mode_shooting==MODE_M || camera_info.state.mode_shooting==MODE_TV || camera_info.state.mode_shooting==MODE_LONG_SHUTTER))
1356                 bracketing.tv96=shooting_get_tv96();
1357             else
1358                 bracketing.tv96=shooting_get_user_tv96();
1359         }
1360         bracketing.tv96_step = 32*conf.tv_bracket_value;
1361     }
1362 
1363     // Adjust delta TV value for shot based on shot number
1364     bracketing.dtv96 += (bracketing.tv96_step * bracket_delta[conf.bracket_type][bracketing.shoot_counter&1]);
1365     // Calculate new TV (note, need to subtract delta because higher values are shorter shutter speeds / darker images)
1366     short value = bracketing.tv96 - (bracketing.dtv96 * bracket_steps[conf.bracket_type][bracketing.shoot_counter&1]);
1367 
1368     // Inc for next shot
1369     bracketing.shoot_counter++;
1370 
1371     // Apply value for next shot to be taken
1372     shooting_set_tv96_direct(value, when);
1373 }
1374 
1375 static void shooting_av_bracketing(int when)
1376 {
1377     // first shot? (actually this is called just after the first shot has been taken)
1378     if (bracketing.shoot_counter == 0)
1379     {
1380         if (!(camera_info.state.mode_shooting==MODE_M || camera_info.state.mode_shooting==MODE_AV))
1381             bracketing.av96 = shooting_get_av96();
1382         else
1383             bracketing.av96 = shooting_get_user_av96();
1384         bracketing.av96_step = 32*conf.av_bracket_value;
1385     }
1386 
1387     // Adjust delta AV value for shot based on shot number
1388     bracketing.dav96 += (bracketing.av96_step * bracket_delta[conf.bracket_type][bracketing.shoot_counter&1]);
1389     // Calculate new AV (note, need to subtract delta because higher values are smaller apertures / darker images)
1390     short value = bracketing.av96 - (bracketing.dav96 * bracket_steps[conf.bracket_type][bracketing.shoot_counter&1]);
1391 
1392     // Inc for next shot
1393     bracketing.shoot_counter++;
1394 
1395     // Apply value for next shot to be taken
1396     if (value != bracketing.av96)
1397     {
1398         shooting_set_av96_direct(value, when);
1399 #ifdef CAM_AV_OVERRIDE_IRIS_FIX
1400         extern int MoveIrisWithAv(short*);
1401         MoveIrisWithAv(&value);
1402 #endif
1403     }
1404 }
1405 
1406 static void shooting_iso_bracketing(int when)
1407 {
1408     // first shot? (actually this is called just after the first shot has been taken)
1409     if (bracketing.shoot_counter == 0)
1410     {
1411         bracketing.iso = shooting_get_iso_real();
1412         bracketing.iso_step = shooting_iso_market_to_real(conf.iso_bracket_value);
1413     }
1414 
1415     // Adjust delta ISO value for shot based on shot number
1416     bracketing.diso += (bracketing.iso_step * bracket_delta[conf.bracket_type][bracketing.shoot_counter&1]);
1417     // Calculate new ISO (higher ISO = brighter image)
1418     short value = bracketing.iso + (bracketing.diso * bracket_steps[conf.bracket_type][bracketing.shoot_counter&1]);
1419     if (value <= 0) value = 50;
1420 
1421     // Inc for next shot
1422     bracketing.shoot_counter++;
1423 
1424     // Apply value for next shot to be taken
1425     shooting_set_iso_real(value, when);
1426 }
1427 
1428 static void shooting_subject_distance_bracketing(int when)
1429 {
1430     // first shot? (actually this is called just after the first shot has been taken)
1431     if (bracketing.shoot_counter == 0)
1432     {
1433         bracketing.subj_dist = shooting_get_subject_distance();
1434         bracketing.subj_dist_step = conf.subj_dist_bracket_value;
1435     }
1436 
1437     // Adjust delta SD value for shot based on shot number
1438     bracketing.dsubj_dist += (bracketing.subj_dist_step * bracket_delta[conf.bracket_type][bracketing.shoot_counter&1]);
1439     // Calculate new SD
1440     int value = bracketing.subj_dist + (bracketing.dsubj_dist * bracket_steps[conf.bracket_type][bracketing.shoot_counter&1]);
1441     if (value < CAMERA_MIN_DIST) value = CAMERA_MIN_DIST;
1442     else if (value > CAMERA_MAX_DIST) value = CAMERA_MAX_DIST;
1443 
1444     // Inc for next shot
1445     bracketing.shoot_counter++;
1446 
1447     // Apply value for next shot to be taken
1448     shooting_set_focus(value, when);
1449 }
1450 
1451 void bracketing_reset()
1452 {
1453     memset(&bracketing,0,sizeof(bracketing));
1454     memset(&photo_param_put_off,0,sizeof(photo_param_put_off));
1455     photo_param_put_off.tv96=PHOTO_PARAM_TV_NONE;
1456 }
1457 
1458 void bracketing_step(int when)
1459 {
1460     if (is_tv_bracketing_enabled)       shooting_tv_bracketing(when);
1461     else if (is_av_bracketing_enabled)  shooting_av_bracketing(when);
1462     else if (is_iso_bracketing_enabled) shooting_iso_bracketing(when);
1463     else if (is_sd_bracketing_enabled)  shooting_subject_distance_bracketing(when);           
1464 }
1465 
1466 void shooting_bracketing(void)
1467 {  
1468     if (shooting_get_drive_mode()!=0)  
1469     {
1470         if (camera_info.state.mode_shooting!=MODE_STITCH && camera_info.state.mode_shooting!=MODE_BEST_IMAGE)
1471         {
1472             if (camera_info.state.state_shooting_progress != SHOOTING_PROGRESS_PROCESSING)
1473                 bracketing_reset() ;
1474             bracketing_step(SET_NOW) ;
1475         }
1476     }
1477 }
1478 
1479 //-------------------------------------------------------------------
1480 // Other state test functions. 
1481 // Not strictly shooting related; but somehow ended up here.
1482 
1483 // Check if any shutter, aperture or ISO overrides active, for cameras
1484 // that require capt seq hack to override canon "quick press" behavior
1485 // caller must save regs
1486 int captseq_hack_override_active()
1487 {
1488     if (camera_info.state.state_kbd_script_run) {
1489         if ( photo_param_put_off.tv96 != PHOTO_PARAM_TV_NONE || photo_param_put_off.sv96 )
1490             return 1;
1491 #if CAM_HAS_IRIS_DIAPHRAGM
1492         if(photo_param_put_off.av96)
1493             return 1;
1494 #endif
1495     }
1496     if(conf.override_disable==1)
1497         return 0;
1498     if(is_iso_override_enabled)
1499         return 1;
1500     if(is_tv_override_enabled)
1501         return 1;
1502 #if CAM_HAS_IRIS_DIAPHRAGM
1503     if(is_av_override_enabled)
1504         return 1;
1505 #endif
1506     return 0;
1507 }
1508 
1509 #ifdef CAM_SIMPLE_MOVIE_STATUS
1510 extern int simple_movie_status;
1511 #else
1512 extern int movie_status;
1513 #endif
1514 
1515 void set_movie_status(int status)
1516 {
1517 #ifndef CAM_SIMPLE_MOVIE_STATUS
1518     switch(status)
1519     {
1520         case 1:
1521             if (movie_status == VIDEO_RECORD_IN_PROGRESS)
1522             {
1523                 movie_status = VIDEO_RECORD_STOPPED;
1524             }
1525             break;
1526         case 2:
1527             if (movie_status == VIDEO_RECORD_STOPPED)
1528             {
1529                 movie_status = VIDEO_RECORD_IN_PROGRESS;
1530             }
1531             break;
1532         case 3:
1533             if (movie_status == VIDEO_RECORD_STOPPED || movie_status == VIDEO_RECORD_IN_PROGRESS)
1534             {
1535                 movie_status = VIDEO_RECORD_STOPPING;
1536             }
1537             break;
1538     }
1539 #else // CAM_SIMPLE_MOVIE_STATUS
1540       // no known way to control the recording process
1541 #endif
1542 }
1543 
1544 int get_movie_status()
1545 {
1546 #ifndef CAM_SIMPLE_MOVIE_STATUS
1547     return movie_status;
1548 #else
1549     // firmware movie_status interpreted as: zero - not recording, nonzero - recording
1550     return simple_movie_status?VIDEO_RECORD_IN_PROGRESS:VIDEO_RECORD_STOPPED;
1551 #endif
1552 }
1553 
1554 // Return whether video is being recorded
1555 int is_video_recording()
1556 {
1557 #ifndef CAM_SIMPLE_MOVIE_STATUS
1558 #if defined(CAM_HAS_MOVIE_DIGEST_MODE)
1559     // If camera has movie digest mode then movie_status values are different than previous models
1560     // 'movie_status' values
1561     //      0 - after startup
1562     //      1 - movie recording stopped
1563     //      4 - movie recording in progress, or in 'movie digest' scene mode
1564     //      5 - movie recording stopping
1565     //      6 - in video mode, not recording
1566     return ((movie_status == VIDEO_RECORD_IN_PROGRESS) && (camera_info.state.mode_shooting != MODE_VIDEO_MOVIE_DIGEST));
1567 #else
1568     // 'movie_status' values
1569     //      0 - after startup
1570     //      1 - movie recording stopped
1571     //      4 - movie recording in progress
1572     //      5 - movie recording stopping
1573     return (movie_status > 1);
1574 #endif
1575 #else // CAM_SIMPLE_MOVIE_STATUS
1576     return (simple_movie_status!=0);
1577 #endif
1578 }
1579 
1580 // Converted from MODE_IS_VIDEO macro (philmoz July 2011)
1581 int mode_is_video(int m)
1582 {
1583     m = m & MODE_SHOOTING_MASK;
1584     return (m==MODE_VIDEO_STD ||
1585             m==MODE_VIDEO_SPEED ||
1586             m==MODE_VIDEO_COMPACT ||
1587             m==MODE_VIDEO_MY_COLORS ||
1588             m==MODE_VIDEO_COLOR_ACCENT ||
1589             m==MODE_VIDEO_COLOR_SWAP ||
1590             m==MODE_VIDEO_MINIATURE ||
1591             m==MODE_VIDEO_TIME_LAPSE ||
1592             m==MODE_VIDEO_IFRAME_MOVIE ||
1593             m==MODE_VIDEO_M ||
1594             m==MODE_VIDEO_STAR_TIME_LAPSE ||
1595             m==MODE_VIDEO_SHORT_CLIP
1596         // not clear if this should be considered a video mode ?
1597         //  m==MODE_VIDEO_MOVIE_DIGEST
1598         );
1599 }
1600 
1601 // override in platform/<cam>/main.c if playrec_mode is not found or different
1602 int __attribute__((weak)) rec_mode_active(void)
1603 {
1604     return (playrec_mode==2 || playrec_mode==4 || playrec_mode==5);
1605 }
1606 
1607 int mode_get(void)
1608 {
1609     int mode, t=0xFF;
1610 
1611     mode = (rec_mode_active())?MODE_REC:MODE_PLAY;
1612 
1613 #ifdef CAM_SWIVEL_SCREEN
1614     extern int screen_opened(void);     // 0 not open, non-zero open
1615     extern int screen_rotated(void);    // 0 not rotated, non-zero rotated
1616 
1617     mode |= (screen_opened())?MODE_SCREEN_OPENED:0;
1618     mode |= (screen_rotated())?MODE_SCREEN_ROTATED:0;
1619 #endif
1620 
1621 #ifdef CAM_SUPPORT_BITMAP_RES_CHANGE
1622     extern void update_screen_dimensions();
1623     update_screen_dimensions();
1624 #endif
1625 
1626     get_property_case(PROPCASE_SHOOTING_MODE, &t, 4);
1627 #ifdef CAM_MASK_VID_REC_ACTIVE
1628     mode |= shooting_mode_canon2chdk(t & (~CAM_MASK_VID_REC_ACTIVE));
1629 #else
1630     mode |= shooting_mode_canon2chdk(t);
1631 #endif
1632 
1633     // Set camera state variables from mode info
1634     camera_info.state.mode = mode;
1635     camera_info.state.mode_shooting = mode & MODE_SHOOTING_MASK;
1636     camera_info.state.mode_video = mode_is_video(mode);
1637     camera_info.state.mode_rec = ((mode & MODE_MASK) == MODE_REC);
1638     camera_info.state.mode_rec_or_review = camera_info.state.mode_rec && (recreview_hold==0 || conf.show_osd_in_review);
1639     camera_info.state.mode_play = ((mode & MODE_MASK) == MODE_PLAY);
1640     camera_info.state.mode_photo = (camera_info.state.mode_play || !(camera_info.state.mode_video || (mode&MODE_SHOOTING_MASK)==MODE_STITCH));
1641 
1642 #ifdef CAM_CLEAN_OVERLAY
1643     extern void handle_clean_overlay();
1644     handle_clean_overlay();
1645 #endif
1646 
1647     return (mode);
1648 }
1649 
1650 //-------------------------------------------------------------------
1651 // Exposure control in video
1652 
1653 #if CAM_EV_IN_VIDEO
1654 
1655 int ev_video_avail;
1656 int ev_video;
1657 int save_tv_video;
1658 int tv_min_video;
1659 
1660 int get_ev_video_avail(void)
1661 {
1662     return ev_video_avail;
1663 }
1664 
1665 int get_ev_video(void)
1666 {
1667     return ev_video;
1668 }
1669 
1670 void set_ev_video_avail(int x)
1671 {
1672     extern void ExpCtrlTool_StartContiAE(int, int);
1673     extern void ExpCtrlTool_StopContiAE(int, int);
1674 
1675     if (ev_video_avail==x) return;
1676     ev_video_avail=x;
1677     if (x)
1678     {
1679         ev_video=0;
1680         ExpCtrlTool_StopContiAE(0,0);
1681         get_property_case(PROPCASE_TV,&save_tv_video,2);
1682         if (camera_info.state.mode_shooting==MODE_VIDEO_SPEED)
1683             tv_min_video=577;   // 1/60
1684         else
1685             tv_min_video=480;   //1/30
1686     }
1687     else
1688         ExpCtrlTool_StartContiAE(0,0);
1689 }
1690 
1691 void set_ev_video(int x)
1692 {
1693     short ev, tv;
1694     if ((x<-4)||(x>4))
1695         return;
1696 
1697     ev=48*x;
1698     tv=save_tv_video-ev;
1699     if (tv>=tv_min_video)
1700     {
1701         ev_video=x;
1702         extern short SetAE_ShutterSpeed(short* tv);
1703         SetAE_ShutterSpeed(&tv);
1704     }
1705 }
1706 
1707 #endif
1708 
1709 //-------------------------------------------------------------------
1710 // Main exposure override function called from capt_seq_task()
1711 
1712 static void shooting_set_flash_override()
1713 {
1714     int mode, flash, power;
1715     flash = 1;
1716 
1717     // Set flash mode & override
1718     if ((conf.flash_video_override && is_video_recording()) || (!conf.flash_video_override))
1719     {
1720         if (conf.flash_manual_override)
1721         {
1722             mode = 1;   // Manual flash
1723             power = conf.flash_video_override_power;
1724             set_property_case(PROPCASE_FLASH_ADJUST_MODE, &mode, sizeof(mode));
1725             set_property_case(PROPCASE_FLASH_FIRE, &flash, sizeof(flash));
1726             set_property_case(PROPCASE_FLASH_MANUAL_OUTPUT, &power, sizeof(power));
1727         }
1728         else if (conf.flash_enable_exp_comp)
1729         {
1730             mode = 0;   // Auto flash
1731             power = (conf.flash_exp_comp - 9) * 32;
1732             set_property_case(PROPCASE_FLASH_ADJUST_MODE, &mode, sizeof(mode));
1733             set_property_case(PROPCASE_FLASH_FIRE, &flash, sizeof(flash));
1734 #if defined(PROPCASE_FLASH_EXP_COMP)
1735             set_property_case(PROPCASE_FLASH_EXP_COMP, &power, sizeof(power));
1736 #endif
1737 #if defined(PROPCASE_EXT_FLASH_EXP_COMP)
1738             set_property_case(PROPCASE_EXT_FLASH_EXP_COMP, &power, sizeof(power));
1739 #endif
1740         }
1741     }
1742 }
1743 
1744 void shooting_expo_param_override_thumb(void)
1745 {
1746     if (((camera_info.state.state_kbd_script_run) || (usb_remote_active)) && (photo_param_put_off.tv96 != PHOTO_PARAM_TV_NONE))
1747     {
1748         shooting_set_tv96_direct(photo_param_put_off.tv96, SET_NOW);
1749         photo_param_put_off.tv96=PHOTO_PARAM_TV_NONE;
1750     }
1751     else if (is_tv_override_enabled)
1752     {
1753         shooting_set_tv96_direct(shooting_get_tv96_override_value(),SET_NOW);
1754     }
1755 
1756     if (((camera_info.state.state_kbd_script_run) || (usb_remote_active)) && (photo_param_put_off.sv96))
1757     {
1758         shooting_set_sv96(photo_param_put_off.sv96, SET_NOW);
1759         photo_param_put_off.sv96=0;
1760     }
1761     else if (is_iso_override_enabled)
1762         shooting_set_iso_real(shooting_get_iso_override_value(), SET_NOW);
1763     else if (conf.autoiso_enable && shooting_get_flash_mode()/*NOT FOR FLASH AUTO MODE*/ && autoiso_and_bracketing_overrides_are_enabled)
1764         shooting_set_autoiso(shooting_get_iso_mode());
1765 
1766     if (((camera_info.state.state_kbd_script_run) || (usb_remote_active)) && (photo_param_put_off.av96))
1767     {
1768         shooting_set_av96_direct(photo_param_put_off.av96, SET_NOW);
1769         photo_param_put_off.av96=0;
1770     }
1771     else if (is_av_override_enabled)
1772         shooting_set_av96_direct(shooting_get_av96_override_value(), SET_NOW);
1773 
1774     if (((camera_info.state.state_kbd_script_run) || (usb_remote_active)) && (photo_param_put_off.subj_dist))
1775     {
1776         shooting_set_focus(photo_param_put_off.subj_dist, SET_NOW);
1777         photo_param_put_off.subj_dist=0;
1778     }
1779     else if (is_sd_override_enabled)
1780         shooting_set_focus(shooting_get_subject_distance_override_value(), SET_NOW);
1781 
1782 #if CAM_HAS_ND_FILTER
1783     if ((camera_info.state.state_kbd_script_run) && (photo_param_put_off.nd_filter))
1784     {
1785         shooting_set_nd_filter_state(photo_param_put_off.nd_filter, SET_NOW);
1786         photo_param_put_off.nd_filter=0;
1787     }
1788     else if (conf.nd_filter_state && !(conf.override_disable==1))
1789         shooting_set_nd_filter_state(conf.nd_filter_state, SET_NOW);
1790 #endif
1791 
1792 #if CAM_REAR_CURTAIN
1793     shooting_set_flash_sync_curtain(conf.flash_sync_curtain);
1794 #endif
1795 
1796     // Set flash mode & override
1797     shooting_set_flash_override();
1798 
1799 #if CAM_QUALITY_OVERRIDE
1800     // this doesn't really need to be set in the override hook.
1801     // should only need to be set once if the users doesn't change back, but doing it here ensures it is set
1802     shooting_set_image_quality(conf.fast_image_quality);
1803 #endif
1804 
1805     libscriptapi->shoot_hook(SCRIPT_SHOOT_HOOK_PRESHOOT);
1806 }
1807 
1808 // Override ISO settings only
1809 // need to do this before exposure calc for ISO, as well as after on some cameras
1810 void shooting_expo_iso_override_thumb(void)
1811 {
1812     if ((camera_info.state.state_kbd_script_run) && (photo_param_put_off.sv96))
1813     {
1814         shooting_set_sv96(photo_param_put_off.sv96, SET_NOW);
1815         // photo_param_put_off.sv96 is not reset here, it will be reset in next call to shooting_expo_param_override
1816     }
1817     else if (is_iso_override_enabled)
1818         shooting_set_iso_real(shooting_get_iso_override_value(), SET_NOW);
1819     else if (conf.autoiso_enable && shooting_get_flash_mode()/*NOT FOR FLASH AUTO MODE*/ && autoiso_and_bracketing_overrides_are_enabled)
1820         shooting_set_autoiso(shooting_get_iso_mode());
1821 
1822 #if defined(CAM_HAS_ND_FILTER) && defined(CAM_HAS_NATIVE_ND_FILTER)
1823     if ((camera_info.state.state_kbd_script_run) && (photo_param_put_off.nd_filter))
1824     {
1825         shooting_set_nd_filter_state(photo_param_put_off.nd_filter, SET_NOW);
1826         //photo_param_put_off.nd_filter=0;
1827     }
1828     else if (conf.nd_filter_state && !(conf.override_disable==1))
1829         shooting_set_nd_filter_state(conf.nd_filter_state, SET_NOW);
1830 #endif
1831 
1832     // Set flash mode & override
1833     shooting_set_flash_override();
1834 }
1835 
1836 /*
1837 switch camera between playback (0) and record(1)
1838 uses switch_mode_usb if a usb connection is present
1839 */
1840 void shooting_set_playrec_mode(int mode)
1841 {
1842     // use PTP compatible switch if usb bit power detected, and USB remote not configured to use USB
1843     // TODO should just check the USB bit seen by canon firmware (after masking)
1844     if (get_usb_bit() && !(conf.remote_enable == 1 && conf.remote_input_channel == REMOTE_INPUT_USB))
1845     {
1846         switch_mode_usb(mode);
1847         return;
1848     }
1849     if(mode)
1850         levent_set_record();
1851     else
1852         levent_set_play();
1853 }

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