SF_OMX_video_common.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2021 StarFive Technology Co., Ltd.
  4. */
  5. #include "SF_OMX_video_common.h"
  6. OMX_ERRORTYPE GetStateCommon(OMX_IN OMX_HANDLETYPE hComponent, OMX_OUT OMX_STATETYPE *pState)
  7. {
  8. OMX_ERRORTYPE ret = OMX_ErrorNone;
  9. OMX_COMPONENTTYPE *pOMXComponent = NULL;
  10. SF_OMX_COMPONENT *pSfOMXComponent = NULL;
  11. ComponentState state;
  12. OMX_STATETYPE nextState;
  13. FunctionIn();
  14. if (hComponent == NULL)
  15. {
  16. ret = OMX_ErrorBadParameter;
  17. goto EXIT;
  18. }
  19. pOMXComponent = (OMX_COMPONENTTYPE *)hComponent;
  20. pSfOMXComponent = pOMXComponent->pComponentPrivate;
  21. nextState = pSfOMXComponent->nextState;
  22. state = pSfOMXComponent->functions->ComponentGetState(pSfOMXComponent->hSFComponentExecoder);
  23. LOG(SF_LOG_INFO, "state = %d\r\n", state);
  24. switch (state)
  25. {
  26. case COMPONENT_STATE_CREATED:
  27. *pState = OMX_StateIdle;
  28. break;
  29. case COMPONENT_STATE_NONE:
  30. case COMPONENT_STATE_TERMINATED:
  31. *pState = OMX_StateLoaded;
  32. break;
  33. case COMPONENT_STATE_PREPARED:
  34. case COMPONENT_STATE_EXECUTED:
  35. if (nextState == OMX_StateIdle || nextState == OMX_StateExecuting || nextState == OMX_StatePause)
  36. {
  37. *pState = nextState;
  38. }
  39. break;
  40. default:
  41. LOG(SF_LOG_WARN, "unknown state:%d \r\n", state);
  42. ret = OMX_ErrorUndefined;
  43. break;
  44. }
  45. EXIT:
  46. FunctionOut();
  47. return ret;
  48. }
  49. OMX_ERRORTYPE ComponentClearCommon(SF_OMX_COMPONENT *hComponent)
  50. {
  51. hComponent->functions->ComponentRelease(hComponent->hSFComponentExecoder);
  52. hComponent->functions->ComponentDestroy(hComponent->hSFComponentExecoder, NULL);
  53. hComponent->functions->DeInitLog();
  54. dlclose(hComponent->soHandle);
  55. free(hComponent->functions);
  56. free(hComponent->testConfig);
  57. free(hComponent->config);
  58. free(hComponent->lsnCtx);
  59. free(hComponent->pOMXComponent);
  60. for (int i = 0; i < 2; i++)
  61. {
  62. OMX_PARAM_PORTDEFINITIONTYPE *pPortDefinition = &hComponent->portDefinition[i];
  63. free(pPortDefinition->format.video.cMIMEType);
  64. }
  65. return OMX_ErrorNone;
  66. }
  67. BOOL CheckEncTestConfig(TestEncConfig *testConfig)
  68. {
  69. FunctionIn();
  70. if ((testConfig->compareType & (1 << MODE_SAVE_ENCODED)) && testConfig->bitstreamFileName[0] == 0)
  71. {
  72. testConfig->compareType &= ~(1 << MODE_SAVE_ENCODED);
  73. LOG(SF_LOG_ERR, "You want to Save bitstream data. Set the path\r\n");
  74. return FALSE;
  75. }
  76. if ((testConfig->compareType & (1 << MODE_COMP_ENCODED)) && testConfig->ref_stream_path[0] == 0)
  77. {
  78. testConfig->compareType &= ~(1 << MODE_COMP_ENCODED);
  79. LOG(SF_LOG_ERR, "You want to Compare bitstream data. Set the path\r\n");
  80. return FALSE;
  81. }
  82. FunctionOut();
  83. return TRUE;
  84. }
  85. BOOL CheckDecTestConfig(TestDecConfig *testConfig)
  86. {
  87. BOOL isValidParameters = TRUE;
  88. /* Check parameters */
  89. if (testConfig->skipMode < 0 || testConfig->skipMode == 3 || testConfig->skipMode > 4)
  90. {
  91. LOG(SF_LOG_WARN, "Invalid skip mode: %d\n", testConfig->skipMode);
  92. isValidParameters = FALSE;
  93. }
  94. if ((FORMAT_422 < testConfig->wtlFormat && testConfig->wtlFormat <= FORMAT_400) ||
  95. testConfig->wtlFormat < FORMAT_420 || testConfig->wtlFormat >= FORMAT_MAX)
  96. {
  97. LOG(SF_LOG_WARN, "Invalid WTL format(%d)\n", testConfig->wtlFormat);
  98. isValidParameters = FALSE;
  99. }
  100. if (isValidParameters == TRUE && (testConfig->scaleDownWidth > 0 || testConfig->scaleDownHeight > 0))
  101. {
  102. }
  103. if (testConfig->renderType < RENDER_DEVICE_NULL || testConfig->renderType >= RENDER_DEVICE_MAX)
  104. {
  105. LOG(SF_LOG_WARN, "unknown render device type(%d)\n", testConfig->renderType);
  106. isValidParameters = FALSE;
  107. }
  108. if (testConfig->thumbnailMode == TRUE && testConfig->skipMode != 0)
  109. {
  110. LOG(SF_LOG_WARN, "Turn off thumbnail mode or skip mode\n");
  111. isValidParameters = FALSE;
  112. }
  113. return isValidParameters;
  114. }
  115. OMX_ERRORTYPE InitComponentStructorCommon(SF_OMX_COMPONENT *hComponent)
  116. {
  117. OMX_ERRORTYPE ret = OMX_ErrorNone;
  118. char *strDebugLevel = NULL;
  119. int debugLevel = 0;
  120. FunctionIn();
  121. if (hComponent == NULL)
  122. {
  123. ret = OMX_ErrorBadParameter;
  124. goto EXIT;
  125. }
  126. hComponent->pOMXComponent = malloc(sizeof(OMX_COMPONENTTYPE));
  127. if (hComponent->pOMXComponent == NULL)
  128. {
  129. ret = OMX_ErrorInsufficientResources;
  130. LOG(SF_LOG_ERR, "malloc fail\r\n");
  131. goto ERROR;
  132. }
  133. memset(hComponent->pOMXComponent, 0, sizeof(OMX_COMPONENTTYPE));
  134. hComponent->soHandle = dlopen(hComponent->libName, RTLD_NOW);
  135. if (hComponent->soHandle == NULL)
  136. {
  137. ret = OMX_ErrorInsufficientResources;
  138. LOG(SF_LOG_ERR, "could not open %s\r\n", hComponent->libName);
  139. goto ERROR;
  140. }
  141. hComponent->functions = malloc(sizeof(SF_COMPONENT_FUNCTIONS));
  142. if (hComponent->functions == NULL)
  143. {
  144. ret = OMX_ErrorInsufficientResources;
  145. LOG(SF_LOG_ERR, "malloc fail\r\n");
  146. goto ERROR;
  147. }
  148. memset(hComponent->functions, 0, sizeof(SF_COMPONENT_FUNCTIONS));
  149. sf_get_component_functions(hComponent->functions, hComponent->soHandle);
  150. // Init VPU log
  151. if (hComponent->functions->InitLog && hComponent->functions->SetMaxLogLevel)
  152. {
  153. hComponent->functions->InitLog();
  154. strDebugLevel = getenv("VPU_DEBUG");
  155. if (strDebugLevel)
  156. {
  157. debugLevel = atoi(strDebugLevel);
  158. if (debugLevel >=0)
  159. {
  160. hComponent->functions->SetMaxLogLevel(debugLevel);
  161. }
  162. }
  163. }
  164. if (strstr(hComponent->componentName, "sf.enc") != NULL)
  165. {
  166. hComponent->testConfig = malloc(sizeof(TestEncConfig));
  167. if (hComponent->testConfig == NULL)
  168. {
  169. ret = OMX_ErrorInsufficientResources;
  170. LOG(SF_LOG_ERR, "malloc fail\r\n");
  171. goto ERROR;
  172. }
  173. hComponent->lsnCtx = malloc(sizeof(EncListenerContext));
  174. if (hComponent->lsnCtx == NULL)
  175. {
  176. ret = OMX_ErrorInsufficientResources;
  177. LOG(SF_LOG_ERR, "malloc fail\r\n");
  178. goto ERROR;
  179. }
  180. memset(hComponent->testConfig, 0, sizeof(TestEncConfig));
  181. memset(hComponent->lsnCtx, 0, sizeof(EncListenerContext));
  182. hComponent->functions->SetDefaultEncTestConfig(hComponent->testConfig);
  183. }
  184. else if (strstr(hComponent->componentName, "sf.dec") != NULL)
  185. {
  186. hComponent->testConfig = malloc(sizeof(TestDecConfig));
  187. if (hComponent->testConfig == NULL)
  188. {
  189. ret = OMX_ErrorInsufficientResources;
  190. LOG(SF_LOG_ERR, "malloc fail\r\n");
  191. goto ERROR;
  192. }
  193. hComponent->lsnCtx = malloc(sizeof(DecListenerContext));
  194. if (hComponent->lsnCtx == NULL)
  195. {
  196. ret = OMX_ErrorInsufficientResources;
  197. LOG(SF_LOG_ERR, "malloc fail\r\n");
  198. goto ERROR;
  199. }
  200. memset(hComponent->testConfig, 0, sizeof(TestDecConfig));
  201. memset(hComponent->lsnCtx, 0, sizeof(DecListenerContext));
  202. hComponent->functions->SetDefaultDecTestConfig(hComponent->testConfig);
  203. }
  204. else
  205. {
  206. ret = OMX_ErrorBadParameter;
  207. LOG(SF_LOG_ERR, "unknown component!\r\n");
  208. goto ERROR;
  209. }
  210. hComponent->config = malloc(sizeof(CNMComponentConfig));
  211. if (hComponent->config == NULL)
  212. {
  213. ret = OMX_ErrorInsufficientResources;
  214. LOG(SF_LOG_ERR, "malloc fail\r\n");
  215. goto ERROR;
  216. }
  217. memset(hComponent->config, 0, sizeof(CNMComponentConfig));
  218. hComponent->pOMXComponent->pComponentPrivate = hComponent;
  219. for (int i = 0; i < 2; i++)
  220. {
  221. OMX_PARAM_PORTDEFINITIONTYPE *pPortDefinition = &hComponent->portDefinition[i];
  222. OMX_VIDEO_PARAM_AVCTYPE *pAVCComponent = &hComponent->AVCComponent[i];
  223. OMX_VIDEO_PARAM_HEVCTYPE *pHEVCComponent = &hComponent->HEVCComponent[i];
  224. INIT_SET_SIZE_VERSION(pPortDefinition, OMX_PARAM_PORTDEFINITIONTYPE);
  225. INIT_SET_SIZE_VERSION(pAVCComponent, OMX_VIDEO_PARAM_AVCTYPE);
  226. INIT_SET_SIZE_VERSION(pHEVCComponent, OMX_VIDEO_PARAM_HEVCTYPE);
  227. pPortDefinition->nPortIndex = i;
  228. pPortDefinition->nBufferCountActual = VPU_OUTPUT_BUF_NUMBER;
  229. pPortDefinition->nBufferCountMin = VPU_OUTPUT_BUF_NUMBER;
  230. pPortDefinition->nBufferSize = 0;
  231. pPortDefinition->eDomain = OMX_PortDomainVideo;
  232. pPortDefinition->format.video.nFrameWidth = DEFAULT_FRAME_WIDTH;
  233. pPortDefinition->format.video.nFrameHeight = DEFAULT_FRAME_HEIGHT;
  234. pPortDefinition->format.video.nStride = DEFAULT_FRAME_WIDTH;
  235. pPortDefinition->format.video.nSliceHeight = DEFAULT_FRAME_HEIGHT;
  236. pPortDefinition->format.video.eCompressionFormat = OMX_VIDEO_CodingUnused;
  237. pPortDefinition->format.video.cMIMEType = malloc(OMX_MAX_STRINGNAME_SIZE);
  238. pPortDefinition->format.video.xFramerate = 30;
  239. if (pPortDefinition->format.video.cMIMEType == NULL)
  240. {
  241. ret = OMX_ErrorInsufficientResources;
  242. free(pPortDefinition->format.video.cMIMEType);
  243. pPortDefinition->format.video.cMIMEType = NULL;
  244. LOG(SF_LOG_ERR, "malloc fail\r\n");
  245. goto ERROR;
  246. }
  247. memset(pPortDefinition->format.video.cMIMEType, 0, OMX_MAX_STRINGNAME_SIZE);
  248. pPortDefinition->format.video.pNativeRender = 0;
  249. pPortDefinition->format.video.bFlagErrorConcealment = OMX_FALSE;
  250. pPortDefinition->format.video.eColorFormat = OMX_COLOR_FormatUnused;
  251. pPortDefinition->bEnabled = OMX_TRUE;
  252. pAVCComponent->nPortIndex = i;
  253. pAVCComponent->nPFrames = 30;
  254. pAVCComponent->eProfile = OMX_VIDEO_AVCProfileHigh;
  255. pHEVCComponent->nPortIndex = i;
  256. pHEVCComponent->nKeyFrameInterval = 30;
  257. pHEVCComponent->eProfile = OMX_VIDEO_HEVCProfileMain;
  258. }
  259. hComponent->portDefinition[0].eDir = OMX_DirInput;
  260. hComponent->portDefinition[0].nBufferSize = DEFAULT_VIDEO_INPUT_BUFFER_SIZE;
  261. hComponent->portDefinition[0].nBufferCountActual = VPU_INPUT_BUF_NUMBER;
  262. hComponent->portDefinition[0].nBufferCountMin = VPU_INPUT_BUF_NUMBER;
  263. strcpy(hComponent->portDefinition[1].format.video.cMIMEType, "raw/video");
  264. hComponent->portDefinition[1].format.video.eColorFormat = OMX_COLOR_FormatYUV420SemiPlanar;
  265. hComponent->portDefinition[1].eDir = OMX_DirOutput;
  266. hComponent->portDefinition[1].nBufferSize = DEFAULT_VIDEO_OUTPUT_BUFFER_SIZE;
  267. hComponent->portDefinition[1].nBufferCountActual = VPU_OUTPUT_BUF_NUMBER;
  268. hComponent->portDefinition[1].nBufferCountMin = VPU_OUTPUT_BUF_NUMBER;
  269. memset(hComponent->pBufferArray, 0, sizeof(hComponent->pBufferArray));
  270. hComponent->memory_optimization = OMX_TRUE;
  271. FunctionOut();
  272. EXIT:
  273. return ret;
  274. ERROR:
  275. if (hComponent->pOMXComponent)
  276. {
  277. free(hComponent->pOMXComponent);
  278. hComponent->pOMXComponent = NULL;
  279. }
  280. if (hComponent->functions)
  281. {
  282. free(hComponent->functions);
  283. hComponent->functions = NULL;
  284. }
  285. if (hComponent->testConfig)
  286. {
  287. free(hComponent->testConfig);
  288. hComponent->testConfig = NULL;
  289. }
  290. if (hComponent->lsnCtx)
  291. {
  292. free(hComponent->lsnCtx);
  293. hComponent->lsnCtx = NULL;
  294. }
  295. if (hComponent->config)
  296. {
  297. free(hComponent->config);
  298. hComponent->config = NULL;
  299. }
  300. return ret;
  301. }