2 Copyright 2004-2005 Chris Tallon
4 This file is part of VOMP.
6 VOMP is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 VOMP is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with VOMP; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "dssourcefilter.h"
24 #include "dsallocator.h"
41 filtermutex=CreateMutex(NULL,FALSE,NULL);
43 offsetvideonotset=true;
44 offsetaudionotset=true;
51 cur_audio_media_sample=NULL;
52 cur_video_media_sample=NULL;
66 CloseHandle(filtermutex);
73 int VideoWin::init(UCHAR tformat)
75 if (initted) return 0;
78 tvsize=Video::ASPECT16X9; //Internally Vomp should think we are a 16:9 TV
82 if (!setFormat(tformat)){ shutdown(); return 0; }
86 int VideoWin::setTVsize(UCHAR ttvsize)
92 int VideoWin::setDefaultAspect()
94 return setAspectRatio(Video::ASPECT4X3);
97 int VideoWin::shutdown()
99 if (!initted) return 0;
104 int VideoWin::setFormat(UCHAR tformat)
106 if (!initted) return 0;
107 if ((tformat != PAL) && (tformat != NTSC)) return 0;
123 int VideoWin::setConnection(UCHAR tconnection)
125 if (!initted) return 0;
126 if ((tconnection != COMPOSITERGB) && (tconnection != SVIDEO)) return 0;
127 connection = tconnection;
132 int VideoWin::setAspectRatio(UCHAR taspectRatio)
134 if (!initted) return 0;
135 if ((taspectRatio != ASPECT4X3) && (taspectRatio != ASPECT16X9)) return 0;
136 aspectRatio = taspectRatio;
141 int VideoWin::setMode(UCHAR tmode)
143 if (!initted) return 0;
145 //if ((tmode == LETTERBOX) && (tvsize == ASPECT16X9)) return 0; // invalid mode
147 if ((tmode != NORMAL) && (tmode != LETTERBOX) && (tmode != UNKNOWN2) && (tmode != QUARTER) && (tmode != EIGHTH)
148 && (tmode != ZOOM) && (tmode != UNKNOWN6)) return 0;
157 int VideoWin::signalOff()
162 int VideoWin::signalOn()
167 int VideoWin::setSource()
169 if (!initted) return 0;
174 int VideoWin::setPosition(int x, int y)
176 if (!initted) return 0;
177 if (mode==QUARTER || mode==EIGHTH) {
186 if (!initted) return 0;
191 #ifdef DS_DEBUG // This stuff would not included in vomp due to lincemse restrcitions
192 #include "dshelper.h"
199 if (!initted) return 0;
204 if (hres=CoCreateInstance(CLSID_FilterGraph,NULL,CLSCTX_INPROC_SERVER,
205 IID_IGraphBuilder,(void**)&dsgraphbuilder)!=S_OK) {
209 AddToRot(dsgraphbuilder,&graphidentifier);
211 //This is just a try to see if building the graph works
212 // dsgraphbuilder->RenderFile(L"D:\\Projekte\\VTP Client\\test.mpa" ,NULL);
213 //So this is the real code, this prevents the feeder from calling noexisting objects!
214 WaitForSingleObject(filtermutex,INFINITE);
216 sourcefilter=new DsSourceFilter(); //Creating our Source filter for pushing Data
218 if (hres=dsgraphbuilder->AddFilter(sourcefilter,L"Vomp Win Source Filter")!=S_OK) {
219 Log::getInstance()->log("VideoWin", Log::WARN , "Failed adding Vomp Source Filter!");
221 ReleaseMutex(filtermutex);
225 if (hres=dsgraphbuilder->Render(sourcefilter->GetPin(0)/*audio*/)!=S_OK) {
226 Log::getInstance()->log("VideoWin", Log::WARN , "Failed rendering audio!");
228 ReleaseMutex(filtermutex);
234 //We alloc the vmr9 as next step
235 if (hres=CoCreateInstance(CLSID_VideoMixingRenderer9,0,
236 CLSCTX_INPROC_SERVER,IID_IBaseFilter,(void**) &dsvmrrenderer)!=S_OK) {
237 Log::getInstance()->log("VideoWin", Log::WARN ,"Failed creating VMR9 renderer!");
239 ReleaseMutex(filtermutex);
242 if (hres=dsgraphbuilder->AddFilter(dsvmrrenderer,L"VMR9")!=S_OK) {
244 Log::getInstance()->log("VideoWin", Log::WARN ,"Failed adding VMR9 renderer!");
245 ReleaseMutex(filtermutex);
248 IVMRFilterConfig9* vmrfilconfig;
249 if (dsvmrrenderer->QueryInterface(IID_IVMRFilterConfig9,(void**)&vmrfilconfig)!=S_OK) {
251 Log::getInstance()->log("VideoWin", Log::WARN ,"Failed getting VMR9 Filterconfig interface!");
252 ReleaseMutex(filtermutex);
255 vmrfilconfig->SetRenderingMode(VMR9Mode_Renderless);
256 vmrfilconfig->Release();
258 if (dsvmrrenderer->QueryInterface(IID_IVMRSurfaceAllocatorNotify9,(void**)& dsvmrsurfnotify)!=S_OK) {
260 Log::getInstance()->log("VideoWin", Log::WARN ,"Failed getting VMR9 Surface Allocator interface!");
261 ReleaseMutex(filtermutex);
264 allocatorvmr=new DsAllocator();
265 dsvmrsurfnotify->AdviseSurfaceAllocator(NULL,allocatorvmr);
266 allocatorvmr->AdviseNotify(dsvmrsurfnotify);
271 IFilterGraph2*fg2=NULL;
272 if (dsgraphbuilder->QueryInterface(IID_IFilterGraph2,(void**)&fg2)!=S_OK) {
273 Log::getInstance()->log("VideoWin", Log::WARN , "Failed querying for FilterGraph2 Interface!");
275 ReleaseMutex(filtermutex);
278 if (hres=fg2->RenderEx(sourcefilter->GetPin(1)/*video*/,
279 AM_RENDEREX_RENDERTOEXISTINGRENDERERS,NULL)!=S_OK) {
280 Log::getInstance()->log("VideoWin", Log::WARN , "Failed rendering Video!");
282 ReleaseMutex(filtermutex);
287 if (hres=CoCreateInstance(CLSID_SystemClock,NULL,CLSCTX_INPROC_SERVER,
288 IID_IReferenceClock,(void**)&dsrefclock)!=S_OK) {
292 dsgraphbuilder->QueryInterface(IID_IMediaFilter,(void **) &dsmediafilter);
293 dsmediafilter->SetSyncSource(dsrefclock);
295 dsgraphbuilder->QueryInterface(IID_IMediaControl,(void **) &dsmediacontrol);
297 dsmediacontrol->Run();
298 ReleaseMutex(filtermutex);
304 if (!initted) return 0;
312 int VideoWin::reset()
314 if (!initted) return 0;
321 int VideoWin::pause()
323 if (!initted) return 0;
324 if (dsmediacontrol) dsmediacontrol->Pause();
328 int VideoWin::unPause() // FIXME get rid - same as play!!
329 {//No on windows this is not the same, I don't get rid of!
330 if (!initted) return 0;
331 if (dsmediacontrol) dsmediacontrol->Run();
335 int VideoWin::fastForward()
337 if (!initted) return 0;
341 int VideoWin::unFastForward()
343 if (!initted) return 0;
347 int VideoWin::attachFrameBuffer()
349 if (!initted) return 0;
353 int VideoWin::blank(void)
358 ULLONG VideoWin::getCurrentTimestamp()
360 REFERENCE_TIME cr_time,startoffset;
362 if (!dsrefclock || !sourcefilter) return 0;
364 dsrefclock->GetTime(&cr_time);
365 startoffset=sourcefilter->getStartOffset();
366 cr_time-=startoffset;
367 cr_time-=lastreftimeRT;
368 ULLONG result=frameNumberToTimecode(
369 VDR::getInstance()->frameNumberFromPosition(lastreftimeBYTE));
370 result+=(ULLONG)(cr_time/10000LL*90LL);
375 ULONG VideoWin::timecodeToFrameNumber(ULLONG timecode)
377 if (format == PAL) return (ULONG)(((double)timecode / (double)90000) * (double)25);
378 else return (ULONG)(((double)timecode / (double)90000) * (double)30);
381 ULLONG VideoWin::frameNumberToTimecode(ULONG framenumber)
383 if (format == PAL) return (ULLONG)(((double)framenumber * (double)90000) / (double)25);
384 else return (ULLONG)(((double)framenumber * (double)90000) / (double)30);
387 void VideoWin::CleanupDS()
389 WaitForSingleObject(filtermutex,INFINITE);
390 if (cur_audio_media_sample) {
391 cur_audio_media_sample->Release();
392 cur_audio_media_sample=NULL;
394 if (cur_video_media_sample) {
395 cur_video_media_sample->Release();
396 cur_video_media_sample=NULL;
398 if (dsvmrsurfnotify) {
399 dsvmrsurfnotify->Release();
400 dsvmrsurfnotify=NULL;
403 dsvmrrenderer->Release();
408 allocatorvmr->Release();
413 dsrefclock->Release();
417 dsmediafilter->Release();
421 if (dsmediacontrol) {
422 dsmediacontrol->Stop();
423 dsmediacontrol->Release();
428 RemoveFromRot(graphidentifier);
430 dsgraphbuilder->Release();
432 sourcefilter=NULL; //The Graph Builder destroys our SourceFilter
434 ReleaseMutex(filtermutex);
439 UINT VideoWin::DeliverMediaSample(MediaPacket packet,
443 /*First Check, if we have an audio sample*/
445 /*First Check, if we have an audio sample*/
447 IMediaSample* ms=NULL;
448 REFERENCE_TIME reftime1=0;
449 REFERENCE_TIME reftime2=0;
452 if (packet.disconti) {
454 DeliverVideoMediaSample();
459 /*Inspect PES-Header */
461 if (*samplepos==0) {//stripheader
462 headerstrip=buffer[packet.pos_buffer+8]+9/*is this right*/;
463 *samplepos+=headerstrip;
464 if ( packet.synched ) {
465 DeliverVideoMediaSample();//write out old data
466 /* if (packet.presentation_time<0) { //Preroll?
467 *samplepos=packet.length;//if we have not processed at least one
468 return packet.length;//synched packet ignore it!
471 reftime1=packet.presentation_time;
475 if (!firstsynched) {//
476 *samplepos=packet.length;//if we have not processed at least one
477 return packet.length;//synched packet ignore it!
485 if (!getCurrentVideoMediaSample(&ms) || ms==NULL) {// get the current sample
490 ms_pos=ms->GetActualDataLength();
491 ms_length=ms->GetSize();
492 haveToCopy=min(ms_length-ms_pos,packet.length-*samplepos);
493 if ((ms_length-ms_pos)<1) {
494 DeliverVideoMediaSample(); //we are full!
495 if (!getCurrentVideoMediaSample(&ms) || ms==NULL) {// get the current sample
500 ms_pos=ms->GetActualDataLength();
501 ms_length=ms->GetSize();
502 haveToCopy=min(ms_length-ms_pos,packet.length-*samplepos);
504 ms->GetPointer(&ms_buf);
507 if (ms_pos==0) {//will only be changed on first packet
508 if (packet.disconti) {
509 ms->SetDiscontinuity(TRUE);
511 ms->SetDiscontinuity(FALSE);
513 if (packet.synched) {
514 ms->SetSyncPoint(TRUE);
515 ms->SetTime(&reftime1,&reftime2);
516 //ms->SetTime(NULL,NULL);
517 ms->SetMediaTime(NULL, NULL);
518 if (reftime1<0) ms->SetPreroll(TRUE);
519 else ms->SetPreroll(FALSE);
520 /*Timecode handling*/
521 lastreftimeRT=reftime1;
522 lastreftimeBYTE=packet.recording_byte_pos;
525 ms->SetSyncPoint(FALSE);
526 ms->SetTime(NULL,NULL);
527 ms->SetMediaTime(NULL, NULL);
528 ms->SetPreroll(FALSE);
530 // ms->SetSyncPoint(TRUE);
535 memcpy(ms_buf+ms_pos,buffer+packet.pos_buffer+*samplepos,haveToCopy);
536 ms->SetActualDataLength(haveToCopy+ms_pos);
538 *samplepos+=haveToCopy;
540 return haveToCopy+headerstrip;
544 *samplepos+=packet.length;
545 MILLISLEEP(0); //yet not implemented//bad idea
546 return packet.length;
550 int VideoWin::getCurrentAudioMediaSample(IMediaSample** ms)
552 //WaitForSingleObject(filtermutex,INFINITE);
554 // ReleaseMutex(filtermutex);
557 if (cur_audio_media_sample) {
558 *ms=cur_audio_media_sample;//already open
561 if (!sourcefilter->getCurrentAudioMediaSample(ms)) {
562 // ReleaseMutex(filtermutex);
564 if (*ms) (*ms)->SetActualDataLength(0);
565 cur_audio_media_sample=*ms;
566 //Don't release the mutex before deliver
570 int VideoWin::getCurrentVideoMediaSample(IMediaSample** ms)
572 //WaitForSingleObject(filtermutex,INFINITE);
574 // ReleaseMutex(filtermutex);
577 if (cur_video_media_sample) {
578 *ms=cur_video_media_sample;//already open
581 if (!sourcefilter->getCurrentVideoMediaSample(ms)) {
582 // ReleaseMutex(filtermutex);
584 if (*ms) (*ms)->SetActualDataLength(0);
586 cur_video_media_sample=*ms;
587 //Don't release the mutex before deliver
591 int VideoWin::DeliverAudioMediaSample(){
592 if (cur_audio_media_sample) {
593 sourcefilter->DeliverAudioMediaSample(cur_audio_media_sample);
594 cur_audio_media_sample=NULL;
596 //ReleaseMutex(filtermutex);
600 int VideoWin::DeliverVideoMediaSample(){
601 if (cur_video_media_sample) {
602 sourcefilter->DeliverVideoMediaSample(cur_video_media_sample);
603 cur_video_media_sample=NULL;
605 //ReleaseMutex(filtermutex);
609 long long VideoWin::SetStartOffset(long long curreftime, bool *rsync)
613 startoffset=curreftime;//offset is set for audio
615 offsetvideonotset=false;
619 if (offsetvideonotset) {
620 offsetvideonotset=false;
623 if ( (curreftime-lastrefvideotime)>10000000LL
624 || (curreftime-lastrefvideotime)<-10000000LL) {//if pts jumps to big resync
625 startoffset+=curreftime-lastrefvideotime;
626 lastrefaudiotime+=curreftime-lastrefvideotime;
628 offsetaudionotset=true;
634 lastrefvideotime=curreftime;
639 long long VideoWin::SetStartAudioOffset(long long curreftime, bool *rsync)
643 startoffset=curreftime;
645 offsetaudionotset=false;
647 if (offsetaudionotset) {
648 offsetaudionotset=false;
651 if ( (curreftime-lastrefaudiotime)>10000000LL
652 || (curreftime-lastrefaudiotime)<-10000000LL) {//if pts jumps to big resync
653 startoffset+=curreftime-lastrefaudiotime;
654 lastrefvideotime+=curreftime-lastrefaudiotime;
656 offsetvideonotset=true;
662 lastrefaudiotime=curreftime;
666 void VideoWin::ResetTimeOffsets() {
667 offsetnotset=true; //called from demuxer
668 offsetvideonotset=true;
669 offsetaudionotset=true;
686 int VideoWin::test2()