SF_OMX_Wave420L_encoder.c 93 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2022 StarFive Technology Co., Ltd.
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <unistd.h>
  8. #include <string.h>
  9. #include <sys/types.h>
  10. #include <unistd.h>
  11. #include "SF_OMX_Wave420L_encoder.h"
  12. #include "SF_OMX_Core.h"
  13. #include <sys/prctl.h>
  14. #define STREAM_READ_ALL_SIZE (0)
  15. #define WAVE420L_CONFIG_FILE "/lib/firmware/encoder_defconfig.cfg"
  16. #define OMX_Command_StopThread OMX_CommandMax
  17. static OMX_BOOL EmptyBufferDone(SF_OMX_COMPONENT *pSfOMXComponent, OMX_BUFFERHEADERTYPE *pBuffer);
  18. static OMX_BOOL FillBufferDone(SF_OMX_COMPONENT *pSfOMXComponent, OMX_BUFFERHEADERTYPE *pBuffer);
  19. static void FlushBuffer(SF_OMX_COMPONENT *pSfOMXComponent, OMX_U32 nPortNumber);
  20. static void CmdThread(void *args);
  21. static void EncoderThread(void *args);
  22. static void sf_get_component_functions(SF_W420L_FUNCTIONS *funcs, OMX_PTR *sohandle)
  23. {
  24. FunctionIn();
  25. funcs->VPU_GetProductId = dlsym(sohandle, "VPU_GetProductId");
  26. funcs->VPU_GetVersionInfo = dlsym(sohandle, "VPU_GetVersionInfo");
  27. funcs->VPU_InitWithBitcode = dlsym(sohandle, "VPU_InitWithBitcode");
  28. funcs->VPU_DeInit = dlsym(sohandle, "VPU_DeInit");
  29. funcs->GetEncOpenParam = dlsym(sohandle, "GetEncOpenParam");
  30. funcs->GetEncOpenParamDefault = dlsym(sohandle, "GetEncOpenParamDefault");
  31. funcs->VPU_EncOpen = dlsym(sohandle, "VPU_EncOpen");
  32. funcs->VPU_EncClose = dlsym(sohandle, "VPU_EncClose");
  33. funcs->VPU_GetFrameBufSize = dlsym(sohandle, "VPU_GetFrameBufSize");
  34. funcs->VPU_EncRegisterFrameBuffer = dlsym(sohandle, "VPU_EncRegisterFrameBuffer");
  35. funcs->VPU_EncAllocateFrameBuffer = dlsym(sohandle, "VPU_EncAllocateFrameBuffer");
  36. funcs->VPU_EncGiveCommand = dlsym(sohandle, "VPU_EncGiveCommand");
  37. funcs->VPU_EncGetInitialInfo = dlsym(sohandle, "VPU_EncGetInitialInfo");
  38. funcs->VPU_SWReset = dlsym(sohandle, "VPU_SWReset");
  39. funcs->VPU_EncStartOneFrame = dlsym(sohandle, "VPU_EncStartOneFrame");
  40. funcs->VPU_WaitInterrupt = dlsym(sohandle, "VPU_WaitInterrupt");
  41. funcs->VPU_ClearInterrupt = dlsym(sohandle, "VPU_ClearInterrupt");
  42. funcs->VPU_EncGetOutputInfo = dlsym(sohandle, "VPU_EncGetOutputInfo");
  43. funcs->vdi_allocate_dma_memory = dlsym(sohandle, "vdi_allocate_dma_memory");
  44. funcs->vdi_free_dma_memory = dlsym(sohandle, "vdi_free_dma_memory");
  45. funcs->YuvFeeder_Create = dlsym(sohandle, "YuvFeeder_Create");
  46. funcs->YuvFeeder_Feed = dlsym(sohandle, "YuvFeeder_Feed");
  47. funcs->YuvFeeder_Destroy = dlsym(sohandle, "YuvFeeder_Destroy");
  48. funcs->BufferStreamReader_Create = dlsym(sohandle, "BufferStreamReader_Create");
  49. funcs->BufferStreamReader_Act = dlsym(sohandle, "BufferStreamReader_Act");
  50. funcs->BufferStreamReader_Destroy = dlsym(sohandle, "BufferStreamReader_Destroy");
  51. funcs->Comparator_Create = dlsym(sohandle, "Comparator_Create");
  52. funcs->Comparator_Destroy = dlsym(sohandle, "Comparator_Destroy");
  53. funcs->Comparator_CheckEOF = dlsym(sohandle, "Comparator_CheckEOF");
  54. funcs->DisplayEncodedInformation = dlsym(sohandle, "DisplayEncodedInformation");
  55. funcs->PrintVpuVersionInfo = dlsym(sohandle, "PrintVpuVersionInfo");
  56. funcs->CalcStride = dlsym(sohandle, "CalcStride");
  57. funcs->GetPackedFormat = dlsym(sohandle, "GetPackedFormat");
  58. funcs->LoadFirmware = dlsym(sohandle, "LoadFirmware");
  59. funcs->SetMaxLogLevel = dlsym(sohandle, "SetMaxLogLevel");
  60. funcs->EnterLock = dlsym(sohandle, "EnterLock");
  61. funcs->LeaveLock = dlsym(sohandle, "LeaveLock");
  62. funcs->PrintVpuStatus = dlsym(sohandle, "PrintVpuStatus");
  63. FunctionOut();
  64. }
  65. OMX_ERRORTYPE WaveOmxInit(SF_OMX_COMPONENT *pSfOMXComponent)
  66. {
  67. OMX_ERRORTYPE ret = OMX_ErrorNone;
  68. char *strDebugLevel = NULL;
  69. int debugLevel = 0;
  70. SF_WAVE420L_IMPLEMEMT *pImp = NULL;
  71. FunctionIn();
  72. if (pSfOMXComponent == NULL)
  73. {
  74. ret = OMX_ErrorBadParameter;
  75. goto EXIT;
  76. }
  77. pSfOMXComponent->pOMXComponent = malloc(sizeof(OMX_COMPONENTTYPE));
  78. if (pSfOMXComponent->pOMXComponent == NULL)
  79. {
  80. ret = OMX_ErrorInsufficientResources;
  81. LOG(SF_LOG_ERR, "malloc fail\r\n");
  82. goto ERROR;
  83. }
  84. memset(pSfOMXComponent->pOMXComponent, 0, sizeof(OMX_COMPONENTTYPE));
  85. pSfOMXComponent->soHandle = dlopen(pSfOMXComponent->libName, RTLD_NOW);
  86. if (pSfOMXComponent->soHandle == NULL)
  87. {
  88. ret = OMX_ErrorInsufficientResources;
  89. LOG(SF_LOG_ERR, "could not open %s, error: %s\r\n", pSfOMXComponent->libName, dlerror());
  90. goto ERROR;
  91. }
  92. pSfOMXComponent->componentImpl = malloc(sizeof(SF_WAVE420L_IMPLEMEMT));
  93. if (pSfOMXComponent->componentImpl == NULL)
  94. {
  95. ret = OMX_ErrorInsufficientResources;
  96. LOG(SF_LOG_ERR, "malloc fail\r\n");
  97. goto ERROR;
  98. }
  99. memset(pSfOMXComponent->componentImpl, 0, sizeof(SF_WAVE420L_IMPLEMEMT));
  100. pImp = (SF_WAVE420L_IMPLEMEMT *)pSfOMXComponent->componentImpl;
  101. pImp->functions = malloc(sizeof(SF_W420L_FUNCTIONS));
  102. //pImp->frameFormat = DEFAULT_FRAME_FORMAT;
  103. if (pImp->functions == NULL)
  104. {
  105. ret = OMX_ErrorInsufficientResources;
  106. LOG(SF_LOG_ERR, "malloc fail\r\n");
  107. goto ERROR;
  108. }
  109. memset(pImp->functions, 0, sizeof(SF_W420L_FUNCTIONS));
  110. sf_get_component_functions(pImp->functions, pSfOMXComponent->soHandle);
  111. // Init Vpu log
  112. if (pImp->functions->SetMaxLogLevel)
  113. {
  114. strDebugLevel = getenv("VPU_DEBUG");
  115. if (strDebugLevel)
  116. {
  117. debugLevel = atoi(strDebugLevel);
  118. }else{
  119. debugLevel = 0;
  120. }
  121. if (debugLevel >=0)
  122. {
  123. pImp->functions->SetMaxLogLevel(debugLevel);
  124. }
  125. }
  126. pSfOMXComponent->pOMXComponent->pComponentPrivate = pSfOMXComponent;
  127. pImp->EmptyQueue = SF_Queue_Create(ENC_SRC_BUF_NUM, sizeof(OMX_BUFFERHEADERTYPE*));
  128. if (NULL == pImp->EmptyQueue)
  129. {
  130. LOG(SF_LOG_ERR, "create EmptyQueue error");
  131. return OMX_ErrorInsufficientResources;
  132. }
  133. pImp->FillQueue = SF_Queue_Create(ENC_SRC_BUF_NUM, sizeof(OMX_BUFFERHEADERTYPE*));
  134. if (NULL == pImp->FillQueue)
  135. {
  136. LOG(SF_LOG_ERR, "create FillQueue error");
  137. return OMX_ErrorInsufficientResources;
  138. }
  139. pImp->CmdQueue = SF_Queue_Create(ENC_SRC_BUF_NUM, sizeof(SF_OMX_CMD));
  140. if (NULL == pImp->CmdQueue)
  141. {
  142. LOG(SF_LOG_ERR, "create CmdQueue error");
  143. return OMX_ErrorInsufficientResources;
  144. }
  145. pImp->pauseQ = SF_Queue_Create(20, sizeof(OMX_BUFFERHEADERTYPE*));
  146. if (NULL == pImp->pauseQ)
  147. {
  148. LOG(SF_LOG_ERR, "create pauseQ error");
  149. return OMX_ErrorInsufficientResources;
  150. }
  151. for (int i = 0; i < OMX_PORT_MAX; i++)
  152. {
  153. OMX_PARAM_PORTDEFINITIONTYPE *pPortDefinition = &pSfOMXComponent->portDefinition[i];
  154. OMX_VIDEO_PARAM_HEVCTYPE *pHEVCComponent = &pImp->HEVCComponent[i];
  155. INIT_SET_SIZE_VERSION(pPortDefinition, OMX_PARAM_PORTDEFINITIONTYPE);
  156. INIT_SET_SIZE_VERSION(pHEVCComponent, OMX_VIDEO_PARAM_HEVCTYPE);
  157. pPortDefinition->nPortIndex = i;
  158. pPortDefinition->nBufferCountActual = (i == 0 ? WAVE_DEFAULT_INPUT_BUF_NUMBER : WAVE_DEFAULT_OUTPUT_BUF_NUMBER);
  159. pPortDefinition->nBufferCountMin = (i == 0 ? WAVE_DEFAULT_INPUT_BUF_NUMBER : WAVE_DEFAULT_OUTPUT_BUF_NUMBER);
  160. pPortDefinition->nBufferSize = (i == 0 ? DEFAULT_MJPEG_INPUT_BUFFER_SIZE : DEFAULT_MJPEG_OUTPUT_BUFFER_SIZE);
  161. pPortDefinition->eDomain = OMX_PortDomainVideo;
  162. pPortDefinition->format.video.nFrameWidth = DEFAULT_FRAME_WIDTH;
  163. pPortDefinition->format.video.nFrameHeight = DEFAULT_FRAME_HEIGHT;
  164. pPortDefinition->format.video.nStride = DEFAULT_FRAME_WIDTH;
  165. pPortDefinition->format.video.nSliceHeight = DEFAULT_FRAME_HEIGHT;
  166. pPortDefinition->format.video.xFramerate = DEFAULT_FRAMERATE;
  167. pPortDefinition->format.video.pNativeRender = 0;
  168. pPortDefinition->format.video.bFlagErrorConcealment = OMX_FALSE;
  169. pPortDefinition->bEnabled = OMX_TRUE;
  170. pPortDefinition->eDir = (i == 0 ? OMX_DirInput : OMX_DirOutput);
  171. pHEVCComponent->nPortIndex = i;
  172. pHEVCComponent->nKeyFrameInterval = DEFAULT_GOP;
  173. pHEVCComponent->eProfile = OMX_VIDEO_HEVCProfileMain;
  174. pSfOMXComponent->assignedBufferNum[i] = 0;
  175. }
  176. pSfOMXComponent->portDefinition[OMX_INPUT_PORT_INDEX].format.video.cMIMEType = "raw/video";
  177. pSfOMXComponent->portDefinition[OMX_OUTPUT_PORT_INDEX].format.video.cMIMEType = "video/H265";
  178. pSfOMXComponent->portDefinition[OMX_INPUT_PORT_INDEX].format.video.eColorFormat = OMX_COLOR_FormatYUV420Planar;
  179. pSfOMXComponent->portDefinition[OMX_OUTPUT_PORT_INDEX].format.video.eColorFormat = OMX_COLOR_FormatUnused;
  180. pSfOMXComponent->portDefinition[OMX_INPUT_PORT_INDEX].format.video.eCompressionFormat = OMX_VIDEO_CodingUnused;
  181. pSfOMXComponent->portDefinition[OMX_OUTPUT_PORT_INDEX].format.video.eCompressionFormat = OMX_VIDEO_CodingHEVC;
  182. memset(pSfOMXComponent->pBufferArray, 0, sizeof(pSfOMXComponent->pBufferArray));
  183. pSfOMXComponent->memory_optimization = OMX_TRUE;
  184. /* Set componentVersion */
  185. pSfOMXComponent->componentVersion.s.nVersionMajor = VERSIONMAJOR_NUMBER;
  186. pSfOMXComponent->componentVersion.s.nVersionMinor = VERSIONMINOR_NUMBER;
  187. pSfOMXComponent->componentVersion.s.nRevision = REVISION_NUMBER;
  188. pSfOMXComponent->componentVersion.s.nStep = STEP_NUMBER;
  189. /* Set specVersion */
  190. pSfOMXComponent->specVersion.s.nVersionMajor = VERSIONMAJOR_NUMBER;
  191. pSfOMXComponent->specVersion.s.nVersionMinor = VERSIONMINOR_NUMBER;
  192. pSfOMXComponent->specVersion.s.nRevision = REVISION_NUMBER;
  193. pSfOMXComponent->specVersion.s.nStep = STEP_NUMBER;
  194. memset(pSfOMXComponent->markType, 0, sizeof(pSfOMXComponent->markType));
  195. pSfOMXComponent->propagateMarkType.hMarkTargetComponent = NULL;
  196. pSfOMXComponent->propagateMarkType.pMarkData = NULL;
  197. for (int i = 0; i < 2; i++)
  198. {
  199. ret = SF_SemaphoreCreate(&pSfOMXComponent->portSemaphore[i]);
  200. if (ret)
  201. goto ERROR;
  202. ret = SF_SemaphoreCreate(&pSfOMXComponent->portUnloadSemaphore[i]);
  203. if (ret)
  204. goto ERROR;
  205. }
  206. ret = SF_SemaphoreCreate(&pImp->pauseSemaphore);
  207. if (ret)
  208. goto ERROR;
  209. CreateThread(&pImp->pCmdThread, CmdThread, (void *)pSfOMXComponent);
  210. pImp->bCmdRunning = OMX_TRUE;
  211. pImp->tmpFramerate = 25;
  212. FunctionOut();
  213. EXIT:
  214. return ret;
  215. ERROR:
  216. for (int i = 0; i < 2; i++)
  217. {
  218. SF_SemaphoreTerminate(pSfOMXComponent->portSemaphore[i]);
  219. SF_SemaphoreTerminate(pSfOMXComponent->portUnloadSemaphore[i]);
  220. }
  221. SF_SemaphoreTerminate(pImp->pauseSemaphore);
  222. if (pSfOMXComponent->pOMXComponent)
  223. {
  224. free(pSfOMXComponent->pOMXComponent);
  225. pSfOMXComponent->pOMXComponent = NULL;
  226. }
  227. return ret;
  228. }
  229. static OMX_ERRORTYPE SetDefaultConfig(SF_OMX_COMPONENT *pSfOMXComponent)
  230. {
  231. OMX_ERRORTYPE ret = OMX_ErrorNone;
  232. SF_WAVE420L_IMPLEMEMT *pImp = (SF_WAVE420L_IMPLEMEMT *)pSfOMXComponent->componentImpl;
  233. TestEncConfig *pEncConfig = &pImp->encConfig;
  234. FunctionIn();
  235. pEncConfig->stdMode = STD_HEVC;
  236. pEncConfig->frame_endian = VPU_FRAME_ENDIAN;
  237. pEncConfig->stream_endian = VPU_STREAM_ENDIAN;
  238. pEncConfig->source_endian = VPU_SOURCE_ENDIAN;
  239. pEncConfig->mapType = COMPRESSED_FRAME_MAP;
  240. pEncConfig->coreIdx = 0;
  241. pEncConfig->picWidth = DEFAULT_FRAME_WIDTH;
  242. pEncConfig->picHeight = DEFAULT_FRAME_HEIGHT;
  243. pEncConfig->cbcrInterleave = OMX_FALSE;
  244. pEncConfig->nv21 = OMX_FALSE;
  245. pEncConfig->srcFormat = FORMAT_420;
  246. pEncConfig->packedFormat = NOT_PACKED;
  247. pEncConfig->yuv_mode = NOT_PACKED;
  248. strcpy(pEncConfig->cfgFileName, WAVE420L_CONFIG_FILE);
  249. //Wave420L not support
  250. pEncConfig->rotAngle = 0;
  251. pEncConfig->mirDir = 0;
  252. pEncConfig->srcFormat3p4b = 0;
  253. pEncConfig->ringBufferEnable = 0;
  254. FunctionOut();
  255. return ret;
  256. }
  257. static OMX_ERRORTYPE InitEncoder(SF_OMX_COMPONENT *pSfOMXComponent)
  258. {
  259. RetCode ret;
  260. SF_WAVE420L_IMPLEMEMT *pImp = (SF_WAVE420L_IMPLEMEMT *)pSfOMXComponent->componentImpl;
  261. TestEncConfig *pEncConfig = &pImp->encConfig;
  262. Int32 productId;
  263. Uint32 BitCodesizeInWord;
  264. Uint16* pusBitCode;
  265. FunctionIn();
  266. productId = Warp_VPU_GetProductId(pImp, pEncConfig->coreIdx);
  267. LOG(SF_LOG_INFO, "product ID = %d FW PATH = %s\r\n", productId, pSfOMXComponent->fwPath);
  268. if (Warp_LoadFirmware(pImp, productId, (Uint8**)&pusBitCode, &BitCodesizeInWord, pSfOMXComponent->fwPath) < 0) {
  269. LOG(SF_LOG_ERR, "%s:%d Failed to load firmware: %s\r\n", __FUNCTION__, __LINE__, pSfOMXComponent->fwPath);
  270. return OMX_ErrorInsufficientResources;
  271. }
  272. ret = Warp_VPU_InitWithBitcode(pImp, pEncConfig->coreIdx, (const Uint16*)pusBitCode, BitCodesizeInWord);
  273. if (ret && ret != RETCODE_CALLED_BEFORE)
  274. {
  275. LOG(SF_LOG_ERR, "Failed to boot up VPU with bit code\r\n");
  276. free(pusBitCode);
  277. return OMX_ErrorInsufficientResources;
  278. }
  279. Warp_PrintVpuVersionInfo(pImp, pEncConfig->coreIdx);
  280. free(pusBitCode);
  281. return OMX_ErrorNone;
  282. FunctionOut();
  283. }
  284. static void CmdThread(void *args)
  285. {
  286. SF_OMX_COMPONENT *pSfOMXComponent = (SF_OMX_COMPONENT *)args;
  287. SF_WAVE420L_IMPLEMEMT *pImp = (SF_WAVE420L_IMPLEMEMT *)pSfOMXComponent->componentImpl;
  288. OMX_BUFFERHEADERTYPE *pOMXBuffer = NULL;
  289. OMX_BUFFERHEADERTYPE **ppBuffer = NULL;
  290. OMX_ERRORTYPE ret = OMX_ErrorNone;
  291. OMX_STATETYPE comCurrentState;
  292. OMX_U32 i = 0, cnt = 0;
  293. SF_OMX_CMD *pCmd;
  294. void *pNull = NULL;
  295. void *ThreadRet;
  296. while(1){
  297. pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
  298. pCmd = (SF_OMX_CMD*)SF_Queue_Dequeue_Block(pImp->CmdQueue);
  299. pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
  300. pthread_testcancel();
  301. pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  302. if(pCmd->Cmd == OMX_Command_StopThread)
  303. break;
  304. switch (pCmd->Cmd)
  305. {
  306. case OMX_CommandStateSet:
  307. LOG(SF_LOG_INFO, "OMX dest state = %X\r\n", pCmd->nParam);
  308. comCurrentState = pSfOMXComponent->state;
  309. if (comCurrentState == pCmd->nParam)
  310. {
  311. LOG(SF_LOG_DEBUG, "same state %d\r\n", pCmd->nParam);
  312. pSfOMXComponent->callbacks->EventHandler(pSfOMXComponent->pOMXComponent, pSfOMXComponent->pAppData,
  313. OMX_EventError, OMX_ErrorSameState, pCmd->nParam, NULL);
  314. break;
  315. }
  316. ret = OMX_ErrorNone;
  317. switch (pCmd->nParam)
  318. {
  319. case OMX_StateInvalid:
  320. switch (comCurrentState)
  321. {
  322. case OMX_StateExecuting:
  323. pImp->bThreadRunning = 0;
  324. // enqueue null mean encoder thread cycle end
  325. SF_Queue_Enqueue(pImp->EmptyQueue, &pNull);
  326. SF_Queue_Enqueue(pImp->FillQueue, &pNull);
  327. pthread_join(pImp->pProcessThread->pthread, &ThreadRet);
  328. case OMX_StateIdle:
  329. case OMX_StatePause:
  330. case OMX_StateLoaded:
  331. case OMX_StateWaitForResources:
  332. default:
  333. pSfOMXComponent->state = OMX_StateInvalid;
  334. break;
  335. }
  336. ret = OMX_ErrorInvalidState;
  337. break;
  338. case OMX_StateLoaded:
  339. switch (comCurrentState)
  340. {
  341. case OMX_StateWaitForResources:
  342. break;
  343. case OMX_StateIdle:
  344. Warp_VPU_DeInit(pImp, pImp->coreIdx);
  345. for (i = 0; i < 2; i++)
  346. {
  347. if (pSfOMXComponent->portDefinition[i].bEnabled){
  348. LOG(SF_LOG_INFO,"unload SemaphoreWait \r\n");
  349. SF_SemaphoreWait(pSfOMXComponent->portUnloadSemaphore[i]);
  350. LOG(SF_LOG_INFO,"unload SemaphoreWait out\r\n");
  351. }
  352. }
  353. break;
  354. default:
  355. ret = OMX_ErrorIncorrectStateTransition;
  356. break;
  357. }
  358. break;
  359. case OMX_StateIdle:
  360. if (comCurrentState == OMX_StatePause)
  361. {
  362. comCurrentState = pSfOMXComponent->stateBeforePause;
  363. }
  364. switch (comCurrentState)
  365. {
  366. case OMX_StateLoaded:
  367. ret = InitEncoder(pSfOMXComponent);
  368. if (ret)
  369. {
  370. break;
  371. }
  372. for (i = 0; i < 2; i++)
  373. {
  374. if (pSfOMXComponent->portDefinition[i].bEnabled){
  375. LOG(SF_LOG_INFO,"SemaphoreWait \r\n");
  376. SF_SemaphoreWait(pSfOMXComponent->portSemaphore[i]);
  377. LOG(SF_LOG_INFO,"SemaphoreWait out\r\n");
  378. }
  379. }
  380. break;
  381. case OMX_StateExecuting:
  382. pImp->bThreadRunning = 0;
  383. // enqueue null mean encoder thread cycle end
  384. SF_Queue_Enqueue(pImp->EmptyQueue, &pNull);
  385. SF_Queue_Enqueue(pImp->FillQueue, &pNull);
  386. pthread_join(pImp->pProcessThread->pthread, &ThreadRet);
  387. LOG(SF_LOG_INFO, "Encoder thread end %ld\r\n", (Uint64)ThreadRet);
  388. FlushBuffer(pSfOMXComponent,OMX_INPUT_PORT_INDEX);
  389. FlushBuffer(pSfOMXComponent,OMX_OUTPUT_PORT_INDEX);
  390. SF_Queue_Flush(pImp->EmptyQueue);
  391. SF_Queue_Flush(pImp->FillQueue);
  392. break;
  393. case OMX_StateIdle:
  394. pImp->bPause = OMX_FALSE;
  395. break;
  396. default:
  397. ret = OMX_ErrorIncorrectStateTransition;
  398. break;
  399. }
  400. break;
  401. case OMX_StateWaitForResources:
  402. switch (comCurrentState)
  403. {
  404. case OMX_StateLoaded:
  405. break;
  406. default:
  407. ret = OMX_ErrorIncorrectStateTransition;
  408. break;
  409. }
  410. break;
  411. case OMX_StateExecuting:
  412. if (comCurrentState == OMX_StatePause)
  413. {
  414. comCurrentState = pSfOMXComponent->stateBeforePause;
  415. }
  416. switch (comCurrentState)
  417. {
  418. case OMX_StateIdle:
  419. pImp->bThreadRunning = OMX_TRUE;
  420. pImp->bPause = OMX_FALSE;
  421. pImp->tmpCounter = 0;
  422. CreateThread(&pImp->pProcessThread, EncoderThread, (void *)pSfOMXComponent);
  423. break;
  424. case OMX_StateExecuting:
  425. pImp->bPause = OMX_FALSE;
  426. SF_SemaphorePost(pImp->pauseSemaphore);
  427. break;
  428. default:
  429. ret = OMX_ErrorIncorrectStateTransition;
  430. break;
  431. }
  432. break;
  433. case OMX_StatePause:
  434. switch (comCurrentState)
  435. {
  436. case OMX_StateIdle:
  437. case OMX_StateExecuting:
  438. pImp->bPause = OMX_TRUE;
  439. pSfOMXComponent->stateBeforePause = comCurrentState;
  440. break;
  441. default:
  442. ret = OMX_ErrorIncorrectStateTransition;
  443. break;
  444. }
  445. break;
  446. default:
  447. ret = OMX_ErrorIncorrectStateTransition;
  448. break;
  449. }
  450. if (ret == OMX_ErrorNone)
  451. {
  452. pSfOMXComponent->state = pCmd->nParam;
  453. pSfOMXComponent->traningState = OMX_TransStateInvalid;
  454. pSfOMXComponent->callbacks->EventHandler(pSfOMXComponent->pOMXComponent, pSfOMXComponent->pAppData,
  455. OMX_EventCmdComplete, OMX_CommandStateSet, pCmd->nParam, NULL);
  456. LOG(SF_LOG_DEBUG, "complete cmd StateSet %d\r\n", pCmd->nParam);
  457. if (pSfOMXComponent->state == OMX_StateExecuting)
  458. {
  459. ppBuffer = SF_Queue_Dequeue(pImp->pauseQ);
  460. while (ppBuffer)
  461. {
  462. pOMXBuffer = *ppBuffer;
  463. pSfOMXComponent->callbacks->FillBufferDone(pSfOMXComponent->pOMXComponent, pSfOMXComponent->pAppData, pOMXBuffer);
  464. ppBuffer = SF_Queue_Dequeue(pImp->pauseQ);
  465. }
  466. }
  467. }
  468. else
  469. {
  470. pSfOMXComponent->callbacks->EventHandler(pSfOMXComponent->pOMXComponent, pSfOMXComponent->pAppData,
  471. OMX_EventError, ret, 0, NULL);
  472. LOG(SF_LOG_DEBUG, "Can't go to state %d from %d, ret %X \r\n",pSfOMXComponent->state, pCmd->nParam, ret);
  473. }
  474. break;
  475. case OMX_CommandFlush:
  476. {
  477. LOG(SF_LOG_INFO, "flush port %d\r\n", pCmd->nParam);
  478. OMX_U32 nPort = pCmd->nParam;
  479. cnt = (pCmd->nParam == OMX_ALL) ? 2 : 1;
  480. for (i = 0; i < cnt; i++) {
  481. if (pCmd->nParam == OMX_ALL)
  482. nPort = i;
  483. else
  484. nPort = pCmd->nParam;
  485. if (nPort == 0)
  486. {
  487. FlushBuffer(pSfOMXComponent, nPort);
  488. pSfOMXComponent->callbacks->EventHandler(pSfOMXComponent->pOMXComponent, pSfOMXComponent->pAppData,
  489. OMX_EventCmdComplete, OMX_CommandFlush, nPort, NULL);
  490. }
  491. else
  492. {
  493. FlushBuffer(pSfOMXComponent, nPort);
  494. if (pSfOMXComponent->state == OMX_StatePause)
  495. {
  496. ppBuffer = SF_Queue_Dequeue(pImp->pauseQ);
  497. while (ppBuffer)
  498. {
  499. pOMXBuffer = *ppBuffer;
  500. pSfOMXComponent->callbacks->FillBufferDone(pSfOMXComponent->pOMXComponent, pSfOMXComponent->pAppData, pOMXBuffer);
  501. ppBuffer = SF_Queue_Dequeue(pImp->pauseQ);
  502. }
  503. }
  504. pSfOMXComponent->callbacks->EventHandler(pSfOMXComponent->pOMXComponent, pSfOMXComponent->pAppData,
  505. OMX_EventCmdComplete, OMX_CommandFlush, nPort, NULL);
  506. }
  507. }
  508. }
  509. break;
  510. case OMX_CommandPortDisable:
  511. {
  512. LOG(SF_LOG_INFO, "disable port %d\r\n", pCmd->nParam);
  513. OMX_U32 nPort;
  514. cnt = (pCmd->nParam == OMX_ALL) ? 2 : 1;
  515. for (i = 0; i < cnt; i++) {
  516. if (pCmd->nParam == OMX_ALL)
  517. nPort = i;
  518. else
  519. nPort = pCmd->nParam;
  520. FlushBuffer(pSfOMXComponent, nPort);
  521. }
  522. for (i = 0; i < cnt; i++) {
  523. if (pCmd->nParam == OMX_ALL)
  524. nPort = i;
  525. else
  526. nPort = pCmd->nParam;
  527. if (pSfOMXComponent->state != OMX_StateLoaded)
  528. SF_SemaphoreWait(pSfOMXComponent->portUnloadSemaphore[nPort]);
  529. pSfOMXComponent->callbacks->EventHandler(pSfOMXComponent->pOMXComponent, pSfOMXComponent->pAppData,
  530. OMX_EventCmdComplete, OMX_CommandPortDisable, nPort, NULL);
  531. }
  532. }
  533. break;
  534. case OMX_CommandPortEnable:
  535. {
  536. LOG(SF_LOG_INFO, "enable port %d\r\n", pCmd->nParam);
  537. OMX_U32 nPort;
  538. cnt = (pCmd->nParam == OMX_ALL) ? 2 : 1;
  539. for (i = 0; i < cnt; i++) {
  540. if (pCmd->nParam == OMX_ALL)
  541. nPort = i;
  542. else
  543. nPort = pCmd->nParam;
  544. if ((pSfOMXComponent->state != OMX_StateLoaded) &&
  545. (pSfOMXComponent->state != OMX_StateWaitForResources))
  546. SF_SemaphoreWait(pSfOMXComponent->portSemaphore[nPort]);
  547. pSfOMXComponent->callbacks->EventHandler(pSfOMXComponent->pOMXComponent, pSfOMXComponent->pAppData,
  548. OMX_EventCmdComplete, OMX_CommandPortEnable, nPort, NULL);
  549. }
  550. }
  551. break;
  552. case OMX_CommandMarkBuffer:
  553. {
  554. LOG(SF_LOG_INFO, "set mark %d\r\n", pCmd->nParam);
  555. OMX_U32 nPort;
  556. nPort = pCmd->nParam;
  557. pSfOMXComponent->markType[nPort].hMarkTargetComponent =
  558. ((OMX_MARKTYPE *)pCmd->pCmdData)->hMarkTargetComponent;
  559. pSfOMXComponent->markType[nPort].pMarkData =
  560. ((OMX_MARKTYPE *)pCmd->pCmdData)->pMarkData;
  561. }
  562. break;
  563. default:
  564. break;
  565. }
  566. }
  567. pthread_exit(NULL);
  568. }
  569. static void EncoderThread(void *args)
  570. {
  571. SF_OMX_COMPONENT *pSfOMXComponent = (SF_OMX_COMPONENT *)args;
  572. SF_WAVE420L_IMPLEMEMT *pImp = (SF_WAVE420L_IMPLEMEMT *)pSfOMXComponent->componentImpl;
  573. TestEncConfig *pEncConfig = &pImp->encConfig;
  574. EncOpenParam *pEncOP = &pImp->encOP;
  575. EncHandle *pHandle = &pImp->handle;
  576. EncInitialInfo *pInitialInfo = &pImp->initialInfo;
  577. EncOutputInfo outputInfo;
  578. FrameBufferAllocInfo fbAllocInfo;
  579. FrameBufferFormat srcFrameFormat;
  580. EncParam encParam;
  581. EncHeaderParam encHeaderParam;
  582. YuvInfo yuvFeederInfo;
  583. ENC_CFG encCfg;
  584. OMX_BUFFERHEADERTYPE *pOutputBuffer;
  585. OMX_BUFFERHEADERTYPE *pInputBuffer;
  586. BitstreamReader bsReader = NULL;
  587. SecAxiUse secAxiUse;
  588. Comparator comparatorBitStream = NULL;//todo use comparator
  589. Int32 srcFrameWidth, srcFrameHeight, srcFrameStride;
  590. Int32 framebufStride = 0, framebufWidth = 0, framebufHeight = 0, FrameBufSize = 0;
  591. Int32 regFrameBufCount;
  592. Int32 productId, instIdx, coreIdx, mapType;
  593. Int32 i, ret, bsBufferCount, bsQueueIndex = 0;
  594. Int32 srcFrameIdx = 0, frameIdx = 0;
  595. Int32 timeoutCount = 0, int_reason = 0;
  596. Uint32 interruptTimeout = VPU_ENC_TIMEOUT;
  597. Uint32 geteos = 0;
  598. Uint32 size;
  599. Uint32 KeyFrameInterval;
  600. void* yuvFeeder = NULL;
  601. pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
  602. instIdx = pEncConfig->instNum;
  603. coreIdx = pEncConfig->coreIdx;
  604. productId = Warp_VPU_GetProductId(pImp, coreIdx);
  605. pEncOP->bitstreamFormat = pEncConfig->stdMode;
  606. mapType = (pEncConfig->mapType & 0x0f);
  607. //Warp_GetEncOpenParamDefault(pImp, pEncOP, pEncConfig);
  608. if (strlen(pEncConfig->cfgFileName) != 0) {
  609. Warp_GetEncOpenParam(pImp, pEncOP, pEncConfig, &encCfg);
  610. }
  611. else {
  612. Warp_GetEncOpenParamDefault(pImp, pEncOP, pEncConfig);
  613. }
  614. //if modify encop should be here
  615. srcFrameWidth = ((pEncOP->picWidth+7)&~7); // width = 8-aligned (CU unit)
  616. srcFrameHeight = ((pEncOP->picHeight+7)&~7); // height = 8-aligned (CU unit)
  617. srcFrameStride = ((pEncOP->picWidth+31)&~31); // stride should be a 32-aligned.
  618. if (pEncConfig->packedFormat >= 1)
  619. pEncConfig->srcFormat = FORMAT_422;
  620. if (pEncConfig->srcFormat == FORMAT_422 && pEncConfig->packedFormat >= PACKED_YUYV) {
  621. Int32 p10bits = pEncConfig->srcFormat3p4b == 0 ? 16 : 32;
  622. Int32 packedFormat =
  623. pImp->functions->GetPackedFormat(pEncOP->srcBitDepth, pEncConfig->packedFormat, p10bits, 1);
  624. if (packedFormat == -1) {
  625. LOG(SF_LOG_ERR, "fail to GetPackedFormat\n" );
  626. goto ERR_ENC_INIT;
  627. }
  628. pEncOP->srcFormat = packedFormat;
  629. srcFrameFormat = (FrameBufferFormat)packedFormat;
  630. pEncOP->nv21 = 0;
  631. pEncOP->cbcrInterleave = 0;
  632. }
  633. else {
  634. pEncOP->srcFormat = pEncConfig->srcFormat;
  635. srcFrameFormat = (FrameBufferFormat)pEncConfig->srcFormat;
  636. pEncOP->nv21 = pEncConfig->nv21;
  637. }
  638. pEncOP->packedFormat = pEncConfig->packedFormat;
  639. framebufWidth = (pEncOP->picWidth +7)&~7;
  640. framebufHeight = (pEncOP->picHeight +7)&~7;
  641. bsBufferCount = DEFAULT_BS_BUFF;
  642. for (i=0; i<bsBufferCount; i++ ) {
  643. pImp->vbStream[i].size = framebufWidth * framebufWidth *2;
  644. if (Warp_vdi_allocate_dma_memory(pImp, coreIdx, &pImp->vbStream[i]) < 0) {
  645. LOG(SF_LOG_ERR, "fail to allocate bitstream buffer\n" );
  646. goto ERR_ENC_INIT;
  647. }
  648. LOG(SF_LOG_DEBUG, "STREAM_BUF=0x%x STREAM_BUF_SIZE=%d(0x%x)\n",pImp->vbStream[i].phys_addr, pImp->vbStream[i].size, pImp->vbStream[i].size);
  649. }
  650. pSfOMXComponent->portDefinition[OMX_OUTPUT_PORT_INDEX].format.video.nStride = framebufWidth;
  651. pSfOMXComponent->portDefinition[OMX_OUTPUT_PORT_INDEX].format.video.nSliceHeight = framebufHeight;
  652. pSfOMXComponent->portDefinition[OMX_OUTPUT_PORT_INDEX].nBufferSize = framebufWidth * framebufHeight * 2;
  653. LOG(SF_LOG_DEBUG, "OMX_EventPortSettingsChanged IN\r\n");
  654. pSfOMXComponent->callbacks->EventHandler(pSfOMXComponent->pOMXComponent, pSfOMXComponent->pAppData, OMX_EventPortSettingsChanged,
  655. OMX_OUTPUT_PORT_INDEX, OMX_IndexParamPortDefinition, NULL);
  656. LOG(SF_LOG_DEBUG, "OMX_EventPortSettingsChanged OUT\r\n");
  657. pEncOP->bitstreamBuffer = pImp->vbStream[0].phys_addr;
  658. pEncOP->bitstreamBufferSize = pImp->vbStream[0].size;//* bsBufferCount;//
  659. pEncOP->ringBufferEnable = pEncConfig->ringBufferEnable;
  660. pEncOP->cbcrInterleave = pEncConfig->cbcrInterleave;
  661. pEncOP->frameEndian = pEncConfig->frame_endian;
  662. pEncOP->streamEndian = pEncConfig->stream_endian;
  663. pEncOP->sourceEndian = pEncConfig->source_endian;
  664. pEncOP->lineBufIntEn = pEncConfig->lineBufIntEn;
  665. pEncOP->coreIdx = coreIdx;
  666. pEncOP->cbcrOrder = CBCR_ORDER_NORMAL;
  667. // host can set useLongTerm to 1 or 0 directly
  668. pEncOP->EncStdParam.hevcParam.useLongTerm =
  669. (pEncConfig->useAsLongtermPeriod > 0 && pEncConfig->refLongtermPeriod > 0) ? 1 : 0;
  670. /* set framerate and bitrate */
  671. /* If xFramerate value from user is stored in Q16 format, should convert into integer */
  672. if(pSfOMXComponent->portDefinition[0].format.video.xFramerate > (1 << 16))
  673. {
  674. pEncOP->frameRateInfo = pSfOMXComponent->portDefinition[0].format.video.xFramerate >> 16;
  675. }
  676. else
  677. {
  678. pEncOP->frameRateInfo = pSfOMXComponent->portDefinition[0].format.video.xFramerate;
  679. }
  680. pEncOP->EncStdParam.hevcParam.numUnitsInTick = 1000;
  681. pEncOP->EncStdParam.hevcParam.timeScale = (pEncOP->frameRateInfo)*1000;
  682. if(pSfOMXComponent->portDefinition[1].format.video.nBitrate)
  683. {
  684. pEncOP->rcEnable = 1;
  685. pEncOP->bitRate = pSfOMXComponent->portDefinition[1].format.video.nBitrate;
  686. }
  687. else if(pEncOP->rcEnable == 1)
  688. {
  689. pSfOMXComponent->portDefinition[1].format.video.nBitrate = pEncOP->bitRate;
  690. }
  691. KeyFrameInterval = pImp->HEVCComponent[OMX_OUTPUT_PORT_INDEX].nKeyFrameInterval;
  692. if (KeyFrameInterval)
  693. {
  694. if (KeyFrameInterval == 1)
  695. {
  696. pEncOP->EncStdParam.hevcParam.gopParam.picParam[0].picType = PIC_TYPE_I;
  697. pEncOP->EncStdParam.hevcParam.gopParam.picParam[0].picQp = encCfg.hevcCfg.intraQP;
  698. }
  699. pEncOP->EncStdParam.hevcParam.intraPeriod = KeyFrameInterval;
  700. }
  701. ret = Warp_VPU_EncOpen(pImp, pHandle, pEncOP);
  702. if (ret) {
  703. LOG(SF_LOG_ERR, "VPU_EncOpen failed Error code is 0x%x \n", ret );
  704. goto ERR_ENC_INIT;
  705. }
  706. ret = Warp_VPU_EncGetInitialInfo(pImp, *pHandle, pInitialInfo);
  707. if (ret) {
  708. LOG(SF_LOG_ERR, "VPU_EncGetInitialInfo failed Error code is 0x%x \n", ret );
  709. goto ERR_ENC_OPEN;
  710. }
  711. LOG(SF_LOG_INFO, "* Enc InitialInfo =>\n instance #%d, \n minframeBuffercount: %u\n minSrcBufferCount: %d\n",
  712. instIdx, pInitialInfo->minFrameBufferCount, pInitialInfo->minSrcFrameCount);
  713. LOG(SF_LOG_INFO, " picWidth: %u\n picHeight: %u\n ",pEncOP->picWidth, pEncOP->picHeight);
  714. //create comparator here
  715. secAxiUse.u.wave4.useEncImdEnable = (pEncConfig->secondary_axi & 0x1)?TRUE:FALSE; //USE_IMD_INTERNAL_BUF
  716. secAxiUse.u.wave4.useEncRdoEnable = (pEncConfig->secondary_axi & 0x2)?TRUE:FALSE; //USE_RDO_INTERNAL_BUF
  717. secAxiUse.u.wave4.useEncLfEnable = (pEncConfig->secondary_axi & 0x4)?TRUE:FALSE; //USE_LF_INTERNAL_BUF
  718. Warp_VPU_EncGiveCommand(pImp, *pHandle, SET_SEC_AXI, &secAxiUse);
  719. /* Allocate framebuffers for recon. */
  720. framebufStride = Warp_CalcStride(pImp, framebufWidth, framebufHeight,
  721. (FrameBufferFormat)pEncOP->srcFormat, pEncOP->cbcrInterleave, (TiledMapType)mapType, FALSE);
  722. FrameBufSize = Warp_VPU_GetFrameBufSize(pImp, coreIdx, framebufStride,
  723. framebufHeight, (TiledMapType)mapType, pEncOP->srcFormat, pEncOP->cbcrInterleave, NULL);
  724. regFrameBufCount = pInitialInfo->minFrameBufferCount;
  725. for (i = 0; i < regFrameBufCount; i++) {
  726. pImp->vbReconFrameBuf[i].size = FrameBufSize;
  727. if (Warp_vdi_allocate_dma_memory(pImp,coreIdx, &pImp->vbReconFrameBuf[i]) < 0) {
  728. LOG(SF_LOG_ERR, "SF_LOG_fail to allocate recon buffer\n" );
  729. goto ERR_ENC_OPEN;
  730. }
  731. pImp->fbRecon[i].bufY = pImp->vbReconFrameBuf[i].phys_addr;
  732. pImp->fbRecon[i].bufCb = (PhysicalAddress)-1;
  733. pImp->fbRecon[i].bufCr = (PhysicalAddress)-1;
  734. pImp->fbRecon[i].size = FrameBufSize;
  735. pImp->fbRecon[i].updateFbInfo = TRUE;
  736. LOG(SF_LOG_DEBUG, "Recon_BUF=0x%x Recon_BUF_SIZE=%d(0x%x)\n",pImp->vbReconFrameBuf[i].phys_addr, pImp->vbReconFrameBuf[i].size, pImp->vbReconFrameBuf[i].size);
  737. }
  738. ret = Warp_VPU_EncRegisterFrameBuffer(pImp, *pHandle, pImp->fbRecon,
  739. regFrameBufCount, framebufStride, framebufHeight, mapType);
  740. if(ret) {
  741. LOG(SF_LOG_ERR, "VPU_EncRegisterFrameBuffer failed Error code is 0x%x \n", ret );
  742. goto ERR_ENC_OPEN;
  743. }
  744. fbAllocInfo.mapType = LINEAR_FRAME_MAP;
  745. srcFrameStride = Warp_CalcStride(pImp, srcFrameWidth, srcFrameHeight, (FrameBufferFormat)srcFrameFormat,
  746. pEncOP->cbcrInterleave, (TiledMapType)fbAllocInfo.mapType, FALSE);
  747. FrameBufSize = Warp_VPU_GetFrameBufSize(pImp, coreIdx, srcFrameStride, srcFrameHeight,
  748. (TiledMapType)fbAllocInfo.mapType, srcFrameFormat, pEncOP->cbcrInterleave, NULL);
  749. fbAllocInfo.format = (FrameBufferFormat)srcFrameFormat;
  750. fbAllocInfo.cbcrInterleave = pEncOP->cbcrInterleave;
  751. fbAllocInfo.stride = srcFrameStride;
  752. fbAllocInfo.height = srcFrameHeight;
  753. fbAllocInfo.endian = pEncOP->sourceEndian;
  754. fbAllocInfo.type = FB_TYPE_PPU;
  755. fbAllocInfo.num = pInitialInfo->minSrcFrameCount + EXTRA_SRC_BUFFER_NUM;
  756. fbAllocInfo.nv21 = pEncOP->nv21;
  757. LOG(SF_LOG_INFO, "Allocated source framebuffers : %d, size : %d\n", fbAllocInfo.num, FrameBufSize);
  758. for (i = 0; i < fbAllocInfo.num; i++) {
  759. pImp->vbSourceFrameBuf[i].size = FrameBufSize;
  760. if (Warp_vdi_allocate_dma_memory(pImp, coreIdx, &pImp->vbSourceFrameBuf[i]) < 0)
  761. {
  762. LOG(SF_LOG_ERR, "fail to allocate frame buffer\n" );
  763. goto ERR_ENC_OPEN;
  764. }
  765. pImp->fbSrc[i].bufY = pImp->vbSourceFrameBuf[i].phys_addr;
  766. pImp->fbSrc[i].bufCb = (PhysicalAddress)-1;
  767. pImp->fbSrc[i].bufCr = (PhysicalAddress)-1;
  768. pImp->fbSrc[i].size = FrameBufSize;
  769. pImp->fbSrc[i].updateFbInfo = TRUE;
  770. LOG(SF_LOG_DEBUG, "Source_BUF=0x%x Source_BUF_SIZE=%d(0x%x)\n",pImp->vbSourceFrameBuf[i].phys_addr, pImp->vbSourceFrameBuf[i].size, pImp->vbSourceFrameBuf[i].size);
  771. }
  772. ret = Warp_VPU_EncAllocateFrameBuffer(pImp, *pHandle, fbAllocInfo, pImp->fbSrc);
  773. if (ret) {
  774. LOG(SF_LOG_ERR, "VPU_EncAllocateFrameBuffer fail to allocate source frame buffer is 0x%x \n", ret );
  775. goto ERR_ENC_OPEN;
  776. }
  777. pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
  778. pthread_testcancel();
  779. pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  780. memset(&encParam, 0x00, sizeof(EncParam));
  781. encParam.skipPicture = 0;
  782. encParam.quantParam = pEncConfig->picQpY;
  783. encParam.skipPicture = 0;
  784. encParam.forcePicQpEnable = 0;
  785. encParam.forcePicQpI = 0;
  786. encParam.forcePicQpP = 0;
  787. encParam.forcePicQpB = 0;
  788. encParam.forcePicTypeEnable = 0;
  789. encParam.forcePicType = 0;
  790. encParam.codeOption.implicitHeaderEncode = 1; // FW will encode header data implicitly when changing the header syntaxes
  791. encParam.codeOption.encodeAUD = pEncConfig->encAUD;
  792. encParam.codeOption.encodeEOS = 0;
  793. memset(&encHeaderParam, 0x00, sizeof(EncHeaderParam));
  794. encHeaderParam.buf = pEncOP->bitstreamBuffer;
  795. encHeaderParam.size = pEncOP->bitstreamBufferSize;
  796. encHeaderParam.headerType = CODEOPT_ENC_VPS | CODEOPT_ENC_SPS | CODEOPT_ENC_PPS;
  797. ret = Warp_VPU_EncGiveCommand(pImp, *pHandle, ENC_PUT_VIDEO_HEADER, &encHeaderParam);
  798. if (ret) {
  799. LOG(SF_LOG_ERR, "VPU_EncGiveCommand ( ENC_PUT_VIDEO_HEADER ) for VPS/SPS/PPS failed Error Reason code : 0x%x \n", ret);
  800. goto ERR_ENC_OPEN;
  801. }
  802. if (encHeaderParam.size == 0) {
  803. LOG(SF_LOG_ERR, "encHeaderParam.size=0\n");
  804. goto ERR_ENC_OPEN;
  805. }
  806. bsReader = Warp_BufferStreamReader_Create(pImp, pEncOP->ringBufferEnable, (EndianMode)pEncOP->streamEndian, pHandle);
  807. pOutputBuffer = *(OMX_BUFFERHEADERTYPE**)SF_Queue_Dequeue_Block(pImp->FillQueue);
  808. if(pOutputBuffer == NULL){
  809. pImp->bThreadRunning = 0;
  810. goto ERR_ENC_OPEN;
  811. }
  812. LOG(SF_LOG_DEBUG, "get output buff %p index %d\r\n", pOutputBuffer->pBuffer,
  813. ((SF_OMX_BUF_INFO*)pOutputBuffer->pOutputPortPrivate)->index);
  814. Warp_EnterLock(pImp, coreIdx);
  815. size = Warp_BufferStreamReader_Act(pImp, bsReader, encHeaderParam.buf,
  816. pEncOP->bitstreamBufferSize, encHeaderParam.size, pOutputBuffer->pBuffer, comparatorBitStream);
  817. Warp_LeaveLock(pImp, coreIdx);
  818. if (size == 0) {
  819. LOG(SF_LOG_ERR, "fill header size 0, end\n");
  820. goto ERR_ENC_OPEN;
  821. }
  822. pOutputBuffer->nFilledLen = size;
  823. pOutputBuffer->nFlags = OMX_BUFFERFLAG_CODECCONFIG;
  824. FillBufferDone(pSfOMXComponent, pOutputBuffer);
  825. memset(&yuvFeederInfo, 0x00, sizeof(YuvInfo));
  826. yuvFeederInfo.cbcrInterleave = pEncConfig->cbcrInterleave;
  827. yuvFeederInfo.nv21 = pEncConfig->nv21;
  828. yuvFeederInfo.packedFormat = pEncConfig->packedFormat;
  829. yuvFeederInfo.srcFormat = pEncOP->srcFormat;
  830. yuvFeederInfo.srcPlanar = TRUE;
  831. yuvFeederInfo.srcStride = srcFrameStride;
  832. yuvFeederInfo.srcHeight = srcFrameHeight;
  833. yuvFeeder = Warp_YuvFeeder_Create(pImp, SOURCE_YUV_WITH_BUFFER, NULL, yuvFeederInfo);
  834. if ( yuvFeeder == NULL ) {
  835. LOG(SF_LOG_ERR, "YuvFeeder_Create error");
  836. goto ERR_ENC_OPEN;
  837. }
  838. LOG(SF_LOG_INFO, "Enc Start :\n");
  839. Warp_DisplayEncodedInformation(pImp, *pHandle, STD_HEVC, 0, NULL, 0, 0);
  840. while(1){
  841. if(!pImp->bThreadRunning){
  842. LOG(SF_LOG_DEBUG,"encoder thread end\n");
  843. break;
  844. }
  845. if(pImp->bPause){
  846. LOG(SF_LOG_DEBUG,"in pause\n");
  847. SF_SemaphoreWait(pImp->pauseSemaphore);
  848. LOG(SF_LOG_DEBUG,"out pause\n");
  849. }
  850. pInputBuffer = *(OMX_BUFFERHEADERTYPE**)SF_Queue_Dequeue_Block(pImp->EmptyQueue);
  851. if(pInputBuffer == NULL){
  852. pImp->bThreadRunning = 0;
  853. LOG(SF_LOG_DEBUG, "null input buffer,end\n");
  854. break;
  855. }
  856. if(!pInputBuffer->nFilledLen){
  857. LOG(SF_LOG_DEBUG, "end of stream,end\n");
  858. pImp->bThreadRunning = 0;
  859. EmptyBufferDone(pSfOMXComponent, pInputBuffer);
  860. pOutputBuffer = *(OMX_BUFFERHEADERTYPE**)SF_Queue_Dequeue_Block(pImp->FillQueue);
  861. pOutputBuffer->nFilledLen = 0;
  862. pOutputBuffer->nFlags = OMX_BUFFERFLAG_EOS;
  863. FillBufferDone(pSfOMXComponent, pOutputBuffer);
  864. break;
  865. }else if(pInputBuffer->nFlags == OMX_BUFFERFLAG_EOS){
  866. LOG(SF_LOG_DEBUG, "end of stream,end\n");
  867. //encParam.srcEndFlag = 1;
  868. geteos = 1;
  869. }
  870. LOG(SF_LOG_DEBUG, "get input buff %p index %d\r\n", pInputBuffer->pBuffer,
  871. ((SF_OMX_BUF_INFO*)pInputBuffer->pInputPortPrivate)->index);
  872. srcFrameIdx = (frameIdx%fbAllocInfo.num);
  873. encParam.srcIdx = srcFrameIdx;
  874. ret = Warp_YuvFeeder_Feed(pImp, yuvFeeder, coreIdx, &pImp->fbSrc[srcFrameIdx], pEncOP->picWidth, pEncOP->picHeight, pInputBuffer->pBuffer);
  875. LOG(SF_LOG_DEBUG,"feed src %p size %ld fbSrcIdx %d ret %d\n",pInputBuffer->pBuffer, pInputBuffer->nFilledLen, srcFrameIdx, ret);
  876. if ( ret == 0 ) {
  877. encParam.srcEndFlag = 1; // when there is no more source image to be encoded, srcEndFlag should be set 1. because of encoding delay for WAVE420
  878. }
  879. EmptyBufferDone(pSfOMXComponent, pInputBuffer);
  880. if ( encParam.srcEndFlag != 1) {
  881. pImp->fbSrc[srcFrameIdx].srcBufState = SRC_BUFFER_USE_ENCODE;
  882. encParam.sourceFrame = &pImp->fbSrc[srcFrameIdx];
  883. encParam.sourceFrame->sourceLBurstEn = 0;///???
  884. }
  885. bsQueueIndex = (bsQueueIndex+1)%bsBufferCount;
  886. encParam.picStreamBufferAddr = pImp->vbStream[bsQueueIndex].phys_addr; // can set the newly allocated buffer.
  887. encParam.picStreamBufferSize = pEncOP->bitstreamBufferSize;
  888. if (pEncConfig->useAsLongtermPeriod > 0 && pEncConfig->refLongtermPeriod > 0) {
  889. encParam.useCurSrcAsLongtermPic = (frameIdx % pEncConfig->useAsLongtermPeriod) == 0 ? 1 : 0;
  890. encParam.useLongtermRef = (frameIdx % pEncConfig->refLongtermPeriod) == 0 ? 1 : 0;
  891. }
  892. // Start encoding a frame.
  893. frameIdx++;
  894. LOG(SF_LOG_DEBUG, "vpu start one frame\r\n");
  895. ret = Warp_VPU_EncStartOneFrame(pImp,*pHandle, &encParam);
  896. if(ret) {
  897. LOG(SF_LOG_ERR, "VPU_EncStartOneFrame failed Error code is 0x%x \n", ret );
  898. Warp_LeaveLock(pImp, coreIdx);
  899. goto ERR_ENC_OPEN;
  900. }
  901. timeoutCount = 0;
  902. while(1){
  903. int_reason = Warp_VPU_WaitInterrupt(pImp, coreIdx, VPU_WAIT_TIME_OUT);
  904. if (int_reason == -1) {
  905. if (interruptTimeout > 0 && timeoutCount*VPU_WAIT_TIME_OUT > interruptTimeout) {
  906. LOG(SF_LOG_ERR, "Error : encoder timeout happened\n");
  907. Warp_PrintVpuStatus(pImp, coreIdx, productId);
  908. Warp_VPU_SWReset(pImp, coreIdx, SW_RESET_SAFETY, *pHandle);
  909. break;
  910. }
  911. int_reason = 0;
  912. timeoutCount++;
  913. }
  914. if (int_reason & (1<<INT_BIT_BIT_BUF_FULL)) {
  915. LOG(SF_LOG_WARN,"INT_BIT_BIT_BUF_FULL \n");
  916. pOutputBuffer = *(OMX_BUFFERHEADERTYPE**)SF_Queue_Dequeue_Block(pImp->FillQueue);
  917. if(pOutputBuffer == NULL){
  918. pImp->bThreadRunning = 0;
  919. LOG(SF_LOG_DEBUG, "null output buffer,end\n");
  920. goto ERR_ENC_OPEN;
  921. }
  922. LOG(SF_LOG_DEBUG, "get output buff %p index %d\r\n", pOutputBuffer->pBuffer,
  923. ((SF_OMX_BUF_INFO*)pOutputBuffer->pOutputPortPrivate)->index);
  924. size = Warp_BufferStreamReader_Act(pImp, bsReader, pEncOP->bitstreamBuffer, pEncOP->bitstreamBufferSize, STREAM_READ_ALL_SIZE, pOutputBuffer->pBuffer, comparatorBitStream);
  925. if (size == 0) {
  926. LOG(SF_LOG_ERR, "BufferStreamReader acc err \r\n");
  927. goto ERR_ENC_OPEN;
  928. }
  929. pOutputBuffer->nFilledLen = size;
  930. pOutputBuffer->nFlags = 0;
  931. if (geteos)
  932. {
  933. pOutputBuffer->nFlags |= OMX_BUFFERFLAG_EOS;
  934. geteos = 0;
  935. }
  936. FillBufferDone(pSfOMXComponent, pOutputBuffer);
  937. }
  938. if (int_reason) {
  939. Warp_VPU_ClearInterrupt(pImp, coreIdx);
  940. if (int_reason & (1<<INT_WAVE_ENC_PIC)) {
  941. break;
  942. }
  943. }
  944. }
  945. ret = Warp_VPU_EncGetOutputInfo(pImp, *pHandle, &outputInfo);
  946. if (ret ) {
  947. LOG(SF_LOG_ERR, "VPU_EncGetOutputInfo failed Error code is 0x%x \n", ret );
  948. if (ret == RETCODE_STREAM_BUF_FULL) {
  949. LOG(SF_LOG_ERR, "RETCODE_STREAM_BUF_FULL\n");
  950. continue;
  951. }
  952. else if ( ret == RETCODE_MEMORY_ACCESS_VIOLATION || ret == RETCODE_CP0_EXCEPTION || ret == RETCODE_ACCESS_VIOLATION_HW)
  953. {
  954. Warp_EnterLock(pImp, coreIdx);
  955. Warp_PrintVpuStatus(pImp, coreIdx, productId);
  956. Warp_VPU_SWReset(pImp,coreIdx, SW_RESET_SAFETY, *pHandle);
  957. Warp_LeaveLock(pImp, coreIdx);
  958. } else {
  959. Warp_EnterLock(pImp, coreIdx);
  960. Warp_PrintVpuStatus(pImp, coreIdx, productId);
  961. Warp_VPU_SWReset(pImp, coreIdx, SW_RESET_SAFETY, *pHandle);
  962. Warp_LeaveLock(pImp, coreIdx);
  963. }
  964. LOG(SF_LOG_ERR, "something err \r\n");
  965. goto ERR_ENC_OPEN;
  966. }
  967. Warp_DisplayEncodedInformation(pImp, *pHandle, STD_HEVC, 0, &outputInfo, encParam.srcEndFlag , srcFrameIdx);
  968. if (outputInfo.bitstreamWrapAround == 1) {
  969. LOG(SF_LOG_WARN, "Warnning!! BitStream buffer wrap arounded. prepare more large buffer \n", ret );
  970. }
  971. if (outputInfo.bitstreamSize == 0 && outputInfo.reconFrameIndex >= 0) {
  972. LOG(SF_LOG_ERR, "ERROR!!! bitstreamsize = 0 \n");
  973. }
  974. if (pEncOP->lineBufIntEn == 0) {
  975. if (outputInfo.wrPtr < outputInfo.rdPtr)
  976. {
  977. LOG(SF_LOG_ERR, "wrptr < rdptr\n");
  978. goto ERR_ENC_OPEN;
  979. }
  980. }
  981. if ( outputInfo.bitstreamSize ) {
  982. pOutputBuffer = *(OMX_BUFFERHEADERTYPE**)SF_Queue_Dequeue_Block(pImp->FillQueue);
  983. if(pOutputBuffer == NULL){
  984. pImp->bThreadRunning = 0;
  985. LOG(SF_LOG_DEBUG, "null output buffer,end\n");
  986. goto ERR_ENC_OPEN;
  987. }
  988. LOG(SF_LOG_DEBUG, "get output buff %p index %d\r\n", pOutputBuffer->pBuffer,
  989. ((SF_OMX_BUF_INFO*)pOutputBuffer->pOutputPortPrivate)->index);
  990. Warp_EnterLock(pImp, coreIdx);
  991. size = Warp_BufferStreamReader_Act(pImp, bsReader, outputInfo.bitstreamBuffer, pEncOP->bitstreamBufferSize,
  992. outputInfo.bitstreamSize, pOutputBuffer->pBuffer,comparatorBitStream);
  993. Warp_LeaveLock(pImp, coreIdx);
  994. if (size == 0) {
  995. LOG(SF_LOG_ERR, "BufferStreamReader acc err \r\n");
  996. goto ERR_ENC_OPEN;
  997. }
  998. pOutputBuffer->nFilledLen = size;
  999. pOutputBuffer->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
  1000. if (outputInfo.reconFrameIndex == -1)
  1001. pOutputBuffer->nFlags |= OMX_BUFFERFLAG_EOS;
  1002. FillBufferDone(pSfOMXComponent, pOutputBuffer);
  1003. }
  1004. if (outputInfo.reconFrameIndex == -1) // end of encoding
  1005. {
  1006. LOG(SF_LOG_DEBUG, "end of coding\n");
  1007. break;
  1008. }
  1009. }
  1010. ERR_ENC_OPEN:
  1011. FlushBuffer(pSfOMXComponent,OMX_INPUT_PORT_INDEX);
  1012. FlushBuffer(pSfOMXComponent,OMX_OUTPUT_PORT_INDEX);
  1013. for (i = 0; i < regFrameBufCount; i++) {
  1014. if (pImp->vbReconFrameBuf[i].size > 0) {
  1015. Warp_vdi_free_dma_memory(pImp, coreIdx, &pImp->vbReconFrameBuf[i]);
  1016. }
  1017. }
  1018. for (i = 0; i < fbAllocInfo.num; i++) {
  1019. if (pImp->vbSourceFrameBuf[i].size > 0) {
  1020. Warp_vdi_free_dma_memory(pImp, coreIdx, &pImp->vbSourceFrameBuf[i]);
  1021. }
  1022. }
  1023. Warp_VPU_EncClose(pImp, *pHandle);
  1024. LOG(SF_LOG_DEBUG,"close vpu \r\n");
  1025. ERR_ENC_INIT:
  1026. for (i=0; i< bsBufferCount ; i++) {
  1027. if (pImp->vbStream[i].size){
  1028. Warp_vdi_free_dma_memory(pImp, coreIdx, &pImp->vbStream[i]);
  1029. }
  1030. }
  1031. pthread_exit(NULL);
  1032. }
  1033. static OMX_ERRORTYPE SF_OMX_ComponentTunnelRequest(
  1034. OMX_IN OMX_HANDLETYPE hComp,
  1035. OMX_IN OMX_U32 nPort,
  1036. OMX_IN OMX_HANDLETYPE hTunneledComp,
  1037. OMX_IN OMX_U32 nTunneledPort,
  1038. OMX_INOUT OMX_TUNNELSETUPTYPE *pTunnelSetup)
  1039. {
  1040. OMX_ERRORTYPE ret = OMX_ErrorNone;
  1041. (void) hComp;
  1042. (void) nPort;
  1043. (void) hTunneledComp;
  1044. (void) nTunneledPort;
  1045. (void) pTunnelSetup;
  1046. ret = OMX_ErrorTunnelingUnsupported;
  1047. goto EXIT;
  1048. EXIT:
  1049. return ret;
  1050. }
  1051. static void FlushBuffer(SF_OMX_COMPONENT *pSfOMXComponent, OMX_U32 nPortNumber)
  1052. {
  1053. SF_WAVE420L_IMPLEMEMT *pImp = (SF_WAVE420L_IMPLEMEMT *)pSfOMXComponent->componentImpl;
  1054. OMX_BUFFERHEADERTYPE *pOMXBuffer = NULL;
  1055. OMX_BUFFERHEADERTYPE **ppBuffer = NULL;
  1056. FunctionIn();
  1057. switch (nPortNumber)
  1058. {
  1059. case OMX_INPUT_PORT_INDEX:
  1060. while (OMX_TRUE)
  1061. {
  1062. ppBuffer = SF_Queue_Dequeue(pImp->EmptyQueue);
  1063. if (NULL == ppBuffer)
  1064. {
  1065. LOG(SF_LOG_INFO, "No more buffer in input port\r\n");
  1066. break;
  1067. }
  1068. pOMXBuffer = *ppBuffer;
  1069. if(!pOMXBuffer)
  1070. continue;
  1071. LOG(SF_LOG_INFO, "Flush Buffer %p\r\n", pOMXBuffer);
  1072. pOMXBuffer->nFlags = OMX_BUFFERFLAG_EOS;
  1073. EmptyBufferDone(pSfOMXComponent, pOMXBuffer);
  1074. }
  1075. break;
  1076. case OMX_OUTPUT_PORT_INDEX:
  1077. while (OMX_TRUE)
  1078. {
  1079. ppBuffer = SF_Queue_Dequeue(pImp->FillQueue);
  1080. if (NULL == ppBuffer)
  1081. {
  1082. LOG(SF_LOG_INFO, "No more buffer in output port\r\n");
  1083. break;
  1084. }
  1085. pOMXBuffer = *ppBuffer;
  1086. if(!pOMXBuffer)
  1087. continue;
  1088. LOG(SF_LOG_INFO, "Flush Buffer %p\r\n", pOMXBuffer);
  1089. pOMXBuffer->nFilledLen = 0;
  1090. pOMXBuffer->nFlags = OMX_BUFFERFLAG_EOS;
  1091. FillBufferDone(pSfOMXComponent, pOMXBuffer);
  1092. }
  1093. break;
  1094. default:
  1095. break;
  1096. }
  1097. FunctionOut();
  1098. }
  1099. static OMX_ERRORTYPE SF_OMX_EmptyThisBuffer(
  1100. OMX_IN OMX_HANDLETYPE hComponent,
  1101. OMX_IN OMX_BUFFERHEADERTYPE *pBuffer)
  1102. {
  1103. OMX_ERRORTYPE ret = OMX_ErrorNone;
  1104. FunctionIn();
  1105. if (hComponent == NULL || pBuffer == NULL)
  1106. {
  1107. ret = OMX_ErrorBadParameter;
  1108. goto EXIT;
  1109. }
  1110. OMX_COMPONENTTYPE *pOMXComponent = (OMX_COMPONENTTYPE *)hComponent;
  1111. SF_OMX_COMPONENT *pSfOMXComponent = (SF_OMX_COMPONENT *)pOMXComponent->pComponentPrivate;
  1112. SF_WAVE420L_IMPLEMEMT *pImp = (SF_WAVE420L_IMPLEMEMT *)pSfOMXComponent->componentImpl;
  1113. LOG(SF_LOG_DEBUG, "bufheader %p nFilledLen = %d, nFlags = %d, pBuffer = %p\r\n", pBuffer, pBuffer->nFilledLen, pBuffer->nFlags, pBuffer->pBuffer);
  1114. if (!pSfOMXComponent->portDefinition[OMX_INPUT_PORT_INDEX].bEnabled)
  1115. {
  1116. LOG(SF_LOG_INFO, "feed buffer when input port stop\r\n");
  1117. return OMX_ErrorIncorrectStateOperation;
  1118. }
  1119. if (pBuffer->nInputPortIndex != OMX_INPUT_PORT_INDEX)
  1120. {
  1121. LOG(SF_LOG_INFO, "Incorrect nInputPortIndex %d\r\n", pBuffer->nInputPortIndex);
  1122. return OMX_ErrorBadPortIndex;
  1123. }
  1124. ret = SF_Queue_Enqueue(pImp->EmptyQueue, &pBuffer);
  1125. EXIT:
  1126. FunctionOut();
  1127. return ret;
  1128. }
  1129. static OMX_ERRORTYPE SF_OMX_FillThisBuffer(
  1130. OMX_IN OMX_HANDLETYPE hComponent,
  1131. OMX_IN OMX_BUFFERHEADERTYPE *pBuffer)
  1132. {
  1133. OMX_ERRORTYPE ret = OMX_ErrorNone;
  1134. FunctionIn();
  1135. if (hComponent == NULL || pBuffer == NULL)
  1136. {
  1137. ret = OMX_ErrorBadParameter;
  1138. goto EXIT;
  1139. }
  1140. OMX_COMPONENTTYPE *pOMXComponent = (OMX_COMPONENTTYPE *)hComponent;
  1141. SF_OMX_COMPONENT *pSfOMXComponent = (SF_OMX_COMPONENT *)pOMXComponent->pComponentPrivate;
  1142. SF_WAVE420L_IMPLEMEMT *pImp = (SF_WAVE420L_IMPLEMEMT *)pSfOMXComponent->componentImpl;
  1143. LOG(SF_LOG_DEBUG, "bufheader %p nFilledLen = %d, nFlags = %d, pBuffer = %p\r\n", pBuffer, pBuffer->nFilledLen, pBuffer->nFlags, pBuffer->pBuffer);
  1144. if (!pSfOMXComponent->portDefinition[OMX_OUTPUT_PORT_INDEX].bEnabled)
  1145. {
  1146. LOG(SF_LOG_INFO, "feed buffer when output port stop\r\n");
  1147. return OMX_ErrorIncorrectStateOperation;
  1148. }
  1149. if (pBuffer->nOutputPortIndex != OMX_OUTPUT_PORT_INDEX)
  1150. {
  1151. LOG(SF_LOG_INFO, "Incorrect nOutputPortIndex %d\r\n", pBuffer->nOutputPortIndex);
  1152. return OMX_ErrorBadPortIndex;
  1153. }
  1154. ret = SF_Queue_Enqueue(pImp->FillQueue, &pBuffer);
  1155. EXIT:
  1156. FunctionOut();
  1157. return ret;
  1158. }
  1159. static OMX_ERRORTYPE SF_OMX_UseBuffer(
  1160. OMX_IN OMX_HANDLETYPE hComponent,
  1161. OMX_INOUT OMX_BUFFERHEADERTYPE **ppBufferHdr,
  1162. OMX_IN OMX_U32 nPortIndex,
  1163. OMX_IN OMX_PTR pAppPrivate,
  1164. OMX_IN OMX_U32 nSizeBytes,
  1165. OMX_IN OMX_U8 *pBuffer)
  1166. {
  1167. OMX_ERRORTYPE ret = OMX_ErrorNone;
  1168. OMX_BUFFERHEADERTYPE *temp_bufferHeader;
  1169. SF_OMX_BUF_INFO *pBufInfo;
  1170. OMX_COMPONENTTYPE *pOMXComponent = (OMX_COMPONENTTYPE *)hComponent;
  1171. SF_OMX_COMPONENT *pSfOMXComponent = pOMXComponent->pComponentPrivate;
  1172. OMX_U32 i;
  1173. FunctionIn();
  1174. temp_bufferHeader = (OMX_BUFFERHEADERTYPE *)malloc(sizeof(OMX_BUFFERHEADERTYPE));
  1175. if (temp_bufferHeader == NULL)
  1176. {
  1177. LOG(SF_LOG_ERR, "malloc fail\r\n");
  1178. return OMX_ErrorInsufficientResources;
  1179. }
  1180. pBufInfo = (SF_OMX_BUF_INFO *)malloc(sizeof(SF_OMX_BUF_INFO));
  1181. if (pBufInfo == NULL)
  1182. {
  1183. LOG(SF_LOG_ERR, "malloc fail\r\n");
  1184. free(temp_bufferHeader);
  1185. return OMX_ErrorInsufficientResources;
  1186. }
  1187. memset(temp_bufferHeader, 0, sizeof(OMX_BUFFERHEADERTYPE));
  1188. temp_bufferHeader->nAllocLen = nSizeBytes;
  1189. temp_bufferHeader->pAppPrivate = pAppPrivate;
  1190. temp_bufferHeader->pBuffer = pBuffer;
  1191. temp_bufferHeader->nSize = sizeof(OMX_BUFFERHEADERTYPE);
  1192. memcpy(&(temp_bufferHeader->nVersion), &(pSfOMXComponent->componentVersion), sizeof(OMX_VERSIONTYPE));
  1193. if(nPortIndex == OMX_INPUT_PORT_INDEX)
  1194. {
  1195. for (i = 0; i < MAX_BUFF_NUM; i++)
  1196. {
  1197. if (!pSfOMXComponent->pBufferArray[OMX_INPUT_PORT_INDEX][i])
  1198. {
  1199. pBufInfo->type = SF_BUFFER_DMA_EXTERNAL;
  1200. pBufInfo->index = i;
  1201. temp_bufferHeader->pInputPortPrivate = (OMX_PTR)pBufInfo;
  1202. temp_bufferHeader->nInputPortIndex = OMX_INPUT_PORT_INDEX;
  1203. pSfOMXComponent->pBufferArray[OMX_INPUT_PORT_INDEX][i] = temp_bufferHeader;
  1204. pSfOMXComponent->assignedBufferNum[OMX_INPUT_PORT_INDEX] ++;
  1205. if (pSfOMXComponent->assignedBufferNum[OMX_INPUT_PORT_INDEX] ==
  1206. pSfOMXComponent->portDefinition[OMX_INPUT_PORT_INDEX].nBufferCountActual)
  1207. {
  1208. pSfOMXComponent->portDefinition[OMX_INPUT_PORT_INDEX].bPopulated = OMX_TRUE;
  1209. SF_SemaphorePost(pSfOMXComponent->portSemaphore[OMX_INPUT_PORT_INDEX]);
  1210. }
  1211. break;
  1212. }
  1213. }
  1214. if (i == MAX_BUFF_NUM){
  1215. LOG(SF_LOG_ERR, "buffer array full\r\n");
  1216. free(temp_bufferHeader);
  1217. free(pBufInfo);
  1218. ret = OMX_ErrorInsufficientResources;
  1219. }
  1220. }
  1221. else if(nPortIndex == OMX_OUTPUT_PORT_INDEX)
  1222. {
  1223. for (i = 0; i < MAX_BUFF_NUM; i++)
  1224. {
  1225. if (!pSfOMXComponent->pBufferArray[OMX_OUTPUT_PORT_INDEX][i])
  1226. {
  1227. pBufInfo->type = SF_BUFFER_DMA_EXTERNAL;
  1228. pBufInfo->index = i;
  1229. temp_bufferHeader->pOutputPortPrivate = (OMX_PTR)pBufInfo;
  1230. temp_bufferHeader->nOutputPortIndex = OMX_OUTPUT_PORT_INDEX;
  1231. pSfOMXComponent->pBufferArray[OMX_OUTPUT_PORT_INDEX][i] = temp_bufferHeader;
  1232. pSfOMXComponent->assignedBufferNum[OMX_OUTPUT_PORT_INDEX] ++;
  1233. if (pSfOMXComponent->assignedBufferNum[OMX_OUTPUT_PORT_INDEX] ==
  1234. pSfOMXComponent->portDefinition[OMX_OUTPUT_PORT_INDEX].nBufferCountActual)
  1235. {
  1236. pSfOMXComponent->portDefinition[OMX_OUTPUT_PORT_INDEX].bPopulated = OMX_TRUE;
  1237. SF_SemaphorePost(pSfOMXComponent->portSemaphore[OMX_OUTPUT_PORT_INDEX]);
  1238. }
  1239. break;
  1240. }
  1241. }
  1242. if (i == MAX_BUFF_NUM){
  1243. LOG(SF_LOG_ERR, "buffer array full\r\n");
  1244. free(temp_bufferHeader);
  1245. free(pBufInfo);
  1246. ret = OMX_ErrorInsufficientResources;
  1247. }
  1248. }
  1249. *ppBufferHdr = temp_bufferHeader;
  1250. FunctionOut();
  1251. return ret;
  1252. }
  1253. static OMX_BOOL FillBufferDone(SF_OMX_COMPONENT *pSfOMXComponent, OMX_BUFFERHEADERTYPE *pBuffer)
  1254. {
  1255. SF_WAVE420L_IMPLEMEMT *pImp = (SF_WAVE420L_IMPLEMEMT *)pSfOMXComponent->componentImpl;
  1256. FunctionIn();
  1257. pBuffer->nTimeStamp = (pImp->tmpCounter*1000000)/pImp->tmpFramerate;
  1258. pImp->tmpCounter++;
  1259. LOG(SF_LOG_PERF, "OMX finish one buffer, address = %p, size = %d, nTimeStamp = %d, nFlags = %X\r\n",
  1260. pBuffer->pBuffer, pBuffer->nFilledLen, pBuffer->nTimeStamp, pBuffer->nFlags);
  1261. if (pSfOMXComponent->propagateMarkType.hMarkTargetComponent != NULL) {
  1262. LOG(SF_LOG_INFO, "Component propagate mark to output port\r\n");
  1263. pBuffer->hMarkTargetComponent = pSfOMXComponent->propagateMarkType.hMarkTargetComponent;
  1264. pBuffer->pMarkData = pSfOMXComponent->propagateMarkType.pMarkData;
  1265. pSfOMXComponent->propagateMarkType.hMarkTargetComponent = NULL;
  1266. pSfOMXComponent->propagateMarkType.pMarkData = NULL;
  1267. }
  1268. if (pSfOMXComponent->state == OMX_StatePause)
  1269. {
  1270. LOG(SF_LOG_INFO, "tmp store buf when pause\r\n");
  1271. SF_Queue_Enqueue(pImp->pauseQ, &pBuffer);
  1272. }
  1273. else{
  1274. if (pBuffer->nFlags & OMX_BUFFERFLAG_EOS)
  1275. {
  1276. pSfOMXComponent->callbacks->EventHandler(pSfOMXComponent->pOMXComponent, pSfOMXComponent->pAppData, OMX_EventBufferFlag,
  1277. OMX_OUTPUT_PORT_INDEX, pBuffer->nFlags, NULL);
  1278. }
  1279. pSfOMXComponent->callbacks->FillBufferDone(pSfOMXComponent->pOMXComponent, pSfOMXComponent->pAppData, pBuffer);
  1280. }
  1281. FunctionOut();
  1282. return OMX_TRUE;
  1283. }
  1284. static OMX_BOOL EmptyBufferDone(SF_OMX_COMPONENT *pSfOMXComponent, OMX_BUFFERHEADERTYPE *pBuffer)
  1285. {
  1286. FunctionIn();
  1287. LOG(SF_LOG_DEBUG, "EmptyBufferDone IN\r\n");
  1288. LOG(SF_LOG_PERF, "OMX empty one buffer, address = %p, size = %d, nTimeStamp = %d, nFlags = %X\r\n",
  1289. pBuffer->pBuffer, pBuffer->nFilledLen, pBuffer->nTimeStamp, pBuffer->nFlags);
  1290. if (pSfOMXComponent->markType[OMX_INPUT_PORT_INDEX].hMarkTargetComponent != NULL)
  1291. {
  1292. LOG(SF_LOG_INFO, "set Component mark %p\r\n", pSfOMXComponent->markType[OMX_INPUT_PORT_INDEX].hMarkTargetComponent);
  1293. pBuffer->hMarkTargetComponent = pSfOMXComponent->markType[OMX_INPUT_PORT_INDEX].hMarkTargetComponent;
  1294. pBuffer->pMarkData = pSfOMXComponent->markType[OMX_INPUT_PORT_INDEX].pMarkData;
  1295. pSfOMXComponent->markType[OMX_INPUT_PORT_INDEX].hMarkTargetComponent = NULL;
  1296. pSfOMXComponent->markType[OMX_INPUT_PORT_INDEX].pMarkData = NULL;
  1297. }
  1298. if (pBuffer->hMarkTargetComponent != NULL)
  1299. {
  1300. if (pBuffer->hMarkTargetComponent == pSfOMXComponent->pOMXComponent)
  1301. {
  1302. LOG(SF_LOG_INFO, "Component mark hit\r\n");
  1303. pSfOMXComponent->callbacks->EventHandler(pSfOMXComponent->pOMXComponent,
  1304. pSfOMXComponent->pAppData,
  1305. OMX_EventMark,
  1306. 0, 0, pBuffer->pMarkData);
  1307. } else {
  1308. LOG(SF_LOG_INFO, "Component propagate mark from input port\r\n");
  1309. pSfOMXComponent->propagateMarkType.hMarkTargetComponent = pBuffer->hMarkTargetComponent;
  1310. pSfOMXComponent->propagateMarkType.pMarkData = pBuffer->pMarkData;
  1311. }
  1312. }
  1313. pSfOMXComponent->callbacks->EmptyBufferDone(pSfOMXComponent->pOMXComponent, pSfOMXComponent->pAppData, pBuffer);
  1314. LOG(SF_LOG_DEBUG, "EmptyBufferDone OUT\r\n");
  1315. FunctionOut();
  1316. return OMX_TRUE;
  1317. }
  1318. static OMX_ERRORTYPE SF_OMX_AllocateBuffer(
  1319. OMX_IN OMX_HANDLETYPE hComponent,
  1320. OMX_INOUT OMX_BUFFERHEADERTYPE **ppBuffer,
  1321. OMX_IN OMX_U32 nPortIndex,
  1322. OMX_IN OMX_PTR pAppPrivate,
  1323. OMX_IN OMX_U32 nSizeBytes)
  1324. {
  1325. OMX_COMPONENTTYPE *pOMXComponent = (OMX_COMPONENTTYPE *)hComponent;
  1326. SF_OMX_COMPONENT *pSfOMXComponent = pOMXComponent->pComponentPrivate;
  1327. OMX_ERRORTYPE ret = OMX_ErrorNone;
  1328. OMX_BUFFERHEADERTYPE *temp_bufferHeader;
  1329. SF_OMX_BUF_INFO *pBufInfo;
  1330. OMX_U32 i;
  1331. FunctionIn();
  1332. if (nSizeBytes == 0)
  1333. {
  1334. LOG(SF_LOG_ERR, "nSizeBytes = %d\r\n", nSizeBytes);
  1335. return OMX_ErrorBadParameter;
  1336. }
  1337. temp_bufferHeader = (OMX_BUFFERHEADERTYPE *)malloc(sizeof(OMX_BUFFERHEADERTYPE));
  1338. if (temp_bufferHeader == NULL)
  1339. {
  1340. LOG(SF_LOG_ERR, "malloc fail\r\n");
  1341. return OMX_ErrorInsufficientResources;
  1342. }
  1343. memset(temp_bufferHeader, 0, sizeof(OMX_BUFFERHEADERTYPE));
  1344. pBufInfo = (SF_OMX_BUF_INFO *)malloc(sizeof(SF_OMX_BUF_INFO));
  1345. if (pBufInfo == NULL)
  1346. {
  1347. LOG(SF_LOG_ERR, "malloc fail\r\n");
  1348. free(temp_bufferHeader);
  1349. return OMX_ErrorInsufficientResources;
  1350. }
  1351. temp_bufferHeader->nAllocLen = nSizeBytes;
  1352. temp_bufferHeader->pAppPrivate = pAppPrivate;
  1353. temp_bufferHeader->nSize = sizeof(OMX_BUFFERHEADERTYPE);
  1354. memcpy(&(temp_bufferHeader->nVersion), &(pSfOMXComponent->componentVersion), sizeof(OMX_VERSIONTYPE));
  1355. if (nPortIndex == OMX_OUTPUT_PORT_INDEX)
  1356. {
  1357. for (i = 0; i < MAX_BUFF_NUM; i++)
  1358. {
  1359. if (!pSfOMXComponent->pBufferArray[OMX_OUTPUT_PORT_INDEX][i])
  1360. {
  1361. temp_bufferHeader->pBuffer = malloc(nSizeBytes);
  1362. memset(temp_bufferHeader->pBuffer, 0, nSizeBytes);
  1363. temp_bufferHeader->nOutputPortIndex = OMX_OUTPUT_PORT_INDEX;
  1364. pBufInfo->type = SF_BUFFER_NOMAL;
  1365. pBufInfo->index = i;
  1366. temp_bufferHeader->pOutputPortPrivate = (void*)pBufInfo;
  1367. LOG(SF_LOG_PERF, "alloc normal output buffer %d \r\n", i);
  1368. pSfOMXComponent->pBufferArray[OMX_OUTPUT_PORT_INDEX][i] = temp_bufferHeader;
  1369. pSfOMXComponent->assignedBufferNum[OMX_OUTPUT_PORT_INDEX]++;
  1370. if (pSfOMXComponent->assignedBufferNum[OMX_OUTPUT_PORT_INDEX] ==
  1371. pSfOMXComponent->portDefinition[OMX_OUTPUT_PORT_INDEX].nBufferCountActual)
  1372. {
  1373. pSfOMXComponent->portDefinition[OMX_OUTPUT_PORT_INDEX].bPopulated = OMX_TRUE;
  1374. SF_SemaphorePost(pSfOMXComponent->portSemaphore[OMX_OUTPUT_PORT_INDEX]);
  1375. }
  1376. break;
  1377. }
  1378. }
  1379. if (i == MAX_BUFF_NUM){
  1380. LOG(SF_LOG_ERR, "buffer array full\r\n");
  1381. temp_bufferHeader->pBuffer = NULL;
  1382. }
  1383. }
  1384. else if (nPortIndex == OMX_INPUT_PORT_INDEX)
  1385. {
  1386. for (i = 0; i < MAX_BUFF_NUM; i++)
  1387. {
  1388. if (!pSfOMXComponent->pBufferArray[OMX_INPUT_PORT_INDEX][i])
  1389. {
  1390. temp_bufferHeader->pBuffer = malloc(nSizeBytes);
  1391. memset(temp_bufferHeader->pBuffer, 0, nSizeBytes);
  1392. temp_bufferHeader->nInputPortIndex = OMX_INPUT_PORT_INDEX;
  1393. pBufInfo->type = SF_BUFFER_NOMAL;
  1394. pBufInfo->index = i;
  1395. temp_bufferHeader->pInputPortPrivate = (void*)pBufInfo;
  1396. LOG(SF_LOG_PERF, "alloc normal intput buffer %d \r\n", i);
  1397. pSfOMXComponent->pBufferArray[OMX_INPUT_PORT_INDEX][i] = temp_bufferHeader;
  1398. pSfOMXComponent->assignedBufferNum[OMX_INPUT_PORT_INDEX]++;
  1399. if (pSfOMXComponent->assignedBufferNum[OMX_INPUT_PORT_INDEX] ==
  1400. pSfOMXComponent->portDefinition[OMX_INPUT_PORT_INDEX].nBufferCountActual)
  1401. {
  1402. pSfOMXComponent->portDefinition[OMX_INPUT_PORT_INDEX].bPopulated = OMX_TRUE;
  1403. SF_SemaphorePost(pSfOMXComponent->portSemaphore[OMX_INPUT_PORT_INDEX]);
  1404. }
  1405. break;
  1406. }
  1407. }
  1408. if (i == MAX_BUFF_NUM){
  1409. LOG(SF_LOG_ERR, "buffer array full\r\n");
  1410. temp_bufferHeader->pBuffer = NULL;
  1411. }
  1412. }
  1413. if (temp_bufferHeader->pBuffer == NULL)
  1414. {
  1415. free(pBufInfo);
  1416. free(temp_bufferHeader);
  1417. LOG(SF_LOG_ERR, "malloc fail\r\n");
  1418. return OMX_ErrorInsufficientResources;
  1419. }
  1420. *ppBuffer = temp_bufferHeader;
  1421. LOG(SF_LOG_INFO, "nPortIndex = %d, buffheader %p Buffer address = %p, nFilledLen = %d, nSizeBytes = %d\r\n",
  1422. nPortIndex, temp_bufferHeader, temp_bufferHeader->pBuffer, temp_bufferHeader->nFilledLen, nSizeBytes);
  1423. FunctionOut();
  1424. return ret;
  1425. }
  1426. static OMX_ERRORTYPE SF_OMX_FreeBuffer(
  1427. OMX_IN OMX_HANDLETYPE hComponent,
  1428. OMX_IN OMX_U32 nPortIndex,
  1429. OMX_IN OMX_BUFFERHEADERTYPE *pBufferHdr)
  1430. {
  1431. SF_OMX_BUF_INFO *pBufInfo;
  1432. OMX_COMPONENTTYPE *pOMXComponent = (OMX_COMPONENTTYPE *)hComponent;
  1433. SF_OMX_COMPONENT *pSfOMXComponent = pOMXComponent->pComponentPrivate;
  1434. FunctionIn();
  1435. if(pBufferHdr == NULL)
  1436. return OMX_ErrorBadParameter;
  1437. if(nPortIndex >= OMX_PORT_MAX)
  1438. return OMX_ErrorBadParameter;
  1439. if(nPortIndex == OMX_INPUT_PORT_INDEX)
  1440. pBufInfo = pBufferHdr->pInputPortPrivate;
  1441. else if(nPortIndex == OMX_OUTPUT_PORT_INDEX)
  1442. pBufInfo = pBufferHdr->pOutputPortPrivate;
  1443. LOG(SF_LOG_INFO, "free header %p pBuffer address = %p on port %d\r\n",
  1444. pBufferHdr, pBufferHdr->pBuffer, nPortIndex);
  1445. if ((pSfOMXComponent->traningState != OMX_TransStateIdleToLoaded) &&
  1446. (pSfOMXComponent->portDefinition[nPortIndex].bEnabled))
  1447. {
  1448. LOG(SF_LOG_INFO, "port Unpopulated\r\n");
  1449. pSfOMXComponent->callbacks->EventHandler(pSfOMXComponent->pOMXComponent, pSfOMXComponent->pAppData,
  1450. OMX_EventError, OMX_ErrorPortUnpopulated, nPortIndex, NULL);
  1451. }
  1452. if(pBufInfo->type == SF_BUFFER_NOMAL)
  1453. {
  1454. if(pBufferHdr->pBuffer)
  1455. free(pBufferHdr->pBuffer);
  1456. }
  1457. pSfOMXComponent->pBufferArray[nPortIndex][pBufInfo->index] = NULL;
  1458. pSfOMXComponent->assignedBufferNum[nPortIndex]--;
  1459. LOG(SF_LOG_INFO, "remain %d buff port %d assgin\r\n",pSfOMXComponent->assignedBufferNum[nPortIndex], nPortIndex);
  1460. free(pBufferHdr);
  1461. free(pBufInfo);
  1462. if (pSfOMXComponent->assignedBufferNum[nPortIndex] == 0) {
  1463. LOG(SF_LOG_INFO, "unloadedResource signal set\r\n");
  1464. SF_SemaphorePost(pSfOMXComponent->portUnloadSemaphore[nPortIndex]);
  1465. pSfOMXComponent->portDefinition[nPortIndex].bPopulated = OMX_FALSE;
  1466. }
  1467. FunctionOut();
  1468. return OMX_ErrorNone;
  1469. }
  1470. static OMX_ERRORTYPE SF_OMX_GetParameter(
  1471. OMX_IN OMX_HANDLETYPE hComponent,
  1472. OMX_IN OMX_INDEXTYPE nParamIndex,
  1473. OMX_INOUT OMX_PTR ComponentParameterStructure)
  1474. {
  1475. OMX_ERRORTYPE ret = OMX_ErrorNone;
  1476. OMX_COMPONENTTYPE *pOMXComponent = (OMX_COMPONENTTYPE *)hComponent;
  1477. SF_OMX_COMPONENT *pSfOMXComponent = pOMXComponent->pComponentPrivate;
  1478. SF_WAVE420L_IMPLEMEMT *pImp = (SF_WAVE420L_IMPLEMEMT *)pSfOMXComponent->componentImpl;
  1479. OMX_PARAM_PORTDEFINITIONTYPE *pOutputPort = &pSfOMXComponent->portDefinition[OMX_OUTPUT_PORT_INDEX];
  1480. FunctionIn();
  1481. if (hComponent == NULL || ComponentParameterStructure == NULL)
  1482. {
  1483. ret = OMX_ErrorBadParameter;
  1484. goto EXIT;
  1485. }
  1486. LOG(SF_LOG_INFO, "Get parameter on index %X\r\n", nParamIndex);
  1487. switch ((OMX_U32)nParamIndex)
  1488. {
  1489. case OMX_IndexParamVideoInit:
  1490. {
  1491. OMX_PORT_PARAM_TYPE *portParam = (OMX_PORT_PARAM_TYPE *)ComponentParameterStructure;
  1492. portParam->nPorts = OMX_PORT_MAX;
  1493. portParam->nStartPortNumber = OMX_INPUT_PORT_INDEX;
  1494. break;
  1495. }
  1496. case OMX_IndexParamAudioInit:
  1497. case OMX_IndexParamImageInit:
  1498. case OMX_IndexParamOtherInit:
  1499. {
  1500. OMX_PORT_PARAM_TYPE *portParam = (OMX_PORT_PARAM_TYPE *)ComponentParameterStructure;
  1501. portParam->nPorts = 0;
  1502. portParam->nStartPortNumber = 0;
  1503. break;
  1504. }
  1505. case OMX_IndexParamVideoPortFormat:
  1506. {
  1507. OMX_VIDEO_PARAM_PORTFORMATTYPE *portFormat = (OMX_VIDEO_PARAM_PORTFORMATTYPE *)ComponentParameterStructure;
  1508. OMX_U32 index = portFormat->nIndex;
  1509. LOG(SF_LOG_INFO, "Get video port format at index %d\r\n", index);
  1510. if (portFormat->nPortIndex == OMX_OUTPUT_PORT_INDEX)
  1511. {
  1512. if (index > 0)
  1513. {
  1514. ret = OMX_ErrorNoMore;
  1515. break;
  1516. }
  1517. portFormat->eCompressionFormat = pSfOMXComponent->portDefinition[OMX_OUTPUT_PORT_INDEX].format.video.eCompressionFormat;
  1518. portFormat->eColorFormat = pSfOMXComponent->portDefinition[OMX_OUTPUT_PORT_INDEX].format.video.eColorFormat;
  1519. portFormat->xFramerate = pSfOMXComponent->portDefinition[OMX_OUTPUT_PORT_INDEX].format.video.xFramerate;
  1520. }
  1521. else if (portFormat->nPortIndex == OMX_INPUT_PORT_INDEX)
  1522. {
  1523. switch (index)
  1524. {
  1525. case 0:
  1526. portFormat->eCompressionFormat = OMX_VIDEO_CodingUnused;
  1527. portFormat->eColorFormat = OMX_COLOR_FormatYUV420Planar;
  1528. portFormat->xFramerate = pSfOMXComponent->portDefinition[OMX_INPUT_PORT_INDEX].format.video.xFramerate;
  1529. break;
  1530. case 1:
  1531. portFormat->eCompressionFormat = OMX_VIDEO_CodingUnused;
  1532. portFormat->eColorFormat = OMX_COLOR_FormatYUV420SemiPlanar;
  1533. portFormat->xFramerate = pSfOMXComponent->portDefinition[OMX_INPUT_PORT_INDEX].format.video.xFramerate;
  1534. break;
  1535. case 2:
  1536. portFormat->eCompressionFormat = OMX_VIDEO_CodingUnused;
  1537. portFormat->eColorFormat = OMX_COLOR_FormatYVU420SemiPlanar;
  1538. portFormat->xFramerate = pSfOMXComponent->portDefinition[OMX_INPUT_PORT_INDEX].format.video.xFramerate;
  1539. break;
  1540. default:
  1541. if (index > 0)
  1542. {
  1543. ret = OMX_ErrorNoMore;
  1544. }
  1545. break;
  1546. }
  1547. }
  1548. else
  1549. {
  1550. ret = OMX_ErrorBadPortIndex;
  1551. }
  1552. break;
  1553. }
  1554. case OMX_IndexParamVideoBitrate:
  1555. {
  1556. OMX_VIDEO_PARAM_BITRATETYPE *videoRateControl = (OMX_VIDEO_PARAM_BITRATETYPE *)ComponentParameterStructure;
  1557. OMX_U32 portIndex = videoRateControl->nPortIndex;
  1558. if ((portIndex != OMX_OUTPUT_PORT_INDEX)) {
  1559. ret = OMX_ErrorBadPortIndex;
  1560. goto EXIT;
  1561. }
  1562. else
  1563. {
  1564. videoRateControl->nTargetBitrate = pOutputPort->format.video.nBitrate;
  1565. }
  1566. LOG(SF_LOG_INFO, "Get nTargetBitrate = %u on port %d\r\n",videoRateControl->nTargetBitrate, videoRateControl->nPortIndex);
  1567. break;
  1568. }
  1569. case OMX_IndexParamVideoQuantization:
  1570. break;
  1571. case OMX_IndexParamPortDefinition:
  1572. {
  1573. OMX_PARAM_PORTDEFINITIONTYPE *pPortDefinition = (OMX_PARAM_PORTDEFINITIONTYPE *)ComponentParameterStructure;
  1574. OMX_U32 portIndex = pPortDefinition->nPortIndex;
  1575. if (portIndex < OMX_PORT_MAX)
  1576. {
  1577. memcpy(pPortDefinition, &pSfOMXComponent->portDefinition[portIndex], pPortDefinition->nSize);
  1578. LOG(SF_LOG_DEBUG, "Get parameter port %X\r\n",portIndex);
  1579. LOG_APPEND(SF_LOG_DEBUG, "width = %d, height = %d\r\n", pPortDefinition->format.video.nFrameWidth, pPortDefinition->format.video.nFrameHeight);
  1580. LOG_APPEND(SF_LOG_DEBUG, "eColorFormat = %d\r\n", pPortDefinition->format.video.eColorFormat);
  1581. LOG_APPEND(SF_LOG_DEBUG, "xFramerate = %d\r\n", pPortDefinition->format.video.xFramerate);
  1582. LOG_APPEND(SF_LOG_DEBUG, "bufferSize = %d\r\n",pPortDefinition->nBufferSize);
  1583. LOG_APPEND(SF_LOG_DEBUG, "Buffer count = %d\r\n", pPortDefinition->nBufferCountActual);
  1584. }
  1585. else
  1586. {
  1587. LOG(SF_LOG_INFO,"Bad port index %d\r\n",portIndex);
  1588. ret = OMX_ErrorBadPortIndex;
  1589. }
  1590. break;
  1591. }
  1592. case OMX_IndexParamVideoIntraRefresh:
  1593. break;
  1594. case OMX_IndexParamStandardComponentRole:
  1595. {
  1596. OMX_PARAM_COMPONENTROLETYPE *pComponentRole = (OMX_PARAM_COMPONENTROLETYPE *)ComponentParameterStructure;
  1597. strcpy((OMX_STRING)(pComponentRole->cRole), pSfOMXComponent->componentRule);
  1598. break;
  1599. }
  1600. case OMX_IndexParamVideoAvc:
  1601. break;
  1602. case OMX_IndexParamVideoHevc:
  1603. {
  1604. OMX_VIDEO_PARAM_HEVCTYPE *pDstHEVCComponent = (OMX_VIDEO_PARAM_HEVCTYPE *)ComponentParameterStructure;
  1605. OMX_U32 nPortIndex = pDstHEVCComponent->nPortIndex;
  1606. OMX_VIDEO_PARAM_HEVCTYPE *pSrcHEVCComponent = &pImp->HEVCComponent[nPortIndex];
  1607. LOG(SF_LOG_INFO, "Get nKeyFrameInterval = %d on port %d\r\n", pSrcHEVCComponent->nKeyFrameInterval, nPortIndex);
  1608. memcpy(pDstHEVCComponent, pSrcHEVCComponent, sizeof(OMX_VIDEO_PARAM_HEVCTYPE));
  1609. break;
  1610. }
  1611. case OMX_IndexParamVideoProfileLevelQuerySupported:
  1612. break;
  1613. case OMX_IndexParamVideoProfileLevelCurrent:
  1614. {
  1615. OMX_VIDEO_PARAM_PROFILELEVELTYPE *pParam = (OMX_VIDEO_PARAM_PROFILELEVELTYPE *)ComponentParameterStructure;
  1616. LOG(SF_LOG_INFO, "Get ProfileLevel on port %d\r\n", pParam->nPortIndex);
  1617. if (pParam->nPortIndex == OMX_OUTPUT_PORT_INDEX)
  1618. {
  1619. //TODO
  1620. pParam->eProfile = OMX_VIDEO_HEVCProfileMain;
  1621. pParam->eLevel = OMX_VIDEO_HEVCMainTierLevel5;
  1622. LOG(SF_LOG_INFO, "eProfile = OMX_VIDEO_HEVCProfileMain\r\n");
  1623. }
  1624. break;
  1625. }
  1626. case OMX_IndexParamCompBufferSupplier:
  1627. {
  1628. OMX_PARAM_BUFFERSUPPLIERTYPE *bufferSupplier = (OMX_PARAM_BUFFERSUPPLIERTYPE *)ComponentParameterStructure;
  1629. OMX_U32 portIndex = bufferSupplier->nPortIndex;
  1630. if (portIndex >= OMX_PORT_MAX)
  1631. ret = OMX_ErrorBadPortIndex;
  1632. break;
  1633. }
  1634. default:
  1635. {
  1636. ret = OMX_ErrorUnsupportedIndex;
  1637. break;
  1638. }
  1639. }
  1640. EXIT:
  1641. FunctionOut();
  1642. return ret;
  1643. }
  1644. static OMX_ERRORTYPE SF_OMX_SetParameter(
  1645. OMX_IN OMX_HANDLETYPE hComponent,
  1646. OMX_IN OMX_INDEXTYPE nIndex,
  1647. OMX_IN OMX_PTR ComponentParameterStructure)
  1648. {
  1649. OMX_ERRORTYPE ret = OMX_ErrorNone;
  1650. FunctionIn();
  1651. if (hComponent == NULL || ComponentParameterStructure == NULL)
  1652. {
  1653. ret = OMX_ErrorBadParameter;
  1654. goto EXIT;
  1655. }
  1656. OMX_COMPONENTTYPE *pOMXComponent = (OMX_COMPONENTTYPE *)hComponent;
  1657. SF_OMX_COMPONENT *pSfOMXComponent = pOMXComponent->pComponentPrivate;
  1658. SF_WAVE420L_IMPLEMEMT *pImp = (SF_WAVE420L_IMPLEMEMT *)pSfOMXComponent->componentImpl;
  1659. OMX_PARAM_PORTDEFINITIONTYPE *pInputPort = &pSfOMXComponent->portDefinition[OMX_INPUT_PORT_INDEX];
  1660. OMX_PARAM_PORTDEFINITIONTYPE *pOutputPort = &pSfOMXComponent->portDefinition[OMX_OUTPUT_PORT_INDEX];
  1661. if (pSfOMXComponent == NULL)
  1662. {
  1663. ret = OMX_ErrorBadParameter;
  1664. goto EXIT;
  1665. }
  1666. LOG(SF_LOG_INFO, "Set parameter on index %X\r\n", nIndex);
  1667. switch ((OMX_U32)nIndex)
  1668. {
  1669. case OMX_IndexParamPortDefinition:
  1670. {
  1671. OMX_PARAM_PORTDEFINITIONTYPE *pPortDefinition = (OMX_PARAM_PORTDEFINITIONTYPE *)ComponentParameterStructure;
  1672. OMX_U32 portIndex = pPortDefinition->nPortIndex;
  1673. OMX_U32 width = pPortDefinition->format.video.nFrameWidth;
  1674. OMX_U32 height = pPortDefinition->format.video.nFrameHeight;
  1675. OMX_U32 nBitrate = pPortDefinition->format.video.nBitrate;
  1676. OMX_U32 xFramerate = pPortDefinition->format.video.xFramerate;
  1677. OMX_U32 bufFrameWidth, bufFrameHeight, bufFrameStride;
  1678. OMX_U32 nBufferCountActual = pPortDefinition->nBufferCountActual;
  1679. LOG(SF_LOG_INFO, "Set width = %d, height = %d, xFramerate = %d, nBitrate = %d, nBufferCountActual = %d on port %d\r\n",
  1680. width, height, xFramerate, nBitrate, nBufferCountActual, portIndex);
  1681. OMX_COLOR_FORMATTYPE eColorFormat = pPortDefinition->format.video.eColorFormat;
  1682. if (xFramerate == 0)
  1683. {
  1684. LOG(SF_LOG_ERR, "Fail to set xFramerate = %d\r\n", xFramerate);
  1685. return OMX_ErrorBadParameter;
  1686. }
  1687. pImp->tmpFramerate = xFramerate;
  1688. if (portIndex == OMX_INPUT_PORT_INDEX)
  1689. {
  1690. switch (eColorFormat)
  1691. {
  1692. case OMX_COLOR_FormatYUV420Planar:
  1693. pImp->encConfig.cbcrInterleave = OMX_FALSE;
  1694. pImp->encConfig.nv21 = OMX_FALSE;
  1695. pImp->encConfig.srcFormat = FORMAT_420;
  1696. pImp->encConfig.packedFormat = NOT_PACKED;
  1697. break;
  1698. case OMX_COLOR_FormatYUV420SemiPlanar:
  1699. pImp->encConfig.cbcrInterleave = OMX_TRUE;
  1700. pImp->encConfig.nv21 = OMX_FALSE;
  1701. pImp->encConfig.srcFormat = FORMAT_420;
  1702. pImp->encConfig.packedFormat = NOT_PACKED;
  1703. break;
  1704. case OMX_COLOR_FormatYVU420SemiPlanar:
  1705. pImp->encConfig.cbcrInterleave = OMX_TRUE;
  1706. pImp->encConfig.nv21 = OMX_TRUE;
  1707. pImp->encConfig.srcFormat = FORMAT_420;
  1708. pImp->encConfig.packedFormat = NOT_PACKED;
  1709. break;
  1710. default:
  1711. ret = OMX_ErrorBadParameter;
  1712. break;
  1713. }
  1714. if(ret)
  1715. return ret;
  1716. memcpy(pInputPort, pPortDefinition, pPortDefinition->nSize);
  1717. pImp->encConfig.picWidth = width;
  1718. pImp->encConfig.picHeight = height;
  1719. bufFrameWidth = ((width+7)&~7); // width = 8-aligned (CU unit)
  1720. bufFrameHeight = ((height+7)&~7); // height = 8-aligned (CU unit)
  1721. bufFrameStride = Warp_CalcStride(pImp, bufFrameWidth, bufFrameHeight, pImp->encConfig.srcFormat,
  1722. pImp->encConfig.cbcrInterleave, LINEAR_FRAME_MAP, FALSE);
  1723. pInputPort->nBufferSize = Warp_VPU_GetFrameBufSize(pImp, pImp->coreIdx, bufFrameStride, bufFrameHeight,
  1724. LINEAR_FRAME_MAP, pImp->encConfig.srcFormat, pImp->encConfig.cbcrInterleave, NULL);
  1725. LOG(SF_LOG_INFO, "alloc buff size %d on port %d\r\n",
  1726. pInputPort->nBufferSize, portIndex);
  1727. }
  1728. else if (portIndex == OMX_OUTPUT_PORT_INDEX)
  1729. {
  1730. memcpy(pOutputPort, pPortDefinition, pPortDefinition->nSize);
  1731. pOutputPort->format.video.nStride = width;
  1732. pOutputPort->format.video.nSliceHeight = height;
  1733. pOutputPort->nBufferSize = width * height * 2;
  1734. }
  1735. else
  1736. {
  1737. LOG(SF_LOG_INFO,"Bad port index %d\r\n",portIndex);
  1738. ret = OMX_ErrorBadPortIndex;
  1739. }
  1740. break;
  1741. }
  1742. case OMX_IndexParamVideoPortFormat:
  1743. {
  1744. OMX_VIDEO_PARAM_PORTFORMATTYPE *portFormat = (OMX_VIDEO_PARAM_PORTFORMATTYPE *)ComponentParameterStructure;
  1745. OMX_COLOR_FORMATTYPE eColorFormat = portFormat->eColorFormat;
  1746. OMX_U32 nPortIndex = portFormat->nPortIndex;
  1747. OMX_PARAM_PORTDEFINITIONTYPE *pPort = &pSfOMXComponent->portDefinition[nPortIndex];
  1748. LOG(SF_LOG_INFO, "Set eColorFormat to %d\r\n", eColorFormat);
  1749. if (nPortIndex == OMX_OUTPUT_PORT_INDEX)
  1750. {
  1751. if (portFormat->eColorFormat != pPort->format.video.eColorFormat ||
  1752. portFormat->eCompressionFormat != pPort->format.video.eCompressionFormat)
  1753. {
  1754. return OMX_ErrorBadParameter;
  1755. }
  1756. }
  1757. else if (nPortIndex == OMX_INPUT_PORT_INDEX)
  1758. {
  1759. if (portFormat->eCompressionFormat != pPort->format.video.eCompressionFormat)
  1760. {
  1761. return OMX_ErrorBadParameter;
  1762. }
  1763. if (portFormat->xFramerate == 0)
  1764. {
  1765. return OMX_ErrorBadParameter;
  1766. }
  1767. switch (portFormat->eColorFormat)
  1768. {
  1769. case OMX_COLOR_FormatYUV420Planar: //I420
  1770. pImp->encConfig.cbcrInterleave = OMX_FALSE;
  1771. pImp->encConfig.nv21 = OMX_FALSE;
  1772. pImp->encConfig.srcFormat = FORMAT_420;
  1773. pImp->encConfig.packedFormat = NOT_PACKED;
  1774. pPort->format.video.xFramerate = portFormat->xFramerate;
  1775. break;
  1776. case OMX_COLOR_FormatYUV420SemiPlanar: //NV12
  1777. pImp->encConfig.cbcrInterleave = OMX_TRUE;
  1778. pImp->encConfig.nv21 = OMX_FALSE;
  1779. pImp->encConfig.srcFormat = FORMAT_420;
  1780. pImp->encConfig.packedFormat = NOT_PACKED;
  1781. pPort->format.video.xFramerate = portFormat->xFramerate;
  1782. break;
  1783. case OMX_COLOR_FormatYVU420SemiPlanar: //NV21
  1784. pImp->encConfig.cbcrInterleave = OMX_TRUE;
  1785. pImp->encConfig.nv21 = OMX_TRUE;
  1786. pImp->encConfig.srcFormat = FORMAT_420;
  1787. pImp->encConfig.packedFormat = NOT_PACKED;
  1788. pPort->format.video.xFramerate = portFormat->xFramerate;
  1789. break;
  1790. default:
  1791. LOG(SF_LOG_ERR, "Error to set parameter: %d, only nv12 nv21 i420 supported\r\n", portFormat->eColorFormat);
  1792. return OMX_ErrorBadParameter;
  1793. break;
  1794. }
  1795. if(!ret){
  1796. pPort->format.video.eColorFormat = eColorFormat;
  1797. }
  1798. }
  1799. else
  1800. {
  1801. return OMX_ErrorBadPortIndex;
  1802. }
  1803. break;
  1804. }
  1805. case OMX_IndexParamVideoBitrate:
  1806. {
  1807. OMX_VIDEO_PARAM_BITRATETYPE *videoRateControl = (OMX_VIDEO_PARAM_BITRATETYPE *)ComponentParameterStructure;
  1808. OMX_U32 portIndex = videoRateControl->nPortIndex;
  1809. LOG(SF_LOG_INFO, "Set nTargetBitrate = %u on port %d\r\n",videoRateControl->nTargetBitrate, portIndex);
  1810. if ((portIndex != 1)) {
  1811. ret = OMX_ErrorBadPortIndex;
  1812. goto EXIT;
  1813. }
  1814. else
  1815. {
  1816. pOutputPort->format.video.nBitrate = videoRateControl->nTargetBitrate;
  1817. }
  1818. ret = OMX_ErrorNone;
  1819. break;
  1820. }
  1821. case OMX_IndexConfigVideoAVCIntraPeriod:
  1822. {
  1823. break;
  1824. }
  1825. case OMX_IndexParamVideoAvc:
  1826. {
  1827. break;
  1828. }
  1829. case OMX_IndexParamVideoHevc:
  1830. {
  1831. OMX_VIDEO_PARAM_HEVCTYPE *pSrcHEVCComponent= (OMX_VIDEO_PARAM_HEVCTYPE *)ComponentParameterStructure;
  1832. OMX_U32 nPortIndex = pSrcHEVCComponent->nPortIndex;
  1833. OMX_VIDEO_PARAM_HEVCTYPE *pDstHEVCComponent = &pImp->HEVCComponent[nPortIndex];
  1834. OMX_U32 nKeyFrameInterval = pSrcHEVCComponent->nKeyFrameInterval;
  1835. LOG(SF_LOG_INFO, "Set nKeyFrameInterval = %d on port %d\r\n", nKeyFrameInterval, nPortIndex);
  1836. if (nKeyFrameInterval > 1024)
  1837. {
  1838. LOG(SF_LOG_ERR, "Set nKeyFrameInterval = %d not supported!\r\n", nKeyFrameInterval);
  1839. ret = OMX_ErrorBadPortIndex;
  1840. goto EXIT;
  1841. }
  1842. memcpy(pDstHEVCComponent, pSrcHEVCComponent, sizeof(OMX_VIDEO_PARAM_HEVCTYPE));
  1843. break;
  1844. }
  1845. case OMX_IndexParamStandardComponentRole:
  1846. {
  1847. if ((pSfOMXComponent->state != OMX_StateLoaded) &&
  1848. (pSfOMXComponent->state != OMX_StateWaitForResources)) {
  1849. ret = OMX_ErrorIncorrectStateOperation;
  1850. }
  1851. break;
  1852. }
  1853. case OMX_IndexParamCompBufferSupplier:
  1854. {
  1855. OMX_PARAM_BUFFERSUPPLIERTYPE *bufferSupplier = (OMX_PARAM_BUFFERSUPPLIERTYPE *)ComponentParameterStructure;
  1856. OMX_U32 portIndex = bufferSupplier->nPortIndex;
  1857. if (portIndex >= OMX_PORT_MAX)
  1858. ret = OMX_ErrorBadPortIndex;
  1859. break;
  1860. }
  1861. case OMX_IndexParamVideoQuantization:
  1862. case OMX_IndexParamVideoIntraRefresh:
  1863. break;
  1864. default:
  1865. ret = OMX_ErrorUnsupportedIndex;
  1866. break;
  1867. }
  1868. EXIT:
  1869. FunctionOut();
  1870. return ret;
  1871. }
  1872. static OMX_ERRORTYPE SF_OMX_SendCommand(
  1873. OMX_IN OMX_HANDLETYPE hComponent,
  1874. OMX_IN OMX_COMMANDTYPE Cmd,
  1875. OMX_IN OMX_U32 nParam,
  1876. OMX_IN OMX_PTR pCmdData)
  1877. {
  1878. OMX_ERRORTYPE ret = OMX_ErrorNone;
  1879. OMX_COMPONENTTYPE *pOMXComponent = (OMX_COMPONENTTYPE *)hComponent;
  1880. SF_OMX_COMPONENT *pSfOMXComponent = pOMXComponent->pComponentPrivate;
  1881. SF_WAVE420L_IMPLEMEMT *pImp = (SF_WAVE420L_IMPLEMEMT *)pSfOMXComponent->componentImpl;
  1882. OMX_STATETYPE comCurrentState;
  1883. SF_OMX_CMD cmd;
  1884. FunctionIn();
  1885. if (hComponent == NULL || pSfOMXComponent == NULL)
  1886. {
  1887. ret = OMX_ErrorBadParameter;
  1888. goto EXIT;
  1889. }
  1890. LOG(SF_LOG_INFO, "cmd = %X, nParam = %X\r\n", Cmd, nParam);
  1891. comCurrentState = pSfOMXComponent->state;
  1892. if (comCurrentState == OMX_StateInvalid) {
  1893. return OMX_ErrorInvalidState;
  1894. }
  1895. switch (Cmd)
  1896. {
  1897. case OMX_CommandStateSet:
  1898. {
  1899. if ((nParam == OMX_StateLoaded) &&
  1900. (pSfOMXComponent->state == OMX_StateIdle))
  1901. {
  1902. pSfOMXComponent->traningState = OMX_TransStateIdleToLoaded;
  1903. }
  1904. else if ((nParam == OMX_StateIdle) &&
  1905. (pSfOMXComponent->state == OMX_StateLoaded))
  1906. {
  1907. pSfOMXComponent->traningState = OMX_TransStateLoadedToIdle;
  1908. }
  1909. }
  1910. break;
  1911. case OMX_CommandFlush:
  1912. {
  1913. if (nParam >= 2 && nParam != OMX_ALL)
  1914. return OMX_ErrorBadPortIndex;
  1915. }
  1916. break;
  1917. case OMX_CommandMarkBuffer:
  1918. {
  1919. if (nParam >= 2)
  1920. return OMX_ErrorBadPortIndex;
  1921. if ((pSfOMXComponent->state != OMX_StateExecuting) &&
  1922. (pSfOMXComponent->state != OMX_StatePause))
  1923. return OMX_ErrorIncorrectStateOperation;
  1924. }
  1925. break;
  1926. case OMX_CommandPortDisable:
  1927. {
  1928. if (nParam >= 2 && nParam != OMX_ALL)
  1929. return OMX_ErrorBadPortIndex;
  1930. if (nParam == OMX_ALL)
  1931. {
  1932. for (int i = 0; i < 2; i++)
  1933. {
  1934. if(pSfOMXComponent->portDefinition[i].bEnabled == OMX_FALSE)
  1935. return OMX_ErrorIncorrectStateOperation;
  1936. }
  1937. for (int i = 0; i < 2; i++)
  1938. {
  1939. pSfOMXComponent->portDefinition[i].bEnabled = OMX_FALSE;
  1940. }
  1941. }
  1942. else
  1943. {
  1944. if (pSfOMXComponent->portDefinition[nParam].bEnabled == OMX_FALSE)
  1945. return OMX_ErrorIncorrectStateOperation;
  1946. pSfOMXComponent->portDefinition[nParam].bEnabled = OMX_FALSE;
  1947. }
  1948. }
  1949. break;
  1950. case OMX_CommandPortEnable:
  1951. {
  1952. if (nParam >= 2 && nParam != OMX_ALL)
  1953. return OMX_ErrorBadPortIndex;
  1954. if (nParam == OMX_ALL)
  1955. {
  1956. for (int i = 0; i < 2; i++)
  1957. {
  1958. if(pSfOMXComponent->portDefinition[i].bEnabled == OMX_TRUE)
  1959. return OMX_ErrorIncorrectStateOperation;
  1960. }
  1961. for (int i = 0; i < 2; i++)
  1962. {
  1963. pSfOMXComponent->portDefinition[i].bEnabled = OMX_TRUE;
  1964. }
  1965. }
  1966. else
  1967. {
  1968. if (pSfOMXComponent->portDefinition[nParam].bEnabled == OMX_TRUE)
  1969. return OMX_ErrorIncorrectStateOperation;
  1970. pSfOMXComponent->portDefinition[nParam].bEnabled = OMX_TRUE;
  1971. }
  1972. }
  1973. break;
  1974. default:
  1975. break;
  1976. }
  1977. cmd.Cmd = Cmd;
  1978. cmd.nParam = nParam;
  1979. cmd.pCmdData = pCmdData;
  1980. ret = SF_Queue_Enqueue(pImp->CmdQueue, &cmd);
  1981. EXIT:
  1982. FunctionOut();
  1983. return ret;
  1984. }
  1985. static OMX_ERRORTYPE SF_OMX_GetState(
  1986. OMX_IN OMX_HANDLETYPE hComponent,
  1987. OMX_OUT OMX_STATETYPE *pState)
  1988. {
  1989. OMX_ERRORTYPE ret = OMX_ErrorNone;
  1990. OMX_COMPONENTTYPE *pOMXComponent = (OMX_COMPONENTTYPE *)hComponent;
  1991. SF_OMX_COMPONENT *pSfOMXComponent = pOMXComponent->pComponentPrivate;
  1992. FunctionIn();
  1993. *pState = pSfOMXComponent->state;
  1994. FunctionOut();
  1995. return ret;
  1996. }
  1997. static OMX_ERRORTYPE SF_OMX_GetConfig(
  1998. OMX_HANDLETYPE hComponent,
  1999. OMX_INDEXTYPE nIndex,
  2000. OMX_PTR pComponentConfigStructure)
  2001. {
  2002. OMX_ERRORTYPE ret = OMX_ErrorNone;
  2003. FunctionIn();
  2004. if (hComponent == NULL)
  2005. {
  2006. ret = OMX_ErrorBadParameter;
  2007. goto EXIT;
  2008. }
  2009. switch (nIndex)
  2010. {
  2011. case OMX_IndexConfigVideoBitrate:
  2012. break;
  2013. case OMX_IndexConfigVideoFramerate:
  2014. break;
  2015. default:
  2016. break;
  2017. }
  2018. EXIT:
  2019. FunctionOut();
  2020. return ret;
  2021. }
  2022. static OMX_ERRORTYPE SF_OMX_SetConfig(
  2023. OMX_HANDLETYPE hComponent,
  2024. OMX_INDEXTYPE nIndex,
  2025. OMX_PTR pComponentConfigStructure)
  2026. {
  2027. OMX_ERRORTYPE ret = OMX_ErrorNone;
  2028. FunctionIn();
  2029. if (hComponent == NULL)
  2030. {
  2031. ret = OMX_ErrorBadParameter;
  2032. goto EXIT;
  2033. }
  2034. switch ((OMX_U32)nIndex)
  2035. {
  2036. case OMX_IndexConfigVideoBitrate:
  2037. break;
  2038. case OMX_IndexConfigVideoFramerate:
  2039. break;
  2040. case OMX_IndexConfigVideoIntraVOPRefresh:
  2041. break;
  2042. default:
  2043. break;
  2044. }
  2045. EXIT:
  2046. FunctionOut();
  2047. return ret;
  2048. }
  2049. static OMX_ERRORTYPE SF_OMX_GetComponentVersion(
  2050. OMX_IN OMX_HANDLETYPE hComponent,
  2051. OMX_OUT OMX_STRING pComponentName,
  2052. OMX_OUT OMX_VERSIONTYPE *pComponentVersion,
  2053. OMX_OUT OMX_VERSIONTYPE *pSpecVersion,
  2054. OMX_OUT OMX_UUIDTYPE *pComponentUUID)
  2055. {
  2056. OMX_ERRORTYPE ret = OMX_ErrorNone;
  2057. OMX_COMPONENTTYPE *pOMXComponent = NULL;
  2058. SF_OMX_COMPONENT *pSfOMXComponent = NULL;
  2059. OMX_U32 compUUID[4];
  2060. FunctionIn();
  2061. /* check parameters */
  2062. if (hComponent == NULL ||
  2063. pComponentName == NULL || pComponentVersion == NULL ||
  2064. pSpecVersion == NULL || pComponentUUID == NULL) {
  2065. ret = OMX_ErrorBadParameter;
  2066. goto EXIT;
  2067. }
  2068. pOMXComponent = (OMX_COMPONENTTYPE *)hComponent;
  2069. if (pOMXComponent->pComponentPrivate == NULL) {
  2070. ret = OMX_ErrorBadParameter;
  2071. goto EXIT;
  2072. }
  2073. pSfOMXComponent = (SF_OMX_COMPONENT *)pOMXComponent->pComponentPrivate;
  2074. if (pSfOMXComponent->state == OMX_StateInvalid) {
  2075. ret = OMX_ErrorInvalidState;
  2076. goto EXIT;
  2077. }
  2078. strcpy(pComponentName, pSfOMXComponent->componentName);
  2079. memcpy(pComponentVersion, &(pSfOMXComponent->componentVersion), sizeof(OMX_VERSIONTYPE));
  2080. memcpy(pSpecVersion, &(pSfOMXComponent->specVersion), sizeof(OMX_VERSIONTYPE));
  2081. /* Fill UUID with handle address, PID and UID.
  2082. * This should guarantee uiniqness */
  2083. compUUID[0] = (OMX_U32)getpid();
  2084. compUUID[1] = (OMX_U32)getpid();
  2085. compUUID[2] = (OMX_U32)getpid();
  2086. compUUID[3] = (OMX_U32)getpid();
  2087. //compUUID[1] = getpid();
  2088. //compUUID[2] = getuid();
  2089. memcpy(*pComponentUUID, compUUID, 4 * sizeof(*compUUID));
  2090. ret = OMX_ErrorNone;
  2091. EXIT:
  2092. FunctionOut();
  2093. return ret;
  2094. }
  2095. static OMX_U32 nInstance = 0;
  2096. static OMX_ERRORTYPE SF_OMX_ComponentConstructor(SF_OMX_COMPONENT *pSfOMXComponent)
  2097. {
  2098. OMX_ERRORTYPE ret = OMX_ErrorNone;
  2099. FunctionIn();
  2100. if (nInstance >= 1)
  2101. {
  2102. ret = OMX_ErrorInsufficientResources;
  2103. goto EXIT;
  2104. }
  2105. nInstance++;
  2106. ret = WaveOmxInit(pSfOMXComponent);
  2107. if (ret != OMX_ErrorNone)
  2108. {
  2109. nInstance--;
  2110. goto EXIT;
  2111. }
  2112. pSfOMXComponent->pOMXComponent->UseBuffer = &SF_OMX_UseBuffer;
  2113. pSfOMXComponent->pOMXComponent->AllocateBuffer = &SF_OMX_AllocateBuffer;
  2114. pSfOMXComponent->pOMXComponent->EmptyThisBuffer = &SF_OMX_EmptyThisBuffer;
  2115. pSfOMXComponent->pOMXComponent->FillThisBuffer = &SF_OMX_FillThisBuffer;
  2116. pSfOMXComponent->pOMXComponent->FreeBuffer = &SF_OMX_FreeBuffer;
  2117. pSfOMXComponent->pOMXComponent->ComponentTunnelRequest = &SF_OMX_ComponentTunnelRequest;
  2118. pSfOMXComponent->pOMXComponent->GetParameter = &SF_OMX_GetParameter;
  2119. pSfOMXComponent->pOMXComponent->SetParameter = &SF_OMX_SetParameter;
  2120. pSfOMXComponent->pOMXComponent->GetConfig = &SF_OMX_GetConfig;
  2121. pSfOMXComponent->pOMXComponent->SetConfig = &SF_OMX_SetConfig;
  2122. pSfOMXComponent->pOMXComponent->SendCommand = &SF_OMX_SendCommand;
  2123. pSfOMXComponent->pOMXComponent->GetState = &SF_OMX_GetState;
  2124. pSfOMXComponent->pOMXComponent->GetComponentVersion = &SF_OMX_GetComponentVersion;
  2125. // pSfOMXComponent->pOMXComponent->GetExtensionIndex = &SF_OMX_GetExtensionIndex;
  2126. // pSfOMXComponent->pOMXComponent->ComponentRoleEnum = &SF_OMX_ComponentRoleEnum;
  2127. // pSfOMXComponent->pOMXComponent->ComponentDeInit = &SF_OMX_ComponentDeInit;
  2128. SetDefaultConfig(pSfOMXComponent);
  2129. EXIT:
  2130. FunctionOut();
  2131. return ret;
  2132. }
  2133. static OMX_ERRORTYPE SF_OMX_ComponentClear(SF_OMX_COMPONENT *pSfOMXComponent)
  2134. {
  2135. OMX_ERRORTYPE ret = OMX_ErrorNone;
  2136. SF_WAVE420L_IMPLEMEMT *pImp = (SF_WAVE420L_IMPLEMEMT *)pSfOMXComponent->componentImpl;
  2137. SF_OMX_CMD cmd;
  2138. void *pNull = NULL;
  2139. void *ThreadRet = NULL;
  2140. FunctionIn();
  2141. if(pImp->bThreadRunning)
  2142. {
  2143. pImp->bThreadRunning = 0;
  2144. /* enqueue null mean encoder thread cycle end */
  2145. SF_Queue_Enqueue(pImp->EmptyQueue, &pNull);
  2146. SF_Queue_Enqueue(pImp->FillQueue, &pNull);
  2147. pthread_join(pImp->pProcessThread->pthread, &ThreadRet);
  2148. LOG(SF_LOG_INFO, "Encoder thread end %ld\r\n", (Uint64)ThreadRet);
  2149. SF_Queue_Flush(pImp->EmptyQueue);
  2150. SF_Queue_Flush(pImp->FillQueue);
  2151. SF_Queue_Destroy(pImp->EmptyQueue);
  2152. SF_Queue_Destroy(pImp->FillQueue);
  2153. }
  2154. pImp->bCmdRunning = 0;
  2155. /* enqueue OMX_Command_StopThread mean cmd thread cycle end */
  2156. cmd.Cmd = OMX_Command_StopThread;
  2157. SF_Queue_Enqueue(pImp->CmdQueue, &cmd);
  2158. pthread_cancel(pImp->pCmdThread->pthread);
  2159. pthread_join(pImp->pCmdThread->pthread, &ThreadRet);
  2160. LOG(SF_LOG_INFO, "Cmd thread end %ld\r\n", (Uint64)ThreadRet);
  2161. SF_Queue_Destroy(pImp->CmdQueue);
  2162. SF_Queue_Destroy(pImp->pauseQ);
  2163. //Warp_VPU_DeInit(pImp, pImp->coreIdx);
  2164. nInstance--;
  2165. FunctionOut();
  2166. return ret;
  2167. }
  2168. SF_OMX_COMPONENT sf_enc_encoder_h265 = {
  2169. .componentName = "OMX.sf.video_encoder.hevc",
  2170. .libName = "libsfenc.so",
  2171. .pOMXComponent = NULL,
  2172. .SF_OMX_ComponentConstructor = SF_OMX_ComponentConstructor,
  2173. .SF_OMX_ComponentClear = SF_OMX_ComponentClear,
  2174. // .bitFormat = STD_HEVC,
  2175. .componentImpl = NULL,
  2176. .fwPath = "/lib/firmware/monet.bin",
  2177. .componentRule = "video_encoder.hevc"};