SF_OMX_Core.c 13 KB

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