18010d4d394b17552bd5ee266175473daaafd660
[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 #include <gst/allocators/gsttizenmemory.h>
34
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38
39 #define GST_INIT_STRUCTURE(param) \
40         memset(&(param), 0, sizeof(param));
41
42 #define MEDIACODEC_ELEMENT_SET_STATE(x_element, x_state)                                          \
43         do {                                                                                            \
44                 LOGD("setting state [%s:%d] to [%s]\n", #x_state, x_state, GST_ELEMENT_NAME(x_element)); \
45                 if (GST_STATE_CHANGE_FAILURE == gst_element_set_state(x_element, x_state)) {                    \
46                         LOGE("failed to set state %s to %s\n", #x_state, GST_ELEMENT_NAME(x_element));        \
47                         goto STATE_CHANGE_FAILED;                                                               \
48                 }                                                                                           \
49         } while (0)
50
51 #define SCMN_IMGB_MAX_PLANE 4
52 #define TBM_API_CHANGE
53 #define DEFAULT_DEC_POOL_SIZE 20
54 #define DEFAULT_ENC_POOL_SIZE 13
55 #define CODECDATA_NOT_USE       0x7FFFFFFF
56 #define AAC_CODECDATA_SIZE      16
57 #define WMA_CODECDATA_SIZE      64
58 #define VORBIS_CODECDATA_SIZE   4096
59 #define MAXINUM_QNUM    20
60 #define THRESHOLD_QNUM  5
61
62 /* gst port layer */
63 typedef struct _mc_gst_port_def_t mc_gst_port_def_t;
64 typedef struct _mc_gst_port_t mc_gst_port_t;
65 typedef struct _mc_gst_core_t mc_gst_core_t;
66 typedef struct _GstMCBuffer GstMCBuffer;
67
68
69 typedef enum {
70         in_port_index = 0,
71         out_port_index
72 } port_index_e;
73
74
75 struct _mc_gst_port_def_t {
76         gchar *mime_type;
77         media_format_mimetype_e coding_type;
78         union {
79                 mc_audio_port_definition_t audio;
80                 mc_video_port_definition_t video;
81         } info;
82 };
83
84 struct _mc_gst_port_t {
85         mc_gst_core_t *core;
86         guint num_buffers;
87         guint buffer_size;
88         guint index;
89         gboolean is_allocated;
90         GQueue *queue;
91         GMutex mutex;
92         GCond buffer_cond;
93         mc_gst_port_def_t port_def;
94 };
95
96 struct _mc_gst_core_t {
97         gint(**vtable)();
98         const gchar *mime;
99         GstElement *pipeline;
100         GstElement *appsrc;
101         GstElement *audioconvert;
102         GstElement *audioresample;
103         GstElement *videoscale;
104         GstElement *videoconvert;
105         GstElement *capsfilter;
106         GstElement *parser;
107         GstElement *fakesink;
108         GstElement *codec;
109         GstCaps *caps;
110         tbm_bufmgr bufmgr;
111         gint drm_fd;
112
113         GMainContext *thread_default;
114         gulong signal_handoff;
115         gint bus_watch_id;
116         gint probe_id;
117
118         GMutex eos_mutex;
119         GMutex eos_wait_mutex;
120         GMutex prepare_lock;
121         GMutex drain_lock;
122         GCond buffer_cond;
123         GCond out_buffer_cond;
124         GCond eos_cond;
125         GCond eos_waiting_cond;
126
127         gboolean output_allocated;
128         gboolean encoder;
129         gboolean video;
130         gboolean is_hw;
131         gboolean eos;
132         gboolean eos_waiting;
133         gboolean codec_config;
134         gboolean need_drain;
135         gboolean need_feed;
136         gboolean need_codec_data;
137         gboolean need_sync_flag;
138         gboolean unprepare_flag;
139         guint prepare_count;
140         guint num_live_buffers;
141         guint etb_count;
142         gint64 offset;
143
144         mediacodec_codec_type_e codec_id;
145         media_format_mimetype_e out_mime;
146         media_format_h output_fmt;
147         mc_gst_port_t *ports[2];
148         mc_bitstream_t bits;
149
150         mc_aqueue_t *available_queue;
151         GQueue *output_queue;
152
153         GstBuffer *codec_data;
154         GstCaps* (*mc_caps_new)(mc_gst_core_t *, mediacodec_codec_type_e, gint);
155         GstAllocator *allocator;
156
157         void* user_cb[_MEDIACODEC_EVENT_TYPE_NUM];
158         void* user_data[_MEDIACODEC_EVENT_TYPE_NUM];
159 };
160
161 struct _GstMCBuffer {
162         GstBuffer *buffer;
163         gint buf_size;
164         mc_gst_core_t* core;
165         media_packet_h packet;
166         void *ext_mem;
167         gboolean has_tbm;
168 };
169
170 enum { fill_inbuf, fill_outbuf };
171
172 int __mc_fill_input_buffer(mc_gst_core_t *core, media_packet_h packet, GstMCBuffer *mcbuffer);
173 int __mc_fill_output_buffer(mc_gst_core_t *core, void *data, int size, GstMCBuffer *mcbuffer);
174
175 int __mc_fill_input_buffer_with_packet(mc_gst_core_t *core, media_packet_h packet, GstMCBuffer *mcbuffer);
176 int __mc_fill_input_buffer_with_adec_packet(mc_gst_core_t *core, media_packet_h packet, GstMCBuffer *mcbuffer);
177 int __mc_fill_input_buffer_with_venc_packet(mc_gst_core_t *core, media_packet_h packet, GstMCBuffer *mcbuffer);
178
179 int __mc_fill_packet_with_output_buffer(mc_gst_core_t *core, void *data, int size, GstMCBuffer *mcbuffer);
180 int __mc_fill_aenc_packet_with_output_buffer(mc_gst_core_t *core, void *data, int size, GstMCBuffer *mcbuffer);
181 int __mc_fill_venc_packet_with_output_buffer(mc_gst_core_t *core, void *data, int size, GstMCBuffer *mcbuffer);
182 int __mc_fill_vdec_packet_with_output_buffer(mc_gst_core_t *core, void *data, int size, GstMCBuffer *mcbuffer);
183
184 mc_gst_core_t *mc_gst_core_new();
185 void mc_gst_core_free(mc_gst_core_t *core);
186
187 mc_gst_port_t *mc_gst_port_new(mc_gst_core_t *core);
188 void mc_gst_port_free(mc_gst_port_t *port);
189
190 mc_ret_e mc_gst_prepare(mc_handle_t *mc_handle);
191 mc_ret_e mc_gst_unprepare(mc_handle_t *mc_handle);
192
193 mc_ret_e mc_gst_process_input(mc_handle_t *mc_handle, media_packet_h inbuf, uint64_t timeOutUs);
194 mc_ret_e mc_gst_get_output(mc_handle_t *mc_handle, media_packet_h *outbuf, uint64_t timeOutUs);
195
196 mc_ret_e mc_gst_flush_buffers(mc_handle_t *mc_handle);
197
198 mc_ret_e mc_gst_get_packet_pool(mc_handle_t *mc_handle, media_packet_pool_h *packet_pool);
199
200 #ifdef __cplusplus
201 }
202 #endif
203
204 #endif /* __TIZEN_MEDIA_CODEC_PORT_GST_H__ */