SF_OMX_Vdec_decoder.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  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 "SF_OMX_video_common.h"
  9. #include "SF_OMX_Core.h"
  10. extern OMX_TICKS gInitTimeStamp;
  11. static void OnEventArrived(Component com, unsigned long event, void *data, void *context)
  12. {
  13. SF_OMX_COMPONENT *pSfOMXComponent = GetSFOMXComponrntByComponent(com);
  14. PortContainerExternal *pPortContainerExternal = (PortContainerExternal *)data;
  15. OMX_BUFFERHEADERTYPE *pOMXBuffer;
  16. static OMX_U32 dec_cnt = 0;
  17. static struct timeval tv_old = {0};
  18. OMX_U32 fps = 0;
  19. OMX_U64 diff_time = 0; // ms
  20. FunctionIn();
  21. LOG(SF_LOG_INFO, "event=%X\r\n", event);
  22. switch (event)
  23. {
  24. case COMPONENT_EVENT_DEC_EMPTY_BUFFER_DONE:
  25. pOMXBuffer = GetOMXBufferByAddr(pSfOMXComponent, (OMX_U8 *)pPortContainerExternal->pBuffer);
  26. pSfOMXComponent->callbacks->EmptyBufferDone(pSfOMXComponent->pOMXComponent, pSfOMXComponent->pAppData, pOMXBuffer);
  27. sem_post(pSfOMXComponent->inputSemaphore);
  28. break;
  29. case COMPONENT_EVENT_DEC_FILL_BUFFER_DONE:
  30. {
  31. struct timeval tv;
  32. pOMXBuffer = GetOMXBufferByAddr(pSfOMXComponent, (OMX_U8 *)pPortContainerExternal->pBuffer);
  33. gettimeofday(&tv, NULL);
  34. pOMXBuffer->nFilledLen = pPortContainerExternal->nFilledLen;
  35. pOMXBuffer->nTimeStamp = tv.tv_sec * 1000000 + tv.tv_usec - gInitTimeStamp;
  36. #if 0
  37. {
  38. FILE *fb = fopen("./out.bcp", "ab+");
  39. LOG(SF_LOG_INFO, "%d %d\r\n", pOMXBuffer->nFilledLen, pOMXBuffer->nOffset);
  40. fwrite(pOMXBuffer->pBuffer, 1, pOMXBuffer->nFilledLen, fb);
  41. fclose(fb);
  42. }
  43. #endif
  44. // Following is to print the decoding fps
  45. if (dec_cnt == 0) {
  46. tv_old = tv;
  47. }
  48. if (dec_cnt++ >= 50) {
  49. diff_time = (tv.tv_sec - tv_old.tv_sec) * 1000 + (tv.tv_usec - tv_old.tv_usec) / 1000;
  50. fps = 1000 * (dec_cnt - 1) / diff_time;
  51. dec_cnt = 0;
  52. LOG(SF_LOG_WARN, "Decoding fps: %d \r\n", fps);
  53. }
  54. LOG(SF_LOG_PERF, "OMX finish one buffer\r\n");
  55. pSfOMXComponent->callbacks->FillBufferDone(pSfOMXComponent->pOMXComponent, pSfOMXComponent->pAppData, pOMXBuffer);
  56. }
  57. break;
  58. case COMPONENT_EVENT_DEC_REGISTER_FB:
  59. {
  60. struct timeval tv;
  61. gettimeofday(&tv, NULL);
  62. gInitTimeStamp = tv.tv_sec * 1000000 + tv.tv_usec;
  63. pSfOMXComponent->callbacks->EventHandler(pSfOMXComponent->pOMXComponent, pSfOMXComponent->pAppData, OMX_EventPortSettingsChanged,
  64. 1, OMX_IndexParamPortDefinition, NULL);
  65. }
  66. break;
  67. case COMPONENT_EVENT_DEC_DECODED_ALL:
  68. pSfOMXComponent->callbacks->EventHandler(pSfOMXComponent->pOMXComponent, pSfOMXComponent->pAppData, OMX_EventBufferFlag,
  69. 1, 1, NULL);
  70. default:
  71. break;
  72. }
  73. FunctionOut();
  74. }
  75. static OMX_ERRORTYPE SF_OMX_EmptyThisBuffer(
  76. OMX_IN OMX_HANDLETYPE hComponent,
  77. OMX_IN OMX_BUFFERHEADERTYPE *pBuffer)
  78. {
  79. OMX_ERRORTYPE ret = OMX_ErrorNone;
  80. FunctionIn();
  81. if (hComponent == NULL)
  82. {
  83. ret = OMX_ErrorBadParameter;
  84. goto EXIT;
  85. }
  86. OMX_COMPONENTTYPE *pOMXComponent = (OMX_COMPONENTTYPE *)hComponent;
  87. SF_OMX_COMPONENT *pSfOMXComponent = pOMXComponent->pComponentPrivate;
  88. ComponentImpl *pFeederComponent = (ComponentImpl *)pSfOMXComponent->hSFComponentFeeder;
  89. sem_wait(pSfOMXComponent->inputSemaphore);
  90. PortContainerExternal *pPortContainerExternal = malloc(sizeof(PortContainerExternal));
  91. if (pPortContainerExternal == NULL)
  92. {
  93. LOG(SF_LOG_ERR, "malloc fail\r\n");
  94. return OMX_ErrorInsufficientResources;
  95. }
  96. memset(pPortContainerExternal, 0, sizeof(PortContainerExternal));
  97. pPortContainerExternal->pBuffer = pBuffer->pBuffer;
  98. pPortContainerExternal->nFilledLen = pBuffer->nFilledLen;
  99. pPortContainerExternal->nFlags = pBuffer->nFlags;
  100. if (pSfOMXComponent->functions->Queue_Enqueue(pFeederComponent->srcPort.inputQ, (void *)pPortContainerExternal) != OMX_TRUE)
  101. {
  102. LOG(SF_LOG_ERR, "%p:%p FAIL\r\n", pFeederComponent->srcPort.inputQ, pPortContainerExternal);
  103. free(pPortContainerExternal);
  104. return OMX_ErrorInsufficientResources;
  105. }
  106. LOG(SF_LOG_PERF, "input queue count=%d\r\n", pSfOMXComponent->functions->Queue_Get_Cnt(pFeederComponent->srcPort.inputQ));
  107. free(pPortContainerExternal);
  108. pFeederComponent->pause = OMX_FALSE;
  109. EXIT:
  110. FunctionOut();
  111. return ret;
  112. }
  113. static OMX_ERRORTYPE SF_OMX_FillThisBuffer(
  114. OMX_IN OMX_HANDLETYPE hComponent,
  115. OMX_IN OMX_BUFFERHEADERTYPE *pBuffer)
  116. {
  117. OMX_ERRORTYPE ret = OMX_ErrorNone;
  118. FunctionIn();
  119. if (hComponent == NULL)
  120. {
  121. ret = OMX_ErrorBadParameter;
  122. goto EXIT;
  123. }
  124. OMX_COMPONENTTYPE *pOMXComponent = (OMX_COMPONENTTYPE *)hComponent;
  125. SF_OMX_COMPONENT *pSfOMXComponent = (SF_OMX_COMPONENT *)pOMXComponent->pComponentPrivate;
  126. ComponentImpl *pRendererComponent = (ComponentImpl *)pSfOMXComponent->hSFComponentRender;
  127. PortContainerExternal *pPortContainerExternal = malloc(sizeof(PortContainerExternal));
  128. if (pPortContainerExternal == NULL)
  129. {
  130. LOG(SF_LOG_ERR, "malloc fail\r\n");
  131. return OMX_ErrorInsufficientResources;
  132. }
  133. memset(pPortContainerExternal, 0, sizeof(PortContainerExternal));
  134. pPortContainerExternal->pBuffer = pBuffer->pBuffer;
  135. pPortContainerExternal->nFilledLen = pBuffer->nAllocLen;
  136. if (pSfOMXComponent->functions->Queue_Enqueue(pRendererComponent->sinkPort.inputQ, (void *)pPortContainerExternal) == FALSE)
  137. {
  138. LOG(SF_LOG_ERR, "%p:%p FAIL\r\n", pRendererComponent->sinkPort.inputQ, pPortContainerExternal);
  139. free(pPortContainerExternal);
  140. return OMX_ErrorInsufficientResources;
  141. }
  142. LOG(SF_LOG_PERF, "output queue count=%d\r\n", pSfOMXComponent->functions->Queue_Get_Cnt(pRendererComponent->sinkPort.inputQ));
  143. free(pPortContainerExternal);
  144. pRendererComponent->pause = OMX_FALSE;
  145. EXIT:
  146. FunctionOut();
  147. return ret;
  148. }
  149. static OMX_ERRORTYPE SF_OMX_UseBuffer(
  150. OMX_IN OMX_HANDLETYPE hComponent,
  151. OMX_INOUT OMX_BUFFERHEADERTYPE **ppBufferHdr,
  152. OMX_IN OMX_U32 nPortIndex,
  153. OMX_IN OMX_PTR pAppPrivate,
  154. OMX_IN OMX_U32 nSizeBytes,
  155. OMX_IN OMX_U8 *pBuffer)
  156. {
  157. OMX_ERRORTYPE ret = OMX_ErrorNone;
  158. OMX_COMPONENTTYPE *pOMXComponent = (OMX_COMPONENTTYPE *)hComponent;
  159. SF_OMX_COMPONENT *pSfOMXComponent = pOMXComponent->pComponentPrivate;
  160. FunctionIn();
  161. if (hComponent == NULL)
  162. {
  163. ret = OMX_ErrorBadParameter;
  164. goto EXIT;
  165. }
  166. OMX_BUFFERHEADERTYPE *temp_bufferHeader = (OMX_BUFFERHEADERTYPE *)malloc(sizeof(OMX_BUFFERHEADERTYPE));
  167. if (temp_bufferHeader == NULL)
  168. {
  169. LOG(SF_LOG_ERR, "malloc fail\r\n");
  170. return OMX_ErrorInsufficientResources;
  171. }
  172. memset(temp_bufferHeader, 0, sizeof(OMX_BUFFERHEADERTYPE));
  173. temp_bufferHeader->nAllocLen = nSizeBytes;
  174. temp_bufferHeader->pAppPrivate = pAppPrivate;
  175. temp_bufferHeader->pBuffer = pBuffer;
  176. *ppBufferHdr = temp_bufferHeader;
  177. ret = StoreOMXBuffer(pSfOMXComponent, temp_bufferHeader);
  178. EXIT:
  179. FunctionOut();
  180. return ret;
  181. }
  182. static OMX_ERRORTYPE SF_OMX_AllocateBuffer(
  183. OMX_IN OMX_HANDLETYPE hComponent,
  184. OMX_INOUT OMX_BUFFERHEADERTYPE **ppBuffer,
  185. OMX_IN OMX_U32 nPortIndex,
  186. OMX_IN OMX_PTR pAppPrivate,
  187. OMX_IN OMX_U32 nSizeBytes)
  188. {
  189. OMX_ERRORTYPE ret = OMX_ErrorNone;
  190. OMX_COMPONENTTYPE *pOMXComponent = (OMX_COMPONENTTYPE *)hComponent;
  191. SF_OMX_COMPONENT *pSfOMXComponent = pOMXComponent->pComponentPrivate;
  192. OMX_BUFFERHEADERTYPE *temp_bufferHeader = (OMX_BUFFERHEADERTYPE *)malloc(sizeof(OMX_BUFFERHEADERTYPE));
  193. if (temp_bufferHeader == NULL)
  194. {
  195. LOG(SF_LOG_ERR, "malloc fail\r\n");
  196. return OMX_ErrorInsufficientResources;
  197. }
  198. memset(temp_bufferHeader, 0, sizeof(OMX_BUFFERHEADERTYPE));
  199. temp_bufferHeader->pBuffer = malloc(nSizeBytes);
  200. if (temp_bufferHeader->pBuffer == NULL)
  201. {
  202. free(temp_bufferHeader);
  203. LOG(SF_LOG_ERR, "malloc fail\r\n");
  204. return OMX_ErrorInsufficientResources;
  205. }
  206. memset(temp_bufferHeader->pBuffer, 0, nSizeBytes);
  207. temp_bufferHeader->nAllocLen = nSizeBytes;
  208. temp_bufferHeader->pAppPrivate = pAppPrivate;
  209. *ppBuffer = temp_bufferHeader;
  210. ret = StoreOMXBuffer(pSfOMXComponent, temp_bufferHeader);
  211. LOG(SF_LOG_PERF, "buffer count = %d\r\n", GetOMXBufferCount(pSfOMXComponent));
  212. FunctionOut();
  213. return ret;
  214. }
  215. static OMX_ERRORTYPE SF_OMX_GetParameter(
  216. OMX_IN OMX_HANDLETYPE hComponent,
  217. OMX_IN OMX_INDEXTYPE nParamIndex,
  218. OMX_INOUT OMX_PTR ComponentParameterStructure)
  219. {
  220. OMX_ERRORTYPE ret = OMX_ErrorNone;
  221. OMX_COMPONENTTYPE *pOMXComponent = (OMX_COMPONENTTYPE *)hComponent;
  222. SF_OMX_COMPONENT *pSfOMXComponent = pOMXComponent->pComponentPrivate;
  223. FunctionIn();
  224. if (hComponent == NULL)
  225. {
  226. ret = OMX_ErrorBadParameter;
  227. goto EXIT;
  228. }
  229. switch ((OMX_U32)nParamIndex)
  230. {
  231. case OMX_IndexParamVideoInit:
  232. break;
  233. case OMX_IndexParamVideoPortFormat:
  234. {
  235. OMX_VIDEO_PARAM_PORTFORMATTYPE *portFormat = (OMX_VIDEO_PARAM_PORTFORMATTYPE *)ComponentParameterStructure;
  236. OMX_U32 index = portFormat->nIndex;
  237. switch (index)
  238. {
  239. case 0:
  240. portFormat->eCompressionFormat = OMX_VIDEO_CodingUnused;
  241. portFormat->eColorFormat = OMX_COLOR_FormatYUV420SemiPlanar;
  242. portFormat->xFramerate = 30;
  243. default:
  244. if (index > 0)
  245. {
  246. ret = OMX_ErrorNoMore;
  247. }
  248. }
  249. }
  250. break;
  251. case OMX_IndexParamVideoBitrate:
  252. break;
  253. case OMX_IndexParamVideoQuantization:
  254. break;
  255. case OMX_IndexParamPortDefinition:
  256. {
  257. OMX_PARAM_PORTDEFINITIONTYPE *pPortDefinition = (OMX_PARAM_PORTDEFINITIONTYPE *)ComponentParameterStructure;
  258. OMX_U32 portIndex = pPortDefinition->nPortIndex;
  259. memcpy(pPortDefinition, &pSfOMXComponent->portDefinition[portIndex], pPortDefinition->nSize);
  260. }
  261. break;
  262. case OMX_IndexParamVideoIntraRefresh:
  263. break;
  264. case OMX_IndexParamStandardComponentRole:
  265. break;
  266. case OMX_IndexParamVideoAvc:
  267. break;
  268. case OMX_IndexParamVideoHevc:
  269. break;
  270. case OMX_IndexParamVideoProfileLevelQuerySupported:
  271. break;
  272. default:
  273. break;
  274. }
  275. EXIT:
  276. FunctionOut();
  277. return ret;
  278. }
  279. static OMX_ERRORTYPE SF_OMX_SetParameter(
  280. OMX_IN OMX_HANDLETYPE hComponent,
  281. OMX_IN OMX_INDEXTYPE nIndex,
  282. OMX_IN OMX_PTR ComponentParameterStructure)
  283. {
  284. OMX_ERRORTYPE ret = OMX_ErrorNone;
  285. FunctionIn();
  286. if (hComponent == NULL)
  287. {
  288. ret = OMX_ErrorBadParameter;
  289. goto EXIT;
  290. }
  291. OMX_COMPONENTTYPE *pOMXComponent = (OMX_COMPONENTTYPE *)hComponent;
  292. SF_OMX_COMPONENT *pSfOMXComponent = pOMXComponent->pComponentPrivate;
  293. if (pSfOMXComponent == NULL)
  294. {
  295. ret = OMX_ErrorBadParameter;
  296. goto EXIT;
  297. }
  298. TestDecConfig *testConfig = pSfOMXComponent->testConfig;
  299. switch ((OMX_U32)nIndex)
  300. {
  301. case OMX_IndexParamVideoPortFormat:
  302. break;
  303. case OMX_IndexParamVideoBitrate:
  304. break;
  305. case OMX_IndexParamVideoQuantization:
  306. break;
  307. case OMX_IndexParamPortDefinition:
  308. {
  309. OMX_PARAM_PORTDEFINITIONTYPE *pPortDefinition = (OMX_PARAM_PORTDEFINITIONTYPE *)ComponentParameterStructure;
  310. OMX_PARAM_PORTDEFINITIONTYPE *pInputPort = &pSfOMXComponent->portDefinition[0];
  311. OMX_PARAM_PORTDEFINITIONTYPE *pOutputPort = &pSfOMXComponent->portDefinition[1];
  312. TestDecConfig *pTestDecConfig = (TestDecConfig *)pSfOMXComponent->testConfig;
  313. OMX_U32 portIndex = pPortDefinition->nPortIndex;
  314. OMX_U32 width = pPortDefinition->format.video.nFrameWidth;
  315. OMX_U32 height = pPortDefinition->format.video.nFrameHeight;
  316. LOG(SF_LOG_INFO, "Set width = %d, height = %d on port %d\r\n", width, height, pPortDefinition->nPortIndex);
  317. memcpy(&pSfOMXComponent->portDefinition[portIndex], pPortDefinition, pPortDefinition->nSize);
  318. if (portIndex == 0)
  319. {
  320. pInputPort->format.video.nStride = width;
  321. pInputPort->format.video.nSliceHeight = height;
  322. pInputPort->nBufferSize = width * height * 2;
  323. } else if (portIndex == 1)
  324. {
  325. if (width == 1)
  326. {
  327. width = pInputPort->format.video.nFrameWidth;
  328. }
  329. if (height == 1)
  330. {
  331. height = pInputPort->format.video.nFrameHeight;
  332. }
  333. int scalew = pInputPort->format.video.nFrameWidth / width;
  334. int scaleh = pInputPort->format.video.nFrameHeight / height;
  335. if (scalew > 8 || scaleh > 8 || scalew < 1 || scaleh < 1)
  336. {
  337. LOG(SF_LOG_ERR, "Scaling should be 1 to 1/8 (down-scaling only)!\r\n");
  338. ret = OMX_ErrorBadParameter;
  339. goto EXIT;
  340. }
  341. pOutputPort->format.video.nFrameWidth = width;
  342. pOutputPort->format.video.nFrameHeight = height;
  343. pOutputPort->format.video.nStride = width;
  344. pOutputPort->format.video.nSliceHeight = height;
  345. pTestDecConfig->scaleDownWidth = VPU_CEIL(width, 2);
  346. pTestDecConfig->scaleDownHeight = VPU_CEIL(height, 2);
  347. LOG(SF_LOG_INFO, "Set scale = %d, %d\r\n", pTestDecConfig->scaleDownWidth , pTestDecConfig->scaleDownHeight);
  348. switch (pOutputPort->format.video.eColorFormat)
  349. {
  350. case OMX_COLOR_FormatYUV420Planar:
  351. case OMX_COLOR_FormatYUV420SemiPlanar:
  352. if (width && height)
  353. pOutputPort->nBufferSize = (width * height * 3) / 2;
  354. break;
  355. default:
  356. if (width && height)
  357. pOutputPort->nBufferSize = width * height * 2;
  358. break;
  359. }
  360. }
  361. }
  362. break;
  363. case OMX_IndexParamVideoIntraRefresh:
  364. break;
  365. case OMX_IndexParamStandardComponentRole:
  366. break;
  367. case OMX_IndexParamVideoAvc:
  368. testConfig->bitFormat = 0;
  369. break;
  370. case OMX_IndexParamVideoHevc:
  371. testConfig->bitFormat = 12;
  372. break;
  373. case OMX_IndexConfigCommonInputCrop:
  374. memcpy(testConfig->inputPath, ComponentParameterStructure, strlen(ComponentParameterStructure));
  375. break;
  376. case OMX_IndexConfigCommonOutputCrop:
  377. memcpy(testConfig->outputPath, ComponentParameterStructure, strlen(ComponentParameterStructure));
  378. break;
  379. default:
  380. break;
  381. }
  382. EXIT:
  383. FunctionOut();
  384. return ret;
  385. }
  386. static OMX_ERRORTYPE SF_OMX_GetConfig(
  387. OMX_HANDLETYPE hComponent,
  388. OMX_INDEXTYPE nIndex,
  389. OMX_PTR pComponentConfigStructure)
  390. {
  391. OMX_ERRORTYPE ret = OMX_ErrorNone;
  392. FunctionIn();
  393. if (hComponent == NULL)
  394. {
  395. ret = OMX_ErrorBadParameter;
  396. goto EXIT;
  397. }
  398. switch (nIndex)
  399. {
  400. case OMX_IndexConfigVideoAVCIntraPeriod:
  401. break;
  402. case OMX_IndexConfigVideoBitrate:
  403. break;
  404. case OMX_IndexConfigVideoFramerate:
  405. break;
  406. default:
  407. break;
  408. }
  409. EXIT:
  410. FunctionOut();
  411. return ret;
  412. }
  413. static OMX_ERRORTYPE SF_OMX_SetConfig(
  414. OMX_HANDLETYPE hComponent,
  415. OMX_INDEXTYPE nIndex,
  416. OMX_PTR pComponentConfigStructure)
  417. {
  418. OMX_ERRORTYPE ret = OMX_ErrorNone;
  419. FunctionIn();
  420. if (hComponent == NULL)
  421. {
  422. ret = OMX_ErrorBadParameter;
  423. goto EXIT;
  424. }
  425. switch ((OMX_U32)nIndex)
  426. {
  427. case OMX_IndexConfigVideoAVCIntraPeriod:
  428. break;
  429. case OMX_IndexConfigVideoBitrate:
  430. break;
  431. case OMX_IndexConfigVideoFramerate:
  432. break;
  433. case OMX_IndexConfigVideoIntraVOPRefresh:
  434. break;
  435. default:
  436. break;
  437. }
  438. EXIT:
  439. FunctionOut();
  440. return ret;
  441. }
  442. static OMX_ERRORTYPE SF_OMX_GetExtensionIndex(
  443. OMX_IN OMX_HANDLETYPE hComponent,
  444. OMX_IN OMX_STRING cParameterName,
  445. OMX_OUT OMX_INDEXTYPE *pIndexType)
  446. {
  447. OMX_ERRORTYPE ret = OMX_ErrorNone;
  448. FunctionIn();
  449. FunctionOut();
  450. return ret;
  451. }
  452. static OMX_ERRORTYPE SF_OMX_GetComponentVersion(
  453. OMX_IN OMX_HANDLETYPE hComponent,
  454. OMX_OUT OMX_STRING pComponentName,
  455. OMX_OUT OMX_VERSIONTYPE *pComponentVersion,
  456. OMX_OUT OMX_VERSIONTYPE *pSpecVersion,
  457. OMX_OUT OMX_UUIDTYPE *pComponentUUID)
  458. {
  459. OMX_ERRORTYPE ret = OMX_ErrorNone;
  460. FunctionIn();
  461. FunctionOut();
  462. return ret;
  463. }
  464. static OMX_ERRORTYPE InitDecoder(SF_OMX_COMPONENT *pSfOMXComponent)
  465. {
  466. TestDecConfig *testConfig = NULL;
  467. CNMComponentConfig *config = NULL;
  468. Uint32 sizeInWord;
  469. char *fwPath = NULL;
  470. if (pSfOMXComponent->hSFComponentExecoder != NULL)
  471. {
  472. return OMX_ErrorNone;
  473. }
  474. FunctionIn();
  475. testConfig = (TestDecConfig *)pSfOMXComponent->testConfig;
  476. testConfig->productId = (ProductId)pSfOMXComponent->functions->VPU_GetProductId(testConfig->coreIdx);
  477. if (CheckDecTestConfig(testConfig) == FALSE)
  478. {
  479. LOG(SF_LOG_ERR, "fail to CheckTestConfig()\n");
  480. return OMX_ErrorBadParameter;
  481. }
  482. switch (testConfig->productId)
  483. {
  484. case PRODUCT_ID_521:
  485. fwPath = pSfOMXComponent->fwPath;
  486. break;
  487. case PRODUCT_ID_511:
  488. fwPath = pSfOMXComponent->fwPath;
  489. break;
  490. case PRODUCT_ID_517:
  491. fwPath = CORE_7_BIT_CODE_FILE_PATH;
  492. break;
  493. default:
  494. LOG(SF_LOG_ERR, "Unknown product id: %d, whether kernel module loaded?\n", testConfig->productId);
  495. return OMX_ErrorBadParameter;
  496. }
  497. LOG(SF_LOG_INFO, "FW PATH = %s\n", fwPath);
  498. if (pSfOMXComponent->functions->LoadFirmware(testConfig->productId, (Uint8 **)&(pSfOMXComponent->pusBitCode), &sizeInWord, fwPath) < 0)
  499. {
  500. LOG(SF_LOG_ERR, "Failed to load firmware: %s\n", fwPath);
  501. return OMX_ErrorInsufficientResources;
  502. }
  503. config = pSfOMXComponent->config;
  504. memcpy(&(config->testDecConfig), testConfig, sizeof(TestDecConfig));
  505. config->bitcode = (Uint8 *)pSfOMXComponent->pusBitCode;
  506. config->sizeOfBitcode = sizeInWord;
  507. if (pSfOMXComponent->functions->SetUpDecoderOpenParam(&config->decOpenParam, &config->testDecConfig) != RETCODE_SUCCESS)
  508. {
  509. LOG(SF_LOG_ERR, "SetupDecoderOpenParam error\n");
  510. return OMX_ErrorBadParameter;
  511. }
  512. pSfOMXComponent->hSFComponentExecoder = pSfOMXComponent->functions->ComponentCreate("wave_decoder", config);
  513. pSfOMXComponent->hSFComponentFeeder = pSfOMXComponent->functions->ComponentCreate("feeder", config);
  514. pSfOMXComponent->hSFComponentRender = pSfOMXComponent->functions->ComponentCreate("renderer", config);
  515. ComponentImpl *pFeederComponent = (ComponentImpl *)pSfOMXComponent->hSFComponentFeeder;
  516. pSfOMXComponent->functions->ComponentPortCreate(&pFeederComponent->srcPort, pSfOMXComponent->hSFComponentFeeder, 5, sizeof(PortContainerExternal));
  517. ComponentImpl *pRenderComponent = (ComponentImpl *)pSfOMXComponent->hSFComponentRender;
  518. pSfOMXComponent->functions->ComponentPortDestroy(&pRenderComponent->sinkPort);
  519. pSfOMXComponent->functions->ComponentPortCreate(&pRenderComponent->sinkPort, pSfOMXComponent->hSFComponentRender, 5, sizeof(PortContainerExternal));
  520. if (pSfOMXComponent->functions->SetupDecListenerContext(pSfOMXComponent->lsnCtx, config, NULL) == TRUE)
  521. {
  522. pSfOMXComponent->functions->ComponentRegisterListener(pSfOMXComponent->hSFComponentExecoder,
  523. COMPONENT_EVENT_DEC_ALL, pSfOMXComponent->functions->DecoderListener, (void *)pSfOMXComponent->lsnCtx);
  524. pSfOMXComponent->functions->ComponentRegisterListener(pSfOMXComponent->hSFComponentExecoder,
  525. COMPONENT_EVENT_DEC_REGISTER_FB, OnEventArrived, (void *)pSfOMXComponent->lsnCtx);
  526. pSfOMXComponent->functions->ComponentRegisterListener(pSfOMXComponent->hSFComponentExecoder,
  527. COMPONENT_EVENT_DEC_DECODED_ALL, OnEventArrived, (void *)pSfOMXComponent->lsnCtx);
  528. pSfOMXComponent->functions->ComponentRegisterListener(pSfOMXComponent->hSFComponentFeeder,
  529. COMPONENT_EVENT_DEC_ALL, OnEventArrived, (void *)pSfOMXComponent->lsnCtx);
  530. pSfOMXComponent->functions->ComponentRegisterListener(pSfOMXComponent->hSFComponentRender,
  531. COMPONENT_EVENT_DEC_ALL, OnEventArrived, (void *)pSfOMXComponent->lsnCtx);
  532. }
  533. else
  534. {
  535. LOG(SF_LOG_ERR, "ComponentRegisterListener fail\r\n");
  536. return OMX_ErrorBadParameter;
  537. }
  538. pSfOMXComponent->functions->ComponentSetupTunnel(pSfOMXComponent->hSFComponentFeeder, pSfOMXComponent->hSFComponentExecoder);
  539. pSfOMXComponent->functions->ComponentSetupTunnel(pSfOMXComponent->hSFComponentExecoder, pSfOMXComponent->hSFComponentRender);
  540. FunctionOut();
  541. return OMX_ErrorNone;
  542. }
  543. static OMX_ERRORTYPE SF_OMX_SendCommand(
  544. OMX_IN OMX_HANDLETYPE hComponent,
  545. OMX_IN OMX_COMMANDTYPE Cmd,
  546. OMX_IN OMX_U32 nParam,
  547. OMX_IN OMX_PTR pCmdData)
  548. {
  549. OMX_ERRORTYPE ret = OMX_ErrorNone;
  550. OMX_COMPONENTTYPE *pOMXComponent = (OMX_COMPONENTTYPE *)hComponent;
  551. SF_OMX_COMPONENT *pSfOMXComponent = pOMXComponent->pComponentPrivate;
  552. ComponentImpl *pSFComponentDecoder = NULL;
  553. ComponentImpl *pSFComponentFeeder = NULL;
  554. ComponentImpl *pSFComponentRender = NULL;
  555. ComponentState componentState;
  556. FunctionIn();
  557. if (hComponent == NULL || pSfOMXComponent == NULL)
  558. {
  559. ret = OMX_ErrorBadParameter;
  560. goto EXIT;
  561. }
  562. switch (Cmd)
  563. {
  564. case OMX_CommandStateSet:
  565. switch (nParam)
  566. {
  567. case OMX_StateIdle:
  568. ret = InitDecoder(pSfOMXComponent);
  569. if (ret != OMX_ErrorNone)
  570. {
  571. goto EXIT;
  572. }
  573. pSFComponentDecoder = (ComponentImpl *)pSfOMXComponent->hSFComponentExecoder;
  574. pSFComponentFeeder = (ComponentImpl *)pSfOMXComponent->hSFComponentFeeder;
  575. pSFComponentRender = (ComponentImpl *)pSfOMXComponent->hSFComponentRender;
  576. pSFComponentDecoder->pause = OMX_TRUE;
  577. pSFComponentFeeder->pause = OMX_TRUE;
  578. pSFComponentRender->pause = OMX_TRUE;
  579. pSfOMXComponent->callbacks->EventHandler(pSfOMXComponent->pOMXComponent, pSfOMXComponent->pAppData,
  580. OMX_EventCmdComplete, OMX_CommandStateSet, nParam, NULL);
  581. break;
  582. case OMX_StateExecuting:
  583. ret = InitDecoder(pSfOMXComponent);
  584. if (ret != OMX_ErrorNone)
  585. {
  586. goto EXIT;
  587. }
  588. pSFComponentDecoder = (ComponentImpl *)pSfOMXComponent->hSFComponentExecoder;
  589. pSFComponentFeeder = (ComponentImpl *)pSfOMXComponent->hSFComponentFeeder;
  590. pSFComponentRender = (ComponentImpl *)pSfOMXComponent->hSFComponentRender;
  591. pSFComponentDecoder->pause = OMX_FALSE;
  592. if (pSFComponentDecoder->thread == NULL)
  593. {
  594. LOG(SF_LOG_INFO, "execute component %s\r\n", pSFComponentDecoder->name);
  595. componentState = pSfOMXComponent->functions->ComponentExecute(pSFComponentDecoder);
  596. LOG(SF_LOG_INFO, "ret = %d\r\n", componentState);
  597. }
  598. if (pSFComponentFeeder->thread == NULL)
  599. {
  600. LOG(SF_LOG_INFO, "execute component %s\r\n", pSFComponentFeeder->name);
  601. componentState = pSfOMXComponent->functions->ComponentExecute(pSFComponentFeeder);
  602. LOG(SF_LOG_INFO, "ret = %d\r\n", componentState);
  603. }
  604. if (pSFComponentRender->thread == NULL)
  605. {
  606. LOG(SF_LOG_INFO, "execute component %s\r\n", pSFComponentRender->name);
  607. componentState = pSfOMXComponent->functions->ComponentExecute(pSFComponentRender);
  608. LOG(SF_LOG_INFO, "ret = %d\r\n", componentState);
  609. }
  610. break;
  611. default:
  612. break;
  613. }
  614. pSfOMXComponent->callbacks->EventHandler(pSfOMXComponent->pOMXComponent, pSfOMXComponent->pAppData,
  615. OMX_EventCmdComplete, OMX_CommandStateSet, nParam, NULL);
  616. break;
  617. case OMX_CommandFlush:
  618. pSfOMXComponent->callbacks->EventHandler(pSfOMXComponent->pOMXComponent, pSfOMXComponent->pAppData,
  619. OMX_EventCmdComplete, OMX_CommandFlush, nParam, NULL);
  620. break;
  621. case OMX_CommandPortDisable:
  622. pSfOMXComponent->callbacks->EventHandler(pSfOMXComponent->pOMXComponent, pSfOMXComponent->pAppData,
  623. OMX_EventCmdComplete, OMX_CommandPortDisable, nParam, NULL);
  624. break;
  625. case OMX_CommandPortEnable:
  626. pSfOMXComponent->callbacks->EventHandler(pSfOMXComponent->pOMXComponent, pSfOMXComponent->pAppData,
  627. OMX_EventCmdComplete, OMX_CommandPortEnable, nParam, NULL);
  628. break;
  629. case OMX_CommandMarkBuffer:
  630. break;
  631. default:
  632. break;
  633. }
  634. EXIT:
  635. FunctionOut();
  636. return ret;
  637. }
  638. static OMX_ERRORTYPE SF_OMX_GetState(
  639. OMX_IN OMX_HANDLETYPE hComponent,
  640. OMX_OUT OMX_STATETYPE *pState)
  641. {
  642. OMX_ERRORTYPE ret = OMX_ErrorNone;
  643. FunctionIn();
  644. GetStateCommon(hComponent, pState);
  645. FunctionOut();
  646. return ret;
  647. }
  648. static OMX_ERRORTYPE SF_OMX_FreeBuffer(
  649. OMX_IN OMX_HANDLETYPE hComponent,
  650. OMX_IN OMX_U32 nPortIndex,
  651. OMX_IN OMX_BUFFERHEADERTYPE *pBufferHdr)
  652. {
  653. OMX_ERRORTYPE ret = OMX_ErrorNone;
  654. OMX_COMPONENTTYPE *pOMXComponent = (OMX_COMPONENTTYPE *)hComponent;
  655. SF_OMX_COMPONENT *pSfOMXComponent = pOMXComponent->pComponentPrivate;
  656. FunctionIn();
  657. ClearOMXBuffer(pSfOMXComponent, pBufferHdr);
  658. LOG(SF_LOG_PERF, "buffer count = %d\r\n", GetOMXBufferCount(pSfOMXComponent));
  659. free(pBufferHdr);
  660. FunctionOut();
  661. return ret;
  662. }
  663. static OMX_ERRORTYPE SF_OMX_UseEGLImage(
  664. OMX_IN OMX_HANDLETYPE hComponent,
  665. OMX_INOUT OMX_BUFFERHEADERTYPE **ppBufferHdr,
  666. OMX_IN OMX_U32 nPortIndex,
  667. OMX_IN OMX_PTR pAppPrivate,
  668. OMX_IN void *eglImage)
  669. {
  670. (void)hComponent;
  671. (void)ppBufferHdr;
  672. (void)nPortIndex;
  673. (void)pAppPrivate;
  674. (void)eglImage;
  675. return OMX_ErrorNotImplemented;
  676. }
  677. static OMX_ERRORTYPE SF_OMX_ComponentConstructor(SF_OMX_COMPONENT *hComponent)
  678. {
  679. OMX_ERRORTYPE ret = OMX_ErrorNone;
  680. FunctionIn();
  681. ret = InitComponentStructorCommon(hComponent);
  682. if (ret != OMX_ErrorNone)
  683. {
  684. goto EXIT;
  685. }
  686. TestDecConfig *pTestDecConfig = (TestDecConfig *)hComponent->testConfig;
  687. pTestDecConfig->feedingMode = FEEDING_METHOD_BUFFER;
  688. pTestDecConfig->bitstreamMode = BS_MODE_PIC_END;
  689. pTestDecConfig->bitFormat = hComponent->bitFormat;
  690. /*
  691. if cbcrInterleave is FALSE and nv21 is FALSE, the default dec format is I420
  692. if cbcrInterleave is TRUE and nv21 is FALSE, then the dec format is NV12
  693. if cbcrInterleave is TRUE and nv21 is TRUE, then the dec format is NV21
  694. */
  695. pTestDecConfig->cbcrInterleave = TRUE;
  696. pTestDecConfig->nv21 = FALSE;
  697. hComponent->pOMXComponent->UseBuffer = &SF_OMX_UseBuffer;
  698. hComponent->pOMXComponent->AllocateBuffer = &SF_OMX_AllocateBuffer;
  699. hComponent->pOMXComponent->EmptyThisBuffer = &SF_OMX_EmptyThisBuffer;
  700. hComponent->pOMXComponent->FillThisBuffer = &SF_OMX_FillThisBuffer;
  701. hComponent->pOMXComponent->FreeBuffer = &SF_OMX_FreeBuffer;
  702. // hComponent->pOMXComponent->ComponentTunnelRequest = &SF_OMX_ComponentTunnelRequest;
  703. hComponent->pOMXComponent->GetParameter = &SF_OMX_GetParameter;
  704. hComponent->pOMXComponent->SetParameter = &SF_OMX_SetParameter;
  705. hComponent->pOMXComponent->GetConfig = &SF_OMX_GetConfig;
  706. hComponent->pOMXComponent->SetConfig = &SF_OMX_SetConfig;
  707. hComponent->pOMXComponent->SendCommand = &SF_OMX_SendCommand;
  708. hComponent->pOMXComponent->GetState = &SF_OMX_GetState;
  709. // hComponent->pOMXComponent->GetExtensionIndex = &SF_OMX_GetExtensionIndex;
  710. // hComponent->pOMXComponent->ComponentRoleEnum = &SF_OMX_ComponentRoleEnum;
  711. // hComponent->pOMXComponent->ComponentDeInit = &SF_OMX_ComponentDeInit;
  712. hComponent->inputSemaphore = (sem_t *)malloc(sizeof(sem_t));
  713. if (hComponent->inputSemaphore == NULL)
  714. {
  715. LOG(SF_LOG_ERR, "malloc fail\r\n");
  716. ret = OMX_ErrorInsufficientResources;
  717. goto EXIT;
  718. }
  719. sem_init(hComponent->inputSemaphore, 0, 5);
  720. EXIT:
  721. FunctionOut();
  722. return ret;
  723. }
  724. static OMX_ERRORTYPE SF_OMX_ComponentClear(SF_OMX_COMPONENT *hComponent)
  725. {
  726. OMX_ERRORTYPE ret = OMX_ErrorNone;
  727. ComponentImpl *pSFComponentDecoder = (ComponentImpl *)hComponent->hSFComponentExecoder;
  728. ComponentImpl *pSFComponentFeeder = (ComponentImpl *)hComponent->hSFComponentFeeder;
  729. ComponentImpl *pSFComponentRender = (ComponentImpl *)hComponent->hSFComponentRender;
  730. FunctionIn();
  731. if (pSFComponentDecoder == NULL || pSFComponentFeeder == NULL || pSFComponentRender == NULL)
  732. {
  733. goto EXIT;
  734. }
  735. pSFComponentDecoder->terminate = OMX_TRUE;
  736. pSFComponentFeeder->terminate = OMX_TRUE;
  737. pSFComponentRender->terminate = OMX_TRUE;
  738. hComponent->functions->ComponentWait(hComponent->hSFComponentFeeder);
  739. hComponent->functions->ComponentWait(hComponent->hSFComponentExecoder);
  740. hComponent->functions->ComponentWait(hComponent->hSFComponentRender);
  741. free(hComponent->pusBitCode);
  742. hComponent->functions->ClearDecListenerContext(hComponent->lsnCtx);
  743. ComponentClearCommon(hComponent);
  744. EXIT:
  745. FunctionOut();
  746. return ret;
  747. }
  748. SF_OMX_COMPONENT sf_dec_decoder_h265 = {
  749. .componentName = "sf.dec.decoder.h265",
  750. .libName = "libsfdec.so",
  751. .pOMXComponent = NULL,
  752. .SF_OMX_ComponentConstructor = SF_OMX_ComponentConstructor,
  753. .SF_OMX_ComponentClear = SF_OMX_ComponentClear,
  754. .functions = NULL,
  755. .bitFormat = STD_HEVC,
  756. .fwPath = "/lib/firmware/chagall.bin"};
  757. SF_OMX_COMPONENT sf_dec_decoder_h264 = {
  758. .componentName = "sf.dec.decoder.h264",
  759. .libName = "libsfdec.so",
  760. .pOMXComponent = NULL,
  761. .SF_OMX_ComponentConstructor = SF_OMX_ComponentConstructor,
  762. .SF_OMX_ComponentClear = SF_OMX_ComponentClear,
  763. .functions = NULL,
  764. .bitFormat = STD_AVC,
  765. .fwPath = "/lib/firmware/chagall.bin"};