2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
27 #include <gst/gstelement.h>
28 #include <gst/app/gstappsrc.h>
31 * Internal Implementation
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);
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);
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);
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);
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);
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);
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);
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);
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);
99 static mc_gst_core_t *mc_gst_core_new();
100 static void mc_gst_core_free(mc_gst_core_t *core);
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);
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};
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};
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};
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};
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};
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};
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};
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};
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};
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};
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};
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};
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};
145 int(*aenc_aac_vtable[])() = {&__mc_fill_input_buffer_with_packet, /* AAC LC Encoder vtable */
146 &__mc_fill_aenc_packet_with_output_buffer};
148 int(*adec_aac_vtable[])() = {&__mc_fill_input_buffer_with_adec_packet, /* AAC LC Decoder Vtable */
149 &__mc_fill_packet_with_output_buffer};
151 int(*adec_aacv12_vtable[])() = {&__mc_fill_input_buffer_with_adec_packet, /* AAC HE Decoder Vtable */
152 &__mc_fill_packet_with_output_buffer};
154 int(*adec_mp3_vtable[])() = {&__mc_fill_input_buffer_with_packet, /* MP3 Decoder Vtable */
155 &__mc_fill_packet_with_output_buffer};
157 int(*adec_amrnb_vtable[])() = {&__mc_fill_input_buffer_with_packet, /* AMR-NB Decoder Vtable */
158 &__mc_fill_packet_with_output_buffer};
160 int(*adec_amrwb_vtable[])() = {&__mc_fill_input_buffer_with_packet, /* AMR-WB Decoder Vtable */
161 &__mc_fill_packet_with_output_buffer};
163 int(*aenc_amrnb_vtable[])() = {&__mc_fill_input_buffer_with_packet, /* AMR-NB Encoder Vtable */
164 &__mc_fill_packet_with_output_buffer};
166 int(*adec_vorbis_vtable[])() = {&__mc_fill_input_buffer_with_packet, /* VORBIS Decoder Vtable */
167 &__mc_fill_packet_with_output_buffer};
169 int(*adec_flac_vtable[])() = {&__mc_fill_input_buffer_with_packet, /* FLAC Decoder Vtable */
170 &__mc_fill_packet_with_output_buffer};
172 int(*adec_wma_vtable[])() = {&__mc_fill_input_buffer_with_packet, /* WMA Decoder Vtable */
173 &__mc_fill_packet_with_output_buffer};
175 #define MEDIACODEC_ELEMENT_SET_STATE(x_element, x_state) \
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; \
185 * fill_inbuf virtual functions
187 int __mc_fill_input_buffer(mc_gst_core_t *core, media_packet_h packet, GstMCBuffer *mcbuffer)
189 return core->vtable[fill_inbuf](core, packet, mcbuffer);
192 int __mc_fill_inbuf_with_mm_video_buffer(mc_gst_core_t *core, media_packet_h packet, GstMCBuffer *mcbuffer)
194 gint ret = MC_ERROR_NONE;
195 uint64_t buf_size = 0;
197 void *buf_data = NULL;
199 ret = media_packet_get_buffer_size(packet, &buf_size);
200 if (ret != MEDIA_PACKET_ERROR_NONE) {
201 LOGW("buffer size get failed");
205 ret = media_packet_get_buffer_data_ptr(packet, &buf_data);
206 if (ret != MEDIA_PACKET_ERROR_NONE) {
207 LOGW("buffer data get failed");
211 mem = __mc_gst_make_tbm_buffer(core, mcbuffer);
213 LOGW("Failed to make tizen memory");
217 gst_buffer_append_memory(mcbuffer->buffer, mem);
218 LOGD("tizen memory appended");
223 static int __mc_fill_input_buffer_with_packet(mc_gst_core_t *core, media_packet_h packet, GstMCBuffer *mcbuffer)
225 gint ret = MC_ERROR_NONE;
226 void *buf_data = NULL;
227 uint64_t buf_size = 0;
229 ret = media_packet_get_buffer_size(packet, &buf_size);
230 if (ret != MEDIA_PACKET_ERROR_NONE) {
231 LOGW("buffer size get failed");
235 ret = media_packet_get_buffer_data_ptr(packet, &buf_data);
236 if (ret != MEDIA_PACKET_ERROR_NONE) {
237 LOGW("buffer data get failed");
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");
251 static int __mc_fill_input_buffer_with_adec_packet(mc_gst_core_t *core, media_packet_h packet, GstMCBuffer *mcbuffer)
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;
260 ret = media_packet_get_buffer_size(packet, &buf_size);
261 if (ret != MEDIA_PACKET_ERROR_NONE) {
262 LOGW("buffer size get failed");
266 ret = media_packet_get_buffer_data_ptr(packet, &buf_data);
267 if (ret != MEDIA_PACKET_ERROR_NONE) {
268 LOGW("buffer data get failed");
272 if (media_packet_is_codec_config(packet, &codec_config) != MEDIA_PACKET_ERROR_NONE) {
273 LOGE("media_packet_is_codec_config failed");
277 if (buf_data != NULL) {
278 media_packet_get_codec_data(packet, &codec_data, &codec_size);
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");
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");
296 static int __mc_fill_input_buffer_with_venc_packet(mc_gst_core_t *core, media_packet_h packet, GstMCBuffer *mcbuffer)
298 gint ret = MC_ERROR_NONE;
310 mc_gst_port_def_t *port_def = &core->ports[in_port_index]->port_def;
312 width = port_def->info.video.width;
313 height = port_def->info.video.height;
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");
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");
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");
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");
340 if (width == stride_width) {
341 mcbuffer->buf_size += stride_width * stride_height;
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);
348 buf_size = stride_width * stride_height;
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);
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;
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);
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;
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;
381 GstMemory *mem = NULL;
383 mem = __mc_gst_make_tbm_buffer(core, mcbuffer);
385 LOGW("Failed to make tizen memory");
389 gst_buffer_append_memory(mcbuffer->buffer, mem);
390 LOGD("tizen memory appended");
397 * fill_outbuf virtual functions
400 static int __mc_fill_output_buffer(mc_gst_core_t *core, void *data, int size, GstMCBuffer *mcbuffer)
402 return core->vtable[fill_outbuf](core, data, size, mcbuffer);
405 static int __mc_fill_vdec_packet_with_output_buffer(mc_gst_core_t *core, void *data, int size, GstMCBuffer *mcbuffer)
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;
419 g_return_val_if_fail(core != NULL, MC_PARAM_ERROR);
421 mc_gst_port_def_t *port_def = &core->ports[out_port_index]->port_def;
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;
430 memset(&tsurf_info, 0x0, sizeof(tbm_surface_info_s));
432 bo[0] = tbm_bo_alloc(core->bufmgr, buf_size, TBM_BO_WC);
434 LOGE("bo allocation failed");
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);
445 for (i = 0; i < tsurf_info.num_planes; i++) {
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;
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;
459 thandle = tbm_bo_map(bo[0], TBM_DEVICE_CPU, TBM_OPTION_WRITE);
460 memcpy(thandle.ptr, data, tsurf_info.size);
463 tsurf = tbm_surface_internal_create_with_bos(&tsurf_info, bo, 1);
465 tsurf = (tbm_surface_h)data;
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;
474 return MC_ERROR_NONE;
477 static int __mc_fill_packet_with_output_buffer(mc_gst_core_t *core, void *data, int size, GstMCBuffer *mcbuffer)
479 gint ret = MC_ERROR_NONE;
480 gchar *ext_mem = NULL;
482 void *packet_data = NULL;
483 media_packet_h packet = NULL;
485 g_return_val_if_fail(core != NULL, MC_PARAM_ERROR);
487 if (core->encoder && core->codec_id == MEDIACODEC_AAC)
488 media_format_set_audio_aac_type(core->output_fmt, FALSE);
490 mem_size = GST_ROUND_UP_4(size);
491 ext_mem = g_malloc0(mem_size);
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");
499 mcbuffer->packet = packet;
500 mcbuffer->ext_mem = ext_mem;
502 media_packet_set_buffer_size(packet, size);
503 media_packet_get_buffer_data_ptr(packet, &packet_data);
504 memcpy(packet_data, data, size);
506 return MC_ERROR_NONE;
509 static int __mc_fill_venc_packet_with_output_buffer(mc_gst_core_t *core, void *data, int size, GstMCBuffer *mcbuffer)
511 gint ret = MC_ERROR_NONE;
512 bool codec_config = FALSE;
513 bool sync_flag = FALSE;
516 gchar *ext_mem = NULL;
518 void *packet_data = NULL;
519 media_packet_h packet = NULL;
521 g_return_val_if_fail(core != NULL, MC_PARAM_ERROR);
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);
530 LOGE("No valid SPS/PPS ...");
531 return MC_INVALID_IN_BUF;
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);
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;
544 return MC_INVALID_IN_BUF;
546 LOGD("codec_config : %d, sync_flag : %d, slice : %d", codec_config, sync_flag, slice);
548 mem_size = GST_ROUND_UP_4(size);
549 ext_mem = g_malloc0(mem_size);
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");
557 mcbuffer->packet = packet;
558 mcbuffer->ext_mem = ext_mem;
560 media_packet_set_buffer_size(packet, size);
561 media_packet_get_buffer_data_ptr(packet, &packet_data);
562 memcpy(packet_data, data, size);
564 core->need_sync_flag = sync_flag ? 1 : 0;
565 core->need_codec_data = codec_config ? 1 : 0;
570 static int __mc_fill_aenc_packet_with_output_buffer(mc_gst_core_t *core, void *data, int size, GstMCBuffer *mcbuffer)
572 int ret = MC_ERROR_NONE;
579 gchar *ext_mem = NULL;
580 const GValue *codec_data = NULL;
581 void *packet_data = NULL;
582 media_packet_h packet = NULL;
584 g_return_val_if_fail(core != NULL, MC_PARAM_ERROR);
586 if (core->encoder && core->codec_id == MEDIACODEC_AAC)
587 media_format_set_audio_aac_type(core->output_fmt, FALSE);
589 mem_size = GST_ROUND_UP_4(size);
590 ext_mem = g_malloc0(mem_size);
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");
598 mcbuffer->packet = packet;
599 mcbuffer->ext_mem = ext_mem;
601 media_packet_set_buffer_size(packet, size);
602 media_packet_get_buffer_data_ptr(packet, &packet_data);
603 memcpy(packet_data, data, size);
605 if (!core->codec_data) {
606 pad = gst_element_get_static_pad(core->fakesink, "sink");
607 peercaps = gst_pad_get_current_caps(pad);
610 if (gst_caps_is_empty(peercaps)) {
611 gst_caps_unref(peercaps);
612 gst_object_unref(pad);
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);
622 gst_object_unref(pad);
623 LOGD("codec data set");
626 if (core->codec_data) {
627 gst_buffer_map(core->codec_data, &map, GST_MAP_READ);
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);
636 gst_buffer_unmap(core->codec_data, &map);
642 static GstCaps *__mc_gst_caps_set_buffer_array(GstCaps * caps, const gchar * name, GstBuffer * buf, ...)
644 GstStructure *struc = NULL;
646 GValue arr_val = { 0 };
647 GValue buf_val = { 0 };
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);
654 struc = gst_caps_get_structure(caps, 0);
656 LOGW("cannot get structure from caps.\n");
658 g_value_init(&arr_val, GST_TYPE_ARRAY);
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);
667 buf = va_arg(va, GstBuffer *);
671 gst_structure_take_value(struc, name, &arr_val);
676 int __mc_set_caps_streamheader(mc_gst_core_t *core, GstMCBuffer *mcbuffer, guint streamheader_size)
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;
684 guint8 *buf_data = NULL;
685 guint8 *tmp_buf = NULL;
687 ret = media_packet_get_buffer_size(mcbuffer->packet, &buf_size);
688 if (ret != MEDIA_PACKET_ERROR_NONE) {
689 LOGW("buffer size get failed");
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");
699 LOGD("Set caps for streamheader in mime : %s and codec_id (0x%x)", core->mime, core->codec_id);
701 if (core->codec_id == MEDIACODEC_VORBIS) {
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)
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);
713 tmp_buf += (30 + 7); /* hsize1 + '0x03' + 'vorbis'*/
715 hsize2 += GST_READ_UINT32_LE(tmp_buf);
717 LOGD("Find streamheader hsize2(%d)", hsize2);
718 gst_buffer_unmap(tmp_header, &map);
719 gst_buffer_unref(tmp_header);
721 /* hsize1 : Identification Header (packet type 1) - fixed 30 byte */
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);
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;
733 gst_buffer_unmap(header1, &map);
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);
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;
746 gst_buffer_unmap(header2, &map);
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);
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;
760 gst_buffer_unmap(header3, &map);
762 LOGD("[vorbis] streamheader hsize1 (%d) + hsize2 (%d) + hsize3 (%d) = Total (%d)",
763 hsize1, hsize2, hsize3, (hsize1 + hsize2 + hsize3));
765 __mc_gst_caps_set_buffer_array(core->caps, "streamheader", header1, header2, header3, NULL);
767 gst_buffer_unref(header1);
768 gst_buffer_unref(header2);
769 gst_buffer_unref(header3);
770 } else if (core->codec_id == MEDIACODEC_FLAC) {
772 * hsize1 : Stream Info (type 0) - fixed 51 byte
773 * hsize2 : Stream Comment (type 4) - variable byte (need calculate)
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);
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);
786 /* hsize1 : Stream Info (type 0) - fixed 51 byte */
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);
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;
799 gst_buffer_unmap(header1, &map);
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);
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;
814 gst_buffer_unmap(header2, &map);
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);
821 LOGE("Not support case of Stream header Caps");
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));
833 int __mc_set_caps_codecdata(mc_gst_core_t *core, GstMCBuffer *mcbuffer, guint fixed_size)
835 gint ret = MC_ERROR_NONE;
836 uint32_t codec_data_size = 0;
838 GstBuffer *codecdata_buffer;
840 void *codec_data = NULL;
842 media_packet_get_codec_data(mcbuffer->packet, &codec_data, &codec_data_size);
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;
855 media_packet_get_buffer_size(mcbuffer->packet, &buffer_size);
856 media_packet_get_buffer_data_ptr(mcbuffer->packet, &buff_data);
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);
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));
872 LOGD("after : set codec data from packet: %" G_GSIZE_FORMAT "", gst_buffer_get_size(mcbuffer->buffer));
873 gst_buffer_unmap(mcbuffer->buffer, &map);
879 int _mc_output_media_packet_new(mc_gst_core_t *core, bool video, bool encoder, media_format_mimetype_e out_mime)
881 if (media_format_create(&core->output_fmt) != MEDIA_FORMAT_ERROR_NONE) {
882 LOGE("media format create failed");
885 mc_gst_port_def_t *port_def = &core->ports[out_port_index]->port_def;
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);
893 media_format_set_video_avg_bps(core->output_fmt, port_def->info.video.bitrate);
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);
901 media_format_set_audio_avg_bps(core->output_fmt, port_def->info.audio.bitrate);
904 return MC_ERROR_NONE;
908 * mc_gst_core functions
910 static mc_gst_core_t *mc_gst_core_new()
916 core = g_new0(mc_gst_core_t, 1);
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;
924 core->available_queue = g_new0(mc_aqueue_t, 1);
925 core->available_queue->input = mc_async_queue_new();
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);
934 core->need_drain = false;
935 core->need_feed = false;
937 core->need_codec_data = false;
938 core->need_sync_flag = false;
939 core->unprepare_flag = false;
940 core->prepare_count = 0;
942 core->codec_data = NULL;
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();
950 LOGD("gst_core(%p) is created", core);
957 static void mc_gst_core_free(mc_gst_core_t *core)
961 mc_aqueue_t *async_queue;
963 async_queue = core->available_queue;
965 mc_async_queue_disable(async_queue->input);
967 g_atomic_int_set(&async_queue->running, 0);
968 g_thread_join(async_queue->thread);
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);
977 mc_async_queue_free(async_queue->input);
980 if (core->allocator) {
981 gst_object_unref(core->allocator);
982 core->allocator = NULL;
985 if (core->codec_data)
986 gst_buffer_unref(core->codec_data);
988 if (core->ports[0] != NULL) {
989 mc_gst_port_free(core->ports[0]);
990 core->ports[0] = NULL;
993 if (core->ports[1] != NULL) {
994 mc_gst_port_free(core->ports[1]);
995 core->ports[1] = NULL;
998 LOGD("gst_core(%p) destroyed", core);
1001 MEDIACODEC_FLEAVE();
1005 * mc_gst_port functions
1007 static mc_gst_port_t *mc_gst_port_new(mc_gst_core_t *core)
1009 MEDIACODEC_FENTER();
1011 mc_gst_port_t *port;
1013 port = g_new0(mc_gst_port_t, 1);
1015 port->num_buffers = -1;
1016 port->buffer_size = 0;
1017 port->is_allocated = 0;
1019 g_mutex_init(&port->mutex);
1020 port->queue = g_queue_new();
1022 LOGD("gst_port(%p) created", port);
1024 MEDIACODEC_FLEAVE();
1028 static void mc_gst_port_free(mc_gst_port_t *port)
1030 MEDIACODEC_FENTER();
1032 g_mutex_clear(&port->mutex);
1033 g_queue_free(port->queue);
1035 LOGD("gst_port(%p) freed", port);
1038 MEDIACODEC_FLEAVE();
1043 gboolean _mc_update_packet_info(mc_gst_core_t *core, media_format_h format)
1045 gboolean is_format_change = FALSE;
1047 mc_gst_port_def_t *input_port_def = &core->ports[in_port_index]->port_def;
1054 gchar *sformat = NULL;
1055 media_format_mimetype_e mimetype = 0;
1057 media_format_get_video_info(format, &mimetype, &width, &height, &bitrate, NULL);
1058 media_format_get_video_frame_rate(format, &framerate);
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;
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);
1088 media_format_get_audio_info(format, NULL, &channel, &samplerate, &bit, &bitrate);
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;
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;
1112 return is_format_change;
1115 static int _mc_set_codec_data(mc_gst_core_t *core, GstMCBuffer *mcbuffer, bool codec_config)
1117 MEDIACODEC_FENTER();
1119 g_return_val_if_fail(core != NULL, MC_PARAM_ERROR);
1120 g_return_val_if_fail(mcbuffer != NULL, MC_PARAM_ERROR);
1122 int ret = MC_ERROR_NONE;
1124 switch (core->codec_id) {
1125 case MEDIACODEC_AAC:
1126 case MEDIACODEC_AAC_HE:
1127 case MEDIACODEC_AAC_HE_PS:
1129 ret = __mc_set_caps_codecdata(core, mcbuffer, AAC_CODECDATA_SIZE);
1130 if (ret != MC_ERROR_NONE)
1131 LOGW("__mc_set_caps_codecdata failed");
1134 case MEDIACODEC_WMAV1:
1135 case MEDIACODEC_WMAV2:
1136 case MEDIACODEC_WMAPRO:
1137 case MEDIACODEC_WMALSL:
1139 ret = __mc_set_caps_codecdata(core, mcbuffer, WMA_CODECDATA_SIZE);
1140 if (ret != MC_ERROR_NONE)
1141 LOGW("__mc_set_caps_codecdata failed");
1144 case MEDIACODEC_VORBIS:
1145 case MEDIACODEC_FLAC:
1147 ret = __mc_set_caps_streamheader(core, mcbuffer, VORBIS_CODECDATA_SIZE);
1148 if (ret != MC_ERROR_NONE)
1149 LOGW("__mc_set_caps_streamheader failed");
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");
1159 LOGD("doesn't need to set codecdata");
1163 MEDIACODEC_FLEAVE();
1169 static gpointer feed_task(gpointer data)
1171 gint ret = MC_ERROR_NONE;
1172 bool codec_config = 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;
1180 MEDIACODEC_FENTER();
1182 mc_gst_core_t *core = (mc_gst_core_t *)data;
1184 while (g_atomic_int_get(&core->available_queue->running)) {
1185 LOGD("waiting for next input....");
1186 input_buffer = _mc_get_input_buffer(core);
1190 media_packet_get_format(input_buffer, &format);
1192 if (_mc_update_packet_info(core, format))
1193 is_format_change = TRUE;
1194 media_format_unref(format);
1197 if (media_packet_is_codec_config(input_buffer, &codec_config) != MEDIA_PACKET_ERROR_NONE) {
1198 LOGE("media_packet_is_codec_config failed");
1202 if (media_packet_is_end_of_stream(input_buffer, &eos) != MEDIA_PACKET_ERROR_NONE) {
1203 LOGE("media_packet_is_end_of_stream failed");
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.
1211 LOGD("end of stream");
1212 gst_app_src_end_of_stream(GST_APP_SRC(core->appsrc));
1213 _mc_wait_for_eos(core);
1216 _mc_gst_handle_input_buffer_used(core, input_buffer);
1220 mcbuffer = _mc_gst_media_packet_to_gstbuffer(core, input_buffer, codec_config);
1222 LOGW("gstbuffer can't make");
1225 /* mcbuffer took the ownership of the input_buffer, so set input_buffer to NULL to aviod double unref */
1226 input_buffer = NULL;
1228 if (codec_config || initiative) {
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);
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;
1243 core->offset = GST_BUFFER_OFFSET_NONE;
1246 if (is_format_change) {
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;
1257 ret = _mc_gst_gstbuffer_to_appsrc(core, mcbuffer);
1258 if (ret != GST_FLOW_OK) {
1259 LOGE("Failed to push gst buffer");
1266 _mc_gst_handle_input_buffer_used(core, input_buffer);
1268 _mc_gst_set_flush_input(core);
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]);
1277 /*LOGE("status : input_buffer : %p, codec_config : %d, eos : %d, encoder : %d in feed_task", input_buffer, codec_config, eos, core->encoder);*/
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);
1286 MEDIACODEC_FLEAVE();
1291 static void __mc_gst_stop_feed(GstElement *pipeline, gpointer data)
1293 mc_gst_core_t *core = (mc_gst_core_t *)data;
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]);
1302 static void __mc_gst_start_feed(GstElement *pipeline, guint size, gpointer data)
1304 mc_gst_core_t *core = (mc_gst_core_t *)data;
1306 LOGI("start_feed called");
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]);
1314 GstCaps *_mc_gst_aud_caps_new(mc_gst_core_t *core, mediacodec_codec_type_e codec_id, gint index)
1316 GstCaps *caps = NULL;
1317 mc_gst_port_def_t *port_def = &core->ports[index]->port_def;
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);
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);
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",
1336 LOGD("CAPS for codec_id (MEDIACODEC_AAC_LC - normal ADTS)");
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!!!");
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",
1350 LOGD("CAPS for codec_id (MEDIACODEC_AAC_HE)");
1353 case MEDIACODEC_MP3:
1354 if (core->encoder) {
1355 LOGD("[MC_NOT_SUPPORTED] mp3 encoder is not supported yet!!!");
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 */
1364 LOGD("CAPS for codec_id (MEDIACODEC_MP3)");
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",
1373 LOGD("CAPS for codec_id (MEDIACODEC_AMR_NB)");
1375 gst_caps_set_simple(caps,
1376 "rate", G_TYPE_INT, 8000,
1380 case MEDIACODEC_AMR_WB:
1381 if (core->encoder) {
1382 LOGD("[MC_NOT_SUPPORTED] amr-wb encoder is not supported yet!!!");
1385 gst_caps_set_simple(caps,
1386 "rate", G_TYPE_INT, 16000,
1390 case MEDIACODEC_VORBIS:
1391 if (core->encoder) {
1392 LOGD("[MC_NOT_SUPPORTED] vorbis encoder is not supported yet!!!");
1397 case MEDIACODEC_FLAC:
1398 if (core->encoder) {
1399 LOGD("[MC_NOT_SUPPORTED] flac encoder is not supported yet!!!");
1402 gst_caps_set_simple(caps,
1403 "framed", G_TYPE_BOOLEAN, TRUE,
1404 /* FIXME - Insert More Info */
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!!!");
1417 * Need to extract from Stream Type Specific ... or
1418 * Need to get Demuxer's Caps from Stream Type Specific
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 */
1425 LOGD(" ----- WMA Need Additional Caps -----------");
1426 if (core->codec_id == MEDIACODEC_WMAV1) {
1427 format_tag = 352; /* 0x160 */
1429 } else if (core->codec_id == MEDIACODEC_WMAV2) {
1430 format_tag = 353; /* 0x161 */
1432 } else if (core->codec_id == MEDIACODEC_WMAPRO) {
1433 format_tag = 354; /* 0x162 */
1435 } else if (core->codec_id == MEDIACODEC_WMALSL) {
1436 format_tag = 355; /* 0x163 */
1439 LOGE("Not support WMA format");
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,
1458 GstCaps *_mc_gst_vid_caps_new(mc_gst_core_t *core, mediacodec_codec_type_e codec_id, gint index)
1460 GstCaps *caps = NULL;
1461 mc_gst_port_def_t *port_def = &core->ports[index]->port_def;
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,
1470 case MEDIACODEC_H263:
1471 if (core->encoder) {
1472 gst_caps_set_simple(caps,
1473 "format", G_TYPE_STRING, port_def->info.video.format,
1476 gst_caps_set_simple(caps,
1477 "mpegversion", G_TYPE_INT, 4,
1478 "framerate", GST_TYPE_FRACTION, 30, 1,
1482 case MEDIACODEC_MPEG4:
1483 if (core->encoder) {
1484 gst_caps_set_simple(caps,
1485 "format", G_TYPE_STRING, port_def->info.video.format,
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,
1496 case MEDIACODEC_H264:
1497 if (core->encoder) {
1498 gst_caps_set_simple(caps,
1499 "format", G_TYPE_STRING, port_def->info.video.format,
1501 g_object_set(GST_OBJECT(core->codec), "byte-stream", TRUE, NULL);
1502 LOGE("format : %s", port_def->info.video.format);
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,
1519 static int _mc_link_vtable(mc_gst_core_t *core, mediacodec_codec_type_e id, gboolean encoder, gboolean is_hw)
1521 MEDIACODEC_FENTER();
1523 g_return_val_if_fail(core != NULL, MC_PARAM_ERROR);
1526 case MEDIACODEC_AAC:
1528 core->vtable = aenc_aac_vtable;
1530 core->vtable = adec_aac_vtable;
1533 case MEDIACODEC_AAC_HE:
1534 case MEDIACODEC_AAC_HE_PS:
1536 LOGD("[MC_NOT_SUPPORTED] he-aac-v12 encoder is not supported yet!!!");
1537 return MC_NOT_SUPPORTED;
1539 core->vtable = adec_aacv12_vtable;
1542 case MEDIACODEC_MP3:
1544 LOGD("[MC_NOT_SUPPORTED] mp3 encoder is not supported yet!!!");
1545 return MC_NOT_SUPPORTED;
1547 core->vtable = adec_mp3_vtable;
1550 case MEDIACODEC_AMR_NB:
1551 LOGD("amrnb vtable");
1553 core->vtable = aenc_amrnb_vtable;
1555 core->vtable = adec_amrnb_vtable;
1558 case MEDIACODEC_AMR_WB:
1559 LOGD("amrwb vtable - Only support decoder");
1561 LOGD("[MC_NOT_SUPPORTED] amr-wb encoder is not supported yet!!!");
1562 return MC_NOT_SUPPORTED;
1564 core->vtable = adec_amrwb_vtable;
1567 case MEDIACODEC_VORBIS:
1568 LOGD("vorbis vtable");
1570 LOGD("[MC_NOT_SUPPORTED] vorbis encoder is not supported yet!!!");
1571 return MC_NOT_SUPPORTED;
1573 core->vtable = adec_vorbis_vtable;
1576 case MEDIACODEC_FLAC:
1577 LOGD("flac vtable");
1579 LOGD("[MC_NOT_SUPPORTED] flac encoder is not supported yet!!!");
1580 return MC_NOT_SUPPORTED;
1582 core->vtable = adec_flac_vtable;
1585 case MEDIACODEC_WMAV1:
1586 case MEDIACODEC_WMAV2:
1587 case MEDIACODEC_WMAPRO:
1588 case MEDIACODEC_WMALSL:
1589 LOGD("wma(V1 / V2 / LSL / PRO) vtable");
1591 LOGD("[MC_NOT_SUPPORTED] wma encoder is not supported yet!!!");
1592 return MC_NOT_SUPPORTED;
1594 core->vtable = adec_wma_vtable;
1597 case MEDIACODEC_H263:
1598 LOGD("h263 vtable");
1600 core->vtable = is_hw ? venc_h263_hw_vtable : venc_h263_sw_vtable;
1603 core->vtable = vdec_h263_hw_vtable;
1605 core->vtable = vdec_h263_sw_vtable;
1609 case MEDIACODEC_MPEG4:
1610 LOGD("mpeg4 vtable");
1612 core->vtable = is_hw ? venc_mpeg4_hw_vtable : venc_mpeg4_sw_vtable;
1614 core->vtable = is_hw ? vdec_mpeg4_hw_vtable : vdec_mpeg4_sw_vtable;
1617 case MEDIACODEC_H264:
1618 LOGD("h264 vtable");
1620 core->vtable = is_hw ? venc_h264_hw_vtable : venc_vtable;
1623 core->vtable = vdec_h264_hw_vtable;
1625 core->vtable = vdec_h264_sw_vtable;
1633 MEDIACODEC_FLEAVE();
1635 return MC_ERROR_NONE;
1638 static gint _mc_gst_gstbuffer_to_appsrc(mc_gst_core_t *core, GstMCBuffer *mcbuffer)
1640 return gst_app_src_push_buffer(GST_APP_SRC(core->appsrc), mcbuffer->buffer);
1643 media_packet_h _mc_get_input_buffer(mc_gst_core_t *core)
1645 LOGD("waiting for input...");
1646 return mc_async_queue_pop(core->available_queue->input);
1649 mc_ret_e mc_gst_prepare(mc_handle_t *mc_handle)
1651 MEDIACODEC_FENTER();
1653 int ret = MC_ERROR_NONE;
1654 media_format_mimetype_e out_mime;
1655 int num_supported_codec = 0;
1657 GstCaps *caps = NULL;
1660 return MC_PARAM_ERROR;
1662 mediacodec_codec_type_e id;
1666 gchar *factory_name = NULL;
1667 mc_codec_map_t *codec_map;
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;
1676 for (i = 0; i < num_supported_codec; i++) {
1677 if ((id == codec_map[i].id) && (hardware == codec_map[i].hardware))
1681 if (i == num_supported_codec)
1682 return MC_NOT_SUPPORTED;
1684 factory_name = codec_map[i].type.factory_name;
1685 out_mime = codec_map[i].type.out_format;
1687 /* gst_core create */
1688 mc_gst_core_t *new_core = mc_gst_core_new();
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;
1701 new_core->bufmgr = tbm_bufmgr_init(new_core->drm_fd);
1702 if (new_core->bufmgr == NULL) {
1703 LOGE("TBM initialization failed");
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);
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");
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]);
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");
1734 ret = _mc_link_vtable(new_core, id, encoder, hardware);
1735 if (ret != MC_ERROR_NONE) {
1736 LOGE("vtable link failed");
1740 /* set caps in capsfilter as default*/
1741 caps = new_core->mc_caps_new(new_core, id, out_port_index);
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;
1748 format = new_core->is_hw ? "SN12" : "I420";
1749 gst_caps_set_simple(caps,
1750 "format", G_TYPE_STRING, format,
1753 g_object_set(new_core->capsfilter, "caps", caps, NULL);
1754 gst_caps_unref(caps);
1756 mc_handle->core = new_core;
1757 LOGD("initialized... %d", ret);
1758 MEDIACODEC_FLEAVE();
1763 mc_ret_e mc_gst_unprepare(mc_handle_t *mc_handle)
1765 MEDIACODEC_FENTER();
1768 int ret = MC_ERROR_NONE;
1769 mc_gst_core_t *core = NULL;
1772 return MC_PARAM_ERROR;
1774 core = (mc_gst_core_t *)mc_handle->core;
1777 LOGD("@%p(%p) core is uninitializing... v(%d)e(%d)", mc_handle, core, core->video, core->encoder);
1779 g_mutex_lock(&core->drain_lock);
1780 core->unprepare_flag = TRUE;
1781 g_mutex_unlock(&core->drain_lock);
1784 _mc_send_eos_signal(core);
1786 _mc_gst_set_flush_input(core);
1788 ret = _mc_gst_destroy_pipeline(core);
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]);
1800 media_format_unref(core->output_fmt);
1802 if (core->bufmgr != NULL) {
1803 tbm_bufmgr_deinit(core->bufmgr);
1804 core->bufmgr = NULL;
1807 if (core->drm_fd != -1) {
1808 close(core->drm_fd);
1809 LOGD("close drm_fd");
1813 mc_gst_core_free(core);
1814 mc_handle->core = NULL;
1818 MEDIACODEC_FLEAVE();
1823 mc_ret_e mc_gst_process_input(mc_handle_t *mc_handle, media_packet_h inbuf, uint64_t timeout_us)
1825 MEDIACODEC_FENTER();
1827 int ret = MC_ERROR_NONE;
1828 mc_gst_core_t *core = NULL;
1831 return MC_PARAM_ERROR;
1833 core = (mc_gst_core_t *)mc_handle->core;
1835 g_mutex_lock(&core->drain_lock);
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);
1842 g_mutex_unlock(&core->drain_lock);
1844 if (core->prepare_count == 0)
1845 return MC_INVALID_STATUS;
1847 g_mutex_lock(&core->drain_lock);
1849 if (!core->eos || !core->unprepare_flag) {
1850 mc_async_queue_push(core->available_queue->input, inbuf);
1853 ret = MC_INVALID_IN_BUF;
1854 g_mutex_unlock(&core->drain_lock);
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);
1862 MEDIACODEC_FLEAVE();
1867 mc_ret_e mc_gst_get_output(mc_handle_t *mc_handle, media_packet_h *outbuf, uint64_t timeout_us)
1869 gint ret = MC_ERROR_NONE;
1870 gint64 end_time = -1;
1872 mc_gst_core_t *core = NULL;
1873 media_packet_h out_packet = NULL;
1875 MEDIACODEC_FENTER();
1876 end_time = g_get_monotonic_time() + timeout_us;
1879 return MC_PARAM_ERROR;
1881 core = (mc_gst_core_t *)mc_handle->core;
1883 g_mutex_lock(&core->ports[1]->mutex);
1885 if (!g_queue_is_empty(core->ports[1]->queue))
1888 LOGD("no buffer, wait for signal");
1889 signalled = g_cond_wait_until(&core->out_buffer_cond, &core->ports[1]->mutex, end_time);
1893 out_packet = g_queue_pop_head(core->ports[1]->queue);
1895 LOGW("Failed to get buffer");
1896 ret = MC_OUTPUT_BUFFER_EMPTY;
1899 *outbuf = out_packet;
1900 LOGI("@%p v(%d)e(%d) got buffer :%p", core, core->video, core->encoder, out_packet);
1902 g_mutex_unlock(&core->ports[1]->mutex);
1904 MEDIACODEC_FLEAVE();
1909 mc_ret_e mc_gst_flush_buffers(mc_handle_t *mc_handle)
1911 gint ret = MC_ERROR_NONE;
1912 mc_gst_core_t *core = NULL;
1914 MEDIACODEC_FENTER();
1917 return MC_PARAM_ERROR;
1919 core = (mc_gst_core_t *)mc_handle->core;
1920 LOGI("@%p v(%d)e(%d) flush_buffers", core, core->video, core->encoder);
1922 ret = _mc_gst_flush_buffers(core);
1924 MEDIACODEC_FLEAVE();
1929 static gboolean __mc_gst_init_gstreamer()
1931 MEDIACODEC_FENTER();
1935 static gboolean initialized = FALSE;
1936 static const int max_argc = 50;
1938 gchar **argv = NULL;
1939 gchar **argv2 = NULL;
1943 LOGD("gstreamer already initialized.\n");
1948 argc = malloc(sizeof(int));
1949 argv = malloc(sizeof(gchar *) *max_argc);
1950 argv2 = malloc(sizeof(gchar *) *max_argc);
1952 if (!argc || !argv || !argv2)
1955 memset(argv, 0, sizeof(gchar *) *max_argc);
1956 memset(argv2, 0, sizeof(gchar *) *max_argc);
1960 argv[0] = g_strdup("media codec");
1962 /* we would not do fork for scanning plugins */
1963 argv[*argc] = g_strdup("--gst-disable-registry-fork");
1966 /* check disable registry scan */
1967 argv[*argc] = g_strdup("--gst-disable-registry-update");
1970 /* check disable segtrap */
1971 argv[*argc] = g_strdup("--gst-disable-segtrap");
1974 LOGD("initializing gstreamer with following parameter\n");
1975 LOGD("argc : %d\n", *argc);
1978 for (i = 0; i < arg_count; i++) {
1980 LOGD("argv[%d] : %s\n", i, argv2[i]);
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");
1993 for (i = 0; i < arg_count; i++)
1994 MC_FREEIF(argv2[i]);
2003 MEDIACODEC_FLEAVE();
2008 for (i = 0; i < arg_count; i++) {
2009 LOGD("free[%d] : %s\n", i, argv2[i]);
2010 MC_FREEIF(argv2[i]);
2020 mc_ret_e _mc_gst_create_pipeline(mc_gst_core_t *core, gchar *factory_name)
2024 MEDIACODEC_FENTER();
2026 g_mutex_lock(&core->prepare_lock);
2027 if (core->prepare_count == 0) {
2029 if (!__mc_gst_init_gstreamer()) {
2030 LOGE("gstreamer initialize fail");
2031 g_mutex_unlock(&core->prepare_lock);
2032 return MC_NOT_INITIALIZED;
2034 core->codec = gst_element_factory_make(factory_name, NULL);
2037 LOGE("codec element create fail");
2041 LOGD("@%p v(%d)e(%d) create_pipeline", core, core->video, core->encoder);
2042 MEDIACODEC_ELEMENT_SET_STATE(core->codec, GST_STATE_READY);
2044 /* create common elements */
2045 core->pipeline = gst_pipeline_new(NULL);
2047 if (!core->pipeline) {
2048 LOGE("pipeline create fail");
2052 core->appsrc = gst_element_factory_make("appsrc", NULL);
2054 if (!core->appsrc) {
2055 LOGE("appsrc can't create");
2059 if (!core->video && core->encoder) {
2060 core->audioconvert = gst_element_factory_make("audioconvert", NULL);
2062 if (!core->audioconvert) {
2063 LOGE("capsfilter can't create");
2067 core->audioresample = gst_element_factory_make("audioresample", NULL);
2069 if (!core->audioresample) {
2070 LOGE("capsfilter can't create");
2075 if (core->video && core->encoder) {
2076 core->videoconvert = gst_element_factory_make("videoconvert", NULL);
2078 if (!core->videoconvert) {
2079 LOGE("videoconvert can't create");
2083 core->videoscale = gst_element_factory_make("videoscale", NULL);
2084 if (!core->videoscale) {
2085 LOGE("videoscale can't create");
2090 core->capsfilter = gst_element_factory_make("capsfilter", NULL);
2092 if (!core->capsfilter) {
2093 LOGE("capsfilter can't create");
2097 core->fakesink = gst_element_factory_make("fakesink", NULL);
2099 if (!core->fakesink) {
2100 LOGE("fakesink create fail");
2103 g_object_set(core->fakesink, "enable-last-sample", FALSE, NULL);
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);
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");
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);
2118 if (!(gst_element_link_many(core->appsrc, core->codec, core->fakesink, NULL))) {
2119 LOGE("gst_element_link_many is failed");
2123 gst_bin_add_many(GST_BIN(core->pipeline), core->appsrc, core->capsfilter, core->codec, core->fakesink, NULL);
2126 if (!(gst_element_link_many(core->appsrc, core->capsfilter, core->codec, core->fakesink, NULL))) {
2127 LOGE("gst_element_link_many is failed");
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();
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));
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);
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);
2149 /* set state PLAYING */
2150 MEDIACODEC_ELEMENT_SET_STATE(GST_ELEMENT_CAST(core->pipeline), GST_STATE_PLAYING);
2154 core->prepare_count++;
2155 g_mutex_unlock(&core->prepare_lock);
2157 MEDIACODEC_FLEAVE();
2159 return MC_ERROR_NONE;
2161 STATE_CHANGE_FAILED:
2165 gst_object_unref(GST_OBJECT(core->codec));
2168 gst_object_unref(GST_OBJECT(core->pipeline));
2171 gst_object_unref(GST_OBJECT(core->appsrc));
2173 if (core->capsfilter)
2174 gst_object_unref(GST_OBJECT(core->capsfilter));
2176 if (core->audioresample)
2177 gst_object_unref(GST_OBJECT(core->audioresample));
2179 if (core->videoscale)
2180 gst_object_unref(GST_OBJECT(core->videoscale));
2182 if (core->videoconvert)
2183 gst_object_unref(GST_OBJECT(core->videoconvert));
2185 if (core->audioconvert)
2186 gst_object_unref(GST_OBJECT(core->audioconvert));
2189 gst_object_unref(GST_OBJECT(core->fakesink));
2191 g_mutex_unlock(&core->prepare_lock);
2196 mc_ret_e _mc_gst_destroy_pipeline(mc_gst_core_t *core)
2198 int ret = MC_ERROR_NONE;
2200 MEDIACODEC_FENTER();
2202 g_mutex_lock(&core->prepare_lock);
2203 core->prepare_count--;
2204 if (core->prepare_count == 0) {
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");
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");
2222 MEDIACODEC_ELEMENT_SET_STATE(core->pipeline, GST_STATE_NULL);
2224 gst_object_unref(GST_OBJECT(core->pipeline));
2226 gst_caps_unref(core->caps);
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);
2235 MEDIACODEC_FLEAVE();
2239 STATE_CHANGE_FAILED:
2241 gst_object_unref(GST_OBJECT(core->pipeline));
2243 LOGD("@%p v(%d)e(%d) destroy_pipeline failed", core, core->video, core->encoder);
2244 g_mutex_unlock(&core->prepare_lock);
2249 void __mc_push_output_to_queue(mc_gst_core_t *core, GstMCBuffer *mcbuffer)
2251 g_mutex_lock(&core->ports[1]->mutex);
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);
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)));
2261 LOGD("dq : v(%d)e(%d)", core->video, core->encoder);
2264 g_mutex_unlock(&core->ports[1]->mutex);
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]);
2272 void __mc_gst_buffer_add(GstElement *element, GstBuffer *buffer, GstPad *pad, gpointer data)
2274 GstMCBuffer *mcbuffer = NULL;
2276 MEDIACODEC_FENTER();
2278 mc_gst_core_t *core = (mc_gst_core_t *)data;
2280 gst_buffer_ref(buffer);
2282 mcbuffer = __mc_gst_make_media_packet(core, buffer);
2284 LOGI("@%p(%d) out_packet : %p", core, core->encoder, mcbuffer->packet);
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));
2291 if (core->need_codec_data) {
2292 media_packet_set_flags(mcbuffer->packet, MEDIA_PACKET_CODEC_CONFIG);
2293 core->need_codec_data = false;
2296 if (core->need_sync_flag) {
2297 media_packet_set_flags(mcbuffer->packet, MEDIA_PACKET_SYNC_FRAME);
2298 core->need_sync_flag = false;
2301 __mc_push_output_to_queue(core, mcbuffer);
2303 gst_buffer_unref(buffer);
2304 LOGE("Failed to dequeue output packet");
2307 MEDIACODEC_FLEAVE();
2312 int __mc_output_buffer_finalize_cb(media_packet_h packet, int error_code, void *user_data)
2314 MEDIACODEC_FENTER();
2316 GstMCBuffer *mcbuffer = (GstMCBuffer *)user_data;
2318 gst_buffer_unref((GstBuffer *)mcbuffer->buffer);
2320 if (mcbuffer->ext_mem) {
2321 g_free(mcbuffer->ext_mem);
2322 LOGD("freed external memory");
2323 mcbuffer->ext_mem = NULL;
2326 LOGD("output port filled buffer : %p", packet);
2329 MEDIACODEC_FLEAVE();
2331 return MEDIA_PACKET_FINALIZE;
2334 gchar *__mc_get_gst_input_format(media_format_mimetype_e mimetype, bool is_hw)
2336 gchar *format = NULL;
2339 case MEDIA_FORMAT_I420:
2342 case MEDIA_FORMAT_NV12:
2348 case MEDIA_FORMAT_ARGB:
2354 LOGD("input packet format : %s", format);
2358 GstMCBuffer *_mc_gst_media_packet_to_gstbuffer(mc_gst_core_t *core, media_packet_h packet, bool codec_config)
2360 gint ret = MEDIA_PACKET_ERROR_NONE;
2361 uint64_t buf_size = 0;
2365 void *buf_data = NULL;
2366 GstMCBuffer *mcbuffer = NULL;
2368 ret = media_packet_get_buffer_size(packet, &buf_size);
2369 if (ret != MEDIA_PACKET_ERROR_NONE) {
2370 LOGW("buffer size get fail");
2374 ret = media_packet_get_buffer_data_ptr(packet, &buf_data);
2375 if (ret != MEDIA_PACKET_ERROR_NONE) {
2376 LOGW("buffer data get fail");
2380 mcbuffer = gst_mediacodec_buffer_new(core, packet, buf_size);
2381 if (mcbuffer == NULL) {
2382 LOGW("failed to create inbuf");
2386 LOGD("packet :v(%d)e(%d) %p, buf_size : %d", core->video, core->encoder, packet, (int)buf_size);
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);
2392 * if __mc_fill_input_buffer() failed, no memory appended to mcbuffer->buffer, so gst_mediacodec_buffer_finalize()
2393 * will not be invoked
2395 gst_buffer_unref(mcbuffer->buffer);
2401 media_packet_get_pts(packet, &dts);
2402 GST_BUFFER_DTS(mcbuffer->buffer) = dts;
2405 media_packet_get_pts(packet, &pts);
2406 GST_BUFFER_PTS(mcbuffer->buffer) = pts;
2409 media_packet_get_duration(packet, &dur);
2410 GST_BUFFER_DURATION(mcbuffer->buffer) = dur;
2412 GST_BUFFER_OFFSET(mcbuffer->buffer) = core->offset;
2414 core->offset += buf_size;
2415 GST_BUFFER_OFFSET_END(mcbuffer->buffer) = core->offset;
2420 GstMCBuffer *__mc_gst_make_media_packet(mc_gst_core_t *core, GstBuffer *buffer)
2422 gint ret = MEDIA_PACKET_ERROR_NONE;
2424 GstMapInfo map = GST_MAP_INFO_INIT;
2425 GstMCBuffer *mcbuffer = NULL;
2430 mcbuffer = (GstMCBuffer *)g_malloc0(sizeof(*mcbuffer));
2432 if (mcbuffer == NULL) {
2433 LOGE("malloc fail");
2437 mem = gst_buffer_peek_memory(buffer, 0);
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);
2443 gst_memory_map(mem, &map, GST_MAP_READ);
2446 gst_memory_unmap(mem, &map);
2449 mcbuffer->buffer = buffer;
2450 mcbuffer->core = core;
2451 mcbuffer->buf_size = size;
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);
2462 int __mc_gst_create_eos_packet(media_format_h fmt, GstMCBuffer *mcbuffer)
2464 gint ret = MC_ERROR_NONE;
2465 media_packet_h packet = NULL;
2467 MEDIACODEC_FENTER();
2468 if (media_packet_create(fmt, __mc_output_buffer_finalize_cb, mcbuffer, &packet)) {
2469 LOGE("media_packet_create failed\n");
2474 if (media_packet_set_flags(packet, MEDIA_PACKET_END_OF_STREAM)) {
2475 LOGE("unable to set EOS flag\n");
2480 mcbuffer->buffer = NULL;
2481 mcbuffer->ext_mem = NULL;
2482 mcbuffer->buf_size = 0;
2483 mcbuffer->packet = packet;
2484 MEDIACODEC_FLEAVE();
2489 media_packet_destroy(packet);
2490 MEDIACODEC_FLEAVE();
2494 void _mc_gst_handle_input_buffer_used(mc_gst_core_t *core, media_packet_h packet)
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);
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]);
2505 g_mutex_lock(&core->drain_lock);
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);
2512 g_mutex_unlock(&core->drain_lock);
2516 gboolean __mc_gst_bus_callback(GstBus *bus, GstMessage *msg, gpointer data)
2518 gint ret = MC_ERROR_NONE;
2519 GstMCBuffer *mcbuffer = NULL;
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)));
2524 switch (GST_MESSAGE_TYPE(msg)) {
2526 case GST_MESSAGE_EOS:
2527 _mc_send_eos_signal(core);
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.");
2534 LOGE("failed to create eos packet.");
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]);
2543 LOGD("End of stream\n");
2546 case GST_MESSAGE_ERROR:
2548 GError *error = NULL;
2550 gst_message_parse_error(msg, &error, NULL);
2553 LOGW("GST error message parsing failed");
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);
2567 LOGW("Unexpected error has occured");
2569 g_mutex_lock(&core->drain_lock);
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);
2577 g_mutex_unlock(&core->drain_lock);
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);
2585 g_error_free(error);
2596 static gboolean __mc_gst_check_useful_message(mc_gst_core_t *core, GstMessage *msg)
2598 gboolean retval = FALSE;
2600 if (!core->pipeline) {
2601 LOGE("mediacodec pipeline handle is null");
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:
2620 static GstBusSyncReply __mc_gst_bus_sync_callback(GstBus *bus, GstMessage *msg, gpointer data)
2622 mc_gst_core_t *core = (mc_gst_core_t *)data;
2623 GstBusSyncReply reply = GST_BUS_DROP;
2625 LOGD("__mc_gst_bus_sync_callback is called : %s", gst_message_type_get_name(GST_MESSAGE_TYPE(msg)));
2627 if (!core->pipeline) {
2628 LOGE("mediacodec pipeline handle is null");
2629 return GST_BUS_PASS;
2632 if (!__mc_gst_check_useful_message(core, msg)) {
2633 gst_message_unref(msg);
2634 return GST_BUS_DROP;
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;
2645 reply = GST_BUS_PASS;
2649 if (reply == GST_BUS_DROP)
2650 gst_message_unref(msg);
2655 static GstMemory *__mc_gst_make_tbm_buffer(mc_gst_core_t *core, GstMCBuffer *mcbuffer)
2657 int ret = MEDIA_PACKET_ERROR_NONE;
2658 tbm_surface_h surface = NULL;
2660 GstMemory *mem = NULL;
2662 if (!mcbuffer->packet) {
2663 LOGE("output is null");
2667 ret = media_packet_get_tbm_surface(mcbuffer->packet, &surface);
2668 if (ret != MEDIA_PACKET_ERROR_NONE) {
2669 LOGE("Failed to get tbm surface");
2673 if (!gst_video_info_from_caps(&vinfo, core->caps)) {
2674 LOGE("Failed to get video info");
2678 mem = gst_tizen_allocator_alloc_surface(core->allocator,
2679 &vinfo, surface, mcbuffer, (GDestroyNotify)__mc_input_buffer_finalize_cb);
2684 static void __mc_input_buffer_finalize_cb(GstMCBuffer *mcbuffer)
2686 MEDIACODEC_FENTER();
2691 LOGD("============>>>>> _finalize_cb : %p, %p", mcbuffer, mcbuffer->packet);
2693 _mc_gst_handle_input_buffer_used(mcbuffer->core, mcbuffer->packet);
2697 MEDIACODEC_FLEAVE();
2700 static GstMCBuffer *gst_mediacodec_buffer_new(mc_gst_core_t *core, media_packet_h packet, uint64_t size)
2702 GstMCBuffer *mcbuffer = NULL;
2704 mcbuffer = (GstMCBuffer *)g_malloc0(sizeof(GstMCBuffer));
2706 mcbuffer->buffer = gst_buffer_new();
2707 mcbuffer->buf_size = 0;
2709 LOGD("creating buffer : %p, %p", mcbuffer, mcbuffer->buffer);
2710 mcbuffer->core = core;
2711 mcbuffer->packet = packet;
2716 static gint __gst_handle_core_error(mc_gst_core_t *core, int code)
2718 gint trans_err = MEDIACODEC_ERROR_NONE;
2720 g_return_val_if_fail(core, MC_PARAM_ERROR);
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:
2739 trans_err = MEDIACODEC_ERROR_INVALID_STREAM;
2746 static gint __gst_handle_library_error(mc_gst_core_t *core, int code)
2748 gint trans_err = MEDIACODEC_ERROR_NONE;
2750 g_return_val_if_fail(core, MC_PARAM_ERROR);
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:
2760 trans_err = MEDIACODEC_ERROR_INVALID_STREAM;
2768 static gint __gst_handle_resource_error(mc_gst_core_t *core, int code)
2770 gint trans_err = MEDIACODEC_ERROR_NONE;
2772 g_return_val_if_fail(core, MC_PARAM_ERROR);
2775 case GST_RESOURCE_ERROR_NO_SPACE_LEFT:
2776 trans_err = MEDIACODEC_ERROR_NO_FREE_SPACE;
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:
2789 trans_err = MEDIACODEC_ERROR_INTERNAL;
2796 static gint __gst_handle_stream_error(mc_gst_core_t *core, GError *error, GstMessage *message)
2798 gint trans_err = MEDIACODEC_ERROR_NONE;
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);
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);
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:
2822 trans_err = MEDIACODEC_ERROR_INVALID_STREAM;
2829 static gint __gst_transform_gsterror(mc_gst_core_t *core, GstMessage *message, GError *error)
2831 gchar *src_element_name = NULL;
2832 GstElement *src_element = NULL;
2833 GstElementFactory *factory = NULL;
2834 const gchar *klass = NULL;
2837 src_element = GST_ELEMENT_CAST(message->src);
2839 goto INTERNAL_ERROR;
2841 src_element_name = GST_ELEMENT_NAME(src_element);
2842 if (!src_element_name)
2843 goto INTERNAL_ERROR;
2845 factory = gst_element_get_factory(src_element);
2847 goto INTERNAL_ERROR;
2849 klass = gst_element_factory_get_klass(factory);
2851 goto INTERNAL_ERROR;
2853 LOGD("error code=%d, msg=%s, src element=%s, class=%s\n",
2854 error->code, error->message, src_element_name, klass);
2856 switch (error->code) {
2857 case GST_STREAM_ERROR_DECODE:
2858 case GST_STREAM_ERROR_FAILED:
2859 return MEDIACODEC_ERROR_INVALID_STREAM;
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;
2869 return MEDIACODEC_ERROR_INTERNAL;
2874 return MEDIACODEC_ERROR_INTERNAL;
2877 static gint _mc_gst_flush_buffers(mc_gst_core_t *core)
2879 gboolean ret = FALSE;
2880 GstEvent *event = NULL;
2882 MEDIACODEC_FENTER();
2884 _mc_gst_set_flush_input(core);
2886 LOGW("Trying send flush_start .... ");
2887 event = gst_event_new_flush_start();
2888 ret = gst_element_send_event(core->appsrc, event);
2890 LOGE("failed to send Flush_Start event");
2894 LOGW("Trying send flush_stop .... ");
2895 event = gst_event_new_flush_stop(TRUE);
2896 ret = gst_element_send_event(core->appsrc, event);
2898 LOGE("failed to send Flush_Stop event");
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);
2906 ret = gst_element_send_event(core->appsrc, event);
2908 LOGE("failed to send seek event");
2912 _mc_gst_set_flush_output(core);
2914 MEDIACODEC_FLEAVE();
2916 return MC_ERROR_NONE;
2920 static void _mc_gst_set_flush_input(mc_gst_core_t *core)
2922 media_packet_h packet = NULL;
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);
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);
2931 _mc_gst_handle_input_buffer_used(core, packet);
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();
2939 static void _mc_gst_set_flush_output(mc_gst_core_t *core)
2941 media_packet_h packet = NULL;
2943 MEDIACODEC_FENTER();
2944 g_mutex_lock(&core->ports[1]->mutex);
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);
2950 media_packet_destroy(packet);
2951 LOGD("outpacket destroyed");
2955 g_mutex_unlock(&core->ports[1]->mutex);
2956 MEDIACODEC_FLEAVE();
2959 #if TIZEN_EXYNOS_SPECIFIC
2961 * Get tiled address of position(x,y)
2964 * width of tiled[in]
2967 * height of tiled[in]
2970 * x position of tield[in]
2973 * y position of tield[in]
2976 * address of tiled data
2978 static gint __tile_4x2_read(gint x_size, gint y_size, gint x_pos, gint y_pos)
2980 gint pixel_x_m1, pixel_y_m1;
2982 gint linear_addr0, linear_addr1, bank_addr ;
2986 pixel_x_m1 = x_size - 1;
2987 pixel_y_m1 = y_size - 1;
2989 roundup_x = ((pixel_x_m1 >> 7) + 1);
2991 x_addr = x_pos >> 2;
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));
2998 if (((x_addr >> 5) & 0x1) == ((y_pos >> 5) & 0x1))
2999 bank_addr = ((x_addr >> 4) & 0x1);
3001 bank_addr = 0x2 | ((x_addr >> 4) & 0x1);
3003 linear_addr0 = (((y_pos & 0x1f) << 4) | (x_addr & 0xf));
3004 linear_addr1 = (((y_pos >> 6) & 0xff) * roundup_x + ((x_addr >> 5) & 0x7f));
3006 if (((x_addr >> 5) & 0x1) == ((y_pos >> 5) & 0x1))
3007 bank_addr = ((x_addr >> 4) & 0x1);
3009 bank_addr = 0x2 | ((x_addr >> 4) & 0x1);
3012 linear_addr0 = linear_addr0 << 2;
3013 trans_addr = (linear_addr1 << 13) | (bank_addr << 11) | linear_addr0;
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
3025 * @param yuv420_dest
3026 * Y or UV plane address of YUV420[out]
3029 * Y or UV plane address of NV12T[in]
3031 * @param yuv420_width
3032 * Width of YUV420[in]
3034 * @param yuv420_height
3035 * Y: Height of YUV420, UV: Height/2 of YUV420[in]
3044 * Crop size of right
3047 * Crop size of buttom
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)
3054 gint tiled_offset = 0, tiled_offset1 = 0;
3055 gint linear_offset = 0;
3056 gint temp1 = 0, temp2 = 0, temp3 = 0, temp4 = 0;
3058 temp3 = yuv420_width-right;
3060 /* real width is greater than or equal 256 */
3062 for (i = top; i < yuv420_height-buttom; i += 1) {
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;
3080 temp2 = ((yuv420_height+31)>>5)<<5;
3081 if ((i + 32) < temp2) {
3082 /* even1 fomula: x+((x+2)>>2)<<2+x_block_num*y */
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;
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;
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;
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;
3117 temp2 = yuv420_width-right-(64-temp1);
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;
3137 tiled_offset = tiled_offset+temp4*2048;
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;
3151 tiled_offset1 = tiled_offset1+temp4*2048;
3152 temp2 = yuv420_width-right-j;
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);
3166 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, temp2);
3169 } else if (temp1 >= 64) {
3170 for (i = top; i < (yuv420_height-buttom); i += 1) {
3172 tiled_offset = __tile_4x2_read(yuv420_width, yuv420_height, j, i);
3173 temp2 = ((j+64)>>6)<<6;
3175 linear_offset = temp1*(i-top);
3177 tiled_offset = tiled_offset+temp4;
3178 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, temp2);
3179 linear_offset = linear_offset+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;
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;
3194 tiled_offset = __tile_4x2_read(yuv420_width, yuv420_height, j, i);
3196 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, temp2);
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);
3205 tiled_offset = tiled_offset+temp4;
3206 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 2);
3207 linear_offset = linear_offset+2;
3214 void _mc_send_eos_signal(mc_gst_core_t *core)
3216 g_mutex_lock(&core->eos_mutex);
3218 g_cond_broadcast(&core->eos_cond);
3219 g_mutex_unlock(&core->eos_mutex);
3220 LOGD("send EOS signal");
3223 void _mc_wait_for_eos(mc_gst_core_t *core)
3225 g_mutex_lock(&core->eos_mutex);
3227 LOGD("waiting for EOS");
3230 g_cond_wait(&core->eos_cond, &core->eos_mutex);
3232 LOGD("received EOS");
3233 g_mutex_unlock(&core->eos_mutex);
3236 const gchar * _mc_error_to_string(mc_ret_e err)
3238 guint err_u = (guint) err;
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";
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";
3278 return "Unknown error";
3283 const gchar * _mc_bit_to_string(int bit)
3296 int _mc_get_mime(mc_gst_core_t *core)
3298 media_format_mimetype_e mime = MEDIA_FORMAT_MAX;
3300 switch (core->codec_id) {
3301 case MEDIACODEC_H264:
3303 mime = (core->is_hw) ? MEDIA_FORMAT_NV12 : MEDIA_FORMAT_I420;
3305 mime = MEDIA_FORMAT_H264_SP;
3307 case MEDIACODEC_MPEG4:
3309 mime = (core->is_hw) ? MEDIA_FORMAT_NV12 : MEDIA_FORMAT_I420;
3311 mime = MEDIA_FORMAT_MPEG4_SP;
3314 case MEDIACODEC_H263:
3316 mime = (core->is_hw) ? MEDIA_FORMAT_NV12 : MEDIA_FORMAT_I420;
3318 mime = MEDIA_FORMAT_H263P;
3321 case MEDIACODEC_AAC:
3323 mime = MEDIA_FORMAT_PCM_F32LE; /*FIXME need to change PCM supported by chipset */
3325 mime = MEDIA_FORMAT_AAC;
3328 case MEDIACODEC_AAC_HE:
3330 mime = MEDIA_FORMAT_PCM_F32LE; /* FIXME need to change PCM supported by chipset */
3332 mime = MEDIA_FORMAT_AAC_HE;
3335 case MEDIACODEC_AAC_HE_PS:
3337 case MEDIACODEC_MP3:
3338 mime = MEDIA_FORMAT_MP3;
3340 case MEDIACODEC_VORBIS:
3342 case MEDIACODEC_FLAC:
3344 case MEDIACODEC_WMAV1:
3346 case MEDIACODEC_WMAV2:
3348 case MEDIACODEC_WMAPRO:
3350 case MEDIACODEC_WMALSL:
3352 case MEDIACODEC_AMR_NB:
3353 mime = MEDIA_FORMAT_AMR_NB;
3355 case MEDIACODEC_AMR_WB:
3356 mime = MEDIA_FORMAT_AMR_WB;
3359 LOGE("NOT SUPPORTED!!!!");
3365 mc_ret_e mc_gst_get_packet_pool(mc_handle_t *mc_handle, media_packet_pool_h *packet_pool)
3367 gint ret = MC_ERROR_NONE;
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;
3376 return MC_PARAM_ERROR;
3378 core = (mc_gst_core_t *)mc_handle->core;
3380 ret = media_packet_pool_create(&pool);
3381 if (ret != MEDIA_PACKET_ERROR_NONE) {
3382 LOGE("media_packet_pool_create failed");
3386 if (media_format_create(&fmt_handle) != MEDIA_FORMAT_ERROR_NONE) {
3387 LOGE("media format create failed");
3388 media_packet_pool_destroy(pool);
3392 mime_format = _mc_get_mime(core);
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);
3400 media_format_set_video_avg_bps(fmt_handle, mc_handle->info.video.bitrate);
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);
3407 media_format_set_audio_avg_bps(fmt_handle, mc_handle->info.audio.bitrate);
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");
3417 /* will use default size temporarily */
3418 max_size = core->encoder ? DEFAULT_ENC_POOL_SIZE : DEFAULT_DEC_POOL_SIZE;
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");
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;
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);
3437 *packet_pool = pool;
3438 return MC_ERROR_NONE;