From 75c4dbbaaa4bc904c282d95766c782c4f153d119 Mon Sep 17 00:00:00 2001 From: Jonathan Thomas Date: Thu, 31 Dec 2020 17:35:49 -0600 Subject: [PATCH] Fixing alpha videos, by handling the conversion to premultiplied RGBA separately --- src/FFmpegReader.cpp | 8 +++++++- src/FFmpegUtilities.h | 10 ++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/FFmpegReader.cpp b/src/FFmpegReader.cpp index 02bb931ea..01f371582 100644 --- a/src/FFmpegReader.cpp +++ b/src/FFmpegReader.cpp @@ -1352,7 +1352,13 @@ void FFmpegReader::ProcessVideoPacket(int64_t requested_frame) { std::shared_ptr f = CreateFrame(current_frame); // Add Image data to frame - f->AddImage(width, height, 4, QImage::Format_RGBA8888_Premultiplied, buffer); + if (!ffmpeg_has_alpha(AV_GET_CODEC_PIXEL_FORMAT(pStream, pCodecCtx))) { + // Add image with no alpha channel, Speed optimization + f->AddImage(width, height, 4, QImage::Format_RGBA8888_Premultiplied, buffer); + } else { + // Add image with alpha channel (this will be converted to premultipled when needed, but is slower) + f->AddImage(width, height, 4, QImage::Format_RGBA8888, buffer); + } // Update working cache working_cache.Add(f); diff --git a/src/FFmpegUtilities.h b/src/FFmpegUtilities.h index c637f9445..c1a7ccd37 100644 --- a/src/FFmpegUtilities.h +++ b/src/FFmpegUtilities.h @@ -126,6 +126,16 @@ #define PIX_FMT_YUV444P AV_PIX_FMT_YUV444P #endif + // Does ffmpeg pixel format contain an alpha channel? + inline static const bool ffmpeg_has_alpha(PixelFormat pix_fmt) + { + if (pix_fmt == AV_PIX_FMT_ARGB || pix_fmt == AV_PIX_FMT_RGBA || pix_fmt == AV_PIX_FMT_ABGR || pix_fmt == AV_PIX_FMT_BGRA || pix_fmt == AV_PIX_FMT_YUVA420P) { + return true; + } else { + return false; + } + } + // FFmpeg's libavutil/common.h defines an RSHIFT incompatible with Ruby's // definition in ruby/config.h, so we move it to FF_RSHIFT #ifdef RSHIFT