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"
26 #include "woptionpane.h"
27 #include "osdopenvg.h"
34 //A lot of parts of this file are heavily inspired by xbmc omx implementations
36 VideoOMX::VideoOMX() {
41 cur_input_buf_omx = NULL;
42 omx_h264 = omx_mpeg2 = true;
46 offsetvideonotset = true;
47 offsetaudionotset = true;
57 deinterlace=2; //advanced
66 int VideoOMX::init(UCHAR tformat)
68 if (initted) return 0;
71 if (!setFormat(tformat)) { shutdown(); return 0; }
72 if (!setConnection(COMPOSITERGB)) { shutdown(); return 0; }
73 if (!setAspectRatio(ASPECT4X3)) { shutdown(); return 0; }
74 if (!setMode(NORMAL)) { shutdown(); return 0; }
75 if (!setSource()) { shutdown(); return 0; }
76 if (!attachFrameBuffer()) { shutdown(); return 0; }
92 int VideoOMX::initUsingOSDObjects()
95 // we are called before the audio
98 if (error!=OMX_ErrorNone) {
99 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX failed %x", error);
107 OMX_ERRORTYPE VideoOMX::EventHandler_OMX(OMX_IN OMX_HANDLETYPE handle,OMX_IN OMX_PTR appdata,
108 OMX_IN OMX_EVENTTYPE event_type,OMX_IN OMX_U32 data1,
109 OMX_IN OMX_U32 data2,OMX_IN OMX_PTR event_data) {
111 Log::getInstance()->log("Video", Log::NOTICE, "eventHandler %x %x %x %x %x",handle,event_type,data1,data2,event_data);
113 struct VPE_OMX_EVENT new_event;
114 new_event.handle=handle;
115 new_event.appdata=appdata;
116 new_event.event_type=event_type;
117 new_event.data1=data1;
118 new_event.data2=data2;
119 new_event.event_data=event_data;
121 VideoOMX *video=(VideoOMX *)getInstance();
122 video->AddOmxEvent(new_event);
124 /* switch (event_type) {
125 case OMX_EventCmdComplete: {
130 return OMX_ErrorNone;
134 void VideoOMX::AddOmxEvent(VPE_OMX_EVENT new_event)
136 omx_event_mutex.Lock();
137 omx_events.push_back(new_event);
138 omx_event_mutex.Unlock();
142 OMX_ERRORTYPE VideoOMX::EmptyBufferDone_OMX(OMX_IN OMX_HANDLETYPE hcomp,OMX_IN OMX_PTR appdata,OMX_IN OMX_BUFFERHEADERTYPE* buffer){
144 //Log::getInstance()->log("Video", Log::NOTICE, "EmptyBufferDone");
145 VideoOMX *video=(VideoOMX *)getInstance();
146 video->ReturnEmptyOMXBuffer(buffer);
147 return OMX_ErrorNone;
151 void VideoOMX::ReturnEmptyOMXBuffer(OMX_BUFFERHEADERTYPE* buffer){
152 input_bufs_omx_mutex.Lock();
153 //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());
154 input_bufs_omx_free.push_back(buffer);
155 //Log::getInstance()->log("Video", Log::NOTICE, "ReturnEmptyOMXBuffer %d",input_bufs_omx_free.size());
156 input_bufs_omx_mutex.Unlock();
159 OMX_ERRORTYPE VideoOMX::FillBufferDone_OMX(OMX_IN OMX_HANDLETYPE hcomp, OMX_IN OMX_PTR appdata,OMX_IN OMX_BUFFERHEADERTYPE* buffer) {
160 Log::getInstance()->log("Video", Log::NOTICE, "FillBufferDone");
161 return OMX_ErrorNone;
166 int VideoOMX::shutdown()
168 if (!initted) return 0;
170 Log::getInstance()->log("Video", Log::NOTICE, "Shutdown video module");
172 DeAllocateCodecsOMX();
174 //vc_tv_show_info(0); // back to console
177 struct fb_var_screeninfo screeninfo;
178 fd_fbset=open("/dev/fb0",O_RDONLY);
180 Log::getInstance()->log("Video", Log::CRIT, "Could not open frame buffer device %d", fd_fbset);
183 if (ioctl(fd_fbset, FBIOGET_VSCREENINFO, &screeninfo)){
185 Log::getInstance()->log("Video", Log::CRIT, "Could not FBIOGET_VSCREENINFO frame buffer device");
188 screeninfo.bits_per_pixel=8;
189 if (ioctl(fd_fbset, FBIOPUT_VSCREENINFO, &screeninfo)){
190 Log::getInstance()->log("Video", Log::CRIT, "Could not FBIOPUT_VSCREENINFO frame buffer device");
192 screeninfo.bits_per_pixel=16;
193 if (ioctl(fd_fbset, FBIOPUT_VSCREENINFO, &screeninfo)){
194 Log::getInstance()->log("Video", Log::CRIT, "Could not FBIOPUT_VSCREENINFO frame buffer device");
202 bool VideoOMX::loadOptionsfromServer(VDR* vdr)
204 Log::getInstance()->log("Video", Log::DEBUG, "VideoOMX config load");
205 char *name=vdr->configLoad("VideoOMX","SDDeinterlacing");
208 if (STRCASECMP(name, "None") == 0) {
210 }/* else if (STRCASECMP(name, "LineDouble") == 0) {
212 }*/ else if (STRCASECMP(name, "Advanced") == 0) {
214 } /*else if (STRCASECMP(name, "Crazy") == 0) {
215 deinterlace = 3; // this does not activate deinterlacing but a image filter, just for fun
217 Log::getInstance()->log("Video", Log::DEBUG, "Set deinterlacing to %s %d",name,deinterlace);
224 bool VideoOMX::handleOptionChanges(Option* option)
226 if (Video::handleOptionChanges(option))
228 switch (option->id) {
230 if (STRCASECMP(option->options[option->userSetChoice], "None") == 0) {
232 } /*else if (STRCASECMP(option->options[option->userSetChoice], "LineDouble")
235 }*/ else if (STRCASECMP(option->options[option->userSetChoice], "Advanced")
238 } /*else if (STRCASECMP(option->options[option->userSetChoice], "Crazy")
242 Log::getInstance()->log("Video", Log::DEBUG, "Set deinterlacing to %s %d",option->options[option->userSetChoice],deinterlace);
251 bool VideoOMX::saveOptionstoServer()
254 switch (deinterlace) {
256 VDR::getInstance()->configSave("VideoOMX","SDDeinterlacing", "None");
259 VDR::getInstance()->configSave("VideoOMX","SDDeinterlacing", "LineDouble");
262 VDR::getInstance()->configSave("VideoOMX","SDDeinterlacing", "Advanced");
265 VDR::getInstance()->configSave("VideoOMX","SDDeinterlacing", "Crazy");
272 /*Option(UINT id, const char* displayText, const char* configSection, const char* configKey, UINT optionType,
273 UINT numChoices, UINT defaultChoice, UINT startInt,
274 const char * const * options, const char * const * optionkeys = NULL, AbstractOption* handler=NULL);*/
276 bool VideoOMX::addOptionsToPanes(int panenumber,Options *options,WOptionPane* pane)
278 if (!Video::addOptionsToPanes(panenumber,options,pane)) return false;
284 static const char* deinterlaceopts[]={"None",/*"LineDouble",*/"Advanced"/*,"Crazy"*/};
285 option = new Option(1,tr("SD Deinterlacing"), "VideoOMX","SDDeinterlacing",Option::TYPE_TEXT,/*4,2*/2,1,0,deinterlaceopts,NULL,false,this);
286 options->push_back(option);
287 pane->addOptionLine(option);
295 int VideoOMX::setTVsize(UCHAR ttvsize)
299 // Override the aspect ratio usage, temporarily use to set the video chip mode
300 if (!setAspectRatio(tvsize)) { shutdown(); return 0; }
302 if ((fdVideo = open("/dev/vdec_dev", O_WRONLY)) < 0) return 0;
303 if (!setSource()) { shutdown(); return 0; }
304 if (!attachFramebuffer()) { shutdown(); return 0; }
306 // Reopening the fd causes the scart aspect line to go back to 4:3
307 // Set this again to the same as the tv screen size
308 if (!setAspectRatio(tvsize)) { shutdown(); return 0; }
310 // mode == LETTERBOX is invalid if the TV is widescreen
311 if (tvsize == ASPECT16X9) setMode(NORMAL);
316 int VideoOMX::setDefaultAspect()
318 return setAspectRatio(tvsize);
323 int VideoOMX::setFormat(UCHAR tformat)
325 if (!initted) return 0;
326 if ((tformat != PAL) && (tformat != NTSC)) return 0;
336 // selectVideoMode(0);
341 void VideoOMX::selectVideoMode(int interlaced)
344 TV_SUPPORTED_MODE_T all_supp_modes[TV_MAX_SUPPORTED_MODES];
345 HDMI_RES_GROUP_T pref_group;
346 TV_SUPPORTED_MODE_T *mymode=NULL;
347 TV_SUPPORTED_MODE_T *mymode_second_best=NULL;
348 bool got_optimum=false;
350 int all_my_modes=vc_tv_hdmi_get_supported_modes(HDMI_RES_GROUP_CEA,
351 all_supp_modes,TV_MAX_SUPPORTED_MODES,
352 &pref_group,&pref_mode);
354 if (format==PAL)target_fps=50;
355 else if (format==NTSC) target_fps=60;
356 if (all_my_modes<=0) {
357 Log::getInstance()->log("Video", Log::NOTICE, "No CEA modes found abort");
361 //Now first determine native resolution
362 int native_width=1920;
363 int native_height=1080;
364 for (int i=0;i<all_my_modes;i++) {
365 if (all_supp_modes[i].native) {
366 mymode=all_supp_modes+i;
367 Log::getInstance()->log("Video", Log::NOTICE, "Found native mode %dx%d %d Hz i: %d",
368 mymode->width,mymode->height,mymode->frame_rate,mymode->scan_mode);
372 //Now find the mode which matches best
373 for (int i=0;i<all_my_modes;i++) {
374 TV_SUPPORTED_MODE_T *curmode=all_supp_modes+i;
375 if (curmode->width==native_width &&
376 curmode->height==native_height &&
377 curmode->frame_rate==target_fps) {
378 if(curmode->scan_mode==interlaced) {
381 Log::getInstance()->log("Video", Log::NOTICE, "Found optimum mode %dx%d %d Hz i: %d",
382 mymode->width,mymode->height,mymode->frame_rate,mymode->scan_mode);
384 mymode_second_best=curmode;
385 Log::getInstance()->log("Video", Log::NOTICE, "Found close to optimum mode %dx%d %d Hz i: %d",
386 mymode_second_best->width,mymode_second_best->height,
387 mymode_second_best->frame_rate,mymode_second_best->scan_mode);
394 Log::getInstance()->log("Video", Log::NOTICE, "Switch to optimum mode");
395 vc_tv_hdmi_power_on_explicit(HDMI_MODE_HDMI,HDMI_RES_GROUP_CEA,mymode->code);
396 } else if (mymode_second_best) {
397 Log::getInstance()->log("Video", Log::NOTICE, "Switch to close to optimum mode");
398 vc_tv_hdmi_power_on_explicit(HDMI_MODE_HDMI,HDMI_RES_GROUP_CEA,mymode_second_best->code);
400 Log::getInstance()->log("Video", Log::NOTICE, "Switch to prefered mode");
401 vc_tv_hdmi_power_on_best(1920,1080,target_fps,interlaced?HDMI_INTERLACED:HDMI_NONINTERLACED,
402 (EDID_MODE_MATCH_FLAG_T)(HDMI_MODE_MATCH_FRAMERATE|HDMI_MODE_MATCH_RESOLUTION|HDMI_MODE_MATCH_SCANMODE));
412 int VideoOMX::setConnection(UCHAR tconnection)
414 if (!initted) return 0;
415 if ((tconnection != COMPOSITERGB) && (tconnection != SVIDEO)) return 0;
416 connection = tconnection;
418 // if (ioctl(fdVideo, AV_SET_VID_OUTPUT, connection) != 0) return 0;
422 int VideoOMX::setAspectRatio(UCHAR taspectRatio)
424 if (!initted) return 0;
425 if ((taspectRatio != ASPECT4X3) && (taspectRatio != ASPECT16X9)) return 0;
426 aspectRatio = taspectRatio;
428 Log::getInstance()->log("Video", Log::DEBUG, "Setting aspect to %i", aspectRatio);
430 // if (ioctl(fdVideo, AV_SET_VID_RATIO, aspectRatio) != 0) return 0;
434 int VideoOMX::setMode(UCHAR tmode)
436 if (!initted) return 0;
443 void VideoOMX::updateMode()
448 OMX_CONFIG_DISPLAYREGIONTYPE dispconf;
449 memset(&dispconf, 0, sizeof(dispconf));
450 dispconf.nSize = sizeof(dispconf);
451 dispconf.nVersion.nVersion = OMX_VERSION;
452 dispconf.nPortIndex = omx_rend_input_port;
454 dispconf.set = OMX_DISPLAY_SET_LAYER;
455 error = OMX_SetParameter(omx_vid_rend, OMX_IndexConfigDisplayRegion,
457 if (error != OMX_ErrorNone) {
458 Log::getInstance()->log("Video", Log::DEBUG,
459 "Set OMX_IndexConfigDisplayRegion1 failed %x", error);
460 clock_mutex.Unlock();
463 dispconf.set = OMX_DISPLAY_SET_FULLSCREEN;
464 if (mode != QUARTER && mode != EIGHTH) {
466 dispconf.fullscreen = OMX_TRUE;
468 dispconf.fullscreen = OMX_FALSE;
470 error = OMX_SetParameter(omx_vid_rend, OMX_IndexConfigDisplayRegion,
472 if (error != OMX_ErrorNone) {
473 Log::getInstance()->log("Video", Log::DEBUG,
474 "Set OMX_IndexConfigDisplayRegion2 failed %x", error);
475 clock_mutex.Unlock();
479 dispconf.set = OMX_DISPLAY_SET_MODE;
480 if (mode != QUARTER && mode != EIGHTH) {
481 dispconf.mode = (mode == NORMAL) ? OMX_DISPLAY_MODE_FILL
482 : OMX_DISPLAY_MODE_LETTERBOX;
484 dispconf.mode = OMX_DISPLAY_MODE_LETTERBOX;
486 error = OMX_SetParameter(omx_vid_rend, OMX_IndexConfigDisplayRegion,
488 if (error != OMX_ErrorNone) {
489 Log::getInstance()->log("Video", Log::DEBUG,
490 "Set OMX_IndexConfigDisplayRegion3 failed %x", error);
491 clock_mutex.Unlock();
495 if (mode == QUARTER || mode == EIGHTH) {
496 unsigned int display_width, display_height;
497 display_width = display_height = 0;
498 if (graphics_get_display_size(0, &display_width, &display_height)
500 Log::getInstance()->log("OSD", Log::WARN,
501 "Getting display size failed! (BCM API) ");
502 clock_mutex.Unlock();
505 //UnSetFullscreen with window
506 dispconf.set = OMX_DISPLAY_SET_DEST_RECT;
507 dispconf.dest_rect.x_offset
508 = (int) (xpos * ((float) display_width));
509 dispconf.dest_rect.y_offset = (int) (ypos
510 * ((float) display_height));
511 if (mode == QUARTER) {
512 dispconf.dest_rect.width = display_width >> 1;
513 dispconf.dest_rect.height = display_height >> 1;
514 } else if (mode == EIGHTH) {
515 dispconf.dest_rect.width = display_width >> 2;
516 dispconf.dest_rect.height = display_height >> 2;
518 error = OMX_SetParameter(omx_vid_rend,
519 OMX_IndexConfigDisplayRegion, &dispconf);
520 if (error != OMX_ErrorNone) {
521 Log::getInstance()->log("Video", Log::DEBUG,
522 "Set OMX_IndexConfigDisplayRegion failed %x", error);
523 clock_mutex.Unlock();
529 clock_mutex.Unlock();
532 int VideoOMX::signalOff()
534 // if (ioctl(fdVideo, AV_SET_VID_DENC, 0) != 0) return 0;
538 int VideoOMX::signalOn()
540 // if (ioctl(fdVideo, AV_SET_VID_DENC, 1) != 0) return 0;
544 int VideoOMX::setSource()
546 if (!initted) return 0;
548 // What does this do...
549 // if (ioctl(fdVideo, AV_SET_VID_SRC, 1) != 0) return 0;
553 int VideoOMX::setPosition(int x, int y) {
556 xpos = ((float) x*2.f) / ((float) screenWidth);
557 ypos = ((float) y*2.f) / ((float) screenHeight);
565 if (!initted) return 0;
567 // if (ioctl(fdVideo, AV_SET_VID_SYNC, 2) != 0) return 0;
572 int VideoOMX::play() {
576 Log::getInstance()->log("Video", Log::DEBUG, "enter play");
578 if (AllocateCodecsOMX()) {
580 // Otherwise fall back to libav
584 Log::getInstance()->log("Video", Log::NOTICE,
585 "Allocate Codecs OMX failed assume h264 unsupported");
588 Log::getInstance()->log("Video", Log::NOTICE,
589 "Allocate Codecs OMX failed assume mpeg2 unsupported");
598 int VideoOMX::initClock()
602 if (clock_references==0)
605 static OMX_CALLBACKTYPE callbacks= {&EventHandler_OMX,&EmptyBufferDone_OMX,&FillBufferDone_OMX};
608 error=OMX_GetHandle(&omx_clock,VPE_OMX_CLOCK,NULL,&callbacks);
610 if (error!=OMX_ErrorNone) {
611 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX clock failed %x", error);
612 clock_mutex.Unlock();
613 DeAllocateCodecsOMX();
617 /* TODO Clock config to separate method */
618 OMX_PORT_PARAM_TYPE p_param;
619 memset(&p_param,0,sizeof(p_param));
620 p_param.nSize=sizeof(p_param);
621 p_param.nVersion.nVersion=OMX_VERSION;
622 error=OMX_GetParameter(omx_clock,OMX_IndexParamOtherInit,&p_param);
623 if (error!=OMX_ErrorNone) {
624 Log::getInstance()->log("Video", Log::DEBUG, "Init clock OMX_GetParameter failed %x", error);
625 clock_mutex.Unlock();
626 DeAllocateCodecsOMX();
629 omx_clock_output_port=p_param.nStartPortNumber;
631 for (unsigned int i=0;i<p_param.nPorts;i++) {
632 if (!DisablePort(omx_clock,p_param.nStartPortNumber+i,true) ) {
633 Log::getInstance()->log("Video", Log::DEBUG, "Disable Ports OMX clock failed %d",i);
634 clock_mutex.Unlock();
635 DeAllocateCodecsOMX();
644 Log::getInstance()->log("Video", Log::DEBUG, "init omx clock %x %x",this,omx_clock);
646 clock_mutex.Unlock();
650 int VideoOMX::getClockAudioandInit(OMX_HANDLETYPE *p_omx_clock,OMX_U32 *p_omx_clock_output_port)
654 *p_omx_clock_output_port=0;
661 OMX_TIME_CONFIG_ACTIVEREFCLOCKTYPE refclock;
662 memset(&refclock,0,sizeof(refclock));
663 refclock.nSize=sizeof(refclock);
664 refclock.nVersion.nVersion=OMX_VERSION;
666 refclock.eClock=OMX_TIME_RefClockAudio;
668 //refclock.eClock=OMX_TIME_RefClockVideo;
669 error=OMX_SetConfig(omx_clock,OMX_IndexConfigTimeActiveRefClock,&refclock);
670 if (error!=OMX_ErrorNone){
671 Log::getInstance()->log("Video", Log::DEBUG, "Clock OMX_IndexConfigTimeActiveRefClock failed %x", error);
672 clock_mutex.Unlock();
673 DeAllocateCodecsOMX();
677 OMX_PORT_PARAM_TYPE p_param;
678 memset(&p_param,0,sizeof(p_param));
679 p_param.nSize=sizeof(p_param);
680 p_param.nVersion.nVersion=OMX_VERSION;
681 error=OMX_GetParameter(omx_clock,OMX_IndexParamOtherInit,&p_param);
682 if (error!=OMX_ErrorNone){
683 Log::getInstance()->log("Video", Log::DEBUG, "Init clock OMX_GetParameter failed %x", error);
684 clock_mutex.Unlock();
685 DeAllocateCodecsOMX();
689 OMX_TIME_CONFIG_CLOCKSTATETYPE clock_conf;
690 memset(&clock_conf,0,sizeof(clock_conf));
691 clock_conf.nSize=sizeof(clock_conf);
692 clock_conf.nVersion.nVersion=OMX_VERSION;
693 clock_conf.eState=OMX_TIME_ClockStateWaitingForStartTime;
694 clock_conf.nStartTime=0;
695 clock_conf.nOffset=0;
696 if (clock_references==1) clock_conf.nWaitMask=OMX_CLOCKPORT1;
697 else clock_conf.nWaitMask=OMX_CLOCKPORT0|OMX_CLOCKPORT1;
698 error=OMX_SetConfig(omx_clock,OMX_IndexConfigTimeClockState,&clock_conf);
699 if (error!=OMX_ErrorNone) {
700 Log::getInstance()->log("Video", Log::DEBUG, "AuI Clock IndexConfigTimeClockState failed %x", error);
704 *p_omx_clock_output_port=p_param.nStartPortNumber+1;
705 *p_omx_clock=omx_clock;
706 clock_mutex.Unlock();
710 int VideoOMX::getClockVideoandInit()
719 if (clock_references==1) { // only if no audio is attached to this clock!
720 OMX_TIME_CONFIG_ACTIVEREFCLOCKTYPE refclock;
721 memset(&refclock,0,sizeof(refclock));
722 refclock.nSize=sizeof(refclock);
723 refclock.nVersion.nVersion=OMX_VERSION;
725 //refclock.eClock=OMX_TIME_RefClockAudio;
727 refclock.eClock=OMX_TIME_RefClockVideo;
728 error=OMX_SetConfig(omx_clock,OMX_IndexConfigTimeActiveRefClock,&refclock);
729 if (error!=OMX_ErrorNone) {
730 Log::getInstance()->log("Video", Log::DEBUG, "Clock OMX_IndexConfigTimeActiveRefClock failed %x", error);
731 clock_mutex.Unlock();
732 DeAllocateCodecsOMX();
737 OMX_PORT_PARAM_TYPE p_param;
738 memset(&p_param,0,sizeof(p_param));
739 p_param.nSize=sizeof(p_param);
740 p_param.nVersion.nVersion=OMX_VERSION;
741 error=OMX_GetParameter(omx_clock,OMX_IndexParamOtherInit,&p_param);
742 if (error!=OMX_ErrorNone){
743 Log::getInstance()->log("Video", Log::DEBUG, "Init clock OMX_GetParameter failed %x", error);
744 clock_mutex.Unlock();
745 DeAllocateCodecsOMX();
750 OMX_TIME_CONFIG_CLOCKSTATETYPE clock_conf;
751 memset(&clock_conf,0,sizeof(clock_conf));
752 clock_conf.nSize=sizeof(clock_conf);
753 clock_conf.nVersion.nVersion=OMX_VERSION;
754 clock_conf.eState=OMX_TIME_ClockStateWaitingForStartTime;
755 clock_conf.nStartTime=0;
756 clock_conf.nOffset=0;
757 if (clock_references==1) clock_conf.nWaitMask=OMX_CLOCKPORT0;
758 else clock_conf.nWaitMask=OMX_CLOCKPORT0|OMX_CLOCKPORT1;
759 error=OMX_SetConfig(omx_clock,OMX_IndexConfigTimeClockState,&clock_conf);
760 if (error!=OMX_ErrorNone) {
761 Log::getInstance()->log("Video", Log::DEBUG, "VuI Clock IndexConfigTimeClockState failed %x", error);
764 omx_clock_output_port=p_param.nStartPortNumber;
765 clock_mutex.Unlock();
770 void VideoOMX::clockUnpause()
774 if (clock_references>0) {
775 OMX_TIME_CONFIG_CLOCKSTATETYPE clock_conf;
776 memset(&clock_conf,0,sizeof(clock_conf));
777 clock_conf.nSize=sizeof(clock_conf);
778 clock_conf.nVersion.nVersion=OMX_VERSION;
779 clock_conf.eState=OMX_TIME_ClockStateRunning;
780 clock_conf.nStartTime=0;
781 clock_conf.nOffset=0;
782 clock_conf.nWaitMask=OMX_CLOCKPORT1;
783 error=OMX_SetConfig(omx_clock,OMX_IndexConfigTimeClockState,&clock_conf);
784 if (error!=OMX_ErrorNone) {
785 Log::getInstance()->log("Video", Log::DEBUG, "ClockUnpause IndexConfigTimeClockState failed %x", error);
788 clock_mutex.Unlock();
792 void VideoOMX::clockPause()
796 if (clock_references>0) {
797 OMX_TIME_CONFIG_CLOCKSTATETYPE clock_conf;
798 memset(&clock_conf,0,sizeof(clock_conf));
799 clock_conf.nSize=sizeof(clock_conf);
800 clock_conf.nVersion.nVersion=OMX_VERSION;
801 clock_conf.eState=OMX_TIME_ClockStateStopped;
802 clock_conf.nStartTime=0;
803 clock_conf.nOffset=0;
804 clock_conf.nWaitMask=OMX_CLOCKPORT1;
805 error=OMX_SetConfig(omx_clock,OMX_IndexConfigTimeClockState,&clock_conf);
806 if (error!=OMX_ErrorNone) {
807 Log::getInstance()->log("Video", Log::DEBUG, "ClockUnpause IndexConfigTimeClockState failed %x", error);
810 clock_mutex.Unlock();
815 int VideoOMX::AllocateCodecsOMX()
818 static OMX_CALLBACKTYPE callbacks= {&EventHandler_OMX,&EmptyBufferDone_OMX,&FillBufferDone_OMX};
820 Demuxer* demux=Demuxer::getInstance();
823 deint_first_frame=false;
825 Log::getInstance()->log("Video", Log::NOTICE, "Allocate Codecs OMX");
826 //Clock, move later to audio including events
828 if (deinterlace!=0 && (demux->getHorizontalSize()<=720 /*|| demux->getHorizontalSize()==1920*/)) { //only deinterlace SD material
830 deint_first_frame=true;
832 Log::getInstance()->log("Video", Log::NOTICE, "Deinterlacing activated %d",deinterlace);
837 if (!getClockVideoandInit()){
838 return 0;// get the clock and init it if necessary
843 Log::getInstance()->log("Video", Log::DEBUG, "idleClock failed");
851 error=OMX_GetHandle(&omx_vid_dec,VPE_OMX_H264_DECODER,NULL,&callbacks);
853 error=OMX_GetHandle(&omx_vid_dec,VPE_OMX_MPEG2_DECODER,NULL,&callbacks);
856 if (error!=OMX_ErrorNone){
857 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX video decoder failed %x", error);
858 clock_mutex.Unlock();
859 DeAllocateCodecsOMX();
864 Log::getInstance()->log("Video", Log::DEBUG, "Nmark3 ");
865 OMX_PORT_PARAM_TYPE p_param;
866 memset(&p_param,0,sizeof(p_param));
867 p_param.nSize=sizeof(p_param);
868 p_param.nVersion.nVersion=OMX_VERSION;
869 error=OMX_GetParameter(omx_vid_dec,OMX_IndexParamVideoInit,&p_param);
870 if (error!=OMX_ErrorNone){
871 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX h264 decoder OMX_GetParameter failed %x", error);
872 clock_mutex.Unlock();
873 DeAllocateCodecsOMX();
876 omx_codec_input_port=p_param.nStartPortNumber;
877 omx_codec_output_port=p_param.nStartPortNumber+1;
879 if (!DisablePort(omx_vid_dec,omx_codec_input_port) || !DisablePort(omx_vid_dec,omx_codec_output_port)) {
880 Log::getInstance()->log("Video", Log::DEBUG, "Disable Ports OMX video decoder failed");
881 clock_mutex.Unlock();
882 DeAllocateCodecsOMX();
886 Log::getInstance()->log("Video", Log::DEBUG, "Nmark4 ");
888 OMX_PARAM_BRCMVIDEODECODEERRORCONCEALMENTTYPE conceal;
889 memset(&conceal,0,sizeof(conceal));
890 conceal.nSize=sizeof(conceal);
891 conceal.nVersion.nVersion=OMX_VERSION;
892 conceal.bStartWithValidFrame=OMX_FALSE;
894 error=OMX_SetParameter(omx_vid_dec,OMX_IndexParamBrcmVideoDecodeErrorConcealment,&conceal);
895 if (error!=OMX_ErrorNone){
896 Log::getInstance()->log("Video", Log::DEBUG, "OMX_IndexParamBrcmVideoDecodeErrorConcealment failed %x", error);
897 clock_mutex.Unlock();
898 DeAllocateCodecsOMX();
903 error = OMX_GetHandle(&omx_vid_deint, VPE_OMX_VIDEO_DEINTERLACE, NULL,
905 if (error != OMX_ErrorNone) {
906 Log::getInstance()->log("Video", Log::DEBUG,
907 "Init OMX video deinterlacer failed %x", error);
908 clock_mutex.Unlock();
909 DeAllocateCodecsOMX();
913 error = OMX_GetParameter(omx_vid_deint, OMX_IndexParamImageInit,
915 if (error != OMX_ErrorNone) {
916 Log::getInstance()->log("Video", Log::DEBUG,
917 "Init OMX video deinterlacer OMX_GetParameter failed %x",
919 clock_mutex.Unlock();
920 DeAllocateCodecsOMX();
923 omx_deint_input_port = p_param.nStartPortNumber;
924 omx_deint_output_port = p_param.nStartPortNumber + 1;
926 if (!DisablePort(omx_vid_deint, omx_deint_input_port, true)
927 || !DisablePort(omx_vid_deint, omx_deint_output_port, true)) {
928 Log::getInstance()->log("Video", Log::DEBUG,
929 "Disable Ports OMX video deint failed");
930 clock_mutex.Unlock();
931 DeAllocateCodecsOMX();
938 error=OMX_GetHandle(&omx_vid_sched,VPE_OMX_VIDEO_SCHED,NULL,&callbacks);
939 if (error!=OMX_ErrorNone){
940 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX video scheduler failed %x", error);
941 clock_mutex.Unlock();
942 DeAllocateCodecsOMX();
948 error=OMX_GetParameter(omx_vid_sched,OMX_IndexParamVideoInit,&p_param);
949 if (error!=OMX_ErrorNone){
950 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX video scheduler OMX_GetParameter failed %x", error);
951 clock_mutex.Unlock();
952 DeAllocateCodecsOMX();
955 omx_shed_input_port=p_param.nStartPortNumber;
956 omx_shed_output_port=p_param.nStartPortNumber+1;
959 error=OMX_GetParameter(omx_vid_sched,OMX_IndexParamOtherInit,&p_param);
960 if (error!=OMX_ErrorNone){
961 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX video scheduler OMX_GetParameter failed %x", error);
962 clock_mutex.Unlock();
963 DeAllocateCodecsOMX();
966 omx_shed_clock_port=p_param.nStartPortNumber;
967 Log::getInstance()->log("Video", Log::DEBUG, "scheduler ports %d %d %d ",omx_shed_input_port,omx_shed_output_port,omx_shed_clock_port);
970 if (!DisablePort(omx_vid_sched,omx_shed_input_port,true) || !DisablePort(omx_vid_sched,omx_shed_output_port,true)
971 || !DisablePort(omx_vid_sched,omx_shed_clock_port,true)) {
972 Log::getInstance()->log("Video", Log::DEBUG, "Disable Ports OMX video shed failed");
973 clock_mutex.Unlock();
974 DeAllocateCodecsOMX();
979 error=OMX_GetHandle(&omx_vid_rend,VPE_OMX_VIDEO_REND,NULL,&callbacks);
980 if (error!=OMX_ErrorNone){
981 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX video rend failed %x", error);
982 clock_mutex.Unlock();
983 DeAllocateCodecsOMX();
987 error=OMX_GetParameter(omx_vid_rend,OMX_IndexParamVideoInit,&p_param);
988 if (error!=OMX_ErrorNone){
989 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX video rend OMX_GetParameter failed %x", error);
990 clock_mutex.Unlock();
991 DeAllocateCodecsOMX();
994 omx_rend_input_port=p_param.nStartPortNumber;
995 //omx_rend_output_port=p_param.nStartPortNumber+1;
998 if (!DisablePort(omx_vid_rend,omx_rend_input_port,true) /*|| !DisablePort(omx_vid_rend,omx_rend_output_port)*/
1000 Log::getInstance()->log("Video", Log::DEBUG, "Disable Ports OMX video rend failed");
1001 clock_mutex.Unlock();
1002 DeAllocateCodecsOMX();
1010 error=OMX_SetupTunnel(omx_clock,omx_clock_output_port,omx_vid_sched,omx_shed_clock_port);
1011 if (error!=OMX_ErrorNone){
1012 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);
1013 clock_mutex.Unlock();
1014 DeAllocateCodecsOMX();
1018 if (!EnablePort(omx_clock,omx_clock_output_port,false) || !EnablePort(omx_vid_sched,omx_shed_clock_port,false)
1020 Log::getInstance()->log("Video", Log::DEBUG, "Enable Ports OMX clock shed failed");
1021 clock_mutex.Unlock();
1022 DeAllocateCodecsOMX();
1027 Log::getInstance()->log("Video", Log::DEBUG, "mark2 ");
1028 if (!ChangeComponentState(omx_vid_sched,OMX_StateIdle)) {
1029 Log::getInstance()->log("Video", Log::DEBUG, "vid_sched idle ChangeComponentState");
1030 clock_mutex.Unlock();
1031 DeAllocateCodecsOMX();
1037 Log::getInstance()->log("Video", Log::DEBUG, "mark1 ");
1038 if ( !CommandFinished(omx_vid_sched,OMX_CommandPortEnable,omx_shed_clock_port)) {
1039 clock_mutex.Unlock();
1040 DeAllocateCodecsOMX();
1047 Log::getInstance()->log("Video", Log::DEBUG, "mark1 special ");
1048 if ( !CommandFinished(omx_clock,OMX_CommandPortEnable,omx_clock_output_port)) {
1049 clock_mutex.Unlock();
1050 DeAllocateCodecsOMX();
1056 Log::getInstance()->log("Video", Log::DEBUG, "mark1b ");
1059 /* error=OMX_SendCommand(omx_vid_dec,OMX_CommandStateSet,OMX_StateIdle,0);
1060 if (error!=OMX_ErrorNone){
1061 Log::getInstance()->log("Video", Log::DEBUG, "vid_dec Send Command to OMX State Idle %x", error);
1065 OMX_VIDEO_PARAM_PORTFORMATTYPE ft_type;
1066 memset(&ft_type,0,sizeof(ft_type));
1067 ft_type.nSize=sizeof(ft_type);
1068 ft_type.nVersion.nVersion=OMX_VERSION;
1070 ft_type.nPortIndex=omx_codec_input_port;
1072 ft_type.eCompressionFormat=OMX_VIDEO_CodingAVC;
1074 ft_type.eCompressionFormat=OMX_VIDEO_CodingMPEG2;
1079 ft_type.xFramerate=0;//25*(1<<16);//demux->getFrameRate()*(1<<16);
1080 Log::getInstance()->log("Video", Log::DEBUG, "Framerate: %d",demux->getFrameRate());
1081 error=OMX_SetParameter(omx_vid_dec,OMX_IndexParamVideoPortFormat,&ft_type);
1082 if (error!=OMX_ErrorNone){
1083 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX_IndexParamVideoPortFormat failed %x", error);
1084 clock_mutex.Unlock();
1085 DeAllocateCodecsOMX();
1090 if (!ChangeComponentState(omx_vid_dec,OMX_StateIdle)) {
1091 Log::getInstance()->log("Video", Log::DEBUG, "vid_dec ChangeComponentState");
1092 clock_mutex.Unlock();
1093 DeAllocateCodecsOMX();
1098 if (!PrepareInputBufsOMX()) {
1099 clock_mutex.Unlock();
1100 DeAllocateCodecsOMX();
1105 error=OMX_SetupTunnel(omx_vid_dec,omx_codec_output_port,omx_vid_sched,omx_shed_input_port);
1106 if (error!=OMX_ErrorNone){
1107 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel dec to sched failed %x", error);
1108 clock_mutex.Unlock();
1109 DeAllocateCodecsOMX();
1115 if (!EnablePort(omx_vid_dec,omx_codec_output_port,false) || !EnablePort(omx_vid_sched,omx_shed_input_port,false)
1117 Log::getInstance()->log("Video", Log::DEBUG, "Enable Ports OMX codec shed failed");
1118 clock_mutex.Unlock();
1119 DeAllocateCodecsOMX();
1123 if ( !CommandFinished(omx_vid_dec,OMX_CommandPortEnable,omx_codec_output_port) || !CommandFinished(omx_vid_sched,OMX_CommandPortEnable,omx_shed_input_port)) {
1124 clock_mutex.Unlock();
1125 DeAllocateCodecsOMX();
1131 error=OMX_SetupTunnel(omx_vid_dec,omx_codec_output_port,omx_vid_deint,omx_deint_input_port);
1132 if (error!=OMX_ErrorNone){
1133 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel dec to deint failed %x", error);
1134 clock_mutex.Unlock();
1135 DeAllocateCodecsOMX();
1141 if (!EnablePort(omx_vid_dec,omx_codec_output_port,false) || !EnablePort(omx_vid_deint,omx_deint_input_port,false)
1143 Log::getInstance()->log("Video", Log::DEBUG, "Enable Ports OMX codec deint failed");
1144 clock_mutex.Unlock();
1145 DeAllocateCodecsOMX();
1149 if ( !CommandFinished(omx_vid_dec,OMX_CommandPortEnable,omx_codec_output_port) || !CommandFinished(omx_vid_deint,OMX_CommandPortEnable,omx_deint_input_port)) {
1150 clock_mutex.Unlock();
1151 DeAllocateCodecsOMX();
1155 if (!ChangeComponentState(omx_vid_deint,OMX_StateIdle)) {
1156 Log::getInstance()->log("Video", Log::DEBUG, "vid_deint ChangeComponentState");
1157 clock_mutex.Unlock();
1158 DeAllocateCodecsOMX();
1162 OMX_CONFIG_IMAGEFILTERPARAMSTYPE imagefilter;
1163 memset(&imagefilter,0,sizeof(imagefilter));
1164 imagefilter.nSize=sizeof(imagefilter);
1165 imagefilter.nVersion.nVersion=OMX_VERSION;
1167 imagefilter.nPortIndex=omx_deint_output_port;
1168 imagefilter.nNumParams=1;
1169 imagefilter.nParams[0]=3;//???
1170 switch (deinterlace) {
1172 imagefilter.eImageFilter=OMX_ImageFilterDeInterlaceLineDouble; break;
1174 imagefilter.eImageFilter=OMX_ImageFilterDeInterlaceAdvanced; break;
1176 imagefilter.eImageFilter=OMX_ImageFilterFilm; break;
1180 error=OMX_SetConfig(omx_vid_deint,OMX_IndexConfigCommonImageFilterParameters,&imagefilter);
1181 if (error!=OMX_ErrorNone){
1182 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX_IndexConfigCommonImageFilterParameters failed %x", error);
1183 clock_mutex.Unlock();
1184 DeAllocateCodecsOMX();
1189 error=OMX_SetupTunnel(omx_vid_deint,omx_deint_output_port,omx_vid_sched,omx_shed_input_port);
1190 if (error!=OMX_ErrorNone){
1191 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel deint to sched failed %x", error);
1192 clock_mutex.Unlock();
1193 DeAllocateCodecsOMX();
1197 if (!EnablePort(omx_vid_deint,omx_deint_output_port,false) || !EnablePort(omx_vid_sched,omx_shed_input_port,false)
1199 Log::getInstance()->log("Video", Log::DEBUG, "Enable Ports OMX deint shed failed");
1200 clock_mutex.Unlock();
1201 DeAllocateCodecsOMX();
1205 if ( !CommandFinished(omx_vid_deint,OMX_CommandPortEnable,omx_deint_output_port) || !CommandFinished(omx_vid_sched,OMX_CommandPortEnable,omx_shed_input_port)) {
1206 clock_mutex.Unlock();
1207 DeAllocateCodecsOMX();
1213 if (!ChangeComponentState(omx_vid_dec,OMX_StateExecuting)) {
1214 Log::getInstance()->log("Video", Log::DEBUG, "omx_vid_dec ChangeComponentState Execute");
1215 clock_mutex.Unlock();
1216 DeAllocateCodecsOMX();
1220 error=OMX_SetupTunnel(omx_vid_sched,omx_shed_output_port,omx_vid_rend,omx_rend_input_port);
1221 if (error!=OMX_ErrorNone){
1222 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel sched to rend failed %x", error);
1223 clock_mutex.Unlock();
1224 DeAllocateCodecsOMX();
1228 if (!EnablePort(omx_vid_sched,omx_shed_output_port,false) || !EnablePort(omx_vid_rend,omx_rend_input_port,false)
1230 Log::getInstance()->log("Video", Log::DEBUG, "Enable Ports OMX shed rend failed");
1231 clock_mutex.Unlock();
1232 DeAllocateCodecsOMX();
1236 if (!CommandFinished(omx_vid_sched,OMX_CommandPortEnable,omx_shed_output_port)
1237 || !CommandFinished(omx_vid_rend,OMX_CommandPortEnable,omx_rend_input_port)) {
1238 clock_mutex.Unlock();
1239 DeAllocateCodecsOMX();
1243 if (!ChangeComponentState(omx_vid_rend,OMX_StateIdle)) {
1244 Log::getInstance()->log("Video", Log::DEBUG, "vid_rend ChangeComponentState");
1245 clock_mutex.Unlock();
1246 DeAllocateCodecsOMX();
1251 if (!ChangeComponentState(omx_vid_deint,OMX_StateExecuting)) {
1252 Log::getInstance()->log("Video", Log::DEBUG, "vid_vid_deint ChangeComponentState");
1253 clock_mutex.Unlock();
1254 DeAllocateCodecsOMX();
1257 DisablePort(omx_vid_deint,omx_deint_output_port,false);
1258 DisablePort(omx_vid_deint,omx_deint_input_port,false);
1261 if (!ChangeComponentState(omx_vid_sched,OMX_StateExecuting)) {
1262 Log::getInstance()->log("Video", Log::DEBUG, "omx_vid_sched ChangeComponentState Execute");
1263 clock_mutex.Unlock();
1264 DeAllocateCodecsOMX();
1268 if (!ChangeComponentState(omx_vid_rend,OMX_StateExecuting)) {
1269 Log::getInstance()->log("Video", Log::DEBUG, "omx_vid_rend ChangeComponentState Execute");
1270 clock_mutex.Unlock();
1271 DeAllocateCodecsOMX();
1276 /*OMX_CONFIG_DISPLAYREGIONTYPE dispconf;
1277 memset(&dispconf,0,sizeof(dispconf));
1278 dispconf.nSize=sizeof(dispconf);
1279 dispconf.nVersion.nVersion=OMX_VERSION;
1281 dispconf.nPortIndex=omx_rend_input_port;
1283 dispconf.set=OMX_DISPLAY_SET_LAYER ;
1285 error=OMX_SetConfig(omx_vid_rend,OMX_IndexConfigDisplayRegion,&dispconf);
1286 if (error!=OMX_ErrorNone){
1287 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX_IndexConfigDisplayRegion failed %x", error);
1288 clock_mutex.Unlock();
1289 DeAllocateCodecsOMX();
1293 /* dispconf.set=OMX_DISPLAY_SET_FULLSCREEN ;
1294 dispconf.fullscreen=OMX_FALSE;
1295 error=OMX_SetParameter(omx_vid_rend,OMX_IndexConfigDisplayRegion,&dispconf);
1296 if (error!=OMX_ErrorNone){
1297 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX_IndexConfigDisplayRegion failed %x", error);
1298 clock_mutex.Unlock();
1299 DeAllocateCodecsOMX();
1303 dispconf.set=OMX_DISPLAY_SET_DEST_RECT;
1304 dispconf.dest_rect.x_offset=100;
1305 dispconf.dest_rect.y_offset=100;
1306 dispconf.dest_rect.width=640;
1307 dispconf.dest_rect.height=480;
1308 error=OMX_SetParameter(omx_vid_rend,OMX_IndexConfigDisplayRegion,&dispconf);
1309 if (error!=OMX_ErrorNone){
1310 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX_IndexConfigDisplayRegion failed %x", error);
1311 clock_mutex.Unlock();
1312 DeAllocateCodecsOMX();
1317 playbacktimeoffset=-GetCurrentSystemTime();
1321 clock_mutex.Unlock();
1325 setClockExecutingandRunning();
1334 int VideoOMX::idleClock()
1336 OMX_ERRORTYPE error;
1337 OMX_STATETYPE temp_state;
1339 OMX_GetState(omx_clock,&temp_state);
1341 if (temp_state!=OMX_StateIdle) {
1342 if (!ChangeComponentState(omx_clock,OMX_StateIdle)) {
1343 Log::getInstance()->log("Video", Log::DEBUG, "omx_clock ChangeComponentState Idle failed");
1344 clock_mutex.Unlock();
1348 clock_mutex.Unlock();
1352 int VideoOMX::setClockExecutingandRunning()
1354 OMX_ERRORTYPE error;
1355 OMX_STATETYPE temp_state;
1357 OMX_GetState(omx_clock,&temp_state);
1359 if (temp_state!=OMX_StateExecuting) {
1360 if (!ChangeComponentState(omx_clock,OMX_StateExecuting)) {
1361 Log::getInstance()->log("Video", Log::DEBUG, "omx_clock ChangeComponentState Execute failed");
1362 clock_mutex.Unlock();
1363 DeAllocateCodecsOMX();
1368 OMX_TIME_CONFIG_CLOCKSTATETYPE clock_conf;
1369 memset(&clock_conf,0,sizeof(clock_conf));
1370 clock_conf.nSize=sizeof(clock_conf);
1371 clock_conf.nVersion.nVersion=OMX_VERSION;
1372 clock_conf.eState=OMX_TIME_ClockStateRunning;
1373 error=OMX_SetConfig(omx_clock,OMX_IndexConfigTimeClockState,&clock_conf);
1374 if (error!=OMX_ErrorNone) {
1375 Log::getInstance()->log("Video", Log::DEBUG, "Clock IndexConfigTimeClockState failed %x", error);
1376 clock_mutex.Unlock();
1377 DeAllocateCodecsOMX();
1380 clock_mutex.Unlock();
1386 int VideoOMX::ChangeComponentState(OMX_HANDLETYPE handle,OMX_STATETYPE type) //needs to be called with locked mutex
1388 OMX_ERRORTYPE error;
1389 error=OMX_SendCommand(handle,OMX_CommandStateSet,type,0);
1390 if (error!=OMX_ErrorNone){
1391 Log::getInstance()->log("Video", Log::DEBUG, "handle %x Send Command to OMX State %x %x",handle,type, error);
1395 if (!CommandFinished(handle,OMX_CommandStateSet,type)) {
1403 int VideoOMX::EnablePort(OMX_HANDLETYPE handle,OMX_U32 port,bool wait) //needs to be called with locked mutex
1405 OMX_ERRORTYPE error;
1406 error=OMX_SendCommand(handle,OMX_CommandPortEnable,port,0);
1407 if (error!=OMX_ErrorNone){
1408 Log::getInstance()->log("Video", Log::DEBUG, "handle %x Send Command to enable port %x %x",handle,port, error);
1412 if (!wait) return 1;
1413 if (!CommandFinished(handle,OMX_CommandPortEnable,port)) {
1421 int VideoOMX::DisablePort(OMX_HANDLETYPE handle,OMX_U32 port,bool wait) //needs to be called with locked mutex
1423 OMX_ERRORTYPE error;
1424 error=OMX_SendCommand(handle,OMX_CommandPortDisable,port,0);
1425 if (error!=OMX_ErrorNone){
1426 Log::getInstance()->log("Video", Log::DEBUG, "handle %x Send Command to disable port %x %x",handle,port, error);
1430 if (!wait) return 1;
1431 if (!CommandFinished(handle,OMX_CommandPortDisable,port)) {
1439 int VideoOMX::WaitForEvent(OMX_HANDLETYPE handle,OMX_U32 event) //needs to be called with locked mutex
1443 omx_event_mutex.Lock();
1444 list<VPE_OMX_EVENT>::iterator itty=omx_events.begin();
1445 while (itty!=omx_events.end()) {
1447 VPE_OMX_EVENT current=*itty;
1448 if (current.handle==handle) { //this is ours
1449 if (current.event_type==OMX_EventError) {
1450 omx_events.erase(itty);
1451 omx_event_mutex.Unlock();
1452 Log::getInstance()->log("Video", Log::DEBUG, "WaitForEvent Finished on Error");
1455 } else if (current.event_type==event) {
1456 omx_events.erase(itty);
1457 omx_event_mutex.Unlock();
1458 Log::getInstance()->log("Video", Log::DEBUG, "WaitForEvent Finished Completed");
1465 omx_event_mutex.Unlock();
1470 Log::getInstance()->log("Video", Log::DEBUG, "WaitForEvent waited too long %x %x",handle,event);
1476 int VideoOMX::CommandFinished(OMX_HANDLETYPE handle,OMX_U32 command,OMX_U32 data2) //needs to be called with locked mutex
1480 omx_event_mutex.Lock();
1481 list<VPE_OMX_EVENT>::iterator itty=omx_events.begin();
1482 while (itty!=omx_events.end()) {
1484 VPE_OMX_EVENT current=*itty;
1485 if (current.handle==handle) { //this is ours
1486 if (current.event_type==OMX_EventError) {
1487 omx_events.erase(itty);
1488 omx_event_mutex.Unlock();
1489 Log::getInstance()->log("Video", Log::DEBUG, "Command Finished on Error");
1492 } else if (current.event_type==OMX_EventCmdComplete && current.data1==command && current.data2==data2) {
1493 omx_events.erase(itty);
1494 omx_event_mutex.Unlock();
1495 //Log::getInstance()->log("Video", Log::DEBUG, "Command Finished Completed");
1502 omx_event_mutex.Unlock();
1507 Log::getInstance()->log("Video", Log::DEBUG, "CommandFinished waited too long %x %x %x",handle,command, data2);
1516 int VideoOMX::PrepareInputBufsOMX() //needs to be called with locked mutex
1518 OMX_ERRORTYPE error;
1519 OMX_PARAM_PORTDEFINITIONTYPE port_def_type;
1520 memset(&port_def_type,0,sizeof(port_def_type));
1521 port_def_type.nSize=sizeof(port_def_type);
1522 port_def_type.nVersion.nVersion=OMX_VERSION;
1523 port_def_type.nPortIndex=omx_codec_input_port;
1525 error=OMX_GetParameter(omx_vid_dec,OMX_IndexParamPortDefinition, &port_def_type);
1527 if (error!=OMX_ErrorNone){
1528 Log::getInstance()->log("Video", Log::DEBUG, "Get OMX OMX_IndexParamPortDefinition failed %x", error);
1530 /* Log::getInstance()->log("Video", Log::DEBUG, "Port para %d %d %d %d %d %d %d", port_def_type.nBufferCountActual,
1531 port_def_type.nBufferCountMin,port_def_type.nBufferSize,port_def_type.bEnabled,port_def_type.bPopulated,
1532 port_def_type.bBuffersContiguous,port_def_type.nBufferAlignment);*/
1534 port_def_type.nBufferCountActual=100;
1535 port_def_type.nBufferSize=max(port_def_type.nBufferSize,200000); // for transcoder important
1537 error=OMX_SetParameter(omx_vid_dec,OMX_IndexParamPortDefinition, &port_def_type);
1539 if (error!=OMX_ErrorNone){
1540 Log::getInstance()->log("Video", Log::DEBUG, "Set OMX OMX_IndexParamPortDefinition failed %x", error);
1544 error=OMX_SendCommand(omx_vid_dec,OMX_CommandPortEnable,omx_codec_input_port,0);
1545 if (error!=OMX_ErrorNone){
1546 Log::getInstance()->log("Video", Log::DEBUG, "Prepare Input bufs Send Command to enable port %x", error);
1550 input_bufs_omx_mutex.Lock();
1551 for (unsigned int i=0; i< port_def_type.nBufferCountActual;i++) {
1553 // unsigned char* new_buffer_data=(unsigned char*)malloc(port_def_type.nbufferSize);
1554 OMX_BUFFERHEADERTYPE *buf_head=NULL;
1555 /* error=OMX_Usebuffer(omx_vid_dec,&buf_head,omx_codec_input_port,NULL,port_def_type.nbufferSize,new_buffer_data);
1556 if (error!=OMX_ErrorNone){
1557 Log::getInstance()->log("Video", Log::DEBUG, "Use OMX_Usebuffer failed %x", error);
1558 input_bufs_omx_mutex.Unlock();
1561 error=OMX_AllocateBuffer(omx_vid_dec,&buf_head,omx_codec_input_port,NULL,port_def_type.nBufferSize);
1562 if (error!=OMX_ErrorNone){
1563 Log::getInstance()->log("Video", Log::DEBUG, "Use OMX_AllocateBuffer failed %x", error);
1564 input_bufs_omx_mutex.Unlock();
1567 input_bufs_omx_all.push_back(buf_head);
1568 input_bufs_omx_free.push_back(buf_head);
1570 omx_first_frame=true;
1573 cur_input_buf_omx=NULL;
1574 input_bufs_omx_mutex.Unlock();
1577 Log::getInstance()->log("Video", Log::DEBUG, "PrepareInputBufsOMX mark3");
1578 if (!CommandFinished(omx_vid_dec,OMX_CommandPortEnable,omx_codec_input_port)) {
1581 Log::getInstance()->log("Video", Log::DEBUG, "PrepareInputBufsOMX mark4");
1586 int VideoOMX::DestroyInputBufsOMX() //need s to be called with locked mutex
1588 OMX_ERRORTYPE error;
1590 cur_input_buf_omx=NULL;
1591 input_bufs_omx_mutex.Lock();
1592 for (int i=0; i< input_bufs_omx_all.size();i++) {
1593 // free(input_bufs_omx_all[i]->pBuffer);
1594 // input_bufs_omx_all[i]->pBuffer=NULL;
1595 error=OMX_FreeBuffer(omx_vid_dec,omx_codec_input_port,input_bufs_omx_all[i]);
1596 if (error!=OMX_ErrorNone){
1597 Log::getInstance()->log("Video", Log::DEBUG, "Use OMX_FreeBuffer failed %x", error);
1598 input_bufs_omx_mutex.Unlock();
1603 input_bufs_omx_all.clear();
1604 input_bufs_omx_free.clear();
1605 input_bufs_omx_present.clear();
1606 input_time_present.clear();
1607 input_time_pts.clear();
1608 input_is_last.clear();
1609 input_bufs_omx_mutex.Unlock();
1616 int VideoOMX::DeAllocateCodecsOMX()
1618 OMX_ERRORTYPE error;
1620 Log::getInstance()->log("Video", Log::DEBUG, "enter deallocatecodecsomx");
1624 if (cur_input_buf_omx) {
1625 cur_input_buf_omx->nFlags|=OMX_BUFFERFLAG_EOS;
1626 OMX_ERRORTYPE error=ProtOMXEmptyThisBuffer(omx_vid_dec,cur_input_buf_omx);
1627 if (error!=OMX_ErrorNone) {
1628 Log::getInstance()->log("Video", Log::DEBUG, "OMX_EmptyThisBuffer failed %x", error);
1631 cur_input_buf_omx=NULL;//write out old data
1635 // first stop the omx elements
1636 if (!ChangeComponentState(omx_vid_dec,OMX_StateIdle)) {
1637 Log::getInstance()->log("Video", Log::DEBUG, "vid_dec ChangeComponentState");
1640 clock_mutex.Unlock();
1646 if (!ChangeComponentState(omx_vid_deint, OMX_StateIdle)) {
1647 Log::getInstance()->log("Video", Log::DEBUG,
1648 "vid_deint ChangeComponentState");
1654 if (!ChangeComponentState(omx_vid_sched,OMX_StateIdle)) {
1655 Log::getInstance()->log("Video", Log::DEBUG, "vid_shed ChangeComponentState");
1659 if (!ChangeComponentState(omx_vid_rend,OMX_StateIdle)) {
1660 Log::getInstance()->log("Video", Log::DEBUG, "vid_rend ChangeComponentState");
1666 // TODO proper deinit sequence
1667 // first flush all buffers
1671 error = OMX_SendCommand(omx_vid_dec, OMX_CommandFlush,
1672 omx_codec_output_port, NULL);
1673 if (error != OMX_ErrorNone) {
1674 Log::getInstance()->log("Video", Log::DEBUG,
1675 "OMX_Flush codec out failed %x", error);
1679 error = OMX_SendCommand(omx_vid_sched, OMX_CommandFlush,
1680 omx_shed_input_port, NULL);
1681 if (error != OMX_ErrorNone) {
1682 Log::getInstance()->log("Video", Log::DEBUG,
1683 "OMX_Flush shed in failed %x", error);
1687 if (!CommandFinished(omx_vid_dec, OMX_CommandFlush,
1688 omx_codec_output_port)) {
1689 Log::getInstance()->log("Video", Log::DEBUG,
1690 "flush cmd codec failed");
1693 if (!CommandFinished(omx_vid_sched, OMX_CommandFlush,
1694 omx_shed_input_port)) {
1695 Log::getInstance()->log("Video", Log::DEBUG,
1696 "flush cmd shed failed");
1699 error = OMX_SendCommand(omx_vid_dec, OMX_CommandFlush,
1700 omx_codec_output_port, NULL);
1701 if (error != OMX_ErrorNone) {
1702 Log::getInstance()->log("Video", Log::DEBUG,
1703 "OMX_Flush codec out failed %x", error);
1707 error = OMX_SendCommand(omx_vid_deint, OMX_CommandFlush,
1708 omx_deint_input_port, NULL);
1709 if (error != OMX_ErrorNone) {
1710 Log::getInstance()->log("Video", Log::DEBUG,
1711 "OMX_Flush deint in failed %x", error);
1715 if (!CommandFinished(omx_vid_dec, OMX_CommandFlush,
1716 omx_codec_output_port)) {
1717 Log::getInstance()->log("Video", Log::DEBUG,
1718 "flush cmd codec failed");
1721 if (!CommandFinished(omx_vid_deint, OMX_CommandFlush,
1722 omx_deint_input_port)) {
1723 Log::getInstance()->log("Video", Log::DEBUG,
1724 "flush cmd deint failed");
1728 error = OMX_SendCommand(omx_vid_deint, OMX_CommandFlush,
1729 omx_deint_output_port, NULL);
1730 if (error != OMX_ErrorNone) {
1731 Log::getInstance()->log("Video", Log::DEBUG,
1732 "OMX_Flush deint out failed %x", error);
1736 error = OMX_SendCommand(omx_vid_sched, OMX_CommandFlush,
1737 omx_shed_input_port, NULL);
1738 if (error != OMX_ErrorNone) {
1739 Log::getInstance()->log("Video", Log::DEBUG,
1740 "OMX_Flush shed in failed %x", error);
1744 if (!CommandFinished(omx_vid_deint, OMX_CommandFlush,
1745 omx_deint_output_port)) {
1746 Log::getInstance()->log("Video", Log::DEBUG,
1747 "flush cmd deint failed");
1750 if (!CommandFinished(omx_vid_sched, OMX_CommandFlush,
1751 omx_shed_input_port)) {
1752 Log::getInstance()->log("Video", Log::DEBUG,
1753 "flush cmd shed failed");
1763 error = OMX_SendCommand(omx_vid_rend, OMX_CommandFlush,
1764 omx_rend_input_port, NULL);
1765 if (error != OMX_ErrorNone) {
1766 Log::getInstance()->log("Video", Log::DEBUG,
1767 "OMX_Flush rend in failed %x", error);
1771 error = OMX_SendCommand(omx_vid_sched, OMX_CommandFlush,
1772 omx_shed_output_port, NULL);
1773 if (error != OMX_ErrorNone) {
1774 Log::getInstance()->log("Video", Log::DEBUG,
1775 "OMX_Flush shed out failed %x", error);
1781 if (!CommandFinished(omx_vid_rend, OMX_CommandFlush,
1782 omx_rend_input_port)) {
1783 Log::getInstance()->log("Video", Log::DEBUG,
1784 "flush cmd shed rend failed");
1787 if (!CommandFinished(omx_vid_sched, OMX_CommandFlush,
1788 omx_shed_output_port)) {
1789 Log::getInstance()->log("Video", Log::DEBUG,
1790 "flush cmd shed rend failed");
1797 error=OMX_SendCommand(omx_clock,OMX_CommandFlush, omx_clock_output_port, NULL);
1798 if (error!=OMX_ErrorNone){
1799 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Flush clock out failed %x", error);
1803 error=OMX_SendCommand(omx_vid_sched,OMX_CommandFlush, omx_shed_clock_port, NULL);
1804 if (error!=OMX_ErrorNone){
1805 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Flush shed clock failed %x", error);
1809 if (!CommandFinished(omx_clock,OMX_CommandFlush,omx_clock_output_port) ||
1810 !CommandFinished(omx_vid_sched,OMX_CommandFlush,omx_shed_clock_port)) {
1811 Log::getInstance()->log("Video", Log::DEBUG, "flush cmd clock shed failed");
1817 error = OMX_SendCommand(omx_vid_dec, OMX_CommandFlush,
1818 omx_codec_input_port, NULL);
1819 if (error != OMX_ErrorNone) {
1820 Log::getInstance()->log("Video", Log::DEBUG,
1821 "OMX_Flush codec out failed %x", error);
1828 DestroyInputBufsOMX();
1831 if (!DisablePort(omx_vid_sched,omx_shed_output_port,true)) {
1832 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 2 ");
1834 if (!DisablePort(omx_vid_rend,omx_rend_input_port,true)) {
1835 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 1");
1841 if (!DisablePort(omx_vid_dec,omx_codec_output_port,true)) {
1842 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 6");
1847 if (!DisablePort(omx_vid_dec,omx_codec_input_port,true)) {
1848 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 7");
1852 if (!DisablePort(omx_vid_deint,omx_deint_output_port,true)) {
1853 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 6a");
1858 if (!DisablePort(omx_vid_deint,omx_deint_input_port,true)) {
1859 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 7a");
1865 if (!DisablePort(omx_vid_sched,omx_shed_input_port,true)) {
1866 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 3");
1869 if (!DisablePort(omx_vid_sched,omx_shed_clock_port,true)) {
1870 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 4");
1873 if (!DisablePort(omx_clock,omx_clock_output_port,true)) {
1874 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 5");
1880 error=OMX_SetupTunnel(omx_vid_dec,omx_codec_output_port,NULL,NULL);
1881 if (error!=OMX_ErrorNone) {
1882 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1887 error=OMX_SetupTunnel(omx_vid_deint,omx_deint_input_port,NULL,NULL);
1888 if (error!=OMX_ErrorNone) {
1889 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1893 error=OMX_SetupTunnel(omx_vid_deint,omx_deint_output_port,NULL,NULL);
1894 if (error!=OMX_ErrorNone) {
1895 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1899 error=OMX_SetupTunnel(omx_vid_sched,omx_shed_input_port,NULL,NULL);
1900 if (error!=OMX_ErrorNone) {
1901 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1906 error=OMX_SetupTunnel(omx_vid_sched,omx_shed_output_port,NULL,NULL);
1907 if (error!=OMX_ErrorNone) {
1908 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1912 error=OMX_SetupTunnel(omx_vid_rend,omx_rend_input_port,NULL,NULL);
1913 if (error!=OMX_ErrorNone) {
1914 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1919 error=OMX_SetupTunnel(omx_clock,omx_clock_output_port,NULL,NULL);
1920 if (error!=OMX_ErrorNone) {
1921 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1925 error=OMX_SetupTunnel(omx_vid_sched,omx_shed_clock_port,NULL,NULL);
1926 if (error!=OMX_ErrorNone) {
1927 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1934 error=OMX_FreeHandle(omx_vid_dec);
1935 error=OMX_FreeHandle(omx_vid_sched);
1936 error=OMX_FreeHandle(omx_vid_rend);
1937 if (dodeint) error=OMX_FreeHandle(omx_vid_deint);
1939 clock_mutex.Unlock();
1941 if (error!=OMX_ErrorNone) {
1942 Log::getInstance()->log("Video", Log::DEBUG, "FreeHandle failed %d", error);
1944 } else clock_mutex.Unlock();
1945 Log::getInstance()->log("Video", Log::DEBUG, "leave deallocate codecs OMX");
1951 void VideoOMX::destroyClock()
1954 if (clock_references>0) {
1956 if (clock_references==0) {
1957 OMX_ERRORTYPE error;
1958 Log::getInstance()->log("Video", Log::DEBUG, "destroy omx clock");
1959 error=OMX_FreeHandle(omx_clock);
1960 if (error!=OMX_ErrorNone) {
1961 Log::getInstance()->log("Video", Log::DEBUG, "FreeHandle failed %d", error);
1966 clock_mutex.Unlock();
1970 int VideoOMX::stop()
1972 if (!initted) return 0;
1975 //Check if libav mode
1976 DeAllocateCodecsOMX();
1981 // if (ioctl(fdVideo, AV_SET_VID_STOP, 0) != 0) return 0;
1985 int VideoOMX::reset()
1987 if (!initted) return 0;
1990 DeAllocateCodecsOMX();
1991 // if (ioctl(fdVideo, AV_SET_VID_RESET, 0x11) != 0) return 0;
1995 int VideoOMX::pause()
1997 if (!initted) return 0;
1998 Log::getInstance()->log("Video", Log::DEBUG, "enter pause");
2001 //maybe also change omx clock?
2002 pausetimecode=GetCurrentSystemTime();
2008 int VideoOMX::unPause() // FIXME get rid - same as play!! Not here!
2010 if (!initted) return 0;
2011 Log::getInstance()->log("Video", Log::DEBUG, "enter unpause");
2014 playbacktimeoffset+=GetCurrentSystemTime()-pausetimecode;
2015 paused=false; // may be also change omx clock
2021 int VideoOMX::fastForward()
2023 if (!initted) return 0;
2025 // if (ioctl(fdVideo, AV_SET_VID_libavWD, 1) != 0) return 0;
2029 int VideoOMX::unFastForward()
2031 if (!initted) return 0;
2033 // if (ioctl(fdVideo, AV_SET_VID_RESET, 0x11) != 0) return 0; // don't need this.
2035 //// if (ioctl(fdVideo, AV_SET_VID_PLAY, 0) != 0) return 0;
2039 int VideoOMX::attachFrameBuffer()
2041 if (!initted) return 0;
2043 // if (ioctl(fdVideo, AV_SET_VID_FB, 0) != 0) return 0;
2047 int VideoOMX::blank(void)
2049 // if (ioctl(fdVideo, AV_SET_VID_FB, 1) != 0) return 0;
2050 // if (ioctl(fdVideo, AV_SET_VID_FB, 0) != 0) return 0;
2054 ULLONG VideoOMX::getCurrentTimestamp()
2056 if (iframemode) return 0;
2062 ULONG VideoOMX::timecodeToFrameNumber(ULLONG timecode)
2064 if (format == PAL) return (ULONG)(((double)timecode / (double)90000) * (double)25);
2065 else return (ULONG)(((double)timecode / (double)90000) * (double)30);
2070 int VideoOMX::test()
2075 // return ioctl(fdVideo, AV_SET_VID_STC, &stc);
2082 int VideoOMX::test2()
2090 long long VideoOMX::SetStartOffset(long long curreftime, bool *rsync)
2094 startoffset=curreftime;//offset is set for audio
2096 offsetvideonotset=false;
2098 if (offsetvideonotset) {
2099 offsetvideonotset=false;
2102 if ( (curreftime-lastrefvideotime)>10000000LL
2103 || (curreftime-lastrefvideotime)<-10000000LL) {//if pts jumps to big resync
2104 startoffset+=curreftime-lastrefvideotime;
2105 lastrefaudiotime+=curreftime-lastrefvideotime;
2107 offsetaudionotset=true;
2114 lastrefvideotime=curreftime;
2120 long long VideoOMX::SetStartAudioOffset(long long curreftime, bool *rsync)
2124 startoffset=curreftime;
2126 offsetaudionotset=false;
2128 if (offsetaudionotset) {
2129 offsetaudionotset=false;
2132 if ( (curreftime-lastrefaudiotime)>10000000LL
2133 || (curreftime-lastrefaudiotime)<-10000000LL) {//if pts jumps to big resync
2134 startoffset+=curreftime-lastrefaudiotime;
2135 lastrefvideotime+=curreftime-lastrefaudiotime;
2137 offsetvideonotset=true;
2143 lastrefaudiotime=curreftime;
2148 void VideoOMX::ResetTimeOffsets() {
2149 offsetnotset=true; //called from demuxer
2150 offsetvideonotset=true;
2151 offsetaudionotset=true;
2160 long long VideoOMX::GetCurrentSystemTime()
2163 clock_gettime(CLOCK_MONOTONIC, &ts);
2164 return ts.tv_sec*10000000LL+ts.tv_nsec/100LL;
2167 void VideoOMX::WaitUntil(long long time)
2169 struct timespec interval;
2170 interval.tv_sec=time/10000000LL;
2171 interval.tv_nsec=(time %10000000LL)*100LL;
2172 while (clock_nanosleep(CLOCK_MONOTONIC,TIMER_ABSTIME,&interval,NULL)==EINTR) {
2173 //Log::getInstance()->log("Video", Log::DEBUG, "Wait until multi");
2178 bool VideoOMX::FrameSkip(long long pts)
2180 //ok first calculate the absolute time
2182 long long target_time=pts-playbacktimeoffset;
2183 // we have to wait untile the next frame
2184 long long offset=Demuxer::getInstance()->getFrameRate();
2185 if (offset==0) offset=25;
2186 offset=-2*10000000LL/offset;
2187 target_time+=offset;
2188 long long current_time=GetCurrentSystemTime();
2190 if ((target_time-current_time)<-400000LL) {
2191 skip=true; // we are too slow
2193 /* Log::getInstance()->log("Video", Log::DEBUG,
2194 "Skipping frames1 %lld %lld %d",target_time-current_time,pts,Demuxer::getInstance()->getFrameRate());
2195 Log::getInstance()->log("Video", Log::DEBUG, "skip detail pts: %lld target: %lld sys: %lld off: %lld diff %lld",pts,target_time,current_time,offset,
2196 target_time-current_time);*/
2201 if ((target_time - current_time) < 0000LL) { //skip a bit more
2202 skip = true; // we are too slow
2204 /* Log::getInstance()->log("Video", Log::DEBUG,"Skipping frames2 %lld %lld %d",target_time-current_time,pts,Demuxer::getInstance()->getFrameRate());
2205 Log::getInstance()->log("Video", Log::DEBUG, "skip detail pts: %lld target: %lld sys: %lld off: %lld diff %lld",pts,target_time,current_time,offset,
2206 target_time-current_time);*/
2216 void VideoOMX::FrameWaitforDisplay(long long pts)
2218 //ok first calculate the absolute time
2219 long long target_time=pts-playbacktimeoffset;
2220 // we have to wait untile the next frame
2221 long long offset=Demuxer::getInstance()->getFrameRate();
2222 long long current_time=GetCurrentSystemTime();
2223 if (offset==0) offset=25;
2224 offset=-3*10000000LL/offset;
2225 target_time+=offset;
2226 //Log::getInstance()->log("Video", Log::DEBUG, "Wait for display pts: %lld tg: %lld sys: %lld off: %lld diff %lld po: %lld",pts,target_time,current_time,offset,
2227 // target_time-current_time,playbacktimeoffset);
2228 if ((target_time-current_time)>1000000LL) target_time=current_time+1000000LL; // something is wrong do not wait too long
2229 //Log::getInstance()->log("Video", Log::DEBUG, "Wait for display pts: %lld tg: %lld sys: %lld off: %lld diff %lld po: %lld",pts,target_time,current_time,offset,
2230 // target_time-current_time,playbacktimeoffset);
2232 WaitUntil(target_time);
2233 //Log::getInstance()->log("Video", Log::DEBUG, "Wait for display out %lld",GetCurrentSystemTime());
2236 void VideoOMX::AdjustAudioPTS(long long pts)
2238 long long newplaybacktimeoffset=pts-GetCurrentSystemTime();
2239 if ((newplaybacktimeoffset-1000000LL)>playbacktimeoffset
2240 || (newplaybacktimeoffset+1000000LL)<playbacktimeoffset) {
2241 Log::getInstance()->log("Video", Log::DEBUG, "Adjust Playbackoffsettime o: %lld n: %lld",
2242 playbacktimeoffset,newplaybacktimeoffset);
2243 playbacktimeoffset=newplaybacktimeoffset;
2248 void VideoOMX::threadPostStopCleanup()
2252 Log::getInstance()->log("Video", Log::DEBUG,
2257 void VideoOMX::threadMethod()
2259 Log::getInstance()->log("Video", Log::DEBUG,
2263 OMX_BUFFERHEADERTYPE* pict=NULL;
2267 input_bufs_omx_mutex.Lock();
2268 if (input_bufs_omx_present.size()>0) {
2269 cur_pts=input_time_pts.front();
2270 pict=input_bufs_omx_present.front();
2271 time=input_time_present.front();
2272 islast=input_is_last.front();
2273 input_bufs_omx_present.pop_front();
2274 input_time_present.pop_front();
2275 input_time_pts.pop_front();
2276 input_is_last.pop_front();
2278 input_bufs_omx_mutex.Unlock();
2282 //Log::getInstance()->log("Video", Log::DEBUG,
2284 if (time!=0 && FrameSkip(time) && !(pict->nFlags &OMX_BUFFERFLAG_STARTTIME)) {
2286 input_bufs_omx_mutex.Lock();
2287 input_bufs_omx_free.push_back(pict);
2288 input_bufs_omx_mutex.Unlock();
2289 //Log::getInstance()->log("Video", Log::DEBUG, "threadMethod Frameskip");
2292 // Log::getInstance()->log("Video", Log::DEBUG,
2293 // "prot empty this buffer in");
2294 if (islast) FrameWaitforDisplay(time);
2295 OMX_ERRORTYPE error = ProtOMXEmptyThisBuffer(omx_vid_dec, pict);
2296 if (error != OMX_ErrorNone) {
2297 Log::getInstance()->log("Video", Log::DEBUG,
2298 "OMX_EmptyThisBuffer failed %x", error);
2300 //Log::getInstance()->log("Video", Log::DEBUG,
2301 // "time %lld",time);
2302 if (deint_first_frame && dodeint) DeinterlaceFix();
2311 Log::getInstance()->log("Video", Log::DEBUG,
2315 void VideoOMX::DeinterlaceFix()
2318 Demuxer* demux=Demuxer::getInstance();
2320 OMX_ERRORTYPE error;
2321 OMX_PARAM_PORTDEFINITIONTYPE port_def_type;
2322 memset(&port_def_type,0,sizeof(port_def_type));
2323 port_def_type.nSize=sizeof(port_def_type);
2324 port_def_type.nVersion.nVersion=OMX_VERSION;
2325 port_def_type.nPortIndex=omx_codec_output_port;
2327 error=OMX_GetParameter(omx_vid_dec,OMX_IndexParamPortDefinition, &port_def_type);
2328 if (error != OMX_ErrorNone) {
2329 Log::getInstance()->log("Video", Log::DEBUG,
2330 "OMX_IndexParamPortDefinition fix failed %x", error);
2331 clock_mutex.Unlock();
2335 if (port_def_type.format.video.nFrameWidth == demux->getHorizontalSize()
2336 && port_def_type.format.video.nFrameHeight == demux->getVerticalSize()){
2337 Log::getInstance()->log("Video", Log::DEBUG,
2338 "Deinit first frame fix");
2339 deint_first_frame=false;
2341 WaitForEvent(omx_vid_dec,OMX_EventPortSettingsChanged);
2342 DisablePort(omx_vid_dec,omx_codec_output_port,false);
2343 DisablePort(omx_vid_sched,omx_shed_input_port,false);
2344 DisablePort(omx_vid_deint,omx_deint_output_port,false);
2345 DisablePort(omx_vid_deint,omx_deint_input_port,false);
2347 port_def_type.nPortIndex=omx_deint_input_port;
2348 error = OMX_SetParameter(omx_vid_deint, OMX_IndexParamPortDefinition,
2350 if (error != OMX_ErrorNone) {
2351 Log::getInstance()->log("Video", Log::DEBUG,
2352 "Set OMX_IndexParamPortDefinition1 failed %x", error);
2353 clock_mutex.Unlock();
2357 port_def_type.nPortIndex=omx_deint_output_port;
2358 error = OMX_SetParameter(omx_vid_deint, OMX_IndexParamPortDefinition,
2360 if (error != OMX_ErrorNone) {
2361 Log::getInstance()->log("Video", Log::DEBUG,
2362 "Set OMX_IndexParamPortDefinition2 failed %x", error);
2363 clock_mutex.Unlock();
2368 EnablePort(omx_vid_dec,omx_codec_output_port,false);
2369 EnablePort(omx_vid_deint,omx_deint_input_port,false);
2370 EnablePort(omx_vid_deint,omx_deint_output_port,false);
2371 EnablePort(omx_vid_sched,omx_shed_input_port,false);
2373 clock_mutex.Unlock();
2379 void VideoOMX::PutBufferToPres(OMX_BUFFERHEADERTYPE* buffer, long long time, long long pts,bool islast)
2381 input_bufs_omx_mutex.Lock();
2382 input_bufs_omx_present.push_back(buffer);
2383 input_time_present.push_back(time);
2384 input_time_pts.push_back(pts);
2385 input_is_last.push_back(islast);
2386 input_bufs_omx_mutex.Unlock();
2390 OMX_ERRORTYPE VideoOMX::ProtOMXEmptyThisBuffer(OMX_HANDLETYPE handle, OMX_BUFFERHEADERTYPE* buffer)
2392 // protect the call to empty this buffer
2395 pthread_testcancel();
2396 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldcancelstate);
2397 pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldcanceltype);
2399 OMX_ERRORTYPE ret_val;
2400 ret_val=OMX_EmptyThisBuffer(handle,buffer);
2401 clock_mutex.Unlock();
2402 pthread_setcancelstate(oldcancelstate, NULL);
2403 pthread_setcanceltype(oldcanceltype, NULL);
2404 pthread_testcancel();
2408 void VideoOMX::PrepareMediaSample(const MediaPacketList& mplist,UINT samplepos)
2411 mediapackets.clear();
2412 list<MediaPacket>::const_iterator begin=mplist.begin();
2413 list<MediaPacket>::const_iterator itty=mplist.begin();
2414 advance(itty,min(mplist.size(),10));
2415 mediapackets.insert(mediapackets.begin(),begin,itty);//front
2419 UINT VideoOMX::DeliverMediaSample(UCHAR* buffer, UINT *samplepos)
2422 while (consumed<mediapackets.size()) {
2423 DeliverMediaPacket(mediapackets[consumed], buffer, samplepos);
2424 if (*samplepos == mediapackets[consumed].length) {
2428 } else return consumed;
2433 UINT VideoOMX::DeliverMediaPacket(MediaPacket packet,
2434 const UCHAR* buffer,
2437 if (packet.type == MPTYPE_VIDEO_H264)
2447 //Later add fail back code for libav
2449 *samplepos+=packet.length;
2450 return packet.length;
2454 if (!omx_running) return 0; // if we are not runnig do not do this
2455 if (paused) return 0; //Block if we pause
2457 if (packet.synched && FrameSkip(packet.presentation_time)) {
2458 *samplepos=packet.length;
2459 Log::getInstance()->log("Video", Log::DEBUG, "DeliverMediaPacketOMX Frameskip");
2460 *samplepos=packet.length;
2461 return packet.length;
2463 //long long current_media_time=GetCurrentSystemTime()+playbacktimeoffset;
2464 /* if (packet.synched &&
2465 (packet.presentation_time<0 /*|| // preroll skip frames
2466 (packet.presentation_time+5000000LL)<(current_media_time)*)) { // we are late skip
2467 Log::getInstance()->log("Video", Log::DEBUG, "DeliverMediaPacketOMX Preroll or too late %lld %lld; %lld", packet.presentation_time,current_media_time,playbacktimeoffset);
2468 *samplepos=packet.length;
2469 return packet.length;
2472 OMX_ERRORTYPE error;
2474 /*First Check, if we have an video sample*/
2478 return 0; //Not in iframe mode!
2482 if (packet.disconti) {
2484 if (cur_input_buf_omx) {
2485 PutBufferToPres(cur_input_buf_omx, lastreftimeOMX,lastreftimePTS, true);
2486 cur_input_buf_omx=NULL;
2490 /*Inspect PES-Header */
2492 // OMX_STATETYPE temp_state;
2493 // OMX_GetState(omx_vid_dec,&temp_state);
2495 if (*samplepos==0) {//stripheader
2496 headerstrip=buffer[packet.pos_buffer+8]+9/*is this right*/;
2497 // if (h264) Log::getInstance()->log("Video", Log::DEBUG, "PES info %x %x %x %x",
2498 // buffer[packet.pos_buffer+0],buffer[packet.pos_buffer+1],buffer[packet.pos_buffer+2],buffer[packet.pos_buffer+3]);
2499 *samplepos+=headerstrip;
2500 if ( packet.synched ) {
2501 if (cur_input_buf_omx) {
2502 cur_input_buf_omx->nFlags|=OMX_BUFFERFLAG_ENDOFFRAME;
2503 PutBufferToPres(cur_input_buf_omx, lastreftimeOMX,lastreftimePTS,true);
2504 cur_input_buf_omx=NULL;//write out old data
2510 if (!firstsynched) {//
2511 *samplepos=packet.length;//if we have not processed at least one
2512 return packet.length;//synched packet ignore it!
2517 if (!cur_input_buf_omx) {
2518 input_bufs_omx_mutex.Lock();
2519 if (input_bufs_omx_free.size()==0) {
2520 input_bufs_omx_mutex.Unlock();
2521 //Log::getInstance()->log("Video", Log::DEBUG, "Deliver MediaPacket no free sample");
2522 return 0; // we do not have a free media sample
2525 cur_input_buf_omx=input_bufs_omx_free.front();
2526 cur_input_buf_omx->nFilledLen=0;
2527 cur_input_buf_omx->nOffset=0;
2528 cur_input_buf_omx->nTimeStamp=0;
2529 input_bufs_omx_free.pop_front();
2530 input_bufs_omx_mutex.Unlock();
2536 if (cur_input_buf_omx->nFilledLen==0) {//will only be changed on first packet
2537 if (packet.synched) {
2538 // Log::getInstance()->log("Video", Log::DEBUG, "packet synched marker");
2540 //lastreftimePTS=packet.pts;
2541 if (omx_first_frame) { // TODO time
2542 cur_input_buf_omx->nFlags=OMX_BUFFERFLAG_STARTTIME;
2543 Log::getInstance()->log("Video", Log::DEBUG, "Starttime");
2544 omx_first_frame=false;
2546 //cur_input_buf_omx->nFlags=0;
2547 cur_input_buf_omx->nFlags|=OMX_BUFFERFLAG_TIME_UNKNOWN;
2549 lastreftimeOMX=packet.presentation_time;
2550 // Log::getInstance()->log("Video", Log::DEBUG, "Time code %lld pts %lld", lastreftimeOMX,packet.pts);
2551 lastreftimePTS=packet.pts;
2552 cur_input_buf_omx->nTimeStamp=0;//lastreftimeOMX; // the clock component is faulty;
2556 cur_input_buf_omx->nFlags=OMX_BUFFERFLAG_TIME_UNKNOWN;
2557 cur_input_buf_omx->nTimeStamp=0;
2558 //Log::getInstance()->log("Video", Log::DEBUG, "packet unsynched marker");
2559 // ms->SetSyncPoint(TRUE);
2561 if (packet.disconti) cur_input_buf_omx->nFlags|=OMX_BUFFERFLAG_DISCONTINUITY;
2566 unsigned int haveToCopy=packet.length-*samplepos;
2568 while (haveToCopy> (cur_input_buf_omx->nAllocLen-cur_input_buf_omx->nFilledLen)) {
2569 //Log::getInstance()->log("Video", Log::DEBUG, "Big buffer %d %d %d",packet.length,cur_input_buf_omx->nAllocLen,cur_input_buf_omx->nFilledLen);
2570 unsigned int cancopy=cur_input_buf_omx->nAllocLen-cur_input_buf_omx->nFilledLen;
2571 memcpy(cur_input_buf_omx->pBuffer+cur_input_buf_omx->nFilledLen,buffer+packet.pos_buffer+*samplepos,cancopy);
2572 haveToCopy-=cancopy;
2573 cur_input_buf_omx->nFilledLen+=cancopy;
2574 *samplepos+=cancopy;
2575 // push old buffer out
2577 PutBufferToPres(cur_input_buf_omx, lastreftimeOMX,lastreftimePTS,false);
2578 cur_input_buf_omx=NULL;
2580 input_bufs_omx_mutex.Lock();
2581 if (input_bufs_omx_free.size()==0) {
2582 input_bufs_omx_mutex.Unlock();
2583 // Log::getInstance()->log("Video", Log::DEBUG, "Deliver MediaPacket no free sample2");
2584 return *samplepos; // we do not have a free media sample
2586 cur_input_buf_omx=input_bufs_omx_free.front();
2587 cur_input_buf_omx->nFilledLen=0;
2588 cur_input_buf_omx->nOffset=0;
2589 cur_input_buf_omx->nTimeStamp=0;
2590 input_bufs_omx_free.pop_front();
2591 input_bufs_omx_mutex.Unlock();
2593 cur_input_buf_omx->nFlags=OMX_BUFFERFLAG_TIME_UNKNOWN;
2596 memcpy(cur_input_buf_omx->pBuffer+cur_input_buf_omx->nFilledLen,
2597 buffer+packet.pos_buffer+*samplepos,haveToCopy);
2598 cur_input_buf_omx->nFilledLen+=haveToCopy;
2602 *samplepos+=haveToCopy;
2611 bool VideoOMX::displayIFrame(const UCHAR* buffer, UINT length) {
2612 if (!omx_running) return false;
2614 EnterIframePlayback();
2616 int haveToCopy = length;
2618 if (!cur_input_buf_omx) {
2619 input_bufs_omx_mutex.Lock();
2620 if (input_bufs_omx_free.size() == 0) {
2621 input_bufs_omx_mutex.Unlock();
2622 // Log::getInstance()->log("Video", Log::DEBUG,
2623 // "Deliver MediaPacket no free sample");
2624 return false; // we do not have a free media sample
2627 cur_input_buf_omx = input_bufs_omx_free.front();
2628 cur_input_buf_omx->nFilledLen = 0;
2629 cur_input_buf_omx->nOffset = 0;
2630 cur_input_buf_omx->nTimeStamp = 0;
2631 input_bufs_omx_free.pop_front();
2632 input_bufs_omx_mutex.Unlock();
2636 unsigned int pattern, packet_length;
2637 unsigned int headerstrip = 0;
2642 //Now we strip the pes header
2643 pattern = (buffer[0] << 16) | (buffer[1] << 8) | (buffer[2]);
2644 while (read_pos + 7 <= length) {
2645 pattern = ((pattern << 8) & 0xFFFFFFFF) | buffer[read_pos + 3];
2646 if (pattern < 0x000001E0 || pattern > 0x000001EF) {
2650 headerstrip = buffer[read_pos + 8] + 9/*is this right*/;
2651 packet_length = ((buffer[read_pos + 4] << 8)
2652 | (buffer[read_pos + 5])) + 6;
2653 if (read_pos + packet_length > length)
2656 if ((headerstrip < packet_length)
2657 && (cur_input_buf_omx->nFilledLen + packet_length
2658 - headerstrip) > cur_input_buf_omx->nAllocLen) {
2660 cur_input_buf_omx->nFlags = OMX_BUFFERFLAG_STARTTIME;
2663 cur_input_buf_omx->nFlags
2664 |= OMX_BUFFERFLAG_TIME_UNKNOWN;
2667 cur_input_buf_omx->nTimeStamp = 0;
2668 PutBufferToPres(cur_input_buf_omx, 0,0,false);
2669 cur_input_buf_omx = NULL;
2671 if (!cur_input_buf_omx) {
2673 while (count < 100 && omx_running && iframemode) {
2676 input_bufs_omx_mutex.Lock();
2677 if (input_bufs_omx_free.size() == 0) {
2678 input_bufs_omx_mutex.Unlock();
2679 // Log::getInstance()->log("Video", Log::DEBUG,
2680 // "Ifrane no free sample");
2682 if (!omx_running) return false;
2685 cur_input_buf_omx = input_bufs_omx_free.front();
2686 cur_input_buf_omx->nFilledLen = 0;
2687 cur_input_buf_omx->nOffset = 0;
2688 cur_input_buf_omx->nTimeStamp = 0;
2689 input_bufs_omx_free.pop_front();
2690 input_bufs_omx_mutex.Unlock();
2693 if (!cur_input_buf_omx)
2698 if (packet_length > headerstrip) {
2700 cur_input_buf_omx->pBuffer
2701 + cur_input_buf_omx->nFilledLen,
2702 buffer + read_pos + headerstrip,
2703 packet_length - headerstrip);
2704 cur_input_buf_omx->nFilledLen += packet_length
2707 read_pos += packet_length;
2709 pattern = (buffer[read_pos] << 16)
2710 | (buffer[read_pos + 1] << 8) | (buffer[read_pos + 2]);
2716 cur_input_buf_omx->nFlags = OMX_BUFFERFLAG_STARTTIME;
2719 cur_input_buf_omx->nFlags |= OMX_BUFFERFLAG_TIME_UNKNOWN;
2722 cur_input_buf_omx->nTimeStamp = 0;
2724 PutBufferToPres(cur_input_buf_omx, 0,0,false);
2725 cur_input_buf_omx = NULL;
2728 MILLISLEEP(40); //Block a bit
2732 int VideoOMX::EnterIframePlayback()
2734 if (cur_input_buf_omx) {
2735 PutBufferToPres(cur_input_buf_omx, lastreftimeOMX, lastreftimePTS,true);
2736 cur_input_buf_omx = NULL;
2738 input_bufs_omx_mutex.Lock();
2739 while (input_bufs_omx_present.size()) {
2740 input_bufs_omx_free.push_back(input_bufs_omx_present.front());
2741 input_bufs_omx_present.pop_front();
2743 input_time_present.clear();
2744 input_time_pts.clear();
2745 input_is_last.clear();
2746 input_bufs_omx_mutex.Unlock();