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");
885 error=OMX_GetHandle(&omx_vid_dec,VPE_OMX_H264_DECODER,NULL,&callbacks);
887 error=OMX_GetHandle(&omx_vid_dec,VPE_OMX_MPEG2_DECODER,NULL,&callbacks);
890 if (error!=OMX_ErrorNone){
891 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX video decoder failed %x", error);
892 clock_mutex.Unlock();
893 DeAllocateCodecsOMX();
898 Log::getInstance()->log("Video", Log::DEBUG, "Nmark3 ");
899 OMX_PORT_PARAM_TYPE p_param;
900 memset(&p_param,0,sizeof(p_param));
901 p_param.nSize=sizeof(p_param);
902 p_param.nVersion.nVersion=OMX_VERSION;
903 error=OMX_GetParameter(omx_vid_dec,OMX_IndexParamVideoInit,&p_param);
904 if (error!=OMX_ErrorNone){
905 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX h264 decoder OMX_GetParameter failed %x", error);
906 clock_mutex.Unlock();
907 DeAllocateCodecsOMX();
910 omx_codec_input_port=p_param.nStartPortNumber;
911 omx_codec_output_port=p_param.nStartPortNumber+1;
913 if (!DisablePort(omx_vid_dec,omx_codec_input_port) || !DisablePort(omx_vid_dec,omx_codec_output_port)) {
914 Log::getInstance()->log("Video", Log::DEBUG, "Disable Ports OMX video decoder failed");
915 clock_mutex.Unlock();
916 DeAllocateCodecsOMX();
920 Log::getInstance()->log("Video", Log::DEBUG, "Nmark4 ");
922 OMX_PARAM_BRCMVIDEODECODEERRORCONCEALMENTTYPE conceal;
923 memset(&conceal,0,sizeof(conceal));
924 conceal.nSize=sizeof(conceal);
925 conceal.nVersion.nVersion=OMX_VERSION;
926 conceal.bStartWithValidFrame=OMX_FALSE;
928 error=OMX_SetParameter(omx_vid_dec,OMX_IndexParamBrcmVideoDecodeErrorConcealment,&conceal);
929 if (error!=OMX_ErrorNone){
930 Log::getInstance()->log("Video", Log::DEBUG, "OMX_IndexParamBrcmVideoDecodeErrorConcealment failed %x", error);
931 clock_mutex.Unlock();
932 DeAllocateCodecsOMX();
937 error = OMX_GetHandle(&omx_vid_deint, VPE_OMX_VIDEO_DEINTERLACE, NULL,
939 if (error != OMX_ErrorNone) {
940 Log::getInstance()->log("Video", Log::DEBUG,
941 "Init OMX video deinterlacer failed %x", error);
942 clock_mutex.Unlock();
943 DeAllocateCodecsOMX();
947 error = OMX_GetParameter(omx_vid_deint, OMX_IndexParamImageInit,
949 if (error != OMX_ErrorNone) {
950 Log::getInstance()->log("Video", Log::DEBUG,
951 "Init OMX video deinterlacer OMX_GetParameter failed %x",
953 clock_mutex.Unlock();
954 DeAllocateCodecsOMX();
957 omx_deint_input_port = p_param.nStartPortNumber;
958 omx_deint_output_port = p_param.nStartPortNumber + 1;
960 if (!DisablePort(omx_vid_deint, omx_deint_input_port, true)
961 || !DisablePort(omx_vid_deint, omx_deint_output_port, true)) {
962 Log::getInstance()->log("Video", Log::DEBUG,
963 "Disable Ports OMX video deint failed");
964 clock_mutex.Unlock();
965 DeAllocateCodecsOMX();
972 error=OMX_GetHandle(&omx_vid_sched,VPE_OMX_VIDEO_SCHED,NULL,&callbacks);
973 if (error!=OMX_ErrorNone){
974 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX video scheduler failed %x", error);
975 clock_mutex.Unlock();
976 DeAllocateCodecsOMX();
982 error=OMX_GetParameter(omx_vid_sched,OMX_IndexParamVideoInit,&p_param);
983 if (error!=OMX_ErrorNone){
984 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX video scheduler OMX_GetParameter failed %x", error);
985 clock_mutex.Unlock();
986 DeAllocateCodecsOMX();
989 omx_shed_input_port=p_param.nStartPortNumber;
990 omx_shed_output_port=p_param.nStartPortNumber+1;
993 error=OMX_GetParameter(omx_vid_sched,OMX_IndexParamOtherInit,&p_param);
994 if (error!=OMX_ErrorNone){
995 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX video scheduler OMX_GetParameter failed %x", error);
996 clock_mutex.Unlock();
997 DeAllocateCodecsOMX();
1000 omx_shed_clock_port=p_param.nStartPortNumber;
1001 Log::getInstance()->log("Video", Log::DEBUG, "scheduler ports %d %d %d ",omx_shed_input_port,omx_shed_output_port,omx_shed_clock_port);
1004 if (!DisablePort(omx_vid_sched,omx_shed_input_port,true) || !DisablePort(omx_vid_sched,omx_shed_output_port,true)
1005 || !DisablePort(omx_vid_sched,omx_shed_clock_port,true)) {
1006 Log::getInstance()->log("Video", Log::DEBUG, "Disable Ports OMX video shed failed");
1007 clock_mutex.Unlock();
1008 DeAllocateCodecsOMX();
1013 error=OMX_GetHandle(&omx_vid_rend,VPE_OMX_VIDEO_REND,NULL,&callbacks);
1014 if (error!=OMX_ErrorNone){
1015 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX video rend failed %x", error);
1016 clock_mutex.Unlock();
1017 DeAllocateCodecsOMX();
1021 error=OMX_GetParameter(omx_vid_rend,OMX_IndexParamVideoInit,&p_param);
1022 if (error!=OMX_ErrorNone){
1023 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX video rend OMX_GetParameter failed %x", error);
1024 clock_mutex.Unlock();
1025 DeAllocateCodecsOMX();
1028 omx_rend_input_port=p_param.nStartPortNumber;
1029 //omx_rend_output_port=p_param.nStartPortNumber+1;
1032 if (!DisablePort(omx_vid_rend,omx_rend_input_port,true) /*|| !DisablePort(omx_vid_rend,omx_rend_output_port)*/
1034 Log::getInstance()->log("Video", Log::DEBUG, "Disable Ports OMX video rend failed");
1035 clock_mutex.Unlock();
1036 DeAllocateCodecsOMX();
1044 error=OMX_SetupTunnel(omx_clock,omx_clock_output_port,omx_vid_sched,omx_shed_clock_port);
1045 if (error!=OMX_ErrorNone){
1046 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);
1047 clock_mutex.Unlock();
1048 DeAllocateCodecsOMX();
1052 if (!EnablePort(omx_clock,omx_clock_output_port,false) || !EnablePort(omx_vid_sched,omx_shed_clock_port,false)
1054 Log::getInstance()->log("Video", Log::DEBUG, "Enable Ports OMX clock shed failed");
1055 clock_mutex.Unlock();
1056 DeAllocateCodecsOMX();
1061 Log::getInstance()->log("Video", Log::DEBUG, "mark2 ");
1062 if (!ChangeComponentState(omx_vid_sched,OMX_StateIdle)) {
1063 Log::getInstance()->log("Video", Log::DEBUG, "vid_sched idle ChangeComponentState");
1064 clock_mutex.Unlock();
1065 DeAllocateCodecsOMX();
1071 Log::getInstance()->log("Video", Log::DEBUG, "mark1 ");
1072 if ( !CommandFinished(omx_vid_sched,OMX_CommandPortEnable,omx_shed_clock_port)) {
1073 clock_mutex.Unlock();
1074 DeAllocateCodecsOMX();
1081 Log::getInstance()->log("Video", Log::DEBUG, "mark1 special ");
1082 if ( !CommandFinished(omx_clock,OMX_CommandPortEnable,omx_clock_output_port)) {
1083 clock_mutex.Unlock();
1084 DeAllocateCodecsOMX();
1090 Log::getInstance()->log("Video", Log::DEBUG, "mark1b ");
1093 /* error=OMX_SendCommand(omx_vid_dec,OMX_CommandStateSet,OMX_StateIdle,0);
1094 if (error!=OMX_ErrorNone){
1095 Log::getInstance()->log("Video", Log::DEBUG, "vid_dec Send Command to OMX State Idle %x", error);
1099 OMX_VIDEO_PARAM_PORTFORMATTYPE ft_type;
1100 memset(&ft_type,0,sizeof(ft_type));
1101 ft_type.nSize=sizeof(ft_type);
1102 ft_type.nVersion.nVersion=OMX_VERSION;
1104 ft_type.nPortIndex=omx_codec_input_port;
1106 ft_type.eCompressionFormat=OMX_VIDEO_CodingAVC;
1108 ft_type.eCompressionFormat=OMX_VIDEO_CodingMPEG2;
1113 ft_type.xFramerate=0*(1<<16);//25*(1<<16);//demux->getFrameRate()*(1<<16);
1114 Log::getInstance()->log("Video", Log::DEBUG, "Framerate: %d",demux->getFrameRate());
1115 error=OMX_SetParameter(omx_vid_dec,OMX_IndexParamVideoPortFormat,&ft_type);
1116 if (error!=OMX_ErrorNone){
1117 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX_IndexParamVideoPortFormat failed %x", error);
1118 clock_mutex.Unlock();
1119 DeAllocateCodecsOMX();
1124 if (!ChangeComponentState(omx_vid_dec,OMX_StateIdle)) {
1125 Log::getInstance()->log("Video", Log::DEBUG, "vid_dec ChangeComponentState");
1126 clock_mutex.Unlock();
1127 DeAllocateCodecsOMX();
1132 if (!PrepareInputBufsOMX()) {
1133 clock_mutex.Unlock();
1134 DeAllocateCodecsOMX();
1139 error=OMX_SetupTunnel(omx_vid_dec,omx_codec_output_port,omx_vid_sched,omx_shed_input_port);
1140 if (error!=OMX_ErrorNone){
1141 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel dec to sched failed %x", error);
1142 clock_mutex.Unlock();
1143 DeAllocateCodecsOMX();
1149 if (!EnablePort(omx_vid_dec,omx_codec_output_port,false) || !EnablePort(omx_vid_sched,omx_shed_input_port,false)
1151 Log::getInstance()->log("Video", Log::DEBUG, "Enable Ports OMX codec shed failed");
1152 clock_mutex.Unlock();
1153 DeAllocateCodecsOMX();
1157 if ( !CommandFinished(omx_vid_dec,OMX_CommandPortEnable,omx_codec_output_port) || !CommandFinished(omx_vid_sched,OMX_CommandPortEnable,omx_shed_input_port)) {
1158 clock_mutex.Unlock();
1159 DeAllocateCodecsOMX();
1165 error=OMX_SetupTunnel(omx_vid_dec,omx_codec_output_port,omx_vid_deint,omx_deint_input_port);
1166 if (error!=OMX_ErrorNone){
1167 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel dec to deint failed %x", error);
1168 clock_mutex.Unlock();
1169 DeAllocateCodecsOMX();
1175 if (!EnablePort(omx_vid_dec,omx_codec_output_port,false) || !EnablePort(omx_vid_deint,omx_deint_input_port,false)
1177 Log::getInstance()->log("Video", Log::DEBUG, "Enable Ports OMX codec deint failed");
1178 clock_mutex.Unlock();
1179 DeAllocateCodecsOMX();
1183 if ( !CommandFinished(omx_vid_dec,OMX_CommandPortEnable,omx_codec_output_port) || !CommandFinished(omx_vid_deint,OMX_CommandPortEnable,omx_deint_input_port)) {
1184 clock_mutex.Unlock();
1185 DeAllocateCodecsOMX();
1189 if (!ChangeComponentState(omx_vid_deint,OMX_StateIdle)) {
1190 Log::getInstance()->log("Video", Log::DEBUG, "vid_deint ChangeComponentState");
1191 clock_mutex.Unlock();
1192 DeAllocateCodecsOMX();
1196 OMX_CONFIG_IMAGEFILTERPARAMSTYPE imagefilter;
1197 memset(&imagefilter,0,sizeof(imagefilter));
1198 imagefilter.nSize=sizeof(imagefilter);
1199 imagefilter.nVersion.nVersion=OMX_VERSION;
1201 imagefilter.nPortIndex=omx_deint_output_port;
1202 imagefilter.nNumParams=1;
1203 imagefilter.nParams[0]=3;//???
1204 switch (deinterlace) {
1206 imagefilter.eImageFilter=OMX_ImageFilterDeInterlaceLineDouble; break;
1208 imagefilter.eImageFilter=OMX_ImageFilterDeInterlaceAdvanced; break;
1210 imagefilter.eImageFilter=OMX_ImageFilterFilm; break;
1214 error=OMX_SetConfig(omx_vid_deint,OMX_IndexConfigCommonImageFilterParameters,&imagefilter);
1215 if (error!=OMX_ErrorNone){
1216 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX_IndexConfigCommonImageFilterParameters failed %x", error);
1217 clock_mutex.Unlock();
1218 DeAllocateCodecsOMX();
1223 error=OMX_SetupTunnel(omx_vid_deint,omx_deint_output_port,omx_vid_sched,omx_shed_input_port);
1224 if (error!=OMX_ErrorNone){
1225 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel deint to sched failed %x", error);
1226 clock_mutex.Unlock();
1227 DeAllocateCodecsOMX();
1231 if (!EnablePort(omx_vid_deint,omx_deint_output_port,false) || !EnablePort(omx_vid_sched,omx_shed_input_port,false)
1233 Log::getInstance()->log("Video", Log::DEBUG, "Enable Ports OMX deint shed failed");
1234 clock_mutex.Unlock();
1235 DeAllocateCodecsOMX();
1239 if ( !CommandFinished(omx_vid_deint,OMX_CommandPortEnable,omx_deint_output_port) || !CommandFinished(omx_vid_sched,OMX_CommandPortEnable,omx_shed_input_port)) {
1240 clock_mutex.Unlock();
1241 DeAllocateCodecsOMX();
1247 if (!ChangeComponentState(omx_vid_dec,OMX_StateExecuting)) {
1248 Log::getInstance()->log("Video", Log::DEBUG, "omx_vid_dec ChangeComponentState Execute");
1249 clock_mutex.Unlock();
1250 DeAllocateCodecsOMX();
1254 error=OMX_SetupTunnel(omx_vid_sched,omx_shed_output_port,omx_vid_rend,omx_rend_input_port);
1255 if (error!=OMX_ErrorNone){
1256 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel sched to rend failed %x", error);
1257 clock_mutex.Unlock();
1258 DeAllocateCodecsOMX();
1262 if (!EnablePort(omx_vid_sched,omx_shed_output_port,false) || !EnablePort(omx_vid_rend,omx_rend_input_port,false)
1264 Log::getInstance()->log("Video", Log::DEBUG, "Enable Ports OMX shed rend failed");
1265 clock_mutex.Unlock();
1266 DeAllocateCodecsOMX();
1270 if (!CommandFinished(omx_vid_sched,OMX_CommandPortEnable,omx_shed_output_port)
1271 || !CommandFinished(omx_vid_rend,OMX_CommandPortEnable,omx_rend_input_port)) {
1272 clock_mutex.Unlock();
1273 DeAllocateCodecsOMX();
1277 if (!ChangeComponentState(omx_vid_rend,OMX_StateIdle)) {
1278 Log::getInstance()->log("Video", Log::DEBUG, "vid_rend ChangeComponentState");
1279 clock_mutex.Unlock();
1280 DeAllocateCodecsOMX();
1285 if (!ChangeComponentState(omx_vid_deint,OMX_StateExecuting)) {
1286 Log::getInstance()->log("Video", Log::DEBUG, "vid_vid_deint ChangeComponentState");
1287 clock_mutex.Unlock();
1288 DeAllocateCodecsOMX();
1291 DisablePort(omx_vid_deint,omx_deint_output_port,false);
1292 DisablePort(omx_vid_deint,omx_deint_input_port,false);
1295 if (!ChangeComponentState(omx_vid_sched,OMX_StateExecuting)) {
1296 Log::getInstance()->log("Video", Log::DEBUG, "omx_vid_sched ChangeComponentState Execute");
1297 clock_mutex.Unlock();
1298 DeAllocateCodecsOMX();
1302 if (!ChangeComponentState(omx_vid_rend,OMX_StateExecuting)) {
1303 Log::getInstance()->log("Video", Log::DEBUG, "omx_vid_rend ChangeComponentState Execute");
1304 clock_mutex.Unlock();
1305 DeAllocateCodecsOMX();
1310 /*OMX_CONFIG_DISPLAYREGIONTYPE dispconf;
1311 memset(&dispconf,0,sizeof(dispconf));
1312 dispconf.nSize=sizeof(dispconf);
1313 dispconf.nVersion.nVersion=OMX_VERSION;
1315 dispconf.nPortIndex=omx_rend_input_port;
1317 dispconf.set=OMX_DISPLAY_SET_LAYER ;
1319 error=OMX_SetConfig(omx_vid_rend,OMX_IndexConfigDisplayRegion,&dispconf);
1320 if (error!=OMX_ErrorNone){
1321 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX_IndexConfigDisplayRegion failed %x", error);
1322 clock_mutex.Unlock();
1323 DeAllocateCodecsOMX();
1327 /* dispconf.set=OMX_DISPLAY_SET_FULLSCREEN ;
1328 dispconf.fullscreen=OMX_FALSE;
1329 error=OMX_SetParameter(omx_vid_rend,OMX_IndexConfigDisplayRegion,&dispconf);
1330 if (error!=OMX_ErrorNone){
1331 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX_IndexConfigDisplayRegion failed %x", error);
1332 clock_mutex.Unlock();
1333 DeAllocateCodecsOMX();
1337 dispconf.set=OMX_DISPLAY_SET_DEST_RECT;
1338 dispconf.dest_rect.x_offset=100;
1339 dispconf.dest_rect.y_offset=100;
1340 dispconf.dest_rect.width=640;
1341 dispconf.dest_rect.height=480;
1342 error=OMX_SetParameter(omx_vid_rend,OMX_IndexConfigDisplayRegion,&dispconf);
1343 if (error!=OMX_ErrorNone){
1344 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX_IndexConfigDisplayRegion failed %x", error);
1345 clock_mutex.Unlock();
1346 DeAllocateCodecsOMX();
1351 //playbacktimeoffset=-GetCurrentSystemTime();
1355 clock_mutex.Unlock();
1358 setClockExecutingandRunning();
1367 int VideoOMX::idleClock()
1369 OMX_ERRORTYPE error;
1370 OMX_STATETYPE temp_state;
1372 OMX_GetState(omx_clock,&temp_state);
1374 if (temp_state!=OMX_StateIdle) {
1375 if (!ChangeComponentState(omx_clock,OMX_StateIdle)) {
1376 Log::getInstance()->log("Video", Log::DEBUG, "omx_clock ChangeComponentState Idle failed");
1377 clock_mutex.Unlock();
1381 clock_mutex.Unlock();
1385 int VideoOMX::setClockExecutingandRunning()
1387 OMX_ERRORTYPE error;
1388 OMX_STATETYPE temp_state;
1390 OMX_GetState(omx_clock,&temp_state);
1392 if (temp_state!=OMX_StateExecuting) {
1393 if (!ChangeComponentState(omx_clock,OMX_StateExecuting)) {
1394 Log::getInstance()->log("Video", Log::DEBUG, "omx_clock ChangeComponentState Execute failed");
1395 clock_mutex.Unlock();
1396 DeAllocateCodecsOMX();
1401 OMX_TIME_CONFIG_CLOCKSTATETYPE clock_conf;
1402 memset(&clock_conf,0,sizeof(clock_conf));
1403 clock_conf.nSize=sizeof(clock_conf);
1404 clock_conf.nVersion.nVersion=OMX_VERSION;
1405 clock_conf.eState=OMX_TIME_ClockStateRunning;
1406 error=OMX_SetConfig(omx_clock,OMX_IndexConfigTimeClockState,&clock_conf);
1407 if (error!=OMX_ErrorNone) {
1408 Log::getInstance()->log("Video", Log::DEBUG, "Clock IndexConfigTimeClockState failed %x", error);
1409 clock_mutex.Unlock();
1410 DeAllocateCodecsOMX();
1413 clock_mutex.Unlock();
1419 int VideoOMX::ChangeComponentState(OMX_HANDLETYPE handle,OMX_STATETYPE type) //needs to be called with locked mutex
1421 OMX_ERRORTYPE error;
1422 error=OMX_SendCommand(handle,OMX_CommandStateSet,type,0);
1423 if (error!=OMX_ErrorNone){
1424 Log::getInstance()->log("Video", Log::DEBUG, "handle %x Send Command to OMX State %x %x",handle,type, error);
1428 if (!CommandFinished(handle,OMX_CommandStateSet,type)) {
1436 int VideoOMX::EnablePort(OMX_HANDLETYPE handle,OMX_U32 port,bool wait) //needs to be called with locked mutex
1438 OMX_ERRORTYPE error;
1439 error=OMX_SendCommand(handle,OMX_CommandPortEnable,port,0);
1440 if (error!=OMX_ErrorNone){
1441 Log::getInstance()->log("Video", Log::DEBUG, "handle %x Send Command to enable port %x %x",handle,port, error);
1445 if (!wait) return 1;
1446 if (!CommandFinished(handle,OMX_CommandPortEnable,port)) {
1454 int VideoOMX::DisablePort(OMX_HANDLETYPE handle,OMX_U32 port,bool wait) //needs to be called with locked mutex
1456 OMX_ERRORTYPE error;
1457 error=OMX_SendCommand(handle,OMX_CommandPortDisable,port,0);
1458 if (error!=OMX_ErrorNone){
1459 Log::getInstance()->log("Video", Log::DEBUG, "handle %x Send Command to disable port %x %x",handle,port, error);
1463 if (!wait) return 1;
1464 if (!CommandFinished(handle,OMX_CommandPortDisable,port)) {
1472 int VideoOMX::WaitForEvent(OMX_HANDLETYPE handle,OMX_U32 event) //needs to be called with locked mutex
1476 omx_event_mutex.Lock();
1477 list<VPE_OMX_EVENT>::iterator itty=omx_events.begin();
1478 while (itty!=omx_events.end()) {
1480 VPE_OMX_EVENT current=*itty;
1481 if (current.handle==handle) { //this is ours
1482 if (current.event_type==OMX_EventError) {
1483 omx_events.erase(itty);
1484 omx_event_mutex.Unlock();
1485 Log::getInstance()->log("Video", Log::DEBUG, "WaitForEvent Finished on Error");
1488 } else if (current.event_type==event) {
1489 omx_events.erase(itty);
1490 omx_event_mutex.Unlock();
1491 Log::getInstance()->log("Video", Log::DEBUG, "WaitForEvent Finished Completed");
1498 omx_event_mutex.Unlock();
1503 Log::getInstance()->log("Video", Log::DEBUG, "WaitForEvent waited too long %x %x",handle,event);
1508 int VideoOMX::clearEvents()
1510 omx_event_mutex.Lock();
1512 omx_event_mutex.Unlock();
1518 int VideoOMX::CommandFinished(OMX_HANDLETYPE handle,OMX_U32 command,OMX_U32 data2) //needs to be called with locked mutex
1522 omx_event_mutex.Lock();
1523 list<VPE_OMX_EVENT>::iterator itty=omx_events.begin();
1524 while (itty!=omx_events.end()) {
1526 VPE_OMX_EVENT current=*itty;
1527 if (current.handle==handle) { //this is ours
1528 if (current.event_type==OMX_EventError) {
1529 omx_events.erase(itty);
1530 omx_event_mutex.Unlock();
1531 Log::getInstance()->log("Video", Log::DEBUG, "Command Finished on Error %x",current.data1);
1534 } else if (current.event_type==OMX_EventCmdComplete && current.data1==command && current.data2==data2) {
1535 omx_events.erase(itty);
1536 omx_event_mutex.Unlock();
1537 //Log::getInstance()->log("Video", Log::DEBUG, "Command Finished Completed");
1544 omx_event_mutex.Unlock();
1549 Log::getInstance()->log("Video", Log::DEBUG, "CommandFinished waited too long %x %x %x",handle,command, data2);
1558 int VideoOMX::PrepareInputBufsOMX() //needs to be called with locked mutex
1560 OMX_ERRORTYPE error;
1561 OMX_PARAM_PORTDEFINITIONTYPE port_def_type;
1562 memset(&port_def_type,0,sizeof(port_def_type));
1563 port_def_type.nSize=sizeof(port_def_type);
1564 port_def_type.nVersion.nVersion=OMX_VERSION;
1565 port_def_type.nPortIndex=omx_codec_input_port;
1567 error=OMX_GetParameter(omx_vid_dec,OMX_IndexParamPortDefinition, &port_def_type);
1569 if (error!=OMX_ErrorNone){
1570 Log::getInstance()->log("Video", Log::DEBUG, "Get OMX OMX_IndexParamPortDefinition failed %x", error);
1572 /* Log::getInstance()->log("Video", Log::DEBUG, "Port para %d %d %d %d %d %d %d", port_def_type.nBufferCountActual,
1573 port_def_type.nBufferCountMin,port_def_type.nBufferSize,port_def_type.bEnabled,port_def_type.bPopulated,
1574 port_def_type.bBuffersContiguous,port_def_type.nBufferAlignment);*/
1576 port_def_type.nBufferCountActual=100;
1577 port_def_type.nBufferSize=max(port_def_type.nBufferSize,200000); // for transcoder important
1579 error=OMX_SetParameter(omx_vid_dec,OMX_IndexParamPortDefinition, &port_def_type);
1581 if (error!=OMX_ErrorNone){
1582 Log::getInstance()->log("Video", Log::DEBUG, "Set OMX OMX_IndexParamPortDefinition failed %x", error);
1586 error=OMX_SendCommand(omx_vid_dec,OMX_CommandPortEnable,omx_codec_input_port,0);
1587 if (error!=OMX_ErrorNone){
1588 Log::getInstance()->log("Video", Log::DEBUG, "Prepare Input bufs Send Command to enable port %x", error);
1592 input_bufs_omx_mutex.Lock();
1593 for (unsigned int i=0; i< port_def_type.nBufferCountActual;i++) {
1595 // unsigned char* new_buffer_data=(unsigned char*)malloc(port_def_type.nbufferSize);
1596 OMX_BUFFERHEADERTYPE *buf_head=NULL;
1597 /* error=OMX_Usebuffer(omx_vid_dec,&buf_head,omx_codec_input_port,NULL,port_def_type.nbufferSize,new_buffer_data);
1598 if (error!=OMX_ErrorNone){
1599 Log::getInstance()->log("Video", Log::DEBUG, "Use OMX_Usebuffer failed %x", error);
1600 input_bufs_omx_mutex.Unlock();
1603 error=OMX_AllocateBuffer(omx_vid_dec,&buf_head,omx_codec_input_port,NULL,port_def_type.nBufferSize);
1604 if (error!=OMX_ErrorNone){
1605 Log::getInstance()->log("Video", Log::DEBUG, "Use OMX_AllocateBuffer failed %x", error);
1606 input_bufs_omx_mutex.Unlock();
1609 input_bufs_omx_all.push_back(buf_head);
1610 input_bufs_omx_free.push_back(buf_head);
1612 omx_first_frame=true;
1615 cur_input_buf_omx=NULL;
1616 input_bufs_omx_mutex.Unlock();
1619 Log::getInstance()->log("Video", Log::DEBUG, "PrepareInputBufsOMX mark3");
1620 if (!CommandFinished(omx_vid_dec,OMX_CommandPortEnable,omx_codec_input_port)) {
1623 Log::getInstance()->log("Video", Log::DEBUG, "PrepareInputBufsOMX mark4");
1628 int VideoOMX::DestroyInputBufsOMX() //need s to be called with locked mutex
1630 OMX_ERRORTYPE error;
1632 cur_input_buf_omx=NULL;
1633 input_bufs_omx_mutex.Lock();
1634 for (int i=0; i< input_bufs_omx_all.size();i++) {
1635 // free(input_bufs_omx_all[i]->pBuffer);
1636 // input_bufs_omx_all[i]->pBuffer=NULL;
1637 error=OMX_FreeBuffer(omx_vid_dec,omx_codec_input_port,input_bufs_omx_all[i]);
1638 if (error!=OMX_ErrorNone){
1639 Log::getInstance()->log("Video", Log::DEBUG, "Use OMX_FreeBuffer failed %x", error);
1640 input_bufs_omx_mutex.Unlock();
1645 input_bufs_omx_all.clear();
1646 input_bufs_omx_free.clear();
1647 input_bufs_omx_mutex.Unlock();
1652 int VideoOMX::FlushRenderingPipe()
1654 OMX_ERRORTYPE error;
1658 error = OMX_SendCommand(omx_vid_dec, OMX_CommandFlush,
1659 omx_codec_output_port, NULL);
1660 if (error != OMX_ErrorNone) {
1661 Log::getInstance()->log("Video", Log::DEBUG,
1662 "OMX_Flush codec out 1 failed %x", error);
1666 error = OMX_SendCommand(omx_vid_sched, OMX_CommandFlush,
1667 omx_shed_input_port, NULL);
1668 if (error != OMX_ErrorNone) {
1669 Log::getInstance()->log("Video", Log::DEBUG,
1670 "OMX_Flush shed in 2 failed %x", error);
1674 if (!CommandFinished(omx_vid_dec, OMX_CommandFlush,
1675 omx_codec_output_port)) {
1676 Log::getInstance()->log("Video", Log::DEBUG,
1677 "flush cmd codec 3 failed");
1680 if (!CommandFinished(omx_vid_sched, OMX_CommandFlush,
1681 omx_shed_input_port)) {
1682 Log::getInstance()->log("Video", Log::DEBUG,
1683 "flush cmd shed 4 failed");
1686 error = OMX_SendCommand(omx_vid_dec, OMX_CommandFlush,
1687 omx_codec_output_port, NULL);
1688 if (error != OMX_ErrorNone) {
1689 Log::getInstance()->log("Video", Log::DEBUG,
1690 "OMX_Flush codec out 5 failed %x", error);
1694 error = OMX_SendCommand(omx_vid_deint, OMX_CommandFlush,
1695 omx_deint_input_port, NULL);
1696 if (error != OMX_ErrorNone) {
1697 Log::getInstance()->log("Video", Log::DEBUG,
1698 "OMX_Flush deint in 6 failed %x", error);
1702 if (!CommandFinished(omx_vid_dec, OMX_CommandFlush,
1703 omx_codec_output_port)) {
1704 Log::getInstance()->log("Video", Log::DEBUG,
1705 "flush cmd codec 7 failed");
1708 if (!CommandFinished(omx_vid_deint, OMX_CommandFlush,
1709 omx_deint_input_port)) {
1710 Log::getInstance()->log("Video", Log::DEBUG,
1711 "flush cmd deint 8 failed");
1715 error = OMX_SendCommand(omx_vid_deint, OMX_CommandFlush,
1716 omx_deint_output_port, NULL);
1717 if (error != OMX_ErrorNone) {
1718 Log::getInstance()->log("Video", Log::DEBUG,
1719 "OMX_Flush deint out 9 failed %x", error);
1723 error = OMX_SendCommand(omx_vid_sched, OMX_CommandFlush,
1724 omx_shed_input_port, NULL);
1725 if (error != OMX_ErrorNone) {
1726 Log::getInstance()->log("Video", Log::DEBUG,
1727 "OMX_Flush shed in 10 failed %x", error);
1731 if (!CommandFinished(omx_vid_deint, OMX_CommandFlush,
1732 omx_deint_output_port)) {
1733 Log::getInstance()->log("Video", Log::DEBUG,
1734 "flush cmd deint 11 failed");
1737 if (!CommandFinished(omx_vid_sched, OMX_CommandFlush,
1738 omx_shed_input_port)) {
1739 Log::getInstance()->log("Video", Log::DEBUG,
1740 "flush cmd shed 12 failed");
1750 error = OMX_SendCommand(omx_vid_rend, OMX_CommandFlush,
1751 omx_rend_input_port, NULL);
1752 if (error != OMX_ErrorNone) {
1753 Log::getInstance()->log("Video", Log::DEBUG,
1754 "OMX_Flush rend in failed %x", error);
1758 error = OMX_SendCommand(omx_vid_sched, OMX_CommandFlush,
1759 omx_shed_output_port, NULL);
1760 if (error != OMX_ErrorNone) {
1761 Log::getInstance()->log("Video", Log::DEBUG,
1762 "OMX_Flush shed out failed %x", error);
1768 if (!CommandFinished(omx_vid_rend, OMX_CommandFlush,
1769 omx_rend_input_port)) {
1770 Log::getInstance()->log("Video", Log::DEBUG,
1771 "flush cmd shed rend failed");
1774 if (!CommandFinished(omx_vid_sched, OMX_CommandFlush,
1775 omx_shed_output_port)) {
1776 Log::getInstance()->log("Video", Log::DEBUG,
1777 "flush cmd shed rend failed");
1782 int VideoOMX::DeAllocateCodecsOMX()
1784 OMX_ERRORTYPE error;
1786 Log::getInstance()->log("Video", Log::DEBUG, "enter deallocatecodecsomx");
1788 if (cur_input_buf_omx) {
1789 cur_input_buf_omx->nFlags|=OMX_BUFFERFLAG_EOS;
1790 OMX_ERRORTYPE error=ProtOMXEmptyThisBuffer(omx_vid_dec,cur_input_buf_omx);
1791 if (error!=OMX_ErrorNone) {
1792 Log::getInstance()->log("Video", Log::DEBUG, "OMX_EmptyThisBuffer failed %x", error);
1795 cur_input_buf_omx=NULL;//write out old data
1800 // first stop the omx elements
1801 if (!ChangeComponentState(omx_vid_dec,OMX_StateIdle)) {
1802 Log::getInstance()->log("Video", Log::DEBUG, "vid_dec ChangeComponentState");
1805 clock_mutex.Unlock();
1811 if (!ChangeComponentState(omx_vid_deint, OMX_StateIdle)) {
1812 Log::getInstance()->log("Video", Log::DEBUG,
1813 "vid_deint ChangeComponentState");
1819 if (!ChangeComponentState(omx_vid_sched,OMX_StateIdle)) {
1820 Log::getInstance()->log("Video", Log::DEBUG, "vid_shed ChangeComponentState");
1824 if (!ChangeComponentState(omx_vid_rend,OMX_StateIdle)) {
1825 Log::getInstance()->log("Video", Log::DEBUG, "vid_rend ChangeComponentState");
1831 // TODO proper deinit sequence
1832 // first flush all buffers
1833 FlushRenderingPipe();
1839 error=OMX_SendCommand(omx_clock,OMX_CommandFlush, omx_clock_output_port, NULL);
1840 if (error!=OMX_ErrorNone){
1841 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Flush clock out failed %x", error);
1845 error=OMX_SendCommand(omx_vid_sched,OMX_CommandFlush, omx_shed_clock_port, NULL);
1846 if (error!=OMX_ErrorNone){
1847 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Flush shed clock failed %x", error);
1851 if (!CommandFinished(omx_clock,OMX_CommandFlush,omx_clock_output_port) ||
1852 !CommandFinished(omx_vid_sched,OMX_CommandFlush,omx_shed_clock_port)) {
1853 Log::getInstance()->log("Video", Log::DEBUG, "flush cmd clock shed failed");
1859 error = OMX_SendCommand(omx_vid_dec, OMX_CommandFlush,
1860 omx_codec_input_port, NULL);
1861 if (error != OMX_ErrorNone) {
1862 Log::getInstance()->log("Video", Log::DEBUG,
1863 "OMX_Flush codec out failed %x", error);
1870 DestroyInputBufsOMX();
1873 if (!DisablePort(omx_vid_sched,omx_shed_output_port,true)) {
1874 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 2 ");
1876 if (!DisablePort(omx_vid_rend,omx_rend_input_port,true)) {
1877 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 1");
1883 if (!DisablePort(omx_vid_dec,omx_codec_output_port,true)) {
1884 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 6");
1889 if (!DisablePort(omx_vid_dec,omx_codec_input_port,true)) {
1890 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 7");
1894 if (!DisablePort(omx_vid_deint,omx_deint_output_port,true)) {
1895 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 6a");
1900 if (!DisablePort(omx_vid_deint,omx_deint_input_port,true)) {
1901 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 7a");
1907 if (!DisablePort(omx_vid_sched,omx_shed_input_port,true)) {
1908 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 3");
1911 if (!DisablePort(omx_vid_sched,omx_shed_clock_port,true)) {
1912 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 4");
1915 if (!DisablePort(omx_clock,omx_clock_output_port,true)) {
1916 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 5");
1922 error=OMX_SetupTunnel(omx_vid_dec,omx_codec_output_port,NULL,NULL);
1923 if (error!=OMX_ErrorNone) {
1924 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1929 error=OMX_SetupTunnel(omx_vid_deint,omx_deint_input_port,NULL,NULL);
1930 if (error!=OMX_ErrorNone) {
1931 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1935 error=OMX_SetupTunnel(omx_vid_deint,omx_deint_output_port,NULL,NULL);
1936 if (error!=OMX_ErrorNone) {
1937 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1941 error=OMX_SetupTunnel(omx_vid_sched,omx_shed_input_port,NULL,NULL);
1942 if (error!=OMX_ErrorNone) {
1943 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1948 error=OMX_SetupTunnel(omx_vid_sched,omx_shed_output_port,NULL,NULL);
1949 if (error!=OMX_ErrorNone) {
1950 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1954 error=OMX_SetupTunnel(omx_vid_rend,omx_rend_input_port,NULL,NULL);
1955 if (error!=OMX_ErrorNone) {
1956 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1961 error=OMX_SetupTunnel(omx_clock,omx_clock_output_port,NULL,NULL);
1962 if (error!=OMX_ErrorNone) {
1963 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1967 error=OMX_SetupTunnel(omx_vid_sched,omx_shed_clock_port,NULL,NULL);
1968 if (error!=OMX_ErrorNone) {
1969 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1976 error=OMX_FreeHandle(omx_vid_dec);
1977 error=OMX_FreeHandle(omx_vid_sched);
1978 error=OMX_FreeHandle(omx_vid_rend);
1979 if (dodeint) error=OMX_FreeHandle(omx_vid_deint);
1981 clock_mutex.Unlock();
1983 if (error!=OMX_ErrorNone) {
1984 Log::getInstance()->log("Video", Log::DEBUG, "FreeHandle failed %d", error);
1986 } else clock_mutex.Unlock();
1987 Log::getInstance()->log("Video", Log::DEBUG, "leave deallocate codecs OMX");
1993 void VideoOMX::destroyClock()
1996 if (clock_references>0) {
1998 if (clock_references==0) {
1999 OMX_ERRORTYPE error;
2000 Log::getInstance()->log("Video", Log::DEBUG, "destroy omx clock");
2001 error=OMX_FreeHandle(omx_clock);
2002 if (error!=OMX_ErrorNone) {
2003 Log::getInstance()->log("Video", Log::DEBUG, "FreeHandle failed %d", error);
2008 clock_mutex.Unlock();
2012 int VideoOMX::stop()
2014 if (!initted) return 0;
2017 //Check if libav mode
2018 DeAllocateCodecsOMX();
2023 // if (ioctl(fdVideo, AV_SET_VID_STOP, 0) != 0) return 0;
2027 int VideoOMX::reset()
2029 if (!initted) return 0;
2032 DeAllocateCodecsOMX();
2033 // if (ioctl(fdVideo, AV_SET_VID_RESET, 0x11) != 0) return 0;
2037 int VideoOMX::pause()
2039 if (!initted) return 0;
2040 Log::getInstance()->log("Video", Log::DEBUG, "enter pause");
2043 //maybe also change omx clock?
2044 //pausetimecode=GetCurrentSystemTime();
2050 int VideoOMX::unPause() // FIXME get rid - same as play!! Not here!
2052 if (!initted) return 0;
2053 Log::getInstance()->log("Video", Log::DEBUG, "enter unpause");
2056 // playbacktimeoffset+=GetCurrentSystemTime()-pausetimecode;
2057 paused=false; // may be also change omx clock
2063 int VideoOMX::fastForward()
2065 if (!initted) return 0;
2067 // if (ioctl(fdVideo, AV_SET_VID_libavWD, 1) != 0) return 0;
2071 int VideoOMX::unFastForward()
2073 if (!initted) return 0;
2075 // if (ioctl(fdVideo, AV_SET_VID_RESET, 0x11) != 0) return 0; // don't need this.
2077 //// if (ioctl(fdVideo, AV_SET_VID_PLAY, 0) != 0) return 0;
2081 int VideoOMX::attachFrameBuffer()
2083 if (!initted) return 0;
2085 // if (ioctl(fdVideo, AV_SET_VID_FB, 0) != 0) return 0;
2089 int VideoOMX::blank(void)
2091 // if (ioctl(fdVideo, AV_SET_VID_FB, 1) != 0) return 0;
2092 // if (ioctl(fdVideo, AV_SET_VID_FB, 0) != 0) return 0;
2096 ULLONG VideoOMX::getCurrentTimestamp() {
2099 long long ncur_clock_time = cur_clock_time;
2103 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldcancelstate);
2104 pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldcanceltype);
2106 OMX_ERRORTYPE error;
2107 OMX_TIME_CONFIG_CLOCKSTATETYPE clock_conf;
2108 memset(&clock_conf, 0, sizeof(clock_conf));
2109 clock_conf.nSize = sizeof(clock_conf);
2110 clock_conf.nVersion.nVersion = OMX_VERSION;
2111 error= OMX_GetConfig(omx_clock, OMX_IndexConfigTimeClockState,
2113 if (error != OMX_ErrorNone) {
2114 Log::getInstance()->log("Video", Log::DEBUG,"getCurrentTimestamp IndexConfigTimeClockState failed %x",error);
2117 if (clock_conf.eState == OMX_TIME_ClockStateRunning) {
2119 OMX_TIME_CONFIG_TIMESTAMPTYPE cur_time_stamp;
2120 memset(&cur_time_stamp, 0, sizeof(cur_time_stamp));
2121 cur_time_stamp.nSize = sizeof(cur_time_stamp);
2122 cur_time_stamp.nVersion.nVersion = OMX_VERSION;
2123 cur_time_stamp.nPortIndex = omx_clock_output_port;
2124 error = OMX_GetConfig(omx_clock, OMX_IndexConfigTimeCurrentMediaTime,
2126 if (error != OMX_ErrorNone) {
2127 Log::getInstance()->log("Video",Log::DEBUG,"getCurrentTimestamp OMX_IndexConfigTimeCurrentMediaTime failed %x",error);
2129 long long temp = cur_time_stamp.nTimestamp.nLowPart
2130 | ((long long) cur_time_stamp.nTimestamp.nHighPart << 32);
2131 ncur_clock_time = cur_clock_time = temp * 10LL;
2134 clock_mutex.Unlock();
2135 pthread_setcancelstate(oldcancelstate, NULL);
2136 pthread_setcanceltype(oldcanceltype, NULL);
2139 //ncur_clock_time -= startoffset;
2140 ncur_clock_time -= lastreftimeOMX;
2141 long long result = lastreftimePTS;
2142 result += (long long) (ncur_clock_time / 10000LL * 90LL);
2144 result = (1LL << 33) - result;
2145 //Log::getInstance()->log("Video", Log::DEBUG,"getCurrentTimestamp %lld %lld %lld %lld %lld %lld",ncur_clock_time,cur_clock_time,lastreftimeOMX,lastreftimePTS,result,startoffset);
2153 ULONG VideoOMX::timecodeToFrameNumber(ULLONG timecode)
2155 if (format == PAL) return (ULONG)(((double)timecode / (double)90000) * (double)25);
2156 else return (ULONG)(((double)timecode / (double)90000) * (double)30);
2161 int VideoOMX::test()
2166 // return ioctl(fdVideo, AV_SET_VID_STC, &stc);
2173 int VideoOMX::test2()
2181 long long VideoOMX::SetStartOffset(long long curreftime, bool *rsync)
2185 startoffset=curreftime;//offset is set for audio
2187 offsetvideonotset=false;
2189 if (offsetvideonotset) {
2190 offsetvideonotset=false;
2193 if ( (curreftime-lastrefvideotime)>10000000LL
2194 || (curreftime-lastrefvideotime)<-10000000LL) {//if pts jumps to big resync
2195 startoffset+=curreftime-lastrefvideotime;
2196 lastrefaudiotime+=curreftime-lastrefvideotime;
2198 offsetaudionotset=true;
2205 lastrefvideotime=curreftime;
2211 long long VideoOMX::SetStartAudioOffset(long long curreftime, bool *rsync)
2215 startoffset=curreftime;
2217 offsetaudionotset=false;
2219 if (offsetaudionotset) {
2220 offsetaudionotset=false;
2223 if ( (curreftime-lastrefaudiotime)>10000000LL
2224 || (curreftime-lastrefaudiotime)<-10000000LL) {//if pts jumps to big resync
2225 startoffset+=curreftime-lastrefaudiotime;
2226 lastrefvideotime+=curreftime-lastrefaudiotime;
2228 offsetvideonotset=true;
2234 lastrefaudiotime=curreftime;
2239 void VideoOMX::ResetTimeOffsets() {
2240 offsetnotset=true; //called from demuxer
2241 offsetvideonotset=true;
2242 offsetaudionotset=true;
2251 void VideoOMX::DeinterlaceFix()
2255 Demuxer* demux=Demuxer::getInstance();
2256 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldcancelstate);
2257 pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldcanceltype);
2259 OMX_ERRORTYPE error;
2260 OMX_PARAM_PORTDEFINITIONTYPE port_def_type;
2261 memset(&port_def_type,0,sizeof(port_def_type));
2262 port_def_type.nSize=sizeof(port_def_type);
2263 port_def_type.nVersion.nVersion=OMX_VERSION;
2264 port_def_type.nPortIndex=omx_codec_output_port;
2266 error=OMX_GetParameter(omx_vid_dec,OMX_IndexParamPortDefinition, &port_def_type);
2267 if (error != OMX_ErrorNone) {
2268 Log::getInstance()->log("Video", Log::DEBUG,
2269 "OMX_IndexParamPortDefinition fix failed %x", error);
2270 clock_mutex.Unlock();
2274 if (port_def_type.format.video.nFrameWidth == demux->getHorizontalSize()
2275 && port_def_type.format.video.nFrameHeight == demux->getVerticalSize()){
2276 Log::getInstance()->log("Video", Log::DEBUG,
2277 "Deinit first frame fix");
2278 deint_first_frame=false;
2280 WaitForEvent(omx_vid_dec,OMX_EventPortSettingsChanged);
2281 DisablePort(omx_vid_dec,omx_codec_output_port,false);
2282 DisablePort(omx_vid_sched,omx_shed_input_port,false);
2283 DisablePort(omx_vid_deint,omx_deint_output_port,false);
2284 DisablePort(omx_vid_deint,omx_deint_input_port,false);
2286 port_def_type.nPortIndex=omx_deint_input_port;
2287 error = OMX_SetParameter(omx_vid_deint, OMX_IndexParamPortDefinition,
2289 if (error != OMX_ErrorNone) {
2290 Log::getInstance()->log("Video", Log::DEBUG,
2291 "Set OMX_IndexParamPortDefinition1 failed %x", error);
2292 clock_mutex.Unlock();
2293 pthread_setcancelstate(oldcancelstate, NULL);
2294 pthread_setcanceltype(oldcanceltype, NULL);
2298 port_def_type.nPortIndex=omx_deint_output_port;
2299 error = OMX_SetParameter(omx_vid_deint, OMX_IndexParamPortDefinition,
2301 if (error != OMX_ErrorNone) {
2302 Log::getInstance()->log("Video", Log::DEBUG,
2303 "Set OMX_IndexParamPortDefinition2 failed %x", error);
2304 clock_mutex.Unlock();
2305 pthread_setcancelstate(oldcancelstate, NULL);
2306 pthread_setcanceltype(oldcanceltype, NULL);
2311 EnablePort(omx_vid_dec,omx_codec_output_port,false);
2312 EnablePort(omx_vid_deint,omx_deint_input_port,false);
2313 EnablePort(omx_vid_deint,omx_deint_output_port,false);
2314 EnablePort(omx_vid_sched,omx_shed_input_port,false);
2316 clock_mutex.Unlock();
2317 pthread_setcancelstate(oldcancelstate, NULL);
2318 pthread_setcanceltype(oldcanceltype, NULL);
2325 void VideoOMX::PutBufferToPres(OMX_BUFFERHEADERTYPE* buffer)
2328 OMX_ERRORTYPE error = ProtOMXEmptyThisBuffer(omx_vid_dec, buffer);
2329 if (error != OMX_ErrorNone) {
2330 Log::getInstance()->log("Video", Log::DEBUG,
2331 "OMX_EmptyThisBuffer failed %x", error);
2333 if (deint_first_frame && dodeint) DeinterlaceFix();
2337 OMX_ERRORTYPE VideoOMX::ProtOMXEmptyThisBuffer(OMX_HANDLETYPE handle, OMX_BUFFERHEADERTYPE* buffer)
2339 // protect the call to empty this buffer
2342 /* long long temp =buffer->nTimeStamp.nLowPart
2343 | ((long long) buffer->nTimeStamp.nHighPart << 32);*/
2345 //pthread_testcancel();
2346 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldcancelstate);
2347 pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldcanceltype);
2350 /* OMX_ERRORTYPE error;
2351 OMX_TIME_CONFIG_TIMESTAMPTYPE timestamp;
2352 memset(×tamp, 0, sizeof(timestamp));
2353 timestamp.nSize = sizeof(timestamp);
2354 timestamp.nVersion.nVersion = OMX_VERSION;
2355 timestamp.nPortIndex =omx_clock_output_port;
2357 error = OMX_GetConfig(omx_clock, OMX_IndexConfigTimeCurrentMediaTime,
2360 if (error != OMX_ErrorNone) {
2361 Log::getInstance()->log("Audio", Log::DEBUG,
2362 "Init OMX_IndexConfigAudioRenderingLatencyfailed %x %d", error,
2363 omx_rend_input_port);
2365 long long temp2 =timestamp.nTimestamp.nLowPart
2366 | ((long long) timestamp.nTimestamp.nHighPart << 32);
2369 Log::getInstance()->log("Video", Log::NOTICE, "OMXETB %x %lld %lld %x",handle,temp,temp2,buffer->nFlags);*/
2370 OMX_ERRORTYPE ret_val;
2371 ret_val=OMX_EmptyThisBuffer(handle,buffer);
2372 clock_mutex.Unlock();
2373 pthread_setcancelstate(oldcancelstate, NULL);
2374 pthread_setcanceltype(oldcanceltype, NULL);
2375 //pthread_testcancel();
2379 bool VideoOMX::detectIFrame(const UCHAR *buffer,unsigned int length)
2381 const UCHAR* curbuf=buffer;
2382 const UCHAR* curbufend=buffer+length;
2383 unsigned int detector=0xFFFFFFFF;
2384 bool gotaud=false; // have seen access unit delimiter
2386 while (curbuf!=curbufend) {
2390 if (detector==0x00000109) {
2392 detector=0xFFFFFFFF;
2393 } else if (gotaud &&detector==0x00000001) {
2395 if (curbuf!=curbufend) {
2396 unsigned char picttype=(*curbuf)& 0x1F;
2397 return picttype==0x07;
2401 if (detector==0x00000100) {
2403 if (curbuf==curbufend) return false;
2405 if (curbuf==curbufend) return false;
2406 unsigned char picttype=((*curbuf) >> 3) & 0x07;
2413 return false; // no frame found
2416 void VideoOMX::PrepareMediaSample(const MediaPacketList& mplist,UINT samplepos)
2419 mediapackets.clear();
2420 list<MediaPacket>::const_iterator begin=mplist.begin();
2421 list<MediaPacket>::const_iterator itty=mplist.begin();
2422 advance(itty,min(mplist.size(),10));
2423 mediapackets.insert(mediapackets.begin(),begin,itty);//front
2427 UINT VideoOMX::DeliverMediaSample(UCHAR* buffer, UINT *samplepos)
2430 while (consumed<mediapackets.size()) {
2431 DeliverMediaPacket(mediapackets[consumed], buffer, samplepos);
2432 if (*samplepos == mediapackets[consumed].length) {
2436 } else return consumed;
2441 UINT VideoOMX::DeliverMediaPacket(MediaPacket packet,
2442 const UCHAR* buffer,
2445 if (packet.type == MPTYPE_VIDEO_H264)
2455 //Later add fail back code for libav
2457 *samplepos+=packet.length;
2458 return packet.length;
2462 if (!omx_running) return 0; // if we are not runnig do not do this
2463 if (paused) return 0; //Block if we pause
2465 // Log::getInstance()->log("Video", Log::DEBUG, "DMP mark 1");
2466 //Log::getInstance()->log("Video", Log::DEBUG, "DeliverMediaPacketOMX time %lld",packet.presentation_time);
2469 OMX_ERRORTYPE error;
2471 /*First Check, if we have an video sample*/
2475 return 0; //Not in iframe mode!
2479 if (packet.disconti) {
2481 if (cur_input_buf_omx) {
2482 PutBufferToPres(cur_input_buf_omx);
2483 cur_input_buf_omx=NULL;
2486 //Log::getInstance()->log("Video", Log::DEBUG, "DMP mark 2");
2487 /*Inspect PES-Header */
2489 // OMX_STATETYPE temp_state;
2490 // OMX_GetState(omx_vid_dec,&temp_state);
2492 if (*samplepos==0) {//stripheader
2493 headerstrip=buffer[packet.pos_buffer+8]+9/*is this right*/;
2494 // if (h264) Log::getInstance()->log("Video", Log::DEBUG, "PES info %x %x %x %x",
2495 // buffer[packet.pos_buffer+0],buffer[packet.pos_buffer+1],buffer[packet.pos_buffer+2],buffer[packet.pos_buffer+3]);
2496 *samplepos+=headerstrip;
2497 if ( packet.synched ) {
2498 if (!firstsynched) {
2499 //Log::getInstance()->log("Video", Log::DEBUG, "DMP mark 2a");
2500 // check if this is an I frame, the decoder does not like non I frames at startup!
2501 if (!detectIFrame(buffer,packet.length)) {
2502 *samplepos=packet.length;//if we have not processed at least one
2503 //Log::getInstance()->log("Video", Log::DEBUG, "DMP mark 3");
2504 return packet.length;//synched packet ignore it!
2507 if (cur_input_buf_omx) {
2508 //Log::getInstance()->log("Video", Log::DEBUG, "DMP mark 4a");
2509 cur_input_buf_omx->nFlags|=OMX_BUFFERFLAG_ENDOFFRAME;
2510 PutBufferToPres(cur_input_buf_omx);
2511 cur_input_buf_omx=NULL;//write out old data
2513 //Log::getInstance()->log("Video", Log::DEBUG, "DMP mark 4b");
2518 if (!firstsynched) {//
2519 *samplepos=packet.length;//if we have not processed at least one
2520 //Log::getInstance()->log("Video", Log::DEBUG, "DMP mark 5");
2521 return packet.length;//synched packet ignore it!
2525 //Log::getInstance()->log("Video", Log::DEBUG, "DMP mark 6");
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 //Log::getInstance()->log("Video", Log::DEBUG, "DMP mark 7");
2532 return 0; // we do not have a free media sample
2535 cur_input_buf_omx=input_bufs_omx_free.front();
2536 cur_input_buf_omx->nFilledLen=0;
2537 cur_input_buf_omx->nOffset=0;
2538 cur_input_buf_omx->nTimeStamp=intToOMXTicks(0);
2539 input_bufs_omx_free.pop_front();
2540 input_bufs_omx_mutex.Unlock();
2546 if (cur_input_buf_omx->nFilledLen==0) {//will only be changed on first packet
2547 if (packet.synched) {
2548 // Log::getInstance()->log("Video", Log::DEBUG, "packet synched marker");
2550 //lastreftimePTS=packet.pts;
2551 if (omx_first_frame) { // TODO time
2552 cur_input_buf_omx->nFlags=OMX_BUFFERFLAG_STARTTIME;
2553 Log::getInstance()->log("Video", Log::DEBUG, "Starttime");
2554 omx_first_frame=false;
2556 cur_input_buf_omx->nFlags=0;
2557 //cur_input_buf_omx->nFlags|=OMX_BUFFERFLAG_TIME_UNKNOWN;
2559 lastreftimeOMX=packet.presentation_time;
2560 // Log::getInstance()->log("Video", Log::DEBUG, "Time code %lld pts %lld", lastreftimeOMX,packet.pts);
2561 lastreftimePTS=packet.pts;
2562 cur_input_buf_omx->nTimeStamp=intToOMXTicks(lastreftimeOMX/10LL); // the clock component is faulty;
2566 cur_input_buf_omx->nFlags=OMX_BUFFERFLAG_TIME_UNKNOWN;
2567 cur_input_buf_omx->nTimeStamp=intToOMXTicks(0);
2568 //Log::getInstance()->log("Video", Log::DEBUG, "packet unsynched marker");
2570 if (packet.disconti) cur_input_buf_omx->nFlags|=OMX_BUFFERFLAG_DISCONTINUITY;
2575 unsigned int haveToCopy=packet.length-*samplepos;
2576 //Log::getInstance()->log("Video", Log::DEBUG, "DMP mark 8");
2578 while (haveToCopy> (cur_input_buf_omx->nAllocLen-cur_input_buf_omx->nFilledLen)) {
2579 //Log::getInstance()->log("Video", Log::DEBUG, "Big buffer %d %d %d",packet.length,cur_input_buf_omx->nAllocLen,cur_input_buf_omx->nFilledLen);
2580 unsigned int cancopy=cur_input_buf_omx->nAllocLen-cur_input_buf_omx->nFilledLen;
2581 memcpy(cur_input_buf_omx->pBuffer+cur_input_buf_omx->nFilledLen,buffer+packet.pos_buffer+*samplepos,cancopy);
2582 haveToCopy-=cancopy;
2583 cur_input_buf_omx->nFilledLen+=cancopy;
2584 *samplepos+=cancopy;
2585 // push old buffer out
2586 //Log::getInstance()->log("Video", Log::DEBUG, "DMP mark 9");
2588 PutBufferToPres(cur_input_buf_omx);
2589 cur_input_buf_omx=NULL;
2591 input_bufs_omx_mutex.Lock();
2592 if (input_bufs_omx_free.size()==0) {
2593 input_bufs_omx_mutex.Unlock();
2594 // Log::getInstance()->log("Video", Log::DEBUG, "Deliver MediaPacket no free sample2");
2595 return *samplepos; // we do not have a free media sample
2597 cur_input_buf_omx=input_bufs_omx_free.front();
2598 cur_input_buf_omx->nFilledLen=0;
2599 cur_input_buf_omx->nOffset=0;
2600 cur_input_buf_omx->nFlags=OMX_BUFFERFLAG_TIME_UNKNOWN;
2601 cur_input_buf_omx->nTimeStamp=intToOMXTicks(0);
2602 input_bufs_omx_free.pop_front();
2603 input_bufs_omx_mutex.Unlock();
2604 //Log::getInstance()->log("Video", Log::DEBUG, "DMP mark 10");
2607 memcpy(cur_input_buf_omx->pBuffer+cur_input_buf_omx->nFilledLen,
2608 buffer+packet.pos_buffer+*samplepos,haveToCopy);
2609 cur_input_buf_omx->nFilledLen+=haveToCopy;
2611 // Log::getInstance()->log("Video", Log::DEBUG, "DMP mark 11");
2613 *samplepos+=haveToCopy;
2622 bool VideoOMX::displayIFrame(const UCHAR* buffer, UINT length) {
2623 if (!omx_running) return false;
2625 EnterIframePlayback();
2627 int haveToCopy = length;
2629 if (!cur_input_buf_omx) {
2630 input_bufs_omx_mutex.Lock();
2631 if (input_bufs_omx_free.size() == 0) {
2632 input_bufs_omx_mutex.Unlock();
2633 // Log::getInstance()->log("Video", Log::DEBUG,
2634 // "Deliver MediaPacket no free sample");
2635 return false; // we do not have a free media sample
2638 cur_input_buf_omx = input_bufs_omx_free.front();
2639 cur_input_buf_omx->nFilledLen = 0;
2640 cur_input_buf_omx->nOffset = 0;
2641 cur_input_buf_omx->nTimeStamp = intToOMXTicks(0);
2642 input_bufs_omx_free.pop_front();
2643 input_bufs_omx_mutex.Unlock();
2647 unsigned int pattern, packet_length;
2648 unsigned int headerstrip = 0;
2653 //Now we strip the pes header
2654 pattern = (buffer[0] << 16) | (buffer[1] << 8) | (buffer[2]);
2655 while (read_pos + 7 <= length) {
2656 pattern = ((pattern << 8) & 0xFFFFFFFF) | buffer[read_pos + 3];
2657 if (pattern < 0x000001E0 || pattern > 0x000001EF) {
2661 headerstrip = buffer[read_pos + 8] + 9/*is this right*/;
2662 packet_length = ((buffer[read_pos + 4] << 8)
2663 | (buffer[read_pos + 5])) + 6;
2664 if (read_pos + packet_length > length)
2667 if ((headerstrip < packet_length)
2668 && (cur_input_buf_omx->nFilledLen + packet_length
2669 - headerstrip) > cur_input_buf_omx->nAllocLen) {
2671 cur_input_buf_omx->nFlags = OMX_BUFFERFLAG_STARTTIME;
2674 cur_input_buf_omx->nFlags
2675 |= OMX_BUFFERFLAG_TIME_UNKNOWN;
2678 cur_input_buf_omx->nTimeStamp = intToOMXTicks(0);
2679 PutBufferToPres(cur_input_buf_omx);
2680 cur_input_buf_omx = NULL;
2682 if (!cur_input_buf_omx) {
2684 while (count < 100 && omx_running && iframemode) {
2687 input_bufs_omx_mutex.Lock();
2688 if (input_bufs_omx_free.size() == 0) {
2689 input_bufs_omx_mutex.Unlock();
2690 // Log::getInstance()->log("Video", Log::DEBUG,
2691 // "Ifrane no free sample");
2693 if (!omx_running) return false;
2696 cur_input_buf_omx = input_bufs_omx_free.front();
2697 cur_input_buf_omx->nFilledLen = 0;
2698 cur_input_buf_omx->nOffset = 0;
2699 cur_input_buf_omx->nTimeStamp = intToOMXTicks(0);
2700 cur_input_buf_omx->nFlags|= OMX_BUFFERFLAG_TIME_UNKNOWN;
2701 input_bufs_omx_free.pop_front();
2702 input_bufs_omx_mutex.Unlock();
2705 if (!cur_input_buf_omx)
2710 if (packet_length > headerstrip) {
2712 cur_input_buf_omx->pBuffer
2713 + cur_input_buf_omx->nFilledLen,
2714 buffer + read_pos + headerstrip,
2715 packet_length - headerstrip);
2716 cur_input_buf_omx->nFilledLen += packet_length
2719 read_pos += packet_length;
2721 pattern = (buffer[read_pos] << 16)
2722 | (buffer[read_pos + 1] << 8) | (buffer[read_pos + 2]);
2728 cur_input_buf_omx->nFlags = OMX_BUFFERFLAG_STARTTIME;
2731 cur_input_buf_omx->nFlags |= OMX_BUFFERFLAG_TIME_UNKNOWN;
2734 cur_input_buf_omx->nTimeStamp = intToOMXTicks(0);
2736 PutBufferToPres(cur_input_buf_omx);
2737 cur_input_buf_omx = NULL;
2740 MILLISLEEP(40); //Block a bit
2744 int VideoOMX::EnterIframePlayback()
2746 Log::getInstance()->log("Video", Log::DEBUG,
2747 "EnterIframePlayback");
2748 if (cur_input_buf_omx) {
2749 PutBufferToPres(cur_input_buf_omx);
2750 cur_input_buf_omx = NULL;
2752 Log::getInstance()->log("Video", Log::DEBUG,
2753 "EnterIframePlayback 2");
2754 ((AudioOMX*)Audio::getInstance())->DeAllocateCodecsOMX();
2755 DeAllocateCodecsOMX();
2756 AllocateCodecsOMX();
2757 Log::getInstance()->log("Video", Log::DEBUG,
2758 "leave IframePlayback");