Skip to contentSkip to conversation

fix for rounding errors in cvt/gtf calculation

PATCH New Prashant Laddha 1 participant 5 messages Apr 23, 2015 Media (V4L/DVB)

lore
Share & tools
lore
#0 lore raw

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

#1 lore raw

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.

Cc Hans Verkuil <[email protected]>
Cc Martin Bugge <[email protected]>

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
index c0e9638..32aa25f 100644
@@ -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
#3 lore raw

PATCH v2 3/4 v4l2-dv-timings: add sanity checks in cvt,gtf calculations

In reply to Prashant Laddha

Wrong values of hfreq and image height can lead to strange timings. Avoid timing calculations for such values.

Suggested By: Martin Bugge <[email protected]>

Cc Hans Verkuil <[email protected]>
Cc 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
index 16c8ac5..4e09792 100644
@@ -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
#2 lore raw

PATCH v2 2/4 v4l2-dv-timings: fix rounding in hblank and hsync calculation

In reply to Prashant Laddha

Changed 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.

Cc Hans Verkuil <[email protected]>
Cc Martin Bugge <[email protected]>
---
 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
index 32aa25f..16c8ac5 100644
@@ -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
#4 lore raw

PATCH v2 4/4 v4l2-dv-timings: replace hsync magic number with a macro

In reply to Prashant Laddha

This change will not change timing calculation. In CVT generator spreadsheet the nominal value of hsync (as a percentage of line) is 8 percent.

Cc Hans Verkuil <[email protected]>
Cc Martin Bugge <[email protected]>
---
 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
index 4e09792..37f0d6f 100644
@@ -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