Merge tizen patch based on 1.12.2
[platform/upstream/gstreamer.git] / omx / gstomx.h
1 /*
2  * Copyright (C) 2011, Hewlett-Packard Development Company, L.P.
3  *   Author: Sebastian Dröge <sebastian.droege@collabora.co.uk>, Collabora Ltd.
4  * Copyright (C) 2013, Collabora Ltd.
5  *   Author: Sebastian Dröge <sebastian.droege@collabora.co.uk>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation
10  * version 2.1 of the License.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
20  *
21  */
22
23 #ifndef __GST_OMX_H__
24 #define __GST_OMX_H__
25
26 #include <gmodule.h>
27 #include <gst/gst.h>
28 #include <string.h>
29 #include <mm_types.h>
30
31 #ifdef HAVE_CONFIG_H
32 #include "config.h"
33 #endif
34
35 #ifdef GST_OMX_STRUCT_PACKING
36 # if GST_OMX_STRUCT_PACKING == 1
37 #  pragma pack(1)
38 # elif GST_OMX_STRUCT_PACKING == 2
39 #  pragma pack(2)
40 # elif GST_OMX_STRUCT_PACKING == 4
41 #  pragma pack(4)
42 # elif GST_OMX_STRUCT_PACKING == 8
43 #  pragma pack(8)
44 # else
45 #  error "Unsupported struct packing value"
46 # endif
47 #endif
48
49 /* If the component may signal EOS before it has finished pushing
50  * out all of its buffers. Happens with egl_render on the rpi.
51  */
52 #define GST_OMX_HACK_SIGNALS_PREMATURE_EOS                            G_GUINT64_CONSTANT (0x0000000000000400)
53
54 #include <OMX_Core.h>
55 #include <OMX_Component.h>
56
57 #ifdef TIZEN_FEATURE_OMX
58 #include <tbm_type.h>
59 #include <tbm_surface.h>
60 #include <tbm_bufmgr.h>
61 #include <unistd.h>
62 #endif
63
64 #ifdef USE_OMX_TARGET_RPI
65 #include <OMX_Broadcom.h>
66 #endif
67
68 #ifdef HAVE_VIDEO_EXT
69 #include <OMX_VideoExt.h>
70 #endif
71
72 #ifdef GST_OMX_STRUCT_PACKING
73 #pragma pack()
74 #endif
75
76 G_BEGIN_DECLS
77
78 #define GST_OMX_INIT_STRUCT(st) G_STMT_START { \
79   memset ((st), 0, sizeof (*(st))); \
80   (st)->nSize = sizeof (*(st)); \
81   (st)->nVersion.s.nVersionMajor = OMX_VERSION_MAJOR; \
82   (st)->nVersion.s.nVersionMinor = OMX_VERSION_MINOR; \
83   (st)->nVersion.s.nRevision = OMX_VERSION_REVISION; \
84   (st)->nVersion.s.nStep = OMX_VERSION_STEP; \
85 } G_STMT_END
86
87 #if defined(USE_OMX_TARGET_EXYNOS) || defined(USE_OMX_TARGET_EXYNOS64)
88 #define OMX_INIT_PARAM(param) G_STMT_START { \
89   memset (&(param), 0, sizeof ((param))); \
90   (param).nSize = sizeof (param); \
91   (param).nVersion.s.nVersionMajor = 1; \
92   (param).nVersion.s.nVersionMinor = 1; \
93 } G_STMT_END
94 #endif
95
96 /* Different hacks that are required to work around
97  * bugs in different OpenMAX implementations
98  */
99 /* In the EventSettingsChanged callback use nData2 instead of nData1 for
100  * the port index. Happens with Bellagio.
101  */
102 #define GST_OMX_HACK_EVENT_PORT_SETTINGS_CHANGED_NDATA_PARAMETER_SWAP G_GUINT64_CONSTANT (0x0000000000000001)
103 /* In the EventSettingsChanged callback assume that port index 0 really
104  * means port index 1. Happens with the Bellagio ffmpegdist video decoder.
105  */
106 #define GST_OMX_HACK_EVENT_PORT_SETTINGS_CHANGED_PORT_0_TO_1          G_GUINT64_CONSTANT (0x0000000000000002)
107 /* If the video framerate is not specified as fraction (Q.16) but as
108  * integer number. Happens with the Bellagio ffmpegdist video encoder.
109  */
110 #define GST_OMX_HACK_VIDEO_FRAMERATE_INTEGER                          G_GUINT64_CONSTANT (0x0000000000000004)
111 /* If the SYNCFRAME flag on encoder output buffers is not used and we
112  * have to assume that all frames are sync frames.
113  * Happens with the Bellagio ffmpegdist video encoder.
114  */
115 #define GST_OMX_HACK_SYNCFRAME_FLAG_NOT_USED                          G_GUINT64_CONSTANT (0x0000000000000008)
116 /* If the component needs to be re-created if the caps change.
117  * Happens with Qualcomm's OpenMAX implementation.
118  */
119 #define GST_OMX_HACK_NO_COMPONENT_RECONFIGURE                         G_GUINT64_CONSTANT (0x0000000000000010)
120
121 /* If the component does not accept empty EOS buffers.
122  * Happens with Qualcomm's OpenMAX implementation.
123  */
124 #define GST_OMX_HACK_NO_EMPTY_EOS_BUFFER                              G_GUINT64_CONSTANT (0x0000000000000020)
125
126 /* If the component might not acknowledge a drain.
127  * Happens with TI's Ducati OpenMAX implementation.
128  */
129 #define GST_OMX_HACK_DRAIN_MAY_NOT_RETURN                             G_GUINT64_CONSTANT (0x0000000000000040)
130
131 /* If the component doesn't allow any component role to be set.
132  * Happens with Broadcom's OpenMAX implementation.
133  */
134 #define GST_OMX_HACK_NO_COMPONENT_ROLE                                G_GUINT64_CONSTANT (0x0000000000000080)
135
136 /* If the component doesn't allow disabling the outport while
137  * when setting the format until the output format is known.
138  */
139 #define GST_OMX_HACK_NO_DISABLE_OUTPORT                               G_GUINT64_CONSTANT (0x0000000000000100)
140
141 /* If the encoder requires input buffers that have a height
142  * which is a multiple of 16 pixels
143  */
144 #define GST_OMX_HACK_HEIGHT_MULTIPLE_16             G_GUINT64_CONSTANT (0x0000000000000200)
145
146 /* If we should pass the profile/level information from upstream to the
147  * OMX decoder. This is a violation of the OMX spec as
148  * OMX_IndexParamVideoProfileLevelCurrent is supposed to be r-o so
149  * do it as a platform specific hack.
150  */
151 #define GST_OMX_HACK_PASS_PROFILE_TO_DECODER        G_GUINT64_CONSTANT (0x0000000000000800)
152
153 typedef struct _GstOMXCore GstOMXCore;
154 typedef struct _GstOMXPort GstOMXPort;
155 typedef enum _GstOMXPortDirection GstOMXPortDirection;
156 typedef struct _GstOMXComponent GstOMXComponent;
157 typedef struct _GstOMXBuffer GstOMXBuffer;
158 typedef struct _GstOMXClassData GstOMXClassData;
159 typedef struct _GstOMXMessage GstOMXMessage;
160
161 /* MODIFICATION */
162 typedef enum GOmxVendor GOmxVendor; /* check omx vender */
163
164 #ifdef TIZEN_FEATURE_OMX
165
166 #define MFC_INPUT_BUFFER_PLANE      1
167 #define MFC_OUTPUT_BUFFER_PLANE     2
168 #define MAX_BUFFER_PLANE            3
169
170 #define MAX_INPUT_BUFFER            16
171 #define MAX_OUTPUT_BUFFER           16
172
173 #define ALIGN(x, a)       (((x) + (a) - 1) & ~((a) - 1))
174
175 typedef struct _TBMBuffer TBMBuffer;
176 typedef struct _TBMInputBuffer TBMInputBuffer;
177 typedef struct _TBMOutputBuffer TBMOutputBuffer;
178 typedef struct _EnableGemBuffersParams EnableGemBuffersParams;
179
180 struct _TBMBuffer
181 {
182    OMX_U32 mBufFD;
183    tbm_bo  mBo;
184    OMX_PTR mPtr;
185    OMX_U32 nAllocLen;
186 };
187
188 struct _TBMInputBuffer
189 {
190     struct _TBMBuffer tbmBuffer[MAX_INPUT_BUFFER];
191     OMX_U32 allocatedCount;
192     GList *buffers;
193 };
194
195 struct _TBMOutputBuffer
196 {
197     MMVideoBuffer *tbmBuffer[MAX_OUTPUT_BUFFER];
198     OMX_U32 allocatedCount;
199     GList *buffers;
200 };
201
202 struct _EnableGemBuffersParams
203 {
204   OMX_U32 nSize;
205   OMX_VERSIONTYPE nVersion;
206   OMX_U32 nPortIndex;
207   OMX_BOOL enable;
208 };
209 #endif
210
211 typedef enum {
212   /* Everything good and the buffer is valid */
213   GST_OMX_ACQUIRE_BUFFER_OK = 0,
214   /* The port is flushing, exit ASAP */
215   GST_OMX_ACQUIRE_BUFFER_FLUSHING,
216   /* The port must be reconfigured */
217   GST_OMX_ACQUIRE_BUFFER_RECONFIGURE,
218   /* The port is EOS */
219   GST_OMX_ACQUIRE_BUFFER_EOS,
220   /* A fatal error happened */
221   GST_OMX_ACQUIRE_BUFFER_ERROR
222 } GstOMXAcquireBufferReturn;
223
224 struct _GstOMXCore {
225   /* Handle to the OpenMAX IL core shared library */
226   GModule *module;
227
228   /* Current number of users, transitions from/to 0
229    * call init/deinit */
230   GMutex lock;
231   gint user_count; /* LOCK */
232
233   /* MODIFICATION */
234   gboolean secure; /* trust zone */
235
236   /* OpenMAX core library functions, protected with LOCK */
237   OMX_ERRORTYPE (*init) (void);
238   OMX_ERRORTYPE (*deinit) (void);
239   OMX_ERRORTYPE (*get_handle) (OMX_HANDLETYPE * handle,
240       OMX_STRING name, OMX_PTR data, OMX_CALLBACKTYPE * callbacks);
241   OMX_ERRORTYPE (*free_handle) (OMX_HANDLETYPE handle);
242   OMX_ERRORTYPE (*setup_tunnel) (OMX_HANDLETYPE output, OMX_U32 outport, OMX_HANDLETYPE input, OMX_U32 inport);
243 };
244
245 typedef enum {
246   GST_OMX_MESSAGE_STATE_SET,
247   GST_OMX_MESSAGE_FLUSH,
248   GST_OMX_MESSAGE_ERROR,
249   GST_OMX_MESSAGE_PORT_ENABLE,
250   GST_OMX_MESSAGE_PORT_SETTINGS_CHANGED,
251   GST_OMX_MESSAGE_BUFFER_FLAG,
252   GST_OMX_MESSAGE_BUFFER_DONE,
253 } GstOMXMessageType;
254
255 typedef enum {
256   GST_OMX_COMPONENT_TYPE_SINK,
257   GST_OMX_COMPONENT_TYPE_SOURCE,
258   GST_OMX_COMPONENT_TYPE_FILTER
259 } GstOmxComponentType;
260
261 struct _GstOMXMessage {
262   GstOMXMessageType type;
263
264   union {
265     struct {
266       OMX_STATETYPE state;
267     } state_set;
268     struct {
269       OMX_U32 port;
270     } flush;
271     struct {
272       OMX_ERRORTYPE error;
273     } error;
274     struct {
275       OMX_U32 port;
276       OMX_BOOL enable;
277     } port_enable;
278     struct {
279       OMX_U32 port;
280     } port_settings_changed;
281     struct {
282       OMX_U32 port;
283       OMX_U32 flags;
284     } buffer_flag;
285     struct {
286       OMX_HANDLETYPE component;
287       OMX_PTR app_data;
288       OMX_BUFFERHEADERTYPE *buffer;
289       OMX_BOOL empty;
290     } buffer_done;
291   } content;
292 };
293
294 struct _GstOMXPort {
295   GstOMXComponent *comp;
296   guint32 index;
297
298   gboolean tunneled;
299
300   OMX_PARAM_PORTDEFINITIONTYPE port_def;
301   GPtrArray *buffers; /* Contains GstOMXBuffer* */
302   GQueue pending_buffers; /* Contains GstOMXBuffer* */
303   gboolean flushing;
304   gboolean flushed; /* TRUE after OMX_CommandFlush was done */
305   gboolean enabled_pending;  /* TRUE after OMX_Command{En,Dis}able */
306   gboolean disabled_pending; /* was done until it took effect */
307   gboolean eos; /* TRUE after a buffer with EOS flag was received */
308 #ifdef TIZEN_FEATURE_OMX
309   gboolean use_buffer;
310   gboolean flush_start;
311 #endif
312
313   /* Increased whenever the settings of these port change.
314    * If settings_cookie != configured_settings_cookie
315    * the port has to be reconfigured.
316    */
317   gint settings_cookie;
318   gint configured_settings_cookie;
319 };
320
321 struct _GstOMXComponent {
322   GstObject *parent;
323
324   gchar *name; /* for debugging mostly */
325
326   OMX_HANDLETYPE handle;
327   GstOMXCore *core;
328
329   guint64 hacks; /* Flags, GST_OMX_HACK_* */
330
331   /* Added once, never changed. No locks necessary */
332   GPtrArray *ports; /* Contains GstOMXPort* */
333   gint n_in_ports, n_out_ports;
334
335   /* Locking order: lock -> messages_lock
336    *
337    * Never hold lock while waiting for messages_cond
338    * Always check that messages is empty before waiting */
339   GMutex lock;
340
341   GQueue messages; /* Queue of GstOMXMessages */
342   GMutex messages_lock;
343   GCond messages_cond;
344
345   OMX_STATETYPE state;
346   /* OMX_StateInvalid if no pending state */
347   OMX_STATETYPE pending_state;
348   /* OMX_ErrorNone usually, if different nothing will work */
349   OMX_ERRORTYPE last_error;
350
351   GList *pending_reconfigure_outports;
352 };
353
354 struct _GstOMXBuffer {
355   GstOMXPort *port;
356   OMX_BUFFERHEADERTYPE *omx_buf;
357
358   /* TRUE if the buffer is used by the port, i.e.
359    * between {Empty,Fill}ThisBuffer and the callback
360    */
361   gboolean used;
362
363   /* Cookie of the settings when this buffer was allocated */
364   gint settings_cookie;
365
366   /* TRUE if this is an EGLImage */
367   gboolean eglimage;
368
369 #ifdef TIZEN_FEATURE_OMX
370   /* MMVideoBuffer array to use TBM buffers */
371    MMVideoBuffer *mm_vbuffer;
372 #endif
373 };
374
375 struct _GstOMXClassData {
376   const gchar *core_name;
377   const gchar *component_name;
378   const gchar *component_role;
379
380   const gchar *default_src_template_caps;
381   const gchar *default_sink_template_caps;
382
383   guint32 in_port_index, out_port_index;
384 #ifdef TIZEN_FEATURE_OMX
385   guint32 in_port_usebuffer, out_port_usebuffer;
386 #endif
387
388   guint64 hacks;
389
390   GstOmxComponentType type;
391 };
392
393 GKeyFile *        gst_omx_get_configuration (void);
394
395 const gchar *     gst_omx_error_to_string (OMX_ERRORTYPE err);
396 const gchar *     gst_omx_state_to_string (OMX_STATETYPE state);
397 const gchar *     gst_omx_command_to_string (OMX_COMMANDTYPE cmd);
398
399 guint64           gst_omx_parse_hacks (gchar ** hacks);
400
401 GstOMXCore *      gst_omx_core_acquire (const gchar * filename);
402 void              gst_omx_core_release (GstOMXCore * core);
403
404
405 GstOMXComponent * gst_omx_component_new (GstObject * parent, const gchar *core_name, const gchar *component_name, const gchar * component_role, guint64 hacks);
406 void              gst_omx_component_free (GstOMXComponent * comp);
407
408 OMX_ERRORTYPE     gst_omx_component_set_state (GstOMXComponent * comp, OMX_STATETYPE state);
409 OMX_STATETYPE     gst_omx_component_get_state (GstOMXComponent * comp, GstClockTime timeout);
410
411 OMX_ERRORTYPE     gst_omx_component_get_last_error (GstOMXComponent * comp);
412 const gchar *     gst_omx_component_get_last_error_string (GstOMXComponent * comp);
413
414 GstOMXPort *      gst_omx_component_add_port (GstOMXComponent * comp, guint32 index);
415 GstOMXPort *      gst_omx_component_get_port (GstOMXComponent * comp, guint32 index);
416
417 OMX_ERRORTYPE     gst_omx_component_get_parameter (GstOMXComponent * comp, OMX_INDEXTYPE index, gpointer param);
418 OMX_ERRORTYPE     gst_omx_component_set_parameter (GstOMXComponent * comp, OMX_INDEXTYPE index, gpointer param);
419
420 OMX_ERRORTYPE     gst_omx_component_get_config (GstOMXComponent * comp, OMX_INDEXTYPE index, gpointer config);
421 OMX_ERRORTYPE     gst_omx_component_set_config (GstOMXComponent * comp, OMX_INDEXTYPE index, gpointer config);
422
423 OMX_ERRORTYPE     gst_omx_setup_tunnel (GstOMXPort * port1, GstOMXPort * port2);
424 OMX_ERRORTYPE     gst_omx_close_tunnel (GstOMXPort * port1, GstOMXPort * port2);
425
426
427 OMX_ERRORTYPE     gst_omx_port_get_port_definition (GstOMXPort * port, OMX_PARAM_PORTDEFINITIONTYPE * port_def);
428 OMX_ERRORTYPE     gst_omx_port_update_port_definition (GstOMXPort *port, OMX_PARAM_PORTDEFINITIONTYPE *port_definition);
429
430 GstOMXAcquireBufferReturn gst_omx_port_acquire_buffer (GstOMXPort *port, GstOMXBuffer **buf);
431 OMX_ERRORTYPE     gst_omx_port_release_buffer (GstOMXPort *port, GstOMXBuffer *buf);
432
433 OMX_ERRORTYPE     gst_omx_port_set_flushing (GstOMXPort *port, GstClockTime timeout, gboolean flush);
434 gboolean          gst_omx_port_is_flushing (GstOMXPort *port);
435
436 OMX_ERRORTYPE     gst_omx_port_allocate_buffers (GstOMXPort *port);
437 #ifdef TIZEN_FEATURE_OMX
438 OMX_ERRORTYPE     gst_omx_port_tbm_allocate_dec_buffers (GstOMXPort * port, tbm_bufmgr  bufmgr, gboolean use_buffer);
439 OMX_ERRORTYPE     gst_omx_port_tbm_allocate_enc_buffers (GstOMXPort * port, tbm_bufmgr  bufmgr, gboolean use_buffer);
440 OMX_ERRORTYPE     gst_omx_component_get_extension_index (GstOMXComponent * comp, OMX_STRING name, gpointer index);
441 void              gst_omx_port_flush_start (GstOMXPort * port, gboolean flush);
442 #endif
443 OMX_ERRORTYPE     gst_omx_port_use_buffers (GstOMXPort *port, const GList *buffers);
444 OMX_ERRORTYPE     gst_omx_port_use_eglimages (GstOMXPort *port, const GList *images);
445 OMX_ERRORTYPE     gst_omx_port_deallocate_buffers (GstOMXPort *port);
446 OMX_ERRORTYPE     gst_omx_port_populate (GstOMXPort *port);
447 OMX_ERRORTYPE     gst_omx_port_wait_buffers_released (GstOMXPort * port, GstClockTime timeout);
448
449 OMX_ERRORTYPE     gst_omx_port_mark_reconfigured (GstOMXPort * port);
450
451 OMX_ERRORTYPE     gst_omx_port_set_enabled (GstOMXPort * port, gboolean enabled);
452 OMX_ERRORTYPE     gst_omx_port_wait_enabled (GstOMXPort * port, GstClockTime timeout);
453 gboolean          gst_omx_port_is_enabled (GstOMXPort * port);
454
455
456 void              gst_omx_set_default_role (GstOMXClassData *class_data, const gchar *default_role);
457
458 /* refered by plugin_init */
459 GST_DEBUG_CATEGORY_EXTERN (gst_omx_video_debug_category);
460
461 G_END_DECLS
462
463 #endif /* __GST_OMX_H__ */