Merge pull request #4539 from Matricom/amcodec
[vuplus_xbmc] / xbmc / cores / omxplayer / OMXImage.h
1 #pragma once
2 /*
3  *      Copyright (C) 2010-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, see
18  *  <http://www.gnu.org/licenses/>.
19  *
20  */
21
22 #if defined(HAVE_OMXLIB)
23
24 #include "OMXCore.h"
25
26 #include <IL/OMX_Video.h>
27
28 #include "OMXClock.h"
29 #if defined(STANDALONE)
30 #define XB_FMT_A8R8G8B8 1
31 #include "File.h"
32 #else
33 #include "filesystem/File.h"
34 #include "guilib/XBTF.h"
35 #endif
36
37 #include "system_gl.h"
38 #include <EGL/egl.h>
39 #include <EGL/eglext.h>
40 #include "threads/Thread.h"
41
42 using namespace XFILE;
43 using namespace std;
44
45 class COMXImageFile;
46
47 class COMXImage : public CThread
48 {
49   struct callbackinfo {
50     CEvent sync;
51     bool (*callback)(EGLDisplay egl_display, EGLContext egl_context, void *cookie);
52     void *cookie;
53     bool result;
54   };
55 protected:
56   virtual void OnStartup();
57   virtual void OnExit();
58   virtual void Process();
59 public:
60   struct textureinfo {
61     int width, height;
62     GLuint texture;
63     EGLImageKHR egl_image;
64     void *parent;
65     const char *filename;
66   };
67   COMXImage();
68   virtual ~COMXImage();
69   void Initialize();
70   void Deinitialize();
71   static COMXImageFile *LoadJpeg(const CStdString& texturePath);
72   static void CloseJpeg(COMXImageFile *file);
73
74   static bool DecodeJpeg(COMXImageFile *file, unsigned int maxWidth, unsigned int maxHeight, unsigned int stride, void *pixels);
75   static bool CreateThumbnailFromSurface(unsigned char* buffer, unsigned int width, unsigned int height,
76       unsigned int format, unsigned int pitch, const CStdString& destFile);
77   static bool ClampLimits(unsigned int &width, unsigned int &height, unsigned int m_width, unsigned int m_height, bool transposed = false);
78   static bool CreateThumb(const CStdString& srcFile, unsigned int width, unsigned int height, std::string &additional_info, const CStdString& destFile);
79   bool SendMessage(bool (*callback)(EGLDisplay egl_display, EGLContext egl_context, void *cookie), void *cookie);
80   bool DecodeJpegToTexture(COMXImageFile *file, unsigned int width, unsigned int height, void **userdata);
81   void DestroyTexture(void *userdata);
82   void GetTexture(void *userdata, GLuint *texture);
83   bool AllocTextureInternal(EGLDisplay egl_display, EGLContext egl_context, struct textureinfo *tex);
84   bool DestroyTextureInternal(EGLDisplay egl_display, EGLContext egl_context, struct textureinfo *tex);
85 private:
86   EGLContext m_egl_context;
87
88   void CreateContext();
89   EGLContext GetEGLContext();
90   CCriticalSection               m_texqueue_lock;
91   XbmcThreads::ConditionVariable m_texqueue_cond;
92   std::queue <struct callbackinfo *> m_texqueue;
93 };
94
95 class COMXImageFile
96 {
97 public:
98   COMXImageFile();
99   virtual ~COMXImageFile();
100   bool ReadFile(const CStdString& inputFile);
101   int  GetOrientation() { return m_orientation; };
102   unsigned int GetWidth()  { return m_width; };
103   unsigned int GetHeight() { return m_height; };
104   unsigned long GetImageSize() { return m_image_size; };
105   const uint8_t *GetImageBuffer() { return (const uint8_t *)m_image_buffer; };
106   const char *GetFilename() { return m_filename; };
107 protected:
108   OMX_IMAGE_CODINGTYPE GetCodingType(unsigned int &width, unsigned int &height);
109   uint8_t           *m_image_buffer;
110   unsigned long     m_image_size;
111   unsigned int      m_width;
112   unsigned int      m_height;
113   int               m_orientation;
114   const char *      m_filename;
115 };
116
117 class COMXImageDec
118 {
119 public:
120   COMXImageDec();
121   virtual ~COMXImageDec();
122
123   // Required overrides
124   void Close();
125   bool Decode(const uint8_t *data, unsigned size, unsigned int width, unsigned int height, unsigned stride, void *pixels);
126   unsigned int GetDecodedWidth() { return (unsigned int)m_decoded_format.format.image.nFrameWidth; };
127   unsigned int GetDecodedHeight() { return (unsigned int)m_decoded_format.format.image.nFrameHeight; };
128   unsigned int GetDecodedStride() { return (unsigned int)m_decoded_format.format.image.nStride; };
129 protected:
130   bool HandlePortSettingChange(unsigned int resize_width, unsigned int resize_height, unsigned int resize_stride);
131   // Components
132   COMXCoreComponent             m_omx_decoder;
133   COMXCoreComponent             m_omx_resize;
134   COMXCoreTunel                 m_omx_tunnel_decode;
135   OMX_BUFFERHEADERTYPE          *m_decoded_buffer;
136   OMX_PARAM_PORTDEFINITIONTYPE  m_decoded_format;
137   CCriticalSection              m_OMXSection;
138 };
139
140 class COMXImageEnc
141 {
142 public:
143   COMXImageEnc();
144   virtual ~COMXImageEnc();
145
146   // Required overrides
147   bool CreateThumbnailFromSurface(unsigned char* buffer, unsigned int width, unsigned int height,
148       unsigned int format, unsigned int pitch, const CStdString& destFile);
149 protected:
150   bool Encode(unsigned char *buffer, int size, unsigned int width, unsigned int height, unsigned int pitch);
151   // Components
152   COMXCoreComponent             m_omx_encoder;
153   OMX_BUFFERHEADERTYPE          *m_encoded_buffer;
154   OMX_PARAM_PORTDEFINITIONTYPE  m_encoded_format;
155   CCriticalSection              m_OMXSection;
156 };
157
158 class COMXImageReEnc
159 {
160 public:
161   COMXImageReEnc();
162   virtual ~COMXImageReEnc();
163
164   // Required overrides
165   void Close();
166   bool ReEncode(COMXImageFile &srcFile, unsigned int width, unsigned int height, void * &pDestBuffer, unsigned int &nDestSize);
167 protected:
168   bool HandlePortSettingChange(unsigned int resize_width, unsigned int resize_height, int orientation, bool port_settings_changed);
169   // Components
170   COMXCoreComponent             m_omx_decoder;
171   COMXCoreComponent             m_omx_resize;
172   COMXCoreComponent             m_omx_encoder;
173   COMXCoreTunel                 m_omx_tunnel_decode;
174   COMXCoreTunel                 m_omx_tunnel_resize;
175   OMX_BUFFERHEADERTYPE          *m_encoded_buffer;
176   CCriticalSection              m_OMXSection;
177   void                          *m_pDestBuffer;
178   unsigned int                  m_nDestAllocSize;
179 };
180
181 class COMXTexture
182 {
183 public:
184   COMXTexture();
185   virtual ~COMXTexture();
186
187   // Required overrides
188   void Close(void);
189   bool Decode(const uint8_t *data, unsigned size, unsigned int width, unsigned int height, void *egl_image);
190 protected:
191   bool HandlePortSettingChange(unsigned int resize_width, unsigned int resize_height, void *egl_image, bool port_settings_changed);
192
193   // Components
194   COMXCoreComponent m_omx_decoder;
195   COMXCoreComponent m_omx_resize;
196   COMXCoreComponent m_omx_egl_render;
197
198   COMXCoreTunel     m_omx_tunnel_decode;
199   COMXCoreTunel     m_omx_tunnel_egl;
200
201   OMX_BUFFERHEADERTYPE *m_egl_buffer;
202   CCriticalSection              m_OMXSection;
203 };
204
205 extern COMXImage g_OMXImage;
206 #endif