for supporting h/w codec
[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         {                                                                                           \
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
54 /* gst port layer */
55 typedef struct _mc_gst_port_t mc_gst_port_t;
56 typedef struct _mc_gst_core_t mc_gst_core_t;
57 typedef struct _GstMCBuffer GstMCBuffer;
58
59 typedef enum {
60     BUF_SHARE_METHOD_PADDR = 0,
61     BUF_SHARE_METHOD_FD,
62     BUF_SHARE_METHOD_TIZEN_BUFFER,
63     BUF_SHARE_METHOD_FLUSH_BUFFER
64 } buf_share_method_t;
65
66 #ifdef TIZEN_PROFILE_LITE
67 struct ion_mmu_data {
68     int master_id;
69     int fd_buffer;
70     unsigned long iova_addr;
71     size_t iova_size;
72 };
73 #endif
74
75 struct _mc_gst_port_t
76 {
77     mc_gst_core_t *core;
78     unsigned int num_buffers;
79     unsigned int buffer_size;
80     unsigned int index;
81     bool is_allocated;
82     media_packet_h *buffers;
83     //GSem
84     GQueue *queue;
85     GMutex mutex;
86     GCond buffer_cond;
87 };
88
89 struct _mc_gst_core_t
90 {
91     int(**vtable)();
92     const char *mime;
93     //int format;
94     gchar *format;
95     GstElement* pipeline;
96     GstElement* appsrc;
97     GstElement* capsfilter;
98     GstElement* parser;
99     GstElement* fakesink;
100     GstElement* codec;
101     MMVideoBuffer *psimgb;
102     tbm_bufmgr bufmgr;
103     int drm_fd;
104
105     GMainContext *thread_default;
106     gulong signal_handoff;
107     gint bus_whatch_id;
108     gint probe_id;
109
110     GMutex eos_mutex;
111     GMutex eos_wait_mutex;
112     GMutex prepare_lock;
113     GMutex drain_lock;
114     GCond eos_cond;
115     GCond eos_waiting_cond;
116
117     GstState state;
118     bool output_allocated;
119     bool encoder;
120     bool video;
121     bool is_hw;
122     bool eos;
123     bool eos_waiting;
124     bool codec_config;
125     bool need_feed;
126     bool need_codec_data;
127     bool need_sync_flag;
128     bool unprepare_flag;
129     unsigned int prepare_count;
130     unsigned int num_live_buffers;
131     unsigned int queued_count;
132     unsigned int dequeued_count;
133
134
135     mediacodec_codec_type_e codec_id;
136     media_format_mimetype_e out_mime;
137     media_format_h output_fmt;
138     mc_gst_port_t *ports[2];
139     mc_bitstream_t bits;
140
141     mc_aqueue_t *available_queue;
142     GQueue *output_queue;
143
144     void *codec_info;
145     unsigned char codec_data[100];
146     int codec_data_size;
147
148     void* user_cb[_MEDIACODEC_EVENT_TYPE_NUM];
149     void* user_data[_MEDIACODEC_EVENT_TYPE_NUM];
150
151 };
152
153 struct _GstMCBuffer
154 {
155     GstBuffer *buffer;
156     int buf_size;
157     mc_gst_core_t* core;
158     media_packet_h pkt;
159     bool has_imgb;
160 };
161
162 enum { fill_inbuf, fill_outbuf, create_caps };
163
164 //int __mc_link_elements(mc_gst_core_t *core);
165 int __mc_fill_input_buffer(mc_gst_core_t *core, media_packet_h pkt, GstMCBuffer *buff);
166 int __mc_fill_output_buffer(mc_gst_core_t *core, void *data, int size, media_packet_h *out_pkt);
167 int __mc_create_caps(mc_gst_core_t *core, GstCaps **caps, GstMCBuffer* buff, bool codec_config);
168
169 //int __mc_venc_link(mc_gst_core_t *core);
170 //int __mc_general_link(mc_gst_core_t *core);
171
172 int __mc_fill_inbuf_with_mm_video_buffer(mc_gst_core_t *core, media_packet_h pkt, GstMCBuffer *buff);
173 int __mc_fill_inbuf_with_packet(mc_gst_core_t *core, media_packet_h pkt, GstMCBuffer *buff);
174 int __mc_fill_inbuf_with_venc_packet(mc_gst_core_t *core, media_packet_h pkt, GstMCBuffer *mc_buffer);
175
176 int __mc_fill_packet_with_outbuf(mc_gst_core_t *core, void *data, int size, media_packet_h *out_pkt);
177 int __mc_fill_venc_packet_with_outbuf(mc_gst_core_t *core, void *data, int size, media_packet_h *out_pkt);
178 int __mc_fill_video_packet_with_mm_video_buffer(mc_gst_core_t *core, void *data, int size, media_packet_h *out_pkt);
179 int __mc_fill_vdec_packet_with_outbuf(mc_gst_core_t *core, void *data, int size, media_packet_h *pkt);
180
181 int __mc_vdec_h264_caps(mc_gst_core_t *core, GstCaps **caps, GstMCBuffer* buff, bool codec_config);
182 int __mc_venc_caps(mc_gst_core_t *core, GstCaps **caps, GstMCBuffer* buff, bool codec_config);
183 int __mc_vdec_caps(mc_gst_core_t *core, GstCaps **caps, GstMCBuffer* buff, bool codec_config);
184 int __mc_vdec_h263_caps(mc_gst_core_t *core, GstCaps **caps, GstMCBuffer* buff, bool codec_config);
185 int __mc_vdec_mpeg4_caps(mc_gst_core_t *core, GstCaps **caps, GstMCBuffer* buff, bool codec_config);
186 int __mc_h264dec_caps(mc_gst_core_t *core, GstCaps **caps, GstMCBuffer* buff, bool codec_config);
187 int __mc_sprddec_mpeg4_caps(mc_gst_core_t *core, GstCaps **caps, GstMCBuffer* buff, bool codec_config);
188 int __mc_hw_h264enc_caps(mc_gst_core_t *core, GstCaps **caps, GstMCBuffer* buff, bool codec_config);
189 int __mc_sprddec_caps(mc_gst_core_t *core, GstCaps **caps, GstMCBuffer* buff, bool codec_config);
190 int __mc_sprdenc_caps(mc_gst_core_t *core, GstCaps **caps, GstMCBuffer* buff, bool codec_config);
191 int __mc_sprdenc_mpeg4_caps(mc_gst_core_t *core, GstCaps **caps, GstMCBuffer* buff, bool codec_config);
192 int __mc_aenc_caps(mc_gst_core_t *core, GstCaps **caps, GstMCBuffer* buff, bool codec_config);
193 int __mc_adec_aac_caps(mc_gst_core_t *core, GstCaps **caps, GstMCBuffer* buff, bool codec_config);
194 int __mc_aenc_aac_caps(mc_gst_core_t *core, GstCaps **caps, GstMCBuffer* buff, bool codec_config);
195 int __mc_adec_caps(mc_gst_core_t *core, GstCaps **caps, GstMCBuffer* buff, bool codec_config);
196 int __mc_adec_aacv12_caps(mc_gst_core_t *core, GstCaps **caps, GstMCBuffer* buff, bool codec_config);
197 int __mc_adec_mp3_caps(mc_gst_core_t *core, GstCaps **caps, GstMCBuffer* buff, bool codec_config);
198 int __mc_adec_amrnb_caps(mc_gst_core_t *core, GstCaps **caps, GstMCBuffer* buff, bool codec_config);
199 int __mc_adec_amrwb_caps(mc_gst_core_t *core, GstCaps **caps, GstMCBuffer* buff, bool codec_config);
200 int __mc_aenc_amrnb_caps(mc_gst_core_t *core, GstCaps **caps, GstMCBuffer* buff, bool codec_config);
201 int __mc_adec_vorbis_caps(mc_gst_core_t *core, GstCaps **caps, GstMCBuffer* buff, bool codec_config);
202 int __mc_adec_flac_caps(mc_gst_core_t *core, GstCaps **caps, GstMCBuffer* buff, bool codec_config);
203 int __mc_adec_wma_caps(mc_gst_core_t *core, GstCaps **caps, GstMCBuffer* buff, bool codec_config);
204 bool _mc_is_voss (unsigned char *p, int size, int *codec_size);
205 bool _mc_is_ivop (unsigned char *p, int size, int pos);
206 bool _mc_is_vop (unsigned char *p, int size, int pos);
207
208
209 void _mc_create_codec_map_from_ini(mc_handle_t *mc_handle, mc_codec_spec_t *spec_emul);
210 void _mc_create_decoder_map_from_ini(mc_handle_t *mc_handle);
211 void _mc_create_encoder_map_from_ini(mc_handle_t *mc_handle);
212 mc_gst_core_t *mc_gst_core_new();
213 void mc_gst_core_free(mc_gst_core_t *core);
214
215 mc_gst_port_t *mc_gst_port_new(mc_gst_core_t *core);
216 void mc_gst_port_free(mc_gst_port_t *port);
217
218 mc_ret_e mc_gst_prepare(mc_handle_t *mc_handle);
219 mc_ret_e mc_gst_unprepare(mc_handle_t *mc_handle);
220
221 mc_ret_e mc_gst_process_input(mc_handle_t *mc_handle, media_packet_h inbuf, uint64_t timeOutUs);
222 mc_ret_e mc_gst_get_output(mc_handle_t *mc_handle, media_packet_h *outbuf, uint64_t timeOutUs);
223
224 mc_ret_e mc_gst_flush_buffers(mc_handle_t *mc_handle);
225
226 #ifdef __cplusplus
227 }
228 #endif
229
230 #endif /* __TIZEN_MEDIA_CODEC_PORT_GST_H__ */