diff mbox series

[FFmpeg-devel,2/5] avutil/pixdesc: Add av_chroma_location_(enum_to_pos|pos_to_enum)

Message ID GV1P250MB0737A6F2E6D7D42780B273BF8F4F9@GV1P250MB0737.EURP250.PROD.OUTLOOK.COM
State Accepted
Headers show
Series [FFmpeg-devel,1/5] avcodec/avcodec: Move AV_ER_* and FF_COMPLIANCE_* to defs.h | 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

Andreas Rheinhardt Sept. 21, 2022, 1:34 a.m. UTC
They are intended as replacements for avcodec_enum_to_chroma_pos()
and avcodec_chroma_pos_to_enum().

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
Will add version bump upon applying.
(Honestly, I am not sure whether there is a need for a replacement
given that Matroska seems to be the only user of this.)

 doc/APIchanges      |  3 +++
 libavutil/pixdesc.c | 23 +++++++++++++++++++++++
 libavutil/pixdesc.h | 22 ++++++++++++++++++++++
 3 files changed, 48 insertions(+)

Comments

Andreas Rheinhardt Sept. 25, 2022, 12:09 a.m. UTC | #1
Andreas Rheinhardt:
> They are intended as replacements for avcodec_enum_to_chroma_pos()
> and avcodec_chroma_pos_to_enum().
> 
> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
> ---
> Will add version bump upon applying.
> (Honestly, I am not sure whether there is a need for a replacement
> given that Matroska seems to be the only user of this.)
> 

It is used by mpv (for its interactions with swscale), so I intend to
apply this patchset-as-is (with the replacement) in two days unless
there are objections.

- Andreas
diff mbox series

Patch

diff --git a/doc/APIchanges b/doc/APIchanges
index cb7592bb64..8ead37dcbb 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -14,6 +14,9 @@  libavutil:     2021-04-27
 
 API changes, most recent first:
 
+2022-09-21 - xxxxxxxxxx - lavu 57.xx.100 - pixdesc.h
+  Add av_chroma_location_enum_to_pos() and av_chroma_location_pos_to_enum().
+
 2022-09-21 - xxxxxxxxxx - lavc 59.45.100 - avcodec.h defs.h
   Move the AV_EF_* and FF_COMPLIANCE_* defines from avcodec.h to defs.h.
 
diff --git a/libavutil/pixdesc.c b/libavutil/pixdesc.c
index b472a94f60..9cd95f1450 100644
--- a/libavutil/pixdesc.c
+++ b/libavutil/pixdesc.c
@@ -3262,3 +3262,26 @@  int av_chroma_location_from_name(const char *name)
 
     return AVERROR(EINVAL);
 }
+
+int av_chroma_location_enum_to_pos(int *xpos, int *ypos, enum AVChromaLocation pos)
+{
+    if (pos <= AVCHROMA_LOC_UNSPECIFIED || pos >= AVCHROMA_LOC_NB)
+        return AVERROR(EINVAL);
+    pos--;
+
+    *xpos = (pos&1) * 128;
+    *ypos = ((pos>>1)^(pos<4)) * 128;
+
+    return 0;
+}
+
+enum AVChromaLocation av_chroma_location_pos_to_enum(int xpos, int ypos)
+{
+    int pos, xout, yout;
+
+    for (pos = AVCHROMA_LOC_UNSPECIFIED + 1; pos < AVCHROMA_LOC_NB; pos++) {
+        if (av_chroma_location_enum_to_pos(&xout, &yout, pos) == 0 && xout == xpos && yout == ypos)
+            return pos;
+    }
+    return AVCHROMA_LOC_UNSPECIFIED;
+}
diff --git a/libavutil/pixdesc.h b/libavutil/pixdesc.h
index 48d9300bfe..0df73e6efe 100644
--- a/libavutil/pixdesc.h
+++ b/libavutil/pixdesc.h
@@ -264,6 +264,28 @@  const char *av_chroma_location_name(enum AVChromaLocation location);
  */
 int av_chroma_location_from_name(const char *name);
 
+/**
+ * Converts AVChromaLocation to swscale x/y chroma position.
+ *
+ * The positions represent the chroma (0,0) position in a coordinates system
+ * with luma (0,0) representing the origin and luma(1,1) representing 256,256
+ *
+ * @param xpos  horizontal chroma sample position
+ * @param ypos  vertical   chroma sample position
+ */
+int av_chroma_location_enum_to_pos(int *xpos, int *ypos, enum AVChromaLocation pos);
+
+/**
+ * Converts swscale x/y chroma position to AVChromaLocation.
+ *
+ * The positions represent the chroma (0,0) position in a coordinates system
+ * with luma (0,0) representing the origin and luma(1,1) representing 256,256
+ *
+ * @param xpos  horizontal chroma sample position
+ * @param ypos  vertical   chroma sample position
+ */
+enum AVChromaLocation av_chroma_location_pos_to_enum(int xpos, int ypos);
+
 /**
  * Return the pixel format corresponding to name.
  *