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
4#include "qffmpegaudioencoderutils_p.h"
5#include "qalgorithms.h"
6
7QT_BEGIN_NAMESPACE
8
9namespace QFFmpeg {
10
11AVSampleFormat adjustSampleFormat(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(sample_fmt: requested))
17 return BestAVScore - 1;
18
19 const int bps = av_get_bytes_per_sample(sample_fmt: format);
20 const int bpsRequested = av_get_bytes_per_sample(sample_fmt: 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(values: supportedFormats, calculateScore: calcScore).first;
30 return result == AV_SAMPLE_FMT_NONE ? requested : result;
31}
32
33int adjustSampleRate(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(values: supportedRates, calculateScore: calcScore).first;
48 return result == 0 ? requested : result;
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(v: supportedMask & ~requestedMask);
67
68 return calculateScoreByChannelsCount(supportedChannelsNumber, requestedChannelsNumber);
69}
70
71#if QT_FFMPEG_HAS_AV_CHANNEL_LAYOUT
72
73AVChannelLayout adjustChannelLayout(const AVChannelLayout *supportedLayouts,
74 const AVChannelLayout &requested)
75{
76 auto calcScore = [&requested](const AVChannelLayout &layout) {
77 if (layout == requested)
78 return BestAVScore;
79
80 // The only realistic case for now:
81 // layout.order == requested.order == AV_CHANNEL_ORDER_NATIVE
82 // Let's consider other orders to make safe code
83
84 if (layout.order == AV_CHANNEL_ORDER_CUSTOM || requested.order == AV_CHANNEL_ORDER_CUSTOM)
85 return calculateScoreByChannelsCount(layout.nb_channels, requested.nb_channels) - 1000;
86
87 const auto offset = layout.order == requested.order ? 1 : 100;
88
89 return calculateScoreByChannelsMask(layout.nb_channels, layout.u.mask,
90 requested.nb_channels, requested.u.mask)
91 - offset;
92 };
93
94 const auto result = findBestAVValue(supportedLayouts, calcScore);
95 return result.second == NotSuitableAVScore ? requested : result.first;
96}
97
98#else
99
100uint64_t adjustChannelLayout(const uint64_t *supportedMasks, uint64_t requested)
101{
102 auto calcScore = [requested](uint64_t mask) {
103 return calculateScoreByChannelsMask(supportedChannelsNumber: qPopulationCount(v: mask), supportedMask: mask,
104 requestedChannelsNumber: qPopulationCount(v: requested), requestedMask: requested);
105 };
106
107 const auto result = findBestAVValue(values: supportedMasks, calculateScore: calcScore).first;
108 return result == 0 ? requested : result;
109}
110
111#endif
112
113} // namespace QFFmpeg
114
115QT_END_NAMESPACE
116

source code of qtmultimedia/src/plugins/multimedia/ffmpeg/recordingengine/qffmpegaudioencoderutils.cpp