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 = MC_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 media_format_get_audio_info(format, &mimetype, &channel, &samplerate, &bit, &bitrate);
301 codec_id = mimetype & codec_mask;
303 if (GET_IS_ENCODER(flags)) {
304 if (!_check_support_audio_info(codec_id, samplerate, channel, bit)) {
305 LOGE("invalid pram is set : samplerate : %d, channel : %d, bit : %d, bitrate : %d",
306 samplerate, channel, bit, bitrate);
307 return MC_PARAM_ERROR;
310 mc_handle->info.audio.samplerate = samplerate;
311 mc_handle->info.audio.channel = channel;
312 mc_handle->info.audio.bit_depth = bit;
313 mc_handle->info.audio.bitrate = bitrate * 1000;
314 } else if (GET_IS_DECODER(flags)) {
315 if (!_check_support_audio_info(codec_id, samplerate, channel, bit)) {
316 LOGE("invalid pram is set : samplerate : %d, channel : %d, bit : %d",
317 samplerate, channel, bit);
318 return MC_PARAM_ERROR;
320 mc_handle->info.audio.samplerate = samplerate;
321 mc_handle->info.audio.channel = channel;
322 mc_handle->info.audio.bit_depth = bit;
324 LOGE("either an encoder or a decoder must be set");
325 return MC_PARAM_ERROR;
327 } else if (type == MEDIA_FORMAT_VIDEO) {
328 media_format_get_video_info(format, &mimetype, &width, &height, &bitrate, NULL);
329 media_format_get_video_frame_rate(format, &fps);
330 codec_id = mimetype & codec_mask;
332 if (GET_IS_ENCODER(flags)) {
333 if (!_check_support_video_info(codec_id, width, height) || fps <= 0 || bitrate <= 0) {
334 LOGE("invalid pram is set : width : %d, height : %d, bitrate : %d, fps : %d",
335 width, height, bitrate, fps);
336 return MC_PARAM_ERROR;
339 mc_handle->info.video.width = width;
340 mc_handle->info.video.height = height;
341 mc_handle->info.video.framerate = fps;
342 mc_handle->info.video.bitrate = bitrate * 1000;
343 } else if (GET_IS_DECODER(flags)) {
344 if (!_check_support_video_info(codec_id, width, height)) {
345 LOGE("invalid pram is set : width : %d, height : %d",
347 return MC_PARAM_ERROR;
350 mc_handle->info.video.width = width;
351 mc_handle->info.video.height = height;
353 LOGE("either an encoder or a decoder must be set");
354 return MC_PARAM_ERROR;
357 LOGE("invalid format type is set");
358 return MC_PARAM_ERROR;
361 if (!GET_IS_HW(flags) && !GET_IS_SW(flags))
362 flags |= MEDIACODEC_SUPPORT_TYPE_SW;
364 for (i = 0; i < mc_handle->ini->num_supported_codecs; i++) {
365 if ((codec_id == spec_emul[i].codec_id) && (flags == spec_emul[i].codec_type))
368 LOGD("support_list : %d, i : %d", mc_handle->ini->num_supported_codecs, i);
370 if (i == mc_handle->ini->num_supported_codecs)
371 return MC_CODEC_NOT_FOUND;
373 mc_handle->port_type = spec_emul[i].port_type;
375 mc_handle->is_encoder = GET_IS_ENCODER(flags) ? 1 : 0;
376 mc_handle->is_hw = GET_IS_HW(flags) ? 1 : 0;
377 mc_handle->codec_id = codec_id;
378 mc_handle->is_video = CHECK_BIT(codec_id, 13);
380 mc_handle->is_prepared = true;
382 LOGD("encoder : %d, hardware : %d, codec_id : %x, video : %d",
383 mc_handle->is_encoder, mc_handle->is_hw, mc_handle->codec_id, mc_handle->is_video);
388 int mc_prepare(MMHandleType mediacodec)
390 int ret = MC_ERROR_NONE;
391 mc_handle_t *mc_handle = (mc_handle_t *) mediacodec;
394 LOGE("fail invaild param\n");
395 return MC_INVALID_ARG;
398 if (!mc_handle->is_prepared)
399 return MC_NOT_INITIALIZED;
401 /* setting core details */
402 switch (mc_handle->port_type) {
403 case MEDIACODEC_PORT_TYPE_GENERAL:
406 case MEDIACODEC_PORT_TYPE_OMX:
409 case MEDIACODEC_PORT_TYPE_GST:
410 ret = mc_gst_prepare(mc_handle);
420 int mc_unprepare(MMHandleType mediacodec)
422 int ret = MC_ERROR_NONE;
423 mc_handle_t *mc_handle = (mc_handle_t *)mediacodec;
426 LOGE("fail invaild param\n");
427 return MC_INVALID_ARG;
430 /* deinit core details */
431 switch (mc_handle->port_type) {
432 case MEDIACODEC_PORT_TYPE_GENERAL:
435 case MEDIACODEC_PORT_TYPE_OMX:
438 case MEDIACODEC_PORT_TYPE_GST:
439 ret = mc_gst_unprepare(mc_handle);
449 int mc_process_input(MMHandleType mediacodec, media_packet_h inbuf, uint64_t timeOutUs)
451 int ret = MC_ERROR_NONE;
453 mc_handle_t *mc_handle = (mc_handle_t *)mediacodec;
456 LOGE("fail invaild param");
457 return MC_INVALID_ARG;
461 LOGE("invaild input buffer");
462 return MC_INVALID_IN_BUF;
465 switch (mc_handle->port_type) {
466 case MEDIACODEC_PORT_TYPE_GENERAL:
469 case MEDIACODEC_PORT_TYPE_OMX:
472 case MEDIACODEC_PORT_TYPE_GST:
473 ret = mc_gst_process_input(mc_handle, inbuf, timeOutUs);
483 int mc_get_output(MMHandleType mediacodec, media_packet_h *outbuf, uint64_t timeOutUs)
485 int ret = MC_ERROR_NONE;
486 mc_handle_t *mc_handle = (mc_handle_t *) mediacodec;
489 LOGE("fail invaild param\n");
490 return MC_INVALID_ARG;
493 /* setting core details */
494 switch (mc_handle->port_type) {
495 case MEDIACODEC_PORT_TYPE_GENERAL:
498 case MEDIACODEC_PORT_TYPE_OMX:
501 case MEDIACODEC_PORT_TYPE_GST:
502 ret = mc_gst_get_output(mc_handle, outbuf, timeOutUs);
512 int mc_flush_buffers(MMHandleType mediacodec)
514 int ret = MC_ERROR_NONE;
515 mc_handle_t *mc_handle = (mc_handle_t *)mediacodec;
518 LOGE("fail invaild param\n");
519 return MC_INVALID_ARG;
522 /* setting core details */
523 switch (mc_handle->port_type) {
524 case MEDIACODEC_PORT_TYPE_GENERAL:
527 case MEDIACODEC_PORT_TYPE_OMX:
530 case MEDIACODEC_PORT_TYPE_GST:
531 ret = mc_gst_flush_buffers(mc_handle);
541 int mc_get_supported_type(MMHandleType mediacodec, mediacodec_codec_type_e codec_type, bool encoder, int *support_type)
543 int ret = MC_ERROR_NONE;
544 mc_handle_t *mc_handle = (mc_handle_t *)mediacodec;
545 mc_codec_map_t *codec_map;
546 int num_supported_codec = 0;
552 LOGE("fail invaild param\n");
553 return MC_INVALID_ARG;
556 codec_map = encoder ? mc_handle->encoder_map : mc_handle->decoder_map;
557 num_supported_codec = encoder ? mc_handle->num_supported_encoder : mc_handle->num_supported_decoder;
559 for (i = 0; i < num_supported_codec; i++) {
560 if (codec_map[i].id == codec_type) {
561 if (codec_map[i].hardware)
562 *support_type |= MEDIACODEC_SUPPORT_TYPE_HW;
564 *support_type |= MEDIACODEC_SUPPORT_TYPE_SW;
571 int mc_set_empty_buffer_cb(MMHandleType mediacodec, mediacodec_input_buffer_used_cb callback, void *user_data)
573 int ret = MC_ERROR_NONE;
574 mc_handle_t *mc_handle = (mc_handle_t *)mediacodec;
577 LOGE("fail invaild param\n");
578 return MC_INVALID_ARG;
581 if (mc_handle->user_cb[_MEDIACODEC_EVENT_TYPE_EMPTYBUFFER]) {
582 LOGE("Already set mediacodec_empty_buffer_cb");
583 return MC_PARAM_ERROR;
586 return MC_INVALID_ARG;
588 LOGD("Set empty buffer callback(cb = %p, data = %p)", callback, user_data);
590 mc_handle->user_cb[_MEDIACODEC_EVENT_TYPE_EMPTYBUFFER] = (mc_empty_buffer_cb) callback;
591 mc_handle->user_data[_MEDIACODEC_EVENT_TYPE_EMPTYBUFFER] = user_data;
593 return MC_ERROR_NONE;
599 int mc_unset_empty_buffer_cb(MMHandleType mediacodec)
601 mc_handle_t *mc_handle = (mc_handle_t *)mediacodec;
604 LOGE("fail invaild param\n");
605 return MC_INVALID_ARG;
608 mc_handle->user_cb[_MEDIACODEC_EVENT_TYPE_EMPTYBUFFER] = NULL;
609 mc_handle->user_data[_MEDIACODEC_EVENT_TYPE_EMPTYBUFFER] = NULL;
611 return MC_ERROR_NONE;
614 int mc_set_fill_buffer_cb(MMHandleType mediacodec, mediacodec_output_buffer_available_cb callback, void *user_data)
616 int ret = MC_ERROR_NONE;
617 mc_handle_t *mc_handle = (mc_handle_t *)mediacodec;
620 LOGE("fail invaild param\n");
621 return MC_INVALID_ARG;
624 if (mc_handle->user_cb[_MEDIACODEC_EVENT_TYPE_FILLBUFFER]) {
625 LOGE("Already set mediacodec_fill_buffer_cb");
626 return MC_PARAM_ERROR;
629 return MC_INVALID_ARG;
631 LOGD("Set fill buffer callback(cb = %p, data = %p)", callback, user_data);
633 mc_handle->user_cb[_MEDIACODEC_EVENT_TYPE_FILLBUFFER] = (mc_fill_buffer_cb) callback;
634 mc_handle->user_data[_MEDIACODEC_EVENT_TYPE_FILLBUFFER] = user_data;
635 return MC_ERROR_NONE;
641 int mc_unset_fill_buffer_cb(MMHandleType mediacodec)
643 mc_handle_t *mc_handle = (mc_handle_t *)mediacodec;
646 LOGE("fail invaild param\n");
647 return MC_INVALID_ARG;
650 mc_handle->user_cb[_MEDIACODEC_EVENT_TYPE_FILLBUFFER] = NULL;
651 mc_handle->user_data[_MEDIACODEC_EVENT_TYPE_FILLBUFFER] = NULL;
653 return MC_ERROR_NONE;
656 int mc_set_error_cb(MMHandleType mediacodec, mediacodec_error_cb callback, void *user_data)
658 int ret = MC_ERROR_NONE;
659 mc_handle_t *mc_handle = (mc_handle_t *) mediacodec;
662 LOGE("fail invaild param\n");
663 return MC_INVALID_ARG;
666 if (mc_handle->user_cb[_MEDIACODEC_EVENT_TYPE_ERROR]) {
667 LOGE("Already set mediacodec_fill_buffer_cb\n");
668 return MC_PARAM_ERROR;
671 return MC_INVALID_ARG;
673 LOGD("Set event handler callback(cb = %p, data = %p)\n", callback, user_data);
675 mc_handle->user_cb[_MEDIACODEC_EVENT_TYPE_ERROR] = (mc_error_cb) callback;
676 mc_handle->user_data[_MEDIACODEC_EVENT_TYPE_ERROR] = user_data;
677 return MC_ERROR_NONE;
683 int mc_unset_error_cb(MMHandleType mediacodec)
685 mc_handle_t *mc_handle = (mc_handle_t *)mediacodec;
688 LOGE("fail invaild param");
689 return MC_INVALID_ARG;
692 mc_handle->user_cb[_MEDIACODEC_EVENT_TYPE_ERROR] = NULL;
693 mc_handle->user_data[_MEDIACODEC_EVENT_TYPE_ERROR] = NULL;
695 return MC_ERROR_NONE;
698 int mc_set_eos_cb(MMHandleType mediacodec, mediacodec_eos_cb callback, void *user_data)
700 int ret = MC_ERROR_NONE;
701 mc_handle_t *mc_handle = (mc_handle_t *) mediacodec;
704 LOGE("fail invaild param\n");
705 return MC_INVALID_ARG;
708 if (mc_handle->user_cb[_MEDIACODEC_EVENT_TYPE_EOS]) {
709 LOGE("Already set mediacodec_fill_buffer_cb");
710 return MC_PARAM_ERROR;
713 return MC_INVALID_ARG;
715 LOGD("Set event handler callback(cb = %p, data = %p)\n", callback, user_data);
717 mc_handle->user_cb[_MEDIACODEC_EVENT_TYPE_EOS] = (mc_eos_cb) callback;
718 mc_handle->user_data[_MEDIACODEC_EVENT_TYPE_EOS] = user_data;
719 return MC_ERROR_NONE;
725 int mc_unset_eos_cb(MMHandleType mediacodec)
727 mc_handle_t *mc_handle = (mc_handle_t *)mediacodec;
730 LOGE("fail invaild param\n");
731 return MC_INVALID_ARG;
734 mc_handle->user_cb[_MEDIACODEC_EVENT_TYPE_EOS] = NULL;
735 mc_handle->user_data[_MEDIACODEC_EVENT_TYPE_EOS] = NULL;
737 return MC_ERROR_NONE;
740 int mc_set_buffer_status_cb(MMHandleType mediacodec, mediacodec_buffer_status_cb callback, void *user_data)
742 int ret = MC_ERROR_NONE;
743 mc_handle_t *mc_handle = (mc_handle_t *)mediacodec;
746 LOGE("fail invaild param\n");
747 return MC_INVALID_ARG;
750 if (mc_handle->user_cb[_MEDIACODEC_EVENT_TYPE_BUFFER_STATUS]) {
751 LOGE("Already set mediacodec_need_data_cb\n");
752 return MC_PARAM_ERROR;
755 return MC_INVALID_ARG;
757 LOGD("Set start feed callback(cb = %p, data = %p)\n", callback, user_data);
759 mc_handle->user_cb[_MEDIACODEC_EVENT_TYPE_BUFFER_STATUS] = (mc_buffer_status_cb) callback;
760 mc_handle->user_data[_MEDIACODEC_EVENT_TYPE_BUFFER_STATUS] = user_data;
761 return MC_ERROR_NONE;
767 int mc_unset_buffer_status_cb(MMHandleType mediacodec)
769 mc_handle_t *mc_handle = (mc_handle_t *)mediacodec;
772 LOGE("fail invaild param\n");
773 return MC_INVALID_ARG;
776 mc_handle->user_cb[_MEDIACODEC_EVENT_TYPE_BUFFER_STATUS] = NULL;
777 mc_handle->user_data[_MEDIACODEC_EVENT_TYPE_BUFFER_STATUS] = NULL;
779 return MC_ERROR_NONE;
782 int mc_set_supported_codec_cb(MMHandleType mediacodec, mediacodec_supported_codec_cb callback, void *user_data)
784 int ret = MC_ERROR_NONE;
785 mc_handle_t *mc_handle = (mc_handle_t *)mediacodec;
788 LOGE("fail invaild param\n");
789 return MC_INVALID_ARG;
792 if (mc_handle->user_cb[_MEDIACODEC_EVENT_TYPE_SUPPORTED_CODEC]) {
793 LOGE("Already set mediacodec_supported_codec_cb\n");
794 return MC_PARAM_ERROR;
797 return MC_INVALID_ARG;
799 LOGD("Set event handler callback(cb = %p, data = %p)", callback, user_data);
801 mc_handle->user_cb[_MEDIACODEC_EVENT_TYPE_SUPPORTED_CODEC] = (mc_supported_codec_cb) callback;
802 mc_handle->user_data[_MEDIACODEC_EVENT_TYPE_SUPPORTED_CODEC] = user_data;
803 return MC_ERROR_NONE;
809 int _mediacodec_foreach_supported_codec(mediacodec_supported_codec_cb callback, void *user_data)
814 gboolean codec[CODEC_NR_ITEMS] = {0,};
816 for (i = 0; i < mc_ini.num_supported_codecs; i++) {
817 index = (int)codec_type_to_simple_enumeration(spec_emul[i].codec_id);
821 for (i = 0; i < CODEC_NR_ITEMS; i++) {
823 index = (int)simple_to_codec_type_enumeration(i);
824 if (!callback(index, user_data)) {
825 LOGW("stop foreach callback");
833 return MEDIACODEC_ERROR_NONE;
836 int mc_get_packet_pool(MMHandleType mediacodec, media_packet_pool_h *pool)
838 int ret = MC_ERROR_NONE;
839 mc_handle_t *mc_handle = (mc_handle_t *)mediacodec;
842 LOGE("fail invaild param\n");
843 return MC_INVALID_ARG;
846 /* setting core details */
847 switch (mc_handle->port_type) {
848 case MEDIACODEC_PORT_TYPE_GENERAL:
851 case MEDIACODEC_PORT_TYPE_OMX:
854 case MEDIACODEC_PORT_TYPE_GST:
855 ret = mc_gst_get_packet_pool(mc_handle, pool);
865 void _mc_create_decoder_map_from_ini(mc_handle_t *mediacodec)
867 int indx = 0, count = 0;
868 int codec_list = mediacodec->ini->codec_list;
869 for (indx = 0; indx < codec_list; indx++) {
870 if (strcmp(mediacodec->ini->codec[indx].codec_info[0].name, "")) {
871 mediacodec->decoder_map[count].id = mediacodec->ini->codec[indx].codec_id;
872 mediacodec->decoder_map[count].hardware = 1; /* hardware */
873 mediacodec->decoder_map[count].type.factory_name = mediacodec->ini->codec[indx].codec_info[0].name;
874 mediacodec->decoder_map[count].type.mime = mediacodec->ini->codec[indx].codec_info[0].mime;
875 mediacodec->decoder_map[count].type.out_format =
876 _mc_convert_media_format_str_to_int(mediacodec->ini->codec[indx].codec_info[0].format);
880 if (strcmp(mediacodec->ini->codec[indx].codec_info[2].name, "")) {
881 mediacodec->decoder_map[count].id = mediacodec->ini->codec[indx].codec_id;
882 mediacodec->decoder_map[count].hardware = 0; /* software */
883 mediacodec->decoder_map[count].type.factory_name = mediacodec->ini->codec[indx].codec_info[2].name;
884 mediacodec->decoder_map[count].type.mime = mediacodec->ini->codec[indx].codec_info[2].mime;
885 mediacodec->decoder_map[count].type.out_format =
886 _mc_convert_media_format_str_to_int(mediacodec->ini->codec[indx].codec_info[2].format);
890 mediacodec->num_supported_decoder = count;
895 void _mc_create_encoder_map_from_ini(mc_handle_t *mediacodec)
897 int indx = 0, count = 0;
898 int codec_list = mediacodec->ini->codec_list;
900 for (indx = 0; indx < codec_list; indx++) {
901 if (strcmp(mediacodec->ini->codec[indx].codec_info[1].name, "")) {
902 mediacodec->encoder_map[count].id = mediacodec->ini->codec[indx].codec_id;
903 mediacodec->encoder_map[count].hardware = 1;
904 mediacodec->encoder_map[count].type.factory_name = mediacodec->ini->codec[indx].codec_info[1].name;
905 mediacodec->encoder_map[count].type.mime = mediacodec->ini->codec[indx].codec_info[1].mime;
906 mediacodec->encoder_map[count].type.out_format =
907 _mc_convert_media_format_str_to_int(mediacodec->ini->codec[indx].codec_info[1].format);
911 if (strcmp(mediacodec->ini->codec[indx].codec_info[3].name, "")) {
912 mediacodec->encoder_map[count].id = mediacodec->ini->codec[indx].codec_id;
913 mediacodec->encoder_map[count].hardware = 0;
914 mediacodec->encoder_map[count].type.factory_name = mediacodec->ini->codec[indx].codec_info[3].name;
915 mediacodec->encoder_map[count].type.mime = mediacodec->ini->codec[indx].codec_info[3].mime;
916 mediacodec->encoder_map[count].type.out_format =
917 _mc_convert_media_format_str_to_int(mediacodec->ini->codec[indx].codec_info[3].format);
921 mediacodec->num_supported_encoder = count;
925 void _mc_create_codec_map_from_ini(mc_handle_t *mediacodec, mc_codec_spec_t *spec_emul)
927 int indx = 0, count = 0;
928 int codec_list = mediacodec->ini->codec_list;
930 for (indx = 0; indx < codec_list; indx++) {
931 if (strcmp(mediacodec->ini->codec[indx].codec_info[0].name, "")) {
932 spec_emul[count].codec_id = mediacodec->ini->codec[indx].codec_id;
933 spec_emul[count].codec_type = MEDIACODEC_DECODER | MEDIACODEC_SUPPORT_TYPE_HW;
934 spec_emul[count].port_type = MEDIACODEC_PORT_TYPE_GST;
937 if (strcmp(mediacodec->ini->codec[indx].codec_info[1].name, "")) {
938 spec_emul[count].codec_id = mediacodec->ini->codec[indx].codec_id;
939 spec_emul[count].codec_type = MEDIACODEC_ENCODER | MEDIACODEC_SUPPORT_TYPE_HW;
940 spec_emul[count].port_type = MEDIACODEC_PORT_TYPE_GST;
943 if (strcmp(mediacodec->ini->codec[indx].codec_info[2].name, "")) {
944 spec_emul[count].codec_id = mediacodec->ini->codec[indx].codec_id;
945 spec_emul[count].codec_type = MEDIACODEC_DECODER | MEDIACODEC_SUPPORT_TYPE_SW;
946 spec_emul[count].port_type = MEDIACODEC_PORT_TYPE_GST;
949 if (strcmp(mediacodec->ini->codec[indx].codec_info[3].name, "")) {
950 spec_emul[count].codec_id = mediacodec->ini->codec[indx].codec_id;
951 spec_emul[count].codec_type = MEDIACODEC_ENCODER | MEDIACODEC_SUPPORT_TYPE_SW;
952 spec_emul[count].port_type = MEDIACODEC_PORT_TYPE_GST;
957 mediacodec->ini->num_supported_codecs = count;
958 LOGE("supported codecs :%d", count);
962 void _mc_create_codec_map_from_ini_static(mc_ini_t *ini, mc_codec_spec_t *spec_emul)
964 int indx = 0, count = 0;
965 int codec_list = ini->codec_list;
967 for (indx = 0; indx < codec_list; indx++) {
968 if (strcmp(ini->codec[indx].codec_info[0].name, "")) {
969 spec_emul[count].codec_id = ini->codec[indx].codec_id;
970 spec_emul[count].codec_type = MEDIACODEC_DECODER | MEDIACODEC_SUPPORT_TYPE_HW;
971 spec_emul[count].port_type = MEDIACODEC_PORT_TYPE_GST;
974 if (strcmp(ini->codec[indx].codec_info[1].name, "")) {
975 spec_emul[count].codec_id = ini->codec[indx].codec_id;
976 spec_emul[count].codec_type = MEDIACODEC_ENCODER | MEDIACODEC_SUPPORT_TYPE_HW;
977 spec_emul[count].port_type = MEDIACODEC_PORT_TYPE_GST;
980 if (strcmp(ini->codec[indx].codec_info[2].name, "")) {
981 spec_emul[count].codec_id = ini->codec[indx].codec_id;
982 spec_emul[count].codec_type = MEDIACODEC_DECODER | MEDIACODEC_SUPPORT_TYPE_SW;
983 spec_emul[count].port_type = MEDIACODEC_PORT_TYPE_GST;
986 if (strcmp(ini->codec[indx].codec_info[3].name, "")) {
987 spec_emul[count].codec_id = ini->codec[indx].codec_id;
988 spec_emul[count].codec_type = MEDIACODEC_ENCODER | MEDIACODEC_SUPPORT_TYPE_SW;
989 spec_emul[count].port_type = MEDIACODEC_PORT_TYPE_GST;
994 ini->num_supported_codecs = count;
995 LOGE("supported codecs :%d", count);
999 codec_type_e codec_type_to_simple_enumeration(mediacodec_codec_type_e media_codec_id)
1001 guint media_codec_id_u = (guint)media_codec_id;
1003 switch (media_codec_id_u) {
1004 case MEDIACODEC_L16:
1006 case MEDIACODEC_ALAW:
1008 case MEDIACODEC_ULAW:
1010 case MEDIACODEC_AMR_NB:
1012 case MEDIACODEC_AMR_WB:
1014 case MEDIACODEC_G729:
1016 case MEDIACODEC_AAC_LC:
1018 case MEDIACODEC_AAC_HE:
1020 case MEDIACODEC_AAC_HE_PS:
1022 case MEDIACODEC_MP3:
1024 case MEDIACODEC_VORBIS:
1026 case MEDIACODEC_FLAC:
1028 case MEDIACODEC_WMAV1:
1030 case MEDIACODEC_WMAV2:
1032 case MEDIACODEC_WMAPRO:
1034 case MEDIACODEC_WMALSL:
1036 case MEDIACODEC_H261:
1038 case MEDIACODEC_H263:
1040 case MEDIACODEC_H264:
1042 case MEDIACODEC_MJPEG:
1044 case MEDIACODEC_MPEG1:
1046 case MEDIACODEC_MPEG2:
1048 case MEDIACODEC_MPEG4:
1050 case MEDIACODEC_HEVC:
1052 case MEDIACODEC_VP8:
1054 case MEDIACODEC_VP9:
1056 case MEDIACODEC_VC1:
1058 case MEDIACODEC_OPUS:
1065 mediacodec_codec_type_e simple_to_codec_type_enumeration(codec_type_e codec_id)
1067 guint codec_id_u = (guint)codec_id;
1069 switch (codec_id_u) {
1071 return MEDIACODEC_L16;
1073 return MEDIACODEC_ALAW;
1075 return MEDIACODEC_ULAW;
1077 return MEDIACODEC_AMR_NB;
1079 return MEDIACODEC_AMR_WB;
1081 return MEDIACODEC_G729;
1083 return MEDIACODEC_AAC_LC;
1085 return MEDIACODEC_AAC_HE;
1087 return MEDIACODEC_AAC_HE_PS;
1089 return MEDIACODEC_MP3;
1091 return MEDIACODEC_VORBIS;
1093 return MEDIACODEC_FLAC;
1095 return MEDIACODEC_WMAV1;
1097 return MEDIACODEC_WMAV2;
1099 return MEDIACODEC_WMAPRO;
1101 return MEDIACODEC_WMALSL;
1103 return MEDIACODEC_H261;
1105 return MEDIACODEC_H263;
1107 return MEDIACODEC_H264;
1109 return MEDIACODEC_MJPEG;
1111 return MEDIACODEC_MPEG1;
1113 return MEDIACODEC_MPEG2;
1115 return MEDIACODEC_MPEG4;
1117 return MEDIACODEC_HEVC;
1119 return MEDIACODEC_VP8;
1121 return MEDIACODEC_VP9;
1123 return MEDIACODEC_VC1;
1125 return MEDIACODEC_OPUS;
1131 gboolean _check_support_audio_info(mediacodec_codec_type_e codec_id, int samplerate, int channel, int bit_depth)
1134 gint maxchannels = 2;
1136 gint s_bit_depth = 32;
1139 case MEDIACODEC_AMR_NB:
1141 const static gint l_rates[] = { 8000 };
1143 n_rates = G_N_ELEMENTS(l_rates);
1144 s_bit_depth = 16; /* NOTE: amrnbenc/amrnbdec surpports S16LE as format*/
1146 while (i < n_rates) {
1147 if (l_rates[i] == samplerate)
1153 LOGE("Invalid samplerate set");
1158 case MEDIACODEC_AAC_LC:
1159 case MEDIACODEC_AAC_HE:
1161 const static gint l_rates[] = { 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, 7350 };
1163 n_rates = G_N_ELEMENTS(l_rates);
1164 s_bit_depth = 32; /* NOTE: avdec_aac/avenc_aac surpports S32LE as format*/
1166 while (i < n_rates) {
1167 if (l_rates[i] == samplerate)
1173 LOGE("Invalid samplerate set");
1178 case MEDIACODEC_MP3:
1180 s_bit_depth = 16; /* NOTE: amrenc/amrnbdec surpports S16LE as format*/
1183 case MEDIACODEC_VORBIS:
1185 s_bit_depth = 32; /* NOTE: vorbisenc/vorbisdec surpports S32LE as format */
1188 case MEDIACODEC_FLAC:
1190 s_bit_depth = 32; /* NOTE: avdec_flac surpports S32LE as format */
1193 case MEDIACODEC_OPUS:
1195 s_bit_depth = 16; /* NOTE: opusenc/ opusdec support S16LE as format according to opus specification*/
1202 if (channel < 0 || channel > maxchannels) {
1203 LOGE("Invalid channel set");
1207 if (bit_depth != s_bit_depth) {
1208 LOGE("Invalid bit set");
1215 gboolean _check_support_video_info(mediacodec_codec_type_e codec_id, int width, int height)
1220 if (width <= 0 || height <= 0) {
1221 LOGE("Invalid resolution set");
1226 case MEDIACODEC_H261:
1228 const static gint widths[] = { 352, 176 };
1229 const static gint heights[] = { 288, 144 };
1230 n_sizes = G_N_ELEMENTS(widths);
1232 while (i < n_sizes) {
1233 if ((widths[i] == width) && (heights[i] == height))
1239 LOGE("Invalid resolution set");
1244 case MEDIACODEC_H263:
1246 const static gint widths[] = { 352, 704, 176, 1408, 128 };
1247 const static gint heights[] = { 288, 576, 144, 1152, 96 };
1248 n_sizes = G_N_ELEMENTS(widths);
1250 while (i < n_sizes) {
1251 if ((widths[i] == width) && (heights[i] == height))
1257 LOGE("Invalid resolution set");