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;
57 ogl_forward_ref_frame_num=0;
58 ogl_backward_ref_frame_num=0;
59 ogl_forward_ref_frame=NULL;
60 ogl_backward_ref_frame=NULL;
69 VideoVPEOGL::~VideoVPEOGL()
74 int VideoVPEOGL::init(UCHAR tformat)
76 if (initted) return 0;
79 // if ((fdVideo = open("/dev/vdec_dev", O_WRONLY)) < 0) return 0;
81 if (!setFormat(tformat)) { shutdown(); return 0; }
82 if (!setConnection(COMPOSITERGB)) { shutdown(); return 0; }
83 if (!setAspectRatio(ASPECT4X3)) { shutdown(); return 0; }
84 if (!setMode(NORMAL)) { shutdown(); return 0; }
85 if (!setSource()) { shutdown(); return 0; }
86 if (!attachFrameBuffer()) { shutdown(); return 0; }
90 /* if (format == PAL) setLetterboxBorder("38");
91 else setLetterboxBorder("31");*/
105 int VideoVPEOGL::initUsingOSDObjects()
107 EGLDisplay i_egl_display;
108 EGLSurface i_egl_surface;
109 EGLContext i_egl_context;
110 EGLConfig i_egl_config;
111 OsdOpenGL *osd=(OsdOpenGL*)osd->getInstance();
112 osd->getEGLObjs(&i_egl_display,&i_egl_surface,&i_egl_context, &i_egl_config);
113 const EGLint attr_context[]={
114 EGL_CONTEXT_CLIENT_VERSION,2,
118 egl_display=i_egl_display;
119 egl_context=eglCreateContext(egl_display,i_egl_config,i_egl_context,attr_context);
120 if (egl_context==EGL_NO_CONTEXT) {
121 Log::getInstance()->log("Video", Log::WARN, "Creating egl context failed! %x",eglGetError());
124 // We create a dummy surface here, in order to allow two contexts
125 const EGLint attr_pbuffer[]={
126 EGL_WIDTH, 1, EGL_HEIGHT,1,
129 egl_surface=eglCreatePbufferSurface(egl_display,i_egl_config,attr_pbuffer);
130 if (egl_surface==EGL_NO_SURFACE) {
131 Log::getInstance()->log("Video", Log::WARN, "Creating egl pbuffer failed! %x",eglGetError());
138 //egl_surface=i_egl_surface;
139 //egl_context=i_egl_context;
142 #ifdef VPE_OMX_SUPPORT
143 // we are called before the audio
146 if (error!=OMX_ErrorNone) {
147 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX failed %x", error);
151 //our callbacks move to play?
155 #ifdef VPE_LIBAV_SUPPORT
158 if (decoding_mode==VPE_NO_XVMC) {
159 mpeg2codec_libav=avcodec_find_decoder(CODEC_ID_MPEG2VIDEO);
161 mpeg2codec_libav=avcodec_find_decoder(CODEC_ID_MPEG2VIDEO_XVMC);
163 if (mpeg2codec_libav==NULL) {
164 Log::getInstance()->log("Video", Log::DEBUG, "Find libav mpeg2 codec failed");
174 #ifdef VPE_OMX_SUPPORT
176 OMX_ERRORTYPE VideoVPEOGL::EventHandler_OMX(OMX_IN OMX_HANDLETYPE handle,OMX_IN OMX_PTR appdata,
177 OMX_IN OMX_EVENTTYPE event_type,OMX_IN OMX_U32 data1,
178 OMX_IN OMX_U32 data2,OMX_IN OMX_PTR event_data) {
180 Log::getInstance()->log("Video", Log::NOTICE, "eventHandler %x %x %x %x %x",handle,event_type,data1,data2,event_data);
182 struct VPE_OMX_EVENT new_event;
183 new_event.handle=handle;
184 new_event.appdata=appdata;
185 new_event.event_type=event_type;
186 new_event.data1=data1;
187 new_event.data2=data2;
188 new_event.event_data=event_data;
190 VideoVPEOGL *video=(VideoVPEOGL *)getInstance();
191 video->AddOmxEvent(new_event);
193 /* switch (event_type) {
194 case OMX_EventCmdComplete: {
199 return OMX_ErrorNone;
203 void VideoVPEOGL::AddOmxEvent(VPE_OMX_EVENT new_event)
205 omx_event_mutex.Lock();
206 omx_events.push_back(new_event);
207 omx_event_mutex.Unlock();
211 OMX_ERRORTYPE VideoVPEOGL::EmptyBufferDone_OMX(OMX_IN OMX_HANDLETYPE hcomp,OMX_IN OMX_PTR appdata,OMX_IN OMX_BUFFERHEADERTYPE* buffer){
213 Log::getInstance()->log("Video", Log::NOTICE, "EmptyBufferDone");
214 VideoVPEOGL *video=(VideoVPEOGL *)getInstance();
215 video->ReturnEmptyOMXBuffer(buffer);
216 return OMX_ErrorNone;
220 void VideoVPEOGL::ReturnEmptyOMXBuffer(OMX_BUFFERHEADERTYPE* buffer){
221 input_bufs_omx_mutex.Lock();
222 Log::getInstance()->log("Video", Log::NOTICE, "ReturnEmptyOMXBuffer %d",input_bufs_omx_free.size());
223 input_bufs_omx_free.push_back(buffer);
224 Log::getInstance()->log("Video", Log::NOTICE, "ReturnEmptyOMXBuffer %d",input_bufs_omx_free.size());
225 input_bufs_omx_mutex.Unlock();
228 OMX_ERRORTYPE VideoVPEOGL::FillBufferDone_OMX(OMX_IN OMX_HANDLETYPE hcomp, OMX_IN OMX_PTR appdata,OMX_IN OMX_BUFFERHEADERTYPE* buffer) {
229 Log::getInstance()->log("Video", Log::NOTICE, "FillBufferDone");
230 return OMX_ErrorNone;
235 int VideoVPEOGL::shutdown()
237 if (!initted) return 0;
242 #ifdef VPE_OMX_SUPPORT
243 DeAllocateCodecsOMX();
246 #ifdef VPE_LIBAV_SUPPORT
247 DeAllocateCodecsLibav();
249 eglDestroyContext(egl_display,egl_context);
254 int VideoVPEOGL::AllocateYUV400OglTexture(VPEOGLFrame* outframe,int width,int height,int stride)
256 Log::getInstance()->log("Video", Log::NOTICE, "Allocate ogl texture 400");
258 glGenTextures(1, &outframe->textures[0]);
259 glBindTexture(GL_TEXTURE_2D, outframe->textures[0]);
260 glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, stride, height, 0, GL_LUMINANCE,
261 GL_UNSIGNED_BYTE, NULL);
263 glGenTextures(1, &outframe->textures[1]);
264 glBindTexture(GL_TEXTURE_2D, outframe->textures[1]);
265 glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, stride>>1, height>>1, 0, GL_LUMINANCE,
266 GL_UNSIGNED_BYTE, NULL);
268 glGenTextures(1, &outframe->textures[2]);
269 glBindTexture(GL_TEXTURE_2D, outframe->textures[2]);
270 glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, stride>>1, height>>1, 0, GL_LUMINANCE,
271 GL_UNSIGNED_BYTE, NULL);
272 outframe->height=height;
273 outframe->width=width;
274 outframe->stride=stride;
279 int VideoVPEOGL::AllocateYUV444OglTexture(VPEOGLFrame* outframe,int width,int height)
281 Log::getInstance()->log("Video", Log::NOTICE, "Allocate ogl texture 444");
283 glGenTextures(1, &outframe->textures[0]);
284 glBindTexture(GL_TEXTURE_2D, outframe->textures[0]);
285 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA,
286 GL_UNSIGNED_BYTE, NULL);
287 outframe->textures[1]=outframe->textures[2]=0; // no handles here
293 outframe->height=height;
294 outframe->width=width;
295 outframe->stride=width; // does not make any sense otherwiese
300 VPEOGLFrame *VideoVPEOGL::getReadyOGLFrame(){
301 VPEOGLFrame *return_obj=NULL;
302 ogl_frame_mutex.Lock();
303 if (ready_ogl_frames.size()>0) {
304 return_obj=ready_ogl_frames.front();
305 ready_ogl_frames.pop_front();
306 ogl_frame_outside=true;
308 ogl_frame_mutex.Unlock();
312 void VideoVPEOGL::returnOGLFrame(VPEOGLFrame *frame)
314 ogl_frame_mutex.Lock();
316 ogl_frame_outside=false;
317 //Log::getInstance()->log("Video", Log::WARN, "returnOGLRFrame mark incoming num: %d",frame->pict_num);
318 if (frame->pict_num==ogl_forward_ref_frame_num ||
319 frame->pict_num==ogl_backward_ref_frame_num ) {
320 recycle_ref_ogl_frames.push_back(frame);
322 free_ogl_frames.push_back(frame);
325 ogl_frame_mutex.Unlock();
328 void VideoVPEOGL::recycleOGLRefFrames()
330 // This function recycles frames formerly used as reference frame
331 ogl_frame_mutex.Lock();
332 list<VPEOGLFrame*> keep;
333 list<VPEOGLFrame*>::iterator itty=recycle_ref_ogl_frames.begin();
334 while (itty!=recycle_ref_ogl_frames.end()) {
335 Log::getInstance()->log("Video", Log::WARN, "recycleOGLRefFrame mark3");
336 if ((*itty)->pict_num==ogl_forward_ref_frame_num
337 || (*itty)->pict_num==ogl_backward_ref_frame_num)
339 // ok we have to keep this
340 keep.push_back(*itty);
342 free_ogl_frames.push_back(*itty);
346 recycle_ref_ogl_frames.clear();
347 recycle_ref_ogl_frames=keep;
349 ogl_frame_mutex.Unlock();
354 void VideoVPEOGL::threadMethod()
356 if (eglMakeCurrent(egl_display, egl_surface, egl_surface, egl_context)== EGL_FALSE) {
357 Log::getInstance()->log("Video", Log::WARN, "Making egl Current failed in thread %d",eglGetError());
364 #ifdef VPE_LIBAV_SUPPORT
365 dec_frame_libav_mutex.Lock();
368 if (dec_frame_libav_upload_and_view_pending.size()>0 ) {
370 dec_frame_libav_uploading=dec_frame_libav_upload_and_view_pending.front();
371 // Log::getInstance()->log("Video", Log::WARN, "threadMethod u and view %d",((VPE_FrameBuf*)dec_frame_libav_uploading->base[0])->pict_num);
372 // if (dec_frame_libav_upload_only_pending.size()>0) Log::getInstance()->log("Video", Log::WARN, "threadMethod u and view %d",
373 // ((VPE_FrameBuf*)dec_frame_libav_upload_only_pending.front()->base[0])->pict_num);
374 list<AVFrame*>::iterator itty=dec_frame_libav_upload_only_pending.begin();
375 while (itty!=dec_frame_libav_upload_only_pending.end())
377 if (*itty==dec_frame_libav_uploading) {
383 if (!blocked) { // this assures that no frame without an upload is pushed out to rendering
384 dec_frame_libav_upload_and_view_pending.pop_front();
386 if (decoding_mode!=VPE_NO_XVMC) upload=false;
388 dec_frame_libav_uploading=NULL;
392 if (!view && dec_frame_libav_upload_only_pending.size()>0) {
394 dec_frame_libav_uploading=dec_frame_libav_upload_only_pending.front();
395 //Log::getInstance()->log("Video", Log::WARN, "threadMethod u %d",((VPE_FrameBuf*)dec_frame_libav_uploading->base[0])->pict_num);
396 dec_frame_libav_upload_only_pending.pop_front();
402 if (dec_frame_libav_upload_and_view_pending.size()>0
403 ||dec_frame_libav_upload_only_pending.size()>0) sleep=false;
405 dec_frame_libav_mutex.Unlock();
408 if (dec_frame_libav_uploading) {
411 int width,height,pixfmt;
412 //First get a free ogl image
413 VPEOGLFrame* out_frame=NULL;
415 ogl_frame_mutex.Lock();
416 // Log::getInstance()->log("Video", Log::WARN, "threadMethod mark upload 1a %d %d %d %d %d",all_ogl_frames.size(),free_ogl_frames.size(),
417 // recycle_ref_ogl_frames.size(),ready_ogl_frames.size(),ogl_frame_outside);
418 if (all_ogl_frames.size()==0) {
419 ogl_frame_mutex.Unlock(); break;
422 if (free_ogl_frames.size()>0) {
426 out_frame=free_ogl_frames.front();
427 free_ogl_frames.pop_front();
428 } else MILLISLEEP(2);
429 ogl_frame_mutex.Unlock();
433 if (out_frame->textures[0]==0 || out_frame->width!=width ||
434 out_frame->height!=height || out_frame->stride!=dec_frame_libav_uploading->linesize[0]) {
435 if (out_frame->textures[0]!=0) {
436 glDeleteTextures(1,&out_frame->textures[0]);
437 out_frame->textures[0]=0;
439 if (out_frame->textures[1]!=0) {
440 glDeleteTextures(1,&out_frame->textures[1]);
441 out_frame->textures[1]=0;
443 if (out_frame->textures[2]!=0) {
444 glDeleteTextures(1,&out_frame->textures[2]);
445 out_frame->textures[2]=0;
448 if (decoding_mode==VPE_NO_XVMC) {
449 if (!AllocateYUV400OglTexture(out_frame,width,height,dec_frame_libav_uploading->linesize[0])) failed=true;
451 if (!AllocateYUV444OglTexture(out_frame,width,height)) failed=true; //We use a YUV 444 texture in this case
452 // the shaders are easier to implement
455 ((VPE_FrameBuf*)dec_frame_libav_uploading->base[0])->ogl_ref=out_frame;
457 //up to now only YUV data, this is for reference only, since the pi is too slow.
458 if (decoding_mode==VPE_NO_XVMC) {
459 glBindTexture(GL_TEXTURE_2D, out_frame->textures[0]);
460 glPixelStorei(GL_UNPACK_ALIGNMENT,1);
461 glTexSubImage2D(GL_TEXTURE_2D,0,0,0,
462 out_frame->stride,out_frame->height,
463 GL_LUMINANCE,GL_UNSIGNED_BYTE,
464 dec_frame_libav_uploading->data[0]);
466 glBindTexture(GL_TEXTURE_2D, out_frame->textures[1]);
467 glPixelStorei(GL_UNPACK_ALIGNMENT,1);
468 glTexSubImage2D(GL_TEXTURE_2D,0,0,0,
469 out_frame->stride>>1,out_frame->height>>1,
470 GL_LUMINANCE,GL_UNSIGNED_BYTE,
471 dec_frame_libav_uploading->data[1]);
473 glBindTexture(GL_TEXTURE_2D, out_frame->textures[2]);
474 glPixelStorei(GL_UNPACK_ALIGNMENT,1);
475 glTexSubImage2D(GL_TEXTURE_2D,0,0,0,
476 out_frame->stride>>1,out_frame->height>>1,
477 GL_LUMINANCE,GL_UNSIGNED_BYTE,
478 dec_frame_libav_uploading->data[2]);
481 xvmc_pix_fmt * pix_fmt=(xvmc_pix_fmt *)dec_frame_libav_uploading->data[2];
482 if (moco_shader && pix_fmt) {
484 moco_shader->uploadDataBlocks(pix_fmt->data_blocks,pix_fmt->next_free_data_block_num,
485 pix_fmt->mv_blocks , pix_fmt->filled_mv_blocks_num );
486 Log::getInstance()->log("Video", Log::WARN, "Pictnum %d Forward %d Backward %d pict_type%d",pix_fmt->p_surface,
487 pix_fmt->p_past_surface,pix_fmt->p_future_surface,dec_frame_libav_uploading->pict_type);
489 if (((int)pix_fmt->p_future_surface)!=0 && ogl_forward_ref_frame_num!=((int)pix_fmt->p_future_surface))
491 //Now determine the frame, that fits
492 ogl_frame_mutex.Lock();
493 for (int i=0;i<all_ogl_frames.size();i++) {
494 if (all_ogl_frames[i]->pict_num==((int)pix_fmt->p_future_surface))
496 ogl_backward_ref_frame=ogl_forward_ref_frame; //mpeg life time axioms
497 ogl_backward_ref_frame_num=ogl_forward_ref_frame_num;
498 ogl_forward_ref_frame=all_ogl_frames[i];
499 ogl_forward_ref_frame_num=(int)pix_fmt->p_future_surface;
504 ogl_frame_mutex.Unlock();
505 recycleOGLRefFrames(); //needed for recycling of reference frames
509 if (((int)pix_fmt->p_past_surface)!=0 && ogl_backward_ref_frame_num!=((int)pix_fmt->p_past_surface))
511 //Now determine the frame, that fits
512 ogl_frame_mutex.Lock();
513 for (int i=0;i<all_ogl_frames.size();i++) {
514 if (all_ogl_frames[i]->pict_num==((int)pix_fmt->p_past_surface))
516 ogl_backward_ref_frame=all_ogl_frames[i];
517 ogl_backward_ref_frame_num=(int)pix_fmt->p_past_surface; // This should not happen, or for DMV, who knows
522 ogl_frame_mutex.Unlock();
523 recycleOGLRefFrames(); //needed for recycling of reference frames
527 if (decoding_mode==VPE_XVMC_MOCOMP)
534 out_frame->pict_num=((VPE_FrameBuf*)dec_frame_libav_uploading->base[0])->pict_num;
536 moco_shader->doMoCo(out_frame,ogl_forward_ref_frame,ogl_backward_ref_frame);
538 // Excute motion compensation
553 //Log::getInstance()->log("Video", Log::WARN, "threadMethod mark view");
554 VPEOGLFrame* out_frame=((VPE_FrameBuf*)dec_frame_libav_uploading->base[0])->ogl_ref;
555 xvmc_pix_fmt * pix_fmt=(xvmc_pix_fmt *)dec_frame_libav_uploading->data[2];
556 Log::getInstance()->log("Video", Log::WARN, "View Pictnum %d Forward %d Backward %d pict_type%d",pix_fmt->p_surface,
557 pix_fmt->p_past_surface,pix_fmt->p_future_surface,dec_frame_libav_uploading->pict_type);
559 releaseFrameBufUpload((VPE_FrameBuf*)dec_frame_libav_uploading->base[0]);
560 ogl_frame_mutex.Lock();
561 ready_ogl_frames.push_back(out_frame);
562 ogl_frame_mutex.Unlock();
563 ((OsdOpenGL*)Osd::getInstance())->AdviseAboutNewFrame(); //Tell him, that we have a frame waiting
565 dec_frame_libav_mutex.Lock();
566 dec_frame_libav_free.push_back(dec_frame_libav_uploading);
567 dec_frame_libav_mutex.Unlock();
571 dec_frame_libav_mutex.Lock();
572 dec_frame_libav_uploading=NULL;
574 if (dec_frame_libav_upload_and_view_pending.size()>0
575 ||dec_frame_libav_upload_only_pending.size()>0) sleep=false;
577 dec_frame_libav_mutex.Unlock();
585 threadWaitForSignal();
586 Log::getInstance()->log("Video", Log::WARN, "threadMethod signalled");
593 void VideoVPEOGL::threadPostStopCleanup()
595 eglMakeCurrent(egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
596 #ifdef VPE_LIBAV_SUPPORT
597 dec_frame_libav_uploading=NULL;
604 int VideoVPEOGL::setTVsize(UCHAR ttvsize)
608 // Override the aspect ratio usage, temporarily use to set the video chip mode
609 if (!setAspectRatio(tvsize)) { shutdown(); return 0; }
611 if ((fdVideo = open("/dev/vdec_dev", O_WRONLY)) < 0) return 0;
612 if (!setSource()) { shutdown(); return 0; }
613 if (!attachFramebuffer()) { shutdown(); return 0; }
615 // Reopening the fd causes the scart aspect line to go back to 4:3
616 // Set this again to the same as the tv screen size
617 if (!setAspectRatio(tvsize)) { shutdown(); return 0; }
619 // mode == LETTERBOX is invalid if the TV is widescreen
620 if (tvsize == ASPECT16X9) setMode(NORMAL);
625 int VideoVPEOGL::setDefaultAspect()
627 return setAspectRatio(tvsize);
632 int VideoVPEOGL::setFormat(UCHAR tformat)
634 if (!initted) return 0;
635 if ((tformat != PAL) && (tformat != NTSC)) return 0;
638 // if (ioctl(fdVideo, AV_SET_VID_DISP_FMT, format) != 0) return 0;
654 int VideoVPEOGL::setConnection(UCHAR tconnection)
656 if (!initted) return 0;
657 if ((tconnection != COMPOSITERGB) && (tconnection != SVIDEO)) return 0;
658 connection = tconnection;
660 // if (ioctl(fdVideo, AV_SET_VID_OUTPUT, connection) != 0) return 0;
664 int VideoVPEOGL::setAspectRatio(UCHAR taspectRatio)
666 if (!initted) return 0;
667 if ((taspectRatio != ASPECT4X3) && (taspectRatio != ASPECT16X9)) return 0;
668 aspectRatio = taspectRatio;
670 Log::getInstance()->log("Video", Log::DEBUG, "Setting aspect to %i", aspectRatio);
672 // if (ioctl(fdVideo, AV_SET_VID_RATIO, aspectRatio) != 0) return 0;
676 int VideoVPEOGL::setMode(UCHAR tmode)
678 if (!initted) return 0;
680 if ((tmode == LETTERBOX) && (tvsize == ASPECT16X9)) return 0; // invalid mode
682 if ((tmode != NORMAL) && (tmode != LETTERBOX) && (tmode != UNKNOWN2) && (tmode != QUARTER) && (tmode != EIGHTH)
683 && (tmode != ZOOM) && (tmode != UNKNOWN6)) return 0;
686 // if (ioctl(fdVideo, AV_SET_VID_MODE, mode) != 0) return 0;
690 int VideoVPEOGL::signalOff()
692 // if (ioctl(fdVideo, AV_SET_VID_DENC, 0) != 0) return 0;
696 int VideoVPEOGL::signalOn()
698 // if (ioctl(fdVideo, AV_SET_VID_DENC, 1) != 0) return 0;
702 int VideoVPEOGL::setSource()
704 if (!initted) return 0;
706 // What does this do...
707 // if (ioctl(fdVideo, AV_SET_VID_SRC, 1) != 0) return 0;
711 int VideoVPEOGL::setPosition(int x, int y)
713 if (!initted) return 0;
715 // vid_pos_regs_t pos_d;
719 /* vid_pos_regs_t pos_d;
721 memset(&pos_d, 0, sizeof(pos_d));
746 pos_d.y = 100; // Top left X
747 pos_d.x = 50; // Top left Y
755 // if (ioctl(fdVideo, AV_SET_VID_POSITION, &pos_d) != 0) return 0;
759 int VideoVPEOGL::sync()
761 if (!initted) return 0;
763 // if (ioctl(fdVideo, AV_SET_VID_SYNC, 2) != 0) return 0;
770 int VideoVPEOGL::play()
772 if (!initted) return 0;
774 #ifdef VPE_OMX_SUPPORT
777 if (!omx_h264) doomx=false;
779 if (!omx_mpeg2) doomx=false;
782 if (AllocateCodecsOMX()) {
783 decoding_backend=VPE_DECODER_OMX;
785 // Otherwise fall back to libav
789 Log::getInstance()->log("Video", Log::NOTICE, "Allocate Codecs OMX failed assume h264 unsupported");
792 Log::getInstance()->log("Video", Log::NOTICE, "Allocate Codecs OMX failed assume mpeg2 unsupported");
797 #ifdef VPE_LIBAV_SUPPORT
798 if (AllocateCodecsLibav()) {
799 decoding_backend=VPE_DECODER_libav;
801 // Otherwise fall back to libav
810 #ifdef VPE_OMX_SUPPORT
811 int VideoVPEOGL::AllocateCodecsOMX()
814 static OMX_CALLBACKTYPE callbacks= {&EventHandler_OMX,&EmptyBufferDone_OMX,&FillBufferDone_OMX};
816 Log::getInstance()->log("Video", Log::NOTICE, "Allocate Codecs OMX");
817 //Clock, move later to audio
821 error=OMX_GetHandle(&omx_clock,VPE_OMX_CLOCK,NULL,&callbacks);
824 if (error!=OMX_ErrorNone){
825 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX clock failed %x", error);
826 DeAllocateCodecsOMX();
832 OMX_PORT_PARAM_TYPE p_param;
833 memset(&p_param,0,sizeof(p_param));
834 p_param.nSize=sizeof(p_param);
835 p_param.nVersion.nVersion=OMX_VERSION;
836 error=OMX_GetParameter(omx_clock,OMX_IndexParamOtherInit,&p_param);
837 if (error!=OMX_ErrorNone){
838 Log::getInstance()->log("Video", Log::DEBUG, "Init clock OMX_GetParameter failed %x", error);
839 DeAllocateCodecsOMX();
842 omx_clock_output_port=p_param.nStartPortNumber;
844 for (unsigned int i=0;i<p_param.nPorts;i++) {
845 if (!DisablePort(omx_clock,p_param.nStartPortNumber+i) ) {
846 Log::getInstance()->log("Video", Log::DEBUG, "Disable Ports OMX clock failed %d",i);
847 DeAllocateCodecsOMX();
853 OMX_TIME_CONFIG_CLOCKSTATETYPE clock_conf;
854 memset(&clock_conf,0,sizeof(clock_conf));
855 clock_conf.nSize=sizeof(clock_conf);
856 clock_conf.nVersion.nVersion=OMX_VERSION;
857 clock_conf.eState=OMX_TIME_ClockStateWaitingForStartTime;
858 clock_conf.nStartTime=0;
859 clock_conf.nOffset=0;
860 clock_conf.nWaitMask=OMX_CLOCKPORT0;
861 error=OMX_SetParameter(omx_clock,OMX_IndexConfigTimeClockState,&clock_conf);
862 if (error!=OMX_ErrorNone){
863 Log::getInstance()->log("Video", Log::DEBUG, "Clock IndexConfigTimeClockState failed %x", error);
864 DeAllocateCodecsOMX();
871 error=OMX_GetHandle(&omx_vid_dec,VPE_OMX_H264_DECODER,NULL,&callbacks);
873 error=OMX_GetHandle(&omx_vid_dec,VPE_OMX_MPEG2_DECODER,NULL,&callbacks);
876 if (error!=OMX_ErrorNone){
877 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX video decoder failed %x", error);
878 DeAllocateCodecsOMX();
884 memset(&p_param,0,sizeof(p_param));
885 p_param.nSize=sizeof(p_param);
886 p_param.nVersion.nVersion=OMX_VERSION;
887 error=OMX_GetParameter(omx_vid_dec,OMX_IndexParamVideoInit,&p_param);
888 if (error!=OMX_ErrorNone){
889 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX h264 decoder OMX_GetParameter failed %x", error);
890 DeAllocateCodecsOMX();
893 omx_codec_input_port=p_param.nStartPortNumber;
894 omx_codec_output_port=p_param.nStartPortNumber+1;
896 if (!DisablePort(omx_vid_dec,omx_codec_input_port) || !DisablePort(omx_vid_dec,omx_codec_output_port)) {
897 Log::getInstance()->log("Video", Log::DEBUG, "Disable Ports OMX video decoder failed");
898 DeAllocateCodecsOMX();
903 OMX_PARAM_BRCMVIDEODECODEERRORCONCEALMENTTYPE conceal;
904 memset(&conceal,0,sizeof(conceal));
905 conceal.nSize=sizeof(conceal);
906 conceal.nVersion.nVersion=OMX_VERSION;
907 conceal.bStartWithValidFrame=OMX_FALSE;
909 error=OMX_SetParameter(omx_vid_dec,OMX_IndexParamBrcmVideoDecodeErrorConcealment,&conceal);
910 if (error!=OMX_ErrorNone){
911 Log::getInstance()->log("Video", Log::DEBUG, "OMX_IndexParamBrcmVideoDecodeErrorConcealment failed %x", error);
912 DeAllocateCodecsOMX();
917 error=OMX_GetHandle(&omx_vid_sched,VPE_OMX_VIDEO_SCHED,NULL,&callbacks);
918 if (error!=OMX_ErrorNone){
919 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX video scheduler failed %x", error);
920 DeAllocateCodecsOMX();
926 error=OMX_GetParameter(omx_vid_sched,OMX_IndexParamVideoInit,&p_param);
927 if (error!=OMX_ErrorNone){
928 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX video scheduler OMX_GetParameter failed %x", error);
929 DeAllocateCodecsOMX();
932 omx_shed_input_port=p_param.nStartPortNumber;
933 omx_shed_output_port=p_param.nStartPortNumber+1;
936 error=OMX_GetParameter(omx_vid_sched,OMX_IndexParamOtherInit,&p_param);
937 if (error!=OMX_ErrorNone){
938 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX video scheduler OMX_GetParameter failed %x", error);
939 DeAllocateCodecsOMX();
942 omx_shed_clock_port=p_param.nStartPortNumber;
945 if (!DisablePort(omx_vid_sched,omx_shed_input_port) || !DisablePort(omx_vid_sched,omx_shed_output_port)
946 || !DisablePort(omx_vid_sched,omx_shed_clock_port)) {
947 Log::getInstance()->log("Video", Log::DEBUG, "Disable Ports OMX video shed failed");
948 DeAllocateCodecsOMX();
953 error=OMX_GetHandle(&omx_vid_rend,VPE_OMX_VIDEO_REND,NULL,&callbacks);
954 if (error!=OMX_ErrorNone){
955 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX video rend failed %x", error);
956 DeAllocateCodecsOMX();
960 error=OMX_GetParameter(omx_vid_rend,OMX_IndexParamVideoInit,&p_param);
961 if (error!=OMX_ErrorNone){
962 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX video rend OMX_GetParameter failed %x", error);
963 DeAllocateCodecsOMX();
966 omx_rend_input_port=p_param.nStartPortNumber;
967 //omx_rend_output_port=p_param.nStartPortNumber+1;
970 if (!DisablePort(omx_vid_rend,omx_rend_input_port) /*|| !DisablePort(omx_vid_rend,omx_rend_output_port)*/
972 Log::getInstance()->log("Video", Log::DEBUG, "Disable Ports OMX video rend failed");
973 DeAllocateCodecsOMX();
986 /* error=OMX_SendCommand(omx_vid_dec,OMX_CommandStateSet,OMX_StateIdle,0);
987 if (error!=OMX_ErrorNone){
988 Log::getInstance()->log("Video", Log::DEBUG, "vid_dec Send Command to OMX State Idle %x", error);
995 OMX_VIDEO_PARAM_PORTFORMATTYPE ft_type;
996 memset(&ft_type,0,sizeof(ft_type));
997 ft_type.nSize=sizeof(ft_type);
998 ft_type.nVersion.nVersion=OMX_VERSION;
1000 ft_type.nPortIndex=omx_codec_input_port;
1002 ft_type.eCompressionFormat=OMX_VIDEO_CodingAVC;
1004 ft_type.eCompressionFormat=OMX_VIDEO_CodingMPEG2;
1007 Demuxer* demux=Demuxer::getInstance();
1009 ft_type.xFramerate=0;//25*(1<<16);//demux->getFrameRate()*(1<<16);
1010 Log::getInstance()->log("Video", Log::DEBUG, "Framerate: %d",demux->getFrameRate());
1011 error=OMX_SetParameter(omx_vid_dec,OMX_IndexParamVideoPortFormat,&ft_type);
1012 if (error!=OMX_ErrorNone){
1013 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX_IndexParamVideoPortFormat failed %x", error);
1014 DeAllocateCodecsOMX();
1019 if (!ChangeComponentState(omx_vid_dec,OMX_StateIdle)) {
1020 Log::getInstance()->log("Video", Log::DEBUG, "vid_dec ChangeComponentState");
1021 DeAllocateCodecsOMX();
1026 if (!PrepareInputBufsOMX()) {
1027 DeAllocateCodecsOMX();
1032 if (!ChangeComponentState(omx_clock,OMX_StateIdle)) {
1033 Log::getInstance()->log("Video", Log::DEBUG, "omx_clock ChangeComponentState Idle");
1034 DeAllocateCodecsOMX();
1038 error=OMX_SetupTunnel(omx_clock,omx_clock_output_port,omx_vid_sched,omx_shed_clock_port);
1039 if (error!=OMX_ErrorNone){
1040 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);
1041 DeAllocateCodecsOMX();
1045 if (!EnablePort(omx_clock,omx_clock_output_port,false) || !EnablePort(omx_vid_sched,omx_shed_clock_port,false)
1047 Log::getInstance()->log("Video", Log::DEBUG, "Enable Ports OMX clock shed failed");
1048 DeAllocateCodecsOMX();
1052 if ( !CommandFinished(omx_vid_sched,OMX_CommandPortEnable,omx_shed_clock_port)) {
1053 DeAllocateCodecsOMX();
1057 error=OMX_SetupTunnel(omx_vid_dec,omx_codec_output_port,omx_vid_sched,omx_shed_input_port);
1058 if (error!=OMX_ErrorNone){
1059 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel dec to sched failed %x", error);
1060 DeAllocateCodecsOMX();
1066 if (!EnablePort(omx_vid_dec,omx_codec_output_port,false) || !EnablePort(omx_vid_sched,omx_shed_input_port,false)
1068 Log::getInstance()->log("Video", Log::DEBUG, "Enable Ports OMX codec shed failed");
1069 DeAllocateCodecsOMX();
1073 if ( !CommandFinished(omx_vid_sched,OMX_CommandPortEnable,omx_shed_input_port)) {
1076 if (!ChangeComponentState(omx_vid_sched,OMX_StateIdle)) {
1077 Log::getInstance()->log("Video", Log::DEBUG, "vid_sched idle ChangeComponentState");
1078 DeAllocateCodecsOMX();
1081 if (!CommandFinished(omx_clock,OMX_CommandPortEnable,omx_clock_output_port)
1082 ||!CommandFinished(omx_vid_dec,OMX_CommandPortEnable,omx_codec_output_port)){
1083 DeAllocateCodecsOMX();
1087 if (!ChangeComponentState(omx_vid_dec,OMX_StateExecuting)) {
1088 Log::getInstance()->log("Video", Log::DEBUG, "omx_vid_dec ChangeComponentState Execute");
1089 DeAllocateCodecsOMX();
1093 error=OMX_SetupTunnel(omx_vid_sched,omx_shed_output_port,omx_vid_rend,omx_rend_input_port);
1094 if (error!=OMX_ErrorNone){
1095 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel sched to rend failed %x", error);
1096 DeAllocateCodecsOMX();
1100 if (!EnablePort(omx_vid_sched,omx_shed_output_port,false) || !EnablePort(omx_vid_rend,omx_rend_input_port,false)
1102 Log::getInstance()->log("Video", Log::DEBUG, "Enable Ports OMX shed rend failed");
1103 DeAllocateCodecsOMX();
1107 if (!CommandFinished(omx_vid_sched,OMX_CommandPortEnable,omx_shed_output_port)
1108 || !CommandFinished(omx_vid_rend,OMX_CommandPortEnable,omx_rend_input_port)) {
1109 DeAllocateCodecsOMX();
1113 if (!ChangeComponentState(omx_vid_rend,OMX_StateIdle)) {
1114 Log::getInstance()->log("Video", Log::DEBUG, "vid_rend ChangeComponentState");
1115 DeAllocateCodecsOMX();
1120 if (!ChangeComponentState(omx_vid_sched,OMX_StateExecuting)) {
1121 Log::getInstance()->log("Video", Log::DEBUG, "omx_vid_sched ChangeComponentState Execute");
1122 DeAllocateCodecsOMX();
1126 if (!ChangeComponentState(omx_vid_rend,OMX_StateExecuting)) {
1127 Log::getInstance()->log("Video", Log::DEBUG, "omx_vid_rend ChangeComponentState Execute");
1128 DeAllocateCodecsOMX();
1133 OMX_CONFIG_DISPLAYREGIONTYPE dispconf;
1134 memset(&dispconf,0,sizeof(dispconf));
1135 dispconf.nSize=sizeof(dispconf);
1136 dispconf.nVersion.nVersion=OMX_VERSION;
1138 dispconf.nPortIndex=omx_rend_input_port;
1140 dispconf.set=OMX_DISPLAY_SET_LAYER ;
1142 error=OMX_SetParameter(omx_vid_rend,OMX_IndexConfigDisplayRegion,&dispconf);
1143 if (error!=OMX_ErrorNone){
1144 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX_IndexConfigDisplayRegion failed %x", error);
1145 DeAllocateCodecsOMX();
1149 /* dispconf.set=OMX_DISPLAY_SET_FULLSCREEN ;
1150 dispconf.fullscreen=OMX_FALSE;
1151 error=OMX_SetParameter(omx_vid_rend,OMX_IndexConfigDisplayRegion,&dispconf);
1152 if (error!=OMX_ErrorNone){
1153 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX_IndexConfigDisplayRegion failed %x", error);
1154 DeAllocateCodecsOMX();
1158 dispconf.set=OMX_DISPLAY_SET_DEST_RECT;
1159 dispconf.dest_rect.x_offset=100;
1160 dispconf.dest_rect.y_offset=100;
1161 dispconf.dest_rect.width=640;
1162 dispconf.dest_rect.height=480;
1163 error=OMX_SetParameter(omx_vid_rend,OMX_IndexConfigDisplayRegion,&dispconf);
1164 if (error!=OMX_ErrorNone){
1165 Log::getInstance()->log("Video", Log::DEBUG, "Init OMX_IndexConfigDisplayRegion failed %x", error);
1166 DeAllocateCodecsOMX();
1173 if (!ChangeComponentState(omx_clock,OMX_StateExecuting)) {
1174 Log::getInstance()->log("Video", Log::DEBUG, "omx_clock ChangeComponentState Exccute");
1175 DeAllocateCodecsOMX();
1185 int VideoVPEOGL::ChangeComponentState(OMX_HANDLETYPE handle,OMX_STATETYPE type)
1187 OMX_ERRORTYPE error;
1188 error=OMX_SendCommand(handle,OMX_CommandStateSet,type,0);
1189 if (error!=OMX_ErrorNone){
1190 Log::getInstance()->log("Video", Log::DEBUG, "handle %x Send Command to OMX State %x %x",handle,type, error);
1194 if (!CommandFinished(handle,OMX_CommandStateSet,type)) {
1202 int VideoVPEOGL::EnablePort(OMX_HANDLETYPE handle,OMX_U32 port,bool wait)
1204 OMX_ERRORTYPE error;
1205 error=OMX_SendCommand(handle,OMX_CommandPortEnable,port,0);
1206 if (error!=OMX_ErrorNone){
1207 Log::getInstance()->log("Video", Log::DEBUG, "handle %x Send Command to enable port %x %x",handle,port, error);
1211 if (!wait) return 1;
1212 if (!CommandFinished(handle,OMX_CommandPortEnable,port)) {
1220 int VideoVPEOGL::DisablePort(OMX_HANDLETYPE handle,OMX_U32 port,bool wait)
1222 OMX_ERRORTYPE error;
1223 error=OMX_SendCommand(handle,OMX_CommandPortDisable,port,0);
1224 if (error!=OMX_ErrorNone){
1225 Log::getInstance()->log("Video", Log::DEBUG, "handle %x Send Command to disable port %x %x",handle,port, error);
1229 if (!wait) return 1;
1230 if (!CommandFinished(handle,OMX_CommandPortDisable,port)) {
1241 int VideoVPEOGL::CommandFinished(OMX_HANDLETYPE handle,OMX_U32 command,OMX_U32 data2)
1245 omx_event_mutex.Lock();
1246 list<VPE_OMX_EVENT>::iterator itty=omx_events.begin();
1247 while (itty!=omx_events.end()) {
1249 VPE_OMX_EVENT current=*itty;
1250 if (current.handle==handle) { //this is ours
1251 if (current.event_type==OMX_EventError) {
1252 omx_events.erase(itty);
1253 omx_event_mutex.Unlock();
1256 } else if (current.event_type==OMX_EventCmdComplete && current.data1==command && current.data2==data2) {
1257 omx_events.erase(itty);
1258 omx_event_mutex.Unlock();
1265 omx_event_mutex.Unlock();
1270 Log::getInstance()->log("Video", Log::DEBUG, "CommandFinished waited too long %x %x %x",handle,command, data2);
1279 int VideoVPEOGL::PrepareInputBufsOMX()
1281 OMX_ERRORTYPE error;
1282 OMX_PARAM_PORTDEFINITIONTYPE port_def_type;
1283 memset(&port_def_type,0,sizeof(port_def_type));
1284 port_def_type.nSize=sizeof(port_def_type);
1285 port_def_type.nVersion.nVersion=OMX_VERSION;
1286 port_def_type.nPortIndex=omx_codec_input_port;
1288 error=OMX_GetParameter(omx_vid_dec,OMX_IndexParamPortDefinition, &port_def_type);
1290 if (error!=OMX_ErrorNone){
1291 Log::getInstance()->log("Video", Log::DEBUG, "Get OMX OMX_IndexParamPortDefinition failed %x", error);
1293 /* Log::getInstance()->log("Video", Log::DEBUG, "Port para %d %d %d %d %d %d %d", port_def_type.nBufferCountActual,
1294 port_def_type.nBufferCountMin,port_def_type.nBufferSize,port_def_type.bEnabled,port_def_type.bPopulated,
1295 port_def_type.bBuffersContiguous,port_def_type.nBufferAlignment);*/
1297 port_def_type.nBufferCountActual=60;
1299 error=OMX_SetParameter(omx_vid_dec,OMX_IndexParamPortDefinition, &port_def_type);
1301 if (error!=OMX_ErrorNone){
1302 Log::getInstance()->log("Video", Log::DEBUG, "Set OMX OMX_IndexParamPortDefinition failed %x", error);
1306 error=OMX_SendCommand(omx_vid_dec,OMX_CommandPortEnable,omx_codec_input_port,0);
1307 if (error!=OMX_ErrorNone){
1308 Log::getInstance()->log("Video", Log::DEBUG, "Prepare Input bufs Send Command to enable port %x", error);
1312 input_bufs_omx_mutex.Lock();
1313 for (unsigned int i=0; i< port_def_type.nBufferCountActual;i++) {
1315 // unsigned char* new_buffer_data=(unsigned char*)malloc(port_def_type.nbufferSize);
1316 OMX_BUFFERHEADERTYPE *buf_head=NULL;
1317 /* error=OMX_Usebuffer(omx_vid_dec,&buf_head,omx_codec_input_port,NULL,port_def_type.nbufferSize,new_buffer_data);
1318 if (error!=OMX_ErrorNone){
1319 Log::getInstance()->log("Video", Log::DEBUG, "Use OMX_Usebuffer failed %x", error);
1320 input_bufs_omx_mutex.Unlock();
1323 error=OMX_AllocateBuffer(omx_vid_dec,&buf_head,omx_codec_input_port,NULL,port_def_type.nBufferSize);
1324 if (error!=OMX_ErrorNone){
1325 Log::getInstance()->log("Video", Log::DEBUG, "Use OMX_AllocateBuffer failed %x", error);
1326 input_bufs_omx_mutex.Unlock();
1329 input_bufs_omx_all.push_back(buf_head);
1330 input_bufs_omx_free.push_back(buf_head);
1332 omx_first_frame=true;
1335 cur_input_buf_omx=NULL;
1336 input_bufs_omx_mutex.Unlock();
1339 Log::getInstance()->log("Video", Log::DEBUG, "PrepareInputBufsOMX mark3");
1340 if (!CommandFinished(omx_vid_dec,OMX_CommandPortEnable,omx_codec_input_port)) {
1343 Log::getInstance()->log("Video", Log::DEBUG, "PrepareInputBufsOMX mark4");
1348 int VideoVPEOGL::DestroyInputBufsOMX()
1350 OMX_ERRORTYPE error;
1352 cur_input_buf_omx=NULL;
1353 input_bufs_omx_mutex.Lock();
1354 for (int i=0; i< input_bufs_omx_all.size();i++) {
1355 // free(input_bufs_omx_all[i]->pBuffer);
1356 // input_bufs_omx_all[i]->pBuffer=NULL;
1357 error=OMX_FreeBuffer(omx_vid_dec,omx_codec_input_port,input_bufs_omx_all[i]);
1358 if (error!=OMX_ErrorNone){
1359 Log::getInstance()->log("Video", Log::DEBUG, "Use OMX_FreeBuffer failed %x", error);
1360 input_bufs_omx_mutex.Unlock();
1364 input_bufs_omx_all.clear();
1365 input_bufs_omx_free.clear();
1366 input_bufs_omx_mutex.Unlock();
1373 int VideoVPEOGL::DeAllocateCodecsOMX()
1375 OMX_ERRORTYPE error;
1378 // first flush all buffers
1380 error=OMX_SendCommand(omx_vid_dec,OMX_CommandFlush, omx_codec_output_port, NULL);
1381 if (error!=OMX_ErrorNone){
1382 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Flush codec out failed %x", error);
1386 error=OMX_SendCommand(omx_vid_sched,OMX_CommandFlush, omx_shed_input_port, NULL);
1387 if (error!=OMX_ErrorNone){
1388 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Flush shed in failed %x", error);
1392 if (!CommandFinished(omx_vid_dec,OMX_CommandFlush,omx_codec_output_port) ||
1393 !CommandFinished(omx_vid_sched,OMX_CommandFlush,omx_shed_input_port)) {
1394 Log::getInstance()->log("Video", Log::DEBUG, "flush cmd codec shed failed");
1397 error=OMX_SendCommand(omx_clock,OMX_CommandFlush, omx_clock_output_port, NULL);
1398 if (error!=OMX_ErrorNone){
1399 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Flush clock out failed %x", error);
1403 error=OMX_SendCommand(omx_vid_sched,OMX_CommandFlush, omx_shed_clock_port, NULL);
1404 if (error!=OMX_ErrorNone){
1405 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Flush shed clock failed %x", error);
1409 if (!CommandFinished(omx_clock,OMX_CommandFlush,omx_clock_output_port) ||
1410 !CommandFinished(omx_vid_sched,OMX_CommandFlush,omx_shed_clock_port)) {
1411 Log::getInstance()->log("Video", Log::DEBUG, "flush cmd clock shed failed");
1414 error=OMX_SendCommand(omx_vid_sched,OMX_CommandFlush, omx_shed_output_port, NULL);
1415 if (error!=OMX_ErrorNone) {
1416 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Flush shed out failed %x", error);
1420 error=OMX_SendCommand(omx_vid_rend,OMX_CommandFlush, omx_rend_input_port, NULL);
1421 if (error!=OMX_ErrorNone) {
1422 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Flush rend in failed %x", error);
1426 if (!CommandFinished(omx_vid_sched,OMX_CommandFlush,omx_shed_output_port) ||
1427 !CommandFinished(omx_vid_rend,OMX_CommandFlush,omx_rend_input_port)) {
1428 Log::getInstance()->log("Video", Log::DEBUG, "flush cmd shed rend failed");
1434 error=OMX_SendCommand(omx_vid_dec,OMX_CommandFlush, omx_codec_input_port, NULL);
1435 if (error!=OMX_ErrorNone){
1436 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Flush codec out failed %x", error);
1441 if (!CommandFinished(omx_vid_dec,OMX_CommandFlush,omx_codec_input_port)) {
1442 Log::getInstance()->log("Video", Log::DEBUG, "flush cmd codec input failed");
1445 DestroyInputBufsOMX();
1448 if (!DisablePort(omx_vid_rend,omx_rend_input_port,true)) {
1449 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 1");
1451 if (!DisablePort(omx_vid_sched,omx_shed_output_port,true)) {
1452 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 2 ");
1455 error=OMX_SetupTunnel(omx_vid_rend,omx_rend_input_port,NULL,NULL);
1456 if (error!=OMX_ErrorNone){
1457 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1461 error=OMX_SetupTunnel(omx_vid_sched,omx_shed_output_port,NULL,NULL);
1462 if (error!=OMX_ErrorNone){
1463 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1467 if (!DisablePort(omx_vid_sched,omx_shed_input_port,true)) {
1468 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 3");
1471 if (!DisablePort(omx_vid_sched,omx_shed_clock_port,true)) {
1472 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 4");
1475 if (!DisablePort(omx_clock,omx_clock_output_port,true)) {
1476 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 5");
1478 if (!DisablePort(omx_vid_dec,omx_codec_output_port,true)) {
1479 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 6");
1484 if (!DisablePort(omx_vid_dec,omx_codec_input_port,true)) {
1485 Log::getInstance()->log("Video", Log::DEBUG, "Disable Tunnel Port failed 7");
1489 error=OMX_SetupTunnel(omx_vid_sched,omx_shed_input_port,NULL,NULL);
1490 if (error!=OMX_ErrorNone){
1491 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1494 error=OMX_SetupTunnel(omx_vid_sched,omx_shed_clock_port,NULL,NULL);
1495 if (error!=OMX_ErrorNone){
1496 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1500 error=OMX_SetupTunnel(omx_clock,omx_clock_output_port,NULL,NULL);
1501 if (error!=OMX_ErrorNone){
1502 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1506 error=OMX_SetupTunnel(omx_vid_dec,omx_codec_output_port,NULL,NULL);
1507 if (error!=OMX_ErrorNone){
1508 Log::getInstance()->log("Video", Log::DEBUG, "OMX_Setup tunnel teardown failed %x", error);
1516 error=OMX_FreeHandle(omx_vid_dec);
1517 error=OMX_FreeHandle(omx_vid_sched);
1518 error=OMX_FreeHandle(omx_vid_rend);
1519 error=OMX_FreeHandle(omx_clock);
1521 if (error!=OMX_ErrorNone) {
1522 Log::getInstance()->log("Video", Log::DEBUG, "FreeHandle failed %d", error);
1532 #ifdef VPE_LIBAV_SUPPORT
1534 enum PixelFormat VideoVPEOGL::get_format_libav(struct AVCodecContext *s, const enum PixelFormat *fmt)
1536 int dec_mode=((VideoVPEOGL*)getInstance())->getlibavDecodingMode();
1537 enum PixelFormat ret_pix=PIX_FMT_NONE;
1538 if (dec_mode==VPE_NO_XVMC) return PIX_FMT_NONE;
1539 while (*fmt!=PIX_FMT_NONE) {
1540 if (*fmt== PIX_FMT_XVMC_MPEG2_IDCT && dec_mode==VPE_XVMC_IDCT) {
1541 ret_pix=PIX_FMT_XVMC_MPEG2_IDCT;
1542 } else if (*fmt== PIX_FMT_XVMC_MPEG2_MC && dec_mode==VPE_XVMC_MOCOMP) {
1543 ret_pix=PIX_FMT_XVMC_MPEG2_MC;
1550 // we use this function to push the data to ogl out before hand, this is only useful for xvmc
1551 void VideoVPEOGL::draw_horiz_band_libav(struct AVCodecContext *s, const AVFrame *src, int offset[4], int y, int type, int height)
1553 if ((y+height)==src->height) {
1554 //Log::getInstance()->log("Video", Log::NOTICE, "draw_horiz_band %d %d %d %d!",y,type,height,((xvmc_pix_fmt *)src->data[2])->p_surface);
1555 ((VideoVPEOGL*)Video::getInstance())->add_dec_frame_upload_only(s,src);
1560 int VideoVPEOGL::reget_buffer_libav(struct AVCodecContext *c, AVFrame *pic)
1562 Log::getInstance()->log("Video", Log::ERR, "Buffer reusing! Should not happen!Not Implemented!");
1566 int VideoVPEOGL::get_buffer_libav(struct AVCodecContext *c, AVFrame *pic)
1568 unsigned int want_sizes[4]={0,0,0,0};
1570 bool normal_pixs=false;
1572 int num_dct_blocks=0;
1575 //reget logic from mplayer
1576 if (pic->opaque && pic->data[0] && (!pic->buffer_hints ||pic->buffer_hints & FF_BUFFER_HINTS_REUSABLE )){
1577 Log::getInstance()->log("Video", Log::ERR, "Buffer reusing! Should not happen!");
1582 if (c->pix_fmt!=PIX_FMT_XVMC_MPEG2_IDCT &&c->pix_fmt!=PIX_FMT_XVMC_MPEG2_MC) {
1584 // standard pixel format
1585 // this is written using much inspiration from libav util.c, so portions from there
1591 avcodec_align_dimensions2(c, &width, &height, s_a);
1592 if ((c->flags & CODEC_FLAG_EMU_EDGE)==0) {
1596 // Now we have to determine alignment size
1597 bool unaligned=true;
1599 av_image_fill_linesizes(pict.linesize, c->pix_fmt, width); //linesizes are derived
1600 width+=width & ~(width-1); //trick from libav, now determine, if the alignment is ok
1602 for (int i=0;i<4;i++) {
1603 if ((pict.linesize[i]%s_a[i])!=0) {
1609 int tot_size=av_image_fill_pointers(pict.data, c->pix_fmt, height, NULL, pict.linesize); //get sizes
1610 for (int i=0;i<4 ;i++) {
1611 if (i<3 && pict.data[i+1]) {
1612 want_sizes[i]=pict.data[i+1]-pict.data[i];
1615 want_sizes[i]=(tot_size-(pict.data[i]-pict.data[0]));
1622 //TODO set linesizes!
1624 num_blocks=((c->width+15)/16)*((c->height+15)/16);
1625 num_dct_blocks=num_blocks*6; //6 blocks per macroblock
1627 want_sizes[2]=sizeof(xvmc_pix_fmt);
1628 want_sizes[1]=sizeof(short)*num_dct_blocks*8*8;
1629 want_sizes[0]=sizeof(XvMCMacroBlock)*num_blocks;
1630 pict.linesize[0]=pict.linesize[1]=pict.linesize[2]=pict.linesize[3]=0;
1634 VPE_FrameBuf *frame_buf=((VideoVPEOGL*)Video::getInstance())->getFrameBuf(want_sizes);
1636 //Log::getInstance()->log("Video", Log::NOTICE, "get buffer %x",frame_buf);
1638 Log::getInstance()->log("Video", Log::ERR, "Getting buffer libav failed");
1644 pic->type=FF_BUFFER_TYPE_USER; // we are controlling the buffers
1645 int hchr_shift,vchr_shift;
1646 avcodec_get_chroma_sub_sample(c->pix_fmt,&hchr_shift,&vchr_shift);
1647 const int pixel_size = av_pix_fmt_descriptors[c->pix_fmt].comp[0].step_minus1+1;
1648 for (int i=0;i<4;i++) {
1649 pic->data[i]=(uint8_t*)frame_buf->data[i];
1650 pic->linesize[i]=pict.linesize[i];
1656 edge_width>>=hchr_shift;
1657 edge_height>>=vchr_shift;
1659 pic->data[i]+=FFALIGN((pic->linesize[i]*16) + (pixel_size*edge_width), s_a[i]);
1663 pic->base[0]=(uint8_t*)frame_buf; // our structure
1664 //pic->extended_data=pic->data;
1665 if(c->pkt) pic->pkt_pts=c->pkt->pts;
1666 else pic->pkt_pts=AV_NOPTS_VALUE;
1667 pic->width=c->width;
1668 pic->height=c->height;
1669 pic->format=c->pix_fmt;
1670 pic->sample_aspect_ratio=c->sample_aspect_ratio;
1671 pic->reordered_opaque= c->reordered_opaque;
1674 xvmc_pix_fmt *pix_xvmc=(xvmc_pix_fmt *)pic->data[2];
1675 pix_xvmc->xvmc_id=AV_XVMC_ID;
1676 pix_xvmc->data_blocks=(short*)pic->data[1];
1677 pix_xvmc->mv_blocks=(XvMCMacroBlock*)pic->data[0];
1678 pix_xvmc->allocated_mv_blocks=num_blocks;
1679 pix_xvmc->allocated_data_blocks=num_dct_blocks;
1680 if (c->pix_fmt==PIX_FMT_XVMC_MPEG2_IDCT) pix_xvmc->idct=1;
1681 else pix_xvmc->idct=0;
1682 pix_xvmc->unsigned_intra=1; // let see what happens
1683 pix_xvmc->p_surface=(XvMCSurface*)frame_buf->pict_num;
1684 pix_xvmc->start_mv_blocks_num=0;
1685 pix_xvmc->filled_mv_blocks_num=0;
1686 pix_xvmc->next_free_data_block_num=0;
1696 void VideoVPEOGL::release_buffer_libav(struct AVCodecContext *c, AVFrame *pic)
1698 // Log::getInstance()->log("Video", Log::NOTICE, "release buffer %x",pic->base[0]);
1699 ((VideoVPEOGL*)Video::getInstance())->releaseFrameBufLibav((VPE_FrameBuf*) pic->base[0]);
1701 pic->data[0]=pic->data[1]=pic->data[2]=pic->data[3]=NULL;
1706 int VideoVPEOGL::AllocateCodecsLibav()
1708 libav_hastime=false;
1709 Log::getInstance()->log("Video", Log::NOTICE, "AllocateCodecslibav");
1710 mpeg2codec_context_libav=avcodec_alloc_context();
1711 if (mpeg2codec_context_libav==NULL) {
1712 Log::getInstance()->log("Video", Log::DEBUG, "Creating libav codec context failed");
1715 if (decoding_mode!=VPE_NO_XVMC) {
1716 mpeg2codec_context_libav->slice_flags=SLICE_FLAG_CODED_ORDER|SLICE_FLAG_ALLOW_FIELD;
1717 if (decoding_mode==VPE_XVMC_MOCOMP) mpeg2codec_context_libav->pix_fmt=PIX_FMT_XVMC_MPEG2_MC;
1718 else mpeg2codec_context_libav->pix_fmt=PIX_FMT_XVMC_MPEG2_IDCT;
1719 mpeg2codec_context_libav->get_format=get_format_libav;
1720 mpeg2codec_context_libav->draw_horiz_band=draw_horiz_band_libav;
1723 mpeg2codec_context_libav->get_buffer=get_buffer_libav;
1724 mpeg2codec_context_libav->reget_buffer=reget_buffer_libav;
1725 mpeg2codec_context_libav->release_buffer=release_buffer_libav;
1729 int avc_ret=avcodec_open(mpeg2codec_context_libav, mpeg2codec_libav);
1731 Log::getInstance()->log("Video", Log::DEBUG, "Opening libav codec failed ");
1734 memset(&incom_packet_libav,0,sizeof(incom_packet_libav));
1735 incom_packet_libav_size=200000;
1736 incom_packet_libav.data=(uint8_t*)av_malloc(incom_packet_libav_size+FF_INPUT_BUFFER_PADDING_SIZE);
1738 dec_frame_libav_mutex.Lock();
1739 for (int i=0;i<3;i++) {
1740 AVFrame *dec_frame_libav=avcodec_alloc_frame(); // may be we need multiple frames, if we want to use async texture upload
1741 if (!dec_frame_libav) {
1742 Log::getInstance()->log("Video", Log::DEBUG, "Allocating dec_frame failed");
1745 dec_frame_libav_all.push_back(dec_frame_libav);
1746 dec_frame_libav_free.push_back(dec_frame_libav);
1748 dec_frame_libav_decoding=NULL;
1749 dec_frame_libav_mutex.Unlock();
1751 ogl_frame_mutex.Lock();
1752 //Allocate texture structs, since we do not know the sizes, we do not allocate the textures yet
1753 for (int i=0;i<5;i++) {
1754 VPEOGLFrame *new_frame=(VPEOGLFrame *)malloc(sizeof(VPEOGLFrame));
1755 new_frame->type=1; //1 = YUV, 2 RGB
1756 new_frame->textures[0]=0;
1757 new_frame->textures[1]=0;
1758 new_frame->textures[2]=0;
1759 new_frame->width=new_frame->height=0;
1760 all_ogl_frames.push_back(new_frame);
1761 free_ogl_frames.push_back(new_frame);
1764 ogl_frame_outside=false;
1766 ogl_frame_mutex.Unlock();
1768 if (decoding_mode==VPE_XVMC_MOCOMP) {
1769 OsdOpenGL* osd=(OsdOpenGL*)osd->getInstance();
1770 osd->BeginPainting(); // get OpenGl context
1771 moco_shader=new GLMocoShader();
1772 moco_shader->init();
1782 int VideoVPEOGL::DeAllocateCodecsLibav()
1784 libav_running=false;
1785 Log::getInstance()->log("Video", Log::NOTICE, "DeAllocateCodecslibav");
1786 dec_frame_libav_mutex.Lock();
1787 dec_frame_libav_upload_and_view_pending.clear();
1788 dec_frame_libav_upload_only_pending.clear();
1789 dec_frame_libav_free.clear();
1790 dec_frame_libav_mutex.Unlock();
1791 while (dec_frame_libav_uploading) {
1792 Log::getInstance()->log("Video", Log::NOTICE, "Wait for uploading to finish");
1795 dec_frame_libav_mutex.Lock();
1796 for (int i=0; i< dec_frame_libav_all.size();i++) {
1797 av_free(dec_frame_libav_all[i]);
1800 dec_frame_libav_all.clear();
1802 av_free(incom_packet_libav.data);
1803 incom_packet_libav.data=NULL;
1804 incom_packet_libav_size=0;
1806 dec_frame_libav_mutex.Unlock();
1807 dec_frame_libav_decoding=NULL;
1808 while (ogl_frame_outside) {
1809 Log::getInstance()->log("Video", Log::NOTICE, "Wait for ogl frame from outside");
1813 ((OsdOpenGL*)Osd::getInstance())->BeginPainting(); // get osd's context
1814 ogl_frame_mutex.Lock();
1815 for (int i=0; i< dec_frame_libav_all.size();i++) {
1816 VPEOGLFrame * del_frame=all_ogl_frames[i];
1817 if (del_frame->textures[0]==0) {
1818 glDeleteTextures(1,&del_frame->textures[0]);
1819 del_frame->textures[0]=0;
1821 if (del_frame->textures[1]==0) {
1822 glDeleteTextures(1,&del_frame->textures[1]);
1823 del_frame->textures[1]=0;
1825 if (del_frame->textures[2]==0) {
1826 glDeleteTextures(1,&del_frame->textures[2]);
1827 del_frame->textures[2]=0;
1830 free(all_ogl_frames[i]);
1832 all_ogl_frames.clear();
1833 free_ogl_frames.clear();
1834 ready_ogl_frames.clear();
1835 recycle_ref_ogl_frames.clear();
1836 ogl_forward_ref_frame_num=0;
1837 ogl_backward_ref_frame_num=0;
1838 ogl_forward_ref_frame=NULL;
1839 ogl_backward_ref_frame=NULL;
1841 ogl_frame_mutex.Unlock();
1842 ((OsdOpenGL*)Osd::getInstance())->EndPainting();
1845 if (mpeg2codec_context_libav) {
1846 avcodec_close(mpeg2codec_context_libav);
1847 av_free(mpeg2codec_context_libav);
1848 mpeg2codec_context_libav=NULL;
1854 vpe_framebuf_mutex.Lock();
1856 for (int i=0;i<all_frame_bufs.size();i++) {
1857 VPE_FrameBuf* current=all_frame_bufs[i];
1858 for (int x=0;x<4;x++) {
1859 if (current->data[x]) {
1860 av_free(current->data[x]);
1865 all_frame_bufs.clear();
1866 free_frame_bufs.clear();
1867 locked_libav_frame_buf.clear();
1868 locked_uploading_frame_buf.clear();
1870 vpe_framebuf_mutex.Unlock();
1875 OsdOpenGL* osd=(OsdOpenGL*)osd->getInstance();
1876 osd->BeginPainting(); // get OpenGl context
1877 moco_shader->deinit();
1891 VPE_FrameBuf *VideoVPEOGL::getFrameBuf(unsigned int *size)
1893 VPE_FrameBuf* current=NULL;
1894 vpe_framebuf_mutex.Lock();
1895 if (free_frame_bufs.size()>0) {
1896 current=free_frame_bufs.front();
1897 free_frame_bufs.pop_front();
1898 } else if (all_frame_bufs.size()<6) {
1899 current=(VPE_FrameBuf*)malloc(sizeof(VPE_FrameBuf));
1900 memset(current,0,sizeof(VPE_FrameBuf));
1902 Log::getInstance()->log("Video", Log::NOTICE, "Framebuffer underrun!");
1903 vpe_framebuf_mutex.Unlock();
1904 return NULL; // We do not have a frame buffer
1906 locked_libav_frame_buf.push_back(current);
1907 vpe_framebuf_mutex.Unlock();
1908 //check if we need reallocation
1909 for (int x=0;x<4;x++) {
1910 if (current->size[x]!=size[x]) {
1911 current->data[x]=av_realloc(current->data[x],size[x]);
1912 current->size[x]=size[x];
1915 framebuf_framenum++;
1916 current->pict_num=framebuf_framenum; //This is used for tracking reference frames through the conversion pipeline
1921 void VideoVPEOGL::lockFrameBufUpload(VPE_FrameBuf* buf)
1923 // first find frame_buf memory
1925 //Log::getInstance()->log("Video", Log::NOTICE, "lock buffer upload %x",buf);
1926 VPE_FrameBuf* current=buf;
1927 vpe_framebuf_mutex.Lock();
1928 if (current) locked_uploading_frame_buf.push_back(current); //locked
1929 vpe_framebuf_mutex.Unlock();
1934 void VideoVPEOGL::releaseFrameBufLibav(VPE_FrameBuf* buf)
1936 // first find frame_buf memory
1937 //Log::getInstance()->log("Video", Log::NOTICE, "release buffer libav %x",buf);
1938 VPE_FrameBuf* current=buf;
1939 vpe_framebuf_mutex.Lock();
1941 locked_libav_frame_buf.remove(current); //unlocked
1942 list<VPE_FrameBuf*>::iterator itty=locked_uploading_frame_buf.begin();
1944 while (itty!=locked_uploading_frame_buf.end()) {
1945 if (*itty==current) {
1952 free_frame_bufs.push_back(current);
1955 vpe_framebuf_mutex.Unlock();
1958 void VideoVPEOGL::releaseFrameBufUpload(VPE_FrameBuf* buf)
1960 // first find frame_buf memory
1961 VPE_FrameBuf* current=buf;
1962 //Log::getInstance()->log("Video", Log::NOTICE, "release buffer upload %x",buf);
1963 vpe_framebuf_mutex.Lock();
1965 locked_uploading_frame_buf.remove(current); //unlocked
1966 list<VPE_FrameBuf*>::iterator itty=locked_libav_frame_buf.begin();
1968 while (itty!=locked_libav_frame_buf.end()) {
1969 if (*itty==current) {
1976 free_frame_bufs.push_back(current);
1979 vpe_framebuf_mutex.Unlock();
1982 void VideoVPEOGL::add_dec_frame_upload_only(struct AVCodecContext *s,const AVFrame* data)
1984 dec_frame_libav_mutex.Lock();
1985 libavwidth=s->width;
1986 libavheight=s->height;
1987 libavpixfmt=s->pix_fmt;
1988 Log::getInstance()->log("Video", Log::DEBUG, "add_dec Frame info %d %d %d %d",libavwidth,libavheight,libavpixfmt,((VPE_FrameBuf*)data->base[0])->pict_num);
1990 dec_frame_libav_upload_only_pending.push_back((AVFrame*)data); // we are only uploading
1991 dec_frame_libav_mutex.Unlock();
2001 int VideoVPEOGL::stop()
2003 if (!initted) return 0;
2005 #ifdef VPE_OMX_SUPPORT
2006 //Check if libav mode
2007 if (decoding_backend==VPE_DECODER_OMX) DeAllocateCodecsOMX();
2014 // if (ioctl(fdVideo, AV_SET_VID_STOP, 0) != 0) return 0;
2018 int VideoVPEOGL::reset()
2020 if (!initted) return 0;
2022 // if (ioctl(fdVideo, AV_SET_VID_RESET, 0x11) != 0) return 0;
2026 int VideoVPEOGL::pause()
2028 if (!initted) return 0;
2030 // if (ioctl(fdVideo, AV_SET_VID_PAUSE, 0) != 0) return 0;
2034 int VideoVPEOGL::unPause() // FIXME get rid - same as play!!
2036 if (!initted) return 0;
2037 // if (ioctl(fdVideo, AV_SET_VID_PLAY, 0) != 0) return 0;
2041 int VideoVPEOGL::fastForward()
2043 if (!initted) return 0;
2045 // if (ioctl(fdVideo, AV_SET_VID_libavWD, 1) != 0) return 0;
2049 int VideoVPEOGL::unFastForward()
2051 if (!initted) return 0;
2053 // if (ioctl(fdVideo, AV_SET_VID_RESET, 0x11) != 0) return 0; // don't need this.
2055 //// if (ioctl(fdVideo, AV_SET_VID_PLAY, 0) != 0) return 0;
2059 int VideoVPEOGL::attachFrameBuffer()
2061 if (!initted) return 0;
2063 // if (ioctl(fdVideo, AV_SET_VID_FB, 0) != 0) return 0;
2067 int VideoVPEOGL::blank(void)
2069 // if (ioctl(fdVideo, AV_SET_VID_FB, 1) != 0) return 0;
2070 // if (ioctl(fdVideo, AV_SET_VID_FB, 0) != 0) return 0;
2074 ULLONG VideoVPEOGL::getCurrentTimestamp()
2076 /* sync_data_t timestamps;
2077 if (ioctl(fdVideo, AV_GET_VID_TIMESTAMPS, ×tamps) == 0)
2079 // FIXME are these the right way around?
2081 timestamps.stc = (timestamps.stc >> 31 ) | (timestamps.stc & 1);
2082 timestamps.pts = (timestamps.pts >> 31 ) | (timestamps.pts & 1);
2084 return timestamps.stc;
2093 ULONG VideoVPEOGL::timecodeToFrameNumber(ULLONG timecode)
2095 if (format == PAL) return (ULONG)(((double)timecode / (double)90000) * (double)25);
2096 else return (ULONG)(((double)timecode / (double)90000) * (double)30);
2100 int VideoVPEOGL::test()
2105 // return ioctl(fdVideo, AV_SET_VID_STC, &stc);
2112 int VideoVPEOGL::test2()
2118 void VideoVPEOGL::PrepareMediaSample(const MediaPacketList& mplist,UINT samplepos)
2120 mediapacket = mplist.front();
2123 UINT VideoVPEOGL::DeliverMediaSample(UCHAR* buffer, UINT *samplepos)
2125 DeliverMediaPacket(mediapacket, buffer, samplepos);
2126 if (*samplepos == mediapacket.length) {
2133 UINT VideoVPEOGL::DeliverMediaPacket(MediaPacket packet,
2134 const UCHAR* buffer,
2137 if (packet.type == MPTYPE_VIDEO_H264)
2145 switch (decoding_backend) {
2146 default: case 0: return 0; // no backend runnigng
2147 #ifdef VPE_OMX_SUPPORT
2148 case VPE_DECODER_OMX: return DeliverMediaPacketOMX(packet,buffer,samplepos);
2150 #ifdef VPE_LIBAV_SUPPORT
2151 case VPE_DECODER_libav: return DeliverMediaPacketlibav(packet,buffer,samplepos);
2156 #ifdef VPE_OMX_SUPPORT
2157 UINT VideoVPEOGL::DeliverMediaPacketOMX(MediaPacket packet,
2158 const UCHAR* buffer,
2163 //Later add fail back code for libav
2165 *samplepos+=packet.length;
2166 return packet.length;
2170 if (!omx_running) return 0; // if we are not runnig do not do this
2173 OMX_ERRORTYPE error;
2175 /* OMX_PARAM_PORTDEFINITIONTYPE port_image;
2176 memset(&port_image,0,sizeof(port_image));
2177 port_image.nSize=sizeof(port_image);
2178 port_image.nVersion.nVersion=OMX_VERSION;
2179 port_image.nPortIndex =omx_codec_output_port;
2180 error=OMX_GetParameter(omx_vid_dec,OMX_IndexParamPortDefinition, &port_image);
2181 if (error!= OMX_ErrorNone){
2182 Log::getInstance()->log("Video", Log::DEBUG, "OMX_GetParameter failed %x", error);
2184 Log::getInstance()->log("Video", Log::DEBUG, "Image port %d %d", port_image.format.video.nFrameWidth , port_image.format.video.nFrameHeight);*/
2186 /*First Check, if we have an audio sample*/
2190 return 0; //Not in iframe mode!
2194 if (packet.disconti) {
2196 if (cur_input_buf_omx) {
2197 OMX_ERRORTYPE error=OMX_EmptyThisBuffer(omx_vid_dec,cur_input_buf_omx);
2198 if (error!=OMX_ErrorNone){
2199 Log::getInstance()->log("Video", Log::DEBUG, "OMX_EmptyThisBuffer failed %x", error);
2201 cur_input_buf_omx=NULL;
2205 /*Inspect PES-Header */
2207 // OMX_STATETYPE temp_state;
2208 // OMX_GetState(omx_vid_dec,&temp_state);
2210 if (*samplepos==0) {//stripheader
2211 headerstrip=buffer[packet.pos_buffer+8]+9/*is this right*/;
2212 *samplepos+=headerstrip;
2213 if ( packet.synched ) {
2215 if (cur_input_buf_omx) {
2216 OMX_ERRORTYPE error=OMX_EmptyThisBuffer(omx_vid_dec,cur_input_buf_omx);
2217 if (error!=OMX_ErrorNone){
2218 Log::getInstance()->log("Video", Log::DEBUG, "OMX_EmptyThisBuffer failed %x", error);
2221 cur_input_buf_omx=NULL;//write out old data
2223 // reftime1=packet.presentation_time;
2224 // reftime2=reftime1+1;
2227 if (!firstsynched) {//
2228 *samplepos=packet.length;//if we have not processed at least one
2229 return packet.length;//synched packet ignore it!
2234 if (!cur_input_buf_omx) {
2235 input_bufs_omx_mutex.Lock();
2236 if (input_bufs_omx_free.size()==0) {
2237 input_bufs_omx_mutex.Unlock();
2238 Log::getInstance()->log("Video", Log::DEBUG, "Deliver MediaPacket no free sample");
2239 return 0; // we do not have a free media sample
2242 cur_input_buf_omx=input_bufs_omx_free.front();
2243 cur_input_buf_omx->nFilledLen=0;
2244 cur_input_buf_omx->nOffset=0;
2245 cur_input_buf_omx->nTimeStamp=0;
2246 input_bufs_omx_free.pop_front();
2247 input_bufs_omx_mutex.Unlock();
2253 if (cur_input_buf_omx->nFilledLen==0) {//will only be changed on first packet
2254 /*if (packet.disconti) {
2255 ms->SetDiscontinuity(TRUE);
2257 ms->SetDiscontinuity(FALSE);
2259 //if (packet.synched) {
2261 //lastreftimePTS=packet.pts;
2262 if (omx_first_frame) { // TODO time
2263 cur_input_buf_omx->nFlags=OMX_BUFFERFLAG_STARTTIME;
2264 omx_first_frame=false;
2270 cur_input_buf_omx->nFlags=OMX_BUFFERFLAG_TIME_UNKNOWN;
2273 // ms->SetSyncPoint(TRUE);
2277 unsigned int haveToCopy=packet.length-*samplepos;
2279 while (haveToCopy> (cur_input_buf_omx->nAllocLen-cur_input_buf_omx->nFilledLen)) {
2280 unsigned int cancopy=cur_input_buf_omx->nAllocLen-cur_input_buf_omx->nFilledLen;
2281 memcpy(cur_input_buf_omx->pBuffer+cur_input_buf_omx->nFilledLen,buffer+packet.pos_buffer+*samplepos,cancopy);
2282 haveToCopy-=cancopy;
2283 cur_input_buf_omx->nFilledLen+=cancopy;
2284 *samplepos+=cancopy;
2285 // push old buffer out
2287 OMX_ERRORTYPE error=OMX_EmptyThisBuffer(omx_vid_dec,cur_input_buf_omx);
2288 if (error!=OMX_ErrorNone){
2289 Log::getInstance()->log("Video", Log::DEBUG, "OMX_EmptyThisBuffer failed %x", error);
2292 input_bufs_omx_mutex.Lock();
2293 if (input_bufs_omx_free.size()==0) {
2294 input_bufs_omx_mutex.Unlock();
2295 //Log::getInstance()->log("Video", Log::DEBUG, "Deliver MediaPacket no free sample");
2296 return *samplepos; // we do not have a free media sample
2298 cur_input_buf_omx=input_bufs_omx_free.front();
2299 cur_input_buf_omx->nFilledLen=0;
2300 cur_input_buf_omx->nOffset=0;
2301 cur_input_buf_omx->nTimeStamp=0;
2302 input_bufs_omx_free.pop_front();
2303 input_bufs_omx_mutex.Unlock();
2305 cur_input_buf_omx->nFlags=OMX_BUFFERFLAG_TIME_UNKNOWN;
2308 memcpy(cur_input_buf_omx->pBuffer+cur_input_buf_omx->nFilledLen,
2309 buffer+packet.pos_buffer+*samplepos,haveToCopy);
2310 cur_input_buf_omx->nFilledLen+=haveToCopy;
2314 *samplepos+=haveToCopy;
2323 #ifdef VPE_LIBAV_SUPPORT
2324 int VideoVPEOGL::DecodePacketlibav()
2326 unsigned int haveToCopy=incom_packet_libav.size;
2327 if (incom_packet_libav.size==0) return 1; // we are already empty
2328 while (haveToCopy>0) {
2332 // Log::getInstance()->log("Video", Log::DEBUG, "Push data to decoder");
2334 #ifdef BENCHMARK_FPS
2335 int cur_time=getTimeMS();
2337 dec_bytes=avcodec_decode_video2(mpeg2codec_context_libav, dec_frame_libav_decoding,
2338 &frame_ready, &incom_packet_libav);
2339 #ifdef BENCHMARK_FPS
2340 time_in_decoder+=getTimeMS()-cur_time;
2341 if (frame_ready) num_frames++;
2342 if ((num_frames%100)==0) {
2343 float fps=1000./(float)(time_in_decoder);
2344 fps*=((float)num_frames);
2345 Log::getInstance()->log("Video", Log::NOTICE, "Current Pure Decoding FPS %g", fps);
2349 Log::getInstance()->log("Video", Log::DEBUG, "Decoding frame failed %x", dec_bytes);
2352 haveToCopy-=dec_bytes;
2354 // Log::getInstance()->log("Video", Log::DEBUG, "We have a frame push it to osd");
2356 lockFrameBufUpload((VPE_FrameBuf*)dec_frame_libav_decoding->base[0]); //lock for upload, so that ffmpeg does not reuse
2357 dec_frame_libav_mutex.Lock();
2358 libavwidth=mpeg2codec_context_libav->width;
2359 libavheight=mpeg2codec_context_libav->height;
2360 libavpixfmt=mpeg2codec_context_libav->pix_fmt;
2361 // Log::getInstance()->log("Video", Log::DEBUG, "Frame info %d %d %d",libavwidth,libavheight,libavpixfmt);
2364 dec_frame_libav_upload_and_view_pending.push_back(dec_frame_libav_decoding);
2365 dec_frame_libav_decoding=NULL;
2366 if (dec_frame_libav_free.size()>0) {
2367 dec_frame_libav_decoding=dec_frame_libav_free.front();
2368 dec_frame_libav_free.pop_front();
2369 dec_frame_libav_mutex.Unlock();
2371 libav_hastime=false;
2373 libav_hastime=false;
2374 dec_frame_libav_mutex.Unlock();
2384 incom_packet_libav.size=0;
2390 UINT VideoVPEOGL::DeliverMediaPacketlibav(MediaPacket packet,
2391 const UCHAR* buffer,
2394 //Later add fail back code for libav
2396 *samplepos+=packet.length;
2397 return packet.length;
2400 if (!libav_running) return 0; // if we are not runnig do not do this
2406 return 0; //Not in iframe mode!
2410 if (packet.disconti) {
2412 if (!DecodePacketlibav()) return 0;
2415 /*Inspect PES-Header */
2416 if (!dec_frame_libav_decoding) {
2417 dec_frame_libav_mutex.Lock();
2418 if (dec_frame_libav_free.size()>0) {
2419 dec_frame_libav_decoding=dec_frame_libav_free.front();
2420 dec_frame_libav_free.pop_front();
2421 dec_frame_libav_mutex.Unlock();
2423 Log::getInstance()->log("Video", Log::DEBUG, "We have no free buffers");
2424 dec_frame_libav_mutex.Unlock();
2432 if (*samplepos==0) {//stripheader
2433 headerstrip=buffer[packet.pos_buffer+8]+9/*is this right*/;
2434 *samplepos+=headerstrip;
2435 if ( packet.synched ) {
2437 if (!DecodePacketlibav()) return 0; // WriteOut old Data
2439 libav_time=packet.presentation_time;
2441 // reftime1=packet.presentation_time;
2442 // reftime2=reftime1+1;
2445 if (!firstsynched) {//
2446 *samplepos=packet.length;//if we have not processed at least one
2447 return packet.length;//synched packet ignore it!
2457 /*if (cur_input_buf_omx->nFilledLen==0) {//will only be changed on first packet
2458 /*if (packet.disconti) {
2459 ms->SetDiscontinuity(TRUE);
2461 ms->SetDiscontinuity(FALSE);
2463 //if (packet.synched) {
2465 //lastreftimePTS=packet.pts;
2466 if (omx_first_frame) { // TODO time
2467 cur_input_buf_omx->nFlags=OMX_BUFFERFLAG_STARTTIME;
2468 omx_first_frame=false;
2474 cur_input_buf_omx->nFlags=OMX_BUFFERFLAG_TIME_UNKNOWN;
2477 // ms->SetSyncPoint(TRUE);
2481 unsigned int haveToCopy=packet.length-*samplepos;
2483 if ((incom_packet_libav_size-incom_packet_libav.size)< haveToCopy) {
2484 // if the buffer is to small reallocate
2485 incom_packet_libav_size+=haveToCopy;
2486 incom_packet_libav.data=(uint8_t*)av_realloc(incom_packet_libav.data,incom_packet_libav_size+FF_INPUT_BUFFER_PADDING_SIZE);
2487 Log::getInstance()->log("Video", Log::DEBUG, "Reallocate avpacket buffer to %d", incom_packet_libav_size);
2489 memcpy(incom_packet_libav.data+incom_packet_libav.size,buffer+packet.pos_buffer+*samplepos,haveToCopy);
2490 incom_packet_libav.size+=haveToCopy;
2492 *samplepos+=haveToCopy;
2507 void VideoVPEOGL::ResetTimeOffsets()
2511 bool VideoVPEOGL::displayIFrame(const UCHAR* buffer, UINT length)
2513 //write(fdVideo, buffer, length);
2514 if (!iframemode) EnterIframePlayback();
2515 // WriteOutTS(buffer,length, h264?MPTYPE_VIDEO_H264 :MPTYPE_VIDEO_MPEG2 );
2521 int VideoVPEOGL::EnterIframePlayback()