blob: c87635abd668f2b7c4de8bb31ae311eae83fd212
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->GetBacklight(source_type); |
55 | mCurrent_backlight = mCur_source_default_backlight; |
56 | myVpp->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->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 | mCur_dest_backlight = mCur_source_default_backlight; |
103 | } else if (temp_str == 'Y') { |
104 | int pwm = HistogramGet_AVE(); |
105 | if (pwm <= 20) { |
106 | mCur_dest_backlight = 14; |
107 | } else if (pwm > 20 && pwm <= 160) { |
108 | mCur_dest_backlight = 57; |
109 | } else { |
110 | mCur_dest_backlight = 100; |
111 | } |
112 | //LOGD("pwm = %d, mCur_dest_backlight = %d", pwm, mCur_dest_backlight); |
113 | } |
114 | } |
115 | close(fd); |
116 | } else { |
117 | mCurrent_backlight = mCur_dest_backlight = mCur_source_default_backlight; |
118 | myVpp->SetBacklight(mCurrent_backlight, mAutoBacklightSource, 0); |
119 | } |
120 | |
121 | /* |
122 | if (pwm > 0) |
123 | pwm_max = pwm; |
124 | else |
125 | pwm_min = pwm; |
126 | pwm = 255 - pwm; |
127 | int average = (pwm_min + pwm_max) / 2; |
128 | dynamicGammaOffset = (pwm - average) / 10; |
129 | dynamicGammaOffset = dynamicGammaOffset * mDefault_auto_bl_value / 100; |
130 | |
131 | //the node is used to adjust current ts is static or dynamtic frame |
132 | char temp_str = 0; |
133 | int fd = open("/sys/module/di/parameters/frame_dynamic", O_RDWR); |
134 | if (fd <= 0) { |
135 | LOGE("open /sys/module/di/parameters/frame_dynamic ERROR!!\n"); |
136 | return; |
137 | } |
138 | |
139 | if (read(fd, &temp_str, 1) > 0) { |
140 | if (temp_str== 'N') { |
141 | mCur_dest_backlight = mCur_source_default_backlight; |
142 | } |
143 | else if (temp_str == 'Y') { |
144 | mCur_dest_backlight = dynamicGamma + dynamicGammaOffset; |
145 | |
146 | if (mCur_dest_backlight > mCur_source_default_backlight) { |
147 | mCur_dest_backlight = mCur_source_default_backlight; |
148 | } |
149 | else if (mCur_dest_backlight < 0) { |
150 | mCur_dest_backlight = 0; |
151 | } |
152 | } |
153 | } |
154 | close(fd); |
155 | */ |
156 | } |
157 | |
158 | void AutoBackLight::adjustBacklight() |
159 | { |
160 | if (mCurrent_backlight == mCur_dest_backlight) { |
161 | return; |
162 | } else if ((mCurrent_backlight - mCur_dest_backlight) > -2 && (mCurrent_backlight - mCur_dest_backlight) < 2) { |
163 | mCurrent_backlight = mCur_dest_backlight; |
164 | myVpp->SetBacklight(mCurrent_backlight, mAutoBacklightSource, 0); |
165 | } else if (mCurrent_backlight < mCur_dest_backlight) { |
166 | mCurrent_backlight = mCurrent_backlight + 2; |
167 | myVpp->SetBacklight(mCurrent_backlight, mAutoBacklightSource, 0); |
168 | } else if (mCurrent_backlight > mCur_dest_backlight) { |
169 | mCurrent_backlight = mCurrent_backlight - 2; |
170 | myVpp->SetBacklight(mCurrent_backlight, mAutoBacklightSource, 0); |
171 | } |
172 | |
173 | //LOGD("mCurrent_backlight = %d", mCurrent_backlight); |
174 | } |
175 | |
176 | /** |
177 | * @ description: get current picture's average brightness |
178 | * @ return: 0~255,0 is darkest,255 is brightest |
179 | */ |
180 | int AutoBackLight::HistogramGet_AVE() |
181 | { |
182 | int hist_ave = 0; |
183 | tvin_parm_t vdinParam; |
184 | if (0 == myTvin->VDIN_GetVdinParam(&vdinParam)) { |
185 | if (vdinParam.pixel_sum != 0) { |
186 | hist_ave = vdinParam.luma_sum / vdinParam.pixel_sum; |
187 | LOGD("[hist_ave][%d].", hist_ave); |
188 | return hist_ave; |
189 | } |
190 | LOGE("vdinParam.pixel_sum is zero, so the value is infinity\n"); |
191 | return -1; |
192 | } |
193 | LOGE("VDIN_GetVdinParam get data error!!!\n"); |
194 | return -1; |
195 | } |
196 | |
197 | bool AutoBackLight::threadLoop() |
198 | { |
199 | int sleeptime = 50;//ms |
200 | int adjustBacklightCount = 0; |
201 | while ( mAutoBacklight_OnOff_Flag ) { |
202 | usleep ( sleeptime * 1000 ); |
203 | adjustBacklightCount++; |
204 | if (adjustBacklightCount == 24) { |
205 | adjustBacklightCount = 0; |
206 | adjustDstBacklight(); |
207 | } |
208 | adjustBacklight(); |
209 | } |
210 | |
211 | return false;//return true, run again, return false,not run. |
212 | } |
213 |