removed dependency of appcore-efl
[platform/core/api/mediacodec.git] / src / media_codec_ini.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #ifndef __MEDIA_CODEC_INI_C__
18 #define __MEDIA_CODEC_INI_C__
19
20 /* includes here */
21 #include <glib.h>
22 #include <stdlib.h>
23 #include <glib/gstdio.h>
24 #include <mm_debug.h>
25 #include <mm_error.h>
26 #include <iniparser.h>
27 #include <media_codec_ini.h>
28 #include <media_codec_port.h>
29
30 #define DEFAULT_VALUE ""
31
32 #define DEFAULT_HW_DECODER_NAME ""
33 #define DEFAULT_HW_DECODER_MIME ""
34 #define DEFAULT_HW_DECODER_FORMAT ""
35
36 #define DEFAULT_HW_ENCODER_NAME ""
37 #define DEFAULT_HW_ENCODER_MIME ""
38 #define DEFAULT_HW_ENCODER_FORMAT ""
39
40 #define DEFAULT_SW_DECODER_NAME ""
41 #define DEFAULT_SW_DECODER_MIME ""
42 #define DEFAULT_SW_DECODER_FORMAT ""
43
44 #define DEFAULT_SW_ENCODER_NAME ""
45 #define DEFAULT_SW_ENCODER_MIME ""
46 #define DEFAULT_SW_ENCODER_FORMAT ""
47
48
49 #define CNAME_SIZE 512
50
51 static codec_list_t general_codec_list[] = {
52         {"h261", MEDIACODEC_H261},
53         {"h263", MEDIACODEC_H263},
54         {"h264", MEDIACODEC_H264},
55         {"mjpeg", MEDIACODEC_MJPEG},
56         {"mpeg1", MEDIACODEC_MPEG1},
57         {"mpeg2", MEDIACODEC_MPEG2},
58         {"mpeg4", MEDIACODEC_MPEG4},
59         {"hevc", MEDIACODEC_HEVC},
60         {"vp8", MEDIACODEC_VP8},
61         {"vp9", MEDIACODEC_VP9},
62         {"vc1", MEDIACODEC_VC1},
63         {"aac_lc", MEDIACODEC_AAC_LC},
64         {"aac_he", MEDIACODEC_AAC_HE},
65         {"aac_he_ps", MEDIACODEC_AAC_HE_PS},
66         {"mp3", MEDIACODEC_MP3},
67         {"amr_nb", MEDIACODEC_AMR_NB},
68         {"amr_wb", MEDIACODEC_AMR_WB},
69         {"vorbis", MEDIACODEC_VORBIS},
70         {"flac", MEDIACODEC_FLAC},
71         {"wmav1", MEDIACODEC_WMAV1},
72         {"wmav2", MEDIACODEC_WMAV2},
73         {"wmapro", MEDIACODEC_WMAPRO},
74 };
75
76 /* internal functions, macros here */
77 #ifdef MEDIA_CODEC_DEFAULT_INI
78 static gboolean _generate_default_ini(void);
79 #endif
80
81 static void _mc_ini_check_ini_status(void);
82
83 /* macro */
84 #define MEDIA_CODEC_INI_GET_STRING(x_dict, x_item, x_ini, x_default) \
85 do {\
86         gchar* str = iniparser_getstring(x_dict, x_ini, x_default); \
87         \
88         if (str &&  \
89                         (strlen(str) > 0) && \
90                         (strlen(str) < MEDIA_CODEC_INI_MAX_STRLEN)) \
91                 strncpy(x_item, str, strlen(str) + 1); \
92         else \
93                 strncpy(x_item, x_default, strlen(x_default) + 1); \
94 } while (0)
95
96 #define MEDIA_CODEC_INI_GET_STRING_FROM_LIST(x_dict, x_list, x_ini, x_default) \
97 do {\
98         char *token = NULL; \
99         char *usr_ptr = NULL; \
100         int index = 0; \
101         const char *delimiters = " ,"; \
102         gchar temp_arr[MEDIA_CODEC_INI_MAX_STRLEN] = {0}; \
103         MEDIA_CODEC_INI_GET_STRING(x_dict, temp_arr, x_ini, x_default); \
104         token = strtok_r(temp_arr, delimiters, &usr_ptr); \
105         while (token) {\
106                 if (index == 0) \
107                 strncpy(x_list.name, token, MEDIA_CODEC_INI_MAX_STRLEN-1);\
108                 else if (index == 1) \
109                 strncpy(x_list.mime, token, MEDIA_CODEC_INI_MAX_STRLEN-1);\
110                 else if (index == 2) \
111                 strncpy(x_list.format, token, MEDIA_CODEC_INI_MAX_STRLEN-1);\
112                 index++;\
113                 token = strtok_r(NULL, delimiters, &usr_ptr); \
114         } \
115 } while (0)
116
117 #define MEDIA_CODEC_INI_GET_COLOR(x_dict, x_item, x_ini, x_default) \
118 do {\
119         gchar* str = iniparser_getstring(x_dict, x_ini, x_default); \
120         \
121         if (str &&  \
122                         (strlen(str) > 0) && \
123                         (strlen(str) < MEDIA_CODEC_INI_MAX_STRLEN)) \
124                 x_item = (guint) strtoul(str, NULL, 16); \
125         else \
126                 x_item = (guint) strtoul(x_default, NULL, 16); \
127 } while (0)
128
129 /* x_ini is the list of index to set TRUE at x_list[index] */
130 #define MEDIA_CODEC_INI_GET_BOOLEAN_FROM_LIST(x_dict, x_list, x_list_max, x_ini, x_default) \
131 do {\
132         int index = 0; \
133         const char *delimiters = " ,"; \
134         char *usr_ptr = NULL; \
135         char *token = NULL; \
136         gchar temp_arr[MEDIA_CODEC_INI_MAX_STRLEN] = {0}; \
137         MEDIA_CODEC_INI_GET_STRING(x_dict, temp_arr, x_ini, x_default); \
138         token = strtok_r(temp_arr, delimiters, &usr_ptr); \
139         while (token) {\
140                 index = atoi(token); \
141                 if (index < 0 || index > x_list_max -1) \
142                         LOGW("%d is not valid index\n", index); \
143                 else \
144                         x_list[index] = TRUE; \
145                 token = strtok_r(NULL, delimiters, &usr_ptr); \
146         } \
147 } while (0)
148
149 /* x_ini is the list of value to be set at x_list[index] */
150 #define MEDIA_CODEC_INI_GET_INT_FROM_LIST(x_dict, x_list, x_list_max, x_ini, x_default) \
151 do {\
152         int index = 0; \
153         int value = 0; \
154         const char *delimiters = " ,"; \
155         char *usr_ptr = NULL; \
156         char *token = NULL; \
157         gchar temp_arr[MEDIA_CODEC_INI_MAX_STRLEN] = {0}; \
158         MEDIA_CODEC_INI_GET_STRING(x_dict, temp_arr, x_ini, x_default); \
159         token = strtok_r(temp_arr, delimiters, &usr_ptr); \
160         while (token) {\
161                 if (index > x_list_max -1) {\
162                         LOGE("%d is not valid index\n", index); \
163                         break; \
164                 } \
165                 else {\
166                         value = atoi(token); \
167                         x_list[index] = value; \
168                         index++; \
169                 } \
170                 token = strtok_r(NULL, delimiters, &usr_ptr); \
171         } \
172 } while (0)
173
174 #define MEDIA_CODEC_GET_DEFAULT_LIST(x_list, x_default) \
175 do {\
176         strncpy(x_list, x_default, MEDIA_CODEC_INI_MAX_STRLEN - 1);\
177 } while (0)
178 #define MEDIA_CODEC_PRINT_LIST(x_list,  x_message) \
179 do {\
180         codec_info_t codec_list = x_list;\
181         LOGW("%s =", x_message);\
182         LOGW("%s %s %s\n", codec_list.name, codec_list.mime, codec_list.format);\
183 } while (0)
184
185 media_format_mimetype_e _mc_convert_media_format_str_to_int(char *sformat)
186 {
187
188         media_format_mimetype_e iformat = MEDIA_FORMAT_I420;
189         if (!strcmp(sformat, "I420")) {
190                 iformat = MEDIA_FORMAT_I420;
191                 goto endf;
192         } else if (!strcmp(sformat, "NV12")) {
193                 iformat = MEDIA_FORMAT_NV12;
194                 goto endf;
195         } else if (!strcmp(sformat, "NV12T")) {
196                 iformat = MEDIA_FORMAT_NV12T;
197                 goto endf;
198         } else if (!strcmp(sformat, "YV12")) {
199                 iformat = MEDIA_FORMAT_YV12;
200                 goto endf;
201         } else if (!strcmp(sformat, "NV21")) {
202                 iformat = MEDIA_FORMAT_NV21;
203                 goto endf;
204         } else if (!strcmp(sformat, "NV16")) {
205                 iformat = MEDIA_FORMAT_NV16;
206         } else if (!strcmp(sformat, "YUYV")) {
207                 iformat = MEDIA_FORMAT_YUYV;
208                 goto endf;
209         } else if (!strcmp(sformat, "UYVY")) {
210                 iformat = MEDIA_FORMAT_UYVY;
211                 goto endf;
212         } else if (!strcmp(sformat, "422P")) {
213                 iformat = MEDIA_FORMAT_422P;
214                 goto endf;
215         } else if (!strcmp(sformat, "RGB565")) {
216                 iformat = MEDIA_FORMAT_RGB565;
217                 goto endf;
218         } else if (!strcmp(sformat, "RGB888")) {
219                 iformat = MEDIA_FORMAT_RGB888;
220                 goto endf;
221         } else if (!strcmp(sformat, "RGBA")) {
222                 iformat = MEDIA_FORMAT_RGBA;
223                 goto endf;
224         } else if (!strcmp(sformat, "ARGB")) {
225                 iformat = MEDIA_FORMAT_ARGB;
226                 goto endf;
227         } else if (!strcmp(sformat, "PCM")) {
228                 iformat = MEDIA_FORMAT_PCM;
229                 goto endf;
230         } else if (!strcmp(sformat, "PCM_F32LE")) {
231                 iformat = MEDIA_FORMAT_PCM_F32LE;
232                 goto endf;
233         } else if (!strcmp(sformat, "H261")) {
234                 iformat = MEDIA_FORMAT_H261;
235                 goto endf;
236         } else if (!strcmp(sformat, "H263")) {
237                 iformat = MEDIA_FORMAT_H263;
238                 goto endf;
239         } else if (!strcmp(sformat, "H263P")) {
240                 iformat = MEDIA_FORMAT_H263P;
241                 goto endf;
242         } else if (!strcmp(sformat, "H264_SP")) {
243                 iformat = MEDIA_FORMAT_H264_SP;
244                 goto endf;
245         } else if (!strcmp(sformat, "H264_MP")) {
246                 iformat = MEDIA_FORMAT_H264_MP;
247                 goto endf;
248         } else if (!strcmp(sformat, "H264_HP")) {
249                 iformat = MEDIA_FORMAT_H264_HP;
250                 goto endf;
251         } else if (!strcmp(sformat, "MPEG4_SP")) {
252                 iformat = MEDIA_FORMAT_MPEG4_SP;
253                 goto endf;
254         } else if (!strcmp(sformat, "MPEG4_ASP")) {
255                 iformat = MEDIA_FORMAT_MPEG4_ASP;
256                 goto endf;
257         } else if (!strcmp(sformat, "AMR_NB")) {
258                 iformat = MEDIA_FORMAT_AMR_NB;
259                 goto endf;
260         } else if (!strcmp(sformat, "AMR_WB")) {
261                 iformat = MEDIA_FORMAT_AMR_WB;
262                 goto endf;
263         } else if (!strcmp(sformat, "AAC_LC")) {
264                 iformat = MEDIA_FORMAT_AAC_LC;
265                 goto endf;
266         } else if (!strcmp(sformat, "AAC_HE")) {
267                 iformat = MEDIA_FORMAT_AAC_HE;
268                 goto endf;
269         } else if (!strcmp(sformat, "AAC_HE_PS")) {
270                 iformat = MEDIA_FORMAT_AAC_HE_PS;
271                 goto endf;
272         } else if (!strcmp(sformat, "MP3")) {
273                 iformat = MEDIA_FORMAT_MP3;
274                 goto endf;
275         } else if (!strcmp(sformat, "VORBIS")) {
276                 iformat = MEDIA_FORMAT_VORBIS;
277                 goto endf;
278         } else if (!strcmp(sformat, "FLAC")) {
279                 iformat = MEDIA_FORMAT_FLAC;
280                 goto endf;
281         } else if (!strcmp(sformat, "WMAV1")) {
282                 iformat = MEDIA_FORMAT_WMAV1;
283                 goto endf;
284         } else if (!strcmp(sformat, "WMAV2")) {
285                 iformat = MEDIA_FORMAT_WMAV2;
286                 goto endf;
287         } else if (!strcmp(sformat, "WMAPRO")) {
288                 iformat = MEDIA_FORMAT_WMAPRO;
289                 goto endf;
290         }
291
292 endf:
293         LOGD("sformat : %x", iformat);
294         return iformat;
295 }
296
297 int mc_ini_load(mc_ini_t *ini)
298 {
299         gchar cname[CNAME_SIZE];
300         int i = 0;
301         dictionary *dict = NULL;
302
303         /* get ini values */
304         memset(ini, 0, sizeof(mc_ini_t));
305
306         ini->codec_list = sizeof(general_codec_list) / sizeof(general_codec_list[0]);
307
308         _mc_ini_check_ini_status();
309
310         /* first, try to load existing ini file */
311         dict = iniparser_load(MEDIA_CODEC_INI_DEFAULT_PATH);
312
313         /* if no file exists. create one with set of default values */
314         if (!dict) {
315 #ifdef MEDIA_CODEC_DEFAULT_INI
316                 LOGD("No inifile found. codec will create default inifile.\n");
317                 if (FALSE == _generate_default_ini()) {
318                         LOGW("Creating default inifile failed. Media Codec will use default values.\n");
319                 } else {
320                         /* load default ini */
321                         dict = iniparser_load(MEDIA_CODEC_INI_DEFAULT_PATH);
322                 }
323 #else
324                 LOGD("No ini file found. \n");
325                 return LOGERROR_FILE_NOT_FOUND;
326 #endif
327         }
328
329
330         if (dict) {/* if dict is available */
331                 /* general */
332                 MEDIA_CODEC_INI_GET_STRING(dict, ini->port_name, "port_in_use:media_codec_port", DEFAULT_PORT);
333                 /* codec */
334                 for (i = 0; i < ini->codec_list; i++) {
335                         memset(cname, 0x00, CNAME_SIZE);
336                         snprintf(cname, CNAME_SIZE, "%s", general_codec_list[i].cname);
337                         int len = strlen(cname);
338                         ini->codec[i].codec_id =  general_codec_list[i].ctype;
339                         snprintf(cname+len, CNAME_SIZE - len, "%s", ":hw_decoder");
340                         MEDIA_CODEC_INI_GET_STRING_FROM_LIST(dict, ini->codec[i].codec_info[0], cname, DEFAULT_VALUE);
341                         snprintf(cname+len, CNAME_SIZE - len, "%s", ":hw_encoder");
342                         MEDIA_CODEC_INI_GET_STRING_FROM_LIST(dict, ini->codec[i].codec_info[1], cname, DEFAULT_VALUE);
343                         snprintf(cname+len, CNAME_SIZE - len, "%s", ":sw_decoder");
344                         MEDIA_CODEC_INI_GET_STRING_FROM_LIST(dict, ini->codec[i].codec_info[2], cname, DEFAULT_VALUE);
345                         snprintf(cname+len, CNAME_SIZE - len, "%s", ":sw_encoder");
346                         MEDIA_CODEC_INI_GET_STRING_FROM_LIST(dict, ini->codec[i].codec_info[3], cname, DEFAULT_VALUE);
347                 }
348         } else {/* if dict is not available just fill the structure with default value */
349
350                 LOGW("failed to load ini. using hardcoded default\n");
351                 /* general */
352                 snprintf(ini->port_name, sizeof(ini->port_name), "%s", DEFAULT_PORT);
353                 for (i = 0; i < ini->codec_list; i++) {
354                         MEDIA_CODEC_GET_DEFAULT_LIST(ini->codec[i].codec_info[0].name,   DEFAULT_HW_DECODER_NAME);
355                         MEDIA_CODEC_GET_DEFAULT_LIST(ini->codec[i].codec_info[0].mime,   DEFAULT_HW_DECODER_MIME);
356                         MEDIA_CODEC_GET_DEFAULT_LIST(ini->codec[i].codec_info[0].format, DEFAULT_HW_DECODER_FORMAT);
357
358                         MEDIA_CODEC_GET_DEFAULT_LIST(ini->codec[i].codec_info[1].name,   DEFAULT_HW_ENCODER_NAME);
359                         MEDIA_CODEC_GET_DEFAULT_LIST(ini->codec[i].codec_info[1].mime,   DEFAULT_HW_ENCODER_MIME);
360                         MEDIA_CODEC_GET_DEFAULT_LIST(ini->codec[i].codec_info[1].format, DEFAULT_HW_ENCODER_FORMAT);
361
362                         MEDIA_CODEC_GET_DEFAULT_LIST(ini->codec[i].codec_info[2].name,   DEFAULT_SW_DECODER_NAME);
363                         MEDIA_CODEC_GET_DEFAULT_LIST(ini->codec[i].codec_info[2].mime,   DEFAULT_SW_DECODER_MIME);
364                         MEDIA_CODEC_GET_DEFAULT_LIST(ini->codec[i].codec_info[2].format, DEFAULT_SW_DECODER_FORMAT);
365
366                         MEDIA_CODEC_GET_DEFAULT_LIST(ini->codec[i].codec_info[3].name,   DEFAULT_SW_ENCODER_NAME);
367                         MEDIA_CODEC_GET_DEFAULT_LIST(ini->codec[i].codec_info[3].mime,   DEFAULT_SW_ENCODER_MIME);
368                         MEDIA_CODEC_GET_DEFAULT_LIST(ini->codec[i].codec_info[3].format, DEFAULT_SW_ENCODER_FORMAT);
369                 }
370         }
371
372         if (0 == strcmp(ini->port_name, "GST_PORT"))
373                 ini->port_type = GST_PORT;
374         else {
375                 LOGE("Invalid port is set to [%s] [%d]\n", ini->port_name, ini->port_type);
376                 iniparser_freedict(dict);
377                 goto ERROR;
378         }
379         LOGD("The port is set to [%s] [%d]\n", ini->port_name, ini->port_type);
380
381         for (i = 0; i < ini->codec_list; i++) {
382                 memset(cname, 0x00, CNAME_SIZE);
383                 snprintf(cname, CNAME_SIZE, "%s", general_codec_list[i].cname);
384                 int len = strlen(cname);
385
386                 snprintf(cname+len, CNAME_SIZE-len, "%s", ":hw_decoder");
387                 MEDIA_CODEC_PRINT_LIST(ini->codec[i].codec_info[0], cname);
388                 snprintf(cname+len, CNAME_SIZE-len, "%s", ":hw_encoder");
389                 MEDIA_CODEC_PRINT_LIST(ini->codec[i].codec_info[1], cname);
390                 snprintf(cname+len, CNAME_SIZE-len, "%s", ":sw_decoder");
391                 MEDIA_CODEC_PRINT_LIST(ini->codec[i].codec_info[2], cname);
392                 snprintf(cname+len, CNAME_SIZE-len, "%s", ":sw_encoder");
393                 MEDIA_CODEC_PRINT_LIST(ini->codec[i].codec_info[3], cname);
394         }
395
396         /* free dict as we got our own structure */
397         iniparser_freedict(dict);
398
399         /* dump structure */
400         LOGD("codec settings -----------------------------------\n");
401
402         /* general */
403         LOGD("port_name: %s\n", ini->port_name);
404         LOGD("port_type : %d\n", ini->port_type);
405
406         return MC_ERROR_NONE;
407 ERROR:
408         return MC_COURRPTED_INI;
409
410 }
411
412 static void _mc_ini_check_ini_status(void)
413 {
414         struct stat ini_buff;
415
416         if (g_stat(MEDIA_CODEC_INI_DEFAULT_PATH, &ini_buff) < 0) {
417                 LOGW("failed to get codec ini status\n");
418         } else {
419                 if (ini_buff.st_size < 5) {
420                         LOGW("codec.ini file size=%d, Corrupted! So, Removed\n", (int)ini_buff.st_size);
421
422                         if (g_remove(MEDIA_CODEC_INI_DEFAULT_PATH) == -1)
423                                 LOGE("failed to delete corrupted ini");
424                 }
425         }
426 }
427
428 #ifdef MEDIA_CODEC_DEFAULT_INI
429 static gboolean _generate_default_ini(void)
430 {
431         FILE *fp = NULL;
432         gchar *default_ini = MEDIA_CODEC_DEFAULT_INI;
433
434         /* create new file */
435         fp = fopen(MEDIA_CODEC_INI_DEFAULT_PATH, "wt");
436
437         if (!fp)
438                 return FALSE;
439
440         /* writing default ini file */
441         if (strlen(default_ini) !=
442                         fwrite(default_ini, 1, strlen(default_ini), fp)) {
443                 fclose(fp);
444                 return FALSE;
445         }
446
447         fclose(fp);
448         return TRUE;
449 }
450 #endif
451 #endif /* #ifdef _MEDIA_CODEC_INI_C_ */