Skip to content

Commit c8efbf6

Browse files
FFmpeg 7 channel layout source refactoring
1 parent 4ecc4d4 commit c8efbf6

File tree

12 files changed

+40
-26
lines changed

12 files changed

+40
-26
lines changed

Sources/FFmpeg/Utils/FFmpegChannelLayoutsMapper.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,21 +125,25 @@ struct FFmpegChannelLayoutsMapper {
125125
/// - returns: A corresponding AVFoundation channel layout, if there exists a mapping for the given
126126
/// ffmpeg channel layout. Nil otherwise.
127127
///
128-
static func mapLayout(ffmpegLayout: Int) -> AVAudioChannelLayout? {
128+
static func mapLayout(ffmpegLayout: AVChannelLayout) -> AVAudioChannelLayout? {
129129

130-
if let layoutTag = layoutsMap[ffmpegLayout] {
130+
if let layoutTag = layoutsMap[Int(ffmpegLayout.u.mask)] {
131131
return AVAudioChannelLayout(layoutTag: layoutTag)
132132
}
133133

134134
return nil
135135
}
136136

137-
static func readableString(for channelLayout: Int64, channelCount: Int32) -> String {
137+
static func readableString(for channelLayout: AVChannelLayout, channelCount: Int32) -> String {
138138

139139
let layoutStringPointer = UnsafeMutablePointer<Int8>.allocate(capacity: 100)
140140
defer {layoutStringPointer.deallocate()}
141141

142-
av_get_channel_layout_string(layoutStringPointer, 100, channelCount, UInt64(channelLayout))
142+
withUnsafePointer(to: channelLayout) {ptr -> Void in
143+
av_channel_layout_describe(ptr, layoutStringPointer, 100)
144+
}
145+
146+
// av_get_channel_layout_string(layoutStringPointer, 100, channelCount, UInt64(channelLayout))
143147
return String(cString: layoutStringPointer).replacingOccurrences(of: "(", with: " (").capitalized
144148
}
145149
}

Sources/FFmpeg/Utils/FFmpegDecoder.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,10 @@ class FFmpegDecoder {
7474

7575
let resampleCtx: FFmpegAVAEResamplingContext?
7676

77-
private(set) lazy var audioFormat: FFmpegAudioFormat = FFmpegAudioFormat(sampleRate: codec.sampleRate, channelCount: codec.channelCount, channelLayout: codec.channelLayout, sampleFormat: codec.sampleFormat)
77+
private(set) lazy var audioFormat: FFmpegAudioFormat = FFmpegAudioFormat(sampleRate: codec.sampleRate,
78+
channelCount: codec.channelCount,
79+
channelLayout: codec.channelLayout,
80+
sampleFormat: codec.sampleFormat)
7881

7982
private(set) lazy var channelCount: Int = Int(audioFormat.channelCount)
8083

Sources/FFmpeg/Wrappers/AudioFileContext.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ class AudioFileContext {
125125
self.sampleRate = sampleRateDouble
126126
self.frameCount = Int64(sampleRateDouble * theFormatContext.duration)
127127

128-
let channelLayout: AVAudioChannelLayout = FFmpegChannelLayoutsMapper.mapLayout(ffmpegLayout: Int(codec.channelLayout)) ?? .stereo
128+
let channelLayout: AVAudioChannelLayout = FFmpegChannelLayoutsMapper.mapLayout(ffmpegLayout: codec.channelLayout) ?? .stereo
129129

130130
self.audioFormat = AVAudioFormat(standardFormatWithSampleRate: sampleRateDouble, channelLayout: channelLayout)
131131

Sources/FFmpeg/Wrappers/FFmpegAudioCodec.swift

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class FFmpegAudioCodec: FFmpegCodec {
5252
///
5353
/// Describes the number and physical / spatial arrangement of the channels. (e.g. "5.1 surround" or "stereo")
5454
///
55-
var channelLayout: Int64 = 0
55+
var channelLayout: AVChannelLayout = .init()
5656

5757
///
5858
/// Instantiates an AudioCodec object, given a pointer to its parameters.
@@ -63,12 +63,13 @@ class FFmpegAudioCodec: FFmpegCodec {
6363

6464
try super.init(fromParameters: paramsPointer)
6565

66-
self.sampleFormat = FFmpegSampleFormat(encapsulating: context.sample_fmt)
67-
self.channelCount = params.channels
68-
6966
// Correct channel layout if necessary.
7067
// NOTE - This is necessary for some files like WAV files that don't specify a channel layout.
71-
self.channelLayout = context.channel_layout != 0 ? Int64(context.channel_layout) : av_get_default_channel_layout(context.channels)
68+
// self.channelLayout = context.channel_layout != 0 ? Int64(context.channel_layout) : av_get_default_channel_layout(context.channels)
69+
self.channelLayout = context.ch_layout
70+
71+
self.sampleFormat = FFmpegSampleFormat(encapsulating: context.sample_fmt)
72+
self.channelCount = params.ch_layout.nb_channels
7273

7374
// Use multithreading to speed up decoding.
7475
self.contextPointer.pointee.thread_count = Self.threadCount
@@ -83,7 +84,8 @@ class FFmpegAudioCodec: FFmpegCodec {
8384
// Some streams may contain the wrong header information. So, recompute these
8485
// values after opening the codec.
8586

86-
self.channelLayout = context.channel_layout != 0 ? Int64(context.channel_layout) : av_get_default_channel_layout(context.channels)
87+
// self.channelLayout = context.channel_layout != 0 ? Int64(context.channel_layout) : av_get_default_channel_layout(context.channels)
88+
self.channelLayout = context.ch_layout
8789
self.sampleFormat = FFmpegSampleFormat(encapsulating: context.sample_fmt)
8890
}
8991

Sources/FFmpeg/Wrappers/FFmpegAudioFormat.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ struct FFmpegAudioFormat {
2323
let channelCount: Int32
2424

2525
/// An ffmpeg identifier for the physical / spatial layout of channels. eg. "5.1 surround" or "stereo".
26-
let channelLayout: Int64
26+
let channelLayout: AVChannelLayout
2727

2828
/// PCM sample format
2929
let sampleFormat: FFmpegSampleFormat

Sources/FFmpeg/Wrappers/FFmpegAudioStream.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ class FFmpegAudioStream: FFmpegStreamProtocol {
8282

8383
var sampleRate: Int32 {codecParams.sample_rate}
8484

85-
var channelCount: Int32 {codecParams.channels}
85+
var channelCount: Int32 {codecParams.ch_layout.nb_channels}
8686

87-
var channelLayout: UInt64 {codecParams.channel_layout}
87+
var channelLayout: UInt64 {codecParams.ch_layout.u.mask}
8888

8989
///
9090
/// All metadata key / value pairs available for this stream.

Sources/FFmpeg/Wrappers/FFmpegCodec.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class FFmpegCodec {
1818
///
1919
/// A pointer to the encapsulated AVCodec object.
2020
///
21-
var pointer: UnsafeMutablePointer<AVCodec>!
21+
var pointer: UnsafePointer<AVCodec>!
2222

2323
///
2424
/// The encapsulated AVCodec object.

Sources/FFmpeg/Wrappers/FFmpegFrame.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ class FFmpegFrame {
3030
///
3131
/// Describes the number and physical / spatial arrangement of the channels. (e.g. "5.1 surround" or "stereo")
3232
///
33-
var channelLayout: UInt64 {avFrame.channel_layout}
33+
var channelLayout: AVChannelLayout {avFrame.ch_layout}
3434

3535
///
3636
/// Number of channels of audio data.
3737
///
38-
var channelCount: Int32 {avFrame.channels}
38+
var channelCount: Int32 {avFrame.ch_layout.nb_channels}
3939

4040
lazy var intChannelCount: Int = Int(channelCount)
4141

Sources/FFmpeg/Wrappers/FFmpegResamplingContext.swift

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,25 +51,31 @@ class FFmpegResamplingContext {
5151
///
5252
/// The channel layout of the input samples.
5353
///
54-
var inputChannelLayout: Int64? {
54+
var inputChannelLayout: AVChannelLayout? {
5555

5656
didSet {
5757

5858
if let channelLayout = inputChannelLayout {
59-
av_opt_set_channel_layout(rawPointer, "in_channel_layout", channelLayout, 0)
59+
60+
withUnsafePointer(to: channelLayout) {ptr -> Void in
61+
av_opt_set_chlayout(rawPointer, "in_channel_layout", ptr, 0)
62+
}
6063
}
6164
}
6265
}
6366

6467
///
6568
/// The (desired) channel layout of the output samples.
6669
///
67-
var outputChannelLayout: Int64? {
70+
var outputChannelLayout: AVChannelLayout? {
6871

6972
didSet {
7073

7174
if let channelLayout = outputChannelLayout {
72-
av_opt_set_channel_layout(rawPointer, "out_channel_layout", channelLayout, 0)
75+
76+
withUnsafePointer(to: channelLayout) {ptr -> Void in
77+
av_opt_set_chlayout(rawPointer, "out_channel_layout", ptr, 0)
78+
}
7379
}
7480
}
7581
}
@@ -183,7 +189,7 @@ class FFmpegAVAEResamplingContext: FFmpegResamplingContext {
183189
///
184190
private static let standardSampleFormat: AVSampleFormat = AV_SAMPLE_FMT_FLTP
185191

186-
init?(channelLayout: Int64, sampleRate: Int64, inputSampleFormat: AVSampleFormat) {
192+
init?(channelLayout: AVChannelLayout, sampleRate: Int64, inputSampleFormat: AVSampleFormat) {
187193

188194
super.init()
189195

0 commit comments

Comments
 (0)