encoder_listener.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. // SPDX-License-Identifier: LGPL-2.1 OR BSD-3-Clause
  2. /*
  3. * Copyright (c) 2019, Chips&Media
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  16. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  19. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  22. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "cnm_app.h"
  27. #include "encoder_listener.h"
  28. #include "misc/debug.h"
  29. #include "misc/bw_monitor.h"
  30. static void HandleEncHandlingIntEvent(Component com, CNMComListenerHandlingInt* param, EncListenerContext* ctx)
  31. {
  32. if (ctx->bwCtx != NULL) {
  33. BWMonitorUpdate(ctx->bwCtx, 1);
  34. }
  35. }
  36. void HandleEncFullEvent(Component com, CNMComListenerEncFull* param, EncListenerContext* ctx)
  37. {
  38. }
  39. void HandleEncGetEncCloseEvent(Component com, CNMComListenerEncClose* param, EncListenerContext* ctx)
  40. {
  41. if (ctx->pfCtx != NULL) {
  42. PFMonitorRelease(ctx->pfCtx);
  43. }
  44. if (ctx->bwCtx != NULL) {
  45. BWMonitorRelease(ctx->bwCtx);
  46. }
  47. }
  48. void HandleEncCompleteSeqEvent(Component com, CNMComListenerEncCompleteSeq* param, EncListenerContext* ctx)
  49. {
  50. if (ctx->performance == TRUE) {
  51. Uint32 fps = (ctx->fps == 0) ? 30 : ctx->fps;
  52. ctx->pfCtx = PFMonitorSetup(ctx->coreIdx, 0, ctx->pfClock, fps, GetBasename((const char *)ctx->cfgFileName), 1);
  53. }
  54. if (ctx->bandwidth == TRUE) {
  55. ctx->bwCtx = BWMonitorSetup(param->handle, TRUE, GetBasename((const char *)ctx->cfgFileName));
  56. }
  57. }
  58. void HandleEncGetOutputEvent(Component com, CNMComListenerEncDone* param, EncListenerContext* ctx)
  59. {
  60. EncOutputInfo* output = param->output;
  61. if (output->reconFrameIndex == RECON_IDX_FLAG_ENC_END)
  62. return;
  63. if (ctx->pfCtx != NULL) {
  64. PFMonitorUpdate(ctx->coreIdx, ctx->pfCtx, output->frameCycle, output->encPrepareEndTick - output->encPrepareStartTick,
  65. output->encProcessingEndTick - output->encProcessingStartTick, output->encEncodeEndTick- output->encEncodeStartTick);
  66. }
  67. if (ctx->bwCtx != NULL) {
  68. BWMonitorUpdatePrint(ctx->bwCtx, output->picType);
  69. }
  70. if (ctx->headerEncDone[param->handle->instIndex] == FALSE) {
  71. ctx->headerEncDone[param->handle->instIndex] = TRUE;
  72. }
  73. if (ctx->match == FALSE) CNMAppStop();
  74. }
  75. void Coda9HandleEncGetOutputEvent(Component com, CNMComListenerEncDone* param, EncListenerContext* ctx)
  76. {
  77. EncOutputInfo* output = param->output;
  78. EncHandle handle = param->handle;
  79. if (output->reconFrameIndex == RECON_IDX_FLAG_ENC_END) return;
  80. if (ctx->pfCtx != NULL) {
  81. PFMonitorUpdate(ctx->coreIdx, ctx->pfCtx, output->frameCycle, output->encPrepareEndTick - output->encPrepareStartTick,
  82. output->encProcessingEndTick - output->encProcessingStartTick, output->encEncodeEndTick- output->encEncodeStartTick);
  83. }
  84. if (ctx->bwCtx != NULL) {
  85. BWMonitorUpdatePrint(ctx->bwCtx, output->picType);
  86. }
  87. if (output->bitstreamSize > 0) {
  88. if (ctx->es != NULL) {
  89. Uint8* buf = NULL;
  90. int compSize = output->bitstreamSize;
  91. PhysicalAddress addr;
  92. if (param->fullInterrupted == TRUE) {
  93. PhysicalAddress rd, wr;
  94. VPU_EncGetBitstreamBuffer(param->handle, &rd, &wr, &compSize);
  95. addr = rd;
  96. }
  97. else {
  98. addr = output->bitstreamBuffer;
  99. compSize = output->bitstreamSize;
  100. }
  101. buf = (Uint8*)osal_malloc(compSize);
  102. vdi_read_memory(ctx->coreIdx, addr, buf, compSize, ctx->streamEndian);
  103. if ( ctx->es != NULL ) {
  104. if ((ctx->match=Comparator_Act(ctx->es, buf, compSize)) == FALSE) {
  105. VLOG(ERR, "<%s:%d> INSTANCE #%d Bitstream Mismatch\n", __FUNCTION__, __LINE__, handle->instIndex);
  106. }
  107. }
  108. osal_free(buf);
  109. }
  110. }
  111. else if (TRUE == param->encodedStreamInfo.ringBufferEnable && NULL != param->encodedStreamInfo.encodedStreamBuf) {
  112. if ( ctx->es != NULL ) {
  113. if ((ctx->match=Comparator_Act(ctx->es, param->encodedStreamInfo.encodedStreamBuf, param->encodedStreamInfo.encodedStreamBufLength)) == FALSE) {
  114. VLOG(ERR, "<%s:%d> INSTANCE #%d Bitstream Mismatch\n", __FUNCTION__, __LINE__, handle->instIndex);
  115. }
  116. }
  117. }
  118. if (ctx->headerEncDone[param->handle->instIndex] == FALSE) {
  119. ctx->headerEncDone[param->handle->instIndex] = TRUE;
  120. }
  121. if (ctx->match == FALSE) CNMAppStop();
  122. }
  123. void Coda9HandleEncMakeHeaderEvent(Component com, CNMComListenerEncMakeHeader* param, EncListenerContext* ctx)
  124. {
  125. EncodedHeaderBufInfo encHeaderInfo = param->encHeaderInfo;
  126. if (0 < encHeaderInfo.encodedHeaderBufSize && NULL != encHeaderInfo.encodedHeaderBuf) {
  127. if (NULL != ctx->es) {
  128. if ((ctx->match=Comparator_Act(ctx->es, encHeaderInfo.encodedHeaderBuf, encHeaderInfo.encodedHeaderBufSize)) == FALSE) {
  129. VLOG(ERR, "<%s:%d> Header Mismatch\n", __FUNCTION__, __LINE__, param->handle->instIndex);
  130. VLOG(ERR, "<%s:%d> INSTANCE #%d Bitstream Mismatch\n", __FUNCTION__, __LINE__, param->handle->instIndex);
  131. }
  132. }
  133. if (encHeaderInfo.fp) {
  134. osal_fwrite(encHeaderInfo.encodedHeaderBuf, encHeaderInfo.encodedHeaderBufSize, 1, encHeaderInfo.fp);
  135. osal_fflush(encHeaderInfo.fp);
  136. }
  137. } else {
  138. VLOG(WARN, "<%s:%d> INSTANCE #%d Empty Header\n", __FUNCTION__, __LINE__, param->handle->instIndex);
  139. }
  140. if (ctx->match == FALSE) CNMAppStop();
  141. }
  142. void EncoderListener(Component com, Uint64 event, void* data, void* context)
  143. {
  144. int productId;
  145. EncHandle handle;
  146. #if defined(SUPPORT_MULTI_INSTANCE_TEST) || defined(SUPPORT_LOOK_AHEAD_RC)
  147. #else
  148. /* int key=0;
  149. if (osal_kbhit()) {
  150. key = osal_getch();
  151. osal_flush_ch();
  152. if (key) {
  153. if (key == 'q' || key == 'Q') {
  154. CNMAppStop();
  155. return;
  156. }
  157. }
  158. } */
  159. #endif
  160. switch (event) {
  161. case COMPONENT_EVENT_ENC_OPEN:
  162. break;
  163. case COMPONENT_EVENT_ENC_ISSUE_SEQ:
  164. break;
  165. case COMPONENT_EVENT_ENC_COMPLETE_SEQ:
  166. HandleEncCompleteSeqEvent(com, (CNMComListenerEncCompleteSeq*)data, (EncListenerContext*)context);
  167. break;
  168. case COMPONENT_EVENT_ENC_REGISTER_FB:
  169. break;
  170. case COMPONENT_EVENT_CODA9_ENC_MAKE_HEADER:
  171. Coda9HandleEncMakeHeaderEvent(com, (CNMComListenerEncMakeHeader*)data, (EncListenerContext*)context);
  172. break;
  173. case COMPONENT_EVENT_ENC_READY_ONE_FRAME:
  174. break;
  175. case COMPONENT_EVENT_ENC_START_ONE_FRAME:
  176. break;
  177. case COMPONENT_EVENT_ENC_HANDLING_INT:
  178. HandleEncHandlingIntEvent(com, (CNMComListenerHandlingInt*)data, (EncListenerContext*)context);
  179. break;
  180. case COMPONENT_EVENT_ENC_GET_OUTPUT_INFO:
  181. handle = ((CNMComListenerEncDone*)data)->handle;
  182. productId = VPU_GetProductId(VPU_HANDLE_CORE_INDEX(handle));
  183. if (TRUE == PRODUCT_ID_W_SERIES(productId)) {
  184. HandleEncGetOutputEvent(com, (CNMComListenerEncDone*)data, (EncListenerContext*)context);
  185. }
  186. else {
  187. Coda9HandleEncGetOutputEvent(com, (CNMComListenerEncDone*)data, (EncListenerContext*)context);
  188. }
  189. break;
  190. case COMPONENT_EVENT_ENC_ENCODED_ALL:
  191. break;
  192. case COMPONENT_EVENT_ENC_CLOSE:
  193. HandleEncGetEncCloseEvent(com, (CNMComListenerEncClose*)data, (EncListenerContext*)context);
  194. break;
  195. case COMPONENT_EVENT_ENC_FULL_INTERRUPT:
  196. HandleEncFullEvent(com, (CNMComListenerEncFull*)data, (EncListenerContext*)context);
  197. break;
  198. default:
  199. break;
  200. }
  201. }
  202. BOOL SetupEncListenerContext(EncListenerContext* ctx, CNMComponentConfig* config)
  203. {
  204. TestEncConfig* encConfig = &config->testEncConfig;
  205. osal_memset((void*)ctx, 0x00, sizeof(EncListenerContext));
  206. if (encConfig->compareType & (1 << MODE_COMP_ENCODED)) {
  207. if ((ctx->es=Comparator_Create(STREAM_COMPARE, encConfig->ref_stream_path, encConfig->cfgFileName)) == NULL) {
  208. VLOG(ERR, "%s:%d Failed to Comparator_Create(%s)\n", __FUNCTION__, __LINE__, encConfig->ref_stream_path);
  209. return FALSE;
  210. }
  211. }
  212. ctx->coreIdx = encConfig->coreIdx;
  213. ctx->streamEndian = encConfig->stream_endian;
  214. ctx->match = TRUE;
  215. ctx->matchOtherInfo= TRUE;
  216. ctx->performance = encConfig->performance;
  217. ctx->bandwidth = encConfig->bandwidth;
  218. ctx->pfClock = encConfig->pfClock;
  219. osal_memcpy(ctx->cfgFileName, encConfig->cfgFileName, sizeof(ctx->cfgFileName));
  220. ctx->ringBufferEnable = encConfig->ringBufferEnable;
  221. ctx->ringBufferWrapEnable = encConfig->ringBufferWrapEnable;
  222. return TRUE;
  223. }
  224. void ClearEncListenerContext(EncListenerContext* ctx)
  225. {
  226. if (ctx->es) Comparator_Destroy(ctx->es);
  227. }