SF_OMX_Core.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. pSf_omx_component = sf_omx_component_list[i];
  103. if (pSf_omx_component == NULL)
  104. {
  105. ret = OMX_ErrorBadParameter;
  106. goto EXIT;
  107. }
  108. if (strcmp(cComponentName, pSf_omx_component->componentName) == 0)
  109. {
  110. ret = pSf_omx_component->SF_OMX_ComponentConstructor(pSf_omx_component);
  111. if (ret != OMX_ErrorNone)
  112. {
  113. goto EXIT;
  114. }
  115. *pHandle = pSf_omx_component->pOMXComponent;
  116. pSf_omx_component->callbacks = pCallBacks;
  117. pSf_omx_component->pAppData = pAppData;
  118. break;
  119. }
  120. }
  121. EXIT:
  122. FunctionOut();
  123. return ret;
  124. }
  125. OMX_API OMX_ERRORTYPE OMX_APIENTRY OMX_FreeHandle(OMX_IN OMX_HANDLETYPE hComponent)
  126. {
  127. OMX_ERRORTYPE ret = OMX_ErrorNone;
  128. OMX_COMPONENTTYPE *pOMXComponent = (OMX_COMPONENTTYPE *)hComponent;
  129. SF_OMX_COMPONENT *pSfOMXComponent = (SF_OMX_COMPONENT *)pOMXComponent->pComponentPrivate;
  130. FunctionIn();
  131. ret = pSfOMXComponent->SF_OMX_ComponentClear(pSfOMXComponent);
  132. FunctionOut();
  133. return ret;
  134. }
  135. OMX_API OMX_ERRORTYPE OMX_APIENTRY OMX_SetupTunnel(
  136. OMX_IN OMX_HANDLETYPE hOutput,
  137. OMX_IN OMX_U32 nPortOutput,
  138. OMX_IN OMX_HANDLETYPE hInput,
  139. OMX_IN OMX_U32 nPortInput)
  140. {
  141. OMX_ERRORTYPE ret = OMX_ErrorNone;
  142. // OMX_COMPONENTTYPE *pOMXComponent_hOutput = (OMX_COMPONENTTYPE *)hOutput;
  143. // OMX_COMPONENTTYPE *pOMXComponent_hInput = (OMX_COMPONENTTYPE *)hInput;
  144. FunctionIn();
  145. ret = OMX_ErrorNotImplemented;
  146. FunctionOut();
  147. return ret;
  148. }
  149. OMX_API OMX_ERRORTYPE OMX_GetContentPipe(
  150. OMX_OUT OMX_HANDLETYPE *hPipe,
  151. OMX_IN OMX_STRING szURI)
  152. {
  153. OMX_ERRORTYPE ret = OMX_ErrorNone;
  154. FunctionIn();
  155. FunctionOut();
  156. return ret;
  157. }
  158. OMX_API OMX_ERRORTYPE OMX_GetComponentsOfRole(
  159. OMX_IN OMX_STRING role,
  160. OMX_INOUT OMX_U32 *pNumComps,
  161. OMX_INOUT OMX_U8 **compNames)
  162. {
  163. OMX_ERRORTYPE ret = OMX_ErrorNone;
  164. FunctionIn();
  165. LOG(SF_LOG_INFO, "Role = %s\r\n", role);
  166. *pNumComps = 0;
  167. for (int i = 0; i < SF_OMX_COMPONENT_NUM; i ++)
  168. {
  169. if (sf_omx_component_list[i] == NULL)
  170. {
  171. break;
  172. }
  173. if (strcmp(sf_omx_component_list[i]->componentRule, role) == 0)
  174. {
  175. if (compNames != NULL) {
  176. strcpy((OMX_STRING)compNames[*pNumComps], sf_omx_component_list[i]->componentName);
  177. }
  178. *pNumComps = (*pNumComps + 1);
  179. LOG(SF_LOG_INFO,"Get component %s, Role = %s\r\n", sf_omx_component_list[i]->componentName, sf_omx_component_list[i]->componentRule)
  180. }
  181. }
  182. FunctionOut();
  183. return ret;
  184. }
  185. OMX_API OMX_ERRORTYPE OMX_GetRolesOfComponent(
  186. OMX_IN OMX_STRING compName,
  187. OMX_INOUT OMX_U32 *pNumRoles,
  188. OMX_OUT OMX_U8 **roles)
  189. {
  190. OMX_ERRORTYPE ret = OMX_ErrorNone;
  191. FunctionIn();
  192. FunctionOut();
  193. return ret;
  194. }
  195. void SF_LogMsgAppend(int level, const char *format, ...)
  196. {
  197. va_list ptr;
  198. if (level > gDebugLevel)
  199. {
  200. return;
  201. }
  202. printf("%66s\t", " ");
  203. va_start(ptr, format);
  204. vprintf(format, ptr);
  205. va_end(ptr);
  206. }
  207. void SF_LogMsg(int level, const char *function, int line, const char *format, ...)
  208. {
  209. char *prefix = "";
  210. va_list ptr;
  211. struct timeval tv;
  212. if (level > gDebugLevel)
  213. {
  214. return;
  215. }
  216. switch (level)
  217. {
  218. case SF_LOG_ERR:
  219. prefix = "[ERROR]";
  220. break;
  221. case SF_LOG_WARN:
  222. prefix = "[WARN ]";
  223. break;
  224. case SF_LOG_PERF:
  225. prefix = "[PERF ]";
  226. break;
  227. case SF_LOG_INFO:
  228. prefix = "[INFO ]";
  229. break;
  230. case SF_LOG_DEBUG:
  231. prefix = "[DEBUG]";
  232. break;
  233. default:
  234. break;
  235. }
  236. gettimeofday(&tv, NULL);
  237. printf("[%06ld:%06ld]%10s%32s%10d\t", tv.tv_sec, tv.tv_usec, prefix, function, line);
  238. va_start(ptr, format);
  239. vprintf(format, ptr);
  240. va_end(ptr);
  241. }