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 "qffmpegavaudioformat_p.h" |
5 | #include "qaudioformat.h" |
6 | #include "qffmpegmediaformatinfo_p.h" |
7 | |
8 | #include <QtCore/qdebug.h> |
9 | |
10 | extern "C"{ |
11 | #include <libavutil/opt.h> |
12 | } |
13 | |
14 | QT_BEGIN_NAMESPACE |
15 | |
16 | namespace QFFmpeg { |
17 | |
18 | AVAudioFormat::AVAudioFormat(const AVCodecContext *context) |
19 | : sampleFormat(context->sample_fmt), sampleRate(context->sample_rate) |
20 | { |
21 | #if QT_FFMPEG_HAS_AV_CHANNEL_LAYOUT |
22 | channelLayout = context->ch_layout; |
23 | #else |
24 | if (context->channel_layout) { |
25 | channelLayoutMask = context->channel_layout; |
26 | } else { |
27 | const auto channelConfig = |
28 | QAudioFormat::defaultChannelConfigForChannelCount(channelCount: context->channels); |
29 | channelLayoutMask = QFFmpegMediaFormatInfo::avChannelLayout(channelConfig); |
30 | } |
31 | #endif |
32 | } |
33 | |
34 | AVAudioFormat::AVAudioFormat(const AVCodecParameters *codecPar) |
35 | : sampleFormat(AVSampleFormat(codecPar->format)), sampleRate(codecPar->sample_rate) |
36 | { |
37 | #if QT_FFMPEG_HAS_AV_CHANNEL_LAYOUT |
38 | channelLayout = codecPar->ch_layout; |
39 | #else |
40 | if (codecPar->channel_layout) { |
41 | channelLayoutMask = codecPar->channel_layout; |
42 | } else { |
43 | const auto channelConfig = |
44 | QAudioFormat::defaultChannelConfigForChannelCount(channelCount: codecPar->channels); |
45 | channelLayoutMask = QFFmpegMediaFormatInfo::avChannelLayout(channelConfig); |
46 | } |
47 | #endif |
48 | } |
49 | |
50 | AVAudioFormat::AVAudioFormat(const QAudioFormat &audioFormat) |
51 | : sampleFormat(QFFmpegMediaFormatInfo::avSampleFormat(format: audioFormat.sampleFormat())), |
52 | sampleRate(audioFormat.sampleRate()) |
53 | { |
54 | const auto channelConfig = audioFormat.channelConfig() == QAudioFormat::ChannelConfigUnknown |
55 | ? QAudioFormat::defaultChannelConfigForChannelCount(channelCount: audioFormat.channelCount()) |
56 | : audioFormat.channelConfig(); |
57 | |
58 | const auto mask = QFFmpegMediaFormatInfo::avChannelLayout(channelConfig); |
59 | |
60 | #if QT_FFMPEG_HAS_AV_CHANNEL_LAYOUT |
61 | av_channel_layout_from_mask(&channelLayout, mask); |
62 | #else |
63 | channelLayoutMask = mask; |
64 | #endif |
65 | } |
66 | |
67 | bool operator==(const AVAudioFormat &lhs, const AVAudioFormat &rhs) |
68 | { |
69 | return lhs.sampleFormat == rhs.sampleFormat && lhs.sampleRate == rhs.sampleRate && |
70 | #if QT_FFMPEG_HAS_AV_CHANNEL_LAYOUT |
71 | lhs.channelLayout == rhs.channelLayout |
72 | #else |
73 | lhs.channelLayoutMask == rhs.channelLayoutMask |
74 | #endif |
75 | ; |
76 | } |
77 | |
78 | QDebug operator<<(QDebug dbg, const AVAudioFormat &format) |
79 | { |
80 | dbg << '['; |
81 | const char *sampleFormatName = av_get_sample_fmt_name(sample_fmt: format.sampleFormat); |
82 | dbg << "sample format:"<< (sampleFormatName ? sampleFormatName : "unknown"); |
83 | dbg << ", sample rate:"<< format.sampleRate; |
84 | |
85 | #if QT_FFMPEG_HAS_AV_CHANNEL_LAYOUT |
86 | dbg << ", channel layout:"<< format.channelLayout; |
87 | #else |
88 | dbg << "channel layout:"<< Qt::bin << format.channelLayoutMask << Qt::dec; |
89 | #endif |
90 | dbg << ']'; |
91 | return dbg; |
92 | } |
93 | |
94 | } // namespace QFFmpeg |
95 | |
96 | QT_END_NAMESPACE |
97 |