2 Copyright 2004-2005 Chris Tallon, 2009 Marten Richter
4 This file is part of VOMP.
6 VOMP is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 VOMP is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with VOMP; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 #include "mtdraspberry.h"
25 #include "osdopengl.h"
27 #include "woptionpane.h"
32 //A lot of parts of this file are heavily inspired by xbmc omx implementations
34 VideoOMX::VideoOMX() {
39 cur_input_buf_omx = NULL;
40 omx_h264 = omx_mpeg2 = true;
44 offsetvideonotset = true;
45 offsetaudionotset = true;
55 deinterlace=2; //advanced
64 int VideoOMX::init(UCHAR tformat)
66 if (initted) return 0;
69 if (!setFormat(tformat)) { shutdown(); return 0; }
70 if (!setConnection(COMPOSITERGB)) { shutdown(); return 0; }
71 if (!setAspectRatio(ASPECT4X3)) { shutdown(); return 0; }
72 if (!setMode(NORMAL)) { shutdown(); return 0; }
73 if (!setSource()) { shutdown(); return 0; }
74 if (!attachFrameBuffer()) { shutdown(); return 0; }
89 int VideoOMX::initUsingOSDObjects()
92 // we are called before the audio
95 if (error!=OMX_ErrorNone) {
96 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX failed %x", error);
104 OMX_ERRORTYPE VideoOMX::EventHandler_OMX(OMX_IN OMX_HANDLETYPE handle,OMX_IN OMX_PTR appdata,
105 OMX_IN OMX_EVENTTYPE event_type,OMX_IN OMX_U32 data1,
106 OMX_IN OMX_U32 data2,OMX_IN OMX_PTR event_data) {
108 Log::getInstance()->log("Video", Log::NOTICE, "eventHandler %x %x %x %x %x",handle,event_type,data1,data2,event_data);
110 struct VPE_OMX_EVENT new_event;
111 new_event.handle=handle;
112 new_event.appdata=appdata;
113 new_event.event_type=event_type;
114 new_event.data1=data1;
115 new_event.data2=data2;
116 new_event.event_data=event_data;
118 VideoOMX *video=(VideoOMX *)getInstance();
119 video->AddOmxEvent(new_event);
121 /* switch (event_type) {
122 case OMX_EventCmdComplete: {
127 return OMX_ErrorNone;
131 void VideoOMX::AddOmxEvent(VPE_OMX_EVENT new_event)
133 omx_event_mutex.Lock();
134 omx_events.push_back(new_event);
135 omx_event_mutex.Unlock();
139 OMX_ERRORTYPE VideoOMX::EmptyBufferDone_OMX(OMX_IN OMX_HANDLETYPE hcomp,OMX_IN OMX_PTR appdata,OMX_IN OMX_BUFFERHEADERTYPE* buffer){
141 //Log::getInstance()->log("Video", Log::NOTICE, "EmptyBufferDone");
142 VideoOMX *video=(VideoOMX *)getInstance();
143 video->ReturnEmptyOMXBuffer(buffer);
144 return OMX_ErrorNone;
148 void VideoOMX::ReturnEmptyOMXBuffer(OMX_BUFFERHEADERTYPE* buffer){
149 input_bufs_omx_mutex.Lock();
150 //Log::getInstance()->log("Video", Log::NOTICE, "ReturnEmptyOMXBuffer %d %d %d",input_bufs_omx_free.size(),input_bufs_omx_present.size(),input_bufs_omx_all.size());
151 input_bufs_omx_free.push_back(buffer);
152 //Log::getInstance()->log("Video", Log::NOTICE, "ReturnEmptyOMXBuffer %d",input_bufs_omx_free.size());
153 input_bufs_omx_mutex.Unlock();
156 OMX_ERRORTYPE VideoOMX::FillBufferDone_OMX(OMX_IN OMX_HANDLETYPE hcomp, OMX_IN OMX_PTR appdata,OMX_IN OMX_BUFFERHEADERTYPE* buffer) {
157 Log::getInstance()->log("Video", Log::NOTICE, "FillBufferDone");
158 return OMX_ErrorNone;
163 int VideoOMX::shutdown()
165 if (!initted) return 0;
168 DeAllocateCodecsOMX();
175 bool VideoOMX::loadOptionsfromServer(VDR* vdr)
177 Log::getInstance()->log("Video", Log::DEBUG, "VideoOMX config load");
178 char *name=vdr->configLoad("VideoOMX","SDDeinterlacing");
181 if (STRCASECMP(name, "None") == 0) {
183 }/* else if (STRCASECMP(name, "LineDouble") == 0) {
185 }*/ else if (STRCASECMP(name, "Advanced") == 0) {
187 } /*else if (STRCASECMP(name, "Crazy") == 0) {
188 deinterlace = 3; // this does not activate deinterlacing but a image filter, just for fun
190 Log::getInstance()->log("Video", Log::DEBUG, "Set deinterlacing to %s %d",name,deinterlace);
197 bool VideoOMX::handleOptionChanges(Option* option)
199 if (Video::handleOptionChanges(option))
201 switch (option->id) {
203 if (STRCASECMP(option->options[option->userSetChoice], "None") == 0) {
205 } /*else if (STRCASECMP(option->options[option->userSetChoice], "LineDouble")
208 }*/ else if (STRCASECMP(option->options[option->userSetChoice], "Advanced")
211 } /*else if (STRCASECMP(option->options[option->userSetChoice], "Crazy")
215 Log::getInstance()->log("Video", Log::DEBUG, "Set deinterlacing to %s %d",option->options[option->userSetChoice],deinterlace);
224 bool VideoOMX::saveOptionstoServer()
227 switch (deinterlace) {
229 VDR::getInstance()->configSave("VideoOMX","SDDeinterlacing", "None");
232 VDR::getInstance()->configSave("VideoOMX","SDDeinterlacing", "LineDouble");
235 VDR::getInstance()->configSave("VideoOMX","SDDeinterlacing", "Advanced");
238 VDR::getInstance()->configSave("VideoOMX","SDDeinterlacing", "Crazy");
245 /*Option(UINT id, const char* displayText, const char* configSection, const char* configKey, UINT optionType,
246 UINT numChoices, UINT defaultChoice, UINT startInt,
247 const char * const * options, const char * const * optionkeys = NULL, AbstractOption* handler=NULL);*/
249 bool VideoOMX::addOptionsToPanes(int panenumber,Options *options,WOptionPane* pane)
251 if (!Video::addOptionsToPanes(panenumber,options,pane)) return false;
257 static const char* deinterlaceopts[]={"None",/*"LineDouble",*/"Advanced"/*,"Crazy"*/};
258 option = new Option(1,tr("SD Deinterlacing"), "VideoOMX","SDDeinterlacing",Option::TYPE_TEXT,/*4,2*/2,1,0,deinterlaceopts,NULL,false,this);
259 options->push_back(option);
260 pane->addOptionLine(option);
268 int VideoOMX::setTVsize(UCHAR ttvsize)
272 // Override the aspect ratio usage, temporarily use to set the video chip mode
273 if (!setAspectRatio(tvsize)) { shutdown(); return 0; }
275 if ((fdVideo = open("/dev/vdec_dev", O_WRONLY)) < 0) return 0;
276 if (!setSource()) { shutdown(); return 0; }
277 if (!attachFramebuffer()) { shutdown(); return 0; }
279 // Reopening the fd causes the scart aspect line to go back to 4:3
280 // Set this again to the same as the tv screen size
281 if (!setAspectRatio(tvsize)) { shutdown(); return 0; }
283 // mode == LETTERBOX is invalid if the TV is widescreen
284 if (tvsize == ASPECT16X9) setMode(NORMAL);
289 int VideoOMX::setDefaultAspect()
291 return setAspectRatio(tvsize);
296 int VideoOMX::setFormat(UCHAR tformat)
298 if (!initted) return 0;
299 if ((tformat != PAL) && (tformat != NTSC)) return 0;
302 // if (ioctl(fdVideo, AV_SET_VID_DISP_FMT, format) != 0) return 0;
318 int VideoOMX::setConnection(UCHAR tconnection)
320 if (!initted) return 0;
321 if ((tconnection != COMPOSITERGB) && (tconnection != SVIDEO)) return 0;
322 connection = tconnection;
324 // if (ioctl(fdVideo, AV_SET_VID_OUTPUT, connection) != 0) return 0;
328 int VideoOMX::setAspectRatio(UCHAR taspectRatio)
330 if (!initted) return 0;
331 if ((taspectRatio != ASPECT4X3) && (taspectRatio != ASPECT16X9)) return 0;
332 aspectRatio = taspectRatio;
334 Log::getInstance()->log("Video", Log::DEBUG, "Setting aspect to %i", aspectRatio);
336 // if (ioctl(fdVideo, AV_SET_VID_RATIO, aspectRatio) != 0) return 0;
340 int VideoOMX::setMode(UCHAR tmode)
342 if (!initted) return 0;
349 void VideoOMX::updateMode()
354 OMX_CONFIG_DISPLAYREGIONTYPE dispconf;
355 memset(&dispconf, 0, sizeof(dispconf));
356 dispconf.nSize = sizeof(dispconf);
357 dispconf.nVersion.nVersion = OMX_VERSION;
358 dispconf.nPortIndex = omx_rend_input_port;
360 dispconf.set = OMX_DISPLAY_SET_LAYER;
361 error = OMX_SetParameter(omx_vid_rend, OMX_IndexConfigDisplayRegion,
363 if (error != OMX_ErrorNone) {
364 Log::getInstance()->log("Video", Log::DEBUG,
365 "Set OMX_IndexConfigDisplayRegion1 failed %x", error);
366 clock_mutex.Unlock();
369 dispconf.set = OMX_DISPLAY_SET_FULLSCREEN;
370 if (mode != QUARTER && mode != EIGHTH) {
372 dispconf.fullscreen = OMX_TRUE;
374 dispconf.fullscreen = OMX_FALSE;
376 error = OMX_SetParameter(omx_vid_rend, OMX_IndexConfigDisplayRegion,
378 if (error != OMX_ErrorNone) {
379 Log::getInstance()->log("Video", Log::DEBUG,
380 "Set OMX_IndexConfigDisplayRegion2 failed %x", error);
381 clock_mutex.Unlock();
385 dispconf.set = OMX_DISPLAY_SET_MODE;
386 if (mode != QUARTER && mode != EIGHTH) {
387 dispconf.mode = (mode == NORMAL) ? OMX_DISPLAY_MODE_FILL
388 : OMX_DISPLAY_MODE_LETTERBOX;
390 dispconf.mode = OMX_DISPLAY_MODE_LETTERBOX;
392 error = OMX_SetParameter(omx_vid_rend, OMX_IndexConfigDisplayRegion,
394 if (error != OMX_ErrorNone) {
395 Log::getInstance()->log("Video", Log::DEBUG,
396 "Set OMX_IndexConfigDisplayRegion3 failed %x", error);
397 clock_mutex.Unlock();
401 if (mode == QUARTER || mode == EIGHTH) {
402 unsigned int display_width, display_height;
403 display_width = display_height = 0;
404 if (graphics_get_display_size(0, &display_width, &display_height)
406 Log::getInstance()->log("OSD", Log::WARN,
407 "Getting display size failed! (BCM API) ");
408 clock_mutex.Unlock();
411 //UnSetFullscreen with window
412 dispconf.set = OMX_DISPLAY_SET_DEST_RECT;
413 dispconf.dest_rect.x_offset
414 = (int) (xpos * ((float) display_width));
415 dispconf.dest_rect.y_offset = (int) (ypos
416 * ((float) display_height));
417 if (mode == QUARTER) {
418 dispconf.dest_rect.width = display_width >> 1;
419 dispconf.dest_rect.height = display_height >> 1;
420 } else if (mode == EIGHTH) {
421 dispconf.dest_rect.width = display_width >> 2;
422 dispconf.dest_rect.height = display_height >> 2;
424 error = OMX_SetParameter(omx_vid_rend,
425 OMX_IndexConfigDisplayRegion, &dispconf);
426 if (error != OMX_ErrorNone) {
427 Log::getInstance()->log("Video", Log::DEBUG,
428 "Set OMX_IndexConfigDisplayRegion failed %x", error);
429 clock_mutex.Unlock();
435 clock_mutex.Unlock();
438 int VideoOMX::signalOff()
440 // if (ioctl(fdVideo, AV_SET_VID_DENC, 0) != 0) return 0;
444 int VideoOMX::signalOn()
446 // if (ioctl(fdVideo, AV_SET_VID_DENC, 1) != 0) return 0;
450 int VideoOMX::setSource()
452 if (!initted) return 0;
454 // What does this do...
455 // if (ioctl(fdVideo, AV_SET_VID_SRC, 1) != 0) return 0;
459 int VideoOMX::setPosition(int x, int y) {
462 xpos = ((float) x*2.f) / ((float) screenWidth);
463 ypos = ((float) y*2.f) / ((float) screenHeight);
471 if (!initted) return 0;
473 // if (ioctl(fdVideo, AV_SET_VID_SYNC, 2) != 0) return 0;
478 int VideoOMX::play() {
482 Log::getInstance()->log("Video", Log::DEBUG, "enter play");
484 if (AllocateCodecsOMX()) {
486 // Otherwise fall back to libav
490 Log::getInstance()->log("Video", Log::NOTICE,
491 "Allocate Codecs OMX failed assume h264 unsupported");
494 Log::getInstance()->log("Video", Log::NOTICE,
495 "Allocate Codecs OMX failed assume mpeg2 unsupported");
504 int VideoOMX::initClock()
508 if (clock_references==0)
511 static OMX_CALLBACKTYPE callbacks= {&EventHandler_OMX,&EmptyBufferDone_OMX,&FillBufferDone_OMX};
514 error=OMX_GetHandle(&omx_clock,VPE_OMX_CLOCK,NULL,&callbacks);
516 if (error!=OMX_ErrorNone) {
517 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX clock failed %x", error);
518 clock_mutex.Unlock();
519 DeAllocateCodecsOMX();
523 /* TODO Clock config to separate method */
524 OMX_PORT_PARAM_TYPE p_param;
525 memset(&p_param,0,sizeof(p_param));
526 p_param.nSize=sizeof(p_param);
527 p_param.nVersion.nVersion=OMX_VERSION;
528 error=OMX_GetParameter(omx_clock,OMX_IndexParamOtherInit,&p_param);
529 if (error!=OMX_ErrorNone) {
530 Log::getInstance()->log("Video", Log::DEBUG, "Init clock OMX_GetParameter failed %x", error);
531 clock_mutex.Unlock();
532 DeAllocateCodecsOMX();
535 omx_clock_output_port=p_param.nStartPortNumber;
537 for (unsigned int i=0;i<p_param.nPorts;i++) {
538 if (!DisablePort(omx_clock,p_param.nStartPortNumber+i,true) ) {
539 Log::getInstance()->log("Video", Log::DEBUG, "Disable Ports OMX clock failed %d",i);
540 clock_mutex.Unlock();
541 DeAllocateCodecsOMX();
550 Log::getInstance()->log("Video", Log::DEBUG, "init omx clock %x %x",this,omx_clock);
552 clock_mutex.Unlock();
556 int VideoOMX::getClockAudioandInit(OMX_HANDLETYPE *p_omx_clock,OMX_U32 *p_omx_clock_output_port)
560 *p_omx_clock_output_port=0;
567 OMX_TIME_CONFIG_ACTIVEREFCLOCKTYPE refclock;
568 memset(&refclock,0,sizeof(refclock));
569 refclock.nSize=sizeof(refclock);
570 refclock.nVersion.nVersion=OMX_VERSION;
572 refclock.eClock=OMX_TIME_RefClockAudio;
574 //refclock.eClock=OMX_TIME_RefClockVideo;
575 error=OMX_SetConfig(omx_clock,OMX_IndexConfigTimeActiveRefClock,&refclock);
576 if (error!=OMX_ErrorNone){
577 Log::getInstance()->log("Video", Log::DEBUG, "Clock OMX_IndexConfigTimeActiveRefClock failed %x", error);
578 clock_mutex.Unlock();
579 DeAllocateCodecsOMX();
583 OMX_PORT_PARAM_TYPE p_param;
584 memset(&p_param,0,sizeof(p_param));
585 p_param.nSize=sizeof(p_param);
586 p_param.nVersion.nVersion=OMX_VERSION;
587 error=OMX_GetParameter(omx_clock,OMX_IndexParamOtherInit,&p_param);
588 if (error!=OMX_ErrorNone){
589 Log::getInstance()->log("Video", Log::DEBUG, "Init clock OMX_GetParameter failed %x", error);
590 clock_mutex.Unlock();
591 DeAllocateCodecsOMX();
595 OMX_TIME_CONFIG_CLOCKSTATETYPE clock_conf;
596 memset(&clock_conf,0,sizeof(clock_conf));
597 clock_conf.nSize=sizeof(clock_conf);
598 clock_conf.nVersion.nVersion=OMX_VERSION;
599 clock_conf.eState=OMX_TIME_ClockStateWaitingForStartTime;
600 clock_conf.nStartTime=0;
601 clock_conf.nOffset=0;
602 if (clock_references==1) clock_conf.nWaitMask=OMX_CLOCKPORT1;
603 else clock_conf.nWaitMask=OMX_CLOCKPORT0|OMX_CLOCKPORT1;
604 error=OMX_SetConfig(omx_clock,OMX_IndexConfigTimeClockState,&clock_conf);
605 if (error!=OMX_ErrorNone) {
606 Log::getInstance()->log("Video", Log::DEBUG, "AuI Clock IndexConfigTimeClockState failed %x", error);
610 *p_omx_clock_output_port=p_param.nStartPortNumber+1;
611 *p_omx_clock=omx_clock;
612 clock_mutex.Unlock();
616 int VideoOMX::getClockVideoandInit()
625 if (clock_references==1) { // only if no audio is attached to this clock!
626 OMX_TIME_CONFIG_ACTIVEREFCLOCKTYPE refclock;
627 memset(&refclock,0,sizeof(refclock));
628 refclock.nSize=sizeof(refclock);
629 refclock.nVersion.nVersion=OMX_VERSION;
631 //refclock.eClock=OMX_TIME_RefClockAudio;
633 refclock.eClock=OMX_TIME_RefClockVideo;
634 error=OMX_SetConfig(omx_clock,OMX_IndexConfigTimeActiveRefClock,&refclock);
635 if (error!=OMX_ErrorNone) {
636 Log::getInstance()->log("Video", Log::DEBUG, "Clock OMX_IndexConfigTimeActiveRefClock failed %x", error);
637 clock_mutex.Unlock();
638 DeAllocateCodecsOMX();
643 OMX_PORT_PARAM_TYPE p_param;
644 memset(&p_param,0,sizeof(p_param));
645 p_param.nSize=sizeof(p_param);
646 p_param.nVersion.nVersion=OMX_VERSION;
647 error=OMX_GetParameter(omx_clock,OMX_IndexParamOtherInit,&p_param);
648 if (error!=OMX_ErrorNone){
649 Log::getInstance()->log("Video", Log::DEBUG, "Init clock OMX_GetParameter failed %x", error);
650 clock_mutex.Unlock();
651 DeAllocateCodecsOMX();
656 OMX_TIME_CONFIG_CLOCKSTATETYPE clock_conf;
657 memset(&clock_conf,0,sizeof(clock_conf));
658 clock_conf.nSize=sizeof(clock_conf);
659 clock_conf.nVersion.nVersion=OMX_VERSION;
660 clock_conf.eState=OMX_TIME_ClockStateWaitingForStartTime;
661 clock_conf.nStartTime=0;
662 clock_conf.nOffset=0;
663 if (clock_references==1) clock_conf.nWaitMask=OMX_CLOCKPORT0;
664 else clock_conf.nWaitMask=OMX_CLOCKPORT0|OMX_CLOCKPORT1;
665 error=OMX_SetConfig(omx_clock,OMX_IndexConfigTimeClockState,&clock_conf);
666 if (error!=OMX_ErrorNone) {
667 Log::getInstance()->log("Video", Log::DEBUG, "VuI Clock IndexConfigTimeClockState failed %x", error);
670 omx_clock_output_port=p_param.nStartPortNumber;
671 clock_mutex.Unlock();
676 void VideoOMX::clockUnpause()
680 if (clock_references>0) {
681 OMX_TIME_CONFIG_CLOCKSTATETYPE clock_conf;
682 memset(&clock_conf,0,sizeof(clock_conf));
683 clock_conf.nSize=sizeof(clock_conf);
684 clock_conf.nVersion.nVersion=OMX_VERSION;
685 clock_conf.eState=OMX_TIME_ClockStateRunning;
686 clock_conf.nStartTime=0;
687 clock_conf.nOffset=0;
688 clock_conf.nWaitMask=OMX_CLOCKPORT1;
689 error=OMX_SetConfig(omx_clock,OMX_IndexConfigTimeClockState,&clock_conf);
690 if (error!=OMX_ErrorNone) {
691 Log::getInstance()->log("Video", Log::DEBUG, "ClockUnpause IndexConfigTimeClockState failed %x", error);
694 clock_mutex.Unlock();
698 void VideoOMX::clockPause()
702 if (clock_references>0) {
703 OMX_TIME_CONFIG_CLOCKSTATETYPE clock_conf;
704 memset(&clock_conf,0,sizeof(clock_conf));
705 clock_conf.nSize=sizeof(clock_conf);
706 clock_conf.nVersion.nVersion=OMX_VERSION;
707 clock_conf.eState=OMX_TIME_ClockStateStopped;
708 clock_conf.nStartTime=0;
709 clock_conf.nOffset=0;
710 clock_conf.nWaitMask=OMX_CLOCKPORT1;
711 error=OMX_SetConfig(omx_clock,OMX_IndexConfigTimeClockState,&clock_conf);
712 if (error!=OMX_ErrorNone) {
713 Log::getInstance()->log("Video", Log::DEBUG, "ClockUnpause IndexConfigTimeClockState failed %x", error);
716 clock_mutex.Unlock();
721 int VideoOMX::AllocateCodecsOMX()
724 static OMX_CALLBACKTYPE callbacks= {&EventHandler_OMX,&EmptyBufferDone_OMX,&FillBufferDone_OMX};
726 Demuxer* demux=Demuxer::getInstance();
729 deint_first_frame=false;
731 Log::getInstance()->log("Video", Log::NOTICE, "Allocate Codecs OMX");
732 //Clock, move later to audio including events
734 if (deinterlace!=0 && demux->getHorizontalSize()<=720) { //only deinterlace SD material
736 deint_first_frame=true;
738 Log::getInstance()->log("Video", Log::NOTICE, "Deinterlacing activated %d",deinterlace);
743 if (!getClockVideoandInit()){
744 return 0;// get the clock and init it if necessary
749 Log::getInstance()->log("Video", Log::DEBUG, "idleClock failed");
757 error=OMX_GetHandle(&omx_vid_dec,VPE_OMX_H264_DECODER,NULL,&callbacks);
759 error=OMX_GetHandle(&omx_vid_dec,VPE_OMX_MPEG2_DECODER,NULL,&callbacks);
762 if (error!=OMX_ErrorNone){
763 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX video decoder failed %x", error);
764 clock_mutex.Unlock();
765 DeAllocateCodecsOMX();
770 Log::getInstance()->log("Video", Log::DEBUG, "Nmark3 ");
771 OMX_PORT_PARAM_TYPE p_param;
772 memset(&p_param,0,sizeof(p_param));
773 p_param.nSize=sizeof(p_param);
774 p_param.nVersion.nVersion=OMX_VERSION;
775 error=OMX_GetParameter(omx_vid_dec,OMX_IndexParamVideoInit,&p_param);
776 if (error!=OMX_ErrorNone){
777 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX h264 decoder OMX_GetParameter failed %x", error);
778 clock_mutex.Unlock();
779 DeAllocateCodecsOMX();
782 omx_codec_input_port=p_param.nStartPortNumber;
783 omx_codec_output_port=p_param.nStartPortNumber+1;
785 if (!DisablePort(omx_vid_dec,omx_codec_input_port) || !DisablePort(omx_vid_dec,omx_codec_output_port)) {
786 Log::getInstance()->log("Video", Log::DEBUG, "Disable Ports OMX video decoder failed");
787 clock_mutex.Unlock();
788 DeAllocateCodecsOMX();
792 Log::getInstance()->log("Video", Log::DEBUG, "Nmark4 ");
794 OMX_PARAM_BRCMVIDEODECODEERRORCONCEALMENTTYPE conceal;
795 memset(&conceal,0,sizeof(conceal));
796 conceal.nSize=sizeof(conceal);
797 conceal.nVersion.nVersion=OMX_VERSION;
798 conceal.bStartWithValidFrame=OMX_FALSE;
800 error=OMX_SetParameter(omx_vid_dec,OMX_IndexParamBrcmVideoDecodeErrorConcealment,&conceal);
801 if (error!=OMX_ErrorNone){
802 Log::getInstance()->log("Video", Log::DEBUG, "OMX_IndexParamBrcmVideoDecodeErrorConcealment failed %x", error);
803 clock_mutex.Unlock();
804 DeAllocateCodecsOMX();
809 error = OMX_GetHandle(&omx_vid_deint, VPE_OMX_VIDEO_DEINTERLACE, NULL,
811 if (error != OMX_ErrorNone) {
812 Log::getInstance()->log("Video", Log::DEBUG,
813 "Init OMX video deinterlacer failed %x", error);
814 clock_mutex.Unlock();
815 DeAllocateCodecsOMX();
819 error = OMX_GetParameter(omx_vid_deint, OMX_IndexParamImageInit,
821 if (error != OMX_ErrorNone) {
822 Log::getInstance()->log("Video", Log::DEBUG,
823 "Init OMX video deinterlacer OMX_GetParameter failed %x",
825 clock_mutex.Unlock();
826 DeAllocateCodecsOMX();
829 omx_deint_input_port = p_param.nStartPortNumber;
830 omx_deint_output_port = p_param.nStartPortNumber + 1;
832 if (!DisablePort(omx_vid_deint, omx_deint_input_port, true)
833 || !DisablePort(omx_vid_deint, omx_deint_output_port, true)) {
834 Log::getInstance()->log("Video", Log::DEBUG,
835 "Disable Ports OMX video deint failed");
836 clock_mutex.Unlock();
837 DeAllocateCodecsOMX();
844 error=OMX_GetHandle(&omx_vid_sched,VPE_OMX_VIDEO_SCHED,NULL,&callbacks);
845 if (error!=OMX_ErrorNone){
846 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX video scheduler failed %x", error);
847 clock_mutex.Unlock();
848 DeAllocateCodecsOMX();
854 error=OMX_GetParameter(omx_vid_sched,OMX_IndexParamVideoInit,&p_param);
855 if (error!=OMX_ErrorNone){
856 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX video scheduler OMX_GetParameter failed %x", error);
857 clock_mutex.Unlock();
858 DeAllocateCodecsOMX();
861 omx_shed_input_port=p_param.nStartPortNumber;
862 omx_shed_output_port=p_param.nStartPortNumber+1;
865 error=OMX_GetParameter(omx_vid_sched,OMX_IndexParamOtherInit,&p_param);
866 if (error!=OMX_ErrorNone){
867 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX video scheduler OMX_GetParameter failed %x", error);
868 clock_mutex.Unlock();
869 DeAllocateCodecsOMX();
872 omx_shed_clock_port=p_param.nStartPortNumber;
873 Log::getInstance()->log("Video", Log::DEBUG, "scheduler ports %d %d %d ",omx_shed_input_port,omx_shed_output_port,omx_shed_clock_port);
876 if (!DisablePort(omx_vid_sched,omx_shed_input_port,true) || !DisablePort(omx_vid_sched,omx_shed_output_port,true)
877 || !DisablePort(omx_vid_sched,omx_shed_clock_port,true)) {
878 Log::getInstance()->log("Video", Log::DEBUG, "Disable Ports OMX video shed failed");
879 clock_mutex.Unlock();
880 DeAllocateCodecsOMX();
885 error=OMX_GetHandle(&omx_vid_rend,VPE_OMX_VIDEO_REND,NULL,&callbacks);
886 if (error!=OMX_ErrorNone){
887 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX video rend failed %x", error);
888 clock_mutex.Unlock();
889 DeAllocateCodecsOMX();
893 error=OMX_GetParameter(omx_vid_rend,OMX_IndexParamVideoInit,&p_param);
894 if (error!=OMX_ErrorNone){
895 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX video rend OMX_GetParameter failed %x", error);
896 clock_mutex.Unlock();
897 DeAllocateCodecsOMX();
900 omx_rend_input_port=p_param.nStartPortNumber;
901 //omx_rend_output_port=p_param.nStartPortNumber+1;
904 if (!DisablePort(omx_vid_rend,omx_rend_input_port,true) /*|| !DisablePort(omx_vid_rend,omx_rend_output_port)*/
906 Log::getInstance()->log("Video", Log::DEBUG, "Disable Ports OMX video rend failed");
907 clock_mutex.Unlock();
908 DeAllocateCodecsOMX();
916 error=OMX_SetupTunnel(omx_clock,omx_clock_output_port,omx_vid_sched,omx_shed_clock_port);
917 if (error!=OMX_ErrorNone){
918 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);
919 clock_mutex.Unlock();
920 DeAllocateCodecsOMX();
924 if (!EnablePort(omx_clock,omx_clock_output_port,false) || !EnablePort(omx_vid_sched,omx_shed_clock_port,false)
926 Log::getInstance()->log("Video", Log::DEBUG, "Enable Ports OMX clock shed failed");
927 clock_mutex.Unlock();
928 DeAllocateCodecsOMX();
933 Log::getInstance()->log("Video", Log::DEBUG, "mark2 ");
934 if (!ChangeComponentState(omx_vid_sched,OMX_StateIdle)) {
935 Log::getInstance()->log("Video", Log::DEBUG, "vid_sched idle ChangeComponentState");
936 clock_mutex.Unlock();
937 DeAllocateCodecsOMX();
943 Log::getInstance()->log("Video", Log::DEBUG, "mark1 ");
944 if ( !CommandFinished(omx_vid_sched,OMX_CommandPortEnable,omx_shed_clock_port)) {
945 clock_mutex.Unlock();
946 DeAllocateCodecsOMX();
953 Log::getInstance()->log("Video", Log::DEBUG, "mark1 special ");
954 if ( !CommandFinished(omx_clock,OMX_CommandPortEnable,omx_clock_output_port)) {
955 clock_mutex.Unlock();
956 DeAllocateCodecsOMX();
962 Log::getInstance()->log("Video", Log::DEBUG, "mark1b ");
965 /* error=OMX_SendCommand(omx_vid_dec,OMX_CommandStateSet,OMX_StateIdle,0);
966 if (error!=OMX_ErrorNone){
967 Log::getInstance()->log("Video", Log::DEBUG, "vid_dec Send Command to OMX State Idle %x", error);
971 OMX_VIDEO_PARAM_PORTFORMATTYPE ft_type;
972 memset(&ft_type,0,sizeof(ft_type));
973 ft_type.nSize=sizeof(ft_type);
974 ft_type.nVersion.nVersion=OMX_VERSION;
976 ft_type.nPortIndex=omx_codec_input_port;
978 ft_type.eCompressionFormat=OMX_VIDEO_CodingAVC;
980 ft_type.eCompressionFormat=OMX_VIDEO_CodingMPEG2;
985 ft_type.xFramerate=0;//25*(1<<16);//demux->getFrameRate()*(1<<16);
986 Log::getInstance()->log("Video", Log::DEBUG, "Framerate: %d",demux->getFrameRate());
987 error=OMX_SetParameter(omx_vid_dec,OMX_IndexParamVideoPortFormat,&ft_type);
988 if (error!=OMX_ErrorNone){
989 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX_IndexParamVideoPortFormat failed %x", error);
990 clock_mutex.Unlock();
991 DeAllocateCodecsOMX();
996 if (!ChangeComponentState(omx_vid_dec,OMX_StateIdle)) {
997 Log::getInstance()->log("Video", Log::DEBUG, "vid_dec ChangeComponentState");
998 clock_mutex.Unlock();
999 DeAllocateCodecsOMX();
1004 if (!PrepareInputBufsOMX()) {
1005 clock_mutex.Unlock();
1006 DeAllocateCodecsOMX();
1011 error=OMX_SetupTunnel(omx_vid_dec,omx_codec_output_port,omx_vid_sched,omx_shed_input_port);
1012 if (error!=OMX_ErrorNone){
1013 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel dec to sched failed %x", error);
1014 clock_mutex.Unlock();
1015 DeAllocateCodecsOMX();
1021 if (!EnablePort(omx_vid_dec,omx_codec_output_port,false) || !EnablePort(omx_vid_sched,omx_shed_input_port,false)
1023 Log::getInstance()->log("Video", Log::DEBUG, "Enable Ports OMX codec shed failed");
1024 clock_mutex.Unlock();
1025 DeAllocateCodecsOMX();
1029 if ( !CommandFinished(omx_vid_dec,OMX_CommandPortEnable,omx_codec_output_port) || !CommandFinished(omx_vid_sched,OMX_CommandPortEnable,omx_shed_input_port)) {
1030 clock_mutex.Unlock();
1031 DeAllocateCodecsOMX();
1037 error=OMX_SetupTunnel(omx_vid_dec,omx_codec_output_port,omx_vid_deint,omx_deint_input_port);
1038 if (error!=OMX_ErrorNone){
1039 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel dec to deint failed %x", error);
1040 clock_mutex.Unlock();
1041 DeAllocateCodecsOMX();
1047 if (!EnablePort(omx_vid_dec,omx_codec_output_port,false) || !EnablePort(omx_vid_deint,omx_deint_input_port,false)
1049 Log::getInstance()->log("Video", Log::DEBUG, "Enable Ports OMX codec deint failed");
1050 clock_mutex.Unlock();
1051 DeAllocateCodecsOMX();
1055 if ( !CommandFinished(omx_vid_dec,OMX_CommandPortEnable,omx_codec_output_port) || !CommandFinished(omx_vid_deint,OMX_CommandPortEnable,omx_deint_input_port)) {
1056 clock_mutex.Unlock();
1057 DeAllocateCodecsOMX();
1061 if (!ChangeComponentState(omx_vid_deint,OMX_StateIdle)) {
1062 Log::getInstance()->log("Video", Log::DEBUG, "vid_deint ChangeComponentState");
1063 clock_mutex.Unlock();
1064 DeAllocateCodecsOMX();
1068 OMX_CONFIG_IMAGEFILTERPARAMSTYPE imagefilter;
1069 memset(&imagefilter,0,sizeof(imagefilter));
1070 imagefilter.nSize=sizeof(imagefilter);
1071 imagefilter.nVersion.nVersion=OMX_VERSION;
1073 imagefilter.nPortIndex=omx_deint_output_port;
1074 imagefilter.nNumParams=1;
1075 imagefilter.nParams[0]=3;//???
1076 switch (deinterlace) {
1078 imagefilter.eImageFilter=OMX_ImageFilterDeInterlaceLineDouble; break;
1080 imagefilter.eImageFilter=OMX_ImageFilterDeInterlaceAdvanced; break;
1082 imagefilter.eImageFilter=OMX_ImageFilterFilm; break;
1086 error=OMX_SetConfig(omx_vid_deint,OMX_IndexConfigCommonImageFilterParameters,&imagefilter);
1087 if (error!=OMX_ErrorNone){
1088 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX_IndexConfigCommonImageFilterParameters failed %x", error);
1089 clock_mutex.Unlock();
1090 DeAllocateCodecsOMX();
1095 error=OMX_SetupTunnel(omx_vid_deint,omx_deint_output_port,omx_vid_sched,omx_shed_input_port);
1096 if (error!=OMX_ErrorNone){
1097 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel deint to sched failed %x", error);
1098 clock_mutex.Unlock();
1099 DeAllocateCodecsOMX();
1103 if (!EnablePort(omx_vid_deint,omx_deint_output_port,false) || !EnablePort(omx_vid_sched,omx_shed_input_port,false)
1105 Log::getInstance()->log("Video", Log::DEBUG, "Enable Ports OMX deint shed failed");
1106 clock_mutex.Unlock();
1107 DeAllocateCodecsOMX();
1111 if ( !CommandFinished(omx_vid_deint,OMX_CommandPortEnable,omx_deint_output_port) || !CommandFinished(omx_vid_sched,OMX_CommandPortEnable,omx_shed_input_port)) {
1112 clock_mutex.Unlock();
1113 DeAllocateCodecsOMX();
1119 if (!ChangeComponentState(omx_vid_dec,OMX_StateExecuting)) {
1120 Log::getInstance()->log("Video", Log::DEBUG, "omx_vid_dec ChangeComponentState Execute");
1121 clock_mutex.Unlock();
1122 DeAllocateCodecsOMX();
1126 error=OMX_SetupTunnel(omx_vid_sched,omx_shed_output_port,omx_vid_rend,omx_rend_input_port);
1127 if (error!=OMX_ErrorNone){
1128 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel sched to rend failed %x", error);
1129 clock_mutex.Unlock();
1130 DeAllocateCodecsOMX();
1134 if (!EnablePort(omx_vid_sched,omx_shed_output_port,false) || !EnablePort(omx_vid_rend,omx_rend_input_port,false)
1136 Log::getInstance()->log("Video", Log::DEBUG, "Enable Ports OMX shed rend failed");
1137 clock_mutex.Unlock();
1138 DeAllocateCodecsOMX();
1142 if (!CommandFinished(omx_vid_sched,OMX_CommandPortEnable,omx_shed_output_port)
1143 || !CommandFinished(omx_vid_rend,OMX_CommandPortEnable,omx_rend_input_port)) {
1144 clock_mutex.Unlock();
1145 DeAllocateCodecsOMX();
1149 if (!ChangeComponentState(omx_vid_rend,OMX_StateIdle)) {
1150 Log::getInstance()->log("Video", Log::DEBUG, "vid_rend ChangeComponentState");
1151 clock_mutex.Unlock();
1152 DeAllocateCodecsOMX();
1157 if (!ChangeComponentState(omx_vid_deint,OMX_StateExecuting)) {
1158 Log::getInstance()->log("Video", Log::DEBUG, "vid_vid_deint ChangeComponentState");
1159 clock_mutex.Unlock();
1160 DeAllocateCodecsOMX();
1163 DisablePort(omx_vid_deint,omx_deint_output_port,false);
1164 DisablePort(omx_vid_deint,omx_deint_input_port,false);
1167 if (!ChangeComponentState(omx_vid_sched,OMX_StateExecuting)) {
1168 Log::getInstance()->log("Video", Log::DEBUG, "omx_vid_sched ChangeComponentState Execute");
1169 clock_mutex.Unlock();
1170 DeAllocateCodecsOMX();
1174 if (!ChangeComponentState(omx_vid_rend,OMX_StateExecuting)) {
1175 Log::getInstance()->log("Video", Log::DEBUG, "omx_vid_rend ChangeComponentState Execute");
1176 clock_mutex.Unlock();
1177 DeAllocateCodecsOMX();
1182 /*OMX_CONFIG_DISPLAYREGIONTYPE dispconf;
1183 memset(&dispconf,0,sizeof(dispconf));
1184 dispconf.nSize=sizeof(dispconf);
1185 dispconf.nVersion.nVersion=OMX_VERSION;
1187 dispconf.nPortIndex=omx_rend_input_port;
1189 dispconf.set=OMX_DISPLAY_SET_LAYER ;
1191 error=OMX_SetConfig(omx_vid_rend,OMX_IndexConfigDisplayRegion,&dispconf);
1192 if (error!=OMX_ErrorNone){
1193 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX_IndexConfigDisplayRegion failed %x", error);
1194 clock_mutex.Unlock();
1195 DeAllocateCodecsOMX();
1199 /* dispconf.set=OMX_DISPLAY_SET_FULLSCREEN ;
1200 dispconf.fullscreen=OMX_FALSE;
1201 error=OMX_SetParameter(omx_vid_rend,OMX_IndexConfigDisplayRegion,&dispconf);
1202 if (error!=OMX_ErrorNone){
1203 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX_IndexConfigDisplayRegion failed %x", error);
1204 clock_mutex.Unlock();
1205 DeAllocateCodecsOMX();
1209 dispconf.set=OMX_DISPLAY_SET_DEST_RECT;
1210 dispconf.dest_rect.x_offset=100;
1211 dispconf.dest_rect.y_offset=100;
1212 dispconf.dest_rect.width=640;
1213 dispconf.dest_rect.height=480;
1214 error=OMX_SetParameter(omx_vid_rend,OMX_IndexConfigDisplayRegion,&dispconf);
1215 if (error!=OMX_ErrorNone){
1216 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX_IndexConfigDisplayRegion failed %x", error);
1217 clock_mutex.Unlock();
1218 DeAllocateCodecsOMX();
1223 playbacktimeoffset=-GetCurrentSystemTime();
1227 clock_mutex.Unlock();
1231 setClockExecutingandRunning();
1240 int VideoOMX::idleClock()
1242 OMX_ERRORTYPE error;
1243 OMX_STATETYPE temp_state;
1245 OMX_GetState(omx_clock,&temp_state);
1247 if (temp_state!=OMX_StateIdle) {
1248 if (!ChangeComponentState(omx_clock,OMX_StateIdle)) {
1249 Log::getInstance()->log("Video", Log::DEBUG, "omx_clock ChangeComponentState Idle failed");
1250 clock_mutex.Unlock();
1254 clock_mutex.Unlock();
1258 int VideoOMX::setClockExecutingandRunning()
1260 OMX_ERRORTYPE error;
1261 OMX_STATETYPE temp_state;
1263 OMX_GetState(omx_clock,&temp_state);
1265 if (temp_state!=OMX_StateExecuting) {
1266 if (!ChangeComponentState(omx_clock,OMX_StateExecuting)) {
1267 Log::getInstance()->log("Video", Log::DEBUG, "omx_clock ChangeComponentState Execute failed");
1268 clock_mutex.Unlock();
1269 DeAllocateCodecsOMX();
1274 OMX_TIME_CONFIG_CLOCKSTATETYPE clock_conf;
1275 memset(&clock_conf,0,sizeof(clock_conf));
1276 clock_conf.nSize=sizeof(clock_conf);
1277 clock_conf.nVersion.nVersion=OMX_VERSION;
1278 clock_conf.eState=OMX_TIME_ClockStateRunning;
1279 error=OMX_SetConfig(omx_clock,OMX_IndexConfigTimeClockState,&clock_conf);
1280 if (error!=OMX_ErrorNone) {
1281 Log::getInstance()->log("Video", Log::DEBUG, "Clock IndexConfigTimeClockState failed %x", error);
1282 clock_mutex.Unlock();
1283 DeAllocateCodecsOMX();
1286 clock_mutex.Unlock();
1292 int VideoOMX::ChangeComponentState(OMX_HANDLETYPE handle,OMX_STATETYPE type) //needs to be called with locked mutex
1294 OMX_ERRORTYPE error;
1295 error=OMX_SendCommand(handle,OMX_CommandStateSet,type,0);
1296 if (error!=OMX_ErrorNone){
1297 Log::getInstance()->log("Video", Log::DEBUG, "handle %x Send Command to OMX State %x %x",handle,type, error);
1301 if (!CommandFinished(handle,OMX_CommandStateSet,type)) {
1309 int VideoOMX::EnablePort(OMX_HANDLETYPE handle,OMX_U32 port,bool wait) //needs to be called with locked mutex
1311 OMX_ERRORTYPE error;
1312 error=OMX_SendCommand(handle,OMX_CommandPortEnable,port,0);
1313 if (error!=OMX_ErrorNone){
1314 Log::getInstance()->log("Video", Log::DEBUG, "handle %x Send Command to enable port %x %x",handle,port, error);
1318 if (!wait) return 1;
1319 if (!CommandFinished(handle,OMX_CommandPortEnable,port)) {
1327 int VideoOMX::DisablePort(OMX_HANDLETYPE handle,OMX_U32 port,bool wait) //needs to be called with locked mutex
1329 OMX_ERRORTYPE error;
1330 error=OMX_SendCommand(handle,OMX_CommandPortDisable,port,0);
1331 if (error!=OMX_ErrorNone){
1332 Log::getInstance()->log("Video", Log::DEBUG, "handle %x Send Command to disable port %x %x",handle,port, error);
1336 if (!wait) return 1;
1337 if (!CommandFinished(handle,OMX_CommandPortDisable,port)) {
1345 int VideoOMX::WaitForEvent(OMX_HANDLETYPE handle,OMX_U32 event) //needs to be called with locked mutex
1349 omx_event_mutex.Lock();
1350 list<VPE_OMX_EVENT>::iterator itty=omx_events.begin();
1351 while (itty!=omx_events.end()) {
1353 VPE_OMX_EVENT current=*itty;
1354 if (current.handle==handle) { //this is ours
1355 if (current.event_type==OMX_EventError) {
1356 omx_events.erase(itty);
1357 omx_event_mutex.Unlock();
1358 Log::getInstance()->log("Video", Log::DEBUG, "WaitForEvent Finished on Error");
1361 } else if (current.event_type==event) {
1362 omx_events.erase(itty);
1363 omx_event_mutex.Unlock();
1364 Log::getInstance()->log("Video", Log::DEBUG, "WaitForEvent Finished Completed");
1371 omx_event_mutex.Unlock();
1376 Log::getInstance()->log("Video", Log::DEBUG, "WaitForEvent waited too long %x %x",handle,event);
1382 int VideoOMX::CommandFinished(OMX_HANDLETYPE handle,OMX_U32 command,OMX_U32 data2) //needs to be called with locked mutex
1386 omx_event_mutex.Lock();
1387 list<VPE_OMX_EVENT>::iterator itty=omx_events.begin();
1388 while (itty!=omx_events.end()) {
1390 VPE_OMX_EVENT current=*itty;
1391 if (current.handle==handle) { //this is ours
1392 if (current.event_type==OMX_EventError) {
1393 omx_events.erase(itty);
1394 omx_event_mutex.Unlock();
1395 Log::getInstance()->log("Video", Log::DEBUG, "Command Finished on Error");
1398 } else if (current.event_type==OMX_EventCmdComplete && current.data1==command && current.data2==data2) {
1399 omx_events.erase(itty);
1400 omx_event_mutex.Unlock();
1401 //Log::getInstance()->log("Video", Log::DEBUG, "Command Finished Completed");
1408 omx_event_mutex.Unlock();
1413 Log::getInstance()->log("Video", Log::DEBUG, "CommandFinished waited too long %x %x %x",handle,command, data2);
1422 int VideoOMX::PrepareInputBufsOMX() //needs to be called with locked mutex
1424 OMX_ERRORTYPE error;
1425 OMX_PARAM_PORTDEFINITIONTYPE port_def_type;
1426 memset(&port_def_type,0,sizeof(port_def_type));
1427 port_def_type.nSize=sizeof(port_def_type);
1428 port_def_type.nVersion.nVersion=OMX_VERSION;
1429 port_def_type.nPortIndex=omx_codec_input_port;
1431 error=OMX_GetParameter(omx_vid_dec,OMX_IndexParamPortDefinition, &port_def_type);
1433 if (error!=OMX_ErrorNone){
1434 Log::getInstance()->log("Video", Log::DEBUG, "Get OMX OMX_IndexParamPortDefinition failed %x", error);
1436 /* Log::getInstance()->log("Video", Log::DEBUG, "Port para %d %d %d %d %d %d %d", port_def_type.nBufferCountActual,
1437 port_def_type.nBufferCountMin,port_def_type.nBufferSize,port_def_type.bEnabled,port_def_type.bPopulated,
1438 port_def_type.bBuffersContiguous,port_def_type.nBufferAlignment);*/
1440 port_def_type.nBufferCountActual=100;
1441 port_def_type.nBufferSize=max(port_def_type.nBufferSize,200000); // for transcoder important
1443 error=OMX_SetParameter(omx_vid_dec,OMX_IndexParamPortDefinition, &port_def_type);
1445 if (error!=OMX_ErrorNone){
1446 Log::getInstance()->log("Video", Log::DEBUG, "Set OMX OMX_IndexParamPortDefinition failed %x", error);
1450 error=OMX_SendCommand(omx_vid_dec,OMX_CommandPortEnable,omx_codec_input_port,0);
1451 if (error!=OMX_ErrorNone){
1452 Log::getInstance()->log("Video", Log::DEBUG, "Prepare Input bufs Send Command to enable port %x", error);
1456 input_bufs_omx_mutex.Lock();
1457 for (unsigned int i=0; i< port_def_type.nBufferCountActual;i++) {
1459 // unsigned char* new_buffer_data=(unsigned char*)malloc(port_def_type.nbufferSize);
1460 OMX_BUFFERHEADERTYPE *buf_head=NULL;
1461 /* error=OMX_Usebuffer(omx_vid_dec,&buf_head,omx_codec_input_port,NULL,port_def_type.nbufferSize,new_buffer_data);
1462 if (error!=OMX_ErrorNone){
1463 Log::getInstance()->log("Video", Log::DEBUG, "Use OMX_Usebuffer failed %x", error);
1464 input_bufs_omx_mutex.Unlock();
1467 error=OMX_AllocateBuffer(omx_vid_dec,&buf_head,omx_codec_input_port,NULL,port_def_type.nBufferSize);
1468 if (error!=OMX_ErrorNone){
1469 Log::getInstance()->log("Video", Log::DEBUG, "Use OMX_AllocateBuffer failed %x", error);
1470 input_bufs_omx_mutex.Unlock();
1473 input_bufs_omx_all.push_back(buf_head);
1474 input_bufs_omx_free.push_back(buf_head);
1476 omx_first_frame=true;
1479 cur_input_buf_omx=NULL;
1480 input_bufs_omx_mutex.Unlock();
1483 Log::getInstance()->log("Video", Log::DEBUG, "PrepareInputBufsOMX mark3");
1484 if (!CommandFinished(omx_vid_dec,OMX_CommandPortEnable,omx_codec_input_port)) {
1487 Log::getInstance()->log("Video", Log::DEBUG, "PrepareInputBufsOMX mark4");
1492 int VideoOMX::DestroyInputBufsOMX() //need s to be called with locked mutex
1494 OMX_ERRORTYPE error;
1496 cur_input_buf_omx=NULL;
1497 input_bufs_omx_mutex.Lock();
1498 for (int i=0; i< input_bufs_omx_all.size();i++) {
1499 // free(input_bufs_omx_all[i]->pBuffer);
1500 // input_bufs_omx_all[i]->pBuffer=NULL;
1501 error=OMX_FreeBuffer(omx_vid_dec,omx_codec_input_port,input_bufs_omx_all[i]);
1502 if (error!=OMX_ErrorNone){
1503 Log::getInstance()->log("Video", Log::DEBUG, "Use OMX_FreeBuffer failed %x", error);
1504 input_bufs_omx_mutex.Unlock();
1509 input_bufs_omx_all.clear();
1510 input_bufs_omx_free.clear();
1511 input_bufs_omx_present.clear();
1512 input_time_present.clear();
1513 input_is_last.clear();
1514 input_bufs_omx_mutex.Unlock();
1521 int VideoOMX::DeAllocateCodecsOMX()
1523 OMX_ERRORTYPE error;
1525 Log::getInstance()->log("Video", Log::DEBUG, "enter deallocatecodecsomx");
1529 if (cur_input_buf_omx) {
1530 cur_input_buf_omx->nFlags|=OMX_BUFFERFLAG_EOS;
1531 OMX_ERRORTYPE error=ProtOMXEmptyThisBuffer(omx_vid_dec,cur_input_buf_omx);
1532 if (error!=OMX_ErrorNone) {
1533 Log::getInstance()->log("Video", Log::DEBUG, "OMX_EmptyThisBuffer failed %x", error);
1536 cur_input_buf_omx=NULL;//write out old data
1540 // first stop the omx elements
1541 if (!ChangeComponentState(omx_vid_dec,OMX_StateIdle)) {
1542 Log::getInstance()->log("Video", Log::DEBUG, "vid_dec ChangeComponentState");
1545 clock_mutex.Unlock();
1551 if (!ChangeComponentState(omx_vid_deint, OMX_StateIdle)) {
1552 Log::getInstance()->log("Video", Log::DEBUG,
1553 "vid_deint ChangeComponentState");
1559 if (!ChangeComponentState(omx_vid_sched,OMX_StateIdle)) {
1560 Log::getInstance()->log("Video", Log::DEBUG, "vid_shed ChangeComponentState");
1564 if (!ChangeComponentState(omx_vid_rend,OMX_StateIdle)) {
1565 Log::getInstance()->log("Video", Log::DEBUG, "vid_rend ChangeComponentState");
1571 // TODO proper deinit sequence
1572 // first flush all buffers
1576 error = OMX_SendCommand(omx_vid_dec, OMX_CommandFlush,
1577 omx_codec_output_port, NULL);
1578 if (error != OMX_ErrorNone) {
1579 Log::getInstance()->log("Video", Log::DEBUG,
1580 "OMX_Flush codec out failed %x", error);
1584 error = OMX_SendCommand(omx_vid_sched, OMX_CommandFlush,
1585 omx_shed_input_port, NULL);
1586 if (error != OMX_ErrorNone) {
1587 Log::getInstance()->log("Video", Log::DEBUG,
1588 "OMX_Flush shed in failed %x", error);
1592 if (!CommandFinished(omx_vid_dec, OMX_CommandFlush,
1593 omx_codec_output_port)) {
1594 Log::getInstance()->log("Video", Log::DEBUG,
1595 "flush cmd codec failed");
1598 if (!CommandFinished(omx_vid_sched, OMX_CommandFlush,
1599 omx_shed_input_port)) {
1600 Log::getInstance()->log("Video", Log::DEBUG,
1601 "flush cmd shed failed");
1604 error = OMX_SendCommand(omx_vid_dec, OMX_CommandFlush,
1605 omx_codec_output_port, NULL);
1606 if (error != OMX_ErrorNone) {
1607 Log::getInstance()->log("Video", Log::DEBUG,
1608 "OMX_Flush codec out failed %x", error);
1612 error = OMX_SendCommand(omx_vid_deint, OMX_CommandFlush,
1613 omx_deint_input_port, NULL);
1614 if (error != OMX_ErrorNone) {
1615 Log::getInstance()->log("Video", Log::DEBUG,
1616 "OMX_Flush deint in failed %x", error);
1620 if (!CommandFinished(omx_vid_dec, OMX_CommandFlush,
1621 omx_codec_output_port)) {
1622 Log::getInstance()->log("Video", Log::DEBUG,
1623 "flush cmd codec failed");
1626 if (!CommandFinished(omx_vid_deint, OMX_CommandFlush,
1627 omx_deint_input_port)) {
1628 Log::getInstance()->log("Video", Log::DEBUG,
1629 "flush cmd deint failed");
1633 error = OMX_SendCommand(omx_vid_deint, OMX_CommandFlush,
1634 omx_deint_output_port, NULL);
1635 if (error != OMX_ErrorNone) {
1636 Log::getInstance()->log("Video", Log::DEBUG,
1637 "OMX_Flush deint out failed %x", error);
1641 error = OMX_SendCommand(omx_vid_sched, OMX_CommandFlush,
1642 omx_shed_input_port, NULL);
1643 if (error != OMX_ErrorNone) {
1644 Log::getInstance()->log("Video", Log::DEBUG,
1645 "OMX_Flush shed in failed %x", error);
1649 if (!CommandFinished(omx_vid_deint, OMX_CommandFlush,
1650 omx_deint_output_port)) {
1651 Log::getInstance()->log("Video", Log::DEBUG,
1652 "flush cmd deint failed");
1655 if (!CommandFinished(omx_vid_sched, OMX_CommandFlush,
1656 omx_shed_input_port)) {
1657 Log::getInstance()->log("Video", Log::DEBUG,
1658 "flush cmd shed failed");
1668 error = OMX_SendCommand(omx_vid_rend, OMX_CommandFlush,
1669 omx_rend_input_port, NULL);
1670 if (error != OMX_ErrorNone) {
1671 Log::getInstance()->log("Video", Log::DEBUG,
1672 "OMX_Flush rend in failed %x", error);
1676 error = OMX_SendCommand(omx_vid_sched, OMX_CommandFlush,
1677 omx_shed_output_port, NULL);
1678 if (error != OMX_ErrorNone) {
1679 Log::getInstance()->log("Video", Log::DEBUG,
1680 "OMX_Flush shed out failed %x", error);
1686 if (!CommandFinished(omx_vid_rend, OMX_CommandFlush,
1687 omx_rend_input_port)) {
1688 Log::getInstance()->log("Video", Log::DEBUG,
1689 "flush cmd shed rend failed");
1692 if (!CommandFinished(omx_vid_sched, OMX_CommandFlush,
1693 omx_shed_output_port)) {
1694 Log::getInstance()->log("Video", Log::DEBUG,
1695 "flush cmd shed rend failed");
1702 error=OMX_SendCommand(omx_clock,OMX_CommandFlush, omx_clock_output_port, NULL);
1703 if (error!=OMX_ErrorNone){
1704 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Flush clock out failed %x", error);
1708 error=OMX_SendCommand(omx_vid_sched,OMX_CommandFlush, omx_shed_clock_port, NULL);
1709 if (error!=OMX_ErrorNone){
1710 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Flush shed clock failed %x", error);
1714 if (!CommandFinished(omx_clock,OMX_CommandFlush,omx_clock_output_port) ||
1715 !CommandFinished(omx_vid_sched,OMX_CommandFlush,omx_shed_clock_port)) {
1716 Log::getInstance()->log("Video", Log::DEBUG, "flush cmd clock shed failed");
1722 error = OMX_SendCommand(omx_vid_dec, OMX_CommandFlush,
1723 omx_codec_input_port, NULL);
1724 if (error != OMX_ErrorNone) {
1725 Log::getInstance()->log("Video", Log::DEBUG,
1726 "OMX_Flush codec out failed %x", error);
1733 DestroyInputBufsOMX();
1736 if (!DisablePort(omx_vid_sched,omx_shed_output_port,true)) {
1737 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 2 ");
1739 if (!DisablePort(omx_vid_rend,omx_rend_input_port,true)) {
1740 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 1");
1746 if (!DisablePort(omx_vid_dec,omx_codec_output_port,true)) {
1747 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 6");
1752 if (!DisablePort(omx_vid_dec,omx_codec_input_port,true)) {
1753 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 7");
1757 if (!DisablePort(omx_vid_deint,omx_deint_output_port,true)) {
1758 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 6a");
1763 if (!DisablePort(omx_vid_deint,omx_deint_input_port,true)) {
1764 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 7a");
1770 if (!DisablePort(omx_vid_sched,omx_shed_input_port,true)) {
1771 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 3");
1774 if (!DisablePort(omx_vid_sched,omx_shed_clock_port,true)) {
1775 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 4");
1778 if (!DisablePort(omx_clock,omx_clock_output_port,true)) {
1779 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 5");
1785 error=OMX_SetupTunnel(omx_vid_dec,omx_codec_output_port,NULL,NULL);
1786 if (error!=OMX_ErrorNone) {
1787 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1792 error=OMX_SetupTunnel(omx_vid_deint,omx_deint_input_port,NULL,NULL);
1793 if (error!=OMX_ErrorNone) {
1794 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1798 error=OMX_SetupTunnel(omx_vid_deint,omx_deint_output_port,NULL,NULL);
1799 if (error!=OMX_ErrorNone) {
1800 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1804 error=OMX_SetupTunnel(omx_vid_sched,omx_shed_input_port,NULL,NULL);
1805 if (error!=OMX_ErrorNone) {
1806 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1811 error=OMX_SetupTunnel(omx_vid_sched,omx_shed_output_port,NULL,NULL);
1812 if (error!=OMX_ErrorNone) {
1813 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1817 error=OMX_SetupTunnel(omx_vid_rend,omx_rend_input_port,NULL,NULL);
1818 if (error!=OMX_ErrorNone) {
1819 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1824 error=OMX_SetupTunnel(omx_clock,omx_clock_output_port,NULL,NULL);
1825 if (error!=OMX_ErrorNone) {
1826 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1830 error=OMX_SetupTunnel(omx_vid_sched,omx_shed_clock_port,NULL,NULL);
1831 if (error!=OMX_ErrorNone) {
1832 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1839 error=OMX_FreeHandle(omx_vid_dec);
1840 error=OMX_FreeHandle(omx_vid_sched);
1841 error=OMX_FreeHandle(omx_vid_rend);
1842 if (dodeint) error=OMX_FreeHandle(omx_vid_deint);
1844 clock_mutex.Unlock();
1846 if (error!=OMX_ErrorNone) {
1847 Log::getInstance()->log("Video", Log::DEBUG, "FreeHandle failed %d", error);
1849 } else clock_mutex.Unlock();
1850 Log::getInstance()->log("Video", Log::DEBUG, "leave deallocate codecs OMX");
1856 void VideoOMX::destroyClock()
1859 if (clock_references>0) {
1861 if (clock_references==0) {
1862 OMX_ERRORTYPE error;
1863 Log::getInstance()->log("Video", Log::DEBUG, "destroy omx clock");
1864 error=OMX_FreeHandle(omx_clock);
1865 if (error!=OMX_ErrorNone) {
1866 Log::getInstance()->log("Video", Log::DEBUG, "FreeHandle failed %d", error);
1871 clock_mutex.Unlock();
1875 int VideoOMX::stop()
1877 if (!initted) return 0;
1880 //Check if libav mode
1881 DeAllocateCodecsOMX();
1886 // if (ioctl(fdVideo, AV_SET_VID_STOP, 0) != 0) return 0;
1890 int VideoOMX::reset()
1892 if (!initted) return 0;
1895 DeAllocateCodecsOMX();
1896 // if (ioctl(fdVideo, AV_SET_VID_RESET, 0x11) != 0) return 0;
1900 int VideoOMX::pause()
1902 if (!initted) return 0;
1903 Log::getInstance()->log("Video", Log::DEBUG, "enter pause");
1906 //maybe also change omx clock?
1907 pausetimecode=GetCurrentSystemTime();
1913 int VideoOMX::unPause() // FIXME get rid - same as play!! Not here!
1915 if (!initted) return 0;
1916 Log::getInstance()->log("Video", Log::DEBUG, "enter unpause");
1919 playbacktimeoffset+=GetCurrentSystemTime()-pausetimecode;
1920 paused=false; // may be also change omx clock
1926 int VideoOMX::fastForward()
1928 if (!initted) return 0;
1930 // if (ioctl(fdVideo, AV_SET_VID_libavWD, 1) != 0) return 0;
1934 int VideoOMX::unFastForward()
1936 if (!initted) return 0;
1938 // if (ioctl(fdVideo, AV_SET_VID_RESET, 0x11) != 0) return 0; // don't need this.
1940 //// if (ioctl(fdVideo, AV_SET_VID_PLAY, 0) != 0) return 0;
1944 int VideoOMX::attachFrameBuffer()
1946 if (!initted) return 0;
1948 // if (ioctl(fdVideo, AV_SET_VID_FB, 0) != 0) return 0;
1952 int VideoOMX::blank(void)
1954 // if (ioctl(fdVideo, AV_SET_VID_FB, 1) != 0) return 0;
1955 // if (ioctl(fdVideo, AV_SET_VID_FB, 0) != 0) return 0;
1959 ULLONG VideoOMX::getCurrentTimestamp()
1961 if (iframemode) return 0;
1962 return lastreftimePTS;
1967 ULONG VideoOMX::timecodeToFrameNumber(ULLONG timecode)
1969 if (format == PAL) return (ULONG)(((double)timecode / (double)90000) * (double)25);
1970 else return (ULONG)(((double)timecode / (double)90000) * (double)30);
1975 int VideoOMX::test()
1980 // return ioctl(fdVideo, AV_SET_VID_STC, &stc);
1987 int VideoOMX::test2()
1995 long long VideoOMX::SetStartOffset(long long curreftime, bool *rsync)
1999 startoffset=curreftime;//offset is set for audio
2001 offsetvideonotset=false;
2003 if (offsetvideonotset) {
2004 offsetvideonotset=false;
2007 if ( (curreftime-lastrefvideotime)>10000000LL
2008 || (curreftime-lastrefvideotime)<-10000000LL) {//if pts jumps to big resync
2009 startoffset+=curreftime-lastrefvideotime;
2010 lastrefaudiotime+=curreftime-lastrefvideotime;
2012 offsetaudionotset=true;
2019 lastrefvideotime=curreftime;
2025 long long VideoOMX::SetStartAudioOffset(long long curreftime, bool *rsync)
2029 startoffset=curreftime;
2031 offsetaudionotset=false;
2033 if (offsetaudionotset) {
2034 offsetaudionotset=false;
2037 if ( (curreftime-lastrefaudiotime)>10000000LL
2038 || (curreftime-lastrefaudiotime)<-10000000LL) {//if pts jumps to big resync
2039 startoffset+=curreftime-lastrefaudiotime;
2040 lastrefvideotime+=curreftime-lastrefaudiotime;
2042 offsetvideonotset=true;
2048 lastrefaudiotime=curreftime;
2053 void VideoOMX::ResetTimeOffsets() {
2054 offsetnotset=true; //called from demuxer
2055 offsetvideonotset=true;
2056 offsetaudionotset=true;
2064 long long VideoOMX::GetCurrentSystemTime()
2067 clock_gettime(CLOCK_MONOTONIC, &ts);
2068 return ts.tv_sec*10000000LL+ts.tv_nsec/100LL;
2071 void VideoOMX::WaitUntil(long long time)
2073 struct timespec interval;
2074 interval.tv_sec=time/10000000LL;
2075 interval.tv_nsec=(time %10000000LL)*100LL;
2076 while (clock_nanosleep(CLOCK_MONOTONIC,TIMER_ABSTIME,&interval,NULL)==EINTR) {
2077 //Log::getInstance()->log("Video", Log::DEBUG, "Wait until multi");
2082 bool VideoOMX::FrameSkip(long long pts)
2084 //ok first calculate the absolute time
2086 long long target_time=pts-playbacktimeoffset;
2087 // we have to wait untile the next frame
2088 long long offset=Demuxer::getInstance()->getFrameRate();
2089 if (offset==0) offset=25;
2090 offset=-2*10000000LL/offset;
2091 target_time+=offset;
2092 long long current_time=GetCurrentSystemTime();
2094 if ((target_time-current_time)<-400000LL) {
2095 skip=true; // we are too slow
2097 /* Log::getInstance()->log("Video", Log::DEBUG,
2098 "Skipping frames1 %lld %lld %d",target_time-current_time,pts,Demuxer::getInstance()->getFrameRate());
2099 Log::getInstance()->log("Video", Log::DEBUG, "skip detail pts: %lld target: %lld sys: %lld off: %lld diff %lld",pts,target_time,current_time,offset,
2100 target_time-current_time);*/
2105 if ((target_time - current_time) < 0000LL) { //skip a bit more
2106 skip = true; // we are too slow
2108 /* Log::getInstance()->log("Video", Log::DEBUG,"Skipping frames2 %lld %lld %d",target_time-current_time,pts,Demuxer::getInstance()->getFrameRate());
2109 Log::getInstance()->log("Video", Log::DEBUG, "skip detail pts: %lld target: %lld sys: %lld off: %lld diff %lld",pts,target_time,current_time,offset,
2110 target_time-current_time);*/
2120 void VideoOMX::FrameWaitforDisplay(long long pts)
2122 //ok first calculate the absolute time
2123 long long target_time=pts-playbacktimeoffset;
2124 // we have to wait untile the next frame
2125 long long offset=Demuxer::getInstance()->getFrameRate();
2126 long long current_time=GetCurrentSystemTime();
2127 if (offset==0) offset=25;
2128 offset=-2*10000000LL/offset;
2129 target_time+=offset;
2130 if ((target_time-current_time)>1000000LL) target_time=current_time+1000000LL; // something is wrong do not wait too long
2131 //Log::getInstance()->log("Video", Log::DEBUG, "Wait for display pts: %lld target: %lld sys: %lld off: %lld diff %lld",pts,target_time,current_time,offset,
2132 // target_time-current_time);
2134 WaitUntil(target_time);
2135 //Log::getInstance()->log("Video", Log::DEBUG, "Wait for display out %lld",GetCurrentSystemTime());
2138 void VideoOMX::AdjustAudioPTS(long long pts)
2140 long long newplaybacktimeoffset=pts-GetCurrentSystemTime();
2141 /* if ((newplaybacktimeoffset-1000000LL)>playbacktimeoffset
2142 || (newplaybacktimeoffset+1000000LL)<playbacktimeoffset) {
2143 Log::getInstance()->log("Video", Log::DEBUG, "Adjust Playbackoffsettime o: %lld n: %lld",
2144 playbacktimeoffset,newplaybacktimeoffset);*/
2145 playbacktimeoffset=newplaybacktimeoffset;
2150 void VideoOMX::threadPostStopCleanup()
2154 Log::getInstance()->log("Video", Log::DEBUG,
2159 void VideoOMX::threadMethod()
2161 Log::getInstance()->log("Video", Log::DEBUG,
2165 OMX_BUFFERHEADERTYPE* pict=NULL;
2169 input_bufs_omx_mutex.Lock();
2170 if (input_bufs_omx_present.size()>0) {
2172 pict=input_bufs_omx_present.front();
2173 time=input_time_present.front();
2174 islast=input_is_last.front();
2175 input_bufs_omx_present.pop_front();
2176 input_time_present.pop_front();
2177 input_is_last.pop_front();
2179 input_bufs_omx_mutex.Unlock();
2183 //Log::getInstance()->log("Video", Log::DEBUG,
2185 if (time!=0 && FrameSkip(time) && !(pict->nFlags &OMX_BUFFERFLAG_STARTTIME)) {
2187 input_bufs_omx_mutex.Lock();
2188 input_bufs_omx_free.push_back(pict);
2189 input_bufs_omx_mutex.Unlock();
2192 OMX_ERRORTYPE error = ProtOMXEmptyThisBuffer(omx_vid_dec, pict);
2193 if (error != OMX_ErrorNone) {
2194 Log::getInstance()->log("Video", Log::DEBUG,
2195 "OMX_EmptyThisBuffer failed %x", error);
2197 if (deint_first_frame && dodeint) DeinterlaceFix();
2198 if (islast) FrameWaitforDisplay(time);
2206 Log::getInstance()->log("Video", Log::DEBUG,
2210 void VideoOMX::DeinterlaceFix()
2213 Demuxer* demux=Demuxer::getInstance();
2215 OMX_ERRORTYPE error;
2216 OMX_PARAM_PORTDEFINITIONTYPE port_def_type;
2217 memset(&port_def_type,0,sizeof(port_def_type));
2218 port_def_type.nSize=sizeof(port_def_type);
2219 port_def_type.nVersion.nVersion=OMX_VERSION;
2220 port_def_type.nPortIndex=omx_codec_output_port;
2222 error=OMX_GetParameter(omx_vid_dec,OMX_IndexParamPortDefinition, &port_def_type);
2223 if (error != OMX_ErrorNone) {
2224 Log::getInstance()->log("Video", Log::DEBUG,
2225 "OMX_IndexParamPortDefinition fix failed %x", error);
2226 clock_mutex.Unlock();
2230 if (port_def_type.format.video.nFrameWidth == demux->getHorizontalSize()
2231 && port_def_type.format.video.nFrameHeight == demux->getVerticalSize()){
2232 Log::getInstance()->log("Video", Log::DEBUG,
2233 "Deinit first frame fix");
2234 deint_first_frame=false;
2236 WaitForEvent(omx_vid_dec,OMX_EventPortSettingsChanged);
2237 DisablePort(omx_vid_dec,omx_codec_output_port,false);
2238 DisablePort(omx_vid_sched,omx_shed_input_port,false);
2239 DisablePort(omx_vid_deint,omx_deint_output_port,false);
2240 DisablePort(omx_vid_deint,omx_deint_input_port,false);
2242 port_def_type.nPortIndex=omx_deint_input_port;
2243 error = OMX_SetParameter(omx_vid_deint, OMX_IndexParamPortDefinition,
2245 if (error != OMX_ErrorNone) {
2246 Log::getInstance()->log("Video", Log::DEBUG,
2247 "Set OMX_IndexParamPortDefinition1 failed %x", error);
2248 clock_mutex.Unlock();
2252 port_def_type.nPortIndex=omx_deint_output_port;
2253 error = OMX_SetParameter(omx_vid_deint, OMX_IndexParamPortDefinition,
2255 if (error != OMX_ErrorNone) {
2256 Log::getInstance()->log("Video", Log::DEBUG,
2257 "Set OMX_IndexParamPortDefinition2 failed %x", error);
2258 clock_mutex.Unlock();
2263 EnablePort(omx_vid_dec,omx_codec_output_port,false);
2264 EnablePort(omx_vid_deint,omx_deint_input_port,false);
2265 EnablePort(omx_vid_deint,omx_deint_output_port,false);
2266 EnablePort(omx_vid_sched,omx_shed_input_port,false);
2268 clock_mutex.Unlock();
2274 void VideoOMX::PutBufferToPres(OMX_BUFFERHEADERTYPE* buffer, long long time,bool islast)
2276 input_bufs_omx_mutex.Lock();
2277 input_bufs_omx_present.push_back(buffer);
2278 input_time_present.push_back(time);
2279 input_is_last.push_back(islast);
2280 input_bufs_omx_mutex.Unlock();
2284 OMX_ERRORTYPE VideoOMX::ProtOMXEmptyThisBuffer(OMX_HANDLETYPE handle, OMX_BUFFERHEADERTYPE* buffer)
2286 // protect the call to empty this buffer
2289 pthread_testcancel();
2290 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldcancelstate);
2291 pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldcanceltype);
2293 OMX_ERRORTYPE ret_val;
2294 ret_val=OMX_EmptyThisBuffer(handle,buffer);
2295 clock_mutex.Unlock();
2296 pthread_setcancelstate(oldcancelstate, NULL);
2297 pthread_setcanceltype(oldcanceltype, NULL);
2298 pthread_testcancel();
2302 void VideoOMX::PrepareMediaSample(const MediaPacketList& mplist,UINT samplepos)
2305 mediapackets.clear();
2306 list<MediaPacket>::const_iterator begin=mplist.begin();
2307 list<MediaPacket>::const_iterator itty=mplist.begin();
2308 advance(itty,min(mplist.size(),10));
2309 mediapackets.insert(mediapackets.begin(),begin,itty);//front
2313 UINT VideoOMX::DeliverMediaSample(UCHAR* buffer, UINT *samplepos)
2316 while (consumed<mediapackets.size()) {
2317 DeliverMediaPacket(mediapackets[consumed], buffer, samplepos);
2318 if (*samplepos == mediapackets[consumed].length) {
2322 } else return consumed;
2327 UINT VideoOMX::DeliverMediaPacket(MediaPacket packet,
2328 const UCHAR* buffer,
2331 if (packet.type == MPTYPE_VIDEO_H264)
2341 //Later add fail back code for libav
2343 *samplepos+=packet.length;
2344 return packet.length;
2348 if (!omx_running) return 0; // if we are not runnig do not do this
2349 if (paused) return 0; //Block if we pause
2351 if (packet.synched && FrameSkip(packet.presentation_time)) {
2352 *samplepos=packet.length;
2353 Log::getInstance()->log("Video", Log::DEBUG, "DeliverMediaPacketOMX Frameskip");
2354 *samplepos=packet.length;
2355 return packet.length;
2357 //long long current_media_time=GetCurrentSystemTime()+playbacktimeoffset;
2358 /* if (packet.synched &&
2359 (packet.presentation_time<0 /*|| // preroll skip frames
2360 (packet.presentation_time+5000000LL)<(current_media_time)*)) { // we are late skip
2361 Log::getInstance()->log("Video", Log::DEBUG, "DeliverMediaPacketOMX Preroll or too late %lld %lld; %lld", packet.presentation_time,current_media_time,playbacktimeoffset);
2362 *samplepos=packet.length;
2363 return packet.length;
2366 OMX_ERRORTYPE error;
2368 /*First Check, if we have an video sample*/
2372 return 0; //Not in iframe mode!
2376 if (packet.disconti) {
2378 if (cur_input_buf_omx) {
2379 PutBufferToPres(cur_input_buf_omx, lastreftimeOMX,true);
2380 cur_input_buf_omx=NULL;
2384 /*Inspect PES-Header */
2386 // OMX_STATETYPE temp_state;
2387 // OMX_GetState(omx_vid_dec,&temp_state);
2389 if (*samplepos==0) {//stripheader
2390 headerstrip=buffer[packet.pos_buffer+8]+9/*is this right*/;
2391 // if (h264) Log::getInstance()->log("Video", Log::DEBUG, "PES info %x %x %x %x",
2392 // buffer[packet.pos_buffer+0],buffer[packet.pos_buffer+1],buffer[packet.pos_buffer+2],buffer[packet.pos_buffer+3]);
2393 *samplepos+=headerstrip;
2394 if ( packet.synched ) {
2395 if (cur_input_buf_omx) {
2396 cur_input_buf_omx->nFlags|=OMX_BUFFERFLAG_ENDOFFRAME;
2397 PutBufferToPres(cur_input_buf_omx, lastreftimeOMX,true);
2398 cur_input_buf_omx=NULL;//write out old data
2404 if (!firstsynched) {//
2405 *samplepos=packet.length;//if we have not processed at least one
2406 return packet.length;//synched packet ignore it!
2411 if (!cur_input_buf_omx) {
2412 input_bufs_omx_mutex.Lock();
2413 if (input_bufs_omx_free.size()==0) {
2414 input_bufs_omx_mutex.Unlock();
2415 //Log::getInstance()->log("Video", Log::DEBUG, "Deliver MediaPacket no free sample");
2416 return 0; // we do not have a free media sample
2419 cur_input_buf_omx=input_bufs_omx_free.front();
2420 cur_input_buf_omx->nFilledLen=0;
2421 cur_input_buf_omx->nOffset=0;
2422 cur_input_buf_omx->nTimeStamp=0;
2423 input_bufs_omx_free.pop_front();
2424 input_bufs_omx_mutex.Unlock();
2430 if (cur_input_buf_omx->nFilledLen==0) {//will only be changed on first packet
2431 if (packet.synched) {
2432 // Log::getInstance()->log("Video", Log::DEBUG, "packet synched marker");
2434 //lastreftimePTS=packet.pts;
2435 if (omx_first_frame) { // TODO time
2436 cur_input_buf_omx->nFlags=OMX_BUFFERFLAG_STARTTIME;
2437 Log::getInstance()->log("Video", Log::DEBUG, "Starttime");
2438 omx_first_frame=false;
2440 //cur_input_buf_omx->nFlags=0;
2441 cur_input_buf_omx->nFlags|=OMX_BUFFERFLAG_TIME_UNKNOWN;
2443 lastreftimeOMX=packet.presentation_time;
2444 // Log::getInstance()->log("Video", Log::DEBUG, "Time code %lld pts %lld", lastreftimeOMX,packet.pts);
2445 lastreftimePTS=packet.pts;
2446 cur_input_buf_omx->nTimeStamp=0;//lastreftimeOMX; // the clock component is faulty;
2450 cur_input_buf_omx->nFlags=OMX_BUFFERFLAG_TIME_UNKNOWN;
2451 cur_input_buf_omx->nTimeStamp=0;
2452 //Log::getInstance()->log("Video", Log::DEBUG, "packet unsynched marker");
2453 // ms->SetSyncPoint(TRUE);
2455 if (packet.disconti) cur_input_buf_omx->nFlags|=OMX_BUFFERFLAG_DISCONTINUITY;
2460 unsigned int haveToCopy=packet.length-*samplepos;
2462 while (haveToCopy> (cur_input_buf_omx->nAllocLen-cur_input_buf_omx->nFilledLen)) {
2463 unsigned int cancopy=cur_input_buf_omx->nAllocLen-cur_input_buf_omx->nFilledLen;
2464 memcpy(cur_input_buf_omx->pBuffer+cur_input_buf_omx->nFilledLen,buffer+packet.pos_buffer+*samplepos,cancopy);
2465 haveToCopy-=cancopy;
2466 cur_input_buf_omx->nFilledLen+=cancopy;
2467 *samplepos+=cancopy;
2468 // push old buffer out
2470 PutBufferToPres(cur_input_buf_omx, lastreftimeOMX,false);
2472 input_bufs_omx_mutex.Lock();
2473 if (input_bufs_omx_free.size()==0) {
2474 input_bufs_omx_mutex.Unlock();
2475 // Log::getInstance()->log("Video", Log::DEBUG, "Deliver MediaPacket no free sample2");
2476 return *samplepos; // we do not have a free media sample
2478 cur_input_buf_omx=input_bufs_omx_free.front();
2479 cur_input_buf_omx->nFilledLen=0;
2480 cur_input_buf_omx->nOffset=0;
2481 cur_input_buf_omx->nTimeStamp=0;
2482 input_bufs_omx_free.pop_front();
2483 input_bufs_omx_mutex.Unlock();
2485 cur_input_buf_omx->nFlags=OMX_BUFFERFLAG_TIME_UNKNOWN;
2488 memcpy(cur_input_buf_omx->pBuffer+cur_input_buf_omx->nFilledLen,
2489 buffer+packet.pos_buffer+*samplepos,haveToCopy);
2490 cur_input_buf_omx->nFilledLen+=haveToCopy;
2494 *samplepos+=haveToCopy;
2503 bool VideoOMX::displayIFrame(const UCHAR* buffer, UINT length) {
2504 if (!omx_running) return false;
2506 EnterIframePlayback();
2508 int haveToCopy = length;
2510 if (!cur_input_buf_omx) {
2511 input_bufs_omx_mutex.Lock();
2512 if (input_bufs_omx_free.size() == 0) {
2513 input_bufs_omx_mutex.Unlock();
2514 // Log::getInstance()->log("Video", Log::DEBUG,
2515 // "Deliver MediaPacket no free sample");
2516 return false; // we do not have a free media sample
2519 cur_input_buf_omx = input_bufs_omx_free.front();
2520 cur_input_buf_omx->nFilledLen = 0;
2521 cur_input_buf_omx->nOffset = 0;
2522 cur_input_buf_omx->nTimeStamp = 0;
2523 input_bufs_omx_free.pop_front();
2524 input_bufs_omx_mutex.Unlock();
2528 unsigned int pattern, packet_length;
2529 unsigned int headerstrip = 0;
2534 //Now we strip the pes header
2535 pattern = (buffer[0] << 16) | (buffer[1] << 8) | (buffer[2]);
2536 while (read_pos + 7 <= length) {
2537 pattern = ((pattern << 8) & 0xFFFFFFFF) | buffer[read_pos + 3];
2538 if (pattern < 0x000001E0 || pattern > 0x000001EF) {
2542 headerstrip = buffer[read_pos + 8] + 9/*is this right*/;
2543 packet_length = ((buffer[read_pos + 4] << 8)
2544 | (buffer[read_pos + 5])) + 6;
2545 if (read_pos + packet_length > length)
2548 if ((headerstrip < packet_length)
2549 && (cur_input_buf_omx->nFilledLen + packet_length
2550 - headerstrip) > cur_input_buf_omx->nAllocLen) {
2552 cur_input_buf_omx->nFlags = OMX_BUFFERFLAG_STARTTIME;
2555 cur_input_buf_omx->nFlags
2556 |= OMX_BUFFERFLAG_TIME_UNKNOWN;
2559 cur_input_buf_omx->nTimeStamp = 0;
2560 PutBufferToPres(cur_input_buf_omx, 0,false);
2561 cur_input_buf_omx = NULL;
2563 if (!cur_input_buf_omx) {
2565 while (count < 100 && omx_running && iframemode) {
2568 input_bufs_omx_mutex.Lock();
2569 if (input_bufs_omx_free.size() == 0) {
2570 input_bufs_omx_mutex.Unlock();
2571 // Log::getInstance()->log("Video", Log::DEBUG,
2572 // "Ifrane no free sample");
2574 if (!omx_running) return false;
2577 cur_input_buf_omx = input_bufs_omx_free.front();
2578 cur_input_buf_omx->nFilledLen = 0;
2579 cur_input_buf_omx->nOffset = 0;
2580 cur_input_buf_omx->nTimeStamp = 0;
2581 input_bufs_omx_free.pop_front();
2582 input_bufs_omx_mutex.Unlock();
2585 if (!cur_input_buf_omx)
2590 if (packet_length > headerstrip) {
2592 cur_input_buf_omx->pBuffer
2593 + cur_input_buf_omx->nFilledLen,
2594 buffer + read_pos + headerstrip,
2595 packet_length - headerstrip);
2596 cur_input_buf_omx->nFilledLen += packet_length
2599 read_pos += packet_length;
2601 pattern = (buffer[read_pos] << 16)
2602 | (buffer[read_pos + 1] << 8) | (buffer[read_pos + 2]);
2608 cur_input_buf_omx->nFlags = OMX_BUFFERFLAG_STARTTIME;
2611 cur_input_buf_omx->nFlags |= OMX_BUFFERFLAG_TIME_UNKNOWN;
2614 cur_input_buf_omx->nTimeStamp = 0;
2615 PutBufferToPres(cur_input_buf_omx, 0,false);
2616 cur_input_buf_omx = NULL;
2619 MILLISLEEP(40); //Block a bit
2623 int VideoOMX::EnterIframePlayback()
2626 if (cur_input_buf_omx) {
2627 PutBufferToPres(cur_input_buf_omx, lastreftimeOMX,true);
2629 cur_input_buf_omx = NULL;
2631 clock_mutex.Unlock();