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