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