PR #26158 opened by moi15moi URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26158 Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26158.patch
# Summary of changes 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. This will help to fix https://github.com/mpv-android/mpv-android/issues/1074 >From 9f22d3d2fdcf3a59b5dedeb1931ce5ba8f1863b6 Mon Sep 17 00:00:00 2001 From: moi15moi <[email protected]> Date: Tue, 17 Nov 2026 20:08:30 -0500 Subject: [PATCH] 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. --- libavcodec/mediacodec_wrapper.c | 9 +++++++++ libavcodec/mediacodec_wrapper.h | 7 +++++++ libavcodec/mediacodecdec.c | 29 +++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+) diff --git a/libavcodec/mediacodec_wrapper.c b/libavcodec/mediacodec_wrapper.c index 895b9aaa206..d0edce2c84f 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 18d07964453..0f1e98f19ab 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 0551370a544..bbfe429b903 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); -- 2.54.0 _______________________________________________ ffmpeg-devel mailing list -- [email protected] To unsubscribe send an email to [email protected]
