diff options
Diffstat (limited to 'src/plugins/multimedia/windows/qwindowsformatinfo.cpp')
-rw-r--r-- | src/plugins/multimedia/windows/qwindowsformatinfo.cpp | 165 |
1 files changed, 64 insertions, 101 deletions
diff --git a/src/plugins/multimedia/windows/qwindowsformatinfo.cpp b/src/plugins/multimedia/windows/qwindowsformatinfo.cpp index 13f87161e..6ef1f7f7f 100644 --- a/src/plugins/multimedia/windows/qwindowsformatinfo.cpp +++ b/src/plugins/multimedia/windows/qwindowsformatinfo.cpp @@ -1,116 +1,85 @@ -/**************************************************************************** -** -** Copyright (C) 2021 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2021 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #include "qwindowsformatinfo_p.h" #include <mfapi.h> #include <mftransform.h> -#include <private/qwindowsiupointer_p.h> +#include <private/qcomptr_p.h> #include <private/qwindowsmultimediautils_p.h> +#include <private/qcomtaskresource_p.h> #include <QtCore/qlist.h> #include <QtCore/qset.h> +#include <QtCore/qhash.h> #include <QtGui/qimagewriter.h> QT_BEGIN_NAMESPACE +namespace { + template<typename T> -static T codecForFormat(GUID format) = delete; +using CheckedCodecs = QHash<QPair<T, QMediaFormat::ConversionMode>, bool>; -template<> -QMediaFormat::AudioCodec codecForFormat(GUID format) +bool isSupportedMFT(const GUID &category, const MFT_REGISTER_TYPE_INFO &type, QMediaFormat::ConversionMode mode) { - return QWindowsMultimediaUtils::codecForAudioFormat(format); + UINT32 count = 0; + IMFActivate **activateArrayRaw = nullptr; + HRESULT hr = MFTEnumEx( + category, + MFT_ENUM_FLAG_ALL, + (mode == QMediaFormat::Encode) ? nullptr : &type, // Input type + (mode == QMediaFormat::Encode) ? &type : nullptr, // Output type + &activateArrayRaw, + &count + ); + + if (FAILED(hr)) + return false; + + QComTaskResource<IMFActivate *[], QComDeleter> activateArray(activateArrayRaw, count); + for (UINT32 i = 0; i < count; ++i) { + ComPtr<IMFTransform> transform; + hr = activateArray[i]->ActivateObject(IID_PPV_ARGS(transform.GetAddressOf())); + if (SUCCEEDED(hr)) + return true; + } + + return false; } -template<> -QMediaFormat::VideoCodec codecForFormat(GUID format) +bool isSupportedCodec(QMediaFormat::AudioCodec codec, QMediaFormat::ConversionMode mode) { - return QWindowsMultimediaUtils::codecForVideoFormat(format); + return isSupportedMFT((mode == QMediaFormat::Encode) ? MFT_CATEGORY_AUDIO_ENCODER : MFT_CATEGORY_AUDIO_DECODER, + { MFMediaType_Audio, QWindowsMultimediaUtils::audioFormatForCodec(codec) }, + mode); } -template<typename T> -static QSet<T> getCodecSet(GUID category) +bool isSupportedCodec(QMediaFormat::VideoCodec codec, QMediaFormat::ConversionMode mode) { - QSet<T> codecSet; - IMFActivate **activateArray = nullptr; - UINT32 num = 0; - - HRESULT hr = MFTEnumEx(category, MFT_ENUM_FLAG_ALL, nullptr, nullptr, &activateArray, &num); - - if (SUCCEEDED(hr)) { - for (UINT32 i = 0; i < num; ++i) { - QWindowsIUPointer<IMFTransform> transform; - UINT32 typeIndex = 0; - - hr = activateArray[i]->ActivateObject(IID_PPV_ARGS(transform.address())); - - while (SUCCEEDED(hr)) { - QWindowsIUPointer<IMFMediaType> mediaType; - - if (category == MFT_CATEGORY_AUDIO_ENCODER || category == MFT_CATEGORY_VIDEO_ENCODER) - hr = transform->GetOutputAvailableType(0, typeIndex++, mediaType.address()); - else - hr = transform->GetInputAvailableType(0, typeIndex++, mediaType.address()); - - if (SUCCEEDED(hr)) { - GUID subtype = GUID_NULL; - hr = mediaType->GetGUID(MF_MT_SUBTYPE, &subtype); - if (SUCCEEDED(hr)) - codecSet.insert(codecForFormat<T>(subtype)); - } - } - } + return isSupportedMFT((mode == QMediaFormat::Encode) ? MFT_CATEGORY_VIDEO_ENCODER : MFT_CATEGORY_VIDEO_DECODER, + { MFMediaType_Video, QWindowsMultimediaUtils::videoFormatForCodec(codec) }, + mode); +} + +template <typename T> +bool isSupportedCodec(T codec, QMediaFormat::ConversionMode m, CheckedCodecs<T> &checkedCodecs) +{ + if (auto it = checkedCodecs.constFind(qMakePair(codec, m)); it != checkedCodecs.constEnd()) + return it.value(); - for (UINT32 i = 0; i < num; ++i) - activateArray[i]->Release(); + const bool supported = isSupportedCodec(codec, m); - CoTaskMemFree(activateArray); - } + checkedCodecs.insert(qMakePair(codec, m), supported); + return supported; +} - return codecSet; } static QList<QImageCapture::FileFormat> getImageFormatList() { QList<QImageCapture::FileFormat> list; - auto formats = QImageWriter::supportedImageFormats(); + const auto formats = QImageWriter::supportedImageFormats(); for (const auto &f : formats) { auto format = QString::fromUtf8(f); @@ -185,31 +154,25 @@ QWindowsFormatInfo::QWindowsFormatInfo() QMediaFormat::WMV, }; - const auto audioDecoders = getCodecSet<QMediaFormat::AudioCodec>(MFT_CATEGORY_AUDIO_DECODER); - const auto audioEncoders = getCodecSet<QMediaFormat::AudioCodec>(MFT_CATEGORY_AUDIO_ENCODER); - const auto videoDecoders = getCodecSet<QMediaFormat::VideoCodec>(MFT_CATEGORY_VIDEO_DECODER); - const auto videoEncoders = getCodecSet<QMediaFormat::VideoCodec>(MFT_CATEGORY_VIDEO_ENCODER); + CheckedCodecs<QMediaFormat::AudioCodec> checkedAudioCodecs; + CheckedCodecs<QMediaFormat::VideoCodec> checkedVideoCodecs; - for (const auto &codecMap : containerTable) { - - const QSet<QMediaFormat::AudioCodec> mapAudioSet(codecMap.audio.cbegin(), codecMap.audio.cend()); - const QSet<QMediaFormat::VideoCodec> mapVideoSet(codecMap.video.cbegin(), codecMap.video.cend()); + auto ensureCodecs = [&] (CodecMap &codecs, QMediaFormat::ConversionMode mode) { + codecs.audio.removeIf([&] (auto codec) { return !isSupportedCodec(codec, mode, checkedAudioCodecs); }); + codecs.video.removeIf([&] (auto codec) { return !isSupportedCodec(codec, mode, checkedVideoCodecs); }); + return !codecs.video.empty() || !codecs.audio.empty(); + }; + for (const auto &codecMap : containerTable) { if (decoderFormats.contains(codecMap.format)) { - CodecMap m; - m.format = codecMap.format; - m.audio = (audioDecoders & mapAudioSet).values(); - m.video = (videoDecoders & mapVideoSet).values(); - if (!m.video.empty() || !m.audio.empty()) + auto m = codecMap; + if (ensureCodecs(m, QMediaFormat::Decode)) decoders.append(m); } if (encoderFormats.contains(codecMap.format)) { - CodecMap m; - m.format = codecMap.format; - m.audio = (audioEncoders & mapAudioSet).values(); - m.video = (videoEncoders & mapVideoSet).values(); - if (!m.video.empty() || !m.audio.empty()) + auto m = codecMap; + if (ensureCodecs(m, QMediaFormat::Encode)) encoders.append(m); } } |