Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
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(QSpan<const AVSampleFormat> supportedFormats, AVSampleFormat requested)
12{
13 auto calcScore = [requested](AVSampleFormat format) {
14 if (format == requested)
15 return BestAVScore;
16 if (format == av_get_planar_sample_fmt(requested))
17 return BestAVScore - 1;
18
19 const int bps = av_get_bytes_per_sample(format);
20 const int bpsRequested = av_get_bytes_per_sample(requested);
21 // choose the closest one with higher bps
22 if (bps >= bpsRequested)
23 return DefaultAVScore - (bps - bpsRequested);
24
25 // choose the closest one with lower bps, considering a priority penalty
26 return DefaultAVScore - (bpsRequested - bps) - 1000000;
27 };
28
29 const auto result = findBestAVValue(supportedFormats, calcScore);
30 return result.value_or(requested);
31}
32
33int adjustSampleRate(QSpan<const int> supportedRates, int requested)
34{
35 auto calcScore = [requested](int rate) {
36 if (requested == rate)
37 return BestAVScore;
38
39 // choose the closest one with higher rate
40 if (rate >= requested)
41 return DefaultAVScore - (rate - requested);
42
43 // choose the closest one with lower rate, considering a priority penalty
44 return DefaultAVScore - (requested - rate) - 1000000;
45 };
46
47 const auto result = findBestAVValue(supportedRates, calcScore);
48 return result.value_or(requested);
49}
50
51static AVScore calculateScoreByChannelsCount(int supportedChannelsNumber,
52 int requestedChannelsNumber)
53{
54 // choose the closest one with higher channels number
55 if (supportedChannelsNumber >= requestedChannelsNumber)
56 return requestedChannelsNumber - supportedChannelsNumber;
57
58 // choose the closest one with lower channels number, considering a priority penalty
59 return supportedChannelsNumber - requestedChannelsNumber - 10000;
60}
61
62static AVScore calculateScoreByChannelsMask(int supportedChannelsNumber, uint64_t supportedMask,
63 int requestedChannelsNumber, uint64_t requestedMask)
64{
65 if ((supportedMask & requestedMask) == requestedMask)
66 return BestAVScore - qPopulationCount(supportedMask & ~requestedMask);
67
68 return calculateScoreByChannelsCount(supportedChannelsNumber, requestedChannelsNumber);
69}
70
71ChannelLayoutT adjustChannelLayout(QSpan<const ChannelLayoutT> supportedLayouts,
72 const ChannelLayoutT &requested)
73{
74#if QT_FFMPEG_HAS_AV_CHANNEL_LAYOUT
75 auto calcScore = [&requested](const ChannelLayoutT &layout) {
76 if (layout == requested)
77 return BestAVScore;
78
79 // The only realistic case for now:
80 // layout.order == requested.order == AV_CHANNEL_ORDER_NATIVE
81 // Let's consider other orders to make safe code
82
83 if (layout.order == AV_CHANNEL_ORDER_CUSTOM || requested.order == AV_CHANNEL_ORDER_CUSTOM)
84 return calculateScoreByChannelsCount(layout.nb_channels, requested.nb_channels) - 1000;
85
86 const auto offset = layout.order == requested.order ? 1 : 100;
87
88 return calculateScoreByChannelsMask(layout.nb_channels, layout.u.mask,
89 requested.nb_channels, requested.u.mask)
90 - offset;
91 };
92
93 const auto result = findBestAVValue(supportedLayouts, calcScore);
94 return result.value_or(requested);
95#else
96 auto calcScore = [requested](ChannelLayoutT mask) {
97 return calculateScoreByChannelsMask(qPopulationCount(mask), mask,
98 qPopulationCount(requested), requested);
99 };
100
101 const auto result = findBestAVValue(supportedLayouts, calcScore);
102 return result.value_or(requested);
103#endif
104}
105
106} // namespace QFFmpeg
107
108QT_END_NAMESPACE
AVSampleFormat adjustSampleFormat(QSpan< const AVSampleFormat > supportedFormats, AVSampleFormat requested)
static AVScore calculateScoreByChannelsCount(int supportedChannelsNumber, int requestedChannelsNumber)
static AVScore calculateScoreByChannelsMask(int supportedChannelsNumber, uint64_t supportedMask, int requestedChannelsNumber, uint64_t requestedMask)
std::conditional_t< QT_FFMPEG_AVIO_WRITE_CONST, const uint8_t *, uint8_t * > AvioWriteBufferType
ChannelLayoutT adjustChannelLayout(QSpan< const ChannelLayoutT > supportedLayouts, const ChannelLayoutT &requested)
int adjustSampleRate(QSpan< const int > supportedRates, int requested)