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 | |
7 | QT_BEGIN_NAMESPACE |
8 | |
9 | namespace QFFmpeg { |
10 | |
11 | AVSampleFormat 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(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); |
30 | return result.value_or(u&: requested); |
31 | } |
32 | |
33 | int 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(values: supportedRates, calculateScore: calcScore); |
48 | return result.value_or(u&: requested); |
49 | } |
50 | |
51 | static 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 | |
62 | static 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 | ChannelLayoutT 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(supportedChannelsNumber: qPopulationCount(v: mask), supportedMask: mask, |
98 | requestedChannelsNumber: qPopulationCount(v: requested), requestedMask: requested); |
99 | }; |
100 | |
101 | const auto result = findBestAVValue(values: supportedLayouts, calculateScore: calcScore); |
102 | return result.value_or(u: requested); |
103 | #endif |
104 | } |
105 | |
106 | } // namespace QFFmpeg |
107 | |
108 | QT_END_NAMESPACE |
109 | |