SF_OMX_Core.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. OMX_ERRORTYPE ClearOMXBuffer(SF_OMX_COMPONENT *pSfOMXComponent, OMX_BUFFERHEADERTYPE *pBuffer)
  196. {
  197. OMX_U32 i = 0;
  198. for (i = 0; i < sizeof(pSfOMXComponent->pBufferArray) / sizeof(pSfOMXComponent->pBufferArray[0]); i++)
  199. {
  200. if (pSfOMXComponent->pBufferArray[i] == pBuffer)
  201. {
  202. pSfOMXComponent->pBufferArray[i] = NULL;
  203. LOG(SF_LOG_DEBUG, "Clear OMX buffer %p at index %d\r\n", pBuffer, i);
  204. return OMX_ErrorNone;
  205. }
  206. }
  207. if (i == sizeof(pSfOMXComponent->pBufferArray) / sizeof(pSfOMXComponent->pBufferArray[0]))
  208. {
  209. LOG(SF_LOG_ERR, "Could Not found omx buffer!\r\n");
  210. }
  211. return OMX_ErrorBadParameter;
  212. }
  213. OMX_U32 GetOMXBufferCount(SF_OMX_COMPONENT *pSfOMXComponent)
  214. {
  215. OMX_U32 i = 0;
  216. OMX_U32 count = 0;
  217. for (i = 0; i < sizeof(pSfOMXComponent->pBufferArray) / sizeof(pSfOMXComponent->pBufferArray[0]); i++)
  218. {
  219. if (pSfOMXComponent->pBufferArray[i] != NULL)
  220. {
  221. LOG(SF_LOG_DEBUG, "find OMX buffer %p at index %d\r\n", pSfOMXComponent->pBufferArray[i], i);
  222. count++;
  223. }
  224. }
  225. return count;
  226. }
  227. OMX_ERRORTYPE StoreOMXBuffer(SF_OMX_COMPONENT *pSfOMXComponent, OMX_BUFFERHEADERTYPE *pBuffer)
  228. {
  229. OMX_U32 i = 0;
  230. for (i = 0; i < sizeof(pSfOMXComponent->pBufferArray) / sizeof(pSfOMXComponent->pBufferArray[0]); i++)
  231. {
  232. if (pSfOMXComponent->pBufferArray[i] == NULL)
  233. {
  234. pSfOMXComponent->pBufferArray[i] = pBuffer;
  235. LOG(SF_LOG_DEBUG, "Store OMX buffer %p at index %d\r\n", pBuffer, i);
  236. return OMX_ErrorNone;
  237. }
  238. }
  239. if (i == sizeof(pSfOMXComponent->pBufferArray))
  240. {
  241. LOG(SF_LOG_ERR, "Buffer arrary full!\r\n");
  242. }
  243. return OMX_ErrorInsufficientResources;
  244. }
  245. OMX_BUFFERHEADERTYPE *GetOMXBufferByAddr(SF_OMX_COMPONENT *pSfOMXComponent, OMX_U8 *pAddr)
  246. {
  247. OMX_U32 i = 0;
  248. OMX_BUFFERHEADERTYPE *pOMXBuffer;
  249. FunctionIn();
  250. LOG(SF_LOG_INFO, "ADDR=%p\r\n", pAddr);
  251. for (i = 0; i < sizeof(pSfOMXComponent->pBufferArray) / sizeof(pSfOMXComponent->pBufferArray[0]); i++)
  252. {
  253. pOMXBuffer = pSfOMXComponent->pBufferArray[i];
  254. LOG(SF_LOG_DEBUG, "Compare OMX buffer %p at index %d with addr %p\r\n", pOMXBuffer, i, pAddr);
  255. if (pOMXBuffer == NULL)
  256. {
  257. continue;
  258. }
  259. if (pOMXBuffer->pBuffer == pAddr)
  260. {
  261. return pOMXBuffer;
  262. }
  263. }
  264. LOG(SF_LOG_ERR, "could not find buffer!\r\n");
  265. FunctionOut();
  266. return NULL;
  267. }
  268. void SF_LogMsgAppend(int level, const char *format, ...)
  269. {
  270. va_list ptr;
  271. if (level > gDebugLevel)
  272. {
  273. return;
  274. }
  275. printf("%66s\t", " ");
  276. va_start(ptr, format);
  277. vprintf(format, ptr);
  278. va_end(ptr);
  279. }
  280. void SF_LogMsg(int level, const char *function, int line, const char *format, ...)
  281. {
  282. char *prefix = "";
  283. va_list ptr;
  284. struct timeval tv;
  285. if (level > gDebugLevel)
  286. {
  287. return;
  288. }
  289. switch (level)
  290. {
  291. case SF_LOG_ERR:
  292. prefix = "[ERROR]";
  293. break;
  294. case SF_LOG_WARN:
  295. prefix = "[WARN ]";
  296. break;
  297. case SF_LOG_PERF:
  298. prefix = "[PERF ]";
  299. break;
  300. case SF_LOG_INFO:
  301. prefix = "[INFO ]";
  302. break;
  303. case SF_LOG_DEBUG:
  304. prefix = "[DEBUG]";
  305. break;
  306. default:
  307. break;
  308. }
  309. gettimeofday(&tv, NULL);
  310. printf("[%06ld:%06ld]%10s%32s%10d\t", tv.tv_sec, tv.tv_usec, prefix, function, line);
  311. va_start(ptr, format);
  312. vprintf(format, ptr);
  313. va_end(ptr);
  314. }