From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTP id 7F2B040A62 for ; Sat, 5 Mar 2022 18:09:30 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 1E6B368B0BB; Sat, 5 Mar 2022 20:09:28 +0200 (EET) Received: from mail-wm1-f52.google.com (mail-wm1-f52.google.com [209.85.128.52]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 2A87A68AED8 for ; Sat, 5 Mar 2022 20:09:21 +0200 (EET) Received: by mail-wm1-f52.google.com with SMTP id i66so6800246wma.5 for ; Sat, 05 Mar 2022 10:09:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=UmAbDpykJOfX6GSQZcDKet2qin3KMdGMOnGucskAot8=; b=CBm91+dDoWmC478xF4kF42pINeDSNG+3Z92rdSOS+ZMhbbOxvFph4ruOyfA6v6z4hH XkfB8J2kJPPbEClJkrnSPPb4OvoZUZE1gV4aFn3eBRmMolHsxfiw/dGR4g4aZUY59MV1 4MhJ7Zq0R4CrBtVStC0uY0bV/BJpEE0gedERSQo2oqnEiQNntD+B/rbu9pxal3s1DLLD rTzGSRKGDsjmQHV+9sh54iHtPKuvWX/Im6bT9dxHBR5f6XbyIdS3uHT1Bey+Z8J08ShM NlxTkwtv6G0uxZ3khaLYtXKfQaNxPKHIFNBgLBP84V3617SYQfZqv8NpC8CmCRhHfpdh CKHw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=UmAbDpykJOfX6GSQZcDKet2qin3KMdGMOnGucskAot8=; b=QXsC3nbPTA2ZAvSA+t1XSaosEAXk9gnSyk2iTW4RDceuckVtyA2pO6IFvvJ0Km9uGK gkbrA3mtfx85AxrKt1+sioio/jUpOKrJ+0KP3jcvHYqyt5nJtZcJ4JWDgQN3NYnKQ11m HD4msHUQJ/mfUJVhEatr9rGO7Jo9T06Xa+ynxkRvcrpa3nWXDDclWTDAaYW7nkdnky37 reM4436Kobb9KqCYPTuGAyI1LjNwmQaCZ8QIMnEI8qZLzU3ZBSEsIV1NocVy4amaip7c eBqJdpX4aSk0yYoP7c4m9PI+xq8IMQIbU/HhmXKYKqbJY6xS50OLCezyrkS/bOOUiBP9 m3MQ== X-Gm-Message-State: AOAM5325d8tErPmFlOJY6SfPsnwGQD8iNgSxoFheuwMjecGtfA9wiI2f PduF8eJJvlc8OlaZfPp1rZM5eKQ4KXYm9A== X-Google-Smtp-Source: ABdhPJyA9prpfx2UyOoxQ2kYvfo17nDIZubh+8iwKpAw33AADSEYGq8pFvRr9PzNPsgHy5fdPLd8gg== X-Received: by 2002:a05:600c:2250:b0:383:bab2:9df5 with SMTP id a16-20020a05600c225000b00383bab29df5mr3165375wmm.162.1646503760083; Sat, 05 Mar 2022 10:09:20 -0800 (PST) Received: from localhost.localdomain ([95.168.118.13]) by smtp.gmail.com with ESMTPSA id o18-20020a05600c511200b00352ec3b4c5asm10380175wms.7.2022.03.05.10.09.18 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 05 Mar 2022 10:09:19 -0800 (PST) From: Paul B Mahol To: ffmpeg-devel@ffmpeg.org Date: Sat, 5 Mar 2022 19:10:59 +0100 Message-Id: <20220305181059.594638-1-onemda@gmail.com> X-Mailer: git-send-email 2.33.0 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH] avcodec/pngdec: support alpha blending for palette apng X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: Update clock test, as PAL8 apngs are now decoded as RGBA. Signed-off-by: Paul B Mahol --- libavcodec/pngdec.c | 61 +++++++++++++++-------------- tests/ref/fate/apng-clock | 80 +++++++++++++++++++-------------------- 2 files changed, 72 insertions(+), 69 deletions(-) diff --git a/libavcodec/pngdec.c b/libavcodec/pngdec.c index 39bb652f67..a335a29b08 100644 --- a/libavcodec/pngdec.c +++ b/libavcodec/pngdec.c @@ -677,7 +677,7 @@ static int decode_idat_chunk(AVCodecContext *avctx, PNGDecContext *s, avctx->pix_fmt = AV_PIX_FMT_RGBA64BE; } else if ((s->bits_per_pixel == 1 || s->bits_per_pixel == 2 || s->bits_per_pixel == 4 || s->bits_per_pixel == 8) && s->color_type == PNG_COLOR_TYPE_PALETTE) { - avctx->pix_fmt = AV_PIX_FMT_PAL8; + avctx->pix_fmt = avctx->codec_id == AV_CODEC_ID_APNG ? AV_PIX_FMT_RGBA : AV_PIX_FMT_PAL8; } else if (s->bit_depth == 1 && s->bits_per_pixel == 1 && avctx->codec_id != AV_CODEC_ID_APNG) { avctx->pix_fmt = AV_PIX_FMT_MONOBLACK; } else if (s->bit_depth == 8 && @@ -1022,7 +1022,6 @@ static int decode_fctl_chunk(AVCodecContext *avctx, PNGDecContext *s, if (blend_op == APNG_BLEND_OP_OVER && !s->has_trns && ( avctx->pix_fmt == AV_PIX_FMT_RGB24 || avctx->pix_fmt == AV_PIX_FMT_RGB48BE || - avctx->pix_fmt == AV_PIX_FMT_PAL8 || avctx->pix_fmt == AV_PIX_FMT_GRAY8 || avctx->pix_fmt == AV_PIX_FMT_GRAY16BE || avctx->pix_fmt == AV_PIX_FMT_MONOBLACK @@ -1070,13 +1069,13 @@ static int handle_p_frame_apng(AVCodecContext *avctx, PNGDecContext *s, ptrdiff_t dst_stride = p->linesize[0]; const uint8_t *src = s->last_picture.f->data[0]; ptrdiff_t src_stride = s->last_picture.f->linesize[0]; + const int bpp = s->color_type == PNG_COLOR_TYPE_PALETTE ? 4 : s->bpp; size_t x, y; if (s->blend_op == APNG_BLEND_OP_OVER && avctx->pix_fmt != AV_PIX_FMT_RGBA && - avctx->pix_fmt != AV_PIX_FMT_GRAY8A && - avctx->pix_fmt != AV_PIX_FMT_PAL8) { + avctx->pix_fmt != AV_PIX_FMT_GRAY8A) { avpriv_request_sample(avctx, "Blending with pixel format %s", av_get_pix_fmt_name(avctx->pix_fmt)); return AVERROR_PATCHWELCOME; @@ -1095,7 +1094,7 @@ static int handle_p_frame_apng(AVCodecContext *avctx, PNGDecContext *s, for (y = s->last_y_offset; y < s->last_y_offset + s->last_h; y++) { memset(s->background_buf + src_stride * y + - s->bpp * s->last_x_offset, 0, s->bpp * s->last_w); + bpp * s->last_x_offset, 0, bpp * s->last_w); } src = s->background_buf; @@ -1103,22 +1102,22 @@ static int handle_p_frame_apng(AVCodecContext *avctx, PNGDecContext *s, // copy unchanged rectangles from the last frame for (y = 0; y < s->y_offset; y++) - memcpy(dst + y * dst_stride, src + y * src_stride, p->width * s->bpp); + memcpy(dst + y * dst_stride, src + y * src_stride, p->width * bpp); for (y = s->y_offset; y < s->y_offset + s->cur_h; y++) { - memcpy(dst + y * dst_stride, src + y * src_stride, s->x_offset * s->bpp); - memcpy(dst + y * dst_stride + (s->x_offset + s->cur_w) * s->bpp, - src + y * src_stride + (s->x_offset + s->cur_w) * s->bpp, - (p->width - s->cur_w - s->x_offset) * s->bpp); + memcpy(dst + y * dst_stride, src + y * src_stride, s->x_offset * bpp); + memcpy(dst + y * dst_stride + (s->x_offset + s->cur_w) * bpp, + src + y * src_stride + (s->x_offset + s->cur_w) * bpp, + (p->width - s->cur_w - s->x_offset) * bpp); } for (y = s->y_offset + s->cur_h; y < p->height; y++) - memcpy(dst + y * dst_stride, src + y * src_stride, p->width * s->bpp); + memcpy(dst + y * dst_stride, src + y * src_stride, p->width * bpp); if (s->blend_op == APNG_BLEND_OP_OVER) { // Perform blending for (y = s->y_offset; y < s->y_offset + s->cur_h; ++y) { - uint8_t *foreground = dst + dst_stride * y + s->bpp * s->x_offset; - const uint8_t *background = src + src_stride * y + s->bpp * s->x_offset; - for (x = s->x_offset; x < s->x_offset + s->cur_w; ++x, foreground += s->bpp, background += s->bpp) { + uint8_t *foreground = dst + dst_stride * y + bpp * s->x_offset; + const uint8_t *background = src + src_stride * y + bpp * s->x_offset; + for (x = s->x_offset; x < s->x_offset + s->cur_w; ++x, foreground += bpp, background += bpp) { size_t b; uint8_t foreground_alpha, background_alpha, output_alpha; uint8_t output[10]; @@ -1137,32 +1136,21 @@ static int handle_p_frame_apng(AVCodecContext *avctx, PNGDecContext *s, foreground_alpha = foreground[1]; background_alpha = background[1]; break; - - case AV_PIX_FMT_PAL8: - foreground_alpha = s->palette[foreground[0]] >> 24; - background_alpha = s->palette[background[0]] >> 24; - break; } if (foreground_alpha == 255) continue; if (foreground_alpha == 0) { - memcpy(foreground, background, s->bpp); - continue; - } - - if (avctx->pix_fmt == AV_PIX_FMT_PAL8) { - // TODO: Alpha blending with PAL8 will likely need the entire image converted over to RGBA first - avpriv_request_sample(avctx, "Alpha blending palette samples"); + memcpy(foreground, background, bpp); continue; } output_alpha = foreground_alpha + FAST_DIV255((255 - foreground_alpha) * background_alpha); - av_assert0(s->bpp <= 10); + av_assert0(bpp <= 10); - for (b = 0; b < s->bpp - 1; ++b) { + for (b = 0; b < bpp - 1; ++b) { if (output_alpha == 0) { output[b] = 0; } else if (background_alpha == 255) { @@ -1172,7 +1160,7 @@ static int handle_p_frame_apng(AVCodecContext *avctx, PNGDecContext *s, } } output[b] = output_alpha; - memcpy(foreground, output, s->bpp); + memcpy(foreground, output, bpp); } } } @@ -1368,6 +1356,21 @@ exit_loop: if (s->bits_per_pixel <= 4) handle_small_bpp(s, p); + if (s->color_type == PNG_COLOR_TYPE_PALETTE && avctx->codec_id == AV_CODEC_ID_APNG) { + for (int y = 0; y < s->height; y++) { + uint8_t *row = &p->data[0][p->linesize[0] * y]; + + for (int x = s->width - 1; x >= 0; x--) { + const uint8_t idx = row[x]; + + row[4*x+2] = s->palette[idx] & 0xFF; + row[4*x+1] = (s->palette[idx] >> 8 ) & 0xFF; + row[4*x+0] = (s->palette[idx] >> 16) & 0xFF; + row[4*x+3] = s->palette[idx] >> 24; + } + } + } + /* apply transparency if needed */ if (s->has_trns && s->color_type != PNG_COLOR_TYPE_PALETTE) { size_t byte_depth = s->bit_depth > 8 ? 2 : 1; diff --git a/tests/ref/fate/apng-clock b/tests/ref/fate/apng-clock index c0480fd4e6..26afc94c19 100644 --- a/tests/ref/fate/apng-clock +++ b/tests/ref/fate/apng-clock @@ -3,43 +3,43 @@ #codec_id 0: rawvideo #dimensions 0: 150x150 #sar 0: 0/1 -0, 0, 0, 1, 23524, 0xf09caaa1 -0, 1, 1, 1, 23524, 0x1a329f21 -0, 2, 2, 1, 23524, 0x9b0ca017 -0, 3, 3, 1, 23524, 0x73b09deb -0, 4, 4, 1, 23524, 0x23039f49 -0, 5, 5, 1, 23524, 0x58869e7b -0, 6, 6, 1, 23524, 0xc26397d2 -0, 7, 7, 1, 23524, 0xe07f7d87 -0, 8, 8, 1, 23524, 0x619a82ba -0, 9, 9, 1, 23524, 0x696c9757 -0, 10, 10, 1, 23524, 0x7303a4a6 -0, 11, 11, 1, 23524, 0x1e149aee -0, 12, 12, 1, 23524, 0x78dd97e0 -0, 13, 13, 1, 23524, 0xad8092d3 -0, 14, 14, 1, 23524, 0x27c090b4 -0, 15, 15, 1, 23524, 0x904c97be -0, 16, 16, 1, 23524, 0x54d29b9e -0, 17, 17, 1, 23524, 0x57689bfa -0, 18, 18, 1, 23524, 0x2772a00e -0, 19, 19, 1, 23524, 0x6a769ef6 -0, 20, 20, 1, 23524, 0x94d8aad5 -0, 21, 21, 1, 23524, 0x52f79ec4 -0, 22, 22, 1, 23524, 0x99ee9fbc -0, 23, 23, 1, 23524, 0xbd6a9e4d -0, 24, 24, 1, 23524, 0xaf4aa1cf -0, 25, 25, 1, 23524, 0xdb929f98 -0, 26, 26, 1, 23524, 0x9e189a6f -0, 27, 27, 1, 23524, 0x3ffb9410 -0, 28, 28, 1, 23524, 0x6fc9917d -0, 29, 29, 1, 23524, 0xe43e94ca -0, 30, 30, 1, 23524, 0x03b6a24f -0, 31, 31, 1, 23524, 0xc54f99b3 -0, 32, 32, 1, 23524, 0x4b9a9748 -0, 33, 33, 1, 23524, 0x25a19003 -0, 34, 34, 1, 23524, 0x2b9d77cc -0, 35, 35, 1, 23524, 0x4a5a9217 -0, 36, 36, 1, 23524, 0x241b9a7c -0, 37, 37, 1, 23524, 0xc9d39b38 -0, 38, 38, 1, 23524, 0xcca69f30 -0, 39, 39, 1, 23524, 0xe50f9ec9 +0, 0, 0, 1, 90000, 0x11af5588 +0, 1, 1, 1, 90000, 0x528c2eb8 +0, 2, 2, 1, 90000, 0xebae2d82 +0, 3, 3, 1, 90000, 0xf57951ef +0, 4, 4, 1, 90000, 0xc2a84734 +0, 5, 5, 1, 90000, 0x2dd74265 +0, 6, 6, 1, 90000, 0x5a6980cb +0, 7, 7, 1, 90000, 0x519373e4 +0, 8, 8, 1, 90000, 0xdca835fb +0, 9, 9, 1, 90000, 0xc8a46d5e +0, 10, 10, 1, 90000, 0xa01a72e3 +0, 11, 11, 1, 90000, 0xacd751fa +0, 12, 12, 1, 90000, 0x464b6d39 +0, 13, 13, 1, 90000, 0x56f1b22c +0, 14, 14, 1, 90000, 0x0595a6bb +0, 15, 15, 1, 90000, 0xb9418cdc +0, 16, 16, 1, 90000, 0x8b2b7bcf +0, 17, 17, 1, 90000, 0x1a026aaf +0, 18, 18, 1, 90000, 0x25443715 +0, 19, 19, 1, 90000, 0x3d9231cd +0, 20, 20, 1, 90000, 0xfef153a8 +0, 21, 21, 1, 90000, 0x6a7c2e7b +0, 22, 22, 1, 90000, 0x1fe92cda +0, 23, 23, 1, 90000, 0x30554ff8 +0, 24, 24, 1, 90000, 0x57e544d9 +0, 25, 25, 1, 90000, 0x4c2d3b69 +0, 26, 26, 1, 90000, 0x22c26e0c +0, 27, 27, 1, 90000, 0x254bb498 +0, 28, 28, 1, 90000, 0xcd02a4ac +0, 29, 29, 1, 90000, 0x54447feb +0, 30, 30, 1, 90000, 0x9dc87b6e +0, 31, 31, 1, 90000, 0xfb49553b +0, 32, 32, 1, 90000, 0x05316fbe +0, 33, 33, 1, 90000, 0xd796da15 +0, 34, 34, 1, 90000, 0x36064111 +0, 35, 35, 1, 90000, 0x470cb58e +0, 36, 36, 1, 90000, 0xf0ba8054 +0, 37, 37, 1, 90000, 0x3f496ff8 +0, 38, 38, 1, 90000, 0xe3a43cff +0, 39, 39, 1, 90000, 0xacdd36ce -- 2.33.0 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".