SF_OMX_Core.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2021 StarFive Technology Co., Ltd.
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <stdlib.h>
  9. #include "SF_OMX_Core.h"
  10. static int gInitialized = 0;
  11. OMX_TICKS gInitTimeStamp = 0;
  12. int gDebugLevel = SF_LOG_ERR;
  13. extern SF_OMX_COMPONENT sf_dec_decoder_h265;
  14. extern SF_OMX_COMPONENT sf_dec_decoder_h264;
  15. extern SF_OMX_COMPONENT sf_enc_encoder_h265;
  16. //extern SF_OMX_COMPONENT sf_enc_encoder_h264;
  17. extern SF_OMX_COMPONENT sf_dec_decoder_mjpeg;
  18. SF_OMX_COMPONENT *sf_omx_component_list[] = {
  19. &sf_dec_decoder_h265,
  20. &sf_dec_decoder_h264,
  21. &sf_enc_encoder_h265,
  22. // &sf_enc_encoder_h264,
  23. &sf_dec_decoder_mjpeg,
  24. NULL,
  25. };
  26. #define SF_OMX_COMPONENT_NUM ((sizeof(sf_omx_component_list) / sizeof(SF_OMX_COMPONENT *)) - 1)
  27. int GetNumberOfComponent()
  28. {
  29. return sizeof(sf_omx_component_list) / sizeof(SF_OMX_COMPONENT *);
  30. }
  31. OMX_ERRORTYPE SF_OMX_ComponentLoad(SF_OMX_COMPONENT *sf_component)
  32. {
  33. OMX_ERRORTYPE ret = OMX_ErrorNone;
  34. FunctionIn();
  35. sf_component->SF_OMX_ComponentConstructor(sf_component);
  36. FunctionOut();
  37. return ret;
  38. }
  39. OMX_API OMX_ERRORTYPE OMX_APIENTRY OMX_Init(void)
  40. {
  41. OMX_ERRORTYPE ret = OMX_ErrorNone;
  42. char *debugLevel = NULL;
  43. FunctionIn();
  44. if (gInitialized == 0)
  45. {
  46. gInitialized = 1;
  47. }
  48. debugLevel = getenv("OMX_DEBUG");
  49. if (debugLevel != NULL)
  50. {
  51. gDebugLevel = atoi(debugLevel);
  52. }
  53. FunctionOut();
  54. return ret;
  55. }
  56. OMX_API OMX_ERRORTYPE OMX_APIENTRY OMX_Deinit(void)
  57. {
  58. OMX_ERRORTYPE ret = OMX_ErrorNone;
  59. FunctionIn();
  60. FunctionOut();
  61. return ret;
  62. }
  63. OMX_API OMX_ERRORTYPE OMX_APIENTRY OMX_ComponentNameEnum(
  64. OMX_OUT OMX_STRING cComponentName,
  65. OMX_IN OMX_U32 nNameLength,
  66. OMX_IN OMX_U32 nIndex)
  67. {
  68. OMX_ERRORTYPE ret = OMX_ErrorNone;
  69. FunctionIn();
  70. if (nIndex >= SF_OMX_COMPONENT_NUM)
  71. {
  72. ret = OMX_ErrorNoMore;
  73. goto EXIT;
  74. }
  75. snprintf(cComponentName, nNameLength, "%s", sf_omx_component_list[nIndex]->componentName);
  76. EXIT:
  77. FunctionOut();
  78. return ret;
  79. }
  80. OMX_API OMX_ERRORTYPE OMX_APIENTRY OMX_GetHandle(
  81. OMX_OUT OMX_HANDLETYPE *pHandle,
  82. OMX_IN OMX_STRING cComponentName,
  83. OMX_IN OMX_PTR pAppData,
  84. OMX_IN OMX_CALLBACKTYPE *pCallBacks)
  85. {
  86. OMX_ERRORTYPE ret = OMX_ErrorNone;
  87. unsigned int i = 0;
  88. SF_OMX_COMPONENT *pSf_omx_component = NULL;
  89. FunctionIn();
  90. if (gInitialized != 1)
  91. {
  92. ret = OMX_ErrorNotReady;
  93. goto EXIT;
  94. }
  95. if ((pHandle == NULL) || (cComponentName == NULL))
  96. {
  97. ret = OMX_ErrorBadParameter;
  98. goto EXIT;
  99. }
  100. for (i = 0; i < SF_OMX_COMPONENT_NUM; i++)
  101. {
  102. if (sf_omx_component_list[i] == NULL)
  103. {
  104. ret = OMX_ErrorBadParameter;
  105. goto EXIT;
  106. }
  107. if (strcmp(cComponentName, sf_omx_component_list[i]->componentName) == 0)
  108. {
  109. pSf_omx_component = malloc(sizeof(SF_OMX_COMPONENT));
  110. if (!pSf_omx_component)
  111. {
  112. goto EXIT;
  113. }
  114. memcpy(pSf_omx_component, sf_omx_component_list[i], sizeof(SF_OMX_COMPONENT));
  115. ret = pSf_omx_component->SF_OMX_ComponentConstructor(pSf_omx_component);
  116. if (ret != OMX_ErrorNone)
  117. {
  118. free(pSf_omx_component);
  119. goto EXIT;
  120. }
  121. *pHandle = pSf_omx_component->pOMXComponent;
  122. pSf_omx_component->callbacks = pCallBacks;
  123. pSf_omx_component->pAppData = pAppData;
  124. pSf_omx_component->state = OMX_StateLoaded;
  125. break;
  126. }
  127. }
  128. EXIT:
  129. FunctionOut();
  130. return ret;
  131. }
  132. OMX_API OMX_ERRORTYPE OMX_APIENTRY OMX_FreeHandle(OMX_IN OMX_HANDLETYPE hComponent)
  133. {
  134. OMX_ERRORTYPE ret = OMX_ErrorNone;
  135. OMX_COMPONENTTYPE *pOMXComponent = (OMX_COMPONENTTYPE *)hComponent;
  136. SF_OMX_COMPONENT *pSfOMXComponent = (SF_OMX_COMPONENT *)pOMXComponent->pComponentPrivate;
  137. FunctionIn();
  138. ret = pSfOMXComponent->SF_OMX_ComponentClear(pSfOMXComponent);
  139. if (!ret)
  140. {
  141. free(pSfOMXComponent);
  142. }
  143. FunctionOut();
  144. return ret;
  145. }
  146. OMX_API OMX_ERRORTYPE OMX_APIENTRY OMX_SetupTunnel(
  147. OMX_IN OMX_HANDLETYPE hOutput,
  148. OMX_IN OMX_U32 nPortOutput,
  149. OMX_IN OMX_HANDLETYPE hInput,
  150. OMX_IN OMX_U32 nPortInput)
  151. {
  152. OMX_ERRORTYPE ret = OMX_ErrorNone;
  153. // OMX_COMPONENTTYPE *pOMXComponent_hOutput = (OMX_COMPONENTTYPE *)hOutput;
  154. // OMX_COMPONENTTYPE *pOMXComponent_hInput = (OMX_COMPONENTTYPE *)hInput;
  155. FunctionIn();
  156. ret = OMX_ErrorNotImplemented;
  157. FunctionOut();
  158. return ret;
  159. }
  160. OMX_API OMX_ERRORTYPE OMX_GetContentPipe(
  161. OMX_OUT OMX_HANDLETYPE *hPipe,
  162. OMX_IN OMX_STRING szURI)
  163. {
  164. OMX_ERRORTYPE ret = OMX_ErrorNone;
  165. FunctionIn();
  166. FunctionOut();
  167. return ret;
  168. }
  169. OMX_API OMX_ERRORTYPE OMX_GetComponentsOfRole(
  170. OMX_IN OMX_STRING role,
  171. OMX_INOUT OMX_U32 *pNumComps,
  172. OMX_INOUT OMX_U8 **compNames)
  173. {
  174. OMX_ERRORTYPE ret = OMX_ErrorNone;
  175. FunctionIn();
  176. LOG(SF_LOG_INFO, "Role = %s\r\n", role);
  177. *pNumComps = 0;
  178. for (int i = 0; i < SF_OMX_COMPONENT_NUM; i ++)
  179. {
  180. if (sf_omx_component_list[i] == NULL)
  181. {
  182. break;
  183. }
  184. if (strcmp(sf_omx_component_list[i]->componentRule, role) == 0)
  185. {
  186. if (compNames != NULL) {
  187. strcpy((OMX_STRING)compNames[*pNumComps], sf_omx_component_list[i]->componentName);
  188. }
  189. *pNumComps = (*pNumComps + 1);
  190. LOG(SF_LOG_INFO,"Get component %s, Role = %s\r\n", sf_omx_component_list[i]->componentName, sf_omx_component_list[i]->componentRule)
  191. }
  192. }
  193. FunctionOut();
  194. return ret;
  195. }
  196. OMX_API OMX_ERRORTYPE OMX_GetRolesOfComponent(
  197. OMX_IN OMX_STRING compName,
  198. OMX_INOUT OMX_U32 *pNumRoles,
  199. OMX_OUT OMX_U8 **roles)
  200. {
  201. OMX_ERRORTYPE ret = OMX_ErrorNone;
  202. FunctionIn();
  203. #if 0 //todo
  204. for (int i = 0; i < SF_OMX_COMPONENT_NUM; i ++)
  205. {
  206. if (sf_omx_component_list[i] == NULL)
  207. {
  208. break;
  209. }
  210. if (strcmp(sf_omx_component_list[i]->componentName, compName) == 0)
  211. {
  212. *pNumRoles = 1;
  213. if (roles != NULL) {
  214. strcpy((OMX_STRING)roles[0], sf_omx_component_list[i]->componentRule);
  215. }
  216. LOG(SF_LOG_INFO,"Get component %s, Role = %s\r\n", sf_omx_component_list[i]->componentName, sf_omx_component_list[i]->componentRule)
  217. }
  218. }
  219. #endif
  220. FunctionOut();
  221. return ret;
  222. }
  223. void SF_LogMsgAppend(int level, const char *format, ...)
  224. {
  225. va_list ptr;
  226. if (level > gDebugLevel)
  227. {
  228. return;
  229. }
  230. printf("%66s\t", " ");
  231. va_start(ptr, format);
  232. vprintf(format, ptr);
  233. va_end(ptr);
  234. }
  235. void SF_LogMsg(int level, const char *function, int line, const char *format, ...)
  236. {
  237. char *prefix = "";
  238. va_list ptr;
  239. struct timeval tv;
  240. if (level > gDebugLevel)
  241. {
  242. return;
  243. }
  244. switch (level)
  245. {
  246. case SF_LOG_ERR:
  247. prefix = "[ERROR]";
  248. break;
  249. case SF_LOG_WARN:
  250. prefix = "[WARN ]";
  251. break;
  252. case SF_LOG_PERF:
  253. prefix = "[PERF ]";
  254. break;
  255. case SF_LOG_INFO:
  256. prefix = "[INFO ]";
  257. break;
  258. case SF_LOG_DEBUG:
  259. prefix = "[DEBUG]";
  260. break;
  261. default:
  262. break;
  263. }
  264. gettimeofday(&tv, NULL);
  265. printf("[%06ld:%06ld]%10s%32s%10d\t", tv.tv_sec, tv.tv_usec, prefix, function, line);
  266. va_start(ptr, format);
  267. vprintf(format, ptr);
  268. va_end(ptr);
  269. }