SF_OMX_Vdec_decoder.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213
  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 char* Event2Str(unsigned long event)
  12. {
  13. char *event_str = NULL;
  14. switch (event)
  15. {
  16. case COMPONENT_EVENT_DEC_OPEN:
  17. event_str = "COMPONENT_EVENT_DEC_OPEN";
  18. break;
  19. case COMPONENT_EVENT_DEC_ISSUE_SEQ:
  20. event_str = "COMPONENT_EVENT_DEC_ISSUE_SEQ";
  21. break;
  22. case COMPONENT_EVENT_DEC_COMPLETE_SEQ:
  23. event_str = "COMPONENT_EVENT_DEC_COMPLETE_SEQ";
  24. break;
  25. case COMPONENT_EVENT_DEC_REGISTER_FB:
  26. event_str = "COMPONENT_EVENT_DEC_REGISTER_FB";
  27. break;
  28. case COMPONENT_EVENT_DEC_READY_ONE_FRAME:
  29. event_str = "COMPONENT_EVENT_DEC_READY_ONE_FRAME";
  30. break;
  31. case COMPONENT_EVENT_DEC_START_ONE_FRAME:
  32. event_str = "COMPONENT_EVENT_DEC_START_ONE_FRAME";
  33. break;
  34. case COMPONENT_EVENT_DEC_INTERRUPT:
  35. event_str = "COMPONENT_EVENT_DEC_INTERRUPT";
  36. break;
  37. case COMPONENT_EVENT_DEC_GET_OUTPUT_INFO:
  38. event_str = "COMPONENT_EVENT_DEC_GET_OUTPUT_INFO";
  39. break;
  40. case COMPONENT_EVENT_DEC_DECODED_ALL:
  41. event_str = "COMPONENT_EVENT_DEC_DECODED_ALL";
  42. break;
  43. case COMPONENT_EVENT_DEC_CLOSE:
  44. event_str = "COMPONENT_EVENT_DEC_CLOSE";
  45. break;
  46. case COMPONENT_EVENT_DEC_RESET_DONE:
  47. event_str = "COMPONENT_EVENT_DEC_RESET_DONE";
  48. break;
  49. case COMPONENT_EVENT_DEC_EMPTY_BUFFER_DONE:
  50. event_str = "COMPONENT_EVENT_DEC_EMPTY_BUFFER_DONE";
  51. break;
  52. case COMPONENT_EVENT_DEC_FILL_BUFFER_DONE:
  53. event_str = "COMPONENT_EVENT_DEC_FILL_BUFFER_DONE";
  54. break;
  55. case COMPONENT_EVENT_DEC_ALL:
  56. event_str = "COMPONENT_EVENT_DEC_ALL";
  57. break;
  58. }
  59. return event_str;
  60. }
  61. static void OnEventArrived(Component com, unsigned long event, void *data, void *context)
  62. {
  63. PortContainerExternal *pPortContainerExternal = (PortContainerExternal *)data;
  64. OMX_BUFFERHEADERTYPE *pOMXBuffer;
  65. static OMX_U32 dec_cnt = 0;
  66. static struct timeval tv_old = {0};
  67. OMX_U32 fps = 0;
  68. OMX_U64 diff_time = 0; // ms
  69. FunctionIn();
  70. SF_OMX_COMPONENT *pSfOMXComponent = GetSFOMXComponrntByComponent(com);
  71. SF_WAVE5_IMPLEMEMT *pSfVideoImplement = (SF_WAVE5_IMPLEMEMT *)pSfOMXComponent->componentImpl;
  72. char *event_str = Event2Str(event);
  73. LOG(SF_LOG_INFO, "event=%lX %s\r\n", event, event_str);
  74. switch (event)
  75. {
  76. case COMPONENT_EVENT_DEC_EMPTY_BUFFER_DONE:
  77. pOMXBuffer = GetOMXBufferByAddr(pSfOMXComponent, (OMX_U8 *)pPortContainerExternal->pBuffer);
  78. if (pOMXBuffer == NULL)
  79. {
  80. LOG(SF_LOG_WARN, "Could not find omx buffer by address\r\n");
  81. return;
  82. }
  83. pSfOMXComponent->callbacks->EmptyBufferDone(pSfOMXComponent->pOMXComponent, pSfOMXComponent->pAppData, pOMXBuffer);
  84. break;
  85. case COMPONENT_EVENT_DEC_FILL_BUFFER_DONE:
  86. {
  87. struct timeval tv;
  88. /*Before each execution of getomxbufferbyaddr, check whether there are still members in the queue*/
  89. PortContainerExternal *output = (PortContainerExternal*)pSfVideoImplement->functions->ComponentPortPeekData(&((ComponentImpl *)com)->sinkPort);
  90. if (output == NULL) {
  91. return;
  92. }
  93. pOMXBuffer = GetOMXBufferByAddr(pSfOMXComponent, (OMX_U8 *)pPortContainerExternal->pBuffer);
  94. if (pOMXBuffer == NULL)
  95. {
  96. LOG(SF_LOG_WARN, "Could not find omx buffer by address\r\n");
  97. return;
  98. }
  99. gettimeofday(&tv, NULL);
  100. if (gInitTimeStamp == 0)
  101. {
  102. gInitTimeStamp = tv.tv_sec * 1000000 + tv.tv_usec;
  103. }
  104. pOMXBuffer->nFilledLen = pPortContainerExternal->nFilledLen;
  105. pOMXBuffer->nTimeStamp = tv.tv_sec * 1000000 + tv.tv_usec - gInitTimeStamp;
  106. #if 0
  107. {
  108. FILE *fb = fopen("./out.bcp", "ab+");
  109. LOG(SF_LOG_INFO, "%p %d\r\n", pOMXBuffer->pBuffer, pOMXBuffer->nFilledLen);
  110. fwrite(pOMXBuffer->pBuffer, 1, pOMXBuffer->nFilledLen, fb);
  111. fclose(fb);
  112. }
  113. #endif
  114. // Following is to print the decoding fps
  115. if (dec_cnt == 0) {
  116. tv_old = tv;
  117. }
  118. if (dec_cnt++ >= 50) {
  119. diff_time = (tv.tv_sec - tv_old.tv_sec) * 1000 + (tv.tv_usec - tv_old.tv_usec) / 1000;
  120. fps = 1000 * (dec_cnt - 1) / diff_time;
  121. dec_cnt = 0;
  122. LOG(SF_LOG_WARN, "Decoding fps: %d \r\n", fps);
  123. }
  124. LOG(SF_LOG_PERF, "OMX finish one buffer, address = %p, size = %d, nTimeStamp = %d, nFlags = %X\r\n",
  125. pOMXBuffer->pBuffer, pOMXBuffer->nFilledLen, pOMXBuffer->nTimeStamp, pOMXBuffer->nFlags);
  126. LOG(SF_LOG_INFO, "indexFrameDisplay = %d, OMXBuferFlag = %d, OMXBufferAddr = %p\r\n",
  127. pPortContainerExternal->nFlags, (OMX_U32)pOMXBuffer->pOutputPortPrivate, pOMXBuffer->pBuffer);
  128. ComponentImpl *pRendererComponent = (ComponentImpl *)pSfVideoImplement->hSFComponentRender;
  129. LOG(SF_LOG_PERF, "output queue count=%d/%d\r\n", pSfVideoImplement->functions->Queue_Get_Cnt(pRendererComponent->sinkPort.inputQ),
  130. pSfOMXComponent->portDefinition[1].nBufferCountActual);
  131. pSfOMXComponent->callbacks->FillBufferDone(pSfOMXComponent->pOMXComponent, pSfOMXComponent->pAppData, pOMXBuffer);
  132. }
  133. break;
  134. case COMPONENT_EVENT_DEC_REGISTER_FB:
  135. {
  136. /*The width and height vpu return will align to 16, eg: 1080 will be 1088. so check scale value at first.*/
  137. TestDecConfig *testConfig = (TestDecConfig *)pSfVideoImplement->testConfig;
  138. OMX_U32 nWidth = testConfig->scaleDownWidth;
  139. OMX_U32 nHeight = testConfig->scaleDownHeight;
  140. if (nWidth <= 0 || nHeight <= 0)
  141. {
  142. /*If scale value not set, then get value from vpu*/
  143. FrameBuffer *frameBuffer = (FrameBuffer *)data;
  144. nWidth = frameBuffer->width;
  145. nHeight = frameBuffer->height;
  146. }
  147. LOG(SF_LOG_INFO, "Get Output width = %d, height = %d\r\n", nWidth, nHeight);
  148. pSfOMXComponent->portDefinition[1].format.video.nFrameWidth = nWidth;
  149. pSfOMXComponent->portDefinition[1].format.video.nFrameHeight = nHeight;
  150. pSfOMXComponent->portDefinition[1].format.video.nStride = nWidth;
  151. pSfOMXComponent->portDefinition[1].format.video.nSliceHeight = nHeight;
  152. /*Caculate buffer size by eColorFormat*/
  153. switch (pSfOMXComponent->portDefinition[1].format.video.eColorFormat)
  154. {
  155. case OMX_COLOR_FormatYUV420Planar:
  156. case OMX_COLOR_FormatYUV420SemiPlanar:
  157. case OMX_COLOR_FormatYUV420PackedSemiPlanar:
  158. case OMX_COLOR_FormatYVU420PackedSemiPlanar:
  159. case OMX_COLOR_FormatYVU420PackedPlanar:
  160. if (nWidth && nHeight) {
  161. pSfOMXComponent->portDefinition[1].nBufferSize = (nWidth * nHeight * 3) / 2;
  162. }
  163. break;
  164. default:
  165. if (nWidth && nHeight) {
  166. pSfOMXComponent->portDefinition[1].nBufferSize = nWidth * nHeight * 2;
  167. }
  168. break;
  169. }
  170. pSfOMXComponent->callbacks->EventHandler(pSfOMXComponent->pOMXComponent, pSfOMXComponent->pAppData, OMX_EventPortSettingsChanged,
  171. 1, OMX_IndexParamPortDefinition, NULL);
  172. }
  173. break;
  174. case COMPONENT_EVENT_DEC_DECODED_ALL:
  175. pSfOMXComponent->callbacks->EventHandler(pSfOMXComponent->pOMXComponent, pSfOMXComponent->pAppData, OMX_EventBufferFlag,
  176. 1, 1, NULL);
  177. default:
  178. break;
  179. }
  180. FunctionOut();
  181. }
  182. static OMX_ERRORTYPE SF_OMX_EmptyThisBuffer(
  183. OMX_IN OMX_HANDLETYPE hComponent,
  184. OMX_IN OMX_BUFFERHEADERTYPE *pBuffer)
  185. {
  186. OMX_ERRORTYPE ret = OMX_ErrorNone;
  187. FunctionIn();
  188. if (hComponent == NULL || pBuffer == NULL)
  189. {
  190. ret = OMX_ErrorBadParameter;
  191. goto EXIT;
  192. }
  193. OMX_COMPONENTTYPE *pOMXComponent = (OMX_COMPONENTTYPE *)hComponent;
  194. SF_OMX_COMPONENT *pSfOMXComponent = pOMXComponent->pComponentPrivate;
  195. SF_WAVE5_IMPLEMEMT *pSfVideoImplement = (SF_WAVE5_IMPLEMEMT *)pSfOMXComponent->componentImpl;
  196. ComponentImpl *pFeederComponent = (ComponentImpl *)pSfVideoImplement->hSFComponentFeeder;
  197. PortContainerExternal *pPortContainerExternal = malloc(sizeof(PortContainerExternal));
  198. if (pPortContainerExternal == NULL)
  199. {
  200. LOG(SF_LOG_ERR, "malloc fail\r\n");
  201. return OMX_ErrorInsufficientResources;
  202. }
  203. memset(pPortContainerExternal, 0, sizeof(PortContainerExternal));
  204. pPortContainerExternal->pBuffer = pBuffer->pBuffer;
  205. pPortContainerExternal->nFilledLen = pBuffer->nFilledLen;
  206. pPortContainerExternal->nFlags = pBuffer->nFlags;
  207. if (pSfVideoImplement->functions->Queue_Enqueue(pFeederComponent->srcPort.inputQ, (void *)pPortContainerExternal) != OMX_TRUE)
  208. {
  209. LOG(SF_LOG_ERR, "%p:%p FAIL\r\n", pFeederComponent->srcPort.inputQ, pPortContainerExternal);
  210. free(pPortContainerExternal);
  211. return OMX_ErrorInsufficientResources;
  212. }
  213. LOG(SF_LOG_PERF, "input queue count=%d/%d\r\n", pSfVideoImplement->functions->Queue_Get_Cnt(pFeederComponent->srcPort.inputQ),
  214. pSfOMXComponent->portDefinition[0].nBufferCountActual);
  215. LOG(SF_LOG_INFO, "input buffer address = %p, size = %d, flag = %x\r\n", pBuffer->pBuffer, pBuffer->nFilledLen, pBuffer->nFlags);
  216. free(pPortContainerExternal);
  217. pFeederComponent->pause = OMX_FALSE;
  218. EXIT:
  219. FunctionOut();
  220. return ret;
  221. }
  222. #define MAX_INDEX 1
  223. static OMX_U64 frame_array[MAX_INDEX] = {-1};
  224. static int frame_array_index = 0;
  225. static OMX_ERRORTYPE SF_OMX_FillThisBuffer(
  226. OMX_IN OMX_HANDLETYPE hComponent,
  227. OMX_IN OMX_BUFFERHEADERTYPE *pBuffer)
  228. {
  229. OMX_ERRORTYPE ret = OMX_ErrorNone;
  230. FunctionIn();
  231. if (hComponent == NULL || pBuffer == NULL)
  232. {
  233. ret = OMX_ErrorBadParameter;
  234. goto EXIT;
  235. }
  236. OMX_COMPONENTTYPE *pOMXComponent = (OMX_COMPONENTTYPE *)hComponent;
  237. SF_OMX_COMPONENT *pSfOMXComponent = (SF_OMX_COMPONENT *)pOMXComponent->pComponentPrivate;
  238. SF_WAVE5_IMPLEMEMT *pSfVideoImplement = (SF_WAVE5_IMPLEMEMT *)pSfOMXComponent->componentImpl;
  239. ComponentImpl *pRendererComponent = (ComponentImpl *)pSfVideoImplement->hSFComponentRender;
  240. PortContainerExternal *pPortContainerExternal = malloc(sizeof(PortContainerExternal));
  241. if (pPortContainerExternal == NULL)
  242. {
  243. LOG(SF_LOG_ERR, "malloc fail\r\n");
  244. return OMX_ErrorInsufficientResources;
  245. }
  246. memset(pPortContainerExternal, 0, sizeof(PortContainerExternal));
  247. pPortContainerExternal->pBuffer = pBuffer->pBuffer;
  248. pPortContainerExternal->nFilledLen = pBuffer->nAllocLen;
  249. if (gInitTimeStamp != 0)
  250. {
  251. int clear = frame_array[frame_array_index];
  252. pSfVideoImplement->functions->Render_DecClrDispFlag(pRendererComponent->context, clear);
  253. frame_array[frame_array_index] = (OMX_U64)(pBuffer->pOutputPortPrivate);
  254. LOG(SF_LOG_INFO, "store display flag: %d, clear display flag: %d\r\n", frame_array[frame_array_index], clear);
  255. frame_array_index ++;
  256. }
  257. if (frame_array_index == MAX_INDEX) frame_array_index = 0;
  258. if (pSfVideoImplement->functions->Queue_Enqueue(pRendererComponent->sinkPort.inputQ, (void *)pPortContainerExternal) == FALSE)
  259. {
  260. LOG(SF_LOG_ERR, "%p:%p FAIL\r\n", pRendererComponent->sinkPort.inputQ, pPortContainerExternal);
  261. free(pPortContainerExternal);
  262. return OMX_ErrorInsufficientResources;
  263. }
  264. LOG(SF_LOG_PERF, "output queue count=%d/%d\r\n", pSfVideoImplement->functions->Queue_Get_Cnt(pRendererComponent->sinkPort.inputQ),
  265. pSfOMXComponent->portDefinition[1].nBufferCountActual);
  266. free(pPortContainerExternal);
  267. pRendererComponent->pause = OMX_FALSE;
  268. EXIT:
  269. FunctionOut();
  270. return ret;
  271. }
  272. static OMX_U64 nOutBufIndex = 0;
  273. static OMX_ERRORTYPE SF_OMX_UseBuffer(
  274. OMX_IN OMX_HANDLETYPE hComponent,
  275. OMX_INOUT OMX_BUFFERHEADERTYPE **ppBufferHdr,
  276. OMX_IN OMX_U32 nPortIndex,
  277. OMX_IN OMX_PTR pAppPrivate,
  278. OMX_IN OMX_U32 nSizeBytes,
  279. OMX_IN OMX_U8 *pBuffer)
  280. {
  281. OMX_ERRORTYPE ret = OMX_ErrorNone;
  282. OMX_COMPONENTTYPE *pOMXComponent = (OMX_COMPONENTTYPE *)hComponent;
  283. SF_OMX_COMPONENT *pSfOMXComponent = pOMXComponent->pComponentPrivate;
  284. SF_WAVE5_IMPLEMEMT *pSfVideoImplement = (SF_WAVE5_IMPLEMEMT *)pSfOMXComponent->componentImpl;
  285. ComponentImpl *pComponentRender = (ComponentImpl *)pSfVideoImplement->hSFComponentRender;
  286. FunctionIn();
  287. if (hComponent == NULL)
  288. {
  289. ret = OMX_ErrorBadParameter;
  290. goto EXIT;
  291. }
  292. if (pSfVideoImplement->functions->AttachDMABuffer(pComponentRender, (Uint64)pBuffer, nSizeBytes) == FALSE)
  293. {
  294. LOG(SF_LOG_ERR, "Failed to attach dma buffer\r\n");
  295. return OMX_ErrorInsufficientResources;
  296. }
  297. OMX_BUFFERHEADERTYPE *temp_bufferHeader = (OMX_BUFFERHEADERTYPE *)malloc(sizeof(OMX_BUFFERHEADERTYPE));
  298. if (temp_bufferHeader == NULL)
  299. {
  300. LOG(SF_LOG_ERR, "malloc fail\r\n");
  301. return OMX_ErrorInsufficientResources;
  302. }
  303. memset(temp_bufferHeader, 0, sizeof(OMX_BUFFERHEADERTYPE));
  304. temp_bufferHeader->nAllocLen = nSizeBytes;
  305. temp_bufferHeader->pAppPrivate = pAppPrivate;
  306. temp_bufferHeader->pBuffer = pBuffer;
  307. *ppBufferHdr = temp_bufferHeader;
  308. if (nPortIndex == 1)
  309. {
  310. temp_bufferHeader->pOutputPortPrivate = (OMX_PTR)nOutBufIndex;
  311. nOutBufIndex ++;
  312. }
  313. ret = StoreOMXBuffer(pSfOMXComponent, temp_bufferHeader);
  314. LOG(SF_LOG_INFO, "pBuffer address = %p, nOutBufIndex = %d\r\n", temp_bufferHeader->pBuffer, (OMX_U64)(temp_bufferHeader->pOutputPortPrivate));
  315. LOG(SF_LOG_PERF, "alloc size = %d, buffer count = %d\r\n",nSizeBytes, GetOMXBufferCount(pSfOMXComponent));
  316. EXIT:
  317. FunctionOut();
  318. return ret;
  319. }
  320. static OMX_ERRORTYPE SF_OMX_AllocateBuffer(
  321. OMX_IN OMX_HANDLETYPE hComponent,
  322. OMX_INOUT OMX_BUFFERHEADERTYPE **ppBuffer,
  323. OMX_IN OMX_U32 nPortIndex,
  324. OMX_IN OMX_PTR pAppPrivate,
  325. OMX_IN OMX_U32 nSizeBytes)
  326. {
  327. OMX_ERRORTYPE ret = OMX_ErrorNone;
  328. OMX_COMPONENTTYPE *pOMXComponent = (OMX_COMPONENTTYPE *)hComponent;
  329. SF_OMX_COMPONENT *pSfOMXComponent = pOMXComponent->pComponentPrivate;
  330. SF_WAVE5_IMPLEMEMT *pSfVideoImplement = (SF_WAVE5_IMPLEMEMT *)pSfOMXComponent->componentImpl;
  331. ComponentImpl *pComponentRender = (ComponentImpl *)pSfVideoImplement->hSFComponentRender;
  332. FunctionIn();
  333. if (nSizeBytes == 0)
  334. {
  335. LOG(SF_LOG_WARN, "nSizeBytes = %d, use default buffer size\r\n", nSizeBytes);
  336. nSizeBytes = DEFAULT_VIDEO_OUTPUT_BUFFER_SIZE;
  337. }
  338. OMX_BUFFERHEADERTYPE *temp_bufferHeader = (OMX_BUFFERHEADERTYPE *)malloc(sizeof(OMX_BUFFERHEADERTYPE));
  339. if (temp_bufferHeader == NULL)
  340. {
  341. LOG(SF_LOG_ERR, "malloc fail\r\n");
  342. return OMX_ErrorInsufficientResources;
  343. }
  344. memset(temp_bufferHeader, 0, sizeof(OMX_BUFFERHEADERTYPE));
  345. temp_bufferHeader->nAllocLen = nSizeBytes;
  346. temp_bufferHeader->pAppPrivate = pAppPrivate;
  347. if (nPortIndex == 1)
  348. {
  349. // Alloc DMA memory first
  350. if (pSfOMXComponent->memory_optimization)
  351. {
  352. temp_bufferHeader->pBuffer = pSfVideoImplement->functions->AllocateFrameBuffer2(pComponentRender, nSizeBytes);
  353. }
  354. // DMA Memory alloc fail, goto normal alloc
  355. if (temp_bufferHeader->pBuffer == NULL)
  356. {
  357. pSfOMXComponent->memory_optimization = OMX_FALSE;
  358. temp_bufferHeader->pBuffer = malloc(nSizeBytes);
  359. memset(temp_bufferHeader->pBuffer, 0, nSizeBytes);
  360. LOG(SF_LOG_PERF, "Use normal buffer\r\n");
  361. }
  362. else
  363. {
  364. LOG(SF_LOG_PERF, "Use DMA buffer\r\n");
  365. }
  366. temp_bufferHeader->pOutputPortPrivate = (OMX_PTR)nOutBufIndex;
  367. nOutBufIndex ++;
  368. }
  369. else if (nPortIndex == 0)
  370. {
  371. temp_bufferHeader->pBuffer = malloc(nSizeBytes);
  372. memset(temp_bufferHeader->pBuffer, 0, nSizeBytes);
  373. }
  374. if (temp_bufferHeader->pBuffer == NULL)
  375. {
  376. free(temp_bufferHeader);
  377. LOG(SF_LOG_ERR, "malloc fail\r\n");
  378. return OMX_ErrorInsufficientResources;
  379. }
  380. *ppBuffer = temp_bufferHeader;
  381. LOG(SF_LOG_INFO, "pBuffer address = %p\r\n", temp_bufferHeader->pBuffer);
  382. ret = StoreOMXBuffer(pSfOMXComponent, temp_bufferHeader);
  383. LOG(SF_LOG_PERF, "alloc size = %d, buffer count = %d\r\n",nSizeBytes, GetOMXBufferCount(pSfOMXComponent));
  384. FunctionOut();
  385. return ret;
  386. }
  387. static OMX_ERRORTYPE SF_OMX_GetParameter(
  388. OMX_IN OMX_HANDLETYPE hComponent,
  389. OMX_IN OMX_INDEXTYPE nParamIndex,
  390. OMX_INOUT OMX_PTR ComponentParameterStructure)
  391. {
  392. OMX_ERRORTYPE ret = OMX_ErrorNone;
  393. OMX_COMPONENTTYPE *pOMXComponent = (OMX_COMPONENTTYPE *)hComponent;
  394. SF_OMX_COMPONENT *pSfOMXComponent = pOMXComponent->pComponentPrivate;
  395. FunctionIn();
  396. LOG(SF_LOG_INFO, "Get parameter on index %X\r\n", nParamIndex);
  397. if (hComponent == NULL)
  398. {
  399. ret = OMX_ErrorBadParameter;
  400. goto EXIT;
  401. }
  402. switch ((OMX_U32)nParamIndex)
  403. {
  404. case OMX_IndexParamVideoInit:
  405. {
  406. OMX_PORT_PARAM_TYPE *portParam = (OMX_PORT_PARAM_TYPE *)ComponentParameterStructure;
  407. portParam->nPorts = 2;
  408. portParam->nStartPortNumber = 0;
  409. }
  410. break;
  411. case OMX_IndexParamVideoPortFormat:
  412. {
  413. OMX_VIDEO_PARAM_PORTFORMATTYPE *portFormat = (OMX_VIDEO_PARAM_PORTFORMATTYPE *)ComponentParameterStructure;
  414. OMX_U32 index = portFormat->nIndex;
  415. switch (index)
  416. {
  417. case 0:
  418. portFormat->eCompressionFormat = OMX_VIDEO_CodingUnused;
  419. portFormat->eColorFormat = OMX_COLOR_FormatYUV420Planar;
  420. portFormat->xFramerate = 30;
  421. break;
  422. case 1:
  423. portFormat->eCompressionFormat = OMX_VIDEO_CodingUnused;
  424. portFormat->eColorFormat = OMX_COLOR_FormatYUV420SemiPlanar;
  425. portFormat->xFramerate = 30;
  426. break;
  427. case 2:
  428. portFormat->eCompressionFormat = OMX_VIDEO_CodingUnused;
  429. portFormat->eColorFormat = OMX_COLOR_FormatYVU420PackedPlanar;
  430. portFormat->xFramerate = 30;
  431. break;
  432. default:
  433. if (index > 0)
  434. {
  435. ret = OMX_ErrorNoMore;
  436. }
  437. break;
  438. }
  439. }
  440. break;
  441. case OMX_IndexParamVideoBitrate:
  442. break;
  443. case OMX_IndexParamVideoQuantization:
  444. break;
  445. case OMX_IndexParamPortDefinition:
  446. {
  447. OMX_PARAM_PORTDEFINITIONTYPE *pPortDefinition = (OMX_PARAM_PORTDEFINITIONTYPE *)ComponentParameterStructure;
  448. OMX_U32 portIndex = pPortDefinition->nPortIndex;
  449. OMX_PARAM_PORTDEFINITIONTYPE *pSrcDefinition = &pSfOMXComponent->portDefinition[portIndex];
  450. LOG(SF_LOG_INFO, "Get parameter width = %d, height = %d from port %X\r\n",
  451. pSrcDefinition->format.video.nFrameWidth, pSrcDefinition->format.video.nFrameHeight, portIndex);
  452. memcpy(pPortDefinition, pSrcDefinition, pPortDefinition->nSize);
  453. }
  454. break;
  455. case OMX_IndexParamVideoIntraRefresh:
  456. break;
  457. case OMX_IndexParamStandardComponentRole:
  458. break;
  459. case OMX_IndexParamVideoAvc:
  460. break;
  461. case OMX_IndexParamVideoHevc:
  462. break;
  463. case OMX_IndexParamVideoProfileLevelQuerySupported:
  464. break;
  465. default:
  466. break;
  467. }
  468. EXIT:
  469. FunctionOut();
  470. return ret;
  471. }
  472. static OMX_ERRORTYPE SF_OMX_SetParameter(
  473. OMX_IN OMX_HANDLETYPE hComponent,
  474. OMX_IN OMX_INDEXTYPE nIndex,
  475. OMX_IN OMX_PTR ComponentParameterStructure)
  476. {
  477. OMX_ERRORTYPE ret = OMX_ErrorNone;
  478. FunctionIn();
  479. if (hComponent == NULL)
  480. {
  481. ret = OMX_ErrorBadParameter;
  482. goto EXIT;
  483. }
  484. OMX_COMPONENTTYPE *pOMXComponent = (OMX_COMPONENTTYPE *)hComponent;
  485. SF_OMX_COMPONENT *pSfOMXComponent = pOMXComponent->pComponentPrivate;
  486. SF_WAVE5_IMPLEMEMT *pSfVideoImplement = (SF_WAVE5_IMPLEMEMT *)pSfOMXComponent->componentImpl;
  487. if (pSfOMXComponent == NULL)
  488. {
  489. ret = OMX_ErrorBadParameter;
  490. goto EXIT;
  491. }
  492. TestDecConfig *testConfig = pSfVideoImplement->testConfig;
  493. LOG(SF_LOG_INFO, "Set parameter on index %X\r\n", nIndex);
  494. switch ((OMX_U32)nIndex)
  495. {
  496. case OMX_IndexParamVideoPortFormat:
  497. {
  498. OMX_VIDEO_PARAM_PORTFORMATTYPE *portFormat = (OMX_VIDEO_PARAM_PORTFORMATTYPE *)ComponentParameterStructure;
  499. OMX_U32 nPortIndex = portFormat->nPortIndex;
  500. TestDecConfig *pTestDecConfig = (TestDecConfig *)pSfVideoImplement->testConfig;
  501. OMX_PARAM_PORTDEFINITIONTYPE *pPort = &pSfOMXComponent->portDefinition[nPortIndex];
  502. LOG(SF_LOG_DEBUG, "Set video format to port %d color %d\r\n", portFormat->nPortIndex, portFormat->eColorFormat);
  503. switch (portFormat->eColorFormat)
  504. {
  505. case OMX_COLOR_FormatYUV420Planar: //I420
  506. pTestDecConfig->cbcrInterleave = FALSE;
  507. pTestDecConfig->nv21 = FALSE;
  508. pPort->format.video.eColorFormat = portFormat->eColorFormat;
  509. break;
  510. case OMX_COLOR_FormatYUV420SemiPlanar: //NV12
  511. case OMX_COLOR_FormatYUV420PackedSemiPlanar:
  512. pTestDecConfig->cbcrInterleave = TRUE;
  513. pTestDecConfig->nv21 = FALSE;
  514. pPort->format.video.eColorFormat = portFormat->eColorFormat;
  515. break;
  516. case OMX_COLOR_FormatYVU420PackedSemiPlanar: //NV21
  517. case OMX_COLOR_FormatYVU420PackedPlanar:
  518. pTestDecConfig->cbcrInterleave = TRUE;
  519. pTestDecConfig->nv21 = TRUE;
  520. pPort->format.video.eColorFormat = portFormat->eColorFormat;
  521. break;
  522. default:
  523. LOG(SF_LOG_ERR, "Error to set parameter: %d, only nv12 nv21 i420 supported\r\n", portFormat->eColorFormat);
  524. return OMX_ErrorBadParameter;
  525. break;
  526. }
  527. break;
  528. }
  529. case OMX_IndexParamVideoBitrate:
  530. break;
  531. case OMX_IndexParamVideoQuantization:
  532. break;
  533. break;
  534. case OMX_IndexParamPortDefinition:
  535. {
  536. OMX_PARAM_PORTDEFINITIONTYPE *pPortDefinition = (OMX_PARAM_PORTDEFINITIONTYPE *)ComponentParameterStructure;
  537. OMX_PARAM_PORTDEFINITIONTYPE *pInputPort = &pSfOMXComponent->portDefinition[0];
  538. OMX_PARAM_PORTDEFINITIONTYPE *pOutputPort = &pSfOMXComponent->portDefinition[1];
  539. TestDecConfig *pTestDecConfig = (TestDecConfig *)pSfVideoImplement->testConfig;
  540. OMX_U32 portIndex = pPortDefinition->nPortIndex;
  541. OMX_U32 width = pPortDefinition->format.video.nFrameWidth;
  542. OMX_U32 height = pPortDefinition->format.video.nFrameHeight;
  543. LOG(SF_LOG_INFO, "Set width = %d, height = %d on port %d\r\n", width, height, pPortDefinition->nPortIndex);
  544. if (portIndex == 0)
  545. {
  546. if (pPortDefinition->nBufferCountActual != pInputPort->nBufferCountActual)
  547. {
  548. LOG(SF_LOG_INFO, "Set input buffer count = %d\r\n", pPortDefinition->nBufferCountActual);
  549. ComponentImpl *pFeederComponent = (ComponentImpl *)pSfVideoImplement->hSFComponentFeeder;
  550. pSfVideoImplement->functions->ComponentPortDestroy(&pFeederComponent->srcPort);
  551. pSfVideoImplement->functions->ComponentPortCreate(&pFeederComponent->srcPort, pSfVideoImplement->hSFComponentFeeder,
  552. pPortDefinition->nBufferCountActual, sizeof(PortContainerExternal));
  553. }
  554. memcpy(&pSfOMXComponent->portDefinition[portIndex], pPortDefinition, pPortDefinition->nSize);
  555. pInputPort->format.video.nStride = width;
  556. pInputPort->format.video.nSliceHeight = height;
  557. pInputPort->nBufferSize = width * height * 2;
  558. }
  559. else if (portIndex == 1)
  560. {
  561. if (pPortDefinition->nBufferCountActual != pOutputPort->nBufferCountActual)
  562. {
  563. ComponentImpl *pRenderComponent = (ComponentImpl *)pSfVideoImplement->hSFComponentRender;
  564. pSfVideoImplement->functions->SetRenderTotalBufferNumber(pRenderComponent, pPortDefinition->nBufferCountActual);
  565. LOG(SF_LOG_INFO, "Set output buffer count = %d\r\n", pPortDefinition->nBufferCountActual);
  566. pSfVideoImplement->functions->ComponentPortDestroy(&pRenderComponent->sinkPort);
  567. pSfVideoImplement->functions->ComponentPortCreate(&pRenderComponent->sinkPort, pSfVideoImplement->hSFComponentRender,
  568. pPortDefinition->nBufferCountActual, sizeof(PortContainerExternal));
  569. }
  570. memcpy(&pSfOMXComponent->portDefinition[portIndex], pPortDefinition, pPortDefinition->nSize);
  571. /*
  572. Some client may set '0' or '1' to output port, in this case use input port parameters
  573. */
  574. if (width <= 1)
  575. {
  576. width = pInputPort->format.video.nFrameWidth;
  577. }
  578. if (height <= 1)
  579. {
  580. height = pInputPort->format.video.nFrameHeight;
  581. }
  582. if (width > 0 && height > 0)
  583. {
  584. int scalew = pInputPort->format.video.nFrameWidth / width;
  585. int scaleh = pInputPort->format.video.nFrameHeight / height;
  586. if (scalew > 8 || scaleh > 8 || scalew < 1 || scaleh < 1)
  587. {
  588. int nInputWidth = pInputPort->format.video.nFrameWidth;
  589. int nInputHeight = pInputPort->format.video.nFrameHeight;
  590. LOG(SF_LOG_WARN, "Scaling should be 1 to 1/8 (down-scaling only)! Use input parameter. "
  591. "OutPut[%d, %d]. Input[%d, %d]\r\n", width, height, nInputWidth, nInputHeight);
  592. width = nInputWidth;
  593. height = nInputHeight;
  594. }
  595. }
  596. pOutputPort->format.video.nFrameWidth = width;
  597. pOutputPort->format.video.nFrameHeight = height;
  598. pOutputPort->format.video.nStride = width;
  599. pOutputPort->format.video.nSliceHeight = height;
  600. pTestDecConfig->scaleDownWidth = VPU_CEIL(width, 2);
  601. pTestDecConfig->scaleDownHeight = VPU_CEIL(height, 2);
  602. LOG(SF_LOG_INFO, "Set scale = %d, %d\r\n", pTestDecConfig->scaleDownWidth , pTestDecConfig->scaleDownHeight);
  603. /*
  604. if cbcrInterleave is FALSE and nv21 is FALSE, the default dec format is I420
  605. if cbcrInterleave is TRUE and nv21 is FALSE, then the dec format is NV12
  606. if cbcrInterleave is TRUE and nv21 is TRUE, then the dec format is NV21
  607. */
  608. LOG(SF_LOG_INFO, "Set format = %d\r\n", pOutputPort->format.video.eColorFormat);
  609. switch (pOutputPort->format.video.eColorFormat)
  610. {
  611. case OMX_COLOR_FormatYUV420Planar: //I420
  612. pTestDecConfig->cbcrInterleave = FALSE;
  613. pTestDecConfig->nv21 = FALSE;
  614. if (width && height)
  615. pOutputPort->nBufferSize = (width * height * 3) / 2;
  616. break;
  617. case OMX_COLOR_FormatYUV420SemiPlanar: //NV12
  618. case OMX_COLOR_FormatYUV420PackedSemiPlanar:
  619. pTestDecConfig->cbcrInterleave = TRUE;
  620. pTestDecConfig->nv21 = FALSE;
  621. if (width && height)
  622. pOutputPort->nBufferSize = (width * height * 3) / 2;
  623. break;
  624. case OMX_COLOR_FormatYVU420PackedSemiPlanar: //NV21
  625. case OMX_COLOR_FormatYVU420PackedPlanar:
  626. pTestDecConfig->cbcrInterleave = TRUE;
  627. pTestDecConfig->nv21 = TRUE;
  628. if (width && height)
  629. pOutputPort->nBufferSize = (width * height * 3) / 2;
  630. break;
  631. default:
  632. LOG(SF_LOG_ERR, "Error to set parameter: %d, only nv12 nv21 i420 supported\r\n",
  633. pOutputPort->format.video.eColorFormat);
  634. return OMX_ErrorBadParameter;
  635. break;
  636. }
  637. }
  638. }
  639. break;
  640. case OMX_IndexParamVideoIntraRefresh:
  641. break;
  642. case OMX_IndexParamStandardComponentRole:
  643. break;
  644. case OMX_IndexParamVideoAvc:
  645. testConfig->bitFormat = 0;
  646. break;
  647. case OMX_IndexParamVideoHevc:
  648. testConfig->bitFormat = 12;
  649. break;
  650. default:
  651. break;
  652. }
  653. EXIT:
  654. FunctionOut();
  655. return ret;
  656. }
  657. static OMX_ERRORTYPE SF_OMX_GetConfig(
  658. OMX_HANDLETYPE hComponent,
  659. OMX_INDEXTYPE nIndex,
  660. OMX_PTR pComponentConfigStructure)
  661. {
  662. OMX_ERRORTYPE ret = OMX_ErrorNone;
  663. FunctionIn();
  664. if (hComponent == NULL)
  665. {
  666. ret = OMX_ErrorBadParameter;
  667. goto EXIT;
  668. }
  669. switch (nIndex)
  670. {
  671. case OMX_IndexConfigVideoAVCIntraPeriod:
  672. break;
  673. case OMX_IndexConfigVideoBitrate:
  674. break;
  675. case OMX_IndexConfigVideoFramerate:
  676. break;
  677. default:
  678. break;
  679. }
  680. EXIT:
  681. FunctionOut();
  682. return ret;
  683. }
  684. static OMX_ERRORTYPE SF_OMX_SetConfig(
  685. OMX_HANDLETYPE hComponent,
  686. OMX_INDEXTYPE nIndex,
  687. OMX_PTR pComponentConfigStructure)
  688. {
  689. OMX_ERRORTYPE ret = OMX_ErrorNone;
  690. FunctionIn();
  691. if (hComponent == NULL)
  692. {
  693. ret = OMX_ErrorBadParameter;
  694. goto EXIT;
  695. }
  696. switch ((OMX_U32)nIndex)
  697. {
  698. case OMX_IndexConfigVideoAVCIntraPeriod:
  699. break;
  700. case OMX_IndexConfigVideoBitrate:
  701. break;
  702. case OMX_IndexConfigVideoFramerate:
  703. break;
  704. case OMX_IndexConfigVideoIntraVOPRefresh:
  705. break;
  706. default:
  707. break;
  708. }
  709. EXIT:
  710. FunctionOut();
  711. return ret;
  712. }
  713. static OMX_ERRORTYPE InitDecoder(SF_OMX_COMPONENT *pSfOMXComponent)
  714. {
  715. TestDecConfig *testConfig = NULL;
  716. CNMComponentConfig *config = NULL;
  717. Uint32 sizeInWord;
  718. char *fwPath = NULL;
  719. SF_WAVE5_IMPLEMEMT *pSfVideoImplement = (SF_WAVE5_IMPLEMEMT *)pSfOMXComponent->componentImpl;
  720. if (pSfVideoImplement->hSFComponentExecoder != NULL)
  721. {
  722. return OMX_ErrorNone;
  723. }
  724. FunctionIn();
  725. testConfig = (TestDecConfig *)pSfVideoImplement->testConfig;
  726. testConfig->productId = (ProductId)pSfVideoImplement->functions->VPU_GetProductId(testConfig->coreIdx);
  727. if (CheckDecTestConfig(testConfig) == FALSE)
  728. {
  729. LOG(SF_LOG_ERR, "fail to CheckTestConfig()\n");
  730. return OMX_ErrorBadParameter;
  731. }
  732. switch (testConfig->productId)
  733. {
  734. case PRODUCT_ID_521:
  735. fwPath = pSfOMXComponent->fwPath;
  736. break;
  737. case PRODUCT_ID_511:
  738. fwPath = pSfOMXComponent->fwPath;
  739. break;
  740. case PRODUCT_ID_517:
  741. fwPath = CORE_7_BIT_CODE_FILE_PATH;
  742. break;
  743. default:
  744. LOG(SF_LOG_ERR, "Unknown product id: %d, whether kernel module loaded?\n", testConfig->productId);
  745. return OMX_ErrorBadParameter;
  746. }
  747. LOG(SF_LOG_INFO, "FW PATH = %s\n", fwPath);
  748. if (pSfVideoImplement->functions->LoadFirmware(testConfig->productId, (Uint8 **)&(pSfVideoImplement->pusBitCode), &sizeInWord, fwPath) < 0)
  749. {
  750. LOG(SF_LOG_ERR, "Failed to load firmware: %s\n", fwPath);
  751. return OMX_ErrorInsufficientResources;
  752. }
  753. config = pSfVideoImplement->config;
  754. memcpy(&(config->testDecConfig), testConfig, sizeof(TestDecConfig));
  755. config->bitcode = (Uint8 *)pSfVideoImplement->pusBitCode;
  756. config->sizeOfBitcode = sizeInWord;
  757. if (pSfVideoImplement->functions->SetUpDecoderOpenParam(&config->decOpenParam, &config->testDecConfig) != RETCODE_SUCCESS)
  758. {
  759. LOG(SF_LOG_ERR, "SetupDecoderOpenParam error\n");
  760. return OMX_ErrorBadParameter;
  761. }
  762. LOG(SF_LOG_DEBUG, "cbcrInterleave = %d, nv21 = %d\r\n", testConfig->cbcrInterleave, testConfig->nv21);
  763. pSfVideoImplement->hSFComponentExecoder = pSfVideoImplement->functions->ComponentCreate("wave_decoder", config);
  764. pSfVideoImplement->hSFComponentFeeder = pSfVideoImplement->functions->ComponentCreate("feeder", config);
  765. pSfVideoImplement->hSFComponentRender = pSfVideoImplement->functions->ComponentCreate("renderer", config);
  766. ComponentImpl *pFeederComponent = (ComponentImpl *)pSfVideoImplement->hSFComponentFeeder;
  767. pSfVideoImplement->functions->ComponentPortCreate(&pFeederComponent->srcPort, pSfVideoImplement->hSFComponentFeeder, VPU_INPUT_BUF_NUMBER, sizeof(PortContainerExternal));
  768. ComponentImpl *pRenderComponent = (ComponentImpl *)pSfVideoImplement->hSFComponentRender;
  769. pSfVideoImplement->functions->ComponentPortDestroy(&pRenderComponent->sinkPort);
  770. pSfVideoImplement->functions->ComponentPortCreate(&pRenderComponent->sinkPort, pSfVideoImplement->hSFComponentRender, VPU_OUTPUT_BUF_NUMBER, sizeof(PortContainerExternal));
  771. if (pSfVideoImplement->functions->SetupDecListenerContext(pSfVideoImplement->lsnCtx, config, NULL) == TRUE)
  772. {
  773. pSfVideoImplement->functions->ComponentRegisterListener(pSfVideoImplement->hSFComponentExecoder,
  774. COMPONENT_EVENT_DEC_ALL, pSfVideoImplement->functions->DecoderListener, (void *)pSfVideoImplement->lsnCtx);
  775. pSfVideoImplement->functions->ComponentRegisterListener(pSfVideoImplement->hSFComponentRender,
  776. COMPONENT_EVENT_DEC_DECODED_ALL, OnEventArrived, (void *)pSfVideoImplement->lsnCtx);
  777. pSfVideoImplement->functions->ComponentRegisterListener(pSfVideoImplement->hSFComponentFeeder,
  778. COMPONENT_EVENT_DEC_ALL, OnEventArrived, (void *)pSfVideoImplement->lsnCtx);
  779. pSfVideoImplement->functions->ComponentRegisterListener(pSfVideoImplement->hSFComponentRender,
  780. COMPONENT_EVENT_DEC_ALL, OnEventArrived, (void *)pSfVideoImplement->lsnCtx);
  781. }
  782. else
  783. {
  784. LOG(SF_LOG_ERR, "ComponentRegisterListener fail\r\n");
  785. return OMX_ErrorBadParameter;
  786. }
  787. pSfVideoImplement->functions->ComponentSetupTunnel(pSfVideoImplement->hSFComponentFeeder, pSfVideoImplement->hSFComponentExecoder);
  788. pSfVideoImplement->functions->ComponentSetupTunnel(pSfVideoImplement->hSFComponentExecoder, pSfVideoImplement->hSFComponentRender);
  789. FunctionOut();
  790. return OMX_ErrorNone;
  791. }
  792. static OMX_ERRORTYPE SF_OMX_SendCommand(
  793. OMX_IN OMX_HANDLETYPE hComponent,
  794. OMX_IN OMX_COMMANDTYPE Cmd,
  795. OMX_IN OMX_U32 nParam,
  796. OMX_IN OMX_PTR pCmdData)
  797. {
  798. OMX_ERRORTYPE ret = OMX_ErrorNone;
  799. OMX_COMPONENTTYPE *pOMXComponent = (OMX_COMPONENTTYPE *)hComponent;
  800. SF_OMX_COMPONENT *pSfOMXComponent = pOMXComponent->pComponentPrivate;
  801. SF_WAVE5_IMPLEMEMT *pSfVideoImplement = (SF_WAVE5_IMPLEMEMT *)pSfOMXComponent->componentImpl;
  802. ComponentImpl *pSFComponentDecoder = NULL;
  803. ComponentImpl *pSFComponentFeeder = NULL;
  804. ComponentImpl *pSFComponentRender = NULL;
  805. ComponentState componentState;
  806. FunctionIn();
  807. if (hComponent == NULL || pSfOMXComponent == NULL)
  808. {
  809. ret = OMX_ErrorBadParameter;
  810. goto EXIT;
  811. }
  812. LOG(SF_LOG_INFO, "cmd = %X, nParam = %X\r\n", Cmd, nParam);
  813. switch (Cmd)
  814. {
  815. case OMX_CommandStateSet:
  816. pSfOMXComponent->nextState = nParam;
  817. LOG(SF_LOG_INFO, "OMX dest state = %X\r\n", nParam);
  818. switch (nParam)
  819. {
  820. case OMX_StateLoaded:
  821. if (pSfVideoImplement->hSFComponentExecoder == NULL)
  822. {
  823. break;
  824. }
  825. pSFComponentDecoder = (ComponentImpl *)pSfVideoImplement->hSFComponentExecoder;
  826. pSFComponentFeeder = (ComponentImpl *)pSfVideoImplement->hSFComponentFeeder;
  827. pSFComponentRender = (ComponentImpl *)pSfVideoImplement->hSFComponentRender;
  828. componentState = pSfVideoImplement->functions->ComponentGetState(pSfVideoImplement->hSFComponentExecoder);
  829. LOG(SF_LOG_INFO, "VPU Current state = %X\r\n", componentState);
  830. switch(componentState)
  831. {
  832. case COMPONENT_STATE_CREATED:
  833. case COMPONENT_STATE_PREPARED:
  834. case COMPONENT_STATE_EXECUTED:
  835. case COMPONENT_STATE_TERMINATED:
  836. if (pSfVideoImplement->functions->Queue_Get_Cnt(pSFComponentRender->sinkPort.inputQ) > 0 ||
  837. pSfVideoImplement->functions->Queue_Get_Cnt(pSFComponentFeeder->srcPort.inputQ) > 0)
  838. {
  839. LOG(SF_LOG_ERR, "Buffer not flush!\r\n")
  840. // ret = OMX_ErrorIncorrectStateTransition;
  841. break;
  842. }
  843. pSFComponentDecoder->terminate = OMX_TRUE;
  844. pSFComponentFeeder->terminate = OMX_TRUE;
  845. pSFComponentRender->terminate = OMX_TRUE;
  846. break;
  847. case COMPONENT_STATE_NONE:
  848. default:
  849. ret = OMX_ErrorIncorrectStateTransition;
  850. break;
  851. }
  852. break;
  853. case OMX_StateIdle:
  854. if (pSfVideoImplement->hSFComponentExecoder == NULL)
  855. {
  856. ret = InitDecoder(pSfOMXComponent);
  857. if (ret != OMX_ErrorNone)
  858. {
  859. goto EXIT;
  860. }
  861. }
  862. pSFComponentDecoder = (ComponentImpl *)pSfVideoImplement->hSFComponentExecoder;
  863. pSFComponentFeeder = (ComponentImpl *)pSfVideoImplement->hSFComponentFeeder;
  864. pSFComponentRender = (ComponentImpl *)pSfVideoImplement->hSFComponentRender;
  865. componentState = pSfVideoImplement->functions->ComponentGetState(pSfVideoImplement->hSFComponentExecoder);
  866. LOG(SF_LOG_INFO, "VPU Current state = %X\r\n", componentState);
  867. switch(componentState)
  868. {
  869. case COMPONENT_STATE_NONE:
  870. ret = OMX_ErrorIncorrectStateTransition;
  871. break;
  872. case COMPONENT_STATE_CREATED:
  873. break;
  874. case COMPONENT_STATE_PREPARED:
  875. case COMPONENT_STATE_EXECUTED:
  876. case COMPONENT_STATE_TERMINATED:
  877. {
  878. pSFComponentDecoder->pause = OMX_TRUE;
  879. pSFComponentFeeder->pause = OMX_TRUE;
  880. pSFComponentRender->pause = OMX_TRUE;
  881. FlushBuffer(pSfOMXComponent, 0);
  882. FlushBuffer(pSfOMXComponent, 1);
  883. }
  884. break;
  885. case COMPONENT_STATE_MAX:
  886. break;
  887. }
  888. pSfOMXComponent->callbacks->EventHandler(pSfOMXComponent->pOMXComponent, pSfOMXComponent->pAppData,
  889. OMX_EventCmdComplete, OMX_CommandStateSet, nParam, NULL);
  890. break;
  891. case OMX_StateExecuting:
  892. ret = InitDecoder(pSfOMXComponent);
  893. if (ret != OMX_ErrorNone)
  894. {
  895. goto EXIT;
  896. }
  897. pSFComponentDecoder = (ComponentImpl *)pSfVideoImplement->hSFComponentExecoder;
  898. pSFComponentFeeder = (ComponentImpl *)pSfVideoImplement->hSFComponentFeeder;
  899. pSFComponentRender = (ComponentImpl *)pSfVideoImplement->hSFComponentRender;
  900. componentState = pSfVideoImplement->functions->ComponentGetState(pSfVideoImplement->hSFComponentExecoder);
  901. LOG(SF_LOG_INFO, "VPU Current state = %X\r\n", componentState);
  902. switch(componentState)
  903. {
  904. case COMPONENT_STATE_NONE:
  905. case COMPONENT_STATE_TERMINATED:
  906. ret = OMX_ErrorIncorrectStateTransition;
  907. break;
  908. case COMPONENT_STATE_CREATED:
  909. if (pSFComponentDecoder->thread == NULL)
  910. {
  911. LOG(SF_LOG_INFO, "execute component %s\r\n", pSFComponentDecoder->name);
  912. componentState = pSfVideoImplement->functions->ComponentExecute(pSFComponentDecoder);
  913. LOG(SF_LOG_INFO, "ret = %d\r\n", componentState);
  914. }
  915. if (pSFComponentFeeder->thread == NULL)
  916. {
  917. LOG(SF_LOG_INFO, "execute component %s\r\n", pSFComponentFeeder->name);
  918. componentState = pSfVideoImplement->functions->ComponentExecute(pSFComponentFeeder);
  919. LOG(SF_LOG_INFO, "ret = %d\r\n", componentState);
  920. }
  921. if (pSFComponentRender->thread == NULL)
  922. {
  923. LOG(SF_LOG_INFO, "execute component %s\r\n", pSFComponentRender->name);
  924. componentState = pSfVideoImplement->functions->ComponentExecute(pSFComponentRender);
  925. LOG(SF_LOG_INFO, "ret = %d\r\n", componentState);
  926. }
  927. break;
  928. case COMPONENT_STATE_PREPARED:
  929. case COMPONENT_STATE_EXECUTED:
  930. pSFComponentDecoder->pause = OMX_FALSE;
  931. pSFComponentFeeder->pause = OMX_FALSE;
  932. pSFComponentRender->pause = OMX_FALSE;
  933. break;
  934. case COMPONENT_STATE_MAX:
  935. break;
  936. }
  937. break;
  938. default:
  939. ret = OMX_ErrorIncorrectStateTransition;
  940. break;
  941. }
  942. pSfOMXComponent->callbacks->EventHandler(pSfOMXComponent->pOMXComponent, pSfOMXComponent->pAppData,
  943. OMX_EventCmdComplete, OMX_CommandStateSet, nParam, NULL);
  944. break;
  945. case OMX_CommandFlush:
  946. {
  947. OMX_U32 nPort = nParam;
  948. FlushBuffer(pSfOMXComponent, nPort);
  949. pSfOMXComponent->callbacks->EventHandler(pSfOMXComponent->pOMXComponent, pSfOMXComponent->pAppData,
  950. OMX_EventCmdComplete, OMX_CommandFlush, nParam, NULL);
  951. }
  952. break;
  953. case OMX_CommandPortDisable:
  954. pSfOMXComponent->callbacks->EventHandler(pSfOMXComponent->pOMXComponent, pSfOMXComponent->pAppData,
  955. OMX_EventCmdComplete, OMX_CommandPortDisable, nParam, NULL);
  956. break;
  957. case OMX_CommandPortEnable:
  958. pSfOMXComponent->callbacks->EventHandler(pSfOMXComponent->pOMXComponent, pSfOMXComponent->pAppData,
  959. OMX_EventCmdComplete, OMX_CommandPortEnable, nParam, NULL);
  960. break;
  961. case OMX_CommandMarkBuffer:
  962. break;
  963. default:
  964. break;
  965. }
  966. EXIT:
  967. FunctionOut();
  968. return ret;
  969. }
  970. static OMX_ERRORTYPE SF_OMX_GetState(
  971. OMX_IN OMX_HANDLETYPE hComponent,
  972. OMX_OUT OMX_STATETYPE *pState)
  973. {
  974. OMX_ERRORTYPE ret = OMX_ErrorNone;
  975. FunctionIn();
  976. GetStateCommon(hComponent, pState);
  977. FunctionOut();
  978. return ret;
  979. }
  980. static OMX_ERRORTYPE SF_OMX_FreeBuffer(
  981. OMX_IN OMX_HANDLETYPE hComponent,
  982. OMX_IN OMX_U32 nPortIndex,
  983. OMX_IN OMX_BUFFERHEADERTYPE *pBufferHdr)
  984. {
  985. OMX_ERRORTYPE ret = OMX_ErrorNone;
  986. OMX_COMPONENTTYPE *pOMXComponent = (OMX_COMPONENTTYPE *)hComponent;
  987. SF_OMX_COMPONENT *pSfOMXComponent = pOMXComponent->pComponentPrivate;
  988. FunctionIn();
  989. if (ClearOMXBuffer(pSfOMXComponent, pBufferHdr) != OMX_ErrorNone)
  990. {
  991. LOG(SF_LOG_ERR, "Could not found pBufferHdr = %p\r\n", pBufferHdr);
  992. return OMX_ErrorBadParameter;
  993. }
  994. LOG(SF_LOG_PERF, "buffer count = %d\r\n", GetOMXBufferCount(pSfOMXComponent));
  995. if (nPortIndex == 1) {
  996. LOG(SF_LOG_INFO, "free %p on output buffer\r\n", pBufferHdr->pBuffer);
  997. pBufferHdr->pBuffer = NULL;
  998. } else {
  999. free(pBufferHdr->pBuffer);
  1000. pBufferHdr->pBuffer = NULL;
  1001. }
  1002. free(pBufferHdr);
  1003. FunctionOut();
  1004. return ret;
  1005. }
  1006. static OMX_ERRORTYPE SF_OMX_ComponentConstructor(SF_OMX_COMPONENT *pSfOMXComponent)
  1007. {
  1008. OMX_ERRORTYPE ret = OMX_ErrorNone;
  1009. FunctionIn();
  1010. ret = InitComponentStructorCommon(pSfOMXComponent);
  1011. if (ret != OMX_ErrorNone)
  1012. {
  1013. goto EXIT;
  1014. }
  1015. SF_WAVE5_IMPLEMEMT *pSfVideoImplement = (SF_WAVE5_IMPLEMEMT *)pSfOMXComponent->componentImpl;
  1016. TestDecConfig *pTestDecConfig = (TestDecConfig *)pSfVideoImplement->testConfig;
  1017. pTestDecConfig->feedingMode = FEEDING_METHOD_BUFFER;
  1018. pTestDecConfig->bitstreamMode = BS_MODE_PIC_END;
  1019. pTestDecConfig->bitFormat = pSfVideoImplement->bitFormat;
  1020. /*
  1021. if cbcrInterleave is FALSE and nv21 is FALSE, the default dec format is I420
  1022. if cbcrInterleave is TRUE and nv21 is FALSE, then the dec format is NV12
  1023. if cbcrInterleave is TRUE and nv21 is TRUE, then the dec format is NV21
  1024. */
  1025. pTestDecConfig->cbcrInterleave = TRUE;
  1026. pTestDecConfig->nv21 = FALSE;
  1027. pSfOMXComponent->pOMXComponent->UseBuffer = &SF_OMX_UseBuffer;
  1028. pSfOMXComponent->pOMXComponent->AllocateBuffer = &SF_OMX_AllocateBuffer;
  1029. pSfOMXComponent->pOMXComponent->EmptyThisBuffer = &SF_OMX_EmptyThisBuffer;
  1030. pSfOMXComponent->pOMXComponent->FillThisBuffer = &SF_OMX_FillThisBuffer;
  1031. pSfOMXComponent->pOMXComponent->FreeBuffer = &SF_OMX_FreeBuffer;
  1032. // pSfOMXComponent->pOMXComponent->ComponentTunnelRequest = &SF_OMX_ComponentTunnelRequest;
  1033. pSfOMXComponent->pOMXComponent->GetParameter = &SF_OMX_GetParameter;
  1034. pSfOMXComponent->pOMXComponent->SetParameter = &SF_OMX_SetParameter;
  1035. pSfOMXComponent->pOMXComponent->GetConfig = &SF_OMX_GetConfig;
  1036. pSfOMXComponent->pOMXComponent->SetConfig = &SF_OMX_SetConfig;
  1037. pSfOMXComponent->pOMXComponent->SendCommand = &SF_OMX_SendCommand;
  1038. pSfOMXComponent->pOMXComponent->GetState = &SF_OMX_GetState;
  1039. // pSfOMXComponent->pOMXComponent->GetExtensionIndex = &SF_OMX_GetExtensionIndex;
  1040. // pSfOMXComponent->pOMXComponent->ComponentRoleEnum = &SF_OMX_ComponentRoleEnum;
  1041. // pSfOMXComponent->pOMXComponent->ComponentDeInit = &SF_OMX_ComponentDeInit;
  1042. EXIT:
  1043. FunctionOut();
  1044. return ret;
  1045. }
  1046. static OMX_ERRORTYPE SF_OMX_ComponentClear(SF_OMX_COMPONENT *pSfOMXComponent)
  1047. {
  1048. OMX_ERRORTYPE ret = OMX_ErrorNone;
  1049. SF_WAVE5_IMPLEMEMT *pSfVideoImplement = (SF_WAVE5_IMPLEMEMT *)pSfOMXComponent->componentImpl;
  1050. ComponentImpl *pSFComponentDecoder = (ComponentImpl *)pSfVideoImplement->hSFComponentExecoder;
  1051. ComponentImpl *pSFComponentFeeder = (ComponentImpl *)pSfVideoImplement->hSFComponentFeeder;
  1052. ComponentImpl *pSFComponentRender = (ComponentImpl *)pSfVideoImplement->hSFComponentRender;
  1053. FunctionIn();
  1054. if (pSFComponentDecoder == NULL || pSFComponentFeeder == NULL || pSFComponentRender == NULL)
  1055. {
  1056. goto EXIT;
  1057. }
  1058. pSFComponentDecoder->terminate = OMX_TRUE;
  1059. pSFComponentFeeder->terminate = OMX_TRUE;
  1060. pSFComponentRender->terminate = OMX_TRUE;
  1061. pSfVideoImplement->functions->ComponentWait(pSfVideoImplement->hSFComponentFeeder);
  1062. pSfVideoImplement->functions->ComponentWait(pSfVideoImplement->hSFComponentExecoder);
  1063. pSfVideoImplement->functions->ComponentWait(pSfVideoImplement->hSFComponentRender);
  1064. free(pSfVideoImplement->pusBitCode);
  1065. pSfVideoImplement->functions->ClearDecListenerContext(pSfVideoImplement->lsnCtx);
  1066. ComponentClearCommon(pSfOMXComponent);
  1067. EXIT:
  1068. FunctionOut();
  1069. return ret;
  1070. }
  1071. SF_OMX_COMPONENT sf_dec_decoder_h265 = {
  1072. .componentName = "sf.dec.decoder.h265",
  1073. .libName = "libsfdec.so",
  1074. .pOMXComponent = NULL,
  1075. .SF_OMX_ComponentConstructor = SF_OMX_ComponentConstructor,
  1076. .SF_OMX_ComponentClear = SF_OMX_ComponentClear,
  1077. // .functions = NULL,
  1078. // .bitFormat = STD_HEVC,
  1079. .componentImpl = NULL,
  1080. .fwPath = "/lib/firmware/chagall.bin",
  1081. .componentRule = "video_decoder.hevc"};
  1082. SF_OMX_COMPONENT sf_dec_decoder_h264 = {
  1083. .componentName = "sf.dec.decoder.h264",
  1084. .libName = "libsfdec.so",
  1085. .pOMXComponent = NULL,
  1086. .SF_OMX_ComponentConstructor = SF_OMX_ComponentConstructor,
  1087. .SF_OMX_ComponentClear = SF_OMX_ComponentClear,
  1088. // .functions = NULL,
  1089. // .bitFormat = STD_AVC,
  1090. .componentImpl = NULL,
  1091. .fwPath = "/lib/firmware/chagall.bin",
  1092. .componentRule = "video_decoder.avc"};