SF_OMX_mjpeg_common.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2021 StarFive Technology Co., Ltd.
  4. */
  5. #include "SF_OMX_mjpeg_common.h"
  6. static void sf_get_component_functions(SF_CODAJ12_FUNCTIONS *funcs, OMX_PTR *sohandle);
  7. OMX_ERRORTYPE GetStateMjpegCommon(OMX_IN OMX_HANDLETYPE hComponent, OMX_OUT OMX_STATETYPE *pState)
  8. {
  9. OMX_ERRORTYPE ret = OMX_ErrorNone;
  10. FunctionIn();
  11. FunctionOut();
  12. return ret;
  13. }
  14. OMX_ERRORTYPE InitMjpegStructorCommon(SF_OMX_COMPONENT *pSfOMXComponent)
  15. {
  16. OMX_ERRORTYPE ret = OMX_ErrorNone;
  17. char *strDebugLevel = NULL;
  18. int debugLevel = 0;
  19. SF_CODAJ12_IMPLEMEMT *pSfCodaj12Implement = NULL;
  20. FunctionIn();
  21. if (pSfOMXComponent == NULL)
  22. {
  23. ret = OMX_ErrorBadParameter;
  24. goto EXIT;
  25. }
  26. pSfOMXComponent->pOMXComponent = malloc(sizeof(OMX_COMPONENTTYPE));
  27. if (pSfOMXComponent->pOMXComponent == NULL)
  28. {
  29. ret = OMX_ErrorInsufficientResources;
  30. LOG(SF_LOG_ERR, "malloc fail\r\n");
  31. goto ERROR;
  32. }
  33. memset(pSfOMXComponent->pOMXComponent, 0, sizeof(OMX_COMPONENTTYPE));
  34. pSfOMXComponent->soHandle = dlopen(pSfOMXComponent->libName, RTLD_NOW);
  35. if (pSfOMXComponent->soHandle == NULL)
  36. {
  37. ret = OMX_ErrorInsufficientResources;
  38. LOG(SF_LOG_ERR, "could not open %s, error: %s\r\n", pSfOMXComponent->libName, dlerror());
  39. goto ERROR;
  40. }
  41. pSfOMXComponent->componentImpl = malloc(sizeof(SF_CODAJ12_IMPLEMEMT));
  42. if (pSfOMXComponent->componentImpl == NULL)
  43. {
  44. ret = OMX_ErrorInsufficientResources;
  45. LOG(SF_LOG_ERR, "malloc fail\r\n");
  46. goto ERROR;
  47. }
  48. memset(pSfOMXComponent->componentImpl, 0, sizeof(SF_CODAJ12_IMPLEMEMT));
  49. pSfCodaj12Implement = (SF_CODAJ12_IMPLEMEMT *)pSfOMXComponent->componentImpl;
  50. pSfCodaj12Implement->functions = malloc(sizeof(SF_CODAJ12_FUNCTIONS));
  51. pSfCodaj12Implement->currentState = OMX_StateLoaded;
  52. pSfCodaj12Implement->frameFormat = DEFAULT_FRAME_FORMAT;
  53. if (pSfCodaj12Implement->functions == NULL)
  54. {
  55. ret = OMX_ErrorInsufficientResources;
  56. LOG(SF_LOG_ERR, "malloc fail\r\n");
  57. goto ERROR;
  58. }
  59. memset(pSfCodaj12Implement->functions, 0, sizeof(SF_CODAJ12_FUNCTIONS));
  60. sf_get_component_functions(pSfCodaj12Implement->functions, pSfOMXComponent->soHandle);
  61. // Init JPU log
  62. if (pSfCodaj12Implement->functions->SetMaxLogLevel)
  63. {
  64. strDebugLevel = getenv("JPU_DEBUG");
  65. if (strDebugLevel)
  66. {
  67. debugLevel = atoi(strDebugLevel);
  68. if (debugLevel >=0)
  69. {
  70. pSfCodaj12Implement->functions->SetMaxLogLevel(debugLevel);
  71. }
  72. }
  73. }
  74. if (strstr(pSfOMXComponent->componentName, "OMX.sf.video_decoder") != NULL)
  75. {
  76. pSfCodaj12Implement->config = malloc(sizeof(DecConfigParam));
  77. if (pSfCodaj12Implement->config == NULL)
  78. {
  79. ret = OMX_ErrorInsufficientResources;
  80. LOG(SF_LOG_ERR, "malloc fail\r\n");
  81. goto ERROR;
  82. }
  83. memset(pSfCodaj12Implement->config, 0, sizeof(DecConfigParam));
  84. }
  85. else
  86. {
  87. ret = OMX_ErrorBadParameter;
  88. LOG(SF_LOG_ERR, "unknown component!\r\n");
  89. goto ERROR;
  90. }
  91. pSfOMXComponent->pOMXComponent->pComponentPrivate = pSfOMXComponent;
  92. pSfCodaj12Implement->sInputMessageQueue = msgget(IPC_PRIVATE, 0666 | IPC_CREAT);
  93. if (pSfCodaj12Implement->sInputMessageQueue < 0)
  94. {
  95. LOG(SF_LOG_ERR, "get ipc_id error");
  96. return OMX_ErrorInsufficientResources;
  97. }
  98. pSfCodaj12Implement->sOutputMessageQueue = msgget(IPC_PRIVATE, 0666 | IPC_CREAT);
  99. if (pSfCodaj12Implement->sOutputMessageQueue < 0)
  100. {
  101. LOG(SF_LOG_ERR, "get ipc_id error");
  102. return OMX_ErrorInsufficientResources;
  103. }
  104. pSfCodaj12Implement->sBufferDoneQueue = msgget(IPC_PRIVATE, 0666 | IPC_CREAT);
  105. if (pSfCodaj12Implement->sBufferDoneQueue < 0)
  106. {
  107. LOG(SF_LOG_ERR, "get ipc_id error");
  108. return OMX_ErrorInsufficientResources;
  109. }
  110. for (int i = 0; i < 2; i++)
  111. {
  112. OMX_PARAM_PORTDEFINITIONTYPE *pPortDefinition = &pSfOMXComponent->portDefinition[i];
  113. // OMX_IMAGE_PARAM_PORTFORMATTYPE * imagePortFormat = &pSfOMXComponent->MJPEGComponent[i];
  114. // memset(&imagePortFormat, 0, sizeof(OMX_IMAGE_PARAM_PORTFORMATTYPE));
  115. // INIT_SET_SIZE_VERSION(imagePortFormat, OMX_PARAM_PORTDEFINITIONTYPE);
  116. // imagePortFormat->nPortIndex = i;
  117. // imagePortFormat->nIndex = 0; //TODO
  118. // imagePortFormat->eCompressionFormat = OMX_IMAGE_CodingJPEG;
  119. // imagePortFormat->eColorFormat = OMX_COLOR_FormatUnused; //TODO
  120. INIT_SET_SIZE_VERSION(pPortDefinition, OMX_PARAM_PORTDEFINITIONTYPE);
  121. pPortDefinition->nPortIndex = i;
  122. pPortDefinition->nBufferCountActual = (i == 0 ? CODAJ12_INPUT_BUF_NUMBER : CODAJ12_OUTPUT_BUF_NUMBER);
  123. pPortDefinition->nBufferCountMin = (i == 0 ? CODAJ12_INPUT_BUF_NUMBER : CODAJ12_OUTPUT_BUF_NUMBER);
  124. pPortDefinition->nBufferSize = (i == 0 ? DEFAULT_MJPEG_INPUT_BUFFER_SIZE : DEFAULT_MJPEG_OUTPUT_BUFFER_SIZE);
  125. pPortDefinition->eDomain = OMX_PortDomainVideo;
  126. // pPortDefinition->format.image.nFrameWidth = DEFAULT_FRAME_WIDTH;
  127. // pPortDefinition->format.image.nFrameHeight = DEFAULT_FRAME_HEIGHT;
  128. // pPortDefinition->format.image.nStride = DEFAULT_FRAME_WIDTH;
  129. // pPortDefinition->format.image.nSliceHeight = DEFAULT_FRAME_HEIGHT;
  130. // pPortDefinition->format.image.eCompressionFormat = OMX_IMAGE_CodingUnused;
  131. // pPortDefinition->format.image.cMIMEType = malloc(OMX_MAX_STRINGNAME_SIZE);
  132. // if (pPortDefinition->format.image.cMIMEType == NULL)
  133. // {
  134. // ret = OMX_ErrorInsufficientResources;
  135. // free(pPortDefinition->format.image.cMIMEType);
  136. // pPortDefinition->format.image.cMIMEType = NULL;
  137. // LOG(SF_LOG_ERR, "malloc fail\r\n");
  138. // goto ERROR;
  139. // }
  140. // memset(pPortDefinition->format.image.cMIMEType, 0, OMX_MAX_STRINGNAME_SIZE);
  141. // pPortDefinition->format.image.pNativeRender = 0;
  142. // pPortDefinition->format.image.bFlagErrorConcealment = OMX_FALSE;
  143. // pPortDefinition->format.image.eColorFormat = OMX_COLOR_FormatUnused;
  144. pPortDefinition->format.video.nFrameWidth = DEFAULT_FRAME_WIDTH;
  145. pPortDefinition->format.video.nFrameHeight = DEFAULT_FRAME_HEIGHT;
  146. pPortDefinition->format.video.nStride = DEFAULT_FRAME_WIDTH;
  147. pPortDefinition->format.video.nSliceHeight = DEFAULT_FRAME_HEIGHT;
  148. pPortDefinition->format.video.eCompressionFormat = OMX_VIDEO_CodingUnused;
  149. pPortDefinition->format.video.cMIMEType = malloc(OMX_MAX_STRINGNAME_SIZE);
  150. pPortDefinition->format.video.xFramerate = 30;
  151. if (pPortDefinition->format.video.cMIMEType == NULL)
  152. {
  153. ret = OMX_ErrorInsufficientResources;
  154. free(pPortDefinition->format.video.cMIMEType);
  155. pPortDefinition->format.video.cMIMEType = NULL;
  156. LOG(SF_LOG_ERR, "malloc fail\r\n");
  157. goto ERROR;
  158. }
  159. memset(pPortDefinition->format.video.cMIMEType, 0, OMX_MAX_STRINGNAME_SIZE);
  160. pPortDefinition->format.video.pNativeRender = 0;
  161. pPortDefinition->format.video.bFlagErrorConcealment = OMX_FALSE;
  162. pPortDefinition->format.video.eColorFormat = OMX_COLOR_FormatUnused;
  163. pPortDefinition->bEnabled = OMX_TRUE;
  164. pPortDefinition->eDir = (i == 0 ? OMX_DirInput : OMX_DirOutput);
  165. pSfOMXComponent->assignedBufferNum[i] = 0;
  166. }
  167. /* Set componentVersion */
  168. pSfOMXComponent->componentVersion.s.nVersionMajor = VERSIONMAJOR_NUMBER;
  169. pSfOMXComponent->componentVersion.s.nVersionMinor = VERSIONMINOR_NUMBER;
  170. pSfOMXComponent->componentVersion.s.nRevision = REVISION_NUMBER;
  171. pSfOMXComponent->componentVersion.s.nStep = STEP_NUMBER;
  172. /* Set specVersion */
  173. pSfOMXComponent->specVersion.s.nVersionMajor = VERSIONMAJOR_NUMBER;
  174. pSfOMXComponent->specVersion.s.nVersionMinor = VERSIONMINOR_NUMBER;
  175. pSfOMXComponent->specVersion.s.nRevision = REVISION_NUMBER;
  176. pSfOMXComponent->specVersion.s.nStep = STEP_NUMBER;
  177. memset(pSfOMXComponent->markType, 0, sizeof(pSfOMXComponent->markType));
  178. pSfOMXComponent->propagateMarkType.hMarkTargetComponent = NULL;
  179. pSfOMXComponent->propagateMarkType.pMarkData = NULL;
  180. for (int i = 0; i < 2; i++)
  181. {
  182. ret = SF_SemaphoreCreate(&pSfOMXComponent->portSemaphore[i]);
  183. if (ret)
  184. goto ERROR;
  185. ret = SF_SemaphoreCreate(&pSfOMXComponent->portUnloadSemaphore[i]);
  186. if (ret)
  187. goto ERROR;
  188. ret = SF_SemaphoreCreate(&pSfOMXComponent->portFlushSemaphore[i]);
  189. if (ret)
  190. goto ERROR;
  191. }
  192. ret = SF_SemaphoreCreate(&pSfCodaj12Implement->pauseOutSemaphore);
  193. if (ret)
  194. goto ERROR;
  195. // strcpy(pSfOMXComponent->portDefinition[1].format.image.cMIMEType, "JPEG");
  196. // pSfOMXComponent->portDefinition[1].format.image.eColorFormat = OMX_COLOR_FormatYUV420SemiPlanar;
  197. pSfOMXComponent->portDefinition[0].format.video.eColorFormat = OMX_COLOR_FormatUnused;
  198. pSfOMXComponent->portDefinition[0].format.video.eCompressionFormat = OMX_VIDEO_CodingMJPEG;
  199. strcpy(pSfOMXComponent->portDefinition[1].format.video.cMIMEType, "JPEG");
  200. pSfOMXComponent->portDefinition[1].format.video.eColorFormat = OMX_COLOR_FormatYUV420SemiPlanar;
  201. pSfOMXComponent->portDefinition[1].format.video.eCompressionFormat = OMX_VIDEO_CodingUnused;
  202. memset(pSfOMXComponent->pBufferArray, 0, sizeof(pSfOMXComponent->pBufferArray));
  203. FunctionOut();
  204. EXIT:
  205. return ret;
  206. ERROR:
  207. for (int i = 0; i < 2; i++)
  208. {
  209. SF_SemaphoreTerminate(pSfOMXComponent->portSemaphore[i]);
  210. SF_SemaphoreTerminate(pSfOMXComponent->portUnloadSemaphore[i]);
  211. SF_SemaphoreTerminate(pSfOMXComponent->portFlushSemaphore[i]);
  212. }
  213. SF_SemaphoreTerminate(pSfCodaj12Implement->pauseOutSemaphore);
  214. if (pSfOMXComponent->pOMXComponent)
  215. {
  216. free(pSfOMXComponent->pOMXComponent);
  217. pSfOMXComponent->pOMXComponent = NULL;
  218. }
  219. return ret;
  220. }
  221. static void sf_get_component_functions(SF_CODAJ12_FUNCTIONS *funcs, OMX_PTR *sohandle)
  222. {
  223. FunctionIn();
  224. funcs->JPU_Init = dlsym(sohandle, "JPU_Init");
  225. funcs->JPU_DecOpen = dlsym(sohandle, "JPU_DecOpen");
  226. funcs->JPU_DecGetInitialInfo = dlsym(sohandle, "JPU_DecGetInitialInfo");
  227. funcs->JPU_DecRegisterFrameBuffer2 = dlsym(sohandle, "JPU_DecRegisterFrameBuffer2");
  228. funcs->JPU_DecRegisterFrameBuffer = dlsym(sohandle, "JPU_DecRegisterFrameBuffer");
  229. funcs->JPU_DecGiveCommand = dlsym(sohandle, "JPU_DecGiveCommand");
  230. funcs->JPU_DecStartOneFrameBySerialNum = dlsym(sohandle, "JPU_DecStartOneFrameBySerialNum");
  231. funcs->JPU_DecStartOneFrame = dlsym(sohandle, "JPU_DecStartOneFrame");
  232. funcs->JPU_DecGetOutputInfo = dlsym(sohandle, "JPU_DecGetOutputInfo");
  233. funcs->JPU_SWReset = dlsym(sohandle, "JPU_SWReset");
  234. funcs->JPU_DecSetRdPtrEx = dlsym(sohandle, "JPU_DecSetRdPtrEx");
  235. funcs->JPU_DecClose = dlsym(sohandle, "JPU_DecClose");
  236. funcs->JPU_WaitInterrupt = dlsym(sohandle, "JPU_WaitInterrupt");
  237. funcs->JPU_GetFrameBufPool = dlsym(sohandle, "JPU_GetFrameBufPool");
  238. funcs->JPU_ClrStatus = dlsym(sohandle, "JPU_ClrStatus");
  239. funcs->JPU_DeInit = dlsym(sohandle, "JPU_DeInit");
  240. funcs->BSFeederBuffer_SetData = dlsym(sohandle, "BSFeederBuffer_SetData");
  241. funcs->BitstreamFeeder_SetData = dlsym(sohandle, "BitstreamFeeder_SetData");
  242. funcs->BitstreamFeeder_Create = dlsym(sohandle, "BitstreamFeeder_Create");
  243. funcs->BitstreamFeeder_Act = dlsym(sohandle, "BitstreamFeeder_Act");
  244. funcs->BitstreamFeeder_Destroy = dlsym(sohandle, "BitstreamFeeder_Destroy");
  245. funcs->jdi_allocate_dma_memory = dlsym(sohandle, "jdi_allocate_dma_memory");
  246. funcs->jdi_free_dma_memory = dlsym(sohandle, "jdi_free_dma_memory");
  247. funcs->AllocateOneFrameBuffer = dlsym(sohandle, "AllocateOneFrameBuffer");
  248. funcs->AllocateFrameBuffer = dlsym(sohandle, "AllocateFrameBuffer");
  249. funcs->FreeFrameBuffer = dlsym(sohandle, "FreeFrameBuffer");
  250. funcs->GetFrameBuffer = dlsym(sohandle, "GetFrameBuffer");
  251. funcs->GetFrameBufferCount = dlsym(sohandle, "GetFrameBufferCount");
  252. funcs->UpdateFrameBuffers = dlsym(sohandle, "UpdateFrameBuffers");
  253. funcs->SetMaxLogLevel = dlsym(sohandle, "SetMaxLogLevel");
  254. funcs->AttachOneFrameBuffer = dlsym(sohandle, "AttachOneFrameBuffer");
  255. funcs->SaveYuvImageHelper = dlsym(sohandle, "SaveYuvImageHelper");
  256. funcs->SaveYuvImageHelperDma = dlsym(sohandle, "SaveYuvImageHelperDma");
  257. FunctionOut();
  258. }
  259. OMX_BOOL AttachOutputBuffer(SF_OMX_COMPONENT *pSfOMXComponent, OMX_U8* pBuffer, OMX_U32 nSizeBytes)
  260. {
  261. SF_CODAJ12_IMPLEMEMT *pSfCodaj12Implement = pSfOMXComponent->componentImpl;
  262. Int32 instIdx = pSfCodaj12Implement->instIdx;
  263. JpgDecOpenParam *decOP = &pSfCodaj12Implement->decOP;
  264. DecConfigParam *decConfig = pSfCodaj12Implement->config;
  265. JpgDecInitialInfo *initialInfo = &pSfCodaj12Implement->initialInfo;
  266. FrameBuffer *pFrameBuf = pSfCodaj12Implement->frameBuf;
  267. JpgDecHandle handle = pSfCodaj12Implement->handle;
  268. Uint32 framebufWidth = 0, framebufHeight = 0, framebufStride = 0;
  269. Uint32 decodingWidth, decodingHeight;
  270. BOOL scalerOn = FALSE;
  271. Uint32 bitDepth = 0;
  272. FrameFormat subsample;
  273. Uint32 temp;
  274. OMX_U8 *virtAddr = NULL;
  275. Uint32 bufferIndex;
  276. JpgRet jpgret;
  277. FunctionIn();
  278. if (initialInfo->sourceFormat == FORMAT_420 || initialInfo->sourceFormat == FORMAT_422)
  279. framebufWidth = JPU_CEIL(16, initialInfo->picWidth);
  280. else
  281. framebufWidth = JPU_CEIL(8, initialInfo->picWidth);
  282. if (initialInfo->sourceFormat == FORMAT_420 || initialInfo->sourceFormat == FORMAT_440)
  283. framebufHeight = JPU_CEIL(16, initialInfo->picHeight);
  284. else
  285. framebufHeight = JPU_CEIL(8, initialInfo->picHeight);
  286. if (framebufWidth == 0 || framebufHeight == 0)
  287. {
  288. LOG(SF_LOG_WARN, "width or height == 0, use port parameters\r\n");
  289. for (int i = 0; i < OMX_PORT_MAX; i ++)
  290. {
  291. OMX_PARAM_PORTDEFINITIONTYPE *pPort = &pSfOMXComponent->portDefinition[i];
  292. framebufWidth = pPort->format.video.nFrameWidth;
  293. framebufHeight = pPort->format.video.nFrameHeight;
  294. if (framebufWidth > 0 && framebufHeight > 0)
  295. {
  296. LOG(SF_LOG_INFO, "Use port: %d\r\n", i);
  297. break;
  298. }
  299. }
  300. if (framebufWidth == 0 || framebufHeight == 0)
  301. {
  302. LOG(SF_LOG_ERR, "Can not get frame size\r\n");
  303. return OMX_FALSE;
  304. }
  305. }
  306. LOG(SF_LOG_DEBUG, "framebufWidth: %d, framebufHeight: %d\r\n", framebufWidth, framebufHeight);
  307. decodingWidth = framebufWidth >> decConfig->iHorScaleMode;
  308. decodingHeight = framebufHeight >> decConfig->iVerScaleMode;
  309. if (decOP->packedFormat != PACKED_FORMAT_NONE && decOP->packedFormat != PACKED_FORMAT_444)
  310. {
  311. // When packed format, scale-down resolution should be multiple of 2.
  312. decodingWidth = JPU_CEIL(2, decodingWidth);
  313. }
  314. if (decConfig->packedFormat)
  315. {
  316. subsample = initialInfo->sourceFormat;
  317. }
  318. else
  319. {
  320. subsample = pSfCodaj12Implement->frameFormat;
  321. }
  322. temp = decodingWidth;
  323. decodingWidth = (decConfig->rotation == 90 || decConfig->rotation == 270) ? decodingHeight : decodingWidth;
  324. decodingHeight = (decConfig->rotation == 90 || decConfig->rotation == 270) ? temp : decodingHeight;
  325. if (decConfig->roiEnable == TRUE)
  326. {
  327. decodingWidth = framebufWidth = initialInfo->roiFrameWidth;
  328. decodingHeight = framebufHeight = initialInfo->roiFrameHeight;
  329. }
  330. LOG(SF_LOG_DEBUG, "decodingWidth: %d, decodingHeight: %d\n", decodingWidth, decodingHeight);
  331. if (decOP->rotation != 0 || decOP->mirror != MIRDIR_NONE)
  332. {
  333. if (decOP->outputFormat != FORMAT_MAX && decOP->outputFormat != initialInfo->sourceFormat)
  334. {
  335. LOG(SF_LOG_ERR, "The rotator cannot work with the format converter together.\n");
  336. return OMX_FALSE;
  337. }
  338. }
  339. LOG(SF_LOG_INFO, "<INSTANCE %d>\n", instIdx);
  340. LOG(SF_LOG_INFO, "SOURCE PICTURE SIZE : W(%d) H(%d)\n", initialInfo->picWidth, initialInfo->picHeight);
  341. LOG(SF_LOG_INFO, "SUBSAMPLE : %d\n", subsample);
  342. bitDepth = initialInfo->bitDepth;
  343. scalerOn = (BOOL)(decConfig->iHorScaleMode || decConfig->iVerScaleMode);
  344. // may be handle == NULL on ffmpeg case
  345. if (bitDepth == 0)
  346. {
  347. bitDepth = 8;
  348. }
  349. LOG(SF_LOG_DEBUG, "AllocateOneFrameBuffer\r\n");
  350. LOG_APPEND(SF_LOG_DEBUG, "instIdx = %d subsample = %d chromaInterleave = %d packedFormat = %d rotation = %d\r\n",
  351. instIdx, subsample, decOP->chromaInterleave, decOP->packedFormat, decConfig->rotation);
  352. LOG_APPEND(SF_LOG_DEBUG, "scalerOn = %d decodingWidth = %d decodingHeight = %d bitDepth = %d\r\n",
  353. scalerOn, decodingWidth, decodingHeight, bitDepth);
  354. if (pSfCodaj12Implement->functions->AttachOneFrameBuffer(instIdx, subsample, decOP->chromaInterleave,
  355. decOP->packedFormat, decConfig->rotation,
  356. scalerOn, decodingWidth, decodingHeight, bitDepth, pBuffer, nSizeBytes, &bufferIndex) == OMX_FALSE)
  357. {
  358. LOG(SF_LOG_ERR, "Fail to attach FrameBuffer\r\n");
  359. return OMX_FALSE;
  360. }
  361. LOG(SF_LOG_INFO, "Allocate frame buffer %p, index = %d\r\n", virtAddr, bufferIndex);
  362. //Register frame buffer
  363. FRAME_BUF *pFrame = pSfCodaj12Implement->functions->GetFrameBuffer(instIdx, bufferIndex);
  364. memcpy(&pSfCodaj12Implement->frame[bufferIndex], pFrame, sizeof(FRAME_BUF));
  365. pFrameBuf[bufferIndex].bufY = pFrame->vbY.phys_addr;
  366. pFrameBuf[bufferIndex].bufCb = pFrame->vbCb.phys_addr;
  367. if (decOP->chromaInterleave == CBCR_SEPARATED)
  368. pFrameBuf[bufferIndex].bufCr = pFrame->vbCr.phys_addr;
  369. pFrameBuf[bufferIndex].stride = pFrame->strideY;
  370. pFrameBuf[bufferIndex].strideC = pFrame->strideC;
  371. pFrameBuf[bufferIndex].endian = decOP->frameEndian;
  372. pFrameBuf[bufferIndex].format = (FrameFormat)pFrame->Format;
  373. framebufStride = pFrameBuf[bufferIndex].stride;
  374. // may be handle == NULL on ffmpeg case
  375. jpgret = pSfCodaj12Implement->functions->JPU_DecRegisterFrameBuffer2(handle, &pFrameBuf[bufferIndex], framebufStride);
  376. LOG(SF_LOG_DEBUG, "JPU_DecRegisterFrameBuffer2 ret = %d\r\n", jpgret);
  377. FunctionOut();
  378. return OMX_TRUE;
  379. }
  380. OMX_U8 *AllocateOutputBuffer(SF_OMX_COMPONENT *pSfOMXComponent, OMX_U32 nSizeBytes)
  381. {
  382. SF_CODAJ12_IMPLEMEMT *pSfCodaj12Implement = pSfOMXComponent->componentImpl;
  383. Int32 instIdx = pSfCodaj12Implement->instIdx;
  384. JpgDecOpenParam *decOP = &pSfCodaj12Implement->decOP;
  385. DecConfigParam *decConfig = pSfCodaj12Implement->config;
  386. JpgDecInitialInfo *initialInfo = &pSfCodaj12Implement->initialInfo;
  387. FrameBuffer *pFrameBuf = pSfCodaj12Implement->frameBuf;
  388. JpgDecHandle handle = pSfCodaj12Implement->handle;
  389. Uint32 framebufWidth = 0, framebufHeight = 0, framebufStride = 0;
  390. Uint32 decodingWidth, decodingHeight;
  391. BOOL scalerOn = FALSE;
  392. Uint32 bitDepth = 0;
  393. FrameFormat subsample;
  394. Uint32 temp;
  395. OMX_U8 *virtAddr;
  396. Uint32 bufferIndex;
  397. JpgRet jpgret;
  398. CbCrInterLeave chromaInterleave;
  399. PackedFormat packedFormat;
  400. FunctionIn();
  401. if (initialInfo->sourceFormat == FORMAT_420 || initialInfo->sourceFormat == FORMAT_422)
  402. framebufWidth = JPU_CEIL(16, initialInfo->picWidth);
  403. else
  404. framebufWidth = JPU_CEIL(8, initialInfo->picWidth);
  405. if (initialInfo->sourceFormat == FORMAT_420 || initialInfo->sourceFormat == FORMAT_440)
  406. framebufHeight = JPU_CEIL(16, initialInfo->picHeight);
  407. else
  408. framebufHeight = JPU_CEIL(8, initialInfo->picHeight);
  409. if (framebufWidth == 0 || framebufHeight == 0)
  410. {
  411. LOG(SF_LOG_WARN, "width or height == 0, use port parameters\r\n");
  412. for (int i = 0; i < OMX_PORT_MAX; i ++)
  413. {
  414. OMX_PARAM_PORTDEFINITIONTYPE *pPort = &pSfOMXComponent->portDefinition[i];
  415. framebufWidth = pPort->format.video.nFrameWidth;
  416. framebufHeight = pPort->format.video.nFrameHeight;
  417. if (framebufWidth > 0 && framebufHeight > 0)
  418. {
  419. LOG(SF_LOG_INFO, "Use port: %d\r\n", i);
  420. break;
  421. }
  422. }
  423. if (initialInfo->sourceFormat == FORMAT_420 || initialInfo->sourceFormat == FORMAT_422)
  424. framebufWidth = JPU_CEIL(16, framebufWidth);
  425. else
  426. framebufWidth = JPU_CEIL(8, framebufWidth);
  427. if (initialInfo->sourceFormat == FORMAT_420 || initialInfo->sourceFormat == FORMAT_440)
  428. framebufHeight = JPU_CEIL(16, framebufHeight);
  429. else
  430. framebufHeight = JPU_CEIL(8, framebufHeight);
  431. if (framebufWidth == 0 || framebufHeight == 0)
  432. {
  433. LOG(SF_LOG_ERR, "Can not get frame size\r\n");
  434. return NULL;
  435. }
  436. }
  437. LOG(SF_LOG_DEBUG, "framebufWidth: %d, framebufHeight: %d\r\n", framebufWidth, framebufHeight);
  438. decodingWidth = framebufWidth >> decConfig->iHorScaleMode;
  439. decodingHeight = framebufHeight >> decConfig->iVerScaleMode;
  440. if (decOP->packedFormat != PACKED_FORMAT_NONE && decOP->packedFormat != PACKED_FORMAT_444)
  441. {
  442. // When packed format, scale-down resolution should be multiple of 2.
  443. decodingWidth = JPU_CEIL(2, decodingWidth);
  444. }
  445. subsample = pSfCodaj12Implement->frameFormat;
  446. temp = decodingWidth;
  447. decodingWidth = (decConfig->rotation == 90 || decConfig->rotation == 270) ? decodingHeight : decodingWidth;
  448. decodingHeight = (decConfig->rotation == 90 || decConfig->rotation == 270) ? temp : decodingHeight;
  449. if (decConfig->roiEnable == TRUE)
  450. {
  451. decodingWidth = framebufWidth = initialInfo->roiFrameWidth;
  452. decodingHeight = framebufHeight = initialInfo->roiFrameHeight;
  453. }
  454. LOG(SF_LOG_DEBUG, "decodingWidth: %d, decodingHeight: %d\n", decodingWidth, decodingHeight);
  455. if (decOP->rotation != 0 || decOP->mirror != MIRDIR_NONE)
  456. {
  457. if (decOP->outputFormat != FORMAT_MAX && decOP->outputFormat != initialInfo->sourceFormat)
  458. {
  459. LOG(SF_LOG_ERR, "The rotator cannot work with the format converter together.\n");
  460. return NULL;
  461. }
  462. }
  463. LOG(SF_LOG_INFO, "<INSTANCE %d>\n", instIdx);
  464. LOG(SF_LOG_INFO, "SOURCE PICTURE SIZE : W(%d) H(%d)\n", initialInfo->picWidth, initialInfo->picHeight);
  465. LOG(SF_LOG_INFO, "SUBSAMPLE : %d\n", subsample);
  466. bitDepth = initialInfo->bitDepth;
  467. scalerOn = (BOOL)(decConfig->iHorScaleMode || decConfig->iVerScaleMode);
  468. // may be handle == NULL on ffmpeg case
  469. if (bitDepth == 0)
  470. {
  471. bitDepth = 8;
  472. }
  473. if (handle == NULL)
  474. {
  475. LOG(SF_LOG_INFO, "JPU not open yet, use default\r\n");
  476. chromaInterleave = decConfig->cbcrInterleave;
  477. packedFormat = decConfig->packedFormat;
  478. }
  479. else
  480. {
  481. chromaInterleave = decOP->chromaInterleave;
  482. packedFormat = decOP->packedFormat;
  483. }
  484. LOG(SF_LOG_DEBUG, "AllocateOneFrameBuffer\r\n");
  485. LOG_APPEND(SF_LOG_DEBUG, "instIdx = %d subsample = %d chromaInterleave = %d packedFormat = %d rotation = %d\r\n",
  486. instIdx, subsample, chromaInterleave, packedFormat, decConfig->rotation);
  487. LOG_APPEND(SF_LOG_DEBUG, "scalerOn = %d decodingWidth = %d decodingHeight = %d bitDepth = %d\r\n",
  488. scalerOn, decodingWidth, decodingHeight, bitDepth);
  489. virtAddr = (OMX_U8 *)pSfCodaj12Implement->functions->AllocateOneFrameBuffer
  490. (instIdx, subsample, chromaInterleave, packedFormat, decConfig->rotation,
  491. scalerOn, decodingWidth, decodingHeight, bitDepth, &bufferIndex);
  492. if (virtAddr == NULL)
  493. {
  494. LOG(SF_LOG_ERR, "Failed to AllocateOneFrameBuffer()\n");
  495. return NULL;
  496. }
  497. LOG(SF_LOG_INFO, "Allocate frame buffer %p, index = %d\r\n", virtAddr, bufferIndex);
  498. //Register frame buffer
  499. FRAME_BUF *pFrame = pSfCodaj12Implement->functions->GetFrameBuffer(instIdx, bufferIndex);
  500. memcpy(&pSfCodaj12Implement->frame[bufferIndex], pFrame, sizeof(FRAME_BUF));
  501. pFrameBuf[bufferIndex].bufY = pFrame->vbY.phys_addr;
  502. pFrameBuf[bufferIndex].bufCb = pFrame->vbCb.phys_addr;
  503. if (decOP->chromaInterleave == CBCR_SEPARATED)
  504. pFrameBuf[bufferIndex].bufCr = pFrame->vbCr.phys_addr;
  505. pFrameBuf[bufferIndex].stride = pFrame->strideY;
  506. pFrameBuf[bufferIndex].strideC = pFrame->strideC;
  507. pFrameBuf[bufferIndex].endian = decOP->frameEndian;
  508. pFrameBuf[bufferIndex].format = (FrameFormat)pFrame->Format;
  509. framebufStride = pFrameBuf[bufferIndex].stride;
  510. // may be handle == NULL on ffmpeg case
  511. jpgret = pSfCodaj12Implement->functions->JPU_DecRegisterFrameBuffer2(handle, &pFrameBuf[bufferIndex], framebufStride);
  512. LOG(SF_LOG_DEBUG, "JPU_DecRegisterFrameBuffer2 ret = %d\r\n", jpgret);
  513. FunctionOut();
  514. return virtAddr;
  515. }
  516. void CodaJ12FlushBuffer(SF_OMX_COMPONENT *pSfOMXComponent, OMX_U32 nPortNumber)
  517. {
  518. SF_CODAJ12_IMPLEMEMT *pSfCodaj12Implement = pSfOMXComponent->componentImpl;
  519. Message data;
  520. OMX_BUFFERHEADERTYPE *pOMXBuffer = NULL;
  521. FunctionIn();
  522. switch (nPortNumber)
  523. {
  524. case OMX_INPUT_PORT_INDEX:
  525. while (OMX_TRUE)
  526. {
  527. if (msgrcv(pSfCodaj12Implement->sInputMessageQueue, (void *)&data, BUFSIZ, 0, IPC_NOWAIT) < 0)
  528. {
  529. if (msgrcv(pSfCodaj12Implement->sBufferDoneQueue, (void *)&data, BUFSIZ, 0, IPC_NOWAIT) < 0)
  530. {
  531. LOG(SF_LOG_INFO, "No more buffer in input port\r\n");
  532. break;
  533. }
  534. }
  535. pOMXBuffer = data.pBuffer;
  536. LOG(SF_LOG_INFO, "get header %p from in q\r\n", pOMXBuffer);
  537. if (pOMXBuffer != NULL)
  538. {
  539. pOMXBuffer->nFilledLen = 0;
  540. pOMXBuffer->nFlags = OMX_BUFFERFLAG_EOS;
  541. LOG(SF_LOG_INFO, "Flush input Buffer header %p pBuffer %p\r\n", pOMXBuffer, pOMXBuffer->pBuffer);
  542. pSfOMXComponent->callbacks->EmptyBufferDone(pSfOMXComponent->pOMXComponent, pSfOMXComponent->pAppData, pOMXBuffer);
  543. pSfOMXComponent->handlingBufferNum[OMX_INPUT_PORT_INDEX]--;
  544. if((pSfOMXComponent->handlingBufferNum[OMX_INPUT_PORT_INDEX] == 0) && pSfOMXComponent->bPortFlushing[OMX_INPUT_PORT_INDEX])
  545. {
  546. LOG(SF_LOG_INFO, "return all input buff\r\n");
  547. SF_SemaphorePost(pSfOMXComponent->portFlushSemaphore[OMX_INPUT_PORT_INDEX]);
  548. }
  549. }
  550. }
  551. break;
  552. case OMX_OUTPUT_PORT_INDEX:
  553. while (OMX_TRUE)
  554. {
  555. if (msgrcv(pSfCodaj12Implement->sOutputMessageQueue, (void *)&data, BUFSIZ, 0, IPC_NOWAIT) < 0)
  556. {
  557. LOG(SF_LOG_INFO, "No more buffer in output port\r\n");
  558. break;
  559. }
  560. pOMXBuffer = data.pBuffer;
  561. LOG(SF_LOG_INFO, "get header %p from out q\r\n", pOMXBuffer);
  562. if (pOMXBuffer != NULL)
  563. {
  564. pOMXBuffer->nFilledLen = 0;
  565. pOMXBuffer->nFlags = OMX_BUFFERFLAG_EOS;
  566. LOG(SF_LOG_INFO, "Flush output Buffer header %p pBuffer %p\r\n", pOMXBuffer, pOMXBuffer->pBuffer);
  567. pSfOMXComponent->callbacks->FillBufferDone(pSfOMXComponent->pOMXComponent, pSfOMXComponent->pAppData, pOMXBuffer);
  568. pSfOMXComponent->handlingBufferNum[OMX_OUTPUT_PORT_INDEX]--;
  569. if((pSfOMXComponent->handlingBufferNum[OMX_OUTPUT_PORT_INDEX] == 0) && pSfOMXComponent->bPortFlushing[OMX_OUTPUT_PORT_INDEX])
  570. {
  571. LOG(SF_LOG_INFO, "return all output buff\r\n");
  572. SF_SemaphorePost(pSfOMXComponent->portFlushSemaphore[OMX_OUTPUT_PORT_INDEX]);
  573. }
  574. }
  575. }
  576. break;
  577. default:
  578. break;
  579. }
  580. FunctionOut();
  581. }