Merge branch 'tizen_line_coverage' into tizen
[platform/core/api/mediacodec.git] / src / media_codec_port_gst.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <dlog.h>
21 #include <media_codec_queue.h>
22 #include <media_codec_port_gst.h>
23 #include <media_packet_internal.h>
24 #include <media_codec_util.h>
25
26 #include <gst/gst.h>
27 #include <gst/gstelement.h>
28 #include <gst/app/gstappsrc.h>
29
30 /*
31  * Internal Implementation
32  */
33 static gpointer feed_task(gpointer data);
34 static void __mc_gst_stop_feed(GstElement *pipeline, gpointer data);
35 static void __mc_gst_start_feed(GstElement *pipeline, guint size, gpointer data);
36 static media_packet_h _mc_get_input_buffer(mc_gst_core_t *core);
37
38 static gboolean __mc_gst_init_gstreamer();
39 static int _mc_output_media_packet_new(mc_gst_core_t *core, bool video, bool encoder, media_format_mimetype_e out_mime);
40 static mc_ret_e _mc_gst_create_pipeline(mc_gst_core_t *core, gchar *factory_name);
41 static mc_ret_e _mc_gst_destroy_pipeline(mc_gst_core_t *core);
42 static void __mc_gst_buffer_add(GstElement *element, GstBuffer *buffer, GstPad *pad, gpointer data);
43 static int __mc_output_buffer_finalize_cb(media_packet_h packet, int error_code, void *user_data);
44 static gboolean _mc_update_packet_info(mc_gst_core_t *core, media_format_h format);
45 static GstMCBuffer *_mc_gst_media_packet_to_gstbuffer(mc_gst_core_t *core, media_packet_h packet, bool codec_config);
46 static int _mc_gst_gstbuffer_to_appsrc(mc_gst_core_t *core, GstMCBuffer *mcbuffer);
47 static gchar *__mc_get_gst_input_format(media_format_mimetype_e mimetype, bool is_hw);
48 static GstMCBuffer *__mc_gst_make_media_packet(mc_gst_core_t *core, GstBuffer *buffer);
49 static gboolean __mc_gst_bus_callback(GstBus *bus, GstMessage *msg, gpointer data);
50 static GstBusSyncReply __mc_gst_bus_sync_callback(GstBus *bus, GstMessage *msg, gpointer data);
51 static GstMemory *__mc_gst_make_tbm_buffer(mc_gst_core_t *core, GstMCBuffer *mcbuffer);
52 static GstMCBuffer *gst_mediacodec_buffer_new(mc_gst_core_t *core, media_packet_h packet, uint64_t size);
53 static void __mc_input_buffer_finalize_cb(GstMCBuffer *buffer);
54 static int __mc_set_caps_streamheader(mc_gst_core_t *core, GstMCBuffer *mcbuffer, guint streamheader_size);
55 static int __mc_set_caps_codecdata(mc_gst_core_t *core, GstMCBuffer *mcbuffer, guint codecdata_size);
56 static void __mc_push_output_to_queue(mc_gst_core_t *core, GstMCBuffer *mcbuffer);
57 static int __mc_gst_create_eos_packet(media_format_h fmt, GstMCBuffer *mcbuffer);
58 static void _mc_gst_handle_input_buffer_used(mc_gst_core_t *core, media_packet_h packet);
59
60 static gint __gst_handle_stream_error(mc_gst_core_t *core, GError *error, GstMessage *message);
61 static gint __gst_transform_gsterror(mc_gst_core_t *core, GstMessage *message, GError *error);
62 static gint __gst_handle_resource_error(mc_gst_core_t *core, int code);
63 static gint __gst_handle_library_error(mc_gst_core_t *core, int code);
64 static gint __gst_handle_core_error(mc_gst_core_t *core, int code);
65 static const gchar * _mc_error_to_string(mc_ret_e err);
66 static const gchar * _mc_bit_to_string(int bit);
67
68 static int _mc_link_vtable(mc_gst_core_t *core, mediacodec_codec_type_e id, gboolean is_encoder, gboolean is_hw);
69 static int _mc_gst_flush_buffers(mc_gst_core_t *core);
70 static void _mc_gst_set_flush_input(mc_gst_core_t *core);
71 static void _mc_gst_set_flush_output(mc_gst_core_t *core);
72
73 #ifdef TIZEN_EXYNOS_SPECIFIC
74 static int __tile_4x2_read(int x_size, int y_size, int x_pos, int y_pos);
75 static void __csc_tiled_to_linear_crop(unsigned char *yuv420_dest,
76                 unsigned char *nv12t_src, int yuv420_width, int yuv420_height,
77                 int left, int top, int right, int buttom);
78 #endif
79
80 static void _mc_send_eos_signal(mc_gst_core_t *core);
81 static void _mc_wait_for_eos(mc_gst_core_t *core);
82 static int _mc_get_mime(mc_gst_core_t *core);
83 static int _mc_set_codec_data(mc_gst_core_t *core, GstMCBuffer *mcbuffer, bool codec_config);
84 GstCaps *_mc_gst_vid_caps_new(mc_gst_core_t *core, mediacodec_codec_type_e codec_id, gint index);
85 GstCaps *_mc_gst_aud_caps_new(mc_gst_core_t *core, mediacodec_codec_type_e codec_id, gint index);
86
87 static int __mc_fill_input_buffer(mc_gst_core_t *core, media_packet_h packet, GstMCBuffer *mcbuffer);
88 static int __mc_fill_output_buffer(mc_gst_core_t *core, void *data, int size, GstMCBuffer *mcbuffer);
89
90 static int __mc_fill_input_buffer_with_packet(mc_gst_core_t *core, media_packet_h packet, GstMCBuffer *mcbuffer);
91 static int __mc_fill_input_buffer_with_adec_packet(mc_gst_core_t *core, media_packet_h packet, GstMCBuffer *mcbuffer);
92 static int __mc_fill_input_buffer_with_venc_packet(mc_gst_core_t *core, media_packet_h packet, GstMCBuffer *mcbuffer);
93
94 static int __mc_fill_packet_with_output_buffer(mc_gst_core_t *core, void *data, int size, GstMCBuffer *mcbuffer);
95 static int __mc_fill_aenc_packet_with_output_buffer(mc_gst_core_t *core, void *data, int size, GstMCBuffer *mcbuffer);
96 static int __mc_fill_venc_packet_with_output_buffer(mc_gst_core_t *core, void *data, int size, GstMCBuffer *mcbuffer);
97 static int __mc_fill_vdec_packet_with_output_buffer(mc_gst_core_t *core, void *data, int size, GstMCBuffer *mcbuffer);
98
99 static mc_gst_core_t *mc_gst_core_new();
100 static void mc_gst_core_free(mc_gst_core_t *core);
101
102 static mc_gst_port_t *mc_gst_port_new(mc_gst_core_t *core);
103 static void mc_gst_port_free(mc_gst_port_t *port);
104 /* video vtable */
105 int(*vdec_vtable[])() = {&__mc_fill_input_buffer_with_packet, &__mc_fill_packet_with_output_buffer};
106 int(*venc_vtable[])() = {&__mc_fill_input_buffer_with_packet, &__mc_fill_packet_with_output_buffer};
107
108
109 int(*vdec_h264_sw_vtable[])() = {&__mc_fill_input_buffer_with_packet,                   /* S/W H.264 Decoder Vtable */
110                                                                 &__mc_fill_vdec_packet_with_output_buffer};
111
112 int(*vdec_mpeg4_sw_vtable[])() = {&__mc_fill_input_buffer_with_packet,                  /* S/W MPEG4 Decoder Vtable */
113                                                                 &__mc_fill_vdec_packet_with_output_buffer};
114
115 int(*vdec_h263_sw_vtable[])() = {&__mc_fill_input_buffer_with_packet,                   /* S/W MPEG4 Decoder Vtable */
116                                                                 &__mc_fill_vdec_packet_with_output_buffer};
117
118 int(*vdec_h264_hw_vtable[])() = {&__mc_fill_input_buffer_with_packet,                   /* H/W H.264 Decoder Vtable */
119                                                                 &__mc_fill_vdec_packet_with_output_buffer};
120
121 int(*vdec_mpeg4_hw_vtable[])() = {&__mc_fill_input_buffer_with_packet,                  /* H/W MPEG4 Decoder Vtable */
122                                                                 &__mc_fill_vdec_packet_with_output_buffer};
123
124 int(*vdec_h263_hw_vtable[])() = {&__mc_fill_input_buffer_with_packet,                   /* H/W MPEG4 Decoder Vtable */
125                                                                 &__mc_fill_vdec_packet_with_output_buffer};
126
127 int(*venc_mpeg4_sw_vtable[])() = {&__mc_fill_input_buffer_with_venc_packet,             /* S/W MPEG4 Encoder Vtable */
128                                                                 &__mc_fill_venc_packet_with_output_buffer};
129
130 int(*venc_h263_sw_vtable[])() = {&__mc_fill_input_buffer_with_venc_packet,              /* S/W MPEG4 Encoder Vtable */
131                                                                 &__mc_fill_venc_packet_with_output_buffer};
132
133 int(*venc_h264_hw_vtable[])() = {&__mc_fill_input_buffer_with_venc_packet,              /* H/W H.264 Encoder Vtable */
134                                                                 &__mc_fill_venc_packet_with_output_buffer};
135
136 int(*venc_mpeg4_hw_vtable[])() = {&__mc_fill_input_buffer_with_venc_packet,             /* H/W MPEG4 Encoder Vtable */
137                                                                 &__mc_fill_venc_packet_with_output_buffer};
138
139 int(*venc_h263_hw_vtable[])() = {&__mc_fill_input_buffer_with_venc_packet,              /* H/W MPEG4 Encoder Vtable */
140                                                                 &__mc_fill_venc_packet_with_output_buffer};
141
142 int(*aenc_vtable[])() = {&__mc_fill_input_buffer_with_packet, &__mc_fill_packet_with_output_buffer};
143 int(*adec_vtable[])() = {&__mc_fill_input_buffer_with_packet, &__mc_fill_packet_with_output_buffer};
144
145 int(*aenc_aac_vtable[])() = {&__mc_fill_input_buffer_with_packet,                    /* AAC LC Encoder vtable */
146                                                         &__mc_fill_aenc_packet_with_output_buffer};
147
148 int(*adec_aac_vtable[])() = {&__mc_fill_input_buffer_with_adec_packet,               /* AAC LC Decoder Vtable */
149                                                         &__mc_fill_packet_with_output_buffer};
150
151 int(*adec_aacv12_vtable[])() = {&__mc_fill_input_buffer_with_adec_packet,            /* AAC HE Decoder Vtable */
152                                                         &__mc_fill_packet_with_output_buffer};
153
154 int(*adec_mp3_vtable[])() = {&__mc_fill_input_buffer_with_packet,                    /* MP3 Decoder Vtable */
155                                                         &__mc_fill_packet_with_output_buffer};
156
157 int(*adec_amrnb_vtable[])() = {&__mc_fill_input_buffer_with_packet,                  /* AMR-NB Decoder Vtable */
158                                                         &__mc_fill_packet_with_output_buffer};
159
160 int(*adec_amrwb_vtable[])() = {&__mc_fill_input_buffer_with_packet,                  /* AMR-WB Decoder Vtable */
161                                                         &__mc_fill_packet_with_output_buffer};
162
163 int(*aenc_amrnb_vtable[])() = {&__mc_fill_input_buffer_with_packet,                  /* AMR-NB Encoder Vtable */
164                                                         &__mc_fill_packet_with_output_buffer};
165
166 int(*adec_vorbis_vtable[])() = {&__mc_fill_input_buffer_with_packet,                 /* VORBIS Decoder Vtable */
167                                                         &__mc_fill_packet_with_output_buffer};
168
169 int(*adec_flac_vtable[])() = {&__mc_fill_input_buffer_with_packet,                   /* FLAC Decoder Vtable */
170                                                         &__mc_fill_packet_with_output_buffer};
171
172 int(*adec_wma_vtable[])() = {&__mc_fill_input_buffer_with_packet,                    /* WMA Decoder Vtable */
173                                                         &__mc_fill_packet_with_output_buffer};
174
175 #define MEDIACODEC_ELEMENT_SET_STATE(x_element, x_state)                                          \
176         do {                                                                                            \
177                 LOGD("setting state [%s:%d] to [%s]\n", #x_state, x_state, GST_ELEMENT_NAME(x_element)); \
178                 if (GST_STATE_CHANGE_FAILURE == gst_element_set_state(x_element, x_state)) {                    \
179                         LOGE("failed to set state %s to %s\n", #x_state, GST_ELEMENT_NAME(x_element));        \
180                         goto STATE_CHANGE_FAILED;                                                               \
181                 }                                                                                           \
182         } while (0)
183
184 /*
185  * fill_inbuf virtual functions
186  */
187 int __mc_fill_input_buffer(mc_gst_core_t *core, media_packet_h packet, GstMCBuffer *mcbuffer)
188 {
189         return core->vtable[fill_inbuf](core, packet, mcbuffer);
190 }
191
192 int __mc_fill_inbuf_with_mm_video_buffer(mc_gst_core_t *core, media_packet_h packet, GstMCBuffer *mcbuffer)
193 {
194         gint ret = MC_ERROR_NONE;
195         uint64_t buf_size = 0;
196         GstMemory *mem;
197         void *buf_data = NULL;
198
199         ret = media_packet_get_buffer_size(packet, &buf_size);
200         if (ret != MEDIA_PACKET_ERROR_NONE) {
201                 LOGW("buffer size get failed");
202                 return MC_ERROR;
203         }
204
205         ret = media_packet_get_buffer_data_ptr(packet, &buf_data);
206         if (ret != MEDIA_PACKET_ERROR_NONE) {
207                 LOGW("buffer data get failed");
208                 return MC_ERROR;
209         }
210
211         mem = __mc_gst_make_tbm_buffer(core, mcbuffer);
212         if (mem == NULL) {
213                 LOGW("Failed to make tizen memory");
214                 return MC_ERROR;
215         }
216
217         gst_buffer_append_memory(mcbuffer->buffer, mem);
218         LOGD("tizen memory appended");
219
220         return ret;
221 }
222
223 static int __mc_fill_input_buffer_with_packet(mc_gst_core_t *core, media_packet_h packet, GstMCBuffer *mcbuffer)
224 {
225         gint ret = MC_ERROR_NONE;
226         void *buf_data = NULL;
227         uint64_t buf_size = 0;
228
229         ret = media_packet_get_buffer_size(packet, &buf_size);
230         if (ret != MEDIA_PACKET_ERROR_NONE) {
231                 LOGW("buffer size get failed");
232                 return MC_ERROR;
233         }
234
235         ret = media_packet_get_buffer_data_ptr(packet, &buf_data);
236         if (ret != MEDIA_PACKET_ERROR_NONE) {
237                 LOGW("buffer data get failed");
238                 return MC_ERROR;
239         }
240
241         if (buf_data != NULL) {
242                 gst_buffer_append_memory(mcbuffer->buffer,
243                                 gst_memory_new_wrapped(GST_MEMORY_FLAG_READONLY, buf_data, buf_size, 0,
244                                         buf_size, mcbuffer, (GDestroyNotify)__mc_input_buffer_finalize_cb));
245                 LOGD("packet data apended");
246         }
247
248         return ret;
249 }
250
251 static int __mc_fill_input_buffer_with_adec_packet(mc_gst_core_t *core, media_packet_h packet, GstMCBuffer *mcbuffer)
252 {
253         gint ret = MC_ERROR_NONE;
254         uint64_t buf_size = 0;
255         unsigned int codec_size = 0;
256         bool codec_config = FALSE;
257         void *buf_data = NULL;
258         void *codec_data = NULL;
259
260         ret = media_packet_get_buffer_size(packet, &buf_size);
261         if (ret != MEDIA_PACKET_ERROR_NONE) {
262                 LOGW("buffer size get failed");
263                 return MC_ERROR;
264         }
265
266         ret = media_packet_get_buffer_data_ptr(packet, &buf_data);
267         if (ret != MEDIA_PACKET_ERROR_NONE) {
268                 LOGW("buffer data get failed");
269                 return MC_ERROR;
270         }
271
272         if (media_packet_is_codec_config(packet, &codec_config) != MEDIA_PACKET_ERROR_NONE) {
273                 LOGE("media_packet_is_codec_config failed");
274                 return MC_ERROR;
275         }
276
277         if (buf_data != NULL) {
278                 media_packet_get_codec_data(packet, &codec_data, &codec_size);
279
280                 if (!codec_data && codec_config) {
281                         gst_buffer_append_memory(mcbuffer->buffer,
282                                         gst_memory_new_wrapped(GST_MEMORY_FLAG_READONLY, buf_data, buf_size, 0,
283                                                 buf_size, NULL, NULL));
284                         LOGD("csd packet data apended");
285                 } else {
286                         gst_buffer_append_memory(mcbuffer->buffer,
287                                         gst_memory_new_wrapped(GST_MEMORY_FLAG_READONLY, buf_data, buf_size, 0,
288                                                 buf_size, mcbuffer, (GDestroyNotify)__mc_input_buffer_finalize_cb));
289                         LOGD("packet data apended");
290                 }
291         }
292
293         return ret;
294 }
295
296 static int __mc_fill_input_buffer_with_venc_packet(mc_gst_core_t *core, media_packet_h packet, GstMCBuffer *mcbuffer)
297 {
298         gint ret = MC_ERROR_NONE;
299         gint i;
300         gint j;
301         gint stride = 0;
302         gint buf_size = 0;
303         gint stride_width;
304         gint stride_height;
305         gint width;
306         gint height;
307         uint32_t plane_num;
308         guint8 *planes[2];
309
310         mc_gst_port_def_t *port_def = &core->ports[in_port_index]->port_def;
311
312         width = port_def->info.video.width;
313         height = port_def->info.video.height;
314
315         ret = media_packet_get_number_of_video_planes(packet, &plane_num);
316         if (ret != MEDIA_PACKET_ERROR_NONE) {
317                 LOGW("media_packet_get_number_of_video_planes failed");
318                 return MC_ERROR;
319         }
320
321         ret = media_packet_get_video_plane_data_ptr(packet, 0, (void **)&planes[0]);
322         if (ret != MEDIA_PACKET_ERROR_NONE) {
323                 LOGW("media_packet_get_video_plane_data_ptr failed");
324                 return MC_ERROR;
325         }
326
327         ret = media_packet_get_video_stride_width(packet, 0, &stride_width);
328         if (ret != MEDIA_PACKET_ERROR_NONE) {
329                 LOGW("media_packet_get_video_stride_width failed");
330                 return MC_ERROR;
331         }
332
333         ret = media_packet_get_video_stride_height(packet, 0, &stride_height);
334         if (ret != MEDIA_PACKET_ERROR_NONE) {
335                 LOGW("media_packet_get_video_stride_width failed");
336                 return MC_ERROR;
337         }
338
339         if (!core->is_hw) {
340                 if (width == stride_width) {
341                         mcbuffer->buf_size += stride_width * stride_height;
342
343                         for (i = 1; i < plane_num; i++) {
344                                 media_packet_get_video_plane_data_ptr(packet, i, (void **)&planes[1]);
345                                 media_packet_get_video_stride_width(packet, i, &stride_width);
346                                 media_packet_get_video_stride_height(packet, i, &stride_height);
347
348                                 buf_size = stride_width * stride_height;
349
350                                 memcpy(planes[0] + mcbuffer->buf_size, planes[1], buf_size);
351                                 mcbuffer->buf_size += buf_size;
352                                 LOGD("plane : %d, buf_size : %d, total : %d", i, buf_size, mcbuffer->buf_size);
353                         }
354                 } else {
355
356                         for (j = 0; j < height; j++) {
357                                 memcpy(planes[0] + mcbuffer->buf_size, planes[0] + stride, width);
358                                 mcbuffer->buf_size += width;
359                                 stride += stride_width;
360                         }
361
362                         stride = 0;
363
364                         for (i = 1; i < plane_num; i++) {
365                                 media_packet_get_video_plane_data_ptr(packet, i, (void **)&planes[1]);
366                                 media_packet_get_video_stride_width(packet, i, &stride_width);
367                                 media_packet_get_video_stride_height(packet, i, &stride_height);
368
369                                 for (j = 0; j < height>>1; j++) {
370                                         memcpy(planes[0] + mcbuffer->buf_size, planes[1] + stride, width>>1);
371                                         mcbuffer->buf_size += width>>1;
372                                         stride += stride_width;
373                                 }
374
375                                 memcpy(planes[0] + mcbuffer->buf_size, planes[1], buf_size);
376                                 LOGD("plane : %d, buf_size : %d, total : %d", i, buf_size, mcbuffer->buf_size);
377                                 mcbuffer->buf_size += buf_size;
378                         }
379                 }
380         } else {
381                 GstMemory *mem = NULL;
382
383                 mem = __mc_gst_make_tbm_buffer(core, mcbuffer);
384                 if (mem == NULL) {
385                         LOGW("Failed to make tizen memory");
386                         return MC_ERROR;
387                 }
388
389                 gst_buffer_append_memory(mcbuffer->buffer, mem);
390                 LOGD("tizen memory appended");
391         }
392         return ret;
393 }
394
395
396 /*
397  * fill_outbuf virtual functions
398  */
399
400 static int __mc_fill_output_buffer(mc_gst_core_t *core, void *data, int size, GstMCBuffer *mcbuffer)
401 {
402         return core->vtable[fill_outbuf](core, data, size, mcbuffer);
403 }
404
405 static int __mc_fill_vdec_packet_with_output_buffer(mc_gst_core_t *core, void *data, int size, GstMCBuffer *mcbuffer)
406 {
407         int i;
408         int stride_width;
409         int stride_height;
410         uint32_t width;
411         uint32_t height;
412         uint32_t buf_size;
413         tbm_surface_info_s tsurf_info;
414         tbm_bo bo[MM_VIDEO_BUFFER_PLANE_MAX];
415         tbm_bo_handle thandle;
416         tbm_surface_h tsurf = NULL;
417         media_packet_h packet = NULL;
418
419         g_return_val_if_fail(core != NULL, MC_PARAM_ERROR);
420
421         mc_gst_port_def_t *port_def = &core->ports[out_port_index]->port_def;
422
423         if (!core->is_hw) {
424                 width = port_def->info.video.width;
425                 height = port_def->info.video.height;
426                 stride_width = GST_ROUND_UP_4(width);
427                 stride_height = height;
428                 buf_size = stride_width * stride_height * 3 / 2;
429
430                 memset(&tsurf_info, 0x0, sizeof(tbm_surface_info_s));
431
432                 bo[0] = tbm_bo_alloc(core->bufmgr, buf_size, TBM_BO_WC);
433                 if (!bo[0]) {
434                         LOGE("bo allocation failed");
435                         return MC_ERROR;
436                 }
437
438                 tsurf_info.width = port_def->info.video.width;
439                 tsurf_info.height = port_def->info.video.height;
440                 tsurf_info.format = TBM_FORMAT_YVU420;
441                 tsurf_info.bpp = tbm_surface_internal_get_bpp(TBM_FORMAT_YVU420);
442                 tsurf_info.num_planes = tbm_surface_internal_get_num_planes(TBM_FORMAT_YVU420);
443                 tsurf_info.size = 0;
444
445                 for (i = 0; i < tsurf_info.num_planes; i++) {
446                         if (i == 0) {
447                                 tsurf_info.planes[i].stride = stride_width;
448                                 tsurf_info.planes[i].size = stride_width * stride_height;
449                                 tsurf_info.planes[i].offset = 0;
450                                 tsurf_info.size = tsurf_info.planes[i].size;
451                         } else {
452                                 tsurf_info.planes[i].stride = GST_ROUND_UP_4(GST_ROUND_UP_2(width) / 2);
453                                 tsurf_info.planes[i].size = GST_ROUND_UP_4(GST_ROUND_UP_2(width) / 2) * (height / 2);
454                                 tsurf_info.planes[i].offset = (tsurf_info.planes[i-1].offset + tsurf_info.planes[i - 1].size);
455                                 tsurf_info.size += tsurf_info.planes[i].size;
456                         }
457                 }
458
459                 thandle = tbm_bo_map(bo[0], TBM_DEVICE_CPU, TBM_OPTION_WRITE);
460                 memcpy(thandle.ptr, data, tsurf_info.size);
461                 tbm_bo_unmap(bo[0]);
462
463                 tsurf = tbm_surface_internal_create_with_bos(&tsurf_info, bo, 1);
464         } else {
465                 tsurf = (tbm_surface_h)data;
466         }
467
468         if (tsurf) {
469                 media_packet_create_from_tbm_surface(core->output_fmt, tsurf,
470                                 (media_packet_finalize_cb)__mc_output_buffer_finalize_cb, mcbuffer, &packet);
471                 mcbuffer->packet = packet;
472         }
473
474         return MC_ERROR_NONE;
475 }
476
477 static int __mc_fill_packet_with_output_buffer(mc_gst_core_t *core, void *data, int size, GstMCBuffer *mcbuffer)
478 {
479         gint ret = MC_ERROR_NONE;
480         gchar *ext_mem = NULL;
481         gint mem_size = 0;
482         void *packet_data = NULL;
483         media_packet_h packet = NULL;
484
485         g_return_val_if_fail(core != NULL, MC_PARAM_ERROR);
486
487         if (core->encoder && core->codec_id == MEDIACODEC_AAC)
488                 media_format_set_audio_aac_type(core->output_fmt, FALSE);
489
490         mem_size = GST_ROUND_UP_4(size);
491         ext_mem = g_malloc0(mem_size);
492
493         ret = media_packet_create_from_external_memory(core->output_fmt, ext_mem, mem_size, __mc_output_buffer_finalize_cb, mcbuffer, &packet);
494         if (ret != MEDIA_PACKET_ERROR_NONE) {
495                 LOGW("media_packet_create_alloc failed");
496                 g_free(ext_mem);
497                 return MC_ERROR;
498         }
499         mcbuffer->packet = packet;
500         mcbuffer->ext_mem = ext_mem;
501
502         media_packet_set_buffer_size(packet, size);
503         media_packet_get_buffer_data_ptr(packet, &packet_data);
504         memcpy(packet_data, data, size);
505
506         return MC_ERROR_NONE;
507 }
508
509 static int __mc_fill_venc_packet_with_output_buffer(mc_gst_core_t *core, void *data, int size, GstMCBuffer *mcbuffer)
510 {
511         gint ret = MC_ERROR_NONE;
512         bool codec_config = FALSE;
513         bool sync_flag = FALSE;
514         bool slice = FALSE;
515         gint mem_size = 0;
516         gchar *ext_mem = NULL;
517         gint data_size = 0;
518         void *packet_data = NULL;
519         media_packet_h packet = NULL;
520
521         g_return_val_if_fail(core != NULL, MC_PARAM_ERROR);
522
523         switch (core->ports[out_port_index]->port_def.coding_type) {
524         case MEDIA_FORMAT_H264_SP:
525         case MEDIA_FORMAT_H264_MP:
526         case MEDIA_FORMAT_H264_HP:
527                 data_size = _mc_check_h264_bytestream((unsigned char *)data, size, 1, &codec_config, &sync_flag, &slice);
528
529                 if (data_size < 0) {
530                         LOGE("No valid SPS/PPS ...");
531                         return MC_INVALID_IN_BUF;
532                 }
533                 break;
534         case MEDIA_FORMAT_MPEG4_SP:
535         case MEDIA_FORMAT_MPEG4_ASP:
536                 _mc_check_mpeg4_out_bytestream((unsigned char *)data, size, &codec_config, &sync_flag);
537                 break;
538         case MEDIA_FORMAT_H263:
539         case MEDIA_FORMAT_H263P:
540                 if (!_mc_check_h263_out_bytestream((unsigned char *)data, size, &sync_flag))
541                         return MC_INVALID_IN_BUF;
542                 break;
543         default:
544                 return MC_INVALID_IN_BUF;
545         }
546         LOGD("codec_config : %d, sync_flag : %d, slice : %d", codec_config, sync_flag, slice);
547
548         mem_size = GST_ROUND_UP_4(size);
549         ext_mem = g_malloc0(mem_size);
550
551         ret = media_packet_create_from_external_memory(core->output_fmt, ext_mem, mem_size, __mc_output_buffer_finalize_cb, mcbuffer, &packet);
552         if (ret != MEDIA_PACKET_ERROR_NONE) {
553                 LOGW("media_packet_create_alloc failed");
554                 g_free(ext_mem);
555                 return MC_ERROR;
556         }
557         mcbuffer->packet = packet;
558         mcbuffer->ext_mem = ext_mem;
559
560         media_packet_set_buffer_size(packet, size);
561         media_packet_get_buffer_data_ptr(packet, &packet_data);
562         memcpy(packet_data, data, size);
563
564         core->need_sync_flag = sync_flag ? 1 : 0;
565         core->need_codec_data = codec_config ? 1 : 0;
566
567         return ret;
568 }
569
570 static int __mc_fill_aenc_packet_with_output_buffer(mc_gst_core_t *core, void *data, int size, GstMCBuffer *mcbuffer)
571 {
572         int ret = MC_ERROR_NONE;
573         gint mem_size = 0;
574         GstPad *pad;
575         GstCaps *peercaps;
576         GstMapInfo map;
577         guint8 *ptr;
578         gsize len;
579         gchar *ext_mem = NULL;
580         const GValue *codec_data = NULL;
581         void *packet_data = NULL;
582         media_packet_h packet = NULL;
583
584         g_return_val_if_fail(core != NULL, MC_PARAM_ERROR);
585
586         if (core->encoder && core->codec_id == MEDIACODEC_AAC)
587                 media_format_set_audio_aac_type(core->output_fmt, FALSE);
588
589         mem_size = GST_ROUND_UP_4(size);
590         ext_mem = g_malloc0(mem_size);
591
592         ret = media_packet_create_from_external_memory(core->output_fmt, ext_mem, mem_size, __mc_output_buffer_finalize_cb, mcbuffer, &packet);
593         if (ret != MEDIA_PACKET_ERROR_NONE) {
594                 LOGW("media_packet_create_alloc failed");
595                 g_free(ext_mem);
596                 return MC_ERROR;
597         }
598         mcbuffer->packet = packet;
599         mcbuffer->ext_mem = ext_mem;
600
601         media_packet_set_buffer_size(packet, size);
602         media_packet_get_buffer_data_ptr(packet, &packet_data);
603         memcpy(packet_data, data, size);
604
605         if (!core->codec_data) {
606                 pad = gst_element_get_static_pad(core->fakesink, "sink");
607                 peercaps = gst_pad_get_current_caps(pad);
608                 if (peercaps) {
609                         GstStructure *s;
610                         if (gst_caps_is_empty(peercaps)) {
611                                 gst_caps_unref(peercaps);
612                                 gst_object_unref(pad);
613                                 LOGW("Empty caps");
614                                 return ret;
615                         }
616
617                         s = gst_caps_get_structure(peercaps, 0);
618                         codec_data = gst_structure_get_value(s, "codec_data");
619                         core->codec_data = GST_BUFFER(g_value_dup_boxed(codec_data));
620                         gst_caps_unref(peercaps);
621                 }
622                 gst_object_unref(pad);
623                 LOGD("codec data set");
624         }
625
626         if (core->codec_data) {
627                 gst_buffer_map(core->codec_data, &map, GST_MAP_READ);
628                 ptr = map.data;
629                 len = map.size;
630                 ret = media_packet_set_codec_data(packet, ptr, len);
631                 if (ret != MEDIA_PACKET_ERROR_NONE) {
632                         LOGW("media_packet_set_codec_data failed");
633                         gst_buffer_unmap(core->codec_data, &map);
634                         return MC_ERROR;
635                 }
636                 gst_buffer_unmap(core->codec_data, &map);
637         }
638
639         return ret;
640 }
641
642 static GstCaps *__mc_gst_caps_set_buffer_array(GstCaps * caps, const gchar * name, GstBuffer * buf, ...)
643 {
644         GstStructure *struc = NULL;
645         va_list va;
646         GValue arr_val = { 0 };
647         GValue buf_val = { 0 };
648
649         g_return_val_if_fail(name != NULL, NULL);
650         g_return_val_if_fail(caps != NULL, NULL);
651         g_return_val_if_fail(gst_caps_is_fixed(caps), NULL);
652         caps = gst_caps_make_writable(caps);
653
654         struc = gst_caps_get_structure(caps, 0);
655         if (!struc)
656                 LOGW("cannot get structure from caps.\n");
657
658         g_value_init(&arr_val, GST_TYPE_ARRAY);
659
660         va_start(va, buf);
661         while (buf) {
662                 g_value_init(&buf_val, GST_TYPE_BUFFER);
663                 gst_value_set_buffer(&buf_val, buf);
664                 gst_value_array_append_value(&arr_val, &buf_val);
665                 g_value_unset(&buf_val);
666
667                 buf = va_arg(va, GstBuffer *);
668         }
669         va_end(va);
670
671         gst_structure_take_value(struc, name, &arr_val);
672
673         return caps;
674 }
675
676 int __mc_set_caps_streamheader(mc_gst_core_t *core, GstMCBuffer *mcbuffer, guint streamheader_size)
677 {
678         gint ret = MEDIA_PACKET_ERROR_NONE;
679         uint64_t buf_size = 0;
680         GstBuffer *header1, *header2, *header3;
681         guint hsize1, hsize2, hsize3;
682         GstBuffer *tmp_header;
683         GstMapInfo map;
684         guint8 *buf_data = NULL;
685         guint8 *tmp_buf = NULL;
686
687         ret = media_packet_get_buffer_size(mcbuffer->packet, &buf_size);
688         if (ret != MEDIA_PACKET_ERROR_NONE) {
689                 LOGW("buffer size get failed");
690                 return ret;
691         }
692
693         ret = media_packet_get_buffer_data_ptr(mcbuffer->packet, (void **)&buf_data);
694         if (ret != MEDIA_PACKET_ERROR_NONE) {
695                 LOGW("buffer data get failed");
696                 return ret;
697         }
698
699         LOGD("Set caps for streamheader in mime : %s and codec_id (0x%x)", core->mime, core->codec_id);
700
701         if (core->codec_id == MEDIACODEC_VORBIS) {
702                 /*
703                  * hsize1 : Identification Header (packet type 1) - fixed 30 byte
704                  * hsize2 : Comment Header (packet type 3) - variable byte (need calculate)
705                  * hsize3 : Setup Header (packet type 5) - variable byte (Used remained bytes)
706                  */
707
708                 /* First of all, Need to fins and calculate size of hsize2 */
709                 tmp_header = gst_buffer_new_and_alloc(streamheader_size);
710                 gst_buffer_fill(tmp_header, 0, buf_data, streamheader_size);
711                 gst_buffer_map(tmp_header, &map, GST_MAP_READ);
712                 tmp_buf = map.data;
713                 tmp_buf += (30 + 7);                /* hsize1 + '0x03' + 'vorbis'*/
714                 hsize2 = (7 + 4);
715                 hsize2 += GST_READ_UINT32_LE(tmp_buf);
716                 hsize2 += (4 + 1);
717                 LOGD("Find streamheader hsize2(%d)", hsize2);
718                 gst_buffer_unmap(tmp_header, &map);
719                 gst_buffer_unref(tmp_header);
720
721                 /*  hsize1 : Identification Header (packet type 1) - fixed 30 byte */
722                 hsize1 = 30;
723                 header1 = gst_buffer_new_and_alloc(hsize1);
724                 gst_buffer_fill(header1, 0, buf_data, hsize1);
725                 gst_buffer_map(header1, &map, GST_MAP_READ);
726                 tmp_buf = map.data;
727                 /* '0x01' + 'vorbis' */
728                 if (*tmp_buf != 0x01) {
729                         LOGE("[ERROR] Invalid Caps of Stream header1");
730                         gst_buffer_unmap(header1, &map);
731                         return MEDIA_PACKET_ERROR_INVALID_PARAMETER;
732                 }
733                 gst_buffer_unmap(header1, &map);
734
735                 /* hsize2 : Comment Header (packet type 3) - variable byte */
736                 header2 = gst_buffer_new_and_alloc(hsize2);
737                 gst_buffer_fill(header2, 0,  (buf_data + (hsize1)), hsize2);
738                 gst_buffer_map(header2, &map, GST_MAP_READ);
739                 tmp_buf = map.data;
740                 /* '0x03' + 'vorbis' */
741                 if (*tmp_buf != 0x03) {
742                         LOGE("[ERROR] Invalid Caps of Stream header2");
743                         gst_buffer_unmap(header2, &map);
744                         return MEDIA_PACKET_ERROR_INVALID_PARAMETER;
745                 }
746                 gst_buffer_unmap(header2, &map);
747
748                 /* hsize3 : Setup Header (packet type 5) - variable byte */
749                 hsize3 = streamheader_size - (hsize1 + hsize2);
750                 header3 = gst_buffer_new_and_alloc(hsize3);
751                 gst_buffer_fill(header3, 0,  (buf_data + (hsize1 + hsize2)), hsize3);
752                 gst_buffer_map(header3, &map, GST_MAP_READ);
753                 tmp_buf = map.data;
754                 /* '0x05' + 'vorbis' */
755                 if (*tmp_buf != 0x05) {
756                         LOGE("[ERROR] Invalid Caps of Stream header3");
757                         gst_buffer_unmap(header3, &map);
758                         return MEDIA_PACKET_ERROR_INVALID_PARAMETER;
759                 }
760                 gst_buffer_unmap(header3, &map);
761
762                 LOGD("[vorbis] streamheader hsize1 (%d) + hsize2 (%d) + hsize3 (%d) = Total (%d)",
763                                 hsize1, hsize2, hsize3, (hsize1 + hsize2 + hsize3));
764
765                 __mc_gst_caps_set_buffer_array(core->caps, "streamheader", header1, header2, header3, NULL);
766
767                 gst_buffer_unref(header1);
768                 gst_buffer_unref(header2);
769                 gst_buffer_unref(header3);
770         } else if (core->codec_id == MEDIACODEC_FLAC) {
771                 /*
772                  * hsize1 : Stream Info (type 0) - fixed 51 byte
773                  * hsize2 : Stream Comment (type 4) - variable byte (need calculate)
774                  */
775
776                 /* First of all, Need to fins and calculate size of hsize2 */
777                 tmp_header = gst_buffer_new_and_alloc(streamheader_size);
778                 gst_buffer_fill(tmp_header, 0, buf_data, streamheader_size);
779                 gst_buffer_map(tmp_header, &map, GST_MAP_READ);
780                 tmp_buf = map.data;
781                 hsize2 = 4 + ((tmp_buf[52] << 16) | (tmp_buf[53] << 8) | (tmp_buf[54]));
782                 LOGD("Find streamheader hsize2(%d)", hsize2);
783                 gst_buffer_unmap(tmp_header, &map);
784                 gst_buffer_unref(tmp_header);
785
786                 /*  hsize1 :  Stream Info (type 0) - fixed 51 byte */
787                 hsize1 = 51;
788                 header1 = gst_buffer_new_and_alloc(hsize1);
789                 gst_buffer_fill(header1, 0, buf_data, hsize1);
790                 gst_buffer_map(header1, &map, GST_MAP_READ);
791                 tmp_buf = map.data;
792                 /* '0x7f' + 'FLAC' */
793                 if (*tmp_buf != 0x07f) {
794                         LOGE("[ERROR] Invalid Caps of Stream header1 (Info)");
795                         gst_buffer_unmap(header1, &map);
796                         gst_buffer_unref(header1);
797                         return MEDIA_PACKET_ERROR_INVALID_PARAMETER;
798                 }
799                 gst_buffer_unmap(header1, &map);
800
801                 /* hsize2 : Stream Comment (type 4) - variable byte (need calculate) */
802                 header2 = gst_buffer_new_and_alloc(hsize2);
803                 gst_buffer_fill(header2, 0, (buf_data + (hsize1)), hsize2);
804                 gst_buffer_map(header2, &map, GST_MAP_READ);
805                 tmp_buf = map.data;
806                 /* '0x84' */
807                 if (*tmp_buf != 0x84) {
808                         LOGE("[ERROR] Invalid Caps of Stream header2 (Comment)");
809                         gst_buffer_unmap(header2, &map);
810                         gst_buffer_unref(header1);
811                         gst_buffer_unref(header2);
812                         return MEDIA_PACKET_ERROR_INVALID_PARAMETER;
813                 }
814                 gst_buffer_unmap(header2, &map);
815
816                 LOGD("[flac] streamheader hsize1 (%d) + hsize2 (%d)  = Total (%d)", hsize1, hsize2, (hsize1 + hsize2));
817                 __mc_gst_caps_set_buffer_array(core->caps, "streamheader", header1, header2, NULL);
818                 gst_buffer_unref(header1);
819                 gst_buffer_unref(header2);
820         } else {
821                 LOGE("Not support case of Stream header Caps");
822         }
823
824         /* Update gstbuffer's data ptr and size for using previous streamheader.*/
825         LOGD("BEFORE : mcbuffer->buffer of size %" G_GSIZE_FORMAT "", gst_buffer_get_size(mcbuffer->buffer));
826         gst_buffer_remove_memory_range(mcbuffer->buffer, streamheader_size, -1);
827         gst_buffer_set_size(mcbuffer->buffer, buf_size - streamheader_size);
828         LOGD("AFTER  : mcbuffer->buffer of size %" G_GSIZE_FORMAT "",  gst_buffer_get_size(mcbuffer->buffer));
829
830         return ret;
831 }
832
833 int __mc_set_caps_codecdata(mc_gst_core_t *core, GstMCBuffer *mcbuffer, guint fixed_size)
834 {
835         gint ret = MC_ERROR_NONE;
836         uint32_t codec_data_size = 0;
837         GstMapInfo map;
838         GstBuffer *codecdata_buffer;
839         guint8 *data = NULL;
840         void *codec_data = NULL;
841
842         media_packet_get_codec_data(mcbuffer->packet, &codec_data, &codec_data_size);
843
844         if (codec_data != NULL) {               /* get the codec data from media_packet_get_codec_data() */
845                 data = g_malloc0(codec_data_size);
846                 memcpy(data, codec_data, codec_data_size);
847                 codecdata_buffer = gst_buffer_new_wrapped(data, codec_data_size);
848                 gst_caps_set_simple(core->caps, "codec_data", GST_TYPE_BUFFER, codecdata_buffer, NULL);
849                 LOGD("set codec data : %" G_GSIZE_FORMAT "", gst_buffer_get_size(codecdata_buffer));
850                 gst_buffer_unref(codecdata_buffer);
851         } else {                                        /* get the codec data from media_packet_get_buffer_data_ptr() */
852                 uint64_t buffer_size = 0;
853                 void *buff_data = NULL;
854
855                 media_packet_get_buffer_size(mcbuffer->packet, &buffer_size);
856                 media_packet_get_buffer_data_ptr(mcbuffer->packet, &buff_data);
857
858                 /* For audio, it's necessary to change the offset value of buffer after extracting csd */
859                 /* it's not necessary to set csd to caps in case of annex-b */
860                 if (buffer_size > fixed_size) {
861                         data = g_malloc0(fixed_size);
862                         gst_buffer_map(mcbuffer->buffer, &map, GST_MAP_READ);
863                         memcpy(data, map.data, fixed_size);
864                         codecdata_buffer = gst_buffer_new_wrapped(data, fixed_size);
865                         gst_caps_set_simple(core->caps, "codec_data", GST_TYPE_BUFFER, codecdata_buffer, NULL);
866                         gst_buffer_unref(codecdata_buffer);
867
868                         gst_buffer_replace_memory(mcbuffer->buffer, 0,
869                                         gst_memory_new_wrapped(GST_MEMORY_FLAG_READONLY, map.data + fixed_size , buffer_size - fixed_size, 0,
870                                                 buffer_size - fixed_size, mcbuffer, (GDestroyNotify)__mc_input_buffer_finalize_cb));
871
872                         LOGD("after : set codec data from packet: %" G_GSIZE_FORMAT "",  gst_buffer_get_size(mcbuffer->buffer));
873                         gst_buffer_unmap(mcbuffer->buffer, &map);
874                 }
875         }
876         return ret;
877 }
878
879 int _mc_output_media_packet_new(mc_gst_core_t *core, bool video, bool encoder, media_format_mimetype_e out_mime)
880 {
881         if (media_format_create(&core->output_fmt) != MEDIA_FORMAT_ERROR_NONE) {
882                 LOGE("media format create failed");
883                 return MC_ERROR;
884         }
885         mc_gst_port_def_t *port_def = &core->ports[out_port_index]->port_def;
886
887         if (video) {
888                 media_format_set_video_mime(core->output_fmt, out_mime);
889                 media_format_set_video_width(core->output_fmt, port_def->info.video.width);
890                 media_format_set_video_height(core->output_fmt, port_def->info.video.height);
891
892                 if (encoder)
893                         media_format_set_video_avg_bps(core->output_fmt, port_def->info.video.bitrate);
894         } else {
895                 media_format_set_audio_mime(core->output_fmt, out_mime);
896                 media_format_set_audio_channel(core->output_fmt, port_def->info.audio.channel);
897                 media_format_set_audio_samplerate(core->output_fmt, port_def->info.audio.samplerate);
898                 media_format_set_audio_bit(core->output_fmt, port_def->info.audio.bit_depth);
899
900                 if (encoder)
901                         media_format_set_audio_avg_bps(core->output_fmt, port_def->info.audio.bitrate);
902         }
903
904         return MC_ERROR_NONE;
905 }
906
907 /*
908  * mc_gst_core functions
909  */
910 static mc_gst_core_t *mc_gst_core_new()
911 {
912         mc_gst_core_t *core;
913
914         MEDIACODEC_FENTER();
915
916         core = g_new0(mc_gst_core_t, 1);
917
918         /* 0 : input, 1 : output */
919         core->ports[0] = mc_gst_port_new(core);
920         core->ports[0]->index = 0;
921         core->ports[1] = mc_gst_port_new(core);
922         core->ports[1]->index = 1;
923
924         core->available_queue = g_new0(mc_aqueue_t, 1);
925         core->available_queue->input = mc_async_queue_new();
926
927         g_mutex_init(&core->eos_mutex);
928         g_cond_init(&core->eos_cond);
929         g_cond_init(&core->buffer_cond);
930         g_cond_init(&core->out_buffer_cond);
931         g_mutex_init(&core->prepare_lock);
932         g_mutex_init(&core->drain_lock);
933
934         core->need_drain = false;
935         core->need_feed = false;
936         core->eos = false;
937         core->need_codec_data = false;
938         core->need_sync_flag = false;
939         core->unprepare_flag = false;
940         core->prepare_count = 0;
941         core->etb_count = 0;
942         core->codec_data = NULL;
943
944         g_atomic_int_set(&core->available_queue->running, 1);
945         core->available_queue->thread = g_thread_new("feed thread", &feed_task, core);
946         core->allocator = gst_tizen_allocator_new();
947
948         core->bufmgr = NULL;
949         core->drm_fd = -1;
950         LOGD("gst_core(%p) is created", core);
951
952         MEDIACODEC_FLEAVE();
953
954         return core;
955 }
956
957 static void mc_gst_core_free(mc_gst_core_t *core)
958 {
959         MEDIACODEC_FENTER();
960
961         mc_aqueue_t *async_queue;
962
963         async_queue = core->available_queue;
964
965         mc_async_queue_disable(async_queue->input);
966
967         g_atomic_int_set(&async_queue->running, 0);
968         g_thread_join(async_queue->thread);
969
970         g_mutex_clear(&core->eos_mutex);
971         g_mutex_clear(&core->prepare_lock);
972         g_mutex_clear(&core->drain_lock);
973         g_cond_clear(&core->eos_cond);
974         g_cond_clear(&core->buffer_cond);
975         g_cond_clear(&core->out_buffer_cond);
976
977         mc_async_queue_free(async_queue->input);
978         g_free(async_queue);
979
980         if (core->allocator) {
981                 gst_object_unref(core->allocator);
982                 core->allocator = NULL;
983         }
984
985         if (core->codec_data)
986                 gst_buffer_unref(core->codec_data);
987
988         if (core->ports[0] != NULL) {
989                 mc_gst_port_free(core->ports[0]);
990                 core->ports[0] = NULL;
991         }
992
993         if (core->ports[1] != NULL) {
994                 mc_gst_port_free(core->ports[1]);
995                 core->ports[1] = NULL;
996         }
997
998         LOGD("gst_core(%p) destroyed", core);
999         g_free(core);
1000
1001         MEDIACODEC_FLEAVE();
1002 }
1003
1004 /*
1005  * mc_gst_port functions
1006  */
1007 static mc_gst_port_t *mc_gst_port_new(mc_gst_core_t *core)
1008 {
1009         MEDIACODEC_FENTER();
1010
1011         mc_gst_port_t *port;
1012
1013         port = g_new0(mc_gst_port_t, 1);
1014         port->core = core;
1015         port->num_buffers = -1;
1016         port->buffer_size = 0;
1017         port->is_allocated = 0;
1018
1019         g_mutex_init(&port->mutex);
1020         port->queue = g_queue_new();
1021
1022         LOGD("gst_port(%p) created", port);
1023
1024         MEDIACODEC_FLEAVE();
1025         return port;
1026 }
1027
1028 static void mc_gst_port_free(mc_gst_port_t *port)
1029 {
1030         MEDIACODEC_FENTER();
1031
1032         g_mutex_clear(&port->mutex);
1033         g_queue_free(port->queue);
1034
1035         LOGD("gst_port(%p) freed", port);
1036         g_free(port);
1037
1038         MEDIACODEC_FLEAVE();
1039
1040         return;
1041 }
1042
1043 gboolean _mc_update_packet_info(mc_gst_core_t *core, media_format_h format)
1044 {
1045         gboolean is_format_change = FALSE;
1046
1047         mc_gst_port_def_t *input_port_def = &core->ports[in_port_index]->port_def;
1048
1049         if (core->video) {
1050                 gint width = 0;
1051                 gint height = 0;
1052                 gint bitrate = 0;
1053                 gint framerate = 0;
1054                 gchar *sformat = NULL;
1055                 media_format_mimetype_e mimetype = 0;
1056
1057                 media_format_get_video_info(format, &mimetype, &width, &height, &bitrate, NULL);
1058                 media_format_get_video_frame_rate(format, &framerate);
1059
1060                 is_format_change |= input_port_def->info.video.width != width;
1061                 is_format_change |= input_port_def->info.video.height != height;
1062                 is_format_change |= ((input_port_def->info.video.framerate != framerate) && (framerate != 0));
1063                 if (is_format_change) {
1064                         LOGD("Format changed : resolution %dx%d -> %dx%d, framerate %d -> %d",
1065                                 input_port_def->info.video.width, input_port_def->info.video.height, width, height,
1066                                 input_port_def->info.video.framerate, framerate);
1067                         input_port_def->info.video.width = width;
1068                         input_port_def->info.video.height = height;
1069                 }
1070
1071                 if (core->encoder) {
1072                         sformat = __mc_get_gst_input_format(mimetype, core->is_hw);
1073                         is_format_change |= ((input_port_def->info.video.bitrate != bitrate * 1000) && (bitrate != 0));
1074                         is_format_change |= g_strcmp0(input_port_def->info.video.format, sformat);
1075                         if (is_format_change) {
1076                                 LOGD("Bitrate changed : %d -> %d", input_port_def->info.video.bitrate, bitrate);
1077                                 LOGD("Format changed : %s -> %s", input_port_def->info.video.format, sformat);
1078                                 input_port_def->info.video.bitrate = bitrate * 1000;
1079                                 input_port_def->info.video.format = g_strdup(sformat);
1080                         }
1081                 }
1082         } else {
1083                 gint channel;
1084                 gint samplerate;
1085                 gint bit;
1086                 gint bitrate;
1087
1088                 media_format_get_audio_info(format, NULL, &channel, &samplerate, &bit, &bitrate);
1089
1090                 is_format_change |= input_port_def->info.audio.channel != channel;
1091                 is_format_change |= input_port_def->info.audio.samplerate != samplerate;
1092                 is_format_change |= input_port_def->info.audio.bit_depth != bit;
1093                 if (is_format_change) {
1094                         LOGD("Audio info. changed : %d -> %d, %d -> %d, %d -> %d",
1095                                 input_port_def->info.audio.channel, channel,
1096                                 input_port_def->info.audio.samplerate, samplerate,
1097                                 input_port_def->info.audio.bit_depth, bit);
1098                         input_port_def->info.audio.channel = channel;
1099                         input_port_def->info.audio.samplerate = samplerate;
1100                         input_port_def->info.audio.bit_depth = bit;
1101                 }
1102
1103                 if (core->encoder) {
1104                         is_format_change |= ((input_port_def->info.audio.bitrate != bitrate * 1000) && (bitrate != 0));
1105                         if (is_format_change) {
1106                                 LOGD("Bitrate changed : %d -> %d", input_port_def->info.audio.bitrate, bitrate);
1107                                 input_port_def->info.audio.bitrate = bitrate * 1000;
1108                         }
1109                 }
1110         }
1111
1112         return is_format_change;
1113 }
1114
1115 static int _mc_set_codec_data(mc_gst_core_t *core, GstMCBuffer *mcbuffer, bool codec_config)
1116 {
1117         MEDIACODEC_FENTER();
1118
1119         g_return_val_if_fail(core != NULL, MC_PARAM_ERROR);
1120         g_return_val_if_fail(mcbuffer != NULL, MC_PARAM_ERROR);
1121
1122         int ret = MC_ERROR_NONE;
1123
1124         switch (core->codec_id) {
1125         case MEDIACODEC_AAC:
1126         case MEDIACODEC_AAC_HE:
1127         case MEDIACODEC_AAC_HE_PS:
1128                 if (codec_config) {
1129                         ret = __mc_set_caps_codecdata(core, mcbuffer, AAC_CODECDATA_SIZE);
1130                         if (ret != MC_ERROR_NONE)
1131                                 LOGW("__mc_set_caps_codecdata failed");
1132                 }
1133                 break;
1134         case MEDIACODEC_WMAV1:
1135         case MEDIACODEC_WMAV2:
1136         case MEDIACODEC_WMAPRO:
1137         case MEDIACODEC_WMALSL:
1138                 if (codec_config) {
1139                         ret = __mc_set_caps_codecdata(core, mcbuffer, WMA_CODECDATA_SIZE);
1140                         if (ret != MC_ERROR_NONE)
1141                                 LOGW("__mc_set_caps_codecdata failed");
1142                 }
1143                 break;
1144         case MEDIACODEC_VORBIS:
1145         case MEDIACODEC_FLAC:
1146                 if (codec_config) {
1147                         ret = __mc_set_caps_streamheader(core, mcbuffer, VORBIS_CODECDATA_SIZE);
1148                         if (ret != MC_ERROR_NONE)
1149                                 LOGW("__mc_set_caps_streamheader failed");
1150                 }
1151                 break;
1152         case MEDIACODEC_H264:
1153         case MEDIACODEC_MPEG4:
1154                 ret = __mc_set_caps_codecdata(core, mcbuffer, CODECDATA_NOT_USE);
1155                 if (ret != MC_ERROR_NONE)
1156                         LOGW("__mc_set_caps_codecdata failed");
1157                 break;
1158         default:
1159                 LOGD("doesn't need to set codecdata");
1160                 break;
1161         }
1162
1163         MEDIACODEC_FLEAVE();
1164         return ret;
1165
1166
1167 }
1168
1169 static gpointer feed_task(gpointer data)
1170 {
1171         gint ret = MC_ERROR_NONE;
1172         bool codec_config = FALSE;
1173         bool eos = FALSE;
1174         bool initiative = TRUE;
1175         bool is_format_change = FALSE;
1176         media_format_h format = NULL;
1177         media_packet_h input_buffer = NULL;
1178         GstMCBuffer *mcbuffer = NULL;
1179
1180         MEDIACODEC_FENTER();
1181
1182         mc_gst_core_t *core = (mc_gst_core_t *)data;
1183
1184         while (g_atomic_int_get(&core->available_queue->running)) {
1185                 LOGD("waiting for next input....");
1186                 input_buffer = _mc_get_input_buffer(core);
1187                 if (!input_buffer)
1188                         goto LEAVE;
1189
1190                 media_packet_get_format(input_buffer,  &format);
1191                 if (format) {
1192                         if (_mc_update_packet_info(core, format))
1193                                 is_format_change = TRUE;
1194                         media_format_unref(format);
1195                 }
1196
1197                 if (media_packet_is_codec_config(input_buffer, &codec_config) != MEDIA_PACKET_ERROR_NONE) {
1198                         LOGE("media_packet_is_codec_config failed");
1199                         goto ERROR;
1200                 }
1201
1202                 if (media_packet_is_end_of_stream(input_buffer, &eos) != MEDIA_PACKET_ERROR_NONE) {
1203                         LOGE("media_packet_is_end_of_stream failed");
1204                         goto ERROR;
1205                 }
1206                 /*
1207                  * receive an eos packet. For decoder + encoder case, encoder always get tbm surface plane first from input data,
1208                  * but eos packet only has flag not valid data, so we don't put it to downstream.
1209                  */
1210                 if (eos) {
1211                         LOGD("end of stream");
1212                         gst_app_src_end_of_stream(GST_APP_SRC(core->appsrc));
1213                         _mc_wait_for_eos(core);
1214                         initiative = TRUE;
1215
1216                         _mc_gst_handle_input_buffer_used(core, input_buffer);
1217                         goto LEAVE;
1218                 }
1219
1220                 mcbuffer = _mc_gst_media_packet_to_gstbuffer(core, input_buffer, codec_config);
1221                 if (!mcbuffer) {
1222                         LOGW("gstbuffer can't make");
1223                         goto ERROR;
1224                 }
1225                 /* mcbuffer took the ownership of the input_buffer, so set input_buffer to NULL to aviod double unref */
1226                 input_buffer = NULL;
1227
1228                 if (codec_config || initiative) {
1229                         GstPad *pad = NULL;
1230                         if (!core->encoder) {
1231                                 ret = _mc_set_codec_data(core, mcbuffer, codec_config);
1232                                 if (ret != MC_ERROR_NONE) {
1233                                         LOGE("failed to set codec data");
1234                                         gst_buffer_unref(mcbuffer->buffer);
1235                                         goto ERROR;
1236                                 }
1237                         }
1238                         pad = gst_element_get_static_pad(core->appsrc, "src");
1239                         gst_pad_push_event(pad, gst_event_new_stream_start("start"));
1240                         gst_object_unref(pad);
1241                         is_format_change = TRUE;
1242                         initiative = FALSE;
1243                         core->offset = GST_BUFFER_OFFSET_NONE;
1244                 }
1245
1246                 if (is_format_change) {
1247                         GstCaps *caps;
1248                         caps = core->mc_caps_new(core, core->codec_id, in_port_index);
1249                         g_object_set(core->appsrc, "caps", caps, NULL);
1250                         gst_caps_unref(caps);
1251                         LOGD("caps updated");
1252                         is_format_change = FALSE;
1253                 }
1254
1255
1256                 /* inject buffer */
1257                 ret = _mc_gst_gstbuffer_to_appsrc(core, mcbuffer);
1258                 if (ret != GST_FLOW_OK) {
1259                         LOGE("Failed to push gst buffer");
1260                         goto ERROR;
1261                 }
1262
1263                 continue;
1264 ERROR:
1265                 if (input_buffer)
1266                         _mc_gst_handle_input_buffer_used(core, input_buffer);
1267
1268                 _mc_gst_set_flush_input(core);
1269
1270                 if (core->user_cb[_MEDIACODEC_EVENT_TYPE_ERROR]) {
1271                         ((mc_error_cb) core->user_cb[_MEDIACODEC_EVENT_TYPE_ERROR])
1272                                 (MC_INTERNAL_ERROR, core->user_data[_MEDIACODEC_EVENT_TYPE_ERROR]);
1273                 }
1274
1275                 continue;
1276 LEAVE:
1277                 /*LOGE("status : input_buffer : %p, codec_config : %d, eos : %d, encoder : %d in feed_task", input_buffer, codec_config, eos, core->encoder);*/
1278                 continue;
1279
1280         }
1281
1282         LOGI("status : input_buffer : %p, codec_config : %d, eos : %d, video : %d, encoder : %d in feed_task",
1283                         input_buffer, codec_config, eos, core->video, core->encoder);
1284         LOGD("feed task finished %p v(%d)e(%d)", core, core->video, core->encoder);
1285
1286         MEDIACODEC_FLEAVE();
1287
1288         return NULL;
1289 }
1290
1291 static void __mc_gst_stop_feed(GstElement *pipeline, gpointer data)
1292 {
1293         mc_gst_core_t *core = (mc_gst_core_t *)data;
1294
1295         LOGI("stop_feed called");
1296         if (core->user_cb[_MEDIACODEC_EVENT_TYPE_BUFFER_STATUS]) {
1297                 ((mc_buffer_status_cb) core->user_cb[_MEDIACODEC_EVENT_TYPE_BUFFER_STATUS])
1298                         (MEDIACODEC_ENOUGH_DATA, core->user_data[_MEDIACODEC_EVENT_TYPE_BUFFER_STATUS]);
1299         }
1300 }
1301
1302 static void __mc_gst_start_feed(GstElement *pipeline, guint size, gpointer data)
1303 {
1304         mc_gst_core_t *core = (mc_gst_core_t *)data;
1305
1306         LOGI("start_feed called");
1307
1308         if (core->user_cb[_MEDIACODEC_EVENT_TYPE_BUFFER_STATUS]) {
1309                 ((mc_buffer_status_cb) core->user_cb[_MEDIACODEC_EVENT_TYPE_BUFFER_STATUS])
1310                         (MEDIACODEC_NEED_DATA, core->user_data[_MEDIACODEC_EVENT_TYPE_BUFFER_STATUS]);
1311         }
1312 }
1313
1314 GstCaps *_mc_gst_aud_caps_new(mc_gst_core_t *core, mediacodec_codec_type_e codec_id, gint index)
1315 {
1316         GstCaps *caps = NULL;
1317         mc_gst_port_def_t *port_def = &core->ports[index]->port_def;
1318
1319         caps = gst_caps_new_simple(core->mime,
1320                 "rate", G_TYPE_INT, port_def->info.audio.samplerate,
1321                 "channels", G_TYPE_INT, port_def->info.audio.channel, NULL);
1322
1323         switch (codec_id) {
1324         case MEDIACODEC_AAC:
1325                 if (core->encoder) {
1326                         gst_caps_set_simple(caps,
1327                                 "format", G_TYPE_STRING, _mc_bit_to_string(port_def->info.audio.bit_depth),
1328                                 "layout", G_TYPE_STRING, "interleaved", NULL);
1329                         g_object_set(GST_OBJECT(core->codec), "compliance", -2, NULL);
1330                 } else {
1331                         gst_caps_set_simple(caps,
1332                                 "framed", G_TYPE_BOOLEAN, TRUE,
1333                                 "mpegversion", G_TYPE_INT, 4,
1334                                 "stream-format", G_TYPE_STRING, "adts",
1335                                 NULL);
1336                         LOGD("CAPS for codec_id (MEDIACODEC_AAC_LC - normal ADTS)");
1337                 }
1338                 break;
1339         case MEDIACODEC_AAC_HE:
1340         case MEDIACODEC_AAC_HE_PS:
1341                 if (core->encoder) {
1342                         LOGD("[MC_NOT_SUPPORTED] he-aac-v12 encoder is not supported yet!!!");
1343                         return NULL;
1344                 } else {
1345                         gst_caps_set_simple(caps,
1346                                 "mpegversion", G_TYPE_INT, 4,
1347                                 "framed", G_TYPE_BOOLEAN, TRUE,
1348                                 "stream-format", G_TYPE_STRING, "raw",
1349                                 NULL);
1350                         LOGD("CAPS for codec_id (MEDIACODEC_AAC_HE)");
1351                 }
1352                 break;
1353         case MEDIACODEC_MP3:
1354                 if (core->encoder) {
1355                         LOGD("[MC_NOT_SUPPORTED] mp3 encoder is not supported yet!!!");
1356                         return NULL;
1357                 } else {
1358                         gst_caps_set_simple(caps,
1359                                 "framed", G_TYPE_BOOLEAN, TRUE,
1360                                 "mpegversion", G_TYPE_INT, 1,       /* To-Do : plz check */
1361                                 "mpegaudioversion", G_TYPE_INT, 1,  /* To-Do : plz check */
1362                                 "layer", G_TYPE_INT, 3,             /* To-Do : plz check */
1363                                 NULL);
1364                         LOGD("CAPS for codec_id (MEDIACODEC_MP3)");
1365                 }
1366                 break;
1367         case MEDIACODEC_AMR_NB:
1368                 if (core->encoder) {
1369                         gst_caps_set_simple(caps,
1370                                 "format", G_TYPE_STRING, _mc_bit_to_string(port_def->info.audio.bit_depth),
1371                                 "layout", G_TYPE_STRING, "interleaved",
1372                                 NULL);
1373                         LOGD("CAPS for codec_id (MEDIACODEC_AMR_NB)");
1374                 } else {
1375                         gst_caps_set_simple(caps,
1376                                 "rate", G_TYPE_INT, 8000,
1377                                 NULL);
1378                 }
1379                 break;
1380         case MEDIACODEC_AMR_WB:
1381                 if (core->encoder) {
1382                         LOGD("[MC_NOT_SUPPORTED] amr-wb encoder is not supported yet!!!");
1383                         return NULL;
1384                 } else {
1385                         gst_caps_set_simple(caps,
1386                                 "rate", G_TYPE_INT, 16000,
1387                                 NULL);
1388                 }
1389                 break;
1390         case MEDIACODEC_VORBIS:
1391                 if (core->encoder) {
1392                         LOGD("[MC_NOT_SUPPORTED] vorbis encoder is not supported yet!!!");
1393                         return NULL;
1394                 } else {
1395                 }
1396                 break;
1397         case MEDIACODEC_FLAC:
1398                 if (core->encoder) {
1399                         LOGD("[MC_NOT_SUPPORTED] flac encoder is not supported yet!!!");
1400                         return NULL;
1401                 } else {
1402                         gst_caps_set_simple(caps,
1403                                 "framed", G_TYPE_BOOLEAN, TRUE,
1404                                 /* FIXME - Insert More Info */
1405                                 NULL);
1406                 }
1407                 break;
1408         case MEDIACODEC_WMAV1:
1409         case MEDIACODEC_WMAV2:
1410         case MEDIACODEC_WMAPRO:
1411         case MEDIACODEC_WMALSL:
1412                 if (core->encoder) {
1413                         LOGD("[MC_NOT_SUPPORTED] wma encoder is not supported yet!!!");
1414                         return NULL;
1415                 } else {
1416                         /*
1417                          * Need to extract from Stream Type Specific ... or
1418                          * Need to get Demuxer's Caps from Stream Type Specific
1419                          */
1420                         guint16 format_tag = 0;
1421                         gint wma_version = 0;
1422                         gint block_align = 1024;      /*FIXME : Need checking */
1423                         gint bitrate = 128000;        /*FIXME : Need checking */
1424
1425                         LOGD(" ----- WMA Need Additional Caps -----------");
1426                         if (core->codec_id == MEDIACODEC_WMAV1) {
1427                                 format_tag = 352;       /* 0x160 */
1428                                 wma_version = 1;
1429                         } else if (core->codec_id == MEDIACODEC_WMAV2) {
1430                                 format_tag = 353;       /* 0x161 */
1431                                 wma_version = 2;
1432                         } else if (core->codec_id == MEDIACODEC_WMAPRO) {
1433                                 format_tag = 354;       /* 0x162 */
1434                                 wma_version = 3;
1435                         } else if (core->codec_id == MEDIACODEC_WMALSL) {
1436                                 format_tag = 355;       /* 0x163 */
1437                                 wma_version = 3;
1438                         } else {
1439                                 LOGE("Not support WMA format");
1440                         }
1441
1442                         gst_caps_set_simple(caps,
1443                                 "bitrate", G_TYPE_INT, bitrate,
1444                                 "depth", G_TYPE_INT, port_def->info.audio.bit_depth,
1445                                 /* FIXME - Need More Info */
1446                                 "wmaversion", G_TYPE_INT, wma_version,
1447                                 "format_tag", G_TYPE_INT, format_tag,
1448                                 "block_align", G_TYPE_INT, block_align,
1449                                 NULL);
1450                 }
1451                 break;
1452         default:
1453                 break;
1454         }
1455         return caps;
1456 }
1457
1458 GstCaps *_mc_gst_vid_caps_new(mc_gst_core_t *core, mediacodec_codec_type_e codec_id, gint index)
1459 {
1460         GstCaps *caps = NULL;
1461         mc_gst_port_def_t *port_def = &core->ports[index]->port_def;
1462
1463         caps = gst_caps_new_simple(core->mime,
1464                 "width", G_TYPE_INT, port_def->info.video.width,
1465                 "height", G_TYPE_INT, port_def->info.video.height,
1466                 "framerate", GST_TYPE_FRACTION, port_def->info.video.framerate, 1,
1467                 NULL);
1468
1469         switch (codec_id) {
1470         case MEDIACODEC_H263:
1471                 if (core->encoder) {
1472                         gst_caps_set_simple(caps,
1473                                 "format", G_TYPE_STRING, port_def->info.video.format,
1474                                 NULL);
1475                 } else {
1476                         gst_caps_set_simple(caps,
1477                                 "mpegversion", G_TYPE_INT, 4,
1478                                 "framerate", GST_TYPE_FRACTION, 30, 1,
1479                                 NULL);
1480                 }
1481                 break;
1482         case MEDIACODEC_MPEG4:
1483                 if (core->encoder) {
1484                         gst_caps_set_simple(caps,
1485                                 "format", G_TYPE_STRING, port_def->info.video.format,
1486                                 NULL);
1487                 } else {
1488                         gst_caps_set_simple(caps,
1489                                 "mpegversion", G_TYPE_INT, 4,
1490                                 "systemstream", G_TYPE_BOOLEAN, false,
1491                                 "parsed", G_TYPE_BOOLEAN, TRUE,
1492                                 "framerate", GST_TYPE_FRACTION, 30, 1,
1493                                 NULL);
1494                 }
1495                 break;
1496         case MEDIACODEC_H264:
1497                 if (core->encoder) {
1498                         gst_caps_set_simple(caps,
1499                                 "format", G_TYPE_STRING, port_def->info.video.format,
1500                                 NULL);
1501                         g_object_set(GST_OBJECT(core->codec), "byte-stream", TRUE, NULL);
1502                         LOGE("format : %s", port_def->info.video.format);
1503                 } else {
1504                         gst_caps_set_simple(caps,
1505                                 "parsed", G_TYPE_BOOLEAN, TRUE,                                         /* FIXME different from sw */
1506                                 "alignment", G_TYPE_STRING, "au",
1507                                 "stream-format", G_TYPE_STRING, "byte-stream",
1508                                 "framerate", GST_TYPE_FRACTION, 30, 1,
1509                                 NULL);
1510                 }
1511                 break;
1512         default:
1513                 break;
1514         }
1515
1516         return caps;
1517 }
1518
1519 static int _mc_link_vtable(mc_gst_core_t *core, mediacodec_codec_type_e id, gboolean encoder, gboolean is_hw)
1520 {
1521         MEDIACODEC_FENTER();
1522
1523         g_return_val_if_fail(core != NULL, MC_PARAM_ERROR);
1524
1525         switch (id) {
1526         case MEDIACODEC_AAC:
1527                 if (encoder) {
1528                         core->vtable = aenc_aac_vtable;
1529                 } else {
1530                         core->vtable = adec_aac_vtable;
1531                 }
1532                 break;
1533         case MEDIACODEC_AAC_HE:
1534         case MEDIACODEC_AAC_HE_PS:
1535                 if (encoder) {
1536                         LOGD("[MC_NOT_SUPPORTED] he-aac-v12 encoder is not supported yet!!!");
1537                         return MC_NOT_SUPPORTED;
1538                 } else {
1539                         core->vtable = adec_aacv12_vtable;
1540                 }
1541                 break;
1542         case MEDIACODEC_MP3:
1543                 if (encoder) {
1544                         LOGD("[MC_NOT_SUPPORTED] mp3 encoder is not supported yet!!!");
1545                         return MC_NOT_SUPPORTED;
1546                 } else {
1547                         core->vtable = adec_mp3_vtable;
1548                 }
1549                 break;
1550         case MEDIACODEC_AMR_NB:
1551                 LOGD("amrnb vtable");
1552                 if (encoder) {
1553                         core->vtable = aenc_amrnb_vtable;
1554                 } else {
1555                         core->vtable = adec_amrnb_vtable;
1556                 }
1557                 break;
1558         case MEDIACODEC_AMR_WB:
1559                 LOGD("amrwb vtable - Only support decoder");
1560                 if (encoder) {
1561                         LOGD("[MC_NOT_SUPPORTED] amr-wb encoder is not supported yet!!!");
1562                         return MC_NOT_SUPPORTED;
1563                 } else {
1564                         core->vtable = adec_amrwb_vtable;
1565                 }
1566                 break;
1567         case MEDIACODEC_VORBIS:
1568                 LOGD("vorbis vtable");
1569                 if (encoder) {
1570                         LOGD("[MC_NOT_SUPPORTED] vorbis encoder is not supported yet!!!");
1571                         return MC_NOT_SUPPORTED;
1572                 } else {
1573                         core->vtable = adec_vorbis_vtable;
1574                 }
1575                 break;
1576         case MEDIACODEC_FLAC:
1577                 LOGD("flac vtable");
1578                 if (encoder) {
1579                         LOGD("[MC_NOT_SUPPORTED] flac encoder is not supported yet!!!");
1580                         return MC_NOT_SUPPORTED;
1581                 } else {
1582                         core->vtable = adec_flac_vtable;
1583                 }
1584                 break;
1585         case MEDIACODEC_WMAV1:
1586         case MEDIACODEC_WMAV2:
1587         case MEDIACODEC_WMAPRO:
1588         case MEDIACODEC_WMALSL:
1589                 LOGD("wma(V1 / V2 / LSL / PRO) vtable");
1590                 if (encoder) {
1591                         LOGD("[MC_NOT_SUPPORTED] wma encoder is not supported yet!!!");
1592                         return MC_NOT_SUPPORTED;
1593                 } else {
1594                         core->vtable = adec_wma_vtable;
1595                 }
1596                 break;
1597         case MEDIACODEC_H263:
1598                 LOGD("h263 vtable");
1599                 if (encoder) {
1600                         core->vtable = is_hw ? venc_h263_hw_vtable : venc_h263_sw_vtable;
1601                 } else {
1602                         if (is_hw) {
1603                                 core->vtable = vdec_h263_hw_vtable;
1604                         } else {
1605                                 core->vtable = vdec_h263_sw_vtable;
1606                         }
1607                 }
1608                 break;
1609         case MEDIACODEC_MPEG4:
1610                 LOGD("mpeg4 vtable");
1611                 if (encoder) {
1612                         core->vtable = is_hw ? venc_mpeg4_hw_vtable : venc_mpeg4_sw_vtable;
1613                 } else {
1614                         core->vtable = is_hw ? vdec_mpeg4_hw_vtable : vdec_mpeg4_sw_vtable;
1615                 }
1616                 break;
1617         case MEDIACODEC_H264:
1618                 LOGD("h264 vtable");
1619                 if (encoder) {
1620                         core->vtable = is_hw ? venc_h264_hw_vtable : venc_vtable;
1621                 } else {
1622                         if (is_hw) {
1623                                 core->vtable = vdec_h264_hw_vtable;
1624                         } else {
1625                                 core->vtable = vdec_h264_sw_vtable;
1626                         }
1627                 }
1628                 break;
1629         default:
1630                 break;
1631         }
1632
1633         MEDIACODEC_FLEAVE();
1634
1635         return MC_ERROR_NONE;
1636 }
1637
1638 static gint _mc_gst_gstbuffer_to_appsrc(mc_gst_core_t *core, GstMCBuffer *mcbuffer)
1639 {
1640         return gst_app_src_push_buffer(GST_APP_SRC(core->appsrc), mcbuffer->buffer);
1641 }
1642
1643 media_packet_h _mc_get_input_buffer(mc_gst_core_t *core)
1644 {
1645         LOGD("waiting for input...");
1646         return mc_async_queue_pop(core->available_queue->input);
1647 }
1648
1649 mc_ret_e mc_gst_prepare(mc_handle_t *mc_handle)
1650 {
1651         MEDIACODEC_FENTER();
1652
1653         int ret = MC_ERROR_NONE;
1654         media_format_mimetype_e out_mime;
1655         int num_supported_codec = 0;
1656         int i = 0;
1657         GstCaps *caps = NULL;
1658
1659         if (!mc_handle)
1660                 return MC_PARAM_ERROR;
1661
1662         mediacodec_codec_type_e id;
1663         bool video;
1664         bool encoder;
1665         bool hardware;
1666         gchar *factory_name = NULL;
1667         mc_codec_map_t *codec_map;
1668
1669         id = mc_handle->codec_id;
1670         video = mc_handle->is_video;
1671         encoder = mc_handle->is_encoder;
1672         hardware = mc_handle->is_hw;
1673         codec_map = encoder ? mc_handle->encoder_map : mc_handle->decoder_map;
1674         num_supported_codec = encoder ? mc_handle->num_supported_encoder : mc_handle->num_supported_decoder;
1675
1676         for (i = 0; i < num_supported_codec; i++) {
1677                 if ((id == codec_map[i].id) && (hardware == codec_map[i].hardware))
1678                         break;
1679         }
1680
1681         if (i == num_supported_codec)
1682                 return MC_NOT_SUPPORTED;
1683
1684         factory_name = codec_map[i].type.factory_name;
1685         out_mime = codec_map[i].type.out_format;
1686
1687         /* gst_core create */
1688         mc_gst_core_t *new_core = mc_gst_core_new();
1689
1690         new_core->mime = codec_map[i].type.mime;
1691         new_core->is_hw = hardware;
1692         new_core->eos = false;
1693         new_core->encoder = encoder;
1694         new_core->video = video;
1695         memcpy(&new_core->ports[in_port_index]->port_def.info, &mc_handle->info, sizeof(mc_handle->info));
1696         memcpy(&new_core->ports[out_port_index]->port_def.info, &mc_handle->info, sizeof(mc_handle->info));
1697         new_core->ports[out_port_index]->port_def.coding_type = out_mime;
1698         new_core->codec_id = id;
1699         new_core->mc_caps_new = video ? &_mc_gst_vid_caps_new : &_mc_gst_aud_caps_new;
1700
1701         new_core->bufmgr = tbm_bufmgr_init(new_core->drm_fd);
1702         if (new_core->bufmgr == NULL) {
1703                 LOGE("TBM initialization failed");
1704                 return MC_ERROR;
1705         }
1706
1707         LOGD("@%p(%p) core is initializing...v(%d)e(%d)", mc_handle, new_core, new_core->video, new_core->encoder);
1708         LOGD("factory name : %s, output_fmt : %x", factory_name, out_mime);
1709
1710         /* create media_packet for output fmt */
1711         ret = _mc_output_media_packet_new(new_core, video, encoder, out_mime);
1712         if (ret != MC_ERROR_NONE) {
1713                 LOGE("Failed to create output pakcet");
1714                 return ret;
1715         }
1716
1717         for (i = 0; i < _MEDIACODEC_EVENT_TYPE_INTERNAL_FILLBUFFER ; i++) {
1718                 LOGD("copy cb function [%d]", i);
1719                 if (mc_handle->user_cb[i]) {
1720                         new_core->user_cb[i] = mc_handle->user_cb[i];
1721                         new_core->user_data[i] = mc_handle->user_data[i];
1722                         LOGD("user_cb[%d] %p, %p", i, new_core->user_cb[i], mc_handle->user_cb[i]);
1723                 }
1724         }
1725
1726         /* create basic core elements */
1727         ret = _mc_gst_create_pipeline(new_core, factory_name);
1728         if (ret != MC_ERROR_NONE) {
1729                 LOGE("failed to create pipeline");
1730                 return ret;
1731         }
1732
1733         /* link vtable */
1734         ret = _mc_link_vtable(new_core, id, encoder, hardware);
1735         if (ret != MC_ERROR_NONE) {
1736                 LOGE("vtable link failed");
1737                 return ret;
1738         }
1739
1740         /* set caps in capsfilter as default*/
1741         caps = new_core->mc_caps_new(new_core, id, out_port_index);
1742
1743         new_core->caps = caps;
1744         /* FIXME will parse input format from ini. format is needed when linking elements*/
1745         if (new_core->video && new_core->encoder) {
1746                 gchar *format = NULL;
1747
1748                 format = new_core->is_hw ? "SN12" : "I420";
1749                 gst_caps_set_simple(caps,
1750                         "format", G_TYPE_STRING, format,
1751                         NULL);
1752         }
1753         g_object_set(new_core->capsfilter, "caps", caps, NULL);
1754         gst_caps_unref(caps);
1755
1756         mc_handle->core = new_core;
1757         LOGD("initialized... %d", ret);
1758         MEDIACODEC_FLEAVE();
1759
1760         return ret;
1761 }
1762
1763 mc_ret_e mc_gst_unprepare(mc_handle_t *mc_handle)
1764 {
1765         MEDIACODEC_FENTER();
1766
1767         int i;
1768         int ret = MC_ERROR_NONE;
1769         mc_gst_core_t *core = NULL;
1770
1771         if (!mc_handle)
1772                 return MC_PARAM_ERROR;
1773
1774         core = (mc_gst_core_t *)mc_handle->core;
1775
1776         if (core) {
1777                 LOGD("@%p(%p) core is uninitializing... v(%d)e(%d)", mc_handle, core, core->video, core->encoder);
1778
1779                 g_mutex_lock(&core->drain_lock);
1780                 core->unprepare_flag = TRUE;
1781                 g_mutex_unlock(&core->drain_lock);
1782
1783                 if (core->eos)
1784                         _mc_send_eos_signal(core);
1785
1786                 _mc_gst_set_flush_input(core);
1787
1788                 ret = _mc_gst_destroy_pipeline(core);
1789
1790                 /* unset callback */
1791                 for (i = 0; i < _MEDIACODEC_EVENT_TYPE_INTERNAL_FILLBUFFER; i++) {
1792                         LOGD("unset cb function [%d]", i);
1793                         if (mc_handle->user_cb[i]) {
1794                                 core->user_cb[i] = NULL;
1795                                 core->user_data[i] = NULL;
1796                                 LOGD("user_cb[%d] %p, %p", i, core->user_cb[i], mc_handle->user_cb[i]);
1797                         }
1798                 }
1799
1800                 media_format_unref(core->output_fmt);
1801
1802                 if (core->bufmgr != NULL) {
1803                         tbm_bufmgr_deinit(core->bufmgr);
1804                         core->bufmgr = NULL;
1805                 }
1806
1807                 if (core->drm_fd != -1) {
1808                         close(core->drm_fd);
1809                         LOGD("close drm_fd");
1810                 }
1811
1812                 if (core != NULL) {
1813                         mc_gst_core_free(core);
1814                         mc_handle->core = NULL;
1815                 }
1816         }
1817
1818         MEDIACODEC_FLEAVE();
1819
1820         return ret;
1821 }
1822
1823 mc_ret_e mc_gst_process_input(mc_handle_t *mc_handle, media_packet_h inbuf, uint64_t timeout_us)
1824 {
1825         MEDIACODEC_FENTER();
1826
1827         int ret = MC_ERROR_NONE;
1828         mc_gst_core_t *core = NULL;
1829
1830         if (!mc_handle)
1831                 return MC_PARAM_ERROR;
1832
1833         core = (mc_gst_core_t *)mc_handle->core;
1834
1835         g_mutex_lock(&core->drain_lock);
1836
1837         while (g_atomic_int_get(&core->etb_count) == MAXINUM_QNUM) {
1838                 LOGD("Waiting until packets are drained..");
1839                 core->need_drain = true;
1840                 g_cond_wait(&core->buffer_cond, &core->drain_lock);
1841         }
1842         g_mutex_unlock(&core->drain_lock);
1843
1844         if (core->prepare_count == 0)
1845                 return MC_INVALID_STATUS;
1846
1847         g_mutex_lock(&core->drain_lock);
1848
1849         if (!core->eos || !core->unprepare_flag) {
1850                 mc_async_queue_push(core->available_queue->input, inbuf);
1851
1852         } else {
1853                 ret = MC_INVALID_IN_BUF;
1854                 g_mutex_unlock(&core->drain_lock);
1855                 return ret;
1856         }
1857
1858         g_mutex_unlock(&core->drain_lock);
1859         g_atomic_int_inc(&core->etb_count);
1860         LOGI("@v(%d)e(%d)process_input(%d): %p", core->video, core->encoder, core->etb_count, inbuf);
1861
1862         MEDIACODEC_FLEAVE();
1863
1864         return ret;
1865 }
1866
1867 mc_ret_e mc_gst_get_output(mc_handle_t *mc_handle, media_packet_h *outbuf, uint64_t timeout_us)
1868 {
1869         gint ret = MC_ERROR_NONE;
1870         gint64 end_time = -1;
1871         gboolean signalled;
1872         mc_gst_core_t *core = NULL;
1873         media_packet_h out_packet = NULL;
1874
1875         MEDIACODEC_FENTER();
1876         end_time = g_get_monotonic_time() + timeout_us;
1877
1878         if (!mc_handle)
1879                 return MC_PARAM_ERROR;
1880
1881         core = (mc_gst_core_t *)mc_handle->core;
1882
1883         g_mutex_lock(&core->ports[1]->mutex);
1884
1885         if (!g_queue_is_empty(core->ports[1]->queue))
1886                 signalled = TRUE;
1887         else {
1888                 LOGD("no buffer, wait for signal");
1889                 signalled = g_cond_wait_until(&core->out_buffer_cond, &core->ports[1]->mutex, end_time);
1890         }
1891
1892         if (signalled)
1893                 out_packet = g_queue_pop_head(core->ports[1]->queue);
1894         else {
1895                 LOGW("Failed to get buffer");
1896                 ret = MC_OUTPUT_BUFFER_EMPTY;
1897         }
1898
1899         *outbuf = out_packet;
1900         LOGI("@%p v(%d)e(%d) got buffer :%p", core, core->video, core->encoder, out_packet);
1901
1902         g_mutex_unlock(&core->ports[1]->mutex);
1903
1904         MEDIACODEC_FLEAVE();
1905
1906         return ret;
1907 }
1908
1909 mc_ret_e mc_gst_flush_buffers(mc_handle_t *mc_handle)
1910 {
1911         gint ret = MC_ERROR_NONE;
1912         mc_gst_core_t *core = NULL;
1913
1914         MEDIACODEC_FENTER();
1915
1916         if (!mc_handle)
1917                 return MC_PARAM_ERROR;
1918
1919         core = (mc_gst_core_t *)mc_handle->core;
1920         LOGI("@%p v(%d)e(%d) flush_buffers", core, core->video, core->encoder);
1921
1922         ret = _mc_gst_flush_buffers(core);
1923
1924         MEDIACODEC_FLEAVE();
1925
1926         return ret;
1927 }
1928
1929 static gboolean __mc_gst_init_gstreamer()
1930 {
1931         MEDIACODEC_FENTER();
1932
1933         gint i = 0;
1934         gint arg_count = 0;
1935         static gboolean initialized = FALSE;
1936         static const int max_argc = 50;
1937         gint *argc = NULL;
1938         gchar **argv = NULL;
1939         gchar **argv2 = NULL;
1940         GError *err = NULL;
1941
1942         if (initialized) {
1943                 LOGD("gstreamer already initialized.\n");
1944                 return TRUE;
1945         }
1946
1947         /* alloc */
1948         argc = malloc(sizeof(int));
1949         argv = malloc(sizeof(gchar *) *max_argc);
1950         argv2 = malloc(sizeof(gchar *) *max_argc);
1951
1952         if (!argc || !argv || !argv2)
1953                 goto ERROR;
1954
1955         memset(argv, 0, sizeof(gchar *) *max_argc);
1956         memset(argv2, 0, sizeof(gchar *) *max_argc);
1957
1958         /* add initial */
1959         *argc = 1;
1960         argv[0] = g_strdup("media codec");
1961
1962         /* we would not do fork for scanning plugins */
1963         argv[*argc] = g_strdup("--gst-disable-registry-fork");
1964         (*argc)++;
1965
1966         /* check disable registry scan */
1967         argv[*argc] = g_strdup("--gst-disable-registry-update");
1968         (*argc)++;
1969
1970         /* check disable segtrap */
1971         argv[*argc] = g_strdup("--gst-disable-segtrap");
1972         (*argc)++;
1973
1974         LOGD("initializing gstreamer with following parameter\n");
1975         LOGD("argc : %d\n", *argc);
1976         arg_count = *argc;
1977
1978         for (i = 0; i < arg_count; i++) {
1979                 argv2[i] = argv[i];
1980                 LOGD("argv[%d] : %s\n", i, argv2[i]);
1981         }
1982
1983         /* initializing gstreamer */
1984         if (!gst_init_check(argc, &argv, &err)) {
1985                 LOGE("Could not initialize GStreamer: %s\n", err ? err->message : "unknown error occurred");
1986                 if (err)
1987                         g_error_free(err);
1988
1989                 goto ERROR;
1990         }
1991
1992         /* release */
1993         for (i = 0; i < arg_count; i++)
1994                 MC_FREEIF(argv2[i]);
1995
1996         MC_FREEIF(argv);
1997         MC_FREEIF(argv2);
1998         MC_FREEIF(argc);
1999
2000         /* done */
2001         initialized = TRUE;
2002
2003         MEDIACODEC_FLEAVE();
2004         return TRUE;
2005
2006 ERROR:
2007         /* release */
2008         for (i = 0; i < arg_count; i++) {
2009                 LOGD("free[%d] : %s\n", i, argv2[i]);
2010                 MC_FREEIF(argv2[i]);
2011         }
2012
2013         MC_FREEIF(argv);
2014         MC_FREEIF(argv2);
2015         MC_FREEIF(argc);
2016
2017         return FALSE;
2018 }
2019
2020 mc_ret_e _mc_gst_create_pipeline(mc_gst_core_t *core, gchar *factory_name)
2021 {
2022         GstBus *bus = NULL;
2023
2024         MEDIACODEC_FENTER();
2025
2026         g_mutex_lock(&core->prepare_lock);
2027         if (core->prepare_count == 0) {
2028
2029                 if (!__mc_gst_init_gstreamer()) {
2030                         LOGE("gstreamer initialize fail");
2031                         g_mutex_unlock(&core->prepare_lock);
2032                         return MC_NOT_INITIALIZED;
2033                 }
2034                 core->codec = gst_element_factory_make(factory_name, NULL);
2035
2036                 if (!core->codec) {
2037                         LOGE("codec element create fail");
2038                         goto ERROR;
2039                 }
2040
2041                 LOGD("@%p v(%d)e(%d) create_pipeline", core, core->video, core->encoder);
2042                 MEDIACODEC_ELEMENT_SET_STATE(core->codec, GST_STATE_READY);
2043
2044                 /* create common elements */
2045                 core->pipeline = gst_pipeline_new(NULL);
2046
2047                 if (!core->pipeline) {
2048                         LOGE("pipeline create fail");
2049                         goto ERROR;
2050                 }
2051
2052                 core->appsrc = gst_element_factory_make("appsrc", NULL);
2053
2054                 if (!core->appsrc) {
2055                         LOGE("appsrc can't create");
2056                         goto ERROR;
2057                 }
2058
2059                 if (!core->video && core->encoder) {
2060                         core->audioconvert = gst_element_factory_make("audioconvert", NULL);
2061
2062                         if (!core->audioconvert) {
2063                                 LOGE("capsfilter can't create");
2064                                 goto ERROR;
2065                         }
2066
2067                         core->audioresample = gst_element_factory_make("audioresample", NULL);
2068
2069                         if (!core->audioresample) {
2070                                 LOGE("capsfilter can't create");
2071                                 goto ERROR;
2072                         }
2073                 }
2074
2075                 if (core->video && core->encoder) {
2076                         core->videoconvert = gst_element_factory_make("videoconvert", NULL);
2077
2078                         if (!core->videoconvert) {
2079                                 LOGE("videoconvert can't create");
2080                                 goto ERROR;
2081                         }
2082
2083                         core->videoscale = gst_element_factory_make("videoscale", NULL);
2084                         if (!core->videoscale) {
2085                                 LOGE("videoscale can't create");
2086                                 goto ERROR;
2087                         }
2088                 }
2089
2090                 core->capsfilter = gst_element_factory_make("capsfilter", NULL);
2091
2092                 if (!core->capsfilter) {
2093                         LOGE("capsfilter can't create");
2094                         goto ERROR;
2095                 }
2096
2097                 core->fakesink = gst_element_factory_make("fakesink", NULL);
2098
2099                 if (!core->fakesink) {
2100                         LOGE("fakesink create fail");
2101                         goto ERROR;
2102                 }
2103                 g_object_set(core->fakesink, "enable-last-sample", FALSE, NULL);
2104
2105                 /*__mc_link_elements(core);*/
2106                 if (!core->video && core->encoder) {
2107                         gst_bin_add_many(GST_BIN(core->pipeline), core->appsrc, core->audioresample, core->audioconvert, core->capsfilter, core->codec, core->fakesink, NULL);
2108
2109                         /* link elements */
2110                         if (!(gst_element_link_many(core->appsrc, core->audioresample, core->audioconvert, core->capsfilter, core->codec, core->fakesink, NULL))) {
2111                                 LOGE("gst_element_link_many is failed");
2112                                 goto ERROR;
2113                         }
2114                 } else if (core->video && core->encoder) {
2115                         gst_bin_add_many(GST_BIN(core->pipeline), core->appsrc, core->capsfilter, core->codec, core->fakesink, NULL);
2116
2117                         /* link elements */
2118                         if (!(gst_element_link_many(core->appsrc, core->codec, core->fakesink, NULL))) {
2119                                 LOGE("gst_element_link_many is failed");
2120                                 goto ERROR;
2121                         }
2122                 } else {
2123                         gst_bin_add_many(GST_BIN(core->pipeline), core->appsrc, core->capsfilter, core->codec, core->fakesink, NULL);
2124
2125                         /* link elements */
2126                         if (!(gst_element_link_many(core->appsrc, core->capsfilter, core->codec, core->fakesink, NULL))) {
2127                                 LOGE("gst_element_link_many is failed");
2128                                 goto ERROR;
2129                         }
2130                 }
2131
2132                 /* connect signals, bus watcher */
2133                 bus = gst_pipeline_get_bus(GST_PIPELINE(core->pipeline));
2134                 core->bus_watch_id = gst_bus_add_watch(bus, __mc_gst_bus_callback, core);
2135                 core->thread_default = g_main_context_get_thread_default();
2136
2137                 /* set sync handler to get tag synchronously */
2138                 gst_bus_set_sync_handler(bus, __mc_gst_bus_sync_callback, core, NULL);
2139                 gst_object_unref(GST_OBJECT(bus));
2140
2141                 /* app src */
2142                 g_signal_connect(core->appsrc, "need-data", G_CALLBACK(__mc_gst_start_feed), core);
2143                 g_signal_connect(core->appsrc, "enough-data", G_CALLBACK(__mc_gst_stop_feed), core);
2144
2145                 /* connect handoff */
2146                 g_object_set(GST_OBJECT(core->fakesink), "signal-handoffs", TRUE, NULL);
2147                 core->signal_handoff = g_signal_connect(core->fakesink, "handoff", G_CALLBACK(__mc_gst_buffer_add), core);
2148
2149                 /* set state PLAYING */
2150                 MEDIACODEC_ELEMENT_SET_STATE(GST_ELEMENT_CAST(core->pipeline), GST_STATE_PLAYING);
2151
2152
2153         }
2154         core->prepare_count++;
2155         g_mutex_unlock(&core->prepare_lock);
2156
2157         MEDIACODEC_FLEAVE();
2158
2159         return MC_ERROR_NONE;
2160
2161 STATE_CHANGE_FAILED:
2162 ERROR:
2163
2164         if (core->codec)
2165                 gst_object_unref(GST_OBJECT(core->codec));
2166
2167         if (core->pipeline)
2168                 gst_object_unref(GST_OBJECT(core->pipeline));
2169
2170         if (core->appsrc)
2171                 gst_object_unref(GST_OBJECT(core->appsrc));
2172
2173         if (core->capsfilter)
2174                 gst_object_unref(GST_OBJECT(core->capsfilter));
2175
2176         if (core->audioresample)
2177                 gst_object_unref(GST_OBJECT(core->audioresample));
2178
2179         if (core->videoscale)
2180                 gst_object_unref(GST_OBJECT(core->videoscale));
2181
2182         if (core->videoconvert)
2183                 gst_object_unref(GST_OBJECT(core->videoconvert));
2184
2185         if (core->audioconvert)
2186                 gst_object_unref(GST_OBJECT(core->audioconvert));
2187
2188         if (core->fakesink)
2189                 gst_object_unref(GST_OBJECT(core->fakesink));
2190
2191         g_mutex_unlock(&core->prepare_lock);
2192
2193         return MC_ERROR;
2194 }
2195
2196 mc_ret_e _mc_gst_destroy_pipeline(mc_gst_core_t *core)
2197 {
2198         int ret = MC_ERROR_NONE;
2199
2200         MEDIACODEC_FENTER();
2201
2202         g_mutex_lock(&core->prepare_lock);
2203         core->prepare_count--;
2204         if (core->prepare_count == 0) {
2205
2206                 if (core->pipeline) {
2207                         /* disconnect signal */
2208                         if (core->fakesink && GST_IS_ELEMENT(core->fakesink)) {
2209                                 if (g_signal_handler_is_connected(core->fakesink, core->signal_handoff)) {
2210                                         g_signal_handler_disconnect(core->fakesink, core->signal_handoff);
2211                                         LOGD("handoff signal destroy");
2212                                 }
2213                         }
2214
2215                         if (core->bus_watch_id) {
2216                                 GSource *source = NULL;
2217                                 source = g_main_context_find_source_by_id(core->thread_default, core->bus_watch_id);
2218                                 g_source_destroy(source);
2219                                 LOGD("bus_watch_id destroy");
2220                         }
2221
2222                         MEDIACODEC_ELEMENT_SET_STATE(core->pipeline, GST_STATE_NULL);
2223
2224                         gst_object_unref(GST_OBJECT(core->pipeline));
2225                         if (core->caps) {
2226                                 gst_caps_unref(core->caps);
2227                                 core->caps = NULL;
2228                         }
2229                 }
2230         }
2231
2232         LOGD("@%p v(%d)e(%d) destroy_pipeline : %d ", core, core->video, core->encoder, core->prepare_count);
2233         g_mutex_unlock(&core->prepare_lock);
2234
2235         MEDIACODEC_FLEAVE();
2236
2237         return ret;
2238
2239 STATE_CHANGE_FAILED:
2240         if (core->pipeline)
2241                 gst_object_unref(GST_OBJECT(core->pipeline));
2242
2243         LOGD("@%p v(%d)e(%d) destroy_pipeline failed", core, core->video, core->encoder);
2244         g_mutex_unlock(&core->prepare_lock);
2245
2246         return MC_ERROR;
2247 }
2248
2249 void __mc_push_output_to_queue(mc_gst_core_t *core, GstMCBuffer *mcbuffer)
2250 {
2251         g_mutex_lock(&core->ports[1]->mutex);
2252
2253         /* push it to output buffer queue */
2254         g_queue_push_tail(core->ports[1]->queue, mcbuffer->packet);
2255         g_cond_broadcast(&core->out_buffer_cond);
2256
2257         if (mcbuffer->buffer) {
2258                 LOGD("dq : v(%d)e(%d) TIMESTAMP = %"GST_TIME_FORMAT " DURATION = %"GST_TIME_FORMAT, core->video, core->encoder,
2259                         GST_TIME_ARGS(GST_BUFFER_TIMESTAMP(mcbuffer->buffer)), GST_TIME_ARGS(GST_BUFFER_DURATION(mcbuffer->buffer)));
2260         } else {
2261                 LOGD("dq : v(%d)e(%d)", core->video, core->encoder);
2262         }
2263
2264         g_mutex_unlock(&core->ports[1]->mutex);
2265
2266         if (core->user_cb[_MEDIACODEC_EVENT_TYPE_FILLBUFFER]) {
2267                 ((mc_fill_buffer_cb) core->user_cb[_MEDIACODEC_EVENT_TYPE_FILLBUFFER])
2268                         (mcbuffer->packet, core->user_data[_MEDIACODEC_EVENT_TYPE_FILLBUFFER]);
2269         }
2270 }
2271
2272 void __mc_gst_buffer_add(GstElement *element, GstBuffer *buffer, GstPad *pad, gpointer data)
2273 {
2274         GstMCBuffer *mcbuffer = NULL;
2275
2276         MEDIACODEC_FENTER();
2277
2278         mc_gst_core_t *core = (mc_gst_core_t *)data;
2279
2280         gst_buffer_ref(buffer);
2281
2282         mcbuffer = __mc_gst_make_media_packet(core, buffer);
2283
2284         LOGI("@%p(%d) out_packet : %p", core, core->encoder, mcbuffer->packet);
2285
2286         if (mcbuffer->packet) {
2287                 media_packet_set_pts(mcbuffer->packet, GST_BUFFER_TIMESTAMP(buffer));
2288                 media_packet_set_dts(mcbuffer->packet, GST_BUFFER_DTS(buffer));
2289                 media_packet_set_duration(mcbuffer->packet, GST_BUFFER_DURATION(buffer));
2290
2291                 if (core->need_codec_data) {
2292                         media_packet_set_flags(mcbuffer->packet, MEDIA_PACKET_CODEC_CONFIG);
2293                         core->need_codec_data = false;
2294                 }
2295
2296                 if (core->need_sync_flag) {
2297                         media_packet_set_flags(mcbuffer->packet, MEDIA_PACKET_SYNC_FRAME);
2298                         core->need_sync_flag = false;
2299                 }
2300
2301                 __mc_push_output_to_queue(core, mcbuffer);
2302         } else {
2303                 gst_buffer_unref(buffer);
2304                 LOGE("Failed to dequeue output packet");
2305         }
2306
2307         MEDIACODEC_FLEAVE();
2308
2309         return;
2310 }
2311
2312 int __mc_output_buffer_finalize_cb(media_packet_h packet, int error_code, void *user_data)
2313 {
2314         MEDIACODEC_FENTER();
2315
2316         GstMCBuffer *mcbuffer = (GstMCBuffer *)user_data;
2317
2318         gst_buffer_unref((GstBuffer *)mcbuffer->buffer);
2319
2320         if (mcbuffer->ext_mem) {
2321                 g_free(mcbuffer->ext_mem);
2322                 LOGD("freed external memory");
2323                 mcbuffer->ext_mem = NULL;
2324         }
2325
2326         LOGD("output port filled buffer : %p", packet);
2327         g_free(mcbuffer);
2328
2329         MEDIACODEC_FLEAVE();
2330
2331         return MEDIA_PACKET_FINALIZE;
2332 }
2333
2334 gchar *__mc_get_gst_input_format(media_format_mimetype_e mimetype, bool is_hw)
2335 {
2336         gchar *format = NULL;
2337
2338         switch (mimetype) {
2339         case MEDIA_FORMAT_I420:
2340                 format = "I420";
2341                 break;
2342         case MEDIA_FORMAT_NV12:
2343                 if (is_hw)
2344                         format = "SN12";
2345                 else
2346                         format = "NV12";
2347                 break;
2348         case MEDIA_FORMAT_ARGB:
2349                 format = "ARGB";
2350                 break;
2351         default:
2352                 break;
2353         }
2354         LOGD("input packet format : %s", format);
2355         return format;
2356 }
2357
2358 GstMCBuffer *_mc_gst_media_packet_to_gstbuffer(mc_gst_core_t *core, media_packet_h packet, bool codec_config)
2359 {
2360         gint ret = MEDIA_PACKET_ERROR_NONE;
2361         uint64_t buf_size = 0;
2362         uint64_t pts = 0;
2363         uint64_t dts = 0;
2364         uint64_t dur = 0;
2365         void *buf_data = NULL;
2366         GstMCBuffer *mcbuffer = NULL;
2367
2368         ret = media_packet_get_buffer_size(packet, &buf_size);
2369         if (ret != MEDIA_PACKET_ERROR_NONE) {
2370                 LOGW("buffer size get fail");
2371                 return NULL;
2372         }
2373
2374         ret = media_packet_get_buffer_data_ptr(packet, &buf_data);
2375         if (ret != MEDIA_PACKET_ERROR_NONE) {
2376                 LOGW("buffer data get fail");
2377                 return NULL;
2378         }
2379
2380         mcbuffer = gst_mediacodec_buffer_new(core, packet, buf_size);
2381         if (mcbuffer == NULL) {
2382                 LOGW("failed to create inbuf");
2383                 return NULL;
2384         }
2385
2386         LOGD("packet :v(%d)e(%d) %p, buf_size : %d", core->video, core->encoder, packet, (int)buf_size);
2387
2388         ret = __mc_fill_input_buffer(core, packet, mcbuffer);
2389         if (ret != MC_ERROR_NONE) {
2390                 LOGW("failed to fill inbuf: %s (ox%08x)", _mc_error_to_string(ret), ret);
2391                 /*
2392                  * if __mc_fill_input_buffer() failed, no memory appended to mcbuffer->buffer, so gst_mediacodec_buffer_finalize()
2393                  * will not be invoked
2394                  */
2395                 gst_buffer_unref(mcbuffer->buffer);
2396                 g_free(mcbuffer);
2397                 return NULL;
2398         }
2399
2400         /* dts */
2401         media_packet_get_pts(packet, &dts);
2402         GST_BUFFER_DTS(mcbuffer->buffer) = dts;
2403
2404         /* pts */
2405         media_packet_get_pts(packet, &pts);
2406         GST_BUFFER_PTS(mcbuffer->buffer) = pts;
2407
2408         /* duration */
2409         media_packet_get_duration(packet, &dur);
2410         GST_BUFFER_DURATION(mcbuffer->buffer) = dur;
2411
2412         GST_BUFFER_OFFSET(mcbuffer->buffer) = core->offset;
2413
2414         core->offset += buf_size;
2415         GST_BUFFER_OFFSET_END(mcbuffer->buffer) = core->offset;
2416
2417         return mcbuffer;
2418 }
2419
2420 GstMCBuffer *__mc_gst_make_media_packet(mc_gst_core_t *core, GstBuffer *buffer)
2421 {
2422         gint ret = MEDIA_PACKET_ERROR_NONE;
2423         GstMemory *mem;
2424         GstMapInfo map = GST_MAP_INFO_INIT;
2425         GstMCBuffer *mcbuffer = NULL;
2426         void *data = NULL;
2427         gint size = 0;
2428
2429
2430         mcbuffer = (GstMCBuffer *)g_malloc0(sizeof(*mcbuffer));
2431
2432         if (mcbuffer == NULL) {
2433                 LOGE("malloc fail");
2434                 return NULL;
2435         }
2436
2437         mem = gst_buffer_peek_memory(buffer, 0);
2438
2439         if (gst_is_tizen_memory(mem)) {
2440                 data = (void *)gst_tizen_memory_get_surface(mem);
2441                 size = tbm_surface_internal_get_size((tbm_surface_h)data);
2442         } else {
2443                 gst_memory_map(mem, &map, GST_MAP_READ);
2444                 data = map.data;
2445                 size = map.size;
2446                 gst_memory_unmap(mem, &map);
2447         }
2448
2449         mcbuffer->buffer = buffer;
2450         mcbuffer->core = core;
2451         mcbuffer->buf_size = size;
2452
2453         ret = __mc_fill_output_buffer(core, data, mcbuffer->buf_size,  mcbuffer);
2454         if (ret != MC_ERROR_NONE) {
2455                 LOGW("failed to fill outbuf: %s (ox%08x)", _mc_error_to_string(ret), ret);
2456                 return NULL;
2457         }
2458
2459         return mcbuffer;
2460 }
2461
2462 int __mc_gst_create_eos_packet(media_format_h fmt, GstMCBuffer *mcbuffer)
2463 {
2464         gint ret = MC_ERROR_NONE;
2465         media_packet_h packet = NULL;
2466
2467         MEDIACODEC_FENTER();
2468         if (media_packet_create(fmt, __mc_output_buffer_finalize_cb, mcbuffer, &packet)) {
2469                 LOGE("media_packet_create failed\n");
2470                 ret = MC_ERROR;
2471                 goto ERROR;
2472         }
2473
2474         if (media_packet_set_flags(packet, MEDIA_PACKET_END_OF_STREAM)) {
2475                 LOGE("unable to set EOS flag\n");
2476                 ret = MC_ERROR;
2477                 goto ERROR;
2478         }
2479
2480         mcbuffer->buffer = NULL;
2481         mcbuffer->ext_mem = NULL;
2482         mcbuffer->buf_size = 0;
2483         mcbuffer->packet = packet;
2484         MEDIACODEC_FLEAVE();
2485
2486         return ret;
2487 ERROR:
2488         if (packet)
2489                 media_packet_destroy(packet);
2490         MEDIACODEC_FLEAVE();
2491         return ret;
2492 }
2493
2494 void _mc_gst_handle_input_buffer_used(mc_gst_core_t *core, media_packet_h packet)
2495 {
2496         if (core) {
2497                 g_atomic_int_dec_and_test(&core->etb_count);
2498                 LOGD("@v(%d)e(%d) input port used(%d)----- ", core->video, core->encoder, core->etb_count);
2499         }
2500
2501         if (core && core->user_cb[_MEDIACODEC_EVENT_TYPE_EMPTYBUFFER]) {
2502                 ((mc_empty_buffer_cb)core->user_cb[_MEDIACODEC_EVENT_TYPE_EMPTYBUFFER])
2503                         (packet, core->user_data[_MEDIACODEC_EVENT_TYPE_EMPTYBUFFER]);
2504
2505                 g_mutex_lock(&core->drain_lock);
2506
2507                 if (core->need_drain) {
2508                         LOGD("@v(%d)e(%d) input port used----- single process_input.....", core->video, core->encoder);
2509                         core->need_drain = false;
2510                         g_cond_signal(&core->buffer_cond);
2511                 }
2512                 g_mutex_unlock(&core->drain_lock);
2513         }
2514 }
2515
2516 gboolean __mc_gst_bus_callback(GstBus *bus, GstMessage *msg, gpointer data)
2517 {
2518         gint ret  = MC_ERROR_NONE;
2519         GstMCBuffer *mcbuffer = NULL;
2520
2521         mc_gst_core_t *core = (mc_gst_core_t *)data;
2522         LOGI("@%p v(%d)e(%d)bus message : %s", core, core->video, core->encoder, gst_message_type_get_name(GST_MESSAGE_TYPE(msg)));
2523
2524         switch (GST_MESSAGE_TYPE(msg)) {
2525
2526         case GST_MESSAGE_EOS:
2527                 _mc_send_eos_signal(core);
2528
2529                 mcbuffer = (GstMCBuffer *)g_malloc0(sizeof(GstMCBuffer));
2530                 if (__mc_gst_create_eos_packet(core->output_fmt, mcbuffer) == MC_ERROR_NONE) {
2531                         __mc_push_output_to_queue(core, mcbuffer);
2532                         LOGD("send eos packet.");
2533                 } else {
2534                         LOGE("failed to create eos packet.");
2535                         g_free(mcbuffer);
2536                 }
2537
2538                 if (core->user_cb[_MEDIACODEC_EVENT_TYPE_EOS]) {
2539                         LOGD("eos callback invoked");
2540                         ((mc_eos_cb) core->user_cb[_MEDIACODEC_EVENT_TYPE_EOS])(core->user_data[_MEDIACODEC_EVENT_TYPE_EOS]);
2541                 }
2542
2543                 LOGD("End of stream\n");
2544                 break;
2545
2546         case GST_MESSAGE_ERROR:
2547         {
2548                 GError *error = NULL;
2549
2550                 gst_message_parse_error(msg, &error, NULL);
2551
2552                 if (!error) {
2553                         LOGW("GST error message parsing failed");
2554                         break;
2555                 }
2556
2557                 if (error) {
2558                         if (error->domain == GST_STREAM_ERROR)
2559                                 ret = __gst_handle_stream_error(core, error, msg);
2560                         else if (error->domain == GST_RESOURCE_ERROR)
2561                                 ret = __gst_handle_resource_error(core, error->code);
2562                         else if (error->domain == GST_LIBRARY_ERROR)
2563                                 ret = __gst_handle_library_error(core, error->code);
2564                         else if (error->domain == GST_CORE_ERROR)
2565                                 ret = __gst_handle_core_error(core, error->code);
2566                         else
2567                                 LOGW("Unexpected error has occured");
2568
2569                 g_mutex_lock(&core->drain_lock);
2570
2571                 if (core->need_drain) {
2572                         LOGD("@v(%d)e(%d) input port used----- single process_input.....", core->video, core->encoder);
2573                         core->need_drain = false;
2574                         g_cond_signal(&core->buffer_cond);
2575                 }
2576
2577                 g_mutex_unlock(&core->drain_lock);
2578
2579                         if (core->user_cb[_MEDIACODEC_EVENT_TYPE_ERROR]) {
2580                                 ((mc_error_cb) core->user_cb[_MEDIACODEC_EVENT_TYPE_ERROR])
2581                                         (ret, core->user_data[_MEDIACODEC_EVENT_TYPE_ERROR]);
2582                                         LOGW("Error : %s (ox%08x)", _mc_error_to_string(ret), ret);
2583                         }
2584                 }
2585                 g_error_free(error);
2586         }
2587                 break;
2588
2589         default:
2590                 break;
2591         }
2592
2593         return TRUE;
2594 }
2595
2596 static gboolean __mc_gst_check_useful_message(mc_gst_core_t *core, GstMessage *msg)
2597 {
2598         gboolean retval = FALSE;
2599
2600         if (!core->pipeline) {
2601                 LOGE("mediacodec pipeline handle is null");
2602                 return TRUE;
2603         }
2604
2605         switch (GST_MESSAGE_TYPE(msg)) {
2606         case GST_MESSAGE_TAG:
2607         case GST_MESSAGE_EOS:
2608         case GST_MESSAGE_ERROR:
2609         case GST_MESSAGE_WARNING:
2610                 retval = TRUE;
2611                 break;
2612         default:
2613                 retval = FALSE;
2614                 break;
2615         }
2616
2617         return retval;
2618 }
2619
2620 static GstBusSyncReply __mc_gst_bus_sync_callback(GstBus *bus, GstMessage *msg, gpointer data)
2621 {
2622         mc_gst_core_t *core = (mc_gst_core_t *)data;
2623         GstBusSyncReply reply = GST_BUS_DROP;
2624
2625         LOGD("__mc_gst_bus_sync_callback is called : %s", gst_message_type_get_name(GST_MESSAGE_TYPE(msg)));
2626
2627         if (!core->pipeline) {
2628                 LOGE("mediacodec pipeline handle is null");
2629                 return GST_BUS_PASS;
2630         }
2631
2632         if (!__mc_gst_check_useful_message(core, msg)) {
2633                 gst_message_unref(msg);
2634                 return GST_BUS_DROP;
2635         }
2636
2637         switch (GST_MESSAGE_TYPE(msg)) {
2638         case GST_MESSAGE_EOS:
2639         case GST_MESSAGE_ERROR:
2640                 __mc_gst_bus_callback(NULL, msg, core);
2641                 reply = GST_BUS_DROP;
2642                 break;
2643
2644         default:
2645                 reply = GST_BUS_PASS;
2646                 break;
2647         }
2648
2649         if (reply == GST_BUS_DROP)
2650                 gst_message_unref(msg);
2651
2652         return reply;
2653 }
2654
2655 static GstMemory *__mc_gst_make_tbm_buffer(mc_gst_core_t *core, GstMCBuffer *mcbuffer)
2656 {
2657         int ret = MEDIA_PACKET_ERROR_NONE;
2658         tbm_surface_h surface = NULL;
2659         GstVideoInfo vinfo;
2660         GstMemory *mem = NULL;
2661
2662         if (!mcbuffer->packet) {
2663                 LOGE("output is null");
2664                 return NULL;
2665         }
2666
2667         ret = media_packet_get_tbm_surface(mcbuffer->packet, &surface);
2668         if (ret != MEDIA_PACKET_ERROR_NONE) {
2669                 LOGE("Failed to get tbm surface");
2670                 return NULL;
2671         }
2672
2673         if (!gst_video_info_from_caps(&vinfo, core->caps)) {
2674                 LOGE("Failed to get video info");
2675                 return NULL;
2676         }
2677
2678         mem = gst_tizen_allocator_alloc_surface(core->allocator,
2679                         &vinfo, surface, mcbuffer, (GDestroyNotify)__mc_input_buffer_finalize_cb);
2680
2681         return mem;
2682 }
2683
2684 static void __mc_input_buffer_finalize_cb(GstMCBuffer *mcbuffer)
2685 {
2686         MEDIACODEC_FENTER();
2687
2688         if (!mcbuffer)
2689                 return;
2690
2691         LOGD("============>>>>>  _finalize_cb  : %p, %p", mcbuffer, mcbuffer->packet);
2692
2693         _mc_gst_handle_input_buffer_used(mcbuffer->core, mcbuffer->packet);
2694
2695         g_free(mcbuffer);
2696
2697         MEDIACODEC_FLEAVE();
2698 }
2699
2700 static GstMCBuffer *gst_mediacodec_buffer_new(mc_gst_core_t *core, media_packet_h packet, uint64_t size)
2701 {
2702         GstMCBuffer *mcbuffer = NULL;
2703
2704         mcbuffer = (GstMCBuffer *)g_malloc0(sizeof(GstMCBuffer));
2705
2706         mcbuffer->buffer = gst_buffer_new();
2707         mcbuffer->buf_size = 0;
2708
2709         LOGD("creating buffer : %p, %p", mcbuffer, mcbuffer->buffer);
2710         mcbuffer->core = core;
2711         mcbuffer->packet = packet;
2712
2713         return mcbuffer;
2714 }
2715
2716 static gint __gst_handle_core_error(mc_gst_core_t *core, int code)
2717 {
2718         gint trans_err = MEDIACODEC_ERROR_NONE;
2719
2720         g_return_val_if_fail(core, MC_PARAM_ERROR);
2721
2722         switch (code) {
2723         case GST_CORE_ERROR_MISSING_PLUGIN:
2724                 return MEDIACODEC_ERROR_NOT_SUPPORTED_FORMAT;
2725         case GST_CORE_ERROR_STATE_CHANGE:
2726         case GST_CORE_ERROR_SEEK:
2727         case GST_CORE_ERROR_NOT_IMPLEMENTED:
2728         case GST_CORE_ERROR_FAILED:
2729         case GST_CORE_ERROR_TOO_LAZY:
2730         case GST_CORE_ERROR_PAD:
2731         case GST_CORE_ERROR_THREAD:
2732         case GST_CORE_ERROR_NEGOTIATION:
2733         case GST_CORE_ERROR_EVENT:
2734         case GST_CORE_ERROR_CAPS:
2735         case GST_CORE_ERROR_TAG:
2736         case GST_CORE_ERROR_CLOCK:
2737         case GST_CORE_ERROR_DISABLED:
2738         default:
2739                 trans_err =  MEDIACODEC_ERROR_INVALID_STREAM;
2740                 break;
2741         }
2742
2743         return trans_err;
2744 }
2745
2746 static gint __gst_handle_library_error(mc_gst_core_t *core, int code)
2747 {
2748         gint trans_err = MEDIACODEC_ERROR_NONE;
2749
2750         g_return_val_if_fail(core, MC_PARAM_ERROR);
2751
2752         switch (code) {
2753         case GST_LIBRARY_ERROR_FAILED:
2754         case GST_LIBRARY_ERROR_TOO_LAZY:
2755         case GST_LIBRARY_ERROR_INIT:
2756         case GST_LIBRARY_ERROR_SHUTDOWN:
2757         case GST_LIBRARY_ERROR_SETTINGS:
2758         case GST_LIBRARY_ERROR_ENCODE:
2759         default:
2760                 trans_err =  MEDIACODEC_ERROR_INVALID_STREAM;
2761                 break;
2762         }
2763
2764         return trans_err;
2765 }
2766
2767
2768 static gint __gst_handle_resource_error(mc_gst_core_t *core, int code)
2769 {
2770         gint trans_err = MEDIACODEC_ERROR_NONE;
2771
2772         g_return_val_if_fail(core, MC_PARAM_ERROR);
2773
2774         switch (code) {
2775         case GST_RESOURCE_ERROR_NO_SPACE_LEFT:
2776                 trans_err = MEDIACODEC_ERROR_NO_FREE_SPACE;
2777                 break;
2778         case GST_RESOURCE_ERROR_WRITE:
2779         case GST_RESOURCE_ERROR_FAILED:
2780         case GST_RESOURCE_ERROR_SEEK:
2781         case GST_RESOURCE_ERROR_TOO_LAZY:
2782         case GST_RESOURCE_ERROR_BUSY:
2783         case GST_RESOURCE_ERROR_OPEN_WRITE:
2784         case GST_RESOURCE_ERROR_OPEN_READ_WRITE:
2785         case GST_RESOURCE_ERROR_CLOSE:
2786         case GST_RESOURCE_ERROR_SYNC:
2787         case GST_RESOURCE_ERROR_SETTINGS:
2788         default:
2789                 trans_err = MEDIACODEC_ERROR_INTERNAL;
2790                 break;
2791         }
2792
2793         return trans_err;
2794 }
2795
2796 static gint __gst_handle_stream_error(mc_gst_core_t *core, GError *error, GstMessage *message)
2797 {
2798         gint trans_err = MEDIACODEC_ERROR_NONE;
2799
2800         g_return_val_if_fail(core, MC_PARAM_ERROR);
2801         g_return_val_if_fail(error, MC_PARAM_ERROR);
2802         g_return_val_if_fail(message, MC_PARAM_ERROR);
2803
2804         switch (error->code) {
2805         case GST_STREAM_ERROR_FAILED:
2806         case GST_STREAM_ERROR_TYPE_NOT_FOUND:
2807         case GST_STREAM_ERROR_DECODE:
2808         case GST_STREAM_ERROR_WRONG_TYPE:
2809         case GST_STREAM_ERROR_DECRYPT:
2810         case GST_STREAM_ERROR_DECRYPT_NOKEY:
2811         case GST_STREAM_ERROR_CODEC_NOT_FOUND:
2812                 trans_err = __gst_transform_gsterror(core, message, error);
2813                 break;
2814
2815         case GST_STREAM_ERROR_NOT_IMPLEMENTED:
2816         case GST_STREAM_ERROR_TOO_LAZY:
2817         case GST_STREAM_ERROR_ENCODE:
2818         case GST_STREAM_ERROR_DEMUX:
2819         case GST_STREAM_ERROR_MUX:
2820         case GST_STREAM_ERROR_FORMAT:
2821         default:
2822                 trans_err = MEDIACODEC_ERROR_INVALID_STREAM;
2823                 break;
2824         }
2825
2826         return trans_err;
2827 }
2828
2829 static gint __gst_transform_gsterror(mc_gst_core_t *core, GstMessage *message, GError *error)
2830 {
2831         gchar *src_element_name = NULL;
2832         GstElement *src_element = NULL;
2833         GstElementFactory *factory = NULL;
2834         const gchar *klass = NULL;
2835
2836
2837         src_element = GST_ELEMENT_CAST(message->src);
2838         if (!src_element)
2839                 goto INTERNAL_ERROR;
2840
2841         src_element_name = GST_ELEMENT_NAME(src_element);
2842         if (!src_element_name)
2843                 goto INTERNAL_ERROR;
2844
2845         factory = gst_element_get_factory(src_element);
2846         if (!factory)
2847                 goto INTERNAL_ERROR;
2848
2849         klass = gst_element_factory_get_klass(factory);
2850         if (!klass)
2851                 goto INTERNAL_ERROR;
2852
2853         LOGD("error code=%d, msg=%s, src element=%s, class=%s\n",
2854                         error->code, error->message, src_element_name, klass);
2855
2856         switch (error->code) {
2857         case GST_STREAM_ERROR_DECODE:
2858         case GST_STREAM_ERROR_FAILED:
2859                 return MEDIACODEC_ERROR_INVALID_STREAM;
2860                 break;
2861
2862         case GST_STREAM_ERROR_CODEC_NOT_FOUND:
2863         case GST_STREAM_ERROR_TYPE_NOT_FOUND:
2864         case GST_STREAM_ERROR_WRONG_TYPE:
2865                 return MEDIACODEC_ERROR_NOT_SUPPORTED_FORMAT;
2866                 break;
2867
2868         default:
2869                 return MEDIACODEC_ERROR_INTERNAL;
2870                 break;
2871         }
2872
2873 INTERNAL_ERROR:
2874         return MEDIACODEC_ERROR_INTERNAL;
2875 }
2876
2877 static gint _mc_gst_flush_buffers(mc_gst_core_t *core)
2878 {
2879         gboolean ret = FALSE;
2880         GstEvent *event = NULL;
2881
2882         MEDIACODEC_FENTER();
2883
2884         _mc_gst_set_flush_input(core);
2885
2886         LOGW("Trying send flush_start .... ");
2887         event = gst_event_new_flush_start();
2888         ret = gst_element_send_event(core->appsrc, event);
2889         if (ret != TRUE) {
2890                 LOGE("failed to send Flush_Start event");
2891                 return MC_ERROR;
2892         }
2893
2894         LOGW("Trying send flush_stop .... ");
2895         event = gst_event_new_flush_stop(TRUE);
2896         ret = gst_element_send_event(core->appsrc, event);
2897         if (ret != TRUE) {
2898                 LOGE("failed to send Flush_Stop event");
2899                 return MC_ERROR;
2900         }
2901
2902         LOGW("Trying send seek .... ");
2903         event = gst_event_new_seek(1.0, GST_FORMAT_BYTES, GST_SEEK_FLAG_FLUSH,
2904                         GST_SEEK_TYPE_SET, 0, GST_SEEK_TYPE_NONE, -1);
2905
2906         ret = gst_element_send_event(core->appsrc, event);
2907         if (ret != TRUE) {
2908                 LOGE("failed to send seek event");
2909                 return MC_ERROR;
2910         }
2911
2912         _mc_gst_set_flush_output(core);
2913
2914         MEDIACODEC_FLEAVE();
2915
2916         return MC_ERROR_NONE;
2917 }
2918
2919
2920 static void _mc_gst_set_flush_input(mc_gst_core_t *core)
2921 {
2922         media_packet_h packet = NULL;
2923
2924         MEDIACODEC_FENTER();
2925         LOGI("_mc_gst_set_flush_input called : input_queue_len = %d, etb_count = %d", core->available_queue->input->length, core->etb_count);
2926
2927         while (!mc_async_queue_is_empty(core->available_queue->input)) {
2928                 packet = mc_async_queue_pop_forced(core->available_queue->input);
2929                 LOGD("%p poped(%d)", packet, core->etb_count);
2930
2931                 _mc_gst_handle_input_buffer_used(core, packet);
2932         }
2933
2934         mc_async_queue_flush(core->available_queue->input);
2935         LOGI("_mc_gst_set_flush_input ended : input_queue_len = %d, etb_count = %d", core->available_queue->input->length, core->etb_count);
2936         MEDIACODEC_FLEAVE();
2937 }
2938
2939 static void _mc_gst_set_flush_output(mc_gst_core_t *core)
2940 {
2941         media_packet_h packet = NULL;
2942
2943         MEDIACODEC_FENTER();
2944         g_mutex_lock(&core->ports[1]->mutex);
2945
2946         while (!g_queue_is_empty(core->ports[1]->queue)) {
2947                 packet = g_queue_pop_head(core->ports[1]->queue);
2948                 LOGD("%p poped", packet);
2949                 if (packet) {
2950                         media_packet_destroy(packet);
2951                         LOGD("outpacket destroyed");
2952                         packet = NULL;
2953                 }
2954         }
2955         g_mutex_unlock(&core->ports[1]->mutex);
2956         MEDIACODEC_FLEAVE();
2957 }
2958
2959 #if TIZEN_EXYNOS_SPECIFIC
2960 /*
2961  * Get tiled address of position(x,y)
2962  *
2963  * @param x_size
2964  *   width of tiled[in]
2965  *
2966  * @param y_size
2967  *   height of tiled[in]
2968  *
2969  * @param x_pos
2970  *   x position of tield[in]
2971  *
2972  * @param src_size
2973  *   y position of tield[in]
2974  *
2975  * @return
2976  *   address of tiled data
2977  */
2978 static gint __tile_4x2_read(gint x_size, gint y_size, gint x_pos, gint y_pos)
2979 {
2980         gint pixel_x_m1, pixel_y_m1;
2981         gint roundup_x;
2982         gint linear_addr0, linear_addr1, bank_addr ;
2983         gint x_addr;
2984         gint trans_addr;
2985
2986         pixel_x_m1 = x_size - 1;
2987         pixel_y_m1 = y_size - 1;
2988
2989         roundup_x = ((pixel_x_m1 >> 7) + 1);
2990
2991         x_addr = x_pos >> 2;
2992
2993         if ((y_size <= y_pos+32) && (y_pos < y_size) &&
2994                         (((pixel_y_m1 >> 5) & 0x1) == 0) && (((y_pos >> 5) & 0x1) == 0)) {
2995                 linear_addr0 = (((y_pos & 0x1f) << 4) | (x_addr & 0xf));
2996                 linear_addr1 = (((y_pos >> 6) & 0xff) * roundup_x + ((x_addr >> 6) & 0x3f));
2997
2998                 if (((x_addr >> 5) & 0x1) == ((y_pos >> 5) & 0x1))
2999                         bank_addr = ((x_addr >> 4) & 0x1);
3000                 else
3001                         bank_addr = 0x2 | ((x_addr >> 4) & 0x1);
3002         } else {
3003                 linear_addr0 = (((y_pos & 0x1f) << 4) | (x_addr & 0xf));
3004                 linear_addr1 = (((y_pos >> 6) & 0xff) * roundup_x + ((x_addr >> 5) & 0x7f));
3005
3006                 if (((x_addr >> 5) & 0x1) == ((y_pos >> 5) & 0x1))
3007                         bank_addr = ((x_addr >> 4) & 0x1);
3008                 else
3009                         bank_addr = 0x2 | ((x_addr >> 4) & 0x1);
3010         }
3011
3012         linear_addr0 = linear_addr0 << 2;
3013         trans_addr = (linear_addr1 << 13) | (bank_addr << 11) | linear_addr0;
3014
3015         return trans_addr;
3016 }
3017
3018 /*
3019  * Converts tiled data to linear
3020  * Crops left, top, right, buttom
3021  * 1. Y of NV12T to Y of YUV420P
3022  * 2. Y of NV12T to Y of YUV420S
3023  * 3. UV of NV12T to UV of YUV420S
3024  *
3025  * @param yuv420_dest
3026  *   Y or UV plane address of YUV420[out]
3027  *
3028  * @param nv12t_src
3029  *   Y or UV plane address of NV12T[in]
3030  *
3031  * @param yuv420_width
3032  *   Width of YUV420[in]
3033  *
3034  * @param yuv420_height
3035  *   Y: Height of YUV420, UV: Height/2 of YUV420[in]
3036  *
3037  * @param left
3038  *   Crop size of left
3039  *
3040  * @param top
3041  *   Crop size of top
3042  *
3043  * @param right
3044  *   Crop size of right
3045  *
3046  * @param buttom
3047  *   Crop size of buttom
3048  */
3049 static void __csc_tiled_to_linear_crop(gchar *yuv420_dest, gchar *nv12t_src,
3050                 gint yuv420_width, gint yuv420_height,
3051                 gint left, int top, gint right, gint buttom)
3052 {
3053         gint i, j;
3054         gint tiled_offset = 0, tiled_offset1 = 0;
3055         gint linear_offset = 0;
3056         gint temp1 = 0, temp2 = 0, temp3 = 0, temp4 = 0;
3057
3058         temp3 = yuv420_width-right;
3059         temp1 = temp3-left;
3060         /* real width is greater than or equal 256 */
3061         if (temp1 >= 256) {
3062                 for (i = top; i < yuv420_height-buttom; i += 1) {
3063                         j = left;
3064                         temp3 = (j>>8)<<8;
3065                         temp3 = temp3>>6;
3066                         temp4 = i>>5;
3067                         if (temp4 & 0x1) {
3068                                 /* odd fomula: 2+x+(x>>2)<<2+x_block_num*(y-1) */
3069                                 tiled_offset = temp4-1;
3070                                 temp1 = ((yuv420_width+127)>>7)<<7;
3071                                 tiled_offset = tiled_offset*(temp1>>6);
3072                                 tiled_offset = tiled_offset+temp3;
3073                                 tiled_offset = tiled_offset+2;
3074                                 temp1 = (temp3>>2)<<2;
3075                                 tiled_offset = tiled_offset+temp1;
3076                                 tiled_offset = tiled_offset<<11;
3077                                 tiled_offset1 = tiled_offset+2048*2;
3078                                 temp4 = 8;
3079                         } else {
3080                                 temp2 = ((yuv420_height+31)>>5)<<5;
3081                                 if ((i + 32) < temp2) {
3082                                         /* even1 fomula: x+((x+2)>>2)<<2+x_block_num*y */
3083                                         temp1 = temp3+2;
3084                                         temp1 = (temp1>>2)<<2;
3085                                         tiled_offset = temp3+temp1;
3086                                         temp1 = ((yuv420_width+127)>>7)<<7;
3087                                         tiled_offset = tiled_offset+temp4*(temp1>>6);
3088                                         tiled_offset = tiled_offset<<11;
3089                                         tiled_offset1 = tiled_offset + 2048 * 6;
3090                                         temp4 = 8;
3091                                 } else {
3092                                         /* even2 fomula: x+x_block_num*y */
3093                                         temp1 = ((yuv420_width+127)>>7)<<7;
3094                                         tiled_offset = temp4*(temp1>>6);
3095                                         tiled_offset = tiled_offset+temp3;
3096                                         tiled_offset = tiled_offset<<11;
3097                                         tiled_offset1 = tiled_offset+2048*2;
3098                                         temp4 = 4;
3099                                 }
3100                         }
3101
3102                         temp1 = i&0x1F;
3103                         tiled_offset = tiled_offset+64*(temp1);
3104                         tiled_offset1 = tiled_offset1+64*(temp1);
3105                         temp2 = yuv420_width-left-right;
3106                         linear_offset = temp2*(i-top);
3107                         temp3 = ((j+256)>>8)<<8;
3108                         temp3 = temp3-j;
3109                         temp1 = left&0x3F;
3110
3111                         if (temp3 > 192) {
3112                                 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset+temp1, 64-temp1);
3113                                 temp2 = ((left+63)>>6)<<6;
3114                                 temp3 = ((yuv420_width-right)>>6)<<6;
3115
3116                                 if (temp2 == temp3)
3117                                         temp2 = yuv420_width-right-(64-temp1);
3118
3119                                 memcpy(yuv420_dest+linear_offset+64-temp1, nv12t_src+tiled_offset+2048, 64);
3120                                 memcpy(yuv420_dest+linear_offset+128-temp1, nv12t_src+tiled_offset1, 64);
3121                                 memcpy(yuv420_dest+linear_offset+192-temp1, nv12t_src+tiled_offset1+2048, 64);
3122                                 linear_offset = linear_offset+256-temp1;
3123                         } else if (temp3 > 128) {
3124                                 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset+2048+temp1, 64-temp1);
3125                                 memcpy(yuv420_dest+linear_offset+64-temp1, nv12t_src+tiled_offset1, 64);
3126                                 memcpy(yuv420_dest+linear_offset+128-temp1, nv12t_src+tiled_offset1+2048, 64);
3127                                 linear_offset = linear_offset+192-temp1;
3128                         } else if (temp3 > 64) {
3129                                 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset1+temp1, 64-temp1);
3130                                 memcpy(yuv420_dest+linear_offset+64-temp1, nv12t_src+tiled_offset1+2048, 64);
3131                                 linear_offset = linear_offset+128-temp1;
3132                         } else if (temp3 > 0) {
3133                                 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset1+2048+temp1, 64-temp1);
3134                                 linear_offset = linear_offset+64-temp1;
3135                         }
3136
3137                         tiled_offset = tiled_offset+temp4*2048;
3138                         j = (left>>8)<<8;
3139                         j = j + 256;
3140                         temp2 = yuv420_width-right-256;
3141                         for (; j <= temp2; j += 256) {
3142                                 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
3143                                 tiled_offset1 = tiled_offset1+temp4*2048;
3144                                 memcpy(yuv420_dest+linear_offset+64, nv12t_src+tiled_offset+2048, 64);
3145                                 memcpy(yuv420_dest+linear_offset+128, nv12t_src+tiled_offset1, 64);
3146                                 tiled_offset = tiled_offset+temp4*2048;
3147                                 memcpy(yuv420_dest+linear_offset+192, nv12t_src+tiled_offset1+2048, 64);
3148                                 linear_offset = linear_offset+256;
3149                         }
3150
3151                         tiled_offset1 = tiled_offset1+temp4*2048;
3152                         temp2 = yuv420_width-right-j;
3153                         if (temp2 > 192) {
3154                                 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
3155                                 memcpy(yuv420_dest+linear_offset+64, nv12t_src+tiled_offset+2048, 64);
3156                                 memcpy(yuv420_dest+linear_offset+128, nv12t_src+tiled_offset1, 64);
3157                                 memcpy(yuv420_dest+linear_offset+192, nv12t_src+tiled_offset1+2048, temp2-192);
3158                         } else if (temp2 > 128) {
3159                                 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
3160                                 memcpy(yuv420_dest+linear_offset+64, nv12t_src+tiled_offset+2048, 64);
3161                                 memcpy(yuv420_dest+linear_offset+128, nv12t_src+tiled_offset1, temp2-128);
3162                         } else if (temp2 > 64) {
3163                                 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
3164                                 memcpy(yuv420_dest+linear_offset+64, nv12t_src+tiled_offset+2048, temp2-64);
3165                         } else {
3166                                 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, temp2);
3167                         }
3168                 }
3169         } else if (temp1 >= 64) {
3170                 for (i = top; i < (yuv420_height-buttom); i += 1) {
3171                         j = left;
3172                         tiled_offset = __tile_4x2_read(yuv420_width, yuv420_height, j, i);
3173                         temp2 = ((j+64)>>6)<<6;
3174                         temp2 = temp2-j;
3175                         linear_offset = temp1*(i-top);
3176                         temp4 = j&0x3;
3177                         tiled_offset = tiled_offset+temp4;
3178                         memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, temp2);
3179                         linear_offset = linear_offset+temp2;
3180                         j = j+temp2;
3181                         if ((j+64) <= temp3) {
3182                                 tiled_offset = __tile_4x2_read(yuv420_width, yuv420_height, j, i);
3183                                 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
3184                                 linear_offset = linear_offset+64;
3185                                 j = j+64;
3186                         }
3187                         if ((j+64) <= temp3) {
3188                                 tiled_offset = __tile_4x2_read(yuv420_width, yuv420_height, j, i);
3189                                 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
3190                                 linear_offset = linear_offset+64;
3191                                 j = j+64;
3192                         }
3193                         if (j < temp3) {
3194                                 tiled_offset = __tile_4x2_read(yuv420_width, yuv420_height, j, i);
3195                                 temp2 = temp3-j;
3196                                 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, temp2);
3197                         }
3198                 }
3199         } else {
3200                 for (i = top; i < (yuv420_height-buttom); i += 1) {
3201                         linear_offset = temp1*(i-top);
3202                         for (j = left; j < (yuv420_width - right); j += 2) {
3203                                 tiled_offset = __tile_4x2_read(yuv420_width, yuv420_height, j, i);
3204                                 temp4 = j&0x3;
3205                                 tiled_offset = tiled_offset+temp4;
3206                                 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 2);
3207                                 linear_offset = linear_offset+2;
3208                         }
3209                 }
3210         }
3211 }
3212 #endif
3213
3214 void _mc_send_eos_signal(mc_gst_core_t *core)
3215 {
3216         g_mutex_lock(&core->eos_mutex);
3217         core->eos = FALSE;
3218         g_cond_broadcast(&core->eos_cond);
3219         g_mutex_unlock(&core->eos_mutex);
3220         LOGD("send EOS signal");
3221 }
3222
3223 void _mc_wait_for_eos(mc_gst_core_t *core)
3224 {
3225         g_mutex_lock(&core->eos_mutex);
3226         core->eos = TRUE;
3227         LOGD("waiting for EOS");
3228
3229         while (core->eos)
3230                 g_cond_wait(&core->eos_cond, &core->eos_mutex);
3231
3232         LOGD("received EOS");
3233         g_mutex_unlock(&core->eos_mutex);
3234 }
3235
3236 const gchar * _mc_error_to_string(mc_ret_e err)
3237 {
3238         guint err_u = (guint) err;
3239
3240         switch (err_u) {
3241         case MC_ERROR:
3242                 return "Error";
3243         case MC_PARAM_ERROR:
3244                 return "Parameter error";
3245         case MC_INVALID_ARG:
3246                 return "Invailid argument";
3247         case MC_PERMISSION_DENIED:
3248                 return "Permission denied";
3249         case MC_INVALID_STATUS:
3250                 return "Invalid status";
3251         case MC_NOT_SUPPORTED:
3252                 return "Not supported";
3253         case MC_INVALID_IN_BUF:
3254                 return "Invalid inputbuffer";
3255         case MC_INVALID_OUT_BUF:
3256                 return "Invalid outputbuffer";
3257         case MC_INTERNAL_ERROR:
3258                 return "Internal error";
3259         case MC_HW_ERROR:
3260                 return "Hardware error";
3261         case MC_NOT_INITIALIZED:
3262                 return "Not initialized";
3263         case MC_INVALID_STREAM:
3264                 return "Invalid stream";
3265         case MC_CODEC_NOT_FOUND:
3266                 return "Codec not found";
3267         case MC_ERROR_DECODE:
3268                 return "Decode error";
3269         case MC_OUTPUT_BUFFER_EMPTY:
3270                 return "Outputbuffer empty";
3271         case MC_OUTPUT_BUFFER_OVERFLOW:
3272                 return "Outputbuffer overflow";
3273         case MC_MEMORY_ALLOCED:
3274                 return "Memory allocated";
3275         case MC_COURRPTED_INI:
3276                 return "Courrpted ini";
3277         default:
3278                 return "Unknown error";
3279
3280         }
3281 }
3282
3283 const gchar * _mc_bit_to_string(int bit)
3284 {
3285         switch (bit) {
3286         case 16:
3287                 return "S16LE";
3288         case 32:
3289                 return "F32LE";
3290         default:
3291                 return "NULL";
3292
3293         }
3294 }
3295
3296 int _mc_get_mime(mc_gst_core_t *core)
3297 {
3298         media_format_mimetype_e mime = MEDIA_FORMAT_MAX;
3299
3300         switch (core->codec_id) {
3301         case MEDIACODEC_H264:
3302                 if (core->encoder)
3303                         mime = (core->is_hw) ? MEDIA_FORMAT_NV12 : MEDIA_FORMAT_I420;
3304                 else
3305                         mime = MEDIA_FORMAT_H264_SP;
3306                 break;
3307         case MEDIACODEC_MPEG4:
3308                 if (core->encoder)
3309                         mime = (core->is_hw) ? MEDIA_FORMAT_NV12 : MEDIA_FORMAT_I420;
3310                 else
3311                         mime = MEDIA_FORMAT_MPEG4_SP;
3312
3313                 break;
3314         case MEDIACODEC_H263:
3315                 if (core->encoder)
3316                         mime = (core->is_hw) ? MEDIA_FORMAT_NV12 : MEDIA_FORMAT_I420;
3317                 else
3318                         mime = MEDIA_FORMAT_H263P;
3319
3320                 break;
3321         case MEDIACODEC_AAC:
3322                 if (core->encoder)
3323                         mime = MEDIA_FORMAT_PCM_F32LE;  /*FIXME need to change PCM supported by chipset */
3324                 else
3325                         mime = MEDIA_FORMAT_AAC;
3326
3327                 break;
3328         case MEDIACODEC_AAC_HE:
3329                 if (core->encoder)
3330                         mime = MEDIA_FORMAT_PCM_F32LE;  /* FIXME need to change PCM supported by chipset */
3331                 else
3332                         mime = MEDIA_FORMAT_AAC_HE;
3333
3334                 break;
3335         case MEDIACODEC_AAC_HE_PS:
3336                 break;
3337         case MEDIACODEC_MP3:
3338                 mime = MEDIA_FORMAT_MP3;
3339                 break;
3340         case MEDIACODEC_VORBIS:
3341                 break;
3342         case MEDIACODEC_FLAC:
3343                 break;
3344         case MEDIACODEC_WMAV1:
3345                 break;
3346         case MEDIACODEC_WMAV2:
3347                 break;
3348         case MEDIACODEC_WMAPRO:
3349                 break;
3350         case MEDIACODEC_WMALSL:
3351                 break;
3352         case MEDIACODEC_AMR_NB:
3353                 mime = MEDIA_FORMAT_AMR_NB;
3354                 break;
3355         case MEDIACODEC_AMR_WB:
3356                 mime = MEDIA_FORMAT_AMR_WB;
3357                 break;
3358         default:
3359                 LOGE("NOT SUPPORTED!!!!");
3360                 break;
3361         }
3362         return mime;
3363 }
3364
3365 mc_ret_e mc_gst_get_packet_pool(mc_handle_t *mc_handle, media_packet_pool_h *packet_pool)
3366 {
3367         gint ret = MC_ERROR_NONE;
3368         gint curr_size;
3369         gint max_size, min_size;
3370         media_format_mimetype_e mime_format = MEDIA_FORMAT_MAX;
3371         media_format_h fmt_handle = NULL;
3372         media_packet_pool_h pool = NULL;
3373         mc_gst_core_t *core = NULL;
3374
3375         if (!mc_handle)
3376                 return MC_PARAM_ERROR;
3377
3378         core = (mc_gst_core_t *)mc_handle->core;
3379
3380         ret = media_packet_pool_create(&pool);
3381         if (ret != MEDIA_PACKET_ERROR_NONE) {
3382                 LOGE("media_packet_pool_create failed");
3383                 return MC_ERROR;
3384         }
3385
3386         if (media_format_create(&fmt_handle) != MEDIA_FORMAT_ERROR_NONE) {
3387                 LOGE("media format create failed");
3388                 media_packet_pool_destroy(pool);
3389                 return MC_ERROR;
3390         }
3391
3392         mime_format = _mc_get_mime(core);
3393
3394         if (core->video) {
3395                 media_format_set_video_mime(fmt_handle, mime_format);
3396                 media_format_set_video_width(fmt_handle, mc_handle->info.video.width);
3397                 media_format_set_video_height(fmt_handle, mc_handle->info.video.height);
3398
3399                 if (core->encoder)
3400                         media_format_set_video_avg_bps(fmt_handle, mc_handle->info.video.bitrate);
3401         } else {
3402                         media_format_set_audio_mime(fmt_handle, mime_format);
3403                         media_format_set_audio_channel(fmt_handle, mc_handle->info.audio.channel);
3404                         media_format_set_audio_samplerate(fmt_handle, mc_handle->info.audio.samplerate);
3405                         media_format_set_audio_bit(fmt_handle, mc_handle->info.audio.bit_depth);
3406                 if (core->encoder)
3407                         media_format_set_audio_avg_bps(fmt_handle, mc_handle->info.audio.bitrate);
3408         }
3409
3410         ret = media_packet_pool_set_media_format(pool, fmt_handle);
3411         if (ret != MEDIA_PACKET_ERROR_NONE) {
3412                 media_packet_pool_destroy(pool);
3413                 LOGE("media_packet_pool_set_media_format failed");
3414                 return MC_ERROR;
3415         }
3416
3417         /* will use default size temporarily */
3418         max_size = core->encoder ? DEFAULT_ENC_POOL_SIZE : DEFAULT_DEC_POOL_SIZE;
3419
3420         min_size = max_size;
3421         ret = media_packet_pool_set_size(pool, min_size, max_size);
3422         if (ret != MEDIA_PACKET_ERROR_NONE) {
3423                 media_packet_pool_destroy(pool);
3424                 LOGE("media_packet_pool_set_size failed");
3425                 return MC_ERROR;
3426         }
3427
3428         ret = media_packet_pool_allocate(pool);
3429         if (ret != MEDIA_PACKET_ERROR_NONE) {
3430                 media_packet_pool_destroy(pool);
3431                 LOGE("media_packet_pool_allocate failed");
3432                 return MC_OUT_OF_MEMORY;
3433         }
3434         media_packet_pool_get_size(pool, &min_size, &max_size, &curr_size);
3435         LOGD("curr_size is  %d min_size is %d and max_size is %d \n", curr_size, min_size, max_size);
3436
3437         *packet_pool = pool;
3438         return MC_ERROR_NONE;
3439 }