Exemplo n.º 1
0
HRESULT CSubPicQueueImpl::RenderTo(ISubPic* pSubPic, REFERENCE_TIME rtStart, REFERENCE_TIME rtStop, double fps, BOOL bIsAnimated)
{
    HRESULT hr = E_FAIL;

    if (!pSubPic) {
        return hr;
    }

    CComPtr<ISubPicProvider> pSubPicProvider;
    if (FAILED(GetSubPicProvider(&pSubPicProvider)) || !pSubPicProvider) {
        return hr;
    }

    if (pSubPic->GetInverseAlpha()) {
        hr = pSubPic->ClearDirtyRect(0x00000000);
    } else {
        hr = pSubPic->ClearDirtyRect(0xFF000000);
    }

    SubPicDesc spd;
    if (SUCCEEDED(hr)) {
        hr = pSubPic->Lock(spd);
    }
    if (SUCCEEDED(hr)) {
        CRect r(0, 0, 0, 0);
        hr = pSubPicProvider->Render(spd, bIsAnimated ? rtStart : ((rtStart + rtStop) / 2), fps, r);

        pSubPic->SetStart(rtStart);
        pSubPic->SetStop(rtStop);

        pSubPic->Unlock(r);
    }

    return hr;
}
Exemplo n.º 2
0
HRESULT ISubPicQueueImpl::RenderTo(ISubPic* pSubPic, REFERENCE_TIME rtStart, REFERENCE_TIME rtStop, double fps, BOOL bIsAnimated)
{
	HRESULT hr = E_FAIL;

	if(!pSubPic)
		return hr;

	CComPtr<ISubPicProvider> pSubPicProvider;
	if(FAILED(GetSubPicProvider(&pSubPicProvider)) || !pSubPicProvider)
		return hr;

	if(FAILED(pSubPicProvider->Lock()))
		return hr;

	SubPicDesc spd;
	if(SUCCEEDED(pSubPic->ClearDirtyRect(0xFF000000))
	&& SUCCEEDED(pSubPic->Lock(spd)))
	{
		CRect r(0,0,0,0);
		hr = pSubPicProvider->Render(spd, bIsAnimated ? rtStart : ((rtStart+rtStop)/2), fps, r);

		pSubPic->SetStart(rtStart);
		pSubPic->SetStop(rtStop);

		pSubPic->Unlock(r);
	}

	pSubPicProvider->Unlock();

	return hr;
}
Exemplo n.º 3
0
DWORD CXySubPicQueue::ThreadProc()
{
    SetThreadName(DWORD(-1), "Subtitle Renderer Thread");
    SetThreadPriority(m_hThread, THREAD_PRIORITY_ABOVE_NORMAL);

    bool bAgain = true;
    for (;;) {
        DWORD Ret = WaitForMultipleObjects(EVENT_COUNT, m_ThreadEvents, FALSE, bAgain ? 0 : INFINITE);
        bAgain = false;

        if (Ret == WAIT_TIMEOUT) {
            ;
        } else if ((Ret - WAIT_OBJECT_0) != EVENT_TIME) {
            break;
        }
        double fps = m_fps;
        REFERENCE_TIME rtTimePerFrame = (REFERENCE_TIME)(10000000.0 / fps);
        REFERENCE_TIME rtNow = UpdateQueue();

        int nMaxSubPic = m_nMaxSubPic;

        CComPtr<ISubPicProvider> pSubPicProvider;
        GetSubPicProvider(&pSubPicProvider);
        CComQIPtr<IXyCompatProvider> pXySubPicProvider = pSubPicProvider;
        if (pXySubPicProvider && SUCCEEDED(pSubPicProvider->Lock())) {
            for (REFERENCE_TIME rtStart = rtNow; !m_fBreakBuffering && GetQueueCount() < nMaxSubPic; rtStart += rtTimePerFrame) {
                REFERENCE_TIME rtStop = rtStart + rtTimePerFrame;

                if (m_rtNow >= rtStart) {
                    if (m_rtNow >= rtStop) {
                        continue;
                    }
                }

                if (rtStart >= m_rtNow + 60 * 10000000i64) {    // we are already one minute ahead, this should be enough
                    break;
                }

                if (m_rtNow > rtStop) {
                    TRACE(_T("BEHIND\n"));
                }

                HRESULT hr = pXySubPicProvider->RequestFrame(rtStart, rtStop, INFINITE);
                if (SUCCEEDED(hr)) {
                    ULONGLONG id;
                    hr = pXySubPicProvider->GetID(&id);
                    if (SUCCEEDED(hr)) {
                        SIZE    MaxTextureSize, VirtualSize;
                        POINT   VirtualTopLeft;
                        HRESULT hr2;

                        if (SUCCEEDED(hr2 = pSubPicProvider->GetTextureSize(0, MaxTextureSize, VirtualSize, VirtualTopLeft))) {
                            m_pAllocator->SetMaxTextureSize(MaxTextureSize);
                        }

                        if (m_llSubId == id && !m_Queue.IsEmpty()) { // same subtitle as last time
                            CComPtr<ISubPic> pSubPic = m_Queue.GetTail();
                            pSubPic->SetStop(rtStop);
#if SUBPIC_TRACE_LEVEL > 1
                            CRect r;
                            pSubPic->GetDirtyRect(&r);
                            TRACE(_T("Skip:   %f->%f      %dx%d\n"), double(pSubPic->GetStart()) / 10000000.0, double(pSubPic->GetStop()) / 10000000.0, r.Width(), r.Height());
#endif
                            continue;
                        } else {
                            CComPtr<ISubPic> pStatic;
                            if (FAILED(m_pAllocator->GetStatic(&pStatic))) {
                                break;
                            }

                            pStatic->SetInverseAlpha(true);
                            hr = RenderTo(pStatic, rtStart, rtStop, fps, true);
#if SUBPIC_TRACE_LEVEL > 0
                            CRect r;
                            pStatic->GetDirtyRect(&r);
                            TRACE(_T("Render: %f->%f      %dx%d\n"), double(rtStart) / 10000000.0, double(rtStop) / 10000000.0, r.Width(), r.Height());
#endif
                            if (FAILED(hr)) {
                                break;
                            }

                            CComPtr<ISubPic> pDynamic;
                            if (FAILED(m_pAllocator->AllocDynamic(&pDynamic))
                                    || FAILED(pStatic->CopyTo(pDynamic))) {
                                break;
                            }

                            if (SUCCEEDED(hr2)) {
                                pDynamic->SetVirtualTextureSize(VirtualSize, VirtualTopLeft);
                            }

                            RelativeTo relativeTo;
                            if (SUCCEEDED(pSubPicProvider->GetRelativeTo(0, relativeTo))) {
                                pDynamic->SetRelativeTo(relativeTo);
                            }

                            AppendQueue(pDynamic);
                            m_llSubId = id;
                            bAgain = true;
                        }
                    }
                }
            }

            pSubPicProvider->Unlock();
        }

        if (m_fBreakBuffering) {
            bAgain = true;
            CAutoLock cQueueLock(&m_csQueueLock);

            REFERENCE_TIME rtInvalidate = m_rtInvalidate;

            POSITION Iter = m_Queue.GetHeadPosition();
            while (Iter) {
                POSITION ThisPos = Iter;
                ISubPic* pSubPic = m_Queue.GetNext(Iter);

                REFERENCE_TIME rtStart = pSubPic->GetStart();
                REFERENCE_TIME rtStop = pSubPic->GetStop();

                if (rtStop > rtInvalidate) {
#if SUBPIC_TRACE_LEVEL >= 0
                    TRACE(_T("Removed subtitle because of invalidation: %f->%f\n"), double(rtStart) / 10000000.0, double(rtStop) / 10000000.0);
#endif
                    m_Queue.RemoveAt(ThisPos);
                    continue;
                }
            }

            /*
            while (GetCount() && GetTail()->GetStop() > rtInvalidate)
            {
                if (GetTail()->GetStart() < rtInvalidate) GetTail()->SetStop(rtInvalidate);
                else
                {
                    RemoveTail();
                }
            }
            */

            m_fBreakBuffering = false;
        }
    }

    return 0;
}
Exemplo n.º 4
0
STDMETHODIMP_(bool) CXySubPicQueueNoThread::LookupSubPic(REFERENCE_TIME rtNow, CComPtr<ISubPic>& ppSubPic)
{
    CComPtr<ISubPic> pSubPic;
    CComPtr<ISubPicProvider> pSubPicProvider;
    GetSubPicProvider(&pSubPicProvider);
    CComQIPtr<IXyCompatProvider> pXySubPicProvider = pSubPicProvider;

    if (pXySubPicProvider) {
        double fps = m_fps;
        REFERENCE_TIME rtTimePerFrame = (REFERENCE_TIME)(10000000.0 / fps);

        REFERENCE_TIME rtStart = rtNow;
        REFERENCE_TIME rtStop = rtNow + rtTimePerFrame;

        HRESULT hr = pXySubPicProvider->RequestFrame(rtStart, rtStop, (DWORD)(1000.0 / fps));
        if (SUCCEEDED(hr)) {
            ULONGLONG id;
            hr = pXySubPicProvider->GetID(&id);
            if (SUCCEEDED(hr)) {
                SIZE    MaxTextureSize, VirtualSize;
                POINT   VirtualTopLeft;
                HRESULT hr2;
                if (SUCCEEDED(hr2 = pSubPicProvider->GetTextureSize(0, MaxTextureSize, VirtualSize, VirtualTopLeft))) {
                    m_pAllocator->SetMaxTextureSize(MaxTextureSize);
                }

                if (!pSubPic) {
                    if (FAILED(m_pAllocator->AllocDynamic(&pSubPic))) {
                        return false;
                    }
                }

                if (m_pSubPic && m_llSubId == id) { // same subtitle as last time
                    pSubPic->SetStop(rtStop);
                    ppSubPic = m_pSubPic;
                } else if (m_pAllocator->IsDynamicWriteOnly()) {
                    CComPtr<ISubPic> pStatic;
                    hr = m_pAllocator->GetStatic(&pStatic);
                    if (SUCCEEDED(hr)) {
                        pStatic->SetInverseAlpha(true);
                        hr = RenderTo(pStatic, rtStart, rtStop, fps, true);
                    }
                    if (SUCCEEDED(hr)) {
                        hr = pStatic->CopyTo(pSubPic);
                    }
                    if (SUCCEEDED(hr)) {
                        ppSubPic = pSubPic;
                        m_llSubId = id;
                    }
                } else {
                    pSubPic->SetInverseAlpha(true);
                    if (SUCCEEDED(RenderTo(pSubPic, rtStart, rtStop, fps, true))) {
                        ppSubPic = pSubPic;
                        m_llSubId = id;
                    }
                }

                if (SUCCEEDED(hr2)) {
                    pSubPic->SetVirtualTextureSize(VirtualSize, VirtualTopLeft);
                }

                RelativeTo relativeTo;
                if (SUCCEEDED(pSubPicProvider->GetRelativeTo(0, relativeTo))) {
                    pSubPic->SetRelativeTo(relativeTo);
                }
            }
        }

        if (ppSubPic) {
            CAutoLock cAutoLock(&m_csLock);

            m_pSubPic = ppSubPic;
        }
    }

    return !!ppSubPic;
}
Exemplo n.º 5
0
STDMETHODIMP_(bool) CSubPicQueueNoThread::LookupSubPic(REFERENCE_TIME rtNow, CComPtr<ISubPic>& ppSubPic)
{
    CComPtr<ISubPic> pSubPic;

    {
        CAutoLock cAutoLock(&m_csLock);

        pSubPic = m_pSubPic;
    }

    if (pSubPic && pSubPic->GetStart() <= rtNow && rtNow < pSubPic->GetStop()) {
        ppSubPic = pSubPic;
    } else {
        CComPtr<ISubPicProvider> pSubPicProvider;
        if (SUCCEEDED(GetSubPicProvider(&pSubPicProvider)) && pSubPicProvider
                && SUCCEEDED(pSubPicProvider->Lock())) {
            double fps = m_fps;
            POSITION pos = pSubPicProvider->GetStartPosition(rtNow, fps);
            if (pos) {
                REFERENCE_TIME rtStart;
                REFERENCE_TIME rtStop;

                if (pSubPicProvider->IsAnimated(pos)) {
                    rtStart = rtNow;
                    rtStop = rtNow + 1;
                } else {
                    rtStart = pSubPicProvider->GetStart(pos, fps);
                    rtStop = pSubPicProvider->GetStop(pos, fps);
                }

                if (rtStart <= rtNow && rtNow < rtStop) {
                    bool    bAllocSubPic = !pSubPic;
                    SIZE    MaxTextureSize, VirtualSize;
                    POINT   VirtualTopLeft;
                    HRESULT hr;
                    if (SUCCEEDED(hr = pSubPicProvider->GetTextureSize(pos, MaxTextureSize, VirtualSize, VirtualTopLeft))) {
                        m_pAllocator->SetMaxTextureSize(MaxTextureSize);
                        if (!bAllocSubPic) {
                            // Ensure the previously allocated subpic is big enough to hold the subtitle to be rendered
                            SIZE maxSize;
                            bAllocSubPic = FAILED(pSubPic->GetMaxSize(&maxSize)) || maxSize.cx < MaxTextureSize.cx || maxSize.cy < MaxTextureSize.cy;
                        }
                    }

                    if (bAllocSubPic) {
                        CAutoLock cAutoLock(&m_csLock);

                        m_pSubPic.Release();

                        if (FAILED(m_pAllocator->AllocDynamic(&m_pSubPic))) {
                            return false;
                        }

                        pSubPic = m_pSubPic;
                    }

                    if (m_pAllocator->IsDynamicWriteOnly()) {
                        CComPtr<ISubPic> pStatic;
                        if (SUCCEEDED(m_pAllocator->GetStatic(&pStatic))
                                && SUCCEEDED(RenderTo(pStatic, rtStart, rtStop, fps, false))
                                && SUCCEEDED(pStatic->CopyTo(pSubPic))) {
                            ppSubPic = pSubPic;
                        }
                    } else {
                        if (SUCCEEDED(RenderTo(pSubPic, rtStart, rtStop, fps, false))) {
                            ppSubPic = pSubPic;
                        }
                    }

                    if (ppSubPic) {
                        if (SUCCEEDED(hr)) {
                            ppSubPic->SetVirtualTextureSize(VirtualSize, VirtualTopLeft);
                        }

                        RelativeTo relativeTo;
                        if (SUCCEEDED(pSubPicProvider->GetRelativeTo(pos, relativeTo))) {
                            ppSubPic->SetRelativeTo(relativeTo);
                        }
                    }
                }
            }

            pSubPicProvider->Unlock();
        }
    }

    return !!ppSubPic;
}
Exemplo n.º 6
0
DWORD CSubPicQueue::ThreadProc()
{
    BOOL bDisableAnim = m_bDisableAnim;
    SetThreadName(DWORD(-1), "Subtitle Renderer Thread");
    SetThreadPriority(m_hThread, bDisableAnim ? THREAD_PRIORITY_LOWEST : THREAD_PRIORITY_ABOVE_NORMAL);

    bool bAgain = true;
    for (;;) {
        DWORD Ret = WaitForMultipleObjects(EVENT_COUNT, m_ThreadEvents, FALSE, bAgain ? 0 : INFINITE);
        bAgain = false;

        if (Ret == WAIT_TIMEOUT) {
            ;
        } else if ((Ret - WAIT_OBJECT_0) != EVENT_TIME) {
            break;
        }
        double fps = m_fps;
        REFERENCE_TIME rtTimePerFrame = (REFERENCE_TIME)(10000000.0 / fps);
        REFERENCE_TIME rtNow = UpdateQueue();

        int nMaxSubPic = m_nMaxSubPic;

        CComPtr<ISubPicProvider> pSubPicProvider;
        if (SUCCEEDED(GetSubPicProvider(&pSubPicProvider)) && pSubPicProvider
                && SUCCEEDED(pSubPicProvider->Lock())) {
            for (POSITION pos = pSubPicProvider->GetStartPosition(rtNow, fps);
                    pos && !m_fBreakBuffering && GetQueueCount() < nMaxSubPic;
                    pos = pSubPicProvider->GetNext(pos)) {
                REFERENCE_TIME rtStart = pSubPicProvider->GetStart(pos, fps);
                REFERENCE_TIME rtStop = pSubPicProvider->GetStop(pos, fps);

                if (m_rtNow >= rtStart) {
                    if (m_rtNow >= rtStop) {
                        continue;
                    }
                }

                if (rtStart >= m_rtNow + 60 * 10000000i64) {    // we are already one minute ahead, this should be enough
                    break;
                }

                if (rtNow < rtStop) {
                    REFERENCE_TIME rtCurrent = std::max(rtNow, rtStart);
                    bool bIsAnimated = pSubPicProvider->IsAnimated(pos) && !bDisableAnim;
                    while (rtCurrent < rtStop) {
                        SIZE    MaxTextureSize, VirtualSize;
                        POINT   VirtualTopLeft;
                        HRESULT hr2;

                        if (SUCCEEDED(hr2 = pSubPicProvider->GetTextureSize(pos, MaxTextureSize, VirtualSize, VirtualTopLeft))) {
                            m_pAllocator->SetMaxTextureSize(MaxTextureSize);
                        }

                        CComPtr<ISubPic> pStatic;
                        if (FAILED(m_pAllocator->GetStatic(&pStatic))) {
                            break;
                        }

                        HRESULT hr;
                        if (bIsAnimated) {
                            REFERENCE_TIME rtEndThis = std::min(rtCurrent + rtTimePerFrame, rtStop);
                            hr = RenderTo(pStatic, rtCurrent, rtEndThis, fps, bIsAnimated);
                            pStatic->SetSegmentStart(rtStart);
                            pStatic->SetSegmentStop(rtStop);
#if SUBPIC_TRACE_LEVEL > 0
                            CRect r;
                            pStatic->GetDirtyRect(&r);
                            TRACE(_T("Render: %f->%f    %f->%f      %dx%d\n"), double(rtCurrent) / 10000000.0, double(rtEndThis) / 10000000.0, double(rtStart) / 10000000.0, double(rtStop) / 10000000.0, r.Width(), r.Height());
#endif
                            rtCurrent = rtEndThis;
                        } else {
                            hr = RenderTo(pStatic, rtStart, rtStop, fps, bIsAnimated);
                            // Non-animated subtitles aren't part of a segment
                            pStatic->SetSegmentStart(0);
                            pStatic->SetSegmentStop(0);
                            rtCurrent = rtStop;
                        }
#if SUBPIC_TRACE_LEVEL > 0
                        if (m_rtNow > rtCurrent) {
                            TRACE(_T("BEHIND\n"));
                        }
#endif

                        if (FAILED(hr)) {
                            break;
                        }

                        if (S_OK != hr) { // subpic was probably empty
                            continue;
                        }

                        CComPtr<ISubPic> pDynamic;
                        if (FAILED(m_pAllocator->AllocDynamic(&pDynamic))
                                || FAILED(pStatic->CopyTo(pDynamic))) {
                            break;
                        }

                        if (SUCCEEDED(hr2)) {
                            pDynamic->SetVirtualTextureSize(VirtualSize, VirtualTopLeft);
                        }

                        RelativeTo relativeTo;
                        if (SUCCEEDED(pSubPicProvider->GetRelativeTo(pos, relativeTo))) {
                            pDynamic->SetRelativeTo(relativeTo);
                        }

                        AppendQueue(pDynamic);
                        bAgain = true;

                        if (GetQueueCount() >= nMaxSubPic) {
                            break;
                        }
                    }
                }
            }

            pSubPicProvider->Unlock();
        }

        if (m_fBreakBuffering) {
            bAgain = true;
            CAutoLock cQueueLock(&m_csQueueLock);

            REFERENCE_TIME rtInvalidate = m_rtInvalidate;

            POSITION Iter = m_Queue.GetHeadPosition();
            while (Iter) {
                POSITION ThisPos = Iter;
                ISubPic* pSubPic = m_Queue.GetNext(Iter);

                REFERENCE_TIME rtStart = pSubPic->GetStart();
                REFERENCE_TIME rtStop = pSubPic->GetStop();
                REFERENCE_TIME rtSegmentStop = pSubPic->GetSegmentStop();

                if (rtSegmentStop > rtInvalidate) {
#if SUBPIC_TRACE_LEVEL >= 0
                    TRACE(_T("Removed subtitle because of invalidation: %f -> %f (%f)\n"),
                          double(rtStart) / 10000000.0, double(rtStop) / 10000000.0, double(rtSegmentStop) / 10000000.0);
#endif
                    m_Queue.RemoveAt(ThisPos);
                    continue;
                }
            }

            /*
            while (GetCount() && GetTail()->GetStop() > rtInvalidate)
            {
                if (GetTail()->GetStart() < rtInvalidate) GetTail()->SetStop(rtInvalidate);
                else
                {
                    RemoveTail();
                }
            }
            */

            m_fBreakBuffering = false;
        }
    }

    return 0;
}
Exemplo n.º 7
0
STDMETHODIMP_(bool) CSubPicQueueNoThread::LookupSubPic(REFERENCE_TIME rtNow, CComPtr<ISubPic> &ppSubPic)
{

    CComPtr<ISubPic> pSubPic;

    {
        CAutoLock cAutoLock(&m_csLock);

        if(!m_pSubPic)
        {
            if(FAILED(m_pAllocator->AllocDynamic(&m_pSubPic)))
                return(false);
        }

        pSubPic = m_pSubPic;
    }

    if(pSubPic->GetStart() <= rtNow && rtNow < pSubPic->GetStop())
    {
        ppSubPic = pSubPic;
    }
    else
    {
        CComPtr<ISubPicProvider> pSubPicProvider;
        if(SUCCEEDED(GetSubPicProvider(&pSubPicProvider)) && pSubPicProvider
            && SUCCEEDED(pSubPicProvider->Lock()))
        {
            double fps = m_fps;

            if(POSITION pos = pSubPicProvider->GetStartPosition(rtNow, fps))
            {
                REFERENCE_TIME rtStart = pSubPicProvider->GetStart(pos, fps);
                REFERENCE_TIME rtStop = pSubPicProvider->GetStop(pos, fps);

                if(pSubPicProvider->IsAnimated(pos))
                {
                    rtStart = rtNow;
                    rtStop = rtNow+1;
                }

                if(rtStart <= rtNow && rtNow < rtStop)
                {
                    SIZE	MaxTextureSize, VirtualSize;
                    POINT	VirtualTopLeft;
                    HRESULT	hr2;
                    if (SUCCEEDED (hr2 = pSubPicProvider->GetTextureSize(pos, MaxTextureSize, VirtualSize, VirtualTopLeft)))
                        m_pAllocator->SetMaxTextureSize(MaxTextureSize);

                    if(m_pAllocator->IsDynamicWriteOnly())
                    {
                        CComPtr<ISubPic> pStatic;
                        if(SUCCEEDED(m_pAllocator->GetStatic(&pStatic))
                            && SUCCEEDED(RenderTo(pStatic, rtStart, rtStop, fps, false))
                            && SUCCEEDED(pStatic->CopyTo(pSubPic)))
                            ppSubPic = pSubPic;
                    }
                    else
                    {
                        if(SUCCEEDED(RenderTo(m_pSubPic, rtStart, rtStop, fps, false)))
                            ppSubPic = pSubPic;
                    }
                    if (SUCCEEDED(hr2))
                        pSubPic->SetVirtualTextureSize (VirtualSize, VirtualTopLeft);
                }
            }

            pSubPicProvider->Unlock();

            if(ppSubPic)
            {
                CAutoLock cAutoLock(&m_csLock);

                m_pSubPic = ppSubPic;
            }
        }
    }

    return(!!ppSubPic);
}
Exemplo n.º 8
0
DWORD CSubPicQueue::ThreadProc()
{	
    BOOL bDisableAnim = m_bDisableAnim;
    SetThreadPriority(m_hThread, bDisableAnim ? THREAD_PRIORITY_LOWEST : THREAD_PRIORITY_ABOVE_NORMAL/*THREAD_PRIORITY_BELOW_NORMAL*/);

    bool bAgain = true;
    while(1)
    {
        DWORD Ret = WaitForMultipleObjects(EVENT_COUNT, m_ThreadEvents, FALSE, bAgain ? 0 : INFINITE);
        bAgain = false;

        if (Ret == WAIT_TIMEOUT)
            ;
        else if ((Ret - WAIT_OBJECT_0) != EVENT_TIME)
            break;
        double fps = m_fps;
        REFERENCE_TIME rtTimePerFrame = max(10000000.0/fps*1.5, 1000000); //1.5 to reduce flick
        REFERENCE_TIME rtNow = UpdateQueue();

        int nMaxSubPic = m_nMaxSubPic;

        CComPtr<ISubPicProvider> pSubPicProvider;
        if(SUCCEEDED(GetSubPicProvider(&pSubPicProvider)) && pSubPicProvider
            && SUCCEEDED(pSubPicProvider->Lock()) && SUCCEEDED(m_pAllocator->Lock()))
        {
            for(POSITION pos = pSubPicProvider->GetStartPosition(rtNow, fps); 
                pos && !m_fBreakBuffering && GetQueueCount() < (size_t)nMaxSubPic; 
                pos = pSubPicProvider->GetNext(pos))
            {
                REFERENCE_TIME rtStart = pSubPicProvider->GetStart(pos, fps);
                REFERENCE_TIME rtStop = pSubPicProvider->GetStop(pos, fps);

                if(m_rtNow >= rtStart)
                {
                    //						m_fBufferUnderrun = true;
                    if(m_rtNow >= rtStop) continue;
                }

                if(rtStart >= m_rtNow + 60*10000000i64) // we are already one minute ahead, this should be enough
                    break;

                if(rtNow < rtStop)
                {
                    REFERENCE_TIME rtCurrent = max(rtNow, rtStart);
                    bool bIsAnimated = pSubPicProvider->IsAnimated(pos) && !bDisableAnim;
                    while (rtCurrent < rtStop)
                    {

                        SIZE	MaxTextureSize, VirtualSize;
                        POINT	VirtualTopLeft;
                        HRESULT	hr2;
                        if (SUCCEEDED (hr2 = pSubPicProvider->GetTextureSize(pos, MaxTextureSize, VirtualSize, VirtualTopLeft)))
                            m_pAllocator->SetMaxTextureSize(MaxTextureSize);

                        CComPtr<ISubPic> pStatic;
                        if(FAILED(m_pAllocator->GetStatic(&pStatic)))
                            break;

                        HRESULT hr;
                        if (bIsAnimated)
                        {
                            //if (rtCurrent < m_rtNow + rtTimePerFrame)
                             //   rtCurrent = min(m_rtNow + rtTimePerFrame, rtStop-1);

                            REFERENCE_TIME rtEndThis = min(rtCurrent + rtTimePerFrame, rtStop);
                            hr = RenderTo(pStatic, rtCurrent, rtEndThis, fps, bIsAnimated);
                            pStatic->SetSegmentStart(rtStart);
                            pStatic->SetSegmentStop(rtStop);
#if DSubPicTraceLevel > 0
                            CRect r;
                            pStatic->GetDirtyRect(&r);
                            TRACE("Render: %f %f %f->%f    %f->%f      %dx%d\n",m_fps ,(double)rtTimePerFrame/ 10000000.0 , double(rtCurrent) / 10000000.0, double(rtEndThis) / 10000000.0, double(rtStart) / 10000000.0, double(rtStop) / 10000000.0, r.Width(), r.Height());
#endif
                            rtCurrent = rtEndThis;


                        }
                        else
                        {
                            hr = RenderTo(pStatic, rtStart, rtStop, fps, bIsAnimated);
                            rtCurrent = rtStop;
                        }			
#if DSubPicTraceLevel > 0
                        if (m_rtNow > rtCurrent)
                        {
                            TRACE("BEHIND\n");
                        }
#endif

                        if(FAILED(hr))
                            break;

                        if(S_OK != hr) // subpic was probably empty
                            continue;

                        CComPtr<ISubPic> pDynamic;
                        if(FAILED(m_pAllocator->AllocDynamic(&pDynamic))
                            || FAILED(pStatic->CopyTo(pDynamic)))
                            break;

                        if (SUCCEEDED (hr2))
                            pDynamic->SetVirtualTextureSize (VirtualSize, VirtualTopLeft);

                        AppendQueue(pDynamic);
                        bAgain = true;

                        if (GetQueueCount() >= (size_t)nMaxSubPic)
                            break;
                    }
                }
            }

            pSubPicProvider->Unlock();
            m_pAllocator->Unlock();
        }

        if(m_fBreakBuffering)
        {
            bAgain = true;
            CAutoLock cQueueLock(&m_csQueueLock);

            REFERENCE_TIME rtInvalidate = m_rtInvalidate;

            POSITION Iter = m_Queue.GetHeadPosition();
            while(Iter)
            {
                POSITION ThisPos = Iter;
                ISubPic *pSubPic = m_Queue.GetNext(Iter);

                REFERENCE_TIME rtStart = pSubPic->GetStart();
                REFERENCE_TIME rtStop = pSubPic->GetStop();

                if (rtStop > rtInvalidate)
                {
#if DSubPicTraceLevel >= 0
                    TRACE(("Removed subtitle because of invalidation: %f->%f\n"), double(rtStart) / 10000000.0, double(rtStop) / 10000000.0);
#endif
                    m_Queue.RemoveAt(ThisPos);
                    continue;
                }
            }

            /*
            while(GetCount() && GetTail()->GetStop() > rtInvalidate)
            {
            if(GetTail()->GetStart() < rtInvalidate) GetTail()->SetStop(rtInvalidate);
            else 
            {
            RemoveTail();
            }
            }
            */

            m_fBreakBuffering = false;
        }
    }

    return(0);
}