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