tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

commit b00727bfd19041149e87ee14e351f2fb7268563e
parent 2be6b6c7d802ce276ca383898c03b741525bacf3
Author: Andrew Osmond <aosmond@gmail.com>
Date:   Thu,  8 Jan 2026 15:56:03 +0000

Bug 2008354 - Part 1. Ensure we select MediaCodec encoders when appropriate on Android. r=media-playback-reviewers,jolin,padenot

This patch makes it so that we only select the platform MediaCodec
encoders for H264, HEVC and when there is a hardware implementation for
VP8, VP9 and AV1.

It also makes it so that we report accurately on whether the underlying
encoder encoder is supported by hardware. This makes us pass more gtests
because the tests need to know if it is H264 software or hardware.

Differential Revision: https://phabricator.services.mozilla.com/D277796

Diffstat:
Mdom/media/platforms/ffmpeg/FFmpegVideoEncoder.cpp | 34++++++++++++++++++++++++++++++++--
Mdom/media/platforms/ffmpeg/FFmpegVideoEncoder.h | 9++++++++-
2 files changed, 40 insertions(+), 3 deletions(-)

diff --git a/dom/media/platforms/ffmpeg/FFmpegVideoEncoder.cpp b/dom/media/platforms/ffmpeg/FFmpegVideoEncoder.cpp @@ -12,6 +12,7 @@ #include "BufferReader.h" #include "EncoderConfig.h" +#include "FFmpegEncoderModule.h" #include "FFmpegLog.h" #include "FFmpegRuntimeLinker.h" #include "FFmpegUtils.h" @@ -25,6 +26,9 @@ #include "mozilla/dom/ImageBitmapBinding.h" #include "mozilla/dom/ImageUtils.h" #include "mozilla/dom/VideoFrameBinding.h" +#ifdef MOZ_WIDGET_ANDROID +#include "mozilla/gfx/gfxVars.h" +#endif #include "nsPrintfCString.h" // The ffmpeg namespace is introduced to avoid the PixelFormat's name conflicts @@ -322,10 +326,18 @@ bool FFmpegVideoEncoder<LIBAV_VER>::ShouldTryHardware() const { // On Android, the MediaCodec encoders are the only ones available to us, // which may be implemented in hardware or software. if (mCodecID == AV_CODEC_ID_H264 || mCodecID == AV_CODEC_ID_HEVC) { - return true; + return StaticPrefs::media_ffvpx_hw_enabled(); } #endif - return mConfig.mHardwarePreference != HardwarePreference::RequireSoftware; + + if (mConfig.mHardwarePreference == HardwarePreference::RequireSoftware) { + return false; + } + + RefPtr<PlatformEncoderModule> pem = + FFmpegEncoderModule<LIBAV_VER>::Create(mLib); + return pem->SupportsCodec(mConfig.mCodec) + .contains(media::EncodeSupport::HardwareEncode); } MediaResult FFmpegVideoEncoder<LIBAV_VER>::InitEncoder() { @@ -354,6 +366,24 @@ MediaResult FFmpegVideoEncoder<LIBAV_VER>::InitEncoderInternal(bool aHardware) { mCodecContext = r.unwrap(); mCodecName = mCodecContext->codec->name; +#ifdef MOZ_WIDGET_ANDROID + // We need to create a MediaCodec encoder for H264/HEVC but it may or may not + // be backed by actual hardware. + switch (mCodecID) { + case AV_CODEC_ID_H264: + mIsHardwareAccelerated = aHardware && gfx::gfxVars::UseH264HwEncode(); + break; + case AV_CODEC_ID_HEVC: + mIsHardwareAccelerated = aHardware && gfx::gfxVars::UseHEVCHwEncode(); + break; + default: + mIsHardwareAccelerated = aHardware; + break; + } +#else + mIsHardwareAccelerated = aHardware; +#endif + // And now the video-specific part mCodecContext->pix_fmt = ffmpeg::FFMPEG_PIX_FMT_YUV420P; // // TODO: do this properly, based on the colorspace of the frame. Setting diff --git a/dom/media/platforms/ffmpeg/FFmpegVideoEncoder.h b/dom/media/platforms/ffmpeg/FFmpegVideoEncoder.h @@ -21,7 +21,8 @@ template <int V> class FFmpegVideoEncoder : public FFmpegDataEncoder<V> {}; template <> -class FFmpegVideoEncoder<LIBAV_VER> : public FFmpegDataEncoder<LIBAV_VER> { +class FFmpegVideoEncoder<LIBAV_VER> final + : public FFmpegDataEncoder<LIBAV_VER> { using PtsMap = SimpleMap<int64_t, int64_t, NoOpPolicy>; public: @@ -35,6 +36,10 @@ class FFmpegVideoEncoder<LIBAV_VER> : public FFmpegDataEncoder<LIBAV_VER> { nsCString GetDescriptionName() const override; + bool IsHardwareAccelerated(nsACString& aFailureReason) const override { + return mIsHardwareAccelerated; + } + protected: virtual ~FFmpegVideoEncoder() = default; // Methods only called on mTaskQueue. @@ -73,6 +78,8 @@ class FFmpegVideoEncoder<LIBAV_VER> : public FFmpegDataEncoder<LIBAV_VER> { uint8_t CurrentTemporalLayerId(); }; Maybe<SVCInfo> mSVCInfo{}; + // Can be accessed on any thread, but only written on during init. + Atomic<bool> mIsHardwareAccelerated{false}; // Some codecs use the input frames pts for rate control. We'd rather only use // the duration. Synthetize fake pts based on integrating over the duration of // input frames.