diff mbox series

[FFmpeg-devel,1/2] avcodec/mediacodecdec: refactor color space utils

Message ID tencent_569C6AE0525BC434FB80AC34693E6B57F309@qq.com
State Accepted
Commit d0c6389e91f2e3254c602be9b5ca2e35922c39a3
Headers show
Series [FFmpeg-devel,1/2] avcodec/mediacodecdec: refactor color space utils | 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

Zhao Zhili March 10, 2023, 8:48 p.m. UTC
From: Zhao Zhili <zhilizhao@tencent.com>

So it can be shared with encoder.

Signed-off-by: Zhao Zhili <zhilizhao@tencent.com>
---
 libavcodec/mediacodec_wrapper.c   | 102 ++++++++++++++++++++++++++++++
 libavcodec/mediacodec_wrapper.h   |  73 +++++++++++++++++++++
 libavcodec/mediacodecdec_common.c |  87 ++-----------------------
 3 files changed, 179 insertions(+), 83 deletions(-)
diff mbox series

Patch

diff --git a/libavcodec/mediacodec_wrapper.c b/libavcodec/mediacodec_wrapper.c
index 34ec2134aa..d1fb640ec2 100644
--- a/libavcodec/mediacodec_wrapper.c
+++ b/libavcodec/mediacodec_wrapper.c
@@ -2542,3 +2542,105 @@  int ff_Build_SDK_INT(AVCodecContext *avctx)
 
     return ret;
 }
+
+static struct {
+    enum FFAMediaFormatColorRange mf_range;
+    enum AVColorRange range;
+} color_range_map[] = {
+    { COLOR_RANGE_FULL,     AVCOL_RANGE_JPEG },
+    { COLOR_RANGE_LIMITED,  AVCOL_RANGE_MPEG },
+};
+
+static struct {
+    enum FFAMediaFormatColorStandard mf_standard;
+    enum AVColorSpace space;
+} color_space_map[] = {
+    { COLOR_STANDARD_BT709,         AVCOL_SPC_BT709         },
+    { COLOR_STANDARD_BT601_PAL,     AVCOL_SPC_BT470BG       },
+    { COLOR_STANDARD_BT601_NTSC,    AVCOL_SPC_SMPTE170M     },
+    { COLOR_STANDARD_BT2020,        AVCOL_SPC_BT2020_NCL    },
+};
+
+static struct {
+    enum FFAMediaFormatColorStandard mf_standard;
+    enum AVColorPrimaries primaries;
+} color_primaries_map[] = {
+    { COLOR_STANDARD_BT709,         AVCOL_PRI_BT709     },
+    { COLOR_STANDARD_BT601_PAL,     AVCOL_PRI_BT470BG   },
+    { COLOR_STANDARD_BT601_NTSC,    AVCOL_PRI_SMPTE170M },
+    { COLOR_STANDARD_BT2020,        AVCOL_PRI_BT2020    },
+};
+
+static struct {
+    enum FFAMediaFormatColorTransfer mf_transfer;
+    enum AVColorTransferCharacteristic transfer;
+} color_transfer_map[] = {
+    { COLOR_TRANSFER_LINEAR,        AVCOL_TRC_LINEAR        },
+    { COLOR_TRANSFER_SDR_VIDEO,     AVCOL_TRC_SMPTE170M     },
+    { COLOR_TRANSFER_ST2084,        AVCOL_TRC_SMPTEST2084   },
+    { COLOR_TRANSFER_HLG,           AVCOL_TRC_ARIB_STD_B67  },
+};
+
+enum AVColorRange ff_AMediaFormatColorRange_to_AVColorRange(int color_range)
+{
+    for (int i = 0; i < FF_ARRAY_ELEMS(color_range_map); i++)
+        if (color_range_map[i].mf_range == color_range)
+            return color_range_map[i].range;
+
+    return AVCOL_RANGE_UNSPECIFIED;
+}
+
+int ff_AMediaFormatColorRange_from_AVColorRange(enum AVColorRange color_range)
+{
+    for (int i = 0; i < FF_ARRAY_ELEMS(color_range_map); i++)
+        if (color_range_map[i].range == color_range)
+            return color_range_map[i].mf_range;
+    return COLOR_RANGE_UNSPECIFIED;
+}
+
+enum AVColorSpace ff_AMediaFormatColorStandard_to_AVColorSpace(int color_standard)
+{
+    for (int i = 0; i < FF_ARRAY_ELEMS(color_space_map); i++)
+        if (color_space_map[i].mf_standard == color_standard)
+            return color_space_map[i].space;
+
+    return AVCOL_SPC_UNSPECIFIED;
+}
+
+int ff_AMediaFormatColorStandard_from_AVColorSpace(enum AVColorSpace color_space)
+{
+    for (int i = 0; i < FF_ARRAY_ELEMS(color_space_map); i++)
+        if (color_space_map[i].space == color_space)
+            return color_space_map[i].mf_standard;
+
+    return COLOR_STANDARD_UNSPECIFIED;
+}
+
+enum AVColorPrimaries ff_AMediaFormatColorStandard_to_AVColorPrimaries(int color_standard)
+{
+    for (int i = 0; i < FF_ARRAY_ELEMS(color_primaries_map); i++)
+        if (color_primaries_map[i].mf_standard == color_standard)
+            return color_primaries_map[i].primaries;
+
+    return AVCOL_PRI_UNSPECIFIED;
+}
+
+enum AVColorTransferCharacteristic
+ff_AMediaFormatColorTransfer_to_AVColorTransfer(int color_transfer)
+{
+    for (int i = 0; i < FF_ARRAY_ELEMS(color_transfer_map); i++)
+        if (color_transfer_map[i].mf_transfer == color_transfer)
+            return color_transfer_map[i].transfer;
+
+    return AVCOL_TRC_UNSPECIFIED;
+}
+
+int ff_AMediaFormatColorTransfer_from_AVColorTransfer(
+    enum AVColorTransferCharacteristic color_transfer)
+{
+    for (int i = 0; i < FF_ARRAY_ELEMS(color_transfer_map); i++)
+        if (color_transfer_map[i].transfer == color_transfer)
+            return color_transfer_map[i].mf_transfer;
+
+    return COLOR_TRANSFER_UNSPECIFIED;
+}
diff --git a/libavcodec/mediacodec_wrapper.h b/libavcodec/mediacodec_wrapper.h
index 1b81e6db84..11a4260497 100644
--- a/libavcodec/mediacodec_wrapper.h
+++ b/libavcodec/mediacodec_wrapper.h
@@ -345,4 +345,77 @@  static inline int ff_AMediaCodec_signalEndOfInputStream(FFAMediaCodec *codec)
 
 int ff_Build_SDK_INT(AVCodecContext *avctx);
 
+enum FFAMediaFormatColorRange {
+    COLOR_RANGE_UNSPECIFIED = 0x0,
+    COLOR_RANGE_FULL        = 0x1,
+    COLOR_RANGE_LIMITED     = 0x2,
+};
+
+enum FFAMediaFormatColorStandard {
+    COLOR_STANDARD_UNSPECIFIED  = 0x0,
+    COLOR_STANDARD_BT709        = 0x1,
+    COLOR_STANDARD_BT601_PAL    = 0x2,
+    COLOR_STANDARD_BT601_NTSC   = 0x4,
+    COLOR_STANDARD_BT2020       = 0x6,
+};
+
+enum FFAMediaFormatColorTransfer {
+    COLOR_TRANSFER_UNSPECIFIED = 0x0,
+    COLOR_TRANSFER_LINEAR      = 0x1,
+    COLOR_TRANSFER_SDR_VIDEO   = 0x3,
+    COLOR_TRANSFER_ST2084      = 0x6,
+    COLOR_TRANSFER_HLG         = 0x7,
+};
+
+/**
+ * Map MediaFormat color range to AVColorRange.
+ *
+ * return AVCOL_RANGE_UNSPECIFIED when failed.
+ */
+enum AVColorRange ff_AMediaFormatColorRange_to_AVColorRange(int color_range);
+
+/**
+ * Map AVColorRange to MediaFormat color range.
+ *
+ * return COLOR_RANGE_UNSPECIFIED when failed.
+ */
+int ff_AMediaFormatColorRange_from_AVColorRange(enum AVColorRange color_range);
+
+/**
+ * Map MediaFormat color standard to AVColorSpace.
+ *
+ * return AVCOL_SPC_UNSPECIFIED when failed.
+ */
+enum AVColorSpace ff_AMediaFormatColorStandard_to_AVColorSpace(int color_standard);
+
+/**
+ * Map AVColorSpace to MediaFormat color standard.
+ *
+ * return COLOR_STANDARD_UNSPECIFIED when failed.
+ */
+int ff_AMediaFormatColorStandard_from_AVColorSpace(enum AVColorSpace color_space);
+
+/**
+ * Map MediaFormat color standard to AVColorPrimaries.
+ *
+ * return AVCOL_PRI_UNSPECIFIED when failed.
+ */
+enum AVColorPrimaries ff_AMediaFormatColorStandard_to_AVColorPrimaries(int color_standard);
+
+/**
+ * Map MediaFormat color transfer to AVColorTransferCharacteristic.
+ *
+ * return AVCOL_TRC_UNSPECIFIED when failed.
+ */
+enum AVColorTransferCharacteristic
+ff_AMediaFormatColorTransfer_to_AVColorTransfer(int color_transfer);
+
+/**
+ * Map AVColorTransferCharacteristic to MediaFormat color transfer.
+ *
+ * return COLOR_TRANSFER_UNSPECIFIED when failed.
+ */
+int ff_AMediaFormatColorTransfer_from_AVColorTransfer(
+    enum AVColorTransferCharacteristic color_transfer);
+
 #endif /* AVCODEC_MEDIACODEC_WRAPPER_H */
diff --git a/libavcodec/mediacodecdec_common.c b/libavcodec/mediacodecdec_common.c
index 03bee11918..1151bb71f9 100644
--- a/libavcodec/mediacodecdec_common.c
+++ b/libavcodec/mediacodecdec_common.c
@@ -85,85 +85,6 @@ 
 #define OUTPUT_DEQUEUE_TIMEOUT_US 8000
 #define OUTPUT_DEQUEUE_BLOCK_TIMEOUT_US 1000000
 
-enum {
-    COLOR_RANGE_FULL    = 0x1,
-    COLOR_RANGE_LIMITED = 0x2,
-};
-
-static enum AVColorRange mcdec_get_color_range(int color_range)
-{
-    switch (color_range) {
-    case COLOR_RANGE_FULL:
-        return AVCOL_RANGE_JPEG;
-    case COLOR_RANGE_LIMITED:
-        return AVCOL_RANGE_MPEG;
-    default:
-        return AVCOL_RANGE_UNSPECIFIED;
-    }
-}
-
-enum {
-    COLOR_STANDARD_BT709      = 0x1,
-    COLOR_STANDARD_BT601_PAL  = 0x2,
-    COLOR_STANDARD_BT601_NTSC = 0x4,
-    COLOR_STANDARD_BT2020     = 0x6,
-};
-
-static enum AVColorSpace mcdec_get_color_space(int color_standard)
-{
-    switch (color_standard) {
-    case COLOR_STANDARD_BT709:
-        return AVCOL_SPC_BT709;
-    case COLOR_STANDARD_BT601_PAL:
-        return AVCOL_SPC_BT470BG;
-    case COLOR_STANDARD_BT601_NTSC:
-        return AVCOL_SPC_SMPTE170M;
-    case COLOR_STANDARD_BT2020:
-        return AVCOL_SPC_BT2020_NCL;
-    default:
-        return AVCOL_SPC_UNSPECIFIED;
-    }
-}
-
-static enum AVColorPrimaries mcdec_get_color_pri(int color_standard)
-{
-    switch (color_standard) {
-    case COLOR_STANDARD_BT709:
-        return AVCOL_PRI_BT709;
-    case COLOR_STANDARD_BT601_PAL:
-        return AVCOL_PRI_BT470BG;
-    case COLOR_STANDARD_BT601_NTSC:
-        return AVCOL_PRI_SMPTE170M;
-    case COLOR_STANDARD_BT2020:
-        return AVCOL_PRI_BT2020;
-    default:
-        return AVCOL_PRI_UNSPECIFIED;
-    }
-}
-
-enum {
-    COLOR_TRANSFER_LINEAR    = 0x1,
-    COLOR_TRANSFER_SDR_VIDEO = 0x3,
-    COLOR_TRANSFER_ST2084    = 0x6,
-    COLOR_TRANSFER_HLG       = 0x7,
-};
-
-static enum AVColorTransferCharacteristic mcdec_get_color_trc(int color_transfer)
-{
-    switch (color_transfer) {
-    case COLOR_TRANSFER_LINEAR:
-        return AVCOL_TRC_LINEAR;
-    case COLOR_TRANSFER_SDR_VIDEO:
-        return AVCOL_TRC_SMPTE170M;
-    case COLOR_TRANSFER_ST2084:
-        return AVCOL_TRC_SMPTEST2084;
-    case COLOR_TRANSFER_HLG:
-        return AVCOL_TRC_ARIB_STD_B67;
-    default:
-        return AVCOL_TRC_UNSPECIFIED;
-    }
-}
-
 enum {
     COLOR_FormatYUV420Planar                              = 0x13,
     COLOR_FormatYUV420SemiPlanar                          = 0x15,
@@ -517,17 +438,17 @@  static int mediacodec_dec_parse_format(AVCodecContext *avctx, MediaCodecDecConte
 
     AMEDIAFORMAT_GET_INT32(color_range, "color-range", 0);
     if (color_range)
-        avctx->color_range = mcdec_get_color_range(color_range);
+        avctx->color_range = ff_AMediaFormatColorRange_to_AVColorRange(color_range);
 
     AMEDIAFORMAT_GET_INT32(color_standard, "color-standard", 0);
     if (color_standard) {
-        avctx->colorspace = mcdec_get_color_space(color_standard);
-        avctx->color_primaries = mcdec_get_color_pri(color_standard);
+        avctx->colorspace = ff_AMediaFormatColorStandard_to_AVColorSpace(color_standard);
+        avctx->color_primaries = ff_AMediaFormatColorStandard_to_AVColorPrimaries(color_standard);
     }
 
     AMEDIAFORMAT_GET_INT32(color_transfer, "color-transfer", 0);
     if (color_transfer)
-        avctx->color_trc = mcdec_get_color_trc(color_transfer);
+        avctx->color_trc = ff_AMediaFormatColorTransfer_to_AVColorTransfer(color_transfer);
 
     av_log(avctx, AV_LOG_INFO,
         "Output crop parameters top=%d bottom=%d left=%d right=%d, "