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.
22 #include <media_codec_queue.h>
23 #include <media_codec_port_gst.h>
24 #include <media_codec_util.h>
27 #include <gst/gstelement.h>
28 #include <gst/app/gstappsrc.h>
30 #ifdef TIZEN_PROFILE_LITE
32 #include <sys/ioctl.h>
33 #include <linux/ion.h>
37 * Internal Implementation
39 static gpointer feed_task(gpointer data);
40 static void __mc_gst_stop_feed(GstElement *pipeline, gpointer data);
41 static void __mc_gst_start_feed(GstElement *pipeline, guint size, gpointer data);
42 static media_packet_h _mc_get_input_buffer(mc_gst_core_t *core);
44 static gboolean __mc_gst_init_gstreamer();
45 static int _mc_output_media_packet_new(mc_gst_core_t *core, bool video, bool encoder, media_format_mimetype_e out_mime);
46 static mc_ret_e _mc_gst_create_pipeline(mc_gst_core_t *core, gchar *factory_name);
47 static mc_ret_e _mc_gst_destroy_pipeline(mc_gst_core_t *core);
48 static void __mc_gst_buffer_add(GstElement *element, GstBuffer *buffer, GstPad *pad, gpointer data);
49 static int __mc_output_buffer_finalize_cb(media_packet_h packet, int error_code, void *user_data);
50 static int _mc_gst_update_caps(mc_gst_core_t *core, GstMCBuffer *mcbuffer, bool codec_config);
51 static gboolean _mc_update_packet_info(mc_gst_core_t *core, media_format_h fmt);
52 static GstMCBuffer *_mc_gst_media_packet_to_gstbuffer(mc_gst_core_t *core, media_packet_h packet, bool codec_config);
53 static int _mc_gst_gstbuffer_to_appsrc(mc_gst_core_t *core, GstMCBuffer *mcbuffer);
54 static gchar *__mc_get_gst_input_format(media_packet_h packet, bool is_hw);
55 static media_packet_h __mc_gst_make_media_packet(mc_gst_core_t *core, unsigned char *data, int size);
56 static gboolean __mc_gst_bus_callback(GstBus *bus, GstMessage *msg, gpointer data);
57 static GstBusSyncReply __mc_gst_bus_sync_callback(GstBus *bus, GstMessage *msg, gpointer data);
58 static MMVideoBuffer *__mc_gst_make_tbm_buffer(mc_gst_core_t *core, media_packet_h packet);
59 static GstMCBuffer *gst_mediacodec_buffer_new(mc_gst_core_t *core, media_packet_h packet, uint64_t size);
60 static void gst_mediacodec_buffer_finalize(GstMCBuffer *buffer);
61 static int __mc_set_caps_streamheader(mc_gst_core_t *core, GstMCBuffer *mcbuffer, guint streamheader_size);
62 static int __mc_set_caps_codecdata(mc_gst_core_t *core, GstMCBuffer *mcbuffer, guint codecdata_size);
63 static void __mc_push_output_to_queue(mc_gst_core_t *core, media_packet_h out_packet);
64 static int __mc_gst_create_eos_packet(media_format_h fmt, media_packet_h *out_packet);
65 static void _mc_gst_handle_input_buffer_used(mc_gst_core_t *core, media_packet_h packet);
67 static gint __gst_handle_stream_error(mc_gst_core_t *core, GError *error, GstMessage *message);
68 static gint __gst_transform_gsterror(mc_gst_core_t *core, GstMessage *message, GError *error);
69 static gint __gst_handle_resource_error(mc_gst_core_t *core, int code);
70 static gint __gst_handle_library_error(mc_gst_core_t *core, int code);
71 static gint __gst_handle_core_error(mc_gst_core_t *core, int code);
72 static const gchar * _mc_error_to_string(mc_ret_e err);
74 static int _mc_link_vtable(mc_gst_core_t *core, mediacodec_codec_type_e id, gboolean is_encoder, gboolean is_hw);
75 #ifdef TIZEN_PROFILE_LITE
76 static int __tbm_get_physical_addr_bo(tbm_bo_handle tbm_bo_handle_fd_t, int *phy_addr, int *phy_size);
78 static int _mc_gst_flush_buffers(mc_gst_core_t *core);
79 static void _mc_gst_set_flush_input(mc_gst_core_t *core);
80 static void _mc_gst_set_flush_output(mc_gst_core_t *core);
82 #ifdef TIZEN_EXYNOS_SPECIFIC
83 static int __tile_4x2_read(int x_size, int y_size, int x_pos, int y_pos);
84 static void __csc_tiled_to_linear_crop(unsigned char *yuv420_dest,
85 unsigned char *nv12t_src, int yuv420_width, int yuv420_height,
86 int left, int top, int right, int buttom);
89 static void _mc_send_eos_signal(mc_gst_core_t *core);
90 static void _mc_wait_for_eos(mc_gst_core_t *core);
91 static int _mediacodec_get_mime(mc_gst_core_t *core);
94 int(*vdec_vtable[])() = {&__mc_fill_input_buffer_with_packet, &__mc_fill_packet_with_output_buffer};
95 int(*venc_vtable[])() = {&__mc_fill_input_buffer_with_packet, &__mc_fill_packet_with_output_buffer};
98 int(*vdec_h264_sw_vtable[])() = {&__mc_fill_input_buffer_with_packet, /* S/W H.264 Decoder Vtable */
99 &__mc_fill_vdec_packet_with_output_buffer};
101 int(*vdec_mpeg4_sw_vtable[])() = {&__mc_fill_input_buffer_with_packet, /* S/W MPEG4 Decoder Vtable */
102 &__mc_fill_vdec_packet_with_output_buffer};
104 int(*vdec_h263_sw_vtable[])() = {&__mc_fill_input_buffer_with_packet, /* S/W MPEG4 Decoder Vtable */
105 &__mc_fill_vdec_packet_with_output_buffer};
107 int(*vdec_h264_hw_vtable[])() = {&__mc_fill_input_buffer_with_packet, /* H/W H.264 Decoder Vtable */
108 &__mc_fill_vdec_packet_with_output_buffer};
110 int(*vdec_mpeg4_hw_vtable[])() = {&__mc_fill_input_buffer_with_packet, /* H/W MPEG4 Decoder Vtable */
111 &__mc_fill_vdec_packet_with_output_buffer};
113 int(*vdec_h263_hw_vtable[])() = {&__mc_fill_input_buffer_with_packet, /* H/W MPEG4 Decoder Vtable */
114 &__mc_fill_vdec_packet_with_output_buffer};
116 int(*venc_mpeg4_sw_vtable[])() = {&__mc_fill_input_buffer_with_venc_packet, /* S/W MPEG4 Encoder Vtable */
117 &__mc_fill_venc_packet_with_output_buffer};
119 int(*venc_h263_sw_vtable[])() = {&__mc_fill_input_buffer_with_venc_packet, /* S/W MPEG4 Encoder Vtable */
120 &__mc_fill_venc_packet_with_output_buffer};
122 int(*venc_h264_hw_vtable[])() = {&__mc_fill_input_buffer_with_venc_packet, /* H/W H.264 Encoder Vtable */
123 &__mc_fill_venc_packet_with_output_buffer};
125 int(*venc_mpeg4_hw_vtable[])() = {&__mc_fill_input_buffer_with_venc_packet, /* H/W MPEG4 Encoder Vtable */
126 &__mc_fill_venc_packet_with_output_buffer};
128 int(*venc_h263_hw_vtable[])() = {&__mc_fill_input_buffer_with_venc_packet, /* H/W MPEG4 Encoder Vtable */
129 &__mc_fill_venc_packet_with_output_buffer};
131 int(*aenc_vtable[])() = {&__mc_fill_input_buffer_with_packet, &__mc_fill_packet_with_output_buffer};
132 int(*adec_vtable[])() = {&__mc_fill_input_buffer_with_packet, &__mc_fill_packet_with_output_buffer};
134 int(*aenc_aac_vtable[])() = {&__mc_fill_input_buffer_with_packet, /* AAC LC Encoder vtable */
135 &__mc_fill_packet_with_output_buffer};
137 int(*adec_aac_vtable[])() = {&__mc_fill_input_buffer_with_packet, /* AAC LC Decoder Vtable */
138 &__mc_fill_packet_with_output_buffer};
140 int(*adec_aacv12_vtable[])() = {&__mc_fill_input_buffer_with_packet, /* AAC HE Decoder Vtable */
141 &__mc_fill_packet_with_output_buffer};
143 int(*adec_mp3_vtable[])() = {&__mc_fill_input_buffer_with_packet, /* MP3 Decoder Vtable */
144 &__mc_fill_packet_with_output_buffer};
146 int(*adec_amrnb_vtable[])() = {&__mc_fill_input_buffer_with_packet, /* AMR-NB Decoder Vtable */
147 &__mc_fill_packet_with_output_buffer};
149 int(*adec_amrwb_vtable[])() = {&__mc_fill_input_buffer_with_packet, /* AMR-WB Decoder Vtable */
150 &__mc_fill_packet_with_output_buffer};
152 int(*aenc_amrnb_vtable[])() = {&__mc_fill_input_buffer_with_packet, /* AMR-NB Encoder Vtable */
153 &__mc_fill_packet_with_output_buffer};
155 int(*adec_vorbis_vtable[])() = {&__mc_fill_input_buffer_with_packet, /* VORBIS Decoder Vtable */
156 &__mc_fill_packet_with_output_buffer};
158 int(*adec_flac_vtable[])() = {&__mc_fill_input_buffer_with_packet, /* FLAC Decoder Vtable */
159 &__mc_fill_packet_with_output_buffer};
161 int(*adec_wma_vtable[])() = {&__mc_fill_input_buffer_with_packet, /* WMA Decoder Vtable */
162 &__mc_fill_packet_with_output_buffer};
166 * fill_inbuf virtual functions
168 int __mc_fill_input_buffer(mc_gst_core_t *core, media_packet_h packet, GstMCBuffer *mcbuffer)
170 return core->vtable[fill_inbuf](core, packet, mcbuffer);
173 int __mc_fill_inbuf_with_mm_video_buffer(mc_gst_core_t *core, media_packet_h packet, GstMCBuffer *mcbuffer)
175 int ret = MC_ERROR_NONE;
177 MMVideoBuffer *mm_vbuffer = NULL;
178 void *buf_data = NULL;
179 uint64_t buf_size = 0;
181 ret = media_packet_get_buffer_size(packet, &buf_size);
182 if (ret != MEDIA_PACKET_ERROR_NONE) {
183 LOGW("buffer size get failed");
187 ret = media_packet_get_buffer_data_ptr(packet, &buf_data);
188 if (ret != MEDIA_PACKET_ERROR_NONE) {
189 LOGW("buffer data get failed");
193 mm_vbuffer = __mc_gst_make_tbm_buffer(core, mcbuffer->packet);
195 if (mm_vbuffer != NULL) {
196 gst_buffer_prepend_memory(mcbuffer->buffer,
197 gst_memory_new_wrapped(GST_MEMORY_FLAG_READONLY, mm_vbuffer, sizeof(*mm_vbuffer), 0,
198 sizeof(*mm_vbuffer), mm_vbuffer, free));
199 LOGD("mm_vbuffer appended, %d, %d", sizeof(*mm_vbuffer), gst_buffer_n_memory(mcbuffer->buffer));
202 if (buf_data != NULL) {
203 gst_buffer_prepend_memory(mcbuffer->buffer,
204 gst_memory_new_wrapped(GST_MEMORY_FLAG_READONLY, buf_data, buf_size, 0,
205 buf_size, mcbuffer, (GDestroyNotify)gst_mediacodec_buffer_finalize));
206 LOGD("packet data apended, %d, %d", buf_size, gst_buffer_n_memory(mcbuffer->buffer));
211 int __mc_fill_input_buffer_with_packet(mc_gst_core_t *core, media_packet_h packet, GstMCBuffer *mcbuffer)
213 int ret = MC_ERROR_NONE;
214 void *buf_data = NULL;
215 uint64_t buf_size = 0;
217 ret = media_packet_get_buffer_size(packet, &buf_size);
218 if (ret != MEDIA_PACKET_ERROR_NONE) {
219 LOGW("buffer size get failed");
223 ret = media_packet_get_buffer_data_ptr(packet, &buf_data);
224 if (ret != MEDIA_PACKET_ERROR_NONE) {
225 LOGW("buffer data get failed");
229 if (buf_data != NULL) {
230 gst_buffer_append_memory(mcbuffer->buffer,
231 gst_memory_new_wrapped(GST_MEMORY_FLAG_READONLY, buf_data, buf_size, 0,
232 buf_size, mcbuffer, (GDestroyNotify)gst_mediacodec_buffer_finalize));
233 LOGD("packet data apended");
239 int __mc_fill_input_buffer_with_venc_packet(mc_gst_core_t *core, media_packet_h packet, GstMCBuffer *mcbuffer)
241 int ret = MC_ERROR_NONE;
254 mc_encoder_info_t *enc_info = (mc_encoder_info_t *)core->codec_info;
256 width = enc_info->width;
257 height = enc_info->height;
259 ret = media_packet_get_number_of_video_planes(packet, &plane_num);
260 if (ret != MEDIA_PACKET_ERROR_NONE) {
261 LOGW("media_packet_get_number_of_video_planes failed");
265 ret = media_packet_get_video_plane_data_ptr(packet, 0, &y_ptr);
266 if (ret != MEDIA_PACKET_ERROR_NONE) {
267 LOGW("media_packet_get_video_plane_data_ptr failed");
271 ret = media_packet_get_video_stride_width(packet, 0, &stride_width);
272 if (ret != MEDIA_PACKET_ERROR_NONE) {
273 LOGW("media_packet_get_video_stride_width failed");
277 ret = media_packet_get_video_stride_height(packet, 0, &stride_height);
278 if (ret != MEDIA_PACKET_ERROR_NONE) {
279 LOGW("media_packet_get_video_stride_width failed");
284 if (width == stride_width) {
285 mcbuffer->buf_size += stride_width * stride_height;
287 for (i = 1; i < plane_num; i++) {
288 media_packet_get_video_plane_data_ptr(packet, i, &uv_ptr);
289 media_packet_get_video_stride_width(packet, i, &stride_width);
290 media_packet_get_video_stride_height(packet, i, &stride_height);
292 buf_size = stride_width * stride_height;
294 memcpy(y_ptr + mcbuffer->buf_size, uv_ptr, buf_size);
295 mcbuffer->buf_size += buf_size;
296 LOGD("plane : %d, buf_size : %d, total : %d", i, buf_size, mcbuffer->buf_size);
300 for (j = 0; j < height; j++) {
301 memcpy(y_ptr + mcbuffer->buf_size, y_ptr + stride, width);
302 mcbuffer->buf_size += width;
303 stride += stride_width;
308 for (i = 1; i < plane_num; i++) {
309 media_packet_get_video_plane_data_ptr(packet, i, &uv_ptr);
310 media_packet_get_video_stride_width(packet, i, &stride_width);
311 media_packet_get_video_stride_height(packet, i, &stride_height);
313 for (j = 0; j < height>>1; j++) {
314 memcpy(y_ptr + mcbuffer->buf_size, uv_ptr + stride, width>>1);
315 mcbuffer->buf_size += width>>1;
316 stride += stride_width;
319 memcpy(y_ptr + mcbuffer->buf_size, uv_ptr, buf_size);
320 LOGD("plane : %d, buf_size : %d, total : %d", i, buf_size, mcbuffer->buf_size);
321 mcbuffer->buf_size += buf_size;
325 MMVideoBuffer *mm_video_buffer = NULL;
327 mm_video_buffer = __mc_gst_make_tbm_buffer(core, packet);
329 gst_buffer_prepend_memory(mcbuffer->buffer,
330 gst_memory_new_wrapped(GST_MEMORY_FLAG_READONLY, mm_video_buffer, sizeof(MMVideoBuffer), 0,
331 sizeof(MMVideoBuffer), mm_video_buffer, free));
333 LOGD("mm_video_buffer appended, %d, %d", sizeof(MMVideoBuffer), gst_buffer_n_memory(mcbuffer->buffer));
336 gst_buffer_prepend_memory(mcbuffer->buffer,
337 gst_memory_new_wrapped(GST_MEMORY_FLAG_READONLY, y_ptr, mcbuffer->buf_size, 0,
338 mcbuffer->buf_size, mcbuffer, (GDestroyNotify)gst_mediacodec_buffer_finalize));
345 * fill_outbuf virtual functions
348 int __mc_fill_output_buffer(mc_gst_core_t *core, void *data, int size, media_packet_h *out_packet)
350 return core->vtable[fill_outbuf](core, data, size, out_packet);
353 int __mc_fill_vdec_packet_with_output_buffer(mc_gst_core_t *core, void *data, int size, media_packet_h *packet)
361 tbm_surface_h tsurf = NULL;
362 tbm_surface_info_s tsurf_info;
363 tbm_bo bo[MM_VIDEO_BUFFER_PLANE_MAX];
364 tbm_bo_handle thandle;
366 g_return_val_if_fail(core != NULL, MC_PARAM_ERROR);
368 mc_decoder_info_t *dec_info = (mc_decoder_info_t *)core->codec_info;
371 width = dec_info->width;
372 height = dec_info->height;
373 stride_width = GST_ROUND_UP_4(width);
374 stride_height = height;
375 buf_size = stride_width * stride_height * 3 / 2;
377 memset(&tsurf_info, 0x0, sizeof(tbm_surface_info_s));
379 bo[0] = tbm_bo_alloc(core->bufmgr, buf_size, TBM_BO_WC);
381 LOGE("bo allocation failed");
385 tsurf_info.width = dec_info->width;
386 tsurf_info.height = dec_info->height;
387 tsurf_info.format = TBM_FORMAT_YVU420;
388 tsurf_info.bpp = tbm_surface_internal_get_bpp(TBM_FORMAT_YVU420);
389 tsurf_info.num_planes = tbm_surface_internal_get_num_planes(TBM_FORMAT_YVU420);
392 for (i = 0; i < tsurf_info.num_planes; i++) {
394 tsurf_info.planes[i].stride = stride_width;
395 tsurf_info.planes[i].size = stride_width * stride_height;
396 tsurf_info.planes[i].offset = 0;
397 tsurf_info.size = tsurf_info.planes[i].size;
399 tsurf_info.planes[i].stride = GST_ROUND_UP_4(GST_ROUND_UP_2(width) / 2);
400 tsurf_info.planes[i].size = GST_ROUND_UP_4(GST_ROUND_UP_2(width) / 2) * (height / 2);
401 tsurf_info.planes[i].offset = (tsurf_info.planes[i-1].offset + tsurf_info.planes[i - 1].size);
402 tsurf_info.size += tsurf_info.planes[i].size;
406 thandle = tbm_bo_map(bo[0], TBM_DEVICE_CPU, TBM_OPTION_WRITE);
407 memcpy(thandle.ptr, data, tsurf_info.size);
410 tsurf = tbm_surface_internal_create_with_bos(&tsurf_info, bo, 1);
413 MMVideoBuffer *mm_vbuffer = NULL;
414 mm_vbuffer = (MMVideoBuffer *)data;
416 if (mm_vbuffer->type == MM_VIDEO_BUFFER_TYPE_TBM_BO) {
417 tbm_surface_info_s tsurf_info;
418 memset(&tsurf_info, 0x0, sizeof(tbm_surface_info_s));
420 /* create tbm surface */
421 for (i = 0; i < MM_VIDEO_BUFFER_PLANE_MAX; i++) {
422 if (mm_vbuffer->handle.bo[i]) {
424 tsurf_info.planes[i].stride = mm_vbuffer->stride_width[i];
429 tsurf_info.width = dec_info->width;
430 tsurf_info.height = dec_info->height;
431 tsurf_info.format = TBM_FORMAT_NV12; /* bo_format */
432 tsurf_info.bpp = tbm_surface_internal_get_bpp(TBM_FORMAT_NV12);
433 tsurf_info.num_planes = tbm_surface_internal_get_num_planes(TBM_FORMAT_NV12);
436 for (i = 0; i < tsurf_info.num_planes; i++) {
437 tsurf_info.planes[i].stride = mm_vbuffer->stride_width[i];
438 tsurf_info.planes[i].size = mm_vbuffer->stride_width[i] * mm_vbuffer->stride_height[i];
441 tsurf_info.planes[i].offset = 0;
443 tsurf_info.planes[i].offset = tsurf_info.planes[i-1].offset + tsurf_info.planes[i - 1].size;
445 tsurf_info.size += tsurf_info.planes[i].size;
446 LOGD("%d plane stride : %d, size : %d", i, tsurf_info.planes[i].stride, tsurf_info.planes[i].size);
448 tsurf = tbm_surface_internal_create_with_bos(&tsurf_info, (tbm_bo *)mm_vbuffer->handle.bo, bo_num);
454 media_packet_create_from_tbm_surface(core->output_fmt, tsurf,
455 (media_packet_finalize_cb)__mc_output_buffer_finalize_cb, core, packet);
458 return MC_ERROR_NONE;
461 int __mc_fill_packet_with_output_buffer(mc_gst_core_t *core, void *data, int size, media_packet_h *out_packet)
463 void *packet_data = NULL;
464 int ret = MC_ERROR_NONE;
465 g_return_val_if_fail(core != NULL, MC_PARAM_ERROR);
467 if (core->encoder && core->codec_id == MEDIACODEC_AAC)
468 media_format_set_audio_aac_type(core->output_fmt, FALSE);
470 ret = media_packet_create_alloc(core->output_fmt, __mc_output_buffer_finalize_cb, core, out_packet);
471 if (ret != MEDIA_PACKET_ERROR_NONE) {
472 LOGW("media_packet_create_alloc failed");
476 media_packet_set_buffer_size(*out_packet, size);
477 media_packet_get_buffer_data_ptr(*out_packet, &packet_data);
478 memcpy(packet_data, data, size);
480 return MC_ERROR_NONE;
483 int __mc_fill_venc_packet_with_output_buffer(mc_gst_core_t *core, void *data, int size, media_packet_h *out_packet)
485 void *packet_data = NULL;
486 bool codec_config = FALSE;
487 bool sync_flag = FALSE;
489 int ret = MC_ERROR_NONE;
491 g_return_val_if_fail(core != NULL, MC_PARAM_ERROR);
493 switch (core->out_mime) {
494 case MEDIA_FORMAT_H264_SP:
495 case MEDIA_FORMAT_H264_MP:
496 case MEDIA_FORMAT_H264_HP:
497 ret = _mc_check_h264_bytestream((unsigned char *)data, size, 1, &codec_config, &sync_flag, &slice);
499 case MEDIA_FORMAT_MPEG4_SP:
500 case MEDIA_FORMAT_MPEG4_ASP:
501 _mc_check_mpeg4_out_bytestream((unsigned char *)data, size, &codec_config, &sync_flag);
503 case MEDIA_FORMAT_H263:
504 case MEDIA_FORMAT_H263P:
505 if (!_mc_check_h263_out_bytestream((unsigned char *)data, size, &sync_flag))
506 return MC_INVALID_IN_BUF;
509 return MC_INVALID_IN_BUF;
511 LOGD("codec_config : %d, sync_flag : %d, slice : %d", codec_config, sync_flag, slice);
513 ret = media_packet_create_alloc(core->output_fmt, __mc_output_buffer_finalize_cb, core, out_packet);
514 if (ret != MEDIA_PACKET_ERROR_NONE) {
515 LOGW("media_packet_create_alloc failed");
519 media_packet_set_buffer_size(*out_packet, size);
520 media_packet_get_buffer_data_ptr(*out_packet, &packet_data);
521 memcpy(packet_data, data, size);
523 core->need_sync_flag = sync_flag ? 1 : 0;
524 core->need_codec_data = codec_config ? 1 : 0;
530 static GstCaps *__mc_gst_caps_set_buffer_array(GstCaps * caps, const gchar * name, GstBuffer * buf, ...)
532 GstStructure *struc = NULL;
534 GValue arr_val = { 0 };
535 GValue buf_val = { 0 };
537 g_return_val_if_fail(name != NULL, NULL);
538 g_return_val_if_fail(caps != NULL, NULL);
539 g_return_val_if_fail(gst_caps_is_fixed(caps), NULL);
540 caps = gst_caps_make_writable(caps);
542 struc = gst_caps_get_structure(caps, 0);
544 LOGW("cannot get structure from caps.\n");
546 g_value_init(&arr_val, GST_TYPE_ARRAY);
550 g_value_init(&buf_val, GST_TYPE_BUFFER);
551 gst_value_set_buffer(&buf_val, buf);
552 gst_value_array_append_value(&arr_val, &buf_val);
553 g_value_unset(&buf_val);
555 buf = va_arg(va, GstBuffer *);
559 gst_structure_take_value(struc, name, &arr_val);
564 int __mc_set_caps_streamheader(mc_gst_core_t *core, GstMCBuffer *mcbuffer, guint streamheader_size)
566 int ret = MEDIA_PACKET_ERROR_NONE;
567 void *buf_data = NULL;
568 uint64_t buf_size = 0;
569 GstBuffer *header1, *header2, *header3;
570 guint hsize1, hsize2, hsize3;
571 GstBuffer *tmp_header;
572 guint8 *tmp_buf = NULL;
575 ret = media_packet_get_buffer_size(mcbuffer->packet, &buf_size);
576 if (ret != MEDIA_PACKET_ERROR_NONE) {
577 LOGW("buffer size get failed");
581 ret = media_packet_get_buffer_data_ptr(mcbuffer->packet, &buf_data);
582 if (ret != MEDIA_PACKET_ERROR_NONE) {
583 LOGW("buffer data get failed");
587 LOGD("Set caps for streamheader in mime : %s and codec_id (0x%x)", core->mime, core->codec_id);
589 if (core->codec_id == MEDIACODEC_VORBIS) {
591 * hsize1 : Identification Header (packet type 1) - fixed 30 byte
592 * hsize2 : Comment Header (packet type 3) - variable byte (need calculate)
593 * hsize3 : Setup Header (packet type 5) - variable byte (Used remained bytes)
596 /* First of all, Need to fins and calculate size of hsize2 */
597 tmp_header = gst_buffer_new_and_alloc(streamheader_size);
598 gst_buffer_fill(tmp_header, 0, buf_data, streamheader_size);
599 gst_buffer_map(tmp_header, &map, GST_MAP_READ);
601 tmp_buf += (30 + 7); /* hsize1 + '0x03' + 'vorbis'*/
603 hsize2 += GST_READ_UINT32_LE(tmp_buf);
605 LOGD("Find streamheader hsize2(%d)", hsize2);
606 gst_buffer_unmap(tmp_header, &map);
607 gst_buffer_unref(tmp_header);
609 /* hsize1 : Identification Header (packet type 1) - fixed 30 byte */
611 header1 = gst_buffer_new_and_alloc(hsize1);
612 gst_buffer_fill(header1, 0, buf_data, hsize1);
613 gst_buffer_map(header1, &map, GST_MAP_READ);
615 /* '0x01' + 'vorbis' */
616 if (*tmp_buf != 0x01) {
617 LOGE("[ERROR] Invalid Caps of Stream header1");
618 gst_buffer_unmap(header1, &map);
619 return MEDIA_PACKET_ERROR_INVALID_PARAMETER;
621 gst_buffer_unmap(header1, &map);
623 /* hsize2 : Comment Header (packet type 3) - variable byte */
624 header2 = gst_buffer_new_and_alloc(hsize2);
625 gst_buffer_fill(header2, 0, (buf_data + (hsize1)), hsize2);
626 gst_buffer_map(header2, &map, GST_MAP_READ);
628 /* '0x03' + 'vorbis' */
629 if (*tmp_buf != 0x03) {
630 LOGE("[ERROR] Invalid Caps of Stream header2");
631 gst_buffer_unmap(header2, &map);
632 return MEDIA_PACKET_ERROR_INVALID_PARAMETER;
634 gst_buffer_unmap(header2, &map);
636 /* hsize3 : Setup Header (packet type 5) - variable byte */
637 hsize3 = streamheader_size - (hsize1 + hsize2);
638 header3 = gst_buffer_new_and_alloc(hsize3);
639 gst_buffer_fill(header3, 0, (buf_data + (hsize1 + hsize2)), hsize3);
640 gst_buffer_map(header3, &map, GST_MAP_READ);
642 /* '0x05' + 'vorbis' */
643 if (*tmp_buf != 0x05) {
644 LOGE("[ERROR] Invalid Caps of Stream header3");
645 gst_buffer_unmap(header3, &map);
646 return MEDIA_PACKET_ERROR_INVALID_PARAMETER;
648 gst_buffer_unmap(header3, &map);
650 LOGD("[vorbis] streamheader hsize1 (%d) + hsize2 (%d) + hsize3 (%d) = Total (%d)",
651 hsize1, hsize2, hsize3, (hsize1 + hsize2 + hsize3));
653 __mc_gst_caps_set_buffer_array(core->caps, "streamheader", header1, header2, header3, NULL);
655 gst_buffer_unref(header1);
656 gst_buffer_unref(header2);
657 gst_buffer_unref(header3);
658 } else if (core->codec_id == MEDIACODEC_FLAC) {
660 * hsize1 : Stream Info (type 0) - fixed 51 byte
661 * hsize2 : Stream Comment (type 4) - variable byte (need calculate)
664 /* First of all, Need to fins and calculate size of hsize2 */
665 tmp_header = gst_buffer_new_and_alloc(streamheader_size);
666 gst_buffer_fill(tmp_header, 0, buf_data, streamheader_size);
667 gst_buffer_map(tmp_header, &map, GST_MAP_READ);
669 hsize2 = 4 + ((tmp_buf[52] << 16) | (tmp_buf[53] << 8) | (tmp_buf[54]));
670 LOGD("Find streamheader hsize2(%d)", hsize2);
671 gst_buffer_unmap(tmp_header, &map);
672 gst_buffer_unref(tmp_header);
674 /* hsize1 : Stream Info (type 0) - fixed 51 byte */
676 header1 = gst_buffer_new_and_alloc(hsize1);
677 gst_buffer_fill(header1, 0, buf_data, hsize1);
678 gst_buffer_map(header1, &map, GST_MAP_READ);
680 /* '0x7f' + 'FLAC' */
681 if (*tmp_buf != 0x07f) {
682 LOGE("[ERROR] Invalid Caps of Stream header1 (Info)");
683 gst_buffer_unmap(header1, &map);
684 gst_buffer_unref(header1);
685 return MEDIA_PACKET_ERROR_INVALID_PARAMETER;
687 gst_buffer_unmap(header1, &map);
689 /* hsize2 : Stream Comment (type 4) - variable byte (need calculate) */
690 header2 = gst_buffer_new_and_alloc(hsize2);
691 gst_buffer_fill(header2, 0, (buf_data + (hsize1)), hsize2);
692 gst_buffer_map(header2, &map, GST_MAP_READ);
695 if (*tmp_buf != 0x84) {
696 LOGE("[ERROR] Invalid Caps of Stream header2 (Comment)");
697 gst_buffer_unmap(header2, &map);
698 gst_buffer_unref(header1);
699 gst_buffer_unref(header2);
700 return MEDIA_PACKET_ERROR_INVALID_PARAMETER;
702 gst_buffer_unmap(header2, &map);
704 LOGD("[flac] streamheader hsize1 (%d) + hsize2 (%d) = Total (%d)", hsize1, hsize2, (hsize1 + hsize2));
705 __mc_gst_caps_set_buffer_array(core->caps, "streamheader", header1, header2, NULL);
706 gst_buffer_unref(header1);
707 gst_buffer_unref(header2);
709 LOGE("Not support case of Stream header Caps");
712 /* Update gstbuffer's data ptr and size for using previous streamheader.*/
713 LOGD("BEFORE : mcbuffer->buffer of size %" G_GSIZE_FORMAT "", gst_buffer_get_size(mcbuffer->buffer));
714 gst_buffer_remove_memory_range(mcbuffer->buffer, streamheader_size, -1);
715 gst_buffer_set_size(mcbuffer->buffer, buf_size - streamheader_size);
716 LOGD("AFTER : mcbuffer->buffer of size %" G_GSIZE_FORMAT "", gst_buffer_get_size(mcbuffer->buffer));
721 int __mc_set_caps_codecdata(mc_gst_core_t *core, GstMCBuffer *mcbuffer, guint fixed_size)
723 int ret = MC_ERROR_NONE;
724 void *codec_data = NULL;
725 unsigned int codec_data_size = 0;
726 GstBuffer *codecdata_buffer;
730 media_packet_get_codec_data(mcbuffer->packet, &codec_data, &codec_data_size);
732 if (codec_data != NULL) { /* get the codec data from media_packet_get_codec_data() */
733 data = g_malloc0(codec_data_size);
734 memcpy(data, codec_data, codec_data_size);
735 codecdata_buffer = gst_buffer_new_wrapped(data, codec_data_size);
736 gst_caps_set_simple(core->caps, "codec_data", GST_TYPE_BUFFER, codecdata_buffer, NULL);
737 LOGD("set codec data : %" G_GSIZE_FORMAT "", gst_buffer_get_size(codecdata_buffer));
738 gst_buffer_unref(codecdata_buffer);
739 } else { /* get the codec data from media_packet_get_buffer_data_ptr() */
740 uint64_t buffer_size = 0;
741 void *buff_data = NULL;
743 media_packet_get_buffer_size(mcbuffer->packet, &buffer_size);
744 media_packet_get_buffer_data_ptr(mcbuffer->packet, &buff_data);
746 /* For audio, it's necessary to change the offset value of buffer after extracting csd */
747 /* it's not necessary to set csd to caps in case of annex-b */
748 if (buffer_size > fixed_size) {
749 data = g_malloc0(fixed_size);
750 gst_buffer_map(mcbuffer->buffer, &map, GST_MAP_READ);
751 memcpy(data, map.data, fixed_size);
752 codecdata_buffer = gst_buffer_new_wrapped(data, fixed_size);
753 gst_buffer_unmap(mcbuffer->buffer, &map);
754 gst_caps_set_simple(core->caps, "codec_data", GST_TYPE_BUFFER, codecdata_buffer, NULL);
755 gst_buffer_unref(codecdata_buffer);
757 gst_buffer_replace_memory(mcbuffer->buffer, 0,
758 gst_memory_new_wrapped(GST_MEMORY_FLAG_READONLY, buff_data + fixed_size , buffer_size - fixed_size, 0,
759 buffer_size - fixed_size, mcbuffer, NULL));
761 LOGD("set codec data from packet: %" G_GSIZE_FORMAT "", gst_buffer_get_size(mcbuffer->buffer));
767 int _mc_output_media_packet_new(mc_gst_core_t *core, bool video, bool encoder, media_format_mimetype_e out_mime)
769 if (media_format_create(&core->output_fmt) != MEDIA_FORMAT_ERROR_NONE) {
770 LOGE("media format create failed");
775 mc_encoder_info_t *info;
777 info = (mc_encoder_info_t *)core->codec_info;
780 media_format_set_video_mime(core->output_fmt, out_mime);
781 media_format_set_video_width(core->output_fmt, info->width);
782 media_format_set_video_height(core->output_fmt, info->height);
783 media_format_set_video_avg_bps(core->output_fmt, info->bitrate);
785 media_format_set_audio_mime(core->output_fmt, out_mime);
786 media_format_set_audio_channel(core->output_fmt, info->channel);
787 media_format_set_audio_samplerate(core->output_fmt, info->samplerate);
788 media_format_set_audio_bit(core->output_fmt, info->bit);
789 media_format_set_audio_avg_bps(core->output_fmt, info->bitrate);
792 mc_decoder_info_t *info;
794 info = (mc_decoder_info_t *)core->codec_info;
797 media_format_set_video_mime(core->output_fmt, out_mime);
798 media_format_set_video_width(core->output_fmt, info->width);
799 media_format_set_video_height(core->output_fmt, info->height);
801 media_format_set_audio_mime(core->output_fmt, out_mime);
802 media_format_set_audio_channel(core->output_fmt, info->channel);
803 media_format_set_audio_samplerate(core->output_fmt, info->samplerate);
804 media_format_set_audio_bit(core->output_fmt, info->bit);
807 return MC_ERROR_NONE;
811 * mc_gst_core functions
813 mc_gst_core_t *mc_gst_core_new()
819 core = g_new0(mc_gst_core_t, 1);
821 /* 0 : input, 1 : output */
822 core->ports[0] = NULL;
823 core->ports[1] = mc_gst_port_new(core);
824 core->ports[1]->index = 1;
826 core->available_queue = g_new0(mc_aqueue_t, 1);
827 core->available_queue->input = mc_async_queue_new();
829 g_mutex_init(&core->eos_mutex);
830 g_cond_init(&core->eos_cond);
831 g_cond_init(&core->buffer_cond);
832 g_cond_init(&core->out_buffer_cond);
833 g_mutex_init(&core->prepare_lock);
834 g_mutex_init(&core->drain_lock);
836 core->need_feed = false;
838 core->need_codec_data = false;
839 core->need_sync_flag = false;
840 core->unprepare_flag = false;
841 core->prepare_count = 0;
845 g_atomic_int_set(&core->available_queue->running, 1);
846 core->available_queue->thread = g_thread_new("feed thread", &feed_task, core);
850 LOGD("gst_core(%p) is created", core);
857 void mc_gst_core_free(mc_gst_core_t *core)
861 mc_aqueue_t *async_queue;
863 async_queue = core->available_queue;
865 mc_async_queue_disable(async_queue->input);
867 g_atomic_int_set(&async_queue->running, 0);
868 g_thread_join(async_queue->thread);
870 g_mutex_clear(&core->eos_mutex);
871 g_mutex_clear(&core->prepare_lock);
872 g_mutex_clear(&core->drain_lock);
873 g_cond_clear(&core->eos_cond);
874 g_cond_clear(&core->buffer_cond);
875 g_cond_clear(&core->out_buffer_cond);
877 mc_async_queue_free(async_queue->input);
880 if (core->ports[1] != NULL) {
881 mc_gst_port_free(core->ports[1]);
882 core->ports[1] = NULL;
885 LOGD("gst_core(%p) destroyed", core);
892 * mc_gst_port functions
894 mc_gst_port_t *mc_gst_port_new(mc_gst_core_t *core)
900 port = g_new0(mc_gst_port_t, 1);
902 port->num_buffers = -1;
903 port->buffer_size = 0;
904 port->is_allocated = 0;
905 port->buffers = NULL;
907 g_mutex_init(&port->mutex);
908 port->queue = g_queue_new();
910 LOGD("gst_port(%p) created", port);
916 void mc_gst_port_free(mc_gst_port_t *port)
920 g_mutex_clear(&port->mutex);
921 g_queue_free(port->queue);
923 LOGD("gst_port(%p) freed", port);
931 gboolean _mc_update_packet_info(mc_gst_core_t *core, media_format_h fmt)
933 gboolean is_updated = FALSE;
935 mc_decoder_info_t *codec_info = (mc_decoder_info_t *)core->codec_info;
942 media_format_get_video_info(fmt, NULL, &width, &height, &bitrate, NULL);
944 if ((codec_info->width != width) || (codec_info->height != height)) {
945 LOGD("Resolution changed : %dx%d -> %dx%d", codec_info->width, codec_info->height, width, height);
946 codec_info->width = width;
947 codec_info->height = height;
952 mc_encoder_info_t *enc_info = (mc_encoder_info_t *)core->codec_info;
954 if (bitrate != 0 && enc_info->bitrate != bitrate * 1000) {
955 LOGD("Bitrate changed : %d -> %d", enc_info->bitrate, bitrate);
956 enc_info->bitrate = bitrate * 1000;
966 media_format_get_audio_info(fmt, NULL, &channel, &samplerate, &bit, &bitrate);
968 if ((codec_info->channel != channel) || (codec_info->samplerate != samplerate) || (codec_info->bit != bit)) {
969 LOGD("Audio info. changed : %d -> %d, %d -> %d, %d -> %d", codec_info->channel, channel, codec_info->samplerate, samplerate, codec_info->bit, bit);
970 codec_info->channel = channel;
971 codec_info->samplerate = samplerate;
972 codec_info->bit = bit;
977 mc_encoder_info_t *enc_info = (mc_encoder_info_t *)core->codec_info;
979 if (bitrate != 0 && enc_info->bitrate != bitrate * 1000) {
980 LOGD("Bitrate changed : %d -> %d", enc_info->bitrate, bitrate);
981 enc_info->bitrate = bitrate * 1000;
990 static int _mc_gst_update_caps(mc_gst_core_t *core, GstMCBuffer *mcbuffer, bool codec_config)
994 g_return_val_if_fail(core != NULL, MC_PARAM_ERROR);
995 g_return_val_if_fail(mcbuffer != NULL, MC_PARAM_ERROR);
997 int ret = MC_ERROR_NONE;
1000 if (core->encoder) {
1001 mc_encoder_info_t *enc_info = (mc_encoder_info_t *)core->codec_info;
1003 core->format = __mc_get_gst_input_format(mcbuffer->packet, core->is_hw);
1004 gst_caps_set_simple(core->caps,
1005 "format", G_TYPE_STRING, core->format,
1006 "width", G_TYPE_INT, enc_info->width,
1007 "height", G_TYPE_INT, enc_info->height,
1008 "framerate", GST_TYPE_FRACTION, enc_info->fps, 1,
1011 if (g_object_class_find_property(G_OBJECT_GET_CLASS(core->codec), "bitrate"))
1012 g_object_set(GST_OBJECT(core->codec), "bitrate", enc_info->bitrate, NULL);
1014 g_object_set(GST_OBJECT(core->codec), "target-bitrate", enc_info->bitrate, NULL);
1015 LOGD("set bitrate = %d.", enc_info->bitrate);
1017 mc_decoder_info_t *dec_info = (mc_decoder_info_t *)core->codec_info;
1019 gst_caps_set_simple(core->caps,
1020 "width", G_TYPE_INT, dec_info->width,
1021 "height", G_TYPE_INT, dec_info->height,
1024 ret = __mc_set_caps_codecdata(core, mcbuffer, CODECDATA_NOT_USE);
1025 if (ret != MC_ERROR_NONE) {
1026 LOGW("__mc_set_caps_codecdata failed");
1030 if (core->encoder) {
1031 mc_encoder_info_t *enc_info = (mc_encoder_info_t *)core->codec_info;
1033 gst_caps_set_simple(core->caps,
1034 "rate", G_TYPE_INT, enc_info->samplerate,
1035 "channels", G_TYPE_INT, enc_info->channel,
1037 g_object_set(GST_OBJECT(core->codec), "bitrate", enc_info->bitrate, NULL);
1039 mc_decoder_info_t *dec_info = (mc_decoder_info_t *)core->codec_info;
1041 gst_caps_set_simple(core->caps,
1042 "rate", G_TYPE_INT, dec_info->samplerate,
1043 "channels", G_TYPE_INT, dec_info->channel,
1047 switch (core->codec_id) {
1048 case MEDIACODEC_AAC:
1049 case MEDIACODEC_AAC_HE:
1050 case MEDIACODEC_AAC_HE_PS:
1051 ret = __mc_set_caps_codecdata(core, mcbuffer, AAC_CODECDATA_SIZE);
1052 if (ret != MC_ERROR_NONE) {
1053 LOGW("__mc_set_caps_codecdata failed");
1056 case MEDIACODEC_WMAV1:
1057 case MEDIACODEC_WMAV2:
1058 case MEDIACODEC_WMAPRO:
1059 case MEDIACODEC_WMALSL:
1060 ret = __mc_set_caps_codecdata(core, mcbuffer, WMA_CODECDATA_SIZE);
1061 if (ret != MC_ERROR_NONE) {
1062 LOGW("__mc_set_caps_codecdata failed");
1065 case MEDIACODEC_VORBIS:
1066 case MEDIACODEC_FLAC:
1067 ret = __mc_set_caps_streamheader(core, mcbuffer, VORBIS_CODECDATA_SIZE);
1068 if (ret != MC_ERROR_NONE) {
1069 LOGW("__mc_set_caps_streamheader failed");
1073 LOGD("doesn't need to set codecdata");
1080 g_object_set(core->appsrc, "caps", core->caps, NULL);
1082 MEDIACODEC_FLEAVE();
1086 static gpointer feed_task(gpointer data)
1088 mc_gst_core_t *core = (mc_gst_core_t *)data;
1089 int ret = MC_ERROR_NONE;
1090 bool codec_config = FALSE;
1092 media_format_h fmt = NULL;
1093 media_packet_h in_buf = NULL;
1094 GstMCBuffer *mcbuffer = NULL;
1095 bool initiative = true;
1097 MEDIACODEC_FENTER();
1099 while (g_atomic_int_get(&core->available_queue->running)) {
1100 LOGD("waiting for next input....");
1101 in_buf = _mc_get_input_buffer(core);
1105 media_packet_get_format(in_buf, &fmt);
1107 if (_mc_update_packet_info(core, fmt))
1109 media_format_unref(fmt);
1112 if (media_packet_is_codec_config(in_buf, &codec_config) != MEDIA_PACKET_ERROR_NONE) {
1113 LOGE("media_packet_is_codec_config failed");
1117 if (media_packet_is_end_of_stream(in_buf, &eos) != MEDIA_PACKET_ERROR_NONE) {
1118 LOGE("media_packet_is_end_of_stream failed");
1122 * receive an eos packet. For decoder + encoder case, encoder always get tbm surface plane first from input data,
1123 * but eos packet only has flag not valid data, so we don't put it to downstream.
1126 LOGD("end of stream");
1127 gst_app_src_end_of_stream(GST_APP_SRC(core->appsrc));
1128 _mc_wait_for_eos(core);
1131 _mc_gst_handle_input_buffer_used(core, in_buf);
1135 mcbuffer = _mc_gst_media_packet_to_gstbuffer(core, in_buf, codec_config);
1137 LOGW("gstbuffer can't make");
1140 /* mcbuffer took the ownership of the in_buf, so set in_buf to NULL to aviod double unref */
1149 ret = _mc_gst_update_caps(core, mcbuffer, codec_config);
1150 if (ret != MC_ERROR_NONE) {
1151 LOGE("failed to update caps");
1152 /* unref mcbuff->buffer will invoke the finalize() of GstMcBuffer */
1153 gst_buffer_unref(mcbuffer->buffer);
1157 pad = gst_element_get_static_pad(core->appsrc, "src");
1158 gst_pad_push_event(pad, gst_event_new_stream_start("start"));
1159 gst_object_unref(pad);
1161 LOGD("caps updated");
1165 ret = _mc_gst_gstbuffer_to_appsrc(core, mcbuffer);
1166 if (ret != GST_FLOW_OK) {
1167 LOGE("Failed to push gst buffer");
1176 _mc_gst_handle_input_buffer_used(core, in_buf);
1178 _mc_gst_set_flush_input(core);
1180 if (core->user_cb[_MEDIACODEC_EVENT_TYPE_ERROR]) {
1181 ((mc_error_cb) core->user_cb[_MEDIACODEC_EVENT_TYPE_ERROR])
1182 (MC_INTERNAL_ERROR, core->user_data[_MEDIACODEC_EVENT_TYPE_ERROR]);
1187 /*LOGE("status : in_buf : %p, codec_config : %d, eos : %d, encoder : %d in feed_task", in_buf, codec_config, eos, core->encoder);*/
1192 LOGI("status : in_buf : %p, codec_config : %d, eos : %d, video : %d, encoder : %d in feed_task",
1193 in_buf, codec_config, eos, core->video, core->encoder);
1194 LOGD("feed task finished %p v(%d)e(%d)", core, core->video, core->encoder);
1196 MEDIACODEC_FLEAVE();
1201 static void __mc_gst_stop_feed(GstElement *pipeline, gpointer data)
1203 mc_gst_core_t *core = (mc_gst_core_t *)data;
1205 LOGI("stop_feed called");
1206 if (core->user_cb[_MEDIACODEC_EVENT_TYPE_BUFFER_STATUS]) {
1207 ((mc_buffer_status_cb) core->user_cb[_MEDIACODEC_EVENT_TYPE_BUFFER_STATUS])
1208 (MEDIACODEC_ENOUGH_DATA, core->user_data[_MEDIACODEC_EVENT_TYPE_BUFFER_STATUS]);
1212 static void __mc_gst_start_feed(GstElement *pipeline, guint size, gpointer data)
1214 mc_gst_core_t *core = (mc_gst_core_t *)data;
1216 LOGI("start_feed called");
1218 if (core->user_cb[_MEDIACODEC_EVENT_TYPE_BUFFER_STATUS]) {
1219 ((mc_buffer_status_cb) core->user_cb[_MEDIACODEC_EVENT_TYPE_BUFFER_STATUS])
1220 (MEDIACODEC_NEED_DATA, core->user_data[_MEDIACODEC_EVENT_TYPE_BUFFER_STATUS]);
1224 static int _mc_link_vtable(mc_gst_core_t *core, mediacodec_codec_type_e id, gboolean encoder, gboolean is_hw)
1226 MEDIACODEC_FENTER();
1228 g_return_val_if_fail(core != NULL, MC_PARAM_ERROR);
1231 case MEDIACODEC_AAC:
1232 /* if set to 'CODEC_CONFIG', then It is also available case of MEDIA_FORMAT_AAC_LC (RAW) */
1233 LOGD("aac lc (adts) vtable");
1235 core->vtable = aenc_aac_vtable;
1236 core->caps = gst_caps_new_simple(core->mime,
1237 "format", G_TYPE_STRING, "F32LE",
1238 "layout", G_TYPE_STRING, "interleaved", NULL);
1239 g_object_set(GST_OBJECT(core->codec), "compliance", -2, NULL);
1241 LOGD("CAPS for codec_id (MEDIACODEC_AAC_LC - normal ADTS)");
1243 core->vtable = adec_aac_vtable;
1244 core->caps = gst_caps_new_simple(core->mime,
1245 "framed", G_TYPE_BOOLEAN, TRUE,
1246 "mpegversion", G_TYPE_INT, 4,
1247 "stream-format", G_TYPE_STRING, "adts",
1251 case MEDIACODEC_AAC_HE:
1252 case MEDIACODEC_AAC_HE_PS:
1253 LOGD("aac he v12 vtable");
1255 LOGD("[MC_NOT_SUPPORTED] he-aac-v12 encoder is not supported yet!!!");
1256 return MC_NOT_SUPPORTED;
1258 LOGD("CAPS for codec_id (MEDIACODEC_AAC_HE and _PS - MP4/M4A case)");
1259 core->vtable = adec_aacv12_vtable;
1260 core->caps = gst_caps_new_simple(core->mime,
1261 "mpegversion", G_TYPE_INT, 4, /*TODO : need adding version /profile*/
1262 "framed", G_TYPE_BOOLEAN, TRUE,
1263 "stream-format", G_TYPE_STRING, "raw",
1267 case MEDIACODEC_MP3:
1268 LOGD("mp3 vtable - Only support decoder");
1270 LOGD("[MC_NOT_SUPPORTED] mp3 encoder is not supported yet!!!");
1271 return MC_NOT_SUPPORTED;
1273 core->vtable = adec_mp3_vtable;
1274 core->caps = gst_caps_new_simple(core->mime,
1275 "framed", G_TYPE_BOOLEAN, TRUE,
1276 "mpegversion", G_TYPE_INT, 1, /* To-Do : plz check */
1277 "mpegaudioversion", G_TYPE_INT, 1, /* To-Do : plz check */
1278 "layer", G_TYPE_INT, 3, /* To-Do : plz check */
1282 case MEDIACODEC_AMR_NB:
1283 LOGD("amrnb vtable");
1285 core->vtable = aenc_amrnb_vtable;
1286 core->caps = gst_caps_new_simple(core->mime,
1287 "format", G_TYPE_STRING, "S16LE",
1288 "layout", G_TYPE_STRING, "interleaved",
1292 core->vtable = adec_amrnb_vtable;
1293 core->caps = gst_caps_new_simple(core->mime,
1294 "rate", G_TYPE_INT, 8000,
1298 case MEDIACODEC_AMR_WB:
1299 LOGD("amrwb vtable - Only support decoder");
1301 LOGD("[MC_NOT_SUPPORTED] amr-wb encoder is not supported yet!!!");
1302 return MC_NOT_SUPPORTED;
1304 core->vtable = adec_amrwb_vtable;
1305 core->caps = gst_caps_new_simple(core->mime,
1306 "rate", G_TYPE_INT, 16000,
1310 case MEDIACODEC_VORBIS:
1311 LOGD("vorbis vtable");
1313 LOGD("[MC_NOT_SUPPORTED] vorbis encoder is not supported yet!!!");
1314 return MC_NOT_SUPPORTED;
1316 core->vtable = adec_vorbis_vtable;
1317 core->caps = gst_caps_new_empty_simple("audio/x-vorbis");
1320 case MEDIACODEC_FLAC:
1321 LOGD("flac vtable");
1323 LOGD("[MC_NOT_SUPPORTED] flac encoder is not supported yet!!!");
1324 return MC_NOT_SUPPORTED;
1326 core->vtable = adec_flac_vtable;
1327 core->caps = gst_caps_new_simple(core->mime,
1328 "framed", G_TYPE_BOOLEAN, TRUE,
1329 /* FIXME - Insert More Info */
1333 case MEDIACODEC_WMAV1:
1334 case MEDIACODEC_WMAV2:
1335 case MEDIACODEC_WMAPRO:
1336 case MEDIACODEC_WMALSL:
1337 LOGD("wma(V1 / V2 / LSL / PRO) vtable");
1339 LOGD("[MC_NOT_SUPPORTED] wma encoder is not supported yet!!!");
1340 return MC_NOT_SUPPORTED;
1342 mc_decoder_info_t *dec_info = (mc_decoder_info_t *)core->codec_info;
1344 core->vtable = adec_wma_vtable;
1347 * Need to extract from Stream Type Specific ... or
1348 * Need to get Demuxer's Caps from Stream Type Specific
1350 guint16 format_tag = 0;
1351 gint wma_version = 0;
1352 gint block_align = 1024; /*FIXME : Need checking */
1353 gint bitrate = 128000; /*FIXME : Need checking */
1355 LOGD(" ----- WMA Need Additional Caps -----------");
1356 if (core->codec_id == MEDIACODEC_WMAV1) {
1357 format_tag = 352; /* 0x160 */
1359 } else if (core->codec_id == MEDIACODEC_WMAV2) {
1360 format_tag = 353; /* 0x161 */
1362 } else if (core->codec_id == MEDIACODEC_WMAPRO) {
1363 format_tag = 354; /* 0x162 */
1365 } else if (core->codec_id == MEDIACODEC_WMALSL) {
1366 format_tag = 355; /* 0x163 */
1369 LOGE("Not support WMA format");
1372 core->caps = gst_caps_new_simple(core->mime,
1373 "bitrate", G_TYPE_INT, bitrate,
1374 "depth", G_TYPE_INT, dec_info->bit,
1375 /* FIXME - Need More Info */
1376 "wmaversion", G_TYPE_INT, wma_version,
1377 "format_tag", G_TYPE_INT, format_tag,
1378 "block_align", G_TYPE_INT, block_align,
1382 case MEDIACODEC_H263:
1383 LOGD("h263 vtable");
1385 core->vtable = is_hw ? venc_h263_hw_vtable : venc_h263_sw_vtable;
1386 core->caps = gst_caps_new_empty_simple("video/x-raw");
1389 core->vtable = vdec_h263_hw_vtable;
1390 core->caps = gst_caps_new_simple(core->mime,
1391 "mpegversion", G_TYPE_INT, 4,
1392 "framerate", GST_TYPE_FRACTION, 30, 1,
1395 core->vtable = vdec_h263_sw_vtable;
1396 core->caps = gst_caps_new_simple(core->mime,
1397 "variant", G_TYPE_STRING, "itu",
1398 "framerate", GST_TYPE_FRACTION, 30, 1,
1403 case MEDIACODEC_MPEG4:
1404 LOGD("mpeg4 vtable");
1406 core->vtable = is_hw ? venc_mpeg4_hw_vtable : venc_mpeg4_sw_vtable;
1407 core->caps = gst_caps_new_empty_simple("video/x-raw");
1409 core->vtable = is_hw ? vdec_mpeg4_hw_vtable : vdec_mpeg4_sw_vtable;
1410 core->caps = gst_caps_new_simple(core->mime,
1411 "mpegversion", G_TYPE_INT, 4,
1412 "systemstream", G_TYPE_BOOLEAN, false,
1413 "parsed", G_TYPE_BOOLEAN, TRUE,
1414 "framerate", GST_TYPE_FRACTION, 30, 1,
1418 case MEDIACODEC_H264:
1419 LOGD("h264 vtable");
1421 core->vtable = is_hw ? venc_h264_hw_vtable : venc_vtable;
1422 core->caps = gst_caps_new_empty_simple("video/x-raw");
1423 #ifdef TIZEN_PROFILE_LITE
1424 g_object_set(GST_OBJECT(core->codec), "byte-stream", TRUE, NULL);
1428 core->vtable = vdec_h264_hw_vtable;
1429 core->caps = gst_caps_new_simple(core->mime,
1430 "parsed", G_TYPE_BOOLEAN, TRUE,
1431 "alignment", G_TYPE_STRING, "au",
1432 "stream-format", G_TYPE_STRING, "byte-stream",
1433 "framerate", GST_TYPE_FRACTION, 30, 1,
1436 core->vtable = vdec_h264_sw_vtable;
1437 core->caps = gst_caps_new_simple(core->mime,
1438 "alignment", G_TYPE_STRING, "au",
1439 "stream-format", G_TYPE_STRING, "byte-stream",
1440 "framerate", GST_TYPE_FRACTION, 30, 1,
1449 g_object_set(core->appsrc, "caps", core->caps, NULL);
1451 return MC_ERROR_NONE;
1454 static int _mc_gst_gstbuffer_to_appsrc(mc_gst_core_t *core, GstMCBuffer *mcbuffer)
1456 MEDIACODEC_FENTER();
1458 int ret = MC_ERROR_NONE;
1460 LOGD("pushed buffer to appsrc : %p, buffer of size %" G_GSIZE_FORMAT "",
1461 mcbuffer->buffer, gst_buffer_get_size(mcbuffer->buffer));
1463 ret = gst_app_src_push_buffer(GST_APP_SRC(core->appsrc), mcbuffer->buffer);
1468 media_packet_h _mc_get_input_buffer(mc_gst_core_t *core)
1470 LOGD("waiting for input...");
1471 return mc_async_queue_pop(core->available_queue->input);
1474 mc_ret_e mc_gst_prepare(mc_handle_t *mc_handle)
1476 MEDIACODEC_FENTER();
1478 int ret = MC_ERROR_NONE;
1479 media_format_mimetype_e out_mime;
1480 int num_supported_codec = 0;
1484 return MC_PARAM_ERROR;
1486 mediacodec_codec_type_e id;
1490 gchar *factory_name = NULL;
1491 mc_codec_map_t *codec_map;
1493 id = mc_handle->codec_id;
1494 video = mc_handle->is_video;
1495 encoder = mc_handle->is_encoder;
1496 hardware = mc_handle->is_hw;
1497 codec_map = encoder ? mc_handle->encoder_map : mc_handle->decoder_map;
1498 num_supported_codec = encoder ? mc_handle->num_supported_encoder : mc_handle->num_supported_decoder;
1500 for (i = 0; i < num_supported_codec; i++) {
1501 if ((id == codec_map[i].id) && (hardware == codec_map[i].hardware))
1505 if (i == num_supported_codec)
1506 return MC_NOT_SUPPORTED;
1508 factory_name = codec_map[i].type.factory_name;
1509 out_mime = codec_map[i].type.out_format;
1511 /* gst_core create */
1512 mc_gst_core_t *new_core = mc_gst_core_new();
1514 new_core->mime = codec_map[i].type.mime;
1515 new_core->is_hw = hardware;
1516 new_core->eos = false;
1517 new_core->encoder = encoder;
1518 new_core->video = video;
1519 new_core->codec_info = encoder ? (void *)&mc_handle->info.encoder : (void *)&mc_handle->info.decoder;
1520 new_core->out_mime = codec_map[i].type.out_format;
1521 new_core->codec_id = id;
1523 new_core->bufmgr = tbm_bufmgr_init(new_core->drm_fd);
1524 if (new_core->bufmgr == NULL) {
1525 LOGE("TBM initialization failed");
1529 LOGD("@%p(%p) core is initializing...v(%d)e(%d)", mc_handle, new_core, new_core->video, new_core->encoder);
1530 LOGD("factory name : %s, output_fmt : %x", factory_name, out_mime);
1532 /* create media_packet for output fmt */
1533 if ((ret = _mc_output_media_packet_new(new_core, video, encoder, out_mime)) != MC_ERROR_NONE) {
1534 LOGE("Failed to create output pakcet");
1538 for (i = 0; i < _MEDIACODEC_EVENT_TYPE_INTERNAL_FILLBUFFER ; i++) {
1539 LOGD("copy cb function [%d]", i);
1540 if (mc_handle->user_cb[i]) {
1541 new_core->user_cb[i] = mc_handle->user_cb[i];
1542 new_core->user_data[i] = mc_handle->user_data[i];
1543 LOGD("user_cb[%d] %p, %p", i, new_core->user_cb[i], mc_handle->user_cb[i]);
1547 mc_handle->core = new_core;
1549 /* create basic core elements */
1550 if ((ret = _mc_gst_create_pipeline(mc_handle->core, factory_name) != MC_ERROR_NONE)) {
1551 LOGE("failed to create pipeline");
1556 if ((ret = _mc_link_vtable(mc_handle->core, id, encoder, hardware)) != MC_ERROR_NONE) {
1557 LOGE("vtable link failed");
1561 LOGD("initialized... %d", ret);
1565 mc_ret_e mc_gst_unprepare(mc_handle_t *mc_handle)
1567 MEDIACODEC_FENTER();
1570 int ret = MC_ERROR_NONE;
1571 mc_gst_core_t *core = NULL;
1574 return MC_PARAM_ERROR;
1576 core = (mc_gst_core_t *)mc_handle->core;
1579 LOGD("@%p(%p) core is uninitializing... v(%d)e(%d)", mc_handle, core, core->video, core->encoder);
1581 g_mutex_lock(&core->drain_lock);
1582 core->unprepare_flag = TRUE;
1583 g_mutex_unlock(&core->drain_lock);
1586 _mc_send_eos_signal(core);
1588 _mc_gst_set_flush_input(core);
1590 ret = _mc_gst_destroy_pipeline(core);
1592 /* unset callback */
1593 for (i = 0; i < _MEDIACODEC_EVENT_TYPE_INTERNAL_FILLBUFFER; i++) {
1594 LOGD("unset cb function [%d]", i);
1595 if (mc_handle->user_cb[i]) {
1596 core->user_cb[i] = NULL;
1597 core->user_data[i] = NULL;
1598 LOGD("user_cb[%d] %p, %p", i, core->user_cb[i], mc_handle->user_cb[i]);
1602 media_format_unref(core->output_fmt);
1604 if (core->bufmgr != NULL) {
1605 tbm_bufmgr_deinit(core->bufmgr);
1606 core->bufmgr = NULL;
1609 if (core->drm_fd != -1) {
1610 close(core->drm_fd);
1611 LOGD("close drm_fd");
1615 mc_gst_core_free(core);
1616 mc_handle->core = NULL;
1623 mc_ret_e mc_gst_process_input(mc_handle_t *mc_handle, media_packet_h inbuf, uint64_t timeOutUs)
1625 MEDIACODEC_FENTER();
1627 int ret = MC_ERROR_NONE;
1628 mc_gst_core_t *core = NULL;
1632 return MC_PARAM_ERROR;
1634 core = (mc_gst_core_t *)mc_handle->core;
1636 g_mutex_lock(&core->drain_lock);
1638 while (g_atomic_int_get(&core->etb_count) > MAXINUM_QNUM) {
1639 end_time = g_get_monotonic_time() + timeOutUs;
1640 LOGD("Waiting until packets are drained..");
1642 if (!g_cond_wait_until(&core->buffer_cond, &core->drain_lock, end_time)) {
1643 LOGD("time out...");
1647 g_mutex_unlock(&core->drain_lock);
1649 if (core->prepare_count == 0)
1650 return MC_INVALID_STATUS;
1652 g_mutex_lock(&core->drain_lock);
1654 if (!core->eos || !core->unprepare_flag) {
1655 mc_async_queue_push(core->available_queue->input, inbuf);
1658 ret = MC_INVALID_IN_BUF;
1659 g_mutex_unlock(&core->drain_lock);
1663 g_mutex_unlock(&core->drain_lock);
1664 g_atomic_int_inc(&core->etb_count);
1665 LOGI("@v(%d)e(%d)process_input(%d): %p", core->video, core->encoder, core->etb_count, inbuf);
1667 MEDIACODEC_FLEAVE();
1672 mc_ret_e mc_gst_get_output(mc_handle_t *mc_handle, media_packet_h *outbuf, uint64_t timeOutUs)
1674 MEDIACODEC_FENTER();
1676 int ret = MC_ERROR_NONE;
1677 mc_gst_core_t *core = NULL;
1678 media_packet_h out_packet = NULL;
1679 gint64 end_time = -1;
1682 end_time = g_get_monotonic_time() + timeOutUs;
1685 return MC_PARAM_ERROR;
1687 core = (mc_gst_core_t *)mc_handle->core;
1689 g_mutex_lock(&core->ports[1]->mutex);
1691 if (!g_queue_is_empty(core->ports[1]->queue))
1694 LOGD("no buffer, wait for signal");
1695 signalled = g_cond_wait_until(&core->out_buffer_cond, &core->ports[1]->mutex, end_time);
1699 out_packet = g_queue_pop_head(core->ports[1]->queue);
1701 LOGW("Failed to get buffer");
1702 ret = MC_OUTPUT_BUFFER_EMPTY;
1705 *outbuf = out_packet;
1706 LOGI("@%p v(%d)e(%d) got buffer :%p", core, core->video, core->encoder, out_packet);
1708 g_mutex_unlock(&core->ports[1]->mutex);
1710 MEDIACODEC_FLEAVE();
1715 mc_ret_e mc_gst_flush_buffers(mc_handle_t *mc_handle)
1717 MEDIACODEC_FENTER();
1719 int ret = MC_ERROR_NONE;
1720 mc_gst_core_t *core = NULL;
1723 return MC_PARAM_ERROR;
1725 core = (mc_gst_core_t *)mc_handle->core;
1726 LOGI("@%p v(%d)e(%d) flush_buffers", core, core->video, core->encoder);
1728 ret = _mc_gst_flush_buffers(core);
1730 MEDIACODEC_FLEAVE();
1735 static gboolean __mc_gst_init_gstreamer()
1737 MEDIACODEC_FENTER();
1739 static gboolean initialized = FALSE;
1740 static const int max_argc = 50;
1742 gchar **argv = NULL;
1743 gchar **argv2 = NULL;
1749 LOGD("gstreamer already initialized.\n");
1754 argc = malloc(sizeof(int));
1755 argv = malloc(sizeof(gchar *) *max_argc);
1756 argv2 = malloc(sizeof(gchar *) *max_argc);
1758 if (!argc || !argv || !argv2)
1761 memset(argv, 0, sizeof(gchar *) *max_argc);
1762 memset(argv2, 0, sizeof(gchar *) *max_argc);
1766 argv[0] = g_strdup("media codec");
1768 /* we would not do fork for scanning plugins */
1769 argv[*argc] = g_strdup("--gst-disable-registry-fork");
1772 /* check disable registry scan */
1773 argv[*argc] = g_strdup("--gst-disable-registry-update");
1776 /* check disable segtrap */
1777 argv[*argc] = g_strdup("--gst-disable-segtrap");
1780 LOGD("initializing gstreamer with following parameter\n");
1781 LOGD("argc : %d\n", *argc);
1784 for (i = 0; i < arg_count; i++) {
1786 LOGD("argv[%d] : %s\n", i, argv2[i]);
1789 /* initializing gstreamer */
1790 if (!gst_init_check(argc, &argv, &err)) {
1791 LOGE("Could not initialize GStreamer: %s\n", err ? err->message : "unknown error occurred");
1799 for (i = 0; i < arg_count; i++)
1800 MC_FREEIF(argv2[i]);
1809 MEDIACODEC_FLEAVE();
1815 for (i = 0; i < arg_count; i++) {
1816 LOGD("free[%d] : %s\n", i, argv2[i]);
1817 MC_FREEIF(argv2[i]);
1827 mc_ret_e _mc_gst_create_pipeline(mc_gst_core_t *core, gchar *factory_name)
1831 MEDIACODEC_FENTER();
1833 g_mutex_lock(&core->prepare_lock);
1834 if (core->prepare_count == 0) {
1836 if (!__mc_gst_init_gstreamer()) {
1837 LOGE("gstreamer initialize fail");
1838 g_mutex_unlock(&core->prepare_lock);
1839 return MC_NOT_INITIALIZED;
1841 core->codec = gst_element_factory_make(factory_name, NULL);
1844 LOGE("codec element create fail");
1848 LOGD("@%p v(%d)e(%d) create_pipeline", core, core->video, core->encoder);
1849 MEDIACODEC_ELEMENT_SET_STATE(core->codec, GST_STATE_READY);
1851 /* create common elements */
1852 core->pipeline = gst_pipeline_new(NULL);
1854 if (!core->pipeline) {
1855 LOGE("pipeline create fail");
1859 core->appsrc = gst_element_factory_make("appsrc", NULL);
1861 if (!core->appsrc) {
1862 LOGE("appsrc can't create");
1866 core->capsfilter = gst_element_factory_make("capsfilter", NULL);
1868 if (!core->capsfilter) {
1869 LOGE("capsfilter can't create");
1873 core->fakesink = gst_element_factory_make("fakesink", NULL);
1875 if (!core->fakesink) {
1876 LOGE("fakesink create fail");
1879 g_object_set(core->fakesink, "enable-last-sample", FALSE, NULL);
1881 /*__mc_link_elements(core);*/
1882 gst_bin_add_many(GST_BIN(core->pipeline), core->appsrc, core->capsfilter, core->codec, core->fakesink, NULL);
1885 if (!(gst_element_link_many(core->appsrc, core->capsfilter, core->codec, core->fakesink, NULL))) {
1886 LOGE("gst_element_link_many is failed");
1890 /* connect signals, bus watcher */
1891 bus = gst_pipeline_get_bus(GST_PIPELINE(core->pipeline));
1892 core->bus_whatch_id = gst_bus_add_watch(bus, __mc_gst_bus_callback, core);
1893 core->thread_default = g_main_context_get_thread_default();
1895 /* set sync handler to get tag synchronously */
1896 gst_bus_set_sync_handler(bus, __mc_gst_bus_sync_callback, core, NULL);
1897 gst_object_unref(GST_OBJECT(bus));
1900 g_signal_connect(core->appsrc, "need-data", G_CALLBACK(__mc_gst_start_feed), core);
1901 g_signal_connect(core->appsrc, "enough-data", G_CALLBACK(__mc_gst_stop_feed), core);
1903 /* connect handoff */
1904 g_object_set(GST_OBJECT(core->fakesink), "signal-handoffs", TRUE, NULL);
1905 core->signal_handoff = g_signal_connect(core->fakesink, "handoff", G_CALLBACK(__mc_gst_buffer_add), core);
1907 /* set state PLAYING */
1908 MEDIACODEC_ELEMENT_SET_STATE(GST_ELEMENT_CAST(core->pipeline), GST_STATE_PLAYING);
1912 core->prepare_count++;
1913 g_mutex_unlock(&core->prepare_lock);
1915 MEDIACODEC_FLEAVE();
1917 return MC_ERROR_NONE;
1919 STATE_CHANGE_FAILED:
1923 gst_object_unref(GST_OBJECT(core->codec));
1926 gst_object_unref(GST_OBJECT(core->pipeline));
1929 gst_object_unref(GST_OBJECT(core->appsrc));
1931 if (core->capsfilter)
1932 gst_object_unref(GST_OBJECT(core->capsfilter));
1935 gst_object_unref(GST_OBJECT(core->fakesink));
1937 g_mutex_unlock(&core->prepare_lock);
1942 mc_ret_e _mc_gst_destroy_pipeline(mc_gst_core_t *core)
1944 int ret = MC_ERROR_NONE;
1946 MEDIACODEC_FENTER();
1948 g_mutex_lock(&core->prepare_lock);
1949 core->prepare_count--;
1950 if (core->prepare_count == 0) {
1952 if (core->pipeline) {
1953 /* disconnect signal */
1954 if (core->fakesink && GST_IS_ELEMENT(core->fakesink)) {
1955 if (g_signal_handler_is_connected(core->fakesink, core->signal_handoff)) {
1956 g_signal_handler_disconnect(core->fakesink, core->signal_handoff);
1957 LOGD("handoff signal destroy");
1961 if (core->bus_whatch_id) {
1962 GSource *source = NULL;
1963 source = g_main_context_find_source_by_id(core->thread_default, core->bus_whatch_id);
1964 g_source_destroy(source);
1965 LOGD("bus_whatch_id destroy");
1968 MEDIACODEC_ELEMENT_SET_STATE(core->pipeline, GST_STATE_NULL);
1970 gst_object_unref(GST_OBJECT(core->pipeline));
1972 gst_caps_unref(core->caps);
1978 LOGD("@%p v(%d)e(%d) destroy_pipeline : %d ", core, core->video, core->encoder, core->prepare_count);
1979 g_mutex_unlock(&core->prepare_lock);
1981 MEDIACODEC_FLEAVE();
1985 STATE_CHANGE_FAILED:
1987 gst_object_unref(GST_OBJECT(core->pipeline));
1989 LOGD("@%p v(%d)e(%d) destroy_pipeline failed", core, core->video, core->encoder);
1990 g_mutex_unlock(&core->prepare_lock);
1995 void __mc_push_output_to_queue(mc_gst_core_t *core, media_packet_h out_packet)
1997 g_mutex_lock(&core->ports[1]->mutex);
1999 /* push it to output buffer queue */
2000 g_queue_push_tail(core->ports[1]->queue, out_packet);
2001 g_cond_broadcast(&core->out_buffer_cond);
2002 g_atomic_int_inc(&core->ftb_count);
2004 GstBuffer *buffer = NULL;
2005 media_packet_get_extra(out_packet, (void**)&buffer);
2007 LOGD("dq : v(%d)e(%d) %d TIMESTAMP = %"GST_TIME_FORMAT " DURATION = %"GST_TIME_FORMAT, core->video, core->encoder,
2008 core->ftb_count, GST_TIME_ARGS(GST_BUFFER_TIMESTAMP(buffer)), GST_TIME_ARGS(GST_BUFFER_DURATION(buffer)));
2010 LOGD("dq : v(%d)e(%d) %d ", core->video, core->encoder, core->ftb_count);
2013 g_mutex_unlock(&core->ports[1]->mutex);
2015 if (core->user_cb[_MEDIACODEC_EVENT_TYPE_FILLBUFFER]) {
2016 ((mc_fill_buffer_cb) core->user_cb[_MEDIACODEC_EVENT_TYPE_FILLBUFFER])
2017 (out_packet, core->user_data[_MEDIACODEC_EVENT_TYPE_FILLBUFFER]);
2021 void __mc_gst_buffer_add(GstElement *element, GstBuffer *buffer, GstPad *pad, gpointer data)
2025 GstMapInfo map = GST_MAP_INFO_INIT;
2026 media_packet_h out_packet = NULL;
2028 MEDIACODEC_FENTER();
2030 mc_gst_core_t *core = (mc_gst_core_t *)data;
2032 gst_buffer_ref(buffer);
2034 n = gst_buffer_n_memory(buffer);
2036 mem = gst_buffer_peek_memory(buffer, n-1);
2038 gst_memory_map(mem, &map, GST_MAP_READ);
2039 LOGD("n : %d, map.data : %p, map.size : %d", n, map.data, map.size);
2041 out_packet = __mc_gst_make_media_packet(core, map.data, map.size);
2043 LOGI("@%p(%d) out_packet : %p", core, core->encoder, out_packet);
2044 gst_memory_unmap(mem, &map);
2047 media_packet_set_extra(out_packet, buffer);
2048 media_packet_set_pts(out_packet, GST_BUFFER_TIMESTAMP(buffer));
2049 media_packet_set_duration(out_packet, GST_BUFFER_DURATION(buffer));
2051 if (core->need_codec_data) {
2052 media_packet_set_flags(out_packet, MEDIA_PACKET_CODEC_CONFIG);
2053 core->need_codec_data = false;
2056 if (core->need_sync_flag) {
2057 media_packet_set_flags(out_packet, MEDIA_PACKET_SYNC_FRAME);
2058 core->need_sync_flag = false;
2061 __mc_push_output_to_queue(core, out_packet);
2063 gst_buffer_unref(buffer);
2064 LOGE("Failed to dequeue output packet");
2067 MEDIACODEC_FLEAVE();
2072 int __mc_output_buffer_finalize_cb(media_packet_h packet, int error_code, void *user_data)
2074 void *buffer = NULL;
2078 GstMapInfo map = GST_MAP_INFO_INIT;
2079 MMVideoBuffer *mm_video_buf = NULL;
2082 MEDIACODEC_FENTER();
2084 mc_gst_core_t *core = (mc_gst_core_t *)user_data;
2086 g_mutex_lock(&core->ports[1]->mutex);
2088 if ((find = g_queue_find(core->ports[1]->queue, packet))) {
2089 LOGD("remove packet from queue");
2090 g_queue_remove(core->ports[1]->queue, packet);
2093 g_mutex_unlock(&core->ports[1]->mutex);
2095 g_atomic_int_dec_and_test(&core->ftb_count);
2096 media_packet_get_extra(packet, &buffer);
2098 n = gst_buffer_n_memory(buffer);
2101 mem = gst_buffer_peek_memory(buffer, n-1);
2102 gst_memory_map(mem, &map, GST_MAP_READ);
2103 mm_video_buf = (MMVideoBuffer *)map.data;
2105 if (!mm_video_buf) {
2106 LOGW("gstbuffer map.data is null");
2108 for (i = 0; i < MM_VIDEO_BUFFER_PLANE_MAX; i++) {
2109 if (mm_video_buf->handle.bo[i])
2110 tbm_bo_unref(mm_video_buf->handle.bo[i]);
2113 gst_memory_unmap(mem, &map);
2115 gst_buffer_unref((GstBuffer *)buffer);
2116 LOGD("@v(%d)e(%d)output port filled buffer(%d): %p", core->video, core->encoder, core->ftb_count, packet);
2118 MEDIACODEC_FLEAVE();
2120 return MEDIA_PACKET_FINALIZE;
2123 gchar *__mc_get_gst_input_format(media_packet_h packet, bool is_hw)
2125 gchar *format = NULL;
2126 media_format_h fmt = NULL;
2127 media_format_mimetype_e mimetype = 0;
2129 media_packet_get_format(packet, &fmt);
2130 media_format_get_video_info(fmt, &mimetype, NULL, NULL, NULL, NULL);
2131 media_format_unref(fmt);
2132 LOGD("input packet mimetype : %x", mimetype);
2135 case MEDIA_FORMAT_I420:
2138 case MEDIA_FORMAT_NV12:
2144 case MEDIA_FORMAT_ARGB:
2150 LOGD("input packet format : %s", format);
2154 GstMCBuffer *_mc_gst_media_packet_to_gstbuffer(mc_gst_core_t *core, media_packet_h packet, bool codec_config)
2156 int ret = MEDIA_PACKET_ERROR_NONE;
2157 GstMCBuffer *mcbuffer = NULL;
2158 void *buf_data = NULL;
2159 uint64_t buf_size = 0;
2163 ret = media_packet_get_buffer_size(packet, &buf_size);
2164 if (ret != MEDIA_PACKET_ERROR_NONE) {
2165 LOGW("buffer size get fail");
2169 ret = media_packet_get_buffer_data_ptr(packet, &buf_data);
2170 if (ret != MEDIA_PACKET_ERROR_NONE) {
2171 LOGW("buffer data get fail");
2175 mcbuffer = gst_mediacodec_buffer_new(core, packet, buf_size);
2176 if (mcbuffer == NULL) {
2177 LOGW("failed to create inbuf");
2181 LOGD("packet :v(%d)e(%d) %p, buf_size : %d", core->video, core->encoder, packet, (int)buf_size);
2183 ret = __mc_fill_input_buffer(core, packet, mcbuffer);
2184 if (ret != MC_ERROR_NONE) {
2185 LOGW("failed to fill inbuf: %s (ox%08x)", _mc_error_to_string(ret), ret);
2187 * if __mc_fill_input_buffer() failed, no memory appended to mcbuffer->buffer, so gst_mediacodec_buffer_finalize()
2188 * will not be invoked
2190 gst_buffer_unref(mcbuffer->buffer);
2196 media_packet_get_pts(packet, &pts);
2197 GST_BUFFER_PTS(mcbuffer->buffer) = pts;
2200 media_packet_get_duration(packet, &dur);
2201 GST_BUFFER_DURATION(mcbuffer->buffer) = dur;
2206 media_packet_h __mc_gst_make_media_packet(mc_gst_core_t *core, unsigned char *data, int size)
2208 int ret = MEDIA_PACKET_ERROR_NONE;
2209 media_packet_h packet = NULL;
2211 ret = __mc_fill_output_buffer(core, data, size, &packet);
2212 if (ret != MC_ERROR_NONE) {
2213 LOGW("failed to fill outbuf: %s (ox%08x)", _mc_error_to_string(ret), ret);
2221 int __mc_gst_create_eos_packet(media_format_h fmt, media_packet_h *out_packet)
2223 media_packet_h mediabuf = NULL;
2224 int ret = MC_ERROR_NONE;
2226 MEDIACODEC_FENTER();
2227 if (media_packet_create(fmt, NULL, NULL, &mediabuf)) {
2228 LOGE("media_packet_create_alloc failed\n");
2233 if (media_packet_set_flags(mediabuf, MEDIA_PACKET_END_OF_STREAM)) {
2234 LOGE("unable to set EOS flag\n");
2239 *out_packet = mediabuf;
2240 MEDIACODEC_FLEAVE();
2245 media_packet_destroy(mediabuf);
2247 MEDIACODEC_FLEAVE();
2251 void _mc_gst_handle_input_buffer_used(mc_gst_core_t *core, media_packet_h packet)
2253 g_atomic_int_dec_and_test(&core->etb_count);
2255 if (core && core->user_cb[_MEDIACODEC_EVENT_TYPE_EMPTYBUFFER]) {
2256 ((mc_empty_buffer_cb)core->user_cb[_MEDIACODEC_EVENT_TYPE_EMPTYBUFFER])
2257 (packet, core->user_data[_MEDIACODEC_EVENT_TYPE_EMPTYBUFFER]);
2261 LOGD("@v(%d)e(%d) input port emptied buffer(%d): %p", core->video, core->encoder, core->etb_count, packet);
2264 gboolean __mc_gst_bus_callback(GstBus *bus, GstMessage *msg, gpointer data)
2266 int ret = MC_ERROR_NONE;
2267 mc_gst_core_t *core = (mc_gst_core_t *)data;
2268 LOGD("@%p v(%d)e(%d)", core, core->video, core->encoder);
2270 switch (GST_MESSAGE_TYPE(msg)) {
2272 case GST_MESSAGE_EOS:
2273 _mc_send_eos_signal(core);
2275 media_packet_h out_packet = NULL;
2276 if (MC_ERROR_NONE == __mc_gst_create_eos_packet(core->output_fmt, &out_packet)) {
2277 __mc_push_output_to_queue(core, out_packet);
2278 LOGD("send eos packet.");
2280 LOGE("failed to create eos packet.");
2283 if (core->user_cb[_MEDIACODEC_EVENT_TYPE_EOS]) {
2284 LOGD("eos callback invoked");
2285 ((mc_eos_cb) core->user_cb[_MEDIACODEC_EVENT_TYPE_EOS])(core->user_data[_MEDIACODEC_EVENT_TYPE_EOS]);
2288 LOGD("End of stream\n");
2291 case GST_MESSAGE_ERROR:
2293 GError *error = NULL;
2295 gst_message_parse_error(msg, &error, NULL);
2298 LOGW("GST error message parsing failed");
2303 if (error->domain == GST_STREAM_ERROR)
2304 ret = __gst_handle_stream_error(core, error, msg);
2305 else if (error->domain == GST_RESOURCE_ERROR)
2306 ret = __gst_handle_resource_error(core, error->code);
2307 else if (error->domain == GST_LIBRARY_ERROR)
2308 ret = __gst_handle_library_error(core, error->code);
2309 else if (error->domain == GST_CORE_ERROR)
2310 ret = __gst_handle_core_error(core, error->code);
2312 LOGW("Unexpected error has occured");
2314 if (core->user_cb[_MEDIACODEC_EVENT_TYPE_ERROR]) {
2315 ((mc_error_cb) core->user_cb[_MEDIACODEC_EVENT_TYPE_ERROR])
2316 (ret, core->user_data[_MEDIACODEC_EVENT_TYPE_ERROR]);
2317 LOGW("Error : %s (ox%08x)", _mc_error_to_string(ret), ret);
2320 g_error_free(error);
2331 static gboolean __mc_gst_check_useful_message(mc_gst_core_t *core, GstMessage *msg)
2333 gboolean retval = false;
2335 if (!core->pipeline) {
2336 LOGE("mediacodec pipeline handle is null");
2340 switch (GST_MESSAGE_TYPE(msg)) {
2341 case GST_MESSAGE_TAG:
2342 case GST_MESSAGE_EOS:
2343 case GST_MESSAGE_ERROR:
2344 case GST_MESSAGE_WARNING:
2355 static GstBusSyncReply __mc_gst_bus_sync_callback(GstBus *bus, GstMessage *msg, gpointer data)
2357 mc_gst_core_t *core = (mc_gst_core_t *)data;
2358 GstBusSyncReply reply = GST_BUS_DROP;
2360 LOGD("__mc_gst_bus_sync_callback is called");
2362 if (!core->pipeline) {
2363 LOGE("mediacodec pipeline handle is null");
2364 return GST_BUS_PASS;
2367 if (!__mc_gst_check_useful_message(core, msg)) {
2368 gst_message_unref(msg);
2369 return GST_BUS_DROP;
2372 switch (GST_MESSAGE_TYPE(msg)) {
2373 case GST_MESSAGE_EOS:
2374 case GST_MESSAGE_ERROR:
2375 __mc_gst_bus_callback(NULL, msg, core);
2376 reply = GST_BUS_DROP;
2380 reply = GST_BUS_PASS;
2384 if (reply == GST_BUS_DROP)
2385 gst_message_unref(msg);
2390 static MMVideoBuffer *__mc_gst_make_tbm_buffer(mc_gst_core_t *core, media_packet_h packet)
2394 tbm_surface_h surface = NULL;
2395 tbm_surface_info_s surface_info;
2396 tbm_bo_handle handle_bo;
2399 LOGE("output is null");
2403 MMVideoBuffer *mm_vbuffer = NULL;
2404 mm_vbuffer = (MMVideoBuffer *)malloc(sizeof(MMVideoBuffer));
2406 LOGE("Failed to alloc MMVideoBuffer");
2409 memset(mm_vbuffer, 0x00, sizeof(MMVideoBuffer));
2411 media_packet_get_tbm_surface(packet, &surface);
2412 num_bos = tbm_surface_internal_get_num_bos(surface);
2413 int err = tbm_surface_get_info((tbm_surface_h)surface, &surface_info);
2414 if (err != TBM_SURFACE_ERROR_NONE) {
2415 LOGE("get tbm surface is failed");
2420 for (i = 0; i < num_bos; i++) {
2421 mm_vbuffer->handle.bo[i] = tbm_surface_internal_get_bo(surface, i);
2422 LOGD("mm_vbuffer->handle.bo[%d] : %p", i, mm_vbuffer->handle.bo[i]);
2425 #ifdef TIZEN_PROFILE_LITE
2428 tbm_bo_handle handle_fd = tbm_bo_get_handle(mm_vbuffer->handle.bo[0], TBM_DEVICE_MM);
2429 if (__tbm_get_physical_addr_bo(handle_fd, &phy_addr, &phy_size) == 0) {
2430 mm_vbuffer->handle.paddr[0] = (void *)phy_addr;
2431 LOGD("mm_vbuffer->paddr : %p", mm_vbuffer->handle.paddr[0]);
2435 mm_vbuffer->type = MM_VIDEO_BUFFER_TYPE_TBM_BO;
2436 mm_vbuffer->width[0] = surface_info.width;
2437 mm_vbuffer->height[0] = surface_info.height;
2438 mm_vbuffer->width[1] = surface_info.width;
2439 mm_vbuffer->height[1] = surface_info.height>>1;
2440 mm_vbuffer->size[0] = surface_info.planes[0].size;
2441 mm_vbuffer->size[1] = surface_info.planes[1].size;
2442 mm_vbuffer->stride_width[0] = surface_info.planes[0].stride;
2443 mm_vbuffer->stride_height[0] = surface_info.planes[0].size / surface_info.planes[0].stride;
2444 mm_vbuffer->stride_width[1] = surface_info.planes[1].stride;
2445 mm_vbuffer->stride_height[1] = surface_info.planes[1].size / surface_info.planes[1].stride;
2447 if (mm_vbuffer->handle.bo[0]) {
2448 handle_bo = tbm_bo_get_handle(mm_vbuffer->handle.bo[0], TBM_DEVICE_CPU);
2449 mm_vbuffer->data[0] = handle_bo.ptr;
2451 handle_bo = tbm_bo_get_handle(mm_vbuffer->handle.bo[0], TBM_DEVICE_MM);
2452 mm_vbuffer->handle.dmabuf_fd[0] = handle_bo.u32;
2455 if (mm_vbuffer->handle.bo[1]) {
2456 handle_bo = tbm_bo_get_handle(mm_vbuffer->handle.bo[1], TBM_DEVICE_CPU);
2457 mm_vbuffer->data[1] = handle_bo.ptr;
2459 handle_bo = tbm_bo_get_handle(mm_vbuffer->handle.bo[1], TBM_DEVICE_MM);
2460 mm_vbuffer->handle.dmabuf_fd[1] = handle_bo.u32;
2462 mm_vbuffer->data[1] = mm_vbuffer->data[0] + mm_vbuffer->stride_width[0] * mm_vbuffer->stride_height[0];
2464 mm_vbuffer->plane_num = 2;
2466 LOGD("size[0] : %d, size[1] : %d, bo[0] :%p, bo[1] :%p", mm_vbuffer->size[0], mm_vbuffer->size[1], mm_vbuffer->handle.bo[0], mm_vbuffer->handle.bo[1]);
2471 static void gst_mediacodec_buffer_finalize(GstMCBuffer *mcbuffer)
2473 MEDIACODEC_FENTER();
2478 mc_gst_core_t *core = (mc_gst_core_t *)mcbuffer->core;
2480 _mc_gst_handle_input_buffer_used(core, mcbuffer->packet);
2485 MEDIACODEC_FLEAVE();
2490 static GstMCBuffer *gst_mediacodec_buffer_new(mc_gst_core_t *core, media_packet_h packet, uint64_t size)
2492 GstMCBuffer *mcbuffer = NULL;
2494 mcbuffer = (GstMCBuffer *)malloc(sizeof(*mcbuffer));
2496 if (mcbuffer == NULL) {
2497 LOGE("malloc fail");
2501 mcbuffer->buffer = gst_buffer_new();
2502 mcbuffer->buf_size = 0;
2504 LOGD("creating buffer : %p, %p", mcbuffer, mcbuffer->buffer);
2505 mcbuffer->core = core;
2506 mcbuffer->packet = packet;
2511 static gint __gst_handle_core_error(mc_gst_core_t *core, int code)
2513 gint trans_err = MEDIACODEC_ERROR_NONE;
2515 g_return_val_if_fail(core, MC_PARAM_ERROR);
2518 case GST_CORE_ERROR_MISSING_PLUGIN:
2519 return MEDIACODEC_ERROR_NOT_SUPPORTED_FORMAT;
2520 case GST_CORE_ERROR_STATE_CHANGE:
2521 case GST_CORE_ERROR_SEEK:
2522 case GST_CORE_ERROR_NOT_IMPLEMENTED:
2523 case GST_CORE_ERROR_FAILED:
2524 case GST_CORE_ERROR_TOO_LAZY:
2525 case GST_CORE_ERROR_PAD:
2526 case GST_CORE_ERROR_THREAD:
2527 case GST_CORE_ERROR_NEGOTIATION:
2528 case GST_CORE_ERROR_EVENT:
2529 case GST_CORE_ERROR_CAPS:
2530 case GST_CORE_ERROR_TAG:
2531 case GST_CORE_ERROR_CLOCK:
2532 case GST_CORE_ERROR_DISABLED:
2534 trans_err = MEDIACODEC_ERROR_INVALID_STREAM;
2541 static gint __gst_handle_library_error(mc_gst_core_t *core, int code)
2543 gint trans_err = MEDIACODEC_ERROR_NONE;
2545 g_return_val_if_fail(core, MC_PARAM_ERROR);
2548 case GST_LIBRARY_ERROR_FAILED:
2549 case GST_LIBRARY_ERROR_TOO_LAZY:
2550 case GST_LIBRARY_ERROR_INIT:
2551 case GST_LIBRARY_ERROR_SHUTDOWN:
2552 case GST_LIBRARY_ERROR_SETTINGS:
2553 case GST_LIBRARY_ERROR_ENCODE:
2555 trans_err = MEDIACODEC_ERROR_INVALID_STREAM;
2563 static gint __gst_handle_resource_error(mc_gst_core_t *core, int code)
2565 gint trans_err = MEDIACODEC_ERROR_NONE;
2567 g_return_val_if_fail(core, MC_PARAM_ERROR);
2570 case GST_RESOURCE_ERROR_NO_SPACE_LEFT:
2571 trans_err = MEDIACODEC_ERROR_NO_FREE_SPACE;
2573 case GST_RESOURCE_ERROR_WRITE:
2574 case GST_RESOURCE_ERROR_FAILED:
2575 case GST_RESOURCE_ERROR_SEEK:
2576 case GST_RESOURCE_ERROR_TOO_LAZY:
2577 case GST_RESOURCE_ERROR_BUSY:
2578 case GST_RESOURCE_ERROR_OPEN_WRITE:
2579 case GST_RESOURCE_ERROR_OPEN_READ_WRITE:
2580 case GST_RESOURCE_ERROR_CLOSE:
2581 case GST_RESOURCE_ERROR_SYNC:
2582 case GST_RESOURCE_ERROR_SETTINGS:
2584 trans_err = MEDIACODEC_ERROR_INTERNAL;
2591 static gint __gst_handle_stream_error(mc_gst_core_t *core, GError *error, GstMessage *message)
2593 gint trans_err = MEDIACODEC_ERROR_NONE;
2595 g_return_val_if_fail(core, MC_PARAM_ERROR);
2596 g_return_val_if_fail(error, MC_PARAM_ERROR);
2597 g_return_val_if_fail(message, MC_PARAM_ERROR);
2599 switch (error->code) {
2600 case GST_STREAM_ERROR_FAILED:
2601 case GST_STREAM_ERROR_TYPE_NOT_FOUND:
2602 case GST_STREAM_ERROR_DECODE:
2603 case GST_STREAM_ERROR_WRONG_TYPE:
2604 case GST_STREAM_ERROR_DECRYPT:
2605 case GST_STREAM_ERROR_DECRYPT_NOKEY:
2606 case GST_STREAM_ERROR_CODEC_NOT_FOUND:
2607 trans_err = __gst_transform_gsterror(core, message, error);
2610 case GST_STREAM_ERROR_NOT_IMPLEMENTED:
2611 case GST_STREAM_ERROR_TOO_LAZY:
2612 case GST_STREAM_ERROR_ENCODE:
2613 case GST_STREAM_ERROR_DEMUX:
2614 case GST_STREAM_ERROR_MUX:
2615 case GST_STREAM_ERROR_FORMAT:
2617 trans_err = MEDIACODEC_ERROR_INVALID_STREAM;
2624 static gint __gst_transform_gsterror(mc_gst_core_t *core, GstMessage *message, GError *error)
2626 gchar *src_element_name = NULL;
2627 GstElement *src_element = NULL;
2628 GstElementFactory *factory = NULL;
2629 const gchar *klass = NULL;
2632 src_element = GST_ELEMENT_CAST(message->src);
2634 goto INTERNAL_ERROR;
2636 src_element_name = GST_ELEMENT_NAME(src_element);
2637 if (!src_element_name)
2638 goto INTERNAL_ERROR;
2640 factory = gst_element_get_factory(src_element);
2642 goto INTERNAL_ERROR;
2644 klass = gst_element_factory_get_klass(factory);
2646 goto INTERNAL_ERROR;
2648 LOGD("error code=%d, msg=%s, src element=%s, class=%s\n",
2649 error->code, error->message, src_element_name, klass);
2651 switch (error->code) {
2652 case GST_STREAM_ERROR_DECODE:
2653 case GST_STREAM_ERROR_FAILED:
2654 return MEDIACODEC_ERROR_INVALID_STREAM;
2657 case GST_STREAM_ERROR_CODEC_NOT_FOUND:
2658 case GST_STREAM_ERROR_TYPE_NOT_FOUND:
2659 case GST_STREAM_ERROR_WRONG_TYPE:
2660 return MEDIACODEC_ERROR_NOT_SUPPORTED_FORMAT;
2664 return MEDIACODEC_ERROR_INTERNAL;
2668 return MEDIACODEC_ERROR_INVALID_STREAM;
2671 return MEDIACODEC_ERROR_INTERNAL;
2674 static int _mc_gst_flush_buffers(mc_gst_core_t *core)
2676 gboolean ret = FALSE;
2677 GstEvent *event = NULL;
2679 MEDIACODEC_FENTER();
2681 _mc_gst_set_flush_input(core);
2683 event = gst_event_new_seek(1.0, GST_FORMAT_BYTES, GST_SEEK_FLAG_FLUSH,
2684 GST_SEEK_TYPE_SET, 0, GST_SEEK_TYPE_NONE, -1);
2686 ret = gst_element_send_event(core->appsrc, event);
2688 LOGE("failed to send seek event");
2692 _mc_gst_set_flush_output(core);
2694 MEDIACODEC_FLEAVE();
2696 return MC_ERROR_NONE;
2700 static void _mc_gst_set_flush_input(mc_gst_core_t *core)
2702 media_packet_h packet = NULL;
2704 MEDIACODEC_FENTER();
2705 LOGI("_mc_gst_set_flush_input called");
2707 while (!mc_async_queue_is_empty(core->available_queue->input)) {
2708 packet = mc_async_queue_pop_forced(core->available_queue->input);
2709 g_atomic_int_dec_and_test(&core->etb_count);
2710 LOGD("%p poped(%d)", packet, core->etb_count);
2712 _mc_gst_handle_input_buffer_used(core, packet);
2715 mc_async_queue_flush(core->available_queue->input);
2716 MEDIACODEC_FLEAVE();
2719 static void _mc_gst_set_flush_output(mc_gst_core_t *core)
2721 media_packet_h packet = NULL;
2723 MEDIACODEC_FENTER();
2724 g_mutex_lock(&core->ports[1]->mutex);
2726 while (!g_queue_is_empty(core->ports[1]->queue)) {
2727 packet = g_queue_pop_head(core->ports[1]->queue);
2728 g_atomic_int_dec_and_test(&core->ftb_count);
2729 LOGD("%p poped(%d)", packet, core->ftb_count);
2731 media_packet_destroy(packet);
2732 LOGD("outpacket destroyed");
2736 g_mutex_unlock(&core->ports[1]->mutex);
2737 MEDIACODEC_FLEAVE();
2740 #ifdef TIZEN_PROFILE_LITE
2741 int __tbm_get_physical_addr_bo(tbm_bo_handle tbm_bo_handle_fd_t, int *phy_addr, int *phy_size)
2743 int tbm_bo_handle_fd;
2747 tbm_bo_handle_fd = tbm_bo_handle_fd_t.u32;
2749 int open_flags = O_RDWR;
2752 struct ion_mmu_data mmu_data;
2753 struct ion_custom_data custom_data;
2755 mmu_data.fd_buffer = tbm_bo_handle_fd;
2756 custom_data.cmd = 4;
2757 custom_data.arg = (unsigned long)&mmu_data;
2759 ion_fd = open("/dev/ion", open_flags);
2761 LOGE("[tbm_get_physical_addr_bo] ion_fd open device failed");
2763 if (ioctl(ion_fd, ION_IOC_CUSTOM, &custom_data) < 0) {
2764 LOGE("[tbm_get_physical_addr_bo] ION_IOC_CUSTOM failed");
2769 *phy_addr = mmu_data.iova_addr;
2770 *phy_size = mmu_data.iova_size;
2774 LOGW("[tbm_get_physical_addr_bo] getting physical address is failed. phy_addr = 0");
2786 #if TIZEN_EXYNOS_SPECIFIC
2788 * Get tiled address of position(x,y)
2791 * width of tiled[in]
2794 * height of tiled[in]
2797 * x position of tield[in]
2800 * y position of tield[in]
2803 * address of tiled data
2805 static int __tile_4x2_read(int x_size, int y_size, int x_pos, int y_pos)
2807 int pixel_x_m1, pixel_y_m1;
2809 int linear_addr0, linear_addr1, bank_addr ;
2813 pixel_x_m1 = x_size - 1;
2814 pixel_y_m1 = y_size - 1;
2816 roundup_x = ((pixel_x_m1 >> 7) + 1);
2818 x_addr = x_pos >> 2;
2820 if ((y_size <= y_pos+32) && (y_pos < y_size) &&
2821 (((pixel_y_m1 >> 5) & 0x1) == 0) && (((y_pos >> 5) & 0x1) == 0)) {
2822 linear_addr0 = (((y_pos & 0x1f) << 4) | (x_addr & 0xf));
2823 linear_addr1 = (((y_pos >> 6) & 0xff) * roundup_x + ((x_addr >> 6) & 0x3f));
2825 if (((x_addr >> 5) & 0x1) == ((y_pos >> 5) & 0x1))
2826 bank_addr = ((x_addr >> 4) & 0x1);
2828 bank_addr = 0x2 | ((x_addr >> 4) & 0x1);
2830 linear_addr0 = (((y_pos & 0x1f) << 4) | (x_addr & 0xf));
2831 linear_addr1 = (((y_pos >> 6) & 0xff) * roundup_x + ((x_addr >> 5) & 0x7f));
2833 if (((x_addr >> 5) & 0x1) == ((y_pos >> 5) & 0x1))
2834 bank_addr = ((x_addr >> 4) & 0x1);
2836 bank_addr = 0x2 | ((x_addr >> 4) & 0x1);
2839 linear_addr0 = linear_addr0 << 2;
2840 trans_addr = (linear_addr1 << 13) | (bank_addr << 11) | linear_addr0;
2846 * Converts tiled data to linear
2847 * Crops left, top, right, buttom
2848 * 1. Y of NV12T to Y of YUV420P
2849 * 2. Y of NV12T to Y of YUV420S
2850 * 3. UV of NV12T to UV of YUV420S
2852 * @param yuv420_dest
2853 * Y or UV plane address of YUV420[out]
2856 * Y or UV plane address of NV12T[in]
2858 * @param yuv420_width
2859 * Width of YUV420[in]
2861 * @param yuv420_height
2862 * Y: Height of YUV420, UV: Height/2 of YUV420[in]
2871 * Crop size of right
2874 * Crop size of buttom
2876 static void __csc_tiled_to_linear_crop(unsigned char *yuv420_dest, unsigned char *nv12t_src,
2877 int yuv420_width, int yuv420_height,
2878 int left, int top, int right, int buttom)
2881 int tiled_offset = 0, tiled_offset1 = 0;
2882 int linear_offset = 0;
2883 int temp1 = 0, temp2 = 0, temp3 = 0, temp4 = 0;
2885 temp3 = yuv420_width-right;
2887 /* real width is greater than or equal 256 */
2889 for (i = top; i < yuv420_height-buttom; i += 1) {
2895 /* odd fomula: 2+x+(x>>2)<<2+x_block_num*(y-1) */
2896 tiled_offset = temp4-1;
2897 temp1 = ((yuv420_width+127)>>7)<<7;
2898 tiled_offset = tiled_offset*(temp1>>6);
2899 tiled_offset = tiled_offset+temp3;
2900 tiled_offset = tiled_offset+2;
2901 temp1 = (temp3>>2)<<2;
2902 tiled_offset = tiled_offset+temp1;
2903 tiled_offset = tiled_offset<<11;
2904 tiled_offset1 = tiled_offset+2048*2;
2907 temp2 = ((yuv420_height+31)>>5)<<5;
2908 if ((i + 32) < temp2) {
2909 /* even1 fomula: x+((x+2)>>2)<<2+x_block_num*y */
2911 temp1 = (temp1>>2)<<2;
2912 tiled_offset = temp3+temp1;
2913 temp1 = ((yuv420_width+127)>>7)<<7;
2914 tiled_offset = tiled_offset+temp4*(temp1>>6);
2915 tiled_offset = tiled_offset<<11;
2916 tiled_offset1 = tiled_offset + 2048 * 6;
2919 /* even2 fomula: x+x_block_num*y */
2920 temp1 = ((yuv420_width+127)>>7)<<7;
2921 tiled_offset = temp4*(temp1>>6);
2922 tiled_offset = tiled_offset+temp3;
2923 tiled_offset = tiled_offset<<11;
2924 tiled_offset1 = tiled_offset+2048*2;
2930 tiled_offset = tiled_offset+64*(temp1);
2931 tiled_offset1 = tiled_offset1+64*(temp1);
2932 temp2 = yuv420_width-left-right;
2933 linear_offset = temp2*(i-top);
2934 temp3 = ((j+256)>>8)<<8;
2939 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset+temp1, 64-temp1);
2940 temp2 = ((left+63)>>6)<<6;
2941 temp3 = ((yuv420_width-right)>>6)<<6;
2944 temp2 = yuv420_width-right-(64-temp1);
2946 memcpy(yuv420_dest+linear_offset+64-temp1, nv12t_src+tiled_offset+2048, 64);
2947 memcpy(yuv420_dest+linear_offset+128-temp1, nv12t_src+tiled_offset1, 64);
2948 memcpy(yuv420_dest+linear_offset+192-temp1, nv12t_src+tiled_offset1+2048, 64);
2949 linear_offset = linear_offset+256-temp1;
2950 } else if (temp3 > 128) {
2951 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset+2048+temp1, 64-temp1);
2952 memcpy(yuv420_dest+linear_offset+64-temp1, nv12t_src+tiled_offset1, 64);
2953 memcpy(yuv420_dest+linear_offset+128-temp1, nv12t_src+tiled_offset1+2048, 64);
2954 linear_offset = linear_offset+192-temp1;
2955 } else if (temp3 > 64) {
2956 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset1+temp1, 64-temp1);
2957 memcpy(yuv420_dest+linear_offset+64-temp1, nv12t_src+tiled_offset1+2048, 64);
2958 linear_offset = linear_offset+128-temp1;
2959 } else if (temp3 > 0) {
2960 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset1+2048+temp1, 64-temp1);
2961 linear_offset = linear_offset+64-temp1;
2964 tiled_offset = tiled_offset+temp4*2048;
2967 temp2 = yuv420_width-right-256;
2968 for (; j <= temp2; j += 256) {
2969 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
2970 tiled_offset1 = tiled_offset1+temp4*2048;
2971 memcpy(yuv420_dest+linear_offset+64, nv12t_src+tiled_offset+2048, 64);
2972 memcpy(yuv420_dest+linear_offset+128, nv12t_src+tiled_offset1, 64);
2973 tiled_offset = tiled_offset+temp4*2048;
2974 memcpy(yuv420_dest+linear_offset+192, nv12t_src+tiled_offset1+2048, 64);
2975 linear_offset = linear_offset+256;
2978 tiled_offset1 = tiled_offset1+temp4*2048;
2979 temp2 = yuv420_width-right-j;
2981 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
2982 memcpy(yuv420_dest+linear_offset+64, nv12t_src+tiled_offset+2048, 64);
2983 memcpy(yuv420_dest+linear_offset+128, nv12t_src+tiled_offset1, 64);
2984 memcpy(yuv420_dest+linear_offset+192, nv12t_src+tiled_offset1+2048, temp2-192);
2985 } else if (temp2 > 128) {
2986 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
2987 memcpy(yuv420_dest+linear_offset+64, nv12t_src+tiled_offset+2048, 64);
2988 memcpy(yuv420_dest+linear_offset+128, nv12t_src+tiled_offset1, temp2-128);
2989 } else if (temp2 > 64) {
2990 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
2991 memcpy(yuv420_dest+linear_offset+64, nv12t_src+tiled_offset+2048, temp2-64);
2993 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, temp2);
2996 } else if (temp1 >= 64) {
2997 for (i = top; i < (yuv420_height-buttom); i += 1) {
2999 tiled_offset = __tile_4x2_read(yuv420_width, yuv420_height, j, i);
3000 temp2 = ((j+64)>>6)<<6;
3002 linear_offset = temp1*(i-top);
3004 tiled_offset = tiled_offset+temp4;
3005 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, temp2);
3006 linear_offset = linear_offset+temp2;
3008 if ((j+64) <= temp3) {
3009 tiled_offset = __tile_4x2_read(yuv420_width, yuv420_height, j, i);
3010 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
3011 linear_offset = linear_offset+64;
3014 if ((j+64) <= temp3) {
3015 tiled_offset = __tile_4x2_read(yuv420_width, yuv420_height, j, i);
3016 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 64);
3017 linear_offset = linear_offset+64;
3021 tiled_offset = __tile_4x2_read(yuv420_width, yuv420_height, j, i);
3023 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, temp2);
3027 for (i = top; i < (yuv420_height-buttom); i += 1) {
3028 linear_offset = temp1*(i-top);
3029 for (j = left; j < (yuv420_width - right); j += 2) {
3030 tiled_offset = __tile_4x2_read(yuv420_width, yuv420_height, j, i);
3032 tiled_offset = tiled_offset+temp4;
3033 memcpy(yuv420_dest+linear_offset, nv12t_src+tiled_offset, 2);
3034 linear_offset = linear_offset+2;
3041 void _mc_send_eos_signal(mc_gst_core_t *core)
3043 g_mutex_lock(&core->eos_mutex);
3045 g_cond_broadcast(&core->eos_cond);
3046 g_mutex_unlock(&core->eos_mutex);
3047 LOGD("send EOS signal");
3050 void _mc_wait_for_eos(mc_gst_core_t *core)
3052 g_mutex_lock(&core->eos_mutex);
3054 LOGD("waiting for EOS");
3057 g_cond_wait(&core->eos_cond, &core->eos_mutex);
3059 LOGD("received EOS");
3060 g_mutex_unlock(&core->eos_mutex);
3063 const gchar * _mc_error_to_string(mc_ret_e err)
3065 guint err_u = (guint) err;
3070 case MC_PARAM_ERROR:
3071 return "Parameter error";
3072 case MC_INVALID_ARG:
3073 return "Invailid argument";
3074 case MC_PERMISSION_DENIED:
3075 return "Permission denied";
3076 case MC_INVALID_STATUS:
3077 return "Invalid status";
3078 case MC_NOT_SUPPORTED:
3079 return "Not supported";
3080 case MC_INVALID_IN_BUF:
3081 return "Invalid inputbuffer";
3082 case MC_INVALID_OUT_BUF:
3083 return "Invalid outputbuffer";
3084 case MC_INTERNAL_ERROR:
3085 return "Internal error";
3087 return "Hardware error";
3088 case MC_NOT_INITIALIZED:
3089 return "Not initialized";
3090 case MC_INVALID_STREAM:
3091 return "Invalid stream";
3092 case MC_CODEC_NOT_FOUND:
3093 return "Codec not found";
3094 case MC_ERROR_DECODE:
3095 return "Decode error";
3096 case MC_OUTPUT_BUFFER_EMPTY:
3097 return "Outputbuffer empty";
3098 case MC_OUTPUT_BUFFER_OVERFLOW:
3099 return "Outputbuffer overflow";
3100 case MC_MEMORY_ALLOCED:
3101 return "Memory allocated";
3102 case MC_COURRPTED_INI:
3103 return "Courrpted ini";
3105 return "Unknown error";
3110 int _mediacodec_get_mime(mc_gst_core_t *core)
3112 media_format_mimetype_e mime = MEDIA_FORMAT_MAX;
3114 switch (core->codec_id) {
3115 case MEDIACODEC_H264:
3117 mime = (core->is_hw) ? MEDIA_FORMAT_NV12 : MEDIA_FORMAT_I420;
3119 mime = MEDIA_FORMAT_H264_SP;
3121 case MEDIACODEC_MPEG4:
3123 mime = (core->is_hw) ? MEDIA_FORMAT_NV12 : MEDIA_FORMAT_I420;
3125 mime = MEDIA_FORMAT_MPEG4_SP;
3128 case MEDIACODEC_H263:
3130 mime = (core->is_hw) ? MEDIA_FORMAT_NV12 : MEDIA_FORMAT_I420;
3132 mime = MEDIA_FORMAT_H263P;
3135 case MEDIACODEC_AAC:
3137 mime = MEDIA_FORMAT_PCM;
3139 mime = MEDIA_FORMAT_AAC;
3142 case MEDIACODEC_AAC_HE:
3144 mime = MEDIA_FORMAT_PCM;
3146 mime = MEDIA_FORMAT_AAC_HE;
3149 case MEDIACODEC_AAC_HE_PS:
3151 case MEDIACODEC_MP3:
3152 mime = MEDIA_FORMAT_MP3;
3154 case MEDIACODEC_VORBIS:
3156 case MEDIACODEC_FLAC:
3158 case MEDIACODEC_WMAV1:
3160 case MEDIACODEC_WMAV2:
3162 case MEDIACODEC_WMAPRO:
3164 case MEDIACODEC_WMALSL:
3166 case MEDIACODEC_AMR_NB:
3167 mime = MEDIA_FORMAT_AMR_NB;
3169 case MEDIACODEC_AMR_WB:
3170 mime = MEDIA_FORMAT_AMR_WB;
3173 LOGE("NOT SUPPORTED!!!!");
3179 mc_ret_e mc_gst_get_packet_pool(mc_handle_t *mc_handle, media_packet_pool_h *packet_pool)
3182 int max_size, min_size;
3183 media_format_mimetype_e mime_format = MEDIA_FORMAT_MAX;
3184 media_format_h fmt_handle = NULL;
3185 media_packet_pool_h pool = NULL;
3186 mc_gst_core_t *core = NULL;
3189 return MC_PARAM_ERROR;
3191 core = (mc_gst_core_t *)mc_handle->core;
3193 int ret = media_packet_pool_create(&pool);
3195 if (ret != MEDIA_PACKET_ERROR_NONE) {
3196 LOGE("media_packet_pool_create failed");
3200 if (media_format_create(&fmt_handle) != MEDIA_FORMAT_ERROR_NONE) {
3201 LOGE("media format create failed");
3205 mime_format = _mediacodec_get_mime(core);
3208 if (core->encoder) {
3209 media_format_set_video_mime(fmt_handle, mime_format);
3210 media_format_set_video_width(fmt_handle, mc_handle->info.encoder.width);
3211 media_format_set_video_height(fmt_handle, mc_handle->info.encoder.height);
3212 media_format_set_video_avg_bps(fmt_handle, mc_handle->info.encoder.bitrate);
3214 media_format_set_video_mime(fmt_handle, mime_format);
3215 media_format_set_video_width(fmt_handle, mc_handle->info.decoder.width);
3216 media_format_set_video_height(fmt_handle, mc_handle->info.decoder.height);
3220 if (core->encoder) {
3221 media_format_set_audio_mime(fmt_handle, mime_format);
3222 media_format_set_audio_channel(fmt_handle, mc_handle->info.encoder.channel);
3223 media_format_set_audio_samplerate(fmt_handle, mc_handle->info.encoder.samplerate);
3224 media_format_set_audio_bit(fmt_handle, mc_handle->info.encoder.bit);
3226 media_format_set_audio_mime(fmt_handle, mime_format);
3227 media_format_set_audio_channel(fmt_handle, mc_handle->info.decoder.channel);
3228 media_format_set_audio_samplerate(fmt_handle, mc_handle->info.decoder.samplerate);
3229 media_format_set_audio_bit(fmt_handle, mc_handle->info.decoder.bit);
3233 ret = media_packet_pool_set_media_format(pool, fmt_handle);
3234 if (ret != MEDIA_PACKET_ERROR_NONE) {
3235 LOGE("media_packet_pool_set_media_format failed");
3239 /* will use default size temporarily */
3240 max_size = core->encoder ? DEFAULT_ENC_POOL_SIZE : DEFAULT_DEC_POOL_SIZE;
3242 min_size = max_size;
3243 ret = media_packet_pool_set_size(pool, min_size, max_size);
3244 if (ret != MEDIA_PACKET_ERROR_NONE) {
3245 LOGE("media_packet_pool_set_size failed");
3249 ret = media_packet_pool_allocate(pool);
3250 if (ret != MEDIA_PACKET_ERROR_NONE) {
3251 LOGE("media_packet_pool_allocate failed");
3252 return MC_OUT_OF_MEMORY;
3254 media_packet_pool_get_size(pool, &min_size, &max_size, &curr_size);
3255 LOGD("curr_size is %d min_size is %d and max_size is %d \n", curr_size, min_size, max_size);
3257 *packet_pool = pool;
3258 return MC_ERROR_NONE;