2 Copyright 2004-2005 Chris Tallon, 2009-12 Marten Richter
4 This file is part of VOMP.
6 VOMP is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 VOMP is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with VOMP. If not, see <https://www.gnu.org/licenses/>.
29 #include "woptionpane.h"
30 #include "osdopenvg.h"
37 //A lot of parts of this file are heavily inspired by xbmc omx implementations
39 static const char* TAG = "VideoOMX";
41 VideoOMX::VideoOMX() {
42 logger = LogNT::getInstance();
47 cur_input_buf_omx = NULL;
48 omx_h264 = omx_mpeg2 = true;
51 omx_vid_stalled = false;
54 offsetvideonotset = true;
55 offsetaudionotset = true;
63 mpeg2_supported=false;
68 deinterlace=2; //advanced
73 strcpy(L_VPE_OMX_CLOCK, VPE_OMX_CLOCK);
74 strcpy(L_VPE_OMX_H264_DECODER, VPE_OMX_H264_DECODER);
75 strcpy(L_VPE_OMX_MPEG2_DECODER, VPE_OMX_MPEG2_DECODER);
76 strcpy(L_VPE_OMX_VIDEO_SCHED, VPE_OMX_VIDEO_SCHED);
77 strcpy(L_VPE_OMX_VIDEO_REND, VPE_OMX_VIDEO_REND);
78 strcpy(L_VPE_OMX_VIDEO_DEINTERLACE, VPE_OMX_VIDEO_DEINTERLACE);
86 int VideoOMX::init(UCHAR tformat)
88 if (initted) return 0;
91 // libcec calls bcm_host_init() - but in case CEC is disabled call it here as well.
92 // Seems safe to call it more than once.
95 int ret=vc_gencmd_send("codec_enabled MPG2");
97 logger->debug(TAG, "vc_gencmd_send failed {:#x}",ret);
100 ret=vc_gencmd_read_response(buffer,sizeof(buffer));
102 logger->debug(TAG, "vc_gencmd_read_response failed {:#x}",ret);
104 if (STRCASECMP(buffer,"MPG2=enabled")==0) {
105 mpeg2_supported=true;
106 } else if (STRCASECMP(buffer,"MPG2=disabled")==0) {
107 mpeg2_supported=false;
109 logger->debug(TAG, "Undefined mpg codec answer {}",buffer);
114 if (!setFormat(tformat)) { shutdown(); return 0; }
115 if (!setConnection(HDMI)) { shutdown(); return 0; }
116 if (!setAspectRatio(ASPECT4X3,12,11)) { shutdown(); return 0; }
117 if (!setMode(NORMAL)) { shutdown(); return 0; }
118 if (!setSource()) { shutdown(); return 0; }
119 if (!attachFrameBuffer()) { shutdown(); return 0; }
121 setTVsize(ASPECT16X9);
127 if (error != OMX_ErrorNone) {
128 logger->debug(TAG, "Init OMX failed {:#x}",
147 OMX_ERRORTYPE VideoOMX::EventHandler_OMX(OMX_IN OMX_HANDLETYPE handle,OMX_IN OMX_PTR appdata,
148 OMX_IN OMX_EVENTTYPE event_type,OMX_IN OMX_U32 data1,
149 OMX_IN OMX_U32 data2,OMX_IN OMX_PTR event_data) {
151 //LogNT::getInstance()->info(TAG, "eventHandler {:#x} {:#x} {:#x} {:#x} {:#x}",handle,event_type,data1,data2,event_data);
153 struct VPE_OMX_EVENT new_event;
154 new_event.handle=handle;
155 new_event.appdata=appdata;
156 new_event.event_type=event_type;
157 new_event.data1=data1;
158 new_event.data2=data2;
159 new_event.event_data=event_data;
161 VideoOMX* video = static_cast<VideoOMX *>(Video::getInstance());
162 video->AddOmxEvent(new_event);
164 /* switch (event_type) {
165 case OMX_EventCmdComplete: {
170 return OMX_ErrorNone;
174 void VideoOMX::signalOmx()
177 * Getting rid of Signal class. It looks like VideoOMX uses a wait-on-condition-variable in WaitForEvent()
178 * and CommandFinished(). These methods both use timed waits and don't use exact thread synchronisation -
179 * i.e. a caught signal will end the wait early but a missed signal doesn't matter. So, I'm just copying
180 * in what the Signal class used to do here and I'll sort it out later.
181 * Q: Are the found places the only synchronisation points? Would it be possible to change this to use
182 * exact sychronisation and remove the wait spin loops? Unknown.
184 * This omx_event_mutex - is this exactly locking the very thing the condition variable is being used
185 * for? i.e. is omx_event_mutex really the mutex that should be being used with the cond var?
187 * Callers of signalOmx:
189 * VideoOMX::AddOmxEvent, VideoOMX::ReturnEmptyOMXBuffer
190 * ImageOMX::ReturnEmptyOMXBuffer, ImageOMX::ReturnFillOMXBuffer
191 * AudioOMX::ReturnEmptyOMXBuffer, AudioOMX::FillBufferDone_OMX
193 * Surprise: WaitForEvent isn't a long running loop while video is playing.
196 omx_event_ready_signal_mutex.lock();
197 omx_event_ready_signal.notify_one(); // Signal called pthread_cond_signal - unblock one
198 omx_event_ready_signal_mutex.unlock();
201 void VideoOMX::AddOmxEvent(VPE_OMX_EVENT new_event)
203 omx_event_mutex.lock();
204 omx_events.push_back(new_event);
205 omx_event_mutex.unlock();
210 OMX_ERRORTYPE VideoOMX::EmptyBufferDone_OMX(OMX_IN OMX_HANDLETYPE hcomp,OMX_IN OMX_PTR appdata,OMX_IN OMX_BUFFERHEADERTYPE* buffer){
212 // LogNT::getInstance()->info(TAG, "EmptyBufferDone");
213 VideoOMX* video = static_cast<VideoOMX *>(Video::getInstance());
214 /* long long temp =buffer->nTimeStamp.nLowPart
215 | ((long long) buffer->nTimeStamp.nHighPart << 32);
216 LogNT::getInstance()->info(TAG, "EBD Video %lld {:#x}",temp,buffer->nFlags);*/
217 video->ReturnEmptyOMXBuffer(buffer);
218 return OMX_ErrorNone;
222 void VideoOMX::ReturnEmptyOMXBuffer(OMX_BUFFERHEADERTYPE* buffer){
223 input_bufs_omx_mutex.lock();
224 //LogNT::getInstance()->info(TAG, "ReturnEmptyOMXBuffer {} {}",input_bufs_omx_free.size(),input_bufs_omx_all.size());
225 input_bufs_omx_free.push_back(buffer);
226 //LogNT::getInstance()->info(TAG, "ReturnEmptyOMXBuffer {}",input_bufs_omx_free.size());
227 input_bufs_omx_mutex.unlock();
232 OMX_ERRORTYPE VideoOMX::FillBufferDone_OMX(OMX_IN OMX_HANDLETYPE hcomp, OMX_IN OMX_PTR appdata,OMX_IN OMX_BUFFERHEADERTYPE* buffer) {
233 //LogNT::getInstance()->info(TAG, "FillBufferDone");
234 return OMX_ErrorNone;
239 int VideoOMX::shutdown()
241 if (!initted) return 0;
243 LogNT::getInstance()->info(TAG, "Shutdown video module");
245 DeAllocateCodecsOMX();
247 //vc_tv_show_info(0); // back to console
250 struct fb_var_screeninfo screeninfo;
251 fd_fbset=open("/dev/fb0",O_RDONLY);
253 LogNT::getInstance()->crit(TAG, "Could not open frame buffer device {}", fd_fbset);
256 if (ioctl(fd_fbset, FBIOGET_VSCREENINFO, &screeninfo)){
258 LogNT::getInstance()->crit(TAG, "Could not FBIOGET_VSCREENINFO frame buffer device");
261 screeninfo.bits_per_pixel=8;
262 if (ioctl(fd_fbset, FBIOPUT_VSCREENINFO, &screeninfo)){
263 LogNT::getInstance()->crit(TAG, "Could not FBIOPUT_VSCREENINFO frame buffer device");
265 screeninfo.bits_per_pixel=16;
266 if (ioctl(fd_fbset, FBIOPUT_VSCREENINFO, &screeninfo)){
267 LogNT::getInstance()->crit(TAG, "Could not FBIOPUT_VSCREENINFO frame buffer device");
275 bool VideoOMX::loadOptionsFromServer(VDR* vdr)
277 logger->debug(TAG, "VideoOMX config load");
278 char *name=vdr->configLoad("VideoOMX","SDDeinterlacing");
281 if (STRCASECMP(name, "None") == 0) {
283 }/* else if (STRCASECMP(name, "LineDouble") == 0) {
285 }*/ else if (STRCASECMP(name, "Advanced") == 0) {
287 } /*else if (STRCASECMP(name, "Crazy") == 0) {
288 deinterlace = 3; // this does not activate deinterlacing but a image filter, just for fun
289 }*/ else if (STRCASECMP(name, "Fast") == 0) {
292 logger->debug(TAG, "Set deinterlacing to {} {}",name,deinterlace);
300 bool VideoOMX::handleOptionChanges(Option* option)
302 if (Video::handleOptionChanges(option))
304 switch (option->id) {
306 if (STRCASECMP(option->options[option->userSetChoice], "None") == 0) {
308 } /*else if (STRCASECMP(option->options[option->userSetChoice], "LineDouble")
311 }*/ else if (STRCASECMP(option->options[option->userSetChoice], "Advanced")
314 } /*else if (STRCASECMP(option->options[option->userSetChoice], "Crazy")
317 }*/ else if (STRCASECMP(option->options[option->userSetChoice], "Fast")
321 logger->debug(TAG, "Set deinterlacing to {} {}",option->options[option->userSetChoice],deinterlace);
330 bool VideoOMX::saveOptionstoServer()
333 switch (deinterlace) {
335 VDR::getInstance()->configSave("VideoOMX","SDDeinterlacing", "None");
338 VDR::getInstance()->configSave("VideoOMX","SDDeinterlacing", "LineDouble");
341 VDR::getInstance()->configSave("VideoOMX","SDDeinterlacing", "Advanced");
344 VDR::getInstance()->configSave("VideoOMX","SDDeinterlacing", "Crazy");
347 VDR::getInstance()->configSave("VideoOMX", "SDDeinterlacing", "Fast");
354 /*Option(UINT id, const char* displayText, const char* configSection, const char* configKey, UINT optionType,
355 UINT numChoices, UINT defaultChoice, UINT startInt,
356 const char * const * options, const char * const * optionkeys = NULL, AbstractOption* handler=NULL);*/
358 bool VideoOMX::addOptionsToPanes(int panenumber,Options *options,WOptionPane* pane)
360 if (!Video::addOptionsToPanes(panenumber,options,pane)) return false;
366 static const char* deinterlaceopts[] = { "None", "Fast",/*"LineDouble",*/"Advanced"/*,"Crazy"*/ };
367 option = new Option(1,tr("SD Deinterlacing"), "VideoOMX","SDDeinterlacing",Option::TYPE_TEXT,/*4,2*/3,2,0,deinterlaceopts,NULL,false,this);
368 options->push_back(option);
369 pane->addOptionLine(option);
377 int VideoOMX::setTVsize(UCHAR ttvsize)
379 if (tvsize!=ttvsize) pendingmodechange=true;
384 UCHAR VideoOMX::getTVsize() {
386 return ASPECT16X9; // in order that aspect ratio changes are reported
390 void VideoOMX::executePendingModeChanges()
392 if (pendingmodechange) {
393 LogNT::getInstance()->info(TAG, "Execute pending mode change");
394 Osd::getInstance()->shutdown();
396 Osd::getInstance()->restore();
397 Osd::getInstance()->init();
398 BoxStack::getInstance()->redrawAllBoxes();
403 int VideoOMX::setDefaultAspect()
405 return setAspectRatio(tvsize,parx,pary);
410 int VideoOMX::setFormat(UCHAR tformat)
412 if (!initted) return 0;
413 if ((tformat != PAL) && (tformat != NTSC)
414 && (tformat != PAL_M) && (tformat != NTSC_J)) return 0;
424 // selectVideoMode(0);
429 void VideoOMX::selectVideoMode(int interlaced)
431 TV_GET_STATE_RESP_T tvstate;
432 vc_tv_get_state(&tvstate);
434 if ((tvstate.state & VC_HDMI_UNPLUGGED)) {
436 LogNT::getInstance()->info(TAG, "HDMI unplugged");
439 LogNT::getInstance()->info(TAG, "HDMI plugged");
440 if (connection==COMPOSITERGB) {
442 LogNT::getInstance()->info(TAG, "SDTV set");
445 LogNT::getInstance()->info(TAG, "HDMI set");
451 TV_SUPPORTED_MODE_T all_supp_modes[200];
452 HDMI_RES_GROUP_T pref_group;
453 TV_SUPPORTED_MODE_T *mymode=NULL;
454 TV_SUPPORTED_MODE_T *mymode_second_best=NULL;
455 // bool got_optimum=false;
457 HDMI_RES_GROUP_T group=HDMI_RES_GROUP_CEA;
458 int all_my_modes=vc_tv_hdmi_get_supported_modes(HDMI_RES_GROUP_CEA,
460 &pref_group,&pref_mode);
461 if (all_my_modes<=0) {
462 group=HDMI_RES_GROUP_DMT;
463 all_my_modes=vc_tv_hdmi_get_supported_modes(HDMI_RES_GROUP_DMT,
465 &pref_group,&pref_mode);
466 LogNT::getInstance()->info(TAG, "No CEA fall back to DMT modes ");
473 if (format==PAL)target_fps=50;
474 else if (format==NTSC) target_fps=60;
476 //Now first determine native resolution
477 int native_width=1920;
478 int native_height=1080;
479 for (int i=0;i<all_my_modes;i++) {
480 if (all_supp_modes[i].native) {
481 mymode=all_supp_modes+i;
482 LogNT::getInstance()->info(TAG, "Found native mode {}x{} {} Hz i: {}",
483 mymode->width,mymode->height,mymode->frame_rate,mymode->scan_mode);
484 native_width=mymode->width;
485 native_height=mymode->height;
489 //Now find the mode which matches best
490 for (int i=0;i<all_my_modes;i++) {
491 TV_SUPPORTED_MODE_T *curmode=all_supp_modes+i;
492 if (curmode->width==native_width &&
493 curmode->height==native_height &&
494 curmode->frame_rate==target_fps) {
495 if(curmode->scan_mode==interlaced) {
498 LogNT::getInstance()->info(TAG, "Found optimum mode {}x{} {} Hz i: {}",
499 mymode->width,mymode->height,mymode->frame_rate,mymode->scan_mode);
501 mymode_second_best=curmode;
502 LogNT::getInstance()->info(TAG, "Found close to optimum mode {}x{} {} Hz i: {}",
503 mymode_second_best->width,mymode_second_best->height,
504 mymode_second_best->frame_rate,mymode_second_best->scan_mode);
509 // InputMan::getInstance()->shutdown(); FIXME FIXME FIXME - disabling this temp, why does this have to run?
512 LogNT::getInstance()->info(TAG, "Switch to optimum mode");
513 vc_tv_hdmi_power_on_explicit(HDMI_MODE_HDMI,group,mymode->code);
514 } else if (mymode_second_best) {
515 LogNT::getInstance()->info(TAG, "Switch to close to optimum mode");
516 vc_tv_hdmi_power_on_explicit(HDMI_MODE_HDMI,group,mymode_second_best->code);
518 LogNT::getInstance()->info(TAG, "Switch to prefered mode");
519 vc_tv_hdmi_power_on_best(1920, 1080, target_fps, interlaced ? HDMI_INTERLACED : HDMI_NONINTERLACED,
520 static_cast<EDID_MODE_MATCH_FLAG_T>(HDMI_MODE_MATCH_FRAMERATE|HDMI_MODE_MATCH_RESOLUTION|HDMI_MODE_MATCH_SCANMODE));
523 outputinterlaced=interlaced;
526 LogNT::getInstance()->info(TAG, "Analog tv case");
527 // InputMan::getInstance()->shutdown(); FIXME FIXME FIXME - disabling this temp, why does this have to run? vc_tv_power_off();
528 SDTV_MODE_T setmode=SDTV_MODE_PAL;
529 SDTV_OPTIONS_T options;
534 LogNT::getInstance()->info(TAG, "SDTV aspect 16:9");
535 options.aspect=SDTV_ASPECT_16_9; break;
537 LogNT::getInstance()->info(TAG, "SDTV aspect 4:3");
538 options.aspect=SDTV_ASPECT_4_3; break;
540 LogNT::getInstance()->info(TAG, "SDTV aspect 14:9");
541 options.aspect=SDTV_ASPECT_14_9; break;
544 if (format==PAL) setmode=SDTV_MODE_PAL;
545 else if (format==NTSC) setmode=SDTV_MODE_NTSC;
546 else if (format==PAL_M)setmode=SDTV_MODE_PAL_M;
547 else if (format==NTSC_J) setmode=SDTV_MODE_NTSC_J;
548 vc_tv_sdtv_power_on(setmode,&options);
552 // InputMan::getInstance()->init(); // FIXME complete shutdown and reinit maybe heavy handed. FIXME FIXME FIXME - disabled temp
553 // If this was just to reinit CEC then funcitons should be made to do that
557 pendingmodechange=false;
562 int VideoOMX::setConnection(UCHAR tconnection)
564 if (!initted) return 0;
565 if ((tconnection != COMPOSITERGB) && (tconnection != HDMI)) return 0;
566 if (connection!=tconnection) pendingmodechange=true;
567 connection = tconnection;
569 // if (ioctl(fdVideo, AV_SET_VID_OUTPUT, connection) != 0) return 0;
573 int VideoOMX::setAspectRatio(UCHAR taspectRatio, int tparx,int tpary)
575 if (!initted) return 0;
576 //if ((taspectRatio != ASPECT4X3) && (taspectRatio != ASPECT16X9)) return 0;
577 aspectRatio = taspectRatio;
581 logger->debug(TAG, "Setting aspect to {}: PAR {} {}", aspectRatio,parx,pary);
584 // if (ioctl(fdVideo, AV_SET_VID_RATIO, aspectRatio) != 0) return 0;
588 int VideoOMX::setMode(UCHAR tmode)
590 if (!initted) return 0;
591 if (tmode==LETTERBOX || tmode==NORMAL) mode=tmode;
596 bool VideoOMX::setVideoDisplay(VideoDisplay display)
598 if (!initted) return false;
599 switch (display.mode)
601 case None: return true; //??
608 xpos = ((float) display.x) / ((float) screenWidth);
609 ypos = ((float) display.y) / ((float) screenHeight);
616 xpos = ((float) display.x) / ((float) screenWidth);
617 ypos = ((float) display.y) / ((float) screenHeight);
624 xpos = ((float) display.x) / ((float) screenWidth);
625 ypos = ((float) display.y) / ((float) screenHeight);
626 width = ((float) display.width) / ((float) screenWidth);
627 height = ((float) display.height) / ((float) screenHeight);
635 void VideoOMX::updateMode()
641 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldcancelstate);
642 pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldcanceltype);
644 OMX_CONFIG_DISPLAYREGIONTYPE dispconf;
645 memset(&dispconf, 0, sizeof(dispconf));
646 dispconf.nSize = sizeof(dispconf);
647 dispconf.nVersion.nVersion = OMX_VERSION;
648 dispconf.nPortIndex = omx_rend_input_port;
650 dispconf.set = OMX_DISPLAY_SET_LAYER;
651 error = OMX_SetParameter(omx_vid_rend, OMX_IndexConfigDisplayRegion,
653 if (error != OMX_ErrorNone) {
654 LogNT::getInstance()->debug(TAG,
655 "Set OMX_IndexConfigDisplayRegion1 failed {:#x}", error);
656 pthread_setcancelstate(oldcancelstate, NULL);
657 pthread_setcanceltype(oldcanceltype, NULL);
658 clock_mutex.unlock();
663 dispconf.pixel_x =parx;
664 dispconf.pixel_y=pary;
665 dispconf.set = OMX_DISPLAY_SET_PIXEL;
666 error = OMX_SetParameter(omx_vid_rend, OMX_IndexConfigDisplayRegion,
668 if (error != OMX_ErrorNone) {
669 LogNT::getInstance()->debug(TAG,
670 "Set OMX_IndexConfigDisplayRegion5 failed {:#x}", error);
671 pthread_setcancelstate(oldcancelstate, NULL);
672 pthread_setcanceltype(oldcanceltype, NULL);
673 clock_mutex.unlock();
679 dispconf.set = OMX_DISPLAY_SET_FULLSCREEN;
682 dispconf.fullscreen = OMX_TRUE;
684 dispconf.fullscreen = OMX_FALSE;
686 error = OMX_SetParameter(omx_vid_rend, OMX_IndexConfigDisplayRegion,
688 if (error != OMX_ErrorNone) {
689 LogNT::getInstance()->debug(TAG,
690 "Set OMX_IndexConfigDisplayRegion2 failed {:#x}", error);
691 pthread_setcancelstate(oldcancelstate, NULL);
692 pthread_setcanceltype(oldcanceltype, NULL);
693 clock_mutex.unlock();
697 dispconf.set = OMX_DISPLAY_SET_MODE;
699 dispconf.mode = (mode == NORMAL) ? OMX_DISPLAY_MODE_FILL
700 : OMX_DISPLAY_MODE_LETTERBOX;
702 dispconf.mode = OMX_DISPLAY_MODE_LETTERBOX;
704 error = OMX_SetParameter(omx_vid_rend, OMX_IndexConfigDisplayRegion,
706 if (error != OMX_ErrorNone) {
707 LogNT::getInstance()->debug(TAG,
708 "Set OMX_IndexConfigDisplayRegion3 failed {:#x}", error);
709 pthread_setcancelstate(oldcancelstate, NULL);
710 pthread_setcanceltype(oldcanceltype, NULL);
711 clock_mutex.unlock();
716 unsigned int display_width, display_height;
717 display_width = display_height = 0;
718 if (graphics_get_display_size(0, &display_width, &display_height)
720 LogNT::getInstance()->warn(TAG,
721 "Getting display size failed! (BCM API) ");
722 pthread_setcancelstate(oldcancelstate, NULL);
723 pthread_setcanceltype(oldcanceltype, NULL);
724 clock_mutex.unlock();
727 //UnSetFullscreen with window
728 dispconf.set = OMX_DISPLAY_SET_DEST_RECT;
729 dispconf.dest_rect.x_offset
730 = (int) (xpos * ((float) display_width));
731 dispconf.dest_rect.y_offset = (int) (ypos
732 * ((float) display_height));
733 dispconf.dest_rect.width = (int) (width * ((float) display_width));
734 dispconf.dest_rect.height = (int) (height * ((float) display_height));
735 LogNT::getInstance()->debug(TAG,
736 "Set dest_rect as {} {} {} {}", dispconf.dest_rect.x_offset,dispconf.dest_rect.y_offset,
737 dispconf.dest_rect.width , dispconf.dest_rect.height);
739 error = OMX_SetParameter(omx_vid_rend,
740 OMX_IndexConfigDisplayRegion, &dispconf);
741 if (error != OMX_ErrorNone) {
742 LogNT::getInstance()->debug(TAG,
743 "Set OMX_IndexConfigDisplayRegion failed {:#x}", error);
744 pthread_setcancelstate(oldcancelstate, NULL);
745 pthread_setcanceltype(oldcanceltype, NULL);
746 clock_mutex.unlock();
750 pthread_setcancelstate(oldcancelstate, NULL);
751 pthread_setcanceltype(oldcanceltype, NULL);
754 clock_mutex.unlock();
757 int VideoOMX::signalOff()
760 LogNT::getInstance()->info(TAG, "signalOff");
761 Osd::getInstance()->stopUpdate(); // turn off drawing thread
762 InputMan::getInstance()->shutdown();
764 InputMan::getInstance()->init(); // FIXME
769 int VideoOMX::signalOn()
772 Osd::getInstance()->shutdown();
773 LogNT::getInstance()->info(TAG, "signalOn");
775 Osd::getInstance()->restore();
776 Osd::getInstance()->init();
777 BoxStack::getInstance()->redrawAllBoxes();
784 int VideoOMX::setSource()
786 if (!initted) return 0;
788 // What does this do...
789 // if (ioctl(fdVideo, AV_SET_VID_SRC, 1) != 0) return 0;
793 int VideoOMX::setPosition(int x, int y) {
796 xpos = ((float) x*2.f) / ((float) screenWidth);
797 ypos = ((float) y*2.f) / ((float) screenHeight);
805 if (!initted) return 0;
807 // if (ioctl(fdVideo, AV_SET_VID_SYNC, 2) != 0) return 0;
811 void VideoOMX::interlaceSwitch4Demux() {
813 Demuxer *demux=Demuxer::getInstance();
815 if (hdmi) { // only switch if hdmi and HD or interlaced SD material
819 int set_interlaced=0;
820 if (demux->getHorizontalSize()>720 && demux->getInterlaced()) {
823 LogNT::getInstance()->info(TAG, "switch interlacing {} {} {}",demux->getInterlaced(),outputinterlaced,set_interlaced);
824 if (outputinterlaced!=set_interlaced) {
825 selectVideoMode(set_interlaced);
826 Osd::getInstance()->shutdown();
827 Osd::getInstance()->restore();
828 Osd::getInstance()->init();
829 BoxStack::getInstance()->redrawAllBoxes();
841 int VideoOMX::play() {
845 logger->debug(TAG, "enter play");
847 interlaceSwitch4Demux();
849 if (AllocateCodecsOMX()) {
851 // Otherwise fall back to libav
855 LogNT::getInstance()->info(TAG,
856 "Allocate Codecs OMX failed assume h264 unsupported");
859 LogNT::getInstance()->info(TAG,
860 "Allocate Codecs OMX failed assume mpeg2 unsupported");
869 int VideoOMX::initClock()
873 if (clock_references==0)
876 static OMX_CALLBACKTYPE callbacks= {&EventHandler_OMX,&EmptyBufferDone_OMX,&FillBufferDone_OMX};
877 omx_event_mutex.lock();
879 omx_event_mutex.unlock();
881 error=OMX_GetHandle(&omx_clock,L_VPE_OMX_CLOCK,NULL,&callbacks);
883 if (error!=OMX_ErrorNone) {
884 logger->debug(TAG, "Init OMX clock failed {:#x}", error);
885 clock_mutex.unlock();
886 DeAllocateCodecsOMX();
890 /* TODO Clock config to separate method */
891 OMX_PORT_PARAM_TYPE p_param;
892 memset(&p_param,0,sizeof(p_param));
893 p_param.nSize=sizeof(p_param);
894 p_param.nVersion.nVersion=OMX_VERSION;
895 error=OMX_GetParameter(omx_clock,OMX_IndexParamOtherInit,&p_param);
896 if (error!=OMX_ErrorNone) {
897 logger->debug(TAG, "Init clock OMX_GetParameter failed {:#x}", error);
898 clock_mutex.unlock();
899 DeAllocateCodecsOMX();
902 omx_clock_output_port=p_param.nStartPortNumber;
904 for (unsigned int i=0;i<p_param.nPorts;i++) {
905 if (!DisablePort(omx_clock,p_param.nStartPortNumber+i,true) ) {
906 logger->debug(TAG, "Disable Ports OMX clock failed {}",i);
907 clock_mutex.unlock();
908 DeAllocateCodecsOMX();
917 logger->debug(TAG, "init omx clock {:p} {}", (void*)this, omx_clock);
919 clock_mutex.unlock();
923 int VideoOMX::getClockAudioandInit(OMX_HANDLETYPE *p_omx_clock,OMX_U32 *p_omx_clock_output_port)
927 *p_omx_clock_output_port=0;
934 OMX_TIME_CONFIG_ACTIVEREFCLOCKTYPE refclock;
935 memset(&refclock,0,sizeof(refclock));
936 refclock.nSize=sizeof(refclock);
937 refclock.nVersion.nVersion=OMX_VERSION;
939 refclock.eClock=OMX_TIME_RefClockAudio;
941 //refclock.eClock=OMX_TIME_RefClockVideo;
942 error=OMX_SetConfig(omx_clock,OMX_IndexConfigTimeActiveRefClock,&refclock);
943 if (error!=OMX_ErrorNone){
944 logger->debug(TAG, "Clock OMX_IndexConfigTimeActiveRefClock failed {:#x}", error);
945 clock_mutex.unlock();
946 DeAllocateCodecsOMX();
950 OMX_PORT_PARAM_TYPE p_param;
951 memset(&p_param,0,sizeof(p_param));
952 p_param.nSize=sizeof(p_param);
953 p_param.nVersion.nVersion=OMX_VERSION;
954 error=OMX_GetParameter(omx_clock,OMX_IndexParamOtherInit,&p_param);
955 if (error!=OMX_ErrorNone){
956 logger->debug(TAG, "Init clock OMX_GetParameter failed {:#x}", error);
957 clock_mutex.unlock();
958 DeAllocateCodecsOMX();
962 OMX_TIME_CONFIG_CLOCKSTATETYPE clock_conf;
963 memset(&clock_conf,0,sizeof(clock_conf));
964 clock_conf.nSize=sizeof(clock_conf);
965 clock_conf.nVersion.nVersion=OMX_VERSION;
966 clock_conf.eState=OMX_TIME_ClockStateWaitingForStartTime;
967 clock_conf.nStartTime=intToOMXTicks(0);
968 clock_conf.nOffset=intToOMXTicks(0);
969 if (clock_references==1) clock_conf.nWaitMask=OMX_CLOCKPORT1;
970 else clock_conf.nWaitMask=OMX_CLOCKPORT0|OMX_CLOCKPORT1;
971 error=OMX_SetConfig(omx_clock,OMX_IndexConfigTimeClockState,&clock_conf);
972 if (error!=OMX_ErrorNone) {
973 logger->debug(TAG, "AuI Clock IndexConfigTimeClockState failed {:#x}", error);
977 *p_omx_clock_output_port=p_param.nStartPortNumber+1;
978 *p_omx_clock=omx_clock;
979 clock_mutex.unlock();
983 int VideoOMX::getClockVideoandInit()
992 if (clock_references==1) { // only if no audio is attached to this clock!
993 OMX_TIME_CONFIG_ACTIVEREFCLOCKTYPE refclock;
994 memset(&refclock,0,sizeof(refclock));
995 refclock.nSize=sizeof(refclock);
996 refclock.nVersion.nVersion=OMX_VERSION;
998 //refclock.eClock=OMX_TIME_RefClockAudio;
1000 refclock.eClock=OMX_TIME_RefClockVideo;
1001 error=OMX_SetConfig(omx_clock,OMX_IndexConfigTimeActiveRefClock,&refclock);
1002 if (error!=OMX_ErrorNone) {
1003 logger->debug(TAG, "Clock OMX_IndexConfigTimeActiveRefClock failed {:#x}", error);
1004 clock_mutex.unlock();
1005 DeAllocateCodecsOMX();
1010 OMX_PORT_PARAM_TYPE p_param;
1011 memset(&p_param,0,sizeof(p_param));
1012 p_param.nSize=sizeof(p_param);
1013 p_param.nVersion.nVersion=OMX_VERSION;
1014 error=OMX_GetParameter(omx_clock,OMX_IndexParamOtherInit,&p_param);
1015 if (error!=OMX_ErrorNone){
1016 logger->debug(TAG, "Init clock OMX_GetParameter failed {:#x}", error);
1017 clock_mutex.unlock();
1018 DeAllocateCodecsOMX();
1023 OMX_TIME_CONFIG_CLOCKSTATETYPE clock_conf;
1024 memset(&clock_conf,0,sizeof(clock_conf));
1025 clock_conf.nSize=sizeof(clock_conf);
1026 clock_conf.nVersion.nVersion=OMX_VERSION;
1027 clock_conf.eState=OMX_TIME_ClockStateStopped;
1028 clock_conf.nStartTime=intToOMXTicks(0);
1029 clock_conf.nOffset=intToOMXTicks(0);
1030 error=OMX_SetConfig(omx_clock,OMX_IndexConfigTimeClockState,&clock_conf);
1031 if (error!=OMX_ErrorNone) {
1032 logger->debug(TAG, "VuI Clock IndexConfigTimeClockState failed {:#x}", error);
1036 memset(&clock_conf,0,sizeof(clock_conf));
1037 clock_conf.nSize=sizeof(clock_conf);
1038 clock_conf.nVersion.nVersion=OMX_VERSION;
1039 clock_conf.eState=OMX_TIME_ClockStateWaitingForStartTime;
1040 clock_conf.nStartTime=intToOMXTicks(0);
1041 clock_conf.nOffset=intToOMXTicks(0);
1042 if (clock_references==1) clock_conf.nWaitMask=OMX_CLOCKPORT0;
1043 else clock_conf.nWaitMask=OMX_CLOCKPORT0|OMX_CLOCKPORT1;
1044 error=OMX_SetConfig(omx_clock,OMX_IndexConfigTimeClockState,&clock_conf);
1045 if (error!=OMX_ErrorNone) {
1046 logger->debug(TAG, "VuI Clock IndexConfigTimeClockState failed {:#x}", error);
1049 omx_clock_output_port=p_param.nStartPortNumber;
1050 clock_mutex.unlock();
1055 void VideoOMX::clockUnpause()
1057 OMX_ERRORTYPE error;
1058 LogNT::getInstance()->info(TAG, "enter Clockunpause");
1060 if (clock_references>0 && clockpaused) {
1061 OMX_TIME_CONFIG_SCALETYPE scale_type;
1062 memset(&scale_type,0,sizeof(scale_type));
1063 scale_type.nSize=sizeof(scale_type);
1064 scale_type.nVersion.nVersion=OMX_VERSION;
1065 scale_type.xScale=1<<16;
1066 error=OMX_SetConfig(omx_clock,OMX_IndexConfigTimeScale,&scale_type);
1067 if (error!=OMX_ErrorNone) {
1068 logger->debug(TAG, "ClockUnpause OMX_IndexConfigTimeScale failed {:#x}", error);
1070 LogNT::getInstance()->info(TAG, "set playback speed ClockUnpause");
1073 clock_mutex.unlock();
1077 void VideoOMX::clockPause()
1079 OMX_ERRORTYPE error;
1080 LogNT::getInstance()->info(TAG, "enter ClockPause");
1082 if (clock_references>0 && !clockpaused) {
1083 OMX_TIME_CONFIG_SCALETYPE scale_type;
1084 memset(&scale_type,0,sizeof(scale_type));
1085 scale_type.nSize=sizeof(scale_type);
1086 scale_type.nVersion.nVersion=OMX_VERSION;
1087 scale_type.xScale=0;
1088 error=OMX_SetConfig(omx_clock,OMX_IndexConfigTimeScale,&scale_type);
1089 if (error!=OMX_ErrorNone) {
1090 logger->debug(TAG, "ClockPause OMX_IndexConfigTimeScale failed {:#x}", error);
1092 LogNT::getInstance()->info(TAG, "set playback speed ClockPause");
1095 clock_mutex.unlock();
1100 int VideoOMX::AllocateCodecsOMX()
1102 OMX_ERRORTYPE error;
1103 static OMX_CALLBACKTYPE callbacks= {&EventHandler_OMX,&EmptyBufferDone_OMX,&FillBufferDone_OMX};
1105 Demuxer* demux=Demuxer::getInstance();
1110 LogNT::getInstance()->info(TAG, "Allocate Codecs OMX");
1111 //Clock, move later to audio including events
1113 LogNT::getInstance()->info(TAG, "Deinter VideoType {} x {} i: {}", demux->getHorizontalSize(),demux->getVerticalSize(),demux->getInterlaced());
1114 if (deinterlace!=0 && /*(demux->getHorizontalSize()<=720 ) &&*/ demux->getInterlaced()) {
1118 LogNT::getInstance()->info(TAG, "Deinterlacing activated {}",deinterlace);
1123 if (!getClockVideoandInit()){
1124 return 0;// get the clock and init it if necessary
1129 logger->debug(TAG, "idleClock failed");
1136 error=OMX_GetHandle(&omx_vid_dec,L_VPE_OMX_H264_DECODER,NULL,&callbacks);
1138 error=OMX_GetHandle(&omx_vid_dec,L_VPE_OMX_MPEG2_DECODER,NULL,&callbacks);
1141 if (error!=OMX_ErrorNone){
1142 logger->debug(TAG, "Init OMX video decoder failed {:#x}", error);
1143 clock_mutex.unlock();
1144 DeAllocateCodecsOMX();
1149 logger->debug(TAG, "Nmark3 ");
1150 OMX_PORT_PARAM_TYPE p_param;
1151 memset(&p_param,0,sizeof(p_param));
1152 p_param.nSize=sizeof(p_param);
1153 p_param.nVersion.nVersion=OMX_VERSION;
1154 error=OMX_GetParameter(omx_vid_dec,OMX_IndexParamVideoInit,&p_param);
1155 if (error!=OMX_ErrorNone){
1156 logger->debug(TAG, "Init OMX h264 decoder OMX_GetParameter failed {:#x}", error);
1157 clock_mutex.unlock();
1158 DeAllocateCodecsOMX();
1161 omx_codec_input_port=p_param.nStartPortNumber;
1162 omx_codec_output_port=p_param.nStartPortNumber+1;
1164 if (!DisablePort(omx_vid_dec,omx_codec_input_port) || !DisablePort(omx_vid_dec,omx_codec_output_port)) {
1165 logger->debug(TAG, "Disable Ports OMX video decoder failed");
1166 clock_mutex.unlock();
1167 DeAllocateCodecsOMX();
1171 logger->debug(TAG, "Nmark4 ");
1173 OMX_PARAM_BRCMVIDEODECODEERRORCONCEALMENTTYPE conceal;
1174 memset(&conceal,0,sizeof(conceal));
1175 conceal.nSize=sizeof(conceal);
1176 conceal.nVersion.nVersion=OMX_VERSION;
1177 conceal.bStartWithValidFrame=OMX_FALSE;
1179 error=OMX_SetParameter(omx_vid_dec,OMX_IndexParamBrcmVideoDecodeErrorConcealment,&conceal);
1180 if (error!=OMX_ErrorNone){
1181 logger->debug(TAG, "OMX_IndexParamBrcmVideoDecodeErrorConcealment failed {:#x}", error);
1182 clock_mutex.unlock();
1183 DeAllocateCodecsOMX();
1188 error = OMX_GetHandle(&omx_vid_deint, L_VPE_OMX_VIDEO_DEINTERLACE, NULL,
1190 if (error != OMX_ErrorNone) {
1191 LogNT::getInstance()->debug(TAG,
1192 "Init OMX video deinterlacer failed {:#x}", error);
1193 clock_mutex.unlock();
1194 DeAllocateCodecsOMX();
1198 error = OMX_GetParameter(omx_vid_deint, OMX_IndexParamImageInit,
1200 if (error != OMX_ErrorNone) {
1201 LogNT::getInstance()->debug(TAG,
1202 "Init OMX video deinterlacer OMX_GetParameter failed {:#x}",
1204 clock_mutex.unlock();
1205 DeAllocateCodecsOMX();
1208 omx_deint_input_port = p_param.nStartPortNumber;
1209 omx_deint_output_port = p_param.nStartPortNumber + 1;
1211 if (!DisablePort(omx_vid_deint, omx_deint_input_port, true)
1212 || !DisablePort(omx_vid_deint, omx_deint_output_port, true)) {
1213 LogNT::getInstance()->debug(TAG,
1214 "Disable Ports OMX video deint failed");
1215 clock_mutex.unlock();
1216 DeAllocateCodecsOMX();
1223 error=OMX_GetHandle(&omx_vid_sched,L_VPE_OMX_VIDEO_SCHED,NULL,&callbacks);
1224 if (error!=OMX_ErrorNone){
1225 logger->debug(TAG, "Init OMX video scheduler failed {:#x}", error);
1226 clock_mutex.unlock();
1227 DeAllocateCodecsOMX();
1233 error=OMX_GetParameter(omx_vid_sched,OMX_IndexParamVideoInit,&p_param);
1234 if (error!=OMX_ErrorNone){
1235 logger->debug(TAG, "Init OMX video scheduler OMX_GetParameter failed {:#x}", error);
1236 clock_mutex.unlock();
1237 DeAllocateCodecsOMX();
1240 omx_shed_input_port=p_param.nStartPortNumber;
1241 omx_shed_output_port=p_param.nStartPortNumber+1;
1244 error=OMX_GetParameter(omx_vid_sched,OMX_IndexParamOtherInit,&p_param);
1245 if (error!=OMX_ErrorNone){
1246 logger->debug(TAG, "Init OMX video scheduler OMX_GetParameter failed {:#x}", error);
1247 clock_mutex.unlock();
1248 DeAllocateCodecsOMX();
1251 omx_shed_clock_port=p_param.nStartPortNumber;
1252 logger->debug(TAG, "scheduler ports {} {} {}",omx_shed_input_port,omx_shed_output_port,omx_shed_clock_port);
1255 if (!DisablePort(omx_vid_sched,omx_shed_input_port,true) || !DisablePort(omx_vid_sched,omx_shed_output_port,true)
1256 || !DisablePort(omx_vid_sched,omx_shed_clock_port,true)) {
1257 logger->debug(TAG, "Disable Ports OMX video shed failed");
1258 clock_mutex.unlock();
1259 DeAllocateCodecsOMX();
1264 error=OMX_GetHandle(&omx_vid_rend,L_VPE_OMX_VIDEO_REND,NULL,&callbacks);
1265 if (error!=OMX_ErrorNone){
1266 logger->debug(TAG, "Init OMX video rend failed {:#x}", error);
1267 clock_mutex.unlock();
1268 DeAllocateCodecsOMX();
1272 error=OMX_GetParameter(omx_vid_rend,OMX_IndexParamVideoInit,&p_param);
1273 if (error!=OMX_ErrorNone){
1274 logger->debug(TAG, "Init OMX video rend OMX_GetParameter failed {:#x}", error);
1275 clock_mutex.unlock();
1276 DeAllocateCodecsOMX();
1279 omx_rend_input_port=p_param.nStartPortNumber;
1280 //omx_rend_output_port=p_param.nStartPortNumber+1;
1283 if (!DisablePort(omx_vid_rend,omx_rend_input_port,true) /*|| !DisablePort(omx_vid_rend,omx_rend_output_port)*/
1285 logger->debug(TAG, "Disable Ports OMX video rend failed");
1286 clock_mutex.unlock();
1287 DeAllocateCodecsOMX();
1295 error=OMX_SetupTunnel(omx_clock,omx_clock_output_port,omx_vid_sched,omx_shed_clock_port);
1296 if (error!=OMX_ErrorNone){
1297 logger->debug(TAG, "OMX_Setup tunnel clock to sched failed {:#x} {} {}", error,omx_clock_output_port,omx_shed_clock_port);
1298 clock_mutex.unlock();
1299 DeAllocateCodecsOMX();
1303 if (!EnablePort(omx_clock,omx_clock_output_port,false) || !EnablePort(omx_vid_sched,omx_shed_clock_port,false)
1305 logger->debug(TAG, "Enable Ports OMX clock shed failed");
1306 clock_mutex.unlock();
1307 DeAllocateCodecsOMX();
1313 if (!ChangeComponentState(omx_vid_sched,OMX_StateIdle)) {
1314 logger->debug(TAG, "vid_sched idle ChangeComponentState");
1315 clock_mutex.unlock();
1316 DeAllocateCodecsOMX();
1323 if ( !CommandFinished(omx_vid_sched,OMX_CommandPortEnable,omx_shed_clock_port)) {
1324 clock_mutex.unlock();
1325 DeAllocateCodecsOMX();
1333 if ( !CommandFinished(omx_clock,OMX_CommandPortEnable,omx_clock_output_port)) {
1334 clock_mutex.unlock();
1335 DeAllocateCodecsOMX();
1341 /* error=OMX_SendCommand(omx_vid_dec,OMX_CommandStateSet,OMX_StateIdle,0);
1342 if (error!=OMX_ErrorNone){
1343 logger->debug(TAG, "vid_dec Send Command to OMX State Idle {:#x}", error);
1347 OMX_VIDEO_PARAM_PORTFORMATTYPE ft_type;
1348 memset(&ft_type,0,sizeof(ft_type));
1349 ft_type.nSize=sizeof(ft_type);
1350 ft_type.nVersion.nVersion=OMX_VERSION;
1352 ft_type.nPortIndex=omx_codec_input_port;
1354 ft_type.eCompressionFormat=OMX_VIDEO_CodingAVC;
1356 ft_type.eCompressionFormat=OMX_VIDEO_CodingMPEG2;
1361 ft_type.xFramerate=0*(1<<16);//25*(1<<16);//demux->getFrameRate()*(1<<16);
1362 logger->debug(TAG, "Framerate: {}",demux->getFrameRate());
1363 error=OMX_SetParameter(omx_vid_dec,OMX_IndexParamVideoPortFormat,&ft_type);
1364 if (error!=OMX_ErrorNone){
1365 logger->debug(TAG, "Init OMX_IndexParamVideoPortFormat failed {:#x}", error);
1366 clock_mutex.unlock();
1367 DeAllocateCodecsOMX();
1372 if (!ChangeComponentState(omx_vid_dec,OMX_StateIdle)) {
1373 logger->debug(TAG, "vid_dec ChangeComponentState");
1374 clock_mutex.unlock();
1375 DeAllocateCodecsOMX();
1379 OMX_CONFIG_BUFFERSTALLTYPE stall_conf;
1380 memset(&stall_conf,0,sizeof(stall_conf));
1381 stall_conf.nSize=sizeof(stall_conf);
1382 stall_conf.nVersion.nVersion=OMX_VERSION;
1383 stall_conf.nPortIndex=omx_codec_output_port;
1384 stall_conf.nDelay=1500*1000;
1385 error=OMX_SetConfig(omx_vid_dec,OMX_IndexConfigBufferStall,&stall_conf);
1386 if (error!=OMX_ErrorNone){
1387 logger->debug(TAG, "Init OMX_IndexConfigBufferStall failed {:#x}", error);
1388 clock_mutex.unlock();
1389 DeAllocateCodecsOMX();
1392 omx_vid_stalled = false;
1394 OMX_CONFIG_REQUESTCALLBACKTYPE req_callback;
1395 memset(&req_callback,0,sizeof(req_callback));
1396 req_callback.nSize=sizeof(req_callback);
1397 req_callback.nVersion.nVersion=OMX_VERSION;
1398 req_callback.nPortIndex=omx_codec_output_port;
1399 req_callback.nIndex=OMX_IndexConfigBufferStall;
1400 req_callback.bEnable=OMX_TRUE;
1401 error=OMX_SetConfig(omx_vid_dec,OMX_IndexConfigRequestCallback,&req_callback);
1402 if (error!=OMX_ErrorNone){
1403 logger->debug(TAG, "Init OMX_IndexConfigRequestCallback failed {:#x}", error);
1404 clock_mutex.unlock();
1405 DeAllocateCodecsOMX();
1411 if (!PrepareInputBufsOMX()) {
1412 clock_mutex.unlock();
1413 DeAllocateCodecsOMX();
1418 error=OMX_SetupTunnel(omx_vid_dec,omx_codec_output_port,omx_vid_sched,omx_shed_input_port);
1419 if (error!=OMX_ErrorNone){
1420 logger->debug(TAG, "OMX_Setup tunnel dec to sched failed {:#x}", error);
1421 clock_mutex.unlock();
1422 DeAllocateCodecsOMX();
1428 if (!EnablePort(omx_vid_dec,omx_codec_output_port,false) || !EnablePort(omx_vid_sched,omx_shed_input_port,false)
1430 logger->debug(TAG, "Enable Ports OMX codec shed failed");
1431 clock_mutex.unlock();
1432 DeAllocateCodecsOMX();
1436 if ( !CommandFinished(omx_vid_dec,OMX_CommandPortEnable,omx_codec_output_port) || !CommandFinished(omx_vid_sched,OMX_CommandPortEnable,omx_shed_input_port)) {
1437 clock_mutex.unlock();
1438 DeAllocateCodecsOMX();
1444 error=OMX_SetupTunnel(omx_vid_dec,omx_codec_output_port,omx_vid_deint,omx_deint_input_port);
1445 if (error!=OMX_ErrorNone){
1446 logger->debug(TAG, "OMX_Setup tunnel dec to deint failed {:#x}", error);
1447 clock_mutex.unlock();
1448 DeAllocateCodecsOMX();
1454 if (!EnablePort(omx_vid_dec,omx_codec_output_port,false) || !EnablePort(omx_vid_deint,omx_deint_input_port,false)
1456 logger->debug(TAG, "Enable Ports OMX codec deint failed");
1457 clock_mutex.unlock();
1458 DeAllocateCodecsOMX();
1462 if ( !CommandFinished(omx_vid_dec,OMX_CommandPortEnable,omx_codec_output_port) || !CommandFinished(omx_vid_deint,OMX_CommandPortEnable,omx_deint_input_port)) {
1463 clock_mutex.unlock();
1464 DeAllocateCodecsOMX();
1468 if (!ChangeComponentState(omx_vid_deint,OMX_StateIdle)) {
1469 logger->debug(TAG, "vid_deint ChangeComponentState");
1470 clock_mutex.unlock();
1471 DeAllocateCodecsOMX();
1475 OMX_CONFIG_IMAGEFILTERPARAMSTYPE imagefilter;
1476 memset(&imagefilter,0,sizeof(imagefilter));
1477 imagefilter.nSize=sizeof(imagefilter);
1478 imagefilter.nVersion.nVersion=OMX_VERSION;
1480 imagefilter.nPortIndex=omx_deint_output_port;
1481 imagefilter.nNumParams=4;
1482 imagefilter.nParams[0]=3;//???
1483 imagefilter.nParams[1]=0;//default frame interval
1484 imagefilter.nParams[2]=0;// frame rate
1485 if (demux->getHorizontalSize() <= 720){
1486 imagefilter.nParams[3] = 1;//use qpus
1490 imagefilter.nParams[3] = 0;//use qpus
1493 switch (deinterlace) {
1495 imagefilter.eImageFilter=OMX_ImageFilterDeInterlaceLineDouble; break;
1497 imagefilter.eImageFilter=OMX_ImageFilterDeInterlaceAdvanced; break;
1499 imagefilter.eImageFilter=OMX_ImageFilterFilm; break;
1501 imagefilter.eImageFilter = OMX_ImageFilterDeInterlaceFast; break;
1505 error=OMX_SetConfig(omx_vid_deint,OMX_IndexConfigCommonImageFilterParameters,&imagefilter);
1506 if (error!=OMX_ErrorNone){
1507 logger->debug(TAG, "Init OMX_IndexConfigCommonImageFilterParameters failed {:#x}", error);
1508 clock_mutex.unlock();
1509 DeAllocateCodecsOMX();
1514 error=OMX_SetupTunnel(omx_vid_deint,omx_deint_output_port,omx_vid_sched,omx_shed_input_port);
1515 if (error!=OMX_ErrorNone){
1516 logger->debug(TAG, "OMX_Setup tunnel deint to sched failed {:#x}", error);
1517 clock_mutex.unlock();
1518 DeAllocateCodecsOMX();
1522 if (!EnablePort(omx_vid_deint,omx_deint_output_port,false) || !EnablePort(omx_vid_sched,omx_shed_input_port,false)
1524 logger->debug(TAG, "Enable Ports OMX deint shed failed");
1525 clock_mutex.unlock();
1526 DeAllocateCodecsOMX();
1530 if ( !CommandFinished(omx_vid_deint,OMX_CommandPortEnable,omx_deint_output_port) || !CommandFinished(omx_vid_sched,OMX_CommandPortEnable,omx_shed_input_port)) {
1531 clock_mutex.unlock();
1532 DeAllocateCodecsOMX();
1538 if (!ChangeComponentState(omx_vid_dec,OMX_StateExecuting)) {
1539 logger->debug(TAG, "omx_vid_dec ChangeComponentState Execute");
1540 clock_mutex.unlock();
1541 DeAllocateCodecsOMX();
1545 error=OMX_SetupTunnel(omx_vid_sched,omx_shed_output_port,omx_vid_rend,omx_rend_input_port);
1546 if (error!=OMX_ErrorNone){
1547 logger->debug(TAG, "OMX_Setup tunnel sched to rend failed {:#x}", error);
1548 clock_mutex.unlock();
1549 DeAllocateCodecsOMX();
1553 if (!EnablePort(omx_vid_sched,omx_shed_output_port,false) || !EnablePort(omx_vid_rend,omx_rend_input_port,false)
1555 logger->debug(TAG, "Enable Ports OMX shed rend failed");
1556 clock_mutex.unlock();
1557 DeAllocateCodecsOMX();
1561 if (!CommandFinished(omx_vid_sched,OMX_CommandPortEnable,omx_shed_output_port)
1562 || !CommandFinished(omx_vid_rend,OMX_CommandPortEnable,omx_rend_input_port)) {
1563 clock_mutex.unlock();
1564 DeAllocateCodecsOMX();
1568 if (!ChangeComponentState(omx_vid_rend,OMX_StateIdle)) {
1569 logger->debug(TAG, "vid_rend ChangeComponentState");
1570 clock_mutex.unlock();
1571 DeAllocateCodecsOMX();
1576 if (!ChangeComponentState(omx_vid_deint,OMX_StateExecuting)) {
1577 logger->debug(TAG, "vid_vid_deint ChangeComponentState");
1578 clock_mutex.unlock();
1579 DeAllocateCodecsOMX();
1582 DisablePort(omx_vid_deint,omx_deint_output_port,false);
1583 DisablePort(omx_vid_deint,omx_deint_input_port,false);
1586 if (!ChangeComponentState(omx_vid_sched,OMX_StateExecuting)) {
1587 logger->debug(TAG, "omx_vid_sched ChangeComponentState Execute");
1588 clock_mutex.unlock();
1589 DeAllocateCodecsOMX();
1593 if (!ChangeComponentState(omx_vid_rend,OMX_StateExecuting)) {
1594 logger->debug(TAG, "omx_vid_rend ChangeComponentState Execute");
1595 clock_mutex.unlock();
1596 DeAllocateCodecsOMX();
1601 /*OMX_CONFIG_DISPLAYREGIONTYPE dispconf;
1602 memset(&dispconf,0,sizeof(dispconf));
1603 dispconf.nSize=sizeof(dispconf);
1604 dispconf.nVersion.nVersion=OMX_VERSION;
1606 dispconf.nPortIndex=omx_rend_input_port;
1608 dispconf.set=OMX_DISPLAY_SET_LAYER ;
1610 error=OMX_SetConfig(omx_vid_rend,OMX_IndexConfigDisplayRegion,&dispconf);
1611 if (error!=OMX_ErrorNone){
1612 logger->debug(TAG, "Init OMX_IndexConfigDisplayRegion failed {:#x}", error);
1613 clock_mutex.unlock();
1614 DeAllocateCodecsOMX();
1618 /* dispconf.set=OMX_DISPLAY_SET_FULLSCREEN ;
1619 dispconf.fullscreen=OMX_FALSE;
1620 error=OMX_SetParameter(omx_vid_rend,OMX_IndexConfigDisplayRegion,&dispconf);
1621 if (error!=OMX_ErrorNone){
1622 logger->debug(TAG, "Init OMX_IndexConfigDisplayRegion failed {:#x}", error);
1623 clock_mutex.unlock();
1624 DeAllocateCodecsOMX();
1628 dispconf.set=OMX_DISPLAY_SET_DEST_RECT;
1629 dispconf.dest_rect.x_offset=100;
1630 dispconf.dest_rect.y_offset=100;
1631 dispconf.dest_rect.width=640;
1632 dispconf.dest_rect.height=480;
1633 error=OMX_SetParameter(omx_vid_rend,OMX_IndexConfigDisplayRegion,&dispconf);
1634 if (error!=OMX_ErrorNone){
1635 logger->debug(TAG, "Init OMX_IndexConfigDisplayRegion failed {:#x}", error);
1636 clock_mutex.unlock();
1637 DeAllocateCodecsOMX();
1642 //playbacktimeoffset=-GetCurrentSystemTime();
1646 clock_mutex.unlock();
1650 setClockExecutingandRunning();
1659 int VideoOMX::idleClock()
1661 //OMX_ERRORTYPE error;
1662 OMX_STATETYPE temp_state;
1664 OMX_GetState(omx_clock,&temp_state);
1666 if (temp_state!=OMX_StateIdle) {
1667 if (!ChangeComponentState(omx_clock,OMX_StateIdle)) {
1668 logger->debug(TAG, "omx_clock ChangeComponentState Idle failed");
1669 clock_mutex.unlock();
1673 clock_mutex.unlock();
1677 int VideoOMX::setClockExecutingandRunning()
1679 OMX_ERRORTYPE error;
1680 OMX_STATETYPE temp_state;
1682 OMX_GetState(omx_clock,&temp_state);
1684 if (temp_state!=OMX_StateExecuting) {
1685 if (!ChangeComponentState(omx_clock,OMX_StateExecuting)) {
1686 logger->debug(TAG, "omx_clock ChangeComponentState Execute failed");
1687 clock_mutex.unlock();
1688 DeAllocateCodecsOMX();
1693 OMX_TIME_CONFIG_CLOCKSTATETYPE clock_conf;
1694 memset(&clock_conf,0,sizeof(clock_conf));
1695 clock_conf.nSize=sizeof(clock_conf);
1696 clock_conf.nVersion.nVersion=OMX_VERSION;
1697 clock_conf.eState=OMX_TIME_ClockStateRunning;
1698 error=OMX_SetConfig(omx_clock,OMX_IndexConfigTimeClockState,&clock_conf);
1699 if (error!=OMX_ErrorNone) {
1700 logger->debug(TAG, "Clock IndexConfigTimeClockState failed {:#x}", error);
1701 clock_mutex.unlock();
1702 DeAllocateCodecsOMX();
1705 clock_mutex.unlock();
1711 int VideoOMX::ChangeComponentState(OMX_HANDLETYPE handle,OMX_STATETYPE type,bool wait) //needs to be called with locked mutex
1713 OMX_ERRORTYPE error;
1714 error=OMX_SendCommand(handle,OMX_CommandStateSet,type,0);
1715 if (error!=OMX_ErrorNone){
1716 logger->debug(TAG, "handle {:#x} Send Command to OMX State {:#x} {:#x}",handle,type, error);
1721 if (!CommandFinished(handle,OMX_CommandStateSet,type)) {
1730 int VideoOMX::EnablePort(OMX_HANDLETYPE handle,OMX_U32 port,bool wait) //needs to be called with locked mutex
1732 OMX_ERRORTYPE error;
1735 OMX_PARAM_PORTDEFINITIONTYPE pdt;
1736 memset(&pdt,0,sizeof(pdt));
1737 pdt.nSize=sizeof(pdt);
1738 pdt.nVersion.nVersion=OMX_VERSION;
1739 pdt.nPortIndex=port;
1740 error=OMX_GetParameter(handle,OMX_IndexParamPortDefinition, &pdt);
1741 if (error==OMX_ErrorNone) {
1742 if(pdt.bEnabled==OMX_TRUE) {
1743 skip=true; //already disabled;
1749 error=OMX_SendCommand(handle,OMX_CommandPortEnable,port,0);
1750 if (error!=OMX_ErrorNone){
1751 logger->debug(TAG, "handle {:#x} Send Command to enable port {:#x} {:#x}",handle,port, error);
1755 if (!wait) return 1;
1756 if (!CommandFinished(handle,OMX_CommandPortEnable,port)) {
1765 int VideoOMX::DisablePort(OMX_HANDLETYPE handle,OMX_U32 port,bool wait) //needs to be called with locked mutex
1767 OMX_ERRORTYPE error;
1770 OMX_PARAM_PORTDEFINITIONTYPE pdt;
1771 memset(&pdt,0,sizeof(pdt));
1772 pdt.nSize=sizeof(pdt);
1773 pdt.nVersion.nVersion=OMX_VERSION;
1774 pdt.nPortIndex=port;
1775 error=OMX_GetParameter(handle,OMX_IndexParamPortDefinition, &pdt);
1776 if (error==OMX_ErrorNone) {
1777 if(pdt.bEnabled==OMX_FALSE) {
1778 skip=true; //already disabled;
1785 error=OMX_SendCommand(handle,OMX_CommandPortDisable,port,0);
1786 if (error!=OMX_ErrorNone){
1787 logger->debug(TAG, "handle {:#x} Send Command to disable port {:#x} {:#x}",handle,port, error);
1791 if (!wait) return 1;
1792 if (!CommandFinished(handle,OMX_CommandPortDisable,port)) {
1800 int VideoOMX::WaitForEvent(OMX_HANDLETYPE handle,OMX_U32 event, int wait) //needs to be called with locked mutex
1803 int iend=(wait/5+1);
1805 omx_event_mutex.lock();
1806 std::list<VPE_OMX_EVENT>::iterator itty=omx_events.begin();
1807 while (itty!=omx_events.end()) {
1809 VPE_OMX_EVENT current=*itty;
1810 if (current.handle==handle) { //this is ours
1811 if (current.event_type==OMX_EventError) {
1812 omx_events.erase(itty);
1813 omx_event_mutex.unlock();
1814 logger->debug(TAG, "WaitForEvent Finished on Error");
1817 } else if (current.event_type==event) {
1818 omx_events.erase(itty);
1819 omx_event_mutex.unlock();
1820 logger->debug(TAG, "WaitForEvent Finished Completed");
1827 omx_event_mutex.unlock();
1829 //logger->debug(TAG, "WaitForEvent");
1830 std::unique_lock<std::mutex> ul(omx_event_ready_signal_mutex);
1831 omx_event_ready_signal.wait_for(ul, std::chrono::milliseconds(10));
1837 logger->debug(TAG, "WaitForEvent waited too long {:p} {:#x}",(void*)handle,event);
1842 int VideoOMX::clearEvents()
1844 omx_event_mutex.lock();
1846 omx_event_mutex.unlock();
1851 int VideoOMX::clearEventsForComponent(OMX_HANDLETYPE handle)
1853 omx_event_mutex.lock();
1854 std::list<VPE_OMX_EVENT>::iterator itty=omx_events.begin();
1855 while (itty!=omx_events.end()) {
1856 VPE_OMX_EVENT current=*itty;
1857 if (current.handle==handle) { //this is ours
1858 itty=omx_events.erase(itty);
1864 omx_event_mutex.unlock();
1868 void VideoOMX::checkForStalledBuffers()
1870 //logger->debug(TAG, "Check stalled");
1872 omx_event_mutex.lock();
1873 std::list<VPE_OMX_EVENT>::iterator itty=omx_events.begin();
1874 while (itty!=omx_events.end()) {
1875 VPE_OMX_EVENT current=*itty;
1876 if (current.event_type==OMX_EventParamOrConfigChanged && current.data1==omx_codec_output_port
1877 && current.handle==omx_vid_dec && current.data2==OMX_IndexConfigBufferStall) {
1878 OMX_ERRORTYPE error;
1879 OMX_CONFIG_BUFFERSTALLTYPE stall_conf;
1880 memset(&stall_conf,0,sizeof(stall_conf));
1881 stall_conf.nSize=sizeof(stall_conf);
1882 stall_conf.nVersion.nVersion=OMX_VERSION;
1883 stall_conf.nPortIndex=omx_codec_output_port;
1884 stall_conf.nDelay=200000;
1886 error=OMX_GetConfig(omx_vid_dec,OMX_IndexConfigBufferStall,&stall_conf);
1887 if (error!=OMX_ErrorNone){
1888 logger->debug(TAG, "Get OMX_IndexConfigBufferStall failed {:#x}", error);
1889 clock_mutex.unlock();
1890 omx_event_mutex.unlock();
1893 if (stall_conf.bStalled==OMX_TRUE) {
1894 omx_vid_stalled=true;
1895 logger->debug(TAG, "Video decoder stalled! {}", stall_conf.nDelay);
1897 omx_vid_stalled=false;
1898 logger->debug(TAG, "Video decoder unstalled! {}",stall_conf.nDelay);
1900 omx_events.erase(itty);
1905 omx_event_mutex.unlock();
1906 clock_mutex.unlock();
1912 int VideoOMX::CommandFinished(OMX_HANDLETYPE handle,OMX_U32 command,OMX_U32 data2) //needs to be called with locked mutex
1915 while (i<200/*1000*/) {
1916 omx_event_mutex.lock();
1917 std::list<VPE_OMX_EVENT>::iterator itty=omx_events.begin();
1918 while (itty!=omx_events.end()) {
1920 VPE_OMX_EVENT current=*itty;
1921 if (current.handle==handle) { //this is ours
1922 if (current.event_type==OMX_EventError) {
1923 omx_events.erase(itty);
1924 omx_event_mutex.unlock();
1925 logger->debug(TAG, "Command Finished on Error {:#x}",current.data1);
1928 } else if (current.event_type==OMX_EventCmdComplete && current.data1==command && current.data2==data2) {
1929 omx_events.erase(itty);
1930 omx_event_mutex.unlock();
1931 //logger->debug(TAG, "Command Finished Completed");
1938 omx_event_mutex.unlock();
1940 std::unique_lock<std::mutex> ul(omx_event_ready_signal_mutex);
1941 omx_event_ready_signal.wait_for(ul, std::chrono::milliseconds(10));
1947 logger->debug(TAG, "CommandFinished waited too long {:#x} {:#x} {:#x}",handle,command, data2);
1954 int VideoOMX::PrepareInputBufsOMX() //needs to be called with locked mutex
1956 OMX_ERRORTYPE error;
1957 OMX_PARAM_PORTDEFINITIONTYPE port_def_type;
1958 memset(&port_def_type,0,sizeof(port_def_type));
1959 port_def_type.nSize=sizeof(port_def_type);
1960 port_def_type.nVersion.nVersion=OMX_VERSION;
1961 port_def_type.nPortIndex=omx_codec_input_port;
1963 error=OMX_GetParameter(omx_vid_dec,OMX_IndexParamPortDefinition, &port_def_type);
1965 if (error!=OMX_ErrorNone){
1966 logger->debug(TAG, "Get OMX OMX_IndexParamPortDefinition failed {:#x}", error);
1968 /* logger->debug(TAG, "Port para {} {} {} {} {} {} {}", port_def_type.nBufferCountActual,
1969 port_def_type.nBufferCountMin,port_def_type.nBufferSize,port_def_type.bEnabled,port_def_type.bPopulated,
1970 port_def_type.bBuffersContiguous,port_def_type.nBufferAlignment);*/
1972 port_def_type.nBufferCountActual=100;
1973 port_def_type.nBufferSize=max(port_def_type.nBufferSize,150000); // for transcoder important
1975 error=OMX_SetParameter(omx_vid_dec,OMX_IndexParamPortDefinition, &port_def_type);
1977 if (error!=OMX_ErrorNone){
1978 logger->debug(TAG, "Set OMX OMX_IndexParamPortDefinition failed {:#x}", error);
1982 error=OMX_SendCommand(omx_vid_dec,OMX_CommandPortEnable,omx_codec_input_port,0);
1983 if (error!=OMX_ErrorNone){
1984 logger->debug(TAG, "Prepare Input bufs Send Command to enable port {:#x}", error);
1988 input_bufs_omx_mutex.lock();
1989 for (unsigned int i=0; i< port_def_type.nBufferCountActual;i++) {
1991 // unsigned char* new_buffer_data=(unsigned char*)malloc(port_def_type.nbufferSize);
1992 OMX_BUFFERHEADERTYPE *buf_head=NULL;
1993 /* error=OMX_Usebuffer(omx_vid_dec,&buf_head,omx_codec_input_port,NULL,port_def_type.nbufferSize,new_buffer_data);
1994 if (error!=OMX_ErrorNone){
1995 logger->debug(TAG, "Use OMX_Usebuffer failed {:#x}", error);
1996 input_bufs_omx_mutex.unlock();
1999 error=OMX_AllocateBuffer(omx_vid_dec,&buf_head,omx_codec_input_port,NULL,port_def_type.nBufferSize);
2000 if (error!=OMX_ErrorNone){
2001 logger->debug(TAG, "Use OMX_AllocateBuffer failed {:#x}", error);
2002 input_bufs_omx_mutex.unlock();
2005 input_bufs_omx_all.push_back(buf_head);
2006 input_bufs_omx_free.push_back(buf_head);
2008 omx_first_frame=true;
2011 cur_input_buf_omx=NULL;
2012 input_bufs_omx_mutex.unlock();
2015 logger->debug(TAG, "PrepareInputBufsOMX mark3");
2016 if (!CommandFinished(omx_vid_dec,OMX_CommandPortEnable,omx_codec_input_port)) {
2019 logger->debug(TAG, "PrepareInputBufsOMX mark4");
2024 int VideoOMX::DestroyInputBufsOMX() //need s to be called with locked mutex
2026 OMX_ERRORTYPE error;
2028 cur_input_buf_omx=NULL;
2029 input_bufs_omx_mutex.lock();
2030 for (UINT i=0; i< input_bufs_omx_all.size();i++) {
2031 // free(input_bufs_omx_all[i]->pBuffer);
2032 // input_bufs_omx_all[i]->pBuffer=NULL;
2033 error=OMX_FreeBuffer(omx_vid_dec,omx_codec_input_port,input_bufs_omx_all[i]);
2034 if (error!=OMX_ErrorNone){
2035 logger->debug(TAG, "Use OMX_FreeBuffer failed {:#x}", error);
2036 input_bufs_omx_mutex.unlock();
2041 input_bufs_omx_all.clear();
2042 input_bufs_omx_free.clear();
2043 input_bufs_omx_mutex.unlock();
2050 int VideoOMX::FlushRenderingPipe()
2052 OMX_ERRORTYPE error;
2056 error = OMX_SendCommand(omx_vid_dec, OMX_CommandFlush,
2057 omx_codec_output_port, NULL);
2058 if (error != OMX_ErrorNone) {
2059 LogNT::getInstance()->debug(TAG,
2060 "OMX_Flush codec out 1 failed {:#x}", error);
2064 error = OMX_SendCommand(omx_vid_sched, OMX_CommandFlush,
2065 omx_shed_input_port, NULL);
2066 if (error != OMX_ErrorNone) {
2067 LogNT::getInstance()->debug(TAG,
2068 "OMX_Flush shed in 2 failed {:#x}", error);
2072 if (!CommandFinished(omx_vid_dec, OMX_CommandFlush,
2073 omx_codec_output_port)) {
2074 LogNT::getInstance()->debug(TAG,
2075 "flush cmd codec 3 failed");
2078 if (!CommandFinished(omx_vid_sched, OMX_CommandFlush,
2079 omx_shed_input_port)) {
2080 LogNT::getInstance()->debug(TAG,
2081 "flush cmd shed 4 failed");
2084 error = OMX_SendCommand(omx_vid_dec, OMX_CommandFlush,
2085 omx_codec_output_port, NULL);
2086 if (error != OMX_ErrorNone) {
2087 LogNT::getInstance()->debug(TAG,
2088 "OMX_Flush codec out 5 failed {:#x}", error);
2092 error = OMX_SendCommand(omx_vid_deint, OMX_CommandFlush,
2093 omx_deint_input_port, NULL);
2094 if (error != OMX_ErrorNone) {
2095 LogNT::getInstance()->debug(TAG,
2096 "OMX_Flush deint in 6 failed {:#x}", error);
2100 if (!CommandFinished(omx_vid_dec, OMX_CommandFlush,
2101 omx_codec_output_port)) {
2102 LogNT::getInstance()->debug(TAG,
2103 "flush cmd codec 7 failed");
2106 if (!CommandFinished(omx_vid_deint, OMX_CommandFlush,
2107 omx_deint_input_port)) {
2108 LogNT::getInstance()->debug(TAG,
2109 "flush cmd deint 8 failed");
2113 error = OMX_SendCommand(omx_vid_deint, OMX_CommandFlush,
2114 omx_deint_output_port, NULL);
2115 if (error != OMX_ErrorNone) {
2116 LogNT::getInstance()->debug(TAG,
2117 "OMX_Flush deint out 9 failed {:#x}", error);
2121 error = OMX_SendCommand(omx_vid_sched, OMX_CommandFlush,
2122 omx_shed_input_port, NULL);
2123 if (error != OMX_ErrorNone) {
2124 LogNT::getInstance()->debug(TAG,
2125 "OMX_Flush shed in 10 failed {:#x}", error);
2129 if (!CommandFinished(omx_vid_deint, OMX_CommandFlush,
2130 omx_deint_output_port)) {
2131 LogNT::getInstance()->debug(TAG,
2132 "flush cmd deint 11 failed");
2135 if (!CommandFinished(omx_vid_sched, OMX_CommandFlush,
2136 omx_shed_input_port)) {
2137 LogNT::getInstance()->debug(TAG,
2138 "flush cmd shed 12 failed");
2146 error = OMX_SendCommand(omx_vid_rend, OMX_CommandFlush,
2147 omx_rend_input_port, NULL);
2148 if (error != OMX_ErrorNone) {
2149 LogNT::getInstance()->debug(TAG,
2150 "OMX_Flush rend in failed {:#x}", error);
2154 error = OMX_SendCommand(omx_vid_sched, OMX_CommandFlush,
2155 omx_shed_output_port, NULL);
2156 if (error != OMX_ErrorNone) {
2157 LogNT::getInstance()->debug(TAG,
2158 "OMX_Flush shed out failed {:#x}", error);
2164 if (!CommandFinished(omx_vid_rend, OMX_CommandFlush,
2165 omx_rend_input_port)) {
2166 LogNT::getInstance()->debug(TAG,
2167 "flush cmd shed rend failed");
2170 if (!CommandFinished(omx_vid_sched, OMX_CommandFlush,
2171 omx_shed_output_port)) {
2172 LogNT::getInstance()->debug(TAG,
2173 "flush cmd shed rend failed");
2180 int VideoOMX::DeAllocateCodecsOMX()
2182 OMX_ERRORTYPE error;
2184 logger->debug(TAG, "enter deallocatecodecsomx");
2186 if (cur_input_buf_omx) {
2187 cur_input_buf_omx->nFlags|=OMX_BUFFERFLAG_EOS;
2188 error=ProtOMXEmptyThisBuffer(omx_vid_dec,cur_input_buf_omx);
2189 if (error!=OMX_ErrorNone) {
2190 logger->debug(TAG, "OMX_EmptyThisBuffer failed {:#x}", error);
2193 cur_input_buf_omx=NULL;//write out old data
2198 // first stop the omx elements
2199 if (!ChangeComponentState(omx_vid_dec,OMX_StateIdle)) {
2200 logger->debug(TAG, "vid_dec ChangeComponentState");
2203 clock_mutex.unlock();
2209 if (!ChangeComponentState(omx_vid_deint, OMX_StateIdle)) {
2210 LogNT::getInstance()->debug(TAG,
2211 "vid_deint ChangeComponentState");
2217 if (!ChangeComponentState(omx_vid_sched,OMX_StateIdle)) {
2218 logger->debug(TAG, "vid_shed ChangeComponentState");
2222 if (!ChangeComponentState(omx_vid_rend,OMX_StateIdle)) {
2223 logger->debug(TAG, "vid_rend ChangeComponentState");
2229 // TODO proper deinit sequence
2230 // first flush all buffers
2231 FlushRenderingPipe();
2237 error=OMX_SendCommand(omx_clock,OMX_CommandFlush, omx_clock_output_port, NULL);
2238 if (error!=OMX_ErrorNone){
2239 logger->debug(TAG, "OMX_Flush clock out failed {:#x}", error);
2243 error=OMX_SendCommand(omx_vid_sched,OMX_CommandFlush, omx_shed_clock_port, NULL);
2244 if (error!=OMX_ErrorNone){
2245 logger->debug(TAG, "OMX_Flush shed clock failed {:#x}", error);
2249 if (!CommandFinished(omx_clock,OMX_CommandFlush,omx_clock_output_port) ||
2250 !CommandFinished(omx_vid_sched,OMX_CommandFlush,omx_shed_clock_port)) {
2251 logger->debug(TAG, "flush cmd clock shed failed");
2257 error = OMX_SendCommand(omx_vid_dec, OMX_CommandFlush,
2258 omx_codec_input_port, NULL);
2259 if (error != OMX_ErrorNone) {
2260 LogNT::getInstance()->debug(TAG,
2261 "OMX_Flush codec out failed {:#x}", error);
2268 DestroyInputBufsOMX();
2271 if (!DisablePort(omx_vid_sched,omx_shed_output_port,true)) {
2272 logger->debug(TAG, "Disable Tunnel Port failed 2");
2274 if (!DisablePort(omx_vid_rend,omx_rend_input_port,true)) {
2275 logger->debug(TAG, "Disable Tunnel Port failed 1");
2281 if (!DisablePort(omx_vid_dec,omx_codec_output_port,true)) {
2282 logger->debug(TAG, "Disable Tunnel Port failed 6");
2287 if (!DisablePort(omx_vid_dec,omx_codec_input_port,true)) {
2288 logger->debug(TAG, "Disable Tunnel Port failed 7");
2292 if (!DisablePort(omx_vid_deint,omx_deint_output_port,true)) {
2293 logger->debug(TAG, "Disable Tunnel Port failed 6a");
2298 if (!DisablePort(omx_vid_deint,omx_deint_input_port,true)) {
2299 logger->debug(TAG, "Disable Tunnel Port failed 7a");
2305 if (!DisablePort(omx_vid_sched,omx_shed_input_port,true)) {
2306 logger->debug(TAG, "Disable Tunnel Port failed 3");
2309 if (!DisablePort(omx_vid_sched,omx_shed_clock_port,true)) {
2310 logger->debug(TAG, "Disable Tunnel Port failed 4");
2313 if (!DisablePort(omx_clock,omx_clock_output_port,true)) {
2314 logger->debug(TAG, "Disable Tunnel Port failed 5");
2320 error=OMX_SetupTunnel(omx_vid_dec,omx_codec_output_port,NULL,0);
2321 if (error!=OMX_ErrorNone) {
2322 logger->debug(TAG, "OMX_Setup tunnel teardown failed {:#x}", error);
2327 error=OMX_SetupTunnel(omx_vid_deint,omx_deint_input_port,NULL,0);
2328 if (error!=OMX_ErrorNone) {
2329 logger->debug(TAG, "OMX_Setup tunnel teardown failed {:#x}", error);
2333 error=OMX_SetupTunnel(omx_vid_deint,omx_deint_output_port,NULL,0);
2334 if (error!=OMX_ErrorNone) {
2335 logger->debug(TAG, "OMX_Setup tunnel teardown failed {:#x}", error);
2339 error=OMX_SetupTunnel(omx_vid_sched,omx_shed_input_port,NULL,0);
2340 if (error!=OMX_ErrorNone) {
2341 logger->debug(TAG, "OMX_Setup tunnel teardown failed {:#x}", error);
2346 error=OMX_SetupTunnel(omx_vid_sched,omx_shed_output_port,NULL,0);
2347 if (error!=OMX_ErrorNone) {
2348 logger->debug(TAG, "OMX_Setup tunnel teardown failed {:#x}", error);
2352 error=OMX_SetupTunnel(omx_vid_rend,omx_rend_input_port,NULL,0);
2353 if (error!=OMX_ErrorNone) {
2354 logger->debug(TAG, "OMX_Setup tunnel teardown failed {:#x}", error);
2359 error=OMX_SetupTunnel(omx_clock,omx_clock_output_port,NULL,0);
2360 if (error!=OMX_ErrorNone) {
2361 logger->debug(TAG, "OMX_Setup tunnel teardown failed {:#x}", error);
2365 error=OMX_SetupTunnel(omx_vid_sched,omx_shed_clock_port,NULL,0);
2366 if (error!=OMX_ErrorNone) {
2367 logger->debug(TAG, "OMX_Setup tunnel teardown failed {:#x}", error);
2374 error=OMX_FreeHandle(omx_vid_dec);
2375 error=OMX_FreeHandle(omx_vid_sched);
2376 error=OMX_FreeHandle(omx_vid_rend);
2377 if (dodeint) error=OMX_FreeHandle(omx_vid_deint);
2379 clock_mutex.unlock();
2381 if (error!=OMX_ErrorNone) {
2382 logger->debug(TAG, "FreeHandle failed {}", error);
2384 } else clock_mutex.unlock();
2385 logger->debug(TAG, "leave deallocate codecs OMX");
2391 void VideoOMX::destroyClock()
2394 if (clock_references>0) {
2396 if (clock_references==0) {
2397 OMX_ERRORTYPE error;
2398 logger->debug(TAG, "destroy omx clock");
2399 error=OMX_FreeHandle(omx_clock);
2400 if (error!=OMX_ErrorNone) {
2401 logger->debug(TAG, "FreeHandle failed {}", error);
2406 clock_mutex.unlock();
2410 int VideoOMX::stop()
2412 if (!initted) return 0;
2415 //Check if libav mode
2416 DeAllocateCodecsOMX();
2421 // if (ioctl(fdVideo, AV_SET_VID_STOP, 0) != 0) return 0;
2425 int VideoOMX::reset()
2427 if (!initted) return 0;
2430 DeAllocateCodecsOMX();
2431 // if (ioctl(fdVideo, AV_SET_VID_RESET, 0x11) != 0) return 0;
2435 int VideoOMX::pause()
2437 if (!initted) return 0;
2438 logger->debug(TAG, "enter pause");
2440 // ignore it audio handles this
2444 int VideoOMX::unPause() // FIXME get rid - same as play!! Not here!
2446 if (!initted) return 0;
2447 logger->debug(TAG, "enter unpause");
2450 //ignore it audio handles this
2455 int VideoOMX::fastForward()
2457 if (!initted) return 0;
2459 // if (ioctl(fdVideo, AV_SET_VID_libavWD, 1) != 0) return 0;
2463 int VideoOMX::unFastForward()
2465 if (!initted) return 0;
2467 // if (ioctl(fdVideo, AV_SET_VID_RESET, 0x11) != 0) return 0; // don't need this.
2469 //// if (ioctl(fdVideo, AV_SET_VID_PLAY, 0) != 0) return 0;
2473 int VideoOMX::attachFrameBuffer()
2475 if (!initted) return 0;
2477 // if (ioctl(fdVideo, AV_SET_VID_FB, 0) != 0) return 0;
2481 int VideoOMX::blank(void)
2483 // if (ioctl(fdVideo, AV_SET_VID_FB, 1) != 0) return 0;
2484 // if (ioctl(fdVideo, AV_SET_VID_FB, 0) != 0) return 0;
2488 ULLONG VideoOMX::getCurrentTimestamp() {
2491 long long ncur_clock_time = cur_clock_time;
2495 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldcancelstate);
2496 pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldcanceltype);
2498 OMX_ERRORTYPE error;
2499 OMX_TIME_CONFIG_CLOCKSTATETYPE clock_conf;
2500 memset(&clock_conf, 0, sizeof(clock_conf));
2501 clock_conf.nSize = sizeof(clock_conf);
2502 clock_conf.nVersion.nVersion = OMX_VERSION;
2503 error= OMX_GetConfig(omx_clock, OMX_IndexConfigTimeClockState,
2505 if (error != OMX_ErrorNone) {
2506 LogNT::getInstance()->debug(TAG,"getCurrentTimestamp IndexConfigTimeClockState failed {:#x}",error);
2509 if (clock_conf.eState == OMX_TIME_ClockStateRunning) {
2511 OMX_TIME_CONFIG_TIMESTAMPTYPE cur_time_stamp;
2512 memset(&cur_time_stamp, 0, sizeof(cur_time_stamp));
2513 cur_time_stamp.nSize = sizeof(cur_time_stamp);
2514 cur_time_stamp.nVersion.nVersion = OMX_VERSION;
2515 cur_time_stamp.nPortIndex = omx_clock_output_port;
2516 error = OMX_GetConfig(omx_clock, OMX_IndexConfigTimeCurrentMediaTime,
2518 if (error != OMX_ErrorNone) {
2519 LogNT::getInstance()->debug(TAG, "getCurrentTimestamp OMX_IndexConfigTimeCurrentMediaTime failed {:#x}",error);
2521 long long temp = cur_time_stamp.nTimestamp.nLowPart
2522 | ((long long) cur_time_stamp.nTimestamp.nHighPart << 32);
2523 ncur_clock_time = cur_clock_time = temp * 10LL;
2526 clock_mutex.unlock();
2527 pthread_setcancelstate(oldcancelstate, NULL);
2528 pthread_setcanceltype(oldcanceltype, NULL);
2531 //ncur_clock_time -= startoffset;
2532 ncur_clock_time -= lastreftimeOMX;
2533 long long result = lastreftimePTS;
2534 if (lastreftimePTS==0) return 0; // invalid time
2535 result += (long long) (ncur_clock_time / 10000LL * 90LL);
2537 result = (1LL << 33) - result;
2538 //LogNT::getInstance()->debug(TAG,"getCurrentTimestamp {} {} {} {} {} {}",ncur_clock_time,cur_clock_time,lastreftimeOMX,lastreftimePTS,result,startoffset);
2546 ULONG VideoOMX::timecodeToFrameNumber(ULLONG timecode)
2548 if (format == PAL) return (ULONG)(((double)timecode / (double)90000) * (double)25);
2549 else return (ULONG)(((double)timecode / (double)90000) * (double)30);
2554 int VideoOMX::test()
2559 // return ioctl(fdVideo, AV_SET_VID_STC, &stc);
2566 int VideoOMX::test2()
2574 long long VideoOMX::SetStartOffset(long long curreftime, bool *rsync)
2578 startoffset=curreftime;//offset is set for audio
2580 offsetvideonotset=false;
2582 if (offsetvideonotset) {
2583 offsetvideonotset=false;
2586 if ( (curreftime-lastrefvideotime)>10000000LL
2587 || (curreftime-lastrefvideotime)<-10000000LL) {//if pts jumps to big resync
2588 startoffset+=curreftime-lastrefvideotime;
2589 lastrefaudiotime+=curreftime-lastrefvideotime;
2591 offsetaudionotset=true;
2598 lastrefvideotime=curreftime;
2604 long long VideoOMX::SetStartAudioOffset(long long curreftime, bool *rsync)
2608 startoffset=curreftime;
2610 offsetaudionotset=false;
2612 if (offsetaudionotset) {
2613 offsetaudionotset=false;
2616 if ( (curreftime-lastrefaudiotime)>10000000LL
2617 || (curreftime-lastrefaudiotime)<-10000000LL) {//if pts jumps to big resync
2618 startoffset+=curreftime-lastrefaudiotime;
2619 lastrefvideotime+=curreftime-lastrefaudiotime;
2621 offsetvideonotset=true;
2627 lastrefaudiotime=curreftime;
2632 void VideoOMX::ResetTimeOffsets() {
2633 offsetnotset=true; //called from demuxer
2634 offsetvideonotset=true;
2635 offsetaudionotset=true;
2644 void VideoOMX::FirstFrameFix()
2648 Demuxer* demux=Demuxer::getInstance();
2649 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldcancelstate);
2650 pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldcanceltype);
2652 if (WaitForEvent(omx_vid_dec,OMX_EventPortSettingsChanged,0)){
2653 WaitForEvent(omx_vid_deint,OMX_EventPortSettingsChanged,0); //clear old messages
2654 OMX_ERRORTYPE error;
2655 OMX_PARAM_PORTDEFINITIONTYPE port_def_type;
2656 memset(&port_def_type,0,sizeof(port_def_type));
2657 port_def_type.nSize=sizeof(port_def_type);
2658 port_def_type.nVersion.nVersion=OMX_VERSION;
2659 port_def_type.nPortIndex=omx_codec_output_port;
2661 error=OMX_GetParameter(omx_vid_dec,OMX_IndexParamPortDefinition, &port_def_type);
2662 if (error != OMX_ErrorNone) {
2663 LogNT::getInstance()->debug(TAG,
2664 "OMX_IndexParamPortDefinition fix failed {:#x}", error);
2665 clock_mutex.unlock();
2670 LogNT::getInstance()->debug(TAG,
2671 "Deinit first frame fix {} {} {} {} {} {} {} {}",port_def_type.format.video.nFrameWidth , demux->getHorizontalSize(),
2672 port_def_type.format.video.nFrameHeight , demux->getVerticalSize(),port_def_type.format.video.nStride,
2673 port_def_type.format.video.nSliceHeight, port_def_type.format.video.xFramerate,
2674 port_def_type.format.video.bFlagErrorConcealment );
2675 LogNT::getInstance()->debug(TAG,
2676 "Deinit first frame fix2 {} {}",
2677 port_def_type.format.video.eCompressionFormat ,
2678 port_def_type.format.video.eColorFormat );
2681 // we cause the video_decode to determine the interlacing properties
2682 OMX_CONFIG_INTERLACETYPE il;
2683 memset(&il,0,sizeof(il));
2684 il.nSize=sizeof(il);
2685 il.nVersion.nVersion=OMX_VERSION;
2686 il.nPortIndex=omx_codec_output_port;
2687 error=OMX_GetConfig(omx_vid_dec,OMX_IndexConfigCommonInterlace, &il);
2688 if (error != OMX_ErrorNone) {
2689 LogNT::getInstance()->debug(TAG,
2690 "OMX_IndexConfigCommonInterlace fix failed {:#x}", error);
2694 DisablePort(omx_vid_dec,omx_codec_output_port,true);
2695 DisablePort(omx_vid_sched,omx_shed_input_port,true);
2698 DisablePort(omx_vid_deint,omx_deint_input_port,true);
2699 // This is a dirty hack
2700 DisablePort(omx_vid_deint,omx_deint_output_port,true);
2703 port_def_type.nPortIndex=omx_deint_output_port;
2704 error = OMX_SetParameter(omx_vid_deint, OMX_IndexParamPortDefinition,
2706 if (error != OMX_ErrorNone) {
2707 LogNT::getInstance()->debug(TAG,
2708 "Set OMX_IndexParamPortDefinition1 failed {:#x}", error);
2709 clock_mutex.unlock();
2710 pthread_setcancelstate(oldcancelstate, NULL);
2711 pthread_setcanceltype(oldcanceltype, NULL);
2717 port_def_type.nPortIndex=omx_deint_input_port;
2718 error = OMX_SetParameter(omx_vid_deint, OMX_IndexParamPortDefinition,
2720 if (error != OMX_ErrorNone) {
2721 LogNT::getInstance()->debug(TAG,
2722 "Set OMX_IndexParamPortDefinition1 failed {:#x}", error);
2723 clock_mutex.unlock();
2724 pthread_setcancelstate(oldcancelstate, NULL);
2725 pthread_setcanceltype(oldcanceltype, NULL);
2728 // WaitForEvent(omx_vid_dec,OMX_EventPortSettingsChanged);
2730 LogNT::getInstance()->debug(TAG,
2732 EnablePort(omx_vid_deint,omx_deint_input_port,true);
2733 WaitForEvent(omx_vid_deint,OMX_EventPortSettingsChanged);
2734 port_def_type.nPortIndex=omx_deint_output_port;
2735 error = OMX_GetParameter(omx_vid_deint, OMX_IndexParamPortDefinition,
2737 if (error != OMX_ErrorNone) {
2738 LogNT::getInstance()->debug(TAG,
2739 "Get OMX_IndexParamPortDefinition2 failed {:#x}", error);
2740 clock_mutex.unlock();
2741 pthread_setcancelstate(oldcancelstate, NULL);
2742 pthread_setcanceltype(oldcanceltype, NULL);
2745 LogNT::getInstance()->debug(TAG,
2746 "Deinit first frame fix3 {} {} {} {} {} {} {} ",port_def_type.format.image.nFrameWidth , demux->getHorizontalSize(),
2747 port_def_type.format.image.nFrameHeight , demux->getVerticalSize(),port_def_type.format.image.nStride,
2748 port_def_type.format.image.nSliceHeight, /*port_def_type.format.image.xFramerate,*/
2749 port_def_type.format.image.bFlagErrorConcealment );
2750 LogNT::getInstance()->debug(TAG,
2751 "Deinit first frame fix4 {} {}",
2752 port_def_type.format.image.eCompressionFormat ,
2753 port_def_type.format.image.eColorFormat );
2754 DisablePort(omx_vid_deint,omx_deint_output_port,true);
2757 port_def_type.nPortIndex=omx_shed_input_port;
2758 error = OMX_SetParameter(omx_vid_sched, OMX_IndexParamPortDefinition,
2760 if (error != OMX_ErrorNone) {
2761 LogNT::getInstance()->debug(TAG,
2762 "Set OMX_IndexParamPortDefinition3 failed {:#x}", error);
2763 clock_mutex.unlock();
2764 pthread_setcancelstate(oldcancelstate, NULL);
2765 pthread_setcanceltype(oldcanceltype, NULL);
2768 WaitForEvent(omx_vid_sched,OMX_EventPortSettingsChanged);
2772 EnablePort(omx_vid_deint,omx_deint_output_port,true);
2774 EnablePort(omx_vid_dec,omx_codec_output_port,true);
2775 EnablePort(omx_vid_sched,omx_shed_input_port,true);
2777 clock_mutex.unlock();
2778 pthread_setcancelstate(oldcancelstate, NULL);
2779 pthread_setcanceltype(oldcanceltype, NULL);
2786 void VideoOMX::PutBufferToPres(OMX_BUFFERHEADERTYPE* buffer)
2789 OMX_ERRORTYPE error = ProtOMXEmptyThisBuffer(omx_vid_dec, buffer);
2790 if (error != OMX_ErrorNone) {
2791 LogNT::getInstance()->debug(TAG,
2792 "OMX_EmptyThisBuffer failed {:#x}", error);
2794 if (first_frame) FirstFrameFix();
2798 OMX_ERRORTYPE VideoOMX::ProtOMXEmptyThisBuffer(OMX_HANDLETYPE handle, OMX_BUFFERHEADERTYPE* buffer)
2800 // protect the call to empty this buffer
2803 /* long long temp =buffer->nTimeStamp.nLowPart
2804 | ((long long) buffer->nTimeStamp.nHighPart << 32);*/
2806 //pthread_testcancel();
2807 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldcancelstate);
2808 pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldcanceltype);
2811 /* OMX_ERRORTYPE error;
2812 OMX_TIME_CONFIG_TIMESTAMPTYPE timestamp;
2813 memset(×tamp, 0, sizeof(timestamp));
2814 timestamp.nSize = sizeof(timestamp);
2815 timestamp.nVersion.nVersion = OMX_VERSION;
2816 timestamp.nPortIndex =omx_clock_output_port;
2818 error = OMX_GetConfig(omx_clock, OMX_IndexConfigTimeCurrentMediaTime,
2821 if (error != OMX_ErrorNone) {
2822 LogNT::getInstance()->debug(TAG,
2823 "Init OMX_IndexConfigAudioRenderingLatencyfailed {:#x} {}", error,
2824 omx_rend_input_port);
2826 long long temp2 =timestamp.nTimestamp.nLowPart
2827 | ((long long) timestamp.nTimestamp.nHighPart << 32);
2830 LogNT::getInstance()->info(TAG, "OMXETB {:#x} {} {} {:#x}",handle,temp,temp2,buffer->nFlags);*/
2831 OMX_ERRORTYPE ret_val;
2832 ret_val=OMX_EmptyThisBuffer(handle,buffer);
2833 clock_mutex.unlock();
2834 pthread_setcancelstate(oldcancelstate, NULL);
2835 pthread_setcanceltype(oldcanceltype, NULL);
2836 //pthread_testcancel();
2840 bool VideoOMX::detectIFrame(const UCHAR *buffer,unsigned int length)
2842 const UCHAR* curbuf=buffer;
2843 const UCHAR* curbufend=buffer+length;
2844 unsigned int detector=0xFFFFFFFF;
2845 bool gotaud=false; // have seen access unit delimiter
2847 while (curbuf!=curbufend) {
2851 if (detector==0x00000109) {
2853 detector=0xFFFFFFFF;
2854 } else if (gotaud &&detector==0x00000001) {
2856 if (curbuf!=curbufend) {
2857 unsigned char picttype=(*curbuf)& 0x1F;
2858 return picttype==0x07;
2862 if (detector==0x00000100) {
2864 if (curbuf==curbufend) return false;
2866 if (curbuf==curbufend) return false;
2867 unsigned char picttype=((*curbuf) >> 3) & 0x07;
2874 return false; // no frame found
2879 bool VideoOMX::DrainTargetBufferFull()
2881 //Check, if we have OMX output buffers
2883 input_bufs_omx_mutex.lock();
2884 full=(input_bufs_omx_free.size()==0);
2885 input_bufs_omx_mutex.unlock();
2886 checkForStalledBuffers(); // check if the decoder has a problem
2887 if (full && omx_vid_stalled && !omx_first_frame) {
2888 omx_vid_stalled=false;
2889 logger->debug(TAG, "Decoder is stalled, do a reset!");
2893 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldcancelstate);
2894 pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldcanceltype);
2897 FlushRenderingPipe();
2898 omx_first_frame=true;
2899 clock_mutex.unlock();
2901 pthread_setcancelstate(oldcancelstate, NULL);
2902 pthread_setcanceltype(oldcanceltype, NULL);
2908 void VideoOMX::PrepareMediaSample(const MediaPacketList& mplist, UINT /* samplepos */)
2911 mediapackets.clear();
2912 std::list<MediaPacket>::const_iterator begin=mplist.begin();
2913 std::list<MediaPacket>::const_iterator itty=mplist.begin();
2914 advance(itty,min(mplist.size(),10));
2915 mediapackets.insert(mediapackets.begin(),begin,itty);//front
2919 UINT VideoOMX::DeliverMediaSample(UCHAR* buffer, UINT *samplepos)
2922 while (consumed < mediapackets.size()) {
2923 DeliverMediaPacket(mediapackets[consumed], buffer, samplepos);
2924 if (*samplepos == mediapackets[consumed].length) {
2928 } else return consumed;
2933 UINT VideoOMX::DeliverMediaPacket(MediaPacket packet,
2934 const UCHAR* buffer,
2937 if (packet.type == MPTYPE_VIDEO_H264)
2947 //Later add fail back code for libav
2949 *samplepos+=packet.length;
2950 return packet.length;
2954 if (!omx_running) return 0; // if we are not runnig do not do this
2956 if (isClockPaused()) return 0; //Block if we pause
2958 // logger->debug(TAG, "DMP mark 1");
2959 //logger->debug(TAG, "DeliverMediaPacketOMX time {}", packet.presentation_time);
2962 /*First Check, if we have an video sample*/
2966 return 0; //Not in iframe mode!
2970 if (packet.disconti) {
2972 if (cur_input_buf_omx) {
2973 PutBufferToPres(cur_input_buf_omx);
2974 cur_input_buf_omx=NULL;
2977 //logger->debug(TAG, "DMP mark 2");
2978 /*Inspect PES-Header */
2980 // OMX_STATETYPE temp_state;
2981 // OMX_GetState(omx_vid_dec,&temp_state);
2983 if (*samplepos==0) {//stripheader
2984 headerstrip=buffer[packet.pos_buffer+8]+9/*is this right*/;
2985 // if (h264) logger->debug(TAG, "PES info {:#x} {:#x} {:#x} {:#x}",
2986 // buffer[packet.pos_buffer+0],buffer[packet.pos_buffer+1],buffer[packet.pos_buffer+2],buffer[packet.pos_buffer+3]);
2987 *samplepos+=headerstrip;
2988 if (headerstrip>=packet.length) {
2989 *samplepos=packet.length;// Packet is obviously damaged
2990 return packet.length;//skip it!
2992 if ( packet.synched ) {
2993 if (!firstsynched) {
2994 //logger->debug(TAG, "DMP mark 2a");
2995 // check if this is an I frame, the decoder does not like non I frames at startup!
2996 if (!detectIFrame(buffer,packet.length)) {
2997 *samplepos=packet.length;//if we have not processed at least one
2998 //logger->debug(TAG, "DMP mark 3");
2999 return packet.length;//synched packet ignore it!
3002 if (cur_input_buf_omx) {
3003 //logger->debug(TAG, "DMP mark 4a");
3004 cur_input_buf_omx->nFlags|=OMX_BUFFERFLAG_ENDOFFRAME;
3005 PutBufferToPres(cur_input_buf_omx);
3006 cur_input_buf_omx=NULL;//write out old data
3008 //logger->debug(TAG, "DMP mark 4b");
3013 if (!firstsynched) {//
3014 *samplepos=packet.length;//if we have not processed at least one
3015 //logger->debug(TAG, "DMP mark 5");
3016 return packet.length;//synched packet ignore it!
3020 //logger->debug(TAG, "DMP mark 6");
3021 if (!cur_input_buf_omx) {
3022 input_bufs_omx_mutex.lock();
3023 if (input_bufs_omx_free.size()==0) {
3024 input_bufs_omx_mutex.unlock();
3025 //logger->debug(TAG, "Deliver MediaPacket no free sample");
3026 //logger->debug(TAG, "DMP mark 7");
3027 return 0; // we do not have a free media sample
3030 cur_input_buf_omx=input_bufs_omx_free.front();
3031 cur_input_buf_omx->nFilledLen=0;
3032 cur_input_buf_omx->nOffset=0;
3033 cur_input_buf_omx->nTimeStamp=intToOMXTicks(0);
3034 input_bufs_omx_free.pop_front();
3035 input_bufs_omx_mutex.unlock();
3041 if (cur_input_buf_omx->nFilledLen==0) {//will only be changed on first packet
3042 if (packet.synched) {
3043 // logger->debug(TAG, "packet synched marker");
3045 //lastreftimePTS=packet.pts;
3046 if (omx_first_frame) { // TODO time
3047 cur_input_buf_omx->nFlags=OMX_BUFFERFLAG_STARTTIME;
3048 logger->debug(TAG, "Starttime");
3049 omx_first_frame=false;
3051 cur_input_buf_omx->nFlags=0;
3052 //cur_input_buf_omx->nFlags|=OMX_BUFFERFLAG_TIME_UNKNOWN;
3054 lastreftimeOMX=packet.presentation_time;
3055 // logger->debug(TAG, "Time code {} pts {}", lastreftimeOMX,packet.pts);
3056 lastreftimePTS=packet.pts;
3057 cur_input_buf_omx->nTimeStamp=intToOMXTicks(lastreftimeOMX/10LL); // the clock component is faulty;
3061 cur_input_buf_omx->nFlags=OMX_BUFFERFLAG_TIME_UNKNOWN;
3062 cur_input_buf_omx->nTimeStamp=intToOMXTicks(0);
3063 //logger->debug(TAG, "packet unsynched marker");
3065 if (packet.disconti) cur_input_buf_omx->nFlags|=OMX_BUFFERFLAG_DISCONTINUITY;
3070 unsigned int haveToCopy=packet.length-*samplepos;
3071 //logger->debug(TAG, "DMP mark 8");
3073 while (haveToCopy> (cur_input_buf_omx->nAllocLen-cur_input_buf_omx->nFilledLen)) {
3074 //logger->debug(TAG, "Big buffer {} {} {}",packet.length,cur_input_buf_omx->nAllocLen,cur_input_buf_omx->nFilledLen);
3075 unsigned int cancopy=cur_input_buf_omx->nAllocLen-cur_input_buf_omx->nFilledLen;
3076 memcpy(cur_input_buf_omx->pBuffer+cur_input_buf_omx->nFilledLen,buffer+packet.pos_buffer+*samplepos,cancopy);
3077 haveToCopy-=cancopy;
3078 cur_input_buf_omx->nFilledLen+=cancopy;
3079 *samplepos+=cancopy;
3080 // push old buffer out
3081 //logger->debug(TAG, "DMP mark 9");
3083 PutBufferToPres(cur_input_buf_omx);
3084 cur_input_buf_omx=NULL;
3086 input_bufs_omx_mutex.lock();
3087 if (input_bufs_omx_free.size()==0) {
3088 input_bufs_omx_mutex.unlock();
3089 // logger->debug(TAG, "Deliver MediaPacket no free sample2");
3090 return *samplepos; // we do not have a free media sample
3092 cur_input_buf_omx=input_bufs_omx_free.front();
3093 cur_input_buf_omx->nFilledLen=0;
3094 cur_input_buf_omx->nOffset=0;
3095 cur_input_buf_omx->nFlags=OMX_BUFFERFLAG_TIME_UNKNOWN;
3096 cur_input_buf_omx->nTimeStamp=intToOMXTicks(0);
3097 input_bufs_omx_free.pop_front();
3098 input_bufs_omx_mutex.unlock();
3099 //logger->debug(TAG, "DMP mark 10");
3102 memcpy(cur_input_buf_omx->pBuffer+cur_input_buf_omx->nFilledLen,
3103 buffer+packet.pos_buffer+*samplepos,haveToCopy);
3104 cur_input_buf_omx->nFilledLen+=haveToCopy;
3106 // logger->debug(TAG, "DMP mark 11");
3108 *samplepos+=haveToCopy;
3117 bool VideoOMX::displayIFrame(const UCHAR* buffer, UINT length) {
3118 if (!omx_running) return false;
3120 EnterIframePlayback();
3122 //int haveToCopy = length;
3124 if (!cur_input_buf_omx) {
3125 input_bufs_omx_mutex.lock();
3126 if (input_bufs_omx_free.size() == 0) {
3127 input_bufs_omx_mutex.unlock();
3128 // LogNT::getInstance()->debug(TAG,
3129 // "Deliver MediaPacket no free sample");
3130 return false; // we do not have a free media sample
3133 cur_input_buf_omx = input_bufs_omx_free.front();
3134 cur_input_buf_omx->nFilledLen = 0;
3135 cur_input_buf_omx->nOffset = 0;
3136 cur_input_buf_omx->nTimeStamp = intToOMXTicks(0);
3137 input_bufs_omx_free.pop_front();
3138 input_bufs_omx_mutex.unlock();
3142 unsigned int pattern, packet_length;
3143 unsigned int headerstrip = 0;
3148 //Now we strip the pes header
3149 pattern = (buffer[0] << 16) | (buffer[1] << 8) | (buffer[2]);
3150 while ((read_pos + 7) <= length) {
3151 pattern = ((pattern << 8) & 0xFFFFFFFF) | buffer[read_pos + 3];
3152 if (pattern < 0x000001E0 || pattern > 0x000001EF) {
3156 headerstrip = buffer[read_pos + 8] + 9/*is this right*/;
3157 packet_length = ((buffer[read_pos + 4] << 8)
3158 | (buffer[read_pos + 5])) + 6;
3159 if (read_pos + packet_length > length)
3162 if ((headerstrip < packet_length)
3163 && (cur_input_buf_omx->nFilledLen + packet_length
3164 - headerstrip) > cur_input_buf_omx->nAllocLen) {
3166 cur_input_buf_omx->nFlags = OMX_BUFFERFLAG_STARTTIME;
3169 cur_input_buf_omx->nFlags
3170 |= OMX_BUFFERFLAG_TIME_UNKNOWN;
3173 cur_input_buf_omx->nTimeStamp = intToOMXTicks(0);
3174 PutBufferToPres(cur_input_buf_omx);
3175 cur_input_buf_omx = NULL;
3177 if (!cur_input_buf_omx) {
3179 while (count < 100 && omx_running && iframemode) {
3182 input_bufs_omx_mutex.lock();
3183 if (input_bufs_omx_free.size() == 0) {
3184 input_bufs_omx_mutex.unlock();
3185 // LogNT::getInstance()->debug(TAG,
3186 // "Ifrane no free sample");
3188 if (!omx_running) return false;
3191 cur_input_buf_omx = input_bufs_omx_free.front();
3192 cur_input_buf_omx->nFilledLen = 0;
3193 cur_input_buf_omx->nOffset = 0;
3194 cur_input_buf_omx->nTimeStamp = intToOMXTicks(0);
3195 cur_input_buf_omx->nFlags|= OMX_BUFFERFLAG_TIME_UNKNOWN;
3196 input_bufs_omx_free.pop_front();
3197 input_bufs_omx_mutex.unlock();
3200 if (!cur_input_buf_omx)
3205 if (packet_length > headerstrip) {
3207 cur_input_buf_omx->pBuffer
3208 + cur_input_buf_omx->nFilledLen,
3209 buffer + read_pos + headerstrip,
3210 packet_length - headerstrip);
3211 cur_input_buf_omx->nFilledLen += packet_length
3214 read_pos += packet_length;
3216 pattern = (buffer[read_pos] << 16)
3217 | (buffer[read_pos + 1] << 8) | (buffer[read_pos + 2]);
3223 cur_input_buf_omx->nFlags = OMX_BUFFERFLAG_STARTTIME;
3226 cur_input_buf_omx->nFlags |= OMX_BUFFERFLAG_TIME_UNKNOWN;
3229 cur_input_buf_omx->nTimeStamp = intToOMXTicks(0);
3231 PutBufferToPres(cur_input_buf_omx);
3232 cur_input_buf_omx = NULL;
3235 MILLISLEEP(40); //Block a bit
3239 int VideoOMX::EnterIframePlayback()
3241 LogNT::getInstance()->debug(TAG,
3242 "EnterIframePlayback");
3243 if (cur_input_buf_omx) {
3244 PutBufferToPres(cur_input_buf_omx);
3245 cur_input_buf_omx = NULL;
3247 LogNT::getInstance()->debug(TAG,
3248 "EnterIframePlayback 2");
3249 dynamic_cast<AudioOMX*>(Audio::getInstance())->DeAllocateCodecsOMX();
3250 DeAllocateCodecsOMX();
3251 AllocateCodecsOMX();
3252 LogNT::getInstance()->debug(TAG,
3253 "leave IframePlayback");