source: branches/release-1_0/platform/generic/shooting.c @ 1812

Revision 1812, 48.4 KB checked in by philmoz, 13 months ago (diff)

Fix subject distance override problems when setting infinity focus.
http://chdk.setepontos.com/index.php?topic=7993.msg84366#msg84366

  • Property svn:eol-style set to native
Line 
1#include "lolevel.h"
2#include "platform.h"
3#include "core.h"
4#include "keyboard.h"
5#include "math.h"
6#include "stdlib.h"
7#include "conf.h"
8#include "histogram.h"
9
10#define USE_FILE_COUNTER_FOR_EXP_COUNTING
11
12#define SS_SIZE (sizeof(shutter_speeds_table)/sizeof(shutter_speeds_table[0]))
13#define SSID_MIN (shutter_speeds_table[0].id)
14#define SSID_MAX (shutter_speeds_table[SS_SIZE-1].id)
15
16#define AS_SIZE (sizeof(aperture_sizes_table)/sizeof(aperture_sizes_table[0]))
17#define ASID_MIN (aperture_sizes_table[0].id)
18#define ASID_MAX (aperture_sizes_table[AS_SIZE-1].id)
19
20#define AV96_MIN (aperture_sizes_table[0].prop_id)
21#define AV96_MAX (aperture_sizes_table[AS_SIZE-1].prop_id)
22
23#define ISO_SIZE (sizeof(iso_table)/sizeof(iso_table[0]))
24#define ISO_MIN (iso_table[0].id)
25#define ISO_MAX (iso_table[ISO_SIZE-1].id)
26#define ISO_MIN_VALUE (iso_table[1-iso_table[0].id].prop_id)
27
28static const unsigned MODESCNT=(sizeof(modemap)/sizeof(modemap[0]));
29
30/*define PROPCASE_CONT_MODE_SHOOT_COUNT                 218*/
31static short iso_market_base=0;
32//static short iso_base=0;
33static short sv96_base=0;
34static short svm96_base=0;
35static short sv96_base_tmp=0;
36//static short override_iso=0;
37static const double sqrt2=1.4142135623731;//square root from 2
38static const double log_2=0.6931471805599;//natural logarithm of 2
39static const double k=12.5;//K is the reflected-light meter calibration constant
40static const short koef[] = {0, 1,10,100,1000};
41static const float shutter_koef[] = {0, 0.00001, 0.0001,0.001,0.01,0.1,1,10,100,1000};
42static const char * expo_shift[] = { "Off", "1/3Ev","2/3Ev", "1Ev", "1 1/3Ev", "1 2/3Ev", "2Ev", "2 1/3Ev", "2 2/3Ev", "3Ev", "3 1/3Ev", "3 2/3Ev", "4Ev"};
43const char* tv_override[]={
44#ifdef CAM_EXT_TV_RANGE
45    // add very long time exposures as approximately powers of 2, adding 15 exposures
46    "2048","1625","1290","1024","812","645","512","406","322","256","203","161","128","101","80",
47#endif
48    "64","50.8", "40.3", "32", "25.4","20","16", "12.7", "10","8", "6.3","5","4","3.2", "2.5","2", "1.6", "1.3", "1", "0.8", "0.6", "0.5", "0.4", "0.3", "1/4", "1/5", "1/6", "1/8", "1/10", "1/13", "1/15", "1/20", "1/25", "1/30", "1/40", "1/50", "1/60", "1/80", "1/100", "1/125", "1/160", "1/200", "1/250", "1/320", "1/400", "1/500", "1/640","1/800", "1/1000", "1/1250", "1/1600","1/2000","1/2500","1/3200","1/4000", "1/5000", "1/6400", "1/8000", "1/10000", "1/12500", "1/16000", "1/20000", "1/25000", "1/32000", "1/40000", "1/50000", "1/64000","1/80000", "1/100k"};
49const int tv_override_amount = sizeof(tv_override)/sizeof(tv_override[0]);
50
51// offset index into tv_override to translate into 1/3 stops in APEX
52#if CAM_EXT_TV_RANGE
53const int tv_override_zero_shift = 18+15;
54#else
55const int tv_override_zero_shift = 18;
56#endif
57static const char * expo_type[] = { "+/-", "-","+"};
58
59static PHOTO_PARAM photo_param_put_off;
60
61static EXPO_BRACKETING_VALUES bracketing;
62
63static short *min_av96_zoom_point_tbl = NULL;
64
65DOF_TYPE dof_values;
66
67//***********************
68/*
69
70static char debug_str[60];
71
72void debug_char(char * deb_str)
73{
74 short debug_str_length=strlen(debug_str);
75 if     (debug_str_length>50) strcpy(debug_str, "");
76 sprintf(debug_str+debug_str_length, "%s", deb_str);
77}
78
79void debug_init()
80{
81 strcpy(debug_str, "");
82}
83
84
85void debug_int(int deb_str)
86{
87 short debug_str_length=strlen(debug_str);
88 if     (debug_str_length>50) strcpy(debug_str, "");
89 sprintf(debug_str+debug_str_length, "%d", deb_str);
90}
91
92
93void debug(char * deb_str, int deb_int)
94{
95 short debug_str_length=strlen(debug_str);
96 if     (debug_str_length>50) strcpy(debug_str, "");
97 sprintf(debug_str+debug_str_length, "%s", deb_str);
98 sprintf(debug_str+strlen(debug_str), "%d", deb_int);
99}
100
101char * get_debug()
102{
103 return debug_str;
104}
105*/
106//***********************
107
108int shooting_get_user_tv_id()
109{
110#if CAM_HAS_USER_TV_MODES
111    short tvv;
112    long i;
113    _GetPropertyCase(PROPCASE_USER_TV, &tvv, sizeof(tvv));
114    for (i=0;i<SS_SIZE;i++){
115        if (shutter_speeds_table[i].prop_id == tvv)
116            return shutter_speeds_table[i].id;
117    }
118#endif       
119    return 0;
120}
121
122short shooting_get_is_mode()
123{
124    short ism = 0;
125    _GetPropertyCase(PROPCASE_IS_MODE, &ism, sizeof(ism));
126    return ism;
127}
128
129
130const ShutterSpeed *shooting_get_tv_line()
131{
132    short tvv;
133    long i;
134    _GetPropertyCase(PROPCASE_USER_TV, &tvv, sizeof(tvv));
135    for (i=0;i<SS_SIZE;i++){
136           if (shutter_speeds_table[i].prop_id == tvv)
137            return &shutter_speeds_table[i];
138    }
139    return 0;
140}
141
142void shooting_set_user_tv_by_id(int v)
143{
144#if CAM_HAS_USER_TV_MODES
145if ((mode_get()&MODE_MASK) != MODE_PLAY){
146    long i;
147//    if ((v<SSID_MIN) || (v>SSID_MAX))
148//      return;
149 for (i=0;i<SS_SIZE;i++){
150        if (shutter_speeds_table[i].id == v){
151            short vv = shutter_speeds_table[i].prop_id;
152            _SetPropertyCase(PROPCASE_USER_TV, &vv, sizeof(vv));
153            //_SetPropertyCase(PROPCASE_TV, &vv, sizeof(vv));
154            return;
155         }
156 }
157}
158#endif
159}
160void shooting_set_prop(int id, int v)
161{
162   short vv = v;
163   _SetPropertyCase(id, &vv, sizeof(vv));
164   return;
165}
166
167int shooting_get_prop(int id)
168{
169    short vv;
170    _GetPropertyCase(id, &vv, sizeof(vv));
171    return vv;
172}
173
174short shooting_get_canon_iso_mode()
175{
176    short isom;
177    _GetPropertyCase(PROPCASE_ISO_MODE, &isom, sizeof(isom));
178     return isom;
179}
180
181
182int shooting_get_iso_mode()
183{
184    short isov;
185    long i;
186    _GetPropertyCase(PROPCASE_ISO_MODE, &isov, sizeof(isov));
187    for (i=0;i<ISO_SIZE;i++){
188                        if (iso_table[i].prop_id == isov)
189                         return iso_table[i].id;
190    }
191    return 0;
192}
193
194void shooting_set_user_tv_by_id_rel(int v)
195{
196#if CAM_HAS_USER_TV_MODES
197if ((mode_get()&MODE_MASK) != MODE_PLAY){
198    int cv = shooting_get_user_tv_id();
199    shooting_set_user_tv_by_id(cv+v);
200}
201#endif   
202}
203
204int shooting_get_user_av_id()
205{
206#if CAM_HAS_IRIS_DIAPHRAGM
207    short avv;
208    long i;
209    _GetPropertyCase(PROPCASE_USER_AV, &avv, sizeof(avv));
210    for (i=0;i<AS_SIZE;i++){
211                        if (aperture_sizes_table[i].prop_id == avv)
212                         return aperture_sizes_table[i].id;
213    }
214#endif   
215    return 0;
216}
217
218short shooting_get_real_aperture() {
219//#if !CAM_HAS_IRIS_DIAPHRAGM
220//    return shooting_get_min_real_aperture();
221//#else
222//I hope that GetCurrentAvValue is correct for ixus70_sd1000 ixus700_sd500 now
223    return shooting_get_aperture_from_av96(_GetCurrentAvValue());
224//#endif
225}
226
227short shooting_get_aperture_from_av96(short av96) {
228        if (av96) return (short)((pow(sqrt2, ((double)av96)/96.0))*100.0);
229        else return -1;
230}
231
232short shooting_get_min_real_aperture() {
233         short av96;
234        _GetPropertyCase(PROPCASE_MIN_AV, &av96, sizeof(av96));
235        if (av96) return shooting_get_aperture_from_av96(av96);
236        else return shooting_get_real_aperture();
237}
238
239short shooting_get_iso_from_sv96(short sv96)
240{
241    return (short)(pow(2, (((double) sv96+168.0)/96.0)));
242}
243
244short shooting_get_iso_real()
245{
246    short sv;
247    _GetPropertyCase(PROPCASE_SV, &sv, sizeof(sv));
248    if (sv == 0) {
249     return 0;
250    }
251    return shooting_get_iso_from_sv96(sv);
252}
253
254short shooting_get_svm96()
255{
256    short  sv;
257     _GetPropertyCase(PROPCASE_SV_MARKET, &sv, sizeof(sv));
258     return sv;
259}
260
261short shooting_get_sv96()
262{
263    short  sv;
264     _GetPropertyCase(PROPCASE_SV, &sv, sizeof(sv));
265     return sv;
266}
267
268short shooting_get_base_sv96()
269{
270   short dsv,sv;
271   if (shooting_get_canon_iso_mode()<50) {     
272       _GetPropertyCase(PROPCASE_DELTA_SV, &dsv, sizeof(dsv));
273       _GetPropertyCase(PROPCASE_SV, &sv, sizeof(sv));
274       sv96_base=(sv-dsv);
275   }
276   return sv96_base;
277}
278
279short shooting_get_sv96_from_iso(short iso)
280{
281  if  (iso>0)
282    return (short)(log(pow(2.0,(-7.0/4.0))*(double)(iso))*96.0/(log_2));
283  return 0; 
284}
285
286short shooting_get_svm96_from_iso(short iso)
287{
288   if  (iso>0) return (short)(log((double)(iso)*32.0/100.0)*96.0/(log_2));
289   return 0;   
290
291
292short shooting_get_iso_market_from_svm96(short svm96)
293{
294        if (svm96>0 )
295                return (short)((double)pow(2, (((double)svm96)/96.0))*100.0/32.0);
296        return 0;       
297}
298
299short shooting_get_iso_market_base()
300{
301//if ((iso_market_base==0) && (shooting_get_iso_mode() <= 0)) {
302//       iso_market_base=(short)shooting_get_iso_market_from_svm96(shooting_get_svm96());
303// }
304 //return iso_market_base;
305  if (iso_market_base==0) {
306      if (ISO_MIN_VALUE==50) iso_market_base=50;
307#if defined(CAMERA_sx40hs)
308      else iso_market_base=200;
309#else
310      else iso_market_base=100;
311#endif
312     }
313  return iso_market_base;       
314}
315
316short shooting_get_svm96_base()
317{
318        if (svm96_base==0) svm96_base=shooting_get_svm96_from_iso(shooting_get_iso_market_base());
319        return svm96_base;
320}
321
322short shooting_get_iso_base()
323{
324        sv96_base=shooting_get_base_sv96();
325        if (sv96_base!=0) return shooting_get_iso_from_sv96(sv96_base);
326        else return 0;
327}
328
329// AUTOISO:EXIF
330short shooting_get_iso_market()
331{
332    //double koef;
333    short iso_mode=shooting_get_canon_iso_mode();
334    if ((iso_mode < 50) || (conf.iso_override_koef && conf.iso_override_value) || (conf.iso_bracket_koef && conf.iso_bracket_value))
335     {
336      short iso_b=shooting_get_iso_base();
337      if (iso_b) return (short)((shooting_get_iso_market_base()*shooting_get_iso_real())/iso_b);
338     }
339    return iso_mode;
340}
341
342void shooting_set_sv96(short sv96, short is_now){
343  short dsv96=0, iso_mode=shooting_get_canon_iso_mode();       
344if ((mode_get()&MODE_MASK) != MODE_PLAY){
345  if (is_now)  {
346    if (iso_mode<50) dsv96 =sv96-shooting_get_base_sv96();
347    else if (sv96_base) dsv96=sv96-sv96_base;
348        else if (sv96_base_tmp) dsv96=sv96-sv96_base_tmp; 
349        else
350         {
351         sv96_base_tmp= (short)((shooting_get_svm96_base()*shooting_get_sv96())/shooting_get_svm96());   
352         dsv96=sv96-sv96_base_tmp;
353         }       
354    while ((shooting_is_flash_ready()!=1) || (focus_busy));
355    short svm96_base =shooting_get_svm96_base();
356    if (iso_mode>=50) shooting_set_iso_mode(0);
357    _SetPropertyCase(PROPCASE_SV_MARKET, &svm96_base, sizeof(svm96_base));
358    _SetPropertyCase(PROPCASE_SV, &sv96, sizeof(sv96));
359    _SetPropertyCase(PROPCASE_DELTA_SV, &dsv96, sizeof(dsv96));
360  }
361  else   photo_param_put_off.sv96=sv96;
362}
363}
364
365/*void shooting_set_iso_real_delta_from_base(int diso)
366{
367 return;
368 int iso=(int)((short)shooting_get_iso_base()+diso);
369 
370 if (iso>0) {
371    shooting_set_sv96((short)(shooting_get_sv96_from_iso(iso)));
372  }
373}*/
374
375void shooting_set_iso_real(short iso, short is_now)
376{
377    if ((mode_get()&MODE_MASK) != MODE_PLAY)
378    {
379        if (iso>0)
380        {
381#ifdef CAM_ISO_LIMIT_IN_HQ_BURST
382            // Limit max ISO in HQ burst mode
383            if ((mode_get() & MODE_SHOOTING_MASK) == MODE_SCN_HIGHSPEED_BURST)
384                if (iso > CAM_ISO_LIMIT_IN_HQ_BURST) iso = CAM_ISO_LIMIT_IN_HQ_BURST;
385#endif
386            shooting_set_sv96(shooting_get_sv96_from_iso(iso), is_now);
387        }
388    }
389}
390
391short shooting_get_bv96()
392{
393    short bv = 0;
394    _GetPropertyCase(PROPCASE_BV, &bv, sizeof(bv));
395    return bv;
396}
397
398short shooting_get_canon_overexposure_value()
399{
400    short bv = 0;
401    _GetPropertyCase(PROPCASE_OVEREXPOSURE, &bv, sizeof(bv));
402    return bv;
403}
404
405
406short shooting_get_flash_mode()
407{
408    short fm = 0;
409    _GetPropertyCase(PROPCASE_FLASH_MODE, &fm, sizeof(fm));
410    return fm;
411}
412
413int shooting_get_luminance()// http://en.wikipedia.org/wiki/APEX_system
414{
415    short bv = shooting_get_bv96();
416    int b=(int)(100*k*pow(2.0,((double)(bv-168)/96.0)));
417    return b;
418}
419
420/*void shooting_set_iso_market(int isom)
421{
422        if (isom>0) {
423   shooting_set_sv96(shooting_get_sv96_from_iso((int)(((double)isom*(double)shooting_get_iso_base())/(double)shooting_get_iso_market_base())));
424  }
425}*/
426
427int shooting_get_exif_subject_dist()
428{
429    int sd = 0;
430    _GetPropertyCase(PROPCASE_SUBJECT_DIST1, &sd, sizeof(sd));
431    return sd;
432}
433
434int shooting_get_lens_to_focal_plane_width()
435{
436        return (int)(lens_get_focus_pos()-lens_get_focus_pos_from_lens());
437}
438
439short shooting_get_aperture_from_av96_1e3(short av96) {
440        if (av96) return (short)(pow(sqrt2, (((double)av96)/96.0))*1000.0 + 0.5);
441        else return -1;
442}
443
444int shooting_get_hyperfocal_distance_1e3_f2(int av_1e3, int fl)
445{
446  if ((av_1e3>0) && (fl>0) && (circle_of_confusion>0))
447    return (int)(((((double)fl*fl)/(av_1e3*circle_of_confusion)*1000)+fl)+0.5);
448  else return (-1);
449}
450
451int shooting_get_hyperfocal_distance_1e3_f(int av, int fl)
452{
453  if ((av>0) && (fl>0) && (circle_of_confusion>0))
454    return (int)(((((double)fl*fl)/(av*circle_of_confusion)*100)+fl)+0.5);
455  else return (-1);
456}
457
458int shooting_get_near_limit_f(int s, int av, int fl)
459{
460  if (shooting_is_infinity_distance()) return (-1);
461  int h_1e3 = shooting_get_hyperfocal_distance_1e3_f(av, fl);
462  double m = ((double)(h_1e3 - fl)+500)/1000*s;
463  int v = ((h_1e3 - 2*fl + 1000*s)+500)/1000;
464  if ((m>0) && (v>0)) return (int)((2*m/v+1)/2);
465  else return (-1);
466}
467
468int shooting_get_far_limit_f(int s, int av, int fl)
469{
470  if (shooting_is_infinity_distance()) return (-1);
471  int h_1e3 = shooting_get_hyperfocal_distance_1e3_f(av, fl);
472  double m = ((double)(h_1e3 - fl)+500)/1000*s;
473  int v = ((h_1e3 - 1000*s)+500)/1000;
474  if ((m>0) && (v>0)) return (int)((2*m/v+1)/2);
475  else return (-1);
476}
477
478short shooting_is_infinity_distance()
479{
480  return (shooting_get_lens_to_focal_plane_width() == 0)?1:0;
481}
482
483int shooting_get_canon_subject_distance()
484{
485        if (conf.dof_use_exif_subj_dist)
486          return shooting_get_exif_subject_dist();
487        else
488          return (lens_get_focus_pos_from_lens());
489}
490
491int shooting_get_subject_distance_()
492{
493   if (!conf.dof_subj_dist_as_near_limit) return shooting_get_canon_subject_distance();
494   else return shooting_get_far_limit_f(shooting_get_canon_subject_distance(),
495                                         shooting_get_min_real_aperture(),
496                                         get_focal_length(lens_get_zoom_point()));
497}
498
499void shooting_update_dof_values()
500{
501  int hyp, hyp_1e3, av_1e3, v, m;
502  int dist = shooting_get_subject_distance_();
503  int zoom_point = lens_get_zoom_point();
504  int fl = get_focal_length(zoom_point);
505  short f_focus_ok = shooting_get_focus_ok();
506  short f_hyp_calc = 0, f_dist_calc = 0;
507  short min_av96_zoom_point = min_av96_zoom_point_tbl[zoom_point];
508  short av96 = shooting_get_user_av96();
509  short curr_av96 = _GetCurrentAvValue();
510  short prop_av96 = shooting_get_av96();
511  short min_av96;
512
513  if (!min_av96_zoom_point_tbl) {
514    min_av96_zoom_point_tbl = (short *) malloc(zoom_points * sizeof(short));
515    if (min_av96_zoom_point_tbl) {
516      memset(min_av96_zoom_point_tbl, 0, zoom_points * sizeof(short));
517      min_av96_zoom_point = 0;
518    }
519  } else min_av96_zoom_point = min_av96_zoom_point_tbl[zoom_point];
520
521  if (min_av96_zoom_point==0 && shooting_in_progress()) {
522    _GetPropertyCase(PROPCASE_MIN_AV, &min_av96, sizeof(min_av96));
523    min_av96_zoom_point_tbl[zoom_point] = min_av96;
524  }
525
526  m = mode_get()&MODE_SHOOTING_MASK;
527  if ((m==MODE_M || m==MODE_AV) && (av96>0) && !f_focus_ok) {
528    if (av96 < min_av96_zoom_point) av96 = min_av96_zoom_point;
529  }
530  else av96 = (abs(curr_av96-prop_av96)<2)?prop_av96:curr_av96;
531
532  av_1e3 = shooting_get_aperture_from_av96_1e3(av96);
533  hyp_1e3 = dof_values.hyperfocal_distance_1e3;
534  hyp = dof_values.hyperfocal_distance;
535 
536  if (dof_values.aperture_value!=av_1e3 || dof_values.focal_length!=fl || (hyp_1e3<0)) {
537    //calc new hyperfocal distance and min stack distance
538    f_hyp_calc = 1;
539    hyp_1e3 = -1;
540    hyp = -1;
541    dof_values.aperture_value = av_1e3;
542    dof_values.focal_length = fl;
543    hyp_1e3 = shooting_get_hyperfocal_distance_1e3_f2(av_1e3, fl);
544    if (hyp_1e3>0) {
545      hyp = (hyp_1e3+500)/1000;
546      dof_values.min_stack_distance = MAX_DIST;
547      v = ((hyp_1e3 - fl)/250 + 2 + 1)/2;
548      if (v>0) {
549        int m = ((fl*((fl - hyp_1e3)/1000 - 1)/500)/v + 1)/2;
550        int m2 = (int)((((double)hyp*(2*fl - hyp_1e3)/1000))/v + 0.5);
551        dof_values.min_stack_distance = sqrt(m*m - m2) - m;
552      } 
553    }
554  }
555
556  if ((dof_values.subject_distance!=dist || (dof_values.hyperfocal_distance_1e3!=hyp_1e3)) && (hyp_1e3>0)) {
557    //calc new NEAR, FAR, DOF values
558    f_dist_calc = 1;
559    dof_values.subject_distance = dist;
560    dof_values.near_limit = -1;
561    dof_values.far_limit = -1;
562    dof_values.depth_of_field = -1;
563    if ((av_1e3>0) && (fl>0) && (dist>0) && (shooting_is_infinity_distance()==0) && (hyp_1e3>0)) {
564      double m = ((double)(hyp_1e3 - fl)/1000 * dist) + 0.5;
565      if (conf.dof_subj_dist_as_near_limit) {
566         dof_values.near_limit = dist;
567      } else {
568        int v = ((((hyp_1e3 - 2*fl + 1000*dist)/500) + 1)/2);
569            if (v>0) dof_values.near_limit = (int)(m/v);
570          }
571      int v = ((((hyp_1e3 - 1000*dist)/500) + 1)/2);
572      if (v>0) dof_values.far_limit = (int)(m/v);
573      if ((dof_values.near_limit>0) && (dof_values.far_limit>0)) {
574        dof_values.depth_of_field = dof_values.far_limit - dof_values.near_limit;
575      }
576    }
577  }
578  dof_values.hyperfocal_distance_1e3 = hyp_1e3;
579  dof_values.hyperfocal_distance = hyp;
580  f_focus_ok = (f_focus_ok && shooting_get_focus_ok());
581  dof_values.hyperfocal_valid = (f_focus_ok || (dof_values.hyperfocal_valid && !f_hyp_calc));
582  dof_values.distance_valid = (f_focus_ok || (dof_values.distance_valid && !f_dist_calc)||(dof_values.hyperfocal_valid && shooting_get_focus_mode()));
583  return;
584}
585
586int shooting_get_subject_distance()
587{
588  shooting_update_dof_values();
589  return dof_values.subject_distance;
590}
591
592int shooting_get_hyperfocal_distance()
593{
594  shooting_update_dof_values();
595  return dof_values.hyperfocal_distance;
596}
597
598int shooting_get_near_limit_of_acceptable_sharpness()
599{
600  shooting_update_dof_values();
601  return dof_values.near_limit;
602}
603
604int shooting_get_far_limit_of_acceptable_sharpness()
605{
606  shooting_update_dof_values();
607  return dof_values.far_limit;
608}
609
610int shooting_get_depth_of_field()
611{
612  shooting_update_dof_values();
613  return dof_values.depth_of_field;
614}
615
616int shooting_get_min_stack_distance()
617{
618  shooting_update_dof_values();
619  return dof_values.min_stack_distance;
620}
621
622short shooting_get_tv96_from_shutter_speed(float t)
623{
624  if (t>0) return (short) (96.0*log(1.0/t)/log_2); 
625  else return (-10000);
626}
627
628float shooting_get_shutter_speed_from_tv96(short tv)
629{
630  return  pow(2,(float)((-1)*tv)/96.0 ); 
631}
632
633short shooting_get_tv96()
634{
635    short tv96;
636    _GetPropertyCase(PROPCASE_TV, &tv96, sizeof(tv96));
637    return tv96;
638}
639
640short shooting_get_user_tv96()
641{
642#if CAM_HAS_USER_TV_MODES
643    short tv;
644    _GetPropertyCase(PROPCASE_USER_TV, &tv, sizeof(tv));
645    return tv;
646#else       
647    return 0;
648#endif   
649}
650
651void shooting_set_user_tv96(short v)
652{
653#if CAM_HAS_USER_TV_MODES
654if ((mode_get()&MODE_MASK) != MODE_PLAY){
655long i;
656//    if ((v<SSID_MIN) || (v>SSID_MAX))
657//      return;
658 for (i=0;i<SS_SIZE;i++){
659        if (shutter_speeds_table[i].prop_id == v){
660                _SetPropertyCase(PROPCASE_USER_TV, &v, sizeof(v));
661                //_SetPropertyCase(PROPCASE_TV, &v, sizeof(v));
662            return;
663          }
664  }
665}
666#endif 
667}
668
669void shooting_set_tv96(short v, short is_now)
670{
671 long i;
672//    if ((v<SSID_MIN) || (v>SSID_MAX))
673//      return;
674if ((mode_get()&MODE_MASK) != MODE_PLAY){
675 for (i=0;i<SS_SIZE;i++){
676        if (shutter_speeds_table[i].prop_id == v){
677       shooting_set_tv96_direct(v, is_now);
678       return;
679     }
680  }
681}
682}
683
684void shooting_set_tv96_direct(short v, short is_now)
685{
686if ((mode_get()&MODE_MASK) != MODE_PLAY){
687    if(is_now) {
688        _SetPropertyCase(PROPCASE_TV, &v, sizeof(v));
689#ifdef PROPCASE_TV2
690       _SetPropertyCase(PROPCASE_TV2, &v, sizeof(v));   // Save override to property that will update JPEG header & Canon OSD
691#endif
692   }
693   else photo_param_put_off.tv96=v;
694}
695}
696
697void shooting_set_shutter_speed(float t, short ev_correction, short is_now)
698{
699        if (t>0) shooting_set_tv96_direct( ((short) 96.0*log(1/t)/log_2) + ev_correction, is_now);
700}
701
702
703void shooting_set_shutter_speed_ubasic(int t, short is_now)
704{
705        if ((mode_get()&MODE_MASK) != MODE_PLAY){
706                if (t>0) shooting_set_tv96_direct((short) (96.0*log(100000.0/(double)t)/log_2), is_now); 
707        }
708}
709
710short shooting_get_av96()
711{
712    short av;
713    _GetPropertyCase(PROPCASE_AV, &av, sizeof(av));
714    return av;
715}
716
717short shooting_get_aperture_sizes_table_size()
718{
719    return AS_SIZE;
720}
721
722
723short shooting_get_aperture_sizes_table_prop_id(short i)
724{
725    return aperture_sizes_table[i].prop_id;
726}
727
728short shooting_get_max_aperture_sizes_table_prop_id()
729{
730    return AV96_MAX;
731}
732
733
734
735short shooting_get_av_prop_id()
736{
737    short av;
738    _GetPropertyCase(PROPCASE_AV, &av, sizeof(av));
739    return av;
740}
741
742
743
744short shooting_get_user_av96()
745{
746#if CAM_HAS_IRIS_DIAPHRAGM
747    short av;
748    _GetPropertyCase(PROPCASE_USER_AV, &av, sizeof(av));
749    return av;
750#else     
751    return 0;
752#endif
753}
754
755
756void shooting_set_av96(short v, short is_now)
757{
758#if CAM_HAS_IRIS_DIAPHRAGM     
759if ((mode_get()&MODE_MASK) != MODE_PLAY){
760 long i;
761//    if ((v<ASID_MIN) || (v>ASID_MAX))
762//      return;
763 for (i=0;i<AS_SIZE;i++){
764        if (aperture_sizes_table[i].prop_id == v){
765           shooting_set_av96_direct(v, is_now);
766           return;
767        }
768  }
769}
770#endif 
771}
772
773void shooting_set_nd_filter_state(short v, short is_now)
774{
775#if CAM_HAS_ND_FILTER
776if ((mode_get()&MODE_MASK) != MODE_PLAY){
777        if (is_now) {
778         if (v==1) _PutInNdFilter();
779         else if (v==2) _PutOutNdFilter();
780        }
781        else photo_param_put_off.nd_filter=v;
782}
783#endif 
784}
785
786
787
788void shooting_set_av96_direct(short v, short is_now)
789
790#if CAM_HAS_IRIS_DIAPHRAGM
791if ((mode_get()&MODE_MASK) != MODE_PLAY){
792 if(is_now) {
793   _SetPropertyCase(PROPCASE_AV, &v, sizeof(v));
794#ifdef PROPCASE_AV2
795   _SetPropertyCase(PROPCASE_AV2, &v, sizeof(v));   // Save override to property that will update JPEG header & Canon OSD
796#endif
797 }
798 else photo_param_put_off.av96=v;
799}
800#endif
801}
802
803
804void shooting_set_user_av96(short v)
805{
806#if CAM_HAS_IRIS_DIAPHRAGM
807    long i;
808if ((mode_get()&MODE_MASK) != MODE_PLAY){
809//    if ((v<ASID_MIN) || (v>ASID_MAX))
810//      return;
811
812 for (i=0;i<AS_SIZE;i++){
813        if (aperture_sizes_table[i].prop_id == v){
814                  _SetPropertyCase(PROPCASE_USER_AV, &v, sizeof(v));
815                  //_SetPropertyCase(PROPCASE_AV, &v, sizeof(v));
816            return;
817        }
818 }
819}
820#endif             
821}
822
823short shooting_get_drive_mode()
824{
825    short m;
826// these cameras do not set PROPCASE_DRIVE_MODE when in custom timer mode
827// SX 200 IS 0,1,2,3,4=Off, 2 Second, Custom, Face Detection
828#if defined (CAM_DRIVE_MODE_FROM_TIMER_MODE)
829   short n;
830   _GetPropertyCase(PROPCASE_TIMER_MODE, &n, sizeof(n));
831   // note we return this only in custom mode.
832   // Other cameras would return 3 if any timer mode is set (?) even if it doesn't do multiple exposures
833   // note that face detect does multiple exposure
834   if(n==3){
835      return n;
836   }
837#endif
838
839    _GetPropertyCase(PROPCASE_DRIVE_MODE, &m, sizeof(m));
840    return m;
841}
842
843short shooting_can_focus()
844{
845 int m=mode_get()&MODE_SHOOTING_MASK;
846#if !CAM_CAN_SD_OVER_NOT_IN_MF && CAM_CAN_SD_OVERRIDE
847 #if CAM_CAN_SD_OVER_IN_AF_LOCK
848  if (shooting_get_prop(PROPCASE_AF_LOCK))
849    return 1;
850 #endif
851  return (shooting_get_common_focus_mode() || MODE_IS_VIDEO(m));
852#elif !CAM_CAN_SD_OVERRIDE
853  return MODE_IS_VIDEO(m);
854#elif defined (CAMERA_ixus800_sd700)
855// TODO whats the reason for this ?!?
856  return (shooting_get_zoom()<8) && (m!=MODE_AUTO) && (m!=MODE_SCN_UNDERWATER);
857#else
858  return 1; 
859#endif                   
860}
861
862short shooting_get_common_focus_mode()
863{
864#if !CAM_HAS_MANUAL_FOCUS && CAM_CAN_SD_OVERRIDE
865  return shooting_get_subject_distance_override_koef();
866#elif !CAM_CAN_SD_OVERRIDE
867  return 0;
868#else
869  return shooting_get_focus_mode();
870#endif                   
871}
872
873short shooting_get_focus_mode()
874{
875  short m;
876  _GetPropertyCase(PROPCASE_FOCUS_MODE, &m, sizeof(m));
877  return m;
878}
879
880short shooting_get_real_focus_mode()
881{
882  short f=shooting_get_focus_mode();
883  short m;
884  _GetPropertyCase(PROPCASE_REAL_FOCUS_MODE, &m, sizeof(m));
885  if (f==0 && m!=0) f=(m==1)?4:m;
886  return f;
887}
888
889short shooting_get_focus_state()
890{
891  if (shooting_get_focus_mode()==1) return -1;
892  int m;
893  _GetPropertyCase(PROPCASE_FOCUS_STATE, &m, sizeof(m));
894  return (short) m;
895}
896 
897short shooting_get_focus_ok()
898{
899  return ((shooting_get_focus_state()!=0) && shooting_in_progress());
900}
901
902/*
903short shooting_get_continuous_mode_shoot_count()
904{
905    short c;
906    _GetPropertyCase(PROPCASE_CONT_MODE_SHOOT_COUNT, &c, sizeof(c));
907    return c;
908}
909*/
910
911
912void shooting_set_user_av_by_id(int v)
913{
914#if CAM_HAS_IRIS_DIAPHRAGM
915long i;
916if ((mode_get()&MODE_MASK) != MODE_PLAY){
917//    if ((v<ASID_MIN) || (v>ASID_MAX))
918//      return;
919  for (i=0;i<AS_SIZE;i++){
920                if (aperture_sizes_table[i].id == v){
921            short vv = aperture_sizes_table[i].prop_id;
922            _SetPropertyCase(PROPCASE_USER_AV, &vv, sizeof(vv));
923            //_SetPropertyCase(PROPCASE_AV, &vv, sizeof(vv));
924            return;
925                }
926  }
927}
928#endif       
929}
930
931void shooting_set_user_av_by_id_rel(int v)
932{
933#if CAM_HAS_IRIS_DIAPHRAGM
934if ((mode_get()&MODE_MASK) != MODE_PLAY){
935    int cv = shooting_get_user_av_id();
936    shooting_set_user_av_by_id(cv+v);
937}
938#endif   
939}
940
941
942int shooting_get_day_seconds()
943{
944    unsigned long t;
945    struct tm *ttm;
946    t = time(NULL);
947    ttm = localtime(&t);
948    return ttm->tm_hour * 3600 + ttm->tm_min * 60 + ttm->tm_sec;
949}
950
951int shooting_get_tick_count()
952{
953    return (int)get_tick_count();
954}
955
956void shooting_set_iso_mode(int v)
957{
958 long i;
959 for (i=0;i<ISO_SIZE;i++){
960        if (iso_table[i].id == v){
961            short vv = iso_table[i].prop_id;
962            _SetPropertyCase(PROPCASE_ISO_MODE, &vv, sizeof(vv));
963            return;
964        }
965  }
966}
967
968int shooting_in_progress()
969{
970    int t = 0;
971    _GetPropertyCase(PROPCASE_SHOOTING, &t, sizeof(&t));
972    return t != 0;
973}
974
975int shooting_is_flash()
976{
977 int t = 0;     
978 _GetPropertyCase(PROPCASE_IS_FLASH_READY, &t,sizeof(&t));
979 return t;
980}
981
982int shooting_is_flash_ready()
983{
984   int t = 0;
985/* well, I'm not sure what's exactly is happening here
986 * but it works for a610-100e
987 */
988   // _GetPropertyCase(204, &t, 4);
989   // if (t == 3){
990   _GetPropertyCase(PROPCASE_FLASH_MODE, &t, sizeof(&t));
991   if ((t != 2) && (shooting_in_progress()))
992     if (shooting_is_flash()) return _IsStrobeChargeCompleted();
993   return 1;
994}
995
996long get_file_counter()
997{
998#ifndef PARAM_FILE_COUNTER
999#       error Please define PARAM_FILE_COUNTER in platform`s shooting.c!
1000#endif
1001    long v = 0;
1002    get_parameter_data(PARAM_FILE_COUNTER, &v, 4);
1003    return v;
1004}
1005
1006long get_exposure_counter()
1007{
1008#if defined(USE_FILE_COUNTER_FOR_EXP_COUNTING) || !defined(PARAM_EXPOSURE_COUNTER)
1009    long v = 0;
1010    get_parameter_data(PARAM_FILE_COUNTER, &v, 4);
1011    v = ((v>>4)&0x3FFF);
1012    return v;
1013#else
1014    long v = 0;
1015    get_parameter_data(PARAM_EXPOSURE_COUNTER, &v, 4);
1016    return v;
1017#endif
1018}
1019
1020short shooting_get_ev_correction1()
1021{
1022    short evc1 = 0;
1023    _GetPropertyCase(PROPCASE_EV_CORRECTION_1, &evc1, sizeof(evc1));
1024    return evc1;
1025}
1026
1027#if CAM_QUALITY_OVERRIDE
1028void shooting_set_image_quality(int imq)
1029{
1030  if (imq != 3){
1031    _SetPropertyCase(PROPCASE_QUALITY, &imq, sizeof(imq));
1032  }
1033}
1034#endif
1035
1036int shooting_get_zoom() {
1037    return lens_get_zoom_point();
1038}
1039
1040void shooting_set_zoom(int v) {
1041    int dist;
1042if ((mode_get()&MODE_MASK) != MODE_PLAY)
1043    {
1044    dist = shooting_get_focus();
1045    lens_set_zoom_point(v);
1046#if defined(CAM_NEED_SET_ZOOM_DELAY)
1047    msleep(CAM_NEED_SET_ZOOM_DELAY);
1048#endif
1049    shooting_set_focus(dist, SET_NOW);
1050  }
1051}
1052
1053void shooting_set_zoom_rel(int v) {
1054    int cv = shooting_get_zoom();
1055if ((mode_get()&MODE_MASK) != MODE_PLAY){
1056    shooting_set_zoom(cv+v);
1057}
1058}
1059
1060void shooting_set_zoom_speed(int v) {
1061if ((mode_get()&MODE_MASK) != MODE_PLAY){
1062    lens_set_zoom_speed(v);
1063}
1064}
1065
1066int shooting_get_focus() {
1067        return shooting_get_subject_distance();
1068}
1069
1070void shooting_set_focus(int v, short is_now) {
1071    int s=v;
1072    if ((mode_get()&MODE_MASK) != MODE_PLAY){
1073        if ((is_now) && shooting_can_focus()) {
1074            if (conf.dof_subj_dist_as_near_limit) {
1075                s=shooting_get_near_limit_f(v,shooting_get_min_real_aperture(),get_focal_length(lens_get_zoom_point()));
1076            }
1077            if (!conf.dof_use_exif_subj_dist && (s != INFINITY_DIST))
1078                s+=shooting_get_lens_to_focal_plane_width();
1079            lens_set_focus_pos(s);
1080        }
1081        else photo_param_put_off.subj_dist=v;
1082    }
1083}
1084
1085void shooting_calc_autoiso_coef( int min_shutter )
1086{
1087        if ( conf.autoiso2_min_shutter_numerator >= min_shutter ) {
1088                conf.autoiso2_coef = 0.0;
1089        }
1090        else {
1091                conf.autoiso2_coef = (float)(conf.autoiso2_max_iso_auto_real - conf.autoiso_max_iso_auto_real) /
1092                                (float)( conf.autoiso2_min_shutter_numerator - min_shutter);
1093        }
1094}
1095
1096void shooting_recalc_conf_autoiso_values()
1097{
1098
1099#ifndef USE_REAL_AUTOISO
1100    // Initialize only once
1101    static int iso_b = 0;   // real base iso
1102    static int iso_m = 0;   // marketing base iso
1103
1104    // @tsv - marketing to real iso correspondance is quite linear so just got multiplier
1105    // Base values are correct only when shoot is prepared, so calc multiplier right before first shoot
1106    if ( !iso_m || !iso_b )
1107    {
1108        iso_b = 10 * shooting_get_iso_base();   // 10 - is additional multiplier from user_entered_value
1109        iso_m = shooting_get_iso_market_base();
1110
1111    }
1112
1113    // check zero in to_market convertion numerator
1114    if ( !iso_m )
1115    iso_m = iso_m ? iso_m : 1;
1116#else
1117    // If real ISO in GUI, then just multiply by 10
1118    static int iso_b = 10;
1119    static int iso_m = 1;
1120
1121#endif
1122
1123    // Calculate realISO (real = market * iso_b / iso_m)
1124    conf.autoiso_max_iso_hi_real    = conf.autoiso_max_iso_hi    * iso_b / iso_m;
1125    conf.autoiso_max_iso_auto_real  = conf.autoiso_max_iso_auto  * iso_b / iso_m;
1126    conf.autoiso_min_iso_real       = conf.autoiso_min_iso       * iso_b / iso_m;     
1127    conf.autoiso2_max_iso_auto_real = conf.autoiso2_max_iso_auto * iso_b / iso_m;
1128
1129    // There are two exceptional situation:
1130    // 1. shutter_numerator2 should be < shutter_numerator1, otherwise exceptional situation
1131    // 2. autoiso2 <= autoiso1
1132    if ( !conf.autoiso2_min_shutter_numerator ) {
1133        conf.autoiso2_max_iso_auto_real = conf.autoiso_max_iso_auto_real;
1134    }
1135
1136    // C2=( iso2_max_auto_real - iso_max_auto_real) / ( tv_num[autoiso2_shutter] - tv_numerator[autoiso_shutter])
1137    shooting_calc_autoiso_coef( conf.autoiso_min_shutter_numerator );
1138}
1139
1140void shooting_set_autoiso(int iso_mode) {
1141        short max_iso;
1142        if ( iso_mode<=0 && conf.autoiso_max_iso_auto_real==0) {
1143                shooting_recalc_conf_autoiso_values();
1144        }
1145        switch (iso_mode) {
1146                case -1: // ISO HI
1147                        //max_iso = conf.autoiso_max_iso_hi*10;
1148                        max_iso = conf.autoiso_max_iso_hi_real;
1149                        break;
1150                case 0: // ISO AUTO
1151                        //max_iso = conf.autoiso_max_iso_auto*10;
1152                        max_iso = conf.autoiso_max_iso_auto_real;
1153                        break;
1154                default:
1155                        return;
1156        }
1157        int m=mode_get()&MODE_SHOOTING_MASK;
1158        // TODO also long shutter ?
1159        if (m==MODE_M || m==MODE_TV || m==MODE_STITCH) return; //Only operate outside of M and Tv
1160        int ev_overexp = 0;
1161        if ( conf.overexp_ev_enum )
1162        {
1163                // No shoot_histogram exist here because no future shot exist yet :)
1164                live_histogram_process_quick();
1165
1166                // step 32 is 1/3ev for tv96
1167                if ( live_histogram_get_range(255-conf.zebra_over,255) >= conf.overexp_threshold ) {
1168                        ev_overexp = conf.overexp_ev_enum << 5;
1169                }
1170                live_histogram_end_process();
1171        }
1172
1173#ifdef OVEREXP_COMPENSATE_OVERALL
1174        float current_shutter = shooting_get_shutter_speed_from_tv96( shooting_get_tv96() + ev_overexp );
1175#else
1176        float current_shutter = shooting_get_shutter_speed_from_tv96(shooting_get_tv96());
1177#endif
1178       
1179        short current_iso=shooting_get_iso_real();
1180               
1181        short min_shutter = conf.autoiso_min_shutter_numerator;
1182        if (min_shutter == 0) {
1183                short IS_factor = (shooting_get_is_mode()<=1)?conf.autoiso_is_factor:1;
1184                min_shutter = get_focal_length(lens_get_zoom_point())*conf.autoiso_user_factor / (IS_factor*1000);
1185                //min_shutter is NOT 1/Xs but optimized for the calculation.
1186                if ( conf.autoiso2_min_shutter_numerator ) {
1187                        shooting_calc_autoiso_coef( min_shutter );
1188                }
1189        }
1190
1191        short target_iso = current_iso * min_shutter * current_shutter;
1192        short min_iso = conf.autoiso_min_iso_real;
1193       
1194        if (target_iso > max_iso) {
1195                ev_overexp=0;
1196
1197                // AutoISO2 if
1198                //      it is turned on (C2!=0.0)
1199                //      and it has valid iso2/shutter2 ( C2<0)
1200                //       and non-IsoHI mode
1201                if ( !iso_mode && conf.autoiso2_coef < 0.0 ) {
1202                        target_iso = (max_iso - min_shutter*conf.autoiso2_coef) / ( 1.0 - conf.autoiso2_coef  / (current_shutter * current_iso) );
1203                        if ( target_iso > conf.autoiso2_max_iso_auto_real ) {
1204                                target_iso = conf.autoiso2_max_iso_auto_real;
1205                        }
1206                } else {
1207                        target_iso = max_iso;
1208                }
1209        } else if (target_iso < min_iso) {
1210                target_iso = min_iso;
1211        }
1212       
1213        float target_shutter = current_shutter *  current_iso / target_iso;
1214       
1215#ifdef OVEREXP_COMPENSATE_OVERALL
1216        shooting_set_shutter_speed(target_shutter, 0, SET_NOW);
1217#else
1218        // Daylight only (below autoiso_max) overexp compensation
1219        shooting_set_shutter_speed(target_shutter, ev_overexp, SET_NOW);
1220#endif
1221       
1222        shooting_set_iso_real(target_iso, SET_NOW);
1223       
1224}
1225
1226void shooting_video_bitrate_change(int v){
1227 int m[]={1,2,3,4,5,6,7,8,10,12};  // m[v]/4 = bitrate*1x
1228 if (v>=(sizeof(m)/sizeof(m[0]))) v=(sizeof(m)/sizeof(m[0]))-1;
1229#if CAM_CHDK_HAS_EXT_VIDEO_MENU
1230 change_video_tables(m[v],4);
1231#endif
1232}
1233
1234float shooting_get_shutter_speed_override_value()
1235{
1236  return (float)conf.tv_override_value*shutter_koef[conf.tv_override_koef]; 
1237}
1238
1239const char * shooting_get_tv_bracket_value()
1240{
1241  return expo_shift[conf.tv_bracket_value];
1242}
1243
1244const char * shooting_get_tv_override_value()
1245{
1246  return tv_override[conf.tv_override_value];
1247}
1248
1249
1250const char * shooting_get_bracket_type()
1251{
1252  return expo_type[conf.bracket_type]; 
1253}
1254
1255
1256short shooting_get_iso_override_value()
1257{
1258  short iso = conf.iso_override_value*koef[conf.iso_override_koef];
1259#ifdef CAM_ISO_LIMIT_IN_HQ_BURST
1260  // Limit max ISO in HQ burst mode (also done in shooting_set_iso_real; but done here so OSD display value is correct)
1261  if ((mode_get() & MODE_SHOOTING_MASK) == MODE_SCN_HIGHSPEED_BURST)
1262    if (iso > CAM_ISO_LIMIT_IN_HQ_BURST) iso = CAM_ISO_LIMIT_IN_HQ_BURST;
1263#endif
1264  return iso;
1265}
1266
1267short shooting_get_iso_bracket_value()
1268{
1269  return conf.iso_bracket_value*koef[conf.iso_bracket_koef]; 
1270}
1271
1272
1273
1274short shooting_get_av96_override_value()
1275{
1276  if (conf.av_override_value<=AS_SIZE)  return (short) aperture_sizes_table[conf.av_override_value-1].prop_id;
1277  else return (short) (AV96_MAX+32*(conf.av_override_value-AS_SIZE));
1278}
1279
1280const char * shooting_get_av_bracket_value()
1281{
1282  return expo_shift[conf.av_bracket_value]; 
1283}
1284
1285
1286int shooting_get_subject_distance_override_value()
1287{
1288    if (conf.subj_dist_override_value != INFINITY_DIST)
1289        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()));
1290    else
1291        return INFINITY_DIST;
1292}
1293
1294int shooting_get_subject_distance_bracket_value()
1295{
1296  return conf.subj_dist_bracket_value*koef[conf.subj_dist_bracket_koef];
1297}
1298
1299int shooting_get_subject_distance_override_koef()
1300{
1301    // Define the adjustment factor values for the subject distance override
1302#if MAX_DIST > 1000000      // Superzoom - e.g. SX30, SX40
1303  static const int koef[] = {0,1,10,100,1000,10000,100000,1000000,-1};
1304#elif MAX_DIST > 100000     // G12, IXUS310
1305  static const int koef[] = {0,1,10,100,1000,10000,100000,-1};
1306#else                       // Original values (MAX_DIST = 65535)
1307  static const int koef[] = {0,1,10,100,1000};
1308#endif
1309  return koef[(conf.subj_dist_override_koef)];
1310}
1311
1312void shooting_tv_bracketing(){
1313  short value, is_odd;
1314  int m=mode_get()&MODE_SHOOTING_MASK;
1315  if (bracketing.shoot_counter==0) { // first shoot
1316    bracketing.shoot_counter=1;
1317    // if Tv override is enabled... (this was adapted from function shooting_expo_param_override() )
1318    if ( ((conf.tv_enum_type) || (conf.tv_override_value)) && (conf.tv_override_koef) && !(conf.override_disable==1) ) {
1319      // ...use Tv override value as seed for bracketing:
1320      if (conf.tv_enum_type)
1321        bracketing.tv96 = 32*(conf.tv_override_value-tv_override_zero_shift);
1322      else
1323        bracketing.tv96 = shooting_get_tv96_from_shutter_speed(shooting_get_shutter_speed_override_value());
1324    }
1325    // Tv override is disabled, use camera's opinion of Tv for bracketing seed value.
1326    else {
1327      if (!(m==MODE_M || m==MODE_TV || m==MODE_LONG_SHUTTER)) bracketing.tv96=shooting_get_tv96();
1328      else bracketing.tv96=shooting_get_user_tv96();
1329    }
1330    bracketing.tv96_step=32*conf.tv_bracket_value;
1331  }
1332  // other shoots
1333   bracketing.shoot_counter++;   
1334   is_odd=(bracketing.shoot_counter&1);
1335   if ((!is_odd) || (conf.bracket_type>0)) bracketing.dtv96+=bracketing.tv96_step;
1336   if (((!is_odd) && (conf.bracket_type==0)) || (conf.bracket_type==1))  value=bracketing.tv96-bracketing.dtv96;
1337   else value=bracketing.tv96+bracketing.dtv96;
1338   shooting_set_tv96_direct(value, SET_NOW);
1339}
1340
1341void shooting_av_bracketing(){
1342    short value,is_odd;
1343
1344    int m = mode_get()&MODE_SHOOTING_MASK;
1345
1346    if (bracketing.shoot_counter == 0) { // first shoot
1347        bracketing.shoot_counter = 1;
1348        //short av_override_value=shooting_get_av96_override_value;
1349        //if (av_override_value) bracketing.av96=av_override_value;
1350        if (!(m==MODE_M || m==MODE_AV))
1351            bracketing.av96 = shooting_get_av96();
1352        else
1353            bracketing.av96 = shooting_get_user_av96();
1354        bracketing.av96_step = 32*conf.av_bracket_value;
1355    }
1356    // other shoots
1357    bracketing.shoot_counter++;
1358    is_odd = (bracketing.shoot_counter&1);
1359    value = bracketing.av96;
1360
1361    if ( !is_odd || (conf.bracket_type > 0) ||
1362         ( is_odd && (conf.bracket_type == 0) && ((bracketing.av96 - bracketing.dav96) < AV96_MIN) ))
1363    {
1364       bracketing.dav96 += bracketing.av96_step;
1365    }
1366
1367    if (((!is_odd && (conf.bracket_type == 0)) || (conf.bracket_type == 1)) && ((bracketing.av96 - bracketing.dav96) >= AV96_MIN))
1368    {
1369        value -= bracketing.dav96;
1370    }
1371    else if ((is_odd && (conf.bracket_type == 0))
1372                || (conf.bracket_type == 2)
1373                || ((!is_odd && (conf.bracket_type == 0)) && ((bracketing.av96 - bracketing.dav96) < AV96_MIN)))
1374    {
1375        value += bracketing.dav96;
1376    }
1377
1378    if (value != bracketing.av96)
1379    {
1380        shooting_set_av96_direct(value, SET_NOW);
1381#ifdef CAM_AV_OVERRIDE_IRIS_FIX
1382        extern int _MoveIrisWithAv(short*);
1383        _MoveIrisWithAv(&value);
1384#endif
1385    }
1386}
1387
1388
1389void shooting_iso_bracketing(){
1390 short value=0, is_odd;
1391 if (bracketing.shoot_counter==0) { // first shoot
1392    bracketing.shoot_counter=1;
1393    bracketing.iso=shooting_get_iso_real();
1394    bracketing.iso_step=shooting_get_iso_bracket_value();
1395    }
1396  // other shoots
1397   bracketing.shoot_counter++;   
1398   is_odd=(bracketing.shoot_counter&1);
1399   
1400   if (((!is_odd) || (conf.bracket_type>0)) || (((is_odd) && (conf.bracket_type==0)) && (bracketing.iso<=bracketing.diso)))
1401    {
1402           bracketing.diso+=bracketing.iso_step;
1403    }
1404   if ((((!is_odd) && (conf.bracket_type==0)) || (conf.bracket_type==1)) && (bracketing.iso>bracketing.diso))
1405     {
1406     value=bracketing.iso-bracketing.diso;
1407     shooting_set_iso_real(value, SET_NOW);
1408     }
1409   else if ((((is_odd) && (conf.bracket_type==0)) || (conf.bracket_type==2)) || (((!is_odd) && (conf.bracket_type==0)) && (bracketing.iso<=bracketing.diso)))
1410     {
1411     value=bracketing.iso+bracketing.diso;
1412     shooting_set_iso_real(value, SET_NOW);
1413     }
1414}
1415
1416void shooting_subject_distance_bracketing(){
1417 short value=0, is_odd;
1418 if (bracketing.shoot_counter==0) { // first shoot
1419    bracketing.shoot_counter=1;
1420    bracketing.subj_dist=shooting_get_focus();
1421    bracketing.subj_dist_step=shooting_get_subject_distance_bracket_value();
1422 }
1423  // other shoots
1424   bracketing.shoot_counter++;   
1425   is_odd=(bracketing.shoot_counter&1);
1426   if (((!is_odd) || (conf.bracket_type>0)) || (((is_odd) && (conf.bracket_type==0)) && (bracketing.subj_dist<=bracketing.dsubj_dist)))
1427   {
1428           bracketing.dsubj_dist+=bracketing.subj_dist_step;
1429   }
1430   if ((((!is_odd) && (conf.bracket_type==0)) || (conf.bracket_type==1)) && (bracketing.subj_dist>bracketing.dsubj_dist))
1431     {
1432     value=bracketing.subj_dist-bracketing.dsubj_dist;
1433     shooting_set_focus(value, SET_NOW);
1434     }
1435   else if ((((is_odd) && (conf.bracket_type==0)) || (conf.bracket_type==2)) || (((!is_odd) && (conf.bracket_type==0)) && (bracketing.subj_dist<=bracketing.dsubj_dist)))
1436     {
1437     value=bracketing.subj_dist+bracketing.dsubj_dist;
1438     shooting_set_focus(value, SET_NOW);
1439    }
1440}
1441
1442
1443void shooting_bracketing(void){
1444  if (shooting_get_drive_mode()!=0)  {
1445    int m=mode_get()&MODE_SHOOTING_MASK;
1446    if (m!=MODE_STITCH && m!=MODE_SCN_BEST_IMAGE) {
1447      if (state_shooting_progress != SHOOTING_PROGRESS_PROCESSING) {
1448        bracketing.shoot_counter=0;
1449        bracketing.av96=0;
1450        bracketing.dav96=0;
1451        bracketing.tv96=0;
1452        bracketing.dtv96=0;
1453        bracketing.sv96=0;
1454        bracketing.dsv96=0;
1455        bracketing.iso=0;
1456        bracketing.diso=0;
1457        bracketing.subj_dist=0;
1458        bracketing.dsubj_dist=0;
1459        bracketing.type=0;
1460      }
1461      if (conf.tv_bracket_value && !(conf.override_disable==1 && conf.override_disable_all))  shooting_tv_bracketing();
1462      else if (conf.av_bracket_value && !(conf.override_disable==1 && conf.override_disable_all)) shooting_av_bracketing();
1463      else if ((conf.iso_bracket_value && !(conf.override_disable==1 && conf.override_disable_all)) && (conf.iso_bracket_koef)) shooting_iso_bracketing();
1464      else if ((conf.subj_dist_bracket_value && !(conf.override_disable==1 && conf.override_disable_all)) && (conf.subj_dist_bracket_koef)) shooting_subject_distance_bracketing();
1465      else if ((conf.subj_dist_bracket_value) && (conf.subj_dist_bracket_koef)) shooting_subject_distance_bracketing();
1466    }
1467  }
1468}
1469
1470#if CAM_REAR_CURTAIN
1471  void shooting_set_flash_sync_curtain(int curtain){
1472   _SetPropertyCase(PROPCASE_FLASH_SYNC_CURTAIN, &curtain, sizeof(curtain));
1473  }
1474#endif
1475
1476// forces manual flash. if set, flash will ALWAYS be triggered (manual)
1477        void shooting_set_flash_video_override(int flash, int power){
1478                int mode = 1;
1479                if ((conf.flash_manual_override && conf.flash_video_override && (movie_status > 1)) || (conf.flash_manual_override && !conf.flash_video_override)) {
1480                                                _SetPropertyCase(PROPCASE_FLASH_ADJUST_MODE, &mode, sizeof(mode));
1481                                                _SetPropertyCase(PROPCASE_FLASH_FIRE, &flash, sizeof(flash));
1482                                                _SetPropertyCase(PROPCASE_FLASH_MANUAL_OUTPUT, &power, sizeof(power));
1483                                        }
1484  }
1485
1486// TODO sd990 hack for overrides
1487// caller must save regs
1488int captseq_hack_override_active() {
1489 if (state_kbd_script_run)
1490        if ( photo_param_put_off.tv96 || photo_param_put_off.sv96 )
1491                return 1;
1492 if(conf.override_disable==1)
1493        return 0;
1494 if(conf.iso_override_value && conf.iso_override_koef)
1495        return 1;
1496 if((conf.tv_enum_type || conf.tv_override_value) && conf.tv_override_koef)
1497        return 1;
1498 return 0;
1499}
1500
1501void __attribute__((naked,noinline)) shooting_expo_param_override(void){
1502 asm volatile("STMFD   SP!, {R0-R12,LR}\n");
1503
1504 //if (conf.tv_override) shooting_set_tv96_direct(-384-32*conf.tv_override);
1505 //short drive_mode=shooting_get_drive_mode();
1506/*if(drive_mode!=last_drive_mode)
1507 {
1508  if (last_drive_mode==0) shoot_counter=0;
1509  last_drive_mode=drive_mode;
1510 }*/
1511 //int m=mode_get()&MODE_SHOOTING_MASK;
1512 if ((state_kbd_script_run) && (photo_param_put_off.tv96)) {
1513  shooting_set_tv96_direct(photo_param_put_off.tv96, SET_NOW); 
1514  photo_param_put_off.tv96=0;
1515 }
1516   else if (((conf.tv_enum_type) || (conf.tv_override_value)) && (conf.tv_override_koef) && !(conf.override_disable==1))
1517   {
1518   if (conf.tv_enum_type)
1519     shooting_set_tv96_direct(32*(conf.tv_override_value-(tv_override_zero_shift)),SET_NOW);
1520   else   
1521    shooting_set_tv96_direct(shooting_get_tv96_from_shutter_speed(shooting_get_shutter_speed_override_value()), SET_NOW);
1522   }
1523 if ((state_kbd_script_run) && (photo_param_put_off.sv96)) {
1524  shooting_set_sv96(photo_param_put_off.sv96, SET_NOW);
1525  photo_param_put_off.sv96=0;
1526  }
1527else if ((conf.iso_override_value) && (conf.iso_override_koef) && !(conf.override_disable==1))
1528  shooting_set_iso_real(shooting_get_iso_override_value(), SET_NOW);
1529 else if (conf.autoiso_enable && shooting_get_flash_mode()/*NOT FOR FLASH AUTO MODE*/ && !(conf.override_disable==1 && conf.override_disable_all))
1530  shooting_set_autoiso(shooting_get_iso_mode());
1531 if ((state_kbd_script_run) && (photo_param_put_off.av96)) {
1532  shooting_set_av96_direct(photo_param_put_off.av96, SET_NOW);
1533  photo_param_put_off.av96=0;
1534  }
1535 else if (conf.av_override_value && !(conf.override_disable==1))
1536 shooting_set_av96_direct(shooting_get_av96_override_value(), SET_NOW);
1537 if ((state_kbd_script_run) && (photo_param_put_off.subj_dist)) {
1538  shooting_set_focus(photo_param_put_off.subj_dist, SET_NOW);
1539  photo_param_put_off.subj_dist=0; 
1540  }
1541  else if ((conf.subj_dist_override_value) && (conf.subj_dist_override_koef) && !(conf.override_disable==1))
1542   shooting_set_focus(shooting_get_subject_distance_override_value(), SET_NOW);
1543#if CAM_HAS_ND_FILTER
1544 if ((state_kbd_script_run) && (photo_param_put_off.nd_filter)) {
1545   shooting_set_nd_filter_state(photo_param_put_off.nd_filter, SET_NOW);
1546   photo_param_put_off.nd_filter=0; 
1547  }
1548 else if (conf.nd_filter_state && !(conf.override_disable==1))
1549   shooting_set_nd_filter_state(conf.nd_filter_state, SET_NOW);
1550#endif 
1551
1552#if CAM_REAR_CURTAIN
1553  shooting_set_flash_sync_curtain(conf.flash_sync_curtain);
1554#endif
1555
1556
1557  shooting_set_flash_video_override(conf.flash_manual_override,conf.flash_video_override_power);
1558#if CAM_QUALITY_OVERRIDE
1559 // this doesn't really need to be set in the override hook.
1560 // should only need to be set once if the users doesn't change back, but doing it here ensures it is set
1561 shooting_set_image_quality(conf.fast_image_quality);
1562#endif
1563 asm volatile("LDMFD   SP!, {R0-R12,PC}\n");
1564}
1565
1566void unlock_optical_zoom(void){
1567
1568#if CAM_CAN_UNLOCK_OPTICAL_ZOOM_IN_VIDEO
1569 if (conf.unlock_optical_zoom_for_video) _UnsetZoomForMovie();
1570#endif
1571
1572#if CAM_EV_IN_VIDEO
1573 set_ev_video_avail(0);
1574#endif
1575
1576}
1577
1578
1579#if CAM_EV_IN_VIDEO
1580
1581int ev_video_avail;
1582int ev_video;
1583int save_tv_video;
1584int tv_min_video;
1585
1586int get_ev_video_avail(void){
1587 return ev_video_avail;
1588}
1589
1590void set_ev_video_avail(int x){
1591 if (ev_video_avail==x) return;
1592 ev_video_avail=x;
1593 if (x) {
1594  ev_video=0;
1595  _ExpCtrlTool_StopContiAE(0,0);
1596  _GetPropertyCase(PROPCASE_TV,&save_tv_video,2);
1597  if ((mode_get()&MODE_SHOOTING_MASK)==MODE_VIDEO_SPEED) tv_min_video=577;  // 1/60
1598   else tv_min_video=480; //1/30
1599 }
1600 else _ExpCtrlTool_StartContiAE(0,0);
1601}
1602
1603int get_ev_video(void){
1604 return ev_video;
1605}
1606
1607void set_ev_video(int x){
1608 short ev, tv;
1609 if ((x<-4)||(x>4)) return;
1610 ev=48*x;
1611 tv=save_tv_video-ev;
1612 if (tv>=tv_min_video) {
1613  ev_video=x;
1614  _SetAE_ShutterSpeed(&tv);
1615 }
1616}
1617
1618#endif
1619
1620int shooting_mode_canon2chdk(int canonmode) {
1621        int i;
1622        for (i=0; i < MODESCNT; i++) {
1623                if (modemap[i].canonmode == canonmode)
1624                        return modemap[i].hackmode;
1625        }
1626        return 0;
1627}
1628
1629int shooting_mode_chdk2canon(int hackmode) {
1630        int i;
1631        for (i=0; i < MODESCNT; i++) {
1632                if (modemap[i].hackmode == hackmode)
1633                        return modemap[i].canonmode;
1634        }
1635        return -1; // 0 is a valid mode on old cameras!
1636}
1637
1638int shooting_set_mode_chdk(int mode) {
1639        int canonmode = shooting_mode_chdk2canon(mode);
1640        if (canonmode == -1 || !rec_mode_active())
1641                return 0;
1642        _SetCurrentCaptureModeType(canonmode);
1643        // TODO since mode seems to be fully set when this returns,
1644        // we could check if it worked
1645        return 1;
1646}
1647
1648int shooting_set_mode_canon(int canonmode) {
1649        if(canonmode == -1 || !rec_mode_active())
1650                return 0;
1651        _SetCurrentCaptureModeType(canonmode);
1652        return 1;
1653}
1654
1655// override in platform/<cam>/main.c if playrec_mode is not found or different
1656int __attribute__((weak)) rec_mode_active(void) {
1657    return (playrec_mode==2 || playrec_mode==4 || playrec_mode==5);
1658}
1659
1660// Converted from MODE_IS_VIDEO macro (philmoz July 2011)
1661int mode_is_video(int m)
1662{
1663    m = m & MODE_SHOOTING_MASK;
1664    return (m==MODE_VIDEO_STD ||
1665            m==MODE_VIDEO_SPEED ||
1666            m==MODE_VIDEO_COMPACT ||
1667            m==MODE_VIDEO_MY_COLORS ||
1668            m==MODE_VIDEO_COLOR_ACCENT ||
1669            m==MODE_VIDEO_COLOR_SWAP ||
1670            m==MODE_VIDEO_MINIATURE ||
1671            m==MODE_VIDEO_TIME_LAPSE ||
1672            m==MODE_VIDEO_PORTRAIT ||
1673            m==MODE_VIDEO_NIGHT ||
1674            m==MODE_VIDEO_INDOOR ||
1675            m==MODE_VIDEO_FOLIAGE ||
1676            m==MODE_VIDEO_SNOW  ||
1677            m==MODE_VIDEO_BEACH ||
1678            m==MODE_VIDEO_AQUARIUM ||
1679            m==MODE_VIDEO_SUPER_MACRO ||
1680            m==MODE_VIDEO_STITCH ||
1681            m==MODE_VIDEO_MANUAL ||
1682            m==MODE_VIDEO_MINIATURE ||
1683            m==MODE_VIDEO_IFRAME_MOVIE
1684// not clear if this should be considered a video mode ?
1685/*            m==MODE_VIDEO_MOVIE_DIGEST*/
1686            );
1687}
1688
1689// currently nothing needs to override this, so not weak
1690int /*__attribute__((weak))*/ mode_get(void) {
1691    int mode, t=0xFF;
1692
1693    mode = (rec_mode_active())?MODE_REC:MODE_PLAY;
1694
1695#ifdef CAM_SWIVEL_SCREEN
1696    mode |= (screen_opened())?MODE_SCREEN_OPENED:0;
1697    mode |= (screen_rotated())?MODE_SCREEN_ROTATED:0;
1698#endif
1699
1700    _GetPropertyCase(PROPCASE_SHOOTING_MODE, &t, 4);
1701        mode |= shooting_mode_canon2chdk(t);
1702
1703    return (mode);
1704}
1705
1706// Only needed on VxWorks
1707#if CAM_DRAW_EXPOSITION
1708
1709// compare PROPCASE_TV with shutter_speeds_table
1710char* shooting_get_tv_str() {
1711    short int tvv;
1712    long i;
1713    _GetPropertyCase(PROPCASE_TV, &tvv, sizeof(tvv));
1714    for(i=0; i<SS_SIZE; i++) {
1715        if(shutter_speeds_table[i].prop_id >= tvv)
1716            return (char*)shutter_speeds_table[i].name;
1717    }
1718    return (void*)"?";
1719}
1720
1721// compare PROPCASE_AV with aperture_sizes_table
1722char* shooting_get_av_str() {
1723    short int avv;
1724    long i;
1725    _GetPropertyCase(PROPCASE_AV, &avv, sizeof(avv));
1726    for(i=0; i<AS_SIZE; i++) {
1727        if(aperture_sizes_table[i].prop_id == avv)
1728            return (char*)aperture_sizes_table[i].name;
1729    }
1730    return (char*) "?";
1731}
1732
1733#endif
1734
1735/*
1736char* shooting_get_iso_str() {
1737    short int isov;
1738    long i;
1739    _GetPropertyCase(PROPCASE_ISO_MODE, &isov, sizeof(isov));
1740    for(i=0; i<ISO_SIZE; i++) {
1741        if(iso_table[i].prop_id == isov)
1742            return (char*)iso_table[i].name;
1743    }
1744    return (char*) "?";
1745}
1746*/
Note: See TracBrowser for help on using the repository browser.