1/*
2 * portbase.h, port base class
3 *
4 * Copyright (c) 2009-2010 Wind River Systems, Inc.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19#ifndef __PORTBASE_H
20#define __PORTBASE_H
21
22#include <pthread.h>
23
24#include <OMX_Core.h>
25#include <OMX_Component.h>
26
27#include <list.h>
28#include <queue.h>
29
30typedef OMX_U8* CustomMemAlloc(OMX_U32 nSizeBytes, OMX_PTR pUserData);
31typedef void  CustomMemFree(OMX_U8 *pBuffer, OMX_PTR pUserData);
32
33class PortBase
34{
35public:
36    /*
37     * constructor & destructor
38     */
39    PortBase();
40    PortBase(const OMX_PARAM_PORTDEFINITIONTYPE *portdefinition);
41    virtual ~PortBase();
42
43    /* end of constructor & destructor */
44
45    /*
46     * accessor
47     */
48    /* owner */
49    void SetOwner(OMX_COMPONENTTYPE *handle);
50    OMX_COMPONENTTYPE *GetOwner(void);
51
52    /* for ReturnThisBuffer() */
53    OMX_ERRORTYPE SetCallbacks(OMX_HANDLETYPE hComponent,
54                               OMX_CALLBACKTYPE *pCallbacks,
55                               OMX_PTR pAppData);
56    /* end of accessor */
57
58    OMX_ERRORTYPE SetMemAllocator(CustomMemAlloc *pMemAlloc, CustomMemFree *pMemFree, OMX_PTR pUserData);
59
60    /* set port buffer alignment, nAlignment=0 means alignment disabled */
61    OMX_ERRORTYPE SetMemAlignment(OMX_U32 nAlignment);
62
63    /*
64     * component methods & helpers
65     */
66    /* Get/SetParameter */
67    OMX_ERRORTYPE SetPortDefinition(const OMX_PARAM_PORTDEFINITIONTYPE *p,
68                                    bool overwrite_readonly);
69    const OMX_PARAM_PORTDEFINITIONTYPE *GetPortDefinition(void);
70
71    /* Use/Allocate/FreeBuffer */
72    OMX_ERRORTYPE UseBuffer(OMX_BUFFERHEADERTYPE **ppBufferHdr,
73                            OMX_U32 nPortIndex,
74                            OMX_PTR pAppPrivate,
75                            OMX_U32 nSizeBytes,
76                            OMX_U8 *pBuffer);
77    OMX_ERRORTYPE AllocateBuffer(OMX_BUFFERHEADERTYPE **ppBuffer,
78                                 OMX_U32 nPortIndex,
79                                 OMX_PTR pAppPrivate,
80                                 OMX_U32 nSizeBytes);
81    OMX_ERRORTYPE FreeBuffer(OMX_U32 nPortIndex,
82                             OMX_BUFFERHEADERTYPE *pBuffer);
83
84    /*
85     * called in ComponentBase::TransStateToLoaded(OMX_StateIdle) or
86     * in ComponentBase::TransStateToIdle(OMX_StateLoaded)
87     * wokeup by Use/Allocate/FreeBuffer
88     */
89    void WaitPortBufferCompletion(void);
90
91    OMX_ERRORTYPE WaitPortBufferCompletionTimeout(int64_t mSec);
92    /* Empty/FillThisBuffer */
93    OMX_ERRORTYPE PushThisBuffer(OMX_BUFFERHEADERTYPE *pBuffer);
94    OMX_BUFFERHEADERTYPE *PopBuffer(void);
95    OMX_U32 BufferQueueLength(void);
96    OMX_U32 RetainedBufferQueueLength(void);
97
98    /* Empty/FillBufferDone */
99    OMX_ERRORTYPE ReturnThisBuffer(OMX_BUFFERHEADERTYPE *pBuffer);
100
101    OMX_ERRORTYPE RetainAndReturnBuffer(OMX_BUFFERHEADERTYPE *pRetain, OMX_BUFFERHEADERTYPE *pReturn);
102
103    /* retain buffer */
104    OMX_ERRORTYPE RetainThisBuffer(OMX_BUFFERHEADERTYPE *pBuffer,
105                                   bool accumulate);
106    /*
107     * components have responsibilty of calling this function to return all
108     * accumulated buffers to omx-il clients.
109     */
110    void ReturnAllRetainedBuffers(void);
111    void ReturnOneRetainedBuffer(void);
112
113    /* flush all buffers not under processing */
114    OMX_ERRORTYPE FlushPort(void);
115
116    bool IsEnabled(void);
117
118    OMX_DIRTYPE GetPortDirection(void);
119    OMX_U32 GetPortBufferCount(void);
120
121    OMX_ERRORTYPE PushMark(OMX_MARKTYPE *mark);
122    OMX_MARKTYPE *PopMark(void);
123
124    /* SendCommand(OMX_CommandPortDisable/Enable) */
125    OMX_ERRORTYPE TransState(OMX_U8 state);
126
127    /* EventHandler(OMX_EventPortSettingChanged) */
128    OMX_ERRORTYPE ReportPortSettingsChanged(void);
129    OMX_ERRORTYPE ReportOutputCrop(void);
130    /* get frame size */
131    OMX_U32 getFrameBufSize(OMX_COLOR_FORMATTYPE colorFormat, OMX_U32 width, OMX_U32 height);
132
133    /* end of component methods & helpers */
134
135    /* TransState, state */
136    static const OMX_U8 OMX_PortDisabled = 0;
137    static const OMX_U8 OMX_PortEnabled = 1;
138
139private:
140    /* common routines for constructor */
141    void __PortBase(void);
142
143    /*
144     * component methods & helpers
145     */
146    OMX_STATETYPE GetOwnerState(void);
147
148    /* end of component methods & helpers */
149
150    /* buffer headers */
151    struct list *buffer_hdrs;
152    OMX_U32 nr_buffer_hdrs;
153    bool buffer_hdrs_completion; /* Use/Allocate/FreeBuffer completion flag */
154    pthread_mutex_t hdrs_lock;
155    pthread_cond_t hdrs_wait;
156
157    struct queue bufferq;
158    pthread_mutex_t bufferq_lock;
159
160    /* retained buffers (only accumulated buffer) */
161    struct queue retainedbufferq;
162    pthread_mutex_t retainedbufferq_lock;
163
164    struct queue markq;
165    pthread_mutex_t markq_lock;
166
167    /* state */
168    OMX_U8 state;
169    pthread_mutex_t state_lock;
170
171    CustomMemAlloc *custom_mem_alloc;
172    CustomMemFree *custom_mem_free;
173    OMX_PTR custom_mem_userdata;
174
175    OMX_U32 mem_alignment;
176
177    /* parameter */
178    OMX_PARAM_PORTDEFINITIONTYPE portdefinition;
179    /* room for portdefinition.format.*.cMIMEType */
180    char definition_format_mimetype[OMX_MAX_STRINGNAME_SIZE];
181
182    OMX_AUDIO_PARAM_PORTFORMATTYPE audioparam;
183
184    /* owner handle */
185    OMX_COMPONENTTYPE *owner;
186
187    /* omx standard callbacks */
188    OMX_PTR appdata;
189    OMX_CALLBACKTYPE *callbacks;
190
191    /* wrs component handle */
192    class ComponentBase *cbase;
193
194    /* Input port size limit. The limit is set to be the size of a 1080P 4:4:4 raw image size,
195       which is 1920x1080x3. */
196    enum {
197        MAX_INPUT_PORT_SIZE = 6220800
198    };
199};
200
201/* end of PortBase */
202
203#endif /* __PORTBASE_H */
204