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

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