-
-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathFFmpegDecoder.cs
More file actions
157 lines (131 loc) · 4.85 KB
/
FFmpegDecoder.cs
File metadata and controls
157 lines (131 loc) · 4.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using SoundFlow.Codecs.FFMpeg.Enums;
using SoundFlow.Codecs.FFMpeg.Exceptions;
using SoundFlow.Enums;
using SoundFlow.Interfaces;
using SoundFlow.Structs;
using SoundFlow.Codecs.FFMpeg.Native;
using SoundFlow.Utils;
namespace SoundFlow.Codecs.FFMpeg;
/// <summary>
/// An <see cref="ISoundDecoder"/> implementation that uses a native FFmpeg wrapper
/// to decode various audio formats.
/// </summary>
internal sealed class FFmpegDecoder : ISoundDecoder
{
private readonly SafeDecoderHandle _handle;
private readonly Stream _stream;
private readonly FFmpeg.ReadCallback _readCallback;
private readonly FFmpeg.SeekCallback _seekCallback;
/// <summary>
/// Initializes a new instance of the <see cref="FFmpegDecoder"/> class.
/// </summary>
/// <param name="stream">The input stream containing the audio data to decode.</param>
/// <param name="targetFormat">The desired output format for the decoded PCM data.</param>
public FFmpegDecoder(Stream stream, AudioFormat targetFormat)
{
_stream = stream;
_readCallback = OnRead;
_seekCallback = OnSeek;
_handle = FFmpeg.CreateDecoder();
if (_handle.IsInvalid)
throw new InvalidOperationException("Failed to create FFmpeg decoder handle.");
var result = FFmpeg.InitializeDecoder(_handle, _readCallback, _seekCallback, IntPtr.Zero,
targetFormat.Format, out var nativeFormat, out var channels, out var sampleRate);
if (result != FFmpegResult.Success)
{
var logMessage = $"Failed to initialize FFmpeg decoder. Result: {result}";
Log.Error(logMessage);
_handle.Dispose();
throw new FFmpegException(result, logMessage);
}
SampleFormat = targetFormat.Format = nativeFormat;
Channels = targetFormat.Channels = (int)channels;
SampleRate = targetFormat.SampleRate = (int)sampleRate;
var lengthInFrames = FFmpeg.GetLengthInPcmFrames(_handle);
if (lengthInFrames < 0)
{
const string logMessage = "Failed to get stream length, the decoder handle may be invalid.";
Log.Error(logMessage);
_handle.Dispose();
throw new InvalidOperationException(logMessage);
}
Length = (int)(lengthInFrames * Channels);
}
/// <inheritdoc />
public bool IsDisposed => _handle.IsClosed;
/// <inheritdoc />
public int Length { get; }
/// <inheritdoc />
public SampleFormat SampleFormat { get; }
/// <inheritdoc />
public int Channels { get; }
/// <inheritdoc />
public int SampleRate { get; }
/// <inheritdoc />
public event EventHandler<EventArgs>? EndOfStreamReached;
/// <inheritdoc />
public unsafe int Decode(Span<float> samples)
{
if (IsDisposed || samples.IsEmpty) return 0;
var framesToRead = samples.Length / Channels;
long framesRead;
fixed (float* pSamples = samples)
{
var result = FFmpeg.ReadPcmFrames(_handle, (IntPtr)pSamples, framesToRead, out framesRead);
if (result != FFmpegResult.Success)
{
throw new FFmpegException(result, $"An unrecoverable error occurred during decoding. Result: {result}");
}
}
var samplesRead = (int)framesRead * Channels;
if (samplesRead == 0)
{
EndOfStreamReached?.Invoke(this, EventArgs.Empty);
}
return samplesRead;
}
/// <inheritdoc />
public bool Seek(int sampleOffset)
{
if (IsDisposed || !_stream.CanSeek) return false;
var frameIndex = sampleOffset / Channels;
var result = FFmpeg.SeekToPcmFrame(_handle, frameIndex);
return result == FFmpegResult.Success;
}
private unsafe nuint OnRead(IntPtr pUserData, IntPtr pBuffer, nuint bytesToRead)
{
try
{
var buffer = new Span<byte>((void*)pBuffer, (int)bytesToRead);
return (nuint)_stream.Read(buffer);
}
catch
{
Log.Critical("Failed to read from stream.");
// Signal error/EOF to FFmpeg by returning 0. FFmpeg will handle this gracefully as AVERROR_EOF.
return 0;
}
}
private long OnSeek(IntPtr pUserData, long offset, SeekWhence whence)
{
try
{
if (!_stream.CanSeek) return -1;
return _stream.Seek(offset, (SeekOrigin)whence);
}
catch
{
Log.Critical("Failed to seek stream.");
return -1;
}
}
/// <inheritdoc />
public void Dispose()
{
if (IsDisposed) return;
_handle.Dispose();
// Ensure the delegates are not collected while the native code might still be using them.
GC.KeepAlive(_readCallback);
GC.KeepAlive(_seekCallback);
}
}