]> git.vomp.tv Git - vompclient-marten.git/blob - videovpeogl.cc
h264 working + code cleanup
[vompclient-marten.git] / videovpeogl.cc
1 /*
2     Copyright 2004-2005 Chris Tallon, 2009 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 "videovpeogl.h"
22 #include "audiovpe.h"
23 #include "mtdraspberry.h"
24 #include "demuxer.h"
25 #include "osdopengl.h"
26
27 // temp
28 #include "log.h"
29
30 //A lot of parts of this file are heavily inspired by xbmc omx implementations
31
32 VideoVPEOGL::VideoVPEOGL()
33 {
34   if (instance) return;
35
36   lastpacketnum=-1;
37   omx_running=false;
38
39   omx_vid_dec=0;
40   cur_input_buf_omx=NULL;
41   omx_h264=omx_mpeg2=true;
42   
43 }
44
45 VideoVPEOGL::~VideoVPEOGL()
46 {
47   instance = NULL;
48 }
49
50 int VideoVPEOGL::init(UCHAR tformat)
51 {
52   if (initted) return 0;
53   initted = 1;
54
55 //  if ((fdVideo = open("/dev/vdec_dev", O_WRONLY)) < 0) return 0;
56
57   if (!setFormat(tformat))           { shutdown(); return 0; }
58   if (!setConnection(COMPOSITERGB))  { shutdown(); return 0; }
59   if (!setAspectRatio(ASPECT4X3))    { shutdown(); return 0; }
60   if (!setMode(NORMAL))              { shutdown(); return 0; }
61   if (!setSource())                  { shutdown(); return 0; }
62   if (!attachFrameBuffer())          { shutdown(); return 0; }
63
64   setTVsize(ASPECT4X3);
65
66 /*  if (format == PAL) setLetterboxBorder("38");
67   else setLetterboxBorder("31");*/
68
69   /* new stuff */
70
71
72   stop();
73
74
75   return 1;
76 }
77
78 int VideoVPEOGL::initUsingOSDObjects()
79 {
80         EGLDisplay i_egl_display;
81         EGLSurface i_egl_surface;
82         EGLContext i_egl_context;
83         OsdOpenGL *osd=(OsdOpenGL*)osd->getInstance();
84         osd->getEGLObjs(&i_egl_display,&i_egl_surface,&i_egl_context);
85
86         egl_display=i_egl_display;
87         egl_surface=i_egl_surface;
88         egl_context=i_egl_context;
89
90
91 #ifdef VPE_OMX_SUPPORT
92 // we are called before the audio
93         OMX_ERRORTYPE error;
94         error=OMX_Init();
95         if (error!=OMX_ErrorNone) {
96                 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX failed %x", error);
97                 return 0;
98         }
99
100         //our callbacks move to play?
101
102
103
104
105 #endif
106         return 1;
107 }
108
109
110 #ifdef VPE_OMX_SUPPORT
111
112 OMX_ERRORTYPE VideoVPEOGL::EventHandler_OMX(OMX_IN OMX_HANDLETYPE handle,OMX_IN OMX_PTR appdata,
113            OMX_IN OMX_EVENTTYPE event_type,OMX_IN OMX_U32 data1,
114            OMX_IN OMX_U32 data2,OMX_IN OMX_PTR event_data) {
115
116         Log::getInstance()->log("Video", Log::NOTICE, "eventHandler %x %x %x %x %x",handle,event_type,data1,data2,event_data);
117
118         struct VPE_OMX_EVENT  new_event;
119         new_event.handle=handle;
120         new_event.appdata=appdata;
121         new_event.event_type=event_type;
122         new_event.data1=data1;
123         new_event.data2=data2;
124         new_event.event_data=event_data;
125
126         VideoVPEOGL *video=(VideoVPEOGL *)getInstance();
127         video->AddOmxEvent(new_event);
128
129 /*      switch (event_type) {
130         case OMX_EventCmdComplete: {
131
132         } break;
133         }*/
134
135         return OMX_ErrorNone;
136
137 }
138
139 void VideoVPEOGL::AddOmxEvent(VPE_OMX_EVENT  new_event)
140 {
141         omx_event_mutex.Lock();
142     omx_events.push_back(new_event);
143         omx_event_mutex.Unlock();
144 }
145
146
147 OMX_ERRORTYPE VideoVPEOGL::EmptyBufferDone_OMX(OMX_IN OMX_HANDLETYPE hcomp,OMX_IN OMX_PTR appdata,OMX_IN OMX_BUFFERHEADERTYPE* buffer){
148
149         Log::getInstance()->log("Video", Log::NOTICE, "EmptyBufferDone");
150         VideoVPEOGL *video=(VideoVPEOGL *)getInstance();
151         video->ReturnEmptyOMXBuffer(buffer);
152         return OMX_ErrorNone;
153
154 }
155
156 void VideoVPEOGL::ReturnEmptyOMXBuffer(OMX_BUFFERHEADERTYPE* buffer){
157         input_bufs_omx_mutex.Lock();
158         Log::getInstance()->log("Video", Log::NOTICE, "ReturnEmptyOMXBuffer %d",input_bufs_omx_free.size());
159         input_bufs_omx_free.push_back(buffer);
160         Log::getInstance()->log("Video", Log::NOTICE, "ReturnEmptyOMXBuffer %d",input_bufs_omx_free.size());
161         input_bufs_omx_mutex.Unlock();
162 }
163
164  OMX_ERRORTYPE VideoVPEOGL::FillBufferDone_OMX(OMX_IN OMX_HANDLETYPE hcomp, OMX_IN OMX_PTR appdata,OMX_IN OMX_BUFFERHEADERTYPE* buffer) {
165          Log::getInstance()->log("Video", Log::NOTICE, "FillBufferDone");
166         return OMX_ErrorNone;
167 }
168
169 #endif
170
171 int VideoVPEOGL::shutdown()
172 {
173   if (!initted) return 0;
174   initted = 0;
175
176 #ifdef VPE_OMX_SUPPORT
177   DeAllocateCodecsOMX();
178   OMX_Deinit();
179 #endif
180   eglDestroyContext(egl_display,egl_context);
181 //  close(fdVideo);
182   return 1;
183 }
184
185
186
187
188 int VideoVPEOGL::setTVsize(UCHAR ttvsize)
189 {
190 /*  tvsize = ttvsize;
191
192   // Override the aspect ratio usage, temporarily use to set the video chip mode
193   if (!setAspectRatio(tvsize))       { shutdown(); return 0; }
194   close(fdVideo);
195   if ((fdVideo = open("/dev/vdec_dev", O_WRONLY)) < 0) return 0;
196   if (!setSource())                  { shutdown(); return 0; }
197   if (!attachFrameBuffer())          { shutdown(); return 0; }
198
199   // Reopening the fd causes the scart aspect line to go back to 4:3
200   // Set this again to the same as the tv screen size
201   if (!setAspectRatio(tvsize))       { shutdown(); return 0; }
202
203   // mode == LETTERBOX is invalid if the TV is widescreen
204   if (tvsize == ASPECT16X9) setMode(NORMAL);
205 */
206   return 1;
207 }
208
209 int VideoVPEOGL::setDefaultAspect()
210 {
211   return setAspectRatio(tvsize);
212 }
213
214
215
216 int VideoVPEOGL::setFormat(UCHAR tformat)
217 {
218   if (!initted) return 0;
219   if ((tformat != PAL) && (tformat != NTSC)) return 0;
220   format = tformat;
221
222 //  if (ioctl(fdVideo, AV_SET_VID_DISP_FMT, format) != 0) return 0;
223
224   if (format == NTSC)
225   {
226     screenWidth = 720;
227     screenHeight = 480;
228   }
229   if (format == PAL)
230   {
231     screenWidth = 720;
232     screenHeight = 576;
233   }
234
235   return 1;
236 }
237
238 int VideoVPEOGL::setConnection(UCHAR tconnection)
239 {
240   if (!initted) return 0;
241   if ((tconnection != COMPOSITERGB) && (tconnection != SVIDEO)) return 0;
242   connection = tconnection;
243
244 //  if (ioctl(fdVideo, AV_SET_VID_OUTPUT, connection) != 0) return 0;
245   return 1;
246 }
247
248 int VideoVPEOGL::setAspectRatio(UCHAR taspectRatio)
249 {
250   if (!initted) return 0;
251   if ((taspectRatio != ASPECT4X3) && (taspectRatio != ASPECT16X9)) return 0;
252   aspectRatio = taspectRatio;
253
254   Log::getInstance()->log("Video", Log::DEBUG, "Setting aspect to %i", aspectRatio);
255
256 //  if (ioctl(fdVideo, AV_SET_VID_RATIO, aspectRatio) != 0) return 0;
257   return 1;
258 }
259
260 int VideoVPEOGL::setMode(UCHAR tmode)
261 {
262   if (!initted) return 0;
263
264   if ((tmode == LETTERBOX) && (tvsize == ASPECT16X9)) return 0; // invalid mode
265
266   if ((tmode != NORMAL) && (tmode != LETTERBOX) && (tmode != UNKNOWN2) && (tmode != QUARTER) && (tmode != EIGHTH)
267       && (tmode != ZOOM) && (tmode != UNKNOWN6)) return 0;
268   mode = tmode;
269
270 //  if (ioctl(fdVideo, AV_SET_VID_MODE, mode) != 0) return 0;
271   return 1;
272 }
273
274 int VideoVPEOGL::signalOff()
275 {
276 //  if (ioctl(fdVideo, AV_SET_VID_DENC, 0) != 0) return 0;
277   return 1;
278 }
279
280 int VideoVPEOGL::signalOn()
281 {
282 //  if (ioctl(fdVideo, AV_SET_VID_DENC, 1) != 0) return 0;
283   return 1;
284 }
285
286 int VideoVPEOGL::setSource()
287 {
288   if (!initted) return 0;
289
290   // What does this do...
291 //  if (ioctl(fdVideo, AV_SET_VID_SRC, 1) != 0) return 0;
292   return 1;
293 }
294
295 int VideoVPEOGL::setPosition(int x, int y)
296 {
297   if (!initted) return 0;
298
299 //  vid_pos_regs_t pos_d;
300 //  pos_d.x = x;
301 //  pos_d.y = y;
302
303 /*  vid_pos_regs_t pos_d;
304
305   memset(&pos_d, 0, sizeof(pos_d));
306
307   pos_d.dest.y = y;
308   pos_d.dest.x = x;
309 /*
310 typedef struct {
311   int w;
312   int h;
313   int scale;
314   int x1;
315   int y;
316   int x;
317   int y2;
318   int x3;
319   int y3;
320   int x4;
321   int y4;
322 } vid_pos_regs_t;
323 */
324
325 /*
326   pos_d.w = 100;
327   pos_d.h = 30;
328   pos_d.scale = 2;
329   pos_d.x1 = 0;
330   pos_d.y = 100;            // Top left X
331   pos_d.x = 50;            // Top left Y
332   pos_d.y2 = 30;
333   pos_d.x3 = 60;
334   pos_d.y3 = 90;
335   pos_d.x4 = 120;
336   pos_d.y4 = 150;
337 */
338
339 //  if (ioctl(fdVideo, AV_SET_VID_POSITION, &pos_d) != 0) return 0;
340   return 1;
341 }
342
343 int VideoVPEOGL::sync()
344 {
345   if (!initted) return 0;
346
347 //  if (ioctl(fdVideo, AV_SET_VID_SYNC, 2) != 0) return 0;
348   return 1;
349 }
350
351
352
353
354 int VideoVPEOGL::play()
355 {
356   if (!initted) return 0;
357 #ifdef VPE_OMX_SUPPORT
358   if (AllocateCodecsOMX()) {
359
360           return 1;
361           // Otherwise fall back to ffmpeg
362   } else {
363           if (h264) {
364                   omx_h264=false;
365                   Log::getInstance()->log("Video", Log::NOTICE, "Allocate Codecs OMX failed asuume h264 unsupported");
366           } else {
367                   omx_mpeg2=false;
368                   Log::getInstance()->log("Video", Log::NOTICE, "Allocate Codecs OMX failed asuume mpeg2 unsupported");
369           }
370   }
371 #endif
372
373 //  if (ioctl(fdVideo, AV_SET_VID_PLAY, 0) != 0) return 0;
374   return 1;
375 }
376
377 #ifdef VPE_OMX_SUPPORT
378 int VideoVPEOGL::AllocateCodecsOMX()
379 {
380         OMX_ERRORTYPE error;
381         static OMX_CALLBACKTYPE callbacks= {&EventHandler_OMX,&EmptyBufferDone_OMX,&FillBufferDone_OMX};
382
383         Log::getInstance()->log("Video", Log::NOTICE, "Allocate Codecs OMX");
384         //Clock, move later to audio
385
386         omx_events.clear();
387
388         error=OMX_GetHandle(&omx_clock,VPE_OMX_CLOCK,NULL,&callbacks);
389
390
391         if (error!=OMX_ErrorNone){
392                 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX clock failed %x", error);
393                 DeAllocateCodecsOMX();
394                 return 0;
395         }
396
397
398
399         OMX_PORT_PARAM_TYPE p_param;
400         memset(&p_param,0,sizeof(p_param));
401         p_param.nSize=sizeof(p_param);
402         p_param.nVersion.nVersion=OMX_VERSION;
403         error=OMX_GetParameter(omx_clock,OMX_IndexParamOtherInit,&p_param);
404         if (error!=OMX_ErrorNone){
405                 Log::getInstance()->log("Video", Log::DEBUG, "Init clock OMX_GetParameter failed %x", error);
406                 DeAllocateCodecsOMX();
407                 return 0;
408         }
409         omx_clock_output_port=p_param.nStartPortNumber;
410
411         for (unsigned int i=0;i<p_param.nPorts;i++) {
412                 if (!DisablePort(omx_clock,p_param.nStartPortNumber+i) ) {
413                         Log::getInstance()->log("Video", Log::DEBUG, "Disable Ports OMX clock failed %d",i);
414                         return 0;
415                 }
416         }
417
418
419         OMX_TIME_CONFIG_CLOCKSTATETYPE clock_conf;
420         memset(&clock_conf,0,sizeof(clock_conf));
421         clock_conf.nSize=sizeof(clock_conf);
422         clock_conf.nVersion.nVersion=OMX_VERSION;
423         clock_conf.eState=OMX_TIME_ClockStateWaitingForStartTime;
424         clock_conf.nStartTime=0;
425         clock_conf.nOffset=0;
426         clock_conf.nWaitMask=OMX_CLOCKPORT0;
427         error=OMX_SetParameter(omx_clock,OMX_IndexConfigTimeClockState,&clock_conf);
428         if (error!=OMX_ErrorNone){
429                 Log::getInstance()->log("Video", Log::DEBUG, "Clock IndexConfigTimeClockState failed %x", error);
430         }
431
432
433
434         if (h264) {
435                 error=OMX_GetHandle(&omx_vid_dec,VPE_OMX_H264_DECODER,NULL,&callbacks);
436         } else {
437                 error=OMX_GetHandle(&omx_vid_dec,VPE_OMX_MPEG2_DECODER,NULL,&callbacks);
438         }
439
440         if (error!=OMX_ErrorNone){
441                 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX video decoder failed %x", error);
442                 DeAllocateCodecsOMX();
443                 return 0;
444         }
445
446
447
448         memset(&p_param,0,sizeof(p_param));
449         p_param.nSize=sizeof(p_param);
450         p_param.nVersion.nVersion=OMX_VERSION;
451         error=OMX_GetParameter(omx_vid_dec,OMX_IndexParamVideoInit,&p_param);
452         if (error!=OMX_ErrorNone){
453                 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX h264 decoder OMX_GetParameter failed %x", error);
454                 DeAllocateCodecsOMX();
455             return 0;
456         }
457         omx_codec_input_port=p_param.nStartPortNumber;
458         omx_codec_output_port=p_param.nStartPortNumber+1;
459
460         if (!DisablePort(omx_vid_dec,omx_codec_input_port) || !DisablePort(omx_vid_dec,omx_codec_output_port)) {
461                 Log::getInstance()->log("Video", Log::DEBUG, "Disable Ports OMX video decoder failed");
462                 return 0;
463         }
464
465
466         OMX_PARAM_BRCMVIDEODECODEERRORCONCEALMENTTYPE conceal;
467         memset(&conceal,0,sizeof(conceal));
468         conceal.nSize=sizeof(conceal);
469         conceal.nVersion.nVersion=OMX_VERSION;
470         conceal.bStartWithValidFrame=OMX_FALSE;
471
472         error=OMX_SetParameter(omx_vid_dec,OMX_IndexParamBrcmVideoDecodeErrorConcealment,&conceal);
473         if (error!=OMX_ErrorNone){
474                 Log::getInstance()->log("Video", Log::DEBUG, "OMX_IndexParamBrcmVideoDecodeErrorConcealment failed %x", error);
475         }
476
477
478         error=OMX_GetHandle(&omx_vid_sched,VPE_OMX_VIDEO_SCHED,NULL,&callbacks);
479         if (error!=OMX_ErrorNone){
480                 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX video scheduler failed %x", error);
481                 DeAllocateCodecsOMX();
482                 return 0;
483         }
484
485
486
487         error=OMX_GetParameter(omx_vid_sched,OMX_IndexParamVideoInit,&p_param);
488         if (error!=OMX_ErrorNone){
489                 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX video scheduler OMX_GetParameter failed %x", error);
490                 DeAllocateCodecsOMX();
491                 return 0;
492         }
493         omx_shed_input_port=p_param.nStartPortNumber;
494         omx_shed_output_port=p_param.nStartPortNumber+1;
495
496
497         error=OMX_GetParameter(omx_vid_sched,OMX_IndexParamOtherInit,&p_param);
498         if (error!=OMX_ErrorNone){
499                 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX video scheduler OMX_GetParameter failed %x", error);
500                 DeAllocateCodecsOMX();
501                 return 0;
502         }
503         omx_shed_clock_port=p_param.nStartPortNumber;
504
505
506         if (!DisablePort(omx_vid_sched,omx_shed_input_port) || !DisablePort(omx_vid_sched,omx_shed_output_port)
507                         || !DisablePort(omx_vid_sched,omx_shed_clock_port)) {
508                 Log::getInstance()->log("Video", Log::DEBUG, "Disable Ports OMX video shed failed");
509                 return 0;
510         }
511
512
513         error=OMX_GetHandle(&omx_vid_rend,VPE_OMX_VIDEO_REND,NULL,&callbacks);
514         if (error!=OMX_ErrorNone){
515                 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX video rend failed %x", error);
516                 DeAllocateCodecsOMX();
517                 return 0;
518         }
519
520         error=OMX_GetParameter(omx_vid_rend,OMX_IndexParamVideoInit,&p_param);
521         if (error!=OMX_ErrorNone){
522                 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX video rend OMX_GetParameter failed %x", error);
523                 DeAllocateCodecsOMX();
524                 return 0;
525         }
526         omx_rend_input_port=p_param.nStartPortNumber;
527         //omx_rend_output_port=p_param.nStartPortNumber+1;
528
529
530         if (!DisablePort(omx_vid_rend,omx_rend_input_port) /*|| !DisablePort(omx_vid_rend,omx_rend_output_port)*/
531                                 ) {
532                         Log::getInstance()->log("Video", Log::DEBUG, "Disable Ports OMX video rend failed");
533                         return 0;
534         }
535
536         //Setuo chain
537
538
539
540
541
542
543
544
545 /*      error=OMX_SendCommand(omx_vid_dec,OMX_CommandStateSet,OMX_StateIdle,0);
546         if (error!=OMX_ErrorNone){
547                 Log::getInstance()->log("Video", Log::DEBUG, "vid_dec Send Command to OMX State Idle %x", error);
548                 return 0;
549         }*/
550
551
552
553
554         OMX_VIDEO_PARAM_PORTFORMATTYPE ft_type;
555         memset(&ft_type,0,sizeof(ft_type));
556         ft_type.nSize=sizeof(ft_type);
557         ft_type.nVersion.nVersion=OMX_VERSION;
558
559         ft_type.nPortIndex=omx_codec_input_port;
560         if (h264) {
561                 ft_type.eCompressionFormat=OMX_VIDEO_CodingAVC;
562         } else {
563                 ft_type.eCompressionFormat=OMX_VIDEO_CodingMPEG2;
564         }
565
566         Demuxer* demux=Demuxer::getInstance();
567
568     ft_type.xFramerate=0;//25*(1<<16);//demux->getFrameRate()*(1<<16);
569     Log::getInstance()->log("Video", Log::DEBUG, "Framerate: %d",demux->getFrameRate());
570         error=OMX_SetParameter(omx_vid_dec,OMX_IndexParamVideoPortFormat,&ft_type);
571         if (error!=OMX_ErrorNone){
572                 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX_IndexParamVideoPortFormat failed %x", error);
573                 return 0;
574         }
575
576
577         if (!ChangeComponentState(omx_vid_dec,OMX_StateIdle)) {
578                 Log::getInstance()->log("Video", Log::DEBUG, "vid_dec ChangeComponentState");
579                 return 0;
580         }
581
582
583         if (!PrepareInputBufsOMX()) {
584                 return 0;
585         }
586
587
588         if (!ChangeComponentState(omx_clock,OMX_StateIdle)) {
589                 Log::getInstance()->log("Video", Log::DEBUG, "omx_clock ChangeComponentState Idle");
590                 return 0;
591         }
592
593         error=OMX_SetupTunnel(omx_clock,omx_clock_output_port,omx_vid_sched,omx_shed_clock_port);
594         if (error!=OMX_ErrorNone){
595                 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);
596                 DeAllocateCodecsOMX();
597                 return 0;
598         }
599
600         if (!EnablePort(omx_clock,omx_clock_output_port,false) || !EnablePort(omx_vid_sched,omx_shed_clock_port,false)
601                                         ) {
602                 Log::getInstance()->log("Video", Log::DEBUG, "Enable Ports OMX clock shed failed");
603                 return 0;
604         }
605
606         if ( !CommandFinished(omx_vid_sched,OMX_CommandPortEnable,omx_shed_clock_port)) {
607                 return 0;
608         }
609
610         error=OMX_SetupTunnel(omx_vid_dec,omx_codec_output_port,omx_vid_sched,omx_shed_input_port);
611         if (error!=OMX_ErrorNone){
612                 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel dec to sched failed %x", error);
613                 DeAllocateCodecsOMX();
614                 return 0;
615         }
616
617
618
619         if (!EnablePort(omx_vid_dec,omx_codec_output_port,false) || !EnablePort(omx_vid_sched,omx_shed_input_port,false)
620                                                 ) {
621                 Log::getInstance()->log("Video", Log::DEBUG, "Enable Ports OMX codec shed failed");
622                 return 0;
623         }
624
625         if ( !CommandFinished(omx_vid_sched,OMX_CommandPortEnable,omx_shed_input_port)) {
626                 return 0;
627         }
628         if (!ChangeComponentState(omx_vid_sched,OMX_StateIdle)) {
629                                 Log::getInstance()->log("Video", Log::DEBUG, "vid_sched idle ChangeComponentState");
630                                 return 0;
631         }
632         if (!CommandFinished(omx_clock,OMX_CommandPortEnable,omx_clock_output_port)
633                         ||!CommandFinished(omx_vid_dec,OMX_CommandPortEnable,omx_codec_output_port)){
634                 return 0;
635         }
636
637         if (!ChangeComponentState(omx_vid_dec,OMX_StateExecuting)) {
638                         Log::getInstance()->log("Video", Log::DEBUG, "omx_vid_dec ChangeComponentState Execute");
639                         return 0;
640         }
641
642         error=OMX_SetupTunnel(omx_vid_sched,omx_shed_output_port,omx_vid_rend,omx_rend_input_port);
643         if (error!=OMX_ErrorNone){
644                 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel  sched to rend failed %x", error);
645                 DeAllocateCodecsOMX();
646                 return 0;
647         }
648
649         if (!EnablePort(omx_vid_sched,omx_shed_output_port,false) || !EnablePort(omx_vid_rend,omx_rend_input_port,false)
650                                                         ) {
651                 Log::getInstance()->log("Video", Log::DEBUG, "Enable Ports OMX  shed rend failed");
652                 return 0;
653         }
654
655         if (!CommandFinished(omx_vid_sched,OMX_CommandPortEnable,omx_shed_output_port)
656                                         || !CommandFinished(omx_vid_rend,OMX_CommandPortEnable,omx_rend_input_port)) {
657                         return 0;
658         }
659
660         if (!ChangeComponentState(omx_vid_rend,OMX_StateIdle)) {
661                 Log::getInstance()->log("Video", Log::DEBUG, "vid_rend ChangeComponentState");
662                 return 0;
663         }
664
665
666         if (!ChangeComponentState(omx_vid_sched,OMX_StateExecuting)) {
667                 Log::getInstance()->log("Video", Log::DEBUG, "omx_vid_sched ChangeComponentState Execute");
668                 return 0;
669         }
670
671         if (!ChangeComponentState(omx_vid_rend,OMX_StateExecuting)) {
672                 Log::getInstance()->log("Video", Log::DEBUG, "omx_vid_rend ChangeComponentState Execute");
673                 return 0;
674         }
675
676         //raspbi specifif
677         OMX_CONFIG_DISPLAYREGIONTYPE dispconf;
678         memset(&dispconf,0,sizeof(dispconf));
679         dispconf.nSize=sizeof(dispconf);
680         dispconf.nVersion.nVersion=OMX_VERSION;
681
682         dispconf.nPortIndex=omx_rend_input_port;
683
684         dispconf.set=OMX_DISPLAY_SET_LAYER ;
685         dispconf.layer=1;
686         error=OMX_SetParameter(omx_vid_rend,OMX_IndexConfigDisplayRegion,&dispconf);
687         if (error!=OMX_ErrorNone){
688                 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX_IndexConfigDisplayRegion failed %x", error);
689                 return 0;
690         }
691
692 /*      dispconf.set=OMX_DISPLAY_SET_FULLSCREEN ;
693         dispconf.fullscreen=OMX_FALSE;
694         error=OMX_SetParameter(omx_vid_rend,OMX_IndexConfigDisplayRegion,&dispconf);
695         if (error!=OMX_ErrorNone){
696                 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX_IndexConfigDisplayRegion failed %x", error);
697                 return 0;
698         }
699
700         dispconf.set=OMX_DISPLAY_SET_DEST_RECT;
701         dispconf.dest_rect.x_offset=100;
702         dispconf.dest_rect.y_offset=100;
703         dispconf.dest_rect.width=640;
704         dispconf.dest_rect.height=480;
705         error=OMX_SetParameter(omx_vid_rend,OMX_IndexConfigDisplayRegion,&dispconf);
706         if (error!=OMX_ErrorNone){
707                 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX_IndexConfigDisplayRegion failed %x", error);
708                 return 0;
709         }*/
710
711
712
713
714         if (!ChangeComponentState(omx_clock,OMX_StateExecuting)) {
715                         Log::getInstance()->log("Video", Log::DEBUG, "omx_clock ChangeComponentState Exccute");
716                         return 0;
717         }
718
719         omx_running=true;
720
721         return 1;
722 }
723
724
725 int VideoVPEOGL::ChangeComponentState(OMX_HANDLETYPE handle,OMX_STATETYPE type)
726 {
727         OMX_ERRORTYPE error;
728         error=OMX_SendCommand(handle,OMX_CommandStateSet,type,0);
729         if (error!=OMX_ErrorNone){
730                 Log::getInstance()->log("Video", Log::DEBUG, "handle %x Send Command to OMX State %x %x",handle,type, error);
731                 return 0;
732         }
733
734         if (!CommandFinished(handle,OMX_CommandStateSet,type)) {
735                 return 0;
736         }
737
738         return 1;
739 }
740
741
742 int VideoVPEOGL::EnablePort(OMX_HANDLETYPE handle,OMX_U32 port,bool wait)
743 {
744         OMX_ERRORTYPE error;
745         error=OMX_SendCommand(handle,OMX_CommandPortEnable,port,0);
746         if (error!=OMX_ErrorNone){
747                 Log::getInstance()->log("Video", Log::DEBUG, "handle %x Send Command to enable port %x %x",handle,port, error);
748                 return 0;
749         }
750
751         if (!wait) return 1;
752         if (!CommandFinished(handle,OMX_CommandPortEnable,port)) {
753                 return 0;
754         }
755
756         return 1;
757 }
758
759
760 int VideoVPEOGL::DisablePort(OMX_HANDLETYPE handle,OMX_U32 port,bool wait)
761 {
762         OMX_ERRORTYPE error;
763         error=OMX_SendCommand(handle,OMX_CommandPortDisable,port,0);
764         if (error!=OMX_ErrorNone){
765                 Log::getInstance()->log("Video", Log::DEBUG, "handle %x Send Command to disable port %x %x",handle,port, error);
766                 return 0;
767         }
768
769         if (!wait) return 1;
770         if (!CommandFinished(handle,OMX_CommandPortDisable,port)) {
771                 return 0;
772         }
773
774
775         return 1;
776 }
777
778
779
780
781 int VideoVPEOGL::CommandFinished(OMX_HANDLETYPE handle,OMX_U32 command,OMX_U32 data2)
782 {
783         int i=0;
784         while (i<1000) {
785                 omx_event_mutex.Lock();
786                 list<VPE_OMX_EVENT>::iterator itty=omx_events.begin();
787                 while (itty!=omx_events.end()) {
788
789                         VPE_OMX_EVENT current=*itty;
790                         if (current.handle==handle) { //this is ours
791                                 if (current.event_type==OMX_EventError) {
792                                         omx_events.erase(itty);
793                                         omx_event_mutex.Unlock();
794                                         return 0;
795
796                                 } else if (current.event_type==OMX_EventCmdComplete && current.data1==command && current.data2==data2) {
797                                         omx_events.erase(itty);
798                                         omx_event_mutex.Unlock();
799                                         return 1;
800                                 }
801                         }
802                         itty++;
803
804                 }
805                 omx_event_mutex.Unlock();
806                 MILLISLEEP(2);
807                 i++;
808
809         }
810         Log::getInstance()->log("Video", Log::DEBUG, "CommandFinished waited too long %x %x %x",handle,command, data2);
811         return 0;
812
813 }
814
815
816
817
818
819 int VideoVPEOGL::PrepareInputBufsOMX()
820 {
821         OMX_ERRORTYPE error;
822         OMX_PARAM_PORTDEFINITIONTYPE port_def_type;
823         memset(&port_def_type,0,sizeof(port_def_type));
824         port_def_type.nSize=sizeof(port_def_type);
825         port_def_type.nVersion.nVersion=OMX_VERSION;
826         port_def_type.nPortIndex=omx_codec_input_port;
827
828         error=OMX_GetParameter(omx_vid_dec,OMX_IndexParamPortDefinition, &port_def_type);
829
830         if (error!=OMX_ErrorNone){
831                         Log::getInstance()->log("Video", Log::DEBUG, "Get OMX OMX_IndexParamPortDefinition failed %x", error);
832         }
833         Log::getInstance()->log("Video", Log::DEBUG, "Port para %d %d %d %d %d %d %d", port_def_type.nBufferCountActual,
834                         port_def_type.nBufferCountMin,port_def_type.nBufferSize,port_def_type.bEnabled,port_def_type.bPopulated,
835                         port_def_type.bBuffersContiguous,port_def_type.nBufferAlignment);
836         Log::getInstance()->log("Video", Log::DEBUG, "OMX minimum buffer num %d", port_def_type.nBufferCountMin);
837
838         port_def_type.nBufferCountActual=60;
839
840         error=OMX_SetParameter(omx_vid_dec,OMX_IndexParamPortDefinition, &port_def_type);
841
842         if (error!=OMX_ErrorNone){
843                         Log::getInstance()->log("Video", Log::DEBUG, "Set OMX OMX_IndexParamPortDefinition failed %x", error);
844         }
845
846
847         Log::getInstance()->log("Video", Log::DEBUG, "PrepareInputBufsOMX mark1");
848         error=OMX_SendCommand(omx_vid_dec,OMX_CommandPortEnable,omx_codec_input_port,0);
849         if (error!=OMX_ErrorNone){
850                 Log::getInstance()->log("Video", Log::DEBUG, "Prepare Input bufs Send Command to enable port %x", error);
851                 return 0;
852         }
853         Log::getInstance()->log("Video", Log::DEBUG, "PrepareInputBufsOMX mark2");
854
855
856
857         Log::getInstance()->log("Video", Log::DEBUG, "PrepareInputBufsOMX mark2a");
858
859         input_bufs_omx_mutex.Lock();
860         for (unsigned int i=0; i< port_def_type.nBufferCountActual;i++) {
861
862         //      unsigned char* new_buffer_data=(unsigned char*)malloc(port_def_type.nBufferSize);
863                 OMX_BUFFERHEADERTYPE *buf_head=NULL;
864         /*      error=OMX_UseBuffer(omx_vid_dec,&buf_head,omx_codec_input_port,NULL,port_def_type.nBufferSize,new_buffer_data);
865                 if (error!=OMX_ErrorNone){
866                         Log::getInstance()->log("Video", Log::DEBUG, "Use OMX_UseBuffer failed %x", error);
867                         input_bufs_omx_mutex.Unlock();
868                         return 0;
869                 }*/
870                 error=OMX_AllocateBuffer(omx_vid_dec,&buf_head,omx_codec_input_port,NULL,port_def_type.nBufferSize);
871                 if (error!=OMX_ErrorNone){
872                         Log::getInstance()->log("Video", Log::DEBUG, "Use OMX_AllocateBuffer failed %x", error);
873                                 input_bufs_omx_mutex.Unlock();
874                         return 0;
875                 }
876                 input_bufs_omx_all.push_back(buf_head);
877                 input_bufs_omx_free.push_back(buf_head);
878         }
879         omx_first_frame=true;
880
881         firstsynched=false;
882         cur_input_buf_omx=NULL;
883         input_bufs_omx_mutex.Unlock();
884
885
886         Log::getInstance()->log("Video", Log::DEBUG, "PrepareInputBufsOMX mark3");
887         if (!CommandFinished(omx_vid_dec,OMX_CommandPortEnable,omx_codec_input_port)) {
888                 return 0;
889         }
890         Log::getInstance()->log("Video", Log::DEBUG, "PrepareInputBufsOMX mark4");
891
892         return 1;
893 }
894
895 int VideoVPEOGL::DestroyInputBufsOMX()
896 {
897         OMX_ERRORTYPE error;
898
899         cur_input_buf_omx=NULL;
900         input_bufs_omx_mutex.Lock();
901         for (int i=0; i< input_bufs_omx_all.size();i++) {
902         //      free(input_bufs_omx_all[i]->pBuffer);
903         //      input_bufs_omx_all[i]->pBuffer=NULL;
904                 error=OMX_FreeBuffer(omx_vid_dec,omx_codec_input_port,input_bufs_omx_all[i]);
905                 if (error!=OMX_ErrorNone){
906                         Log::getInstance()->log("Video", Log::DEBUG, "Use OMX_FreeBuffer failed %x", error);
907                         input_bufs_omx_mutex.Unlock();
908                         return 0;
909                 }
910         }
911         input_bufs_omx_all.clear();
912         input_bufs_omx_free.clear();
913         input_bufs_omx_mutex.Unlock();
914
915 }
916
917
918
919
920 int VideoVPEOGL::DeAllocateCodecsOMX()
921 {
922         OMX_ERRORTYPE error;
923         omx_running=false;
924         if (omx_vid_dec) {
925                 // first flush all buffers
926
927                 error=OMX_SendCommand(omx_vid_dec,OMX_CommandFlush, omx_codec_output_port, NULL);
928                 if (error!=OMX_ErrorNone){
929                                 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Flush codec out failed %x", error);
930
931                 }
932
933                 error=OMX_SendCommand(omx_vid_sched,OMX_CommandFlush, omx_shed_input_port, NULL);
934                 if (error!=OMX_ErrorNone){
935                         Log::getInstance()->log("Video", Log::DEBUG, "OMX_Flush shed in failed %x", error);
936
937                 }
938
939                 if (!CommandFinished(omx_vid_dec,OMX_CommandFlush,omx_codec_output_port) ||
940                                 !CommandFinished(omx_vid_sched,OMX_CommandFlush,omx_shed_input_port)) {
941                         Log::getInstance()->log("Video", Log::DEBUG, "flush cmd codec shed failed");
942                 }
943
944                 error=OMX_SendCommand(omx_clock,OMX_CommandFlush, omx_clock_output_port, NULL);
945                 if (error!=OMX_ErrorNone){
946                         Log::getInstance()->log("Video", Log::DEBUG, "OMX_Flush clock out failed %x", error);
947
948                 }
949
950                 error=OMX_SendCommand(omx_vid_sched,OMX_CommandFlush, omx_shed_clock_port, NULL);
951                 if (error!=OMX_ErrorNone){
952                         Log::getInstance()->log("Video", Log::DEBUG, "OMX_Flush shed clock failed %x", error);
953
954                 }
955
956                 if (!CommandFinished(omx_clock,OMX_CommandFlush,omx_clock_output_port) ||
957                         !CommandFinished(omx_vid_sched,OMX_CommandFlush,omx_shed_clock_port)) {
958                                 Log::getInstance()->log("Video", Log::DEBUG, "flush cmd clock shed failed");
959                 }
960
961                 error=OMX_SendCommand(omx_vid_sched,OMX_CommandFlush, omx_shed_output_port, NULL);
962                 if (error!=OMX_ErrorNone) {
963                         Log::getInstance()->log("Video", Log::DEBUG, "OMX_Flush shed out failed %x", error);
964
965                 }
966
967                 error=OMX_SendCommand(omx_vid_rend,OMX_CommandFlush, omx_rend_input_port, NULL);
968                 if (error!=OMX_ErrorNone) {
969                         Log::getInstance()->log("Video", Log::DEBUG, "OMX_Flush rend in failed %x", error);
970
971                 }
972
973                 if (!CommandFinished(omx_vid_sched,OMX_CommandFlush,omx_shed_output_port) ||
974                                 !CommandFinished(omx_vid_rend,OMX_CommandFlush,omx_rend_input_port)) {
975                         Log::getInstance()->log("Video", Log::DEBUG, "flush cmd shed rend failed");
976                 }
977
978
979
980
981                 error=OMX_SendCommand(omx_vid_dec,OMX_CommandFlush, omx_codec_input_port, NULL);
982                 if (error!=OMX_ErrorNone){
983                         Log::getInstance()->log("Video", Log::DEBUG, "OMX_Flush codec out failed %x", error);
984
985                 }
986
987
988                 if (!CommandFinished(omx_vid_dec,OMX_CommandFlush,omx_codec_input_port)) {
989                         Log::getInstance()->log("Video", Log::DEBUG, "flush cmd codec input failed");
990                 }
991
992                 DestroyInputBufsOMX();
993
994                 //todo flushing
995                 if (!DisablePort(omx_vid_rend,omx_rend_input_port,true)) {
996                         Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 1");
997                 }
998                 if (!DisablePort(omx_vid_sched,omx_shed_output_port,true)) {
999                         Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 2 ");
1000                 }
1001
1002                 error=OMX_SetupTunnel(omx_vid_rend,omx_rend_input_port,NULL,NULL);
1003                 if (error!=OMX_ErrorNone){
1004                         Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1005
1006                 }
1007
1008                 error=OMX_SetupTunnel(omx_vid_sched,omx_shed_output_port,NULL,NULL);
1009                 if (error!=OMX_ErrorNone){
1010                         Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1011
1012                 }
1013
1014                 if (!DisablePort(omx_vid_sched,omx_shed_input_port,true)) {
1015                         Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 3");
1016                 }
1017
1018                 if (!DisablePort(omx_vid_sched,omx_shed_clock_port,true)) {
1019                         Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 4");
1020                 }
1021
1022                 if (!DisablePort(omx_clock,omx_clock_output_port,true)) {
1023                         Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 5");
1024                 }
1025                 if (!DisablePort(omx_vid_dec,omx_codec_output_port,true)) {
1026                         Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 6");
1027                 }
1028
1029
1030
1031                 if (!DisablePort(omx_vid_dec,omx_codec_input_port,true)) {
1032                         Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 7");
1033                 }
1034
1035
1036                 error=OMX_SetupTunnel(omx_vid_sched,omx_shed_input_port,NULL,NULL);
1037                 if (error!=OMX_ErrorNone){
1038                         Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1039
1040                 }
1041                 error=OMX_SetupTunnel(omx_vid_sched,omx_shed_clock_port,NULL,NULL);
1042                 if (error!=OMX_ErrorNone){
1043                         Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1044
1045                 }
1046
1047                 error=OMX_SetupTunnel(omx_clock,omx_clock_output_port,NULL,NULL);
1048                 if (error!=OMX_ErrorNone){
1049                         Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1050
1051                 }
1052
1053                 error=OMX_SetupTunnel(omx_vid_dec,omx_codec_output_port,NULL,NULL);
1054                 if (error!=OMX_ErrorNone){
1055                         Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1056
1057                 }
1058
1059
1060
1061
1062
1063                 error=OMX_FreeHandle(omx_vid_dec);
1064                 error=OMX_FreeHandle(omx_vid_sched);
1065                 error=OMX_FreeHandle(omx_vid_rend);
1066                 error=OMX_FreeHandle(omx_clock);
1067                 omx_vid_dec=NULL;
1068                 if (error!=OMX_ErrorNone) {
1069                         Log::getInstance()->log("Video", Log::DEBUG, "FreeHandle failed %d", error);
1070                 }
1071         }
1072
1073         return 1;
1074 }
1075
1076 #endif
1077
1078 int VideoVPEOGL::stop()
1079 {
1080   if (!initted) return 0;
1081
1082 #ifdef VPE_OMX_SUPPORT
1083   //Check if ffmpeg mode
1084   DeAllocateCodecsOMX();
1085 #endif
1086
1087   
1088
1089 //  if (ioctl(fdVideo, AV_SET_VID_STOP, 0) != 0) return 0;
1090   return 1;
1091 }
1092
1093 int VideoVPEOGL::reset()
1094 {
1095   if (!initted) return 0;
1096  
1097 //  if (ioctl(fdVideo, AV_SET_VID_RESET, 0x11) != 0) return 0;
1098   return 1;
1099 }
1100
1101 int VideoVPEOGL::pause()
1102 {
1103   if (!initted) return 0;
1104
1105 //  if (ioctl(fdVideo, AV_SET_VID_PAUSE, 0) != 0) return 0;
1106   return 1;
1107 }
1108
1109 int VideoVPEOGL::unPause() // FIXME get rid - same as play!!
1110 {
1111   if (!initted) return 0;
1112 //  if (ioctl(fdVideo, AV_SET_VID_PLAY, 0) != 0) return 0;
1113   return 1;
1114 }
1115
1116 int VideoVPEOGL::fastForward()
1117 {
1118   if (!initted) return 0;
1119
1120 //  if (ioctl(fdVideo, AV_SET_VID_FFWD, 1) != 0) return 0;
1121   return 1;
1122 }
1123
1124 int VideoVPEOGL::unFastForward()
1125 {
1126   if (!initted) return 0;
1127
1128 //  if (ioctl(fdVideo, AV_SET_VID_RESET, 0x11) != 0) return 0; // don't need this.
1129
1130  //// if (ioctl(fdVideo, AV_SET_VID_PLAY, 0) != 0) return 0;
1131   return 1;
1132 }
1133
1134 int VideoVPEOGL::attachFrameBuffer()
1135 {
1136   if (!initted) return 0;
1137
1138 //  if (ioctl(fdVideo, AV_SET_VID_FB, 0) != 0) return 0;
1139   return 1;
1140 }
1141
1142 int VideoVPEOGL::blank(void)
1143 {
1144 //  if (ioctl(fdVideo, AV_SET_VID_FB, 1) != 0) return 0;
1145 //  if (ioctl(fdVideo, AV_SET_VID_FB, 0) != 0) return 0;
1146   return 1;
1147 }
1148
1149 ULLONG VideoVPEOGL::getCurrentTimestamp()
1150 {
1151 /*  sync_data_t timestamps;
1152   if (ioctl(fdVideo, AV_GET_VID_TIMESTAMPS, &timestamps) == 0)
1153   {
1154     // FIXME are these the right way around?
1155
1156     timestamps.stc = (timestamps.stc >> 31 ) | (timestamps.stc & 1);
1157     timestamps.pts = (timestamps.pts >> 31 ) | (timestamps.pts & 1);
1158
1159     return timestamps.stc;
1160   }
1161   else
1162   {
1163     return 0;
1164   }*/
1165   return 0;
1166 }
1167
1168 ULONG VideoVPEOGL::timecodeToFrameNumber(ULLONG timecode)
1169 {
1170   if (format == PAL) return (ULONG)(((double)timecode / (double)90000) * (double)25);
1171   else               return (ULONG)(((double)timecode / (double)90000) * (double)30);
1172 }
1173
1174 #ifdef DEV
1175 int VideoVPEOGL::test()
1176 {
1177   return 0;
1178
1179 //  ULLONG stc = 0;
1180 //  return ioctl(fdVideo, AV_SET_VID_STC, &stc);
1181 /*
1182  // reset();
1183   return 1;
1184 */
1185 }
1186
1187 int VideoVPEOGL::test2()
1188 {
1189   return 0;
1190 }
1191 #endif
1192
1193 void VideoVPEOGL::PrepareMediaSample(const MediaPacketList& mplist,UINT samplepos)
1194 {
1195   mediapacket = mplist.front();
1196 }
1197
1198 UINT VideoVPEOGL::DeliverMediaSample(UCHAR* buffer, UINT *samplepos)
1199 {
1200   DeliverMediaPacket(mediapacket, buffer, samplepos);
1201   if (*samplepos == mediapacket.length) {
1202     *samplepos = 0;
1203     return 1;
1204   }
1205   else return 0;
1206 }
1207
1208
1209
1210 UINT VideoVPEOGL::DeliverMediaPacket(MediaPacket packet,
1211                 const UCHAR* buffer,
1212                 UINT *samplepos)
1213 {
1214
1215         if (packet.type == MPTYPE_VIDEO_H264)
1216         {
1217                 h264=true;
1218         }
1219         else
1220         {
1221                 h264=false;
1222         }
1223         //Later add fail back code for ffmpeg
1224         /*if (!videoon) {
1225                 *samplepos+=packet.length;
1226                 return packet.length;
1227         }*/
1228
1229 #ifdef VPE_OMX_SUPPORT
1230         if (!omx_running) return 0; // if we are not runnig do not do this
1231
1232
1233         OMX_ERRORTYPE error;
1234
1235         OMX_PARAM_PORTDEFINITIONTYPE port_image;
1236         memset(&port_image,0,sizeof(port_image));
1237         port_image.nSize=sizeof(port_image);
1238         port_image.nVersion.nVersion=OMX_VERSION;
1239         port_image.nPortIndex =omx_codec_output_port;
1240         error=OMX_GetParameter(omx_vid_dec,OMX_IndexParamPortDefinition, &port_image);
1241         if (error!= OMX_ErrorNone){
1242                 Log::getInstance()->log("Video", Log::DEBUG, "OMX_GetParameter failed %x", error);
1243         }
1244         Log::getInstance()->log("Video", Log::DEBUG, "Image port %d %d", port_image.format.video.nFrameWidth , port_image.format.video.nFrameHeight);
1245
1246         /*First Check, if we have an audio sample*/
1247         if (iframemode) {
1248                 //samplepos=0;
1249                 MILLISLEEP(10);
1250                 return 0; //Not in iframe mode!
1251         }
1252
1253         UINT headerstrip=0;
1254         if (packet.disconti) {
1255                 firstsynched=false;
1256                 if (cur_input_buf_omx) {
1257                         OMX_ERRORTYPE error=OMX_EmptyThisBuffer(omx_vid_dec,cur_input_buf_omx);
1258                         if (error!=OMX_ErrorNone){
1259                                 Log::getInstance()->log("Video", Log::DEBUG, "OMX_EmptyThisBuffer failed %x", error);
1260                         }
1261                         cur_input_buf_omx=NULL;
1262                 }
1263         }
1264
1265         /*Inspect PES-Header */
1266
1267         OMX_STATETYPE temp_state;
1268         OMX_GetState(omx_vid_dec,&temp_state);
1269
1270         if (*samplepos==0) {//stripheader
1271                 headerstrip=buffer[packet.pos_buffer+8]+9/*is this right*/;
1272                 //headerstrip+=6; //h264
1273                 *samplepos+=headerstrip;
1274                 if ( packet.synched ) {
1275
1276                         if (cur_input_buf_omx) {
1277                                 OMX_ERRORTYPE error=OMX_EmptyThisBuffer(omx_vid_dec,cur_input_buf_omx);
1278                                 if (error!=OMX_ErrorNone){
1279                                         Log::getInstance()->log("Video", Log::DEBUG, "OMX_EmptyThisBuffer failed %x", error);
1280                                 }
1281
1282                                 cur_input_buf_omx=NULL;//write out old data
1283                         }
1284                 //      reftime1=packet.presentation_time;
1285                 //      reftime2=reftime1+1;
1286                         firstsynched=true;
1287                 } else {
1288                         if (!firstsynched) {//
1289                                 *samplepos=packet.length;//if we have not processed at least one
1290                                 return packet.length;//synched packet ignore it!
1291                         }
1292                 }
1293         }
1294
1295         if (!cur_input_buf_omx) {
1296                 input_bufs_omx_mutex.Lock();
1297                 if (input_bufs_omx_free.size()==0) {
1298                         input_bufs_omx_mutex.Unlock();
1299                         Log::getInstance()->log("Video", Log::DEBUG, "Deliver MediaPacket no free sample");
1300                         return 0; // we do not have a free media sample
1301
1302                 }
1303                 cur_input_buf_omx=input_bufs_omx_free.front();
1304                 cur_input_buf_omx->nFilledLen=0;
1305                 cur_input_buf_omx->nOffset=0;
1306                 cur_input_buf_omx->nTimeStamp=0;
1307                 input_bufs_omx_free.pop_front();
1308                 input_bufs_omx_mutex.Unlock();
1309         }
1310
1311
1312
1313
1314         if (cur_input_buf_omx->nFilledLen==0) {//will only be changed on first packet
1315                 /*if (packet.disconti) {
1316                         ms->SetDiscontinuity(TRUE);
1317                 } else {
1318                         ms->SetDiscontinuity(FALSE);
1319                 }*/
1320                 //if (packet.synched) {
1321
1322                         //lastreftimePTS=packet.pts;
1323                    if (omx_first_frame) { // TODO time
1324                            cur_input_buf_omx->nFlags=OMX_BUFFERFLAG_STARTTIME;
1325                            omx_first_frame=false;
1326                    } else
1327
1328                 //}
1329                 //else
1330                 //{
1331                         cur_input_buf_omx->nFlags=OMX_BUFFERFLAG_TIME_UNKNOWN;
1332
1333
1334                         //  ms->SetSyncPoint(TRUE);
1335                 //}
1336
1337         }
1338         unsigned int haveToCopy=packet.length-*samplepos;
1339
1340         while (haveToCopy> (cur_input_buf_omx->nAllocLen-cur_input_buf_omx->nFilledLen)) {
1341                 unsigned int cancopy=cur_input_buf_omx->nAllocLen-cur_input_buf_omx->nFilledLen;
1342                 memcpy(cur_input_buf_omx->pBuffer+cur_input_buf_omx->nFilledLen,buffer+packet.pos_buffer+*samplepos,cancopy);
1343                 haveToCopy-=cancopy;
1344                 cur_input_buf_omx->nFilledLen+=cancopy;
1345                 *samplepos+=cancopy;
1346                 // push old buffer out
1347
1348                 OMX_ERRORTYPE error=OMX_EmptyThisBuffer(omx_vid_dec,cur_input_buf_omx);
1349                 if (error!=OMX_ErrorNone){
1350                         Log::getInstance()->log("Video", Log::DEBUG, "OMX_EmptyThisBuffer failed %x", error);
1351                 }
1352                 // get5 new buffer
1353                 input_bufs_omx_mutex.Lock();
1354                 if (input_bufs_omx_free.size()==0) {
1355                         input_bufs_omx_mutex.Unlock();
1356                         //Log::getInstance()->log("Video", Log::DEBUG, "Deliver MediaPacket no free sample");
1357                         return *samplepos; // we do not have a free media sample
1358                 }
1359                 cur_input_buf_omx=input_bufs_omx_free.front();
1360                 cur_input_buf_omx->nFilledLen=0;
1361                 cur_input_buf_omx->nOffset=0;
1362                 cur_input_buf_omx->nTimeStamp=0;
1363                 input_bufs_omx_free.pop_front();
1364                 input_bufs_omx_mutex.Unlock();
1365
1366                 cur_input_buf_omx->nFlags=OMX_BUFFERFLAG_TIME_UNKNOWN;
1367
1368         }
1369         memcpy(cur_input_buf_omx->pBuffer+cur_input_buf_omx->nFilledLen,
1370                         buffer+packet.pos_buffer+*samplepos,haveToCopy);
1371         cur_input_buf_omx->nFilledLen+=haveToCopy;
1372
1373
1374
1375         *samplepos+=haveToCopy;
1376
1377         return *samplepos;
1378
1379 #else
1380
1381         *samplepos+=packet.length; //yet not implemented//bad idea
1382         return packet.length;
1383 #endif
1384 }
1385
1386
1387
1388
1389
1390
1391 void VideoVPEOGL::ResetTimeOffsets()
1392 {
1393 }
1394
1395 bool VideoVPEOGL::displayIFrame(const UCHAR* buffer, UINT length)
1396 {
1397   //write(fdVideo, buffer, length);
1398         if (!iframemode) EnterIframePlayback();
1399 //      WriteOutTS(buffer,length, h264?MPTYPE_VIDEO_H264 :MPTYPE_VIDEO_MPEG2 );
1400  
1401   lastpacketnum=-1;
1402   return true;
1403 }
1404
1405 int VideoVPEOGL::EnterIframePlayback()
1406 {
1407
1408         return 0;
1409 }
1410