SF_OMX_Mjpeg_decoder.c 48 KB

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