enabling omx decoder
[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 #include <OMX_Core.h>
50 #include <OMX_Component.h>
51
52 #ifdef USE_OMX_TARGET_EXYNOS
53 #include <tbm_type.h>
54 #include <tbm_surface.h>
55 #include <tbm_bufmgr.h>
56 #endif
57
58 #ifdef USE_OMX_TARGET_RPI
59 #include <OMX_Broadcom.h>
60 #endif
61
62 #ifdef HAVE_VIDEO_EXT
63 #include <OMX_VideoExt.h>
64 #endif
65
66 #ifdef GST_OMX_STRUCT_PACKING
67 #pragma pack()
68 #endif
69
70 G_BEGIN_DECLS
71
72 #define GST_OMX_INIT_STRUCT(st) G_STMT_START { \
73   memset ((st), 0, sizeof (*(st))); \
74   (st)->nSize = sizeof (*(st)); \
75   (st)->nVersion.s.nVersionMajor = OMX_VERSION_MAJOR; \
76   (st)->nVersion.s.nVersionMinor = OMX_VERSION_MINOR; \
77   (st)->nVersion.s.nRevision = OMX_VERSION_REVISION; \
78   (st)->nVersion.s.nStep = OMX_VERSION_STEP; \
79 } G_STMT_END
80
81 /* Different hacks that are required to work around
82  * bugs in different OpenMAX implementations
83  */
84 /* In the EventSettingsChanged callback use nData2 instead of nData1 for
85  * the port index. Happens with Bellagio.
86  */
87 #define GST_OMX_HACK_EVENT_PORT_SETTINGS_CHANGED_NDATA_PARAMETER_SWAP G_GUINT64_CONSTANT (0x0000000000000001)
88 /* In the EventSettingsChanged callback assume that port index 0 really
89  * means port index 1. Happens with the Bellagio ffmpegdist video decoder.
90  */
91 #define GST_OMX_HACK_EVENT_PORT_SETTINGS_CHANGED_PORT_0_TO_1          G_GUINT64_CONSTANT (0x0000000000000002)
92 /* If the video framerate is not specified as fraction (Q.16) but as
93  * integer number. Happens with the Bellagio ffmpegdist video encoder.
94  */
95 #define GST_OMX_HACK_VIDEO_FRAMERATE_INTEGER                          G_GUINT64_CONSTANT (0x0000000000000004)
96 /* If the SYNCFRAME flag on encoder output buffers is not used and we
97  * have to assume that all frames are sync frames.
98  * Happens with the Bellagio ffmpegdist video encoder.
99  */
100 #define GST_OMX_HACK_SYNCFRAME_FLAG_NOT_USED                          G_GUINT64_CONSTANT (0x0000000000000008)
101 /* If the component needs to be re-created if the caps change.
102  * Happens with Qualcomm's OpenMAX implementation.
103  */
104 #define GST_OMX_HACK_NO_COMPONENT_RECONFIGURE                         G_GUINT64_CONSTANT (0x0000000000000010)
105
106 /* If the component does not accept empty EOS buffers.
107  * Happens with Qualcomm's OpenMAX implementation.
108  */
109 #define GST_OMX_HACK_NO_EMPTY_EOS_BUFFER                              G_GUINT64_CONSTANT (0x0000000000000020)
110
111 /* If the component might not acknowledge a drain.
112  * Happens with TI's Ducati OpenMAX implementation.
113  */
114 #define GST_OMX_HACK_DRAIN_MAY_NOT_RETURN                             G_GUINT64_CONSTANT (0x0000000000000040)
115
116 /* If the component doesn't allow any component role to be set.
117  * Happens with Broadcom's OpenMAX implementation.
118  */
119 #define GST_OMX_HACK_NO_COMPONENT_ROLE                                G_GUINT64_CONSTANT (0x0000000000000080)
120
121 /* If the component doesn't allow disabling the outport while
122  * when setting the format until the output format is known.
123  */
124 #define GST_OMX_HACK_NO_DISABLE_OUTPORT                               G_GUINT64_CONSTANT (0x0000000000000100)
125
126 typedef struct _GstOMXCore GstOMXCore;
127 typedef struct _GstOMXPort GstOMXPort;
128 typedef enum _GstOMXPortDirection GstOMXPortDirection;
129 typedef struct _GstOMXComponent GstOMXComponent;
130 typedef struct _GstOMXBuffer GstOMXBuffer;
131 typedef struct _GstOMXClassData GstOMXClassData;
132 typedef struct _GstOMXMessage GstOMXMessage;
133
134 /* MODIFICATION */
135 typedef enum GOmxVendor GOmxVendor; /* check omx vender */
136
137 #ifdef GST_TIZEN_MODIFICATION
138
139 #define MFC_INPUT_BUFFER_PLANE      1
140 #define MFC_OUTPUT_BUFFER_PLANE     2
141
142 #define MAX_INPUT_BUFFER            16
143 #define MAX_OUTPUT_BUFFER           16
144
145 typedef struct _TBMBuffer TBMBuffer;
146 typedef struct _TBMInputBuffer TBMInputBuffer;
147 typedef struct _TBMOutputBuffer TBMOutputBuffer;
148
149 struct _TBMBuffer
150 {
151    OMX_U32 mBufFD;
152    tbm_bo  mBo;
153    OMX_PTR mPtr;
154    OMX_U32 nAllocLen;
155 };
156
157 struct _TBMInputBuffer
158 {
159     struct _TBMBuffer tbmBuffer[MAX_INPUT_BUFFER];
160     OMX_U32 allocatedCount;
161     GList *buffers;
162 };
163
164 struct _TBMOutputBuffer
165 {
166     MMVideoBuffer *tbmBuffer[MAX_OUTPUT_BUFFER];
167     OMX_U32 allocatedCount;
168     GList *buffers;
169 };
170
171 typedef struct _EnableGemBuffersParams EnableGemBuffersParams;
172
173 struct _EnableGemBuffersParams
174 {
175   OMX_U32 nSize;
176   OMX_VERSIONTYPE nVersion;
177   OMX_U32 nPortIndex;
178   OMX_BOOL enable;
179 };
180 #endif
181
182 enum
183 {
184     BUF_SHARE_METHOD_PADDR = 0,
185     BUF_SHARE_METHOD_FD = 1,
186     BUF_SHARE_METHOD_TIZEN_BUFFER = 2,
187     BUF_SHARE_METHOD_FLUSH_BUFFER = 3,
188 }; /* buf_share_method */
189
190 /* Extended color formats */
191 enum {
192     OMX_EXT_COLOR_FormatNV12TPhysicalAddress = 0x7F000001, /**< Reserved region for introducing Vendor Extensions */
193     OMX_EXT_COLOR_FormatNV12LPhysicalAddress = 0x7F000002,
194     OMX_EXT_COLOR_FormatNV12Tiled = 0x7FC00002,
195     OMX_EXT_COLOR_FormatNV12TFdValue = 0x7F000012,
196     OMX_EXT_COLOR_FormatNV12LFdValue = 0x7F000013
197 };
198
199 #ifdef GST_TIZEN_MODIFICATION
200 /* Extended port settings. */
201 enum {
202     OMX_IndexParamEnablePlatformSpecificBuffers = 0x7F000011
203 };
204 #endif
205
206 /* modification: Add_component_vendor */
207 enum GOmxVendor
208 {
209     GOMX_VENDOR_DEFAULT,
210     GOMX_VENDOR_SLSI_SEC,
211     GOMX_VENDOR_SLSI_EXYNOS,
212     GOMX_VENDOR_QCT,
213     GOMX_VENDOR_SPRD
214 };
215
216 typedef enum {
217   /* Everything good and the buffer is valid */
218   GST_OMX_ACQUIRE_BUFFER_OK = 0,
219   /* The port is flushing, exit ASAP */
220   GST_OMX_ACQUIRE_BUFFER_FLUSHING,
221   /* The port must be reconfigured */
222   GST_OMX_ACQUIRE_BUFFER_RECONFIGURE,
223   /* The port is EOS */
224   GST_OMX_ACQUIRE_BUFFER_EOS,
225   /* A fatal error happened */
226   GST_OMX_ACQUIRE_BUFFER_ERROR
227 } GstOMXAcquireBufferReturn;
228
229 struct _GstOMXCore {
230   /* Handle to the OpenMAX IL core shared library */
231   GModule *module;
232
233   /* Current number of users, transitions from/to 0
234    * call init/deinit */
235   GMutex lock;
236   gint user_count; /* LOCK */
237
238   /* MODIFICATION */
239   GOmxVendor component_vendor; /* to check omx vender */
240   gboolean secure; /* trust zone */
241
242   /* OpenMAX core library functions, protected with LOCK */
243   OMX_ERRORTYPE (*init) (void);
244   OMX_ERRORTYPE (*deinit) (void);
245   OMX_ERRORTYPE (*get_handle) (OMX_HANDLETYPE * handle,
246       OMX_STRING name, OMX_PTR data, OMX_CALLBACKTYPE * callbacks);
247   OMX_ERRORTYPE (*free_handle) (OMX_HANDLETYPE handle);
248   OMX_ERRORTYPE (*setup_tunnel) (OMX_HANDLETYPE output, OMX_U32 outport, OMX_HANDLETYPE input, OMX_U32 inport);
249 };
250
251 typedef enum {
252   GST_OMX_MESSAGE_STATE_SET,
253   GST_OMX_MESSAGE_FLUSH,
254   GST_OMX_MESSAGE_ERROR,
255   GST_OMX_MESSAGE_PORT_ENABLE,
256   GST_OMX_MESSAGE_PORT_SETTINGS_CHANGED,
257   GST_OMX_MESSAGE_BUFFER_FLAG,
258   GST_OMX_MESSAGE_BUFFER_DONE,
259 } GstOMXMessageType;
260
261 typedef enum {
262   GST_OMX_COMPONENT_TYPE_SINK,
263   GST_OMX_COMPONENT_TYPE_SOURCE,
264   GST_OMX_COMPONENT_TYPE_FILTER
265 } GstOmxComponentType;
266
267 struct _GstOMXMessage {
268   GstOMXMessageType type;
269
270   union {
271     struct {
272       OMX_STATETYPE state;
273     } state_set;
274     struct {
275       OMX_U32 port;
276     } flush;
277     struct {
278       OMX_ERRORTYPE error;
279     } error;
280     struct {
281       OMX_U32 port;
282       OMX_BOOL enable;
283     } port_enable;
284     struct {
285       OMX_U32 port;
286     } port_settings_changed;
287     struct {
288       OMX_U32 port;
289       OMX_U32 flags;
290     } buffer_flag;
291     struct {
292       OMX_HANDLETYPE component;
293       OMX_PTR app_data;
294       OMX_BUFFERHEADERTYPE *buffer;
295       OMX_BOOL empty;
296     } buffer_done;
297   } content;
298 };
299
300 struct _GstOMXPort {
301   GstOMXComponent *comp;
302   guint32 index;
303
304   gboolean tunneled;
305
306   OMX_PARAM_PORTDEFINITIONTYPE port_def;
307   GPtrArray *buffers; /* Contains GstOMXBuffer* */
308   GQueue pending_buffers; /* Contains GstOMXBuffer* */
309   gboolean flushing;
310   gboolean flushed; /* TRUE after OMX_CommandFlush was done */
311   gboolean enabled_pending;  /* TRUE after OMX_Command{En,Dis}able */
312   gboolean disabled_pending; /* was done until it took effect */
313   gboolean eos; /* TRUE after a buffer with EOS flag was received */
314
315   /* Increased whenever the settings of these port change.
316    * If settings_cookie != configured_settings_cookie
317    * the port has to be reconfigured.
318    */
319   gint settings_cookie;
320   gint configured_settings_cookie;
321 };
322
323 struct _GstOMXComponent {
324   GstObject *parent;
325
326   gchar *name; /* for debugging mostly */
327
328   OMX_HANDLETYPE handle;
329   GstOMXCore *core;
330
331   guint64 hacks; /* Flags, GST_OMX_HACK_* */
332
333   /* Added once, never changed. No locks necessary */
334   GPtrArray *ports; /* Contains GstOMXPort* */
335   gint n_in_ports, n_out_ports;
336
337   /* Locking order: lock -> messages_lock
338    *
339    * Never hold lock while waiting for messages_cond
340    * Always check that messages is empty before waiting */
341   GMutex lock;
342
343   GQueue messages; /* Queue of GstOMXMessages */
344   GMutex messages_lock;
345   GCond messages_cond;
346
347   OMX_STATETYPE state;
348   /* OMX_StateInvalid if no pending state */
349   OMX_STATETYPE pending_state;
350   /* OMX_ErrorNone usually, if different nothing will work */
351   OMX_ERRORTYPE last_error;
352
353   GList *pending_reconfigure_outports;
354 };
355
356 struct _GstOMXBuffer {
357   GstOMXPort *port;
358   OMX_BUFFERHEADERTYPE *omx_buf;
359
360   /* TRUE if the buffer is used by the port, i.e.
361    * between {Empty,Fill}ThisBuffer and the callback
362    */
363   gboolean used;
364
365   /* Cookie of the settings when this buffer was allocated */
366   gint settings_cookie;
367
368   /* TRUE if this is an EGLImage */
369   gboolean eglimage;
370
371 #ifdef GST_TIZEN_MODIFICATION
372   /* MMVideoBuffer array to use TBM buffers */
373    MMVideoBuffer *mm_vbuffer;
374 #endif
375 };
376
377 struct _GstOMXClassData {
378   const gchar *core_name;
379   const gchar *component_name;
380   const gchar *component_role;
381
382   const gchar *default_src_template_caps;
383   const gchar *default_sink_template_caps;
384
385   guint32 in_port_index, out_port_index;
386
387   guint64 hacks;
388
389   GstOmxComponentType type;
390 };
391
392 GKeyFile *        gst_omx_get_configuration (void);
393
394 const gchar *     gst_omx_error_to_string (OMX_ERRORTYPE err);
395 const gchar *     gst_omx_state_to_string (OMX_STATETYPE state);
396 const gchar *     gst_omx_command_to_string (OMX_COMMANDTYPE cmd);
397
398 guint64           gst_omx_parse_hacks (gchar ** hacks);
399
400 GstOMXCore *      gst_omx_core_acquire (const gchar * filename);
401 void              gst_omx_core_release (GstOMXCore * core);
402
403
404 GstOMXComponent * gst_omx_component_new (GstObject * parent, const gchar *core_name, const gchar *component_name, const gchar * component_role, guint64 hacks);
405 void              gst_omx_component_free (GstOMXComponent * comp);
406
407 OMX_ERRORTYPE     gst_omx_component_set_state (GstOMXComponent * comp, OMX_STATETYPE state);
408 OMX_STATETYPE     gst_omx_component_get_state (GstOMXComponent * comp, GstClockTime timeout);
409
410 OMX_ERRORTYPE     gst_omx_component_get_last_error (GstOMXComponent * comp);
411 const gchar *     gst_omx_component_get_last_error_string (GstOMXComponent * comp);
412
413 GstOMXPort *      gst_omx_component_add_port (GstOMXComponent * comp, guint32 index);
414 GstOMXPort *      gst_omx_component_get_port (GstOMXComponent * comp, guint32 index);
415
416 OMX_ERRORTYPE     gst_omx_component_get_parameter (GstOMXComponent * comp, OMX_INDEXTYPE index, gpointer param);
417 OMX_ERRORTYPE     gst_omx_component_set_parameter (GstOMXComponent * comp, OMX_INDEXTYPE index, gpointer param);
418
419 OMX_ERRORTYPE     gst_omx_component_get_config (GstOMXComponent * comp, OMX_INDEXTYPE index, gpointer config);
420 OMX_ERRORTYPE     gst_omx_component_set_config (GstOMXComponent * comp, OMX_INDEXTYPE index, gpointer config);
421
422 OMX_ERRORTYPE     gst_omx_setup_tunnel (GstOMXPort * port1, GstOMXPort * port2);
423 OMX_ERRORTYPE     gst_omx_close_tunnel (GstOMXPort * port1, GstOMXPort * port2);
424
425
426 OMX_ERRORTYPE     gst_omx_port_get_port_definition (GstOMXPort * port, OMX_PARAM_PORTDEFINITIONTYPE * port_def);
427 OMX_ERRORTYPE     gst_omx_port_update_port_definition (GstOMXPort *port, OMX_PARAM_PORTDEFINITIONTYPE *port_definition);
428
429 GstOMXAcquireBufferReturn gst_omx_port_acquire_buffer (GstOMXPort *port, GstOMXBuffer **buf);
430 OMX_ERRORTYPE     gst_omx_port_release_buffer (GstOMXPort *port, GstOMXBuffer *buf);
431
432 OMX_ERRORTYPE     gst_omx_port_set_flushing (GstOMXPort *port, GstClockTime timeout, gboolean flush);
433 gboolean          gst_omx_port_is_flushing (GstOMXPort *port);
434
435 OMX_ERRORTYPE     gst_omx_port_allocate_buffers (GstOMXPort *port);
436 #ifdef GST_TIZEN_MODIFICATION
437 OMX_ERRORTYPE     gst_omx_port_tbm_allocate_dec_buffers (GstOMXPort * port, tbm_bufmgr  bufMgr, int eCompressionFormat);
438 OMX_ERRORTYPE     gst_omx_port_tbm_allocate_enc_buffers (GstOMXPort * port, tbm_bufmgr  bufMgr, int eCompressionFormat);
439 #endif
440 OMX_ERRORTYPE     gst_omx_port_use_buffers (GstOMXPort *port, const GList *buffers);
441 OMX_ERRORTYPE     gst_omx_port_use_eglimages (GstOMXPort *port, const GList *images);
442 OMX_ERRORTYPE     gst_omx_port_deallocate_buffers (GstOMXPort *port);
443 OMX_ERRORTYPE     gst_omx_port_populate (GstOMXPort *port);
444 OMX_ERRORTYPE     gst_omx_port_wait_buffers_released (GstOMXPort * port, GstClockTime timeout);
445
446 OMX_ERRORTYPE     gst_omx_port_mark_reconfigured (GstOMXPort * port);
447
448 OMX_ERRORTYPE     gst_omx_port_set_enabled (GstOMXPort * port, gboolean enabled);
449 OMX_ERRORTYPE     gst_omx_port_wait_enabled (GstOMXPort * port, GstClockTime timeout);
450 gboolean          gst_omx_port_is_enabled (GstOMXPort * port);
451
452
453 void              gst_omx_set_default_role (GstOMXClassData *class_data, const gchar *default_role);
454
455 #ifdef GST_TIZEN_MODIFICATION
456
457 /*MFC Buffer alignment macros*/
458 #define S5P_FIMV_DEC_BUF_ALIGN                  (8 * 1024)
459 #define S5P_FIMV_ENC_BUF_ALIGN                  (8 * 1024)
460 #define S5P_FIMV_NV12M_HALIGN                   16
461 #define S5P_FIMV_NV12M_LVALIGN                  16
462 #define S5P_FIMV_NV12M_CVALIGN                  8
463 #define S5P_FIMV_NV12MT_HALIGN                  128
464 #define S5P_FIMV_NV12MT_VALIGN                  64
465 #define S5P_FIMV_NV12M_SALIGN                   2048
466 #define S5P_FIMV_NV12MT_SALIGN                  8192
467
468 #define ALIGN(x, a)       (((x) + (a) - 1) & ~((a) - 1))
469
470 /* Buffer alignment defines */
471 #define SZ_1M                                   0x00100000
472 #define S5P_FIMV_D_ALIGN_PLANE_SIZE             64
473
474 #define S5P_FIMV_MAX_FRAME_SIZE                 (2 * SZ_1M)
475 #define S5P_FIMV_NUM_PIXELS_IN_MB_ROW           16
476 #define S5P_FIMV_NUM_PIXELS_IN_MB_COL           16
477
478 /* Macro */
479 #define ALIGN_TO_4KB(x)   ((((x) + (1 << 12) - 1) >> 12) << 12)
480 #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
481 #define CHOOSE_MAX_SIZE(a,b) ((a) > (b) ? (a) : (b))
482
483 int new_calc_plane(int width, int height);
484 int new_calc_yplane(int width, int height);
485 int new_calc_uvplane(int width, int height);
486
487 int calc_plane(int width, int height);
488 int calc_yplane(int width, int height);
489 int calc_uvplane(int width, int height);
490 int gst_omx_calculate_y_size(int compressionFormat, int width, int height);
491 int gst_omx_calculate_uv_size(int compressionFormat, int width, int height);
492
493 tbm_bo            gst_omx_tbm_allocate_bo(tbm_bufmgr hBufmgr, int size);
494 void              gst_omx_tbm_deallocate_bo(tbm_bo bo);
495 OMX_U32           gst_omx_tbm_get_bo_fd(tbm_bo bo);
496 OMX_PTR           gst_omx_tbm_get_bo_ptr(tbm_bo bo);
497
498 #endif
499 /* refered by plugin_init */
500 GST_DEBUG_CATEGORY_EXTERN (gst_omx_video_debug_category);
501
502 G_END_DECLS
503
504 #endif /* __GST_OMX_H__ */