SF_OMX_Mjpeg_decoder.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292
  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", (int)(pBuffer->pOutputPortPrivate));
  868. ret = pSfCodaj12Implement->functions->JPU_DecStartOneFrameBySerialNum(handle, &decParam,(int)(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. end:
  977. FunctionOut();
  978. ThreadExit(NULL);
  979. }
  980. static OMX_ERRORTYPE SF_OMX_SendCommand(
  981. OMX_IN OMX_HANDLETYPE hComponent,
  982. OMX_IN OMX_COMMANDTYPE Cmd,
  983. OMX_IN OMX_U32 nParam,
  984. OMX_IN OMX_PTR pCmdData)
  985. {
  986. OMX_ERRORTYPE ret = OMX_ErrorNone;
  987. OMX_COMPONENTTYPE *pOMXComponent = (OMX_COMPONENTTYPE *)hComponent;
  988. SF_OMX_COMPONENT *pSfOMXComponent = pOMXComponent->pComponentPrivate;
  989. SF_CODAJ12_IMPLEMEMT *pSfCodaj12Implement = pSfOMXComponent->componentImpl;
  990. FunctionIn();
  991. if (hComponent == NULL || pSfOMXComponent == NULL)
  992. {
  993. ret = OMX_ErrorBadParameter;
  994. goto EXIT;
  995. }
  996. LOG(SF_LOG_INFO, "cmd = %X, nParam = %X\r\n", Cmd, nParam);
  997. switch (Cmd)
  998. {
  999. case OMX_CommandStateSet:
  1000. pSfOMXComponent->nextState = nParam;
  1001. LOG(SF_LOG_INFO, "OMX dest state = %X, current state = %X\r\n", nParam, pSfCodaj12Implement->currentState);
  1002. switch (nParam)
  1003. {
  1004. case OMX_StateLoaded:
  1005. break;
  1006. case OMX_StateIdle:
  1007. switch (pSfCodaj12Implement->currentState)
  1008. {
  1009. case OMX_StateExecuting:
  1010. {
  1011. Message data;
  1012. data.msg_type = 1;
  1013. data.msg_flag = OMX_BUFFERFLAG_EOS;
  1014. data.pBuffer = NULL;
  1015. LOG(SF_LOG_DEBUG, "Send to message queue\r\n");
  1016. if (msgsnd(pSfCodaj12Implement->sOutputMessageQueue, (void *)&data, sizeof(data) - sizeof(data.msg_type), 0) == -1)
  1017. {
  1018. LOG(SF_LOG_ERR, "msgsnd failed\n");
  1019. }
  1020. if (msgsnd(pSfCodaj12Implement->sInputMessageQueue, (void *)&data, sizeof(data) - sizeof(data.msg_type), 0) == -1)
  1021. {
  1022. LOG(SF_LOG_ERR, "msgsnd failed\n");
  1023. }
  1024. pSfCodaj12Implement->currentState = OMX_StateIdle;
  1025. break;
  1026. }
  1027. case OMX_StateIdle:
  1028. break;
  1029. case OMX_StateLoaded:
  1030. // InitDecoder(pSfOMXComponent);
  1031. pSfCodaj12Implement->currentState = OMX_StateIdle;
  1032. break;
  1033. default:
  1034. LOG(SF_LOG_ERR, "Can't go to state %d from %d\r\n",pSfCodaj12Implement->currentState, nParam);
  1035. return OMX_ErrorBadParameter;
  1036. break;
  1037. }
  1038. break;
  1039. case OMX_StateExecuting:
  1040. switch (pSfCodaj12Implement->currentState)
  1041. {
  1042. case OMX_StateIdle:
  1043. pSfCodaj12Implement->bThreadRunning = OMX_TRUE;
  1044. CreateThread(&pSfCodaj12Implement->pProcessThread, ProcessThread, (void *)pSfOMXComponent);
  1045. pSfCodaj12Implement->currentState = OMX_StateExecuting;
  1046. break;
  1047. case OMX_StateExecuting:
  1048. break;
  1049. case OMX_StateLoaded:
  1050. default:
  1051. LOG(SF_LOG_ERR, "Can't go to state %d from %d\r\n",pSfCodaj12Implement->currentState, nParam);
  1052. return OMX_ErrorBadParameter;
  1053. break;
  1054. }
  1055. break;
  1056. default:
  1057. ret = OMX_ErrorIncorrectStateTransition;
  1058. break;
  1059. }
  1060. pSfOMXComponent->callbacks->EventHandler(pSfOMXComponent->pOMXComponent, pSfOMXComponent->pAppData,
  1061. OMX_EventCmdComplete, OMX_CommandStateSet, nParam, NULL);
  1062. break;
  1063. case OMX_CommandFlush:
  1064. {
  1065. OMX_U32 nPort = nParam;
  1066. CodaJ12FlushBuffer(pSfOMXComponent, nPort);
  1067. pSfOMXComponent->callbacks->EventHandler(pSfOMXComponent->pOMXComponent, pSfOMXComponent->pAppData,
  1068. OMX_EventCmdComplete, OMX_CommandFlush, nParam, NULL);
  1069. }
  1070. case OMX_CommandPortDisable:
  1071. pSfOMXComponent->callbacks->EventHandler(pSfOMXComponent->pOMXComponent, pSfOMXComponent->pAppData,
  1072. OMX_EventCmdComplete, OMX_CommandPortDisable, nParam, NULL);
  1073. break;
  1074. case OMX_CommandPortEnable:
  1075. pSfOMXComponent->callbacks->EventHandler(pSfOMXComponent->pOMXComponent, pSfOMXComponent->pAppData,
  1076. OMX_EventCmdComplete, OMX_CommandPortEnable, nParam, NULL);
  1077. default:
  1078. break;
  1079. }
  1080. //TODO
  1081. EXIT:
  1082. FunctionOut();
  1083. return ret;
  1084. }
  1085. static OMX_ERRORTYPE SF_OMX_GetState(
  1086. OMX_IN OMX_HANDLETYPE hComponent,
  1087. OMX_OUT OMX_STATETYPE *pState)
  1088. {
  1089. OMX_ERRORTYPE ret = OMX_ErrorNone;
  1090. OMX_COMPONENTTYPE *pOMXComponent = (OMX_COMPONENTTYPE *)hComponent;
  1091. SF_OMX_COMPONENT *pSfOMXComponent = pOMXComponent->pComponentPrivate;
  1092. SF_CODAJ12_IMPLEMEMT *pSfCodaj12Implement = pSfOMXComponent->componentImpl;
  1093. FunctionIn();
  1094. *pState = pSfCodaj12Implement->currentState;
  1095. FunctionOut();
  1096. return ret;
  1097. }
  1098. static OMX_ERRORTYPE SF_OMX_FreeBuffer(
  1099. OMX_IN OMX_HANDLETYPE hComponent,
  1100. OMX_IN OMX_U32 nPortIndex,
  1101. OMX_IN OMX_BUFFERHEADERTYPE *pBufferHdr)
  1102. {
  1103. OMX_ERRORTYPE ret = OMX_ErrorNone;
  1104. FunctionIn();
  1105. //TODO
  1106. FunctionOut();
  1107. return ret;
  1108. }
  1109. static OMX_ERRORTYPE SF_OMX_ComponentConstructor(SF_OMX_COMPONENT *pSfOMXComponent)
  1110. {
  1111. OMX_ERRORTYPE ret = OMX_ErrorNone;
  1112. SF_CODAJ12_IMPLEMEMT *pSfCodaj12Implement;
  1113. FunctionIn();
  1114. ret = InitMjpegStructorCommon(pSfOMXComponent);
  1115. if (ret != OMX_ErrorNone)
  1116. {
  1117. goto EXIT;
  1118. }
  1119. pSfOMXComponent->pOMXComponent->UseBuffer = &SF_OMX_UseBuffer;
  1120. pSfOMXComponent->pOMXComponent->AllocateBuffer = &SF_OMX_AllocateBuffer;
  1121. pSfOMXComponent->pOMXComponent->EmptyThisBuffer = &SF_OMX_EmptyThisBuffer;
  1122. pSfOMXComponent->pOMXComponent->FillThisBuffer = &SF_OMX_FillThisBuffer;
  1123. pSfOMXComponent->pOMXComponent->FreeBuffer = &SF_OMX_FreeBuffer;
  1124. // pSfOMXComponent->pOMXComponent->ComponentTunnelRequest = &SF_OMX_ComponentTunnelRequest;
  1125. pSfOMXComponent->pOMXComponent->GetParameter = &SF_OMX_GetParameter;
  1126. pSfOMXComponent->pOMXComponent->SetParameter = &SF_OMX_SetParameter;
  1127. pSfOMXComponent->pOMXComponent->GetConfig = &SF_OMX_GetConfig;
  1128. pSfOMXComponent->pOMXComponent->SetConfig = &SF_OMX_SetConfig;
  1129. pSfOMXComponent->pOMXComponent->SendCommand = &SF_OMX_SendCommand;
  1130. pSfOMXComponent->pOMXComponent->GetState = &SF_OMX_GetState;
  1131. // pSfOMXComponent->pOMXComponent->GetExtensionIndex = &SF_OMX_GetExtensionIndex;
  1132. // pSfOMXComponent->pOMXComponent->ComponentRoleEnum = &SF_OMX_ComponentRoleEnum;
  1133. // pSfOMXComponent->pOMXComponent->ComponentDeInit = &SF_OMX_ComponentDeInit;
  1134. pSfCodaj12Implement = pSfOMXComponent->componentImpl;
  1135. pSfCodaj12Implement->currentState = OMX_StateLoaded;
  1136. InitDecoder(pSfOMXComponent);
  1137. EXIT:
  1138. FunctionOut();
  1139. return ret;
  1140. }
  1141. static OMX_ERRORTYPE SF_OMX_ComponentClear(SF_OMX_COMPONENT *pSfOMXComponent)
  1142. {
  1143. OMX_ERRORTYPE ret = OMX_ErrorNone;
  1144. SF_CODAJ12_IMPLEMEMT *pSfCodaj12Implement = pSfOMXComponent->componentImpl;
  1145. jpu_buffer_t *vbStream = &pSfCodaj12Implement->vbStream;
  1146. JpgDecHandle handle = pSfCodaj12Implement->handle;
  1147. FunctionIn();
  1148. if (pSfCodaj12Implement->functions->JPU_DecClose(handle) != JPG_RET_SUCCESS)
  1149. {
  1150. LOG(SF_LOG_ERR, "\nDec End\r\n");
  1151. ret = OMX_ErrorHardware;
  1152. }
  1153. pSfCodaj12Implement->functions->BitstreamFeeder_Destroy(pSfCodaj12Implement->feeder);
  1154. if(pSfCodaj12Implement->allocBufFlag == OMX_TRUE)
  1155. {
  1156. pSfCodaj12Implement->functions->FreeFrameBuffer(pSfCodaj12Implement->instIdx);
  1157. }
  1158. pSfCodaj12Implement->functions->jdi_free_dma_memory(vbStream);
  1159. pSfCodaj12Implement->functions->JPU_DeInit();
  1160. // TODO
  1161. // MjpegClearCommon(hComponent);
  1162. FunctionOut();
  1163. return ret;
  1164. }
  1165. //TODO happer
  1166. SF_OMX_COMPONENT sf_dec_decoder_mjpeg = {
  1167. .componentName = "sf.dec.decoder.mjpeg",
  1168. .libName = "libcodadec.so",
  1169. .pOMXComponent = NULL,
  1170. .SF_OMX_ComponentConstructor = SF_OMX_ComponentConstructor,
  1171. .SF_OMX_ComponentClear = SF_OMX_ComponentClear,
  1172. // .functions = NULL,
  1173. // .bitFormat = STD_HEVC,
  1174. .componentImpl = NULL,
  1175. .fwPath = "/lib/firmware/chagall.bin",
  1176. .componentRule = "video_decoder.mjpeg"};