flash sequence for 3A added.
[adaptation/intel_mfld/gst-plugins-atomisp.git] / gst-libs / atomisphal / gstv4l2mfldadvci.c
1 /* GStreamer MFLD ADVIC API
2  * Copyright (C) 2010 Intel Corporation <www.intel.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #include "gstv4l2mfldadvci.h"
21 #include <linux/videodev2.h>
22 #include <sys/ioctl.h>
23 #include <stdio.h>
24 #include <gst/gst.h>
25 #include "ci_adv_pub.h"
26
27 void cam_lib_3a_dbg(const char *format, ...);
28
29 int __android_log_print(int prio, const char *tag, const char *fmt, ...)
30 {
31  return 0;
32 }
33
34 /* for debug message and error message output
35  *
36  */
37 static bool use_3A_debug = FALSE;
38
39 void
40 cam_lib_3a_dbg (const char *format, ...)
41 {
42   va_list ap;
43   if (use_3A_debug) {
44     va_start (ap, format);
45     vfprintf (stdout, format, ap);
46     va_end (ap);
47   }
48 }
49
50
51 static void
52 lib_3a_void (void)
53 {
54   return;
55 }
56
57 static void
58 lib_3a_int (int a)
59 {
60   cam_lib_3a_dbg("%s:%d\n", __func__, __LINE__);
61   return;
62 }
63
64 static int
65 lib_3a_int_void (void)
66 {
67   return 0;
68 }
69
70 static void
71 lib_3a_ae (void)
72 {
73   return;
74 }
75
76 static void
77 lib_3a_init(void)
78 {
79   const char *env;
80   use_3A_debug= ((env = getenv ("LIBMFLDCAM_DEBUG")) && strstr (env, "verbose"));
81   cam_lib_3a_dbg("%s:%d", __func__, __LINE__);
82 }
83
84 static void
85 lib_3a_uninit(void)
86 {
87   cam_lib_3a_dbg("%s:%d\n", __func__, __LINE__);
88   ci_adv_uninit();
89 }
90
91 static void
92 lib_3a_dis_process (struct atomisp_dis_statistics *stats,
93                      struct atomisp_dis_vector *motion_vector)
94 {
95   cam_lib_3a_dbg("%s:%d\n", __func__, __LINE__);
96   return;
97 }
98
99 static void
100 lib_3a_dis_calc_still (struct atomisp_dis_vector * vector, int frame_number)
101 {
102   cam_lib_3a_dbg("%s:%d\n", __func__, __LINE__);
103   void (*dis_calc_still)(vector,frame_number);
104   return;
105 }
106
107 static void
108 lib_3a_do_redeye_removal (ia_frame *ia_frame)
109 {
110   cam_lib_3a_dbg("%s:%d\n", __func__, __LINE__);
111   ia_redeye_correct(ia_frame);
112 }
113
114 static void
115 lib_3a_still_compose (ia_frame *com_buf,
116                      ia_frame bufs[],
117                      int frame_dis,
118                      struct atomisp_dis_vector vectors[])
119 {
120   cam_lib_3a_dbg("%s:%d\n", __func__, __LINE__);
121   ia_dis_still_compose(com_buf, bufs, frame_dis, vectors);
122   return;
123 }
124
125 static void
126 lib_3a_AeSetFlickerMode(ia_3a_ae_flicker_mode mode)
127 {
128   cam_lib_3a_dbg("%s:%d mode:%d\n", __func__, __LINE__,mode);
129   ci_adv_ae_set_flicker_mode(mode);
130 }
131
132 static void
133 lib_3a_AeGetFlickerMode(ia_3a_ae_flicker_mode *mode)
134 {
135   cam_lib_3a_dbg("%s:%d\n", __func__, __LINE__);
136   *mode= ia_3a_ae_get_flicker_mode();
137 }
138
139 static void
140 lib_3a_AeSetExposureProgram(ia_3a_ae_exposure_program program)
141 {
142   cam_lib_3a_dbg("%s:%d\n", __func__, __LINE__);
143   ci_adv_ae_set_exposure_program(program);
144 }
145
146 static void
147 lib_3a_AeGetExposureProgram(ia_3a_ae_exposure_program *program)
148 {
149   cam_lib_3a_dbg("%s:%d\n", __func__, __LINE__);
150   ci_adv_ae_get_exposure_program(program);
151 }
152
153 static void
154 lib_3a_AfSetMode(ia_3a_af_mode mode)
155 {
156   cam_lib_3a_dbg("%s:%d mode:%d\n", __func__, __LINE__,mode);
157   ci_adv_af_set_mode(mode);
158 }
159
160 static void
161 lib_3a_AfGetMode(ia_3a_af_mode *mode)
162 {
163   cam_lib_3a_dbg("%s:%d\n", __func__, __LINE__);
164   *mode = ia_3a_af_get_focus_mode();
165 }
166
167 static void
168 lib_3a_AfSetRange(ia_3a_af_range range)
169 {
170   cam_lib_3a_dbg ("%s:%d\n", __func__, __LINE__);
171   ci_adv_af_set_range(range);
172 }
173
174 static void
175 lib_3a_AfGetRange(ia_3a_af_range *range)
176 {
177   cam_lib_3a_dbg("%s:%d\n", __func__, __LINE__);
178   *range = ia_3a_af_get_focus_range();
179 }
180
181 static void
182 lib_3a_AwbSetMode(ia_3a_awb_mode mode)
183 {
184   cam_lib_3a_dbg("%s:%d mode:%d\n" , __func__, __LINE__,mode);
185   ci_adv_awb_set_mode(mode);
186 }
187
188 static void
189 lib_3a_AwbGetMode(ia_3a_awb_mode *mode)
190 {
191   *mode = ia_3a_awb_get_mode();
192 }
193
194 static void
195 lib_3a_AwbSetLightSource(ia_3a_awb_light_source ls)
196 {
197   cam_lib_3a_dbg("%s:%d\n", __func__, __LINE__);
198   ci_adv_awb_set_light_source(ls);
199 }
200
201 static void
202 lib_3a_AwbGetLightSource(ia_3a_awb_light_source *ls)
203 {
204   cam_lib_3a_dbg("%s:%d\n", __func__, __LINE__);
205   *ls = ia_3a_awb_get_light_source();
206 }
207
208 static ci_adv_Err
209 lib_3a_int_int (int i)
210 {
211   cam_lib_3a_dbg("%s:%d\n", __func__, __LINE__);
212   return ci_adv_Success;
213 }
214
215 static ci_adv_Err
216 lib_3a_intp (int *p)
217 {
218   cam_lib_3a_dbg("%s:%d\n", __func__, __LINE__);
219   return ci_adv_Success;
220 }
221
222 static int
223 lib_3a_isp_set_fd (int fd, const char *sensor_id)
224 {
225   int sensor_type;
226   ia_3a_window window;
227
228   cam_lib_3a_dbg("%s:%d\n", __func__, __LINE__);
229   if (ci_adv_init(sensor_id, fd, NULL) == 0) {
230     sensor_type = SENSOR_TYPE_RAW;
231   } else {
232     sensor_type = SENSOR_TYPE_SOC;
233  }
234
235   if (sensor_type == SENSOR_TYPE_RAW) {
236     cam_lib_3a_dbg("SENSOR_TYPE_RAW");
237     ci_adv_awb_enable(TRUE);
238     ci_adv_awb_lock(FALSE);
239
240     ci_adv_ae_enable(TRUE);
241     ci_adv_ae_lock(FALSE);
242
243     ci_adv_af_enable(TRUE);
244     ci_adv_af_lock(FALSE);
245
246     window.x_left = 0;
247     window.x_right = 0,
248     window.y_top = 0;
249     window.y_bottom = 0;
250     window.weight = 0;
251
252     ci_adv_ae_set_window(&window);
253
254     ci_adv_ae_set_exposure_program(ia_3a_ae_exposure_program_auto);
255     ci_adv_ae_set_mode(ia_3a_ae_mode_auto);
256     ci_adv_af_set_range (ia_3a_af_range_full);
257     ci_adv_awb_set_mode(ia_3a_awb_mode_auto);
258     ci_adv_af_set_mode(ia_3a_af_mode_auto);
259     ci_adv_af_set_metering_mode (ia_3a_af_metering_mode_auto);
260
261   }
262
263   return sensor_type;
264 }
265
266 static void
267 lib_3a_switch_mode (ia_3a_isp_mode mode, float frame_rate)
268 {
269   cam_lib_3a_dbg("%s:%d mode:%d frame_rate:%f\n", __func__, __LINE__, mode,frame_rate);
270   ci_adv_configure(mode, frame_rate);
271 }
272
273 static ci_adv_Err lib_3a_AeGetWindowsNum(int *num)
274 {
275   cam_lib_3a_dbg ("%s:%d\n", __func__, __LINE__);
276   return ci_adv_Success;
277 }
278
279 static void lib_3a_AwbVersion(int * major, int * minor)
280 {
281   cam_lib_3a_dbg("%s:%d\n", __func__, __LINE__);
282   ci_adv_awb_version(major,minor);
283 }
284
285 static void lib_3a_AeVersion(int * major, int * minor)
286 {
287   cam_lib_3a_dbg("%s:%d\n", __func__, __LINE__);
288   ci_adv_ae_version(major,minor);
289 }
290
291 static void lib_3a_AfVersion(int * major, int * minor)
292 {
293   cam_lib_3a_dbg("%s:%d\n", __func__, __LINE__);
294   ci_adv_af_version(major,minor);
295 }
296
297 static void lib_3a_AfGetStillAfStatus(ia_3a_af_status *status)
298 {
299   cam_lib_3a_dbg("%s:%d\n", __func__, __LINE__);
300   *status = ci_adv_af_get_status();
301 }
302
303 static void lib_3a_RerVersion(int * major, int * minor)
304 {
305   cam_lib_3a_dbg("%s:%d\n", __func__, __LINE__);
306   return;
307 }
308
309 static ci_adv_Err lib_3a_AeGetManualShutter(int *time)
310 {
311   /* TODO time is float */
312   cam_lib_3a_dbg("%s:%d\n", __func__, __LINE__);
313   ci_adv_ae_set_manual_shutter((float)*time);
314   return ci_adv_Success;
315 }
316
317 static void lib_3a_Awbdata(unsigned * num_p, unsigned * avg_r, unsigned * avg_gr,
318         unsigned * avg_b, unsigned * avg_gb)
319 {
320   cam_lib_3a_dbg("%s:%d\n", __func__, __LINE__);
321   return;
322 }
323
324 static void lib_3a_AeGetManualAperture(int *aperture)
325 {
326   // TODO returns float
327   cam_lib_3a_dbg("%s:%d\n", __func__, __LINE__);
328   *aperture = (int)ia_3a_ae_get_manual_aperture();
329 }
330
331 static void lib_3a_AeGetFlashMode(ia_3a_ae_flash_mode *mode)
332 {
333   cam_lib_3a_dbg("%s:%d\n", __func__, __LINE__);
334   *mode= ia_3a_ae_get_flash_mode();
335 }
336
337 static void lib_3a_AeSetFlashMode(ia_3a_ae_flash_mode mode)
338 {
339   cam_lib_3a_dbg("%s:%d mode:%d \n", __func__, __LINE__, mode);
340   ci_adv_ae_set_flash_mode(mode);
341 }
342
343 static void lib_3a_AwbGetManualColorTemperature(int *ctemp)
344 {
345   cam_lib_3a_dbg("%s:%d\n", __func__, __LINE__);
346   *ctemp = ia_3a_awb_get_manual_color_temperature();
347 }
348
349 static void
350 lib_3a_Awbmatrix(int msqWbGain[3], int msqCcMtrx[9], unsigned short * shift)
351 {
352   cam_lib_3a_dbg ("%s:%d\n", __func__, __LINE__);
353 }
354
355 static void lib_3a_GetGridInfo(void * grid_info)
356 {
357   cam_lib_3a_dbg("%s:%d\n", __func__, __LINE__);
358 }
359
360 static void
361 lib_3a_AeSetMeteringMode(ia_3a_ae_metering_mode mode)
362 {
363   cam_lib_3a_dbg("%s:%d\n", __func__, __LINE__);
364   ci_adv_ae_set_metering_mode(mode);
365 }
366
367 static void
368 lib_3a_AeGetMeteringMode(ia_3a_ae_metering_mode *mode)
369 {
370   cam_lib_3a_dbg("%s:%d\n", __func__, __LINE__);
371   *mode = ia_3a_ae_get_metering_mode();
372 }
373
374 static void
375 lib_3a_AfSetMeteringMode(ia_3a_af_metering_mode mode)
376 {
377   cam_lib_3a_dbg("%s:%d\n", __func__, __LINE__);
378   ci_adv_ae_set_metering_mode(mode);
379 }
380
381 static void
382 lib_3a_AfGetMeteringMode(ia_3a_af_metering_mode *mode)
383 {
384   cam_lib_3a_dbg("%s:%d\n", __func__, __LINE__);
385   ci_adv_ae_get_metering_mode(mode);
386 }
387
388 static void
389 lib_3a_Ae_Af_GetWindow(ia_3a_window *window)
390 {
391   cam_lib_3a_dbg("%s:%d\n", __func__, __LINE__);
392   ia_3a_ae_get_window(window);
393 }
394
395 static void
396 lib_3a_Ae_Af_SetWindow( ia_3a_window *window)
397 {
398   cam_lib_3a_dbg("%s:window....%d:%d:%d:%d:%d\n",
399       __func__, window->x_left,window->x_right,
400       window->y_top , window->y_bottom, window->weight);
401
402   ci_adv_ae_set_window(window);
403   ci_adv_af_set_windows(1, window);
404 }
405
406 static void
407 lib_3a_af_start( )
408 {
409   cam_lib_3a_dbg("%s:%d\n", __func__, __LINE__);
410   ci_adv_af_start();
411 }
412
413 static void
414 lib_3a_af_stop( )
415 {
416   cam_lib_3a_dbg ("%s:%d\n", __func__, __LINE__);
417   ci_adv_af_stop();
418 }
419
420 static void
421 lib_3a_ae_calc_for_flash( )
422 {
423   cam_lib_3a_dbg("%s:%d\n", __func__, __LINE__);
424   ci_adv_process_for_flash(ia_3a_flash_stage_none);
425 }
426 static void
427 lib_3a_calc_without_flash( )
428 {
429   cam_lib_3a_dbg("%s:%d\n", __func__, __LINE__);
430   ci_adv_process_for_flash(ia_3a_flash_stage_pre);
431 }
432
433 static void
434 lib_3a_calc_with_flash( )
435 {
436   cam_lib_3a_dbg("%s:%d\n", __func__, __LINE__);
437   ci_adv_process_for_flash(ia_3a_flash_stage_main);
438 }
439
440 static void
441 lib_3a_AeAfAwb_process( struct timeval *frame_timestamp)
442 {
443   int status;
444   //cam_lib_3a_dbg("%s:%d\n", __func__, __LINE__);
445   status =  ci_adv_process_frame(TRUE,frame_timestamp);
446 }
447 static void
448 lib_3a_get_statistics( )
449 {
450  // cam_lib_3a_dbg("%s:%d\n", __func__, __LINE__);
451   ci_adv_3a_stat cur_stat;
452   ci_adv_get_3a_stat (&cur_stat);
453  /* cam_lib_3a_dbg("%8.3f, %8.3f, %8.3f, %8.3f, %8d, %8.3f, %8.3f, %8.3f\n",
454                 cur_stat.bv,
455                 cur_stat.tv,
456                 cur_stat.sv,
457                 cur_stat.av,
458                 cur_stat.focus_pos,
459                 cur_stat.wb_gain_r,
460                 cur_stat.wb_gain_g,
461                 cur_stat.wb_gain_b);
462 */
463 }
464
465 static void
466 lib_3a_awb_apply_results( )
467 {
468 }
469 static void
470 lib_3a_AwbSetImageEffect(ia_3a_image_effect effect)
471 {
472   cam_lib_3a_dbg("%s:%d effect %d\n", __func__, __LINE__, effect);
473
474   ci_adv_isp_set_image_effect(effect);
475 }
476
477 static void
478 lib_3a_AwbGetImageEffect(ia_3a_image_effect *effect)
479 {
480   *effect = ci_adv_isp_get_image_effect();
481   cam_lib_3a_dbg("%s:%d effect: %d\n", __func__, __LINE__);
482
483 }
484
485 void
486 lib_3a_link_functions_init (GstV4l2MFLDAdvCI *mfldadvci)
487 {
488
489   mfldadvci->initialized = 0;
490   mfldadvci->init = lib_3a_init;
491   mfldadvci->uninit = lib_3a_uninit;
492   mfldadvci->isp_set_fd = lib_3a_isp_set_fd;
493   mfldadvci->mode_spec_init = lib_3a_int_void; /* TODO */
494   mfldadvci->switch_mode = lib_3a_switch_mode;
495
496   mfldadvci->AeAfAwb_process = lib_3a_AeAfAwb_process;
497   mfldadvci->get_statistics = lib_3a_get_statistics; /* TODO atomisp_dis_statistics  ? */
498
499   mfldadvci->ae_calc_for_flash = lib_3a_ae_calc_for_flash;
500   mfldadvci->ae_calc_without_flash = lib_3a_calc_without_flash;
501   mfldadvci->ae_calc_with_flash = lib_3a_calc_with_flash;
502   mfldadvci->ae_apply_results = lib_3a_ae;
503
504   mfldadvci->af_start = lib_3a_af_start;
505   mfldadvci->af_stop = lib_3a_af_stop;
506   mfldadvci->af_is_complete = lib_3a_int_void;
507   mfldadvci->awb_apply_results = lib_3a_awb_apply_results;
508   mfldadvci->awb_calc_flash = lib_3a_void;
509
510   mfldadvci->dis_read_statistics = lib_3a_void;
511   mfldadvci->update_dis_results = lib_3a_void;
512   mfldadvci->dis_process = lib_3a_dis_process;
513   mfldadvci->dis_calc_still = lib_3a_dis_calc_still;
514
515   mfldadvci->do_redeye_removal = lib_3a_do_redeye_removal;
516   mfldadvci->still_compose = lib_3a_still_compose;
517   mfldadvci->load_gdc_table = lib_3a_void;
518
519   mfldadvci->AeSetBias = lib_3a_int_int;
520   mfldadvci->AeGetBias = lib_3a_intp;
521   mfldadvci->AeSetFlickerMode = lib_3a_AeSetFlickerMode;
522   mfldadvci->AeGetFlickerMode = lib_3a_AeGetFlickerMode;
523   mfldadvci->AeSetExposureProgram = lib_3a_AeSetExposureProgram;
524   mfldadvci->AeGetExposureProgram = lib_3a_AeGetExposureProgram;
525   mfldadvci->AeSetMeteringMode = lib_3a_AeSetMeteringMode;
526   mfldadvci->AeGetMeteringMode = lib_3a_AeGetMeteringMode;
527   mfldadvci->AeGetWindow = lib_3a_Ae_Af_GetWindow;
528   mfldadvci->AeSetWindow = lib_3a_Ae_Af_SetWindow;
529
530
531   mfldadvci->AfSetMode = lib_3a_AfSetMode;
532   mfldadvci->AfGetMode = lib_3a_AfGetMode;
533   mfldadvci->AfSetRange = lib_3a_AfSetRange;
534   mfldadvci->AfGetRange = lib_3a_AfGetRange;
535   mfldadvci->AfSetMeteringMode = lib_3a_AfSetMeteringMode;
536   mfldadvci->AfGetMeteringMode = lib_3a_AfGetMeteringMode;
537   mfldadvci->AfGetWindow = lib_3a_Ae_Af_GetWindow;
538   mfldadvci->AfSetWindow = lib_3a_Ae_Af_SetWindow;
539
540   mfldadvci->AwbSetMode = lib_3a_AwbSetMode;
541   mfldadvci->AwbGetMode = lib_3a_AwbGetMode;
542   mfldadvci->AwbSetLightSource = lib_3a_AwbSetLightSource;
543   mfldadvci->AwbGetLightSource = lib_3a_AwbGetLightSource;
544   mfldadvci->AwbSetImageEffect = lib_3a_AwbSetImageEffect;
545   mfldadvci->AwbGetImageEffect = lib_3a_AwbGetImageEffect;
546
547   mfldadvci->AeGetWindowsNum = lib_3a_AeGetWindowsNum;
548   mfldadvci->AwbVersion = lib_3a_AwbVersion;
549   mfldadvci->AeVersion = lib_3a_AeVersion;
550   mfldadvci->AfVersion = lib_3a_AfVersion;
551   mfldadvci->AfGetStillAfStatus = lib_3a_AfGetStillAfStatus;
552   mfldadvci->RerVersion = lib_3a_RerVersion;
553   mfldadvci->AeGetManualShutter = lib_3a_AeGetManualShutter;
554   mfldadvci->Awbdata = lib_3a_Awbdata;
555   mfldadvci->AeGetManualAperture = lib_3a_AeGetManualAperture;
556   mfldadvci->AeSetFlashMode = lib_3a_AeSetFlashMode;
557   mfldadvci->AeGetFlashMode = lib_3a_AeGetFlashMode;
558   mfldadvci->AwbGetManualColorTemperature = lib_3a_AwbGetManualColorTemperature;
559   mfldadvci->Awbmatrix = lib_3a_Awbmatrix;
560   mfldadvci->GetGridInfo = lib_3a_GetGridInfo;
561 }