1 /*
2  * Copyright (C) 2012 Intel Corporation.  All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 
18 
19 #ifndef ISV_OMXCOMPONENT_H_
20 
21 #define ISV_OMXCOMPONENT_H_
22 
23 #include <utils/Mutex.h>
24 #include <utils/Vector.h>
25 #include <utils/RefBase.h>
26 #include "isv_omxcore.h"
27 #include "isv_processor.h"
28 #include "isv_profile.h"
29 #include "isv_worker.h"
30 #include "isv_bufmanager.h"
31 
32 #define ISV_COMPONENT_DEBUG 0
33 
34 #ifdef TARGET_VPP_USE_GEN
35 #define MIN_INPUT_NUM           (3)
36 #define MIN_OUTPUT_NUM          (3)
37 #else
38 #define MIN_INPUT_NUM           (4)    // forward reference frame number is 3 for merrifield/moorefield
39 #define MIN_OUTPUT_NUM          (10)   // 2.5FRC need hold 2 + 3 + 2 + 3= 10 buffers, without FRC we set to 6
40 #endif
41 #define MIN_ISV_BUFFER_NUM      ((MIN_OUTPUT_NUM) + (MIN_INPUT_NUM))
42 #define UNDEQUEUED_NUM          (4)   // display system hold 4 buffers
43 
44 using namespace android;
45 class ISVComponent;
46 
47 class ISVProcThreadObserver: public ISVProcessorObserver
48 {
49 public:
50     ISVProcThreadObserver(OMX_COMPONENTTYPE *pBaseComponent, OMX_COMPONENTTYPE *pComponent, OMX_CALLBACKTYPE *pCallBacks, sp<ISVBufferManager> bufferManager);
51     ~ISVProcThreadObserver();
52 
53     virtual OMX_ERRORTYPE releaseBuffer(PORT_INDEX index, OMX_BUFFERHEADERTYPE* pBuffer, bool flush);
54     virtual OMX_ERRORTYPE reportOutputCrop();
55 private:
56     OMX_COMPONENTTYPE *mBaseComponent;
57     OMX_COMPONENTTYPE *mComponent;
58     OMX_CALLBACKTYPE *mpCallBacks;
59     sp<ISVBufferManager> mISVBufferManager;
60 };
61 
62 class ISVComponent //: public RefBase
63 {
64 public:
65     /*
66      * construct & destruct
67      */
68     ISVComponent(OMX_PTR);
69     ~ISVComponent();
70 
71     // replace component callbacks
72     OMX_CALLBACKTYPE *getCallBacks(OMX_CALLBACKTYPE*);
73     // pass down the real component&core
setComponent(OMX_COMPONENTTYPE * pComp,ISVOMXCore * pCore)74     void setComponent(OMX_COMPONENTTYPE *pComp, ISVOMXCore *pCore){mComponent = pComp; mCore = pCore;return;}
75     // free the real component
freeComponent()76     OMX_ERRORTYPE freeComponent(){return (*(mCore->mFreeHandle))(static_cast<OMX_HANDLETYPE>(mComponent));}
77     // return ISV component handle
getBaseComponent()78     OMX_COMPONENTTYPE *getBaseComponent(){return &mBaseComponent;}
79 
getComponent()80     OMX_HANDLETYPE getComponent(){return static_cast<OMX_HANDLETYPE>(mComponent);}
81 
82     static Vector<ISVComponent*> g_isv_components;
83 private:
84     /*
85      * component methods & helpers
86      */
87 
88     static OMX_ERRORTYPE SendCommand(
89         OMX_IN  OMX_HANDLETYPE hComponent,
90         OMX_IN  OMX_COMMANDTYPE Cmd,
91         OMX_IN  OMX_U32 nParam1,
92         OMX_IN  OMX_PTR pCmdData);
93     OMX_ERRORTYPE ISV_SendCommand(
94         OMX_IN  OMX_COMMANDTYPE Cmd,
95         OMX_IN  OMX_U32 nParam1,
96         OMX_IN  OMX_PTR pCmdData);
97 
98     static OMX_ERRORTYPE GetParameter(
99         OMX_IN  OMX_HANDLETYPE hComponent,
100         OMX_IN  OMX_INDEXTYPE nParamIndex,
101         OMX_INOUT OMX_PTR pComponentParameterStructure);
102     OMX_ERRORTYPE ISV_GetParameter(
103         OMX_IN  OMX_INDEXTYPE nParamIndex,
104         OMX_INOUT OMX_PTR pComponentParameterStructure);
105 
106     static OMX_ERRORTYPE SetParameter(
107         OMX_IN  OMX_HANDLETYPE hComponent,
108         OMX_IN  OMX_INDEXTYPE nIndex,
109         OMX_IN  OMX_PTR pComponentParameterStructure);
110     OMX_ERRORTYPE ISV_SetParameter(
111         OMX_IN  OMX_INDEXTYPE nIndex,
112         OMX_IN  OMX_PTR pComponentParameterStructure);
113 
114     static OMX_ERRORTYPE GetConfig(
115         OMX_IN  OMX_HANDLETYPE hComponent,
116         OMX_IN  OMX_INDEXTYPE nIndex,
117         OMX_INOUT OMX_PTR pComponentConfigStructure);
118     OMX_ERRORTYPE ISV_GetConfig(
119         OMX_IN  OMX_INDEXTYPE nIndex,
120         OMX_INOUT OMX_PTR pComponentConfigStructure);
121 
122     static OMX_ERRORTYPE SetConfig(
123         OMX_IN  OMX_HANDLETYPE hComponent,
124         OMX_IN  OMX_INDEXTYPE nIndex,
125         OMX_IN  OMX_PTR pComponentConfigStructure);
126     OMX_ERRORTYPE ISV_SetConfig(
127         OMX_IN  OMX_INDEXTYPE nIndex,
128         OMX_IN  OMX_PTR pComponentConfigStructure);
129 
130     static OMX_ERRORTYPE GetExtensionIndex(
131         OMX_IN  OMX_HANDLETYPE hComponent,
132         OMX_IN  OMX_STRING cParameterName,
133         OMX_OUT OMX_INDEXTYPE* pIndexType);
134     OMX_ERRORTYPE ISV_GetExtensionIndex(
135         OMX_IN  OMX_STRING cParameterName,
136         OMX_OUT OMX_INDEXTYPE* pIndexType);
137 
138     static OMX_ERRORTYPE GetState(
139         OMX_IN  OMX_HANDLETYPE hComponent,
140         OMX_OUT OMX_STATETYPE* pState);
141     OMX_ERRORTYPE ISV_GetState(
142         OMX_OUT OMX_STATETYPE* pState);
143 
144     static OMX_ERRORTYPE UseBuffer(
145         OMX_IN OMX_HANDLETYPE hComponent,
146         OMX_INOUT OMX_BUFFERHEADERTYPE** ppBufferHdr,
147         OMX_IN OMX_U32 nPortIndex,
148         OMX_IN OMX_PTR pAppPrivate,
149         OMX_IN OMX_U32 nSizeBytes,
150         OMX_IN OMX_U8* pBuffer);
151     OMX_ERRORTYPE ISV_UseBuffer(
152         OMX_INOUT OMX_BUFFERHEADERTYPE** ppBufferHdr,
153         OMX_IN OMX_U32 nPortIndex,
154         OMX_IN OMX_PTR pAppPrivate,
155         OMX_IN OMX_U32 nSizeBytes,
156         OMX_IN OMX_U8* pBuffer);
157 
158     static OMX_ERRORTYPE AllocateBuffer(
159         OMX_IN OMX_HANDLETYPE hComponent,
160         OMX_INOUT OMX_BUFFERHEADERTYPE** ppBuffer,
161         OMX_IN OMX_U32 nPortIndex,
162         OMX_IN OMX_PTR pAppPrivate,
163         OMX_IN OMX_U32 nSizeBytes);
164     OMX_ERRORTYPE ISV_AllocateBuffer(
165         OMX_INOUT OMX_BUFFERHEADERTYPE** ppBuffer,
166         OMX_IN OMX_U32 nPortIndex,
167         OMX_IN OMX_PTR pAppPrivate,
168         OMX_IN OMX_U32 nSizeBytes);
169 
170     static OMX_ERRORTYPE FreeBuffer(
171         OMX_IN  OMX_HANDLETYPE hComponent,
172         OMX_IN  OMX_U32 nPortIndex,
173         OMX_IN  OMX_BUFFERHEADERTYPE* pBuffer);
174     OMX_ERRORTYPE ISV_FreeBuffer(
175         OMX_IN  OMX_U32 nPortIndex,
176         OMX_IN  OMX_BUFFERHEADERTYPE* pBuffer);
177 
178     static OMX_ERRORTYPE EmptyThisBuffer(
179         OMX_IN  OMX_HANDLETYPE hComponent,
180         OMX_IN  OMX_BUFFERHEADERTYPE* pBuffer);
181     OMX_ERRORTYPE ISV_EmptyThisBuffer(
182         OMX_IN  OMX_BUFFERHEADERTYPE* pBuffer);
183 
184     static OMX_ERRORTYPE FillThisBuffer(
185         OMX_IN  OMX_HANDLETYPE hComponent,
186         OMX_IN  OMX_BUFFERHEADERTYPE* pBuffer);
187     OMX_ERRORTYPE ISV_FillThisBuffer(
188         OMX_IN  OMX_BUFFERHEADERTYPE* pBuffer);
189 
190     static OMX_ERRORTYPE SetCallbacks(
191         OMX_IN  OMX_HANDLETYPE hComponent,
192         OMX_IN  OMX_CALLBACKTYPE* pCallbacks,
193         OMX_IN  OMX_PTR pAppData);
194     OMX_ERRORTYPE ISV_SetCallbacks(
195         OMX_IN  OMX_CALLBACKTYPE* pCallbacks,
196         OMX_IN  OMX_PTR pAppData);
197 
198     static OMX_ERRORTYPE ComponentRoleEnum(
199         OMX_IN OMX_HANDLETYPE hComponent,
200         OMX_OUT OMX_U8 *cRole,
201         OMX_IN OMX_U32 nIndex);
202     OMX_ERRORTYPE ISV_ComponentRoleEnum(
203         OMX_OUT OMX_U8 *cRole,
204         OMX_IN OMX_U32 nIndex);
205 
206     static OMX_ERRORTYPE FillBufferDone(
207         OMX_OUT OMX_HANDLETYPE hComponent,
208         OMX_OUT OMX_PTR pAppData,
209         OMX_OUT OMX_BUFFERHEADERTYPE* pBuffer);
210     OMX_ERRORTYPE ISV_FillBufferDone(
211         OMX_OUT OMX_HANDLETYPE hComponent,
212         OMX_OUT OMX_PTR pAppData,
213         OMX_OUT OMX_BUFFERHEADERTYPE* pBuffer);
214 
215     static OMX_ERRORTYPE EventHandler(
216             OMX_IN OMX_HANDLETYPE hComponent,
217             OMX_IN OMX_PTR pAppData,
218             OMX_IN OMX_EVENTTYPE eEvent,
219             OMX_IN OMX_U32 nData1,
220             OMX_IN OMX_U32 nData2,
221             OMX_IN OMX_PTR pEventData);
222     OMX_ERRORTYPE ISV_EventHandler(
223             OMX_IN OMX_HANDLETYPE hComponent,
224             OMX_IN OMX_PTR pAppData,
225             OMX_IN OMX_EVENTTYPE eEvent,
226             OMX_IN OMX_U32 nData1,
227             OMX_IN OMX_U32 nData2,
228             OMX_IN OMX_PTR pEventData);
229     /* end of component methods & helpers */
230 
231     void SetTypeHeader(OMX_PTR type, OMX_U32 size);
232 
233     // init & deinit functions
234     status_t init(int32_t width, int32_t height);
235     void deinit();
236 
237     const static OMX_U8 OMX_SPEC_VERSION_MAJOR = 1;
238     const static OMX_U8 OMX_SPEC_VERSION_MINOR = 0;
239     const static OMX_U8 OMX_SPEC_VERSION_REVISION = 0;
240     const static OMX_U8 OMX_SPEC_VERSION_STEP = 0;
241 
242     const static OMX_U32 OMX_SPEC_VERSION = 0
243         | (OMX_SPEC_VERSION_MAJOR << 0)
244         | (OMX_SPEC_VERSION_MINOR << 8)
245         | (OMX_SPEC_VERSION_REVISION << 16)
246         | (OMX_SPEC_VERSION_STEP << 24);
247 
248     typedef enum OMX_ISVINDEXEXTTYPE {
249         OMX_IndexISVStartUsed = OMX_IndexVendorStartUnused + 0x0000F000,
250         OMX_IndexExtSetISVMode,                  /**< reference: OMX_U32 */
251     } OMX_ISVINDEXEXTTYPE;
252 
253     typedef enum {
254         ISV_DISABLE = 0,
255         ISV_AUTO,
256     } ISV_MODE;
257 
258 private:
259     OMX_COMPONENTTYPE mBaseComponent;  //returned by GetComponetHandle()
260     OMX_COMPONENTTYPE *mComponent;      // passed from the real OMX core
261     OMX_CALLBACKTYPE *mpCallBacks;
262     ISVOMXCore *mCore;                  // owend by mComponent
263     OMX_CALLBACKTYPE *mpISVCallBacks;
264 
265     // buffer manager
266     sp<ISVBufferManager> mISVBufferManager;
267 
268     bool mThreadRunning;
269 
270     // vpp thread observer
271     sp<ISVProcThreadObserver> mProcThreadObserver;
272 
273     // vpp input buffer count + output buffer count
274     int32_t mNumISVBuffers;
275     int32_t mNumDecoderBuffers;
276     int32_t mNumDecoderBuffersBak;
277     /* To speed up the start up output decoder buffer directly
278      * for certain frames. ISV worker pipeline set up is hide by (in parallel with)
279      * display these output frames.
280      */
281     int32_t mOutputDecoderBufferNum;
282     uint32_t mWidth;
283     uint32_t mHeight;
284     uint32_t mUseAndroidNativeBufferIndex;
285     uint32_t mStoreMetaDataInBuffersIndex;
286     uint32_t mHackFormat;
287 
288     bool mUseAndroidNativeBuffer;
289     bool mUseAndroidNativeBuffer2;
290 
291     bool mVPPEnabled;
292     bool mVPPOn;
293     bool mVPPFlushing;
294     bool mOutputCropChanged;
295     bool mInitialized;
296 #ifdef TARGET_VPP_USE_GEN
297     // vpp thread
298     sp<ISVProcessor> mProcThread;
299 #else
300     static sp<ISVProcessor> mProcThread;
301 #endif
302     // protect create mProcThread instance
303     bool mOwnProcessor;
304     static pthread_mutex_t ProcThreadInstanceLock;
305     Mutex mDecoderBufLock;
306 };
307 
308 #endif  // #define ISV_OMXCOMPONENT_H_
309