SF_OMX_Mjpeg_decoder.c 48 KB

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