blob: db159559ca0c134d9b9cfba25b31c99a00e36334
1 | #define LOG_TAG "AutoBackLight" |
2 | |
3 | #include "AutoBackLight.h" |
4 | #include "../tvsetting/CTvSetting.h" |
5 | #include "../tvconfig/tvconfig.h" |
6 | #include "../tvutils/tvutils.h" |
7 | #include <stdio.h> |
8 | #include <unistd.h> |
9 | #include <sys/types.h> |
10 | #include <sys/stat.h> |
11 | #include <sys/ioctl.h> |
12 | #include <fcntl.h> |
13 | #include <string.h> |
14 | #include <errno.h> |
15 | #include <dlfcn.h> |
16 | #include <linux/fb.h> |
17 | #include <stdlib.h> |
18 | #include <cutils/properties.h> |
19 | |
20 | AutoBackLight::AutoBackLight() |
21 | { |
22 | mAutoBacklightSource = SOURCE_TYPE_TV; |
23 | mCur_source_default_backlight = 100; |
24 | mCur_sig_state == SIG_STATE_NOSIG; |
25 | mAutoBacklight_OnOff_Flag = false; |
26 | mCurrent_backlight = 100; |
27 | mCur_dest_backlight = 100; |
28 | } |
29 | |
30 | AutoBackLight::~AutoBackLight() |
31 | { |
32 | mAutoBacklight_OnOff_Flag = false; |
33 | } |
34 | |
35 | bool AutoBackLight::isAutoBacklightOn() |
36 | { |
37 | return mAutoBacklight_OnOff_Flag; |
38 | } |
39 | |
40 | void AutoBackLight::updateSigState(int state) |
41 | { |
42 | mCur_sig_state = state; |
43 | LOGD("updateSigState = %d", mCur_sig_state); |
44 | } |
45 | |
46 | void AutoBackLight::startAutoBacklight( tv_source_input_type_t source_type ) |
47 | { |
48 | mAutoBacklightSource = source_type; |
49 | mCur_source_default_backlight = CVpp::getInstance()->GetBacklight(source_type); |
50 | mCurrent_backlight = mCur_source_default_backlight; |
51 | CVpp::getInstance()->SetBacklight(mCur_source_default_backlight, source_type, 1); |
52 | |
53 | /* |
54 | mDefault_auto_bl_value = def_source_bl_value; |
55 | dynamicGamma = mDefault_auto_bl_value * mCur_source_default_backlight / 100; |
56 | // this if should not happen |
57 | if (dynamicGamma > mCur_source_default_backlight) { |
58 | dynamicGamma = mCur_source_default_backlight; |
59 | } |
60 | */ |
61 | |
62 | if (!mAutoBacklight_OnOff_Flag) { |
63 | mAutoBacklight_OnOff_Flag = true; |
64 | this->run(); |
65 | } |
66 | } |
67 | |
68 | void AutoBackLight::stopAutoBacklight() |
69 | { |
70 | if (mAutoBacklight_OnOff_Flag) { |
71 | mAutoBacklight_OnOff_Flag = false; |
72 | CVpp::getInstance()->SetBacklight(mCur_source_default_backlight, mAutoBacklightSource, 1); |
73 | } |
74 | } |
75 | |
76 | /** |
77 | * @ description: tpv project |
78 | * @ return:value |
79 | * value <= 20: mCur_dest_backlight is 14 |
80 | * 20 < value <= 160: mCur_dest_backlight is 57 |
81 | *160 < value: mCur_dest_backlight is 100 |
82 | */ |
83 | void AutoBackLight::adjustDstBacklight() |
84 | { |
85 | if (mCur_sig_state == SIG_STATE_STABLE) { |
86 | //the node is used to adjust current ts is static or dynamtic frame |
87 | char temp_str = 0; |
88 | int fd = open("/sys/module/di/parameters/frame_dynamic", O_RDWR); |
89 | if (fd <= 0) { |
90 | LOGE("open /sys/module/di/parameters/frame_dynamic ERROR!!\n"); |
91 | return; |
92 | } |
93 | |
94 | if (read(fd, &temp_str, 1) > 0) { |
95 | |
96 | if (temp_str == 'N') { |
97 | mCur_dest_backlight = mCur_source_default_backlight; |
98 | } else if (temp_str == 'Y') { |
99 | int pwm = HistogramGet_AVE(); |
100 | if (pwm <= 20) { |
101 | mCur_dest_backlight = 14; |
102 | } else if (pwm > 20 && pwm <= 160) { |
103 | mCur_dest_backlight = 57; |
104 | } else { |
105 | mCur_dest_backlight = 100; |
106 | } |
107 | //LOGD("pwm = %d, mCur_dest_backlight = %d", pwm, mCur_dest_backlight); |
108 | } |
109 | } |
110 | close(fd); |
111 | } else { |
112 | mCurrent_backlight = mCur_dest_backlight = mCur_source_default_backlight; |
113 | CVpp::getInstance()->SetBacklight(mCurrent_backlight, mAutoBacklightSource, 0); |
114 | } |
115 | |
116 | /* |
117 | if (pwm > 0) |
118 | pwm_max = pwm; |
119 | else |
120 | pwm_min = pwm; |
121 | pwm = 255 - pwm; |
122 | int average = (pwm_min + pwm_max) / 2; |
123 | dynamicGammaOffset = (pwm - average) / 10; |
124 | dynamicGammaOffset = dynamicGammaOffset * mDefault_auto_bl_value / 100; |
125 | |
126 | //the node is used to adjust current ts is static or dynamtic frame |
127 | char temp_str = 0; |
128 | int fd = open("/sys/module/di/parameters/frame_dynamic", O_RDWR); |
129 | if (fd <= 0) { |
130 | LOGE("open /sys/module/di/parameters/frame_dynamic ERROR!!\n"); |
131 | return; |
132 | } |
133 | |
134 | if (read(fd, &temp_str, 1) > 0) { |
135 | if (temp_str== 'N') { |
136 | mCur_dest_backlight = mCur_source_default_backlight; |
137 | } |
138 | else if (temp_str == 'Y') { |
139 | mCur_dest_backlight = dynamicGamma + dynamicGammaOffset; |
140 | |
141 | if (mCur_dest_backlight > mCur_source_default_backlight) { |
142 | mCur_dest_backlight = mCur_source_default_backlight; |
143 | } |
144 | else if (mCur_dest_backlight < 0) { |
145 | mCur_dest_backlight = 0; |
146 | } |
147 | } |
148 | } |
149 | close(fd); |
150 | */ |
151 | } |
152 | |
153 | void AutoBackLight::adjustBacklight() |
154 | { |
155 | if (mCurrent_backlight == mCur_dest_backlight) { |
156 | return; |
157 | } else if ((mCurrent_backlight - mCur_dest_backlight) > -2 && (mCurrent_backlight - mCur_dest_backlight) < 2) { |
158 | mCurrent_backlight = mCur_dest_backlight; |
159 | CVpp::getInstance()->SetBacklight(mCurrent_backlight, mAutoBacklightSource, 0); |
160 | } else if (mCurrent_backlight < mCur_dest_backlight) { |
161 | mCurrent_backlight = mCurrent_backlight + 2; |
162 | CVpp::getInstance()->SetBacklight(mCurrent_backlight, mAutoBacklightSource, 0); |
163 | } else if (mCurrent_backlight > mCur_dest_backlight) { |
164 | mCurrent_backlight = mCurrent_backlight - 2; |
165 | CVpp::getInstance()->SetBacklight(mCurrent_backlight, mAutoBacklightSource, 0); |
166 | } |
167 | |
168 | //LOGD("mCurrent_backlight = %d", mCurrent_backlight); |
169 | } |
170 | |
171 | /** |
172 | * @ description: get current picture's average brightness |
173 | * @ return: 0~255,0 is darkest,255 is brightest |
174 | */ |
175 | int AutoBackLight::HistogramGet_AVE() |
176 | { |
177 | int hist_ave = 0; |
178 | tvin_parm_t vdinParam; |
179 | if (0 == CTvin::getInstance()->VDIN_GetVdinParam(&vdinParam)) { |
180 | if (vdinParam.pixel_sum != 0) { |
181 | hist_ave = vdinParam.luma_sum / vdinParam.pixel_sum; |
182 | LOGD("[hist_ave][%d].", hist_ave); |
183 | return hist_ave; |
184 | } |
185 | LOGE("vdinParam.pixel_sum is zero, so the value is infinity\n"); |
186 | return -1; |
187 | } |
188 | LOGE("VDIN_GetVdinParam get data error!!!\n"); |
189 | return -1; |
190 | } |
191 | |
192 | bool AutoBackLight::threadLoop() |
193 | { |
194 | int sleeptime = 50;//ms |
195 | int adjustBacklightCount = 0; |
196 | while ( mAutoBacklight_OnOff_Flag ) { |
197 | usleep ( sleeptime * 1000 ); |
198 | adjustBacklightCount++; |
199 | if (adjustBacklightCount == 24) { |
200 | adjustBacklightCount = 0; |
201 | adjustDstBacklight(); |
202 | } |
203 | adjustBacklight(); |
204 | } |
205 | |
206 | return false;//return true, run again, return false,not run. |
207 | } |
208 |