SF_OMX_Mjpeg_decoder.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303
  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_mjpeg_common.h"
  9. #include "SF_OMX_Core.h"
  10. #include <sys/prctl.h>
  11. extern OMX_TICKS gInitTimeStamp;
  12. #define TEMP_DEBUG 1
  13. #define NUM_OF_PORTS 2
  14. static OMX_ERRORTYPE SF_OMX_EmptyThisBuffer(
  15. OMX_IN OMX_HANDLETYPE hComponent,
  16. OMX_IN OMX_BUFFERHEADERTYPE *pBuffer)
  17. {
  18. OMX_ERRORTYPE ret = OMX_ErrorNone;
  19. OMX_BOOL bSendNULL = OMX_FALSE;
  20. OMX_BOOL bSendMessage = OMX_TRUE;
  21. FunctionIn();
  22. if (hComponent == NULL || pBuffer == NULL)
  23. {
  24. ret = OMX_ErrorBadParameter;
  25. goto EXIT;
  26. }
  27. OMX_COMPONENTTYPE *pOMXComponent = (OMX_COMPONENTTYPE *)hComponent;
  28. SF_OMX_COMPONENT *pSfOMXComponent = (SF_OMX_COMPONENT *)pOMXComponent->pComponentPrivate;
  29. SF_CODAJ12_IMPLEMEMT *pSfCodaj12Implement = (SF_CODAJ12_IMPLEMEMT *)pSfOMXComponent->componentImpl;
  30. LOG(SF_LOG_DEBUG, "nFilledLen = %d, nFlags = %d, pBuffer = %p\r\n", pBuffer->nFilledLen, pBuffer->nFlags, pBuffer->pBuffer);
  31. if (pBuffer->nFilledLen == 0 || (pBuffer->nFlags & 0x1) == 0x1)
  32. {
  33. bSendNULL = OMX_TRUE;
  34. }
  35. else
  36. {
  37. bSendNULL = OMX_FALSE;
  38. }
  39. if (pBuffer->pBuffer == NULL)
  40. {
  41. bSendMessage = OMX_FALSE;
  42. }
  43. else
  44. {
  45. bSendMessage = OMX_TRUE;
  46. }
  47. Message data;
  48. data.msg_type = 1;
  49. data.msg_flag = 0;
  50. if (bSendMessage)
  51. {
  52. data.pBuffer = pBuffer;
  53. if (msgsnd(pSfCodaj12Implement->sInputMessageQueue, (void *)&data, sizeof(data) - sizeof(data.msg_type), 0) == -1)
  54. {
  55. LOG(SF_LOG_ERR, "msgsnd failed\n");
  56. }
  57. }
  58. if (bSendNULL)
  59. {
  60. data.pBuffer = NULL;
  61. if (msgsnd(pSfCodaj12Implement->sInputMessageQueue, (void *)&data, sizeof(data) - sizeof(data.msg_type), 0) == -1)
  62. {
  63. LOG(SF_LOG_ERR, "msgsnd failed\n");
  64. }
  65. }
  66. EXIT:
  67. FunctionOut();
  68. return ret;
  69. }
  70. #define MAX_INDEX 1
  71. static OMX_ERRORTYPE SF_OMX_FillThisBuffer(
  72. OMX_IN OMX_HANDLETYPE hComponent,
  73. OMX_IN OMX_BUFFERHEADERTYPE *pBuffer)
  74. {
  75. OMX_ERRORTYPE ret = OMX_ErrorNone;
  76. FunctionIn();
  77. if (hComponent == NULL || pBuffer == NULL)
  78. {
  79. ret = OMX_ErrorBadParameter;
  80. goto EXIT;
  81. }
  82. OMX_COMPONENTTYPE *pOMXComponent = (OMX_COMPONENTTYPE *)hComponent;
  83. SF_OMX_COMPONENT *pSfOMXComponent = (SF_OMX_COMPONENT *)pOMXComponent->pComponentPrivate;
  84. SF_CODAJ12_IMPLEMEMT *pSfCodaj12Implement = (SF_CODAJ12_IMPLEMEMT *)pSfOMXComponent->componentImpl;
  85. LOG(SF_LOG_DEBUG, "nFilledLen = %d, nFlags = %d, pBuffer = %p\r\n", pBuffer->nFilledLen, pBuffer->nFlags, pBuffer->pBuffer);
  86. // if (pSfCodaj12Implement->functions->Queue_Enqueue(&pSfCodaj12Implement->decodeBufferQueue, (void *)pBuffer->pBuffer) == FALSE)
  87. // {
  88. // return OMX_ErrorInsufficientResources;
  89. // }
  90. StoreOMXBuffer(pSfOMXComponent, pBuffer);
  91. Message data;
  92. data.msg_type = 1;
  93. data.msg_flag = 0;
  94. data.pBuffer = pBuffer;
  95. LOG(SF_LOG_DEBUG, "Send to message queue\r\n");
  96. if (msgsnd(pSfCodaj12Implement->sOutputMessageQueue, (void *)&data, sizeof(data) - sizeof(data.msg_type), 0) == -1)
  97. {
  98. LOG(SF_LOG_ERR, "msgsnd failed\n");
  99. }
  100. EXIT:
  101. FunctionOut();
  102. return ret;
  103. }
  104. static OMX_U64 nOutBufIndex = 0;
  105. static OMX_ERRORTYPE SF_OMX_UseBuffer(
  106. OMX_IN OMX_HANDLETYPE hComponent,
  107. OMX_INOUT OMX_BUFFERHEADERTYPE **ppBufferHdr,
  108. OMX_IN OMX_U32 nPortIndex,
  109. OMX_IN OMX_PTR pAppPrivate,
  110. OMX_IN OMX_U32 nSizeBytes,
  111. OMX_IN OMX_U8 *pBuffer)
  112. {
  113. OMX_ERRORTYPE ret = OMX_ErrorNone;
  114. OMX_COMPONENTTYPE *pOMXComponent = (OMX_COMPONENTTYPE *)hComponent;
  115. SF_OMX_COMPONENT *pSfOMXComponent = pOMXComponent->pComponentPrivate;
  116. FunctionIn();
  117. if (hComponent == NULL)
  118. {
  119. ret = OMX_ErrorBadParameter;
  120. goto EXIT;
  121. }
  122. if (AttachOutputBuffer(pSfOMXComponent, pBuffer, nSizeBytes) == OMX_FALSE)
  123. {
  124. LOG(SF_LOG_ERR, "Failed to attach dma buffer\r\n");
  125. return OMX_ErrorInsufficientResources;
  126. }
  127. OMX_BUFFERHEADERTYPE *temp_bufferHeader = (OMX_BUFFERHEADERTYPE *)malloc(sizeof(OMX_BUFFERHEADERTYPE));
  128. if (temp_bufferHeader == NULL)
  129. {
  130. LOG(SF_LOG_ERR, "malloc fail\r\n");
  131. return OMX_ErrorInsufficientResources;
  132. }
  133. memset(temp_bufferHeader, 0, sizeof(OMX_BUFFERHEADERTYPE));
  134. temp_bufferHeader->nAllocLen = nSizeBytes;
  135. temp_bufferHeader->pAppPrivate = pAppPrivate;
  136. temp_bufferHeader->pBuffer = pBuffer;
  137. *ppBufferHdr = temp_bufferHeader;
  138. if (nPortIndex == 1)
  139. {
  140. temp_bufferHeader->pOutputPortPrivate = (OMX_PTR)nOutBufIndex;
  141. nOutBufIndex ++;
  142. }
  143. LOG(SF_LOG_DEBUG, "pBuffer address = %p, nOutBufIndex = %d\r\n", temp_bufferHeader->pBuffer, (OMX_U64)temp_bufferHeader->pOutputPortPrivate);
  144. EXIT:
  145. FunctionOut();
  146. return ret;
  147. }
  148. static OMX_ERRORTYPE SF_OMX_AllocateBuffer(
  149. OMX_IN OMX_HANDLETYPE hComponent,
  150. OMX_INOUT OMX_BUFFERHEADERTYPE **ppBuffer,
  151. OMX_IN OMX_U32 nPortIndex,
  152. OMX_IN OMX_PTR pAppPrivate,
  153. OMX_IN OMX_U32 nSizeBytes)
  154. {
  155. OMX_ERRORTYPE ret = OMX_ErrorNone;
  156. OMX_COMPONENTTYPE *pOMXComponent = (OMX_COMPONENTTYPE *)hComponent;
  157. SF_OMX_COMPONENT *pSfOMXComponent = pOMXComponent->pComponentPrivate;
  158. SF_CODAJ12_IMPLEMEMT *pSfCodaj12Implement = pSfOMXComponent->componentImpl;
  159. FunctionIn();
  160. if (nSizeBytes == 0)
  161. {
  162. LOG(SF_LOG_ERR, "nSizeBytes = %d\r\n", nSizeBytes);
  163. return OMX_ErrorBadParameter;
  164. }
  165. OMX_BUFFERHEADERTYPE *temp_bufferHeader = (OMX_BUFFERHEADERTYPE *)malloc(sizeof(OMX_BUFFERHEADERTYPE));
  166. if (temp_bufferHeader == NULL)
  167. {
  168. LOG(SF_LOG_ERR, "malloc fail\r\n");
  169. return OMX_ErrorInsufficientResources;
  170. }
  171. memset(temp_bufferHeader, 0, sizeof(OMX_BUFFERHEADERTYPE));
  172. temp_bufferHeader->nAllocLen = nSizeBytes;
  173. temp_bufferHeader->pAppPrivate = pAppPrivate;
  174. if (nPortIndex == 1)
  175. {
  176. temp_bufferHeader->pBuffer = AllocateOutputBuffer(pSfOMXComponent, nSizeBytes);
  177. // temp_bufferHeader->nFilledLen = nSizeBytes;
  178. }
  179. else if (nPortIndex == 0)
  180. {
  181. jpu_buffer_t *vbStream = &pSfCodaj12Implement->vbStream;
  182. vbStream->size = (nSizeBytes == 0) ? STREAM_BUF_SIZE : nSizeBytes;
  183. vbStream->size = (vbStream->size + 1023) & ~1023; // ceil128(size)
  184. if (pSfCodaj12Implement->functions->jdi_allocate_dma_memory(vbStream) < 0)
  185. {
  186. LOG(SF_LOG_ERR, "fail to allocate bitstream buffer\r\n");
  187. return OMX_ErrorInsufficientResources;
  188. }
  189. temp_bufferHeader->pBuffer = (OMX_U8 *)vbStream->virt_addr;
  190. temp_bufferHeader->pInputPortPrivate = (void *)vbStream->phys_addr;
  191. }
  192. if (temp_bufferHeader->pBuffer == NULL)
  193. {
  194. free(temp_bufferHeader);
  195. LOG(SF_LOG_ERR, "malloc fail\r\n");
  196. return OMX_ErrorInsufficientResources;
  197. }
  198. *ppBuffer = temp_bufferHeader;
  199. LOG(SF_LOG_INFO, "nPortIndex = %d, pBuffer address = %p, nFilledLen = %d, nSizeBytes = %d\r\n",
  200. nPortIndex, temp_bufferHeader->pBuffer, temp_bufferHeader->nFilledLen, nSizeBytes);
  201. FunctionOut();
  202. return ret;
  203. }
  204. static OMX_ERRORTYPE SF_OMX_GetParameter(
  205. OMX_IN OMX_HANDLETYPE hComponent,
  206. OMX_IN OMX_INDEXTYPE nParamIndex,
  207. OMX_INOUT OMX_PTR ComponentParameterStructure)
  208. {
  209. OMX_ERRORTYPE ret = OMX_ErrorNone;
  210. OMX_COMPONENTTYPE *pOMXComponent = (OMX_COMPONENTTYPE *)hComponent;
  211. SF_OMX_COMPONENT *pSfOMXComponent = pOMXComponent->pComponentPrivate;
  212. FunctionIn();
  213. if (hComponent == NULL)
  214. {
  215. ret = OMX_ErrorBadParameter;
  216. goto EXIT;
  217. }
  218. if (ComponentParameterStructure == NULL)
  219. {
  220. ret = OMX_ErrorBadParameter;
  221. goto EXIT;
  222. }
  223. LOG(SF_LOG_INFO, "Get parameter on index %X\r\n", nParamIndex);
  224. switch ((OMX_U32)nParamIndex)
  225. {
  226. case OMX_IndexParamImageInit:
  227. case OMX_IndexParamVideoInit:
  228. {
  229. OMX_PORT_PARAM_TYPE *portParam = (OMX_PORT_PARAM_TYPE *)ComponentParameterStructure;
  230. portParam->nPorts = NUM_OF_PORTS;
  231. portParam->nStartPortNumber = OMX_INPUT_PORT_INDEX;
  232. break;
  233. }
  234. case OMX_IndexParamImagePortFormat:
  235. {
  236. OMX_IMAGE_PARAM_PORTFORMATTYPE *portFormat = (OMX_IMAGE_PARAM_PORTFORMATTYPE *)ComponentParameterStructure;
  237. OMX_U32 index = portFormat->nIndex;
  238. switch (index)
  239. {
  240. case OMX_INPUT_PORT_INDEX:
  241. portFormat->eCompressionFormat = OMX_VIDEO_CodingUnused;
  242. portFormat->eColorFormat = OMX_COLOR_FormatYUV420SemiPlanar;
  243. default:
  244. if (index > 0)
  245. {
  246. ret = OMX_ErrorNoMore;
  247. }
  248. }
  249. break;
  250. }
  251. case OMX_IndexParamPortDefinition:
  252. {
  253. OMX_PARAM_PORTDEFINITIONTYPE *pPortDefinition = (OMX_PARAM_PORTDEFINITIONTYPE *)ComponentParameterStructure;
  254. OMX_U32 portIndex = pPortDefinition->nPortIndex;
  255. OMX_PARAM_PORTDEFINITIONTYPE *pSrcDefinition = &pSfOMXComponent->portDefinition[portIndex];
  256. LOG(SF_LOG_DEBUG, "Get parameter port %X\r\n",portIndex);
  257. LOG_APPEND(SF_LOG_DEBUG, "width = %d, height = %d\r\n", pSrcDefinition->format.video.nFrameWidth, pSrcDefinition->format.video.nFrameHeight);
  258. LOG_APPEND(SF_LOG_DEBUG, "eColorFormat = %d\r\n", pSrcDefinition->format.video.eColorFormat);
  259. LOG_APPEND(SF_LOG_DEBUG, "xFramerate = %d\r\n", pSrcDefinition->format.video.xFramerate);
  260. LOG_APPEND(SF_LOG_DEBUG, "bufferSize = %d\r\n",pSrcDefinition->nBufferSize);
  261. LOG_APPEND(SF_LOG_DEBUG, "Buffer count = %d\r\n", pSrcDefinition->nBufferCountActual);
  262. memcpy(pPortDefinition, pSrcDefinition, pPortDefinition->nSize);
  263. break;
  264. }
  265. case OMX_IndexParamVideoPortFormat:
  266. {
  267. OMX_VIDEO_PARAM_PORTFORMATTYPE *portFormat = (OMX_VIDEO_PARAM_PORTFORMATTYPE *)ComponentParameterStructure;
  268. OMX_U32 index = portFormat->nIndex;
  269. portFormat->eCompressionFormat = OMX_VIDEO_CodingUnused;
  270. portFormat->xFramerate = 30;
  271. LOG(SF_LOG_DEBUG, "Get video format at port %d index %d\r\n",portFormat->nPortIndex, index);
  272. switch (index)
  273. {
  274. case 0:
  275. portFormat->eColorFormat = OMX_COLOR_FormatYUV420Planar;
  276. break;
  277. case 1:
  278. portFormat->eColorFormat = OMX_COLOR_FormatYUV422Planar;
  279. break;
  280. case 2:
  281. portFormat->eColorFormat = OMX_COLOR_FormatYUV444Interleaved;
  282. break;
  283. case 3:
  284. portFormat->eColorFormat = OMX_COLOR_FormatYUV420SemiPlanar; //NV12
  285. break;
  286. case 4:
  287. portFormat->eColorFormat = OMX_COLOR_FormatYUV420PackedSemiPlanar; //NV21
  288. break;
  289. default:
  290. if (index > 0)
  291. {
  292. ret = OMX_ErrorNoMore;
  293. }
  294. }
  295. break;
  296. }
  297. default:
  298. {
  299. ret = OMX_ErrorNotImplemented;
  300. break;
  301. }
  302. }
  303. EXIT:
  304. FunctionOut();
  305. return ret;
  306. }
  307. static OMX_ERRORTYPE SF_OMX_SetParameter(
  308. OMX_IN OMX_HANDLETYPE hComponent,
  309. OMX_IN OMX_INDEXTYPE nIndex,
  310. OMX_IN OMX_PTR ComponentParameterStructure)
  311. {
  312. OMX_ERRORTYPE ret = OMX_ErrorNone;
  313. FunctionIn();
  314. if (hComponent == NULL)
  315. {
  316. ret = OMX_ErrorBadParameter;
  317. goto EXIT;
  318. }
  319. if (ComponentParameterStructure == NULL)
  320. {
  321. ret = OMX_ErrorBadParameter;
  322. goto EXIT;
  323. }
  324. LOG(SF_LOG_INFO, "Set parameter on index %X\r\n", nIndex);
  325. OMX_COMPONENTTYPE *pOMXComponent = (OMX_COMPONENTTYPE *)hComponent;
  326. SF_OMX_COMPONENT *pSfOMXComponent = pOMXComponent->pComponentPrivate;
  327. SF_CODAJ12_IMPLEMEMT *pSfMjpegImplement = (SF_CODAJ12_IMPLEMEMT *)pSfOMXComponent->componentImpl;
  328. if (pSfOMXComponent == NULL)
  329. {
  330. ret = OMX_ErrorBadParameter;
  331. goto EXIT;
  332. }
  333. switch ((OMX_U32)nIndex)
  334. {
  335. case OMX_IndexParamPortDefinition:
  336. {
  337. OMX_PARAM_PORTDEFINITIONTYPE *pPortDefinition =
  338. (OMX_PARAM_PORTDEFINITIONTYPE *)ComponentParameterStructure;
  339. OMX_PARAM_PORTDEFINITIONTYPE *pInputPort = &pSfOMXComponent->portDefinition[0];
  340. OMX_PARAM_PORTDEFINITIONTYPE *pOutputPort = &pSfOMXComponent->portDefinition[1];
  341. DecConfigParam *decConfig = pSfMjpegImplement->config;
  342. OMX_U32 portIndex = pPortDefinition->nPortIndex;
  343. OMX_U32 width = pPortDefinition->format.video.nFrameWidth;
  344. OMX_U32 height = pPortDefinition->format.video.nFrameHeight;
  345. LOG(SF_LOG_INFO, "Set parameter on port %d\r\n", pPortDefinition->nPortIndex);
  346. LOG_APPEND(SF_LOG_DEBUG, "width = %d, height = %d\r\n",width, height);
  347. LOG_APPEND(SF_LOG_DEBUG, "eColorFormat = %d\r\n", pPortDefinition->format.video.eColorFormat);
  348. LOG_APPEND(SF_LOG_DEBUG, "xFramerate = %d\r\n", pPortDefinition->format.video.xFramerate);
  349. LOG_APPEND(SF_LOG_DEBUG, "bufferSize = %d\r\n",pPortDefinition->nBufferSize);
  350. LOG_APPEND(SF_LOG_DEBUG, "Buffer count = %d\r\n", pPortDefinition->nBufferCountActual);
  351. if (portIndex == (OMX_U32)OMX_INPUT_PORT_INDEX)
  352. {
  353. memcpy(&pSfOMXComponent->portDefinition[portIndex], pPortDefinition, pPortDefinition->nSize);
  354. pInputPort->format.video.nStride = width;
  355. pInputPort->format.video.nSliceHeight = height;
  356. pInputPort->nBufferSize = width * height * 2;
  357. }
  358. else if (portIndex == (OMX_U32)(OMX_OUTPUT_PORT_INDEX))
  359. {
  360. memcpy(&pSfOMXComponent->portDefinition[portIndex], pPortDefinition, pPortDefinition->nSize);
  361. /*
  362. Some client may set '0' or '1' to output port, in this case use input port parameters
  363. */
  364. if (width <= 1)
  365. {
  366. width = pInputPort->format.video.nFrameWidth;
  367. }
  368. if (height <= 1)
  369. {
  370. height = pInputPort->format.video.nFrameHeight;
  371. }
  372. if (width > 0 && height > 0)
  373. {
  374. int scalew = pInputPort->format.video.nFrameWidth / width;
  375. int scaleh = pInputPort->format.video.nFrameHeight / height;
  376. if (scalew > 8 || scaleh > 8 || scalew < 1 || scaleh < 1)
  377. {
  378. int nInputWidth = pInputPort->format.video.nFrameWidth;
  379. int nInputHeight = pInputPort->format.video.nFrameHeight;
  380. LOG(SF_LOG_WARN, "Scaling should be 1 to 1/8 (down-scaling only)! Use input parameter. "
  381. "OutPut[%d, %d]. Input[%d, %d]\r\n",
  382. width, height, nInputWidth, nInputHeight);
  383. width = nInputWidth;
  384. height = nInputHeight;
  385. }
  386. pOutputPort->format.video.nFrameWidth = width;
  387. pOutputPort->format.video.nFrameHeight = height;
  388. pOutputPort->format.video.nStride = width;
  389. pOutputPort->format.video.nSliceHeight = height;
  390. switch (pOutputPort->format.video.eColorFormat)
  391. {
  392. case OMX_COLOR_FormatYUV420Planar:
  393. decConfig->cbcrInterleave = CBCR_SEPARATED;
  394. decConfig->packedFormat = PACKED_FORMAT_NONE;
  395. pOutputPort->nBufferSize = (width * height * 3) / 2;
  396. break;
  397. case OMX_COLOR_FormatYUV420SemiPlanar: //NV12
  398. decConfig->cbcrInterleave = CBCR_INTERLEAVE;
  399. decConfig->packedFormat = PACKED_FORMAT_NONE;
  400. pOutputPort->nBufferSize = (width * height * 3) / 2;
  401. break;
  402. case OMX_COLOR_FormatYUV420PackedSemiPlanar: //NV21
  403. decConfig->cbcrInterleave = CRCB_INTERLEAVE;
  404. decConfig->packedFormat = PACKED_FORMAT_NONE;
  405. pOutputPort->nBufferSize = (width * height * 3) / 2;
  406. break;
  407. case OMX_COLOR_FormatYUV422Planar:
  408. pOutputPort->nBufferSize = width * height * 2;
  409. break;
  410. case OMX_COLOR_FormatYUV444Interleaved:
  411. pOutputPort->nBufferSize = width * height * 3;
  412. break;
  413. default:
  414. pOutputPort->nBufferSize = width * height * 4;
  415. break;
  416. }
  417. }
  418. }
  419. else
  420. {
  421. ret = OMX_ErrorNoMore;
  422. }
  423. break;
  424. }
  425. case OMX_IndexParamVideoPortFormat:
  426. {
  427. OMX_VIDEO_PARAM_PORTFORMATTYPE *portFormat = (OMX_VIDEO_PARAM_PORTFORMATTYPE *)ComponentParameterStructure;
  428. OMX_U32 nPortIndex = portFormat->nPortIndex;
  429. OMX_PARAM_PORTDEFINITIONTYPE *pPort = &pSfOMXComponent->portDefinition[nPortIndex];
  430. DecConfigParam *decConfig = pSfMjpegImplement->config;
  431. LOG(SF_LOG_DEBUG, "Set video format to port %d color %d\r\n", portFormat->nPortIndex, portFormat->eColorFormat);
  432. switch (portFormat->eColorFormat)
  433. {
  434. case OMX_COLOR_FormatYUV420Planar:
  435. decConfig->cbcrInterleave = CBCR_SEPARATED;
  436. decConfig->packedFormat = PACKED_FORMAT_NONE;
  437. pSfMjpegImplement->frameFormat = FORMAT_420;
  438. pPort->format.video.eColorFormat = portFormat->eColorFormat;
  439. break;
  440. case OMX_COLOR_FormatYUV420SemiPlanar: //NV12
  441. decConfig->cbcrInterleave = CBCR_INTERLEAVE;
  442. decConfig->packedFormat = PACKED_FORMAT_NONE;
  443. pSfMjpegImplement->frameFormat = FORMAT_420;
  444. pPort->format.video.eColorFormat = portFormat->eColorFormat;
  445. break;
  446. case OMX_COLOR_FormatYUV420PackedSemiPlanar: //NV21
  447. decConfig->cbcrInterleave = CRCB_INTERLEAVE;
  448. decConfig->packedFormat = PACKED_FORMAT_NONE;
  449. pSfMjpegImplement->frameFormat = FORMAT_420;
  450. pPort->format.video.eColorFormat = portFormat->eColorFormat;
  451. break;
  452. case OMX_COLOR_FormatYUV422Planar:
  453. pSfMjpegImplement->frameFormat = FORMAT_422;
  454. pPort->format.video.eColorFormat = portFormat->eColorFormat;
  455. break;
  456. case OMX_COLOR_FormatYUV444Interleaved:
  457. pSfMjpegImplement->frameFormat = FORMAT_444;
  458. pPort->format.video.eColorFormat = portFormat->eColorFormat;
  459. break;
  460. default:
  461. ret = OMX_ErrorNotImplemented;
  462. break;
  463. }
  464. }
  465. case OMX_IndexParamVideoInit:
  466. {
  467. OMX_PORT_PARAM_TYPE *portParam = (OMX_PORT_PARAM_TYPE *)ComponentParameterStructure;
  468. portParam->nPorts = 2;
  469. portParam->nStartPortNumber = 0;
  470. break;
  471. }
  472. case OMX_IndexParamImagePortFormat:
  473. break;
  474. default:
  475. break;
  476. }
  477. EXIT:
  478. FunctionOut();
  479. return ret;
  480. }
  481. static OMX_ERRORTYPE SF_OMX_GetConfig(
  482. OMX_HANDLETYPE hComponent,
  483. OMX_INDEXTYPE nIndex,
  484. OMX_PTR pComponentConfigStructure)
  485. {
  486. OMX_ERRORTYPE ret = OMX_ErrorNone;
  487. FunctionIn();
  488. if (hComponent == NULL)
  489. {
  490. return OMX_ErrorBadParameter;
  491. }
  492. FunctionOut();
  493. return ret;
  494. }
  495. static OMX_ERRORTYPE SF_OMX_SetConfig(
  496. OMX_HANDLETYPE hComponent,
  497. OMX_INDEXTYPE nIndex,
  498. OMX_PTR pComponentConfigStructure)
  499. {
  500. OMX_ERRORTYPE ret = OMX_ErrorNone;
  501. FunctionIn();
  502. if (hComponent == NULL)
  503. {
  504. return OMX_ErrorBadParameter;
  505. }
  506. FunctionOut();
  507. return ret;
  508. }
  509. static OMX_ERRORTYPE InitDecoder(SF_OMX_COMPONENT *pSfOMXComponent)
  510. {
  511. FunctionIn();
  512. SF_CODAJ12_IMPLEMEMT *pSfCodaj12Implement = pSfOMXComponent->componentImpl;
  513. JpgRet ret = JPG_RET_SUCCESS;
  514. DecConfigParam *decConfig = pSfCodaj12Implement->config;
  515. jpu_buffer_t *vbStream = &pSfCodaj12Implement->vbStream;
  516. ret = pSfCodaj12Implement->functions->JPU_Init();
  517. if (ret != JPG_RET_SUCCESS && ret != JPG_RET_CALLED_BEFORE)
  518. {
  519. LOG(SF_LOG_ERR, "JPU_Init failed Error code is 0x%x \r\n", ret);
  520. return ret;
  521. }
  522. decConfig->feedingMode = FEEDING_METHOD_BUFFER;
  523. LOG(SF_LOG_DEBUG, "feedingMode = %d, StreamEndian = %d\r\n", decConfig->feedingMode, decConfig->StreamEndian);
  524. if ((pSfCodaj12Implement->feeder = pSfCodaj12Implement->functions->BitstreamFeeder_Create(decConfig->bitstreamFileName, decConfig->feedingMode, (EndianMode)decConfig->StreamEndian)) == NULL)
  525. {
  526. return OMX_ErrorResourcesLost;
  527. }
  528. FunctionOut();
  529. return OMX_ErrorNone;
  530. pSfCodaj12Implement->functions->FreeFrameBuffer(pSfCodaj12Implement->instIdx);
  531. pSfCodaj12Implement->functions->jdi_free_dma_memory(vbStream);
  532. pSfCodaj12Implement->functions->JPU_DeInit();
  533. FunctionOut();
  534. return OMX_ErrorHardware;
  535. }
  536. static OMX_BOOL FillBufferDone(SF_OMX_COMPONENT *pSfOMXComponent, OMX_BUFFERHEADERTYPE *pBuffer)
  537. {
  538. struct timeval tv;
  539. static OMX_U32 dec_cnt = 0;
  540. OMX_U32 fps = 0;
  541. OMX_U64 diff_time = 0; // ms
  542. static struct timeval tv_old = {0};
  543. FunctionIn();
  544. gettimeofday(&tv, NULL);
  545. if (gInitTimeStamp == 0)
  546. {
  547. gInitTimeStamp = tv.tv_sec * 1000000 + tv.tv_usec;
  548. }
  549. pBuffer->nTimeStamp = tv.tv_sec * 1000000 + tv.tv_usec - gInitTimeStamp;
  550. if (dec_cnt == 0) {
  551. tv_old = tv;
  552. }
  553. if (dec_cnt++ >= 50) {
  554. diff_time = (tv.tv_sec - tv_old.tv_sec) * 1000 + (tv.tv_usec - tv_old.tv_usec) / 1000;
  555. fps = 1000 * (dec_cnt - 1) / diff_time;
  556. dec_cnt = 0;
  557. LOG(SF_LOG_WARN, "Decoding fps: %d \r\n", fps);
  558. }
  559. LOG(SF_LOG_PERF, "OMX finish one buffer, address = %p, size = %d, nTimeStamp = %d, nFlags = %X\r\n",
  560. pBuffer->pBuffer, pBuffer->nFilledLen, pBuffer->nTimeStamp, pBuffer->nFlags);
  561. // Following comment store data loal
  562. // {
  563. // FILE *fb = fopen("./out.bcp", "ab+");
  564. // LOG(SF_LOG_INFO, "%p %d\r\n", pBuffer->pBuffer, pBuffer->nFilledLen);
  565. // fwrite(pBuffer->pBuffer, 1, pBuffer->nFilledLen, fb);
  566. // fclose(fb);
  567. // }
  568. pSfOMXComponent->callbacks->FillBufferDone(pSfOMXComponent->pOMXComponent, pSfOMXComponent->pAppData, pBuffer);
  569. FunctionOut();
  570. return OMX_TRUE;
  571. }
  572. static OMX_BOOL EmptyBufferDone(SF_OMX_COMPONENT *pSfOMXComponent)
  573. {
  574. SF_CODAJ12_IMPLEMEMT *pSfCodaj12Implement = pSfOMXComponent->componentImpl;
  575. OMX_BUFFERHEADERTYPE *pOMXBuffer = NULL;
  576. Message data;
  577. ssize_t ret;
  578. FunctionIn();
  579. LOG(SF_LOG_INFO, "Wait for done used buffer\r\n");
  580. ret = msgrcv(pSfCodaj12Implement->sBufferDoneQueue, (void *)&data, BUFSIZ, 0, IPC_NOWAIT);
  581. if (ret < 0)
  582. {
  583. LOG(SF_LOG_ERR, "msgrcv failed ret = %x\r\n", ret);
  584. return OMX_FALSE;
  585. }
  586. // switch (ret)
  587. // {
  588. // case ENOMSG:
  589. // LOG(SF_LOG_INFO, "msgrcv no message\n");
  590. // return OMX_TRUE;
  591. // default:
  592. // LOG(SF_LOG_ERR, "msgrcv failed ret = %x\r\n", ret);
  593. // return OMX_FALSE;
  594. // }
  595. pOMXBuffer = data.pBuffer;
  596. //TODO: input/output Buffer done
  597. LOG(SF_LOG_DEBUG, "EmptyBufferDone IN\r\n");
  598. pSfOMXComponent->callbacks->EmptyBufferDone(pSfOMXComponent->pOMXComponent, pSfOMXComponent->pAppData, pOMXBuffer);
  599. LOG(SF_LOG_DEBUG, "EmptyBufferDone OUT\r\n");
  600. FunctionOut();
  601. return OMX_TRUE;
  602. }
  603. static OMX_U32 FeedData(SF_OMX_COMPONENT *pSfOMXComponent)
  604. {
  605. FunctionIn();
  606. SF_CODAJ12_IMPLEMEMT *pSfCodaj12Implement = pSfOMXComponent->componentImpl;
  607. BSFeeder feeder = pSfCodaj12Implement->feeder;
  608. JpgDecHandle handle = pSfCodaj12Implement->handle;
  609. jpu_buffer_t *vbStream = &pSfCodaj12Implement->vbStream;
  610. OMX_U32 nFilledLen = 0;
  611. OMX_BUFFERHEADERTYPE *pOMXBuffer = NULL;
  612. Message data;
  613. LOG(SF_LOG_INFO, "Wait for input buffer\r\n");
  614. if (msgrcv(pSfCodaj12Implement->sInputMessageQueue, (void *)&data, BUFSIZ, 0, 0) == -1)
  615. {
  616. LOG(SF_LOG_ERR, "msgrcv failed\n");
  617. return 0;
  618. }
  619. pOMXBuffer = data.pBuffer;
  620. // if (pOMXBuffer == NULL || data.msg_flag == OMX_BUFFERFLAG_EOS)
  621. // {
  622. // return 0;
  623. // }
  624. if (pOMXBuffer != NULL)
  625. {
  626. if (pOMXBuffer->pBuffer != NULL && pOMXBuffer->nFilledLen > 0)
  627. {
  628. nFilledLen = pOMXBuffer->nFilledLen;
  629. LOG(SF_LOG_INFO, "Address = %p, size = %d\r\n", pOMXBuffer->pBuffer, nFilledLen);
  630. pSfCodaj12Implement->functions->BitstreamFeeder_SetData(feeder, pOMXBuffer->pBuffer, nFilledLen);
  631. pSfCodaj12Implement->functions->BitstreamFeeder_Act(feeder, handle, vbStream);
  632. }
  633. LOG(SF_LOG_INFO, "Send %p to sBufferDoneQueue\r\n", pOMXBuffer);
  634. if (msgsnd(pSfCodaj12Implement->sBufferDoneQueue, (void *)&data, sizeof(data) - sizeof(data.msg_type), 0) == -1)
  635. {
  636. LOG(SF_LOG_ERR, "msgsnd failed\n");
  637. return 0;
  638. }
  639. }
  640. // LOG(SF_LOG_DEBUG, "EmptyBufferDone IN\r\n");
  641. // pSfOMXComponent->callbacks->EmptyBufferDone(pSfOMXComponent->pOMXComponent, pSfOMXComponent->pAppData, pOMXBuffer);
  642. // LOG(SF_LOG_DEBUG, "EmptyBufferDone OUT\r\n");
  643. // if (pOMXBuffer->nFlags & 0x1 == 0x1)
  644. // {
  645. // pSfCodaj12Implement->bThreadRunning = OMX_FALSE;
  646. // }
  647. FunctionOut();
  648. return nFilledLen;
  649. }
  650. static OMX_ERRORTYPE WaitForOutputBufferReady(SF_OMX_COMPONENT *pSfOMXComponent)
  651. {
  652. FunctionIn();
  653. OMX_ERRORTYPE ret = OMX_ErrorNone;
  654. SF_CODAJ12_IMPLEMEMT *pSfCodaj12Implement = pSfOMXComponent->componentImpl;
  655. OMX_BUFFERHEADERTYPE *pOMXBuffer = NULL;
  656. Message data;
  657. LOG(SF_LOG_INFO, "Wait for output buffer\r\n");
  658. if (msgrcv(pSfCodaj12Implement->sOutputMessageQueue, (void *)&data, BUFSIZ, 0, 0) == -1)
  659. {
  660. LOG(SF_LOG_ERR, "msgrcv failed\n");
  661. return OMX_ErrorInsufficientResources;
  662. }
  663. pOMXBuffer = data.pBuffer;
  664. // if (pOMXBuffer == NULL || data.msg_flag == OMX_BUFFERFLAG_EOS)
  665. if (pOMXBuffer == NULL)
  666. {
  667. LOG(SF_LOG_INFO, "Buffer end flag detected! \r\n");
  668. pSfCodaj12Implement->bThreadRunning = OMX_FALSE;
  669. ret = OMX_ErrorNoMore;
  670. }
  671. FunctionOut();
  672. return ret;
  673. }
  674. static void ProcessThread(void *args)
  675. {
  676. SF_OMX_COMPONENT *pSfOMXComponent = (SF_OMX_COMPONENT *)args;
  677. SF_CODAJ12_IMPLEMEMT *pSfCodaj12Implement = pSfOMXComponent->componentImpl;
  678. jpu_buffer_t *vbStream = &pSfCodaj12Implement->vbStream;
  679. JpgDecInitialInfo *initialInfo = &pSfCodaj12Implement->initialInfo;
  680. JpgDecOpenParam *decOP = &pSfCodaj12Implement->decOP;
  681. DecConfigParam *decConfig = pSfCodaj12Implement->config;
  682. JpgDecHandle handle;
  683. Int32 frameIdx = 0;
  684. JpgDecParam decParam = {0};
  685. JpgDecOutputInfo outputInfo = {0};
  686. Int32 int_reason = 0;
  687. Int32 instIdx;
  688. JpgRet ret = JPG_RET_SUCCESS;
  689. FunctionIn();
  690. prctl(PR_SET_NAME, pSfOMXComponent->componentName);
  691. LOG(SF_LOG_INFO, "vbStream phys_addr = %x, size = %d, virt_addr = %x\r\n", vbStream->phys_addr, vbStream->size, vbStream->virt_addr);
  692. decOP->streamEndian = decConfig->StreamEndian;
  693. decOP->frameEndian = decConfig->FrameEndian;
  694. decOP->bitstreamBuffer = vbStream->phys_addr;
  695. decOP->bitstreamBufferSize = vbStream->size;
  696. //set virtual address mapped of physical address
  697. decOP->pBitStream = (BYTE *)vbStream->virt_addr; //lint !e511
  698. decOP->chromaInterleave = decConfig->cbcrInterleave;
  699. decOP->packedFormat = decConfig->packedFormat;
  700. decOP->roiEnable = decConfig->roiEnable;
  701. decOP->roiOffsetX = decConfig->roiOffsetX;
  702. decOP->roiOffsetY = decConfig->roiOffsetY;
  703. decOP->roiWidth = decConfig->roiWidth;
  704. decOP->roiHeight = decConfig->roiHeight;
  705. decOP->rotation = decConfig->rotation;
  706. decOP->mirror = decConfig->mirror;
  707. decOP->pixelJustification = decConfig->pixelJustification;
  708. decOP->outputFormat = pSfCodaj12Implement->frameFormat; //decConfig->subsample;
  709. decOP->intrEnableBit = ((1 << INT_JPU_DONE) | (1 << INT_JPU_ERROR) | (1 << INT_JPU_BIT_BUF_EMPTY));
  710. LOG(SF_LOG_DEBUG, "streamEndian = %x\r\n", decOP->streamEndian);
  711. LOG_APPEND(SF_LOG_DEBUG, "frameEndian = %x\r\n", decOP->frameEndian);
  712. LOG_APPEND(SF_LOG_DEBUG, "bitstreamBuffer = %x\r\n", decOP->bitstreamBuffer);
  713. LOG_APPEND(SF_LOG_DEBUG, "bitstreamBufferSize = %x\r\n", decOP->bitstreamBufferSize);
  714. LOG_APPEND(SF_LOG_DEBUG, "pBitStream = %x\r\n", decOP->pBitStream);
  715. LOG_APPEND(SF_LOG_DEBUG, "chromaInterleave = %x\r\n", decOP->chromaInterleave);
  716. LOG_APPEND(SF_LOG_DEBUG, "packedFormat = %x\r\n", decOP->packedFormat);
  717. LOG_APPEND(SF_LOG_DEBUG, "roiEnable = %x\r\n", decOP->roiEnable);
  718. LOG_APPEND(SF_LOG_DEBUG, "roiOffsetX = %x\r\n", decOP->roiOffsetX);
  719. LOG_APPEND(SF_LOG_DEBUG, "roiOffsetY = %x\r\n", decOP->roiOffsetY);
  720. LOG_APPEND(SF_LOG_DEBUG, "roiWidth = %x\r\n", decOP->roiWidth);
  721. LOG_APPEND(SF_LOG_DEBUG, "roiHeight = %x\r\n", decOP->roiHeight);
  722. LOG_APPEND(SF_LOG_DEBUG, "rotation = %x\r\n", decOP->rotation);
  723. LOG_APPEND(SF_LOG_DEBUG, "mirror = %x\r\n", decOP->mirror);
  724. LOG_APPEND(SF_LOG_DEBUG, "pixelJustification = %x\r\n", decOP->pixelJustification);
  725. LOG_APPEND(SF_LOG_DEBUG, "outputFormat = %x\r\n", decOP->outputFormat);
  726. LOG_APPEND(SF_LOG_DEBUG, "intrEnableBit = %x\r\n", decOP->intrEnableBit);
  727. ret = pSfCodaj12Implement->functions->JPU_DecOpen(&handle, decOP);
  728. if (ret != JPG_RET_SUCCESS)
  729. {
  730. LOG(SF_LOG_ERR, "JPU_DecOpen failed Error code is 0x%x \r\n", ret);
  731. return;
  732. }
  733. pSfCodaj12Implement->handle = handle;
  734. instIdx = pSfCodaj12Implement->instIdx = pSfCodaj12Implement->handle->instIndex;
  735. do
  736. {
  737. OMX_U32 size = FeedData(pSfOMXComponent);
  738. if (size <= 0)
  739. {
  740. LOG(SF_LOG_INFO, "FeedData = %d, end thread\r\n", size);
  741. pSfCodaj12Implement->bThreadRunning = OMX_FALSE;
  742. }
  743. if ((ret = pSfCodaj12Implement->functions->JPU_DecGetInitialInfo(handle, initialInfo)) != JPG_RET_SUCCESS)
  744. {
  745. if (JPG_RET_BIT_EMPTY == ret)
  746. {
  747. LOG(SF_LOG_INFO, "<%s:%d> BITSTREAM EMPTY\n", __FUNCTION__, __LINE__);
  748. continue;
  749. }
  750. else
  751. {
  752. LOG(SF_LOG_ERR, "JPU_DecGetInitialInfo failed Error code is 0x%x, inst=%d \n", ret, instIdx);
  753. return;
  754. }
  755. }
  756. } while (JPG_RET_SUCCESS != ret);
  757. //TODO:Set output info
  758. OMX_PARAM_PORTDEFINITIONTYPE *pPortDefinition = &pSfOMXComponent->portDefinition[OMX_OUTPUT_PORT_INDEX];
  759. LOG(SF_LOG_DEBUG, "picWidth = %d, picHeight = %d\r\n", initialInfo->picWidth, initialInfo->picHeight);
  760. pPortDefinition->format.video.nFrameWidth = initialInfo->picWidth;
  761. pPortDefinition->format.video.nFrameHeight = initialInfo->picHeight;
  762. pPortDefinition->format.video.nStride = initialInfo->picWidth;
  763. pPortDefinition->format.video.nSliceHeight = initialInfo->picHeight;
  764. pPortDefinition->format.video.eCompressionFormat = OMX_IMAGE_CodingUnused;
  765. switch (pSfCodaj12Implement->frameFormat)
  766. {
  767. case FORMAT_400:
  768. case FORMAT_420:
  769. pPortDefinition->nBufferSize = initialInfo->picWidth * initialInfo->picHeight * 3 / 2;
  770. break;
  771. case FORMAT_422:
  772. case FORMAT_440:
  773. pPortDefinition->nBufferSize = initialInfo->picWidth * initialInfo->picHeight * 2;
  774. break;
  775. case FORMAT_444:
  776. case FORMAT_MAX:
  777. pPortDefinition->nBufferSize = initialInfo->picWidth * initialInfo->picHeight * 3;
  778. break;
  779. default:
  780. pPortDefinition->nBufferSize = initialInfo->picWidth * initialInfo->picHeight * 4;
  781. break;
  782. }
  783. LOG(SF_LOG_DEBUG, "Output buffer size = %d\r\n", pPortDefinition->nBufferSize);
  784. if (pPortDefinition->nBufferCountActual < initialInfo->minFrameBufferCount)
  785. {
  786. pPortDefinition->nBufferCountActual = initialInfo->minFrameBufferCount;
  787. }
  788. if (pPortDefinition->nBufferCountMin < initialInfo->minFrameBufferCount)
  789. {
  790. pPortDefinition->nBufferCountMin = initialInfo->minFrameBufferCount;
  791. }
  792. LOG(SF_LOG_DEBUG, "OMX_EventPortSettingsChanged IN\r\n");
  793. pSfOMXComponent->callbacks->EventHandler(pSfOMXComponent->pOMXComponent, pSfOMXComponent->pAppData, OMX_EventPortSettingsChanged,
  794. 1, OMX_IndexParamPortDefinition, NULL);
  795. LOG(SF_LOG_DEBUG, "OMX_EventPortSettingsChanged OUT\r\n");
  796. LOG(SF_LOG_INFO, "Wait for out buffers ready\r\n");
  797. OMX_U32 nTimeout = 100;
  798. OMX_U32 nBufferNumber = pSfOMXComponent->portDefinition[OMX_OUTPUT_PORT_INDEX].nBufferCountActual;
  799. while (OMX_TRUE)
  800. {
  801. // LOG(SF_LOG_INFO, "GetFrameBufferCount addr = %p\r\n", pSfCodaj12Implement->functions->GetFrameBufferCount);
  802. nBufferNumber = pSfOMXComponent->portDefinition[OMX_OUTPUT_PORT_INDEX].nBufferCountActual;
  803. OMX_U32 number = pSfCodaj12Implement->functions->GetFrameBufferCount(instIdx);
  804. OMX_S32 oldNumber = -1;
  805. if (number < nBufferNumber)
  806. {
  807. if (oldNumber < number)
  808. {
  809. LOG(SF_LOG_INFO, "Wait for output buffer %d/%d\r\n", number, initialInfo->minFrameBufferCount);
  810. oldNumber = number;
  811. }
  812. usleep(50 * 1000);
  813. nTimeout --;
  814. if (nTimeout < 0)
  815. {
  816. LOG(SF_LOG_ERR, "No buffers found on output port\r\n");
  817. LOG(SF_LOG_DEBUG, "OMX_EventBufferFlag IN\r\n");
  818. pSfOMXComponent->callbacks->EventHandler(pSfOMXComponent->pOMXComponent, pSfOMXComponent->pAppData, OMX_EventBufferFlag,
  819. 1, 1, NULL);
  820. LOG(SF_LOG_DEBUG, "OMX_EventBufferFlag OUT\r\n");
  821. return;
  822. }
  823. }
  824. else
  825. {
  826. break;
  827. }
  828. }
  829. FRAME_BUF *pFrame = pSfCodaj12Implement->frame;
  830. LOG(SF_LOG_INFO, "Update and regist %d FrameBuffers\r\n", nBufferNumber);
  831. LOG(SF_LOG_DEBUG, "%20s%20s%20s%20s\r\n", "virtAddr", "Y", "CB", "CR");
  832. for (int i = 0; i < nBufferNumber; i ++)
  833. {
  834. LOG_APPEND(SF_LOG_DEBUG, "%20lX%20lX%20lX%20lX\r\n", pFrame[i].vbY.virt_addr,
  835. pFrame[i].vbY.phys_addr, pFrame[i].vbCb.phys_addr, pFrame[i].vbCr.phys_addr);
  836. }
  837. // ffmpeg: may register frame buffer append
  838. if (pSfCodaj12Implement->functions->JPU_GetFrameBufPool(pSfCodaj12Implement->handle) == NULL)
  839. {
  840. JpgRet ret = JPG_RET_SUCCESS;
  841. pSfCodaj12Implement->functions->UpdateFrameBuffers(instIdx, nBufferNumber, pFrame);
  842. ret = pSfCodaj12Implement->functions->JPU_DecRegisterFrameBuffer(pSfCodaj12Implement->handle, pSfCodaj12Implement->frameBuf,
  843. nBufferNumber, pSfCodaj12Implement->frameBuf[0].stride);
  844. if (ret != JPG_RET_SUCCESS)
  845. {
  846. LOG(SF_LOG_ERR, "JPU_DecRegisterFrameBuffer fail ret = %d\r\n", ret);
  847. pSfCodaj12Implement->bThreadRunning = OMX_FALSE;
  848. }
  849. }
  850. pSfCodaj12Implement->functions->JPU_DecGiveCommand(handle, SET_JPG_SCALE_HOR, &(decConfig->iHorScaleMode));
  851. pSfCodaj12Implement->functions->JPU_DecGiveCommand(handle, SET_JPG_SCALE_VER, &(decConfig->iVerScaleMode));
  852. /* LOG HEADER */
  853. LOG(SF_LOG_INFO, "I F FB_INDEX FRAME_START ECS_START CONSUME RD_PTR WR_PTR CYCLE\n");
  854. LOG(SF_LOG_INFO, "-------------------------------------------------------------------------------\n");
  855. while (pSfCodaj12Implement->bThreadRunning)
  856. {
  857. if (WaitForOutputBufferReady(pSfOMXComponent) != OMX_ErrorNone)
  858. {
  859. continue;
  860. }
  861. ret = pSfCodaj12Implement->functions->JPU_DecStartOneFrame(handle, &decParam);
  862. if (ret != JPG_RET_SUCCESS && ret != JPG_RET_EOS)
  863. {
  864. if (ret == JPG_RET_BIT_EMPTY)
  865. {
  866. LOG(SF_LOG_INFO, "BITSTREAM NOT ENOUGH.............\n");
  867. OMX_U32 size = FeedData(pSfOMXComponent);
  868. if (size <= 0)
  869. {
  870. LOG(SF_LOG_INFO, "FeedData = %d, end thread\r\n", size);
  871. pSfCodaj12Implement->bThreadRunning = OMX_FALSE;
  872. }
  873. continue;
  874. }
  875. LOG(SF_LOG_ERR, "JPU_DecStartOneFrame failed Error code is 0x%x \n", ret);
  876. return;
  877. }
  878. if (ret == JPG_RET_EOS)
  879. {
  880. pSfCodaj12Implement->functions->JPU_DecGetOutputInfo(handle, &outputInfo);
  881. break;
  882. }
  883. while (1)
  884. {
  885. if ((int_reason = pSfCodaj12Implement->functions->JPU_WaitInterrupt(handle, JPU_INTERRUPT_TIMEOUT_MS)) == -1)
  886. {
  887. LOG(SF_LOG_ERR, "Error : timeout happened\n");
  888. pSfCodaj12Implement->functions->JPU_SWReset(handle);
  889. break;
  890. }
  891. if (int_reason & ((1 << INT_JPU_DONE) | (1 << INT_JPU_ERROR) | (1 << INT_JPU_SLICE_DONE)))
  892. {
  893. // Do no clear INT_JPU_DONE and INT_JPU_ERROR interrupt. these will be cleared in JPU_DecGetOutputInfo.
  894. LOG(SF_LOG_INFO, "\tINSTANCE #%d int_reason: %08x\n", handle->instIndex, int_reason);
  895. break;
  896. }
  897. // if (int_reason & (1 << INT_JPU_BIT_BUF_EMPTY))
  898. // {
  899. // if (decConfig->feedingMode != FEEDING_METHOD_FRAME_SIZE)
  900. // {
  901. // pSfCodaj12Implement->functions->BitstreamFeeder_Act(feeder, handle, &vbStream);
  902. // }
  903. // pSfCodaj12Implement->functions->JPU_ClrStatus(handle, (1 << INT_JPU_BIT_BUF_EMPTY));
  904. // }
  905. }
  906. //LOG(SF_LOG_INFO, "\t<->INSTANCE #%d JPU_WaitInterrupt\n", handle->instIndex);
  907. EmptyBufferDone(pSfOMXComponent);
  908. if ((ret = pSfCodaj12Implement->functions->JPU_DecGetOutputInfo(handle, &outputInfo)) != JPG_RET_SUCCESS)
  909. {
  910. LOG(SF_LOG_ERR, "JPU_DecGetOutputInfo failed Error code is 0x%x \n", ret);
  911. return;
  912. }
  913. if (outputInfo.decodingSuccess == 0)
  914. LOG(SF_LOG_ERR, "JPU_DecGetOutputInfo decode fail framdIdx %d \n", frameIdx);
  915. LOG(SF_LOG_INFO, "%02d %04d %8d %8x %8x %10d %8x %8x %10d\n",
  916. instIdx, frameIdx, outputInfo.indexFrameDisplay, outputInfo.bytePosFrameStart, outputInfo.ecsPtr, outputInfo.consumedByte,
  917. outputInfo.rdPtr, outputInfo.wrPtr, outputInfo.frameCycle);
  918. if (outputInfo.indexFrameDisplay == -1)
  919. break;
  920. FRAME_BUF *pFrame = pSfCodaj12Implement->functions->GetFrameBuffer(instIdx, outputInfo.indexFrameDisplay);
  921. OMX_U8 *virtAddr = (OMX_U8 *)pFrame->vbY.virt_addr;
  922. //TODO: Get OMX buffer by virt addr
  923. OMX_BUFFERHEADERTYPE *pBuffer = GetOMXBufferByAddr(pSfOMXComponent, virtAddr);
  924. if (pBuffer == NULL)
  925. {
  926. LOG(SF_LOG_ERR, "Could not find omx buffer by address\r\n");
  927. LOG(SF_LOG_DEBUG, "OMX_EventBufferFlag IN\r\n");
  928. pSfOMXComponent->callbacks->EventHandler(pSfOMXComponent->pOMXComponent, pSfOMXComponent->pAppData, OMX_EventBufferFlag,
  929. 1, 1, NULL);
  930. LOG(SF_LOG_DEBUG, "OMX_EventBufferFlag OUT\r\n");
  931. goto end;
  932. }
  933. ClearOMXBuffer(pSfOMXComponent, pBuffer);
  934. switch (pSfCodaj12Implement->frameFormat)
  935. {
  936. case FORMAT_420:
  937. pBuffer->nFilledLen = outputInfo.decPicWidth * outputInfo.decPicHeight * 3 / 2;
  938. break;
  939. case FORMAT_422:
  940. pBuffer->nFilledLen = outputInfo.decPicWidth * outputInfo.decPicHeight * 2;
  941. break;
  942. case FORMAT_444:
  943. pBuffer->nFilledLen = outputInfo.decPicWidth * outputInfo.decPicHeight * 3;
  944. break;
  945. default:
  946. LOG(SF_LOG_ERR, "Unsupported format: %d\r\n", pSfCodaj12Implement->frameFormat);
  947. break;
  948. }
  949. LOG(SF_LOG_DEBUG, "decPicSize = [%d %d], pBuffer = %p\r\n",
  950. outputInfo.decPicWidth, outputInfo.decPicHeight, pBuffer->pBuffer);
  951. // AdjustFrame(pBuffer, outputInfo.decPicWidth, outputInfo.decPicHeight, nDestStride, nDestHeight);
  952. FillBufferDone(pSfOMXComponent, pBuffer);
  953. // LOG(SF_LOG_DEBUG, "FillBufferDone IN\r\n");
  954. // Following comment store data loal
  955. // {
  956. // FILE *fb = fopen("./out.bcp", "ab+");
  957. // LOG(SF_LOG_INFO, "%p %d\r\n", pBuffer->pBuffer, pBuffer->nFilledLen);
  958. // fwrite(pBuffer->pBuffer, 1, pBuffer->nFilledLen, fb);
  959. // fclose(fb);
  960. // }
  961. // pSfOMXComponent->callbacks->FillBufferDone(pSfOMXComponent->pOMXComponent, pSfOMXComponent->pAppData, pBuffer);
  962. // LOG(SF_LOG_DEBUG, "FillBufferDone OUT\r\n");
  963. if (outputInfo.numOfErrMBs)
  964. {
  965. Int32 errRstIdx, errPosX, errPosY;
  966. errRstIdx = (outputInfo.numOfErrMBs & 0x0F000000) >> 24;
  967. errPosX = (outputInfo.numOfErrMBs & 0x00FFF000) >> 12;
  968. errPosY = (outputInfo.numOfErrMBs & 0x00000FFF);
  969. LOG(SF_LOG_ERR, "Error restart Idx : %d, MCU x:%d, y:%d, in Frame : %d \n", errRstIdx, errPosX, errPosY, frameIdx);
  970. }
  971. // if (pSfCodaj12Implement->bThreadRunning == OMX_FALSE)
  972. // {
  973. // break;
  974. // }
  975. pSfCodaj12Implement->functions->JPU_DecSetRdPtrEx(handle, vbStream->phys_addr, TRUE);
  976. OMX_U32 size = FeedData(pSfOMXComponent);
  977. if (size <= 0)
  978. {
  979. LOG(SF_LOG_INFO, "FeedData = %d, end thread\r\n", size);
  980. pSfCodaj12Implement->bThreadRunning = OMX_FALSE;
  981. }
  982. }
  983. LOG(SF_LOG_DEBUG, "OMX_EventBufferFlag IN\r\n");
  984. pSfOMXComponent->callbacks->EventHandler(pSfOMXComponent->pOMXComponent, pSfOMXComponent->pAppData, OMX_EventBufferFlag,
  985. 1, 1, NULL);
  986. LOG(SF_LOG_DEBUG, "OMX_EventBufferFlag OUT\r\n");
  987. CodaJ12FlushBuffer(pSfOMXComponent, OMX_INPUT_PORT_INDEX);
  988. CodaJ12FlushBuffer(pSfOMXComponent, OMX_OUTPUT_PORT_INDEX);
  989. pSfCodaj12Implement->currentState = OMX_StateIdle;
  990. end:
  991. FunctionOut();
  992. ThreadExit(NULL);
  993. }
  994. static OMX_ERRORTYPE SF_OMX_SendCommand(
  995. OMX_IN OMX_HANDLETYPE hComponent,
  996. OMX_IN OMX_COMMANDTYPE Cmd,
  997. OMX_IN OMX_U32 nParam,
  998. OMX_IN OMX_PTR pCmdData)
  999. {
  1000. OMX_ERRORTYPE ret = OMX_ErrorNone;
  1001. OMX_COMPONENTTYPE *pOMXComponent = (OMX_COMPONENTTYPE *)hComponent;
  1002. SF_OMX_COMPONENT *pSfOMXComponent = pOMXComponent->pComponentPrivate;
  1003. SF_CODAJ12_IMPLEMEMT *pSfCodaj12Implement = pSfOMXComponent->componentImpl;
  1004. FunctionIn();
  1005. if (hComponent == NULL || pSfOMXComponent == NULL)
  1006. {
  1007. ret = OMX_ErrorBadParameter;
  1008. goto EXIT;
  1009. }
  1010. LOG(SF_LOG_INFO, "cmd = %X, nParam = %X\r\n", Cmd, nParam);
  1011. switch (Cmd)
  1012. {
  1013. case OMX_CommandStateSet:
  1014. pSfOMXComponent->nextState = nParam;
  1015. LOG(SF_LOG_INFO, "OMX dest state = %X, current state = %X\r\n", nParam, pSfCodaj12Implement->currentState);
  1016. switch (nParam)
  1017. {
  1018. case OMX_StateLoaded:
  1019. break;
  1020. case OMX_StateIdle:
  1021. switch (pSfCodaj12Implement->currentState)
  1022. {
  1023. case OMX_StateExecuting:
  1024. {
  1025. Message data;
  1026. data.msg_type = 1;
  1027. data.msg_flag = OMX_BUFFERFLAG_EOS;
  1028. data.pBuffer = NULL;
  1029. LOG(SF_LOG_DEBUG, "Send to message queue\r\n");
  1030. if (msgsnd(pSfCodaj12Implement->sOutputMessageQueue, (void *)&data, sizeof(data) - sizeof(data.msg_type), 0) == -1)
  1031. {
  1032. LOG(SF_LOG_ERR, "msgsnd failed\n");
  1033. }
  1034. if (msgsnd(pSfCodaj12Implement->sInputMessageQueue, (void *)&data, sizeof(data) - sizeof(data.msg_type), 0) == -1)
  1035. {
  1036. LOG(SF_LOG_ERR, "msgsnd failed\n");
  1037. }
  1038. pSfCodaj12Implement->currentState = OMX_StateIdle;
  1039. break;
  1040. }
  1041. case OMX_StateIdle:
  1042. break;
  1043. case OMX_StateLoaded:
  1044. // InitDecoder(pSfOMXComponent);
  1045. pSfCodaj12Implement->currentState = OMX_StateIdle;
  1046. break;
  1047. default:
  1048. LOG(SF_LOG_ERR, "Can't go to state %d from %d\r\n",pSfCodaj12Implement->currentState, nParam);
  1049. return OMX_ErrorBadParameter;
  1050. break;
  1051. }
  1052. break;
  1053. case OMX_StateExecuting:
  1054. switch (pSfCodaj12Implement->currentState)
  1055. {
  1056. case OMX_StateIdle:
  1057. pSfCodaj12Implement->bThreadRunning = OMX_TRUE;
  1058. CreateThread(&pSfCodaj12Implement->pProcessThread, ProcessThread, (void *)pSfOMXComponent);
  1059. pSfCodaj12Implement->currentState = OMX_StateExecuting;
  1060. break;
  1061. case OMX_StateExecuting:
  1062. break;
  1063. case OMX_StateLoaded:
  1064. default:
  1065. LOG(SF_LOG_ERR, "Can't go to state %d from %d\r\n",pSfCodaj12Implement->currentState, nParam);
  1066. return OMX_ErrorBadParameter;
  1067. break;
  1068. }
  1069. break;
  1070. default:
  1071. ret = OMX_ErrorIncorrectStateTransition;
  1072. break;
  1073. }
  1074. pSfOMXComponent->callbacks->EventHandler(pSfOMXComponent->pOMXComponent, pSfOMXComponent->pAppData,
  1075. OMX_EventCmdComplete, OMX_CommandStateSet, nParam, NULL);
  1076. break;
  1077. case OMX_CommandFlush:
  1078. {
  1079. OMX_U32 nPort = nParam;
  1080. CodaJ12FlushBuffer(pSfOMXComponent, nPort);
  1081. pSfOMXComponent->callbacks->EventHandler(pSfOMXComponent->pOMXComponent, pSfOMXComponent->pAppData,
  1082. OMX_EventCmdComplete, OMX_CommandFlush, nParam, NULL);
  1083. }
  1084. case OMX_CommandPortDisable:
  1085. pSfOMXComponent->callbacks->EventHandler(pSfOMXComponent->pOMXComponent, pSfOMXComponent->pAppData,
  1086. OMX_EventCmdComplete, OMX_CommandPortDisable, nParam, NULL);
  1087. break;
  1088. case OMX_CommandPortEnable:
  1089. pSfOMXComponent->callbacks->EventHandler(pSfOMXComponent->pOMXComponent, pSfOMXComponent->pAppData,
  1090. OMX_EventCmdComplete, OMX_CommandPortEnable, nParam, NULL);
  1091. default:
  1092. break;
  1093. }
  1094. //TODO
  1095. EXIT:
  1096. FunctionOut();
  1097. return ret;
  1098. }
  1099. static OMX_ERRORTYPE SF_OMX_GetState(
  1100. OMX_IN OMX_HANDLETYPE hComponent,
  1101. OMX_OUT OMX_STATETYPE *pState)
  1102. {
  1103. OMX_ERRORTYPE ret = OMX_ErrorNone;
  1104. OMX_COMPONENTTYPE *pOMXComponent = (OMX_COMPONENTTYPE *)hComponent;
  1105. SF_OMX_COMPONENT *pSfOMXComponent = pOMXComponent->pComponentPrivate;
  1106. SF_CODAJ12_IMPLEMEMT *pSfCodaj12Implement = pSfOMXComponent->componentImpl;
  1107. FunctionIn();
  1108. *pState = pSfCodaj12Implement->currentState;
  1109. FunctionOut();
  1110. return ret;
  1111. }
  1112. static OMX_ERRORTYPE SF_OMX_FreeBuffer(
  1113. OMX_IN OMX_HANDLETYPE hComponent,
  1114. OMX_IN OMX_U32 nPortIndex,
  1115. OMX_IN OMX_BUFFERHEADERTYPE *pBufferHdr)
  1116. {
  1117. OMX_ERRORTYPE ret = OMX_ErrorNone;
  1118. FunctionIn();
  1119. //TODO
  1120. FunctionOut();
  1121. return ret;
  1122. }
  1123. static OMX_ERRORTYPE SF_OMX_ComponentConstructor(SF_OMX_COMPONENT *pSfOMXComponent)
  1124. {
  1125. OMX_ERRORTYPE ret = OMX_ErrorNone;
  1126. SF_CODAJ12_IMPLEMEMT *pSfCodaj12Implement;
  1127. FunctionIn();
  1128. ret = InitMjpegStructorCommon(pSfOMXComponent);
  1129. if (ret != OMX_ErrorNone)
  1130. {
  1131. goto EXIT;
  1132. }
  1133. pSfOMXComponent->pOMXComponent->UseBuffer = &SF_OMX_UseBuffer;
  1134. pSfOMXComponent->pOMXComponent->AllocateBuffer = &SF_OMX_AllocateBuffer;
  1135. pSfOMXComponent->pOMXComponent->EmptyThisBuffer = &SF_OMX_EmptyThisBuffer;
  1136. pSfOMXComponent->pOMXComponent->FillThisBuffer = &SF_OMX_FillThisBuffer;
  1137. pSfOMXComponent->pOMXComponent->FreeBuffer = &SF_OMX_FreeBuffer;
  1138. // pSfOMXComponent->pOMXComponent->ComponentTunnelRequest = &SF_OMX_ComponentTunnelRequest;
  1139. pSfOMXComponent->pOMXComponent->GetParameter = &SF_OMX_GetParameter;
  1140. pSfOMXComponent->pOMXComponent->SetParameter = &SF_OMX_SetParameter;
  1141. pSfOMXComponent->pOMXComponent->GetConfig = &SF_OMX_GetConfig;
  1142. pSfOMXComponent->pOMXComponent->SetConfig = &SF_OMX_SetConfig;
  1143. pSfOMXComponent->pOMXComponent->SendCommand = &SF_OMX_SendCommand;
  1144. pSfOMXComponent->pOMXComponent->GetState = &SF_OMX_GetState;
  1145. // pSfOMXComponent->pOMXComponent->GetExtensionIndex = &SF_OMX_GetExtensionIndex;
  1146. // pSfOMXComponent->pOMXComponent->ComponentRoleEnum = &SF_OMX_ComponentRoleEnum;
  1147. // pSfOMXComponent->pOMXComponent->ComponentDeInit = &SF_OMX_ComponentDeInit;
  1148. pSfCodaj12Implement = pSfOMXComponent->componentImpl;
  1149. pSfCodaj12Implement->currentState = OMX_StateLoaded;
  1150. InitDecoder(pSfOMXComponent);
  1151. EXIT:
  1152. FunctionOut();
  1153. return ret;
  1154. }
  1155. static OMX_ERRORTYPE SF_OMX_ComponentClear(SF_OMX_COMPONENT *pSfOMXComponent)
  1156. {
  1157. OMX_ERRORTYPE ret = OMX_ErrorNone;
  1158. SF_CODAJ12_IMPLEMEMT *pSfCodaj12Implement = pSfOMXComponent->componentImpl;
  1159. jpu_buffer_t *vbStream = &pSfCodaj12Implement->vbStream;
  1160. JpgDecHandle handle = pSfCodaj12Implement->handle;
  1161. FunctionIn();
  1162. if (pSfCodaj12Implement->functions->JPU_DecClose(handle) != JPG_RET_SUCCESS)
  1163. {
  1164. LOG(SF_LOG_ERR, "\nDec End\r\n");
  1165. ret = OMX_ErrorHardware;
  1166. }
  1167. pSfCodaj12Implement->functions->BitstreamFeeder_Destroy(pSfCodaj12Implement->feeder);
  1168. pSfCodaj12Implement->functions->FreeFrameBuffer(pSfCodaj12Implement->instIdx);
  1169. pSfCodaj12Implement->functions->jdi_free_dma_memory(vbStream);
  1170. pSfCodaj12Implement->functions->JPU_DeInit();
  1171. // TODO
  1172. // MjpegClearCommon(hComponent);
  1173. FunctionOut();
  1174. return ret;
  1175. }
  1176. //TODO happer
  1177. SF_OMX_COMPONENT sf_dec_decoder_mjpeg = {
  1178. .componentName = "sf.dec.decoder.mjpeg",
  1179. .libName = "libcodadec.so",
  1180. .pOMXComponent = NULL,
  1181. .SF_OMX_ComponentConstructor = SF_OMX_ComponentConstructor,
  1182. .SF_OMX_ComponentClear = SF_OMX_ComponentClear,
  1183. // .functions = NULL,
  1184. // .bitFormat = STD_HEVC,
  1185. .componentImpl = NULL,
  1186. .fwPath = "/lib/firmware/chagall.bin",
  1187. .componentRule = "video_decoder.mjpeg"};