SF_OMX_Core.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. static SF_OMX_COMPONENT *sf_omx_component_list[] = {
  16. &sf_dec_decoder_h265,
  17. &sf_dec_decoder_h264,
  18. NULL,
  19. };
  20. #define SF_OMX_COMPONENT_NUM (sizeof(sf_omx_component_list) / sizeof(SF_OMX_COMPONENT *))
  21. int GetNumberOfComponent()
  22. {
  23. return sizeof(sf_omx_component_list) / sizeof(SF_OMX_COMPONENT *);
  24. }
  25. OMX_ERRORTYPE SF_OMX_ComponentLoad(SF_OMX_COMPONENT *sf_component)
  26. {
  27. OMX_ERRORTYPE ret = OMX_ErrorNone;
  28. FunctionIn();
  29. sf_component->SF_OMX_ComponentConstructor(sf_component);
  30. FunctionOut();
  31. return ret;
  32. }
  33. OMX_API OMX_ERRORTYPE OMX_APIENTRY OMX_Init(void)
  34. {
  35. OMX_ERRORTYPE ret = OMX_ErrorNone;
  36. char *debugLevel = NULL;
  37. FunctionIn();
  38. if (gInitialized == 0)
  39. {
  40. gInitialized = 1;
  41. }
  42. debugLevel = getenv("OMX_DEBUG");
  43. if (debugLevel != NULL)
  44. {
  45. gDebugLevel = atoi(debugLevel);
  46. }
  47. FunctionOut();
  48. return ret;
  49. }
  50. OMX_API OMX_ERRORTYPE OMX_APIENTRY OMX_Deinit(void)
  51. {
  52. OMX_ERRORTYPE ret = OMX_ErrorNone;
  53. FunctionIn();
  54. FunctionOut();
  55. return ret;
  56. }
  57. OMX_API OMX_ERRORTYPE OMX_APIENTRY OMX_ComponentNameEnum(
  58. OMX_OUT OMX_STRING cComponentName,
  59. OMX_IN OMX_U32 nNameLength,
  60. OMX_IN OMX_U32 nIndex)
  61. {
  62. OMX_ERRORTYPE ret = OMX_ErrorNone;
  63. FunctionIn();
  64. if (nIndex >= SF_OMX_COMPONENT_NUM)
  65. {
  66. ret = OMX_ErrorNoMore;
  67. goto EXIT;
  68. }
  69. snprintf(cComponentName, nNameLength, "%s", sf_omx_component_list[nIndex]->componentName);
  70. EXIT:
  71. FunctionOut();
  72. return ret;
  73. }
  74. OMX_API OMX_ERRORTYPE OMX_APIENTRY OMX_GetHandle(
  75. OMX_OUT OMX_HANDLETYPE *pHandle,
  76. OMX_IN OMX_STRING cComponentName,
  77. OMX_IN OMX_PTR pAppData,
  78. OMX_IN OMX_CALLBACKTYPE *pCallBacks)
  79. {
  80. OMX_ERRORTYPE ret = OMX_ErrorNone;
  81. unsigned int i = 0;
  82. SF_OMX_COMPONENT *pSf_omx_component = NULL;
  83. FunctionIn();
  84. if (gInitialized != 1)
  85. {
  86. ret = OMX_ErrorNotReady;
  87. goto EXIT;
  88. }
  89. if ((pHandle == NULL) || (cComponentName == NULL))
  90. {
  91. ret = OMX_ErrorBadParameter;
  92. goto EXIT;
  93. }
  94. for (i = 0; i < SF_OMX_COMPONENT_NUM; i++)
  95. {
  96. pSf_omx_component = sf_omx_component_list[i];
  97. if (pSf_omx_component == NULL)
  98. {
  99. ret = OMX_ErrorBadParameter;
  100. goto EXIT;
  101. }
  102. if (strcmp(cComponentName, pSf_omx_component->componentName) == 0)
  103. {
  104. ret = pSf_omx_component->SF_OMX_ComponentConstructor(pSf_omx_component);
  105. if (ret != OMX_ErrorNone)
  106. {
  107. goto EXIT;
  108. }
  109. *pHandle = pSf_omx_component->pOMXComponent;
  110. pSf_omx_component->callbacks = pCallBacks;
  111. pSf_omx_component->pAppData = pAppData;
  112. break;
  113. }
  114. }
  115. EXIT:
  116. FunctionOut();
  117. return ret;
  118. }
  119. OMX_API OMX_ERRORTYPE OMX_APIENTRY OMX_FreeHandle(OMX_IN OMX_HANDLETYPE hComponent)
  120. {
  121. OMX_ERRORTYPE ret = OMX_ErrorNone;
  122. OMX_COMPONENTTYPE *pOMXComponent = (OMX_COMPONENTTYPE *)hComponent;
  123. SF_OMX_COMPONENT *pSfOMXComponent = (SF_OMX_COMPONENT *)pOMXComponent->pComponentPrivate;
  124. FunctionIn();
  125. ret = pSfOMXComponent->SF_OMX_ComponentClear(pSfOMXComponent);
  126. FunctionOut();
  127. return ret;
  128. }
  129. OMX_API OMX_ERRORTYPE OMX_APIENTRY OMX_SetupTunnel(
  130. OMX_IN OMX_HANDLETYPE hOutput,
  131. OMX_IN OMX_U32 nPortOutput,
  132. OMX_IN OMX_HANDLETYPE hInput,
  133. OMX_IN OMX_U32 nPortInput)
  134. {
  135. OMX_ERRORTYPE ret = OMX_ErrorNone;
  136. // OMX_COMPONENTTYPE *pOMXComponent_hOutput = (OMX_COMPONENTTYPE *)hOutput;
  137. // OMX_COMPONENTTYPE *pOMXComponent_hInput = (OMX_COMPONENTTYPE *)hInput;
  138. FunctionIn();
  139. ret = OMX_ErrorNotImplemented;
  140. FunctionOut();
  141. return ret;
  142. }
  143. OMX_API OMX_ERRORTYPE OMX_GetContentPipe(
  144. OMX_OUT OMX_HANDLETYPE *hPipe,
  145. OMX_IN OMX_STRING szURI)
  146. {
  147. OMX_ERRORTYPE ret = OMX_ErrorNone;
  148. FunctionIn();
  149. FunctionOut();
  150. return ret;
  151. }
  152. OMX_API OMX_ERRORTYPE OMX_GetComponentsOfRole(
  153. OMX_IN OMX_STRING role,
  154. OMX_INOUT OMX_U32 *pNumComps,
  155. OMX_INOUT OMX_U8 **compNames)
  156. {
  157. OMX_ERRORTYPE ret = OMX_ErrorNone;
  158. FunctionIn();
  159. FunctionOut();
  160. return ret;
  161. }
  162. OMX_API OMX_ERRORTYPE OMX_GetRolesOfComponent(
  163. OMX_IN OMX_STRING compName,
  164. OMX_INOUT OMX_U32 *pNumRoles,
  165. OMX_OUT OMX_U8 **roles)
  166. {
  167. OMX_ERRORTYPE ret = OMX_ErrorNone;
  168. FunctionIn();
  169. FunctionOut();
  170. return ret;
  171. }
  172. void sf_get_component_functions(SF_COMPONENT_FUNCTIONS *funcs, OMX_PTR *sohandle)
  173. {
  174. FunctionIn();
  175. funcs->ComponentCreate = dlsym(sohandle, "ComponentCreate");
  176. funcs->ComponentExecute = dlsym(sohandle, "ComponentExecute");
  177. funcs->ComponentGetParameter = dlsym(sohandle, "ComponentGetParameter");
  178. funcs->ComponentGetState = dlsym(sohandle, "ComponentGetState");
  179. funcs->ComponentNotifyListeners = dlsym(sohandle, "ComponentNotifyListeners");
  180. funcs->ComponentPortCreate = dlsym(sohandle, "ComponentPortCreate");
  181. funcs->ComponentPortDestroy = dlsym(sohandle, "ComponentPortDestroy");
  182. funcs->ComponentPortFlush = dlsym(sohandle, "ComponentPortFlush");
  183. funcs->ComponentPortGetData = dlsym(sohandle, "ComponentPortGetData");
  184. funcs->ComponentPortPeekData = dlsym(sohandle, "ComponentPortPeekData");
  185. funcs->ComponentPortSetData = dlsym(sohandle, "ComponentPortSetData");
  186. funcs->ComponentPortWaitReadyStatus = dlsym(sohandle, "ComponentPortWaitReadyStatus");
  187. funcs->ComponentRelease = dlsym(sohandle, "ComponentRelease");
  188. funcs->ComponentSetParameter = dlsym(sohandle, "ComponentSetParameter");
  189. funcs->ComponentStop = dlsym(sohandle, "ComponentStop");
  190. funcs->ComponentSetupTunnel = dlsym(sohandle, "ComponentSetupTunnel");
  191. funcs->ComponentWait = dlsym(sohandle, "ComponentWait");
  192. funcs->WaitBeforeComponentPortGetData = dlsym(sohandle, "WaitBeforeComponentPortGetData");
  193. funcs->ComponentChangeState = dlsym(sohandle, "ComponentChangeState");
  194. funcs->ComponentDestroy = dlsym(sohandle, "ComponentDestroy");
  195. funcs->ComponentRegisterListener = dlsym(sohandle, "ComponentRegisterListener");
  196. funcs->ComponentPortHasInputData = dlsym(sohandle, "ComponentPortHasInputData");
  197. funcs->ComponentPortGetSize = dlsym(sohandle, "ComponentPortGetSize");
  198. funcs->ComponentParamReturnTest = dlsym(sohandle, "ComponentParamReturnTest");
  199. //Listener
  200. funcs->SetupDecListenerContext = dlsym(sohandle, "SetupDecListenerContext");
  201. funcs->SetupEncListenerContext = dlsym(sohandle, "SetupEncListenerContext");
  202. funcs->ClearDecListenerContext = dlsym(sohandle, "ClearDecListenerContext");
  203. funcs->HandleDecCompleteSeqEvent = dlsym(sohandle, "HandleDecCompleteSeqEvent");
  204. funcs->HandleDecRegisterFbEvent = dlsym(sohandle, "HandleDecRegisterFbEvent");
  205. funcs->HandleDecGetOutputEvent = dlsym(sohandle, "HandleDecGetOutputEvent");
  206. funcs->HandleDecCloseEvent = dlsym(sohandle, "HandleDecCloseEvent");
  207. funcs->ClearEncListenerContext = dlsym(sohandle, "ClearEncListenerContext");
  208. funcs->HandleEncFullEvent = dlsym(sohandle, "HandleEncFullEvent");
  209. funcs->HandleEncGetOutputEvent = dlsym(sohandle, "HandleEncGetOutputEvent");
  210. funcs->HandleEncCompleteSeqEvent = dlsym(sohandle, "HandleEncCompleteSeqEvent");
  211. funcs->HandleEncGetEncCloseEvent = dlsym(sohandle, "HandleEncGetEncCloseEvent");
  212. funcs->EncoderListener = dlsym(sohandle, "EncoderListener");
  213. funcs->DecoderListener = dlsym(sohandle, "DecoderListener");
  214. // Helper
  215. funcs->SetDefaultEncTestConfig = dlsym(sohandle, "SetDefaultEncTestConfig");
  216. funcs->SetDefaultDecTestConfig = dlsym(sohandle, "SetDefaultDecTestConfig");
  217. funcs->LoadFirmware = dlsym(sohandle, "LoadFirmware");
  218. funcs->SetupEncoderOpenParam = dlsym(sohandle, "SetupEncoderOpenParam");
  219. funcs->SetUpDecoderOpenParam = dlsym(sohandle, "SetUpDecoderOpenParam");
  220. // VPU
  221. funcs->VPU_GetProductId = dlsym(sohandle, "VPU_GetProductId");
  222. funcs->Queue_Enqueue = dlsym(sohandle, "Queue_Enqueue");
  223. funcs->Queue_Get_Cnt = dlsym(sohandle, "Queue_Get_Cnt");
  224. // VPU Log
  225. funcs->InitLog = dlsym(sohandle, "InitLog");
  226. funcs->DeInitLog = dlsym(sohandle, "DeInitLog");
  227. funcs->SetMaxLogLevel = dlsym(sohandle, "SetMaxLogLevel");
  228. funcs->GetMaxLogLevel = dlsym(sohandle, "GetMaxLogLevel");
  229. FunctionOut();
  230. }
  231. SF_OMX_COMPONENT *GetSFOMXComponrntByComponent(Component *pComponent)
  232. {
  233. SF_OMX_COMPONENT *pSFOMXComponent = NULL;
  234. int size = GetNumberOfComponent();
  235. FunctionIn();
  236. for (int i = 0; i < size; i++)
  237. {
  238. pSFOMXComponent = sf_omx_component_list[i];
  239. if (pSFOMXComponent == NULL)
  240. {
  241. goto EXIT;
  242. }
  243. if (pSFOMXComponent->hSFComponentExecoder == pComponent || pSFOMXComponent->hSFComponentFeeder == pComponent || pSFOMXComponent->hSFComponentRender == pComponent)
  244. {
  245. break;
  246. }
  247. }
  248. EXIT:
  249. FunctionOut();
  250. return pSFOMXComponent;
  251. }
  252. OMX_ERRORTYPE ClearOMXBuffer(SF_OMX_COMPONENT *pSfOMXComponent, OMX_BUFFERHEADERTYPE *pBuffer)
  253. {
  254. OMX_U32 i = 0;
  255. for (i = 0; i < sizeof(pSfOMXComponent->pBufferArray) / sizeof(pSfOMXComponent->pBufferArray[0]); i++)
  256. {
  257. if (pSfOMXComponent->pBufferArray[i] == pBuffer)
  258. {
  259. pSfOMXComponent->pBufferArray[i] = NULL;
  260. return OMX_ErrorNone;
  261. }
  262. }
  263. if (i == sizeof(pSfOMXComponent->pBufferArray) / sizeof(pSfOMXComponent->pBufferArray[0]))
  264. {
  265. LOG(SF_LOG_ERR, "Could Not found omx buffer!\r\n");
  266. }
  267. return OMX_ErrorBadParameter;
  268. }
  269. OMX_U32 GetOMXBufferCount(SF_OMX_COMPONENT *pSfOMXComponent)
  270. {
  271. OMX_U32 i = 0;
  272. OMX_U32 count = 0;
  273. for (i = 0; i < sizeof(pSfOMXComponent->pBufferArray) / sizeof(pSfOMXComponent->pBufferArray[0]); i++)
  274. {
  275. if (pSfOMXComponent->pBufferArray[i] != NULL)
  276. {
  277. count++;
  278. }
  279. }
  280. return count;
  281. }
  282. OMX_ERRORTYPE StoreOMXBuffer(SF_OMX_COMPONENT *pSfOMXComponent, OMX_BUFFERHEADERTYPE *pBuffer)
  283. {
  284. OMX_U32 i = 0;
  285. for (i = 0; i < sizeof(pSfOMXComponent->pBufferArray) / sizeof(pSfOMXComponent->pBufferArray[0]); i++)
  286. {
  287. if (pSfOMXComponent->pBufferArray[i] == NULL)
  288. {
  289. pSfOMXComponent->pBufferArray[i] = pBuffer;
  290. return OMX_ErrorNone;
  291. }
  292. }
  293. if (i == sizeof(pSfOMXComponent->pBufferArray))
  294. {
  295. LOG(SF_LOG_ERR, "Buffer arrary full!\r\n");
  296. }
  297. return OMX_ErrorInsufficientResources;
  298. }
  299. OMX_BUFFERHEADERTYPE *GetOMXBufferByAddr(SF_OMX_COMPONENT *pSfOMXComponent, OMX_U8 *pAddr)
  300. {
  301. OMX_U32 i = 0;
  302. OMX_BUFFERHEADERTYPE *pOMXBuffer;
  303. for (i = 0; i < sizeof(pSfOMXComponent->pBufferArray) / sizeof(pSfOMXComponent->pBufferArray[0]); i++)
  304. {
  305. pOMXBuffer = pSfOMXComponent->pBufferArray[i];
  306. if (pOMXBuffer->pBuffer == pAddr)
  307. {
  308. return pOMXBuffer;
  309. }
  310. }
  311. if (i == sizeof(pSfOMXComponent->pBufferArray))
  312. {
  313. LOG(SF_LOG_ERR, "could not find buffer!\r\n");
  314. }
  315. return NULL;
  316. }
  317. void SF_LogMsg(int level, const char *function, int line, const char *format, ...)
  318. {
  319. char *prefix = "";
  320. va_list ptr;
  321. struct timeval tv;
  322. if (level > gDebugLevel)
  323. {
  324. return;
  325. }
  326. switch (level)
  327. {
  328. case SF_LOG_ERR:
  329. prefix = "[ERROR]";
  330. break;
  331. case SF_LOG_WARN:
  332. prefix = "[WARN ]";
  333. break;
  334. case SF_LOG_PERF:
  335. prefix = "[PERF ]";
  336. break;
  337. case SF_LOG_INFO:
  338. prefix = "[INFO ]";
  339. break;
  340. case SF_LOG_DEBUG:
  341. prefix = "[DEBUG]";
  342. break;
  343. default:
  344. break;
  345. }
  346. gettimeofday(&tv, NULL);
  347. printf("[%06d:%06d]%10s%32s%10d\t", tv.tv_sec, tv.tv_usec, prefix, function, line);
  348. va_start(ptr, format);
  349. vprintf(format, ptr);
  350. va_end(ptr);
  351. }