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;
52 //decoding_mode=VPE_XVMC_IDCT;
64 VideoVPEOGL::~VideoVPEOGL()
69 int VideoVPEOGL::init(UCHAR tformat)
71 if (initted) return 0;
74 // if ((fdVideo = open("/dev/vdec_dev", O_WRONLY)) < 0) return 0;
76 if (!setFormat(tformat)) { shutdown(); return 0; }
77 if (!setConnection(COMPOSITERGB)) { shutdown(); return 0; }
78 if (!setAspectRatio(ASPECT4X3)) { shutdown(); return 0; }
79 if (!setMode(NORMAL)) { shutdown(); return 0; }
80 if (!setSource()) { shutdown(); return 0; }
81 if (!attachFrameBuffer()) { shutdown(); return 0; }
85 /* if (format == PAL) setLetterboxBorder("38");
86 else setLetterboxBorder("31");*/
100 int VideoVPEOGL::initUsingOSDObjects()
102 EGLDisplay i_egl_display;
103 EGLSurface i_egl_surface;
104 EGLContext i_egl_context;
105 EGLConfig i_egl_config;
106 OsdOpenGL *osd=(OsdOpenGL*)osd->getInstance();
107 osd->getEGLObjs(&i_egl_display,&i_egl_surface,&i_egl_context, &i_egl_config);
108 const EGLint attr_context[]={
109 EGL_CONTEXT_CLIENT_VERSION,2,
113 egl_display=i_egl_display;
114 egl_context=eglCreateContext(egl_display,i_egl_config,i_egl_context,attr_context);
115 if (egl_context==EGL_NO_CONTEXT) {
116 Log::getInstance()->log("Video", Log::WARN, "Creating egl context failed! %d",eglGetError());
119 // We create a dummy surface here, in order to allow two contexts
120 const EGLint attr_pbuffer[]={
121 EGL_WIDTH, 1, EGL_HEIGHT,1,
124 egl_surface=eglCreatePbufferSurface(egl_display,i_egl_config,attr_pbuffer);
125 if (egl_surface==EGL_NO_SURFACE) {
126 Log::getInstance()->log("Video", Log::WARN, "Creating egl pbuffer failed! %d",eglGetError());
133 //egl_surface=i_egl_surface;
134 //egl_context=i_egl_context;
137 #ifdef VPE_OMX_SUPPORT
138 // we are called before the audio
141 if (error!=OMX_ErrorNone) {
142 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX failed %x", error);
146 //our callbacks move to play?
150 #ifdef VPE_LIBAV_SUPPORT
153 if (decoding_mode==VPE_NO_XVMC) {
154 mpeg2codec_libav=avcodec_find_decoder(CODEC_ID_MPEG2VIDEO);
156 mpeg2codec_libav=avcodec_find_decoder(CODEC_ID_MPEG2VIDEO_XVMC);
158 if (mpeg2codec_libav==NULL) {
159 Log::getInstance()->log("Video", Log::DEBUG, "Find libav mpeg2 codec failed");
169 #ifdef VPE_OMX_SUPPORT
171 OMX_ERRORTYPE VideoVPEOGL::EventHandler_OMX(OMX_IN OMX_HANDLETYPE handle,OMX_IN OMX_PTR appdata,
172 OMX_IN OMX_EVENTTYPE event_type,OMX_IN OMX_U32 data1,
173 OMX_IN OMX_U32 data2,OMX_IN OMX_PTR event_data) {
175 Log::getInstance()->log("Video", Log::NOTICE, "eventHandler %x %x %x %x %x",handle,event_type,data1,data2,event_data);
177 struct VPE_OMX_EVENT new_event;
178 new_event.handle=handle;
179 new_event.appdata=appdata;
180 new_event.event_type=event_type;
181 new_event.data1=data1;
182 new_event.data2=data2;
183 new_event.event_data=event_data;
185 VideoVPEOGL *video=(VideoVPEOGL *)getInstance();
186 video->AddOmxEvent(new_event);
188 /* switch (event_type) {
189 case OMX_EventCmdComplete: {
194 return OMX_ErrorNone;
198 void VideoVPEOGL::AddOmxEvent(VPE_OMX_EVENT new_event)
200 omx_event_mutex.Lock();
201 omx_events.push_back(new_event);
202 omx_event_mutex.Unlock();
206 OMX_ERRORTYPE VideoVPEOGL::EmptyBufferDone_OMX(OMX_IN OMX_HANDLETYPE hcomp,OMX_IN OMX_PTR appdata,OMX_IN OMX_BUFFERHEADERTYPE* buffer){
208 Log::getInstance()->log("Video", Log::NOTICE, "EmptyBufferDone");
209 VideoVPEOGL *video=(VideoVPEOGL *)getInstance();
210 video->ReturnEmptyOMXBuffer(buffer);
211 return OMX_ErrorNone;
215 void VideoVPEOGL::ReturnEmptyOMXBuffer(OMX_BUFFERHEADERTYPE* buffer){
216 input_bufs_omx_mutex.Lock();
217 Log::getInstance()->log("Video", Log::NOTICE, "ReturnEmptyOMXBuffer %d",input_bufs_omx_free.size());
218 input_bufs_omx_free.push_back(buffer);
219 Log::getInstance()->log("Video", Log::NOTICE, "ReturnEmptyOMXBuffer %d",input_bufs_omx_free.size());
220 input_bufs_omx_mutex.Unlock();
223 OMX_ERRORTYPE VideoVPEOGL::FillBufferDone_OMX(OMX_IN OMX_HANDLETYPE hcomp, OMX_IN OMX_PTR appdata,OMX_IN OMX_BUFFERHEADERTYPE* buffer) {
224 Log::getInstance()->log("Video", Log::NOTICE, "FillBufferDone");
225 return OMX_ErrorNone;
230 int VideoVPEOGL::shutdown()
232 if (!initted) return 0;
237 #ifdef VPE_OMX_SUPPORT
238 DeAllocateCodecsOMX();
241 #ifdef VPE_LIBAV_SUPPORT
242 DeAllocateCodecsLibav();
244 eglDestroyContext(egl_display,egl_context);
249 int VideoVPEOGL::AllocateYUV400OglTexture(VPEOGLFrame* outframe,int width,int height,int stride)
251 Log::getInstance()->log("Video", Log::NOTICE, "Allocate ogl texture 400");
253 glGenTextures(1, &outframe->textures[0]);
254 glBindTexture(GL_TEXTURE_2D, outframe->textures[0]);
255 glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, stride, height, 0, GL_LUMINANCE,
256 GL_UNSIGNED_BYTE, NULL);
258 glGenTextures(1, &outframe->textures[1]);
259 glBindTexture(GL_TEXTURE_2D, outframe->textures[1]);
260 glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, stride>>1, height>>1, 0, GL_LUMINANCE,
261 GL_UNSIGNED_BYTE, NULL);
263 glGenTextures(1, &outframe->textures[2]);
264 glBindTexture(GL_TEXTURE_2D, outframe->textures[2]);
265 glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, stride>>1, height>>1, 0, GL_LUMINANCE,
266 GL_UNSIGNED_BYTE, NULL);
267 outframe->height=height;
268 outframe->width=width;
269 outframe->stride=stride;
274 int VideoVPEOGL::AllocateYUV444OglTexture(VPEOGLFrame* outframe,int width,int height,int stride)
276 Log::getInstance()->log("Video", Log::NOTICE, "Allocate ogl texture 444");
278 glGenTextures(1, &outframe->textures[0]);
279 glBindTexture(GL_TEXTURE_2D, outframe->textures[0]);
280 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, stride, height, 0, GL_RGBA,
281 GL_UNSIGNED_BYTE, NULL);
282 outframe->textures[1]=outframe->textures[2]=0; // no handles here
288 outframe->height=height;
289 outframe->width=width;
290 outframe->stride=stride;
295 VPEOGLFrame *VideoVPEOGL::getReadyOGLFrame(){
296 VPEOGLFrame *return_obj=NULL;
297 ogl_frame_mutex.Lock();
298 if (ready_ogl_frames.size()>0) {
299 return_obj=ready_ogl_frames.front();
300 ready_ogl_frames.pop_front();
301 ogl_frame_outside=true;
303 ogl_frame_mutex.Unlock();
307 void VideoVPEOGL::returnOGLFrame(VPEOGLFrame *frame)
309 ogl_frame_mutex.Lock();
311 ogl_frame_outside=false;
312 free_ogl_frames.push_back(frame);
314 ogl_frame_mutex.Unlock();
317 void VideoVPEOGL::threadMethod()
319 if (eglMakeCurrent(egl_display, egl_surface, egl_surface, egl_context)== EGL_FALSE) {
320 Log::getInstance()->log("Video", Log::WARN, "Making egl Current failed in thread %d",eglGetError());
325 #ifdef VPE_LIBAV_SUPPORT
326 dec_frame_libav_mutex.Lock();
327 if (dec_frame_libav_upload_pending.size()>0) {
328 dec_frame_libav_uploading=dec_frame_libav_upload_pending.front();
329 dec_frame_libav_upload_pending.pop_front();
330 if (dec_frame_libav_upload_pending.size()>0) sleep=false;
332 dec_frame_libav_mutex.Unlock();
333 if (dec_frame_libav_uploading) {
334 int width,height,pixfmt;
335 //First get a free ogl image
336 VPEOGLFrame* out_frame=NULL;
338 ogl_frame_mutex.Lock();
339 if (all_ogl_frames.size()==0) {
340 ogl_frame_mutex.Unlock(); break;
343 if (free_ogl_frames.size()>0) {
347 out_frame=free_ogl_frames.front();
348 free_ogl_frames.pop_front();
349 } else MILLISLEEP(2);
350 ogl_frame_mutex.Unlock();
354 if (out_frame->textures[0]==0 || out_frame->width!=width ||
355 out_frame->height!=height || out_frame->stride!=dec_frame_libav_uploading->linesize[0]) {
356 if (out_frame->textures[0]!=0) {
357 glDeleteTextures(1,&out_frame->textures[0]);
358 out_frame->textures[0]=0;
360 if (out_frame->textures[1]!=0) {
361 glDeleteTextures(1,&out_frame->textures[1]);
362 out_frame->textures[1]=0;
364 if (out_frame->textures[2]!=0) {
365 glDeleteTextures(1,&out_frame->textures[2]);
366 out_frame->textures[2]=0;
369 if (decoding_mode==VPE_NO_XVMC) {
370 if (!AllocateYUV400OglTexture(out_frame,width,height,dec_frame_libav_uploading->linesize[0])) failed=true;
372 if (!AllocateYUV444OglTexture(out_frame,width,height,dec_frame_libav_uploading->linesize[0])) failed=true; //We use a YUV 444 texture in this case
373 // the shaders are easier to implement
377 //up to now only YUV data, this is for reference only, since the pi is too slow.
378 if (decoding_mode==VPE_NO_XVMC) {
379 glBindTexture(GL_TEXTURE_2D, out_frame->textures[0]);
380 glPixelStorei(GL_UNPACK_ALIGNMENT,1);
381 glTexSubImage2D(GL_TEXTURE_2D,0,0,0,
382 out_frame->stride,out_frame->height,
383 GL_LUMINANCE,GL_UNSIGNED_BYTE,
384 dec_frame_libav_uploading->data[0]);
386 glBindTexture(GL_TEXTURE_2D, out_frame->textures[1]);
387 glPixelStorei(GL_UNPACK_ALIGNMENT,1);
388 glTexSubImage2D(GL_TEXTURE_2D,0,0,0,
389 out_frame->stride>>1,out_frame->height>>1,
390 GL_LUMINANCE,GL_UNSIGNED_BYTE,
391 dec_frame_libav_uploading->data[1]);
393 glBindTexture(GL_TEXTURE_2D, out_frame->textures[2]);
394 glPixelStorei(GL_UNPACK_ALIGNMENT,1);
395 glTexSubImage2D(GL_TEXTURE_2D,0,0,0,
396 out_frame->stride>>1,out_frame->height>>1,
397 GL_LUMINANCE,GL_UNSIGNED_BYTE,
398 dec_frame_libav_uploading->data[2]);
401 xvmc_pix_fmt * pix_fmt=(xvmc_pix_fmt *)dec_frame_libav_uploading->data[2];
402 if (moco_shader && pix_fmt) {
405 moco_shader->uploadDataBlocks(pix_fmt->data_blocks,pix_fmt->next_free_data_block_num,
406 pix_fmt->mv_blocks , pix_fmt->filled_mv_blocks_num );
407 if (decoding_mode==VPE_XVMC_MOCOMP)
415 // Excute motion compensation
420 releaseFrameBufUpload((VPE_FrameBuf*)dec_frame_libav_uploading->base[0]);
423 ogl_frame_mutex.Lock();
424 ready_ogl_frames.push_back(out_frame);
425 ogl_frame_mutex.Unlock();
426 ((OsdOpenGL*)Osd::getInstance())->AdviseAboutNewFrame(); //Tell him, that we have a frame waiting
430 dec_frame_libav_mutex.Lock();
431 dec_frame_libav_free.push_back(dec_frame_libav_uploading);
432 dec_frame_libav_uploading=NULL;
433 dec_frame_libav_mutex.Unlock();
446 if (sleep) threadWaitForSignal();
452 void VideoVPEOGL::threadPostStopCleanup()
454 eglMakeCurrent(egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
455 #ifdef VPE_LIBAV_SUPPORT
456 dec_frame_libav_uploading=NULL;
463 int VideoVPEOGL::setTVsize(UCHAR ttvsize)
467 // Override the aspect ratio usage, temporarily use to set the video chip mode
468 if (!setAspectRatio(tvsize)) { shutdown(); return 0; }
470 if ((fdVideo = open("/dev/vdec_dev", O_WRONLY)) < 0) return 0;
471 if (!setSource()) { shutdown(); return 0; }
472 if (!attachFramebuffer()) { shutdown(); return 0; }
474 // Reopening the fd causes the scart aspect line to go back to 4:3
475 // Set this again to the same as the tv screen size
476 if (!setAspectRatio(tvsize)) { shutdown(); return 0; }
478 // mode == LETTERBOX is invalid if the TV is widescreen
479 if (tvsize == ASPECT16X9) setMode(NORMAL);
484 int VideoVPEOGL::setDefaultAspect()
486 return setAspectRatio(tvsize);
491 int VideoVPEOGL::setFormat(UCHAR tformat)
493 if (!initted) return 0;
494 if ((tformat != PAL) && (tformat != NTSC)) return 0;
497 // if (ioctl(fdVideo, AV_SET_VID_DISP_FMT, format) != 0) return 0;
513 int VideoVPEOGL::setConnection(UCHAR tconnection)
515 if (!initted) return 0;
516 if ((tconnection != COMPOSITERGB) && (tconnection != SVIDEO)) return 0;
517 connection = tconnection;
519 // if (ioctl(fdVideo, AV_SET_VID_OUTPUT, connection) != 0) return 0;
523 int VideoVPEOGL::setAspectRatio(UCHAR taspectRatio)
525 if (!initted) return 0;
526 if ((taspectRatio != ASPECT4X3) && (taspectRatio != ASPECT16X9)) return 0;
527 aspectRatio = taspectRatio;
529 Log::getInstance()->log("Video", Log::DEBUG, "Setting aspect to %i", aspectRatio);
531 // if (ioctl(fdVideo, AV_SET_VID_RATIO, aspectRatio) != 0) return 0;
535 int VideoVPEOGL::setMode(UCHAR tmode)
537 if (!initted) return 0;
539 if ((tmode == LETTERBOX) && (tvsize == ASPECT16X9)) return 0; // invalid mode
541 if ((tmode != NORMAL) && (tmode != LETTERBOX) && (tmode != UNKNOWN2) && (tmode != QUARTER) && (tmode != EIGHTH)
542 && (tmode != ZOOM) && (tmode != UNKNOWN6)) return 0;
545 // if (ioctl(fdVideo, AV_SET_VID_MODE, mode) != 0) return 0;
549 int VideoVPEOGL::signalOff()
551 // if (ioctl(fdVideo, AV_SET_VID_DENC, 0) != 0) return 0;
555 int VideoVPEOGL::signalOn()
557 // if (ioctl(fdVideo, AV_SET_VID_DENC, 1) != 0) return 0;
561 int VideoVPEOGL::setSource()
563 if (!initted) return 0;
565 // What does this do...
566 // if (ioctl(fdVideo, AV_SET_VID_SRC, 1) != 0) return 0;
570 int VideoVPEOGL::setPosition(int x, int y)
572 if (!initted) return 0;
574 // vid_pos_regs_t pos_d;
578 /* vid_pos_regs_t pos_d;
580 memset(&pos_d, 0, sizeof(pos_d));
605 pos_d.y = 100; // Top left X
606 pos_d.x = 50; // Top left Y
614 // if (ioctl(fdVideo, AV_SET_VID_POSITION, &pos_d) != 0) return 0;
618 int VideoVPEOGL::sync()
620 if (!initted) return 0;
622 // if (ioctl(fdVideo, AV_SET_VID_SYNC, 2) != 0) return 0;
629 int VideoVPEOGL::play()
631 if (!initted) return 0;
633 #ifdef VPE_OMX_SUPPORT
636 if (!omx_h264) doomx=false;
638 if (!omx_mpeg2) doomx=false;
641 if (AllocateCodecsOMX()) {
642 decoding_backend=VPE_DECODER_OMX;
644 // Otherwise fall back to libav
648 Log::getInstance()->log("Video", Log::NOTICE, "Allocate Codecs OMX failed assume h264 unsupported");
651 Log::getInstance()->log("Video", Log::NOTICE, "Allocate Codecs OMX failed assume mpeg2 unsupported");
656 #ifdef VPE_LIBAV_SUPPORT
657 if (AllocateCodecsLibav()) {
658 decoding_backend=VPE_DECODER_libav;
660 // Otherwise fall back to libav
669 #ifdef VPE_OMX_SUPPORT
670 int VideoVPEOGL::AllocateCodecsOMX()
673 static OMX_CALLBACKTYPE callbacks= {&EventHandler_OMX,&EmptyBufferDone_OMX,&FillBufferDone_OMX};
675 Log::getInstance()->log("Video", Log::NOTICE, "Allocate Codecs OMX");
676 //Clock, move later to audio
680 error=OMX_GetHandle(&omx_clock,VPE_OMX_CLOCK,NULL,&callbacks);
683 if (error!=OMX_ErrorNone){
684 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX clock failed %x", error);
685 DeAllocateCodecsOMX();
691 OMX_PORT_PARAM_TYPE p_param;
692 memset(&p_param,0,sizeof(p_param));
693 p_param.nSize=sizeof(p_param);
694 p_param.nVersion.nVersion=OMX_VERSION;
695 error=OMX_GetParameter(omx_clock,OMX_IndexParamOtherInit,&p_param);
696 if (error!=OMX_ErrorNone){
697 Log::getInstance()->log("Video", Log::DEBUG, "Init clock OMX_GetParameter failed %x", error);
698 DeAllocateCodecsOMX();
701 omx_clock_output_port=p_param.nStartPortNumber;
703 for (unsigned int i=0;i<p_param.nPorts;i++) {
704 if (!DisablePort(omx_clock,p_param.nStartPortNumber+i) ) {
705 Log::getInstance()->log("Video", Log::DEBUG, "Disable Ports OMX clock failed %d",i);
706 DeAllocateCodecsOMX();
712 OMX_TIME_CONFIG_CLOCKSTATETYPE clock_conf;
713 memset(&clock_conf,0,sizeof(clock_conf));
714 clock_conf.nSize=sizeof(clock_conf);
715 clock_conf.nVersion.nVersion=OMX_VERSION;
716 clock_conf.eState=OMX_TIME_ClockStateWaitingForStartTime;
717 clock_conf.nStartTime=0;
718 clock_conf.nOffset=0;
719 clock_conf.nWaitMask=OMX_CLOCKPORT0;
720 error=OMX_SetParameter(omx_clock,OMX_IndexConfigTimeClockState,&clock_conf);
721 if (error!=OMX_ErrorNone){
722 Log::getInstance()->log("Video", Log::DEBUG, "Clock IndexConfigTimeClockState failed %x", error);
723 DeAllocateCodecsOMX();
730 error=OMX_GetHandle(&omx_vid_dec,VPE_OMX_H264_DECODER,NULL,&callbacks);
732 error=OMX_GetHandle(&omx_vid_dec,VPE_OMX_MPEG2_DECODER,NULL,&callbacks);
735 if (error!=OMX_ErrorNone){
736 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX video decoder failed %x", error);
737 DeAllocateCodecsOMX();
743 memset(&p_param,0,sizeof(p_param));
744 p_param.nSize=sizeof(p_param);
745 p_param.nVersion.nVersion=OMX_VERSION;
746 error=OMX_GetParameter(omx_vid_dec,OMX_IndexParamVideoInit,&p_param);
747 if (error!=OMX_ErrorNone){
748 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX h264 decoder OMX_GetParameter failed %x", error);
749 DeAllocateCodecsOMX();
752 omx_codec_input_port=p_param.nStartPortNumber;
753 omx_codec_output_port=p_param.nStartPortNumber+1;
755 if (!DisablePort(omx_vid_dec,omx_codec_input_port) || !DisablePort(omx_vid_dec,omx_codec_output_port)) {
756 Log::getInstance()->log("Video", Log::DEBUG, "Disable Ports OMX video decoder failed");
757 DeAllocateCodecsOMX();
762 OMX_PARAM_BRCMVIDEODECODEERRORCONCEALMENTTYPE conceal;
763 memset(&conceal,0,sizeof(conceal));
764 conceal.nSize=sizeof(conceal);
765 conceal.nVersion.nVersion=OMX_VERSION;
766 conceal.bStartWithValidFrame=OMX_FALSE;
768 error=OMX_SetParameter(omx_vid_dec,OMX_IndexParamBrcmVideoDecodeErrorConcealment,&conceal);
769 if (error!=OMX_ErrorNone){
770 Log::getInstance()->log("Video", Log::DEBUG, "OMX_IndexParamBrcmVideoDecodeErrorConcealment failed %x", error);
771 DeAllocateCodecsOMX();
776 error=OMX_GetHandle(&omx_vid_sched,VPE_OMX_VIDEO_SCHED,NULL,&callbacks);
777 if (error!=OMX_ErrorNone){
778 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX video scheduler failed %x", error);
779 DeAllocateCodecsOMX();
785 error=OMX_GetParameter(omx_vid_sched,OMX_IndexParamVideoInit,&p_param);
786 if (error!=OMX_ErrorNone){
787 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX video scheduler OMX_GetParameter failed %x", error);
788 DeAllocateCodecsOMX();
791 omx_shed_input_port=p_param.nStartPortNumber;
792 omx_shed_output_port=p_param.nStartPortNumber+1;
795 error=OMX_GetParameter(omx_vid_sched,OMX_IndexParamOtherInit,&p_param);
796 if (error!=OMX_ErrorNone){
797 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX video scheduler OMX_GetParameter failed %x", error);
798 DeAllocateCodecsOMX();
801 omx_shed_clock_port=p_param.nStartPortNumber;
804 if (!DisablePort(omx_vid_sched,omx_shed_input_port) || !DisablePort(omx_vid_sched,omx_shed_output_port)
805 || !DisablePort(omx_vid_sched,omx_shed_clock_port)) {
806 Log::getInstance()->log("Video", Log::DEBUG, "Disable Ports OMX video shed failed");
807 DeAllocateCodecsOMX();
812 error=OMX_GetHandle(&omx_vid_rend,VPE_OMX_VIDEO_REND,NULL,&callbacks);
813 if (error!=OMX_ErrorNone){
814 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX video rend failed %x", error);
815 DeAllocateCodecsOMX();
819 error=OMX_GetParameter(omx_vid_rend,OMX_IndexParamVideoInit,&p_param);
820 if (error!=OMX_ErrorNone){
821 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX video rend OMX_GetParameter failed %x", error);
822 DeAllocateCodecsOMX();
825 omx_rend_input_port=p_param.nStartPortNumber;
826 //omx_rend_output_port=p_param.nStartPortNumber+1;
829 if (!DisablePort(omx_vid_rend,omx_rend_input_port) /*|| !DisablePort(omx_vid_rend,omx_rend_output_port)*/
831 Log::getInstance()->log("Video", Log::DEBUG, "Disable Ports OMX video rend failed");
832 DeAllocateCodecsOMX();
845 /* error=OMX_SendCommand(omx_vid_dec,OMX_CommandStateSet,OMX_StateIdle,0);
846 if (error!=OMX_ErrorNone){
847 Log::getInstance()->log("Video", Log::DEBUG, "vid_dec Send Command to OMX State Idle %x", error);
854 OMX_VIDEO_PARAM_PORTFORMATTYPE ft_type;
855 memset(&ft_type,0,sizeof(ft_type));
856 ft_type.nSize=sizeof(ft_type);
857 ft_type.nVersion.nVersion=OMX_VERSION;
859 ft_type.nPortIndex=omx_codec_input_port;
861 ft_type.eCompressionFormat=OMX_VIDEO_CodingAVC;
863 ft_type.eCompressionFormat=OMX_VIDEO_CodingMPEG2;
866 Demuxer* demux=Demuxer::getInstance();
868 ft_type.xFramerate=0;//25*(1<<16);//demux->getFrameRate()*(1<<16);
869 Log::getInstance()->log("Video", Log::DEBUG, "Framerate: %d",demux->getFrameRate());
870 error=OMX_SetParameter(omx_vid_dec,OMX_IndexParamVideoPortFormat,&ft_type);
871 if (error!=OMX_ErrorNone){
872 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX_IndexParamVideoPortFormat failed %x", error);
873 DeAllocateCodecsOMX();
878 if (!ChangeComponentState(omx_vid_dec,OMX_StateIdle)) {
879 Log::getInstance()->log("Video", Log::DEBUG, "vid_dec ChangeComponentState");
880 DeAllocateCodecsOMX();
885 if (!PrepareInputBufsOMX()) {
886 DeAllocateCodecsOMX();
891 if (!ChangeComponentState(omx_clock,OMX_StateIdle)) {
892 Log::getInstance()->log("Video", Log::DEBUG, "omx_clock ChangeComponentState Idle");
893 DeAllocateCodecsOMX();
897 error=OMX_SetupTunnel(omx_clock,omx_clock_output_port,omx_vid_sched,omx_shed_clock_port);
898 if (error!=OMX_ErrorNone){
899 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);
900 DeAllocateCodecsOMX();
904 if (!EnablePort(omx_clock,omx_clock_output_port,false) || !EnablePort(omx_vid_sched,omx_shed_clock_port,false)
906 Log::getInstance()->log("Video", Log::DEBUG, "Enable Ports OMX clock shed failed");
907 DeAllocateCodecsOMX();
911 if ( !CommandFinished(omx_vid_sched,OMX_CommandPortEnable,omx_shed_clock_port)) {
912 DeAllocateCodecsOMX();
916 error=OMX_SetupTunnel(omx_vid_dec,omx_codec_output_port,omx_vid_sched,omx_shed_input_port);
917 if (error!=OMX_ErrorNone){
918 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel dec to sched failed %x", error);
919 DeAllocateCodecsOMX();
925 if (!EnablePort(omx_vid_dec,omx_codec_output_port,false) || !EnablePort(omx_vid_sched,omx_shed_input_port,false)
927 Log::getInstance()->log("Video", Log::DEBUG, "Enable Ports OMX codec shed failed");
928 DeAllocateCodecsOMX();
932 if ( !CommandFinished(omx_vid_sched,OMX_CommandPortEnable,omx_shed_input_port)) {
935 if (!ChangeComponentState(omx_vid_sched,OMX_StateIdle)) {
936 Log::getInstance()->log("Video", Log::DEBUG, "vid_sched idle ChangeComponentState");
937 DeAllocateCodecsOMX();
940 if (!CommandFinished(omx_clock,OMX_CommandPortEnable,omx_clock_output_port)
941 ||!CommandFinished(omx_vid_dec,OMX_CommandPortEnable,omx_codec_output_port)){
942 DeAllocateCodecsOMX();
946 if (!ChangeComponentState(omx_vid_dec,OMX_StateExecuting)) {
947 Log::getInstance()->log("Video", Log::DEBUG, "omx_vid_dec ChangeComponentState Execute");
948 DeAllocateCodecsOMX();
952 error=OMX_SetupTunnel(omx_vid_sched,omx_shed_output_port,omx_vid_rend,omx_rend_input_port);
953 if (error!=OMX_ErrorNone){
954 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel sched to rend failed %x", error);
955 DeAllocateCodecsOMX();
959 if (!EnablePort(omx_vid_sched,omx_shed_output_port,false) || !EnablePort(omx_vid_rend,omx_rend_input_port,false)
961 Log::getInstance()->log("Video", Log::DEBUG, "Enable Ports OMX shed rend failed");
962 DeAllocateCodecsOMX();
966 if (!CommandFinished(omx_vid_sched,OMX_CommandPortEnable,omx_shed_output_port)
967 || !CommandFinished(omx_vid_rend,OMX_CommandPortEnable,omx_rend_input_port)) {
968 DeAllocateCodecsOMX();
972 if (!ChangeComponentState(omx_vid_rend,OMX_StateIdle)) {
973 Log::getInstance()->log("Video", Log::DEBUG, "vid_rend ChangeComponentState");
974 DeAllocateCodecsOMX();
979 if (!ChangeComponentState(omx_vid_sched,OMX_StateExecuting)) {
980 Log::getInstance()->log("Video", Log::DEBUG, "omx_vid_sched ChangeComponentState Execute");
981 DeAllocateCodecsOMX();
985 if (!ChangeComponentState(omx_vid_rend,OMX_StateExecuting)) {
986 Log::getInstance()->log("Video", Log::DEBUG, "omx_vid_rend ChangeComponentState Execute");
987 DeAllocateCodecsOMX();
992 OMX_CONFIG_DISPLAYREGIONTYPE dispconf;
993 memset(&dispconf,0,sizeof(dispconf));
994 dispconf.nSize=sizeof(dispconf);
995 dispconf.nVersion.nVersion=OMX_VERSION;
997 dispconf.nPortIndex=omx_rend_input_port;
999 dispconf.set=OMX_DISPLAY_SET_LAYER ;
1001 error=OMX_SetParameter(omx_vid_rend,OMX_IndexConfigDisplayRegion,&dispconf);
1002 if (error!=OMX_ErrorNone){
1003 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX_IndexConfigDisplayRegion failed %x", error);
1004 DeAllocateCodecsOMX();
1008 /* dispconf.set=OMX_DISPLAY_SET_FULLSCREEN ;
1009 dispconf.fullscreen=OMX_FALSE;
1010 error=OMX_SetParameter(omx_vid_rend,OMX_IndexConfigDisplayRegion,&dispconf);
1011 if (error!=OMX_ErrorNone){
1012 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX_IndexConfigDisplayRegion failed %x", error);
1013 DeAllocateCodecsOMX();
1017 dispconf.set=OMX_DISPLAY_SET_DEST_RECT;
1018 dispconf.dest_rect.x_offset=100;
1019 dispconf.dest_rect.y_offset=100;
1020 dispconf.dest_rect.width=640;
1021 dispconf.dest_rect.height=480;
1022 error=OMX_SetParameter(omx_vid_rend,OMX_IndexConfigDisplayRegion,&dispconf);
1023 if (error!=OMX_ErrorNone){
1024 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX_IndexConfigDisplayRegion failed %x", error);
1025 DeAllocateCodecsOMX();
1032 if (!ChangeComponentState(omx_clock,OMX_StateExecuting)) {
1033 Log::getInstance()->log("Video", Log::DEBUG, "omx_clock ChangeComponentState Exccute");
1034 DeAllocateCodecsOMX();
1044 int VideoVPEOGL::ChangeComponentState(OMX_HANDLETYPE handle,OMX_STATETYPE type)
1046 OMX_ERRORTYPE error;
1047 error=OMX_SendCommand(handle,OMX_CommandStateSet,type,0);
1048 if (error!=OMX_ErrorNone){
1049 Log::getInstance()->log("Video", Log::DEBUG, "handle %x Send Command to OMX State %x %x",handle,type, error);
1053 if (!CommandFinished(handle,OMX_CommandStateSet,type)) {
1061 int VideoVPEOGL::EnablePort(OMX_HANDLETYPE handle,OMX_U32 port,bool wait)
1063 OMX_ERRORTYPE error;
1064 error=OMX_SendCommand(handle,OMX_CommandPortEnable,port,0);
1065 if (error!=OMX_ErrorNone){
1066 Log::getInstance()->log("Video", Log::DEBUG, "handle %x Send Command to enable port %x %x",handle,port, error);
1070 if (!wait) return 1;
1071 if (!CommandFinished(handle,OMX_CommandPortEnable,port)) {
1079 int VideoVPEOGL::DisablePort(OMX_HANDLETYPE handle,OMX_U32 port,bool wait)
1081 OMX_ERRORTYPE error;
1082 error=OMX_SendCommand(handle,OMX_CommandPortDisable,port,0);
1083 if (error!=OMX_ErrorNone){
1084 Log::getInstance()->log("Video", Log::DEBUG, "handle %x Send Command to disable port %x %x",handle,port, error);
1088 if (!wait) return 1;
1089 if (!CommandFinished(handle,OMX_CommandPortDisable,port)) {
1100 int VideoVPEOGL::CommandFinished(OMX_HANDLETYPE handle,OMX_U32 command,OMX_U32 data2)
1104 omx_event_mutex.Lock();
1105 list<VPE_OMX_EVENT>::iterator itty=omx_events.begin();
1106 while (itty!=omx_events.end()) {
1108 VPE_OMX_EVENT current=*itty;
1109 if (current.handle==handle) { //this is ours
1110 if (current.event_type==OMX_EventError) {
1111 omx_events.erase(itty);
1112 omx_event_mutex.Unlock();
1115 } else if (current.event_type==OMX_EventCmdComplete && current.data1==command && current.data2==data2) {
1116 omx_events.erase(itty);
1117 omx_event_mutex.Unlock();
1124 omx_event_mutex.Unlock();
1129 Log::getInstance()->log("Video", Log::DEBUG, "CommandFinished waited too long %x %x %x",handle,command, data2);
1138 int VideoVPEOGL::PrepareInputBufsOMX()
1140 OMX_ERRORTYPE error;
1141 OMX_PARAM_PORTDEFINITIONTYPE port_def_type;
1142 memset(&port_def_type,0,sizeof(port_def_type));
1143 port_def_type.nSize=sizeof(port_def_type);
1144 port_def_type.nVersion.nVersion=OMX_VERSION;
1145 port_def_type.nPortIndex=omx_codec_input_port;
1147 error=OMX_GetParameter(omx_vid_dec,OMX_IndexParamPortDefinition, &port_def_type);
1149 if (error!=OMX_ErrorNone){
1150 Log::getInstance()->log("Video", Log::DEBUG, "Get OMX OMX_IndexParamPortDefinition failed %x", error);
1152 /* Log::getInstance()->log("Video", Log::DEBUG, "Port para %d %d %d %d %d %d %d", port_def_type.nBufferCountActual,
1153 port_def_type.nBufferCountMin,port_def_type.nBufferSize,port_def_type.bEnabled,port_def_type.bPopulated,
1154 port_def_type.bBuffersContiguous,port_def_type.nBufferAlignment);*/
1156 port_def_type.nBufferCountActual=60;
1158 error=OMX_SetParameter(omx_vid_dec,OMX_IndexParamPortDefinition, &port_def_type);
1160 if (error!=OMX_ErrorNone){
1161 Log::getInstance()->log("Video", Log::DEBUG, "Set OMX OMX_IndexParamPortDefinition failed %x", error);
1165 error=OMX_SendCommand(omx_vid_dec,OMX_CommandPortEnable,omx_codec_input_port,0);
1166 if (error!=OMX_ErrorNone){
1167 Log::getInstance()->log("Video", Log::DEBUG, "Prepare Input bufs Send Command to enable port %x", error);
1171 input_bufs_omx_mutex.Lock();
1172 for (unsigned int i=0; i< port_def_type.nBufferCountActual;i++) {
1174 // unsigned char* new_buffer_data=(unsigned char*)malloc(port_def_type.nbufferSize);
1175 OMX_BUFFERHEADERTYPE *buf_head=NULL;
1176 /* error=OMX_Usebuffer(omx_vid_dec,&buf_head,omx_codec_input_port,NULL,port_def_type.nbufferSize,new_buffer_data);
1177 if (error!=OMX_ErrorNone){
1178 Log::getInstance()->log("Video", Log::DEBUG, "Use OMX_Usebuffer failed %x", error);
1179 input_bufs_omx_mutex.Unlock();
1182 error=OMX_AllocateBuffer(omx_vid_dec,&buf_head,omx_codec_input_port,NULL,port_def_type.nBufferSize);
1183 if (error!=OMX_ErrorNone){
1184 Log::getInstance()->log("Video", Log::DEBUG, "Use OMX_AllocateBuffer failed %x", error);
1185 input_bufs_omx_mutex.Unlock();
1188 input_bufs_omx_all.push_back(buf_head);
1189 input_bufs_omx_free.push_back(buf_head);
1191 omx_first_frame=true;
1194 cur_input_buf_omx=NULL;
1195 input_bufs_omx_mutex.Unlock();
1198 Log::getInstance()->log("Video", Log::DEBUG, "PrepareInputBufsOMX mark3");
1199 if (!CommandFinished(omx_vid_dec,OMX_CommandPortEnable,omx_codec_input_port)) {
1202 Log::getInstance()->log("Video", Log::DEBUG, "PrepareInputBufsOMX mark4");
1207 int VideoVPEOGL::DestroyInputBufsOMX()
1209 OMX_ERRORTYPE error;
1211 cur_input_buf_omx=NULL;
1212 input_bufs_omx_mutex.Lock();
1213 for (int i=0; i< input_bufs_omx_all.size();i++) {
1214 // free(input_bufs_omx_all[i]->pBuffer);
1215 // input_bufs_omx_all[i]->pBuffer=NULL;
1216 error=OMX_FreeBuffer(omx_vid_dec,omx_codec_input_port,input_bufs_omx_all[i]);
1217 if (error!=OMX_ErrorNone){
1218 Log::getInstance()->log("Video", Log::DEBUG, "Use OMX_FreeBuffer failed %x", error);
1219 input_bufs_omx_mutex.Unlock();
1223 input_bufs_omx_all.clear();
1224 input_bufs_omx_free.clear();
1225 input_bufs_omx_mutex.Unlock();
1232 int VideoVPEOGL::DeAllocateCodecsOMX()
1234 OMX_ERRORTYPE error;
1237 // first flush all buffers
1239 error=OMX_SendCommand(omx_vid_dec,OMX_CommandFlush, omx_codec_output_port, NULL);
1240 if (error!=OMX_ErrorNone){
1241 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Flush codec out failed %x", error);
1245 error=OMX_SendCommand(omx_vid_sched,OMX_CommandFlush, omx_shed_input_port, NULL);
1246 if (error!=OMX_ErrorNone){
1247 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Flush shed in failed %x", error);
1251 if (!CommandFinished(omx_vid_dec,OMX_CommandFlush,omx_codec_output_port) ||
1252 !CommandFinished(omx_vid_sched,OMX_CommandFlush,omx_shed_input_port)) {
1253 Log::getInstance()->log("Video", Log::DEBUG, "flush cmd codec shed failed");
1256 error=OMX_SendCommand(omx_clock,OMX_CommandFlush, omx_clock_output_port, NULL);
1257 if (error!=OMX_ErrorNone){
1258 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Flush clock out failed %x", error);
1262 error=OMX_SendCommand(omx_vid_sched,OMX_CommandFlush, omx_shed_clock_port, NULL);
1263 if (error!=OMX_ErrorNone){
1264 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Flush shed clock failed %x", error);
1268 if (!CommandFinished(omx_clock,OMX_CommandFlush,omx_clock_output_port) ||
1269 !CommandFinished(omx_vid_sched,OMX_CommandFlush,omx_shed_clock_port)) {
1270 Log::getInstance()->log("Video", Log::DEBUG, "flush cmd clock shed failed");
1273 error=OMX_SendCommand(omx_vid_sched,OMX_CommandFlush, omx_shed_output_port, NULL);
1274 if (error!=OMX_ErrorNone) {
1275 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Flush shed out failed %x", error);
1279 error=OMX_SendCommand(omx_vid_rend,OMX_CommandFlush, omx_rend_input_port, NULL);
1280 if (error!=OMX_ErrorNone) {
1281 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Flush rend in failed %x", error);
1285 if (!CommandFinished(omx_vid_sched,OMX_CommandFlush,omx_shed_output_port) ||
1286 !CommandFinished(omx_vid_rend,OMX_CommandFlush,omx_rend_input_port)) {
1287 Log::getInstance()->log("Video", Log::DEBUG, "flush cmd shed rend failed");
1293 error=OMX_SendCommand(omx_vid_dec,OMX_CommandFlush, omx_codec_input_port, NULL);
1294 if (error!=OMX_ErrorNone){
1295 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Flush codec out failed %x", error);
1300 if (!CommandFinished(omx_vid_dec,OMX_CommandFlush,omx_codec_input_port)) {
1301 Log::getInstance()->log("Video", Log::DEBUG, "flush cmd codec input failed");
1304 DestroyInputBufsOMX();
1307 if (!DisablePort(omx_vid_rend,omx_rend_input_port,true)) {
1308 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 1");
1310 if (!DisablePort(omx_vid_sched,omx_shed_output_port,true)) {
1311 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 2 ");
1314 error=OMX_SetupTunnel(omx_vid_rend,omx_rend_input_port,NULL,NULL);
1315 if (error!=OMX_ErrorNone){
1316 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1320 error=OMX_SetupTunnel(omx_vid_sched,omx_shed_output_port,NULL,NULL);
1321 if (error!=OMX_ErrorNone){
1322 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1326 if (!DisablePort(omx_vid_sched,omx_shed_input_port,true)) {
1327 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 3");
1330 if (!DisablePort(omx_vid_sched,omx_shed_clock_port,true)) {
1331 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 4");
1334 if (!DisablePort(omx_clock,omx_clock_output_port,true)) {
1335 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 5");
1337 if (!DisablePort(omx_vid_dec,omx_codec_output_port,true)) {
1338 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 6");
1343 if (!DisablePort(omx_vid_dec,omx_codec_input_port,true)) {
1344 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 7");
1348 error=OMX_SetupTunnel(omx_vid_sched,omx_shed_input_port,NULL,NULL);
1349 if (error!=OMX_ErrorNone){
1350 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1353 error=OMX_SetupTunnel(omx_vid_sched,omx_shed_clock_port,NULL,NULL);
1354 if (error!=OMX_ErrorNone){
1355 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1359 error=OMX_SetupTunnel(omx_clock,omx_clock_output_port,NULL,NULL);
1360 if (error!=OMX_ErrorNone){
1361 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1365 error=OMX_SetupTunnel(omx_vid_dec,omx_codec_output_port,NULL,NULL);
1366 if (error!=OMX_ErrorNone){
1367 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1375 error=OMX_FreeHandle(omx_vid_dec);
1376 error=OMX_FreeHandle(omx_vid_sched);
1377 error=OMX_FreeHandle(omx_vid_rend);
1378 error=OMX_FreeHandle(omx_clock);
1380 if (error!=OMX_ErrorNone) {
1381 Log::getInstance()->log("Video", Log::DEBUG, "FreeHandle failed %d", error);
1391 #ifdef VPE_LIBAV_SUPPORT
1393 enum PixelFormat VideoVPEOGL::get_format_libav(struct AVCodecContext *s, const enum PixelFormat *fmt)
1395 int dec_mode=((VideoVPEOGL*)getInstance())->getlibavDecodingMode();
1396 enum PixelFormat ret_pix=PIX_FMT_NONE;
1397 if (dec_mode==VPE_NO_XVMC) return PIX_FMT_NONE;
1398 while (*fmt!=PIX_FMT_NONE) {
1399 if (*fmt== PIX_FMT_XVMC_MPEG2_IDCT && dec_mode==VPE_XVMC_IDCT) {
1400 ret_pix=PIX_FMT_XVMC_MPEG2_IDCT;
1401 } else if (*fmt== PIX_FMT_XVMC_MPEG2_MC && dec_mode==VPE_XVMC_MOCOMP) {
1402 ret_pix=PIX_FMT_XVMC_MPEG2_MC;
1409 int VideoVPEOGL::reget_buffer_libav(struct AVCodecContext *c, AVFrame *pic)
1411 Log::getInstance()->log("Video", Log::ERR, "Buffer reusing! Should not happen!Not Implemented!");
1415 int VideoVPEOGL::get_buffer_libav(struct AVCodecContext *c, AVFrame *pic)
1417 unsigned int want_sizes[4]={0,0,0,0};
1419 bool normal_pixs=false;
1421 int num_dct_blocks=0;
1424 //reget logic from mplayer
1425 if (pic->opaque && pic->data[0] && (!pic->buffer_hints ||pic->buffer_hints & FF_BUFFER_HINTS_REUSABLE )){
1426 Log::getInstance()->log("Video", Log::ERR, "Buffer reusing! Should not happen!");
1431 if (c->pix_fmt!=PIX_FMT_XVMC_MPEG2_IDCT &&c->pix_fmt!=PIX_FMT_XVMC_MPEG2_MC) {
1433 // standard pixel format
1434 // this is written using much inspiration from libav util.c, so portions from there
1440 avcodec_align_dimensions2(c, &width, &height, s_a);
1441 if ((c->flags & CODEC_FLAG_EMU_EDGE)==0) {
1445 // Now we have to determine alignment size
1446 bool unaligned=true;
1448 av_image_fill_linesizes(pict.linesize, c->pix_fmt, width); //linesizes are derived
1449 width+=width & ~(width-1); //trick from libav, now determine, if the alignment is ok
1451 for (int i=0;i<4;i++) {
1452 if ((pict.linesize[i]%s_a[i])!=0) {
1458 int tot_size=av_image_fill_pointers(pict.data, c->pix_fmt, height, NULL, pict.linesize); //get sizes
1459 for (int i=0;i<4 ;i++) {
1460 if (i<3 && pict.data[i+1]) {
1461 want_sizes[i]=pict.data[i+1]-pict.data[i];
1464 want_sizes[i]=(tot_size-(pict.data[i]-pict.data[0]));
1471 //TODO set linesizes!
1473 num_blocks=((c->width+15)/16)*((c->height+15)/16);
1474 num_dct_blocks=num_blocks*6; //6 blocks per macroblock
1476 want_sizes[2]=sizeof(xvmc_pix_fmt);
1477 want_sizes[1]=sizeof(short)*num_dct_blocks*8*8;
1478 want_sizes[0]=sizeof(XvMCMacroBlock)*num_blocks;
1479 pict.linesize[0]=pict.linesize[1]=pict.linesize[2]=pict.linesize[3]=0;
1483 VPE_FrameBuf *frame_buf=((VideoVPEOGL*)Video::getInstance())->getFrameBuf(want_sizes);
1485 //Log::getInstance()->log("Video", Log::NOTICE, "get buffer %x",frame_buf);
1487 Log::getInstance()->log("Video", Log::ERR, "Getting buffer libav failed");
1493 pic->type=FF_BUFFER_TYPE_USER; // we are controlling the buffers
1494 int hchr_shift,vchr_shift;
1495 avcodec_get_chroma_sub_sample(c->pix_fmt,&hchr_shift,&vchr_shift);
1496 const int pixel_size = av_pix_fmt_descriptors[c->pix_fmt].comp[0].step_minus1+1;
1497 for (int i=0;i<4;i++) {
1498 pic->data[i]=(uint8_t*)frame_buf->data[i];
1499 pic->linesize[i]=pict.linesize[i];
1505 edge_width>>=hchr_shift;
1506 edge_height>>=vchr_shift;
1508 pic->data[i]+=FFALIGN((pic->linesize[i]*16) + (pixel_size*edge_width), s_a[i]);
1512 pic->base[0]=(uint8_t*)frame_buf; // our structure
1513 //pic->extended_data=pic->data;
1514 if(c->pkt) pic->pkt_pts=c->pkt->pts;
1515 else pic->pkt_pts=AV_NOPTS_VALUE;
1516 pic->width=c->width;
1517 pic->height=c->height;
1518 pic->format=c->pix_fmt;
1519 pic->sample_aspect_ratio=c->sample_aspect_ratio;
1520 pic->reordered_opaque= c->reordered_opaque;
1523 xvmc_pix_fmt *pix_xvmc=(xvmc_pix_fmt *)pic->data[2];
1524 pix_xvmc->xvmc_id=AV_XVMC_ID;
1525 pix_xvmc->data_blocks=(short*)pic->data[1];
1526 pix_xvmc->mv_blocks=(XvMCMacroBlock*)pic->data[0];
1527 pix_xvmc->allocated_mv_blocks=num_blocks;
1528 pix_xvmc->allocated_data_blocks=num_dct_blocks;
1529 if (c->pix_fmt==PIX_FMT_XVMC_MPEG2_IDCT) pix_xvmc->idct=1;
1530 else pix_xvmc->idct=0;
1531 pix_xvmc->unsigned_intra=0; // let see what happens
1532 pix_xvmc->p_surface=(XvMCSurface*)frame_buf->pict_num;
1533 pix_xvmc->start_mv_blocks_num=0;
1534 pix_xvmc->filled_mv_blocks_num=0;
1535 pix_xvmc->next_free_data_block_num=0;
1545 void VideoVPEOGL::release_buffer_libav(struct AVCodecContext *c, AVFrame *pic)
1547 // Log::getInstance()->log("Video", Log::NOTICE, "release buffer %x",pic->base[0]);
1548 ((VideoVPEOGL*)Video::getInstance())->releaseFrameBufLibav((VPE_FrameBuf*) pic->base[0]);
1550 pic->data[0]=pic->data[1]=pic->data[2]=pic->data[3]=NULL;
1555 int VideoVPEOGL::AllocateCodecsLibav()
1557 libav_hastime=false;
1558 Log::getInstance()->log("Video", Log::NOTICE, "AllocateCodecslibav");
1559 mpeg2codec_context_libav=avcodec_alloc_context();
1560 if (mpeg2codec_context_libav==NULL) {
1561 Log::getInstance()->log("Video", Log::DEBUG, "Creating libav codec context failed");
1564 if (decoding_mode!=VPE_NO_XVMC) {
1565 mpeg2codec_context_libav->slice_flags=SLICE_FLAG_CODED_ORDER|SLICE_FLAG_ALLOW_FIELD;
1566 if (decoding_mode==VPE_XVMC_MOCOMP) mpeg2codec_context_libav->pix_fmt=PIX_FMT_XVMC_MPEG2_MC;
1567 else mpeg2codec_context_libav->pix_fmt=PIX_FMT_XVMC_MPEG2_IDCT;
1568 mpeg2codec_context_libav->get_format=get_format_libav;
1571 mpeg2codec_context_libav->get_buffer=get_buffer_libav;
1572 mpeg2codec_context_libav->reget_buffer=reget_buffer_libav;
1573 mpeg2codec_context_libav->release_buffer=release_buffer_libav;
1576 int avc_ret=avcodec_open(mpeg2codec_context_libav, mpeg2codec_libav);
1578 Log::getInstance()->log("Video", Log::DEBUG, "Opening libav codec failed ");
1581 memset(&incom_packet_libav,0,sizeof(incom_packet_libav));
1582 incom_packet_libav_size=200000;
1583 incom_packet_libav.data=(uint8_t*)av_malloc(incom_packet_libav_size+FF_INPUT_BUFFER_PADDING_SIZE);
1585 dec_frame_libav_mutex.Lock();
1586 for (int i=0;i<3;i++) {
1587 AVFrame *dec_frame_libav=avcodec_alloc_frame(); // may be we need multiple frames, if we want to use async texture upload
1588 if (!dec_frame_libav) {
1589 Log::getInstance()->log("Video", Log::DEBUG, "Allocating dec_frame failed");
1592 dec_frame_libav_all.push_back(dec_frame_libav);
1593 dec_frame_libav_free.push_back(dec_frame_libav);
1595 dec_frame_libav_decoding=NULL;
1596 dec_frame_libav_mutex.Unlock();
1598 ogl_frame_mutex.Lock();
1599 //Allocate texture structs, since we do not know the sizes, we do not allocate the textures yet
1600 for (int i=0;i<3;i++) {
1601 VPEOGLFrame *new_frame=(VPEOGLFrame *)malloc(sizeof(VPEOGLFrame));
1602 new_frame->type=1; //1 = YUV, 2 RGB
1603 new_frame->textures[0]=0;
1604 new_frame->textures[1]=0;
1605 new_frame->textures[2]=0;
1606 new_frame->width=new_frame->height=0;
1607 all_ogl_frames.push_back(new_frame);
1608 free_ogl_frames.push_back(new_frame);
1611 ogl_frame_outside=false;
1613 ogl_frame_mutex.Unlock();
1615 if (decoding_mode==VPE_XVMC_MOCOMP) {
1616 OsdOpenGL* osd=(OsdOpenGL*)osd->getInstance();
1617 osd->BeginPainting(); // get OpenGl context
1618 moco_shader=new GLMocoShader();
1619 moco_shader->init();
1629 int VideoVPEOGL::DeAllocateCodecsLibav()
1631 libav_running=false;
1632 Log::getInstance()->log("Video", Log::NOTICE, "DeAllocateCodecslibav");
1633 dec_frame_libav_mutex.Lock();
1634 dec_frame_libav_upload_pending.clear();
1635 dec_frame_libav_free.clear();
1636 dec_frame_libav_mutex.Unlock();
1637 while (dec_frame_libav_uploading) {
1638 Log::getInstance()->log("Video", Log::NOTICE, "Wait for uploading to finish");
1641 dec_frame_libav_mutex.Lock();
1642 for (int i=0; i< dec_frame_libav_all.size();i++) {
1643 av_free(dec_frame_libav_all[i]);
1646 dec_frame_libav_all.clear();
1648 av_free(incom_packet_libav.data);
1649 incom_packet_libav.data=NULL;
1650 incom_packet_libav_size=0;
1652 dec_frame_libav_mutex.Unlock();
1653 dec_frame_libav_decoding=NULL;
1654 while (ogl_frame_outside) {
1655 Log::getInstance()->log("Video", Log::NOTICE, "Wait for ogl frame from outside");
1659 ((OsdOpenGL*)Osd::getInstance())->BeginPainting(); // get osd's context
1660 ogl_frame_mutex.Lock();
1661 for (int i=0; i< dec_frame_libav_all.size();i++) {
1662 VPEOGLFrame * del_frame=all_ogl_frames[i];
1663 if (del_frame->textures[0]==0) {
1664 glDeleteTextures(1,&del_frame->textures[0]);
1665 del_frame->textures[0]=0;
1667 if (del_frame->textures[1]==0) {
1668 glDeleteTextures(1,&del_frame->textures[1]);
1669 del_frame->textures[1]=0;
1671 if (del_frame->textures[2]==0) {
1672 glDeleteTextures(1,&del_frame->textures[2]);
1673 del_frame->textures[2]=0;
1676 free(all_ogl_frames[i]);
1678 all_ogl_frames.clear();
1679 free_ogl_frames.clear();
1680 ready_ogl_frames.clear();
1681 ogl_frame_mutex.Unlock();
1682 ((OsdOpenGL*)Osd::getInstance())->EndPainting();
1685 if (mpeg2codec_context_libav) {
1686 avcodec_close(mpeg2codec_context_libav);
1687 av_free(mpeg2codec_context_libav);
1688 mpeg2codec_context_libav=NULL;
1694 vpe_framebuf_mutex.Lock();
1696 for (int i=0;i<all_frame_bufs.size();i++) {
1697 VPE_FrameBuf* current=all_frame_bufs[i];
1698 for (int x=0;x<4;x++) {
1699 if (current->data[x]) {
1700 av_free(current->data[x]);
1705 all_frame_bufs.clear();
1706 free_frame_bufs.clear();
1707 locked_libav_frame_buf.clear();
1708 locked_uploading_frame_buf.clear();
1710 vpe_framebuf_mutex.Unlock();
1715 OsdOpenGL* osd=(OsdOpenGL*)osd->getInstance();
1716 osd->BeginPainting(); // get OpenGl context
1717 moco_shader->deinit();
1731 VPE_FrameBuf *VideoVPEOGL::getFrameBuf(unsigned int *size)
1733 VPE_FrameBuf* current=NULL;
1734 vpe_framebuf_mutex.Lock();
1735 if (free_frame_bufs.size()>0) {
1736 current=free_frame_bufs.front();
1737 free_frame_bufs.pop_front();
1738 } else if (all_frame_bufs.size()<6) {
1739 current=(VPE_FrameBuf*)malloc(sizeof(VPE_FrameBuf));
1740 memset(current,0,sizeof(VPE_FrameBuf));
1742 Log::getInstance()->log("Video", Log::NOTICE, "Framebuffer underrun!");
1743 vpe_framebuf_mutex.Unlock();
1744 return NULL; // We do not have a frame buffer
1746 locked_libav_frame_buf.push_back(current);
1747 vpe_framebuf_mutex.Unlock();
1748 //check if we need reallocation
1749 for (int x=0;x<4;x++) {
1750 if (current->size[x]!=size[x]) {
1751 current->data[x]=av_realloc(current->data[x],size[x]);
1752 current->size[x]=size[x];
1755 framebuf_framenum++;
1756 current->pict_num=framebuf_framenum; //This is used for tracking reference frames through the conversion pipeline
1761 void VideoVPEOGL::lockFrameBufUpload(VPE_FrameBuf* buf)
1763 // first find frame_buf memory
1765 //Log::getInstance()->log("Video", Log::NOTICE, "lock buffer upload %x",buf);
1766 VPE_FrameBuf* current=buf;
1767 vpe_framebuf_mutex.Lock();
1768 if (current) locked_uploading_frame_buf.push_back(current); //locked
1769 vpe_framebuf_mutex.Unlock();
1774 void VideoVPEOGL::releaseFrameBufLibav(VPE_FrameBuf* buf)
1776 // first find frame_buf memory
1777 //Log::getInstance()->log("Video", Log::NOTICE, "release buffer libav %x",buf);
1778 VPE_FrameBuf* current=buf;
1779 vpe_framebuf_mutex.Lock();
1781 locked_libav_frame_buf.remove(current); //unlocked
1782 list<VPE_FrameBuf*>::iterator itty=locked_uploading_frame_buf.begin();
1784 while (itty!=locked_uploading_frame_buf.end()) {
1785 if (*itty==current) {
1792 free_frame_bufs.push_back(current);
1795 vpe_framebuf_mutex.Unlock();
1798 void VideoVPEOGL::releaseFrameBufUpload(VPE_FrameBuf* buf)
1800 // first find frame_buf memory
1801 VPE_FrameBuf* current=buf;
1802 //Log::getInstance()->log("Video", Log::NOTICE, "release buffer upload %x",buf);
1803 vpe_framebuf_mutex.Lock();
1805 locked_uploading_frame_buf.remove(current); //unlocked
1806 list<VPE_FrameBuf*>::iterator itty=locked_libav_frame_buf.begin();
1808 while (itty!=locked_libav_frame_buf.end()) {
1809 if (*itty==current) {
1816 free_frame_bufs.push_back(current);
1819 vpe_framebuf_mutex.Unlock();
1829 int VideoVPEOGL::stop()
1831 if (!initted) return 0;
1833 #ifdef VPE_OMX_SUPPORT
1834 //Check if libav mode
1835 if (decoding_backend==VPE_DECODER_OMX) DeAllocateCodecsOMX();
1842 // if (ioctl(fdVideo, AV_SET_VID_STOP, 0) != 0) return 0;
1846 int VideoVPEOGL::reset()
1848 if (!initted) return 0;
1850 // if (ioctl(fdVideo, AV_SET_VID_RESET, 0x11) != 0) return 0;
1854 int VideoVPEOGL::pause()
1856 if (!initted) return 0;
1858 // if (ioctl(fdVideo, AV_SET_VID_PAUSE, 0) != 0) return 0;
1862 int VideoVPEOGL::unPause() // FIXME get rid - same as play!!
1864 if (!initted) return 0;
1865 // if (ioctl(fdVideo, AV_SET_VID_PLAY, 0) != 0) return 0;
1869 int VideoVPEOGL::fastForward()
1871 if (!initted) return 0;
1873 // if (ioctl(fdVideo, AV_SET_VID_libavWD, 1) != 0) return 0;
1877 int VideoVPEOGL::unFastForward()
1879 if (!initted) return 0;
1881 // if (ioctl(fdVideo, AV_SET_VID_RESET, 0x11) != 0) return 0; // don't need this.
1883 //// if (ioctl(fdVideo, AV_SET_VID_PLAY, 0) != 0) return 0;
1887 int VideoVPEOGL::attachFrameBuffer()
1889 if (!initted) return 0;
1891 // if (ioctl(fdVideo, AV_SET_VID_FB, 0) != 0) return 0;
1895 int VideoVPEOGL::blank(void)
1897 // if (ioctl(fdVideo, AV_SET_VID_FB, 1) != 0) return 0;
1898 // if (ioctl(fdVideo, AV_SET_VID_FB, 0) != 0) return 0;
1902 ULLONG VideoVPEOGL::getCurrentTimestamp()
1904 /* sync_data_t timestamps;
1905 if (ioctl(fdVideo, AV_GET_VID_TIMESTAMPS, ×tamps) == 0)
1907 // FIXME are these the right way around?
1909 timestamps.stc = (timestamps.stc >> 31 ) | (timestamps.stc & 1);
1910 timestamps.pts = (timestamps.pts >> 31 ) | (timestamps.pts & 1);
1912 return timestamps.stc;
1921 ULONG VideoVPEOGL::timecodeToFrameNumber(ULLONG timecode)
1923 if (format == PAL) return (ULONG)(((double)timecode / (double)90000) * (double)25);
1924 else return (ULONG)(((double)timecode / (double)90000) * (double)30);
1928 int VideoVPEOGL::test()
1933 // return ioctl(fdVideo, AV_SET_VID_STC, &stc);
1940 int VideoVPEOGL::test2()
1946 void VideoVPEOGL::PrepareMediaSample(const MediaPacketList& mplist,UINT samplepos)
1948 mediapacket = mplist.front();
1951 UINT VideoVPEOGL::DeliverMediaSample(UCHAR* buffer, UINT *samplepos)
1953 DeliverMediaPacket(mediapacket, buffer, samplepos);
1954 if (*samplepos == mediapacket.length) {
1961 UINT VideoVPEOGL::DeliverMediaPacket(MediaPacket packet,
1962 const UCHAR* buffer,
1965 if (packet.type == MPTYPE_VIDEO_H264)
1973 switch (decoding_backend) {
1974 default: case 0: return 0; // no backend runnigng
1975 #ifdef VPE_OMX_SUPPORT
1976 case VPE_DECODER_OMX: return DeliverMediaPacketOMX(packet,buffer,samplepos);
1978 #ifdef VPE_LIBAV_SUPPORT
1979 case VPE_DECODER_libav: return DeliverMediaPacketlibav(packet,buffer,samplepos);
1984 #ifdef VPE_OMX_SUPPORT
1985 UINT VideoVPEOGL::DeliverMediaPacketOMX(MediaPacket packet,
1986 const UCHAR* buffer,
1991 //Later add fail back code for libav
1993 *samplepos+=packet.length;
1994 return packet.length;
1998 if (!omx_running) return 0; // if we are not runnig do not do this
2001 OMX_ERRORTYPE error;
2003 /* OMX_PARAM_PORTDEFINITIONTYPE port_image;
2004 memset(&port_image,0,sizeof(port_image));
2005 port_image.nSize=sizeof(port_image);
2006 port_image.nVersion.nVersion=OMX_VERSION;
2007 port_image.nPortIndex =omx_codec_output_port;
2008 error=OMX_GetParameter(omx_vid_dec,OMX_IndexParamPortDefinition, &port_image);
2009 if (error!= OMX_ErrorNone){
2010 Log::getInstance()->log("Video", Log::DEBUG, "OMX_GetParameter failed %x", error);
2012 Log::getInstance()->log("Video", Log::DEBUG, "Image port %d %d", port_image.format.video.nFrameWidth , port_image.format.video.nFrameHeight);*/
2014 /*First Check, if we have an audio sample*/
2018 return 0; //Not in iframe mode!
2022 if (packet.disconti) {
2024 if (cur_input_buf_omx) {
2025 OMX_ERRORTYPE error=OMX_EmptyThisBuffer(omx_vid_dec,cur_input_buf_omx);
2026 if (error!=OMX_ErrorNone){
2027 Log::getInstance()->log("Video", Log::DEBUG, "OMX_EmptyThisBuffer failed %x", error);
2029 cur_input_buf_omx=NULL;
2033 /*Inspect PES-Header */
2035 // OMX_STATETYPE temp_state;
2036 // OMX_GetState(omx_vid_dec,&temp_state);
2038 if (*samplepos==0) {//stripheader
2039 headerstrip=buffer[packet.pos_buffer+8]+9/*is this right*/;
2040 *samplepos+=headerstrip;
2041 if ( packet.synched ) {
2043 if (cur_input_buf_omx) {
2044 OMX_ERRORTYPE error=OMX_EmptyThisBuffer(omx_vid_dec,cur_input_buf_omx);
2045 if (error!=OMX_ErrorNone){
2046 Log::getInstance()->log("Video", Log::DEBUG, "OMX_EmptyThisBuffer failed %x", error);
2049 cur_input_buf_omx=NULL;//write out old data
2051 // reftime1=packet.presentation_time;
2052 // reftime2=reftime1+1;
2055 if (!firstsynched) {//
2056 *samplepos=packet.length;//if we have not processed at least one
2057 return packet.length;//synched packet ignore it!
2062 if (!cur_input_buf_omx) {
2063 input_bufs_omx_mutex.Lock();
2064 if (input_bufs_omx_free.size()==0) {
2065 input_bufs_omx_mutex.Unlock();
2066 Log::getInstance()->log("Video", Log::DEBUG, "Deliver MediaPacket no free sample");
2067 return 0; // we do not have a free media sample
2070 cur_input_buf_omx=input_bufs_omx_free.front();
2071 cur_input_buf_omx->nFilledLen=0;
2072 cur_input_buf_omx->nOffset=0;
2073 cur_input_buf_omx->nTimeStamp=0;
2074 input_bufs_omx_free.pop_front();
2075 input_bufs_omx_mutex.Unlock();
2081 if (cur_input_buf_omx->nFilledLen==0) {//will only be changed on first packet
2082 /*if (packet.disconti) {
2083 ms->SetDiscontinuity(TRUE);
2085 ms->SetDiscontinuity(FALSE);
2087 //if (packet.synched) {
2089 //lastreftimePTS=packet.pts;
2090 if (omx_first_frame) { // TODO time
2091 cur_input_buf_omx->nFlags=OMX_BUFFERFLAG_STARTTIME;
2092 omx_first_frame=false;
2098 cur_input_buf_omx->nFlags=OMX_BUFFERFLAG_TIME_UNKNOWN;
2101 // ms->SetSyncPoint(TRUE);
2105 unsigned int haveToCopy=packet.length-*samplepos;
2107 while (haveToCopy> (cur_input_buf_omx->nAllocLen-cur_input_buf_omx->nFilledLen)) {
2108 unsigned int cancopy=cur_input_buf_omx->nAllocLen-cur_input_buf_omx->nFilledLen;
2109 memcpy(cur_input_buf_omx->pBuffer+cur_input_buf_omx->nFilledLen,buffer+packet.pos_buffer+*samplepos,cancopy);
2110 haveToCopy-=cancopy;
2111 cur_input_buf_omx->nFilledLen+=cancopy;
2112 *samplepos+=cancopy;
2113 // push old buffer out
2115 OMX_ERRORTYPE error=OMX_EmptyThisBuffer(omx_vid_dec,cur_input_buf_omx);
2116 if (error!=OMX_ErrorNone){
2117 Log::getInstance()->log("Video", Log::DEBUG, "OMX_EmptyThisBuffer failed %x", error);
2120 input_bufs_omx_mutex.Lock();
2121 if (input_bufs_omx_free.size()==0) {
2122 input_bufs_omx_mutex.Unlock();
2123 //Log::getInstance()->log("Video", Log::DEBUG, "Deliver MediaPacket no free sample");
2124 return *samplepos; // we do not have a free media sample
2126 cur_input_buf_omx=input_bufs_omx_free.front();
2127 cur_input_buf_omx->nFilledLen=0;
2128 cur_input_buf_omx->nOffset=0;
2129 cur_input_buf_omx->nTimeStamp=0;
2130 input_bufs_omx_free.pop_front();
2131 input_bufs_omx_mutex.Unlock();
2133 cur_input_buf_omx->nFlags=OMX_BUFFERFLAG_TIME_UNKNOWN;
2136 memcpy(cur_input_buf_omx->pBuffer+cur_input_buf_omx->nFilledLen,
2137 buffer+packet.pos_buffer+*samplepos,haveToCopy);
2138 cur_input_buf_omx->nFilledLen+=haveToCopy;
2142 *samplepos+=haveToCopy;
2151 #ifdef VPE_LIBAV_SUPPORT
2152 int VideoVPEOGL::DecodePacketlibav()
2154 unsigned int haveToCopy=incom_packet_libav.size;
2155 if (incom_packet_libav.size==0) return 1; // we are already empty
2156 while (haveToCopy>0) {
2160 // Log::getInstance()->log("Video", Log::DEBUG, "Push data to decoder");
2162 #ifdef BENCHMARK_FPS
2163 int cur_time=getTimeMS();
2165 dec_bytes=avcodec_decode_video2(mpeg2codec_context_libav, dec_frame_libav_decoding,
2166 &frame_ready, &incom_packet_libav);
2167 #ifdef BENCHMARK_FPS
2168 time_in_decoder+=getTimeMS()-cur_time;
2169 if (frame_ready) num_frames++;
2170 if ((num_frames%100)==0) {
2171 float fps=1000./(float)(time_in_decoder);
2172 fps*=((float)num_frames);
2173 Log::getInstance()->log("Video", Log::NOTICE, "Current Pure Decoding FPS %g", fps);
2177 Log::getInstance()->log("Video", Log::DEBUG, "Decoding frame failed %x", dec_bytes);
2180 haveToCopy-=dec_bytes;
2182 // Log::getInstance()->log("Video", Log::DEBUG, "We have a frame push it to osd");
2184 lockFrameBufUpload((VPE_FrameBuf*)dec_frame_libav_decoding->base[0]); //lock for upload, so that ffmpeg does not reuse
2185 dec_frame_libav_mutex.Lock();
2186 libavwidth=mpeg2codec_context_libav->width;
2187 libavheight=mpeg2codec_context_libav->height;
2188 libavpixfmt=mpeg2codec_context_libav->pix_fmt;
2189 // Log::getInstance()->log("Video", Log::DEBUG, "Frame info %d %d %d",libavwidth,libavheight,libavpixfmt);
2192 dec_frame_libav_upload_pending.push_back(dec_frame_libav_decoding);
2193 dec_frame_libav_decoding=NULL;
2194 if (dec_frame_libav_free.size()>0) {
2195 dec_frame_libav_decoding=dec_frame_libav_free.front();
2196 dec_frame_libav_free.pop_front();
2197 dec_frame_libav_mutex.Unlock();
2199 libav_hastime=false;
2201 libav_hastime=false;
2202 dec_frame_libav_mutex.Unlock();
2211 incom_packet_libav.size=0;
2217 UINT VideoVPEOGL::DeliverMediaPacketlibav(MediaPacket packet,
2218 const UCHAR* buffer,
2221 //Later add fail back code for libav
2223 *samplepos+=packet.length;
2224 return packet.length;
2227 if (!libav_running) return 0; // if we are not runnig do not do this
2233 return 0; //Not in iframe mode!
2237 if (packet.disconti) {
2239 if (!DecodePacketlibav()) return 0;
2242 /*Inspect PES-Header */
2243 if (!dec_frame_libav_decoding) {
2244 dec_frame_libav_mutex.Lock();
2245 if (dec_frame_libav_free.size()>0) {
2246 dec_frame_libav_decoding=dec_frame_libav_free.front();
2247 dec_frame_libav_free.pop_front();
2248 dec_frame_libav_mutex.Unlock();
2250 Log::getInstance()->log("Video", Log::DEBUG, "We have no free buffers");
2251 dec_frame_libav_mutex.Unlock();
2259 if (*samplepos==0) {//stripheader
2260 headerstrip=buffer[packet.pos_buffer+8]+9/*is this right*/;
2261 *samplepos+=headerstrip;
2262 if ( packet.synched ) {
2264 if (!DecodePacketlibav()) return 0; // WriteOut old Data
2266 libav_time=packet.presentation_time;
2268 // reftime1=packet.presentation_time;
2269 // reftime2=reftime1+1;
2272 if (!firstsynched) {//
2273 *samplepos=packet.length;//if we have not processed at least one
2274 return packet.length;//synched packet ignore it!
2284 /*if (cur_input_buf_omx->nFilledLen==0) {//will only be changed on first packet
2285 /*if (packet.disconti) {
2286 ms->SetDiscontinuity(TRUE);
2288 ms->SetDiscontinuity(FALSE);
2290 //if (packet.synched) {
2292 //lastreftimePTS=packet.pts;
2293 if (omx_first_frame) { // TODO time
2294 cur_input_buf_omx->nFlags=OMX_BUFFERFLAG_STARTTIME;
2295 omx_first_frame=false;
2301 cur_input_buf_omx->nFlags=OMX_BUFFERFLAG_TIME_UNKNOWN;
2304 // ms->SetSyncPoint(TRUE);
2308 unsigned int haveToCopy=packet.length-*samplepos;
2310 if ((incom_packet_libav_size-incom_packet_libav.size)< haveToCopy) {
2311 // if the buffer is to small reallocate
2312 incom_packet_libav_size+=haveToCopy;
2313 incom_packet_libav.data=(uint8_t*)av_realloc(incom_packet_libav.data,incom_packet_libav_size+FF_INPUT_BUFFER_PADDING_SIZE);
2314 Log::getInstance()->log("Video", Log::DEBUG, "Reallocate avpacket buffer to %d", incom_packet_libav_size);
2316 memcpy(incom_packet_libav.data+incom_packet_libav.size,buffer+packet.pos_buffer+*samplepos,haveToCopy);
2317 incom_packet_libav.size+=haveToCopy;
2319 *samplepos+=haveToCopy;
2334 void VideoVPEOGL::ResetTimeOffsets()
2338 bool VideoVPEOGL::displayIFrame(const UCHAR* buffer, UINT length)
2340 //write(fdVideo, buffer, length);
2341 if (!iframemode) EnterIframePlayback();
2342 // WriteOutTS(buffer,length, h264?MPTYPE_VIDEO_H264 :MPTYPE_VIDEO_MPEG2 );
2348 int VideoVPEOGL::EnterIframePlayback()