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(COMPOSITERGB)) { 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; }
89 if (error != OMX_ErrorNone) {
90 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX failed %x",
109 OMX_ERRORTYPE VideoOMX::EventHandler_OMX(OMX_IN OMX_HANDLETYPE handle,OMX_IN OMX_PTR appdata,
110 OMX_IN OMX_EVENTTYPE event_type,OMX_IN OMX_U32 data1,
111 OMX_IN OMX_U32 data2,OMX_IN OMX_PTR event_data) {
113 Log::getInstance()->log("Video", Log::NOTICE, "eventHandler %x %x %x %x %x",handle,event_type,data1,data2,event_data);
115 struct VPE_OMX_EVENT new_event;
116 new_event.handle=handle;
117 new_event.appdata=appdata;
118 new_event.event_type=event_type;
119 new_event.data1=data1;
120 new_event.data2=data2;
121 new_event.event_data=event_data;
123 VideoOMX *video=(VideoOMX *)getInstance();
124 video->AddOmxEvent(new_event);
126 /* switch (event_type) {
127 case OMX_EventCmdComplete: {
132 return OMX_ErrorNone;
136 void VideoOMX::AddOmxEvent(VPE_OMX_EVENT new_event)
138 omx_event_mutex.Lock();
139 omx_events.push_back(new_event);
140 omx_event_mutex.Unlock();
144 OMX_ERRORTYPE VideoOMX::EmptyBufferDone_OMX(OMX_IN OMX_HANDLETYPE hcomp,OMX_IN OMX_PTR appdata,OMX_IN OMX_BUFFERHEADERTYPE* buffer){
146 //Log::getInstance()->log("Video", Log::NOTICE, "EmptyBufferDone");
147 VideoOMX *video=(VideoOMX *)getInstance();
148 /* long long temp =buffer->nTimeStamp.nLowPart
149 | ((long long) buffer->nTimeStamp.nHighPart << 32);
150 Log::getInstance()->log("Video", Log::NOTICE, "EBD Video %lld %x",temp,buffer->nFlags);*/
151 video->ReturnEmptyOMXBuffer(buffer);
152 return OMX_ErrorNone;
156 void VideoOMX::ReturnEmptyOMXBuffer(OMX_BUFFERHEADERTYPE* buffer){
157 input_bufs_omx_mutex.Lock();
158 //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());
159 input_bufs_omx_free.push_back(buffer);
160 //Log::getInstance()->log("Video", Log::NOTICE, "ReturnEmptyOMXBuffer %d",input_bufs_omx_free.size());
161 input_bufs_omx_mutex.Unlock();
164 OMX_ERRORTYPE VideoOMX::FillBufferDone_OMX(OMX_IN OMX_HANDLETYPE hcomp, OMX_IN OMX_PTR appdata,OMX_IN OMX_BUFFERHEADERTYPE* buffer) {
165 Log::getInstance()->log("Video", Log::NOTICE, "FillBufferDone");
166 return OMX_ErrorNone;
171 int VideoOMX::shutdown()
173 if (!initted) return 0;
175 Log::getInstance()->log("Video", Log::NOTICE, "Shutdown video module");
177 DeAllocateCodecsOMX();
179 //vc_tv_show_info(0); // back to console
182 struct fb_var_screeninfo screeninfo;
183 fd_fbset=open("/dev/fb0",O_RDONLY);
185 Log::getInstance()->log("Video", Log::CRIT, "Could not open frame buffer device %d", fd_fbset);
188 if (ioctl(fd_fbset, FBIOGET_VSCREENINFO, &screeninfo)){
190 Log::getInstance()->log("Video", Log::CRIT, "Could not FBIOGET_VSCREENINFO frame buffer device");
193 screeninfo.bits_per_pixel=8;
194 if (ioctl(fd_fbset, FBIOPUT_VSCREENINFO, &screeninfo)){
195 Log::getInstance()->log("Video", Log::CRIT, "Could not FBIOPUT_VSCREENINFO frame buffer device");
197 screeninfo.bits_per_pixel=16;
198 if (ioctl(fd_fbset, FBIOPUT_VSCREENINFO, &screeninfo)){
199 Log::getInstance()->log("Video", Log::CRIT, "Could not FBIOPUT_VSCREENINFO frame buffer device");
207 bool VideoOMX::loadOptionsfromServer(VDR* vdr)
209 Log::getInstance()->log("Video", Log::DEBUG, "VideoOMX config load");
210 char *name=vdr->configLoad("VideoOMX","SDDeinterlacing");
213 if (STRCASECMP(name, "None") == 0) {
215 }/* else if (STRCASECMP(name, "LineDouble") == 0) {
217 }*/ else if (STRCASECMP(name, "Advanced") == 0) {
219 } /*else if (STRCASECMP(name, "Crazy") == 0) {
220 deinterlace = 3; // this does not activate deinterlacing but a image filter, just for fun
222 Log::getInstance()->log("Video", Log::DEBUG, "Set deinterlacing to %s %d",name,deinterlace);
229 bool VideoOMX::handleOptionChanges(Option* option)
231 if (Video::handleOptionChanges(option))
233 switch (option->id) {
235 if (STRCASECMP(option->options[option->userSetChoice], "None") == 0) {
237 } /*else if (STRCASECMP(option->options[option->userSetChoice], "LineDouble")
240 }*/ else if (STRCASECMP(option->options[option->userSetChoice], "Advanced")
243 } /*else if (STRCASECMP(option->options[option->userSetChoice], "Crazy")
247 Log::getInstance()->log("Video", Log::DEBUG, "Set deinterlacing to %s %d",option->options[option->userSetChoice],deinterlace);
256 bool VideoOMX::saveOptionstoServer()
259 switch (deinterlace) {
261 VDR::getInstance()->configSave("VideoOMX","SDDeinterlacing", "None");
264 VDR::getInstance()->configSave("VideoOMX","SDDeinterlacing", "LineDouble");
267 VDR::getInstance()->configSave("VideoOMX","SDDeinterlacing", "Advanced");
270 VDR::getInstance()->configSave("VideoOMX","SDDeinterlacing", "Crazy");
277 /*Option(UINT id, const char* displayText, const char* configSection, const char* configKey, UINT optionType,
278 UINT numChoices, UINT defaultChoice, UINT startInt,
279 const char * const * options, const char * const * optionkeys = NULL, AbstractOption* handler=NULL);*/
281 bool VideoOMX::addOptionsToPanes(int panenumber,Options *options,WOptionPane* pane)
283 if (!Video::addOptionsToPanes(panenumber,options,pane)) return false;
289 static const char* deinterlaceopts[]={"None",/*"LineDouble",*/"Advanced"/*,"Crazy"*/};
290 option = new Option(1,tr("SD Deinterlacing"), "VideoOMX","SDDeinterlacing",Option::TYPE_TEXT,/*4,2*/2,1,0,deinterlaceopts,NULL,false,this);
291 options->push_back(option);
292 pane->addOptionLine(option);
300 int VideoOMX::setTVsize(UCHAR ttvsize)
304 // Override the aspect ratio usage, temporarily use to set the video chip mode
305 if (!setAspectRatio(tvsize)) { shutdown(); return 0; }
307 if ((fdVideo = open("/dev/vdec_dev", O_WRONLY)) < 0) return 0;
308 if (!setSource()) { shutdown(); return 0; }
309 if (!attachFramebuffer()) { shutdown(); return 0; }
311 // Reopening the fd causes the scart aspect line to go back to 4:3
312 // Set this again to the same as the tv screen size
313 if (!setAspectRatio(tvsize)) { shutdown(); return 0; }
315 // mode == LETTERBOX is invalid if the TV is widescreen
316 if (tvsize == ASPECT16X9) setMode(NORMAL);
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)) return 0;
341 // selectVideoMode(0);
346 void VideoOMX::selectVideoMode(int interlaced)
349 TV_SUPPORTED_MODE_T all_supp_modes[TV_MAX_SUPPORTED_MODES];
350 HDMI_RES_GROUP_T pref_group;
351 TV_SUPPORTED_MODE_T *mymode=NULL;
352 TV_SUPPORTED_MODE_T *mymode_second_best=NULL;
353 bool got_optimum=false;
355 int all_my_modes=vc_tv_hdmi_get_supported_modes(HDMI_RES_GROUP_CEA,
356 all_supp_modes,TV_MAX_SUPPORTED_MODES,
357 &pref_group,&pref_mode);
359 if (format==PAL)target_fps=50;
360 else if (format==NTSC) target_fps=60;
361 if (all_my_modes<=0) {
362 Log::getInstance()->log("Video", Log::NOTICE, "No CEA modes found abort");
366 //Now first determine native resolution
367 int native_width=1920;
368 int native_height=1080;
369 for (int i=0;i<all_my_modes;i++) {
370 if (all_supp_modes[i].native) {
371 mymode=all_supp_modes+i;
372 Log::getInstance()->log("Video", Log::NOTICE, "Found native mode %dx%d %d Hz i: %d",
373 mymode->width,mymode->height,mymode->frame_rate,mymode->scan_mode);
374 native_width=mymode->width;
375 native_height=mymode->height;
379 //Now find the mode which matches best
380 for (int i=0;i<all_my_modes;i++) {
381 TV_SUPPORTED_MODE_T *curmode=all_supp_modes+i;
382 if (curmode->width==native_width &&
383 curmode->height==native_height &&
384 curmode->frame_rate==target_fps) {
385 if(curmode->scan_mode==interlaced) {
388 Log::getInstance()->log("Video", Log::NOTICE, "Found optimum mode %dx%d %d Hz i: %d",
389 mymode->width,mymode->height,mymode->frame_rate,mymode->scan_mode);
391 mymode_second_best=curmode;
392 Log::getInstance()->log("Video", Log::NOTICE, "Found close to optimum mode %dx%d %d Hz i: %d",
393 mymode_second_best->width,mymode_second_best->height,
394 mymode_second_best->frame_rate,mymode_second_best->scan_mode);
400 Remote::getInstance()->shutdown();
402 Log::getInstance()->log("Video", Log::NOTICE, "Switch to optimum mode");
403 vc_tv_hdmi_power_on_explicit(HDMI_MODE_HDMI,HDMI_RES_GROUP_CEA,mymode->code);
404 } else if (mymode_second_best) {
405 Log::getInstance()->log("Video", Log::NOTICE, "Switch to close to optimum mode");
406 vc_tv_hdmi_power_on_explicit(HDMI_MODE_HDMI,HDMI_RES_GROUP_CEA,mymode_second_best->code);
408 Log::getInstance()->log("Video", Log::NOTICE, "Switch to prefered mode");
409 vc_tv_hdmi_power_on_best(1920,1080,target_fps,interlaced?HDMI_INTERLACED:HDMI_NONINTERLACED,
410 (EDID_MODE_MATCH_FLAG_T)(HDMI_MODE_MATCH_FRAMERATE|HDMI_MODE_MATCH_RESOLUTION|HDMI_MODE_MATCH_SCANMODE));
412 Remote::getInstance()->init("");
421 int VideoOMX::setConnection(UCHAR tconnection)
423 if (!initted) return 0;
424 if ((tconnection != COMPOSITERGB) && (tconnection != SVIDEO)) return 0;
425 connection = tconnection;
427 // if (ioctl(fdVideo, AV_SET_VID_OUTPUT, connection) != 0) return 0;
431 int VideoOMX::setAspectRatio(UCHAR taspectRatio)
433 if (!initted) return 0;
434 if ((taspectRatio != ASPECT4X3) && (taspectRatio != ASPECT16X9)) return 0;
435 aspectRatio = taspectRatio;
437 Log::getInstance()->log("Video", Log::DEBUG, "Setting aspect to %i", aspectRatio);
439 // if (ioctl(fdVideo, AV_SET_VID_RATIO, aspectRatio) != 0) return 0;
443 int VideoOMX::setMode(UCHAR tmode)
445 if (!initted) return 0;
452 void VideoOMX::updateMode()
457 OMX_CONFIG_DISPLAYREGIONTYPE dispconf;
458 memset(&dispconf, 0, sizeof(dispconf));
459 dispconf.nSize = sizeof(dispconf);
460 dispconf.nVersion.nVersion = OMX_VERSION;
461 dispconf.nPortIndex = omx_rend_input_port;
463 dispconf.set = OMX_DISPLAY_SET_LAYER;
464 error = OMX_SetParameter(omx_vid_rend, OMX_IndexConfigDisplayRegion,
466 if (error != OMX_ErrorNone) {
467 Log::getInstance()->log("Video", Log::DEBUG,
468 "Set OMX_IndexConfigDisplayRegion1 failed %x", error);
469 clock_mutex.Unlock();
472 dispconf.set = OMX_DISPLAY_SET_FULLSCREEN;
473 if (mode != QUARTER && mode != EIGHTH) {
475 dispconf.fullscreen = OMX_TRUE;
477 dispconf.fullscreen = OMX_FALSE;
479 error = OMX_SetParameter(omx_vid_rend, OMX_IndexConfigDisplayRegion,
481 if (error != OMX_ErrorNone) {
482 Log::getInstance()->log("Video", Log::DEBUG,
483 "Set OMX_IndexConfigDisplayRegion2 failed %x", error);
484 clock_mutex.Unlock();
488 dispconf.set = OMX_DISPLAY_SET_MODE;
489 if (mode != QUARTER && mode != EIGHTH) {
490 dispconf.mode = (mode == NORMAL) ? OMX_DISPLAY_MODE_FILL
491 : OMX_DISPLAY_MODE_LETTERBOX;
493 dispconf.mode = OMX_DISPLAY_MODE_LETTERBOX;
495 error = OMX_SetParameter(omx_vid_rend, OMX_IndexConfigDisplayRegion,
497 if (error != OMX_ErrorNone) {
498 Log::getInstance()->log("Video", Log::DEBUG,
499 "Set OMX_IndexConfigDisplayRegion3 failed %x", error);
500 clock_mutex.Unlock();
504 if (mode == QUARTER || mode == EIGHTH) {
505 unsigned int display_width, display_height;
506 display_width = display_height = 0;
507 if (graphics_get_display_size(0, &display_width, &display_height)
509 Log::getInstance()->log("OSD", Log::WARN,
510 "Getting display size failed! (BCM API) ");
511 clock_mutex.Unlock();
514 //UnSetFullscreen with window
515 dispconf.set = OMX_DISPLAY_SET_DEST_RECT;
516 dispconf.dest_rect.x_offset
517 = (int) (xpos * ((float) display_width));
518 dispconf.dest_rect.y_offset = (int) (ypos
519 * ((float) display_height));
520 if (mode == QUARTER) {
521 dispconf.dest_rect.width = display_width >> 1;
522 dispconf.dest_rect.height = display_height >> 1;
523 } else if (mode == EIGHTH) {
524 dispconf.dest_rect.width = display_width >> 2;
525 dispconf.dest_rect.height = display_height >> 2;
527 error = OMX_SetParameter(omx_vid_rend,
528 OMX_IndexConfigDisplayRegion, &dispconf);
529 if (error != OMX_ErrorNone) {
530 Log::getInstance()->log("Video", Log::DEBUG,
531 "Set OMX_IndexConfigDisplayRegion failed %x", error);
532 clock_mutex.Unlock();
538 clock_mutex.Unlock();
541 int VideoOMX::signalOff()
544 Log::getInstance()->log("Video", Log::NOTICE, "signalOff");
546 Osd::getInstance()->stopUpdate(); // turn off drawing thread
551 int VideoOMX::signalOn()
554 Log::getInstance()->log("Video", Log::NOTICE, "signalOn");
555 Osd::getInstance()->shutdown();
557 Osd::getInstance()->restore();
558 Osd::getInstance()->init((void*)"");
559 BoxStack::getInstance()->redrawAllBoxes();
566 int VideoOMX::setSource()
568 if (!initted) return 0;
570 // What does this do...
571 // if (ioctl(fdVideo, AV_SET_VID_SRC, 1) != 0) return 0;
575 int VideoOMX::setPosition(int x, int y) {
578 xpos = ((float) x*2.f) / ((float) screenWidth);
579 ypos = ((float) y*2.f) / ((float) screenHeight);
587 if (!initted) return 0;
589 // if (ioctl(fdVideo, AV_SET_VID_SYNC, 2) != 0) return 0;
594 int VideoOMX::play() {
598 Log::getInstance()->log("Video", Log::DEBUG, "enter play");
600 if (AllocateCodecsOMX()) {
602 // Otherwise fall back to libav
606 Log::getInstance()->log("Video", Log::NOTICE,
607 "Allocate Codecs OMX failed assume h264 unsupported");
610 Log::getInstance()->log("Video", Log::NOTICE,
611 "Allocate Codecs OMX failed assume mpeg2 unsupported");
620 int VideoOMX::initClock()
624 if (clock_references==0)
627 static OMX_CALLBACKTYPE callbacks= {&EventHandler_OMX,&EmptyBufferDone_OMX,&FillBufferDone_OMX};
630 error=OMX_GetHandle(&omx_clock,VPE_OMX_CLOCK,NULL,&callbacks);
632 if (error!=OMX_ErrorNone) {
633 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX clock failed %x", error);
634 clock_mutex.Unlock();
635 DeAllocateCodecsOMX();
639 /* TODO Clock config to separate method */
640 OMX_PORT_PARAM_TYPE p_param;
641 memset(&p_param,0,sizeof(p_param));
642 p_param.nSize=sizeof(p_param);
643 p_param.nVersion.nVersion=OMX_VERSION;
644 error=OMX_GetParameter(omx_clock,OMX_IndexParamOtherInit,&p_param);
645 if (error!=OMX_ErrorNone) {
646 Log::getInstance()->log("Video", Log::DEBUG, "Init clock OMX_GetParameter failed %x", error);
647 clock_mutex.Unlock();
648 DeAllocateCodecsOMX();
651 omx_clock_output_port=p_param.nStartPortNumber;
653 for (unsigned int i=0;i<p_param.nPorts;i++) {
654 if (!DisablePort(omx_clock,p_param.nStartPortNumber+i,true) ) {
655 Log::getInstance()->log("Video", Log::DEBUG, "Disable Ports OMX clock failed %d",i);
656 clock_mutex.Unlock();
657 DeAllocateCodecsOMX();
666 Log::getInstance()->log("Video", Log::DEBUG, "init omx clock %x %x",this,omx_clock);
668 clock_mutex.Unlock();
672 int VideoOMX::getClockAudioandInit(OMX_HANDLETYPE *p_omx_clock,OMX_U32 *p_omx_clock_output_port)
676 *p_omx_clock_output_port=0;
683 OMX_TIME_CONFIG_ACTIVEREFCLOCKTYPE refclock;
684 memset(&refclock,0,sizeof(refclock));
685 refclock.nSize=sizeof(refclock);
686 refclock.nVersion.nVersion=OMX_VERSION;
688 refclock.eClock=OMX_TIME_RefClockAudio;
690 //refclock.eClock=OMX_TIME_RefClockVideo;
691 error=OMX_SetConfig(omx_clock,OMX_IndexConfigTimeActiveRefClock,&refclock);
692 if (error!=OMX_ErrorNone){
693 Log::getInstance()->log("Video", Log::DEBUG, "Clock OMX_IndexConfigTimeActiveRefClock failed %x", error);
694 clock_mutex.Unlock();
695 DeAllocateCodecsOMX();
699 OMX_PORT_PARAM_TYPE p_param;
700 memset(&p_param,0,sizeof(p_param));
701 p_param.nSize=sizeof(p_param);
702 p_param.nVersion.nVersion=OMX_VERSION;
703 error=OMX_GetParameter(omx_clock,OMX_IndexParamOtherInit,&p_param);
704 if (error!=OMX_ErrorNone){
705 Log::getInstance()->log("Video", Log::DEBUG, "Init clock OMX_GetParameter failed %x", error);
706 clock_mutex.Unlock();
707 DeAllocateCodecsOMX();
711 OMX_TIME_CONFIG_CLOCKSTATETYPE clock_conf;
712 memset(&clock_conf,0,sizeof(clock_conf));
713 clock_conf.nSize=sizeof(clock_conf);
714 clock_conf.nVersion.nVersion=OMX_VERSION;
715 clock_conf.eState=OMX_TIME_ClockStateWaitingForStartTime;
716 clock_conf.nStartTime=intToOMXTicks(0);
717 clock_conf.nOffset=intToOMXTicks(0);
718 if (clock_references==1) clock_conf.nWaitMask=OMX_CLOCKPORT1;
719 else clock_conf.nWaitMask=OMX_CLOCKPORT0|OMX_CLOCKPORT1;
720 error=OMX_SetConfig(omx_clock,OMX_IndexConfigTimeClockState,&clock_conf);
721 if (error!=OMX_ErrorNone) {
722 Log::getInstance()->log("Video", Log::DEBUG, "AuI Clock IndexConfigTimeClockState failed %x", error);
726 *p_omx_clock_output_port=p_param.nStartPortNumber+1;
727 *p_omx_clock=omx_clock;
728 clock_mutex.Unlock();
732 int VideoOMX::getClockVideoandInit()
741 if (clock_references==1) { // only if no audio is attached to this clock!
742 OMX_TIME_CONFIG_ACTIVEREFCLOCKTYPE refclock;
743 memset(&refclock,0,sizeof(refclock));
744 refclock.nSize=sizeof(refclock);
745 refclock.nVersion.nVersion=OMX_VERSION;
747 //refclock.eClock=OMX_TIME_RefClockAudio;
749 refclock.eClock=OMX_TIME_RefClockVideo;
750 error=OMX_SetConfig(omx_clock,OMX_IndexConfigTimeActiveRefClock,&refclock);
751 if (error!=OMX_ErrorNone) {
752 Log::getInstance()->log("Video", Log::DEBUG, "Clock OMX_IndexConfigTimeActiveRefClock failed %x", error);
753 clock_mutex.Unlock();
754 DeAllocateCodecsOMX();
759 OMX_PORT_PARAM_TYPE p_param;
760 memset(&p_param,0,sizeof(p_param));
761 p_param.nSize=sizeof(p_param);
762 p_param.nVersion.nVersion=OMX_VERSION;
763 error=OMX_GetParameter(omx_clock,OMX_IndexParamOtherInit,&p_param);
764 if (error!=OMX_ErrorNone){
765 Log::getInstance()->log("Video", Log::DEBUG, "Init clock OMX_GetParameter failed %x", error);
766 clock_mutex.Unlock();
767 DeAllocateCodecsOMX();
772 OMX_TIME_CONFIG_CLOCKSTATETYPE clock_conf;
773 memset(&clock_conf,0,sizeof(clock_conf));
774 clock_conf.nSize=sizeof(clock_conf);
775 clock_conf.nVersion.nVersion=OMX_VERSION;
776 clock_conf.eState=OMX_TIME_ClockStateStopped;
777 clock_conf.nStartTime=intToOMXTicks(0);
778 clock_conf.nOffset=intToOMXTicks(0);
779 error=OMX_SetConfig(omx_clock,OMX_IndexConfigTimeClockState,&clock_conf);
780 if (error!=OMX_ErrorNone) {
781 Log::getInstance()->log("Video", Log::DEBUG, "VuI Clock IndexConfigTimeClockState failed %x", error);
785 memset(&clock_conf,0,sizeof(clock_conf));
786 clock_conf.nSize=sizeof(clock_conf);
787 clock_conf.nVersion.nVersion=OMX_VERSION;
788 clock_conf.eState=OMX_TIME_ClockStateWaitingForStartTime;
789 clock_conf.nStartTime=intToOMXTicks(0);
790 clock_conf.nOffset=intToOMXTicks(0);
791 if (clock_references==1) clock_conf.nWaitMask=OMX_CLOCKPORT0;
792 else clock_conf.nWaitMask=OMX_CLOCKPORT0|OMX_CLOCKPORT1;
793 error=OMX_SetConfig(omx_clock,OMX_IndexConfigTimeClockState,&clock_conf);
794 if (error!=OMX_ErrorNone) {
795 Log::getInstance()->log("Video", Log::DEBUG, "VuI Clock IndexConfigTimeClockState failed %x", error);
798 omx_clock_output_port=p_param.nStartPortNumber;
799 clock_mutex.Unlock();
804 void VideoOMX::clockUnpause()
808 if (clock_references>0) {
809 OMX_TIME_CONFIG_CLOCKSTATETYPE clock_conf;
810 memset(&clock_conf,0,sizeof(clock_conf));
811 clock_conf.nSize=sizeof(clock_conf);
812 clock_conf.nVersion.nVersion=OMX_VERSION;
813 clock_conf.eState=OMX_TIME_ClockStateRunning;
814 clock_conf.nStartTime=intToOMXTicks(0);
815 clock_conf.nOffset=intToOMXTicks(0);
816 clock_conf.nWaitMask=OMX_CLOCKPORT1;
817 error=OMX_SetConfig(omx_clock,OMX_IndexConfigTimeClockState,&clock_conf);
818 if (error!=OMX_ErrorNone) {
819 Log::getInstance()->log("Video", Log::DEBUG, "ClockUnpause IndexConfigTimeClockState failed %x", error);
822 clock_mutex.Unlock();
826 void VideoOMX::clockPause()
830 if (clock_references>0) {
831 OMX_TIME_CONFIG_CLOCKSTATETYPE clock_conf;
832 memset(&clock_conf,0,sizeof(clock_conf));
833 clock_conf.nSize=sizeof(clock_conf);
834 clock_conf.nVersion.nVersion=OMX_VERSION;
835 clock_conf.eState=OMX_TIME_ClockStateStopped;
836 clock_conf.nStartTime=intToOMXTicks(0);
837 clock_conf.nOffset=intToOMXTicks(0);
838 clock_conf.nWaitMask=OMX_CLOCKPORT1;
839 error=OMX_SetConfig(omx_clock,OMX_IndexConfigTimeClockState,&clock_conf);
840 if (error!=OMX_ErrorNone) {
841 Log::getInstance()->log("Video", Log::DEBUG, "ClockUnpause IndexConfigTimeClockState failed %x", error);
844 clock_mutex.Unlock();
849 int VideoOMX::AllocateCodecsOMX()
852 static OMX_CALLBACKTYPE callbacks= {&EventHandler_OMX,&EmptyBufferDone_OMX,&FillBufferDone_OMX};
854 Demuxer* demux=Demuxer::getInstance();
857 deint_first_frame=false;
859 Log::getInstance()->log("Video", Log::NOTICE, "Allocate Codecs OMX");
860 //Clock, move later to audio including events
862 Log::getInstance()->log("Video", Log::NOTICE, "VideoType %d x %d i: %d", demux->getHorizontalSize(),demux->getVerticalSize(),demux->getInterlaced());
863 if (deinterlace!=0 && (demux->getHorizontalSize()<=720 ) && demux->getInterlaced()) { //only deinterlace SD material
865 deint_first_frame=true;
867 Log::getInstance()->log("Video", Log::NOTICE, "Deinterlacing activated %d",deinterlace);
872 if (!getClockVideoandInit()){
873 return 0;// get the clock and init it if necessary
878 Log::getInstance()->log("Video", Log::DEBUG, "idleClock failed");
886 error=OMX_GetHandle(&omx_vid_dec,VPE_OMX_H264_DECODER,NULL,&callbacks);
888 error=OMX_GetHandle(&omx_vid_dec,VPE_OMX_MPEG2_DECODER,NULL,&callbacks);
891 if (error!=OMX_ErrorNone){
892 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX video decoder failed %x", error);
893 clock_mutex.Unlock();
894 DeAllocateCodecsOMX();
899 Log::getInstance()->log("Video", Log::DEBUG, "Nmark3 ");
900 OMX_PORT_PARAM_TYPE p_param;
901 memset(&p_param,0,sizeof(p_param));
902 p_param.nSize=sizeof(p_param);
903 p_param.nVersion.nVersion=OMX_VERSION;
904 error=OMX_GetParameter(omx_vid_dec,OMX_IndexParamVideoInit,&p_param);
905 if (error!=OMX_ErrorNone){
906 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX h264 decoder OMX_GetParameter failed %x", error);
907 clock_mutex.Unlock();
908 DeAllocateCodecsOMX();
911 omx_codec_input_port=p_param.nStartPortNumber;
912 omx_codec_output_port=p_param.nStartPortNumber+1;
914 if (!DisablePort(omx_vid_dec,omx_codec_input_port) || !DisablePort(omx_vid_dec,omx_codec_output_port)) {
915 Log::getInstance()->log("Video", Log::DEBUG, "Disable Ports OMX video decoder failed");
916 clock_mutex.Unlock();
917 DeAllocateCodecsOMX();
921 Log::getInstance()->log("Video", Log::DEBUG, "Nmark4 ");
923 OMX_PARAM_BRCMVIDEODECODEERRORCONCEALMENTTYPE conceal;
924 memset(&conceal,0,sizeof(conceal));
925 conceal.nSize=sizeof(conceal);
926 conceal.nVersion.nVersion=OMX_VERSION;
927 conceal.bStartWithValidFrame=OMX_FALSE;
929 error=OMX_SetParameter(omx_vid_dec,OMX_IndexParamBrcmVideoDecodeErrorConcealment,&conceal);
930 if (error!=OMX_ErrorNone){
931 Log::getInstance()->log("Video", Log::DEBUG, "OMX_IndexParamBrcmVideoDecodeErrorConcealment failed %x", error);
932 clock_mutex.Unlock();
933 DeAllocateCodecsOMX();
938 error = OMX_GetHandle(&omx_vid_deint, VPE_OMX_VIDEO_DEINTERLACE, NULL,
940 if (error != OMX_ErrorNone) {
941 Log::getInstance()->log("Video", Log::DEBUG,
942 "Init OMX video deinterlacer failed %x", error);
943 clock_mutex.Unlock();
944 DeAllocateCodecsOMX();
948 error = OMX_GetParameter(omx_vid_deint, OMX_IndexParamImageInit,
950 if (error != OMX_ErrorNone) {
951 Log::getInstance()->log("Video", Log::DEBUG,
952 "Init OMX video deinterlacer OMX_GetParameter failed %x",
954 clock_mutex.Unlock();
955 DeAllocateCodecsOMX();
958 omx_deint_input_port = p_param.nStartPortNumber;
959 omx_deint_output_port = p_param.nStartPortNumber + 1;
961 if (!DisablePort(omx_vid_deint, omx_deint_input_port, true)
962 || !DisablePort(omx_vid_deint, omx_deint_output_port, true)) {
963 Log::getInstance()->log("Video", Log::DEBUG,
964 "Disable Ports OMX video deint failed");
965 clock_mutex.Unlock();
966 DeAllocateCodecsOMX();
973 error=OMX_GetHandle(&omx_vid_sched,VPE_OMX_VIDEO_SCHED,NULL,&callbacks);
974 if (error!=OMX_ErrorNone){
975 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX video scheduler failed %x", error);
976 clock_mutex.Unlock();
977 DeAllocateCodecsOMX();
983 error=OMX_GetParameter(omx_vid_sched,OMX_IndexParamVideoInit,&p_param);
984 if (error!=OMX_ErrorNone){
985 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX video scheduler OMX_GetParameter failed %x", error);
986 clock_mutex.Unlock();
987 DeAllocateCodecsOMX();
990 omx_shed_input_port=p_param.nStartPortNumber;
991 omx_shed_output_port=p_param.nStartPortNumber+1;
994 error=OMX_GetParameter(omx_vid_sched,OMX_IndexParamOtherInit,&p_param);
995 if (error!=OMX_ErrorNone){
996 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX video scheduler OMX_GetParameter failed %x", error);
997 clock_mutex.Unlock();
998 DeAllocateCodecsOMX();
1001 omx_shed_clock_port=p_param.nStartPortNumber;
1002 Log::getInstance()->log("Video", Log::DEBUG, "scheduler ports %d %d %d ",omx_shed_input_port,omx_shed_output_port,omx_shed_clock_port);
1005 if (!DisablePort(omx_vid_sched,omx_shed_input_port,true) || !DisablePort(omx_vid_sched,omx_shed_output_port,true)
1006 || !DisablePort(omx_vid_sched,omx_shed_clock_port,true)) {
1007 Log::getInstance()->log("Video", Log::DEBUG, "Disable Ports OMX video shed failed");
1008 clock_mutex.Unlock();
1009 DeAllocateCodecsOMX();
1014 error=OMX_GetHandle(&omx_vid_rend,VPE_OMX_VIDEO_REND,NULL,&callbacks);
1015 if (error!=OMX_ErrorNone){
1016 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX video rend failed %x", error);
1017 clock_mutex.Unlock();
1018 DeAllocateCodecsOMX();
1022 error=OMX_GetParameter(omx_vid_rend,OMX_IndexParamVideoInit,&p_param);
1023 if (error!=OMX_ErrorNone){
1024 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX video rend OMX_GetParameter failed %x", error);
1025 clock_mutex.Unlock();
1026 DeAllocateCodecsOMX();
1029 omx_rend_input_port=p_param.nStartPortNumber;
1030 //omx_rend_output_port=p_param.nStartPortNumber+1;
1033 if (!DisablePort(omx_vid_rend,omx_rend_input_port,true) /*|| !DisablePort(omx_vid_rend,omx_rend_output_port)*/
1035 Log::getInstance()->log("Video", Log::DEBUG, "Disable Ports OMX video rend failed");
1036 clock_mutex.Unlock();
1037 DeAllocateCodecsOMX();
1045 error=OMX_SetupTunnel(omx_clock,omx_clock_output_port,omx_vid_sched,omx_shed_clock_port);
1046 if (error!=OMX_ErrorNone){
1047 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);
1048 clock_mutex.Unlock();
1049 DeAllocateCodecsOMX();
1053 if (!EnablePort(omx_clock,omx_clock_output_port,false) || !EnablePort(omx_vid_sched,omx_shed_clock_port,false)
1055 Log::getInstance()->log("Video", Log::DEBUG, "Enable Ports OMX clock shed failed");
1056 clock_mutex.Unlock();
1057 DeAllocateCodecsOMX();
1062 Log::getInstance()->log("Video", Log::DEBUG, "mark2 ");
1063 if (!ChangeComponentState(omx_vid_sched,OMX_StateIdle)) {
1064 Log::getInstance()->log("Video", Log::DEBUG, "vid_sched idle ChangeComponentState");
1065 clock_mutex.Unlock();
1066 DeAllocateCodecsOMX();
1072 Log::getInstance()->log("Video", Log::DEBUG, "mark1 ");
1073 if ( !CommandFinished(omx_vid_sched,OMX_CommandPortEnable,omx_shed_clock_port)) {
1074 clock_mutex.Unlock();
1075 DeAllocateCodecsOMX();
1082 Log::getInstance()->log("Video", Log::DEBUG, "mark1 special ");
1083 if ( !CommandFinished(omx_clock,OMX_CommandPortEnable,omx_clock_output_port)) {
1084 clock_mutex.Unlock();
1085 DeAllocateCodecsOMX();
1091 Log::getInstance()->log("Video", Log::DEBUG, "mark1b ");
1094 /* error=OMX_SendCommand(omx_vid_dec,OMX_CommandStateSet,OMX_StateIdle,0);
1095 if (error!=OMX_ErrorNone){
1096 Log::getInstance()->log("Video", Log::DEBUG, "vid_dec Send Command to OMX State Idle %x", error);
1100 OMX_VIDEO_PARAM_PORTFORMATTYPE ft_type;
1101 memset(&ft_type,0,sizeof(ft_type));
1102 ft_type.nSize=sizeof(ft_type);
1103 ft_type.nVersion.nVersion=OMX_VERSION;
1105 ft_type.nPortIndex=omx_codec_input_port;
1107 ft_type.eCompressionFormat=OMX_VIDEO_CodingAVC;
1109 ft_type.eCompressionFormat=OMX_VIDEO_CodingMPEG2;
1114 ft_type.xFramerate=0*(1<<16);//25*(1<<16);//demux->getFrameRate()*(1<<16);
1115 Log::getInstance()->log("Video", Log::DEBUG, "Framerate: %d",demux->getFrameRate());
1116 error=OMX_SetParameter(omx_vid_dec,OMX_IndexParamVideoPortFormat,&ft_type);
1117 if (error!=OMX_ErrorNone){
1118 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX_IndexParamVideoPortFormat failed %x", error);
1119 clock_mutex.Unlock();
1120 DeAllocateCodecsOMX();
1125 if (!ChangeComponentState(omx_vid_dec,OMX_StateIdle)) {
1126 Log::getInstance()->log("Video", Log::DEBUG, "vid_dec ChangeComponentState");
1127 clock_mutex.Unlock();
1128 DeAllocateCodecsOMX();
1133 if (!PrepareInputBufsOMX()) {
1134 clock_mutex.Unlock();
1135 DeAllocateCodecsOMX();
1140 error=OMX_SetupTunnel(omx_vid_dec,omx_codec_output_port,omx_vid_sched,omx_shed_input_port);
1141 if (error!=OMX_ErrorNone){
1142 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel dec to sched failed %x", error);
1143 clock_mutex.Unlock();
1144 DeAllocateCodecsOMX();
1150 if (!EnablePort(omx_vid_dec,omx_codec_output_port,false) || !EnablePort(omx_vid_sched,omx_shed_input_port,false)
1152 Log::getInstance()->log("Video", Log::DEBUG, "Enable Ports OMX codec shed failed");
1153 clock_mutex.Unlock();
1154 DeAllocateCodecsOMX();
1158 if ( !CommandFinished(omx_vid_dec,OMX_CommandPortEnable,omx_codec_output_port) || !CommandFinished(omx_vid_sched,OMX_CommandPortEnable,omx_shed_input_port)) {
1159 clock_mutex.Unlock();
1160 DeAllocateCodecsOMX();
1166 error=OMX_SetupTunnel(omx_vid_dec,omx_codec_output_port,omx_vid_deint,omx_deint_input_port);
1167 if (error!=OMX_ErrorNone){
1168 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel dec to deint failed %x", error);
1169 clock_mutex.Unlock();
1170 DeAllocateCodecsOMX();
1176 if (!EnablePort(omx_vid_dec,omx_codec_output_port,false) || !EnablePort(omx_vid_deint,omx_deint_input_port,false)
1178 Log::getInstance()->log("Video", Log::DEBUG, "Enable Ports OMX codec deint failed");
1179 clock_mutex.Unlock();
1180 DeAllocateCodecsOMX();
1184 if ( !CommandFinished(omx_vid_dec,OMX_CommandPortEnable,omx_codec_output_port) || !CommandFinished(omx_vid_deint,OMX_CommandPortEnable,omx_deint_input_port)) {
1185 clock_mutex.Unlock();
1186 DeAllocateCodecsOMX();
1190 if (!ChangeComponentState(omx_vid_deint,OMX_StateIdle)) {
1191 Log::getInstance()->log("Video", Log::DEBUG, "vid_deint ChangeComponentState");
1192 clock_mutex.Unlock();
1193 DeAllocateCodecsOMX();
1197 OMX_CONFIG_IMAGEFILTERPARAMSTYPE imagefilter;
1198 memset(&imagefilter,0,sizeof(imagefilter));
1199 imagefilter.nSize=sizeof(imagefilter);
1200 imagefilter.nVersion.nVersion=OMX_VERSION;
1202 imagefilter.nPortIndex=omx_deint_output_port;
1203 imagefilter.nNumParams=1;
1204 imagefilter.nParams[0]=3;//???
1205 switch (deinterlace) {
1207 imagefilter.eImageFilter=OMX_ImageFilterDeInterlaceLineDouble; break;
1209 imagefilter.eImageFilter=OMX_ImageFilterDeInterlaceAdvanced; break;
1211 imagefilter.eImageFilter=OMX_ImageFilterFilm; break;
1215 error=OMX_SetConfig(omx_vid_deint,OMX_IndexConfigCommonImageFilterParameters,&imagefilter);
1216 if (error!=OMX_ErrorNone){
1217 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX_IndexConfigCommonImageFilterParameters failed %x", error);
1218 clock_mutex.Unlock();
1219 DeAllocateCodecsOMX();
1224 error=OMX_SetupTunnel(omx_vid_deint,omx_deint_output_port,omx_vid_sched,omx_shed_input_port);
1225 if (error!=OMX_ErrorNone){
1226 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel deint to sched failed %x", error);
1227 clock_mutex.Unlock();
1228 DeAllocateCodecsOMX();
1232 if (!EnablePort(omx_vid_deint,omx_deint_output_port,false) || !EnablePort(omx_vid_sched,omx_shed_input_port,false)
1234 Log::getInstance()->log("Video", Log::DEBUG, "Enable Ports OMX deint shed failed");
1235 clock_mutex.Unlock();
1236 DeAllocateCodecsOMX();
1240 if ( !CommandFinished(omx_vid_deint,OMX_CommandPortEnable,omx_deint_output_port) || !CommandFinished(omx_vid_sched,OMX_CommandPortEnable,omx_shed_input_port)) {
1241 clock_mutex.Unlock();
1242 DeAllocateCodecsOMX();
1248 if (!ChangeComponentState(omx_vid_dec,OMX_StateExecuting)) {
1249 Log::getInstance()->log("Video", Log::DEBUG, "omx_vid_dec ChangeComponentState Execute");
1250 clock_mutex.Unlock();
1251 DeAllocateCodecsOMX();
1255 error=OMX_SetupTunnel(omx_vid_sched,omx_shed_output_port,omx_vid_rend,omx_rend_input_port);
1256 if (error!=OMX_ErrorNone){
1257 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel sched to rend failed %x", error);
1258 clock_mutex.Unlock();
1259 DeAllocateCodecsOMX();
1263 if (!EnablePort(omx_vid_sched,omx_shed_output_port,false) || !EnablePort(omx_vid_rend,omx_rend_input_port,false)
1265 Log::getInstance()->log("Video", Log::DEBUG, "Enable Ports OMX shed rend failed");
1266 clock_mutex.Unlock();
1267 DeAllocateCodecsOMX();
1271 if (!CommandFinished(omx_vid_sched,OMX_CommandPortEnable,omx_shed_output_port)
1272 || !CommandFinished(omx_vid_rend,OMX_CommandPortEnable,omx_rend_input_port)) {
1273 clock_mutex.Unlock();
1274 DeAllocateCodecsOMX();
1278 if (!ChangeComponentState(omx_vid_rend,OMX_StateIdle)) {
1279 Log::getInstance()->log("Video", Log::DEBUG, "vid_rend ChangeComponentState");
1280 clock_mutex.Unlock();
1281 DeAllocateCodecsOMX();
1286 if (!ChangeComponentState(omx_vid_deint,OMX_StateExecuting)) {
1287 Log::getInstance()->log("Video", Log::DEBUG, "vid_vid_deint ChangeComponentState");
1288 clock_mutex.Unlock();
1289 DeAllocateCodecsOMX();
1292 DisablePort(omx_vid_deint,omx_deint_output_port,false);
1293 DisablePort(omx_vid_deint,omx_deint_input_port,false);
1296 if (!ChangeComponentState(omx_vid_sched,OMX_StateExecuting)) {
1297 Log::getInstance()->log("Video", Log::DEBUG, "omx_vid_sched ChangeComponentState Execute");
1298 clock_mutex.Unlock();
1299 DeAllocateCodecsOMX();
1303 if (!ChangeComponentState(omx_vid_rend,OMX_StateExecuting)) {
1304 Log::getInstance()->log("Video", Log::DEBUG, "omx_vid_rend ChangeComponentState Execute");
1305 clock_mutex.Unlock();
1306 DeAllocateCodecsOMX();
1311 /*OMX_CONFIG_DISPLAYREGIONTYPE dispconf;
1312 memset(&dispconf,0,sizeof(dispconf));
1313 dispconf.nSize=sizeof(dispconf);
1314 dispconf.nVersion.nVersion=OMX_VERSION;
1316 dispconf.nPortIndex=omx_rend_input_port;
1318 dispconf.set=OMX_DISPLAY_SET_LAYER ;
1320 error=OMX_SetConfig(omx_vid_rend,OMX_IndexConfigDisplayRegion,&dispconf);
1321 if (error!=OMX_ErrorNone){
1322 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX_IndexConfigDisplayRegion failed %x", error);
1323 clock_mutex.Unlock();
1324 DeAllocateCodecsOMX();
1328 /* dispconf.set=OMX_DISPLAY_SET_FULLSCREEN ;
1329 dispconf.fullscreen=OMX_FALSE;
1330 error=OMX_SetParameter(omx_vid_rend,OMX_IndexConfigDisplayRegion,&dispconf);
1331 if (error!=OMX_ErrorNone){
1332 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX_IndexConfigDisplayRegion failed %x", error);
1333 clock_mutex.Unlock();
1334 DeAllocateCodecsOMX();
1338 dispconf.set=OMX_DISPLAY_SET_DEST_RECT;
1339 dispconf.dest_rect.x_offset=100;
1340 dispconf.dest_rect.y_offset=100;
1341 dispconf.dest_rect.width=640;
1342 dispconf.dest_rect.height=480;
1343 error=OMX_SetParameter(omx_vid_rend,OMX_IndexConfigDisplayRegion,&dispconf);
1344 if (error!=OMX_ErrorNone){
1345 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX_IndexConfigDisplayRegion failed %x", error);
1346 clock_mutex.Unlock();
1347 DeAllocateCodecsOMX();
1352 //playbacktimeoffset=-GetCurrentSystemTime();
1356 clock_mutex.Unlock();
1359 setClockExecutingandRunning();
1368 int VideoOMX::idleClock()
1370 OMX_ERRORTYPE error;
1371 OMX_STATETYPE temp_state;
1373 OMX_GetState(omx_clock,&temp_state);
1375 if (temp_state!=OMX_StateIdle) {
1376 if (!ChangeComponentState(omx_clock,OMX_StateIdle)) {
1377 Log::getInstance()->log("Video", Log::DEBUG, "omx_clock ChangeComponentState Idle failed");
1378 clock_mutex.Unlock();
1382 clock_mutex.Unlock();
1386 int VideoOMX::setClockExecutingandRunning()
1388 OMX_ERRORTYPE error;
1389 OMX_STATETYPE temp_state;
1391 OMX_GetState(omx_clock,&temp_state);
1393 if (temp_state!=OMX_StateExecuting) {
1394 if (!ChangeComponentState(omx_clock,OMX_StateExecuting)) {
1395 Log::getInstance()->log("Video", Log::DEBUG, "omx_clock ChangeComponentState Execute failed");
1396 clock_mutex.Unlock();
1397 DeAllocateCodecsOMX();
1402 OMX_TIME_CONFIG_CLOCKSTATETYPE clock_conf;
1403 memset(&clock_conf,0,sizeof(clock_conf));
1404 clock_conf.nSize=sizeof(clock_conf);
1405 clock_conf.nVersion.nVersion=OMX_VERSION;
1406 clock_conf.eState=OMX_TIME_ClockStateRunning;
1407 error=OMX_SetConfig(omx_clock,OMX_IndexConfigTimeClockState,&clock_conf);
1408 if (error!=OMX_ErrorNone) {
1409 Log::getInstance()->log("Video", Log::DEBUG, "Clock IndexConfigTimeClockState failed %x", error);
1410 clock_mutex.Unlock();
1411 DeAllocateCodecsOMX();
1414 clock_mutex.Unlock();
1420 int VideoOMX::ChangeComponentState(OMX_HANDLETYPE handle,OMX_STATETYPE type) //needs to be called with locked mutex
1422 OMX_ERRORTYPE error;
1423 error=OMX_SendCommand(handle,OMX_CommandStateSet,type,0);
1424 if (error!=OMX_ErrorNone){
1425 Log::getInstance()->log("Video", Log::DEBUG, "handle %x Send Command to OMX State %x %x",handle,type, error);
1429 if (!CommandFinished(handle,OMX_CommandStateSet,type)) {
1437 int VideoOMX::EnablePort(OMX_HANDLETYPE handle,OMX_U32 port,bool wait) //needs to be called with locked mutex
1439 OMX_ERRORTYPE error;
1440 error=OMX_SendCommand(handle,OMX_CommandPortEnable,port,0);
1441 if (error!=OMX_ErrorNone){
1442 Log::getInstance()->log("Video", Log::DEBUG, "handle %x Send Command to enable port %x %x",handle,port, error);
1446 if (!wait) return 1;
1447 if (!CommandFinished(handle,OMX_CommandPortEnable,port)) {
1455 int VideoOMX::DisablePort(OMX_HANDLETYPE handle,OMX_U32 port,bool wait) //needs to be called with locked mutex
1457 OMX_ERRORTYPE error;
1458 error=OMX_SendCommand(handle,OMX_CommandPortDisable,port,0);
1459 if (error!=OMX_ErrorNone){
1460 Log::getInstance()->log("Video", Log::DEBUG, "handle %x Send Command to disable port %x %x",handle,port, error);
1464 if (!wait) return 1;
1465 if (!CommandFinished(handle,OMX_CommandPortDisable,port)) {
1473 int VideoOMX::WaitForEvent(OMX_HANDLETYPE handle,OMX_U32 event) //needs to be called with locked mutex
1477 omx_event_mutex.Lock();
1478 list<VPE_OMX_EVENT>::iterator itty=omx_events.begin();
1479 while (itty!=omx_events.end()) {
1481 VPE_OMX_EVENT current=*itty;
1482 if (current.handle==handle) { //this is ours
1483 if (current.event_type==OMX_EventError) {
1484 omx_events.erase(itty);
1485 omx_event_mutex.Unlock();
1486 Log::getInstance()->log("Video", Log::DEBUG, "WaitForEvent Finished on Error");
1489 } else if (current.event_type==event) {
1490 omx_events.erase(itty);
1491 omx_event_mutex.Unlock();
1492 Log::getInstance()->log("Video", Log::DEBUG, "WaitForEvent Finished Completed");
1499 omx_event_mutex.Unlock();
1504 Log::getInstance()->log("Video", Log::DEBUG, "WaitForEvent waited too long %x %x",handle,event);
1509 int VideoOMX::clearEvents()
1511 omx_event_mutex.Lock();
1513 omx_event_mutex.Unlock();
1519 int VideoOMX::CommandFinished(OMX_HANDLETYPE handle,OMX_U32 command,OMX_U32 data2) //needs to be called with locked mutex
1523 omx_event_mutex.Lock();
1524 list<VPE_OMX_EVENT>::iterator itty=omx_events.begin();
1525 while (itty!=omx_events.end()) {
1527 VPE_OMX_EVENT current=*itty;
1528 if (current.handle==handle) { //this is ours
1529 if (current.event_type==OMX_EventError) {
1530 omx_events.erase(itty);
1531 omx_event_mutex.Unlock();
1532 Log::getInstance()->log("Video", Log::DEBUG, "Command Finished on Error %x",current.data1);
1535 } else if (current.event_type==OMX_EventCmdComplete && current.data1==command && current.data2==data2) {
1536 omx_events.erase(itty);
1537 omx_event_mutex.Unlock();
1538 //Log::getInstance()->log("Video", Log::DEBUG, "Command Finished Completed");
1545 omx_event_mutex.Unlock();
1550 Log::getInstance()->log("Video", Log::DEBUG, "CommandFinished waited too long %x %x %x",handle,command, data2);
1559 int VideoOMX::PrepareInputBufsOMX() //needs to be called with locked mutex
1561 OMX_ERRORTYPE error;
1562 OMX_PARAM_PORTDEFINITIONTYPE port_def_type;
1563 memset(&port_def_type,0,sizeof(port_def_type));
1564 port_def_type.nSize=sizeof(port_def_type);
1565 port_def_type.nVersion.nVersion=OMX_VERSION;
1566 port_def_type.nPortIndex=omx_codec_input_port;
1568 error=OMX_GetParameter(omx_vid_dec,OMX_IndexParamPortDefinition, &port_def_type);
1570 if (error!=OMX_ErrorNone){
1571 Log::getInstance()->log("Video", Log::DEBUG, "Get OMX OMX_IndexParamPortDefinition failed %x", error);
1573 /* Log::getInstance()->log("Video", Log::DEBUG, "Port para %d %d %d %d %d %d %d", port_def_type.nBufferCountActual,
1574 port_def_type.nBufferCountMin,port_def_type.nBufferSize,port_def_type.bEnabled,port_def_type.bPopulated,
1575 port_def_type.bBuffersContiguous,port_def_type.nBufferAlignment);*/
1577 port_def_type.nBufferCountActual=100;
1578 port_def_type.nBufferSize=max(port_def_type.nBufferSize,200000); // for transcoder important
1580 error=OMX_SetParameter(omx_vid_dec,OMX_IndexParamPortDefinition, &port_def_type);
1582 if (error!=OMX_ErrorNone){
1583 Log::getInstance()->log("Video", Log::DEBUG, "Set OMX OMX_IndexParamPortDefinition failed %x", error);
1587 error=OMX_SendCommand(omx_vid_dec,OMX_CommandPortEnable,omx_codec_input_port,0);
1588 if (error!=OMX_ErrorNone){
1589 Log::getInstance()->log("Video", Log::DEBUG, "Prepare Input bufs Send Command to enable port %x", error);
1593 input_bufs_omx_mutex.Lock();
1594 for (unsigned int i=0; i< port_def_type.nBufferCountActual;i++) {
1596 // unsigned char* new_buffer_data=(unsigned char*)malloc(port_def_type.nbufferSize);
1597 OMX_BUFFERHEADERTYPE *buf_head=NULL;
1598 /* error=OMX_Usebuffer(omx_vid_dec,&buf_head,omx_codec_input_port,NULL,port_def_type.nbufferSize,new_buffer_data);
1599 if (error!=OMX_ErrorNone){
1600 Log::getInstance()->log("Video", Log::DEBUG, "Use OMX_Usebuffer failed %x", error);
1601 input_bufs_omx_mutex.Unlock();
1604 error=OMX_AllocateBuffer(omx_vid_dec,&buf_head,omx_codec_input_port,NULL,port_def_type.nBufferSize);
1605 if (error!=OMX_ErrorNone){
1606 Log::getInstance()->log("Video", Log::DEBUG, "Use OMX_AllocateBuffer failed %x", error);
1607 input_bufs_omx_mutex.Unlock();
1610 input_bufs_omx_all.push_back(buf_head);
1611 input_bufs_omx_free.push_back(buf_head);
1613 omx_first_frame=true;
1616 cur_input_buf_omx=NULL;
1617 input_bufs_omx_mutex.Unlock();
1620 Log::getInstance()->log("Video", Log::DEBUG, "PrepareInputBufsOMX mark3");
1621 if (!CommandFinished(omx_vid_dec,OMX_CommandPortEnable,omx_codec_input_port)) {
1624 Log::getInstance()->log("Video", Log::DEBUG, "PrepareInputBufsOMX mark4");
1629 int VideoOMX::DestroyInputBufsOMX() //need s to be called with locked mutex
1631 OMX_ERRORTYPE error;
1633 cur_input_buf_omx=NULL;
1634 input_bufs_omx_mutex.Lock();
1635 for (int i=0; i< input_bufs_omx_all.size();i++) {
1636 // free(input_bufs_omx_all[i]->pBuffer);
1637 // input_bufs_omx_all[i]->pBuffer=NULL;
1638 error=OMX_FreeBuffer(omx_vid_dec,omx_codec_input_port,input_bufs_omx_all[i]);
1639 if (error!=OMX_ErrorNone){
1640 Log::getInstance()->log("Video", Log::DEBUG, "Use OMX_FreeBuffer failed %x", error);
1641 input_bufs_omx_mutex.Unlock();
1646 input_bufs_omx_all.clear();
1647 input_bufs_omx_free.clear();
1648 input_bufs_omx_mutex.Unlock();
1653 int VideoOMX::FlushRenderingPipe()
1655 OMX_ERRORTYPE error;
1659 error = OMX_SendCommand(omx_vid_dec, OMX_CommandFlush,
1660 omx_codec_output_port, NULL);
1661 if (error != OMX_ErrorNone) {
1662 Log::getInstance()->log("Video", Log::DEBUG,
1663 "OMX_Flush codec out 1 failed %x", error);
1667 error = OMX_SendCommand(omx_vid_sched, OMX_CommandFlush,
1668 omx_shed_input_port, NULL);
1669 if (error != OMX_ErrorNone) {
1670 Log::getInstance()->log("Video", Log::DEBUG,
1671 "OMX_Flush shed in 2 failed %x", error);
1675 if (!CommandFinished(omx_vid_dec, OMX_CommandFlush,
1676 omx_codec_output_port)) {
1677 Log::getInstance()->log("Video", Log::DEBUG,
1678 "flush cmd codec 3 failed");
1681 if (!CommandFinished(omx_vid_sched, OMX_CommandFlush,
1682 omx_shed_input_port)) {
1683 Log::getInstance()->log("Video", Log::DEBUG,
1684 "flush cmd shed 4 failed");
1687 error = OMX_SendCommand(omx_vid_dec, OMX_CommandFlush,
1688 omx_codec_output_port, NULL);
1689 if (error != OMX_ErrorNone) {
1690 Log::getInstance()->log("Video", Log::DEBUG,
1691 "OMX_Flush codec out 5 failed %x", error);
1695 error = OMX_SendCommand(omx_vid_deint, OMX_CommandFlush,
1696 omx_deint_input_port, NULL);
1697 if (error != OMX_ErrorNone) {
1698 Log::getInstance()->log("Video", Log::DEBUG,
1699 "OMX_Flush deint in 6 failed %x", error);
1703 if (!CommandFinished(omx_vid_dec, OMX_CommandFlush,
1704 omx_codec_output_port)) {
1705 Log::getInstance()->log("Video", Log::DEBUG,
1706 "flush cmd codec 7 failed");
1709 if (!CommandFinished(omx_vid_deint, OMX_CommandFlush,
1710 omx_deint_input_port)) {
1711 Log::getInstance()->log("Video", Log::DEBUG,
1712 "flush cmd deint 8 failed");
1716 error = OMX_SendCommand(omx_vid_deint, OMX_CommandFlush,
1717 omx_deint_output_port, NULL);
1718 if (error != OMX_ErrorNone) {
1719 Log::getInstance()->log("Video", Log::DEBUG,
1720 "OMX_Flush deint out 9 failed %x", error);
1724 error = OMX_SendCommand(omx_vid_sched, OMX_CommandFlush,
1725 omx_shed_input_port, NULL);
1726 if (error != OMX_ErrorNone) {
1727 Log::getInstance()->log("Video", Log::DEBUG,
1728 "OMX_Flush shed in 10 failed %x", error);
1732 if (!CommandFinished(omx_vid_deint, OMX_CommandFlush,
1733 omx_deint_output_port)) {
1734 Log::getInstance()->log("Video", Log::DEBUG,
1735 "flush cmd deint 11 failed");
1738 if (!CommandFinished(omx_vid_sched, OMX_CommandFlush,
1739 omx_shed_input_port)) {
1740 Log::getInstance()->log("Video", Log::DEBUG,
1741 "flush cmd shed 12 failed");
1751 error = OMX_SendCommand(omx_vid_rend, OMX_CommandFlush,
1752 omx_rend_input_port, NULL);
1753 if (error != OMX_ErrorNone) {
1754 Log::getInstance()->log("Video", Log::DEBUG,
1755 "OMX_Flush rend in failed %x", error);
1759 error = OMX_SendCommand(omx_vid_sched, OMX_CommandFlush,
1760 omx_shed_output_port, NULL);
1761 if (error != OMX_ErrorNone) {
1762 Log::getInstance()->log("Video", Log::DEBUG,
1763 "OMX_Flush shed out failed %x", error);
1769 if (!CommandFinished(omx_vid_rend, OMX_CommandFlush,
1770 omx_rend_input_port)) {
1771 Log::getInstance()->log("Video", Log::DEBUG,
1772 "flush cmd shed rend failed");
1775 if (!CommandFinished(omx_vid_sched, OMX_CommandFlush,
1776 omx_shed_output_port)) {
1777 Log::getInstance()->log("Video", Log::DEBUG,
1778 "flush cmd shed rend failed");
1783 int VideoOMX::DeAllocateCodecsOMX()
1785 OMX_ERRORTYPE error;
1787 Log::getInstance()->log("Video", Log::DEBUG, "enter deallocatecodecsomx");
1789 if (cur_input_buf_omx) {
1790 cur_input_buf_omx->nFlags|=OMX_BUFFERFLAG_EOS;
1791 OMX_ERRORTYPE error=ProtOMXEmptyThisBuffer(omx_vid_dec,cur_input_buf_omx);
1792 if (error!=OMX_ErrorNone) {
1793 Log::getInstance()->log("Video", Log::DEBUG, "OMX_EmptyThisBuffer failed %x", error);
1796 cur_input_buf_omx=NULL;//write out old data
1801 // first stop the omx elements
1802 if (!ChangeComponentState(omx_vid_dec,OMX_StateIdle)) {
1803 Log::getInstance()->log("Video", Log::DEBUG, "vid_dec ChangeComponentState");
1806 clock_mutex.Unlock();
1812 if (!ChangeComponentState(omx_vid_deint, OMX_StateIdle)) {
1813 Log::getInstance()->log("Video", Log::DEBUG,
1814 "vid_deint ChangeComponentState");
1820 if (!ChangeComponentState(omx_vid_sched,OMX_StateIdle)) {
1821 Log::getInstance()->log("Video", Log::DEBUG, "vid_shed ChangeComponentState");
1825 if (!ChangeComponentState(omx_vid_rend,OMX_StateIdle)) {
1826 Log::getInstance()->log("Video", Log::DEBUG, "vid_rend ChangeComponentState");
1832 // TODO proper deinit sequence
1833 // first flush all buffers
1834 FlushRenderingPipe();
1840 error=OMX_SendCommand(omx_clock,OMX_CommandFlush, omx_clock_output_port, NULL);
1841 if (error!=OMX_ErrorNone){
1842 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Flush clock out failed %x", error);
1846 error=OMX_SendCommand(omx_vid_sched,OMX_CommandFlush, omx_shed_clock_port, NULL);
1847 if (error!=OMX_ErrorNone){
1848 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Flush shed clock failed %x", error);
1852 if (!CommandFinished(omx_clock,OMX_CommandFlush,omx_clock_output_port) ||
1853 !CommandFinished(omx_vid_sched,OMX_CommandFlush,omx_shed_clock_port)) {
1854 Log::getInstance()->log("Video", Log::DEBUG, "flush cmd clock shed failed");
1860 error = OMX_SendCommand(omx_vid_dec, OMX_CommandFlush,
1861 omx_codec_input_port, NULL);
1862 if (error != OMX_ErrorNone) {
1863 Log::getInstance()->log("Video", Log::DEBUG,
1864 "OMX_Flush codec out failed %x", error);
1871 DestroyInputBufsOMX();
1874 if (!DisablePort(omx_vid_sched,omx_shed_output_port,true)) {
1875 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 2 ");
1877 if (!DisablePort(omx_vid_rend,omx_rend_input_port,true)) {
1878 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 1");
1884 if (!DisablePort(omx_vid_dec,omx_codec_output_port,true)) {
1885 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 6");
1890 if (!DisablePort(omx_vid_dec,omx_codec_input_port,true)) {
1891 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 7");
1895 if (!DisablePort(omx_vid_deint,omx_deint_output_port,true)) {
1896 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 6a");
1901 if (!DisablePort(omx_vid_deint,omx_deint_input_port,true)) {
1902 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 7a");
1908 if (!DisablePort(omx_vid_sched,omx_shed_input_port,true)) {
1909 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 3");
1912 if (!DisablePort(omx_vid_sched,omx_shed_clock_port,true)) {
1913 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 4");
1916 if (!DisablePort(omx_clock,omx_clock_output_port,true)) {
1917 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 5");
1923 error=OMX_SetupTunnel(omx_vid_dec,omx_codec_output_port,NULL,NULL);
1924 if (error!=OMX_ErrorNone) {
1925 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1930 error=OMX_SetupTunnel(omx_vid_deint,omx_deint_input_port,NULL,NULL);
1931 if (error!=OMX_ErrorNone) {
1932 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1936 error=OMX_SetupTunnel(omx_vid_deint,omx_deint_output_port,NULL,NULL);
1937 if (error!=OMX_ErrorNone) {
1938 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1942 error=OMX_SetupTunnel(omx_vid_sched,omx_shed_input_port,NULL,NULL);
1943 if (error!=OMX_ErrorNone) {
1944 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1949 error=OMX_SetupTunnel(omx_vid_sched,omx_shed_output_port,NULL,NULL);
1950 if (error!=OMX_ErrorNone) {
1951 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1955 error=OMX_SetupTunnel(omx_vid_rend,omx_rend_input_port,NULL,NULL);
1956 if (error!=OMX_ErrorNone) {
1957 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1962 error=OMX_SetupTunnel(omx_clock,omx_clock_output_port,NULL,NULL);
1963 if (error!=OMX_ErrorNone) {
1964 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1968 error=OMX_SetupTunnel(omx_vid_sched,omx_shed_clock_port,NULL,NULL);
1969 if (error!=OMX_ErrorNone) {
1970 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1977 error=OMX_FreeHandle(omx_vid_dec);
1978 error=OMX_FreeHandle(omx_vid_sched);
1979 error=OMX_FreeHandle(omx_vid_rend);
1980 if (dodeint) error=OMX_FreeHandle(omx_vid_deint);
1982 clock_mutex.Unlock();
1984 if (error!=OMX_ErrorNone) {
1985 Log::getInstance()->log("Video", Log::DEBUG, "FreeHandle failed %d", error);
1987 } else clock_mutex.Unlock();
1988 Log::getInstance()->log("Video", Log::DEBUG, "leave deallocate codecs OMX");
1994 void VideoOMX::destroyClock()
1997 if (clock_references>0) {
1999 if (clock_references==0) {
2000 OMX_ERRORTYPE error;
2001 Log::getInstance()->log("Video", Log::DEBUG, "destroy omx clock");
2002 error=OMX_FreeHandle(omx_clock);
2003 if (error!=OMX_ErrorNone) {
2004 Log::getInstance()->log("Video", Log::DEBUG, "FreeHandle failed %d", error);
2009 clock_mutex.Unlock();
2013 int VideoOMX::stop()
2015 if (!initted) return 0;
2018 //Check if libav mode
2019 DeAllocateCodecsOMX();
2024 // if (ioctl(fdVideo, AV_SET_VID_STOP, 0) != 0) return 0;
2028 int VideoOMX::reset()
2030 if (!initted) return 0;
2033 DeAllocateCodecsOMX();
2034 // if (ioctl(fdVideo, AV_SET_VID_RESET, 0x11) != 0) return 0;
2038 int VideoOMX::pause()
2040 if (!initted) return 0;
2041 Log::getInstance()->log("Video", Log::DEBUG, "enter pause");
2044 //maybe also change omx clock?
2045 //pausetimecode=GetCurrentSystemTime();
2051 int VideoOMX::unPause() // FIXME get rid - same as play!! Not here!
2053 if (!initted) return 0;
2054 Log::getInstance()->log("Video", Log::DEBUG, "enter unpause");
2057 // playbacktimeoffset+=GetCurrentSystemTime()-pausetimecode;
2058 paused=false; // may be also change omx clock
2064 int VideoOMX::fastForward()
2066 if (!initted) return 0;
2068 // if (ioctl(fdVideo, AV_SET_VID_libavWD, 1) != 0) return 0;
2072 int VideoOMX::unFastForward()
2074 if (!initted) return 0;
2076 // if (ioctl(fdVideo, AV_SET_VID_RESET, 0x11) != 0) return 0; // don't need this.
2078 //// if (ioctl(fdVideo, AV_SET_VID_PLAY, 0) != 0) return 0;
2082 int VideoOMX::attachFrameBuffer()
2084 if (!initted) return 0;
2086 // if (ioctl(fdVideo, AV_SET_VID_FB, 0) != 0) return 0;
2090 int VideoOMX::blank(void)
2092 // if (ioctl(fdVideo, AV_SET_VID_FB, 1) != 0) return 0;
2093 // if (ioctl(fdVideo, AV_SET_VID_FB, 0) != 0) return 0;
2097 ULLONG VideoOMX::getCurrentTimestamp() {
2100 long long ncur_clock_time = cur_clock_time;
2103 OMX_ERRORTYPE error;
2104 OMX_TIME_CONFIG_CLOCKSTATETYPE clock_conf;
2105 memset(&clock_conf, 0, sizeof(clock_conf));
2106 clock_conf.nSize = sizeof(clock_conf);
2107 clock_conf.nVersion.nVersion = OMX_VERSION;
2108 error= OMX_GetConfig(omx_clock, OMX_IndexConfigTimeClockState,
2110 if (error != OMX_ErrorNone) {
2111 Log::getInstance()->log("Video", Log::DEBUG,"getCurrentTimestamp IndexConfigTimeClockState failed %x",error);
2114 if (clock_conf.eState == OMX_TIME_ClockStateRunning) {
2116 OMX_TIME_CONFIG_TIMESTAMPTYPE cur_time_stamp;
2117 memset(&cur_time_stamp, 0, sizeof(cur_time_stamp));
2118 cur_time_stamp.nSize = sizeof(cur_time_stamp);
2119 cur_time_stamp.nVersion.nVersion = OMX_VERSION;
2120 cur_time_stamp.nPortIndex = omx_clock_output_port;
2121 error = OMX_GetConfig(omx_clock, OMX_IndexConfigTimeCurrentMediaTime,
2123 if (error != OMX_ErrorNone) {
2124 Log::getInstance()->log("Video",Log::DEBUG,"getCurrentTimestamp OMX_IndexConfigTimeCurrentMediaTime failed %x",error);
2126 long long temp = cur_time_stamp.nTimestamp.nLowPart
2127 | ((long long) cur_time_stamp.nTimestamp.nHighPart << 32);
2128 ncur_clock_time = cur_clock_time = temp * 10LL;
2131 clock_mutex.Unlock();
2134 //ncur_clock_time -= startoffset;
2135 ncur_clock_time -= lastreftimeOMX;
2136 long long result = lastreftimePTS;
2137 result += (long long) (ncur_clock_time / 10000LL * 90LL);
2139 result = (1LL << 33) - result;
2140 //Log::getInstance()->log("Video", Log::DEBUG,"getCurrentTimestamp %lld %lld %lld %lld %lld %lld",ncur_clock_time,cur_clock_time,lastreftimeOMX,lastreftimePTS,result,startoffset);
2148 ULONG VideoOMX::timecodeToFrameNumber(ULLONG timecode)
2150 if (format == PAL) return (ULONG)(((double)timecode / (double)90000) * (double)25);
2151 else return (ULONG)(((double)timecode / (double)90000) * (double)30);
2156 int VideoOMX::test()
2161 // return ioctl(fdVideo, AV_SET_VID_STC, &stc);
2168 int VideoOMX::test2()
2176 long long VideoOMX::SetStartOffset(long long curreftime, bool *rsync)
2180 startoffset=curreftime;//offset is set for audio
2182 offsetvideonotset=false;
2184 if (offsetvideonotset) {
2185 offsetvideonotset=false;
2188 if ( (curreftime-lastrefvideotime)>10000000LL
2189 || (curreftime-lastrefvideotime)<-10000000LL) {//if pts jumps to big resync
2190 startoffset+=curreftime-lastrefvideotime;
2191 lastrefaudiotime+=curreftime-lastrefvideotime;
2193 offsetaudionotset=true;
2200 lastrefvideotime=curreftime;
2206 long long VideoOMX::SetStartAudioOffset(long long curreftime, bool *rsync)
2210 startoffset=curreftime;
2212 offsetaudionotset=false;
2214 if (offsetaudionotset) {
2215 offsetaudionotset=false;
2218 if ( (curreftime-lastrefaudiotime)>10000000LL
2219 || (curreftime-lastrefaudiotime)<-10000000LL) {//if pts jumps to big resync
2220 startoffset+=curreftime-lastrefaudiotime;
2221 lastrefvideotime+=curreftime-lastrefaudiotime;
2223 offsetvideonotset=true;
2229 lastrefaudiotime=curreftime;
2234 void VideoOMX::ResetTimeOffsets() {
2235 offsetnotset=true; //called from demuxer
2236 offsetvideonotset=true;
2237 offsetaudionotset=true;
2246 void VideoOMX::DeinterlaceFix()
2249 Demuxer* demux=Demuxer::getInstance();
2251 OMX_ERRORTYPE error;
2252 OMX_PARAM_PORTDEFINITIONTYPE port_def_type;
2253 memset(&port_def_type,0,sizeof(port_def_type));
2254 port_def_type.nSize=sizeof(port_def_type);
2255 port_def_type.nVersion.nVersion=OMX_VERSION;
2256 port_def_type.nPortIndex=omx_codec_output_port;
2258 error=OMX_GetParameter(omx_vid_dec,OMX_IndexParamPortDefinition, &port_def_type);
2259 if (error != OMX_ErrorNone) {
2260 Log::getInstance()->log("Video", Log::DEBUG,
2261 "OMX_IndexParamPortDefinition fix failed %x", error);
2262 clock_mutex.Unlock();
2266 if (port_def_type.format.video.nFrameWidth == demux->getHorizontalSize()
2267 && port_def_type.format.video.nFrameHeight == demux->getVerticalSize()){
2268 Log::getInstance()->log("Video", Log::DEBUG,
2269 "Deinit first frame fix");
2270 deint_first_frame=false;
2272 WaitForEvent(omx_vid_dec,OMX_EventPortSettingsChanged);
2273 DisablePort(omx_vid_dec,omx_codec_output_port,false);
2274 DisablePort(omx_vid_sched,omx_shed_input_port,false);
2275 DisablePort(omx_vid_deint,omx_deint_output_port,false);
2276 DisablePort(omx_vid_deint,omx_deint_input_port,false);
2278 port_def_type.nPortIndex=omx_deint_input_port;
2279 error = OMX_SetParameter(omx_vid_deint, OMX_IndexParamPortDefinition,
2281 if (error != OMX_ErrorNone) {
2282 Log::getInstance()->log("Video", Log::DEBUG,
2283 "Set OMX_IndexParamPortDefinition1 failed %x", error);
2284 clock_mutex.Unlock();
2288 port_def_type.nPortIndex=omx_deint_output_port;
2289 error = OMX_SetParameter(omx_vid_deint, OMX_IndexParamPortDefinition,
2291 if (error != OMX_ErrorNone) {
2292 Log::getInstance()->log("Video", Log::DEBUG,
2293 "Set OMX_IndexParamPortDefinition2 failed %x", error);
2294 clock_mutex.Unlock();
2299 EnablePort(omx_vid_dec,omx_codec_output_port,false);
2300 EnablePort(omx_vid_deint,omx_deint_input_port,false);
2301 EnablePort(omx_vid_deint,omx_deint_output_port,false);
2302 EnablePort(omx_vid_sched,omx_shed_input_port,false);
2304 clock_mutex.Unlock();
2310 void VideoOMX::PutBufferToPres(OMX_BUFFERHEADERTYPE* buffer)
2313 OMX_ERRORTYPE error = ProtOMXEmptyThisBuffer(omx_vid_dec, buffer);
2314 if (error != OMX_ErrorNone) {
2315 Log::getInstance()->log("Video", Log::DEBUG,
2316 "OMX_EmptyThisBuffer failed %x", error);
2318 if (deint_first_frame && dodeint) DeinterlaceFix();
2322 OMX_ERRORTYPE VideoOMX::ProtOMXEmptyThisBuffer(OMX_HANDLETYPE handle, OMX_BUFFERHEADERTYPE* buffer)
2324 // protect the call to empty this buffer
2327 /* long long temp =buffer->nTimeStamp.nLowPart
2328 | ((long long) buffer->nTimeStamp.nHighPart << 32);*/
2330 pthread_testcancel();
2331 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldcancelstate);
2332 pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldcanceltype);
2335 /* OMX_ERRORTYPE error;
2336 OMX_TIME_CONFIG_TIMESTAMPTYPE timestamp;
2337 memset(×tamp, 0, sizeof(timestamp));
2338 timestamp.nSize = sizeof(timestamp);
2339 timestamp.nVersion.nVersion = OMX_VERSION;
2340 timestamp.nPortIndex =omx_clock_output_port;
2342 error = OMX_GetConfig(omx_clock, OMX_IndexConfigTimeCurrentMediaTime,
2345 if (error != OMX_ErrorNone) {
2346 Log::getInstance()->log("Audio", Log::DEBUG,
2347 "Init OMX_IndexConfigAudioRenderingLatencyfailed %x %d", error,
2348 omx_rend_input_port);
2350 long long temp2 =timestamp.nTimestamp.nLowPart
2351 | ((long long) timestamp.nTimestamp.nHighPart << 32);
2354 Log::getInstance()->log("Video", Log::NOTICE, "OMXETB %x %lld %lld %x",handle,temp,temp2,buffer->nFlags);*/
2355 OMX_ERRORTYPE ret_val;
2356 ret_val=OMX_EmptyThisBuffer(handle,buffer);
2357 clock_mutex.Unlock();
2358 pthread_setcancelstate(oldcancelstate, NULL);
2359 pthread_setcanceltype(oldcanceltype, NULL);
2360 pthread_testcancel();
2364 bool VideoOMX::detectIFrame(const UCHAR *buffer,unsigned int length)
2366 const UCHAR* curbuf=buffer;
2367 const UCHAR* curbufend=buffer+length;
2368 unsigned int detector=0xFFFFFFFF;
2369 bool gotaud=false; // have seen access unit delimiter
2371 while (curbuf!=curbufend) {
2375 if (detector==0x00000109) {
2377 detector=0xFFFFFFFF;
2378 } else if (gotaud &&detector==0x00000001) {
2380 if (curbuf!=curbufend) {
2381 unsigned char picttype=(*curbuf)& 0x1F;
2382 return picttype==0x07;
2386 if (detector==0x00000100) {
2388 if (curbuf==curbufend) return false;
2390 if (curbuf==curbufend) return false;
2391 unsigned char picttype=((*curbuf) >> 3) & 0x07;
2398 return false; // no frame found
2401 void VideoOMX::PrepareMediaSample(const MediaPacketList& mplist,UINT samplepos)
2404 mediapackets.clear();
2405 list<MediaPacket>::const_iterator begin=mplist.begin();
2406 list<MediaPacket>::const_iterator itty=mplist.begin();
2407 advance(itty,min(mplist.size(),10));
2408 mediapackets.insert(mediapackets.begin(),begin,itty);//front
2412 UINT VideoOMX::DeliverMediaSample(UCHAR* buffer, UINT *samplepos)
2415 while (consumed<mediapackets.size()) {
2416 DeliverMediaPacket(mediapackets[consumed], buffer, samplepos);
2417 if (*samplepos == mediapackets[consumed].length) {
2421 } else return consumed;
2426 UINT VideoOMX::DeliverMediaPacket(MediaPacket packet,
2427 const UCHAR* buffer,
2430 if (packet.type == MPTYPE_VIDEO_H264)
2440 //Later add fail back code for libav
2442 *samplepos+=packet.length;
2443 return packet.length;
2447 if (!omx_running) return 0; // if we are not runnig do not do this
2448 if (paused) return 0; //Block if we pause
2450 //Log::getInstance()->log("Video", Log::DEBUG, "DeliverMediaPacketOMX time %lld",packet.presentation_time);
2451 /* if (packet.synched && packet.presentation_time <= 0) {
2452 *samplepos = packet.length;
2453 firstsynched = false;
2454 Log::getInstance()->log("Video", Log::DEBUG,
2455 "DeliverMediaPacketOMX Frameskip");
2456 return packet.length;
2459 /*if (packet.synched && FrameSkip(packet.presentation_time)) {
2460 *samplepos=packet.length;
2461 Log::getInstance()->log("Video", Log::DEBUG, "DeliverMediaPacketOMX Frameskip");
2462 *samplepos=packet.length;
2463 return packet.length;
2465 //long long current_media_time=GetCurrentSystemTime()+playbacktimeoffset;
2466 /* if (packet.synched &&
2467 (packet.presentation_time<0 /*|| // preroll skip frames
2468 (packet.presentation_time+5000000LL)<(current_media_time)*)) { // we are late skip
2469 Log::getInstance()->log("Video", Log::DEBUG, "DeliverMediaPacketOMX Preroll or too late %lld %lld; %lld", packet.presentation_time,current_media_time,playbacktimeoffset);
2470 *samplepos=packet.length;
2471 return packet.length;
2474 OMX_ERRORTYPE error;
2476 /*First Check, if we have an video sample*/
2480 return 0; //Not in iframe mode!
2484 if (packet.disconti) {
2486 if (cur_input_buf_omx) {
2487 PutBufferToPres(cur_input_buf_omx);
2488 cur_input_buf_omx=NULL;
2492 /*Inspect PES-Header */
2494 // OMX_STATETYPE temp_state;
2495 // OMX_GetState(omx_vid_dec,&temp_state);
2497 if (*samplepos==0) {//stripheader
2498 headerstrip=buffer[packet.pos_buffer+8]+9/*is this right*/;
2499 // if (h264) Log::getInstance()->log("Video", Log::DEBUG, "PES info %x %x %x %x",
2500 // buffer[packet.pos_buffer+0],buffer[packet.pos_buffer+1],buffer[packet.pos_buffer+2],buffer[packet.pos_buffer+3]);
2501 *samplepos+=headerstrip;
2502 if ( packet.synched ) {
2503 if (!firstsynched) {
2504 // check if this is an I frame, the decoder does not like non I frames at startup!
2505 if (!detectIFrame(buffer,packet.length)) {
2506 *samplepos=packet.length;//if we have not processed at least one
2507 return packet.length;//synched packet ignore it!
2510 if (cur_input_buf_omx) {
2511 cur_input_buf_omx->nFlags|=OMX_BUFFERFLAG_ENDOFFRAME;
2512 PutBufferToPres(cur_input_buf_omx);
2513 cur_input_buf_omx=NULL;//write out old data
2519 if (!firstsynched) {//
2520 *samplepos=packet.length;//if we have not processed at least one
2521 return packet.length;//synched packet ignore it!
2526 if (!cur_input_buf_omx) {
2527 input_bufs_omx_mutex.Lock();
2528 if (input_bufs_omx_free.size()==0) {
2529 input_bufs_omx_mutex.Unlock();
2530 //Log::getInstance()->log("Video", Log::DEBUG, "Deliver MediaPacket no free sample");
2531 return 0; // we do not have a free media sample
2534 cur_input_buf_omx=input_bufs_omx_free.front();
2535 cur_input_buf_omx->nFilledLen=0;
2536 cur_input_buf_omx->nOffset=0;
2537 cur_input_buf_omx->nTimeStamp=intToOMXTicks(0);
2538 input_bufs_omx_free.pop_front();
2539 input_bufs_omx_mutex.Unlock();
2545 if (cur_input_buf_omx->nFilledLen==0) {//will only be changed on first packet
2546 if (packet.synched) {
2547 // Log::getInstance()->log("Video", Log::DEBUG, "packet synched marker");
2549 //lastreftimePTS=packet.pts;
2550 if (omx_first_frame) { // TODO time
2551 cur_input_buf_omx->nFlags=OMX_BUFFERFLAG_STARTTIME;
2552 Log::getInstance()->log("Video", Log::DEBUG, "Starttime");
2553 omx_first_frame=false;
2555 cur_input_buf_omx->nFlags=0;
2556 //cur_input_buf_omx->nFlags|=OMX_BUFFERFLAG_TIME_UNKNOWN;
2558 lastreftimeOMX=packet.presentation_time;
2559 // Log::getInstance()->log("Video", Log::DEBUG, "Time code %lld pts %lld", lastreftimeOMX,packet.pts);
2560 lastreftimePTS=packet.pts;
2561 cur_input_buf_omx->nTimeStamp=intToOMXTicks(lastreftimeOMX/10LL); // the clock component is faulty;
2565 cur_input_buf_omx->nFlags=OMX_BUFFERFLAG_TIME_UNKNOWN;
2566 cur_input_buf_omx->nTimeStamp=intToOMXTicks(0);
2567 //Log::getInstance()->log("Video", Log::DEBUG, "packet unsynched marker");
2569 if (packet.disconti) cur_input_buf_omx->nFlags|=OMX_BUFFERFLAG_DISCONTINUITY;
2574 unsigned int haveToCopy=packet.length-*samplepos;
2576 while (haveToCopy> (cur_input_buf_omx->nAllocLen-cur_input_buf_omx->nFilledLen)) {
2577 //Log::getInstance()->log("Video", Log::DEBUG, "Big buffer %d %d %d",packet.length,cur_input_buf_omx->nAllocLen,cur_input_buf_omx->nFilledLen);
2578 unsigned int cancopy=cur_input_buf_omx->nAllocLen-cur_input_buf_omx->nFilledLen;
2579 memcpy(cur_input_buf_omx->pBuffer+cur_input_buf_omx->nFilledLen,buffer+packet.pos_buffer+*samplepos,cancopy);
2580 haveToCopy-=cancopy;
2581 cur_input_buf_omx->nFilledLen+=cancopy;
2582 *samplepos+=cancopy;
2583 // push old buffer out
2585 PutBufferToPres(cur_input_buf_omx);
2586 cur_input_buf_omx=NULL;
2588 input_bufs_omx_mutex.Lock();
2589 if (input_bufs_omx_free.size()==0) {
2590 input_bufs_omx_mutex.Unlock();
2591 // Log::getInstance()->log("Video", Log::DEBUG, "Deliver MediaPacket no free sample2");
2592 return *samplepos; // we do not have a free media sample
2594 cur_input_buf_omx=input_bufs_omx_free.front();
2595 cur_input_buf_omx->nFilledLen=0;
2596 cur_input_buf_omx->nOffset=0;
2597 cur_input_buf_omx->nFlags=OMX_BUFFERFLAG_TIME_UNKNOWN;
2598 cur_input_buf_omx->nTimeStamp=intToOMXTicks(0);
2599 input_bufs_omx_free.pop_front();
2600 input_bufs_omx_mutex.Unlock();
2603 memcpy(cur_input_buf_omx->pBuffer+cur_input_buf_omx->nFilledLen,
2604 buffer+packet.pos_buffer+*samplepos,haveToCopy);
2605 cur_input_buf_omx->nFilledLen+=haveToCopy;
2609 *samplepos+=haveToCopy;
2618 bool VideoOMX::displayIFrame(const UCHAR* buffer, UINT length) {
2619 if (!omx_running) return false;
2621 EnterIframePlayback();
2623 int haveToCopy = length;
2625 if (!cur_input_buf_omx) {
2626 input_bufs_omx_mutex.Lock();
2627 if (input_bufs_omx_free.size() == 0) {
2628 input_bufs_omx_mutex.Unlock();
2629 // Log::getInstance()->log("Video", Log::DEBUG,
2630 // "Deliver MediaPacket no free sample");
2631 return false; // we do not have a free media sample
2634 cur_input_buf_omx = input_bufs_omx_free.front();
2635 cur_input_buf_omx->nFilledLen = 0;
2636 cur_input_buf_omx->nOffset = 0;
2637 cur_input_buf_omx->nTimeStamp = intToOMXTicks(0);
2638 input_bufs_omx_free.pop_front();
2639 input_bufs_omx_mutex.Unlock();
2643 unsigned int pattern, packet_length;
2644 unsigned int headerstrip = 0;
2649 //Now we strip the pes header
2650 pattern = (buffer[0] << 16) | (buffer[1] << 8) | (buffer[2]);
2651 while (read_pos + 7 <= length) {
2652 pattern = ((pattern << 8) & 0xFFFFFFFF) | buffer[read_pos + 3];
2653 if (pattern < 0x000001E0 || pattern > 0x000001EF) {
2657 headerstrip = buffer[read_pos + 8] + 9/*is this right*/;
2658 packet_length = ((buffer[read_pos + 4] << 8)
2659 | (buffer[read_pos + 5])) + 6;
2660 if (read_pos + packet_length > length)
2663 if ((headerstrip < packet_length)
2664 && (cur_input_buf_omx->nFilledLen + packet_length
2665 - headerstrip) > cur_input_buf_omx->nAllocLen) {
2667 cur_input_buf_omx->nFlags = OMX_BUFFERFLAG_STARTTIME;
2670 cur_input_buf_omx->nFlags
2671 |= OMX_BUFFERFLAG_TIME_UNKNOWN;
2674 cur_input_buf_omx->nTimeStamp = intToOMXTicks(0);
2675 PutBufferToPres(cur_input_buf_omx);
2676 cur_input_buf_omx = NULL;
2678 if (!cur_input_buf_omx) {
2680 while (count < 100 && omx_running && iframemode) {
2683 input_bufs_omx_mutex.Lock();
2684 if (input_bufs_omx_free.size() == 0) {
2685 input_bufs_omx_mutex.Unlock();
2686 // Log::getInstance()->log("Video", Log::DEBUG,
2687 // "Ifrane no free sample");
2689 if (!omx_running) return false;
2692 cur_input_buf_omx = input_bufs_omx_free.front();
2693 cur_input_buf_omx->nFilledLen = 0;
2694 cur_input_buf_omx->nOffset = 0;
2695 cur_input_buf_omx->nTimeStamp = intToOMXTicks(0);
2696 cur_input_buf_omx->nFlags|= OMX_BUFFERFLAG_TIME_UNKNOWN;
2697 input_bufs_omx_free.pop_front();
2698 input_bufs_omx_mutex.Unlock();
2701 if (!cur_input_buf_omx)
2706 if (packet_length > headerstrip) {
2708 cur_input_buf_omx->pBuffer
2709 + cur_input_buf_omx->nFilledLen,
2710 buffer + read_pos + headerstrip,
2711 packet_length - headerstrip);
2712 cur_input_buf_omx->nFilledLen += packet_length
2715 read_pos += packet_length;
2717 pattern = (buffer[read_pos] << 16)
2718 | (buffer[read_pos + 1] << 8) | (buffer[read_pos + 2]);
2724 cur_input_buf_omx->nFlags = OMX_BUFFERFLAG_STARTTIME;
2727 cur_input_buf_omx->nFlags |= OMX_BUFFERFLAG_TIME_UNKNOWN;
2730 cur_input_buf_omx->nTimeStamp = intToOMXTicks(0);
2732 PutBufferToPres(cur_input_buf_omx);
2733 cur_input_buf_omx = NULL;
2736 MILLISLEEP(40); //Block a bit
2740 int VideoOMX::EnterIframePlayback()
2742 Log::getInstance()->log("Video", Log::DEBUG,
2743 "EnterIframePlayback");
2744 if (cur_input_buf_omx) {
2745 PutBufferToPres(cur_input_buf_omx);
2746 cur_input_buf_omx = NULL;
2748 Log::getInstance()->log("Video", Log::DEBUG,
2749 "EnterIframePlayback 2");
2750 ((AudioOMX*)Audio::getInstance())->DeAllocateCodecsOMX();
2751 DeAllocateCodecsOMX();
2752 AllocateCodecsOMX();
2753 Log::getInstance()->log("Video", Log::DEBUG,
2754 "leave IframePlayback");