2 Copyright 2004-2005 Chris Tallon, 2009 Marten Richter
4 This file is part of VOMP.
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.
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.
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.
21 #include "videovpeogl.h"
23 #include "mtdraspberry.h"
25 #include "osdopengl.h"
30 //A lot of parts of this file are heavily inspired by xbmc omx implementations
32 VideoVPEOGL::VideoVPEOGL()
36 #ifdef VPE_OMX_SUPPORT
40 cur_input_buf_omx=NULL;
41 omx_h264=omx_mpeg2=true;
44 #ifdef VPE_LIBAV_SUPPORT
45 mpeg2codec_context_libav=NULL;
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;
62 VideoVPEOGL::~VideoVPEOGL()
67 int VideoVPEOGL::init(UCHAR tformat)
69 if (initted) return 0;
72 // if ((fdVideo = open("/dev/vdec_dev", O_WRONLY)) < 0) return 0;
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; }
83 /* if (format == PAL) setLetterboxBorder("38");
84 else setLetterboxBorder("31");*/
98 int VideoVPEOGL::initUsingOSDObjects()
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,
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());
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,
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());
131 //egl_surface=i_egl_surface;
132 //egl_context=i_egl_context;
135 #ifdef VPE_OMX_SUPPORT
136 // we are called before the audio
139 if (error!=OMX_ErrorNone) {
140 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX failed %x", error);
144 //our callbacks move to play?
148 #ifdef VPE_LIBAV_SUPPORT
151 if (decoding_mode==VPE_NO_XVMC) {
152 mpeg2codec_libav=avcodec_find_decoder(CODEC_ID_MPEG2VIDEO);
154 mpeg2codec_libav=avcodec_find_decoder(CODEC_ID_MPEG2VIDEO_XVMC);
156 if (mpeg2codec_libav==NULL) {
157 Log::getInstance()->log("Video", Log::DEBUG, "Find libav mpeg2 codec failed");
167 #ifdef VPE_OMX_SUPPORT
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) {
173 Log::getInstance()->log("Video", Log::NOTICE, "eventHandler %x %x %x %x %x",handle,event_type,data1,data2,event_data);
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;
183 VideoVPEOGL *video=(VideoVPEOGL *)getInstance();
184 video->AddOmxEvent(new_event);
186 /* switch (event_type) {
187 case OMX_EventCmdComplete: {
192 return OMX_ErrorNone;
196 void VideoVPEOGL::AddOmxEvent(VPE_OMX_EVENT new_event)
198 omx_event_mutex.Lock();
199 omx_events.push_back(new_event);
200 omx_event_mutex.Unlock();
204 OMX_ERRORTYPE VideoVPEOGL::EmptyBufferDone_OMX(OMX_IN OMX_HANDLETYPE hcomp,OMX_IN OMX_PTR appdata,OMX_IN OMX_BUFFERHEADERTYPE* buffer){
206 Log::getInstance()->log("Video", Log::NOTICE, "EmptyBufferDone");
207 VideoVPEOGL *video=(VideoVPEOGL *)getInstance();
208 video->ReturnEmptyOMXBuffer(buffer);
209 return OMX_ErrorNone;
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();
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;
228 int VideoVPEOGL::shutdown()
230 if (!initted) return 0;
235 #ifdef VPE_OMX_SUPPORT
236 DeAllocateCodecsOMX();
239 #ifdef VPE_LIBAV_SUPPORT
240 DeAllocateCodecsLibav();
242 eglDestroyContext(egl_display,egl_context);
247 int VideoVPEOGL::AllocateYUVOglTexture(VPEOGLFrame* outframe,int width,int height,int stride)
249 Log::getInstance()->log("Video", Log::NOTICE, "Allocate ogl texture");
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);
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);
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;
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;
280 ogl_frame_mutex.Unlock();
284 void VideoVPEOGL::returnOGLFrame(VPEOGLFrame *frame)
286 ogl_frame_mutex.Lock();
288 ogl_frame_outside=false;
289 free_ogl_frames.push_back(frame);
291 ogl_frame_mutex.Unlock();
294 void VideoVPEOGL::threadMethod()
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());
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;
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;
315 ogl_frame_mutex.Lock();
316 if (all_ogl_frames.size()==0) {
317 ogl_frame_mutex.Unlock(); break;
320 if (free_ogl_frames.size()>0) {
324 out_frame=free_ogl_frames.front();
325 free_ogl_frames.pop_front();
326 } else MILLISLEEP(2);
327 ogl_frame_mutex.Unlock();
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;
337 if (out_frame->textures[1]!=0) {
338 glDeleteTextures(1,&out_frame->textures[1]);
339 out_frame->textures[1]=0;
341 if (out_frame->textures[2]!=0) {
342 glDeleteTextures(1,&out_frame->textures[2]);
343 out_frame->textures[2]=0;
345 if (!AllocateYUVOglTexture(out_frame,width,height,dec_frame_libav_uploading->linesize[0])) failed=true;
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);
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]);
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]);
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]);
373 releaseFrameBufUpload((VPE_FrameBuf*)dec_frame_libav_uploading->base[0]);
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
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();
399 if (sleep) threadWaitForSignal();
405 void VideoVPEOGL::threadPostStopCleanup()
407 eglMakeCurrent(egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
408 #ifdef VPE_LIBAV_SUPPORT
409 dec_frame_libav_uploading=NULL;
416 int VideoVPEOGL::setTVsize(UCHAR ttvsize)
420 // Override the aspect ratio usage, temporarily use to set the video chip mode
421 if (!setAspectRatio(tvsize)) { shutdown(); return 0; }
423 if ((fdVideo = open("/dev/vdec_dev", O_WRONLY)) < 0) return 0;
424 if (!setSource()) { shutdown(); return 0; }
425 if (!attachFramebuffer()) { shutdown(); return 0; }
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; }
431 // mode == LETTERBOX is invalid if the TV is widescreen
432 if (tvsize == ASPECT16X9) setMode(NORMAL);
437 int VideoVPEOGL::setDefaultAspect()
439 return setAspectRatio(tvsize);
444 int VideoVPEOGL::setFormat(UCHAR tformat)
446 if (!initted) return 0;
447 if ((tformat != PAL) && (tformat != NTSC)) return 0;
450 // if (ioctl(fdVideo, AV_SET_VID_DISP_FMT, format) != 0) return 0;
466 int VideoVPEOGL::setConnection(UCHAR tconnection)
468 if (!initted) return 0;
469 if ((tconnection != COMPOSITERGB) && (tconnection != SVIDEO)) return 0;
470 connection = tconnection;
472 // if (ioctl(fdVideo, AV_SET_VID_OUTPUT, connection) != 0) return 0;
476 int VideoVPEOGL::setAspectRatio(UCHAR taspectRatio)
478 if (!initted) return 0;
479 if ((taspectRatio != ASPECT4X3) && (taspectRatio != ASPECT16X9)) return 0;
480 aspectRatio = taspectRatio;
482 Log::getInstance()->log("Video", Log::DEBUG, "Setting aspect to %i", aspectRatio);
484 // if (ioctl(fdVideo, AV_SET_VID_RATIO, aspectRatio) != 0) return 0;
488 int VideoVPEOGL::setMode(UCHAR tmode)
490 if (!initted) return 0;
492 if ((tmode == LETTERBOX) && (tvsize == ASPECT16X9)) return 0; // invalid mode
494 if ((tmode != NORMAL) && (tmode != LETTERBOX) && (tmode != UNKNOWN2) && (tmode != QUARTER) && (tmode != EIGHTH)
495 && (tmode != ZOOM) && (tmode != UNKNOWN6)) return 0;
498 // if (ioctl(fdVideo, AV_SET_VID_MODE, mode) != 0) return 0;
502 int VideoVPEOGL::signalOff()
504 // if (ioctl(fdVideo, AV_SET_VID_DENC, 0) != 0) return 0;
508 int VideoVPEOGL::signalOn()
510 // if (ioctl(fdVideo, AV_SET_VID_DENC, 1) != 0) return 0;
514 int VideoVPEOGL::setSource()
516 if (!initted) return 0;
518 // What does this do...
519 // if (ioctl(fdVideo, AV_SET_VID_SRC, 1) != 0) return 0;
523 int VideoVPEOGL::setPosition(int x, int y)
525 if (!initted) return 0;
527 // vid_pos_regs_t pos_d;
531 /* vid_pos_regs_t pos_d;
533 memset(&pos_d, 0, sizeof(pos_d));
558 pos_d.y = 100; // Top left X
559 pos_d.x = 50; // Top left Y
567 // if (ioctl(fdVideo, AV_SET_VID_POSITION, &pos_d) != 0) return 0;
571 int VideoVPEOGL::sync()
573 if (!initted) return 0;
575 // if (ioctl(fdVideo, AV_SET_VID_SYNC, 2) != 0) return 0;
582 int VideoVPEOGL::play()
584 if (!initted) return 0;
586 #ifdef VPE_OMX_SUPPORT
589 if (!omx_h264) doomx=false;
591 if (!omx_mpeg2) doomx=false;
594 if (AllocateCodecsOMX()) {
595 decoding_backend=VPE_DECODER_OMX;
597 // Otherwise fall back to libav
601 Log::getInstance()->log("Video", Log::NOTICE, "Allocate Codecs OMX failed assume h264 unsupported");
604 Log::getInstance()->log("Video", Log::NOTICE, "Allocate Codecs OMX failed assume mpeg2 unsupported");
609 #ifdef VPE_LIBAV_SUPPORT
610 if (AllocateCodecsLibav()) {
611 decoding_backend=VPE_DECODER_libav;
613 // Otherwise fall back to libav
622 #ifdef VPE_OMX_SUPPORT
623 int VideoVPEOGL::AllocateCodecsOMX()
626 static OMX_CALLBACKTYPE callbacks= {&EventHandler_OMX,&EmptyBufferDone_OMX,&FillBufferDone_OMX};
628 Log::getInstance()->log("Video", Log::NOTICE, "Allocate Codecs OMX");
629 //Clock, move later to audio
633 error=OMX_GetHandle(&omx_clock,VPE_OMX_CLOCK,NULL,&callbacks);
636 if (error!=OMX_ErrorNone){
637 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX clock failed %x", error);
638 DeAllocateCodecsOMX();
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();
654 omx_clock_output_port=p_param.nStartPortNumber;
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();
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();
683 error=OMX_GetHandle(&omx_vid_dec,VPE_OMX_H264_DECODER,NULL,&callbacks);
685 error=OMX_GetHandle(&omx_vid_dec,VPE_OMX_MPEG2_DECODER,NULL,&callbacks);
688 if (error!=OMX_ErrorNone){
689 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX video decoder failed %x", error);
690 DeAllocateCodecsOMX();
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();
705 omx_codec_input_port=p_param.nStartPortNumber;
706 omx_codec_output_port=p_param.nStartPortNumber+1;
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();
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;
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();
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();
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();
744 omx_shed_input_port=p_param.nStartPortNumber;
745 omx_shed_output_port=p_param.nStartPortNumber+1;
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();
754 omx_shed_clock_port=p_param.nStartPortNumber;
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();
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();
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();
778 omx_rend_input_port=p_param.nStartPortNumber;
779 //omx_rend_output_port=p_param.nStartPortNumber+1;
782 if (!DisablePort(omx_vid_rend,omx_rend_input_port) /*|| !DisablePort(omx_vid_rend,omx_rend_output_port)*/
784 Log::getInstance()->log("Video", Log::DEBUG, "Disable Ports OMX video rend failed");
785 DeAllocateCodecsOMX();
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);
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;
812 ft_type.nPortIndex=omx_codec_input_port;
814 ft_type.eCompressionFormat=OMX_VIDEO_CodingAVC;
816 ft_type.eCompressionFormat=OMX_VIDEO_CodingMPEG2;
819 Demuxer* demux=Demuxer::getInstance();
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();
831 if (!ChangeComponentState(omx_vid_dec,OMX_StateIdle)) {
832 Log::getInstance()->log("Video", Log::DEBUG, "vid_dec ChangeComponentState");
833 DeAllocateCodecsOMX();
838 if (!PrepareInputBufsOMX()) {
839 DeAllocateCodecsOMX();
844 if (!ChangeComponentState(omx_clock,OMX_StateIdle)) {
845 Log::getInstance()->log("Video", Log::DEBUG, "omx_clock ChangeComponentState Idle");
846 DeAllocateCodecsOMX();
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();
857 if (!EnablePort(omx_clock,omx_clock_output_port,false) || !EnablePort(omx_vid_sched,omx_shed_clock_port,false)
859 Log::getInstance()->log("Video", Log::DEBUG, "Enable Ports OMX clock shed failed");
860 DeAllocateCodecsOMX();
864 if ( !CommandFinished(omx_vid_sched,OMX_CommandPortEnable,omx_shed_clock_port)) {
865 DeAllocateCodecsOMX();
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();
878 if (!EnablePort(omx_vid_dec,omx_codec_output_port,false) || !EnablePort(omx_vid_sched,omx_shed_input_port,false)
880 Log::getInstance()->log("Video", Log::DEBUG, "Enable Ports OMX codec shed failed");
881 DeAllocateCodecsOMX();
885 if ( !CommandFinished(omx_vid_sched,OMX_CommandPortEnable,omx_shed_input_port)) {
888 if (!ChangeComponentState(omx_vid_sched,OMX_StateIdle)) {
889 Log::getInstance()->log("Video", Log::DEBUG, "vid_sched idle ChangeComponentState");
890 DeAllocateCodecsOMX();
893 if (!CommandFinished(omx_clock,OMX_CommandPortEnable,omx_clock_output_port)
894 ||!CommandFinished(omx_vid_dec,OMX_CommandPortEnable,omx_codec_output_port)){
895 DeAllocateCodecsOMX();
899 if (!ChangeComponentState(omx_vid_dec,OMX_StateExecuting)) {
900 Log::getInstance()->log("Video", Log::DEBUG, "omx_vid_dec ChangeComponentState Execute");
901 DeAllocateCodecsOMX();
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();
912 if (!EnablePort(omx_vid_sched,omx_shed_output_port,false) || !EnablePort(omx_vid_rend,omx_rend_input_port,false)
914 Log::getInstance()->log("Video", Log::DEBUG, "Enable Ports OMX shed rend failed");
915 DeAllocateCodecsOMX();
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();
925 if (!ChangeComponentState(omx_vid_rend,OMX_StateIdle)) {
926 Log::getInstance()->log("Video", Log::DEBUG, "vid_rend ChangeComponentState");
927 DeAllocateCodecsOMX();
932 if (!ChangeComponentState(omx_vid_sched,OMX_StateExecuting)) {
933 Log::getInstance()->log("Video", Log::DEBUG, "omx_vid_sched ChangeComponentState Execute");
934 DeAllocateCodecsOMX();
938 if (!ChangeComponentState(omx_vid_rend,OMX_StateExecuting)) {
939 Log::getInstance()->log("Video", Log::DEBUG, "omx_vid_rend ChangeComponentState Execute");
940 DeAllocateCodecsOMX();
945 OMX_CONFIG_DISPLAYREGIONTYPE dispconf;
946 memset(&dispconf,0,sizeof(dispconf));
947 dispconf.nSize=sizeof(dispconf);
948 dispconf.nVersion.nVersion=OMX_VERSION;
950 dispconf.nPortIndex=omx_rend_input_port;
952 dispconf.set=OMX_DISPLAY_SET_LAYER ;
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();
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();
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();
985 if (!ChangeComponentState(omx_clock,OMX_StateExecuting)) {
986 Log::getInstance()->log("Video", Log::DEBUG, "omx_clock ChangeComponentState Exccute");
987 DeAllocateCodecsOMX();
997 int VideoVPEOGL::ChangeComponentState(OMX_HANDLETYPE handle,OMX_STATETYPE type)
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);
1006 if (!CommandFinished(handle,OMX_CommandStateSet,type)) {
1014 int VideoVPEOGL::EnablePort(OMX_HANDLETYPE handle,OMX_U32 port,bool wait)
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);
1023 if (!wait) return 1;
1024 if (!CommandFinished(handle,OMX_CommandPortEnable,port)) {
1032 int VideoVPEOGL::DisablePort(OMX_HANDLETYPE handle,OMX_U32 port,bool wait)
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);
1041 if (!wait) return 1;
1042 if (!CommandFinished(handle,OMX_CommandPortDisable,port)) {
1053 int VideoVPEOGL::CommandFinished(OMX_HANDLETYPE handle,OMX_U32 command,OMX_U32 data2)
1057 omx_event_mutex.Lock();
1058 list<VPE_OMX_EVENT>::iterator itty=omx_events.begin();
1059 while (itty!=omx_events.end()) {
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();
1068 } else if (current.event_type==OMX_EventCmdComplete && current.data1==command && current.data2==data2) {
1069 omx_events.erase(itty);
1070 omx_event_mutex.Unlock();
1077 omx_event_mutex.Unlock();
1082 Log::getInstance()->log("Video", Log::DEBUG, "CommandFinished waited too long %x %x %x",handle,command, data2);
1091 int VideoVPEOGL::PrepareInputBufsOMX()
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;
1100 error=OMX_GetParameter(omx_vid_dec,OMX_IndexParamPortDefinition, &port_def_type);
1102 if (error!=OMX_ErrorNone){
1103 Log::getInstance()->log("Video", Log::DEBUG, "Get OMX OMX_IndexParamPortDefinition failed %x", error);
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);*/
1109 port_def_type.nBufferCountActual=60;
1111 error=OMX_SetParameter(omx_vid_dec,OMX_IndexParamPortDefinition, &port_def_type);
1113 if (error!=OMX_ErrorNone){
1114 Log::getInstance()->log("Video", Log::DEBUG, "Set OMX OMX_IndexParamPortDefinition failed %x", error);
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);
1124 input_bufs_omx_mutex.Lock();
1125 for (unsigned int i=0; i< port_def_type.nBufferCountActual;i++) {
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();
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();
1141 input_bufs_omx_all.push_back(buf_head);
1142 input_bufs_omx_free.push_back(buf_head);
1144 omx_first_frame=true;
1147 cur_input_buf_omx=NULL;
1148 input_bufs_omx_mutex.Unlock();
1151 Log::getInstance()->log("Video", Log::DEBUG, "PrepareInputBufsOMX mark3");
1152 if (!CommandFinished(omx_vid_dec,OMX_CommandPortEnable,omx_codec_input_port)) {
1155 Log::getInstance()->log("Video", Log::DEBUG, "PrepareInputBufsOMX mark4");
1160 int VideoVPEOGL::DestroyInputBufsOMX()
1162 OMX_ERRORTYPE error;
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();
1176 input_bufs_omx_all.clear();
1177 input_bufs_omx_free.clear();
1178 input_bufs_omx_mutex.Unlock();
1185 int VideoVPEOGL::DeAllocateCodecsOMX()
1187 OMX_ERRORTYPE error;
1190 // first flush all buffers
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);
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);
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");
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);
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);
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");
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);
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);
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");
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);
1253 if (!CommandFinished(omx_vid_dec,OMX_CommandFlush,omx_codec_input_port)) {
1254 Log::getInstance()->log("Video", Log::DEBUG, "flush cmd codec input failed");
1257 DestroyInputBufsOMX();
1260 if (!DisablePort(omx_vid_rend,omx_rend_input_port,true)) {
1261 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 1");
1263 if (!DisablePort(omx_vid_sched,omx_shed_output_port,true)) {
1264 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 2 ");
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);
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);
1279 if (!DisablePort(omx_vid_sched,omx_shed_input_port,true)) {
1280 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 3");
1283 if (!DisablePort(omx_vid_sched,omx_shed_clock_port,true)) {
1284 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 4");
1287 if (!DisablePort(omx_clock,omx_clock_output_port,true)) {
1288 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 5");
1290 if (!DisablePort(omx_vid_dec,omx_codec_output_port,true)) {
1291 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 6");
1296 if (!DisablePort(omx_vid_dec,omx_codec_input_port,true)) {
1297 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 7");
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);
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);
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);
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);
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);
1333 if (error!=OMX_ErrorNone) {
1334 Log::getInstance()->log("Video", Log::DEBUG, "FreeHandle failed %d", error);
1344 #ifdef VPE_LIBAV_SUPPORT
1346 enum PixelFormat VideoVPEOGL::get_format_libav(struct AVCodecContext *s, const enum PixelFormat *fmt)
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;
1362 int VideoVPEOGL::reget_buffer_libav(struct AVCodecContext *c, AVFrame *pic)
1364 Log::getInstance()->log("Video", Log::ERR, "Buffer reusing! Should not happen!Not Implemented!");
1368 int VideoVPEOGL::get_buffer_libav(struct AVCodecContext *c, AVFrame *pic)
1370 unsigned int want_sizes[4]={0,0,0,0};
1372 bool normal_pixs=false;
1374 int num_dct_blocks=0;
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!");
1384 if (c->pix_fmt!=PIX_FMT_XVMC_MPEG2_IDCT &&c->pix_fmt!=PIX_FMT_XVMC_MPEG2_MC) {
1386 // standard pixel format
1387 // this is written using much inspiration from libav util.c, so portions from there
1393 avcodec_align_dimensions2(c, &width, &height, s_a);
1394 if ((c->flags & CODEC_FLAG_EMU_EDGE)==0) {
1398 // Now we have to determine alignment size
1399 bool unaligned=true;
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
1404 for (int i=0;i<4;i++) {
1405 if ((pict.linesize[i]%s_a[i])!=0) {
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];
1417 want_sizes[i]=(tot_size-(pict.data[i]-pict.data[0]));
1424 //TODO set linesizes!
1426 num_blocks=((c->width+15)/16)*((c->height+15)/16);
1427 num_dct_blocks=num_blocks*6; //6 blocks per macroblock
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;
1436 VPE_FrameBuf *frame_buf=((VideoVPEOGL*)Video::getInstance())->getFrameBuf(want_sizes);
1438 //Log::getInstance()->log("Video", Log::NOTICE, "get buffer %x",frame_buf);
1440 Log::getInstance()->log("Video", Log::ERR, "Getting buffer libav failed");
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];
1458 edge_width>>=hchr_shift;
1459 edge_height>>=vchr_shift;
1461 pic->data[i]+=FFALIGN((pic->linesize[i]*16) + (pixel_size*edge_width), s_a[i]);
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;
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;
1498 void VideoVPEOGL::release_buffer_libav(struct AVCodecContext *c, AVFrame *pic)
1500 // Log::getInstance()->log("Video", Log::NOTICE, "release buffer %x",pic->base[0]);
1501 ((VideoVPEOGL*)Video::getInstance())->releaseFrameBufLibav((VPE_FrameBuf*) pic->base[0]);
1503 pic->data[0]=pic->data[1]=pic->data[2]=pic->data[3]=NULL;
1508 int VideoVPEOGL::AllocateCodecsLibav()
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");
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;
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;
1529 int avc_ret=avcodec_open(mpeg2codec_context_libav, mpeg2codec_libav);
1531 Log::getInstance()->log("Video", Log::DEBUG, "Opening libav codec failed ");
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);
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");
1545 dec_frame_libav_all.push_back(dec_frame_libav);
1546 dec_frame_libav_free.push_back(dec_frame_libav);
1548 dec_frame_libav_decoding=NULL;
1549 dec_frame_libav_mutex.Unlock();
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);
1564 ogl_frame_outside=false;
1566 ogl_frame_mutex.Unlock();
1574 int VideoVPEOGL::DeAllocateCodecsLibav()
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");
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]);
1591 dec_frame_libav_all.clear();
1593 av_free(incom_packet_libav.data);
1594 incom_packet_libav.data=NULL;
1595 incom_packet_libav_size=0;
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");
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;
1612 if (del_frame->textures[1]==0) {
1613 glDeleteTextures(1,&del_frame->textures[1]);
1614 del_frame->textures[1]=0;
1616 if (del_frame->textures[2]==0) {
1617 glDeleteTextures(1,&del_frame->textures[2]);
1618 del_frame->textures[2]=0;
1620 free(all_ogl_frames[i]);
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();
1629 if (mpeg2codec_context_libav) {
1630 avcodec_close(mpeg2codec_context_libav);
1631 av_free(mpeg2codec_context_libav);
1632 mpeg2codec_context_libav=NULL;
1638 vpe_framebuf_mutex.Lock();
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]);
1649 all_frame_bufs.clear();
1650 free_frame_bufs.clear();
1651 locked_libav_frame_buf.clear();
1652 locked_uploading_frame_buf.clear();
1654 vpe_framebuf_mutex.Unlock();
1661 VPE_FrameBuf *VideoVPEOGL::getFrameBuf(unsigned int *size)
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));
1672 Log::getInstance()->log("Video", Log::NOTICE, "Framebuffer underrun!");
1673 vpe_framebuf_mutex.Unlock();
1674 return NULL; // We do not have a frame buffer
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];
1685 framebuf_framenum++;
1686 current->pict_num=framebuf_framenum; //This is used for tracking reference frames through the conversion pipeline
1691 void VideoVPEOGL::lockFrameBufUpload(VPE_FrameBuf* buf)
1693 // first find frame_buf memory
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();
1704 void VideoVPEOGL::releaseFrameBufLibav(VPE_FrameBuf* buf)
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();
1711 locked_libav_frame_buf.remove(current); //unlocked
1712 list<VPE_FrameBuf*>::iterator itty=locked_uploading_frame_buf.begin();
1714 while (itty!=locked_uploading_frame_buf.end()) {
1715 if (*itty==current) {
1722 free_frame_bufs.push_back(current);
1725 vpe_framebuf_mutex.Unlock();
1728 void VideoVPEOGL::releaseFrameBufUpload(VPE_FrameBuf* buf)
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();
1735 locked_uploading_frame_buf.remove(current); //unlocked
1736 list<VPE_FrameBuf*>::iterator itty=locked_libav_frame_buf.begin();
1738 while (itty!=locked_libav_frame_buf.end()) {
1739 if (*itty==current) {
1746 free_frame_bufs.push_back(current);
1749 vpe_framebuf_mutex.Unlock();
1759 int VideoVPEOGL::stop()
1761 if (!initted) return 0;
1763 #ifdef VPE_OMX_SUPPORT
1764 //Check if libav mode
1765 if (decoding_backend==VPE_DECODER_OMX) DeAllocateCodecsOMX();
1772 // if (ioctl(fdVideo, AV_SET_VID_STOP, 0) != 0) return 0;
1776 int VideoVPEOGL::reset()
1778 if (!initted) return 0;
1780 // if (ioctl(fdVideo, AV_SET_VID_RESET, 0x11) != 0) return 0;
1784 int VideoVPEOGL::pause()
1786 if (!initted) return 0;
1788 // if (ioctl(fdVideo, AV_SET_VID_PAUSE, 0) != 0) return 0;
1792 int VideoVPEOGL::unPause() // FIXME get rid - same as play!!
1794 if (!initted) return 0;
1795 // if (ioctl(fdVideo, AV_SET_VID_PLAY, 0) != 0) return 0;
1799 int VideoVPEOGL::fastForward()
1801 if (!initted) return 0;
1803 // if (ioctl(fdVideo, AV_SET_VID_libavWD, 1) != 0) return 0;
1807 int VideoVPEOGL::unFastForward()
1809 if (!initted) return 0;
1811 // if (ioctl(fdVideo, AV_SET_VID_RESET, 0x11) != 0) return 0; // don't need this.
1813 //// if (ioctl(fdVideo, AV_SET_VID_PLAY, 0) != 0) return 0;
1817 int VideoVPEOGL::attachFrameBuffer()
1819 if (!initted) return 0;
1821 // if (ioctl(fdVideo, AV_SET_VID_FB, 0) != 0) return 0;
1825 int VideoVPEOGL::blank(void)
1827 // if (ioctl(fdVideo, AV_SET_VID_FB, 1) != 0) return 0;
1828 // if (ioctl(fdVideo, AV_SET_VID_FB, 0) != 0) return 0;
1832 ULLONG VideoVPEOGL::getCurrentTimestamp()
1834 /* sync_data_t timestamps;
1835 if (ioctl(fdVideo, AV_GET_VID_TIMESTAMPS, ×tamps) == 0)
1837 // FIXME are these the right way around?
1839 timestamps.stc = (timestamps.stc >> 31 ) | (timestamps.stc & 1);
1840 timestamps.pts = (timestamps.pts >> 31 ) | (timestamps.pts & 1);
1842 return timestamps.stc;
1851 ULONG VideoVPEOGL::timecodeToFrameNumber(ULLONG timecode)
1853 if (format == PAL) return (ULONG)(((double)timecode / (double)90000) * (double)25);
1854 else return (ULONG)(((double)timecode / (double)90000) * (double)30);
1858 int VideoVPEOGL::test()
1863 // return ioctl(fdVideo, AV_SET_VID_STC, &stc);
1870 int VideoVPEOGL::test2()
1876 void VideoVPEOGL::PrepareMediaSample(const MediaPacketList& mplist,UINT samplepos)
1878 mediapacket = mplist.front();
1881 UINT VideoVPEOGL::DeliverMediaSample(UCHAR* buffer, UINT *samplepos)
1883 DeliverMediaPacket(mediapacket, buffer, samplepos);
1884 if (*samplepos == mediapacket.length) {
1891 UINT VideoVPEOGL::DeliverMediaPacket(MediaPacket packet,
1892 const UCHAR* buffer,
1895 if (packet.type == MPTYPE_VIDEO_H264)
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);
1908 #ifdef VPE_LIBAV_SUPPORT
1909 case VPE_DECODER_libav: return DeliverMediaPacketlibav(packet,buffer,samplepos);
1914 #ifdef VPE_OMX_SUPPORT
1915 UINT VideoVPEOGL::DeliverMediaPacketOMX(MediaPacket packet,
1916 const UCHAR* buffer,
1921 //Later add fail back code for libav
1923 *samplepos+=packet.length;
1924 return packet.length;
1928 if (!omx_running) return 0; // if we are not runnig do not do this
1931 OMX_ERRORTYPE error;
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);
1942 Log::getInstance()->log("Video", Log::DEBUG, "Image port %d %d", port_image.format.video.nFrameWidth , port_image.format.video.nFrameHeight);*/
1944 /*First Check, if we have an audio sample*/
1948 return 0; //Not in iframe mode!
1952 if (packet.disconti) {
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);
1959 cur_input_buf_omx=NULL;
1963 /*Inspect PES-Header */
1965 // OMX_STATETYPE temp_state;
1966 // OMX_GetState(omx_vid_dec,&temp_state);
1968 if (*samplepos==0) {//stripheader
1969 headerstrip=buffer[packet.pos_buffer+8]+9/*is this right*/;
1970 *samplepos+=headerstrip;
1971 if ( packet.synched ) {
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);
1979 cur_input_buf_omx=NULL;//write out old data
1981 // reftime1=packet.presentation_time;
1982 // reftime2=reftime1+1;
1985 if (!firstsynched) {//
1986 *samplepos=packet.length;//if we have not processed at least one
1987 return packet.length;//synched packet ignore it!
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
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();
2011 if (cur_input_buf_omx->nFilledLen==0) {//will only be changed on first packet
2012 /*if (packet.disconti) {
2013 ms->SetDiscontinuity(TRUE);
2015 ms->SetDiscontinuity(FALSE);
2017 //if (packet.synched) {
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;
2028 cur_input_buf_omx->nFlags=OMX_BUFFERFLAG_TIME_UNKNOWN;
2031 // ms->SetSyncPoint(TRUE);
2035 unsigned int haveToCopy=packet.length-*samplepos;
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
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);
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
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();
2063 cur_input_buf_omx->nFlags=OMX_BUFFERFLAG_TIME_UNKNOWN;
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;
2072 *samplepos+=haveToCopy;
2081 #ifdef VPE_LIBAV_SUPPORT
2082 int VideoVPEOGL::DecodePacketlibav()
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) {
2090 // Log::getInstance()->log("Video", Log::DEBUG, "Push data to decoder");
2092 #ifdef BENCHMARK_FPS
2093 int cur_time=getTimeMS();
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);
2107 Log::getInstance()->log("Video", Log::DEBUG, "Decoding frame failed %x", dec_bytes);
2110 haveToCopy-=dec_bytes;
2112 // Log::getInstance()->log("Video", Log::DEBUG, "We have a frame push it to osd");
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);
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();
2129 libav_hastime=false;
2131 libav_hastime=false;
2132 dec_frame_libav_mutex.Unlock();
2141 incom_packet_libav.size=0;
2147 UINT VideoVPEOGL::DeliverMediaPacketlibav(MediaPacket packet,
2148 const UCHAR* buffer,
2151 //Later add fail back code for libav
2153 *samplepos+=packet.length;
2154 return packet.length;
2157 if (!libav_running) return 0; // if we are not runnig do not do this
2163 return 0; //Not in iframe mode!
2167 if (packet.disconti) {
2169 if (!DecodePacketlibav()) return 0;
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();
2180 Log::getInstance()->log("Video", Log::DEBUG, "We have no free buffers");
2181 dec_frame_libav_mutex.Unlock();
2189 if (*samplepos==0) {//stripheader
2190 headerstrip=buffer[packet.pos_buffer+8]+9/*is this right*/;
2191 *samplepos+=headerstrip;
2192 if ( packet.synched ) {
2194 if (!DecodePacketlibav()) return 0; // WriteOut old Data
2196 libav_time=packet.presentation_time;
2198 // reftime1=packet.presentation_time;
2199 // reftime2=reftime1+1;
2202 if (!firstsynched) {//
2203 *samplepos=packet.length;//if we have not processed at least one
2204 return packet.length;//synched packet ignore it!
2214 /*if (cur_input_buf_omx->nFilledLen==0) {//will only be changed on first packet
2215 /*if (packet.disconti) {
2216 ms->SetDiscontinuity(TRUE);
2218 ms->SetDiscontinuity(FALSE);
2220 //if (packet.synched) {
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;
2231 cur_input_buf_omx->nFlags=OMX_BUFFERFLAG_TIME_UNKNOWN;
2234 // ms->SetSyncPoint(TRUE);
2238 unsigned int haveToCopy=packet.length-*samplepos;
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);
2246 memcpy(incom_packet_libav.data+incom_packet_libav.size,buffer+packet.pos_buffer+*samplepos,haveToCopy);
2247 incom_packet_libav.size+=haveToCopy;
2249 *samplepos+=haveToCopy;
2264 void VideoVPEOGL::ResetTimeOffsets()
2268 bool VideoVPEOGL::displayIFrame(const UCHAR* buffer, UINT length)
2270 //write(fdVideo, buffer, length);
2271 if (!iframemode) EnterIframePlayback();
2272 // WriteOutTS(buffer,length, h264?MPTYPE_VIDEO_H264 :MPTYPE_VIDEO_MPEG2 );
2278 int VideoVPEOGL::EnterIframePlayback()