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