comparison libvo/gtf.c @ 7069:21e1ab99cb21

General Timing Formula algorithm from a scratch. vo_vesa.c so now adjust the timing to highest possible refresh rate using the monitor capabilities from a config file. patch by Rudolf Marek <MAREKR2@cs.felk.cvut.cz>
author arpi
date Thu, 22 Aug 2002 23:03:51 +0000
parents
children d701716bfbbc
comparison
equal deleted inserted replaced
7068:6c2d746b17bf 7069:21e1ab99cb21
1 /*
2 * Copyright (C) Rudolf Marek <r.marek@sh.cvut.cz> - Aug 2002
3 *
4 * You can redistribute this file under terms and conditions
5 * of GNU General Public licence v2.
6 *
7 * GTF calculations formulas are taken from GTF_V1R1.xls
8 * created by ANDY.MORRISH@NSC.COM
9 */
10
11 //Version 0.4
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <math.h>
15 #include "gtf.h"
16
17 #undef GTF_DEBUG
18
19 #ifdef GTF_DEBUG
20 #define DEBUG_PRINTF(a,b) printf(a,b);
21 #else
22 #define DEBUG_PRINTF(a,b)
23 #endif
24
25 static GTF_constants GTF_given_constants = { 3.0,550.0,1,8,1.8,8,40,20,128,600 };
26
27 static double round(double v)
28 {
29 return floor(v + 0.5);
30 }
31
32 static void GetRoundedConstants(GTF_constants *c)
33 {
34 c->Vsync_need = round(GTF_given_constants.Vsync_need);
35 c->min_Vsync_BP = GTF_given_constants.min_Vsync_BP;
36 c->min_front_porch = round(GTF_given_constants.min_front_porch);
37 c->char_cell_granularity = GTF_given_constants.char_cell_granularity;
38 c->margin_width = GTF_given_constants.margin_width;
39 c->sync_width = GTF_given_constants.sync_width;
40 c->c = ((GTF_given_constants.c - GTF_given_constants.j)*(GTF_given_constants.k / 256)) + GTF_given_constants.j;
41 c->j = GTF_given_constants.j;
42 c->k = GTF_given_constants.k;
43 c->m = (GTF_given_constants.k / 256) * GTF_given_constants.m;
44 }
45
46 void GTF_calcTimings(double X,double Y,double freq, int type,
47 int want_margins, int want_interlace,struct VesaCRTCInfoBlock *result )
48 {
49 GTF_constants c;
50 double RR, margin_top, margin_bottom, margin_left, margin_right;
51 double estimated_H_period,sync_plus_BP,BP,interlace,V_total_lines_field;
52 double estimated_V_field_rate,actual_H_period,actual_V_field_freq;
53 double total_active_pixels, ideal_duty_cycle, blanking_time, H_total_pixels;
54 double H_freq, pixel_freq,actual_V_frame_freq;
55 double H_sync_start, H_sync_end, H_back_porch, H_front_porch, H_sync_width;
56 double V_back_porch, V_front_porch, V_sync_start, V_sync_end,V_sync_width;
57 double ideal_H_period;
58 GetRoundedConstants(&c);
59
60
61 pixel_freq = RR = freq;
62
63 /* DETERMINE IF 1/2 LINE INTERLACE IS PRESENT */
64
65 interlace = 0;
66
67 if (want_interlace) {
68 RR = RR * 2;
69 Y=Y/2;
70 interlace = 0.5;
71 }
72
73 result->Flags = 0;
74
75 if ((Y==300)||(Y==200)||(Y==240))
76 {
77 Y*=2;
78 result->Flags = VESA_CRTC_DOUBLESCAN; /* TODO: check if mode support */
79 }
80
81 /* DETERMINE NUMBER OF LINES IN V MARGIN */
82 /* DETERMINE NUMBER OF PIXELS IN H MARGIN [pixels] */
83
84 margin_left = margin_right = 0;
85 margin_top = margin_bottom = 0;
86
87 if (want_margins) {
88 margin_top = margin_bottom = (c.margin_width / 100) * Y;
89 margin_left = round(( X* c.margin_width/100)/c.char_cell_granularity) \
90 * c.char_cell_granularity;
91 margin_right = margin_left;
92 DEBUG_PRINTF("margin_left_right : %f\n",margin_right)
93 DEBUG_PRINTF("margin_top_bottom : %f\n",margin_top)
94 }
95
96 /* FIND TOTAL NUMBER OF ACTIVE PIXELS (IMAGE + MARGIN) [pixels] */
97
98 total_active_pixels = margin_left + margin_right + X;
99 DEBUG_PRINTF("total_active_pixels: %f\n",total_active_pixels)
100
101 if (type == GTF_PF)
102 {
103 ideal_H_period = ((c.c-100)+(sqrt(((100-c.c)*(100-c.c) )+(0.4*c.m*(total_active_pixels + margin_left + margin_right) / freq))))/2/c.m*1000;
104
105 DEBUG_PRINTF("ideal_H_period: %f\n",ideal_H_period)
106
107 /* FIND IDEAL BLANKING DUTY CYCLE FROM FORMULA [%] */
108 ideal_duty_cycle = c.c - (c.m * ideal_H_period /1000);
109 DEBUG_PRINTF("ideal_duty_cycle: %f\n",ideal_duty_cycle)
110
111 /* FIND BLANKING TIME (TO NEAREST CHAR CELL) [pixels] */
112
113 blanking_time = round(total_active_pixels * ideal_duty_cycle \
114 / (100-ideal_duty_cycle) / (2*c.char_cell_granularity)) \
115 * (2*c.char_cell_granularity);
116 DEBUG_PRINTF("blanking_time : %f\n",blanking_time )
117
118 /* FIND TOTAL NUMBER OF PIXELS IN A LINE [pixels] */
119 H_total_pixels = total_active_pixels + blanking_time ;
120 DEBUG_PRINTF("H_total_pixels: %f\n",H_total_pixels)
121 H_freq = freq / H_total_pixels * 1000;
122 DEBUG_PRINTF("H_freq: %f\n",H_freq)
123 actual_H_period = 1000 / H_freq;
124 DEBUG_PRINTF("actual_H_period: %f\n",actual_H_period)
125 sync_plus_BP = round(H_freq * c.min_Vsync_BP/1000);
126 // sync_plus_BP = round( freq / H_total_pixels * c.min_Vsync_BP);
127
128 DEBUG_PRINTF("sync_plus_BP: %f\n",sync_plus_BP)
129
130 } else if (type == GTF_VF)
131 {
132
133 /* ESTIMATE HORIZ. PERIOD [us] */
134
135 estimated_H_period = (( 1/RR ) - c.min_Vsync_BP/1000000 ) / (Y + (2 * margin_top) + c.min_front_porch + interlace) * 1000000;
136
137 DEBUG_PRINTF("estimated_H_period: %f\n",estimated_H_period)
138
139 /* FIND NUMBER OF LINES IN (SYNC + BACK PORCH) [lines] */
140
141 sync_plus_BP = round( c.min_Vsync_BP / estimated_H_period );
142 DEBUG_PRINTF("sync_plus_BP: %f\n",sync_plus_BP)
143
144 } else if (type == GTF_HF)
145 {
146 sync_plus_BP = round(freq * c.min_Vsync_BP/1000);
147 DEBUG_PRINTF("sync_plus_BP: %f\n",sync_plus_BP)
148 }
149
150
151
152 /* FIND TOTAL NUMBER OF LINES IN VERTICAL FIELD */
153
154 V_total_lines_field = sync_plus_BP+interlace+margin_bottom+margin_top+Y+c.min_front_porch;
155 DEBUG_PRINTF("V_total_lines_field : %f\n",V_total_lines_field )
156
157 if (type == GTF_VF)
158 {
159 /* ESTIMATE VERTICAL FIELD RATE [hz] */
160
161 estimated_V_field_rate = 1 / estimated_H_period / V_total_lines_field * 1000000;
162 DEBUG_PRINTF(" estimated_V_field_rate: %f\n", estimated_V_field_rate)
163 /* FIND ACTUAL HORIZONTAL PERIOD [us] */
164
165 actual_H_period = estimated_H_period / (RR / estimated_V_field_rate);
166 DEBUG_PRINTF("actual_H_period: %f\n",actual_H_period)
167 /* FIND ACTUAL VERTICAL FIELD FREQUENCY [Hz] */
168
169 actual_V_field_freq = 1 / actual_H_period / V_total_lines_field * 1000000;
170 DEBUG_PRINTF("actual_V_field_freq: %f\n",actual_V_field_freq)
171
172 /* FIND IDEAL BLANKING DUTY CYCLE FROM FORMULA [%] */
173 ideal_duty_cycle = c.c - (c.m * actual_H_period /1000);
174 DEBUG_PRINTF("ideal_duty_cycle: %f\n",ideal_duty_cycle)
175 //if (type == GTF_VF)
176 //{
177 //moved
178 //}
179 } else if (type == GTF_HF)
180 {
181 /* FIND IDEAL BLANKING DUTY CYCLE FROM FORMULA [%] */
182 ideal_duty_cycle = c.c - (c.m / freq);
183 DEBUG_PRINTF("ideal_duty_cycle: %f\n",ideal_duty_cycle)
184 }
185
186 /* FIND BLANKING TIME (TO NEAREST CHAR CELL) [pixels] */
187
188 if (!(type == GTF_PF))
189 {
190 blanking_time = round(total_active_pixels * ideal_duty_cycle \
191 / (100-ideal_duty_cycle) / (2*c.char_cell_granularity)) \
192 * (2*c.char_cell_granularity);
193 DEBUG_PRINTF("blanking_time : %f\n",blanking_time )
194 }
195 else
196 // if (type == GTF_PF)
197 {
198 actual_V_field_freq = H_freq / V_total_lines_field * 1000;
199 }
200
201 if (type == GTF_HF)
202 {
203 /* Hz */
204 actual_V_field_freq = freq / V_total_lines_field * 1000;
205 DEBUG_PRINTF("actual_V_field_freq: %f\n",actual_V_field_freq)
206 }
207
208
209 actual_V_frame_freq = actual_V_field_freq;
210
211 /* FIND ACTUAL VERTICAL FRAME FREQUENCY [Hz]*/
212
213 if (want_interlace) actual_V_frame_freq = actual_V_field_freq / 2;
214 DEBUG_PRINTF("actual_V_frame_freq: %f\n",actual_V_frame_freq)
215
216 // V_freq = actual_V_frame_freq;
217 // DEBUG_PRINTF("V_freq %f\n",V_freq)
218
219
220 if (!(type == GTF_PF))
221 {
222 /* FIND TOTAL NUMBER OF PIXELS IN A LINE [pixels] */
223 H_total_pixels = total_active_pixels + blanking_time ;
224 DEBUG_PRINTF("H_total_pixels: %f\n",H_total_pixels)
225 if (type == GTF_VF)
226 {
227 /* FIND PIXEL FREQUENCY [Mhz] */
228 pixel_freq = H_total_pixels / actual_H_period ;
229 DEBUG_PRINTF("pixel_freq: %f\n",pixel_freq)
230 } else if (type == GTF_HF)
231 {
232 /* FIND PIXEL FREQUENCY [Mhz] */
233 pixel_freq = H_total_pixels * freq / 1000 ;
234 DEBUG_PRINTF("pixel_freq: %f\n",pixel_freq)
235 actual_H_period = 1000/freq;
236 }
237
238 /* FIND ACTUAL HORIZONTAL FREQUENCY [KHz] */
239
240 H_freq = 1000 / actual_H_period;
241 DEBUG_PRINTF("H_freq %f\n",H_freq)
242
243
244 }
245
246 /* FIND NUMBER OF LINES IN BACK PORCH [lines] */
247
248 BP = sync_plus_BP - c.Vsync_need;
249 DEBUG_PRINTF("BP: %f\n",BP)
250
251 /*------------------------------------------------------------------------------------------------*/
252 /* FIND H SYNC WIDTH (TO NEAREST CHAR CELL) */
253 H_sync_width = round(c.sync_width/100*H_total_pixels/c.char_cell_granularity)*c.char_cell_granularity;
254 DEBUG_PRINTF("H_sync_width %f\n",H_sync_width)
255
256 /* FIND FRONT H PORCH(TO NEAREST CHAR CELL) */
257 H_front_porch = (blanking_time/2) - H_sync_width;
258 DEBUG_PRINTF("H_front_porch %f\n",H_front_porch)
259 /* FIND BACK H PORCH(TO NEAREST CHAR CELL) */
260 H_back_porch = H_sync_width + H_front_porch;
261 DEBUG_PRINTF("H_back_porch%f\n",H_back_porch)
262
263 H_sync_start = H_total_pixels - (H_sync_width + H_back_porch);
264 DEBUG_PRINTF("H_sync_start %f\n",H_sync_start)
265 H_sync_end = H_total_pixels - H_back_porch;
266 DEBUG_PRINTF("H_sync_end %f\n",H_sync_end)
267
268 V_back_porch = interlace + BP;
269 DEBUG_PRINTF("V_back_porch%f\n",V_back_porch)
270 V_front_porch = interlace + c.min_front_porch;
271 DEBUG_PRINTF("V_front_porch%f\n",V_front_porch)
272
273 V_sync_width = c.Vsync_need;
274 V_sync_start = V_total_lines_field - (V_sync_width + V_back_porch);
275 DEBUG_PRINTF("V_sync_start %f\n",V_sync_start)
276 V_sync_end = V_total_lines_field - V_back_porch;
277 DEBUG_PRINTF("V_sync_end %f\n",V_sync_end)
278
279 result->hTotal = H_total_pixels;
280 result-> hSyncStart = H_sync_start; /* Horizontal sync start in pixels */
281 result-> hSyncEnd = H_sync_end; /* Horizontal sync end in pixels */
282 result-> vTotal= V_total_lines_field; /* Vertical total in lines */
283 result-> vSyncStart = V_sync_start; /* Vertical sync start in lines */
284 result-> vSyncEnd = V_sync_end; /* Vertical sync end in lines */
285 result-> Flags = (result->Flags)|VESA_CRTC_HSYNC_NEG; /* Flags (Interlaced, Double Scan etc) */
286
287 if (want_interlace)
288 {
289 result->Flags = (result->Flags) | VESA_CRTC_INTERLACED;
290 }
291
292 result-> PixelClock = pixel_freq*1000000; /* Pixel clock in units of Hz */
293 result-> RefreshRate = actual_V_frame_freq*100;/* Refresh rate in units of 0.01 Hz*/
294
295 }
296
297