diff --git a/Vocaluxe/Lib/Video/Acinerella/CDecoder.cs b/Vocaluxe/Lib/Video/Acinerella/CDecoder.cs index 0ec4f8d3..406db75f 100644 --- a/Vocaluxe/Lib/Video/Acinerella/CDecoder.cs +++ b/Vocaluxe/Lib/Video/Acinerella/CDecoder.cs @@ -8,14 +8,12 @@ using VocaluxeLib.Draw; namespace Vocaluxe.Lib.Video.Acinerella { - class CDecoder : IDisposable + class CDecoder { private IntPtr _Instance = IntPtr.Zero; // acinerella instance private IntPtr _Videodecoder = IntPtr.Zero; // acinerella video decoder instance private readonly Stopwatch _LoopTimer = new Stopwatch(); - private Closeproc _Closeproc; // delegate for stream closing - private int _StreamID; // stream ID for stream closing private string _FileName; // current video file name private bool _FileOpened; @@ -25,7 +23,6 @@ namespace Vocaluxe.Lib.Video.Acinerella private float _CurrentVideoTime; // current video position private bool _BufferFull; // buffer is full, waiting for free frame slot - private bool _Skip; // do skip private float _Time; private float _Gap; private float _Start; @@ -58,13 +55,6 @@ namespace Vocaluxe.Lib.Video.Acinerella _Thread = new Thread(_Execute); } - public void Free(Closeproc closeProc, int streamID) - { - _Closeproc = closeProc; - _StreamID = streamID; - _Terminated = true; - } - public float Length { get { return _FileOpened ? _Duration : 0f; } @@ -97,6 +87,9 @@ namespace Vocaluxe.Lib.Video.Acinerella if (!File.Exists(fileName)) return false; + //Do this here as one may want to get the length afterwards! + _FileOpened = _DoOpen(); + _FileName = fileName; _Thread.Priority = ThreadPriority.Normal; _Thread.Name = Path.GetFileName(fileName); @@ -104,6 +97,11 @@ namespace Vocaluxe.Lib.Video.Acinerella return true; } + public void Close() + { + _Terminated = true; + } + public bool GetFrame(ref CTexture frame, float time, out float videoTime) { videoTime = 0; @@ -160,7 +158,7 @@ namespace Vocaluxe.Lib.Video.Acinerella } #region Threading - private void _DoSkip() + private void _Skip() { if (!_FileOpened) return; @@ -209,24 +207,23 @@ namespace Vocaluxe.Lib.Video.Acinerella } _BufferFull = false; - _Skip = false; _NewFrame = false; //EventDecode.Set(); } private void _Execute() { - _DoOpen(); //EventDecode.Set(); while (!_Terminated) { { //if (EventDecode.WaitOne(10)) + bool skip = false; lock (_MutexSyncSignals) { _Time = _SetTime; if (_SetSkip) - _Skip = true; + skip = true; _SetSkip = false; _Gap = _SetGap; @@ -234,11 +231,11 @@ namespace Vocaluxe.Lib.Video.Acinerella _Loop = Loop; } - if (_Skip) - _DoSkip(); + if (skip) + _Skip(); if (!_NewFrame) - _DoDecode(); + _Decode(); if (_NewFrame) _Copy(); @@ -246,36 +243,32 @@ namespace Vocaluxe.Lib.Video.Acinerella } } - _DoFree(); + _Free(); } - private void _DoOpen() + private bool _DoOpen() { bool ok; - SACInstance instance = new SACInstance(); try { _Instance = CAcinerella.AcInit(); CAcinerella.AcOpen2(_Instance, _FileName); - instance = (SACInstance)Marshal.PtrToStructure(_Instance, typeof(SACInstance)); + SACInstance instance = (SACInstance)Marshal.PtrToStructure(_Instance, typeof(SACInstance)); + _Duration = instance.Info.Duration / 1000f; ok = instance.Opened; } catch (Exception) { - CLog.LogError("Error opening video file: " + _FileName); ok = false; } - - if (!instance.Opened || !ok) + if (!ok) { - //Free(); - return; + CLog.LogError("Error opening video file: " + _FileName); + return false; } - _Duration = instance.Info.Duration / 1000f; - int videoStreamIndex; SACDecoder videodecoder; try @@ -287,14 +280,15 @@ namespace Vocaluxe.Lib.Video.Acinerella catch (Exception) { CLog.LogError("Error opening video file (can't find decoder): " + _FileName); - return; + _Free(); + return false; } if (videoStreamIndex < 0) { - //Free(); - return; + _Free(); + return false; } _Width = videodecoder.StreamInfo.VideoInfo.FrameWidth; @@ -312,10 +306,11 @@ namespace Vocaluxe.Lib.Video.Acinerella _FrameBuffer[i].Displayed = true; _FrameBuffer[i].Data = new byte[_Width * _Height * 4]; } - _FileOpened = true; + + return true; } - private void _DoDecode() + private void _Decode() { const int framedropcount = 4; @@ -394,7 +389,7 @@ namespace Vocaluxe.Lib.Video.Acinerella _SetTime = 0f; } - _DoSkip(); + _Skip(); } else _NoMoreFrames = true; @@ -510,21 +505,17 @@ namespace Vocaluxe.Lib.Video.Acinerella return result; } - private void _DoFree() + private void _Free() { if (_Videodecoder != IntPtr.Zero) CAcinerella.AcFreeDecoder(_Videodecoder); if (_Instance != IntPtr.Zero) + { CAcinerella.AcClose(_Instance); - - if (_Instance != IntPtr.Zero) CAcinerella.AcFree(_Instance); - - _Closeproc(_StreamID); + } } #endregion Threading - - public void Dispose() {} } } \ No newline at end of file diff --git a/Vocaluxe/Lib/Video/CVideoDecoder.cs b/Vocaluxe/Lib/Video/CVideoDecoder.cs deleted file mode 100644 index 79ea8b52..00000000 --- a/Vocaluxe/Lib/Video/CVideoDecoder.cs +++ /dev/null @@ -1,96 +0,0 @@ -#region license -// /* -// This file is part of Vocaluxe. -// -// Vocaluxe is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// Vocaluxe is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with Vocaluxe. If not, see . -// */ -#endregion - -using System.Collections.Generic; -using System.Linq; -using VocaluxeLib.Draw; - -namespace Vocaluxe.Lib.Video -{ - abstract class CVideoDecoder : IVideoDecoder - { - protected List _Streams = new List(); - protected bool _Initialized; - - public virtual bool Init() - { - _Streams = new List(); - _Initialized = true; - - return true; - } - - public abstract void CloseAll(); - - public virtual int GetNumStreams() - { - return _Streams.Count; - } - - public virtual int Load(string videoFileName) - { - return 0; - } - - public virtual bool Close(int streamID) - { - return true; - } - - public virtual float GetLength(int streamID) - { - return 0f; - } - - public abstract bool GetFrame(int streamID, ref CTexture frame, float time, out float videoTime); - - public virtual bool Skip(int streamID, float start, float gap) - { - return true; - } - - public virtual void SetLoop(int streamID, bool loop) {} - - public virtual void Pause(int streamID) {} - - public virtual void Resume(int streamID) {} - - public virtual bool Finished(int streamID) - { - return false; - } - - public virtual void Update() {} - - protected bool _AlreadyAdded(int streamID) - { - return _Streams.Any(st => st.Handle == streamID); - } - - protected int _GetStreamIndex(int streamID) - { - for (int i = 0; i < _Streams.Count; i++) - { - if (_Streams[i].Handle == streamID) - return i; - } - return -1; - } - } -} \ No newline at end of file diff --git a/Vocaluxe/Lib/Video/CVideoDecoderFFmpeg.cs b/Vocaluxe/Lib/Video/CVideoDecoderFFmpeg.cs index 000181f4..d2714b9c 100644 --- a/Vocaluxe/Lib/Video/CVideoDecoderFFmpeg.cs +++ b/Vocaluxe/Lib/Video/CVideoDecoderFFmpeg.cs @@ -24,171 +24,142 @@ using VocaluxeLib.Draw; namespace Vocaluxe.Lib.Video { - delegate void Closeproc(int streamID); - - class CVideoDecoderFFmpeg : CVideoDecoder + class CVideoDecoderFFmpeg : IVideoDecoder { - private readonly List _Decoder = new List(); - private Closeproc _Closeproc; - private int _Count = 1; + private readonly Dictionary _Decoder = new Dictionary(); + private int _LastID; private readonly Object _MutexDecoder = new Object(); - public override bool Init() + public bool Init() { - _Closeproc = _CloseProc; CloseAll(); - - return base.Init(); + return true; } - public override void CloseAll() + public void CloseAll() { lock (_MutexDecoder) { - for (int i = 0; i < _Decoder.Count; i++) - _Decoder[i].Free(_Closeproc, i + 1); + foreach (var decoder in _Decoder.Values) + decoder.Close(); + _Decoder.Clear(); } } - public override int Load(string videoFileName) + public int Load(string videoFileName) { - SVideoStreams stream = new SVideoStreams(0); CDecoder decoder = new CDecoder(); if (decoder.Open(videoFileName)) { lock (_MutexDecoder) { - _Decoder.Add(decoder); - stream.Handle = _Count++; - _Streams.Add(stream); - return stream.Handle; + int id = _LastID++; + _Decoder.Add(id, decoder); + return id; } } return -1; } - public override bool Close(int streamID) + public bool Close(int streamID) { - if (_Initialized) + lock (_MutexDecoder) { - lock (_MutexDecoder) + CDecoder decoder; + if (_Decoder.TryGetValue(streamID, out decoder)) { - if (_AlreadyAdded(streamID)) - { - _Decoder[_GetStreamIndex(streamID)].Free(_Closeproc, streamID); - return true; - } + decoder.Close(); + _Decoder.Remove(streamID); + return true; } } return false; } - public override bool GetFrame(int streamID, ref CTexture frame, float time, out float videoTime) + public int GetNumStreams() + { + return _Decoder.Count; + } + + public bool GetFrame(int streamID, ref CTexture frame, float time, out float videoTime) { videoTime = 0; - if (_Initialized) + + lock (_MutexDecoder) { - lock (_MutexDecoder) - { - if (_AlreadyAdded(streamID)) - return _Decoder[_GetStreamIndex(streamID)].GetFrame(ref frame, time, out videoTime); - } + CDecoder decoder; + if (_Decoder.TryGetValue(streamID, out decoder)) + return decoder.GetFrame(ref frame, time, out videoTime); } return false; } - public override float GetLength(int streamID) + public float GetLength(int streamID) { - if (_Initialized) + lock (_MutexDecoder) { - lock (_MutexDecoder) - { - if (_AlreadyAdded(streamID)) - return _Decoder[_GetStreamIndex(streamID)].Length; - } + CDecoder decoder; + if (_Decoder.TryGetValue(streamID, out decoder)) + return decoder.Length; } return 0f; } - public override bool Skip(int streamID, float start, float gap) + public bool Skip(int streamID, float start, float gap) { - if (_Initialized) + lock (_MutexDecoder) { - lock (_MutexDecoder) - { - if (_AlreadyAdded(streamID)) - return _Decoder[_GetStreamIndex(streamID)].Skip(start, gap); - } + CDecoder decoder; + if (_Decoder.TryGetValue(streamID, out decoder)) + return decoder.Skip(start, gap); } return false; } - public override void SetLoop(int streamID, bool loop) + public void SetLoop(int streamID, bool loop) { - if (_Initialized) + lock (_MutexDecoder) { - lock (_MutexDecoder) - { - if (_AlreadyAdded(streamID)) - _Decoder[_GetStreamIndex(streamID)].Loop = loop; - } + CDecoder decoder; + if (_Decoder.TryGetValue(streamID, out decoder)) + decoder.Loop = loop; } } - public override void Pause(int streamID) + public void Pause(int streamID) { - if (_Initialized) + lock (_MutexDecoder) { - lock (_MutexDecoder) - { - if (_AlreadyAdded(streamID)) - _Decoder[_GetStreamIndex(streamID)].Paused = true; - } + CDecoder decoder; + if (_Decoder.TryGetValue(streamID, out decoder)) + decoder.Paused = true; } } - public override void Resume(int streamID) + public void Resume(int streamID) { - if (_Initialized) + lock (_MutexDecoder) { - lock (_MutexDecoder) - { - if (_AlreadyAdded(streamID)) - _Decoder[_GetStreamIndex(streamID)].Paused = false; - } + CDecoder decoder; + if (_Decoder.TryGetValue(streamID, out decoder)) + decoder.Paused = false; } } - public override bool Finished(int streamID) + public bool Finished(int streamID) { - if (_Initialized) + lock (_MutexDecoder) { - lock (_MutexDecoder) - { - if (_AlreadyAdded(streamID)) - return _Decoder[_GetStreamIndex(streamID)].Finished; - } + CDecoder decoder; + if (_Decoder.TryGetValue(streamID, out decoder)) + return decoder.Finished; } return true; } - private void _CloseProc(int streamID) - { - if (_Initialized) - { - lock (_MutexDecoder) - { - if (_AlreadyAdded(streamID)) - { - int index = _GetStreamIndex(streamID); - _Decoder.RemoveAt(index); - _Streams.RemoveAt(index); - } - } - } - } + public void Update(){} } struct SFrameBuffer diff --git a/Vocaluxe/Lib/Video/IVideoDecoder.cs b/Vocaluxe/Lib/Video/IVideoDecoder.cs index 39b54587..6c48a24e 100644 --- a/Vocaluxe/Lib/Video/IVideoDecoder.cs +++ b/Vocaluxe/Lib/Video/IVideoDecoder.cs @@ -21,16 +21,6 @@ using VocaluxeLib.Draw; namespace Vocaluxe.Lib.Video { - struct SVideoStreams - { - public int Handle; - - public SVideoStreams(int stream) - { - Handle = stream; - } - } - interface IVideoDecoder { bool Init(); diff --git a/Vocaluxe/Vocaluxe.csproj b/Vocaluxe/Vocaluxe.csproj index 176fbbfa..29172362 100644 --- a/Vocaluxe/Vocaluxe.csproj +++ b/Vocaluxe/Vocaluxe.csproj @@ -242,7 +242,6 @@ -