summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorwm4 <wm4@nowhere>2020-05-02 23:48:43 +0200
committerwm4 <wm4@nowhere>2020-05-06 15:27:25 +0200
commitb2720dba0f7427cf23be889f7c87ab25d9c6da85 (patch)
tree2088429e75809deb073926817877bf01544b0b5e
parent349eb0b7fcdcedc8061979443dd73bfa032118d0 (diff)
downloadmpv-b2720dba0f7427cf23be889f7c87ab25d9c6da85.tar.bz2
mpv-b2720dba0f7427cf23be889f7c87ab25d9c6da85.tar.xz
mp_image: add some helpers
This is really basic for planar image data access; not sure why there weren't such helpers before. They also handle trickier formats that use bit-packing, or they would be mich simpler. (This affects only BGR4/BGR4/MONOW/MONOH, I hope whoever invented them is proud of triggering so many special cases for so little gain.)
-rw-r--r--video/mp_image.c20
-rw-r--r--video/mp_image.h3
2 files changed, 23 insertions, 0 deletions
diff --git a/video/mp_image.c b/video/mp_image.c
index dba787452e..cd0fe17713 100644
--- a/video/mp_image.c
+++ b/video/mp_image.c
@@ -1041,3 +1041,23 @@ void memset16_pic(void *dst, int fill, int unitsPerLine, int height, int stride)
}
}
}
+
+// Pixel at the given luma position on the given plane, possibly rounded down.
+void *mp_image_pixel_ptr(struct mp_image *img, int plane, int x, int y)
+{
+ return img->planes[plane] +
+ img->stride[plane] * (ptrdiff_t)(y >> img->fmt.ys[plane]) +
+ (size_t)(x >> img->fmt.xs[plane]) * img->fmt.bpp[plane] / 8;
+}
+
+// Number of bytes for w pixels, using luma pixels, possibly rounded up.
+// x0 is the start pixel; matters if the start pixel is rounded down.
+// (E.g. 8 bpp, x0=7, w=7 => pixels 0..15 => 2 bytes)
+size_t mp_image_plane_bytes(struct mp_image *img, int plane, int x0, int w)
+{
+ int bpp = img->fmt.bpp[plane];
+ int xs = img->fmt.xs[plane];
+ size_t b_x0 = (x0 >> xs) * bpp / 8;
+ size_t b_x1 = (((x0 + w + (1 << xs) - 1) >> xs) * bpp + 7) / 8;
+ return b_x1 - b_x0;
+}
diff --git a/video/mp_image.h b/video/mp_image.h
index e51022dd5c..f111d35129 100644
--- a/video/mp_image.h
+++ b/video/mp_image.h
@@ -185,4 +185,7 @@ void memcpy_pic(void *dst, const void *src, int bytesPerLine, int height,
void memset_pic(void *dst, int fill, int bytesPerLine, int height, int stride);
void memset16_pic(void *dst, int fill, int unitsPerLine, int height, int stride);
+void *mp_image_pixel_ptr(struct mp_image *img, int plane, int x, int y);
+size_t mp_image_plane_bytes(struct mp_image *img, int plane, int x0, int w);
+
#endif /* MPLAYER_MP_IMAGE_H */