3da69a8a7a5e34dd6e6ab625f22053151d02779d
[platform/core/api/mediacodec.git] / include / media_codec_port_gst.h
1
2 /*
3 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 #ifndef __TIZEN_MEDIA_CODEC_PORT_GST_H__
19 #define __TIZEN_MEDIA_CODEC_PORT_GST_H__
20
21 #include <unistd.h>
22 #include <tizen.h>
23 #include <media_codec.h>
24 #include <media_codec_private.h>
25 #include <media_codec_port.h>
26 #include <media_codec_bitstream.h>
27
28 #include <tbm_type.h>
29 #include <tbm_surface.h>
30 #include <tbm_bufmgr.h>
31 #include <tbm_surface_internal.h>
32 #include <gst/video/video-format.h>
33
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37
38 #define GST_INIT_STRUCTURE(param) \
39         memset(&(param), 0, sizeof(param));
40
41 #define MEDIACODEC_ELEMENT_SET_STATE(x_element, x_state)                                          \
42         do {                                                                                            \
43                 LOGD("setting state [%s:%d] to [%s]\n", #x_state, x_state, GST_ELEMENT_NAME(x_element)); \
44                 if (GST_STATE_CHANGE_FAILURE == gst_element_set_state(x_element, x_state)) {                    \
45                         LOGE("failed to set state %s to %s\n", #x_state, GST_ELEMENT_NAME(x_element));        \
46                         goto STATE_CHANGE_FAILED;                                                               \
47                 }                                                                                           \
48         } while (0)
49
50 #define SCMN_IMGB_MAX_PLANE 4
51 #define TBM_API_CHANGE
52 #define DEFAULT_POOL_SIZE 13
53 #define AAC_CODECDATA_SIZE      16
54 #define WMA_CODECDATA_SIZE      64
55 #define VORBIS_CODECDATA_SIZE   4096
56 #define MAXINUM_QNUM    15
57 #define THRESHOLD_QNUM  5
58
59 /* gst port layer */
60 typedef struct _mc_gst_port_t mc_gst_port_t;
61 typedef struct _mc_gst_core_t mc_gst_core_t;
62 typedef struct _GstMCBuffer GstMCBuffer;
63
64 typedef enum {
65         BUF_SHARE_METHOD_PADDR = 0,
66         BUF_SHARE_METHOD_FD,
67         BUF_SHARE_METHOD_TIZEN_BUFFER,
68         BUF_SHARE_METHOD_FLUSH_BUFFER
69 } buf_share_method_t;
70
71 #ifdef TIZEN_PROFILE_LITE
72 struct ion_mmu_data {
73         int master_id;
74         int fd_buffer;
75         unsigned long iova_addr;
76         size_t iova_size;
77 };
78 #endif
79
80 struct _mc_gst_port_t {
81         mc_gst_core_t *core;
82         unsigned int num_buffers;
83         unsigned int buffer_size;
84         unsigned int index;
85         bool is_allocated;
86         media_packet_h *buffers;
87         GQueue *queue;
88         GMutex mutex;
89         GCond buffer_cond;
90 };
91
92 struct _mc_gst_core_t {
93         int(**vtable)();
94         const char *mime;
95         gchar *format;
96         GstElement *pipeline;
97         GstElement *appsrc;
98         GstElement *capsfilter;
99         GstElement *parser;
100         GstElement *fakesink;
101         GstElement *codec;
102         GstCaps *caps;
103         tbm_bufmgr bufmgr;
104         int drm_fd;
105
106         GMainContext *thread_default;
107         gulong signal_handoff;
108         gint bus_whatch_id;
109         gint probe_id;
110
111         GMutex eos_mutex;
112         GMutex eos_wait_mutex;
113         GMutex prepare_lock;
114         GMutex drain_lock;
115         GCond buffer_cond;
116         GCond out_buffer_cond;
117         GCond eos_cond;
118         GCond eos_waiting_cond;
119
120         bool output_allocated;
121         bool encoder;
122         bool video;
123         bool is_hw;
124         bool eos;
125         bool eos_waiting;
126         bool codec_config;
127         bool need_feed;
128         bool need_codec_data;
129         bool need_sync_flag;
130         bool unprepare_flag;
131         unsigned int prepare_count;
132         unsigned int num_live_buffers;
133         unsigned int etb_count;
134         unsigned int ftb_count;
135
136         mediacodec_codec_type_e codec_id;
137         media_format_mimetype_e out_mime;
138         media_format_h output_fmt;
139         mc_gst_port_t *ports[2];
140         mc_bitstream_t bits;
141
142         mc_aqueue_t *available_queue;
143         GQueue *output_queue;
144
145         void *codec_info;
146
147         void* user_cb[_MEDIACODEC_EVENT_TYPE_NUM];
148         void* user_data[_MEDIACODEC_EVENT_TYPE_NUM];
149 };
150
151 struct _GstMCBuffer {
152         GstBuffer *buffer;
153         int buf_size;
154         mc_gst_core_t* core;
155         media_packet_h pkt;
156         bool has_imgb;
157 };
158
159 enum { fill_inbuf, fill_outbuf };
160
161 int __mc_fill_input_buffer(mc_gst_core_t *core, media_packet_h pkt, GstMCBuffer *buff);
162 int __mc_fill_output_buffer(mc_gst_core_t *core, void *data, int size, media_packet_h *out_pkt);
163
164 int __mc_fill_input_buffer_with_packet(mc_gst_core_t *core, media_packet_h pkt, GstMCBuffer *buff);
165 int __mc_fill_input_buffer_with_venc_packet(mc_gst_core_t *core, media_packet_h pkt, GstMCBuffer *mc_buffer);
166
167 int __mc_fill_packet_with_output_buffer(mc_gst_core_t *core, void *data, int size, media_packet_h *out_pkt);
168 int __mc_fill_venc_packet_with_output_buffer(mc_gst_core_t *core, void *data, int size, media_packet_h *out_pkt);
169 int __mc_fill_vdec_packet_with_output_buffer(mc_gst_core_t *core, void *data, int size, media_packet_h *pkt);
170
171 mc_gst_core_t *mc_gst_core_new();
172 void mc_gst_core_free(mc_gst_core_t *core);
173
174 mc_gst_port_t *mc_gst_port_new(mc_gst_core_t *core);
175 void mc_gst_port_free(mc_gst_port_t *port);
176
177 mc_ret_e mc_gst_prepare(mc_handle_t *mc_handle);
178 mc_ret_e mc_gst_unprepare(mc_handle_t *mc_handle);
179
180 mc_ret_e mc_gst_process_input(mc_handle_t *mc_handle, media_packet_h inbuf, uint64_t timeOutUs);
181 mc_ret_e mc_gst_get_output(mc_handle_t *mc_handle, media_packet_h *outbuf, uint64_t timeOutUs);
182
183 mc_ret_e mc_gst_flush_buffers(mc_handle_t *mc_handle);
184
185 mc_ret_e mc_gst_get_packet_pool(mc_handle_t *mc_handle, media_packet_pool_h *pkt_pool);
186
187 #ifdef __cplusplus
188 }
189 #endif
190
191 #endif /* __TIZEN_MEDIA_CODEC_PORT_GST_H__ */