Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qffmpegaudioencoderutils.cpp
Go to the documentation of this file.
1// Copyright (C) 2024 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5#include "qalgorithms.h"
6
8
9namespace QFFmpeg {
10
11AVSampleFormat adjustSampleFormat(const AVSampleFormat *supportedFormats, AVSampleFormat requested)
12{
13 auto calcScore = [requested](AVSampleFormat format) {
14 return format == requested ? BestAVScore
15 : format == av_get_planar_sample_fmt(requested) ? BestAVScore - 1
16 : 0;
17 };
18
19 const auto result = findBestAVFormat(supportedFormats, calcScore).first;
20 return result == AV_SAMPLE_FMT_NONE ? requested : result;
21}
22
23int adjustSampleRate(const int *supportedRates, int requested)
24{
25 auto calcScore = [requested](int rate) {
26 return requested == rate ? BestAVScore
27 : requested <= rate ? rate - requested
28 : requested - rate - 1000000;
29 };
30
31 const auto result = findBestAVValue(supportedRates, calcScore).first;
32 return result == 0 ? requested : result;
33}
34
35static AVScore calculateScoreByChannelsCount(int supportedChannelsNumber,
36 int requestedChannelsNumber)
37{
38 if (supportedChannelsNumber >= requestedChannelsNumber)
39 return requestedChannelsNumber - supportedChannelsNumber;
40
41 return supportedChannelsNumber - requestedChannelsNumber - 10000;
42}
43
44static AVScore calculateScoreByChannelsMask(int supportedChannelsNumber, uint64_t supportedMask,
45 int requestedChannelsNumber, uint64_t requestedMask)
46{
47 if ((supportedMask & requestedMask) == requestedMask)
48 return BestAVScore - qPopulationCount(supportedMask & ~requestedMask);
49
50 return calculateScoreByChannelsCount(supportedChannelsNumber, requestedChannelsNumber);
51}
52
53#if QT_FFMPEG_OLD_CHANNEL_LAYOUT
54
55uint64_t adjustChannelLayout(const uint64_t *supportedMasks, uint64_t requested)
56{
57 auto calcScore = [requested](uint64_t mask) {
59 qPopulationCount(requested), requested);
60 };
61
62 const auto result = findBestAVValue(supportedMasks, calcScore).first;
63 return result == 0 ? requested : result;
64}
65
66#else
67
68AVChannelLayout adjustChannelLayout(const AVChannelLayout *supportedLayouts,
69 const AVChannelLayout &requested)
70{
71 auto calcScore = [&requested](const AVChannelLayout &layout) {
72 if (layout == requested)
73 return BestAVScore;
74
75 // The only realistic case for now:
76 // layout.order == requested.order == AV_CHANNEL_ORDER_NATIVE
77 // Let's consider other orders to make safe code
78
79 if (layout.order == AV_CHANNEL_ORDER_CUSTOM || requested.order == AV_CHANNEL_ORDER_CUSTOM)
80 return calculateScoreByChannelsCount(layout.nb_channels, requested.nb_channels) - 1000;
81
82 const auto offset = layout.order == requested.order ? 1 : 100;
83
84 return calculateScoreByChannelsMask(layout.nb_channels, layout.u.mask,
85 requested.nb_channels, requested.u.mask)
86 - offset;
87 };
88
89 const auto result = findBestAVValue(supportedLayouts, calcScore);
90 return result.second == NotSuitableAVScore ? requested : result.first;
91}
92
93#endif
94
95} // namespace QFFmpeg
96
constexpr AVScore BestAVScore
Definition qffmpeg_p.h:159
std::pair< Format, AVScore > findBestAVFormat(const Format *fmts, const CalculateScore &calculateScore)
Definition qffmpeg_p.h:210
static AVScore calculateScoreByChannelsCount(int supportedChannelsNumber, int requestedChannelsNumber)
AVSampleFormat adjustSampleFormat(const AVSampleFormat *supportedFormats, AVSampleFormat requested)
int AVScore
Definition qffmpeg_p.h:158
static AVScore calculateScoreByChannelsMask(int supportedChannelsNumber, uint64_t supportedMask, int requestedChannelsNumber, uint64_t requestedMask)
AVChannelLayout adjustChannelLayout(const AVChannelLayout *supportedLayouts, const AVChannelLayout &requested)
int adjustSampleRate(const int *supportedRates, int requested)
constexpr AVScore NotSuitableAVScore
Definition qffmpeg_p.h:161
auto findBestAVValue(const Value *values, const CalculateScore &calculateScore, Value invalidValue={})
Definition qffmpeg_p.h:193
Combined button and popup list for selecting options.
Q_DECL_CONST_FUNCTION QT_POPCOUNT_CONSTEXPR uint qPopulationCount(quint32 v) noexcept
GLenum GLuint GLintptr offset
GLint GLint GLint GLint GLint GLint GLint GLbitfield mask
GLint GLsizei GLsizei GLenum format
GLuint GLenum * rate
GLuint64EXT * result
[6]
QVBoxLayout * layout