Commit 288839e860 for ffmpeg

commit 288839e860c7e55e466170215fc478fa7e750e97
Author: moi15moi <moi15moismokerlolilol@gmail.com>
Date:   Tue Nov 17 20:08:30 2026 -0500

    avcodec/mediacodecdec: pass container color metadata as a hint

    Set color-range, color-standard and color-transfer in the input
    MediaFormat when they are known from the container.
    MediaCodec uses these as defaults when the bitstream does not signal color aspects.

    Without them, Android fills in its own defaults (ex: COLOR_TRANSFER_SDR_VIDEO),
    which are then reported in the output format and override the container metadata.
    This breaks VP9 HLG content in WebM, since VP9 bitstreams doesn't carry transfer
    characteristics, so frames are tagged SMPTE170M instead of ARIB STD-B67.

diff --git a/libavcodec/mediacodec_wrapper.c b/libavcodec/mediacodec_wrapper.c
index 895b9aaa20..d0edce2c84 100644
--- a/libavcodec/mediacodec_wrapper.c
+++ b/libavcodec/mediacodec_wrapper.c
@@ -2771,6 +2771,15 @@ enum AVColorPrimaries ff_AMediaFormatColorStandard_to_AVColorPrimaries(int color
     return AVCOL_PRI_UNSPECIFIED;
 }

+int ff_AMediaFormatColorStandard_from_AVColorPrimaries(enum AVColorPrimaries color_primaries)
+{
+    for (int i = 0; i < FF_ARRAY_ELEMS(color_primaries_map); i++)
+        if (color_primaries_map[i].primaries == color_primaries)
+            return color_primaries_map[i].mf_standard;
+
+    return COLOR_STANDARD_UNSPECIFIED;
+}
+
 enum AVColorTransferCharacteristic
 ff_AMediaFormatColorTransfer_to_AVColorTransfer(int color_transfer)
 {
diff --git a/libavcodec/mediacodec_wrapper.h b/libavcodec/mediacodec_wrapper.h
index 18d0796445..0f1e98f19a 100644
--- a/libavcodec/mediacodec_wrapper.h
+++ b/libavcodec/mediacodec_wrapper.h
@@ -430,6 +430,13 @@ int ff_AMediaFormatColorStandard_from_AVColorSpace(enum AVColorSpace color_space
  */
 enum AVColorPrimaries ff_AMediaFormatColorStandard_to_AVColorPrimaries(int color_standard);

+/**
+ * Map AVColorPrimaries to MediaFormat color standard.
+ *
+ * return COLOR_STANDARD_UNSPECIFIED when failed.
+ */
+int ff_AMediaFormatColorStandard_from_AVColorPrimaries(enum AVColorPrimaries color_primaries);
+
 /**
  * Map MediaFormat color transfer to AVColorTransferCharacteristic.
  *
diff --git a/libavcodec/mediacodecdec.c b/libavcodec/mediacodecdec.c
index 0551370a54..bbfe429b90 100644
--- a/libavcodec/mediacodecdec.c
+++ b/libavcodec/mediacodecdec.c
@@ -307,6 +307,24 @@ static int common_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
 }
 #endif

+/* A MediaFormat color standard fixes both the primaries and the matrix.
+ * Only hint one when the container's primaries and matrix agree on it.
+ * BT470BG and SMPTE170M are the same matrix,
+ * so for BT.601 the primaries decide between PAL and NTSC.
+ */
+static int mediacodec_color_standard_hint(AVCodecContext *avctx)
+{
+    int standard = ff_AMediaFormatColorStandard_from_AVColorPrimaries(avctx->color_primaries);
+    enum AVColorSpace space = avctx->colorspace;
+
+    if (standard == COLOR_STANDARD_BT601_PAL || standard == COLOR_STANDARD_BT601_NTSC)
+        return space == AVCOL_SPC_BT470BG || space == AVCOL_SPC_SMPTE170M ?
+               standard : COLOR_STANDARD_UNSPECIFIED;
+
+    return ff_AMediaFormatColorStandard_from_AVColorSpace(space) == standard ?
+           standard : COLOR_STANDARD_UNSPECIFIED;
+}
+
 static av_cold int mediacodec_decode_init(AVCodecContext *avctx)
 {
     int ret;
@@ -436,6 +454,17 @@ static av_cold int mediacodec_decode_init(AVCodecContext *avctx)
     if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
         ff_AMediaFormat_setInt32(format, "width", avctx->width);
         ff_AMediaFormat_setInt32(format, "height", avctx->height);
+
+        ret = ff_AMediaFormatColorRange_from_AVColorRange(avctx->color_range);
+        if (ret != COLOR_RANGE_UNSPECIFIED)
+            ff_AMediaFormat_setInt32(format, "color-range", ret);
+        ret = mediacodec_color_standard_hint(avctx);
+        if (ret != COLOR_STANDARD_UNSPECIFIED)
+            ff_AMediaFormat_setInt32(format, "color-standard", ret);
+        ret = ff_AMediaFormatColorTransfer_from_AVColorTransfer(avctx->color_trc);
+        if (ret != COLOR_TRANSFER_UNSPECIFIED)
+            ff_AMediaFormat_setInt32(format, "color-transfer", ret);
+        ret = 0;
     } else {
         ff_AMediaFormat_setInt32(format, "channel-count", avctx->ch_layout.nb_channels);
         ff_AMediaFormat_setInt32(format, "sample-rate", avctx->sample_rate);