Removed useless CVideoDecoder.cs

Cleanup of FFMpeg Class
This commit is contained in:
Flamefire committed 2013-05-21 23:55:18 +02:00
1 parent 26bfadde64
commit 389aecd772
5 files changed
+95 -240

No files matched your search

+33 -42
View File
@@ -8,14 +8,12 @@ using VocaluxeLib.Draw;
namespace Vocaluxe.Lib.Video.Acinerella namespace Vocaluxe.Lib.Video.Acinerella
{ {
class CDecoder : IDisposable class CDecoder
{ {
private IntPtr _Instance = IntPtr.Zero; // acinerella instance private IntPtr _Instance = IntPtr.Zero; // acinerella instance
private IntPtr _Videodecoder = IntPtr.Zero; // acinerella video decoder instance private IntPtr _Videodecoder = IntPtr.Zero; // acinerella video decoder instance
private readonly Stopwatch _LoopTimer = new Stopwatch(); 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 string _FileName; // current video file name
private bool _FileOpened; private bool _FileOpened;
@@ -25,7 +23,6 @@ namespace Vocaluxe.Lib.Video.Acinerella
private float _CurrentVideoTime; // current video position private float _CurrentVideoTime; // current video position
private bool _BufferFull; // buffer is full, waiting for free frame slot private bool _BufferFull; // buffer is full, waiting for free frame slot
private bool _Skip; // do skip
private float _Time; private float _Time;
private float _Gap; private float _Gap;
private float _Start; private float _Start;
@@ -58,13 +55,6 @@ namespace Vocaluxe.Lib.Video.Acinerella
_Thread = new Thread(_Execute); _Thread = new Thread(_Execute);
} }
public void Free(Closeproc closeProc, int streamID)
{
_Closeproc = closeProc;
_StreamID = streamID;
_Terminated = true;
}
public float Length public float Length
{ {
get { return _FileOpened ? _Duration : 0f; } get { return _FileOpened ? _Duration : 0f; }
@@ -97,6 +87,9 @@ namespace Vocaluxe.Lib.Video.Acinerella
if (!File.Exists(fileName)) if (!File.Exists(fileName))
return false; return false;
//Do this here as one may want to get the length afterwards!
_FileOpened = _DoOpen();
_FileName = fileName; _FileName = fileName;
_Thread.Priority = ThreadPriority.Normal; _Thread.Priority = ThreadPriority.Normal;
_Thread.Name = Path.GetFileName(fileName); _Thread.Name = Path.GetFileName(fileName);
@@ -104,6 +97,11 @@ namespace Vocaluxe.Lib.Video.Acinerella
return true; return true;
} }
public void Close()
{
_Terminated = true;
}
public bool GetFrame(ref CTexture frame, float time, out float videoTime) public bool GetFrame(ref CTexture frame, float time, out float videoTime)
{ {
videoTime = 0; videoTime = 0;
@@ -160,7 +158,7 @@ namespace Vocaluxe.Lib.Video.Acinerella
} }
#region Threading #region Threading
private void _DoSkip() private void _Skip()
{ {
if (!_FileOpened) if (!_FileOpened)
return; return;
@@ -209,24 +207,23 @@ namespace Vocaluxe.Lib.Video.Acinerella
} }
_BufferFull = false; _BufferFull = false;
_Skip = false;
_NewFrame = false; _NewFrame = false;
//EventDecode.Set(); //EventDecode.Set();
} }
private void _Execute() private void _Execute()
{ {
_DoOpen();
//EventDecode.Set(); //EventDecode.Set();
while (!_Terminated) while (!_Terminated)
{ {
{ {
//if (EventDecode.WaitOne(10)) //if (EventDecode.WaitOne(10))
bool skip = false;
lock (_MutexSyncSignals) lock (_MutexSyncSignals)
{ {
_Time = _SetTime; _Time = _SetTime;
if (_SetSkip) if (_SetSkip)
_Skip = true; skip = true;
_SetSkip = false; _SetSkip = false;
_Gap = _SetGap; _Gap = _SetGap;
@@ -234,11 +231,11 @@ namespace Vocaluxe.Lib.Video.Acinerella
_Loop = Loop; _Loop = Loop;
} }
if (_Skip) if (skip)
_DoSkip(); _Skip();
if (!_NewFrame) if (!_NewFrame)
_DoDecode(); _Decode();
if (_NewFrame) if (_NewFrame)
_Copy(); _Copy();
@@ -246,36 +243,32 @@ namespace Vocaluxe.Lib.Video.Acinerella
} }
} }
_DoFree(); _Free();
} }
private void _DoOpen() private bool _DoOpen()
{ {
bool ok; bool ok;
SACInstance instance = new SACInstance();
try try
{ {
_Instance = CAcinerella.AcInit(); _Instance = CAcinerella.AcInit();
CAcinerella.AcOpen2(_Instance, _FileName); 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; ok = instance.Opened;
} }
catch (Exception) catch (Exception)
{ {
CLog.LogError("Error opening video file: " + _FileName);
ok = false; ok = false;
} }
if (!ok)
if (!instance.Opened || !ok)
{ {
//Free(); CLog.LogError("Error opening video file: " + _FileName);
return; return false;
} }
_Duration = instance.Info.Duration / 1000f;
int videoStreamIndex; int videoStreamIndex;
SACDecoder videodecoder; SACDecoder videodecoder;
try try
@@ -287,14 +280,15 @@ namespace Vocaluxe.Lib.Video.Acinerella
catch (Exception) catch (Exception)
{ {
CLog.LogError("Error opening video file (can't find decoder): " + _FileName); CLog.LogError("Error opening video file (can't find decoder): " + _FileName);
return; _Free();
return false;
} }
if (videoStreamIndex < 0) if (videoStreamIndex < 0)
{ {
//Free(); _Free();
return; return false;
} }
_Width = videodecoder.StreamInfo.VideoInfo.FrameWidth; _Width = videodecoder.StreamInfo.VideoInfo.FrameWidth;
@@ -312,10 +306,11 @@ namespace Vocaluxe.Lib.Video.Acinerella
_FrameBuffer[i].Displayed = true; _FrameBuffer[i].Displayed = true;
_FrameBuffer[i].Data = new byte[_Width * _Height * 4]; _FrameBuffer[i].Data = new byte[_Width * _Height * 4];
} }
_FileOpened = true;
return true;
} }
private void _DoDecode() private void _Decode()
{ {
const int framedropcount = 4; const int framedropcount = 4;
@@ -394,7 +389,7 @@ namespace Vocaluxe.Lib.Video.Acinerella
_SetTime = 0f; _SetTime = 0f;
} }
_DoSkip(); _Skip();
} }
else else
_NoMoreFrames = true; _NoMoreFrames = true;
@@ -510,21 +505,17 @@ namespace Vocaluxe.Lib.Video.Acinerella
return result; return result;
} }
private void _DoFree() private void _Free()
{ {
if (_Videodecoder != IntPtr.Zero) if (_Videodecoder != IntPtr.Zero)
CAcinerella.AcFreeDecoder(_Videodecoder); CAcinerella.AcFreeDecoder(_Videodecoder);
if (_Instance != IntPtr.Zero) if (_Instance != IntPtr.Zero)
{
CAcinerella.AcClose(_Instance); CAcinerella.AcClose(_Instance);
if (_Instance != IntPtr.Zero)
CAcinerella.AcFree(_Instance); CAcinerella.AcFree(_Instance);
}
_Closeproc(_StreamID);
} }
#endregion Threading #endregion Threading
public void Dispose() {}
} }
} }
-96
View File
@@ -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 <http://www.gnu.org/licenses/>.
// */
#endregion
using System.Collections.Generic;
using System.Linq;
using VocaluxeLib.Draw;
namespace Vocaluxe.Lib.Video
{
abstract class CVideoDecoder : IVideoDecoder
{
protected List<SVideoStreams> _Streams = new List<SVideoStreams>();
protected bool _Initialized;
public virtual bool Init()
{
_Streams = new List<SVideoStreams>();
_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;
}
}
}
+62 -91
View File
@@ -24,171 +24,142 @@ using VocaluxeLib.Draw;
namespace Vocaluxe.Lib.Video namespace Vocaluxe.Lib.Video
{ {
delegate void Closeproc(int streamID); class CVideoDecoderFFmpeg : IVideoDecoder
class CVideoDecoderFFmpeg : CVideoDecoder
{ {
private readonly List<CDecoder> _Decoder = new List<CDecoder>(); private readonly Dictionary<int, CDecoder> _Decoder = new Dictionary<int, CDecoder>();
private Closeproc _Closeproc; private int _LastID;
private int _Count = 1;
private readonly Object _MutexDecoder = new Object(); private readonly Object _MutexDecoder = new Object();
public override bool Init() public bool Init()
{ {
_Closeproc = _CloseProc;
CloseAll(); CloseAll();
return true;
return base.Init();
} }
public override void CloseAll() public void CloseAll()
{ {
lock (_MutexDecoder) lock (_MutexDecoder)
{ {
for (int i = 0; i < _Decoder.Count; i++) foreach (var decoder in _Decoder.Values)
_Decoder[i].Free(_Closeproc, i + 1); decoder.Close();
_Decoder.Clear();
} }
} }
public override int Load(string videoFileName) public int Load(string videoFileName)
{ {
SVideoStreams stream = new SVideoStreams(0);
CDecoder decoder = new CDecoder(); CDecoder decoder = new CDecoder();
if (decoder.Open(videoFileName)) if (decoder.Open(videoFileName))
{ {
lock (_MutexDecoder) lock (_MutexDecoder)
{ {
_Decoder.Add(decoder); int id = _LastID++;
stream.Handle = _Count++; _Decoder.Add(id, decoder);
_Streams.Add(stream); return id;
return stream.Handle;
} }
} }
return -1; 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.Close();
{ _Decoder.Remove(streamID);
_Decoder[_GetStreamIndex(streamID)].Free(_Closeproc, streamID); return true;
return true;
}
} }
} }
return false; 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; videoTime = 0;
if (_Initialized)
lock (_MutexDecoder)
{ {
lock (_MutexDecoder) CDecoder decoder;
{ if (_Decoder.TryGetValue(streamID, out decoder))
if (_AlreadyAdded(streamID)) return decoder.GetFrame(ref frame, time, out videoTime);
return _Decoder[_GetStreamIndex(streamID)].GetFrame(ref frame, time, out videoTime);
}
} }
return false; return false;
} }
public override float GetLength(int streamID) public float GetLength(int streamID)
{ {
if (_Initialized) lock (_MutexDecoder)
{ {
lock (_MutexDecoder) CDecoder decoder;
{ if (_Decoder.TryGetValue(streamID, out decoder))
if (_AlreadyAdded(streamID)) return decoder.Length;
return _Decoder[_GetStreamIndex(streamID)].Length;
}
} }
return 0f; 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) CDecoder decoder;
{ if (_Decoder.TryGetValue(streamID, out decoder))
if (_AlreadyAdded(streamID)) return decoder.Skip(start, gap);
return _Decoder[_GetStreamIndex(streamID)].Skip(start, gap);
}
} }
return false; return false;
} }
public override void SetLoop(int streamID, bool loop) public void SetLoop(int streamID, bool loop)
{ {
if (_Initialized) lock (_MutexDecoder)
{ {
lock (_MutexDecoder) CDecoder decoder;
{ if (_Decoder.TryGetValue(streamID, out decoder))
if (_AlreadyAdded(streamID)) decoder.Loop = loop;
_Decoder[_GetStreamIndex(streamID)].Loop = loop;
}
} }
} }
public override void Pause(int streamID) public void Pause(int streamID)
{ {
if (_Initialized) lock (_MutexDecoder)
{ {
lock (_MutexDecoder) CDecoder decoder;
{ if (_Decoder.TryGetValue(streamID, out decoder))
if (_AlreadyAdded(streamID)) decoder.Paused = true;
_Decoder[_GetStreamIndex(streamID)].Paused = true;
}
} }
} }
public override void Resume(int streamID) public void Resume(int streamID)
{ {
if (_Initialized) lock (_MutexDecoder)
{ {
lock (_MutexDecoder) CDecoder decoder;
{ if (_Decoder.TryGetValue(streamID, out decoder))
if (_AlreadyAdded(streamID)) decoder.Paused = false;
_Decoder[_GetStreamIndex(streamID)].Paused = false;
}
} }
} }
public override bool Finished(int streamID) public bool Finished(int streamID)
{ {
if (_Initialized) lock (_MutexDecoder)
{ {
lock (_MutexDecoder) CDecoder decoder;
{ if (_Decoder.TryGetValue(streamID, out decoder))
if (_AlreadyAdded(streamID)) return decoder.Finished;
return _Decoder[_GetStreamIndex(streamID)].Finished;
}
} }
return true; return true;
} }
private void _CloseProc(int streamID) public void Update(){}
{
if (_Initialized)
{
lock (_MutexDecoder)
{
if (_AlreadyAdded(streamID))
{
int index = _GetStreamIndex(streamID);
_Decoder.RemoveAt(index);
_Streams.RemoveAt(index);
}
}
}
}
} }
struct SFrameBuffer struct SFrameBuffer
-10
View File
@@ -21,16 +21,6 @@ using VocaluxeLib.Draw;
namespace Vocaluxe.Lib.Video namespace Vocaluxe.Lib.Video
{ {
struct SVideoStreams
{
public int Handle;
public SVideoStreams(int stream)
{
Handle = stream;
}
}
interface IVideoDecoder interface IVideoDecoder
{ {
bool Init(); bool Init();
-1
View File
@@ -242,7 +242,6 @@
<Compile Include="Lib\Sound\IPlayback.cs" /> <Compile Include="Lib\Sound\IPlayback.cs" />
<Compile Include="Lib\Sound\IRecord.cs" /> <Compile Include="Lib\Sound\IRecord.cs" />
<Compile Include="Lib\Video\Acinerella\CAcinerella.cs" /> <Compile Include="Lib\Video\Acinerella\CAcinerella.cs" />
<Compile Include="Lib\Video\CVideoDecoder.cs" />
<Compile Include="Lib\Video\CVideoDecoderFFmpeg.cs" /> <Compile Include="Lib\Video\CVideoDecoderFFmpeg.cs" />
<Compile Include="Lib\Video\CVideoDecoderGstreamer.cs" /> <Compile Include="Lib\Video\CVideoDecoderGstreamer.cs" />
<Compile Include="Lib\Video\Gstreamer\CGstreamerVideoWrapper.cs" /> <Compile Include="Lib\Video\Gstreamer\CGstreamerVideoWrapper.cs" />