encoder_listener.c 9.8 KB

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