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