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

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