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"
30 //A lot of parts of this file are heavily inspired by xbmc omx implementations
32 VideoOMX::VideoOMX() {
37 cur_input_buf_omx = NULL;
38 omx_h264 = omx_mpeg2 = true;
42 offsetvideonotset = true;
43 offsetaudionotset = true;
58 int VideoOMX::init(UCHAR tformat)
60 if (initted) return 0;
63 if (!setFormat(tformat)) { shutdown(); return 0; }
64 if (!setConnection(COMPOSITERGB)) { shutdown(); return 0; }
65 if (!setAspectRatio(ASPECT4X3)) { shutdown(); return 0; }
66 if (!setMode(NORMAL)) { shutdown(); return 0; }
67 if (!setSource()) { shutdown(); return 0; }
68 if (!attachFrameBuffer()) { shutdown(); return 0; }
83 int VideoOMX::initUsingOSDObjects()
86 // we are called before the audio
89 if (error!=OMX_ErrorNone) {
90 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX failed %x", error);
98 OMX_ERRORTYPE VideoOMX::EventHandler_OMX(OMX_IN OMX_HANDLETYPE handle,OMX_IN OMX_PTR appdata,
99 OMX_IN OMX_EVENTTYPE event_type,OMX_IN OMX_U32 data1,
100 OMX_IN OMX_U32 data2,OMX_IN OMX_PTR event_data) {
102 Log::getInstance()->log("Video", Log::NOTICE, "eventHandler %x %x %x %x %x",handle,event_type,data1,data2,event_data);
104 struct VPE_OMX_EVENT new_event;
105 new_event.handle=handle;
106 new_event.appdata=appdata;
107 new_event.event_type=event_type;
108 new_event.data1=data1;
109 new_event.data2=data2;
110 new_event.event_data=event_data;
112 VideoOMX *video=(VideoOMX *)getInstance();
113 video->AddOmxEvent(new_event);
115 /* switch (event_type) {
116 case OMX_EventCmdComplete: {
121 return OMX_ErrorNone;
125 void VideoOMX::AddOmxEvent(VPE_OMX_EVENT new_event)
127 omx_event_mutex.Lock();
128 omx_events.push_back(new_event);
129 omx_event_mutex.Unlock();
133 OMX_ERRORTYPE VideoOMX::EmptyBufferDone_OMX(OMX_IN OMX_HANDLETYPE hcomp,OMX_IN OMX_PTR appdata,OMX_IN OMX_BUFFERHEADERTYPE* buffer){
135 //Log::getInstance()->log("Video", Log::NOTICE, "EmptyBufferDone");
136 VideoOMX *video=(VideoOMX *)getInstance();
137 video->ReturnEmptyOMXBuffer(buffer);
138 return OMX_ErrorNone;
142 void VideoOMX::ReturnEmptyOMXBuffer(OMX_BUFFERHEADERTYPE* buffer){
143 input_bufs_omx_mutex.Lock();
144 // 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());
145 input_bufs_omx_free.push_back(buffer);
146 //Log::getInstance()->log("Video", Log::NOTICE, "ReturnEmptyOMXBuffer %d",input_bufs_omx_free.size());
147 input_bufs_omx_mutex.Unlock();
150 OMX_ERRORTYPE VideoOMX::FillBufferDone_OMX(OMX_IN OMX_HANDLETYPE hcomp, OMX_IN OMX_PTR appdata,OMX_IN OMX_BUFFERHEADERTYPE* buffer) {
151 Log::getInstance()->log("Video", Log::NOTICE, "FillBufferDone");
152 return OMX_ErrorNone;
157 int VideoOMX::shutdown()
159 if (!initted) return 0;
162 DeAllocateCodecsOMX();
168 int VideoOMX::setTVsize(UCHAR ttvsize)
172 // Override the aspect ratio usage, temporarily use to set the video chip mode
173 if (!setAspectRatio(tvsize)) { shutdown(); return 0; }
175 if ((fdVideo = open("/dev/vdec_dev", O_WRONLY)) < 0) return 0;
176 if (!setSource()) { shutdown(); return 0; }
177 if (!attachFramebuffer()) { shutdown(); return 0; }
179 // Reopening the fd causes the scart aspect line to go back to 4:3
180 // Set this again to the same as the tv screen size
181 if (!setAspectRatio(tvsize)) { shutdown(); return 0; }
183 // mode == LETTERBOX is invalid if the TV is widescreen
184 if (tvsize == ASPECT16X9) setMode(NORMAL);
189 int VideoOMX::setDefaultAspect()
191 return setAspectRatio(tvsize);
196 int VideoOMX::setFormat(UCHAR tformat)
198 if (!initted) return 0;
199 if ((tformat != PAL) && (tformat != NTSC)) return 0;
202 // if (ioctl(fdVideo, AV_SET_VID_DISP_FMT, format) != 0) return 0;
218 int VideoOMX::setConnection(UCHAR tconnection)
220 if (!initted) return 0;
221 if ((tconnection != COMPOSITERGB) && (tconnection != SVIDEO)) return 0;
222 connection = tconnection;
224 // if (ioctl(fdVideo, AV_SET_VID_OUTPUT, connection) != 0) return 0;
228 int VideoOMX::setAspectRatio(UCHAR taspectRatio)
230 if (!initted) return 0;
231 if ((taspectRatio != ASPECT4X3) && (taspectRatio != ASPECT16X9)) return 0;
232 aspectRatio = taspectRatio;
234 Log::getInstance()->log("Video", Log::DEBUG, "Setting aspect to %i", aspectRatio);
236 // if (ioctl(fdVideo, AV_SET_VID_RATIO, aspectRatio) != 0) return 0;
240 int VideoOMX::setMode(UCHAR tmode)
242 if (!initted) return 0;
244 if ((tmode == LETTERBOX) && (tvsize == ASPECT16X9)) return 0; // invalid mode
246 if ((tmode != NORMAL) && (tmode != LETTERBOX) && (tmode != UNKNOWN2) && (tmode != QUARTER) && (tmode != EIGHTH)
247 && (tmode != ZOOM) && (tmode != UNKNOWN6)) return 0;
250 // if (ioctl(fdVideo, AV_SET_VID_MODE, mode) != 0) return 0;
254 int VideoOMX::signalOff()
256 // if (ioctl(fdVideo, AV_SET_VID_DENC, 0) != 0) return 0;
260 int VideoOMX::signalOn()
262 // if (ioctl(fdVideo, AV_SET_VID_DENC, 1) != 0) return 0;
266 int VideoOMX::setSource()
268 if (!initted) return 0;
270 // What does this do...
271 // if (ioctl(fdVideo, AV_SET_VID_SRC, 1) != 0) return 0;
275 int VideoOMX::setPosition(int x, int y)
277 if (!initted) return 0;
279 // vid_pos_regs_t pos_d;
283 /* vid_pos_regs_t pos_d;
285 memset(&pos_d, 0, sizeof(pos_d));
310 pos_d.y = 100; // Top left X
311 pos_d.x = 50; // Top left Y
319 // if (ioctl(fdVideo, AV_SET_VID_POSITION, &pos_d) != 0) return 0;
325 if (!initted) return 0;
327 // if (ioctl(fdVideo, AV_SET_VID_SYNC, 2) != 0) return 0;
332 int VideoOMX::play() {
336 Log::getInstance()->log("Video", Log::DEBUG, "enter play");
338 if (AllocateCodecsOMX()) {
340 // Otherwise fall back to libav
344 Log::getInstance()->log("Video", Log::NOTICE,
345 "Allocate Codecs OMX failed assume h264 unsupported");
348 Log::getInstance()->log("Video", Log::NOTICE,
349 "Allocate Codecs OMX failed assume mpeg2 unsupported");
358 int VideoOMX::initClock()
362 if (clock_references==0)
365 static OMX_CALLBACKTYPE callbacks= {&EventHandler_OMX,&EmptyBufferDone_OMX,&FillBufferDone_OMX};
368 error=OMX_GetHandle(&omx_clock,VPE_OMX_CLOCK,NULL,&callbacks);
370 if (error!=OMX_ErrorNone) {
371 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX clock failed %x", error);
372 clock_mutex.Unlock();
373 DeAllocateCodecsOMX();
377 /* TODO Clock config to separate method */
378 OMX_PORT_PARAM_TYPE p_param;
379 memset(&p_param,0,sizeof(p_param));
380 p_param.nSize=sizeof(p_param);
381 p_param.nVersion.nVersion=OMX_VERSION;
382 error=OMX_GetParameter(omx_clock,OMX_IndexParamOtherInit,&p_param);
383 if (error!=OMX_ErrorNone) {
384 Log::getInstance()->log("Video", Log::DEBUG, "Init clock OMX_GetParameter failed %x", error);
385 clock_mutex.Unlock();
386 DeAllocateCodecsOMX();
389 omx_clock_output_port=p_param.nStartPortNumber;
391 for (unsigned int i=0;i<p_param.nPorts;i++) {
392 if (!DisablePort(omx_clock,p_param.nStartPortNumber+i,true) ) {
393 Log::getInstance()->log("Video", Log::DEBUG, "Disable Ports OMX clock failed %d",i);
394 clock_mutex.Unlock();
395 DeAllocateCodecsOMX();
404 Log::getInstance()->log("Video", Log::DEBUG, "init omx clock %x %x",this,omx_clock);
406 clock_mutex.Unlock();
410 int VideoOMX::getClockAudioandInit(OMX_HANDLETYPE *p_omx_clock,OMX_U32 *p_omx_clock_output_port)
414 *p_omx_clock_output_port=0;
421 OMX_TIME_CONFIG_ACTIVEREFCLOCKTYPE refclock;
422 memset(&refclock,0,sizeof(refclock));
423 refclock.nSize=sizeof(refclock);
424 refclock.nVersion.nVersion=OMX_VERSION;
426 refclock.eClock=OMX_TIME_RefClockAudio;
428 //refclock.eClock=OMX_TIME_RefClockVideo;
429 error=OMX_SetConfig(omx_clock,OMX_IndexConfigTimeActiveRefClock,&refclock);
430 if (error!=OMX_ErrorNone){
431 Log::getInstance()->log("Video", Log::DEBUG, "Clock OMX_IndexConfigTimeActiveRefClock failed %x", error);
432 clock_mutex.Unlock();
433 DeAllocateCodecsOMX();
437 OMX_PORT_PARAM_TYPE p_param;
438 memset(&p_param,0,sizeof(p_param));
439 p_param.nSize=sizeof(p_param);
440 p_param.nVersion.nVersion=OMX_VERSION;
441 error=OMX_GetParameter(omx_clock,OMX_IndexParamOtherInit,&p_param);
442 if (error!=OMX_ErrorNone){
443 Log::getInstance()->log("Video", Log::DEBUG, "Init clock OMX_GetParameter failed %x", error);
444 clock_mutex.Unlock();
445 DeAllocateCodecsOMX();
449 OMX_TIME_CONFIG_CLOCKSTATETYPE clock_conf;
450 memset(&clock_conf,0,sizeof(clock_conf));
451 clock_conf.nSize=sizeof(clock_conf);
452 clock_conf.nVersion.nVersion=OMX_VERSION;
453 clock_conf.eState=OMX_TIME_ClockStateWaitingForStartTime;
454 clock_conf.nStartTime=0;
455 clock_conf.nOffset=0;
456 if (clock_references==1) clock_conf.nWaitMask=OMX_CLOCKPORT1;
457 else clock_conf.nWaitMask=OMX_CLOCKPORT0|OMX_CLOCKPORT1;
458 error=OMX_SetConfig(omx_clock,OMX_IndexConfigTimeClockState,&clock_conf);
459 if (error!=OMX_ErrorNone) {
460 Log::getInstance()->log("Video", Log::DEBUG, "AuI Clock IndexConfigTimeClockState failed %x", error);
464 *p_omx_clock_output_port=p_param.nStartPortNumber+1;
465 *p_omx_clock=omx_clock;
466 clock_mutex.Unlock();
470 int VideoOMX::getClockVideoandInit()
479 if (clock_references==1) { // only if no audio is attached to this clock!
480 OMX_TIME_CONFIG_ACTIVEREFCLOCKTYPE refclock;
481 memset(&refclock,0,sizeof(refclock));
482 refclock.nSize=sizeof(refclock);
483 refclock.nVersion.nVersion=OMX_VERSION;
485 //refclock.eClock=OMX_TIME_RefClockAudio;
487 refclock.eClock=OMX_TIME_RefClockVideo;
488 error=OMX_SetConfig(omx_clock,OMX_IndexConfigTimeActiveRefClock,&refclock);
489 if (error!=OMX_ErrorNone) {
490 Log::getInstance()->log("Video", Log::DEBUG, "Clock OMX_IndexConfigTimeActiveRefClock failed %x", error);
491 clock_mutex.Unlock();
492 DeAllocateCodecsOMX();
497 OMX_PORT_PARAM_TYPE p_param;
498 memset(&p_param,0,sizeof(p_param));
499 p_param.nSize=sizeof(p_param);
500 p_param.nVersion.nVersion=OMX_VERSION;
501 error=OMX_GetParameter(omx_clock,OMX_IndexParamOtherInit,&p_param);
502 if (error!=OMX_ErrorNone){
503 Log::getInstance()->log("Video", Log::DEBUG, "Init clock OMX_GetParameter failed %x", error);
504 clock_mutex.Unlock();
505 DeAllocateCodecsOMX();
510 OMX_TIME_CONFIG_CLOCKSTATETYPE clock_conf;
511 memset(&clock_conf,0,sizeof(clock_conf));
512 clock_conf.nSize=sizeof(clock_conf);
513 clock_conf.nVersion.nVersion=OMX_VERSION;
514 clock_conf.eState=OMX_TIME_ClockStateWaitingForStartTime;
515 clock_conf.nStartTime=0;
516 clock_conf.nOffset=0;
517 if (clock_references==1) clock_conf.nWaitMask=OMX_CLOCKPORT0;
518 else clock_conf.nWaitMask=OMX_CLOCKPORT0|OMX_CLOCKPORT1;
519 error=OMX_SetConfig(omx_clock,OMX_IndexConfigTimeClockState,&clock_conf);
520 if (error!=OMX_ErrorNone) {
521 Log::getInstance()->log("Video", Log::DEBUG, "VuI Clock IndexConfigTimeClockState failed %x", error);
524 omx_clock_output_port=p_param.nStartPortNumber;
525 clock_mutex.Unlock();
530 void VideoOMX::clockUnpause()
534 OMX_TIME_CONFIG_CLOCKSTATETYPE clock_conf;
535 memset(&clock_conf,0,sizeof(clock_conf));
536 clock_conf.nSize=sizeof(clock_conf);
537 clock_conf.nVersion.nVersion=OMX_VERSION;
538 clock_conf.eState=OMX_TIME_ClockStateRunning;
539 clock_conf.nStartTime=0;
540 clock_conf.nOffset=0;
541 clock_conf.nWaitMask=OMX_CLOCKPORT1;
542 error=OMX_SetConfig(omx_clock,OMX_IndexConfigTimeClockState,&clock_conf);
543 if (error!=OMX_ErrorNone) {
544 Log::getInstance()->log("Video", Log::DEBUG, "ClockUnpause IndexConfigTimeClockState failed %x", error);
547 clock_mutex.Unlock();
551 void VideoOMX::clockPause()
555 OMX_TIME_CONFIG_CLOCKSTATETYPE clock_conf;
556 memset(&clock_conf,0,sizeof(clock_conf));
557 clock_conf.nSize=sizeof(clock_conf);
558 clock_conf.nVersion.nVersion=OMX_VERSION;
559 clock_conf.eState=OMX_TIME_ClockStateStopped;
560 clock_conf.nStartTime=0;
561 clock_conf.nOffset=0;
562 clock_conf.nWaitMask=OMX_CLOCKPORT1;
563 error=OMX_SetConfig(omx_clock,OMX_IndexConfigTimeClockState,&clock_conf);
564 if (error!=OMX_ErrorNone) {
565 Log::getInstance()->log("Video", Log::DEBUG, "ClockUnpause IndexConfigTimeClockState failed %x", error);
567 clock_mutex.Unlock();
572 int VideoOMX::AllocateCodecsOMX()
575 static OMX_CALLBACKTYPE callbacks= {&EventHandler_OMX,&EmptyBufferDone_OMX,&FillBufferDone_OMX};
577 Log::getInstance()->log("Video", Log::NOTICE, "Allocate Codecs OMX");
578 //Clock, move later to audio including events
580 Log::getInstance()->log("Video", Log::DEBUG, "Nmark1 ");
581 if (!getClockVideoandInit()){
582 return 0;// get the clock and init it if necessary
584 Log::getInstance()->log("Video", Log::DEBUG, "Nmark2 ");
587 Log::getInstance()->log("Video", Log::DEBUG, "idleClock failed");
595 error=OMX_GetHandle(&omx_vid_dec,VPE_OMX_H264_DECODER,NULL,&callbacks);
597 error=OMX_GetHandle(&omx_vid_dec,VPE_OMX_MPEG2_DECODER,NULL,&callbacks);
600 if (error!=OMX_ErrorNone){
601 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX video decoder failed %x", error);
602 clock_mutex.Unlock();
603 DeAllocateCodecsOMX();
608 Log::getInstance()->log("Video", Log::DEBUG, "Nmark3 ");
609 OMX_PORT_PARAM_TYPE p_param;
610 memset(&p_param,0,sizeof(p_param));
611 p_param.nSize=sizeof(p_param);
612 p_param.nVersion.nVersion=OMX_VERSION;
613 error=OMX_GetParameter(omx_vid_dec,OMX_IndexParamVideoInit,&p_param);
614 if (error!=OMX_ErrorNone){
615 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX h264 decoder OMX_GetParameter failed %x", error);
616 clock_mutex.Unlock();
617 DeAllocateCodecsOMX();
620 omx_codec_input_port=p_param.nStartPortNumber;
621 omx_codec_output_port=p_param.nStartPortNumber+1;
623 if (!DisablePort(omx_vid_dec,omx_codec_input_port) || !DisablePort(omx_vid_dec,omx_codec_output_port)) {
624 Log::getInstance()->log("Video", Log::DEBUG, "Disable Ports OMX video decoder failed");
625 clock_mutex.Unlock();
626 DeAllocateCodecsOMX();
630 Log::getInstance()->log("Video", Log::DEBUG, "Nmark4 ");
632 OMX_PARAM_BRCMVIDEODECODEERRORCONCEALMENTTYPE conceal;
633 memset(&conceal,0,sizeof(conceal));
634 conceal.nSize=sizeof(conceal);
635 conceal.nVersion.nVersion=OMX_VERSION;
636 conceal.bStartWithValidFrame=OMX_FALSE;
638 error=OMX_SetParameter(omx_vid_dec,OMX_IndexParamBrcmVideoDecodeErrorConcealment,&conceal);
639 if (error!=OMX_ErrorNone){
640 Log::getInstance()->log("Video", Log::DEBUG, "OMX_IndexParamBrcmVideoDecodeErrorConcealment failed %x", error);
641 clock_mutex.Unlock();
642 DeAllocateCodecsOMX();
647 error=OMX_GetHandle(&omx_vid_sched,VPE_OMX_VIDEO_SCHED,NULL,&callbacks);
648 if (error!=OMX_ErrorNone){
649 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX video scheduler failed %x", error);
650 clock_mutex.Unlock();
651 DeAllocateCodecsOMX();
657 error=OMX_GetParameter(omx_vid_sched,OMX_IndexParamVideoInit,&p_param);
658 if (error!=OMX_ErrorNone){
659 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX video scheduler OMX_GetParameter failed %x", error);
660 clock_mutex.Unlock();
661 DeAllocateCodecsOMX();
664 omx_shed_input_port=p_param.nStartPortNumber;
665 omx_shed_output_port=p_param.nStartPortNumber+1;
668 error=OMX_GetParameter(omx_vid_sched,OMX_IndexParamOtherInit,&p_param);
669 if (error!=OMX_ErrorNone){
670 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX video scheduler OMX_GetParameter failed %x", error);
671 clock_mutex.Unlock();
672 DeAllocateCodecsOMX();
675 omx_shed_clock_port=p_param.nStartPortNumber;
676 Log::getInstance()->log("Video", Log::DEBUG, "scheduler ports %d %d %d ",omx_shed_input_port,omx_shed_output_port,omx_shed_clock_port);
679 if (!DisablePort(omx_vid_sched,omx_shed_input_port,true) || !DisablePort(omx_vid_sched,omx_shed_output_port,true)
680 || !DisablePort(omx_vid_sched,omx_shed_clock_port,true)) {
681 Log::getInstance()->log("Video", Log::DEBUG, "Disable Ports OMX video shed failed");
682 clock_mutex.Unlock();
683 DeAllocateCodecsOMX();
688 error=OMX_GetHandle(&omx_vid_rend,VPE_OMX_VIDEO_REND,NULL,&callbacks);
689 if (error!=OMX_ErrorNone){
690 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX video rend failed %x", error);
691 clock_mutex.Unlock();
692 DeAllocateCodecsOMX();
696 error=OMX_GetParameter(omx_vid_rend,OMX_IndexParamVideoInit,&p_param);
697 if (error!=OMX_ErrorNone){
698 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX video rend OMX_GetParameter failed %x", error);
699 clock_mutex.Unlock();
700 DeAllocateCodecsOMX();
703 omx_rend_input_port=p_param.nStartPortNumber;
704 //omx_rend_output_port=p_param.nStartPortNumber+1;
707 if (!DisablePort(omx_vid_rend,omx_rend_input_port,true) /*|| !DisablePort(omx_vid_rend,omx_rend_output_port)*/
709 Log::getInstance()->log("Video", Log::DEBUG, "Disable Ports OMX video rend failed");
710 clock_mutex.Unlock();
711 DeAllocateCodecsOMX();
719 error=OMX_SetupTunnel(omx_clock,omx_clock_output_port,omx_vid_sched,omx_shed_clock_port);
720 if (error!=OMX_ErrorNone){
721 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);
722 clock_mutex.Unlock();
723 DeAllocateCodecsOMX();
727 if (!EnablePort(omx_clock,omx_clock_output_port,false) || !EnablePort(omx_vid_sched,omx_shed_clock_port,false)
729 Log::getInstance()->log("Video", Log::DEBUG, "Enable Ports OMX clock shed failed");
730 clock_mutex.Unlock();
731 DeAllocateCodecsOMX();
736 Log::getInstance()->log("Video", Log::DEBUG, "mark2 ");
737 if (!ChangeComponentState(omx_vid_sched,OMX_StateIdle)) {
738 Log::getInstance()->log("Video", Log::DEBUG, "vid_sched idle ChangeComponentState");
739 clock_mutex.Unlock();
740 DeAllocateCodecsOMX();
746 Log::getInstance()->log("Video", Log::DEBUG, "mark1 ");
747 if ( !CommandFinished(omx_vid_sched,OMX_CommandPortEnable,omx_shed_clock_port)) {
748 clock_mutex.Unlock();
749 DeAllocateCodecsOMX();
756 Log::getInstance()->log("Video", Log::DEBUG, "mark1 special ");
757 if ( !CommandFinished(omx_clock,OMX_CommandPortEnable,omx_clock_output_port)) {
758 clock_mutex.Unlock();
759 DeAllocateCodecsOMX();
762 clock_mutex.Unlock();
765 Log::getInstance()->log("Video", Log::DEBUG, "mark1b ");
768 /* error=OMX_SendCommand(omx_vid_dec,OMX_CommandStateSet,OMX_StateIdle,0);
769 if (error!=OMX_ErrorNone){
770 Log::getInstance()->log("Video", Log::DEBUG, "vid_dec Send Command to OMX State Idle %x", error);
774 OMX_VIDEO_PARAM_PORTFORMATTYPE ft_type;
775 memset(&ft_type,0,sizeof(ft_type));
776 ft_type.nSize=sizeof(ft_type);
777 ft_type.nVersion.nVersion=OMX_VERSION;
779 ft_type.nPortIndex=omx_codec_input_port;
781 ft_type.eCompressionFormat=OMX_VIDEO_CodingAVC;
783 ft_type.eCompressionFormat=OMX_VIDEO_CodingMPEG2;
786 Demuxer* demux=Demuxer::getInstance();
788 ft_type.xFramerate=0;//25*(1<<16);//demux->getFrameRate()*(1<<16);
789 Log::getInstance()->log("Video", Log::DEBUG, "Framerate: %d",demux->getFrameRate());
790 error=OMX_SetParameter(omx_vid_dec,OMX_IndexParamVideoPortFormat,&ft_type);
791 if (error!=OMX_ErrorNone){
792 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX_IndexParamVideoPortFormat failed %x", error);
793 clock_mutex.Unlock();
794 DeAllocateCodecsOMX();
799 if (!ChangeComponentState(omx_vid_dec,OMX_StateIdle)) {
800 Log::getInstance()->log("Video", Log::DEBUG, "vid_dec ChangeComponentState");
801 clock_mutex.Unlock();
802 DeAllocateCodecsOMX();
807 if (!PrepareInputBufsOMX()) {
808 clock_mutex.Unlock();
809 DeAllocateCodecsOMX();
814 error=OMX_SetupTunnel(omx_vid_dec,omx_codec_output_port,omx_vid_sched,omx_shed_input_port);
815 if (error!=OMX_ErrorNone){
816 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel dec to sched failed %x", error);
817 clock_mutex.Unlock();
818 DeAllocateCodecsOMX();
824 if (!EnablePort(omx_vid_dec,omx_codec_output_port,false) || !EnablePort(omx_vid_sched,omx_shed_input_port,false)
826 Log::getInstance()->log("Video", Log::DEBUG, "Enable Ports OMX codec shed failed");
827 clock_mutex.Unlock();
828 DeAllocateCodecsOMX();
832 if ( !CommandFinished(omx_vid_dec,OMX_CommandPortEnable,omx_codec_output_port) || !CommandFinished(omx_vid_sched,OMX_CommandPortEnable,omx_shed_input_port)) {
833 clock_mutex.Unlock();
834 DeAllocateCodecsOMX();
838 if (!ChangeComponentState(omx_vid_dec,OMX_StateExecuting)) {
839 Log::getInstance()->log("Video", Log::DEBUG, "omx_vid_dec ChangeComponentState Execute");
840 clock_mutex.Unlock();
841 DeAllocateCodecsOMX();
845 error=OMX_SetupTunnel(omx_vid_sched,omx_shed_output_port,omx_vid_rend,omx_rend_input_port);
846 if (error!=OMX_ErrorNone){
847 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel sched to rend failed %x", error);
848 clock_mutex.Unlock();
849 DeAllocateCodecsOMX();
853 if (!EnablePort(omx_vid_sched,omx_shed_output_port,false) || !EnablePort(omx_vid_rend,omx_rend_input_port,false)
855 Log::getInstance()->log("Video", Log::DEBUG, "Enable Ports OMX shed rend failed");
856 clock_mutex.Unlock();
857 DeAllocateCodecsOMX();
861 if (!CommandFinished(omx_vid_sched,OMX_CommandPortEnable,omx_shed_output_port)
862 || !CommandFinished(omx_vid_rend,OMX_CommandPortEnable,omx_rend_input_port)) {
863 clock_mutex.Unlock();
864 DeAllocateCodecsOMX();
868 if (!ChangeComponentState(omx_vid_rend,OMX_StateIdle)) {
869 Log::getInstance()->log("Video", Log::DEBUG, "vid_rend ChangeComponentState");
870 clock_mutex.Unlock();
871 DeAllocateCodecsOMX();
876 if (!ChangeComponentState(omx_vid_sched,OMX_StateExecuting)) {
877 Log::getInstance()->log("Video", Log::DEBUG, "omx_vid_sched ChangeComponentState Execute");
878 clock_mutex.Unlock();
879 DeAllocateCodecsOMX();
883 if (!ChangeComponentState(omx_vid_rend,OMX_StateExecuting)) {
884 Log::getInstance()->log("Video", Log::DEBUG, "omx_vid_rend ChangeComponentState Execute");
885 clock_mutex.Unlock();
886 DeAllocateCodecsOMX();
891 OMX_CONFIG_DISPLAYREGIONTYPE dispconf;
892 memset(&dispconf,0,sizeof(dispconf));
893 dispconf.nSize=sizeof(dispconf);
894 dispconf.nVersion.nVersion=OMX_VERSION;
896 dispconf.nPortIndex=omx_rend_input_port;
898 dispconf.set=OMX_DISPLAY_SET_LAYER ;
900 error=OMX_SetConfig(omx_vid_rend,OMX_IndexConfigDisplayRegion,&dispconf);
901 if (error!=OMX_ErrorNone){
902 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX_IndexConfigDisplayRegion failed %x", error);
903 clock_mutex.Unlock();
904 DeAllocateCodecsOMX();
908 /* dispconf.set=OMX_DISPLAY_SET_FULLSCREEN ;
909 dispconf.fullscreen=OMX_FALSE;
910 error=OMX_SetParameter(omx_vid_rend,OMX_IndexConfigDisplayRegion,&dispconf);
911 if (error!=OMX_ErrorNone){
912 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX_IndexConfigDisplayRegion failed %x", error);
913 clock_mutex.Unlock();
914 DeAllocateCodecsOMX();
918 dispconf.set=OMX_DISPLAY_SET_DEST_RECT;
919 dispconf.dest_rect.x_offset=100;
920 dispconf.dest_rect.y_offset=100;
921 dispconf.dest_rect.width=640;
922 dispconf.dest_rect.height=480;
923 error=OMX_SetParameter(omx_vid_rend,OMX_IndexConfigDisplayRegion,&dispconf);
924 if (error!=OMX_ErrorNone){
925 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX_IndexConfigDisplayRegion failed %x", error);
926 clock_mutex.Unlock();
927 DeAllocateCodecsOMX();
932 playbacktimeoffset=-GetCurrentSystemTime();
936 clock_mutex.Unlock();
939 setClockExecutingandRunning();
948 int VideoOMX::idleClock()
951 OMX_STATETYPE temp_state;
953 OMX_GetState(omx_clock,&temp_state);
955 if (temp_state!=OMX_StateIdle) {
956 if (!ChangeComponentState(omx_clock,OMX_StateIdle)) {
957 Log::getInstance()->log("Video", Log::DEBUG, "omx_clock ChangeComponentState Idle failed");
958 clock_mutex.Unlock();
962 clock_mutex.Unlock();
966 int VideoOMX::setClockExecutingandRunning()
969 OMX_STATETYPE temp_state;
971 OMX_GetState(omx_clock,&temp_state);
973 if (temp_state!=OMX_StateExecuting) {
974 if (!ChangeComponentState(omx_clock,OMX_StateExecuting)) {
975 Log::getInstance()->log("Video", Log::DEBUG, "omx_clock ChangeComponentState Execute failed");
976 clock_mutex.Unlock();
977 DeAllocateCodecsOMX();
982 OMX_TIME_CONFIG_CLOCKSTATETYPE clock_conf;
983 memset(&clock_conf,0,sizeof(clock_conf));
984 clock_conf.nSize=sizeof(clock_conf);
985 clock_conf.nVersion.nVersion=OMX_VERSION;
986 clock_conf.eState=OMX_TIME_ClockStateRunning;
987 error=OMX_SetConfig(omx_clock,OMX_IndexConfigTimeClockState,&clock_conf);
988 if (error!=OMX_ErrorNone) {
989 Log::getInstance()->log("Video", Log::DEBUG, "Clock IndexConfigTimeClockState failed %x", error);
990 clock_mutex.Unlock();
991 DeAllocateCodecsOMX();
994 clock_mutex.Unlock();
1000 int VideoOMX::ChangeComponentState(OMX_HANDLETYPE handle,OMX_STATETYPE type) //needs to be called with locked mutex
1002 OMX_ERRORTYPE error;
1003 error=OMX_SendCommand(handle,OMX_CommandStateSet,type,0);
1004 if (error!=OMX_ErrorNone){
1005 Log::getInstance()->log("Video", Log::DEBUG, "handle %x Send Command to OMX State %x %x",handle,type, error);
1009 if (!CommandFinished(handle,OMX_CommandStateSet,type)) {
1017 int VideoOMX::EnablePort(OMX_HANDLETYPE handle,OMX_U32 port,bool wait) //needs to be called with locked mutex
1019 OMX_ERRORTYPE error;
1020 error=OMX_SendCommand(handle,OMX_CommandPortEnable,port,0);
1021 if (error!=OMX_ErrorNone){
1022 Log::getInstance()->log("Video", Log::DEBUG, "handle %x Send Command to enable port %x %x",handle,port, error);
1026 if (!wait) return 1;
1027 if (!CommandFinished(handle,OMX_CommandPortEnable,port)) {
1035 int VideoOMX::DisablePort(OMX_HANDLETYPE handle,OMX_U32 port,bool wait) //needs to be called with locked mutex
1037 OMX_ERRORTYPE error;
1038 error=OMX_SendCommand(handle,OMX_CommandPortDisable,port,0);
1039 if (error!=OMX_ErrorNone){
1040 Log::getInstance()->log("Video", Log::DEBUG, "handle %x Send Command to disable port %x %x",handle,port, error);
1044 if (!wait) return 1;
1045 if (!CommandFinished(handle,OMX_CommandPortDisable,port)) {
1056 int VideoOMX::CommandFinished(OMX_HANDLETYPE handle,OMX_U32 command,OMX_U32 data2) //needs to be called with locked mutex
1060 omx_event_mutex.Lock();
1061 list<VPE_OMX_EVENT>::iterator itty=omx_events.begin();
1062 while (itty!=omx_events.end()) {
1064 VPE_OMX_EVENT current=*itty;
1065 if (current.handle==handle) { //this is ours
1066 if (current.event_type==OMX_EventError) {
1067 omx_events.erase(itty);
1068 omx_event_mutex.Unlock();
1069 Log::getInstance()->log("Video", Log::DEBUG, "Command Finished on Error");
1072 } else if (current.event_type==OMX_EventCmdComplete && current.data1==command && current.data2==data2) {
1073 omx_events.erase(itty);
1074 omx_event_mutex.Unlock();
1075 //Log::getInstance()->log("Video", Log::DEBUG, "Command Finished Completed");
1082 omx_event_mutex.Unlock();
1087 Log::getInstance()->log("Video", Log::DEBUG, "CommandFinished waited too long %x %x %x",handle,command, data2);
1096 int VideoOMX::PrepareInputBufsOMX() //needs to be called with locked mutex
1098 OMX_ERRORTYPE error;
1099 OMX_PARAM_PORTDEFINITIONTYPE port_def_type;
1100 memset(&port_def_type,0,sizeof(port_def_type));
1101 port_def_type.nSize=sizeof(port_def_type);
1102 port_def_type.nVersion.nVersion=OMX_VERSION;
1103 port_def_type.nPortIndex=omx_codec_input_port;
1105 error=OMX_GetParameter(omx_vid_dec,OMX_IndexParamPortDefinition, &port_def_type);
1107 if (error!=OMX_ErrorNone){
1108 Log::getInstance()->log("Video", Log::DEBUG, "Get OMX OMX_IndexParamPortDefinition failed %x", error);
1110 /* Log::getInstance()->log("Video", Log::DEBUG, "Port para %d %d %d %d %d %d %d", port_def_type.nBufferCountActual,
1111 port_def_type.nBufferCountMin,port_def_type.nBufferSize,port_def_type.bEnabled,port_def_type.bPopulated,
1112 port_def_type.bBuffersContiguous,port_def_type.nBufferAlignment);*/
1114 port_def_type.nBufferCountActual=100;
1115 port_def_type.nBufferSize=max(port_def_type.nBufferSize,200000); // for transcoder important
1117 error=OMX_SetParameter(omx_vid_dec,OMX_IndexParamPortDefinition, &port_def_type);
1119 if (error!=OMX_ErrorNone){
1120 Log::getInstance()->log("Video", Log::DEBUG, "Set OMX OMX_IndexParamPortDefinition failed %x", error);
1124 error=OMX_SendCommand(omx_vid_dec,OMX_CommandPortEnable,omx_codec_input_port,0);
1125 if (error!=OMX_ErrorNone){
1126 Log::getInstance()->log("Video", Log::DEBUG, "Prepare Input bufs Send Command to enable port %x", error);
1130 input_bufs_omx_mutex.Lock();
1131 for (unsigned int i=0; i< port_def_type.nBufferCountActual;i++) {
1133 // unsigned char* new_buffer_data=(unsigned char*)malloc(port_def_type.nbufferSize);
1134 OMX_BUFFERHEADERTYPE *buf_head=NULL;
1135 /* error=OMX_Usebuffer(omx_vid_dec,&buf_head,omx_codec_input_port,NULL,port_def_type.nbufferSize,new_buffer_data);
1136 if (error!=OMX_ErrorNone){
1137 Log::getInstance()->log("Video", Log::DEBUG, "Use OMX_Usebuffer failed %x", error);
1138 input_bufs_omx_mutex.Unlock();
1141 error=OMX_AllocateBuffer(omx_vid_dec,&buf_head,omx_codec_input_port,NULL,port_def_type.nBufferSize);
1142 if (error!=OMX_ErrorNone){
1143 Log::getInstance()->log("Video", Log::DEBUG, "Use OMX_AllocateBuffer failed %x", error);
1144 input_bufs_omx_mutex.Unlock();
1147 input_bufs_omx_all.push_back(buf_head);
1148 input_bufs_omx_free.push_back(buf_head);
1150 omx_first_frame=true;
1153 cur_input_buf_omx=NULL;
1154 input_bufs_omx_mutex.Unlock();
1157 Log::getInstance()->log("Video", Log::DEBUG, "PrepareInputBufsOMX mark3");
1158 if (!CommandFinished(omx_vid_dec,OMX_CommandPortEnable,omx_codec_input_port)) {
1161 Log::getInstance()->log("Video", Log::DEBUG, "PrepareInputBufsOMX mark4");
1166 int VideoOMX::DestroyInputBufsOMX() //need s to be called with locked mutex
1168 OMX_ERRORTYPE error;
1170 cur_input_buf_omx=NULL;
1171 input_bufs_omx_mutex.Lock();
1172 for (int i=0; i< input_bufs_omx_all.size();i++) {
1173 // free(input_bufs_omx_all[i]->pBuffer);
1174 // input_bufs_omx_all[i]->pBuffer=NULL;
1175 error=OMX_FreeBuffer(omx_vid_dec,omx_codec_input_port,input_bufs_omx_all[i]);
1176 if (error!=OMX_ErrorNone){
1177 Log::getInstance()->log("Video", Log::DEBUG, "Use OMX_FreeBuffer failed %x", error);
1178 input_bufs_omx_mutex.Unlock();
1183 input_bufs_omx_all.clear();
1184 input_bufs_omx_free.clear();
1185 input_bufs_omx_present.clear();
1186 input_time_present.clear();
1187 input_bufs_omx_mutex.Unlock();
1194 int VideoOMX::DeAllocateCodecsOMX()
1196 OMX_ERRORTYPE error;
1198 Log::getInstance()->log("Video", Log::DEBUG, "enter deallocatecodecsomx");
1202 if (cur_input_buf_omx) {
1203 cur_input_buf_omx->nFlags|=OMX_BUFFERFLAG_EOS;
1204 OMX_ERRORTYPE error=ProtOMXEmptyThisBuffer(omx_vid_dec,cur_input_buf_omx);
1205 if (error!=OMX_ErrorNone) {
1206 Log::getInstance()->log("Video", Log::DEBUG, "OMX_EmptyThisBuffer failed %x", error);
1209 cur_input_buf_omx=NULL;//write out old data
1213 // first stop the omx elements
1214 if (!ChangeComponentState(omx_vid_dec,OMX_StateIdle)) {
1215 Log::getInstance()->log("Video", Log::DEBUG, "vid_dec ChangeComponentState");
1218 clock_mutex.Unlock();
1224 if (!ChangeComponentState(omx_vid_sched,OMX_StateIdle)) {
1225 Log::getInstance()->log("Video", Log::DEBUG, "vid_shed ChangeComponentState");
1229 if (!ChangeComponentState(omx_vid_rend,OMX_StateIdle)) {
1230 Log::getInstance()->log("Video", Log::DEBUG, "vid_rend ChangeComponentState");
1236 // TODO proper deinit sequence
1237 // first flush all buffers
1240 error = OMX_SendCommand(omx_vid_dec, OMX_CommandFlush,
1241 omx_codec_output_port, NULL);
1242 if (error != OMX_ErrorNone) {
1243 Log::getInstance()->log("Video", Log::DEBUG,
1244 "OMX_Flush codec out failed %x", error);
1248 error = OMX_SendCommand(omx_vid_sched, OMX_CommandFlush,
1249 omx_shed_input_port, NULL);
1250 if (error != OMX_ErrorNone) {
1251 Log::getInstance()->log("Video", Log::DEBUG,
1252 "OMX_Flush shed in failed %x", error);
1256 if (!CommandFinished(omx_vid_dec, OMX_CommandFlush,
1257 omx_codec_output_port)) {
1258 Log::getInstance()->log("Video", Log::DEBUG,
1259 "flush cmd codec failed");
1262 if (!CommandFinished(omx_vid_sched, OMX_CommandFlush,
1263 omx_shed_input_port)) {
1264 Log::getInstance()->log("Video", Log::DEBUG,
1265 "flush cmd shed failed");
1271 error = OMX_SendCommand(omx_vid_rend, OMX_CommandFlush,
1272 omx_rend_input_port, NULL);
1273 if (error != OMX_ErrorNone) {
1274 Log::getInstance()->log("Video", Log::DEBUG,
1275 "OMX_Flush rend in failed %x", error);
1279 error = OMX_SendCommand(omx_vid_sched, OMX_CommandFlush,
1280 omx_shed_output_port, NULL);
1281 if (error != OMX_ErrorNone) {
1282 Log::getInstance()->log("Video", Log::DEBUG,
1283 "OMX_Flush shed out failed %x", error);
1289 if (!CommandFinished(omx_vid_rend, OMX_CommandFlush,
1290 omx_rend_input_port)) {
1291 Log::getInstance()->log("Video", Log::DEBUG,
1292 "flush cmd shed rend failed");
1295 if (!CommandFinished(omx_vid_sched, OMX_CommandFlush,
1296 omx_shed_output_port)) {
1297 Log::getInstance()->log("Video", Log::DEBUG,
1298 "flush cmd shed rend failed");
1305 error=OMX_SendCommand(omx_clock,OMX_CommandFlush, omx_clock_output_port, NULL);
1306 if (error!=OMX_ErrorNone){
1307 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Flush clock out failed %x", error);
1311 error=OMX_SendCommand(omx_vid_sched,OMX_CommandFlush, omx_shed_clock_port, NULL);
1312 if (error!=OMX_ErrorNone){
1313 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Flush shed clock failed %x", error);
1317 if (!CommandFinished(omx_clock,OMX_CommandFlush,omx_clock_output_port) ||
1318 !CommandFinished(omx_vid_sched,OMX_CommandFlush,omx_shed_clock_port)) {
1319 Log::getInstance()->log("Video", Log::DEBUG, "flush cmd clock shed failed");
1325 error = OMX_SendCommand(omx_vid_dec, OMX_CommandFlush,
1326 omx_codec_input_port, NULL);
1327 if (error != OMX_ErrorNone) {
1328 Log::getInstance()->log("Video", Log::DEBUG,
1329 "OMX_Flush codec out failed %x", error);
1336 DestroyInputBufsOMX();
1339 if (!DisablePort(omx_vid_sched,omx_shed_output_port,true)) {
1340 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 2 ");
1342 if (!DisablePort(omx_vid_rend,omx_rend_input_port,true)) {
1343 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 1");
1349 if (!DisablePort(omx_vid_dec,omx_codec_output_port,true)) {
1350 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 6");
1355 if (!DisablePort(omx_vid_dec,omx_codec_input_port,true)) {
1356 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 7");
1362 if (!DisablePort(omx_vid_sched,omx_shed_input_port,true)) {
1363 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 3");
1366 if (!DisablePort(omx_vid_sched,omx_shed_clock_port,true)) {
1367 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 4");
1370 if (!DisablePort(omx_clock,omx_clock_output_port,true)) {
1371 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 5");
1377 error=OMX_SetupTunnel(omx_vid_dec,omx_codec_output_port,NULL,NULL);
1378 if (error!=OMX_ErrorNone) {
1379 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1383 error=OMX_SetupTunnel(omx_vid_sched,omx_shed_input_port,NULL,NULL);
1384 if (error!=OMX_ErrorNone) {
1385 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1390 error=OMX_SetupTunnel(omx_vid_sched,omx_shed_output_port,NULL,NULL);
1391 if (error!=OMX_ErrorNone) {
1392 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1396 error=OMX_SetupTunnel(omx_vid_rend,omx_rend_input_port,NULL,NULL);
1397 if (error!=OMX_ErrorNone) {
1398 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1403 error=OMX_SetupTunnel(omx_clock,omx_clock_output_port,NULL,NULL);
1404 if (error!=OMX_ErrorNone) {
1405 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1409 error=OMX_SetupTunnel(omx_vid_sched,omx_shed_clock_port,NULL,NULL);
1410 if (error!=OMX_ErrorNone) {
1411 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1418 error=OMX_FreeHandle(omx_vid_dec);
1419 error=OMX_FreeHandle(omx_vid_sched);
1420 error=OMX_FreeHandle(omx_vid_rend);
1422 clock_mutex.Unlock();
1424 if (error!=OMX_ErrorNone) {
1425 Log::getInstance()->log("Video", Log::DEBUG, "FreeHandle failed %d", error);
1427 } else clock_mutex.Unlock();
1428 Log::getInstance()->log("Video", Log::DEBUG, "leave deallocate codecs OMX");
1434 void VideoOMX::destroyClock()
1437 if (clock_references>0) {
1439 if (clock_references==0) {
1440 OMX_ERRORTYPE error;
1441 Log::getInstance()->log("Video", Log::DEBUG, "destroy omx clock");
1442 error=OMX_FreeHandle(omx_clock);
1443 if (error!=OMX_ErrorNone) {
1444 Log::getInstance()->log("Video", Log::DEBUG, "FreeHandle failed %d", error);
1449 clock_mutex.Unlock();
1453 int VideoOMX::stop()
1455 if (!initted) return 0;
1458 //Check if libav mode
1459 DeAllocateCodecsOMX();
1464 // if (ioctl(fdVideo, AV_SET_VID_STOP, 0) != 0) return 0;
1468 int VideoOMX::reset()
1470 if (!initted) return 0;
1473 DeAllocateCodecsOMX();
1474 // if (ioctl(fdVideo, AV_SET_VID_RESET, 0x11) != 0) return 0;
1478 int VideoOMX::pause()
1480 if (!initted) return 0;
1481 Log::getInstance()->log("Video", Log::DEBUG, "enter pause");
1484 //maybe also change omx clock?
1485 pausetimecode=GetCurrentSystemTime();
1491 int VideoOMX::unPause() // FIXME get rid - same as play!! Not here!
1493 if (!initted) return 0;
1494 Log::getInstance()->log("Video", Log::DEBUG, "enter unpause");
1497 playbacktimeoffset+=GetCurrentSystemTime()-pausetimecode;
1498 paused=false; // may be also change omx clock
1504 int VideoOMX::fastForward()
1506 if (!initted) return 0;
1508 // if (ioctl(fdVideo, AV_SET_VID_libavWD, 1) != 0) return 0;
1512 int VideoOMX::unFastForward()
1514 if (!initted) return 0;
1516 // if (ioctl(fdVideo, AV_SET_VID_RESET, 0x11) != 0) return 0; // don't need this.
1518 //// if (ioctl(fdVideo, AV_SET_VID_PLAY, 0) != 0) return 0;
1522 int VideoOMX::attachFrameBuffer()
1524 if (!initted) return 0;
1526 // if (ioctl(fdVideo, AV_SET_VID_FB, 0) != 0) return 0;
1530 int VideoOMX::blank(void)
1532 // if (ioctl(fdVideo, AV_SET_VID_FB, 1) != 0) return 0;
1533 // if (ioctl(fdVideo, AV_SET_VID_FB, 0) != 0) return 0;
1537 ULLONG VideoOMX::getCurrentTimestamp()
1539 if (iframemode) return 0;
1540 return lastreftimePTS;
1545 ULONG VideoOMX::timecodeToFrameNumber(ULLONG timecode)
1547 if (format == PAL) return (ULONG)(((double)timecode / (double)90000) * (double)25);
1548 else return (ULONG)(((double)timecode / (double)90000) * (double)30);
1553 int VideoOMX::test()
1558 // return ioctl(fdVideo, AV_SET_VID_STC, &stc);
1565 int VideoOMX::test2()
1573 long long VideoOMX::SetStartOffset(long long curreftime, bool *rsync)
1577 startoffset=curreftime;//offset is set for audio
1579 offsetvideonotset=false;
1581 if (offsetvideonotset) {
1582 offsetvideonotset=false;
1585 if ( (curreftime-lastrefvideotime)>10000000LL
1586 || (curreftime-lastrefvideotime)<-10000000LL) {//if pts jumps to big resync
1587 startoffset+=curreftime-lastrefvideotime;
1588 lastrefaudiotime+=curreftime-lastrefvideotime;
1590 offsetaudionotset=true;
1597 lastrefvideotime=curreftime;
1603 long long VideoOMX::SetStartAudioOffset(long long curreftime, bool *rsync)
1607 startoffset=curreftime;
1609 offsetaudionotset=false;
1611 if (offsetaudionotset) {
1612 offsetaudionotset=false;
1615 if ( (curreftime-lastrefaudiotime)>10000000LL
1616 || (curreftime-lastrefaudiotime)<-10000000LL) {//if pts jumps to big resync
1617 startoffset+=curreftime-lastrefaudiotime;
1618 lastrefvideotime+=curreftime-lastrefaudiotime;
1620 offsetvideonotset=true;
1626 lastrefaudiotime=curreftime;
1631 void VideoOMX::ResetTimeOffsets() {
1632 offsetnotset=true; //called from demuxer
1633 offsetvideonotset=true;
1634 offsetaudionotset=true;
1642 long long VideoOMX::GetCurrentSystemTime()
1645 clock_gettime(CLOCK_MONOTONIC, &ts);
1646 return ts.tv_sec*10000000LL+ts.tv_nsec/100LL;
1649 void VideoOMX::WaitUntil(long long time)
1651 struct timespec interval;
1652 interval.tv_sec=time/10000000LL;
1653 interval.tv_nsec=(time %10000000LL)*100LL;
1654 while (clock_nanosleep(CLOCK_MONOTONIC,TIMER_ABSTIME,&interval,NULL)==EINTR) {
1655 //Log::getInstance()->log("Video", Log::DEBUG, "Wait until multi");
1660 bool VideoOMX::FrameSkip(long long pts)
1662 //ok first calculate the absolute time
1664 long long target_time=pts-playbacktimeoffset;
1665 // we have to wait untile the next frame
1666 long long offset=Demuxer::getInstance()->getFrameRate();
1667 if (offset==0) offset=25;
1668 offset=10000000LL/offset;
1669 target_time+=offset;
1670 long long current_time=GetCurrentSystemTime();
1672 if ((target_time-current_time)<-1000000LL) {
1673 skip=true; // we are too slow
1675 /* Log::getInstance()->log("Video", Log::DEBUG,
1676 "Skipping frames1 %lld %lld",target_time-current_time,pts);
1677 Log::getInstance()->log("Video", Log::DEBUG, "skip detail pts: %lld target: %lld sys: %lld off: %lld diff %lld",pts,target_time,current_time,offset,
1678 target_time-current_time);*/
1683 if ((target_time - current_time) < 0LL) {
1684 skip = true; // we are too slow
1686 /* Log::getInstance()->log("Video", Log::DEBUG,"Skipping frames2 %lld %lld",target_time-current_time,pts);
1687 Log::getInstance()->log("Video", Log::DEBUG, "skip detail pts: %lld target: %lld sys: %lld off: %lld diff %lld",pts,target_time,current_time,offset,
1688 target_time-current_time);*/
1698 void VideoOMX::FrameWaitforDisplay(long long pts)
1700 //ok first calculate the absolute time
1701 long long target_time=pts-playbacktimeoffset;
1702 // we have to wait untile the next frame
1703 long long offset=Demuxer::getInstance()->getFrameRate();
1704 long long current_time=GetCurrentSystemTime();
1705 if (offset==0) offset=25;
1706 offset=10000000LL/offset;
1707 target_time+=offset;
1708 if ((target_time-current_time)>1000000LL) target_time=current_time+1000000LL; // something is wrong do not wait too long
1709 //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,
1710 // target_time-current_time);
1712 WaitUntil(target_time);
1713 //Log::getInstance()->log("Video", Log::DEBUG, "Wait for display out %lld",GetCurrentSystemTime());
1716 void VideoOMX::AdjustAudioPTS(long long pts)
1718 long long newplaybacktimeoffset=pts-GetCurrentSystemTime();
1719 /* if ((newplaybacktimeoffset-1000000LL)>playbacktimeoffset
1720 || (newplaybacktimeoffset+1000000LL)<playbacktimeoffset) {
1721 Log::getInstance()->log("Video", Log::DEBUG, "Adjust Playbackoffsettime o: %lld n: %lld",
1722 playbacktimeoffset,newplaybacktimeoffset);*/
1723 playbacktimeoffset=newplaybacktimeoffset;
1728 void VideoOMX::threadPostStopCleanup()
1732 Log::getInstance()->log("Video", Log::DEBUG,
1737 void VideoOMX::threadMethod()
1739 Log::getInstance()->log("Video", Log::DEBUG,
1743 OMX_BUFFERHEADERTYPE* pict=NULL;
1746 input_bufs_omx_mutex.Lock();
1747 if (input_bufs_omx_present.size()>0) {
1749 pict=input_bufs_omx_present.front();
1750 time=input_time_present.front();
1751 input_bufs_omx_present.pop_front();
1752 input_time_present.pop_front();
1754 input_bufs_omx_mutex.Unlock();
1758 //Log::getInstance()->log("Video", Log::DEBUG,
1760 if (time!=0 &&FrameSkip(time) && !(pict->nFlags &OMX_BUFFERFLAG_STARTTIME)) {
1762 input_bufs_omx_mutex.Lock();
1763 input_bufs_omx_free.push_back(pict);
1764 input_bufs_omx_mutex.Unlock();
1767 OMX_ERRORTYPE error = ProtOMXEmptyThisBuffer(omx_vid_dec, pict);
1768 if (error != OMX_ErrorNone) {
1769 Log::getInstance()->log("Video", Log::DEBUG,
1770 "OMX_EmptyThisBuffer failed %x", error);
1772 if (time!=0) FrameWaitforDisplay(time);
1780 Log::getInstance()->log("Video", Log::DEBUG,
1784 void VideoOMX::PutBufferToPres(OMX_BUFFERHEADERTYPE* buffer, long long time)
1786 input_bufs_omx_mutex.Lock();
1787 input_bufs_omx_present.push_back(buffer);
1788 input_time_present.push_back(time);
1789 input_bufs_omx_mutex.Unlock();
1793 OMX_ERRORTYPE VideoOMX::ProtOMXEmptyThisBuffer(OMX_HANDLETYPE handle, OMX_BUFFERHEADERTYPE* buffer)
1795 // protect the call to empty this buffer
1798 pthread_testcancel();
1799 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldcancelstate);
1800 pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldcanceltype);
1802 OMX_ERRORTYPE ret_val;
1803 ret_val=OMX_EmptyThisBuffer(handle,buffer);
1804 clock_mutex.Unlock();
1805 pthread_setcancelstate(oldcancelstate, NULL);
1806 pthread_setcanceltype(oldcanceltype, NULL);
1807 pthread_testcancel();
1811 void VideoOMX::PrepareMediaSample(const MediaPacketList& mplist,UINT samplepos)
1813 mediapacket = mplist.front();
1816 UINT VideoOMX::DeliverMediaSample(UCHAR* buffer, UINT *samplepos)
1818 DeliverMediaPacket(mediapacket, buffer, samplepos);
1819 if (*samplepos == mediapacket.length) {
1826 UINT VideoOMX::DeliverMediaPacket(MediaPacket packet,
1827 const UCHAR* buffer,
1830 if (packet.type == MPTYPE_VIDEO_H264)
1840 //Later add fail back code for libav
1842 *samplepos+=packet.length;
1843 return packet.length;
1847 if (!omx_running) return 0; // if we are not runnig do not do this
1848 if (paused) return 0; //Block if we pause
1850 long long current_media_time=GetCurrentSystemTime()+playbacktimeoffset;
1851 /* if (packet.synched &&
1852 (packet.presentation_time<0 /*|| // preroll skip frames
1853 (packet.presentation_time+5000000LL)<(current_media_time)*)) { // we are late skip
1854 Log::getInstance()->log("Video", Log::DEBUG, "DeliverMediaPacketOMX Preroll or too late %lld %lld; %lld", packet.presentation_time,current_media_time,playbacktimeoffset);
1855 *samplepos=packet.length;
1856 return packet.length;
1859 OMX_ERRORTYPE error;
1861 /*First Check, if we have an video sample*/
1865 return 0; //Not in iframe mode!
1869 if (packet.disconti) {
1871 if (cur_input_buf_omx) {
1872 PutBufferToPres(cur_input_buf_omx, lastreftimeOMX);
1873 cur_input_buf_omx=NULL;
1877 /*Inspect PES-Header */
1879 // OMX_STATETYPE temp_state;
1880 // OMX_GetState(omx_vid_dec,&temp_state);
1882 if (*samplepos==0) {//stripheader
1883 headerstrip=buffer[packet.pos_buffer+8]+9/*is this right*/;
1884 // if (h264) Log::getInstance()->log("Video", Log::DEBUG, "PES info %x %x %x %x",
1885 // buffer[packet.pos_buffer+0],buffer[packet.pos_buffer+1],buffer[packet.pos_buffer+2],buffer[packet.pos_buffer+3]);
1886 *samplepos+=headerstrip;
1887 if ( packet.synched ) {
1888 if (cur_input_buf_omx) {
1889 cur_input_buf_omx->nFlags|=OMX_BUFFERFLAG_ENDOFFRAME;
1890 PutBufferToPres(cur_input_buf_omx, lastreftimeOMX);
1891 cur_input_buf_omx=NULL;//write out old data
1897 if (!firstsynched) {//
1898 *samplepos=packet.length;//if we have not processed at least one
1899 return packet.length;//synched packet ignore it!
1904 if (!cur_input_buf_omx) {
1905 input_bufs_omx_mutex.Lock();
1906 if (input_bufs_omx_free.size()==0) {
1907 input_bufs_omx_mutex.Unlock();
1908 //Log::getInstance()->log("Video", Log::DEBUG, "Deliver MediaPacket no free sample");
1909 return 0; // we do not have a free media sample
1912 cur_input_buf_omx=input_bufs_omx_free.front();
1913 cur_input_buf_omx->nFilledLen=0;
1914 cur_input_buf_omx->nOffset=0;
1915 cur_input_buf_omx->nTimeStamp=0;
1916 input_bufs_omx_free.pop_front();
1917 input_bufs_omx_mutex.Unlock();
1923 if (cur_input_buf_omx->nFilledLen==0) {//will only be changed on first packet
1924 if (packet.synched) {
1925 // Log::getInstance()->log("Video", Log::DEBUG, "packet synched marker");
1927 //lastreftimePTS=packet.pts;
1928 if (omx_first_frame) { // TODO time
1929 cur_input_buf_omx->nFlags=OMX_BUFFERFLAG_STARTTIME;
1930 Log::getInstance()->log("Video", Log::DEBUG, "Starttime");
1931 omx_first_frame=false;
1933 //cur_input_buf_omx->nFlags=0;
1934 cur_input_buf_omx->nFlags|=OMX_BUFFERFLAG_TIME_UNKNOWN;
1936 lastreftimeOMX=packet.presentation_time;
1937 // Log::getInstance()->log("Video", Log::DEBUG, "Time code %lld pts %lld", lastreftimeOMX,packet.pts);
1938 lastreftimePTS=packet.pts;
1939 cur_input_buf_omx->nTimeStamp=0;//lastreftimeOMX; // the clock component is faulty;
1943 cur_input_buf_omx->nFlags=OMX_BUFFERFLAG_TIME_UNKNOWN;
1944 cur_input_buf_omx->nTimeStamp=0;
1947 // ms->SetSyncPoint(TRUE);
1949 if (packet.disconti) cur_input_buf_omx->nFlags|=OMX_BUFFERFLAG_DISCONTINUITY;
1954 unsigned int haveToCopy=packet.length-*samplepos;
1956 while (haveToCopy> (cur_input_buf_omx->nAllocLen-cur_input_buf_omx->nFilledLen)) {
1957 unsigned int cancopy=cur_input_buf_omx->nAllocLen-cur_input_buf_omx->nFilledLen;
1958 memcpy(cur_input_buf_omx->pBuffer+cur_input_buf_omx->nFilledLen,buffer+packet.pos_buffer+*samplepos,cancopy);
1959 haveToCopy-=cancopy;
1960 cur_input_buf_omx->nFilledLen+=cancopy;
1961 *samplepos+=cancopy;
1962 // push old buffer out
1964 PutBufferToPres(cur_input_buf_omx, 0LL);
1966 input_bufs_omx_mutex.Lock();
1967 if (input_bufs_omx_free.size()==0) {
1968 input_bufs_omx_mutex.Unlock();
1969 //Log::getInstance()->log("Video", Log::DEBUG, "Deliver MediaPacket no free sample");
1970 return *samplepos; // we do not have a free media sample
1972 cur_input_buf_omx=input_bufs_omx_free.front();
1973 cur_input_buf_omx->nFilledLen=0;
1974 cur_input_buf_omx->nOffset=0;
1975 cur_input_buf_omx->nTimeStamp=0;
1976 input_bufs_omx_free.pop_front();
1977 input_bufs_omx_mutex.Unlock();
1979 cur_input_buf_omx->nFlags=OMX_BUFFERFLAG_TIME_UNKNOWN;
1982 memcpy(cur_input_buf_omx->pBuffer+cur_input_buf_omx->nFilledLen,
1983 buffer+packet.pos_buffer+*samplepos,haveToCopy);
1984 cur_input_buf_omx->nFilledLen+=haveToCopy;
1988 *samplepos+=haveToCopy;
1997 bool VideoOMX::displayIFrame(const UCHAR* buffer, UINT length) {
1998 if (!omx_running) return false;
2000 EnterIframePlayback();
2002 int haveToCopy = length;
2004 if (!cur_input_buf_omx) {
2005 input_bufs_omx_mutex.Lock();
2006 if (input_bufs_omx_free.size() == 0) {
2007 input_bufs_omx_mutex.Unlock();
2008 // Log::getInstance()->log("Video", Log::DEBUG,
2009 // "Deliver MediaPacket no free sample");
2010 return false; // we do not have a free media sample
2013 cur_input_buf_omx = input_bufs_omx_free.front();
2014 cur_input_buf_omx->nFilledLen = 0;
2015 cur_input_buf_omx->nOffset = 0;
2016 cur_input_buf_omx->nTimeStamp = 0;
2017 input_bufs_omx_free.pop_front();
2018 input_bufs_omx_mutex.Unlock();
2022 unsigned int pattern, packet_length;
2023 unsigned int headerstrip = 0;
2028 //Now we strip the pes header
2029 pattern = (buffer[0] << 16) | (buffer[1] << 8) | (buffer[2]);
2030 while (read_pos + 7 <= length) {
2031 pattern = ((pattern << 8) & 0xFFFFFFFF) | buffer[read_pos + 3];
2032 if (pattern < 0x000001E0 || pattern > 0x000001EF) {
2036 headerstrip = buffer[read_pos + 8] + 9/*is this right*/;
2037 packet_length = ((buffer[read_pos + 4] << 8)
2038 | (buffer[read_pos + 5])) + 6;
2039 if (read_pos + packet_length > length)
2042 if ((headerstrip < packet_length)
2043 && (cur_input_buf_omx->nFilledLen + packet_length
2044 - headerstrip) > cur_input_buf_omx->nAllocLen) {
2046 cur_input_buf_omx->nFlags = OMX_BUFFERFLAG_STARTTIME;
2049 cur_input_buf_omx->nFlags
2050 |= OMX_BUFFERFLAG_TIME_UNKNOWN;
2053 cur_input_buf_omx->nTimeStamp = 0;
2054 PutBufferToPres(cur_input_buf_omx, 0);
2055 cur_input_buf_omx = NULL;
2057 if (!cur_input_buf_omx) {
2059 while (count < 100 && omx_running && iframemode) {
2062 input_bufs_omx_mutex.Lock();
2063 if (input_bufs_omx_free.size() == 0) {
2064 input_bufs_omx_mutex.Unlock();
2065 // Log::getInstance()->log("Video", Log::DEBUG,
2066 // "Ifrane no free sample");
2068 if (!omx_running) return false;
2071 cur_input_buf_omx = input_bufs_omx_free.front();
2072 cur_input_buf_omx->nFilledLen = 0;
2073 cur_input_buf_omx->nOffset = 0;
2074 cur_input_buf_omx->nTimeStamp = 0;
2075 input_bufs_omx_free.pop_front();
2076 input_bufs_omx_mutex.Unlock();
2079 if (!cur_input_buf_omx)
2084 if (packet_length > headerstrip) {
2086 cur_input_buf_omx->pBuffer
2087 + cur_input_buf_omx->nFilledLen,
2088 buffer + read_pos + headerstrip,
2089 packet_length - headerstrip);
2090 cur_input_buf_omx->nFilledLen += packet_length
2093 read_pos += packet_length;
2095 pattern = (buffer[read_pos] << 16)
2096 | (buffer[read_pos + 1] << 8) | (buffer[read_pos + 2]);
2102 cur_input_buf_omx->nFlags = OMX_BUFFERFLAG_STARTTIME;
2105 cur_input_buf_omx->nFlags |= OMX_BUFFERFLAG_TIME_UNKNOWN;
2108 cur_input_buf_omx->nTimeStamp = 0;
2109 PutBufferToPres(cur_input_buf_omx, 0);
2110 cur_input_buf_omx = NULL;
2113 MILLISLEEP(40); //Block a bit
2117 int VideoOMX::EnterIframePlayback()
2120 if (cur_input_buf_omx) {
2121 PutBufferToPres(cur_input_buf_omx, lastreftimeOMX);
2123 cur_input_buf_omx = NULL;
2125 clock_mutex.Unlock();