2 Copyright 2004-2005 Chris Tallon, 2009 Marten Richter
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 #include "mtdraspberry.h"
25 #include "osdopengl.h"
27 #include "woptionpane.h"
32 //A lot of parts of this file are heavily inspired by xbmc omx implementations
34 VideoOMX::VideoOMX() {
39 cur_input_buf_omx = NULL;
40 omx_h264 = omx_mpeg2 = true;
44 offsetvideonotset = true;
45 offsetaudionotset = true;
55 deinterlace=2; //advanced
64 int VideoOMX::init(UCHAR tformat)
66 if (initted) return 0;
69 if (!setFormat(tformat)) { shutdown(); return 0; }
70 if (!setConnection(COMPOSITERGB)) { shutdown(); return 0; }
71 if (!setAspectRatio(ASPECT4X3)) { shutdown(); return 0; }
72 if (!setMode(NORMAL)) { shutdown(); return 0; }
73 if (!setSource()) { shutdown(); return 0; }
74 if (!attachFrameBuffer()) { shutdown(); return 0; }
89 int VideoOMX::initUsingOSDObjects()
92 // we are called before the audio
95 if (error!=OMX_ErrorNone) {
96 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX failed %x", error);
104 OMX_ERRORTYPE VideoOMX::EventHandler_OMX(OMX_IN OMX_HANDLETYPE handle,OMX_IN OMX_PTR appdata,
105 OMX_IN OMX_EVENTTYPE event_type,OMX_IN OMX_U32 data1,
106 OMX_IN OMX_U32 data2,OMX_IN OMX_PTR event_data) {
108 Log::getInstance()->log("Video", Log::NOTICE, "eventHandler %x %x %x %x %x",handle,event_type,data1,data2,event_data);
110 struct VPE_OMX_EVENT new_event;
111 new_event.handle=handle;
112 new_event.appdata=appdata;
113 new_event.event_type=event_type;
114 new_event.data1=data1;
115 new_event.data2=data2;
116 new_event.event_data=event_data;
118 VideoOMX *video=(VideoOMX *)getInstance();
119 video->AddOmxEvent(new_event);
121 /* switch (event_type) {
122 case OMX_EventCmdComplete: {
127 return OMX_ErrorNone;
131 void VideoOMX::AddOmxEvent(VPE_OMX_EVENT new_event)
133 omx_event_mutex.Lock();
134 omx_events.push_back(new_event);
135 omx_event_mutex.Unlock();
139 OMX_ERRORTYPE VideoOMX::EmptyBufferDone_OMX(OMX_IN OMX_HANDLETYPE hcomp,OMX_IN OMX_PTR appdata,OMX_IN OMX_BUFFERHEADERTYPE* buffer){
141 //Log::getInstance()->log("Video", Log::NOTICE, "EmptyBufferDone");
142 VideoOMX *video=(VideoOMX *)getInstance();
143 video->ReturnEmptyOMXBuffer(buffer);
144 return OMX_ErrorNone;
148 void VideoOMX::ReturnEmptyOMXBuffer(OMX_BUFFERHEADERTYPE* buffer){
149 input_bufs_omx_mutex.Lock();
150 //Log::getInstance()->log("Video", Log::NOTICE, "ReturnEmptyOMXBuffer %d %d %d",input_bufs_omx_free.size(),input_bufs_omx_present.size(),input_bufs_omx_all.size());
151 input_bufs_omx_free.push_back(buffer);
152 //Log::getInstance()->log("Video", Log::NOTICE, "ReturnEmptyOMXBuffer %d",input_bufs_omx_free.size());
153 input_bufs_omx_mutex.Unlock();
156 OMX_ERRORTYPE VideoOMX::FillBufferDone_OMX(OMX_IN OMX_HANDLETYPE hcomp, OMX_IN OMX_PTR appdata,OMX_IN OMX_BUFFERHEADERTYPE* buffer) {
157 Log::getInstance()->log("Video", Log::NOTICE, "FillBufferDone");
158 return OMX_ErrorNone;
163 int VideoOMX::shutdown()
165 if (!initted) return 0;
168 DeAllocateCodecsOMX();
175 bool VideoOMX::loadOptionsfromServer(VDR* vdr)
177 Log::getInstance()->log("Video", Log::DEBUG, "VideoOMX config load");
178 char *name=vdr->configLoad("VideoOMX","SDDeinterlacing");
181 if (STRCASECMP(name, "None") == 0) {
183 }/* else if (STRCASECMP(name, "LineDouble") == 0) {
185 }*/ else if (STRCASECMP(name, "Advanced") == 0) {
187 } /*else if (STRCASECMP(name, "Crazy") == 0) {
188 deinterlace = 3; // this does not activate deinterlacing but a image filter, just for fun
190 Log::getInstance()->log("Video", Log::DEBUG, "Set deinterlacing to %s %d",name,deinterlace);
197 bool VideoOMX::handleOptionChanges(Option* option)
199 if (Video::handleOptionChanges(option))
201 switch (option->id) {
203 if (STRCASECMP(option->options[option->userSetChoice], "None") == 0) {
205 } /*else if (STRCASECMP(option->options[option->userSetChoice], "LineDouble")
208 }*/ else if (STRCASECMP(option->options[option->userSetChoice], "Advanced")
211 } /*else if (STRCASECMP(option->options[option->userSetChoice], "Crazy")
215 Log::getInstance()->log("Video", Log::DEBUG, "Set deinterlacing to %s %d",option->options[option->userSetChoice],deinterlace);
224 bool VideoOMX::saveOptionstoServer()
227 switch (deinterlace) {
229 VDR::getInstance()->configSave("VideoOMX","SDDeinterlacing", "None");
232 VDR::getInstance()->configSave("VideoOMX","SDDeinterlacing", "LineDouble");
235 VDR::getInstance()->configSave("VideoOMX","SDDeinterlacing", "Advanced");
238 VDR::getInstance()->configSave("VideoOMX","SDDeinterlacing", "Crazy");
245 /*Option(UINT id, const char* displayText, const char* configSection, const char* configKey, UINT optionType,
246 UINT numChoices, UINT defaultChoice, UINT startInt,
247 const char * const * options, const char * const * optionkeys = NULL, AbstractOption* handler=NULL);*/
249 bool VideoOMX::addOptionsToPanes(int panenumber,Options *options,WOptionPane* pane)
251 if (!Video::addOptionsToPanes(panenumber,options,pane)) return false;
257 static const char* deinterlaceopts[]={"None",/*"LineDouble",*/"Advanced"/*,"Crazy"*/};
258 option = new Option(1,tr("SD Deinterlacing"), "VideoOMX","SDDeinterlacing",Option::TYPE_TEXT,/*4,2*/2,1,0,deinterlaceopts,NULL,false,this);
259 options->push_back(option);
260 pane->addOptionLine(option);
268 int VideoOMX::setTVsize(UCHAR ttvsize)
272 // Override the aspect ratio usage, temporarily use to set the video chip mode
273 if (!setAspectRatio(tvsize)) { shutdown(); return 0; }
275 if ((fdVideo = open("/dev/vdec_dev", O_WRONLY)) < 0) return 0;
276 if (!setSource()) { shutdown(); return 0; }
277 if (!attachFramebuffer()) { shutdown(); return 0; }
279 // Reopening the fd causes the scart aspect line to go back to 4:3
280 // Set this again to the same as the tv screen size
281 if (!setAspectRatio(tvsize)) { shutdown(); return 0; }
283 // mode == LETTERBOX is invalid if the TV is widescreen
284 if (tvsize == ASPECT16X9) setMode(NORMAL);
289 int VideoOMX::setDefaultAspect()
291 return setAspectRatio(tvsize);
296 int VideoOMX::setFormat(UCHAR tformat)
298 if (!initted) return 0;
299 if ((tformat != PAL) && (tformat != NTSC)) return 0;
302 // if (ioctl(fdVideo, AV_SET_VID_DISP_FMT, format) != 0) return 0;
318 int VideoOMX::setConnection(UCHAR tconnection)
320 if (!initted) return 0;
321 if ((tconnection != COMPOSITERGB) && (tconnection != SVIDEO)) return 0;
322 connection = tconnection;
324 // if (ioctl(fdVideo, AV_SET_VID_OUTPUT, connection) != 0) return 0;
328 int VideoOMX::setAspectRatio(UCHAR taspectRatio)
330 if (!initted) return 0;
331 if ((taspectRatio != ASPECT4X3) && (taspectRatio != ASPECT16X9)) return 0;
332 aspectRatio = taspectRatio;
334 Log::getInstance()->log("Video", Log::DEBUG, "Setting aspect to %i", aspectRatio);
336 // if (ioctl(fdVideo, AV_SET_VID_RATIO, aspectRatio) != 0) return 0;
340 int VideoOMX::setMode(UCHAR tmode)
342 if (!initted) return 0;
349 void VideoOMX::updateMode()
354 OMX_CONFIG_DISPLAYREGIONTYPE dispconf;
355 memset(&dispconf, 0, sizeof(dispconf));
356 dispconf.nSize = sizeof(dispconf);
357 dispconf.nVersion.nVersion = OMX_VERSION;
358 dispconf.nPortIndex = omx_rend_input_port;
360 dispconf.set = OMX_DISPLAY_SET_LAYER;
361 error = OMX_SetParameter(omx_vid_rend, OMX_IndexConfigDisplayRegion,
363 if (error != OMX_ErrorNone) {
364 Log::getInstance()->log("Video", Log::DEBUG,
365 "Set OMX_IndexConfigDisplayRegion1 failed %x", error);
366 clock_mutex.Unlock();
369 dispconf.set = OMX_DISPLAY_SET_FULLSCREEN;
370 if (mode != QUARTER && mode != EIGHTH) {
372 dispconf.fullscreen = OMX_TRUE;
374 dispconf.fullscreen = OMX_FALSE;
376 error = OMX_SetParameter(omx_vid_rend, OMX_IndexConfigDisplayRegion,
378 if (error != OMX_ErrorNone) {
379 Log::getInstance()->log("Video", Log::DEBUG,
380 "Set OMX_IndexConfigDisplayRegion2 failed %x", error);
381 clock_mutex.Unlock();
385 dispconf.set = OMX_DISPLAY_SET_MODE;
386 if (mode != QUARTER && mode != EIGHTH) {
387 dispconf.mode = (mode == NORMAL) ? OMX_DISPLAY_MODE_FILL
388 : OMX_DISPLAY_MODE_LETTERBOX;
390 dispconf.mode = OMX_DISPLAY_MODE_LETTERBOX;
392 error = OMX_SetParameter(omx_vid_rend, OMX_IndexConfigDisplayRegion,
394 if (error != OMX_ErrorNone) {
395 Log::getInstance()->log("Video", Log::DEBUG,
396 "Set OMX_IndexConfigDisplayRegion3 failed %x", error);
397 clock_mutex.Unlock();
401 if (mode == QUARTER || mode == EIGHTH) {
402 unsigned int display_width, display_height;
403 display_width = display_height = 0;
404 if (graphics_get_display_size(0, &display_width, &display_height)
406 Log::getInstance()->log("OSD", Log::WARN,
407 "Getting display size failed! (BCM API) ");
408 clock_mutex.Unlock();
411 //UnSetFullscreen with window
412 dispconf.set = OMX_DISPLAY_SET_DEST_RECT;
413 dispconf.dest_rect.x_offset
414 = (int) (xpos * ((float) display_width));
415 dispconf.dest_rect.y_offset = (int) (ypos
416 * ((float) display_height));
417 if (mode == QUARTER) {
418 dispconf.dest_rect.width = display_width >> 1;
419 dispconf.dest_rect.height = display_height >> 1;
420 } else if (mode == EIGHTH) {
421 dispconf.dest_rect.width = display_width >> 2;
422 dispconf.dest_rect.height = display_height >> 2;
424 error = OMX_SetParameter(omx_vid_rend,
425 OMX_IndexConfigDisplayRegion, &dispconf);
426 if (error != OMX_ErrorNone) {
427 Log::getInstance()->log("Video", Log::DEBUG,
428 "Set OMX_IndexConfigDisplayRegion failed %x", error);
429 clock_mutex.Unlock();
435 clock_mutex.Unlock();
438 int VideoOMX::signalOff()
440 // if (ioctl(fdVideo, AV_SET_VID_DENC, 0) != 0) return 0;
444 int VideoOMX::signalOn()
446 // if (ioctl(fdVideo, AV_SET_VID_DENC, 1) != 0) return 0;
450 int VideoOMX::setSource()
452 if (!initted) return 0;
454 // What does this do...
455 // if (ioctl(fdVideo, AV_SET_VID_SRC, 1) != 0) return 0;
459 int VideoOMX::setPosition(int x, int y) {
462 xpos = ((float) x*2.f) / ((float) screenWidth);
463 ypos = ((float) y*2.f) / ((float) screenHeight);
471 if (!initted) return 0;
473 // if (ioctl(fdVideo, AV_SET_VID_SYNC, 2) != 0) return 0;
478 int VideoOMX::play() {
482 Log::getInstance()->log("Video", Log::DEBUG, "enter play");
484 if (AllocateCodecsOMX()) {
486 // Otherwise fall back to libav
490 Log::getInstance()->log("Video", Log::NOTICE,
491 "Allocate Codecs OMX failed assume h264 unsupported");
494 Log::getInstance()->log("Video", Log::NOTICE,
495 "Allocate Codecs OMX failed assume mpeg2 unsupported");
504 int VideoOMX::initClock()
508 if (clock_references==0)
511 static OMX_CALLBACKTYPE callbacks= {&EventHandler_OMX,&EmptyBufferDone_OMX,&FillBufferDone_OMX};
514 error=OMX_GetHandle(&omx_clock,VPE_OMX_CLOCK,NULL,&callbacks);
516 if (error!=OMX_ErrorNone) {
517 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX clock failed %x", error);
518 clock_mutex.Unlock();
519 DeAllocateCodecsOMX();
523 /* TODO Clock config to separate method */
524 OMX_PORT_PARAM_TYPE p_param;
525 memset(&p_param,0,sizeof(p_param));
526 p_param.nSize=sizeof(p_param);
527 p_param.nVersion.nVersion=OMX_VERSION;
528 error=OMX_GetParameter(omx_clock,OMX_IndexParamOtherInit,&p_param);
529 if (error!=OMX_ErrorNone) {
530 Log::getInstance()->log("Video", Log::DEBUG, "Init clock OMX_GetParameter failed %x", error);
531 clock_mutex.Unlock();
532 DeAllocateCodecsOMX();
535 omx_clock_output_port=p_param.nStartPortNumber;
537 for (unsigned int i=0;i<p_param.nPorts;i++) {
538 if (!DisablePort(omx_clock,p_param.nStartPortNumber+i,true) ) {
539 Log::getInstance()->log("Video", Log::DEBUG, "Disable Ports OMX clock failed %d",i);
540 clock_mutex.Unlock();
541 DeAllocateCodecsOMX();
550 Log::getInstance()->log("Video", Log::DEBUG, "init omx clock %x %x",this,omx_clock);
552 clock_mutex.Unlock();
556 int VideoOMX::getClockAudioandInit(OMX_HANDLETYPE *p_omx_clock,OMX_U32 *p_omx_clock_output_port)
560 *p_omx_clock_output_port=0;
567 OMX_TIME_CONFIG_ACTIVEREFCLOCKTYPE refclock;
568 memset(&refclock,0,sizeof(refclock));
569 refclock.nSize=sizeof(refclock);
570 refclock.nVersion.nVersion=OMX_VERSION;
572 refclock.eClock=OMX_TIME_RefClockAudio;
574 //refclock.eClock=OMX_TIME_RefClockVideo;
575 error=OMX_SetConfig(omx_clock,OMX_IndexConfigTimeActiveRefClock,&refclock);
576 if (error!=OMX_ErrorNone){
577 Log::getInstance()->log("Video", Log::DEBUG, "Clock OMX_IndexConfigTimeActiveRefClock failed %x", error);
578 clock_mutex.Unlock();
579 DeAllocateCodecsOMX();
583 OMX_PORT_PARAM_TYPE p_param;
584 memset(&p_param,0,sizeof(p_param));
585 p_param.nSize=sizeof(p_param);
586 p_param.nVersion.nVersion=OMX_VERSION;
587 error=OMX_GetParameter(omx_clock,OMX_IndexParamOtherInit,&p_param);
588 if (error!=OMX_ErrorNone){
589 Log::getInstance()->log("Video", Log::DEBUG, "Init clock OMX_GetParameter failed %x", error);
590 clock_mutex.Unlock();
591 DeAllocateCodecsOMX();
595 OMX_TIME_CONFIG_CLOCKSTATETYPE clock_conf;
596 memset(&clock_conf,0,sizeof(clock_conf));
597 clock_conf.nSize=sizeof(clock_conf);
598 clock_conf.nVersion.nVersion=OMX_VERSION;
599 clock_conf.eState=OMX_TIME_ClockStateWaitingForStartTime;
600 clock_conf.nStartTime=0;
601 clock_conf.nOffset=0;
602 if (clock_references==1) clock_conf.nWaitMask=OMX_CLOCKPORT1;
603 else clock_conf.nWaitMask=OMX_CLOCKPORT0|OMX_CLOCKPORT1;
604 error=OMX_SetConfig(omx_clock,OMX_IndexConfigTimeClockState,&clock_conf);
605 if (error!=OMX_ErrorNone) {
606 Log::getInstance()->log("Video", Log::DEBUG, "AuI Clock IndexConfigTimeClockState failed %x", error);
610 *p_omx_clock_output_port=p_param.nStartPortNumber+1;
611 *p_omx_clock=omx_clock;
612 clock_mutex.Unlock();
616 int VideoOMX::getClockVideoandInit()
625 if (clock_references==1) { // only if no audio is attached to this clock!
626 OMX_TIME_CONFIG_ACTIVEREFCLOCKTYPE refclock;
627 memset(&refclock,0,sizeof(refclock));
628 refclock.nSize=sizeof(refclock);
629 refclock.nVersion.nVersion=OMX_VERSION;
631 //refclock.eClock=OMX_TIME_RefClockAudio;
633 refclock.eClock=OMX_TIME_RefClockVideo;
634 error=OMX_SetConfig(omx_clock,OMX_IndexConfigTimeActiveRefClock,&refclock);
635 if (error!=OMX_ErrorNone) {
636 Log::getInstance()->log("Video", Log::DEBUG, "Clock OMX_IndexConfigTimeActiveRefClock failed %x", error);
637 clock_mutex.Unlock();
638 DeAllocateCodecsOMX();
643 OMX_PORT_PARAM_TYPE p_param;
644 memset(&p_param,0,sizeof(p_param));
645 p_param.nSize=sizeof(p_param);
646 p_param.nVersion.nVersion=OMX_VERSION;
647 error=OMX_GetParameter(omx_clock,OMX_IndexParamOtherInit,&p_param);
648 if (error!=OMX_ErrorNone){
649 Log::getInstance()->log("Video", Log::DEBUG, "Init clock OMX_GetParameter failed %x", error);
650 clock_mutex.Unlock();
651 DeAllocateCodecsOMX();
656 OMX_TIME_CONFIG_CLOCKSTATETYPE clock_conf;
657 memset(&clock_conf,0,sizeof(clock_conf));
658 clock_conf.nSize=sizeof(clock_conf);
659 clock_conf.nVersion.nVersion=OMX_VERSION;
660 clock_conf.eState=OMX_TIME_ClockStateWaitingForStartTime;
661 clock_conf.nStartTime=0;
662 clock_conf.nOffset=0;
663 if (clock_references==1) clock_conf.nWaitMask=OMX_CLOCKPORT0;
664 else clock_conf.nWaitMask=OMX_CLOCKPORT0|OMX_CLOCKPORT1;
665 error=OMX_SetConfig(omx_clock,OMX_IndexConfigTimeClockState,&clock_conf);
666 if (error!=OMX_ErrorNone) {
667 Log::getInstance()->log("Video", Log::DEBUG, "VuI Clock IndexConfigTimeClockState failed %x", error);
670 omx_clock_output_port=p_param.nStartPortNumber;
671 clock_mutex.Unlock();
676 void VideoOMX::clockUnpause()
680 if (clock_references>0) {
681 OMX_TIME_CONFIG_CLOCKSTATETYPE clock_conf;
682 memset(&clock_conf,0,sizeof(clock_conf));
683 clock_conf.nSize=sizeof(clock_conf);
684 clock_conf.nVersion.nVersion=OMX_VERSION;
685 clock_conf.eState=OMX_TIME_ClockStateRunning;
686 clock_conf.nStartTime=0;
687 clock_conf.nOffset=0;
688 clock_conf.nWaitMask=OMX_CLOCKPORT1;
689 error=OMX_SetConfig(omx_clock,OMX_IndexConfigTimeClockState,&clock_conf);
690 if (error!=OMX_ErrorNone) {
691 Log::getInstance()->log("Video", Log::DEBUG, "ClockUnpause IndexConfigTimeClockState failed %x", error);
694 clock_mutex.Unlock();
698 void VideoOMX::clockPause()
702 if (clock_references>0) {
703 OMX_TIME_CONFIG_CLOCKSTATETYPE clock_conf;
704 memset(&clock_conf,0,sizeof(clock_conf));
705 clock_conf.nSize=sizeof(clock_conf);
706 clock_conf.nVersion.nVersion=OMX_VERSION;
707 clock_conf.eState=OMX_TIME_ClockStateStopped;
708 clock_conf.nStartTime=0;
709 clock_conf.nOffset=0;
710 clock_conf.nWaitMask=OMX_CLOCKPORT1;
711 error=OMX_SetConfig(omx_clock,OMX_IndexConfigTimeClockState,&clock_conf);
712 if (error!=OMX_ErrorNone) {
713 Log::getInstance()->log("Video", Log::DEBUG, "ClockUnpause IndexConfigTimeClockState failed %x", error);
716 clock_mutex.Unlock();
721 int VideoOMX::AllocateCodecsOMX()
724 static OMX_CALLBACKTYPE callbacks= {&EventHandler_OMX,&EmptyBufferDone_OMX,&FillBufferDone_OMX};
726 Demuxer* demux=Demuxer::getInstance();
730 Log::getInstance()->log("Video", Log::NOTICE, "Allocate Codecs OMX");
731 //Clock, move later to audio including events
733 if (deinterlace!=0 && demux->getHorizontalSize()<=720) { //only deinterlace SD material
735 deint_first_frame=true;
737 Log::getInstance()->log("Video", Log::NOTICE, "Deinterlacing activated %d",deinterlace);
742 if (!getClockVideoandInit()){
743 return 0;// get the clock and init it if necessary
748 Log::getInstance()->log("Video", Log::DEBUG, "idleClock failed");
756 error=OMX_GetHandle(&omx_vid_dec,VPE_OMX_H264_DECODER,NULL,&callbacks);
758 error=OMX_GetHandle(&omx_vid_dec,VPE_OMX_MPEG2_DECODER,NULL,&callbacks);
761 if (error!=OMX_ErrorNone){
762 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX video decoder failed %x", error);
763 clock_mutex.Unlock();
764 DeAllocateCodecsOMX();
769 Log::getInstance()->log("Video", Log::DEBUG, "Nmark3 ");
770 OMX_PORT_PARAM_TYPE p_param;
771 memset(&p_param,0,sizeof(p_param));
772 p_param.nSize=sizeof(p_param);
773 p_param.nVersion.nVersion=OMX_VERSION;
774 error=OMX_GetParameter(omx_vid_dec,OMX_IndexParamVideoInit,&p_param);
775 if (error!=OMX_ErrorNone){
776 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX h264 decoder OMX_GetParameter failed %x", error);
777 clock_mutex.Unlock();
778 DeAllocateCodecsOMX();
781 omx_codec_input_port=p_param.nStartPortNumber;
782 omx_codec_output_port=p_param.nStartPortNumber+1;
784 if (!DisablePort(omx_vid_dec,omx_codec_input_port) || !DisablePort(omx_vid_dec,omx_codec_output_port)) {
785 Log::getInstance()->log("Video", Log::DEBUG, "Disable Ports OMX video decoder failed");
786 clock_mutex.Unlock();
787 DeAllocateCodecsOMX();
791 Log::getInstance()->log("Video", Log::DEBUG, "Nmark4 ");
793 OMX_PARAM_BRCMVIDEODECODEERRORCONCEALMENTTYPE conceal;
794 memset(&conceal,0,sizeof(conceal));
795 conceal.nSize=sizeof(conceal);
796 conceal.nVersion.nVersion=OMX_VERSION;
797 conceal.bStartWithValidFrame=OMX_FALSE;
799 error=OMX_SetParameter(omx_vid_dec,OMX_IndexParamBrcmVideoDecodeErrorConcealment,&conceal);
800 if (error!=OMX_ErrorNone){
801 Log::getInstance()->log("Video", Log::DEBUG, "OMX_IndexParamBrcmVideoDecodeErrorConcealment failed %x", error);
802 clock_mutex.Unlock();
803 DeAllocateCodecsOMX();
808 error = OMX_GetHandle(&omx_vid_deint, VPE_OMX_VIDEO_DEINTERLACE, NULL,
810 if (error != OMX_ErrorNone) {
811 Log::getInstance()->log("Video", Log::DEBUG,
812 "Init OMX video deinterlacer failed %x", error);
813 clock_mutex.Unlock();
814 DeAllocateCodecsOMX();
818 error = OMX_GetParameter(omx_vid_deint, OMX_IndexParamImageInit,
820 if (error != OMX_ErrorNone) {
821 Log::getInstance()->log("Video", Log::DEBUG,
822 "Init OMX video deinterlacer OMX_GetParameter failed %x",
824 clock_mutex.Unlock();
825 DeAllocateCodecsOMX();
828 omx_deint_input_port = p_param.nStartPortNumber;
829 omx_deint_output_port = p_param.nStartPortNumber + 1;
831 if (!DisablePort(omx_vid_deint, omx_deint_input_port, true)
832 || !DisablePort(omx_vid_deint, omx_deint_output_port, true)) {
833 Log::getInstance()->log("Video", Log::DEBUG,
834 "Disable Ports OMX video deint failed");
835 clock_mutex.Unlock();
836 DeAllocateCodecsOMX();
843 error=OMX_GetHandle(&omx_vid_sched,VPE_OMX_VIDEO_SCHED,NULL,&callbacks);
844 if (error!=OMX_ErrorNone){
845 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX video scheduler failed %x", error);
846 clock_mutex.Unlock();
847 DeAllocateCodecsOMX();
853 error=OMX_GetParameter(omx_vid_sched,OMX_IndexParamVideoInit,&p_param);
854 if (error!=OMX_ErrorNone){
855 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX video scheduler OMX_GetParameter failed %x", error);
856 clock_mutex.Unlock();
857 DeAllocateCodecsOMX();
860 omx_shed_input_port=p_param.nStartPortNumber;
861 omx_shed_output_port=p_param.nStartPortNumber+1;
864 error=OMX_GetParameter(omx_vid_sched,OMX_IndexParamOtherInit,&p_param);
865 if (error!=OMX_ErrorNone){
866 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX video scheduler OMX_GetParameter failed %x", error);
867 clock_mutex.Unlock();
868 DeAllocateCodecsOMX();
871 omx_shed_clock_port=p_param.nStartPortNumber;
872 Log::getInstance()->log("Video", Log::DEBUG, "scheduler ports %d %d %d ",omx_shed_input_port,omx_shed_output_port,omx_shed_clock_port);
875 if (!DisablePort(omx_vid_sched,omx_shed_input_port,true) || !DisablePort(omx_vid_sched,omx_shed_output_port,true)
876 || !DisablePort(omx_vid_sched,omx_shed_clock_port,true)) {
877 Log::getInstance()->log("Video", Log::DEBUG, "Disable Ports OMX video shed failed");
878 clock_mutex.Unlock();
879 DeAllocateCodecsOMX();
884 error=OMX_GetHandle(&omx_vid_rend,VPE_OMX_VIDEO_REND,NULL,&callbacks);
885 if (error!=OMX_ErrorNone){
886 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX video rend failed %x", error);
887 clock_mutex.Unlock();
888 DeAllocateCodecsOMX();
892 error=OMX_GetParameter(omx_vid_rend,OMX_IndexParamVideoInit,&p_param);
893 if (error!=OMX_ErrorNone){
894 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX video rend OMX_GetParameter failed %x", error);
895 clock_mutex.Unlock();
896 DeAllocateCodecsOMX();
899 omx_rend_input_port=p_param.nStartPortNumber;
900 //omx_rend_output_port=p_param.nStartPortNumber+1;
903 if (!DisablePort(omx_vid_rend,omx_rend_input_port,true) /*|| !DisablePort(omx_vid_rend,omx_rend_output_port)*/
905 Log::getInstance()->log("Video", Log::DEBUG, "Disable Ports OMX video rend failed");
906 clock_mutex.Unlock();
907 DeAllocateCodecsOMX();
915 error=OMX_SetupTunnel(omx_clock,omx_clock_output_port,omx_vid_sched,omx_shed_clock_port);
916 if (error!=OMX_ErrorNone){
917 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel clock to sched failed %x %d %d", error,omx_clock_output_port,omx_shed_clock_port);
918 clock_mutex.Unlock();
919 DeAllocateCodecsOMX();
923 if (!EnablePort(omx_clock,omx_clock_output_port,false) || !EnablePort(omx_vid_sched,omx_shed_clock_port,false)
925 Log::getInstance()->log("Video", Log::DEBUG, "Enable Ports OMX clock shed failed");
926 clock_mutex.Unlock();
927 DeAllocateCodecsOMX();
932 Log::getInstance()->log("Video", Log::DEBUG, "mark2 ");
933 if (!ChangeComponentState(omx_vid_sched,OMX_StateIdle)) {
934 Log::getInstance()->log("Video", Log::DEBUG, "vid_sched idle ChangeComponentState");
935 clock_mutex.Unlock();
936 DeAllocateCodecsOMX();
942 Log::getInstance()->log("Video", Log::DEBUG, "mark1 ");
943 if ( !CommandFinished(omx_vid_sched,OMX_CommandPortEnable,omx_shed_clock_port)) {
944 clock_mutex.Unlock();
945 DeAllocateCodecsOMX();
952 Log::getInstance()->log("Video", Log::DEBUG, "mark1 special ");
953 if ( !CommandFinished(omx_clock,OMX_CommandPortEnable,omx_clock_output_port)) {
954 clock_mutex.Unlock();
955 DeAllocateCodecsOMX();
961 Log::getInstance()->log("Video", Log::DEBUG, "mark1b ");
964 /* error=OMX_SendCommand(omx_vid_dec,OMX_CommandStateSet,OMX_StateIdle,0);
965 if (error!=OMX_ErrorNone){
966 Log::getInstance()->log("Video", Log::DEBUG, "vid_dec Send Command to OMX State Idle %x", error);
970 OMX_VIDEO_PARAM_PORTFORMATTYPE ft_type;
971 memset(&ft_type,0,sizeof(ft_type));
972 ft_type.nSize=sizeof(ft_type);
973 ft_type.nVersion.nVersion=OMX_VERSION;
975 ft_type.nPortIndex=omx_codec_input_port;
977 ft_type.eCompressionFormat=OMX_VIDEO_CodingAVC;
979 ft_type.eCompressionFormat=OMX_VIDEO_CodingMPEG2;
984 ft_type.xFramerate=0;//25*(1<<16);//demux->getFrameRate()*(1<<16);
985 Log::getInstance()->log("Video", Log::DEBUG, "Framerate: %d",demux->getFrameRate());
986 error=OMX_SetParameter(omx_vid_dec,OMX_IndexParamVideoPortFormat,&ft_type);
987 if (error!=OMX_ErrorNone){
988 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX_IndexParamVideoPortFormat failed %x", error);
989 clock_mutex.Unlock();
990 DeAllocateCodecsOMX();
995 if (!ChangeComponentState(omx_vid_dec,OMX_StateIdle)) {
996 Log::getInstance()->log("Video", Log::DEBUG, "vid_dec ChangeComponentState");
997 clock_mutex.Unlock();
998 DeAllocateCodecsOMX();
1003 if (!PrepareInputBufsOMX()) {
1004 clock_mutex.Unlock();
1005 DeAllocateCodecsOMX();
1010 error=OMX_SetupTunnel(omx_vid_dec,omx_codec_output_port,omx_vid_sched,omx_shed_input_port);
1011 if (error!=OMX_ErrorNone){
1012 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel dec to sched failed %x", error);
1013 clock_mutex.Unlock();
1014 DeAllocateCodecsOMX();
1020 if (!EnablePort(omx_vid_dec,omx_codec_output_port,false) || !EnablePort(omx_vid_sched,omx_shed_input_port,false)
1022 Log::getInstance()->log("Video", Log::DEBUG, "Enable Ports OMX codec shed failed");
1023 clock_mutex.Unlock();
1024 DeAllocateCodecsOMX();
1028 if ( !CommandFinished(omx_vid_dec,OMX_CommandPortEnable,omx_codec_output_port) || !CommandFinished(omx_vid_sched,OMX_CommandPortEnable,omx_shed_input_port)) {
1029 clock_mutex.Unlock();
1030 DeAllocateCodecsOMX();
1036 error=OMX_SetupTunnel(omx_vid_dec,omx_codec_output_port,omx_vid_deint,omx_deint_input_port);
1037 if (error!=OMX_ErrorNone){
1038 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel dec to deint failed %x", error);
1039 clock_mutex.Unlock();
1040 DeAllocateCodecsOMX();
1046 if (!EnablePort(omx_vid_dec,omx_codec_output_port,false) || !EnablePort(omx_vid_deint,omx_deint_input_port,false)
1048 Log::getInstance()->log("Video", Log::DEBUG, "Enable Ports OMX codec deint failed");
1049 clock_mutex.Unlock();
1050 DeAllocateCodecsOMX();
1054 if ( !CommandFinished(omx_vid_dec,OMX_CommandPortEnable,omx_codec_output_port) || !CommandFinished(omx_vid_deint,OMX_CommandPortEnable,omx_deint_input_port)) {
1055 clock_mutex.Unlock();
1056 DeAllocateCodecsOMX();
1060 if (!ChangeComponentState(omx_vid_deint,OMX_StateIdle)) {
1061 Log::getInstance()->log("Video", Log::DEBUG, "vid_deint ChangeComponentState");
1062 clock_mutex.Unlock();
1063 DeAllocateCodecsOMX();
1067 OMX_CONFIG_IMAGEFILTERPARAMSTYPE imagefilter;
1068 memset(&imagefilter,0,sizeof(imagefilter));
1069 imagefilter.nSize=sizeof(imagefilter);
1070 imagefilter.nVersion.nVersion=OMX_VERSION;
1072 imagefilter.nPortIndex=omx_deint_output_port;
1073 imagefilter.nNumParams=1;
1074 imagefilter.nParams[0]=3;//???
1075 switch (deinterlace) {
1077 imagefilter.eImageFilter=OMX_ImageFilterDeInterlaceLineDouble; break;
1079 imagefilter.eImageFilter=OMX_ImageFilterDeInterlaceAdvanced; break;
1081 imagefilter.eImageFilter=OMX_ImageFilterFilm; break;
1085 error=OMX_SetConfig(omx_vid_deint,OMX_IndexConfigCommonImageFilterParameters,&imagefilter);
1086 if (error!=OMX_ErrorNone){
1087 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX_IndexConfigCommonImageFilterParameters failed %x", error);
1088 clock_mutex.Unlock();
1089 DeAllocateCodecsOMX();
1094 error=OMX_SetupTunnel(omx_vid_deint,omx_deint_output_port,omx_vid_sched,omx_shed_input_port);
1095 if (error!=OMX_ErrorNone){
1096 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel deint to sched failed %x", error);
1097 clock_mutex.Unlock();
1098 DeAllocateCodecsOMX();
1102 if (!EnablePort(omx_vid_deint,omx_deint_output_port,false) || !EnablePort(omx_vid_sched,omx_shed_input_port,false)
1104 Log::getInstance()->log("Video", Log::DEBUG, "Enable Ports OMX deint shed failed");
1105 clock_mutex.Unlock();
1106 DeAllocateCodecsOMX();
1110 if ( !CommandFinished(omx_vid_deint,OMX_CommandPortEnable,omx_deint_output_port) || !CommandFinished(omx_vid_sched,OMX_CommandPortEnable,omx_shed_input_port)) {
1111 clock_mutex.Unlock();
1112 DeAllocateCodecsOMX();
1118 if (!ChangeComponentState(omx_vid_dec,OMX_StateExecuting)) {
1119 Log::getInstance()->log("Video", Log::DEBUG, "omx_vid_dec ChangeComponentState Execute");
1120 clock_mutex.Unlock();
1121 DeAllocateCodecsOMX();
1125 error=OMX_SetupTunnel(omx_vid_sched,omx_shed_output_port,omx_vid_rend,omx_rend_input_port);
1126 if (error!=OMX_ErrorNone){
1127 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel sched to rend failed %x", error);
1128 clock_mutex.Unlock();
1129 DeAllocateCodecsOMX();
1133 if (!EnablePort(omx_vid_sched,omx_shed_output_port,false) || !EnablePort(omx_vid_rend,omx_rend_input_port,false)
1135 Log::getInstance()->log("Video", Log::DEBUG, "Enable Ports OMX shed rend failed");
1136 clock_mutex.Unlock();
1137 DeAllocateCodecsOMX();
1141 if (!CommandFinished(omx_vid_sched,OMX_CommandPortEnable,omx_shed_output_port)
1142 || !CommandFinished(omx_vid_rend,OMX_CommandPortEnable,omx_rend_input_port)) {
1143 clock_mutex.Unlock();
1144 DeAllocateCodecsOMX();
1148 if (!ChangeComponentState(omx_vid_rend,OMX_StateIdle)) {
1149 Log::getInstance()->log("Video", Log::DEBUG, "vid_rend ChangeComponentState");
1150 clock_mutex.Unlock();
1151 DeAllocateCodecsOMX();
1156 if (!ChangeComponentState(omx_vid_deint,OMX_StateExecuting)) {
1157 Log::getInstance()->log("Video", Log::DEBUG, "vid_vid_deint ChangeComponentState");
1158 clock_mutex.Unlock();
1159 DeAllocateCodecsOMX();
1162 DisablePort(omx_vid_deint,omx_deint_output_port,false);
1163 DisablePort(omx_vid_deint,omx_deint_input_port,false);
1166 if (!ChangeComponentState(omx_vid_sched,OMX_StateExecuting)) {
1167 Log::getInstance()->log("Video", Log::DEBUG, "omx_vid_sched ChangeComponentState Execute");
1168 clock_mutex.Unlock();
1169 DeAllocateCodecsOMX();
1173 if (!ChangeComponentState(omx_vid_rend,OMX_StateExecuting)) {
1174 Log::getInstance()->log("Video", Log::DEBUG, "omx_vid_rend ChangeComponentState Execute");
1175 clock_mutex.Unlock();
1176 DeAllocateCodecsOMX();
1181 /*OMX_CONFIG_DISPLAYREGIONTYPE dispconf;
1182 memset(&dispconf,0,sizeof(dispconf));
1183 dispconf.nSize=sizeof(dispconf);
1184 dispconf.nVersion.nVersion=OMX_VERSION;
1186 dispconf.nPortIndex=omx_rend_input_port;
1188 dispconf.set=OMX_DISPLAY_SET_LAYER ;
1190 error=OMX_SetConfig(omx_vid_rend,OMX_IndexConfigDisplayRegion,&dispconf);
1191 if (error!=OMX_ErrorNone){
1192 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX_IndexConfigDisplayRegion failed %x", error);
1193 clock_mutex.Unlock();
1194 DeAllocateCodecsOMX();
1198 /* dispconf.set=OMX_DISPLAY_SET_FULLSCREEN ;
1199 dispconf.fullscreen=OMX_FALSE;
1200 error=OMX_SetParameter(omx_vid_rend,OMX_IndexConfigDisplayRegion,&dispconf);
1201 if (error!=OMX_ErrorNone){
1202 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX_IndexConfigDisplayRegion failed %x", error);
1203 clock_mutex.Unlock();
1204 DeAllocateCodecsOMX();
1208 dispconf.set=OMX_DISPLAY_SET_DEST_RECT;
1209 dispconf.dest_rect.x_offset=100;
1210 dispconf.dest_rect.y_offset=100;
1211 dispconf.dest_rect.width=640;
1212 dispconf.dest_rect.height=480;
1213 error=OMX_SetParameter(omx_vid_rend,OMX_IndexConfigDisplayRegion,&dispconf);
1214 if (error!=OMX_ErrorNone){
1215 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX_IndexConfigDisplayRegion failed %x", error);
1216 clock_mutex.Unlock();
1217 DeAllocateCodecsOMX();
1222 playbacktimeoffset=-GetCurrentSystemTime();
1226 clock_mutex.Unlock();
1230 setClockExecutingandRunning();
1239 int VideoOMX::idleClock()
1241 OMX_ERRORTYPE error;
1242 OMX_STATETYPE temp_state;
1244 OMX_GetState(omx_clock,&temp_state);
1246 if (temp_state!=OMX_StateIdle) {
1247 if (!ChangeComponentState(omx_clock,OMX_StateIdle)) {
1248 Log::getInstance()->log("Video", Log::DEBUG, "omx_clock ChangeComponentState Idle failed");
1249 clock_mutex.Unlock();
1253 clock_mutex.Unlock();
1257 int VideoOMX::setClockExecutingandRunning()
1259 OMX_ERRORTYPE error;
1260 OMX_STATETYPE temp_state;
1262 OMX_GetState(omx_clock,&temp_state);
1264 if (temp_state!=OMX_StateExecuting) {
1265 if (!ChangeComponentState(omx_clock,OMX_StateExecuting)) {
1266 Log::getInstance()->log("Video", Log::DEBUG, "omx_clock ChangeComponentState Execute failed");
1267 clock_mutex.Unlock();
1268 DeAllocateCodecsOMX();
1273 OMX_TIME_CONFIG_CLOCKSTATETYPE clock_conf;
1274 memset(&clock_conf,0,sizeof(clock_conf));
1275 clock_conf.nSize=sizeof(clock_conf);
1276 clock_conf.nVersion.nVersion=OMX_VERSION;
1277 clock_conf.eState=OMX_TIME_ClockStateRunning;
1278 error=OMX_SetConfig(omx_clock,OMX_IndexConfigTimeClockState,&clock_conf);
1279 if (error!=OMX_ErrorNone) {
1280 Log::getInstance()->log("Video", Log::DEBUG, "Clock IndexConfigTimeClockState failed %x", error);
1281 clock_mutex.Unlock();
1282 DeAllocateCodecsOMX();
1285 clock_mutex.Unlock();
1291 int VideoOMX::ChangeComponentState(OMX_HANDLETYPE handle,OMX_STATETYPE type) //needs to be called with locked mutex
1293 OMX_ERRORTYPE error;
1294 error=OMX_SendCommand(handle,OMX_CommandStateSet,type,0);
1295 if (error!=OMX_ErrorNone){
1296 Log::getInstance()->log("Video", Log::DEBUG, "handle %x Send Command to OMX State %x %x",handle,type, error);
1300 if (!CommandFinished(handle,OMX_CommandStateSet,type)) {
1308 int VideoOMX::EnablePort(OMX_HANDLETYPE handle,OMX_U32 port,bool wait) //needs to be called with locked mutex
1310 OMX_ERRORTYPE error;
1311 error=OMX_SendCommand(handle,OMX_CommandPortEnable,port,0);
1312 if (error!=OMX_ErrorNone){
1313 Log::getInstance()->log("Video", Log::DEBUG, "handle %x Send Command to enable port %x %x",handle,port, error);
1317 if (!wait) return 1;
1318 if (!CommandFinished(handle,OMX_CommandPortEnable,port)) {
1326 int VideoOMX::DisablePort(OMX_HANDLETYPE handle,OMX_U32 port,bool wait) //needs to be called with locked mutex
1328 OMX_ERRORTYPE error;
1329 error=OMX_SendCommand(handle,OMX_CommandPortDisable,port,0);
1330 if (error!=OMX_ErrorNone){
1331 Log::getInstance()->log("Video", Log::DEBUG, "handle %x Send Command to disable port %x %x",handle,port, error);
1335 if (!wait) return 1;
1336 if (!CommandFinished(handle,OMX_CommandPortDisable,port)) {
1344 int VideoOMX::WaitForEvent(OMX_HANDLETYPE handle,OMX_U32 event) //needs to be called with locked mutex
1348 omx_event_mutex.Lock();
1349 list<VPE_OMX_EVENT>::iterator itty=omx_events.begin();
1350 while (itty!=omx_events.end()) {
1352 VPE_OMX_EVENT current=*itty;
1353 if (current.handle==handle) { //this is ours
1354 if (current.event_type==OMX_EventError) {
1355 omx_events.erase(itty);
1356 omx_event_mutex.Unlock();
1357 Log::getInstance()->log("Video", Log::DEBUG, "WaitForEvent Finished on Error");
1360 } else if (current.event_type==event) {
1361 omx_events.erase(itty);
1362 omx_event_mutex.Unlock();
1363 Log::getInstance()->log("Video", Log::DEBUG, "WaitForEvent Finished Completed");
1370 omx_event_mutex.Unlock();
1375 Log::getInstance()->log("Video", Log::DEBUG, "WaitForEvent waited too long %x %x",handle,event);
1381 int VideoOMX::CommandFinished(OMX_HANDLETYPE handle,OMX_U32 command,OMX_U32 data2) //needs to be called with locked mutex
1385 omx_event_mutex.Lock();
1386 list<VPE_OMX_EVENT>::iterator itty=omx_events.begin();
1387 while (itty!=omx_events.end()) {
1389 VPE_OMX_EVENT current=*itty;
1390 if (current.handle==handle) { //this is ours
1391 if (current.event_type==OMX_EventError) {
1392 omx_events.erase(itty);
1393 omx_event_mutex.Unlock();
1394 Log::getInstance()->log("Video", Log::DEBUG, "Command Finished on Error");
1397 } else if (current.event_type==OMX_EventCmdComplete && current.data1==command && current.data2==data2) {
1398 omx_events.erase(itty);
1399 omx_event_mutex.Unlock();
1400 //Log::getInstance()->log("Video", Log::DEBUG, "Command Finished Completed");
1407 omx_event_mutex.Unlock();
1412 Log::getInstance()->log("Video", Log::DEBUG, "CommandFinished waited too long %x %x %x",handle,command, data2);
1421 int VideoOMX::PrepareInputBufsOMX() //needs to be called with locked mutex
1423 OMX_ERRORTYPE error;
1424 OMX_PARAM_PORTDEFINITIONTYPE port_def_type;
1425 memset(&port_def_type,0,sizeof(port_def_type));
1426 port_def_type.nSize=sizeof(port_def_type);
1427 port_def_type.nVersion.nVersion=OMX_VERSION;
1428 port_def_type.nPortIndex=omx_codec_input_port;
1430 error=OMX_GetParameter(omx_vid_dec,OMX_IndexParamPortDefinition, &port_def_type);
1432 if (error!=OMX_ErrorNone){
1433 Log::getInstance()->log("Video", Log::DEBUG, "Get OMX OMX_IndexParamPortDefinition failed %x", error);
1435 /* Log::getInstance()->log("Video", Log::DEBUG, "Port para %d %d %d %d %d %d %d", port_def_type.nBufferCountActual,
1436 port_def_type.nBufferCountMin,port_def_type.nBufferSize,port_def_type.bEnabled,port_def_type.bPopulated,
1437 port_def_type.bBuffersContiguous,port_def_type.nBufferAlignment);*/
1439 port_def_type.nBufferCountActual=100;
1440 port_def_type.nBufferSize=max(port_def_type.nBufferSize,200000); // for transcoder important
1442 error=OMX_SetParameter(omx_vid_dec,OMX_IndexParamPortDefinition, &port_def_type);
1444 if (error!=OMX_ErrorNone){
1445 Log::getInstance()->log("Video", Log::DEBUG, "Set OMX OMX_IndexParamPortDefinition failed %x", error);
1449 error=OMX_SendCommand(omx_vid_dec,OMX_CommandPortEnable,omx_codec_input_port,0);
1450 if (error!=OMX_ErrorNone){
1451 Log::getInstance()->log("Video", Log::DEBUG, "Prepare Input bufs Send Command to enable port %x", error);
1455 input_bufs_omx_mutex.Lock();
1456 for (unsigned int i=0; i< port_def_type.nBufferCountActual;i++) {
1458 // unsigned char* new_buffer_data=(unsigned char*)malloc(port_def_type.nbufferSize);
1459 OMX_BUFFERHEADERTYPE *buf_head=NULL;
1460 /* error=OMX_Usebuffer(omx_vid_dec,&buf_head,omx_codec_input_port,NULL,port_def_type.nbufferSize,new_buffer_data);
1461 if (error!=OMX_ErrorNone){
1462 Log::getInstance()->log("Video", Log::DEBUG, "Use OMX_Usebuffer failed %x", error);
1463 input_bufs_omx_mutex.Unlock();
1466 error=OMX_AllocateBuffer(omx_vid_dec,&buf_head,omx_codec_input_port,NULL,port_def_type.nBufferSize);
1467 if (error!=OMX_ErrorNone){
1468 Log::getInstance()->log("Video", Log::DEBUG, "Use OMX_AllocateBuffer failed %x", error);
1469 input_bufs_omx_mutex.Unlock();
1472 input_bufs_omx_all.push_back(buf_head);
1473 input_bufs_omx_free.push_back(buf_head);
1475 omx_first_frame=true;
1478 cur_input_buf_omx=NULL;
1479 input_bufs_omx_mutex.Unlock();
1482 Log::getInstance()->log("Video", Log::DEBUG, "PrepareInputBufsOMX mark3");
1483 if (!CommandFinished(omx_vid_dec,OMX_CommandPortEnable,omx_codec_input_port)) {
1486 Log::getInstance()->log("Video", Log::DEBUG, "PrepareInputBufsOMX mark4");
1491 int VideoOMX::DestroyInputBufsOMX() //need s to be called with locked mutex
1493 OMX_ERRORTYPE error;
1495 cur_input_buf_omx=NULL;
1496 input_bufs_omx_mutex.Lock();
1497 for (int i=0; i< input_bufs_omx_all.size();i++) {
1498 // free(input_bufs_omx_all[i]->pBuffer);
1499 // input_bufs_omx_all[i]->pBuffer=NULL;
1500 error=OMX_FreeBuffer(omx_vid_dec,omx_codec_input_port,input_bufs_omx_all[i]);
1501 if (error!=OMX_ErrorNone){
1502 Log::getInstance()->log("Video", Log::DEBUG, "Use OMX_FreeBuffer failed %x", error);
1503 input_bufs_omx_mutex.Unlock();
1508 input_bufs_omx_all.clear();
1509 input_bufs_omx_free.clear();
1510 input_bufs_omx_present.clear();
1511 input_time_present.clear();
1512 input_is_last.clear();
1513 input_bufs_omx_mutex.Unlock();
1520 int VideoOMX::DeAllocateCodecsOMX()
1522 OMX_ERRORTYPE error;
1524 Log::getInstance()->log("Video", Log::DEBUG, "enter deallocatecodecsomx");
1528 if (cur_input_buf_omx) {
1529 cur_input_buf_omx->nFlags|=OMX_BUFFERFLAG_EOS;
1530 OMX_ERRORTYPE error=ProtOMXEmptyThisBuffer(omx_vid_dec,cur_input_buf_omx);
1531 if (error!=OMX_ErrorNone) {
1532 Log::getInstance()->log("Video", Log::DEBUG, "OMX_EmptyThisBuffer failed %x", error);
1535 cur_input_buf_omx=NULL;//write out old data
1539 // first stop the omx elements
1540 if (!ChangeComponentState(omx_vid_dec,OMX_StateIdle)) {
1541 Log::getInstance()->log("Video", Log::DEBUG, "vid_dec ChangeComponentState");
1544 clock_mutex.Unlock();
1550 if (!ChangeComponentState(omx_vid_deint, OMX_StateIdle)) {
1551 Log::getInstance()->log("Video", Log::DEBUG,
1552 "vid_deint ChangeComponentState");
1558 if (!ChangeComponentState(omx_vid_sched,OMX_StateIdle)) {
1559 Log::getInstance()->log("Video", Log::DEBUG, "vid_shed ChangeComponentState");
1563 if (!ChangeComponentState(omx_vid_rend,OMX_StateIdle)) {
1564 Log::getInstance()->log("Video", Log::DEBUG, "vid_rend ChangeComponentState");
1570 // TODO proper deinit sequence
1571 // first flush all buffers
1575 error = OMX_SendCommand(omx_vid_dec, OMX_CommandFlush,
1576 omx_codec_output_port, NULL);
1577 if (error != OMX_ErrorNone) {
1578 Log::getInstance()->log("Video", Log::DEBUG,
1579 "OMX_Flush codec out failed %x", error);
1583 error = OMX_SendCommand(omx_vid_sched, OMX_CommandFlush,
1584 omx_shed_input_port, NULL);
1585 if (error != OMX_ErrorNone) {
1586 Log::getInstance()->log("Video", Log::DEBUG,
1587 "OMX_Flush shed in failed %x", error);
1591 if (!CommandFinished(omx_vid_dec, OMX_CommandFlush,
1592 omx_codec_output_port)) {
1593 Log::getInstance()->log("Video", Log::DEBUG,
1594 "flush cmd codec failed");
1597 if (!CommandFinished(omx_vid_sched, OMX_CommandFlush,
1598 omx_shed_input_port)) {
1599 Log::getInstance()->log("Video", Log::DEBUG,
1600 "flush cmd shed failed");
1603 error = OMX_SendCommand(omx_vid_dec, OMX_CommandFlush,
1604 omx_codec_output_port, NULL);
1605 if (error != OMX_ErrorNone) {
1606 Log::getInstance()->log("Video", Log::DEBUG,
1607 "OMX_Flush codec out failed %x", error);
1611 error = OMX_SendCommand(omx_vid_deint, OMX_CommandFlush,
1612 omx_deint_input_port, NULL);
1613 if (error != OMX_ErrorNone) {
1614 Log::getInstance()->log("Video", Log::DEBUG,
1615 "OMX_Flush deint in failed %x", error);
1619 if (!CommandFinished(omx_vid_dec, OMX_CommandFlush,
1620 omx_codec_output_port)) {
1621 Log::getInstance()->log("Video", Log::DEBUG,
1622 "flush cmd codec failed");
1625 if (!CommandFinished(omx_vid_deint, OMX_CommandFlush,
1626 omx_deint_input_port)) {
1627 Log::getInstance()->log("Video", Log::DEBUG,
1628 "flush cmd deint failed");
1632 error = OMX_SendCommand(omx_vid_deint, OMX_CommandFlush,
1633 omx_deint_output_port, NULL);
1634 if (error != OMX_ErrorNone) {
1635 Log::getInstance()->log("Video", Log::DEBUG,
1636 "OMX_Flush deint out failed %x", error);
1640 error = OMX_SendCommand(omx_vid_sched, OMX_CommandFlush,
1641 omx_shed_input_port, NULL);
1642 if (error != OMX_ErrorNone) {
1643 Log::getInstance()->log("Video", Log::DEBUG,
1644 "OMX_Flush shed in failed %x", error);
1648 if (!CommandFinished(omx_vid_deint, OMX_CommandFlush,
1649 omx_deint_output_port)) {
1650 Log::getInstance()->log("Video", Log::DEBUG,
1651 "flush cmd deint failed");
1654 if (!CommandFinished(omx_vid_sched, OMX_CommandFlush,
1655 omx_shed_input_port)) {
1656 Log::getInstance()->log("Video", Log::DEBUG,
1657 "flush cmd shed failed");
1667 error = OMX_SendCommand(omx_vid_rend, OMX_CommandFlush,
1668 omx_rend_input_port, NULL);
1669 if (error != OMX_ErrorNone) {
1670 Log::getInstance()->log("Video", Log::DEBUG,
1671 "OMX_Flush rend in failed %x", error);
1675 error = OMX_SendCommand(omx_vid_sched, OMX_CommandFlush,
1676 omx_shed_output_port, NULL);
1677 if (error != OMX_ErrorNone) {
1678 Log::getInstance()->log("Video", Log::DEBUG,
1679 "OMX_Flush shed out failed %x", error);
1685 if (!CommandFinished(omx_vid_rend, OMX_CommandFlush,
1686 omx_rend_input_port)) {
1687 Log::getInstance()->log("Video", Log::DEBUG,
1688 "flush cmd shed rend failed");
1691 if (!CommandFinished(omx_vid_sched, OMX_CommandFlush,
1692 omx_shed_output_port)) {
1693 Log::getInstance()->log("Video", Log::DEBUG,
1694 "flush cmd shed rend failed");
1701 error=OMX_SendCommand(omx_clock,OMX_CommandFlush, omx_clock_output_port, NULL);
1702 if (error!=OMX_ErrorNone){
1703 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Flush clock out failed %x", error);
1707 error=OMX_SendCommand(omx_vid_sched,OMX_CommandFlush, omx_shed_clock_port, NULL);
1708 if (error!=OMX_ErrorNone){
1709 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Flush shed clock failed %x", error);
1713 if (!CommandFinished(omx_clock,OMX_CommandFlush,omx_clock_output_port) ||
1714 !CommandFinished(omx_vid_sched,OMX_CommandFlush,omx_shed_clock_port)) {
1715 Log::getInstance()->log("Video", Log::DEBUG, "flush cmd clock shed failed");
1721 error = OMX_SendCommand(omx_vid_dec, OMX_CommandFlush,
1722 omx_codec_input_port, NULL);
1723 if (error != OMX_ErrorNone) {
1724 Log::getInstance()->log("Video", Log::DEBUG,
1725 "OMX_Flush codec out failed %x", error);
1732 DestroyInputBufsOMX();
1735 if (!DisablePort(omx_vid_sched,omx_shed_output_port,true)) {
1736 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 2 ");
1738 if (!DisablePort(omx_vid_rend,omx_rend_input_port,true)) {
1739 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 1");
1745 if (!DisablePort(omx_vid_dec,omx_codec_output_port,true)) {
1746 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 6");
1751 if (!DisablePort(omx_vid_dec,omx_codec_input_port,true)) {
1752 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 7");
1756 if (!DisablePort(omx_vid_deint,omx_deint_output_port,true)) {
1757 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 6a");
1762 if (!DisablePort(omx_vid_deint,omx_deint_input_port,true)) {
1763 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 7a");
1769 if (!DisablePort(omx_vid_sched,omx_shed_input_port,true)) {
1770 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 3");
1773 if (!DisablePort(omx_vid_sched,omx_shed_clock_port,true)) {
1774 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 4");
1777 if (!DisablePort(omx_clock,omx_clock_output_port,true)) {
1778 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 5");
1784 error=OMX_SetupTunnel(omx_vid_dec,omx_codec_output_port,NULL,NULL);
1785 if (error!=OMX_ErrorNone) {
1786 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1791 error=OMX_SetupTunnel(omx_vid_deint,omx_deint_input_port,NULL,NULL);
1792 if (error!=OMX_ErrorNone) {
1793 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1797 error=OMX_SetupTunnel(omx_vid_deint,omx_deint_output_port,NULL,NULL);
1798 if (error!=OMX_ErrorNone) {
1799 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1803 error=OMX_SetupTunnel(omx_vid_sched,omx_shed_input_port,NULL,NULL);
1804 if (error!=OMX_ErrorNone) {
1805 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1810 error=OMX_SetupTunnel(omx_vid_sched,omx_shed_output_port,NULL,NULL);
1811 if (error!=OMX_ErrorNone) {
1812 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1816 error=OMX_SetupTunnel(omx_vid_rend,omx_rend_input_port,NULL,NULL);
1817 if (error!=OMX_ErrorNone) {
1818 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1823 error=OMX_SetupTunnel(omx_clock,omx_clock_output_port,NULL,NULL);
1824 if (error!=OMX_ErrorNone) {
1825 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1829 error=OMX_SetupTunnel(omx_vid_sched,omx_shed_clock_port,NULL,NULL);
1830 if (error!=OMX_ErrorNone) {
1831 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1838 error=OMX_FreeHandle(omx_vid_dec);
1839 error=OMX_FreeHandle(omx_vid_sched);
1840 error=OMX_FreeHandle(omx_vid_rend);
1841 if (dodeint) error=OMX_FreeHandle(omx_vid_deint);
1843 clock_mutex.Unlock();
1845 if (error!=OMX_ErrorNone) {
1846 Log::getInstance()->log("Video", Log::DEBUG, "FreeHandle failed %d", error);
1848 } else clock_mutex.Unlock();
1849 Log::getInstance()->log("Video", Log::DEBUG, "leave deallocate codecs OMX");
1855 void VideoOMX::destroyClock()
1858 if (clock_references>0) {
1860 if (clock_references==0) {
1861 OMX_ERRORTYPE error;
1862 Log::getInstance()->log("Video", Log::DEBUG, "destroy omx clock");
1863 error=OMX_FreeHandle(omx_clock);
1864 if (error!=OMX_ErrorNone) {
1865 Log::getInstance()->log("Video", Log::DEBUG, "FreeHandle failed %d", error);
1870 clock_mutex.Unlock();
1874 int VideoOMX::stop()
1876 if (!initted) return 0;
1879 //Check if libav mode
1880 DeAllocateCodecsOMX();
1885 // if (ioctl(fdVideo, AV_SET_VID_STOP, 0) != 0) return 0;
1889 int VideoOMX::reset()
1891 if (!initted) return 0;
1894 DeAllocateCodecsOMX();
1895 // if (ioctl(fdVideo, AV_SET_VID_RESET, 0x11) != 0) return 0;
1899 int VideoOMX::pause()
1901 if (!initted) return 0;
1902 Log::getInstance()->log("Video", Log::DEBUG, "enter pause");
1905 //maybe also change omx clock?
1906 pausetimecode=GetCurrentSystemTime();
1912 int VideoOMX::unPause() // FIXME get rid - same as play!! Not here!
1914 if (!initted) return 0;
1915 Log::getInstance()->log("Video", Log::DEBUG, "enter unpause");
1918 playbacktimeoffset+=GetCurrentSystemTime()-pausetimecode;
1919 paused=false; // may be also change omx clock
1925 int VideoOMX::fastForward()
1927 if (!initted) return 0;
1929 // if (ioctl(fdVideo, AV_SET_VID_libavWD, 1) != 0) return 0;
1933 int VideoOMX::unFastForward()
1935 if (!initted) return 0;
1937 // if (ioctl(fdVideo, AV_SET_VID_RESET, 0x11) != 0) return 0; // don't need this.
1939 //// if (ioctl(fdVideo, AV_SET_VID_PLAY, 0) != 0) return 0;
1943 int VideoOMX::attachFrameBuffer()
1945 if (!initted) return 0;
1947 // if (ioctl(fdVideo, AV_SET_VID_FB, 0) != 0) return 0;
1951 int VideoOMX::blank(void)
1953 // if (ioctl(fdVideo, AV_SET_VID_FB, 1) != 0) return 0;
1954 // if (ioctl(fdVideo, AV_SET_VID_FB, 0) != 0) return 0;
1958 ULLONG VideoOMX::getCurrentTimestamp()
1960 if (iframemode) return 0;
1961 return lastreftimePTS;
1966 ULONG VideoOMX::timecodeToFrameNumber(ULLONG timecode)
1968 if (format == PAL) return (ULONG)(((double)timecode / (double)90000) * (double)25);
1969 else return (ULONG)(((double)timecode / (double)90000) * (double)30);
1974 int VideoOMX::test()
1979 // return ioctl(fdVideo, AV_SET_VID_STC, &stc);
1986 int VideoOMX::test2()
1994 long long VideoOMX::SetStartOffset(long long curreftime, bool *rsync)
1998 startoffset=curreftime;//offset is set for audio
2000 offsetvideonotset=false;
2002 if (offsetvideonotset) {
2003 offsetvideonotset=false;
2006 if ( (curreftime-lastrefvideotime)>10000000LL
2007 || (curreftime-lastrefvideotime)<-10000000LL) {//if pts jumps to big resync
2008 startoffset+=curreftime-lastrefvideotime;
2009 lastrefaudiotime+=curreftime-lastrefvideotime;
2011 offsetaudionotset=true;
2018 lastrefvideotime=curreftime;
2024 long long VideoOMX::SetStartAudioOffset(long long curreftime, bool *rsync)
2028 startoffset=curreftime;
2030 offsetaudionotset=false;
2032 if (offsetaudionotset) {
2033 offsetaudionotset=false;
2036 if ( (curreftime-lastrefaudiotime)>10000000LL
2037 || (curreftime-lastrefaudiotime)<-10000000LL) {//if pts jumps to big resync
2038 startoffset+=curreftime-lastrefaudiotime;
2039 lastrefvideotime+=curreftime-lastrefaudiotime;
2041 offsetvideonotset=true;
2047 lastrefaudiotime=curreftime;
2052 void VideoOMX::ResetTimeOffsets() {
2053 offsetnotset=true; //called from demuxer
2054 offsetvideonotset=true;
2055 offsetaudionotset=true;
2063 long long VideoOMX::GetCurrentSystemTime()
2066 clock_gettime(CLOCK_MONOTONIC, &ts);
2067 return ts.tv_sec*10000000LL+ts.tv_nsec/100LL;
2070 void VideoOMX::WaitUntil(long long time)
2072 struct timespec interval;
2073 interval.tv_sec=time/10000000LL;
2074 interval.tv_nsec=(time %10000000LL)*100LL;
2075 while (clock_nanosleep(CLOCK_MONOTONIC,TIMER_ABSTIME,&interval,NULL)==EINTR) {
2076 //Log::getInstance()->log("Video", Log::DEBUG, "Wait until multi");
2081 bool VideoOMX::FrameSkip(long long pts)
2083 //ok first calculate the absolute time
2085 long long target_time=pts-playbacktimeoffset;
2086 // we have to wait untile the next frame
2087 long long offset=Demuxer::getInstance()->getFrameRate();
2088 if (offset==0) offset=25;
2089 offset=-2*10000000LL/offset;
2090 target_time+=offset;
2091 long long current_time=GetCurrentSystemTime();
2093 if ((target_time-current_time)<-400000LL) {
2094 skip=true; // we are too slow
2096 /* Log::getInstance()->log("Video", Log::DEBUG,
2097 "Skipping frames1 %lld %lld %d",target_time-current_time,pts,Demuxer::getInstance()->getFrameRate());
2098 Log::getInstance()->log("Video", Log::DEBUG, "skip detail pts: %lld target: %lld sys: %lld off: %lld diff %lld",pts,target_time,current_time,offset,
2099 target_time-current_time);*/
2104 if ((target_time - current_time) < 0000LL) { //skip a bit more
2105 skip = true; // we are too slow
2107 /* Log::getInstance()->log("Video", Log::DEBUG,"Skipping frames2 %lld %lld %d",target_time-current_time,pts,Demuxer::getInstance()->getFrameRate());
2108 Log::getInstance()->log("Video", Log::DEBUG, "skip detail pts: %lld target: %lld sys: %lld off: %lld diff %lld",pts,target_time,current_time,offset,
2109 target_time-current_time);*/
2119 void VideoOMX::FrameWaitforDisplay(long long pts)
2121 //ok first calculate the absolute time
2122 long long target_time=pts-playbacktimeoffset;
2123 // we have to wait untile the next frame
2124 long long offset=Demuxer::getInstance()->getFrameRate();
2125 long long current_time=GetCurrentSystemTime();
2126 if (offset==0) offset=25;
2127 offset=-2*10000000LL/offset;
2128 target_time+=offset;
2129 if ((target_time-current_time)>1000000LL) target_time=current_time+1000000LL; // something is wrong do not wait too long
2130 //Log::getInstance()->log("Video", Log::DEBUG, "Wait for display pts: %lld target: %lld sys: %lld off: %lld diff %lld",pts,target_time,current_time,offset,
2131 // target_time-current_time);
2133 WaitUntil(target_time);
2134 //Log::getInstance()->log("Video", Log::DEBUG, "Wait for display out %lld",GetCurrentSystemTime());
2137 void VideoOMX::AdjustAudioPTS(long long pts)
2139 long long newplaybacktimeoffset=pts-GetCurrentSystemTime();
2140 /* if ((newplaybacktimeoffset-1000000LL)>playbacktimeoffset
2141 || (newplaybacktimeoffset+1000000LL)<playbacktimeoffset) {
2142 Log::getInstance()->log("Video", Log::DEBUG, "Adjust Playbackoffsettime o: %lld n: %lld",
2143 playbacktimeoffset,newplaybacktimeoffset);*/
2144 playbacktimeoffset=newplaybacktimeoffset;
2149 void VideoOMX::threadPostStopCleanup()
2153 Log::getInstance()->log("Video", Log::DEBUG,
2158 void VideoOMX::threadMethod()
2160 Log::getInstance()->log("Video", Log::DEBUG,
2164 OMX_BUFFERHEADERTYPE* pict=NULL;
2168 input_bufs_omx_mutex.Lock();
2169 if (input_bufs_omx_present.size()>0) {
2171 pict=input_bufs_omx_present.front();
2172 time=input_time_present.front();
2173 islast=input_is_last.front();
2174 input_bufs_omx_present.pop_front();
2175 input_time_present.pop_front();
2176 input_is_last.pop_front();
2178 input_bufs_omx_mutex.Unlock();
2182 //Log::getInstance()->log("Video", Log::DEBUG,
2184 if (time!=0 && FrameSkip(time) && !(pict->nFlags &OMX_BUFFERFLAG_STARTTIME)) {
2186 input_bufs_omx_mutex.Lock();
2187 input_bufs_omx_free.push_back(pict);
2188 input_bufs_omx_mutex.Unlock();
2191 OMX_ERRORTYPE error = ProtOMXEmptyThisBuffer(omx_vid_dec, pict);
2192 if (error != OMX_ErrorNone) {
2193 Log::getInstance()->log("Video", Log::DEBUG,
2194 "OMX_EmptyThisBuffer failed %x", error);
2196 if (deint_first_frame && dodeint) DeinterlaceFix();
2197 if (islast) FrameWaitforDisplay(time);
2205 Log::getInstance()->log("Video", Log::DEBUG,
2209 void VideoOMX::DeinterlaceFix()
2212 Demuxer* demux=Demuxer::getInstance();
2214 OMX_ERRORTYPE error;
2215 OMX_PARAM_PORTDEFINITIONTYPE port_def_type;
2216 memset(&port_def_type,0,sizeof(port_def_type));
2217 port_def_type.nSize=sizeof(port_def_type);
2218 port_def_type.nVersion.nVersion=OMX_VERSION;
2219 port_def_type.nPortIndex=omx_codec_output_port;
2221 error=OMX_GetParameter(omx_vid_dec,OMX_IndexParamPortDefinition, &port_def_type);
2222 if (error != OMX_ErrorNone) {
2223 Log::getInstance()->log("Video", Log::DEBUG,
2224 "OMX_IndexParamPortDefinition fix failed %x", error);
2225 clock_mutex.Unlock();
2229 if (port_def_type.format.video.nFrameWidth == demux->getHorizontalSize()
2230 && port_def_type.format.video.nFrameHeight == demux->getVerticalSize()){
2231 Log::getInstance()->log("Video", Log::DEBUG,
2232 "Deinit first frame fix");
2233 deint_first_frame=false;
2235 WaitForEvent(omx_vid_dec,OMX_EventPortSettingsChanged);
2236 DisablePort(omx_vid_dec,omx_codec_output_port,false);
2237 DisablePort(omx_vid_sched,omx_shed_input_port,false);
2238 DisablePort(omx_vid_deint,omx_deint_output_port,false);
2239 DisablePort(omx_vid_deint,omx_deint_input_port,false);
2241 port_def_type.nPortIndex=omx_deint_input_port;
2242 error = OMX_SetParameter(omx_vid_deint, OMX_IndexParamPortDefinition,
2244 if (error != OMX_ErrorNone) {
2245 Log::getInstance()->log("Video", Log::DEBUG,
2246 "Set OMX_IndexParamPortDefinition1 failed %x", error);
2247 clock_mutex.Unlock();
2251 port_def_type.nPortIndex=omx_deint_output_port;
2252 error = OMX_SetParameter(omx_vid_deint, OMX_IndexParamPortDefinition,
2254 if (error != OMX_ErrorNone) {
2255 Log::getInstance()->log("Video", Log::DEBUG,
2256 "Set OMX_IndexParamPortDefinition2 failed %x", error);
2257 clock_mutex.Unlock();
2262 EnablePort(omx_vid_dec,omx_codec_output_port,false);
2263 EnablePort(omx_vid_deint,omx_deint_input_port,false);
2264 EnablePort(omx_vid_deint,omx_deint_output_port,false);
2265 EnablePort(omx_vid_sched,omx_shed_input_port,false);
2267 clock_mutex.Unlock();
2273 void VideoOMX::PutBufferToPres(OMX_BUFFERHEADERTYPE* buffer, long long time,bool islast)
2275 input_bufs_omx_mutex.Lock();
2276 input_bufs_omx_present.push_back(buffer);
2277 input_time_present.push_back(time);
2278 input_is_last.push_back(islast);
2279 input_bufs_omx_mutex.Unlock();
2283 OMX_ERRORTYPE VideoOMX::ProtOMXEmptyThisBuffer(OMX_HANDLETYPE handle, OMX_BUFFERHEADERTYPE* buffer)
2285 // protect the call to empty this buffer
2288 pthread_testcancel();
2289 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldcancelstate);
2290 pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldcanceltype);
2292 OMX_ERRORTYPE ret_val;
2293 ret_val=OMX_EmptyThisBuffer(handle,buffer);
2294 clock_mutex.Unlock();
2295 pthread_setcancelstate(oldcancelstate, NULL);
2296 pthread_setcanceltype(oldcanceltype, NULL);
2297 pthread_testcancel();
2301 void VideoOMX::PrepareMediaSample(const MediaPacketList& mplist,UINT samplepos)
2303 mediapackets.clear();
2304 list<MediaPacket>::const_iterator begin=mplist.begin();
2305 list<MediaPacket>::const_iterator itty=mplist.begin();
2306 advance(itty,min(mplist.size(),10));
2307 mediapackets.insert(mediapackets.begin(),begin,itty);//front
2310 UINT VideoOMX::DeliverMediaSample(UCHAR* buffer, UINT *samplepos)
2313 while (consumed<mediapackets.size()) {
2314 MediaPacket mediapacket=mediapackets[consumed];
2315 DeliverMediaPacket(mediapacket, buffer, samplepos);
2316 if (*samplepos == mediapacket.length) {
2320 } else return consumed;
2325 UINT VideoOMX::DeliverMediaPacket(MediaPacket packet,
2326 const UCHAR* buffer,
2329 if (packet.type == MPTYPE_VIDEO_H264)
2339 //Later add fail back code for libav
2341 *samplepos+=packet.length;
2342 return packet.length;
2346 if (!omx_running) return 0; // if we are not runnig do not do this
2347 if (paused) return 0; //Block if we pause
2349 if (packet.synched && FrameSkip(packet.presentation_time)) {
2350 *samplepos=packet.length;
2351 Log::getInstance()->log("Video", Log::DEBUG, "DeliverMediaPacketOMX Frameskip");
2352 *samplepos=packet.length;
2353 return packet.length;
2355 //long long current_media_time=GetCurrentSystemTime()+playbacktimeoffset;
2356 /* if (packet.synched &&
2357 (packet.presentation_time<0 /*|| // preroll skip frames
2358 (packet.presentation_time+5000000LL)<(current_media_time)*)) { // we are late skip
2359 Log::getInstance()->log("Video", Log::DEBUG, "DeliverMediaPacketOMX Preroll or too late %lld %lld; %lld", packet.presentation_time,current_media_time,playbacktimeoffset);
2360 *samplepos=packet.length;
2361 return packet.length;
2364 OMX_ERRORTYPE error;
2366 /*First Check, if we have an video sample*/
2370 return 0; //Not in iframe mode!
2374 if (packet.disconti) {
2376 if (cur_input_buf_omx) {
2377 PutBufferToPres(cur_input_buf_omx, lastreftimeOMX,true);
2378 cur_input_buf_omx=NULL;
2382 /*Inspect PES-Header */
2384 // OMX_STATETYPE temp_state;
2385 // OMX_GetState(omx_vid_dec,&temp_state);
2387 if (*samplepos==0) {//stripheader
2388 headerstrip=buffer[packet.pos_buffer+8]+9/*is this right*/;
2389 // if (h264) Log::getInstance()->log("Video", Log::DEBUG, "PES info %x %x %x %x",
2390 // buffer[packet.pos_buffer+0],buffer[packet.pos_buffer+1],buffer[packet.pos_buffer+2],buffer[packet.pos_buffer+3]);
2391 *samplepos+=headerstrip;
2392 if ( packet.synched ) {
2393 if (cur_input_buf_omx) {
2394 cur_input_buf_omx->nFlags|=OMX_BUFFERFLAG_ENDOFFRAME;
2395 PutBufferToPres(cur_input_buf_omx, lastreftimeOMX,true);
2396 cur_input_buf_omx=NULL;//write out old data
2402 if (!firstsynched) {//
2403 *samplepos=packet.length;//if we have not processed at least one
2404 return packet.length;//synched packet ignore it!
2409 if (!cur_input_buf_omx) {
2410 input_bufs_omx_mutex.Lock();
2411 if (input_bufs_omx_free.size()==0) {
2412 input_bufs_omx_mutex.Unlock();
2413 //Log::getInstance()->log("Video", Log::DEBUG, "Deliver MediaPacket no free sample");
2414 return 0; // we do not have a free media sample
2417 cur_input_buf_omx=input_bufs_omx_free.front();
2418 cur_input_buf_omx->nFilledLen=0;
2419 cur_input_buf_omx->nOffset=0;
2420 cur_input_buf_omx->nTimeStamp=0;
2421 input_bufs_omx_free.pop_front();
2422 input_bufs_omx_mutex.Unlock();
2428 if (cur_input_buf_omx->nFilledLen==0) {//will only be changed on first packet
2429 if (packet.synched) {
2430 // Log::getInstance()->log("Video", Log::DEBUG, "packet synched marker");
2432 //lastreftimePTS=packet.pts;
2433 if (omx_first_frame) { // TODO time
2434 cur_input_buf_omx->nFlags=OMX_BUFFERFLAG_STARTTIME;
2435 Log::getInstance()->log("Video", Log::DEBUG, "Starttime");
2436 omx_first_frame=false;
2438 //cur_input_buf_omx->nFlags=0;
2439 cur_input_buf_omx->nFlags|=OMX_BUFFERFLAG_TIME_UNKNOWN;
2441 lastreftimeOMX=packet.presentation_time;
2442 // Log::getInstance()->log("Video", Log::DEBUG, "Time code %lld pts %lld", lastreftimeOMX,packet.pts);
2443 lastreftimePTS=packet.pts;
2444 cur_input_buf_omx->nTimeStamp=0;//lastreftimeOMX; // the clock component is faulty;
2448 cur_input_buf_omx->nFlags=OMX_BUFFERFLAG_TIME_UNKNOWN;
2449 cur_input_buf_omx->nTimeStamp=0;
2450 //Log::getInstance()->log("Video", Log::DEBUG, "packet unsynched marker");
2451 // ms->SetSyncPoint(TRUE);
2453 if (packet.disconti) cur_input_buf_omx->nFlags|=OMX_BUFFERFLAG_DISCONTINUITY;
2458 unsigned int haveToCopy=packet.length-*samplepos;
2460 while (haveToCopy> (cur_input_buf_omx->nAllocLen-cur_input_buf_omx->nFilledLen)) {
2461 unsigned int cancopy=cur_input_buf_omx->nAllocLen-cur_input_buf_omx->nFilledLen;
2462 memcpy(cur_input_buf_omx->pBuffer+cur_input_buf_omx->nFilledLen,buffer+packet.pos_buffer+*samplepos,cancopy);
2463 haveToCopy-=cancopy;
2464 cur_input_buf_omx->nFilledLen+=cancopy;
2465 *samplepos+=cancopy;
2466 // push old buffer out
2468 PutBufferToPres(cur_input_buf_omx, lastreftimeOMX,false);
2470 input_bufs_omx_mutex.Lock();
2471 if (input_bufs_omx_free.size()==0) {
2472 input_bufs_omx_mutex.Unlock();
2473 // Log::getInstance()->log("Video", Log::DEBUG, "Deliver MediaPacket no free sample2");
2474 return *samplepos; // we do not have a free media sample
2476 cur_input_buf_omx=input_bufs_omx_free.front();
2477 cur_input_buf_omx->nFilledLen=0;
2478 cur_input_buf_omx->nOffset=0;
2479 cur_input_buf_omx->nTimeStamp=0;
2480 input_bufs_omx_free.pop_front();
2481 input_bufs_omx_mutex.Unlock();
2483 cur_input_buf_omx->nFlags=OMX_BUFFERFLAG_TIME_UNKNOWN;
2486 memcpy(cur_input_buf_omx->pBuffer+cur_input_buf_omx->nFilledLen,
2487 buffer+packet.pos_buffer+*samplepos,haveToCopy);
2488 cur_input_buf_omx->nFilledLen+=haveToCopy;
2492 *samplepos+=haveToCopy;
2501 bool VideoOMX::displayIFrame(const UCHAR* buffer, UINT length) {
2502 if (!omx_running) return false;
2504 EnterIframePlayback();
2506 int haveToCopy = length;
2508 if (!cur_input_buf_omx) {
2509 input_bufs_omx_mutex.Lock();
2510 if (input_bufs_omx_free.size() == 0) {
2511 input_bufs_omx_mutex.Unlock();
2512 // Log::getInstance()->log("Video", Log::DEBUG,
2513 // "Deliver MediaPacket no free sample");
2514 return false; // we do not have a free media sample
2517 cur_input_buf_omx = input_bufs_omx_free.front();
2518 cur_input_buf_omx->nFilledLen = 0;
2519 cur_input_buf_omx->nOffset = 0;
2520 cur_input_buf_omx->nTimeStamp = 0;
2521 input_bufs_omx_free.pop_front();
2522 input_bufs_omx_mutex.Unlock();
2526 unsigned int pattern, packet_length;
2527 unsigned int headerstrip = 0;
2532 //Now we strip the pes header
2533 pattern = (buffer[0] << 16) | (buffer[1] << 8) | (buffer[2]);
2534 while (read_pos + 7 <= length) {
2535 pattern = ((pattern << 8) & 0xFFFFFFFF) | buffer[read_pos + 3];
2536 if (pattern < 0x000001E0 || pattern > 0x000001EF) {
2540 headerstrip = buffer[read_pos + 8] + 9/*is this right*/;
2541 packet_length = ((buffer[read_pos + 4] << 8)
2542 | (buffer[read_pos + 5])) + 6;
2543 if (read_pos + packet_length > length)
2546 if ((headerstrip < packet_length)
2547 && (cur_input_buf_omx->nFilledLen + packet_length
2548 - headerstrip) > cur_input_buf_omx->nAllocLen) {
2550 cur_input_buf_omx->nFlags = OMX_BUFFERFLAG_STARTTIME;
2553 cur_input_buf_omx->nFlags
2554 |= OMX_BUFFERFLAG_TIME_UNKNOWN;
2557 cur_input_buf_omx->nTimeStamp = 0;
2558 PutBufferToPres(cur_input_buf_omx, 0,false);
2559 cur_input_buf_omx = NULL;
2561 if (!cur_input_buf_omx) {
2563 while (count < 100 && omx_running && iframemode) {
2566 input_bufs_omx_mutex.Lock();
2567 if (input_bufs_omx_free.size() == 0) {
2568 input_bufs_omx_mutex.Unlock();
2569 // Log::getInstance()->log("Video", Log::DEBUG,
2570 // "Ifrane no free sample");
2572 if (!omx_running) return false;
2575 cur_input_buf_omx = input_bufs_omx_free.front();
2576 cur_input_buf_omx->nFilledLen = 0;
2577 cur_input_buf_omx->nOffset = 0;
2578 cur_input_buf_omx->nTimeStamp = 0;
2579 input_bufs_omx_free.pop_front();
2580 input_bufs_omx_mutex.Unlock();
2583 if (!cur_input_buf_omx)
2588 if (packet_length > headerstrip) {
2590 cur_input_buf_omx->pBuffer
2591 + cur_input_buf_omx->nFilledLen,
2592 buffer + read_pos + headerstrip,
2593 packet_length - headerstrip);
2594 cur_input_buf_omx->nFilledLen += packet_length
2597 read_pos += packet_length;
2599 pattern = (buffer[read_pos] << 16)
2600 | (buffer[read_pos + 1] << 8) | (buffer[read_pos + 2]);
2606 cur_input_buf_omx->nFlags = OMX_BUFFERFLAG_STARTTIME;
2609 cur_input_buf_omx->nFlags |= OMX_BUFFERFLAG_TIME_UNKNOWN;
2612 cur_input_buf_omx->nTimeStamp = 0;
2613 PutBufferToPres(cur_input_buf_omx, 0,false);
2614 cur_input_buf_omx = NULL;
2617 MILLISLEEP(40); //Block a bit
2621 int VideoOMX::EnterIframePlayback()
2624 if (cur_input_buf_omx) {
2625 PutBufferToPres(cur_input_buf_omx, lastreftimeOMX,true);
2627 cur_input_buf_omx = NULL;
2629 clock_mutex.Unlock();