fix for rounding errors in cvt/gtf calculation
This is second version of patch series towards fixing rounding errors in cvt,gtf timing calculations.
In version 1, the fixes for rounding errors have added on top of patches meant for interlaced support. Based on the feedback from Hans, I have now (in v2), reworked the patches such that, patches for rounding fixes will go first.
Also, based on suggestion from Martin, added sanity checks in v2.
Please review following patches and share your comments.
[PATCH v2 1/4] v4l2-dv-timings: fix rounding error in vsync_bp [PATCH v2 2/4] v4l2-dv-timings: fix rounding in hblank and hsync [PATCH v2 3/4] v4l2-dv-timings: add sanity checks in cvt,gtf [PATCH v2 4/4] v4l2-dv-timings: replace hsync magic number with a
Thanks to Martin for helping me to validate the rounding fixes.
Regards,
Prashant
PATCH v2 1/4 v4l2-dv-timings: fix rounding error in vsync_bp calculation
Changed the rounding offsets used in vsync_bp calculation in cvt and gtf timings. The results for vsync_bp should now match with results from timing generator spreadsheets for cvt and gtf standards.
In the vsync_bp calculation for cvt, always round down the value of (CVT_MIN_VSYNC_BP / h_period_est) and then add 1. It thus, reflects the equation used in timing generator spreadsheet. Using 1999999 as rounding offset, could pontentially lead to bumping up the vsync_bp value by extra 1.
In the vsync_bp calculations for gtf, instead of round up or round down, round the (CVT_MIN_VSYNC_BP / h_period_est) to the nearest integer.
Thanks to Martin Bugge <[email protected]> for validating with standards and suggestions on equations.
--- drivers/media/v4l2-core/v4l2-dv-timings.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/media/v4l2-core/v4l2-dv-timings.c +4 -3
@@ -368,14 +368,14 @@ bool v4l2_detect_cvt(unsigned frame_height, unsigned hfreq, unsigned vsync,/* Vertical */ if (reduced_blanking) { v_fp =CVT_RB_V_FPORCH ; - v_bp = (CVT_RB_MIN_V_BLANK * hfreq +1999999 ) /1000000 ; + v_bp = (CVT_RB_MIN_V_BLANK * hfreq) /1000000 +1 ; v_bp -= vsync + v_fp;if (v_bp <CVT_RB_MIN_V_BPORCH ) v_bp =CVT_RB_MIN_V_BPORCH ; }else { v_fp =CVT_MIN_V_PORCH_RND ; - v_bp = (CVT_MIN_VSYNC_BP * hfreq +1999999 ) /1000000 - vsync; + v_bp = (CVT_MIN_VSYNC_BP * hfreq) /1000000 +1 - vsync;if (v_bp <CVT_MIN_V_BPORCH ) v_bp =CVT_MIN_V_BPORCH ;
@@ -529,7 +529,8 @@ bool v4l2_detect_gtf(unsigned frame_height,/* Vertical */ v_fp =GTF_V_FP ; - v_bp = (GTF_MIN_VSYNC_BP * hfreq +999999 ) /1000000 - vsync; + + v_bp = (GTF_MIN_VSYNC_BP * hfreq +500000 ) /1000000 - vsync; image_height = (frame_height - v_fp - vsync - v_bp +1 ) & ~0x1 ;if (aspect.numerator ==0 || aspect.denominator ==0 ) {
Signature · 1 line
1.9.1
PATCH v2 3/4 v4l2-dv-timings: add sanity checks in cvt,gtf calculations
In reply to Prashant LaddhaWrong values of hfreq and image height can lead to strange timings. Avoid timing calculations for such values.
Suggested By: Martin Bugge <[email protected]>
--- drivers/media/v4l2-core/v4l2-dv-timings.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
drivers/media/v4l2-core/v4l2-dv-timings.c +12 -0
@@ -365,6 +365,9 @@ bool v4l2_detect_cvt(unsigned frame_height, unsigned hfreq, unsigned vsync,else return false; +if (hfreq ==0 ) +return false; +/* Vertical */ if (reduced_blanking) { v_fp =CVT_RB_V_FPORCH ;
@@ -382,6 +385,9 @@ bool v4l2_detect_cvt(unsigned frame_height, unsigned hfreq, unsigned vsync, } image_height = (frame_height - v_fp - vsync - v_bp +1 ) & ~0x1 ; +if (image_height <0 ) +return false; +/* Aspect ratio based on vsync */ switch (vsync) {case 4 :
@@ -527,12 +533,18 @@ bool v4l2_detect_gtf(unsigned frame_height,else return false; +if (hfreq ==0 ) +return false; +/* Vertical */ v_fp =GTF_V_FP ; v_bp = (GTF_MIN_VSYNC_BP * hfreq +500000 ) /1000000 - vsync; image_height = (frame_height - v_fp - vsync - v_bp +1 ) & ~0x1 ; +if (image_height <0 ) +return false; +if (aspect.numerator ==0 || aspect.denominator ==0 ) { aspect.numerator =16 ; aspect.denominator =9 ;
Signature · 1 line
1.9.1
PATCH v2 2/4 v4l2-dv-timings: fix rounding in hblank and hsync calculation
In reply to Prashant LaddhaChanged the rounding calculation for hblank and hsync to match it to equations in cvt and gtf standards.
In cvt calculation, hsync needs to be rounded down.
In gtf calculations, hblank needs to be rounded to nearest multiple of twice the cell granularity and hsync needs to be rounded to the nearest multiple of cell granularity.
--- drivers/media/v4l2-core/v4l2-dv-timings.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
drivers/media/v4l2-core/v4l2-dv-timings.c +5 -4
@@ -436,8 +436,8 @@ bool v4l2_detect_cvt(unsigned frame_height, unsigned hfreq, unsigned vsync, h_bp = h_blank /2 ; frame_width = image_width + h_blank; - hsync = (frame_width *8 +50 ) /100 ; - hsync = hsync - hsync %CVT_CELL_GRAN ; + hsync = frame_width *8 /100 ; + hsync = (hsync /CVT_CELL_GRAN ) *CVT_CELL_GRAN ; h_fp = h_blank - hsync - h_bp; }
@@ -552,14 +552,15 @@ bool v4l2_detect_gtf(unsigned frame_height, (hfreq * (100 - GTF_S_C_PRIME) + GTF_S_M_PRIME * 1000) / 2) / (hfreq * (100 -GTF_S_C_PRIME ) +GTF_S_M_PRIME *1000 ); - h_blank = h_blank - h_blank % (2 *GTF_CELL_GRAN ); + h_blank = ((h_blank +GTF_CELL_GRAN ) / (2 *GTF_CELL_GRAN )) * + (2 *GTF_CELL_GRAN ); frame_width = image_width + h_blank; pix_clk = (image_width + h_blank) * hfreq; pix_clk = pix_clk /GTF_PXL_CLK_GRAN *GTF_PXL_CLK_GRAN ; hsync = (frame_width *8 +50 ) /100 ; - hsync = hsync - hsync %GTF_CELL_GRAN ; + hsync = ((hsync +GTF_CELL_GRAN /2 ) /GTF_CELL_GRAN ) *GTF_CELL_GRAN ; h_fp = h_blank /2 - hsync;
Signature · 1 line
1.9.1
PATCH v2 4/4 v4l2-dv-timings: replace hsync magic number with a macro
In reply to Prashant LaddhaThis change will not change timing calculation. In CVT generator spreadsheet the nominal value of hsync (as a percentage of line) is 8 percent.
--- drivers/media/v4l2-core/v4l2-dv-timings.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/media/v4l2-core/v4l2-dv-timings.c +2 -1
@@ -313,6 +313,7 @@ EXPORT_SYMBOL_GPL(v4l2_print_dv_timings);#define CVT_MIN_V_BPORCH 7/* lines */ #define CVT_MIN_V_PORCH_RND 3/* lines */ #define CVT_MIN_VSYNC_BP 550/* min time of vsync + back porch (us) */ +#define CVT_HSYNC_PERCENT 8/* nominal hsync as percentage of line */ /* Normal blanking for CVT uses GTF to calculate horizontal blanking */ #define CVT_CELL_GRAN 8/* character cell granularity */
@@ -442,7 +443,7 @@ bool v4l2_detect_cvt(unsigned frame_height, unsigned hfreq, unsigned vsync, h_bp = h_blank /2 ; frame_width = image_width + h_blank; - hsync = frame_width *8 /100 ; + hsync = frame_width *CVT_HSYNC_PERCENT /100 ; hsync = (hsync /CVT_CELL_GRAN ) *CVT_CELL_GRAN ; h_fp = h_blank - hsync - h_bp; }
Signature · 1 line
1.9.1