root/core/shooting.c

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

DEFINITIONS

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

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