SF_OMX_Mjpeg_decoder.c 61 KB

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