From d6ce720765d0684d596f3238ba77f6c16c0cee44 Mon Sep 17 00:00:00 2001 From: Niklas Haas Date: Wed, 19 Feb 2025 20:35:20 +0100 Subject: [PATCH] avcodec/png: set correct alpha mode PNG always uses straight alpha. cf. https://www.w3.org/TR/PNG-Rationale.html > Although each form of alpha storage has its advantages, we did not want to > require all PNG viewers to handle both forms. We standardized on non- > premultiplied alpha as being the lossless and more general case. --- libavcodec/pngdec.c | 5 +++++ libavcodec/pngenc.c | 6 ++++++ 2 files changed, 11 insertions(+) diff --git a/libavcodec/pngdec.c b/libavcodec/pngdec.c index ce817d44fa..fff27305d5 100644 --- a/libavcodec/pngdec.c +++ b/libavcodec/pngdec.c @@ -998,6 +998,11 @@ static int decode_idat_chunk(AVCodecContext *avctx, PNGDecContext *s, s->bpp += byte_depth; } + /* PNG spec mandates independent alpha channel */ + if (s->color_type == PNG_COLOR_TYPE_RGB_ALPHA || + s->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) + avctx->alpha_mode = AVALPHA_MODE_STRAIGHT; + ff_progress_frame_unref(&s->picture); if (s->dispose_op == APNG_DISPOSE_OP_PREVIOUS) { /* We only need a buffer for the current picture. */ diff --git a/libavcodec/pngenc.c b/libavcodec/pngenc.c index 635c89e87e..5baad9aad5 100644 --- a/libavcodec/pngenc.c +++ b/libavcodec/pngenc.c @@ -1255,6 +1255,9 @@ const FFCodec ff_png_encoder = { AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY8A, AV_PIX_FMT_GRAY16BE, AV_PIX_FMT_YA16BE, AV_PIX_FMT_MONOBLACK), + .alpha_modes = (const enum AVAlphaMode[]) { + AVALPHA_MODE_STRAIGHT, AVALPHA_MODE_UNSPECIFIED + }, .p.priv_class = &pngenc_class, .caps_internal = FF_CODEC_CAP_ICC_PROFILES, }; @@ -1275,6 +1278,9 @@ const FFCodec ff_apng_encoder = { AV_PIX_FMT_PAL8, AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY8A, AV_PIX_FMT_GRAY16BE, AV_PIX_FMT_YA16BE), + .alpha_modes = (const enum AVAlphaMode[]) { + AVALPHA_MODE_STRAIGHT, AVALPHA_MODE_UNSPECIFIED + }, .p.priv_class = &pngenc_class, .caps_internal = FF_CODEC_CAP_ICC_PROFILES, };