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

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