All of lore.kernel.org
 help / color / mirror / Atom feed
From: Prashant Laddha <prladdha@cisco.com>
To: linux-media@vger.kernel.org
Cc: Hans Verkuil <hans.verkuil@cisco.com>,
	Prashant Laddha <prladdha@cisco.com>
Subject: [PATCH 2/2] v4l2-utils: fix overflow in cvt, gtf calculations
Date: Mon,  4 May 2015 16:18:59 +0530	[thread overview]
Message-ID: <1430736539-28469-3-git-send-email-prladdha@cisco.com> (raw)
In-Reply-To: <1430736539-28469-1-git-send-email-prladdha@cisco.com>

Some of the intermediate calculations can exceed 32 bit signed range,
especially for higher resolutions and refresh rates. Type casting the
intermediate values to higher precision to avoid overflow.

Cc: Hans Verkuil <hans.verkuil@cisco.com>
Signed-off-by: Prashant Laddha <prladdha@cisco.com>
---
 utils/v4l2-ctl/v4l2-ctl-modes.cpp | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)

diff --git a/utils/v4l2-ctl/v4l2-ctl-modes.cpp b/utils/v4l2-ctl/v4l2-ctl-modes.cpp
index 4689006..072763a 100644
--- a/utils/v4l2-ctl/v4l2-ctl-modes.cpp
+++ b/utils/v4l2-ctl/v4l2-ctl-modes.cpp
@@ -216,7 +216,7 @@ bool calc_cvt_modeline(int image_width, int image_height,
 		if (ideal_blank_duty_cycle < 20 * HV_FACTOR)
 			ideal_blank_duty_cycle = 20 * HV_FACTOR;
 
-		h_blank = active_h_pixel * ideal_blank_duty_cycle /
+		h_blank = active_h_pixel * (long long)ideal_blank_duty_cycle /
 			 (100 * HV_FACTOR - ideal_blank_duty_cycle);
 		h_blank -= h_blank % (2 * CVT_CELL_GRAN);
 
@@ -430,7 +430,6 @@ bool calc_gtf_modeline(int image_width, int image_height,
 	tmp2 = active_v_lines + GTF_MIN_PORCH + interlace;
 
 	h_period_est = tmp1 / (tmp2 * v_refresh);
-
 	v_sync_bp = GTF_MIN_VSYNC_BP * HV_FACTOR * 100 / h_period_est;
 	v_sync_bp = (v_sync_bp + 50) / 100;
 
@@ -444,7 +443,7 @@ bool calc_gtf_modeline(int image_width, int image_height,
 	v_refresh_est = (HV_FACTOR * (long long)1000000) /
 			(h_period_est * total_v_lines / HV_FACTOR);
 
-	h_period = (h_period_est * v_refresh_est) /
+	h_period = ((long long)h_period_est * v_refresh_est) /
 		   (v_refresh * HV_FACTOR);
 
 	if (!reduced_blanking)
@@ -455,7 +454,7 @@ bool calc_gtf_modeline(int image_width, int image_height,
 				      GTF_S_M_PRIME * h_period / 1000;
 
 
-	h_blank = active_h_pixel * ideal_blank_duty_cycle /
+	h_blank = active_h_pixel * (long long)ideal_blank_duty_cycle /
 			 (100 * HV_FACTOR - ideal_blank_duty_cycle);
 	h_blank = ((h_blank + GTF_CELL_GRAN) / (2 * GTF_CELL_GRAN))
 			  * (2 * GTF_CELL_GRAN);
@@ -467,7 +466,6 @@ bool calc_gtf_modeline(int image_width, int image_height,
 
 	h_fp = h_blank / 2 - h_sync;
 	h_bp = h_fp + h_sync;
-
 	pixel_clock = ((long long)total_h_pixel * HV_FACTOR * 1000000)
 					/ h_period;
 	/* Not sure if clock value needs to be truncated to multiple
-- 
1.9.1


      parent reply	other threads:[~2015-05-04 11:18 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-05-04 10:48 [PATCH 0/2] rounding and overflow fix in cvt,gtf calculations Prashant Laddha
2015-05-04 10:48 ` [PATCH 1/2] v4l2-ctl-modes: fix hblank, hsync rounding in gtf calculation Prashant Laddha
2015-05-04 10:48 ` Prashant Laddha [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1430736539-28469-3-git-send-email-prladdha@cisco.com \
    --to=prladdha@cisco.com \
    --cc=hans.verkuil@cisco.com \
    --cc=linux-media@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.