changed: Add logic to properly handle subtitles for stacked files
[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 enum TextureAction {TEXTURE_ALLOC, TEXTURE_DELETE };
50
51 struct textureinfo {
52   TextureAction action;
53   int width, height;
54   GLuint texture;
55   EGLImageKHR egl_image;
56   void *parent;
57   const char *filename;
58   CEvent sync;
59 };
60
61 protected:
62   virtual void OnStartup();
63   virtual void OnExit();
64   virtual void Process();
65 public:
66   COMXImage();
67   virtual ~COMXImage();
68   void Initialize();
69   void Deinitialize();
70   static COMXImageFile *LoadJpeg(const CStdString& texturePath);
71   static void CloseJpeg(COMXImageFile *file);
72
73   static bool DecodeJpeg(COMXImageFile *file, unsigned int maxWidth, unsigned int maxHeight, unsigned int stride, void *pixels);
74   static bool CreateThumbnailFromSurface(unsigned char* buffer, unsigned int width, unsigned int height,
75       unsigned int format, unsigned int pitch, const CStdString& destFile);
76   static bool ClampLimits(unsigned int &width, unsigned int &height, unsigned int m_width, unsigned int m_height, bool transposed = false);
77   static bool CreateThumb(const CStdString& srcFile, unsigned int width, unsigned int height, std::string &additional_info, const CStdString& destFile);
78   bool DecodeJpegToTexture(COMXImageFile *file, unsigned int width, unsigned int height, void **userdata);
79   void DestroyTexture(void *userdata);
80   void GetTexture(void *userdata, GLuint *texture);
81 private:
82   EGLDisplay m_egl_display;
83   EGLContext m_egl_context;
84
85   void CreateContext();
86   CCriticalSection               m_texqueue_lock;
87   XbmcThreads::ConditionVariable m_texqueue_cond;
88   std::queue <struct textureinfo *> m_texqueue;
89   void AllocTextureInternal(struct textureinfo *tex);
90   void DestroyTextureInternal(struct textureinfo *tex);
91 };
92
93 class COMXImageFile
94 {
95 public:
96   COMXImageFile();
97   virtual ~COMXImageFile();
98   bool ReadFile(const CStdString& inputFile);
99   int  GetOrientation() { return m_orientation; };
100   unsigned int GetWidth()  { return m_width; };
101   unsigned int GetHeight() { return m_height; };
102   unsigned long GetImageSize() { return m_image_size; };
103   const uint8_t *GetImageBuffer() { return (const uint8_t *)m_image_buffer; };
104   const char *GetFilename() { return m_filename; };
105 protected:
106   OMX_IMAGE_CODINGTYPE GetCodingType(unsigned int &width, unsigned int &height);
107   uint8_t           *m_image_buffer;
108   unsigned long     m_image_size;
109   unsigned int      m_width;
110   unsigned int      m_height;
111   int               m_orientation;
112   const char *      m_filename;
113 };
114
115 class COMXImageDec
116 {
117 public:
118   COMXImageDec();
119   virtual ~COMXImageDec();
120
121   // Required overrides
122   void Close();
123   bool Decode(const uint8_t *data, unsigned size, unsigned int width, unsigned int height, unsigned stride, void *pixels);
124   unsigned int GetDecodedWidth() { return (unsigned int)m_decoded_format.format.image.nFrameWidth; };
125   unsigned int GetDecodedHeight() { return (unsigned int)m_decoded_format.format.image.nFrameHeight; };
126   unsigned int GetDecodedStride() { return (unsigned int)m_decoded_format.format.image.nStride; };
127 protected:
128   bool HandlePortSettingChange(unsigned int resize_width, unsigned int resize_height);
129   // Components
130   COMXCoreComponent             m_omx_decoder;
131   COMXCoreComponent             m_omx_resize;
132   COMXCoreTunel                 m_omx_tunnel_decode;
133   OMX_BUFFERHEADERTYPE          *m_decoded_buffer;
134   OMX_PARAM_PORTDEFINITIONTYPE  m_decoded_format;
135   CCriticalSection              m_OMXSection;
136 };
137
138 class COMXImageEnc
139 {
140 public:
141   COMXImageEnc();
142   virtual ~COMXImageEnc();
143
144   // Required overrides
145   bool CreateThumbnailFromSurface(unsigned char* buffer, unsigned int width, unsigned int height,
146       unsigned int format, unsigned int pitch, const CStdString& destFile);
147 protected:
148   bool Encode(unsigned char *buffer, int size, unsigned int width, unsigned int height, unsigned int pitch);
149   // Components
150   COMXCoreComponent             m_omx_encoder;
151   OMX_BUFFERHEADERTYPE          *m_encoded_buffer;
152   OMX_PARAM_PORTDEFINITIONTYPE  m_encoded_format;
153   CCriticalSection              m_OMXSection;
154 };
155
156 class COMXImageReEnc
157 {
158 public:
159   COMXImageReEnc();
160   virtual ~COMXImageReEnc();
161
162   // Required overrides
163   void Close();
164   bool ReEncode(COMXImageFile &srcFile, unsigned int width, unsigned int height, void * &pDestBuffer, unsigned int &nDestSize);
165 protected:
166   bool HandlePortSettingChange(unsigned int resize_width, unsigned int resize_height, bool port_settings_changed);
167   // Components
168   COMXCoreComponent             m_omx_decoder;
169   COMXCoreComponent             m_omx_resize;
170   COMXCoreComponent             m_omx_encoder;
171   COMXCoreTunel                 m_omx_tunnel_decode;
172   COMXCoreTunel                 m_omx_tunnel_resize;
173   OMX_BUFFERHEADERTYPE          *m_encoded_buffer;
174   CCriticalSection              m_OMXSection;
175   void                          *m_pDestBuffer;
176   unsigned int                  m_nDestAllocSize;
177 };
178
179 class COMXTexture
180 {
181 public:
182   COMXTexture();
183   virtual ~COMXTexture();
184
185   // Required overrides
186   void Close(void);
187   bool Decode(const uint8_t *data, unsigned size, unsigned int width, unsigned int height, void *egl_image, void *egl_display);
188 protected:
189   bool HandlePortSettingChange(unsigned int resize_width, unsigned int resize_height, void *egl_image, void *egl_display, bool port_settings_changed);
190
191   // Components
192   COMXCoreComponent m_omx_decoder;
193   COMXCoreComponent m_omx_resize;
194   COMXCoreComponent m_omx_egl_render;
195
196   COMXCoreTunel     m_omx_tunnel_decode;
197   COMXCoreTunel     m_omx_tunnel_egl;
198
199   OMX_BUFFERHEADERTYPE *m_egl_buffer;
200   CCriticalSection              m_OMXSection;
201 };
202
203 extern COMXImage g_OMXImage;
204 #endif