930d445dd392ad8b5b45301755c0cacb12b127d9
[vuplus_xbmc] / xbmc / cores / dvdplayer / DVDCodecs / Video / DVDVideoCodecAndroidMediaCodec.h
1 #pragma once
2 /*
3  *      Copyright (C) 2013 Team XBMC
4  *      http://xbmc.org
5  *
6  *  This Program 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, or (at your option)
9  *  any later version.
10  *
11  *  This Program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with XBMC; see the file COPYING.  If not, write to
18  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
19  *  http://www.gnu.org/copyleft/gpl.html
20  *
21  */
22
23 #include <queue>
24 #include <vector>
25 #include <boost/shared_ptr.hpp>
26
27 #include "DVDVideoCodec.h"
28 #include "DVDStreamInfo.h"
29 #include "threads/Thread.h"
30 #include "threads/SingleLock.h"
31
32 class CJNISurface;
33 class CJNISurfaceTexture;
34 class CJNIMediaCodec;
35 class CDVDMediaCodecOnFrameAvailable;
36 class CJNIByteBuffer;
37 class CBitstreamConverter;
38
39 typedef struct amc_demux {
40   uint8_t  *pData;
41   int       iSize;
42   double    dts;
43   double    pts;
44 } amc_demux;
45
46 class CDVDMediaCodecInfo
47 {
48 public:
49   CDVDMediaCodecInfo( int index,
50                       unsigned int texture,
51                       boost::shared_ptr<CJNIMediaCodec> &codec,
52                       boost::shared_ptr<CJNISurfaceTexture> &surfacetexture,
53                       boost::shared_ptr<CDVDMediaCodecOnFrameAvailable> &frameready);
54
55   // reference counting
56   CDVDMediaCodecInfo* Retain();
57   long                Release();
58
59   // meat and potatos
60   void                Validate(bool state);
61   // MediaCodec related
62   void                ReleaseOutputBuffer(bool render);
63   // SurfaceTexture released
64   int                 GetIndex() const;
65   int                 GetTextureID() const;
66   void                GetTransformMatrix(float *textureMatrix);
67   void                UpdateTexImage();
68
69 private:
70   // private because we are reference counted
71   virtual            ~CDVDMediaCodecInfo();
72
73   long                m_refs;
74   bool                m_valid;
75   int                 m_index;
76   unsigned int        m_texture;
77   int64_t             m_timestamp;
78   CCriticalSection    m_section;
79   // shared_ptr bits, shared between
80   // CDVDVideoCodecAndroidMediaCodec and LinuxRenderGLES.
81   boost::shared_ptr<CJNIMediaCodec> m_codec;
82   boost::shared_ptr<CJNISurfaceTexture> m_surfacetexture;
83   boost::shared_ptr<CDVDMediaCodecOnFrameAvailable> m_frameready;
84 };
85
86 class CDVDVideoCodecAndroidMediaCodec : public CDVDVideoCodec
87 {
88 public:
89   CDVDVideoCodecAndroidMediaCodec();
90   virtual ~CDVDVideoCodecAndroidMediaCodec();
91
92   // required overrides
93   virtual bool    Open(CDVDStreamInfo &hints, CDVDCodecOptions &options);
94   virtual void    Dispose();
95   virtual int     Decode(uint8_t *pData, int iSize, double dts, double pts);
96   virtual void    Reset();
97   virtual bool    GetPicture(DVDVideoPicture *pDvdVideoPicture);
98   virtual bool    ClearPicture(DVDVideoPicture* pDvdVideoPicture);
99   virtual void    SetDropState(bool bDrop);
100   virtual int     GetDataSize(void);
101   virtual double  GetTimeSize(void);
102   virtual const char* GetName(void) { return m_formatname; }
103   virtual unsigned GetAllowedReferences();
104
105 protected:
106   void            FlushInternal(void);
107   void            ConfigureMediaCodec(void);
108   int             GetOutputPicture(void);
109   void            OutputFormatChanged(void);
110
111   // surface handling functions
112   static void     CallbackInitSurfaceTexture(void*);
113   void            InitSurfaceTexture(void);
114   void            ReleaseSurfaceTexture(void);
115
116   CDVDStreamInfo  m_hints;
117   std::string     m_mime;
118   std::string     m_codecname;
119   const char     *m_formatname;
120   bool            m_opened;
121   bool            m_drop;
122
123   CJNISurface    *m_surface;
124   unsigned int    m_textureId;
125   // we need these as shared_ptr because CDVDVideoCodecAndroidMediaCodec
126   // will get deleted before CLinuxRendererGLES is shut down and
127   // CLinuxRendererGLES refs them via CDVDMediaCodecInfo.
128   boost::shared_ptr<CJNIMediaCodec> m_codec;
129   boost::shared_ptr<CJNISurfaceTexture> m_surfaceTexture;
130   boost::shared_ptr<CDVDMediaCodecOnFrameAvailable> m_frameAvailable;
131
132   std::queue<amc_demux> m_demux;
133   std::vector<CJNIByteBuffer> m_input;
134   std::vector<CJNIByteBuffer> m_output;
135   std::vector<CDVDMediaCodecInfo*> m_inflight;
136
137   CBitstreamConverter *m_bitstream;
138   DVDVideoPicture m_videobuffer;
139
140   bool            m_render_sw;
141   int             m_src_offset[4];
142   int             m_src_stride[4];
143 };