123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343 |
-
- using System.Runtime.InteropServices;
- using Bmp.Core.FFMpeg.CsCoreExt;
- namespace Bmp.Core.FFMpeg.CsCorePorts.FFMpegWrap;
- /// <summary>
- /// Generic FFmpeg based decoder.
- /// </summary>
- /// <remarks>
- /// The <see cref="FfmpegDecoder" /> uses the FFmpeg libraries to decode audio files.
- /// In order to make sure that the FFmpeg libraries are compatible with the <see cref="FfmpegDecoder" />,
- /// use the binaries shipped with the CSCore.Ffmpeg project.
- /// If a custom build is necessary, use the FFmpeg source code, from the CSCore git repository
- /// (https://github.com/filoe/cscore).
- /// </remarks>
- public class FfmpegDecoder : IWaveSource
- {
- // BmpMod: Add `AttachedPic` and `BitPerRawSample`
- public FFMpegAttachedPicCollection? AttachedPics => _formatContext == null ? null : new FFMpegAttachedPicCollection(_formatContext.FormatContext);
- public unsafe int? BitPerRawSample => _formatContext != null ? _formatContext.SelectedStream.Stream.codec->bits_per_raw_sample : null;
- public unsafe long? StreamBitPerSecond => _formatContext != null ? _formatContext.SelectedStream.Stream.codec->bit_rate : null;
- public long? FileBitPerSecond => _formatContext?.FormatContext.bit_rate;
- public unsafe string? FileFormat
- {
- get
- {
- if (_formatContext == null) return null;
- var codec = Marshal.PtrToStringUTF8((nint)_formatContext.SelectedStream.Stream.codec->codec->long_name);
- var container = Marshal.PtrToStringUTF8((nint)_formatContext.FormatContext.iformat->name);
- return $"{codec} @ {container}";
- }
- }
- // Orig
- private readonly object _lockObject = new object();
- private readonly Uri _uri;
- private FfmpegStream? _ffmpegStream;
- private AvFormatContext? _formatContext;
- private bool _disposeStream = false;
- private byte[] _overflowBuffer = Array.Empty<byte>();
- private int _overflowCount;
- private int _overflowOffset;
- private long _position;
- private Stream? _stream;
- /// <summary>
- /// Gets a dictionary with found metadata.
- /// </summary>
- public Dictionary<string, string> Metadata
- {
- get
- {
- if (_formatContext == null)
- return new Dictionary<string, string>();
- return _formatContext.Metadata;
- }
- }
- /// <summary>
- /// Initializes a new instance of the <see cref="FfmpegDecoder" /> class based on a specified filename or url.
- /// </summary>
- /// <param name="url">A url containing a filename or web url. </param>
- /// <exception cref="FfmpegException">
- /// Any ffmpeg error.
- /// </exception>
- /// <exception cref="NotSupportedException">
- /// DBL format is not supported.
- /// or
- /// Audio Sample Format not supported.
- /// </exception>
- /// <exception cref="ArgumentNullException">uri</exception>
- public FfmpegDecoder(string url)
- {
- const int invalidArgument = unchecked((int)0xffffffea);
- _uri = new Uri(url);
- try
- {
- _formatContext = new AvFormatContext(url);
- Initialize();
- }
- catch (FfmpegException ex)
- {
- if (ex.ErrorCode == invalidArgument && "avformat_open_input".Equals(ex.Function, StringComparison.OrdinalIgnoreCase))
- {
- if (!TryInitializeWithFileAsStream(url))
- throw;
- }
- else
- {
- throw;
- }
- }
- }
- /// <summary>
- /// Initializes a new instance of the <see cref="FfmpegDecoder" /> class based on a <see cref="Stream" />.
- /// </summary>
- /// <param name="stream">The stream.</param>
- /// <exception cref="FfmpegException">Any ffmpeg error.</exception>
- /// <exception cref="ArgumentNullException">stream</exception>
- /// <exception cref="ArgumentException">Stream is not readable.</exception>
- /// <exception cref="OutOfMemoryException">Could not allocate FormatContext.</exception>
- /// <exception cref="NotSupportedException">
- /// DBL format is not supported.
- /// or
- /// Audio Sample Format not supported.
- /// </exception>
- public FfmpegDecoder(Stream stream)
- {
- if (stream == null)
- throw new ArgumentNullException("stream");
- InitializeWithStream(stream, false);
- }
- /// <summary>
- /// Reads a sequence of bytes from the <see cref="FfmpegDecoder" /> and advances the position within the
- /// stream by the
- /// number of bytes read.
- /// </summary>
- /// <param name="buffer">
- /// An array of bytes. When this method returns, the <paramref name="buffer" /> contains the specified
- /// array of bytes with the values between <paramref name="offset" /> and (<paramref name="offset" /> +
- /// <paramref name="count" /> - 1) replaced by the bytes read from the current source.
- /// </param>
- /// <param name="offset">
- /// The zero-based offset in the <paramref name="buffer" /> at which to begin storing the data
- /// read from the current stream.
- /// </param>
- /// <param name="count">The maximum number of bytes to read from the current source.</param>
- /// <returns>The total number of bytes read into the buffer.</returns>
- public int Read(byte[] buffer, int offset, int count)
- {
- var read = 0;
- count -= count % WaveFormat.BlockAlign;
- var fetchedOverflows = GetOverflows(buffer, ref offset, count);
- read += fetchedOverflows;
- while (read < count)
- {
- long packetPosition;
- int bufferLength;
- lock (_lockObject)
- {
- using (var frame = new AvFrame(_formatContext))
- {
- double seconds;
- bufferLength = frame.ReadNextFrame(out seconds, ref _overflowBuffer);
- packetPosition = this.GetRawElements(TimeSpan.FromSeconds(seconds));
- }
- }
- if (bufferLength <= 0)
- {
- //if (_uri != null && !_uri.IsFile)
- //{
- // //webstream: don't exit, maybe the connection was lost -> give it a try to recover
- // Thread.Sleep(10);
- //}
- //else
- break; //no webstream -> exit
- }
- var bytesToCopy = Math.Min(count - read, bufferLength);
- Array.Copy(_overflowBuffer, 0, buffer, offset, bytesToCopy);
- read += bytesToCopy;
- offset += bytesToCopy;
- _overflowCount = bufferLength > bytesToCopy ? bufferLength - bytesToCopy : 0;
- _overflowOffset = bufferLength > bytesToCopy ? bytesToCopy : 0;
- _position = packetPosition + read - fetchedOverflows;
- }
- if (fetchedOverflows == read)
- {
- //no new packet was decoded -> add the read bytes to the position
- _position += read;
- }
- return read;
- }
- /// <summary>
- /// Gets a value indicating whether the <see cref="FfmpegDecoder" /> supports seeking.
- /// </summary>
- public bool CanSeek
- {
- get
- {
- if (_formatContext == null)
- return false;
- return _formatContext.CanSeek;
- }
- }
- /// <summary>
- /// Gets the <see cref="IAudioSource.WaveFormat" /> of the waveform-audio data.
- /// </summary>
- public WaveFormat WaveFormat { get; private set; }
- /// <summary>
- /// Gets or sets the current position in bytes.
- /// </summary>
- public long Position
- {
- get { return _position; }
- set { SeekPosition(value); }
- }
- /// <summary>
- /// Gets the length of the waveform-audio data in bytes.
- /// </summary>
- public long Length
- {
- get
- {
- if (_formatContext == null || _formatContext.SelectedStream == null)
- return 0;
- return this.GetRawElements(TimeSpan.FromSeconds(_formatContext.LengthInSeconds));
- }
- }
- /// <summary>
- /// Releases all allocated resources used by the <see cref="FfmpegDecoder" />.
- /// </summary>
- public void Dispose()
- {
- Dispose(true);
- }
- /// <summary>
- /// Releases unmanaged and - optionally - managed resources.
- /// </summary>
- /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
- protected void Dispose(bool disposing)
- {
- GC.SuppressFinalize(this);
- if (disposing)
- {
- if (_disposeStream && _stream != null)
- {
- _stream.Dispose();
- _stream = null;
- }
- if (_formatContext != null)
- {
- _formatContext.Dispose();
- _formatContext = null;
- }
- if (_ffmpegStream != null)
- {
- _ffmpegStream.Dispose();
- _ffmpegStream = null;
- }
- }
- }
- private unsafe void Initialize()
- {
- if (_formatContext == null) throw new InvalidOperationException();
- WaveFormat = _formatContext.SelectedStream.GetSuggestedWaveFormat();
- }
- private void InitializeWithStream(Stream stream, bool disposeStream)
- {
- _stream = stream;
- _disposeStream = disposeStream;
- _ffmpegStream = new FfmpegStream(stream, false);
- _formatContext = new AvFormatContext(_ffmpegStream);
- Initialize();
- }
- private bool TryInitializeWithFileAsStream(string filename)
- {
- if (!File.Exists(filename))
- return false;
- Stream stream = null;
- try
- {
- stream = File.OpenRead(filename);
- InitializeWithStream(stream, true);
- return true;
- }
- catch (Exception)
- {
- if (stream != null)
- {
- stream.Dispose();
- }
- return false;
- }
- }
- /// <summary>
- /// Finalizes an instance of the <see cref="FfmpegDecoder" /> class.
- /// </summary>
- ~FfmpegDecoder()
- {
- Dispose(false);
- }
- private void SeekPosition(long position)
- {
- //https://ffmpeg.org/doxygen/trunk/seek-test_8c-source.html
- var seconds = this.GetMilliseconds(position) / 1000.0;
- lock (_lockObject)
- {
- _formatContext.SeekFile(seconds);
- _position = position;
- _overflowCount = 0;
- _overflowOffset = 0;
- }
- }
- private int GetOverflows(byte[] buffer, ref int offset, int count)
- {
- if (_overflowCount != 0 && _overflowBuffer != null && count > 0)
- {
- var bytesToCopy = Math.Min(count, _overflowCount);
- Array.Copy(_overflowBuffer, _overflowOffset, buffer, offset, bytesToCopy);
- _overflowCount -= bytesToCopy;
- _overflowOffset += bytesToCopy;
- offset += bytesToCopy;
- return bytesToCopy;
- }
- return 0;
- }
- }
|