From b3495d9ccf5ad1d5d84284affed1083eee5887ee Mon Sep 17 00:00:00 2001
From: wm4 <wm4@nowhere>
Date: Fri, 10 Apr 2015 20:58:26 +0200
Subject: [PATCH] mp_image: remove redundant plane_w/h fields

Seems relatively painful in this case, but they are morally wrong.
---
 video/filter/vf_mirror.c |  5 +++--
 video/mp_image.c         | 32 +++++++++++++++++++++-----------
 video/mp_image.h         |  6 ++----
 video/out/gl_video.c     |  9 +++++----
 4 files changed, 31 insertions(+), 21 deletions(-)

diff --git a/video/filter/vf_mirror.c b/video/filter/vf_mirror.c
index 90316b2be9..3b4138c535 100644
--- a/video/filter/vf_mirror.c
+++ b/video/filter/vf_mirror.c
@@ -63,10 +63,11 @@ static struct mp_image *filter(struct vf_instance *vf, struct mp_image *mpi)
     mp_image_copy_attributes(dmpi, mpi);
 
     for (int p = 0; p < mpi->num_planes; p++) {
-        for (int y = 0; y < mpi->plane_h[p]; y++) {
+        int plane_h = mp_image_plane_h(mpi, p);
+        for (int y = 0; y < plane_h; y++) {
             void *p_src = mpi->planes[p] + mpi->stride[p] * y;
             void *p_dst = dmpi->planes[p] + dmpi->stride[p] * y;
-            int w = dmpi->plane_w[p];
+            int w = mp_image_plane_w(dmpi, p);
             if (mpi->imgfmt == IMGFMT_YUYV) {
                 mirror_4_m(p_dst, p_src, w / 2, 2, 1, 0, 3);
             } else if (mpi->imgfmt == IMGFMT_UYVY) {
diff --git a/video/mp_image.c b/video/mp_image.c
index aa0bb5cf25..2c1e01cef9 100644
--- a/video/mp_image.c
+++ b/video/mp_image.c
@@ -119,7 +119,7 @@ static bool mp_image_alloc_planes(struct mp_image *mpi)
     size_t plane_size[MP_MAX_PLANES];
     for (int n = 0; n < MP_MAX_PLANES; n++) {
         int alloc_h = MP_ALIGN_UP(mpi->h, 32) >> mpi->fmt.ys[n];
-        int line_bytes = (mpi->plane_w[n] * mpi->fmt.bpp[n] + 7) / 8;
+        int line_bytes = (mp_image_plane_w(mpi, n) * mpi->fmt.bpp[n] + 7) / 8;
         mpi->stride[n] = FFALIGN(line_bytes, SWS_MIN_BYTE_ALIGN);
         plane_size[n] = mpi->stride[n] * alloc_h;
     }
@@ -165,16 +165,24 @@ int mp_chroma_div_up(int size, int shift)
     return (size + (1 << shift) - 1) >> shift;
 }
 
+// Return the storage width in pixels of the given plane.
+int mp_image_plane_w(struct mp_image *mpi, int plane)
+{
+    return mp_chroma_div_up(mpi->w, mpi->fmt.xs[plane]);
+}
+
+// Return the storage height in pixels of the given plane.
+int mp_image_plane_h(struct mp_image *mpi, int plane)
+{
+    return mp_chroma_div_up(mpi->h, mpi->fmt.ys[plane]);
+}
+
 // Caller has to make sure this doesn't exceed the allocated plane data/strides.
 void mp_image_set_size(struct mp_image *mpi, int w, int h)
 {
     assert(w >= 0 && h >= 0);
     mpi->w = mpi->params.w = mpi->params.d_w = w;
     mpi->h = mpi->params.h = mpi->params.d_h = h;
-    for (int n = 0; n < mpi->num_planes; n++) {
-        mpi->plane_w[n] = mp_chroma_div_up(mpi->w, mpi->fmt.xs[n]);
-        mpi->plane_h[n] = mp_chroma_div_up(mpi->h, mpi->fmt.ys[n]);
-    }
 }
 
 void mp_image_set_params(struct mp_image *image,
@@ -328,8 +336,9 @@ void mp_image_copy(struct mp_image *dst, struct mp_image *src)
     assert(dst->w == src->w && dst->h == src->h);
     assert(mp_image_is_writeable(dst));
     for (int n = 0; n < dst->num_planes; n++) {
-        int line_bytes = (dst->plane_w[n] * dst->fmt.bpp[n] + 7) / 8;
-        memcpy_pic(dst->planes[n], src->planes[n], line_bytes, dst->plane_h[n],
+        int line_bytes = (mp_image_plane_w(dst, n) * dst->fmt.bpp[n] + 7) / 8;
+        int plane_h = mp_image_plane_h(dst, n);
+        memcpy_pic(dst->planes[n], src->planes[n], line_bytes, plane_h,
                    dst->stride[n], src->stride[n]);
     }
     // Watch out for AV_PIX_FMT_FLAG_PSEUDOPAL retardation
@@ -417,13 +426,13 @@ void mp_image_clear(struct mp_image *img, int x0, int y0, int x1, int y1)
 
     for (int p = 0; p < area.num_planes; p++) {
         int bpp = area.fmt.bpp[p];
-        int bytes = (area.plane_w[p] * bpp + 7) / 8;
+        int bytes = (mp_image_plane_w(&area, p) * bpp + 7) / 8;
         if (bpp <= 8) {
             memset_pic(area.planes[p], plane_clear[p], bytes,
-                       area.plane_h[p], area.stride[p]);
+                       mp_image_plane_h(&area, p), area.stride[p]);
         } else {
             memset16_pic(area.planes[p], plane_clear[p], (bytes + 1) / 2,
-                         area.plane_h[p], area.stride[p]);
+                         mp_image_plane_h(&area, p), area.stride[p]);
         }
     }
 }
@@ -431,7 +440,8 @@ void mp_image_clear(struct mp_image *img, int x0, int y0, int x1, int y1)
 void mp_image_vflip(struct mp_image *img)
 {
     for (int p = 0; p < img->num_planes; p++) {
-        img->planes[p] = img->planes[p] + img->stride[p] * (img->plane_h[p] - 1);
+        int plane_h = mp_image_plane_h(img, p);
+        img->planes[p] = img->planes[p] + img->stride[p] * (plane_h - 1);
         img->stride[p] = -img->stride[p];
     }
 }
diff --git a/video/mp_image.h b/video/mp_image.h
index 16862f9882..95fe8ad20e 100644
--- a/video/mp_image.h
+++ b/video/mp_image.h
@@ -95,10 +95,6 @@ typedef struct mp_image {
     int pict_type; // 0->unknown, 1->I, 2->P, 3->B
     int fields;
 
-    /* redundant */
-    int plane_w[MP_MAX_PLANES];
-    int plane_h[MP_MAX_PLANES];
-
     /* only inside filter chain */
     double pts;
     /* memory management */
@@ -125,6 +121,8 @@ void mp_image_crop_rc(struct mp_image *img, struct mp_rect rc);
 void mp_image_vflip(struct mp_image *img);
 
 void mp_image_set_size(struct mp_image *mpi, int w, int h);
+int mp_image_plane_w(struct mp_image *mpi, int plane);
+int mp_image_plane_h(struct mp_image *mpi, int plane);
 
 void mp_image_setfmt(mp_image_t* mpi, int out_fmt);
 void mp_image_steal_data(struct mp_image *dst, struct mp_image *src);
diff --git a/video/out/gl_video.c b/video/out/gl_video.c
index b61d27aeee..3e915d9d0a 100644
--- a/video/out/gl_video.c
+++ b/video/out/gl_video.c
@@ -2086,8 +2086,8 @@ static bool get_image(struct gl_video *p, struct mp_image *mpi)
 
     for (int n = 0; n < p->plane_count; n++) {
         struct texplane *plane = &vimg->planes[n];
-        mpi->stride[n] = mpi->plane_w[n] * p->image_desc.bytes[n];
-        int needed_size = mpi->plane_h[n] * mpi->stride[n];
+        mpi->stride[n] = mp_image_plane_w(mpi, n) * p->image_desc.bytes[n];
+        int needed_size = mp_image_plane_h(mpi, n) * mpi->stride[n];
         if (!plane->gl_buffer)
             gl->GenBuffers(1, &plane->gl_buffer);
         gl->BindBuffer(GL_PIXEL_UNPACK_BUFFER, plane->gl_buffer);
@@ -2132,8 +2132,9 @@ void gl_video_upload_image(struct gl_video *p, struct mp_image *mpi)
     bool pbo = false;
     if (!vimg->planes[0].buffer_ptr && get_image(p, &mpi2)) {
         for (int n = 0; n < p->plane_count; n++) {
-            int line_bytes = mpi->plane_w[n] * p->image_desc.bytes[n];
-            memcpy_pic(mpi2.planes[n], mpi->planes[n], line_bytes, mpi->plane_h[n],
+            int line_bytes = mp_image_plane_w(mpi, n) * p->image_desc.bytes[n];
+            int plane_h = mp_image_plane_h(mpi, n);
+            memcpy_pic(mpi2.planes[n], mpi->planes[n], line_bytes, plane_h,
                        mpi2.stride[n], mpi->stride[n]);
         }
         pbo = true;