[FFmpeg-devel] [PATCH v2 07/18] avcodec/exr: set correct alpha mode

Niklas Haas ffmpeg at haasn.xyz
Wed Jul 23 16:47:10 EEST 2025


From: Niklas Haas <git at haasn.dev>

OpenEXR always uses premultiplied alpha, as per the spec.

cf. https://openexr.com/en/latest/TechnicalIntroduction.html

> By convention, all color channels are premultiplied by alpha, so that
> `foreground + (1-alpha) x background` performs a correct “over” operation.
> (See Premultiplied vs. Un-Premultiplied Color Channels)
>
> In the visual effects industry premultiplied color channels are the norm,
> and application software packages typically use internal image
> representations that are also premultiplied.
---
 libavcodec/exr.c    | 3 +++
 libavcodec/exrenc.c | 3 +++
 2 files changed, 6 insertions(+)

diff --git a/libavcodec/exr.c b/libavcodec/exr.c
index 0a6aab662e..9f0daf863c 100644
--- a/libavcodec/exr.c
+++ b/libavcodec/exr.c
@@ -2124,6 +2124,9 @@ static int decode_frame(AVCodecContext *avctx, AVFrame *picture,
         return AVERROR_INVALIDDATA;
     }
 
+    if (s->channel_offsets[3] >= 0)
+        avctx->alpha_mode = AVALPHA_MODE_PREMULTIPLIED;
+
 #if FF_API_EXR_GAMMA
     if (s->apply_trc_type != AVCOL_TRC_UNSPECIFIED)
         avctx->color_trc = s->apply_trc_type;
diff --git a/libavcodec/exrenc.c b/libavcodec/exrenc.c
index c9685890ea..54f63d9eb6 100644
--- a/libavcodec/exrenc.c
+++ b/libavcodec/exrenc.c
@@ -553,4 +553,7 @@ const FFCodec ff_exr_encoder = {
     FF_CODEC_ENCODE_CB(encode_frame),
     .close          = encode_close,
     CODEC_PIXFMTS(AV_PIX_FMT_GRAYF32, AV_PIX_FMT_GBRPF32, AV_PIX_FMT_GBRAPF32),
+    .alpha_modes    = (const enum AVAlphaMode[]) {
+        AVALPHA_MODE_PREMULTIPLIED, AVALPHA_MODE_UNSPECIFIED
+    },
 };
-- 
2.50.1



More information about the ffmpeg-devel mailing list