2 Copyright 2004-2005 Chris Tallon, 2009-12 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"
36 //A lot of parts of this file are heavily inspired by xbmc omx implementations
38 VideoOMX::VideoOMX() {
43 cur_input_buf_omx = NULL;
44 omx_h264 = omx_mpeg2 = true;
48 offsetvideonotset = true;
49 offsetaudionotset = true;
61 deinterlace=2; //advanced
72 int VideoOMX::init(UCHAR tformat)
74 if (initted) return 0;
77 if (!setFormat(tformat)) { shutdown(); return 0; }
78 if (!setConnection(HDMI)) { shutdown(); return 0; }
79 if (!setAspectRatio(ASPECT4X3)) { shutdown(); return 0; }
80 if (!setMode(NORMAL)) { shutdown(); return 0; }
81 if (!setSource()) { shutdown(); return 0; }
82 if (!attachFrameBuffer()) { shutdown(); return 0; }
90 if (error != OMX_ErrorNone) {
91 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX failed %x",
110 OMX_ERRORTYPE VideoOMX::EventHandler_OMX(OMX_IN OMX_HANDLETYPE handle,OMX_IN OMX_PTR appdata,
111 OMX_IN OMX_EVENTTYPE event_type,OMX_IN OMX_U32 data1,
112 OMX_IN OMX_U32 data2,OMX_IN OMX_PTR event_data) {
114 Log::getInstance()->log("Video", Log::NOTICE, "eventHandler %x %x %x %x %x",handle,event_type,data1,data2,event_data);
116 struct VPE_OMX_EVENT new_event;
117 new_event.handle=handle;
118 new_event.appdata=appdata;
119 new_event.event_type=event_type;
120 new_event.data1=data1;
121 new_event.data2=data2;
122 new_event.event_data=event_data;
124 VideoOMX *video=(VideoOMX *)getInstance();
125 video->AddOmxEvent(new_event);
127 /* switch (event_type) {
128 case OMX_EventCmdComplete: {
133 return OMX_ErrorNone;
137 void VideoOMX::AddOmxEvent(VPE_OMX_EVENT new_event)
139 omx_event_mutex.Lock();
140 omx_events.push_back(new_event);
141 omx_event_mutex.Unlock();
145 OMX_ERRORTYPE VideoOMX::EmptyBufferDone_OMX(OMX_IN OMX_HANDLETYPE hcomp,OMX_IN OMX_PTR appdata,OMX_IN OMX_BUFFERHEADERTYPE* buffer){
147 //Log::getInstance()->log("Video", Log::NOTICE, "EmptyBufferDone");
148 VideoOMX *video=(VideoOMX *)getInstance();
149 /* long long temp =buffer->nTimeStamp.nLowPart
150 | ((long long) buffer->nTimeStamp.nHighPart << 32);
151 Log::getInstance()->log("Video", Log::NOTICE, "EBD Video %lld %x",temp,buffer->nFlags);*/
152 video->ReturnEmptyOMXBuffer(buffer);
153 return OMX_ErrorNone;
157 void VideoOMX::ReturnEmptyOMXBuffer(OMX_BUFFERHEADERTYPE* buffer){
158 input_bufs_omx_mutex.Lock();
159 //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());
160 input_bufs_omx_free.push_back(buffer);
161 //Log::getInstance()->log("Video", Log::NOTICE, "ReturnEmptyOMXBuffer %d",input_bufs_omx_free.size());
162 input_bufs_omx_mutex.Unlock();
165 OMX_ERRORTYPE VideoOMX::FillBufferDone_OMX(OMX_IN OMX_HANDLETYPE hcomp, OMX_IN OMX_PTR appdata,OMX_IN OMX_BUFFERHEADERTYPE* buffer) {
166 Log::getInstance()->log("Video", Log::NOTICE, "FillBufferDone");
167 return OMX_ErrorNone;
172 int VideoOMX::shutdown()
174 if (!initted) return 0;
176 Log::getInstance()->log("Video", Log::NOTICE, "Shutdown video module");
178 DeAllocateCodecsOMX();
180 //vc_tv_show_info(0); // back to console
183 struct fb_var_screeninfo screeninfo;
184 fd_fbset=open("/dev/fb0",O_RDONLY);
186 Log::getInstance()->log("Video", Log::CRIT, "Could not open frame buffer device %d", fd_fbset);
189 if (ioctl(fd_fbset, FBIOGET_VSCREENINFO, &screeninfo)){
191 Log::getInstance()->log("Video", Log::CRIT, "Could not FBIOGET_VSCREENINFO frame buffer device");
194 screeninfo.bits_per_pixel=8;
195 if (ioctl(fd_fbset, FBIOPUT_VSCREENINFO, &screeninfo)){
196 Log::getInstance()->log("Video", Log::CRIT, "Could not FBIOPUT_VSCREENINFO frame buffer device");
198 screeninfo.bits_per_pixel=16;
199 if (ioctl(fd_fbset, FBIOPUT_VSCREENINFO, &screeninfo)){
200 Log::getInstance()->log("Video", Log::CRIT, "Could not FBIOPUT_VSCREENINFO frame buffer device");
208 bool VideoOMX::loadOptionsfromServer(VDR* vdr)
210 Log::getInstance()->log("Video", Log::DEBUG, "VideoOMX config load");
211 char *name=vdr->configLoad("VideoOMX","SDDeinterlacing");
214 if (STRCASECMP(name, "None") == 0) {
216 }/* else if (STRCASECMP(name, "LineDouble") == 0) {
218 }*/ else if (STRCASECMP(name, "Advanced") == 0) {
220 } /*else if (STRCASECMP(name, "Crazy") == 0) {
221 deinterlace = 3; // this does not activate deinterlacing but a image filter, just for fun
223 Log::getInstance()->log("Video", Log::DEBUG, "Set deinterlacing to %s %d",name,deinterlace);
230 bool VideoOMX::handleOptionChanges(Option* option)
232 if (Video::handleOptionChanges(option))
234 switch (option->id) {
236 if (STRCASECMP(option->options[option->userSetChoice], "None") == 0) {
238 } /*else if (STRCASECMP(option->options[option->userSetChoice], "LineDouble")
241 }*/ else if (STRCASECMP(option->options[option->userSetChoice], "Advanced")
244 } /*else if (STRCASECMP(option->options[option->userSetChoice], "Crazy")
248 Log::getInstance()->log("Video", Log::DEBUG, "Set deinterlacing to %s %d",option->options[option->userSetChoice],deinterlace);
257 bool VideoOMX::saveOptionstoServer()
260 switch (deinterlace) {
262 VDR::getInstance()->configSave("VideoOMX","SDDeinterlacing", "None");
265 VDR::getInstance()->configSave("VideoOMX","SDDeinterlacing", "LineDouble");
268 VDR::getInstance()->configSave("VideoOMX","SDDeinterlacing", "Advanced");
271 VDR::getInstance()->configSave("VideoOMX","SDDeinterlacing", "Crazy");
278 /*Option(UINT id, const char* displayText, const char* configSection, const char* configKey, UINT optionType,
279 UINT numChoices, UINT defaultChoice, UINT startInt,
280 const char * const * options, const char * const * optionkeys = NULL, AbstractOption* handler=NULL);*/
282 bool VideoOMX::addOptionsToPanes(int panenumber,Options *options,WOptionPane* pane)
284 if (!Video::addOptionsToPanes(panenumber,options,pane)) return false;
290 static const char* deinterlaceopts[]={"None",/*"LineDouble",*/"Advanced"/*,"Crazy"*/};
291 option = new Option(1,tr("SD Deinterlacing"), "VideoOMX","SDDeinterlacing",Option::TYPE_TEXT,/*4,2*/2,1,0,deinterlaceopts,NULL,false,this);
292 options->push_back(option);
293 pane->addOptionLine(option);
301 int VideoOMX::setTVsize(UCHAR ttvsize)
303 if (tvsize!=ttvsize) pendingmodechange=true;
308 void VideoOMX::executePendingModeChanges()
310 if (pendingmodechange) {
311 Log::getInstance()->log("Video", Log::NOTICE, "Execute pending mode change");
312 Osd::getInstance()->shutdown();
314 Osd::getInstance()->restore();
315 Osd::getInstance()->init((void*) "");
316 BoxStack::getInstance()->redrawAllBoxes();
321 int VideoOMX::setDefaultAspect()
323 return setAspectRatio(tvsize);
328 int VideoOMX::setFormat(UCHAR tformat)
330 if (!initted) return 0;
331 if ((tformat != PAL) && (tformat != NTSC)
332 && (tformat != PAL_M) && (tformat != NTSC_J)) return 0;
342 // selectVideoMode(0);
347 void VideoOMX::selectVideoMode(int interlaced)
350 TV_SUPPORTED_MODE_T all_supp_modes[TV_MAX_SUPPORTED_MODES];
351 HDMI_RES_GROUP_T pref_group;
352 TV_SUPPORTED_MODE_T *mymode=NULL;
353 TV_SUPPORTED_MODE_T *mymode_second_best=NULL;
354 bool got_optimum=false;
356 HDMI_RES_GROUP_T group=HDMI_RES_GROUP_CEA;
357 int all_my_modes=vc_tv_hdmi_get_supported_modes(HDMI_RES_GROUP_CEA,
358 all_supp_modes,TV_MAX_SUPPORTED_MODES,
359 &pref_group,&pref_mode);
360 if (all_my_modes<=0) {
361 group=HDMI_RES_GROUP_DMT;
362 all_my_modes=vc_tv_hdmi_get_supported_modes(HDMI_RES_GROUP_DMT,
363 all_supp_modes,TV_MAX_SUPPORTED_MODES,
364 &pref_group,&pref_mode);
365 Log::getInstance()->log("Video", Log::NOTICE, "No CEA fall back to DMT modes ");
368 if (all_my_modes<=0 || connection==COMPOSITERGB ) {
370 Log::getInstance()->log("Video", Log::NOTICE, "No CEA and DMT modes found analog tv case?");
372 Remote::getInstance()->shutdown();
373 SDTV_MODE_T setmode=SDTV_MODE_PAL;
374 SDTV_OPTIONS_T options;
379 Log::getInstance()->log("Video", Log::NOTICE, "SDTV aspect 16:9");
380 options.aspect=SDTV_ASPECT_16_9; break;
382 Log::getInstance()->log("Video", Log::NOTICE, "SDTV aspect 4:3");
383 options.aspect=SDTV_ASPECT_4_3; break;
385 Log::getInstance()->log("Video", Log::NOTICE, "SDTV aspect 14:9");
386 options.aspect=SDTV_ASPECT_14_9; break;
389 if (format==PAL) setmode=SDTV_MODE_PAL;
390 else if (format==NTSC) setmode=SDTV_MODE_NTSC;
391 else if (format==PAL_M)setmode=SDTV_MODE_PAL_M;
392 else if (format==NTSC_J) setmode=SDTV_MODE_NTSC_J;
393 vc_tv_sdtv_power_on(setmode,&options);
398 if (format==PAL)target_fps=50;
399 else if (format==NTSC) target_fps=60;
401 //Now first determine native resolution
402 int native_width=1920;
403 int native_height=1080;
404 for (int i=0;i<all_my_modes;i++) {
405 if (all_supp_modes[i].native) {
406 mymode=all_supp_modes+i;
407 Log::getInstance()->log("Video", Log::NOTICE, "Found native mode %dx%d %d Hz i: %d",
408 mymode->width,mymode->height,mymode->frame_rate,mymode->scan_mode);
409 native_width=mymode->width;
410 native_height=mymode->height;
414 //Now find the mode which matches best
415 for (int i=0;i<all_my_modes;i++) {
416 TV_SUPPORTED_MODE_T *curmode=all_supp_modes+i;
417 if (curmode->width==native_width &&
418 curmode->height==native_height &&
419 curmode->frame_rate==target_fps) {
420 if(curmode->scan_mode==interlaced) {
423 Log::getInstance()->log("Video", Log::NOTICE, "Found optimum mode %dx%d %d Hz i: %d",
424 mymode->width,mymode->height,mymode->frame_rate,mymode->scan_mode);
426 mymode_second_best=curmode;
427 Log::getInstance()->log("Video", Log::NOTICE, "Found close to optimum mode %dx%d %d Hz i: %d",
428 mymode_second_best->width,mymode_second_best->height,
429 mymode_second_best->frame_rate,mymode_second_best->scan_mode);
435 Remote::getInstance()->shutdown();
437 Log::getInstance()->log("Video", Log::NOTICE, "Switch to optimum mode");
438 vc_tv_hdmi_power_on_explicit(HDMI_MODE_HDMI,group,mymode->code);
439 } else if (mymode_second_best) {
440 Log::getInstance()->log("Video", Log::NOTICE, "Switch to close to optimum mode");
441 vc_tv_hdmi_power_on_explicit(HDMI_MODE_HDMI,group,mymode_second_best->code);
443 Log::getInstance()->log("Video", Log::NOTICE, "Switch to prefered mode");
444 vc_tv_hdmi_power_on_best(1920,1080,target_fps,interlaced?HDMI_INTERLACED:HDMI_NONINTERLACED,
445 (EDID_MODE_MATCH_FLAG_T)(HDMI_MODE_MATCH_FRAMERATE|HDMI_MODE_MATCH_RESOLUTION|HDMI_MODE_MATCH_SCANMODE));
450 Remote::getInstance()->init("");
454 pendingmodechange=false;
459 int VideoOMX::setConnection(UCHAR tconnection)
461 if (!initted) return 0;
462 if ((tconnection != COMPOSITERGB) && (tconnection != HDMI)) return 0;
463 if (connection!=tconnection) pendingmodechange=true;
464 connection = tconnection;
466 // if (ioctl(fdVideo, AV_SET_VID_OUTPUT, connection) != 0) return 0;
470 int VideoOMX::setAspectRatio(UCHAR taspectRatio)
472 if (!initted) return 0;
473 if ((taspectRatio != ASPECT4X3) && (taspectRatio != ASPECT16X9)) return 0;
474 aspectRatio = taspectRatio;
476 Log::getInstance()->log("Video", Log::DEBUG, "Setting aspect to %i", aspectRatio);
478 // if (ioctl(fdVideo, AV_SET_VID_RATIO, aspectRatio) != 0) return 0;
482 int VideoOMX::setMode(UCHAR tmode)
484 if (!initted) return 0;
491 void VideoOMX::updateMode()
496 OMX_CONFIG_DISPLAYREGIONTYPE dispconf;
497 memset(&dispconf, 0, sizeof(dispconf));
498 dispconf.nSize = sizeof(dispconf);
499 dispconf.nVersion.nVersion = OMX_VERSION;
500 dispconf.nPortIndex = omx_rend_input_port;
502 dispconf.set = OMX_DISPLAY_SET_LAYER;
503 error = OMX_SetParameter(omx_vid_rend, OMX_IndexConfigDisplayRegion,
505 if (error != OMX_ErrorNone) {
506 Log::getInstance()->log("Video", Log::DEBUG,
507 "Set OMX_IndexConfigDisplayRegion1 failed %x", error);
508 clock_mutex.Unlock();
511 dispconf.set = OMX_DISPLAY_SET_FULLSCREEN;
512 if (mode != QUARTER && mode != EIGHTH) {
514 dispconf.fullscreen = OMX_TRUE;
516 dispconf.fullscreen = OMX_FALSE;
518 error = OMX_SetParameter(omx_vid_rend, OMX_IndexConfigDisplayRegion,
520 if (error != OMX_ErrorNone) {
521 Log::getInstance()->log("Video", Log::DEBUG,
522 "Set OMX_IndexConfigDisplayRegion2 failed %x", error);
523 clock_mutex.Unlock();
527 dispconf.set = OMX_DISPLAY_SET_MODE;
528 if (mode != QUARTER && mode != EIGHTH) {
529 dispconf.mode = (mode == NORMAL) ? OMX_DISPLAY_MODE_FILL
530 : OMX_DISPLAY_MODE_LETTERBOX;
532 dispconf.mode = OMX_DISPLAY_MODE_LETTERBOX;
534 error = OMX_SetParameter(omx_vid_rend, OMX_IndexConfigDisplayRegion,
536 if (error != OMX_ErrorNone) {
537 Log::getInstance()->log("Video", Log::DEBUG,
538 "Set OMX_IndexConfigDisplayRegion3 failed %x", error);
539 clock_mutex.Unlock();
543 if (mode == QUARTER || mode == EIGHTH) {
544 unsigned int display_width, display_height;
545 display_width = display_height = 0;
546 if (graphics_get_display_size(0, &display_width, &display_height)
548 Log::getInstance()->log("OSD", Log::WARN,
549 "Getting display size failed! (BCM API) ");
550 clock_mutex.Unlock();
553 //UnSetFullscreen with window
554 dispconf.set = OMX_DISPLAY_SET_DEST_RECT;
555 dispconf.dest_rect.x_offset
556 = (int) (xpos * ((float) display_width));
557 dispconf.dest_rect.y_offset = (int) (ypos
558 * ((float) display_height));
559 if (mode == QUARTER) {
560 dispconf.dest_rect.width = display_width >> 1;
561 dispconf.dest_rect.height = display_height >> 1;
562 } else if (mode == EIGHTH) {
563 dispconf.dest_rect.width = display_width >> 2;
564 dispconf.dest_rect.height = display_height >> 2;
566 error = OMX_SetParameter(omx_vid_rend,
567 OMX_IndexConfigDisplayRegion, &dispconf);
568 if (error != OMX_ErrorNone) {
569 Log::getInstance()->log("Video", Log::DEBUG,
570 "Set OMX_IndexConfigDisplayRegion failed %x", error);
571 clock_mutex.Unlock();
577 clock_mutex.Unlock();
580 int VideoOMX::signalOff()
583 Log::getInstance()->log("Video", Log::NOTICE, "signalOff");
585 Osd::getInstance()->stopUpdate(); // turn off drawing thread
590 int VideoOMX::signalOn()
593 Log::getInstance()->log("Video", Log::NOTICE, "signalOn");
594 Osd::getInstance()->shutdown();
596 Osd::getInstance()->restore();
597 Osd::getInstance()->init((void*)"");
598 BoxStack::getInstance()->redrawAllBoxes();
605 int VideoOMX::setSource()
607 if (!initted) return 0;
609 // What does this do...
610 // if (ioctl(fdVideo, AV_SET_VID_SRC, 1) != 0) return 0;
614 int VideoOMX::setPosition(int x, int y) {
617 xpos = ((float) x*2.f) / ((float) screenWidth);
618 ypos = ((float) y*2.f) / ((float) screenHeight);
626 if (!initted) return 0;
628 // if (ioctl(fdVideo, AV_SET_VID_SYNC, 2) != 0) return 0;
633 int VideoOMX::play() {
637 Log::getInstance()->log("Video", Log::DEBUG, "enter play");
639 if (AllocateCodecsOMX()) {
641 // Otherwise fall back to libav
645 Log::getInstance()->log("Video", Log::NOTICE,
646 "Allocate Codecs OMX failed assume h264 unsupported");
649 Log::getInstance()->log("Video", Log::NOTICE,
650 "Allocate Codecs OMX failed assume mpeg2 unsupported");
659 int VideoOMX::initClock()
663 if (clock_references==0)
666 static OMX_CALLBACKTYPE callbacks= {&EventHandler_OMX,&EmptyBufferDone_OMX,&FillBufferDone_OMX};
669 error=OMX_GetHandle(&omx_clock,VPE_OMX_CLOCK,NULL,&callbacks);
671 if (error!=OMX_ErrorNone) {
672 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX clock failed %x", error);
673 clock_mutex.Unlock();
674 DeAllocateCodecsOMX();
678 /* TODO Clock config to separate method */
679 OMX_PORT_PARAM_TYPE p_param;
680 memset(&p_param,0,sizeof(p_param));
681 p_param.nSize=sizeof(p_param);
682 p_param.nVersion.nVersion=OMX_VERSION;
683 error=OMX_GetParameter(omx_clock,OMX_IndexParamOtherInit,&p_param);
684 if (error!=OMX_ErrorNone) {
685 Log::getInstance()->log("Video", Log::DEBUG, "Init clock OMX_GetParameter failed %x", error);
686 clock_mutex.Unlock();
687 DeAllocateCodecsOMX();
690 omx_clock_output_port=p_param.nStartPortNumber;
692 for (unsigned int i=0;i<p_param.nPorts;i++) {
693 if (!DisablePort(omx_clock,p_param.nStartPortNumber+i,true) ) {
694 Log::getInstance()->log("Video", Log::DEBUG, "Disable Ports OMX clock failed %d",i);
695 clock_mutex.Unlock();
696 DeAllocateCodecsOMX();
705 Log::getInstance()->log("Video", Log::DEBUG, "init omx clock %x %x",this,omx_clock);
707 clock_mutex.Unlock();
711 int VideoOMX::getClockAudioandInit(OMX_HANDLETYPE *p_omx_clock,OMX_U32 *p_omx_clock_output_port)
715 *p_omx_clock_output_port=0;
722 OMX_TIME_CONFIG_ACTIVEREFCLOCKTYPE refclock;
723 memset(&refclock,0,sizeof(refclock));
724 refclock.nSize=sizeof(refclock);
725 refclock.nVersion.nVersion=OMX_VERSION;
727 refclock.eClock=OMX_TIME_RefClockAudio;
729 //refclock.eClock=OMX_TIME_RefClockVideo;
730 error=OMX_SetConfig(omx_clock,OMX_IndexConfigTimeActiveRefClock,&refclock);
731 if (error!=OMX_ErrorNone){
732 Log::getInstance()->log("Video", Log::DEBUG, "Clock OMX_IndexConfigTimeActiveRefClock failed %x", error);
733 clock_mutex.Unlock();
734 DeAllocateCodecsOMX();
738 OMX_PORT_PARAM_TYPE p_param;
739 memset(&p_param,0,sizeof(p_param));
740 p_param.nSize=sizeof(p_param);
741 p_param.nVersion.nVersion=OMX_VERSION;
742 error=OMX_GetParameter(omx_clock,OMX_IndexParamOtherInit,&p_param);
743 if (error!=OMX_ErrorNone){
744 Log::getInstance()->log("Video", Log::DEBUG, "Init clock OMX_GetParameter failed %x", error);
745 clock_mutex.Unlock();
746 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=intToOMXTicks(0);
756 clock_conf.nOffset=intToOMXTicks(0);
757 if (clock_references==1) clock_conf.nWaitMask=OMX_CLOCKPORT1;
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, "AuI Clock IndexConfigTimeClockState failed %x", error);
765 *p_omx_clock_output_port=p_param.nStartPortNumber+1;
766 *p_omx_clock=omx_clock;
767 clock_mutex.Unlock();
771 int VideoOMX::getClockVideoandInit()
780 if (clock_references==1) { // only if no audio is attached to this clock!
781 OMX_TIME_CONFIG_ACTIVEREFCLOCKTYPE refclock;
782 memset(&refclock,0,sizeof(refclock));
783 refclock.nSize=sizeof(refclock);
784 refclock.nVersion.nVersion=OMX_VERSION;
786 //refclock.eClock=OMX_TIME_RefClockAudio;
788 refclock.eClock=OMX_TIME_RefClockVideo;
789 error=OMX_SetConfig(omx_clock,OMX_IndexConfigTimeActiveRefClock,&refclock);
790 if (error!=OMX_ErrorNone) {
791 Log::getInstance()->log("Video", Log::DEBUG, "Clock OMX_IndexConfigTimeActiveRefClock failed %x", error);
792 clock_mutex.Unlock();
793 DeAllocateCodecsOMX();
798 OMX_PORT_PARAM_TYPE p_param;
799 memset(&p_param,0,sizeof(p_param));
800 p_param.nSize=sizeof(p_param);
801 p_param.nVersion.nVersion=OMX_VERSION;
802 error=OMX_GetParameter(omx_clock,OMX_IndexParamOtherInit,&p_param);
803 if (error!=OMX_ErrorNone){
804 Log::getInstance()->log("Video", Log::DEBUG, "Init clock OMX_GetParameter failed %x", error);
805 clock_mutex.Unlock();
806 DeAllocateCodecsOMX();
811 OMX_TIME_CONFIG_CLOCKSTATETYPE clock_conf;
812 memset(&clock_conf,0,sizeof(clock_conf));
813 clock_conf.nSize=sizeof(clock_conf);
814 clock_conf.nVersion.nVersion=OMX_VERSION;
815 clock_conf.eState=OMX_TIME_ClockStateStopped;
816 clock_conf.nStartTime=intToOMXTicks(0);
817 clock_conf.nOffset=intToOMXTicks(0);
818 error=OMX_SetConfig(omx_clock,OMX_IndexConfigTimeClockState,&clock_conf);
819 if (error!=OMX_ErrorNone) {
820 Log::getInstance()->log("Video", Log::DEBUG, "VuI Clock IndexConfigTimeClockState failed %x", error);
824 memset(&clock_conf,0,sizeof(clock_conf));
825 clock_conf.nSize=sizeof(clock_conf);
826 clock_conf.nVersion.nVersion=OMX_VERSION;
827 clock_conf.eState=OMX_TIME_ClockStateWaitingForStartTime;
828 clock_conf.nStartTime=intToOMXTicks(0);
829 clock_conf.nOffset=intToOMXTicks(0);
830 if (clock_references==1) clock_conf.nWaitMask=OMX_CLOCKPORT0;
831 else clock_conf.nWaitMask=OMX_CLOCKPORT0|OMX_CLOCKPORT1;
832 error=OMX_SetConfig(omx_clock,OMX_IndexConfigTimeClockState,&clock_conf);
833 if (error!=OMX_ErrorNone) {
834 Log::getInstance()->log("Video", Log::DEBUG, "VuI Clock IndexConfigTimeClockState failed %x", error);
837 omx_clock_output_port=p_param.nStartPortNumber;
838 clock_mutex.Unlock();
843 void VideoOMX::clockUnpause()
847 if (clock_references>0) {
848 OMX_TIME_CONFIG_CLOCKSTATETYPE clock_conf;
849 memset(&clock_conf,0,sizeof(clock_conf));
850 clock_conf.nSize=sizeof(clock_conf);
851 clock_conf.nVersion.nVersion=OMX_VERSION;
852 clock_conf.eState=OMX_TIME_ClockStateRunning;
853 clock_conf.nStartTime=intToOMXTicks(0);
854 clock_conf.nOffset=intToOMXTicks(0);
855 clock_conf.nWaitMask=OMX_CLOCKPORT1;
856 error=OMX_SetConfig(omx_clock,OMX_IndexConfigTimeClockState,&clock_conf);
857 if (error!=OMX_ErrorNone) {
858 Log::getInstance()->log("Video", Log::DEBUG, "ClockUnpause IndexConfigTimeClockState failed %x", error);
861 clock_mutex.Unlock();
865 void VideoOMX::clockPause()
869 if (clock_references>0) {
870 OMX_TIME_CONFIG_CLOCKSTATETYPE clock_conf;
871 memset(&clock_conf,0,sizeof(clock_conf));
872 clock_conf.nSize=sizeof(clock_conf);
873 clock_conf.nVersion.nVersion=OMX_VERSION;
874 clock_conf.eState=OMX_TIME_ClockStateStopped;
875 clock_conf.nStartTime=intToOMXTicks(0);
876 clock_conf.nOffset=intToOMXTicks(0);
877 clock_conf.nWaitMask=OMX_CLOCKPORT1;
878 error=OMX_SetConfig(omx_clock,OMX_IndexConfigTimeClockState,&clock_conf);
879 if (error!=OMX_ErrorNone) {
880 Log::getInstance()->log("Video", Log::DEBUG, "ClockUnpause IndexConfigTimeClockState failed %x", error);
883 clock_mutex.Unlock();
888 int VideoOMX::AllocateCodecsOMX()
891 static OMX_CALLBACKTYPE callbacks= {&EventHandler_OMX,&EmptyBufferDone_OMX,&FillBufferDone_OMX};
893 Demuxer* demux=Demuxer::getInstance();
896 deint_first_frame=false;
898 Log::getInstance()->log("Video", Log::NOTICE, "Allocate Codecs OMX");
899 //Clock, move later to audio including events
901 Log::getInstance()->log("Video", Log::NOTICE, "VideoType %d x %d i: %d", demux->getHorizontalSize(),demux->getVerticalSize(),demux->getInterlaced());
902 if (deinterlace!=0 && (demux->getHorizontalSize()<=720 ) && demux->getInterlaced()) { //only deinterlace SD material
904 deint_first_frame=true;
906 Log::getInstance()->log("Video", Log::NOTICE, "Deinterlacing activated %d",deinterlace);
911 if (!getClockVideoandInit()){
912 return 0;// get the clock and init it if necessary
917 Log::getInstance()->log("Video", Log::DEBUG, "idleClock failed");
924 error=OMX_GetHandle(&omx_vid_dec,VPE_OMX_H264_DECODER,NULL,&callbacks);
926 error=OMX_GetHandle(&omx_vid_dec,VPE_OMX_MPEG2_DECODER,NULL,&callbacks);
929 if (error!=OMX_ErrorNone){
930 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX video decoder failed %x", error);
931 clock_mutex.Unlock();
932 DeAllocateCodecsOMX();
937 Log::getInstance()->log("Video", Log::DEBUG, "Nmark3 ");
938 OMX_PORT_PARAM_TYPE p_param;
939 memset(&p_param,0,sizeof(p_param));
940 p_param.nSize=sizeof(p_param);
941 p_param.nVersion.nVersion=OMX_VERSION;
942 error=OMX_GetParameter(omx_vid_dec,OMX_IndexParamVideoInit,&p_param);
943 if (error!=OMX_ErrorNone){
944 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX h264 decoder OMX_GetParameter failed %x", error);
945 clock_mutex.Unlock();
946 DeAllocateCodecsOMX();
949 omx_codec_input_port=p_param.nStartPortNumber;
950 omx_codec_output_port=p_param.nStartPortNumber+1;
952 if (!DisablePort(omx_vid_dec,omx_codec_input_port) || !DisablePort(omx_vid_dec,omx_codec_output_port)) {
953 Log::getInstance()->log("Video", Log::DEBUG, "Disable Ports OMX video decoder failed");
954 clock_mutex.Unlock();
955 DeAllocateCodecsOMX();
959 Log::getInstance()->log("Video", Log::DEBUG, "Nmark4 ");
961 OMX_PARAM_BRCMVIDEODECODEERRORCONCEALMENTTYPE conceal;
962 memset(&conceal,0,sizeof(conceal));
963 conceal.nSize=sizeof(conceal);
964 conceal.nVersion.nVersion=OMX_VERSION;
965 conceal.bStartWithValidFrame=OMX_FALSE;
967 error=OMX_SetParameter(omx_vid_dec,OMX_IndexParamBrcmVideoDecodeErrorConcealment,&conceal);
968 if (error!=OMX_ErrorNone){
969 Log::getInstance()->log("Video", Log::DEBUG, "OMX_IndexParamBrcmVideoDecodeErrorConcealment failed %x", error);
970 clock_mutex.Unlock();
971 DeAllocateCodecsOMX();
976 error = OMX_GetHandle(&omx_vid_deint, VPE_OMX_VIDEO_DEINTERLACE, NULL,
978 if (error != OMX_ErrorNone) {
979 Log::getInstance()->log("Video", Log::DEBUG,
980 "Init OMX video deinterlacer failed %x", error);
981 clock_mutex.Unlock();
982 DeAllocateCodecsOMX();
986 error = OMX_GetParameter(omx_vid_deint, OMX_IndexParamImageInit,
988 if (error != OMX_ErrorNone) {
989 Log::getInstance()->log("Video", Log::DEBUG,
990 "Init OMX video deinterlacer OMX_GetParameter failed %x",
992 clock_mutex.Unlock();
993 DeAllocateCodecsOMX();
996 omx_deint_input_port = p_param.nStartPortNumber;
997 omx_deint_output_port = p_param.nStartPortNumber + 1;
999 if (!DisablePort(omx_vid_deint, omx_deint_input_port, true)
1000 || !DisablePort(omx_vid_deint, omx_deint_output_port, true)) {
1001 Log::getInstance()->log("Video", Log::DEBUG,
1002 "Disable Ports OMX video deint failed");
1003 clock_mutex.Unlock();
1004 DeAllocateCodecsOMX();
1011 error=OMX_GetHandle(&omx_vid_sched,VPE_OMX_VIDEO_SCHED,NULL,&callbacks);
1012 if (error!=OMX_ErrorNone){
1013 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX video scheduler failed %x", error);
1014 clock_mutex.Unlock();
1015 DeAllocateCodecsOMX();
1021 error=OMX_GetParameter(omx_vid_sched,OMX_IndexParamVideoInit,&p_param);
1022 if (error!=OMX_ErrorNone){
1023 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX video scheduler OMX_GetParameter failed %x", error);
1024 clock_mutex.Unlock();
1025 DeAllocateCodecsOMX();
1028 omx_shed_input_port=p_param.nStartPortNumber;
1029 omx_shed_output_port=p_param.nStartPortNumber+1;
1032 error=OMX_GetParameter(omx_vid_sched,OMX_IndexParamOtherInit,&p_param);
1033 if (error!=OMX_ErrorNone){
1034 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX video scheduler OMX_GetParameter failed %x", error);
1035 clock_mutex.Unlock();
1036 DeAllocateCodecsOMX();
1039 omx_shed_clock_port=p_param.nStartPortNumber;
1040 Log::getInstance()->log("Video", Log::DEBUG, "scheduler ports %d %d %d ",omx_shed_input_port,omx_shed_output_port,omx_shed_clock_port);
1043 if (!DisablePort(omx_vid_sched,omx_shed_input_port,true) || !DisablePort(omx_vid_sched,omx_shed_output_port,true)
1044 || !DisablePort(omx_vid_sched,omx_shed_clock_port,true)) {
1045 Log::getInstance()->log("Video", Log::DEBUG, "Disable Ports OMX video shed failed");
1046 clock_mutex.Unlock();
1047 DeAllocateCodecsOMX();
1052 error=OMX_GetHandle(&omx_vid_rend,VPE_OMX_VIDEO_REND,NULL,&callbacks);
1053 if (error!=OMX_ErrorNone){
1054 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX video rend failed %x", error);
1055 clock_mutex.Unlock();
1056 DeAllocateCodecsOMX();
1060 error=OMX_GetParameter(omx_vid_rend,OMX_IndexParamVideoInit,&p_param);
1061 if (error!=OMX_ErrorNone){
1062 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX video rend OMX_GetParameter failed %x", error);
1063 clock_mutex.Unlock();
1064 DeAllocateCodecsOMX();
1067 omx_rend_input_port=p_param.nStartPortNumber;
1068 //omx_rend_output_port=p_param.nStartPortNumber+1;
1071 if (!DisablePort(omx_vid_rend,omx_rend_input_port,true) /*|| !DisablePort(omx_vid_rend,omx_rend_output_port)*/
1073 Log::getInstance()->log("Video", Log::DEBUG, "Disable Ports OMX video rend failed");
1074 clock_mutex.Unlock();
1075 DeAllocateCodecsOMX();
1083 error=OMX_SetupTunnel(omx_clock,omx_clock_output_port,omx_vid_sched,omx_shed_clock_port);
1084 if (error!=OMX_ErrorNone){
1085 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);
1086 clock_mutex.Unlock();
1087 DeAllocateCodecsOMX();
1091 if (!EnablePort(omx_clock,omx_clock_output_port,false) || !EnablePort(omx_vid_sched,omx_shed_clock_port,false)
1093 Log::getInstance()->log("Video", Log::DEBUG, "Enable Ports OMX clock shed failed");
1094 clock_mutex.Unlock();
1095 DeAllocateCodecsOMX();
1100 Log::getInstance()->log("Video", Log::DEBUG, "mark2 ");
1101 if (!ChangeComponentState(omx_vid_sched,OMX_StateIdle)) {
1102 Log::getInstance()->log("Video", Log::DEBUG, "vid_sched idle ChangeComponentState");
1103 clock_mutex.Unlock();
1104 DeAllocateCodecsOMX();
1110 Log::getInstance()->log("Video", Log::DEBUG, "mark1 ");
1111 if ( !CommandFinished(omx_vid_sched,OMX_CommandPortEnable,omx_shed_clock_port)) {
1112 clock_mutex.Unlock();
1113 DeAllocateCodecsOMX();
1120 Log::getInstance()->log("Video", Log::DEBUG, "mark1 special ");
1121 if ( !CommandFinished(omx_clock,OMX_CommandPortEnable,omx_clock_output_port)) {
1122 clock_mutex.Unlock();
1123 DeAllocateCodecsOMX();
1129 Log::getInstance()->log("Video", Log::DEBUG, "mark1b ");
1132 /* error=OMX_SendCommand(omx_vid_dec,OMX_CommandStateSet,OMX_StateIdle,0);
1133 if (error!=OMX_ErrorNone){
1134 Log::getInstance()->log("Video", Log::DEBUG, "vid_dec Send Command to OMX State Idle %x", error);
1138 OMX_VIDEO_PARAM_PORTFORMATTYPE ft_type;
1139 memset(&ft_type,0,sizeof(ft_type));
1140 ft_type.nSize=sizeof(ft_type);
1141 ft_type.nVersion.nVersion=OMX_VERSION;
1143 ft_type.nPortIndex=omx_codec_input_port;
1145 ft_type.eCompressionFormat=OMX_VIDEO_CodingAVC;
1147 ft_type.eCompressionFormat=OMX_VIDEO_CodingMPEG2;
1152 ft_type.xFramerate=0*(1<<16);//25*(1<<16);//demux->getFrameRate()*(1<<16);
1153 Log::getInstance()->log("Video", Log::DEBUG, "Framerate: %d",demux->getFrameRate());
1154 error=OMX_SetParameter(omx_vid_dec,OMX_IndexParamVideoPortFormat,&ft_type);
1155 if (error!=OMX_ErrorNone){
1156 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX_IndexParamVideoPortFormat failed %x", error);
1157 clock_mutex.Unlock();
1158 DeAllocateCodecsOMX();
1163 if (!ChangeComponentState(omx_vid_dec,OMX_StateIdle)) {
1164 Log::getInstance()->log("Video", Log::DEBUG, "vid_dec ChangeComponentState");
1165 clock_mutex.Unlock();
1166 DeAllocateCodecsOMX();
1171 if (!PrepareInputBufsOMX()) {
1172 clock_mutex.Unlock();
1173 DeAllocateCodecsOMX();
1178 error=OMX_SetupTunnel(omx_vid_dec,omx_codec_output_port,omx_vid_sched,omx_shed_input_port);
1179 if (error!=OMX_ErrorNone){
1180 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel dec to sched failed %x", error);
1181 clock_mutex.Unlock();
1182 DeAllocateCodecsOMX();
1188 if (!EnablePort(omx_vid_dec,omx_codec_output_port,false) || !EnablePort(omx_vid_sched,omx_shed_input_port,false)
1190 Log::getInstance()->log("Video", Log::DEBUG, "Enable Ports OMX codec shed failed");
1191 clock_mutex.Unlock();
1192 DeAllocateCodecsOMX();
1196 if ( !CommandFinished(omx_vid_dec,OMX_CommandPortEnable,omx_codec_output_port) || !CommandFinished(omx_vid_sched,OMX_CommandPortEnable,omx_shed_input_port)) {
1197 clock_mutex.Unlock();
1198 DeAllocateCodecsOMX();
1204 error=OMX_SetupTunnel(omx_vid_dec,omx_codec_output_port,omx_vid_deint,omx_deint_input_port);
1205 if (error!=OMX_ErrorNone){
1206 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel dec to deint failed %x", error);
1207 clock_mutex.Unlock();
1208 DeAllocateCodecsOMX();
1214 if (!EnablePort(omx_vid_dec,omx_codec_output_port,false) || !EnablePort(omx_vid_deint,omx_deint_input_port,false)
1216 Log::getInstance()->log("Video", Log::DEBUG, "Enable Ports OMX codec deint failed");
1217 clock_mutex.Unlock();
1218 DeAllocateCodecsOMX();
1222 if ( !CommandFinished(omx_vid_dec,OMX_CommandPortEnable,omx_codec_output_port) || !CommandFinished(omx_vid_deint,OMX_CommandPortEnable,omx_deint_input_port)) {
1223 clock_mutex.Unlock();
1224 DeAllocateCodecsOMX();
1228 if (!ChangeComponentState(omx_vid_deint,OMX_StateIdle)) {
1229 Log::getInstance()->log("Video", Log::DEBUG, "vid_deint ChangeComponentState");
1230 clock_mutex.Unlock();
1231 DeAllocateCodecsOMX();
1235 OMX_CONFIG_IMAGEFILTERPARAMSTYPE imagefilter;
1236 memset(&imagefilter,0,sizeof(imagefilter));
1237 imagefilter.nSize=sizeof(imagefilter);
1238 imagefilter.nVersion.nVersion=OMX_VERSION;
1240 imagefilter.nPortIndex=omx_deint_output_port;
1241 imagefilter.nNumParams=1;
1242 imagefilter.nParams[0]=3;//???
1243 switch (deinterlace) {
1245 imagefilter.eImageFilter=OMX_ImageFilterDeInterlaceLineDouble; break;
1247 imagefilter.eImageFilter=OMX_ImageFilterDeInterlaceAdvanced; break;
1249 imagefilter.eImageFilter=OMX_ImageFilterFilm; break;
1253 error=OMX_SetConfig(omx_vid_deint,OMX_IndexConfigCommonImageFilterParameters,&imagefilter);
1254 if (error!=OMX_ErrorNone){
1255 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX_IndexConfigCommonImageFilterParameters failed %x", error);
1256 clock_mutex.Unlock();
1257 DeAllocateCodecsOMX();
1262 error=OMX_SetupTunnel(omx_vid_deint,omx_deint_output_port,omx_vid_sched,omx_shed_input_port);
1263 if (error!=OMX_ErrorNone){
1264 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel deint to sched failed %x", error);
1265 clock_mutex.Unlock();
1266 DeAllocateCodecsOMX();
1270 if (!EnablePort(omx_vid_deint,omx_deint_output_port,false) || !EnablePort(omx_vid_sched,omx_shed_input_port,false)
1272 Log::getInstance()->log("Video", Log::DEBUG, "Enable Ports OMX deint shed failed");
1273 clock_mutex.Unlock();
1274 DeAllocateCodecsOMX();
1278 if ( !CommandFinished(omx_vid_deint,OMX_CommandPortEnable,omx_deint_output_port) || !CommandFinished(omx_vid_sched,OMX_CommandPortEnable,omx_shed_input_port)) {
1279 clock_mutex.Unlock();
1280 DeAllocateCodecsOMX();
1286 if (!ChangeComponentState(omx_vid_dec,OMX_StateExecuting)) {
1287 Log::getInstance()->log("Video", Log::DEBUG, "omx_vid_dec ChangeComponentState Execute");
1288 clock_mutex.Unlock();
1289 DeAllocateCodecsOMX();
1293 error=OMX_SetupTunnel(omx_vid_sched,omx_shed_output_port,omx_vid_rend,omx_rend_input_port);
1294 if (error!=OMX_ErrorNone){
1295 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel sched to rend failed %x", error);
1296 clock_mutex.Unlock();
1297 DeAllocateCodecsOMX();
1301 if (!EnablePort(omx_vid_sched,omx_shed_output_port,false) || !EnablePort(omx_vid_rend,omx_rend_input_port,false)
1303 Log::getInstance()->log("Video", Log::DEBUG, "Enable Ports OMX shed rend failed");
1304 clock_mutex.Unlock();
1305 DeAllocateCodecsOMX();
1309 if (!CommandFinished(omx_vid_sched,OMX_CommandPortEnable,omx_shed_output_port)
1310 || !CommandFinished(omx_vid_rend,OMX_CommandPortEnable,omx_rend_input_port)) {
1311 clock_mutex.Unlock();
1312 DeAllocateCodecsOMX();
1316 if (!ChangeComponentState(omx_vid_rend,OMX_StateIdle)) {
1317 Log::getInstance()->log("Video", Log::DEBUG, "vid_rend ChangeComponentState");
1318 clock_mutex.Unlock();
1319 DeAllocateCodecsOMX();
1324 if (!ChangeComponentState(omx_vid_deint,OMX_StateExecuting)) {
1325 Log::getInstance()->log("Video", Log::DEBUG, "vid_vid_deint ChangeComponentState");
1326 clock_mutex.Unlock();
1327 DeAllocateCodecsOMX();
1330 DisablePort(omx_vid_deint,omx_deint_output_port,false);
1331 DisablePort(omx_vid_deint,omx_deint_input_port,false);
1334 if (!ChangeComponentState(omx_vid_sched,OMX_StateExecuting)) {
1335 Log::getInstance()->log("Video", Log::DEBUG, "omx_vid_sched ChangeComponentState Execute");
1336 clock_mutex.Unlock();
1337 DeAllocateCodecsOMX();
1341 if (!ChangeComponentState(omx_vid_rend,OMX_StateExecuting)) {
1342 Log::getInstance()->log("Video", Log::DEBUG, "omx_vid_rend ChangeComponentState Execute");
1343 clock_mutex.Unlock();
1344 DeAllocateCodecsOMX();
1349 /*OMX_CONFIG_DISPLAYREGIONTYPE dispconf;
1350 memset(&dispconf,0,sizeof(dispconf));
1351 dispconf.nSize=sizeof(dispconf);
1352 dispconf.nVersion.nVersion=OMX_VERSION;
1354 dispconf.nPortIndex=omx_rend_input_port;
1356 dispconf.set=OMX_DISPLAY_SET_LAYER ;
1358 error=OMX_SetConfig(omx_vid_rend,OMX_IndexConfigDisplayRegion,&dispconf);
1359 if (error!=OMX_ErrorNone){
1360 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX_IndexConfigDisplayRegion failed %x", error);
1361 clock_mutex.Unlock();
1362 DeAllocateCodecsOMX();
1366 /* dispconf.set=OMX_DISPLAY_SET_FULLSCREEN ;
1367 dispconf.fullscreen=OMX_FALSE;
1368 error=OMX_SetParameter(omx_vid_rend,OMX_IndexConfigDisplayRegion,&dispconf);
1369 if (error!=OMX_ErrorNone){
1370 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX_IndexConfigDisplayRegion failed %x", error);
1371 clock_mutex.Unlock();
1372 DeAllocateCodecsOMX();
1376 dispconf.set=OMX_DISPLAY_SET_DEST_RECT;
1377 dispconf.dest_rect.x_offset=100;
1378 dispconf.dest_rect.y_offset=100;
1379 dispconf.dest_rect.width=640;
1380 dispconf.dest_rect.height=480;
1381 error=OMX_SetParameter(omx_vid_rend,OMX_IndexConfigDisplayRegion,&dispconf);
1382 if (error!=OMX_ErrorNone){
1383 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX_IndexConfigDisplayRegion failed %x", error);
1384 clock_mutex.Unlock();
1385 DeAllocateCodecsOMX();
1390 //playbacktimeoffset=-GetCurrentSystemTime();
1394 clock_mutex.Unlock();
1397 setClockExecutingandRunning();
1406 int VideoOMX::idleClock()
1408 OMX_ERRORTYPE error;
1409 OMX_STATETYPE temp_state;
1411 OMX_GetState(omx_clock,&temp_state);
1413 if (temp_state!=OMX_StateIdle) {
1414 if (!ChangeComponentState(omx_clock,OMX_StateIdle)) {
1415 Log::getInstance()->log("Video", Log::DEBUG, "omx_clock ChangeComponentState Idle failed");
1416 clock_mutex.Unlock();
1420 clock_mutex.Unlock();
1424 int VideoOMX::setClockExecutingandRunning()
1426 OMX_ERRORTYPE error;
1427 OMX_STATETYPE temp_state;
1429 OMX_GetState(omx_clock,&temp_state);
1431 if (temp_state!=OMX_StateExecuting) {
1432 if (!ChangeComponentState(omx_clock,OMX_StateExecuting)) {
1433 Log::getInstance()->log("Video", Log::DEBUG, "omx_clock ChangeComponentState Execute failed");
1434 clock_mutex.Unlock();
1435 DeAllocateCodecsOMX();
1440 OMX_TIME_CONFIG_CLOCKSTATETYPE clock_conf;
1441 memset(&clock_conf,0,sizeof(clock_conf));
1442 clock_conf.nSize=sizeof(clock_conf);
1443 clock_conf.nVersion.nVersion=OMX_VERSION;
1444 clock_conf.eState=OMX_TIME_ClockStateRunning;
1445 error=OMX_SetConfig(omx_clock,OMX_IndexConfigTimeClockState,&clock_conf);
1446 if (error!=OMX_ErrorNone) {
1447 Log::getInstance()->log("Video", Log::DEBUG, "Clock IndexConfigTimeClockState failed %x", error);
1448 clock_mutex.Unlock();
1449 DeAllocateCodecsOMX();
1452 clock_mutex.Unlock();
1458 int VideoOMX::ChangeComponentState(OMX_HANDLETYPE handle,OMX_STATETYPE type) //needs to be called with locked mutex
1460 OMX_ERRORTYPE error;
1461 error=OMX_SendCommand(handle,OMX_CommandStateSet,type,0);
1462 if (error!=OMX_ErrorNone){
1463 Log::getInstance()->log("Video", Log::DEBUG, "handle %x Send Command to OMX State %x %x",handle,type, error);
1467 if (!CommandFinished(handle,OMX_CommandStateSet,type)) {
1475 int VideoOMX::EnablePort(OMX_HANDLETYPE handle,OMX_U32 port,bool wait) //needs to be called with locked mutex
1477 OMX_ERRORTYPE error;
1478 error=OMX_SendCommand(handle,OMX_CommandPortEnable,port,0);
1479 if (error!=OMX_ErrorNone){
1480 Log::getInstance()->log("Video", Log::DEBUG, "handle %x Send Command to enable port %x %x",handle,port, error);
1484 if (!wait) return 1;
1485 if (!CommandFinished(handle,OMX_CommandPortEnable,port)) {
1493 int VideoOMX::DisablePort(OMX_HANDLETYPE handle,OMX_U32 port,bool wait) //needs to be called with locked mutex
1495 OMX_ERRORTYPE error;
1496 error=OMX_SendCommand(handle,OMX_CommandPortDisable,port,0);
1497 if (error!=OMX_ErrorNone){
1498 Log::getInstance()->log("Video", Log::DEBUG, "handle %x Send Command to disable port %x %x",handle,port, error);
1502 if (!wait) return 1;
1503 if (!CommandFinished(handle,OMX_CommandPortDisable,port)) {
1511 int VideoOMX::WaitForEvent(OMX_HANDLETYPE handle,OMX_U32 event) //needs to be called with locked mutex
1515 omx_event_mutex.Lock();
1516 list<VPE_OMX_EVENT>::iterator itty=omx_events.begin();
1517 while (itty!=omx_events.end()) {
1519 VPE_OMX_EVENT current=*itty;
1520 if (current.handle==handle) { //this is ours
1521 if (current.event_type==OMX_EventError) {
1522 omx_events.erase(itty);
1523 omx_event_mutex.Unlock();
1524 Log::getInstance()->log("Video", Log::DEBUG, "WaitForEvent Finished on Error");
1527 } else if (current.event_type==event) {
1528 omx_events.erase(itty);
1529 omx_event_mutex.Unlock();
1530 Log::getInstance()->log("Video", Log::DEBUG, "WaitForEvent Finished Completed");
1537 omx_event_mutex.Unlock();
1542 Log::getInstance()->log("Video", Log::DEBUG, "WaitForEvent waited too long %x %x",handle,event);
1547 int VideoOMX::clearEvents()
1549 omx_event_mutex.Lock();
1551 omx_event_mutex.Unlock();
1557 int VideoOMX::CommandFinished(OMX_HANDLETYPE handle,OMX_U32 command,OMX_U32 data2) //needs to be called with locked mutex
1561 omx_event_mutex.Lock();
1562 list<VPE_OMX_EVENT>::iterator itty=omx_events.begin();
1563 while (itty!=omx_events.end()) {
1565 VPE_OMX_EVENT current=*itty;
1566 if (current.handle==handle) { //this is ours
1567 if (current.event_type==OMX_EventError) {
1568 omx_events.erase(itty);
1569 omx_event_mutex.Unlock();
1570 Log::getInstance()->log("Video", Log::DEBUG, "Command Finished on Error %x",current.data1);
1573 } else if (current.event_type==OMX_EventCmdComplete && current.data1==command && current.data2==data2) {
1574 omx_events.erase(itty);
1575 omx_event_mutex.Unlock();
1576 //Log::getInstance()->log("Video", Log::DEBUG, "Command Finished Completed");
1583 omx_event_mutex.Unlock();
1588 Log::getInstance()->log("Video", Log::DEBUG, "CommandFinished waited too long %x %x %x",handle,command, data2);
1597 int VideoOMX::PrepareInputBufsOMX() //needs to be called with locked mutex
1599 OMX_ERRORTYPE error;
1600 OMX_PARAM_PORTDEFINITIONTYPE port_def_type;
1601 memset(&port_def_type,0,sizeof(port_def_type));
1602 port_def_type.nSize=sizeof(port_def_type);
1603 port_def_type.nVersion.nVersion=OMX_VERSION;
1604 port_def_type.nPortIndex=omx_codec_input_port;
1606 error=OMX_GetParameter(omx_vid_dec,OMX_IndexParamPortDefinition, &port_def_type);
1608 if (error!=OMX_ErrorNone){
1609 Log::getInstance()->log("Video", Log::DEBUG, "Get OMX OMX_IndexParamPortDefinition failed %x", error);
1611 /* Log::getInstance()->log("Video", Log::DEBUG, "Port para %d %d %d %d %d %d %d", port_def_type.nBufferCountActual,
1612 port_def_type.nBufferCountMin,port_def_type.nBufferSize,port_def_type.bEnabled,port_def_type.bPopulated,
1613 port_def_type.bBuffersContiguous,port_def_type.nBufferAlignment);*/
1615 port_def_type.nBufferCountActual=100;
1616 port_def_type.nBufferSize=max(port_def_type.nBufferSize,200000); // for transcoder important
1618 error=OMX_SetParameter(omx_vid_dec,OMX_IndexParamPortDefinition, &port_def_type);
1620 if (error!=OMX_ErrorNone){
1621 Log::getInstance()->log("Video", Log::DEBUG, "Set OMX OMX_IndexParamPortDefinition failed %x", error);
1625 error=OMX_SendCommand(omx_vid_dec,OMX_CommandPortEnable,omx_codec_input_port,0);
1626 if (error!=OMX_ErrorNone){
1627 Log::getInstance()->log("Video", Log::DEBUG, "Prepare Input bufs Send Command to enable port %x", error);
1631 input_bufs_omx_mutex.Lock();
1632 for (unsigned int i=0; i< port_def_type.nBufferCountActual;i++) {
1634 // unsigned char* new_buffer_data=(unsigned char*)malloc(port_def_type.nbufferSize);
1635 OMX_BUFFERHEADERTYPE *buf_head=NULL;
1636 /* error=OMX_Usebuffer(omx_vid_dec,&buf_head,omx_codec_input_port,NULL,port_def_type.nbufferSize,new_buffer_data);
1637 if (error!=OMX_ErrorNone){
1638 Log::getInstance()->log("Video", Log::DEBUG, "Use OMX_Usebuffer failed %x", error);
1639 input_bufs_omx_mutex.Unlock();
1642 error=OMX_AllocateBuffer(omx_vid_dec,&buf_head,omx_codec_input_port,NULL,port_def_type.nBufferSize);
1643 if (error!=OMX_ErrorNone){
1644 Log::getInstance()->log("Video", Log::DEBUG, "Use OMX_AllocateBuffer failed %x", error);
1645 input_bufs_omx_mutex.Unlock();
1648 input_bufs_omx_all.push_back(buf_head);
1649 input_bufs_omx_free.push_back(buf_head);
1651 omx_first_frame=true;
1654 cur_input_buf_omx=NULL;
1655 input_bufs_omx_mutex.Unlock();
1658 Log::getInstance()->log("Video", Log::DEBUG, "PrepareInputBufsOMX mark3");
1659 if (!CommandFinished(omx_vid_dec,OMX_CommandPortEnable,omx_codec_input_port)) {
1662 Log::getInstance()->log("Video", Log::DEBUG, "PrepareInputBufsOMX mark4");
1667 int VideoOMX::DestroyInputBufsOMX() //need s to be called with locked mutex
1669 OMX_ERRORTYPE error;
1671 cur_input_buf_omx=NULL;
1672 input_bufs_omx_mutex.Lock();
1673 for (int i=0; i< input_bufs_omx_all.size();i++) {
1674 // free(input_bufs_omx_all[i]->pBuffer);
1675 // input_bufs_omx_all[i]->pBuffer=NULL;
1676 error=OMX_FreeBuffer(omx_vid_dec,omx_codec_input_port,input_bufs_omx_all[i]);
1677 if (error!=OMX_ErrorNone){
1678 Log::getInstance()->log("Video", Log::DEBUG, "Use OMX_FreeBuffer failed %x", error);
1679 input_bufs_omx_mutex.Unlock();
1684 input_bufs_omx_all.clear();
1685 input_bufs_omx_free.clear();
1686 input_bufs_omx_mutex.Unlock();
1691 int VideoOMX::FlushRenderingPipe()
1693 OMX_ERRORTYPE error;
1697 error = OMX_SendCommand(omx_vid_dec, OMX_CommandFlush,
1698 omx_codec_output_port, NULL);
1699 if (error != OMX_ErrorNone) {
1700 Log::getInstance()->log("Video", Log::DEBUG,
1701 "OMX_Flush codec out 1 failed %x", error);
1705 error = OMX_SendCommand(omx_vid_sched, OMX_CommandFlush,
1706 omx_shed_input_port, NULL);
1707 if (error != OMX_ErrorNone) {
1708 Log::getInstance()->log("Video", Log::DEBUG,
1709 "OMX_Flush shed in 2 failed %x", error);
1713 if (!CommandFinished(omx_vid_dec, OMX_CommandFlush,
1714 omx_codec_output_port)) {
1715 Log::getInstance()->log("Video", Log::DEBUG,
1716 "flush cmd codec 3 failed");
1719 if (!CommandFinished(omx_vid_sched, OMX_CommandFlush,
1720 omx_shed_input_port)) {
1721 Log::getInstance()->log("Video", Log::DEBUG,
1722 "flush cmd shed 4 failed");
1725 error = OMX_SendCommand(omx_vid_dec, OMX_CommandFlush,
1726 omx_codec_output_port, NULL);
1727 if (error != OMX_ErrorNone) {
1728 Log::getInstance()->log("Video", Log::DEBUG,
1729 "OMX_Flush codec out 5 failed %x", error);
1733 error = OMX_SendCommand(omx_vid_deint, OMX_CommandFlush,
1734 omx_deint_input_port, NULL);
1735 if (error != OMX_ErrorNone) {
1736 Log::getInstance()->log("Video", Log::DEBUG,
1737 "OMX_Flush deint in 6 failed %x", error);
1741 if (!CommandFinished(omx_vid_dec, OMX_CommandFlush,
1742 omx_codec_output_port)) {
1743 Log::getInstance()->log("Video", Log::DEBUG,
1744 "flush cmd codec 7 failed");
1747 if (!CommandFinished(omx_vid_deint, OMX_CommandFlush,
1748 omx_deint_input_port)) {
1749 Log::getInstance()->log("Video", Log::DEBUG,
1750 "flush cmd deint 8 failed");
1754 error = OMX_SendCommand(omx_vid_deint, OMX_CommandFlush,
1755 omx_deint_output_port, NULL);
1756 if (error != OMX_ErrorNone) {
1757 Log::getInstance()->log("Video", Log::DEBUG,
1758 "OMX_Flush deint out 9 failed %x", error);
1762 error = OMX_SendCommand(omx_vid_sched, OMX_CommandFlush,
1763 omx_shed_input_port, NULL);
1764 if (error != OMX_ErrorNone) {
1765 Log::getInstance()->log("Video", Log::DEBUG,
1766 "OMX_Flush shed in 10 failed %x", error);
1770 if (!CommandFinished(omx_vid_deint, OMX_CommandFlush,
1771 omx_deint_output_port)) {
1772 Log::getInstance()->log("Video", Log::DEBUG,
1773 "flush cmd deint 11 failed");
1776 if (!CommandFinished(omx_vid_sched, OMX_CommandFlush,
1777 omx_shed_input_port)) {
1778 Log::getInstance()->log("Video", Log::DEBUG,
1779 "flush cmd shed 12 failed");
1789 error = OMX_SendCommand(omx_vid_rend, OMX_CommandFlush,
1790 omx_rend_input_port, NULL);
1791 if (error != OMX_ErrorNone) {
1792 Log::getInstance()->log("Video", Log::DEBUG,
1793 "OMX_Flush rend in failed %x", error);
1797 error = OMX_SendCommand(omx_vid_sched, OMX_CommandFlush,
1798 omx_shed_output_port, NULL);
1799 if (error != OMX_ErrorNone) {
1800 Log::getInstance()->log("Video", Log::DEBUG,
1801 "OMX_Flush shed out failed %x", error);
1807 if (!CommandFinished(omx_vid_rend, OMX_CommandFlush,
1808 omx_rend_input_port)) {
1809 Log::getInstance()->log("Video", Log::DEBUG,
1810 "flush cmd shed rend failed");
1813 if (!CommandFinished(omx_vid_sched, OMX_CommandFlush,
1814 omx_shed_output_port)) {
1815 Log::getInstance()->log("Video", Log::DEBUG,
1816 "flush cmd shed rend failed");
1821 int VideoOMX::DeAllocateCodecsOMX()
1823 OMX_ERRORTYPE error;
1825 Log::getInstance()->log("Video", Log::DEBUG, "enter deallocatecodecsomx");
1827 if (cur_input_buf_omx) {
1828 cur_input_buf_omx->nFlags|=OMX_BUFFERFLAG_EOS;
1829 OMX_ERRORTYPE error=ProtOMXEmptyThisBuffer(omx_vid_dec,cur_input_buf_omx);
1830 if (error!=OMX_ErrorNone) {
1831 Log::getInstance()->log("Video", Log::DEBUG, "OMX_EmptyThisBuffer failed %x", error);
1834 cur_input_buf_omx=NULL;//write out old data
1839 // first stop the omx elements
1840 if (!ChangeComponentState(omx_vid_dec,OMX_StateIdle)) {
1841 Log::getInstance()->log("Video", Log::DEBUG, "vid_dec ChangeComponentState");
1844 clock_mutex.Unlock();
1850 if (!ChangeComponentState(omx_vid_deint, OMX_StateIdle)) {
1851 Log::getInstance()->log("Video", Log::DEBUG,
1852 "vid_deint ChangeComponentState");
1858 if (!ChangeComponentState(omx_vid_sched,OMX_StateIdle)) {
1859 Log::getInstance()->log("Video", Log::DEBUG, "vid_shed ChangeComponentState");
1863 if (!ChangeComponentState(omx_vid_rend,OMX_StateIdle)) {
1864 Log::getInstance()->log("Video", Log::DEBUG, "vid_rend ChangeComponentState");
1870 // TODO proper deinit sequence
1871 // first flush all buffers
1872 FlushRenderingPipe();
1878 error=OMX_SendCommand(omx_clock,OMX_CommandFlush, omx_clock_output_port, NULL);
1879 if (error!=OMX_ErrorNone){
1880 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Flush clock out failed %x", error);
1884 error=OMX_SendCommand(omx_vid_sched,OMX_CommandFlush, omx_shed_clock_port, NULL);
1885 if (error!=OMX_ErrorNone){
1886 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Flush shed clock failed %x", error);
1890 if (!CommandFinished(omx_clock,OMX_CommandFlush,omx_clock_output_port) ||
1891 !CommandFinished(omx_vid_sched,OMX_CommandFlush,omx_shed_clock_port)) {
1892 Log::getInstance()->log("Video", Log::DEBUG, "flush cmd clock shed failed");
1898 error = OMX_SendCommand(omx_vid_dec, OMX_CommandFlush,
1899 omx_codec_input_port, NULL);
1900 if (error != OMX_ErrorNone) {
1901 Log::getInstance()->log("Video", Log::DEBUG,
1902 "OMX_Flush codec out failed %x", error);
1909 DestroyInputBufsOMX();
1912 if (!DisablePort(omx_vid_sched,omx_shed_output_port,true)) {
1913 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 2 ");
1915 if (!DisablePort(omx_vid_rend,omx_rend_input_port,true)) {
1916 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 1");
1922 if (!DisablePort(omx_vid_dec,omx_codec_output_port,true)) {
1923 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 6");
1928 if (!DisablePort(omx_vid_dec,omx_codec_input_port,true)) {
1929 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 7");
1933 if (!DisablePort(omx_vid_deint,omx_deint_output_port,true)) {
1934 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 6a");
1939 if (!DisablePort(omx_vid_deint,omx_deint_input_port,true)) {
1940 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 7a");
1946 if (!DisablePort(omx_vid_sched,omx_shed_input_port,true)) {
1947 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 3");
1950 if (!DisablePort(omx_vid_sched,omx_shed_clock_port,true)) {
1951 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 4");
1954 if (!DisablePort(omx_clock,omx_clock_output_port,true)) {
1955 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 5");
1961 error=OMX_SetupTunnel(omx_vid_dec,omx_codec_output_port,NULL,NULL);
1962 if (error!=OMX_ErrorNone) {
1963 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1968 error=OMX_SetupTunnel(omx_vid_deint,omx_deint_input_port,NULL,NULL);
1969 if (error!=OMX_ErrorNone) {
1970 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1974 error=OMX_SetupTunnel(omx_vid_deint,omx_deint_output_port,NULL,NULL);
1975 if (error!=OMX_ErrorNone) {
1976 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1980 error=OMX_SetupTunnel(omx_vid_sched,omx_shed_input_port,NULL,NULL);
1981 if (error!=OMX_ErrorNone) {
1982 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1987 error=OMX_SetupTunnel(omx_vid_sched,omx_shed_output_port,NULL,NULL);
1988 if (error!=OMX_ErrorNone) {
1989 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1993 error=OMX_SetupTunnel(omx_vid_rend,omx_rend_input_port,NULL,NULL);
1994 if (error!=OMX_ErrorNone) {
1995 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
2000 error=OMX_SetupTunnel(omx_clock,omx_clock_output_port,NULL,NULL);
2001 if (error!=OMX_ErrorNone) {
2002 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
2006 error=OMX_SetupTunnel(omx_vid_sched,omx_shed_clock_port,NULL,NULL);
2007 if (error!=OMX_ErrorNone) {
2008 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
2015 error=OMX_FreeHandle(omx_vid_dec);
2016 error=OMX_FreeHandle(omx_vid_sched);
2017 error=OMX_FreeHandle(omx_vid_rend);
2018 if (dodeint) error=OMX_FreeHandle(omx_vid_deint);
2020 clock_mutex.Unlock();
2022 if (error!=OMX_ErrorNone) {
2023 Log::getInstance()->log("Video", Log::DEBUG, "FreeHandle failed %d", error);
2025 } else clock_mutex.Unlock();
2026 Log::getInstance()->log("Video", Log::DEBUG, "leave deallocate codecs OMX");
2032 void VideoOMX::destroyClock()
2035 if (clock_references>0) {
2037 if (clock_references==0) {
2038 OMX_ERRORTYPE error;
2039 Log::getInstance()->log("Video", Log::DEBUG, "destroy omx clock");
2040 error=OMX_FreeHandle(omx_clock);
2041 if (error!=OMX_ErrorNone) {
2042 Log::getInstance()->log("Video", Log::DEBUG, "FreeHandle failed %d", error);
2047 clock_mutex.Unlock();
2051 int VideoOMX::stop()
2053 if (!initted) return 0;
2056 //Check if libav mode
2057 DeAllocateCodecsOMX();
2062 // if (ioctl(fdVideo, AV_SET_VID_STOP, 0) != 0) return 0;
2066 int VideoOMX::reset()
2068 if (!initted) return 0;
2071 DeAllocateCodecsOMX();
2072 // if (ioctl(fdVideo, AV_SET_VID_RESET, 0x11) != 0) return 0;
2076 int VideoOMX::pause()
2078 if (!initted) return 0;
2079 Log::getInstance()->log("Video", Log::DEBUG, "enter pause");
2082 //maybe also change omx clock?
2083 //pausetimecode=GetCurrentSystemTime();
2089 int VideoOMX::unPause() // FIXME get rid - same as play!! Not here!
2091 if (!initted) return 0;
2092 Log::getInstance()->log("Video", Log::DEBUG, "enter unpause");
2095 // playbacktimeoffset+=GetCurrentSystemTime()-pausetimecode;
2096 paused=false; // may be also change omx clock
2102 int VideoOMX::fastForward()
2104 if (!initted) return 0;
2106 // if (ioctl(fdVideo, AV_SET_VID_libavWD, 1) != 0) return 0;
2110 int VideoOMX::unFastForward()
2112 if (!initted) return 0;
2114 // if (ioctl(fdVideo, AV_SET_VID_RESET, 0x11) != 0) return 0; // don't need this.
2116 //// if (ioctl(fdVideo, AV_SET_VID_PLAY, 0) != 0) return 0;
2120 int VideoOMX::attachFrameBuffer()
2122 if (!initted) return 0;
2124 // if (ioctl(fdVideo, AV_SET_VID_FB, 0) != 0) return 0;
2128 int VideoOMX::blank(void)
2130 // if (ioctl(fdVideo, AV_SET_VID_FB, 1) != 0) return 0;
2131 // if (ioctl(fdVideo, AV_SET_VID_FB, 0) != 0) return 0;
2135 ULLONG VideoOMX::getCurrentTimestamp() {
2138 long long ncur_clock_time = cur_clock_time;
2142 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldcancelstate);
2143 pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldcanceltype);
2145 OMX_ERRORTYPE error;
2146 OMX_TIME_CONFIG_CLOCKSTATETYPE clock_conf;
2147 memset(&clock_conf, 0, sizeof(clock_conf));
2148 clock_conf.nSize = sizeof(clock_conf);
2149 clock_conf.nVersion.nVersion = OMX_VERSION;
2150 error= OMX_GetConfig(omx_clock, OMX_IndexConfigTimeClockState,
2152 if (error != OMX_ErrorNone) {
2153 Log::getInstance()->log("Video", Log::DEBUG,"getCurrentTimestamp IndexConfigTimeClockState failed %x",error);
2156 if (clock_conf.eState == OMX_TIME_ClockStateRunning) {
2158 OMX_TIME_CONFIG_TIMESTAMPTYPE cur_time_stamp;
2159 memset(&cur_time_stamp, 0, sizeof(cur_time_stamp));
2160 cur_time_stamp.nSize = sizeof(cur_time_stamp);
2161 cur_time_stamp.nVersion.nVersion = OMX_VERSION;
2162 cur_time_stamp.nPortIndex = omx_clock_output_port;
2163 error = OMX_GetConfig(omx_clock, OMX_IndexConfigTimeCurrentMediaTime,
2165 if (error != OMX_ErrorNone) {
2166 Log::getInstance()->log("Video",Log::DEBUG,"getCurrentTimestamp OMX_IndexConfigTimeCurrentMediaTime failed %x",error);
2168 long long temp = cur_time_stamp.nTimestamp.nLowPart
2169 | ((long long) cur_time_stamp.nTimestamp.nHighPart << 32);
2170 ncur_clock_time = cur_clock_time = temp * 10LL;
2173 clock_mutex.Unlock();
2174 pthread_setcancelstate(oldcancelstate, NULL);
2175 pthread_setcanceltype(oldcanceltype, NULL);
2178 //ncur_clock_time -= startoffset;
2179 ncur_clock_time -= lastreftimeOMX;
2180 long long result = lastreftimePTS;
2181 if (lastreftimePTS==0) return 0; // invalid time
2182 result += (long long) (ncur_clock_time / 10000LL * 90LL);
2184 result = (1LL << 33) - result;
2185 //Log::getInstance()->log("Video", Log::DEBUG,"getCurrentTimestamp %lld %lld %lld %lld %lld %lld",ncur_clock_time,cur_clock_time,lastreftimeOMX,lastreftimePTS,result,startoffset);
2193 ULONG VideoOMX::timecodeToFrameNumber(ULLONG timecode)
2195 if (format == PAL) return (ULONG)(((double)timecode / (double)90000) * (double)25);
2196 else return (ULONG)(((double)timecode / (double)90000) * (double)30);
2201 int VideoOMX::test()
2206 // return ioctl(fdVideo, AV_SET_VID_STC, &stc);
2213 int VideoOMX::test2()
2221 long long VideoOMX::SetStartOffset(long long curreftime, bool *rsync)
2225 startoffset=curreftime;//offset is set for audio
2227 offsetvideonotset=false;
2229 if (offsetvideonotset) {
2230 offsetvideonotset=false;
2233 if ( (curreftime-lastrefvideotime)>10000000LL
2234 || (curreftime-lastrefvideotime)<-10000000LL) {//if pts jumps to big resync
2235 startoffset+=curreftime-lastrefvideotime;
2236 lastrefaudiotime+=curreftime-lastrefvideotime;
2238 offsetaudionotset=true;
2245 lastrefvideotime=curreftime;
2251 long long VideoOMX::SetStartAudioOffset(long long curreftime, bool *rsync)
2255 startoffset=curreftime;
2257 offsetaudionotset=false;
2259 if (offsetaudionotset) {
2260 offsetaudionotset=false;
2263 if ( (curreftime-lastrefaudiotime)>10000000LL
2264 || (curreftime-lastrefaudiotime)<-10000000LL) {//if pts jumps to big resync
2265 startoffset+=curreftime-lastrefaudiotime;
2266 lastrefvideotime+=curreftime-lastrefaudiotime;
2268 offsetvideonotset=true;
2274 lastrefaudiotime=curreftime;
2279 void VideoOMX::ResetTimeOffsets() {
2280 offsetnotset=true; //called from demuxer
2281 offsetvideonotset=true;
2282 offsetaudionotset=true;
2291 void VideoOMX::DeinterlaceFix()
2295 Demuxer* demux=Demuxer::getInstance();
2296 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldcancelstate);
2297 pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldcanceltype);
2299 OMX_ERRORTYPE error;
2300 OMX_PARAM_PORTDEFINITIONTYPE port_def_type;
2301 memset(&port_def_type,0,sizeof(port_def_type));
2302 port_def_type.nSize=sizeof(port_def_type);
2303 port_def_type.nVersion.nVersion=OMX_VERSION;
2304 port_def_type.nPortIndex=omx_codec_output_port;
2306 error=OMX_GetParameter(omx_vid_dec,OMX_IndexParamPortDefinition, &port_def_type);
2307 if (error != OMX_ErrorNone) {
2308 Log::getInstance()->log("Video", Log::DEBUG,
2309 "OMX_IndexParamPortDefinition fix failed %x", error);
2310 clock_mutex.Unlock();
2314 if (port_def_type.format.video.nFrameWidth == demux->getHorizontalSize()
2315 && port_def_type.format.video.nFrameHeight == demux->getVerticalSize()){
2316 Log::getInstance()->log("Video", Log::DEBUG,
2317 "Deinit first frame fix");
2318 deint_first_frame=false;
2320 WaitForEvent(omx_vid_dec,OMX_EventPortSettingsChanged);
2321 DisablePort(omx_vid_dec,omx_codec_output_port,false);
2322 DisablePort(omx_vid_sched,omx_shed_input_port,false);
2323 DisablePort(omx_vid_deint,omx_deint_output_port,false);
2324 DisablePort(omx_vid_deint,omx_deint_input_port,false);
2326 port_def_type.nPortIndex=omx_deint_input_port;
2327 error = OMX_SetParameter(omx_vid_deint, OMX_IndexParamPortDefinition,
2329 if (error != OMX_ErrorNone) {
2330 Log::getInstance()->log("Video", Log::DEBUG,
2331 "Set OMX_IndexParamPortDefinition1 failed %x", error);
2332 clock_mutex.Unlock();
2333 pthread_setcancelstate(oldcancelstate, NULL);
2334 pthread_setcanceltype(oldcanceltype, NULL);
2338 port_def_type.nPortIndex=omx_deint_output_port;
2339 error = OMX_SetParameter(omx_vid_deint, OMX_IndexParamPortDefinition,
2341 if (error != OMX_ErrorNone) {
2342 Log::getInstance()->log("Video", Log::DEBUG,
2343 "Set OMX_IndexParamPortDefinition2 failed %x", error);
2344 clock_mutex.Unlock();
2345 pthread_setcancelstate(oldcancelstate, NULL);
2346 pthread_setcanceltype(oldcanceltype, NULL);
2351 EnablePort(omx_vid_dec,omx_codec_output_port,false);
2352 EnablePort(omx_vid_deint,omx_deint_input_port,false);
2353 EnablePort(omx_vid_deint,omx_deint_output_port,false);
2354 EnablePort(omx_vid_sched,omx_shed_input_port,false);
2356 clock_mutex.Unlock();
2357 pthread_setcancelstate(oldcancelstate, NULL);
2358 pthread_setcanceltype(oldcanceltype, NULL);
2365 void VideoOMX::PutBufferToPres(OMX_BUFFERHEADERTYPE* buffer)
2368 OMX_ERRORTYPE error = ProtOMXEmptyThisBuffer(omx_vid_dec, buffer);
2369 if (error != OMX_ErrorNone) {
2370 Log::getInstance()->log("Video", Log::DEBUG,
2371 "OMX_EmptyThisBuffer failed %x", error);
2373 if (deint_first_frame && dodeint) DeinterlaceFix();
2377 OMX_ERRORTYPE VideoOMX::ProtOMXEmptyThisBuffer(OMX_HANDLETYPE handle, OMX_BUFFERHEADERTYPE* buffer)
2379 // protect the call to empty this buffer
2382 /* long long temp =buffer->nTimeStamp.nLowPart
2383 | ((long long) buffer->nTimeStamp.nHighPart << 32);*/
2385 //pthread_testcancel();
2386 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldcancelstate);
2387 pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldcanceltype);
2390 /* OMX_ERRORTYPE error;
2391 OMX_TIME_CONFIG_TIMESTAMPTYPE timestamp;
2392 memset(×tamp, 0, sizeof(timestamp));
2393 timestamp.nSize = sizeof(timestamp);
2394 timestamp.nVersion.nVersion = OMX_VERSION;
2395 timestamp.nPortIndex =omx_clock_output_port;
2397 error = OMX_GetConfig(omx_clock, OMX_IndexConfigTimeCurrentMediaTime,
2400 if (error != OMX_ErrorNone) {
2401 Log::getInstance()->log("Audio", Log::DEBUG,
2402 "Init OMX_IndexConfigAudioRenderingLatencyfailed %x %d", error,
2403 omx_rend_input_port);
2405 long long temp2 =timestamp.nTimestamp.nLowPart
2406 | ((long long) timestamp.nTimestamp.nHighPart << 32);
2409 Log::getInstance()->log("Video", Log::NOTICE, "OMXETB %x %lld %lld %x",handle,temp,temp2,buffer->nFlags);*/
2410 OMX_ERRORTYPE ret_val;
2411 ret_val=OMX_EmptyThisBuffer(handle,buffer);
2412 clock_mutex.Unlock();
2413 pthread_setcancelstate(oldcancelstate, NULL);
2414 pthread_setcanceltype(oldcanceltype, NULL);
2415 //pthread_testcancel();
2419 bool VideoOMX::detectIFrame(const UCHAR *buffer,unsigned int length)
2421 const UCHAR* curbuf=buffer;
2422 const UCHAR* curbufend=buffer+length;
2423 unsigned int detector=0xFFFFFFFF;
2424 bool gotaud=false; // have seen access unit delimiter
2426 while (curbuf!=curbufend) {
2430 if (detector==0x00000109) {
2432 detector=0xFFFFFFFF;
2433 } else if (gotaud &&detector==0x00000001) {
2435 if (curbuf!=curbufend) {
2436 unsigned char picttype=(*curbuf)& 0x1F;
2437 return picttype==0x07;
2441 if (detector==0x00000100) {
2443 if (curbuf==curbufend) return false;
2445 if (curbuf==curbufend) return false;
2446 unsigned char picttype=((*curbuf) >> 3) & 0x07;
2453 return false; // no frame found
2456 void VideoOMX::PrepareMediaSample(const MediaPacketList& mplist,UINT samplepos)
2459 mediapackets.clear();
2460 list<MediaPacket>::const_iterator begin=mplist.begin();
2461 list<MediaPacket>::const_iterator itty=mplist.begin();
2462 advance(itty,min(mplist.size(),10));
2463 mediapackets.insert(mediapackets.begin(),begin,itty);//front
2467 UINT VideoOMX::DeliverMediaSample(UCHAR* buffer, UINT *samplepos)
2470 while (consumed<mediapackets.size()) {
2471 DeliverMediaPacket(mediapackets[consumed], buffer, samplepos);
2472 if (*samplepos == mediapackets[consumed].length) {
2476 } else return consumed;
2481 UINT VideoOMX::DeliverMediaPacket(MediaPacket packet,
2482 const UCHAR* buffer,
2485 if (packet.type == MPTYPE_VIDEO_H264)
2495 //Later add fail back code for libav
2497 *samplepos+=packet.length;
2498 return packet.length;
2502 if (!omx_running) return 0; // if we are not runnig do not do this
2503 if (paused) return 0; //Block if we pause
2505 // Log::getInstance()->log("Video", Log::DEBUG, "DMP mark 1");
2506 //Log::getInstance()->log("Video", Log::DEBUG, "DeliverMediaPacketOMX time %lld",packet.presentation_time);
2509 OMX_ERRORTYPE error;
2511 /*First Check, if we have an video sample*/
2515 return 0; //Not in iframe mode!
2519 if (packet.disconti) {
2521 if (cur_input_buf_omx) {
2522 PutBufferToPres(cur_input_buf_omx);
2523 cur_input_buf_omx=NULL;
2526 //Log::getInstance()->log("Video", Log::DEBUG, "DMP mark 2");
2527 /*Inspect PES-Header */
2529 // OMX_STATETYPE temp_state;
2530 // OMX_GetState(omx_vid_dec,&temp_state);
2532 if (*samplepos==0) {//stripheader
2533 headerstrip=buffer[packet.pos_buffer+8]+9/*is this right*/;
2534 // if (h264) Log::getInstance()->log("Video", Log::DEBUG, "PES info %x %x %x %x",
2535 // buffer[packet.pos_buffer+0],buffer[packet.pos_buffer+1],buffer[packet.pos_buffer+2],buffer[packet.pos_buffer+3]);
2536 *samplepos+=headerstrip;
2537 if ( packet.synched ) {
2538 if (!firstsynched) {
2539 //Log::getInstance()->log("Video", Log::DEBUG, "DMP mark 2a");
2540 // check if this is an I frame, the decoder does not like non I frames at startup!
2541 if (!detectIFrame(buffer,packet.length)) {
2542 *samplepos=packet.length;//if we have not processed at least one
2543 //Log::getInstance()->log("Video", Log::DEBUG, "DMP mark 3");
2544 return packet.length;//synched packet ignore it!
2547 if (cur_input_buf_omx) {
2548 //Log::getInstance()->log("Video", Log::DEBUG, "DMP mark 4a");
2549 cur_input_buf_omx->nFlags|=OMX_BUFFERFLAG_ENDOFFRAME;
2550 PutBufferToPres(cur_input_buf_omx);
2551 cur_input_buf_omx=NULL;//write out old data
2553 //Log::getInstance()->log("Video", Log::DEBUG, "DMP mark 4b");
2558 if (!firstsynched) {//
2559 *samplepos=packet.length;//if we have not processed at least one
2560 //Log::getInstance()->log("Video", Log::DEBUG, "DMP mark 5");
2561 return packet.length;//synched packet ignore it!
2565 //Log::getInstance()->log("Video", Log::DEBUG, "DMP mark 6");
2566 if (!cur_input_buf_omx) {
2567 input_bufs_omx_mutex.Lock();
2568 if (input_bufs_omx_free.size()==0) {
2569 input_bufs_omx_mutex.Unlock();
2570 //Log::getInstance()->log("Video", Log::DEBUG, "Deliver MediaPacket no free sample");
2571 //Log::getInstance()->log("Video", Log::DEBUG, "DMP mark 7");
2572 return 0; // we do not have a free media sample
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=intToOMXTicks(0);
2579 input_bufs_omx_free.pop_front();
2580 input_bufs_omx_mutex.Unlock();
2586 if (cur_input_buf_omx->nFilledLen==0) {//will only be changed on first packet
2587 if (packet.synched) {
2588 // Log::getInstance()->log("Video", Log::DEBUG, "packet synched marker");
2590 //lastreftimePTS=packet.pts;
2591 if (omx_first_frame) { // TODO time
2592 cur_input_buf_omx->nFlags=OMX_BUFFERFLAG_STARTTIME;
2593 Log::getInstance()->log("Video", Log::DEBUG, "Starttime");
2594 omx_first_frame=false;
2596 cur_input_buf_omx->nFlags=0;
2597 //cur_input_buf_omx->nFlags|=OMX_BUFFERFLAG_TIME_UNKNOWN;
2599 lastreftimeOMX=packet.presentation_time;
2600 // Log::getInstance()->log("Video", Log::DEBUG, "Time code %lld pts %lld", lastreftimeOMX,packet.pts);
2601 lastreftimePTS=packet.pts;
2602 cur_input_buf_omx->nTimeStamp=intToOMXTicks(lastreftimeOMX/10LL); // the clock component is faulty;
2606 cur_input_buf_omx->nFlags=OMX_BUFFERFLAG_TIME_UNKNOWN;
2607 cur_input_buf_omx->nTimeStamp=intToOMXTicks(0);
2608 //Log::getInstance()->log("Video", Log::DEBUG, "packet unsynched marker");
2610 if (packet.disconti) cur_input_buf_omx->nFlags|=OMX_BUFFERFLAG_DISCONTINUITY;
2615 unsigned int haveToCopy=packet.length-*samplepos;
2616 //Log::getInstance()->log("Video", Log::DEBUG, "DMP mark 8");
2618 while (haveToCopy> (cur_input_buf_omx->nAllocLen-cur_input_buf_omx->nFilledLen)) {
2619 //Log::getInstance()->log("Video", Log::DEBUG, "Big buffer %d %d %d",packet.length,cur_input_buf_omx->nAllocLen,cur_input_buf_omx->nFilledLen);
2620 unsigned int cancopy=cur_input_buf_omx->nAllocLen-cur_input_buf_omx->nFilledLen;
2621 memcpy(cur_input_buf_omx->pBuffer+cur_input_buf_omx->nFilledLen,buffer+packet.pos_buffer+*samplepos,cancopy);
2622 haveToCopy-=cancopy;
2623 cur_input_buf_omx->nFilledLen+=cancopy;
2624 *samplepos+=cancopy;
2625 // push old buffer out
2626 //Log::getInstance()->log("Video", Log::DEBUG, "DMP mark 9");
2628 PutBufferToPres(cur_input_buf_omx);
2629 cur_input_buf_omx=NULL;
2631 input_bufs_omx_mutex.Lock();
2632 if (input_bufs_omx_free.size()==0) {
2633 input_bufs_omx_mutex.Unlock();
2634 // Log::getInstance()->log("Video", Log::DEBUG, "Deliver MediaPacket no free sample2");
2635 return *samplepos; // we do not have a free media sample
2637 cur_input_buf_omx=input_bufs_omx_free.front();
2638 cur_input_buf_omx->nFilledLen=0;
2639 cur_input_buf_omx->nOffset=0;
2640 cur_input_buf_omx->nFlags=OMX_BUFFERFLAG_TIME_UNKNOWN;
2641 cur_input_buf_omx->nTimeStamp=intToOMXTicks(0);
2642 input_bufs_omx_free.pop_front();
2643 input_bufs_omx_mutex.Unlock();
2644 //Log::getInstance()->log("Video", Log::DEBUG, "DMP mark 10");
2647 memcpy(cur_input_buf_omx->pBuffer+cur_input_buf_omx->nFilledLen,
2648 buffer+packet.pos_buffer+*samplepos,haveToCopy);
2649 cur_input_buf_omx->nFilledLen+=haveToCopy;
2651 // Log::getInstance()->log("Video", Log::DEBUG, "DMP mark 11");
2653 *samplepos+=haveToCopy;
2662 bool VideoOMX::displayIFrame(const UCHAR* buffer, UINT length) {
2663 if (!omx_running) return false;
2665 EnterIframePlayback();
2667 int haveToCopy = length;
2669 if (!cur_input_buf_omx) {
2670 input_bufs_omx_mutex.Lock();
2671 if (input_bufs_omx_free.size() == 0) {
2672 input_bufs_omx_mutex.Unlock();
2673 // Log::getInstance()->log("Video", Log::DEBUG,
2674 // "Deliver MediaPacket no free sample");
2675 return false; // we do not have a free media sample
2678 cur_input_buf_omx = input_bufs_omx_free.front();
2679 cur_input_buf_omx->nFilledLen = 0;
2680 cur_input_buf_omx->nOffset = 0;
2681 cur_input_buf_omx->nTimeStamp = intToOMXTicks(0);
2682 input_bufs_omx_free.pop_front();
2683 input_bufs_omx_mutex.Unlock();
2687 unsigned int pattern, packet_length;
2688 unsigned int headerstrip = 0;
2693 //Now we strip the pes header
2694 pattern = (buffer[0] << 16) | (buffer[1] << 8) | (buffer[2]);
2695 while (read_pos + 7 <= length) {
2696 pattern = ((pattern << 8) & 0xFFFFFFFF) | buffer[read_pos + 3];
2697 if (pattern < 0x000001E0 || pattern > 0x000001EF) {
2701 headerstrip = buffer[read_pos + 8] + 9/*is this right*/;
2702 packet_length = ((buffer[read_pos + 4] << 8)
2703 | (buffer[read_pos + 5])) + 6;
2704 if (read_pos + packet_length > length)
2707 if ((headerstrip < packet_length)
2708 && (cur_input_buf_omx->nFilledLen + packet_length
2709 - headerstrip) > cur_input_buf_omx->nAllocLen) {
2711 cur_input_buf_omx->nFlags = OMX_BUFFERFLAG_STARTTIME;
2714 cur_input_buf_omx->nFlags
2715 |= OMX_BUFFERFLAG_TIME_UNKNOWN;
2718 cur_input_buf_omx->nTimeStamp = intToOMXTicks(0);
2719 PutBufferToPres(cur_input_buf_omx);
2720 cur_input_buf_omx = NULL;
2722 if (!cur_input_buf_omx) {
2724 while (count < 100 && omx_running && iframemode) {
2727 input_bufs_omx_mutex.Lock();
2728 if (input_bufs_omx_free.size() == 0) {
2729 input_bufs_omx_mutex.Unlock();
2730 // Log::getInstance()->log("Video", Log::DEBUG,
2731 // "Ifrane no free sample");
2733 if (!omx_running) return false;
2736 cur_input_buf_omx = input_bufs_omx_free.front();
2737 cur_input_buf_omx->nFilledLen = 0;
2738 cur_input_buf_omx->nOffset = 0;
2739 cur_input_buf_omx->nTimeStamp = intToOMXTicks(0);
2740 cur_input_buf_omx->nFlags|= OMX_BUFFERFLAG_TIME_UNKNOWN;
2741 input_bufs_omx_free.pop_front();
2742 input_bufs_omx_mutex.Unlock();
2745 if (!cur_input_buf_omx)
2750 if (packet_length > headerstrip) {
2752 cur_input_buf_omx->pBuffer
2753 + cur_input_buf_omx->nFilledLen,
2754 buffer + read_pos + headerstrip,
2755 packet_length - headerstrip);
2756 cur_input_buf_omx->nFilledLen += packet_length
2759 read_pos += packet_length;
2761 pattern = (buffer[read_pos] << 16)
2762 | (buffer[read_pos + 1] << 8) | (buffer[read_pos + 2]);
2768 cur_input_buf_omx->nFlags = OMX_BUFFERFLAG_STARTTIME;
2771 cur_input_buf_omx->nFlags |= OMX_BUFFERFLAG_TIME_UNKNOWN;
2774 cur_input_buf_omx->nTimeStamp = intToOMXTicks(0);
2776 PutBufferToPres(cur_input_buf_omx);
2777 cur_input_buf_omx = NULL;
2780 MILLISLEEP(40); //Block a bit
2784 int VideoOMX::EnterIframePlayback()
2786 Log::getInstance()->log("Video", Log::DEBUG,
2787 "EnterIframePlayback");
2788 if (cur_input_buf_omx) {
2789 PutBufferToPres(cur_input_buf_omx);
2790 cur_input_buf_omx = NULL;
2792 Log::getInstance()->log("Video", Log::DEBUG,
2793 "EnterIframePlayback 2");
2794 ((AudioOMX*)Audio::getInstance())->DeAllocateCodecsOMX();
2795 DeAllocateCodecsOMX();
2796 AllocateCodecsOMX();
2797 Log::getInstance()->log("Video", Log::DEBUG,
2798 "leave IframePlayback");