From 50cd363c01efab0ced53cfdeed2e9169fcfad7ac Mon Sep 17 00:00:00 2001 From: Niklas Haas Date: Thu, 19 Oct 2023 17:52:14 +0200 Subject: [PATCH] video/mp_image: handle non-power-of-two alignment Needed for odd formats like rgb24. --- common/common.h | 4 ++++ video/mp_image.c | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/common/common.h b/common/common.h index 4015f9b8c3..ccdd94bba9 100644 --- a/common/common.h +++ b/common/common.h @@ -39,6 +39,7 @@ #define MPSWAP(type, a, b) \ do { type SWAP_tmp = b; b = a; a = SWAP_tmp; } while (0) #define MP_ARRAY_SIZE(s) (sizeof(s) / sizeof((s)[0])) +#define MP_DIV_UP(x, y) (((x) + (y) - 1) / (y)) // align must be a power of two (align >= 1), x >= 0 #define MP_ALIGN_UP(x, align) (((x) + (align) - 1) & ~((align) - 1)) @@ -46,6 +47,9 @@ #define MP_IS_ALIGNED(x, align) (!((x) & ((align) - 1))) #define MP_IS_POWER_OF_2(x) ((x) > 0 && !((x) & ((x) - 1))) +// align to non power of two +#define MP_ALIGN_NPOT(x, align) ((align) ? MP_DIV_UP(x, align) * (align) : (x)) + // Return "a", or if that is NOPTS, return "def". #define MP_PTS_OR_DEF(a, def) ((a) == MP_NOPTS_VALUE ? (def) : (a)) // If one of the values is NOPTS, always pick the other one. diff --git a/video/mp_image.c b/video/mp_image.c index c3a6b50a78..5c4f30b686 100644 --- a/video/mp_image.c +++ b/video/mp_image.c @@ -69,7 +69,7 @@ static int mp_image_layout(int imgfmt, int w, int h, int stride_align, int alloc_w = mp_chroma_div_up(w, desc.xs[n]); int alloc_h = MP_ALIGN_UP(h, 32) >> desc.ys[n]; int line_bytes = (alloc_w * desc.bpp[n] + 7) / 8; - out_stride[n] = MP_ALIGN_UP(line_bytes, stride_align); + out_stride[n] = MP_ALIGN_NPOT(line_bytes, stride_align); out_plane_size[n] = out_stride[n] * alloc_h; } if (desc.flags & MP_IMGFLAG_PAL)