]> git.vomp.tv Git - vompclient-marten.git/blob - videovpeogl.cc
ffmpeg softdecoding only Y (bw)
[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   lastpacketnum=-1;
35
36 #ifdef VPE_OMX_SUPPORT
37   omx_running=false;
38
39   omx_vid_dec=0;
40   cur_input_buf_omx=NULL;
41   omx_h264=omx_mpeg2=true;
42 #endif
43
44 #ifdef VPE_FFMPEG_SUPPORT
45   mpeg2codec_context_ff=NULL;
46   ffmpeg_running=false;
47   dec_frame_ff_uploading=NULL;
48   dec_frame_ff_decoding=NULL;
49 #endif
50   
51 }
52
53 VideoVPEOGL::~VideoVPEOGL()
54 {
55   instance = NULL;
56 }
57
58 int VideoVPEOGL::init(UCHAR tformat)
59 {
60   if (initted) return 0;
61   initted = 1;
62
63 //  if ((fdVideo = open("/dev/vdec_dev", O_WRONLY)) < 0) return 0;
64
65   if (!setFormat(tformat))           { shutdown(); return 0; }
66   if (!setConnection(COMPOSITERGB))  { shutdown(); return 0; }
67   if (!setAspectRatio(ASPECT4X3))    { shutdown(); return 0; }
68   if (!setMode(NORMAL))              { shutdown(); return 0; }
69   if (!setSource())                  { shutdown(); return 0; }
70   if (!attachFrameBuffer())          { shutdown(); return 0; }
71
72   setTVsize(ASPECT4X3);
73
74 /*  if (format == PAL) setLetterboxBorder("38");
75   else setLetterboxBorder("31");*/
76
77   /* new stuff */
78
79
80
81
82   //stop();
83
84
85
86   return 1;
87 }
88
89 int VideoVPEOGL::initUsingOSDObjects()
90 {
91         EGLDisplay i_egl_display;
92         EGLSurface i_egl_surface;
93         EGLContext i_egl_context;
94         EGLConfig i_egl_config;
95         OsdOpenGL *osd=(OsdOpenGL*)osd->getInstance();
96         osd->getEGLObjs(&i_egl_display,&i_egl_surface,&i_egl_context, &i_egl_config);
97         const EGLint attr_context[]={
98                         EGL_CONTEXT_CLIENT_VERSION,2,
99                 EGL_NONE
100         };
101
102         egl_display=i_egl_display;
103         egl_context=eglCreateContext(egl_display,i_egl_config,i_egl_context,attr_context);
104         if (egl_context==EGL_NO_CONTEXT) {
105                  Log::getInstance()->log("Video", Log::WARN, "Creating egl context failed! %d",eglGetError());
106                  return 0;
107         }
108         // We create a dummy surface here, in order to allow two contexts
109         const EGLint attr_pbuffer[]={
110                         EGL_WIDTH, 1, EGL_HEIGHT,1,
111                     EGL_NONE
112         };
113         egl_surface=eglCreatePbufferSurface(egl_display,i_egl_config,attr_pbuffer);
114         if (egl_surface==EGL_NO_SURFACE) {
115                  Log::getInstance()->log("Video", Log::WARN, "Creating egl pbuffer failed! %d",eglGetError());
116                  return 0;
117         }
118
119
120
121
122         //egl_surface=i_egl_surface;
123         //egl_context=i_egl_context;
124
125
126 #ifdef VPE_OMX_SUPPORT
127 // we are called before the audio
128         OMX_ERRORTYPE error;
129         error=OMX_Init();
130         if (error!=OMX_ErrorNone) {
131                 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX failed %x", error);
132                 return 0;
133         }
134
135         //our callbacks move to play?
136
137 #endif
138
139 #ifdef VPE_FFMPEG_SUPPORT
140
141         av_register_all();
142         mpeg2codec_ff=avcodec_find_decoder(CODEC_ID_MPEG2VIDEO);
143         //mpeg2codec_ff=avcodec_find_decoder(CODEC_ID_MPEG2VIDEO_XVMC);
144         if (mpeg2codec_ff==NULL) {
145                 Log::getInstance()->log("Video", Log::DEBUG, "Find ffmpeg mpeg2 codec failed");
146                 return 0;
147         }
148
149 #endif
150         threadStart();
151         return 1;
152 }
153
154
155 #ifdef VPE_OMX_SUPPORT
156
157 OMX_ERRORTYPE VideoVPEOGL::EventHandler_OMX(OMX_IN OMX_HANDLETYPE handle,OMX_IN OMX_PTR appdata,
158            OMX_IN OMX_EVENTTYPE event_type,OMX_IN OMX_U32 data1,
159            OMX_IN OMX_U32 data2,OMX_IN OMX_PTR event_data) {
160
161         Log::getInstance()->log("Video", Log::NOTICE, "eventHandler %x %x %x %x %x",handle,event_type,data1,data2,event_data);
162
163         struct VPE_OMX_EVENT  new_event;
164         new_event.handle=handle;
165         new_event.appdata=appdata;
166         new_event.event_type=event_type;
167         new_event.data1=data1;
168         new_event.data2=data2;
169         new_event.event_data=event_data;
170
171         VideoVPEOGL *video=(VideoVPEOGL *)getInstance();
172         video->AddOmxEvent(new_event);
173
174 /*      switch (event_type) {
175         case OMX_EventCmdComplete: {
176
177         } break;
178         }*/
179
180         return OMX_ErrorNone;
181
182 }
183
184 void VideoVPEOGL::AddOmxEvent(VPE_OMX_EVENT  new_event)
185 {
186         omx_event_mutex.Lock();
187     omx_events.push_back(new_event);
188         omx_event_mutex.Unlock();
189 }
190
191
192 OMX_ERRORTYPE VideoVPEOGL::EmptyBufferDone_OMX(OMX_IN OMX_HANDLETYPE hcomp,OMX_IN OMX_PTR appdata,OMX_IN OMX_BUFFERHEADERTYPE* buffer){
193
194         Log::getInstance()->log("Video", Log::NOTICE, "EmptyBufferDone");
195         VideoVPEOGL *video=(VideoVPEOGL *)getInstance();
196         video->ReturnEmptyOMXBuffer(buffer);
197         return OMX_ErrorNone;
198
199 }
200
201 void VideoVPEOGL::ReturnEmptyOMXBuffer(OMX_BUFFERHEADERTYPE* buffer){
202         input_bufs_omx_mutex.Lock();
203         Log::getInstance()->log("Video", Log::NOTICE, "ReturnEmptyOMXBuffer %d",input_bufs_omx_free.size());
204         input_bufs_omx_free.push_back(buffer);
205         Log::getInstance()->log("Video", Log::NOTICE, "ReturnEmptyOMXBuffer %d",input_bufs_omx_free.size());
206         input_bufs_omx_mutex.Unlock();
207 }
208
209  OMX_ERRORTYPE VideoVPEOGL::FillBufferDone_OMX(OMX_IN OMX_HANDLETYPE hcomp, OMX_IN OMX_PTR appdata,OMX_IN OMX_BUFFERHEADERTYPE* buffer) {
210          Log::getInstance()->log("Video", Log::NOTICE, "FillBufferDone");
211         return OMX_ErrorNone;
212 }
213
214 #endif
215
216 int VideoVPEOGL::shutdown()
217 {
218   if (!initted) return 0;
219   initted = 0;
220   threadCancel();
221
222   decoding_backend=0;
223 #ifdef VPE_OMX_SUPPORT
224   DeAllocateCodecsOMX();
225   OMX_Deinit();
226 #endif
227 #ifdef VPE_FFMPEG_SUPPORT
228   DeAllocateCodecsFFMPEG();
229 #endif
230   eglDestroyContext(egl_display,egl_context);
231 //  close(fdVideo);
232   return 1;
233 }
234
235 int VideoVPEOGL::AllocateYUVOglTexture(VPEOGLFrame* outframe,int width,int height,int stride)
236 {
237         Log::getInstance()->log("Video", Log::NOTICE, "Allocate ogl texture");
238         // Y
239         glGenTextures(1, &outframe->textures[0]);
240         glBindTexture(GL_TEXTURE_2D, outframe->textures[0]);
241         glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, stride, height, 0, GL_LUMINANCE,
242                                 GL_UNSIGNED_BYTE, NULL);
243         // U
244         glGenTextures(1, &outframe->textures[1]);
245         glBindTexture(GL_TEXTURE_2D, outframe->textures[1]);
246         glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, stride>>1, height>>1, 0, GL_LUMINANCE,
247                                 GL_UNSIGNED_BYTE, NULL);
248         // V
249         glGenTextures(1, &outframe->textures[2]);
250         glBindTexture(GL_TEXTURE_2D, outframe->textures[2]);
251         glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, stride>>1, height>>1, 0, GL_LUMINANCE,
252                         GL_UNSIGNED_BYTE, NULL);
253         outframe->height=height;
254         outframe->width=width;
255         outframe->stride=stride;
256         return 1;
257 }
258
259
260 VPEOGLFrame *VideoVPEOGL::getReadyOGLFrame(){
261         VPEOGLFrame *return_obj=NULL;
262         ogl_frame_mutex.Lock();
263         if (ready_ogl_frames.size()>0) {
264                 return_obj=ready_ogl_frames.front();
265                 ready_ogl_frames.pop_front();
266                 ogl_frame_outside=true;
267         }
268         ogl_frame_mutex.Unlock();
269         return return_obj;
270 }
271
272 void VideoVPEOGL::returnOGLFrame(VPEOGLFrame *frame)
273 {
274         ogl_frame_mutex.Lock();
275         if (frame) {
276                 ogl_frame_outside=false;
277                 free_ogl_frames.push_back(frame);
278         }
279         ogl_frame_mutex.Unlock();
280 }
281
282 void VideoVPEOGL::threadMethod()
283 {
284         if (eglMakeCurrent(egl_display, egl_surface, egl_surface, egl_context)== EGL_FALSE) {
285                 Log::getInstance()->log("Video", Log::WARN, "Making egl Current failed in thread %d",eglGetError());
286                 return;
287         }
288         while (1) {
289                 bool sleep=true;
290 #ifdef VPE_FFMPEG_SUPPORT
291                 dec_frame_ff_mutex.Lock();
292                 if (dec_frame_ff_upload_pending.size()>0) {
293                         dec_frame_ff_uploading=dec_frame_ff_upload_pending.front();
294                         dec_frame_ff_upload_pending.pop_front();
295                         if (dec_frame_ff_upload_pending.size()>0) sleep=false;
296                 }
297                 dec_frame_ff_mutex.Unlock();
298                 if (dec_frame_ff_uploading) {
299                         int width,height,pixfmt;
300                          //First get a free ogl image
301                         VPEOGLFrame* out_frame=NULL;
302                         while (!out_frame) {
303                                 ogl_frame_mutex.Lock();
304                                 if (all_ogl_frames.size()==0) {
305                                         ogl_frame_mutex.Unlock(); break;
306                                 }
307
308                                 if (free_ogl_frames.size()>0) {
309                                         width=ffwidth;
310                                         height=ffheight;
311                                         pixfmt=ffpixfmt;
312                                         out_frame=free_ogl_frames.front();
313                                         free_ogl_frames.pop_front();
314                                 } else MILLISLEEP(2);
315                                 ogl_frame_mutex.Unlock();
316                         }
317                         bool failed=false;
318                         if (out_frame) {
319                                 if (out_frame->textures[0]==0 || out_frame->width!=width ||
320                                                 out_frame->height!=height || out_frame->stride!=dec_frame_ff_uploading->linesize[0]) {
321                                         if (out_frame->textures[0]==0) {
322                                                 glDeleteTextures(1,&out_frame->textures[0]);
323                                                 out_frame->textures[0]=0;
324                                         }
325                                         if (out_frame->textures[1]==0) {
326                                                 glDeleteTextures(1,&out_frame->textures[1]);
327                                                 out_frame->textures[1]=0;
328                                         }
329                                         if (out_frame->textures[2]==0) {
330                                                 glDeleteTextures(1,&out_frame->textures[2]);
331                                                 out_frame->textures[2]=0;
332                                         }
333                                         if (!AllocateYUVOglTexture(out_frame,width,height,dec_frame_ff_uploading->linesize[0])) failed=true;
334                                 }
335                                 if (!failed) {
336                                         //up to now only YUV data, this is for reference only, since the pi is too slow.
337                                         glBindTexture(GL_TEXTURE_2D, out_frame->textures[0]);
338                                         glPixelStorei(GL_UNPACK_ALIGNMENT,1);
339
340                                         glTexSubImage2D(GL_TEXTURE_2D,0,0,0,
341                                                         dec_frame_ff_uploading->linesize[0],height,
342                                                         GL_LUMINANCE,GL_UNSIGNED_BYTE,
343                                                         dec_frame_ff_uploading->data[0]);
344
345
346                                         glBindTexture(GL_TEXTURE_2D, out_frame->textures[1]);
347                                         glPixelStorei(GL_UNPACK_ALIGNMENT,1);
348                                         glTexSubImage2D(GL_TEXTURE_2D,0,0,0,
349                                                         dec_frame_ff_uploading->linesize[1],height>>1,
350                                                         GL_LUMINANCE,GL_UNSIGNED_BYTE,
351                                                         dec_frame_ff_uploading->data[1]);
352
353                                         glBindTexture(GL_TEXTURE_2D, out_frame->textures[2]);
354                                         glPixelStorei(GL_UNPACK_ALIGNMENT,1);
355                                         glTexSubImage2D(GL_TEXTURE_2D,0,0,0,
356                                                         dec_frame_ff_uploading->linesize[2],height>>1,
357                                                         GL_LUMINANCE,GL_UNSIGNED_BYTE,
358                                                         dec_frame_ff_uploading->data[2]);
359                                         ogl_frame_mutex.Lock();
360                                         ready_ogl_frames.push_back(out_frame);
361                                         ogl_frame_mutex.Unlock();
362                                         ((OsdOpenGL*)Osd::getInstance())->AdviseAboutNewFrame(); //Tell him, that we have a frame waiting
363
364                                 }
365
366                                 dec_frame_ff_mutex.Lock();
367                                 dec_frame_ff_free.push_back(dec_frame_ff_uploading);
368                                 dec_frame_ff_uploading=NULL;
369                                 dec_frame_ff_mutex.Unlock();
370
371
372
373
374                         }
375
376
377
378
379                 }
380 #endif
381
382                 if (sleep) threadWaitForSignal();
383                 threadCheckExit();
384         }
385
386 }
387
388 void VideoVPEOGL::threadPostStopCleanup()
389 {
390         eglMakeCurrent(egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
391 #ifdef VPE_FFMPEG_SUPPORT
392         dec_frame_ff_uploading=NULL;
393 #endif
394 }
395
396
397
398
399 int VideoVPEOGL::setTVsize(UCHAR ttvsize)
400 {
401 /*  tvsize = ttvsize;
402
403   // Override the aspect ratio usage, temporarily use to set the video chip mode
404   if (!setAspectRatio(tvsize))       { shutdown(); return 0; }
405   close(fdVideo);
406   if ((fdVideo = open("/dev/vdec_dev", O_WRONLY)) < 0) return 0;
407   if (!setSource())                  { shutdown(); return 0; }
408   if (!attachFrameBuffer())          { shutdown(); return 0; }
409
410   // Reopening the fd causes the scart aspect line to go back to 4:3
411   // Set this again to the same as the tv screen size
412   if (!setAspectRatio(tvsize))       { shutdown(); return 0; }
413
414   // mode == LETTERBOX is invalid if the TV is widescreen
415   if (tvsize == ASPECT16X9) setMode(NORMAL);
416 */
417   return 1;
418 }
419
420 int VideoVPEOGL::setDefaultAspect()
421 {
422   return setAspectRatio(tvsize);
423 }
424
425
426
427 int VideoVPEOGL::setFormat(UCHAR tformat)
428 {
429   if (!initted) return 0;
430   if ((tformat != PAL) && (tformat != NTSC)) return 0;
431   format = tformat;
432
433 //  if (ioctl(fdVideo, AV_SET_VID_DISP_FMT, format) != 0) return 0;
434
435   if (format == NTSC)
436   {
437     screenWidth = 720;
438     screenHeight = 480;
439   }
440   if (format == PAL)
441   {
442     screenWidth = 720;
443     screenHeight = 576;
444   }
445
446   return 1;
447 }
448
449 int VideoVPEOGL::setConnection(UCHAR tconnection)
450 {
451   if (!initted) return 0;
452   if ((tconnection != COMPOSITERGB) && (tconnection != SVIDEO)) return 0;
453   connection = tconnection;
454
455 //  if (ioctl(fdVideo, AV_SET_VID_OUTPUT, connection) != 0) return 0;
456   return 1;
457 }
458
459 int VideoVPEOGL::setAspectRatio(UCHAR taspectRatio)
460 {
461   if (!initted) return 0;
462   if ((taspectRatio != ASPECT4X3) && (taspectRatio != ASPECT16X9)) return 0;
463   aspectRatio = taspectRatio;
464
465   Log::getInstance()->log("Video", Log::DEBUG, "Setting aspect to %i", aspectRatio);
466
467 //  if (ioctl(fdVideo, AV_SET_VID_RATIO, aspectRatio) != 0) return 0;
468   return 1;
469 }
470
471 int VideoVPEOGL::setMode(UCHAR tmode)
472 {
473   if (!initted) return 0;
474
475   if ((tmode == LETTERBOX) && (tvsize == ASPECT16X9)) return 0; // invalid mode
476
477   if ((tmode != NORMAL) && (tmode != LETTERBOX) && (tmode != UNKNOWN2) && (tmode != QUARTER) && (tmode != EIGHTH)
478       && (tmode != ZOOM) && (tmode != UNKNOWN6)) return 0;
479   mode = tmode;
480
481 //  if (ioctl(fdVideo, AV_SET_VID_MODE, mode) != 0) return 0;
482   return 1;
483 }
484
485 int VideoVPEOGL::signalOff()
486 {
487 //  if (ioctl(fdVideo, AV_SET_VID_DENC, 0) != 0) return 0;
488   return 1;
489 }
490
491 int VideoVPEOGL::signalOn()
492 {
493 //  if (ioctl(fdVideo, AV_SET_VID_DENC, 1) != 0) return 0;
494   return 1;
495 }
496
497 int VideoVPEOGL::setSource()
498 {
499   if (!initted) return 0;
500
501   // What does this do...
502 //  if (ioctl(fdVideo, AV_SET_VID_SRC, 1) != 0) return 0;
503   return 1;
504 }
505
506 int VideoVPEOGL::setPosition(int x, int y)
507 {
508   if (!initted) return 0;
509
510 //  vid_pos_regs_t pos_d;
511 //  pos_d.x = x;
512 //  pos_d.y = y;
513
514 /*  vid_pos_regs_t pos_d;
515
516   memset(&pos_d, 0, sizeof(pos_d));
517
518   pos_d.dest.y = y;
519   pos_d.dest.x = x;
520 /*
521 typedef struct {
522   int w;
523   int h;
524   int scale;
525   int x1;
526   int y;
527   int x;
528   int y2;
529   int x3;
530   int y3;
531   int x4;
532   int y4;
533 } vid_pos_regs_t;
534 */
535
536 /*
537   pos_d.w = 100;
538   pos_d.h = 30;
539   pos_d.scale = 2;
540   pos_d.x1 = 0;
541   pos_d.y = 100;            // Top left X
542   pos_d.x = 50;            // Top left Y
543   pos_d.y2 = 30;
544   pos_d.x3 = 60;
545   pos_d.y3 = 90;
546   pos_d.x4 = 120;
547   pos_d.y4 = 150;
548 */
549
550 //  if (ioctl(fdVideo, AV_SET_VID_POSITION, &pos_d) != 0) return 0;
551   return 1;
552 }
553
554 int VideoVPEOGL::sync()
555 {
556   if (!initted) return 0;
557
558 //  if (ioctl(fdVideo, AV_SET_VID_SYNC, 2) != 0) return 0;
559   return 1;
560 }
561
562
563
564
565 int VideoVPEOGL::play()
566 {
567   if (!initted) return 0;
568   decoding_backend=0;
569 #ifdef VPE_OMX_SUPPORT
570   bool doomx=true;
571   if (h264) {
572           if (!omx_h264) doomx=false;
573   } else {
574           if (!omx_mpeg2) doomx=false;
575   }
576   if (doomx) {
577           if (AllocateCodecsOMX()) {
578                   decoding_backend=VPE_DECODER_OMX;
579                   return 1;
580                   // Otherwise fall back to ffmpeg
581           } else {
582                   if (h264) {
583                           omx_h264=false;
584                           Log::getInstance()->log("Video", Log::NOTICE, "Allocate Codecs OMX failed assume h264 unsupported");
585                   } else {
586                           omx_mpeg2=false;
587                           Log::getInstance()->log("Video", Log::NOTICE, "Allocate Codecs OMX failed assume mpeg2 unsupported");
588                   }
589           }
590   }
591 #endif
592 #ifdef VPE_FFMPEG_SUPPORT
593   if (AllocateCodecsFFMPEG()) {
594           decoding_backend=VPE_DECODER_FFMPEG;
595           return 1;
596                   // Otherwise fall back to ffmpeg
597   }
598 #endif
599   return 0;
600
601
602
603 }
604
605 #ifdef VPE_OMX_SUPPORT
606 int VideoVPEOGL::AllocateCodecsOMX()
607 {
608         OMX_ERRORTYPE error;
609         static OMX_CALLBACKTYPE callbacks= {&EventHandler_OMX,&EmptyBufferDone_OMX,&FillBufferDone_OMX};
610
611         Log::getInstance()->log("Video", Log::NOTICE, "Allocate Codecs OMX");
612         //Clock, move later to audio
613
614         omx_events.clear();
615
616         error=OMX_GetHandle(&omx_clock,VPE_OMX_CLOCK,NULL,&callbacks);
617
618
619         if (error!=OMX_ErrorNone){
620                 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX clock failed %x", error);
621                 DeAllocateCodecsOMX();
622                 return 0;
623         }
624
625
626
627         OMX_PORT_PARAM_TYPE p_param;
628         memset(&p_param,0,sizeof(p_param));
629         p_param.nSize=sizeof(p_param);
630         p_param.nVersion.nVersion=OMX_VERSION;
631         error=OMX_GetParameter(omx_clock,OMX_IndexParamOtherInit,&p_param);
632         if (error!=OMX_ErrorNone){
633                 Log::getInstance()->log("Video", Log::DEBUG, "Init clock OMX_GetParameter failed %x", error);
634                 DeAllocateCodecsOMX();
635                 return 0;
636         }
637         omx_clock_output_port=p_param.nStartPortNumber;
638
639         for (unsigned int i=0;i<p_param.nPorts;i++) {
640                 if (!DisablePort(omx_clock,p_param.nStartPortNumber+i) ) {
641                         Log::getInstance()->log("Video", Log::DEBUG, "Disable Ports OMX clock failed %d",i);
642                         DeAllocateCodecsOMX();
643                         return 0;
644                 }
645         }
646
647
648         OMX_TIME_CONFIG_CLOCKSTATETYPE clock_conf;
649         memset(&clock_conf,0,sizeof(clock_conf));
650         clock_conf.nSize=sizeof(clock_conf);
651         clock_conf.nVersion.nVersion=OMX_VERSION;
652         clock_conf.eState=OMX_TIME_ClockStateWaitingForStartTime;
653         clock_conf.nStartTime=0;
654         clock_conf.nOffset=0;
655         clock_conf.nWaitMask=OMX_CLOCKPORT0;
656         error=OMX_SetParameter(omx_clock,OMX_IndexConfigTimeClockState,&clock_conf);
657         if (error!=OMX_ErrorNone){
658                 Log::getInstance()->log("Video", Log::DEBUG, "Clock IndexConfigTimeClockState failed %x", error);
659                 DeAllocateCodecsOMX();
660                 return 0;
661         }
662
663
664
665         if (h264) {
666                 error=OMX_GetHandle(&omx_vid_dec,VPE_OMX_H264_DECODER,NULL,&callbacks);
667         } else {
668                 error=OMX_GetHandle(&omx_vid_dec,VPE_OMX_MPEG2_DECODER,NULL,&callbacks);
669         }
670
671         if (error!=OMX_ErrorNone){
672                 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX video decoder failed %x", error);
673                 DeAllocateCodecsOMX();
674                 return 0;
675         }
676
677
678
679         memset(&p_param,0,sizeof(p_param));
680         p_param.nSize=sizeof(p_param);
681         p_param.nVersion.nVersion=OMX_VERSION;
682         error=OMX_GetParameter(omx_vid_dec,OMX_IndexParamVideoInit,&p_param);
683         if (error!=OMX_ErrorNone){
684                 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX h264 decoder OMX_GetParameter failed %x", error);
685                 DeAllocateCodecsOMX();
686             return 0;
687         }
688         omx_codec_input_port=p_param.nStartPortNumber;
689         omx_codec_output_port=p_param.nStartPortNumber+1;
690
691         if (!DisablePort(omx_vid_dec,omx_codec_input_port) || !DisablePort(omx_vid_dec,omx_codec_output_port)) {
692                 Log::getInstance()->log("Video", Log::DEBUG, "Disable Ports OMX video decoder failed");
693                 DeAllocateCodecsOMX();
694                 return 0;
695         }
696
697
698         OMX_PARAM_BRCMVIDEODECODEERRORCONCEALMENTTYPE conceal;
699         memset(&conceal,0,sizeof(conceal));
700         conceal.nSize=sizeof(conceal);
701         conceal.nVersion.nVersion=OMX_VERSION;
702         conceal.bStartWithValidFrame=OMX_FALSE;
703
704         error=OMX_SetParameter(omx_vid_dec,OMX_IndexParamBrcmVideoDecodeErrorConcealment,&conceal);
705         if (error!=OMX_ErrorNone){
706                 Log::getInstance()->log("Video", Log::DEBUG, "OMX_IndexParamBrcmVideoDecodeErrorConcealment failed %x", error);
707                 DeAllocateCodecsOMX();
708                 return 0;
709         }
710
711
712         error=OMX_GetHandle(&omx_vid_sched,VPE_OMX_VIDEO_SCHED,NULL,&callbacks);
713         if (error!=OMX_ErrorNone){
714                 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX video scheduler failed %x", error);
715                 DeAllocateCodecsOMX();
716                 return 0;
717         }
718
719
720
721         error=OMX_GetParameter(omx_vid_sched,OMX_IndexParamVideoInit,&p_param);
722         if (error!=OMX_ErrorNone){
723                 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX video scheduler OMX_GetParameter failed %x", error);
724                 DeAllocateCodecsOMX();
725                 return 0;
726         }
727         omx_shed_input_port=p_param.nStartPortNumber;
728         omx_shed_output_port=p_param.nStartPortNumber+1;
729
730
731         error=OMX_GetParameter(omx_vid_sched,OMX_IndexParamOtherInit,&p_param);
732         if (error!=OMX_ErrorNone){
733                 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX video scheduler OMX_GetParameter failed %x", error);
734                 DeAllocateCodecsOMX();
735                 return 0;
736         }
737         omx_shed_clock_port=p_param.nStartPortNumber;
738
739
740         if (!DisablePort(omx_vid_sched,omx_shed_input_port) || !DisablePort(omx_vid_sched,omx_shed_output_port)
741                         || !DisablePort(omx_vid_sched,omx_shed_clock_port)) {
742                 Log::getInstance()->log("Video", Log::DEBUG, "Disable Ports OMX video shed failed");
743                 DeAllocateCodecsOMX();
744                 return 0;
745         }
746
747
748         error=OMX_GetHandle(&omx_vid_rend,VPE_OMX_VIDEO_REND,NULL,&callbacks);
749         if (error!=OMX_ErrorNone){
750                 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX video rend failed %x", error);
751                 DeAllocateCodecsOMX();
752                 return 0;
753         }
754
755         error=OMX_GetParameter(omx_vid_rend,OMX_IndexParamVideoInit,&p_param);
756         if (error!=OMX_ErrorNone){
757                 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX video rend OMX_GetParameter failed %x", error);
758                 DeAllocateCodecsOMX();
759                 return 0;
760         }
761         omx_rend_input_port=p_param.nStartPortNumber;
762         //omx_rend_output_port=p_param.nStartPortNumber+1;
763
764
765         if (!DisablePort(omx_vid_rend,omx_rend_input_port) /*|| !DisablePort(omx_vid_rend,omx_rend_output_port)*/
766                                 ) {
767                 Log::getInstance()->log("Video", Log::DEBUG, "Disable Ports OMX video rend failed");
768                 DeAllocateCodecsOMX();
769                 return 0;
770         }
771
772         //Setuo chain
773
774
775
776
777
778
779
780
781 /*      error=OMX_SendCommand(omx_vid_dec,OMX_CommandStateSet,OMX_StateIdle,0);
782         if (error!=OMX_ErrorNone){
783                 Log::getInstance()->log("Video", Log::DEBUG, "vid_dec Send Command to OMX State Idle %x", error);
784                 return 0;
785         }*/
786
787
788
789
790         OMX_VIDEO_PARAM_PORTFORMATTYPE ft_type;
791         memset(&ft_type,0,sizeof(ft_type));
792         ft_type.nSize=sizeof(ft_type);
793         ft_type.nVersion.nVersion=OMX_VERSION;
794
795         ft_type.nPortIndex=omx_codec_input_port;
796         if (h264) {
797                 ft_type.eCompressionFormat=OMX_VIDEO_CodingAVC;
798         } else {
799                 ft_type.eCompressionFormat=OMX_VIDEO_CodingMPEG2;
800         }
801
802         Demuxer* demux=Demuxer::getInstance();
803
804     ft_type.xFramerate=0;//25*(1<<16);//demux->getFrameRate()*(1<<16);
805     Log::getInstance()->log("Video", Log::DEBUG, "Framerate: %d",demux->getFrameRate());
806         error=OMX_SetParameter(omx_vid_dec,OMX_IndexParamVideoPortFormat,&ft_type);
807         if (error!=OMX_ErrorNone){
808                 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX_IndexParamVideoPortFormat failed %x", error);
809                 DeAllocateCodecsOMX();
810                 return 0;
811         }
812
813
814         if (!ChangeComponentState(omx_vid_dec,OMX_StateIdle)) {
815                 Log::getInstance()->log("Video", Log::DEBUG, "vid_dec ChangeComponentState");
816                 DeAllocateCodecsOMX();
817                 return 0;
818         }
819
820
821         if (!PrepareInputBufsOMX()) {
822                 DeAllocateCodecsOMX();
823                 return 0;
824         }
825
826
827         if (!ChangeComponentState(omx_clock,OMX_StateIdle)) {
828                 Log::getInstance()->log("Video", Log::DEBUG, "omx_clock ChangeComponentState Idle");
829                 DeAllocateCodecsOMX();
830                 return 0;
831         }
832
833         error=OMX_SetupTunnel(omx_clock,omx_clock_output_port,omx_vid_sched,omx_shed_clock_port);
834         if (error!=OMX_ErrorNone){
835                 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);
836                 DeAllocateCodecsOMX();
837                 return 0;
838         }
839
840         if (!EnablePort(omx_clock,omx_clock_output_port,false) || !EnablePort(omx_vid_sched,omx_shed_clock_port,false)
841                                         ) {
842                 Log::getInstance()->log("Video", Log::DEBUG, "Enable Ports OMX clock shed failed");
843                 DeAllocateCodecsOMX();
844                 return 0;
845         }
846
847         if ( !CommandFinished(omx_vid_sched,OMX_CommandPortEnable,omx_shed_clock_port)) {
848                 DeAllocateCodecsOMX();
849                 return 0;
850         }
851
852         error=OMX_SetupTunnel(omx_vid_dec,omx_codec_output_port,omx_vid_sched,omx_shed_input_port);
853         if (error!=OMX_ErrorNone){
854                 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel dec to sched failed %x", error);
855                 DeAllocateCodecsOMX();
856                 return 0;
857         }
858
859
860
861         if (!EnablePort(omx_vid_dec,omx_codec_output_port,false) || !EnablePort(omx_vid_sched,omx_shed_input_port,false)
862                                                 ) {
863                 Log::getInstance()->log("Video", Log::DEBUG, "Enable Ports OMX codec shed failed");
864                 DeAllocateCodecsOMX();
865                 return 0;
866         }
867
868         if ( !CommandFinished(omx_vid_sched,OMX_CommandPortEnable,omx_shed_input_port)) {
869                 return 0;
870         }
871         if (!ChangeComponentState(omx_vid_sched,OMX_StateIdle)) {
872                 Log::getInstance()->log("Video", Log::DEBUG, "vid_sched idle ChangeComponentState");
873                 DeAllocateCodecsOMX();
874                 return 0;
875         }
876         if (!CommandFinished(omx_clock,OMX_CommandPortEnable,omx_clock_output_port)
877                         ||!CommandFinished(omx_vid_dec,OMX_CommandPortEnable,omx_codec_output_port)){
878                 DeAllocateCodecsOMX();
879                 return 0;
880         }
881
882         if (!ChangeComponentState(omx_vid_dec,OMX_StateExecuting)) {
883                 Log::getInstance()->log("Video", Log::DEBUG, "omx_vid_dec ChangeComponentState Execute");
884                 DeAllocateCodecsOMX();
885                 return 0;
886         }
887
888         error=OMX_SetupTunnel(omx_vid_sched,omx_shed_output_port,omx_vid_rend,omx_rend_input_port);
889         if (error!=OMX_ErrorNone){
890                 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel  sched to rend failed %x", error);
891                 DeAllocateCodecsOMX();
892                 return 0;
893         }
894
895         if (!EnablePort(omx_vid_sched,omx_shed_output_port,false) || !EnablePort(omx_vid_rend,omx_rend_input_port,false)
896                                                         ) {
897                 Log::getInstance()->log("Video", Log::DEBUG, "Enable Ports OMX  shed rend failed");
898                 DeAllocateCodecsOMX();
899                 return 0;
900         }
901
902         if (!CommandFinished(omx_vid_sched,OMX_CommandPortEnable,omx_shed_output_port)
903                                         || !CommandFinished(omx_vid_rend,OMX_CommandPortEnable,omx_rend_input_port)) {
904                 DeAllocateCodecsOMX();
905                 return 0;
906         }
907
908         if (!ChangeComponentState(omx_vid_rend,OMX_StateIdle)) {
909                 Log::getInstance()->log("Video", Log::DEBUG, "vid_rend ChangeComponentState");
910                 DeAllocateCodecsOMX();
911                 return 0;
912         }
913
914
915         if (!ChangeComponentState(omx_vid_sched,OMX_StateExecuting)) {
916                 Log::getInstance()->log("Video", Log::DEBUG, "omx_vid_sched ChangeComponentState Execute");
917                 DeAllocateCodecsOMX();
918                 return 0;
919         }
920
921         if (!ChangeComponentState(omx_vid_rend,OMX_StateExecuting)) {
922                 Log::getInstance()->log("Video", Log::DEBUG, "omx_vid_rend ChangeComponentState Execute");
923                 DeAllocateCodecsOMX();
924                 return 0;
925         }
926
927         //raspbi specifif
928         OMX_CONFIG_DISPLAYREGIONTYPE dispconf;
929         memset(&dispconf,0,sizeof(dispconf));
930         dispconf.nSize=sizeof(dispconf);
931         dispconf.nVersion.nVersion=OMX_VERSION;
932
933         dispconf.nPortIndex=omx_rend_input_port;
934
935         dispconf.set=OMX_DISPLAY_SET_LAYER ;
936         dispconf.layer=1;
937         error=OMX_SetParameter(omx_vid_rend,OMX_IndexConfigDisplayRegion,&dispconf);
938         if (error!=OMX_ErrorNone){
939                 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX_IndexConfigDisplayRegion failed %x", error);
940                 DeAllocateCodecsOMX();
941                 return 0;
942         }
943
944 /*      dispconf.set=OMX_DISPLAY_SET_FULLSCREEN ;
945         dispconf.fullscreen=OMX_FALSE;
946         error=OMX_SetParameter(omx_vid_rend,OMX_IndexConfigDisplayRegion,&dispconf);
947         if (error!=OMX_ErrorNone){
948                 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX_IndexConfigDisplayRegion failed %x", error);
949                 DeAllocateCodecsOMX();
950                 return 0;
951         }
952
953         dispconf.set=OMX_DISPLAY_SET_DEST_RECT;
954         dispconf.dest_rect.x_offset=100;
955         dispconf.dest_rect.y_offset=100;
956         dispconf.dest_rect.width=640;
957         dispconf.dest_rect.height=480;
958         error=OMX_SetParameter(omx_vid_rend,OMX_IndexConfigDisplayRegion,&dispconf);
959         if (error!=OMX_ErrorNone){
960                 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX_IndexConfigDisplayRegion failed %x", error);
961                 DeAllocateCodecsOMX();
962                 return 0;
963         }*/
964
965
966
967
968         if (!ChangeComponentState(omx_clock,OMX_StateExecuting)) {
969                         Log::getInstance()->log("Video", Log::DEBUG, "omx_clock ChangeComponentState Exccute");
970                         DeAllocateCodecsOMX();
971                         return 0;
972         }
973
974         omx_running=true;
975
976         return 1;
977 }
978
979
980 int VideoVPEOGL::ChangeComponentState(OMX_HANDLETYPE handle,OMX_STATETYPE type)
981 {
982         OMX_ERRORTYPE error;
983         error=OMX_SendCommand(handle,OMX_CommandStateSet,type,0);
984         if (error!=OMX_ErrorNone){
985                 Log::getInstance()->log("Video", Log::DEBUG, "handle %x Send Command to OMX State %x %x",handle,type, error);
986                 return 0;
987         }
988
989         if (!CommandFinished(handle,OMX_CommandStateSet,type)) {
990                 return 0;
991         }
992
993         return 1;
994 }
995
996
997 int VideoVPEOGL::EnablePort(OMX_HANDLETYPE handle,OMX_U32 port,bool wait)
998 {
999         OMX_ERRORTYPE error;
1000         error=OMX_SendCommand(handle,OMX_CommandPortEnable,port,0);
1001         if (error!=OMX_ErrorNone){
1002                 Log::getInstance()->log("Video", Log::DEBUG, "handle %x Send Command to enable port %x %x",handle,port, error);
1003                 return 0;
1004         }
1005
1006         if (!wait) return 1;
1007         if (!CommandFinished(handle,OMX_CommandPortEnable,port)) {
1008                 return 0;
1009         }
1010
1011         return 1;
1012 }
1013
1014
1015 int VideoVPEOGL::DisablePort(OMX_HANDLETYPE handle,OMX_U32 port,bool wait)
1016 {
1017         OMX_ERRORTYPE error;
1018         error=OMX_SendCommand(handle,OMX_CommandPortDisable,port,0);
1019         if (error!=OMX_ErrorNone){
1020                 Log::getInstance()->log("Video", Log::DEBUG, "handle %x Send Command to disable port %x %x",handle,port, error);
1021                 return 0;
1022         }
1023
1024         if (!wait) return 1;
1025         if (!CommandFinished(handle,OMX_CommandPortDisable,port)) {
1026                 return 0;
1027         }
1028
1029
1030         return 1;
1031 }
1032
1033
1034
1035
1036 int VideoVPEOGL::CommandFinished(OMX_HANDLETYPE handle,OMX_U32 command,OMX_U32 data2)
1037 {
1038         int i=0;
1039         while (i<1000) {
1040                 omx_event_mutex.Lock();
1041                 list<VPE_OMX_EVENT>::iterator itty=omx_events.begin();
1042                 while (itty!=omx_events.end()) {
1043
1044                         VPE_OMX_EVENT current=*itty;
1045                         if (current.handle==handle) { //this is ours
1046                                 if (current.event_type==OMX_EventError) {
1047                                         omx_events.erase(itty);
1048                                         omx_event_mutex.Unlock();
1049                                         return 0;
1050
1051                                 } else if (current.event_type==OMX_EventCmdComplete && current.data1==command && current.data2==data2) {
1052                                         omx_events.erase(itty);
1053                                         omx_event_mutex.Unlock();
1054                                         return 1;
1055                                 }
1056                         }
1057                         itty++;
1058
1059                 }
1060                 omx_event_mutex.Unlock();
1061                 MILLISLEEP(2);
1062                 i++;
1063
1064         }
1065         Log::getInstance()->log("Video", Log::DEBUG, "CommandFinished waited too long %x %x %x",handle,command, data2);
1066         return 0;
1067
1068 }
1069
1070
1071
1072
1073
1074 int VideoVPEOGL::PrepareInputBufsOMX()
1075 {
1076         OMX_ERRORTYPE error;
1077         OMX_PARAM_PORTDEFINITIONTYPE port_def_type;
1078         memset(&port_def_type,0,sizeof(port_def_type));
1079         port_def_type.nSize=sizeof(port_def_type);
1080         port_def_type.nVersion.nVersion=OMX_VERSION;
1081         port_def_type.nPortIndex=omx_codec_input_port;
1082
1083         error=OMX_GetParameter(omx_vid_dec,OMX_IndexParamPortDefinition, &port_def_type);
1084
1085         if (error!=OMX_ErrorNone){
1086                         Log::getInstance()->log("Video", Log::DEBUG, "Get OMX OMX_IndexParamPortDefinition failed %x", error);
1087         }
1088 /*      Log::getInstance()->log("Video", Log::DEBUG, "Port para %d %d %d %d %d %d %d", port_def_type.nBufferCountActual,
1089                         port_def_type.nBufferCountMin,port_def_type.nBufferSize,port_def_type.bEnabled,port_def_type.bPopulated,
1090                         port_def_type.bBuffersContiguous,port_def_type.nBufferAlignment);*/
1091
1092         port_def_type.nBufferCountActual=60;
1093
1094         error=OMX_SetParameter(omx_vid_dec,OMX_IndexParamPortDefinition, &port_def_type);
1095
1096         if (error!=OMX_ErrorNone){
1097                         Log::getInstance()->log("Video", Log::DEBUG, "Set OMX OMX_IndexParamPortDefinition failed %x", error);
1098         }
1099
1100
1101         error=OMX_SendCommand(omx_vid_dec,OMX_CommandPortEnable,omx_codec_input_port,0);
1102         if (error!=OMX_ErrorNone){
1103                 Log::getInstance()->log("Video", Log::DEBUG, "Prepare Input bufs Send Command to enable port %x", error);
1104                 return 0;
1105         }
1106
1107         input_bufs_omx_mutex.Lock();
1108         for (unsigned int i=0; i< port_def_type.nBufferCountActual;i++) {
1109
1110         //      unsigned char* new_buffer_data=(unsigned char*)malloc(port_def_type.nBufferSize);
1111                 OMX_BUFFERHEADERTYPE *buf_head=NULL;
1112         /*      error=OMX_UseBuffer(omx_vid_dec,&buf_head,omx_codec_input_port,NULL,port_def_type.nBufferSize,new_buffer_data);
1113                 if (error!=OMX_ErrorNone){
1114                         Log::getInstance()->log("Video", Log::DEBUG, "Use OMX_UseBuffer failed %x", error);
1115                         input_bufs_omx_mutex.Unlock();
1116                         return 0;
1117                 }*/
1118                 error=OMX_AllocateBuffer(omx_vid_dec,&buf_head,omx_codec_input_port,NULL,port_def_type.nBufferSize);
1119                 if (error!=OMX_ErrorNone){
1120                         Log::getInstance()->log("Video", Log::DEBUG, "Use OMX_AllocateBuffer failed %x", error);
1121                                 input_bufs_omx_mutex.Unlock();
1122                         return 0;
1123                 }
1124                 input_bufs_omx_all.push_back(buf_head);
1125                 input_bufs_omx_free.push_back(buf_head);
1126         }
1127         omx_first_frame=true;
1128
1129         firstsynched=false;
1130         cur_input_buf_omx=NULL;
1131         input_bufs_omx_mutex.Unlock();
1132
1133
1134         Log::getInstance()->log("Video", Log::DEBUG, "PrepareInputBufsOMX mark3");
1135         if (!CommandFinished(omx_vid_dec,OMX_CommandPortEnable,omx_codec_input_port)) {
1136                 return 0;
1137         }
1138         Log::getInstance()->log("Video", Log::DEBUG, "PrepareInputBufsOMX mark4");
1139
1140         return 1;
1141 }
1142
1143 int VideoVPEOGL::DestroyInputBufsOMX()
1144 {
1145         OMX_ERRORTYPE error;
1146
1147         cur_input_buf_omx=NULL;
1148         input_bufs_omx_mutex.Lock();
1149         for (int i=0; i< input_bufs_omx_all.size();i++) {
1150         //      free(input_bufs_omx_all[i]->pBuffer);
1151         //      input_bufs_omx_all[i]->pBuffer=NULL;
1152                 error=OMX_FreeBuffer(omx_vid_dec,omx_codec_input_port,input_bufs_omx_all[i]);
1153                 if (error!=OMX_ErrorNone){
1154                         Log::getInstance()->log("Video", Log::DEBUG, "Use OMX_FreeBuffer failed %x", error);
1155                         input_bufs_omx_mutex.Unlock();
1156                         return 0;
1157                 }
1158         }
1159         input_bufs_omx_all.clear();
1160         input_bufs_omx_free.clear();
1161         input_bufs_omx_mutex.Unlock();
1162
1163 }
1164
1165
1166
1167
1168 int VideoVPEOGL::DeAllocateCodecsOMX()
1169 {
1170         OMX_ERRORTYPE error;
1171         omx_running=false;
1172         if (omx_vid_dec) {
1173                 // first flush all buffers
1174
1175                 error=OMX_SendCommand(omx_vid_dec,OMX_CommandFlush, omx_codec_output_port, NULL);
1176                 if (error!=OMX_ErrorNone){
1177                                 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Flush codec out failed %x", error);
1178
1179                 }
1180
1181                 error=OMX_SendCommand(omx_vid_sched,OMX_CommandFlush, omx_shed_input_port, NULL);
1182                 if (error!=OMX_ErrorNone){
1183                         Log::getInstance()->log("Video", Log::DEBUG, "OMX_Flush shed in failed %x", error);
1184
1185                 }
1186
1187                 if (!CommandFinished(omx_vid_dec,OMX_CommandFlush,omx_codec_output_port) ||
1188                                 !CommandFinished(omx_vid_sched,OMX_CommandFlush,omx_shed_input_port)) {
1189                         Log::getInstance()->log("Video", Log::DEBUG, "flush cmd codec shed failed");
1190                 }
1191
1192                 error=OMX_SendCommand(omx_clock,OMX_CommandFlush, omx_clock_output_port, NULL);
1193                 if (error!=OMX_ErrorNone){
1194                         Log::getInstance()->log("Video", Log::DEBUG, "OMX_Flush clock out failed %x", error);
1195
1196                 }
1197
1198                 error=OMX_SendCommand(omx_vid_sched,OMX_CommandFlush, omx_shed_clock_port, NULL);
1199                 if (error!=OMX_ErrorNone){
1200                         Log::getInstance()->log("Video", Log::DEBUG, "OMX_Flush shed clock failed %x", error);
1201
1202                 }
1203
1204                 if (!CommandFinished(omx_clock,OMX_CommandFlush,omx_clock_output_port) ||
1205                         !CommandFinished(omx_vid_sched,OMX_CommandFlush,omx_shed_clock_port)) {
1206                                 Log::getInstance()->log("Video", Log::DEBUG, "flush cmd clock shed failed");
1207                 }
1208
1209                 error=OMX_SendCommand(omx_vid_sched,OMX_CommandFlush, omx_shed_output_port, NULL);
1210                 if (error!=OMX_ErrorNone) {
1211                         Log::getInstance()->log("Video", Log::DEBUG, "OMX_Flush shed out failed %x", error);
1212
1213                 }
1214
1215                 error=OMX_SendCommand(omx_vid_rend,OMX_CommandFlush, omx_rend_input_port, NULL);
1216                 if (error!=OMX_ErrorNone) {
1217                         Log::getInstance()->log("Video", Log::DEBUG, "OMX_Flush rend in failed %x", error);
1218
1219                 }
1220
1221                 if (!CommandFinished(omx_vid_sched,OMX_CommandFlush,omx_shed_output_port) ||
1222                                 !CommandFinished(omx_vid_rend,OMX_CommandFlush,omx_rend_input_port)) {
1223                         Log::getInstance()->log("Video", Log::DEBUG, "flush cmd shed rend failed");
1224                 }
1225
1226
1227
1228
1229                 error=OMX_SendCommand(omx_vid_dec,OMX_CommandFlush, omx_codec_input_port, NULL);
1230                 if (error!=OMX_ErrorNone){
1231                         Log::getInstance()->log("Video", Log::DEBUG, "OMX_Flush codec out failed %x", error);
1232
1233                 }
1234
1235
1236                 if (!CommandFinished(omx_vid_dec,OMX_CommandFlush,omx_codec_input_port)) {
1237                         Log::getInstance()->log("Video", Log::DEBUG, "flush cmd codec input failed");
1238                 }
1239
1240                 DestroyInputBufsOMX();
1241
1242                 //todo flushing
1243                 if (!DisablePort(omx_vid_rend,omx_rend_input_port,true)) {
1244                         Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 1");
1245                 }
1246                 if (!DisablePort(omx_vid_sched,omx_shed_output_port,true)) {
1247                         Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 2 ");
1248                 }
1249
1250                 error=OMX_SetupTunnel(omx_vid_rend,omx_rend_input_port,NULL,NULL);
1251                 if (error!=OMX_ErrorNone){
1252                         Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1253
1254                 }
1255
1256                 error=OMX_SetupTunnel(omx_vid_sched,omx_shed_output_port,NULL,NULL);
1257                 if (error!=OMX_ErrorNone){
1258                         Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1259
1260                 }
1261
1262                 if (!DisablePort(omx_vid_sched,omx_shed_input_port,true)) {
1263                         Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 3");
1264                 }
1265
1266                 if (!DisablePort(omx_vid_sched,omx_shed_clock_port,true)) {
1267                         Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 4");
1268                 }
1269
1270                 if (!DisablePort(omx_clock,omx_clock_output_port,true)) {
1271                         Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 5");
1272                 }
1273                 if (!DisablePort(omx_vid_dec,omx_codec_output_port,true)) {
1274                         Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 6");
1275                 }
1276
1277
1278
1279                 if (!DisablePort(omx_vid_dec,omx_codec_input_port,true)) {
1280                         Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 7");
1281                 }
1282
1283
1284                 error=OMX_SetupTunnel(omx_vid_sched,omx_shed_input_port,NULL,NULL);
1285                 if (error!=OMX_ErrorNone){
1286                         Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1287
1288                 }
1289                 error=OMX_SetupTunnel(omx_vid_sched,omx_shed_clock_port,NULL,NULL);
1290                 if (error!=OMX_ErrorNone){
1291                         Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1292
1293                 }
1294
1295                 error=OMX_SetupTunnel(omx_clock,omx_clock_output_port,NULL,NULL);
1296                 if (error!=OMX_ErrorNone){
1297                         Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1298
1299                 }
1300
1301                 error=OMX_SetupTunnel(omx_vid_dec,omx_codec_output_port,NULL,NULL);
1302                 if (error!=OMX_ErrorNone){
1303                         Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1304
1305                 }
1306
1307
1308
1309
1310
1311                 error=OMX_FreeHandle(omx_vid_dec);
1312                 error=OMX_FreeHandle(omx_vid_sched);
1313                 error=OMX_FreeHandle(omx_vid_rend);
1314                 error=OMX_FreeHandle(omx_clock);
1315                 omx_vid_dec=NULL;
1316                 if (error!=OMX_ErrorNone) {
1317                         Log::getInstance()->log("Video", Log::DEBUG, "FreeHandle failed %d", error);
1318                 }
1319         }
1320
1321         return 1;
1322 }
1323
1324 #endif
1325
1326
1327 #ifdef VPE_FFMPEG_SUPPORT
1328
1329 int VideoVPEOGL::AllocateCodecsFFMPEG()
1330 {
1331         ffmpeg_hastime=false;
1332         Log::getInstance()->log("Video", Log::NOTICE, "AllocateCodecsFFmpeg");
1333         mpeg2codec_context_ff=avcodec_alloc_context();
1334         if (mpeg2codec_context_ff==NULL) {
1335                 Log::getInstance()->log("Video", Log::DEBUG, "Creating ffmpeg codec context failed");
1336                 return 0;
1337         }
1338         if(avcodec_open(mpeg2codec_context_ff, mpeg2codec_ff)<0) {
1339                 Log::getInstance()->log("Video", Log::DEBUG, "Opening ffmpeg codec  failed");
1340                 return 0;
1341         }
1342         Log::getInstance()->log("Video", Log::NOTICE, "AllocateCodecsFFmpeg mark1");
1343         dec_frame_ff_mutex.Lock();
1344         Log::getInstance()->log("Video", Log::NOTICE, "AllocateCodecsFFmpeg mark2");
1345         for (int i=0;i<3;i++) {
1346                         AVFrame *dec_frame_ff=avcodec_alloc_frame(); // may be we need multiple frames, if we want to use async texture upload
1347                         if (!dec_frame_ff) {
1348                                 Log::getInstance()->log("Video", Log::DEBUG, "Allocating dec_frame  failed");
1349                                 return 0;
1350                         }
1351                         dec_frame_ff_all.push_back(dec_frame_ff);
1352                         dec_frame_ff_free.push_back(dec_frame_ff);
1353         }
1354         dec_frame_ff_decoding=NULL;
1355         Log::getInstance()->log("Video", Log::NOTICE, "AllocateCodecsFFmpeg mark 3");
1356         dec_frame_ff_mutex.Unlock();
1357
1358         ogl_frame_mutex.Lock();
1359         //Allocate texture structs, since we do not know the sizes, we do not allocate the textures yet
1360         for (int i=0;i<3;i++) {
1361                 VPEOGLFrame *new_frame=(VPEOGLFrame *)malloc(sizeof(VPEOGLFrame));
1362                 new_frame->type=1; //1 = YUV, 2 RGB
1363                 new_frame->textures[0]=0;
1364                 new_frame->textures[1]=0;
1365                 new_frame->textures[2]=0;
1366                 new_frame->width=new_frame->height=0;
1367                 all_ogl_frames.push_back(new_frame);
1368                 free_ogl_frames.push_back(new_frame);
1369
1370         }
1371         ogl_frame_outside=false;
1372
1373         ogl_frame_mutex.Unlock();
1374
1375         ffmpeg_running=true;
1376
1377         return 1;
1378
1379 }
1380
1381 int VideoVPEOGL::DeAllocateCodecsFFMPEG()
1382 {
1383         ffmpeg_running=false;
1384         Log::getInstance()->log("Video", Log::NOTICE, "DeAllocateCodecsFFmpeg");
1385         dec_frame_ff_mutex.Lock();
1386         dec_frame_ff_upload_pending.clear();
1387         dec_frame_ff_free.clear();
1388         dec_frame_ff_mutex.Unlock();
1389         while (dec_frame_ff_uploading) {
1390                 Log::getInstance()->log("Video", Log::NOTICE, "Wait for uploading to finish");
1391                 MILLISLEEP(20);
1392         }
1393         dec_frame_ff_mutex.Lock();
1394         for (int i=0; i< dec_frame_ff_all.size();i++) {
1395                 av_free(dec_frame_ff_all[i]);
1396         }
1397
1398         dec_frame_ff_all.clear();
1399         dec_frame_ff_mutex.Unlock();
1400         dec_frame_ff_decoding=NULL;
1401
1402         while (ogl_frame_outside) {
1403                 Log::getInstance()->log("Video", Log::NOTICE, "Wait for ogl frame from outside");
1404                 MILLISLEEP(20);
1405         }
1406
1407         ((OsdOpenGL*)Osd::getInstance())->BeginPainting(); // get osd's context
1408         ogl_frame_mutex.Lock();
1409         for (int i=0; i< dec_frame_ff_all.size();i++) {
1410                 VPEOGLFrame * del_frame=all_ogl_frames[i];
1411                 if (del_frame->textures[0]==0) {
1412                         glDeleteTextures(1,&del_frame->textures[0]);
1413                         del_frame->textures[0]=0;
1414                 }
1415                 if (del_frame->textures[1]==0) {
1416                         glDeleteTextures(1,&del_frame->textures[1]);
1417                         del_frame->textures[1]=0;
1418                 }
1419                 if (del_frame->textures[2]==0) {
1420                         glDeleteTextures(1,&del_frame->textures[2]);
1421                         del_frame->textures[2]=0;
1422                 }
1423                 free(all_ogl_frames[i]);
1424         }
1425         all_ogl_frames.clear();
1426         free_ogl_frames.clear();
1427         ready_ogl_frames.clear();
1428         ogl_frame_mutex.Unlock();
1429         ((OsdOpenGL*)Osd::getInstance())->EndPainting();
1430
1431
1432         if (mpeg2codec_context_ff) {
1433                 avcodec_close(mpeg2codec_context_ff);
1434                 av_free(mpeg2codec_context_ff);
1435                 mpeg2codec_context_ff=NULL;
1436
1437         }
1438
1439
1440
1441         return 1;
1442 }
1443
1444
1445
1446
1447
1448 #endif
1449
1450 int VideoVPEOGL::stop()
1451 {
1452   if (!initted) return 0;
1453
1454 #ifdef VPE_OMX_SUPPORT
1455   //Check if ffmpeg mode
1456   if (decoding_backend==VPE_DECODER_OMX) DeAllocateCodecsOMX();
1457   decoding_backend=0;
1458
1459 #endif
1460
1461   
1462
1463 //  if (ioctl(fdVideo, AV_SET_VID_STOP, 0) != 0) return 0;
1464   return 1;
1465 }
1466
1467 int VideoVPEOGL::reset()
1468 {
1469   if (!initted) return 0;
1470  
1471 //  if (ioctl(fdVideo, AV_SET_VID_RESET, 0x11) != 0) return 0;
1472   return 1;
1473 }
1474
1475 int VideoVPEOGL::pause()
1476 {
1477   if (!initted) return 0;
1478
1479 //  if (ioctl(fdVideo, AV_SET_VID_PAUSE, 0) != 0) return 0;
1480   return 1;
1481 }
1482
1483 int VideoVPEOGL::unPause() // FIXME get rid - same as play!!
1484 {
1485   if (!initted) return 0;
1486 //  if (ioctl(fdVideo, AV_SET_VID_PLAY, 0) != 0) return 0;
1487   return 1;
1488 }
1489
1490 int VideoVPEOGL::fastForward()
1491 {
1492   if (!initted) return 0;
1493
1494 //  if (ioctl(fdVideo, AV_SET_VID_FFWD, 1) != 0) return 0;
1495   return 1;
1496 }
1497
1498 int VideoVPEOGL::unFastForward()
1499 {
1500   if (!initted) return 0;
1501
1502 //  if (ioctl(fdVideo, AV_SET_VID_RESET, 0x11) != 0) return 0; // don't need this.
1503
1504  //// if (ioctl(fdVideo, AV_SET_VID_PLAY, 0) != 0) return 0;
1505   return 1;
1506 }
1507
1508 int VideoVPEOGL::attachFrameBuffer()
1509 {
1510   if (!initted) return 0;
1511
1512 //  if (ioctl(fdVideo, AV_SET_VID_FB, 0) != 0) return 0;
1513   return 1;
1514 }
1515
1516 int VideoVPEOGL::blank(void)
1517 {
1518 //  if (ioctl(fdVideo, AV_SET_VID_FB, 1) != 0) return 0;
1519 //  if (ioctl(fdVideo, AV_SET_VID_FB, 0) != 0) return 0;
1520   return 1;
1521 }
1522
1523 ULLONG VideoVPEOGL::getCurrentTimestamp()
1524 {
1525 /*  sync_data_t timestamps;
1526   if (ioctl(fdVideo, AV_GET_VID_TIMESTAMPS, &timestamps) == 0)
1527   {
1528     // FIXME are these the right way around?
1529
1530     timestamps.stc = (timestamps.stc >> 31 ) | (timestamps.stc & 1);
1531     timestamps.pts = (timestamps.pts >> 31 ) | (timestamps.pts & 1);
1532
1533     return timestamps.stc;
1534   }
1535   else
1536   {
1537     return 0;
1538   }*/
1539   return 0;
1540 }
1541
1542 ULONG VideoVPEOGL::timecodeToFrameNumber(ULLONG timecode)
1543 {
1544   if (format == PAL) return (ULONG)(((double)timecode / (double)90000) * (double)25);
1545   else               return (ULONG)(((double)timecode / (double)90000) * (double)30);
1546 }
1547
1548 #ifdef DEV
1549 int VideoVPEOGL::test()
1550 {
1551   return 0;
1552
1553 //  ULLONG stc = 0;
1554 //  return ioctl(fdVideo, AV_SET_VID_STC, &stc);
1555 /*
1556  // reset();
1557   return 1;
1558 */
1559 }
1560
1561 int VideoVPEOGL::test2()
1562 {
1563   return 0;
1564 }
1565 #endif
1566
1567 void VideoVPEOGL::PrepareMediaSample(const MediaPacketList& mplist,UINT samplepos)
1568 {
1569   mediapacket = mplist.front();
1570 }
1571
1572 UINT VideoVPEOGL::DeliverMediaSample(UCHAR* buffer, UINT *samplepos)
1573 {
1574   DeliverMediaPacket(mediapacket, buffer, samplepos);
1575   if (*samplepos == mediapacket.length) {
1576     *samplepos = 0;
1577     return 1;
1578   }
1579   else return 0;
1580 }
1581
1582 UINT VideoVPEOGL::DeliverMediaPacket(MediaPacket packet,
1583                 const UCHAR* buffer,
1584                 UINT *samplepos)
1585 {
1586         if (packet.type == MPTYPE_VIDEO_H264)
1587         {
1588                 h264=true;
1589         }
1590         else
1591         {
1592                 h264=false;
1593         }
1594         switch (decoding_backend) {
1595         default: case 0: return 0; // no backend runnigng
1596 #ifdef VPE_OMX_SUPPORT
1597         case VPE_DECODER_OMX: return DeliverMediaPacketOMX(packet,buffer,samplepos);
1598 #endif
1599 #ifdef VPE_FFMPEG_SUPPORT
1600         case VPE_DECODER_FFMPEG: return DeliverMediaPacketFFMPEG(packet,buffer,samplepos);
1601 #endif
1602         }
1603 }
1604
1605 #ifdef VPE_OMX_SUPPORT
1606 UINT VideoVPEOGL::DeliverMediaPacketOMX(MediaPacket packet,
1607                 const UCHAR* buffer,
1608                 UINT *samplepos)
1609 {
1610
1611
1612         //Later add fail back code for ffmpeg
1613         /*if (!videoon) {
1614                 *samplepos+=packet.length;
1615                 return packet.length;
1616         }*/
1617
1618
1619         if (!omx_running) return 0; // if we are not runnig do not do this
1620
1621
1622         OMX_ERRORTYPE error;
1623
1624 /*      OMX_PARAM_PORTDEFINITIONTYPE port_image;
1625         memset(&port_image,0,sizeof(port_image));
1626         port_image.nSize=sizeof(port_image);
1627         port_image.nVersion.nVersion=OMX_VERSION;
1628         port_image.nPortIndex =omx_codec_output_port;
1629         error=OMX_GetParameter(omx_vid_dec,OMX_IndexParamPortDefinition, &port_image);
1630         if (error!= OMX_ErrorNone){
1631                 Log::getInstance()->log("Video", Log::DEBUG, "OMX_GetParameter failed %x", error);
1632         }
1633         Log::getInstance()->log("Video", Log::DEBUG, "Image port %d %d", port_image.format.video.nFrameWidth , port_image.format.video.nFrameHeight);*/
1634
1635         /*First Check, if we have an audio sample*/
1636         if (iframemode) {
1637                 //samplepos=0;
1638                 MILLISLEEP(10);
1639                 return 0; //Not in iframe mode!
1640         }
1641
1642         UINT headerstrip=0;
1643         if (packet.disconti) {
1644                 firstsynched=false;
1645                 if (cur_input_buf_omx) {
1646                         OMX_ERRORTYPE error=OMX_EmptyThisBuffer(omx_vid_dec,cur_input_buf_omx);
1647                         if (error!=OMX_ErrorNone){
1648                                 Log::getInstance()->log("Video", Log::DEBUG, "OMX_EmptyThisBuffer failed %x", error);
1649                         }
1650                         cur_input_buf_omx=NULL;
1651                 }
1652         }
1653
1654         /*Inspect PES-Header */
1655
1656 //      OMX_STATETYPE temp_state;
1657 //      OMX_GetState(omx_vid_dec,&temp_state);
1658
1659         if (*samplepos==0) {//stripheader
1660                 headerstrip=buffer[packet.pos_buffer+8]+9/*is this right*/;
1661                 *samplepos+=headerstrip;
1662                 if ( packet.synched ) {
1663
1664                         if (cur_input_buf_omx) {
1665                                 OMX_ERRORTYPE error=OMX_EmptyThisBuffer(omx_vid_dec,cur_input_buf_omx);
1666                                 if (error!=OMX_ErrorNone){
1667                                         Log::getInstance()->log("Video", Log::DEBUG, "OMX_EmptyThisBuffer failed %x", error);
1668                                 }
1669
1670                                 cur_input_buf_omx=NULL;//write out old data
1671                         }
1672                 //      reftime1=packet.presentation_time;
1673                 //      reftime2=reftime1+1;
1674                         firstsynched=true;
1675                 } else {
1676                         if (!firstsynched) {//
1677                                 *samplepos=packet.length;//if we have not processed at least one
1678                                 return packet.length;//synched packet ignore it!
1679                         }
1680                 }
1681         }
1682
1683         if (!cur_input_buf_omx) {
1684                 input_bufs_omx_mutex.Lock();
1685                 if (input_bufs_omx_free.size()==0) {
1686                         input_bufs_omx_mutex.Unlock();
1687                         Log::getInstance()->log("Video", Log::DEBUG, "Deliver MediaPacket no free sample");
1688                         return 0; // we do not have a free media sample
1689
1690                 }
1691                 cur_input_buf_omx=input_bufs_omx_free.front();
1692                 cur_input_buf_omx->nFilledLen=0;
1693                 cur_input_buf_omx->nOffset=0;
1694                 cur_input_buf_omx->nTimeStamp=0;
1695                 input_bufs_omx_free.pop_front();
1696                 input_bufs_omx_mutex.Unlock();
1697         }
1698
1699
1700
1701
1702         if (cur_input_buf_omx->nFilledLen==0) {//will only be changed on first packet
1703                 /*if (packet.disconti) {
1704                         ms->SetDiscontinuity(TRUE);
1705                 } else {
1706                         ms->SetDiscontinuity(FALSE);
1707                 }*/
1708                 //if (packet.synched) {
1709
1710                         //lastreftimePTS=packet.pts;
1711                    if (omx_first_frame) { // TODO time
1712                            cur_input_buf_omx->nFlags=OMX_BUFFERFLAG_STARTTIME;
1713                            omx_first_frame=false;
1714                    } else
1715
1716                 //}
1717                 //else
1718                 //{
1719                         cur_input_buf_omx->nFlags=OMX_BUFFERFLAG_TIME_UNKNOWN;
1720
1721
1722                         //  ms->SetSyncPoint(TRUE);
1723                 //}
1724
1725         }
1726         unsigned int haveToCopy=packet.length-*samplepos;
1727
1728         while (haveToCopy> (cur_input_buf_omx->nAllocLen-cur_input_buf_omx->nFilledLen)) {
1729                 unsigned int cancopy=cur_input_buf_omx->nAllocLen-cur_input_buf_omx->nFilledLen;
1730                 memcpy(cur_input_buf_omx->pBuffer+cur_input_buf_omx->nFilledLen,buffer+packet.pos_buffer+*samplepos,cancopy);
1731                 haveToCopy-=cancopy;
1732                 cur_input_buf_omx->nFilledLen+=cancopy;
1733                 *samplepos+=cancopy;
1734                 // push old buffer out
1735
1736                 OMX_ERRORTYPE error=OMX_EmptyThisBuffer(omx_vid_dec,cur_input_buf_omx);
1737                 if (error!=OMX_ErrorNone){
1738                         Log::getInstance()->log("Video", Log::DEBUG, "OMX_EmptyThisBuffer failed %x", error);
1739                 }
1740                 // get5 new buffer
1741                 input_bufs_omx_mutex.Lock();
1742                 if (input_bufs_omx_free.size()==0) {
1743                         input_bufs_omx_mutex.Unlock();
1744                         //Log::getInstance()->log("Video", Log::DEBUG, "Deliver MediaPacket no free sample");
1745                         return *samplepos; // we do not have a free media sample
1746                 }
1747                 cur_input_buf_omx=input_bufs_omx_free.front();
1748                 cur_input_buf_omx->nFilledLen=0;
1749                 cur_input_buf_omx->nOffset=0;
1750                 cur_input_buf_omx->nTimeStamp=0;
1751                 input_bufs_omx_free.pop_front();
1752                 input_bufs_omx_mutex.Unlock();
1753
1754                 cur_input_buf_omx->nFlags=OMX_BUFFERFLAG_TIME_UNKNOWN;
1755
1756         }
1757         memcpy(cur_input_buf_omx->pBuffer+cur_input_buf_omx->nFilledLen,
1758                         buffer+packet.pos_buffer+*samplepos,haveToCopy);
1759         cur_input_buf_omx->nFilledLen+=haveToCopy;
1760
1761
1762
1763         *samplepos+=haveToCopy;
1764
1765         return *samplepos;
1766
1767 }
1768
1769 #endif
1770
1771
1772 #ifdef VPE_FFMPEG_SUPPORT
1773 UINT VideoVPEOGL::DeliverMediaPacketFFMPEG(MediaPacket packet,
1774                 const UCHAR* buffer,
1775                 UINT *samplepos)
1776 {
1777         //Later add fail back code for ffmpeg
1778         /*if (!videoon) {
1779                 *samplepos+=packet.length;
1780                 return packet.length;
1781         }*/
1782
1783         if (!ffmpeg_running) return 0; // if we are not runnig do not do this
1784
1785
1786         if (iframemode) {
1787                 //samplepos=0;
1788                 MILLISLEEP(10);
1789                 return 0; //Not in iframe mode!
1790         }
1791
1792         UINT headerstrip=0;
1793 /*      if (packet.disconti) {
1794                 firstsynched=false;
1795                 if (cur_input_buf_omx) {
1796                         OMX_ERRORTYPE error=OMX_EmptyThisBuffer(omx_vid_dec,cur_input_buf_omx);
1797                         if (error!=OMX_ErrorNone){
1798                                 Log::getInstance()->log("Video", Log::DEBUG, "OMX_EmptyThisBuffer failed %x", error);
1799                         }
1800                         cur_input_buf_omx=NULL;
1801                 }
1802         }*/
1803
1804         /*Inspect PES-Header */
1805         if (!dec_frame_ff_decoding) {
1806                 dec_frame_ff_mutex.Lock();
1807                 if (dec_frame_ff_free.size()>0) {
1808                         dec_frame_ff_decoding=dec_frame_ff_free.front();
1809                         dec_frame_ff_free.pop_front();
1810                         dec_frame_ff_mutex.Unlock();
1811                 } else {
1812                         Log::getInstance()->log("Video", Log::DEBUG, "We have no free buffers");
1813                         dec_frame_ff_mutex.Unlock();
1814                         // No free Buffers
1815                         return 0;
1816                 }
1817         }
1818
1819
1820
1821         if (*samplepos==0) {//stripheader
1822                 headerstrip=buffer[packet.pos_buffer+8]+9/*is this right*/;
1823                 *samplepos+=headerstrip;
1824                 if ( packet.synched ) {
1825
1826                         /*if (cur_input_buf_omx) {
1827                                 OMX_ERRORTYPE error=OMX_EmptyThisBuffer(omx_vid_dec,cur_input_buf_omx);
1828                                 if (error!=OMX_ErrorNone){
1829                                         Log::getInstance()->log("Video", Log::DEBUG, "OMX_EmptyThisBuffer failed %x", error);
1830                                 }
1831
1832                                 cur_input_buf_omx=NULL;//write out old data
1833                         }*/
1834                         ffmpeg_time=packet.presentation_time;
1835                         ffmpeg_hastime=true;
1836                 //      reftime1=packet.presentation_time;
1837                 //      reftime2=reftime1+1;
1838                         firstsynched=true;
1839                 } else {
1840                         if (!firstsynched) {//
1841                                 *samplepos=packet.length;//if we have not processed at least one
1842                                 return packet.length;//synched packet ignore it!
1843                         }
1844                 }
1845         }
1846
1847
1848
1849
1850
1851
1852         /*if (cur_input_buf_omx->nFilledLen==0) {//will only be changed on first packet
1853                 /*if (packet.disconti) {
1854                         ms->SetDiscontinuity(TRUE);
1855                 } else {
1856                         ms->SetDiscontinuity(FALSE);
1857                 }*
1858                 //if (packet.synched) {
1859
1860                         //lastreftimePTS=packet.pts;
1861                    if (omx_first_frame) { // TODO time
1862                            cur_input_buf_omx->nFlags=OMX_BUFFERFLAG_STARTTIME;
1863                            omx_first_frame=false;
1864                    } else
1865
1866                 //}
1867                 //else
1868                 //{
1869                         cur_input_buf_omx->nFlags=OMX_BUFFERFLAG_TIME_UNKNOWN;
1870
1871
1872                         //  ms->SetSyncPoint(TRUE);
1873                 //}
1874
1875         }*/
1876         unsigned int haveToCopy=packet.length-*samplepos;
1877         while (haveToCopy>0) {
1878                 int dec_bytes=0;
1879                 int frame_ready=0;
1880
1881         //      Log::getInstance()->log("Video", Log::DEBUG, "Push data to decoder");
1882                 dec_bytes=avcodec_decode_video(mpeg2codec_context_ff, dec_frame_ff_decoding,
1883                                 &frame_ready, buffer+packet.pos_buffer+*samplepos, haveToCopy);
1884                 if (dec_bytes<0) {
1885                         Log::getInstance()->log("Video", Log::DEBUG, "Decoding frame failed %x", dec_bytes);
1886                         return *samplepos;
1887                 }
1888                 *samplepos+=dec_bytes;
1889                 haveToCopy-=dec_bytes;
1890                 if (frame_ready) {
1891                 //      Log::getInstance()->log("Video", Log::DEBUG, "We have a frame push it to osd");
1892
1893                         dec_frame_ff_mutex.Lock();
1894                         ffwidth=mpeg2codec_context_ff->width;
1895                         ffheight=mpeg2codec_context_ff->height;
1896                         ffpixfmt=mpeg2codec_context_ff->pix_fmt;
1897                 //      Log::getInstance()->log("Video", Log::DEBUG, "Frame info %d %d %d",ffwidth,ffheight,ffpixfmt);
1898
1899                         dec_frame_ff_upload_pending.push_back(dec_frame_ff_decoding);
1900                         dec_frame_ff_decoding=NULL;
1901                         if (dec_frame_ff_free.size()>0) {
1902                                 dec_frame_ff_decoding=dec_frame_ff_free.front();
1903                                 dec_frame_ff_free.pop_front();
1904                                 dec_frame_ff_mutex.Unlock();
1905                                 threadSignal();
1906                                 ffmpeg_hastime=false;
1907                         } else {
1908                                 ffmpeg_hastime=false;
1909                                 dec_frame_ff_mutex.Unlock();
1910                                 // No free Buffers
1911                                 return *samplepos;
1912                         }
1913
1914
1915                 }
1916
1917         }
1918         return *samplepos;
1919
1920
1921 }
1922
1923 #endif
1924
1925
1926
1927
1928
1929
1930 void VideoVPEOGL::ResetTimeOffsets()
1931 {
1932 }
1933
1934 bool VideoVPEOGL::displayIFrame(const UCHAR* buffer, UINT length)
1935 {
1936   //write(fdVideo, buffer, length);
1937         if (!iframemode) EnterIframePlayback();
1938 //      WriteOutTS(buffer,length, h264?MPTYPE_VIDEO_H264 :MPTYPE_VIDEO_MPEG2 );
1939  
1940   lastpacketnum=-1;
1941   return true;
1942 }
1943
1944 int VideoVPEOGL::EnterIframePlayback()
1945 {
1946
1947         return 0;
1948 }
1949