SF_OMX_video_common.c 10 KB

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