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