Merge pull request #5101 from FernetMenta/ffmpeg-threads
[vuplus_xbmc] / xbmc / cores / dvdplayer / DVDCodecs / Video / VAAPI.h
1 /*
2  *      Copyright (C) 2005-2013 Team XBMC
3  *      http://xbmc.org
4  *
5  *  This library is free software; you can redistribute it and/or
6  *  modify it under the terms of the GNU Lesser General Public
7  *  License as published by the Free Software Foundation; either
8  *  version 2.1 of the License, or (at your option) any later version.
9  *
10  *  This library is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  *  Lesser General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with XBMC; see the file COPYING.  If not, see
17  *  <http://www.gnu.org/licenses/>.
18  *
19  */
20 #pragma once
21
22 #include "system_gl.h"
23
24 #include "DllAvCodec.h"
25 #include "DVDVideoCodecFFmpeg.h"
26 #include <libavcodec/vaapi.h>
27 #include <va/va.h>
28 #include <va/va_x11.h>
29 #include <va/va_glx.h>
30 #include <list>
31 #include <boost/shared_ptr.hpp>
32
33
34 namespace VAAPI {
35
36 typedef boost::shared_ptr<VASurfaceID const> VASurfacePtr;
37
38 struct CDisplay
39   : CCriticalSection
40 {
41   CDisplay(VADisplay display, bool deinterlace)
42     : m_display(display)
43     , m_lost(false)
44     , m_deinterlace(deinterlace)
45     , m_support_4k(true)
46   {}
47  ~CDisplay();
48
49   VADisplay get() { return m_display; }
50   bool      lost()          { return m_lost; }
51   void      lost(bool lost) { m_lost = lost; }
52   bool      support_deinterlace() { return m_deinterlace; };
53   bool      support_4k() { return m_support_4k; };
54   void      support_4k(bool support_4k) { m_support_4k = support_4k; };
55 private:
56   VADisplay m_display;
57   bool      m_lost;
58   bool      m_deinterlace;
59   bool      m_support_4k;
60 };
61
62 typedef boost::shared_ptr<CDisplay> CDisplayPtr;
63
64 struct CSurface
65 {
66   CSurface(VASurfaceID id, CDisplayPtr& display)
67    : m_id(id)
68    , m_display(display)
69   {}
70
71  ~CSurface();
72
73   VASurfaceID m_id;
74   CDisplayPtr m_display;
75 };
76
77 typedef boost::shared_ptr<CSurface> CSurfacePtr;
78
79 struct CSurfaceGL
80 {
81   CSurfaceGL(void* id, CDisplayPtr& display)
82     : m_id(id)
83     , m_display(display)
84   {}
85  ~CSurfaceGL();
86  
87   void*       m_id;
88   CDisplayPtr m_display;
89 };
90
91 typedef boost::shared_ptr<CSurfaceGL> CSurfaceGLPtr;
92
93 // silly type to avoid includes
94 struct CHolder
95 {
96   CDisplayPtr   display;
97   CSurfacePtr   surface;
98   CSurfaceGLPtr surfglx;
99
100   CHolder()
101   {}
102 };
103
104 class CDecoder
105   : public CDVDVideoCodecFFmpeg::IHardwareDecoder
106 {
107   bool EnsureContext(AVCodecContext *avctx);
108   bool EnsureSurfaces(AVCodecContext *avctx, unsigned n_surfaces_count);
109 public:
110   CDecoder();
111  ~CDecoder();
112   virtual bool Open      (AVCodecContext* avctx, const enum PixelFormat, unsigned int surfaces = 0);
113   virtual int  Decode    (AVCodecContext* avctx, AVFrame* frame);
114   virtual bool GetPicture(AVCodecContext* avctx, AVFrame* frame, DVDVideoPicture* picture);
115   virtual int  Check     (AVCodecContext* avctx);
116   virtual void Close();
117   virtual const std::string Name() { return "vaapi"; }
118   virtual CCriticalSection* Section() { if(m_display) return m_display.get(); else return NULL; }
119   virtual unsigned GetAllowedReferences();
120
121   int   GetBuffer(AVCodecContext *avctx, AVFrame *pic);
122   void  RelBuffer(AVCodecContext *avctx, AVFrame *pic);
123
124   VADisplay    GetDisplay() { return m_display->get(); }
125 protected:
126   
127   static const unsigned  m_surfaces_max = 32;
128   unsigned               m_surfaces_count;
129   VASurfaceID            m_surfaces[m_surfaces_max];
130   unsigned               m_renderbuffers_count;
131
132   int                    m_refs;
133   std::list<CSurfacePtr> m_surfaces_used;
134   std::list<CSurfacePtr> m_surfaces_free;
135
136   CDisplayPtr    m_display;
137   VAConfigID     m_config;
138   VAContextID    m_context;
139
140   vaapi_context *m_hwaccel;
141
142   CHolder        m_holder; // silly struct to pass data to renderer
143 };
144
145 }