Make loops smooth and decoder more robust

This commit is contained in:
Flamefire committed 2014-03-12 16:25:33 +01:00
1 parent c6a22388d6
commit cc35e7fd66
2 files changed
+67 -72

No files matched your search

+7 -20
View File
@@ -26,7 +26,6 @@ namespace Vocaluxe.Lib.Video.Acinerella
{ {
private readonly Stopwatch _LoopTimer = new Stopwatch(); private readonly Stopwatch _LoopTimer = new Stopwatch();
private float _LastShownTime = -1f; // time if the last shown frame in s
private float _Gap; private float _Gap;
private bool _Paused; private bool _Paused;
private float _LoopTime; private float _LoopTime;
@@ -128,26 +127,12 @@ namespace Vocaluxe.Lib.Video.Acinerella
time += _Gap; time += _Gap;
_Thread.SyncTime(time); _Thread.SyncTime(time);
if (Math.Abs(_LastShownTime - time) < 1f / 1000 && frame != null) //Check 1 ms difference bool finished;
{ _Thread.GetFrame(ref frame, ref time, out finished);
videoTime = _LastShownTime - _Gap;
return true;
}
CDecoderThread.EFrameState state = _Thread.GetFrame(ref frame, _LastShownTime + _Gap, ref time);
switch (state)
{
case CDecoderThread.EFrameState.EndFrame:
Finished = true;
videoTime = Length - _Gap;
break;
case CDecoderThread.EFrameState.ValidFrame:
_LastShownTime = time;
videoTime = time - _Gap; videoTime = time - _Gap;
break;
default: if (finished) //Only set, not reset
videoTime = _LastShownTime - _Gap; Finished = true;
break;
}
return frame != null; return frame != null;
} }
@@ -156,6 +141,8 @@ namespace Vocaluxe.Lib.Video.Acinerella
Finished = false; Finished = false;
_Gap = gap; _Gap = gap;
_Thread.Skip(start + gap); _Thread.Skip(start + gap);
if (Loop)
Loop = true; //Reset loop (timer)
return true; return true;
} }
} }
+60 -52
View File
@@ -1,4 +1,5 @@
using System; using System;
using System.Diagnostics;
using System.IO; using System.IO;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Threading; using System.Threading;
@@ -12,12 +13,7 @@ namespace Vocaluxe.Lib.Video.Acinerella
//Most others are to be called by this thread (_Thread instance) only! //Most others are to be called by this thread (_Thread instance) only!
class CDecoderThread class CDecoderThread
{ {
public enum EFrameState private const float _LoopedRequestTime = -0.001f; //Magic const to detect if decoder looped automaticly
{
ValidFrame,
InvalidFrame,
EndFrame
}
private Thread _Thread; private Thread _Thread;
private readonly Object _BufferMutex = new Object(); private readonly Object _BufferMutex = new Object();
@@ -27,6 +23,7 @@ namespace Vocaluxe.Lib.Video.Acinerella
private readonly CFramebuffer _Framebuffer; private readonly CFramebuffer _Framebuffer;
private float _LastDecodedTime; // time of last decoded frame private float _LastDecodedTime; // time of last decoded frame
private float _LastShownTime = -1f; // time if the last shown frame in s IMPORTANT: Write only in context of reader
// time of last requested frame aka current should-be position (_LastDecodedTime should be >=_RequestTime) // time of last requested frame aka current should-be position (_LastDecodedTime should be >=_RequestTime)
//HAS to be < Length //HAS to be < Length
public float RequestTime { get; private set; } public float RequestTime { get; private set; }
@@ -42,7 +39,6 @@ namespace Vocaluxe.Lib.Video.Acinerella
private bool _FrameAvailable; private bool _FrameAvailable;
private bool _NoMoreFrames; private bool _NoMoreFrames;
private readonly AutoResetEvent _EvWakeUp = new AutoResetEvent(false); private readonly AutoResetEvent _EvWakeUp = new AutoResetEvent(false);
private readonly AutoResetEvent _EvPause = new AutoResetEvent(false);
private readonly AutoResetEvent _EvNoMoreFrames = new AutoResetEvent(false); private readonly AutoResetEvent _EvNoMoreFrames = new AutoResetEvent(false);
private bool _IsSleeping; private bool _IsSleeping;
private int _WaitCount; private int _WaitCount;
@@ -53,6 +49,7 @@ namespace Vocaluxe.Lib.Video.Acinerella
public CDecoderThread() public CDecoderThread()
{ {
_Framebuffer = new CFramebuffer(10); _Framebuffer = new CFramebuffer(10);
_FrameDuration = 0.02f; // Set a reasonable standard till correct value is set
} }
//Open the file and get the length. //Open the file and get the length.
@@ -98,7 +95,6 @@ namespace Vocaluxe.Lib.Video.Acinerella
public void Stop() public void Stop()
{ {
_Terminated = true; _Terminated = true;
_EvPause.Set();
_EvWakeUp.Set(); _EvWakeUp.Set();
_EvNoMoreFrames.Set(); _EvNoMoreFrames.Set();
} }
@@ -107,7 +103,6 @@ namespace Vocaluxe.Lib.Video.Acinerella
{ {
if (_Paused) if (_Paused)
return; return;
_EvPause.Reset();
_Paused = true; _Paused = true;
} }
@@ -116,7 +111,6 @@ namespace Vocaluxe.Lib.Video.Acinerella
if (!_Paused) if (!_Paused)
return; return;
_Paused = false; _Paused = false;
_EvPause.Set();
} }
//Sets the position to the given time discarding all decoded frames //Sets the position to the given time discarding all decoded frames
@@ -131,37 +125,37 @@ namespace Vocaluxe.Lib.Video.Acinerella
lock (_BufferMutex) //Cover clear, RequestTime=, _RequestSkip= lock (_BufferMutex) //Cover clear, RequestTime=, _RequestSkip=
{ {
_Framebuffer.Clear(); _Framebuffer.Clear();
RequestTime = time; RequestTime = time;
_LastShownTime = time - _FrameDuration; //Set this to time to detect overflow of time in FindFrame but subtract FrameDuration so GetFrame will get the first frame
_RequestSkip = true; _RequestSkip = true;
} }
_EvNoMoreFrames.Set(); _EvNoMoreFrames.Set();
} }
private CFramebuffer.CFrame _FindFrame(float lastTime, float now) private CFramebuffer.CFrame _FindFrame(float now)
{ {
CFramebuffer.CFrame result = null; CFramebuffer.CFrame result = null;
CFramebuffer.CFrame frame; CFramebuffer.CFrame frame;
_Framebuffer.ResetStack(); _Framebuffer.ResetStack();
while ((frame = _Framebuffer.Pop()) != null) while ((frame = _Framebuffer.Pop()) != null)
{ {
float frameEnd = frame.Time + _FrameDuration; //float frameEnd = frame.Time + _FrameDuration;
if (!Loop || lastTime < now) float frameTime = frame.Time;
if (frameTime > now)
{ {
//Regular case: Frame has to be between last frame and now //2 Cases: all following frames are after this one, or we have a loop and 'now' wrapped over
if (frameEnd > now) //First case is if we have no loop or we did not wrap or frame is before last one (last is the case if frame is already one of the new iterations, e.g. Last=19 now=1 frame=2)
break; // Next frames are after this one if (!Loop || _LastShownTime <= now || frameTime < _LastShownTime)
if (frame.Time < lastTime) break; //Following frames (incl this one) are after now, so do not consider any of them
{
frame.SetRead(); //Frame is before last one, Discard
continue;
} }
} // ReSharper disable CompareOfFloatsByEqualityOperator
else else if (Loop && RequestTime == _LoopedRequestTime)
// ReSharper restore CompareOfFloatsByEqualityOperator
{ {
//Loop case: time rolled over -> Frame has to be after last one OR before now (which is after last one but rolled over) //Frame time might have wrapped but now did not
if (frame.Time < lastTime && frameEnd > now) if (frameTime < _LastShownTime && _LastShownTime <= now)
break; break; //Following frames (incl this one) are after now, so do not consider any of them
} }
//Get the last(newest) possible frame and skip the rest //Get the last(newest) possible frame and skip the rest
if (result != null) if (result != null)
@@ -171,36 +165,49 @@ namespace Vocaluxe.Lib.Video.Acinerella
} }
result = frame; result = frame;
if (_Paused) if (_Paused)
break; //Just get 1 frame if paused break; //Just get 1 frame if paused otherwise a paused movie could move a bit
} }
return result; return result;
} }
public EFrameState GetFrame(ref CTexture frame, float lastTime, ref float time) /// <summary>
/// Gets a frame
/// </summary>
/// <param name="frame">Referenz to texture where frame should be put in (can be null, then texture is created)</param>
/// <param name="time">Maximum start time for frame</param>
/// <param name="finished">Set to whether there are no more frames (stream finished, no loop, future calls will always return false)</param>
/// <returns>True if a new frame was gotten</returns>
public bool GetFrame(ref CTexture frame, ref float time, out bool finished)
{ {
EFrameState result; bool result;
CFramebuffer.CFrame curFrame = _FindFrame(lastTime, time); if (Math.Abs(_LastShownTime - time) < _FrameDuration && frame != null) //Check 1 frame difference
{
time = _LastShownTime;
result = false;
}
else
{
CFramebuffer.CFrame curFrame = _FindFrame(time);
if (curFrame != null) if (curFrame != null)
{ {
CDraw.UpdateOrAddTexture(ref frame, _Width, _Height, curFrame.Data); CDraw.UpdateOrAddTexture(ref frame, _Width, _Height, curFrame.Data);
if (!_Paused) if (!_Paused)
curFrame.SetRead(); curFrame.SetRead();
time = curFrame.Time; time = curFrame.Time;
result = (frame != null) ? EFrameState.ValidFrame : EFrameState.InvalidFrame; _LastShownTime = time;
result = frame != null;
} }
else else
{ result = false;
if (_NoMoreFrames && _Framebuffer.IsEmpty())
result = EFrameState.EndFrame;
else
result = EFrameState.InvalidFrame;
}
if (_IsSleeping) if (_IsSleeping)
{ {
_IsSleeping = false; _IsSleeping = false;
_EvWakeUp.Set(); _EvWakeUp.Set();
} }
}
finished = _NoMoreFrames && _Framebuffer.IsEmpty();
return result; return result;
} }
@@ -209,22 +216,22 @@ namespace Vocaluxe.Lib.Video.Acinerella
{ {
if (_Thread == null) if (_Thread == null)
return; //Not initialized return; //Not initialized
while (time >= Length)
time -= Length; if (RequestTime - time >= _FrameDuration)
if (RequestTime - time >= 1f / 1000f)
{ {
//Jump back more than 1ms //Jump back more than 1 frame. To guarantee the order in the buffer use Skip() which clears the buffer
//Will mostly happen in loops when looped through
Skip(time); Skip(time);
} }
else if (time - RequestTime >= 1f / 1000f) else if (time - RequestTime >= _FrameDuration)
{ {
//we were more than 1 ms to slow -> Jump forward //we were more than 1 frame to slow -> Jump forward (This is save, as the decoder will skip frames if necessary)
// ReSharper disable CompareOfFloatsByEqualityOperator // ReSharper disable CompareOfFloatsByEqualityOperator
if (Loop && RequestTime == 0f) if (Loop && RequestTime == _LoopedRequestTime)
// ReSharper restore CompareOfFloatsByEqualityOperator // ReSharper restore CompareOfFloatsByEqualityOperator
{ {
//Check if we had a loop //In a loop our decoder may have reset RequestTime to 0 but we want a frame from the end of the video
//Skipping forward is fatal as it resets the decoder to decode already decoded frames causing lags
//So first check if we have a valid frame in our buffer
_Framebuffer.ResetStack(); _Framebuffer.ResetStack();
CFramebuffer.CFrame frame; CFramebuffer.CFrame frame;
while ((frame = _Framebuffer.Pop()) != null) while ((frame = _Framebuffer.Pop()) != null)
@@ -232,6 +239,9 @@ namespace Vocaluxe.Lib.Video.Acinerella
if (frame.Time + _FrameDuration >= time) if (frame.Time + _FrameDuration >= time)
return; return;
} }
//If we don't the Length might be inaccurate (e.g. last frame ends at 19.98 but Length=20)
if (time >= Length - 2 * _FrameDuration)
return;
} }
RequestTime = time; RequestTime = time;
@@ -305,7 +315,7 @@ namespace Vocaluxe.Lib.Video.Acinerella
{ {
const int minFrameDropCount = 4; const int minFrameDropCount = 4;
if (_Paused || _NoMoreFrames) if (_NoMoreFrames)
return; return;
float videoTime = RequestTime; float videoTime = RequestTime;
@@ -344,7 +354,7 @@ namespace Vocaluxe.Lib.Video.Acinerella
{ {
if (Loop) if (Loop)
{ {
RequestTime = 0; RequestTime = _LoopedRequestTime;
_Skip(); _Skip();
} }
else else
@@ -379,8 +389,6 @@ namespace Vocaluxe.Lib.Video.Acinerella
while (!_Terminated) while (!_Terminated)
{ {
if (_Paused)
_EvPause.WaitOne();
if (_NoMoreFrames) if (_NoMoreFrames)
_EvNoMoreFrames.WaitOne(); _EvNoMoreFrames.WaitOne();
if (_RequestSkip) if (_RequestSkip)
@@ -418,7 +426,7 @@ namespace Vocaluxe.Lib.Video.Acinerella
} }
else else
{ {
Thread.Sleep((int)((_Framebuffer.Size - 2) * _FrameDuration * 1000)); Thread.Sleep((int)(_Framebuffer.Size * _FrameDuration * 1000 / 2));
_WaitCount++; _WaitCount++;
} }
} }