]> git.vomp.tv Git - vompclient-marten.git/blob - videoomx.cc
Updates on mode switching code, still turned off
[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
686         if (hdmi) { // only switch if hdmi and HD or interlaced SD material
687
688
689                 //OMX_Deinit();
690                 int set_interlaced=0;
691                 if (demux->getHorizontalSize()>720  && demux->getInterlaced()) {
692                         set_interlaced=1;
693                 }
694                 Log::getInstance()->log("Video", Log::NOTICE, "switch interlacing %d %d %d",demux->getInterlaced(),outputinterlaced,set_interlaced);
695                 if (outputinterlaced!=set_interlaced) {
696                         selectVideoMode(set_interlaced);
697                         Osd::getInstance()->shutdown();
698                         Osd::getInstance()->restore();
699                         Osd::getInstance()->init((void*)"");
700                         BoxStack::getInstance()->redrawAllBoxes();
701                         initted=1;
702                 }
703                 //OMX_Init();
704
705
706         }
707
708
709 }
710
711
712 int VideoOMX::play() {
713         if (!initted)
714                 return 0;
715         iframemode = false;
716         Log::getInstance()->log("Video", Log::DEBUG, "enter play");
717
718         interlaceSwitch4Demux();
719
720         if (AllocateCodecsOMX()) {
721                 return 1;
722                 // Otherwise fall back to libav
723         } else {
724                 if (h264) {
725                         omx_h264 = false;
726                         Log::getInstance()->log("Video", Log::NOTICE,
727                                         "Allocate Codecs OMX failed assume h264 unsupported");
728                 } else {
729                         omx_mpeg2 = false;
730                         Log::getInstance()->log("Video", Log::NOTICE,
731                                         "Allocate Codecs OMX failed assume mpeg2 unsupported");
732                 }
733         }
734
735         return 0;
736
737 }
738
739
740 int VideoOMX::initClock()
741 {
742         OMX_ERRORTYPE error;
743         clock_mutex.Lock();
744         if (clock_references==0)
745         {
746
747                 static OMX_CALLBACKTYPE callbacks= {&EventHandler_OMX,&EmptyBufferDone_OMX,&FillBufferDone_OMX};
748                 omx_events.clear();
749
750                 error=OMX_GetHandle(&omx_clock,VPE_OMX_CLOCK,NULL,&callbacks);
751
752                 if (error!=OMX_ErrorNone) {
753                         Log::getInstance()->log("Video", Log::DEBUG, "Init OMX clock failed %x", error);
754                         clock_mutex.Unlock();
755                         DeAllocateCodecsOMX();
756                         return 0;
757                 }
758
759                 /* TODO Clock config to separate method */
760                 OMX_PORT_PARAM_TYPE p_param;
761                 memset(&p_param,0,sizeof(p_param));
762                 p_param.nSize=sizeof(p_param);
763                 p_param.nVersion.nVersion=OMX_VERSION;
764                 error=OMX_GetParameter(omx_clock,OMX_IndexParamOtherInit,&p_param);
765                 if (error!=OMX_ErrorNone) {
766                         Log::getInstance()->log("Video", Log::DEBUG, "Init clock OMX_GetParameter failed %x", error);
767                         clock_mutex.Unlock();
768                         DeAllocateCodecsOMX();
769                         return 0;
770                 }
771                 omx_clock_output_port=p_param.nStartPortNumber;
772
773                 for (unsigned int i=0;i<p_param.nPorts;i++) {
774                         if (!DisablePort(omx_clock,p_param.nStartPortNumber+i,true) ) {
775                                 Log::getInstance()->log("Video", Log::DEBUG, "Disable Ports OMX clock failed %d",i);
776                                 clock_mutex.Unlock();
777                                 DeAllocateCodecsOMX();
778                                 return 0;
779                         }
780                 }
781
782
783
784
785         }
786         Log::getInstance()->log("Video", Log::DEBUG, "init omx clock %x %x",this,omx_clock);
787         clock_references++;
788         clock_mutex.Unlock();
789         return 1;
790 }
791
792 int VideoOMX::getClockAudioandInit(OMX_HANDLETYPE *p_omx_clock,OMX_U32 *p_omx_clock_output_port)
793 {
794         OMX_ERRORTYPE error;
795         *p_omx_clock=NULL;
796         *p_omx_clock_output_port=0;
797
798         if (!initClock()) {
799                 return 0;
800         }
801         clock_mutex.Lock();
802
803         OMX_TIME_CONFIG_ACTIVEREFCLOCKTYPE refclock;
804         memset(&refclock,0,sizeof(refclock));
805         refclock.nSize=sizeof(refclock);
806         refclock.nVersion.nVersion=OMX_VERSION;
807
808         refclock.eClock=OMX_TIME_RefClockAudio;
809
810         //refclock.eClock=OMX_TIME_RefClockVideo;
811         error=OMX_SetConfig(omx_clock,OMX_IndexConfigTimeActiveRefClock,&refclock);
812         if (error!=OMX_ErrorNone){
813                 Log::getInstance()->log("Video", Log::DEBUG, "Clock OMX_IndexConfigTimeActiveRefClock failed %x", error);
814                 clock_mutex.Unlock();
815                 DeAllocateCodecsOMX();
816                 return 0;
817         }
818
819         OMX_PORT_PARAM_TYPE p_param;
820         memset(&p_param,0,sizeof(p_param));
821         p_param.nSize=sizeof(p_param);
822         p_param.nVersion.nVersion=OMX_VERSION;
823         error=OMX_GetParameter(omx_clock,OMX_IndexParamOtherInit,&p_param);
824         if (error!=OMX_ErrorNone){
825                 Log::getInstance()->log("Video", Log::DEBUG, "Init clock OMX_GetParameter failed %x", error);
826                 clock_mutex.Unlock();
827                 DeAllocateCodecsOMX();
828                 return 0;
829         }
830
831         OMX_TIME_CONFIG_CLOCKSTATETYPE clock_conf;
832         memset(&clock_conf,0,sizeof(clock_conf));
833         clock_conf.nSize=sizeof(clock_conf);
834         clock_conf.nVersion.nVersion=OMX_VERSION;
835         clock_conf.eState=OMX_TIME_ClockStateWaitingForStartTime;
836         clock_conf.nStartTime=intToOMXTicks(0);
837         clock_conf.nOffset=intToOMXTicks(0);
838         if (clock_references==1) clock_conf.nWaitMask=OMX_CLOCKPORT1;
839         else clock_conf.nWaitMask=OMX_CLOCKPORT0|OMX_CLOCKPORT1;
840         error=OMX_SetConfig(omx_clock,OMX_IndexConfigTimeClockState,&clock_conf);
841         if (error!=OMX_ErrorNone) {
842                 Log::getInstance()->log("Video", Log::DEBUG, "AuI Clock IndexConfigTimeClockState failed %x", error);
843         }
844
845
846         *p_omx_clock_output_port=p_param.nStartPortNumber+1;
847         *p_omx_clock=omx_clock;
848         clock_mutex.Unlock();
849         return 1;
850 }
851
852 int VideoOMX::getClockVideoandInit()
853 {
854         OMX_ERRORTYPE error;
855
856         if (!initClock()) {
857                 return 0;
858         }
859         clock_mutex.Lock();
860
861         if (clock_references==1) { // only if no audio is attached to this clock!
862                 OMX_TIME_CONFIG_ACTIVEREFCLOCKTYPE refclock;
863                 memset(&refclock,0,sizeof(refclock));
864                 refclock.nSize=sizeof(refclock);
865                 refclock.nVersion.nVersion=OMX_VERSION;
866
867                 //refclock.eClock=OMX_TIME_RefClockAudio;
868
869                 refclock.eClock=OMX_TIME_RefClockVideo;
870                 error=OMX_SetConfig(omx_clock,OMX_IndexConfigTimeActiveRefClock,&refclock);
871                 if (error!=OMX_ErrorNone) {
872                         Log::getInstance()->log("Video", Log::DEBUG, "Clock OMX_IndexConfigTimeActiveRefClock failed %x", error);
873                         clock_mutex.Unlock();
874                         DeAllocateCodecsOMX();
875                         return 0;
876                 }
877         }
878
879         OMX_PORT_PARAM_TYPE p_param;
880         memset(&p_param,0,sizeof(p_param));
881         p_param.nSize=sizeof(p_param);
882         p_param.nVersion.nVersion=OMX_VERSION;
883         error=OMX_GetParameter(omx_clock,OMX_IndexParamOtherInit,&p_param);
884         if (error!=OMX_ErrorNone){
885                 Log::getInstance()->log("Video", Log::DEBUG, "Init clock OMX_GetParameter failed %x", error);
886                 clock_mutex.Unlock();
887                 DeAllocateCodecsOMX();
888                 return 0;
889         }
890
891
892         OMX_TIME_CONFIG_CLOCKSTATETYPE clock_conf;
893         memset(&clock_conf,0,sizeof(clock_conf));
894         clock_conf.nSize=sizeof(clock_conf);
895         clock_conf.nVersion.nVersion=OMX_VERSION;
896         clock_conf.eState=OMX_TIME_ClockStateStopped;
897         clock_conf.nStartTime=intToOMXTicks(0);
898         clock_conf.nOffset=intToOMXTicks(0);
899         error=OMX_SetConfig(omx_clock,OMX_IndexConfigTimeClockState,&clock_conf);
900         if (error!=OMX_ErrorNone) {
901                 Log::getInstance()->log("Video", Log::DEBUG, "VuI Clock IndexConfigTimeClockState failed %x", error);
902         }
903
904
905         memset(&clock_conf,0,sizeof(clock_conf));
906         clock_conf.nSize=sizeof(clock_conf);
907         clock_conf.nVersion.nVersion=OMX_VERSION;
908         clock_conf.eState=OMX_TIME_ClockStateWaitingForStartTime;
909         clock_conf.nStartTime=intToOMXTicks(0);
910         clock_conf.nOffset=intToOMXTicks(0);
911         if (clock_references==1) clock_conf.nWaitMask=OMX_CLOCKPORT0;
912         else clock_conf.nWaitMask=OMX_CLOCKPORT0|OMX_CLOCKPORT1;
913         error=OMX_SetConfig(omx_clock,OMX_IndexConfigTimeClockState,&clock_conf);
914         if (error!=OMX_ErrorNone) {
915                 Log::getInstance()->log("Video", Log::DEBUG, "VuI Clock IndexConfigTimeClockState failed %x", error);
916         }
917
918         omx_clock_output_port=p_param.nStartPortNumber;
919         clock_mutex.Unlock();
920
921         return 1;
922 }
923
924 void VideoOMX::clockUnpause()
925 {
926         OMX_ERRORTYPE error;
927         Log::getInstance()->log("Video", Log::NOTICE, "enter Clockunpause");
928         clock_mutex.Lock();
929         if (clock_references>0 && clockpaused) {
930                 OMX_TIME_CONFIG_SCALETYPE scale_type;
931                 memset(&scale_type,0,sizeof(scale_type));
932                 scale_type.nSize=sizeof(scale_type);
933                 scale_type.nVersion.nVersion=OMX_VERSION;
934                 scale_type.xScale=1<<16;
935                 error=OMX_SetConfig(omx_clock,OMX_IndexConfigTimeScale,&scale_type);
936                 if (error!=OMX_ErrorNone) {
937                         Log::getInstance()->log("Video", Log::DEBUG, "ClockUnpause OMX_IndexConfigTimeScale failed %x", error);
938                 }
939                 Log::getInstance()->log("Video", Log::NOTICE, "set playback speed ClockUnpause");
940                 clockpaused=false;
941         }
942         clock_mutex.Unlock();
943 }
944
945
946 void VideoOMX::clockPause()
947 {
948         OMX_ERRORTYPE error;
949         Log::getInstance()->log("Video", Log::NOTICE, "enter ClockPause");
950         clock_mutex.Lock();
951         if (clock_references>0 && !clockpaused) {
952                 OMX_TIME_CONFIG_SCALETYPE scale_type;
953                 memset(&scale_type,0,sizeof(scale_type));
954                 scale_type.nSize=sizeof(scale_type);
955                 scale_type.nVersion.nVersion=OMX_VERSION;
956                 scale_type.xScale=0;
957                 error=OMX_SetConfig(omx_clock,OMX_IndexConfigTimeScale,&scale_type);
958                 if (error!=OMX_ErrorNone) {
959                         Log::getInstance()->log("Video", Log::DEBUG, "ClockPause OMX_IndexConfigTimeScale failed %x", error);
960                 }
961                 Log::getInstance()->log("Video", Log::NOTICE, "set playback speed ClockPause");
962                 clockpaused=true;
963         }
964         clock_mutex.Unlock();
965 }
966
967
968
969 int VideoOMX::AllocateCodecsOMX()
970 {
971         OMX_ERRORTYPE error;
972         static OMX_CALLBACKTYPE callbacks= {&EventHandler_OMX,&EmptyBufferDone_OMX,&FillBufferDone_OMX};
973
974         Demuxer* demux=Demuxer::getInstance();
975
976         dodeint=false;
977         deint_first_frame=false;
978
979         Log::getInstance()->log("Video", Log::NOTICE, "Allocate Codecs OMX");
980         //Clock, move later to audio including events
981
982         Log::getInstance()->log("Video", Log::NOTICE, "VideoType %d x %d i: %d", demux->getHorizontalSize(),demux->getVerticalSize(),demux->getInterlaced());
983         if (deinterlace!=0 && (demux->getHorizontalSize()<=720 ) && demux->getInterlaced()) { //only deinterlace SD material
984                 dodeint=true;
985                 deint_first_frame=true;
986
987                 Log::getInstance()->log("Video", Log::NOTICE, "Deinterlacing activated %d",deinterlace);
988
989         }
990
991
992         if (!getClockVideoandInit()){
993                 return 0;// get the clock and init it if necessary
994         }
995
996
997         if (!idleClock()) {
998                 Log::getInstance()->log("Video", Log::DEBUG, "idleClock failed");
999                 return 0;
1000         }
1001         /* TODO end */
1002
1003         clock_mutex.Lock();
1004         if (h264) {
1005                 error=OMX_GetHandle(&omx_vid_dec,VPE_OMX_H264_DECODER,NULL,&callbacks);
1006         } else {
1007                 error=OMX_GetHandle(&omx_vid_dec,VPE_OMX_MPEG2_DECODER,NULL,&callbacks);
1008         }
1009
1010         if (error!=OMX_ErrorNone){
1011                 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX video decoder failed %x", error);
1012                 clock_mutex.Unlock();
1013                 DeAllocateCodecsOMX();
1014                 return 0;
1015         }
1016
1017
1018         Log::getInstance()->log("Video", Log::DEBUG, "Nmark3 ");
1019         OMX_PORT_PARAM_TYPE p_param;
1020         memset(&p_param,0,sizeof(p_param));
1021         p_param.nSize=sizeof(p_param);
1022         p_param.nVersion.nVersion=OMX_VERSION;
1023         error=OMX_GetParameter(omx_vid_dec,OMX_IndexParamVideoInit,&p_param);
1024         if (error!=OMX_ErrorNone){
1025                 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX h264 decoder OMX_GetParameter failed %x", error);
1026                 clock_mutex.Unlock();
1027                 DeAllocateCodecsOMX();
1028             return 0;
1029         }
1030         omx_codec_input_port=p_param.nStartPortNumber;
1031         omx_codec_output_port=p_param.nStartPortNumber+1;
1032
1033         if (!DisablePort(omx_vid_dec,omx_codec_input_port) || !DisablePort(omx_vid_dec,omx_codec_output_port)) {
1034                 Log::getInstance()->log("Video", Log::DEBUG, "Disable Ports OMX video decoder failed");
1035                 clock_mutex.Unlock();
1036                 DeAllocateCodecsOMX();
1037                 return 0;
1038         }
1039
1040         Log::getInstance()->log("Video", Log::DEBUG, "Nmark4 ");
1041
1042         OMX_PARAM_BRCMVIDEODECODEERRORCONCEALMENTTYPE conceal;
1043         memset(&conceal,0,sizeof(conceal));
1044         conceal.nSize=sizeof(conceal);
1045         conceal.nVersion.nVersion=OMX_VERSION;
1046         conceal.bStartWithValidFrame=OMX_FALSE;
1047
1048         error=OMX_SetParameter(omx_vid_dec,OMX_IndexParamBrcmVideoDecodeErrorConcealment,&conceal);
1049         if (error!=OMX_ErrorNone){
1050                 Log::getInstance()->log("Video", Log::DEBUG, "OMX_IndexParamBrcmVideoDecodeErrorConcealment failed %x", error);
1051                 clock_mutex.Unlock();
1052                 DeAllocateCodecsOMX();
1053                 return 0;
1054         }
1055
1056         if (dodeint) {
1057                 error = OMX_GetHandle(&omx_vid_deint, VPE_OMX_VIDEO_DEINTERLACE, NULL,
1058                                 &callbacks);
1059                 if (error != OMX_ErrorNone) {
1060                         Log::getInstance()->log("Video", Log::DEBUG,
1061                                         "Init OMX video deinterlacer failed %x", error);
1062                         clock_mutex.Unlock();
1063                         DeAllocateCodecsOMX();
1064                         return 0;
1065                 }
1066
1067                 error = OMX_GetParameter(omx_vid_deint, OMX_IndexParamImageInit,
1068                                 &p_param);
1069                 if (error != OMX_ErrorNone) {
1070                         Log::getInstance()->log("Video", Log::DEBUG,
1071                                         "Init OMX video deinterlacer OMX_GetParameter failed %x",
1072                                         error);
1073                         clock_mutex.Unlock();
1074                         DeAllocateCodecsOMX();
1075                         return 0;
1076                 }
1077                 omx_deint_input_port = p_param.nStartPortNumber;
1078                 omx_deint_output_port = p_param.nStartPortNumber + 1;
1079
1080                 if (!DisablePort(omx_vid_deint, omx_deint_input_port, true)
1081                                 || !DisablePort(omx_vid_deint, omx_deint_output_port, true)) {
1082                         Log::getInstance()->log("Video", Log::DEBUG,
1083                                         "Disable Ports OMX video deint failed");
1084                         clock_mutex.Unlock();
1085                         DeAllocateCodecsOMX();
1086                         return 0;
1087                 }
1088
1089         }
1090
1091
1092         error=OMX_GetHandle(&omx_vid_sched,VPE_OMX_VIDEO_SCHED,NULL,&callbacks);
1093         if (error!=OMX_ErrorNone){
1094                 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX video scheduler failed %x", error);
1095                 clock_mutex.Unlock();
1096                 DeAllocateCodecsOMX();
1097                 return 0;
1098         }
1099
1100
1101
1102         error=OMX_GetParameter(omx_vid_sched,OMX_IndexParamVideoInit,&p_param);
1103         if (error!=OMX_ErrorNone){
1104                 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX video scheduler OMX_GetParameter failed %x", error);
1105                 clock_mutex.Unlock();
1106                 DeAllocateCodecsOMX();
1107                 return 0;
1108         }
1109         omx_shed_input_port=p_param.nStartPortNumber;
1110         omx_shed_output_port=p_param.nStartPortNumber+1;
1111
1112
1113         error=OMX_GetParameter(omx_vid_sched,OMX_IndexParamOtherInit,&p_param);
1114         if (error!=OMX_ErrorNone){
1115                 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX video scheduler OMX_GetParameter failed %x", error);
1116                 clock_mutex.Unlock();
1117                 DeAllocateCodecsOMX();
1118                 return 0;
1119         }
1120         omx_shed_clock_port=p_param.nStartPortNumber;
1121         Log::getInstance()->log("Video", Log::DEBUG, "scheduler ports %d %d %d ",omx_shed_input_port,omx_shed_output_port,omx_shed_clock_port);
1122
1123
1124         if (!DisablePort(omx_vid_sched,omx_shed_input_port,true) || !DisablePort(omx_vid_sched,omx_shed_output_port,true)
1125                         || !DisablePort(omx_vid_sched,omx_shed_clock_port,true)) {
1126                 Log::getInstance()->log("Video", Log::DEBUG, "Disable Ports OMX video shed failed");
1127                 clock_mutex.Unlock();
1128                 DeAllocateCodecsOMX();
1129                 return 0;
1130         }
1131
1132
1133         error=OMX_GetHandle(&omx_vid_rend,VPE_OMX_VIDEO_REND,NULL,&callbacks);
1134         if (error!=OMX_ErrorNone){
1135                 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX video rend failed %x", error);
1136                 clock_mutex.Unlock();
1137                 DeAllocateCodecsOMX();
1138                 return 0;
1139         }
1140
1141         error=OMX_GetParameter(omx_vid_rend,OMX_IndexParamVideoInit,&p_param);
1142         if (error!=OMX_ErrorNone){
1143                 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX video rend OMX_GetParameter failed %x", error);
1144                 clock_mutex.Unlock();
1145                 DeAllocateCodecsOMX();
1146                 return 0;
1147         }
1148         omx_rend_input_port=p_param.nStartPortNumber;
1149         //omx_rend_output_port=p_param.nStartPortNumber+1;
1150
1151
1152         if (!DisablePort(omx_vid_rend,omx_rend_input_port,true) /*|| !DisablePort(omx_vid_rend,omx_rend_output_port)*/
1153                                 ) {
1154                 Log::getInstance()->log("Video", Log::DEBUG, "Disable Ports OMX video rend failed");
1155                 clock_mutex.Unlock();
1156                 DeAllocateCodecsOMX();
1157                 return 0;
1158         }
1159
1160         //Setuo chain
1161
1162
1163
1164         error=OMX_SetupTunnel(omx_clock,omx_clock_output_port,omx_vid_sched,omx_shed_clock_port);
1165         if (error!=OMX_ErrorNone){
1166                 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);
1167                 clock_mutex.Unlock();
1168                 DeAllocateCodecsOMX();
1169                 return 0;
1170         }
1171
1172         if (!EnablePort(omx_clock,omx_clock_output_port,false) || !EnablePort(omx_vid_sched,omx_shed_clock_port,false)
1173                                         ) {
1174                 Log::getInstance()->log("Video", Log::DEBUG, "Enable Ports OMX clock shed failed");
1175                 clock_mutex.Unlock();
1176                 DeAllocateCodecsOMX();
1177                 return 0;
1178         }
1179
1180
1181         Log::getInstance()->log("Video", Log::DEBUG, "mark2 ");
1182         if (!ChangeComponentState(omx_vid_sched,OMX_StateIdle)) {
1183                 Log::getInstance()->log("Video", Log::DEBUG, "vid_sched idle ChangeComponentState");
1184                 clock_mutex.Unlock();
1185                 DeAllocateCodecsOMX();
1186                 return 0;
1187         }
1188
1189
1190
1191         Log::getInstance()->log("Video", Log::DEBUG, "mark1 ");
1192         if ( !CommandFinished(omx_vid_sched,OMX_CommandPortEnable,omx_shed_clock_port)) {
1193                 clock_mutex.Unlock();
1194                 DeAllocateCodecsOMX();
1195                 return 0;
1196         }
1197
1198
1199
1200
1201         Log::getInstance()->log("Video", Log::DEBUG, "mark1 special ");
1202         if ( !CommandFinished(omx_clock,OMX_CommandPortEnable,omx_clock_output_port)) {
1203                 clock_mutex.Unlock();
1204                 DeAllocateCodecsOMX();
1205                 return 0;
1206         }
1207
1208
1209
1210         Log::getInstance()->log("Video", Log::DEBUG, "mark1b ");
1211
1212
1213 /*      error=OMX_SendCommand(omx_vid_dec,OMX_CommandStateSet,OMX_StateIdle,0);
1214         if (error!=OMX_ErrorNone){
1215                 Log::getInstance()->log("Video", Log::DEBUG, "vid_dec Send Command to OMX State Idle %x", error);
1216                 return 0;
1217         }*/
1218
1219         OMX_VIDEO_PARAM_PORTFORMATTYPE ft_type;
1220         memset(&ft_type,0,sizeof(ft_type));
1221         ft_type.nSize=sizeof(ft_type);
1222         ft_type.nVersion.nVersion=OMX_VERSION;
1223
1224         ft_type.nPortIndex=omx_codec_input_port;
1225         if (h264) {
1226                 ft_type.eCompressionFormat=OMX_VIDEO_CodingAVC;
1227         } else {
1228                 ft_type.eCompressionFormat=OMX_VIDEO_CodingMPEG2;
1229         }
1230
1231
1232
1233     ft_type.xFramerate=0*(1<<16);//25*(1<<16);//demux->getFrameRate()*(1<<16);
1234     Log::getInstance()->log("Video", Log::DEBUG, "Framerate: %d",demux->getFrameRate());
1235         error=OMX_SetParameter(omx_vid_dec,OMX_IndexParamVideoPortFormat,&ft_type);
1236         if (error!=OMX_ErrorNone){
1237                 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX_IndexParamVideoPortFormat failed %x", error);
1238                 clock_mutex.Unlock();
1239                 DeAllocateCodecsOMX();
1240                 return 0;
1241         }
1242
1243
1244         if (!ChangeComponentState(omx_vid_dec,OMX_StateIdle)) {
1245                 Log::getInstance()->log("Video", Log::DEBUG, "vid_dec ChangeComponentState");
1246                 clock_mutex.Unlock();
1247                 DeAllocateCodecsOMX();
1248                 return 0;
1249         }
1250
1251
1252         if (!PrepareInputBufsOMX()) {
1253                 clock_mutex.Unlock();
1254                 DeAllocateCodecsOMX();
1255                 return 0;
1256         }
1257
1258         if (!dodeint) {
1259                 error=OMX_SetupTunnel(omx_vid_dec,omx_codec_output_port,omx_vid_sched,omx_shed_input_port);
1260                 if (error!=OMX_ErrorNone){
1261                         Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel dec to sched failed %x", error);
1262                         clock_mutex.Unlock();
1263                         DeAllocateCodecsOMX();
1264                         return 0;
1265                 }
1266
1267
1268
1269                 if (!EnablePort(omx_vid_dec,omx_codec_output_port,false) || !EnablePort(omx_vid_sched,omx_shed_input_port,false)
1270                 ) {
1271                         Log::getInstance()->log("Video", Log::DEBUG, "Enable Ports OMX codec shed failed");
1272                         clock_mutex.Unlock();
1273                         DeAllocateCodecsOMX();
1274                         return 0;
1275                 }
1276
1277                 if ( !CommandFinished(omx_vid_dec,OMX_CommandPortEnable,omx_codec_output_port) || !CommandFinished(omx_vid_sched,OMX_CommandPortEnable,omx_shed_input_port)) {
1278                         clock_mutex.Unlock();
1279                         DeAllocateCodecsOMX();
1280                         return 0;
1281                 }
1282
1283         } else {
1284
1285                 error=OMX_SetupTunnel(omx_vid_dec,omx_codec_output_port,omx_vid_deint,omx_deint_input_port);
1286                 if (error!=OMX_ErrorNone){
1287                         Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel dec to deint failed %x", error);
1288                         clock_mutex.Unlock();
1289                         DeAllocateCodecsOMX();
1290                         return 0;
1291                 }
1292
1293
1294
1295                 if (!EnablePort(omx_vid_dec,omx_codec_output_port,false) || !EnablePort(omx_vid_deint,omx_deint_input_port,false)
1296                 ) {
1297                         Log::getInstance()->log("Video", Log::DEBUG, "Enable Ports OMX codec deint failed");
1298                         clock_mutex.Unlock();
1299                         DeAllocateCodecsOMX();
1300                         return 0;
1301                 }
1302
1303                 if ( !CommandFinished(omx_vid_dec,OMX_CommandPortEnable,omx_codec_output_port) || !CommandFinished(omx_vid_deint,OMX_CommandPortEnable,omx_deint_input_port)) {
1304                         clock_mutex.Unlock();
1305                         DeAllocateCodecsOMX();
1306                         return 0;
1307                 }
1308
1309                 if (!ChangeComponentState(omx_vid_deint,OMX_StateIdle)) {
1310                         Log::getInstance()->log("Video", Log::DEBUG, "vid_deint ChangeComponentState");
1311                         clock_mutex.Unlock();
1312                         DeAllocateCodecsOMX();
1313                         return 0;
1314                 }
1315
1316                 OMX_CONFIG_IMAGEFILTERPARAMSTYPE imagefilter;
1317                 memset(&imagefilter,0,sizeof(imagefilter));
1318                 imagefilter.nSize=sizeof(imagefilter);
1319                 imagefilter.nVersion.nVersion=OMX_VERSION;
1320
1321                 imagefilter.nPortIndex=omx_deint_output_port;
1322                 imagefilter.nNumParams=1;
1323                 imagefilter.nParams[0]=3;//???
1324                 switch (deinterlace) {
1325                 case 1:
1326                         imagefilter.eImageFilter=OMX_ImageFilterDeInterlaceLineDouble; break;
1327                 case 2:
1328                         imagefilter.eImageFilter=OMX_ImageFilterDeInterlaceAdvanced; break;
1329                 case 3:
1330                         imagefilter.eImageFilter=OMX_ImageFilterFilm; break;
1331                 }
1332
1333
1334                 error=OMX_SetConfig(omx_vid_deint,OMX_IndexConfigCommonImageFilterParameters,&imagefilter);
1335                 if (error!=OMX_ErrorNone){
1336                         Log::getInstance()->log("Video", Log::DEBUG, "Init OMX_IndexConfigCommonImageFilterParameters failed %x", error);
1337                         clock_mutex.Unlock();
1338                         DeAllocateCodecsOMX();
1339                         return 0;
1340                 }
1341
1342
1343                 error=OMX_SetupTunnel(omx_vid_deint,omx_deint_output_port,omx_vid_sched,omx_shed_input_port);
1344                 if (error!=OMX_ErrorNone){
1345                         Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel deint to sched failed %x", error);
1346                         clock_mutex.Unlock();
1347                         DeAllocateCodecsOMX();
1348                         return 0;
1349                 }
1350
1351                 if (!EnablePort(omx_vid_deint,omx_deint_output_port,false) || !EnablePort(omx_vid_sched,omx_shed_input_port,false)
1352                 ) {
1353                         Log::getInstance()->log("Video", Log::DEBUG, "Enable Ports OMX deint shed failed");
1354                         clock_mutex.Unlock();
1355                         DeAllocateCodecsOMX();
1356                         return 0;
1357                 }
1358
1359                 if ( !CommandFinished(omx_vid_deint,OMX_CommandPortEnable,omx_deint_output_port) || !CommandFinished(omx_vid_sched,OMX_CommandPortEnable,omx_shed_input_port)) {
1360                         clock_mutex.Unlock();
1361                         DeAllocateCodecsOMX();
1362                         return 0;
1363                 }
1364
1365         }
1366
1367         if (!ChangeComponentState(omx_vid_dec,OMX_StateExecuting)) {
1368                 Log::getInstance()->log("Video", Log::DEBUG, "omx_vid_dec ChangeComponentState Execute");
1369                 clock_mutex.Unlock();
1370                 DeAllocateCodecsOMX();
1371                 return 0;
1372         }
1373
1374         error=OMX_SetupTunnel(omx_vid_sched,omx_shed_output_port,omx_vid_rend,omx_rend_input_port);
1375         if (error!=OMX_ErrorNone){
1376                 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel  sched to rend failed %x", error);
1377                 clock_mutex.Unlock();
1378                 DeAllocateCodecsOMX();
1379                 return 0;
1380         }
1381
1382         if (!EnablePort(omx_vid_sched,omx_shed_output_port,false) || !EnablePort(omx_vid_rend,omx_rend_input_port,false)
1383                                                         ) {
1384                 Log::getInstance()->log("Video", Log::DEBUG, "Enable Ports OMX  shed rend failed");
1385                 clock_mutex.Unlock();
1386                 DeAllocateCodecsOMX();
1387                 return 0;
1388         }
1389
1390         if (!CommandFinished(omx_vid_sched,OMX_CommandPortEnable,omx_shed_output_port)
1391                                         || !CommandFinished(omx_vid_rend,OMX_CommandPortEnable,omx_rend_input_port)) {
1392                 clock_mutex.Unlock();
1393                 DeAllocateCodecsOMX();
1394                 return 0;
1395         }
1396
1397         if (!ChangeComponentState(omx_vid_rend,OMX_StateIdle)) {
1398                 Log::getInstance()->log("Video", Log::DEBUG, "vid_rend ChangeComponentState");
1399                 clock_mutex.Unlock();
1400                 DeAllocateCodecsOMX();
1401                 return 0;
1402         }
1403
1404         if (dodeint) {
1405                 if (!ChangeComponentState(omx_vid_deint,OMX_StateExecuting)) {
1406                         Log::getInstance()->log("Video", Log::DEBUG, "vid_vid_deint ChangeComponentState");
1407                         clock_mutex.Unlock();
1408                         DeAllocateCodecsOMX();
1409                         return 0;
1410                 }
1411                 DisablePort(omx_vid_deint,omx_deint_output_port,false);
1412                 DisablePort(omx_vid_deint,omx_deint_input_port,false);
1413         }
1414
1415         if (!ChangeComponentState(omx_vid_sched,OMX_StateExecuting)) {
1416                 Log::getInstance()->log("Video", Log::DEBUG, "omx_vid_sched ChangeComponentState Execute");
1417                 clock_mutex.Unlock();
1418                 DeAllocateCodecsOMX();
1419                 return 0;
1420         }
1421
1422         if (!ChangeComponentState(omx_vid_rend,OMX_StateExecuting)) {
1423                 Log::getInstance()->log("Video", Log::DEBUG, "omx_vid_rend ChangeComponentState Execute");
1424                 clock_mutex.Unlock();
1425                 DeAllocateCodecsOMX();
1426                 return 0;
1427         }
1428
1429         //raspbi specifif
1430         /*OMX_CONFIG_DISPLAYREGIONTYPE dispconf;
1431         memset(&dispconf,0,sizeof(dispconf));
1432         dispconf.nSize=sizeof(dispconf);
1433         dispconf.nVersion.nVersion=OMX_VERSION;
1434
1435         dispconf.nPortIndex=omx_rend_input_port;
1436
1437         dispconf.set=OMX_DISPLAY_SET_LAYER ;
1438         dispconf.layer=1;
1439         error=OMX_SetConfig(omx_vid_rend,OMX_IndexConfigDisplayRegion,&dispconf);
1440         if (error!=OMX_ErrorNone){
1441                 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX_IndexConfigDisplayRegion failed %x", error);
1442                 clock_mutex.Unlock();
1443                 DeAllocateCodecsOMX();
1444                 return 0;
1445         }*/
1446
1447 /*      dispconf.set=OMX_DISPLAY_SET_FULLSCREEN ;
1448         dispconf.fullscreen=OMX_FALSE;
1449         error=OMX_SetParameter(omx_vid_rend,OMX_IndexConfigDisplayRegion,&dispconf);
1450         if (error!=OMX_ErrorNone){
1451                 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX_IndexConfigDisplayRegion failed %x", error);
1452                 clock_mutex.Unlock();
1453                 DeAllocateCodecsOMX();
1454                 return 0;
1455         }
1456
1457         dispconf.set=OMX_DISPLAY_SET_DEST_RECT;
1458         dispconf.dest_rect.x_offset=100;
1459         dispconf.dest_rect.y_offset=100;
1460         dispconf.dest_rect.width=640;
1461         dispconf.dest_rect.height=480;
1462         error=OMX_SetParameter(omx_vid_rend,OMX_IndexConfigDisplayRegion,&dispconf);
1463         if (error!=OMX_ErrorNone){
1464                 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX_IndexConfigDisplayRegion failed %x", error);
1465                 clock_mutex.Unlock();
1466                 DeAllocateCodecsOMX();
1467                 return 0;
1468         }*/
1469
1470
1471         //playbacktimeoffset=-GetCurrentSystemTime();
1472
1473         iframemode=false;
1474         omx_running=true;
1475         clock_mutex.Unlock();
1476         clockUnpause();
1477         updateMode();
1478
1479         setClockExecutingandRunning();
1480
1481
1482
1483
1484
1485         return 1;
1486 }
1487
1488 int VideoOMX::idleClock()
1489 {
1490         OMX_ERRORTYPE error;
1491         OMX_STATETYPE temp_state;
1492         clock_mutex.Lock();
1493         OMX_GetState(omx_clock,&temp_state);
1494
1495         if (temp_state!=OMX_StateIdle) {
1496                 if (!ChangeComponentState(omx_clock,OMX_StateIdle)) {
1497                         Log::getInstance()->log("Video", Log::DEBUG, "omx_clock ChangeComponentState Idle failed");
1498                         clock_mutex.Unlock();
1499                         return 0;
1500                 }
1501         }
1502         clock_mutex.Unlock();
1503         return 1;
1504 }
1505
1506 int VideoOMX::setClockExecutingandRunning()
1507 {
1508         OMX_ERRORTYPE error;
1509         OMX_STATETYPE temp_state;
1510         clock_mutex.Lock();
1511         OMX_GetState(omx_clock,&temp_state);
1512
1513         if (temp_state!=OMX_StateExecuting) {
1514                 if (!ChangeComponentState(omx_clock,OMX_StateExecuting)) {
1515                         Log::getInstance()->log("Video", Log::DEBUG, "omx_clock ChangeComponentState Execute failed");
1516                         clock_mutex.Unlock();
1517                         DeAllocateCodecsOMX();
1518                         return 0;
1519                 }
1520         }
1521
1522         OMX_TIME_CONFIG_CLOCKSTATETYPE clock_conf;
1523         memset(&clock_conf,0,sizeof(clock_conf));
1524         clock_conf.nSize=sizeof(clock_conf);
1525         clock_conf.nVersion.nVersion=OMX_VERSION;
1526         clock_conf.eState=OMX_TIME_ClockStateRunning;
1527         error=OMX_SetConfig(omx_clock,OMX_IndexConfigTimeClockState,&clock_conf);
1528         if (error!=OMX_ErrorNone) {
1529                 Log::getInstance()->log("Video", Log::DEBUG, "Clock IndexConfigTimeClockState failed %x", error);
1530                 clock_mutex.Unlock();
1531                 DeAllocateCodecsOMX();
1532                 return 0;
1533         }
1534         clock_mutex.Unlock();
1535         return 1;
1536
1537 }
1538
1539
1540 int VideoOMX::ChangeComponentState(OMX_HANDLETYPE handle,OMX_STATETYPE type) //needs to be called with locked mutex
1541 {
1542         OMX_ERRORTYPE error;
1543         error=OMX_SendCommand(handle,OMX_CommandStateSet,type,0);
1544         if (error!=OMX_ErrorNone){
1545                 Log::getInstance()->log("Video", Log::DEBUG, "handle %x Send Command to OMX State %x %x",handle,type, error);
1546                 return 0;
1547         }
1548
1549         if (!CommandFinished(handle,OMX_CommandStateSet,type)) {
1550                 return 0;
1551         }
1552
1553         return 1;
1554 }
1555
1556
1557 int VideoOMX::EnablePort(OMX_HANDLETYPE handle,OMX_U32 port,bool wait) //needs to be called with locked mutex
1558 {
1559         OMX_ERRORTYPE error;
1560         error=OMX_SendCommand(handle,OMX_CommandPortEnable,port,0);
1561         if (error!=OMX_ErrorNone){
1562                 Log::getInstance()->log("Video", Log::DEBUG, "handle %x Send Command to enable port %x %x",handle,port, error);
1563                 return 0;
1564         }
1565
1566         if (!wait) return 1;
1567         if (!CommandFinished(handle,OMX_CommandPortEnable,port)) {
1568                 return 0;
1569         }
1570
1571         return 1;
1572 }
1573
1574
1575 int VideoOMX::DisablePort(OMX_HANDLETYPE handle,OMX_U32 port,bool wait) //needs to be called with locked mutex
1576 {
1577         OMX_ERRORTYPE error;
1578         error=OMX_SendCommand(handle,OMX_CommandPortDisable,port,0);
1579         if (error!=OMX_ErrorNone){
1580                 Log::getInstance()->log("Video", Log::DEBUG, "handle %x Send Command to disable port %x %x",handle,port, error);
1581                 return 0;
1582         }
1583
1584         if (!wait) return 1;
1585         if (!CommandFinished(handle,OMX_CommandPortDisable,port)) {
1586                 return 0;
1587         }
1588
1589
1590         return 1;
1591 }
1592
1593 int VideoOMX::WaitForEvent(OMX_HANDLETYPE handle,OMX_U32 event) //needs to be called with locked mutex
1594 {
1595         int i=0;
1596         while (i<1000) {
1597                 omx_event_mutex.Lock();
1598                 list<VPE_OMX_EVENT>::iterator itty=omx_events.begin();
1599                 while (itty!=omx_events.end()) {
1600
1601                         VPE_OMX_EVENT current=*itty;
1602                         if (current.handle==handle) { //this is ours
1603                                 if (current.event_type==OMX_EventError) {
1604                                         omx_events.erase(itty);
1605                                         omx_event_mutex.Unlock();
1606                                         Log::getInstance()->log("Video", Log::DEBUG, "WaitForEvent Finished on Error");
1607                                         return 0;
1608
1609                                 } else if (current.event_type==event) {
1610                                         omx_events.erase(itty);
1611                                         omx_event_mutex.Unlock();
1612                                         Log::getInstance()->log("Video", Log::DEBUG, "WaitForEvent Finished Completed");
1613                                         return 1;
1614                                 }
1615                         }
1616                         itty++;
1617
1618                 }
1619                 omx_event_mutex.Unlock();
1620                 MILLISLEEP(2);
1621                 i++;
1622
1623         }
1624         Log::getInstance()->log("Video", Log::DEBUG, "WaitForEvent waited too long %x %x",handle,event);
1625         return 0;
1626
1627 }
1628
1629 int VideoOMX::clearEvents()
1630 {
1631         omx_event_mutex.Lock();
1632         omx_events.clear();
1633         omx_event_mutex.Unlock();
1634
1635         return 1;
1636 }
1637
1638
1639 int VideoOMX::CommandFinished(OMX_HANDLETYPE handle,OMX_U32 command,OMX_U32 data2) //needs to be called with locked mutex
1640 {
1641         int i=0;
1642         while (i<1000) {
1643                 omx_event_mutex.Lock();
1644                 list<VPE_OMX_EVENT>::iterator itty=omx_events.begin();
1645                 while (itty!=omx_events.end()) {
1646
1647                         VPE_OMX_EVENT current=*itty;
1648                         if (current.handle==handle) { //this is ours
1649                                 if (current.event_type==OMX_EventError) {
1650                                         omx_events.erase(itty);
1651                                         omx_event_mutex.Unlock();
1652                                         Log::getInstance()->log("Video", Log::DEBUG, "Command Finished on Error %x",current.data1);
1653                                         return 0;
1654
1655                                 } else if (current.event_type==OMX_EventCmdComplete && current.data1==command && current.data2==data2) {
1656                                         omx_events.erase(itty);
1657                                         omx_event_mutex.Unlock();
1658                                         //Log::getInstance()->log("Video", Log::DEBUG, "Command Finished Completed");
1659                                         return 1;
1660                                 }
1661                         }
1662                         itty++;
1663
1664                 }
1665                 omx_event_mutex.Unlock();
1666                 MILLISLEEP(2);
1667                 i++;
1668
1669         }
1670         Log::getInstance()->log("Video", Log::DEBUG, "CommandFinished waited too long %x %x %x",handle,command, data2);
1671         return 0;
1672
1673 }
1674
1675
1676
1677
1678
1679 int VideoOMX::PrepareInputBufsOMX() //needs to be called with locked mutex
1680 {
1681         OMX_ERRORTYPE error;
1682         OMX_PARAM_PORTDEFINITIONTYPE port_def_type;
1683         memset(&port_def_type,0,sizeof(port_def_type));
1684         port_def_type.nSize=sizeof(port_def_type);
1685         port_def_type.nVersion.nVersion=OMX_VERSION;
1686         port_def_type.nPortIndex=omx_codec_input_port;
1687
1688         error=OMX_GetParameter(omx_vid_dec,OMX_IndexParamPortDefinition, &port_def_type);
1689
1690         if (error!=OMX_ErrorNone){
1691                         Log::getInstance()->log("Video", Log::DEBUG, "Get OMX OMX_IndexParamPortDefinition failed %x", error);
1692         }
1693 /*      Log::getInstance()->log("Video", Log::DEBUG, "Port para %d %d %d %d %d %d %d", port_def_type.nBufferCountActual,
1694                         port_def_type.nBufferCountMin,port_def_type.nBufferSize,port_def_type.bEnabled,port_def_type.bPopulated,
1695                         port_def_type.bBuffersContiguous,port_def_type.nBufferAlignment);*/
1696
1697         port_def_type.nBufferCountActual=100;
1698         port_def_type.nBufferSize=max(port_def_type.nBufferSize,200000); // for transcoder important
1699
1700         error=OMX_SetParameter(omx_vid_dec,OMX_IndexParamPortDefinition, &port_def_type);
1701
1702         if (error!=OMX_ErrorNone){
1703                         Log::getInstance()->log("Video", Log::DEBUG, "Set OMX OMX_IndexParamPortDefinition failed %x", error);
1704         }
1705
1706
1707         error=OMX_SendCommand(omx_vid_dec,OMX_CommandPortEnable,omx_codec_input_port,0);
1708         if (error!=OMX_ErrorNone){
1709                 Log::getInstance()->log("Video", Log::DEBUG, "Prepare Input bufs Send Command to enable port %x", error);
1710                 return 0;
1711         }
1712
1713         input_bufs_omx_mutex.Lock();
1714         for (unsigned int i=0; i< port_def_type.nBufferCountActual;i++) {
1715
1716         //      unsigned char* new_buffer_data=(unsigned char*)malloc(port_def_type.nbufferSize);
1717                 OMX_BUFFERHEADERTYPE *buf_head=NULL;
1718         /*      error=OMX_Usebuffer(omx_vid_dec,&buf_head,omx_codec_input_port,NULL,port_def_type.nbufferSize,new_buffer_data);
1719                 if (error!=OMX_ErrorNone){
1720                         Log::getInstance()->log("Video", Log::DEBUG, "Use OMX_Usebuffer failed %x", error);
1721                         input_bufs_omx_mutex.Unlock();
1722                         return 0;
1723                 }*/
1724                 error=OMX_AllocateBuffer(omx_vid_dec,&buf_head,omx_codec_input_port,NULL,port_def_type.nBufferSize);
1725                 if (error!=OMX_ErrorNone){
1726                         Log::getInstance()->log("Video", Log::DEBUG, "Use OMX_AllocateBuffer failed %x", error);
1727                                 input_bufs_omx_mutex.Unlock();
1728                         return 0;
1729                 }
1730                 input_bufs_omx_all.push_back(buf_head);
1731                 input_bufs_omx_free.push_back(buf_head);
1732         }
1733         omx_first_frame=true;
1734
1735         firstsynched=false;
1736         cur_input_buf_omx=NULL;
1737         input_bufs_omx_mutex.Unlock();
1738
1739
1740         Log::getInstance()->log("Video", Log::DEBUG, "PrepareInputBufsOMX mark3");
1741         if (!CommandFinished(omx_vid_dec,OMX_CommandPortEnable,omx_codec_input_port)) {
1742                 return 0;
1743         }
1744         Log::getInstance()->log("Video", Log::DEBUG, "PrepareInputBufsOMX mark4");
1745
1746         return 1;
1747 }
1748
1749 int VideoOMX::DestroyInputBufsOMX() //need s to be called with locked mutex
1750 {
1751         OMX_ERRORTYPE error;
1752
1753         cur_input_buf_omx=NULL;
1754         input_bufs_omx_mutex.Lock();
1755         for (int i=0; i< input_bufs_omx_all.size();i++) {
1756         //      free(input_bufs_omx_all[i]->pBuffer);
1757         //      input_bufs_omx_all[i]->pBuffer=NULL;
1758                 error=OMX_FreeBuffer(omx_vid_dec,omx_codec_input_port,input_bufs_omx_all[i]);
1759                 if (error!=OMX_ErrorNone){
1760                         Log::getInstance()->log("Video", Log::DEBUG, "Use OMX_FreeBuffer failed %x", error);
1761                         input_bufs_omx_mutex.Unlock();
1762                         return 0;
1763                 }
1764
1765         }
1766         input_bufs_omx_all.clear();
1767         input_bufs_omx_free.clear();
1768         input_bufs_omx_mutex.Unlock();
1769
1770 }
1771
1772
1773 int VideoOMX::FlushRenderingPipe()
1774 {
1775         OMX_ERRORTYPE error;
1776
1777         if (!dodeint) {
1778
1779                 error = OMX_SendCommand(omx_vid_dec, OMX_CommandFlush,
1780                                 omx_codec_output_port, NULL);
1781                 if (error != OMX_ErrorNone) {
1782                         Log::getInstance()->log("Video", Log::DEBUG,
1783                                         "OMX_Flush codec out 1 failed %x", error);
1784
1785                 }
1786
1787                 error = OMX_SendCommand(omx_vid_sched, OMX_CommandFlush,
1788                                 omx_shed_input_port, NULL);
1789                 if (error != OMX_ErrorNone) {
1790                         Log::getInstance()->log("Video", Log::DEBUG,
1791                                         "OMX_Flush shed in 2 failed %x", error);
1792
1793                 }
1794
1795                 if (!CommandFinished(omx_vid_dec, OMX_CommandFlush,
1796                                 omx_codec_output_port)) {
1797                         Log::getInstance()->log("Video", Log::DEBUG,
1798                                         "flush cmd codec  3 failed");
1799                 }
1800
1801                 if (!CommandFinished(omx_vid_sched, OMX_CommandFlush,
1802                                 omx_shed_input_port)) {
1803                         Log::getInstance()->log("Video", Log::DEBUG,
1804                                         "flush cmd  shed 4 failed");
1805                 }
1806         } else {
1807                 error = OMX_SendCommand(omx_vid_dec, OMX_CommandFlush,
1808                                 omx_codec_output_port, NULL);
1809                 if (error != OMX_ErrorNone) {
1810                         Log::getInstance()->log("Video", Log::DEBUG,
1811                                         "OMX_Flush codec out 5 failed %x", error);
1812
1813                 }
1814
1815                 error = OMX_SendCommand(omx_vid_deint, OMX_CommandFlush,
1816                                 omx_deint_input_port, NULL);
1817                 if (error != OMX_ErrorNone) {
1818                         Log::getInstance()->log("Video", Log::DEBUG,
1819                                         "OMX_Flush deint in 6 failed %x", error);
1820
1821                 }
1822
1823                 if (!CommandFinished(omx_vid_dec, OMX_CommandFlush,
1824                                 omx_codec_output_port)) {
1825                         Log::getInstance()->log("Video", Log::DEBUG,
1826                                         "flush cmd codec  7 failed");
1827                 }
1828
1829                 if (!CommandFinished(omx_vid_deint, OMX_CommandFlush,
1830                                 omx_deint_input_port)) {
1831                         Log::getInstance()->log("Video", Log::DEBUG,
1832                                         "flush cmd  deint 8 failed");
1833                 }
1834
1835                 //m
1836                 error = OMX_SendCommand(omx_vid_deint, OMX_CommandFlush,
1837                                 omx_deint_output_port, NULL);
1838                 if (error != OMX_ErrorNone) {
1839                         Log::getInstance()->log("Video", Log::DEBUG,
1840                                         "OMX_Flush deint out 9 failed %x", error);
1841
1842                 }
1843
1844                 error = OMX_SendCommand(omx_vid_sched, OMX_CommandFlush,
1845                                 omx_shed_input_port, NULL);
1846                 if (error != OMX_ErrorNone) {
1847                         Log::getInstance()->log("Video", Log::DEBUG,
1848                                         "OMX_Flush shed in 10 failed %x", error);
1849
1850                 }
1851
1852                 if (!CommandFinished(omx_vid_deint, OMX_CommandFlush,
1853                                 omx_deint_output_port)) {
1854                         Log::getInstance()->log("Video", Log::DEBUG,
1855                                         "flush cmd deint 11 failed");
1856                 }
1857
1858                 if (!CommandFinished(omx_vid_sched, OMX_CommandFlush,
1859                                 omx_shed_input_port)) {
1860                         Log::getInstance()->log("Video", Log::DEBUG,
1861                                         "flush cmd  shed 12 failed");
1862                 }
1863
1864
1865
1866         }
1867
1868
1869
1870
1871         error = OMX_SendCommand(omx_vid_rend, OMX_CommandFlush,
1872                         omx_rend_input_port, NULL);
1873         if (error != OMX_ErrorNone) {
1874                 Log::getInstance()->log("Video", Log::DEBUG,
1875                                 "OMX_Flush rend in failed %x", error);
1876
1877         }
1878
1879         error = OMX_SendCommand(omx_vid_sched, OMX_CommandFlush,
1880                         omx_shed_output_port, NULL);
1881         if (error != OMX_ErrorNone) {
1882                 Log::getInstance()->log("Video", Log::DEBUG,
1883                                 "OMX_Flush shed out failed %x", error);
1884
1885         }
1886
1887
1888
1889         if (!CommandFinished(omx_vid_rend, OMX_CommandFlush,
1890                         omx_rend_input_port)) {
1891                 Log::getInstance()->log("Video", Log::DEBUG,
1892                                 "flush cmd shed rend failed");
1893         }
1894
1895         if (!CommandFinished(omx_vid_sched, OMX_CommandFlush,
1896                         omx_shed_output_port)) {
1897                 Log::getInstance()->log("Video", Log::DEBUG,
1898                                 "flush cmd shed rend failed");
1899         }
1900 }
1901
1902
1903 int VideoOMX::DeAllocateCodecsOMX()
1904 {
1905         OMX_ERRORTYPE error;
1906         omx_running=false;
1907           Log::getInstance()->log("Video", Log::DEBUG, "enter deallocatecodecsomx");
1908
1909    if (cur_input_buf_omx) {
1910                 cur_input_buf_omx->nFlags|=OMX_BUFFERFLAG_EOS;
1911                 OMX_ERRORTYPE error=ProtOMXEmptyThisBuffer(omx_vid_dec,cur_input_buf_omx);
1912                 if (error!=OMX_ErrorNone) {
1913                         Log::getInstance()->log("Video", Log::DEBUG, "OMX_EmptyThisBuffer failed %x", error);
1914                 }
1915
1916                 cur_input_buf_omx=NULL;//write out old data
1917         }
1918    clock_mutex.Lock();
1919    clearEvents();
1920         if (omx_vid_dec) {
1921                 // first stop the omx elements
1922                 if (!ChangeComponentState(omx_vid_dec,OMX_StateIdle)) {
1923                         Log::getInstance()->log("Video", Log::DEBUG, "vid_dec ChangeComponentState");
1924
1925                 }
1926                 clock_mutex.Unlock();
1927
1928                 idleClock();
1929                 clock_mutex.Lock();
1930
1931                 if (dodeint) {
1932                         if (!ChangeComponentState(omx_vid_deint, OMX_StateIdle)) {
1933                                 Log::getInstance()->log("Video", Log::DEBUG,
1934                                                 "vid_deint ChangeComponentState");
1935
1936                         }
1937                 }
1938
1939
1940                 if (!ChangeComponentState(omx_vid_sched,OMX_StateIdle)) {
1941                         Log::getInstance()->log("Video", Log::DEBUG, "vid_shed ChangeComponentState");
1942
1943                 }
1944
1945                 if (!ChangeComponentState(omx_vid_rend,OMX_StateIdle)) {
1946                         Log::getInstance()->log("Video", Log::DEBUG, "vid_rend ChangeComponentState");
1947
1948                 }
1949
1950
1951
1952         // TODO proper deinit sequence
1953                 // first flush all buffers
1954                 FlushRenderingPipe();
1955
1956
1957
1958
1959
1960                 error=OMX_SendCommand(omx_clock,OMX_CommandFlush, omx_clock_output_port, NULL);
1961                 if (error!=OMX_ErrorNone){
1962                         Log::getInstance()->log("Video", Log::DEBUG, "OMX_Flush clock out failed %x", error);
1963
1964                 }
1965
1966                 error=OMX_SendCommand(omx_vid_sched,OMX_CommandFlush, omx_shed_clock_port, NULL);
1967                 if (error!=OMX_ErrorNone){
1968                         Log::getInstance()->log("Video", Log::DEBUG, "OMX_Flush shed clock failed %x", error);
1969
1970                 }
1971
1972                 if (!CommandFinished(omx_clock,OMX_CommandFlush,omx_clock_output_port) ||
1973                         !CommandFinished(omx_vid_sched,OMX_CommandFlush,omx_shed_clock_port)) {
1974                                 Log::getInstance()->log("Video", Log::DEBUG, "flush cmd clock shed failed");
1975                 }
1976
1977
1978
1979
1980                 error = OMX_SendCommand(omx_vid_dec, OMX_CommandFlush,
1981                                 omx_codec_input_port, NULL);
1982                 if (error != OMX_ErrorNone) {
1983                         Log::getInstance()->log("Video", Log::DEBUG,
1984                                         "OMX_Flush codec out failed %x", error);
1985
1986                 }
1987
1988
1989
1990
1991                 DestroyInputBufsOMX();
1992
1993                 //todo flushing
1994                 if (!DisablePort(omx_vid_sched,omx_shed_output_port,true)) {
1995                         Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 2 ");
1996                 }
1997                 if (!DisablePort(omx_vid_rend,omx_rend_input_port,true)) {
1998                         Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 1");
1999                 }
2000
2001
2002
2003
2004                 if (!DisablePort(omx_vid_dec,omx_codec_output_port,true)) {
2005                         Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 6");
2006                 }
2007
2008
2009
2010                 if (!DisablePort(omx_vid_dec,omx_codec_input_port,true)) {
2011                         Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 7");
2012                 }
2013
2014                 if (dodeint) {
2015                         if (!DisablePort(omx_vid_deint,omx_deint_output_port,true)) {
2016                                 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 6a");
2017                         }
2018
2019
2020
2021                         if (!DisablePort(omx_vid_deint,omx_deint_input_port,true)) {
2022                                 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 7a");
2023                         }
2024                 }
2025
2026
2027
2028                 if (!DisablePort(omx_vid_sched,omx_shed_input_port,true)) {
2029                         Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 3");
2030                 }
2031
2032                 if (!DisablePort(omx_vid_sched,omx_shed_clock_port,true)) {
2033                         Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 4");
2034                 }
2035
2036                 if (!DisablePort(omx_clock,omx_clock_output_port,true)) {
2037                         Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 5");
2038                 }
2039
2040
2041
2042
2043                 error=OMX_SetupTunnel(omx_vid_dec,omx_codec_output_port,NULL,NULL);
2044                 if (error!=OMX_ErrorNone) {
2045                         Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
2046
2047                 }
2048
2049                 if (dodeint) {
2050                         error=OMX_SetupTunnel(omx_vid_deint,omx_deint_input_port,NULL,NULL);
2051                         if (error!=OMX_ErrorNone) {
2052                                 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
2053                         }
2054
2055
2056                         error=OMX_SetupTunnel(omx_vid_deint,omx_deint_output_port,NULL,NULL);
2057                         if (error!=OMX_ErrorNone) {
2058                                 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
2059                         }
2060                 }
2061
2062                 error=OMX_SetupTunnel(omx_vid_sched,omx_shed_input_port,NULL,NULL);
2063                 if (error!=OMX_ErrorNone) {
2064                         Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
2065
2066                 }
2067
2068
2069                 error=OMX_SetupTunnel(omx_vid_sched,omx_shed_output_port,NULL,NULL);
2070                 if (error!=OMX_ErrorNone) {
2071                         Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
2072
2073                 }
2074
2075                 error=OMX_SetupTunnel(omx_vid_rend,omx_rend_input_port,NULL,NULL);
2076                 if (error!=OMX_ErrorNone) {
2077                         Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
2078
2079                 }
2080
2081
2082                 error=OMX_SetupTunnel(omx_clock,omx_clock_output_port,NULL,NULL);
2083                 if (error!=OMX_ErrorNone) {
2084                         Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
2085
2086                 }
2087
2088                 error=OMX_SetupTunnel(omx_vid_sched,omx_shed_clock_port,NULL,NULL);
2089                 if (error!=OMX_ErrorNone) {
2090                         Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
2091
2092                 }
2093
2094
2095
2096
2097                 error=OMX_FreeHandle(omx_vid_dec);
2098                 error=OMX_FreeHandle(omx_vid_sched);
2099                 error=OMX_FreeHandle(omx_vid_rend);
2100                 if (dodeint) error=OMX_FreeHandle(omx_vid_deint);
2101                 omx_vid_dec=NULL;
2102                 clock_mutex.Unlock();
2103                 destroyClock();
2104                 if (error!=OMX_ErrorNone) {
2105                         Log::getInstance()->log("Video", Log::DEBUG, "FreeHandle failed %d", error);
2106                 }
2107         } else  clock_mutex.Unlock();
2108           Log::getInstance()->log("Video", Log::DEBUG, "leave deallocate codecs OMX");
2109
2110         return 1;
2111 }
2112
2113
2114 void VideoOMX::destroyClock()
2115 {
2116         clock_mutex.Lock();
2117         if (clock_references>0) {
2118                 clock_references--;
2119                 if (clock_references==0) {
2120                         OMX_ERRORTYPE error;
2121                         Log::getInstance()->log("Video", Log::DEBUG, "destroy omx clock");
2122                         error=OMX_FreeHandle(omx_clock);
2123                         if (error!=OMX_ErrorNone) {
2124                                 Log::getInstance()->log("Video", Log::DEBUG, "FreeHandle failed %d", error);
2125                         }
2126
2127                 }
2128         }
2129         clock_mutex.Unlock();
2130
2131 }
2132
2133 int VideoOMX::stop()
2134 {
2135   if (!initted) return 0;
2136   iframemode=false;
2137
2138   //Check if libav mode
2139   DeAllocateCodecsOMX();
2140
2141
2142
2143
2144 //  if (ioctl(fdVideo, AV_SET_VID_STOP, 0) != 0) return 0;
2145   return 1;
2146 }
2147
2148 int VideoOMX::reset()
2149 {
2150   if (!initted) return 0;
2151
2152   iframemode=false;
2153   DeAllocateCodecsOMX();
2154 //  if (ioctl(fdVideo, AV_SET_VID_RESET, 0x11) != 0) return 0;
2155   return 1;
2156 }
2157
2158 int VideoOMX::pause()
2159 {
2160   if (!initted) return 0;
2161   Log::getInstance()->log("Video", Log::DEBUG, "enter pause");
2162  // clockPause();
2163   // ignore it audio handles this
2164   return 1;
2165 }
2166
2167 int VideoOMX::unPause() // FIXME get rid - same as play!! Not here!
2168 {
2169   if (!initted) return 0;
2170   Log::getInstance()->log("Video", Log::DEBUG, "enter unpause");
2171
2172   //clockUnpause();
2173   //ignore it audio handles this
2174
2175   return 1;
2176 }
2177
2178 int VideoOMX::fastForward()
2179 {
2180   if (!initted) return 0;
2181
2182 //  if (ioctl(fdVideo, AV_SET_VID_libavWD, 1) != 0) return 0;
2183   return 1;
2184 }
2185
2186 int VideoOMX::unFastForward()
2187 {
2188   if (!initted) return 0;
2189
2190 //  if (ioctl(fdVideo, AV_SET_VID_RESET, 0x11) != 0) return 0; // don't need this.
2191
2192  //// if (ioctl(fdVideo, AV_SET_VID_PLAY, 0) != 0) return 0;
2193   return 1;
2194 }
2195
2196 int VideoOMX::attachFrameBuffer()
2197 {
2198   if (!initted) return 0;
2199
2200 //  if (ioctl(fdVideo, AV_SET_VID_FB, 0) != 0) return 0;
2201   return 1;
2202 }
2203
2204 int VideoOMX::blank(void)
2205 {
2206 //  if (ioctl(fdVideo, AV_SET_VID_FB, 1) != 0) return 0;
2207 //  if (ioctl(fdVideo, AV_SET_VID_FB, 0) != 0) return 0;
2208   return 1;
2209 }
2210
2211 ULLONG VideoOMX::getCurrentTimestamp() {
2212         if (iframemode)
2213                 return 0;
2214         long long ncur_clock_time = cur_clock_time;
2215         if (omx_running) {
2216                 int oldcancelstate;
2217                 int oldcanceltype;
2218                 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldcancelstate);
2219                 pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldcanceltype);
2220                 clock_mutex.Lock();
2221                 OMX_ERRORTYPE error;
2222                 OMX_TIME_CONFIG_CLOCKSTATETYPE clock_conf;
2223                 memset(&clock_conf, 0, sizeof(clock_conf));
2224                 clock_conf.nSize = sizeof(clock_conf);
2225                 clock_conf.nVersion.nVersion = OMX_VERSION;
2226                 error= OMX_GetConfig(omx_clock, OMX_IndexConfigTimeClockState,
2227                                 &clock_conf);
2228                 if (error != OMX_ErrorNone) {
2229                         Log::getInstance()->log("Video", Log::DEBUG,"getCurrentTimestamp IndexConfigTimeClockState failed %x",error);
2230                 }
2231
2232                 if (clock_conf.eState == OMX_TIME_ClockStateRunning) {
2233
2234                         OMX_TIME_CONFIG_TIMESTAMPTYPE cur_time_stamp;
2235                         memset(&cur_time_stamp, 0, sizeof(cur_time_stamp));
2236                         cur_time_stamp.nSize = sizeof(cur_time_stamp);
2237                         cur_time_stamp.nVersion.nVersion = OMX_VERSION;
2238                         cur_time_stamp.nPortIndex = omx_clock_output_port;
2239                         error = OMX_GetConfig(omx_clock, OMX_IndexConfigTimeCurrentMediaTime,
2240                                         &cur_time_stamp);
2241                         if (error != OMX_ErrorNone) {
2242                                 Log::getInstance()->log("Video",Log::DEBUG,"getCurrentTimestamp OMX_IndexConfigTimeCurrentMediaTime failed %x",error);
2243                         } else {
2244                                 long long temp = cur_time_stamp.nTimestamp.nLowPart
2245                                                 | ((long long) cur_time_stamp.nTimestamp.nHighPart << 32);
2246                                 ncur_clock_time = cur_clock_time = temp * 10LL;
2247                         }
2248                 }
2249                 clock_mutex.Unlock();
2250                 pthread_setcancelstate(oldcancelstate, NULL);
2251                 pthread_setcanceltype(oldcanceltype, NULL);
2252         }
2253
2254         //ncur_clock_time -= startoffset;
2255         ncur_clock_time -= lastreftimeOMX;
2256         long long result = lastreftimePTS;
2257         if (lastreftimePTS==0) return 0; // invalid time
2258         result += (long long) (ncur_clock_time / 10000LL * 90LL);
2259         if (result < 0)
2260                 result = (1LL << 33) - result;
2261         //Log::getInstance()->log("Video", Log::DEBUG,"getCurrentTimestamp %lld %lld %lld %lld %lld %lld",ncur_clock_time,cur_clock_time,lastreftimeOMX,lastreftimePTS,result,startoffset);
2262
2263         return result;
2264
2265 }
2266
2267 // to be removed
2268 /*
2269 ULONG VideoOMX::timecodeToFrameNumber(ULLONG timecode)
2270 {
2271   if (format == PAL) return (ULONG)(((double)timecode / (double)90000) * (double)25);
2272   else               return (ULONG)(((double)timecode / (double)90000) * (double)30);
2273 }
2274
2275 */
2276 #ifdef DEV
2277 int VideoOMX::test()
2278 {
2279   return 0;
2280
2281 //  ULLONG stc = 0;
2282 //  return ioctl(fdVideo, AV_SET_VID_STC, &stc);
2283 /*
2284  // reset();
2285   return 1;
2286 */
2287 }
2288
2289 int VideoOMX::test2()
2290 {
2291   return 0;
2292 }
2293 #endif
2294
2295
2296
2297 long long VideoOMX::SetStartOffset(long long curreftime, bool *rsync)
2298 {
2299   *rsync=false;
2300   if (offsetnotset) {
2301     startoffset=curreftime;//offset is set for audio
2302     offsetnotset=false;
2303     offsetvideonotset=false;
2304   } else {
2305     if (offsetvideonotset) {
2306       offsetvideonotset=false;
2307       *rsync=true;
2308     } else {
2309       if ( (curreftime-lastrefvideotime)>10000000LL
2310         || (curreftime-lastrefvideotime)<-10000000LL) {//if pts jumps to big resync
2311         startoffset+=curreftime-lastrefvideotime;
2312         lastrefaudiotime+=curreftime-lastrefvideotime;
2313         //*rsync=true;
2314         offsetaudionotset=true;
2315
2316       }
2317     }
2318
2319   }
2320
2321   lastrefvideotime=curreftime;
2322
2323   return startoffset;
2324
2325 }
2326
2327 long long VideoOMX::SetStartAudioOffset(long long curreftime, bool *rsync)
2328 {
2329   *rsync=false;
2330   if (offsetnotset) {
2331     startoffset=curreftime;
2332     offsetnotset=false;
2333     offsetaudionotset=false;
2334   }else {
2335     if (offsetaudionotset) {
2336       offsetaudionotset=false;
2337       *rsync=true;
2338     } else {
2339       if ( (curreftime-lastrefaudiotime)>10000000LL
2340         || (curreftime-lastrefaudiotime)<-10000000LL) {//if pts jumps to big resync
2341         startoffset+=curreftime-lastrefaudiotime;
2342         lastrefvideotime+=curreftime-lastrefaudiotime;
2343         //*rsync=true;
2344         offsetvideonotset=true;
2345
2346       }
2347     }
2348
2349   }
2350   lastrefaudiotime=curreftime;
2351   return startoffset;
2352
2353 }
2354
2355 void VideoOMX::ResetTimeOffsets() {
2356   offsetnotset=true; //called from demuxer
2357   offsetvideonotset=true;
2358   offsetaudionotset=true;
2359   startoffset=0;
2360   lastrefaudiotime=0;
2361   lastrefvideotime=0;
2362   lastreftimeOMX=0;
2363   lastreftimePTS=0;
2364 }
2365
2366
2367 void VideoOMX::DeinterlaceFix()
2368 {
2369         int oldcancelstate;
2370         int oldcanceltype;
2371         Demuxer* demux=Demuxer::getInstance();
2372         pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldcancelstate);
2373         pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldcanceltype);
2374         clock_mutex.Lock();
2375         OMX_ERRORTYPE error;
2376         OMX_PARAM_PORTDEFINITIONTYPE port_def_type;
2377         memset(&port_def_type,0,sizeof(port_def_type));
2378         port_def_type.nSize=sizeof(port_def_type);
2379         port_def_type.nVersion.nVersion=OMX_VERSION;
2380         port_def_type.nPortIndex=omx_codec_output_port;
2381
2382         error=OMX_GetParameter(omx_vid_dec,OMX_IndexParamPortDefinition, &port_def_type);
2383         if (error != OMX_ErrorNone) {
2384                 Log::getInstance()->log("Video", Log::DEBUG,
2385                                 "OMX_IndexParamPortDefinition fix failed %x", error);
2386                 clock_mutex.Unlock();
2387                 return;
2388         }
2389
2390         if (port_def_type.format.video.nFrameWidth == demux->getHorizontalSize()
2391                         && port_def_type.format.video.nFrameHeight == demux->getVerticalSize()){
2392                 Log::getInstance()->log("Video", Log::DEBUG,
2393                                                         "Deinit first frame fix");
2394                 deint_first_frame=false;
2395
2396                 WaitForEvent(omx_vid_dec,OMX_EventPortSettingsChanged);
2397                 DisablePort(omx_vid_dec,omx_codec_output_port,false);
2398                 DisablePort(omx_vid_sched,omx_shed_input_port,false);
2399                 DisablePort(omx_vid_deint,omx_deint_output_port,false);
2400                 DisablePort(omx_vid_deint,omx_deint_input_port,false);
2401
2402                 port_def_type.nPortIndex=omx_deint_input_port;
2403                 error = OMX_SetParameter(omx_vid_deint, OMX_IndexParamPortDefinition,
2404                                 &port_def_type);
2405                 if (error != OMX_ErrorNone) {
2406                         Log::getInstance()->log("Video", Log::DEBUG,
2407                                         "Set OMX_IndexParamPortDefinition1 failed %x", error);
2408                         clock_mutex.Unlock();
2409                         pthread_setcancelstate(oldcancelstate, NULL);
2410                         pthread_setcanceltype(oldcanceltype, NULL);
2411                         return;
2412                 }
2413
2414                 port_def_type.nPortIndex=omx_deint_output_port;
2415                 error = OMX_SetParameter(omx_vid_deint, OMX_IndexParamPortDefinition,
2416                                 &port_def_type);
2417                 if (error != OMX_ErrorNone) {
2418                         Log::getInstance()->log("Video", Log::DEBUG,
2419                                         "Set OMX_IndexParamPortDefinition2 failed %x", error);
2420                         clock_mutex.Unlock();
2421                         pthread_setcancelstate(oldcancelstate, NULL);
2422                         pthread_setcanceltype(oldcanceltype, NULL);
2423                         return;
2424                 }
2425
2426
2427                 EnablePort(omx_vid_dec,omx_codec_output_port,false);
2428                 EnablePort(omx_vid_deint,omx_deint_input_port,false);
2429                 EnablePort(omx_vid_deint,omx_deint_output_port,false);
2430                 EnablePort(omx_vid_sched,omx_shed_input_port,false);
2431         }
2432         clock_mutex.Unlock();
2433         pthread_setcancelstate(oldcancelstate, NULL);
2434         pthread_setcanceltype(oldcanceltype, NULL);
2435
2436
2437
2438 }
2439
2440
2441 void VideoOMX::PutBufferToPres(OMX_BUFFERHEADERTYPE* buffer)
2442 {
2443
2444         OMX_ERRORTYPE error = ProtOMXEmptyThisBuffer(omx_vid_dec, buffer);
2445         if (error != OMX_ErrorNone) {
2446                 Log::getInstance()->log("Video", Log::DEBUG,
2447                                 "OMX_EmptyThisBuffer failed %x", error);
2448         }
2449         if (deint_first_frame && dodeint) DeinterlaceFix();
2450
2451 }
2452
2453 OMX_ERRORTYPE VideoOMX::ProtOMXEmptyThisBuffer(OMX_HANDLETYPE handle, OMX_BUFFERHEADERTYPE* buffer)
2454 {
2455         // protect the call to empty this buffer
2456         int oldcancelstate;
2457         int oldcanceltype;
2458 /*      long long temp =buffer->nTimeStamp.nLowPart
2459                                                         | ((long long) buffer->nTimeStamp.nHighPart << 32);*/
2460
2461         //pthread_testcancel();
2462         pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldcancelstate);
2463         pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldcanceltype);
2464         clock_mutex.Lock();
2465 // Diagnosis code
2466 /*      OMX_ERRORTYPE error;
2467         OMX_TIME_CONFIG_TIMESTAMPTYPE timestamp;
2468         memset(&timestamp, 0, sizeof(timestamp));
2469         timestamp.nSize = sizeof(timestamp);
2470         timestamp.nVersion.nVersion = OMX_VERSION;
2471         timestamp.nPortIndex =omx_clock_output_port;
2472
2473         error = OMX_GetConfig(omx_clock, OMX_IndexConfigTimeCurrentMediaTime,
2474                         &timestamp);
2475
2476         if (error != OMX_ErrorNone) {
2477                 Log::getInstance()->log("Audio", Log::DEBUG,
2478                                 "Init OMX_IndexConfigAudioRenderingLatencyfailed %x %d", error,
2479                                 omx_rend_input_port);
2480         }
2481         long long temp2 =timestamp.nTimestamp.nLowPart
2482                                                                 | ((long long) timestamp.nTimestamp.nHighPart << 32);
2483
2484
2485         Log::getInstance()->log("Video", Log::NOTICE, "OMXETB %x %lld %lld %x",handle,temp,temp2,buffer->nFlags);*/
2486         OMX_ERRORTYPE ret_val;
2487         ret_val=OMX_EmptyThisBuffer(handle,buffer);
2488         clock_mutex.Unlock();
2489         pthread_setcancelstate(oldcancelstate, NULL);
2490         pthread_setcanceltype(oldcanceltype, NULL);
2491         //pthread_testcancel();
2492         return ret_val;
2493 }
2494
2495 bool VideoOMX::detectIFrame(const UCHAR *buffer,unsigned int length)
2496 {
2497         const UCHAR* curbuf=buffer;
2498         const UCHAR* curbufend=buffer+length;
2499         unsigned int detector=0xFFFFFFFF;
2500         bool gotaud=false; // have seen access unit delimiter
2501
2502         while (curbuf!=curbufend) {
2503                 detector<<=8;
2504                 detector|=*curbuf;
2505                 if (h264) {
2506                         if (detector==0x00000109) {
2507                                 gotaud=true;
2508                                 detector=0xFFFFFFFF;
2509                         } else if (gotaud &&detector==0x00000001) {
2510                                 curbuf++;
2511                                 if (curbuf!=curbufend) {
2512                                         unsigned char picttype=(*curbuf)& 0x1F;
2513                                         return picttype==0x07;
2514                                 }
2515                         }
2516                 } else {
2517                         if (detector==0x00000100) {
2518                                 curbuf++;
2519                                 if (curbuf==curbufend) return false;
2520                                 curbuf++;
2521                                 if (curbuf==curbufend) return false;
2522                                 unsigned char picttype=((*curbuf) >> 3) & 0x07;
2523                                 return picttype==1;
2524                         }
2525                 }
2526                 curbuf++;
2527         }
2528
2529         return false; // no frame found
2530 }
2531
2532 void VideoOMX::PrepareMediaSample(const MediaPacketList& mplist,UINT samplepos)
2533 {
2534         
2535         mediapackets.clear();
2536         list<MediaPacket>::const_iterator begin=mplist.begin();
2537         list<MediaPacket>::const_iterator itty=mplist.begin();
2538         advance(itty,min(mplist.size(),10));
2539         mediapackets.insert(mediapackets.begin(),begin,itty);//front
2540         
2541 }
2542
2543 UINT VideoOMX::DeliverMediaSample(UCHAR* buffer, UINT *samplepos)
2544 {
2545         int consumed=0;
2546         while (consumed<mediapackets.size()) {
2547             DeliverMediaPacket(mediapackets[consumed], buffer, samplepos);
2548             if (*samplepos == mediapackets[consumed].length) {
2549                 *samplepos = 0;
2550                 consumed++;
2551                 //return 1;
2552             } else return consumed;
2553         }
2554         return consumed;
2555 }
2556
2557 UINT VideoOMX::DeliverMediaPacket(MediaPacket packet,
2558                 const UCHAR* buffer,
2559                 UINT *samplepos)
2560 {
2561         if (packet.type == MPTYPE_VIDEO_H264)
2562         {
2563                 h264=true;
2564         }
2565         else
2566         {
2567                 h264=false;
2568         }
2569
2570
2571         //Later add fail back code for libav
2572 /*      if (!videoon) {
2573                 *samplepos+=packet.length;
2574                 return packet.length;
2575         }*/
2576
2577
2578         if (!omx_running) return 0; // if we are not runnig do not do this
2579         if (isClockPaused()) return 0; //Block if we pause
2580
2581 //      Log::getInstance()->log("Video", Log::DEBUG, "DMP mark 1");
2582         //Log::getInstance()->log("Video", Log::DEBUG, "DeliverMediaPacketOMX time %lld",packet.presentation_time);
2583
2584
2585         OMX_ERRORTYPE error;
2586
2587         /*First Check, if we have an video sample*/
2588         if (iframemode) {
2589                 //samplepos=0;
2590                 MILLISLEEP(10);
2591                 return 0; //Not in iframe mode!
2592         }
2593
2594         UINT headerstrip=0;
2595         if (packet.disconti) {
2596                 firstsynched=false;
2597                 if (cur_input_buf_omx) {
2598                         PutBufferToPres(cur_input_buf_omx);
2599                         cur_input_buf_omx=NULL;
2600                 }
2601         }
2602         //Log::getInstance()->log("Video", Log::DEBUG, "DMP mark 2");
2603         /*Inspect PES-Header */
2604
2605 //      OMX_STATETYPE temp_state;
2606 //      OMX_GetState(omx_vid_dec,&temp_state);
2607
2608         if (*samplepos==0) {//stripheader
2609                 headerstrip=buffer[packet.pos_buffer+8]+9/*is this right*/;
2610         //      if (h264) Log::getInstance()->log("Video", Log::DEBUG, "PES info %x %x %x %x",
2611         //                      buffer[packet.pos_buffer+0],buffer[packet.pos_buffer+1],buffer[packet.pos_buffer+2],buffer[packet.pos_buffer+3]);
2612                 *samplepos+=headerstrip;
2613                 if ( packet.synched ) {
2614                         if (!firstsynched) {
2615                                 //Log::getInstance()->log("Video", Log::DEBUG, "DMP mark 2a");
2616                           // check if this is an I frame, the decoder does not like non I frames at startup!
2617                           if (!detectIFrame(buffer,packet.length)) {
2618                                   *samplepos=packet.length;//if we have not processed at least one
2619                                   //Log::getInstance()->log("Video", Log::DEBUG, "DMP mark 3");
2620                                   return packet.length;//synched packet ignore it!
2621                           }
2622                         }
2623                         if (cur_input_buf_omx) {
2624                                 //Log::getInstance()->log("Video", Log::DEBUG, "DMP mark 4a");
2625                                 cur_input_buf_omx->nFlags|=OMX_BUFFERFLAG_ENDOFFRAME;
2626                                 PutBufferToPres(cur_input_buf_omx);
2627                                 cur_input_buf_omx=NULL;//write out old data
2628
2629                                 //Log::getInstance()->log("Video", Log::DEBUG, "DMP mark 4b");
2630
2631                         }
2632                         firstsynched=true;
2633                 } else {
2634                         if (!firstsynched) {//
2635                                 *samplepos=packet.length;//if we have not processed at least one
2636                                 //Log::getInstance()->log("Video", Log::DEBUG, "DMP mark 5");
2637                                 return packet.length;//synched packet ignore it!
2638                         }
2639                 }
2640         }
2641         //Log::getInstance()->log("Video", Log::DEBUG, "DMP mark 6");
2642         if (!cur_input_buf_omx) {
2643                 input_bufs_omx_mutex.Lock();
2644                 if (input_bufs_omx_free.size()==0) {
2645                         input_bufs_omx_mutex.Unlock();
2646                         //Log::getInstance()->log("Video", Log::DEBUG, "Deliver MediaPacket no free sample");
2647                         //Log::getInstance()->log("Video", Log::DEBUG, "DMP mark 7");
2648                         return 0; // we do not have a free media sample
2649
2650                 }
2651                 cur_input_buf_omx=input_bufs_omx_free.front();
2652                 cur_input_buf_omx->nFilledLen=0;
2653                 cur_input_buf_omx->nOffset=0;
2654                 cur_input_buf_omx->nTimeStamp=intToOMXTicks(0);
2655                 input_bufs_omx_free.pop_front();
2656                 input_bufs_omx_mutex.Unlock();
2657         }
2658
2659
2660
2661
2662         if (cur_input_buf_omx->nFilledLen==0) {//will only be changed on first packet
2663                 if (packet.synched) {
2664                 //      Log::getInstance()->log("Video", Log::DEBUG, "packet synched marker");
2665
2666                         //lastreftimePTS=packet.pts;
2667                    if (omx_first_frame) { // TODO time
2668                            cur_input_buf_omx->nFlags=OMX_BUFFERFLAG_STARTTIME;
2669                            Log::getInstance()->log("Video", Log::DEBUG, "Starttime");
2670                            omx_first_frame=false;
2671                    } else {
2672                            cur_input_buf_omx->nFlags=0;
2673                            //cur_input_buf_omx->nFlags|=OMX_BUFFERFLAG_TIME_UNKNOWN;
2674                    }
2675                    lastreftimeOMX=packet.presentation_time;
2676                   // Log::getInstance()->log("Video", Log::DEBUG, "Time code %lld pts %lld", lastreftimeOMX,packet.pts);
2677                    lastreftimePTS=packet.pts;
2678                    cur_input_buf_omx->nTimeStamp=intToOMXTicks(lastreftimeOMX/10LL); // the clock component is faulty;
2679                 }
2680                 else
2681                 {
2682                         cur_input_buf_omx->nFlags=OMX_BUFFERFLAG_TIME_UNKNOWN;
2683                         cur_input_buf_omx->nTimeStamp=intToOMXTicks(0);
2684                         //Log::getInstance()->log("Video", Log::DEBUG, "packet unsynched marker");
2685                 }
2686                 if (packet.disconti) cur_input_buf_omx->nFlags|=OMX_BUFFERFLAG_DISCONTINUITY;
2687
2688
2689
2690         }
2691         unsigned int haveToCopy=packet.length-*samplepos;
2692         //Log::getInstance()->log("Video", Log::DEBUG, "DMP mark 8");
2693
2694         while (haveToCopy> (cur_input_buf_omx->nAllocLen-cur_input_buf_omx->nFilledLen)) {
2695                 //Log::getInstance()->log("Video", Log::DEBUG, "Big buffer %d %d %d",packet.length,cur_input_buf_omx->nAllocLen,cur_input_buf_omx->nFilledLen);
2696                 unsigned int cancopy=cur_input_buf_omx->nAllocLen-cur_input_buf_omx->nFilledLen;
2697                 memcpy(cur_input_buf_omx->pBuffer+cur_input_buf_omx->nFilledLen,buffer+packet.pos_buffer+*samplepos,cancopy);
2698                 haveToCopy-=cancopy;
2699                 cur_input_buf_omx->nFilledLen+=cancopy;
2700                 *samplepos+=cancopy;
2701                 // push old buffer out
2702                 //Log::getInstance()->log("Video", Log::DEBUG, "DMP mark 9");
2703
2704                 PutBufferToPres(cur_input_buf_omx);
2705                 cur_input_buf_omx=NULL;
2706                 // get5 new buffer
2707                 input_bufs_omx_mutex.Lock();
2708                 if (input_bufs_omx_free.size()==0) {
2709                         input_bufs_omx_mutex.Unlock();
2710                 //      Log::getInstance()->log("Video", Log::DEBUG, "Deliver MediaPacket no free sample2");
2711                         return *samplepos; // we do not have a free media sample
2712                 }
2713                 cur_input_buf_omx=input_bufs_omx_free.front();
2714                 cur_input_buf_omx->nFilledLen=0;
2715                 cur_input_buf_omx->nOffset=0;
2716                 cur_input_buf_omx->nFlags=OMX_BUFFERFLAG_TIME_UNKNOWN;
2717                 cur_input_buf_omx->nTimeStamp=intToOMXTicks(0);
2718                 input_bufs_omx_free.pop_front();
2719                 input_bufs_omx_mutex.Unlock();
2720                 //Log::getInstance()->log("Video", Log::DEBUG, "DMP mark 10");
2721
2722         }
2723         memcpy(cur_input_buf_omx->pBuffer+cur_input_buf_omx->nFilledLen,
2724                         buffer+packet.pos_buffer+*samplepos,haveToCopy);
2725         cur_input_buf_omx->nFilledLen+=haveToCopy;
2726
2727 //      Log::getInstance()->log("Video", Log::DEBUG, "DMP mark 11");
2728
2729         *samplepos+=haveToCopy;
2730
2731         return *samplepos;
2732
2733 }
2734
2735
2736
2737
2738 bool VideoOMX::displayIFrame(const UCHAR* buffer, UINT length) {
2739         if (!omx_running) return false;
2740         if (!iframemode)
2741                 EnterIframePlayback();
2742
2743         int haveToCopy = length;
2744
2745         if (!cur_input_buf_omx) {
2746                 input_bufs_omx_mutex.Lock();
2747                 if (input_bufs_omx_free.size() == 0) {
2748                         input_bufs_omx_mutex.Unlock();
2749                 //      Log::getInstance()->log("Video", Log::DEBUG,
2750                         //              "Deliver MediaPacket no free sample");
2751                         return false; // we do not have a free media sample
2752
2753                 }
2754                 cur_input_buf_omx = input_bufs_omx_free.front();
2755                 cur_input_buf_omx->nFilledLen = 0;
2756                 cur_input_buf_omx->nOffset = 0;
2757                 cur_input_buf_omx->nTimeStamp = intToOMXTicks(0);
2758                 input_bufs_omx_free.pop_front();
2759                 input_bufs_omx_mutex.Unlock();
2760         }
2761
2762         int read_pos = 0;
2763         unsigned int pattern, packet_length;
2764         unsigned int headerstrip = 0;
2765         bool first = true;
2766         if (length < 4){
2767                 return false;
2768         }
2769         //Now we strip the pes header
2770         pattern = (buffer[0] << 16) | (buffer[1] << 8) | (buffer[2]);
2771         while (read_pos + 7 <= length) {
2772                 pattern = ((pattern << 8) & 0xFFFFFFFF) | buffer[read_pos + 3];
2773                 if (pattern < 0x000001E0 || pattern > 0x000001EF) {
2774                         read_pos++;
2775                         continue;
2776                 } else {
2777                         headerstrip = buffer[read_pos + 8] + 9/*is this right*/;
2778                         packet_length = ((buffer[read_pos + 4] << 8)
2779                                         | (buffer[read_pos + 5])) + 6;
2780                         if (read_pos + packet_length > length)
2781                                 read_pos = length;
2782                         else {
2783                                 if ((headerstrip < packet_length)
2784                                                 && (cur_input_buf_omx->nFilledLen + packet_length
2785                                                                 - headerstrip) > cur_input_buf_omx->nAllocLen) {
2786                                         if (first) {
2787                                                 cur_input_buf_omx->nFlags = OMX_BUFFERFLAG_STARTTIME;
2788
2789                                         } else {
2790                                                 cur_input_buf_omx->nFlags
2791                                                                 |= OMX_BUFFERFLAG_TIME_UNKNOWN;
2792
2793                                         }
2794                                         cur_input_buf_omx->nTimeStamp = intToOMXTicks(0);
2795                                         PutBufferToPres(cur_input_buf_omx);
2796                                         cur_input_buf_omx = NULL;
2797
2798                                         if (!cur_input_buf_omx) {
2799                                                 int count = 0;
2800                                                 while (count < 100 && omx_running && iframemode) {
2801                                                         count++;
2802
2803                                                         input_bufs_omx_mutex.Lock();
2804                                                         if (input_bufs_omx_free.size() == 0) {
2805                                                                 input_bufs_omx_mutex.Unlock();
2806                                         //                      Log::getInstance()->log("Video", Log::DEBUG,
2807                                                 //                              "Ifrane no free sample");
2808                                                                 MILLISLEEP(5);
2809                                                                 if (!omx_running) return false;
2810                                                                 continue;
2811                                                         }
2812                                                         cur_input_buf_omx = input_bufs_omx_free.front();
2813                                                         cur_input_buf_omx->nFilledLen = 0;
2814                                                         cur_input_buf_omx->nOffset = 0;
2815                                                         cur_input_buf_omx->nTimeStamp = intToOMXTicks(0);
2816                                                         cur_input_buf_omx->nFlags|= OMX_BUFFERFLAG_TIME_UNKNOWN;
2817                                                         input_bufs_omx_free.pop_front();
2818                                                         input_bufs_omx_mutex.Unlock();
2819                                                         break;
2820                                                 }
2821                                                 if (!cur_input_buf_omx)
2822                                                         return false;
2823                                         }
2824
2825                                 }
2826                                 if (packet_length > headerstrip) {
2827                                         memcpy(
2828                                                         cur_input_buf_omx->pBuffer
2829                                                                         + cur_input_buf_omx->nFilledLen,
2830                                                         buffer + read_pos + headerstrip,
2831                                                         packet_length - headerstrip);
2832                                         cur_input_buf_omx->nFilledLen += packet_length
2833                                                         - headerstrip;
2834                                 }
2835                                 read_pos += packet_length;
2836
2837                                 pattern = (buffer[read_pos] << 16)
2838                                                 | (buffer[read_pos + 1] << 8) | (buffer[read_pos + 2]);
2839                         }
2840                 }
2841         }
2842
2843         if (first) {
2844                 cur_input_buf_omx->nFlags = OMX_BUFFERFLAG_STARTTIME;
2845
2846         } else {
2847                 cur_input_buf_omx->nFlags |= OMX_BUFFERFLAG_TIME_UNKNOWN;
2848
2849         }
2850         cur_input_buf_omx->nTimeStamp = intToOMXTicks(0);
2851         
2852         PutBufferToPres(cur_input_buf_omx);
2853         cur_input_buf_omx = NULL;
2854
2855
2856         MILLISLEEP(40); //Block a bit
2857         return true;
2858 }
2859
2860 int VideoOMX::EnterIframePlayback()
2861 {
2862         Log::getInstance()->log("Video", Log::DEBUG,
2863                                                 "EnterIframePlayback");
2864         if (cur_input_buf_omx) {
2865                 PutBufferToPres(cur_input_buf_omx);
2866                 cur_input_buf_omx = NULL;
2867         }
2868         Log::getInstance()->log("Video", Log::DEBUG,
2869                                                         "EnterIframePlayback 2");
2870         ((AudioOMX*)Audio::getInstance())->DeAllocateCodecsOMX();
2871         DeAllocateCodecsOMX();
2872         AllocateCodecsOMX();
2873         Log::getInstance()->log("Video", Log::DEBUG,
2874                                                         "leave IframePlayback");
2875
2876         iframemode=true;
2877
2878         return 1;
2879 }
2880