diff mbox series

[FFmpeg-devel,v4,4/6] lavu/display: Add horizontal and vertical scaling to the display matrix

Message ID 20220919094604.4645-5-thilo.borgmann@mail.de
State New
Headers show
Series Add display_matrix option | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 success Make fate finished
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Thilo Borgmann Sept. 19, 2022, 9:46 a.m. UTC
---
 doc/APIchanges      |  3 +++
 libavutil/display.c | 35 +++++++++++++++++++++++++++++++++++
 libavutil/display.h | 26 ++++++++++++++++++++++++++
 libavutil/version.h |  2 +-
 4 files changed, 65 insertions(+), 1 deletion(-)

Comments

Andreas Rheinhardt Sept. 19, 2022, 1:37 p.m. UTC | #1
Thilo Borgmann:
> ---
>  doc/APIchanges      |  3 +++
>  libavutil/display.c | 35 +++++++++++++++++++++++++++++++++++
>  libavutil/display.h | 26 ++++++++++++++++++++++++++
>  libavutil/version.h |  2 +-
>  4 files changed, 65 insertions(+), 1 deletion(-)
> 
> diff --git a/doc/APIchanges b/doc/APIchanges
> index e4b2fc8799..9dc393d999 100644
> --- a/doc/APIchanges
> +++ b/doc/APIchanges
> @@ -14,6 +14,9 @@ libavutil:     2021-04-27
>  
>  API changes, most recent first:
>  
> +2022-09-19 - xxxxxxxxxx - lavu 57.38.100 - display.h
> +  Add av_display_matrix_scale(), av_display_{h,v}scale_get()
> +
>  2022-09-19 - xxxxxxxxxx - lavu 57.37.100 - opt.h
>    Add AV_OPT_FLAG_ARGUMENT.
>    Add av_arg_show() to print arguments to options.
> diff --git a/libavutil/display.c b/libavutil/display.c
> index d31061283c..434ed50104 100644
> --- a/libavutil/display.c
> +++ b/libavutil/display.c
> @@ -48,6 +48,30 @@ double av_display_rotation_get(const int32_t matrix[9])
>      return -rotation;
>  }
>  
> +double av_display_hscale_get(const int32_t matrix[9])
> +{
> +    double scale;
> +
> +    scale = hypot(CONV_FP(matrix[0]), CONV_FP(matrix[1]));
> +
> +    if (scale == 0.0)
> +        return NAN;
> +
> +    return scale;
> +}
> +
> +double av_display_vscale_get(const int32_t matrix[9])
> +{
> +    double scale;
> +
> +    scale = hypot(CONV_FP(matrix[3]), CONV_FP(matrix[4]));
> +
> +    if (scale == 0.0)
> +        return NAN;
> +
> +    return scale;
> +}

Did I not already tell you that an arbitrary displaymatrix doesn't have
well-defined horizontal and vertical scaling factors? Apart from that,
the formulae are wrong even for the case where it has one, because you
forgot the scaling given by what is called z in display.h.
(Apart from that, these functions do not return the horizontal/vertical
scaling factors; they return the length of the image of the
horizontal/vertical unit vector under the transformation, but these
vectors need not (and in general: will not) point into the
horizontal/vertical direction at all; the return value is therefore not
the "hor/ver scaling by which the transformation matrix scales the frame
in the hor/ver direction", although the documentation claims that. The
reason for this is of course that an arbitrary matrix doesn't have
well-defined scaling factors.)

> +
>  void av_display_rotation_set(int32_t matrix[9], double angle)
>  {
>      double radians = -angle * M_PI / 180.0f;
> @@ -72,3 +96,14 @@ void av_display_matrix_flip(int32_t matrix[9], int hflip, int vflip)
>          for (i = 0; i < 9; i++)
>              matrix[i] *= flip[i % 3];
>  }
> +
> +void av_display_matrix_scale(int32_t matrix[9], double hscale, double vscale)
> +{
> +    // hscale
> +    matrix[0] = CONV_DB(CONV_FP(matrix[0]) * hscale);
> +    matrix[1] = CONV_DB(CONV_FP(matrix[1]) * hscale);
> +
> +    // vscale
> +    matrix[3] = CONV_DB(CONV_FP(matrix[3]) * vscale);
> +    matrix[4] = CONV_DB(CONV_FP(matrix[4]) * vscale);
> +}
> diff --git a/libavutil/display.h b/libavutil/display.h
> index 31d8bef361..ef507eb521 100644
> --- a/libavutil/display.h
> +++ b/libavutil/display.h
> @@ -86,6 +86,24 @@
>   */
>  double av_display_rotation_get(const int32_t matrix[9]);
>  
> +/**
> + * Extract the horizontal scaling component of the transformation matrix.
> + *
> + * @param matrix the transformation matrix
> + * @return the horizontal scaling by which the transformation matrix scales the frame
> + *         in the horizontal direction.
> + */
> +double av_display_hscale_get(const int32_t matrix[9]);
> +
> +/**
> + * Extract the vertical scaling component of the transformation matrix.
> + *
> + * @param matrix the transformation matrix
> + * @return the vertical scaling by which the transformation matrix scales the frame
> + *         in the vertical direction.
> + */
> +double av_display_vscale_get(const int32_t matrix[9]);
> +
>  /**
>   * Initialize a transformation matrix describing a pure clockwise
>   * rotation by the specified angle (in degrees).
> @@ -105,6 +123,14 @@ void av_display_rotation_set(int32_t matrix[9], double angle);
>   */
>  void av_display_matrix_flip(int32_t matrix[9], int hflip, int vflip);
>  
> +/**
> + * Scale the input matrix horizontally and/or vertically.
> + *
> + * @param matrix an allocated transformation matrix
> + * @param hscale scale factor by which the matrix should be scaled horizontally
> + * @param vscale scale factor by which the matrix should be scaled vertically
> + */
> +void av_display_matrix_scale(int32_t matrix[9], double hscale, double vscale);
>  /**
>   * @}
>   * @}
> diff --git a/libavutil/version.h b/libavutil/version.h
> index 9c44cef6aa..5aca550f45 100644
> --- a/libavutil/version.h
> +++ b/libavutil/version.h
> @@ -79,7 +79,7 @@
>   */
>  
>  #define LIBAVUTIL_VERSION_MAJOR  57
> -#define LIBAVUTIL_VERSION_MINOR  37
> +#define LIBAVUTIL_VERSION_MINOR  38
>  #define LIBAVUTIL_VERSION_MICRO 100
>  
>  #define LIBAVUTIL_VERSION_INT   AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \
diff mbox series

Patch

diff --git a/doc/APIchanges b/doc/APIchanges
index e4b2fc8799..9dc393d999 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -14,6 +14,9 @@  libavutil:     2021-04-27
 
 API changes, most recent first:
 
+2022-09-19 - xxxxxxxxxx - lavu 57.38.100 - display.h
+  Add av_display_matrix_scale(), av_display_{h,v}scale_get()
+
 2022-09-19 - xxxxxxxxxx - lavu 57.37.100 - opt.h
   Add AV_OPT_FLAG_ARGUMENT.
   Add av_arg_show() to print arguments to options.
diff --git a/libavutil/display.c b/libavutil/display.c
index d31061283c..434ed50104 100644
--- a/libavutil/display.c
+++ b/libavutil/display.c
@@ -48,6 +48,30 @@  double av_display_rotation_get(const int32_t matrix[9])
     return -rotation;
 }
 
+double av_display_hscale_get(const int32_t matrix[9])
+{
+    double scale;
+
+    scale = hypot(CONV_FP(matrix[0]), CONV_FP(matrix[1]));
+
+    if (scale == 0.0)
+        return NAN;
+
+    return scale;
+}
+
+double av_display_vscale_get(const int32_t matrix[9])
+{
+    double scale;
+
+    scale = hypot(CONV_FP(matrix[3]), CONV_FP(matrix[4]));
+
+    if (scale == 0.0)
+        return NAN;
+
+    return scale;
+}
+
 void av_display_rotation_set(int32_t matrix[9], double angle)
 {
     double radians = -angle * M_PI / 180.0f;
@@ -72,3 +96,14 @@  void av_display_matrix_flip(int32_t matrix[9], int hflip, int vflip)
         for (i = 0; i < 9; i++)
             matrix[i] *= flip[i % 3];
 }
+
+void av_display_matrix_scale(int32_t matrix[9], double hscale, double vscale)
+{
+    // hscale
+    matrix[0] = CONV_DB(CONV_FP(matrix[0]) * hscale);
+    matrix[1] = CONV_DB(CONV_FP(matrix[1]) * hscale);
+
+    // vscale
+    matrix[3] = CONV_DB(CONV_FP(matrix[3]) * vscale);
+    matrix[4] = CONV_DB(CONV_FP(matrix[4]) * vscale);
+}
diff --git a/libavutil/display.h b/libavutil/display.h
index 31d8bef361..ef507eb521 100644
--- a/libavutil/display.h
+++ b/libavutil/display.h
@@ -86,6 +86,24 @@ 
  */
 double av_display_rotation_get(const int32_t matrix[9]);
 
+/**
+ * Extract the horizontal scaling component of the transformation matrix.
+ *
+ * @param matrix the transformation matrix
+ * @return the horizontal scaling by which the transformation matrix scales the frame
+ *         in the horizontal direction.
+ */
+double av_display_hscale_get(const int32_t matrix[9]);
+
+/**
+ * Extract the vertical scaling component of the transformation matrix.
+ *
+ * @param matrix the transformation matrix
+ * @return the vertical scaling by which the transformation matrix scales the frame
+ *         in the vertical direction.
+ */
+double av_display_vscale_get(const int32_t matrix[9]);
+
 /**
  * Initialize a transformation matrix describing a pure clockwise
  * rotation by the specified angle (in degrees).
@@ -105,6 +123,14 @@  void av_display_rotation_set(int32_t matrix[9], double angle);
  */
 void av_display_matrix_flip(int32_t matrix[9], int hflip, int vflip);
 
+/**
+ * Scale the input matrix horizontally and/or vertically.
+ *
+ * @param matrix an allocated transformation matrix
+ * @param hscale scale factor by which the matrix should be scaled horizontally
+ * @param vscale scale factor by which the matrix should be scaled vertically
+ */
+void av_display_matrix_scale(int32_t matrix[9], double hscale, double vscale);
 /**
  * @}
  * @}
diff --git a/libavutil/version.h b/libavutil/version.h
index 9c44cef6aa..5aca550f45 100644
--- a/libavutil/version.h
+++ b/libavutil/version.h
@@ -79,7 +79,7 @@ 
  */
 
 #define LIBAVUTIL_VERSION_MAJOR  57
-#define LIBAVUTIL_VERSION_MINOR  37
+#define LIBAVUTIL_VERSION_MINOR  38
 #define LIBAVUTIL_VERSION_MICRO 100
 
 #define LIBAVUTIL_VERSION_INT   AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \