]> git.vomp.tv Git - vompclient.git/blob - src/videoomx.cc
Add disable-hdmi-modechange to runtime config
[vompclient.git] / src / videoomx.cc
1 /*
2     Copyright 2004-2005 Chris Tallon, 2009-12 Marten Richter
3
4     This file is part of VOMP.
5
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.
10
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.
15
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/>.
18 */
19
20 #include <algorithm>
21 #include <chrono>
22
23 #include <bcm_host.h>
24 #include <linux/fb.h>
25
26
27 #include "log.h"
28 #include "audioomx.h"
29 #include "demuxer.h"
30 #include "vdr.h"
31 #include "woptionpane.h"
32 #include "osdopenvg.h"
33 #include "boxstack.h"
34 #include "inputman.h"
35 #include "util.h"
36 #include "config.h"
37
38 #include "videoomx.h"
39
40 //A lot of parts of this file are heavily inspired by xbmc omx implementations
41
42 static const char* TAG = "VideoOMX";
43
44 VideoOMX::VideoOMX() {
45     logger = LogNT::getInstance();
46
47         omx_running = false;
48
49         omx_vid_dec = 0;
50         cur_input_buf_omx = NULL;
51         omx_h264 = omx_mpeg2 = true;
52         clock_references = 0;
53
54         omx_vid_stalled = false;
55
56         offsetnotset = true;
57         offsetvideonotset = true;
58         offsetaudionotset = true;
59         startoffset = 0;
60         lastrefaudiotime = 0;
61         lastrefvideotime = 0;
62         lastreftimeOMX = 0;
63         lastreftimePTS = 0;
64         firstsynched = false;
65         cur_clock_time=0;
66         mpeg2_supported=false;
67         //cur_pts=0;
68
69         mode=NORMAL;
70         xpos=ypos=0.f;
71         deinterlace=2; //advanced
72
73         signalon=false;
74         outputinterlaced=0;
75
76         strcpy(L_VPE_OMX_CLOCK, VPE_OMX_CLOCK);
77         strcpy(L_VPE_OMX_H264_DECODER, VPE_OMX_H264_DECODER);
78         strcpy(L_VPE_OMX_MPEG2_DECODER, VPE_OMX_MPEG2_DECODER);
79         strcpy(L_VPE_OMX_VIDEO_SCHED, VPE_OMX_VIDEO_SCHED);
80         strcpy(L_VPE_OMX_VIDEO_REND, VPE_OMX_VIDEO_REND);
81         strcpy(L_VPE_OMX_VIDEO_DEINTERLACE, VPE_OMX_VIDEO_DEINTERLACE);
82 }
83
84 VideoOMX::~VideoOMX()
85 {
86   instance = NULL;
87 }
88
89 int VideoOMX::init(u1 tformat)
90 {
91   if (initted) return 0;
92   initted = 1;
93
94   // libcec calls bcm_host_init() - but in case CEC is disabled call it here as well.
95   // Seems safe to call it more than once.
96   bcm_host_init();
97
98   int ret=vc_gencmd_send("codec_enabled MPG2");
99   if (ret!=0) {
100           logger->debug(TAG, "vc_gencmd_send failed {:#x}",ret);
101   } else {
102           char buffer[1024];
103           ret=vc_gencmd_read_response(buffer,sizeof(buffer));
104           if (ret!=0) {
105                   logger->debug(TAG, "vc_gencmd_read_response failed {:#x}",ret);
106           } else {
107                   if (STRCASECMP(buffer,"MPG2=enabled")==0) {
108                           mpeg2_supported=true;
109                   } else if (STRCASECMP(buffer,"MPG2=disabled")==0) {
110                           mpeg2_supported=false;
111                   } else {
112                           logger->debug(TAG, "Undefined mpg codec answer {}",buffer);
113                   }
114           }
115   }
116
117   if (!setFormat(tformat))           { shutdown(); return 0; }
118   if (!setConnection(HDMI))  { shutdown(); return 0; }
119   if (!setAspectRatio(ASPECT4X3,12,11))    { shutdown(); return 0; }
120   if (!setMode(NORMAL))              { shutdown(); return 0; }
121   if (!setSource())                  { shutdown(); return 0; }
122   if (!attachFrameBuffer())          { shutdown(); return 0; }
123
124   setTVsize(ASPECT16X9);
125   selectVideoMode(0);
126
127
128   OMX_ERRORTYPE error;
129         error = OMX_Init();
130         if (error != OMX_ErrorNone) {
131                 logger->debug(TAG, "Init OMX failed {:#x}",
132                                 error);
133                 return 0;
134         }
135
136
137
138
139
140   //stop();
141
142
143
144   return 1;
145 }
146
147
148
149
150 OMX_ERRORTYPE VideoOMX::EventHandler_OMX(OMX_IN OMX_HANDLETYPE handle,OMX_IN OMX_PTR appdata,
151            OMX_IN OMX_EVENTTYPE event_type,OMX_IN OMX_U32 data1,
152            OMX_IN OMX_U32 data2,OMX_IN OMX_PTR event_data) {
153
154         //LogNT::getInstance()->info(TAG, "eventHandler {:#x} {:#x} {:#x} {:#x} {:#x}",handle,event_type,data1,data2,event_data);
155
156         struct VPE_OMX_EVENT  new_event;
157         new_event.handle=handle;
158         new_event.appdata=appdata;
159         new_event.event_type=event_type;
160         new_event.data1=data1;
161         new_event.data2=data2;
162         new_event.event_data=event_data;
163
164         VideoOMX* video = static_cast<VideoOMX *>(Video::getInstance());
165         video->AddOmxEvent(new_event);
166
167 /*      switch (event_type) {
168         case OMX_EventCmdComplete: {
169
170         } break;
171         }*/
172
173         return OMX_ErrorNone;
174
175 }
176
177 void VideoOMX::signalOmx()
178 {
179   /*
180    * Getting rid of Signal class. It looks like VideoOMX uses a wait-on-condition-variable in WaitForEvent()
181    * and CommandFinished(). These methods both use timed waits and don't use exact thread synchronisation -
182    * i.e. a caught signal will end the wait early but a missed signal doesn't matter. So, I'm just copying
183    * in what the Signal class used to do here and I'll sort it out later.
184    * Q: Are the found places the only synchronisation points? Would it be possible to change this to use
185    * exact sychronisation and remove the wait spin loops? Unknown.
186    *
187    * This omx_event_mutex - is this exactly locking the very thing the condition variable is being used
188    * for? i.e. is omx_event_mutex really the mutex that should be being used with the cond var?
189    *
190    * Callers of signalOmx:
191    *
192    * VideoOMX::AddOmxEvent, VideoOMX::ReturnEmptyOMXBuffer
193    * ImageOMX::ReturnEmptyOMXBuffer, ImageOMX::ReturnFillOMXBuffer
194    * AudioOMX::ReturnEmptyOMXBuffer, AudioOMX::FillBufferDone_OMX
195    *
196    * Surprise: WaitForEvent isn't a long running loop while video is playing.
197    */
198
199   omx_event_ready_signal_mutex.lock();
200   omx_event_ready_signal.notify_one(); // Signal called pthread_cond_signal - unblock one
201   omx_event_ready_signal_mutex.unlock();
202 };
203
204 void VideoOMX::AddOmxEvent(VPE_OMX_EVENT  new_event)
205 {
206   omx_event_mutex.lock();
207   omx_events.push_back(new_event);
208   omx_event_mutex.unlock();
209
210   signalOmx();
211 }
212
213 OMX_ERRORTYPE VideoOMX::EmptyBufferDone_OMX(OMX_IN OMX_HANDLETYPE /*hcomp*/, OMX_IN OMX_PTR /*appdata*/, OMX_IN OMX_BUFFERHEADERTYPE* buffer){
214
215 //      LogNT::getInstance()->info(TAG, "EmptyBufferDone");
216         VideoOMX* video = static_cast<VideoOMX *>(Video::getInstance());
217 /*      long long temp =buffer->nTimeStamp.nLowPart
218                                                                 | ((long long) buffer->nTimeStamp.nHighPart << 32);
219         LogNT::getInstance()->info(TAG, "EBD Video %lld  {:#x}",temp,buffer->nFlags);*/
220         video->ReturnEmptyOMXBuffer(buffer);
221         return OMX_ErrorNone;
222
223 }
224
225 void VideoOMX::ReturnEmptyOMXBuffer(OMX_BUFFERHEADERTYPE* buffer){
226         input_bufs_omx_mutex.lock();
227         //LogNT::getInstance()->info(TAG, "ReturnEmptyOMXBuffer {} {}",input_bufs_omx_free.size(),input_bufs_omx_all.size());
228         input_bufs_omx_free.push_back(buffer);
229         //LogNT::getInstance()->info(TAG, "ReturnEmptyOMXBuffer {}",input_bufs_omx_free.size());
230         input_bufs_omx_mutex.unlock();
231
232     signalOmx();
233 }
234
235  OMX_ERRORTYPE VideoOMX::FillBufferDone_OMX(OMX_IN OMX_HANDLETYPE /*hcomp*/, OMX_IN OMX_PTR /*appdata*/, OMX_IN OMX_BUFFERHEADERTYPE* /*buffer*/) {
236          //LogNT::getInstance()->info(TAG, "FillBufferDone");
237         return OMX_ErrorNone;
238 }
239
240
241
242 int VideoOMX::shutdown()
243 {
244   if (!initted) return 0;
245   initted = 0;
246   LogNT::getInstance()->info(TAG, "Shutdown video module");
247
248   DeAllocateCodecsOMX();
249   OMX_Deinit();
250   //vc_tv_show_info(0); // back to console
251   // Restore console
252   int fd_fbset=0;
253   struct fb_var_screeninfo screeninfo;
254   fd_fbset=open("/dev/fb0",O_RDONLY);
255   if (fd_fbset<0) {
256           LogNT::getInstance()->crit(TAG, "Could not open frame buffer device {}", fd_fbset);
257           return 0;
258   }
259   if (ioctl(fd_fbset, FBIOGET_VSCREENINFO, &screeninfo)){
260           close(fd_fbset);
261           LogNT::getInstance()->crit(TAG, "Could not FBIOGET_VSCREENINFO frame buffer device");
262           return 0;
263   }
264   screeninfo.bits_per_pixel=8;
265   if (ioctl(fd_fbset, FBIOPUT_VSCREENINFO, &screeninfo)){
266           LogNT::getInstance()->crit(TAG, "Could not FBIOPUT_VSCREENINFO frame buffer device");
267   }
268   screeninfo.bits_per_pixel=16;
269   if (ioctl(fd_fbset, FBIOPUT_VSCREENINFO, &screeninfo)){
270           LogNT::getInstance()->crit(TAG, "Could not FBIOPUT_VSCREENINFO frame buffer device");
271   }
272   close(fd_fbset);
273   return 1;
274 }
275
276
277
278 bool VideoOMX::loadOptionsFromServer(VDR* vdr)
279 {
280         logger->debug(TAG, "VideoOMX config load");
281     char *name=vdr->configLoad("VideoOMX","SDDeinterlacing");
282
283     if (name != NULL) {
284                 if (STRCASECMP(name, "None") == 0) {
285                         deinterlace = 0;
286                 }/* else if (STRCASECMP(name, "LineDouble") == 0) {
287                         deinterlace = 1;
288                 }*/ else if (STRCASECMP(name, "Advanced") == 0) {
289                         deinterlace = 2;
290                 } /*else if (STRCASECMP(name, "Crazy") == 0) {
291                         deinterlace = 3; // this does not activate deinterlacing but a image filter, just for fun
292                 }*/ else if (STRCASECMP(name, "Fast") == 0) {
293                         deinterlace = 4;
294                 }
295                 logger->debug(TAG, "Set deinterlacing to {} {}",name,deinterlace);
296                 delete[] name;
297         }
298
299    return true;
300
301 }
302
303 bool VideoOMX::handleOptionChanges(Option* option)
304 {
305     if (Video::handleOptionChanges(option))
306                 return true;
307         switch (option->id) {
308         case 1: {
309                 if (STRCASECMP(option->options[option->userSetChoice], "None") == 0) {
310                         deinterlace = 0;
311                 } /*else if (STRCASECMP(option->options[option->userSetChoice], "LineDouble")
312                                 == 0) {
313                         deinterlace = 1;
314                 }*/ else if (STRCASECMP(option->options[option->userSetChoice], "Advanced")
315                                 == 0) {
316                         deinterlace = 2;
317                 } /*else if (STRCASECMP(option->options[option->userSetChoice], "Crazy")
318                                 == 0) {
319                         deinterlace = 3;
320                 }*/ else if (STRCASECMP(option->options[option->userSetChoice], "Fast")
321                         == 0) {
322                         deinterlace = 4;
323                 }
324                 logger->debug(TAG, "Set deinterlacing to {} {}",option->options[option->userSetChoice],deinterlace);
325                 return true;
326         }
327         break;
328         };
329         return false;
330
331 }
332
333 bool VideoOMX::saveOptionstoServer()
334 {
335
336     switch (deinterlace) {
337         case 0:
338                 VDR::getInstance()->configSave("VideoOMX","SDDeinterlacing", "None");
339                 break;
340         /*case 1:
341                 VDR::getInstance()->configSave("VideoOMX","SDDeinterlacing", "LineDouble");
342                 break;*/
343         case 2:
344                 VDR::getInstance()->configSave("VideoOMX","SDDeinterlacing", "Advanced");
345                 break;
346         /*case 3:
347                 VDR::getInstance()->configSave("VideoOMX","SDDeinterlacing", "Crazy");
348                 break;*/
349         case 4:
350                 VDR::getInstance()->configSave("VideoOMX", "SDDeinterlacing", "Fast");
351                 break;
352         };
353
354     return true;
355 }
356
357 /*Option(u4 id, const char* displayText, const char* configSection, const char* configKey, u4 optionType,
358            u4 numChoices, u4 defaultChoice, u4 startInt,
359            const char * const * options, const char * const * optionkeys = NULL, AbstractOption* handler=NULL);*/
360
361 bool VideoOMX::addOptionsToPanes(int panenumber,Options *options,WOptionPane* pane)
362 {
363     if (!Video::addOptionsToPanes(panenumber,options,pane)) return false;
364
365
366     Option* option;
367     if (panenumber == 2)
368     {
369                 static const char* deinterlaceopts[] = { "None", "Fast",/*"LineDouble",*/"Advanced"/*,"Crazy"*/ };
370         option = new Option(1,tr("SD Deinterlacing"), "VideoOMX","SDDeinterlacing",Option::TYPE_TEXT,/*4,2*/3,2,0,deinterlaceopts,NULL,false,this);
371         options->push_back(option);
372         pane->addOptionLine(option);
373     }
374
375     return true;
376 }
377
378
379
380 int VideoOMX::setTVsize(u1 ttvsize)
381 {
382   if (tvsize!=ttvsize) pendingmodechange=true;
383   tvsize=ttvsize;
384   return 1;
385 }
386
387 u1 VideoOMX::getTVsize()       {
388         /*if (hdmi)*/
389         return ASPECT16X9; // in order that aspect ratio changes are reported
390         //return tvsize;
391 }
392
393 void VideoOMX::executePendingModeChanges()
394 {
395         if (pendingmodechange) {
396                 LogNT::getInstance()->info(TAG, "Execute pending mode change");
397                 Osd::getInstance()->shutdown();
398                 selectVideoMode(0);
399                 Osd::getInstance()->restore();
400                 Osd::getInstance()->init();
401                 BoxStack::getInstance()->redrawAllBoxes();
402                 initted = 1;
403         }
404 }
405
406 int VideoOMX::setDefaultAspect()
407 {
408   return setAspectRatio(tvsize,parx,pary);
409 }
410
411
412
413 int VideoOMX::setFormat(u1 tformat)
414 {
415   if (!initted) return 0;
416   if ((tformat != PAL) && (tformat != NTSC)
417                   && (tformat != PAL_M) && (tformat != NTSC_J)) return 0;
418   format = PAL;
419   tvsystem = tformat;
420
421   if (format == PAL)
422   {
423     screenWidth = 720;
424     screenHeight = 576;
425   }
426
427 //  selectVideoMode(0);
428
429   return 1;
430 }
431
432 void VideoOMX::selectVideoMode(int interlaced)
433 {
434         TV_GET_STATE_RESP_T tvstate;
435         vc_tv_get_state(&tvstate);
436
437         if ((tvstate.state & VC_HDMI_UNPLUGGED)) {
438                 hdmi = false;
439                 LogNT::getInstance()->info(TAG, "HDMI unplugged");
440         } else {
441                 hdmi = true;
442                 LogNT::getInstance()->info(TAG, "HDMI plugged");
443                 if (connection==COMPOSITERGB) {
444                         hdmi=false;
445                         LogNT::getInstance()->info(TAG, "SDTV set");
446                 } else {
447                         hdmi=true;
448                         LogNT::getInstance()->info(TAG, "HDMI set");
449                 }
450         }
451
452
453         if (hdmi) {
454                 TV_SUPPORTED_MODE_T all_supp_modes[200];
455                 HDMI_RES_GROUP_T pref_group;
456                 TV_SUPPORTED_MODE_T  *mymode=NULL;
457                 TV_SUPPORTED_MODE_T  *mymode_second_best=NULL;
458                 // bool got_optimum=false;
459                 uint32_t pref_mode;
460                 HDMI_RES_GROUP_T group=HDMI_RES_GROUP_CEA;
461                 int all_my_modes=vc_tv_hdmi_get_supported_modes(HDMI_RES_GROUP_CEA,
462                                 all_supp_modes,200,
463                                 &pref_group,&pref_mode);
464                 if (all_my_modes<=0) {
465                         group=HDMI_RES_GROUP_DMT;
466                         all_my_modes=vc_tv_hdmi_get_supported_modes(HDMI_RES_GROUP_DMT,
467                                         all_supp_modes,200,
468                                         &pref_group,&pref_mode);
469                         LogNT::getInstance()->info(TAG, "No CEA fall back to DMT modes ");
470                 }
471
472
473
474
475                 int target_fps=50;
476                 if (format==PAL)target_fps=50;
477                 else if (format==NTSC) target_fps=60;
478
479                 //Now first determine native resolution
480                 int native_width=1920;
481                 int native_height=1080;
482                 for (int i=0;i<all_my_modes;i++) {
483                         if (all_supp_modes[i].native) {
484                                 mymode=all_supp_modes+i;
485                                 LogNT::getInstance()->info(TAG, "Found native mode {}x{} {} Hz i: {}",
486                                                 mymode->width,mymode->height,mymode->frame_rate,mymode->scan_mode);
487                                 native_width=mymode->width;
488                                 native_height=mymode->height;
489                         }
490
491                 }
492                 //Now find the mode which matches best
493                 for (int i=0;i<all_my_modes;i++) {
494                         TV_SUPPORTED_MODE_T  *curmode=all_supp_modes+i;
495                         if (curmode->width==native_width &&
496                                         curmode->height==native_height &&
497                                         curmode->frame_rate==target_fps) {
498                                 if(curmode->scan_mode==interlaced) {
499                                         //got_optimum=true;
500                                         mymode=curmode;
501                                         LogNT::getInstance()->info(TAG, "Found optimum mode {}x{} {} Hz i: {}",
502                                                         mymode->width,mymode->height,mymode->frame_rate,mymode->scan_mode);
503                                 } else {
504                                         mymode_second_best=curmode;
505                                         LogNT::getInstance()->info(TAG, "Found close to optimum mode {}x{} {} Hz i: {}",
506                                                         mymode_second_best->width,mymode_second_best->height,
507                                                         mymode_second_best->frame_rate,mymode_second_best->scan_mode);
508                                 }
509
510                         }
511                 }
512                 // InputMan::getInstance()->shutdown(); FIXME FIXME FIXME - disabling this temp, why does this have to run?
513
514
515                 bool disableHDMIModeChange{false};
516                 bool confSuccess = Config::getInstance()->getBool("videoomx", "disable-hdmi-modechange", disableHDMIModeChange);
517
518                 if (!(confSuccess && disableHDMIModeChange))
519                 {
520                   vc_tv_power_off();
521                   if (mymode) {
522                           LogNT::getInstance()->info(TAG, "Switch to optimum mode");
523                           vc_tv_hdmi_power_on_explicit(HDMI_MODE_HDMI,group,mymode->code);
524                   } else if (mymode_second_best) {
525                           LogNT::getInstance()->info(TAG, "Switch to close to optimum mode");
526                           vc_tv_hdmi_power_on_explicit(HDMI_MODE_HDMI,group,mymode_second_best->code);
527                   } else {
528                           LogNT::getInstance()->info(TAG, "Switch to prefered mode");
529                           vc_tv_hdmi_power_on_best(1920, 1080, target_fps, interlaced ? HDMI_INTERLACED : HDMI_NONINTERLACED,
530                                           static_cast<EDID_MODE_MATCH_FLAG_T>(HDMI_MODE_MATCH_FRAMERATE|HDMI_MODE_MATCH_RESOLUTION|HDMI_MODE_MATCH_SCANMODE));
531                   }
532                 }
533
534                 hdmi=true;
535                 outputinterlaced=interlaced;
536         } else {
537                 /* analog tv case */
538                 LogNT::getInstance()->info(TAG, "Analog tv case");
539                 // InputMan::getInstance()->shutdown(); FIXME FIXME FIXME - disabling this temp, why does this have to run?             vc_tv_power_off();
540                 SDTV_MODE_T setmode=SDTV_MODE_PAL;
541                 SDTV_OPTIONS_T options;
542
543                 switch (tvsize) {
544                 default:
545                 case ASPECT16X9:
546                         LogNT::getInstance()->info(TAG, "SDTV aspect 16:9");
547                         options.aspect=SDTV_ASPECT_16_9; break;
548                 case ASPECT4X3:
549                         LogNT::getInstance()->info(TAG, "SDTV aspect 4:3");
550                         options.aspect=SDTV_ASPECT_4_3; break;
551                 case ASPECT14X9:
552                         LogNT::getInstance()->info(TAG, "SDTV aspect 14:9");
553                         options.aspect=SDTV_ASPECT_14_9; break;
554                 };
555
556                 if (format==PAL) setmode=SDTV_MODE_PAL;
557                 else if (format==NTSC) setmode=SDTV_MODE_NTSC;
558                 else if (format==PAL_M)setmode=SDTV_MODE_PAL_M;
559                 else if (format==NTSC_J) setmode=SDTV_MODE_NTSC_J;
560                 vc_tv_sdtv_power_on(setmode,&options);
561                 hdmi=false;
562         }
563
564 //      InputMan::getInstance()->init(); // FIXME complete shutdown and reinit maybe heavy handed. FIXME FIXME FIXME - disabled temp
565     // If this was just to reinit CEC then funcitons should be made to do that
566
567
568         signalon=true;
569         pendingmodechange=false;
570
571 }
572
573
574 int VideoOMX::setConnection(u1 tconnection)
575 {
576   if (!initted) return 0;
577   if ((tconnection != COMPOSITERGB)  && (tconnection != HDMI)) return 0;
578   if (connection!=tconnection) pendingmodechange=true;
579   connection = tconnection;
580
581 //  if (ioctl(fdVideo, AV_SET_VID_OUTPUT, connection) != 0) return 0;
582   return 1;
583 }
584
585 int VideoOMX::setAspectRatio(u1 taspectRatio, int tparx,int tpary)
586 {
587   if (!initted) return 0;
588   //if ((taspectRatio != ASPECT4X3) && (taspectRatio != ASPECT16X9)) return 0;
589   aspectRatio = taspectRatio;
590   parx=tparx;
591   pary=tpary;
592
593   logger->debug(TAG, "Setting aspect to {}: PAR {} {}", aspectRatio,parx,pary);
594
595   updateMode();
596 //  if (ioctl(fdVideo, AV_SET_VID_RATIO, aspectRatio) != 0) return 0;
597   return 1;
598 }
599
600 int VideoOMX::setMode(u1 tmode)
601 {
602   if (!initted) return 0;
603   if (tmode==LETTERBOX || tmode==NORMAL) mode=tmode;
604   updateMode();
605   return 1;
606 }
607
608 bool VideoOMX::setVideoDisplay(VideoDisplay display)
609 {
610         if (!initted) return false;
611         switch (display.mode)
612         {
613         case None: return true; //??
614         case Fullscreen: {
615                 windowed = false;
616
617         } break;
618         case Quarter: {
619                 windowed =true;
620                 xpos = ((float) display.x) / ((float) screenWidth);
621                 ypos = ((float) display.y) / ((float) screenHeight);
622                 width = 0.5f;
623                 height = 0.5f;
624         } break;
625
626         case Eighth: {
627                 windowed =true;
628                 xpos = ((float) display.x) / ((float) screenWidth);
629                 ypos = ((float) display.y) / ((float) screenHeight);
630                 width = 0.25f;
631                 height = 0.25f;
632         } break;
633
634         case Window: {
635                 windowed =true;
636                 xpos = ((float) display.x) / ((float) screenWidth);
637                 ypos = ((float) display.y) / ((float) screenHeight);
638                 width = ((float) display.width) / ((float) screenWidth);
639                 height = ((float) display.height) / ((float) screenHeight);
640         }break;
641         }
642         updateMode();
643         return true;
644 }
645
646
647 void VideoOMX::updateMode()
648 {
649         clock_mutex.lock();
650         if (omx_running) {
651                 int oldcancelstate;
652                 int oldcanceltype;
653                 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldcancelstate);
654                 pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldcanceltype);
655                 OMX_ERRORTYPE error;
656                 OMX_CONFIG_DISPLAYREGIONTYPE dispconf;
657                 memset(&dispconf, 0, sizeof(dispconf));
658                 dispconf.nSize = sizeof(dispconf);
659                 dispconf.nVersion.nVersion = OMX_VERSION;
660                 dispconf.nPortIndex = omx_rend_input_port;
661                 dispconf.layer = 1;
662                 dispconf.set = OMX_DISPLAY_SET_LAYER;
663                 error = OMX_SetParameter(omx_vid_rend, OMX_IndexConfigDisplayRegion,
664                                 &dispconf);
665                 if (error != OMX_ErrorNone) {
666                         LogNT::getInstance()->debug(TAG,
667                                         "Set OMX_IndexConfigDisplayRegion1 failed {:#x}", error);
668                         pthread_setcancelstate(oldcancelstate, NULL);
669                         pthread_setcanceltype(oldcanceltype, NULL);
670                         clock_mutex.unlock();
671                         return;
672                 }
673
674
675                 dispconf.pixel_x =parx;
676                 dispconf.pixel_y=pary;
677                 dispconf.set = OMX_DISPLAY_SET_PIXEL;
678                 error = OMX_SetParameter(omx_vid_rend, OMX_IndexConfigDisplayRegion,
679                                 &dispconf);
680                 if (error != OMX_ErrorNone) {
681                         LogNT::getInstance()->debug(TAG,
682                                         "Set OMX_IndexConfigDisplayRegion5 failed {:#x}", error);
683                         pthread_setcancelstate(oldcancelstate, NULL);
684                         pthread_setcanceltype(oldcanceltype, NULL);
685                         clock_mutex.unlock();
686                         return;
687                 }
688
689
690
691                 dispconf.set = OMX_DISPLAY_SET_FULLSCREEN;
692                 if (!windowed) {
693                         //Set Fullscreen
694                         dispconf.fullscreen = OMX_TRUE;
695                 } else {
696                         dispconf.fullscreen = OMX_FALSE;
697                 }
698                 error = OMX_SetParameter(omx_vid_rend, OMX_IndexConfigDisplayRegion,
699                                 &dispconf);
700                 if (error != OMX_ErrorNone) {
701                         LogNT::getInstance()->debug(TAG,
702                                         "Set OMX_IndexConfigDisplayRegion2 failed {:#x}", error);
703                         pthread_setcancelstate(oldcancelstate, NULL);
704                         pthread_setcanceltype(oldcanceltype, NULL);
705                         clock_mutex.unlock();
706                         return;
707                 }
708
709                 dispconf.set = OMX_DISPLAY_SET_MODE;
710                 if (!windowed) {
711                         dispconf.mode = (mode == NORMAL) ? OMX_DISPLAY_MODE_FILL
712                                         : OMX_DISPLAY_MODE_LETTERBOX;
713                 } else {
714                         dispconf.mode = OMX_DISPLAY_MODE_LETTERBOX;
715                 }
716                 error = OMX_SetParameter(omx_vid_rend, OMX_IndexConfigDisplayRegion,
717                                 &dispconf);
718                 if (error != OMX_ErrorNone) {
719                         LogNT::getInstance()->debug(TAG,
720                                         "Set OMX_IndexConfigDisplayRegion3 failed {:#x}", error);
721                         pthread_setcancelstate(oldcancelstate, NULL);
722                         pthread_setcanceltype(oldcanceltype, NULL);
723                         clock_mutex.unlock();
724                         return;
725                 }
726
727                 if (windowed) {
728                         unsigned int display_width, display_height;
729                         display_width = display_height = 0;
730                         if (graphics_get_display_size(0, &display_width, &display_height)
731                                         < 0) {
732                                 LogNT::getInstance()->warn(TAG,
733                                                 "Getting display size failed! (BCM API) ");
734                                 pthread_setcancelstate(oldcancelstate, NULL);
735                                 pthread_setcanceltype(oldcanceltype, NULL);
736                                 clock_mutex.unlock();
737                                 return;
738                         }
739                         //UnSetFullscreen with window
740                         dispconf.set = OMX_DISPLAY_SET_DEST_RECT;
741                         dispconf.dest_rect.x_offset
742                                         = (int) (xpos * ((float) display_width));
743                         dispconf.dest_rect.y_offset = (int) (ypos
744                                         * ((float) display_height));
745                         dispconf.dest_rect.width = (int) (width * ((float) display_width));
746                         dispconf.dest_rect.height = (int) (height * ((float) display_height));
747                         LogNT::getInstance()->debug(TAG,
748                                                                         "Set dest_rect as {} {} {} {}", dispconf.dest_rect.x_offset,dispconf.dest_rect.y_offset,
749                                                                         dispconf.dest_rect.width , dispconf.dest_rect.height);
750
751                         error = OMX_SetParameter(omx_vid_rend,
752                                         OMX_IndexConfigDisplayRegion, &dispconf);
753                         if (error != OMX_ErrorNone) {
754                                 LogNT::getInstance()->debug(TAG,
755                                                 "Set OMX_IndexConfigDisplayRegion failed {:#x}", error);
756                                 pthread_setcancelstate(oldcancelstate, NULL);
757                                 pthread_setcanceltype(oldcanceltype, NULL);
758                                 clock_mutex.unlock();
759                                 return;
760                         }
761                 }
762                 pthread_setcancelstate(oldcancelstate, NULL);
763                 pthread_setcanceltype(oldcanceltype, NULL);
764
765         }
766         clock_mutex.unlock();
767 }
768
769 int VideoOMX::signalOff()
770 {
771         //TODO reinit osd
772         LogNT::getInstance()->info(TAG, "signalOff");
773         Osd::getInstance()->stopUpdate(); // turn off drawing thread
774     // FIXME FIXME FIXME why do this? Probably to do with reinitting CEC, but should refine this
775         InputMan::getInstance()->shutdown();
776         vc_tv_power_off();
777         InputMan::getInstance()->init();
778         signalon=false;
779     return 1;
780 }
781
782 int VideoOMX::signalOn()
783 {
784   if (!signalon)  {
785           Osd::getInstance()->shutdown();
786           LogNT::getInstance()->info(TAG, "signalOn");
787           selectVideoMode(0);
788           Osd::getInstance()->restore();
789           Osd::getInstance()->init();
790           BoxStack::getInstance()->redrawAllBoxes();
791           initted=1;
792
793   }
794   return 1;
795 }
796
797 int VideoOMX::setSource()
798 {
799   if (!initted) return 0;
800
801   // What does this do...
802 //  if (ioctl(fdVideo, AV_SET_VID_SRC, 1) != 0) return 0;
803   return 1;
804 }
805
806 int VideoOMX::setPosition(int x, int y) {
807         if (!initted)
808                 return 0;
809         xpos = ((float) x*2.f) / ((float) screenWidth);
810         ypos = ((float) y*2.f) / ((float) screenHeight);
811
812         updateMode();
813         return 1;
814 }
815
816 int VideoOMX::sync()
817 {
818   if (!initted) return 0;
819
820 //  if (ioctl(fdVideo, AV_SET_VID_SYNC, 2) != 0) return 0;
821   return 1;
822 }
823
824 void VideoOMX::interlaceSwitch4Demux() {
825         return;
826         Demuxer *demux=Demuxer::getInstance();
827
828         if (hdmi) { // only switch if hdmi and HD or interlaced SD material
829
830
831                 //OMX_Deinit();
832                 int set_interlaced=0;
833                 if (demux->getHorizontalSize()>720  && demux->getInterlaced()) {
834                         set_interlaced=1;
835                 }
836                 LogNT::getInstance()->info(TAG, "switch interlacing {} {} {}",demux->getInterlaced(),outputinterlaced,set_interlaced);
837                 if (outputinterlaced!=set_interlaced) {
838                         selectVideoMode(set_interlaced);
839                         Osd::getInstance()->shutdown();
840                         Osd::getInstance()->restore();
841                         Osd::getInstance()->init();
842                         BoxStack::getInstance()->redrawAllBoxes();
843                         initted=1;
844                 }
845                 //OMX_Init();
846
847
848         }
849
850
851 }
852
853
854 int VideoOMX::play() {
855         if (!initted)
856                 return 0;
857         iframemode = false;
858         logger->debug(TAG, "enter play");
859
860         interlaceSwitch4Demux();
861
862         if (AllocateCodecsOMX()) {
863                 return 1;
864                 // Otherwise fall back to libav
865         } else {
866                 if (h264) {
867                         omx_h264 = false;
868                         LogNT::getInstance()->info(TAG,
869                                         "Allocate Codecs OMX failed assume h264 unsupported");
870                 } else {
871                         omx_mpeg2 = false;
872                         LogNT::getInstance()->info(TAG,
873                                         "Allocate Codecs OMX failed assume mpeg2 unsupported");
874                 }
875         }
876
877         return 0;
878
879 }
880
881
882 int VideoOMX::initClock()
883 {
884         OMX_ERRORTYPE error;
885         clock_mutex.lock();
886         if (clock_references==0)
887         {
888
889                 static OMX_CALLBACKTYPE callbacks= {&EventHandler_OMX,&EmptyBufferDone_OMX,&FillBufferDone_OMX};
890                 omx_event_mutex.lock();
891                 omx_events.clear();
892                 omx_event_mutex.unlock();
893
894                 error=OMX_GetHandle(&omx_clock,L_VPE_OMX_CLOCK,NULL,&callbacks);
895
896                 if (error!=OMX_ErrorNone) {
897                         logger->debug(TAG, "Init OMX clock failed {:#x}", error);
898                         clock_mutex.unlock();
899                         DeAllocateCodecsOMX();
900                         return 0;
901                 }
902
903                 /* TODO Clock config to separate method */
904                 OMX_PORT_PARAM_TYPE p_param;
905                 memset(&p_param,0,sizeof(p_param));
906                 p_param.nSize=sizeof(p_param);
907                 p_param.nVersion.nVersion=OMX_VERSION;
908                 error=OMX_GetParameter(omx_clock,OMX_IndexParamOtherInit,&p_param);
909                 if (error!=OMX_ErrorNone) {
910                         logger->debug(TAG, "Init clock OMX_GetParameter failed {:#x}", error);
911                         clock_mutex.unlock();
912                         DeAllocateCodecsOMX();
913                         return 0;
914                 }
915                 omx_clock_output_port=p_param.nStartPortNumber;
916
917                 for (unsigned int i=0;i<p_param.nPorts;i++) {
918                         if (!DisablePort(omx_clock,p_param.nStartPortNumber+i,true) ) {
919                                 logger->debug(TAG, "Disable Ports OMX clock failed {}",i);
920                                 clock_mutex.unlock();
921                                 DeAllocateCodecsOMX();
922                                 return 0;
923                         }
924                 }
925
926
927
928
929         }
930         logger->debug(TAG, "init omx clock {:p} {}", (void*)this, omx_clock);
931         clock_references++;
932         clock_mutex.unlock();
933         return 1;
934 }
935
936 int VideoOMX::getClockAudioandInit(OMX_HANDLETYPE *p_omx_clock,OMX_U32 *p_omx_clock_output_port)
937 {
938         OMX_ERRORTYPE error;
939         *p_omx_clock=NULL;
940         *p_omx_clock_output_port=0;
941
942         if (!initClock()) {
943                 return 0;
944         }
945         clock_mutex.lock();
946
947         OMX_TIME_CONFIG_ACTIVEREFCLOCKTYPE refclock;
948         memset(&refclock,0,sizeof(refclock));
949         refclock.nSize=sizeof(refclock);
950         refclock.nVersion.nVersion=OMX_VERSION;
951
952         refclock.eClock=OMX_TIME_RefClockAudio;
953
954         //refclock.eClock=OMX_TIME_RefClockVideo;
955         error=OMX_SetConfig(omx_clock,OMX_IndexConfigTimeActiveRefClock,&refclock);
956         if (error!=OMX_ErrorNone){
957                 logger->debug(TAG, "Clock OMX_IndexConfigTimeActiveRefClock failed {:#x}", error);
958                 clock_mutex.unlock();
959                 DeAllocateCodecsOMX();
960                 return 0;
961         }
962
963         OMX_PORT_PARAM_TYPE p_param;
964         memset(&p_param,0,sizeof(p_param));
965         p_param.nSize=sizeof(p_param);
966         p_param.nVersion.nVersion=OMX_VERSION;
967         error=OMX_GetParameter(omx_clock,OMX_IndexParamOtherInit,&p_param);
968         if (error!=OMX_ErrorNone){
969                 logger->debug(TAG, "Init clock OMX_GetParameter failed {:#x}", error);
970                 clock_mutex.unlock();
971                 DeAllocateCodecsOMX();
972                 return 0;
973         }
974
975         OMX_TIME_CONFIG_CLOCKSTATETYPE clock_conf;
976         memset(&clock_conf,0,sizeof(clock_conf));
977         clock_conf.nSize=sizeof(clock_conf);
978         clock_conf.nVersion.nVersion=OMX_VERSION;
979         clock_conf.eState=OMX_TIME_ClockStateWaitingForStartTime;
980         clock_conf.nStartTime=intToOMXTicks(0);
981         clock_conf.nOffset=intToOMXTicks(0);
982         if (clock_references==1) clock_conf.nWaitMask=OMX_CLOCKPORT1;
983         else clock_conf.nWaitMask=OMX_CLOCKPORT0|OMX_CLOCKPORT1;
984         error=OMX_SetConfig(omx_clock,OMX_IndexConfigTimeClockState,&clock_conf);
985         if (error!=OMX_ErrorNone) {
986                 logger->debug(TAG, "AuI Clock IndexConfigTimeClockState failed {:#x}", error);
987         }
988
989
990         *p_omx_clock_output_port=p_param.nStartPortNumber+1;
991         *p_omx_clock=omx_clock;
992         clock_mutex.unlock();
993         return 1;
994 }
995
996 int VideoOMX::getClockVideoandInit()
997 {
998         OMX_ERRORTYPE error;
999
1000         if (!initClock()) {
1001                 return 0;
1002         }
1003         clock_mutex.lock();
1004
1005         if (clock_references==1) { // only if no audio is attached to this clock!
1006                 OMX_TIME_CONFIG_ACTIVEREFCLOCKTYPE refclock;
1007                 memset(&refclock,0,sizeof(refclock));
1008                 refclock.nSize=sizeof(refclock);
1009                 refclock.nVersion.nVersion=OMX_VERSION;
1010
1011                 //refclock.eClock=OMX_TIME_RefClockAudio;
1012
1013                 refclock.eClock=OMX_TIME_RefClockVideo;
1014                 error=OMX_SetConfig(omx_clock,OMX_IndexConfigTimeActiveRefClock,&refclock);
1015                 if (error!=OMX_ErrorNone) {
1016                         logger->debug(TAG, "Clock OMX_IndexConfigTimeActiveRefClock failed {:#x}", error);
1017                         clock_mutex.unlock();
1018                         DeAllocateCodecsOMX();
1019                         return 0;
1020                 }
1021         }
1022
1023         OMX_PORT_PARAM_TYPE p_param;
1024         memset(&p_param,0,sizeof(p_param));
1025         p_param.nSize=sizeof(p_param);
1026         p_param.nVersion.nVersion=OMX_VERSION;
1027         error=OMX_GetParameter(omx_clock,OMX_IndexParamOtherInit,&p_param);
1028         if (error!=OMX_ErrorNone){
1029                 logger->debug(TAG, "Init clock OMX_GetParameter failed {:#x}", error);
1030                 clock_mutex.unlock();
1031                 DeAllocateCodecsOMX();
1032                 return 0;
1033         }
1034
1035
1036         OMX_TIME_CONFIG_CLOCKSTATETYPE clock_conf;
1037         memset(&clock_conf,0,sizeof(clock_conf));
1038         clock_conf.nSize=sizeof(clock_conf);
1039         clock_conf.nVersion.nVersion=OMX_VERSION;
1040         clock_conf.eState=OMX_TIME_ClockStateStopped;
1041         clock_conf.nStartTime=intToOMXTicks(0);
1042         clock_conf.nOffset=intToOMXTicks(0);
1043         error=OMX_SetConfig(omx_clock,OMX_IndexConfigTimeClockState,&clock_conf);
1044         if (error!=OMX_ErrorNone) {
1045                 logger->debug(TAG, "VuI Clock IndexConfigTimeClockState failed {:#x}", error);
1046         }
1047
1048
1049         memset(&clock_conf,0,sizeof(clock_conf));
1050         clock_conf.nSize=sizeof(clock_conf);
1051         clock_conf.nVersion.nVersion=OMX_VERSION;
1052         clock_conf.eState=OMX_TIME_ClockStateWaitingForStartTime;
1053         clock_conf.nStartTime=intToOMXTicks(0);
1054         clock_conf.nOffset=intToOMXTicks(0);
1055         if (clock_references==1) clock_conf.nWaitMask=OMX_CLOCKPORT0;
1056         else clock_conf.nWaitMask=OMX_CLOCKPORT0|OMX_CLOCKPORT1;
1057         error=OMX_SetConfig(omx_clock,OMX_IndexConfigTimeClockState,&clock_conf);
1058         if (error!=OMX_ErrorNone) {
1059                 logger->debug(TAG, "VuI Clock IndexConfigTimeClockState failed {:#x}", error);
1060         }
1061
1062         omx_clock_output_port=p_param.nStartPortNumber;
1063         clock_mutex.unlock();
1064
1065         return 1;
1066 }
1067
1068 void VideoOMX::clockUnpause()
1069 {
1070         OMX_ERRORTYPE error;
1071         LogNT::getInstance()->info(TAG, "enter Clockunpause");
1072         clock_mutex.lock();
1073         if (clock_references>0 && clockpaused) {
1074                 OMX_TIME_CONFIG_SCALETYPE scale_type;
1075                 memset(&scale_type,0,sizeof(scale_type));
1076                 scale_type.nSize=sizeof(scale_type);
1077                 scale_type.nVersion.nVersion=OMX_VERSION;
1078                 scale_type.xScale=1<<16;
1079                 error=OMX_SetConfig(omx_clock,OMX_IndexConfigTimeScale,&scale_type);
1080                 if (error!=OMX_ErrorNone) {
1081                         logger->debug(TAG, "ClockUnpause OMX_IndexConfigTimeScale failed {:#x}", error);
1082                 }
1083                 LogNT::getInstance()->info(TAG, "set playback speed ClockUnpause");
1084                 clockpaused=false;
1085         }
1086         clock_mutex.unlock();
1087 }
1088
1089
1090 void VideoOMX::clockPause()
1091 {
1092         OMX_ERRORTYPE error;
1093         LogNT::getInstance()->info(TAG, "enter ClockPause");
1094         clock_mutex.lock();
1095         if (clock_references>0 && !clockpaused) {
1096                 OMX_TIME_CONFIG_SCALETYPE scale_type;
1097                 memset(&scale_type,0,sizeof(scale_type));
1098                 scale_type.nSize=sizeof(scale_type);
1099                 scale_type.nVersion.nVersion=OMX_VERSION;
1100                 scale_type.xScale=0;
1101                 error=OMX_SetConfig(omx_clock,OMX_IndexConfigTimeScale,&scale_type);
1102                 if (error!=OMX_ErrorNone) {
1103                         logger->debug(TAG, "ClockPause OMX_IndexConfigTimeScale failed {:#x}", error);
1104                 }
1105                 LogNT::getInstance()->info(TAG, "set playback speed ClockPause");
1106                 clockpaused=true;
1107         }
1108         clock_mutex.unlock();
1109 }
1110
1111
1112
1113 int VideoOMX::AllocateCodecsOMX()
1114 {
1115         OMX_ERRORTYPE error;
1116         static OMX_CALLBACKTYPE callbacks= {&EventHandler_OMX,&EmptyBufferDone_OMX,&FillBufferDone_OMX};
1117
1118         Demuxer* demux=Demuxer::getInstance();
1119
1120         dodeint=false;
1121         first_frame=true;
1122
1123         LogNT::getInstance()->info(TAG, "Allocate Codecs OMX");
1124         //Clock, move later to audio including events
1125
1126         LogNT::getInstance()->info(TAG, "Deinter VideoType {} x {} i: {}", demux->getHorizontalSize(),demux->getVerticalSize(),demux->getInterlaced());
1127         if (deinterlace!=0 && /*(demux->getHorizontalSize()<=720 ) &&*/ demux->getInterlaced()) { 
1128                 dodeint=true;
1129
1130
1131                 LogNT::getInstance()->info(TAG, "Deinterlacing activated {}",deinterlace);
1132
1133         }
1134
1135
1136         if (!getClockVideoandInit()){
1137                 return 0;// get the clock and init it if necessary
1138         }
1139
1140
1141         if (!idleClock()) {
1142                 logger->debug(TAG, "idleClock failed");
1143                 return 0;
1144         }
1145         /* TODO end */
1146
1147         clock_mutex.lock();
1148         if (h264) {
1149                 error=OMX_GetHandle(&omx_vid_dec,L_VPE_OMX_H264_DECODER,NULL,&callbacks);
1150         } else {
1151                 error=OMX_GetHandle(&omx_vid_dec,L_VPE_OMX_MPEG2_DECODER,NULL,&callbacks);
1152         }
1153
1154         if (error!=OMX_ErrorNone){
1155                 logger->debug(TAG, "Init OMX video decoder failed {:#x}", error);
1156                 clock_mutex.unlock();
1157                 DeAllocateCodecsOMX();
1158                 return 0;
1159         }
1160
1161
1162         logger->debug(TAG, "Nmark3 ");
1163         OMX_PORT_PARAM_TYPE p_param;
1164         memset(&p_param,0,sizeof(p_param));
1165         p_param.nSize=sizeof(p_param);
1166         p_param.nVersion.nVersion=OMX_VERSION;
1167         error=OMX_GetParameter(omx_vid_dec,OMX_IndexParamVideoInit,&p_param);
1168         if (error!=OMX_ErrorNone){
1169                 logger->debug(TAG, "Init OMX h264 decoder OMX_GetParameter failed {:#x}", error);
1170                 clock_mutex.unlock();
1171                 DeAllocateCodecsOMX();
1172             return 0;
1173         }
1174         omx_codec_input_port=p_param.nStartPortNumber;
1175         omx_codec_output_port=p_param.nStartPortNumber+1;
1176
1177         if (!DisablePort(omx_vid_dec,omx_codec_input_port) || !DisablePort(omx_vid_dec,omx_codec_output_port)) {
1178                 logger->debug(TAG, "Disable Ports OMX video decoder failed");
1179                 clock_mutex.unlock();
1180                 DeAllocateCodecsOMX();
1181                 return 0;
1182         }
1183
1184         logger->debug(TAG, "Nmark4 ");
1185
1186         OMX_PARAM_BRCMVIDEODECODEERRORCONCEALMENTTYPE conceal;
1187         memset(&conceal,0,sizeof(conceal));
1188         conceal.nSize=sizeof(conceal);
1189         conceal.nVersion.nVersion=OMX_VERSION;
1190         conceal.bStartWithValidFrame=OMX_FALSE;
1191
1192         error=OMX_SetParameter(omx_vid_dec,OMX_IndexParamBrcmVideoDecodeErrorConcealment,&conceal);
1193         if (error!=OMX_ErrorNone){
1194                 logger->debug(TAG, "OMX_IndexParamBrcmVideoDecodeErrorConcealment failed {:#x}", error);
1195                 clock_mutex.unlock();
1196                 DeAllocateCodecsOMX();
1197                 return 0;
1198         }
1199
1200         if (dodeint) {
1201                 error = OMX_GetHandle(&omx_vid_deint, L_VPE_OMX_VIDEO_DEINTERLACE, NULL,
1202                                 &callbacks);
1203                 if (error != OMX_ErrorNone) {
1204                         LogNT::getInstance()->debug(TAG,
1205                                         "Init OMX video deinterlacer failed {:#x}", error);
1206                         clock_mutex.unlock();
1207                         DeAllocateCodecsOMX();
1208                         return 0;
1209                 }
1210
1211                 error = OMX_GetParameter(omx_vid_deint, OMX_IndexParamImageInit,
1212                                 &p_param);
1213                 if (error != OMX_ErrorNone) {
1214                         LogNT::getInstance()->debug(TAG,
1215                                         "Init OMX video deinterlacer OMX_GetParameter failed {:#x}",
1216                                         error);
1217                         clock_mutex.unlock();
1218                         DeAllocateCodecsOMX();
1219                         return 0;
1220                 }
1221                 omx_deint_input_port = p_param.nStartPortNumber;
1222                 omx_deint_output_port = p_param.nStartPortNumber + 1;
1223
1224                 if (!DisablePort(omx_vid_deint, omx_deint_input_port, true)
1225                                 || !DisablePort(omx_vid_deint, omx_deint_output_port, true)) {
1226                         LogNT::getInstance()->debug(TAG,
1227                                         "Disable Ports OMX video deint failed");
1228                         clock_mutex.unlock();
1229                         DeAllocateCodecsOMX();
1230                         return 0;
1231                 }
1232
1233         }
1234
1235
1236         error=OMX_GetHandle(&omx_vid_sched,L_VPE_OMX_VIDEO_SCHED,NULL,&callbacks);
1237         if (error!=OMX_ErrorNone){
1238                 logger->debug(TAG, "Init OMX video scheduler failed {:#x}", error);
1239                 clock_mutex.unlock();
1240                 DeAllocateCodecsOMX();
1241                 return 0;
1242         }
1243
1244
1245
1246         error=OMX_GetParameter(omx_vid_sched,OMX_IndexParamVideoInit,&p_param);
1247         if (error!=OMX_ErrorNone){
1248                 logger->debug(TAG, "Init OMX video scheduler OMX_GetParameter failed {:#x}", error);
1249                 clock_mutex.unlock();
1250                 DeAllocateCodecsOMX();
1251                 return 0;
1252         }
1253         omx_shed_input_port=p_param.nStartPortNumber;
1254         omx_shed_output_port=p_param.nStartPortNumber+1;
1255
1256
1257         error=OMX_GetParameter(omx_vid_sched,OMX_IndexParamOtherInit,&p_param);
1258         if (error!=OMX_ErrorNone){
1259                 logger->debug(TAG, "Init OMX video scheduler OMX_GetParameter failed {:#x}", error);
1260                 clock_mutex.unlock();
1261                 DeAllocateCodecsOMX();
1262                 return 0;
1263         }
1264         omx_shed_clock_port=p_param.nStartPortNumber;
1265         logger->debug(TAG, "scheduler ports {} {} {}",omx_shed_input_port,omx_shed_output_port,omx_shed_clock_port);
1266
1267
1268         if (!DisablePort(omx_vid_sched,omx_shed_input_port,true) || !DisablePort(omx_vid_sched,omx_shed_output_port,true)
1269                         || !DisablePort(omx_vid_sched,omx_shed_clock_port,true)) {
1270                 logger->debug(TAG, "Disable Ports OMX video shed failed");
1271                 clock_mutex.unlock();
1272                 DeAllocateCodecsOMX();
1273                 return 0;
1274         }
1275
1276
1277         error=OMX_GetHandle(&omx_vid_rend,L_VPE_OMX_VIDEO_REND,NULL,&callbacks);
1278         if (error!=OMX_ErrorNone){
1279                 logger->debug(TAG, "Init OMX video rend failed {:#x}", error);
1280                 clock_mutex.unlock();
1281                 DeAllocateCodecsOMX();
1282                 return 0;
1283         }
1284
1285         error=OMX_GetParameter(omx_vid_rend,OMX_IndexParamVideoInit,&p_param);
1286         if (error!=OMX_ErrorNone){
1287                 logger->debug(TAG, "Init OMX video rend OMX_GetParameter failed {:#x}", error);
1288                 clock_mutex.unlock();
1289                 DeAllocateCodecsOMX();
1290                 return 0;
1291         }
1292         omx_rend_input_port=p_param.nStartPortNumber;
1293         //omx_rend_output_port=p_param.nStartPortNumber+1;
1294
1295
1296         if (!DisablePort(omx_vid_rend,omx_rend_input_port,true) /*|| !DisablePort(omx_vid_rend,omx_rend_output_port)*/
1297                                 ) {
1298                 logger->debug(TAG, "Disable Ports OMX video rend failed");
1299                 clock_mutex.unlock();
1300                 DeAllocateCodecsOMX();
1301                 return 0;
1302         }
1303
1304         //Setuo chain
1305
1306
1307
1308         error=OMX_SetupTunnel(omx_clock,omx_clock_output_port,omx_vid_sched,omx_shed_clock_port);
1309         if (error!=OMX_ErrorNone){
1310                 logger->debug(TAG, "OMX_Setup tunnel clock to sched failed {:#x} {} {}", error,omx_clock_output_port,omx_shed_clock_port);
1311                 clock_mutex.unlock();
1312                 DeAllocateCodecsOMX();
1313                 return 0;
1314         }
1315
1316         if (!EnablePort(omx_clock,omx_clock_output_port,false) || !EnablePort(omx_vid_sched,omx_shed_clock_port,false)
1317                                         ) {
1318                 logger->debug(TAG, "Enable Ports OMX clock shed failed");
1319                 clock_mutex.unlock();
1320                 DeAllocateCodecsOMX();
1321                 return 0;
1322         }
1323
1324
1325
1326         if (!ChangeComponentState(omx_vid_sched,OMX_StateIdle)) {
1327                 logger->debug(TAG, "vid_sched idle ChangeComponentState");
1328                 clock_mutex.unlock();
1329                 DeAllocateCodecsOMX();
1330                 return 0;
1331         }
1332
1333
1334
1335
1336         if ( !CommandFinished(omx_vid_sched,OMX_CommandPortEnable,omx_shed_clock_port)) {
1337                 clock_mutex.unlock();
1338                 DeAllocateCodecsOMX();
1339                 return 0;
1340         }
1341
1342
1343
1344
1345
1346         if ( !CommandFinished(omx_clock,OMX_CommandPortEnable,omx_clock_output_port)) {
1347                 clock_mutex.unlock();
1348                 DeAllocateCodecsOMX();
1349                 return 0;
1350         }
1351
1352
1353
1354 /*      error=OMX_SendCommand(omx_vid_dec,OMX_CommandStateSet,OMX_StateIdle,0);
1355         if (error!=OMX_ErrorNone){
1356                 logger->debug(TAG, "vid_dec Send Command to OMX State Idle {:#x}", error);
1357                 return 0;
1358         }*/
1359
1360         OMX_VIDEO_PARAM_PORTFORMATTYPE ft_type;
1361         memset(&ft_type,0,sizeof(ft_type));
1362         ft_type.nSize=sizeof(ft_type);
1363         ft_type.nVersion.nVersion=OMX_VERSION;
1364
1365         ft_type.nPortIndex=omx_codec_input_port;
1366         if (h264) {
1367                 ft_type.eCompressionFormat=OMX_VIDEO_CodingAVC;
1368         } else {
1369                 ft_type.eCompressionFormat=OMX_VIDEO_CodingMPEG2;
1370         }
1371
1372
1373
1374     ft_type.xFramerate=0*(1<<16);//25*(1<<16);//demux->getFrameRate()*(1<<16);
1375     logger->debug(TAG, "Framerate: {}",demux->getFrameRate());
1376         error=OMX_SetParameter(omx_vid_dec,OMX_IndexParamVideoPortFormat,&ft_type);
1377         if (error!=OMX_ErrorNone){
1378                 logger->debug(TAG, "Init OMX_IndexParamVideoPortFormat failed {:#x}", error);
1379                 clock_mutex.unlock();
1380                 DeAllocateCodecsOMX();
1381                 return 0;
1382         }
1383
1384
1385         if (!ChangeComponentState(omx_vid_dec,OMX_StateIdle)) {
1386                 logger->debug(TAG, "vid_dec ChangeComponentState");
1387                 clock_mutex.unlock();
1388                 DeAllocateCodecsOMX();
1389                 return 0;
1390         }
1391
1392         OMX_CONFIG_BUFFERSTALLTYPE stall_conf;
1393         memset(&stall_conf,0,sizeof(stall_conf));
1394         stall_conf.nSize=sizeof(stall_conf);
1395         stall_conf.nVersion.nVersion=OMX_VERSION;
1396         stall_conf.nPortIndex=omx_codec_output_port;
1397         stall_conf.nDelay=1500*1000;
1398         error=OMX_SetConfig(omx_vid_dec,OMX_IndexConfigBufferStall,&stall_conf);
1399         if (error!=OMX_ErrorNone){
1400                 logger->debug(TAG, "Init OMX_IndexConfigBufferStall failed {:#x}", error);
1401                 clock_mutex.unlock();
1402                 DeAllocateCodecsOMX();
1403                 return 0;
1404         }
1405         omx_vid_stalled = false;
1406
1407         OMX_CONFIG_REQUESTCALLBACKTYPE req_callback;
1408         memset(&req_callback,0,sizeof(req_callback));
1409         req_callback.nSize=sizeof(req_callback);
1410         req_callback.nVersion.nVersion=OMX_VERSION;
1411         req_callback.nPortIndex=omx_codec_output_port;
1412         req_callback.nIndex=OMX_IndexConfigBufferStall;
1413         req_callback.bEnable=OMX_TRUE;
1414         error=OMX_SetConfig(omx_vid_dec,OMX_IndexConfigRequestCallback,&req_callback);
1415         if (error!=OMX_ErrorNone){
1416                 logger->debug(TAG, "Init OMX_IndexConfigRequestCallback  failed {:#x}", error);
1417                 clock_mutex.unlock();
1418                 DeAllocateCodecsOMX();
1419                 return 0;
1420         }
1421
1422
1423
1424         if (!PrepareInputBufsOMX()) {
1425                 clock_mutex.unlock();
1426                 DeAllocateCodecsOMX();
1427                 return 0;
1428         }
1429
1430         if (!dodeint) {
1431                 error=OMX_SetupTunnel(omx_vid_dec,omx_codec_output_port,omx_vid_sched,omx_shed_input_port);
1432                 if (error!=OMX_ErrorNone){
1433                         logger->debug(TAG, "OMX_Setup tunnel dec to sched failed {:#x}", error);
1434                         clock_mutex.unlock();
1435                         DeAllocateCodecsOMX();
1436                         return 0;
1437                 }
1438
1439
1440
1441                 if (!EnablePort(omx_vid_dec,omx_codec_output_port,false) || !EnablePort(omx_vid_sched,omx_shed_input_port,false)
1442                 ) {
1443                         logger->debug(TAG, "Enable Ports OMX codec shed failed");
1444                         clock_mutex.unlock();
1445                         DeAllocateCodecsOMX();
1446                         return 0;
1447                 }
1448
1449                 if ( !CommandFinished(omx_vid_dec,OMX_CommandPortEnable,omx_codec_output_port) || !CommandFinished(omx_vid_sched,OMX_CommandPortEnable,omx_shed_input_port)) {
1450                         clock_mutex.unlock();
1451                         DeAllocateCodecsOMX();
1452                         return 0;
1453                 }
1454
1455         } else {
1456
1457                 error=OMX_SetupTunnel(omx_vid_dec,omx_codec_output_port,omx_vid_deint,omx_deint_input_port);
1458                 if (error!=OMX_ErrorNone){
1459                         logger->debug(TAG, "OMX_Setup tunnel dec to deint failed {:#x}", error);
1460                         clock_mutex.unlock();
1461                         DeAllocateCodecsOMX();
1462                         return 0;
1463                 }
1464
1465
1466
1467                 if (!EnablePort(omx_vid_dec,omx_codec_output_port,false) || !EnablePort(omx_vid_deint,omx_deint_input_port,false)
1468                 ) {
1469                         logger->debug(TAG, "Enable Ports OMX codec deint failed");
1470                         clock_mutex.unlock();
1471                         DeAllocateCodecsOMX();
1472                         return 0;
1473                 }
1474
1475                 if ( !CommandFinished(omx_vid_dec,OMX_CommandPortEnable,omx_codec_output_port) || !CommandFinished(omx_vid_deint,OMX_CommandPortEnable,omx_deint_input_port)) {
1476                         clock_mutex.unlock();
1477                         DeAllocateCodecsOMX();
1478                         return 0;
1479                 }
1480
1481                 if (!ChangeComponentState(omx_vid_deint,OMX_StateIdle)) {
1482                         logger->debug(TAG, "vid_deint ChangeComponentState");
1483                         clock_mutex.unlock();
1484                         DeAllocateCodecsOMX();
1485                         return 0;
1486                 }
1487
1488                 OMX_CONFIG_IMAGEFILTERPARAMSTYPE imagefilter;
1489                 memset(&imagefilter,0,sizeof(imagefilter));
1490                 imagefilter.nSize=sizeof(imagefilter);
1491                 imagefilter.nVersion.nVersion=OMX_VERSION;
1492
1493                 imagefilter.nPortIndex=omx_deint_output_port;
1494                 imagefilter.nNumParams=4;
1495                 imagefilter.nParams[0]=3;//???
1496                 imagefilter.nParams[1]=0;//default frame interval
1497                 imagefilter.nParams[2]=0;// frame rate
1498                 if (demux->getHorizontalSize() <= 720){
1499                         imagefilter.nParams[3] = 1;//use qpus
1500                 }
1501                 else 
1502                 {
1503                         imagefilter.nParams[3] = 0;//use qpus
1504                 }
1505
1506                 switch (deinterlace) {
1507                 case 1:
1508                         imagefilter.eImageFilter=OMX_ImageFilterDeInterlaceLineDouble; break;
1509                 case 2:
1510                         imagefilter.eImageFilter=OMX_ImageFilterDeInterlaceAdvanced; break;
1511                 case 3:
1512                         imagefilter.eImageFilter=OMX_ImageFilterFilm; break;
1513                 case 4:
1514                         imagefilter.eImageFilter = OMX_ImageFilterDeInterlaceFast; break;
1515                 }
1516
1517
1518                 error=OMX_SetConfig(omx_vid_deint,OMX_IndexConfigCommonImageFilterParameters,&imagefilter);
1519                 if (error!=OMX_ErrorNone){
1520                         logger->debug(TAG, "Init OMX_IndexConfigCommonImageFilterParameters failed {:#x}", error);
1521                         clock_mutex.unlock();
1522                         DeAllocateCodecsOMX();
1523                         return 0;
1524                 }
1525
1526
1527                 error=OMX_SetupTunnel(omx_vid_deint,omx_deint_output_port,omx_vid_sched,omx_shed_input_port);
1528                 if (error!=OMX_ErrorNone){
1529                         logger->debug(TAG, "OMX_Setup tunnel deint to sched failed {:#x}", error);
1530                         clock_mutex.unlock();
1531                         DeAllocateCodecsOMX();
1532                         return 0;
1533                 }
1534
1535                 if (!EnablePort(omx_vid_deint,omx_deint_output_port,false) || !EnablePort(omx_vid_sched,omx_shed_input_port,false)
1536                 ) {
1537                         logger->debug(TAG, "Enable Ports OMX deint shed failed");
1538                         clock_mutex.unlock();
1539                         DeAllocateCodecsOMX();
1540                         return 0;
1541                 }
1542
1543                 if ( !CommandFinished(omx_vid_deint,OMX_CommandPortEnable,omx_deint_output_port) || !CommandFinished(omx_vid_sched,OMX_CommandPortEnable,omx_shed_input_port)) {
1544                         clock_mutex.unlock();
1545                         DeAllocateCodecsOMX();
1546                         return 0;
1547                 }
1548
1549         }
1550
1551         if (!ChangeComponentState(omx_vid_dec,OMX_StateExecuting)) {
1552                 logger->debug(TAG, "omx_vid_dec ChangeComponentState Execute");
1553                 clock_mutex.unlock();
1554                 DeAllocateCodecsOMX();
1555                 return 0;
1556         }
1557
1558         error=OMX_SetupTunnel(omx_vid_sched,omx_shed_output_port,omx_vid_rend,omx_rend_input_port);
1559         if (error!=OMX_ErrorNone){
1560                 logger->debug(TAG, "OMX_Setup tunnel  sched to rend failed {:#x}", error);
1561                 clock_mutex.unlock();
1562                 DeAllocateCodecsOMX();
1563                 return 0;
1564         }
1565
1566         if (!EnablePort(omx_vid_sched,omx_shed_output_port,false) || !EnablePort(omx_vid_rend,omx_rend_input_port,false)
1567                                                         ) {
1568                 logger->debug(TAG, "Enable Ports OMX  shed rend failed");
1569                 clock_mutex.unlock();
1570                 DeAllocateCodecsOMX();
1571                 return 0;
1572         }
1573
1574         if (!CommandFinished(omx_vid_sched,OMX_CommandPortEnable,omx_shed_output_port)
1575                                         || !CommandFinished(omx_vid_rend,OMX_CommandPortEnable,omx_rend_input_port)) {
1576                 clock_mutex.unlock();
1577                 DeAllocateCodecsOMX();
1578                 return 0;
1579         }
1580
1581         if (!ChangeComponentState(omx_vid_rend,OMX_StateIdle)) {
1582                 logger->debug(TAG, "vid_rend ChangeComponentState");
1583                 clock_mutex.unlock();
1584                 DeAllocateCodecsOMX();
1585                 return 0;
1586         }
1587
1588         if (dodeint) {
1589                 if (!ChangeComponentState(omx_vid_deint,OMX_StateExecuting)) {
1590                         logger->debug(TAG, "vid_vid_deint ChangeComponentState");
1591                         clock_mutex.unlock();
1592                         DeAllocateCodecsOMX();
1593                         return 0;
1594                 }
1595                 DisablePort(omx_vid_deint,omx_deint_output_port,false);
1596                 DisablePort(omx_vid_deint,omx_deint_input_port,false);
1597         }
1598
1599         if (!ChangeComponentState(omx_vid_sched,OMX_StateExecuting)) {
1600                 logger->debug(TAG, "omx_vid_sched ChangeComponentState Execute");
1601                 clock_mutex.unlock();
1602                 DeAllocateCodecsOMX();
1603                 return 0;
1604         }
1605
1606         if (!ChangeComponentState(omx_vid_rend,OMX_StateExecuting)) {
1607                 logger->debug(TAG, "omx_vid_rend ChangeComponentState Execute");
1608                 clock_mutex.unlock();
1609                 DeAllocateCodecsOMX();
1610                 return 0;
1611         }
1612
1613         //raspbi specifif
1614         /*OMX_CONFIG_DISPLAYREGIONTYPE dispconf;
1615         memset(&dispconf,0,sizeof(dispconf));
1616         dispconf.nSize=sizeof(dispconf);
1617         dispconf.nVersion.nVersion=OMX_VERSION;
1618
1619         dispconf.nPortIndex=omx_rend_input_port;
1620
1621         dispconf.set=OMX_DISPLAY_SET_LAYER ;
1622         dispconf.layer=1;
1623         error=OMX_SetConfig(omx_vid_rend,OMX_IndexConfigDisplayRegion,&dispconf);
1624         if (error!=OMX_ErrorNone){
1625                 logger->debug(TAG, "Init OMX_IndexConfigDisplayRegion failed {:#x}", error);
1626                 clock_mutex.unlock();
1627                 DeAllocateCodecsOMX();
1628                 return 0;
1629         }*/
1630
1631 /*      dispconf.set=OMX_DISPLAY_SET_FULLSCREEN ;
1632         dispconf.fullscreen=OMX_FALSE;
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();
1638                 return 0;
1639         }
1640
1641         dispconf.set=OMX_DISPLAY_SET_DEST_RECT;
1642         dispconf.dest_rect.x_offset=100;
1643         dispconf.dest_rect.y_offset=100;
1644         dispconf.dest_rect.width=640;
1645         dispconf.dest_rect.height=480;
1646         error=OMX_SetParameter(omx_vid_rend,OMX_IndexConfigDisplayRegion,&dispconf);
1647         if (error!=OMX_ErrorNone){
1648                 logger->debug(TAG, "Init OMX_IndexConfigDisplayRegion failed {:#x}", error);
1649                 clock_mutex.unlock();
1650                 DeAllocateCodecsOMX();
1651                 return 0;
1652         }*/
1653
1654
1655         //playbacktimeoffset=-GetCurrentSystemTime();
1656
1657         iframemode=false;
1658         omx_running=true;
1659         clock_mutex.unlock();
1660         clockUnpause();
1661         updateMode();
1662
1663         setClockExecutingandRunning();
1664
1665
1666
1667
1668
1669         return 1;
1670 }
1671
1672 int VideoOMX::idleClock()
1673 {
1674         //OMX_ERRORTYPE error;
1675         OMX_STATETYPE temp_state;
1676         clock_mutex.lock();
1677         OMX_GetState(omx_clock,&temp_state);
1678
1679         if (temp_state!=OMX_StateIdle) {
1680                 if (!ChangeComponentState(omx_clock,OMX_StateIdle)) {
1681                         logger->debug(TAG, "omx_clock ChangeComponentState Idle failed");
1682                         clock_mutex.unlock();
1683                         return 0;
1684                 }
1685         }
1686         clock_mutex.unlock();
1687         return 1;
1688 }
1689
1690 int VideoOMX::setClockExecutingandRunning()
1691 {
1692         OMX_ERRORTYPE error;
1693         OMX_STATETYPE temp_state;
1694         clock_mutex.lock();
1695         OMX_GetState(omx_clock,&temp_state);
1696
1697         if (temp_state!=OMX_StateExecuting) {
1698                 if (!ChangeComponentState(omx_clock,OMX_StateExecuting)) {
1699                         logger->debug(TAG, "omx_clock ChangeComponentState Execute failed");
1700                         clock_mutex.unlock();
1701                         DeAllocateCodecsOMX();
1702                         return 0;
1703                 }
1704         }
1705
1706         OMX_TIME_CONFIG_CLOCKSTATETYPE clock_conf;
1707         memset(&clock_conf,0,sizeof(clock_conf));
1708         clock_conf.nSize=sizeof(clock_conf);
1709         clock_conf.nVersion.nVersion=OMX_VERSION;
1710         clock_conf.eState=OMX_TIME_ClockStateRunning;
1711         error=OMX_SetConfig(omx_clock,OMX_IndexConfigTimeClockState,&clock_conf);
1712         if (error!=OMX_ErrorNone) {
1713                 logger->debug(TAG, "Clock IndexConfigTimeClockState failed {:#x}", error);
1714                 clock_mutex.unlock();
1715                 DeAllocateCodecsOMX();
1716                 return 0;
1717         }
1718         clock_mutex.unlock();
1719         return 1;
1720
1721 }
1722
1723
1724 int VideoOMX::ChangeComponentState(OMX_HANDLETYPE handle,OMX_STATETYPE type,bool wait) //needs to be called with locked mutex
1725 {
1726         OMX_ERRORTYPE error;
1727         error=OMX_SendCommand(handle,OMX_CommandStateSet,type,0);
1728         if (error!=OMX_ErrorNone){
1729                 logger->debug(TAG, "handle {:#x} Send Command to OMX State {:#x} {:#x}",handle,type, error);
1730                 return 0;
1731         }
1732
1733         if (wait) {
1734                 if (!CommandFinished(handle,OMX_CommandStateSet,type)) {
1735                         return 0;
1736                 }
1737         }
1738
1739         return 1;
1740 }
1741
1742
1743 int VideoOMX::EnablePort(OMX_HANDLETYPE handle,OMX_U32 port,bool wait) //needs to be called with locked mutex
1744 {
1745         OMX_ERRORTYPE error;
1746         bool skip=false;
1747
1748         OMX_PARAM_PORTDEFINITIONTYPE pdt;
1749         memset(&pdt,0,sizeof(pdt));
1750         pdt.nSize=sizeof(pdt);
1751         pdt.nVersion.nVersion=OMX_VERSION;
1752         pdt.nPortIndex=port;
1753         error=OMX_GetParameter(handle,OMX_IndexParamPortDefinition, &pdt);
1754         if (error==OMX_ErrorNone) {
1755                 if(pdt.bEnabled==OMX_TRUE) {
1756                         skip=true; //already disabled;
1757                 }
1758
1759         }
1760
1761         if (!skip) {
1762                 error=OMX_SendCommand(handle,OMX_CommandPortEnable,port,0);
1763                 if (error!=OMX_ErrorNone){
1764                         logger->debug(TAG, "handle {:#x} Send Command to enable port {:#x} {:#x}",handle,port, error);
1765                         return 0;
1766                 }
1767
1768                 if (!wait) return 1;
1769                 if (!CommandFinished(handle,OMX_CommandPortEnable,port)) {
1770                         return 0;
1771                 }
1772
1773         }
1774         return 1;
1775 }
1776
1777
1778 int VideoOMX::DisablePort(OMX_HANDLETYPE handle,OMX_U32 port,bool wait) //needs to be called with locked mutex
1779 {
1780         OMX_ERRORTYPE error;
1781         bool skip=false;
1782
1783         OMX_PARAM_PORTDEFINITIONTYPE pdt;
1784         memset(&pdt,0,sizeof(pdt));
1785         pdt.nSize=sizeof(pdt);
1786         pdt.nVersion.nVersion=OMX_VERSION;
1787         pdt.nPortIndex=port;
1788         error=OMX_GetParameter(handle,OMX_IndexParamPortDefinition, &pdt);
1789         if (error==OMX_ErrorNone) {
1790                 if(pdt.bEnabled==OMX_FALSE) {
1791                         skip=true; //already disabled;
1792                 }
1793
1794         }
1795
1796
1797         if (!skip) {
1798                 error=OMX_SendCommand(handle,OMX_CommandPortDisable,port,0);
1799                 if (error!=OMX_ErrorNone){
1800                         logger->debug(TAG, "handle {:#x} Send Command to disable port {:#x} {:#x}",handle,port, error);
1801                         return 0;
1802                 }
1803
1804                 if (!wait) return 1;
1805                 if (!CommandFinished(handle,OMX_CommandPortDisable,port)) {
1806                         return 0;
1807                 }
1808         }
1809
1810         return 1;
1811 }
1812
1813 int VideoOMX::WaitForEvent(OMX_HANDLETYPE handle,OMX_U32 event, int wait) //needs to be called with locked mutex
1814 {
1815         int i=0;
1816         int iend=(wait/5+1);
1817         while (i<iend) {
1818                 omx_event_mutex.lock();
1819                 std::list<VPE_OMX_EVENT>::iterator itty=omx_events.begin();
1820                 while (itty!=omx_events.end()) {
1821
1822                         VPE_OMX_EVENT current=*itty;
1823                         if (current.handle==handle) { //this is ours
1824                                 if (current.event_type==OMX_EventError) {
1825                                         omx_events.erase(itty);
1826                                         omx_event_mutex.unlock();
1827                                         logger->debug(TAG, "WaitForEvent Finished on Error");
1828                                         return 0;
1829
1830                                 } else if (current.event_type==event) {
1831                                         omx_events.erase(itty);
1832                                         omx_event_mutex.unlock();
1833                                         logger->debug(TAG, "WaitForEvent Finished Completed");
1834                                         return 1;
1835                                 }
1836                         }
1837                         itty++;
1838
1839                 }
1840                 omx_event_mutex.unlock();
1841
1842         //logger->debug(TAG, "WaitForEvent");
1843         std::unique_lock<std::mutex> ul(omx_event_ready_signal_mutex);
1844         omx_event_ready_signal.wait_for(ul, std::chrono::milliseconds(10));
1845         ul.unlock();
1846
1847         i++;
1848
1849         }
1850         logger->debug(TAG, "WaitForEvent waited too long {:p} {:#x}",(void*)handle,event);
1851         return 0;
1852
1853 }
1854
1855 int VideoOMX::clearEvents()
1856 {
1857         omx_event_mutex.lock();
1858         omx_events.clear();
1859         omx_event_mutex.unlock();
1860
1861         return 1;
1862 }
1863
1864 int VideoOMX::clearEventsForComponent(OMX_HANDLETYPE handle)
1865 {
1866         omx_event_mutex.lock();
1867         std::list<VPE_OMX_EVENT>::iterator itty=omx_events.begin();
1868         while (itty!=omx_events.end()) {
1869                 VPE_OMX_EVENT current=*itty;
1870                 if (current.handle==handle) { //this is ours
1871                         itty=omx_events.erase(itty);
1872                         continue;
1873                 }
1874                 itty++;
1875
1876         }
1877         omx_event_mutex.unlock();
1878         return 1;
1879 }
1880
1881 void VideoOMX::checkForStalledBuffers()
1882 {
1883         //logger->debug(TAG, "Check stalled");
1884         clock_mutex.lock();
1885         omx_event_mutex.lock();
1886         std::list<VPE_OMX_EVENT>::iterator itty=omx_events.begin();
1887         while (itty!=omx_events.end()) {
1888                 VPE_OMX_EVENT current=*itty;
1889                 if (current.event_type==OMX_EventParamOrConfigChanged && current.data1==omx_codec_output_port
1890                                 && current.handle==omx_vid_dec && current.data2==OMX_IndexConfigBufferStall) {
1891                         OMX_ERRORTYPE error;
1892                         OMX_CONFIG_BUFFERSTALLTYPE stall_conf;
1893                         memset(&stall_conf,0,sizeof(stall_conf));
1894                         stall_conf.nSize=sizeof(stall_conf);
1895                         stall_conf.nVersion.nVersion=OMX_VERSION;
1896                         stall_conf.nPortIndex=omx_codec_output_port;
1897                         stall_conf.nDelay=200000;
1898
1899                         error=OMX_GetConfig(omx_vid_dec,OMX_IndexConfigBufferStall,&stall_conf);
1900                         if (error!=OMX_ErrorNone){
1901                                         logger->debug(TAG, "Get OMX_IndexConfigBufferStall failed {:#x}", error);
1902                                         clock_mutex.unlock();
1903                                         omx_event_mutex.unlock();
1904                                         return ;
1905                                 }
1906                         if (stall_conf.bStalled==OMX_TRUE) {
1907                                 omx_vid_stalled=true;
1908                                 logger->debug(TAG, "Video decoder stalled! {}", stall_conf.nDelay);
1909                         } else {
1910                                 omx_vid_stalled=false;
1911                                 logger->debug(TAG, "Video decoder unstalled! {}",stall_conf.nDelay);
1912                         }
1913                         omx_events.erase(itty);
1914                         break;
1915                 }
1916                 itty++;
1917         }
1918         omx_event_mutex.unlock();
1919         clock_mutex.unlock();
1920 }
1921
1922
1923
1924
1925 int VideoOMX::CommandFinished(OMX_HANDLETYPE handle,OMX_U32 command,OMX_U32 data2) //needs to be called with locked mutex
1926 {
1927         int i=0;
1928         while (i<200/*1000*/) {
1929                 omx_event_mutex.lock();
1930                 std::list<VPE_OMX_EVENT>::iterator itty=omx_events.begin();
1931                 while (itty!=omx_events.end()) {
1932
1933                         VPE_OMX_EVENT current=*itty;
1934                         if (current.handle==handle) { //this is ours
1935                                 if (current.event_type==OMX_EventError) {
1936                                         omx_events.erase(itty);
1937                                         omx_event_mutex.unlock();
1938                                         logger->debug(TAG, "Command Finished on Error {:#x}",current.data1);
1939                                         return 0;
1940
1941                                 } else if (current.event_type==OMX_EventCmdComplete && current.data1==command && current.data2==data2) {
1942                                         omx_events.erase(itty);
1943                                         omx_event_mutex.unlock();
1944                                         //logger->debug(TAG, "Command Finished Completed");
1945                                         return 1;
1946                                 }
1947                         }
1948                         itty++;
1949
1950                 }
1951                 omx_event_mutex.unlock();
1952
1953         std::unique_lock<std::mutex> ul(omx_event_ready_signal_mutex);
1954         omx_event_ready_signal.wait_for(ul, std::chrono::milliseconds(10));
1955         ul.unlock();
1956
1957                 i++;
1958
1959         }
1960         logger->debug(TAG, "CommandFinished waited too long {:#x} {:#x} {:#x}",handle,command, data2);
1961         return 0;
1962
1963 }
1964
1965
1966
1967 int VideoOMX::PrepareInputBufsOMX() //needs to be called with locked mutex
1968 {
1969         OMX_ERRORTYPE error;
1970         OMX_PARAM_PORTDEFINITIONTYPE port_def_type;
1971         memset(&port_def_type,0,sizeof(port_def_type));
1972         port_def_type.nSize=sizeof(port_def_type);
1973         port_def_type.nVersion.nVersion=OMX_VERSION;
1974         port_def_type.nPortIndex=omx_codec_input_port;
1975
1976         error=OMX_GetParameter(omx_vid_dec,OMX_IndexParamPortDefinition, &port_def_type);
1977
1978         if (error!=OMX_ErrorNone){
1979                         logger->debug(TAG, "Get OMX OMX_IndexParamPortDefinition failed {:#x}", error);
1980         }
1981 /*      logger->debug(TAG, "Port para {} {} {} {} {} {} {}", port_def_type.nBufferCountActual,
1982                         port_def_type.nBufferCountMin,port_def_type.nBufferSize,port_def_type.bEnabled,port_def_type.bPopulated,
1983                         port_def_type.bBuffersContiguous,port_def_type.nBufferAlignment);*/
1984
1985         port_def_type.nBufferCountActual=100;
1986         port_def_type.nBufferSize=std::max(toi4(port_def_type.nBufferSize),150000); // for transcoder important
1987
1988         error=OMX_SetParameter(omx_vid_dec,OMX_IndexParamPortDefinition, &port_def_type);
1989
1990         if (error!=OMX_ErrorNone){
1991                         logger->debug(TAG, "Set OMX OMX_IndexParamPortDefinition failed {:#x}", error);
1992         }
1993
1994
1995         error=OMX_SendCommand(omx_vid_dec,OMX_CommandPortEnable,omx_codec_input_port,0);
1996         if (error!=OMX_ErrorNone){
1997                 logger->debug(TAG, "Prepare Input bufs Send Command to enable port {:#x}", error);
1998                 return 0;
1999         }
2000
2001         input_bufs_omx_mutex.lock();
2002         for (unsigned int i=0; i< port_def_type.nBufferCountActual;i++) {
2003
2004         //      unsigned char* new_buffer_data=(unsigned char*)malloc(port_def_type.nbufferSize);
2005                 OMX_BUFFERHEADERTYPE *buf_head=NULL;
2006         /*      error=OMX_Usebuffer(omx_vid_dec,&buf_head,omx_codec_input_port,NULL,port_def_type.nbufferSize,new_buffer_data);
2007                 if (error!=OMX_ErrorNone){
2008                         logger->debug(TAG, "Use OMX_Usebuffer failed {:#x}", error);
2009                         input_bufs_omx_mutex.unlock();
2010                         return 0;
2011                 }*/
2012                 error=OMX_AllocateBuffer(omx_vid_dec,&buf_head,omx_codec_input_port,NULL,port_def_type.nBufferSize);
2013                 if (error!=OMX_ErrorNone){
2014                         logger->debug(TAG, "Use OMX_AllocateBuffer failed {:#x}", error);
2015                                 input_bufs_omx_mutex.unlock();
2016                         return 0;
2017                 }
2018                 input_bufs_omx_all.push_back(buf_head);
2019                 input_bufs_omx_free.push_back(buf_head);
2020         }
2021         omx_first_frame=true;
2022
2023         firstsynched=false;
2024         cur_input_buf_omx=NULL;
2025         input_bufs_omx_mutex.unlock();
2026
2027
2028         logger->debug(TAG, "PrepareInputBufsOMX mark3");
2029         if (!CommandFinished(omx_vid_dec,OMX_CommandPortEnable,omx_codec_input_port)) {
2030                 return 0;
2031         }
2032         logger->debug(TAG, "PrepareInputBufsOMX mark4");
2033
2034         return 1;
2035 }
2036
2037 int VideoOMX::DestroyInputBufsOMX() //need s to be called with locked mutex
2038 {
2039         OMX_ERRORTYPE error;
2040
2041         cur_input_buf_omx=NULL;
2042         input_bufs_omx_mutex.lock();
2043         for (u4 i=0; i< input_bufs_omx_all.size();i++) {
2044         //      free(input_bufs_omx_all[i]->pBuffer);
2045         //      input_bufs_omx_all[i]->pBuffer=NULL;
2046                 error=OMX_FreeBuffer(omx_vid_dec,omx_codec_input_port,input_bufs_omx_all[i]);
2047                 if (error!=OMX_ErrorNone){
2048                         logger->debug(TAG, "Use OMX_FreeBuffer failed {:#x}", error);
2049                         input_bufs_omx_mutex.unlock();
2050                         return 0;
2051                 }
2052
2053         }
2054         input_bufs_omx_all.clear();
2055         input_bufs_omx_free.clear();
2056         input_bufs_omx_mutex.unlock();
2057
2058         return 1;
2059 }
2060
2061
2062
2063 int VideoOMX::FlushRenderingPipe()
2064 {
2065         OMX_ERRORTYPE error;
2066
2067         if (!dodeint) {
2068
2069                 error = OMX_SendCommand(omx_vid_dec, OMX_CommandFlush,
2070                                 omx_codec_output_port, NULL);
2071                 if (error != OMX_ErrorNone) {
2072                         LogNT::getInstance()->debug(TAG,
2073                                         "OMX_Flush codec out 1 failed {:#x}", error);
2074
2075                 }
2076
2077                 error = OMX_SendCommand(omx_vid_sched, OMX_CommandFlush,
2078                                 omx_shed_input_port, NULL);
2079                 if (error != OMX_ErrorNone) {
2080                         LogNT::getInstance()->debug(TAG,
2081                                         "OMX_Flush shed in 2 failed {:#x}", error);
2082
2083                 }
2084
2085                 if (!CommandFinished(omx_vid_dec, OMX_CommandFlush,
2086                                 omx_codec_output_port)) {
2087                         LogNT::getInstance()->debug(TAG,
2088                                         "flush cmd codec  3 failed");
2089                 }
2090
2091                 if (!CommandFinished(omx_vid_sched, OMX_CommandFlush,
2092                                 omx_shed_input_port)) {
2093                         LogNT::getInstance()->debug(TAG,
2094                                         "flush cmd  shed 4 failed");
2095                 }
2096         } else {
2097                 error = OMX_SendCommand(omx_vid_dec, OMX_CommandFlush,
2098                                 omx_codec_output_port, NULL);
2099                 if (error != OMX_ErrorNone) {
2100                         LogNT::getInstance()->debug(TAG,
2101                                         "OMX_Flush codec out 5 failed {:#x}", error);
2102
2103                 }
2104
2105                 error = OMX_SendCommand(omx_vid_deint, OMX_CommandFlush,
2106                                 omx_deint_input_port, NULL);
2107                 if (error != OMX_ErrorNone) {
2108                         LogNT::getInstance()->debug(TAG,
2109                                         "OMX_Flush deint in 6 failed {:#x}", error);
2110
2111                 }
2112
2113                 if (!CommandFinished(omx_vid_dec, OMX_CommandFlush,
2114                                 omx_codec_output_port)) {
2115                         LogNT::getInstance()->debug(TAG,
2116                                         "flush cmd codec  7 failed");
2117                 }
2118
2119                 if (!CommandFinished(omx_vid_deint, OMX_CommandFlush,
2120                                 omx_deint_input_port)) {
2121                         LogNT::getInstance()->debug(TAG,
2122                                         "flush cmd  deint 8 failed");
2123                 }
2124
2125                 //m
2126                 error = OMX_SendCommand(omx_vid_deint, OMX_CommandFlush,
2127                                 omx_deint_output_port, NULL);
2128                 if (error != OMX_ErrorNone) {
2129                         LogNT::getInstance()->debug(TAG,
2130                                         "OMX_Flush deint out 9 failed {:#x}", error);
2131
2132                 }
2133
2134                 error = OMX_SendCommand(omx_vid_sched, OMX_CommandFlush,
2135                                 omx_shed_input_port, NULL);
2136                 if (error != OMX_ErrorNone) {
2137                         LogNT::getInstance()->debug(TAG,
2138                                         "OMX_Flush shed in 10 failed {:#x}", error);
2139
2140                 }
2141
2142                 if (!CommandFinished(omx_vid_deint, OMX_CommandFlush,
2143                                 omx_deint_output_port)) {
2144                         LogNT::getInstance()->debug(TAG,
2145                                         "flush cmd deint 11 failed");
2146                 }
2147
2148                 if (!CommandFinished(omx_vid_sched, OMX_CommandFlush,
2149                                 omx_shed_input_port)) {
2150                         LogNT::getInstance()->debug(TAG,
2151                                         "flush cmd  shed 12 failed");
2152                 }
2153
2154         }
2155
2156
2157
2158
2159         error = OMX_SendCommand(omx_vid_rend, OMX_CommandFlush,
2160                         omx_rend_input_port, NULL);
2161         if (error != OMX_ErrorNone) {
2162                 LogNT::getInstance()->debug(TAG,
2163                                 "OMX_Flush rend in failed {:#x}", error);
2164
2165         }
2166
2167         error = OMX_SendCommand(omx_vid_sched, OMX_CommandFlush,
2168                         omx_shed_output_port, NULL);
2169         if (error != OMX_ErrorNone) {
2170                 LogNT::getInstance()->debug(TAG,
2171                                 "OMX_Flush shed out failed {:#x}", error);
2172
2173         }
2174
2175
2176
2177         if (!CommandFinished(omx_vid_rend, OMX_CommandFlush,
2178                         omx_rend_input_port)) {
2179                 LogNT::getInstance()->debug(TAG,
2180                                 "flush cmd shed rend failed");
2181         }
2182
2183         if (!CommandFinished(omx_vid_sched, OMX_CommandFlush,
2184                         omx_shed_output_port)) {
2185                 LogNT::getInstance()->debug(TAG,
2186                                 "flush cmd shed rend failed");
2187         }
2188
2189         return 1;
2190 }
2191
2192
2193 int VideoOMX::DeAllocateCodecsOMX()
2194 {
2195         OMX_ERRORTYPE error;
2196         omx_running=false;
2197           logger->debug(TAG, "enter deallocatecodecsomx");
2198
2199    if (cur_input_buf_omx) {
2200                 cur_input_buf_omx->nFlags|=OMX_BUFFERFLAG_EOS;
2201                 error=ProtOMXEmptyThisBuffer(omx_vid_dec,cur_input_buf_omx);
2202                 if (error!=OMX_ErrorNone) {
2203                         logger->debug(TAG, "OMX_EmptyThisBuffer failed {:#x}", error);
2204                 }
2205
2206                 cur_input_buf_omx=NULL;//write out old data
2207         }
2208    clock_mutex.lock();
2209    clearEvents();
2210         if (omx_vid_dec) {
2211                 // first stop the omx elements
2212                 if (!ChangeComponentState(omx_vid_dec,OMX_StateIdle)) {
2213                         logger->debug(TAG, "vid_dec ChangeComponentState");
2214
2215                 }
2216                 clock_mutex.unlock();
2217
2218                 idleClock();
2219                 clock_mutex.lock();
2220
2221                 if (dodeint) {
2222                         if (!ChangeComponentState(omx_vid_deint, OMX_StateIdle)) {
2223                                 LogNT::getInstance()->debug(TAG,
2224                                                 "vid_deint ChangeComponentState");
2225
2226                         }
2227                 }
2228
2229
2230                 if (!ChangeComponentState(omx_vid_sched,OMX_StateIdle)) {
2231                         logger->debug(TAG, "vid_shed ChangeComponentState");
2232
2233                 }
2234
2235                 if (!ChangeComponentState(omx_vid_rend,OMX_StateIdle)) {
2236                         logger->debug(TAG, "vid_rend ChangeComponentState");
2237
2238                 }
2239
2240
2241
2242         // TODO proper deinit sequence
2243                 // first flush all buffers
2244                 FlushRenderingPipe();
2245
2246
2247
2248
2249
2250                 error=OMX_SendCommand(omx_clock,OMX_CommandFlush, omx_clock_output_port, NULL);
2251                 if (error!=OMX_ErrorNone){
2252                         logger->debug(TAG, "OMX_Flush clock out failed {:#x}", error);
2253
2254                 }
2255
2256                 error=OMX_SendCommand(omx_vid_sched,OMX_CommandFlush, omx_shed_clock_port, NULL);
2257                 if (error!=OMX_ErrorNone){
2258                         logger->debug(TAG, "OMX_Flush shed clock failed {:#x}", error);
2259
2260                 }
2261
2262                 if (!CommandFinished(omx_clock,OMX_CommandFlush,omx_clock_output_port) ||
2263                         !CommandFinished(omx_vid_sched,OMX_CommandFlush,omx_shed_clock_port)) {
2264                                 logger->debug(TAG, "flush cmd clock shed failed");
2265                 }
2266
2267
2268
2269
2270                 error = OMX_SendCommand(omx_vid_dec, OMX_CommandFlush,
2271                                 omx_codec_input_port, NULL);
2272                 if (error != OMX_ErrorNone) {
2273                         LogNT::getInstance()->debug(TAG,
2274                                         "OMX_Flush codec out failed {:#x}", error);
2275
2276                 }
2277
2278
2279
2280
2281                 DestroyInputBufsOMX();
2282
2283                 //todo flushing
2284                 if (!DisablePort(omx_vid_sched,omx_shed_output_port,true)) {
2285                         logger->debug(TAG, "Disable Tunnel Port failed 2");
2286                 }
2287                 if (!DisablePort(omx_vid_rend,omx_rend_input_port,true)) {
2288                         logger->debug(TAG, "Disable Tunnel Port failed 1");
2289                 }
2290
2291
2292
2293
2294                 if (!DisablePort(omx_vid_dec,omx_codec_output_port,true)) {
2295                         logger->debug(TAG, "Disable Tunnel Port failed 6");
2296                 }
2297
2298
2299
2300                 if (!DisablePort(omx_vid_dec,omx_codec_input_port,true)) {
2301                         logger->debug(TAG, "Disable Tunnel Port failed 7");
2302                 }
2303
2304                 if (dodeint) {
2305                         if (!DisablePort(omx_vid_deint,omx_deint_output_port,true)) {
2306                                 logger->debug(TAG, "Disable Tunnel Port failed 6a");
2307                         }
2308
2309
2310
2311                         if (!DisablePort(omx_vid_deint,omx_deint_input_port,true)) {
2312                                 logger->debug(TAG, "Disable Tunnel Port failed 7a");
2313                         }
2314                 }
2315
2316
2317
2318                 if (!DisablePort(omx_vid_sched,omx_shed_input_port,true)) {
2319                         logger->debug(TAG, "Disable Tunnel Port failed 3");
2320                 }
2321
2322                 if (!DisablePort(omx_vid_sched,omx_shed_clock_port,true)) {
2323                         logger->debug(TAG, "Disable Tunnel Port failed 4");
2324                 }
2325
2326                 if (!DisablePort(omx_clock,omx_clock_output_port,true)) {
2327                         logger->debug(TAG, "Disable Tunnel Port failed 5");
2328                 }
2329
2330
2331
2332
2333                 error=OMX_SetupTunnel(omx_vid_dec,omx_codec_output_port,NULL,0);
2334                 if (error!=OMX_ErrorNone) {
2335                         logger->debug(TAG, "OMX_Setup tunnel teardown failed {:#x}", error);
2336
2337                 }
2338
2339                 if (dodeint) {
2340                         error=OMX_SetupTunnel(omx_vid_deint,omx_deint_input_port,NULL,0);
2341                         if (error!=OMX_ErrorNone) {
2342                                 logger->debug(TAG, "OMX_Setup tunnel teardown failed {:#x}", error);
2343                         }
2344
2345
2346                         error=OMX_SetupTunnel(omx_vid_deint,omx_deint_output_port,NULL,0);
2347                         if (error!=OMX_ErrorNone) {
2348                                 logger->debug(TAG, "OMX_Setup tunnel teardown failed {:#x}", error);
2349                         }
2350                 }
2351
2352                 error=OMX_SetupTunnel(omx_vid_sched,omx_shed_input_port,NULL,0);
2353                 if (error!=OMX_ErrorNone) {
2354                         logger->debug(TAG, "OMX_Setup tunnel teardown failed {:#x}", error);
2355
2356                 }
2357
2358
2359                 error=OMX_SetupTunnel(omx_vid_sched,omx_shed_output_port,NULL,0);
2360                 if (error!=OMX_ErrorNone) {
2361                         logger->debug(TAG, "OMX_Setup tunnel teardown failed {:#x}", error);
2362
2363                 }
2364
2365                 error=OMX_SetupTunnel(omx_vid_rend,omx_rend_input_port,NULL,0);
2366                 if (error!=OMX_ErrorNone) {
2367                         logger->debug(TAG, "OMX_Setup tunnel teardown failed {:#x}", error);
2368
2369                 }
2370
2371
2372                 error=OMX_SetupTunnel(omx_clock,omx_clock_output_port,NULL,0);
2373                 if (error!=OMX_ErrorNone) {
2374                         logger->debug(TAG, "OMX_Setup tunnel teardown failed {:#x}", error);
2375
2376                 }
2377
2378                 error=OMX_SetupTunnel(omx_vid_sched,omx_shed_clock_port,NULL,0);
2379                 if (error!=OMX_ErrorNone) {
2380                         logger->debug(TAG, "OMX_Setup tunnel teardown failed {:#x}", error);
2381
2382                 }
2383
2384
2385
2386
2387                 error=OMX_FreeHandle(omx_vid_dec);
2388                 error=OMX_FreeHandle(omx_vid_sched);
2389                 error=OMX_FreeHandle(omx_vid_rend);
2390                 if (dodeint) error=OMX_FreeHandle(omx_vid_deint);
2391                 omx_vid_dec=NULL;
2392                 omx_vid_stalled = false;
2393                 clock_mutex.unlock();
2394                 destroyClock();
2395                 if (error!=OMX_ErrorNone) {
2396                         logger->debug(TAG, "FreeHandle failed {}", error);
2397                 }
2398         } else  clock_mutex.unlock();
2399           logger->debug(TAG, "leave deallocate codecs OMX");
2400
2401         return 1;
2402 }
2403
2404
2405 void VideoOMX::destroyClock()
2406 {
2407         clock_mutex.lock();
2408         if (clock_references>0) {
2409                 clock_references--;
2410                 if (clock_references==0) {
2411                         OMX_ERRORTYPE error;
2412                         logger->debug(TAG, "destroy omx clock");
2413                         error=OMX_FreeHandle(omx_clock);
2414                         if (error!=OMX_ErrorNone) {
2415                                 logger->debug(TAG, "FreeHandle failed {}", error);
2416                         }
2417
2418                 }
2419         }
2420         clock_mutex.unlock();
2421
2422 }
2423
2424 int VideoOMX::stop()
2425 {
2426   if (!initted) return 0;
2427   iframemode=false;
2428
2429   //Check if libav mode
2430   DeAllocateCodecsOMX();
2431
2432
2433
2434
2435 //  if (ioctl(fdVideo, AV_SET_VID_STOP, 0) != 0) return 0;
2436   return 1;
2437 }
2438
2439 int VideoOMX::reset()
2440 {
2441   if (!initted) return 0;
2442
2443   iframemode=false;
2444   DeAllocateCodecsOMX();
2445 //  if (ioctl(fdVideo, AV_SET_VID_RESET, 0x11) != 0) return 0;
2446   return 1;
2447 }
2448
2449 int VideoOMX::pause()
2450 {
2451   if (!initted) return 0;
2452   logger->debug(TAG, "enter pause");
2453  // clockPause();
2454   // ignore it audio handles this
2455   return 1;
2456 }
2457
2458 int VideoOMX::unPause() // FIXME get rid - same as play!! Not here!
2459 {
2460   if (!initted) return 0;
2461   logger->debug(TAG, "enter unpause");
2462
2463   //clockUnpause();
2464   //ignore it audio handles this
2465
2466   return 1;
2467 }
2468
2469 int VideoOMX::fastForward()
2470 {
2471   if (!initted) return 0;
2472
2473 //  if (ioctl(fdVideo, AV_SET_VID_libavWD, 1) != 0) return 0;
2474   return 1;
2475 }
2476
2477 int VideoOMX::unFastForward()
2478 {
2479   if (!initted) return 0;
2480
2481 //  if (ioctl(fdVideo, AV_SET_VID_RESET, 0x11) != 0) return 0; // don't need this.
2482
2483  //// if (ioctl(fdVideo, AV_SET_VID_PLAY, 0) != 0) return 0;
2484   return 1;
2485 }
2486
2487 int VideoOMX::attachFrameBuffer()
2488 {
2489   if (!initted) return 0;
2490
2491 //  if (ioctl(fdVideo, AV_SET_VID_FB, 0) != 0) return 0;
2492   return 1;
2493 }
2494
2495 int VideoOMX::blank(void)
2496 {
2497 //  if (ioctl(fdVideo, AV_SET_VID_FB, 1) != 0) return 0;
2498 //  if (ioctl(fdVideo, AV_SET_VID_FB, 0) != 0) return 0;
2499   return 1;
2500 }
2501
2502 u8 VideoOMX::getCurrentTimestamp() {
2503         if (iframemode)
2504                 return 0;
2505         long long ncur_clock_time = cur_clock_time;
2506         if (omx_running) {
2507                 int oldcancelstate;
2508                 int oldcanceltype;
2509                 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldcancelstate);
2510                 pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldcanceltype);
2511                 clock_mutex.lock();
2512                 OMX_ERRORTYPE error;
2513                 OMX_TIME_CONFIG_CLOCKSTATETYPE clock_conf;
2514                 memset(&clock_conf, 0, sizeof(clock_conf));
2515                 clock_conf.nSize = sizeof(clock_conf);
2516                 clock_conf.nVersion.nVersion = OMX_VERSION;
2517                 error= OMX_GetConfig(omx_clock, OMX_IndexConfigTimeClockState,
2518                                 &clock_conf);
2519                 if (error != OMX_ErrorNone) {
2520                         LogNT::getInstance()->debug(TAG,"getCurrentTimestamp IndexConfigTimeClockState failed {:#x}",error);
2521                 }
2522
2523                 if (clock_conf.eState == OMX_TIME_ClockStateRunning) {
2524
2525                         OMX_TIME_CONFIG_TIMESTAMPTYPE cur_time_stamp;
2526                         memset(&cur_time_stamp, 0, sizeof(cur_time_stamp));
2527                         cur_time_stamp.nSize = sizeof(cur_time_stamp);
2528                         cur_time_stamp.nVersion.nVersion = OMX_VERSION;
2529                         cur_time_stamp.nPortIndex = omx_clock_output_port;
2530                         error = OMX_GetConfig(omx_clock, OMX_IndexConfigTimeCurrentMediaTime,
2531                                         &cur_time_stamp);
2532                         if (error != OMX_ErrorNone) {
2533                                 LogNT::getInstance()->debug(TAG, "getCurrentTimestamp OMX_IndexConfigTimeCurrentMediaTime failed {:#x}",error);
2534                         } else {
2535                                 long long temp = cur_time_stamp.nTimestamp.nLowPart
2536                                                 | ((long long) cur_time_stamp.nTimestamp.nHighPart << 32);
2537                                 ncur_clock_time = cur_clock_time = temp * 10LL;
2538                         }
2539                 }
2540                 clock_mutex.unlock();
2541                 pthread_setcancelstate(oldcancelstate, NULL);
2542                 pthread_setcanceltype(oldcanceltype, NULL);
2543         }
2544
2545         //ncur_clock_time -= startoffset;
2546         ncur_clock_time -= lastreftimeOMX;
2547         long long result = lastreftimePTS;
2548         if (lastreftimePTS==0) return 0; // invalid time
2549         result += (long long) (ncur_clock_time / 10000LL * 90LL);
2550         if (result < 0)
2551                 result = (1LL << 33) - result;
2552         //LogNT::getInstance()->debug(TAG,"getCurrentTimestamp {} {} {} {} {} {}",ncur_clock_time,cur_clock_time,lastreftimeOMX,lastreftimePTS,result,startoffset);
2553
2554         return result;
2555
2556 }
2557
2558 // to be removed
2559 /*
2560 u4 VideoOMX::timecodeToFrameNumber(u8 timecode)
2561 {
2562   if (format == PAL) return (u4)(((double)timecode / (double)90000) * (double)25);
2563   else               return (u4)(((double)timecode / (double)90000) * (double)30);
2564 }
2565
2566 */
2567 #ifdef DEV
2568 int VideoOMX::test()
2569 {
2570   return 0;
2571
2572 //  u8 stc = 0;
2573 //  return ioctl(fdVideo, AV_SET_VID_STC, &stc);
2574 /*
2575  // reset();
2576   return 1;
2577 */
2578 }
2579
2580 int VideoOMX::test2()
2581 {
2582   return 0;
2583 }
2584 #endif
2585
2586
2587
2588 long long VideoOMX::SetStartOffset(long long curreftime, bool *rsync)
2589 {
2590   *rsync=false;
2591   if (offsetnotset) {
2592     startoffset=curreftime;//offset is set for audio
2593     offsetnotset=false;
2594     offsetvideonotset=false;
2595   } else {
2596     if (offsetvideonotset) {
2597       offsetvideonotset=false;
2598       *rsync=true;
2599     } else {
2600       if ( (curreftime-lastrefvideotime)>10000000LL
2601         || (curreftime-lastrefvideotime)<-10000000LL) {//if pts jumps to big resync
2602         startoffset+=curreftime-lastrefvideotime;
2603         lastrefaudiotime+=curreftime-lastrefvideotime;
2604         //*rsync=true;
2605         offsetaudionotset=true;
2606
2607       }
2608     }
2609
2610   }
2611
2612   lastrefvideotime=curreftime;
2613
2614   return startoffset;
2615
2616 }
2617
2618 long long VideoOMX::SetStartAudioOffset(long long curreftime, bool *rsync)
2619 {
2620   *rsync=false;
2621   if (offsetnotset) {
2622     startoffset=curreftime;
2623     offsetnotset=false;
2624     offsetaudionotset=false;
2625   }else {
2626     if (offsetaudionotset) {
2627       offsetaudionotset=false;
2628       *rsync=true;
2629     } else {
2630       if ( (curreftime-lastrefaudiotime)>10000000LL
2631         || (curreftime-lastrefaudiotime)<-10000000LL) {//if pts jumps to big resync
2632         startoffset+=curreftime-lastrefaudiotime;
2633         lastrefvideotime+=curreftime-lastrefaudiotime;
2634         //*rsync=true;
2635         offsetvideonotset=true;
2636
2637       }
2638     }
2639
2640   }
2641   lastrefaudiotime=curreftime;
2642   return startoffset;
2643
2644 }
2645
2646 void VideoOMX::ResetTimeOffsets() {
2647   offsetnotset=true; //called from demuxer
2648   offsetvideonotset=true;
2649   offsetaudionotset=true;
2650   startoffset=0;
2651   lastrefaudiotime=0;
2652   lastrefvideotime=0;
2653   lastreftimeOMX=0;
2654   lastreftimePTS=0;
2655 }
2656
2657
2658 void VideoOMX::FirstFrameFix()
2659 {
2660         int oldcancelstate;
2661         int oldcanceltype;
2662         Demuxer* demux=Demuxer::getInstance();
2663         pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldcancelstate);
2664         pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldcanceltype);
2665         clock_mutex.lock();
2666         if (WaitForEvent(omx_vid_dec,OMX_EventPortSettingsChanged,0)){
2667                 WaitForEvent(omx_vid_deint,OMX_EventPortSettingsChanged,0); //clear old messages
2668                 OMX_ERRORTYPE error;
2669                 OMX_PARAM_PORTDEFINITIONTYPE port_def_type;
2670                 memset(&port_def_type,0,sizeof(port_def_type));
2671                 port_def_type.nSize=sizeof(port_def_type);
2672                 port_def_type.nVersion.nVersion=OMX_VERSION;
2673                 port_def_type.nPortIndex=omx_codec_output_port;
2674
2675                 error=OMX_GetParameter(omx_vid_dec,OMX_IndexParamPortDefinition, &port_def_type);
2676                 if (error != OMX_ErrorNone) {
2677                         LogNT::getInstance()->debug(TAG,
2678                                         "OMX_IndexParamPortDefinition fix failed {:#x}", error);
2679                         clock_mutex.unlock();
2680                         return;
2681                 }
2682
2683
2684                 LogNT::getInstance()->debug(TAG,
2685                                                         "Deinit first frame fix {} {} {} {} {} {} {} {}",port_def_type.format.video.nFrameWidth , demux->getHorizontalSize(),
2686                                                         port_def_type.format.video.nFrameHeight , demux->getVerticalSize(),port_def_type.format.video.nStride,
2687                                                         port_def_type.format.video.nSliceHeight, port_def_type.format.video.xFramerate,
2688                                                          port_def_type.format.video.bFlagErrorConcealment );
2689                 LogNT::getInstance()->debug(TAG,
2690                                                                         "Deinit first frame fix2 {}  {}",
2691                                                                         port_def_type.format.video.eCompressionFormat ,
2692                                                                         port_def_type.format.video.eColorFormat );
2693                 first_frame=false;
2694
2695                 // we cause the video_decode to determine the interlacing properties
2696                 OMX_CONFIG_INTERLACETYPE il;
2697                 memset(&il,0,sizeof(il));
2698                 il.nSize=sizeof(il);
2699                 il.nVersion.nVersion=OMX_VERSION;
2700                 il.nPortIndex=omx_codec_output_port;
2701                 error=OMX_GetConfig(omx_vid_dec,OMX_IndexConfigCommonInterlace, &il);
2702                 if (error != OMX_ErrorNone) {
2703                         LogNT::getInstance()->debug(TAG,
2704                                         "OMX_IndexConfigCommonInterlace fix failed {:#x}", error);
2705                 }
2706
2707
2708                 DisablePort(omx_vid_dec,omx_codec_output_port,true);
2709                 DisablePort(omx_vid_sched,omx_shed_input_port,true);
2710                 if (dodeint) {
2711
2712                         DisablePort(omx_vid_deint,omx_deint_input_port,true);
2713                         // This is a dirty hack
2714                         DisablePort(omx_vid_deint,omx_deint_output_port,true);
2715
2716
2717                         port_def_type.nPortIndex=omx_deint_output_port;
2718                         error = OMX_SetParameter(omx_vid_deint, OMX_IndexParamPortDefinition,
2719                                         &port_def_type);
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);
2726                                 return;
2727                         }
2728                         // dirty hack end
2729
2730
2731                         port_def_type.nPortIndex=omx_deint_input_port;
2732                         error = OMX_SetParameter(omx_vid_deint, OMX_IndexParamPortDefinition,
2733                                         &port_def_type);
2734                         if (error != OMX_ErrorNone) {
2735                                 LogNT::getInstance()->debug(TAG,
2736                                                 "Set OMX_IndexParamPortDefinition1 failed {:#x}", error);
2737                                 clock_mutex.unlock();
2738                                 pthread_setcancelstate(oldcancelstate, NULL);
2739                                 pthread_setcanceltype(oldcanceltype, NULL);
2740                                 return;
2741                         }
2742                 //      WaitForEvent(omx_vid_dec,OMX_EventPortSettingsChanged);
2743
2744                         LogNT::getInstance()->debug(TAG,
2745                                                                         "Marker");
2746                         EnablePort(omx_vid_deint,omx_deint_input_port,true);
2747                         WaitForEvent(omx_vid_deint,OMX_EventPortSettingsChanged);
2748                         port_def_type.nPortIndex=omx_deint_output_port;
2749                         error = OMX_GetParameter(omx_vid_deint, OMX_IndexParamPortDefinition,
2750                                         &port_def_type);
2751                         if (error != OMX_ErrorNone) {
2752                                 LogNT::getInstance()->debug(TAG,
2753                                                 "Get OMX_IndexParamPortDefinition2 failed {:#x}", error);
2754                                 clock_mutex.unlock();
2755                                 pthread_setcancelstate(oldcancelstate, NULL);
2756                                 pthread_setcanceltype(oldcanceltype, NULL);
2757                                 return;
2758                         }
2759                         LogNT::getInstance()->debug(TAG,
2760                                         "Deinit first frame fix3 {} {} {} {} {} {} {} ",port_def_type.format.image.nFrameWidth , demux->getHorizontalSize(),
2761                                         port_def_type.format.image.nFrameHeight , demux->getVerticalSize(),port_def_type.format.image.nStride,
2762                                         port_def_type.format.image.nSliceHeight, /*port_def_type.format.image.xFramerate,*/
2763                                         port_def_type.format.image.bFlagErrorConcealment );
2764                         LogNT::getInstance()->debug(TAG,
2765                                         "Deinit first frame fix4 {}  {}",
2766                                         port_def_type.format.image.eCompressionFormat ,
2767                                         port_def_type.format.image.eColorFormat );
2768                         DisablePort(omx_vid_deint,omx_deint_output_port,true);
2769
2770                 }
2771                 port_def_type.nPortIndex=omx_shed_input_port;
2772                 error = OMX_SetParameter(omx_vid_sched, OMX_IndexParamPortDefinition,
2773                                         &port_def_type);
2774                 if (error != OMX_ErrorNone) {
2775                         LogNT::getInstance()->debug(TAG,
2776                                         "Set OMX_IndexParamPortDefinition3 failed {:#x}", error);
2777                         clock_mutex.unlock();
2778                         pthread_setcancelstate(oldcancelstate, NULL);
2779                         pthread_setcanceltype(oldcanceltype, NULL);
2780                         return;
2781                 }
2782                 WaitForEvent(omx_vid_sched,OMX_EventPortSettingsChanged);
2783
2784
2785                 if (dodeint) {
2786                         EnablePort(omx_vid_deint,omx_deint_output_port,true);
2787                 }
2788                 EnablePort(omx_vid_dec,omx_codec_output_port,true);
2789                 EnablePort(omx_vid_sched,omx_shed_input_port,true);
2790         }
2791         clock_mutex.unlock();
2792         pthread_setcancelstate(oldcancelstate, NULL);
2793         pthread_setcanceltype(oldcanceltype, NULL);
2794
2795
2796
2797 }
2798
2799
2800 void VideoOMX::PutBufferToPres(OMX_BUFFERHEADERTYPE* buffer)
2801 {
2802
2803         OMX_ERRORTYPE error = ProtOMXEmptyThisBuffer(omx_vid_dec, buffer);
2804         if (error != OMX_ErrorNone) {
2805                 LogNT::getInstance()->debug(TAG,
2806                                 "OMX_EmptyThisBuffer failed {:#x}", error);
2807         }
2808         if (first_frame) FirstFrameFix();
2809
2810 }
2811
2812 OMX_ERRORTYPE VideoOMX::ProtOMXEmptyThisBuffer(OMX_HANDLETYPE handle, OMX_BUFFERHEADERTYPE* buffer)
2813 {
2814         // protect the call to empty this buffer
2815         int oldcancelstate;
2816         int oldcanceltype;
2817 /*      long long temp =buffer->nTimeStamp.nLowPart
2818                                                         | ((long long) buffer->nTimeStamp.nHighPart << 32);*/
2819
2820         //pthread_testcancel();
2821         pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldcancelstate);
2822         pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldcanceltype);
2823         clock_mutex.lock();
2824 // Diagnosis code
2825 /*      OMX_ERRORTYPE error;
2826         OMX_TIME_CONFIG_TIMESTAMPTYPE timestamp;
2827         memset(&timestamp, 0, sizeof(timestamp));
2828         timestamp.nSize = sizeof(timestamp);
2829         timestamp.nVersion.nVersion = OMX_VERSION;
2830         timestamp.nPortIndex =omx_clock_output_port;
2831
2832         error = OMX_GetConfig(omx_clock, OMX_IndexConfigTimeCurrentMediaTime,
2833                         &timestamp);
2834
2835         if (error != OMX_ErrorNone) {
2836                 LogNT::getInstance()->debug(TAG,
2837                                 "Init OMX_IndexConfigAudioRenderingLatencyfailed {:#x} {}", error,
2838                                 omx_rend_input_port);
2839         }
2840         long long temp2 =timestamp.nTimestamp.nLowPart
2841                                                                 | ((long long) timestamp.nTimestamp.nHighPart << 32);
2842
2843
2844         LogNT::getInstance()->info(TAG, "OMXETB {:#x} {} {} {:#x}",handle,temp,temp2,buffer->nFlags);*/
2845         OMX_ERRORTYPE ret_val;
2846         ret_val=OMX_EmptyThisBuffer(handle,buffer);
2847         clock_mutex.unlock();
2848         pthread_setcancelstate(oldcancelstate, NULL);
2849         pthread_setcanceltype(oldcanceltype, NULL);
2850         //pthread_testcancel();
2851         return ret_val;
2852 }
2853
2854 bool VideoOMX::detectIFrame(const u1 *buffer,unsigned int length)
2855 {
2856         const u1* curbuf=buffer;
2857         const u1* curbufend=buffer+length;
2858         unsigned int detector=0xFFFFFFFF;
2859         bool gotaud=false; // have seen access unit delimiter
2860
2861         while (curbuf!=curbufend) {
2862                 detector<<=8;
2863                 detector|=*curbuf;
2864                 if (h264) {
2865                         if (detector==0x00000109) {
2866                                 gotaud=true;
2867                                 detector=0xFFFFFFFF;
2868                         } else if (gotaud &&detector==0x00000001) {
2869                                 curbuf++;
2870                                 if (curbuf!=curbufend) {
2871                                         unsigned char picttype=(*curbuf)& 0x1F;
2872                                         return picttype==0x07;
2873                                 }
2874                         }
2875                 } else {
2876                         if (detector==0x00000100) {
2877                                 curbuf++;
2878                                 if (curbuf==curbufend) return false;
2879                                 curbuf++;
2880                                 if (curbuf==curbufend) return false;
2881                                 unsigned char picttype=((*curbuf) >> 3) & 0x07;
2882                                 return picttype==1;
2883                         }
2884                 }
2885                 curbuf++;
2886         }
2887
2888         return false; // no frame found
2889 }
2890
2891
2892
2893 bool VideoOMX::DrainTargetBufferFull()
2894 {
2895         //Check, if we have OMX output buffers
2896         bool full;
2897         input_bufs_omx_mutex.lock();
2898         full=(input_bufs_omx_free.size()==0);
2899         input_bufs_omx_mutex.unlock();
2900         checkForStalledBuffers(); // check if the decoder has a problem
2901         if (full && omx_vid_stalled && !omx_first_frame) {
2902                 omx_vid_stalled=false;
2903                 logger->debug(TAG, "Decoder is stalled, do a reset!");
2904
2905                 int oldcancelstate;
2906                 int oldcanceltype;
2907                 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldcancelstate);
2908                 pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldcanceltype);
2909
2910                 clock_mutex.lock();
2911                 FlushRenderingPipe();
2912                 omx_first_frame=true;
2913                 clock_mutex.unlock();
2914
2915                 pthread_setcancelstate(oldcancelstate, NULL);
2916                 pthread_setcanceltype(oldcanceltype, NULL);
2917         }
2918         return full;
2919
2920 }
2921
2922 void VideoOMX::PrepareMediaSample(const MediaPacketList& mplist, u4 /* samplepos */)
2923 {
2924
2925         mediapackets.clear();
2926         std::list<MediaPacket>::const_iterator begin=mplist.begin();
2927         std::list<MediaPacket>::const_iterator itty=mplist.begin();
2928         advance(itty,std::min(mplist.size(),10U));
2929         mediapackets.insert(mediapackets.begin(),begin,itty);//front
2930
2931 }
2932
2933 u4 VideoOMX::DeliverMediaSample(u1* buffer, u4 *samplepos)
2934 {
2935         u4 consumed=0;
2936         while (consumed < mediapackets.size()) {
2937             DeliverMediaPacket(mediapackets[consumed], buffer, samplepos);
2938             if (*samplepos == mediapackets[consumed].length) {
2939                 *samplepos = 0;
2940                 consumed++;
2941                 //return 1;
2942             } else return consumed;
2943         }
2944         return consumed;
2945 }
2946
2947 u4 VideoOMX::DeliverMediaPacket(MediaPacket packet,
2948                 const u1* buffer,
2949                 u4 *samplepos)
2950 {
2951         if (packet.type == MPTYPE_VIDEO_H264)
2952         {
2953                 h264=true;
2954         }
2955         else
2956         {
2957                 h264=false;
2958         }
2959
2960
2961         //Later add fail back code for libav
2962 /*      if (!videoon) {
2963                 *samplepos+=packet.length;
2964                 return packet.length;
2965         }*/
2966
2967
2968         if (!omx_running) return 0; // if we are not runnig do not do this
2969
2970         if (isClockPaused()) return 0; //Block if we pause
2971
2972 //      logger->debug(TAG, "DMP mark 1");
2973         //logger->debug(TAG, "DeliverMediaPacketOMX time {}", packet.presentation_time);
2974
2975
2976         /*First Check, if we have an video sample*/
2977         if (iframemode) {
2978                 //samplepos=0;
2979                 MILLISLEEP(10);
2980                 return 0; //Not in iframe mode!
2981         }
2982
2983         u4 headerstrip=0;
2984         if (packet.disconti) {
2985                 firstsynched=false;
2986                 if (cur_input_buf_omx) {
2987                         PutBufferToPres(cur_input_buf_omx);
2988                         cur_input_buf_omx=NULL;
2989                 }
2990         }
2991         //logger->debug(TAG, "DMP mark 2");
2992         /*Inspect PES-Header */
2993
2994 //      OMX_STATETYPE temp_state;
2995 //      OMX_GetState(omx_vid_dec,&temp_state);
2996
2997         if (*samplepos==0) {//stripheader
2998                 headerstrip=buffer[packet.pos_buffer+8]+9/*is this right*/;
2999         //      if (h264) logger->debug(TAG, "PES info {:#x} {:#x} {:#x} {:#x}",
3000         //                      buffer[packet.pos_buffer+0],buffer[packet.pos_buffer+1],buffer[packet.pos_buffer+2],buffer[packet.pos_buffer+3]);
3001                 *samplepos+=headerstrip;
3002                 if (headerstrip>=packet.length) {
3003                      *samplepos=packet.length;// Packet is obviously damaged
3004                      return packet.length;//skip it!
3005                 }
3006                 if ( packet.synched ) {
3007                         if (!firstsynched) {
3008                                 //logger->debug(TAG, "DMP mark 2a");
3009                           // check if this is an I frame, the decoder does not like non I frames at startup!
3010                           if (!detectIFrame(buffer,packet.length)) {
3011                                   *samplepos=packet.length;//if we have not processed at least one
3012                                   //logger->debug(TAG, "DMP mark 3");
3013                                   return packet.length;//synched packet ignore it!
3014                           }
3015                         }
3016                         if (cur_input_buf_omx) {
3017                                 //logger->debug(TAG, "DMP mark 4a");
3018                                 cur_input_buf_omx->nFlags|=OMX_BUFFERFLAG_ENDOFFRAME;
3019                                 PutBufferToPres(cur_input_buf_omx);
3020                                 cur_input_buf_omx=NULL;//write out old data
3021
3022                                 //logger->debug(TAG, "DMP mark 4b");
3023
3024                         }
3025                         firstsynched=true;
3026                 } else {
3027                         if (!firstsynched) {//
3028                                 *samplepos=packet.length;//if we have not processed at least one
3029                                 //logger->debug(TAG, "DMP mark 5");
3030                                 return packet.length;//synched packet ignore it!
3031                         }
3032                 }
3033         }
3034         //logger->debug(TAG, "DMP mark 6");
3035         if (!cur_input_buf_omx) {
3036                 input_bufs_omx_mutex.lock();
3037                 if (input_bufs_omx_free.size()==0) {
3038                         input_bufs_omx_mutex.unlock();
3039                         //logger->debug(TAG, "Deliver MediaPacket no free sample");
3040                         //logger->debug(TAG, "DMP mark 7");
3041                         return 0; // we do not have a free media sample
3042
3043                 }
3044                 cur_input_buf_omx=input_bufs_omx_free.front();
3045                 cur_input_buf_omx->nFilledLen=0;
3046                 cur_input_buf_omx->nOffset=0;
3047                 cur_input_buf_omx->nTimeStamp=intToOMXTicks(0);
3048                 input_bufs_omx_free.pop_front();
3049                 input_bufs_omx_mutex.unlock();
3050         }
3051
3052
3053
3054
3055         if (cur_input_buf_omx->nFilledLen==0) {//will only be changed on first packet
3056                 if (packet.synched) {
3057                 //      logger->debug(TAG, "packet synched marker");
3058
3059                         //lastreftimePTS=packet.pts;
3060                    if (omx_first_frame) { // TODO time
3061                            cur_input_buf_omx->nFlags=OMX_BUFFERFLAG_STARTTIME;
3062                            logger->debug(TAG, "Starttime");
3063                            omx_first_frame=false;
3064                    } else {
3065                            cur_input_buf_omx->nFlags=0;
3066                            //cur_input_buf_omx->nFlags|=OMX_BUFFERFLAG_TIME_UNKNOWN;
3067                    }
3068                    lastreftimeOMX=packet.presentation_time;
3069                   // logger->debug(TAG, "Time code {} pts {}", lastreftimeOMX,packet.pts);
3070                    lastreftimePTS=packet.pts;
3071                    cur_input_buf_omx->nTimeStamp=intToOMXTicks(lastreftimeOMX/10LL); // the clock component is faulty;
3072                 }
3073                 else
3074                 {
3075                         cur_input_buf_omx->nFlags=OMX_BUFFERFLAG_TIME_UNKNOWN;
3076                         cur_input_buf_omx->nTimeStamp=intToOMXTicks(0);
3077                         //logger->debug(TAG, "packet unsynched marker");
3078                 }
3079                 if (packet.disconti) cur_input_buf_omx->nFlags|=OMX_BUFFERFLAG_DISCONTINUITY;
3080
3081
3082
3083         }
3084         unsigned int haveToCopy=packet.length-*samplepos;
3085         //logger->debug(TAG, "DMP mark 8");
3086
3087         while (haveToCopy> (cur_input_buf_omx->nAllocLen-cur_input_buf_omx->nFilledLen)) {
3088                 //logger->debug(TAG, "Big buffer {} {} {}",packet.length,cur_input_buf_omx->nAllocLen,cur_input_buf_omx->nFilledLen);
3089                 unsigned int cancopy=cur_input_buf_omx->nAllocLen-cur_input_buf_omx->nFilledLen;
3090                 memcpy(cur_input_buf_omx->pBuffer+cur_input_buf_omx->nFilledLen,buffer+packet.pos_buffer+*samplepos,cancopy);
3091                 haveToCopy-=cancopy;
3092                 cur_input_buf_omx->nFilledLen+=cancopy;
3093                 *samplepos+=cancopy;
3094                 // push old buffer out
3095                 //logger->debug(TAG, "DMP mark 9");
3096
3097                 PutBufferToPres(cur_input_buf_omx);
3098                 cur_input_buf_omx=NULL;
3099                 // get5 new buffer
3100                 input_bufs_omx_mutex.lock();
3101                 if (input_bufs_omx_free.size()==0) {
3102                         input_bufs_omx_mutex.unlock();
3103                 //      logger->debug(TAG, "Deliver MediaPacket no free sample2");
3104                         return *samplepos; // we do not have a free media sample
3105                 }
3106                 cur_input_buf_omx=input_bufs_omx_free.front();
3107                 cur_input_buf_omx->nFilledLen=0;
3108                 cur_input_buf_omx->nOffset=0;
3109                 cur_input_buf_omx->nFlags=OMX_BUFFERFLAG_TIME_UNKNOWN;
3110                 cur_input_buf_omx->nTimeStamp=intToOMXTicks(0);
3111                 input_bufs_omx_free.pop_front();
3112                 input_bufs_omx_mutex.unlock();
3113                 //logger->debug(TAG, "DMP mark 10");
3114
3115         }
3116         memcpy(cur_input_buf_omx->pBuffer+cur_input_buf_omx->nFilledLen,
3117                         buffer+packet.pos_buffer+*samplepos,haveToCopy);
3118         cur_input_buf_omx->nFilledLen+=haveToCopy;
3119
3120 //      logger->debug(TAG, "DMP mark 11");
3121
3122         *samplepos+=haveToCopy;
3123
3124         return *samplepos;
3125
3126 }
3127
3128
3129
3130
3131 bool VideoOMX::displayIFrame(const u1* buffer, u4 length) {
3132         if (!omx_running) return false;
3133         if (!iframemode)
3134                 EnterIframePlayback();
3135
3136         //int haveToCopy = length;
3137
3138         if (!cur_input_buf_omx) {
3139                 input_bufs_omx_mutex.lock();
3140                 if (input_bufs_omx_free.size() == 0) {
3141                         input_bufs_omx_mutex.unlock();
3142                 //      LogNT::getInstance()->debug(TAG,
3143                         //              "Deliver MediaPacket no free sample");
3144                         return false; // we do not have a free media sample
3145
3146                 }
3147                 cur_input_buf_omx = input_bufs_omx_free.front();
3148                 cur_input_buf_omx->nFilledLen = 0;
3149                 cur_input_buf_omx->nOffset = 0;
3150                 cur_input_buf_omx->nTimeStamp = intToOMXTicks(0);
3151                 input_bufs_omx_free.pop_front();
3152                 input_bufs_omx_mutex.unlock();
3153         }
3154
3155         u4 read_pos = 0;
3156         unsigned int pattern, packet_length;
3157         unsigned int headerstrip = 0;
3158         bool first = true;
3159         if (length < 4){
3160                 return false;
3161         }
3162         //Now we strip the pes header
3163         pattern = (buffer[0] << 16) | (buffer[1] << 8) | (buffer[2]);
3164         while ((read_pos + 7) <= length) {
3165                 pattern = ((pattern << 8) & 0xFFFFFFFF) | buffer[read_pos + 3];
3166                 if (pattern < 0x000001E0 || pattern > 0x000001EF) {
3167                         read_pos++;
3168                         continue;
3169                 } else {
3170                         headerstrip = buffer[read_pos + 8] + 9/*is this right*/;
3171                         packet_length = ((buffer[read_pos + 4] << 8)
3172                                         | (buffer[read_pos + 5])) + 6;
3173                         if (read_pos + packet_length > length)
3174                                 read_pos = length;
3175                         else {
3176                                 if ((headerstrip < packet_length)
3177                                                 && (cur_input_buf_omx->nFilledLen + packet_length
3178                                                                 - headerstrip) > cur_input_buf_omx->nAllocLen) {
3179                                         if (first) {
3180                                                 cur_input_buf_omx->nFlags = OMX_BUFFERFLAG_STARTTIME;
3181
3182                                         } else {
3183                                                 cur_input_buf_omx->nFlags
3184                                                                 |= OMX_BUFFERFLAG_TIME_UNKNOWN;
3185
3186                                         }
3187                                         cur_input_buf_omx->nTimeStamp = intToOMXTicks(0);
3188                                         PutBufferToPres(cur_input_buf_omx);
3189                                         cur_input_buf_omx = NULL;
3190
3191                                         if (!cur_input_buf_omx) {
3192                                                 int count = 0;
3193                                                 while (count < 100 && omx_running && iframemode) {
3194                                                         count++;
3195
3196                                                         input_bufs_omx_mutex.lock();
3197                                                         if (input_bufs_omx_free.size() == 0) {
3198                                                                 input_bufs_omx_mutex.unlock();
3199                                         //                      LogNT::getInstance()->debug(TAG,
3200                                                 //                              "Ifrane no free sample");
3201                                                                 MILLISLEEP(5);
3202                                                                 if (!omx_running) return false;
3203                                                                 continue;
3204                                                         }
3205                                                         cur_input_buf_omx = input_bufs_omx_free.front();
3206                                                         cur_input_buf_omx->nFilledLen = 0;
3207                                                         cur_input_buf_omx->nOffset = 0;
3208                                                         cur_input_buf_omx->nTimeStamp = intToOMXTicks(0);
3209                                                         cur_input_buf_omx->nFlags|= OMX_BUFFERFLAG_TIME_UNKNOWN;
3210                                                         input_bufs_omx_free.pop_front();
3211                                                         input_bufs_omx_mutex.unlock();
3212                                                         break;
3213                                                 }
3214                                                 if (!cur_input_buf_omx)
3215                                                         return false;
3216                                         }
3217
3218                                 }
3219                                 if (packet_length > headerstrip) {
3220                                         memcpy(
3221                                                         cur_input_buf_omx->pBuffer
3222                                                                         + cur_input_buf_omx->nFilledLen,
3223                                                         buffer + read_pos + headerstrip,
3224                                                         packet_length - headerstrip);
3225                                         cur_input_buf_omx->nFilledLen += packet_length
3226                                                         - headerstrip;
3227                                 }
3228                                 read_pos += packet_length;
3229
3230                                 pattern = (buffer[read_pos] << 16)
3231                                                 | (buffer[read_pos + 1] << 8) | (buffer[read_pos + 2]);
3232                         }
3233                 }
3234         }
3235
3236         if (first) {
3237                 cur_input_buf_omx->nFlags = OMX_BUFFERFLAG_STARTTIME;
3238
3239         } else {
3240                 cur_input_buf_omx->nFlags |= OMX_BUFFERFLAG_TIME_UNKNOWN;
3241
3242         }
3243         cur_input_buf_omx->nTimeStamp = intToOMXTicks(0);
3244
3245         PutBufferToPres(cur_input_buf_omx);
3246         cur_input_buf_omx = NULL;
3247
3248
3249         MILLISLEEP(40); //Block a bit
3250         return true;
3251 }
3252
3253 int VideoOMX::EnterIframePlayback()
3254 {
3255         LogNT::getInstance()->debug(TAG,
3256                                                 "EnterIframePlayback");
3257         if (cur_input_buf_omx) {
3258                 PutBufferToPres(cur_input_buf_omx);
3259                 cur_input_buf_omx = NULL;
3260         }
3261         LogNT::getInstance()->debug(TAG,
3262                                                         "EnterIframePlayback 2");
3263         dynamic_cast<AudioOMX*>(Audio::getInstance())->DeAllocateCodecsOMX();
3264         DeAllocateCodecsOMX();
3265         AllocateCodecsOMX();
3266         LogNT::getInstance()->debug(TAG,
3267                                                         "leave IframePlayback");
3268
3269         iframemode=true;
3270
3271         return 1;
3272 }
3273