SF_OMX_mjpeg_common.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  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. }
  166. // strcpy(pSfOMXComponent->portDefinition[1].format.image.cMIMEType, "JPEG");
  167. // pSfOMXComponent->portDefinition[1].format.image.eColorFormat = OMX_COLOR_FormatYUV420SemiPlanar;
  168. strcpy(pSfOMXComponent->portDefinition[1].format.video.cMIMEType, "JPEG");
  169. pSfOMXComponent->portDefinition[1].format.video.eColorFormat = OMX_COLOR_FormatYUV420SemiPlanar;
  170. memset(pSfOMXComponent->pBufferArray, 0, sizeof(pSfOMXComponent->pBufferArray));
  171. pSfOMXComponent->memory_optimization = OMX_TRUE;
  172. FunctionOut();
  173. EXIT:
  174. return ret;
  175. ERROR:
  176. if (pSfOMXComponent->pOMXComponent)
  177. {
  178. free(pSfOMXComponent->pOMXComponent);
  179. pSfOMXComponent->pOMXComponent = NULL;
  180. }
  181. return ret;
  182. }
  183. static void sf_get_component_functions(SF_CODAJ12_FUNCTIONS *funcs, OMX_PTR *sohandle)
  184. {
  185. FunctionIn();
  186. funcs->JPU_Init = dlsym(sohandle, "JPU_Init");
  187. funcs->JPU_DecOpen = dlsym(sohandle, "JPU_DecOpen");
  188. funcs->JPU_DecGetInitialInfo = dlsym(sohandle, "JPU_DecGetInitialInfo");
  189. funcs->JPU_DecRegisterFrameBuffer2 = dlsym(sohandle, "JPU_DecRegisterFrameBuffer2");
  190. funcs->JPU_DecRegisterFrameBuffer = dlsym(sohandle, "JPU_DecRegisterFrameBuffer");
  191. funcs->JPU_DecGiveCommand = dlsym(sohandle, "JPU_DecGiveCommand");
  192. funcs->JPU_DecStartOneFrameBySerialNum = dlsym(sohandle, "JPU_DecStartOneFrameBySerialNum");
  193. funcs->JPU_DecGetOutputInfo = dlsym(sohandle, "JPU_DecGetOutputInfo");
  194. funcs->JPU_SWReset = dlsym(sohandle, "JPU_SWReset");
  195. funcs->JPU_DecSetRdPtrEx = dlsym(sohandle, "JPU_DecSetRdPtrEx");
  196. funcs->JPU_DecClose = dlsym(sohandle, "JPU_DecClose");
  197. funcs->JPU_WaitInterrupt = dlsym(sohandle, "JPU_WaitInterrupt");
  198. funcs->JPU_GetFrameBufPool = dlsym(sohandle, "JPU_GetFrameBufPool");
  199. funcs->JPU_ClrStatus = dlsym(sohandle, "JPU_ClrStatus");
  200. funcs->JPU_DeInit = dlsym(sohandle, "JPU_DeInit");
  201. funcs->BSFeederBuffer_SetData = dlsym(sohandle, "BSFeederBuffer_SetData");
  202. funcs->BitstreamFeeder_SetData = dlsym(sohandle, "BitstreamFeeder_SetData");
  203. funcs->BitstreamFeeder_Create = dlsym(sohandle, "BitstreamFeeder_Create");
  204. funcs->BitstreamFeeder_Act = dlsym(sohandle, "BitstreamFeeder_Act");
  205. funcs->BitstreamFeeder_Destroy = dlsym(sohandle, "BitstreamFeeder_Destroy");
  206. funcs->jdi_allocate_dma_memory = dlsym(sohandle, "jdi_allocate_dma_memory");
  207. funcs->jdi_free_dma_memory = dlsym(sohandle, "jdi_free_dma_memory");
  208. funcs->AllocateOneFrameBuffer = dlsym(sohandle, "AllocateOneFrameBuffer");
  209. funcs->FreeFrameBuffer = dlsym(sohandle, "FreeFrameBuffer");
  210. funcs->GetFrameBuffer = dlsym(sohandle, "GetFrameBuffer");
  211. funcs->GetFrameBufferCount = dlsym(sohandle, "GetFrameBufferCount");
  212. funcs->UpdateFrameBuffers = dlsym(sohandle, "UpdateFrameBuffers");
  213. funcs->SetMaxLogLevel = dlsym(sohandle, "SetMaxLogLevel");
  214. funcs->AttachOneFrameBuffer = dlsym(sohandle, "AttachOneFrameBuffer");
  215. FunctionOut();
  216. }
  217. OMX_BOOL AttachOutputBuffer(SF_OMX_COMPONENT *pSfOMXComponent, OMX_U8* pBuffer, OMX_U32 nSizeBytes)
  218. {
  219. SF_CODAJ12_IMPLEMEMT *pSfCodaj12Implement = pSfOMXComponent->componentImpl;
  220. Int32 instIdx = pSfCodaj12Implement->instIdx;
  221. JpgDecOpenParam *decOP = &pSfCodaj12Implement->decOP;
  222. DecConfigParam *decConfig = pSfCodaj12Implement->config;
  223. JpgDecInitialInfo *initialInfo = &pSfCodaj12Implement->initialInfo;
  224. FrameBuffer *pFrameBuf = pSfCodaj12Implement->frameBuf;
  225. JpgDecHandle handle = pSfCodaj12Implement->handle;
  226. Uint32 framebufWidth = 0, framebufHeight = 0, framebufStride = 0;
  227. Uint32 decodingWidth, decodingHeight;
  228. BOOL scalerOn = FALSE;
  229. Uint32 bitDepth = 0;
  230. FrameFormat subsample;
  231. Uint32 temp;
  232. OMX_U8 *virtAddr = NULL;
  233. Uint32 bufferIndex;
  234. JpgRet jpgret;
  235. FunctionIn();
  236. if (initialInfo->sourceFormat == FORMAT_420 || initialInfo->sourceFormat == FORMAT_422)
  237. framebufWidth = JPU_CEIL(16, initialInfo->picWidth);
  238. else
  239. framebufWidth = JPU_CEIL(8, initialInfo->picWidth);
  240. if (initialInfo->sourceFormat == FORMAT_420 || initialInfo->sourceFormat == FORMAT_440)
  241. framebufHeight = JPU_CEIL(16, initialInfo->picHeight);
  242. else
  243. framebufHeight = JPU_CEIL(8, initialInfo->picHeight);
  244. if (framebufWidth == 0 || framebufHeight == 0)
  245. {
  246. LOG(SF_LOG_WARN, "width or height == 0, use port parameters\r\n");
  247. for (int i = 0; i < OMX_PORT_MAX; i ++)
  248. {
  249. OMX_PARAM_PORTDEFINITIONTYPE *pPort = &pSfOMXComponent->portDefinition[i];
  250. framebufWidth = pPort->format.video.nFrameWidth;
  251. framebufHeight = pPort->format.video.nFrameHeight;
  252. if (framebufWidth > 0 && framebufHeight > 0)
  253. {
  254. LOG(SF_LOG_INFO, "Use port: %d\r\n", i);
  255. break;
  256. }
  257. }
  258. if (framebufWidth == 0 || framebufHeight == 0)
  259. {
  260. LOG(SF_LOG_ERR, "Can not get frame size\r\n");
  261. return OMX_FALSE;
  262. }
  263. }
  264. LOG(SF_LOG_DEBUG, "framebufWidth: %d, framebufHeight: %d\r\n", framebufWidth, framebufHeight);
  265. decodingWidth = framebufWidth >> decConfig->iHorScaleMode;
  266. decodingHeight = framebufHeight >> decConfig->iVerScaleMode;
  267. if (decOP->packedFormat != PACKED_FORMAT_NONE && decOP->packedFormat != PACKED_FORMAT_444)
  268. {
  269. // When packed format, scale-down resolution should be multiple of 2.
  270. decodingWidth = JPU_CEIL(2, decodingWidth);
  271. }
  272. subsample = pSfCodaj12Implement->frameFormat;
  273. temp = decodingWidth;
  274. decodingWidth = (decConfig->rotation == 90 || decConfig->rotation == 270) ? decodingHeight : decodingWidth;
  275. decodingHeight = (decConfig->rotation == 90 || decConfig->rotation == 270) ? temp : decodingHeight;
  276. if (decConfig->roiEnable == TRUE)
  277. {
  278. decodingWidth = framebufWidth = initialInfo->roiFrameWidth;
  279. decodingHeight = framebufHeight = initialInfo->roiFrameHeight;
  280. }
  281. LOG(SF_LOG_DEBUG, "decodingWidth: %d, decodingHeight: %d\n", decodingWidth, decodingHeight);
  282. if (decOP->rotation != 0 || decOP->mirror != MIRDIR_NONE)
  283. {
  284. if (decOP->outputFormat != FORMAT_MAX && decOP->outputFormat != initialInfo->sourceFormat)
  285. {
  286. LOG(SF_LOG_ERR, "The rotator cannot work with the format converter together.\n");
  287. return OMX_FALSE;
  288. }
  289. }
  290. LOG(SF_LOG_INFO, "<INSTANCE %d>\n", instIdx);
  291. LOG(SF_LOG_INFO, "SOURCE PICTURE SIZE : W(%d) H(%d)\n", initialInfo->picWidth, initialInfo->picHeight);
  292. LOG(SF_LOG_INFO, "SUBSAMPLE : %d\n", subsample);
  293. bitDepth = initialInfo->bitDepth;
  294. scalerOn = (BOOL)(decConfig->iHorScaleMode || decConfig->iVerScaleMode);
  295. // may be handle == NULL on ffmpeg case
  296. if (bitDepth == 0)
  297. {
  298. bitDepth = 8;
  299. }
  300. LOG(SF_LOG_DEBUG, "AllocateOneFrameBuffer\r\n");
  301. LOG_APPEND(SF_LOG_DEBUG, "instIdx = %d subsample = %d chromaInterleave = %d packedFormat = %d rotation = %d\r\n",
  302. instIdx, subsample, decOP->chromaInterleave, decOP->packedFormat, decConfig->rotation);
  303. LOG_APPEND(SF_LOG_DEBUG, "scalerOn = %d decodingWidth = %d decodingHeight = %d bitDepth = %d\r\n",
  304. scalerOn, decodingWidth, decodingHeight, bitDepth);
  305. if (pSfCodaj12Implement->functions->AttachOneFrameBuffer(instIdx, subsample, decOP->chromaInterleave,
  306. decOP->packedFormat, decConfig->rotation,
  307. scalerOn, decodingWidth, decodingHeight, bitDepth, pBuffer, nSizeBytes, &bufferIndex) == OMX_FALSE)
  308. {
  309. LOG(SF_LOG_ERR, "Fail to attach FrameBuffer\r\n");
  310. return OMX_FALSE;
  311. }
  312. LOG(SF_LOG_INFO, "Allocate frame buffer %p, index = %d\r\n", virtAddr, bufferIndex);
  313. //Register frame buffer
  314. FRAME_BUF *pFrame = pSfCodaj12Implement->functions->GetFrameBuffer(instIdx, bufferIndex);
  315. memcpy(&pSfCodaj12Implement->frame[bufferIndex], pFrame, sizeof(FRAME_BUF));
  316. pFrameBuf[bufferIndex].bufY = pFrame->vbY.phys_addr;
  317. pFrameBuf[bufferIndex].bufCb = pFrame->vbCb.phys_addr;
  318. if (decOP->chromaInterleave == CBCR_SEPARATED)
  319. pFrameBuf[bufferIndex].bufCr = pFrame->vbCr.phys_addr;
  320. pFrameBuf[bufferIndex].stride = pFrame->strideY;
  321. pFrameBuf[bufferIndex].strideC = pFrame->strideC;
  322. pFrameBuf[bufferIndex].endian = decOP->frameEndian;
  323. pFrameBuf[bufferIndex].format = (FrameFormat)pFrame->Format;
  324. framebufStride = pFrameBuf[bufferIndex].stride;
  325. // may be handle == NULL on ffmpeg case
  326. jpgret = pSfCodaj12Implement->functions->JPU_DecRegisterFrameBuffer2(handle, &pFrameBuf[bufferIndex], framebufStride);
  327. LOG(SF_LOG_DEBUG, "JPU_DecRegisterFrameBuffer2 ret = %d\r\n", jpgret);
  328. FunctionOut();
  329. return OMX_TRUE;
  330. }
  331. OMX_U8 *AllocateOutputBuffer(SF_OMX_COMPONENT *pSfOMXComponent, OMX_U32 nSizeBytes)
  332. {
  333. SF_CODAJ12_IMPLEMEMT *pSfCodaj12Implement = pSfOMXComponent->componentImpl;
  334. Int32 instIdx = pSfCodaj12Implement->instIdx;
  335. JpgDecOpenParam *decOP = &pSfCodaj12Implement->decOP;
  336. DecConfigParam *decConfig = pSfCodaj12Implement->config;
  337. JpgDecInitialInfo *initialInfo = &pSfCodaj12Implement->initialInfo;
  338. FrameBuffer *pFrameBuf = pSfCodaj12Implement->frameBuf;
  339. JpgDecHandle handle = pSfCodaj12Implement->handle;
  340. Uint32 framebufWidth = 0, framebufHeight = 0, framebufStride = 0;
  341. Uint32 decodingWidth, decodingHeight;
  342. BOOL scalerOn = FALSE;
  343. Uint32 bitDepth = 0;
  344. FrameFormat subsample;
  345. Uint32 temp;
  346. OMX_U8 *virtAddr;
  347. Uint32 bufferIndex;
  348. JpgRet jpgret;
  349. CbCrInterLeave chromaInterleave;
  350. PackedFormat packedFormat;
  351. FunctionIn();
  352. if (initialInfo->sourceFormat == FORMAT_420 || initialInfo->sourceFormat == FORMAT_422)
  353. framebufWidth = JPU_CEIL(16, initialInfo->picWidth);
  354. else
  355. framebufWidth = JPU_CEIL(8, initialInfo->picWidth);
  356. if (initialInfo->sourceFormat == FORMAT_420 || initialInfo->sourceFormat == FORMAT_440)
  357. framebufHeight = JPU_CEIL(16, initialInfo->picHeight);
  358. else
  359. framebufHeight = JPU_CEIL(8, initialInfo->picHeight);
  360. if (framebufWidth == 0 || framebufHeight == 0)
  361. {
  362. LOG(SF_LOG_WARN, "width or height == 0, use port parameters\r\n");
  363. for (int i = 0; i < OMX_PORT_MAX; i ++)
  364. {
  365. OMX_PARAM_PORTDEFINITIONTYPE *pPort = &pSfOMXComponent->portDefinition[i];
  366. framebufWidth = pPort->format.video.nFrameWidth;
  367. framebufHeight = pPort->format.video.nFrameHeight;
  368. if (framebufWidth > 0 && framebufHeight > 0)
  369. {
  370. LOG(SF_LOG_INFO, "Use port: %d\r\n", i);
  371. break;
  372. }
  373. }
  374. if (initialInfo->sourceFormat == FORMAT_420 || initialInfo->sourceFormat == FORMAT_422)
  375. framebufWidth = JPU_CEIL(16, framebufWidth);
  376. else
  377. framebufWidth = JPU_CEIL(8, framebufWidth);
  378. if (initialInfo->sourceFormat == FORMAT_420 || initialInfo->sourceFormat == FORMAT_440)
  379. framebufHeight = JPU_CEIL(16, framebufHeight);
  380. else
  381. framebufHeight = JPU_CEIL(8, framebufHeight);
  382. if (framebufWidth == 0 || framebufHeight == 0)
  383. {
  384. LOG(SF_LOG_ERR, "Can not get frame size\r\n");
  385. return NULL;
  386. }
  387. }
  388. LOG(SF_LOG_DEBUG, "framebufWidth: %d, framebufHeight: %d\r\n", framebufWidth, framebufHeight);
  389. decodingWidth = framebufWidth >> decConfig->iHorScaleMode;
  390. decodingHeight = framebufHeight >> decConfig->iVerScaleMode;
  391. if (decOP->packedFormat != PACKED_FORMAT_NONE && decOP->packedFormat != PACKED_FORMAT_444)
  392. {
  393. // When packed format, scale-down resolution should be multiple of 2.
  394. decodingWidth = JPU_CEIL(2, decodingWidth);
  395. }
  396. subsample = pSfCodaj12Implement->frameFormat;
  397. temp = decodingWidth;
  398. decodingWidth = (decConfig->rotation == 90 || decConfig->rotation == 270) ? decodingHeight : decodingWidth;
  399. decodingHeight = (decConfig->rotation == 90 || decConfig->rotation == 270) ? temp : decodingHeight;
  400. if (decConfig->roiEnable == TRUE)
  401. {
  402. decodingWidth = framebufWidth = initialInfo->roiFrameWidth;
  403. decodingHeight = framebufHeight = initialInfo->roiFrameHeight;
  404. }
  405. LOG(SF_LOG_DEBUG, "decodingWidth: %d, decodingHeight: %d\n", decodingWidth, decodingHeight);
  406. if (decOP->rotation != 0 || decOP->mirror != MIRDIR_NONE)
  407. {
  408. if (decOP->outputFormat != FORMAT_MAX && decOP->outputFormat != initialInfo->sourceFormat)
  409. {
  410. LOG(SF_LOG_ERR, "The rotator cannot work with the format converter together.\n");
  411. return NULL;
  412. }
  413. }
  414. LOG(SF_LOG_INFO, "<INSTANCE %d>\n", instIdx);
  415. LOG(SF_LOG_INFO, "SOURCE PICTURE SIZE : W(%d) H(%d)\n", initialInfo->picWidth, initialInfo->picHeight);
  416. LOG(SF_LOG_INFO, "SUBSAMPLE : %d\n", subsample);
  417. bitDepth = initialInfo->bitDepth;
  418. scalerOn = (BOOL)(decConfig->iHorScaleMode || decConfig->iVerScaleMode);
  419. // may be handle == NULL on ffmpeg case
  420. if (bitDepth == 0)
  421. {
  422. bitDepth = 8;
  423. }
  424. if (handle == NULL)
  425. {
  426. LOG(SF_LOG_INFO, "JPU not open yet, use default\r\n");
  427. chromaInterleave = decConfig->cbcrInterleave;
  428. packedFormat = decConfig->packedFormat;
  429. }
  430. else
  431. {
  432. chromaInterleave = decOP->chromaInterleave;
  433. packedFormat = decOP->packedFormat;
  434. }
  435. LOG(SF_LOG_DEBUG, "AllocateOneFrameBuffer\r\n");
  436. LOG_APPEND(SF_LOG_DEBUG, "instIdx = %d subsample = %d chromaInterleave = %d packedFormat = %d rotation = %d\r\n",
  437. instIdx, subsample, chromaInterleave, packedFormat, decConfig->rotation);
  438. LOG_APPEND(SF_LOG_DEBUG, "scalerOn = %d decodingWidth = %d decodingHeight = %d bitDepth = %d\r\n",
  439. scalerOn, decodingWidth, decodingHeight, bitDepth);
  440. virtAddr = (OMX_U8 *)pSfCodaj12Implement->functions->AllocateOneFrameBuffer
  441. (instIdx, subsample, chromaInterleave, packedFormat, decConfig->rotation,
  442. scalerOn, decodingWidth, decodingHeight, bitDepth, &bufferIndex);
  443. if (virtAddr == NULL)
  444. {
  445. LOG(SF_LOG_ERR, "Failed to AllocateOneFrameBuffer()\n");
  446. return NULL;
  447. }
  448. LOG(SF_LOG_INFO, "Allocate frame buffer %p, index = %d\r\n", virtAddr, bufferIndex);
  449. //Register frame buffer
  450. FRAME_BUF *pFrame = pSfCodaj12Implement->functions->GetFrameBuffer(instIdx, bufferIndex);
  451. memcpy(&pSfCodaj12Implement->frame[bufferIndex], pFrame, sizeof(FRAME_BUF));
  452. pFrameBuf[bufferIndex].bufY = pFrame->vbY.phys_addr;
  453. pFrameBuf[bufferIndex].bufCb = pFrame->vbCb.phys_addr;
  454. if (decOP->chromaInterleave == CBCR_SEPARATED)
  455. pFrameBuf[bufferIndex].bufCr = pFrame->vbCr.phys_addr;
  456. pFrameBuf[bufferIndex].stride = pFrame->strideY;
  457. pFrameBuf[bufferIndex].strideC = pFrame->strideC;
  458. pFrameBuf[bufferIndex].endian = decOP->frameEndian;
  459. pFrameBuf[bufferIndex].format = (FrameFormat)pFrame->Format;
  460. framebufStride = pFrameBuf[bufferIndex].stride;
  461. // may be handle == NULL on ffmpeg case
  462. jpgret = pSfCodaj12Implement->functions->JPU_DecRegisterFrameBuffer2(handle, &pFrameBuf[bufferIndex], framebufStride);
  463. LOG(SF_LOG_DEBUG, "JPU_DecRegisterFrameBuffer2 ret = %d\r\n", jpgret);
  464. FunctionOut();
  465. return virtAddr;
  466. }
  467. void CodaJ12FlushBuffer(SF_OMX_COMPONENT *pSfOMXComponent, OMX_U32 nPortNumber)
  468. {
  469. SF_CODAJ12_IMPLEMEMT *pSfCodaj12Implement = pSfOMXComponent->componentImpl;
  470. Message data;
  471. OMX_BUFFERHEADERTYPE *pOMXBuffer = NULL;
  472. FunctionIn();
  473. switch (nPortNumber)
  474. {
  475. case OMX_INPUT_PORT_INDEX:
  476. while (OMX_TRUE)
  477. {
  478. if (msgrcv(pSfCodaj12Implement->sInputMessageQueue, (void *)&data, BUFSIZ, 0, IPC_NOWAIT) < 0)
  479. {
  480. if (msgrcv(pSfCodaj12Implement->sBufferDoneQueue, (void *)&data, BUFSIZ, 0, IPC_NOWAIT) < 0)
  481. {
  482. LOG(SF_LOG_INFO, "No more buffer in input port\r\n");
  483. break;
  484. }
  485. }
  486. pOMXBuffer = data.pBuffer;
  487. LOG(SF_LOG_INFO, "Flush Buffer %p\r\n", pOMXBuffer);
  488. if (pOMXBuffer != NULL)
  489. {
  490. pOMXBuffer->nFilledLen = 0;
  491. pOMXBuffer->nFlags = OMX_BUFFERFLAG_EOS;
  492. pSfOMXComponent->callbacks->EmptyBufferDone(pSfOMXComponent->pOMXComponent, pSfOMXComponent->pAppData, pOMXBuffer);
  493. }
  494. }
  495. break;
  496. case OMX_OUTPUT_PORT_INDEX:
  497. for(int i = 0; i < MCA_MAX_INDEX; i++){
  498. pOMXBuffer = pSfCodaj12Implement->mesCacheArr[i].pBuffer;
  499. LOG(SF_LOG_INFO, "Flush Buffer %p\r\n", pOMXBuffer);
  500. if (pOMXBuffer != NULL)
  501. {
  502. pOMXBuffer->nFilledLen = 0;
  503. pOMXBuffer->nFlags = OMX_BUFFERFLAG_EOS;
  504. pSfOMXComponent->callbacks->FillBufferDone(pSfOMXComponent->pOMXComponent, pSfOMXComponent->pAppData, pOMXBuffer);
  505. }
  506. }
  507. while (OMX_TRUE)
  508. {
  509. if (msgrcv(pSfCodaj12Implement->sOutputMessageQueue, (void *)&data, BUFSIZ, 0, IPC_NOWAIT) < 0)
  510. {
  511. LOG(SF_LOG_INFO, "No more buffer in output port\r\n");
  512. break;
  513. }
  514. pOMXBuffer = data.pBuffer;
  515. LOG(SF_LOG_INFO, "Flush Buffer %p\r\n", pOMXBuffer);
  516. if (pOMXBuffer != NULL)
  517. {
  518. pOMXBuffer->nFilledLen = 0;
  519. pOMXBuffer->nFlags = OMX_BUFFERFLAG_EOS;
  520. pSfOMXComponent->callbacks->FillBufferDone(pSfOMXComponent->pOMXComponent, pSfOMXComponent->pAppData, pOMXBuffer);
  521. }
  522. }
  523. break;
  524. default:
  525. break;
  526. }
  527. FunctionOut();
  528. }