X64 transport [Part 5] (Update plugins.cpp)
[xy_vsfilter.git] / src / subpic / SimpleSubPicProviderImpl.cpp
blobc276310928962d4af7106d28857ecf2173e5f92f
1 #include "stdafx.h"
2 #include "SimpleSubPicProviderImpl.h"
3 #include "SimpleSubPicWrapper.h"
4 #include "../subtitles/xy_bitmap.h"
5 #include "SimpleSubpicImpl.h"
6 #include "PooledSubPic.h"
7 #include "IDirectVobSubXy.h"
9 //////////////////////////////////////////////////////////////////////////
11 // SimpleSubPicProvider
12 //
15 SimpleSubPicProvider::SimpleSubPicProvider( int alpha_blt_dst_type, SIZE spd_size, RECT video_rect, IDirectVobSubXy *consumer
16 , HRESULT* phr/*=NULL*/ )
17 : CUnknown(NAME("CSubPicQueueImpl"), NULL)
18 , m_alpha_blt_dst_type(alpha_blt_dst_type)
19 , m_spd_size(spd_size)
20 , m_video_rect(video_rect)
21 , m_rtNow(0)
22 , m_fps(25.0)
23 , m_consumer(consumer)
25 if(phr) {
26 *phr = consumer ? S_OK : E_INVALIDARG;
29 m_prefered_colortype.AddTail(MSP_AYUV_PLANAR);
30 m_prefered_colortype.AddTail(MSP_AYUV);
31 m_prefered_colortype.AddTail(MSP_XY_AUYV);
32 m_prefered_colortype.AddTail(MSP_RGBA);
35 SimpleSubPicProvider::~SimpleSubPicProvider()
40 STDMETHODIMP SimpleSubPicProvider::NonDelegatingQueryInterface( REFIID riid, void** ppv )
42 return
43 QI(ISimpleSubPicProvider)
44 __super::NonDelegatingQueryInterface(riid, ppv);
47 STDMETHODIMP SimpleSubPicProvider::SetSubPicProvider( IUnknown* subpic_provider )
49 CComQIPtr<ISubPicProviderEx2> tmp = subpic_provider;
50 if (tmp)
52 CAutoLock cAutoLock(&m_csSubPicProvider);
53 m_pSubPicProviderEx = tmp;
55 if(m_pSubPicProviderEx!=NULL)
57 POSITION pos = m_prefered_colortype.GetHeadPosition();
58 while(pos!=NULL)
60 int color_type = m_prefered_colortype.GetNext(pos);
61 if( m_pSubPicProviderEx->IsColorTypeSupported( color_type ) &&
62 IsSpdColorTypeSupported( color_type ) )
64 m_spd_type = color_type;
65 break;
69 Invalidate();
71 return S_OK;
73 return E_NOTIMPL;
76 STDMETHODIMP SimpleSubPicProvider::GetSubPicProvider( IUnknown** subpic_provider )
78 if(!subpic_provider) {
79 return E_POINTER;
82 CAutoLock cAutoLock(&m_csSubPicProvider);
84 if(m_pSubPicProviderEx) {
85 *subpic_provider = m_pSubPicProviderEx;
86 (*subpic_provider)->AddRef();
89 return !!*subpic_provider ? S_OK : E_FAIL;
92 STDMETHODIMP SimpleSubPicProvider::SetFPS( double fps )
94 m_fps = fps;
96 return S_OK;
99 STDMETHODIMP SimpleSubPicProvider::SetTime( REFERENCE_TIME rtNow )
101 m_rtNow = rtNow;
103 return S_OK;
106 STDMETHODIMP SimpleSubPicProvider::Invalidate( REFERENCE_TIME rtInvalidate /*= -1*/ )
108 CAutoLock cQueueLock(&m_csLock);
110 if( m_pSubPic && m_subpic_stop >= rtInvalidate)
112 m_pSubPic = NULL;
114 return S_OK;
117 STDMETHODIMP_(bool) SimpleSubPicProvider::LookupSubPic( REFERENCE_TIME now /*[in]*/, ISimpleSubPic** output_subpic /*[out]*/ )
119 if(output_subpic!=NULL)
121 *output_subpic = NULL;
122 CComPtr<IXySubRenderFrame> temp = NULL;
123 bool result = LookupSubPicEx(now, &temp);
124 if (result && temp)
126 (*output_subpic = new SimpleSubpic(temp, m_alpha_blt_dst_type))->AddRef();
128 return result;
130 return false;
133 STDMETHODIMP SimpleSubPicProvider::GetStats( int& nSubPics, REFERENCE_TIME& rtNow, REFERENCE_TIME& rtStart, REFERENCE_TIME& rtStop )
135 CAutoLock cAutoLock(&m_csLock);
137 nSubPics = 0;
138 rtNow = m_rtNow;
139 rtStart = rtStop = 0;
141 if(m_pSubPic)
143 nSubPics = 1;
144 rtStart = m_subpic_start;
145 rtStop = m_subpic_stop;
148 return S_OK;
151 STDMETHODIMP SimpleSubPicProvider::GetStats( int nSubPic, REFERENCE_TIME& rtStart, REFERENCE_TIME& rtStop )
153 CAutoLock cAutoLock(&m_csLock);
155 if(!m_pSubPic || nSubPic != 0)
156 return E_INVALIDARG;
158 rtStart = m_subpic_start;
159 rtStop = m_subpic_stop;
161 return S_OK;
164 bool SimpleSubPicProvider::LookupSubPicEx(REFERENCE_TIME rtNow, IXySubRenderFrame** sub_render_frame)
166 if(!sub_render_frame)
167 return(false);
169 *sub_render_frame = NULL;
171 if(m_subpic_start <= rtNow && rtNow < m_subpic_stop && m_pSubPic)
173 (*sub_render_frame = m_pSubPic)->AddRef();
175 else
177 CComPtr<ISubPicProviderEx2> pSubPicProvider;
178 if(SUCCEEDED(GetSubPicProviderEx(&pSubPicProvider)) && pSubPicProvider
179 && SUCCEEDED(pSubPicProvider->Lock()))
181 double fps = m_fps;
183 if(POSITION pos = pSubPicProvider->GetStartPosition(rtNow, fps))
185 REFERENCE_TIME rtStart = pSubPicProvider->GetStart(pos, fps);
186 REFERENCE_TIME rtStop = pSubPicProvider->GetStop(pos, fps);
188 if(rtStart <= rtNow && rtNow < rtStop)
190 RenderTo(sub_render_frame, rtNow, rtNow+1, fps, true);
194 pSubPicProvider->Unlock();
196 if(*sub_render_frame)
198 CAutoLock cAutoLock(&m_csLock);
200 m_pSubPic = *sub_render_frame;
204 return(!!*sub_render_frame);
207 HRESULT SimpleSubPicProvider::GetSubPicProviderEx( ISubPicProviderEx2** pSubPicProviderEx )
209 if(!pSubPicProviderEx) {
210 return E_POINTER;
213 CAutoLock cAutoLock(&m_csSubPicProvider);
215 if(m_pSubPicProviderEx) {
216 *pSubPicProviderEx = m_pSubPicProviderEx;
217 (*pSubPicProviderEx)->AddRef();
220 return !!*pSubPicProviderEx ? S_OK : E_FAIL;
223 HRESULT SimpleSubPicProvider::RenderTo( IXySubRenderFrame** pSubPic, REFERENCE_TIME rtStart, REFERENCE_TIME rtStop, double fps, BOOL bIsAnimated )
225 HRESULT hr = E_FAIL;
227 if(!pSubPic) {
228 return hr;
231 CComPtr<ISubPicProviderEx2> pSubPicProviderEx;
232 if(FAILED(GetSubPicProviderEx(&pSubPicProviderEx)) || !pSubPicProviderEx) {
233 return hr;
236 if(FAILED(pSubPicProviderEx->Lock())) {
237 return hr;
240 CAtlList<CRect> rectList;
242 CComPtr<IXySubRenderFrame> sub_render_frame;
243 SIZE size_render_with;
244 ASSERT(m_consumer);
245 hr = m_consumer->XyGetSize(DirectVobSubXyOptions::SIZE_LAYOUT_WITH, &size_render_with);
246 if (FAILED(hr))
248 return hr;
251 hr = pSubPicProviderEx->RenderEx(pSubPic, m_spd_type, m_spd_size,
252 size_render_with, CRect(0,0,size_render_with.cx,size_render_with.cy),
253 bIsAnimated ? rtStart : ((rtStart+rtStop)/2), fps);
255 POSITION pos = pSubPicProviderEx->GetStartPosition(rtStart, fps);
257 pSubPicProviderEx->GetStartStop(pos, fps, m_subpic_start, m_subpic_stop);
259 pSubPicProviderEx->Unlock();
261 return hr;
264 bool SimpleSubPicProvider::IsSpdColorTypeSupported( int type )
266 if( (type==MSP_RGBA)
268 (type==MSP_XY_AUYV && m_alpha_blt_dst_type == MSP_YUY2)
270 (type==MSP_AYUV && m_alpha_blt_dst_type == MSP_AYUV)//ToDo: fix me MSP_AYUV
272 (type==MSP_AYUV_PLANAR && (m_alpha_blt_dst_type == MSP_IYUV ||
273 m_alpha_blt_dst_type == MSP_YV12 ||
274 m_alpha_blt_dst_type == MSP_P010 ||
275 m_alpha_blt_dst_type == MSP_P016 ||
276 m_alpha_blt_dst_type == MSP_NV12 ||
277 m_alpha_blt_dst_type == MSP_NV21)) )
279 return true;
281 return false;
284 //////////////////////////////////////////////////////////////////////////
286 // SimpleSubPicProvider2
289 SimpleSubPicProvider2::SimpleSubPicProvider2( int alpha_blt_dst_type, SIZE max_size, SIZE cur_size, RECT video_rect,
290 IDirectVobSubXy *consumer, HRESULT* phr/*=NULL*/)
291 : CUnknown(NAME("SimpleSubPicProvider2"), NULL)
292 , m_alpha_blt_dst_type(alpha_blt_dst_type)
293 , m_max_size(max_size)
294 , m_cur_size(cur_size)
295 , m_video_rect(video_rect)
296 , m_now(0)
297 , m_fps(25.0)
298 , m_consumer(consumer)
300 if (phr)
302 *phr= consumer ? S_OK : E_INVALIDARG;
304 m_ex_provider = NULL;
305 m_old_provider = NULL;
306 m_cur_provider = NULL;
309 SimpleSubPicProvider2::~SimpleSubPicProvider2()
311 delete m_ex_provider;
312 delete m_old_provider;
315 STDMETHODIMP SimpleSubPicProvider2::NonDelegatingQueryInterface( REFIID riid, void** ppv )
317 return
318 QI(ISimpleSubPicProvider)
319 __super::NonDelegatingQueryInterface(riid, ppv);
322 STDMETHODIMP SimpleSubPicProvider2::SetSubPicProvider( IUnknown* subpic_provider )
324 const int MAX_SUBPIC_QUEUE_LENGTH = 1;
325 HRESULT hr;
326 CComQIPtr<ISubPicProviderEx2> tmp = subpic_provider;
327 if (tmp)
329 m_cur_provider = NULL;
330 delete m_old_provider;
331 m_old_provider = NULL;
332 if (!m_ex_provider)
334 m_ex_provider = new SimpleSubPicProvider(m_alpha_blt_dst_type, m_cur_size, m_video_rect, m_consumer, &hr);
335 m_ex_provider->SetFPS(m_fps);
336 m_ex_provider->SetTime(m_now);
338 if (m_ex_provider==NULL)
340 ASSERT(m_ex_provider!=NULL);
341 return E_FAIL;
343 m_cur_provider = m_ex_provider;
345 else
347 m_cur_provider = NULL;
348 delete m_ex_provider;
349 m_ex_provider = NULL;
350 if (!m_old_provider)
352 CComPtr<ISubPicExAllocator> pSubPicAllocator = new CPooledSubPicAllocator(m_alpha_blt_dst_type,
353 m_max_size, MAX_SUBPIC_QUEUE_LENGTH + 1);
354 ASSERT(pSubPicAllocator);
355 pSubPicAllocator->SetCurSize(m_cur_size);
356 pSubPicAllocator->SetCurVidRect(m_video_rect);
357 m_old_provider = new CSubPicQueueNoThread(pSubPicAllocator, &hr);
358 m_old_provider->SetFPS(m_fps);
359 m_old_provider->SetTime(m_now);
360 if (FAILED(hr) || m_old_provider==NULL)
362 ASSERT(SUCCEEDED(hr));
363 return hr;
366 m_cur_provider = m_old_provider;
368 return m_cur_provider->SetSubPicProvider(subpic_provider);
371 STDMETHODIMP SimpleSubPicProvider2::GetSubPicProvider( IUnknown** subpic_provider )
373 ASSERT(m_cur_provider);
374 if (!m_cur_provider)
376 return S_FALSE;
378 return m_cur_provider->GetSubPicProvider(subpic_provider);
381 STDMETHODIMP SimpleSubPicProvider2::SetFPS( double fps )
383 m_fps = fps;
384 ASSERT(m_cur_provider);
385 if (!m_cur_provider)
387 return S_FALSE;
389 return m_cur_provider->SetFPS(fps);
392 STDMETHODIMP SimpleSubPicProvider2::SetTime( REFERENCE_TIME rtNow )
394 m_now = rtNow;
395 ASSERT(m_cur_provider);
396 if (!m_cur_provider)
398 return S_FALSE;
400 return m_cur_provider->SetTime(rtNow);
403 STDMETHODIMP SimpleSubPicProvider2::Invalidate( REFERENCE_TIME rtInvalidate /*= -1*/ )
405 //ASSERT(m_cur_provider);
406 if (!m_cur_provider)
408 return S_FALSE;
410 return m_cur_provider->Invalidate(rtInvalidate);
413 STDMETHODIMP_(bool) SimpleSubPicProvider2::LookupSubPic( REFERENCE_TIME now /*[in]*/, ISimpleSubPic** output_subpic/*[out]*/ )
415 ASSERT(m_cur_provider);
416 if (!m_cur_provider)
418 return false;
420 return m_cur_provider->LookupSubPic(now, output_subpic);
423 STDMETHODIMP SimpleSubPicProvider2::GetStats( int& nSubPics, REFERENCE_TIME& rtNow, REFERENCE_TIME& rtStart, REFERENCE_TIME& rtStop )
425 nSubPics = 0;
426 ASSERT(m_cur_provider);
427 if (!m_cur_provider)
429 return S_FALSE;
431 return m_cur_provider->GetStats(nSubPics, rtNow, rtStart, rtStop);
434 STDMETHODIMP SimpleSubPicProvider2::GetStats( int nSubPic, REFERENCE_TIME& rtStart, REFERENCE_TIME& rtStop )
436 ASSERT(m_cur_provider);
437 if (!m_cur_provider)
439 return S_FALSE;
441 return m_cur_provider->GetStats(nSubPic, rtStart, rtStop);