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