1 /* Copyright (c) 2012, The Linux Foundation. All rights reserved.
2  *
3  * Redistribution and use in source and binary forms, with or without
4  * modification, are permitted provided that the following conditions are
5  * met:
6  *     * Redistributions of source code must retain the above copyright
7  *       notice, this list of conditions and the following disclaimer.
8  *     * Redistributions in binary form must reproduce the above
9  *       copyright notice, this list of conditions and the following
10  *       disclaimer in the documentation and/or other materials provided
11  *       with the distribution.
12  *     * Neither the name of The Linux Foundation nor the names of its
13  *       contributors may be used to endorse or promote products derived
14  *       from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  */
29 
30 #ifndef _OMX_JPEG_COMMON_H_
31 #define _OMX_JPEG_COMMON_H_
32 #include "OMX_Core.h"
33 #include "stdio.h"
34 #include "omx_debug.h"
35 
36 #define INPUT_PORT 0
37 #define OUTPUT_PORT 1
38 #define CEILING16(X) (((X) + 0x000F) & 0xFFF0)
39 #define YUV_SIZER(W, H) ((W * H * 3) /2 )
40 
41 #define DEFAULT_PICTURE_WIDTH 640
42 #define DEFAULT_PICTURE_HEIGHT 480
43 
44 #define GET_COMP(X) ((OMX_PTR)((OMX_COMPONENTTYPE*)X)->pComponentPrivate)
45 #define PAD_TO_WORD(a)               (((a)+3)&~3)
46 #define OMX_JPEG_QUEUE_CAPACITY 100
47 
48 
49 #define OMX_MM_MALLOC(size) jpeg_malloc(size, __FILE__, __LINE__)
50 #define OMX_MM_ZERO(pointer, type) memset(pointer, 0 ,sizeof(type))
51 #define OMX_MM_FREE(pointer) {if (pointer) \
52                                jpeg_free(pointer); \
53                              else \
54 	                       OMX_DBG_INFO("%s:%d: null pointer", __FILE__, __LINE__); \
55                              pointer = NULL; \
56 	                     }
57 #define ONERROR(X, handler) if(X) { \
58         OMX_DBG_ERROR("Failure at  %s:%d", __FILE__, __LINE__); \
59         handler; \
60         return X; \
61     }
62 #define ERROR_ENCODING(X, handler) if(X) { \
63         OMX_DBG_ERROR("Failure at  %s:%d", __FILE__, __LINE__); \
64         handler; \
65         pthread_mutex_unlock(&comp->abort_mutex); \
66         return X; \
67     }
68 #define ONWARNING(X) if(X) {\
69         OMX_DBG_ERROR("Warning: Failure at %s:%d", __FILE__, __LINE__); \
70     }
71 
72 #define MIN(a,b)  (((a) < (b)) ? (a) : (b))
73 
74 /* These are custom events added as an extention to the OMX Events*/
75 typedef enum {
76   OMX_EVENT_ETB_DONE = OMX_EventVendorStartUnused+1,
77   OMX_EVENT_FTB_DONE,
78   OMX_EVENT_THUMBNAIL_DROPPED,
79   OMX_EVENT_JPEG_ERROR,
80   OMX_EVENT_JPEG_ABORT,
81   OMX_EVENT_MAIN_IMAGE,
82   OMX_EVENT_THUMBNAIL_IMAGE,
83   OMX_EVENT_DONE
84 }omx_jpeg_events;
85 
86 /*This enum is consistent with the jpeg_color_format enum.
87 Please be careful while changing the order. It has to
88 match exactly*/
89 typedef enum {
90   OMX_YCRCBLP_H2V2 = 0,
91   OMX_YCBCRLP_H2V2 = 1,
92 
93   OMX_YCRCBLP_H2V1 = 2,
94   OMX_YCBCRLP_H2V1 = 3,
95 
96   OMX_YCRCBLP_H1V2 = 4,
97   OMX_YCBCRLP_H1V2 = 5,
98 
99   OMX_YCRCBLP_H1V1 = 6,
100   OMX_YCBCRLP_H1V1 = 7,
101 
102   OMX_RGB565 = 8,
103   OMX_RGB888 = 9,
104   OMX_RGBa   = 10,
105 
106   OMX_JPEG_BITSTREAM_H2V2 = 12,
107   OMX_JPEG_BITSTREAM_H2V1 = 14,
108   OMX_JPEG_BITSTREAM_H1V2 = 16,
109   OMX_JPEG_BITSTREAM_H1V1 = 18,
110 
111   OMX_JPEG_COLOR_FORMAT_MAX,
112 
113 } omx_jpeg_color_format;
114 
115 /*This enum is consistent with the jpege_preferences_t  and
116 jpegd_preferences_t enum.Please be careful while changing the order.
117 It has to match exactly*/
118 typedef enum {
119   OMX_JPEG_PREF_HW_ACCELERATED_PREFERRED = 0,
120   OMX_JPEG_PREF_HW_ACCELERATED_ONLY,
121   OMX_JPEG_PREF_SOFTWARE_PREFERRED,
122   OMX_JPEG_PREF_SOFTWARE_ONLY,
123   OMX_JPEG_PREF_MAX,
124 
125 } omx_jpeg_preference;
126 
127 typedef enum {
128     OMX_JPEG_MESSAGE_INITIAL=0,
129     OMX_JPEG_MESSAGE_ETB,
130     OMX_JPEG_MESSAGE_FTB,
131     OMX_JPEG_MESSAGE_ETB_DONE,
132     OMX_JPEG_MESSAGE_FTB_DONE,
133     OMX_JPEG_MESSAGE_START_ENCODE,
134     OMX_JPEG_MESSAGE_START_DECODE,
135     OMX_JPEG_MESSAGE_CHANGE_STATE,
136     OMX_JPEG_MESSAGE_FLUSH,
137     OMX_JPEG_MESSAGE_STOP,
138     OMX_JPEG_MESSAGE_FLUSH_COMPLETE,
139     OMX_JPEG_MESSAGE_TRANSACT_COMPLETE,
140     OMX_JPEG_MESSAGE_DEINIT,
141     OMX_JPEG_MESSAGE_EVENT, //for event callback args contain event info
142     OMX_JPEG_MESSAGE_DECODED_IMAGE,
143     OMX_JPEG_MESSAGE_DECODE_DONE,
144 
145 }omx_jpeg_message;
146 
147 typedef enum omx_jpeg_image_type{
148   OMX_JPEG =0,
149   OMX_JPS,
150   OMX_MPO,
151 
152 }omx_jpeg_image_type;
153 
154 //message arg
155 typedef union omx_jpeg_message_arg{
156     void * pValue;
157     int iValue;
158 }omx_jpeg_message_arg;
159 
160 //message
161 typedef struct omx_jpeg_queue_item{
162     omx_jpeg_message message;
163     omx_jpeg_message_arg args[3];
164 
165 }omx_jpeg_queue_item;
166 
167 
168 typedef enum {
169     OMX_JPEG_QUEUE_COMMAND=0,
170     OMX_JPEG_QUEUE_ETB,
171     OMX_JPEG_QUEUE_FTB,
172     OMX_JPEG_QUEUE_ABORT,
173 }omx_jpeg_queue_type;
174 
175 typedef struct omx_jpeg_queue{
176     omx_jpeg_queue_item container[OMX_JPEG_QUEUE_CAPACITY];
177     int front;
178     int back;
179     int size;
180     omx_jpeg_queue_type type;
181 }omx_jpeg_queue;
182 
183 typedef struct omx_jpeg_message_queue{
184     omx_jpeg_queue command;
185     omx_jpeg_queue etb;
186     omx_jpeg_queue ftb;
187     omx_jpeg_queue abort;
188     pthread_mutex_t lock;
189     pthread_cond_t cond;
190     int messageCount;
191     int initialized;
192 }omx_jpeg_message_queue;
193 
194 int omx_jpeg_queue_insert(omx_jpeg_queue* queue, omx_jpeg_queue_item * item);
195 int omx_jpeg_queue_remove(omx_jpeg_queue * queue, omx_jpeg_queue_item* item);
196 void omx_jpeg_message_queue_init(omx_jpeg_message_queue * queue);
197 void omx_jpeg_queue_init(omx_jpeg_queue * queue);
198 int omx_jpeg_queue_flush(omx_jpeg_queue * queue);
199 //Common OMX Functions
200 OMX_ERRORTYPE
201 omx_component_image_init(OMX_IN OMX_HANDLETYPE hComp, OMX_IN OMX_STRING componentName);
202 
203 
204 OMX_ERRORTYPE
205 omx_component_image_get_version(OMX_IN OMX_HANDLETYPE               hComp,
206                              OMX_OUT OMX_STRING          componentName,
207                              OMX_OUT OMX_VERSIONTYPE* componentVersion,
208                              OMX_OUT OMX_VERSIONTYPE*      specVersion,
209                              OMX_OUT OMX_UUIDTYPE*       componentUUID);
210 
211 OMX_ERRORTYPE
212 omx_component_image_send_command(OMX_IN OMX_HANDLETYPE hComp,
213                               OMX_IN OMX_COMMANDTYPE  cmd,
214                               OMX_IN OMX_U32       param1,
215                               OMX_IN OMX_PTR      cmdData);
216 
217 OMX_ERRORTYPE
218 omx_component_image_get_parameter(OMX_IN OMX_HANDLETYPE     hComp,
219                                OMX_IN OMX_INDEXTYPE paramIndex,
220                                OMX_INOUT OMX_PTR     paramData);
221 
222 OMX_ERRORTYPE
223 omx_component_image_set_parameter(OMX_IN OMX_HANDLETYPE     hComp,
224                                OMX_IN OMX_INDEXTYPE paramIndex,
225                                OMX_IN OMX_PTR        paramData);
226 
227 OMX_ERRORTYPE
228 omx_component_image_get_config(OMX_IN OMX_HANDLETYPE      hComp,
229           OMX_IN OMX_INDEXTYPE configIndex,
230           OMX_INOUT OMX_PTR     configData);
231 
232 OMX_ERRORTYPE
233 omx_component_image_set_config(OMX_IN OMX_HANDLETYPE      hComp,
234                             OMX_IN OMX_INDEXTYPE configIndex,
235                             OMX_IN OMX_PTR        configData);
236 
237 OMX_ERRORTYPE
238 omx_component_image_get_extension_index(OMX_IN OMX_HANDLETYPE      hComp,
239                                      OMX_IN OMX_STRING      paramName,
240                                      OMX_OUT OMX_INDEXTYPE* indexType);
241 
242 OMX_ERRORTYPE
243 omx_component_image_get_state(OMX_IN OMX_HANDLETYPE  hComp,
244                            OMX_OUT OMX_STATETYPE* state);
245 
246 OMX_ERRORTYPE
247 omx_component_image_tunnel_request(OMX_IN OMX_HANDLETYPE                hComp,
248                                 OMX_IN OMX_U32                        port,
249                                 OMX_IN OMX_HANDLETYPE        peerComponent,
250                                 OMX_IN OMX_U32                    peerPort,
251                                 OMX_INOUT OMX_TUNNELSETUPTYPE* tunnelSetup);
252 
253 OMX_ERRORTYPE
254 omx_component_image_use_buffer(OMX_IN OMX_HANDLETYPE                hComp,
255                             OMX_INOUT OMX_BUFFERHEADERTYPE** bufferHdr,
256                             OMX_IN OMX_U32                        port,
257                             OMX_IN OMX_PTR                     appData,
258                             OMX_IN OMX_U32                       bytes,
259                             OMX_IN OMX_U8*                      buffer);
260 
261 
262 OMX_ERRORTYPE
263 omx_component_image_allocate_buffer(OMX_IN OMX_HANDLETYPE                hComp,
264                                  OMX_INOUT OMX_BUFFERHEADERTYPE** bufferHdr,
265                                  OMX_IN OMX_U32                        port,
266                                  OMX_IN OMX_PTR                     appData,
267                                  OMX_IN OMX_U32                       bytes);
268 
269 OMX_ERRORTYPE
270 omx_component_image_free_buffer(OMX_IN OMX_HANDLETYPE         hComp,
271                              OMX_IN OMX_U32                 port,
272                              OMX_IN OMX_BUFFERHEADERTYPE* buffer);
273 
274 OMX_ERRORTYPE
275 omx_component_image_empty_this_buffer(OMX_IN OMX_HANDLETYPE         hComp,
276                                    OMX_IN OMX_BUFFERHEADERTYPE* buffer);
277 
278 OMX_ERRORTYPE
279 omx_component_image_fill_this_buffer(OMX_IN OMX_HANDLETYPE         hComp,
280                                   OMX_IN OMX_BUFFERHEADERTYPE* buffer);
281 
282 OMX_ERRORTYPE
283 omx_component_image_set_callbacks(OMX_IN OMX_HANDLETYPE        hComp,
284                                OMX_IN OMX_CALLBACKTYPE* callbacks,
285                                OMX_IN OMX_PTR             appData);
286 
287 OMX_ERRORTYPE
288 omx_component_image_deinit(OMX_IN OMX_HANDLETYPE hComp);
289 
290 OMX_ERRORTYPE
291 omx_component_image_use_EGL_image(OMX_IN OMX_HANDLETYPE                hComp,
292                                OMX_INOUT OMX_BUFFERHEADERTYPE** bufferHdr,
293                                OMX_IN OMX_U32                        port,
294                                OMX_IN OMX_PTR                     appData,
295                                OMX_IN void*                      eglImage);
296 
297 OMX_ERRORTYPE
298 omx_component_image_role_enum(OMX_IN OMX_HANDLETYPE hComp,
299                            OMX_OUT OMX_U8*        role,
300                            OMX_IN OMX_U32        index);
301 
302 #endif
303