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