From 00e766870b5fe749242aab2b452ab9220700bf47 Mon Sep 17 00:00:00 2001
From: Vito Caputo <vcaputo@pengaru.com>
Date: Thu, 9 Jul 2020 20:48:32 -0700
Subject: yuv_utils: gracefully ignore odd pixels for UV

This is temporary, in preparation for potentially odd-dimensioned
image buffers.

For now I'll just skip the last row / column in the UV update for such
odd dimensions.

In the future there could be a read-modify-write dance for handling the
last row/column or something like that.
---
 recordmydesktop/src/rmd_yuv_utils.c | 42 +++++++++++++++++++------------------
 1 file changed, 22 insertions(+), 20 deletions(-)

(limited to 'recordmydesktop/src')

diff --git a/recordmydesktop/src/rmd_yuv_utils.c b/recordmydesktop/src/rmd_yuv_utils.c
index 33cf2f6..f433b27 100644
--- a/recordmydesktop/src/rmd_yuv_utils.c
+++ b/recordmydesktop/src/rmd_yuv_utils.c
@@ -205,12 +205,13 @@ static inline int blocknum(int xv, int yv, int widthv, int blocksize)
 					*_vg = Vg, *_vb = Vb;					\
 	register u_int##__depth__##_t	*datapi = (u_int##__depth__##_t *)data,			\
 					*datapi_next = NULL;					\
+	int				w_odd = width_tm % 2, h_odd = height_tm % 2;		\
 												\
 	if (sampling == __PXL_AVERAGE)								\
 		datapi_next = datapi + width_tm;						\
 												\
-	for (int k = 0; k < height_tm; k += 2) {						\
-		for (int i = 0; i < width_tm; i += 2) {						\
+	for (int k = 0; k < height_tm - h_odd; k += 2) {					\
+		for (int i = 0; i < width_tm - w_odd; i += 2) {					\
 			UPDATE_A_UV_PIXEL(	yuv_U,						\
 						yuv_V,						\
 						t_val,						\
@@ -227,12 +228,12 @@ static inline int blocknum(int xv, int yv, int widthv, int blocksize)
 			yuv_V++;								\
 		}										\
 												\
-		yuv_U += ((yuv)->y_stride - width_tm) >> 1;					\
-		yuv_V += ((yuv)->y_stride - width_tm) >> 1;					\
+		yuv_U += ((yuv)->y_stride - (width_tm - w_odd * 2)) >> 1;			\
+		yuv_V += ((yuv)->y_stride - (width_tm - w_odd * 2)) >> 1;			\
 												\
-		datapi += width_tm;								\
+		datapi += width_tm + w_odd;							\
 		if (sampling == __PXL_AVERAGE)							\
-			datapi_next += width_tm;						\
+			datapi_next += width_tm + w_odd;					\
 	}											\
 }
 
@@ -290,13 +291,14 @@ static inline int blocknum(int xv, int yv, int widthv, int blocksize)
 					*datapi_next = NULL,					\
 					*datapi_back = (u_int##__depth__##_t *)data_back,	\
 					*datapi_back_next = NULL;				\
+	int				w_odd = width_tm % 2, h_odd = height_tm % 2;		\
 												\
 	if (sampling == __PXL_AVERAGE) {							\
 		datapi_next = datapi + width_tm;						\
 		datapi_back_next = datapi_back + width_tm;					\
 												\
-		for (int k = 0; k < height_tm; k += 2) {					\
-			for (int i = 0; i < width_tm; i += 2) {					\
+		for (int k = 0; k < height_tm - h_odd; k += 2) {				\
+			for (int i = 0; i < width_tm - w_odd; i += 2) {				\
 				if (	(*datapi != *datapi_back ||				\
 					(*(datapi + 1) != *(datapi_back + 1)) ||		\
 					(*datapi_next != *datapi_back_next) ||			\
@@ -324,17 +326,17 @@ static inline int blocknum(int xv, int yv, int widthv, int blocksize)
 				yuv_V++;							\
 			}									\
 												\
-			yuv_U += ((yuv)->y_stride - width_tm) >> 1;				\
-			yuv_V += ((yuv)->y_stride - width_tm) >> 1;				\
+			yuv_U += ((yuv)->y_stride - (width_tm - w_odd * 2)) >> 1;		\
+			yuv_V += ((yuv)->y_stride - (width_tm - w_odd * 2)) >> 1;		\
 												\
-			datapi += width_tm;							\
-			datapi_back += width_tm;						\
-			datapi_next += width_tm;						\
-			datapi_back_next += width_tm;						\
+			datapi += width_tm + w_odd;						\
+			datapi_back += width_tm + w_odd;					\
+			datapi_next += width_tm + w_odd;					\
+			datapi_back_next += width_tm + w_odd;					\
 		}										\
 	} else {										\
-		for (int k = 0; k < height_tm; k += 2) {					\
-			for (int i = 0; i < width_tm; i += 2) {					\
+		for (int k = 0; k < height_tm - h_odd; k += 2) {				\
+			for (int i = 0; i < width_tm - w_odd; i += 2) {				\
 				if ((*datapi != *datapi_back)) {				\
 					UPDATE_A_UV_PIXEL(	yuv_U,				\
 								yuv_V,				\
@@ -356,11 +358,11 @@ static inline int blocknum(int xv, int yv, int widthv, int blocksize)
 				yuv_V++;							\
 			}									\
 												\
-			yuv_U += ((yuv)->y_stride - width_tm) >> 1;				\
-			yuv_V += ((yuv)->y_stride - width_tm) >> 1;				\
+			yuv_U += ((yuv)->y_stride - (width_tm - w_odd * 2)) >> 1;		\
+			yuv_V += ((yuv)->y_stride - (width_tm - w_odd * 2)) >> 1;		\
 												\
-			datapi += width_tm;							\
-			datapi_back += width_tm;						\
+			datapi += width_tm + w_odd;						\
+			datapi_back += width_tm + w_odd;					\
 		}										\
 	}											\
 }
-- 
cgit v1.2.3