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

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

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