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.
23 #include <media_codec.h>
24 #include <media_codec_private.h>
25 #include <media_codec_port.h>
26 #include <media_codec_port_gst.h>
27 #include <media_codec_spec_emul.h>
28 #include <media_codec_ini.h>
30 static gboolean _check_support_audio_info(mediacodec_codec_type_e codec_id, int samplerate, int channel, int bit_depth);
31 static gboolean _check_support_video_info(mediacodec_codec_type_e codec_id, int width, int height);
33 int mc_create(MMHandleType *mediacodec)
35 mc_handle_t *new_mediacodec = NULL;
36 int ret = MC_ERROR_NONE;
38 /* alloc mediacodec structure */
39 new_mediacodec = (mc_handle_t *)g_malloc(sizeof(mc_handle_t));
41 if (!new_mediacodec) {
42 LOGE("Cannot allocate memory for mediacodec");
46 memset(new_mediacodec, 0, sizeof(mc_handle_t));
48 new_mediacodec->is_encoder = false;
49 new_mediacodec->is_video = false;
50 new_mediacodec->is_hw = true;
51 new_mediacodec->is_prepared = false;
52 new_mediacodec->codec_id = MEDIACODEC_NONE;
54 new_mediacodec->ports[0] = NULL;
55 new_mediacodec->ports[1] = NULL;
57 new_mediacodec->num_supported_decoder = 0;
58 new_mediacodec->num_supported_encoder = 0;
60 new_mediacodec->core = NULL;
61 new_mediacodec->ini = &mc_ini;
65 ret = mc_ini_load(&mc_ini);
66 if (ret != MC_ERROR_NONE)
72 _mc_create_codec_map_from_ini(new_mediacodec, spec_emul);
74 /* create decoder map from ini */
75 _mc_create_decoder_map_from_ini(new_mediacodec);
77 /* create encoder map from ini */
78 _mc_create_encoder_map_from_ini(new_mediacodec);
80 *mediacodec = (MMHandleType)new_mediacodec;
85 g_free(new_mediacodec);
86 return MC_INVALID_ARG;
89 int mc_destroy(MMHandleType mediacodec)
91 int ret = MC_ERROR_NONE;
92 mc_handle_t *mc_handle = (mc_handle_t *) mediacodec;
95 LOGE("fail invaild param\n");
96 return MC_INVALID_ARG;
99 LOGD("mediacodec : %p", mediacodec);
101 if (mc_handle->core != NULL) {
102 if (mc_gst_unprepare(mc_handle) != MC_ERROR_NONE) {
103 LOGE("mc_gst_unprepare() failed");
108 mc_handle->is_prepared = false;
110 /* free mediacodec structure */
112 g_free((void *)mc_handle);
119 int mc_set_codec(MMHandleType mediacodec, mediacodec_codec_type_e codec_id, int flags)
121 int ret = MC_ERROR_NONE;
122 mc_handle_t *mc_handle = (mc_handle_t *) mediacodec;
126 LOGE("fail invaild param");
127 return MC_INVALID_ARG;
130 /* Mandatory setting */
131 if (!GET_IS_ENCODER(flags) && !GET_IS_DECODER(flags)) {
132 LOGE("should be encoder or decoder");
133 return MC_PARAM_ERROR;
136 /* if user doesn't set codec-type, s/w codec would be set */
137 if (!GET_IS_HW(flags) && !GET_IS_SW(flags))
138 flags |= MEDIACODEC_SUPPORT_TYPE_SW;
140 for (i = 0; i < mc_handle->ini->num_supported_codecs; i++) {
141 if ((codec_id == spec_emul[i].codec_id) && (flags == spec_emul[i].codec_type))
144 LOGD("support_list : %d, i : %d", mc_handle->ini->num_supported_codecs, i);
146 if (i == mc_handle->ini->num_supported_codecs)
147 return MC_NOT_SUPPORTED;
149 mc_handle->port_type = spec_emul[i].port_type;
151 mc_handle->is_encoder = GET_IS_ENCODER(flags) ? 1 : 0;
152 mc_handle->is_hw = GET_IS_HW(flags) ? 1 : 0;
153 mc_handle->codec_id = codec_id;
154 mc_handle->is_video = CHECK_BIT(codec_id, 13);
156 mc_handle->is_prepared = false;
158 LOGD("encoder : %d, hardware : %d, codec_id : %x, video : %d",
159 mc_handle->is_encoder, mc_handle->is_hw, mc_handle->codec_id, mc_handle->is_video);
164 int mc_set_vdec_info(MMHandleType mediacodec, int width, int height)
166 int ret = MC_ERROR_NONE;
167 mc_handle_t* mc_handle = (mc_handle_t *)mediacodec;
170 LOGE("fail invaild param\n");
171 return MC_INVALID_ARG;
174 if (!_check_support_video_info(mc_handle->codec_id, width, height)) {
175 LOGE("invaild param[res %dx%d]", width, height);
176 return MC_PARAM_ERROR;
179 MEDIACODEC_CHECK_CONDITION(mc_handle->codec_id && mc_handle->is_video && !mc_handle->is_encoder,
180 MC_INVALID_ARG, "MEDIACODEC_ERROR_INVALID_PARAMETER");
182 mc_handle->info.video.width = width;
183 mc_handle->info.video.height = height;
185 mc_handle->is_prepared = true;
190 int mc_set_venc_info(MMHandleType mediacodec, int width, int height, int fps, int target_bits)
192 int ret = MC_ERROR_NONE;
193 mc_handle_t *mc_handle = (mc_handle_t *) mediacodec;
196 LOGE("fail invaild param\n");
197 return MC_INVALID_ARG;
200 if (!_check_support_video_info(mc_handle->codec_id, width, height) || fps < 0 || target_bits < 0) {
201 LOGE("invaild param[res %dx%d, fps %d, target_bits %d]", width, height, fps, target_bits);
202 return MC_PARAM_ERROR;
205 MEDIACODEC_CHECK_CONDITION(mc_handle->codec_id && mc_handle->is_video && mc_handle->is_encoder,
206 MC_INVALID_ARG, "MEDIACODEC_ERROR_INVALID_PARAMETER");
208 mc_handle->info.video.width = width;
209 mc_handle->info.video.height = height;
210 mc_handle->info.video.framerate = fps;
211 mc_handle->info.video.bitrate = target_bits * 1000;
212 mc_handle->is_prepared = true;
217 int mc_set_adec_info(MMHandleType mediacodec, int samplerate, int channel, int bit)
219 int ret = MC_ERROR_NONE;
220 mc_handle_t *mc_handle = (mc_handle_t *) mediacodec;
223 LOGE("fail invaild param\n");
224 return MC_INVALID_ARG;
227 if (!_check_support_audio_info(mc_handle->codec_id, samplerate, channel, bit)) {
228 LOGE("invaild param[samplerate %d, channel %d, bit %d]", samplerate, channel, bit);
229 return MC_PARAM_ERROR;
232 MEDIACODEC_CHECK_CONDITION(mc_handle->codec_id && !mc_handle->is_video && !mc_handle->is_encoder,
233 MC_INVALID_ARG, "MEDIACODEC_ERROR_INVALID_PARAMETER");
235 mc_handle->info.audio.samplerate = samplerate;
236 mc_handle->info.audio.channel = channel;
237 mc_handle->info.audio.bit_depth = bit;
238 mc_handle->is_prepared = true;
243 int mc_set_aenc_info(MMHandleType mediacodec, int samplerate, int channel, int bit, int bitrate)
245 int ret = MC_ERROR_NONE;
246 mc_handle_t * mc_handle = (mc_handle_t *) mediacodec;
249 LOGE("fail invaild param\n");
250 return MC_INVALID_ARG;
253 if (!_check_support_audio_info(mc_handle->codec_id, samplerate, channel, bit) || bitrate < 0) {
254 LOGE("invaild param[samplerate %d, channel %d, bit %d, bitrate %d]", samplerate, channel, bit, bitrate);
255 return MC_PARAM_ERROR;
258 MEDIACODEC_CHECK_CONDITION(mc_handle->codec_id && !mc_handle->is_video && mc_handle->is_encoder,
259 MC_INVALID_ARG, "MEDIACODEC_ERROR_INVALID_PARAMETER");
261 mc_handle->info.audio.samplerate = samplerate;
262 mc_handle->info.audio.channel = channel;
263 mc_handle->info.audio.bit_depth = bit;
264 mc_handle->info.audio.bitrate = bitrate * 1000;
266 mc_handle->is_prepared = true;
271 int mc_configure(MMHandleType mediacodec, media_format_h format, int flags)
273 int ret = MEDIA_FORMAT_ERROR_NONE;
274 mc_handle_t *mc_handle = (mc_handle_t *) mediacodec;
276 const int codec_mask = 0xFFF0;
285 media_format_type_e type;
286 media_format_mimetype_e mimetype;
289 LOGE("fail invaild param");
290 return MC_INVALID_ARG;
293 if (media_format_get_type(format, &type) != MEDIA_FORMAT_ERROR_NONE) {
294 LOGE("failed to retrieve type");
295 return MC_INVALID_ARG;
298 if (type == MEDIA_FORMAT_AUDIO) {
299 ret = media_format_get_audio_info(format, &mimetype, &channel, &samplerate, &bit, &bitrate);
300 if (ret != MEDIA_FORMAT_ERROR_NONE) {
301 LOGE("get audio info failed[0x%x]", ret);
302 return MC_INVALID_ARG;
305 codec_id = mimetype & codec_mask;
307 if (GET_IS_ENCODER(flags)) {
308 if (!_check_support_audio_info(codec_id, samplerate, channel, bit)) {
309 LOGE("invalid pram is set : samplerate : %d, channel : %d, bit : %d, bitrate : %d",
310 samplerate, channel, bit, bitrate);
311 return MC_PARAM_ERROR;
314 mc_handle->info.audio.samplerate = samplerate;
315 mc_handle->info.audio.channel = channel;
316 mc_handle->info.audio.bit_depth = bit;
317 mc_handle->info.audio.bitrate = bitrate * 1000;
318 } else if (GET_IS_DECODER(flags)) {
319 if (!_check_support_audio_info(codec_id, samplerate, channel, bit)) {
320 LOGE("invalid pram is set : samplerate : %d, channel : %d, bit : %d",
321 samplerate, channel, bit);
322 return MC_PARAM_ERROR;
324 mc_handle->info.audio.samplerate = samplerate;
325 mc_handle->info.audio.channel = channel;
326 mc_handle->info.audio.bit_depth = bit;
328 LOGE("either an encoder or a decoder must be set");
329 return MC_PARAM_ERROR;
331 } else if (type == MEDIA_FORMAT_VIDEO) {
332 ret = media_format_get_video_info(format, &mimetype, &width, &height, &bitrate, NULL);
333 ret |= media_format_get_video_frame_rate(format, &fps);
334 if (ret != MEDIA_FORMAT_ERROR_NONE) {
335 LOGE("get video info/frame_rate failed[0x%x]", ret);
336 return MC_INVALID_ARG;
339 codec_id = mimetype & codec_mask;
341 if (GET_IS_ENCODER(flags)) {
342 if (!_check_support_video_info(codec_id, width, height) || fps <= 0 || bitrate <= 0) {
343 LOGE("invalid pram is set : width : %d, height : %d, bitrate : %d, fps : %d",
344 width, height, bitrate, fps);
345 return MC_PARAM_ERROR;
348 mc_handle->info.video.width = width;
349 mc_handle->info.video.height = height;
350 mc_handle->info.video.framerate = fps;
351 mc_handle->info.video.bitrate = bitrate * 1000;
352 } else if (GET_IS_DECODER(flags)) {
353 if (!_check_support_video_info(codec_id, width, height)) {
354 LOGE("invalid pram is set : width : %d, height : %d",
356 return MC_PARAM_ERROR;
359 mc_handle->info.video.width = width;
360 mc_handle->info.video.height = height;
362 LOGE("either an encoder or a decoder must be set");
363 return MC_PARAM_ERROR;
366 LOGE("invalid format type is set");
367 return MC_PARAM_ERROR;
370 if (!GET_IS_HW(flags) && !GET_IS_SW(flags))
371 flags |= MEDIACODEC_SUPPORT_TYPE_SW;
373 for (i = 0; i < mc_handle->ini->num_supported_codecs; i++) {
374 if ((codec_id == spec_emul[i].codec_id) && (flags == spec_emul[i].codec_type))
377 LOGD("support_list : %d, i : %d", mc_handle->ini->num_supported_codecs, i);
379 if (i == mc_handle->ini->num_supported_codecs)
380 return MC_CODEC_NOT_FOUND;
382 mc_handle->port_type = spec_emul[i].port_type;
384 mc_handle->is_encoder = GET_IS_ENCODER(flags) ? 1 : 0;
385 mc_handle->is_hw = GET_IS_HW(flags) ? 1 : 0;
386 mc_handle->codec_id = codec_id;
387 mc_handle->is_video = CHECK_BIT(codec_id, 13);
389 mc_handle->is_prepared = true;
391 LOGD("encoder : %d, hardware : %d, codec_id : %x, video : %d",
392 mc_handle->is_encoder, mc_handle->is_hw, mc_handle->codec_id, mc_handle->is_video);
394 return MC_ERROR_NONE;
397 int mc_prepare(MMHandleType mediacodec)
399 int ret = MC_ERROR_NONE;
400 mc_handle_t *mc_handle = (mc_handle_t *) mediacodec;
403 LOGE("fail invaild param\n");
404 return MC_INVALID_ARG;
407 if (!mc_handle->is_prepared)
408 return MC_NOT_INITIALIZED;
410 /* setting core details */
411 switch (mc_handle->port_type) {
412 case MEDIACODEC_PORT_TYPE_GENERAL:
415 case MEDIACODEC_PORT_TYPE_OMX:
418 case MEDIACODEC_PORT_TYPE_GST:
419 ret = mc_gst_prepare(mc_handle);
429 int mc_unprepare(MMHandleType mediacodec)
431 int ret = MC_ERROR_NONE;
432 mc_handle_t *mc_handle = (mc_handle_t *)mediacodec;
435 LOGE("fail invaild param\n");
436 return MC_INVALID_ARG;
439 /* deinit core details */
440 switch (mc_handle->port_type) {
441 case MEDIACODEC_PORT_TYPE_GENERAL:
444 case MEDIACODEC_PORT_TYPE_OMX:
447 case MEDIACODEC_PORT_TYPE_GST:
448 ret = mc_gst_unprepare(mc_handle);
458 int mc_process_input(MMHandleType mediacodec, media_packet_h inbuf, uint64_t timeOutUs)
460 int ret = MC_ERROR_NONE;
462 mc_handle_t *mc_handle = (mc_handle_t *)mediacodec;
465 LOGE("fail invaild param");
466 return MC_INVALID_ARG;
470 LOGE("invaild input buffer");
471 return MC_INVALID_IN_BUF;
474 switch (mc_handle->port_type) {
475 case MEDIACODEC_PORT_TYPE_GENERAL:
478 case MEDIACODEC_PORT_TYPE_OMX:
481 case MEDIACODEC_PORT_TYPE_GST:
482 ret = mc_gst_process_input(mc_handle, inbuf, timeOutUs);
492 int mc_get_output(MMHandleType mediacodec, media_packet_h *outbuf, uint64_t timeOutUs)
494 int ret = MC_ERROR_NONE;
495 mc_handle_t *mc_handle = (mc_handle_t *) mediacodec;
498 LOGE("fail invaild param\n");
499 return MC_INVALID_ARG;
503 LOGE("invaild outbuf buffer");
504 return MC_INVALID_OUT_BUF;
507 /* setting core details */
508 switch (mc_handle->port_type) {
509 case MEDIACODEC_PORT_TYPE_GENERAL:
512 case MEDIACODEC_PORT_TYPE_OMX:
515 case MEDIACODEC_PORT_TYPE_GST:
516 ret = mc_gst_get_output(mc_handle, outbuf, timeOutUs);
526 int mc_flush_buffers(MMHandleType mediacodec)
528 int ret = MC_ERROR_NONE;
529 mc_handle_t *mc_handle = (mc_handle_t *)mediacodec;
532 LOGE("fail invaild param\n");
533 return MC_INVALID_ARG;
536 /* setting core details */
537 switch (mc_handle->port_type) {
538 case MEDIACODEC_PORT_TYPE_GENERAL:
541 case MEDIACODEC_PORT_TYPE_OMX:
544 case MEDIACODEC_PORT_TYPE_GST:
545 ret = mc_gst_flush_buffers(mc_handle);
555 int mc_get_supported_type(MMHandleType mediacodec, mediacodec_codec_type_e codec_type, bool encoder, int *support_type)
557 int ret = MC_ERROR_NONE;
558 mc_handle_t *mc_handle = (mc_handle_t *)mediacodec;
559 mc_codec_map_t *codec_map;
560 int num_supported_codec = 0;
566 LOGE("fail invaild param\n");
567 return MC_INVALID_ARG;
570 codec_map = encoder ? mc_handle->encoder_map : mc_handle->decoder_map;
571 num_supported_codec = encoder ? mc_handle->num_supported_encoder : mc_handle->num_supported_decoder;
573 for (i = 0; i < num_supported_codec; i++) {
574 if (codec_map[i].id == codec_type) {
575 if (codec_map[i].hardware)
576 *support_type |= MEDIACODEC_SUPPORT_TYPE_HW;
578 *support_type |= MEDIACODEC_SUPPORT_TYPE_SW;
585 int mc_set_empty_buffer_cb(MMHandleType mediacodec, mediacodec_input_buffer_used_cb callback, void *user_data)
587 int ret = MC_ERROR_NONE;
588 mc_handle_t *mc_handle = (mc_handle_t *)mediacodec;
591 LOGE("fail invaild param\n");
592 return MC_INVALID_ARG;
595 if (mc_handle->user_cb[_MEDIACODEC_EVENT_TYPE_EMPTYBUFFER]) {
596 LOGE("Already set mediacodec_empty_buffer_cb");
597 return MC_PARAM_ERROR;
600 return MC_INVALID_ARG;
602 LOGD("Set empty buffer callback(cb = %p, data = %p)", callback, user_data);
604 mc_handle->user_cb[_MEDIACODEC_EVENT_TYPE_EMPTYBUFFER] = (mc_empty_buffer_cb) callback;
605 mc_handle->user_data[_MEDIACODEC_EVENT_TYPE_EMPTYBUFFER] = user_data;
607 return MC_ERROR_NONE;
613 int mc_unset_empty_buffer_cb(MMHandleType mediacodec)
615 mc_handle_t *mc_handle = (mc_handle_t *)mediacodec;
618 LOGE("fail invaild param\n");
619 return MC_INVALID_ARG;
622 mc_handle->user_cb[_MEDIACODEC_EVENT_TYPE_EMPTYBUFFER] = NULL;
623 mc_handle->user_data[_MEDIACODEC_EVENT_TYPE_EMPTYBUFFER] = NULL;
625 return MC_ERROR_NONE;
628 int mc_set_fill_buffer_cb(MMHandleType mediacodec, mediacodec_output_buffer_available_cb callback, void *user_data)
630 int ret = MC_ERROR_NONE;
631 mc_handle_t *mc_handle = (mc_handle_t *)mediacodec;
634 LOGE("fail invaild param\n");
635 return MC_INVALID_ARG;
638 if (mc_handle->user_cb[_MEDIACODEC_EVENT_TYPE_FILLBUFFER]) {
639 LOGE("Already set mediacodec_fill_buffer_cb");
640 return MC_PARAM_ERROR;
643 return MC_INVALID_ARG;
645 LOGD("Set fill buffer callback(cb = %p, data = %p)", callback, user_data);
647 mc_handle->user_cb[_MEDIACODEC_EVENT_TYPE_FILLBUFFER] = (mc_fill_buffer_cb) callback;
648 mc_handle->user_data[_MEDIACODEC_EVENT_TYPE_FILLBUFFER] = user_data;
649 return MC_ERROR_NONE;
655 int mc_unset_fill_buffer_cb(MMHandleType mediacodec)
657 mc_handle_t *mc_handle = (mc_handle_t *)mediacodec;
660 LOGE("fail invaild param\n");
661 return MC_INVALID_ARG;
664 mc_handle->user_cb[_MEDIACODEC_EVENT_TYPE_FILLBUFFER] = NULL;
665 mc_handle->user_data[_MEDIACODEC_EVENT_TYPE_FILLBUFFER] = NULL;
667 return MC_ERROR_NONE;
670 int mc_set_error_cb(MMHandleType mediacodec, mediacodec_error_cb callback, void *user_data)
672 int ret = MC_ERROR_NONE;
673 mc_handle_t *mc_handle = (mc_handle_t *) mediacodec;
676 LOGE("fail invaild param\n");
677 return MC_INVALID_ARG;
680 if (mc_handle->user_cb[_MEDIACODEC_EVENT_TYPE_ERROR]) {
681 LOGE("Already set mediacodec_fill_buffer_cb\n");
682 return MC_PARAM_ERROR;
685 return MC_INVALID_ARG;
687 LOGD("Set event handler callback(cb = %p, data = %p)\n", callback, user_data);
689 mc_handle->user_cb[_MEDIACODEC_EVENT_TYPE_ERROR] = (mc_error_cb) callback;
690 mc_handle->user_data[_MEDIACODEC_EVENT_TYPE_ERROR] = user_data;
691 return MC_ERROR_NONE;
697 int mc_unset_error_cb(MMHandleType mediacodec)
699 mc_handle_t *mc_handle = (mc_handle_t *)mediacodec;
702 LOGE("fail invaild param");
703 return MC_INVALID_ARG;
706 mc_handle->user_cb[_MEDIACODEC_EVENT_TYPE_ERROR] = NULL;
707 mc_handle->user_data[_MEDIACODEC_EVENT_TYPE_ERROR] = NULL;
709 return MC_ERROR_NONE;
712 int mc_set_eos_cb(MMHandleType mediacodec, mediacodec_eos_cb callback, void *user_data)
714 int ret = MC_ERROR_NONE;
715 mc_handle_t *mc_handle = (mc_handle_t *) mediacodec;
718 LOGE("fail invaild param\n");
719 return MC_INVALID_ARG;
722 if (mc_handle->user_cb[_MEDIACODEC_EVENT_TYPE_EOS]) {
723 LOGE("Already set mediacodec_fill_buffer_cb");
724 return MC_PARAM_ERROR;
727 return MC_INVALID_ARG;
729 LOGD("Set event handler callback(cb = %p, data = %p)\n", callback, user_data);
731 mc_handle->user_cb[_MEDIACODEC_EVENT_TYPE_EOS] = (mc_eos_cb) callback;
732 mc_handle->user_data[_MEDIACODEC_EVENT_TYPE_EOS] = user_data;
733 return MC_ERROR_NONE;
739 int mc_unset_eos_cb(MMHandleType mediacodec)
741 mc_handle_t *mc_handle = (mc_handle_t *)mediacodec;
744 LOGE("fail invaild param\n");
745 return MC_INVALID_ARG;
748 mc_handle->user_cb[_MEDIACODEC_EVENT_TYPE_EOS] = NULL;
749 mc_handle->user_data[_MEDIACODEC_EVENT_TYPE_EOS] = NULL;
751 return MC_ERROR_NONE;
754 int mc_set_buffer_status_cb(MMHandleType mediacodec, mediacodec_buffer_status_cb callback, void *user_data)
756 int ret = MC_ERROR_NONE;
757 mc_handle_t *mc_handle = (mc_handle_t *)mediacodec;
760 LOGE("fail invaild param\n");
761 return MC_INVALID_ARG;
764 if (mc_handle->user_cb[_MEDIACODEC_EVENT_TYPE_BUFFER_STATUS]) {
765 LOGE("Already set mediacodec_need_data_cb\n");
766 return MC_PARAM_ERROR;
769 return MC_INVALID_ARG;
771 LOGD("Set start feed callback(cb = %p, data = %p)\n", callback, user_data);
773 mc_handle->user_cb[_MEDIACODEC_EVENT_TYPE_BUFFER_STATUS] = (mc_buffer_status_cb) callback;
774 mc_handle->user_data[_MEDIACODEC_EVENT_TYPE_BUFFER_STATUS] = user_data;
775 return MC_ERROR_NONE;
781 int mc_unset_buffer_status_cb(MMHandleType mediacodec)
783 mc_handle_t *mc_handle = (mc_handle_t *)mediacodec;
786 LOGE("fail invaild param\n");
787 return MC_INVALID_ARG;
790 mc_handle->user_cb[_MEDIACODEC_EVENT_TYPE_BUFFER_STATUS] = NULL;
791 mc_handle->user_data[_MEDIACODEC_EVENT_TYPE_BUFFER_STATUS] = NULL;
793 return MC_ERROR_NONE;
796 int _mediacodec_foreach_supported_codec(mediacodec_supported_codec_cb callback, void *user_data)
801 gboolean codec[CODEC_NR_ITEMS] = {0,};
803 for (i = 0; i < mc_ini.num_supported_codecs; i++) {
804 index = (int)codec_type_to_simple_enumeration(spec_emul[i].codec_id);
808 for (i = 0; i < CODEC_NR_ITEMS; i++) {
810 index = (int)simple_to_codec_type_enumeration(i);
811 if (!callback(index, user_data)) {
812 LOGW("stop foreach callback");
820 return MEDIACODEC_ERROR_NONE;
823 int mc_get_packet_pool(MMHandleType mediacodec, media_packet_pool_h *pool)
825 int ret = MC_ERROR_NONE;
826 mc_handle_t *mc_handle = (mc_handle_t *)mediacodec;
829 LOGE("fail invaild param\n");
830 return MC_INVALID_ARG;
833 /* setting core details */
834 switch (mc_handle->port_type) {
835 case MEDIACODEC_PORT_TYPE_GENERAL:
838 case MEDIACODEC_PORT_TYPE_OMX:
841 case MEDIACODEC_PORT_TYPE_GST:
842 ret = mc_gst_get_packet_pool(mc_handle, pool);
852 void _mc_create_decoder_map_from_ini(mc_handle_t *mediacodec)
854 int indx = 0, count = 0;
855 int codec_list = mediacodec->ini->codec_list;
856 for (indx = 0; indx < codec_list; indx++) {
857 if (strcmp(mediacodec->ini->codec[indx].codec_info[MEDIA_CODEC_ROLE_HW_DECODER].name, "")) {
858 mediacodec->decoder_map[count].id = mediacodec->ini->codec[indx].codec_id;
859 mediacodec->decoder_map[count].hardware = 1; /* hardware */
860 mediacodec->decoder_map[count].type.factory_name = mediacodec->ini->codec[indx].codec_info[MEDIA_CODEC_ROLE_HW_DECODER].name;
861 mediacodec->decoder_map[count].type.mime = mediacodec->ini->codec[indx].codec_info[MEDIA_CODEC_ROLE_HW_DECODER].mime;
862 mediacodec->decoder_map[count].type.out_format =
863 _mc_convert_media_format_str_to_int(mediacodec->ini->codec[indx].codec_info[MEDIA_CODEC_ROLE_HW_DECODER].format);
864 mediacodec->decoder_map[count].type.property = &mediacodec->ini->codec[indx].codec_info[MEDIA_CODEC_ROLE_HW_DECODER].property;
868 if (strcmp(mediacodec->ini->codec[indx].codec_info[MEDIA_CODEC_ROLE_SW_DECODER].name, "")) {
869 mediacodec->decoder_map[count].id = mediacodec->ini->codec[indx].codec_id;
870 mediacodec->decoder_map[count].hardware = 0; /* software */
871 mediacodec->decoder_map[count].type.factory_name = mediacodec->ini->codec[indx].codec_info[MEDIA_CODEC_ROLE_SW_DECODER].name;
872 mediacodec->decoder_map[count].type.mime = mediacodec->ini->codec[indx].codec_info[MEDIA_CODEC_ROLE_SW_DECODER].mime;
873 mediacodec->decoder_map[count].type.out_format =
874 _mc_convert_media_format_str_to_int(mediacodec->ini->codec[indx].codec_info[MEDIA_CODEC_ROLE_SW_DECODER].format);
875 mediacodec->decoder_map[count].type.property = &mediacodec->ini->codec[indx].codec_info[MEDIA_CODEC_ROLE_SW_DECODER].property;
879 mediacodec->num_supported_decoder = count;
884 void _mc_create_encoder_map_from_ini(mc_handle_t *mediacodec)
886 int indx = 0, count = 0;
887 int codec_list = mediacodec->ini->codec_list;
889 for (indx = 0; indx < codec_list; indx++) {
890 if (strcmp(mediacodec->ini->codec[indx].codec_info[MEDIA_CODEC_ROLE_HW_ENCODER].name, "")) {
891 mediacodec->encoder_map[count].id = mediacodec->ini->codec[indx].codec_id;
892 mediacodec->encoder_map[count].hardware = 1;
893 mediacodec->encoder_map[count].type.factory_name = mediacodec->ini->codec[indx].codec_info[MEDIA_CODEC_ROLE_HW_ENCODER].name;
894 mediacodec->encoder_map[count].type.mime = mediacodec->ini->codec[indx].codec_info[MEDIA_CODEC_ROLE_HW_ENCODER].mime;
895 mediacodec->encoder_map[count].type.out_format =
896 _mc_convert_media_format_str_to_int(mediacodec->ini->codec[indx].codec_info[MEDIA_CODEC_ROLE_HW_ENCODER].format);
897 mediacodec->encoder_map[count].type.property = &mediacodec->ini->codec[indx].codec_info[MEDIA_CODEC_ROLE_HW_ENCODER].property;
901 if (strcmp(mediacodec->ini->codec[indx].codec_info[MEDIA_CODEC_ROLE_SW_ENCODER].name, "")) {
902 mediacodec->encoder_map[count].id = mediacodec->ini->codec[indx].codec_id;
903 mediacodec->encoder_map[count].hardware = 0;
904 mediacodec->encoder_map[count].type.factory_name = mediacodec->ini->codec[indx].codec_info[MEDIA_CODEC_ROLE_SW_ENCODER].name;
905 mediacodec->encoder_map[count].type.mime = mediacodec->ini->codec[indx].codec_info[MEDIA_CODEC_ROLE_SW_ENCODER].mime;
906 mediacodec->encoder_map[count].type.out_format =
907 _mc_convert_media_format_str_to_int(mediacodec->ini->codec[indx].codec_info[MEDIA_CODEC_ROLE_SW_ENCODER].format);
908 mediacodec->encoder_map[count].type.property = &mediacodec->ini->codec[indx].codec_info[MEDIA_CODEC_ROLE_SW_ENCODER].property;
912 mediacodec->num_supported_encoder = count;
916 void _mc_create_codec_map_from_ini(mc_handle_t *mediacodec, mc_codec_spec_t *spec_emul)
918 int indx = 0, count = 0;
919 int codec_list = mediacodec->ini->codec_list;
921 for (indx = 0; indx < codec_list; indx++) {
922 if (strcmp(mediacodec->ini->codec[indx].codec_info[MEDIA_CODEC_ROLE_HW_DECODER].name, "")) {
923 spec_emul[count].codec_id = mediacodec->ini->codec[indx].codec_id;
924 spec_emul[count].codec_type = MEDIACODEC_DECODER | MEDIACODEC_SUPPORT_TYPE_HW;
925 spec_emul[count].port_type = MEDIACODEC_PORT_TYPE_GST;
928 if (strcmp(mediacodec->ini->codec[indx].codec_info[MEDIA_CODEC_ROLE_HW_ENCODER].name, "")) {
929 spec_emul[count].codec_id = mediacodec->ini->codec[indx].codec_id;
930 spec_emul[count].codec_type = MEDIACODEC_ENCODER | MEDIACODEC_SUPPORT_TYPE_HW;
931 spec_emul[count].port_type = MEDIACODEC_PORT_TYPE_GST;
934 if (strcmp(mediacodec->ini->codec[indx].codec_info[MEDIA_CODEC_ROLE_SW_DECODER].name, "")) {
935 spec_emul[count].codec_id = mediacodec->ini->codec[indx].codec_id;
936 spec_emul[count].codec_type = MEDIACODEC_DECODER | MEDIACODEC_SUPPORT_TYPE_SW;
937 spec_emul[count].port_type = MEDIACODEC_PORT_TYPE_GST;
940 if (strcmp(mediacodec->ini->codec[indx].codec_info[MEDIA_CODEC_ROLE_SW_ENCODER].name, "")) {
941 spec_emul[count].codec_id = mediacodec->ini->codec[indx].codec_id;
942 spec_emul[count].codec_type = MEDIACODEC_ENCODER | MEDIACODEC_SUPPORT_TYPE_SW;
943 spec_emul[count].port_type = MEDIACODEC_PORT_TYPE_GST;
948 mediacodec->ini->num_supported_codecs = count;
949 LOGE("supported codecs :%d", count);
953 void _mc_create_codec_map_from_ini_static(mc_ini_t *ini, mc_codec_spec_t *spec_emul)
955 int indx = 0, count = 0;
956 int codec_list = ini->codec_list;
958 for (indx = 0; indx < codec_list; indx++) {
959 if (strcmp(ini->codec[indx].codec_info[MEDIA_CODEC_ROLE_HW_DECODER].name, "")) {
960 spec_emul[count].codec_id = ini->codec[indx].codec_id;
961 spec_emul[count].codec_type = MEDIACODEC_DECODER | MEDIACODEC_SUPPORT_TYPE_HW;
962 spec_emul[count].port_type = MEDIACODEC_PORT_TYPE_GST;
965 if (strcmp(ini->codec[indx].codec_info[MEDIA_CODEC_ROLE_HW_ENCODER].name, "")) {
966 spec_emul[count].codec_id = ini->codec[indx].codec_id;
967 spec_emul[count].codec_type = MEDIACODEC_ENCODER | MEDIACODEC_SUPPORT_TYPE_HW;
968 spec_emul[count].port_type = MEDIACODEC_PORT_TYPE_GST;
971 if (strcmp(ini->codec[indx].codec_info[MEDIA_CODEC_ROLE_SW_DECODER].name, "")) {
972 spec_emul[count].codec_id = ini->codec[indx].codec_id;
973 spec_emul[count].codec_type = MEDIACODEC_DECODER | MEDIACODEC_SUPPORT_TYPE_SW;
974 spec_emul[count].port_type = MEDIACODEC_PORT_TYPE_GST;
977 if (strcmp(ini->codec[indx].codec_info[MEDIA_CODEC_ROLE_SW_ENCODER].name, "")) {
978 spec_emul[count].codec_id = ini->codec[indx].codec_id;
979 spec_emul[count].codec_type = MEDIACODEC_ENCODER | MEDIACODEC_SUPPORT_TYPE_SW;
980 spec_emul[count].port_type = MEDIACODEC_PORT_TYPE_GST;
985 ini->num_supported_codecs = count;
986 LOGE("supported codecs :%d", count);
990 codec_type_e codec_type_to_simple_enumeration(mediacodec_codec_type_e media_codec_id)
992 guint media_codec_id_u = (guint)media_codec_id;
994 switch (media_codec_id_u) {
997 case MEDIACODEC_ALAW:
999 case MEDIACODEC_ULAW:
1001 case MEDIACODEC_AMR_NB:
1003 case MEDIACODEC_AMR_WB:
1005 case MEDIACODEC_G729:
1007 case MEDIACODEC_AAC_LC:
1009 case MEDIACODEC_AAC_HE:
1011 case MEDIACODEC_AAC_HE_PS:
1013 case MEDIACODEC_MP3:
1015 case MEDIACODEC_VORBIS:
1017 case MEDIACODEC_FLAC:
1019 case MEDIACODEC_WMAV1:
1021 case MEDIACODEC_WMAV2:
1023 case MEDIACODEC_WMAPRO:
1025 case MEDIACODEC_WMALSL:
1027 case MEDIACODEC_H261:
1029 case MEDIACODEC_H263:
1031 case MEDIACODEC_H264:
1033 case MEDIACODEC_MJPEG:
1035 case MEDIACODEC_MPEG1:
1037 case MEDIACODEC_MPEG2:
1039 case MEDIACODEC_MPEG4:
1041 case MEDIACODEC_HEVC:
1043 case MEDIACODEC_VP8:
1045 case MEDIACODEC_VP9:
1047 case MEDIACODEC_VC1:
1049 case MEDIACODEC_OPUS:
1056 mediacodec_codec_type_e simple_to_codec_type_enumeration(codec_type_e codec_id)
1058 guint codec_id_u = (guint)codec_id;
1060 switch (codec_id_u) {
1062 return MEDIACODEC_L16;
1064 return MEDIACODEC_ALAW;
1066 return MEDIACODEC_ULAW;
1068 return MEDIACODEC_AMR_NB;
1070 return MEDIACODEC_AMR_WB;
1072 return MEDIACODEC_G729;
1074 return MEDIACODEC_AAC_LC;
1076 return MEDIACODEC_AAC_HE;
1078 return MEDIACODEC_AAC_HE_PS;
1080 return MEDIACODEC_MP3;
1082 return MEDIACODEC_VORBIS;
1084 return MEDIACODEC_FLAC;
1086 return MEDIACODEC_WMAV1;
1088 return MEDIACODEC_WMAV2;
1090 return MEDIACODEC_WMAPRO;
1092 return MEDIACODEC_WMALSL;
1094 return MEDIACODEC_H261;
1096 return MEDIACODEC_H263;
1098 return MEDIACODEC_H264;
1100 return MEDIACODEC_MJPEG;
1102 return MEDIACODEC_MPEG1;
1104 return MEDIACODEC_MPEG2;
1106 return MEDIACODEC_MPEG4;
1108 return MEDIACODEC_HEVC;
1110 return MEDIACODEC_VP8;
1112 return MEDIACODEC_VP9;
1114 return MEDIACODEC_VC1;
1116 return MEDIACODEC_OPUS;
1122 gboolean _check_support_audio_info(mediacodec_codec_type_e codec_id, int samplerate, int channel, int bit_depth)
1125 gint maxchannels = 2;
1127 gint s_bit_depth = 32;
1130 case MEDIACODEC_AMR_NB:
1132 const static gint l_rates[] = { 8000 };
1134 n_rates = G_N_ELEMENTS(l_rates);
1135 s_bit_depth = 16; /* NOTE: amrnbenc/amrnbdec surpports S16LE as format*/
1137 while (i < n_rates) {
1138 if (l_rates[i] == samplerate)
1144 LOGE("Invalid samplerate set");
1149 case MEDIACODEC_AAC_LC:
1150 case MEDIACODEC_AAC_HE:
1152 const static gint l_rates[] = { 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, 7350 };
1154 n_rates = G_N_ELEMENTS(l_rates);
1155 s_bit_depth = 32; /* NOTE: avdec_aac/avenc_aac surpports S32LE as format*/
1157 while (i < n_rates) {
1158 if (l_rates[i] == samplerate)
1164 LOGE("Invalid samplerate set");
1169 case MEDIACODEC_MP3:
1171 s_bit_depth = 16; /* NOTE: amrenc/amrnbdec surpports S16LE as format*/
1174 case MEDIACODEC_VORBIS:
1176 s_bit_depth = 32; /* NOTE: vorbisenc/vorbisdec surpports S32LE as format */
1179 case MEDIACODEC_FLAC:
1181 s_bit_depth = 32; /* NOTE: avdec_flac surpports S32LE as format */
1184 case MEDIACODEC_OPUS:
1186 s_bit_depth = 16; /* NOTE: opusenc/ opusdec support S16LE as format according to opus specification*/
1193 if (channel < 0 || channel > maxchannels) {
1194 LOGE("Invalid channel set");
1198 if (bit_depth != s_bit_depth) {
1199 LOGE("Invalid bit set");
1206 gboolean _check_support_video_info(mediacodec_codec_type_e codec_id, int width, int height)
1211 if (width <= 0 || height <= 0) {
1212 LOGE("Invalid resolution set");
1217 case MEDIACODEC_H261:
1219 const static gint widths[] = { 352, 176 };
1220 const static gint heights[] = { 288, 144 };
1221 n_sizes = G_N_ELEMENTS(widths);
1223 while (i < n_sizes) {
1224 if ((widths[i] == width) && (heights[i] == height))
1230 LOGE("Invalid resolution set");
1235 case MEDIACODEC_H263:
1237 const static gint widths[] = { 352, 704, 176, 1408, 128 };
1238 const static gint heights[] = { 288, 576, 144, 1152, 96 };
1239 n_sizes = G_N_ELEMENTS(widths);
1241 while (i < n_sizes) {
1242 if ((widths[i] == width) && (heights[i] == height))
1248 LOGE("Invalid resolution set");