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