1 // Copyright (c) 2017-2019 Intel Corporation
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in all
11 // copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 // SOFTWARE.
20 
21 #ifndef _MFX_ALLOC_WRAPPER_H_
22 #define _MFX_ALLOC_WRAPPER_H_
23 
24 #include <vector>
25 #include <memory> // unique_ptr
26 
27 #include "mfx_common.h"
28 #include "umc_memory_allocator.h"
29 #include "umc_frame_allocator.h"
30 #include "umc_frame_data.h"
31 
32 #include "mfxvideo++int.h"
33 
34 #define MFX_UMC_MAX_ALLOC_SIZE 128
35 
36 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
37 // mfx_UMC_MemAllocator - buffer allocator
38 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
39 class mfx_UMC_MemAllocator : public UMC::MemoryAllocator
40 {
41     DYNAMIC_CAST_DECL(mfx_UMC_MemAllocator, MemoryAllocator)
42 
43 public:
44     mfx_UMC_MemAllocator(void);
45     virtual ~mfx_UMC_MemAllocator(void);
46 
47     // Initiates object
48     virtual UMC::Status InitMem(UMC::MemoryAllocatorParams *pParams, VideoCORE* mfxCore);
49 
50     // Closes object and releases all allocated memory
51     virtual UMC::Status Close();
52 
53     // Allocates or reserves physical memory and return unique ID
54     // Sets lock counter to 0
55     virtual UMC::Status Alloc(UMC::MemID *pNewMemID, size_t Size, uint32_t Flags, uint32_t Align = 16);
56 
57     // Lock() provides pointer from ID. If data is not in memory (swapped)
58     // prepares (restores) it. Increases lock counter
59     virtual void *Lock(UMC::MemID MID);
60 
61     // Unlock() decreases lock counter
62     virtual UMC::Status Unlock(UMC::MemID MID);
63 
64     // Notifies that the data wont be used anymore. Memory can be free
65     virtual UMC::Status Free(UMC::MemID MID);
66 
67     // Immediately deallocates memory regardless of whether it is in use (locked) or no
68     virtual UMC::Status DeallocateMem(UMC::MemID MID);
69 
70 protected:
71     VideoCORE* m_pCore;
72 };
73 
74 
75 enum  {
76     mfx_UMC_ReallocAllowed = 1,
77 } mfx_UMC_FrameAllocator_Flags;
78 
79 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
80 // mfx_UMC_FrameAllocator
81 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
82 class mfx_UMC_FrameAllocator : public UMC::FrameAllocator
83 {
84     DYNAMIC_CAST_DECL(mfx_UMC_FrameAllocator, UMC::FrameAllocator)
85 
86 public:
87     mfx_UMC_FrameAllocator(void);
88     virtual ~mfx_UMC_FrameAllocator(void);
89 
90     // Initiates object
91     virtual UMC::Status InitMfx(UMC::FrameAllocatorParams *pParams,
92                                 VideoCORE* mfxCore,
93                                 const mfxVideoParam *params,
94                                 const mfxFrameAllocRequest *request,
95                                 mfxFrameAllocResponse *response,
96                                 bool isUseExternalFrames,
97                                 bool isSWplatform);
98 
99     // Closes object and releases all allocated memory
100     virtual UMC::Status Close();
101 
102     // Allocates or reserves physical memory and returns unique ID
103     // Sets lock counter to 0
104     virtual UMC::Status Alloc(UMC::FrameMemID *pNewMemID, const UMC::VideoDataInfo * info, uint32_t flags);
105 
106     virtual UMC::Status GetFrameHandle(UMC::FrameMemID memId, void * handle);
107 
108     // Lock() provides pointer from ID. If data is not in memory (swapped)
109     // prepares (restores) it. Increases lock counter
110     virtual const UMC::FrameData* Lock(UMC::FrameMemID mid);
111 
112     // Unlock() decreases lock counter
113     virtual UMC::Status Unlock(UMC::FrameMemID mid);
114 
115     // Notifies that the data won't be used anymore. Memory can be freed.
116     virtual UMC::Status IncreaseReference(UMC::FrameMemID mid);
117 
118     // Notifies that the data won't be used anymore. Memory can be freed.
119     virtual UMC::Status DecreaseReference(UMC::FrameMemID mid);
120 
121     virtual UMC::Status Reset();
122 
123     virtual mfxStatus SetCurrentMFXSurface(mfxFrameSurface1 *srf, bool isOpaq);
124 
125     virtual mfxFrameSurface1*  GetSurface(UMC::FrameMemID index, mfxFrameSurface1 *surface_work, const mfxVideoParam * videoPar);
126     virtual mfxStatus          PrepareToOutput(mfxFrameSurface1 *surface_work, UMC::FrameMemID index, const mfxVideoParam * videoPar, bool isOpaq);
127     mfxI32 FindSurface(mfxFrameSurface1 *surf, bool isOpaq);
128     mfxI32 FindFreeSurface();
129 
130     void SetSfcPostProcessingFlag(bool flagToSet);
131 
132     void SetExternalFramesResponse(mfxFrameAllocResponse *response);
133     mfxFrameSurface1 * GetInternalSurface(UMC::FrameMemID index);
134     mfxFrameSurface1 * GetSurfaceByIndex(UMC::FrameMemID index);
135 
ConvertMemId(UMC::FrameMemID index)136     mfxMemId ConvertMemId(UMC::FrameMemID index)
137     {
138         return m_frameDataInternal.GetSurface(index).Data.MemId;
139     };
140 
141 protected:
142     struct  surf_descr
143     {
surf_descrsurf_descr144         surf_descr(mfxFrameSurface1* FrameSurface, bool isUsed):FrameSurface(FrameSurface),
145                                                                 isUsed(isUsed)
146         {
147         };
surf_descrsurf_descr148         surf_descr():FrameSurface(0),
149                      isUsed(false)
150         {
151         };
152         mfxFrameSurface1* FrameSurface;
153         bool              isUsed;
154     };
155 
156     virtual UMC::Status Free(UMC::FrameMemID mid);
157 
158     virtual mfxI32 AddSurface(mfxFrameSurface1 *surface);
159 
160     class InternalFrameData
161     {
162         class FrameRefInfo
163         {
164         public:
165             FrameRefInfo();
166             void Reset();
167 
168             mfxU32 m_referenceCounter;
169         };
170 
171         typedef std::pair<mfxFrameSurface1, UMC::FrameData> FrameInfo;
172 
173     public:
174 
175         mfxFrameSurface1 & GetSurface(mfxU32 index);
176         UMC::FrameData   & GetFrameData(mfxU32 index);
177         void ResetFrameData(mfxU32 index);
178         mfxU32 IncreaseRef(mfxU32 index);
179         mfxU32 DecreaseRef(mfxU32 index);
180 
181         bool IsValidMID(mfxU32 index) const;
182 
183         void AddNewFrame(mfx_UMC_FrameAllocator * alloc, mfxFrameSurface1 *surface, UMC::VideoDataInfo * info);
184 
185         mfxU32 GetSize() const;
186 
187         void Close();
188         void Reset();
189 
190         void Resize(mfxU32 size);
191 
192     private:
193         std::vector<FrameInfo>  m_frameData;
194         std::vector<FrameRefInfo>  m_frameDataRefs;
195     };
196 
197     InternalFrameData m_frameDataInternal;
198 
199     std::vector<surf_descr> m_extSurfaces;
200 
201     mfxI32        m_curIndex;
202 
203     bool m_IsUseExternalFrames;
204     bool m_sfcVideoPostProcessing;
205 
206     mfxFrameInfo m_surface_info;  // for copying
207 
208     UMC::VideoDataInfo m_info;
209 
210     VideoCORE* m_pCore;
211 
212     mfxFrameAllocResponse *m_externalFramesResponse;
213 
214     bool       m_isSWDecode;
215     mfxU16     m_IOPattern;
216 };
217 
218 
219 class mfx_UMC_FrameAllocator_D3D : public mfx_UMC_FrameAllocator
220 {
221 public:
222     virtual mfxStatus PrepareToOutput(mfxFrameSurface1 *surface_work, UMC::FrameMemID index, const mfxVideoParam * videoPar, bool isOpaq);
223 };
224 
225 #if defined (MFX_ENABLE_MJPEG_VIDEO_DECODE)
226 class VideoVppJpegD3D9;
227 
228 class mfx_UMC_FrameAllocator_D3D_Converter : public mfx_UMC_FrameAllocator_D3D
229 {
230     std::unique_ptr<VideoVppJpegD3D9> m_pCc;
231 
232     mfxStatus InitVideoVppJpegD3D9(const mfxVideoParam *params);
233     mfxStatus FindSurfaceByMemId(const UMC::FrameData* in, bool isOpaq, const mfxHDLPair &hdlPair,
234                                  // output param
235                                  mfxFrameSurface1 &surface);
236 public:
237     virtual UMC::Status InitMfx(UMC::FrameAllocatorParams *pParams,
238                                 VideoCORE* mfxCore,
239                                 const mfxVideoParam *params,
240                                 const mfxFrameAllocRequest *request,
241                                 mfxFrameAllocResponse *response,
242                                 bool isUseExternalFrames,
243                                 bool isSWplatform) override;
244 
245     // suppose that Close() calls Reset(), so override only Reset()
246     virtual UMC::Status Reset() override;
247 
248     typedef struct
249     {
250         int32_t colorFormat;
251         size_t UOffset;
252         size_t VOffset;
253     } JPEG_Info;
254 
255     void SetJPEGInfo(JPEG_Info * jpegInfo);
256 
257     mfxStatus StartPreparingToOutput(mfxFrameSurface1 *surface_work, UMC::FrameData* in, const mfxVideoParam * par, mfxU16 *taskId, bool isOpaq);
258     mfxStatus CheckPreparingToOutput(mfxFrameSurface1 *surface_work, UMC::FrameData* in, const mfxVideoParam * par, mfxU16 taskId);
259 
260 protected:
261 
262     mfxStatus ConvertToNV12(UMC::FrameMemID index, mfxFrameSurface1 *dst);
263     JPEG_Info  m_jpegInfo;
264 };
265 
266 #endif // #if defined (MFX_ENABLE_MJPEG_VIDEO_DECODE) && defined (MFX_VA_WIN)
267 
268 #endif //_MFX_ALLOC_WRAPPER_H_
269