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; }
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); }
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; }
void ReadInEvents(void) { struct DosEvent newevent; union REGS r; int new_mouse_x,new_mouse_y,new_buttons; disable(); while(peek(0,0x41A)!=peek(0,0x41C)) { newevent.type=/*bioskey(1);*/peek(0x40,peek(0,0x41A)); newevent.time=Timer(); enable(); if((newevent.type&0x00FF)==0x00E0 && (newevent.type&0xFF00)>=0x3B00) newevent.type&=0xFF00; else if((newevent.type&0x00FF)!=0 || newevent.type==0x0300) newevent.type&=255; if(AppendQueue(&newevent)) bioskey(0); disable(); } enable(); r.w.ax=3; int86(0x33,&r,&r); /* qry pos & butt */ new_mouse_x=(r.w.cx+4)/8; new_mouse_y=(r.w.dx+4)/8; new_buttons=((r.w.bx&1)?LEFT_BUTTON:0) |((r.w.bx&2)?RIGHT_BUTTON:0) |((r.w.bx&4)?CENTER_BUTTON:0); if(new_mouse_x!=old_mouse_x || new_mouse_y!=old_mouse_y) /* mouse movement */ { newevent.type=M_MOVE; newevent.time=Timer(); newevent.info.coord.x=new_mouse_x; newevent.info.coord.y=new_mouse_y; if(AppendQueue(&newevent)) { old_mouse_x=new_mouse_x; old_mouse_y=new_mouse_y; } } if((old_buttons&LEFT_BUTTON) && !(new_buttons&LEFT_BUTTON)) { newevent.type=M_RBUTTON; newevent.time=Timer(); newevent.info.code=LEFT_BUTTON; if(AppendQueue(&newevent)) old_buttons&=~LEFT_BUTTON; } if(!(old_buttons&LEFT_BUTTON) && (new_buttons&LEFT_BUTTON)) { newevent.type=M_BUTTON; newevent.time=Timer(); newevent.info.code=LEFT_BUTTON; if(AppendQueue(&newevent)) old_buttons|=LEFT_BUTTON; } if((old_buttons&RIGHT_BUTTON) && !(new_buttons&RIGHT_BUTTON)) { newevent.type=M_RBUTTON; newevent.time=Timer(); newevent.info.code=RIGHT_BUTTON; if(AppendQueue(&newevent)) old_buttons&=~RIGHT_BUTTON; } if(!(old_buttons&RIGHT_BUTTON) && (new_buttons&RIGHT_BUTTON)) { newevent.type=M_BUTTON; newevent.time=Timer(); newevent.info.code=RIGHT_BUTTON; if(AppendQueue(&newevent)) old_buttons|=RIGHT_BUTTON; } if((old_buttons&CENTER_BUTTON) && !(new_buttons&CENTER_BUTTON)) { newevent.type=M_RBUTTON; newevent.time=Timer(); newevent.info.code=CENTER_BUTTON; if(AppendQueue(&newevent)) old_buttons&=~CENTER_BUTTON; } if(!(old_buttons&CENTER_BUTTON) && (new_buttons&CENTER_BUTTON)) { newevent.type=M_RBUTTON; newevent.time=Timer(); newevent.info.code=CENTER_BUTTON; if(AppendQueue(&newevent)) old_buttons|=CENTER_BUTTON; } }