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.
20 #include <media_codec.h>
21 #include <media_codec_private.h>
22 #include <media_codec_port.h>
26 static gboolean __mediacodec_empty_buffer_cb(media_packet_h pkt, void *user_data);
27 static gboolean __mediacodec_fill_buffer_cb(media_packet_h pkt, void *user_data);
28 static gboolean __mediacodec_error_cb(mediacodec_error_e error, void *user_data);
29 static gboolean __mediacodec_eos_cb(void *user_data);
32 * Internal Implementation
34 int __convert_error_code(int code, char* func_name)
36 int ret = MEDIACODEC_ERROR_INVALID_OPERATION;
37 char* msg = "MEDIACOODEC_INVALID_OPERATION";
41 ret = MEDIACODEC_ERROR_NONE;
42 msg = "MEDIACODEC_ERROR_NONE";
46 ret = MEDIACODEC_ERROR_INVALID_PARAMETER;
47 msg = "MEDIACODEC_ERROR_INVALID_PARAMETER";
49 case MC_PERMISSION_DENIED:
50 ret = MEDIACODEC_ERROR_PERMISSION_DENIED;
51 msg = "MEDIACODEC_ERROR_PERMISSION_DENIED";
53 case MC_INVALID_STATUS:
54 ret = MEDIACODEC_ERROR_INVALID_STATE;
55 msg = "MEDIACODEC_ERROR_INVALID_STATE";
57 case MC_NOT_SUPPORTED:
58 ret = MEDIACODEC_ERROR_NOT_SUPPORTED_ON_DEVICE;
59 msg = "MEDIACODEC_ERROR_NOT_SUPPORTED_ON_DEVICE";
62 case MC_INTERNAL_ERROR:
64 ret = MEDIACODEC_ERROR_INVALID_OPERATION;
65 msg = "MEDIACODEC_ERROR_INVALID_OPERATION";
67 case MC_INVALID_STREAM:
68 ret = MEDIACODEC_ERROR_INVALID_STREAM;
69 msg = "MEDIACODEC_ERROR_INVALID_STREAM";
71 case MC_CODEC_NOT_FOUND:
72 ret = MEDIACODEC_ERROR_CODEC_NOT_FOUND;
73 msg = "MEDIACODEC_ERROR_CODEC_NOT_FOUND";
76 ret = MEDIACODEC_ERROR_DECODE;
77 msg = "MEDIACODEC_ERROR_DECODE";
79 case MC_INVALID_IN_BUF:
80 ret = MEDIACODEC_ERROR_INVALID_INBUFFER;
81 msg = "MEDIACODEC_ERROR_INVALID_INBUFFER";
83 case MC_INVALID_OUT_BUF:
84 ret = MEDIACODEC_ERROR_INVALID_OUTBUFFER;
85 msg = "MEDIACODEC_ERROR_INVALID_OUTBUFFER";
87 case MC_NOT_INITIALIZED:
88 ret = MEDIACODEC_ERROR_NOT_INITIALIZED;
89 msg = "MEDIACODEC_ERROR_NOT_INITIALIZED";
91 case MC_OUTPUT_BUFFER_EMPTY:
92 case MC_OUTPUT_BUFFER_OVERFLOW:
93 ret = MEDIACODEC_ERROR_BUFFER_NOT_AVAILABLE;
94 msg = "MEDIACODEC_ERROR_BUFFER_NOT_AVAILABLE";
97 ret = MEDIACODEC_ERROR_INTERNAL;
98 msg = "MEDIACODEC_ERROR_INTERNAL";
101 LOGD("[%s] %s(0x%08x) : core fw error(0x%x)", func_name, msg, ret, code);
105 bool __mediacodec_state_validate(mediacodec_h mediacodec, mediacodec_state_e threshold)
107 mediacodec_s * handle = (mediacodec_s *) mediacodec;
109 if(handle->state < threshold)
115 int mediacodec_create(mediacodec_h *mediacodec)
117 MEDIACODEC_INSTANCE_CHECK(mediacodec);
118 mediacodec_s *handle;
121 LOGD ("mediacodec_create..\n");
123 handle = (mediacodec_s*)malloc( sizeof(mediacodec_s));
126 memset(handle, 0 , sizeof(mediacodec_s));
130 LOGE("MEDIACODEC_ERROR_OUT_OF_MEMORY(0x%08x)", MEDIACODEC_ERROR_OUT_OF_MEMORY);
131 return MEDIACODEC_ERROR_OUT_OF_MEMORY;
134 ret = mc_create(&handle->mc_handle);
135 if (ret != MEDIACODEC_ERROR_NONE)
137 LOGE("MEDIACODEC_ERROR_INVALID_OPERATION(0x%08x)", MEDIACODEC_ERROR_INVALID_OPERATION);
138 handle->state = MEDIACODEC_STATE_NONE;
141 return MEDIACODEC_ERROR_INVALID_OPERATION;
145 *mediacodec = (mediacodec_h) handle;
146 handle->state = MEDIACODEC_STATE_IDLE;
147 LOGD("new handle : %p", *mediacodec);
151 mc_set_empty_buffer_cb(handle->mc_handle, (mediacodec_input_buffer_used_cb)__mediacodec_empty_buffer_cb, handle);
152 mc_set_fill_buffer_cb(handle->mc_handle, (mediacodec_output_buffer_available_cb)__mediacodec_fill_buffer_cb, handle);
153 mc_set_error_cb(handle->mc_handle, (mediacodec_error_cb)__mediacodec_error_cb, handle);
154 mc_set_eos_cb(handle->mc_handle, (mediacodec_eos_cb)__mediacodec_eos_cb, handle);
156 return MEDIACODEC_ERROR_NONE;
160 int mediacodec_destroy(mediacodec_h mediacodec)
162 LOGD ("[%s] Start, handle to destroy : %p", __FUNCTION__, mediacodec);
163 MEDIACODEC_INSTANCE_CHECK(mediacodec);
164 mediacodec_s * handle = (mediacodec_s *) mediacodec;
166 int ret = mc_destroy(handle->mc_handle);
167 if (ret != MEDIACODEC_ERROR_NONE) {
168 LOGD("MEDIACODEC_ERROR_INVALID_OPERATION(0x%08x)", MEDIACODEC_ERROR_INVALID_OPERATION);
169 return MEDIACODEC_ERROR_INVALID_OPERATION;
173 handle->state = MEDIACODEC_STATE_NONE;
176 return MEDIACODEC_ERROR_NONE;
180 int mediacodec_set_codec(mediacodec_h mediacodec, mediacodec_codec_type_e codec_id, mediacodec_support_type_e flags)
182 MEDIACODEC_INSTANCE_CHECK(mediacodec);
183 mediacodec_s * handle = (mediacodec_s *) mediacodec;
184 MEDIACODEC_STATE_CHECK(handle, MEDIACODEC_STATE_IDLE);
186 int ret = mc_set_codec(handle->mc_handle, codec_id, flags);
188 if (ret != MEDIACODEC_ERROR_NONE)
190 return __convert_error_code(ret,(char*)__FUNCTION__);
194 handle->state = MEDIACODEC_STATE_IDLE;
195 return MEDIACODEC_ERROR_NONE;
199 int mediacodec_set_vdec_info(mediacodec_h mediacodec, int width, int height)
201 MEDIACODEC_INSTANCE_CHECK(mediacodec);
202 mediacodec_s * handle = (mediacodec_s *) mediacodec;
203 MEDIACODEC_STATE_CHECK(handle, MEDIACODEC_STATE_IDLE);
205 int ret = mc_set_vdec_info(handle->mc_handle, width, height);
207 if (ret != MEDIACODEC_ERROR_NONE)
209 return __convert_error_code(ret,(char*)__FUNCTION__);
213 handle->state = MEDIACODEC_STATE_IDLE;
214 return MEDIACODEC_ERROR_NONE;
218 int mediacodec_set_venc_info(mediacodec_h mediacodec, int width, int height, int fps, int target_bits)
220 MEDIACODEC_INSTANCE_CHECK(mediacodec);
221 mediacodec_s * handle = (mediacodec_s *) mediacodec;
222 MEDIACODEC_STATE_CHECK(handle, MEDIACODEC_STATE_IDLE);
224 int ret = mc_set_venc_info(handle->mc_handle, width, height, fps, target_bits);
226 if (ret != MEDIACODEC_ERROR_NONE)
228 return __convert_error_code(ret,(char*)__FUNCTION__);
232 handle->state = MEDIACODEC_STATE_IDLE;
233 return MEDIACODEC_ERROR_NONE;
237 int mediacodec_set_adec_info(mediacodec_h mediacodec, int samplerate, int channel, int bit)
239 MEDIACODEC_INSTANCE_CHECK(mediacodec);
240 mediacodec_s * handle = (mediacodec_s *) mediacodec;
241 MEDIACODEC_STATE_CHECK(handle, MEDIACODEC_STATE_IDLE);
243 int ret = mc_set_adec_info(handle->mc_handle, samplerate, channel, bit);
245 if (ret != MEDIACODEC_ERROR_NONE)
247 return __convert_error_code(ret,(char*)__FUNCTION__);
251 handle->state = MEDIACODEC_STATE_IDLE;
252 return MEDIACODEC_ERROR_NONE;
256 int mediacodec_set_aenc_info(mediacodec_h mediacodec, int samplerate, int channel, int bit, int bitrate)
258 MEDIACODEC_INSTANCE_CHECK(mediacodec);
259 mediacodec_s * handle = (mediacodec_s *) mediacodec;
260 MEDIACODEC_STATE_CHECK(handle, MEDIACODEC_STATE_IDLE);
262 int ret = mc_set_aenc_info(handle->mc_handle, samplerate, channel, bit, bitrate);
264 if (ret != MEDIACODEC_ERROR_NONE)
266 return __convert_error_code(ret,(char*)__FUNCTION__);
270 handle->state = MEDIACODEC_STATE_IDLE;
271 return MEDIACODEC_ERROR_NONE;
275 int mediacodec_prepare(mediacodec_h mediacodec)
277 MEDIACODEC_INSTANCE_CHECK(mediacodec);
278 mediacodec_s * handle = (mediacodec_s *) mediacodec;
279 MEDIACODEC_STATE_CHECK(handle, MEDIACODEC_STATE_IDLE);
281 int ret = mc_prepare(handle->mc_handle);
283 if (ret != MEDIACODEC_ERROR_NONE)
285 return __convert_error_code(ret,(char*)__FUNCTION__);
289 handle->state = MEDIACODEC_STATE_READY;
290 return MEDIACODEC_ERROR_NONE;
294 int mediacodec_unprepare(mediacodec_h mediacodec)
296 MEDIACODEC_INSTANCE_CHECK(mediacodec);
297 mediacodec_s * handle = (mediacodec_s *) mediacodec;
299 int ret = mc_unprepare(handle->mc_handle);
301 if (ret != MEDIACODEC_ERROR_NONE)
303 return __convert_error_code(ret,(char*)__FUNCTION__);
307 handle->state = MEDIACODEC_STATE_IDLE;
308 return MEDIACODEC_ERROR_NONE;
312 int mediacodec_process_input(mediacodec_h mediacodec, media_packet_h inbuf, uint64_t timeOutUs)
314 MEDIACODEC_INSTANCE_CHECK(mediacodec);
315 mediacodec_s * handle = (mediacodec_s *) mediacodec;
316 MEDIACODEC_STATE_CHECK(handle, MEDIACODEC_STATE_READY);
318 int ret = mc_process_input(handle->mc_handle, inbuf, timeOutUs);
320 if (ret != MEDIACODEC_ERROR_NONE)
322 return __convert_error_code(ret,(char*)__FUNCTION__);
326 //handle->state = MEDIACODEC_STATE_EXCUTE;
327 return MEDIACODEC_ERROR_NONE;
331 int mediacodec_get_output(mediacodec_h mediacodec, media_packet_h *outbuf, uint64_t timeOutUs)
333 MEDIACODEC_INSTANCE_CHECK(mediacodec);
334 mediacodec_s * handle = (mediacodec_s *) mediacodec;
335 MEDIACODEC_STATE_CHECK(handle, MEDIACODEC_STATE_READY);
337 int ret = mc_get_output(handle->mc_handle, outbuf, timeOutUs);
339 if (ret != MEDIACODEC_ERROR_NONE)
341 return __convert_error_code(ret,(char*)__FUNCTION__);
345 //handle->state = MEDIACODEC_STATE_EXCUTE;
346 return MEDIACODEC_ERROR_NONE;
350 int mediacodec_set_input_buffer_used_cb(mediacodec_h mediacodec, mediacodec_input_buffer_used_cb callback, void* user_data)
352 MEDIACODEC_INSTANCE_CHECK(mediacodec);
353 mediacodec_s * handle = (mediacodec_s *) mediacodec;
354 MEDIACODEC_STATE_CHECK(handle, MEDIACODEC_STATE_IDLE);
356 handle->empty_buffer_cb = callback;
357 handle->empty_buffer_cb_userdata = user_data;
359 LOGD("set empty_buffer_cb(%p)", callback);
361 return MEDIACODEC_ERROR_NONE;
364 int mediacodec_unset_input_buffer_used_cb(mediacodec_h mediacodec)
366 MEDIACODEC_INSTANCE_CHECK(mediacodec);
367 mediacodec_s * handle = (mediacodec_s *) mediacodec;
369 handle->empty_buffer_cb = NULL;
370 handle->empty_buffer_cb_userdata = NULL;
372 return MEDIACODEC_ERROR_NONE;
376 int mediacodec_set_output_buffer_available_cb(mediacodec_h mediacodec, mediacodec_output_buffer_available_cb callback, void* user_data)
378 MEDIACODEC_INSTANCE_CHECK(mediacodec);
379 mediacodec_s * handle = (mediacodec_s *) mediacodec;
380 MEDIACODEC_STATE_CHECK(handle, MEDIACODEC_STATE_IDLE);
382 handle->fill_buffer_cb = callback;
383 handle->fill_buffer_cb_userdata = user_data;
385 LOGD("set fill_buffer_cb(%p)", callback);
387 return MEDIACODEC_ERROR_NONE;
391 int mediacodec_unset_output_buffer_available_cb(mediacodec_h mediacodec)
393 MEDIACODEC_INSTANCE_CHECK(mediacodec);
394 mediacodec_s * handle = (mediacodec_s *) mediacodec;
396 handle->fill_buffer_cb = NULL;
397 handle->fill_buffer_cb_userdata = NULL;
399 return MEDIACODEC_ERROR_NONE;
402 int mediacodec_set_error_cb(mediacodec_h mediacodec, mediacodec_error_cb callback, void* user_data)
404 MEDIACODEC_INSTANCE_CHECK(mediacodec);
405 mediacodec_s * handle = (mediacodec_s *) mediacodec;
406 MEDIACODEC_STATE_CHECK(handle, MEDIACODEC_STATE_IDLE);
408 handle->error_cb = callback;
409 handle->error_cb_userdata = user_data;
411 LOGD("set error_cb(%p)", callback);
413 return MEDIACODEC_ERROR_NONE;
417 int mediacodec_unset_error_cb(mediacodec_h mediacodec)
419 MEDIACODEC_INSTANCE_CHECK(mediacodec);
420 mediacodec_s * handle = (mediacodec_s *) mediacodec;
422 handle->error_cb = NULL;
423 handle->error_cb_userdata = NULL;
425 return MEDIACODEC_ERROR_NONE;
428 int mediacodec_set_eos_cb(mediacodec_h mediacodec, mediacodec_eos_cb callback, void* user_data)
430 MEDIACODEC_INSTANCE_CHECK(mediacodec);
431 mediacodec_s * handle = (mediacodec_s *) mediacodec;
432 MEDIACODEC_STATE_CHECK(handle, MEDIACODEC_STATE_IDLE);
434 handle->eos_cb = callback;
435 handle->eos_cb_userdata = user_data;
437 LOGD("set eos_cb(%p)", callback);
439 return MEDIACODEC_ERROR_NONE;
443 int mediacodec_unset_eos_cb(mediacodec_h mediacodec)
445 MEDIACODEC_INSTANCE_CHECK(mediacodec);
446 mediacodec_s * handle = (mediacodec_s *) mediacodec;
448 handle->eos_cb = NULL;
449 handle->eos_cb_userdata = NULL;
451 return MEDIACODEC_ERROR_NONE;
454 static gboolean __mediacodec_empty_buffer_cb(media_packet_h pkt, void *user_data)
456 if(user_data == NULL || pkt == NULL)
459 mediacodec_s * handle = (mediacodec_s *) user_data;
461 if ( handle->empty_buffer_cb )
463 ((mediacodec_input_buffer_used_cb)handle->empty_buffer_cb)(pkt, handle->empty_buffer_cb_userdata);
469 static gboolean __mediacodec_fill_buffer_cb(media_packet_h pkt, void *user_data)
471 if(user_data == NULL || pkt == NULL)
474 mediacodec_s * handle = (mediacodec_s *) user_data;
476 if ( handle->fill_buffer_cb )
478 ((mediacodec_output_buffer_available_cb)handle->fill_buffer_cb)(pkt, handle->fill_buffer_cb_userdata);
484 static gboolean __mediacodec_error_cb(mediacodec_error_e error, void *user_data)
486 if(user_data == NULL)
489 mediacodec_s * handle = (mediacodec_s *) user_data;
491 if ( handle->error_cb )
493 ((mediacodec_error_cb)handle->error_cb)(error, handle->error_cb_userdata);
499 static gboolean __mediacodec_eos_cb(void *user_data)
501 if(user_data == NULL)
504 mediacodec_s * handle = (mediacodec_s *) user_data;
506 if ( handle->eos_cb )
508 ((mediacodec_eos_cb)handle->eos_cb)(handle->eos_cb_userdata);