#include <stdio.h>
//#include "Types.h"
#include "Debug.h"
#include "Sensor.h"
#include "Ael.h"
#include "Fll.h"
#include "AESceneMode.h"
#include <math.h>
//#include "..\..\main\hce\Capture.h"
//#include "..\..\..\Main\Hce\AwbCal.h"
#include "Os.h"
// Debug define
#define DISPLAY_INFO 0
#define DEBUG_AEEXIF 0
#define DEBUG_AEDRIVER 0
#define DEBUG_AEEVAL 0
#define DEBUG_AEINPUT 0
#define DEBUG_AEWEIGHT 0
#if DISPLAY_INFO
#include "Draw.h"
#endif
#define abs(a) ((a) >=0?(a):-(a))
#define AELOCK 1
#define AEUNLOCK 0
//extern ULONG flash_fileid;
//extern unsigned char flash_write_data[100];
//static unsigned short histogram[256];
static unsigned long sAERvals[WIN_SIZE_X][WIN_SIZE_Y];
static unsigned long InputLuma[32][32];
//static unsigned long sAERvalsThem[WIN_SIZE_X][WIN_SIZE_Y];
//static unsigned long AELuma[32][32];
static unsigned int AEstart;
static unsigned int AEstart_forAF;
static float EVbias;
//static char buffWriteState = 0 ;//0 :finished writed 1:writing
// apex system
static APEXDat apexAntiShake;
static APEXDat apexStill;
static APEXDat apexMonitor;
static APEXDat apexMovie;
static APEXDat apexAF;
static APEXDat apexStore;
// exif data
static unsigned int exif_fireflash;
static unsigned char exif_metering;
static unsigned char exif_aperturePosition;
static unsigned short exif_fnumber;
static signed short exif_av;
static signed short exif_maxav;
static signed short exif_sv;
static signed short exif_tv;
static signed short exif_lv;
static unsigned short exif_flashmode;
static signed long exif_EVbias;
static unsigned long exif_Numerator;
static unsigned long exif_Denominator;
// for anti-shake mode
static signed long AntiShake_sv;
static signed long AntiShake_tv;
static signed long deltaEV;
static unsigned short shutterCloseTime;
static unsigned short shutterDelay_DB;
static unsigned char metering;
static unsigned char Weight[WIN_SIZE_X][WIN_SIZE_Y];
static unsigned long averageLuma;
static unsigned long lockambientLuma;
static unsigned long targetLuma;
static unsigned long TotalAmbientLuma;
static unsigned long TotalWeight;
static signed short previous_LV = 0;
static signed short pprevious_LV = 0;
static signed short current_LV = 0;
static unsigned char LV_trend = LV_FORWARD;
extern unsigned long histogram[256];
STATIC TX_SEMAPHORE txAeInfoProtect;
STATIC TX_SEMAPHORE txflashlightInfoProtect;
/*
// degamma table (m735)
static const unsigned long degammaTbl[] =
{
500 , 3000 , 6000 , 9500 , 13000 , 16000 , 19000 , 22000 ,
25000 , 28000 , 31000 , 34500 , 38000 , 41000 , 44000 , 47000 ,
50000 , 53000 , 56500 , 60000 , 63000 , 66000 , 69000 , 72000 ,
75000 , 78500 , 82000 , 85000 , 88000 , 91000 , 94000 , 97500 ,
101000 , 104000 , 107000 , 110000 , 113500 , 117000 , 120000 , 123000 ,
126000 , 129500 , 133000 , 136000 , 139000 , 142000 , 145500 , 149000 ,
152000 , 155000 , 158000 , 161500 , 165000 , 168000 , 171500 , 175000 ,
178000 , 181000 , 184500 , 188000 , 191000 , 194500 , 198000 , 201000 ,
204500 , 208000 , 211000 , 214500 , 218000 , 221000 , 224500 , 228000 ,
231500 , 235000 , 238000 , 241500 , 245000 , 248500 , 252000 , 255000 ,
258500 , 262000 , 265500 , 269000 , 272500 , 276000 , 279500 , 283000 ,
286500 , 290000 , 293500 , 297500 , 301000 , 304500 , 308000 , 311500 ,
315500 , 319000 , 322500 , 326500 , 330000 , 333500 , 337500 , 341500 ,
345000 , 348500 , 352500 , 356500 , 360000 , 363500 , 367500 , 371500 ,
375500 , 379500 , 383500 , 387500 , 391500 , 395500 , 399500 , 403500 ,
407500 , 411500 , 415500 , 419500 , 424000 , 428500 , 432500 , 436500 ,
440500 , 445000 , 449500 , 454000 , 458500 , 462500 , 467000 , 471500 ,
476000 , 481000 , 486000 , 490500 , 495000 , 500000 , 505000 , 510000 ,
515000 , 520000 , 525000 , 530000 , 535000 , 540000 , 545500 , 551000 ,
556000 , 561500 , 567500 , 573500 , 579500 , 585500 , 591500 , 597500 ,
603500 , 610000 , 617000 , 623500 , 630000 , 637500 , 645000 , 652500 ,
660000 , 668000 , 676500 , 685000 , 694000 , 703000 , 712500 , 722000 ,
732000 , 742500 , 753500 , 765000 , 777000 , 790000 , 803000 , 816500 ,
831500 , 847500 , 864000 , 882000 , 901000 , 920500 , 941000 , 962000 ,
984000 , 1006500 , 1028500 , 1050500 , 1072500 , 1094500 , 1116500 , 1138000 ,
1159500 , 1181000 , 1202500 , 1224500 , 1247000 , 1269500 , 1291500 , 1313500 ,
1336000 , 1359500 , 1383000 , 1407000 , 1432000 , 1457500 , 1484000 , 1512000 ,
1541500 , 1572500 , 1606000 , 1642000 , 1680500 , 1723000 , 1768000 , 1814500 ,
1863500 , 1913000 , 1962500 , 2011000 , 2057000 , 2101000 , 2143500 , 2184500 ,
2224500 , 2264500 , 2304500 , 2344500 , 2385500 , 2428000 , 2473000 , 2520500 ,
2571000 , 2627500 , 2690500 , 2761000 , 2839000 , 2921000 , 3002500 , 3079500 ,
3150500 , 3217500 , 3283500 , 3350000 , 3420000 , 3499000 , 3601000 , 3878497
};
*/
/*
// degamma table (T737) 2007-03-09
static const unsigned long degammaTbl[] =
{
2500 , 8000 , 13000 , 18000 , 23000 , 28000 , 33000 , 38500 ,
44000 , 49000 , 54000 , 59000 , 64000 , 69000 , 74000 , 79000 ,
84000 , 89500 , 95000 , 100000 , 105000 , 110000 , 115000 , 120000 ,
125000 , 130000 , 135000 , 140000 , 144500 , 149000 , 154000 , 159000 ,
164000 , 169000 , 174000 , 179000 , 183500 , 188000 , 193000 , 198000 ,
202500 , 207000 , 212000 , 217000 , 221500 , 226000 , 231000 , 235500 ,
240000 , 244500 , 249000 , 253500 , 258000 , 263000 , 267500 , 271500 ,
276000 , 280500 , 285000 , 289500 , 294000 , 298500 , 302500 , 307000 ,
311500 , 315500 , 320000 , 324500 , 328500 , 333000 , 337500 , 341500 ,
346000 , 350500 , 354500 , 358500 , 363000 , 367500 , 371500 , 375500 ,
380000 , 384500 , 388500 , 392500 , 397000 , 401500 , 405500 , 409500 ,
414000 , 418500 , 422500 , 426500 , 430500 , 435000 , 439500 , 443500 ,
447500 , 452000 , 456500 , 460500 , 464500 , 469000 , 473500 , 477500 ,
482000 , 486500 , 490500 , 494500 , 499000 , 503500 , 507500 , 512000 ,
516500 , 521000 , 525500 , 529500 , 534000 , 538500 , 543000 , 547500 ,
552000 , 556500 , 561000 , 565500 , 570000 , 574500 , 579000 , 583500 ,
588000 , 593000 , 597500 , 602000 , 607000 , 611500 , 616000 , 621000 ,
626000 , 631000 , 636000 , 641000 , 646000 , 651000 , 656000 , 661000 ,
666000 , 671000 , 676000 , 681000 , 686500 , 692000 , 697000 , 702500 ,
708000 , 713500 , 719000 , 724500 , 730000 , 735500 , 741500 , 747500 ,
753500 , 759500 , 765000 , 771000 , 777500 , 783500 , 789500 , 796000 ,
802500 , 808500 , 815000 , 822000 , 829000 , 835500 , 842000 , 849500 ,
857000 , 864000 , 871500 , 879000 , 886500 , 894500 , 902500 , 910500 ,
919000 , 928000 , 936500 , 945500 , 955000 , 964500 , 974500 , 984500 ,
995000 , 1006500 , 1018000 , 1030000 , 1043000 , 1056500 , 1071000 , 1086500 ,
1103500 , 1121500 , 1140500 , 1161500 , 1184500 , 1209500 , 1236500 , 1265000 ,
1295000 , 1325500 , 1356500 , 1388500 , 1420500 , 1453000 , 1486000 , 1519000 ,
1552500 , 1586500 , 1620500 , 1655500 , 1692000 , 1730000 , 1771000 , 1815500 ,
1864000 , 1916500 , 1970000 , 2022000 , 2071500 , 2117500 , 2160500 , 2202000 ,
2243000 , 2283000 , 2322500 , 2363000 , 2404000 , 2445500 , 2489000 , 2535000 ,
2585000 , 2641000 , 2704000 , 2776500 , 2859000 , 2947000 , 3031000 , 3105000 ,
3170500 , 3232500 , 3295000 , 3361500 , 3437000 , 3530500 , 3711000 , 3967496
};
*/
// degamma table (T737) 2007-04-02
static const unsigned long degammaTbl[] =
{
0 , 66000 , 69000 , 72000 , 75000 , 78500 , 82000 , 85000 ,
88000 , 91000 , 94500 , 98000 , 101000 , 104000 , 107000 , 110000 ,
113500 , 117000 , 120000 , 123000 , 126500 , 130000 , 133000 , 136000 ,
139500 , 143000 , 146000 , 149000 , 152000 , 155500 , 159000 , 162000 ,
165000 , 168500 , 172000 , 175000 , 178500 , 182000 , 185000 , 188000 ,
191500 , 195000 , 198000 , 201000 , 204500 , 208000 , 211000 , 214500 ,
218000 , 221000 , 224500 , 228000 , 231000 , 234500 , 238000 ,